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 {...@@ -1074,10 +1074,10 @@ pub const Scope = struct {
1074 callee: zir.Inst.Ref,1074 callee: zir.Inst.Ref,
1075 args: []const zir.Inst.Ref,1075 args: []const zir.Inst.Ref,
1076 /// Absolute node index. This function does the conversion to offset from Decl.1076 /// 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,
1078 ) !zir.Inst.Ref {1078 ) !zir.Inst.Ref {
1079 assert(callee != 0);1079 assert(callee != 0);
1080 assert(abs_node_index != 0);1080 assert(src_node != 0);
1081 const gpa = gz.zir_code.gpa;1081 const gpa = gz.zir_code.gpa;
1082 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);1082 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
1083 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);1083 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
...@@ -1094,7 +1094,7 @@ pub const Scope = struct {...@@ -1094,7 +1094,7 @@ pub const Scope = struct {
1094 gz.zir_code.instructions.appendAssumeCapacity(.{1094 gz.zir_code.instructions.appendAssumeCapacity(.{
1095 .tag = tag,1095 .tag = tag,
1096 .data = .{ .pl_node = .{1096 .data = .{ .pl_node = .{
1097 .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),1097 .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
1098 .payload_index = payload_index,1098 .payload_index = payload_index,
1099 } },1099 } },
1100 });1100 });
...@@ -1138,14 +1138,24 @@ pub const Scope = struct {...@@ -1138,14 +1138,24 @@ pub const Scope = struct {
1138 tag: zir.Inst.Tag,1138 tag: zir.Inst.Tag,
1139 operand: zir.Inst.Ref,1139 operand: zir.Inst.Ref,
1140 /// Absolute node index. This function does the conversion to offset from Decl.1140 /// 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,
1142 ) !zir.Inst.Ref {1142 ) !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 {
1143 assert(operand != 0);1153 assert(operand != 0);
1144 return gz.add(.{1154 return gz.addAsIndex(.{
1145 .tag = tag,1155 .tag = tag,
1146 .data = .{ .un_node = .{1156 .data = .{ .un_node = .{
1147 .operand = operand,1157 .operand = operand,
1148 .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),1158 .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
1149 } },1159 } },
1150 });1160 });
1151 }1161 }
...@@ -1154,7 +1164,7 @@ pub const Scope = struct {...@@ -1154,7 +1164,7 @@ pub const Scope = struct {
1154 gz: *GenZir,1164 gz: *GenZir,
1155 tag: zir.Inst.Tag,1165 tag: zir.Inst.Tag,
1156 /// Absolute node index. This function does the conversion to offset from Decl.1166 /// 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,
1158 extra: anytype,1168 extra: anytype,
1159 ) !zir.Inst.Ref {1169 ) !zir.Inst.Ref {
1160 const gpa = gz.zir_code.gpa;1170 const gpa = gz.zir_code.gpa;
...@@ -1166,7 +1176,7 @@ pub const Scope = struct {...@@ -1166,7 +1176,7 @@ pub const Scope = struct {
1166 gz.zir_code.instructions.appendAssumeCapacity(.{1176 gz.zir_code.instructions.appendAssumeCapacity(.{
1167 .tag = tag,1177 .tag = tag,
1168 .data = .{ .pl_node = .{1178 .data = .{ .pl_node = .{
1169 .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index),1179 .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node),
1170 .payload_index = payload_index,1180 .payload_index = payload_index,
1171 } },1181 } },
1172 });1182 });
...@@ -1239,9 +1249,18 @@ pub const Scope = struct {...@@ -1239,9 +1249,18 @@ pub const Scope = struct {
1239 lhs: zir.Inst.Ref,1249 lhs: zir.Inst.Ref,
1240 rhs: zir.Inst.Ref,1250 rhs: zir.Inst.Ref,
1241 ) !zir.Inst.Ref {1251 ) !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 {
1242 assert(lhs != 0);1261 assert(lhs != 0);
1243 assert(rhs != 0);1262 assert(rhs != 0);
1244 return gz.add(.{1263 return gz.addAsIndex(.{
1245 .tag = tag,1264 .tag = tag,
1246 .data = .{ .bin = .{1265 .data = .{ .bin = .{
1247 .lhs = lhs,1266 .lhs = lhs,
...@@ -1265,11 +1284,11 @@ pub const Scope = struct {...@@ -1265,11 +1284,11 @@ pub const Scope = struct {
1265 gz: *GenZir,1284 gz: *GenZir,
1266 tag: zir.Inst.Tag,1285 tag: zir.Inst.Tag,
1267 /// Absolute node index. This function does the conversion to offset from Decl.1286 /// 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,
1269 ) !zir.Inst.Ref {1288 ) !zir.Inst.Ref {
1270 return gz.add(.{1289 return gz.add(.{
1271 .tag = tag,1290 .tag = tag,
1272 .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index) },1291 .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(src_node) },
1273 });1292 });
1274 }1293 }
12751294
...@@ -1321,6 +1340,10 @@ pub const Scope = struct {...@@ -1321,6 +1340,10 @@ pub const Scope = struct {
1321 }1340 }
13221341
1323 pub fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref {1342 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 {
1324 const gpa = gz.zir_code.gpa;1347 const gpa = gz.zir_code.gpa;
1325 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);1348 try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1);
1326 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);1349 try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1);
...@@ -1328,7 +1351,7 @@ pub const Scope = struct {...@@ -1328,7 +1351,7 @@ pub const Scope = struct {
1328 const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);1351 const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len);
1329 gz.zir_code.instructions.appendAssumeCapacity(inst);1352 gz.zir_code.instructions.appendAssumeCapacity(inst);
1330 gz.instructions.appendAssumeCapacity(new_index);1353 gz.instructions.appendAssumeCapacity(new_index);
1331 return new_index + gz.zir_code.ref_start_index;1354 return new_index;
1332 }1355 }
1333 };1356 };
13341357
src/astgen.zig+5-5
...@@ -1306,11 +1306,11 @@ fn varDecl(...@@ -1306,11 +1306,11 @@ fn varDecl(
1306 if (var_decl.ast.type_node != 0) {1306 if (var_decl.ast.type_node != 0) {
1307 const type_inst = try typeExpr(mod, &init_scope.base, var_decl.ast.type_node);1307 const type_inst = try typeExpr(mod, &init_scope.base, var_decl.ast.type_node);
1308 opt_type_inst = type_inst;1308 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);
1310 } else {1310 } else {
1311 const alloc = try init_scope.addUnNode(.alloc_inferred, undefined, node);1311 const alloc = try init_scope.addUnNodeAsIndex(.alloc_inferred, undefined, node);
1312 resolve_inferred_alloc = alloc;1312 resolve_inferred_alloc = init_scope.zir_code.ref_start_index + alloc;
1313 init_scope.rl_ptr = alloc - init_scope.zir_code.ref_start_index;1313 init_scope.rl_ptr = alloc;
1314 }1314 }
1315 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };1315 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
1316 const init_inst = try expr(mod, &init_scope.base, init_result_loc, var_decl.ast.init_node);1316 const init_inst = try expr(mod, &init_scope.base, init_result_loc, var_decl.ast.init_node);
...@@ -3201,7 +3201,7 @@ fn asRlPtr(...@@ -3201,7 +3201,7 @@ fn asRlPtr(
3201 };3201 };
3202 defer as_scope.instructions.deinit(mod.gpa);3202 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);
3205 const result = try expr(mod, &as_scope.base, .{ .block_ptr = &as_scope }, operand_node);3205 const result = try expr(mod, &as_scope.base, .{ .block_ptr = &as_scope }, operand_node);
3206 const parent_zir = &parent_gz.instructions;3206 const parent_zir = &parent_gz.instructions;
3207 if (as_scope.rvalue_rl_count == 1) {3207 if (as_scope.rvalue_rl_count == 1) {