authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-23 12:54:18-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-23 12:54:18-07:00
log866be099f8a16389e83ba4f5d8b3122b14b09e77
tree1b9de9209b33846bc05aa6f8d87b86ef6d9c602d
parent830143905e5e89e6dcf8a0831790072ee88eb2f9

stage2: add helper functions to clean up astgen Ref/Index


2 files changed, 40 insertions(+), 17 deletions(-)

src/Module.zig+35-12
......@@ -1074,10 +1074,10 @@ pub const Scope = struct {
10741074 callee: zir.Inst.Ref,
10751075 args: []const zir.Inst.Ref,
10761076 /// Absolute node index. This function does the conversion to offset from Decl.
1077 abs_node_index: ast.Node.Index,
1077 src_node: ast.Node.Index,
10781078 ) !zir.Inst.Ref {
10791079 assert(callee != 0);
1080 assert(abs_node_index != 0);
1080 assert(src_node != 0);
10811081 const gpa = gz.zir_code.gpa;
10821082 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
10831083 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
......@@ -1094,7 +1094,7 @@ pub const Scope = struct {
10941094 gz.zir_code.instructions.appendAssumeCapacity(.{
10951095 .tag = tag,
10961096 .data = .{ .pl_node = .{
1097 .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),
1097 .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
10981098 .payload_index = payload_index,
10991099 } },
11001100 });
......@@ -1138,14 +1138,24 @@ pub const Scope = struct {
11381138 tag: zir.Inst.Tag,
11391139 operand: zir.Inst.Ref,
11401140 /// Absolute node index. This function does the conversion to offset from Decl.
1141 abs_node_index: ast.Node.Index,
1141 src_node: ast.Node.Index,
11421142 ) !zir.Inst.Ref {
1143 return gz.zir_code.ref_start_index + try gz.addUnNodeAsIndex(tag, operand, src_node);
1144 }
1145
1146 pub fn addUnNodeAsIndex(
1147 gz: *GenZir,
1148 tag: zir.Inst.Tag,
1149 operand: zir.Inst.Ref,
1150 /// Absolute node index. This function does the conversion to offset from Decl.
1151 src_node: ast.Node.Index,
1152 ) !zir.Inst.Index {
11431153 assert(operand != 0);
1144 return gz.add(.{
1154 return gz.addAsIndex(.{
11451155 .tag = tag,
11461156 .data = .{ .un_node = .{
11471157 .operand = operand,
1148 .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),
1158 .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
11491159 } },
11501160 });
11511161 }
......@@ -1154,7 +1164,7 @@ pub const Scope = struct {
11541164 gz: *GenZir,
11551165 tag: zir.Inst.Tag,
11561166 /// Absolute node index. This function does the conversion to offset from Decl.
1157 abs_node_index: ast.Node.Index,
1167 src_node: ast.Node.Index,
11581168 extra: anytype,
11591169 ) !zir.Inst.Ref {
11601170 const gpa = gz.zir_code.gpa;
......@@ -1166,7 +1176,7 @@ pub const Scope = struct {
11661176 gz.zir_code.instructions.appendAssumeCapacity(.{
11671177 .tag = tag,
11681178 .data = .{ .pl_node = .{
1169 .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),
1179 .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
11701180 .payload_index = payload_index,
11711181 } },
11721182 });
......@@ -1239,9 +1249,18 @@ pub const Scope = struct {
12391249 lhs: zir.Inst.Ref,
12401250 rhs: zir.Inst.Ref,
12411251 ) !zir.Inst.Ref {
1252 return gz.zir_code.ref_start_index + try gz.addBinAsIndex(tag, lhs, rhs);
1253 }
1254
1255 pub fn addBinAsIndex(
1256 gz: *GenZir,
1257 tag: zir.Inst.Tag,
1258 lhs: zir.Inst.Ref,
1259 rhs: zir.Inst.Ref,
1260 ) !zir.Inst.Index {
12421261 assert(lhs != 0);
12431262 assert(rhs != 0);
1244 return gz.add(.{
1263 return gz.addAsIndex(.{
12451264 .tag = tag,
12461265 .data = .{ .bin = .{
12471266 .lhs = lhs,
......@@ -1265,11 +1284,11 @@ pub const Scope = struct {
12651284 gz: *GenZir,
12661285 tag: zir.Inst.Tag,
12671286 /// Absolute node index. This function does the conversion to offset from Decl.
1268 abs_node_index: ast.Node.Index,
1287 src_node: ast.Node.Index,
12691288 ) !zir.Inst.Ref {
12701289 return gz.add(.{
12711290 .tag = tag,
1272 .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index) },
1291 .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(src_node) },
12731292 });
12741293 }
12751294
......@@ -1321,6 +1340,10 @@ pub const Scope = struct {
13211340 }
13221341
13231342 pub fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref {
1343 return gz.zir_code.ref_start_index + try gz.addAsIndex(inst);
1344 }
1345
1346 pub fn addAsIndex(gz: *GenZir, inst: zir.Inst) !zir.Inst.Index {
13241347 const gpa = gz.zir_code.gpa;
13251348 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
13261349 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
......@@ -1328,7 +1351,7 @@ pub const Scope = struct {
13281351 const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
13291352 gz.zir_code.instructions.appendAssumeCapacity(inst);
13301353 gz.instructions.appendAssumeCapacity(new_index);
1331 return new_index + gz.zir_code.ref_start_index;
1354 return new_index;
13321355 }
13331356 };
13341357
src/astgen.zig+5-5
......@@ -1306,11 +1306,11 @@ fn varDecl(
13061306 if (var_decl.ast.type_node != 0) {
13071307 const type_inst = try typeExpr(mod, &init_scope.base, var_decl.ast.type_node);
13081308 opt_type_inst = type_inst;
1309 init_scope.rl_ptr = (try init_scope.addUnNode(.alloc, type_inst, node)) - init_scope.zir_code.ref_start_index;
1309 init_scope.rl_ptr = try init_scope.addUnNodeAsIndex(.alloc, type_inst, node);
13101310 } else {
1311 const alloc = try init_scope.addUnNode(.alloc_inferred, undefined, node);
1312 resolve_inferred_alloc = alloc;
1313 init_scope.rl_ptr = alloc - init_scope.zir_code.ref_start_index;
1311 const alloc = try init_scope.addUnNodeAsIndex(.alloc_inferred, undefined, node);
1312 resolve_inferred_alloc = init_scope.zir_code.ref_start_index + alloc;
1313 init_scope.rl_ptr = alloc;
13141314 }
13151315 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
13161316 const init_inst = try expr(mod, &init_scope.base, init_result_loc, var_decl.ast.init_node);
......@@ -3201,7 +3201,7 @@ fn asRlPtr(
32013201 };
32023202 defer as_scope.instructions.deinit(mod.gpa);
32033203
3204 as_scope.rl_ptr = (try as_scope.addBin(.coerce_result_ptr, dest_type, result_ptr)) - as_scope.zir_code.ref_start_index;
3204 as_scope.rl_ptr = try as_scope.addBinAsIndex(.coerce_result_ptr, dest_type, result_ptr);
32053205 const result = try expr(mod, &as_scope.base, .{ .block_ptr = &as_scope }, operand_node);
32063206 const parent_zir = &parent_gz.instructions;
32073207 if (as_scope.rvalue_rl_count == 1) {