forked from facebook/hhvm
/
codegen.h
321 lines (274 loc) · 10.6 KB
/
codegen.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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#ifndef incl_HPHP_VM_CG_H_
#define incl_HPHP_VM_CG_H_
#include <vector>
#include "runtime/vm/translator/hopt/ir.h"
#include "runtime/vm/translator/targetcache.h"
#include <runtime/vm/translator/translator-x64.h>
namespace HPHP {
namespace VM {
namespace JIT {
using namespace HPHP::VM::Transl;
using namespace HPHP::VM::Transl::TargetCache;
class FailedCodeGen : public std::exception {
public:
const char* file;
const int line;
const char* func;
FailedCodeGen(const char* _file, int _line, const char* _func) :
file(_file), line(_line), func(_func) { }
};
void cgPunt(const char* _file, int _line, const char* _func);
#define CG_PUNT(instr) do { \
if (tx64) { \
cgPunt( __FILE__, __LINE__, #instr); \
} \
} while(0)
struct ArgGroup;
enum SyncOptions {
kNoSyncPoint,
kSyncPoint
};
class CodeGenerator {
public:
// typedef copied from TranslatorX64 class
typedef Transl::X64Assembler Asm;
CodeGenerator(Asm& as, Asm& astubs, Transl::TranslatorX64* tx64) :
m_as(as), m_astubs(astubs), m_tx64(tx64),
m_curInst(NULL), m_lastMarker(NULL), m_curTrace(NULL) {
}
void cgTrace(Trace* trace, vector<TransBCMapping>* bcMap);
Address cgBreakpoint(X64Assembler &a);
Address cgInst(IRInstruction* inst);
// Autogenerate function declarations for each IR instruction in ir.h
#define OPC(name, hasDst, canCSE, essential, effects, native, consRef, \
prodRef, mayModRefs, rematerializable, error) \
Address cg ## name (IRInstruction* inst);
IR_OPCODES
#undef OPC
// helper functions for code generation
Address cgCallHelper(Asm& a,
TCA addr,
PhysReg dstReg,
SyncOptions sync,
ArgGroup& args);
Address cgCallHelper(Asm&,
TCA addr,
SSATmp* dst,
SyncOptions sync,
ArgGroup& args);
Address cgStore(PhysReg base,
int64_t off,
SSATmp* src,
bool genStoreType = true);
Address cgStoreTypedValue(PhysReg base, int64_t off, SSATmp* src);
Address cgLoad(Type::Tag type,
SSATmp* dst,
PhysReg base,
int64_t off,
LabelInstruction* label,
IRInstruction* inst = NULL);
Address cgGuardType(Type::Tag type,
PhysReg baseReg,
int64_t offset,
LabelInstruction* label,
IRInstruction* instr);
Address cgStMemWork(IRInstruction* inst, bool genStoreType);
Address cgStRefWork(IRInstruction* inst, bool genStoreType);
Address cgStLocWork(IRInstruction* inst, bool genStoreType);
Address cgStPropWork(IRInstruction* inst, bool genStoreType);
Address cgIncRefWork(Type::Tag type, SSATmp* dst, SSATmp* src);
Address cgDecRefWork(IRInstruction* inst, bool genZeroCheck);
Address cgNegateWork(SSATmp* dst, SSATmp* src);
Address cgNotWork(SSATmp* dst, SSATmp* src);
Address cgSpillStackWork(IRInstruction* inst, bool allocActRec);
Address cgAllocActRec1(IRInstruction* inst); // One source, sp
Address cgAllocActRec5(SSATmp* dst,
SSATmp* sp,
SSATmp* fp,
SSATmp* func,
SSATmp* objOrCls,
SSATmp* nArgs);
Address cgAllocActRec6(SSATmp* dst,
SSATmp* sp,
SSATmp* fp,
SSATmp* func,
SSATmp* objOrCls,
SSATmp* nArgs,
SSATmp* magicName);
Address cgLoadTypedValue(Type::Tag type,
SSATmp* dst,
PhysReg base,
int64_t off,
LabelInstruction* label,
IRInstruction* inst);
Address cgNegate(IRInstruction* inst); // helper
Address cgJcc(IRInstruction* inst); // helper
Address cgOpCmpHelper(
IRInstruction* inst,
void (Asm::*setter)(Reg8),
int64 (*str_cmp_str)(StringData*, StringData*),
int64 (*str_cmp_int)(StringData*, int64),
int64 (*str_cmp_obj)(StringData*, ObjectData*),
int64 (*obj_cmp_obj)(ObjectData*, ObjectData*),
int64 (*obj_cmp_int)(ObjectData*, int64),
int64 (*arr_cmp_arr)(ArrayData*, ArrayData*));
Address cgJmpZeroHelper(IRInstruction* inst, ConditionCode cc);
private:
void emitTraceCall(CodeGenerator::Asm& as, int64 pcOff);
void emitTraceRet(CodeGenerator::Asm& as);
Address emitCheckStack(CodeGenerator::Asm& as, SSATmp* sp, uint32 numElems,
bool allocActRec);
Address emitCheckCell(CodeGenerator::Asm& as,
SSATmp* sp,
uint32 index);
Address cgCheckStaticBit(Type::Tag type,
PhysReg reg,
bool regIsCount);
Address cgCheckStaticBitAndDecRef(Type::Tag type,
PhysReg dataReg,
LabelInstruction* exit);
Address cgCheckRefCountedType(PhysReg typeReg);
Address cgCheckRefCountedType(PhysReg baseReg,
int64 offset);
Address cgDecRefStaticType(Type::Tag type,
PhysReg dataReg,
LabelInstruction* exit,
bool genZeroCheck);
Address cgDecRefDynamicType(PhysReg typeReg,
PhysReg dataReg,
LabelInstruction* exit,
bool genZeroCheck);
Address cgDecRefDynamicTypeMem(PhysReg baseReg,
int64 offset,
LabelInstruction* exit);
Address cgDecRefMem(Type::Tag type,
PhysReg baseReg,
int64 offset,
LabelInstruction* exit);
Address cgCheckUninit(SSATmp* src, LabelInstruction* label);
Address emitFwdJcc(ConditionCode cc, LabelInstruction* label);
Address emitFwdJmp(Asm& as, LabelInstruction* label);
Address emitFwdJmp(LabelInstruction* label);
Address emitSmashableFwdJmp(LabelInstruction* label, SSATmp* toSmash);
Address emitSmashableFwdJccAtEnd(ConditionCode cc, LabelInstruction* label,
SSATmp* toSmash);
Address emitSmashableFwdJcc(ConditionCode cc, LabelInstruction* label,
SSATmp* toSmash);
void emitGuardOrFwdJcc(IRInstruction* inst,
ConditionCode cc,
LabelInstruction* label);
Address emitContVarEnvHelperCall(SSATmp* fp, TCA helper);
const Func* getCurrFunc();
void recordSyncPoint(Asm& as);
Address getDtor(DataType type);
Address getDtorGeneric();
Address getDtorTyped();
/*
* Fields
*/
Asm& m_as;
Asm& m_astubs;
TranslatorX64* m_tx64;
// current instruction for which code is being generated
IRInstruction* m_curInst;
// the last marker instruction before curInst
LabelInstruction* m_lastMarker;
Trace* m_curTrace;
};
class ArgDesc {
public:
enum Kind { Reg, Imm, Addr };
PhysReg getDstReg() const { return m_dstReg; }
PhysReg getSrcReg() const { return m_srcReg; }
Kind getKind() const { return m_kind; }
void setDstReg(PhysReg reg) { m_dstReg = reg; }
Address genCode(CodeGenerator::Asm& as) const;
Immed getImm() const { return m_imm; }
private: // These should be created using ArgGroup.
friend struct ArgGroup;
explicit ArgDesc(Kind kind, PhysReg srcReg, Immed immVal)
: m_kind(kind)
, m_srcReg(srcReg)
, m_dstReg(reg::noreg)
, m_imm(immVal)
{}
explicit ArgDesc(SSATmp* tmp, bool val = true);
private:
Kind m_kind;
PhysReg m_srcReg;
PhysReg m_dstReg;
Immed m_imm;
};
/*
* Bag of ArgDesc for use with cgCallHelper.
*
* You can create this using function chaining. Example:
*
* ArgGroup args;
* args.imm(0)
* .reg(rax)
* .immPtr(StringData::GetStaticString("Yo"))
* ;
* ASSERT(args.size() == 3);
*/
struct ArgGroup {
size_t size() const { return m_args.size(); }
ArgDesc& operator[](size_t i) {
ASSERT(i < size());
return m_args[i];
}
ArgGroup& imm(uintptr_t imm) {
m_args.push_back(ArgDesc(ArgDesc::Imm, InvalidReg, imm));
return *this;
}
template<class T> ArgGroup& immPtr(const T* ptr) {
return imm(uintptr_t(ptr));
}
ArgGroup& reg(PhysReg reg) {
m_args.push_back(ArgDesc(ArgDesc::Reg, PhysReg(reg), -1));
return *this;
}
ArgGroup& type(Type::Tag tag) {
m_args.push_back(ArgDesc(ArgDesc::Imm, InvalidReg,
Type::toDataType(tag)));
return *this;
}
ArgGroup& addr(PhysReg base, uintptr_t off) {
m_args.push_back(ArgDesc(ArgDesc::Addr, PhysReg(base), off));
return *this;
}
ArgGroup& ssa(SSATmp* tmp) {
m_args.push_back(ArgDesc(tmp));
return *this;
}
/* loads the type of tmp into an arg register */
ArgGroup& type(SSATmp* tmp) {
m_args.push_back(ArgDesc(tmp, false));
return *this;
}
private:
std::vector<ArgDesc> m_args;
};
void genCodeForTrace(Trace* trace,
CodeGenerator::Asm& a,
CodeGenerator::Asm& astubs,
IRFactory* irFactory,
vector<TransBCMapping>* bcMap,
TranslatorX64* tx64);
}}}
#endif