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(...@@ -1062,8 +1062,6 @@ fn varDecl(
1062 block_arena: *Allocator,1062 block_arena: *Allocator,
1063 var_decl: ast.full.VarDecl,1063 var_decl: ast.full.VarDecl,
1064) InnerError!*Scope {1064) InnerError!*Scope {
1065 if (true) @panic("TODO update for zir-memory-layout");
1066
1067 if (var_decl.comptime_token) |comptime_token| {1065 if (var_decl.comptime_token) |comptime_token| {
1068 return mod.failTok(scope, comptime_token, "TODO implement comptime locals", .{});1066 return mod.failTok(scope, comptime_token, "TODO implement comptime locals", .{});
1069 }1067 }
...@@ -1153,23 +1151,21 @@ fn varDecl(...@@ -1153,23 +1151,21 @@ fn varDecl(
1153 // result location pointer.1151 // result location pointer.
1154 var init_scope: Scope.GenZir = .{1152 var init_scope: Scope.GenZir = .{
1155 .parent = scope,1153 .parent = scope,
1156 .decl = scope.ownerDecl().?,
1157 .arena = scope.arena(),
1158 .force_comptime = scope.isComptime(),1154 .force_comptime = scope.isComptime(),
1159 .instructions = .{},1155 .zir_code = scope.getGenZir().zir_code,
1160 };1156 };
1161 defer init_scope.instructions.deinit(mod.gpa);1157 defer init_scope.instructions.deinit(mod.gpa);
11621158
1163 var resolve_inferred_alloc: ?*zir.Inst = null;1159 var resolve_inferred_alloc: ?zir.Inst.Ref = null;
1164 var opt_type_inst: ?*zir.Inst = null;1160 var opt_type_inst: ?zir.Inst.Ref = null;
1165 if (var_decl.ast.type_node != 0) {1161 if (var_decl.ast.type_node != 0) {
1166 const type_inst = try typeExpr(mod, &init_scope.base, var_decl.ast.type_node);1162 const type_inst = try typeExpr(mod, &init_scope.base, var_decl.ast.type_node);
1167 opt_type_inst = type_inst;1163 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);
1169 } else {1165 } else {
1170 const alloc = try addZIRNoOpT(mod, &init_scope.base, name_src, .alloc_inferred);1166 const alloc = try init_scope.addUnNode(.alloc_inferred, undefined, node);
1171 resolve_inferred_alloc = &alloc.base;1167 resolve_inferred_alloc = alloc;
1172 init_scope.rl_ptr = &alloc.base;1168 init_scope.rl_ptr = alloc;
1173 }1169 }
1174 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };1170 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
1175 const init_inst = try expr(mod, &init_scope.base, init_result_loc, var_decl.ast.init_node);1171 const init_inst = try expr(mod, &init_scope.base, init_result_loc, var_decl.ast.init_node);
...@@ -1182,15 +1178,19 @@ fn varDecl(...@@ -1182,15 +1178,19 @@ fn varDecl(
1182 const expected_len = parent_zir.items.len + init_scope.instructions.items.len - 2;1178 const expected_len = parent_zir.items.len + init_scope.instructions.items.len - 2;
1183 try parent_zir.ensureCapacity(mod.gpa, expected_len);1179 try parent_zir.ensureCapacity(mod.gpa, expected_len);
1184 for (init_scope.instructions.items) |src_inst| {1180 for (init_scope.instructions.items) |src_inst| {
1185 if (src_inst == init_scope.rl_ptr.?) continue;1181 if (src_inst == init_scope.rl_ptr) continue;
1186 if (src_inst.castTag(.store_to_block_ptr)) |store| {1182 const src_inst_dtag = init_scope.zir_code.instructions.get(src_inst);
1187 if (store.positionals.lhs == init_scope.rl_ptr.?) continue;1183 if (src_inst_dtag.tag == .store_to_block_ptr) {
1184 if (src_inst_dtag.data.bin.lhs == init_scope.rl_ptr) continue;
1188 }1185 }
1189 parent_zir.appendAssumeCapacity(src_inst);1186 parent_zir.appendAssumeCapacity(src_inst);
1190 }1187 }
1191 assert(parent_zir.items.len == expected_len);1188 assert(parent_zir.items.len == expected_len);
1192 const casted_init = if (opt_type_inst) |type_inst|1189 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 })
1194 else1194 else
1195 init_inst;1195 init_inst;
11961196
...@@ -1200,6 +1200,7 @@ fn varDecl(...@@ -1200,6 +1200,7 @@ fn varDecl(
1200 .gen_zir = gz,1200 .gen_zir = gz,
1201 .name = ident_name,1201 .name = ident_name,
1202 .inst = casted_init,1202 .inst = casted_init,
1203 .src = gz.nodeSrcLoc(node),
1203 };1204 };
1204 return &sub_scope.base;1205 return &sub_scope.base;
1205 }1206 }
...@@ -1210,43 +1211,46 @@ fn varDecl(...@@ -1210,43 +1211,46 @@ fn varDecl(
1210 const expected_len = parent_zir.items.len + init_scope.instructions.items.len;1211 const expected_len = parent_zir.items.len + init_scope.instructions.items.len;
1211 try parent_zir.ensureCapacity(mod.gpa, expected_len);1212 try parent_zir.ensureCapacity(mod.gpa, expected_len);
1212 for (init_scope.instructions.items) |src_inst| {1213 for (init_scope.instructions.items) |src_inst| {
1213 if (src_inst.castTag(.store_to_block_ptr)) |store| {1214 const src_inst_dtag = init_scope.zir_code.instructions.get(src_inst);
1214 if (store.positionals.lhs == init_scope.rl_ptr.?) {1215 if (src_inst_dtag.tag == .store_to_block_ptr) {
1215 src_inst.tag = .store_to_inferred_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;
1216 }1218 }
1217 }1219 }
1218 parent_zir.appendAssumeCapacity(src_inst);1220 parent_zir.appendAssumeCapacity(src_inst);
1219 }1221 }
1220 assert(parent_zir.items.len == expected_len);1222 assert(parent_zir.items.len == expected_len);
1221 if (resolve_inferred_alloc) |inst| {1223 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));
1223 }1225 }
1224 const sub_scope = try block_arena.create(Scope.LocalPtr);1226 const sub_scope = try block_arena.create(Scope.LocalPtr);
1225 sub_scope.* = .{1227 sub_scope.* = .{
1226 .parent = scope,1228 .parent = scope,
1227 .gen_zir = gz,1229 .gen_zir = gz,
1228 .name = ident_name,1230 .name = ident_name,
1229 .ptr = init_scope.rl_ptr.?,1231 .ptr = init_scope.rl_ptr,
1232 .src = gz.nodeSrcLoc(node),
1230 };1233 };
1231 return &sub_scope.base;1234 return &sub_scope.base;
1232 },1235 },
1233 .keyword_var => {1236 .keyword_var => {
1234 var resolve_inferred_alloc: ?*zir.Inst = null;1237 var resolve_inferred_alloc: ?zir.Inst.Ref = null;
1235 const var_data: struct {1238 const var_data: struct {
1236 result_loc: ResultLoc,1239 result_loc: ResultLoc,
1237 alloc: *zir.Inst,1240 alloc: zir.Inst.Ref,
1238 } = if (var_decl.ast.type_node != 0) a: {1241 } = if (var_decl.ast.type_node != 0) a: {
1239 const type_inst = try typeExpr(mod, scope, var_decl.ast.type_node);1242 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);
1241 break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } };1245 break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } };
1242 } else a: {1246 } else a: {
1243 const alloc = try addZIRNoOpT(mod, scope, name_src, .alloc_inferred_mut);1247 const alloc = try gz.addUnNode(.alloc_inferred_mut, undefined, node);
1244 resolve_inferred_alloc = &alloc.base;1248 resolve_inferred_alloc = alloc;
1245 break :a .{ .alloc = &alloc.base, .result_loc = .{ .inferred_ptr = alloc } };1249 break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc } };
1246 };1250 };
1247 const init_inst = try expr(mod, scope, var_data.result_loc, var_decl.ast.init_node);1251 const init_inst = try expr(mod, scope, var_data.result_loc, var_decl.ast.init_node);
1248 if (resolve_inferred_alloc) |inst| {1252 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);
1250 }1254 }
1251 const sub_scope = try block_arena.create(Scope.LocalPtr);1255 const sub_scope = try block_arena.create(Scope.LocalPtr);
1252 sub_scope.* = .{1256 sub_scope.* = .{