-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
557 lines (470 loc) · 11.9 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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include "thrvcc.h"
#include <libgen.h>
#include <string.h>
static char *RVPath = "";
// -E
static bool OptE;
// -S
static bool OptS;
// -c
static bool OptC;
// -cc1
static bool OptCC1;
// -###
static bool OptHashHashHash;
// -o
static char *OptO;
struct StringArray IncludePaths;
// input file name
char *BaseFile;
// output file name
static char *OutputFile;
// Input file area
static struct StringArray InputPaths;
// temporary files
static struct StringArray TmpFiles;
// output thrvcc usage
static void usage(int status)
{
fprintf(stderr, "thrvcc [ -o <path> ] <file>\n");
exit(status);
}
// determines whether an option that takes one parameter has a single parameter
static bool take_arg(char *arg)
{
char *A[] = { "-o", "-I" };
for (int i = 0; i < sizeof(A) / sizeof(*A); i++)
if (!strcmp(arg, A[i]))
return true;
return false;
}
static void add_default_include_paths(char *argv)
{
str_array_push(&IncludePaths,
format("%s/include", dirname(strdup(argv))));
str_array_push(&IncludePaths, "/usr/local/include");
str_array_push(&IncludePaths, "/usr/include/riscv64-linux-gnu");
str_array_push(&IncludePaths, "/usr/include");
}
// parse the args passed in
static void parse_args(int argc, char **argv)
{
// make sure that option that require one parameter, exist a parameter
for (int i = 1; i < argc; i++)
if (take_arg(argv[i]))
if (!argv[++i])
usage(1);
// Iterate over all parameters passed into the program
for (int i = 1; i < argc; i++) {
// -###
if (!strcmp(argv[i], "-###")) {
OptHashHashHash = true;
continue;
}
// -cc1
if (!strcmp(argv[i], "-cc1")) {
OptCC1 = true;
continue;
}
// if --help, output usage()
if (!strcmp(argv[i], "--help"))
usage(0);
// parse "-o XXX" args
if (!strcmp(argv[i], "-o")) {
// target file path
OptO = argv[++i];
continue;
}
// parse "-oXXX" args
if (!strncmp(argv[i], "-o", 2)) {
// target file path
OptO = argv[i] + 2;
continue;
}
// parse -S
if (!strcmp(argv[i], "-S")) {
OptS = true;
continue;
}
// parse -c
if (!strcmp(argv[i], "-c")) {
OptC = true;
continue;
}
// parse -E
if (!strcmp(argv[i], "-E")) {
OptE = true;
continue;
}
// parse -I
if (!strncmp(argv[i], "-I", 2)) {
str_array_push(&IncludePaths, argv[i] + 2);
continue;
}
// parse -cc1-input
if (!strcmp(argv[i], "-cc1-input")) {
BaseFile = argv[++i];
continue;
}
// parse -cc1-output
if (!strcmp(argv[i], "-cc1-output")) {
OutputFile = argv[++i];
continue;
}
// parse "-" args
if (argv[i][0] == '-' && argv[i][1] != '\0')
error_out("unknown argument: %s", argv[i]);
// others, input file path
str_array_push(&InputPaths, argv[i]);
}
// if InputPath not exist, error
if (InputPaths.len == 0)
error_out("no input files");
}
// open the file need to write
static FILE *open_file(char *path)
{
if (!path || strcmp(path, "-") == 0)
return stdout;
// open file in w mode
FILE *out = fopen(path, "w");
if (!out)
error_out("cannot open output file: %s: %s", path,
strerror(errno));
return out;
}
// determine if the string P ends with the string Q
static bool ends_with(char *P, char *Q)
{
int len1 = strlen(P);
int len2 = strlen(Q);
return (len1 >= len2) && !strcmp(P + len1 - len2, Q);
}
// replace file extern name
static char *replace_extn(char *tmpl, char *extn)
{
char *file_name = basename(strdup(tmpl));
char *dot = strrchr(file_name, '.');
if (dot)
*dot = '\0';
return format("%s%s", file_name, extn);
}
// cleanup temporary files
static void cleanup(void)
{
for (int i = 0; i < TmpFiles.len; i++)
unlink(TmpFiles.data[i]);
}
// create temporary files
static char *create_tmpfile(void)
{
char *path = strdup("/tmp/thrvcc-XXXXXX");
int fd = mkstemp(path);
if (fd == -1)
error_out("mkstemp failed: %s", strerror(errno));
close(fd);
str_array_push(&TmpFiles, path);
return path;
}
// run subprocess
static void run_subprocess(char **argv)
{
// print all cli args of subprocess
if (OptHashHashHash) {
// program name
fprintf(stderr, "%s", argv[0]);
// program parameters
for (int i = 1; argv[i]; i++)
fprintf(stderr, " %s", argv[i]);
// execute
fprintf(stderr, "\n");
}
// fork-exec model
// creates a copy of the current process, here a child process is created
// return -1 error bit, return 0,success
if (fork() == 0) {
execvp(argv[0], argv);
fprintf(stderr, "exec failed: %s: %s\n", argv[0],
strerror(errno));
_exit(1);
}
// parent process, wait
int status;
while (wait(&status) > 0)
;
// handle subprocess return value
if (status != 0)
exit(1);
}
// call cc1
// cc1 is thrvcc itself
// call itself
static void run_cc1(int argc, char **argv, char *input, char *output)
{
// alloc more 10, casue new parameters
char **args = calloc(argc + 10, sizeof(char *));
// write parameters that pass in into program
memcpy(args, argv, argc * sizeof(char *));
// add '-cc1'
args[argc++] = "-cc1";
if (input) {
args[argc++] = "-cc1-input";
args[argc++] = input;
}
if (output) {
args[argc++] = "-cc1-output";
args[argc++] = output;
}
// call itself, pass in
run_subprocess(args);
}
// when -E, print all terminators
static void print_tokens(struct Token *token)
{
// default output stdout
FILE *out = open_file(OptO ? OptO : "-");
// reg line numbers
int line = 1;
for (; token->kind != TK_EOF; token = token->next) {
if (line > 1 && token->at_bol)
fprintf(out, "\n");
if (token->has_space && !token->at_bol)
fprintf(out, " ");
fprintf(out, "%.*s", token->len, token->location);
line++;
}
fprintf(out, "\n");
}
// compile C to asm
static void cc1(void)
{
// parse input file, gen token stream
struct Token *token = lexer_file(BaseFile);
// if generate terminator stream failed, error out file
if (!token)
error_out("%s: %s", BaseFile, strerror(errno));
// preprocess
token = preprocesser(token);
// if -E, print C code after preprocess
if (OptE) {
print_tokens(token);
return;
}
// parse gen ast
struct Obj_Var *prog = parse(token);
FILE *out = open_file(OutputFile);
codegen(prog, out);
}
// call assembler
static void assembler(char *input, char *output)
{
#ifdef RVPATH
char *as = format("%s/bin/riscv64-unknown-linux-gnu-as", RVPath);
#else
char *as = "as";
#endif /* ifdef RVPATH */
char *cmd[] = { as, "-c", input, "-o", output, NULL };
run_subprocess(cmd);
}
static char *find_file(char *pattern)
{
char *path = NULL;
glob_t buf = {};
glob(pattern, 0, NULL, &buf);
if (buf.gl_pathc > 0)
path = strdup(buf.gl_pathv[buf.gl_pathc - 1]);
globfree(&buf);
return path;
}
bool file_exists(char *path)
{
struct stat st;
return !stat(path, &st);
}
static char *find_lib_path(void)
{
#ifdef RVPATH
if (file_exists(format("%s/sysroot/usr/lib/crti.o", RVPath)))
return format("%s/sysroot/usr/lib", RVPath);
#else
if (file_exists("/usr/lib/riscv64-linux-gnu/crti.o"))
return "/usr/lib/riscv64-linux-gnu";
if (file_exists("/usr/lib64/crti.o"))
return "/usr/lib64";
#endif /* ifdef RVPATH */
error_out("library path is not find");
return NULL;
}
static char *find_gcc_lib_path(void)
{
#ifdef RVPATH
char *path = find_file(format(
"%s/lib/gcc/riscv64-unknown-linux-gnu/*/crtbegin.o", RVPath));
if (path)
return dirname(path);
#endif /* ifdef RVPATH */
char *paths[] = {
"/usr/lib/gcc/riscv64-linux-gnu/*/crtbegin.o ",
//Gentoo
"/usr/lib/gcc/riscv64-pc-linux-gnu/*/crtbegin.o",
// Fedora
"/usr/lib/gcc/riscv64-redhat-linux/*/crtbegin.o",
};
for (int i = 0; i < sizeof(paths) / sizeof(*paths); i++) {
char *path = find_file(paths[i]);
if (path)
return dirname(path);
}
error_out("gcc library path is not found");
return NULL;
}
static void run_linker(struct StringArray *inputs, char *output)
{
// args need be passed to ld
struct StringArray arr = {};
#ifdef RVPATH
char *ld = format("%s/bin/riscv64-unknown-linux-gnu-ld", RVPath);
#else
char *ld = "ld";
#endif /* ifdef RVPATH */
str_array_push(&arr, ld);
str_array_push(&arr, "-o");
str_array_push(&arr, output);
str_array_push(&arr, "-m");
str_array_push(&arr, "elf64lriscv");
str_array_push(&arr, "-dynamic-linker");
#ifdef RVPATH
char *lp64d =
format("%s/sysroot/lib/ld-linux-riscv64-lp64d.so.1", RVPath);
#else
char *lp64d = "/lib/ld-linux-riscv64-lp64d.so.1";
#endif /* ifdef RVPATH */
str_array_push(&arr, lp64d);
char *lib_path = find_lib_path();
char *gcc_lib_path = find_gcc_lib_path();
str_array_push(&arr, format("%s/crt1.o", lib_path));
str_array_push(&arr, format("%s/crti.o", lib_path));
str_array_push(&arr, format("%s/crtbegin.o", gcc_lib_path));
str_array_push(&arr, format("-L%s", gcc_lib_path));
str_array_push(&arr, format("-L%s", lib_path));
str_array_push(&arr, format("-L%s/..", lib_path));
#ifdef RVPATH
str_array_push(&arr, format("-L%s/sysroot/usr/lib64", RVPath));
str_array_push(&arr, format("-L%s/sysroot/lib64", RVPath));
str_array_push(&arr, format("-L%s/sysroot/usr/lib/riscv64-linux-gnu",
RVPath));
str_array_push(&arr, format("-L%s/sysroot/usr/lib/riscv64-pc-linux-gnu",
RVPath));
str_array_push(&arr, format("-L%s/sysroot/usr/lib/riscv64-redhat-linux",
RVPath));
str_array_push(&arr, format("-L%s/sysroot/usr/lib", RVPath));
str_array_push(&arr, format("-L%s/sysroot/lib", RVPath));
#else
str_array_push(&arr, "-L/usr/lib64");
str_array_push(&arr, "-L/lib64");
str_array_push(&arr, "-L/usr/lib/riscv64-linux-gnu");
str_array_push(&arr, "-L/usr/lib/riscv64-pc-linux-gnu");
str_array_push(&arr, "-L/usr/lib/riscv64-redhat-linux");
str_array_push(&arr, "-L/usr/lib");
str_array_push(&arr, "-L/lib");
#endif /* ifdef RVPATH */
for (int i = 0; i < inputs->len; i++)
str_array_push(&arr, inputs->data[i]);
str_array_push(&arr, "-lc");
str_array_push(&arr, "-lgcc");
str_array_push(&arr, "--as-needed");
str_array_push(&arr, "-lgcc_s");
str_array_push(&arr, "--no-as-needed");
str_array_push(&arr, format("%s/crtend.o", gcc_lib_path));
str_array_push(&arr, format("%s/crtn.o", lib_path));
str_array_push(&arr, NULL);
run_subprocess(arr.data);
}
// Compiler Driven Flow
//
// source file
// ↓
// preprocess file
// ↓
// cc1 compile to asm
// ↓
// as compile to obj
// ↓
// ld link to exec
int main(int argc, char *argv[])
{
#ifdef RVPATH
RVPath = getenv("RISCV");
if (RVPath == NULL)
error_out("riscv not find");
#endif /* ifdef MACRO */
// call cleanup() when program end
atexit(cleanup);
parse_args(argc, argv);
// if -cc1
// compile c to asm directly
if (OptCC1) {
add_default_include_paths(argv[0]);
cc1();
return 0;
}
// currently, it is not possible to output multiple input files
// to a single file after -c -S -E
if (InputPaths.len > 1 && OptO && (OptC || OptS || OptE))
error_out(
"cannot specify '-o' with '-c', '-S' or '-E' with multiple files");
// ld's args
struct StringArray ld_args = {};
// iterate though all input file
for (int i = 0; i < InputPaths.len; i++) {
// read input file
char *input = InputPaths.data[i];
char *output;
if (OptO)
output = OptO;
else if (OptS)
output = replace_extn(input, ".s");
else
output = replace_extn(input, ".o");
// .o
if (ends_with(input, ".o")) {
str_array_push(&ld_args, input);
continue;
}
// .s
if (ends_with(input, ".s")) {
if (!OptS)
assembler(input, output);
continue;
}
// .c
if (!ends_with(input, ".c") && strcmp(input, "-"))
error_out("unknown file extension: %s", input);
if (OptE) {
run_cc1(argc, argv, input, NULL);
continue;
}
// if '-S', call cc1
if (OptS) {
run_cc1(argc, argv, input, output);
continue;
}
// compile and assemble
if (OptC) {
char *tmp = create_tmpfile();
run_cc1(argc, argv, input, tmp);
assembler(tmp, output);
continue;
}
// else call cc1 and as
char *tmp1 = create_tmpfile();
char *tmp2 = create_tmpfile();
run_cc1(argc, argv, input, tmp1);
assembler(tmp1, tmp2);
str_array_push(&ld_args, tmp2);
continue;
}
if (ld_args.len > 0)
run_linker(&ld_args, OptO ? OptO : "a.out");
return 0;
}