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 ;
21 TUPLE: abstract-c-type
22 { class class initial: object }
23 { boxed-class class initial: object }
24 { boxer-quot callable }
25 { 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 ;
65 GENERIC: c-type-class ( name -- class )
67 M: abstract-c-type c-type-class class>> ;
69 GENERIC: c-type-boxed-class ( name -- class )
71 M: abstract-c-type c-type-boxed-class boxed-class>> ;
73 GENERIC: c-type-boxer-quot ( name -- quot )
75 M: abstract-c-type c-type-boxer-quot boxer-quot>> ;
77 GENERIC: c-type-unboxer-quot ( name -- quot )
79 M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ;
81 GENERIC: c-type-rep ( name -- rep )
83 M: c-type c-type-rep rep>> ;
85 GENERIC: c-type-getter ( name -- quot )
87 M: c-type c-type-getter getter>> ;
89 GENERIC: c-type-copier ( name -- quot )
91 M: c-type c-type-copier drop [ ] ;
93 GENERIC: c-type-setter ( name -- quot )
95 M: c-type c-type-setter setter>> ;
97 GENERIC: c-type-align ( name -- n ) foldable
99 M: abstract-c-type c-type-align align>> ;
101 GENERIC: c-type-align-first ( name -- n )
103 M: abstract-c-type c-type-align-first align-first>> ;
105 GENERIC: base-type ( c-type -- c-type )
107 M: c-type-name base-type lookup-c-type ;
109 M: c-type base-type ;
111 GENERIC: heap-size ( name -- size )
113 M: abstract-c-type heap-size size>> ;
117 MACRO: alien-value ( c-type -- quot: ( c-ptr offset -- value ) )
118 [ c-type-getter ] [ c-type-boxer-quot ] bi append ;
120 MACRO: alien-copy-value ( c-type -- quot: ( c-ptr offset -- value ) )
121 [ c-type-getter ] [ c-type-copier ] [ c-type-boxer-quot ] tri 3append ;
123 MACRO: set-alien-value ( c-type -- quot: ( value c-ptr offset -- ) )
124 [ c-type-unboxer-quot [ [ ] ] [ '[ _ 2dip ] ] if-empty ]
128 : array-accessor ( n c-ptr c-type -- c-ptr offset c-type )
129 [ swapd heap-size * >fixnum ] keep ; inline
131 : alien-element ( n c-ptr c-type -- value )
132 array-accessor alien-value ; inline
134 : set-alien-element ( value n c-ptr c-type -- )
135 array-accessor set-alien-value ; inline
137 PROTOCOL: c-type-protocol
151 CONSULT: c-type-protocol c-type-name
154 PREDICATE: typedef-word < c-type-word
155 "c-type" word-prop [ c-type-name? ] [ array? ] bi or ;
157 : typedef ( old new -- )
159 [ nip define-symbol ]
160 [ swap "c-type" set-word-prop ]
163 TUPLE: long-long-type < c-type ;
165 : <long-long-type> ( -- c-type )
168 : if-void ( ..a c-type true: ( ..a -- ..b ) false: ( ..a c-type -- ..b ) -- ..b )
169 pick void? [ drop nip call ] [ nip call ] if ; inline
172 ptrdiff_t intptr_t uintptr_t size_t
175 CONSTANT: primitive-types
187 : >c-bool ( ? -- int ) 1 0 ? ; inline
189 : c-bool> ( int -- ? ) 0 = not ; inline
193 : 8-byte-alignment ( c-type -- c-type )
195 { [ cpu x86.32? os windows? not and ] [ 4 >>align 4 >>align-first ] }
196 [ 8 >>align 8 >>align-first ]
199 : resolve-pointer-typedef ( type -- base-type )
200 dup "c-type" word-prop dup word?
201 [ nip resolve-pointer-typedef ] [
202 pointer? [ drop void* ] when
205 : primitive-pointer-type? ( type -- ? )
207 resolve-pointer-typedef [ void? ] [ primitive-types member? ] bi or
210 : (pointer-c-type) ( void* type -- void*' )
211 [ clone ] dip c-type-boxer-quot '[ _ [ f ] if* ] >>boxer-quot ;
215 M: pointer lookup-c-type
216 [ \ void* lookup-c-type ] dip
217 to>> dup primitive-pointer-type? [ drop ] [ (pointer-c-type) ] if ;
223 [ alien-cell ] >>getter
224 [ set-alien-cell ] >>setter
225 bootstrap-cell >>size
226 bootstrap-cell >>align
227 bootstrap-cell >>align-first
228 [ >c-ptr ] >>unboxer-quot
229 "allot_alien" >>boxer
230 "alien_offset" >>unboxer
236 [ alien-signed-2 ] >>getter
237 [ set-alien-signed-2 ] >>setter
241 "from_signed_2" >>boxer
242 "to_signed_2" >>unboxer
243 [ >fixnum ] >>unboxer-quot
249 [ alien-unsigned-2 ] >>getter
250 [ set-alien-unsigned-2 ] >>setter
254 "from_unsigned_2" >>boxer
255 "to_unsigned_2" >>unboxer
256 [ >fixnum ] >>unboxer-quot
262 [ alien-signed-1 ] >>getter
263 [ set-alien-signed-1 ] >>setter
267 "from_signed_1" >>boxer
268 "to_signed_1" >>unboxer
269 [ >fixnum ] >>unboxer-quot
275 [ alien-unsigned-1 ] >>getter
276 [ set-alien-unsigned-1 ] >>setter
280 "from_unsigned_1" >>boxer
281 "to_unsigned_1" >>unboxer
282 [ >fixnum ] >>unboxer-quot
287 math:float >>boxed-class
288 [ alien-float ] >>getter
289 [ set-alien-float ] >>setter
296 [ >float ] >>unboxer-quot
301 math:float >>boxed-class
302 [ alien-double ] >>getter
303 [ set-alien-double ] >>setter
306 "from_double" >>boxer
307 "to_double" >>unboxer
309 [ >float ] >>unboxer-quot
316 [ alien-signed-4 ] >>getter
317 [ set-alien-signed-4 ] >>setter
321 "from_signed_4" >>boxer
322 "to_signed_4" >>unboxer
323 [ >fixnum ] >>unboxer-quot
329 [ alien-unsigned-4 ] >>getter
330 [ set-alien-unsigned-4 ] >>setter
334 "from_unsigned_4" >>boxer
335 "to_unsigned_4" >>unboxer
336 [ >fixnum ] >>unboxer-quot
341 integer >>boxed-class
342 [ alien-signed-cell ] >>getter
343 [ set-alien-signed-cell ] >>setter
347 "from_signed_cell" >>boxer
348 "to_fixnum" >>unboxer
349 [ >integer ] >>unboxer-quot
354 integer >>boxed-class
355 [ alien-unsigned-cell ] >>getter
356 [ set-alien-unsigned-cell ] >>setter
360 "from_unsigned_cell" >>boxer
362 [ >integer ] >>unboxer-quot
366 \ int lookup-c-type \ long typedef
367 \ uint lookup-c-type \ ulong typedef
369 \ longlong lookup-c-type \ long typedef
370 \ ulonglong lookup-c-type \ ulong typedef
373 \ longlong lookup-c-type \ ptrdiff_t typedef
374 \ longlong lookup-c-type \ intptr_t typedef
376 \ ulonglong lookup-c-type \ uintptr_t typedef
377 \ ulonglong lookup-c-type \ size_t typedef
381 integer >>boxed-class
382 [ alien-signed-cell ] >>getter
383 [ set-alien-signed-cell ] >>setter
387 "from_signed_cell" >>boxer
388 "to_fixnum" >>unboxer
389 [ >integer ] >>unboxer-quot
394 integer >>boxed-class
395 [ alien-unsigned-cell ] >>getter
396 [ set-alien-unsigned-cell ] >>setter
400 "from_unsigned_cell" >>boxer
402 [ >integer ] >>unboxer-quot
407 integer >>boxed-class
408 [ alien-signed-8 ] >>getter
409 [ set-alien-signed-8 ] >>setter
412 "from_signed_8" >>boxer
413 "to_signed_8" >>unboxer
414 [ >integer ] >>unboxer-quot
419 integer >>boxed-class
420 [ alien-unsigned-8 ] >>getter
421 [ set-alien-unsigned-8 ] >>setter
424 "from_unsigned_8" >>boxer
425 "to_unsigned_8" >>unboxer
426 [ >integer ] >>unboxer-quot
429 \ int lookup-c-type \ long typedef
430 \ uint lookup-c-type \ ulong typedef
432 \ int lookup-c-type \ ptrdiff_t typedef
433 \ int lookup-c-type \ intptr_t typedef
435 \ uint lookup-c-type \ uintptr_t typedef
436 \ uint lookup-c-type \ size_t typedef
439 \ uchar lookup-c-type clone
440 [ >c-bool ] >>unboxer-quot
441 [ c-bool> ] >>boxer-quot
445 ] with-compilation-unit
447 M: char-16-rep rep-component-type drop char ;
448 M: uchar-16-rep rep-component-type drop uchar ;
449 M: short-8-rep rep-component-type drop short ;
450 M: ushort-8-rep rep-component-type drop ushort ;
451 M: int-4-rep rep-component-type drop int ;
452 M: uint-4-rep rep-component-type drop uint ;
453 M: longlong-2-rep rep-component-type drop longlong ;
454 M: ulonglong-2-rep rep-component-type drop ulonglong ;
455 M: float-4-rep rep-component-type drop float ;
456 M: double-2-rep rep-component-type drop double ;
458 : (unsigned-interval) ( bytes -- from to ) [ 0 ] dip 8 * 2^ 1 - ; foldable
459 : unsigned-interval ( c-type -- from to ) heap-size (unsigned-interval) ; foldable
460 : (signed-interval) ( bytes -- from to ) 8 * 1 - 2^ [ neg ] [ 1 - ] bi ; foldable
461 : signed-interval ( c-type -- from to ) heap-size (signed-interval) ; foldable
463 : c-type-interval ( c-type -- from to )
465 { [ dup { float double } member-eq? ] [ drop -1/0. 1/0. ] }
466 { [ dup { char short int long longlong } member-eq? ] [ signed-interval ] }
467 { [ dup { uchar ushort uint ulong ulonglong } member-eq? ] [ unsigned-interval ] }
470 : c-type-clamp ( value c-type -- value' )
471 dup { float double } member-eq?
472 [ drop ] [ c-type-interval clamp ] if ; inline