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.parser
6 classes.struct.bit-accessors classes.tuple classes.tuple.parser
7 combinators combinators.smart cpu.architecture fry
8 functors.backend generalizations generic.parser kernel
9 kernel.private lexer libc locals macros math math.order parser
10 quotations sequences slots slots.private specialized-arrays
11 stack-checker.dependencies summary vectors vocabs.parser words
12 classes.private generic definitions ;
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 ! We hijack the core slots vocab's slot-spec type for struct
28 ! fields. Note that 'offset' is in bits, not bytes, to support
30 TUPLE: struct-slot-spec < slot-spec
33 ! For a struct-bit-slot-spec, offset is in bits, not bytes
34 TUPLE: struct-bit-slot-spec < struct-slot-spec
37 PREDICATE: struct-class < tuple-class
38 superclass \ struct eq? ;
42 : struct-slots ( struct-class -- slots )
43 "c-type" word-prop fields>> ;
45 M: struct-class group-words
46 struct-slots slot-group-words ;
51 2 slot { c-ptr } declare ; inline
55 2dup [ class-of ] bi@ = [
57 [ [ >c-ptr ] [ binary-object ] bi* memory= ]
58 [ [ >c-ptr not ] both? ]
61 ] [ 2drop f ] if ; inline
65 [ uchar <c-direct-array> hashcode* ] [ 3drop 0 ] if ; inline
67 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
69 : memory>struct ( ptr class -- struct )
70 ! This is sub-optimal if the class is not literal, but gets
71 ! optimized down to efficient code if it is.
72 '[ _ boa ] call( ptr -- struct ) ; inline
75 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
76 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
79 : (malloc-struct) ( class -- struct )
80 [ heap-size malloc ] keep memory>struct ; inline
82 : malloc-struct ( class -- struct )
83 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
85 : (struct) ( class -- struct )
86 [ heap-size (byte-array) ] keep memory>struct ; inline
88 : <struct> ( class -- struct )
89 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
91 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
93 [ <wrapper> \ (struct) [ ] 2sequence ]
97 [ [ name>> setter-word 1quotation ] map \ spread ] bi
99 ] [ ] output>sequence ;
102 : pad-struct-slots ( values class -- values' class )
103 [ struct-slots [ initial>> ] map over length tail append ] keep ;
105 : sign-extend ( n bits -- n' )
107 ! http://guru.multimedia.cx/fast-sign-extension/
108 1 - -1 swap shift [ + ] keep bitxor ; inline
110 : sign-extender ( signed? bits -- quot )
111 '[ _ [ _ sign-extend ] when ] ;
113 GENERIC: (reader-quot) ( slot -- quot )
115 M: struct-slot-spec (reader-quot)
116 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
118 M: struct-bit-slot-spec (reader-quot)
119 [ [ offset>> ] [ bits>> ] bi bit-reader ]
120 [ [ signed?>> ] [ bits>> ] bi sign-extender ]
124 GENERIC: (writer-quot) ( slot -- quot )
126 M: struct-slot-spec (writer-quot)
127 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
129 M: struct-bit-slot-spec (writer-quot)
130 [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
132 : (boxer-quot) ( class -- quot )
133 '[ _ memory>struct ] ;
135 : (unboxer-quot) ( class -- quot )
138 MACRO: read-struct-slot ( slot -- )
139 dup type>> depends-on-c-type
142 MACRO: write-struct-slot ( slot -- )
143 dup type>> depends-on-c-type
147 M: struct-class boa>object
148 swap pad-struct-slots
149 [ <struct> ] [ struct-slots ] bi
150 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
152 M: struct-class initial-value* <struct> ; inline
154 ! Struct slot accessors
156 GENERIC: struct-slot-values ( struct -- sequence )
158 M: struct-class reader-quot
159 dup type>> array? [ dup type>> first define-array-vocab drop ] when
160 nip '[ _ read-struct-slot ] ;
162 M: struct-class writer-quot
163 nip '[ _ write-struct-slot ] ;
165 : offset-of ( field struct -- offset )
166 struct-slots slot-named offset>> ; inline
170 TUPLE: struct-c-type < abstract-c-type
172 return-in-registers? ;
174 INSTANCE: struct-c-type value-type
176 ! M: struct-c-type c-type ;
177 M: struct-c-type lookup-c-type ;
179 M: struct-c-type base-type ;
181 : large-struct? ( type -- ? )
183 { [ dup void? ] [ drop f ] }
184 { [ dup base-type struct-c-type? not ] [ drop f ] }
185 [ return-struct-in-registers? not ]
189 : struct-slot-values-quot ( class -- quot )
191 [ name>> reader-word 1quotation ] map
192 \ cleave [ ] 2sequence
193 \ output>array [ ] 2sequence ;
195 : (define-struct-slot-values-method) ( class -- )
196 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
197 define-inline-method ;
199 : forget-struct-slot-values-method ( class -- )
200 \ struct-slot-values ?lookup-method forget ;
202 : clone-underlying ( struct -- byte-array )
203 binary-object memory>byte-array ; inline
205 : (define-clone-method) ( class -- )
207 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
208 define-inline-method ;
210 : forget-clone-method ( class -- )
211 \ clone ?lookup-method forget ;
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 ( slot-spec offset -- n )
227 over packed?>> [ 2drop 1 ] [
229 0 = [ c-type-align-first ] [ c-type-align ] if
232 M: struct-slot-spec compute-slot-offset
233 [ over c-type-align-at 8 * align ] keep
234 [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
236 M: struct-bit-slot-spec compute-slot-offset
237 [ offset<< ] [ bits>> + ] 2bi ;
239 : compute-struct-offsets ( slots -- size )
240 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
242 : compute-union-offsets ( slots -- size )
243 1 [ 0 >>offset type>> heap-size max ] reduce ;
245 : struct-alignment ( slots -- align )
246 [ struct-bit-slot-spec? not ] filter
247 1 [ dup offset>> c-type-align-at max ] reduce ;
251 M: struct byte-length class-of "struct-size" word-prop ; foldable
252 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
257 : struct-needs-prototype? ( class -- ? )
258 struct-slots [ initial>> binary-zero? ] all? not ;
260 : make-struct-prototype ( class -- prototype )
261 dup struct-needs-prototype? [
262 [ "c-type" word-prop size>> <byte-array> ]
268 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
272 : (struct-methods) ( class -- )
273 [ (define-struct-slot-values-method) ]
274 [ (define-clone-method) ]
277 : check-struct-slots ( slots -- )
278 [ type>> lookup-c-type drop ] each ;
280 : redefine-struct-tuple-class ( class -- )
281 [ struct f define-tuple-class ] [ make-final ] bi ;
283 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
284 slot-specs check-struct-slots
285 slot-specs empty? [ struct-must-have-slots ] when
286 class redefine-struct-tuple-class
287 slot-specs offsets-quot call :> unaligned-size
288 slot-specs alignment-quot call :> alignment
289 unaligned-size alignment align :> size
291 class slot-specs size alignment c-type-for-class :> c-type
294 class slot-specs define-accessors
295 class size "struct-size" set-word-prop
296 class dup make-struct-prototype "prototype" set-word-prop
297 class (struct-methods) ; inline
299 : make-packed-slots ( slots -- slot-specs )
300 make-slots [ t >>packed? ] map! ;
304 : define-struct-class ( class slots -- )
306 [ compute-struct-offsets ] [ struct-alignment ]
307 (define-struct-class) ;
309 : define-packed-struct-class ( class slots -- )
311 [ compute-struct-offsets ] [ drop 1 ]
312 (define-struct-class) ;
314 : define-union-struct-class ( class slots -- )
316 [ compute-union-offsets ] [ struct-alignment ]
317 (define-struct-class) ;
319 ERROR: invalid-struct-slot token ;
321 : struct-slot-class ( c-type -- class' )
322 lookup-c-type c-type-boxed-class
323 dup \ byte-array = [ drop \ c-ptr ] when ;
325 M: struct-class reset-class
327 [ dup "c-type" word-prop fields>> forget-slot-accessors ]
329 [ forget-struct-slot-values-method ]
330 [ forget-clone-method ] bi
332 [ { "c-type" "layout" "struct-size" } reset-props ]
340 ERROR: bad-type-for-bits type ;
342 :: set-bits ( slot-spec n -- slot-spec )
343 struct-bit-slot-spec new
348 [ bad-type-for-bits ]
350 slot-spec name>> >>name
351 slot-spec class>> >>class
352 slot-spec type>> >>type
353 slot-spec read-only>> >>read-only
354 slot-spec initial>> >>initial ;
356 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
359 { initial: [ [ first >>initial ] [ rest ] bi ] }
360 { read-only [ [ t >>read-only ] dip ] }
361 { bits: [ [ first set-bits ] [ rest ] bi ] }
362 [ bad-slot-attribute ]
368 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
369 [ struct-slot-spec new ] 3dip
371 [ [ >>type ] [ struct-slot-class >>class ] bi ]
372 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
375 : parse-struct-slot ( -- slot )
376 scan-token scan-c-type \ } parse-until <struct-slot-spec> ;
378 : parse-struct-slots ( slots -- slots' more? )
381 { "{" [ parse-struct-slot suffix! t ] }
382 [ invalid-struct-slot ]
385 : parse-struct-definition ( -- class slots )
386 scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
387 dup [ name>> ] map check-duplicate-slots ;
391 parse-struct-definition define-struct-class ;
393 SYNTAX: PACKED-STRUCT:
394 parse-struct-definition define-packed-struct-class ;
396 SYNTAX: UNION-STRUCT:
397 parse-struct-definition define-union-struct-class ;
400 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
403 scan-word scan-object swap memory>struct suffix! ;
408 : scan-c-type` ( -- c-type/param )
409 scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
411 : parse-struct-slot` ( accum -- accum )
412 scan-string-param scan-c-type` \ } parse-until
413 [ <struct-slot-spec> suffix! ] 3curry append! ;
415 : parse-struct-slots` ( accum -- accum more? )
418 { "{" [ parse-struct-slot` t ] }
419 [ invalid-struct-slot ]
424 FUNCTOR-SYNTAX: STRUCT:
426 [ 8 <vector> ] append!
427 [ parse-struct-slots` ] [ ] while
428 [ >array define-struct-class ] append! ;
430 USING: vocabs vocabs.loader ;
432 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when