aboutsummaryrefslogtreecommitdiff
path: root/src/compile.zig
blob: 9d70a581ebeb688e7652135c2f92e8f1321eb3ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const std = @import("std");
const Allocator = std.mem.Allocator;
const root = @import("root");
const Token = root.Lexer.Token;
const Expr = root.Expr;
const Location = root.Lexer.Location;

pub const VReg = enum(u32) { _ };

pub const Instr = struct {
    loc: Location,
    type: Type,

    pub const Type = union(enum) {
        constant: Constant,
        bin_op: BinOp,
        print: Print,
    };

    pub const Constant = struct {
        dest: VReg,
        value: u64,

        pub fn sources(_: Constant) Sources {
            return Sources.init(0) catch unreachable;
        }
    };

    pub const BinOp = struct {
        dest: VReg,
        lhs: VReg,
        rhs: VReg,
        op: Op,

        const Op = enum {
            add,
            sub,
        };

        pub fn sources(self: BinOp) Sources {
            return Sources.fromSlice(&.{ self.lhs, self.rhs }) catch unreachable;
        }
    };

    pub const Print = struct {
        arg: VReg,

        pub fn sources(self: Print) Sources {
            return Sources.fromSlice(&.{self.arg}) catch unreachable;
        }
    };

    pub fn sources(self: Instr) Sources {
        return switch (self.type) {
            inline else => |instr| instr.sources(),
        };
    }

    pub fn dest(self: *const Instr) ?VReg {
        return switch (self.type) {
            inline .constant, .bin_op => |s| s.dest,
            inline .print => null,
        };
    }

    pub const Sources = std.BoundedArray(VReg, 2);
};

pub const Block = struct {
    // arguments: []Reg,
    instrs: []Instr,
    vreg_last_use: std.AutoHashMap(VReg, usize),

    fn init(allocator: Allocator, instrs: []Instr) !Block {
        var vreg_last_use: std.AutoHashMap(VReg, usize) = .init(allocator);
        for (0.., instrs) |i, instr| {
            for (instr.sources().slice()) |src|
                try vreg_last_use.put(src, i);
        }
        return .{
            .instrs = instrs,
            .vreg_last_use = vreg_last_use,
        };
    }
};

pub fn compile(allocator: Allocator, source: []const u8, expr: *const Expr) !Block {
    const instrs: std.ArrayListUnmanaged(Instr) = try .initCapacity(allocator, 0);
    var ctx: CompileContext = .{
        .allocator = allocator,
        .source = source,
        .register_counter = 0,
        .instrs = instrs,
    };
    _ = try ctx.compileExpr(expr);
    return .init(allocator, ctx.instrs.items);
}

const CompileContext = struct {
    allocator: Allocator,
    source: []const u8,
    register_counter: u32,
    instrs: std.ArrayListUnmanaged(Instr),

    const Self = @This();

    fn addInstr(self: *Self, instr: Instr) !void {
        try self.instrs.append(self.allocator, instr);
    }

    fn compileExpr(self: *Self, expr: *const Expr) !VReg {
        const dest = self.register();
        switch (expr.type) {
            .integer_literal => try addInstr(self, .{
                .loc = expr.loc,
                .type = .{ .constant = .{ .dest = dest, .value = expr.loc.getInt(self.source) } },
            }),
            .bin_op => |binop| {
                const lhs = try self.compileExpr(binop.lhs);
                const rhs = try self.compileExpr(binop.rhs);
                try addInstr(self, .{
                    .loc = expr.loc,
                    .type = .{
                        .bin_op = .{
                            .dest = dest,
                            .lhs = lhs,
                            .rhs = rhs,
                            .op = switch (binop.op) {
                                .plus => .add,
                                .minus => .sub,
                            },
                        },
                    },
                });
            },
            .call => |call| {
                if (call.proc.type == .identifier and
                    std.mem.eql(u8, call.proc.loc.getIdent(self.source), "print"))
                {
                    const arg = try self.compileExpr(call.arg);
                    try self.addInstr(.{
                        .loc = expr.loc,
                        .type = .{ .print = .{ .arg = arg } },
                    });
                    // BUG: we're returning a bogus virtual register that we didn't write to
                } else {
                    return error.CantCallAnythingButPrint;
                }
            },
            .identifier => return error.CantCompileIdentifierExpr,
            .invalid => return error.CantCompileInvalidExpr,
        }
        return dest;
    }

    fn register(self: *Self) VReg {
        const reg: VReg = @enumFromInt(self.register_counter);
        self.register_counter += 1;
        return reg;
    }
};