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
|
#include "common.h"
#include "obj/obj.h"
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
struct option ccopt;
struct inclpaths *cinclpaths;
static void
addinclpath(const char *path)
{
struct inclpaths *p = alloc(&globarena, sizeof *cinclpaths, 0);
p->next = cinclpaths;
p->path = path;
cinclpaths = p;
}
/* parse an argument of the form 'opt=abcd'
* e.g. arg="foo=bar123"; opt="foo"; returns "bar123" */
static const char *
optval(const char *arg, const char *opt)
{
uint n1 = strlen(arg), n2 = strlen(opt);
if (n1 < n2+1 || memcmp(arg, opt, n2) != 0 || arg[n2] != '=')
return NULL;
return arg + n2 + 1;
}
/* "foo.bar" -> "bar"; ".dotfile" -> "" */
static const char *
fileext(const char *path)
{
const char *dot = NULL;
assert(path && *path && "empty");
for (++path; *path; ++path) {
if (*path == '.') dot = path;
}
return dot ? dot+1 : "";
}
enum inft { IFTauto, IFTc, IFTasm, IFTobj, IFTar };
static enum inft
ftdetect(const char *s)
{
const char *ext = fileext(s);
if (!strcmp(ext, "c")) return IFTc;
if (!strcmp(ext, "o")) return IFTobj;
if (!strcmp(ext, "a")) return IFTar;
if (!strcmp(ext, "s")) return IFTasm;
warn(NULL, "assuming %'s is C source file", s);
return IFTc;
}
static union {
struct arena a;
char mem[sizeof(struct arena) + (1<<10)];
} _arenamem;
struct arena *globarena = &_arenamem.a;
/* withext("x/y.c", "o") -> "y.o"; withext("f9", "s") -> "f9.s" */
static const char *
withext(const char *path, const char *ext)
{
char *res;
size_t len;
const char *oext, *file = path;
while (*path)
if (*path++ == '/')
file = path;
assert(*file && "no filename");
oext = fileext(file);
if (!*oext)
len = strlen(file);
else
len = oext - file - 1;
res = alloc(&globarena, len + 1 + (ext ? strlen(ext) + 1 : 0), 1);
memcpy(res, file, len);
if (ext) {
res[len] = '.';
memcpy(res + len + 1, ext, strlen(ext));
} else {
res[len] = 0;
}
return res;
}
static struct task {
enum outft { OFTexe, OFTdll, OFTobj, OFTasm, OFTc } outft;
const char *out;
const char *targ;
const char *inf[64];
enum inft inft[64];
char **runargs;
vec_of(const char *) linkargs;
int ninf;
bool verbose, run;
} task;
static void prihelp(void);
static void
optparse(char **args)
{
const char *arg, *x;
enum inft ft = IFTauto;
while ((arg = *++args)) {
if (*arg++ != '-' || !*arg) {
assert(task.ninf < countof(task.inf) && "too many infiles");
task.inf[task.ninf] = arg[-1] != '-' ? arg-1 : "/dev/stdin";
task.inft[task.ninf] = ft ? ft : ftdetect(arg-1);
++task.ninf;
ft = IFTauto;
if (task.run) {
task.runargs = args+1;
return;
}
continue;
}
if (!strcmp(arg, "help") || !strcmp(arg, "h") || !strcmp(arg, "-help")) {
prihelp();
exit(0);
} else if ((x = optval(arg, "std"))) {
if (!strcmp(x, "c89") || !strcmp(x, "c90")) ccopt.cstd = STDC89;
else if (!strcmp(x, "c99")) ccopt.cstd = STDC99;
else if (!strcmp(x, "c11")) ccopt.cstd = STDC11;
else if (!strcmp(x, "c2x")) ccopt.cstd = STDC23;
else if (!strcmp(x, "c23")) ccopt.cstd = STDC23;
else goto Bad;
} else if (!strcmp(arg, "pedantic")) {
ccopt.pedant = 1;
} else if (!strcmp(arg, "trigraphs")) {
ccopt.trigraph = 1;
} else if (*arg == 'd' && arg[1]) {
/* see common.h§struct option */
while (*++arg) switch (*arg | 32) {
case 'p': ccopt.dbg.p = 1; break;
case 'a': ccopt.dbg.a = 1; break;
case 'm': ccopt.dbg.m = 1; break;
case 'o': ccopt.dbg.o = 1; break;
case 'i': ccopt.dbg.i = 1; break;
case 'l': ccopt.dbg.l = 1; break;
case 'r': ccopt.dbg.r = 1; break;
default: warn(NULL, "-d: invalid debug flag %'c", *arg);
}
} else if (*arg == 'o') {
if (arg[1]) task.out = arg+1;
else if (args[1]) task.out = *++args;
else fatal(NULL, "missing path after `-o`");
} else if (*arg == 'f') {
/* -fabc / -fno-abc flags */
const char *flag = arg+1;
bool set = 1;
if (!strncmp(flag, "no-", 3)) {
set = 0;
flag += 3;
}
if (!strcmp(flag, "pie") || !strcmp(flag, "PIE")) ccopt.pie = set;
else if (!strcmp(flag, "pic") || !strcmp(flag, "PIC")) ccopt.pic = set;
else goto Bad;
} else if (*arg == 'l') {
vpush(&task.linkargs, arg-1);
} else if (!strcmp(arg, "v") || !strcmp(arg, "-verbose")) {
task.verbose = 1;
} else if (!strcmp(arg, "c")) {
task.outft = OFTobj;
} else if (!strcmp(arg, "E")) {
task.outft = OFTc;
} else if (!strcmp(arg, "xc")) {
ft = IFTc;
} else if (!strcmp(arg, "xo")) {
ft = IFTobj;
} else if (!strcmp(arg, "run")) {
task.run = 1;
if (task.ninf > 0) {
task.runargs = args+1;
return;
}
} else if (*arg == 'g') {
/* TODO debug info */
} else if (*arg == 'D' || *arg == 'U') {
void cpppredef(bool undef, const char *cmd);
const char *def = arg[1] ? arg+1 : *++args;
if (!def) fatal(NULL, "macro name missing after `-%c`", *arg);
cpppredef(*arg == 'U', def);
} else if (*arg == 'O') {
/* TODO optimization level */
} else if (*arg == 'I') {
const char *p = arg[1] ? arg+1 : *++args;
if (!p) fatal(NULL, "missing path after `-I`");
else addinclpath(p);
} else if (*arg == 'M') {
++arg;
if (*arg == 'F' || *arg == 'T' || *arg == 'Q') {
const char *p = arg[1] ? arg+1 : *++args;
if (!p) fatal(NULL, "missing path after `-M%c`", *arg);
}
/* TODO depfiles */
} else if (*arg == 'W') {
if (!strcmp(arg+1, "error")) {
ccopt.werror = 1;
}
/* TODO warning switches */
} else Bad: warn(NULL, "unrecognized option: %'s", arg-1);
}
if (!task.ninf) fatal(NULL, "no input files");
if (!task.out) {
switch (task.outft) {
case OFTdll:
case OFTexe: if (!task.run) task.out = "a.out"; break;
case OFTasm: task.out = withext(*task.inf, "s"); break;
case OFTobj: task.out = withext(*task.inf, "o"); break;
case OFTc: break;
}
}
if (!in_range(task.outft, OFTexe, OFTdll) && task.outft != OFTc && task.ninf > 1)
fatal(NULL, "too many input files");
}
static const char *
tempfile(const char *path, const char *ext)
{
long time(void *);
int id;
static int id2;
static char sbuf[1024];
const char *tmpdir;
const char *file = path;
struct wbuf fbuf = MEMBUF(sbuf, sizeof sbuf);
tmpdir = getenv("TMPDIR");
tmpdir = tmpdir ? tmpdir : "/tmp";
id = getpid();
id = id < 0 ? time(NULL) : id;
while (*path)
if (*path++ == '/')
file = path;
bfmt(&fbuf, "%s/cc%x@%u@", tmpdir, id, id2++);
while (*file++) {
char c = file[-1];
if (in_range(c, 'a', 'z') || in_range(c, 'A', 'Z') || in_range(c, '0', '9')
|| c == '_' || c == '-')
{
ioputc(&fbuf, c);
} else if (c == '.') break;
else ioputc(&fbuf, '_');
}
bfmt(&fbuf, "%s%s", &"."[!ext], ext ? ext : "");
ioputc(&fbuf, 0);
assert(!fbuf.err);
return alloccopy(&globarena, fbuf.buf, fbuf.len, 1);
}
static int cc1(const char *out, const char *in);
static const char *tempobj[countof(task.inf)], *tempout;
static void
mktemps(void) {
for (int i = 0; i < task.ninf; ++i) {
if (task.inft[i] == IFTc)
tempobj[i] = tempfile(task.inf[i], "o");
}
if (!task.out)
task.out = tempout = tempfile(task.ninf > 1 ? "run" : withext(task.inf[0], NULL), NULL);
}
static void
cleantemps(void)
{
for (int i = 0; i < task.ninf; ++i) {
if (tempobj[i]) unlink(tempobj[i]), tempobj[i] = NULL;
}
if (tempout)
unlink(tempout), tempout = NULL;
}
static void
sigcleantemps(int _)
{
cleantemps();
}
static void
compileobjs(void)
{
int wstat;
pid_t p;
if (!ccopt.dbg.any) mktemps();
for (int i = 0; i < task.ninf; ++i) {
if (task.inft[i] == IFTc) {
if ((p = fork()) < 0) {
error(NULL, "fork(): %s\n", strerror(errno));
exit(1);
} else if (p == 0) {
exit(cc1(tempobj[i], task.inf[i]));
}
waitpid(p, &wstat, 0);
if (!WIFEXITED(wstat)) exit(127);
if (WEXITSTATUS(wstat) != 0) {
cleantemps();
exit(WEXITSTATUS(wstat));
}
} else if (task.inft[i] == IFTobj || task.inft[i] == IFTar) {
} else assert(!"not obj");
}
if (!ccopt.dbg.any) {
atexit(cleantemps);
signal(SIGINT, sigcleantemps);
}
}
static int
dolink(void)
{
static const char *cmdbuf[10];
pid_t p;
int wstat;
vec_of(const char *) cmd = VINIT(cmdbuf, countof(cmdbuf));
/* TODO don't depend on external c compiler, find lib and runtime paths and
* invoke linker directly.. */
vpush(&cmd, "/bin/gcc");
if (task.outft == OFTdll) {
vpush(&cmd, "-shared");
} else if (task.outft == OFTexe) {
vpush(&cmd, ccopt.pie ? "-pie" : "-no-pie");
}
vpushn(&cmd, task.linkargs.p, task.linkargs.n);
vpush(&cmd, "-o");
vpush(&cmd, task.out);
assert(task.ninf > 0);
for (int i = 0; i < task.ninf; ++i) {
const char *o;
switch (task.inft[i]) {
case IFTc: o = tempobj[i]; break;
case IFTobj: case IFTar:
o = task.inf[i]; break;
default: assert(!"link obj?");
}
vpush(&cmd, o);
}
if (task.verbose) {
efmt("> ");
for (int i = 0; i < cmd.n; ++i)
efmt("%s ", cmd.p[i]);
efmt("\n");
}
vpush(&cmd, NULL);
if ((p = fork()) < 0) {
error(NULL, "fork(): %s\n", strerror(errno));
exit(1);
} else if (p == 0) {
if (!execv(cmd.p[0], (char **)cmd.p)) {
error(NULL, "execv(): %s\n", strerror(errno));
exit(1);
}
}
vfree(&cmd);
waitpid(p, &wstat, 0);
if (!WIFEXITED(wstat)) return 127;
return WEXITSTATUS(wstat);
}
#include <fcntl.h> /* open */
static int
dorun(void)
{
if (task.verbose) {
efmt("> exec %s", task.out);
for (char **s = task.runargs; *s; ++s)
efmt(" %s", *s);
efmt("\n");
}
int fd = open(task.out, O_RDONLY);
if (fd < 0) {
error(NULL, "open(): %s\n", strerror(errno));
return 1;
}
if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0) {
error(NULL, "fcntl(): %s\n", strerror(errno));
return 1;
}
cleantemps();
extern char **environ;
int fexecve(int fd, char *const argv[], char *const envp[]);
fexecve(fd, task.runargs - 1, environ);
error(NULL, "fexecv(): %s\n", strerror(errno));
return 1;
}
static int
driver(void)
{
void cpp(struct wbuf *, const char *);
if (task.verbose)
efmt("# Target: %s\n", task.targ);
if (task.outft == OFTobj) {
assert(task.ninf == 1);
if (*task.inft != IFTc)
fatal(NULL, "not a C source file: %s", task.inf[0]);
return cc1(task.out, *task.inf);
} else if (task.outft == OFTc) {
struct wbuf _buf = {0}, *buf = &bstdout;
if (task.out) {
buf = &_buf;
buf->buf = alloc(&globarena, buf->cap = 1<<12, 1);
buf->fd = open(task.out, O_CREAT | O_TRUNC | O_WRONLY, 0777);
if (buf->fd < 0) {
error(NULL, "open(%'s): %s", task.out, strerror(errno));
return 1;
}
}
bool ok = 1;
if (!task.out && task.ninf == 1)
cpp(buf, task.inf[0]);
else for (int i = 0; i < task.ninf; ++i) {
pid_t p;
int wstat;
if ((p = fork()) < 0) {
error(NULL, "fork(): %s\n", strerror(errno));
ok = 0;
} else if (p == 0) {
cpp(buf, task.inf[i]);
exit(0);
}
waitpid(p, &wstat, 0);
if (!WIFEXITED(wstat)) ok = 0;
ok = ok && WEXITSTATUS(wstat) == 0;
}
ioflush(buf);
if (task.out)
close(buf->fd);
return ok ? 0 : 1;
} else if (task.outft == OFTexe || task.outft == OFTdll) {
compileobjs();
if (ccopt.dbg.any) return 0;
if (!task.run) return dolink();
int st = dolink();
if (st == 0) st = dorun();
return st;
}
assert(0);
}
static int
cc1(const char *out, const char *in)
{
void ccomp(const char *);
extern int nerror;
if (task.verbose) efmt("cc1(/*out*/ %'s, /*in*/ %'s)\n", out, in);
if (!ccopt.dbg.any) objini(in, out);
ccomp(in);
if (!ccopt.dbg.any && !nerror) objfini();
return !!nerror;
}
static void
detectcolor(void)
{
const char *s;
if (!isatty(STDERR_FILENO)
|| ((s = getenv("NO_COLOR")) && *s)
|| ((s = getenv("TERM")) && !strcmp(s, "dumb")))
ccopt.nocolor = 1;
}
static void
sysinclpaths(void)
{
static const char *paths[] = {
"/usr/local/include",
"/usr/include"
};
for (int i = 0; i < countof(paths); ++i)
addinclpath(paths[i]);
}
static void
prihelp(void)
{
pfmt("Usage: antcc [options] infile(s)...\n"
" antcc [options] -run infile [arguments...]\n"
" antcc [options] infile(s)... -run [arguments...]\n"
"Options:\n"
" -help \tPrint this help message\n"
" -std=<..> \tSet C standard (c89, c99, c11, c23)\n"
" -pedantic \tWarnings for strict standards compliance\n"
" -d{pamoilr} \tDebug print IR after {parse, abi, mem, opts, isel, live, rega}\n"
" -o <file> \tPlace the output into <file>\n"
" -v \tVerbose output\n"
" -c \tEmit object file but do not link\n"
" -run \tRun compiled sources\n"
" -Idir \tAdd include path\n"
" -Dsym[=val] \tDefine macro\n"
" -Usym \tUndefine macro\n"
" -E \tPreprocess only\n"
" -llib \tLink with library\n"
" -fpie \tEmit code for position independent executable\n"
" -fpic \tEmit position independent code\n"
" -x[c|o] \tSpecify type of next input file (C, object)\n"
);
}
int
main(int argc, char **argv)
{
globarena->cap = sizeof(_arenamem.mem) - sizeof(struct arena);
ioinit();
/* setup defaults */
detectcolor();
sysinclpaths();
ccopt.cstd = STDC11;
ccopt.pie = 1;
ccopt.dbgout = &bstdout;
/* parse cli ags */
if (argc == 1) {
prihelp();
return 1;
}
optparse(argv);
/* global init */
task.targ = task.targ ? task.targ : "amd64-sysv";
targ_init(task.targ);
return driver();
}
/* vim:set ts=3 sw=3 expandtab: */
|