| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  | ! Copyright (C) 2010, 2011 Joe Groff, Daniel Ehrenberg, | 
					
						
							|  |  |  | ! John Benediktsson, Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license | 
					
						
							| 
									
										
										
										
											2010-01-31 08:48:39 -05:00
										 |  |  | USING: accessors alien alien.c-types alien.data alien.parser | 
					
						
							| 
									
										
										
										
											2011-12-01 02:09:49 -05:00
										 |  |  | arrays byte-arrays classes classes.parser classes.private | 
					
						
							| 
									
										
										
										
											2011-09-26 16:42:32 -04:00
										 |  |  | classes.struct.bit-accessors classes.tuple classes.tuple.parser | 
					
						
							| 
									
										
										
										
											2015-08-12 09:49:02 -04:00
										 |  |  | combinators combinators.smart cpu.architecture definitions | 
					
						
							|  |  |  | delegate.private fry functors.backend generalizations generic | 
					
						
							|  |  |  | generic.parser io kernel kernel.private lexer libc locals macros | 
					
						
							|  |  |  | math math.order parser quotations sequences slots slots.private | 
					
						
							|  |  |  | specialized-arrays stack-checker.dependencies summary vectors | 
					
						
							|  |  |  | vocabs.loader vocabs.parser words ;
 | 
					
						
							| 
									
										
										
										
											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 } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  | ! We hijack the core slots vocab's slot-spec type for struct | 
					
						
							|  |  |  | ! fields. Note that 'offset' is in bits, not bytes, to support | 
					
						
							|  |  |  | ! bitfields. | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  | TUPLE: struct-slot-spec < slot-spec | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  |     type packed? ;
 | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2015-07-20 03:46:33 -04:00
										 |  |  |     superclass-of \ struct eq? ;
 | 
					
						
							| 
									
										
										
										
											2009-09-10 16:59:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-08 15:52:46 -04:00
										 |  |  | M: struct-class group-words | 
					
						
							|  |  |  |     struct-slots slot-group-words ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | ! struct allocation | 
					
						
							|  |  |  | M: struct >c-ptr | 
					
						
							|  |  |  |     2 slot { c-ptr } declare ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-18 19:00:29 -04:00
										 |  |  | : read-struct ( class -- struct )
 | 
					
						
							|  |  |  |     [ heap-size read ] [ memory>struct ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							| 
									
										
										
										
											2015-08-16 00:40:39 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : init-struct ( class with-prototype: ( prototype -- alien ) sans-prototype: ( class -- alien ) -- alien )
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:40:13 -04:00
										 |  |  |     '[ dup struct-prototype _ _ ?if ] keep memory>struct ; inline
 | 
					
						
							| 
									
										
										
										
											2015-08-16 00:40:39 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							| 
									
										
										
										
											2015-08-16 00:40:39 -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 )
 | 
					
						
							| 
									
										
										
										
											2015-08-16 00:40:39 -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) | 
					
						
							| 
									
										
										
										
											2010-05-22 01:25:10 -04:00
										 |  |  |     [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ alien-value ] ;
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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) | 
					
						
							| 
									
										
										
										
											2010-05-22 01:25:10 -04:00
										 |  |  |     [ offset>> ] [ type>> ] bi '[ >c-ptr _ _ set-alien-value ] ;
 | 
					
						
							| 
									
										
										
										
											2009-08-18 11:26:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-08 14:10:51 -04:00
										 |  |  | M: struct-bit-slot-spec (writer-quot) | 
					
						
							| 
									
										
										
										
											2010-05-22 01:25:10 -04:00
										 |  |  |     [ 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 01:16:11 -04:00
										 |  |  | MACRO: read-struct-slot ( slot -- quot )
 | 
					
						
							| 
									
										
										
										
											2012-06-21 02:55:24 -04:00
										 |  |  |     dup type>> add-depends-on-c-type | 
					
						
							| 
									
										
										
										
											2010-02-22 01:46:52 -05:00
										 |  |  |     (reader-quot) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 01:16:11 -04:00
										 |  |  | MACRO: write-struct-slot ( slot -- quot )
 | 
					
						
							| 
									
										
										
										
											2012-06-21 02:55:24 -04:00
										 |  |  |     dup type>> add-depends-on-c-type | 
					
						
							| 
									
										
										
										
											2010-02-22 01:46:52 -05:00
										 |  |  |     (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
 | 
					
						
							| 
									
										
										
										
											2018-06-19 20:15:05 -04:00
										 |  |  |     [ [ (writer-quot) call( value struct -- ) ] with 2each ] keepd ;
 | 
					
						
							| 
									
										
										
										
											2009-08-12 15:40:06 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-12 17:48:00 -05:00
										 |  |  | M: struct-class initial-value* <struct> t ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-23 20:41:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-10 17:53:38 -04:00
										 |  |  | M: struct equal? | 
					
						
							|  |  |  |     2dup [ class-of ] same? [ | 
					
						
							|  |  |  |         [ struct-slot-values ] same?
 | 
					
						
							|  |  |  |     ] [ 2drop f ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct hashcode* | 
					
						
							|  |  |  |     nip dup >c-ptr [ struct-slot-values hashcode ] [ drop 0 ] if ; 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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-24 17:31:10 -04:00
										 |  |  | M: struct-c-type lookup-c-type ;
 | 
					
						
							| 
									
										
										
										
											2009-09-15 18:38:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-09 21:36:52 -04:00
										 |  |  | M: struct-c-type base-type ;
 | 
					
						
							| 
									
										
										
										
											2009-09-15 18:38:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-11 23:23:41 -04:00
										 |  |  | : large-struct? ( type -- ? )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { [ dup void? ] [ drop f ] } | 
					
						
							|  |  |  |         { [ dup base-type struct-c-type? not ] [ drop f ] } | 
					
						
							|  |  |  |         [ return-struct-in-registers? not ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							| 
									
										
										
										
											2009-09-15 18:38:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-02 23:06:44 -04:00
										 |  |  | : forget-struct-slot-values-method ( class -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-03 18:49:49 -04:00
										 |  |  |     \ struct-slot-values ?lookup-method forget ;
 | 
					
						
							| 
									
										
										
										
											2011-10-02 23:06:44 -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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-02 23:06:44 -04:00
										 |  |  | : forget-clone-method ( class -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-03 18:49:49 -04:00
										 |  |  |     \ clone ?lookup-method forget ;
 | 
					
						
							| 
									
										
										
										
											2011-10-02 23:06:44 -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' )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  | : c-type-align-at ( slot-spec offset -- n )
 | 
					
						
							|  |  |  |     over packed?>> [ 2drop 1 ] [ | 
					
						
							|  |  |  |         [ type>> ] dip
 | 
					
						
							|  |  |  |         0 = [ c-type-align-first ] [ c-type-align ] if
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: struct-slot-spec compute-slot-offset | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  |     [ over c-type-align-at 8 * align ] keep
 | 
					
						
							| 
									
										
										
										
											2010-05-05 16:52:54 -04:00
										 |  |  |     [ [ 8 /i ] dip offset<< ] [ type>> heap-size 8 * + ] 2bi ;
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  | M: struct-bit-slot-spec compute-slot-offset | 
					
						
							| 
									
										
										
										
											2010-05-05 16:52:54 -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 )
 | 
					
						
							| 
									
										
										
										
											2015-05-12 21:50:34 -04:00
										 |  |  |     [ struct-bit-slot-spec? ] reject | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  |     1 [ dup offset>> c-type-align-at max ] reduce ;
 | 
					
						
							| 
									
										
										
										
											2009-11-10 20:34:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 00:00:37 -04:00
										 |  |  | : struct-size ( class -- n ) "struct-size" word-prop ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: struct byte-length class-of struct-size ; inline foldable
 | 
					
						
							| 
									
										
										
										
											2011-09-25 14:49:27 -04:00
										 |  |  | M: struct binary-zero? binary-object uchar <c-direct-array> [ 0 = ] all? ; inline
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | : 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
 | 
					
						
							| 
									
										
										
										
											2018-06-19 20:15:05 -04:00
										 |  |  |             over [ swapd [ call( value struct -- ) ] keepd ] [ 2drop ] if
 | 
					
						
							| 
									
										
										
										
											2009-09-23 20:41:46 -04:00
										 |  |  |         ] 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 -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-24 17:31:10 -04:00
										 |  |  |     [ type>> lookup-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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  | :: (define-struct-class) ( class slot-specs offsets-quot alignment-quot -- )
 | 
					
						
							|  |  |  |     slot-specs check-struct-slots | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |     slot-specs empty? [ struct-must-have-slots ] when
 | 
					
						
							| 
									
										
										
										
											2009-09-24 12:32:25 -04:00
										 |  |  |     class redefine-struct-tuple-class | 
					
						
							| 
									
										
										
										
											2009-11-19 20:34:46 -05:00
										 |  |  |     slot-specs offsets-quot call :> unaligned-size | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  |     slot-specs alignment-quot call :> alignment | 
					
						
							| 
									
										
										
										
											2009-11-19 20:34:46 -05:00
										 |  |  |     unaligned-size alignment align :> size | 
					
						
							| 
									
										
										
										
											2009-09-24 12:32:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -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
 | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : make-packed-slots ( slots -- slot-specs )
 | 
					
						
							|  |  |  |     make-slots [ t >>packed? ] map! ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 23:59:13 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2009-08-13 16:55:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : define-struct-class ( class slots -- )
 | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  |     make-slots | 
					
						
							|  |  |  |     [ compute-struct-offsets ] [ struct-alignment ] | 
					
						
							|  |  |  |     (define-struct-class) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-packed-struct-class ( class slots -- )
 | 
					
						
							|  |  |  |     make-packed-slots | 
					
						
							|  |  |  |     [ compute-struct-offsets ] [ drop 1 ] | 
					
						
							|  |  |  |     (define-struct-class) ;
 | 
					
						
							| 
									
										
										
										
											2009-08-13 16:55:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : define-union-struct-class ( class slots -- )
 | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  |     make-slots | 
					
						
							|  |  |  |     [ compute-union-offsets ] [ struct-alignment ] | 
					
						
							|  |  |  |     (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' )
 | 
					
						
							| 
									
										
										
										
											2011-10-24 17:31:10 -04:00
										 |  |  |     lookup-c-type c-type-boxed-class | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  |     dup \ byte-array = [ drop \ c-ptr ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-02 23:06:44 -04:00
										 |  |  | M: struct-class reset-class | 
					
						
							| 
									
										
										
										
											2011-10-03 20:12:49 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2011-10-06 16:18:50 -04:00
										 |  |  |         [ dup "c-type" word-prop fields>> forget-slot-accessors ] | 
					
						
							| 
									
										
										
										
											2011-10-03 20:12:49 -04:00
										 |  |  |         [ | 
					
						
							|  |  |  |             [ forget-struct-slot-values-method ] | 
					
						
							|  |  |  |             [ forget-clone-method ] bi
 | 
					
						
							|  |  |  |         ] | 
					
						
							| 
									
										
										
										
											2015-06-09 14:53:46 -04:00
										 |  |  |         [ { "c-type" "layout" "struct-size" } remove-word-props ] | 
					
						
							| 
									
										
										
										
											2011-10-03 20:12:49 -04:00
										 |  |  |         [ call-next-method ] | 
					
						
							|  |  |  |     } cleave ;
 | 
					
						
							| 
									
										
										
										
											2011-10-02 23:06:44 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 02:43:32 -04:00
										 |  |  | SYMBOL: bits: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  | :: set-bits ( slot-spec n -- slot-spec )
 | 
					
						
							|  |  |  |     struct-bit-slot-spec new
 | 
					
						
							|  |  |  |         n >>bits | 
					
						
							| 
									
										
										
										
											2012-09-16 17:42:18 -04:00
										 |  |  |         slot-spec type>> c-type-signed >>signed? | 
					
						
							| 
									
										
										
										
											2009-10-07 22:35:12 -04:00
										 |  |  |         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 ] | 
					
						
							| 
									
										
										
										
											2011-11-12 19:43:16 -05:00
										 |  |  |     [ [ >>type ] [ struct-slot-class init-slot-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 )
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |     scan-token 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? )
 | 
					
						
							| 
									
										
										
										
											2010-07-06 16:20:08 -04:00
										 |  |  |     scan-token { | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  |         { ";" [ f ] } | 
					
						
							| 
									
										
										
										
											2009-10-28 16:29:01 -04:00
										 |  |  |         { "{" [ parse-struct-slot suffix! t ] } | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |         [ invalid-struct-slot ] | 
					
						
							| 
									
										
										
										
											2009-08-25 14:03:43 -04:00
										 |  |  |     } case ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-11 22:13:18 -04:00
										 |  |  | : parse-struct-definition ( -- class slots )
 | 
					
						
							| 
									
										
										
										
											2011-09-27 16:20:07 -04:00
										 |  |  |     scan-new-class 8 <vector> [ parse-struct-slots ] [ ] while >array
 | 
					
						
							| 
									
										
										
										
											2010-02-25 14:52:21 -05:00
										 |  |  |     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 ;
 | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYNTAX: PACKED-STRUCT: | 
					
						
							|  |  |  |     parse-struct-definition define-packed-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
 | 
					
						
							| 
									
										
										
										
											2016-03-21 00:03:44 -04:00
										 |  |  | : scan-c-type* ( -- c-type/param )
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |     scan-token dup "{" = [ drop \ } parse-until >array ] [ search ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-08-29 22:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 00:03:44 -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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 00:03:44 -04:00
										 |  |  | : parse-struct-slots* ( accum -- accum more? )
 | 
					
						
							| 
									
										
										
										
											2011-10-01 19:42:37 -04:00
										 |  |  |     scan-token { | 
					
						
							| 
									
										
										
										
											2009-08-29 22:04:19 -04:00
										 |  |  |         { ";" [ f ] } | 
					
						
							| 
									
										
										
										
											2016-03-21 00:03:44 -04:00
										 |  |  |         { "{" [ parse-struct-slot* t ] } | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |         [ invalid-struct-slot ] | 
					
						
							| 
									
										
										
										
											2009-08-29 22:04:19 -04:00
										 |  |  |     } case ;
 | 
					
						
							| 
									
										
										
										
											2011-08-26 00:02:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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!
 | 
					
						
							| 
									
										
										
										
											2016-03-21 00:03:44 -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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-18 15:29:24 -04:00
										 |  |  | { "classes.struct" "prettyprint" } "classes.struct.prettyprint" require-when |