aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/jq/compile.zig
blob: 043262eda822373b25c19da84ecfcf44226a0032 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const std = @import("std");
const jv = @import("../jv.zig");
const Ast = @import("./parse.zig").Ast;
const BinaryOp = @import("./parse.zig").BinaryOp;

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

pub const Opcode = enum {
    nop,
    ret,
    jump,
    jump_unless,
    fork,
    dup,
    pop,
    subexp_begin,
    subexp_end,
    index,
    index_opt,
    add,
    sub,
    mul,
    div,
    mod,
    eq,
    ne,
    lt,
    gt,
    le,
    ge,
    @"const",
    const_true,
    const_false,
};

pub const Instr = union(Opcode) {
    const Self = @This();

    nop,
    ret,
    jump: usize,
    jump_unless: usize,
    fork: usize,
    dup,
    pop,
    subexp_begin,
    subexp_end,
    index,
    index_opt,
    add,
    sub,
    mul,
    div,
    mod,
    eq,
    ne,
    lt,
    gt,
    le,
    ge,
    @"const": ConstIndex,
    const_true,
    const_false,

    pub fn op(self: Self) Opcode {
        return self;
    }
};

fn compileExpr(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocator, ast: *const Ast) ![]Instr {
    var instrs = try std.ArrayList(Instr).initCapacity(allocator, 16);

    switch (ast.*) {
        .identity => try instrs.append(allocator, .nop),
        .index => |idx| {
            const base_instrs = try compileExpr(allocator, compile_allocator, idx.base);
            defer allocator.free(base_instrs);
            const index_instrs = try compileExpr(allocator, compile_allocator, idx.index);
            defer allocator.free(index_instrs);
            try instrs.appendSlice(allocator, base_instrs);
            try instrs.append(allocator, .subexp_begin);
            try instrs.appendSlice(allocator, index_instrs);
            try instrs.append(allocator, .subexp_end);
            try instrs.append(allocator, if (idx.is_optional) .index_opt else .index);
        },
        .literal => |idx| try instrs.append(allocator, .{ .@"const" = idx }),
        .binary_expr => |binary_expr| {
            const rhs_instrs = try compileExpr(allocator, compile_allocator, binary_expr.rhs);
            defer allocator.free(rhs_instrs);
            const lhs_instrs = try compileExpr(allocator, compile_allocator, binary_expr.lhs);
            defer allocator.free(lhs_instrs);
            try instrs.append(allocator, .subexp_begin);
            try instrs.appendSlice(allocator, rhs_instrs);
            try instrs.append(allocator, .subexp_end);
            try instrs.append(allocator, .subexp_begin);
            try instrs.appendSlice(allocator, lhs_instrs);
            try instrs.append(allocator, .subexp_end);
            const op_instr: Instr = switch (binary_expr.op) {
                .add => .add,
                .sub => .sub,
                .mul => .mul,
                .div => .div,
                .mod => .mod,
                .eq => .eq,
                .ne => .ne,
                .lt => .lt,
                .gt => .gt,
                .le => .le,
                .ge => .ge,
                else => return error.Unimplemented,
            };
            try instrs.append(allocator, op_instr);
        },
        .and_expr => |and_expr| {
            //     DUP
            //     <lhs>
            //     JUMP_UNLESS l3
            //     POP
            //     <rhs>
            //     JUMP_UNLESS l1
            //     CONST_TRUE
            //     JUMP l2
            // l1: CONST_FALSE
            // l2: JUMP l4
            // l3: POP
            //     CONST_FALSE
            // l4:
            const lhs_instrs = try compileExpr(allocator, compile_allocator, and_expr.lhs);
            defer allocator.free(lhs_instrs);
            const rhs_instrs = try compileExpr(allocator, compile_allocator, and_expr.rhs);
            defer allocator.free(rhs_instrs);

            try instrs.append(allocator, .dup);
            try instrs.appendSlice(allocator, lhs_instrs);
            const jump1_idx = instrs.items.len;
            try instrs.append(allocator, .{ .jump_unless = 0 });
            try instrs.append(allocator, .pop);
            try instrs.appendSlice(allocator, rhs_instrs);
            const jump2_idx = instrs.items.len;
            try instrs.append(allocator, .{ .jump_unless = 0 });
            try instrs.append(allocator, .const_true);
            const jump3_idx = instrs.items.len;
            try instrs.append(allocator, .{ .jump = 0 });
            const l1 = instrs.items.len;
            try instrs.append(allocator, .const_false);
            const jump4_idx = instrs.items.len;
            const l2 = instrs.items.len;
            try instrs.append(allocator, .{ .jump = 0 });
            const l3 = instrs.items.len;
            try instrs.append(allocator, .pop);
            try instrs.append(allocator, .const_false);
            const l4 = instrs.items.len;

            instrs.items[jump1_idx] = .{ .jump_unless = l3 - jump1_idx };
            instrs.items[jump2_idx] = .{ .jump_unless = l1 - jump2_idx };
            instrs.items[jump3_idx] = .{ .jump = l2 - jump3_idx };
            instrs.items[jump4_idx] = .{ .jump = l4 - jump4_idx };
        },
        .or_expr => |or_expr| {
            //     DUP
            //     <lhs>
            //     JUMP_UNLESS l1
            //     POP
            //     CONST_TRUE
            //     JUMP l3
            // l1: POP
            //     <rhs>
            //     JUMP_UNLESS l2
            //     CONST_TRUE
            //     JUMP l3
            // l2: CONST_FALSE
            // l3:
            const lhs_instrs = try compileExpr(allocator, compile_allocator, or_expr.lhs);
            defer allocator.free(lhs_instrs);
            const rhs_instrs = try compileExpr(allocator, compile_allocator, or_expr.rhs);
            defer allocator.free(rhs_instrs);

            try instrs.append(allocator, .dup);
            try instrs.appendSlice(allocator, lhs_instrs);
            const jump1_idx = instrs.items.len;
            try instrs.append(allocator, .{ .jump_unless = 0 });
            try instrs.append(allocator, .pop);
            try instrs.append(allocator, .const_true);
            const jump2_idx = instrs.items.len;
            try instrs.append(allocator, .{ .jump = 0 });
            const l1 = instrs.items.len;
            try instrs.append(allocator, .pop);
            try instrs.appendSlice(allocator, rhs_instrs);
            const jump3_idx = instrs.items.len;
            try instrs.append(allocator, .{ .jump_unless = 0 });
            try instrs.append(allocator, .const_true);
            const jump4_idx = instrs.items.len;
            try instrs.append(allocator, .{ .jump = 0 });
            const l2 = instrs.items.len;
            try instrs.append(allocator, .const_false);
            const l3 = instrs.items.len;

            instrs.items[jump1_idx] = .{ .jump_unless = l1 - jump1_idx };
            instrs.items[jump2_idx] = .{ .jump = l3 - jump2_idx };
            instrs.items[jump3_idx] = .{ .jump_unless = l2 - jump3_idx };
            instrs.items[jump4_idx] = .{ .jump = l3 - jump4_idx };
        },
        .pipe => |pipe_expr| {
            const lhs_instrs = try compileExpr(allocator, compile_allocator, pipe_expr.lhs);
            defer allocator.free(lhs_instrs);
            const rhs_instrs = try compileExpr(allocator, compile_allocator, pipe_expr.rhs);
            defer allocator.free(rhs_instrs);
            try instrs.appendSlice(allocator, lhs_instrs);
            try instrs.appendSlice(allocator, rhs_instrs);
        },
        .comma => |comma_expr| {
            //     FORK l1
            //     <lhs>
            //     JUMP l2
            // l1: <rhs>
            // l2:
            const lhs_instrs = try compileExpr(allocator, compile_allocator, comma_expr.lhs);
            defer allocator.free(lhs_instrs);
            const rhs_instrs = try compileExpr(allocator, compile_allocator, comma_expr.rhs);
            defer allocator.free(rhs_instrs);
            const fork_index = instrs.items.len;
            try instrs.append(allocator, .{ .fork = 0 });
            try instrs.appendSlice(allocator, lhs_instrs);
            const jump_index = instrs.items.len;
            try instrs.append(allocator, .{ .jump = 0 });
            const l1 = instrs.items.len;
            try instrs.appendSlice(allocator, rhs_instrs);
            const l2 = instrs.items.len;
            instrs.items[fork_index] = .{ .fork = l1 - fork_index };
            instrs.items[jump_index] = .{ .jump = l2 - jump_index };
        },
    }

    return instrs.toOwnedSlice(allocator);
}

pub fn compile(allocator: std.mem.Allocator, compile_allocator: std.mem.Allocator, ast: *const Ast) ![]Instr {
    var instrs = try std.ArrayList(Instr).initCapacity(allocator, 16);
    const expr_instrs = try compileExpr(allocator, compile_allocator, ast);
    defer allocator.free(expr_instrs);
    try instrs.appendSlice(allocator, expr_instrs);
    try instrs.append(allocator, .ret);
    return instrs.toOwnedSlice(allocator);
}