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 ;
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 ! We hijack the core slots vocab's slot-spec type for struct
27 ! fields. Note that 'offset' is in bits, not bytes, to support
29 TUPLE: struct-slot-spec < slot-spec
32 ! For a struct-bit-slot-spec, offset is in bits, not bytes
33 TUPLE: struct-bit-slot-spec < struct-slot-spec
36 PREDICATE: struct-class < tuple-class
37 superclass-of \ struct eq? ;
41 : struct-slots ( struct-class -- slots )
42 "c-type" word-prop fields>> ;
44 M: struct-class group-words
45 struct-slots slot-group-words ;
50 2 slot { c-ptr } declare ; inline
54 2dup [ class-of ] same? [
56 [ [ >c-ptr ] [ binary-object ] bi* memory= ]
57 [ [ >c-ptr not ] both? ]
60 ] [ 2drop f ] if ; inline
64 [ uchar <c-direct-array> hashcode* ] [ 3drop 0 ] if ; inline
66 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
68 : memory>struct ( ptr class -- struct )
69 ! This is sub-optimal if the class is not literal, but gets
70 ! optimized down to efficient code if it is.
71 '[ _ boa ] call( ptr -- struct ) ; inline
73 : read-struct ( class -- struct )
74 [ heap-size read ] [ memory>struct ] bi ;
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 -- quot )
141 dup type>> add-depends-on-c-type
144 MACRO: write-struct-slot ( slot -- quot )
145 dup type>> add-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> t ; 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 lookup-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 : forget-struct-slot-values-method ( class -- )
201 \ struct-slot-values ?lookup-method forget ;
203 : clone-underlying ( struct -- byte-array )
204 binary-object memory>byte-array ; inline
206 : (define-clone-method) ( class -- )
208 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
209 define-inline-method ;
211 : forget-clone-method ( class -- )
212 \ clone ?lookup-method forget ;
214 :: c-type-for-class ( class slots size align -- c-type )
222 class (unboxer-quot) >>unboxer-quot
223 class (boxer-quot) >>boxer-quot ;
225 GENERIC: compute-slot-offset ( offset class -- offset' )
227 : c-type-align-at ( slot-spec offset -- n )
228 over packed?>> [ 2drop 1 ] [
230 0 = [ c-type-align-first ] [ c-type-align ] if
233 M: struct-slot-spec compute-slot-offset
234 [ over c-type-align-at 8 * align ] keep
235 [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
237 M: struct-bit-slot-spec compute-slot-offset
238 [ offset<< ] [ bits>> + ] 2bi ;
240 : compute-struct-offsets ( slots -- size )
241 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
243 : compute-union-offsets ( slots -- size )
244 1 [ 0 >>offset type>> heap-size max ] reduce ;
246 : struct-alignment ( slots -- align )
247 [ struct-bit-slot-spec? ] reject
248 1 [ dup offset>> c-type-align-at max ] reduce ;
252 : struct-size ( class -- n ) "struct-size" word-prop ; inline
254 M: struct byte-length class-of struct-size ; inline foldable
255 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
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>> lookup-c-type drop ] each ;
283 : redefine-struct-tuple-class ( class -- )
284 [ struct f define-tuple-class ] [ make-final ] bi ;
286 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
287 slot-specs check-struct-slots
288 slot-specs empty? [ throw-struct-must-have-slots ] when
289 class redefine-struct-tuple-class
290 slot-specs offsets-quot call :> unaligned-size
291 slot-specs alignment-quot call :> 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
302 : make-packed-slots ( slots -- slot-specs )
303 make-slots [ t >>packed? ] map! ;
307 : define-struct-class ( class slots -- )
309 [ compute-struct-offsets ] [ struct-alignment ]
310 (define-struct-class) ;
312 : define-packed-struct-class ( class slots -- )
314 [ compute-struct-offsets ] [ drop 1 ]
315 (define-struct-class) ;
317 : define-union-struct-class ( class slots -- )
319 [ compute-union-offsets ] [ struct-alignment ]
320 (define-struct-class) ;
322 ERROR: invalid-struct-slot token ;
324 : struct-slot-class ( c-type -- class' )
325 lookup-c-type c-type-boxed-class
326 dup \ byte-array = [ drop \ c-ptr ] when ;
328 M: struct-class reset-class
330 [ dup "c-type" word-prop fields>> forget-slot-accessors ]
332 [ forget-struct-slot-values-method ]
333 [ forget-clone-method ] bi
335 [ { "c-type" "layout" "struct-size" } remove-word-props ]
343 :: set-bits ( slot-spec n -- slot-spec )
344 struct-bit-slot-spec new
346 slot-spec type>> c-type-signed >>signed?
347 slot-spec name>> >>name
348 slot-spec class>> >>class
349 slot-spec type>> >>type
350 slot-spec read-only>> >>read-only
351 slot-spec initial>> >>initial ;
353 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
356 { initial: [ [ first >>initial ] [ rest ] bi ] }
357 { read-only [ [ t >>read-only ] dip ] }
358 { bits: [ [ first set-bits ] [ rest ] bi ] }
359 [ bad-slot-attribute ]
365 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
366 [ struct-slot-spec new ] 3dip
368 [ [ >>type ] [ struct-slot-class init-slot-class ] bi ]
369 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
372 : parse-struct-slot ( -- slot )
373 scan-token scan-c-type \ } parse-until <struct-slot-spec> ;
375 : parse-struct-slots ( slots -- slots' more? )
378 { "{" [ parse-struct-slot suffix! t ] }
379 [ throw-invalid-struct-slot ]
382 : parse-struct-definition ( -- class slots )
383 scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
384 dup [ name>> ] map check-duplicate-slots ;
388 parse-struct-definition define-struct-class ;
390 SYNTAX: PACKED-STRUCT:
391 parse-struct-definition define-packed-struct-class ;
393 SYNTAX: UNION-STRUCT:
394 parse-struct-definition define-union-struct-class ;
397 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
400 scan-word scan-object swap memory>struct suffix! ;
405 : scan-c-type` ( -- c-type/param )
406 scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
408 : parse-struct-slot` ( accum -- accum )
409 scan-string-param scan-c-type` \ } parse-until
410 [ <struct-slot-spec> suffix! ] 3curry append! ;
412 : parse-struct-slots` ( accum -- accum more? )
415 { "{" [ parse-struct-slot` t ] }
416 [ throw-invalid-struct-slot ]
421 FUNCTOR-SYNTAX: STRUCT:
423 [ 8 <vector> ] append!
424 [ parse-struct-slots` ] [ ] while
425 [ >array define-struct-class ] append! ;
427 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when