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