-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsunder.c
459 lines (412 loc) · 16.2 KB
/
sunder.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// SPDX-License-Identifier: Apache-2.0
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "sunder.h"
static char const* arch_list[] = {
[ARCH_AMD64] = "amd64",
[ARCH_ARM64] = "arm64",
[ARCH_WASM32] = "wasm32",
};
enum arch
cstr_to_arch(char const* cstr)
{
for (size_t i = 0; i < ARRAY_COUNT(arch_list); ++i) {
if (0 == strcmp(cstr, arch_list[i])) {
return (enum arch)i;
}
}
fatal(NO_LOCATION, "unknown arch `%s`", cstr);
return 0;
}
struct module*
module_new(char const* name, char const* path)
{
assert(name != NULL);
assert(path != NULL);
struct module* const self = xalloc(NULL, sizeof(*self));
memset(self, 0x00, sizeof(*self));
self->name = intern_cstr(name);
self->path = intern_cstr(path);
char* const source = read_source(self->path);
freeze(source - 1);
self->source = source;
self->source_count = strlen(source);
self->symbols = symbol_table_new(NULL);
symbol_table_insert(
self->symbols,
context()->interned.any,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.any),
false);
symbol_table_insert(
self->symbols,
context()->interned.void_,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.void_),
false);
symbol_table_insert(
self->symbols,
context()->interned.bool_,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.bool_),
false);
symbol_table_insert(
self->symbols,
context()->interned.byte,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.byte),
false);
symbol_table_insert(
self->symbols,
context()->interned.u8,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.u8),
false);
symbol_table_insert(
self->symbols,
context()->interned.s8,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.s8),
false);
symbol_table_insert(
self->symbols,
context()->interned.u16,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.u16),
false);
symbol_table_insert(
self->symbols,
context()->interned.s16,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.s16),
false);
symbol_table_insert(
self->symbols,
context()->interned.u32,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.u32),
false);
symbol_table_insert(
self->symbols,
context()->interned.s32,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.s32),
false);
symbol_table_insert(
self->symbols,
context()->interned.u64,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.u64),
false);
symbol_table_insert(
self->symbols,
context()->interned.s64,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.s64),
false);
symbol_table_insert(
self->symbols,
context()->interned.usize,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.usize),
false);
symbol_table_insert(
self->symbols,
context()->interned.ssize,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.ssize),
false);
symbol_table_insert(
self->symbols,
context()->interned.integer,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.integer),
false);
symbol_table_insert(
self->symbols,
context()->interned.f32,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.f32),
false);
symbol_table_insert(
self->symbols,
context()->interned.f64,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.f64),
false);
symbol_table_insert(
self->symbols,
context()->interned.real,
symbol_table_lookup(
context()->global_symbol_table, context()->interned.real),
false);
self->exports = symbol_table_new(NULL);
return self;
}
void
module_del(struct module* self)
{
assert(self != NULL);
symbol_table_freeze(self->symbols);
symbol_table_freeze(self->exports);
sbuf_fini(self->ordered);
memset(self, 0x00, sizeof(*self));
xalloc(self, XALLOC_FREE);
}
static struct context s_context;
void
context_init(void)
{
intern_init();
s_context.interned.empty = intern_cstr("");
s_context.interned.builtin = intern_cstr("builtin");
s_context.interned.return_ = intern_cstr("return");
s_context.interned.main = intern_cstr("main");
s_context.interned.any = intern_cstr("any");
s_context.interned.void_ = intern_cstr("void");
s_context.interned.bool_ = intern_cstr("bool");
s_context.interned.byte = intern_cstr("byte");
s_context.interned.u8 = intern_cstr("u8");
s_context.interned.s8 = intern_cstr("s8");
s_context.interned.u16 = intern_cstr("u16");
s_context.interned.s16 = intern_cstr("s16");
s_context.interned.u32 = intern_cstr("u32");
s_context.interned.s32 = intern_cstr("s32");
s_context.interned.u64 = intern_cstr("u64");
s_context.interned.s64 = intern_cstr("s64");
s_context.interned.usize = intern_cstr("usize");
s_context.interned.ssize = intern_cstr("ssize");
s_context.interned.integer = intern_cstr("integer");
s_context.interned.y = intern_cstr("y");
s_context.interned.u = intern_cstr("u");
s_context.interned.s = intern_cstr("s");
s_context.interned.f32 = intern_cstr("f32");
s_context.interned.f64 = intern_cstr("f64");
s_context.interned.real = intern_cstr("real");
s_context.env.SUNDER_HOME = getenv("SUNDER_HOME");
s_context.env.SUNDER_HOME = s_context.env.SUNDER_HOME != NULL
? intern_cstr(s_context.env.SUNDER_HOME)
: s_context.interned.empty;
s_context.env.SUNDER_ARCH = getenv("SUNDER_ARCH");
s_context.env.SUNDER_ARCH = s_context.env.SUNDER_ARCH != NULL
? intern_cstr(s_context.env.SUNDER_ARCH)
: intern_cstr(STRINGIFY(SUNDER_DEFAULT_ARCH));
for (char const* p = s_context.env.SUNDER_ARCH; *p; ++p) {
if (!safe_isalnum(*p)) {
fatal(
NO_LOCATION,
"SUNDER_ARCH `%s` contains disallowed character `%c`",
s_context.env.SUNDER_ARCH,
*p);
}
}
s_context.env.SUNDER_HOST = getenv("SUNDER_HOST");
s_context.env.SUNDER_HOST = s_context.env.SUNDER_HOST != NULL
? intern_cstr(s_context.env.SUNDER_HOST)
: intern_cstr(STRINGIFY(SUNDER_DEFAULT_HOST));
for (char const* p = s_context.env.SUNDER_HOST; *p; ++p) {
if (!safe_isalnum(*p)) {
fatal(
NO_LOCATION,
"SUNDER_HOST `%s` contains disallowed character `%c`",
s_context.env.SUNDER_HOST,
*p);
}
}
s_context.env.SUNDER_SEARCH_PATH = getenv("SUNDER_SEARCH_PATH");
s_context.env.SUNDER_SEARCH_PATH = s_context.env.SUNDER_SEARCH_PATH != NULL
? intern_cstr(s_context.env.SUNDER_SEARCH_PATH)
: s_context.interned.empty;
s_context.env.SUNDER_CC = getenv("SUNDER_CC");
s_context.env.SUNDER_CC = s_context.env.SUNDER_CC != NULL
? intern_cstr(s_context.env.SUNDER_CC)
: intern_cstr(STRINGIFY(SUNDER_DEFAULT_CC));
s_context.env.SUNDER_CFLAGS = getenv("SUNDER_CFLAGS");
s_context.env.SUNDER_CFLAGS = s_context.env.SUNDER_CFLAGS != NULL
? intern_cstr(s_context.env.SUNDER_CFLAGS)
: s_context.interned.empty;
s_context.arch = cstr_to_arch(s_context.env.SUNDER_ARCH);
#define INIT_BIGINT_CONSTANT(ident, str_literal) \
struct bigint* const ident = bigint_new_cstr(str_literal); \
bigint_freeze(ident); \
s_context.ident = ident;
INIT_BIGINT_CONSTANT(u8_min, "+0x00")
INIT_BIGINT_CONSTANT(u8_max, "+0xFF")
INIT_BIGINT_CONSTANT(s8_min, "-128")
INIT_BIGINT_CONSTANT(s8_max, "+127")
INIT_BIGINT_CONSTANT(u16_min, "+0x0000")
INIT_BIGINT_CONSTANT(u16_max, "+0xFFFF")
INIT_BIGINT_CONSTANT(s16_min, "-32768")
INIT_BIGINT_CONSTANT(s16_max, "+32767")
INIT_BIGINT_CONSTANT(u32_min, "+0x00000000")
INIT_BIGINT_CONSTANT(u32_max, "+0xFFFFFFFF")
INIT_BIGINT_CONSTANT(s32_min, "-2147483648")
INIT_BIGINT_CONSTANT(s32_max, "+2147483647")
INIT_BIGINT_CONSTANT(u64_min, "+0x0000000000000000")
INIT_BIGINT_CONSTANT(u64_max, "+0xFFFFFFFFFFFFFFFF")
INIT_BIGINT_CONSTANT(s64_min, "-9223372036854775808")
INIT_BIGINT_CONSTANT(s64_max, "+9223372036854775807")
s_context.usize_min = s_context.u64_min;
s_context.usize_max = s_context.u64_max;
s_context.ssize_min = s_context.s64_min;
s_context.ssize_max = s_context.s64_max;
INIT_BIGINT_CONSTANT(f32_integer_min, STRINGIFY(IEEE754_FLT_INTEGER_MIN))
INIT_BIGINT_CONSTANT(f32_integer_max, STRINGIFY(IEEE754_FLT_INTEGER_MAX))
INIT_BIGINT_CONSTANT(f64_integer_min, STRINGIFY(IEEE754_DBL_INTEGER_MIN))
INIT_BIGINT_CONSTANT(f64_integer_max, STRINGIFY(IEEE754_DBL_INTEGER_MAX))
#undef INIT_BIGINT_CONSTANT
s_context.types = NULL;
s_context.static_symbols = NULL;
s_context.global_symbol_table = symbol_table_new(NULL);
s_context.modules = NULL;
s_context.builtin.location = (struct source_location){
s_context.interned.builtin,
NO_LINE,
NO_PSRC,
};
#define INIT_BUILTIN_TYPE(builtin_lvalue, /* struct type* */ t) \
{ \
struct type* const type = t; \
freeze(type); \
sbuf_push(s_context.types, type); \
struct symbol* const symbol = \
symbol_new_type(s_context.builtin.location, type); \
freeze(symbol); \
symbol_table_insert( \
s_context.global_symbol_table, symbol->name, symbol, false); \
builtin_lvalue = type; \
}
INIT_BUILTIN_TYPE(s_context.builtin.any, type_new_any());
INIT_BUILTIN_TYPE(s_context.builtin.void_, type_new_void());
INIT_BUILTIN_TYPE(s_context.builtin.bool_, type_new_bool());
INIT_BUILTIN_TYPE(s_context.builtin.byte, type_new_byte());
INIT_BUILTIN_TYPE(s_context.builtin.u8, type_new_u8());
INIT_BUILTIN_TYPE(s_context.builtin.s8, type_new_s8());
INIT_BUILTIN_TYPE(s_context.builtin.u16, type_new_u16());
INIT_BUILTIN_TYPE(s_context.builtin.s16, type_new_s16());
INIT_BUILTIN_TYPE(s_context.builtin.u32, type_new_u32());
INIT_BUILTIN_TYPE(s_context.builtin.s32, type_new_s32());
INIT_BUILTIN_TYPE(s_context.builtin.u64, type_new_u64());
INIT_BUILTIN_TYPE(s_context.builtin.s64, type_new_s64());
INIT_BUILTIN_TYPE(s_context.builtin.usize, type_new_usize());
INIT_BUILTIN_TYPE(s_context.builtin.ssize, type_new_ssize());
INIT_BUILTIN_TYPE(s_context.builtin.integer, type_new_integer());
INIT_BUILTIN_TYPE(s_context.builtin.f32, type_new_f32());
INIT_BUILTIN_TYPE(s_context.builtin.f64, type_new_f64());
INIT_BUILTIN_TYPE(s_context.builtin.real, type_new_real());
#undef INIT_BUILTIN_TYPE
// Instantiate `*byte` and `[]byte` types. These types are not builtins in
// the traditional sense, but they are types relating to string literals,
// which are a primitive part of the Sunder language. By creating these
// types here with the `type_unique_*` functions, we guarantee that these
// types will appear just after the other builtins within the types list.
struct type const* const byte = s_context.builtin.byte;
s_context.builtin.pointer_to_byte = type_unique_pointer(byte);
s_context.builtin.slice_of_byte = type_unique_slice(byte);
}
/* util.c */
extern sbuf(char*) interned;
void
context_fini(void)
{
// Finalizing modules, interned strings, chilling & frozen objects, and
// other dynamically allocated resources at program exit is unnecessary
// when the operating system is already going to unmap pages associated
// with the process during cleanup. Sunder tooling built in release mode
// skips the finalization of dynamically allocated resources in order to
// avoid superfluously spinning in a loop and calling `free` on objects
// that are about to be unmapped anyway. Sunder tooling built in debug mode
// *will* finalize dynamically allocated resources (1) as a way to ensure
// that the model of object lifecycles is correctly implemented *if*
// finalization were to be explicitly re-enabled, and (2) in order to allow
// tooling such as LeakSanitizer to find legitimate sources of memory leaks
// during application testing.
#ifndef NDEBUG
struct context* const self = &s_context;
for (size_t i = 0; i < sbuf_count(self->modules); ++i) {
module_del(self->modules[i]);
}
sbuf_fini(self->modules);
intern_fini();
sbuf_fini(self->types);
sbuf_fini(self->static_symbols);
symbol_table_freeze(self->global_symbol_table);
sbuf(struct symbol_table*) const chilling_symbol_tables =
self->chilling_symbol_tables;
for (size_t i = 0; i < sbuf_count(chilling_symbol_tables); ++i) {
symbol_table_freeze(chilling_symbol_tables[i]);
}
sbuf_fini(self->chilling_symbol_tables);
freeze_fini();
memset(self, 0x00, sizeof(*self));
#endif
}
struct context*
context(void)
{
return &s_context;
}
struct module const*
load_module(char const* name, char const* path)
{
assert(path != NULL);
assert(lookup_module(path) == NULL);
struct module* const module = module_new(name, path);
sbuf_push(s_context.modules, module);
parse(module);
order(module);
resolve(module);
module->loaded = true;
return module;
}
struct module const*
lookup_module(char const* path)
{
assert(path != NULL);
for (size_t i = 0; i < sbuf_count(context()->modules); ++i) {
if (context()->modules[i]->path == path) {
return context()->modules[i];
}
}
return NULL;
}
void
validate_main_is_defined_correctly(void)
{
struct symbol const* main_symbol = NULL;
for (size_t i = 0; i < sbuf_count(context()->static_symbols); ++i) {
struct symbol const* const symbol = context()->static_symbols[i];
if (symbol->name != context()->interned.main) {
continue;
}
struct address const* const address = symbol_get_address(symbol);
if (address == NULL) {
continue;
}
// XXX: Checking if the symbol name matches the static address name
// here is really a check to see if this main symbol is not part of a
// namespace. Namespaced main functions such as `foo::main` should be
// skipped, and checking the static address name is one way to work
// around the lack of namespace information on symbols.
assert(address->kind == ADDRESS_STATIC);
assert(address->data.static_.offset == 0);
if (symbol->name != address->data.static_.name) {
continue;
}
main_symbol = symbol;
break;
}
if (main_symbol == NULL || main_symbol->kind != SYMBOL_FUNCTION) {
fatal(NO_LOCATION, "main function is not defined");
}
struct type const* const expected_type =
type_unique_function(NULL, context()->builtin.void_);
if (main_symbol->data.function->type != expected_type) {
fatal(
main_symbol->location,
"main has invalid type `%s` (expected `%s`)",
main_symbol->data.function->type->name,
expected_type->name);
}
}