-
Notifications
You must be signed in to change notification settings - Fork 0
/
intercode.cpp
233 lines (201 loc) · 7.16 KB
/
intercode.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
#include "intercode.h"
#include "genir.h"
#include "symbol.h"
#include "symtab.h"
void Quaternion::init()
{
label = "";
op = OP_NOP;
result = arg1 = arg2 = NULL;
fun = NULL;
target = NULL;
}
Quaternion::Quaternion()
{
init();
this->op = OP_LABEL;
label = GenIR::genLb();
}
Quaternion::~Quaternion()
{}
Quaternion::Quaternion(Operator op, Var* result, Var* arg1, Var* arg2)
{
init();
this->op = op;
this->result = result;
this->arg1 = arg1;
this->arg2 = arg2;
}
Quaternion::Quaternion(Operator op, Fun* fun, Var* result) //函数相关指令
{
init();
this->op = op;
this->fun = fun;
this->result = result;
}
Quaternion::Quaternion(Operator op, Var* arg1) //单参或无参指令
{
init();
this->op = op;
this->arg1 = arg1;
}
Quaternion::Quaternion(Operator op, Quaternion* target, Var* arg1, Var* arg2) //跳转指令
{
this->op = op;
this->target = target;
this->arg1 = arg1;
this->arg2 = arg2;
}
Operator Quaternion::getOp()
{
return op;
}
string Quaternion::getLabel()
{
return label;
}
Fun* Quaternion::getFun()
{
return fun;
}
Var* Quaternion::getResult()
{
return result;
}
Var* Quaternion::getArg1()
{
return arg1;
}
Var* Quaternion::getArg2()
{
return arg2;
}
void Quaternion::setArg1(Var* arg)
{
arg1 = arg;
}
Quaternion* Quaternion::getTarget()
{
return target;
}
void Quaternion::setFirst()
{
isFirst = true;
}
bool Quaternion::getFirst()
{
return isFirst;
}
bool Quaternion::isJmp()
{
if(op == OP_JMP || op == OP_RET || op == OP_RETX)
return true;
return false;
}
bool Quaternion::isJmpCond()
{
if(op == OP_JF || op == OP_JT || op == OP_JNE)
return true;
return false;
}
bool Quaternion::isDec()
{
return op == OP_DEC;
}
Block* Quaternion::getBlock()
{
return block;
}
void Quaternion::setBlock(Block* blk)
{
block = blk;
}
void Quaternion::replace(Operator op, Quaternion* target, Var* arg1, Var* arg2)
{
this->op = op;
this->target = target;
this->arg1 = arg1;
this->arg2 = arg2;
}
void Quaternion::replace(Operator op, Var* result, Var* arg1, Var* arg2)
{
this->op = op;
this->result = result;
this->arg1 = arg1;
this->arg2 = arg2;
}
void Quaternion::toString()
{
//注意 string用%s输出时要用内置的c_str()转成char*来输出
if(op == OP_LABEL) printf("OP_LABEL %s", label.c_str());
else if(op == OP_ENTRY) printf("OP_ENTRY %s", fun->getName().c_str());
else if(op == OP_EXIT) printf("OP_EXIT %s", fun->getName().c_str());
else if(op == OP_DEC) printf("OP_DEC var is "), arg1->info();
else if(op == OP_ASSIGN) printf("OP_ASSIGN "), result->info(), printf("="), arg1->info();
else if(op == OP_ADD) printf("OP_ADD "), result->info(), printf("="), arg1->info(), printf("+"), arg2->info();
else if(op == OP_SUB) printf("OP_SUB "), result->info(), printf("="), arg1->info(), printf("-"), arg2->info();
else if(op == OP_MUL) printf("OP_MUL "), result->info(), printf("="), arg1->info(), printf("*"), arg2->info();
else if(op == OP_DIV) printf("OP_DIV "), result->info(), printf("="), arg1->info(), printf("/"), arg2->info();
else if(op == OP_MOD) printf("OP_MOD "), result->info(), printf("="), arg1->info(), printf("mod"), arg2->info();
else if(op == OP_NEG) printf("OP_NEG "), result->info(), printf("="), printf("-"), arg1->info();
else if(op == OP_AND) printf("OP_AND "), result->info(), printf("="), arg1->info(), printf("&"), arg2->info();
else if(op == OP_OR) printf("OP_OR "), result->info(), printf("="), arg1->info(), printf("|"), arg2->info();
else if(op == OP_XOR) printf("OP_XOR "), result->info(), printf("="), arg1->info(), printf("^"), arg2->info();
else if(op == OP_INV) printf("OP_INV "), result->info(), printf("="), printf("~"), arg1->info();
else if(op == OP_AAND) printf("OP_AADD "), result->info(), printf("="), arg1->info(), printf("&&"), arg2->info();
else if(op == OP_OOR) printf("OP_OOR "), result->info(), printf("="), arg1->info(), printf("||"), arg2->info();
else if(op == OP_GE) printf("OP_GE "), result->info(), printf("="), arg1->info(), printf(">="), arg2->info();
else if(op == OP_GT) printf("OP_GT "), result->info(), printf("="), arg1->info(), printf(">"), arg2->info();
else if(op == OP_LE) printf("OP_LE "), result->info(), printf("="), arg1->info(), printf("<="), arg2->info();
else if(op == OP_LT) printf("OP_LT "), result->info(), printf("="), arg1->info(), printf("<"), arg2->info();
else if(op == OP_EQU) printf("OP_EQU "), result->info(), printf("="), arg1->info(), printf("=="), arg2->info();
else if(op == OP_NEQU) printf("OP_NEQU "), result->info(), printf("="), arg1->info(), printf("!="), arg2->info();
else if(op == OP_NOT) printf("OP_NOT "), result->info(), printf("="), printf("!"), arg1->info();
else if(op == OP_LEA) printf("OP_LEA "), result->info(), printf("="), printf("&"), arg1->info();
else if(op == OP_SET) printf("OP_SET "), printf("*"), arg1->info(), printf("="), result->info();
else if(op == OP_GET) printf("OP_GET "), result->info(), printf("="), printf("*"), arg1->info();
else if(op == OP_JMP) printf("OP_JMP "), printf("goto %s", target->label.c_str());
else if(op == OP_JNE) printf("OP_JNE "), printf("if "), arg1->info(), printf("!="), arg2->info(), printf(" then goto %s", target->label.c_str());
else if(op == OP_JT) printf("OP_JT "), printf("if "), arg1->info(), printf(" then goto %s", target->label.c_str());
else if(op == OP_JF) printf("OP_JF "), printf("if not "), arg1->info(), printf(" then goto %s", target->label.c_str());
else if(op == OP_ARG) printf("OP_ARG push arg "), arg1->info();
else if(op == OP_PROC) printf("OP_PROC call function %s", fun->getName().c_str());
else if(op == OP_CALL) printf("OP_CALL "), result->info(), printf("=%s", fun->getName().c_str());
else if(op == OP_RET) printf("OP_RET return point is %s", target->label.c_str());
else if(op == OP_RETX) printf("OP_RETX return value is "), arg1->info(), printf(" and the return point is %s", target->label.c_str());
else if(op == OP_READ) printf("OP_READ read the "), arg1->info();
else if(op == OP_WRITE) printf("OP_WRITE write the "), arg1->info();
printf("\n");
}
InterCode::InterCode()
{
intercode.clear();
}
InterCode::~InterCode()
{
for(auto i: intercode)
delete i;
}
void InterCode::addQuaternion(Quaternion* code)
{
intercode.push_back(code);
}
vector<Quaternion*> InterCode::getInterCode()
{
return intercode;
}
void InterCode::makeFirst()
{
intercode[0]->setFirst(); //Enter
intercode[1]->setFirst(); //第一条
intercode[intercode.size() - 1]->setFirst(); //Exit
for(int i = 1; i < intercode.size(); i ++)
{
Quaternion* code = intercode[i];
if(code->isJmp() || code->isJmpCond())
{
code->getTarget()->setFirst(); //标签是首指令
intercode[i + 1]->setFirst(); //跳转指令下一条是首指令
}
}
}