-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.c
317 lines (247 loc) · 7.46 KB
/
compiler.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
#include "compiler.h"
#include "aarch64.h"
#include "alloc.h"
#include "codegen.h"
#include "eep.h"
#include "emit.h"
#include "io.h"
#include "ir/build.h"
#include "ir/eliminate_phi.h"
#include "ir/ir.h"
#include "ir/prettyprint.h"
#include "ir/validate.h"
#include "lex.h"
#include "log.h"
#include "lower.h"
#include "lsra.h"
#include "macos/mach-o.h"
#include "parse.h"
#include "preproc.h"
#include "rv32i.h"
#include "target.h"
#include "util.h"
#include "vector.h"
#include <stdio.h>
struct compiler {
struct arena_allocator *arena;
struct compile_args args;
struct preproc *preproc;
struct parser *parser;
struct typechk *typechk;
char *output;
};
static const struct target *get_target(const struct compile_args *args) {
switch (args->target_arch) {
case COMPILE_TARGET_ARCH_NATIVE:
BUG("hit COMPILE_TARGET_ARCH_NATIVE in compiler! should have been chosen "
"earlier");
case COMPILE_TARGET_ARCH_MACOS_X86_64:
TODO("macOS x64 target not yet implemented");
// FIXME: linux is actually subtly different in register usage and calling conv
case COMPILE_TARGET_ARCH_LINUX_ARM64:
return &AARCH64_LINUX_TARGET;
case COMPILE_TARGET_ARCH_MACOS_ARM64:
return &AARCH64_MACOS_TARGET;
case COMPILE_TARGET_ARCH_RV32I:
return &RV32I_TARGET;
case COMPILE_TARGET_ARCH_EEP:
BUG("redo eep");
// return &EEP_TARGET;
}
BUG("unexpected target in `get_target`");
}
enum compiler_create_result
create_compiler(struct program *program, const char *output, const char *path,
const struct compile_args *args, struct compiler **compiler) {
*compiler = nonnull_malloc(sizeof(**compiler));
(*compiler)->args = *args;
if (preproc_create(program, path, args->num_include_paths,
args->include_paths, args->fixed_timestamp,
&(*compiler)->preproc) != PREPROC_CREATE_RESULT_SUCCESS) {
err("failed to create preproc");
return COMPILER_CREATE_RESULT_FAILURE;
}
if (parser_create(program, (*compiler)->preproc, &(*compiler)->parser) !=
PARSER_CREATE_RESULT_SUCCESS) {
err("failed to create parser");
return COMPILER_CREATE_RESULT_FAILURE;
}
const struct target *target = get_target(args);
if (typechk_create(target, args, (*compiler)->parser,
&(*compiler)->typechk) != TYPECHK_CREATE_RESULT_SUCCESS) {
err("failed to create typechk");
return COMPILER_CREATE_RESULT_FAILURE;
}
arena_allocator_create(&(*compiler)->arena);
size_t sz = strlen(output) + 1;
(*compiler)->output = arena_alloc((*compiler)->arena, sz);
strcpy((*compiler)->output, output);
(*compiler)->output[sz] = '\0';
return COMPILER_CREATE_RESULT_SUCCESS;
}
static void debug_print_stage(struct ir_unit *ir,
UNUSED_ARG(const char *name)) {
debug_print_ir(stderr, ir, NULL, NULL);
}
#define COMPILER_STAGE(stage) \
{ \
disable_log(); \
\
if (COMPILER_LOG_ENABLED(compiler, COMPILE_LOG_FLAGS_##stage)) { \
enable_log(); \
BEGIN_STAGE(#stage); \
} \
}
enum compile_result compile(struct compiler *compiler) {
struct parse_result parse_result;
if (compiler->args.preproc_only) {
// preproc is kept local as it is seperate to other stages
BEGIN_STAGE("PREPROC");
if (compiler->args.log_flags & COMPILE_LOG_FLAGS_PREPROC) {
enable_log();
}
FILE *file;
// FIXME: hacky (and in main.c)
if (strcmp(compiler->output, "stdout") == 0) {
file = stdout;
} else {
file = fopen(compiler->output, "w");
}
if (!file) {
return COMPILE_RESULT_BAD_FILE;
}
preproc_process(compiler->preproc, file);
disable_log();
return COMPILE_RESULT_SUCCESS;
}
{
COMPILER_STAGE(PARSE);
parse_result = parse(compiler->parser);
BEGIN_STAGE("AST");
if (log_enabled()) {
debug_print_ast(compiler->parser, &parse_result.translation_unit);
}
}
struct typechk_result typechk_result;
{
COMPILER_STAGE(TYPECHK);
typechk_result =
td_typechk(compiler->typechk, &parse_result.translation_unit);
if (log_enabled()) {
debug_print_td(compiler->typechk, &typechk_result.translation_unit);
}
}
const struct target *target = get_target(&compiler->args);
struct ir_unit *ir;
{
COMPILER_STAGE(IR);
ir =
build_ir_for_translationunit(target, compiler->typechk, compiler->arena,
&typechk_result.translation_unit);
if (log_enabled()) {
debug_print_stage(ir, "ir");
}
ir_validate(ir);
}
{
COMPILER_STAGE(LOWER);
lower(ir, target);
if (log_enabled()) {
debug_print_stage(ir, "lower");
}
ir_validate(ir);
}
{
COMPILER_STAGE(REGALLOC);
struct ir_glb *glb = ir->first_global;
while (glb) {
if (glb->def_ty == IR_GLB_DEF_TY_UNDEFINED) {
glb = glb->succ;
continue;
}
switch (glb->ty) {
case IR_GLB_TY_DATA:
break;
case IR_GLB_TY_FUNC:
lsra_register_alloc(glb->func, target->reg_info);
break;
}
glb = glb->succ;
}
if (log_enabled()) {
debug_print_stage(ir, "regalloc");
}
ir_validate(ir);
BEGIN_STAGE("ELIM PHI");
glb = ir->first_global;
while (glb) {
if (glb->def_ty == IR_GLB_DEF_TY_UNDEFINED) {
glb = glb->succ;
continue;
}
switch (glb->ty) {
case IR_GLB_TY_DATA:
break;
case IR_GLB_TY_FUNC:
eliminate_phi(glb->func);
rebuild_ids(glb->func);
break;
}
glb = glb->succ;
}
if (log_enabled()) {
debug_print_stage(ir, "elim_phi");
}
ir_validate(ir);
}
{
struct ir_glb *glb = ir->first_global;
while (glb) {
if (glb->def_ty == IR_GLB_DEF_TY_UNDEFINED) {
glb = glb->succ;
continue;
}
switch (glb->ty) {
case IR_GLB_TY_DATA:
break;
case IR_GLB_TY_FUNC:
rebuild_ids(glb->func);
break;
}
glb = glb->succ;
}
}
struct emitted_unit unit;
{
COMPILER_STAGE(EMIT);
struct codegen_unit *codegen = target->codegen(ir);
if (log_enabled() && target->debug_print_codegen) {
debug_print_stage(ir, "emit");
target->debug_print_codegen(stderr, codegen);
}
unit = target->emit_function(codegen);
}
struct build_object_args args = {.compile_args = &compiler->args,
.output = compiler->output,
.entries = unit.entries,
.num_entries = unit.num_entries};
{
COMPILER_STAGE(ASM);
target->build_object(&args);
if (log_enabled()) {
if (!target->debug_disasm) {
warn("DISASM not supported for current target");
} else {
BEGIN_STAGE("DISASSEMBLY");
target->debug_disasm(args.output);
}
}
}
return COMPILE_RESULT_SUCCESS;
}
void free_compiler(struct compiler **compiler) {
arena_allocator_free(&(*compiler)->arena);
parser_free(&(*compiler)->parser);
free(*compiler);
*compiler = NULL;
}