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 functors.backend generalizations generic
9 generic.parser io kernel kernel.private lexer libc math
10 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
81 : pad-struct-slots ( values class -- values' class )
82 [ struct-slots [ initial>> ] map over length tail append ] keep ;
84 : sign-extend ( n bits -- n' )
86 ! http://guru.multimedia.cx/fast-sign-extension/
87 1 - -1 swap shift [ + ] keep bitxor ; inline
89 : sign-extender ( signed? bits -- quot )
90 '[ _ [ _ sign-extend ] when ] ;
92 GENERIC: (reader-quot) ( slot -- quot: ( struct -- value ) )
94 M: struct-slot-spec (reader-quot)
95 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
97 M: struct-bit-slot-spec (reader-quot)
98 [ [ offset>> ] [ bits>> ] bi bit-reader ]
99 [ [ signed?>> ] [ bits>> ] bi sign-extender ]
103 GENERIC: (writer-quot) ( slot -- quot: ( value struct -- ) )
105 M: struct-slot-spec (writer-quot)
106 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
108 M: struct-bit-slot-spec (writer-quot)
109 [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
111 : (boxer-quot) ( class -- quot )
112 '[ _ memory>struct ] ;
114 : (unboxer-quot) ( class -- quot )
117 MACRO: read-struct-slot ( slot -- quot: ( struct -- value ) )
118 dup type>> add-depends-on-c-type
121 MACRO: write-struct-slot ( slot -- quot: ( value struct -- ) )
122 dup type>> add-depends-on-c-type
127 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
129 [ length ] [ [ (writer-quot) '[ over @ ] ] map ] bi
130 '[ [ _ (struct) ] _ ndip _ spread ] ;
132 M: struct-class boa>object
133 swap pad-struct-slots
134 [ (struct) ] [ struct-slots ] bi
135 [ [ (writer-quot) call( value struct -- ) ] with 2each ] keepd ;
137 M: struct-class initial-value* <struct> t ; inline
139 ! Struct slot accessors
141 GENERIC: struct-slot-values ( struct -- sequence )
143 M: struct-class reader-quot
144 dup type>> array? [ dup type>> first define-array-vocab drop ] when
145 nip '[ _ read-struct-slot ] ;
147 M: struct-class writer-quot
148 nip '[ _ write-struct-slot ] ;
150 : offset-of ( field struct -- offset )
151 struct-slots slot-named offset>> ; inline
154 2dup [ class-of ] same? [
155 [ struct-slot-values ] same?
156 ] [ 2drop f ] if ; inline
159 nip dup >c-ptr [ struct-slot-values hashcode ] [ drop 0 ] if ; inline
163 TUPLE: struct-c-type < abstract-c-type
165 return-in-registers? ;
167 INSTANCE: struct-c-type value-type
169 M: struct-c-type lookup-c-type ;
171 M: struct-c-type base-type ;
173 : large-struct? ( type -- ? )
175 { [ dup void? ] [ drop f ] }
176 { [ dup base-type struct-c-type? not ] [ drop f ] }
177 [ return-struct-in-registers? not ]
181 : struct-slot-values-quot ( class -- quot )
183 [ name>> reader-word 1quotation ] map
184 \ cleave [ ] 2sequence
185 \ output>array [ ] 2sequence ;
187 : (define-struct-slot-values-method) ( class -- )
188 [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
189 define-inline-method ;
191 : forget-struct-slot-values-method ( class -- )
192 \ struct-slot-values ?lookup-method forget ;
194 : clone-underlying ( struct -- byte-array )
195 binary-object memory>byte-array ; inline
197 : (define-clone-method) ( class -- )
199 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
200 define-inline-method ;
202 : forget-clone-method ( class -- )
203 \ clone ?lookup-method forget ;
205 :: c-type-for-class ( class slots size align -- c-type )
213 class (unboxer-quot) >>unboxer-quot
214 class (boxer-quot) >>boxer-quot ;
216 GENERIC: compute-slot-offset ( offset class -- offset' )
218 : c-type-align-at ( slot-spec offset -- n )
219 over packed?>> [ 2drop 1 ] [
221 0 = [ c-type-align-first ] [ c-type-align ] if
224 M: struct-slot-spec compute-slot-offset
225 [ over c-type-align-at 8 * align ] keep
226 [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
228 M: struct-bit-slot-spec compute-slot-offset
229 [ offset<< ] [ bits>> + ] 2bi ;
231 : compute-struct-offsets ( slots -- size )
232 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
234 : compute-union-offsets ( slots -- size )
235 1 [ 0 >>offset type>> heap-size max ] reduce ;
237 : struct-alignment ( slots -- align )
238 [ struct-bit-slot-spec? ] reject
239 1 [ dup offset>> c-type-align-at max ] reduce ;
243 : struct-size ( class -- n ) "struct-size" word-prop ; inline
245 M: struct byte-length class-of struct-size ; inline foldable
246 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
251 : struct-needs-prototype? ( class -- ? )
252 struct-slots [ initial>> binary-zero? ] all? not ;
254 : make-struct-prototype ( class -- prototype )
255 dup struct-needs-prototype? [
256 [ "c-type" word-prop size>> <byte-array> ]
262 over [ swapd [ call( value struct -- ) ] keepd ] [ 2drop ] if
266 : (struct-methods) ( class -- )
267 [ (define-struct-slot-values-method) ]
268 [ (define-clone-method) ]
271 : check-struct-slots ( slots -- )
272 [ type>> lookup-c-type drop ] each ;
274 : redefine-struct-tuple-class ( class -- )
275 [ struct f define-tuple-class ] [ make-final ] bi ;
277 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
278 slot-specs check-struct-slots
279 slot-specs empty? [ struct-must-have-slots ] when
280 class redefine-struct-tuple-class
281 slot-specs offsets-quot call :> unaligned-size
282 slot-specs alignment-quot call :> alignment
283 unaligned-size alignment align :> size
285 class slot-specs size alignment c-type-for-class :> c-type
288 class slot-specs define-accessors
289 class size "struct-size" set-word-prop
290 class dup make-struct-prototype "prototype" set-word-prop
291 class (struct-methods) ; inline
293 : make-packed-slots ( slots -- slot-specs )
294 make-slots [ t >>packed? ] map! ;
298 : define-struct-class ( class slots -- )
300 [ compute-struct-offsets ] [ struct-alignment ]
301 (define-struct-class) ;
303 : define-packed-struct-class ( class slots -- )
305 [ compute-struct-offsets ] [ drop 1 ]
306 (define-struct-class) ;
308 : define-union-struct-class ( class slots -- )
310 [ compute-union-offsets ] [ struct-alignment ]
311 (define-struct-class) ;
313 ERROR: invalid-struct-slot token ;
315 : struct-slot-class ( c-type -- class' )
316 lookup-c-type c-type-boxed-class
317 dup \ byte-array = [ drop \ c-ptr ] when ;
319 M: struct-class reset-class
321 [ dup "c-type" word-prop fields>> forget-slot-accessors ]
323 [ forget-struct-slot-values-method ]
324 [ forget-clone-method ] bi
326 [ { "c-type" "layout" "struct-size" } remove-word-props ]
334 :: set-bits ( slot-spec n -- slot-spec )
335 struct-bit-slot-spec new
337 slot-spec type>> c-type-signed >>signed?
338 slot-spec name>> >>name
339 slot-spec class>> >>class
340 slot-spec type>> >>type
341 slot-spec read-only>> >>read-only
342 slot-spec initial>> >>initial ;
344 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
347 { initial: [ [ first >>initial ] [ rest ] bi ] }
348 { read-only [ [ t >>read-only ] dip ] }
349 { bits: [ [ first set-bits ] [ rest ] bi ] }
350 [ bad-slot-attribute ]
356 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
357 [ struct-slot-spec new ] 3dip
359 [ [ >>type ] [ struct-slot-class init-slot-class ] bi ]
360 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
363 : parse-struct-slot ( -- slot )
364 scan-token scan-c-type \ } parse-until <struct-slot-spec> ;
366 : parse-struct-slots ( slots -- slots' more? )
369 { "{" [ parse-struct-slot suffix! t ] }
370 [ invalid-struct-slot ]
373 : parse-struct-definition ( -- class slots )
374 scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
375 dup [ name>> ] map check-duplicate-slots ;
379 parse-struct-definition define-struct-class ;
381 SYNTAX: PACKED-STRUCT:
382 parse-struct-definition define-packed-struct-class ;
384 SYNTAX: UNION-STRUCT:
385 parse-struct-definition define-union-struct-class ;
388 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
391 scan-word scan-object swap memory>struct suffix! ;
396 : scan-c-type* ( -- c-type/param )
397 scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
399 : parse-struct-slot* ( accum -- accum )
400 scan-string-param scan-c-type* \ } parse-until
401 [ <struct-slot-spec> suffix! ] 3curry append! ;
403 : parse-struct-slots* ( accum -- accum more? )
406 { "{" [ parse-struct-slot* t ] }
407 [ invalid-struct-slot ]
412 FUNCTOR-SYNTAX: STRUCT:
414 [ 8 <vector> ] append!
415 [ parse-struct-slots* ] [ ] while
416 [ >array define-struct-class ] append! ;
418 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when