diff options
Diffstat (limited to 'src/compile.zig')
-rw-r--r-- | src/compile.zig | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/src/compile.zig b/src/compile.zig index 822153f..ad5fc59 100644 --- a/src/compile.zig +++ b/src/compile.zig @@ -11,17 +11,21 @@ pub const Instr = struct { loc: Location, type: Type, - const Type = union(enum) { + pub const Type = union(enum) { constant: Constant, bin_op: BinOp, }; - const Constant = struct { + pub const Constant = struct { dest: VReg, value: u64, + + pub fn sources(_: Constant) Sources { + return Sources.init(0) catch unreachable; + } }; - const BinOp = struct { + pub const BinOp = struct { dest: VReg, lhs: VReg, rhs: VReg, @@ -30,12 +34,15 @@ pub const Instr = struct { const Op = enum { add, }; + + pub fn sources(self: BinOp) Sources { + return Sources.fromSlice(&.{ self.lhs, self.rhs }) catch unreachable; + } }; - pub fn sources(self: Instr) std.BoundedArray(VReg, 2) { + pub fn sources(self: Instr) Sources { return switch (self.type) { - .bin_op => |bin_op| std.BoundedArray(VReg, 2).fromSlice(&.{ bin_op.lhs, bin_op.rhs }) catch unreachable, - .constant => std.BoundedArray(VReg, 2).init(0) catch unreachable, + inline else => |instr| instr.sources(), }; } @@ -44,6 +51,8 @@ pub const Instr = struct { inline .constant, .bin_op => |s| s.dest, }; } + + pub const Sources = std.BoundedArray(VReg, 2); }; pub const Block = struct { |