diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-03-23 12:54:18 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-03-23 12:54:18 -0700 |
| commit | 866be099f8a16389e83ba4f5d8b3122b14b09e77 (patch) | |
| tree | 1b9de9209b33846bc05aa6f8d87b86ef6d9c602d /src/Module.zig | |
| parent | 830143905e5e89e6dcf8a0831790072ee88eb2f9 (diff) | |
| download | zig-866be099f8a16389e83ba4f5d8b3122b14b09e77.tar.gz zig-866be099f8a16389e83ba4f5d8b3122b14b09e77.zip | |
stage2: add helper functions to clean up astgen Ref/Index
Diffstat (limited to 'src/Module.zig')
| -rw-r--r-- | src/Module.zig | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/src/Module.zig b/src/Module.zig index e0695437b8..0b9b4960ae 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -1074,10 +1074,10 @@ pub const Scope = struct { callee: zir.Inst.Ref, args: []const zir.Inst.Ref, /// Absolute node index. This function does the conversion to offset from Decl. - abs_node_index: ast.Node.Index, + src_node: ast.Node.Index, ) !zir.Inst.Ref { assert(callee != 0); - assert(abs_node_index != 0); + assert(src_node != 0); const gpa = gz.zir_code.gpa; try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1); @@ -1094,7 +1094,7 @@ pub const Scope = struct { gz.zir_code.instructions.appendAssumeCapacity(.{ .tag = tag, .data = .{ .pl_node = .{ - .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index), + .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node), .payload_index = payload_index, } }, }); @@ -1138,14 +1138,24 @@ pub const Scope = struct { tag: zir.Inst.Tag, operand: zir.Inst.Ref, /// Absolute node index. This function does the conversion to offset from Decl. - abs_node_index: ast.Node.Index, + src_node: ast.Node.Index, ) !zir.Inst.Ref { + return gz.zir_code.ref_start_index + try gz.addUnNodeAsIndex(tag, operand, src_node); + } + + pub fn addUnNodeAsIndex( + gz: *GenZir, + tag: zir.Inst.Tag, + operand: zir.Inst.Ref, + /// Absolute node index. This function does the conversion to offset from Decl. + src_node: ast.Node.Index, + ) !zir.Inst.Index { assert(operand != 0); - return gz.add(.{ + return gz.addAsIndex(.{ .tag = tag, .data = .{ .un_node = .{ .operand = operand, - .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index), + .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node), } }, }); } @@ -1154,7 +1164,7 @@ pub const Scope = struct { gz: *GenZir, tag: zir.Inst.Tag, /// Absolute node index. This function does the conversion to offset from Decl. - abs_node_index: ast.Node.Index, + src_node: ast.Node.Index, extra: anytype, ) !zir.Inst.Ref { const gpa = gz.zir_code.gpa; @@ -1166,7 +1176,7 @@ pub const Scope = struct { gz.zir_code.instructions.appendAssumeCapacity(.{ .tag = tag, .data = .{ .pl_node = .{ - .src_node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index), + .src_node = gz.zir_code.decl.nodeIndexToRelative(src_node), .payload_index = payload_index, } }, }); @@ -1239,9 +1249,18 @@ pub const Scope = struct { lhs: zir.Inst.Ref, rhs: zir.Inst.Ref, ) !zir.Inst.Ref { + return gz.zir_code.ref_start_index + try gz.addBinAsIndex(tag, lhs, rhs); + } + + pub fn addBinAsIndex( + gz: *GenZir, + tag: zir.Inst.Tag, + lhs: zir.Inst.Ref, + rhs: zir.Inst.Ref, + ) !zir.Inst.Index { assert(lhs != 0); assert(rhs != 0); - return gz.add(.{ + return gz.addAsIndex(.{ .tag = tag, .data = .{ .bin = .{ .lhs = lhs, @@ -1265,11 +1284,11 @@ pub const Scope = struct { gz: *GenZir, tag: zir.Inst.Tag, /// Absolute node index. This function does the conversion to offset from Decl. - abs_node_index: ast.Node.Index, + src_node: ast.Node.Index, ) !zir.Inst.Ref { return gz.add(.{ .tag = tag, - .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(abs_node_index) }, + .data = .{ .node = gz.zir_code.decl.nodeIndexToRelative(src_node) }, }); } @@ -1321,6 +1340,10 @@ pub const Scope = struct { } pub fn add(gz: *GenZir, inst: zir.Inst) !zir.Inst.Ref { + return gz.zir_code.ref_start_index + try gz.addAsIndex(inst); + } + + pub fn addAsIndex(gz: *GenZir, inst: zir.Inst) !zir.Inst.Index { const gpa = gz.zir_code.gpa; try gz.instructions.ensureCapacity(gpa, gz.instructions.items.len + 1); try gz.zir_code.instructions.ensureCapacity(gpa, gz.zir_code.instructions.len + 1); @@ -1328,7 +1351,7 @@ pub const Scope = struct { const new_index = @intCast(zir.Inst.Index, gz.zir_code.instructions.len); gz.zir_code.instructions.appendAssumeCapacity(inst); gz.instructions.appendAssumeCapacity(new_index); - return new_index + gz.zir_code.ref_start_index; + return new_index; } }; |
