1 module orelang.Util;
2 
3 version (WithFFI) {
4 extern (C) {
5   struct ffi_type {
6     size_t size;
7     ushort alignment;
8     ushort type;
9     ffi_type** elements;
10   }
11 
12   extern __gshared {
13     ffi_type ffi_type_void;
14     ffi_type ffi_type_uint8;
15     ffi_type ffi_type_sint8;
16     ffi_type ffi_type_uint16;
17     ffi_type ffi_type_sint16;
18     ffi_type ffi_type_uint32;
19     ffi_type ffi_type_sint32;
20     ffi_type ffi_type_uint64;
21     ffi_type ffi_type_sint64;
22     ffi_type ffi_type_float;
23     ffi_type ffi_type_double;
24     ffi_type ffi_type_pointer;
25 
26     ffi_type ffi_type_longdouble;
27   }
28 
29   enum ffi_status {
30     FFI_OK = 0,
31     FFI_BAD_TYPEDEF,
32     FFI_BAD_ABI
33   }
34 
35   struct ffi_cif {
36     ffi_abi abi;
37     uint nargs;
38     ffi_type** arg_types;
39     ffi_type* rtype;
40     uint bytes;
41     uint flags;
42   }
43 
44   alias ffi_arg = ulong;
45   alias ffi_sarg = long;
46 
47   enum ffi_abi {
48     FFI_FIRST_ABI = 0,
49     FFI_SYSV,
50     FFI_UNIX64, /* Unix variants all use the same ABI for x86-64  */
51     FFI_THISCALL,
52     FFI_FASTCALL,
53     FFI_STDCALL,
54     FFI_PASCAL,
55     FFI_REGISTER,
56     FFI_LAST_ABI,
57     FFI_DEFAULT_ABI = FFI_UNIX64
58   }
59 
60   ffi_status ffi_prep_cif(ffi_cif* cif, ffi_abi abi, uint nargs,
61       ffi_type* rtype, ffi_type** atypes);
62 
63   ffi_status ffi_prep_cif_var(ffi_cif* cif, ffi_abi abi, uint nfixedargs,
64       uint ntotalargs, ffi_type* rtype, ffi_type** atypes);
65 
66   void ffi_call(ffi_cif* cif, //void (*fn)(void),
67       void* fn, void* rvalue, void** avalue);
68 }
69 }