authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-23 16:47:41-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-23 16:47:41-07:00
logbf7c3e9355530680b066a573c1743f9d570fddc5
treec38db95a607a14ac89df17079ad465845cf9a6b3
parentbe673e67937bbc3e2c74591f8f447f848b2a566a

astgen: fixups regarding var decls and rl_ptr


3 files changed, 19 insertions(+), 40 deletions(-)

src/Module.zig+4-27
......@@ -930,7 +930,7 @@ pub const Scope = struct {
930930 /// Only valid when setBlockResultLoc is called.
931931 break_result_loc: astgen.ResultLoc = undefined,
932932 /// When a block has a pointer result location, here it is.
933 rl_ptr: zir.Inst.Index = 0,
933 rl_ptr: zir.Inst.Ref = 0,
934934 /// Keeps track of how many branches of a block did not actually
935935 /// consume the result location. astgen uses this to figure out
936936 /// whether to rely on break instructions or writing to the result
......@@ -1136,18 +1136,8 @@ pub const Scope = struct {
11361136 /// Absolute node index. This function does the conversion to offset from Decl.
11371137 src_node: ast.Node.Index,
11381138 ) !zir.Inst.Ref {
1139 return gz.zir_code.ref_start_index + try gz.addUnNodeAsIndex(tag, operand, src_node);
1140 }
1141
1142 pub fn addUnNodeAsIndex(
1143 gz: *GenZir,
1144 tag: zir.Inst.Tag,
1145 operand: zir.Inst.Ref,
1146 /// Absolute node index. This function does the conversion to offset from Decl.
1147 src_node: ast.Node.Index,
1148 ) !zir.Inst.Index {
11491139 assert(operand != 0);
1150 return gz.addAsIndex(.{
1140 return gz.add(.{
11511141 .tag = tag,
11521142 .data = .{ .un_node = .{
11531143 .operand = operand,
......@@ -1245,18 +1235,9 @@ pub const Scope = struct {
12451235 lhs: zir.Inst.Ref,
12461236 rhs: zir.Inst.Ref,
12471237 ) !zir.Inst.Ref {
1248 return gz.zir_code.ref_start_index + try gz.addBinAsIndex(tag, lhs, rhs);
1249 }
1250
1251 pub fn addBinAsIndex(
1252 gz: *GenZir,
1253 tag: zir.Inst.Tag,
1254 lhs: zir.Inst.Ref,
1255 rhs: zir.Inst.Ref,
1256 ) !zir.Inst.Index {
12571238 assert(lhs != 0);
12581239 assert(rhs != 0);
1259 return gz.addAsIndex(.{
1240 return gz.add(.{
12601241 .tag = tag,
12611242 .data = .{ .bin = .{
12621243 .lhs = lhs,
......@@ -1336,10 +1317,6 @@ pub const Scope = struct {
13361317 }
13371318
13381319 pub fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref {
1339 return gz.zir_code.ref_start_index + try gz.addAsIndex(inst);
1340 }
1341
1342 pub fn addAsIndex(gz: *GenZir, inst: zir.Inst) !zir.Inst.Index {
13431320 const gpa = gz.zir_code.gpa;
13441321 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
13451322 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
......@@ -1347,7 +1324,7 @@ pub const Scope = struct {
13471324 const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
13481325 gz.zir_code.instructions.appendAssumeCapacity(inst);
13491326 gz.instructions.appendAssumeCapacity(new_index);
1350 return new_index;
1327 return gz.zir_code.ref_start_index + new_index;
13511328 }
13521329 };
13531330
src/astgen.zig+14-12
......@@ -1195,6 +1195,7 @@ fn varDecl(
11951195 return mod.failNode(scope, var_decl.ast.align_node, "TODO implement alignment on locals", .{});
11961196 }
11971197 const gz = scope.getGenZir();
1198 const wzc = gz.zir_code;
11981199 const tree = scope.tree();
11991200 const token_tags = tree.tokens.items(.tag);
12001201
......@@ -1276,7 +1277,7 @@ fn varDecl(
12761277 var init_scope: Scope.GenZir = .{
12771278 .parent = scope,
12781279 .force_comptime = gz.force_comptime,
1279 .zir_code = gz.zir_code,
1280 .zir_code = wzc,
12801281 };
12811282 defer init_scope.instructions.deinit(mod.gpa);
12821283
......@@ -1285,16 +1286,16 @@ fn varDecl(
12851286 if (var_decl.ast.type_node != 0) {
12861287 const type_inst = try typeExpr(mod, &init_scope.base, var_decl.ast.type_node);
12871288 opt_type_inst = type_inst;
1288 init_scope.rl_ptr = try init_scope.addUnNodeAsIndex(.alloc, type_inst, node);
1289 init_scope.rl_ptr = try init_scope.addUnNode(.alloc, type_inst, node);
12891290 } else {
1290 const alloc = try init_scope.addUnNodeAsIndex(.alloc_inferred, undefined, node);
1291 resolve_inferred_alloc = init_scope.zir_code.ref_start_index + alloc;
1291 const alloc = try init_scope.addUnNode(.alloc_inferred, undefined, node);
1292 resolve_inferred_alloc = alloc;
12921293 init_scope.rl_ptr = alloc;
12931294 }
12941295 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
12951296 const init_inst = try expr(mod, &init_scope.base, init_result_loc, var_decl.ast.init_node);
1296 const zir_tags = gz.zir_code.instructions.items(.tag);
1297 const zir_datas = gz.zir_code.instructions.items(.data);
1297 const zir_tags = wzc.instructions.items(.tag);
1298 const zir_datas = wzc.instructions.items(.data);
12981299
12991300 const parent_zir = &gz.instructions;
13001301 if (init_scope.rvalue_rl_count == 1) {
......@@ -1305,7 +1306,7 @@ fn varDecl(
13051306 const expected_len = parent_zir.items.len + init_scope.instructions.items.len - 2;
13061307 try parent_zir.ensureCapacity(mod.gpa, expected_len);
13071308 for (init_scope.instructions.items) |src_inst| {
1308 if (src_inst == init_scope.rl_ptr) continue;
1309 if (wzc.ref_start_index + src_inst == init_scope.rl_ptr) continue;
13091310 if (zir_tags[src_inst] == .store_to_block_ptr) {
13101311 if (zir_datas[src_inst].bin.lhs == init_scope.rl_ptr) continue;
13111312 }
......@@ -3192,26 +3193,27 @@ fn asRlPtr(
31923193 // result location. If it does, elide the coerce_result_ptr instruction
31933194 // as well as the store instruction, instead passing the result as an rvalue.
31943195 const parent_gz = scope.getGenZir();
3196 const wzc = parent_gz.zir_code;
31953197
31963198 var as_scope: Scope.GenZir = .{
31973199 .parent = scope,
3198 .zir_code = parent_gz.zir_code,
3200 .zir_code = wzc,
31993201 .force_comptime = parent_gz.force_comptime,
32003202 .instructions = .{},
32013203 };
32023204 defer as_scope.instructions.deinit(mod.gpa);
32033205
3204 as_scope.rl_ptr = try as_scope.addBinAsIndex(.coerce_result_ptr, dest_type, result_ptr);
3206 as_scope.rl_ptr = try as_scope.addBin(.coerce_result_ptr, dest_type, result_ptr);
32053207 const result = try expr(mod, &as_scope.base, .{ .block_ptr = &as_scope }, operand_node);
32063208 const parent_zir = &parent_gz.instructions;
32073209 if (as_scope.rvalue_rl_count == 1) {
32083210 // Busted! This expression didn't actually need a pointer.
3209 const zir_tags = parent_gz.zir_code.instructions.items(.tag);
3210 const zir_datas = parent_gz.zir_code.instructions.items(.data);
3211 const zir_tags = wzc.instructions.items(.tag);
3212 const zir_datas = wzc.instructions.items(.data);
32113213 const expected_len = parent_zir.items.len + as_scope.instructions.items.len - 2;
32123214 try parent_zir.ensureCapacity(mod.gpa, expected_len);
32133215 for (as_scope.instructions.items) |src_inst| {
3214 if (src_inst == as_scope.rl_ptr) continue;
3216 if (wzc.ref_start_index + src_inst == as_scope.rl_ptr) continue;
32153217 if (zir_tags[src_inst] == .store_to_block_ptr) {
32163218 if (zir_datas[src_inst].bin.lhs == as_scope.rl_ptr) continue;
32173219 }
test/stage2/test.zig+1-1
......@@ -355,7 +355,7 @@ pub fn addCases(ctx: *TestContext) !void {
355355 \\ const z = @TypeOf(true, 1);
356356 \\ unreachable;
357357 \\}
358 , &[_][]const u8{":2:29: error: incompatible types: 'bool' and 'comptime_int'"});
358 , &[_][]const u8{":2:15: error: incompatible types: 'bool' and 'comptime_int'"});
359359 }
360360
361361 {