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
|
/*
* Main entry point of the bootstrap compiler.
*
* Copyright © 2025 Samuel Lidén Borell <samuel@kodafritt.se>
*
* SPDX-License-Identifier: EUPL-1.2+
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "compiler.h"
#if defined(_WIN32) || defined(_WIN64)
#define DIRSEP '\\'
#else
#define DIRSEP '/'
#endif
#define PATH_LIMIT 512
static size_t rootdir_len = 0;
static FILE *f = NULL;
static char fullpath[PATH_LIMIT];
char *current_filename = NULL;
int current_line = 0;
NORETURN void error(const char *s)
{
fprintf(stderr, "%s:%d: %s\n", current_filename, current_line, s);
exit(EXIT_FAILURE);
}
static void open_file(const char *filename)
{
size_t filename_len;
filename_len = strlen(filename);
if (filename_len + 1 + rootdir_len + 1 > PATH_LIMIT) {
FAIL("filename too long");
}
memcpy(fullpath+rootdir_len+1, filename, filename_len);
memreplace(fullpath+rootdir_len+1, '/', DIRSEP, filename_len);
fullpath[rootdir_len+1+filename_len] = '\0';
f = fopen(fullpath, "rb");
if (!f) {
perror(fullpath);
abort();
}
current_filename = fullpath;
current_line = 0;
}
static void close_file(void)
{
NO_NEG(fclose(f));
}
static void parse(void)
{
int i;
module_start();
/* TODO decide on a file name here.
in particular, check that "sources.index" is not used for anything else. */
open_file("sources.index");
parse_source_index(f);
close_file();
for (i = 0; i < num_sources; i++) {
open_file(sources[i]);
parse_file(f, path_basename(sources[i]));
close_file();
}
num_sources = 0;
}
static void set_rootdir(const char *rootdir)
{
rootdir_len = strlen(rootdir);
if (rootdir_len+2 >= PATH_LIMIT) FAIL("source path too long");
memcpy(fullpath, rootdir, rootdir_len);
fullpath[rootdir_len] = DIRSEP;
}
static void reset_sources_index(void)
{
num_sources = 0;
}
int main(int argc, char **argv)
{
int i;
if (argc < 3) {
fprintf(stderr, "usage: stage1 <out.c-file> <source-dir>...\n");
return EXIT_FAILURE;
}
/* Parse each module. This is a hack, that just throws everything
together in a single namespace (which is incorrect, but works
for compiling the SLUL compiler). */
modules = NULL;
for (i = 2; i < argc; i++) {
set_rootdir(argv[i]);
parse();
reset_sources_index();
}
emit_c_code(argv[1]);
return EXIT_SUCCESS;
}
|