about summary refs log tree commit diff
path: root/boot/transpile.c
blob: 5b27967a6af834604d3dc787a3dc8fd6abd24ac4 (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
/*
 * transpiler from a catskill intermediate translation unit,
 * to the c programming language, as the main translation target.
 *
 * walks a fully-lowered translation unit and emits (slightly ugly)
 * c source code.
 *
 * Copyright (c) 2025-2026, Mel G. <mel@rnrd.eu>
 *
 * SPDX-License-Identifier: MPL-2.0
 */

#pragma once

#include "catboot.h"

// the output of the transpilation pass.
// used for both file output and string buffer output.
struct Transpile_Output
{
    FILE* file;
    struct String_Buffer string;
};

struct Transpile_Output
transpile_output_from_file(FILE* file)
{
    check(file != nil, "transpile output file is nil");
    return (struct Transpile_Output){
        .file = file,
        .string = string_buffer_empty(),
    };
}

#define TRANSPILE_OUTPUT_MAX_LENGTH 262144 // 256 KiB

struct Transpile_Output
transpile_output_from_string(void)
{
    return (struct Transpile_Output){
        .file = nil,
        .string = string_buffer_new(TRANSPILE_OUTPUT_MAX_LENGTH),
    };
}

#define TRANSPILE_OUTPUT_MAX_WRITE_LENGTH 4096

void
transpile_output_write(struct Transpile_Output* output, const ascii* format, ...)
{
    va_list args;
    va_start(args, format);

    if (output->file) {
        vfprintf(output->file, format, args);
    } else {
        ascii buffer[TRANSPILE_OUTPUT_MAX_WRITE_LENGTH];
        int written = vsnprintf(buffer, TRANSPILE_OUTPUT_MAX_WRITE_LENGTH, format, args);
        check(written >= 0 && (uint)written < TRANSPILE_OUTPUT_MAX_LENGTH,
              "transpile output string buffer overflow");

        string_buffer_append_c_str(&output->string, buffer);
    }

    va_end(args);
}

struct String
transpile_output_string(struct Transpile_Output* output)
{
    check(output->file == nil, "transpile output is not a string");
    return string_buffer_to_string(&output->string);
}

FILE*
transpile_output_file(struct Transpile_Output* output)
{
    check(output->file != nil, "transpile output is not a file");
    return output->file;
}

struct Transpiler
{
    struct Transpile_Output output;
    struct Unit* unit;
};

void
transpiler_new(struct Transpiler* transpiler, struct Transpile_Output output)
{
    *transpiler = (struct Transpiler){
        .output = output,
    };
}

#define TRANSPILE_WRITE(...) transpile_output_write(&transpiler->output, __VA_ARGS__)

// emit the forward declaration for a single type.
// TODO: per-kind emission lands in a follow-up commit.
void
transpile_type_forward(struct Transpiler* transpiler, struct Type* type)
{
    (void)transpiler;
    (void)type;
}

// emit the full definition for a single type.
// TODO: per-kind emission lands in a follow-up commit.
void
transpile_type_definition(struct Transpiler* transpiler, struct Type* type)
{
    (void)transpiler;
    (void)type;
}

// emit a function signature (used for both forward-decls and the line
// preceding a function body).
// TODO: per-kind emission lands in a follow-up commit.
void
transpile_function_signature(struct Transpiler* transpiler, struct Function* function)
{
    (void)transpiler;
    (void)function;
}

// emit a function body in the form `signature { block }`.
// TODO: per-kind emission lands in a follow-up commit.
void
transpile_function_body(struct Transpiler* transpiler, struct Function* function)
{
    (void)transpiler;
    (void)function;
}

// walk a lowered translation unit and emit c source into the transpiler's output.
// the order of the translation unit, and thus the final source output is fixed.
// preamble, then imports, type forwards, type definitions in topological dependency
// order, function forwads, function definitions, and runtime trailer (if main present).
int
transpile_unit(struct Transpiler* transpiler, struct Unit* unit)
{
    transpiler->unit = unit;

    TRANSPILE_WRITE("#include \"core.c\"\n");

    FOR_EACH_ARRAY (struct Import, import, &unit->imports) {
        TRANSPILE_WRITE("#include \"%.*s\"\n", (int)import->path.length, import->path.data);
    }

    FOR_EACH_ARRAY (struct Type*, type_slot, &unit->types.entries) {
        transpile_type_forward(transpiler, *type_slot);
    }

    FOR_EACH_ARRAY (Type_Id, ordered_id, &unit->type_emission_order) {
        struct Type* type = *array_at(struct Type*, &unit->types.entries, *ordered_id);
        transpile_type_definition(transpiler, type);
    }

    bool main_takes_args = false;
    bool has_main = false;
    FOR_EACH_ARRAY (struct Function*, function_slot, &unit->functions.entries) {
        struct Function* function = *function_slot;
        transpile_function_signature(transpiler, function);
        if (function->is_main) {
            has_main = true;
            main_takes_args = function->main_takes_args;
        }
    }

    FOR_EACH_ARRAY (struct Function*, function_slot, &unit->functions.entries) {
        transpile_function_body(transpiler, *function_slot);
    }

    if (has_main) {
        if (main_takes_args) TRANSPILE_WRITE("#define CATSKILL_MAIN_TAKES_ARGS\n");
        TRANSPILE_WRITE("#include \"runtime.c\"\n");
    }

    return 0;
}