]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types.factor
alien: improve documentation
[factor.git] / basis / alien / c-types / c-types.factor
1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: byte-arrays arrays assocs kernel kernel.private math
4 math.order math.parser namespaces make parser sequences strings
5 words splitting cpu.architecture alien alien.accessors
6 alien.strings quotations layouts system compiler.units io
7 io.files io.encodings.binary io.streams.memory accessors
8 combinators effects continuations fry classes vocabs
9 vocabs.loader words.symbol ;
10 QUALIFIED: math
11 IN: alien.c-types
12
13 SYMBOLS:
14     char uchar
15     short ushort
16     int uint
17     long ulong
18     longlong ulonglong
19     float double
20     void* bool
21     void ;
22
23 DEFER: <int>
24 DEFER: *char
25
26 TUPLE: abstract-c-type
27 { class class initial: object }
28 { boxed-class class initial: object }
29 { boxer-quot callable }
30 { unboxer-quot callable }
31 { getter callable }
32 { setter callable }
33 size
34 align ;
35
36 TUPLE: c-type < abstract-c-type
37 boxer
38 unboxer
39 { rep initial: int-rep }
40 stack-align? ;
41
42 : <c-type> ( -- c-type )
43     \ c-type new ; inline
44
45 SYMBOL: c-types
46
47 global [
48     c-types [ H{ } assoc-like ] change
49 ] bind
50
51 ERROR: no-c-type name ;
52
53 PREDICATE: c-type-word < word
54     "c-type" word-prop ;
55
56 UNION: c-type-name string word ;
57
58 ! C type protocol
59 GENERIC: c-type ( name -- c-type ) foldable
60
61 GENERIC: resolve-pointer-type ( name -- c-type )
62
63 M: word resolve-pointer-type
64     dup "pointer-c-type" word-prop
65     [ ] [ drop void* ] ?if ;
66
67 M: string resolve-pointer-type
68     dup "*" append dup c-types get at
69     [ nip ] [
70         drop
71         c-types get at dup c-type-name?
72         [ resolve-pointer-type ] [ drop void* ] if
73     ] if ;
74
75 : resolve-typedef ( name -- c-type )
76     dup c-type-name? [ c-type ] when ;
77
78 : parse-array-type ( name -- dims c-type )
79     "[" split unclip
80     [ [ "]" ?tail drop string>number ] map ] dip ;
81
82 M: string c-type ( name -- c-type )
83     CHAR: ] over member? [
84         parse-array-type prefix
85     ] [
86         dup c-types get at [ ] [
87             "*" ?tail [ resolve-pointer-type ] [ no-c-type ] if
88         ] ?if resolve-typedef
89     ] if ;
90
91 M: word c-type
92     "c-type" word-prop resolve-typedef ;
93
94 : void? ( c-type -- ? )
95     { void "void" } member? ;
96
97 GENERIC: c-struct? ( c-type -- ? )
98
99 M: object c-struct?
100     drop f ;
101 M: c-type-name c-struct?
102     dup void? [ drop f ] [ c-type c-struct? ] if ;
103
104 ! These words being foldable means that words need to be
105 ! recompiled if a C type is redefined. Even so, folding the
106 ! size facilitates some optimizations.
107 GENERIC: c-type-class ( name -- class )
108
109 M: abstract-c-type c-type-class class>> ;
110
111 M: c-type-name c-type-class c-type c-type-class ;
112
113 GENERIC: c-type-boxed-class ( name -- class )
114
115 M: abstract-c-type c-type-boxed-class boxed-class>> ;
116
117 M: c-type-name c-type-boxed-class c-type c-type-boxed-class ;
118
119 GENERIC: c-type-boxer ( name -- boxer )
120
121 M: c-type c-type-boxer boxer>> ;
122
123 M: c-type-name c-type-boxer c-type c-type-boxer ;
124
125 GENERIC: c-type-boxer-quot ( name -- quot )
126
127 M: abstract-c-type c-type-boxer-quot boxer-quot>> ;
128
129 M: c-type-name c-type-boxer-quot c-type c-type-boxer-quot ;
130
131 GENERIC: c-type-unboxer ( name -- boxer )
132
133 M: c-type c-type-unboxer unboxer>> ;
134
135 M: c-type-name c-type-unboxer c-type c-type-unboxer ;
136
137 GENERIC: c-type-unboxer-quot ( name -- quot )
138
139 M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ;
140
141 M: c-type-name c-type-unboxer-quot c-type c-type-unboxer-quot ;
142
143 GENERIC: c-type-rep ( name -- rep )
144
145 M: c-type c-type-rep rep>> ;
146
147 M: c-type-name c-type-rep c-type c-type-rep ;
148
149 GENERIC: c-type-getter ( name -- quot )
150
151 M: c-type c-type-getter getter>> ;
152
153 M: c-type-name c-type-getter c-type c-type-getter ;
154
155 GENERIC: c-type-setter ( name -- quot )
156
157 M: c-type c-type-setter setter>> ;
158
159 M: c-type-name c-type-setter c-type c-type-setter ;
160
161 GENERIC: c-type-align ( name -- n )
162
163 M: abstract-c-type c-type-align align>> ;
164
165 M: c-type-name c-type-align c-type c-type-align ;
166
167 GENERIC: c-type-stack-align? ( name -- ? )
168
169 M: c-type c-type-stack-align? stack-align?>> ;
170
171 M: c-type-name c-type-stack-align? c-type c-type-stack-align? ;
172
173 : c-type-box ( n c-type -- )
174     [ c-type-rep ] [ c-type-boxer [ "No boxer" throw ] unless* ] bi
175     %box ;
176
177 : c-type-unbox ( n c-type -- )
178     [ c-type-rep ] [ c-type-unboxer [ "No unboxer" throw ] unless* ] bi
179     %unbox ;
180
181 GENERIC: box-parameter ( n c-type -- )
182
183 M: c-type box-parameter c-type-box ;
184
185 M: c-type-name box-parameter c-type box-parameter ;
186
187 GENERIC: box-return ( c-type -- )
188
189 M: c-type box-return f swap c-type-box ;
190
191 M: c-type-name box-return c-type box-return ;
192
193 GENERIC: unbox-parameter ( n c-type -- )
194
195 M: c-type unbox-parameter c-type-unbox ;
196
197 M: c-type-name unbox-parameter c-type unbox-parameter ;
198
199 GENERIC: unbox-return ( c-type -- )
200
201 M: c-type unbox-return f swap c-type-unbox ;
202
203 M: c-type-name unbox-return c-type unbox-return ;
204
205 : little-endian? ( -- ? ) 1 <int> *char 1 = ; foldable
206
207 GENERIC: heap-size ( name -- size ) foldable
208
209 M: c-type-name heap-size c-type heap-size ;
210
211 M: abstract-c-type heap-size size>> ;
212
213 GENERIC: stack-size ( name -- size ) foldable
214
215 M: c-type-name stack-size c-type stack-size ;
216
217 M: c-type stack-size size>> cell align ;
218
219 GENERIC: byte-length ( seq -- n ) flushable
220
221 M: byte-array byte-length length ; inline
222
223 M: f byte-length drop 0 ; inline
224
225 MIXIN: value-type
226
227 : c-getter ( name -- quot )
228     c-type-getter [
229         [ "Cannot read struct fields with this type" throw ]
230     ] unless* ;
231
232 : c-type-getter-boxer ( name -- quot )
233     [ c-getter ] [ c-type-boxer-quot ] bi append ;
234
235 : c-setter ( name -- quot )
236     c-type-setter [
237         [ "Cannot write struct fields with this type" throw ]
238     ] unless* ;
239
240 : array-accessor ( c-type quot -- def )
241     [
242         \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi*
243     ] [ ] make ;
244
245 GENERIC: typedef ( old new -- )
246
247 PREDICATE: typedef-word < c-type-word
248     "c-type" word-prop c-type-name? ;
249
250 M: string typedef ( old new -- ) c-types get set-at ;
251 M: word typedef ( old new -- )
252     {
253         [ nip define-symbol ]
254         [ name>> typedef ]
255         [ swap "c-type" set-word-prop ]
256         [
257             swap dup c-type-name? [
258                 resolve-pointer-type
259                 "pointer-c-type" set-word-prop
260             ] [ 2drop ] if
261         ]
262     } 2cleave ;
263
264 TUPLE: long-long-type < c-type ;
265
266 : <long-long-type> ( -- c-type )
267     long-long-type new ;
268
269 M: long-long-type unbox-parameter ( n c-type -- )
270     c-type-unboxer %unbox-long-long ;
271
272 M: long-long-type unbox-return ( c-type -- )
273     f swap unbox-parameter ;
274
275 M: long-long-type box-parameter ( n c-type -- )
276     c-type-boxer %box-long-long ;
277
278 M: long-long-type box-return ( c-type -- )
279     f swap box-parameter ;
280
281 : define-deref ( name -- )
282     [ CHAR: * prefix "alien.c-types" create ] [ c-getter 0 prefix ] bi
283     (( c-ptr -- value )) define-inline ;
284
285 : define-out ( name -- )
286     [ "alien.c-types" constructor-word ]
287     [ dup c-setter '[ _ heap-size <byte-array> [ 0 @ ] keep ] ] bi
288     (( value -- c-ptr )) define-inline ;
289
290 : define-primitive-type ( c-type name -- )
291     [ typedef ]
292     [ name>> define-deref ]
293     [ name>> define-out ]
294     tri ;
295
296 : if-void ( c-type true false -- )
297     pick void? [ drop nip call ] [ nip call ] if ; inline
298
299 CONSTANT: primitive-types
300     {
301         char uchar
302         short ushort
303         int uint
304         long ulong
305         longlong ulonglong
306         float double
307         void* bool
308     }
309
310 SYMBOLS:
311     ptrdiff_t intptr_t size_t
312     char* uchar* ;
313
314 [
315     <c-type>
316         c-ptr >>class
317         c-ptr >>boxed-class
318         [ alien-cell ] >>getter
319         [ [ >c-ptr ] 2dip set-alien-cell ] >>setter
320         bootstrap-cell >>size
321         bootstrap-cell >>align
322         [ >c-ptr ] >>unboxer-quot
323         "box_alien" >>boxer
324         "alien_offset" >>unboxer
325     \ void* define-primitive-type
326
327     <long-long-type>
328         integer >>class
329         integer >>boxed-class
330         [ alien-signed-8 ] >>getter
331         [ set-alien-signed-8 ] >>setter
332         8 >>size
333         8 >>align
334         "box_signed_8" >>boxer
335         "to_signed_8" >>unboxer
336     \ longlong define-primitive-type
337
338     <long-long-type>
339         integer >>class
340         integer >>boxed-class
341         [ alien-unsigned-8 ] >>getter
342         [ set-alien-unsigned-8 ] >>setter
343         8 >>size
344         8 >>align
345         "box_unsigned_8" >>boxer
346         "to_unsigned_8" >>unboxer
347     \ ulonglong define-primitive-type
348
349     <c-type>
350         integer >>class
351         integer >>boxed-class
352         [ alien-signed-cell ] >>getter
353         [ set-alien-signed-cell ] >>setter
354         bootstrap-cell >>size
355         bootstrap-cell >>align
356         "box_signed_cell" >>boxer
357         "to_fixnum" >>unboxer
358     \ long define-primitive-type
359
360     <c-type>
361         integer >>class
362         integer >>boxed-class
363         [ alien-unsigned-cell ] >>getter
364         [ set-alien-unsigned-cell ] >>setter
365         bootstrap-cell >>size
366         bootstrap-cell >>align
367         "box_unsigned_cell" >>boxer
368         "to_cell" >>unboxer
369     \ ulong define-primitive-type
370
371     <c-type>
372         integer >>class
373         integer >>boxed-class
374         [ alien-signed-4 ] >>getter
375         [ set-alien-signed-4 ] >>setter
376         4 >>size
377         4 >>align
378         "box_signed_4" >>boxer
379         "to_fixnum" >>unboxer
380     \ int define-primitive-type
381
382     <c-type>
383         integer >>class
384         integer >>boxed-class
385         [ alien-unsigned-4 ] >>getter
386         [ set-alien-unsigned-4 ] >>setter
387         4 >>size
388         4 >>align
389         "box_unsigned_4" >>boxer
390         "to_cell" >>unboxer
391     \ uint define-primitive-type
392
393     <c-type>
394         fixnum >>class
395         fixnum >>boxed-class
396         [ alien-signed-2 ] >>getter
397         [ set-alien-signed-2 ] >>setter
398         2 >>size
399         2 >>align
400         "box_signed_2" >>boxer
401         "to_fixnum" >>unboxer
402     \ short define-primitive-type
403
404     <c-type>
405         fixnum >>class
406         fixnum >>boxed-class
407         [ alien-unsigned-2 ] >>getter
408         [ set-alien-unsigned-2 ] >>setter
409         2 >>size
410         2 >>align
411         "box_unsigned_2" >>boxer
412         "to_cell" >>unboxer
413     \ ushort define-primitive-type
414
415     <c-type>
416         fixnum >>class
417         fixnum >>boxed-class
418         [ alien-signed-1 ] >>getter
419         [ set-alien-signed-1 ] >>setter
420         1 >>size
421         1 >>align
422         "box_signed_1" >>boxer
423         "to_fixnum" >>unboxer
424     \ char define-primitive-type
425
426     <c-type>
427         fixnum >>class
428         fixnum >>boxed-class
429         [ alien-unsigned-1 ] >>getter
430         [ set-alien-unsigned-1 ] >>setter
431         1 >>size
432         1 >>align
433         "box_unsigned_1" >>boxer
434         "to_cell" >>unboxer
435     \ uchar define-primitive-type
436
437     <c-type>
438         [ alien-unsigned-1 0 = not ] >>getter
439         [ [ 1 0 ? ] 2dip set-alien-unsigned-1 ] >>setter
440         1 >>size
441         1 >>align
442         "box_boolean" >>boxer
443         "to_boolean" >>unboxer
444     \ bool define-primitive-type
445
446     <c-type>
447         math:float >>class
448         math:float >>boxed-class
449         [ alien-float ] >>getter
450         [ [ >float ] 2dip set-alien-float ] >>setter
451         4 >>size
452         4 >>align
453         "box_float" >>boxer
454         "to_float" >>unboxer
455         float-rep >>rep
456         [ >float ] >>unboxer-quot
457     \ float define-primitive-type
458
459     <c-type>
460         math:float >>class
461         math:float >>boxed-class
462         [ alien-double ] >>getter
463         [ [ >float ] 2dip set-alien-double ] >>setter
464         8 >>size
465         8 >>align
466         "box_double" >>boxer
467         "to_double" >>unboxer
468         double-rep >>rep
469         [ >float ] >>unboxer-quot
470     \ double define-primitive-type
471
472     \ long \ ptrdiff_t typedef
473     \ long \ intptr_t typedef
474     \ ulong \ size_t typedef
475 ] with-compilation-unit
476
477 M: char-16-rep rep-component-type drop char ;
478 M: uchar-16-rep rep-component-type drop uchar ;
479 M: short-8-rep rep-component-type drop short ;
480 M: ushort-8-rep rep-component-type drop ushort ;
481 M: int-4-rep rep-component-type drop int ;
482 M: uint-4-rep rep-component-type drop uint ;
483 M: longlong-2-rep rep-component-type drop longlong ;
484 M: ulonglong-2-rep rep-component-type drop ulonglong ;
485 M: float-4-rep rep-component-type drop float ;
486 M: double-2-rep rep-component-type drop double ;
487
488 : (unsigned-interval) ( bytes -- from to ) [ 0 ] dip 8 * 2^ 1 - ; foldable
489 : unsigned-interval ( c-type -- from to ) heap-size (unsigned-interval) ; foldable
490 : (signed-interval) ( bytes -- from to ) 8 * 1 - 2^ [ neg ] [ 1 - ] bi ; foldable
491 : signed-interval ( c-type -- from to ) heap-size (signed-interval) ; foldable
492
493 : c-type-interval ( c-type -- from to )
494     {
495         { [ dup { float double } memq? ] [ drop -1/0. 1/0. ] }
496         { [ dup { char short int long longlong } memq? ] [ signed-interval ] }
497         { [ dup { uchar ushort uint ulong ulonglong } memq? ] [ unsigned-interval ] }
498     } cond ; foldable
499
500 : c-type-clamp ( value c-type -- value' ) c-type-interval clamp ; inline