1 ! (c)Joe Groff, Daniel Ehrenberg bsd license
2 USING: accessors alien alien.c-types alien.data alien.parser arrays
3 byte-arrays classes classes.parser classes.tuple classes.tuple.parser
4 classes.tuple.private combinators combinators.short-circuit
5 combinators.smart cpu.architecture definitions functors.backend
6 fry generalizations generic.parser kernel kernel.private lexer
7 libc locals macros make math math.order parser quotations
8 sequences slots slots.private specialized-arrays vectors words
9 summary namespaces assocs vocabs.parser math.functions
10 classes.struct.bit-accessors bit-arrays ;
14 SPECIALIZED-ARRAY: uchar
16 ERROR: struct-must-have-slots ;
18 M: struct-must-have-slots summary
19 drop "Struct definitions must have slots" ;
22 { (underlying) c-ptr read-only } ;
24 TUPLE: struct-slot-spec < slot-spec
27 ! For a struct-bit-slot-spec, offset is in bits, not bytes
28 TUPLE: struct-bit-slot-spec < struct-slot-spec
31 PREDICATE: struct-class < tuple-class
32 superclass \ struct eq? ;
34 M: struct-class valid-superclass? drop f ;
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 )
130 M: struct-class boa>object
131 swap pad-struct-slots
132 [ <struct> ] [ struct-slots ] bi
133 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
135 M: struct-class initial-value* <struct> ; inline
137 ! Struct slot accessors
139 GENERIC: struct-slot-values ( struct -- sequence )
141 M: struct-class reader-quot
144 M: struct-class writer-quot
147 : offset-of ( field struct -- offset )
148 struct-slots slot-named offset>> ; inline
152 TUPLE: struct-c-type < abstract-c-type
154 return-in-registers? ;
156 INSTANCE: struct-c-type value-type
158 M: struct-c-type c-type ;
160 M: struct-c-type c-type-stack-align? drop f ;
162 : if-value-struct ( ctype true false -- )
163 [ dup value-struct? ] 2dip '[ drop void* @ ] if ; inline
165 M: struct-c-type unbox-parameter
166 [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
168 M: struct-c-type box-parameter
169 [ %box-large-struct ] [ box-parameter ] if-value-struct ;
171 : if-small-struct ( c-type true false -- ? )
172 [ dup return-struct-in-registers? ] 2dip '[ f swap @ ] if ; inline
174 M: struct-c-type unbox-return
175 [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
177 M: struct-c-type box-return
178 [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
180 M: struct-c-type stack-size
181 [ heap-size ] [ stack-size ] if-value-struct ;
183 M: struct-c-type c-struct? drop t ;
186 : struct-slot-values-quot ( class -- quot )
188 [ name>> reader-word 1quotation ] map
189 \ cleave [ ] 2sequence
190 \ output>array [ ] 2sequence ;
192 : define-inline-method ( class generic quot -- )
193 [ create-method-in ] dip [ define ] [ drop make-inline ] 2bi ;
195 : (define-struct-slot-values-method) ( class -- )
196 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
197 define-inline-method ;
199 : clone-underlying ( struct -- byte-array )
200 [ >c-ptr ] [ byte-length ] bi memory>byte-array ; inline
202 : (define-clone-method) ( class -- )
204 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
205 define-inline-method ;
207 :: c-type-for-class ( class slots size align -- c-type )
215 class (unboxer-quot) >>unboxer-quot
216 class (boxer-quot) >>boxer-quot ;
218 GENERIC: compute-slot-offset ( offset class -- offset' )
220 : c-type-align-at ( class offset -- n )
221 0 = [ c-type-align-first ] [ c-type-align ] if ;
223 M: struct-slot-spec compute-slot-offset
224 [ type>> over c-type-align-at 8 * align ] keep
225 [ [ 8 /i ] dip (>>offset) ] [ type>> heap-size 8 * + ] 2bi ;
227 M: struct-bit-slot-spec compute-slot-offset
228 [ (>>offset) ] [ bits>> + ] 2bi ;
230 : compute-struct-offsets ( slots -- size )
231 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
233 : compute-union-offsets ( slots -- size )
234 1 [ 0 >>offset type>> heap-size max ] reduce ;
236 : struct-alignment ( slots -- align )
237 [ struct-bit-slot-spec? not ] filter
238 1 [ [ type>> ] [ offset>> ] bi c-type-align-at max ] reduce ;
242 M: struct byte-length class "struct-size" word-prop ; foldable
247 GENERIC: binary-zero? ( value -- ? )
249 M: object binary-zero? drop f ;
250 M: f binary-zero? drop t ;
251 M: number binary-zero? 0 = ;
252 M: struct binary-zero? >c-ptr [ 0 = ] all? ;
254 : struct-needs-prototype? ( class -- ? )
255 struct-slots [ initial>> binary-zero? ] all? not ;
257 : make-struct-prototype ( class -- prototype )
258 dup struct-needs-prototype? [
259 [ "c-type" word-prop size>> <byte-array> ]
265 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
269 : (struct-methods) ( class -- )
270 [ (define-struct-slot-values-method) ]
271 [ (define-clone-method) ]
274 : check-struct-slots ( slots -- )
275 [ type>> c-type drop ] each ;
277 : redefine-struct-tuple-class ( class -- )
278 [ dup class? [ forget-class ] [ drop ] if ] [ struct f define-tuple-class ] bi ;
280 :: (define-struct-class) ( class slots offsets-quot -- )
281 slots empty? [ struct-must-have-slots ] when
282 class redefine-struct-tuple-class
283 slots make-slots dup check-struct-slots :> slot-specs
284 slot-specs offsets-quot call :> unaligned-size
285 slot-specs struct-alignment :> alignment
286 unaligned-size alignment align :> size
288 class slot-specs size alignment c-type-for-class :> c-type
291 class slot-specs define-accessors
292 class size "struct-size" set-word-prop
293 class dup make-struct-prototype "prototype" set-word-prop
294 class (struct-methods) ; inline
297 : define-struct-class ( class slots -- )
298 [ compute-struct-offsets ] (define-struct-class) ;
300 : define-union-struct-class ( class slots -- )
301 [ compute-union-offsets ] (define-struct-class) ;
303 M: struct-class reset-class
304 [ call-next-method ] [ name>> c-types get delete-at ] bi ;
306 ERROR: invalid-struct-slot token ;
308 : struct-slot-class ( c-type -- class' )
309 c-type c-type-boxed-class
310 dup \ byte-array = [ drop \ c-ptr ] when ;
316 ERROR: bad-type-for-bits type ;
318 :: set-bits ( slot-spec n -- slot-spec )
319 struct-bit-slot-spec new
324 [ bad-type-for-bits ]
326 slot-spec name>> >>name
327 slot-spec class>> >>class
328 slot-spec type>> >>type
329 slot-spec read-only>> >>read-only
330 slot-spec initial>> >>initial ;
332 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
335 { initial: [ [ first >>initial ] [ rest ] bi ] }
336 { read-only [ [ t >>read-only ] dip ] }
337 { bits: [ [ first set-bits ] [ rest ] bi ] }
338 [ bad-slot-attribute ]
344 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
345 [ struct-slot-spec new ] 3dip
347 [ [ >>type ] [ struct-slot-class >>class ] bi ]
348 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
351 : parse-struct-slot ( -- slot )
352 scan scan-c-type \ } parse-until <struct-slot-spec> ;
354 : parse-struct-slots ( slots -- slots' more? )
357 { "{" [ parse-struct-slot suffix! t ] }
358 { f [ unexpected-eof ] }
359 [ invalid-struct-slot ]
362 : parse-struct-definition ( -- class slots )
363 CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array ;
367 parse-struct-definition define-struct-class ;
368 SYNTAX: UNION-STRUCT:
369 parse-struct-definition define-union-struct-class ;
372 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
375 scan-word scan-object swap memory>struct suffix! ;
380 : scan-c-type` ( -- c-type/param )
381 scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
383 : parse-struct-slot` ( accum -- accum )
384 scan-string-param scan-c-type` \ } parse-until
385 [ <struct-slot-spec> suffix! ] 3curry append! ;
387 : parse-struct-slots` ( accum -- accum more? )
390 { "{" [ parse-struct-slot` t ] }
391 [ invalid-struct-slot ]
395 FUNCTOR-SYNTAX: STRUCT:
397 [ 8 <vector> ] append!
398 [ parse-struct-slots` ] [ ] while
399 [ >array define-struct-class ] append! ;
401 USING: vocabs vocabs.loader ;
403 "prettyprint" vocab [ "classes.struct.prettyprint" require ] when