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 {...@@ -930,7 +930,7 @@ pub const Scope = struct {
930 /// Only valid when setBlockResultLoc is called.930 /// Only valid when setBlockResultLoc is called.
931 break_result_loc: astgen.ResultLoc = undefined,931 break_result_loc: astgen.ResultLoc = undefined,
932 /// When a block has a pointer result location, here it is.932 /// 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,
934 /// Keeps track of how many branches of a block did not actually934 /// Keeps track of how many branches of a block did not actually
935 /// consume the result location. astgen uses this to figure out935 /// consume the result location. astgen uses this to figure out
936 /// whether to rely on break instructions or writing to the result936 /// whether to rely on break instructions or writing to the result
...@@ -1136,18 +1136,8 @@ pub const Scope = struct {...@@ -1136,18 +1136,8 @@ pub const Scope = struct {
1136 /// Absolute node index. This function does the conversion to offset from Decl.1136 /// Absolute node index. This function does the conversion to offset from Decl.
1137 src_node: ast.Node.Index,1137 src_node: ast.Node.Index,
1138 ) !zir.Inst.Ref {1138 ) !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 {
1149 assert(operand != 0);1139 assert(operand != 0);
1150 return gz.addAsIndex(.{1140 return gz.add(.{
1151 .tag = tag,1141 .tag = tag,
1152 .data = .{ .un_node = .{1142 .data = .{ .un_node = .{
1153 .operand = operand,1143 .operand = operand,
...@@ -1245,18 +1235,9 @@ pub const Scope = struct {...@@ -1245,18 +1235,9 @@ pub const Scope = struct {
1245 lhs: zir.Inst.Ref,1235 lhs: zir.Inst.Ref,
1246 rhs: zir.Inst.Ref,1236 rhs: zir.Inst.Ref,
1247 ) !zir.Inst.Ref {1237 ) !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 {
1257 assert(lhs != 0);1238 assert(lhs != 0);
1258 assert(rhs != 0);1239 assert(rhs != 0);
1259 return gz.addAsIndex(.{1240 return gz.add(.{
1260 .tag = tag,1241 .tag = tag,
1261 .data = .{ .bin = .{1242 .data = .{ .bin = .{
1262 .lhs = lhs,1243 .lhs = lhs,
...@@ -1336,10 +1317,6 @@ pub const Scope = struct {...@@ -1336,10 +1317,6 @@ pub const Scope = struct {
1336 }1317 }
13371318
1338 pub fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref {1319 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 {
1343 const gpa = gz.zir_code.gpa;1320 const gpa = gz.zir_code.gpa;
1344 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);1321 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
1345 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);1322 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
...@@ -1347,7 +1324,7 @@ pub const Scope = struct {...@@ -1347,7 +1324,7 @@ pub const Scope = struct {
1347 const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);1324 const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
1348 gz.zir_code.instructions.appendAssumeCapacity(inst);1325 gz.zir_code.instructions.appendAssumeCapacity(inst);
1349 gz.instructions.appendAssumeCapacity(new_index);1326 gz.instructions.appendAssumeCapacity(new_index);
1350 return new_index;1327 return gz.zir_code.ref_start_index + new_index;
1351 }1328 }
1352 };1329 };
13531330
src/astgen.zig+14-12
...@@ -1195,6 +1195,7 @@ fn varDecl(...@@ -1195,6 +1195,7 @@ fn varDecl(
1195 return mod.failNode(scope, var_decl.ast.align_node, "TODO implement alignment on locals", .{});1195 return mod.failNode(scope, var_decl.ast.align_node, "TODO implement alignment on locals", .{});
1196 }1196 }
1197 const gz = scope.getGenZir();1197 const gz = scope.getGenZir();
1198 const wzc = gz.zir_code;
1198 const tree = scope.tree();1199 const tree = scope.tree();
1199 const token_tags = tree.tokens.items(.tag);1200 const token_tags = tree.tokens.items(.tag);
12001201
...@@ -1276,7 +1277,7 @@ fn varDecl(...@@ -1276,7 +1277,7 @@ fn varDecl(
1276 var init_scope: Scope.GenZir = .{1277 var init_scope: Scope.GenZir = .{
1277 .parent = scope,1278 .parent = scope,
1278 .force_comptime = gz.force_comptime,1279 .force_comptime = gz.force_comptime,
1279 .zir_code = gz.zir_code,1280 .zir_code = wzc,
1280 };1281 };
1281 defer init_scope.instructions.deinit(mod.gpa);1282 defer init_scope.instructions.deinit(mod.gpa);
12821283
...@@ -1285,16 +1286,16 @@ fn varDecl(...@@ -1285,16 +1286,16 @@ fn varDecl(
1285 if (var_decl.ast.type_node != 0) {1286 if (var_decl.ast.type_node != 0) {
1286 const type_inst = try typeExpr(mod, &init_scope.base, var_decl.ast.type_node);1287 const type_inst = try typeExpr(mod, &init_scope.base, var_decl.ast.type_node);
1287 opt_type_inst = type_inst;1288 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);
1289 } else {1290 } else {
1290 const alloc = try init_scope.addUnNodeAsIndex(.alloc_inferred, undefined, node);1291 const alloc = try init_scope.addUnNode(.alloc_inferred, undefined, node);
1291 resolve_inferred_alloc = init_scope.zir_code.ref_start_index + alloc;1292 resolve_inferred_alloc = alloc;
1292 init_scope.rl_ptr = alloc;1293 init_scope.rl_ptr = alloc;
1293 }1294 }
1294 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };1295 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
1295 const init_inst = try expr(mod, &init_scope.base, init_result_loc, var_decl.ast.init_node);1296 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_tags = wzc.instructions.items(.tag);
1297 const zir_datas = gz.zir_code.instructions.items(.data);1298 const zir_datas = wzc.instructions.items(.data);
12981299
1299 const parent_zir = &gz.instructions;1300 const parent_zir = &gz.instructions;
1300 if (init_scope.rvalue_rl_count == 1) {1301 if (init_scope.rvalue_rl_count == 1) {
...@@ -1305,7 +1306,7 @@ fn varDecl(...@@ -1305,7 +1306,7 @@ fn varDecl(
1305 const expected_len = parent_zir.items.len + init_scope.instructions.items.len - 2;1306 const expected_len = parent_zir.items.len + init_scope.instructions.items.len - 2;
1306 try parent_zir.ensureCapacity(mod.gpa, expected_len);1307 try parent_zir.ensureCapacity(mod.gpa, expected_len);
1307 for (init_scope.instructions.items) |src_inst| {1308 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;
1309 if (zir_tags[src_inst] == .store_to_block_ptr) {1310 if (zir_tags[src_inst] == .store_to_block_ptr) {
1310 if (zir_datas[src_inst].bin.lhs == init_scope.rl_ptr) continue;1311 if (zir_datas[src_inst].bin.lhs == init_scope.rl_ptr) continue;
1311 }1312 }
...@@ -3192,26 +3193,27 @@ fn asRlPtr(...@@ -3192,26 +3193,27 @@ fn asRlPtr(
3192 // result location. If it does, elide the coerce_result_ptr instruction3193 // result location. If it does, elide the coerce_result_ptr instruction
3193 // as well as the store instruction, instead passing the result as an rvalue.3194 // as well as the store instruction, instead passing the result as an rvalue.
3194 const parent_gz = scope.getGenZir();3195 const parent_gz = scope.getGenZir();
3196 const wzc = parent_gz.zir_code;
31953197
3196 var as_scope: Scope.GenZir = .{3198 var as_scope: Scope.GenZir = .{
3197 .parent = scope,3199 .parent = scope,
3198 .zir_code = parent_gz.zir_code,3200 .zir_code = wzc,
3199 .force_comptime = parent_gz.force_comptime,3201 .force_comptime = parent_gz.force_comptime,
3200 .instructions = .{},3202 .instructions = .{},
3201 };3203 };
3202 defer as_scope.instructions.deinit(mod.gpa);3204 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);
3205 const result = try expr(mod, &as_scope.base, .{ .block_ptr = &as_scope }, operand_node);3207 const result = try expr(mod, &as_scope.base, .{ .block_ptr = &as_scope }, operand_node);
3206 const parent_zir = &parent_gz.instructions;3208 const parent_zir = &parent_gz.instructions;
3207 if (as_scope.rvalue_rl_count == 1) {3209 if (as_scope.rvalue_rl_count == 1) {
3208 // Busted! This expression didn't actually need a pointer.3210 // Busted! This expression didn't actually need a pointer.
3209 const zir_tags = parent_gz.zir_code.instructions.items(.tag);3211 const zir_tags = wzc.instructions.items(.tag);
3210 const zir_datas = parent_gz.zir_code.instructions.items(.data);3212 const zir_datas = wzc.instructions.items(.data);
3211 const expected_len = parent_zir.items.len + as_scope.instructions.items.len - 2;3213 const expected_len = parent_zir.items.len + as_scope.instructions.items.len - 2;
3212 try parent_zir.ensureCapacity(mod.gpa, expected_len);3214 try parent_zir.ensureCapacity(mod.gpa, expected_len);
3213 for (as_scope.instructions.items) |src_inst| {3215 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;
3215 if (zir_tags[src_inst] == .store_to_block_ptr) {3217 if (zir_tags[src_inst] == .store_to_block_ptr) {
3216 if (zir_datas[src_inst].bin.lhs == as_scope.rl_ptr) continue;3218 if (zir_datas[src_inst].bin.lhs == as_scope.rl_ptr) continue;
3217 }3219 }
test/stage2/test.zig+1-1
...@@ -355,7 +355,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -355,7 +355,7 @@ pub fn addCases(ctx: *TestContext) !void {
355 \\ const z = @TypeOf(true, 1);355 \\ const z = @TypeOf(true, 1);
356 \\ unreachable;356 \\ unreachable;
357 \\}357 \\}
358 , &[_][]const u8{":2:29: error: incompatible types: 'bool' and 'comptime_int'"});358 , &[_][]const u8{":2:15: error: incompatible types: 'bool' and 'comptime_int'"});
359 }359 }
360360
361 {361 {