-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymbol.cpp
More file actions
277 lines (231 loc) · 7.15 KB
/
symbol.cpp
File metadata and controls
277 lines (231 loc) · 7.15 KB
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
#include "symbol.h"
#include "statement.h"
#include <algorithm>
#include "symtable.h"
#define alignment(size) ((size) / 4 * 4 + ((size) % 4 == 0 ? 0 : 4))
Symbol::Symbol(DeclSection Section, string Name) : Name(Name), Section(Section){ /*transform(this->Name.begin(), this->Name.end(), this->Name.begin(), toupper);*/ }
Symbol::Symbol(Symbol* Sym) : Name(Sym->Name), Section(Sym->Section){}
SymLabel::SymLabel(string Name) : Symbol(DeclLabel, Name){}
SymType::SymType(string Name, Symbol* Type) : Symbol(DeclType, Name), Type(Type), TypeID(TypeID_BadType){}
SymType::SymType(string Name, MyTypeID TypeID) : Symbol(DeclType, Name), Type(nullptr), TypeID(TypeID){}
SymPointer::SymPointer(string Name, Symbol* Type) : Symbol(DeclType, Name), Type(Type){}
SymIdent::SymIdent(DeclSection Section, string Name, Expr* InitExp, Symbol* Type, ArgState State) : Symbol(Section, Name), InitExp(InitExp), Type(Type), State(State), isLocal(true) {}
SymVar::SymVar(string Name, Expr* InitExp, Symbol* Type, ArgState State) : SymIdent(DeclVar, Name, InitExp, Type, State){}
SymConst::SymConst(string Name, Expr* InitExp, Symbol* Type, ArgState State) : SymIdent(DeclConst, Name, InitExp, Type, State){}
SymDynArray::SymDynArray(Symbol* Type) : Type(Type), TypeID(TypeID_Array), Symbol(DeclType){}
SymArray::SymArray(Symbol* Type, int Left, int Right) : SymDynArray(Type), Left(Left), Right(Right) {}
SymStringType::SymStringType(int Length) : Length(Length), TypeID(TypeID_String){}
SymCall::SymCall(DeclSection Section, string Name, SymTable* Table, Statement* Stmt, int argc) : Symbol(Section, Name), Table(Table), Stmt(Stmt), argc(argc){}
SymFunction::SymFunction(string Name, SymTable* Table, Statement* Stmt, int argc, Symbol* Type): SymCall(DeclFunction, Name, Table, Stmt, argc), Type(Type){}
SymProcedure::SymProcedure(string Name, SymTable* Table, Statement* Stmt, int argc) : SymCall(DeclProcedure, Name, Table, Stmt, argc){}
SymRecord::SymRecord(SymTable* Table, string Name, int argc) : Symbol(DeclRecord, Name), Table(Table), argc(argc) {}
/*class SymTable {
public:
vector<Symbol*> Symbols;
void Print(int Spaces);
//int GetVariableSize();
void GenerateLocalVariables(Asm_Code* Code, int first);
};*/
bool Symbol::isSame(string Value){
return Name.length() == Value.length() && _strnicmp(Name.c_str(), Value.c_str(), Name.length()) == 0;
}
bool Symbol::Compare(Symbol* Sym) {
return false;
}
void SymLabel::Print(int Spaces){
print_indent(Spaces);
cout << "Label\t" << Name << endl;
}
void SymType::Print(int Spaces){
if (Name.size() == 0) { /* identifier is const and identifier have no TypeName but identifier has auto detect type */
return;
}
print_indent(Spaces);
cout << "Type" << indent << Name << endl;
if (Type != nullptr)
Type->Print(Spaces + 1);
}
void SymPointer::Print(int Spaces) {
print_indent(Spaces);
cout << "Type" << indent << Name << endl;
Type->Print(Spaces + 1);
}
void SymDynArray::Print(int Spaces){
print_indent(Spaces);
cout << "DynArray" << indent << Name << endl;
Type->Print(Spaces);
}
void SymArray::Print(int Spaces){
print_indent(Spaces);
cout << "Array" << indent << Left << indent << Right << endl;
Type->Print(Spaces);
}
void SymStringType::Print(int Spaces){
print_indent(Spaces);
cout << "string" << indent;
Length != -1 ? cout << Length << endl : cout << endl;
}
void SymVar::Print(int Spaces){
print_indent(Spaces);
cout << "Var" << indent << Name << endl;
Type->Print(Spaces + 1);
if (InitExp != nullptr) {
InitExp->Print(Spaces + 1);
}
}
void SymConst::Print(int Spaces){
print_indent(Spaces);
cout << "Const" << indent << Name << endl;
if (Type != nullptr) {
Type->Print(Spaces + 1);
}
InitExp->Print(Spaces + 1);
}
void SymRecord::Print(int Spaces) {
print_indent(Spaces);
cout << "Record" << endl;
this->Table->Print(Spaces + 1);
print_indent(Spaces);
cout << "End" << endl;
}
void SymFunction::Print(int Spaces) {
print_indent(Spaces);
cout << "Function" << indent << Name << indent << argc << endl;
Table->Print(Spaces + 1);
Stmt->Print(Spaces + 1);
}
void SymProcedure::Print(int Spaces) {
print_indent(Spaces);
cout << "Procedure" << indent << Name << indent << argc << endl;
Table->Print(Spaces + 1);
Stmt->Print(Spaces + 1);
}
int SymPointer::GetSize() {
return 4;
}
int SymStringType::GetSize() {
return 4;
}
int SymRecord::GetSize() {
int size = 0;
for (auto it = Table->Symbols.begin(); it < Table->Symbols.end(); ++it) {
size += ((SymIdent*)*it)->GetSize();
}
return alignment(size);
}
int SymDynArray::GetSize() {
return 4;
}
int SymArray::GetSize() {
return (Right - Left + 1)*Type->GetSize();
}
int SymDynArray::GetLow() {
return 0;
}
int SymDynArray::GetHigh() {
return 0;
}
int SymArray::GetLow() {
return Left;
}
int SymArray::GetHigh() {
return Right;
}
int SymType::GetSize() {
switch (TypeID) {
case TypeID_Integer:
return 4;
case TypeID_Double:
return 8;
case TypeID_Char:
case TypeID_Boolean:
return alignment(1);
default:
if (Type == nullptr) {
return 0;
}
return Type->GetSize();
}
}
string SymType::GenerateName() {
switch (TypeID) {
case TypeID_Integer:
return "dd";
case TypeID_Char:
return "db";
case TypeID_Boolean:
return "db";
case TypeID_Double:
return "dq";
}
}
string SymPointer::GenerateName() {
return "dd";
}
string SymCall::GenerateName() {
return "c_" + to_string(argc) + "_" + Name + "_" + to_string((int)&argc);
}
void SymCall::Generate(Asm_Code* Code) {
Asm_Function* Asm_Func = new Asm_Function(GenerateName(), vector<Asm_Cmd*>(), 0);
Asm_Code* FuncCode = new Asm_Code();
FuncCode->Fmts = Code->Fmts;
FuncCode->ConstStrings = Code->ConstStrings;
FuncCode->Add(Push, EBP);
FuncCode->Add(Mov, EBP, ESP);
FuncCode->depth = Code->depth + 1;
auto size = Table->GenerateLocalVariables(FuncCode, Section == DeclFunction ? argc - 1 : argc, argc, FuncCode->depth);
Code->max_depth = max(FuncCode->depth, Code->depth);
Stmt->Generate(FuncCode);
FuncCode->Add(Add, ESP, to_string(size.first));
FuncCode->Add(Pop, EBP);
FuncCode->Add(Ret, to_string(size.second));
for (auto it = FuncCode->Cmds.begin(); it < FuncCode->Cmds.end(); ++it) {
Asm_Func->Cmds.push_back(*it);
}
for (auto it = FuncCode->Functions.begin(); it < FuncCode->Functions.end(); ++it) {
Asm_Func->Functions.push_back(*it);
}
Code->Add(Asm_Func);
}
string SymIdent::GenerateName() {
return "v_" + Name;
}
void SymIdent::Generate(Asm_Code* Code) {
Code->Add(GenerateName(), Type->GenerateName(), GetInitList());
}
int SymIdent::GetSize() {
if (Type == nullptr) {
return 0;
}
return Type->GetSize();
}
int SymFunction::GetSize() {
return Type->GetSize();
}
string SymArray::GenerateName() {
return "times " + to_string(Right - Left + 1) + " " + Type->GenerateName();
}
string SymRecord::GenerateName() {
return "times " + to_string(GetSize()) + " db";
}
string SymIdent::GetInitList() {
if (InitExp == nullptr) {
return "0";
}
return InitExp->GenerateInitList() + (InitExp->TypeExp != ConstDoubleExp && InitExp->TypeID == TypeID_Double ? ".0" : "");
}
Symbol* SymType::GetType() {
return Type;
}
Symbol* SymIdent::GetType() {
return Type;
}
Symbol* SymDynArray::GetType() {
return Type;
}
Symbol* SymFunction::GetType() {
return Type;
}
Symbol* SymPointer::GetType() {
return Type;
}