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 ;
16 SPECIALIZED-ARRAY: uchar
18 ERROR: struct-must-have-slots ;
20 M: struct-must-have-slots summary
21 drop "Struct definitions must have slots" ;
24 { (underlying) c-ptr read-only } ;
26 TUPLE: struct-slot-spec < slot-spec
29 ! For a struct-bit-slot-spec, offset is in bits, not bytes
30 TUPLE: struct-bit-slot-spec < struct-slot-spec
33 PREDICATE: struct-class < tuple-class
34 superclass \ struct eq? ;
38 : struct-slots ( struct-class -- slots )
39 "c-type" word-prop fields>> ;
44 2 slot { c-ptr } declare ; inline
49 [ [ >c-ptr ] [ [ >c-ptr ] [ byte-length ] bi ] bi* memory= ]
53 [ >c-ptr ] [ byte-length ] bi <direct-uchar-array> hashcode* ; inline
55 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
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
63 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
64 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
67 : (malloc-struct) ( class -- struct )
68 [ heap-size malloc ] keep memory>struct ; inline
70 : malloc-struct ( class -- struct )
71 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
73 : (struct) ( class -- struct )
74 [ heap-size (byte-array) ] keep memory>struct ; inline
76 : <struct> ( class -- struct )
77 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
79 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
81 [ <wrapper> \ (struct) [ ] 2sequence ]
85 [ [ name>> setter-word 1quotation ] map \ spread ] bi
87 ] [ ] output>sequence ;
90 : pad-struct-slots ( values class -- values' class )
91 [ struct-slots [ initial>> ] map over length tail append ] keep ;
93 : sign-extend ( n bits -- n' )
95 ! http://guru.multimedia.cx/fast-sign-extension/
96 1 - -1 swap shift [ + ] keep bitxor ; inline
98 : sign-extender ( signed? bits -- quot )
99 '[ _ [ _ sign-extend ] when ] ;
101 GENERIC: (reader-quot) ( slot -- quot )
103 M: struct-slot-spec (reader-quot)
104 [ type>> c-type-getter-boxer ]
105 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
107 M: struct-bit-slot-spec (reader-quot)
108 [ [ offset>> ] [ bits>> ] bi bit-reader ]
109 [ [ signed?>> ] [ bits>> ] bi sign-extender ]
113 GENERIC: (writer-quot) ( slot -- quot )
115 M: struct-slot-spec (writer-quot)
117 [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
119 M: struct-bit-slot-spec (writer-quot)
120 [ offset>> ] [ bits>> ] bi bit-writer
123 : (boxer-quot) ( class -- quot )
124 '[ _ memory>struct ] ;
126 : (unboxer-quot) ( class -- quot )
129 MACRO: read-struct-slot ( slot -- )
130 dup type>> depends-on-c-type
133 MACRO: write-struct-slot ( slot -- )
134 dup type>> depends-on-c-type
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 ;
143 M: struct-class initial-value* <struct> ; inline
145 ! Struct slot accessors
147 GENERIC: struct-slot-values ( struct -- sequence )
149 M: struct-class reader-quot
150 dup array? [ dup first define-array-vocab drop ] when
151 nip '[ _ read-struct-slot ] ;
153 M: struct-class writer-quot
154 nip '[ _ write-struct-slot ] ;
156 : offset-of ( field struct -- offset )
157 struct-slots slot-named offset>> ; inline
161 TUPLE: struct-c-type < abstract-c-type
163 return-in-registers? ;
165 INSTANCE: struct-c-type value-type
167 M: struct-c-type c-type ;
169 M: struct-c-type c-type-stack-align? drop f ;
171 : if-value-struct ( ctype true false -- )
172 [ dup value-struct? ] 2dip '[ drop void* @ ] if ; inline
174 M: struct-c-type unbox-parameter
175 [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
177 M: struct-c-type box-parameter
178 [ %box-large-struct ] [ box-parameter ] if-value-struct ;
180 : if-small-struct ( c-type true false -- ? )
181 [ dup return-struct-in-registers? ] 2dip '[ f swap @ ] if ; inline
183 M: struct-c-type unbox-return
184 [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
186 M: struct-c-type box-return
187 [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
189 M: struct-c-type stack-size
190 [ heap-size ] [ stack-size ] if-value-struct ;
192 M: struct-c-type c-struct? drop t ;
195 : struct-slot-values-quot ( class -- quot )
197 [ name>> reader-word 1quotation ] map
198 \ cleave [ ] 2sequence
199 \ output>array [ ] 2sequence ;
201 : (define-struct-slot-values-method) ( class -- )
202 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
203 define-inline-method ;
205 : clone-underlying ( struct -- byte-array )
206 [ >c-ptr ] [ byte-length ] bi memory>byte-array ; inline
208 : (define-clone-method) ( class -- )
210 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
211 define-inline-method ;
213 :: c-type-for-class ( class slots size align -- c-type )
221 class (unboxer-quot) >>unboxer-quot
222 class (boxer-quot) >>boxer-quot ;
224 GENERIC: compute-slot-offset ( offset class -- offset' )
226 : c-type-align-at ( class offset -- n )
227 0 = [ c-type-align-first ] [ c-type-align ] if ;
229 M: struct-slot-spec compute-slot-offset
230 [ type>> over c-type-align-at 8 * align ] keep
231 [ [ 8 /i ] dip (>>offset) ] [ type>> heap-size 8 * + ] 2bi ;
233 M: struct-bit-slot-spec compute-slot-offset
234 [ (>>offset) ] [ bits>> + ] 2bi ;
236 : compute-struct-offsets ( slots -- size )
237 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
239 : compute-union-offsets ( slots -- size )
240 1 [ 0 >>offset type>> heap-size max ] reduce ;
242 : struct-alignment ( slots -- align )
243 [ struct-bit-slot-spec? not ] filter
244 1 [ [ type>> ] [ offset>> ] bi c-type-align-at max ] reduce ;
248 M: struct byte-length class "struct-size" word-prop ; foldable
253 GENERIC: binary-zero? ( value -- ? )
255 M: object binary-zero? drop f ;
256 M: f binary-zero? drop t ;
257 M: number binary-zero? 0 = ;
258 M: struct binary-zero? >c-ptr [ 0 = ] all? ;
260 : struct-needs-prototype? ( class -- ? )
261 struct-slots [ initial>> binary-zero? ] all? not ;
263 : make-struct-prototype ( class -- prototype )
264 dup struct-needs-prototype? [
265 [ "c-type" word-prop size>> <byte-array> ]
271 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
275 : (struct-methods) ( class -- )
276 [ (define-struct-slot-values-method) ]
277 [ (define-clone-method) ]
280 : check-struct-slots ( slots -- )
281 [ type>> c-type drop ] each ;
283 : redefine-struct-tuple-class ( class -- )
284 [ struct f define-tuple-class ] [ make-final ] bi ;
286 :: (define-struct-class) ( class slots offsets-quot -- )
287 slots empty? [ struct-must-have-slots ] when
288 class redefine-struct-tuple-class
289 slots make-slots dup check-struct-slots :> slot-specs
290 slot-specs offsets-quot call :> unaligned-size
291 slot-specs struct-alignment :> alignment
292 unaligned-size alignment align :> size
294 class slot-specs size alignment c-type-for-class :> c-type
297 class slot-specs define-accessors
298 class size "struct-size" set-word-prop
299 class dup make-struct-prototype "prototype" set-word-prop
300 class (struct-methods) ; inline
303 : define-struct-class ( class slots -- )
304 [ compute-struct-offsets ] (define-struct-class) ;
306 : define-union-struct-class ( class slots -- )
307 [ compute-union-offsets ] (define-struct-class) ;
309 ERROR: invalid-struct-slot token ;
311 : struct-slot-class ( c-type -- class' )
312 c-type c-type-boxed-class
313 dup \ byte-array = [ drop \ c-ptr ] when ;
319 ERROR: bad-type-for-bits type ;
321 :: set-bits ( slot-spec n -- slot-spec )
322 struct-bit-slot-spec new
327 [ bad-type-for-bits ]
329 slot-spec name>> >>name
330 slot-spec class>> >>class
331 slot-spec type>> >>type
332 slot-spec read-only>> >>read-only
333 slot-spec initial>> >>initial ;
335 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
338 { initial: [ [ first >>initial ] [ rest ] bi ] }
339 { read-only [ [ t >>read-only ] dip ] }
340 { bits: [ [ first set-bits ] [ rest ] bi ] }
341 [ bad-slot-attribute ]
347 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
348 [ struct-slot-spec new ] 3dip
350 [ [ >>type ] [ struct-slot-class >>class ] bi ]
351 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
354 : parse-struct-slot ( -- slot )
355 scan scan-c-type \ } parse-until <struct-slot-spec> ;
357 : parse-struct-slots ( slots -- slots' more? )
360 { "{" [ parse-struct-slot suffix! t ] }
361 { f [ unexpected-eof ] }
362 [ invalid-struct-slot ]
365 : parse-struct-definition ( -- class slots )
366 CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array ;
370 parse-struct-definition define-struct-class ;
371 SYNTAX: UNION-STRUCT:
372 parse-struct-definition define-union-struct-class ;
375 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
378 scan-word scan-object swap memory>struct suffix! ;
383 : scan-c-type` ( -- c-type/param )
384 scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
386 : parse-struct-slot` ( accum -- accum )
387 scan-string-param scan-c-type` \ } parse-until
388 [ <struct-slot-spec> suffix! ] 3curry append! ;
390 : parse-struct-slots` ( accum -- accum more? )
393 { "{" [ parse-struct-slot` t ] }
394 [ invalid-struct-slot ]
398 FUNCTOR-SYNTAX: STRUCT:
400 [ 8 <vector> ] append!
401 [ parse-struct-slots` ] [ ] while
402 [ >array define-struct-class ] append! ;
404 USING: vocabs vocabs.loader ;
406 "prettyprint" vocab [ "classes.struct.prettyprint" require ] when