]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct.factor
move some allocation words that don't really have much to do with c types out of...
[factor.git] / basis / classes / struct / struct.factor
1 ! (c)Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.data alien.parser arrays
3 byte-arrays classes classes.parser classes.tuple classes.tuple.parser
4 classes.tuple.private combinators combinators.short-circuit
5 combinators.smart cpu.architecture definitions functors.backend
6 fry generalizations generic.parser kernel kernel.private lexer
7 libc locals macros make math math.order parser quotations
8 sequences slots slots.private specialized-arrays vectors words
9 summary namespaces assocs vocabs.parser ;
10 IN: classes.struct
11
12 SPECIALIZED-ARRAY: uchar
13
14 ERROR: struct-must-have-slots ;
15
16 M: struct-must-have-slots summary
17     drop "Struct definitions must have slots" ;
18
19 TUPLE: struct
20     { (underlying) c-ptr read-only } ;
21
22 TUPLE: struct-slot-spec < slot-spec
23     type ;
24
25 PREDICATE: struct-class < tuple-class
26     superclass \ struct eq? ;
27
28 M: struct-class valid-superclass? drop f ;
29
30 GENERIC: struct-slots ( struct-class -- slots )
31
32 M: struct-class struct-slots "struct-slots" word-prop ;
33
34 ! struct allocation
35
36 M: struct >c-ptr
37     2 slot { c-ptr } declare ; inline
38
39 M: struct equal?
40     {
41         [ [ class ] bi@ = ]
42         [ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
43     } 2&& ; inline
44
45 M: struct hashcode*
46     [ >c-ptr ] [ byte-length ] bi <direct-uchar-array> hashcode* ; inline    
47
48 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
49
50 : memory>struct ( ptr class -- struct )
51     ! This is sub-optimal if the class is not literal, but gets
52     ! optimized down to efficient code if it is.
53     '[ _ boa ] call( ptr -- struct ) ; inline
54
55 <PRIVATE
56 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
57     '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
58 PRIVATE>
59
60 : (malloc-struct) ( class -- struct )
61     [ heap-size malloc ] keep memory>struct ; inline
62
63 : malloc-struct ( class -- struct )
64     [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
65
66 : (struct) ( class -- struct )
67     [ heap-size (byte-array) ] keep memory>struct ; inline
68
69 : <struct> ( class -- struct )
70     [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
71
72 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
73     [
74         [ <wrapper> \ (struct) [ ] 2sequence ]
75         [
76             struct-slots
77             [ length \ ndip ]
78             [ [ name>> setter-word 1quotation ] map \ spread ] bi
79         ] bi
80     ] [ ] output>sequence ;
81
82 <PRIVATE
83 : pad-struct-slots ( values class -- values' class )
84     [ struct-slots [ initial>> ] map over length tail append ] keep ;
85
86 : (reader-quot) ( slot -- quot )
87     [ type>> c-type-getter-boxer ]
88     [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
89
90 : (writer-quot) ( slot -- quot )
91     [ type>> c-setter ]
92     [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
93
94 : (boxer-quot) ( class -- quot )
95     '[ _ memory>struct ] ;
96
97 : (unboxer-quot) ( class -- quot )
98     drop [ >c-ptr ] ;
99 PRIVATE>
100
101 M: struct-class boa>object
102     swap pad-struct-slots
103     [ <struct> ] [ struct-slots ] bi 
104     [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
105
106 ! Struct slot accessors
107
108 GENERIC: struct-slot-values ( struct -- sequence )
109
110 M: struct-class reader-quot
111     nip (reader-quot) ;
112
113 M: struct-class writer-quot
114     nip (writer-quot) ;
115
116 ! c-types
117
118 TUPLE: struct-c-type < abstract-c-type
119     fields
120     return-in-registers? ;
121
122 INSTANCE: struct-c-type value-type
123
124 M: struct-c-type c-type ;
125
126 M: struct-c-type c-type-stack-align? drop f ;
127
128 : if-value-struct ( ctype true false -- )
129     [ dup value-struct? ] 2dip '[ drop void* @ ] if ; inline
130
131 M: struct-c-type unbox-parameter
132     [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
133
134 M: struct-c-type box-parameter
135     [ %box-large-struct ] [ box-parameter ] if-value-struct ;
136
137 : if-small-struct ( c-type true false -- ? )
138     [ dup return-struct-in-registers? ] 2dip '[ f swap @ ] if ; inline
139
140 M: struct-c-type unbox-return
141     [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
142
143 M: struct-c-type box-return
144     [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
145
146 M: struct-c-type stack-size
147     [ heap-size ] [ stack-size ] if-value-struct ;
148
149 M: struct-c-type c-struct? drop t ;
150
151 <PRIVATE
152 : struct-slot-values-quot ( class -- quot )
153     struct-slots
154     [ name>> reader-word 1quotation ] map
155     \ cleave [ ] 2sequence
156     \ output>array [ ] 2sequence ;
157
158 : define-inline-method ( class generic quot -- )
159     [ create-method-in ] dip [ define ] [ drop make-inline ] 2bi ;
160
161 : (define-struct-slot-values-method) ( class -- )
162     [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
163     define-inline-method ;
164
165 : clone-underlying ( struct -- byte-array )
166     [ >c-ptr ] [ byte-length ] bi memory>byte-array ; inline
167
168 : (define-clone-method) ( class -- )
169     [ \ clone ]
170     [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
171     define-inline-method ;
172
173 : c-type-for-class ( class -- c-type )
174     struct-c-type new swap {
175         [ drop byte-array >>class ]
176         [ >>boxed-class ]
177         [ struct-slots >>fields ]
178         [ "struct-size" word-prop >>size ]
179         [ "struct-align" word-prop >>align ]
180         [ (unboxer-quot) >>unboxer-quot ]
181         [ (boxer-quot) >>boxer-quot ]
182     } cleave ;
183     
184 : align-offset ( offset class -- offset' )
185     c-type-align align ;
186
187 : struct-offsets ( slots -- size )
188     0 [
189         [ type>> align-offset ] keep
190         [ (>>offset) ] [ type>> heap-size + ] 2bi
191     ] reduce ;
192
193 : union-struct-offsets ( slots -- size )
194     [ 0 >>offset type>> heap-size ] [ max ] map-reduce ;
195
196 : struct-align ( slots -- align )
197     [ type>> c-type-align ] [ max ] map-reduce ;
198 PRIVATE>
199
200 M: struct byte-length class "struct-size" word-prop ; foldable
201
202 ! class definition
203
204 <PRIVATE
205 : make-struct-prototype ( class -- prototype )
206     [ "struct-size" word-prop <byte-array> ]
207     [ memory>struct ]
208     [ struct-slots ] tri
209     [
210         [ initial>> ]
211         [ (writer-quot) ] bi
212         over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
213     ] each ;
214
215 : (struct-methods) ( class -- )
216     [ (define-struct-slot-values-method) ]
217     [ (define-clone-method) ]
218     bi ;
219
220 : (struct-word-props) ( class slots size align -- )
221     [
222         [ "struct-slots" set-word-prop ]
223         [ define-accessors ] 2bi
224     ]
225     [ "struct-size" set-word-prop ]
226     [ "struct-align" set-word-prop ] tri-curry*
227     [ tri ] 3curry
228     [ dup make-struct-prototype "prototype" set-word-prop ]
229     [ (struct-methods) ] tri ;
230
231 : check-struct-slots ( slots -- )
232     [ type>> c-type drop ] each ;
233
234 : redefine-struct-tuple-class ( class -- )
235     [ dup class? [ forget-class ] [ drop ] if ] [ struct f define-tuple-class ] bi ;
236
237 : (define-struct-class) ( class slots offsets-quot -- )
238     [ 
239         empty?
240         [ struct-must-have-slots ]
241         [ redefine-struct-tuple-class ] if
242     ]
243     swap '[
244         make-slots dup
245         [ check-struct-slots ] _ [ struct-align [ align ] keep ] tri
246         (struct-word-props)
247     ]
248     [ drop [ c-type-for-class ] keep typedef ] 2tri ; inline
249 PRIVATE>
250
251 : define-struct-class ( class slots -- )
252     [ struct-offsets ] (define-struct-class) ;
253
254 : define-union-struct-class ( class slots -- )
255     [ union-struct-offsets ] (define-struct-class) ;
256
257 M: struct-class reset-class
258     [ call-next-method ] [ name>> c-types get delete-at ] bi ;
259
260 ERROR: invalid-struct-slot token ;
261
262 : struct-slot-class ( c-type -- class' )
263     c-type c-type-boxed-class
264     dup \ byte-array = [ drop \ c-ptr ] when ;
265
266 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
267     [ struct-slot-spec new ] 3dip
268     [ >>name ]
269     [ [ >>type ] [ struct-slot-class >>class ] bi ]
270     [ [ dup empty? ] [ peel-off-attributes ] until drop ] tri* ;
271
272 <PRIVATE
273 : parse-struct-slot ( -- slot )
274     scan scan-c-type \ } parse-until <struct-slot-spec> ;
275     
276 : parse-struct-slots ( slots -- slots' more? )
277     scan {
278         { ";" [ f ] }
279         { "{" [ parse-struct-slot over push t ] }
280         { f [ unexpected-eof ] }
281         [ invalid-struct-slot ]
282     } case ;
283
284 : parse-struct-definition ( -- class slots )
285     CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array ;
286 PRIVATE>
287
288 SYNTAX: STRUCT:
289     parse-struct-definition define-struct-class ;
290 SYNTAX: UNION-STRUCT:
291     parse-struct-definition define-union-struct-class ;
292
293 SYNTAX: S{
294     scan-word dup struct-slots parse-tuple-literal-slots parsed ;
295
296 SYNTAX: S@
297     scan-word scan-object swap memory>struct parsed ;
298
299 ! functor support
300
301 <PRIVATE
302 : scan-c-type` ( -- c-type/param )
303     scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
304
305 : parse-struct-slot` ( accum -- accum )
306     scan-string-param scan-c-type` \ } parse-until
307     [ <struct-slot-spec> over push ] 3curry over push-all ;
308
309 : parse-struct-slots` ( accum -- accum more? )
310     scan {
311         { ";" [ f ] }
312         { "{" [ parse-struct-slot` t ] }
313         [ invalid-struct-slot ]
314     } case ;
315 PRIVATE>
316
317 FUNCTOR-SYNTAX: STRUCT:
318     scan-param parsed
319     [ 8 <vector> ] over push-all
320     [ parse-struct-slots` ] [ ] while
321     [ >array define-struct-class ] over push-all ;
322
323 USING: vocabs vocabs.loader ;
324
325 "prettyprint" vocab [ "classes.struct.prettyprint" require ] when