-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.c
262 lines (243 loc) · 8.76 KB
/
compiler.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
#include "compiler.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "SymbolTable.h"
#include "Utilities.h"
#include "registerGenerator.h"
#include "InstructionsEmitter.h"
#include "LabelGenerator.h"
SymbolTable * table = NULL;
int yyerror(const char * );
ASTNode * getNode(){
return (ASTNode * ) malloc(sizeof(ASTNode));
}
ASTNode* CreateNumNode(int num)
{
ASTNode* node = (ASTNode*)malloc(sizeof(ASTNode));
node->type = ASTNODE_NUM;
node->num = num;
return node;
}
ASTNode* CreateIdentNode(char* name)
{
if(table == NULL || lookup(table, name) == 0){
yyerror("Ident not declared");
}
ASTNode* identNode = (ASTNode * ) malloc(sizeof(ASTNode));
identNode->name = name;
identNode->type = ASTNODE_IDENT;
return identNode;
}
ASTNode * CreateLHSNode(char * name){
ASTNode * node = CreateIdentNode(name);
printf("Creating left-hand IDENT node for %s\n", name);
return node;
}
void getOperation(ASTOp op, char operation []){
switch(op){
case ADD_OP:
strcpy(operation, "Addition");
break;
case MULT_OP:
strcpy(operation, "Multiplication");
break;
case SUB_OP:
strcpy(operation, "Subtraction");
break;
case DIV_OP:
strcpy(operation, "Division");
break;
case ASSSIGN_OP:
strcpy(operation, "Assignment");
break;
case OR_OP:
strcpy(operation, "OR");
break;
case AND_OP:
strcpy(operation, "AND");
break;
default:
strcpy(operation, "");
break;
}
}
ASTNode * createBinaryOPNode(ASTOp op, ASTNode * left, ASTNode * right){
ASTNode * node = getNode();
node->type = ASTNODE_ARITH_OP;
node->op = op;
node->left =left;
node->right = right;
return node;
}
ASTNode * CreateArithmeticOpNode(ASTNode * left, ASTNode * right, ASTOp op){
ASTNode * node = createBinaryOPNode(op, left, right);
char operation [100];
getOperation(op, operation);
printf("Creating %s node\n", operation);
return node;
}
ASTNode * CreateAssignmentNode(ASTNode * left, ASTNode * right){
ASTNode * node = getNode();
node->type = ASTNODE_ASSIGN;
node ->left = left;
node->right = right;
char operation[100];
getOperation(ASSSIGN_OP, operation);
printf("Creating %s node\n", operation);
return node;
}
// Take a statement node and a statement list node and connect them together
// to form a bigger statement list node (add the statement to the statement list).
// Return a pointer to the bigger list that resulted from this linking
ASTNode* CreateStatementListNode(ASTNode* st, ASTNode* stList)
{
ASTNode * node = getNode();
node->type = ASTNODE_STMT;
node->left = st;
node->right = stList;
printf("Adding a Statement to a Statement list\n");
return node;
}
void addDeclaration(char * name){
if(table == NULL){
table = createSymbolTable(1229);
}
const int status = insert(table, name, "int");
if(status == -1){
char prefix[] = "Multiple declarations of ";
char * message = calloc(strlen(name) + strlen(prefix) + 4, sizeof(char));
strcpy(message, prefix);
strcat(message, "'");
strcat(message, name);
strcat(message, "'");
yyerror(message);
free(message);
}else
{
printf("Processing declaration of %s\n", name);
}
}
ASTNode * CreateCompareNode(ASTNode* left, ASTOp op, ASTNode * right){
ASTNode * node = getNode();
node->type = ASTNODE_COMPARE;
node->op = op;
node->left = left;
node->right = right;
printf("Creating Compare node\n");
return node;
}
ASTNode * CreateLogicalNode(ASTNode * left, ASTOp op, ASTNode * right){
ASTNode * node = getNode();
node->type = ASTNODE_LOGIC_OP;
node->op = op;
node->left = left;
node->right = right;
char operation[100];
getOperation(op, operation);
printf("Creating %s node\n", operation);
return node;
}
ASTNode * CreateIFNode(ASTNode * left, ASTNode * right){
ASTNode * node = getNode();
node->type = ASTNODE_IF;
node->left = left;
node->right = right;
printf("Creating if Statement node\n");
return node;
}
ASTNode * CreateIfElseNode(ASTNode * left, ASTNode * right, ASTNode * next){
ASTNode * node = getNode();
node->type = ASTNODE_IFELSE;
node->left = left;
ASTNode * ifelseBodyNode = getNode();
ifelseBodyNode->type = ASTNODE_IFELSELOOPBODY;
ifelseBodyNode->left = right;
ifelseBodyNode->right = next;
node->right = ifelseBodyNode;
printf("Creating if-else Statement node\n");
return node;
}
ASTNode * CreateWhileNode(ASTNode* left, ASTNode * right){
ASTNode * node = getNode();
node->type = ASTNODE_WHILE;
node->left = left;
node->right = right;
printf("Creating while loop node\n");
return node;
}
char * generateCode(ASTNode * node, RegisterGenerator* regGen, LabelGenerator * labGen, FILE * output){
char * lreg = NULL;
char * rreg = NULL;
char * reg = NULL;
char * labels []= {"", "", "", ""};
switch(node->type){
case ASTNODE_IDENT:
reg = generate(regGen);
fputs(emitLoadForIdentifier(node->name, table, reg), output);
return reg;
case ASTNODE_NUM:
reg = generate(regGen);
fputs(emitLoadForNum(node->num, reg), output);
return reg;
case ASTNODE_ASSIGN:
rreg = generateCode(node->right, regGen, labGen, output);
fputs(emitStoreForAssignment(rreg, table, node->left->name), output);
return "";
case ASTNODE_STMT:
generateCode(node->left, regGen, labGen, output);
if(node->right){
generateCode(node->right, regGen, labGen,output);
}
return "";
case ASTNODE_ARITH_OP:
case ASTNODE_COMPARE:
case ASTNODE_LOGIC_OP:
lreg = generateCode(node->left, regGen, labGen,output);
rreg = generateCode(node->right, regGen,labGen,output);
reg = generate(regGen);
fputs(emitBinaryInstruction((int) node->op, lreg, rreg, reg), output);
return reg;
case ASTNODE_IF:
getIfLabelName(labGen, labels);
lreg = generateCode(node->left, regGen, labGen,output);
fputs(emitCBRInstruction(lreg, labels[0], labels[1]), output);
fputs(getLabelString(labels[0]), output);
generateCode(node->right, regGen, labGen,output);
fputs(getLabelString(labels[1]), output);
return "";
case ASTNODE_WHILE:
getWhileLabelName(labGen,labels);
fputs(getLabelString(labels[0]), output);
lreg = generateCode(node->left, regGen, labGen,output);
fputs(emitCBRInstruction(lreg, labels[1], labels[2]), output);
fputs(getLabelString(labels[1]), output);
generateCode(node->right, regGen, labGen,output);
fputs(emitJumpInstruction(labels[0]), output);
fputs(getLabelString(labels[2]),output);
return "";
case ASTNODE_IFELSE:
getIfElseLabelName(labGen, labels);
lreg = generateCode(node->left, regGen, labGen, output);
fputs(emitCBRInstruction(lreg, labels[0], labels[1]), output);
fputs(getLabelString(labels[0]), output);
generateCode(node->right->left, regGen, labGen,output);
fputs(emitJumpInstruction(labels[2]), output);
fputs(getLabelString(labels[1]), output);
generateCode(node->right->right, regGen, labGen,output);
fputs(getLabelString(labels[2]), output);
return "";
default:
return "";
}
}
void GenerateILOC(ASTNode* node, FILE * output){
RegisterGenerator * regGen = createRegisterGenerator();
LabelGenerator * labelGen = createLabelGenerator();
if(node == NULL){
fputs("", output);
} else{
generateCode(node, regGen, labelGen, output);
}
fclose(output);
}