-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3ac.hpp
408 lines (366 loc) · 8.53 KB
/
3ac.hpp
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
#ifndef HOLEYC_3AC_HPP
#define HOLEYC_3AC_HPP
#include <assert.h>
#include <vector>
#include "list"
#include "map"
#include "err.hpp"
#include "symbol_table.hpp"
namespace holeyc{
class TypeAnalysis;
class Procedure;
class IRProgram;
class Label{
public:
Label(std::string nameIn){
this->name = nameIn;
}
std::string toString(){
return "lbl_" + this->name;
}
std::string getName(){
return name;
}
private:
std::string name;
};
enum OpdWidth{
ADDR, QUADWORD, BYTE
};
class Opd{
public:
Opd(OpdWidth widthIn) : myWidth(widthIn){}
virtual std::string valString() = 0;
virtual std::string locString() = 0;
virtual OpdWidth getWidth(){ return myWidth; }
virtual void genLoad(std::ostream& out, std::string dstReg) = 0;
virtual void genStore(std::ostream& out, std::string srcReg) = 0;
static OpdWidth width(const DataType * type){
if (const BasicType * basic = type->asBasic()){
if (basic->isChar()){ return BYTE; }
if (basic->isBool()){ return BYTE; }
return QUADWORD;
} else if (const PtrType * ptr = type->asPtr()){
return ADDR;
}
assert(false);
}
virtual std::string getMemoryLoc() = 0;
private:
OpdWidth myWidth;
};
class SymOpd : public Opd{
public:
virtual std::string valString() override{
return "[" + mySym->getName() + "]";
}
virtual std::string locString() override{
return mySym->getName();
}
std::string getName(){
return mySym->getName();
}
const SemSymbol * getSym(){ return mySym; }
virtual void genLoad(std::ostream& out, std::string loc)
override;
virtual void genStore(std::ostream& out, std::string loc)
override;
virtual void setMemoryLoc(std::string loc){
myLoc = loc;
}
virtual std::string getMemoryLoc(){
return myLoc;
}
private:
//Private Constructor
SymOpd(SemSymbol * sym, OpdWidth width)
: Opd(width), mySym(sym) {}
SemSymbol * mySym;
friend class Procedure;
friend class IRProgram;
std::string myLoc;
};
class LitOpd : public Opd{
public:
LitOpd(std::string valIn, OpdWidth width)
: Opd(width), val(valIn){ }
virtual std::string valString() override{
return val;
}
virtual std::string locString() override{
throw InternalError("Tried to get location of a constant");
}
virtual void genLoad(std::ostream& out, std::string loc)
override;
virtual void genStore(std::ostream& out, std::string loc)
override;
std::string getMemoryLoc() { return "$" + val; }
private:
std::string val;
};
class AuxOpd : public Opd{
public:
AuxOpd(std::string nameIn, OpdWidth width)
: Opd(width), name(nameIn) { }
virtual std::string valString() override{
return "[" + getName() + "]";
}
virtual std::string locString() override{
return getName();
}
std::string getName(){
return name;
}
virtual void genLoad(std::ostream& out, std::string loc)
override;
virtual void genStore(std::ostream& out, std::string loc)
override;
virtual void setMemoryLoc(std::string loc){
myLoc = loc;
}
virtual std::string getMemoryLoc(){
return myLoc;
}
private:
std::string name;
std::string myLoc = "UNINIT";
};
enum BinOp {
ADD, SUB, DIV, MULT, OR, AND, EQ, NEQ, LT, GT, LTE, GTE
};
enum UnaryOp{
NEG, NOT
};
enum Intrinsic {
OUTPUT, INPUT
};
class Quad{
public:
Quad();
void addLabel(Label * label);
virtual std::string repr() = 0;
std::string commentStr();
virtual std::string toString(bool verbose=false);
void setComment(std::string commentIn);
virtual void codegenX64(std::ostream& out) = 0;
void codegenLabels(std::ostream& out);
private:
std::string myComment;
std::list<Label *> labels;
};
class BinOpQuad : public Quad{
public:
BinOpQuad(Opd * dstIn, BinOp opIn, Opd * src1In, Opd * src2In);
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
Opd * dst;
BinOp op;
Opd * src1;
Opd * src2;
};
class UnaryOpQuad : public Quad {
public:
UnaryOpQuad(Opd * dstIn, UnaryOp opIn, Opd * srcIn);
std::string repr() override ;
void codegenX64(std::ostream& out) override;
private:
Opd * dst;
UnaryOp op;
Opd * src;
};
class AssignQuad : public Quad{
public:
AssignQuad(Opd * dstIn, Opd * srcIn)
: dst(dstIn), src(srcIn)
{ }
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
Opd * dst;
Opd * src;
};
class LocQuad : public Quad {
public:
LocQuad(Opd * srcIn, Opd * tgtIn, bool srcLocIn)
: src(srcIn), tgt(tgtIn), srcLoc(srcLocIn){ }
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
Opd * src;
Opd * tgt;
bool srcLoc;
};
class JmpQuad : public Quad {
public:
JmpQuad(Label * tgtIn);
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
Label * tgt;
};
class JmpIfQuad : public Quad {
public:
JmpIfQuad(Opd * cndIn, Label * tgtIn);
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
Opd * cnd;
Label * tgt;
};
class NopQuad : public Quad {
public:
NopQuad();
std::string repr() override;
void codegenX64(std::ostream& out) override;
};
class IntrinsicQuad : public Quad {
public:
IntrinsicQuad(Intrinsic intrinsic, Opd * arg);
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
Opd * myArg;
Intrinsic myIntrinsic;
};
class CallQuad : public Quad{
public:
CallQuad(SemSymbol * calleeIn);
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
SemSymbol * callee;
};
class EnterQuad : public Quad{
public:
EnterQuad(Procedure * proc);
virtual std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
Procedure * myProc;
};
class LeaveQuad : public Quad{
public:
LeaveQuad(Procedure * proc);
virtual std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
Procedure * myProc;
};
class SetArgQuad : public Quad{
public:
SetArgQuad(size_t indexIn, Opd * opdIn);
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
size_t index;
Opd * opd;
};
class GetArgQuad : public Quad{
public:
GetArgQuad(size_t indexIn, Opd * opdIn);
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
size_t index;
Opd * opd;
};
class SetRetQuad : public Quad{
public:
SetRetQuad(Opd * opdIn);
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
Opd * opd;
};
class GetRetQuad : public Quad{
public:
GetRetQuad(Opd * opdIn);
std::string repr() override;
void codegenX64(std::ostream& out) override;
private:
Opd * opd;
};
class Procedure{
public:
Procedure(IRProgram * prog, std::string name);
void addQuad(Quad * quad);
Quad * popQuad();
IRProgram * getProg();
std::list<SymOpd *> getFormals() { return formals; }
std::list<SymOpd *> getLocals(){
std::list<SymOpd *> locs;
for(auto key_val_pair : locals)
locs.push_back(key_val_pair.second);
return locs;
}
std::list<AuxOpd *> getTemps(){ return temps; }
holeyc::Label * makeLabel();
void gatherLocal(SemSymbol * sym);
void gatherFormal(SemSymbol * sym);
SymOpd * getSymOpd(SemSymbol * sym);
AuxOpd * makeTmp(OpdWidth width);
std::string toString(bool verbose=false);
std::string getName();
holeyc::Label * getLeaveLabel();
void toX64(std::ostream& out);
size_t localsSize() const;
size_t numTemps() const;
std::list<Quad *> * getQuads(){
return bodyQuads;
}
private:
void allocLocals();
EnterQuad * enter;
Quad * leave;
Label * leaveLabel;
IRProgram * myProg;
std::map<SemSymbol *, SymOpd *> locals;
std::list<AuxOpd *> temps;
std::list<SymOpd *> formals;
std::list<Quad *> * bodyQuads;
std::string myName;
size_t maxTmp;
};
class IRProgram{
public:
IRProgram(TypeAnalysis * taIn) : ta(taIn){}
Procedure * makeProc(std::string name);
std::list<Procedure *> getProcs() { return procs; }
Label * makeLabel();
Opd * makeString(std::string val);
void gatherGlobal(SemSymbol * sym);
SymOpd * getGlobal(SemSymbol * sym);
OpdWidth opWidth(ASTNode * node);
std::list<SymOpd *> getGlobals(){
std::list<SymOpd *> gbls;
for(auto key_val_pair : globals)
gbls.push_back(key_val_pair.second);
return gbls;
}
std::list<AuxOpd *> getAuxForStrings(){
std::list<AuxOpd *> auxs;
for(auto key_val_pair : strings)
auxs.push_back(key_val_pair.first);
return auxs;
}
std::vector<std::string> getStrings(){
std::vector<std::string> strs;
for(auto key_val_pair : strings)
strs.push_back(key_val_pair.second);
return strs;
}
std::string toString(bool verbose=false);
void toX64(std::ostream& out);
private:
TypeAnalysis * ta;
size_t max_label = 0;
size_t str_idx = 0;
std::list<Procedure *> procs;
HashMap<AuxOpd *, std::string> strings;
std::map<SemSymbol *, SymOpd *> globals;
void datagenX64(std::ostream& out);
void allocGlobals();
};
}
#endif