forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall-spec.h
More file actions
446 lines (383 loc) · 13.2 KB
/
Copy pathcall-spec.h
File metadata and controls
446 lines (383 loc) · 13.2 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present 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_JIT_CALL_SPEC_H_
#define incl_HPHP_JIT_CALL_SPEC_H_
#include "hphp/runtime/ext/asio/asio-blockable.h"
#include "hphp/runtime/vm/jit/type.h"
#include "hphp/runtime/vm/jit/vasm-reg.h"
#include "hphp/runtime/base/array-data.h"
#include "hphp/runtime/base/record-data.h"
#include "hphp/runtime/vm/class-meth-data-ref.h"
#include <array>
#include <cstdint>
#include <type_traits>
#include <vector>
namespace HPHP {
struct ActRec;
namespace jit {
struct CallDest;
///////////////////////////////////////////////////////////////////////////////
namespace detail {
/*
* Type::operator| can't reasonably be made constexpr. This macro constructs a
* constexpr Type that's the union of two other non-Mem, non-specialized Types.
*/
#define U(t1, t2) Type(Type::k##t1 | Type::k##t2, Ptr::NotPtr, Mem::NotMem)
#define CPP_TYPES \
T(ActRec*, TFramePtr) \
T(AsioBlockableChain, TABC) \
T(Class*, TCls) \
T(Func*, TFunc) \
T(RFuncData*, TRFunc) \
T(ClsMethDataRef, TClsMeth) \
T(RClsMethData*, TRClsMeth) \
T(NamedEntity*, TNamedEntity) \
T(ResourceHdr*, TRes) \
T(StringData*, TStr) \
T(TCA, TTCA) \
T(TypedValue&, TPtrToCell) \
T(TypedValue*, TPtrToCell) \
T(TypedValue, TCell) \
T(bool, TBool) \
T(double, TDbl) \
T(int32_t, TInt) \
T(int64_t, TInt) \
T(unsigned long, TInt) \
T(unsigned long long, TInt) \
T(unsigned int, U(Int, RDSHandle)) \
T(tv_lval, TLvalToCell) \
T(tv_rval, TLvalToCell)
/*
* jit_cpp_type<> handles all types that are the same for parameters and return
* values.
*/
template<typename T, typename Enable = void> struct jit_cpp_type;
#define T(native_t, jit_t) \
template<> struct jit_cpp_type<native_t> { \
static auto constexpr type() { return jit_t; } \
};
CPP_TYPES
#undef T
#undef U
#undef CPP_TYPES
/*
* All subtypes of ObjectData and ArrayData map to TObj or TArrLike,
* respectively.
*/
template<typename O> struct jit_cpp_type<
O*, std::enable_if_t<std::is_base_of<ObjectData, O>::value>
> {
static auto constexpr type() { return TObj; }
};
template<typename A> struct jit_cpp_type<
A*, std::enable_if_t<std::is_base_of<ArrayData, A>::value>
> {
static auto constexpr type() { return TArrLike; }
};
template<typename A> struct jit_cpp_type<
A*, std::enable_if_t<std::is_base_of<RecordData, A>::value>
> {
static auto constexpr type() { return TRecord; }
};
template<typename A> struct jit_cpp_type<
A*, std::enable_if_t<std::is_base_of<RecordDesc, A>::value>
> {
static auto constexpr type() { return TRecDesc; }
};
/*
* Parameter types: Many helper functions take various enums or pointers to
* runtime types that have no jit::Type equivalent. These are usually passed as
* untyped arguments to ArgGroup, which come through as TBottom, but some are
* from constant TInt values. Default to TInt for types not handled by
* jit_cpp_type<>, which allows both TInt and TBottom arguments.
*/
template<typename T, typename Enable = Type>
struct jit_param_type {
static auto constexpr type() { return TInt; }
};
template<typename T>
struct jit_param_type<T, decltype(jit_cpp_type<T>::type())> {
static auto constexpr type() { return jit_cpp_type<T>::type(); }
};
/*
* Return types: Unlike argument types, we don't have a default Type for return
* types. All returned values must map to a valid hhir value, so we require a
* jit::Type for every possible C++ return type.
*
* There are two return types that should never be used as an hhir value: void
* and void*. Map those to TTop, so they aren't accepted by any desired return
* type.
*/
template<typename T> struct jit_ret_type : jit_cpp_type<T> {};
template<> struct jit_ret_type<void> {
static auto constexpr type() { return TTop; }
};
template<> struct jit_ret_type<void*> {
static auto constexpr type() { return TTop; }
};
template<typename T>
struct strip_inner_const { using type = T; };
template<typename T>
struct strip_inner_const<T const*> { using type = T*; };
template<typename T>
using strip_t = typename strip_inner_const<std::remove_cv_t<T>>::type;
}
/*
* FuncType holds the signature of a C++ function, with jit::Type equivalents
* for all C++ types involved.
*/
struct FuncType {
FuncType() = default;
template<typename Ret, typename... Args>
FuncType(Ret (*f)(Args...))
: ret{detail::jit_ret_type<detail::strip_t<Ret>>::type()}
, params{detail::jit_param_type<detail::strip_t<Args>>::type()...}
{}
Type ret;
std::vector<Type> params;
};
/*
* Return a pointer to a static FuncType for the given function pointer.
*/
template<typename Ret, typename... Args>
const FuncType* get_func_type(Ret (*f)(Args...)) {
static const FuncType type{f};
return &type;
}
/*
* Get the kind of the given subtype of TArr, if we know it statically.
*/
folly::Optional<ArrayData::ArrayKind> getArrayKind(Type type);
/*
* Information about how to make different sorts of calls from the JIT.
*/
struct CallSpec {
enum class Kind : uint8_t {
/*
* Normal direct call to a known C++ function.
*
* We don't distinguish between our representations for direct calls to
* free functions and those to member functions, with the assumption that
* they have the same calling convention.
*/
Direct,
/*
* Smashable direct call.
*
* Just like `Direct', except we ensure that the call target can be safely
* mutated even in the presence of concurrent execution.
*/
Smashable,
/*
* Call the appropriate destructor (i.e., release) function for the unitary
* argument.
*
* A Vreg containing the data pointer's DataType is used to determine the
* correct function to call.
*/
Destructor,
/*
* Call a unique stub.
*
* Produce a call to JIT code which begins with a prologue{} Vinstr.
*
* TODO(#8425101): Make this true.
*/
Stub,
/*
* Call the appropriate release function for an object.
*
* A Vreg containing the object's class is used to determine the correct
* function to call.
*/
ObjDestructor
};
CallSpec() = delete;
CallSpec(const CallSpec&) = default;
CallSpec& operator=(const CallSpec&) = default;
/////////////////////////////////////////////////////////////////////////////
// Static constructors.
/*
* A Direct call to the free C++ function `fp'. By default, the signature
* will be inferred from the given function pointer. This can be avoided by
* explicitly passing nullptr for the FuncType.
*/
template<class Ret, class... Args>
static CallSpec direct(Ret (*fp)(Args...), const FuncType* type) {
return CallSpec { Kind::Direct, reinterpret_cast<void*>(fp), type };
}
template<class F>
static CallSpec direct(F f) {
return direct(f, get_func_type(f));
}
/*
* A Direct call to the /non-virtual/ C++ instance method function `fp'.
*
* This uses ABI-specific tricks to get the address of the code out of the
* pointer-to-member. Additionally, we use the same Kind to represent both
* free functions and member functions, which assumes that they have the same
* calling convention.
*/
template<class Ret, class Cls, class... Args>
static CallSpec method(Ret (Cls::*fp)(Args...)) {
return CallSpec { Kind::Direct, getMethodPtr(fp) };
}
template<class Ret, class Cls, class... Args>
static CallSpec method(Ret (Cls::*fp)(Args...) const) {
return CallSpec { Kind::Direct, getMethodPtr(fp) };
}
/*
* A Smashable direct call to the free C++ function `fp'.
*/
template<class Ret, class... Args>
static CallSpec smashable(Ret (*fp)(Args...)) {
return CallSpec { Kind::Smashable, reinterpret_cast<void*>(fp) };
}
static CallSpec smashable(TCA fp) {
return CallSpec { Kind::Smashable, fp };
}
/*
* A call to an ArrayData method with type `arrType` and function table `p',
* along with `fp`, the ArrayData generic dispatch method for the table `p`.
* We take `arrType` so we can devirtualize this call when its kind is known;
* otherwise, we'll emit a method call to `fp`. Example usage:
*
* CallSpec::array(arr_type, &g_array_funcs.release, &ArrayData::release);
*
* The call mechanism assumes that the first argument to the direct calls is
* an ArrayData*, and that the rest of the arguments are parallel between the
* direct calls in `p` and the method `fp`.
*
* Note that we also load the ArrayData's kind and use it to make an indirect
* call to a method in `p` in the JIT; in fact, we used to emit such code.
* However, doing so is a branch miss loss (in particular, for set methods)
* over calling `fp`. We're not sure why, but here are two hypotheses:
*
* 1. BOLT can inline the likely virtual target into the function `fp`.
* 2. We may be save on indirect-call prediction cache with only one call.
*
* In any case, making this change looks like it's neutral to a small win in
* load tests, in addition to generating cleaner code.
*/
template<class Ret, class... Args>
static CallSpec array(
const Type& arr_type,
Ret (*const (*p)[ArrayData::kNumKinds])(ArrayData*, Args...),
Ret (ArrayData::*fp)(Args...)) {
if (auto const kind = getArrayKind(arr_type)) {
return direct((*p)[*kind]);
}
return method(fp);
}
template<class Ret, class... Args>
static CallSpec array(
const Type& arr_type,
Ret (*const (*p)[ArrayData::kNumKinds])(const ArrayData*, Args...),
Ret (ArrayData::*fp)(Args...) const) {
if (auto const kind = getArrayKind(arr_type)) {
return direct((*p)[*kind]);
}
return method(fp);
}
/*
* A Destructor call for the DataType in `r'.
*/
static CallSpec destruct(Vreg r) {
return CallSpec { Kind::Destructor, r };
}
/*
* A Stub call to `addr'.
*/
static CallSpec stub(TCA addr) {
return CallSpec { Kind::Stub, addr };
}
/*
* A Destructor for an object with class `cls'.
*/
static CallSpec objDestruct(Vreg cls) {
return CallSpec { Kind::ObjDestructor, cls };
}
/////////////////////////////////////////////////////////////////////////////
// Accessors.
/*
* Return the type tag.
*/
Kind kind() const { return m_typeKind.tag(); }
/*
* The address of a C++ function, for Direct or Smashable calls.
*/
void* address() const {
assertx(kind() == Kind::Direct ||
kind() == Kind::Smashable);
return m_u.fp;
}
/*
* The register containing the DataType, for Destructor calls.
*/
Vreg reg() const {
assertx(kind() == Kind::Destructor ||
kind() == Kind::ObjDestructor);
return m_u.reg;
}
/*
* The address of the unique stub, for Stub calls.
*/
TCA stubAddr() const {
assertx(kind() == Kind::Stub);
return m_u.stub;
}
/*
* If this CallSpec was constructed with a FuncKind, verify that the given
* argument types and expected return type match the FuncKind.
*/
bool verifySignature(const CallDest& dest,
const std::vector<Type>& args) const;
/////////////////////////////////////////////////////////////////////////////
bool operator==(const CallSpec& o) const {
auto const k1 = kind();
auto const k2 = o.kind();
if (k1 != k2) return false;
switch (k1) {
case CallSpec::Kind::Direct:
case CallSpec::Kind::Smashable: return address() == o.address();
case CallSpec::Kind::Destructor: return reg() == o.reg();
case CallSpec::Kind::Stub: return stubAddr() == o.stubAddr();
case CallSpec::Kind::ObjDestructor: return reg() == o.reg();
}
always_assert(false);
}
bool operator!=(const CallSpec& o) const { return !(*this == o); }
private:
union U {
/* implicit */ U(void* fp) : fp(fp) {}
/* implicit */ U(Vreg reg) : reg(reg) {}
/* implicit */ U(TCA stub) : stub(stub) {}
void* fp;
Vreg reg;
TCA stub;
};
private:
CallSpec(Kind k, U u, const FuncType* type = nullptr)
: m_typeKind{k, type}
, m_u{u}
{}
private:
CompactTaggedPtr<const FuncType, Kind> m_typeKind;
U m_u;
};
///////////////////////////////////////////////////////////////////////////////
}}
#endif