1 ! Copyright (C) 2010, 2011 Joe Groff, Daniel Ehrenberg,
2 ! John Benediktsson, Slava Pestov.
3 ! See http://factorcode.org/license.txt for BSD license
5 DEFER: struct-slots ! for stack-checker
6 DEFER: struct-class? ! for stack-checker
7 DEFER: <struct-boa> ! for stack-checker
8 USING: accessors alien alien.c-types alien.data alien.parser
9 arrays assocs byte-arrays classes classes.parser classes.private
10 classes.struct.bit-accessors classes.tuple classes.tuple.parser
11 classes.tuple.private combinators combinators.short-circuit
12 combinators.smart cpu.architecture definitions delegate.private
13 effects functors.backend generalizations generic generic.parser
14 io kernel kernel.private lexer libc math math.order parser
15 quotations sequences sequences.private slots slots.private
16 specialized-arrays stack-checker.dependencies summary vectors
17 vocabs.loader vocabs.parser words ;
19 SPECIALIZED-ARRAY: uchar
21 ERROR: struct-must-have-slots ;
23 M: struct-must-have-slots summary
24 drop "Struct definitions must have slots" ;
27 { (underlying) c-ptr read-only } ;
29 ! We hijack the core slots vocab's slot-spec type for struct
30 ! fields. Note that 'offset' is in bits, not bytes, to support
32 TUPLE: struct-slot-spec < slot-spec
35 ! For a struct-bit-slot-spec, offset is in bits, not bytes
36 TUPLE: struct-bit-slot-spec < struct-slot-spec
39 PREDICATE: struct-class < tuple-class
40 superclass-of \ struct eq? ;
44 : struct-slots ( struct-class -- slots )
45 "c-type" word-prop fields>> ;
47 M: struct-class group-words
48 struct-slots slot-group-words ;
52 2 slot { c-ptr } declare ; inline
54 : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
56 : memory>struct ( ptr class -- struct )
57 ! This is sub-optimal if the class is not literal, but gets
58 ! optimized down to efficient code if it is.
59 struct-class check-instance
60 M\ tuple-class boa execute( ptr class -- struct ) ;
62 : read-struct ( class -- struct )
63 [ heap-size read ] [ memory>struct ] bi ;
67 : init-struct ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
68 '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
72 : (malloc-struct) ( class -- struct )
73 [ heap-size malloc ] keep memory>struct ; inline
75 : malloc-struct ( class -- struct )
76 [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] init-struct ; inline
78 : (struct) ( class -- struct )
79 [ heap-size (byte-array) ] keep memory>struct ; inline
81 : <struct> ( class -- struct )
82 [ >c-ptr clone ] [ heap-size <byte-array> ] init-struct ; inline
86 : pad-struct-slots ( values class -- values' class )
87 [ struct-slots [ initial>> ] map over length tail append ] keep ;
89 : sign-extend ( n bits -- n' )
91 ! http://guru.multimedia.cx/fast-sign-extension/
92 1 - -1 swap shift [ + ] keep bitxor ; inline
94 : sign-extender ( signed? bits -- quot )
95 '[ _ [ _ sign-extend ] when ] ;
97 GENERIC: (reader-quot) ( slot -- quot: ( struct -- value ) )
99 M: struct-slot-spec (reader-quot)
100 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
102 M: struct-bit-slot-spec (reader-quot)
103 [ [ offset>> ] [ bits>> ] bi bit-reader ]
104 [ [ signed?>> ] [ bits>> ] bi sign-extender ]
108 GENERIC: (writer-quot) ( slot -- quot: ( value struct -- ) )
110 M: struct-slot-spec (writer-quot)
111 [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
113 M: struct-bit-slot-spec (writer-quot)
114 [ offset>> ] [ bits>> ] bi bit-writer [ >c-ptr ] prepose ;
116 : (boxer-quot) ( class -- quot )
117 '[ _ memory>struct ] ;
119 : (unboxer-quot) ( class -- quot )
122 MACRO: read-struct-slot ( slot -- quot: ( struct -- value ) )
123 dup type>> add-depends-on-c-type
126 MACRO: write-struct-slot ( slot -- quot: ( value struct -- ) )
127 dup type>> add-depends-on-c-type
132 MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
134 [ length ] [ [ (writer-quot) '[ over @ ] ] map ] bi
135 '[ [ _ (struct) ] _ ndip _ spread ] ;
137 M: struct-class boa>object
138 swap pad-struct-slots
139 [ <struct> ] [ struct-slots ] bi
140 [ [ (writer-quot) call( value struct -- ) ] with 2each ] keepd ;
142 M: struct-class new <struct> ;
144 M: struct-class boa <struct-boa> ;
146 M: struct-class boa-effect
147 [ struct-slots [ name>> ] map ] [ name>> 1array ] bi <effect> ;
149 M: struct-class initial-value* <struct> t ; inline
151 ! Struct slot accessors
153 GENERIC: struct-slot-values ( struct -- sequence )
155 M: struct-class reader-quot
156 dup type>> array? [ dup type>> first define-array-vocab drop ] when
157 nip '[ _ read-struct-slot ] ;
159 M: struct-class writer-quot
160 nip '[ _ write-struct-slot ] ;
162 : offset-of ( field struct -- offset )
163 struct-slots slot-named offset>> ; inline
165 ! XXX: make this faster
167 nip dup >c-ptr [ struct-slot-values hashcode ] [ drop 0 ] if ; inline
171 TUPLE: struct-c-type < abstract-c-type
173 return-in-registers? ;
175 INSTANCE: struct-c-type value-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 : struct-equals-quot ( class -- quot )
204 [ name>> reader-word '[ [ _ execute ] same? ] ] map
205 '[ over _ instance? [ _ 2&& ] [ 2drop f ] if ] ;
207 : define-equal-method ( class -- )
208 [ \ equal? ] [ struct-equals-quot ] bi define-inline-method ;
210 : forget-equal-method ( class -- )
211 \ equal? ?lookup-method forget ;
213 : clone-underlying ( struct -- byte-array )
214 binary-object memory>byte-array ; inline
216 : define-clone-method ( class -- )
218 [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
219 define-inline-method ;
221 : forget-clone-method ( class -- )
222 \ clone ?lookup-method forget ;
224 :: c-type-for-class ( class slots size align -- c-type )
232 class (unboxer-quot) >>unboxer-quot
233 class (boxer-quot) >>boxer-quot ;
235 GENERIC: compute-slot-offset ( offset class -- offset' )
237 : c-type-align-at ( slot-spec offset -- n )
238 over packed?>> [ 2drop 1 ] [
240 0 = [ c-type-align-first ] [ c-type-align ] if
243 M: struct-slot-spec compute-slot-offset
244 [ over c-type-align-at 8 * align ] keep
245 [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
247 M: struct-bit-slot-spec compute-slot-offset
248 [ offset<< ] [ bits>> + ] 2bi ;
250 : compute-struct-offsets ( slots -- size )
251 0 [ compute-slot-offset ] reduce 8 align 8 /i ;
253 : compute-union-offsets ( slots -- size )
254 1 [ 0 >>offset type>> heap-size max ] reduce ;
256 : struct-alignment ( slots -- align )
257 [ struct-bit-slot-spec? ] reject
258 1 [ dup offset>> c-type-align-at max ] reduce ;
262 : struct-size ( class -- n ) "struct-size" word-prop ; inline
264 M: struct byte-length class-of struct-size ; inline foldable
265 M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
270 : struct-needs-prototype? ( class -- ? )
271 struct-slots [ initial>> binary-zero? ] all? not ;
273 : make-struct-prototype ( class -- prototype )
274 dup struct-needs-prototype? [
275 [ "c-type" word-prop size>> <byte-array> ]
281 over [ swapd [ call( value struct -- ) ] keepd ] [ 2drop ] if
285 : (struct-methods) ( class -- )
286 [ define-struct-slot-values-method ]
287 [ define-clone-method ]
288 [ define-equal-method ]
291 : check-struct-slots ( slots -- )
292 [ type>> lookup-c-type drop ] each ;
294 : redefine-struct-tuple-class ( class -- )
295 [ struct f redefine-tuple-class ] [ make-final ] bi ;
297 : resize-underlying ( struct -- )
298 [ 2 slot dup byte-array? ]
299 [ class-of "struct-size" word-prop '[ _ swap resize ] [ drop f ] if ]
302 M: struct update-tuple
303 ! make sure underlying byte-array is correct size, but maybe
304 ! has incorrect contents... is there something better to do?
305 [ resize-underlying ] [ call-next-method ] bi ;
307 : forget-struct-slot-accessors ( class -- )
308 dup "c-type" word-prop [
310 fields>> forget-slot-accessors
314 :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
315 slot-specs check-struct-slots
316 slot-specs empty? [ struct-must-have-slots ] when
317 class redefine-struct-tuple-class
318 slot-specs offsets-quot call :> unaligned-size
319 slot-specs alignment-quot call :> alignment
320 unaligned-size alignment align :> size
322 class slot-specs size alignment c-type-for-class :> c-type
324 class forget-struct-slot-accessors
327 class slot-specs define-accessors
328 class size "struct-size" set-word-prop
329 class dup make-struct-prototype "prototype" set-word-prop
330 class (struct-methods) ; inline
332 : make-packed-slots ( slots -- slot-specs )
333 make-slots [ t >>packed? ] map! ;
337 : define-struct-class ( class slots -- )
339 [ compute-struct-offsets ] [ struct-alignment ]
340 (define-struct-class) ;
342 : define-packed-struct-class ( class slots -- )
344 [ compute-struct-offsets ] [ drop 1 ]
345 (define-struct-class) ;
347 : define-union-struct-class ( class slots -- )
349 [ compute-union-offsets ] [ struct-alignment ]
350 (define-struct-class) ;
352 ERROR: invalid-struct-slot token ;
354 : struct-slot-class ( c-type -- class' )
355 lookup-c-type c-type-boxed-class
356 dup \ byte-array = [ drop \ c-ptr ] when ;
358 M: struct-class reset-class
360 [ \ <struct-boa> def>> first delete-at ]
361 [ forget-struct-slot-accessors ]
362 [ forget-struct-slot-values-method ]
363 [ forget-clone-method ]
364 [ forget-equal-method ]
365 [ { "c-type" "layout" "struct-size" } remove-word-props ]
373 :: set-bits ( slot-spec n -- slot-spec )
374 struct-bit-slot-spec new
376 slot-spec type>> c-type-signed >>signed?
377 slot-spec name>> >>name
378 slot-spec class>> >>class
379 slot-spec type>> >>type
380 slot-spec read-only>> >>read-only
381 slot-spec initial>> >>initial ;
383 : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
386 { initial: [ [ first >>initial ] [ rest ] bi ] }
387 { read-only [ [ t >>read-only ] dip ] }
388 { bits: [ [ first set-bits ] [ rest ] bi ] }
389 [ bad-slot-attribute ]
395 : <struct-slot-spec> ( name c-type attributes -- slot-spec )
396 [ struct-slot-spec new ] 3dip
398 [ [ >>type ] [ struct-slot-class init-slot-class ] bi ]
399 [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
402 : parse-struct-slot ( -- slot )
403 scan-token scan-c-type \ } parse-until <struct-slot-spec> ;
405 : parse-struct-slots ( slots -- slots' more? )
408 { "{" [ parse-struct-slot suffix! t ] }
409 [ invalid-struct-slot ]
412 : parse-struct-definition ( -- class slots )
413 scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
414 dup [ name>> ] map check-duplicate-slots ;
418 parse-struct-definition define-struct-class ;
420 SYNTAX: PACKED-STRUCT:
421 parse-struct-definition define-packed-struct-class ;
423 SYNTAX: UNION-STRUCT:
424 parse-struct-definition define-union-struct-class ;
427 scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
430 scan-word scan-object swap memory>struct suffix! ;
435 : scan-c-type* ( -- c-type/param )
436 scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
438 : parse-struct-slot* ( accum -- accum )
439 scan-string-param scan-c-type* \ } parse-until
440 [ <struct-slot-spec> suffix! ] 3curry append! ;
442 : parse-struct-slots* ( accum -- accum more? )
445 { "{" [ parse-struct-slot* t ] }
446 [ invalid-struct-slot ]
451 FUNCTOR-SYNTAX: STRUCT:
453 [ 8 <vector> ] append!
454 [ parse-struct-slots* ] [ ] while
455 [ >array define-struct-class ] append! ;
457 { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when