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