1 ! Copyright (C) 2010, 2011 Joe Groff, Daniel Ehrenberg,
2 ! John Benediktsson, Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors alien alien.c-types alien.data alien.parser
5 arrays byte-arrays classes classes.private classes.parser
6 classes.tuple classes.tuple.parser classes.tuple.private
7 combinators combinators.short-circuit combinators.smart
8 cpu.architecture definitions functors.backend fry
9 generalizations generic.parser kernel kernel.private lexer libc
10 locals macros make math math.order parser quotations sequences
11 slots slots.private specialized-arrays vectors words summary
12 namespaces assocs vocabs.parser math.functions
13 classes.struct.bit-accessors bit-arrays
14 stack-checker.dependencies system layouts ;
15 FROM: delegate.private => group-words slot-group-words ;
19 SPECIALIZED-ARRAY: uchar
21 ERROR: struct-must-have-slots ;
23 M: struct-must-have-slots summary
24 drop "Struct definitions must have slots" ;
27 { (underlying) c-ptr read-only } ;
29 ! We hijack the core slots vocab's slot-spec type for struct
30 ! fields. Note that 'offset' is in bits, not bytes, to support
32 TUPLE: struct-slot-spec < slot-spec
35 ! For a struct-bit-slot-spec, offset is in bits, not bytes
36 TUPLE: struct-bit-slot-spec < struct-slot-spec
39 PREDICATE: struct-class < tuple-class
40 superclass \ struct eq? ;
44 : struct-slots ( struct-class -- slots )
45 "c-type" word-prop fields>> ;
47 M: struct-class group-words
48 struct-slots slot-group-words ;
53 2 slot { c-ptr } declare ; inline
57 2dup [ class ] bi@ = [
59 [ [ >c-ptr ] [ binary-object ] bi* memory= ]
60 [ [ >c-ptr not ] both? ]
63 ] [ 2drop f ] if ; inline
67 [ uchar <c-direct-array> hashcode* ] [ 3drop 0 ] if ; inline
69 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
71 : memory>struct ( ptr class -- struct )
72 ! This is sub-optimal if the class is not literal, but gets
73 ! optimized down to efficient code if it is.
74 '[ _ boa ] call( ptr -- struct ) ; inline
77 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
78 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
81 : (malloc-struct) ( class -- struct )
82 [ heap-size malloc ] keep memory>struct ; inline
84 : malloc-struct ( class -- struct )
85 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
87 : (struct) ( class -- struct )
88 [ heap-size (byte-array) ] keep memory>struct ; inline
90 : <struct> ( class -- struct )
91 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
93 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
95 [ <wrapper> \ (struct) [ ] 2sequence ]
99 [ [ name>> setter-word 1quotation ] map \ spread ] bi
101 ] [ ] output>sequence ;
104 : pad-struct-slots ( values class -- values' class )
105 [ struct-slots [ initial>> ] map over length tail append ] keep ;
107 : sign-extend ( n bits -- n' )
109 ! http://guru.multimedia.cx/fast-sign-extension/
110 1 - -1 swap shift [ + ] keep bitxor ; inline
112 : sign-extender ( signed? bits -- quot )
113 '[ _ [ _ sign-extend ] when ] ;
115 GENERIC: (reader-quot) ( slot -- quot )
117 M: struct-slot-spec (reader-quot)
118 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
120 M: struct-bit-slot-spec (reader-quot)
121 [ [ offset>> ] [ bits>> ] bi bit-reader ]
122 [ [ signed?>> ] [ bits>> ] bi sign-extender ]
126 GENERIC: (writer-quot) ( slot -- quot )
128 M: struct-slot-spec (writer-quot)
129 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
131 M: struct-bit-slot-spec (writer-quot)
132 [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
134 : (boxer-quot) ( class -- quot )
135 '[ _ memory>struct ] ;
137 : (unboxer-quot) ( class -- quot )
140 MACRO: read-struct-slot ( slot -- )
141 dup type>> depends-on-c-type
144 MACRO: write-struct-slot ( slot -- )
145 dup type>> depends-on-c-type
149 M: struct-class boa>object
150 swap pad-struct-slots
151 [ <struct> ] [ struct-slots ] bi
152 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
154 M: struct-class initial-value* <struct> ; inline
156 ! Struct slot accessors
158 GENERIC: struct-slot-values ( struct -- sequence )
160 M: struct-class reader-quot
161 dup type>> array? [ dup type>> first define-array-vocab drop ] when
162 nip '[ _ read-struct-slot ] ;
164 M: struct-class writer-quot
165 nip '[ _ write-struct-slot ] ;
167 : offset-of ( field struct -- offset )
168 struct-slots slot-named offset>> ; inline
172 TUPLE: struct-c-type < abstract-c-type
174 return-in-registers? ;
176 INSTANCE: struct-c-type value-type
178 M: struct-c-type c-type ;
180 M: struct-c-type base-type ;
182 : large-struct? ( type -- ? )
184 { [ dup void? ] [ drop f ] }
185 { [ dup base-type struct-c-type? not ] [ drop f ] }
186 [ return-struct-in-registers? not ]
190 : struct-slot-values-quot ( class -- quot )
192 [ name>> reader-word 1quotation ] map
193 \ cleave [ ] 2sequence
194 \ output>array [ ] 2sequence ;
196 : (define-struct-slot-values-method) ( class -- )
197 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
198 define-inline-method ;
200 : clone-underlying ( struct -- byte-array )
201 binary-object memory>byte-array ; inline
203 : (define-clone-method) ( class -- )
205 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
206 define-inline-method ;
208 :: c-type-for-class ( class slots size align -- c-type )
216 class (unboxer-quot) >>unboxer-quot
217 class (boxer-quot) >>boxer-quot ;
219 GENERIC: compute-slot-offset ( offset class -- offset' )
221 : c-type-align-at ( slot-spec offset -- n )
222 over packed?>> [ 2drop 1 ] [
224 0 = [ c-type-align-first ] [ c-type-align ] if
227 M: struct-slot-spec compute-slot-offset
228 [ over c-type-align-at 8 * align ] keep
229 [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
231 M: struct-bit-slot-spec compute-slot-offset
232 [ offset<< ] [ bits>> + ] 2bi ;
234 : compute-struct-offsets ( slots -- size )
235 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
237 : compute-union-offsets ( slots -- size )
238 1 [ 0 >>offset type>> heap-size max ] reduce ;
240 : struct-alignment ( slots -- align )
241 [ struct-bit-slot-spec? not ] filter
242 1 [ dup offset>> c-type-align-at max ] reduce ;
246 M: struct byte-length class "struct-size" word-prop ; foldable
247 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
252 : struct-needs-prototype? ( class -- ? )
253 struct-slots [ initial>> binary-zero? ] all? not ;
255 : make-struct-prototype ( class -- prototype )
256 dup struct-needs-prototype? [
257 [ "c-type" word-prop size>> <byte-array> ]
263 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
267 : (struct-methods) ( class -- )
268 [ (define-struct-slot-values-method) ]
269 [ (define-clone-method) ]
272 : check-struct-slots ( slots -- )
273 [ type>> c-type drop ] each ;
275 : redefine-struct-tuple-class ( class -- )
276 [ struct f define-tuple-class ] [ make-final ] bi ;
278 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
279 slot-specs check-struct-slots
280 slot-specs empty? [ struct-must-have-slots ] when
281 class redefine-struct-tuple-class
282 slot-specs offsets-quot call :> unaligned-size
283 slot-specs alignment-quot call :> alignment
284 unaligned-size alignment align :> size
286 class slot-specs size alignment c-type-for-class :> c-type
289 class slot-specs define-accessors
290 class size "struct-size" set-word-prop
291 class dup make-struct-prototype "prototype" set-word-prop
292 class (struct-methods) ; inline
294 : make-packed-slots ( slots -- slot-specs )
295 make-slots [ t >>packed? ] map! ;
299 : define-struct-class ( class slots -- )
301 [ compute-struct-offsets ] [ struct-alignment ]
302 (define-struct-class) ;
304 : define-packed-struct-class ( class slots -- )
306 [ compute-struct-offsets ] [ drop 1 ]
307 (define-struct-class) ;
309 : define-union-struct-class ( class slots -- )
311 [ compute-union-offsets ] [ struct-alignment ]
312 (define-struct-class) ;
314 ERROR: invalid-struct-slot token ;
316 : struct-slot-class ( c-type -- class' )
317 c-type c-type-boxed-class
318 dup \ byte-array = [ drop \ c-ptr ] when ;
324 ERROR: bad-type-for-bits type ;
326 :: set-bits ( slot-spec n -- slot-spec )
327 struct-bit-slot-spec new
332 [ bad-type-for-bits ]
334 slot-spec name>> >>name
335 slot-spec class>> >>class
336 slot-spec type>> >>type
337 slot-spec read-only>> >>read-only
338 slot-spec initial>> >>initial ;
340 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
343 { initial: [ [ first >>initial ] [ rest ] bi ] }
344 { read-only [ [ t >>read-only ] dip ] }
345 { bits: [ [ first set-bits ] [ rest ] bi ] }
346 [ bad-slot-attribute ]
352 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
353 [ struct-slot-spec new ] 3dip
355 [ [ >>type ] [ struct-slot-class >>class ] bi ]
356 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
359 : parse-struct-slot ( -- slot )
360 scan scan-c-type \ } parse-until <struct-slot-spec> ;
362 : parse-struct-slots ( slots -- slots' more? )
365 { "{" [ parse-struct-slot suffix! t ] }
366 [ invalid-struct-slot ]
369 : parse-struct-definition ( -- class slots )
370 CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array
371 dup [ name>> ] map check-duplicate-slots ;
375 parse-struct-definition define-struct-class ;
377 SYNTAX: PACKED-STRUCT:
378 parse-struct-definition define-packed-struct-class ;
380 SYNTAX: UNION-STRUCT:
381 parse-struct-definition define-union-struct-class ;
384 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
387 scan-word scan-object swap memory>struct suffix! ;
392 : scan-c-type` ( -- c-type/param )
393 scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
395 : parse-struct-slot` ( accum -- accum )
396 scan-string-param scan-c-type` \ } parse-until
397 [ <struct-slot-spec> suffix! ] 3curry append! ;
399 : parse-struct-slots` ( accum -- accum more? )
402 { "{" [ parse-struct-slot` t ] }
403 [ invalid-struct-slot ]
408 FUNCTOR-SYNTAX: STRUCT:
410 [ 8 <vector> ] append!
411 [ parse-struct-slots` ] [ ] while
412 [ >array define-struct-class ] append! ;
414 USING: vocabs vocabs.loader ;
416 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when