]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct.factor
Merge branch 'master' of factorcode.org:/git/factor
[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
3 arrays byte-arrays classes classes.private classes.parser
4 classes.tuple classes.tuple.parser classes.tuple.private
5 combinators combinators.short-circuit combinators.smart
6 cpu.architecture definitions functors.backend fry
7 generalizations generic.parser kernel kernel.private lexer libc
8 locals macros make math math.order parser quotations sequences
9 slots slots.private specialized-arrays vectors words summary
10 namespaces assocs vocabs.parser math.functions
11 classes.struct.bit-accessors bit-arrays
12 stack-checker.dependencies system layouts ;
13 QUALIFIED: math
14 IN: classes.struct
15
16 SPECIALIZED-ARRAY: uchar
17
18 ERROR: struct-must-have-slots ;
19
20 M: struct-must-have-slots summary
21     drop "Struct definitions must have slots" ;
22
23 TUPLE: struct
24     { (underlying) c-ptr read-only } ;
25
26 TUPLE: struct-slot-spec < slot-spec
27     type ;
28
29 ! For a struct-bit-slot-spec, offset is in bits, not bytes
30 TUPLE: struct-bit-slot-spec < struct-slot-spec
31     bits signed? ;
32
33 PREDICATE: struct-class < tuple-class
34     superclass \ struct eq? ;
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 ] [ binary-object ] bi* memory= ]
50     } 2&& ; inline
51
52 M: struct hashcode*
53     binary-object <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-getter ]
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
129 MACRO: read-struct-slot ( slot -- )
130     dup type>> depends-on-c-type
131     (reader-quot) ;
132
133 MACRO: write-struct-slot ( slot -- )
134     dup type>> depends-on-c-type
135     (writer-quot) ;
136 PRIVATE>
137
138 M: struct-class boa>object
139     swap pad-struct-slots
140     [ <struct> ] [ struct-slots ] bi
141     [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
142
143 M: struct-class initial-value* <struct> ; inline
144
145 ! Struct slot accessors
146
147 GENERIC: struct-slot-values ( struct -- sequence )
148
149 M: struct-class reader-quot
150     dup type>> array? [ dup type>> first define-array-vocab drop ] when
151     nip '[ _ read-struct-slot ] ;
152
153 M: struct-class writer-quot
154     nip '[ _ write-struct-slot ] ;
155
156 : offset-of ( field struct -- offset )
157     struct-slots slot-named offset>> ; inline
158
159 ! c-types
160
161 TUPLE: struct-c-type < abstract-c-type
162     fields
163     return-in-registers? ;
164
165 INSTANCE: struct-c-type value-type
166
167 M: struct-c-type c-type ;
168
169 M: struct-c-type base-type ;
170
171 M: struct-c-type stack-size
172     dup value-struct? [ heap-size cell align ] [ drop cell ] if ;
173
174 HOOK: flatten-struct-type cpu ( type -- pairs )
175
176 M: object flatten-struct-type
177     stack-size cell /i { int-rep f } <repetition> ;
178
179 : large-struct? ( type -- ? )
180     {
181         { [ dup void? ] [ drop f ] }
182         { [ dup base-type struct-c-type? not ] [ drop f ] }
183         [ return-struct-in-registers? not ]
184     } cond ;
185
186 <PRIVATE
187 : struct-slot-values-quot ( class -- quot )
188     struct-slots
189     [ name>> reader-word 1quotation ] map
190     \ cleave [ ] 2sequence
191     \ output>array [ ] 2sequence ;
192
193 : (define-struct-slot-values-method) ( class -- )
194     [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
195     define-inline-method ;
196
197 : clone-underlying ( struct -- byte-array )
198     binary-object memory>byte-array ; inline
199
200 : (define-clone-method) ( class -- )
201     [ \ clone ]
202     [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
203     define-inline-method ;
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 ( class offset -- n )
219     0 = [ c-type-align-first ] [ c-type-align ] if ;
220
221 M: struct-slot-spec compute-slot-offset
222     [ type>> over c-type-align-at 8 * align ] keep
223     [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
224
225 M: struct-bit-slot-spec compute-slot-offset
226     [ offset<< ] [ bits>> + ] 2bi ;
227
228 : compute-struct-offsets ( slots -- size )
229     0 [ compute-slot-offset ] reduce 8 align 8 /i ;
230
231 : compute-union-offsets ( slots -- size )
232     1 [ 0 >>offset type>> heap-size max ] reduce ;
233
234 : struct-alignment ( slots -- align )
235     [ struct-bit-slot-spec? not ] filter
236     1 [ [ type>> ] [ offset>> ] bi c-type-align-at max ] reduce ;
237
238 PRIVATE>
239
240 M: struct byte-length class "struct-size" word-prop ; foldable
241
242 ! class definition
243
244 <PRIVATE
245 GENERIC: binary-zero? ( value -- ? )
246
247 M: object binary-zero? drop f ;
248 M: f binary-zero? drop t ;
249 M: number binary-zero? 0 = ;
250 M: struct binary-zero? >c-ptr [ 0 = ] all? ;
251
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 slots offsets-quot -- )
279     slots empty? [ struct-must-have-slots ] when
280     class redefine-struct-tuple-class
281     slots make-slots dup check-struct-slots :> slot-specs
282     slot-specs offsets-quot call :> unaligned-size
283     slot-specs struct-alignment :> 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 PRIVATE>
294
295 : define-struct-class ( class slots -- )
296     [ compute-struct-offsets ] (define-struct-class) ;
297
298 : define-union-struct-class ( class slots -- )
299     [ compute-union-offsets ] (define-struct-class) ;
300
301 ERROR: invalid-struct-slot token ;
302
303 : struct-slot-class ( c-type -- class' )
304     c-type c-type-boxed-class
305     dup \ byte-array = [ drop \ c-ptr ] when ;
306
307 SYMBOL: bits:
308
309 <PRIVATE
310
311 ERROR: bad-type-for-bits type ;
312
313 :: set-bits ( slot-spec n -- slot-spec )
314     struct-bit-slot-spec new
315         n >>bits
316         slot-spec type>> {
317             { int [ t ] }
318             { uint [ f ] }
319             [ bad-type-for-bits ]
320         } case >>signed?
321         slot-spec name>> >>name
322         slot-spec class>> >>class
323         slot-spec type>> >>type
324         slot-spec read-only>> >>read-only
325         slot-spec initial>> >>initial ;
326
327 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
328     dup empty? [
329         unclip {
330             { initial: [ [ first >>initial ] [ rest ] bi ] }
331             { read-only [ [ t >>read-only ] dip ] }
332             { bits: [ [ first set-bits ] [ rest ] bi ] }
333             [ bad-slot-attribute ]
334         } case
335     ] unless ;
336
337 PRIVATE>
338
339 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
340     [ struct-slot-spec new ] 3dip
341     [ >>name ]
342     [ [ >>type ] [ struct-slot-class >>class ] bi ]
343     [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
344
345 <PRIVATE
346 : parse-struct-slot ( -- slot )
347     scan scan-c-type \ } parse-until <struct-slot-spec> ;
348
349 : parse-struct-slots ( slots -- slots' more? )
350     scan {
351         { ";" [ f ] }
352         { "{" [ parse-struct-slot suffix! t ] }
353         { f [ unexpected-eof ] }
354         [ invalid-struct-slot ]
355     } case ;
356
357 : parse-struct-definition ( -- class slots )
358     CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array
359     dup [ name>> ] map check-duplicate-slots ;
360 PRIVATE>
361
362 SYNTAX: STRUCT:
363     parse-struct-definition define-struct-class ;
364 SYNTAX: UNION-STRUCT:
365     parse-struct-definition define-union-struct-class ;
366
367 SYNTAX: S{
368     scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
369
370 SYNTAX: S@
371     scan-word scan-object swap memory>struct suffix! ;
372
373 ! functor support
374
375 <PRIVATE
376 : scan-c-type` ( -- c-type/param )
377     scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
378
379 : parse-struct-slot` ( accum -- accum )
380     scan-string-param scan-c-type` \ } parse-until
381     [ <struct-slot-spec> suffix! ] 3curry append! ;
382
383 : parse-struct-slots` ( accum -- accum more? )
384     scan {
385         { ";" [ f ] }
386         { "{" [ parse-struct-slot` t ] }
387         [ invalid-struct-slot ]
388     } case ;
389 PRIVATE>
390
391 FUNCTOR-SYNTAX: STRUCT:
392     scan-param suffix!
393     [ 8 <vector> ] append!
394     [ parse-struct-slots` ] [ ] while
395     [ >array define-struct-class ] append! ;
396
397 USING: vocabs vocabs.loader ;
398
399 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when