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
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 ppc? os macosx? and ] [ 4 >>align 8 >>align-first ] }
196 { [ cpu x86.32? os windows? not and ] [ 4 >>align 4 >>align-first ] }
197 [ 8 >>align 8 >>align-first ]
200 : resolve-pointer-typedef ( type -- base-type )
201 dup "c-type" word-prop dup word?
202 [ nip resolve-pointer-typedef ] [
203 pointer? [ drop void* ] when
206 : primitive-pointer-type? ( type -- ? )
208 resolve-pointer-typedef [ void? ] [ primitive-types member? ] bi or
211 : (pointer-c-type) ( void* type -- void*' )
212 [ clone ] dip c-type-boxer-quot '[ _ [ f ] if* ] >>boxer-quot ;
216 M: pointer lookup-c-type
217 [ \ void* lookup-c-type ] dip
218 to>> dup primitive-pointer-type? [ drop ] [ (pointer-c-type) ] if ;
224 [ alien-cell ] >>getter
225 [ set-alien-cell ] >>setter
226 bootstrap-cell >>size
227 bootstrap-cell >>align
228 bootstrap-cell >>align-first
229 [ >c-ptr ] >>unboxer-quot
230 "allot_alien" >>boxer
231 "alien_offset" >>unboxer
237 [ alien-signed-2 ] >>getter
238 [ set-alien-signed-2 ] >>setter
242 "from_signed_2" >>boxer
243 "to_signed_2" >>unboxer
244 [ >fixnum ] >>unboxer-quot
250 [ alien-unsigned-2 ] >>getter
251 [ set-alien-unsigned-2 ] >>setter
255 "from_unsigned_2" >>boxer
256 "to_unsigned_2" >>unboxer
257 [ >fixnum ] >>unboxer-quot
263 [ alien-signed-1 ] >>getter
264 [ set-alien-signed-1 ] >>setter
268 "from_signed_1" >>boxer
269 "to_signed_1" >>unboxer
270 [ >fixnum ] >>unboxer-quot
276 [ alien-unsigned-1 ] >>getter
277 [ set-alien-unsigned-1 ] >>setter
281 "from_unsigned_1" >>boxer
282 "to_unsigned_1" >>unboxer
283 [ >fixnum ] >>unboxer-quot
288 math:float >>boxed-class
289 [ alien-float ] >>getter
290 [ set-alien-float ] >>setter
297 [ >float ] >>unboxer-quot
302 math:float >>boxed-class
303 [ alien-double ] >>getter
304 [ set-alien-double ] >>setter
307 "from_double" >>boxer
308 "to_double" >>unboxer
310 [ >float ] >>unboxer-quot
317 [ alien-signed-4 ] >>getter
318 [ set-alien-signed-4 ] >>setter
322 "from_signed_4" >>boxer
323 "to_signed_4" >>unboxer
324 [ >fixnum ] >>unboxer-quot
330 [ alien-unsigned-4 ] >>getter
331 [ set-alien-unsigned-4 ] >>setter
335 "from_unsigned_4" >>boxer
336 "to_unsigned_4" >>unboxer
337 [ >fixnum ] >>unboxer-quot
342 integer >>boxed-class
343 [ alien-signed-cell ] >>getter
344 [ set-alien-signed-cell ] >>setter
348 "from_signed_cell" >>boxer
349 "to_fixnum" >>unboxer
350 [ >integer ] >>unboxer-quot
355 integer >>boxed-class
356 [ alien-unsigned-cell ] >>getter
357 [ set-alien-unsigned-cell ] >>setter
361 "from_unsigned_cell" >>boxer
363 [ >integer ] >>unboxer-quot
367 \ int lookup-c-type \ long typedef
368 \ uint lookup-c-type \ ulong typedef
370 \ longlong lookup-c-type \ long typedef
371 \ ulonglong lookup-c-type \ ulong typedef
374 \ longlong lookup-c-type \ ptrdiff_t typedef
375 \ longlong lookup-c-type \ intptr_t typedef
377 \ ulonglong lookup-c-type \ uintptr_t typedef
378 \ ulonglong lookup-c-type \ size_t typedef
382 integer >>boxed-class
383 [ alien-signed-cell ] >>getter
384 [ set-alien-signed-cell ] >>setter
388 "from_signed_cell" >>boxer
389 "to_fixnum" >>unboxer
390 [ >integer ] >>unboxer-quot
395 integer >>boxed-class
396 [ alien-unsigned-cell ] >>getter
397 [ set-alien-unsigned-cell ] >>setter
401 "from_unsigned_cell" >>boxer
403 [ >integer ] >>unboxer-quot
408 integer >>boxed-class
409 [ alien-signed-8 ] >>getter
410 [ set-alien-signed-8 ] >>setter
413 "from_signed_8" >>boxer
414 "to_signed_8" >>unboxer
415 [ >integer ] >>unboxer-quot
420 integer >>boxed-class
421 [ alien-unsigned-8 ] >>getter
422 [ set-alien-unsigned-8 ] >>setter
425 "from_unsigned_8" >>boxer
426 "to_unsigned_8" >>unboxer
427 [ >integer ] >>unboxer-quot
430 \ int lookup-c-type \ long typedef
431 \ uint lookup-c-type \ ulong typedef
433 \ int lookup-c-type \ ptrdiff_t typedef
434 \ int lookup-c-type \ intptr_t typedef
436 \ uint lookup-c-type \ uintptr_t typedef
437 \ uint lookup-c-type \ size_t typedef
440 cpu ppc? os macosx? and \ uint \ uchar ? lookup-c-type clone
441 [ >c-bool ] >>unboxer-quot
442 [ c-bool> ] >>boxer-quot
446 ] with-compilation-unit
448 M: char-16-rep rep-component-type drop char ;
449 M: uchar-16-rep rep-component-type drop uchar ;
450 M: short-8-rep rep-component-type drop short ;
451 M: ushort-8-rep rep-component-type drop ushort ;
452 M: int-4-rep rep-component-type drop int ;
453 M: uint-4-rep rep-component-type drop uint ;
454 M: longlong-2-rep rep-component-type drop longlong ;
455 M: ulonglong-2-rep rep-component-type drop ulonglong ;
456 M: float-4-rep rep-component-type drop float ;
457 M: double-2-rep rep-component-type drop double ;
459 : (unsigned-interval) ( bytes -- from to ) [ 0 ] dip 8 * 2^ 1 - ; foldable
460 : unsigned-interval ( c-type -- from to ) heap-size (unsigned-interval) ; foldable
461 : (signed-interval) ( bytes -- from to ) 8 * 1 - 2^ [ neg ] [ 1 - ] bi ; foldable
462 : signed-interval ( c-type -- from to ) heap-size (signed-interval) ; foldable
464 : c-type-interval ( c-type -- from to )
466 { [ dup { float double } member-eq? ] [ drop -1/0. 1/0. ] }
467 { [ dup { char short int long longlong } member-eq? ] [ signed-interval ] }
468 { [ dup { uchar ushort uint ulong ulonglong } member-eq? ] [ unsigned-interval ] }
471 : c-type-clamp ( value c-type -- value' )
472 dup { float double } member-eq?
473 [ drop ] [ c-type-interval clamp ] if ; inline