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 FROM: delegate.private => group-words slot-group-words ;
17 SPECIALIZED-ARRAY: uchar
19 ERROR: struct-must-have-slots ;
21 M: struct-must-have-slots summary
22 drop "Struct definitions must have slots" ;
25 { (underlying) c-ptr read-only } ;
27 TUPLE: struct-slot-spec < slot-spec
30 ! For a struct-bit-slot-spec, offset is in bits, not bytes
31 TUPLE: struct-bit-slot-spec < struct-slot-spec
34 PREDICATE: struct-class < tuple-class
35 superclass \ struct eq? ;
39 : struct-slots ( struct-class -- slots )
40 "c-type" word-prop fields>> ;
42 M: struct-class group-words
43 struct-slots slot-group-words ;
48 2 slot { c-ptr } declare ; inline
53 [ [ >c-ptr ] [ binary-object ] bi* memory= ]
57 binary-object <direct-uchar-array> hashcode* ; inline
59 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
61 : memory>struct ( ptr class -- struct )
62 ! This is sub-optimal if the class is not literal, but gets
63 ! optimized down to efficient code if it is.
64 '[ _ boa ] call( ptr -- struct ) ; inline
67 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
68 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
71 : (malloc-struct) ( class -- struct )
72 [ heap-size malloc ] keep memory>struct ; inline
74 : malloc-struct ( class -- struct )
75 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
77 : (struct) ( class -- struct )
78 [ heap-size (byte-array) ] keep memory>struct ; inline
80 : <struct> ( class -- struct )
81 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
83 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
85 [ <wrapper> \ (struct) [ ] 2sequence ]
89 [ [ name>> setter-word 1quotation ] map \ spread ] bi
91 ] [ ] output>sequence ;
94 : pad-struct-slots ( values class -- values' class )
95 [ struct-slots [ initial>> ] map over length tail append ] keep ;
97 : sign-extend ( n bits -- n' )
99 ! http://guru.multimedia.cx/fast-sign-extension/
100 1 - -1 swap shift [ + ] keep bitxor ; inline
102 : sign-extender ( signed? bits -- quot )
103 '[ _ [ _ sign-extend ] when ] ;
105 GENERIC: (reader-quot) ( slot -- quot )
107 M: struct-slot-spec (reader-quot)
108 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
110 M: struct-bit-slot-spec (reader-quot)
111 [ [ offset>> ] [ bits>> ] bi bit-reader ]
112 [ [ signed?>> ] [ bits>> ] bi sign-extender ]
116 GENERIC: (writer-quot) ( slot -- quot )
118 M: struct-slot-spec (writer-quot)
119 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
121 M: struct-bit-slot-spec (writer-quot)
122 [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
124 : (boxer-quot) ( class -- quot )
125 '[ _ memory>struct ] ;
127 : (unboxer-quot) ( class -- quot )
130 MACRO: read-struct-slot ( slot -- )
131 dup type>> depends-on-c-type
134 MACRO: write-struct-slot ( slot -- )
135 dup type>> depends-on-c-type
139 M: struct-class boa>object
140 swap pad-struct-slots
141 [ <struct> ] [ struct-slots ] bi
142 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
144 M: struct-class initial-value* <struct> ; inline
146 ! Struct slot accessors
148 GENERIC: struct-slot-values ( struct -- sequence )
150 M: struct-class reader-quot
151 dup type>> array? [ dup type>> first define-array-vocab drop ] when
152 nip '[ _ read-struct-slot ] ;
154 M: struct-class writer-quot
155 nip '[ _ write-struct-slot ] ;
157 : offset-of ( field struct -- offset )
158 struct-slots slot-named offset>> ; inline
162 TUPLE: struct-c-type < abstract-c-type
164 return-in-registers? ;
166 INSTANCE: struct-c-type value-type
168 M: struct-c-type c-type ;
170 M: struct-c-type base-type ;
172 : large-struct? ( type -- ? )
174 { [ dup void? ] [ drop f ] }
175 { [ dup base-type struct-c-type? not ] [ drop f ] }
176 [ return-struct-in-registers? not ]
180 : struct-slot-values-quot ( class -- quot )
182 [ name>> reader-word 1quotation ] map
183 \ cleave [ ] 2sequence
184 \ output>array [ ] 2sequence ;
186 : (define-struct-slot-values-method) ( class -- )
187 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
188 define-inline-method ;
190 : clone-underlying ( struct -- byte-array )
191 binary-object memory>byte-array ; inline
193 : (define-clone-method) ( class -- )
195 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
196 define-inline-method ;
198 :: c-type-for-class ( class slots size align -- c-type )
206 class (unboxer-quot) >>unboxer-quot
207 class (boxer-quot) >>boxer-quot ;
209 GENERIC: compute-slot-offset ( offset class -- offset' )
211 : c-type-align-at ( class offset -- n )
212 0 = [ c-type-align-first ] [ c-type-align ] if ;
214 M: struct-slot-spec compute-slot-offset
215 [ type>> over c-type-align-at 8 * align ] keep
216 [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
218 M: struct-bit-slot-spec compute-slot-offset
219 [ offset<< ] [ bits>> + ] 2bi ;
221 : compute-struct-offsets ( slots -- size )
222 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
224 : compute-union-offsets ( slots -- size )
225 1 [ 0 >>offset type>> heap-size max ] reduce ;
227 : struct-alignment ( slots -- align )
228 [ struct-bit-slot-spec? not ] filter
229 1 [ [ type>> ] [ offset>> ] bi c-type-align-at max ] reduce ;
233 M: struct byte-length class "struct-size" word-prop ; foldable
234 M: struct binary-zero? binary-object <direct-uchar-array> [ 0 = ] all? ; inline
239 : struct-needs-prototype? ( class -- ? )
240 struct-slots [ initial>> binary-zero? ] all? not ;
242 : make-struct-prototype ( class -- prototype )
243 dup struct-needs-prototype? [
244 [ "c-type" word-prop size>> <byte-array> ]
250 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
254 : (struct-methods) ( class -- )
255 [ (define-struct-slot-values-method) ]
256 [ (define-clone-method) ]
259 : check-struct-slots ( slots -- )
260 [ type>> c-type drop ] each ;
262 : redefine-struct-tuple-class ( class -- )
263 [ struct f define-tuple-class ] [ make-final ] bi ;
265 :: (define-struct-class) ( class slots offsets-quot -- )
266 slots empty? [ struct-must-have-slots ] when
267 class redefine-struct-tuple-class
268 slots make-slots dup check-struct-slots :> slot-specs
269 slot-specs offsets-quot call :> unaligned-size
270 slot-specs struct-alignment :> alignment
271 unaligned-size alignment align :> size
273 class slot-specs size alignment c-type-for-class :> c-type
276 class slot-specs define-accessors
277 class size "struct-size" set-word-prop
278 class dup make-struct-prototype "prototype" set-word-prop
279 class (struct-methods) ; inline
282 : define-struct-class ( class slots -- )
283 [ compute-struct-offsets ] (define-struct-class) ;
285 : define-union-struct-class ( class slots -- )
286 [ compute-union-offsets ] (define-struct-class) ;
288 ERROR: invalid-struct-slot token ;
290 : struct-slot-class ( c-type -- class' )
291 c-type c-type-boxed-class
292 dup \ byte-array = [ drop \ c-ptr ] when ;
298 ERROR: bad-type-for-bits type ;
300 :: set-bits ( slot-spec n -- slot-spec )
301 struct-bit-slot-spec new
306 [ bad-type-for-bits ]
308 slot-spec name>> >>name
309 slot-spec class>> >>class
310 slot-spec type>> >>type
311 slot-spec read-only>> >>read-only
312 slot-spec initial>> >>initial ;
314 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
317 { initial: [ [ first >>initial ] [ rest ] bi ] }
318 { read-only [ [ t >>read-only ] dip ] }
319 { bits: [ [ first set-bits ] [ rest ] bi ] }
320 [ bad-slot-attribute ]
326 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
327 [ struct-slot-spec new ] 3dip
329 [ [ >>type ] [ struct-slot-class >>class ] bi ]
330 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
333 : parse-struct-slot ( -- slot )
334 scan scan-c-type \ } parse-until <struct-slot-spec> ;
336 : parse-struct-slots ( slots -- slots' more? )
339 { "{" [ parse-struct-slot suffix! t ] }
340 [ invalid-struct-slot ]
343 : parse-struct-definition ( -- class slots )
344 CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array
345 dup [ name>> ] map check-duplicate-slots ;
349 parse-struct-definition define-struct-class ;
350 SYNTAX: UNION-STRUCT:
351 parse-struct-definition define-union-struct-class ;
354 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
357 scan-word scan-object swap memory>struct suffix! ;
362 : scan-c-type` ( -- c-type/param )
363 scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
365 : parse-struct-slot` ( accum -- accum )
366 scan-string-param scan-c-type` \ } parse-until
367 [ <struct-slot-spec> suffix! ] 3curry append! ;
369 : parse-struct-slots` ( accum -- accum more? )
372 { "{" [ parse-struct-slot` t ] }
373 [ invalid-struct-slot ]
377 FUNCTOR-SYNTAX: STRUCT:
379 [ 8 <vector> ] append!
380 [ parse-struct-slots` ] [ ] while
381 [ >array define-struct-class ] append! ;
383 USING: vocabs vocabs.loader ;
385 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when