File: ForeignFunctionInterfaceTypes

package info (click to toggle)
mlton 20100608-5.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 36,628 kB
  • ctags: 70,047
  • sloc: ansic: 18,441; lisp: 2,879; makefile: 1,572; sh: 1,326; pascal: 256; asm: 97
file content (301 lines) | stat: -rw-r--r-- 7,003 bytes parent folder | download | duplicates (3)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>ForeignFunctionInterfaceTypes - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton MLTONWIKIVERSION</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      ForeignFunctionInterfaceTypes
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "TitleIndex">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
MLton's <a href="ForeignFunctionInterface">ForeignFunctionInterface</a> only allows values of certain SML types to be passed between SML and C.  The following types are allowed: <tt>bool</tt>, <tt>char</tt>, <tt>int</tt>, <tt>real</tt>, <tt>word</tt>. All of the different sizes of (fixed-sized) integers, reals, and words are supported as well: <tt>Int8.int</tt>, <tt>Int16.int</tt>, <tt>Int32.int</tt>, <tt>Int64.int</tt>, <tt>Real32.real</tt>, <tt>Real64.real</tt>, <tt>Word8.word</tt>, <tt>Word16.word</tt>, <tt>Word32.word</tt>, <tt>Word64.word</tt>.  There is a special type, <tt>MLton.Pointer.t</tt>, for passing C pointers -- see <a href="MLtonPointer">MLtonPointer</a> for details. <p>
Arrays, refs, and vectors of the above types are also allowed. Because in MLton monomorphic arrays and vectors are exactly the same as their polymorphic counterpart, these are also allowed.  Hence, <tt>string</tt>, <tt>char&nbsp;vector</tt>, and <tt>CharVector.vector</tt> are also allowed.  Strings are not null terminated, unless you manually do so from the SML side. 
</p>
<p>
Unfortunately, passing tuples or datatypes is not allowed because that would interfere with representation optimizations. 
</p>
<p>
The C header file that <tt>-export-header</tt> generates includes <tt>typedef</tt>s for the C types corresponding to the SML types.  Here is the mapping between SML types and C types. 
</p>

<div>
<table>
<tr>
<td>
 SML type </td>
<td>
 C typedef </td>
<td>
 C type </td>
<td>
 Note </td>
</tr>
<tr>
<td>
 <tt>array</tt> </td>
<td>
 <tt>Pointer</tt> </td>
<td>
 <tt>unsigned&nbsp;char&nbsp;*</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>bool</tt> </td>
<td>
 <tt>Bool</tt> </td>
<td>
 <tt>int32_t</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>char</tt> </td>
<td>
 <tt>Char8</tt> </td>
<td>
 <tt>uint8_t</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>Int8.int</tt> </td>
<td>
 <tt>Int8</tt> </td>
<td>
 <tt>int8_t</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>Int16.int</tt> </td>
<td>
 <tt>Int16</tt> </td>
<td>
 <tt>int16_t</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>Int32.int</tt> </td>
<td>
 <tt>Int32</tt> </td>
<td>
 <tt>int32_t</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>Int64.int</tt> </td>
<td>
 <tt>Int64</tt> </td>
<td>
 <tt>int64_t</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>int</tt> </td>
<td>
 <tt>Int32</tt> </td>
<td>
 <tt>int32_t</tt> </td>
<td>
 <a href="#Default">(default)</a> </td>
</tr>
<tr>
<td>
 <tt>MLton.Pointer.t</tt> </td>
<td>
 <tt>Pointer</tt> </td>
<td>
 <tt>unsigned&nbsp;char&nbsp;*</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>Real32.real</tt> </td>
<td>
 <tt>Real32</tt> </td>
<td>
 <tt>float</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>Real64.real</tt> </td>
<td>
 <tt>Real64</tt> </td>
<td>
 <tt>double</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>real</tt> </td>
<td>
 <tt>Real64</tt> </td>
<td>
 <tt>double</tt> </td>
<td>
 <a href="#Default">(default)</a> </td>
</tr>
<tr>
<td>
 <tt>ref</tt> </td>
<td>
 <tt>Pointer</tt> </td>
<td>
 <tt>unsigned&nbsp;char&nbsp;*</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>string</tt> </td>
<td>
 <tt>Pointer</tt> </td>
<td>
 <tt>unsigned&nbsp;char&nbsp;*</tt> </td>
<td>
 <a href="#ReadOnly">(read only)</a> </td>
</tr>
<tr>
<td>
 <tt>vector</tt> </td>
<td>
 <tt>Pointer</tt> </td>
<td>
 <tt>unsigned&nbsp;char&nbsp;*</tt> </td>
<td>
 <a href="#ReadOnly">(read only)</a> </td>
</tr>
<tr>
<td>
 <tt>Word8.word</tt> </td>
<td>
 <tt>Word8</tt> </td>
<td>
 <tt>uint8_t</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>Word16.word</tt> </td>
<td>
 <tt>Word16</tt> </td>
<td>
 <tt>uint16_t</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>Word32.word</tt> </td>
<td>
 <tt>Word32</tt> </td>
<td>
 <tt>uint32_t</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>Word64.word</tt> </td>
<td>
 <tt>Word64</tt> </td>
<td>
 <tt>uint64_t</tt> </td>
<td>
 </td>
</tr>
<tr>
<td>
 <tt>word</tt> </td>
<td>
 <tt>Word32</tt> </td>
<td>
 <tt>uint32_t</tt> </td>
<td>
 <a href="#Default">(default)</a> </td>
</tr>
</table>
</div>
<p>
<a id="Default"></a>Note (default): The default <tt>int</tt>, <tt>real</tt>, and <tt>word</tt> types may be set by the <tt>-default-type&nbsp;</tt><em>type</em> <a href="CompileTimeOptions"> compiler option</a>.  The given C typedef and C types correspond to the default behavior. 
</p>
<p>
<a id="ReadOnly"></a>Note (read only):  Because MLton assumes that vectors and strings are read-only (and will perform optimizations that, for instance, cause them to share space), you must not modify the data pointed to by the <tt>unsigned&nbsp;char&nbsp;*</tt> in C code. 
</p>
<p>
Although the C type of an array, ref, or vector is always <tt>Pointer</tt>, in reality, the object has the natural C representation.  Your C code should cast to the appropriate C type if you want to keep the C compiler from complaining. 
</p>
<p>
When calling an <a href="CallingFromSMLToC"> imported C function from SML</a> that returns an array, ref, or vector result or when calling an <a href="CallingFromCToSML"> exported SML function from C</a> that takes an array, ref, or string argument, then the object must be an ML object allocated on the ML heap.  (Although an array, ref, or vector object has the natural C representation, the object also has an additional header used by the SML runtime system.) 
</p>
</div>



<p>
<hr>
Last edited on 2007-08-16 01:14:48 by <span title="c-71-57-91-146.hsd1.il.comcast.net"><a href="MatthewFluet">MatthewFluet</a></span>.
</body></html>