aboutsummaryrefslogtreecommitdiffstats
path: root/src/ir.h
blob: 514a3ac1923f2682a8ccf6d319a5a972000d21f2 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#ifndef BDL_IR_H
#define BDL_IR_H

typedef struct LineInfo {
    size_t line;
    size_t col;
} LineInfo;

typedef enum Op {
    // Arithmetic ops.
    // - Binary operations.
    // - Arguments are passed via the stack.
    OP_ADD,
    OP_SUB,
    OP_MUL,
    OP_DIV,
    OP_MOD,
    // Logic ops.
    OP_NOT,
    // Stack ops.
    // - Requires an Object to push into the stack.
    OP_PUSH,
    // - Discards the last value in the stack.
    OP_DROP,
    // - Duplicates the last value in the stack.
    OP_DUP,
    // - Rotates the last three elements in the stack.
    // - Right: [ a b c -> c a b ]
    // - Left:  [ a b c -> b c a]
    OP_ROT_RIGHT,
    OP_ROT_LEFT,
    // A label for memory access.
    // - The argument should be a unique value.
    OP_LABEL,
    // Jump/conditional ops.
    // - Take a label as argument.
    // - For conditional jumps, the last value in the stack is used.
    OP_JUMP,
    OP_JUMP_IF_TRUE,
    OP_JUMP_IF_FALSE,
    // - These require numerical objects on the stack.
    // - Consume two items in the stack.
    // - Jumps to the given label as argument when appropriate.
    OP_JUMP_IF_EQ,
    OP_JUMP_IF_NEQ,
    OP_JUMP_IF_GT,
    OP_JUMP_IF_LT,
    OP_JUMP_IF_GE,
    OP_JUMP_IF_LE,
    // Primitive complex commands.
    // - Prints the last object in the stack.
    OP_PRINT,
    // Procedures.
    // - Requires a function name as parameter.
    OP_CALL,
    // - Return position is on a know location of the stack based on the offset
    // of locals, parameters, etc.
    OP_RETURN,
    // TODO: add remaining ops.
} Op;

static const char* ops_str[] = {
    [OP_ADD]           = "OP_ADD",
    [OP_SUB]           = "OP_SUB",
    [OP_MUL]           = "OP_MUL",
    [OP_DIV]           = "OP_DIV",
    [OP_MOD]           = "OP_MOD",
    [OP_NOT]           = "OP_NOT",
    [OP_PUSH]          = "OP_PUSH",
    [OP_DROP]          = "OP_DROP",
    [OP_DUP]           = "OP_DUP",
    [OP_ROT_RIGHT]     = "OP_ROT_RIGHT",
    [OP_ROT_LEFT]      = "OP_ROT_LEFT",
    [OP_LABEL]         = "OP_LABEL",
    [OP_JUMP]          = "OP_JUMP",
    [OP_JUMP_IF_TRUE]  = "OP_JUMP_IF_TRUE",
    [OP_JUMP_IF_FALSE] = "OP_JUMP_IF_FALSE",
    [OP_JUMP_IF_EQ]    = "OP_JUMP_IF_EQ",
    [OP_JUMP_IF_NEQ]   = "OP_JUMP_IF_NEQ",
    [OP_JUMP_IF_GT]    = "OP_JUMP_IF_GT",
    [OP_JUMP_IF_LT]    = "OP_JUMP_IF_LT",
    [OP_JUMP_IF_GE]    = "OP_JUMP_IF_GE",
    [OP_JUMP_IF_LE]    = "OP_JUMP_IF_LE",
    [OP_PRINT]         = "OP_PRINT",
    [OP_CALL]          = "OP_CALL",
    [OP_RETURN]        = "OP_RETURN",
};

typedef struct Instruction {
    Op op;

    // Op arguments.
    union {
        // OP_PUSH
        Object *argument;

        // OP_LABEL
        // OP_JUMP
        // OP_JUMP_IF_FALSE
        size_t label_id;
    };

    // Original line/column for debugging purposes.
    size_t line;
    size_t col;
} Instruction;

typedef struct Procedure {
    // Procedure name.
    char *name;

    // Program code.
    Instruction *instructions;

    // Number of locals and parameters.
    size_t n_params;
    size_t n_locals;
} Procedure;

typedef struct ProgramIr {
    Procedure **procedures;
    size_t labels;
} ProgramIr;

#define INST_SIMPLE(PROC, OP, LINE, COL) \
    do { \
        Instruction inst = (Instruction){(OP), NULL, (LINE), (COL)}; \
        array_push((PROC)->instructions, inst); \
    } while(false);

#define INST_ARG(PROC, OP, ARG, LINE, COL) \
    do { \
        Instruction inst = (Instruction){(OP), (ARG), (LINE), (COL)}; \
        array_push((PROC)->instructions, inst); \
    } while(false);

void
print_instruction(Instruction *instruction) {
    printf("%4ld:%-4ld ", instruction->line, instruction->col);
    Op op = instruction->op;
    switch (op) {
        case OP_PUSH: {
            printf("%-16s -> ", ops_str[op]);
            OBJ_PRINT(instruction->argument);
        } break;
        case OP_JUMP:
        case OP_JUMP_IF_TRUE:
        case OP_JUMP_IF_FALSE:
        case OP_JUMP_IF_EQ:
        case OP_JUMP_IF_NEQ:
        case OP_JUMP_IF_GT:
        case OP_JUMP_IF_LT:
        case OP_JUMP_IF_GE:
        case OP_JUMP_IF_LE:
        case OP_LABEL: {
            printf("%-16s -> %zu\n", ops_str[op], instruction->label_id);
        } break;
        default: {
            printf("%s\n", ops_str[op]);
        } break;
    }
}

void
print_procedure(Procedure *proc) {
    printf("=====  %.*s  =====\n", (int)array_size(proc->name), proc->name);
    printf("code:\n");
    for (size_t i = 0; i < array_size(proc->instructions); ++i) {
        print_instruction(&proc->instructions[i]);
    }
}

Procedure *
proc_alloc(ProgramIr *program, StringView name) {
    Procedure *proc = calloc(1, sizeof(Procedure));
    array_init(proc->name, name.n);
    array_insert(proc->name, name.start, name.n);
    array_init(proc->instructions, 0);
    array_push(program->procedures, proc);
    return proc;
}

void compile_object(ProgramIr *program, Procedure *proc, Object *obj);

void
compile_arithmetic(ProgramIr *program, Procedure *proc, Op op,
        size_t line, size_t col, Object *args) {
    compile_object(program, proc, args->head);
    args = args->tail;
    while (args != NULL) {
        compile_object(program, proc, args->head);
        args = args->tail;
        INST_SIMPLE(proc, op, line, col);
    }
}

void
compile_numeric_cmp(ProgramIr *program, Procedure *proc, Op op,
        size_t line, size_t col, Object *args) {
    size_t label_false = program->labels++;
    size_t label_exit = program->labels++;
    compile_object(program, proc, args->head);
    args = args->tail;
    while (args != NULL) {
        compile_object(program, proc, args->head);
        args = args->tail;
        INST_SIMPLE(proc, OP_DUP, line, col);
        INST_SIMPLE(proc, OP_ROT_RIGHT, line, col);
        INST_ARG(proc, op, label_false, line, col);
    }
    INST_SIMPLE(proc, OP_DROP, line, col);
    INST_ARG(proc, OP_PUSH, &obj_true, line, col);
    INST_ARG(proc, OP_JUMP, label_exit, line, col);
    INST_ARG(proc, OP_LABEL, label_false, line, col);
    INST_SIMPLE(proc, OP_DROP, line, col);
    INST_ARG(proc, OP_PUSH, &obj_false, line, col);
    INST_ARG(proc, OP_LABEL, label_exit, line, col);
}

void
compile_print(ProgramIr *program, Procedure *proc,
        size_t line, size_t col, Object *args) {
    while (args != NULL) {
        compile_object(program, proc, args->head);
        args = args->tail;
        INST_SIMPLE(proc, OP_PRINT, line, col);
    }
}

void
compile_not(ProgramIr *program, Procedure *proc,
        size_t line, size_t col, Object *args) {
    compile_object(program, proc, args->head);
    INST_SIMPLE(proc, OP_NOT, line, col);
}

void
compile_and(ProgramIr *program, Procedure *proc,
        size_t line, size_t col, Object *args) {
    size_t label_false = program->labels++;
    size_t label_exit = program->labels++;
    while (args != NULL) {
        compile_object(program, proc, args->head);
        args = args->tail;
        INST_ARG(proc, OP_JUMP_IF_FALSE, label_false, line, col);
    }
    INST_ARG(proc, OP_PUSH, &obj_true, line, col);
    INST_ARG(proc, OP_JUMP, label_exit, line, col);
    INST_ARG(proc, OP_LABEL, label_false, line, col);
    INST_ARG(proc, OP_PUSH, &obj_false, line, col);
    INST_ARG(proc, OP_LABEL, label_exit, line, col);
}

void
compile_or(ProgramIr *program, Procedure *proc,
        size_t line, size_t col, Object *args) {
    size_t label_true = program->labels++;
    size_t label_exit = program->labels++;
    while (args != NULL) {
        compile_object(program, proc, args->head);
        args = args->tail;
        INST_ARG(proc, OP_JUMP_IF_TRUE, label_true, line, col);
    }
    INST_ARG(proc, OP_PUSH, &obj_false, line, col);
    INST_ARG(proc, OP_JUMP, label_exit, line, col);
    INST_ARG(proc, OP_LABEL, label_true, line, col);
    INST_ARG(proc, OP_PUSH, &obj_true, line, col);
    INST_ARG(proc, OP_LABEL, label_exit, line, col);
}

void
compile_proc_call(ProgramIr *program, Procedure *proc, Object *obj) {
    size_t line = obj->line;
    size_t col = obj->col;
    if (obj->head->type == OBJ_TYPE_BUILTIN) {
        switch (obj->head->builtin) {
            case BUILTIN_ADD: {
                compile_arithmetic(program, proc, OP_ADD, line, col, obj->tail);
            } break;
            case BUILTIN_SUB: {
                compile_arithmetic(program, proc, OP_SUB, line, col, obj->tail);
            } break;
            case BUILTIN_MUL: {
                compile_arithmetic(program, proc, OP_MUL, line, col, obj->tail);
            } break;
            case BUILTIN_DIV: {
                compile_arithmetic(program, proc, OP_DIV, line, col, obj->tail);
            } break;
            case BUILTIN_MOD: {
                compile_arithmetic(program, proc, OP_MOD, line, col, obj->tail);
            } break;
            case BUILTIN_PRINT: {
                compile_print(program, proc, line, col, obj->tail);
            } break;
            case BUILTIN_NOT: {
                compile_not(program, proc, line, col, obj->tail);
            } break;
            case BUILTIN_AND: {
                compile_and(program, proc, line, col, obj->tail);
            } break;
            case BUILTIN_OR: {
                compile_or(program, proc, line, col, obj->tail);
            } break;
            case BUILTIN_EQ: {
                compile_numeric_cmp(program, proc, OP_JUMP_IF_NEQ, line, col, obj->tail);
            } break;
            case BUILTIN_GT: {
                compile_numeric_cmp(program, proc, OP_JUMP_IF_LE, line, col, obj->tail);
            } break;
            case BUILTIN_LT: {
                compile_numeric_cmp(program, proc, OP_JUMP_IF_GE, line, col, obj->tail);
            } break;
            case BUILTIN_GE: {
                compile_numeric_cmp(program, proc, OP_JUMP_IF_LT, line, col, obj->tail);
            } break;
            case BUILTIN_LE: {
                compile_numeric_cmp(program, proc, OP_JUMP_IF_GT, line, col, obj->tail);
            } break;
            default: {
                assert(false && "builtin not implemented");
            } break;
        }
    } else {
        assert(false && "compile_proc_call: not implemented");
    }
}

void
compile_object(ProgramIr *program, Procedure *proc, Object *obj) {
    switch (obj->type) {
        case OBJ_TYPE_NIL:
        case OBJ_TYPE_TRUE:
        case OBJ_TYPE_FALSE:
        case OBJ_TYPE_STRING:
        case OBJ_TYPE_FIXNUM: {
            INST_ARG(proc, OP_PUSH, obj, obj->line, obj->col);
        } break;
        case OBJ_TYPE_PAIR: { compile_proc_call(program, proc, obj); } break;
        // case OBJ_TYPE_IF: { compile_if(obj); } break;
        // case OBJ_TYPE_LAMBDA: { compile_lambda(obj); } break;
        // case OBJ_TYPE_DEF: { compile_def(obj); } break;
        // case OBJ_TYPE_SYMBOL: { compile_symbol(obj); } break;
        default: {
            // TODO: assert?
            fprintf(stderr, "NOT IMPLEMENTED: compile_object for ");
            OBJ_PRINT(obj);
            exit(-1);
        } break;
    }
}

ProgramIr
compile(Program program) {
    ProgramIr program_ir = {0};
    array_init(program_ir.procedures, 0);
    Procedure *main = proc_alloc(&program_ir, STRING("main"));

    for (size_t i = 0; i < array_size(program.roots); i++) {
        Object *root = program.roots[i];
        compile_object(&program_ir, main, root);
    }

    // DEBUG:...
    for (size_t i = 0; i < array_size(program_ir.procedures); ++i) {
        print_procedure(program_ir.procedures[i]);
    }

    return program_ir;
}

#endif // BDL_IR_H