| 1 |
= ForeignFunctionInterfaceTypes |
| 2 |
|
| 3 |
MLton's <<ForeignFunctionInterface#>> only allows values of certain SML |
| 4 |
types to be passed between SML and C. The following types are |
| 5 |
allowed: `bool`, `char`, `int`, `real`, `word`. All of the different |
| 6 |
sizes of (fixed-sized) integers, reals, and words are supported as |
| 7 |
well: `Int8.int`, `Int16.int`, `Int32.int`, `Int64.int`, |
| 8 |
`Real32.real`, `Real64.real`, `Word8.word`, `Word16.word`, |
| 9 |
`Word32.word`, `Word64.word`. There is a special type, |
| 10 |
`MLton.Pointer.t`, for passing C pointers -- see <<MLtonPointer#>> for |
| 11 |
details. |
| 12 |
|
| 13 |
Arrays, refs, and vectors of the above types are also allowed. |
| 14 |
Because in MLton monomorphic arrays and vectors are exactly the same |
| 15 |
as their polymorphic counterpart, these are also allowed. Hence, |
| 16 |
`string`, `char vector`, and `CharVector.vector` are also allowed. |
| 17 |
Strings are not null terminated, unless you manually do so from the |
| 18 |
SML side. |
| 19 |
|
| 20 |
Unfortunately, passing tuples or datatypes is not allowed because that |
| 21 |
would interfere with representation optimizations. |
| 22 |
|
| 23 |
The C header file that `-export-header` generates includes |
| 24 |
``typedef``s for the C types corresponding to the SML types. Here is |
| 25 |
the mapping between SML types and C types. |
| 26 |
|
| 27 |
[options="header"] |
| 28 |
|=== |
| 29 |
| SML type | C typedef | C type | Note |
| 30 |
| `array` | `Pointer` | `unsigned char *` | |
| 31 |
| `bool` | `Bool` | `int32_t` | |
| 32 |
| `char` | `Char8` | `uint8_t` | |
| 33 |
| `Int8.int` | `Int8` | `int8_t` | |
| 34 |
| `Int16.int` | `Int16` | `int16_t` | |
| 35 |
| `Int32.int` | `Int32` | `int32_t` | |
| 36 |
| `Int64.int` | `Int64` | `int64_t` | |
| 37 |
| `int` | `Int32` | `int32_t` | <<#Default,(default)>> |
| 38 |
| `MLton.Pointer.t` | `Pointer` | `unsigned char *` | |
| 39 |
| `Real32.real` | `Real32` | `float` | |
| 40 |
| `Real64.real` | `Real64` | `double` | |
| 41 |
| `real` | `Real64` | `double` | <<#Default,(default)>> |
| 42 |
| `ref` | `Pointer` | `unsigned char *` | |
| 43 |
| `string` | `Pointer` | `unsigned char *` | <<#ReadOnly,(read only)>> |
| 44 |
| `vector` | `Pointer` | `unsigned char *` | <<#ReadOnly,(read only)>> |
| 45 |
| `Word8.word` | `Word8` | `uint8_t` | |
| 46 |
| `Word16.word` | `Word16` | `uint16_t` | |
| 47 |
| `Word32.word` | `Word32` | `uint32_t` | |
| 48 |
| `Word64.word` | `Word64` | `uint64_t` | |
| 49 |
| `word` | `Word32` | `uint32_t` | <<#Default,(default)>> |
| 50 |
|=== |
| 51 |
|
| 52 |
[[Default]]Note (default): The default `int`, `real`, and |
| 53 |
`word` types may be set by the ``-default-type __type__`` |
| 54 |
<<CompileTimeOptions#,compiler option>>. The given C typedef and C |
| 55 |
types correspond to the default behavior. |
| 56 |
|
| 57 |
[[ReadOnly]]Note (read only): Because MLton assumes that |
| 58 |
vectors and strings are read-only (and will perform optimizations |
| 59 |
that, for instance, cause them to share space), you must not modify |
| 60 |
the data pointed to by the `unsigned char *` in C code. |
| 61 |
|
| 62 |
Although the C type of an array, ref, or vector is always `Pointer`, |
| 63 |
in reality, the object has the natural C representation. Your C code |
| 64 |
should cast to the appropriate C type if you want to keep the C |
| 65 |
compiler from complaining. |
| 66 |
|
| 67 |
When calling an <<CallingFromSMLToC#,imported C function from SML>> |
| 68 |
that returns an array, ref, or vector result or when calling an |
| 69 |
<<CallingFromCToSML#,exported SML function from C>> that takes an |
| 70 |
array, ref, or string argument, then the object must be an ML object |
| 71 |
allocated on the ML heap. (Although an array, ref, or vector object |
| 72 |
has the natural C representation, the object also has an additional |
| 73 |
header used by the SML runtime system.) |
| 74 |
|
| 75 |
In addition, there is an <<MLBasis#>> file, `$(SML_LIB)/basis/c-types.mlb`, |
| 76 |
which provides structure aliases for various C types: |
| 77 |
|
| 78 |
|=== |
| 79 |
| C type | Structure | Signature |
| 80 |
| `char` | `C_Char` | `INTEGER` |
| 81 |
| `signed char` | `C_SChar` | `INTEGER` |
| 82 |
| `unsigned char` | `C_UChar` | `WORD` |
| 83 |
| `short` | `C_Short` | `INTEGER` |
| 84 |
| `signed short` | `C_SShort` | `INTEGER` |
| 85 |
| `unsigned short` | `C_UShort` | `WORD` |
| 86 |
| `int` | `C_Int` | `INTEGER` |
| 87 |
| `signed int` | `C_SInt` | `INTEGER` |
| 88 |
| `unsigned int` | `C_UInt` | `WORD` |
| 89 |
| `long` | `C_Long` | `INTEGER` |
| 90 |
| `signed long` | `C_SLong` | `INTEGER` |
| 91 |
| `unsigned long` | `C_ULong` | `WORD` |
| 92 |
| `long long` | `C_LongLong` | `INTEGER` |
| 93 |
| `signed long long` | `C_SLongLong` | `INTEGER` |
| 94 |
| `unsigned long long` | `C_ULongLong` | `WORD` |
| 95 |
| `float` | `C_Float` | `REAL` |
| 96 |
| `double` | `C_Double` | `REAL` |
| 97 |
| `size_t` | `C_Size` | `WORD` |
| 98 |
| `ptrdiff_t` | `C_Ptrdiff` | `INTEGER` |
| 99 |
| `intmax_t` | `C_Intmax` | `INTEGER` |
| 100 |
| `uintmax_t` | `C_UIntmax` | `WORD` |
| 101 |
| `intptr_t` | `C_Intptr` | `INTEGER` |
| 102 |
| `uintptr_t` | `C_UIntptr` | `WORD` |
| 103 |
| `void *` | `C_Pointer` | `WORD` |
| 104 |
|=== |
| 105 |
|
| 106 |
These aliases depend on the configuration of the C compiler for the |
| 107 |
target architecture, and are independent of the configuration of MLton |
| 108 |
(including the ``-default-type __type__`` |
| 109 |
<<CompileTimeOptions#,compiler option>>). |