-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtacdef.h
322 lines (254 loc) · 8.04 KB
/
tacdef.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
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
#ifndef TACDEF_H__
#define TACDEF_H__
#include <variant>
#include <string>
#include <list>
#include <memory>
#include <vector>
#include <map>
#include <unordered_set>
#include <cinttypes>
#include <type_traits>
#include <functional>
#include "platform.h"
struct SymtabEntry;
class ListSymtab;
class Type;
namespace Tac
{
enum Opcode
{
Nop,
LabelLine, /** LabelLine <label> */
Loadr /** load value. The address is specified by a register.*/,
/// Loadr <base register> <offset> <aim register>
Loadrc /** load value. The address is specified by a register and an offset */,
Loadi /** load an immidiate number */,
Loadru, Loadrcu, Loadiu,
Storer, /// Storer <valueToBeStored> <empty> <destAddr>
Storerc, /// Storerc <valueToBeStored> <destAddrOffset> <destAddrBase>
Add, Sub, Mul, Divu, Divs, Modu, Mods,
Shl, Shrl, Shra, BAnd, BOr, BXor, BInv,
Movrr, /// copy between reigisters
Extu, Exts, /// 扩展指令表示将一定长度(由指令的width字段指出)的半字或字节扩展到字长
Jmp, /** Jmp <> <> <dest> */
Jeq, Jne, Jgs, Jges, Jls, Jles,
Jgu, Jgeu, Jlu, Jleu,
Call, /** call <funcent> <empty> <reg(return value)> */
Ret, /** return a value in register : ret <reg(optional)>*/
LoadVarPtr, /// LoadVarPtr <StackObject> <empty> <reg>
Memcpy, /// Memcpy <rhsAddr> <size> <lhsAddr>
/** 用于从函数体内获得参数的值或地址 */
/** num 算上返回值变成的参数 */
GetParamVal, /// maybe from memory!!! should not be optimized out
GetParamPtr, /// GetParam* <num> <empty> <reg>
Alloca, /// Alloca <StackObject> allocate memory on stack
AllocaTemp, /// AllocaTemp <StackObject> <empty> <reg(save the address)>
/// same as Alloca, except the allocated object will be 'Dealloca'ed later
Dealloca, /// Dealloca <StackObject>
LoadGlobalPtr, /// LoadGlobalPtr <string> <> <reg>
LoadConstantPtr, /// LoadConstantPtr <seq number> <> <reg>
};
struct Reg
{
int n; /// exclusive in a particular function
explicit Reg(int r = -1) : n(r)
{}
std::string toString() const;
bool operator==(Reg rhs) const;
bool operator<(Reg rhs) const { return n < rhs.n; }
};
struct Label
{
int n; /// exclusive for a particular file
explicit Label(int i = -1) : n(i)
{}
std::string toString() const;
bool operator==(Label rhs) const;
bool operator<(Label rhs) const { return n < rhs.n; }
/// label must be unique in file scope.
};
/// represent a piece of stack memory
struct StackObject
{
int n; /// exclusive for a particular function
size_t alignAt; /// 直接按字节计。值得注意的是,在汇编里,是按照2的次方字节计的
uint64_t size;
StackObject(int i, uint64_t s, size_t align)
: n(i), alignAt(align), size(s)
{}
};
/// In asm, some constants are too big to fit in a instruction
/// In this situation, they are labeled and made to be part of
/// the object file.
/// 为了两种情况而设计:1.(初始化或未初始化的)全局变量,
/// 2. 尺寸比较大的字面量(字符串字面量、大于字长的字面量如32位架构中的double)
struct StaticObject
{
/// padding ".space"
struct Padding
{
size_t size;
bool operator==(const Padding& rhs) const
{ return size == rhs.size; }
};
/// global variables use variable name as their id,
/// while readonly data like string literal and double literal
/// use a unique sequence number as their id
/// bin data like .word .half
using BinData = std::variant<int8_t, int16_t, int32_t, int64_t, double, Padding>;
using DataType = std::variant<std::monostate,
std::string, /// character data, null terminated ascii
std::vector<BinData>// bin data
>;
DataType data;
size_t size;
size_t align;
explicit StaticObject(const std::string& ascii);
StaticObject(size_t size, size_t align,
std::vector<BinData>&& data);
StaticObject(size_t size, size_t align); /// not initialized. 'data' holds std::monostate
bool operator==(const StaticObject& rhs) const
{ return data == rhs.data; }
bool initialized() const {
return data.index() != 0;
}
};
struct Var
{
/// int64 : big enough
struct ImmType
{
int64_t val;
/// not necessary to be explicit
template<typename IntType>
ImmType(IntType v) : val(static_cast<int64_t>(v)) {
static_assert(std::is_integral_v<IntType>);
}
};
struct FuncLabel
{
std::string name;
};
struct VarLabel
{
std::string name;
};
std::variant<std::monostate,
Reg, ImmType, StackObject, Label,
FuncLabel /* for functions only */,
VarLabel /* for global variables only */
> uvar;
Var() {}
Var(Reg r);
Var(const StackObject& o);
Var(FuncLabel f); /// for functions only
Var(VarLabel v);
Var(ImmType imm);
Var(Label l);
std::string toString() const;
static const Var empty;
};
/**
* PassBy的意义:
* Value:针对标量,直接寄存器传递就可以
* Ptr: 针对struct,Call指令里的寄存器只表示对象的地址。
* 因为在目前的 ir 架构中,struct只能放在内存里,只能用其地址去标记它。
*/
struct ArgInfo
{
enum { Value, Ptr };
Reg reg;
int regMeans; /// reg is a value or a ptr
size_t size;
size_t align;
bool scalar; /// only scalars are possible to be passed by registers
};
using ArgPassingSpec = std::vector<ArgInfo>;
struct Quad
{
Opcode op;
std::size_t width;
Var opnd1;
Var opnd2;
Var res;
// only available when op is "Call"
std::unique_ptr<ArgPassingSpec> passingSpec;
Quad() : op(Nop)
{
}
Quad(Opcode optor, Var v1 = Var::empty,
Var v2 = Var::empty, Var r = Var::empty, std::size_t sw = PTRSIZE)
: op(optor), opnd1(v1), opnd2(v2), res(r), width(sw)
{}
void setPassingSpec(ArgPassingSpec&& args) {
passingSpec = std::make_unique<ArgPassingSpec>(std::move(args));
}
std::string toString() const;
};
struct BasicBlock;
using BasicBlockList = std::list<BasicBlock>;
using BasicBlockPtr = BasicBlockList::iterator;
using ConstBasicBlockPtr = BasicBlockList::const_iterator;
struct BasicBlock
{
std::list<Quad> quads;
std::vector<BasicBlockPtr> succs;
std::vector<BasicBlockPtr> preds;
void addQuad(Quad&& q) { quads.push_back(std::move(q)); }
};
struct ParamInfo
{
size_t size;
size_t align;
bool scalar;
bool ambiguous;
bool isUnsigned; // true if this param is an unsigned integer
};
struct Function
{
std::string name;
std::vector<ParamInfo> params;
std::shared_ptr<Type> retType;
BasicBlockList basicBlocks;
explicit Function(std::string n, std::shared_ptr<Type> ret)
: name(std::move(n)), retType(std::move(ret)) {}
std::string toString() const;
void addBasicBlock(BasicBlock bb);
template<typename Func>
void addBasicBlock(BasicBlock bb, Func&& finishingTouch)
{
addBasicBlock(std::move(bb));
finishingTouch(std::prev(basicBlocks.end()));
}
};
struct TacIR
{
std::vector<std::pair<std::string, StaticObject>> globalVars;
std::vector<StaticObject> literalPool; /// named by seq num
std::list<Function> funcs;
std::string toString() const;
void addGlobalVar(const std::string& name, const StaticObject& so);
int addLiteral(const StaticObject& so);
};
} // end namespace tac
namespace std
{
template<>
struct hash<Tac::BasicBlockPtr>
{
size_t operator()(Tac::BasicBlockPtr p) const
{
return std::hash<decltype(&(*p))>()(&(*p));
}
};
template<>
struct hash<Tac::ConstBasicBlockPtr>
{
size_t operator()(Tac::ConstBasicBlockPtr p) const
{
return std::hash<decltype(&(*p))>()(&(*p));
}
};
} // end namespace std
#endif //TACDEF_H__