aboutsummaryrefslogtreecommitdiffstats
path: root/src/compiler.h
blob: 441b66574694b79c647b17bcf79d04d3c301aad4 (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
#ifndef BDL_COMPILER_H
#define BDL_COMPILER_H


#define PRELUDE_FILE "src/x86_64/prelude.asm"
#define POSTLUDE_FILE "src/x86_64/postlude.asm"

#define NIL_VAL      1
#define BOOL_MASK    3
#define BOOL_TAG     3
#define BOOL_SHIFT   2
#define TRUE_VAL     ((1 << BOOL_SHIFT) | BOOL_TAG)
#define FALSE_VAL    ((0 << BOOL_SHIFT) | BOOL_TAG)
#define FIXNUM_MASK  1
#define FIXNUM_TAG   0
#define FIXNUM_SHIFT 1

void compile_object(Object *obj);
void compile_fixnum(Object *obj);
void compile_proc_call(Object *obj);
void compile(Root *roots);

char *
generate_label(void) {
    // Generate a unique label allocated on the heap. The caller is responsible
    // for freeing the memory.
    static size_t label_counter = 0;
    char buf[32];
    memset(buf, 0, 32);
    sprintf(buf, ".BDLL%ld", label_counter++);
    char * ret = malloc(strlen(buf));
    memcpy(ret, buf, strlen(buf));
    return ret;
}

void
emit_file(char *file_name) {
    FILE *file = fopen(file_name, "r");
    if (!file) {
        fprintf(stderr, "error: couldn't open input file: %s\n", file_name);
        exit(EXIT_FAILURE);
    }
    char buf[1024];
    size_t n = 0;
    while ((n = fread(&buf, 1, 1024, file)) > 0) {
        fwrite(buf, 1, n, stdout);
    }
}

void
compile_fixnum(Object *obj) {
    printf("    ;; --> compile_fixnum\n");
    printf("    mov     rax, %ld\n", (obj->fixnum << FIXNUM_SHIFT) | FIXNUM_TAG);
    printf("    push    rax\n");
    printf("    ;; <-- compile_fixnum\n");
}

void
compile_boolean(Object *obj) {
    printf("    ;; --> compile_boolean\n");
    int is_true = obj->type == OBJ_TYPE_TRUE;
    printf("    mov     rax, %d\n", (is_true << BOOL_SHIFT) | BOOL_TAG);
    printf("    push    rax\n");
    printf("    ;; <-- compile_boolean\n");
}

void
compile_nil(void) {
    printf("    ;; --> compile_nil\n");
    printf("    mov     rax, %d\n", NIL_VAL);
    printf("    push    rax\n");
    printf("    ;; <-- compile_nil\n");
}

typedef enum OpType {
    // Arithmetic.
    OP_ADD,
    OP_SUB,
    OP_MUL,
    OP_DIV,
    OP_MOD,
    // Type predicates.
    OP_IS_NIL,
    OP_IS_ZERO,
    OP_IS_BOOL,
    OP_IS_FIXNUM,
    // Logic operations.
    OP_NOT,
    OP_AND,
    OP_OR,
    OP_EQUAL,
    OP_GREATER,
    OP_LESS,
    OP_GREATER_EQ,
    OP_LESS_EQ,
} OpType;

void
compile_type_predicate(OpType op, Object* args) {
    printf("    ;; --> compile_type_predicate\n");
    compile_object(args->head);
    printf("    pop     rax\n");
    switch (op) {
        case OP_IS_NIL: {
            printf("    cmp     rax, %d\n", NIL_VAL);
        } break;
        case OP_IS_ZERO: {
            printf("    cmp     rax, 0\n");
        } break;
        case OP_IS_BOOL: {
            printf("    and     rax, %d\n", BOOL_MASK);
            printf("    cmp     rax, %d\n", BOOL_TAG);
        } break;
        case OP_IS_FIXNUM: {
            printf("    and     rax, %d\n", FIXNUM_MASK);
            printf("    cmp     rax, %d\n", FIXNUM_TAG);
        } break;
        default: break;
    }
    printf("    mov     rax, 0\n");
    printf("    sete    al\n");
    printf("    shl     rax, %d\n", BOOL_SHIFT);
    printf("    or      rax, %d\n", BOOL_TAG);
    printf("    push    rax\n");
    printf("    ;; <-- compile_type_predicate\n");
}

void
compile_logic_list(OpType op, Object* args) {
    printf("    ;; --> compile_logic_list\n");
    compile_object(args->head);
    if (op == OP_NOT) {
        printf("    pop     rax\n");
        printf("    cmp     rax, %d\n", FALSE_VAL);
        printf("    mov     rax, 0\n");
        printf("    sete    al\n");
        printf("    shl     rax, %d\n", BOOL_SHIFT);
        printf("    or      rax, %d\n", BOOL_TAG);
        printf("    push    rax\n");
        printf("    ;; <-- compile_logic_list\n");
        return;
    }

    // TODO: Make sure to stop evaluating if the condition is not met.
    args = args->tail;
    while (args != NULL) {
        compile_object(args->head);
        args = args->tail;

        // Current value.
        printf("    pop     rcx\n");
        printf("    cmp     rcx, %d\n", FALSE_VAL);
        printf("    mov     rcx, 0\n");
        printf("    setne   cl\n");

        // Previous value.
        printf("    pop     rax\n");
        printf("    cmp     rax, %d\n", FALSE_VAL);
        printf("    mov     rax, 0\n");
        printf("    setne   al\n");

        switch (op) {
            case OP_AND: { printf("    and     al, cl\n"); } break;
            case OP_OR:  { printf("    or      al, cl\n"); } break;
            default: break;
        }
        printf("    shl     rax, %d\n", BOOL_SHIFT);
        printf("    or      rax, %d\n", BOOL_TAG);
        printf("    push    rax\n");
    }
    printf("    ;; <-- compile_logic_list\n");
}

void
compile_cmp_list(OpType op, Object* args) {
    printf("    ;; --> compile_cmp_list\n");
    compile_object(args->head);

    // TODO: Make sure to stop evaluating if the condition is not met.
    // FIXME: Only working for two argument lists right now because we can't
    // accumulate the bool and compare it with an int. We can fix it by passing
    // two values on the stack (previous value and current result) or by only
    // passing a previous value and having a conditional jump (need labels for
    // that).
    args = args->tail;
    while (args != NULL) {
        compile_object(args->head);
        args = args->tail;

        // Current value.
        printf("    pop     rcx\n");
        printf("    sar     rcx, %d\n", FIXNUM_SHIFT);

        // Previous value.
        printf("    pop     rax\n");
        printf("    sar     rax, %d\n", FIXNUM_SHIFT);

        printf("    cmp     rax, rcx\n");
        printf("    mov     rax, 0\n");

        switch (op) {
            case OP_EQUAL: {
                printf("    sete    al\n");
            } break;
            case OP_GREATER: {
                printf("    setg    al\n");
            } break;
            case OP_LESS: {
                printf("    setl    al\n");
            } break;
            case OP_GREATER_EQ: {
                printf("    setge   al\n");
            } break;
            case OP_LESS_EQ: {
                printf("    setle   al\n");
            } break;
            default: break;
        }
        printf("    shl     rax, %d\n", BOOL_SHIFT);
        printf("    or      rax, %d\n", BOOL_TAG);
        printf("    push    rax\n");
    }
    printf("    ;; <-- compile_cmp_list\n");
}

void
compile_arithmetic_list(OpType op, Object* args) {
    printf("    ;; --> compile_arithmetic\n");
    compile_object(args->head);
    args = args->tail;
    while (args != NULL) {
        compile_object(args->head);
        args = args->tail;
        printf("    pop     rcx\n");
        printf("    pop     rax\n");
        switch (op) {
            case OP_ADD: { printf("    add     rax, rcx\n"); } break;
            case OP_SUB: { printf("    sub     rax, rcx\n"); } break;
            case OP_MUL: {
                printf("    sar     rax, %d\n", FIXNUM_SHIFT);
                printf("    sar     rcx, %d\n", FIXNUM_SHIFT);
                printf("    mul     rcx\n");
                printf("    shl     rax, %d\n", FIXNUM_SHIFT);
            } break;
            case OP_DIV: {
                printf("    sar     rax, %d\n", FIXNUM_SHIFT);
                printf("    sar     rcx, %d\n", FIXNUM_SHIFT);
                printf("    mov     rdx, 0\n");
                printf("    div     rcx\n");
                printf("    shl     rax, %d\n", FIXNUM_SHIFT);
            } break;
            case OP_MOD: {
                printf("    sar     rax, %d\n", FIXNUM_SHIFT);
                printf("    sar     rcx, %d\n", FIXNUM_SHIFT);
                printf("    mov     rdx, 0\n");
                printf("    div     rcx\n");
                printf("    mov     rax, rdx\n");
                printf("    shl     rax, %d\n", FIXNUM_SHIFT);
            } break;
            default: break;
        }
        printf("    push    rax\n");
    }
    printf("    ;; <-- compile_arithmetic\n");
}

void
compile_proc_call(Object *obj) {
    // TODO: Probably we want to use a hash table for these lookups that is
    // initialized at the start of the compilation procedure.
    if (sv_equal(&obj->head->text, &STRING("+"))) {
        compile_arithmetic_list(OP_ADD, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("-"))) {
        compile_arithmetic_list(OP_SUB, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("*"))) {
        compile_arithmetic_list(OP_MUL, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("/"))) {
        compile_arithmetic_list(OP_DIV, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("%"))) {
        compile_arithmetic_list(OP_MOD, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("nil?"))) {
        compile_type_predicate(OP_IS_NIL, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("zero?"))) {
        compile_type_predicate(OP_IS_ZERO, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("fixnum?"))) {
        compile_type_predicate(OP_IS_FIXNUM, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("bool?"))) {
        compile_type_predicate(OP_IS_BOOL, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("display"))) {
        compile_object(obj->tail->head);
        printf("    pop     rdi\n");
        printf("    call    display\n");
    } else if (sv_equal(&obj->head->text, &STRING("not"))) {
        compile_logic_list(OP_NOT, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("and"))) {
        compile_logic_list(OP_AND, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("or"))) {
        compile_logic_list(OP_OR, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("="))) {
        compile_cmp_list(OP_EQUAL, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING(">"))) {
        compile_cmp_list(OP_GREATER, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("<"))) {
        compile_cmp_list(OP_LESS, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING(">="))) {
        compile_cmp_list(OP_GREATER_EQ, obj->tail);
    } else if (sv_equal(&obj->head->text, &STRING("<="))) {
        compile_cmp_list(OP_LESS_EQ, obj->tail);
    } else {
        fprintf(stderr, "error: not implemented\n");
        exit(EXIT_FAILURE);
    }
}

void
compile_object(Object *obj) {
    switch (obj->type) {
        case OBJ_TYPE_NIL: { compile_nil(); } break;
        case OBJ_TYPE_TRUE:
        case OBJ_TYPE_FALSE: { compile_boolean(obj); } break;
        case OBJ_TYPE_FIXNUM: { compile_fixnum(obj); } break;
        case OBJ_TYPE_PAIR: { compile_proc_call(obj); } break;
        default: break;
    }
}

void
compile(Root *roots) {
    printf("%%define NIL_VAL      %d\n", NIL_VAL);
    printf("%%define BOOL_MASK    %d\n", BOOL_MASK);
    printf("%%define BOOL_TAG     %d\n", BOOL_TAG);
    printf("%%define BOOL_SHIFT   %d\n", BOOL_SHIFT);
    printf("%%define FIXNUM_MASK  %d\n", FIXNUM_MASK);
    printf("%%define FIXNUM_TAG   %d\n", FIXNUM_TAG);
    printf("%%define FIXNUM_SHIFT %d\n", FIXNUM_SHIFT);
    printf("\n");
    emit_file(PRELUDE_FILE);
    for (size_t i = 0; i < array_size(roots); i++) {
        Object *root = roots[i];
        compile_object(root);
        // OBJ_PRINT(root);
    }
    emit_file(POSTLUDE_FILE);
}

#endif // BDL_COMPILER_H