forked from cop3402/toy1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.c
More file actions
42 lines (39 loc) · 1.16 KB
/
codegen.c
File metadata and controls
42 lines (39 loc) · 1.16 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
#include "codegen.h"
//#include "parser.h"
void gencode_statement_list(T_statement_list statement_list) {
if (statement_list == NULL) {
return;
}
gencode_statement(statement_list->statement);
gencode_statement_list(statement_list->statement_list);
}
void gencode_statement(T_statement statement) {
gencode_expression(statement->expression);
}
void gencode_expression(T_expression expression) {
printf("\n");
printf("\tmovl $%d, %%eax\n", expression->operand1->number);
printf("\tmovl $%d, %%ebx\n", expression->operand2->number);
switch(expression->op->character) {
case '+':
printf("\taddl %%ebx, %%eax\n");
break;
case '-':
printf("\tsubl %%ebx, %%eax\n");
break;
case '*':
printf("\timull %%ebx, %%eax\n");
break;
case '/':
printf("\tcltd\n");
printf("\tidivl %%ebx\n");
break;
default:
assert(false);
break;
}
printf("\tmovl %%eax, %%esi\n");
printf("\tleaq .LC0(%%rip), %%rdi\n");
printf("\tmovl $0, %%eax\n");
printf("\tcall printf@PLT\n");
}