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
|
/*
* Main entry point of the bootstrap compiler.
*
* Copyright © 2025 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"
#if defined(_WIN32) || defined(_WIN64)
#define DIRSEP '\\'
#else
#define DIRSEP '/'
#endif
#define PATH_LIMIT 512
static size_t rootdir_len = 0;
static const char *rootdir;
static FILE *f = NULL;
static const char *output_filename = NULL;
const char *current_filename = NULL;
static char *last_alloced_filename = NULL;
unsigned current_line = 0;
NORETURN void error(const char *s)
{
fprintf(stderr, "%s:%u: %s\n", current_filename, current_line, s);
if (output_filename) {
remove(output_filename);
}
exit(EXIT_FAILURE);
}
void warning(const char *s)
{
fprintf(stderr, "%s:%u: warning: %s\n",
current_filename, current_line, s);
}
static void open_file(const char *filename)
{
size_t filename_len, total_len;
char *fullpath;
filename_len = strlen(filename);
total_len = filename_len + 1 + rootdir_len + 1;
if (total_len > PATH_LIMIT) {
FAIL("filename too long");
}
fullpath = malloc(total_len);
NO_NULL(fullpath);
/* Root dir */
memcpy(fullpath, rootdir, rootdir_len);
fullpath[rootdir_len] = DIRSEP;
/* Source file */
memcpy(fullpath+rootdir_len+1, filename, filename_len);
replacechar(fullpath+rootdir_len+1, '/', DIRSEP, filename_len);
fullpath[rootdir_len+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 parse(void)
{
unsigned 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();
free(last_alloced_filename);
builtins_init();
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_arg)
{
rootdir_len = strlen(rootdir_arg);
if (rootdir_len+2 >= PATH_LIMIT) FAIL("source path too long");
rootdir = rootdir_arg;
}
static void reset_sources_index(void)
{
num_sources = 0;
}
int main(int argc, char **argv)
{
int i;
if (argc < 3) {
fprintf(stderr, "usage: stage1 <_gen.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();
}
output_filename = argv[1];
emit_c_code(output_filename);
return EXIT_SUCCESS;
}
|