-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
276 lines (213 loc) · 7.23 KB
/
main.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
#include "compiler.h"
#include "lex.h"
#include "link.h"
#include "log.h"
#include "macos/mach-o.h"
#include "parse.h"
#include "program.h"
#include "util.h"
#include "vector.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
enum parse_args_result {
PARSE_ARGS_RESULT_SUCCESS = 0,
PARSE_ARGS_RESULT_NO_SOURCES = 1,
PARSE_ARGS_RESULT_ERROR = 2,
};
static enum parse_args_result parse_args(int argc, char **argv,
struct compile_args *args,
const char ***sources, size_t *num_sources);
static char *readfile(const char *path) {
FILE *f = fopen(path, "r");
if (!f) {
return NULL;
}
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
invariant_assert(fsize != -1L, "ftell failed");
rewind(f);
char *content = nonnull_malloc((unsigned long)fsize + 1);
fread(content, (unsigned long)fsize, 1, f);
fclose(f);
content[fsize] = '\0';
return content;
}
static bool target_needs_linking(const struct compile_args *args) {
return args->target_arch != COMPILE_TARGET_ARCH_EEP;
}
int main(int argc, char **argv) {
enable_log();
info("parsing command line args");
struct compile_args args;
const char **sources;
size_t num_sources;
if (parse_args(argc, argv, &args, &sources, &num_sources) !=
PARSE_ARGS_RESULT_SUCCESS) {
err("failed to parse arguments");
return -1;
}
if (args.target_arch == COMPILE_TARGET_ARCH_NATIVE) {
info("Compiling for native platform - assuming macOS ARM64...\n");
args.target_arch = COMPILE_TARGET_ARCH_MACOS_ARM64;
}
char **objects = nonnull_malloc(sizeof(*objects) * num_sources);
info("beginning compilation stage...");
for (size_t i = 0; i < num_sources; i++) {
info("compiling source file \"%s\"", sources[i]);
const char *source = readfile(sources[i]);
if (!source) {
err("source file \"%s\" could not be read!", sources[i]);
return COMPILE_RESULT_BAD_FILE;
}
char *object_file = nonnull_malloc(strlen(sources[i]) + sizeof(".obj"));
strcpy(object_file, sources[i]);
strcat(object_file, ".obj");
info("compiling source file '%s' into object file '%s'", sources[i],
object_file);
objects[i] = object_file;
struct program program = {.text = source};
disable_log();
struct compiler *compiler;
if (create_compiler(&program, object_file, &args, &compiler) !=
COMPILER_CREATE_RESULT_SUCCESS) {
err("failed to create compiler");
return -1;
}
if (compile(compiler) != COMPILE_RESULT_SUCCESS) {
err("compilation failed!");
return -1;
}
enable_log();
}
struct link_args link_args = {.objects = (const char * const *)objects,
.num_objects = num_sources,
.output = args.output ? args.output : "a.out"};
if (target_needs_linking(&args)) {
if (link_objects(&link_args) != LINK_RESULT_SUCCESS) {
err("link failed");
exit(-1);
}
}
info("Compilation succeeded!");
for (size_t i = 0; i < num_sources; i++) {
free(objects[i]);
}
free(objects);
}
static const char *try_get_arg(const char *arg, const char *prefix) {
if (strncmp(arg, prefix, strlen(prefix)) == 0) {
return &arg[strlen(prefix)];
}
return NULL;
}
PRINTF_ARGS(0) static void parse_arg_error(const char *fmt, ...) {
va_list v;
va_start(v, fmt);
vfprintf(stderr, fmt, v);
fprintf(stderr, "\n");
va_end(v);
}
static bool parse_log_flag(const char *flag, enum compile_log_flags *flags) {
#define LOG_FLAG(name, str) \
if (strcmp(flag, str) == 0) { \
*flags |= name; \
return true; \
}
LOG_FLAG(COMPILE_LOG_FLAGS_PREPROC, "preproc");
LOG_FLAG(COMPILE_LOG_FLAGS_PARSE, "parse");
LOG_FLAG(COMPILE_LOG_FLAGS_TYPECHK, "typechk");
LOG_FLAG(COMPILE_LOG_FLAGS_IR, "ir");
LOG_FLAG(COMPILE_LOG_FLAGS_REGALLOC, "regalloc");
LOG_FLAG(COMPILE_LOG_FLAGS_PRE_EMIT, "pre_emit");
LOG_FLAG(COMPILE_LOG_FLAGS_EMIT, "emit");
LOG_FLAG(COMPILE_LOG_FLAGS_ASM, "asm");
LOG_FLAG(COMPILE_LOG_FLAGS_ALL, "all");
parse_arg_error("Unrecognised log flag '-L%s'", flag);
return false;
}
static bool parse_target_flag(const char *flag, enum compile_target_arch *arch) {
if (strcmp(flag, "x64") == 0) {
*arch = COMPILE_TARGET_ARCH_MACOS_X86_64;
return true;
} else if (strcmp(flag, "aarch64") == 0) {
*arch = COMPILE_TARGET_ARCH_MACOS_ARM64;
return true;
} else if (strcmp(flag, "eep") == 0) {
*arch = COMPILE_TARGET_ARCH_EEP;
return true;
}
return false;
}
static bool parse_output(const char *str, char **output) {
size_t output_len = strlen(str);
if (output_len) {
char *name = nonnull_malloc(output_len + 1);
memcpy(name, str, output_len);
name[output_len] = '\0';
*output = name;
return true;
}
return false;
}
static enum parse_args_result parse_args(int argc, char **argv,
struct compile_args *args,
const char ***sources, size_t *num_sources) {
memset(args, 0, sizeof(*args));
// default to native arch
args->target_arch = COMPILE_TARGET_ARCH_NATIVE;
// should always be true as argv[0] is program namr
invariant_assert(argc > 0, "argc must be >0");
struct vector *include_path_vec = vector_create(sizeof(*argv));
struct vector *sources_vec = vector_create(sizeof(*argv));
for (int i = 1; i < argc; i++) {
const char *arg = argv[i];
// allows both '-Tfoo' and '-T foo' by using the next argument if the current
// one is empty (after prefix) if it uses the next argument it skips it
#define GET_ARGUMENT(v) v = (!(v) || (v)[0] || i + 1 == argc ? v : argv[++i])
const char *log = try_get_arg(arg, "-L");
GET_ARGUMENT(log);
if (log) {
if (!parse_log_flag(log, &args->log_flags)) {
return PARSE_ARGS_RESULT_ERROR;
}
continue;
}
const char *include_path = try_get_arg(arg, "-I");
GET_ARGUMENT(include_path);
if (include_path) {
vector_push_back(include_path_vec, include_path);
continue;
}
const char *target = try_get_arg(arg, "-T");
GET_ARGUMENT(target);
if (target) {
if (!parse_target_flag(target, &args->target_arch)) {
return PARSE_ARGS_RESULT_ERROR;
}
continue;
}
const char *output = try_get_arg(arg, "-o");
GET_ARGUMENT(output);
if (output) {
if (!parse_output(output, &args->output)) {
return PARSE_ARGS_RESULT_ERROR;
}
continue;
}
#undef GET_ARGUMENT
// wasn't recognised as a flag, assume source arg
vector_push_back(sources_vec, &arg);
}
args->num_include_paths = vector_length(include_path_vec);
args->include_paths = nonnull_malloc(vector_byte_size(include_path_vec));
vector_copy_to(include_path_vec, args->include_paths);
*num_sources = vector_length(sources_vec);
if (*num_sources == 0) {
parse_arg_error("No sources provided!");
return PARSE_ARGS_RESULT_NO_SOURCES;
}
*sources = nonnull_malloc(vector_byte_size(sources_vec));
vector_copy_to(sources_vec, *sources);
return PARSE_ARGS_RESULT_SUCCESS;
}