-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcc.c
919 lines (808 loc) · 21.8 KB
/
xcc.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/***************************************************
*
* Tokenizer
* Flattens the input into a list of Tokens.
*
****************************************************/
// token type
typedef enum {
TK_RESERVED, // symbol
TK_NUM, // integer token
TK_IDENT, // Identifiers
TK_EOF, // token indicating EO input
} TokenKind;
typedef struct Token Token;
/** struct of a single node of the linked list of tokens **/
// token type
struct Token {
TokenKind kind; // token type
Token *next; // next input token
int val; // If kind is TK_NUM, its number
char *str; // token string
int len;
};
// Input program in a string for error message display
char *user_input;
// Token currently focusedン, i.e. current pointer.
Token *token;
// function for reporting errors
// takes the same arguments as printf
void error(char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
// Reports error at location and exits.
void error_at(char *loc, char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
int pos = loc - user_input;
fprintf(stderr, "%s\n", user_input);
fprintf(stderr, "%*s", pos, ""); // print pos spaces.
fprintf(stderr, "^ ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
exit(1);
}
// If the next token is the expected symbol, read one token forward
// returns true. Otherwise, it returns false.
bool consume(char *op) {
if (token->kind != TK_RESERVED || strlen(op) != token->len ||
memcmp(token->str, op, token->len))
return false;
token = token->next;
return true;
}
// Expect Identifier and move forward
Token *consume_ident() {
if (token->kind != TK_IDENT)
return NULL;
Token *t = token;
token = token->next;
return t;
}
// If the next token is the expected symbol, read one token.
// otherwise report an error.
void expect(char *op) {
if (token->kind != TK_RESERVED || strlen(op) != token->len ||
memcmp(token->str, op, token->len))
error_at(token->str, "expected '%c' ", op);
token = token->next;
}
// If the next token is a number, read one token and return that number.
// otherwise report an error.
int expect_number() {
if (token->kind != TK_NUM)
error_at(token->str, "expected a number");
int val = token->val;
token = token->next;
return val;
}
// Ensure that the current token is TK_IDENT.
char *expect_ident(void) {
if (token->kind != TK_IDENT)
error_at(token->str, "expected an identifier");
char *s = strndup(token->str, token->len);
token = token->next;
return s;
}
bool at_eof() { return token->kind == TK_EOF; }
/*****
* create a new node (token) and connect to cur,
* i.e. rest of the linked list
*****/
Token *new_token(TokenKind kind, Token *cur, char *str, int len) {
Token *tok = calloc(1, sizeof(Token));
tok->kind = kind;
tok->str = str;
cur->next = tok;
tok->len = len;
return tok;
}
bool startswith(char *p, char *q) { return memcmp(p, q, strlen(q)) == 0; }
static bool is_alpha(char c) {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_';
}
static bool is_alnum(char c) { return is_alpha(c) || ('0' <= c && c <= '9'); }
/******
* tokenize the user input and return it, basically creation of a liked list
******/
char *starts_with_reserved(char *p) {
// Keyword
static char *kw[] = {"return", "if", "else", "while", "for"};
for (int i = 0; i < sizeof(kw) / sizeof(*kw); i++) {
int len = strlen(kw[i]);
if (startswith(p, kw[i]) && !is_alnum(p[len]))
return kw[i];
}
// Multi-letter punctuator
static char *ops[] = {"==", "!=", "<=", ">="};
for (int i = 0; i < sizeof(ops) / sizeof(*ops); i++)
if (startswith(p, ops[i]))
return ops[i];
return NULL;
}
Token *tokenize() {
char *p = user_input;
Token head;
head.next = NULL;
Token *cur = &head;
while (*p) {
// skip whitespaceプ
if (isspace(*p)) {
p++;
continue;
}
// Keywords or multi-letter punctuators
char *keyw = starts_with_reserved(p);
if (keyw) {
int len = strlen(keyw);
cur = new_token(TK_RESERVED, cur, p, len);
p += len;
continue;
}
// Identifier
if (is_alpha(*p)) {
char *q = p++;
while (is_alnum(*p))
p++;
cur = new_token(TK_IDENT, cur, q, p - q);
continue;
}
// Single-letter punctuator
if (ispunct(*p)) {
cur = new_token(TK_RESERVED, cur, p++,
1); // create a new token (node), join to the current.
continue;
}
if (isdigit(*p)) {
cur = new_token(TK_NUM, cur, p, 0);
char *q = p;
cur->val = strtol(p, &p, 10);
cur->len = p - q;
continue;
}
error_at(p, "invalid token");
}
new_token(TK_EOF, cur, p, 0);
return head.next;
}
/**************************************************************
*
* Parser - recursive descent parser
*
* Current Grammar:
* program = stmt*
* stmt = "return" expr ";" | expr ";"
* | "if" "(" expr ")" stmt ("else" stmt)?
* | "while" "(" expr ")" stmt
* | "for" "(" expr? ";" expr ";" expr ";" ")" stmt
* | "{" stmt* "}"
* | expr ";"
* expr = assign
* assign = equality ("=" assign)?
* equality = relational ("==" relational | "!=" relational)*
* relational = add ("<" add | "<=" add | ">" add | ">=" add)*
* add = mul ("+" mul | "-" mul)*
* mul = unary ("*" unary | "/" unary)*
* unary = ("+" | "-")? primary
* primary = num | ident | "(" expr ")"
*
***************************************************************/
// Local variable
typedef struct Var Var;
struct Var {
Var *next;
char *name; // Variable name
int offset; // Offset from RBP
};
// Node type of Abstract syntax tree
typedef enum {
ND_ADD,
ND_SUB,
ND_MUL,
ND_DIV,
ND_NUM,
ND_ASSIGN, // =
ND_EQ, // ==
ND_NE, // !=
ND_LT, // <
ND_LE, // <=
ND_VAR, // variable
ND_IF, // if statement
ND_WHILE, // while statement
ND_BLOCK, // {}
ND_FUNCALL, // Function call
ND_FOR, // for statement
ND_EXPR_STMT, // expression statement
ND_RET, // return statement
} NodeKind;
typedef struct Node Node;
// single node type if the abstract syntax tree
struct Node {
NodeKind kind; // node type
Node *next; // Next node (nodes are seperated by ';')
Node *lhs; // pointer to left
Node *rhs; // pointer to right
// "if" or "while" or "for"
Node *cond;
Node *then;
Node *els;
Node *init; // initial value
Node *inc; // increament value
// Block of code, {}
Node *body;
// function name
char* funcname;
Node* args;
Var *var; // if kind == ND_VAR
long val; // only used when kind is ND_NUM
};
typedef struct Function Function;
struct Function {
Function *next;
char *name;
Node *node;
Var *locals;
int stack_size;
};
// All local variable instances created during parsing are
// accumulated to this list.
Var *locals;
// Find a local variable by name.
static Var *find_var(Token *tok) {
for (Var *var = locals; var; var = var->next)
if (strlen(var->name) == tok->len && !strncmp(tok->str, var->name, tok->len))
return var;
return NULL;
}
/** create a new node (token) **/
Node *new_node(NodeKind kind, Node *lhs, Node *rhs) {
Node *node = calloc(1, sizeof(Node));
node->kind = kind;
node->lhs = lhs;
node->rhs = rhs;
return node;
}
// if kind is Variable
Node *new_var_node(Var *var) {
Node *node = new_node(ND_VAR, NULL, NULL);
node->var = var;
return node;
}
// if kind is num.
Node *new_node_num(int val) {
Node *node = calloc(1, sizeof(Node));
node->kind = ND_NUM;
node->val = val;
return node;
}
Var *new_lvar(char *name) {
Var *var = calloc(1, sizeof(Var));
var->next = locals;
var->name = name;
locals = var; //var added to the list of locals
return var;
}
Function *program();
Function *function();
Node *stmt();
Node *expr();
Node *assign();
Node *equality();
Node *mul();
Node *primary();
Node *unary();
Node *relational();
Node *add();
// program = function*
Function *program() {
Function head = {};
Function *cur = &head;
while(!at_eof()) {
cur->next = function();
cur = cur->next;
}
return head.next;
}
// function = ident "(" ")" "{" stmt* "}"
Function *function(){
locals = NULL;
char *name = expect_ident();
expect("(");
expect(")");
expect("{");
Node head = {};
Node *cur = &head;
while (!consume("}")) {
cur->next = stmt();
cur = cur->next;
}
Function *fn = calloc(1, sizeof(Function));
fn->name = name;
fn->node = head.next;
fn->locals = locals;
return fn;
// Comment for some old code;
// now the pointer prog, has all the local variables and points to the head of the list of
// nodes in the program, nodes are all the statements seperated by ';'
// so we call codegen giving it prog, codegen now iterates at all the nodes of the prog, i.e. all the
// statements seperated by ;
}
Node *read_expr_stmt(void) {
return new_node(ND_EXPR_STMT, expr(), NULL);
}
/*********************************************
* ND_RET __ ___ND_EXPR_STMT
* | | |
* | | |
* stmt = "return" expr ";" | expr ";"
**********************************************/
// stmt = "return" expr ";"
// | "if" "(" expr ")" stmt ("else" stmt)?
// | "while" "(" expr ")" stmt
// | "for" "(" expr? ";" expr ";" expr ";" ")" stmt
// | "{" stmt* "}"
// | expr ";"
Node *stmt() {
if (consume("return")) {
Node *node = new_node(ND_RET, expr(), NULL);
expect(";");
return node;
}
if (consume("if")) {
Node *node = new_node(ND_IF, NULL, NULL);
expect("(");
node->cond = expr();
expect(")");
node->then = stmt(); // Note that for `then` stmt() is called
// while for `cond` expr() is called.
if (consume("else"))
node->els = stmt();
return node;
}
if(consume("while")) {
Node *node = new_node(ND_WHILE, NULL, NULL);
expect("(");
node->cond = expr();
expect(")");
node->then = stmt();
return node;
}
if(consume("for")){
Node* node = new_node(ND_FOR, NULL, NULL);
expect("(");
if(!consume(";")) {
node->init = read_expr_stmt();
expect(";");
}
if(!consume(";")){
node->cond = expr();
expect(";");
}
if(!consume(")")){
node->inc = read_expr_stmt();
expect(")");
}
node->then = stmt();
return node;
}
if (consume("{")) {
Node head = {};
Node* cur = &head;
while(!consume("}")) {
cur->next = stmt();
cur = cur->next;
}
Node* node = new_node(ND_BLOCK, NULL, NULL);
node->body = head.next;
return node;
}
Node *node = read_expr_stmt();
expect(";");
return node;
}
// expr = assign
Node *expr() { return assign(); }
// assign = equality ("=" assign)?
Node *assign() {
Node *node = equality();
if (consume("="))
node = new_node(ND_ASSIGN, node, assign());
return node;
}
// equality = relational ("==" relational | "!=" relational)*
Node *equality() {
Node *node = relational();
for (;;) {
if (consume("=="))
node = new_node(ND_EQ, node, relational());
else if (consume("!="))
node = new_node(ND_NE, node, relational());
else
return node;
}
}
// relational = add ("<" add | "<=" add | ">" add | ">=" add)*
Node *relational() {
Node *node = add();
for (;;) {
if (consume("<"))
node = new_node(ND_LT, node, add());
else if (consume("<="))
node = new_node(ND_LE, node, add());
else if (consume(">"))
node = new_node(ND_LT, add(), node);
else if (consume(">="))
node = new_node(ND_LE, add(), node);
else
return node;
}
}
// add = mul ("+" mul | "-" mul)*
Node *add() {
Node *node = mul();
for (;;) {
if (consume("+")) {
node = new_node(ND_ADD, node, mul());
} else if (consume("-")) {
node = new_node(ND_SUB, node, mul());
} else {
return node;
}
}
}
// with unary operations : mul = unary ("*" unary | "/" unary)*
Node *mul() {
Node *node = unary();
for (;;) {
if (consume("*")) {
node = new_node(ND_MUL, node, unary());
} else if (consume("/")) {
node = new_node(ND_DIV, node, unary());
} else {
return node;
}
}
}
// unary = ("+" | "-")? primary
Node *unary() {
if (consume("+")) {
return primary();
}
if (consume("-")) {
return new_node(ND_SUB, new_node_num(0), primary());
}
return primary();
}
// func-args = "(" (assign ("," assign)*)? ")"
Node* func_args(){
if(consume(")"))
return NULL;
Node *head = assign();
Node *cur = head;
while(consume(",")){
cur->next = assign();
cur = cur->next;
}
expect(")");
return head;
}
// primary = "(" expr ")" | ident func-args? | num
Node *primary() {
// if next token is "(" it should be a "(" expr ")"
if (consume("(")) {
Node *node = expr();
expect(")");
return node;
}
// It could be a Identifier, accept that
Token *tok = consume_ident();
if (tok) {
// Function call
if(consume("(")){
Node* node = new_node(ND_FUNCALL, NULL, NULL);
node->funcname = strndup(tok->str, tok->len);
node->args = func_args();
return node;
}
// Variable
// If the variable had previously appeared, use offset.
// For a new variable, create a new lvar, set a new offset and use that offset.
Var *var = find_var(tok);
if (!var)
var = new_lvar(strndup(tok->str, tok->len));
// The strndup() function returns a pointer to a new string which is a
// duplicate of the string s but only copies at most n bytes
// now that it is ensured that offset is set, create a new node for the variable.
return new_var_node(var);
}
// except number
return new_node_num(expect_number());
}
/*************************************************************
*
* Asm Code Generation
*
* traversing the Abstract Syntax Tree to emit assembly.
*
**************************************************************/
int labelseq = 1;
char *argreg[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
char *funcname;
// Pushes the given node's address to the stack.
void gen_addr(Node *node) {
if (node->kind == ND_VAR) {
printf(" lea rax, [rbp-%d]\n", node->var->offset);
printf(" push rax\n");
return;
}
error("not an lvalue"); // not an object that occupies memory, i.e. not a
// variable for example.
}
void load(void) {
// update rax to value of the address it contains. more precisely,
// replace the value at the top of the stack with the value it points to,
// and set rax to the same value too
printf(" pop rax\n");
printf(" mov rax, [rax]\n"); // square bracket used for dereferencing.
printf(" push rax\n");
}
void store(void) {
printf(" pop rdi\n"); // value to store is in rdi.
printf(" pop rax\n");
printf(" mov [rax], rdi\n");
printf(" push rdi\n");
}
void gen(Node *node) {
switch (node->kind) {
case ND_NUM:
printf(" push %ld\n", node->val);
return;
case ND_EXPR_STMT:
gen(node->lhs);
printf(" add rsp, 8\n");
return;
case ND_VAR:
gen_addr(node);
load();
return;
case ND_ASSIGN:
gen_addr(node->lhs);
gen(node->rhs);
store();
return;
case ND_IF: {
int seq = labelseq++;
if(node->els){
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.elseLABEL.%d\n", seq);
gen(node->then);
printf(" jmp .L.endLABEL.%d\n", seq);
printf(".L.elseLABEL.%d:\n", seq);
gen(node->els);
printf(".L.endLABEL.%d:\n", seq);
} else{
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.endLABEL.%d\n", seq);
gen(node->then);
printf(".L.endLABEL.%d:\n", seq);
}
return;
}
case ND_WHILE: {
int seq = labelseq++;
printf(".L.beginLOOP.%d:\n", seq);
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.endLOOP.%d\n", seq);
gen(node->then);
printf(" jmp .L.beginLOOP.%d\n", seq);
printf(".L.endLOOP.%d:\n", seq);
return;
}
case ND_FOR: {
int seq = labelseq++;
if(node->init)
gen(node->init);
printf(".L.beginLOOP.%d:\n", seq);
if(node->cond){
gen(node->cond);
printf(" pop rax\n");
printf(" cmp rax, 0\n");
printf(" je .L.endLOOP.%d\n", seq);
}
gen(node->then);
if(node->inc)
gen(node->inc);
printf(" jmp .L.beginLOOP.%d\n", seq);
printf(".L.endLOOP.%d:\n", seq);
return;
}
case ND_BLOCK:
for (Node *n = node->body; n; n=n->next){
gen(n);
}
return;
case ND_FUNCALL: {
int nargs = 0;
for (Node* arg = node->args;arg;arg = arg=arg->next) {
gen(arg);
nargs++;
}
for (int i = nargs - 1; i >= 0; i--){
printf(" pop %s\n", argreg[i]);
}
// We need to align RSP to a 16 byte boundary before
// calling a function because it is an ABI requirement.
// RAX is set to 0 for variadic function.
int seq = labelseq++;
printf(" mov rax, rsp\n");
printf(" and rax, 15\n");
printf(" jnz .L.call.%d\n", seq);
printf(" mov rax, 0\n");
printf(" call %s\n", node->funcname);
printf(" jmp .L.end.%d\n", seq);
printf(".L.call.%d:\n", seq);
printf(" sub rsp, 8\n");
printf(" mov rax, 0\n");
printf(" call %s\n", node->funcname);
printf(" add rsp, 8\n");
printf(".L.end.%d:\n", seq);
printf(" push rax\n");
return;
}
case ND_RET:
gen(node->lhs);
printf(" pop rax\n");
printf(" jmp .L.return.%s\n", funcname);
return;
}
gen(node->lhs);
gen(node->rhs);
printf(" pop rdi\n");
printf(" pop rax\n");
switch (node->kind) {
case ND_ADD:
printf(" add rax, rdi\n");
break;
case ND_SUB:
printf(" sub rax, rdi\n");
break;
case ND_MUL:
printf(" imul rax, rdi\n");
break;
case ND_DIV:
printf(" cqo\n");
printf(" idiv rdi\n");
break;
case ND_EQ:
printf(" cmp rax, rdi\n");
printf(" sete al\n");
printf(" movzb rax, al\n");
break;
case ND_NE:
printf(" cmp rax, rdi\n");
printf(" setne al\n");
printf(" movzb rax, al\n");
break;
case ND_LT:
printf(" cmp rax, rdi\n");
printf(" setl al\n");
printf(" movzb rax, al\n");
break;
case ND_LE:
printf(" cmp rax, rdi\n");
printf(" setle al\n");
printf(" movzb rax, al\n");
break;
}
printf(" push rax\n");
}
/**
Debugging functions.
**/
#define max(a, b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
const char* kindNames[] = {"ADD",
"SUB",
"MUL",
"DIV",
"NUM",
"ASSIGN",
"EQ",
"NE",
"LT",
"LE",
"VAR",
"IF",
"EXPR_STMT",
"RET"
};
int height(Node* root){
if(!root)
return 0;
int h = max(height(root->lhs), height(root->rhs));
return h+1;
}
void printGivenLevel(struct Node* root, int level) {
if (root == NULL)
return;
if (level == 1)
fprintf(stdout, "%s ", kindNames[root->kind]);
else if (level > 1) {
printGivenLevel(root->lhs, level-1);
printGivenLevel(root->rhs, level-1);
}
}
void printLevelOrder(struct Node* root)
{
int h = height(root);
for (int i=1; i<=h; i++) {
printGivenLevel(root, i);
fprintf(stdout, "%s", " \n");
}
}
void debug(Node* root){
printLevelOrder(root);
}
/*******************************/
int main(int argc, char **argv) {
if (argc != 2) {
error("The number of arguments is incorrect ");
return 1;
}
// tokenize and parseuser_inputmain.c
user_input = argv[1];
token = tokenize(); // token pointer now points to the `head` returned by
// tokenizer.
//Node *node = program();
Function *prog = program();
for (Function *fn = prog; fn; fn = fn->next) {
// Assign offsets to local variables.
int offset = 0;
for (Var *var = prog->locals; var; var = var->next) {
offset += 8;
var->offset = offset;
}
fn->stack_size = offset;
}
//for (Node *node = prog->node; node; node = node->next){
// debug(node);
//}
// print the first half of the assembly
printf(".intel_syntax noprefix\n");
for (Function *fn = prog; fn; fn = fn->next) {
printf(".global %s\n", fn->name);
printf("%s:\n", fn->name);
funcname = fn->name;
// Prologue
printf(" push rbp\n");
printf(" mov rbp, rsp\n");
printf(" sub rsp, %d\n", fn->stack_size);
// Traverse AST to generate assembly
for (Node *node = fn->node; node; node = node->next){
gen(node);
}
// Epilogue
printf(".L.return.%s:\n", funcname);
printf(" mov rsp, rbp\n");
printf(" pop rbp\n");
printf(" ret\n");
}
return 0;
}