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 ;
15 SPECIALIZED-ARRAY: uchar
17 ERROR: struct-must-have-slots ;
19 M: struct-must-have-slots summary
20 drop "Struct definitions must have slots" ;
23 { (underlying) c-ptr read-only } ;
25 TUPLE: struct-slot-spec < slot-spec
28 ! For a struct-bit-slot-spec, offset is in bits, not bytes
29 TUPLE: struct-bit-slot-spec < struct-slot-spec
32 PREDICATE: struct-class < tuple-class
33 superclass \ struct eq? ;
37 : struct-slots ( struct-class -- slots )
38 "c-type" word-prop fields>> ;
43 2 slot { c-ptr } declare ; inline
48 [ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
52 [ >c-ptr ] [ byte-length ] bi <direct-uchar-array> hashcode* ; inline
54 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
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 '[ _ boa ] call( ptr -- struct ) ; inline
62 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
63 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
66 : (malloc-struct) ( class -- struct )
67 [ heap-size malloc ] keep memory>struct ; inline
69 : malloc-struct ( class -- struct )
70 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
72 : (struct) ( class -- struct )
73 [ heap-size (byte-array) ] keep memory>struct ; inline
75 : <struct> ( class -- struct )
76 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
78 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
80 [ <wrapper> \ (struct) [ ] 2sequence ]
84 [ [ name>> setter-word 1quotation ] map \ spread ] bi
86 ] [ ] output>sequence ;
89 : pad-struct-slots ( values class -- values' class )
90 [ struct-slots [ initial>> ] map over length tail append ] keep ;
92 : sign-extend ( n bits -- n' )
94 ! http://guru.multimedia.cx/fast-sign-extension/
95 1 - -1 swap shift [ + ] keep bitxor ; inline
97 : sign-extender ( signed? bits -- quot )
98 '[ _ [ _ sign-extend ] when ] ;
100 GENERIC: (reader-quot) ( slot -- quot )
102 M: struct-slot-spec (reader-quot)
103 [ type>> c-type-getter-boxer ]
104 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
106 M: struct-bit-slot-spec (reader-quot)
107 [ [ offset>> ] [ bits>> ] bi bit-reader ]
108 [ [ signed?>> ] [ bits>> ] bi sign-extender ]
112 GENERIC: (writer-quot) ( slot -- quot )
114 M: struct-slot-spec (writer-quot)
116 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
118 M: struct-bit-slot-spec (writer-quot)
119 [ offset>> ] [ bits>> ] bi bit-writer
122 : (boxer-quot) ( class -- quot )
123 '[ _ memory>struct ] ;
125 : (unboxer-quot) ( class -- quot )
129 M: struct-class boa>object
130 swap pad-struct-slots
131 [ <struct> ] [ struct-slots ] bi
132 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
134 M: struct-class initial-value* <struct> ; inline
136 ! Struct slot accessors
138 GENERIC: struct-slot-values ( struct -- sequence )
140 M: struct-class reader-quot
143 M: struct-class writer-quot
146 : offset-of ( field struct -- offset )
147 struct-slots slot-named offset>> ; inline
151 TUPLE: struct-c-type < abstract-c-type
153 return-in-registers? ;
155 INSTANCE: struct-c-type value-type
157 M: struct-c-type c-type ;
159 M: struct-c-type c-type-stack-align? drop f ;
161 : if-value-struct ( ctype true false -- )
162 [ dup value-struct? ] 2dip '[ drop void* @ ] if ; inline
164 M: struct-c-type unbox-parameter
165 [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
167 M: struct-c-type box-parameter
168 [ %box-large-struct ] [ box-parameter ] if-value-struct ;
170 : if-small-struct ( c-type true false -- ? )
171 [ dup return-struct-in-registers? ] 2dip '[ f swap @ ] if ; inline
173 M: struct-c-type unbox-return
174 [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
176 M: struct-c-type box-return
177 [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
179 M: struct-c-type stack-size
180 [ heap-size ] [ stack-size ] if-value-struct ;
182 M: struct-c-type c-struct? drop t ;
185 : struct-slot-values-quot ( class -- quot )
187 [ name>> reader-word 1quotation ] map
188 \ cleave [ ] 2sequence
189 \ output>array [ ] 2sequence ;
191 : (define-struct-slot-values-method) ( class -- )
192 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
193 define-inline-method ;
195 : clone-underlying ( struct -- byte-array )
196 [ >c-ptr ] [ byte-length ] bi memory>byte-array ; inline
198 : (define-clone-method) ( class -- )
200 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
201 define-inline-method ;
203 :: c-type-for-class ( class slots size align -- c-type )
211 class (unboxer-quot) >>unboxer-quot
212 class (boxer-quot) >>boxer-quot ;
214 GENERIC: compute-slot-offset ( offset class -- offset' )
216 : c-type-align-at ( class offset -- n )
217 0 = [ c-type-align-first ] [ c-type-align ] if ;
219 M: struct-slot-spec compute-slot-offset
220 [ type>> over c-type-align-at 8 * align ] keep
221 [ [ 8 /i ] dip (>>offset) ] [ type>> heap-size 8 * + ] 2bi ;
223 M: struct-bit-slot-spec compute-slot-offset
224 [ (>>offset) ] [ bits>> + ] 2bi ;
226 : compute-struct-offsets ( slots -- size )
227 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
229 : compute-union-offsets ( slots -- size )
230 1 [ 0 >>offset type>> heap-size max ] reduce ;
232 : struct-alignment ( slots -- align )
233 [ struct-bit-slot-spec? not ] filter
234 1 [ [ type>> ] [ offset>> ] bi c-type-align-at max ] reduce ;
238 M: struct byte-length class "struct-size" word-prop ; foldable
243 GENERIC: binary-zero? ( value -- ? )
245 M: object binary-zero? drop f ;
246 M: f binary-zero? drop t ;
247 M: number binary-zero? 0 = ;
248 M: struct binary-zero? >c-ptr [ 0 = ] all? ;
250 : struct-needs-prototype? ( class -- ? )
251 struct-slots [ initial>> binary-zero? ] all? not ;
253 : make-struct-prototype ( class -- prototype )
254 dup struct-needs-prototype? [
255 [ "c-type" word-prop size>> <byte-array> ]
261 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
265 : (struct-methods) ( class -- )
266 [ (define-struct-slot-values-method) ]
267 [ (define-clone-method) ]
270 : check-struct-slots ( slots -- )
271 [ type>> c-type drop ] each ;
273 : redefine-struct-tuple-class ( class -- )
274 [ struct f define-tuple-class ] [ make-final ] bi ;
276 :: (define-struct-class) ( class slots offsets-quot -- )
277 slots empty? [ struct-must-have-slots ] when
278 class redefine-struct-tuple-class
279 slots make-slots dup check-struct-slots :> slot-specs
280 slot-specs offsets-quot call :> unaligned-size
281 slot-specs struct-alignment :> alignment
282 unaligned-size alignment align :> size
284 class slot-specs size alignment c-type-for-class :> c-type
287 class slot-specs define-accessors
288 class size "struct-size" set-word-prop
289 class dup make-struct-prototype "prototype" set-word-prop
290 class (struct-methods) ; inline
293 : define-struct-class ( class slots -- )
294 [ compute-struct-offsets ] (define-struct-class) ;
296 : define-union-struct-class ( class slots -- )
297 [ compute-union-offsets ] (define-struct-class) ;
299 ERROR: invalid-struct-slot token ;
301 : struct-slot-class ( c-type -- class' )
302 c-type c-type-boxed-class
303 dup \ byte-array = [ drop \ c-ptr ] when ;
309 ERROR: bad-type-for-bits type ;
311 :: set-bits ( slot-spec n -- slot-spec )
312 struct-bit-slot-spec new
317 [ bad-type-for-bits ]
319 slot-spec name>> >>name
320 slot-spec class>> >>class
321 slot-spec type>> >>type
322 slot-spec read-only>> >>read-only
323 slot-spec initial>> >>initial ;
325 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
328 { initial: [ [ first >>initial ] [ rest ] bi ] }
329 { read-only [ [ t >>read-only ] dip ] }
330 { bits: [ [ first set-bits ] [ rest ] bi ] }
331 [ bad-slot-attribute ]
337 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
338 [ struct-slot-spec new ] 3dip
340 [ [ >>type ] [ struct-slot-class >>class ] bi ]
341 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
344 : parse-struct-slot ( -- slot )
345 scan scan-c-type \ } parse-until <struct-slot-spec> ;
347 : parse-struct-slots ( slots -- slots' more? )
350 { "{" [ parse-struct-slot suffix! t ] }
351 { f [ unexpected-eof ] }
352 [ invalid-struct-slot ]
355 : parse-struct-definition ( -- class slots )
356 CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array ;
360 parse-struct-definition define-struct-class ;
361 SYNTAX: UNION-STRUCT:
362 parse-struct-definition define-union-struct-class ;
365 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
368 scan-word scan-object swap memory>struct suffix! ;
373 : scan-c-type` ( -- c-type/param )
374 scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
376 : parse-struct-slot` ( accum -- accum )
377 scan-string-param scan-c-type` \ } parse-until
378 [ <struct-slot-spec> suffix! ] 3curry append! ;
380 : parse-struct-slots` ( accum -- accum more? )
383 { "{" [ parse-struct-slot` t ] }
384 [ invalid-struct-slot ]
388 FUNCTOR-SYNTAX: STRUCT:
390 [ 8 <vector> ] append!
391 [ parse-struct-slots` ] [ ] while
392 [ >array define-struct-class ] append! ;
394 USING: vocabs vocabs.loader ;
396 "prettyprint" vocab [ "classes.struct.prettyprint" require ] when