forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra-data.cpp
182 lines (158 loc) · 6.47 KB
/
extra-data.cpp
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
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2014 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/runtime/vm/jit/extra-data.h"
#include "hphp/runtime/vm/jit/ssa-tmp.h"
#include "hphp/runtime/vm/jit/abi-x64.h"
#include "hphp/util/text-util.h"
namespace HPHP { namespace jit {
std::string NewStructData::show() const {
std::ostringstream os;
auto delim = "";
for (uint32_t i = 0; i < numKeys; i++) {
os << delim << "\"" <<
escapeStringForCPP(keys[i]->data(), keys[i]->size()) <<
"\"";
delim = ",";
}
return os.str();
}
//////////////////////////////////////////////////////////////////////
namespace {
FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_cseHash, cseHash);
FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_cseEquals, cseEquals);
FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_clone, clone);
FOLLY_CREATE_HAS_MEMBER_FN_TRAITS(has_show, show);
/*
* dispatchExtra translates from runtime values for the Opcode enum
* into compile time types. The goal is to call a `targetFunction'
* that is overloaded on the extra data type structs.
*
* The purpose of the MAKE_DISPATCHER layer is to weed out Opcode
* values that have no associated extra data.
*
* Basically this is doing dynamic dispatch without a vtable in
* IRExtraData, instead using the Opcode tag from the associated
* instruction to discriminate the runtime type.
*
* Note: functions made with this currently only make sense to call if
* it's already known that the opcode has extra data. If you call it
* for one that doesn't, you'll get an abort. Generally hasExtra()
* should be checked first.
*/
#define MAKE_DISPATCHER(name, rettype, targetFunction) \
template<bool HasExtra, Opcode opc> struct name { \
template<class... Args> \
static rettype go(IRExtraData* vp, Args&&...) { not_reached(); } \
}; \
template<Opcode opc> struct name<true,opc> { \
template<class... Args> \
static rettype go(IRExtraData* vp, Args&&... args) { \
return targetFunction( \
static_cast<typename IRExtraDataType<opc>::type*>(vp), \
std::forward<Args>(args)... \
); \
} \
};
template<
class RetType,
template<bool, Opcode> class Dispatcher,
class... Args
>
RetType dispatchExtra(Opcode opc, IRExtraData* data, Args&&... args) {
#define O(opcode, dstinfo, srcinfo, flags) \
case opcode: \
return Dispatcher< \
OpHasExtraData<opcode>::value, \
opcode \
>::go(data, std::forward<Args>(args)...);
switch (opc) { IR_OPCODES default: not_reached(); }
#undef O
not_reached();
}
template<class T>
typename std::enable_if<
has_cseHash<T,size_t () const>::value,
size_t
>::type cseHashExtraImpl(T* t) { return t->cseHash(); }
size_t cseHashExtraImpl(IRExtraData*) {
// This probably means an instruction was marked CanCSE but its
// extra data had no hash function.
always_assert(!"attempted to hash extra data that didn't "
"provide a hash function");
}
template<class T>
typename std::enable_if<
has_cseEquals<T,bool (T const&) const>::value ||
has_cseEquals<T,bool (T) const>::value,
bool
>::type cseEqualsExtraImpl(T* t, IRExtraData* o) {
return t->cseEquals(*static_cast<T*>(o));
}
bool cseEqualsExtraImpl(IRExtraData*, IRExtraData*) {
// This probably means an instruction was marked CanCSE but its
// extra data had no equals function.
always_assert(!"attempted to compare extra data that didn't "
"provide an equals function");
}
// Clone using a data-specific clone function.
template<class T>
typename std::enable_if<
has_clone<T,T* (Arena&) const>::value,
T*
>::type cloneExtraImpl(T* t, Arena& arena) {
return t->clone(arena);
}
// Use the copy constructor if no clone() function was supplied.
template<class T>
typename std::enable_if<
!has_clone<T,T* (Arena&) const>::value,
T*
>::type cloneExtraImpl(T* t, Arena& arena) {
return new (arena) T(*t);
}
template<class T>
typename std::enable_if<
has_show<T,std::string () const>::value,
std::string
>::type showExtraImpl(T* t) { return t->show(); }
std::string showExtraImpl(const IRExtraData*) { return "..."; }
MAKE_DISPATCHER(HashDispatcher, size_t, cseHashExtraImpl);
MAKE_DISPATCHER(EqualsDispatcher, bool, cseEqualsExtraImpl);
MAKE_DISPATCHER(CloneDispatcher, IRExtraData*, cloneExtraImpl);
MAKE_DISPATCHER(ShowDispatcher, std::string, showExtraImpl);
}
//////////////////////////////////////////////////////////////////////
size_t cseHashExtra(Opcode opc, const IRExtraData* data) {
return dispatchExtra<size_t,HashDispatcher>(
opc, const_cast<IRExtraData*>(data));
}
bool cseEqualsExtra(
Opcode opc,
const IRExtraData* data,
const IRExtraData* other
) {
return dispatchExtra<bool,EqualsDispatcher>(
opc, const_cast<IRExtraData*>(data), const_cast<IRExtraData*>(other));
}
IRExtraData* cloneExtra(Opcode opc, IRExtraData* data, Arena& a) {
return dispatchExtra<IRExtraData*,CloneDispatcher>(opc, data, a);
}
std::string showExtra(Opcode opc, const IRExtraData* data) {
return dispatchExtra<std::string,ShowDispatcher>(opc,
const_cast<IRExtraData*>(data));
}
//////////////////////////////////////////////////////////////////////
}}