-
Notifications
You must be signed in to change notification settings - Fork 0
/
quad.hpp
692 lines (625 loc) · 15.5 KB
/
quad.hpp
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#include <stdarg.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "symtable.hpp"
#include <errno.h>
#include <sys/stat.h>
// #define EXPAND_SIZE 1024
// #define CURR_SIZE (total * sizeof(quad))
// #define NEW_SIZE (EXPAND_SIZE * sizeof(quad) + CURR_SIZE)
typedef struct expr expr;
typedef struct quad quad;
void debug_quad(quad* q, int i = 0);
typedef enum iopcode {
assign_op,
add,
sub,
mul_op,
div_op,
mod_op,
uminus_op,
and_op,
or_op,
not_op,
if_eq,
if_noteq,
if_lesseq,
if_greater_eq,
if_less,
if_greater,
call,
param,
ret,
getretval,
funcstart,
funcend,
tablecreate,
tablegetelem,
tablesetelem,
jump,
nop
} iopcode;
string iop_tostr(iopcode iop) {
switch (iop) {
case assign_op:
return "assign_op";
case add:
return "add";
case sub:
return "sub";
case mul_op:
return "mul_op";
case div_op:
return "div_op";
case mod_op:
return "mod_op";
case uminus_op:
return "uminus_op";
case and_op:
return "and_op";
case or_op:
return "or_op";
case not_op:
return "not_op";
case if_eq:
return "if_eq";
case if_noteq:
return "if_noteq";
case if_lesseq:
return "if_lesseq";
case if_greater_eq:
return "if_greater_eq";
case if_less:
return "if_less";
case if_greater:
return "if_greater";
case call:
return "call";
case param:
return "param";
case ret:
return "ret";
case getretval:
return "getretval";
case funcstart:
return "funcstart";
case funcend:
return "funcend";
case tablecreate:
return "tablecreate";
case tablegetelem:
return "tablegetelem";
case tablesetelem:
return "tablesetelem";
case jump:
return "jump";
default:
assert(0);
}
}
struct quad {
iopcode iop;
expr* result;
expr* arg1;
expr* arg2;
int label;
unsigned line;
unsigned taddress;
};
typedef enum expr_t {
var_e,
tableitem_e,
programfunc_e,
libraryfunc_e,
arithexpr_e,
boolexpr_e,
assignexpr_e,
newtable_e,
constnum_e,
constbool_e,
conststring_e,
nil_e,
} expr_t;
string expr_t_tostr(expr_t type) {
switch (type) {
case var_e:
return "var_e";
case tableitem_e:
return "tableitem_e";
case programfunc_e:
return "programfunc_e";
case libraryfunc_e:
return "libraryfunc_e";
case arithexpr_e:
return "arithexpr_e";
case boolexpr_e:
return "boolexpr_e";
case assignexpr_e:
return "assignexpr_e";
case newtable_e:
return "newtable_e";
case constnum_e:
return "constnum_e";
case constbool_e:
return "constbool_e";
case conststring_e:
return "conststring_e";
case nil_e:
return "nil_e,";
default:
assert(0);
}
}
typedef struct expr {
expr_t type;
SymbolTableEntry* sym;
struct expr* index;
double numConst;
char* strConst;
unsigned char boolConst;
struct expr* next;
vector<unsigned int> truelist;
vector<unsigned int> falselist;
} expr;
struct X {
expr* e;
int label;
};
struct call_l {
expr* elist;
bool method;
char* name;
};
struct stmt_l {
vector<unsigned int> breaklist;
vector<unsigned int> contlist;
};
struct for_prefix {
int test;
int enter;
};
expr* newExpr(expr_t t);
vector<quad*> quads;
int tmpcounter = 0;
int loopcnt = 0;
stack<int> loopcntStack;
SymTable symbol_table = *new SymTable();
// // Functions
// quad* expand() {
// assert(currQuad == total);
// quad* p = new quad();
// quads.push_back(p);
// total += EXPAND_SIZE;
// return p;
// }
void emit(iopcode iop, expr* arg1 = NULL, expr* arg2 = NULL,
expr* result = NULL, int label = -1) {
quad* p;
// if (currQuad == total)
// p = expand();
// else{
// p = new quad();
// quads.at(currQuad) = p;
// }
// currQuad++;
p = new quad();
expr* nil_expr = newExpr(nil_e);
p->iop = iop;
p->arg1 = arg1 == NULL ? nil_expr : arg1;
p->arg2 = arg2 == NULL ? nil_expr : arg2;
p->result = result == NULL ? nil_expr : result;
p->label = label;
// debug_quad(p);
quads.push_back(p);
}
void emit_function(iopcode iop, expr* result) { emit(iop, NULL, NULL, result); }
void patchLabel(unsigned int quadNo, int label) {
assert(quadNo < quads.size());
assert(label != -1);
// currQuad);
quads[quadNo]->label = (unsigned int)label;
}
void patchLabel(vector<unsigned int> quads_c, int label) {
for (int i = 0; i < quads_c.size(); i++) {
// printf(" %d", quads_c[i]);
patchLabel(quads_c[i], label);
}
// printf(" \n");
}
char* new_tmpname() {
char name[1000];
sprintf(name, "$t%d", tmpcounter);
// printf("tmp: $t%d\n", tmpcounter);
tmpcounter++;
return strdup(name);
}
void reset_tmp() { tmpcounter = 0; }
SymbolTableEntry* new_tmp(unsigned int lineno) {
char* name = new_tmpname();
SymbolTableEntry* sym = symbol_table.lookUp_curscope(name);
if (NULL == sym) {
sym = symbol_table.insert(name, lineno, LOCAL);
sym->space = currScopeSpace();
sym->offset = currScopeOffset();
inCurrScopeOffset();
return sym;
} else
return sym;
}
unsigned int nextQuadLabel() { return quads.size(); } // currQuad;}
expr* lvalue_expr(SymbolTableEntry* entry) {
assert(entry);
expr* new_expr = new expr();
new_expr->sym = entry;
new_expr->next = (expr*)0;
switch (entry->type) {
case FORMAL: {
}
case LOCAL: {
}
case GLOBAL: {
new_expr->type = var_e;
break;
}
case LIBFUNC: {
new_expr->type = libraryfunc_e;
break;
}
case USERFUNC: {
new_expr->type = programfunc_e;
break;
}
default:
assert(0);
}
return new_expr;
}
expr* newExpr(expr_t t) {
expr* new_expr = new expr();
new_expr->type = t;
new_expr->next = NULL;
return new_expr;
}
expr* newExpr_constString(char* s) {
expr* e = newExpr(conststring_e);
e->strConst = strdup(s);
return e;
}
expr* newExpr_constNum(double i) {
expr* e = newExpr(constnum_e);
e->numConst = i;
return e;
}
expr* newExpr_constBool(bool b) {
expr* e = newExpr(constbool_e);
e->boolConst = b;
return e;
}
expr* emit_ifTableItem(expr* e) { // FIXME:
if (e->type != tableitem_e)
return e;
else {
expr* result = newExpr(var_e);
result->sym = new_tmp(symbol_table.get_lineno(e->sym));
emit(tablegetelem, e, e->index, result);
return result;
}
}
// function to create new expression/quad ??
expr* member_item(expr* lvalue, char* name) {
lvalue = emit_ifTableItem(lvalue);
expr* item = newExpr(tableitem_e);
item->sym = lvalue->sym;
item->index = newExpr_constString(name);
return item;
}
expr* make_call(expr* lvalue, expr* elist) {
expr* func = emit_ifTableItem(lvalue);
assert(func);
// assert(!symbol_table.is_var(func->sym->type));
expr* curr = elist;
while (curr != NULL ) {
emit(param, curr, NULL, NULL);
curr = curr->next;
}
emit(call, func, NULL, NULL);
expr* result = newExpr(var_e);
assert(lvalue->sym);
result->sym = new_tmp(lvalue->sym->value.funcVal->line);
emit(getretval, NULL, NULL, result);
return result;
}
void comperror(char* format, ...) {
va_list args;
va_start(args, format);
printf(format, args);
va_end(args);
}
void checkUminus(expr* e) {
if (e->type == constbool_e || e->type == conststring_e || e->type == nil_e ||
e->type == newtable_e || e->type == programfunc_e ||
e->type == libraryfunc_e || e->type == boolexpr_e)
comperror("Illegal expr to unary -");
}
double compute(iopcode op, double a, double b) {
switch (op) {
case add:
return a + b;
case sub:
return a - b;
case mul_op:
return a * b;
case div_op:
return b ? a / b : 0;
case mod_op:
return ((int)a) % ((int)b);
default:
assert(false);
}
}
bool compute_rel(iopcode op, double a, double b) {
switch (op) {
case if_greater:
return a > b;
case if_less:
return a < b;
case if_greater_eq:
return a >= b;
case if_lesseq:
return a <= b;
case if_eq:
return a == b;
case if_noteq:
return a != b;
default:
assert(false);
}
}
bool compute(iopcode op, bool a, bool b) {
switch (op) {
case and_op:
return a && b;
case or_op:
return a || b;
case if_eq:
return a == b;
case if_noteq:
return a != b;
default:
assert(false);
}
}
bool is_same(expr_t a, expr_t b) {
if (a == var_e || b == var_e) return true;
if (a == b) return true;
if ((a == arithexpr_e || a == constnum_e) &&
(b == arithexpr_e || b == constnum_e))
return true;
if ((a == constbool_e || a == boolexpr_e) &&
(b == constbool_e || b == boolexpr_e))
return true;
if ((a == newtable_e || b == nil_e) && (b == newtable_e || a == nil_e))
return true;
if (a == nil_e || b == nil_e) return true;
return false;
}
bool isvalid_arithmeticCheck(expr_t a, expr_t b) {
// printf("isvalid_arithmeticCheck: %s : %s\n", expr_t_tostr(a).c_str(),
// expr_t_tostr(b).c_str());
if (a == programfunc_e || a == libraryfunc_e || b == programfunc_e ||
b == libraryfunc_e) {
return false;
}
if (a == nil_e || a == boolexpr_e || a == constbool_e || a == newtable_e ||
a == conststring_e) {
return false;
}
if (b == nil_e || b == boolexpr_e || b == constbool_e || b == newtable_e ||
b == conststring_e) {
return false;
}
return true;
}
bool get_bool(expr* e) {
switch (e->type) {
case newtable_e:
case programfunc_e:
case libraryfunc_e:
return true;
case nil_e:
return false;
case conststring_e:
return e->strConst != "";
case constnum_e:
case arithexpr_e:
return e->numConst != 0;
case boolexpr_e:
case constbool_e:
return e->boolConst;
case tableitem_e:
assert(false);
default:
assert(false);
}
}
string get_string(expr* e, int index = 0) {
if (e == NULL) return "NULL";
switch (e->type) {
case constbool_e:
return e->boolConst ? "true" : "false";
case constnum_e: {
std::ostringstream ss;
ss << e->numConst;
return ss.str();
}
case conststring_e:
return strdup(e->strConst);
case nil_e:
return " ";
case tableitem_e:
case newtable_e:
case var_e:
case programfunc_e:
case libraryfunc_e:
case arithexpr_e:
case boolexpr_e:
case assignexpr_e:
return symbol_table.get_name(e->sym);
}
}
void change_type(expr* lvalue, expr* expr) {
// printf("~~~~~~~~Changed %s %s type %s\n", symbol_table.get_name(lvalue->sym),
// expr_t_tostr(lvalue->type).c_str(), expr_t_tostr(expr->type).c_str());
assert(lvalue->type != libraryfunc_e);
if (expr->type == constnum_e) {
lvalue->numConst = expr->numConst;
lvalue->type = arithexpr_e;
if (lvalue->sym->type == USERFUNC) {
Variable* v = new Variable();
Function* f = lvalue->sym->value.funcVal;
v->scope = f->scope;
v->line = f->line;
f->totalLocals--;
if (f->totalLocals == 0) {
// delete args and delete function struct
}
lvalue->sym->value.varVal = v;
}
} else if (expr->type == constbool_e) {
lvalue->boolConst = expr->boolConst;
lvalue->type = boolexpr_e;
if (lvalue->sym->type == USERFUNC) {
Variable* v = new Variable();
Function* f = lvalue->sym->value.funcVal;
v->scope = f->scope;
v->line = f->line;
v->name = f->name;
f->totalLocals--;
if (f->totalLocals == 0) {
// delete args and delete function struct
}
lvalue->sym->value.varVal = v;
}
} else if (expr->type == conststring_e) {
lvalue->strConst = expr->strConst;
if (lvalue->sym->type == USERFUNC) {
Variable* v = new Variable();
Function* f = lvalue->sym->value.funcVal;
v->scope = f->scope;
v->line = f->line;
f->totalLocals--;
if (f->totalLocals == 0) {
// delete args and delete function struct
}
lvalue->sym->value.varVal = v;
}
} else if (expr->type == nil_e) {
return; // assignment of null expression
} else {
assert(expr->sym);
lvalue->sym = expr->sym;
}
lvalue->type = expr->type;
// probably delete expr node
}
void debug_quad(quad* q, int i) {
printf("Quad %d", i);
printf(" %s", iop_tostr(q->iop).c_str());
printf(" %s", get_string(q->result).c_str());
printf(" %s", get_string(q->arg1).c_str());
printf(" %s", get_string(q->arg2).c_str());
printf(" %d\n", (q->label));
}
void printQuads() {
ofstream myfile;
string s = "quads.txt";
myfile.open(s.c_str());
int sizes[6] = {7, 8, 8, 6, 6, 7};
for (int i = 0; i < quads.size(); i++) {
quad* q = quads.at(i);
stringstream ss;
ss << q->label;
string label = (q->label == -1) ? "" : ss.str();
if (sizes[5] < label.size() + 2) sizes[5] = label.size() + 2;
ss << i;
if (sizes[0] < ss.str().size() + 2) sizes[0] = ss.str().size() + 2;
if (sizes[1] < iop_tostr(q->iop).size() + 2)
sizes[1] = iop_tostr(q->iop).size() + 2;
if (sizes[2] < get_string(q->result).size() + 2)
sizes[2] = get_string(q->result).size() + 2;
if (sizes[3] < get_string(q->arg1).size() + 2)
sizes[3] = get_string(q->arg1).size() + 2;
if (sizes[4] < get_string(q->arg2).size() + 2)
sizes[4] = get_string(q->arg2).size() + 2;
}
int sum = 0;
for (int i = 0; i < 6; i++) sum += sizes[i];
myfile << "\n\n"
<< string(((sum - 7) / 2), '-') << " Quads "
<< string(((sum - 7) / 2)+(sum%2==0), '-') << "\n"
<< setw(sizes[0]) << "quad#" << setw(sizes[1]) << "opcode"
<< setw(sizes[2]) << "result" << setw(sizes[3]) << "arg1"
<< setw(sizes[4]) << "arg2" << setw(sizes[5]) << "label" << endl
<< string(sum, '-') << endl;
for (int i = 0; i < quads.size(); i++) {
quad* q = quads.at(i);
stringstream ss;
ss << q->label;
string label = (q->label == -1) ? "" : ss.str();
myfile << setw(sizes[0]) << i << setw(sizes[1]) << iop_tostr(q->iop)
<< setw(sizes[2]) << get_string(q->result) << setw(sizes[3])
<< get_string(q->arg1) << setw(sizes[4]) << get_string(q->arg2)
<< setw(sizes[5]) << label << endl;
}
myfile.close();
}
vector<unsigned int> newList(unsigned int a) {
vector<unsigned int> b;
b.push_back(a);
return b;
}
vector<unsigned int> merge(vector<unsigned int> a, vector<unsigned int> b) {
vector<unsigned int> c;
// printf("ton pairno\n");
// printf("A:");
// for (int i = 0; i < a.size(); i++) {
// printf("%d ", a[i]);
// }
// printf("\nB:");
// for (int i = 0; i < b.size(); i++) {
// printf("%d ", b[i]);
// }
c.reserve(a.size() + b.size()); // preallocate memory
c.insert(c.end(), a.begin(), a.end());
c.insert(c.end(), b.begin(), b.end());
// for (int i = 0; i < c.size(); i++) {
// printf("%d ", c[i]);
// }
// printf("\n");
return c;
}
stmt_l* merge(stmt_l* a, stmt_l* b) {
stmt_l* c = new stmt_l();
c->breaklist = merge((a != NULL ? (a->breaklist) : (c->breaklist)),
(b != NULL ? (b->breaklist) : (c->breaklist)));
c->contlist = merge((a != NULL ? (a->contlist) : (c->contlist)),
(b != NULL ? (b->contlist) : (c->contlist)));
return c;
}
void reverseUtil(expr* curr, expr* prev, expr** head) {
if (!curr->next) {
*head = curr;
curr->next = prev;
return;
}
expr* next = curr->next;
curr->next = prev;
reverseUtil(next, curr, head);
}
void reverse_list(expr** head) {
if (!head) return;
reverseUtil(*head, NULL, head);
}