-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntaxnodes.cpp
594 lines (516 loc) · 14.9 KB
/
syntaxnodes.cpp
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
#define _CRT_SECURE_NO_WARNINGS
#include "syntaxnodes.h"
#include <vector>
#include <set>
#include "symbol.h"
#include <algorithm>
#include "symtable.h"
#include "checktype.h"
#include <map>
set<TokenType> RelOp = { TK_GREAT, TK_GREAT_EQUAL, TK_LESS, TK_LESS_EQUAL, TK_EQUAL, TK_NOT_EQUAL };
Expr::Expr(TypeExpr TypeExp) : TypeExp(TypeExp){}
ExprBinOp::ExprBinOp(Expr* Left, Token Op, Expr* Right) : Left(Left), Op(Op), Right(Right), Expr(BinExp){}
ExprUnarOp::ExprUnarOp(Token Op, Expr* Exp) : Op(Op), Exp(Exp), Expr(UnarExp){}
ExprConst::ExprConst(Token Value, TypeExpr TypeExp) : Value(Value), Expr(TypeExp){}
ExprBoolConst::ExprBoolConst(Token Value) : ExprConst(Value, ConstBoolExp){}
ExprIntConst::ExprIntConst(Token Value) : ExprConst(Value, ConstIntExp){}
ExprIntConst::ExprIntConst(string Value) : ExprConst(Token(Position(), Value, TK_INTEGER_VALUE), ConstIntExp) {}
ExprDoubleConst::ExprDoubleConst(Token Value) : ExprConst(Value, ConstDoubleExp){}
ExprStringConst::ExprStringConst(Token Value) : ExprConst(Value, ConstStringExp){}
ExprIdent::ExprIdent(Symbol* Sym, Position Pos) : Sym(Sym), Expr(VarExp), Pos(Pos){}
ExprArrayIndex::ExprArrayIndex(Expr* Left, Expr* Right) : Left(Left), Right(Right), Expr(ArrayExp){}
ExprAssign::ExprAssign(Expr* Left, Expr* Right) : Left(Left), Right(Right), Expr(AssignExp){}
ExprFunction::ExprFunction(Expr* Left, vector<Expr*> Args) : Left(Left), Args(Args), Expr(FunctionExp){}
ExprRecord::ExprRecord(Expr* Left, Symbol* Right) : Left(Left), Right(Right), Expr(RecordExp){}
ExprInitList::ExprInitList(vector<Expr*> List) : Expr(InitExp), List(List) {}
ExprPointer::ExprPointer(Expr* Exp) : Exp(Exp), Expr(PointerExp) {}
ExprDereference::ExprDereference(Expr* Exp) : Exp(Exp), Expr(DereferenceExp) {}
void ExprUnarOp::Print(int Spaces){
Exp->Print(Spaces + 1);
print_indent(Spaces);
cout << Op.Source.c_str() << endl;
}
void ExprBinOp::Print(int Spaces){
Right->Print(Spaces + 1);
print_indent(Spaces);
cout << Op.Source.c_str() << endl;
Left->Print(Spaces + 1);
}
void ExprConst::Print(int Spaces){
print_indent(Spaces);
cout << Value.Source.c_str() << endl;
}
void ExprIdent::Print(int Spaces){
print_indent(Spaces);
cout << Sym->Name << endl;
}
void ExprAssign::Print(int Spaces){
Right->Print(Spaces + 1);
print_indent(Spaces);
cout << ":=" << endl;
Left->Print(Spaces + 1);
}
void ExprFunction::Print(int Spaces){
for (auto it = Args.begin(); it < Args.end(); ++it) {
(*it)->Print(Spaces + 1);
}
print_indent(Spaces);
cout << "()" << endl;
Left->Print(Spaces + 1);
}
void ExprRecord::Print(int Spaces){
print_indent(Spaces + 1);
cout << Right->Name << endl;
print_indent(Spaces);
cout << "." << endl;
Left->Print(Spaces + 1);
}
void ExprArrayIndex::Print(int Spaces){
Right->Print(Spaces + 1);
print_indent(Spaces);
cout << "[]" << endl;
Left->Print(Spaces + 1);
}
void ExprInitList::Print(const int Spaces) {
for (auto it = List.begin(); it < List.end(); ++it) {
(*it)->Print(Spaces);
}
}
void ExprPointer::Print(const int Spaces) {
Exp->Print(Spaces + 1);
print_indent(Spaces);
cout << "@" << endl;
}
void ExprDereference::Print(const int Spaces) {
print_indent(Spaces);
cout << "^" << endl;
Exp->Print(Spaces + 1);
}
void Expr::GetIdentStr(ExpArgList* List){}
void ExprBinOp::GetIdentStr(ExpArgList* List){
Right->GetIdentStr(List);
Left->GetIdentStr(List);
}
void ExprUnarOp::GetIdentStr(ExpArgList* List){
Exp->GetIdentStr(List);
}
void ExprConst::GetIdentStr(ExpArgList* List){}
void ExprIdent::GetIdentStr(ExpArgList* List){
List->Vec.push_back(Sym->Name);
}
void ExprAssign::GetIdentStr(ExpArgList* List){
List->Flag = false;
}
void ExprArrayIndex::GetIdentStr(ExpArgList* List){
Left->GetIdentStr(List);
Right->GetIdentStr(List);
}
void ExprFunction::GetIdentStr(ExpArgList* List){
List->Flag = false;
}
void ExprRecord::GetIdentStr(ExpArgList* List){
List->Flag = false;
}
map<TokenType, AsmOpType> BinOperations = {
make_pair(TK_PLUS, Add), make_pair(TK_MINUS, Sub), make_pair(TK_XOR, Xor), make_pair(TK_OR, Or), make_pair(TK_MUL, IMul), make_pair(TK_AND, And)
};
void ExprBinOp::Generate_Bool_Expr(Asm_Code* Code, ArgState State, AsmOpType Op) {
string LabelName_1 = Code->GetLocalLabelName();
string LabelName_2 = Code->GetLocalLabelName();
Left->Generate(Code, State);
Code->Add(Pop, EAX);
Code->Add(Test, EAX, EAX);
Code->Add(Op, LabelName_1);
Right->Generate(Code, State);
Code->Add(Jmp, LabelName_2);
Code->AddLabel(LabelName_1);
Code->Add(Push, EAX);
Code->AddLabel(LabelName_2);
}
#define GetCmd(TypeID, Cmd_f, Cmd_i) (TypeID == TypeID_Double ? Cmd_f : Cmd_i)
void ExprBinOp::Generate_Relation_Expr(Asm_Code* Code, ArgState State) {
string LabelName_1 = Code->GetLocalLabelName();
string LabelName_2 = Code->GetLocalLabelName();
Left->Generate(Code, State);
if (Left->TypeID != TypeID_Double && Right->TypeID == TypeID_Double) {
Code->Add(Fild, dword, addr, ESP);
Code->Add(Sub, ESP, 4);
Code->Add(Fstp, qword, addr, ESP);
}
Right->Generate(Code, State);
if (Left->TypeID == TypeID_Double && Right->TypeID != TypeID_Double) {
Code->Add(Fild, dword, addr, ESP);
Code->Add(Sub, ESP, 4);
Code->Add(Fstp, qword, addr, ESP);
}
auto TypeID = Left->TypeID == TypeID_Double || Right->TypeID == TypeID_Double ? TypeID_Double : TypeID_Integer;
if (TypeID == TypeID_Double) {
Code->Add(Fld, qword, addr, ESP);
Code->Add(Fld, qword, addr, ESP, 8);
Code->Add(Add, ESP, 16);
}
if (Left->TypeID == TypeID_Double || Right->TypeID == TypeID_Double) {
Code->Add(Fcomip, ST0, ST1);
Code->Add(Fstp);
}
else {
Code->Add(Pop, EBX);
Code->Add(Pop, EAX);
Code->Add(Cmp, EAX, EBX);
}
switch (Op.Type) {
case TK_GREAT:
Code->Add(GetCmd(TypeID, Ja, Jg), LabelName_1);
break;
case TK_GREAT_EQUAL:
Code->Add(GetCmd(TypeID, Jae, Jge), LabelName_1);
break;
case TK_LESS:
Code->Add(GetCmd(TypeID, Jb, Jl), LabelName_1);
break;
case TK_LESS_EQUAL:
Code->Add(GetCmd(TypeID, Jbe, Jle), LabelName_1);
break;
case TK_EQUAL:
Code->Add(Je, LabelName_1);
break;
case TK_NOT_EQUAL:
Code->Add(Jne, LabelName_1);
break;
}
Code->Add(Push, "0");
Code->Add(Jmp, LabelName_2);
Code->AddLabel(LabelName_1);
Code->Add(Push, "1");
Code->AddLabel(LabelName_2);
}
void ExprBinOp::Generate_Double_Expr(Asm_Code* Code, ArgState State) {
Right->Generate(Code, State);
Left->Generate(Code, State);
if (Left->TypeID == TypeID_Double) {
Code->Add(Fld, qword, addr, ESP);
Code->Add(Add, ESP, 8);
}
else {
Code->Add(Fild, dword, addr, ESP);
Code->Add(Add, ESP, 4);
}
auto TypeID = Right->TypeID;
auto Size = TypeID == TypeID_Double ? qword : dword;
switch (Op.Type) {
case TK_PLUS:
Code->Add(GetCmd(TypeID, Fadd, Fiadd), Size, addr, ESP);
break;
case TK_MINUS:
Code->Add(GetCmd(TypeID, Fsub, Fisub), Size, addr, ESP);
break;
case TK_MUL:
Code->Add(GetCmd(TypeID, Fmul, Fimul), Size, addr, ESP);
break;
case TK_DIV:
Code->Add(GetCmd(TypeID, Fdiv, Fidiv), Size, addr, ESP);
break;
}
if (Right->TypeID != TypeID_Double) {
Code->Add(Sub, ESP, 4);
}
Code->Add(Fstp, qword, addr, ESP);
}
void ExprBinOp::Generate(Asm_Code* Code, ArgState State) {
if (RelOp.find(Op.Type) != RelOp.end()) {
Generate_Relation_Expr(Code, State);
return;
}
if (TypeID == TypeID_Double) {
Generate_Double_Expr(Code, State);
return;
}
if (Left->TypeID == TypeID_Boolean && (Op.Type == TK_AND || Op.Type == TK_OR)) {
if (Op.Type == TK_AND) {
Generate_Bool_Expr(Code, State, Jz);
}
if (Op.Type == TK_OR) {
Generate_Bool_Expr(Code, State, Jnz);
}
return;
}
Left->Generate(Code);
if (Op.Type == TK_SHL || Op.Type == TK_SHR) { /* shl/shr eax, A */
Code->Add(Pop, EAX); /* A - const only */
if (Right->TypeExp != ConstIntExp) {
throw ConstIntExpressionExpected(Op.Pos);
}
Code->Add(Op.Type == TK_SHL ? Shl : Shr, EAX, ((ExprIntConst*)Right)->Value.Source);
Code->Add(Push, EAX);
return;
}
Right->Generate(Code);
Code->Add(Pop, EBX);
Code->Add(Pop, EAX);
if (Op.Type == TK_DIV_INT || Op.Type == TK_MOD) {
Code->Add(Xor, EDX, EDX);
Code->Add(Div, EBX);
Op.Type == TK_DIV_INT ? Code->Add(Push, EAX) : Code->Add(Push, EDX);
return;
}
for (auto it = BinOperations.cbegin(); it != BinOperations.cend(); ++it) {
if ((*it).first == Op.Type) {
Code->Add((*it).second, EAX, EBX);
Code->Add(Push, EAX);
return;
}
}
}
void ExprUnarOp::Generate(Asm_Code* Code, ArgState State) {
switch (Op.Type) {
case TK_PLUS:
return;
case TK_MINUS:
Exp->Generate(Code);
Code->Add(Pop, EAX);
Code->Add(Neg, EAX);
Code->Add(Push, EAX);
return;
case TK_NOT:
Exp->Generate(Code);
Code->Add(Pop, EAX);
Code->Add(Not, EAX);
Code->Add(Push, EAX);
return;
}
}
void ExprBoolConst::Generate(Asm_Code* Code, ArgState State) {
Code->Add(Push, _stricmp(Value.Source.c_str(), "true") == 0 ? "1" : "0");
}
void ExprIntConst::Generate(Asm_Code* Code, ArgState State) {
Code->Add(Push, Value.Source);
}
void ExprIntConst::ConvertToDouble(Asm_Code* Code, ArgState State) {
Code->Add(Fild, dword, addr, ESP);
Code->Add(Sub, ESP, 4);
Code->Add(Fstp, qword, addr, ESP);
}
void ExprDoubleConst::Generate(Asm_Code* Code, ArgState State) {
string Name = Code->AddDoubleVar(Value.Source);
Code->Add(Push, dword, addr, Name, 4);
Code->Add(Push, dword, addr, Name, 0);
}
void ExprStringConst::Generate(Asm_Code* Code, ArgState State) {
if (Value.Source.size() == 1) {
Code->Add(Push, '\'' + Value.Source + '\'');
return;
}
Code->Add(Push, Code->AddConstString(Value.Source));
}
static void PushRValue(Asm_Code* Code, int Size) {
for (int i = Size - 4; i >= 0; i -= 4) {
Code->Add(Push, dword, addr, EAX, i);
}
}
void ExprIdent::Generate(Asm_Code* Code, ArgState State) {
auto IdenSym = (SymIdent*)Sym;
if (IdenSym->isLocal) {
if (IdenSym->depth < Code->depth) {
Code->Add(Mov, EAX, addr, "depth", 4 * IdenSym->depth);
}
else {
Code->Add(Mov, EAX, EBP);
}
Code->Add(Add, EAX, IdenSym->offset);
}
else {
Code->Add(Mov, EAX, IdenSym->GenerateName());
}
if ((IdenSym->State == Var || IdenSym->State == Const)) {
Code->Add(Mov, EAX, addr, EAX);
}
if (State == RValue) {
PushRValue(Code, IdenSym->GetSize());
return;
}
Code->Add(Push, EAX);
}
void ExprAssign::Generate(Asm_Code* Code, ArgState State) {
Right->Generate(Code);
Left->Generate(Code, Var);
Code->Add(Pop, EAX);
if (Left->TypeID == TypeID_Double && Right->TypeID != TypeID_Double) {
Right->ConvertToDouble(Code);
}
int Size = max(Left->GetSize(), Right->GetSize());
for (int i = 0; i < Size; i += 4) {
Code->Add(Pop, EBX);
Code->Add(Mov, addr, EAX, i, EBX);
}
}
void ExprArrayIndex::Generate(Asm_Code* Code, ArgState State) {
Left->Generate(Code, Var);
Right->Generate(Code);
Code->Add(Pop, EBX);
int Size = GetSize();
int Low = GetBound(0).first;
Code->Add(Sub, EBX, Low);
Code->Add(IMul, EBX, Size);
if (Left->TypeExp == FunctionExp) {
Code->Add(Mov, EAX, ESP);
Code->Add(Add, EAX, EBX);
Code->Add(Add, ESP, Left->GetSize());
}
else {
Code->Add(Pop, EAX);
Code->Add(Add, EAX, EBX);
}
if (State == RValue) {
PushRValue(Code, Size);
return;
}
Code->Add(Push, EAX);
}
void ExprRecord::Generate(Asm_Code* Code, ArgState State) {
Left->Generate(Code, Var);
int offset = ((SymIdent*)Right)->offset;
if (Left->TypeExp == FunctionExp) {
Code->Add(Mov, EAX, ESP);
Code->Add(Add, EAX, offset);
Code->Add(Add, ESP, Left->GetSize());
}
else {
Code->Add(Pop, EAX);
Code->Add(Add, EAX, offset);
}
if (State == RValue) {
PushRValue(Code, Right->GetSize());
return;
}
Code->Add(Push, EAX);
}
void ExprFunction::GenerateWrite(Asm_Code* Code, int argc) {
vector<MyTypeID> TypeIDexp;
for (auto it = Args.rbegin(); it != Args.rend(); ++it) {
(*it)->Generate(Code);
TypeIDexp.push_back(CheckType(((SymProcedure*)((ExprIdent*)Left)->Sym)->Table, Position()).GetTypeID(*it));
}
string format = "\'";
for (auto it = TypeIDexp.rbegin(); it != TypeIDexp.rend(); ++it) {
switch (*it) {
case TypeID_Integer:
case TypeID_Pointer:
format += "%d";
break;
case TypeID_Char:
format += "%c";
break;
case TypeID_String:
format += "%s";
break;
case TypeID_Double:
format += "%f";
break;
}
}
Code->Add(Push, Code->AddFormat(format += argc == argc_writeln ? "\', 0xA, 0x0" : "\', 0x0"));
Code->Add(Call, "_printf");
int Size = 0;
for (auto it = Args.begin(); it < Args.end(); ++it) {
Size += (*it)->GetSize();
}
Code->Add(Add, ESP, Size + 4);
}
void ExprFunction::Generate(Asm_Code* Code, ArgState State) {
SymCall* LSym = (SymCall*)((ExprIdent*)Left)->Sym;
int argc = LSym->argc;
if (argc >= 0) {
if (LSym->Section == DeclFunction) {
Code->Add(Sub, ESP, LSym->Table->Symbols[argc - 1]->GetSize());
}
Code->Add(Mov, EAX, "depth");
Code->Add(Mov, addr, EAX, 4 * Code->depth, EBP);
for (int i = 0; i < Args.size(); ++i) {
Args[i]->Generate(Code, ((SymIdent*)(LSym->Table->Symbols[i]))->State);
}
Code->Add(Call, LSym->GenerateName());
}
if (argc == argc_write || argc == argc_writeln){
this->GenerateWrite(Code, argc);
}
}
void ExprInitList::Generate(Asm_Code* Code, ArgState State) {
for (int i = List.size() - 1; i >= 0; --i) {
List[i]->TypeID = TypeID;
List[i]->Generate(Code);
}
}
void ExprPointer::Generate(Asm_Code* Code, ArgState State) {
Exp->Generate(Code, RValue);
}
void ExprDereference::Generate(Asm_Code* Code, ArgState State) {
Exp->Generate(Code, RValue);
}
string ExprConst::GenerateInitList() {
return Value.Source;
}
string ExprBoolConst::GenerateInitList() {
return _stricmp(Value.Source.c_str(), "true") == 0 ? "1" : "0";
}
string ExprInitList::GenerateInitList() {
string Ans = List[0]->GenerateInitList();
for (int i = 1; i < List.size(); ++i) {
Ans += ", " + List[i]->GenerateInitList();
}
return Ans;
}
int ExprBinOp::GetSize() {
return max(Left->GetSize(), Right->GetSize());
}
int ExprUnarOp::GetSize() {
return Exp->GetSize();
}
int ExprConst::GetSize() {
return 4;
}
int ExprDoubleConst::GetSize() {
return 8;
}
int ExprIdent::GetSize() {
return Sym->GetSize();
}
pair<int, int> ExprIdent::GetBound(int depth) {
Symbol* SymId = Sym->GetType();
for (int i = 0; i < depth - 1; ++i) {
SymId = SymId->GetType();
}
return make_pair(((SymDynArray*)SymId)->GetLow(), ((SymDynArray*)SymId)->GetHigh());
}
int ExprArrayIndex::GetSize() {
auto Bound = Left->GetBound(1);
int Size = Left->GetSize();
return Left->GetSize() / (Bound.second - Bound.first + 1);
}
pair<int, int> ExprArrayIndex::GetBound(int depth) {
return Left->GetBound(depth + 1);
}
int ExprFunction::GetSize() {
return Left->GetSize();
}
pair<int, int> ExprFunction::GetBound(int depth) {
return Left->GetBound(depth);
}
int ExprRecord::GetSize() {
return Right->GetSize();
}
pair<int, int> ExprRecord::GetBound(int depth) {
Symbol* SymId = Right->GetType();
for (int i = 0; i < depth - 1; ++i) {
SymId = SymId->GetType();
}
return make_pair(((SymDynArray*)SymId)->GetLow(), ((SymDynArray*)SymId)->GetHigh());
}
int ExprInitList::GetSize() {
return List[0]->GetSize();
}
int ExprDereference::GetSize() {
return Exp->GetSize();
}
pair<int, int> ExprDereference::GetBound(int depth) {
return Exp->GetBound(depth);
}