1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays generic hashtables kernel kernel.private
4 math namespaces parser sequences strings words libc fry
5 alien.c-types alien.structs.fields cpu.architecture ;
8 TUPLE: struct-type size align fields ;
10 M: struct-type heap-size size>> ;
12 M: struct-type c-type-class drop object ;
14 M: struct-type c-type-align align>> ;
16 M: struct-type c-type-stack-align? drop f ;
18 : if-value-struct ( ctype true false -- )
19 [ dup value-struct? ] 2dip '[ drop "void*" @ ] if ; inline
21 M: struct-type unbox-parameter
22 [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
24 M: struct-type box-parameter
25 [ %box-large-struct ] [ box-parameter ] if-value-struct ;
27 : if-small-struct ( c-type true false -- ? )
28 [ dup struct-small-enough? ] 2dip '[ f swap @ ] if ; inline
30 M: struct-type unbox-return
31 [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
33 M: struct-type box-return
34 [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
36 M: struct-type stack-size
37 [ heap-size ] [ stack-size ] if-value-struct ;
39 : c-struct? ( type -- ? ) (c-type) struct-type? ;
41 : (define-struct) ( name size align fields -- )
42 [ [ align ] keep ] dip
46 : define-struct-early ( name vocab fields -- fields )
47 [ first2 <field-spec> ] with with map ;
49 : compute-struct-align ( types -- n )
50 [ c-type-align ] map supremum ;
52 : define-struct ( name vocab fields -- )
54 [ struct-offsets ] keep
55 [ [ type>> ] map compute-struct-align ] keep
56 [ (define-struct) ] keep
57 ] dip [ swap define-field ] curry each ;
59 : define-union ( name vocab members -- )
60 [ expand-constants ] map
61 [ [ heap-size ] map supremum ] keep
62 compute-struct-align f (define-struct) ;