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 ;
18 SPECIALIZED-ARRAY: uchar
20 ERROR: struct-must-have-slots ;
22 M: struct-must-have-slots summary
23 drop "Struct definitions must have slots" ;
26 { (underlying) c-ptr read-only } ;
28 TUPLE: struct-slot-spec < slot-spec
31 ! For a struct-bit-slot-spec, offset is in bits, not bytes
32 TUPLE: struct-bit-slot-spec < struct-slot-spec
35 PREDICATE: struct-class < tuple-class
36 superclass \ struct eq? ;
40 : struct-slots ( struct-class -- slots )
41 "c-type" word-prop fields>> ;
43 M: struct-class group-words
44 struct-slots slot-group-words ;
49 2 slot { c-ptr } declare ; inline
54 [ [ >c-ptr ] [ binary-object ] bi* memory= ]
58 binary-object <direct-uchar-array> hashcode* ; inline
60 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
62 : memory>struct ( ptr class -- struct )
63 ! This is sub-optimal if the class is not literal, but gets
64 ! optimized down to efficient code if it is.
65 '[ _ boa ] call( ptr -- struct ) ; inline
68 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
69 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
72 : (malloc-struct) ( class -- struct )
73 [ heap-size malloc ] keep memory>struct ; inline
75 : malloc-struct ( class -- struct )
76 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
78 : (struct) ( class -- struct )
79 [ heap-size (byte-array) ] keep memory>struct ; inline
81 : <struct> ( class -- struct )
82 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
84 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
86 [ <wrapper> \ (struct) [ ] 2sequence ]
90 [ [ name>> setter-word 1quotation ] map \ spread ] bi
92 ] [ ] output>sequence ;
95 : pad-struct-slots ( values class -- values' class )
96 [ struct-slots [ initial>> ] map over length tail append ] keep ;
98 : sign-extend ( n bits -- n' )
100 ! http://guru.multimedia.cx/fast-sign-extension/
101 1 - -1 swap shift [ + ] keep bitxor ; inline
103 : sign-extender ( signed? bits -- quot )
104 '[ _ [ _ sign-extend ] when ] ;
106 GENERIC: (reader-quot) ( slot -- quot )
108 M: struct-slot-spec (reader-quot)
109 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
111 M: struct-bit-slot-spec (reader-quot)
112 [ [ offset>> ] [ bits>> ] bi bit-reader ]
113 [ [ signed?>> ] [ bits>> ] bi sign-extender ]
117 GENERIC: (writer-quot) ( slot -- quot )
119 M: struct-slot-spec (writer-quot)
120 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
122 M: struct-bit-slot-spec (writer-quot)
123 [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
125 : (boxer-quot) ( class -- quot )
126 '[ _ memory>struct ] ;
128 : (unboxer-quot) ( class -- quot )
131 MACRO: read-struct-slot ( slot -- )
132 dup type>> depends-on-c-type
135 MACRO: write-struct-slot ( slot -- )
136 dup type>> depends-on-c-type
140 M: struct-class boa>object
141 swap pad-struct-slots
142 [ <struct> ] [ struct-slots ] bi
143 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
145 M: struct-class initial-value* <struct> ; inline
147 ! Struct slot accessors
149 GENERIC: struct-slot-values ( struct -- sequence )
151 M: struct-class reader-quot
152 dup type>> array? [ dup type>> first define-array-vocab drop ] when
153 nip '[ _ read-struct-slot ] ;
155 M: struct-class writer-quot
156 nip '[ _ write-struct-slot ] ;
158 : offset-of ( field struct -- offset )
159 struct-slots slot-named offset>> ; inline
163 TUPLE: struct-c-type < abstract-c-type
165 return-in-registers? ;
167 INSTANCE: struct-c-type value-type
169 M: struct-c-type c-type ;
171 M: struct-c-type base-type ;
173 : large-struct? ( type -- ? )
175 { [ dup void? ] [ drop f ] }
176 { [ dup base-type struct-c-type? not ] [ drop f ] }
177 [ return-struct-in-registers? not ]
181 : struct-slot-values-quot ( class -- quot )
183 [ name>> reader-word 1quotation ] map
184 \ cleave [ ] 2sequence
185 \ output>array [ ] 2sequence ;
187 : (define-struct-slot-values-method) ( class -- )
188 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
189 define-inline-method ;
191 : clone-underlying ( struct -- byte-array )
192 binary-object memory>byte-array ; inline
194 : (define-clone-method) ( class -- )
196 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
197 define-inline-method ;
199 :: c-type-for-class ( class slots size align -- c-type )
207 class (unboxer-quot) >>unboxer-quot
208 class (boxer-quot) >>boxer-quot ;
210 GENERIC: compute-slot-offset ( offset class -- offset' )
212 : c-type-align-at ( class offset -- n )
213 0 = [ c-type-align-first ] [ c-type-align ] if ;
215 M: struct-slot-spec compute-slot-offset
216 [ type>> over c-type-align-at 8 * align ] keep
217 [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
219 M: struct-bit-slot-spec compute-slot-offset
220 [ offset<< ] [ bits>> + ] 2bi ;
222 : compute-struct-offsets ( slots -- size )
223 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
225 : compute-union-offsets ( slots -- size )
226 1 [ 0 >>offset type>> heap-size max ] reduce ;
228 : struct-alignment ( slots -- align )
229 [ struct-bit-slot-spec? not ] filter
230 1 [ [ type>> ] [ offset>> ] bi c-type-align-at max ] reduce ;
234 M: struct byte-length class "struct-size" word-prop ; foldable
239 GENERIC: binary-zero? ( value -- ? )
241 M: object binary-zero? drop f ; inline
242 M: f binary-zero? drop t ; inline
243 M: integer binary-zero? zero? ; inline
244 M: float binary-zero? double>bits zero? ; inline
245 M: complex binary-zero? >rect [ binary-zero? ] both? ; inline
246 M: struct binary-zero? binary-object <direct-uchar-array> [ 0 = ] all? ; inline
248 : struct-needs-prototype? ( class -- ? )
249 struct-slots [ initial>> binary-zero? ] all? not ;
251 : make-struct-prototype ( class -- prototype )
252 dup struct-needs-prototype? [
253 [ "c-type" word-prop size>> <byte-array> ]
259 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
263 : (struct-methods) ( class -- )
264 [ (define-struct-slot-values-method) ]
265 [ (define-clone-method) ]
268 : check-struct-slots ( slots -- )
269 [ type>> c-type drop ] each ;
271 : redefine-struct-tuple-class ( class -- )
272 [ struct f define-tuple-class ] [ make-final ] bi ;
274 :: (define-struct-class) ( class slots offsets-quot -- )
275 slots empty? [ struct-must-have-slots ] when
276 class redefine-struct-tuple-class
277 slots make-slots dup check-struct-slots :> slot-specs
278 slot-specs offsets-quot call :> unaligned-size
279 slot-specs struct-alignment :> alignment
280 unaligned-size alignment align :> size
282 class slot-specs size alignment c-type-for-class :> c-type
285 class slot-specs define-accessors
286 class size "struct-size" set-word-prop
287 class dup make-struct-prototype "prototype" set-word-prop
288 class (struct-methods) ; inline
291 : define-struct-class ( class slots -- )
292 [ compute-struct-offsets ] (define-struct-class) ;
294 : define-union-struct-class ( class slots -- )
295 [ compute-union-offsets ] (define-struct-class) ;
297 ERROR: invalid-struct-slot token ;
299 : struct-slot-class ( c-type -- class' )
300 c-type c-type-boxed-class
301 dup \ byte-array = [ drop \ c-ptr ] when ;
307 ERROR: bad-type-for-bits type ;
309 :: set-bits ( slot-spec n -- slot-spec )
310 struct-bit-slot-spec new
315 [ bad-type-for-bits ]
317 slot-spec name>> >>name
318 slot-spec class>> >>class
319 slot-spec type>> >>type
320 slot-spec read-only>> >>read-only
321 slot-spec initial>> >>initial ;
323 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
326 { initial: [ [ first >>initial ] [ rest ] bi ] }
327 { read-only [ [ t >>read-only ] dip ] }
328 { bits: [ [ first set-bits ] [ rest ] bi ] }
329 [ bad-slot-attribute ]
335 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
336 [ struct-slot-spec new ] 3dip
338 [ [ >>type ] [ struct-slot-class >>class ] bi ]
339 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
342 : parse-struct-slot ( -- slot )
343 scan scan-c-type \ } parse-until <struct-slot-spec> ;
345 : parse-struct-slots ( slots -- slots' more? )
348 { "{" [ parse-struct-slot suffix! t ] }
349 { f [ unexpected-eof ] }
350 [ invalid-struct-slot ]
353 : parse-struct-definition ( -- class slots )
354 CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array
355 dup [ name>> ] map check-duplicate-slots ;
359 parse-struct-definition define-struct-class ;
360 SYNTAX: UNION-STRUCT:
361 parse-struct-definition define-union-struct-class ;
364 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
367 scan-word scan-object swap memory>struct suffix! ;
372 : scan-c-type` ( -- c-type/param )
373 scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
375 : parse-struct-slot` ( accum -- accum )
376 scan-string-param scan-c-type` \ } parse-until
377 [ <struct-slot-spec> suffix! ] 3curry append! ;
379 : parse-struct-slots` ( accum -- accum more? )
382 { "{" [ parse-struct-slot` t ] }
383 [ invalid-struct-slot ]
387 FUNCTOR-SYNTAX: STRUCT:
389 [ 8 <vector> ] append!
390 [ parse-struct-slots` ] [ ] while
391 [ >array define-struct-class ] append! ;
393 USING: vocabs vocabs.loader ;
395 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when