authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-15 22:36:35-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-07-15 22:36:35-07:00
logd29dd5834b9d7386bb88e44bd2852428863cae81
tree6a1f15d341c00be937ef9308ec750b35c308a5c0
parentaf12596e8d728423e361e4755a6078c5ef8faf69

stage2: local consts

These are now supported enough that this example code hits the limitations of the register allocator: fn add(a: u32, b: u32) void { const c = a + b; // 7 const d = a + c; // 10 const e = d + b; // 14 assert(e == 14); } // error: TODO implement copyToNewRegister So now the next step is to implement register allocation as planned.

5 files changed, 72 insertions(+), 36 deletions(-)

src-self-hosted/Module.zig+33-10
......@@ -700,19 +700,22 @@ pub const Scope = struct {
700700 pub const GenZIR = struct {
701701 pub const base_tag: Tag = .gen_zir;
702702 base: Scope = Scope{ .tag = base_tag },
703 /// Parents can be: `GenZIR`, `ZIRModule`, `File`
704 parent: *Scope,
703705 decl: *Decl,
704706 arena: *Allocator,
707 /// The first N instructions in a function body ZIR are arg instructions.
705708 instructions: std.ArrayListUnmanaged(*zir.Inst) = .{},
706709 };
707710
708711 /// This structure lives as long as the AST generation of the Block
709 /// node that contains the variable. This struct's parents can be
710 /// other `LocalVar` and finally a `GenZIR` at the top.
712 /// node that contains the variable.
711713 pub const LocalVar = struct {
712714 pub const base_tag: Tag = .local_var;
713715 base: Scope = Scope{ .tag = base_tag },
714 gen_zir: *GenZIR,
716 /// Parents can be: `LocalVar`, `GenZIR`.
715717 parent: *Scope,
718 gen_zir: *GenZIR,
716719 name: []const u8,
717720 inst: *zir.Inst,
718721 };
......@@ -1164,6 +1167,7 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
11641167 var fn_type_scope: Scope.GenZIR = .{
11651168 .decl = decl,
11661169 .arena = &fn_type_scope_arena.allocator,
1170 .parent = decl.scope,
11671171 };
11681172 defer fn_type_scope.instructions.deinit(self.gpa);
11691173
......@@ -1241,12 +1245,32 @@ fn astGenAndAnalyzeDecl(self: *Module, decl: *Decl) !bool {
12411245 var gen_scope: Scope.GenZIR = .{
12421246 .decl = decl,
12431247 .arena = &gen_scope_arena.allocator,
1248 .parent = decl.scope,
12441249 };
12451250 defer gen_scope.instructions.deinit(self.gpa);
12461251
1252 // We need an instruction for each parameter, and they must be first in the body.
1253 try gen_scope.instructions.resize(self.gpa, fn_proto.params_len);
1254 var params_scope = &gen_scope.base;
1255 for (fn_proto.params()) |param, i| {
1256 const name_token = param.name_token.?;
1257 const src = tree.token_locs[name_token].start;
1258 const param_name = tree.tokenSlice(name_token);
1259 const arg = try newZIRInst(&gen_scope_arena.allocator, src, zir.Inst.Arg, .{}, .{});
1260 gen_scope.instructions.items[i] = &arg.base;
1261 const sub_scope = try gen_scope_arena.allocator.create(Scope.LocalVar);
1262 sub_scope.* = .{
1263 .parent = params_scope,
1264 .gen_zir = &gen_scope,
1265 .name = param_name,
1266 .inst = &arg.base,
1267 };
1268 params_scope = &sub_scope.base;
1269 }
1270
12471271 const body_block = body_node.cast(ast.Node.Block).?;
12481272
1249 try astgen.blockExpr(self, &gen_scope.base, body_block);
1273 try astgen.blockExpr(self, params_scope, body_block);
12501274
12511275 if (!fn_type.fnReturnType().isNoReturn() and (gen_scope.instructions.items.len == 0 or
12521276 !gen_scope.instructions.items[gen_scope.instructions.items.len - 1].tag.isNoReturn()))
......@@ -2236,17 +2260,16 @@ fn analyzeInstCompileError(self: *Module, scope: *Scope, inst: *zir.Inst.Compile
22362260fn analyzeInstArg(self: *Module, scope: *Scope, inst: *zir.Inst.Arg) InnerError!*Inst {
22372261 const b = try self.requireRuntimeBlock(scope, inst.base.src);
22382262 const fn_ty = b.func.?.owner_decl.typed_value.most_recent.typed_value.ty;
2263 const param_index = b.instructions.items.len;
22392264 const param_count = fn_ty.fnParamLen();
2240 if (inst.positionals.index >= param_count) {
2265 if (param_index >= param_count) {
22412266 return self.fail(scope, inst.base.src, "parameter index {} outside list of length {}", .{
2242 inst.positionals.index,
2267 param_index,
22432268 param_count,
22442269 });
22452270 }
2246 const param_type = fn_ty.fnParamType(inst.positionals.index);
2247 return self.addNewInstArgs(b, inst.base.src, param_type, Inst.Arg, .{
2248 .index = inst.positionals.index,
2249 });
2271 const param_type = fn_ty.fnParamType(param_index);
2272 return self.addNewInstArgs(b, inst.base.src, param_type, Inst.Arg, {});
22502273}
22512274
22522275fn analyzeInstBlock(self: *Module, scope: *Scope, inst: *zir.Inst.Block) InnerError!*Inst {
src-self-hosted/astgen.zig+25-14
......@@ -160,6 +160,7 @@ fn ifExpr(mod: *Module, scope: *Scope, if_node: *ast.Node.If) InnerError!*zir.In
160160 }
161161 }
162162 var block_scope: Scope.GenZIR = .{
163 .parent = scope,
163164 .decl = scope.decl().?,
164165 .arena = scope.arena(),
165166 .instructions = .{},
......@@ -180,6 +181,7 @@ fn ifExpr(mod: *Module, scope: *Scope, if_node: *ast.Node.If) InnerError!*zir.In
180181 .instructions = try block_scope.arena.dupe(*zir.Inst, block_scope.instructions.items),
181182 });
182183 var then_scope: Scope.GenZIR = .{
184 .parent = scope,
183185 .decl = block_scope.decl,
184186 .arena = block_scope.arena,
185187 .instructions = .{},
......@@ -199,6 +201,7 @@ fn ifExpr(mod: *Module, scope: *Scope, if_node: *ast.Node.If) InnerError!*zir.In
199201 };
200202
201203 var else_scope: Scope.GenZIR = .{
204 .parent = scope,
202205 .decl = block_scope.decl,
203206 .arena = block_scope.arena,
204207 .instructions = .{},
......@@ -250,7 +253,11 @@ fn controlFlowExpr(
250253}
251254
252255fn identifier(mod: *Module, scope: *Scope, ident: *ast.Node.Identifier) InnerError!*zir.Inst {
256 const tracy = trace(@src());
257 defer tracy.end();
258
253259 const tree = scope.tree();
260 // TODO implement @"aoeu" identifiers
254261 const ident_name = tree.tokenSlice(ident.token);
255262 const src = tree.token_locs[ident.token].start;
256263 if (mem.eql(u8, ident_name, "_")) {
......@@ -288,23 +295,27 @@ fn identifier(mod: *Module, scope: *Scope, ident: *ast.Node.Identifier) InnerErr
288295 }
289296 }
290297
291 if (mod.lookupDeclName(scope, ident_name)) |decl| {
292 return try mod.addZIRInst(scope, src, zir.Inst.DeclValInModule, .{ .decl = decl }, .{});
298 // Local variables, including function parameters.
299 {
300 var s = scope;
301 while (true) switch (s.tag) {
302 .local_var => {
303 const local_var = s.cast(Scope.LocalVar).?;
304 if (mem.eql(u8, local_var.name, ident_name)) {
305 return local_var.inst;
306 }
307 s = local_var.parent;
308 },
309 .gen_zir => s = s.cast(Scope.GenZIR).?.parent,
310 else => break,
311 };
293312 }
294313
295 // Function parameter
296 if (scope.decl()) |decl| {
297 if (tree.root_node.decls()[decl.src_index].cast(ast.Node.FnProto)) |fn_proto| {
298 for (fn_proto.params()) |param, i| {
299 const param_name = tree.tokenSlice(param.name_token.?);
300 if (mem.eql(u8, param_name, ident_name)) {
301 return try mod.addZIRInst(scope, src, zir.Inst.Arg, .{ .index = i }, .{});
302 }
303 }
304 }
314 if (mod.lookupDeclName(scope, ident_name)) |decl| {
315 return try mod.addZIRInst(scope, src, zir.Inst.DeclValInModule, .{ .decl = decl }, .{});
305316 }
306317
307 return mod.failNode(scope, &ident.base, "TODO implement local variable identifier lookup", .{});
318 return mod.failNode(scope, &ident.base, "use of undeclared identifier '{}'", .{ident_name});
308319}
309320
310321fn stringLiteral(mod: *Module, scope: *Scope, str_lit: *ast.Node.StringLiteral) InnerError!*zir.Inst {
......@@ -434,7 +445,7 @@ fn callExpr(mod: *Module, scope: *Scope, node: *ast.Node.Call) InnerError!*zir.I
434445 const lhs = try expr(mod, scope, node.lhs);
435446
436447 const param_nodes = node.params();
437 const args = try scope.cast(Scope.GenZIR).?.arena.alloc(*zir.Inst, param_nodes.len);
448 const args = try scope.getGenZIR().arena.alloc(*zir.Inst, param_nodes.len);
438449 for (param_nodes) |param_node, i| {
439450 args[i] = try expr(mod, scope, param_node);
440451 }
src-self-hosted/codegen.zig+5-1
......@@ -73,6 +73,7 @@ pub fn generateSymbol(
7373 .code = code,
7474 .err_msg = null,
7575 .args = mc_args,
76 .arg_index = 0,
7677 .branch_stack = &branch_stack,
7778 .src = src,
7879 };
......@@ -255,6 +256,7 @@ const Function = struct {
255256 code: *std.ArrayList(u8),
256257 err_msg: ?*ErrorMsg,
257258 args: []MCValue,
259 arg_index: usize,
258260 src: usize,
259261
260262 /// Whenever there is a runtime branch, we push a Branch onto this stack,
......@@ -603,7 +605,9 @@ const Function = struct {
603605 }
604606
605607 fn genArg(self: *Function, inst: *ir.Inst.Arg) !MCValue {
606 return self.args[inst.args.index];
608 const i = self.arg_index;
609 self.arg_index += 1;
610 return self.args[i];
607611 }
608612
609613 fn genBreakpoint(self: *Function, src: usize, comptime arch: std.Target.Cpu.Arch) !MCValue {
src-self-hosted/ir.zig+1-4
......@@ -101,10 +101,7 @@ pub const Inst = struct {
101101 pub const Arg = struct {
102102 pub const base_tag = Tag.arg;
103103 base: Inst,
104
105 args: struct {
106 index: usize,
107 },
104 args: void,
108105 };
109106
110107 pub const Assembly = struct {
src-self-hosted/zir.zig+8-7
......@@ -34,7 +34,8 @@ pub const Inst = struct {
3434
3535 /// These names are used directly as the instruction names in the text format.
3636 pub const Tag = enum {
37 /// Function parameter value.
37 /// Function parameter value. These must be first in a function's main block,
38 /// in respective order with the parameters.
3839 arg,
3940 /// A labeled block of code, which can return a value.
4041 block,
......@@ -184,9 +185,7 @@ pub const Inst = struct {
184185 pub const base_tag = Tag.arg;
185186 base: Inst,
186187
187 positionals: struct {
188 index: usize,
189 },
188 positionals: struct {},
190189 kw_args: struct {},
191190 };
192191
......@@ -1384,15 +1383,17 @@ const EmitZIR = struct {
13841383 for (src_decls.items) |ir_decl| {
13851384 switch (ir_decl.analysis) {
13861385 .unreferenced => continue,
1386
13871387 .complete => {},
1388 .codegen_failure => {}, // We still can emit the ZIR.
1389 .codegen_failure_retryable => {}, // We still can emit the ZIR.
1390
13881391 .in_progress => unreachable,
13891392 .outdated => unreachable,
13901393
13911394 .sema_failure,
13921395 .sema_failure_retryable,
1393 .codegen_failure,
13941396 .dependency_failure,
1395 .codegen_failure_retryable,
13961397 => if (self.old_module.failed_decls.get(ir_decl)) |err_msg| {
13971398 const fail_inst = try self.arena.allocator.create(Inst.CompileError);
13981399 fail_inst.* = .{
......@@ -1728,7 +1729,7 @@ const EmitZIR = struct {
17281729 .src = inst.src,
17291730 .tag = Inst.Arg.base_tag,
17301731 },
1731 .positionals = .{ .index = old_inst.args.index },
1732 .positionals = .{},
17321733 .kw_args = .{},
17331734 };
17341735 break :blk &new_inst.base;