This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffi.d
580 lines (515 loc) · 10.6 KB
/
ffi.d
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
module ffi;
enum ffi_status
{
FFI_OK,
FFI_BAD_TYPEDEF,
FFI_BAD_ABI,
}
version (X86)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_SYSV
}
}
else version (X86_64)
{
version (Windows)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_WIN64
}
}
else
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 2, // FFI_UNIX64
}
}
}
else version (ARM)
{
enum ffi_abi
{
// TODO: Check for VFP (FFI_VFP).
FFI_DEFAULT_ABI = 1, // FFI_SYSV
}
}
else version (PPC)
{
version (AIX)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_AIX
}
}
else version (OSX)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_DARWIN
}
}
else version (FreeBSD)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_SYSV
}
}
else
{
enum ffi_abi
{
// TODO: Detect soft float (FFI_LINUX_SOFT_FLOAT) and FFI_LINUX.
FFI_DEFAULT_ABI = 2, // FFI_GCC_SYSV
}
}
}
else version (PPC64)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 3, // FFI_LINUX64
}
}
else version (IA64)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_UNIX
}
}
else version (MIPS)
{
enum ffi_abi
{
// TODO: Detect soft float (FFI_*_SOFT_FLOAT).
// TODO: Detect O32 vs N32.
FFI_DEFAULT_ABI = 1, // FFI_O32
}
}
else version (MIPS64)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 3, // FFI_N64
}
}
else version (SPARC)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_V8
}
}
else version (SPARC64)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 3, // FFI_V9
}
}
else version (S390)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_SYSV
}
}
else version (S390X)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_SYSV
}
}
else version (HPPA)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_PA32
}
}
else version (HPPA64)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_PA64
}
}
else version (SH)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_SYSV
}
}
else version (SH64)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_SYSV
}
}
else version (Alpha)
{
enum ffi_abi
{
FFI_DEFAULT_ABI = 1, // FFI_OSF
}
}
else
static assert(false, "Unsupported architecture/platform.");
struct ffi_type
{
size_t size;
ushort alignment;
ushort type;
ffi_type** elements;
}
struct ffi_cif
{
int abi;
uint nargs;
ffi_type** arg_types;
ffi_type* rtype;
uint bytes;
uint flags;
}
struct ffi_closure
{
char[128] tramp;
ffi_cif* cif;
ffi_closure_fun fun;
void* user_data;
}
extern (C)
{
alias void function(ffi_cif*, void*, void**, void*) ffi_closure_fun;
extern __gshared
{
ffi_type ffi_type_void;
ffi_type ffi_type_uint8;
ffi_type ffi_type_sint8;
ffi_type ffi_type_uint16;
ffi_type ffi_type_sint16;
ffi_type ffi_type_uint32;
ffi_type ffi_type_sint32;
ffi_type ffi_type_uint64;
ffi_type ffi_type_sint64;
ffi_type ffi_type_float;
ffi_type ffi_type_double;
ffi_type ffi_type_pointer;
}
nothrow
{
ffi_status ffi_prep_cif(ffi_cif* cif,
ffi_abi abi,
uint nargs,
ffi_type* rtype,
ffi_type** atypes);
void ffi_call(ffi_cif* cif,
void* fn,
void* rvalue,
void** avalue);
void* ffi_closure_alloc(size_t size,
void** code);
void ffi_closure_free(void* writable);
ffi_status ffi_prep_closure_loc(ffi_closure* closure,
ffi_cif* cif,
ffi_closure_fun fun,
void* user_data,
void* codeloc);
}
}
struct FFIType
{
private ffi_type* _type;
private this(ffi_type* type)
{
_type = type;
}
this(FFIType*[] fields)
in
{
foreach (field; fields)
assert(field);
}
body
{
_type = new ffi_type();
_type.type = 13; // FFI_TYPE_STRUCT
ffi_type*[] f;
foreach (fld; fields)
f ~= fld._type;
_type.elements = f.ptr;
}
shared static this()
{
_ffiVoid = FFIType(&ffi_type_void);
_ffiByte = FFIType(&ffi_type_sint8);
_ffiUByte = FFIType(&ffi_type_uint8);
_ffiShort = FFIType(&ffi_type_sint16);
_ffiUShort = FFIType(&ffi_type_uint16);
_ffiInt = FFIType(&ffi_type_sint32);
_ffiUInt = FFIType(&ffi_type_uint32);
_ffiLong = FFIType(&ffi_type_sint64);
_ffiULong = FFIType(&ffi_type_uint64);
_ffiFloat = FFIType(&ffi_type_float);
_ffiDouble = FFIType(&ffi_type_double);
_ffiPointer = FFIType(&ffi_type_pointer);
}
private __gshared FFIType _ffiVoid;
private __gshared FFIType _ffiByte;
private __gshared FFIType _ffiUByte;
private __gshared FFIType _ffiShort;
private __gshared FFIType _ffiUShort;
private __gshared FFIType _ffiInt;
private __gshared FFIType _ffiUInt;
private __gshared FFIType _ffiLong;
private __gshared FFIType _ffiULong;
private __gshared FFIType _ffiFloat;
private __gshared FFIType _ffiDouble;
private __gshared FFIType _ffiPointer;
@property static FFIType* ffiVoid()
out (result)
{
assert(result);
}
body
{
return &_ffiVoid;
}
@property static FFIType* ffiByte()
out (result)
{
assert(result);
}
body
{
return &_ffiByte;
}
@property static FFIType* ffiUByte()
out (result)
{
assert(result);
}
body
{
return &_ffiUByte;
}
@property static FFIType* ffiShort()
out (result)
{
assert(result);
}
body
{
return &_ffiShort;
}
@property static FFIType* ffiUShort()
out (result)
{
assert(result);
}
body
{
return &_ffiUShort;
}
@property static FFIType* ffiInt()
out (result)
{
assert(result);
}
body
{
return &_ffiInt;
}
@property static FFIType* ffiUInt()
out (result)
{
assert(result);
}
body
{
return &_ffiUInt;
}
@property static FFIType* ffiLong()
out (result)
{
assert(result);
}
body
{
return &_ffiLong;
}
@property static FFIType* ffiULong()
out (result)
{
assert(result);
}
body
{
return &_ffiULong;
}
@property static FFIType* ffiFloat()
out (result)
{
assert(result);
}
body
{
return &_ffiFloat;
}
@property static FFIType* ffiDouble()
out (result)
{
assert(result);
}
body
{
return &_ffiDouble;
}
@property static FFIType* ffiPointer()
out (result)
{
assert(result);
}
body
{
return &_ffiPointer;
}
}
enum FFIStatus
{
success,
badType,
badABI,
}
version (Win32)
{
enum FFIInterface
{
platform,
stdCall,
}
}
else
{
enum FFIInterface
{
platform,
}
}
alias void function() FFIFunction;
FFIStatus ffiCall(FFIFunction func,
FFIType* returnType,
FFIType*[] parameterTypes,
void* returnValue,
void*[] argumentValues,
FFIInterface abi = FFIInterface.platform)
in
{
assert(func);
assert(returnType);
foreach (param; parameterTypes)
assert(param);
if (returnType != FFIType.ffiVoid)
assert(returnValue);
foreach (arg; argumentValues)
assert(arg);
assert(argumentValues.length == parameterTypes.length);
}
body
{
ffi_type*[] argTypes;
foreach (param; parameterTypes)
argTypes ~= param._type;
int selectedABI = ffi_abi.FFI_DEFAULT_ABI;
version (Win32)
{
if (abi == FFIInterface.stdCall)
selectedABI = 2; // FFI_STDCALL
}
ffi_cif cif;
auto status = ffi_prep_cif(&cif, cast(ffi_abi)selectedABI, cast(uint)argTypes.length, returnType._type, argTypes.ptr);
if (status != ffi_status.FFI_OK)
return cast(FFIStatus)status;
ffi_call(&cif, cast(void*)func, returnValue, argumentValues.ptr);
return FFIStatus.success;
}
final class FFIClosure
{
private ffi_cif* _cif;
private FFIFunction _function;
private FFIClosureFunction _closure;
private void* _memory;
private this(ffi_cif* cif, void* memory, FFIFunction function_, FFIClosureFunction closure)
{
_cif = cif;
_memory = memory;
_function = function_;
_closure = closure;
}
~this()
{
ffi_closure_free(_memory);
}
@property FFIFunction* function_()
out (result)
{
assert(*result);
}
body
{
return &_function;
}
@property FFIClosureFunction* closure()
out (result)
{
assert(*result);
}
body
{
return &_closure;
}
}
alias void delegate(void*, void**) FFIClosureFunction;
private extern (C) void closureHandler(ffi_cif* cif, void* ret, void** args, FFIClosure closure)
{
auto cb = *closure.closure;
cb(ret, args);
}
FFIClosure ffiClosure(FFIClosureFunction func,
FFIType* returnType,
FFIType*[] parameterTypes,
FFIInterface abi = FFIInterface.platform)
in
{
assert(func);
}
body
{
ffi_type*[] argTypes;
foreach (param; parameterTypes)
argTypes ~= param._type;
int selectedABI = ffi_abi.FFI_DEFAULT_ABI;
version (Win32)
{
if (abi == FFIInterface.stdCall)
selectedABI = 2; // FFI_STDCALL
}
auto cif = new ffi_cif();
if (ffi_prep_cif(cif, cast(ffi_abi)selectedABI, cast(uint)argTypes.length, returnType._type, argTypes.ptr) != ffi_status.FFI_OK)
return null;
void* code;
auto mem = cast(ffi_closure*)ffi_closure_alloc(ffi_closure.sizeof, &code);
auto closure = new FFIClosure(cif, mem, cast(FFIFunction)code, func);
if (ffi_prep_closure_loc(mem, cif, cast(ffi_closure_fun)&closureHandler, cast(void*)closure, code) != ffi_status.FFI_OK)
return null;
return closure;
}