-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7cc.c
More file actions
388 lines (321 loc) · 7.95 KB
/
Copy path7cc.c
File metadata and controls
388 lines (321 loc) · 7.95 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
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// global variable
int pos = 0;
//////////////////////////////////////////
// Tokenizer
//////////////////////////////////////////
// vector
typedef struct
{
void **data;
int capacity; // size of buffer
int len; // number of elements
} Vector;
Vector *new_vector()
{
Vector *vec = malloc(sizeof(Vector));
vec->data = malloc(sizeof(void *) * 16);
vec->capacity = 16;
vec->len = 0;
return vec;
}
void vec_push(Vector *vec, void *elem)
{
if (vec->capacity == vec->len)
{
vec->capacity *= 2;
vec->data = realloc(vec->data, sizeof(void *) * vec->capacity);
}
vec->data[vec->len++] = elem;
}
// Values represent type of token
enum
{
TK_NUM = 256, // int
TK_EOF, // End of Input
};
// Type
typedef struct
{
int ty; // Type of token
int val; // Value
char *input; // String of input (for error message)
} Token;
// Token tokens[100];
// Variable-length Vector
Vector *tokens;
// a token used temporary
Token *token_tmp;
// Divide p-strings by each tokens
void tokenize(char *p)
{
int i = 0;
tokens = new_vector();
while (*p)
{
token_tmp = (Token *)malloc(sizeof(Token *));
// Skip white space
if (isspace(*p))
{
p++;
continue;
}
// Operator
if (*p == '+' || *p == '-' || *p == '*' || *p == '/' || *p == '(' || *p == ')')
{
token_tmp->ty = *p;
token_tmp->input = p;
vec_push(tokens, (void *)token_tmp);
//printf("operator: %d %s\n", token_tmp->ty, token_tmp->input);
i++;
p++;
continue;
}
// Integer
if (isdigit(*p))
{
token_tmp->ty = TK_NUM;
token_tmp->input = p;
token_tmp->val = strtol(p, &p, 10);
vec_push(tokens, (void *)token_tmp);
//printf("integer: %d %s %d\n", token_tmp->ty, token_tmp->input, token_tmp->val);
i++;
continue;
}
fprintf(stderr, "Unable to tokenize: %s\n", p);
exit(1);
}
token_tmp = (Token *)malloc(sizeof(Token *));
token_tmp->ty = TK_EOF;
token_tmp->input = p;
vec_push(tokens, (void *)token_tmp);
//printf("EOF: %d %s\n", token_tmp->ty, token_tmp->input);
//printf("length: %d\n", tokens->len);
}
// Report Error
/*void error(int i)
{
fprintf(stderr, "Unexpected Token: %s\n", tokens[i].input);
exit(1);
}*/
//////////////////////////////////////////
// Abstract Syntax Tree
//////////////////////////////////////////
enum
{
ND_NUM = 256, // Type of Int Node
};
typedef struct Node
{
int ty; // operator or ND_NUM
struct Node *lhs; // left-hand side
struct Node *rhs; // right-hand side
int val; // value when ty is ND_NUM
} Node;
// Create Node (2-term operator)
Node *new_node(int ty, Node *lhs, Node *rhs)
{
Node *node = malloc(sizeof(Node));
node->ty = ty;
node->lhs = lhs;
node->rhs = rhs;
return node;
}
// Create Node (number)
Node *new_node_num(int val)
{
Node *node = malloc(sizeof(Node));
node->ty = ND_NUM;
node->val = val;
return node;
}
Token *token_pop;
// Check whether a next token is an expected type
int consume(int ty)
{
//printf("compare type: %d\n", ty);
token_pop = (Token *)malloc(sizeof(Token *));
token_pop = (Token *)tokens->data[pos];
//printf("pos %d: (ty, input) = (%d, %s)\n", pos, token_pop->ty, token_pop->input);
if (token_pop->ty != ty)
return 0;
pos++;
return 1;
}
// parser
Node *add();
Node *mul();
Node *term();
Node *term()
{
//printf(" term()\n");
if (consume('('))
{
Node *node = add();
if (!consume(')'))
{
token_pop = (Token *)tokens->data[pos];
fprintf(stderr, "No mutch parenthesis: %s", token_pop->input);
}
return node;
}
//printf("pos: %d\n", pos);
token_pop = (Token *)tokens->data[pos];
//printf("type: %d\n", token_pop->ty);
if (token_pop->ty == TK_NUM)
{
pos++;
return new_node_num(token_pop->val);
}
fprintf(stderr, "This token is neither Number nor Parenthesis: %s", token_pop->input);
}
Node *mul()
{
//printf(" mul()\n");
Node *node = term();
for( ; ; )
{
if (consume('*'))
{
node = new_node('*', node, term());
}
else if (consume('/'))
{
node = new_node('/', node, term());
}
else
{
return node;
}
}
}
Node *add()
{
//printf(" add()\n");
Node *node = mul();
for( ; ; )
{
if (consume('+'))
{
node = new_node('+', node, mul());
}
else if (consume('-'))
{
node = new_node('-', node, mul());
}
else
{
return node;
}
}
}
//////////////////////////////////////////
// Stack Compiler
//////////////////////////////////////////
void gen(Node *node)
{
if (node->ty == ND_NUM)
{
printf(" push %d\n", node->val);
return;
}
gen(node->lhs);
gen(node->rhs);
printf(" pop rdi\n");
printf(" pop rax\n");
switch (node->ty)
{
case '+':
printf(" add rax, rdi\n");
break;
case '-':
printf(" sub rax, rdi\n");
break;
case '*':
printf(" mul rdi\n");
break;
case '/':
printf(" mov rdx, 0\n");
printf(" div rdi\n");
}
printf(" push rax\n");
}
//////////////////////////////////////////
// Test
//////////////////////////////////////////
int expect(int line, int expected, int actual)
{
if (expected == actual)
return;
fprintf(stderr, "%d: %d expected, but got %d\n", line, expected, actual);
exit(1);
}
void runtest()
{
//printf("Test: Create Vector-unit\n");
Vector *vec = new_vector();
expect(__LINE__, 0, vec->len);
for(int i = 0; i < 100; i++)
vec_push(vec, (void *)i);
expect(__LINE__, 100, vec->len);
expect(__LINE__, 0, (int)vec->data[0]);
expect(__LINE__, 50, (int)vec->data[50]);
expect(__LINE__, 99, (int)vec->data[99]);
//printf("Done\n");
//printf("Test: Re-cast token\n");
Vector *vec_token = new_vector();
//printf("Vector vec_token created\n");
char *p = "0";
//printf("char p created\n");
Token *t;
t = (Token *)malloc(sizeof(Token *));
t->ty = TK_NUM; //printf("State: t->ty = p; done\n");
t->input = p; //printf("State: t->input = p; done\n");
t->val = strtol(p, &p, 10); //printf("State: t->val = strtol(p, &p, 10); done\n");
vec_push(vec_token, (void *)t);
//printf("push token done\n");
Token *t_pop;
t_pop = (Token *)vec_token->data[0];
//printf("t_pop->input: %s\n", t_pop->input);
//printf("t_pop->ty: %d\n", t_pop->ty);
//printf("t_pop->val: %d\n", t_pop->val);
//printf("Done\n");
//printf("OK\n");
}
//////////////////////////////////////////
// Main Function
//////////////////////////////////////////
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Invalid number of arguments\n");
return 1;
}
if (!strcmp(argv[1], "-test"))
{
printf("run test\n");
runtest();
return 0;
}
// tokenize and parse
//printf("Start: Tokenizer\n");
tokenize(argv[1]);
//printf("Done: Tokenizer\n");
//printf("Start: Parser\n");
Node *node = add();
//printf("Done: Parser\n");
// header
printf(".intel_syntax noprefix\n");
printf(".global main\n");
printf("main:\n");
// Code generation
gen(node);
// Pop answer from stuck-top
printf(" pop rax\n");
printf(" ret\n");
return 0;
}