]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/c-types/c-types.factor
alien: some code cleanups and fixes
[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
22 SINGLETON: void
23
24 DEFER: <int>
25 DEFER: *char
26
27 TUPLE: abstract-c-type
28 { class class initial: object }
29 { boxed-class class initial: object }
30 { boxer-quot callable }
31 { unboxer-quot callable }
32 { getter callable }
33 { setter callable }
34 { size integer }
35 { align integer }
36 { align-first integer } ;
37
38 TUPLE: c-type < abstract-c-type
39 boxer
40 unboxer
41 { rep initial: int-rep }
42 stack-align? ;
43
44 : <c-type> ( -- c-type )
45     \ c-type new ; inline
46
47 ERROR: no-c-type name ;
48
49 PREDICATE: c-type-word < word
50     "c-type" word-prop ;
51
52 UNION: c-type-name string c-type-word ;
53
54 ! C type protocol
55 GENERIC: c-type ( name -- c-type ) foldable
56
57 GENERIC: resolve-pointer-type ( name -- c-type )
58
59 << \ void \ void* "pointer-c-type" set-word-prop >>
60
61 M: word resolve-pointer-type
62     dup "pointer-c-type" word-prop
63     [ ] [ drop void* ] ?if ;
64
65 M: array resolve-pointer-type
66     first resolve-pointer-type ;
67
68 : resolve-typedef ( name -- c-type )
69     dup void? [ no-c-type ] when
70     dup c-type-name? [ c-type ] when ;
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-name 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-name 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-name 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-name 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-name 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-name 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-name 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-name 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-name 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-name 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-name c-type-align c-type c-type-align ;
144
145 GENERIC: c-type-align-first ( name -- n )
146
147 M: c-type-name 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-name 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-name 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-name 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-name 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-name 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-name 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-name 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-name? ;
237
238 M: word typedef ( old new -- )
239     {
240         [ nip define-symbol ]
241         [ swap "c-type" set-word-prop ]
242         [
243             swap dup c-type-name? [
244                 resolve-pointer-type
245                 "pointer-c-type" set-word-prop
246             ] [ 2drop ] if
247         ]
248     } 2cleave ;
249
250 TUPLE: long-long-type < c-type ;
251
252 : <long-long-type> ( -- c-type )
253     long-long-type new ;
254
255 M: long-long-type unbox-parameter ( n c-type -- )
256     c-type-unboxer %unbox-long-long ;
257
258 M: long-long-type unbox-return ( c-type -- )
259     f swap unbox-parameter ;
260
261 M: long-long-type box-parameter ( n c-type -- )
262     c-type-boxer %box-long-long ;
263
264 M: long-long-type box-return ( c-type -- )
265     f swap box-parameter ;
266
267 : define-deref ( c-type -- )
268     [ name>> CHAR: * prefix "alien.c-types" create ] [ c-getter 0 prefix ] bi
269     (( c-ptr -- value )) define-inline ;
270
271 : define-out ( c-type -- )
272     [ name>> "alien.c-types" constructor-word ]
273     [ dup c-setter '[ _ heap-size (byte-array) [ 0 @ ] keep ] ] bi
274     (( value -- c-ptr )) define-inline ;
275
276 : define-primitive-type ( c-type name -- )
277     [ typedef ] [ define-deref ] [ define-out ] tri ;
278
279 : if-void ( c-type true false -- )
280     pick void? [ drop nip call ] [ nip call ] if ; inline
281
282 CONSTANT: primitive-types
283     {
284         char uchar
285         short ushort
286         int uint
287         long ulong
288         longlong ulonglong
289         float double
290         void* bool
291     }
292
293 SYMBOLS:
294     ptrdiff_t intptr_t uintptr_t size_t
295     char* uchar* ;
296
297 : 8-byte-alignment ( c-type -- c-type )
298     {
299         { [ cpu ppc? os macosx? and ] [ 4 >>align 8 >>align-first ] }
300         { [ cpu x86.32? os windows? not and ] [ 4 >>align 4 >>align-first ] }
301         [ 8 >>align 8 >>align-first ]
302     } cond ;
303
304 [
305     <c-type>
306         c-ptr >>class
307         c-ptr >>boxed-class
308         [ alien-cell ] >>getter
309         [ [ >c-ptr ] 2dip set-alien-cell ] >>setter
310         bootstrap-cell >>size
311         bootstrap-cell >>align
312         bootstrap-cell >>align-first
313         [ >c-ptr ] >>unboxer-quot
314         "allot_alien" >>boxer
315         "alien_offset" >>unboxer
316     \ void* define-primitive-type
317
318     <c-type>
319         integer >>class
320         integer >>boxed-class
321         [ alien-signed-4 ] >>getter
322         [ set-alien-signed-4 ] >>setter
323         4 >>size
324         4 >>align
325         4 >>align-first
326         "from_signed_4" >>boxer
327         "to_fixnum" >>unboxer
328     \ int define-primitive-type
329
330     <c-type>
331         integer >>class
332         integer >>boxed-class
333         [ alien-unsigned-4 ] >>getter
334         [ set-alien-unsigned-4 ] >>setter
335         4 >>size
336         4 >>align
337         4 >>align-first
338         "from_unsigned_4" >>boxer
339         "to_cell" >>unboxer
340     \ uint define-primitive-type
341
342     <c-type>
343         fixnum >>class
344         fixnum >>boxed-class
345         [ alien-signed-2 ] >>getter
346         [ set-alien-signed-2 ] >>setter
347         2 >>size
348         2 >>align
349         2 >>align-first
350         "from_signed_2" >>boxer
351         "to_fixnum" >>unboxer
352     \ short define-primitive-type
353
354     <c-type>
355         fixnum >>class
356         fixnum >>boxed-class
357         [ alien-unsigned-2 ] >>getter
358         [ set-alien-unsigned-2 ] >>setter
359         2 >>size
360         2 >>align
361         2 >>align-first
362         "from_unsigned_2" >>boxer
363         "to_cell" >>unboxer
364     \ ushort define-primitive-type
365
366     <c-type>
367         fixnum >>class
368         fixnum >>boxed-class
369         [ alien-signed-1 ] >>getter
370         [ set-alien-signed-1 ] >>setter
371         1 >>size
372         1 >>align
373         1 >>align-first
374         "from_signed_1" >>boxer
375         "to_fixnum" >>unboxer
376     \ char define-primitive-type
377
378     <c-type>
379         fixnum >>class
380         fixnum >>boxed-class
381         [ alien-unsigned-1 ] >>getter
382         [ set-alien-unsigned-1 ] >>setter
383         1 >>size
384         1 >>align
385         1 >>align-first
386         "from_unsigned_1" >>boxer
387         "to_cell" >>unboxer
388     \ uchar define-primitive-type
389
390     cpu ppc? [
391         <c-type>
392             [ alien-unsigned-4 c-bool> ] >>getter
393             [ [ >c-bool ] 2dip set-alien-unsigned-4 ] >>setter
394             4 >>size
395             4 >>align
396             4 >>align-first
397             "from_boolean" >>boxer
398             "to_boolean" >>unboxer
399     ] [
400         <c-type>
401             [ alien-unsigned-1 c-bool> ] >>getter
402             [ [ >c-bool ] 2dip set-alien-unsigned-1 ] >>setter
403             1 >>size
404             1 >>align
405             1 >>align-first
406             "from_boolean" >>boxer
407             "to_boolean" >>unboxer
408     ] if
409     \ bool define-primitive-type
410
411     <c-type>
412         math:float >>class
413         math:float >>boxed-class
414         [ alien-float ] >>getter
415         [ [ >float ] 2dip set-alien-float ] >>setter
416         4 >>size
417         4 >>align
418         4 >>align-first
419         "from_float" >>boxer
420         "to_float" >>unboxer
421         float-rep >>rep
422         [ >float ] >>unboxer-quot
423     \ float define-primitive-type
424
425     <c-type>
426         math:float >>class
427         math:float >>boxed-class
428         [ alien-double ] >>getter
429         [ [ >float ] 2dip set-alien-double ] >>setter
430         8 >>size
431         8-byte-alignment
432         "from_double" >>boxer
433         "to_double" >>unboxer
434         double-rep >>rep
435         [ >float ] >>unboxer-quot
436     \ double define-primitive-type
437
438     cell 8 = [
439         <c-type>
440             integer >>class
441             integer >>boxed-class
442             [ alien-signed-cell ] >>getter
443             [ set-alien-signed-cell ] >>setter
444             bootstrap-cell >>size
445             bootstrap-cell >>align
446             bootstrap-cell >>align-first
447             "from_signed_cell" >>boxer
448             "to_fixnum" >>unboxer
449         \ longlong define-primitive-type
450
451         <c-type>
452             integer >>class
453             integer >>boxed-class
454             [ alien-unsigned-cell ] >>getter
455             [ set-alien-unsigned-cell ] >>setter
456             bootstrap-cell >>size
457             bootstrap-cell >>align
458             bootstrap-cell >>align-first
459             "from_unsigned_cell" >>boxer
460             "to_cell" >>unboxer
461         \ ulonglong define-primitive-type
462
463         os windows? [
464             \ int c-type \ long define-primitive-type
465             \ uint c-type \ ulong define-primitive-type
466         ] [
467             \ longlong c-type \ long define-primitive-type
468             \ ulonglong c-type \ ulong define-primitive-type
469         ] if
470
471         \ longlong c-type \ ptrdiff_t typedef
472         \ longlong c-type \ intptr_t typedef
473
474         \ ulonglong c-type \ uintptr_t typedef
475         \ ulonglong c-type \ size_t typedef
476     ] [
477         <long-long-type>
478             integer >>class
479             integer >>boxed-class
480             [ alien-signed-8 ] >>getter
481             [ set-alien-signed-8 ] >>setter
482             8 >>size
483             8-byte-alignment
484             "from_signed_8" >>boxer
485             "to_signed_8" >>unboxer
486         \ longlong define-primitive-type
487
488         <long-long-type>
489             integer >>class
490             integer >>boxed-class
491             [ alien-unsigned-8 ] >>getter
492             [ set-alien-unsigned-8 ] >>setter
493             8 >>size
494             8-byte-alignment
495             "from_unsigned_8" >>boxer
496             "to_unsigned_8" >>unboxer
497         \ ulonglong define-primitive-type
498
499         \ int c-type \ long define-primitive-type
500         \ uint c-type \ ulong define-primitive-type
501
502         \ int c-type \ ptrdiff_t typedef
503         \ int c-type \ intptr_t typedef
504
505         \ uint c-type \ uintptr_t typedef
506         \ uint c-type \ size_t typedef
507     ] if
508 ] with-compilation-unit
509
510 M: char-16-rep rep-component-type drop char ;
511 M: uchar-16-rep rep-component-type drop uchar ;
512 M: short-8-rep rep-component-type drop short ;
513 M: ushort-8-rep rep-component-type drop ushort ;
514 M: int-4-rep rep-component-type drop int ;
515 M: uint-4-rep rep-component-type drop uint ;
516 M: longlong-2-rep rep-component-type drop longlong ;
517 M: ulonglong-2-rep rep-component-type drop ulonglong ;
518 M: float-4-rep rep-component-type drop float ;
519 M: double-2-rep rep-component-type drop double ;
520
521 : (unsigned-interval) ( bytes -- from to ) [ 0 ] dip 8 * 2^ 1 - ; foldable
522 : unsigned-interval ( c-type -- from to ) heap-size (unsigned-interval) ; foldable
523 : (signed-interval) ( bytes -- from to ) 8 * 1 - 2^ [ neg ] [ 1 - ] bi ; foldable
524 : signed-interval ( c-type -- from to ) heap-size (signed-interval) ; foldable
525
526 : c-type-interval ( c-type -- from to )
527     {
528         { [ dup { float double } member-eq? ] [ drop -1/0. 1/0. ] }
529         { [ dup { char short int long longlong } member-eq? ] [ signed-interval ] }
530         { [ dup { uchar ushort uint ulong ulonglong } member-eq? ] [ unsigned-interval ] }
531     } cond ; foldable
532
533 : c-type-clamp ( value c-type -- value' )
534     dup { float double } member-eq?
535     [ drop ] [ c-type-interval clamp ] if ; inline