forked from klange/toarucc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcc-compiler.c
450 lines (349 loc) · 9 KB
/
tcc-compiler.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
/* vim: tabstop=4 shiftwidth=4
* This file is part of the Toaru Compiler Collection (toarucc)
* and is licensed under the ISC license. See LICENSE for details.
*
* tcc-compiler - C compiler
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TOARUCC_VERSION_MAJOR 0
#define TOARUCC_VERSION_MINOR 1
#define TOARUCC_VERSION_PATCH 5
#define MAX_INPUT_FILES 32
#define MAX_INPUT_NAME 64
#define MAX_OUTPUT_NAME 64
int skip_compiling = 0;
int skip_assembling = 0;
int skip_linking = 0;
char input_name[MAX_INPUT_NAME][MAX_INPUT_FILES] = {};
char output_name[MAX_OUTPUT_NAME] = {};
FILE *gpFile = NULL;
void preprocess()
{
fprintf(stderr, "Preprocessing...\n");
//ssize_t nRead = 0;
//char *sLine = NULL;
//size_t nLen = 0;
int nBufLen = 255;
char sBufData[nBufLen];
FILE *pFileProcessed = fopen("test_proc.c", "w");
if(!pFileProcessed)
{
fprintf(stderr, "Failed to open a preprocessed file!\n");
exit(EXIT_FAILURE);
};
fprintf(stderr, "* Building a replacement table...\n");
int skipline = 0;
//while((nRead = getline(&sLine, &nLen, gpFile)) != -1)
while(fgets(sBufData, nBufLen, gpFile))
{
// TODO: find and replace all #
// Check if the first symbol is equal to a # sign
if(sBufData[0] == '#')
{
char *sToken = NULL;
sToken = strtok(sBufData + 1, " ");
fprintf(stderr, "Found a token: %s\n", sToken);
if(!strcmp(sToken, "include"))
{
fprintf(stderr, "Found an include directive!\n");
sToken = strtok(NULL, " ");
// TODO: check for null token
fprintf(stderr, "Found an include path: %s\n", sToken);
// TODO: replace with the contents of the file
continue;
}
else if(!strcmp(sToken, "define"))
{
fprintf(stderr, "Found a define directive!\n");
sToken = strtok(NULL, " ");
// TODO: check for null token
fprintf(stderr, "Found a define key: %s\n", sToken);
sToken = strtok(NULL, " ");
// TODO: check for null token
fprintf(stderr, "Found a define value: %s\n", sToken);
// TODO
//gTokenReplacementMap[sToken] = ;
continue;
}
else if(!strcmp(sToken, "undef"))
{
fprintf(stderr, "Found an undef directive!\n");
continue;
}
else if(!strcmp(sToken, "pragma"))
{
fprintf(stderr, "Found a pragma directive!\n");
sToken = strtok(NULL, " ");
// TODO: check for null token
fprintf(stderr, "Found a pragma key: %s\n", sToken);
if(!strcmp(sToken, "once"))
{
// TODO
};
continue;
}
else if(!strcmp(sToken, "error"))
{
fprintf(stderr, "Found an error directive!\n");
continue;
}
else if(!strcmp(sToken, "warning"))
{
fprintf(stderr, "Found a warning directive!\n");
continue;
}
else if(!strcmp(sToken, "if"))
{
fprintf(stderr, "Found an if directive!\n");
int result = 1;
sToken = strtok(NULL, " ");
// TODO: check for null token
if(!strcmp(sToken, "defined"))
{
sToken = strtok(NULL, " ");
// TODO: check for null token
// TODO: parse the expression and evaluate it
if(!strcmp(sToken, "FALSE")) // TODO: temp
result = 0;
}
else
{
if(!strcmp(sToken, "FALSE")) // TODO: temp
result = 0;
};
if(result == 0)
skipline = 1;
continue;
}
else if(!strcmp(sToken, "ifdef"))
{
fprintf(stderr, "Found an ifdef directive!\n");
sToken = strtok(NULL, " ");
// TODO: check for null token
// TODO: parse the expression and evaluate it
if(!strcmp(sToken, "FALSE")) // TODO: temp
skipline = 1;
continue;
}
else if(!strcmp(sToken, "ifndef"))
{
fprintf(stderr, "Found an ifndef directive!\n");
sToken = strtok(NULL, " ");
// TODO: check for null token
// TODO: parse the expression and evaluate it
if(!strcmp(sToken, "TRUE")) // TODO: temp
skipline = 1;
continue;
}
else if(!strcmp(sToken, "elseif") || !strcmp(sToken, "elif"))
{
fprintf(stderr, "Found an elseif directive!\n");
int result = 1;
sToken = strtok(NULL, " ");
// TODO: check for null token
if(!strcmp(sToken, "defined"))
{
sToken = strtok(NULL, " ");
// TODO: check for null token
// TODO: parse the expression and evaluate it
if(!strcmp(sToken, "FALSE")) // TODO: temp
result = 0;
}
else
{
if(!strcmp(sToken, "FALSE")) // TODO: temp
result = 0;
};
if(result == 0)
skipline = 1;
continue;
}
else if(!strncmp(sToken, "else", 4))
{
fprintf(stderr, "Found an else directive!\n");
if(skipline)
skipline = 0;
continue;
}
else if(!strncmp(sToken, "endif", 5))
{
fprintf(stderr, "Found an endif directive!\n");
skipline = 0;
continue;
}
else
{
fprintf(stderr, "Unknown directive: %s!\n", sToken);
return;
};
}
else // A regular line
{
// Check for comments
{
char *pCurChar = strchr(sBufData, '/');
if(pCurChar != NULL)
{
// TODO: add an option to leave comments?
++pCurChar;
if(*pCurChar == '/')
{
//skipline = 1;
fprintf(stderr, "Found a single-line comment: %s\n", pCurChar + 1);
continue;
}
else if(*pCurChar == '*')
{
// TODO: skip until the '* /' symbols
fprintf(stderr, "Found a multi-line comment start!\n");
//skipping = 1;
}
else if(*(pCurChar - 2) == '*')
{
fprintf(stderr, "Found a multi-line comment end!\n");
//skipping = 0;
};
};
};
// Replace tabs with spaces
{
char *pCurChar = strchr(sBufData, '\t');
while(pCurChar != NULL)
{
*pCurChar = ' ';
pCurChar = strchr(sBufData, '\t');
};
};
};
if(!skipline)
{
//fprintf(stderr, "Read: %zu\n", nRead);
//fprintf(stderr, "Line: %s\n", sLine);
fprintf(stderr, "%s", sBufData);
fputs(sBufData, pFileProcessed);
};
};
fclose(pFileProcessed);
};
void tokenize()
{
fprintf(stderr, "Tokenizing...\n");
};
void parse()
{
fprintf(stderr, "Parsing...\n");
};
void optimize()
{
fprintf(stderr, "Optimizing...\n");
};
void compile()
{
fprintf(stderr, "Compiling...\n");
tokenize();
parse();
optimize();
};
void assemble()
{
fprintf(stderr, "Assembling...\n");
};
void link()
{
fprintf(stderr, "Linking...\n");
};
void print_help()
{
fprintf(stderr, "Usage: toarucc [options] file...\n");
fprintf(stderr, "Options:\n");
fprintf(stderr, " --help\t\tDisplay this information.\n");
fprintf(stderr, " --version\t\tDisplay compiler version information.\n");
fprintf(stderr, " -E\t\t\tPreprocess only; do not compile, assemble or link.\n");
fprintf(stderr, " -S\t\t\tPreprocess and compile, but do not assemble or link.\n");
fprintf(stderr, " -c\t\t\tPreprocess, compile and assemble, but don't link.\n");
fprintf(stderr, " -o <file>\t\tPlace the output input the specified <file>.\n");
};
void print_version_short()
{
fprintf(stderr, "toarucc version %d.%d.%d\n", TOARUCC_VERSION_MAJOR, TOARUCC_VERSION_MINOR, TOARUCC_VERSION_PATCH);
};
void print_version()
{
fprintf(stderr, "toarucc %d.%d.%d\n", TOARUCC_VERSION_MAJOR, TOARUCC_VERSION_MINOR, TOARUCC_VERSION_PATCH);
fprintf(stderr, "Copyright (C) 2020-2022 ToaruCC Contributors\n");
fprintf(stderr,
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
};
int main(int argc, char **argv)
{
for(int i = 0; i < argc; ++i)
{
if(!strcmp(argv[i], "--help"))
{
print_help();
exit(EXIT_SUCCESS);
};
if(!strcmp(argv[i], "-v"))
{
print_version_short();
exit(EXIT_SUCCESS);
};
// Print out the compiler's version
if(!strcmp(argv[i], "--version"))
{
print_version();
exit(EXIT_SUCCESS);
};
// Only preprocess the code, without compiling it
if(!strcmp(argv[i], "-E"))
{
skip_compiling = 1;
continue;
};
// Only compile the code, without assembling it
if(!strcmp(argv[i], "-S"))
{
skip_assembling = 1;
continue;
};
// Only compile the code, without linking it
if(!strcmp(argv[i], "-c"))
{
skip_linking = 1;
continue;
};
// Sets the output name and type
if(!strcmp(argv[i], "-o"))
{
// TODO: check for arg
strncpy(output_name, argv[i + 1], sizeof(output_name) - 1);
continue;
};
};
strcpy(input_name[0], "test.c");
// Open each of the file in a list and compile it
// TODO
gpFile = fopen(input_name[0], "r");
if(!gpFile)
{
fprintf(stderr, "Failed to open the file '%s'!\n", input_name[0]);
exit(EXIT_FAILURE);
};
preprocess();
if(!skip_compiling)
{
compile();
if(!skip_assembling)
{
assemble();
if(!skip_linking)
link();
};
};
fclose(gpFile);
fprintf(stderr, "\nDone!\n");
return EXIT_SUCCESS;
};