-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathFFICall.cpp
More file actions
312 lines (272 loc) · 11.6 KB
/
FFICall.cpp
File metadata and controls
312 lines (272 loc) · 11.6 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
#include "FFICall.h"
#include "ArgConverter.h"
#include "Helpers.h"
#include <sstream>
namespace tns {
ffi_type* FFICall::GetArgumentType(const TypeEncoding* typeEncoding, bool isStructMember) {
switch (typeEncoding->type) {
case BinaryTypeEncodingType::VoidEncoding: {
return &ffi_type_void;
}
case BinaryTypeEncodingType::IdEncoding:
case BinaryTypeEncodingType::InterfaceDeclarationReference:
case BinaryTypeEncodingType::InstanceTypeEncoding:
case BinaryTypeEncodingType::SelectorEncoding:
case BinaryTypeEncodingType::BlockEncoding:
case BinaryTypeEncodingType::CStringEncoding:
case BinaryTypeEncodingType::ClassEncoding:
case BinaryTypeEncodingType::PointerEncoding:
case BinaryTypeEncodingType::ProtocolEncoding:
case BinaryTypeEncodingType::FunctionPointerEncoding:
case BinaryTypeEncodingType::IncompleteArrayEncoding: {
return &ffi_type_pointer;
}
case BinaryTypeEncodingType::UnicharEncoding: {
return &ffi_type_ushort;
}
case BinaryTypeEncodingType::BoolEncoding:
case BinaryTypeEncodingType::UCharEncoding:
case BinaryTypeEncodingType::CharEncoding: {
return &ffi_type_sint8;
}
case BinaryTypeEncodingType::UShortEncoding: {
return &ffi_type_uint16;
}
case BinaryTypeEncodingType::ShortEncoding: {
return &ffi_type_sint16;
}
case BinaryTypeEncodingType::UIntEncoding: {
return &ffi_type_uint32;
}
case BinaryTypeEncodingType::IntEncoding: {
return &ffi_type_sint32;
}
case BinaryTypeEncodingType::ULongEncoding: {
#if defined(__LP64__)
return &ffi_type_uint64;
#else
return &ffi_type_uint32;
#endif
}
case BinaryTypeEncodingType::LongEncoding: {
#if defined(__LP64__)
return &ffi_type_sint64;
#else
return &ffi_type_sint32;
#endif
}
case BinaryTypeEncodingType::ULongLongEncoding: {
return &ffi_type_uint64;
}
case BinaryTypeEncodingType::LongLongEncoding: {
return &ffi_type_sint64;
}
case BinaryTypeEncodingType::FloatEncoding: {
return &ffi_type_float;
}
case BinaryTypeEncodingType::DoubleEncoding: {
return &ffi_type_double;
}
case BinaryTypeEncodingType::ExtVectorEncoding: {
size_t size = typeEncoding->details.extVector.size;
#if defined(__x86_64__)
// We need isStructMember because double3 vectors are handled
// differently in x86_64. When a vector is a struct field
// it is passed in memory but when not - the ST0 register is
// used for the third element. In armv8 double3 vector will always
// be passed in memory (as it's size > 16).
if (size == 3 && isStructMember) {
#else
// For armv8 we always need to pass the array size
// as the vector would fill a whole register in order
// to calculate the proper flags value.
if (size == 3) {
#endif
size = 4;
}
const TypeEncoding* innerType = typeEncoding->details.extVector.getInnerType();
ffi_type* innerFFIType = FFICall::GetArgumentType(innerType, isStructMember);
ffi_type* type = new ffi_type({ .size = size * innerFFIType->size, .alignment = innerFFIType->alignment, .type = FFI_TYPE_EXT_VECTOR });
type->elements = new ffi_type*[size + 1];
if (size > 0) {
std::fill(type->elements, type->elements + size, innerFFIType);
}
type->elements[size] = nullptr;
return type;
}
case BinaryTypeEncodingType::StructDeclarationReference: {
const char* structName = typeEncoding->details.declarationReference.name.valuePtr();
const Meta* meta = ArgConverter::GetMeta(structName);
tns::Assert(meta->type() == MetaType::Struct);
const StructMeta* structMeta = static_cast<const StructMeta*>(meta);
StructInfo structInfo = FFICall::GetStructInfo(structMeta, structName);
return structInfo.FFIType();
}
case BinaryTypeEncodingType::ConstantArrayEncoding: {
if (isStructMember) {
const TypeEncoding* innerType = typeEncoding->details.constantArray.getInnerType();
ffi_type* innerFFIType = FFICall::GetArgumentType(innerType, isStructMember);
int32_t size = typeEncoding->details.constantArray.size;
ffi_type* ffiType = new ffi_type({ .size = size * innerFFIType->size, .alignment = innerFFIType->alignment, .type = FFI_TYPE_STRUCT });
ffiType->elements = new ffi_type*[size + 1];
if (size > 0) {
std::fill(ffiType->elements, ffiType->elements + size, innerFFIType);
}
ffiType->elements[size] = nullptr;
return ffiType;
}
return &ffi_type_pointer;
}
case BinaryTypeEncodingType::AnonymousStructEncoding: {
size_t count = typeEncoding->details.anonymousRecord.fieldsCount;
const TypeEncoding* fieldEncoding = typeEncoding->details.anonymousRecord.getFieldsEncodings();
const String* fieldNames = typeEncoding->details.anonymousRecord.getFieldNames();
StructInfo structInfo = FFICall::GetStructInfo(count, fieldEncoding, fieldNames);
return structInfo.FFIType();
}
default: {
break;
}
}
// TODO: implement all the possible encoding types
tns::Assert(false);
return nullptr;
}
void FFICall::DisposeFFIType(ffi_type* type, const TypeEncoding* typeEncoding) {
if (type == nullptr) {
return;
}
switch (typeEncoding->type) {
case BinaryTypeEncodingType::ExtVectorEncoding: {
// dispose innerFFIType
if(type->elements[0] != nullptr) {
DisposeFFIType(type->elements[0], typeEncoding->details.extVector.getInnerType());
}
delete[] type->elements;
delete type;
break;
}
case BinaryTypeEncodingType::ConstantArrayEncoding: {
if(type == &ffi_type_pointer) {
break;
}
if(type->elements[0] != nullptr) {
DisposeFFIType(type->elements[0], typeEncoding->details.constantArray.getInnerType());
}
delete[] type->elements;
delete type;
break;
}
default:
break;
}
}
StructInfo FFICall::GetStructInfo(const StructMeta* structMeta, std::string structName) {
size_t fieldsCount = structMeta->fieldsCount();
const TypeEncoding* fieldEncoding = structMeta->fieldsEncodings()->first();
const String* fieldNames = structMeta->fieldNames().first();
if (structName.empty()) {
structName = structMeta->name();
}
StructInfo structInfo = FFICall::GetStructInfo(fieldsCount, fieldEncoding, fieldNames, structName);
return structInfo;
}
StructInfo FFICall::GetStructInfo(size_t fieldsCount, const TypeEncoding* fieldEncoding, const String* fieldNames, std::string structName) {
if (structName.empty()) {
const TypeEncoding* temp = fieldEncoding;
std::stringstream ss;
for (int i = 0; i < fieldsCount; i++) {
std::string fieldName = fieldNames[i].valuePtr();
ss << fieldName << "_" << temp->type;
temp = temp->next();
}
structName = ss.str();
}
auto it = structInfosCache_.find(structName);
if (it != structInfosCache_.end()) {
return it->second;
}
std::vector<StructField> fields;
fields.reserve(fieldsCount);
ffi_type* ffiType = new ffi_type({ .size = 0, .alignment = 0, .type = FFI_TYPE_STRUCT });
ffiType->elements = new ffi_type*[fieldsCount + 1];
#if defined(__x86_64__)
bool hasNestedStruct = false;
#endif
for (int i = 0; i < fieldsCount; i++) {
ffi_type* fieldFFIType = FFICall::GetArgumentType(fieldEncoding, true);
#if defined(__x86_64__)
hasNestedStruct = hasNestedStruct || (fieldFFIType->type == FFI_TYPE_STRUCT);
#endif
ffiType->elements[i] = fieldFFIType;
size_t offset = ffiType->size;
unsigned short alignment = fieldFFIType->alignment;
size_t padding = (alignment - (offset % alignment)) % alignment;
std::string fieldName = fieldNames[i].valuePtr();
offset += padding;
StructField field(offset, fieldFFIType, fieldName, fieldEncoding);
fields.push_back(field);
ffiType->size = offset + fieldFFIType->size;
ffiType->alignment = std::max(ffiType->alignment, fieldFFIType->alignment);
fieldEncoding = fieldEncoding->next();
}
ffiType->elements[fieldsCount] = nullptr;
#if defined(__x86_64__)
/*
If on 64-bit architecture, flatten the nested structures, because libffi can't handle them.
*/
if (hasNestedStruct) {
std::vector<ffi_type*> flattenedFfiTypes;
std::vector<ffi_type*> stack; // simulate recursion with stack (no need of other function)
stack.push_back(ffiType);
while (!stack.empty()) {
ffi_type* currentType = stack.back();
stack.pop_back();
if (currentType->type != FFI_TYPE_STRUCT) {
flattenedFfiTypes.push_back(currentType);
} else {
ffi_type** nullPtr = currentType->elements; // the end of elements array
while (*nullPtr != nullptr) {
nullPtr++;
}
// add fields' ffi types in reverse order in the stack, so they will be popped in correct order
for (ffi_type** field = nullPtr - 1; field >= currentType->elements; field--) {
stack.push_back(*field);
}
}
}
delete[] ffiType->elements;
ffiType->elements = new ffi_type*[flattenedFfiTypes.size() + 1];
memcpy(ffiType->elements, flattenedFfiTypes.data(), flattenedFfiTypes.size() * sizeof(ffi_type*));
ffiType->elements[flattenedFfiTypes.size()] = nullptr;
}
#endif
StructInfo structInfo(structName, ffiType, fields);
structInfosCache_.emplace(structName, structInfo);
return structInfo;
}
ParametrizedCall* ParametrizedCall::Get(const TypeEncoding* typeEncoding, const int initialParameterIndex, const int argsCount) {
auto it = callsCache_.find(typeEncoding);
if (it != callsCache_.end()) {
return it->second;
}
const ffi_type** parameterTypesFFITypes = new const ffi_type*[argsCount]();
ffi_type* returnType = FFICall::GetArgumentType(typeEncoding);
for (int i = 0; i < initialParameterIndex; i++) {
parameterTypesFFITypes[i] = &ffi_type_pointer;
}
const TypeEncoding* enc = typeEncoding;
for (int i = initialParameterIndex; i < argsCount; i++) {
enc = enc->next();
parameterTypesFFITypes[i] = FFICall::GetArgumentType(enc);
}
ffi_cif* cif = new ffi_cif();
ffi_status status = ffi_prep_cif(cif, FFI_DEFAULT_ABI, argsCount, returnType, const_cast<ffi_type**>(parameterTypesFFITypes));
tns::Assert(status == FFI_OK);
ParametrizedCall* call = new ParametrizedCall(cif);
callsCache_.emplace(typeEncoding, call);
return call;
}
robin_hood::unordered_map<const TypeEncoding*, ParametrizedCall*> ParametrizedCall::callsCache_;
robin_hood::unordered_map<std::string, StructInfo> FFICall::structInfosCache_;
}