]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types.factor
use a "pointer" wrapper tuple to indicate pointer types instead of the current slipsh...
[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     bool void*
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 integer }
34 { align integer }
35 { align-first integer } ;
36
37 TUPLE: c-type < abstract-c-type
38 boxer
39 unboxer
40 { rep initial: int-rep }
41 stack-align? ;
42
43 : <c-type> ( -- c-type )
44     \ c-type new ; inline
45
46 ERROR: no-c-type name ;
47
48 PREDICATE: c-type-word < word
49     "c-type" word-prop ;
50
51 ! C type protocol
52 GENERIC: c-type ( name -- c-type ) foldable
53
54 : void? ( c-type -- ? )
55     void = ; inline
56
57 TUPLE: pointer { to initial: void read-only } ;
58 C: <pointer> pointer
59
60 : resolve-typedef ( name -- c-type )
61     dup void? [ no-c-type ] when
62     dup c-type-word? [ c-type ] when ;
63
64 <PRIVATE
65
66 : parse-array-type ( name -- dims c-type )
67     "[" split unclip
68     [ [ "]" ?tail drop string>number ] map ] dip ;
69
70 PRIVATE>
71
72 M: word c-type
73     dup "c-type" word-prop resolve-typedef
74     [ ] [ no-c-type ] ?if ;
75
76 GENERIC: c-struct? ( c-type -- ? )
77
78 M: object c-struct? drop f ;
79
80 M: c-type-word c-struct? dup void? [ drop f ] [ c-type c-struct? ] if ;
81
82 ! These words being foldable means that words need to be
83 ! recompiled if a C type is redefined. Even so, folding the
84 ! size facilitates some optimizations.
85 GENERIC: c-type-class ( name -- class )
86
87 M: abstract-c-type c-type-class class>> ;
88
89 M: c-type-word c-type-class c-type c-type-class ;
90
91 GENERIC: c-type-boxed-class ( name -- class )
92
93 M: abstract-c-type c-type-boxed-class boxed-class>> ;
94
95 M: c-type-word c-type-boxed-class c-type c-type-boxed-class ;
96
97 GENERIC: c-type-boxer ( name -- boxer )
98
99 M: c-type c-type-boxer boxer>> ;
100
101 M: c-type-word c-type-boxer c-type c-type-boxer ;
102
103 GENERIC: c-type-boxer-quot ( name -- quot )
104
105 M: abstract-c-type c-type-boxer-quot boxer-quot>> ;
106
107 M: c-type-word c-type-boxer-quot c-type c-type-boxer-quot ;
108
109 GENERIC: c-type-unboxer ( name -- boxer )
110
111 M: c-type c-type-unboxer unboxer>> ;
112
113 M: c-type-word c-type-unboxer c-type c-type-unboxer ;
114
115 GENERIC: c-type-unboxer-quot ( name -- quot )
116
117 M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ;
118
119 M: c-type-word c-type-unboxer-quot c-type c-type-unboxer-quot ;
120
121 GENERIC: c-type-rep ( name -- rep )
122
123 M: c-type c-type-rep rep>> ;
124
125 M: c-type-word c-type-rep c-type c-type-rep ;
126
127 GENERIC: c-type-getter ( name -- quot )
128
129 M: c-type c-type-getter getter>> ;
130
131 M: c-type-word c-type-getter c-type c-type-getter ;
132
133 GENERIC: c-type-setter ( name -- quot )
134
135 M: c-type c-type-setter setter>> ;
136
137 M: c-type-word c-type-setter c-type c-type-setter ;
138
139 GENERIC: c-type-align ( name -- n )
140
141 M: abstract-c-type c-type-align align>> ;
142
143 M: c-type-word c-type-align c-type c-type-align ;
144
145 GENERIC: c-type-align-first ( name -- n )
146
147 M: c-type-word c-type-align-first c-type c-type-align-first ;
148
149 M: abstract-c-type c-type-align-first align-first>> ;
150
151 GENERIC: c-type-stack-align? ( name -- ? )
152
153 M: c-type c-type-stack-align? stack-align?>> ;
154
155 M: c-type-word c-type-stack-align? c-type c-type-stack-align? ;
156
157 : c-type-box ( n c-type -- )
158     [ c-type-rep ] [ c-type-boxer [ "No boxer" throw ] unless* ] bi
159     %box ;
160
161 : c-type-unbox ( n c-type -- )
162     [ c-type-rep ] [ c-type-unboxer [ "No unboxer" throw ] unless* ] bi
163     %unbox ;
164
165 GENERIC: box-parameter ( n c-type -- )
166
167 M: c-type box-parameter c-type-box ;
168
169 M: c-type-word box-parameter c-type box-parameter ;
170
171 GENERIC: box-return ( c-type -- )
172
173 M: c-type box-return f swap c-type-box ;
174
175 M: c-type-word box-return c-type box-return ;
176
177 GENERIC: unbox-parameter ( n c-type -- )
178
179 M: c-type unbox-parameter c-type-unbox ;
180
181 M: c-type-word unbox-parameter c-type unbox-parameter ;
182
183 GENERIC: unbox-return ( c-type -- )
184
185 M: c-type unbox-return f swap c-type-unbox ;
186
187 M: c-type-word unbox-return c-type unbox-return ;
188
189 : little-endian? ( -- ? ) 1 <int> *char 1 = ; foldable
190
191 GENERIC: heap-size ( name -- size )
192
193 M: c-type-word heap-size c-type heap-size ;
194
195 M: abstract-c-type heap-size size>> ;
196
197 GENERIC: stack-size ( name -- size )
198
199 M: c-type-word stack-size c-type stack-size ;
200
201 M: c-type stack-size size>> cell align ;
202
203 GENERIC: byte-length ( seq -- n ) flushable
204
205 M: byte-array byte-length length ; inline
206
207 M: f byte-length drop 0 ; inline
208
209 : >c-bool ( ? -- int ) 1 0 ? ; inline
210
211 : c-bool> ( int -- ? ) 0 = not ; inline
212
213 MIXIN: value-type
214
215 : c-getter ( name -- quot )
216     c-type-getter [
217         [ "Cannot read struct fields with this type" throw ]
218     ] unless* ;
219
220 : c-type-getter-boxer ( name -- quot )
221     [ c-getter ] [ c-type-boxer-quot ] bi append ;
222
223 : c-setter ( name -- quot )
224     c-type-setter [
225         [ "Cannot write struct fields with this type" throw ]
226     ] unless* ;
227
228 : array-accessor ( c-type quot -- def )
229     [
230         \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi*
231     ] [ ] make ;
232
233 GENERIC: typedef ( old new -- )
234
235 PREDICATE: typedef-word < c-type-word
236     "c-type" word-prop c-type-word? ;
237
238 M: word typedef ( old new -- )
239     {
240         [ nip define-symbol ]
241         [ swap "c-type" set-word-prop ]
242     } 2cleave ;
243
244 M: pointer typedef ( old new -- )
245     to>> dup c-type-word?
246     [ ]
247     [ 2drop ] if ;
248
249 TUPLE: long-long-type < c-type ;
250
251 : <long-long-type> ( -- c-type )
252     long-long-type new ;
253
254 M: long-long-type unbox-parameter ( n c-type -- )
255     c-type-unboxer %unbox-long-long ;
256
257 M: long-long-type unbox-return ( c-type -- )
258     f swap unbox-parameter ;
259
260 M: long-long-type box-parameter ( n c-type -- )
261     c-type-boxer %box-long-long ;
262
263 M: long-long-type box-return ( c-type -- )
264     f swap box-parameter ;
265
266 : define-deref ( c-type -- )
267     [ name>> CHAR: * prefix "alien.c-types" create ] [ c-getter 0 prefix ] bi
268     (( c-ptr -- value )) define-inline ;
269
270 : define-out ( c-type -- )
271     [ name>> "alien.c-types" constructor-word ]
272     [ dup c-setter '[ _ heap-size (byte-array) [ 0 @ ] keep ] ] bi
273     (( value -- c-ptr )) define-inline ;
274
275 : define-primitive-type ( c-type name -- )
276     [ typedef ] [ define-deref ] [ define-out ] tri ;
277
278 : if-void ( c-type true false -- )
279     pick void? [ drop nip call ] [ nip call ] if ; inline
280
281 CONSTANT: primitive-types
282     {
283         char uchar
284         short ushort
285         int uint
286         long ulong
287         longlong ulonglong
288         float double
289         void* bool
290     }
291
292 SYMBOLS:
293     ptrdiff_t intptr_t uintptr_t size_t
294     char* ;
295
296 <PRIVATE
297
298 : (pointer-c-type) ( void* type -- void*' )
299     [ clone ] dip c-type-boxer-quot >>boxer-quot ;
300
301 : string-pointer-type? ( type -- ? )
302     dup pointer? [ drop f ]
303     [ resolve-typedef { char uchar } member? ] if ;
304
305 : primitive-pointer-type? ( type -- ? )
306     dup pointer? [ drop t ] [
307         resolve-typedef [ void? ] [ primitive-types member? ] bi or
308     ] if ;
309
310 PRIVATE>
311
312 M: pointer c-type
313     [ \ void* c-type ] dip
314     to>> {
315         { [ dup string-pointer-type? ] [ drop \ char* c-type ] }
316         { [ dup primitive-pointer-type? ] [ drop ] }
317         [ (pointer-c-type) ]
318     } cond ;
319
320 : 8-byte-alignment ( c-type -- c-type )
321     {
322         { [ cpu ppc? os macosx? and ] [ 4 >>align 8 >>align-first ] }
323         { [ cpu x86.32? os windows? not and ] [ 4 >>align 4 >>align-first ] }
324         [ 8 >>align 8 >>align-first ]
325     } cond ;
326
327 [
328     <c-type>
329         c-ptr >>class
330         c-ptr >>boxed-class
331         [ alien-cell ] >>getter
332         [ [ >c-ptr ] 2dip set-alien-cell ] >>setter
333         bootstrap-cell >>size
334         bootstrap-cell >>align
335         bootstrap-cell >>align-first
336         [ >c-ptr ] >>unboxer-quot
337         "allot_alien" >>boxer
338         "alien_offset" >>unboxer
339     \ void* define-primitive-type
340
341     <c-type>
342         integer >>class
343         integer >>boxed-class
344         [ alien-signed-4 ] >>getter
345         [ set-alien-signed-4 ] >>setter
346         4 >>size
347         4 >>align
348         4 >>align-first
349         "from_signed_4" >>boxer
350         "to_fixnum" >>unboxer
351     \ int define-primitive-type
352
353     <c-type>
354         integer >>class
355         integer >>boxed-class
356         [ alien-unsigned-4 ] >>getter
357         [ set-alien-unsigned-4 ] >>setter
358         4 >>size
359         4 >>align
360         4 >>align-first
361         "from_unsigned_4" >>boxer
362         "to_cell" >>unboxer
363     \ uint define-primitive-type
364
365     <c-type>
366         fixnum >>class
367         fixnum >>boxed-class
368         [ alien-signed-2 ] >>getter
369         [ set-alien-signed-2 ] >>setter
370         2 >>size
371         2 >>align
372         2 >>align-first
373         "from_signed_2" >>boxer
374         "to_fixnum" >>unboxer
375     \ short define-primitive-type
376
377     <c-type>
378         fixnum >>class
379         fixnum >>boxed-class
380         [ alien-unsigned-2 ] >>getter
381         [ set-alien-unsigned-2 ] >>setter
382         2 >>size
383         2 >>align
384         2 >>align-first
385         "from_unsigned_2" >>boxer
386         "to_cell" >>unboxer
387     \ ushort define-primitive-type
388
389     <c-type>
390         fixnum >>class
391         fixnum >>boxed-class
392         [ alien-signed-1 ] >>getter
393         [ set-alien-signed-1 ] >>setter
394         1 >>size
395         1 >>align
396         1 >>align-first
397         "from_signed_1" >>boxer
398         "to_fixnum" >>unboxer
399     \ char define-primitive-type
400
401     <c-type>
402         fixnum >>class
403         fixnum >>boxed-class
404         [ alien-unsigned-1 ] >>getter
405         [ set-alien-unsigned-1 ] >>setter
406         1 >>size
407         1 >>align
408         1 >>align-first
409         "from_unsigned_1" >>boxer
410         "to_cell" >>unboxer
411     \ uchar define-primitive-type
412
413     cpu ppc? [
414         <c-type>
415             [ alien-unsigned-4 c-bool> ] >>getter
416             [ [ >c-bool ] 2dip set-alien-unsigned-4 ] >>setter
417             4 >>size
418             4 >>align
419             4 >>align-first
420             "from_boolean" >>boxer
421             "to_boolean" >>unboxer
422     ] [
423         <c-type>
424             [ alien-unsigned-1 c-bool> ] >>getter
425             [ [ >c-bool ] 2dip set-alien-unsigned-1 ] >>setter
426             1 >>size
427             1 >>align
428             1 >>align-first
429             "from_boolean" >>boxer
430             "to_boolean" >>unboxer
431     ] if
432     \ bool define-primitive-type
433
434     <c-type>
435         math:float >>class
436         math:float >>boxed-class
437         [ alien-float ] >>getter
438         [ [ >float ] 2dip set-alien-float ] >>setter
439         4 >>size
440         4 >>align
441         4 >>align-first
442         "from_float" >>boxer
443         "to_float" >>unboxer
444         float-rep >>rep
445         [ >float ] >>unboxer-quot
446     \ float define-primitive-type
447
448     <c-type>
449         math:float >>class
450         math:float >>boxed-class
451         [ alien-double ] >>getter
452         [ [ >float ] 2dip set-alien-double ] >>setter
453         8 >>size
454         8-byte-alignment
455         "from_double" >>boxer
456         "to_double" >>unboxer
457         double-rep >>rep
458         [ >float ] >>unboxer-quot
459     \ double define-primitive-type
460
461     cell 8 = [
462         <c-type>
463             integer >>class
464             integer >>boxed-class
465             [ alien-signed-cell ] >>getter
466             [ set-alien-signed-cell ] >>setter
467             bootstrap-cell >>size
468             bootstrap-cell >>align
469             bootstrap-cell >>align-first
470             "from_signed_cell" >>boxer
471             "to_fixnum" >>unboxer
472         \ longlong define-primitive-type
473
474         <c-type>
475             integer >>class
476             integer >>boxed-class
477             [ alien-unsigned-cell ] >>getter
478             [ set-alien-unsigned-cell ] >>setter
479             bootstrap-cell >>size
480             bootstrap-cell >>align
481             bootstrap-cell >>align-first
482             "from_unsigned_cell" >>boxer
483             "to_cell" >>unboxer
484         \ ulonglong define-primitive-type
485
486         os windows? [
487             \ int c-type \ long define-primitive-type
488             \ uint c-type \ ulong define-primitive-type
489         ] [
490             \ longlong c-type \ long define-primitive-type
491             \ ulonglong c-type \ ulong define-primitive-type
492         ] if
493
494         \ longlong c-type \ ptrdiff_t typedef
495         \ longlong c-type \ intptr_t typedef
496
497         \ ulonglong c-type \ uintptr_t typedef
498         \ ulonglong c-type \ size_t typedef
499     ] [
500         <long-long-type>
501             integer >>class
502             integer >>boxed-class
503             [ alien-signed-8 ] >>getter
504             [ set-alien-signed-8 ] >>setter
505             8 >>size
506             8-byte-alignment
507             "from_signed_8" >>boxer
508             "to_signed_8" >>unboxer
509         \ longlong define-primitive-type
510
511         <long-long-type>
512             integer >>class
513             integer >>boxed-class
514             [ alien-unsigned-8 ] >>getter
515             [ set-alien-unsigned-8 ] >>setter
516             8 >>size
517             8-byte-alignment
518             "from_unsigned_8" >>boxer
519             "to_unsigned_8" >>unboxer
520         \ ulonglong define-primitive-type
521
522         \ int c-type \ long define-primitive-type
523         \ uint c-type \ ulong define-primitive-type
524
525         \ int c-type \ ptrdiff_t typedef
526         \ int c-type \ intptr_t typedef
527
528         \ uint c-type \ uintptr_t typedef
529         \ uint c-type \ size_t typedef
530     ] if
531 ] with-compilation-unit
532
533 M: char-16-rep rep-component-type drop char ;
534 M: uchar-16-rep rep-component-type drop uchar ;
535 M: short-8-rep rep-component-type drop short ;
536 M: ushort-8-rep rep-component-type drop ushort ;
537 M: int-4-rep rep-component-type drop int ;
538 M: uint-4-rep rep-component-type drop uint ;
539 M: longlong-2-rep rep-component-type drop longlong ;
540 M: ulonglong-2-rep rep-component-type drop ulonglong ;
541 M: float-4-rep rep-component-type drop float ;
542 M: double-2-rep rep-component-type drop double ;
543
544 : (unsigned-interval) ( bytes -- from to ) [ 0 ] dip 8 * 2^ 1 - ; foldable
545 : unsigned-interval ( c-type -- from to ) heap-size (unsigned-interval) ; foldable
546 : (signed-interval) ( bytes -- from to ) 8 * 1 - 2^ [ neg ] [ 1 - ] bi ; foldable
547 : signed-interval ( c-type -- from to ) heap-size (signed-interval) ; foldable
548
549 : c-type-interval ( c-type -- from to )
550     {
551         { [ dup { float double } member-eq? ] [ drop -1/0. 1/0. ] }
552         { [ dup { char short int long longlong } member-eq? ] [ signed-interval ] }
553         { [ dup { uchar ushort uint ulong ulonglong } member-eq? ] [ unsigned-interval ] }
554     } cond ; foldable
555
556 : c-type-clamp ( value c-type -- value' )
557     dup { float double } member-eq?
558     [ drop ] [ c-type-interval clamp ] if ; inline