authorgravatar for jacoblevgw@gmail.comJacob G-W <jacoblevgw@gmail.com> 2021-03-21 22:30:52-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-21 20:10:27-07:00
log7a55671b89c85d412aa37dfa18fc6faaed1eba22
treec745b2f484a70b2fd564d061c24f3549e266d803
parent5769c963e0748088ba2636e870cbc6f887d10454

zir-memory-layout: astgen: varDecl


1 files changed, 31 insertions(+), 27 deletions(-)

src/astgen.zig+31-27
......@@ -1062,8 +1062,6 @@ fn varDecl(
10621062 block_arena: *Allocator,
10631063 var_decl: ast.full.VarDecl,
10641064) InnerError!*Scope {
1065 if (true) @panic("TODO update for zir-memory-layout");
1066
10671065 if (var_decl.comptime_token) |comptime_token| {
10681066 return mod.failTok(scope, comptime_token, "TODO implement comptime locals", .{});
10691067 }
......@@ -1153,23 +1151,21 @@ fn varDecl(
11531151 // result location pointer.
11541152 var init_scope: Scope.GenZir = .{
11551153 .parent = scope,
1156 .decl = scope.ownerDecl().?,
1157 .arena = scope.arena(),
11581154 .force_comptime = scope.isComptime(),
1159 .instructions = .{},
1155 .zir_code = scope.getGenZir().zir_code,
11601156 };
11611157 defer init_scope.instructions.deinit(mod.gpa);
11621158
1163 var resolve_inferred_alloc: ?*zir.Inst = null;
1164 var opt_type_inst: ?*zir.Inst = null;
1159 var resolve_inferred_alloc: ?zir.Inst.Ref = null;
1160 var opt_type_inst: ?zir.Inst.Ref = null;
11651161 if (var_decl.ast.type_node != 0) {
11661162 const type_inst = try typeExpr(mod, &init_scope.base, var_decl.ast.type_node);
11671163 opt_type_inst = type_inst;
1168 init_scope.rl_ptr = try addZIRUnOp(mod, &init_scope.base, name_src, .alloc, type_inst);
1164 init_scope.rl_ptr = try init_scope.addUnNode(.alloc, type_inst, node);
11691165 } else {
1170 const alloc = try addZIRNoOpT(mod, &init_scope.base, name_src, .alloc_inferred);
1171 resolve_inferred_alloc = &alloc.base;
1172 init_scope.rl_ptr = &alloc.base;
1166 const alloc = try init_scope.addUnNode(.alloc_inferred, undefined, node);
1167 resolve_inferred_alloc = alloc;
1168 init_scope.rl_ptr = alloc;
11731169 }
11741170 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
11751171 const init_inst = try expr(mod, &init_scope.base, init_result_loc, var_decl.ast.init_node);
......@@ -1182,15 +1178,19 @@ fn varDecl(
11821178 const expected_len = parent_zir.items.len + init_scope.instructions.items.len - 2;
11831179 try parent_zir.ensureCapacity(mod.gpa, expected_len);
11841180 for (init_scope.instructions.items) |src_inst| {
1185 if (src_inst == init_scope.rl_ptr.?) continue;
1186 if (src_inst.castTag(.store_to_block_ptr)) |store| {
1187 if (store.positionals.lhs == init_scope.rl_ptr.?) continue;
1181 if (src_inst == init_scope.rl_ptr) continue;
1182 const src_inst_dtag = init_scope.zir_code.instructions.get(src_inst);
1183 if (src_inst_dtag.tag == .store_to_block_ptr) {
1184 if (src_inst_dtag.data.bin.lhs == init_scope.rl_ptr) continue;
11881185 }
11891186 parent_zir.appendAssumeCapacity(src_inst);
11901187 }
11911188 assert(parent_zir.items.len == expected_len);
11921189 const casted_init = if (opt_type_inst) |type_inst|
1193 try addZIRBinOp(mod, scope, type_inst.src, .as, type_inst, init_inst)
1190 try gz.addPlNode(.as_node, var_decl.ast.type_node, zir.Inst.As{
1191 .dest_type = type_inst,
1192 .operand = init_inst,
1193 })
11941194 else
11951195 init_inst;
11961196
......@@ -1200,6 +1200,7 @@ fn varDecl(
12001200 .gen_zir = gz,
12011201 .name = ident_name,
12021202 .inst = casted_init,
1203 .src = gz.nodeSrcLoc(node),
12031204 };
12041205 return &sub_scope.base;
12051206 }
......@@ -1210,43 +1211,46 @@ fn varDecl(
12101211 const expected_len = parent_zir.items.len + init_scope.instructions.items.len;
12111212 try parent_zir.ensureCapacity(mod.gpa, expected_len);
12121213 for (init_scope.instructions.items) |src_inst| {
1213 if (src_inst.castTag(.store_to_block_ptr)) |store| {
1214 if (store.positionals.lhs == init_scope.rl_ptr.?) {
1215 src_inst.tag = .store_to_inferred_ptr;
1214 const src_inst_dtag = init_scope.zir_code.instructions.get(src_inst);
1215 if (src_inst_dtag.tag == .store_to_block_ptr) {
1216 if (src_inst_dtag.data.bin.lhs == init_scope.rl_ptr) {
1217 init_scope.zir_code.instructions.items(.tag)[src_inst] = .store_to_inferred_ptr;
12161218 }
12171219 }
12181220 parent_zir.appendAssumeCapacity(src_inst);
12191221 }
12201222 assert(parent_zir.items.len == expected_len);
12211223 if (resolve_inferred_alloc) |inst| {
1222 _ = try addZIRUnOp(mod, scope, name_src, .resolve_inferred_alloc, inst);
1224 _ = try gz.addUnTok(.resolve_inferred_alloc, inst, tree.firstToken(node));
12231225 }
12241226 const sub_scope = try block_arena.create(Scope.LocalPtr);
12251227 sub_scope.* = .{
12261228 .parent = scope,
12271229 .gen_zir = gz,
12281230 .name = ident_name,
1229 .ptr = init_scope.rl_ptr.?,
1231 .ptr = init_scope.rl_ptr,
1232 .src = gz.nodeSrcLoc(node),
12301233 };
12311234 return &sub_scope.base;
12321235 },
12331236 .keyword_var => {
1234 var resolve_inferred_alloc: ?*zir.Inst = null;
1237 var resolve_inferred_alloc: ?zir.Inst.Ref = null;
12351238 const var_data: struct {
12361239 result_loc: ResultLoc,
1237 alloc: *zir.Inst,
1240 alloc: zir.Inst.Ref,
12381241 } = if (var_decl.ast.type_node != 0) a: {
12391242 const type_inst = try typeExpr(mod, scope, var_decl.ast.type_node);
1240 const alloc = try addZIRUnOp(mod, scope, name_src, .alloc_mut, type_inst);
1243
1244 const alloc = try gz.addUnNode(.alloc_mut, type_inst, node);
12411245 break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } };
12421246 } else a: {
1243 const alloc = try addZIRNoOpT(mod, scope, name_src, .alloc_inferred_mut);
1244 resolve_inferred_alloc = &alloc.base;
1245 break :a .{ .alloc = &alloc.base, .result_loc = .{ .inferred_ptr = alloc } };
1247 const alloc = try gz.addUnNode(.alloc_inferred_mut, undefined, node);
1248 resolve_inferred_alloc = alloc;
1249 break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc } };
12461250 };
12471251 const init_inst = try expr(mod, scope, var_data.result_loc, var_decl.ast.init_node);
12481252 if (resolve_inferred_alloc) |inst| {
1249 _ = try addZIRUnOp(mod, scope, name_src, .resolve_inferred_alloc, inst);
1253 _ = try gz.addUnNode(.resolve_inferred_alloc, inst, node);
12501254 }
12511255 const sub_scope = try block_arena.create(Scope.LocalPtr);
12521256 sub_scope.* = .{