]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct.factor
alien.data: make binary-zero? public and move it from classes.struct.private
[factor.git] / basis / classes / struct / struct.factor
1 ! (c)Joe Groff, Daniel Ehrenberg bsd license
2 USING: accessors alien alien.c-types alien.data alien.parser
3 arrays byte-arrays classes classes.private classes.parser
4 classes.tuple classes.tuple.parser classes.tuple.private
5 combinators combinators.short-circuit combinators.smart
6 cpu.architecture definitions functors.backend fry
7 generalizations generic.parser kernel kernel.private lexer libc
8 locals macros make math math.order parser quotations sequences
9 slots slots.private specialized-arrays vectors words summary
10 namespaces assocs vocabs.parser math.functions
11 classes.struct.bit-accessors bit-arrays
12 stack-checker.dependencies system layouts ;
13 FROM: delegate.private => group-words slot-group-words ;
14 QUALIFIED: math
15 IN: classes.struct
16
17 SPECIALIZED-ARRAY: uchar
18
19 ERROR: struct-must-have-slots ;
20
21 M: struct-must-have-slots summary
22     drop "Struct definitions must have slots" ;
23
24 TUPLE: struct
25     { (underlying) c-ptr read-only } ;
26
27 TUPLE: struct-slot-spec < slot-spec
28     type ;
29
30 ! For a struct-bit-slot-spec, offset is in bits, not bytes
31 TUPLE: struct-bit-slot-spec < struct-slot-spec
32     bits signed? ;
33
34 PREDICATE: struct-class < tuple-class
35     superclass \ struct eq? ;
36
37 SLOT: fields
38
39 : struct-slots ( struct-class -- slots )
40     "c-type" word-prop fields>> ;
41
42 M: struct-class group-words
43     struct-slots slot-group-words ;
44
45 ! struct allocation
46
47 M: struct >c-ptr
48     2 slot { c-ptr } declare ; inline
49
50 M: struct equal?
51     {
52         [ [ class ] bi@ = ]
53         [ [ >c-ptr ] [ binary-object ] bi* memory= ]
54     } 2&& ; inline
55
56 M: struct hashcode*
57     binary-object <direct-uchar-array> hashcode* ; inline
58
59 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
60
61 : memory>struct ( ptr class -- struct )
62     ! This is sub-optimal if the class is not literal, but gets
63     ! optimized down to efficient code if it is.
64     '[ _ boa ] call( ptr -- struct ) ; inline
65
66 <PRIVATE
67 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
68     '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
69 PRIVATE>
70
71 : (malloc-struct) ( class -- struct )
72     [ heap-size malloc ] keep memory>struct ; inline
73
74 : malloc-struct ( class -- struct )
75     [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
76
77 : (struct) ( class -- struct )
78     [ heap-size (byte-array) ] keep memory>struct ; inline
79
80 : <struct> ( class -- struct )
81     [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
82
83 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
84     [
85         [ <wrapper> \ (struct) [ ] 2sequence ]
86         [
87             struct-slots
88             [ length \ ndip ]
89             [ [ name>> setter-word 1quotation ] map \ spread ] bi
90         ] bi
91     ] [ ] output>sequence ;
92
93 <PRIVATE
94 : pad-struct-slots ( values class -- values' class )
95     [ struct-slots [ initial>> ] map over length tail append ] keep ;
96
97 : sign-extend ( n bits -- n' )
98     ! formula from:
99     ! http://guru.multimedia.cx/fast-sign-extension/
100     1 - -1 swap shift [ + ] keep bitxor ; inline
101
102 : sign-extender ( signed? bits -- quot )
103     '[ _ [ _ sign-extend ] when ] ;
104
105 GENERIC: (reader-quot) ( slot -- quot )
106
107 M: struct-slot-spec (reader-quot)
108     [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
109
110 M: struct-bit-slot-spec (reader-quot)
111     [ [ offset>> ] [ bits>> ] bi bit-reader ]
112     [ [ signed?>> ] [ bits>> ] bi sign-extender ]
113     bi compose
114     [ >c-ptr ] prepose ;
115
116 GENERIC: (writer-quot) ( slot -- quot )
117
118 M: struct-slot-spec (writer-quot)
119     [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
120
121 M: struct-bit-slot-spec (writer-quot)
122     [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
123
124 : (boxer-quot) ( class -- quot )
125     '[ _ memory>struct ] ;
126
127 : (unboxer-quot) ( class -- quot )
128     drop [ >c-ptr ] ;
129
130 MACRO: read-struct-slot ( slot -- )
131     dup type>> depends-on-c-type
132     (reader-quot) ;
133
134 MACRO: write-struct-slot ( slot -- )
135     dup type>> depends-on-c-type
136     (writer-quot) ;
137 PRIVATE>
138
139 M: struct-class boa>object
140     swap pad-struct-slots
141     [ <struct> ] [ struct-slots ] bi
142     [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
143
144 M: struct-class initial-value* <struct> ; inline
145
146 ! Struct slot accessors
147
148 GENERIC: struct-slot-values ( struct -- sequence )
149
150 M: struct-class reader-quot
151     dup type>> array? [ dup type>> first define-array-vocab drop ] when
152     nip '[ _ read-struct-slot ] ;
153
154 M: struct-class writer-quot
155     nip '[ _ write-struct-slot ] ;
156
157 : offset-of ( field struct -- offset )
158     struct-slots slot-named offset>> ; inline
159
160 ! c-types
161
162 TUPLE: struct-c-type < abstract-c-type
163     fields
164     return-in-registers? ;
165
166 INSTANCE: struct-c-type value-type
167
168 M: struct-c-type c-type ;
169
170 M: struct-c-type base-type ;
171
172 : large-struct? ( type -- ? )
173     {
174         { [ dup void? ] [ drop f ] }
175         { [ dup base-type struct-c-type? not ] [ drop f ] }
176         [ return-struct-in-registers? not ]
177     } cond ;
178
179 <PRIVATE
180 : struct-slot-values-quot ( class -- quot )
181     struct-slots
182     [ name>> reader-word 1quotation ] map
183     \ cleave [ ] 2sequence
184     \ output>array [ ] 2sequence ;
185
186 : (define-struct-slot-values-method) ( class -- )
187     [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
188     define-inline-method ;
189
190 : clone-underlying ( struct -- byte-array )
191     binary-object memory>byte-array ; inline
192
193 : (define-clone-method) ( class -- )
194     [ \ clone ]
195     [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
196     define-inline-method ;
197
198 :: c-type-for-class ( class slots size align -- c-type )
199     struct-c-type new
200         byte-array >>class
201         class >>boxed-class
202         slots >>fields
203         size >>size
204         align >>align
205         align >>align-first
206         class (unboxer-quot) >>unboxer-quot
207         class (boxer-quot) >>boxer-quot ;
208
209 GENERIC: compute-slot-offset ( offset class -- offset' )
210
211 : c-type-align-at ( class offset -- n )
212     0 = [ c-type-align-first ] [ c-type-align ] if ;
213
214 M: struct-slot-spec compute-slot-offset
215     [ type>> over c-type-align-at 8 * align ] keep
216     [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
217
218 M: struct-bit-slot-spec compute-slot-offset
219     [ offset<< ] [ bits>> + ] 2bi ;
220
221 : compute-struct-offsets ( slots -- size )
222     0 [ compute-slot-offset ] reduce 8 align 8 /i ;
223
224 : compute-union-offsets ( slots -- size )
225     1 [ 0 >>offset type>> heap-size max ] reduce ;
226
227 : struct-alignment ( slots -- align )
228     [ struct-bit-slot-spec? not ] filter
229     1 [ [ type>> ] [ offset>> ] bi c-type-align-at max ] reduce ;
230
231 PRIVATE>
232
233 M: struct byte-length class "struct-size" word-prop ; foldable
234 M: struct binary-zero? binary-object <direct-uchar-array> [ 0 = ] all? ; inline
235
236 ! class definition
237
238 <PRIVATE
239 : struct-needs-prototype? ( class -- ? )
240     struct-slots [ initial>> binary-zero? ] all? not ;
241
242 : make-struct-prototype ( class -- prototype )
243     dup struct-needs-prototype? [
244         [ "c-type" word-prop size>> <byte-array> ]
245         [ memory>struct ]
246         [ struct-slots ] tri
247         [
248             [ initial>> ]
249             [ (writer-quot) ] bi
250             over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
251         ] each
252     ] [ drop f ] if ;
253
254 : (struct-methods) ( class -- )
255     [ (define-struct-slot-values-method) ]
256     [ (define-clone-method) ]
257     bi ;
258
259 : check-struct-slots ( slots -- )
260     [ type>> c-type drop ] each ;
261
262 : redefine-struct-tuple-class ( class -- )
263     [ struct f define-tuple-class ] [ make-final ] bi ;
264
265 :: (define-struct-class) ( class slots offsets-quot -- )
266     slots empty? [ struct-must-have-slots ] when
267     class redefine-struct-tuple-class
268     slots make-slots dup check-struct-slots :> slot-specs
269     slot-specs offsets-quot call :> unaligned-size
270     slot-specs struct-alignment :> alignment
271     unaligned-size alignment align :> size
272
273     class  slot-specs  size  alignment  c-type-for-class :> c-type
274
275     c-type class typedef
276     class slot-specs define-accessors
277     class size "struct-size" set-word-prop
278     class dup make-struct-prototype "prototype" set-word-prop
279     class (struct-methods) ; inline
280 PRIVATE>
281
282 : define-struct-class ( class slots -- )
283     [ compute-struct-offsets ] (define-struct-class) ;
284
285 : define-union-struct-class ( class slots -- )
286     [ compute-union-offsets ] (define-struct-class) ;
287
288 ERROR: invalid-struct-slot token ;
289
290 : struct-slot-class ( c-type -- class' )
291     c-type c-type-boxed-class
292     dup \ byte-array = [ drop \ c-ptr ] when ;
293
294 SYMBOL: bits:
295
296 <PRIVATE
297
298 ERROR: bad-type-for-bits type ;
299
300 :: set-bits ( slot-spec n -- slot-spec )
301     struct-bit-slot-spec new
302         n >>bits
303         slot-spec type>> {
304             { int [ t ] }
305             { uint [ f ] }
306             [ bad-type-for-bits ]
307         } case >>signed?
308         slot-spec name>> >>name
309         slot-spec class>> >>class
310         slot-spec type>> >>type
311         slot-spec read-only>> >>read-only
312         slot-spec initial>> >>initial ;
313
314 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
315     dup empty? [
316         unclip {
317             { initial: [ [ first >>initial ] [ rest ] bi ] }
318             { read-only [ [ t >>read-only ] dip ] }
319             { bits: [ [ first set-bits ] [ rest ] bi ] }
320             [ bad-slot-attribute ]
321         } case
322     ] unless ;
323
324 PRIVATE>
325
326 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
327     [ struct-slot-spec new ] 3dip
328     [ >>name ]
329     [ [ >>type ] [ struct-slot-class >>class ] bi ]
330     [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
331
332 <PRIVATE
333 : parse-struct-slot ( -- slot )
334     scan scan-c-type \ } parse-until <struct-slot-spec> ;
335
336 : parse-struct-slots ( slots -- slots' more? )
337     scan {
338         { ";" [ f ] }
339         { "{" [ parse-struct-slot suffix! t ] }
340         { f [ unexpected-eof ] }
341         [ invalid-struct-slot ]
342     } case ;
343
344 : parse-struct-definition ( -- class slots )
345     CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array
346     dup [ name>> ] map check-duplicate-slots ;
347 PRIVATE>
348
349 SYNTAX: STRUCT:
350     parse-struct-definition define-struct-class ;
351 SYNTAX: UNION-STRUCT:
352     parse-struct-definition define-union-struct-class ;
353
354 SYNTAX: S{
355     scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
356
357 SYNTAX: S@
358     scan-word scan-object swap memory>struct suffix! ;
359
360 ! functor support
361
362 <PRIVATE
363 : scan-c-type` ( -- c-type/param )
364     scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
365
366 : parse-struct-slot` ( accum -- accum )
367     scan-string-param scan-c-type` \ } parse-until
368     [ <struct-slot-spec> suffix! ] 3curry append! ;
369
370 : parse-struct-slots` ( accum -- accum more? )
371     scan {
372         { ";" [ f ] }
373         { "{" [ parse-struct-slot` t ] }
374         [ invalid-struct-slot ]
375     } case ;
376 PRIVATE>
377
378 FUNCTOR-SYNTAX: STRUCT:
379     scan-param suffix!
380     [ 8 <vector> ] append!
381     [ parse-struct-slots` ] [ ] while
382     [ >array define-struct-class ] append! ;
383
384 USING: vocabs vocabs.loader ;
385
386 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when