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