]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct.factor
3763d65ab12d29467c960b0e1d8d04790beaefbc
[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 combinators.smart cpu.architecture definitions delegate.private
13 effects functors.backend generalizations generic generic.parser
14 io kernel kernel.private lexer libc math math.order parser
15 quotations sequences sequences.private slots slots.private
16 specialized-arrays stack-checker.dependencies summary vectors
17 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 hashcode*
166     nip dup >c-ptr [ struct-slot-values hashcode ] [ drop 0 ] if ; inline
167
168 ! c-types
169
170 TUPLE: struct-c-type < abstract-c-type
171     fields
172     return-in-registers? ;
173
174 INSTANCE: struct-c-type value-type
175
176 M: struct-c-type lookup-c-type ;
177
178 M: struct-c-type base-type ;
179
180 : large-struct? ( type -- ? )
181     {
182         { [ dup void? ] [ drop f ] }
183         { [ dup base-type struct-c-type? not ] [ drop f ] }
184         [ return-struct-in-registers? not ]
185     } cond ;
186
187 <PRIVATE
188 : struct-slot-values-quot ( class -- quot )
189     struct-slots
190     [ name>> reader-word 1quotation ] map
191     \ cleave [ ] 2sequence
192     \ output>array [ ] 2sequence ;
193
194 : define-struct-slot-values-method ( class -- )
195     [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
196     define-inline-method ;
197
198 : forget-struct-slot-values-method ( class -- )
199     \ struct-slot-values ?lookup-method forget ;
200
201 : struct-equals-quot ( class -- quot )
202     dup struct-slots
203     [ name>> reader-word '[ [ _ execute ] same? ] ] map
204     '[ over _ instance? [ _ 2&& ] [ 2drop f ] if ] ;
205
206 : define-equal-method ( class -- )
207     [ \ equal? ] [ struct-equals-quot ] bi define-inline-method ;
208
209 : forget-equal-method ( class -- )
210     \ equal? ?lookup-method forget ;
211
212 : clone-underlying ( struct -- byte-array )
213     binary-object memory>byte-array ; inline
214
215 : define-clone-method ( class -- )
216     [ \ clone ]
217     [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
218     define-inline-method ;
219
220 : forget-clone-method ( class -- )
221     \ clone ?lookup-method forget ;
222
223 :: c-type-for-class ( class slots size align -- c-type )
224     struct-c-type new
225         byte-array >>class
226         class >>boxed-class
227         slots >>fields
228         size >>size
229         align >>align
230         align >>align-first
231         class (unboxer-quot) >>unboxer-quot
232         class (boxer-quot) >>boxer-quot ;
233
234 GENERIC: compute-slot-offset ( offset class -- offset' )
235
236 : c-type-align-at ( slot-spec offset -- n )
237     over packed?>> [ 2drop 1 ] [
238         [ type>> ] dip
239         0 = [ c-type-align-first ] [ c-type-align ] if
240     ] if ;
241
242 M: struct-slot-spec compute-slot-offset
243     [ over c-type-align-at 8 * align ] keep
244     [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
245
246 M: struct-bit-slot-spec compute-slot-offset
247     [ offset<< ] [ bits>> + ] 2bi ;
248
249 : compute-struct-offsets ( slots -- size )
250     0 [ compute-slot-offset ] reduce 8 align 8 /i ;
251
252 : compute-union-offsets ( slots -- size )
253     1 [ 0 >>offset type>> heap-size max ] reduce ;
254
255 : struct-alignment ( slots -- align )
256     [ struct-bit-slot-spec? ] reject
257     1 [ dup offset>> c-type-align-at max ] reduce ;
258
259 PRIVATE>
260
261 : struct-size ( class -- n ) "struct-size" word-prop ; inline
262
263 M: struct byte-length class-of struct-size ; inline foldable
264 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
265
266 ! class definition
267
268 <PRIVATE
269 : struct-needs-prototype? ( class -- ? )
270     struct-slots [ initial>> binary-zero? ] all? not ;
271
272 : make-struct-prototype ( class -- prototype )
273     dup struct-needs-prototype? [
274         [ "c-type" word-prop size>> <byte-array> ]
275         [ memory>struct ]
276         [ struct-slots ] tri
277         [
278             [ initial>> ]
279             [ (writer-quot) ] bi
280             over [ swapd [ call( value struct -- ) ] keepd ] [ 2drop ] if
281         ] each
282     ] [ drop f ] if ;
283
284 : (struct-methods) ( class -- )
285     [ define-struct-slot-values-method ]
286     [ define-clone-method ]
287     [ define-equal-method ]
288     tri ;
289
290 : check-struct-slots ( slots -- )
291     [ type>> lookup-c-type drop ] each ;
292
293 : redefine-struct-tuple-class ( class -- )
294     [ struct f redefine-tuple-class ] [ make-final ] bi ;
295
296 : resize-underlying ( struct -- )
297     [ 2 slot dup byte-array? ]
298     [ class-of "struct-size" word-prop '[ _ swap resize ] [ drop f ] if ]
299     [ 2 set-slot ] tri ;
300
301 M: struct update-tuple
302     ! make sure underlying byte-array is correct size, but maybe
303     ! has incorrect contents... is there something better to do?
304     [ resize-underlying ] [ call-next-method ] bi ;
305
306 : forget-struct-slot-accessors ( class -- )
307     dup "c-type" word-prop [
308         dup struct-c-type? [
309             fields>> forget-slot-accessors
310         ] [ 2drop ] if
311     ] [ drop ] if* ;
312
313 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
314     slot-specs check-struct-slots
315     slot-specs empty? [ struct-must-have-slots ] when
316     class redefine-struct-tuple-class
317     slot-specs offsets-quot call :> unaligned-size
318     slot-specs alignment-quot call :> alignment
319     unaligned-size alignment align :> size
320
321     class slot-specs size alignment c-type-for-class :> c-type
322
323     class forget-struct-slot-accessors
324
325     c-type class typedef
326     class slot-specs define-accessors
327     class size "struct-size" set-word-prop
328     class dup make-struct-prototype "prototype" set-word-prop
329     class (struct-methods) ; inline
330
331 : make-packed-slots ( slots -- slot-specs )
332     make-slots [ t >>packed? ] map! ;
333
334 PRIVATE>
335
336 : define-struct-class ( class slots -- )
337     make-slots
338     [ compute-struct-offsets ] [ struct-alignment ]
339     (define-struct-class) ;
340
341 : define-packed-struct-class ( class slots -- )
342     make-packed-slots
343     [ compute-struct-offsets ] [ drop 1 ]
344     (define-struct-class) ;
345
346 : define-union-struct-class ( class slots -- )
347     make-slots
348     [ compute-union-offsets ] [ struct-alignment ]
349     (define-struct-class) ;
350
351 ERROR: invalid-struct-slot token ;
352
353 : struct-slot-class ( c-type -- class' )
354     lookup-c-type c-type-boxed-class
355     dup \ byte-array = [ drop \ c-ptr ] when ;
356
357 M: struct-class reset-class
358     {
359         [ \ <struct-boa> def>> first delete-at ]
360         [ forget-struct-slot-accessors ]
361         [ forget-struct-slot-values-method ]
362         [ forget-clone-method ]
363         [ forget-equal-method ]
364         [ { "c-type" "layout" "struct-size" } remove-word-props ]
365         [ call-next-method ]
366     } cleave ;
367
368 SYMBOL: bits:
369
370 <PRIVATE
371
372 :: set-bits ( slot-spec n -- slot-spec )
373     struct-bit-slot-spec new
374         n >>bits
375         slot-spec type>> c-type-signed >>signed?
376         slot-spec name>> >>name
377         slot-spec class>> >>class
378         slot-spec type>> >>type
379         slot-spec read-only>> >>read-only
380         slot-spec initial>> >>initial ;
381
382 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
383     dup empty? [
384         unclip {
385             { initial: [ [ first >>initial ] [ rest ] bi ] }
386             { read-only [ [ t >>read-only ] dip ] }
387             { bits: [ [ first set-bits ] [ rest ] bi ] }
388             [ bad-slot-attribute ]
389         } case
390     ] unless ;
391
392 PRIVATE>
393
394 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
395     [ struct-slot-spec new ] 3dip
396     [ >>name ]
397     [ [ >>type ] [ struct-slot-class init-slot-class ] bi ]
398     [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
399
400 <PRIVATE
401 : parse-struct-slot ( -- slot )
402     scan-token scan-c-type \ } parse-until <struct-slot-spec> ;
403
404 : parse-struct-slots ( slots -- slots' more? )
405     scan-token {
406         { ";" [ f ] }
407         { "{" [ parse-struct-slot suffix! t ] }
408         [ invalid-struct-slot ]
409     } case ;
410
411 : parse-struct-definition ( -- class slots )
412     scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
413     dup [ name>> ] map check-duplicate-slots ;
414 PRIVATE>
415
416 SYNTAX: STRUCT:
417     parse-struct-definition define-struct-class ;
418
419 SYNTAX: PACKED-STRUCT:
420     parse-struct-definition define-packed-struct-class ;
421
422 SYNTAX: UNION-STRUCT:
423     parse-struct-definition define-union-struct-class ;
424
425 SYNTAX: S{
426     scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
427
428 SYNTAX: S@
429     scan-word scan-object swap memory>struct suffix! ;
430
431 ! functor support
432
433 <PRIVATE
434 : scan-c-type* ( -- c-type/param )
435     scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
436
437 : parse-struct-slot* ( accum -- accum )
438     scan-string-param scan-c-type* \ } parse-until
439     [ <struct-slot-spec> suffix! ] 3curry append! ;
440
441 : parse-struct-slots* ( accum -- accum more? )
442     scan-token {
443         { ";" [ f ] }
444         { "{" [ parse-struct-slot* t ] }
445         [ invalid-struct-slot ]
446     } case ;
447
448 PRIVATE>
449
450 FUNCTOR-SYNTAX: STRUCT:
451     scan-param suffix!
452     [ 8 <vector> ] append!
453     [ parse-struct-slots* ] [ ] while
454     [ >array define-struct-class ] append! ;
455
456 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when