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