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