forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp-call.h
More file actions
169 lines (150 loc) · 5.05 KB
/
Copy pathcpp-call.h
File metadata and controls
169 lines (150 loc) · 5.05 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
/*
+----------------------------------------------------------------------+
| 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 |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#ifndef incl_HPHP_VM_JIT_CPP_CALL_H_
#define incl_HPHP_VM_JIT_CPP_CALL_H_
#include "hphp/runtime/base/array-data.h"
#include "hphp/runtime/vm/jit/vasm-reg.h"
#include <cstdint>
namespace HPHP { namespace jit {
/*
* Information about how to make different sorts of calls from the JIT
* to C++ code.
*/
struct CppCall {
enum class Kind : uint8_t {
/*
* Normal direct calls to a known address. We don't distinguish
* between direct calls to methods or direct calls to free
* functions, with the assumption they have the same calling
* convention.
*/
Direct,
/*
* Limited support for C++ virtual function calls (simple single
* inheritance situations---we're not supporting things like this
* pointer adjustments, etc).
*/
Virtual,
/*
* Call through the "rotated" ArrayData vtable. This is used to
* call ArrayData apis by loading a function pointer out of
* g_array_funcs at an offset for the supplied function, indexing
* by array kind.
*/
ArrayVirt,
/*
* Call Destructor function using DataType as an index; expect
* the type in rsi, which we will destroy
*/
Destructor,
};
CppCall() = delete;
CppCall(const CppCall&) = default;
/*
* Create a CppCall that represents a direct call to a non-member
* function.
*/
template<class Ret, class... Args>
static CppCall direct(Ret (*pfun)(Args...)) {
return CppCall { Kind::Direct, reinterpret_cast<void*>(pfun) };
}
/*
* Create a CppCall that targets a /non-virtual/ C++ instance
* method.
*
* Note: this uses ABI-specific tricks to get the address of the
* code out of the pointer-to-member, and the fact that we just
* treat it as the same Kind after this stuff requires that the
* calling convention for member functions and non-member functions
* is the same.
*/
template<class Ret, class Cls, class... Args>
static CppCall method(Ret (Cls::*fp)(Args...)) {
return CppCall { Kind::Direct, getMethodPtr(fp) };
}
template<class Ret, class Cls, class... Args>
static CppCall method(Ret (Cls::*fp)(Args...) const) {
return CppCall { Kind::Direct, getMethodPtr(fp) };
}
/*
* Call an array function. Takes a pointer to an array of function
* pointers to use for the particular entry point. For example,
*
* CppCall::array(&g_array_funcs.nvGetInt)
*
* The call mechanism assumes that the first argument to the function is an
* ArrayData*, and loads the kind from there. This should only be used if
* you know that the array data vtable is in low memory---e.g. in
* code-gen-x64 you should go through arrayCallIfLowMem() first.
*/
template<class Ret, class... Args>
static CppCall array(Ret (*const (*p)[ArrayData::kNumKinds])(Args...)) {
const void* vp = p;
return CppCall { Kind::ArrayVirt, const_cast<void*>(vp) };
}
/*
* Create a CppCall that represents a call to a virtual function.
*/
static CppCall virt(int vtableOffset) {
return CppCall { Kind::Virtual, vtableOffset };
}
/*
* Call destructor using r as function table index
*/
static CppCall destruct(Vreg r) {
return CppCall { Kind::Destructor, r };
}
/*
* Return the type tag.
*/
Kind kind() const { return m_kind; }
/*
* The point of this class is to discriminate these three fields are
* mutually exclusive, depending on the kind.
*/
void* address() const {
assert(kind() == Kind::Direct);
return m_u.fptr;
}
int vtableOffset() const {
assert(m_kind == Kind::Virtual);
return m_u.vtableOffset;
}
Vreg reg() const {
assert(m_kind == Kind::Destructor);
return m_u.reg;
}
void* arrayTable() const {
assert(kind() == Kind::ArrayVirt);
return m_u.fptr;
}
private:
union U {
/* implicit */ U(void* fptr) : fptr(fptr) {}
/* implicit */ U(int vtableOffset) : vtableOffset(vtableOffset) {}
/* implicit */ U(Vreg reg) : reg(reg) {}
void* fptr;
int vtableOffset;
Vreg reg;
};
private:
CppCall(Kind k, U u) : m_kind(k), m_u(u) {}
private:
Kind m_kind;
U m_u;
};
} }
#endif