| 
									
										
										
										
											2010-02-19 18:05:12 -05:00
										 |  |  | ! Copyright (C) 2008, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-11-14 21:18:16 -05:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  | USING: accessors alien alien.c-types alien.data alien.parser | 
					
						
							|  |  |  | assocs byte-arrays classes compiler.units functors kernel lexer | 
					
						
							| 
									
										
										
										
											2009-11-11 17:08:40 -05:00
										 |  |  | libc math math.vectors math.vectors.private namespaces | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  | parser prettyprint.custom sequences sequences.private strings | 
					
						
							|  |  |  | summary vocabs vocabs.loader vocabs.parser vocabs.generated | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  | words fry combinators make ;
 | 
					
						
							| 
									
										
										
										
											2008-11-14 21:18:16 -05:00
										 |  |  | IN: specialized-arrays | 
					
						
							| 
									
										
										
										
											2009-09-04 23:01:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | MIXIN: specialized-array | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-04 23:01:55 -04:00
										 |  |  | INSTANCE: specialized-array sequence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: direct-array-syntax ( obj -- word )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | ERROR: bad-byte-array-length byte-array type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: bad-byte-array-length summary | 
					
						
							|  |  |  |     drop "Byte array length doesn't divide type width" ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-19 18:05:12 -05:00
										 |  |  | ERROR: not-a-byte-array alien ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: not-a-byte-array summary | 
					
						
							|  |  |  |     drop "Not a byte array" ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | : (underlying) ( n c-type -- array )
 | 
					
						
							|  |  |  |     heap-size * (byte-array) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <underlying> ( n type -- array )
 | 
					
						
							|  |  |  |     heap-size * <byte-array> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FUNCTOR: define-array ( T -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A            DEFINES-CLASS ${T}-array | 
					
						
							|  |  |  | <A>          DEFINES <${A}> | 
					
						
							|  |  |  | (A)          DEFINES (${A}) | 
					
						
							|  |  |  | <direct-A>   DEFINES <direct-${A}> | 
					
						
							|  |  |  | malloc-A     DEFINES malloc-${A} | 
					
						
							|  |  |  | >A           DEFINES >${A} | 
					
						
							|  |  |  | byte-array>A DEFINES byte-array>${A} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A{           DEFINES ${A}{ | 
					
						
							|  |  |  | A@           DEFINES ${A}@ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NTH          [ T dup c-type-getter-boxer array-accessor ] | 
					
						
							|  |  |  | SET-NTH      [ T dup c-setter array-accessor ] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WHERE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: A | 
					
						
							|  |  |  | { underlying c-ptr read-only } | 
					
						
							| 
									
										
										
										
											2010-02-17 08:39:40 -05:00
										 |  |  | { length array-capacity read-only } ; final
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : <direct-A> ( alien len -- specialized-array ) A boa ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-02 00:12:53 -04:00
										 |  |  | : <A> ( n -- specialized-array )
 | 
					
						
							|  |  |  |     [ \ T <underlying> ] keep <direct-A> ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-02 00:12:53 -04:00
										 |  |  | : (A) ( n -- specialized-array )
 | 
					
						
							|  |  |  |     [ \ T (underlying) ] keep <direct-A> ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-02 00:12:53 -04:00
										 |  |  | : malloc-A ( len -- specialized-array )
 | 
					
						
							|  |  |  |     [ \ T heap-size calloc ] keep <direct-A> ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : byte-array>A ( byte-array -- specialized-array )
 | 
					
						
							| 
									
										
										
										
											2010-02-19 18:05:12 -05:00
										 |  |  |     >c-ptr dup byte-array? [ | 
					
						
							|  |  |  |         dup length \ T heap-size /mod 0 =
 | 
					
						
							|  |  |  |         [ <direct-A> ] | 
					
						
							|  |  |  |         [ drop \ T bad-byte-array-length ] if
 | 
					
						
							|  |  |  |     ] [ not-a-byte-array ] if ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: A clone [ underlying>> clone ] [ length>> ] bi <direct-A> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A length length>> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A nth-unsafe underlying>> NTH call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A set-nth-unsafe underlying>> SET-NTH call ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >A ( seq -- specialized-array ) A new clone-like ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A like drop dup A instance? [ >A ] unless ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A new-sequence drop (A) ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A equal? over A instance? [ sequence= ] [ 2drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: A resize | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  |         [ \ T heap-size * ] [ underlying>> ] bi*
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |         resize-byte-array | 
					
						
							|  |  |  |     ] [ drop ] 2bi
 | 
					
						
							|  |  |  |     <direct-A> ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-24 10:50:31 -05:00
										 |  |  | M: A element-size drop \ T heap-size ; inline
 | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  | M: A direct-array-syntax drop \ A@ ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | M: A pprint-delims drop \ A{ \ } ;
 | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | M: A >pprint-sequence ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYNTAX: A{ \ } [ >A ] parse-literal ;
 | 
					
						
							| 
									
										
										
										
											2009-10-28 14:38:27 -04:00
										 |  |  | SYNTAX: A@ scan-object scan-object <direct-A> suffix! ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: A specialized-array | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 23:48:10 -05:00
										 |  |  | M: A vs+ [ + \ T c-type-clamp ] 2map ; inline
 | 
					
						
							|  |  |  | M: A vs- [ - \ T c-type-clamp ] 2map ; inline
 | 
					
						
							|  |  |  | M: A vs* [ * \ T c-type-clamp ] 2map ; inline
 | 
					
						
							| 
									
										
										
										
											2009-12-04 15:43:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 23:48:10 -05:00
										 |  |  | M: A v*high [ * \ T heap-size neg shift ] 2map ; inline
 | 
					
						
							| 
									
										
										
										
											2009-12-05 14:32:31 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | ;FUNCTOR | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 02:13:31 -05:00
										 |  |  | GENERIC: underlying-type ( c-type -- c-type' )
 | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 02:13:31 -05:00
										 |  |  | M: c-type-word underlying-type | 
					
						
							| 
									
										
										
										
											2010-02-21 19:27:36 -05:00
										 |  |  |     dup "c-type" word-prop { | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  |         { [ dup not ] [ drop no-c-type ] } | 
					
						
							| 
									
										
										
										
											2010-02-21 22:23:47 -05:00
										 |  |  |         { [ dup pointer? ] [ 2drop void* ] } | 
					
						
							| 
									
										
										
										
											2010-02-21 19:27:36 -05:00
										 |  |  |         { [ dup c-type-word? ] [ nip underlying-type ] } | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  |         [ drop ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-22 02:13:31 -05:00
										 |  |  | M: pointer underlying-type | 
					
						
							|  |  |  |     drop void* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | : specialized-array-vocab ( c-type -- vocab )
 | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         "specialized-arrays.instances." % | 
					
						
							|  |  |  |         [ vocabulary>> % "." % ] | 
					
						
							|  |  |  |         [ name>> % ] | 
					
						
							|  |  |  |         bi
 | 
					
						
							|  |  |  |     ] "" make ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  | : define-array-vocab ( type -- vocab )
 | 
					
						
							| 
									
										
										
										
											2009-09-21 00:16:02 -04:00
										 |  |  |     underlying-type | 
					
						
							| 
									
										
										
										
											2009-09-10 15:46:26 -04:00
										 |  |  |     [ specialized-array-vocab ] [ '[ _ define-array ] ] bi
 | 
					
						
							|  |  |  |     generate-vocab ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ERROR: specialized-array-vocab-not-loaded c-type ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 19:27:36 -05:00
										 |  |  | M: c-type-word c-array-constructor | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     underlying-type | 
					
						
							|  |  |  |     dup [ name>> "<" "-array>" surround ] [ specialized-array-vocab ] bi lookup | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 22:23:47 -05:00
										 |  |  | M: pointer c-array-constructor drop void* c-array-constructor ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 19:27:36 -05:00
										 |  |  | M: c-type-word c-(array)-constructor | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     underlying-type | 
					
						
							|  |  |  |     dup [ name>> "(" "-array)" surround ] [ specialized-array-vocab ] bi lookup | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 22:23:47 -05:00
										 |  |  | M: pointer c-(array)-constructor drop void* c-(array)-constructor ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 19:27:36 -05:00
										 |  |  | M: c-type-word c-direct-array-constructor | 
					
						
							| 
									
										
										
										
											2009-10-19 05:41:53 -04:00
										 |  |  |     underlying-type | 
					
						
							|  |  |  |     dup [ name>> "<direct-" "-array>" surround ] [ specialized-array-vocab ] bi lookup | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  |     [ ] [ specialized-array-vocab-not-loaded ] ?if ; foldable
 | 
					
						
							| 
									
										
										
										
											2010-02-24 02:18:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-21 22:23:47 -05:00
										 |  |  | M: pointer c-direct-array-constructor drop void* c-direct-array-constructor ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-09 11:43:37 -04:00
										 |  |  | SYNTAX: SPECIALIZED-ARRAYS: | 
					
						
							| 
									
										
										
										
											2010-03-01 01:06:47 -05:00
										 |  |  |     ";" [ parse-c-type define-array-vocab use-vocab ] each-token ;
 | 
					
						
							| 
									
										
										
										
											2009-10-09 11:43:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | SYNTAX: SPECIALIZED-ARRAY: | 
					
						
							| 
									
										
										
										
											2009-09-16 21:54:22 -04:00
										 |  |  |     scan-c-type define-array-vocab use-vocab ;
 | 
					
						
							| 
									
										
										
										
											2009-09-09 23:33:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-18 01:13:37 -04:00
										 |  |  | "prettyprint" "specialized-arrays.prettyprint" require-when | 
					
						
							| 
									
										
										
										
											2009-10-23 05:24:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-18 01:13:37 -04:00
										 |  |  | "mirrors" "specialized-arrays.mirrors" require-when |