public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
72db1c66b45f492e2b8af460a9a01f87d563dc67 blob 2930 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
 
/*
 * 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"

#define SYS_exit (60)

static void
codegen_linux_x86_64_emit_start_entrypoint(FILE *out);

static void
codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn);

void
codegen_linux_x86_64_emit_program(FILE *out, ast_node_t *node)
{
    codegen_linux_x86_64_emit_start_entrypoint(out);

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

    ast_fn_definition_t fn = program.fn->data.as_fn_def;

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

static void
codegen_linux_x86_64_emit_start_entrypoint(FILE *out)
{
    fprintf(out, ".text\n");
    fprintf(out, ".globl _start\n\n");

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

static void
codegen_linux_x86_64_emit_expression(FILE *out, ast_node_t *expr_node)
{
    switch (expr_node->kind) {
        case AST_NODE_LITERAL: {
            ast_literal_t literal_u32 = expr_node->data.as_literal;
            assert(literal_u32.kind == AST_LITERAL_U32);
            uint32_t exit_code = literal_u32.value.as_u32;

            fprintf(out, "    mov $%d, %%eax\n", exit_code);
            return;
        }
        case AST_NODE_BINARY_OP:
        default:
            assert(0 && "NOT IMPLEMENTED");
    }
}

static void
codegen_linux_x86_64_emit_function(FILE *out, ast_fn_definition_t *fn)
{
    ast_node_t *block_node = fn->block;
    assert(block_node->kind == AST_NODE_BLOCK);
    ast_block_t block = block_node->data.as_block;

    assert(list_size(block.nodes) == 1);

    list_item_t *nodes_item = list_get(block.nodes, 0);
    ast_node_t *return_node = nodes_item->value;
    assert(return_node->kind == AST_NODE_RETURN_STMT);
    ast_return_stmt_t return_stmt = return_node->data.as_return_stmt;

    ast_node_t *expr = return_stmt.data;

    fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier));

    codegen_linux_x86_64_emit_expression(out, expr);

    fprintf(out, "    ret\n");
}
debug log:

solving 72db1c6 ...
found 72db1c6 in http://lists.johnnyrichard.com/olang/20240427121832.203068-2-johnny@johnnyrichard.com/
found d4f8222 in https://git.johnnyrichard.com/olang.git
preparing index
index prepared:
100644 d4f8222c193a09ecad571a7c6edc30d8e6775be9	src/codegen_linux_x86_64.c

applying [1/1] http://lists.johnnyrichard.com/olang/20240427121832.203068-2-johnny@johnnyrichard.com/
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index d4f8222..72db1c6 100644

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

index at:
100644 72db1c66b45f492e2b8af460a9a01f87d563dc67	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