-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstmt_eval.c
374 lines (325 loc) · 8.45 KB
/
stmt_eval.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
#include "stmt_eval.h"
#include "tokenize.h"
#include "sym.h"
#include "c8.h"
#include "sym.h"
#include <stdlib.h>
#include <stdbool.h>
/*
(+ (- 4 3) 2) => this should resolve completely
prevAccumulatedTotal = 2
now we look at (- 4 3)
prevAccumulatedTotal = 1 - then we return and it gets added.
what should happen
(+ (- 4 3 x) a 2)
we look at a particular expression and try to resolve it as much.
if we can resolve it, we return true, and don't need to look at it again - we can just dump it into an accumulation
if we can't resolve it, we dump an accumulated total and the other thing in a register, return false - which means that anything depending on it can't be resolved
**/
typedef int (*accum_val_t)(int, int);
typedef int (*accum_reg_t)(int *, int *, int); // accumulation register, subtraction register, input register
void resolve_exp_to_reg(struct ast_node *stmt, int output_reg)
{
struct symbol nextS;
switch (stmt->t_operatortype)
{
case T_IDENT:
if (stmt->sType == S_FUNC)
{
st_execute(stmt);
if (output_reg != 0)
c8_load_instr_reg(output_reg, 0);
break;
}
nextS = resolve_symbol(stmt->key.sloc);
if (nextS.loc.type == L_REG)
{
c8_load_instr_reg(output_reg, nextS.loc.loc);
}
else
{
printf("Fatal: unknown identifier on line %d\n", get_lineNo());
exit(1);
}
break;
case T_INTLIT:
c8_load_instr_const(stmt->key.val, output_reg);
break;
case T_STRING:
case T_SPRITE_LITERAL_DECL:
c8_load_instr_label(m_object_get_label_for_index(stmt->key.mloc), output_reg);
break;
default:
st_execute(stmt);
if (output_reg != 0)
c8_load_instr_reg(output_reg, 0);
}
}
bool resolve_exp(struct ast_node *stmt, int *res)
{
int accumulatedTotal = 0;
bool canResolve = true;
int accumReg = -1;
int extraSubReg = -1;
int innerExp = 0;
for (int i = 0; i < stmt->kChildren; i++)
{
if (stmt->children[i]->t_operatortype == T_INTLIT)
{
if (stmt->t_operatortype == T_PLUS || (stmt->t_operatortype == T_MINUS && i == 0))
accumulatedTotal += stmt->children[i]->key.val;
else if (stmt->t_operatortype == T_MINUS)
accumulatedTotal -= stmt->children[i]->key.val;
}
else if (stmt->children[i]->t_operatortype == T_PLUS || stmt->children[i]->t_operatortype == T_MINUS)
{
canResolve = canResolve && resolve_exp(stmt->children[i], &innerExp);
if (!canResolve)
{
if (accumReg == -1 && (stmt->t_operatortype == T_PLUS || (stmt->t_operatortype == T_MINUS && i == 0)))
{
accumReg = c8_alloc_reg();
c8_load_instr_reg(accumReg, 0);
}
else if (extraSubReg == -1 && stmt->t_operatortype == T_MINUS)
{
extraSubReg = c8_alloc_reg();
c8_load_instr_reg(extraSubReg, 0);
}
else if (stmt->t_operatortype == T_PLUS)
c8_add_instr_reg(accumReg, 0);
else if (stmt->t_operatortype == T_MINUS)
c8_add_instr_reg(extraSubReg, 0);
}
else
{
if (stmt->t_operatortype == T_PLUS || (stmt->t_operatortype == T_MINUS && i == 0))
accumulatedTotal += innerExp;
else if (stmt->t_operatortype == T_MINUS)
accumulatedTotal -= innerExp;
}
}
else if (stmt->children[i]->sType == S_FUNC)
{
canResolve = false;
st_execute(stmt->children[i]);
if (accumReg == -1 && (stmt->t_operatortype == T_PLUS || (stmt->t_operatortype == T_MINUS && i == 0)))
{
accumReg = c8_alloc_reg();
c8_load_instr_reg(accumReg, 0);
}
else if (extraSubReg == -1 && stmt->t_operatortype == T_MINUS)
{
extraSubReg = c8_alloc_reg();
c8_load_instr_reg(extraSubReg, 0);
}
else if (stmt->t_operatortype == T_PLUS)
c8_add_instr_reg(accumReg, 0);
else if (stmt->t_operatortype == T_MINUS)
c8_add_instr_reg(extraSubReg, 0);
}
else if (stmt->children[i]->t_operatortype == T_IDENT)
{
canResolve = false;
struct symbol nextS = resolve_symbol(stmt->children[i]->key.sloc);
if (nextS.loc.type == L_REG)
{
if (accumReg == -1 && (stmt->t_operatortype == T_PLUS || (stmt->t_operatortype == T_MINUS && i == 0)))
{
accumReg = c8_alloc_reg();
// if we encounter a variable as the first thing, we want to load it into the accumulator
c8_load_instr_reg(accumReg, nextS.loc.loc);
// if we encounter a variable as a later operand
}
else if (extraSubReg == -1 && stmt->t_operatortype == T_MINUS)
{
extraSubReg = c8_alloc_reg();
c8_load_instr_reg(extraSubReg, nextS.loc.loc);
}
else if (stmt->t_operatortype == T_PLUS)
{
c8_add_instr_reg(accumReg, nextS.loc.loc);
}
else if (stmt->t_operatortype == T_MINUS)
{
c8_add_instr_reg(extraSubReg, nextS.loc.loc);
}
}
else
{
printf("Memory access error arithmetic\n");
exit(1);
}
}
}
if (accumulatedTotal > 0 && accumReg != -1)
c8_add_instr_const(accumulatedTotal, accumReg);
if (extraSubReg != -1 && accumReg == -1)
{
accumReg = c8_alloc_reg();
c8_load_instr_const(accumulatedTotal, accumReg);
}
if (extraSubReg != -1)
{
c8_sub_instr_reg(accumReg, extraSubReg);
c8_free_reg(extraSubReg);
}
// accumReg has the final result, or accumulatedTotal
if (accumReg != -1)
{
c8_load_instr_reg(0, accumReg);
c8_free_reg(accumReg);
}
if (canResolve)
{
*res = accumulatedTotal;
}
return canResolve;
}
void gen_arith_res(struct ast_node *stmt)
{
int x = -1;
bool didResolve = resolve_exp(stmt, &x);
if (didResolve)
c8_load_instr_const(x, 0);
}
void execute_setq(struct ast_node *stmt)
{
struct symbol cur_symbol;
struct symbol next_sym;
if (stmt->kChildren % 2 == 1)
{
printf("Error: line %d: setq may not have an odd # of parameters\n", get_lineNo());
exit(1);
}
for (int i = 0; i < stmt->kChildren; i += 2)
{
resolve_exp_to_reg(stmt->children[i + 1], 0);
cur_symbol = resolve_symbol(stmt->children[i]->key.sloc);
if (cur_symbol.i_type == I_NONE)
{
cur_symbol = sym_declare_symbol(stmt->children[i]->key.sloc);
}
c8_load_instr_reg(cur_symbol.loc.loc, 0);
}
}
void execute_fxn(struct ast_node *stmt)
{
if (stmt->kChildren > 5)
{
printf("Error: functions may not have more than 3 parameters\n");
exit(1);
}
int nRegisters = 0;
int alloc_registers[5];
int product_registers[5]; // registers that have a function evaluation that should be done first
// this is to make sure that the function evaluation happens before the function call
for (int i = 0; i < stmt->kChildren; i++)
{
if (stmt->children[i]->sType == S_FUNC)
{
st_execute(stmt->children[i]);
product_registers[i] = c8_alloc_reg();
c8_load_instr_reg(product_registers[i], 0);
}
else
{
product_registers[i] = -1;
}
}
for (int i = 0; i < stmt->kChildren; i++)
{
int next_reg;
next_reg = c8_alloc_param_reg();
alloc_registers[i] = next_reg;
nRegisters++;
if (stmt->children[i]->sType == S_FUNC)
{
c8_load_instr_reg(next_reg, product_registers[i]);
}
else
{
resolve_exp_to_reg(stmt->children[i], next_reg);
}
}
c8_callq(sym_get_symbol_for(stmt->key.sloc));
for (int i = 0; i < nRegisters; i++)
{
c8_free_reg(alloc_registers[i]);
}
}
void execute_cls(struct ast_node *stmt)
{
if (stmt->kChildren > 0)
{
printf("cls received unexpected # of arguments: line %d\n", get_lineNo());
exit(1);
}
c8_cls();
}
void execute_if(struct ast_node *stmt)
{
struct symbol nextS;
char else_label[15];
char endif_label[15];
bool hasElse = stmt->kChildren == 3;
if (stmt->kChildren < 2 || stmt->kChildren > 3)
{
printf("Invalid if statement on line %d\n", get_lineNo());
exit(1);
}
resolve_exp_to_reg(stmt->children[0], 0);
c8_if_stmt_branch(true, else_label, endif_label);
resolve_exp_to_reg(stmt->children[1], 0);
c8_jp_label(endif_label);
c8_print_label(else_label);
if (hasElse)
{
resolve_exp_to_reg(stmt->children[2], 0);
}
else
{
c8_load_instr_const(0, 0);
}
c8_print_label(endif_label);
}
// this fxn will generate assembly
void st_execute(struct ast_node *stmt)
{
struct ast_node *glue;
short *sprite_data = NULL;
int i;
switch (stmt->t_operatortype)
{
case T_SPRITE_LITERAL_DECL:
sprite_data = (short *)malloc(sizeof(short) * stmt->kChildren);
for (i = 0; i < stmt->kChildren; i++)
{
sprite_data[i] = stmt->children[i]->key.val;
}
stmt->key.mloc = m_object_add_data((char *)sprite_data, M_SPRITE);
free(sprite_data);
break;
case T_IDENT:
execute_fxn(stmt);
break;
case T_INTLIT:
break;
case T_PLUS:
case T_MINUS:
gen_arith_res(stmt);
break;
case T_SETQ:
execute_setq(stmt);
break;
case T_CLS:
execute_cls(stmt);
break;
case T_IF:
execute_if(stmt);
break;
default:
printf("Fatal: unsupported statement\n");
exit(1);
}
}