-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntermediateCode.cpp
175 lines (165 loc) · 5.87 KB
/
IntermediateCode.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
//
// Created by hzt on 2023/11/11.
//
#include "IntermediateCode.h"
vector<int> strings;
vector<ICode *> mainICodes ;
map<string, vector<ICode *>> otherFuncICodes;
vector<ICode *>globalDef ;
vector<IEntry *> IEntries;
int tempMemoryAddressTop = 268500992;
int IEntry::generateId() {
static int id_generate = 0;
return id_generate++;
}
IEntry::IEntry(){
this->Id = generateId();
this->startAddress = tempMemoryAddressTop;
this->name = "@"+ to_string(this->Id);
this->values_Id = new vector<int>;
this->values = new vector<int>;
this->imm = 0;
tempMemoryAddressTop += 4;
IEntries.push_back(this);
// iEntry->address
}
//FIXME数组的定义时的IEntry的生成 type = 0 只有地址时才是1
IEntry::IEntry(int length){
this->Id = generateId();
this->startAddress = tempMemoryAddressTop;
this->values_Id = new vector<int>;
this->values = new vector<int>;
this->name = "@"+ to_string(this->Id);
this->imm = 0;
tempMemoryAddressTop += length*4;
IEntries.push_back(this);
// iEntry->address
}
string IntermediateCode::iCode2str(ICode *iCode) {
IEntry *var1 = iCode->src1;
IEntry *var2 = iCode->src2;
IEntry *var3 = iCode->dst;
switch (iCode->type) {
case VAR_Def_Has_Value:
return "define var_has_value_or: id, " + to_string(var1->Id)+ " value:"+ to_string(IEntries.at(var1->values_Id->at(0))->imm);//非const都先直接用IEntry存起来 后端生成时再解包
case VAR_Def_No_Value:
return "define var_no_value : id, " + to_string(var1->Id);
case ARRAY_VAR_Def_Has_Value:
return "define var_array_has_value : id, " + to_string(var1->Id);
case ARRAY_Def_No_Value:
return "define var_array_has_value : id, " + to_string(var1->Id);
case Const_Def_Has_Value:
return "define const_has_value : id, " + to_string(var1->Id) + " value: " + to_string(var1->values->at(0));
case ARRAY_CONST_Def_Has_Value:
return "define const_array_has_value : id," + to_string(var1->Id);
case Add:
return "Add :" + var1->name + "type:" + to_string(var1->type) ;
break;
case Sub:
return "Sub :" + var1->name + "type:" + to_string(var1->type) ;
break;
case Mult:
return "Mult :" + var1->name + "type:" + to_string(var1->type) ;
break;
case Div:
return "Div :" + var1->name + "type:" + to_string(var1->type) ;
break;
case Mod:
return "Mod :" + var1->name + "type:" + to_string(var1->type) ;
break;
case GetInt:
return "GetInt :" + var1->name + "type:" + to_string(var1->type) ;
break;
case GetArrayElement:
return "GetArrayElement :" + var1->name + "type:" + to_string(var1->type) ;
break;
case FuncCall:
return "FuncCall :" + var1->name + "type:" + to_string(var1->type) ;
break;
case FuncDef:
return " FuncDef:" + var1->name + "type:" + to_string(var1->type) ;
break;
}
}
void IntermediateCode::addDef(bool isGlobal, IntermediateCodeType type, IEntry *src1, IEntry *src2, IEntry *dst) {
iCode = new ICode();
// dst = new IEntry;//最后翻译时 会根据约定的IEntryType去使用IEntry
iCode->type = type;
iCode->src1 = src1;
iCode->src2 = src2;
iCode->dst = dst;
if (isGlobal) {
globalDef.push_back(iCode);
} else {
if (isInOtherFunc) {
if (otherFuncICodes.find(funcLabel) == otherFuncICodes.end()) {
otherFuncICodes.insert(pair<string, vector<ICode *>>(funcLabel, vector<ICode *>()));
}
otherFuncICodes.at(funcLabel).push_back(iCode);
} else {
mainICodes.push_back(iCode);
}
}
}
void IntermediateCode::addICode(IntermediateCodeType type, IEntry *src1, IEntry *src2, IEntry *dst) {
iCode = new ICode();
iCode->type = type;
iCode->src1 = src1;
iCode->src2 = src2;
iCode->dst = dst;
if (dst){
dst->canGetValue = false;
}
if (isInOtherFunc) {
if (otherFuncICodes.find(funcLabel) == otherFuncICodes.end()) {
otherFuncICodes.insert(pair<string, vector<ICode *>>(funcLabel, vector<ICode *>()));
}
otherFuncICodes.at(funcLabel).push_back(iCode);
} else {
mainICodes.push_back(iCode);
}
}
//TODO:dst需要运行时才知道value了 dst确保进入时已经new过
void IntermediateCode::addICode(IntermediateCodeType type, int src1, IEntry *src2, IEntry *dst) {
iCode = new ICode();
iCode->type = type;
auto* s1 = new IEntry;
s1->canGetValue = true;
s1->imm = src1;
iCode->src1 = s1;
iCode->src2 = src2;
iCode->dst = dst;
if (dst){
dst->canGetValue = false;
}
if (isInOtherFunc){
if (otherFuncICodes.find(funcLabel) == otherFuncICodes.end()){
otherFuncICodes.insert(pair<string,vector<ICode*>>(funcLabel,vector<ICode*>()));
}
otherFuncICodes.at(funcLabel).push_back(iCode);
}else{
mainICodes.push_back(iCode);
}
}
//void IntermediateCode::debug_print() {
// output<<"#全局变量"<<endl;
// for (auto def:globalDef) {
// //输出iCode的类型、和非空的IEntry
// output<< iCode2str(def) << endl;
// }
// output<<"#主函数"<<endl;
// //print mainICodes
// for (auto item:mainICodes) {
// //输出iCode的类型、和非空的IEntry
// output << iCode2str(item) << endl;
// }
// output<<"#其他自定义函数"<<endl;
// //按顺序输出otherFuncICodes的每一个FuncLabel的ICodes
// for (const auto& func:otherFuncICodes) {
// output<<"#自定义函数名:"+func.first<<endl;
// for (auto item:func.second) {
// output << iCode2str(item) << endl;
//
// }
// }
//}