-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstruction.h
More file actions
213 lines (190 loc) · 5.46 KB
/
Copy pathInstruction.h
File metadata and controls
213 lines (190 loc) · 5.46 KB
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
#ifndef __INSTRUCTION_H__
#define __INSTRUCTION_H__
#include <map>
#include <sstream>
#include <vector>
#include "AsmBuilder.h"
#include "Operand.h"
class BasicBlock;
class Instruction
{
public:
Instruction(unsigned instType, BasicBlock* insert_bb = nullptr);
virtual ~Instruction();
BasicBlock* getParent();
bool isUncond() const { return instType == UNCOND; };
bool isCond() const { return instType == COND; };
bool isAlloc() const { return instType == ALLOCA; };
bool isRet() const { return instType == RET; };
void setParent(BasicBlock*);
void setNext(Instruction*);
void setPrev(Instruction*);
Instruction* getNext();
Instruction* getPrev();
virtual void output() const = 0;
MachineOperand* genMachineOperand(Operand*);
MachineOperand* genMachineReg(int reg);
MachineOperand* genMachineVReg();
MachineOperand* genMachineImm(int val);
MachineOperand* genMachineLabel(int block_no);
virtual void genMachineCode(AsmBuilder*) = 0;
protected:
unsigned instType;
unsigned opcode;
Instruction* prev;
Instruction* next;
BasicBlock* parent;
std::vector<Operand*> operands;
enum
{
BINARY,
COND,
UNCOND,
RET,
LOAD,
STORE,
CMP,
ALLOCA,
CALL,
ZEXT,
XOR,
GEP
};
};
// meaningless instruction, used as the head node of the instruction list.
class DummyInstruction : public Instruction
{
public:
DummyInstruction() : Instruction(-1, nullptr){};
void output() const {};
void genMachineCode(AsmBuilder*){};
};
class AllocaInstruction : public Instruction
{
public:
AllocaInstruction(Operand* dst, SymbolEntry* se, BasicBlock* insert_bb = nullptr);
~AllocaInstruction();
void output() const;
void genMachineCode(AsmBuilder*);
private:
SymbolEntry* se;
};
class LoadInstruction : public Instruction
{
public:
LoadInstruction(Operand* dst, Operand* src_addr, BasicBlock* insert_bb = nullptr);
~LoadInstruction();
void output() const;
void genMachineCode(AsmBuilder*);
};
class StoreInstruction : public Instruction
{
public:
StoreInstruction(Operand* dst_addr, Operand* src, BasicBlock* insert_bb = nullptr);
~StoreInstruction();
void output() const;
void genMachineCode(AsmBuilder*);
};
class BinaryInstruction : public Instruction
{
public:
BinaryInstruction(unsigned opcode,
Operand* dst,
Operand* src1,
Operand* src2,
BasicBlock* insert_bb = nullptr);
~BinaryInstruction();
void output() const;
void genMachineCode(AsmBuilder*);
enum { SUB, ADD, AND, OR, MUL, DIV, MOD };
};
class CmpInstruction : public Instruction
{
public:
CmpInstruction(unsigned opcode, Operand* dst, Operand* src1, Operand* src2, BasicBlock* insert_bb = nullptr);
~CmpInstruction();
void output() const;
void genMachineCode(AsmBuilder*);
enum { E, NE, L, LE, G, GE };
};
// unconditional branch
class UncondBrInstruction : public Instruction
{
public:
UncondBrInstruction(BasicBlock*, BasicBlock* insert_bb = nullptr);
void output() const;
void setBranch(BasicBlock*);
BasicBlock* getBranch();
void genMachineCode(AsmBuilder*);
protected:
BasicBlock* branch;
};
// conditional branch
class CondBrInstruction : public Instruction
{
public:
CondBrInstruction(BasicBlock*, BasicBlock*, Operand*, BasicBlock* insert_bb = nullptr);
~CondBrInstruction();
void output() const;
void setTrueBranch(BasicBlock*);
BasicBlock* getTrueBranch();
void setFalseBranch(BasicBlock*);
BasicBlock* getFalseBranch();
void genMachineCode(AsmBuilder*);
protected:
BasicBlock* true_branch;
BasicBlock* false_branch;
};
class RetInstruction : public Instruction
{
public:
RetInstruction(Operand* src, BasicBlock* insert_bb = nullptr);
~RetInstruction();
void output() const;
void genMachineCode(AsmBuilder*);
};
class CallInstruction : public Instruction
{
private:
SymbolEntry* func;
Operand* dst;
public:
CallInstruction(Operand* dst, SymbolEntry* func, std::vector<Operand*> params, BasicBlock* insert_bb = nullptr);
~CallInstruction();
void output() const;
void genMachineCode(AsmBuilder*);
};
class ZextInstruction : public Instruction
{
public:
ZextInstruction(Operand* dst, Operand* src, BasicBlock* insert_bb = nullptr);
~ZextInstruction();
void output() const;
void genMachineCode(AsmBuilder*);
};
class XorInstruction : public Instruction
{
public:
XorInstruction(Operand* dst, Operand* src, BasicBlock* insert_bb = nullptr);
~XorInstruction();
void output() const;
void genMachineCode(AsmBuilder*);
};
class GetPointerInstruction : public Instruction
{
private:
bool paramFirst;
bool firstDim;
bool last;
Operand* init;
public:
GetPointerInstruction(Operand* dst, Operand* arr, Operand* idx, BasicBlock* insert_bb = nullptr, bool paramFirst = false);
~GetPointerInstruction();
void output() const;
void genMachineCode(AsmBuilder*);
void setFirst() { firstDim = true; };
void setLast() { last = true; };
Operand* getInitInst() const { return init; };
void setInitInst(Operand* init) { this->init = init; };
};
#endif