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