| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  | ! (c)Joe Groff, Daniel Ehrenberg bsd license | 
					
						
							| 
									
										
										
										
											2010-01-31 08:48:39 -05:00
										 |  |  | USING: accessors alien alien.c-types alien.data alien.parser | 
					
						
							|  |  |  | arrays byte-arrays classes classes.private classes.parser | 
					
						
							|  |  |  | classes.tuple classes.tuple.parser classes.tuple.private | 
					
						
							|  |  |  | combinators combinators.short-circuit combinators.smart | 
					
						
							|  |  |  | cpu.architecture definitions functors.backend fry | 
					
						
							|  |  |  | generalizations generic.parser kernel kernel.private lexer libc | 
					
						
							|  |  |  | locals macros make math math.order parser quotations sequences | 
					
						
							|  |  |  | slots slots.private specialized-arrays vectors words summary | 
					
						
							|  |  |  | namespaces assocs vocabs.parser math.functions | 
					
						
							| 
									
										
										
										
											2010-02-22 01:46:52 -05:00
										 |  |  | classes.struct.bit-accessors bit-arrays | 
					
						
							|  |  |  | stack-checker.dependencies ;
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  | QUALIFIED: math | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | IN: classes.struct | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | SPECIALIZED-ARRAY: uchar | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-26 23:37:59 -04:00
										 |  |  | ERROR: struct-must-have-slots ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-10 16:59:27 -04:00
										 |  |  | M: struct-must-have-slots summary | 
					
						
							|  |  |  |     drop "Struct definitions must have slots" ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | TUPLE: struct | 
					
						
							|  |  |  |     { (underlying) c-ptr read-only } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  | TUPLE: struct-slot-spec < slot-spec | 
					
						
							| 
									
										
										
										
											2009-09-15 18:38:49 -04:00
										 |  |  |     type ;
 | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  | ! For a struct-bit-slot-spec, offset is in bits, not bytes | 
					
						
							|  |  |  | TUPLE: struct-bit-slot-spec < struct-slot-spec | 
					
						
							|  |  |  |     bits signed? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-10 16:59:27 -04:00
										 |  |  | PREDICATE: struct-class < tuple-class | 
					
						
							|  |  |  |     superclass \ struct eq? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-24 13:07:41 -04:00
										 |  |  | SLOT: fields | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-24 12:32:25 -04:00
										 |  |  | : struct-slots ( struct-class -- slots )
 | 
					
						
							|  |  |  |     "c-type" word-prop fields>> ;
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | ! struct allocation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct >c-ptr | 
					
						
							|  |  |  |     2 slot { c-ptr } declare ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-26 19:05:38 -04:00
										 |  |  | M: struct equal? | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ [ class ] bi@ = ] | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  |         [ [ >c-ptr ] [ binary-object ] bi* memory= ] | 
					
						
							| 
									
										
										
										
											2009-08-31 11:51:45 -04:00
										 |  |  |     } 2&& ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct hashcode* | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  |     binary-object <direct-uchar-array> hashcode* ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-26 19:05:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:40:13 -04:00
										 |  |  | : struct-prototype ( class -- prototype ) "prototype" word-prop ; foldable
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | : memory>struct ( ptr class -- struct )
 | 
					
						
							| 
									
										
										
										
											2009-09-07 18:45:03 -04:00
										 |  |  |     ! This is sub-optimal if the class is not literal, but gets | 
					
						
							|  |  |  |     ! optimized down to efficient code if it is. | 
					
						
							|  |  |  |     '[ _ boa ] call( ptr -- struct ) ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:40:13 -04:00
										 |  |  | : (init-struct) ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
 | 
					
						
							|  |  |  |     '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:40:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (malloc-struct) ( class -- struct )
 | 
					
						
							|  |  |  |     [ heap-size malloc ] keep memory>struct ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | : malloc-struct ( class -- struct )
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:13:54 -04:00
										 |  |  |     [ >c-ptr malloc-byte-array ] [ 1 swap heap-size calloc ] (init-struct) ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-14 07:09:37 -04:00
										 |  |  | : (struct) ( class -- struct )
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:40:13 -04:00
										 |  |  |     [ heap-size (byte-array) ] keep memory>struct ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-26 22:27:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-14 07:09:37 -04:00
										 |  |  | : <struct> ( class -- struct )
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:13:54 -04:00
										 |  |  |     [ >c-ptr clone ] [ heap-size <byte-array> ] (init-struct) ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-12 09:37:39 -04:00
										 |  |  | MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-08-14 07:09:37 -04:00
										 |  |  |         [ <wrapper> \ (struct) [ ] 2sequence ] | 
					
						
							| 
									
										
										
										
											2009-08-12 09:37:39 -04:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  |             struct-slots | 
					
						
							| 
									
										
										
										
											2009-08-12 09:37:39 -04:00
										 |  |  |             [ length \ ndip ] | 
					
						
							|  |  |  |             [ [ name>> setter-word 1quotation ] map \ spread ] bi
 | 
					
						
							|  |  |  |         ] bi
 | 
					
						
							|  |  |  |     ] [ ] output>sequence ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2009-08-18 11:26:45 -04:00
										 |  |  | : pad-struct-slots ( values class -- values' class )
 | 
					
						
							| 
									
										
										
										
											2009-08-19 21:21:57 -04:00
										 |  |  |     [ struct-slots [ initial>> ] map over length tail append ] keep ;
 | 
					
						
							| 
									
										
										
										
											2009-08-12 15:40:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  | : sign-extend ( n bits -- n' )
 | 
					
						
							|  |  |  |     ! formula from: | 
					
						
							|  |  |  |     ! http://guru.multimedia.cx/fast-sign-extension/ | 
					
						
							|  |  |  |     1 - -1 swap shift [ + ] keep bitxor ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-08 00:51:18 -04:00
										 |  |  | : sign-extender ( signed? bits -- quot )
 | 
					
						
							|  |  |  |     '[ _ [ _ sign-extend ] when ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  | GENERIC: (reader-quot) ( slot -- quot )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-slot-spec (reader-quot) | 
					
						
							|  |  |  |     [ type>> c-type-getter-boxer ] | 
					
						
							|  |  |  |     [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-bit-slot-spec (reader-quot) | 
					
						
							| 
									
										
										
										
											2009-10-08 00:51:18 -04:00
										 |  |  |     [ [ offset>> ] [ bits>> ] bi bit-reader ] | 
					
						
							|  |  |  |     [ [ signed?>> ] [ bits>> ] bi sign-extender ] | 
					
						
							|  |  |  |     bi compose
 | 
					
						
							|  |  |  |     [ >c-ptr ] prepose ;
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  | GENERIC: (writer-quot) ( slot -- quot )
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  | M: struct-slot-spec (writer-quot) | 
					
						
							| 
									
										
										
										
											2009-09-15 18:38:49 -04:00
										 |  |  |     [ type>> c-setter ] | 
					
						
							| 
									
										
										
										
											2009-08-18 11:26:45 -04:00
										 |  |  |     [ offset>> [ >c-ptr ] swap suffix ] bi prepend ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-08 14:10:51 -04:00
										 |  |  | M: struct-bit-slot-spec (writer-quot) | 
					
						
							|  |  |  |     [ offset>> ] [ bits>> ] bi bit-writer | 
					
						
							|  |  |  |     [ >c-ptr ] prepose ;
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-19 23:50:02 -04:00
										 |  |  | : (boxer-quot) ( class -- quot )
 | 
					
						
							|  |  |  |     '[ _ memory>struct ] ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (unboxer-quot) ( class -- quot )
 | 
					
						
							|  |  |  |     drop [ >c-ptr ] ;
 | 
					
						
							| 
									
										
										
										
											2010-02-22 01:46:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | MACRO: read-struct-slot ( slot -- )
 | 
					
						
							|  |  |  |     dup type>> depends-on-c-type | 
					
						
							|  |  |  |     (reader-quot) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MACRO: write-struct-slot ( slot -- )
 | 
					
						
							|  |  |  |     dup type>> depends-on-c-type | 
					
						
							|  |  |  |     (writer-quot) ;
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2009-08-19 23:50:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-12 15:40:06 -04:00
										 |  |  | M: struct-class boa>object | 
					
						
							|  |  |  |     swap pad-struct-slots | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  |     [ <struct> ] [ struct-slots ] bi
 | 
					
						
							| 
									
										
										
										
											2009-08-18 11:26:45 -04:00
										 |  |  |     [ [ (writer-quot) call( value struct -- ) ] with 2each ] curry keep ;
 | 
					
						
							| 
									
										
										
										
											2009-08-12 15:40:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 20:41:46 -04:00
										 |  |  | M: struct-class initial-value* <struct> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | ! Struct slot accessors | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  | GENERIC: struct-slot-values ( struct -- sequence )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | M: struct-class reader-quot | 
					
						
							| 
									
										
										
										
											2010-02-23 02:57:10 -05:00
										 |  |  |     dup type>> array? [ dup type>> first define-array-vocab drop ] when
 | 
					
						
							| 
									
										
										
										
											2010-02-22 01:46:52 -05:00
										 |  |  |     nip '[ _ read-struct-slot ] ;
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-12 09:15:46 -04:00
										 |  |  | M: struct-class writer-quot | 
					
						
							| 
									
										
										
										
											2010-02-22 01:46:52 -05:00
										 |  |  |     nip '[ _ write-struct-slot ] ;
 | 
					
						
							| 
									
										
										
										
											2009-08-12 09:15:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-23 20:41:46 -04:00
										 |  |  | : offset-of ( field struct -- offset )
 | 
					
						
							|  |  |  |     struct-slots slot-named offset>> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-30 00:18:31 -04:00
										 |  |  | ! c-types | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-15 18:38:49 -04:00
										 |  |  | TUPLE: struct-c-type < abstract-c-type | 
					
						
							|  |  |  |     fields | 
					
						
							|  |  |  |     return-in-registers? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: struct-c-type value-type | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-c-type c-type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-c-type c-type-stack-align? drop f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : if-value-struct ( ctype true false -- )
 | 
					
						
							| 
									
										
										
										
											2009-09-16 10:20:47 -04:00
										 |  |  |     [ dup value-struct? ] 2dip '[ drop void* @ ] if ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-15 18:38:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: struct-c-type unbox-parameter | 
					
						
							|  |  |  |     [ %unbox-large-struct ] [ unbox-parameter ] if-value-struct ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-c-type box-parameter | 
					
						
							|  |  |  |     [ %box-large-struct ] [ box-parameter ] if-value-struct ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : if-small-struct ( c-type true false -- ? )
 | 
					
						
							|  |  |  |     [ dup return-struct-in-registers? ] 2dip '[ f swap @ ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-c-type unbox-return | 
					
						
							|  |  |  |     [ %unbox-small-struct ] [ %unbox-large-struct ] if-small-struct ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-c-type box-return | 
					
						
							|  |  |  |     [ %box-small-struct ] [ %box-large-struct ] if-small-struct ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-c-type stack-size | 
					
						
							|  |  |  |     [ heap-size ] [ stack-size ] if-value-struct ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-c-type c-struct? drop t ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  | : struct-slot-values-quot ( class -- quot )
 | 
					
						
							|  |  |  |     struct-slots | 
					
						
							| 
									
										
										
										
											2009-08-12 13:16:43 -04:00
										 |  |  |     [ name>> reader-word 1quotation ] map
 | 
					
						
							|  |  |  |     \ cleave [ ] 2sequence
 | 
					
						
							|  |  |  |     \ output>array [ ] 2sequence ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-19 19:53:44 -04:00
										 |  |  | : (define-struct-slot-values-method) ( class -- )
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:13:54 -04:00
										 |  |  |     [ \ struct-slot-values ] [ struct-slot-values-quot ] bi
 | 
					
						
							|  |  |  |     define-inline-method ;
 | 
					
						
							| 
									
										
										
										
											2009-08-12 13:16:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:13:54 -04:00
										 |  |  | : clone-underlying ( struct -- byte-array )
 | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  |     binary-object memory>byte-array ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:13:54 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : (define-clone-method) ( class -- )
 | 
					
						
							| 
									
										
										
										
											2009-08-30 22:01:44 -04:00
										 |  |  |     [ \ clone ] | 
					
						
							|  |  |  |     [ \ clone-underlying swap literalize \ memory>struct [ ] 3sequence ] bi
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:13:54 -04:00
										 |  |  |     define-inline-method ;
 | 
					
						
							| 
									
										
										
										
											2009-08-25 20:04:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-24 12:32:25 -04:00
										 |  |  | :: c-type-for-class ( class slots size align -- c-type )
 | 
					
						
							|  |  |  |     struct-c-type new
 | 
					
						
							|  |  |  |         byte-array >>class | 
					
						
							|  |  |  |         class >>boxed-class | 
					
						
							|  |  |  |         slots >>fields | 
					
						
							|  |  |  |         size >>size | 
					
						
							|  |  |  |         align >>align | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  |         align >>align-first | 
					
						
							| 
									
										
										
										
											2009-09-24 12:32:25 -04:00
										 |  |  |         class (unboxer-quot) >>unboxer-quot | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  |         class (boxer-quot) >>boxer-quot ;
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  | GENERIC: compute-slot-offset ( offset class -- offset' )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : c-type-align-at ( class offset -- n )
 | 
					
						
							|  |  |  |     0 = [ c-type-align-first ] [ c-type-align ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct-slot-spec compute-slot-offset | 
					
						
							|  |  |  |     [ type>> over c-type-align-at 8 * align ] keep
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  |     [ [ 8 /i ] dip (>>offset) ] [ type>> heap-size 8 * + ] 2bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  | M: struct-bit-slot-spec compute-slot-offset | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  |     [ (>>offset) ] [ bits>> + ] 2bi ;
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  | : compute-struct-offsets ( slots -- size )
 | 
					
						
							|  |  |  |     0 [ compute-slot-offset ] reduce 8 align 8 /i ;
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  | : compute-union-offsets ( slots -- size )
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  |     1 [ 0 >>offset type>> heap-size max ] reduce ;
 | 
					
						
							| 
									
										
										
										
											2009-08-13 16:55:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  | : struct-alignment ( slots -- align )
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  |     [ struct-bit-slot-spec? not ] filter
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  |     1 [ [ type>> ] [ offset>> ] bi c-type-align-at max ] reduce ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-24 14:00:26 -04:00
										 |  |  | M: struct byte-length class "struct-size" word-prop ; foldable
 | 
					
						
							| 
									
										
										
										
											2009-09-04 23:01:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | ! class definition | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2009-09-23 20:41:46 -04:00
										 |  |  | GENERIC: binary-zero? ( value -- ? )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: object binary-zero? drop f ;
 | 
					
						
							|  |  |  | M: f binary-zero? drop t ;
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  | M: number binary-zero? 0 = ;
 | 
					
						
							|  |  |  | M: struct binary-zero? >c-ptr [ 0 = ] all? ;
 | 
					
						
							| 
									
										
										
										
											2009-09-23 20:41:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : struct-needs-prototype? ( class -- ? )
 | 
					
						
							|  |  |  |     struct-slots [ initial>> binary-zero? ] all? not ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-26 22:27:12 -04:00
										 |  |  | : make-struct-prototype ( class -- prototype )
 | 
					
						
							| 
									
										
										
										
											2009-09-23 20:41:46 -04:00
										 |  |  |     dup struct-needs-prototype? [ | 
					
						
							| 
									
										
										
										
											2009-09-24 12:32:25 -04:00
										 |  |  |         [ "c-type" word-prop size>> <byte-array> ] | 
					
						
							| 
									
										
										
										
											2009-09-23 20:41:46 -04:00
										 |  |  |         [ memory>struct ] | 
					
						
							|  |  |  |         [ struct-slots ] tri
 | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             [ initial>> ] | 
					
						
							|  |  |  |             [ (writer-quot) ] bi
 | 
					
						
							|  |  |  |             over [ swapd [ call( value struct -- ) ] curry keep ] [ 2drop ] if
 | 
					
						
							|  |  |  |         ] each
 | 
					
						
							|  |  |  |     ] [ drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-25 20:04:29 -04:00
										 |  |  | : (struct-methods) ( class -- )
 | 
					
						
							|  |  |  |     [ (define-struct-slot-values-method) ] | 
					
						
							| 
									
										
										
										
											2009-08-30 21:13:54 -04:00
										 |  |  |     [ (define-clone-method) ] | 
					
						
							| 
									
										
										
										
											2009-09-04 23:01:55 -04:00
										 |  |  |     bi ;
 | 
					
						
							| 
									
										
										
										
											2009-08-25 20:04:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | : check-struct-slots ( slots -- )
 | 
					
						
							| 
									
										
										
										
											2009-09-15 18:38:49 -04:00
										 |  |  |     [ type>> c-type drop ] each ;
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-03 12:12:58 -04:00
										 |  |  | : redefine-struct-tuple-class ( class -- )
 | 
					
						
							| 
									
										
										
										
											2010-02-17 08:19:57 -05:00
										 |  |  |     [ struct f define-tuple-class ] [ make-final ] bi ;
 | 
					
						
							| 
									
										
										
										
											2009-09-03 12:12:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-24 12:32:25 -04:00
										 |  |  | :: (define-struct-class) ( class slots offsets-quot -- )
 | 
					
						
							|  |  |  |     slots empty? [ struct-must-have-slots ] when
 | 
					
						
							|  |  |  |     class redefine-struct-tuple-class | 
					
						
							|  |  |  |     slots make-slots dup check-struct-slots :> slot-specs | 
					
						
							| 
									
										
										
										
											2009-11-19 20:34:46 -05:00
										 |  |  |     slot-specs offsets-quot call :> unaligned-size | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  |     slot-specs struct-alignment :> alignment | 
					
						
							| 
									
										
										
										
											2009-11-19 20:34:46 -05:00
										 |  |  |     unaligned-size alignment align :> size | 
					
						
							| 
									
										
										
										
											2009-09-24 12:32:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-24 20:54:32 -04:00
										 |  |  |     class  slot-specs  size  alignment  c-type-for-class :> c-type | 
					
						
							| 
									
										
										
										
											2009-09-24 12:32:25 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     c-type class typedef | 
					
						
							|  |  |  |     class slot-specs define-accessors | 
					
						
							| 
									
										
										
										
											2009-09-24 14:00:26 -04:00
										 |  |  |     class size "struct-size" set-word-prop | 
					
						
							| 
									
										
										
										
											2009-09-24 12:32:25 -04:00
										 |  |  |     class dup make-struct-prototype "prototype" set-word-prop | 
					
						
							|  |  |  |     class (struct-methods) ; inline
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2009-08-13 16:55:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : define-struct-class ( class slots -- )
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  |     [ compute-struct-offsets ] (define-struct-class) ;
 | 
					
						
							| 
									
										
										
										
											2009-08-13 16:55:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : define-union-struct-class ( class slots -- )
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  |     [ compute-union-offsets ] (define-struct-class) ;
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  | ERROR: invalid-struct-slot token ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : struct-slot-class ( c-type -- class' )
 | 
					
						
							| 
									
										
										
										
											2009-08-25 14:18:20 -04:00
										 |  |  |     c-type c-type-boxed-class | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  |     dup \ byte-array = [ drop \ c-ptr ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  | SYMBOL: bits: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ERROR: bad-type-for-bits type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  | :: set-bits ( slot-spec n -- slot-spec )
 | 
					
						
							|  |  |  |     struct-bit-slot-spec new
 | 
					
						
							|  |  |  |         n >>bits | 
					
						
							|  |  |  |         slot-spec type>> { | 
					
						
							|  |  |  |             { int [ t ] } | 
					
						
							|  |  |  |             { uint [ f ] } | 
					
						
							|  |  |  |             [ bad-type-for-bits ] | 
					
						
							|  |  |  |         } case >>signed? | 
					
						
							|  |  |  |         slot-spec name>> >>name | 
					
						
							|  |  |  |         slot-spec class>> >>class | 
					
						
							|  |  |  |         slot-spec type>> >>type | 
					
						
							|  |  |  |         slot-spec read-only>> >>read-only | 
					
						
							|  |  |  |         slot-spec initial>> >>initial ;
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : peel-off-struct-attributes ( slot-spec array -- slot-spec array )
 | 
					
						
							|  |  |  |     dup empty? [ | 
					
						
							|  |  |  |         unclip { | 
					
						
							|  |  |  |             { initial: [ [ first >>initial ] [ rest ] bi ] } | 
					
						
							|  |  |  |             { read-only [ [ t >>read-only ] dip ] } | 
					
						
							|  |  |  |             { bits: [ [ first set-bits ] [ rest ] bi ] } | 
					
						
							|  |  |  |             [ bad-slot-attribute ] | 
					
						
							|  |  |  |         } case
 | 
					
						
							|  |  |  |     ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-30 23:37:37 -04:00
										 |  |  | : <struct-slot-spec> ( name c-type attributes -- slot-spec )
 | 
					
						
							|  |  |  |     [ struct-slot-spec new ] 3dip
 | 
					
						
							|  |  |  |     [ >>name ] | 
					
						
							| 
									
										
										
										
											2009-09-15 18:38:49 -04:00
										 |  |  |     [ [ >>type ] [ struct-slot-class >>class ] bi ] | 
					
						
							| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  |     [ [ dup empty? ] [ peel-off-struct-attributes ] until drop ] tri* ;
 | 
					
						
							| 
									
										
										
										
											2009-08-30 23:37:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  | : parse-struct-slot ( -- slot )
 | 
					
						
							| 
									
										
										
										
											2009-08-30 23:37:37 -04:00
										 |  |  |     scan scan-c-type \ } parse-until <struct-slot-spec> ;
 | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  | : parse-struct-slots ( slots -- slots' more? )
 | 
					
						
							|  |  |  |     scan { | 
					
						
							|  |  |  |         { ";" [ f ] } | 
					
						
							| 
									
										
										
										
											2009-10-28 16:29:01 -04:00
										 |  |  |         { "{" [ parse-struct-slot suffix! t ] } | 
					
						
							| 
									
										
										
										
											2009-09-10 16:59:27 -04:00
										 |  |  |         { f [ unexpected-eof ] } | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  |         [ invalid-struct-slot ] | 
					
						
							|  |  |  |     } case ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | : parse-struct-definition ( -- class slots )
 | 
					
						
							| 
									
										
										
										
											2010-02-25 14:52:21 -05:00
										 |  |  |     CREATE-CLASS 8 <vector> [ parse-struct-slots ] [ ] while >array
 | 
					
						
							|  |  |  |     dup [ name>> ] map check-duplicate-slots ;
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYNTAX: STRUCT: | 
					
						
							|  |  |  |     parse-struct-definition define-struct-class ;
 | 
					
						
							| 
									
										
										
										
											2009-08-13 16:55:22 -04:00
										 |  |  | SYNTAX: UNION-STRUCT: | 
					
						
							|  |  |  |     parse-struct-definition define-union-struct-class ;
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-26 18:19:30 -04:00
										 |  |  | SYNTAX: S{ | 
					
						
							| 
									
										
										
										
											2009-10-28 14:38:27 -04:00
										 |  |  |     scan-word dup struct-slots parse-tuple-literal-slots suffix! ;
 | 
					
						
							| 
									
										
										
										
											2009-08-26 18:19:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:46:31 -04:00
										 |  |  | SYNTAX: S@ | 
					
						
							| 
									
										
										
										
											2009-10-28 14:38:27 -04:00
										 |  |  |     scan-word scan-object swap memory>struct suffix! ;
 | 
					
						
							| 
									
										
										
										
											2009-08-30 21:46:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | ! functor support | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2009-08-29 22:04:19 -04:00
										 |  |  | : scan-c-type` ( -- c-type/param )
 | 
					
						
							| 
									
										
										
										
											2009-09-15 22:43:18 -04:00
										 |  |  |     scan dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-08-29 22:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-30 23:37:37 -04:00
										 |  |  | : parse-struct-slot` ( accum -- accum )
 | 
					
						
							|  |  |  |     scan-string-param scan-c-type` \ } parse-until | 
					
						
							| 
									
										
										
										
											2009-10-28 16:29:01 -04:00
										 |  |  |     [ <struct-slot-spec> suffix! ] 3curry append! ;
 | 
					
						
							| 
									
										
										
										
											2009-08-29 22:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : parse-struct-slots` ( accum -- accum more? )
 | 
					
						
							|  |  |  |     scan { | 
					
						
							|  |  |  |         { ";" [ f ] } | 
					
						
							|  |  |  |         { "{" [ parse-struct-slot` t ] } | 
					
						
							|  |  |  |         [ invalid-struct-slot ] | 
					
						
							|  |  |  |     } case ;
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2009-08-29 22:04:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | FUNCTOR-SYNTAX: STRUCT: | 
					
						
							| 
									
										
										
										
											2009-10-28 14:38:27 -04:00
										 |  |  |     scan-param suffix!
 | 
					
						
							| 
									
										
										
										
											2009-10-28 16:29:01 -04:00
										 |  |  |     [ 8 <vector> ] append!
 | 
					
						
							| 
									
										
										
										
											2009-08-29 22:04:19 -04:00
										 |  |  |     [ parse-struct-slots` ] [ ] while
 | 
					
						
							| 
									
										
										
										
											2009-10-28 16:29:01 -04:00
										 |  |  |     [ >array define-struct-class ] append! ;
 | 
					
						
							| 
									
										
										
										
											2009-08-29 22:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-12 10:37:09 -04:00
										 |  |  | USING: vocabs vocabs.loader ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-18 01:13:37 -04:00
										 |  |  | "prettyprint" "classes.struct.prettyprint" require-when |