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 ;
83 INSTANCE: c-type array-c-type
85 GENERIC: require-c-type-arrays ( c-type -- )
87 M: object require-c-type-arrays
90 M: array-c-type require-c-type-arrays
91 [ array-class>> ?require-word ]
92 [ sequence-mixin-class>> ?require-word ]
93 [ direct-array-class>> ?require-word ] tri ;
95 M: string require-c-type-arrays
96 c-type require-c-type-arrays ;
98 M: array require-c-type-arrays
99 first c-type require-c-type-arrays ;
101 ERROR: specialized-array-vocab-not-loaded vocab word ;
103 GENERIC: c-type-array-constructor ( c-type -- word ) foldable
105 M: string c-type-array-constructor
106 c-type c-type-array-constructor ;
107 M: array c-type-array-constructor
108 first c-type c-type-array-constructor ;
109 M: array-c-type c-type-array-constructor
110 array-constructor>> dup word?
111 [ first2 specialized-array-vocab-not-loaded ] unless ;
113 GENERIC: c-type-direct-array-constructor ( c-type -- word ) foldable
115 M: string c-type-direct-array-constructor
116 c-type c-type-direct-array-constructor ;
117 M: array c-type-direct-array-constructor
118 first c-type c-type-direct-array-constructor ;
119 M: array-c-type c-type-direct-array-constructor
120 direct-array-constructor>> dup word?
121 [ first2 specialized-array-vocab-not-loaded ] unless ;
123 GENERIC: <c-type-array> ( len c-type -- array )
124 M: object <c-type-array>
125 c-type-array-constructor execute( len -- array ) ; inline
127 GENERIC: <c-type-direct-array> ( alien len c-type -- array )
128 M: object <c-type-direct-array>
129 c-type-direct-array-constructor execute( alien len -- array ) ; inline
131 GENERIC: c-type-class ( name -- class )
133 M: abstract-c-type c-type-class class>> ;
135 M: string c-type-class c-type c-type-class ;
137 GENERIC: c-type-boxed-class ( name -- class )
139 M: abstract-c-type c-type-boxed-class boxed-class>> ;
141 M: string c-type-boxed-class c-type c-type-boxed-class ;
143 GENERIC: c-type-boxer ( name -- boxer )
145 M: c-type c-type-boxer boxer>> ;
147 M: string c-type-boxer c-type c-type-boxer ;
149 GENERIC: c-type-boxer-quot ( name -- quot )
151 M: abstract-c-type c-type-boxer-quot boxer-quot>> ;
153 M: string c-type-boxer-quot c-type c-type-boxer-quot ;
155 GENERIC: c-type-unboxer ( name -- boxer )
157 M: c-type c-type-unboxer unboxer>> ;
159 M: string c-type-unboxer c-type c-type-unboxer ;
161 GENERIC: c-type-unboxer-quot ( name -- quot )
163 M: abstract-c-type c-type-unboxer-quot unboxer-quot>> ;
165 M: string c-type-unboxer-quot c-type c-type-unboxer-quot ;
167 GENERIC: c-type-rep ( name -- rep )
169 M: c-type c-type-rep rep>> ;
171 M: string c-type-rep c-type c-type-rep ;
173 GENERIC: c-type-getter ( name -- quot )
175 M: c-type c-type-getter getter>> ;
177 M: string c-type-getter c-type c-type-getter ;
179 GENERIC: c-type-setter ( name -- quot )
181 M: c-type c-type-setter setter>> ;
183 M: string c-type-setter c-type c-type-setter ;
185 GENERIC: c-type-align ( name -- n )
187 M: abstract-c-type c-type-align align>> ;
189 M: string c-type-align c-type c-type-align ;
191 GENERIC: c-type-stack-align? ( name -- ? )
193 M: c-type c-type-stack-align? stack-align?>> ;
195 M: string c-type-stack-align? c-type c-type-stack-align? ;
197 : c-type-box ( n type -- )
198 [ c-type-rep ] [ c-type-boxer [ "No boxer" throw ] unless* ] bi
201 : c-type-unbox ( n ctype -- )
202 [ c-type-rep ] [ c-type-unboxer [ "No unboxer" throw ] unless* ] bi
205 GENERIC: box-parameter ( n ctype -- )
207 M: c-type box-parameter c-type-box ;
209 M: string box-parameter c-type box-parameter ;
211 GENERIC: box-return ( ctype -- )
213 M: c-type box-return f swap c-type-box ;
215 M: string box-return c-type box-return ;
217 GENERIC: unbox-parameter ( n ctype -- )
219 M: c-type unbox-parameter c-type-unbox ;
221 M: string unbox-parameter c-type unbox-parameter ;
223 GENERIC: unbox-return ( ctype -- )
225 M: c-type unbox-return f swap c-type-unbox ;
227 M: string unbox-return c-type unbox-return ;
229 ! These words being foldable means that words need to be
230 ! recompiled if a C type is redefined. Even so, folding the
231 ! size facilitates some optimizations.
232 GENERIC: heap-size ( type -- size ) foldable
234 M: string heap-size c-type heap-size ;
236 M: abstract-c-type heap-size size>> ;
238 GENERIC: stack-size ( type -- size ) foldable
240 M: string stack-size c-type stack-size ;
242 M: c-type stack-size size>> cell align ;
244 GENERIC: byte-length ( seq -- n ) flushable
246 M: byte-array byte-length length ;
248 M: f byte-length drop 0 ;
250 : c-getter ( name -- quot )
252 [ "Cannot read struct fields with this type" throw ]
255 : c-type-getter-boxer ( name -- quot )
256 [ c-getter ] [ c-type-boxer-quot ] bi append ;
258 : c-setter ( name -- quot )
260 [ "Cannot write struct fields with this type" throw ]
263 : <c-array> ( n type -- array )
264 heap-size * <byte-array> ; inline
266 : <c-object> ( type -- array )
267 1 swap <c-array> ; inline
269 : malloc-array ( n type -- alien )
270 heap-size calloc ; inline
272 : malloc-object ( type -- alien )
273 1 swap malloc-array ; inline
275 : malloc-byte-array ( byte-array -- alien )
276 dup byte-length [ nip malloc dup ] 2keep memcpy ;
278 : memory>byte-array ( alien len -- byte-array )
279 [ nip (byte-array) dup ] 2keep memcpy ;
281 : malloc-string ( string encoding -- alien )
282 string>alien malloc-byte-array ;
284 M: memory-stream stream-read
286 [ index>> ] [ alien>> ] bi <displaced-alien>
287 swap memory>byte-array
288 ] [ [ + ] change-index drop ] 2bi ;
290 : byte-array>memory ( byte-array base -- )
291 swap dup byte-length memcpy ;
293 : array-accessor ( type quot -- def )
295 \ swap , [ heap-size , [ * >fixnum ] % ] [ % ] bi*
298 : typedef ( old new -- ) c-types get set-at ;
300 TUPLE: long-long-type < c-type ;
302 : <long-long-type> ( -- type )
305 M: long-long-type unbox-parameter ( n type -- )
306 c-type-unboxer %unbox-long-long ;
308 M: long-long-type unbox-return ( type -- )
309 f swap unbox-parameter ;
311 M: long-long-type box-parameter ( n type -- )
312 c-type-boxer %box-long-long ;
314 M: long-long-type box-return ( type -- )
315 f swap box-parameter ;
317 : define-deref ( name -- )
318 [ CHAR: * prefix "alien.c-types" create ] [ c-getter 0 prefix ] bi
319 (( c-ptr -- value )) define-inline ;
321 : define-out ( name -- )
322 [ "alien.c-types" constructor-word ]
323 [ dup c-setter '[ _ <c-object> [ 0 @ ] keep ] ] bi
324 (( value -- c-ptr )) define-inline ;
326 : >c-bool ( ? -- int ) 1 0 ? ; inline
328 : c-bool> ( int -- ? ) 0 = not ; inline
330 : define-primitive-type ( type name -- )
336 : expand-constants ( c-type -- c-type' )
341 def>> call( -- object )
347 : malloc-file-contents ( path -- alien len )
348 binary file-contents [ malloc-byte-array ] [ length ] bi ;
350 : if-void ( type true false -- )
351 pick "void" = [ drop nip call ] [ nip call ] if ; inline
353 : ?lookup ( vocab word -- word/pair )
354 over vocab [ swap lookup ] [ 2array ] if ;
356 : set-array-class* ( c-type vocab-stem type-stem -- c-type )
359 [ "specialized-arrays." prepend ]
360 [ "-array" append ] bi* ?lookup >>array-class
363 [ "specialized-arrays." prepend ]
364 [ "<" "-array>" surround ] bi* ?lookup >>array-constructor
367 [ "specialized-arrays." prepend ]
368 [ "-sequence" append ] bi* ?lookup >>sequence-mixin-class
371 [ "specialized-arrays.direct." prepend ]
372 [ "direct-" "-array" surround ] bi* ?lookup >>direct-array-class
375 [ "specialized-arrays.direct." prepend ]
376 [ "<direct-" "-array>" surround ] bi* ?lookup >>direct-array-constructor
380 : set-array-class ( c-type stem -- c-type )
381 dup set-array-class* ;
383 CONSTANT: primitive-types
389 "longlong" "ulonglong"
398 [ alien-cell ] >>getter
399 [ [ >c-ptr ] 2dip set-alien-cell ] >>setter
400 bootstrap-cell >>size
401 bootstrap-cell >>align
402 [ >c-ptr ] >>unboxer-quot
404 "alien_offset" >>unboxer
405 "alien" "void*" set-array-class*
406 "void*" define-primitive-type
410 integer >>boxed-class
411 [ alien-signed-8 ] >>getter
412 [ set-alien-signed-8 ] >>setter
415 "box_signed_8" >>boxer
416 "to_signed_8" >>unboxer
417 "longlong" set-array-class
418 "longlong" define-primitive-type
422 integer >>boxed-class
423 [ alien-unsigned-8 ] >>getter
424 [ set-alien-unsigned-8 ] >>setter
427 "box_unsigned_8" >>boxer
428 "to_unsigned_8" >>unboxer
429 "ulonglong" set-array-class
430 "ulonglong" define-primitive-type
434 integer >>boxed-class
435 [ alien-signed-cell ] >>getter
436 [ set-alien-signed-cell ] >>setter
437 bootstrap-cell >>size
438 bootstrap-cell >>align
439 "box_signed_cell" >>boxer
440 "to_fixnum" >>unboxer
441 "long" set-array-class
442 "long" define-primitive-type
446 integer >>boxed-class
447 [ alien-unsigned-cell ] >>getter
448 [ set-alien-unsigned-cell ] >>setter
449 bootstrap-cell >>size
450 bootstrap-cell >>align
451 "box_unsigned_cell" >>boxer
453 "ulong" set-array-class
454 "ulong" define-primitive-type
458 integer >>boxed-class
459 [ alien-signed-4 ] >>getter
460 [ set-alien-signed-4 ] >>setter
463 "box_signed_4" >>boxer
464 "to_fixnum" >>unboxer
465 "int" set-array-class
466 "int" define-primitive-type
470 integer >>boxed-class
471 [ alien-unsigned-4 ] >>getter
472 [ set-alien-unsigned-4 ] >>setter
475 "box_unsigned_4" >>boxer
477 "uint" set-array-class
478 "uint" define-primitive-type
483 [ alien-signed-2 ] >>getter
484 [ set-alien-signed-2 ] >>setter
487 "box_signed_2" >>boxer
488 "to_fixnum" >>unboxer
489 "short" set-array-class
490 "short" define-primitive-type
495 [ alien-unsigned-2 ] >>getter
496 [ set-alien-unsigned-2 ] >>setter
499 "box_unsigned_2" >>boxer
501 "ushort" set-array-class
502 "ushort" define-primitive-type
507 [ alien-signed-1 ] >>getter
508 [ set-alien-signed-1 ] >>setter
511 "box_signed_1" >>boxer
512 "to_fixnum" >>unboxer
513 "char" set-array-class
514 "char" define-primitive-type
519 [ alien-unsigned-1 ] >>getter
520 [ set-alien-unsigned-1 ] >>setter
523 "box_unsigned_1" >>boxer
525 "uchar" set-array-class
526 "uchar" define-primitive-type
529 [ alien-unsigned-1 c-bool> ] >>getter
530 [ [ >c-bool ] 2dip set-alien-unsigned-1 ] >>setter
533 "box_boolean" >>boxer
534 "to_boolean" >>unboxer
535 "bool" set-array-class
536 "bool" define-primitive-type
541 [ alien-float ] >>getter
542 [ [ >float ] 2dip set-alien-float ] >>setter
547 single-float-rep >>rep
548 [ >float ] >>unboxer-quot
549 "float" set-array-class
550 "float" define-primitive-type
555 [ alien-double ] >>getter
556 [ [ >float ] 2dip set-alien-double ] >>setter
560 "to_double" >>unboxer
561 double-float-rep >>rep
562 [ >float ] >>unboxer-quot
563 "double" set-array-class
564 "double" define-primitive-type
566 "long" "ptrdiff_t" typedef
567 "long" "intptr_t" typedef
568 "ulong" "size_t" typedef
569 ] with-compilation-unit