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