aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/main.c
blob: 2fe02bf060543aed4742951df86575ff5ce18931 (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

/*
 * Main entry point of the bootstrap compiler.
 *
 * Copyright © 2025-2026 Samuel Lidén Borell <samuel@kodafritt.se>
 *
 * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later
 */
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "compiler.h"
#include "token.h"

#if defined(_WIN32) || defined(_WIN64)
    #define DIRSEP '\\'
#else
    #define DIRSEP '/'
#endif

#define PATH_LIMIT 512

static size_t ifacedir_len = 0;
static const char *ifacedir = NULL;

static size_t rootdir_len = 0;
static const char *rootdir;
static FILE *f = NULL;

static const char *output_c_filename = NULL;
const char *current_filename = NULL;
static char *last_alloced_filename = NULL;
unsigned current_line = 0;

NORETURN void error(const char *s)
{
    error_len(s, NULL, 0);
}

NORETURN void error_str(const char *s, const char *arg)
{
    error_len(s, arg, arg ? strlen(arg) : 0);
}

NORETURN void error_len(const char *s, const char *arg, size_t arglen)
{
    assert(arglen <= INT_MAX);
    fprintf(stderr, "%s:%u: %s%s%.*s\n", current_filename, current_line,
            s,
            arg ? ": " : "",
            (int)arglen, arg ? arg : "");
    if (output_c_filename) {
        remove(output_c_filename);
    }
    exit(EXIT_FAILURE);
}

NORETURN void error_token(const char *s, const struct LexemeInfo *li)
{
    error_len(s, li->string, li->len);
}

NORETURN void error_ident(const char *s, const struct Ident *ident)
{
    error_len(s, ident->node.name, ident->node.length);
}

void set_srcloc(const struct SourceLocation *srcloc)
{
    assert(srcloc->filename != NULL);
    current_filename = srcloc->filename;
    current_line = srcloc->line;
}

NORETURN void error_at_ident(const char *s, const struct Ident *ident)
{
    set_srcloc(&ident->srcloc);
    error_len(s, ident->node.name, ident->node.length);
}

NORETURN void error_var(const char *s, const struct Var *var)
{
    error_ident(s, &var->ident);
}

void warning(const char *s)
{
    fprintf(stderr, "%s:%u: warning: %s\n",
            current_filename, current_line, s);
}

static void open_file(const char *dir, size_t dirlen, const char *filename)
{
    size_t filename_len, total_len;
    char *fullpath;

    filename_len = strlen(filename);
    total_len = dirlen + 1 + filename_len + 1;
    if (total_len > PATH_LIMIT) {
        FAIL("filename too long");
    }
    fullpath = malloc(total_len);
    NO_NULL(fullpath);
    /* Root dir */
    memcpy(fullpath, dir, dirlen);
    fullpath[dirlen] = DIRSEP;
    /* Source file */
    memcpy(fullpath+dirlen+1, filename, filename_len);
    replacechar(fullpath+dirlen+1, '/', DIRSEP, filename_len);
    fullpath[dirlen+1+filename_len] = '\0';

    f = fopen(fullpath, "rb");
    if (!f) {
        perror(fullpath);
        exit(EXIT_FAILURE);
    }
    last_alloced_filename = fullpath;
    current_filename = fullpath;
    current_line = 0;
}

static void close_file(void)
{
    assert(f != NULL);
    NO_NEG(fclose(f));
    f = NULL;
    current_filename = NULL;
    current_line = 0;
}

static void free_last_filename(void)
{
    free(last_alloced_filename);
    last_alloced_filename = NULL;
}

static void completed_one_module(void)
{
    if (unbound_types_list) {
        error_at_ident("Undefined type",
                       &unbound_types_list->ident);
    }
    if (unbound_funcs_list) {
        error_at_ident("Undefined function",
                       &unbound_funcs_list->ident);
    }

    /* TODO compute API-hashes etc. */
    mod_declared_versions = NULL;
    mod_declared_versions_list = NULL;

    current_module_id++;
}

static void parse_core_interface(void)
{
    /* TODO rename core_wip.slul to core.slul once it's API-stable */
    open_file(ifacedir, ifacedir_len, "core_wip.slul");
    /* TODO forbid Uppercase initial letter in interfaces?
            or at least don't create an implicit class. */
    parse_file(f, "core_wip.slul");
    builtins_bind();
    close_file();
    completed_one_module();
}

static void parse_dependency_interfaces(void)
{
    char filename[50+5+1]; /* "<modulename>.slul\0" */
    const struct DependencyInfo *dep;

    for (dep = dependencies_list; dep; dep = dep->next) {
        size_t namelen = dep->node.length;
        assert(namelen <= 50);
        memcpy(filename, dep->node.name, namelen);
        memcpy(filename+namelen, ".slul", 5+1);

        open_file(ifacedir, ifacedir_len, filename);
        parse_file(f, filename);
        close_file();
        compute_api_hashes();
        completed_one_module();
    }
}

static void parse_exported_interface(void)
{
    assert(is_library);
    in_exported_interface = true;
    open_file(rootdir, rootdir_len, "interface.slul");
    parse_file(f, "interface.slul");
    close_file();
    compute_api_hashes();
    completed_one_module();
    in_exported_interface = false;
}

static void parse(void)
{
    const struct SourceFile *sourcefile;

    ast_init();

    /* TODO Decide on a file name here. In particular,
            check that "sources.index" is not used for anything else.
                - Go uses extensions: go.mod, go.sum, etc.
                  How about:
                    slul.sources
                    slul.list
                    slul.index
                  And then, for the other types of files:
                    slul.deps
                    slul.exports
                    slul.known (known hashes / pubkeys)
                  (Also, how about renaming slul?)
            */
    open_file(rootdir, rootdir_len, "sources.index");
    parse_source_index(f);
    close_file();
    free_last_filename();

    open_file(rootdir, rootdir_len, "deps.index");
    parse_dependencies_list(f);
    close_file();
    free_last_filename();

    parse_core_interface();
    parse_dependency_interfaces();
    if (is_library) {
        parse_exported_interface();
    }
    interfaces_done = true;

    for (sourcefile = sources; sourcefile; sourcefile = sourcefile->next) {
        const char *filename = sourcefile->filename;
        open_file(rootdir, rootdir_len, filename);
        parse_file(f, path_basename(filename));
        close_file();
    }
    completed_one_module();
}

static void set_rootdir(const char *rootdir_arg)
{
    rootdir_len = strlen(rootdir_arg);
    if (rootdir_len+2 >= PATH_LIMIT) FAIL("source path too long");
    rootdir = rootdir_arg;
}

int main(int argc, char **argv)
{
    enum {
        IFACEDIR_ARG = 1,
        SOURCE_DIR_ARG = 2,
        C_OUTPUT_ARG = 3
    };

    if (argc != 4) {
        fprintf(stderr, "usage: "
            "stage1 <interface-dir> <source-dir> <output.c>\n");
        return EXIT_FAILURE;
    }

    /* TODO allow the interface files to be read directly from the modules? */
    ifacedir = argv[IFACEDIR_ARG];
    ifacedir_len = strlen(ifacedir);

    set_rootdir(argv[SOURCE_DIR_ARG]);
    parse();

    output_c_filename = argv[C_OUTPUT_ARG];
    emit_c_code(output_c_filename);
    return EXIT_SUCCESS;
}