/
tac.h
284 lines (236 loc) · 6.46 KB
/
tac.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
/* File: tac.h
* -----------
* This module contains the Instruction class (and its various
* subclasses) that represent Tac instructions and the Location
* class used for operands to those instructions.
*
* Each instruction is mostly just a little struct with a
* few fields, but each responds polymorphically to the methods
* Print and Emit, the first is used to print out the TAC form of
* the instruction (helpful when debugging) and the second to
* convert to the appropriate MIPS assembly.
*
* The operands to each instruction are of Location class.
* A Location object is a simple representation of where a variable
* exists at runtime, i.e. whether it is on the stack or global
* segment and at what offset relative to the current fp or gp.
*
* You may need to make changes/extensions to these classes
* if you are working on IR optimization.
*/
#ifndef _H_tac
#define _H_tac
#include "list.h" // for VTabl
#include <iostream>
#include <string>
class Mips;
// A Location object is used to identify the operands to the
// various TAC instructions. A Location is either fp or gp
// relative (depending on whether in stack or global segemnt)
// and has an offset relative to the base of that segment.
// For example, a declaration for integer num as the first local
// variable in a function would be assigned a Location object
// with name "num", segment fpRelative, and offset -8.
typedef enum {fpRelative, gpRelative} Segment;
typedef enum {
OpNoOp,
OpLoadConstant,
OpLoadStringConstant,
OpLoadLabel,
OpAssign,
OpLoad,
OpStore,
OpBinaryOp,
OpLabel,
OpGoto,
OpIfZ,
OpBeginFunc,
OpEndFunc,
OpReturn,
OpPushParam,
OpPopParams,
OpRemoveParams,
OpLCall,
OpACall,
OpVTable
} InstructionType;
class Location
{
protected:
const char *variableName;
Segment segment;
int offset;
int refOffset;
bool isReference;
public:
Location(Segment seg, int offset, const char *name);
Location(Location *base, int refOff) :
variableName(base->variableName), segment(base->segment),
offset(base->offset), refOffset(refOff), isReference(true) {}
const char *GetName() { return variableName; }
Segment GetSegment() { return segment; }
int GetOffset() { return offset; }
bool IsReference() { return isReference; }
int GetRefOffset() { return refOffset; }
};
// base class from which all Tac instructions derived
// has the interface for the 2 polymorphic messages: Print & Emit
class Instruction {
friend class df_base_type;
friend class df_live_var;
protected:
char printed[128];
InstructionType op;
public:
virtual void Print();
virtual void EmitSpecific(Mips *mips) = 0;
virtual void Emit(Mips *mips);
virtual InstructionType OpClass();
std::string command() { return printed; }
};
// for convenience, the instruction classes are listed here.
// the interfaces for the classes follows below
class LoadConstant;
class LoadStringConstant;
class LoadLabel;
class Assign;
class Load;
class Store;
class BinaryOp;
class Label;
class Goto;
class IfZ;
class BeginFunc;
class EndFunc;
class Return;
class PushParam;
class RemoveParams;
class LCall;
class ACall;
class VTable;
class LoadConstant: public Instruction {
Location *dst;
int val;
public:
LoadConstant(Location *dst, int val);
void EmitSpecific(Mips *mips);
};
class LoadStringConstant: public Instruction {
Location *dst;
char *str;
public:
LoadStringConstant(Location *dst, const char *s);
void EmitSpecific(Mips *mips);
};
class LoadLabel: public Instruction {
Location *dst;
const char *label;
public:
LoadLabel(Location *dst, const char *label);
void EmitSpecific(Mips *mips);
};
class Assign: public Instruction {
Location *dst, *src;
public:
Assign(Location *dst, Location *src);
void EmitSpecific(Mips *mips);
};
class Load: public Instruction {
Location *dst, *src;
int offset;
public:
Load(Location *dst, Location *src, int offset = 0);
void EmitSpecific(Mips *mips);
};
class Store: public Instruction {
Location *dst, *src;
int offset;
public:
Store(Location *d, Location *s, int offset = 0);
void EmitSpecific(Mips *mips);
};
class BinaryOp: public Instruction {
public:
typedef enum {Add, Sub, Mul, Div, Mod, Eq, Less, And, Or, NumOps} OpCode;
static const char * const opName[NumOps];
static OpCode OpCodeForName(const char *name);
protected:
OpCode code;
Location *dst, *op1, *op2;
public:
BinaryOp(OpCode c, Location *dst, Location *op1, Location *op2);
void EmitSpecific(Mips *mips);
};
class Label: public Instruction {
const char *label;
public:
Label(const char *label);
void Print();
void EmitSpecific(Mips *mips);
};
class Goto: public Instruction {
const char *label;
public:
Goto(const char *label);
void EmitSpecific(Mips *mips);
};
class IfZ: public Instruction {
Location *test;
const char *label;
public:
IfZ(Location *test, const char *label);
void EmitSpecific(Mips *mips);
};
class BeginFunc: public Instruction {
int frameSize;
public:
BeginFunc();
// used to backpatch the instruction with frame size once known
void SetFrameSize(int numBytesForAllLocalsAndTemps);
void EmitSpecific(Mips *mips);
};
class EndFunc: public Instruction {
public:
EndFunc();
void EmitSpecific(Mips *mips);
};
class Return: public Instruction {
Location *val;
public:
Return(Location *val);
void EmitSpecific(Mips *mips);
};
class PushParam: public Instruction {
Location *param;
public:
PushParam(Location *param);
void EmitSpecific(Mips *mips);
};
class PopParams: public Instruction {
int numBytes;
public:
PopParams(int numBytesOfParamsToRemove);
void EmitSpecific(Mips *mips);
};
class LCall: public Instruction {
const char *label;
Location *dst;
public:
LCall(const char *labe, Location *result);
void EmitSpecific(Mips *mips);
};
class ACall: public Instruction {
Location *dst, *methodAddr;
public:
ACall(Location *meth, Location *result);
void EmitSpecific(Mips *mips);
};
class VTable: public Instruction {
List<const char *> *methodLabels;
const char *label;
public:
VTable(const char *labelForTable, List<const char *> *methodLabels);
void Print();
void EmitSpecific(Mips *mips);
};
#endif