forked from vieiraeduardos/tiny-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgen.c
264 lines (242 loc) · 8.42 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
/****************************************************/
/* File: cgen.c */
/* The code generator implementation */
/* for the TINY compiler */
/* (generates code for the TM machine) */
/* Compiler Construction: Principles and Practice */
/* Kenneth C. Louden */
/****************************************************/
#include "globals.h"
#include "symtab.h"
#include "code.h"
#include "cgen.h"
/* tmpOffset is the memory offset for temps
It is decremented each time a temp is
stored, and incremeted when loaded again
*/
static int tmpOffset = 0;
/* prototype for internal recursive code generator */
static void cGen (TreeNode * tree);
/* Procedure genStmt generates code at a statement node */
static void genStmt( TreeNode * tree)
{ TreeNode * p1, * p2, * p3, *curCase;
int savedLoc1,savedLoc2,currentLoc, jmpToNextLoc, lastPos;
int loc;
switch (tree->kind.stmt) {
case SwitchK :
if (TraceCode) emitComment("-> switch") ;
p1 = tree->child[0] ;
p2 = tree->child[1] ;
//get the variable
cGen(p1);
//generate Cases
cGen(p2);
break; /* switch_k */
case CaseK:
curCase = tree;
if (TraceCode) emitComment("-> ") ;
emitRM("LDA", ac1, 0, ac,"tentando colocar o valor de ac em ac1");
//loop que gera os cases
do{
p1 = curCase->child[0] ;
p2 = curCase->child[1] ;
/* get constant */
cGen(p1);
emitRO("SUB",ac,ac1,ac,"op ==") ;//subtrai AC de AC1
emitRM("JEQ",ac,1,pc,"br if true");//PULA O PROXIMO COMANDO SE O RESULTADO FOR 0
jmpToNextLoc = emitSkip(1);//pula 1 linha pra deixar espaço pro jmp que leva pro proximo case
cGen(p2);//gera os statements
lastPos = emitSkip(0);//salva ultima posição
emitBackup(jmpToNextLoc);//volta pra local do jmp pro proximo case
emitRM("LDA",pc,(lastPos - jmpToNextLoc),pc,"unconditional jmp");//pula pra posição do proximo case
emitRestore();
curCase = curCase->sibling;
}while(curCase!=NULL);
break; /* switch_k */
case IfK :
if (TraceCode) emitComment("-> if") ;
p1 = tree->child[0] ;
p2 = tree->child[1] ;
p3 = tree->child[2] ;
/* generate code for test expression */
cGen(p1);
savedLoc1 = emitSkip(1) ;
emitComment("if: jump to else belongs here");
/* recurse on then part */
cGen(p2);
savedLoc2 = emitSkip(1) ;
emitComment("if: jump to end belongs here");
currentLoc = emitSkip(0) ;
emitBackup(savedLoc1) ;
emitRM_Abs("JEQ",ac,currentLoc,"if: jmp to else");
emitRestore() ;
/* recurse on else part */
cGen(p3);
currentLoc = emitSkip(0) ;
emitBackup(savedLoc2) ;
emitRM_Abs("LDA",pc,currentLoc,"jmp to end") ;
emitRestore() ;
if (TraceCode) emitComment("<- if") ;
break; /* if_k */
case WhileK:
if (TraceCode) emitComment("-> while");
p1 = tree->child[0];
p2 = tree->child[1];
currentLoc = emitSkip(0);
cGen(p1);
savedLoc1 = emitSkip(1);
cGen(p2);
emitRM_Abs("LDA", pc, currentLoc, "while: jump after body comes back here");
currentLoc = emitSkip(0);
emitBackup(savedLoc1);
emitRM_Abs("JEQ", ac, currentLoc, "while: jump back to body");
emitRestore();
if (TraceCode) emitComment("<- while");
break;
case RepeatK:
if (TraceCode) emitComment("-> repeat") ;
p1 = tree->child[0] ;
p2 = tree->child[1] ;
savedLoc1 = emitSkip(0);
emitComment("repeat: jump after body comes back here");
/* generate code for body */
cGen(p1);
/* generate code for test */
cGen(p2);
emitRM_Abs("JEQ",ac,savedLoc1,"repeat: jmp back to body");
if (TraceCode) emitComment("<- repeat") ;
break; /* repeat */
case AssignK:
if (TraceCode) emitComment("-> assign") ;
/* generate code for rhs */
cGen(tree->child[0]);
/* now store value */
loc = st_lookup(tree->attr.name);
emitRM("ST",ac,loc,gp,"assign: store value");
if (TraceCode) emitComment("<- assign") ;
break; /* assign_k */
case ReadK:
emitRO("IN",ac,0,0,"read integer value");
loc = st_lookup(tree->attr.name);
emitRM("ST",ac,loc,gp,"read: store value");
break;
case WriteK:
/* generate code for expression to write */
cGen(tree->child[0]);
/* now output it */
emitRO("OUT",ac,0,0,"write ac");
break;
default:
break;
}
} /* genStmt */
/* Procedure genExp generates code at an expression node */
static void genExp( TreeNode * tree)
{ int loc;
TreeNode * p1, * p2;
switch (tree->kind.exp) {
case ConstK :
if (TraceCode) emitComment("-> Const") ;
/* gen code to load integer constant using LDC */
emitRM("LDC",ac,tree->attr.val,0,"load const");
if (TraceCode) emitComment("<- Const") ;
break; /* ConstK */
case IdK :
if (TraceCode) emitComment("-> Id") ;
loc = st_lookup(tree->attr.name);
emitRM("LD",ac,loc,gp,"load id value");
if (TraceCode) emitComment("<- Id") ;
break; /* IdK */
case OpK :
if (TraceCode) emitComment("-> Op") ;
p1 = tree->child[0];
p2 = tree->child[1];
/* gen code for ac = left arg */
cGen(p1);
/* gen code to push left operand */
emitRM("ST",ac,tmpOffset--,mp,"op: push left");
/* gen code for ac = right operand */
cGen(p2);
/* now load left operand */
emitRM("LD",ac1,++tmpOffset,mp,"op: load left");
switch (tree->attr.op) {
case PLUS :
emitRO("ADD",ac,ac1,ac,"op +");
break;
case MINUS :
emitRO("SUB",ac,ac1,ac,"op -");
break;
case TIMES :
emitRO("MUL",ac,ac1,ac,"op *");
break;
case OVER :
emitRO("DIV",ac,ac1,ac,"op /");
break;
case LT :
emitRO("SUB",ac,ac1,ac,"op <") ;
emitRM("JLT",ac,2,pc,"br if true") ;
emitRM("LDC",ac,0,ac,"false case") ;
emitRM("LDA",pc,1,pc,"unconditional jmp") ;
emitRM("LDC",ac,1,ac,"true case") ;
break;
case EQ :
emitRO("SUB",ac,ac1,ac,"op ==") ;
emitRM("JEQ",ac,2,pc,"br if true");
emitRM("LDC",ac,0,ac,"false case") ;
emitRM("LDA",pc,1,pc,"unconditional jmp") ;
emitRM("LDC",ac,1,ac,"true case") ;
break;
default:
emitComment("BUG: Unknown operator");
break;
} /* case op */
if (TraceCode) emitComment("<- Op") ;
break; /* OpK */
default:
break;
}
} /* genExp */
/* Procedure cGen recursively generates code by
* tree traversal
*/
static void cGen( TreeNode * tree)
{ if (tree != NULL)
{ switch (tree->nodekind) {
case StmtK:
genStmt(tree);
break;
case ExpK:
genExp(tree);
break;
default:
break;
}
cGen(tree->sibling);
}
}
/**********************************************/
/* the primary function of the code generator */
/**********************************************/
/* Procedure codeGen generates code to a code
* file by traversal of the syntax tree. The
* second parameter (codefile) is the file name
* of the code file, and is used to print the
* file name as a comment in the code file
*/
void codeGen(TreeNode * syntaxTree, char * codefile)
{ char * s = malloc(strlen(codefile)+7);
strcpy(s,"File: ");
strcat(s,codefile);
emitComment("TINY Compilation to TM Code");
emitComment(s);
/* generate standard prelude */
emitComment("Standard prelude:");
emitRM("LD",mp,0,ac,"load maxaddress from location 0");
emitRM("ST",ac,0,ac,"clear location 0");
emitComment("End of standard prelude.");
/* generate code for TINY program */
cGen(syntaxTree);
/* finish */
emitComment("End of execution.");
emitRO("HALT",0,0,0,"");
}