aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/out.c
blob: d2c9df496de9c3e3fb6e86031099dac837733cef (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

/*
 * C code generator. Does not actually call the compiler,
 * that's done by the Makefile. The other `out*.c` files handle
 * specific parts of C code generation.
 *
 * Copyright © 2020-2025 Samuel Lidén Borell <samuel@kodafritt.se>
 *
 * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later
 */
#include <assert.h>
#include <stdio.h>
#include "compiler.h"
#include "out.h"
#include "token.h"

static void emit_header(void)
{
    outf(
        "/* Generated code from bootstrap compiler */\n"
        "#include \"rtl.h\"\n"
        "\n");
}

static void emit_footer(void)
{
}

void emit_ident(struct Ident *ident)
{
    outf("%.*s", ident->node.length, ident->node.name);
}

/* FIXME arrays, optional types, generics... */
void emit_typeref_prefix(struct TypeRef *tr)
{
    /* TODO output parentheses for nested types e.g. array of pointer etc. */
    switch (tr->kind) {
    case TR_UNKNOWN:
        /* TODO at the moment, this happens for unimplemented exprs:
                E_ARRAY */
        outf("int /*unimplemented stuff*/ ");
        /*ast_error("TR_UNKNOWN encountered at codegen time");*/
        break;
    case TR_VOID:
        unreachable();
    case TR_BOOL:
        outf("bool ");
        break;
    case TR_INT:
        outf("SlulInt ");
        break;
    case TR_CLASS:
        if ((tr->quals & Q_VAR) == 0) {
            outf("const ");
        }
        emit_type(tr->u.class_);
        outc(' ');
        outc('*');
    }
}

void emit_typeref_suffix(struct TypeRef *tr)
{
    /* TODO arrays? */
    (void)tr;
}

void emit_func_ident(struct Func *func)
{
    struct Type *class_ = func->class_;
    if (class_) {
        if (class_->outer) {
            assert(class_->outer->outer == NULL);
            emit_ident(&class_->outer->ident);
            outc('_');
        }
        emit_ident(&class_->ident);
        outc('_');
    }
    emit_ident(&func->ident);
}

void emit_type_name(struct Type *type)
{
    if (type->outer) {
        emit_ident(&type->outer->ident);
        outc('_');
    }
    emit_ident(&type->ident);
}

void emit_type(struct Type *type)
{
    outf("struct ");
    emit_type_name(type);
}


static void emit_module(struct Module *mod)
{
    struct Type *type, *inner;

    emit_string_constants();

    if (mod->types_list) {
        outc('\n');
        /* Pre-declare types */
        for (type = mod->types_list; type; type = type->next) {
            predeclare_type(type);
            for (inner = type->inner_types_list; inner; inner = inner->next) {
                assert(inner->inner_types_list == NULL);
                predeclare_type(inner);
            }
        }

        /* Emit types */
        /* XXX this is tricky if structs are allowed to be embedded
            without a pointer. Then it has to be done in the correct order */
        for (type = mod->types_list; type; type = type->next) {
            define_type(type);
            for (inner = type->inner_types_list; inner; inner = inner->next) {
                define_type(inner);
            }
        }
    }

    outc('\n');

    /* Pre-declare functions */
    predeclare_funcs(mod->funcs_list);
    for (type = mod->types_list; type; type = type->next) {
        current_type = type;
        predeclare_funcs(type->funcs_list);
        predeclare_funcs(type->ctors_list);
        for (inner = type->inner_types_list; inner; inner = inner->next) {
            current_type = inner;
            predeclare_funcs(inner->funcs_list);
            predeclare_funcs(inner->ctors_list);
        }
    }
    current_type = NULL;

    /* Emit function bodies */
    define_funcs(mod->funcs_list);
    for (type = mod->types_list; type; type = type->next) {
        current_type = type;
        define_funcs(type->funcs_list);
        define_funcs(type->ctors_list);
        for (inner = type->inner_types_list; inner; inner = inner->next) {
            current_type = inner;
            define_funcs(inner->funcs_list);
            define_funcs(inner->ctors_list);
        }
    }
    current_type = NULL;
}

static void emit_code(void)
{
    struct Module *mod;

    assert(current_type == NULL);
    assert(current_func == NULL);

    for (mod = modules; mod; mod = mod->next) {
        emit_module(mod);
    }


    /* TODO test code. replace this.
            maybe the stdlib should be in a separate object file?
            (which could contain `main()`) */
    beginf("int main(void) {\n");
    indentf("printf(\"Test\\n\");\n");
    indentf("return EXIT_SUCCESS;\n");
    endf("}\n");
}

void emit_c_code(const char *filename)
{
    outfile_path = filename;
    outfile = fopen(filename, "wb");
    if (!outfile) {
        io_error();
    }
    emit_header();
    emit_code();
    emit_footer();
    if (fclose(outfile) < 0) {
        io_error();
    }
}