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 classes.private
6 classes.struct.bit-accessors classes.tuple classes.tuple.parser
7 combinators combinators.smart cpu.architecture definitions
8 delegate.private fry functors.backend generalizations generic
9 generic.parser io kernel kernel.private lexer libc locals macros
10 math math.order parser quotations sequences slots slots.private
11 specialized-arrays stack-checker.dependencies summary vectors
12 vocabs.loader vocabs.parser words ;
15 SPECIALIZED-ARRAY: uchar
17 ERROR: struct-must-have-slots ;
19 M: struct-must-have-slots summary
20 drop "Struct definitions must have slots" ;
23 { (underlying) c-ptr read-only } ;
25 ! We hijack the core slots vocab's slot-spec type for struct
26 ! fields. Note that 'offset' is in bits, not bytes, to support
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-of \ 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 ;
48 2 slot { c-ptr } declare ; inline
50 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
52 : memory>struct ( ptr class -- struct )
53 ! This is sub-optimal if the class is not literal, but gets
54 ! optimized down to efficient code if it is.
55 '[ _ boa ] call( ptr -- struct ) ; inline
57 : read-struct ( class -- struct )
58 [ heap-size read ] [ memory>struct ] bi ;
62 : init-struct ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
63 '[ 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 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
106 M: struct-bit-slot-spec (reader-quot)
107 [ [ offset>> ] [ bits>> ] bi bit-reader ]
108 [ [ signed?>> ] [ bits>> ] bi sign-extender ]
112 GENERIC: (writer-quot) ( slot -- quot )
114 M: struct-slot-spec (writer-quot)
115 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
117 M: struct-bit-slot-spec (writer-quot)
118 [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
120 : (boxer-quot) ( class -- quot )
121 '[ _ memory>struct ] ;
123 : (unboxer-quot) ( class -- quot )
126 MACRO: read-struct-slot ( slot -- quot )
127 dup type>> add-depends-on-c-type
130 MACRO: write-struct-slot ( slot -- quot )
131 dup type>> add-depends-on-c-type
135 M: struct-class boa>object
136 swap pad-struct-slots
137 [ <struct> ] [ struct-slots ] bi
138 [ [ (writer-quot) call( value struct -- ) ] with 2each ] keepd ;
140 M: struct-class initial-value* <struct> t ; inline
142 ! Struct slot accessors
144 GENERIC: struct-slot-values ( struct -- sequence )
146 M: struct-class reader-quot
147 dup type>> array? [ dup type>> first define-array-vocab drop ] when
148 nip '[ _ read-struct-slot ] ;
150 M: struct-class writer-quot
151 nip '[ _ write-struct-slot ] ;
153 : offset-of ( field struct -- offset )
154 struct-slots slot-named offset>> ; inline
157 2dup [ class-of ] same? [
158 [ struct-slot-values ] same?
159 ] [ 2drop f ] if ; inline
162 nip dup >c-ptr [ struct-slot-values hashcode ] [ drop 0 ] if ; inline
166 TUPLE: struct-c-type < abstract-c-type
168 return-in-registers? ;
170 INSTANCE: struct-c-type value-type
172 M: struct-c-type lookup-c-type ;
174 M: struct-c-type base-type ;
176 : large-struct? ( type -- ? )
178 { [ dup void? ] [ drop f ] }
179 { [ dup base-type struct-c-type? not ] [ drop f ] }
180 [ return-struct-in-registers? not ]
184 : struct-slot-values-quot ( class -- quot )
186 [ name>> reader-word 1quotation ] map
187 \ cleave [ ] 2sequence
188 \ output>array [ ] 2sequence ;
190 : (define-struct-slot-values-method) ( class -- )
191 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
192 define-inline-method ;
194 : forget-struct-slot-values-method ( class -- )
195 \ struct-slot-values ?lookup-method forget ;
197 : clone-underlying ( struct -- byte-array )
198 binary-object memory>byte-array ; inline
200 : (define-clone-method) ( class -- )
202 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
203 define-inline-method ;
205 : forget-clone-method ( class -- )
206 \ clone ?lookup-method forget ;
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? ] reject
242 1 [ dup offset>> c-type-align-at max ] reduce ;
246 : struct-size ( class -- n ) "struct-size" word-prop ; inline
248 M: struct byte-length class-of struct-size ; inline foldable
249 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
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 -- ) ] keepd ] [ 2drop ] if
269 : (struct-methods) ( class -- )
270 [ (define-struct-slot-values-method) ]
271 [ (define-clone-method) ]
274 : check-struct-slots ( slots -- )
275 [ type>> lookup-c-type drop ] each ;
277 : redefine-struct-tuple-class ( class -- )
278 [ struct f define-tuple-class ] [ make-final ] bi ;
280 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
281 slot-specs check-struct-slots
282 slot-specs empty? [ struct-must-have-slots ] when
283 class redefine-struct-tuple-class
284 slot-specs offsets-quot call :> unaligned-size
285 slot-specs alignment-quot call :> 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
296 : make-packed-slots ( slots -- slot-specs )
297 make-slots [ t >>packed? ] map! ;
301 : define-struct-class ( class slots -- )
303 [ compute-struct-offsets ] [ struct-alignment ]
304 (define-struct-class) ;
306 : define-packed-struct-class ( class slots -- )
308 [ compute-struct-offsets ] [ drop 1 ]
309 (define-struct-class) ;
311 : define-union-struct-class ( class slots -- )
313 [ compute-union-offsets ] [ struct-alignment ]
314 (define-struct-class) ;
316 ERROR: invalid-struct-slot token ;
318 : struct-slot-class ( c-type -- class' )
319 lookup-c-type c-type-boxed-class
320 dup \ byte-array = [ drop \ c-ptr ] when ;
322 M: struct-class reset-class
324 [ dup "c-type" word-prop fields>> forget-slot-accessors ]
326 [ forget-struct-slot-values-method ]
327 [ forget-clone-method ] bi
329 [ { "c-type" "layout" "struct-size" } remove-word-props ]
337 :: set-bits ( slot-spec n -- slot-spec )
338 struct-bit-slot-spec new
340 slot-spec type>> c-type-signed >>signed?
341 slot-spec name>> >>name
342 slot-spec class>> >>class
343 slot-spec type>> >>type
344 slot-spec read-only>> >>read-only
345 slot-spec initial>> >>initial ;
347 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
350 { initial: [ [ first >>initial ] [ rest ] bi ] }
351 { read-only [ [ t >>read-only ] dip ] }
352 { bits: [ [ first set-bits ] [ rest ] bi ] }
353 [ bad-slot-attribute ]
359 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
360 [ struct-slot-spec new ] 3dip
362 [ [ >>type ] [ struct-slot-class init-slot-class ] bi ]
363 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
366 : parse-struct-slot ( -- slot )
367 scan-token scan-c-type \ } parse-until <struct-slot-spec> ;
369 : parse-struct-slots ( slots -- slots' more? )
372 { "{" [ parse-struct-slot suffix! t ] }
373 [ invalid-struct-slot ]
376 : parse-struct-definition ( -- class slots )
377 scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
378 dup [ name>> ] map check-duplicate-slots ;
382 parse-struct-definition define-struct-class ;
384 SYNTAX: PACKED-STRUCT:
385 parse-struct-definition define-packed-struct-class ;
387 SYNTAX: UNION-STRUCT:
388 parse-struct-definition define-union-struct-class ;
391 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
394 scan-word scan-object swap memory>struct suffix! ;
399 : scan-c-type* ( -- c-type/param )
400 scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
402 : parse-struct-slot* ( accum -- accum )
403 scan-string-param scan-c-type* \ } parse-until
404 [ <struct-slot-spec> suffix! ] 3curry append! ;
406 : parse-struct-slots* ( accum -- accum more? )
409 { "{" [ parse-struct-slot* t ] }
410 [ invalid-struct-slot ]
415 FUNCTOR-SYNTAX: STRUCT:
417 [ 8 <vector> ] append!
418 [ parse-struct-slots* ] [ ] while
419 [ >array define-struct-class ] append! ;
421 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when