factor/basis/alien/structs/structs.factor

84 lines
2.2 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.
2009-02-05 19:51:50 -05:00
USING: accessors arrays assocs 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 math.order
quotations ;
2007-09-20 18:09:08 -04:00
IN: alien.structs
TUPLE: struct-type
size
align
fields
{ boxer-quot callable }
{ unboxer-quot callable }
{ getter callable }
{ setter callable } ;
2007-09-20 18:09:08 -04:00
M: struct-type heap-size size>> ;
2007-09-20 18:09:08 -04:00
2008-11-29 05:59:29 -05:00
M: struct-type c-type-class drop object ;
M: struct-type c-type-align align>> ;
2007-09-20 18:09:08 -04:00
M: struct-type c-type-stack-align? drop f ;
M: struct-type c-type-boxer-quot boxer-quot>> ;
M: struct-type c-type-unboxer-quot unboxer-quot>> ;
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? ;
2008-11-14 21:18:16 -05:00
: (define-struct) ( name size align fields -- )
2008-11-29 14:37:38 -05:00
[ [ align ] keep ] dip
struct-type new
swap >>fields
swap >>align
swap >>size
2008-11-14 21:18:16 -05:00
swap typedef ;
2007-09-20 18:09:08 -04:00
2008-12-02 01:24:00 -05:00
: make-fields ( 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 ] [ max ] map-reduce ;
2007-09-20 18:09:08 -04:00
: define-struct ( name vocab fields -- )
2008-12-02 01:24:00 -05:00
[
[ 2drop ] [ make-fields ] 3bi
2008-11-29 14:37:38 -05:00
[ struct-offsets ] keep
[ [ type>> ] map compute-struct-align ] keep
[ (define-struct) ] keep
2008-12-02 01:24:00 -05:00
] [ 2drop '[ _ swap define-field ] ] 3bi each ;
2007-09-20 18:09:08 -04:00
2008-12-02 01:24:00 -05:00
: define-union ( name members -- )
2007-09-20 18:09:08 -04:00
[ expand-constants ] map
[ [ heap-size ] [ max ] map-reduce ] keep
2007-09-20 18:09:08 -04:00
compute-struct-align f (define-struct) ;
2009-02-05 19:51:50 -05:00
: offset-of ( field struct -- offset )
c-types get at fields>>
[ name>> = ] with find nip offset>> ;