]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/structs/structs.factor
Specialized array overhaul
[factor.git] / basis / alien / structs / structs.factor
1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs generic hashtables kernel kernel.private
4 math namespaces parser sequences strings words libc fry
5 alien.c-types alien.structs.fields cpu.architecture math.order
6 quotations byte-arrays ;
7 IN: alien.structs
8
9 TUPLE: struct-type < abstract-c-type fields return-in-registers? ;
10
11 M: struct-type c-type ;
12
13 M: struct-type c-type-stack-align? drop f ;
14
15 : if-value-struct ( ctype true false -- )
16     [ dup value-struct? ] 2dip '[ drop "void*" @ ] if ; inline
17
18 M: struct-type unbox-parameter
19     [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
20
21 M: struct-type box-parameter
22     [ %box-large-struct ] [ box-parameter ] if-value-struct ;
23
24 : if-small-struct ( c-type true false -- ? )
25     [ dup return-struct-in-registers? ] 2dip '[ f swap @ ] if ; inline
26
27 M: struct-type unbox-return
28     [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
29
30 M: struct-type box-return
31     [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
32
33 M: struct-type stack-size
34     [ heap-size ] [ stack-size ] if-value-struct ;
35
36 : c-struct? ( type -- ? ) (c-type) struct-type? ;
37
38 : (define-struct) ( name size align fields class -- )
39     [ [ align ] keep ] 2dip new
40         byte-array >>class
41         byte-array >>boxed-class
42         swap >>fields
43         swap >>align
44         swap >>size
45         swap typedef ;
46
47 : make-fields ( name vocab fields -- fields )
48     [ first2 <field-spec> ] with with map ;
49
50 : compute-struct-align ( types -- n )
51     [ c-type-align ] [ max ] map-reduce ;
52
53 : define-struct ( name vocab fields -- )
54     [ 2drop ] [ make-fields ] 3bi
55     [ struct-offsets ] keep
56     [ [ type>> ] map compute-struct-align ] keep
57     [ struct-type (define-struct) ] keep
58     [ define-field ] each ; deprecated
59
60 : define-union ( name members -- )
61     [ [ heap-size ] [ max ] map-reduce ] keep
62     compute-struct-align f struct-type (define-struct) ; deprecated
63
64 : offset-of ( field struct -- offset )
65     c-types get at fields>> 
66     [ name>> = ] with find nip offset>> ;
67
68 USE: vocabs.loader
69 "specialized-arrays" require