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