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 GENERIC: c-type-array-constructor ( c-type -- word ) foldable
100 M: string c-type-array-constructor
101 c-type c-type-array-constructor ;
102 M: array c-type-array-constructor
103 first c-type c-type-array-constructor ;
104 M: c-type c-type-array-constructor
105 array-constructor>> ;
107 GENERIC: c-type-direct-array-constructor ( c-type -- word ) foldable
109 M: string c-type-direct-array-constructor
110 c-type c-type-array-constructor ;
111 M: array c-type-direct-array-constructor
112 first c-type c-type-direct-array-constructor ;
113 M: c-type c-type-direct-array-constructor
114 direct-array-constructor>> ;
116 : <c-type-array> ( len c-type -- array )
117 c-type-array-constructor execute( len -- array ) ; inline
118 : <c-type-direct-array> ( len c-type -- array )
119 c-type-direct-array-constructor execute( len -- array ) ; inline
121 GENERIC: c-type-class ( name -- class )
123 M: abstract-c-type c-type-class class>> ;
125 M: string c-type-class c-type c-type-class ;
127 GENERIC: c-type-boxed-class ( name -- class )
129 M: abstract-c-type c-type-boxed-class boxed-class>> ;
131 M: string c-type-boxed-class c-type c-type-boxed-class ;
133 GENERIC: c-type-boxer ( name -- boxer )
135 M: c-type c-type-boxer boxer>> ;
137 M: string c-type-boxer c-type c-type-boxer ;
139 GENERIC: c-type-boxer-quot ( name -- quot )
141 M: abstract-c-type c-type-boxer-quot boxer-quot>> ;
143 M: string c-type-boxer-quot c-type c-type-boxer-quot ;
145 GENERIC: c-type-unboxer ( name -- boxer )
147 M: c-type c-type-unboxer unboxer>> ;
149 M: string c-type-unboxer c-type c-type-unboxer ;
151 GENERIC: c-type-unboxer-quot ( name -- quot )
153 M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ;
155 M: string c-type-unboxer-quot c-type c-type-unboxer-quot ;
157 GENERIC: c-type-rep ( name -- rep )
159 M: c-type c-type-rep rep>> ;
161 M: string c-type-rep c-type c-type-rep ;
163 GENERIC: c-type-getter ( name -- quot )
165 M: c-type c-type-getter getter>> ;
167 M: string c-type-getter c-type c-type-getter ;
169 GENERIC: c-type-setter ( name -- quot )
171 M: c-type c-type-setter setter>> ;
173 M: string c-type-setter c-type c-type-setter ;
175 GENERIC: c-type-align ( name -- n )
177 M: abstract-c-type c-type-align align>> ;
179 M: string c-type-align c-type c-type-align ;
181 GENERIC: c-type-stack-align? ( name -- ? )
183 M: c-type c-type-stack-align? stack-align?>> ;
185 M: string c-type-stack-align? c-type c-type-stack-align? ;
187 : c-type-box ( n type -- )
188 [ c-type-rep ] [ c-type-boxer [ "No boxer" throw ] unless* ] bi
191 : c-type-unbox ( n ctype -- )
192 [ c-type-rep ] [ c-type-unboxer [ "No unboxer" throw ] unless* ] bi
195 GENERIC: box-parameter ( n ctype -- )
197 M: c-type box-parameter c-type-box ;
199 M: string box-parameter c-type box-parameter ;
201 GENERIC: box-return ( ctype -- )
203 M: c-type box-return f swap c-type-box ;
205 M: string box-return c-type box-return ;
207 GENERIC: unbox-parameter ( n ctype -- )
209 M: c-type unbox-parameter c-type-unbox ;
211 M: string unbox-parameter c-type unbox-parameter ;
213 GENERIC: unbox-return ( ctype -- )
215 M: c-type unbox-return f swap c-type-unbox ;
217 M: string unbox-return c-type unbox-return ;
219 ! These words being foldable means that words need to be
220 ! recompiled if a C type is redefined. Even so, folding the
221 ! size facilitates some optimizations.
222 GENERIC: heap-size ( type -- size ) foldable
224 M: string heap-size c-type heap-size ;
226 M: abstract-c-type heap-size size>> ;
228 GENERIC: stack-size ( type -- size ) foldable
230 M: string stack-size c-type stack-size ;
232 M: c-type stack-size size>> cell align ;
234 GENERIC: byte-length ( seq -- n ) flushable
236 M: byte-array byte-length length ;
238 M: f byte-length drop 0 ;
240 : c-getter ( name -- quot )
242 [ "Cannot read struct fields with this type" throw ]
245 : c-type-getter-boxer ( name -- quot )
246 [ c-getter ] [ c-type-boxer-quot ] bi append ;
248 : c-setter ( name -- quot )
250 [ "Cannot write struct fields with this type" throw ]
253 : <c-array> ( n type -- array )
254 heap-size * <byte-array> ; inline
256 : <c-object> ( type -- array )
257 1 swap <c-array> ; inline
259 : malloc-array ( n type -- alien )
260 heap-size calloc ; inline
262 : malloc-object ( type -- alien )
263 1 swap malloc-array ; inline
265 : malloc-byte-array ( byte-array -- alien )
266 dup byte-length [ nip malloc dup ] 2keep memcpy ;
268 : memory>byte-array ( alien len -- byte-array )
269 [ nip (byte-array) dup ] 2keep memcpy ;
271 : malloc-string ( string encoding -- alien )
272 string>alien malloc-byte-array ;
274 M: memory-stream stream-read
276 [ index>> ] [ alien>> ] bi <displaced-alien>
277 swap memory>byte-array
278 ] [ [ + ] change-index drop ] 2bi ;
280 : byte-array>memory ( byte-array base -- )
281 swap dup byte-length memcpy ;
283 : array-accessor ( type quot -- def )
285 \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi*
288 : typedef ( old new -- ) c-types get set-at ;
290 TUPLE: long-long-type < c-type ;
292 : <long-long-type> ( -- type )
295 M: long-long-type unbox-parameter ( n type -- )
296 c-type-unboxer %unbox-long-long ;
298 M: long-long-type unbox-return ( type -- )
299 f swap unbox-parameter ;
301 M: long-long-type box-parameter ( n type -- )
302 c-type-boxer %box-long-long ;
304 M: long-long-type box-return ( type -- )
305 f swap box-parameter ;
307 : define-deref ( name -- )
308 [ CHAR: * prefix "alien.c-types" create ] [ c-getter 0 prefix ] bi
309 (( c-ptr -- value )) define-inline ;
311 : define-out ( name -- )
312 [ "alien.c-types" constructor-word ]
313 [ dup c-setter '[ _ <c-object> [ 0 @ ] keep ] ] bi
314 (( value -- c-ptr )) define-inline ;
316 : >c-bool ( ? -- int ) 1 0 ? ; inline
318 : c-bool> ( int -- ? ) 0 = not ; inline
320 : define-primitive-type ( type name -- )
326 : expand-constants ( c-type -- c-type' )
331 def>> call( -- object )
337 : malloc-file-contents ( path -- alien len )
338 binary file-contents [ malloc-byte-array ] [ length ] bi ;
340 : if-void ( type true false -- )
341 pick "void" = [ drop nip call ] [ nip call ] if ; inline
343 : ?lookup ( vocab word -- word/pair )
344 over vocab [ swap lookup ] [ 2array ] if ;
346 : set-array-class* ( c-type vocab-stem type-stem -- c-type )
349 [ "specialized-arrays." prepend ]
350 [ "-array" append ] bi* ?lookup >>array-class
353 [ "specialized-arrays." prepend ]
354 [ "<" "-array>" surround ] bi* ?lookup >>array-constructor
357 [ "specialized-arrays." prepend ]
358 [ "-sequence" append ] bi* ?lookup >>sequence-mixin-class
361 [ "specialized-arrays.direct." prepend ]
362 [ "direct-" "-array" surround ] bi* ?lookup >>direct-array-class
365 [ "specialized-arrays.direct." prepend ]
366 [ "<direct-" "-array>" surround ] bi* ?lookup >>direct-array-constructor
370 : set-array-class ( c-type stem -- c-type )
371 dup set-array-class* ;
373 CONSTANT: primitive-types
379 "longlong" "ulonglong"
388 [ alien-cell ] >>getter
389 [ [ >c-ptr ] 2dip set-alien-cell ] >>setter
390 bootstrap-cell >>size
391 bootstrap-cell >>align
392 [ >c-ptr ] >>unboxer-quot
394 "alien_offset" >>unboxer
395 "alien" "void*" set-array-class*
396 "void*" define-primitive-type
400 integer >>boxed-class
401 [ alien-signed-8 ] >>getter
402 [ set-alien-signed-8 ] >>setter
405 "box_signed_8" >>boxer
406 "to_signed_8" >>unboxer
407 "longlong" set-array-class
408 "longlong" define-primitive-type
412 integer >>boxed-class
413 [ alien-unsigned-8 ] >>getter
414 [ set-alien-unsigned-8 ] >>setter
417 "box_unsigned_8" >>boxer
418 "to_unsigned_8" >>unboxer
419 "ulonglong" set-array-class
420 "ulonglong" define-primitive-type
424 integer >>boxed-class
425 [ alien-signed-cell ] >>getter
426 [ set-alien-signed-cell ] >>setter
427 bootstrap-cell >>size
428 bootstrap-cell >>align
429 "box_signed_cell" >>boxer
430 "to_fixnum" >>unboxer
431 "long" set-array-class
432 "long" define-primitive-type
436 integer >>boxed-class
437 [ alien-unsigned-cell ] >>getter
438 [ set-alien-unsigned-cell ] >>setter
439 bootstrap-cell >>size
440 bootstrap-cell >>align
441 "box_unsigned_cell" >>boxer
443 "ulong" set-array-class
444 "ulong" define-primitive-type
448 integer >>boxed-class
449 [ alien-signed-4 ] >>getter
450 [ set-alien-signed-4 ] >>setter
453 "box_signed_4" >>boxer
454 "to_fixnum" >>unboxer
455 "int" set-array-class
456 "int" define-primitive-type
460 integer >>boxed-class
461 [ alien-unsigned-4 ] >>getter
462 [ set-alien-unsigned-4 ] >>setter
465 "box_unsigned_4" >>boxer
467 "uint" set-array-class
468 "uint" define-primitive-type
473 [ alien-signed-2 ] >>getter
474 [ set-alien-signed-2 ] >>setter
477 "box_signed_2" >>boxer
478 "to_fixnum" >>unboxer
479 "short" set-array-class
480 "short" define-primitive-type
485 [ alien-unsigned-2 ] >>getter
486 [ set-alien-unsigned-2 ] >>setter
489 "box_unsigned_2" >>boxer
491 "ushort" set-array-class
492 "ushort" define-primitive-type
497 [ alien-signed-1 ] >>getter
498 [ set-alien-signed-1 ] >>setter
501 "box_signed_1" >>boxer
502 "to_fixnum" >>unboxer
503 "char" set-array-class
504 "char" define-primitive-type
509 [ alien-unsigned-1 ] >>getter
510 [ set-alien-unsigned-1 ] >>setter
513 "box_unsigned_1" >>boxer
515 "uchar" set-array-class
516 "uchar" define-primitive-type
519 [ alien-unsigned-1 c-bool> ] >>getter
520 [ [ >c-bool ] 2dip set-alien-unsigned-1 ] >>setter
523 "box_boolean" >>boxer
524 "to_boolean" >>unboxer
525 "bool" set-array-class
526 "bool" define-primitive-type
531 [ alien-float ] >>getter
532 [ [ >float ] 2dip set-alien-float ] >>setter
537 single-float-rep >>rep
538 [ >float ] >>unboxer-quot
539 "float" set-array-class
540 "float" define-primitive-type
545 [ alien-double ] >>getter
546 [ [ >float ] 2dip set-alien-double ] >>setter
550 "to_double" >>unboxer
551 double-float-rep >>rep
552 [ >float ] >>unboxer-quot
553 "double" set-array-class
554 "double" define-primitive-type
556 "long" "ptrdiff_t" typedef
557 "long" "intptr_t" typedef
558 "ulong" "size_t" typedef
559 ] with-compilation-unit