-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompmain.c
More file actions
352 lines (335 loc) · 9.53 KB
/
compmain.c
File metadata and controls
352 lines (335 loc) · 9.53 KB
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
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <pthread.h>
#include <getopt.h>
#include <sys/wait.h>
#include "compintern.h"
#include "printree.h"
#include "3ac.h"
#include "ssa.h"
#include "opt.h"
#include "codegen.h"
#include "reg.h"
#ifdef NODEBUG
#define DEBUG(a)
#else
#define DEBUG(a) a
#endif
#if !defined(USECLANG) && !defined(USEMUSL) && !defined(USEGCC)
#define USEGCC
#endif
#ifndef LIBDIR
#define LIBDIR "/usr/lib/"
#endif
//ELF magic bits
const char magic[16] = {0x7f, 0x45, 0x4c, 0x46, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
pthread_mutex_t printlock, listlock; //global mutexes for synchronizing printing, processing
int listptr, linkallstart; //global variables for processing the list of files to compile, link
int predefines; //file descriptor for file with command line -D options passed through to it
DYNARR* includepath;
char tmpname[32] = "precompilationXXXXXX"; //name of precompile file
struct yyltype {int first_line, last_line, first_column, last_column; char* filename;};
struct yyltype* yyget_lloc(void*);
void yyset_lloc(struct yyltype*, void*);
int yyparse(void* scanner, char* filename);
int yyset_in(FILE*, void*);
int yyset_debug(int flag, void*);
void yylex_init_extra(void*, void**);
void yylex_destroy(void*);
//outputs file name for .joeccs object file
static char* explainjoke(char* filename, char lastchar) {
char* lastind = strrchr(filename, '.');
int len;
if(lastind) {
len = lastind - filename;
} else {
len = strlen(filename);
}
char* newname = malloc(len + 8);
strncpy(newname, filename, len);
strcpy(newname + len, ".joecc");
newname[len + 6] = lastchar;
newname[len + 7] = '\0';
return newname;
}
//compiles a given file
static char filecomp(char** filename) {
FILE* precontext = fdopen(dup(predefines), "r");
FILE* yyin = fopen(*filename, "r");
if(yyin == NULL)
return 0;
char maymagic[16];
size_t magiclen = fread(maymagic, 1, 16, yyin);
if(magiclen == 16 && !memcmp(magic, maymagic, 16)) {
fclose(yyin);
return 0;
//It's an ELF file
char type = fgetc(yyin);
switch(type) {
case 0: case -1:
case 4: //core dump
return 1; //error
case 1:
return 1; //need to link
case 2:
return 1; //static exe
case 3:
return 1; //dynamic exe
}
} else {
rewind(yyin);
}
void* scanner;
struct lexctx* lctx = ctxinit(yyin, *filename);
yylex_init_extra(lctx, &scanner);
struct yyltype* ylt = malloc(sizeof(LOCTYPE));
ylt->last_line = ylt->first_line = 0;
ylt->last_column = ylt->first_column = 0;
ylt->filename = tmpname;
yyset_lloc(ylt, scanner);
DEBUG(yyset_debug(0, scanner));//not debugging lexer for now
yyset_in(precontext, scanner);
yyparse(scanner, *filename);
free(ylt);
yylex_destroy(scanner);
dadtorcfr(lctx->enumerat2free, (void(*)(void*)) freenum);
qchtdtor(lctx->defines, (void (*)(void*)) freemd);
qhtdtor(lctx->withindefines);
dadtor(lctx->definestack);
dadtor(lctx->ls->locs);
dadtor(lctx->ls->argpp);
dadtorfr(lctx->halflocs);
if(lctx->failurestate) {
*filename = NULL;
} else {
DYNARR* funcky = qhtpairs(lctx->funcs);
char* newname = explainjoke(*filename, 's');
FILE* objf = fopen(newname, "w");
startgenfile(objf, lctx);
DEBUG(pthread_mutex_lock(&printlock));
puts("Functions defined:");
for(int i = 0; i < funcky->length; i++) {
QHASHPAIR* pairthere = daget(funcky, i);
if(pairthere->value) {
FUNC* f = pairthere->value;
//treefunc(pairthere->value);
DEBUG(putchar('\n'));
DEBUG(puts(pairthere->key));
PROGRAM* prog = linefunc(f); //fix main func
splitcrit(prog); //for GVN
prunebranch(prog); //esp for do while 0
blockunblock(prog);//remove unnecessary edges
rmunreach(prog);//maybe?
collatealloc(prog);
remove_nops(prog);
DEBUG(printf("Ops before SSA %d\n", countops(prog)));
DEBUG(treeprog(prog, pairthere->key, "pressa"));
ssa(prog); //no nops are generated here
DEBUG(printf("Ops after SSA %d\n", countops(prog)));
DEBUG(treeprog(prog, pairthere->key, "justssa"));
constfold(prog);
gvn(prog);
remove_nops(prog);
DEBUG(printf("Ops after GVN %d\n", countops(prog)));
DEBUG(treeprog(prog, pairthere->key, "withgvn"));
ldstrsep(prog);
DEBUG(printf("Ops after ldstrsep %d\n", countops(prog)));
DEBUG(treeprog(prog, pairthere->key, "ldstrsep"));
renumber(prog);
DEBUG(treeprog(prog, pairthere->key, "renumber"));
liveness(prog);
remove_nops(prog);
DEBUG(treeprog(prog, pairthere->key, "liveness"));
genprogfile(objf, f->retrn, pairthere->key, prog);
freeprog(prog);
}
}
fclose(objf);
free(newname);
dadtor(funcky);
}
DEBUG(pthread_mutex_unlock(&printlock));
scopepop(lctx);
qchtdtor(lctx->funcs, (void(*)(void*)) &rfreefunc);
dadtor(lctx->scopes);
dadtorcfr(lctx->enstruct2free, (void(*)(void*)) wipestruct);
dadtorcfr(lctx->externglobals, (void(*)(void*)) freeinit);
dadtorcfr(lctx->globals, (void(*)(void*)) freeinit);
free(lctx->ls);
char fsv = lctx->failurestate;
free(lctx);
return fsv;
}
//delegates to the threads which thing they compile next
static void* ldeleg(void* arg) {
char** argv = arg;
while(1) {
char** fn;
pthread_mutex_lock(&listlock);
fn = &argv[listptr];
if(!*fn) {
pthread_mutex_unlock(&listlock);
return NULL;
}
++listptr;
pthread_mutex_unlock(&listlock);
filecomp(fn);
}
}
//links specified files
static void linkall(char const* outfile, char** argv, int argc) {
DYNARR* fnames = dactor(8);
dapush(fnames, strdup("/bin/ld"));
//++ to skip first element
int forkpid;
char** newargy = argv + linkallstart;
for(int i = 0; i < argc; i++) {
char* humorless = *(newargy++);
if(humorless) {
char* humored = explainjoke(humorless, 's');
char* humored2 = explainjoke(humorless, 'o');
switch((forkpid = fork())) {
case -1:
exit(-1);
case 0:
execl("/bin/as", "/bin/as", humored, "-o", humored2, NULL);
break;
default:
waitpid(forkpid, NULL, 0);
break;
}
dapush(fnames, humored2);
free(humored);
}
}
dapush(fnames, strdup(LIBDIR "crt1.o"));
dapush(fnames, strdup(LIBDIR "crti.o"));
dapush(fnames, strdup(LIBDIR "libc.a"));
dapush(fnames, strdup(LIBDIR "libgcc_s.so.1"));
dapush(fnames, strdup("-o"));
dapush(fnames, strdup(outfile));
dapush(fnames, NULL);
switch((forkpid = fork())) {
case -1:
exit(-1);
case 0:
execv("/bin/ld", (char* const*) fnames->arr);
break;
default:
waitpid(forkpid, NULL, 0);
break;
}
dapop(fnames);
dadtorfr(fnames);
}
const char* searchpath[] = {
#ifdef USECLANG
"/usr/lib/clang/" HEADERS_VERSION "/include",
#elif defined(USEMUSL)
"/usr/lib/musl/include",
#elif defined(USEGCC)
"/usr/lib/gcc/x86_64-pc-linux-gnu/" HEADERS_VERSION "/include",
#else
#error
#endif
"/usr/local/include",
#ifdef USEGCC
"/usr/lib/gcc/x86_64-pc-linux-gnu/" HEADERS_VERSION "/include-fixed",
#endif
"/usr/include",
};
int main(int argc, char** argv) {
if(argc <= 1) {
exit(0);
}
pthread_t pt2, pt3, pt4;
int opt, opt_ind;
char const* filedest = "a.out";
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{NULL, 0, NULL, 0},
};
predefines = mkstemp(tmpname);
unlink(tmpname);
includepath = dactor(8);
for(unsigned int i = 0; i < sizeof(searchpath) / sizeof(char*); i++)
dapush(includepath, strdup(searchpath[i]));
while((opt = getopt_long(argc, argv, "D:cl:o:hvI:", long_options, &opt_ind)) != -1) {
char* c;
switch(opt) {
case 'l':
break;
case 'c':
break;
case 'o':
filedest = optarg;
if(!filedest) {
fprintf(stderr, "No output file specified\n");
exit(-1);
}
break;
case 'h':
printf("JOECC Compiler version 0.0.1-alpha\n");
printf("JOECC Copyright (C) 2020-2021 Jonathan Singer\n");
printf("This program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, see the license for further details.\n");
return 0;
case 'v':
printf("JOECC Compiler version 0.0.1-alpha\n");
break;
case 'D':
//figure out how to actually use predefines
c = strchr(optarg, '=');
if(c != NULL)
*c = ' ';
dprintf(predefines, "#define %s\n", optarg);
break;
case 'I': //figure out how to add to include path
c = realpath(optarg, NULL);
//handle errno
dapush(includepath,c);
break;
case 0:
break;
default:
break;
}
}
listptr = optind;
linkallstart = optind;
pthread_mutex_init(&printlock, NULL);
pthread_mutex_init(&listlock, NULL);
switch(argc - optind) {
default:
pthread_create(&pt4, NULL, ldeleg, argv);
//fall through
case 3:
pthread_create(&pt3, NULL, ldeleg, argv);
//fall through
case 2:
pthread_create(&pt2, NULL, ldeleg, argv);
//fall through
case 1:
ldeleg(argv);
case 0:
break;
}
switch(argc - optind) {
default:
pthread_join(pt4, NULL);
//fall through
case 3:
pthread_join(pt3, NULL);
//fall through
case 2:
pthread_join(pt2, NULL);
case 1:
case 0:
break;
}
dadtorfr(includepath);
close(predefines);
linkall(filedest, argv, argc);
return 0;
}