-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtacgenerator.h
262 lines (213 loc) · 7.92 KB
/
tacgenerator.h
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
#ifndef TACGENERATOR_H__
#define TACGENERATOR_H__
#include "visitor.h"
#include "tacdef.h"
#include <list>
#include <stack>
#include <optional>
class TacGenerator : public Visitor
{
public:
Tac::TacIR& ir() { return ir_; }
void visit(ASTRoot* node) override;
void visit(FuncDef* node) override;
void visit(VarDef* node) override;
void visit(CompoundDecl*) override {}
void visit(CompoundDef*) override {}
void visit(EnumDef*) override {}
void visit(TypeAlias*) override {}
void visit(FuncDecl*) override {}
SymbolTable& currScope() { return *scopeStack_.top(); }
void pushScope(SymbolTable* s) { scopeStack_.push(s); }
void popScope() { scopeStack_.pop(); }
int addLiteral(Tac::StaticObject const& so) { return ir_.addLiteral(so); }
void addGlobalVar(std::string const& name, Tac::StaticObject const& so)
{
ir_.addGlobalVar(name, so);
}
Tac::Label nextLabel()
{
return Tac::Label(labelNo_++);
}
private:
std::stack<SymbolTable*> scopeStack_;
Tac::TacIR ir_;
int labelNo_ = 1;
};
class FuncGenerator : public Visitor
{
public:
FuncGenerator(TacGenerator& tacgen,
std::string funcName, ListSymtab* params,
std::shared_ptr<Type> retType);
struct TempStackObjectDeallocGuard
{
FuncGenerator& fg_;
explicit TempStackObjectDeallocGuard(FuncGenerator& fg) : fg_(fg) {
assert(fg_.tempStackObjects_.empty());
}
~TempStackObjectDeallocGuard() {
while (!fg_.tempStackObjects_.empty()) {
auto so = fg_.tempStackObjects_.top();
fg_.tempStackObjects_.pop();
fg_.emit({Tac::Dealloca, so});
}
}
};
Tac::Function const& func() const { return currFunction_; };
Tac::Function& yieldFunc();
void visit(FuncDef* node) override;
void visit(Block* node) override;
void visit(IfStmt* node) override;
void visit(WhileStmt* node) override;
void visit(ForStmt* node) override;
void visit(DoWhileStmt* node) override;
void visit(VarDef* node) override;
void visit(ReturnStmt* node) override;
void visit(BinaryOpExpr* node) override;
/// trivial. forward to ValueGenerator
void visit(UnaryOpExpr* node) override;
void visit(FuncCallExpr* node) override;
void visit(MemberExpr* node) override;
void visit(ArrayRefExpr* node) override;
void visit(VarExpr* node) override;
void visit(CastExpr* node) override;
void visit(LiteralExpr* node) override;
void visit(SizeofExpr* node) override;
void visit(EmptyExpr* node) override {};
void visit(CompoundDecl*) override {}
void visit(CompoundDef*) override {}
void visit(EnumDef*) override {}
void visit(TypeAlias*) override {}
void visit(FuncDecl*) override {}
/// not implemented:
// void visit(LabelStmt* node) override;
// void visit(CaseStmt* node) override;
// void visit(SwitchStmt* node) override;
Tac::Reg nextReg();
Tac::Label nextLabel();
Tac::StackObject nextStackObject(uint64_t size, size_t align);
SymbolTable& currScope() { return tacGen_.currScope(); }
void pushScope(SymbolTable* s) { tacGen_.pushScope(s); }
void popScope() { tacGen_.popScope(); }
std::list<Tac::Quad>::iterator emit(Tac::Quad&& quad);
std::list<Tac::Quad>::iterator lastQuad();
void pushTempStackObject(const Tac::StackObject& s) { tempStackObjects_.push(s); }
int addLiteral(Tac::StaticObject const& o) { return tacGen_.addLiteral(o); }
void addGlobalVar(std::string const& s, Tac::StaticObject const& o)
{
tacGen_.addGlobalVar(s, o);
}
TacGenerator& tacGenerator() { return tacGen_; }
private:
TacGenerator& tacGen_;
Tac::Function currFunction_;
std::list<Tac::Quad> quads_;
std::stack<Tac::StackObject> tempStackObjects_;
int regNo_ = 0;
int stackObjectNo_ = 1;
void compoundAssignment(CompoundType* type, Tac::Reg lhsAddr, Tac::Reg rhsAddr);
};
class FuncUtil
{
protected:
FuncGenerator& funcGenerator_;
FuncUtil(FuncGenerator& f) : funcGenerator_(f) {}
Tac::Reg nextReg() { return funcGenerator_.nextReg(); }
Tac::Label nextLabel() { return funcGenerator_.nextLabel(); }
Tac::StackObject nextStackObject(uint64_t size, size_t align)
{
return funcGenerator_.nextStackObject(size, align);
}
SymbolTable& currScope() { return funcGenerator_.currScope(); }
std::list<Tac::Quad>::iterator emit(Tac::Quad&& quad) { return funcGenerator_.emit(std::move(quad)); }
std::list<Tac::Quad>::iterator lastQuad() { return funcGenerator_.lastQuad(); }
void pushTempStackObject(const Tac::StackObject& s) { funcGenerator_.pushTempStackObject(s); }
int addLiteral(Tac::StaticObject const& o) { return funcGenerator_.addLiteral(o); }
void addGlobalVar(std::string const& n, Tac::StaticObject const& o)
{
funcGenerator_.addGlobalVar(n, o);
}
};
class BranchGenerator : public Visitor, public FuncUtil
{
public:
BranchGenerator(FuncGenerator& tg, bool when, Tac::Label lbl)
: FuncUtil(tg), when_(when), goto_(lbl)
{}
void visit(UnaryOpExpr* node) override; // !
void visit(BinaryOpExpr* node) override; // < > && || ...
void visit(FuncCallExpr* node) override;
void visit(MemberExpr* node) override;
void visit(ArrayRefExpr* node) override;
void visit(VarExpr* node) override;
void visit(CastExpr* node) override;
void visit(LiteralExpr* node) override;
void visit(SizeofExpr* node) override;
private:
bool when_;
Tac::Label goto_;
Tac::Opcode genJumpInst(Token::OperatorType op,
const std::shared_ptr<Expr>& lhs,
const std::shared_ptr<Expr>& rhs);
void implicitCond(Tac::Reg, const std::shared_ptr<Type>&);
};
/* 要是函数返回了一个结构体,本来讲道理它是个值类型,
* 但是没办法,我们得为它分配内存,所以扔在LValueGenerator里?
*/
/**
* 对于标量,返回一份其拷贝
* 对于非标量:
* 对于数组,其值即其地址,返回其地址
* 对于struct & union:这种情况不由ValueGenerator处理,假设不可能遇到。
* */
class ValueGenerator : public Visitor, public FuncUtil
{
public:
explicit ValueGenerator(FuncGenerator& t) : FuncUtil(t) {}
ValueGenerator(FuncGenerator& t, Tac::Reg aim);
Tac::Reg value() { return reg_; };
void visit(UnaryOpExpr* node) override;
void visit(BinaryOpExpr* node) override;
void visit(FuncCallExpr* node) override;
void visit(MemberExpr* node) override;
void visit(ArrayRefExpr* node) override;
void visit(VarExpr* node) override;
void visit(CastExpr* node) override;
void visit(LiteralExpr* node) override;
void visit(SizeofExpr* node) override;
// void visit(EmptyExpr* node) override;
private:
Tac::Reg reg_;
std::optional<Tac::Reg> target_;
Tac::Reg cast(Tac::Reg reg,
const std::shared_ptr<Type>& from, const std::shared_ptr<Type>& to,
bool inplace);
/// an substitution of 'nextReg()'
Tac::Reg regToWrite() {
if (target_.has_value()) {
return target_.value();
} else {
return nextReg();
}
}
};
class LValueGenerator: public Visitor, public FuncUtil
{
public:
explicit LValueGenerator(FuncGenerator& f) : FuncUtil(f) {}
Tac::Reg addr() { return addr_; }
bool inMemory() { return inMemory_; }
void visit(UnaryOpExpr* node) override;
void visit(MemberExpr* node) override;
void visit(ArrayRefExpr* node) override;
void visit(VarExpr* node) override;
void visit(FuncCallExpr* node) override;
private:
Tac::Reg addr_;
bool inMemory_ = true;
// true if this lvalue is not kept in register
// which means addr() returns the address of the lvalue
// otherwise the value is in register, addr() returns the bounded register
};
#endif //TACGENERATOR_H__