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 libc math
4 namespaces make parser sequences strings words splitting math.parser
5 cpu.architecture alien alien.accessors alien.strings quotations
6 layouts system compiler.units io io.files io.encodings.binary
7 io.streams.memory accessors combinators effects continuations fry
8 classes vocabs vocabs.loader ;
14 : little-endian? ( -- ? ) 1 <int> *char 1 = ; foldable
16 TUPLE: abstract-c-type
17 { class class initial: object }
18 { boxed-class class initial: object }
19 { boxer-quot callable }
20 { unboxer-quot callable }
28 direct-array-constructor
29 sequence-mixin-class ;
31 TUPLE: c-type < abstract-c-type
34 { rep initial: int-rep }
37 : <c-type> ( -- type )
43 c-types [ H{ } assoc-like ] change
46 ERROR: no-c-type name ;
48 : (c-type) ( name -- type/f )
49 c-types get-global at dup [
50 dup string? [ (c-type) ] when
54 GENERIC: c-type ( name -- type ) foldable
56 : resolve-pointer-type ( name -- name )
57 c-types get at dup string?
58 [ "*" append ] [ drop "void*" ] if
61 : resolve-typedef ( name -- type )
62 dup string? [ c-type ] when ;
64 : parse-array-type ( name -- array )
66 [ [ "]" ?tail drop string>number ] map ] dip prefix ;
68 M: string c-type ( name -- type )
69 CHAR: ] over member? [
75 "*" ?tail [ resolve-pointer-type ] [ no-c-type ] if
79 : ?require-word ( word/pair -- )
80 dup word? [ drop ] [ first require ] ?if ;
82 GENERIC: require-c-type-arrays ( c-type -- )
84 M: object require-c-type-arrays
87 M: c-type require-c-type-arrays
88 [ array-class>> ?require-word ]
89 [ sequence-mixin-class>> ?require-word ]
90 [ direct-array-class>> ?require-word ] tri ;
92 M: string require-c-type-arrays
93 c-type require-c-type-arrays ;
95 M: array require-c-type-arrays
96 first c-type require-c-type-arrays ;
98 ERROR: specialized-array-vocab-not-loaded vocab word ;
100 : c-type-array-constructor ( c-type -- word )
101 array-constructor>> dup array?
102 [ first2 specialized-array-vocab-not-loaded ] when ; foldable
104 : c-type-direct-array-constructor ( c-type -- word )
105 direct-array-constructor>> dup array?
106 [ first2 specialized-array-vocab-not-loaded ] when ; foldable
108 GENERIC: <c-type-array> ( len c-type -- array )
109 M: object <c-type-array>
110 c-type-array-constructor execute( len -- array ) ; inline
111 M: string <c-type-array>
112 c-type <c-type-array> ; inline
113 M: array <c-type-array>
114 first c-type <c-type-array> ; inline
116 GENERIC: <c-type-direct-array> ( alien len c-type -- array )
117 M: object <c-type-direct-array>
118 c-type-direct-array-constructor execute( alien len -- array ) ; inline
119 M: string <c-type-direct-array>
120 c-type <c-type-direct-array> ; inline
121 M: array <c-type-direct-array>
122 first c-type <c-type-direct-array> ; inline
124 GENERIC: c-type-class ( name -- class )
126 M: abstract-c-type c-type-class class>> ;
128 M: string c-type-class c-type c-type-class ;
130 GENERIC: c-type-boxed-class ( name -- class )
132 M: abstract-c-type c-type-boxed-class boxed-class>> ;
134 M: string c-type-boxed-class c-type c-type-boxed-class ;
136 GENERIC: c-type-boxer ( name -- boxer )
138 M: c-type c-type-boxer boxer>> ;
140 M: string c-type-boxer c-type c-type-boxer ;
142 GENERIC: c-type-boxer-quot ( name -- quot )
144 M: abstract-c-type c-type-boxer-quot boxer-quot>> ;
146 M: string c-type-boxer-quot c-type c-type-boxer-quot ;
148 GENERIC: c-type-unboxer ( name -- boxer )
150 M: c-type c-type-unboxer unboxer>> ;
152 M: string c-type-unboxer c-type c-type-unboxer ;
154 GENERIC: c-type-unboxer-quot ( name -- quot )
156 M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ;
158 M: string c-type-unboxer-quot c-type c-type-unboxer-quot ;
160 GENERIC: c-type-rep ( name -- rep )
162 M: c-type c-type-rep rep>> ;
164 M: string c-type-rep c-type c-type-rep ;
166 GENERIC: c-type-getter ( name -- quot )
168 M: c-type c-type-getter getter>> ;
170 M: string c-type-getter c-type c-type-getter ;
172 GENERIC: c-type-setter ( name -- quot )
174 M: c-type c-type-setter setter>> ;
176 M: string c-type-setter c-type c-type-setter ;
178 GENERIC: c-type-align ( name -- n )
180 M: abstract-c-type c-type-align align>> ;
182 M: string c-type-align c-type c-type-align ;
184 GENERIC: c-type-stack-align? ( name -- ? )
186 M: c-type c-type-stack-align? stack-align?>> ;
188 M: string c-type-stack-align? c-type c-type-stack-align? ;
190 : c-type-box ( n type -- )
191 [ c-type-rep ] [ c-type-boxer [ "No boxer" throw ] unless* ] bi
194 : c-type-unbox ( n ctype -- )
195 [ c-type-rep ] [ c-type-unboxer [ "No unboxer" throw ] unless* ] bi
198 GENERIC: box-parameter ( n ctype -- )
200 M: c-type box-parameter c-type-box ;
202 M: string box-parameter c-type box-parameter ;
204 GENERIC: box-return ( ctype -- )
206 M: c-type box-return f swap c-type-box ;
208 M: string box-return c-type box-return ;
210 GENERIC: unbox-parameter ( n ctype -- )
212 M: c-type unbox-parameter c-type-unbox ;
214 M: string unbox-parameter c-type unbox-parameter ;
216 GENERIC: unbox-return ( ctype -- )
218 M: c-type unbox-return f swap c-type-unbox ;
220 M: string unbox-return c-type unbox-return ;
222 ! These words being foldable means that words need to be
223 ! recompiled if a C type is redefined. Even so, folding the
224 ! size facilitates some optimizations.
225 GENERIC: heap-size ( type -- size ) foldable
227 M: string heap-size c-type heap-size ;
229 M: abstract-c-type heap-size size>> ;
231 GENERIC: stack-size ( type -- size ) foldable
233 M: string stack-size c-type stack-size ;
235 M: c-type stack-size size>> cell align ;
237 GENERIC: byte-length ( seq -- n ) flushable
239 M: byte-array byte-length length ; inline
241 M: f byte-length drop 0 ; inline
243 : c-getter ( name -- quot )
245 [ "Cannot read struct fields with this type" throw ]
248 : c-type-getter-boxer ( name -- quot )
249 [ c-getter ] [ c-type-boxer-quot ] bi append ;
251 : c-setter ( name -- quot )
253 [ "Cannot write struct fields with this type" throw ]
256 : <c-array> ( n type -- array )
257 heap-size * <byte-array> ; inline deprecated
259 : <c-object> ( type -- array )
260 heap-size <byte-array> ; inline
262 : (c-object) ( type -- array )
263 heap-size (byte-array) ; inline
265 : malloc-array ( n type -- alien )
266 [ heap-size calloc ] [ <c-type-direct-array> ] 2bi ; inline
268 : malloc-object ( type -- alien )
269 1 swap heap-size calloc ; inline
271 : malloc-byte-array ( byte-array -- alien )
272 dup byte-length [ nip malloc dup ] 2keep memcpy ;
274 : memory>byte-array ( alien len -- byte-array )
275 [ nip (byte-array) dup ] 2keep memcpy ;
277 : malloc-string ( string encoding -- alien )
278 string>alien malloc-byte-array ;
280 M: memory-stream stream-read
282 [ index>> ] [ alien>> ] bi <displaced-alien>
283 swap memory>byte-array
284 ] [ [ + ] change-index drop ] 2bi ;
286 : byte-array>memory ( byte-array base -- )
287 swap dup byte-length memcpy ; inline
289 : array-accessor ( type quot -- def )
291 \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi*
294 : typedef ( old new -- ) c-types get set-at ;
296 TUPLE: long-long-type < c-type ;
298 : <long-long-type> ( -- type )
301 M: long-long-type unbox-parameter ( n type -- )
302 c-type-unboxer %unbox-long-long ;
304 M: long-long-type unbox-return ( type -- )
305 f swap unbox-parameter ;
307 M: long-long-type box-parameter ( n type -- )
308 c-type-boxer %box-long-long ;
310 M: long-long-type box-return ( type -- )
311 f swap box-parameter ;
313 : define-deref ( name -- )
314 [ CHAR: * prefix "alien.c-types" create ] [ c-getter 0 prefix ] bi
315 (( c-ptr -- value )) define-inline ;
317 : define-out ( name -- )
318 [ "alien.c-types" constructor-word ]
319 [ dup c-setter '[ _ <c-object> [ 0 @ ] keep ] ] bi
320 (( value -- c-ptr )) define-inline ;
322 : >c-bool ( ? -- int ) 1 0 ? ; inline
324 : c-bool> ( int -- ? ) 0 = not ; inline
326 : define-primitive-type ( type name -- )
332 : malloc-file-contents ( path -- alien len )
333 binary file-contents [ malloc-byte-array ] [ length ] bi ;
335 : if-void ( type true false -- )
336 pick "void" = [ drop nip call ] [ nip call ] if ; inline
338 : ?lookup ( vocab word -- word/pair )
339 over vocab [ swap lookup ] [ 2array ] if ;
341 : set-array-class* ( c-type vocab-stem type-stem -- c-type )
344 [ "specialized-arrays." prepend ]
345 [ "-array" append ] bi* ?lookup >>array-class
348 [ "specialized-arrays." prepend ]
349 [ "<" "-array>" surround ] bi* ?lookup >>array-constructor
352 [ "specialized-arrays." prepend ]
353 [ "-sequence" append ] bi* ?lookup >>sequence-mixin-class
356 [ "specialized-arrays.direct." prepend ]
357 [ "direct-" "-array" surround ] bi* ?lookup >>direct-array-class
360 [ "specialized-arrays.direct." prepend ]
361 [ "<direct-" "-array>" surround ] bi* ?lookup >>direct-array-constructor
365 : set-array-class ( c-type stem -- c-type )
366 dup set-array-class* ;
368 CONSTANT: primitive-types
374 "longlong" "ulonglong"
383 [ alien-cell ] >>getter
384 [ [ >c-ptr ] 2dip set-alien-cell ] >>setter
385 bootstrap-cell >>size
386 bootstrap-cell >>align
387 [ >c-ptr ] >>unboxer-quot
389 "alien_offset" >>unboxer
390 "alien" "void*" set-array-class*
391 "void*" define-primitive-type
395 integer >>boxed-class
396 [ alien-signed-8 ] >>getter
397 [ set-alien-signed-8 ] >>setter
400 "box_signed_8" >>boxer
401 "to_signed_8" >>unboxer
402 "longlong" set-array-class
403 "longlong" define-primitive-type
407 integer >>boxed-class
408 [ alien-unsigned-8 ] >>getter
409 [ set-alien-unsigned-8 ] >>setter
412 "box_unsigned_8" >>boxer
413 "to_unsigned_8" >>unboxer
414 "ulonglong" set-array-class
415 "ulonglong" define-primitive-type
419 integer >>boxed-class
420 [ alien-signed-cell ] >>getter
421 [ set-alien-signed-cell ] >>setter
422 bootstrap-cell >>size
423 bootstrap-cell >>align
424 "box_signed_cell" >>boxer
425 "to_fixnum" >>unboxer
426 "long" set-array-class
427 "long" define-primitive-type
431 integer >>boxed-class
432 [ alien-unsigned-cell ] >>getter
433 [ set-alien-unsigned-cell ] >>setter
434 bootstrap-cell >>size
435 bootstrap-cell >>align
436 "box_unsigned_cell" >>boxer
438 "ulong" set-array-class
439 "ulong" define-primitive-type
443 integer >>boxed-class
444 [ alien-signed-4 ] >>getter
445 [ set-alien-signed-4 ] >>setter
448 "box_signed_4" >>boxer
449 "to_fixnum" >>unboxer
450 "int" set-array-class
451 "int" define-primitive-type
455 integer >>boxed-class
456 [ alien-unsigned-4 ] >>getter
457 [ set-alien-unsigned-4 ] >>setter
460 "box_unsigned_4" >>boxer
462 "uint" set-array-class
463 "uint" define-primitive-type
468 [ alien-signed-2 ] >>getter
469 [ set-alien-signed-2 ] >>setter
472 "box_signed_2" >>boxer
473 "to_fixnum" >>unboxer
474 "short" set-array-class
475 "short" define-primitive-type
480 [ alien-unsigned-2 ] >>getter
481 [ set-alien-unsigned-2 ] >>setter
484 "box_unsigned_2" >>boxer
486 "ushort" set-array-class
487 "ushort" define-primitive-type
492 [ alien-signed-1 ] >>getter
493 [ set-alien-signed-1 ] >>setter
496 "box_signed_1" >>boxer
497 "to_fixnum" >>unboxer
498 "char" set-array-class
499 "char" define-primitive-type
504 [ alien-unsigned-1 ] >>getter
505 [ set-alien-unsigned-1 ] >>setter
508 "box_unsigned_1" >>boxer
510 "uchar" set-array-class
511 "uchar" define-primitive-type
514 [ alien-unsigned-1 c-bool> ] >>getter
515 [ [ >c-bool ] 2dip set-alien-unsigned-1 ] >>setter
518 "box_boolean" >>boxer
519 "to_boolean" >>unboxer
520 "bool" set-array-class
521 "bool" define-primitive-type
526 [ alien-float ] >>getter
527 [ [ >float ] 2dip set-alien-float ] >>setter
532 single-float-rep >>rep
533 [ >float ] >>unboxer-quot
534 "float" set-array-class
535 "float" define-primitive-type
540 [ alien-double ] >>getter
541 [ [ >float ] 2dip set-alien-double ] >>setter
545 "to_double" >>unboxer
546 double-float-rep >>rep
547 [ >float ] >>unboxer-quot
548 "double" set-array-class
549 "double" define-primitive-type
551 "long" "ptrdiff_t" typedef
552 "long" "intptr_t" typedef
553 "ulong" "size_t" typedef
554 ] with-compilation-unit