-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.c
282 lines (209 loc) · 6.37 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
#include "compiler.h"
#include "aarch64.h"
#include "alloc.h"
#include "codegen.h"
#include "eep.h"
#include "emit.h"
#include "ir/build.h"
#include "ir/eliminate_phi.h"
#include "ir/ir.h"
#include "ir/prettyprint.h"
#include "lex.h"
#include "log.h"
#include "lsra.h"
#include "lower.h"
#include "macos/mach-o.h"
#include "parse.h"
#include "preproc.h"
#include "target.h"
#include "util.h"
#include "vector.h"
#include <stdio.h>
struct compiler {
struct arena_allocator *arena;
struct compile_args args;
struct parser *parser;
char *output;
};
enum compiler_create_result create_compiler(struct program *program,
const char *output,
const struct compile_args *args,
struct compiler **compiler) {
*compiler = nonnull_malloc(sizeof(**compiler));
(*compiler)->args = *args;
// preproc is kept local as it is seperate to other stages
struct preproc *preproc;
if (preproc_create(program, args->num_include_paths, args->include_paths, &preproc) != PREPROC_CREATE_RESULT_SUCCESS) {
err("failed to create preproc");
return COMPILER_CREATE_RESULT_FAILURE;
}
BEGIN_STAGE("PREPROC");
if (args->log_flags & COMPILE_LOG_FLAGS_PREPROC) {
enable_log();
}
struct preprocessed_program preprocessed_program = preproc_process(preproc);
slog("%s\n", preprocessed_program.text);
disable_log();
if (parser_create(&preprocessed_program, &(*compiler)->parser) !=
PARSER_CREATE_RESULT_SUCCESS) {
err("failed to create parser");
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;
}
void debug_print_stage(struct ir_unit *ir, const char *name) {
UNUSED_ARG(name);
debug_print_ir(stderr, ir, NULL, NULL);
}
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");
break;
case COMPILE_TARGET_ARCH_MACOS_X86_64:
todo("macOS x64 target not yet implemented");
case COMPILE_TARGET_ARCH_MACOS_ARM64:
return &AARCH64_TARGET;
case COMPILE_TARGET_ARCH_EEP:
bug("redo eep");
// return &EEP_TARGET;
}
bug("unexpected target in `get_target`");
}
enum compile_result compile(struct compiler *compiler) {
if (COMPILER_LOG_ENABLED(compiler, COMPILE_LOG_FLAGS_PARSE)) {
enable_log();
}
BEGIN_STAGE("LEX + PARSE");
struct parse_result result = parse(compiler->parser);
BEGIN_STAGE("AST");
if (compiler->args.log_flags & COMPILE_LOG_FLAGS_PARSE) {
debug_print_ast(compiler->parser, &result.translation_unit);
}
disable_log();
const struct target *target = get_target(&compiler->args);
if (COMPILER_LOG_ENABLED(compiler, COMPILE_LOG_FLAGS_IR)) {
enable_log();
}
BEGIN_STAGE("IR BUILD");
struct ir_unit *ir = build_ir_for_translationunit(
compiler->parser, compiler->arena, &result.translation_unit);
{
BEGIN_STAGE("IR");
if (compiler->args.log_flags & COMPILE_LOG_FLAGS_IR) {
debug_print_stage(ir, "ir");
}
BEGIN_STAGE("LOWERING");
lower(ir, target);
BEGIN_STAGE("POST PRE REG LOWER IR");
if (compiler->args.log_flags & COMPILE_LOG_FLAGS_IR) {
debug_print_stage(ir, "lower");
}
disable_log();
}
{
if (COMPILER_LOG_ENABLED(compiler, COMPILE_LOG_FLAGS_REGALLOC)) {
enable_log();
}
BEGIN_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 (compiler->args.log_flags & COMPILE_LOG_FLAGS_REGALLOC) {
debug_print_stage(ir, "regalloc");
}
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);
break;
}
glb = glb->succ;
}
if (compiler->args.log_flags & COMPILE_LOG_FLAGS_REGALLOC) {
debug_print_stage(ir, "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);
break;
}
glb = glb->succ;
}
if (compiler->args.log_flags & COMPILE_LOG_FLAGS_REGALLOC) {
debug_print_stage(ir, "elim_phi");
}
}
if (COMPILER_LOG_ENABLED(compiler, COMPILE_LOG_FLAGS_EMIT)) {
enable_log();
}
BEGIN_STAGE("PRE-EMIT");
if (compiler->args.log_flags & COMPILE_LOG_FLAGS_PRE_EMIT) {
debug_print_stage(ir, "pre_emit");
}
BEGIN_STAGE("CODEGEN");
struct codegen_unit *codegen = target->codegen(ir);
BEGIN_STAGE("EMITTING");
if (compiler->args.log_flags & COMPILE_LOG_FLAGS_EMIT) {
target->debug_print_codegen(stderr, codegen);
}
struct emitted_unit unit = target->emit_function(codegen);
disable_log();
struct build_object_args args = {.compile_args = &compiler->args,
.output = compiler->output,
.entries = unit.entries,
.num_entries = unit.num_entries};
BEGIN_STAGE("OBJECT FILE");
target->build_object(&args);
if (COMPILER_LOG_ENABLED(compiler, COMPILE_LOG_FLAGS_ASM)) {
enable_log();
if (!target->debug_disasm) {
warn("DISASM not supported for current target");
} else {
BEGIN_STAGE("DISASSEMBLY");
target->debug_disasm(args.output);
}
disable_log();
}
return COMPILE_RESULT_SUCCESS;
}
void free_compiler(struct compiler **compiler) {
arena_allocator_free(&(*compiler)->arena);
parser_free(&(*compiler)->parser);
free(*compiler);
*compiler = NULL;
}