-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcompilium.c
295 lines (261 loc) · 7.96 KB
/
compilium.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
#include "compilium.h"
const char *symbol_prefix;
const char *include_path;
bool is_preprocess_only = false;
_Noreturn void Error(const char *fmt, ...) {
fflush(stdout);
fprintf(stderr, "Error: ");
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
_Noreturn void __assert(const char *expr_str, const char *file, int line) {
Error("Assertion failed: %s at %s:%d\n", expr_str, file, line);
}
void TestList(void);
void TestType(void);
static struct Node *ParseCompilerArgs(int argc, char **argv) {
// returns replacement_list: ASTList which contains macro replacement
struct Node *replacement_list = AllocList();
symbol_prefix = "_";
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--target-os") == 0) {
i++;
if (strcmp(argv[i], "Darwin") == 0) {
symbol_prefix = "_";
// Define __APPLE__ macro
PushKeyValueToList(replacement_list, "__APPLE__",
CreateMacroReplacement(NULL, NULL));
} else if (strcmp(argv[i], "Linux") == 0) {
symbol_prefix = "";
} else {
Error("Unknown os type %s", argv[i]);
}
} else if (strcmp(argv[i], "-I") == 0) {
i++;
include_path = argv[i];
assert(include_path);
if (include_path[strlen(include_path) - 1] != '/') {
Error("Include path (-I <path>) should be ended with '/'");
}
fprintf(stderr, "Include path: %s\n", include_path);
} else if (strcmp(argv[i], "--run-unittest=List") == 0) {
TestList();
} else if (strcmp(argv[i], "--run-unittest=Type") == 0) {
TestType();
} else if (strcmp(argv[i], "-E") == 0) {
is_preprocess_only = true;
} else {
Error("Unknown argument: %s", argv[i]);
}
}
return replacement_list;
}
void PrintTokenLine(struct Node *t) {
assert(t);
const char *line_begin = t->begin;
while (t->src_str < line_begin) {
if (line_begin[-1] == '\n') break;
line_begin--;
}
fprintf(stderr, "Line %d:\n", t->line);
for (const char *p = line_begin; *p && *p != '\n'; p++) {
fputc(*p <= ' ' ? ' ' : *p, stderr);
}
fputc('\n', stderr);
const char *p;
for (p = line_begin; p < t->begin; p++) {
fputc(' ', stderr);
}
for (int i = 0; i < t->length; i++) {
fputc('^', stderr);
p++;
}
for (; *p && *p != '\n'; p++) {
fputc(' ', stderr);
}
fputc('\n', stderr);
}
_Noreturn void ErrorWithToken(struct Node *t, const char *fmt, ...) {
PrintTokenLine(t);
fprintf(stderr, "Error: ");
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
exit(EXIT_FAILURE);
}
struct Node *AllocList() {
return AllocNode(kASTList);
}
void ExpandListSizeIfNeeded(struct Node *list) {
if (list->size < list->capacity) return;
list->capacity = (list->capacity + 1) * 2;
list->nodes = realloc(list->nodes, sizeof(struct Node *) * list->capacity);
assert(list->nodes);
assert(list->size < list->capacity);
}
void PushToList(struct Node *list, struct Node *node) {
ExpandListSizeIfNeeded(list);
list->nodes[list->size++] = node;
}
void PushKeyValueToList(struct Node *list, const char *key,
struct Node *value) {
assert(key && value);
ExpandListSizeIfNeeded(list);
list->nodes[list->size++] = CreateASTKeyValue(key, value);
}
int GetSizeOfList(struct Node *list) {
assert(list && list->type == kASTList);
return list->size;
}
struct Node *GetNodeAt(struct Node *list, int index) {
assert(list && list->type == kASTList);
assert(0 <= index && index < list->size);
return list->nodes[index];
}
struct Node *GetNodeByTokenKey(struct Node *list, struct Node *key) {
assert(list && list->type == kASTList);
for (int i = 0; i < list->size; i++) {
struct Node *n = list->nodes[i];
if (n->type != kASTKeyValue) continue;
if (IsEqualTokenWithCStr(key, n->key)) return n->value;
}
return NULL;
}
struct Node *GetNodeByKey(struct Node *list, const char *key) {
assert(list && list->type == kASTList);
for (int i = 0; i < list->size; i++) {
struct Node *n = list->nodes[i];
if (n->type != kASTKeyValue) continue;
if (strcmp(n->key, key) == 0) return n->value;
}
return NULL;
}
void TestList() {
fprintf(stderr, "Testing List...");
struct Node *list = AllocList();
struct Node *item1 = AllocNode(kNodeNone);
struct Node *item2 = AllocNode(kNodeNone);
assert(list);
PushToList(list, item1);
assert(GetSizeOfList(list) == 1);
PushToList(list, item2);
assert(GetSizeOfList(list) == 2);
assert(GetNodeAt(list, 0) == item1);
assert(GetNodeAt(list, 1) == item2);
int base_capacity = list->capacity;
while (GetSizeOfList(list) <= base_capacity) {
PushToList(list, item1);
}
assert(list->capacity > base_capacity);
assert(GetNodeAt(list, 0) == item1);
assert(GetNodeAt(list, 1) == item2);
assert(GetNodeAt(list, GetSizeOfList(list) - 1) == item1);
PushKeyValueToList(list, "item1", item1);
PushKeyValueToList(list, "item2", item2);
assert(GetNodeByKey(list, "item1") == item1);
assert(GetNodeByKey(list, "item2") == item2);
assert(GetNodeByKey(list, "not_existed") == NULL);
fprintf(stderr, "PASS\n");
exit(EXIT_SUCCESS);
}
// System V AMD64 ABI:
// args:
// RDI, RSI, RDX, RCX, R8, R9
// callee-saved(should be kept on return):
// RBX, RBP, R12, R13, R14, R15
// caller-saved(can be destroyed):
// otherwise
// Compilium register plan:
// RAX: reserved for return values
// RCX: reserved for shift ops
// RDX: 3rd parameter, reserved for div/mul ops
// RBX: reserved (callee-saved)
// RSP: reserved for stack pointer
// RBP: reserved for frame pointer
// RSI: 2nd parameter
// RDI: 1st parameter
// R8 : 5th parameter
// R9 : 6th parameter
// R10: scratch
// R11: scratch
// R12: reserved (callee-saved)
// R13: reserved (callee-saved)
// R14: reserved (callee-saved)
// R15: reserved (callee-saved)
const char *reg_names_64[NUM_OF_SCRATCH_REGS + 1] = {
// padding
NULL,
// params
"rdi", "rsi", "r8", "r9",
// scratch
"r10", "r11",
// callee-saved
"r12", "r13", "r14", "r15"};
const char *reg_names_32[NUM_OF_SCRATCH_REGS + 1] = {
// padding
NULL,
// params
"edi", "esi", "r8d", "r9d",
// scratch
"r10d", "r11d",
// callee-saved
"r12d", "r13d", "r14d", "r15d"};
const char *reg_names_8[NUM_OF_SCRATCH_REGS + 1] = {
// padding
NULL,
// params
"dil", "sil", "r8b", "r9b",
// scratch
"r10b", "r11b",
// callee-saved
"r12b", "r13b", "r14b", "r15d"};
const char *param_reg_names_64[NUM_OF_PARAM_REGISTERS] = {"rdi", "rsi", "rdx",
"rcx", "r8", "r9"};
const char *param_reg_names_32[NUM_OF_PARAM_REGISTERS] = {"edi", "esi", "edx",
"ecx", "r8d", "r9d"};
const char *param_reg_names_8[NUM_OF_PARAM_REGISTERS] = {"dl", "sil", "dl",
"cl", "r8b", "r9b"};
#define INITIAL_INPUT_SIZE 8192
const char *ReadFile(FILE *fp) {
int buf_size = INITIAL_INPUT_SIZE;
char *input = malloc(buf_size);
int input_size = 0;
int c;
while ((c = fgetc(fp)) != EOF) {
input[input_size++] = c;
if (input_size == buf_size) {
buf_size <<= 1;
assert((input = realloc(input, buf_size)));
}
}
assert(input_size < buf_size);
input[input_size] = 0;
return input;
}
int main(int argc, char *argv[]) {
struct Node *replacement_list = ParseCompilerArgs(argc, argv);
const char *input = ReadFile(stdin);
struct Node *tokens = Tokenize(input);
fputs("Preprocess begin\n", stderr);
Preprocess(&tokens, replacement_list);
if (is_preprocess_only) {
OutputTokenSequenceAsCSource(tokens);
return 0;
}
fputs("Parse begin\n", stderr);
struct Node *ast = Parse(&tokens);
PrintASTNode(ast);
fputc('\n', stderr);
fputs("Analyze begin\n", stderr);
struct SymbolEntry *ctx = Analyze(ast);
PrintASTNode(ast);
fputc('\n', stderr);
Generate(ast, ctx);
return 0;
}