public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
fa2a08290a3ed6b2f557cb54ce768270c4f68d52 blob 19641 bytes (raw)

  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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
 
/*
 * Copyright (C) 2024 olang maintainers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include <assert.h>
#include <stdint.h>
#include <stdio.h>

#include "codegen_linux_x86_64.h"
#include "list.h"
#include "map.h"
#include "scope.h"

#define SYS_exit (60)
#define PTR_HEX_CSTR_SIZE (18 + 1)

// FIXME: move label_index to codegen_linux_x86_64_t structure
size_t label_index;

static void
codegen_linux_x86_64_emit_start_entrypoint(codegen_x86_64_t *codegen);

static void
codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_t *fn);

static size_t
type_to_bytes(type_t *type);

static char *
bytes_to_mov(size_t bytes);

static char *
bytes_to_rax(size_t bytes);

void
codegen_linux_x86_64_init(codegen_x86_64_t *codegen, arena_t *arena, FILE *out)
{
    assert(codegen);
    assert(arena);
    assert(codegen);
    codegen->base_offset = 0;
    codegen->symbols_stack_offset = map_new(arena);
    codegen->out = out;
    codegen->arena = arena;
}

void
codegen_linux_x86_64_emit_program(codegen_x86_64_t *codegen, ast_node_t *node)
{
    label_index = 0;
    codegen_linux_x86_64_emit_start_entrypoint(codegen);

    assert(node->kind == AST_NODE_PROGRAM);
    ast_program_t program = node->as_program;

    ast_fn_definition_t fn = program.fn->as_fn_def;

    assert(string_view_eq_to_cstr(fn.identifier, "main"));
    codegen_linux_x86_64_emit_function(codegen, &fn);
}

static void
codegen_linux_x86_64_emit_start_entrypoint(codegen_x86_64_t *codegen)
{
    fprintf(codegen->out, ".text\n");
    fprintf(codegen->out, ".globl _start\n\n");

    fprintf(codegen->out, "_start:\n");
    fprintf(codegen->out, "    call main\n");
    fprintf(codegen->out, "    mov %%eax, %%edi\n");
    fprintf(codegen->out, "    mov $%d, %%eax\n", SYS_exit);
    fprintf(codegen->out, "    syscall\n");
}

static size_t
codegen_linux_x86_64_get_next_label(void)
{
    return ++label_index;
}

static void
codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
{
    switch (expr_node->kind) {
        case AST_NODE_LITERAL: {
            ast_literal_t literal_u32 = expr_node->as_literal;
            assert(literal_u32.kind == AST_LITERAL_U32);
            uint32_t n = literal_u32.as_u32;

            fprintf(codegen->out, "    mov $%d, %%rax\n", n);
            return;
        }
        case AST_NODE_REF: {
            ast_ref_t ref = expr_node->as_ref;

            symbol_t *symbol = scope_lookup(ref.scope, ref.identifier);
            assert(symbol);

            char symbol_ptr[PTR_HEX_CSTR_SIZE];
            sprintf(symbol_ptr, "%p", (void *)symbol);

            size_t *offset = (size_t *)map_get(codegen->symbols_stack_offset, symbol_ptr);
            assert(offset);

            size_t type_size = type_to_bytes(&symbol->type);

            fprintf(
                codegen->out, "    %s -%ld(%%rbp), %s\n", bytes_to_mov(type_size), *offset, bytes_to_rax(type_size));
            return;
        }
        case AST_NODE_BINARY_OP: {
            ast_binary_op_t bin_op = expr_node->as_bin_op;
            switch (bin_op.kind) {
                case AST_BINOP_ADDITION: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    add %%rcx, %%rax\n");

                    return;
                }
                case AST_BINOP_MULTIPLICATION: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    mul %%rcx\n");

                    return;
                }
                case AST_BINOP_DIVISION: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    xor %%rdx, %%rdx\n");
                    fprintf(codegen->out, "    div %%rcx\n");

                    return;
                }
                case AST_BINOP_REMINDER: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");
                    fprintf(codegen->out, "    xor %%edx, %%edx\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    xor %%rdx, %%rdx\n");
                    fprintf(codegen->out, "    div %%rcx\n");
                    fprintf(codegen->out, "    mov %%edx, %%eax\n");

                    return;
                }
                case AST_BINOP_SUBTRACTION: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    sub %%rcx, %%rax\n");

                    return;
                }
                case AST_BINOP_CMP_EQ: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    cmp %%rcx, %%rax\n");
                    fprintf(codegen->out, "    sete %%al\n");
                    fprintf(codegen->out, "    movzb %%al, %%rax\n");

                    return;
                }
                case AST_BINOP_CMP_LT: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    cmp %%rcx, %%rax\n");
                    fprintf(codegen->out, "    setl %%al\n");
                    fprintf(codegen->out, "    movzb %%al, %%rax\n");

                    return;
                }
                case AST_BINOP_CMP_GT: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    cmp %%rcx, %%rax\n");
                    fprintf(codegen->out, "    setg %%al\n");
                    fprintf(codegen->out, "    movzb %%al, %%rax\n");

                    return;
                }
                case AST_BINOP_CMP_NEQ: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    cmp %%rcx, %%rax\n");
                    fprintf(codegen->out, "    setne %%al\n");
                    fprintf(codegen->out, "    movzb %%al, %%rax\n");

                    return;
                }
                case AST_BINOP_CMP_LEQ: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    cmp %%rcx, %%rax\n");
                    fprintf(codegen->out, "    setle %%al\n");
                    fprintf(codegen->out, "    movzb %%al, %%rax\n");

                    return;
                }
                case AST_BINOP_CMP_GEQ: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    cmp %%rcx, %%rax\n");
                    fprintf(codegen->out, "    setge %%al\n");
                    fprintf(codegen->out, "    movzb %%al, %%rax\n");

                    return;
                }
                case AST_BINOP_BITWISE_LSHIFT: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    shl %%cl, %%rax\n");

                    return;
                }
                case AST_BINOP_BITWISE_RSHIFT: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    shr %%cl, %%rax\n");

                    return;
                }
                case AST_BINOP_BITWISE_XOR: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    xor %%ecx, %%eax\n");

                    return;
                }
                case AST_BINOP_BITWISE_AND: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    and %%ecx, %%eax\n");

                    return;
                }
                case AST_BINOP_BITWISE_OR: {
                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    push %%rax\n");

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    pop %%rcx\n");
                    fprintf(codegen->out, "    or %%ecx, %%eax\n");

                    return;
                }
                case AST_BINOP_LOGICAL_AND: {
                    size_t label_exit = codegen_linux_x86_64_get_next_label();

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    cmp $0, %%rax\n");
                    fprintf(codegen->out, "    je .L%ld\n", label_exit);

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    cmp $0, %%rax\n");
                    fprintf(codegen->out, "    je .L%ld\n", label_exit);
                    fprintf(codegen->out, "    mov $1, %%rax\n");
                    fprintf(codegen->out, ".L%ld:\n", label_exit);

                    return;
                }
                case AST_BINOP_LOGICAL_OR: {
                    size_t label_t = codegen_linux_x86_64_get_next_label();
                    size_t label_f = codegen_linux_x86_64_get_next_label();

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.lhs);
                    fprintf(codegen->out, "    cmp $0, %%rax\n");
                    fprintf(codegen->out, "    jne .L%ld\n", label_t);

                    codegen_linux_x86_64_emit_expression(codegen, bin_op.rhs);
                    fprintf(codegen->out, "    cmp $0, %%rax\n");
                    fprintf(codegen->out, "    je .L%ld\n", label_f);

                    fprintf(codegen->out, ".L%ld:\n", label_t);
                    fprintf(codegen->out, "    mov $1, %%rax\n");
                    fprintf(codegen->out, ".L%ld:\n", label_f);

                    return;
                }
                default: {
                    assert(0 && "unsupported binary operation");
                    return;
                }
            }
        }
        default:
            assert(0 && "unsupported expression");
    }
}
static void
codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
{
    size_t block_offset = codegen->base_offset;
    size_t nodes_len = list_size(block->nodes);

    for (size_t i = 0; i < nodes_len; ++i) {
        ast_node_t *node = list_get(block->nodes, i)->value;
        switch (node->kind) {
            case AST_NODE_RETURN_STMT: {
                ast_return_stmt_t return_stmt = node->as_return_stmt;

                ast_node_t *expr = return_stmt.data;

                codegen_linux_x86_64_emit_expression(codegen, expr);

                fprintf(codegen->out, "    mov %%rbp, %%rsp\n");
                fprintf(codegen->out, "    ret\n");

                break;
            }

            case AST_NODE_VAR_DEF: {
                ast_var_definition_t var_def = node->as_var_def;
                scope_t *scope = var_def.scope;

                symbol_t *symbol = scope_lookup(scope, var_def.identifier);
                assert(symbol);

                char symbol_ptr[PTR_HEX_CSTR_SIZE];
                sprintf(symbol_ptr, "%p", (void *)symbol);

                if (var_def.value) {
                    codegen_linux_x86_64_emit_expression(codegen, var_def.value);
                }

                size_t *offset = arena_alloc(codegen->arena, sizeof(size_t));
                *offset = codegen->base_offset;

                map_put(codegen->symbols_stack_offset, symbol_ptr, offset);

                size_t type_size = type_to_bytes(&symbol->type);

                fprintf(codegen->out,
                        "    %s %s, -%ld(%%rbp)\n",
                        bytes_to_mov(type_size),
                        bytes_to_rax(type_size),
                        codegen->base_offset);
                codegen->base_offset += type_size;

                break;
            }

            case AST_NODE_IF_STMT: {
                ast_if_stmt_t if_stmt = node->as_if_stmt;

                ast_node_t *cond = if_stmt.cond;
                ast_node_t *then = if_stmt.then;
                ast_node_t *_else = if_stmt._else;

                size_t end_if_label = codegen_linux_x86_64_get_next_label();
                size_t end_else_label = codegen_linux_x86_64_get_next_label();

                codegen_linux_x86_64_emit_expression(codegen, cond);
                fprintf(codegen->out, "    cmp $1, %%rax\n");
                fprintf(codegen->out, "    jnz .L%ld\n", end_if_label);

                assert(then->kind == AST_NODE_BLOCK && "invalid if-then block");
                ast_block_t then_block = then->as_block;

                codegen_linux_x86_64_emit_block(codegen, &then_block);
                fprintf(codegen->out, "    jmp .L%ld\n", end_else_label);

                fprintf(codegen->out, ".L%ld:\n", end_if_label);

                if (_else != NULL) {
                    ast_block_t else_block = _else->as_block;
                    codegen_linux_x86_64_emit_block(codegen, &else_block);
                }

                fprintf(codegen->out, ".L%ld:\n", end_else_label);

                break;
            }
            default: {
                // FIXME: improve error: replace the node->kind to a string representation
                fprintf(stderr, "node kind %d not supported\n", node->kind);
                assert(0 && "unsupported block statement");
                break;
            }
        }
    }

    codegen->base_offset = block_offset;
}

static size_t
type_to_bytes(type_t *type)
{
    switch (type->kind) {
        case TYPE_PRIMITIVE: {
            switch (type->as_primitive) {
                case TYPE_U8:
                    return 1;
                case TYPE_U16:
                    return 2;
                case TYPE_U32:
                    return 4;
                case TYPE_U64:
                    return 8;
            }

            assert(0 && "unreachable");
            return 0;
        }
        case TYPE_USER_DEFINED: {
            assert(0 && "user defined types are not defined yet");
        }
    }

    assert(0 && "unreachable");
    return 0;
}

static size_t
calculate_fn_local_size(scope_t *scope)
{
    assert(scope);

    size_t local_size = 0;

    map_kv_t *kvs[scope->symbols->size];

    map_get_kvs(scope->symbols, kvs);

    for (size_t i = 0; i < scope->symbols->size; ++i) {
        symbol_t *symbol = (symbol_t *)kvs[i]->value;
        local_size += type_to_bytes(&symbol->type);
    }

    size_t max_child_local_size = 0;

    list_item_t *item = list_head(scope->children);

    while (item != NULL) {
        size_t child_local_size = calculate_fn_local_size((scope_t *)item->value);

        if (child_local_size > max_child_local_size) {
            max_child_local_size = child_local_size;
        }

        item = list_next(item);
    }

    return local_size + max_child_local_size;
}

static void
codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_t *fn)
{
    codegen->base_offset = 8;
    ast_node_t *block_node = fn->block;
    fprintf(codegen->out, "" SV_FMT ":\n", SV_ARG(fn->identifier));

    fprintf(codegen->out, "    mov %%rsp, %%rbp\n");

    size_t local_size = calculate_fn_local_size(fn->scope);

    // TODO: get the local_size from function scope

    if (local_size != 0) {
        fprintf(codegen->out, "    sub $%ld, %%rsp\n", local_size);
    }

    assert(block_node->kind == AST_NODE_BLOCK);
    ast_block_t block = block_node->as_block;

    codegen_linux_x86_64_emit_block(codegen, &block);
}

static char *
bytes_to_mov(size_t bytes)
{
    if (bytes <= 1) {
        return "movb";
    } else if (bytes <= 2) {
        return "movw";
    } else if (bytes <= 4) {
        return "movl";
    }
    return "movq";
}

static char *
bytes_to_rax(size_t bytes)
{
    if (bytes <= 1) {
        return "%ah";
    } else if (bytes <= 2) {
        return "%ax";
    } else if (bytes <= 4) {
        return "%eax";
    }
    return "%rax";
}
debug log:

solving fa2a082 ...
found fa2a082 in http://lists.johnnyrichard.com/olang/20240921082437.396691-6-carlos@maniero.me/
found 415c81b in http://lists.johnnyrichard.com/olang/20240921082437.396691-5-carlos@maniero.me/
found 1fa6c58 in https://git.johnnyrichard.com/olang.git
preparing index
index prepared:
100644 1fa6c589ad72ffd0ee38a4c795dc0197b446e9c5	src/codegen_linux_x86_64.c

applying [1/2] http://lists.johnnyrichard.com/olang/20240921082437.396691-5-carlos@maniero.me/
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 1fa6c58..415c81b 100644


applying [2/2] http://lists.johnnyrichard.com/olang/20240921082437.396691-6-carlos@maniero.me/
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 415c81b..fa2a082 100644

Checking patch src/codegen_linux_x86_64.c...
Applied patch src/codegen_linux_x86_64.c cleanly.
Checking patch src/codegen_linux_x86_64.c...
Applied patch src/codegen_linux_x86_64.c cleanly.

index at:
100644 fa2a08290a3ed6b2f557cb54ce768270c4f68d52	src/codegen_linux_x86_64.c

Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox