-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.cpp
326 lines (293 loc) · 7.48 KB
/
obj.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
#include "obj.h"
#include "symbol.h"
#include "symtab.h"
#include "armplat.h"
#define emit(args...) arm_code.push_back(new Arm(args))
Arm::Arm(string op, string res, string arg1, string arg2, string addition)
{
dead = false;
this->op = op;
this->res = res;
this->arg1 = arg1;
this->arg2 = arg2;
this->addition = addition;
}
Arm::~Arm()
{}
void Arm::setDead()
{
dead = true;
}
string Arm::getArmCode()
{
string code = "";
if(dead) return code;
if(op == "") return code;
code += op;
if(res != "") code += " " + res;
if(arg1 != "") code += ", " + arg1;
if(arg2 != "") code += ", " + arg2;
if(addition != "") code += "; " + addition;
return code;
}
string Obj::immNum(int num, bool f)
{
stringstream ss;
string s = "";
ss << num;
if(f) s = "#" + ss.str();
else s = ss.str();
return s;
}
Obj::Obj()
{
arm_code.clear();
}
Obj::~Obj()
{
for(auto code: arm_code)
delete code;
}
void Obj::objCode(string op)
{
emit(op);
}
void Obj::objCode(string op, string res)
{
emit(op, res);
}
void Obj::objCode(string op, string res, string arg1)
{
emit(op, res, arg1);
}
void Obj::objCode(string op, string res, string arg1, string arg2)
{
emit(op, res, arg1, arg2);
}
void Obj::ldrFake(string reg, string name)
{
emit("ldr", reg, "=" + name);
}
void Obj::ldrImm(string reg, int num)
{
if(ARM::validConstExpr(num))
emit("mov", reg, immNum(num));
else
ldrFake(reg, immNum(num, false)); //用ldr伪指令进行加载
}
void Obj::ldrBase(string reg, string baseReg, int offset, bool isChar)
{
string base = baseReg;
if(ARM::validOffset(offset))
base += ", " + immNum(offset);
else
{
ldrImm(reg, offset);
base += ", " + reg;
}
base = "[" + base + "]";
if(isChar) emit("ldrb", reg, base);
else emit("ldr", reg, base);
}
void Obj::ldrVar(string reg, Var* var)
{
if(!var) return;
if(var->isConst()) //常量加载
{
if(var->isBaseType()) //数字或者字符
ldrImm(reg, var->getVal());
else //字符串
ldrFake(reg, var->getName());
}
else //变量加载
{
//TODO 寄存器优化
bool isGlb = !(var->getOffset());
bool isVar = !var->isArr();
if(isGlb) //全局变量通过数据段查找
{
ldrFake(reg, var->getName());
if(isVar) ldrBase(reg, reg, 0, var->isChar());
}
else //局部变量通过fp查找
{
if(isVar)
ldrBase(reg, "fp", var->getOffset(), var->isChar());
else
leaStack(reg, var->getOffset());
}
}
}
void Obj::ldrLeaVar(string reg, Var* var)
{
int offset = var->getOffset();
if(!offset) //全局变量
ldrFake(reg, var->getName());
else
leaStack(reg, var->getOffset());
}
void Obj::ldrArg(Fun* fun)
{
vector<Var* > args = fun->getPara();
//TODO 寄存器优化涉及
}
void Obj::strBase(string reg, string baseReg, string tmpReg, int offset, bool isChar)
{
//printf("I got aabab!"); system("pause");
string base = baseReg;
if(ARM::validOffset(offset))
base += ", " + immNum(offset);
else
{
ldrImm(tmpReg, offset);
base += ", " + reg;
}
//printf("I got hereeeeeeee!"); system("pause");
base = "[" + base + "]";
if(isChar) emit("strb", reg, base);
else emit("str", reg, base);
}
void Obj::strVar(string reg, string tmpReg, Var* var)
{
//保存寄存器值到变量
if(!var) return;
//TODO 寄存器优化
bool isGlb = !(var->getOffset());
if(isGlb) //全局变量通过数据段查找
{
ldrFake(tmpReg, var->getName());
strBase(reg, tmpReg, tmpReg, 0, var->isChar());
}
else //局部变量通过fp查找
strBase(reg, "fp", tmpReg, var->getOffset(), var->isChar());
//TODO 分割优化
}
void Obj::leaStack(string reg, int offset)
{
if(!ARM::validConstExpr(offset))
{
ldrImm(reg, offset);
emit("add", reg, "fp", reg);
}
else
emit("add", reg, "fp", immNum(offset));
}
void Obj::allocStack(Fun* fun, string tmpReg)
{
//TODO 考虑重定向
int base = ARM::stackBase;
int offset = fun->getMaxDep() - base;
//printf("++++++++++max %d+++++++++", fun->getMaxDep());
// emit("stmfd", "sp!", "{fp,ip,lr,pc}");
// emit("sub", "fp", "ip", "#4");
if(!ARM::validConstExpr(offset))
{
ldrImm(tmpReg, offset);
emit("sub", "sp", "sp", tmpReg);
}
else
emit("sub", "sp", "sp", immNum(offset));
}
void Obj::callFun(Fun* fun, string tmpReg)
{
string name = fun->getName();
emit("bl", name);
int offset = fun->getPara().size() * 4;
if(!ARM::validConstExpr(offset))
{
ldrImm(tmpReg, offset);
emit("add", "sp", "sp", tmpReg);
}
else
emit("add", "sp", "sp", immNum(offset));
}
void Obj::callLibFun(string fun, string res2, string reg0, string reg1)
{
emit("stmfd", "sp!", "{r0-r7}");
emit("mov", "r0", reg0);
emit("mov", "r1", reg1);
emit("bl", fun);
emit("mov", res2, "r0");
emit("ldmfd", "sp!", "{r0-r7}");
}
void Obj::logicAnd(string resReg, string reg1, string reg2)
{
emit("cmp", reg1, "#0");
emit("moveq", resReg, "#0");
emit("movne", resReg, "#1");
emit("cmpeq", reg2, "#0");
emit("moveq", resReg, "#0");
}
void Obj::logicOr(string resReg, string reg1, string reg2)
{
emit("cmp", reg1, "#0");
emit("moveq", resReg, "#0");
emit("movne", resReg, "#1");
emit("cmpeq", reg2, "#0");
emit("movne", resReg, "#1");
}
void Obj::logicNot(string resReg, string reg1)
{
// Pro
emit("cmp", reg1, "#0");
emit("moveq", resReg, "#1");
emit("movne", resReg, "#0");
}
void Obj::cmp(string resReg, string reg1, string reg2, string cmp, string fcmp)
{
emit("cmp", reg1, reg2);
emit("mov" + cmp, resReg, "#1");
emit("mov" + fcmp, resReg, "#0");
}
void Obj::label(string name)
{
emit(name, ":");
}
void Obj::comment(string str)
{
emit("#", str);
}
void Obj::nop()
{
emit("");
}
void Obj::printObjCode(FILE* file)
{
for(auto i: arm_code)
{
string code = i->getArmCode();
if(code != "") fprintf(file, "\t%s\n", code.c_str());
}
}
void Obj::initVar(string reg, string tmpReg, Var* var)
{
if(!var->isInitVar()) return;
if(var->isBaseType())
ldrImm(reg, var->getVal());
else
ldrFake(reg, var->getPtrVal());
strVar(reg, tmpReg, var);
}
void Obj::in_out(string reg0, string reg1, string reg2, Var* var, Operator op)
{
int offset = var->getOffset();
//printf("I got !"); system("pause");
if(!offset)
{
if(op == OP_READ) ldrFake(reg0, var->getName());
else ldrVar(reg0, var);
}
else
{
if(op == OP_READ) leaStack(reg0, offset);
else ldrVar(reg0, var);
}
//printf("I got in_out1!"); system("pause");
ldrFake(reg2, SymTab::deciOut->getName());
//printf("I got in_out2!"); system("pause");
emit("mov", reg1, reg0);
if(op == OP_READ) emit("bl", "scanf");
else emit("bl", "printf");
//printf("I got in_out3!"); system("pause");
//Pro 是否需要寄存器保护
}