forked from rui314/8cc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
239 lines (221 loc) · 6.34 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
// Copyright 2012 Rui Ueyama <[email protected]>
// This program is free software licensed under the MIT license.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "8cc.h"
static char *inputfile;
static char *outputfile;
static bool dumpast;
static bool cpponly;
static bool dumpasm;
static bool dontlink;
static String *cppdefs;
static List *tmpfiles = &EMPTY_LIST;
static void usage(void) {
fprintf(stderr,
"Usage: 8cc [ -E ][ -a ] [ -h ] <file>\n\n"
"\n"
" -I<path> add to include path\n"
" -E print preprocessed source code\n"
" -D name Predefine name as a macro\n"
" -D name=def\n"
" -S Stop before assembly (default)\n"
" -c Do not run linker (default)\n"
" -U name Undefine name\n"
" -fdump-ast print AST\n"
" -fno-dump-source Do not emit source code as assembly comment\n"
" -fno-dump-stack Do not print stacktrace\n"
" -d cpp print tokens for debugging\n"
" -o filename Output to the specified file\n"
" -g Do nothing at this moment\n"
" -Wall Enable all warnings\n"
" -Werror Make all warnings into errors\n"
" -O<number> Does nothing at this moment\n"
" -m64 Output 64-bit code (default)\n"
" -w Disable all warnings\n"
" -h print this help\n"
"\n"
"One of -a, -c, -E or -S must be specified.\n\n");
exit(1);
}
static void delete_temp_files(void) {
Iter *iter = list_iter(tmpfiles);
while (!iter_end(iter))
unlink(iter_next(iter));
}
static char *replace_suffix(char *filename, char suffix) {
char *r = format("%s", filename);
char *p = r + strlen(r) - 1;
if (*p != 'c')
error("filename suffix is not .c");
*p = suffix;
return r;
}
static FILE *open_output_file(void) {
if (!outputfile) {
if (dumpasm) {
outputfile = replace_suffix(inputfile, 's');
} else {
outputfile = format("/tmp/8ccXXXXXX.s");
if (!mkstemps(outputfile, 2))
perror("mkstemps");
list_push(tmpfiles, outputfile);
}
}
if (!strcmp(outputfile, "-"))
return stdout;
FILE *fp = fopen(outputfile, "w");
if (!fp)
perror("fopen");
return fp;
}
static void parse_warnings_arg(char *s) {
if (!strcmp(s, "error"))
warning_is_error = true;
else if (strcmp(s, "all"))
error("unknown -W option: %s", s);
}
static void parse_debug_arg(char *s) {
char *tok, *save;
while ((tok = strtok_r(s, ",", &save)) != NULL) {
s = NULL;
if (!strcmp(tok, "cpp"))
debug_cpp = true;
else
error("Unknown debug parameter: %s", tok);
}
}
static void parse_f_arg(char *s) {
if (!strcmp(s, "dump-ast"))
dumpast = true;
else if (!strcmp(s, "no-dump-stack"))
dumpstack = false;
else if (!strcmp(s, "no-dump-source"))
dumpsource = false;
else
usage();
}
static void parse_m_arg(char *s) {
if (strcmp(s, "64"))
error("Only 64 is allowed for -m, but got %s", s);
}
static void parseopt(int argc, char **argv) {
cppdefs = make_string();
for (;;) {
int opt = getopt(argc, argv, "I:ED:O:SU:W:acd:f:gm:o:hw");
if (opt == -1)
break;
switch (opt) {
case 'I':
add_include_path(optarg);
break;
case 'E':
cpponly = true;
break;
case 'D': {
char *p = strchr(optarg, '=');
if (p)
*p = ' ';
string_appendf(cppdefs, "#define %s\n", optarg);
break;
}
case 'O':
break;
case 'S':
dumpasm = true;
break;
case 'U':
string_appendf(cppdefs, "#undef %s\n", optarg);
break;
case 'W':
parse_warnings_arg(optarg);
break;
case 'a':
dumpast = true;
break;
case 'c':
dontlink = true;
break;
case 'd':
parse_debug_arg(optarg);
break;
case 'f':
parse_f_arg(optarg);
break;
case 'm':
parse_m_arg(optarg);
break;
case 'g':
break;
case 'o':
outputfile = optarg;
break;
case 'w':
enable_warning = false;
break;
case 'h':
default:
usage();
}
}
if (optind != argc - 1)
usage();
if (!dumpast && !cpponly && !dumpasm && !dontlink)
error("One of -a, -c, -E or -S must be specified");
inputfile = argv[optind];
}
static void preprocess(void) {
for (;;) {
Token *tok = read_token();
if (!tok)
break;
if (tok->bol)
printf("\n");
if (tok->nspace)
printf("%*c", tok->nspace, ' ');
printf("%s", t2s(tok));
}
printf("\n");
exit(0);
}
int main(int argc, char **argv) {
setbuf(stdout, NULL);
if (atexit(delete_temp_files))
perror("atexit");
cpp_init();
parse_init();
parseopt(argc, argv);
if (string_len(cppdefs) > 0)
cpp_eval(get_cstring(cppdefs));
lex_init(inputfile);
set_output_file(open_output_file());
if (cpponly)
preprocess();
List *toplevels = read_toplevels();
for (Iter *i = list_iter(toplevels); !iter_end(i);) {
Node *v = iter_next(i);
if (dumpast)
printf("%s", a2s(v));
else
emit_toplevel(v);
}
close_output_file();
if (!dumpast && !dumpasm) {
char *objfile = replace_suffix(inputfile, 'o');
pid_t pid = fork();
if (pid < 0) perror("fork");
if (pid == 0) {
execlp("as", "as", "-o", objfile, "-c", outputfile, (char *)NULL);
perror("execl failed");
}
int status;
waitpid(pid, &status, 0);
if (status < 0)
error("as failed");
}
return 0;
}