factor/basis/alien/structs/structs.factor

61 lines
1.8 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays generic hashtables kernel kernel.private
2008-11-17 14:34:37 -05:00
math namespaces parser sequences strings words libc fry
alien.c-types alien.structs.fields cpu.architecture ;
2007-09-20 18:09:08 -04:00
IN: alien.structs
TUPLE: struct-type size align fields ;
M: struct-type heap-size size>> ;
2007-09-20 18:09:08 -04:00
M: struct-type c-type-align align>> ;
2007-09-20 18:09:08 -04:00
M: struct-type c-type-stack-align? drop f ;
2008-11-17 14:34:37 -05:00
: if-value-struct ( ctype true false -- )
[ dup value-struct? ] 2dip '[ drop "void*" @ ] if ; inline
2007-09-20 18:09:08 -04:00
2008-11-17 14:34:37 -05:00
M: struct-type unbox-parameter
[ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
2007-09-20 18:09:08 -04:00
M: struct-type box-parameter
2008-11-17 14:34:37 -05:00
[ %box-large-struct ] [ box-parameter ] if-value-struct ;
: if-small-struct ( c-type true false -- ? )
[ dup struct-small-enough? ] 2dip '[ f swap @ ] if ; inline
M: struct-type unbox-return
[ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
2007-09-20 18:09:08 -04:00
M: struct-type box-return
2008-11-17 14:34:37 -05:00
[ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
2007-09-20 18:09:08 -04:00
M: struct-type stack-size
2008-11-17 14:34:37 -05:00
[ heap-size ] [ stack-size ] if-value-struct ;
2007-09-20 18:09:08 -04:00
: c-struct? ( type -- ? ) (c-type) struct-type? ;
: (define-struct) ( name vocab size align fields -- )
>r [ align ] keep r>
struct-type boa
2007-09-20 18:09:08 -04:00
-rot define-c-type ;
: define-struct-early ( name vocab fields -- fields )
2008-11-17 14:34:37 -05:00
[ first2 <field-spec> ] with with map ;
2007-09-20 18:09:08 -04:00
: compute-struct-align ( types -- n )
[ c-type-align ] map supremum ;
: define-struct ( name vocab fields -- )
pick >r
[ struct-offsets ] keep
[ [ type>> ] map compute-struct-align ] keep
2007-09-20 18:09:08 -04:00
[ (define-struct) ] keep
r> [ swap define-field ] curry each ;
: define-union ( name vocab members -- )
[ expand-constants ] map
[ [ heap-size ] map supremum ] keep
compute-struct-align f (define-struct) ;