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 }
26 TUPLE: c-type < abstract-c-type
29 { rep initial: int-rep }
34 direct-array-constructor
35 sequence-mixin-class ;
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 GENERIC: c-type-array-constructor ( c-type -- word ) foldable
102 M: string c-type-array-constructor
103 c-type c-type-array-constructor ;
104 M: array c-type-array-constructor
105 first c-type c-type-array-constructor ;
106 M: c-type c-type-array-constructor
107 array-constructor>> dup word?
108 [ first2 specialized-array-vocab-not-loaded ] unless ;
110 GENERIC: c-type-direct-array-constructor ( c-type -- word ) foldable
112 M: string c-type-direct-array-constructor
113 c-type c-type-direct-array-constructor ;
114 M: array c-type-direct-array-constructor
115 first c-type c-type-direct-array-constructor ;
116 M: c-type c-type-direct-array-constructor
117 direct-array-constructor>> dup word?
118 [ first2 specialized-array-vocab-not-loaded ] unless ;
120 GENERIC: <c-type-array> ( len c-type -- array )
121 M: object <c-type-array>
122 c-type-array-constructor execute( len -- array ) ; inline
124 GENERIC: <c-type-direct-array> ( alien len c-type -- array )
125 M: object <c-type-direct-array>
126 c-type-direct-array-constructor execute( alien len -- array ) ; inline
128 GENERIC: c-type-class ( name -- class )
130 M: abstract-c-type c-type-class class>> ;
132 M: string c-type-class c-type c-type-class ;
134 GENERIC: c-type-boxed-class ( name -- class )
136 M: abstract-c-type c-type-boxed-class boxed-class>> ;
138 M: string c-type-boxed-class c-type c-type-boxed-class ;
140 GENERIC: c-type-boxer ( name -- boxer )
142 M: c-type c-type-boxer boxer>> ;
144 M: string c-type-boxer c-type c-type-boxer ;
146 GENERIC: c-type-boxer-quot ( name -- quot )
148 M: abstract-c-type c-type-boxer-quot boxer-quot>> ;
150 M: string c-type-boxer-quot c-type c-type-boxer-quot ;
152 GENERIC: c-type-unboxer ( name -- boxer )
154 M: c-type c-type-unboxer unboxer>> ;
156 M: string c-type-unboxer c-type c-type-unboxer ;
158 GENERIC: c-type-unboxer-quot ( name -- quot )
160 M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ;
162 M: string c-type-unboxer-quot c-type c-type-unboxer-quot ;
164 GENERIC: c-type-rep ( name -- rep )
166 M: c-type c-type-rep rep>> ;
168 M: string c-type-rep c-type c-type-rep ;
170 GENERIC: c-type-getter ( name -- quot )
172 M: c-type c-type-getter getter>> ;
174 M: string c-type-getter c-type c-type-getter ;
176 GENERIC: c-type-setter ( name -- quot )
178 M: c-type c-type-setter setter>> ;
180 M: string c-type-setter c-type c-type-setter ;
182 GENERIC: c-type-align ( name -- n )
184 M: abstract-c-type c-type-align align>> ;
186 M: string c-type-align c-type c-type-align ;
188 GENERIC: c-type-stack-align? ( name -- ? )
190 M: c-type c-type-stack-align? stack-align?>> ;
192 M: string c-type-stack-align? c-type c-type-stack-align? ;
194 : c-type-box ( n type -- )
195 [ c-type-rep ] [ c-type-boxer [ "No boxer" throw ] unless* ] bi
198 : c-type-unbox ( n ctype -- )
199 [ c-type-rep ] [ c-type-unboxer [ "No unboxer" throw ] unless* ] bi
202 GENERIC: box-parameter ( n ctype -- )
204 M: c-type box-parameter c-type-box ;
206 M: string box-parameter c-type box-parameter ;
208 GENERIC: box-return ( ctype -- )
210 M: c-type box-return f swap c-type-box ;
212 M: string box-return c-type box-return ;
214 GENERIC: unbox-parameter ( n ctype -- )
216 M: c-type unbox-parameter c-type-unbox ;
218 M: string unbox-parameter c-type unbox-parameter ;
220 GENERIC: unbox-return ( ctype -- )
222 M: c-type unbox-return f swap c-type-unbox ;
224 M: string unbox-return c-type unbox-return ;
226 ! These words being foldable means that words need to be
227 ! recompiled if a C type is redefined. Even so, folding the
228 ! size facilitates some optimizations.
229 GENERIC: heap-size ( type -- size ) foldable
231 M: string heap-size c-type heap-size ;
233 M: abstract-c-type heap-size size>> ;
235 GENERIC: stack-size ( type -- size ) foldable
237 M: string stack-size c-type stack-size ;
239 M: c-type stack-size size>> cell align ;
241 GENERIC: byte-length ( seq -- n ) flushable
243 M: byte-array byte-length length ;
245 M: f byte-length drop 0 ;
247 : c-getter ( name -- quot )
249 [ "Cannot read struct fields with this type" throw ]
252 : c-type-getter-boxer ( name -- quot )
253 [ c-getter ] [ c-type-boxer-quot ] bi append ;
255 : c-setter ( name -- quot )
257 [ "Cannot write struct fields with this type" throw ]
260 : <c-array> ( n type -- array )
261 heap-size * <byte-array> ; inline
263 : <c-object> ( type -- array )
264 1 swap <c-array> ; inline
266 : malloc-array ( n type -- alien )
267 heap-size calloc ; inline
269 : malloc-object ( type -- alien )
270 1 swap malloc-array ; inline
272 : malloc-byte-array ( byte-array -- alien )
273 dup byte-length [ nip malloc dup ] 2keep memcpy ;
275 : memory>byte-array ( alien len -- byte-array )
276 [ nip (byte-array) dup ] 2keep memcpy ;
278 : malloc-string ( string encoding -- alien )
279 string>alien malloc-byte-array ;
281 M: memory-stream stream-read
283 [ index>> ] [ alien>> ] bi <displaced-alien>
284 swap memory>byte-array
285 ] [ [ + ] change-index drop ] 2bi ;
287 : byte-array>memory ( byte-array base -- )
288 swap dup byte-length memcpy ;
290 : array-accessor ( type quot -- def )
292 \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi*
295 : typedef ( old new -- ) c-types get set-at ;
297 TUPLE: long-long-type < c-type ;
299 : <long-long-type> ( -- type )
302 M: long-long-type unbox-parameter ( n type -- )
303 c-type-unboxer %unbox-long-long ;
305 M: long-long-type unbox-return ( type -- )
306 f swap unbox-parameter ;
308 M: long-long-type box-parameter ( n type -- )
309 c-type-boxer %box-long-long ;
311 M: long-long-type box-return ( type -- )
312 f swap box-parameter ;
314 : define-deref ( name -- )
315 [ CHAR: * prefix "alien.c-types" create ] [ c-getter 0 prefix ] bi
316 (( c-ptr -- value )) define-inline ;
318 : define-out ( name -- )
319 [ "alien.c-types" constructor-word ]
320 [ dup c-setter '[ _ <c-object> [ 0 @ ] keep ] ] bi
321 (( value -- c-ptr )) define-inline ;
323 : >c-bool ( ? -- int ) 1 0 ? ; inline
325 : c-bool> ( int -- ? ) 0 = not ; inline
327 : define-primitive-type ( type name -- )
333 : expand-constants ( c-type -- c-type' )
338 def>> call( -- object )
344 : malloc-file-contents ( path -- alien len )
345 binary file-contents [ malloc-byte-array ] [ length ] bi ;
347 : if-void ( type true false -- )
348 pick "void" = [ drop nip call ] [ nip call ] if ; inline
350 : ?lookup ( vocab word -- word/pair )
351 over vocab [ swap lookup ] [ 2array ] if ;
353 : set-array-class* ( c-type vocab-stem type-stem -- c-type )
356 [ "specialized-arrays." prepend ]
357 [ "-array" append ] bi* ?lookup >>array-class
360 [ "specialized-arrays." prepend ]
361 [ "<" "-array>" surround ] bi* ?lookup >>array-constructor
364 [ "specialized-arrays." prepend ]
365 [ "-sequence" append ] bi* ?lookup >>sequence-mixin-class
368 [ "specialized-arrays.direct." prepend ]
369 [ "direct-" "-array" surround ] bi* ?lookup >>direct-array-class
372 [ "specialized-arrays.direct." prepend ]
373 [ "<direct-" "-array>" surround ] bi* ?lookup >>direct-array-constructor
377 : set-array-class ( c-type stem -- c-type )
378 dup set-array-class* ;
380 CONSTANT: primitive-types
386 "longlong" "ulonglong"
395 [ alien-cell ] >>getter
396 [ [ >c-ptr ] 2dip set-alien-cell ] >>setter
397 bootstrap-cell >>size
398 bootstrap-cell >>align
399 [ >c-ptr ] >>unboxer-quot
401 "alien_offset" >>unboxer
402 "alien" "void*" set-array-class*
403 "void*" define-primitive-type
407 integer >>boxed-class
408 [ alien-signed-8 ] >>getter
409 [ set-alien-signed-8 ] >>setter
412 "box_signed_8" >>boxer
413 "to_signed_8" >>unboxer
414 "longlong" set-array-class
415 "longlong" define-primitive-type
419 integer >>boxed-class
420 [ alien-unsigned-8 ] >>getter
421 [ set-alien-unsigned-8 ] >>setter
424 "box_unsigned_8" >>boxer
425 "to_unsigned_8" >>unboxer
426 "ulonglong" set-array-class
427 "ulonglong" define-primitive-type
431 integer >>boxed-class
432 [ alien-signed-cell ] >>getter
433 [ set-alien-signed-cell ] >>setter
434 bootstrap-cell >>size
435 bootstrap-cell >>align
436 "box_signed_cell" >>boxer
437 "to_fixnum" >>unboxer
438 "long" set-array-class
439 "long" define-primitive-type
443 integer >>boxed-class
444 [ alien-unsigned-cell ] >>getter
445 [ set-alien-unsigned-cell ] >>setter
446 bootstrap-cell >>size
447 bootstrap-cell >>align
448 "box_unsigned_cell" >>boxer
450 "ulong" set-array-class
451 "ulong" define-primitive-type
455 integer >>boxed-class
456 [ alien-signed-4 ] >>getter
457 [ set-alien-signed-4 ] >>setter
460 "box_signed_4" >>boxer
461 "to_fixnum" >>unboxer
462 "int" set-array-class
463 "int" define-primitive-type
467 integer >>boxed-class
468 [ alien-unsigned-4 ] >>getter
469 [ set-alien-unsigned-4 ] >>setter
472 "box_unsigned_4" >>boxer
474 "uint" set-array-class
475 "uint" define-primitive-type
480 [ alien-signed-2 ] >>getter
481 [ set-alien-signed-2 ] >>setter
484 "box_signed_2" >>boxer
485 "to_fixnum" >>unboxer
486 "short" set-array-class
487 "short" define-primitive-type
492 [ alien-unsigned-2 ] >>getter
493 [ set-alien-unsigned-2 ] >>setter
496 "box_unsigned_2" >>boxer
498 "ushort" set-array-class
499 "ushort" define-primitive-type
504 [ alien-signed-1 ] >>getter
505 [ set-alien-signed-1 ] >>setter
508 "box_signed_1" >>boxer
509 "to_fixnum" >>unboxer
510 "char" set-array-class
511 "char" define-primitive-type
516 [ alien-unsigned-1 ] >>getter
517 [ set-alien-unsigned-1 ] >>setter
520 "box_unsigned_1" >>boxer
522 "uchar" set-array-class
523 "uchar" define-primitive-type
526 [ alien-unsigned-1 c-bool> ] >>getter
527 [ [ >c-bool ] 2dip set-alien-unsigned-1 ] >>setter
530 "box_boolean" >>boxer
531 "to_boolean" >>unboxer
532 "bool" set-array-class
533 "bool" define-primitive-type
538 [ alien-float ] >>getter
539 [ [ >float ] 2dip set-alien-float ] >>setter
544 single-float-rep >>rep
545 [ >float ] >>unboxer-quot
546 "float" set-array-class
547 "float" define-primitive-type
552 [ alien-double ] >>getter
553 [ [ >float ] 2dip set-alien-double ] >>setter
557 "to_double" >>unboxer
558 double-float-rep >>rep
559 [ >float ] >>unboxer-quot
560 "double" set-array-class
561 "double" define-primitive-type
563 "long" "ptrdiff_t" typedef
564 "long" "intptr_t" typedef
565 "ulong" "size_t" typedef
566 ] with-compilation-unit