-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcgen.c
executable file
·266 lines (236 loc) · 5.65 KB
/
cgen.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
#include "cgen.h"
#include "ast.h"
#include "list.h"
#include "utils.h"
#include "type.h"
#include <stdio.h>
static void codegen_(AST*);
static void codegen_bexpr(AST*);
static void codegen_assign(AST*);
static void codegen_array_access(AST*);
static void codegen_variable(AST*);
static void codegen_ret(AST*);
static void codegen_while(AST*);
static void codegen_if_then(AST*);
static void codegen_compound(AST*);
static void codegen_param(AST*);
static void codegen_function(AST*);
static void codegen_var_decl(AST*);
void codegen(list* program)
{
for (struct node* tmp = program->head; tmp != NULL; tmp = tmp->next)
codegen_(tmp->data);
}
static void codegen_(AST* node)
{
switch (node->kind_)
{
case K_NUM:
{ char buf[15];
sprintf(buf,"%d", node->d.val_);
emit3("mov", "eax", buf);
} break;
// FALL THROUGH
case BE_LT:
case BE_LTE:
case BE_GT:
case BE_GTE:
case BE_EQ:
case BE_NEQ:
case BE_ADD:
case BE_SUB:
case BE_MUL:
case BE_DIV:
codegen_bexpr(node);
break;
case K_CALL:
codegen(node->d.call.args_);
break;
case E_ASSIGN:
codegen_assign(node);
break;
case ARRAY_ACCESS:
codegen_array_access(node);
break;
case VAR_NAME:
codegen_variable(node);
break;
case RETURN:
codegen_ret(node);
break;
case WHILE:
codegen_while(node);
break;
case IF_THEN:
codegen_if_then(node);
break;
case COMPOUND:
codegen_compound(node);
break;
case PARAM:
codegen_param(node);
break;
case FUNCTION:
codegen_function(node);
break;
case VAR_DECL:
codegen_var_decl(node);
break;
default:
fprintf(stderr,"unknown node kind\n");
break;
}
}
static void codegen_bexpr(AST* node)
{
codegen_(node->d.be.lhs_);
emit2("push","eax");
codegen_(node->d.be.rhs_);
emit3("mov","ebx","eax");
emit2("pop","eax");
switch(node->kind_)
{
case BE_LT:
emit3("cmp","eax","ebx");
emit2("setl","esi");
break;
case BE_LTE:
emit3("cmp","eax","ebx");
emit2("setle","esi");
break;
case BE_GT:
emit3("cmp","eax","ebx");
emit2("setg","esi");
break;
case BE_GTE:
emit3("cmp","eax","ebx");
emit2("setge","esi");
break;
case BE_EQ:
emit3("cmp","eax","ebx");
emit2("sete","esi");
break;
case BE_NEQ:
emit3("cmp","eax","ebx");
emit2("setne","esi");
break;
case BE_ADD:
emit3("add","eax","ebx");
break;
case BE_SUB:
emit3("sub","eax","ebx");
break;
case BE_MUL:
emit3("imul","eax","ebx");
break;
case BE_DIV:
emit3("idiv","eax","ebx");
break;
default:
fprintf(stderr,"unknown binary operator\n");
break;
}
}
static void codegen_assign(AST* node)
{
codegen_(node->d.assign.expr_);
codegen_(node->d.assign.to_);
emit1("assign");
}
static void codegen_array_access(AST* node)
{
codegen_(node->d.access.expr_);
emit3("imul","eax","4");
emit1("access");
}
//optimize this
static void codegen_variable(AST* node)
{
}
static void codegen_ret(AST* node)
{
if(node->d.ret.expr_ != NULL)
{
codegen_(node->d.ret.expr_);
}
emit2("jmp",".exit");
}
static void codegen_while(AST* node)
{
char* begin = new_label();
char* end = new_label();
emit_label(begin);
codegen_(node->d.while_loop.expr_);
emit2("jz",end);
codegen_(node->d.while_loop.stmt_);
emit2("jmp", begin);
emit_label(end);
}
static void codegen_if_then(AST* node)
{
if(node->d.if_then.else_stmt_ == NULL)
{
char* end = new_label();
codegen_(node->d.if_then.expr_);
emit2("jz",end);
codegen_(node->d.if_then.if_stmt_);
emit_label(end);
}
else
{
char* not_if = new_label();
char* end_if = new_label();
codegen_(node->d.if_then.expr_);
emit2("jz",not_if);
codegen_(node->d.if_then.if_stmt_);
emit2("jmp",end_if);
emit_label(not_if);
codegen_(node->d.if_then.else_stmt_);
emit_label(end_if);
}
}
static void codegen_compound(AST* node)
{
codegen(node->d.compound.decls_);
codegen(node->d.compound.stmts_);
}
//optimize this
static void codegen_param(AST* node)
{
emit3("mov","esi","edi");
emit2("push","esi");
}
static void codegen_function(AST* node)
{
emit_label(node->d.function.name_);
//prologue
emit2("push", "ebp");
emit3("mov", "ebp", "esp");
emit3("sub", "esp", "0");
emit2("push", "ebx");
emit2("push", "esi");
emit2("push", "edi");
codegen(node->d.function.params_);
codegen_(node->d.function.body_);
//epilogue
emit_label("exit");
emit2("pop","edi");
emit2("pop","esi");
emit2("pop","ebx");
emit3("add", "esp", "0");
emit3("mov", "esp", "ebp");
emit2("pop", "ebp");
emit1("ret");
}
//optimize this
static void codegen_var_decl(AST* node)
{
if(node->d.var_decl.type_spec_ != TYPE_INT)
{
emit3("sub","esp","4");
}
else
{
emit3("sub","esp","0");
}
}