]> gitweb.factorcode.org Git - factor.git/blob - basis/classes/struct/struct.factor
Language change: tuple slot setter words with stack effect ( value object -- ) are...
[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 : if-value-struct ( ctype true false -- )
170     [ dup value-struct? ] 2dip '[ drop void* @ ] if ; inline
171
172 M: struct-c-type unbox-parameter
173     [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
174
175 M: struct-c-type box-parameter
176     [ %box-large-struct ] [ box-parameter ] if-value-struct ;
177
178 : if-small-struct ( c-type true false -- ? )
179     [ dup return-struct-in-registers? ] 2dip '[ f swap @ ] if ; inline
180
181 M: struct-c-type unbox-return
182     [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
183
184 M: struct-c-type box-return
185     [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
186
187 M: struct-c-type stack-size
188     [ heap-size cell align ] [ stack-size ] if-value-struct ;
189
190 HOOK: flatten-struct-type cpu ( type -- reps )
191
192 M: object flatten-struct-type int-rep (flatten-c-type) ;
193
194 M: struct-c-type flatten-c-type flatten-struct-type ;
195
196 M: struct-c-type c-struct? drop t ;
197
198 <PRIVATE
199 : struct-slot-values-quot ( class -- quot )
200     struct-slots
201     [ name>> reader-word 1quotation ] map
202     \ cleave [ ] 2sequence
203     \ output>array [ ] 2sequence ;
204
205 : (define-struct-slot-values-method) ( class -- )
206     [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
207     define-inline-method ;
208
209 : clone-underlying ( struct -- byte-array )
210     binary-object memory>byte-array ; inline
211
212 : (define-clone-method) ( class -- )
213     [ \ clone ]
214     [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
215     define-inline-method ;
216
217 :: c-type-for-class ( class slots size align -- c-type )
218     struct-c-type new
219         byte-array >>class
220         class >>boxed-class
221         slots >>fields
222         size >>size
223         align >>align
224         align >>align-first
225         class (unboxer-quot) >>unboxer-quot
226         class (boxer-quot) >>boxer-quot ;
227
228 GENERIC: compute-slot-offset ( offset class -- offset' )
229
230 : c-type-align-at ( class offset -- n )
231     0 = [ c-type-align-first ] [ c-type-align ] if ;
232
233 M: struct-slot-spec compute-slot-offset
234     [ type>> over c-type-align-at 8 * align ] keep
235     [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
236
237 M: struct-bit-slot-spec compute-slot-offset
238     [ offset<< ] [ bits>> + ] 2bi ;
239
240 : compute-struct-offsets ( slots -- size )
241     0 [ compute-slot-offset ] reduce 8 align 8 /i ;
242
243 : compute-union-offsets ( slots -- size )
244     1 [ 0 >>offset type>> heap-size max ] reduce ;
245
246 : struct-alignment ( slots -- align )
247     [ struct-bit-slot-spec? not ] filter
248     1 [ [ type>> ] [ offset>> ] bi c-type-align-at max ] reduce ;
249
250 PRIVATE>
251
252 M: struct byte-length class "struct-size" word-prop ; foldable
253
254 ! class definition
255
256 <PRIVATE
257 GENERIC: binary-zero? ( value -- ? )
258
259 M: object binary-zero? drop f ;
260 M: f binary-zero? drop t ;
261 M: number binary-zero? 0 = ;
262 M: struct binary-zero? >c-ptr [ 0 = ] all? ;
263
264 : struct-needs-prototype? ( class -- ? )
265     struct-slots [ initial>> binary-zero? ] all? not ;
266
267 : make-struct-prototype ( class -- prototype )
268     dup struct-needs-prototype? [
269         [ "c-type" word-prop size>> <byte-array> ]
270         [ memory>struct ]
271         [ struct-slots ] tri
272         [
273             [ initial>> ]
274             [ (writer-quot) ] bi
275             over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
276         ] each
277     ] [ drop f ] if ;
278
279 : (struct-methods) ( class -- )
280     [ (define-struct-slot-values-method) ]
281     [ (define-clone-method) ]
282     bi ;
283
284 : check-struct-slots ( slots -- )
285     [ type>> c-type drop ] each ;
286
287 : redefine-struct-tuple-class ( class -- )
288     [ struct f define-tuple-class ] [ make-final ] bi ;
289
290 :: (define-struct-class) ( class slots offsets-quot -- )
291     slots empty? [ struct-must-have-slots ] when
292     class redefine-struct-tuple-class
293     slots make-slots dup check-struct-slots :> slot-specs
294     slot-specs offsets-quot call :> unaligned-size
295     slot-specs struct-alignment :> alignment
296     unaligned-size alignment align :> size
297
298     class  slot-specs  size  alignment  c-type-for-class :> c-type
299
300     c-type class typedef
301     class slot-specs define-accessors
302     class size "struct-size" set-word-prop
303     class dup make-struct-prototype "prototype" set-word-prop
304     class (struct-methods) ; inline
305 PRIVATE>
306
307 : define-struct-class ( class slots -- )
308     [ compute-struct-offsets ] (define-struct-class) ;
309
310 : define-union-struct-class ( class slots -- )
311     [ compute-union-offsets ] (define-struct-class) ;
312
313 ERROR: invalid-struct-slot token ;
314
315 : struct-slot-class ( c-type -- class' )
316     c-type c-type-boxed-class
317     dup \ byte-array = [ drop \ c-ptr ] when ;
318
319 SYMBOL: bits:
320
321 <PRIVATE
322
323 ERROR: bad-type-for-bits type ;
324
325 :: set-bits ( slot-spec n -- slot-spec )
326     struct-bit-slot-spec new
327         n >>bits
328         slot-spec type>> {
329             { int [ t ] }
330             { uint [ f ] }
331             [ bad-type-for-bits ]
332         } case >>signed?
333         slot-spec name>> >>name
334         slot-spec class>> >>class
335         slot-spec type>> >>type
336         slot-spec read-only>> >>read-only
337         slot-spec initial>> >>initial ;
338
339 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
340     dup empty? [
341         unclip {
342             { initial: [ [ first >>initial ] [ rest ] bi ] }
343             { read-only [ [ t >>read-only ] dip ] }
344             { bits: [ [ first set-bits ] [ rest ] bi ] }
345             [ bad-slot-attribute ]
346         } case
347     ] unless ;
348
349 PRIVATE>
350
351 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
352     [ struct-slot-spec new ] 3dip
353     [ >>name ]
354     [ [ >>type ] [ struct-slot-class >>class ] bi ]
355     [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
356
357 <PRIVATE
358 : parse-struct-slot ( -- slot )
359     scan scan-c-type \ } parse-until <struct-slot-spec> ;
360
361 : parse-struct-slots ( slots -- slots' more? )
362     scan {
363         { ";" [ f ] }
364         { "{" [ parse-struct-slot suffix! t ] }
365         { f [ unexpected-eof ] }
366         [ invalid-struct-slot ]
367     } case ;
368
369 : parse-struct-definition ( -- class slots )
370     CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array
371     dup [ name>> ] map check-duplicate-slots ;
372 PRIVATE>
373
374 SYNTAX: STRUCT:
375     parse-struct-definition define-struct-class ;
376 SYNTAX: UNION-STRUCT:
377     parse-struct-definition define-union-struct-class ;
378
379 SYNTAX: S{
380     scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
381
382 SYNTAX: S@
383     scan-word scan-object swap memory>struct suffix! ;
384
385 ! functor support
386
387 <PRIVATE
388 : scan-c-type` ( -- c-type/param )
389     scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
390
391 : parse-struct-slot` ( accum -- accum )
392     scan-string-param scan-c-type` \ } parse-until
393     [ <struct-slot-spec> suffix! ] 3curry append! ;
394
395 : parse-struct-slots` ( accum -- accum more? )
396     scan {
397         { ";" [ f ] }
398         { "{" [ parse-struct-slot` t ] }
399         [ invalid-struct-slot ]
400     } case ;
401 PRIVATE>
402
403 FUNCTOR-SYNTAX: STRUCT:
404     scan-param suffix!
405     [ 8 <vector> ] append!
406     [ parse-struct-slots` ] [ ] while
407     [ >array define-struct-class ] append! ;
408
409 USING: vocabs vocabs.loader ;
410
411 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when