1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.accessors arrays byte-arrays
4 classes combinators compiler.units cpu.architecture delegate
5 fry kernel layouts locals macros math math.order quotations
6 sequences system words words.symbol summary ;
20 TUPLE: abstract-c-type
21 { class class initial: object }
22 { boxed-class class initial: object }
23 { boxer-quot callable }
24 { unboxer-quot callable }
30 { align-first integer } ;
32 TUPLE: c-type < abstract-c-type
35 { rep initial: int-rep } ;
37 : <c-type> ( -- c-type )
40 ERROR: no-c-type word ;
42 M: no-c-type summary drop "Not a C type" ;
45 GENERIC: lookup-c-type ( name -- c-type ) foldable
47 PREDICATE: c-type-word < word
48 "c-type" word-prop >boolean ;
50 TUPLE: pointer { to initial: void read-only } ;
56 : resolve-typedef ( name -- c-type )
57 dup void? [ no-c-type ] when
58 dup c-type-name? [ lookup-c-type ] when ;
61 dup "c-type" word-prop resolve-typedef
62 [ ] [ no-c-type ] ?if ;
64 GENERIC: c-type-class ( name -- class )
66 M: abstract-c-type c-type-class class>> ;
68 GENERIC: c-type-boxed-class ( name -- class )
70 M: abstract-c-type c-type-boxed-class boxed-class>> ;
72 GENERIC: c-type-boxer-quot ( name -- quot )
74 M: abstract-c-type c-type-boxer-quot boxer-quot>> ;
76 GENERIC: c-type-unboxer-quot ( name -- quot )
78 M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ;
80 GENERIC: c-type-rep ( name -- rep )
82 M: c-type c-type-rep rep>> ;
84 GENERIC: c-type-getter ( name -- quot )
86 M: c-type c-type-getter getter>> ;
88 GENERIC: c-type-copier ( name -- quot )
90 M: c-type c-type-copier drop [ ] ;
92 GENERIC: c-type-setter ( name -- quot )
94 M: c-type c-type-setter setter>> ;
96 GENERIC: c-type-signed ( name -- boolean ) foldable
98 M: abstract-c-type c-type-signed signed>> ;
100 GENERIC: c-type-align ( name -- n ) foldable
102 M: abstract-c-type c-type-align align>> ;
104 GENERIC: c-type-align-first ( name -- n )
106 M: abstract-c-type c-type-align-first align-first>> ;
108 GENERIC: base-type ( c-type -- c-type )
110 M: c-type-name base-type lookup-c-type ;
112 M: c-type base-type ;
114 GENERIC: heap-size ( name -- size )
116 M: abstract-c-type heap-size size>> ;
120 MACRO: alien-value ( c-type -- quot: ( c-ptr offset -- value ) )
121 [ c-type-getter ] [ c-type-boxer-quot ] bi append ;
123 MACRO: alien-copy-value ( c-type -- quot: ( c-ptr offset -- value ) )
124 [ c-type-getter ] [ c-type-copier ] [ c-type-boxer-quot ] tri 3append ;
126 MACRO: set-alien-value ( c-type -- quot: ( value c-ptr offset -- ) )
127 [ c-type-unboxer-quot [ [ ] ] [ '[ _ 2dip ] ] if-empty ]
131 : array-accessor ( n c-ptr c-type -- c-ptr offset c-type )
132 [ swapd heap-size * >fixnum ] keep ; inline
134 : alien-element ( n c-ptr c-type -- value )
135 array-accessor alien-value ; inline
137 : set-alien-element ( value n c-ptr c-type -- )
138 array-accessor set-alien-value ; inline
140 PROTOCOL: c-type-protocol
155 CONSULT: c-type-protocol c-type-name
158 PREDICATE: typedef-word < c-type-word
159 "c-type" word-prop [ c-type-name? ] [ array? ] bi or ;
161 : typedef ( old new -- )
163 [ nip define-symbol ]
164 [ swap "c-type" set-word-prop ]
167 TUPLE: long-long-type < c-type ;
169 : <long-long-type> ( -- c-type )
172 : if-void ( ..a c-type true: ( ..a -- ..b ) false: ( ..a c-type -- ..b ) -- ..b )
173 pick void? [ drop nip call ] [ nip call ] if ; inline
176 ptrdiff_t intptr_t uintptr_t size_t
177 c-string int8_t uint8_t int16_t uint16_t
178 int32_t uint32_t int64_t uint64_t ;
180 CONSTANT: primitive-types
192 : >c-bool ( ? -- int ) 1 0 ? ; inline
194 : c-bool> ( int -- ? ) 0 = not ; inline
198 : 8-byte-alignment ( c-type -- c-type )
200 { [ cpu x86.32? os windows? not and ] [ 4 >>align 4 >>align-first ] }
201 [ 8 >>align 8 >>align-first ]
204 : resolve-pointer-typedef ( type -- base-type )
205 dup "c-type" word-prop dup word?
206 [ nip resolve-pointer-typedef ] [
207 pointer? [ drop void* ] when
210 : primitive-pointer-type? ( type -- ? )
212 resolve-pointer-typedef [ void? ] [ primitive-types member? ] bi or
215 : (pointer-c-type) ( void* type -- void*' )
216 [ clone ] dip c-type-boxer-quot '[ _ [ f ] if* ] >>boxer-quot ;
220 M: pointer lookup-c-type
221 [ \ void* lookup-c-type ] dip
222 to>> dup primitive-pointer-type? [ drop ] [ (pointer-c-type) ] if ;
228 [ alien-cell ] >>getter
229 [ set-alien-cell ] >>setter
230 bootstrap-cell >>size
231 bootstrap-cell >>align
232 bootstrap-cell >>align-first
233 [ >c-ptr ] >>unboxer-quot
234 "allot_alien" >>boxer
235 "alien_offset" >>unboxer
241 [ alien-signed-2 ] >>getter
242 [ set-alien-signed-2 ] >>setter
247 "from_signed_2" >>boxer
248 "to_signed_2" >>unboxer
249 [ >fixnum ] >>unboxer-quot
255 [ alien-unsigned-2 ] >>getter
256 [ set-alien-unsigned-2 ] >>setter
260 "from_unsigned_2" >>boxer
261 "to_unsigned_2" >>unboxer
262 [ >fixnum ] >>unboxer-quot
268 [ alien-signed-1 ] >>getter
269 [ set-alien-signed-1 ] >>setter
274 "from_signed_1" >>boxer
275 "to_signed_1" >>unboxer
276 [ >fixnum ] >>unboxer-quot
282 [ alien-unsigned-1 ] >>getter
283 [ set-alien-unsigned-1 ] >>setter
287 "from_unsigned_1" >>boxer
288 "to_unsigned_1" >>unboxer
289 [ >fixnum ] >>unboxer-quot
294 math:float >>boxed-class
295 [ alien-float ] >>getter
296 [ set-alien-float ] >>setter
303 [ >float ] >>unboxer-quot
308 math:float >>boxed-class
309 [ alien-double ] >>getter
310 [ set-alien-double ] >>setter
313 "from_double" >>boxer
314 "to_double" >>unboxer
316 [ >float ] >>unboxer-quot
324 [ alien-signed-4 ] >>getter
325 [ set-alien-signed-4 ] >>setter
330 "from_signed_4" >>boxer
331 "to_signed_4" >>unboxer
332 [ >fixnum ] >>unboxer-quot
339 [ alien-unsigned-4 ] >>getter
340 [ set-alien-unsigned-4 ] >>setter
344 "from_unsigned_4" >>boxer
345 "to_unsigned_4" >>unboxer
346 [ >fixnum ] >>unboxer-quot
352 integer >>boxed-class
353 [ alien-signed-cell ] >>getter
354 [ set-alien-signed-cell ] >>setter
359 "from_signed_cell" >>boxer
360 "to_signed_8" >>unboxer
361 [ >integer ] >>unboxer-quot
367 integer >>boxed-class
368 [ alien-unsigned-cell ] >>getter
369 [ set-alien-unsigned-cell ] >>setter
373 "from_unsigned_cell" >>boxer
375 [ >integer ] >>unboxer-quot
379 \ int lookup-c-type \ long typedef
380 \ uint lookup-c-type \ ulong typedef
382 \ longlong lookup-c-type \ long typedef
383 \ ulonglong lookup-c-type \ ulong typedef
386 \ longlong lookup-c-type \ ptrdiff_t typedef
387 \ longlong lookup-c-type \ intptr_t typedef
389 \ ulonglong lookup-c-type \ uintptr_t typedef
390 \ ulonglong lookup-c-type \ size_t typedef
395 integer >>boxed-class
396 [ alien-signed-cell ] >>getter
397 [ set-alien-signed-cell ] >>setter
402 "from_signed_cell" >>boxer
403 "to_fixnum" >>unboxer
404 [ >integer ] >>unboxer-quot
410 integer >>boxed-class
411 [ alien-unsigned-cell ] >>getter
412 [ set-alien-unsigned-cell ] >>setter
416 "from_unsigned_cell" >>boxer
418 [ >integer ] >>unboxer-quot
424 integer >>boxed-class
425 [ alien-signed-8 ] >>getter
426 [ set-alien-signed-8 ] >>setter
430 "from_signed_8" >>boxer
431 "to_signed_8" >>unboxer
432 [ >integer ] >>unboxer-quot
438 integer >>boxed-class
439 [ alien-unsigned-8 ] >>getter
440 [ set-alien-unsigned-8 ] >>setter
443 "from_unsigned_8" >>boxer
444 "to_unsigned_8" >>unboxer
445 [ >integer ] >>unboxer-quot
448 \ int lookup-c-type \ long typedef
449 \ uint lookup-c-type \ ulong typedef
451 \ int lookup-c-type \ ptrdiff_t typedef
452 \ int lookup-c-type \ intptr_t typedef
454 \ uint lookup-c-type \ uintptr_t typedef
455 \ uint lookup-c-type \ size_t typedef
458 \ uchar lookup-c-type clone
459 [ >c-bool ] >>unboxer-quot
460 [ c-bool> ] >>boxer-quot
464 \ char lookup-c-type int8_t typedef
465 \ short lookup-c-type int16_t typedef
466 \ int lookup-c-type int32_t typedef
467 \ longlong lookup-c-type int64_t typedef
469 \ uchar lookup-c-type uint8_t typedef
470 \ ushort lookup-c-type uint16_t typedef
471 \ uint lookup-c-type uint32_t typedef
472 \ ulonglong lookup-c-type uint64_t typedef
474 ] with-compilation-unit
476 M: char-16-rep rep-component-type drop char ;
477 M: uchar-16-rep rep-component-type drop uchar ;
478 M: short-8-rep rep-component-type drop short ;
479 M: ushort-8-rep rep-component-type drop ushort ;
480 M: int-4-rep rep-component-type drop int ;
481 M: uint-4-rep rep-component-type drop uint ;
482 M: longlong-2-rep rep-component-type drop longlong ;
483 M: ulonglong-2-rep rep-component-type drop ulonglong ;
484 M: float-4-rep rep-component-type drop float ;
485 M: double-2-rep rep-component-type drop double ;
487 : (unsigned-interval) ( bytes -- from to ) [ 0 ] dip 8 * 2^ 1 - ; foldable
488 : unsigned-interval ( c-type -- from to ) heap-size (unsigned-interval) ; foldable
489 : (signed-interval) ( bytes -- from to ) 8 * 1 - 2^ [ neg ] [ 1 - ] bi ; foldable
490 : signed-interval ( c-type -- from to ) heap-size (signed-interval) ; foldable
492 : c-type-interval ( c-type -- from to )
494 { [ dup { float double } member-eq? ] [ drop -1/0. 1/0. ] }
495 { [ dup c-type-signed ] [ signed-interval ] }
496 { [ dup c-type-signed not ] [ unsigned-interval ] }
499 : c-type-clamp ( value c-type -- value' )
500 dup { float double } member-eq?
501 [ drop ] [ c-type-interval clamp ] if ; inline