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 fry
8 functors.backend generalizations generic generic.parser io 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.loader
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 ] same? [
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
74 : read-struct ( class -- struct )
75 [ heap-size read ] [ memory>struct ] bi ;
78 : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
79 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
82 : (malloc-struct) ( class -- struct )
83 [ heap-size malloc ] keep memory>struct ; inline
85 : malloc-struct ( class -- struct )
86 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
88 : (struct) ( class -- struct )
89 [ heap-size (byte-array) ] keep memory>struct ; inline
91 : <struct> ( class -- struct )
92 [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
94 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
96 [ <wrapper> \ (struct) [ ] 2sequence ]
100 [ [ name>> setter-word 1quotation ] map \ spread ] bi
102 ] [ ] output>sequence ;
105 : pad-struct-slots ( values class -- values' class )
106 [ struct-slots [ initial>> ] map over length tail append ] keep ;
108 : sign-extend ( n bits -- n' )
110 ! http://guru.multimedia.cx/fast-sign-extension/
111 1 - -1 swap shift [ + ] keep bitxor ; inline
113 : sign-extender ( signed? bits -- quot )
114 '[ _ [ _ sign-extend ] when ] ;
116 GENERIC: (reader-quot) ( slot -- quot )
118 M: struct-slot-spec (reader-quot)
119 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
121 M: struct-bit-slot-spec (reader-quot)
122 [ [ offset>> ] [ bits>> ] bi bit-reader ]
123 [ [ signed?>> ] [ bits>> ] bi sign-extender ]
127 GENERIC: (writer-quot) ( slot -- quot )
129 M: struct-slot-spec (writer-quot)
130 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
132 M: struct-bit-slot-spec (writer-quot)
133 [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
135 : (boxer-quot) ( class -- quot )
136 '[ _ memory>struct ] ;
138 : (unboxer-quot) ( class -- quot )
141 MACRO: read-struct-slot ( slot -- )
142 dup type>> add-depends-on-c-type
145 MACRO: write-struct-slot ( slot -- )
146 dup type>> add-depends-on-c-type
150 M: struct-class boa>object
151 swap pad-struct-slots
152 [ <struct> ] [ struct-slots ] bi
153 [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
155 M: struct-class initial-value* <struct> t ; inline
157 ! Struct slot accessors
159 GENERIC: struct-slot-values ( struct -- sequence )
161 M: struct-class reader-quot
162 dup type>> array? [ dup type>> first define-array-vocab drop ] when
163 nip '[ _ read-struct-slot ] ;
165 M: struct-class writer-quot
166 nip '[ _ write-struct-slot ] ;
168 : offset-of ( field struct -- offset )
169 struct-slots slot-named offset>> ; inline
173 TUPLE: struct-c-type < abstract-c-type
175 return-in-registers? ;
177 INSTANCE: struct-c-type value-type
179 M: struct-c-type lookup-c-type ;
181 M: struct-c-type base-type ;
183 : large-struct? ( type -- ? )
185 { [ dup void? ] [ drop f ] }
186 { [ dup base-type struct-c-type? not ] [ drop f ] }
187 [ return-struct-in-registers? not ]
191 : struct-slot-values-quot ( class -- quot )
193 [ name>> reader-word 1quotation ] map
194 \ cleave [ ] 2sequence
195 \ output>array [ ] 2sequence ;
197 : (define-struct-slot-values-method) ( class -- )
198 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
199 define-inline-method ;
201 : forget-struct-slot-values-method ( class -- )
202 \ struct-slot-values ?lookup-method forget ;
204 : clone-underlying ( struct -- byte-array )
205 binary-object memory>byte-array ; inline
207 : (define-clone-method) ( class -- )
209 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
210 define-inline-method ;
212 : forget-clone-method ( class -- )
213 \ clone ?lookup-method forget ;
215 :: c-type-for-class ( class slots size align -- c-type )
223 class (unboxer-quot) >>unboxer-quot
224 class (boxer-quot) >>boxer-quot ;
226 GENERIC: compute-slot-offset ( offset class -- offset' )
228 : c-type-align-at ( slot-spec offset -- n )
229 over packed?>> [ 2drop 1 ] [
231 0 = [ c-type-align-first ] [ c-type-align ] if
234 M: struct-slot-spec compute-slot-offset
235 [ over c-type-align-at 8 * align ] keep
236 [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
238 M: struct-bit-slot-spec compute-slot-offset
239 [ offset<< ] [ bits>> + ] 2bi ;
241 : compute-struct-offsets ( slots -- size )
242 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
244 : compute-union-offsets ( slots -- size )
245 1 [ 0 >>offset type>> heap-size max ] reduce ;
247 : struct-alignment ( slots -- align )
248 [ struct-bit-slot-spec? not ] filter
249 1 [ dup offset>> c-type-align-at max ] reduce ;
253 : struct-size ( class -- n ) "struct-size" word-prop ; inline
255 M: struct byte-length class-of struct-size ; inline foldable
256 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
261 : struct-needs-prototype? ( class -- ? )
262 struct-slots [ initial>> binary-zero? ] all? not ;
264 : make-struct-prototype ( class -- prototype )
265 dup struct-needs-prototype? [
266 [ "c-type" word-prop size>> <byte-array> ]
272 over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
276 : (struct-methods) ( class -- )
277 [ (define-struct-slot-values-method) ]
278 [ (define-clone-method) ]
281 : check-struct-slots ( slots -- )
282 [ type>> lookup-c-type drop ] each ;
284 : redefine-struct-tuple-class ( class -- )
285 [ struct f define-tuple-class ] [ make-final ] bi ;
287 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
288 slot-specs check-struct-slots
289 slot-specs empty? [ struct-must-have-slots ] when
290 class redefine-struct-tuple-class
291 slot-specs offsets-quot call :> unaligned-size
292 slot-specs alignment-quot call :> alignment
293 unaligned-size alignment align :> size
295 class slot-specs size alignment c-type-for-class :> c-type
298 class slot-specs define-accessors
299 class size "struct-size" set-word-prop
300 class dup make-struct-prototype "prototype" set-word-prop
301 class (struct-methods) ; inline
303 : make-packed-slots ( slots -- slot-specs )
304 make-slots [ t >>packed? ] map! ;
308 : define-struct-class ( class slots -- )
310 [ compute-struct-offsets ] [ struct-alignment ]
311 (define-struct-class) ;
313 : define-packed-struct-class ( class slots -- )
315 [ compute-struct-offsets ] [ drop 1 ]
316 (define-struct-class) ;
318 : define-union-struct-class ( class slots -- )
320 [ compute-union-offsets ] [ struct-alignment ]
321 (define-struct-class) ;
323 ERROR: invalid-struct-slot token ;
325 : struct-slot-class ( c-type -- class' )
326 lookup-c-type c-type-boxed-class
327 dup \ byte-array = [ drop \ c-ptr ] when ;
329 M: struct-class reset-class
331 [ dup "c-type" word-prop fields>> forget-slot-accessors ]
333 [ forget-struct-slot-values-method ]
334 [ forget-clone-method ] bi
336 [ { "c-type" "layout" "struct-size" } reset-props ]
344 :: set-bits ( slot-spec n -- slot-spec )
345 struct-bit-slot-spec new
347 slot-spec type>> c-type-signed >>signed?
348 slot-spec name>> >>name
349 slot-spec class>> >>class
350 slot-spec type>> >>type
351 slot-spec read-only>> >>read-only
352 slot-spec initial>> >>initial ;
354 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
357 { initial: [ [ first >>initial ] [ rest ] bi ] }
358 { read-only [ [ t >>read-only ] dip ] }
359 { bits: [ [ first set-bits ] [ rest ] bi ] }
360 [ bad-slot-attribute ]
366 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
367 [ struct-slot-spec new ] 3dip
369 [ [ >>type ] [ struct-slot-class init-slot-class ] bi ]
370 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
373 : parse-struct-slot ( -- slot )
374 scan-token scan-c-type \ } parse-until <struct-slot-spec> ;
376 : parse-struct-slots ( slots -- slots' more? )
379 { "{" [ parse-struct-slot suffix! t ] }
380 [ invalid-struct-slot ]
383 : parse-struct-definition ( -- class slots )
384 scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
385 dup [ name>> ] map check-duplicate-slots ;
389 parse-struct-definition define-struct-class ;
391 SYNTAX: PACKED-STRUCT:
392 parse-struct-definition define-packed-struct-class ;
394 SYNTAX: UNION-STRUCT:
395 parse-struct-definition define-union-struct-class ;
398 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
401 scan-word scan-object swap memory>struct suffix! ;
406 : scan-c-type` ( -- c-type/param )
407 scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
409 : parse-struct-slot` ( accum -- accum )
410 scan-string-param scan-c-type` \ } parse-until
411 [ <struct-slot-spec> suffix! ] 3curry append! ;
413 : parse-struct-slots` ( accum -- accum more? )
416 { "{" [ parse-struct-slot` t ] }
417 [ invalid-struct-slot ]
422 FUNCTOR-SYNTAX: STRUCT:
424 [ 8 <vector> ] append!
425 [ parse-struct-slots` ] [ ] while
426 [ >array define-struct-class ] append! ;
428 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when