OLD | NEW |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 #ifndef INCLUDE_STATIC_FFI_H_ | 5 #ifndef INCLUDE_STATIC_FFI_H_ |
6 #define INCLUDE_STATIC_FFI_H_ | 6 #define INCLUDE_STATIC_FFI_H_ |
7 | 7 |
8 #include "include/fletch_api.h" | 8 #include "include/dartino_api.h" |
9 | 9 |
10 /** | 10 /** |
11 * The static FFI interface of fletch can be used in two ways. The easiest way | 11 * The static FFI interface of dartino can be used in two ways. The easiest way |
12 * is to define an export table that covers all functions that should be | 12 * is to define an export table that covers all functions that should be |
13 * available via FFI. This is done using the FLETCH_EXPORT_TABLE macros | 13 * available via FFI. This is done using the DARTINO_EXPORT_TABLE macros |
14 * defined below. | 14 * defined below. |
15 * | 15 * |
16 * FLETCH_EXPORT_TABLE_BEGIN | 16 * DARTINO_EXPORT_TABLE_BEGIN |
17 * FLETCH_EXPORT_TABLE_ENTRY("magic_meat", FFITestMagicMeat) | 17 * DARTINO_EXPORT_TABLE_ENTRY("magic_meat", FFITestMagicMeat) |
18 * FLETCH_EXPORT_TABLE_ENTRY("magic_veg", FFITestMagicVeg) | 18 * DARTINO_EXPORT_TABLE_ENTRY("magic_veg", FFITestMagicVeg) |
19 * FLETCH_EXPORT_TABLE_END | 19 * DARTINO_EXPORT_TABLE_END |
20 * | 20 * |
21 * While easy to integrate into an existing build, this solution does not | 21 * While easy to integrate into an existing build, this solution does not |
22 * compose well. All exported functions have to be declared in a single | 22 * compose well. All exported functions have to be declared in a single |
23 * location. | 23 * location. |
24 * | 24 * |
25 * Alternatively, a linker script can be used to collect the exported | 25 * Alternatively, a linker script can be used to collect the exported |
26 * functions from various files. For this, you have to add the following to | 26 * functions from various files. For this, you have to add the following to |
27 * the output declaration of the rodata section in your linker script | 27 * the output declaration of the rodata section in your linker script |
28 * | 28 * |
29 * . = ALIGN(4); | 29 * . = ALIGN(4); |
30 * fletch_ffi_table = .; | 30 * dartino_ffi_table = .; |
31 * KEEP(*(.fletchffi)) | 31 * KEEP(*(.dartinoffi)) |
32 * QUAD(0) | 32 * QUAD(0) |
33 * QUAD(0) | 33 * QUAD(0) |
34 * . = ALIGN(4); | 34 * . = ALIGN(4); |
35 * | 35 * |
36 * and export all external functions you want to call via FFI using the below | 36 * and export all external functions you want to call via FFI using the below |
37 * two macros: | 37 * two macros: |
38 * | 38 * |
39 * FLETCH_EXPORT_STATIC(fun) exports the C function 'fun' as 'fun' in dart. | 39 * DARTINO_EXPORT_STATIC(fun) exports the C function 'fun' as 'fun' in dart. |
40 * | 40 * |
41 * FLETCH_EXPORT_STATIC_RENAME(name, fun) exports the C function 'fun' as | 41 * DARTINO_EXPORT_STATIC_RENAME(name, fun) exports the C function 'fun' as |
42 * 'name' in dart. | 42 * 'name' in dart. |
43 */ | 43 */ |
44 | 44 |
45 typedef struct { | 45 typedef struct { |
46 const char* const name; | 46 const char* const name; |
47 const void* const ptr; | 47 const void* const ptr; |
48 } FletchStaticFFISymbol; | 48 } DartinoStaticFFISymbol; |
49 | 49 |
50 #ifdef __cplusplus | 50 #ifdef __cplusplus |
51 #define FLETCH_EXPORT_TABLE_BEGIN \ | 51 #define DARTINO_EXPORT_TABLE_BEGIN \ |
52 extern "C" { \ | 52 extern "C" { \ |
53 FLETCH_VISIBILITY_DEFAULT FletchStaticFFISymbol fletch_ffi_table[] = { | 53 DARTINO_VISIBILITY_DEFAULT DartinoStaticFFISymbol dartino_ffi_table[] = { |
54 #define FLETCH_EXPORT_TABLE_ENTRY(name, fun) \ | 54 #define DARTINO_EXPORT_TABLE_ENTRY(name, fun) \ |
55 {name, reinterpret_cast<const void*>(&fun)}, | 55 {name, reinterpret_cast<const void*>(&fun)}, |
56 #define FLETCH_EXPORT_TABLE_END {NULL, NULL}};} | 56 #define DARTINO_EXPORT_TABLE_END {NULL, NULL}};} |
57 #else | 57 #else |
58 #define FLETCH_EXPORT_TABLE_BEGIN \ | 58 #define DARTINO_EXPORT_TABLE_BEGIN \ |
59 FLETCH_VISIBILITY_DEFAULT FletchStaticFFISymbol fletch_ffi_table[] = { | 59 DARTINO_VISIBILITY_DEFAULT DartinoStaticFFISymbol dartino_ffi_table[] = { |
60 #define FLETCH_EXPORT_TABLE_ENTRY(name, fun) {name, &fun}, | 60 #define DARTINO_EXPORT_TABLE_ENTRY(name, fun) {name, &fun}, |
61 #define FLETCH_EXPORT_TABLE_END {NULL, NULL}}; | 61 #define DARTINO_EXPORT_TABLE_END {NULL, NULL}}; |
62 #endif | 62 #endif |
63 | 63 |
64 #define FLETCH_FUNCTION_NAME(name) #name | 64 #define DARTINO_FUNCTION_NAME(name) #name |
65 #define FLETCH_EXPORT_FFI FLETCH_EXPORT __attribute__((section(".fletchffi"))) | 65 #define DARTINO_EXPORT_FFI \ |
| 66 DARTINO_EXPORT __attribute__((section(".dartinoffi"))) |
66 | 67 |
67 #define FLETCH_EXPORT_STATIC(fun) \ | 68 #define DARTINO_EXPORT_STATIC(fun) \ |
68 FLETCH_EXPORT_FFI FletchStaticFFISymbol fletch_ffi_entry_ ## fun = { \ | 69 DARTINO_EXPORT_FFI DartinoStaticFFISymbol dartino_ffi_entry_ ## fun = { \ |
69 FLETCH_FUNCTION_NAME(fun), \ | 70 DARTINO_FUNCTION_NAME(fun), \ |
70 &fun }; \ | 71 &fun }; \ |
71 | 72 |
72 #define FLETCH_EXPORT_STATIC_RENAME(name, fun) \ | 73 #define DARTINO_EXPORT_STATIC_RENAME(name, fun) \ |
73 FLETCH_EXPORT_FFI FletchStaticFFISymbol fletch_ffi_entry_ ## name = { \ | 74 DARTINO_EXPORT_FFI DartinoStaticFFISymbol dartino_ffi_entry_ ## name = { \ |
74 #name, &fun }; \ | 75 #name, &fun }; \ |
75 | 76 |
76 #endif // INCLUDE_STATIC_FFI_H_ | 77 #endif // INCLUDE_STATIC_FFI_H_ |
OLD | NEW |