| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: fry assocs arrays byte-arrays strings accessors sequences | 
					
						
							|  |  |  | kernel slots classes.algebra classes.tuple classes.tuple.private | 
					
						
							|  |  |  | words math math.private combinators sequences.private namespaces | 
					
						
							| 
									
										
										
										
											2008-07-30 16:37:40 -04:00
										 |  |  | slots.private classes compiler.tree.propagation.info ;
 | 
					
						
							| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  | IN: compiler.tree.propagation.slots | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Propagation of immutable slots and array lengths | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Revisit this code when delegation is removed and when complex | 
					
						
							|  |  |  | ! numbers become tuples. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | UNION: fixed-length-sequence array byte-array string ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | : sequence-constructor? ( word -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-12-09 19:17:04 -05:00
										 |  |  |     { <array> <byte-array> (byte-array) <string> } memq? ;
 | 
					
						
							| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : constructor-output-class ( word -- class )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { <array> array } | 
					
						
							|  |  |  |         { <byte-array> byte-array } | 
					
						
							| 
									
										
										
										
											2008-12-09 19:17:04 -05:00
										 |  |  |         { (byte-array) byte-array } | 
					
						
							| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  |         { <string> string } | 
					
						
							|  |  |  |     } at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | : propagate-sequence-constructor ( #call word -- infos )
 | 
					
						
							| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  |     [ in-d>> first <sequence-info> ] | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  |     [ constructor-output-class <class-info> ] | 
					
						
							|  |  |  |     bi* value-info-intersect 1array ;
 | 
					
						
							| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | : tuple-constructor? ( word -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-08-02 00:31:43 -04:00
										 |  |  |     { <tuple-boa> <complex> } memq? ;
 | 
					
						
							| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  | : fold-<tuple-boa> ( values class -- info )
 | 
					
						
							| 
									
										
										
										
											2008-09-03 04:46:56 -04:00
										 |  |  |     [ [ literal>> ] map ] dip prefix >tuple | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  |     <literal-info> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 18:36:24 -04:00
										 |  |  | : (propagate-tuple-constructor) ( values class -- info )
 | 
					
						
							|  |  |  |     [ [ value-info ] map ] dip [ read-only-slots ] keep
 | 
					
						
							| 
									
										
										
										
											2008-09-03 04:46:56 -04:00
										 |  |  |     over rest-slice [ dup [ literal?>> ] when ] all? [ | 
					
						
							|  |  |  |         [ rest-slice ] dip fold-<tuple-boa> | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         <tuple-info> | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 18:36:24 -04:00
										 |  |  | : propagate-<tuple-boa> ( #call -- info )
 | 
					
						
							|  |  |  |     in-d>> unclip-last
 | 
					
						
							| 
									
										
										
										
											2008-11-05 23:20:29 -05:00
										 |  |  |     value-info literal>> first (propagate-tuple-constructor) ;
 | 
					
						
							| 
									
										
										
										
											2008-07-30 18:36:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | : propagate-<complex> ( #call -- info )
 | 
					
						
							| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  |     in-d>> [ value-info ] map complex <tuple-info> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | : propagate-tuple-constructor ( #call word -- infos )
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  |         { \ <tuple-boa> [ propagate-<tuple-boa> ] } | 
					
						
							|  |  |  |         { \ <complex> [ propagate-<complex> ] } | 
					
						
							|  |  |  |     } case 1array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | : read-only-slot? ( n class -- ? )
 | 
					
						
							|  |  |  |     all-slots [ offset>> = ] with find nip
 | 
					
						
							|  |  |  |     dup [ read-only>> ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : literal-info-slot ( slot object -- info/f )
 | 
					
						
							| 
									
										
										
										
											2008-07-30 16:37:40 -04:00
										 |  |  |     2dup class read-only-slot? | 
					
						
							|  |  |  |     [ swap slot <literal-info> ] [ 2drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : length-accessor? ( slot info -- ? )
 | 
					
						
							|  |  |  |     [ 1 = ] [ length>> ] bi* and ;
 | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-25 03:07:45 -04:00
										 |  |  | : value-info-slot ( slot info -- info' )
 | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |         { [ over 0 = ] [ 2drop fixnum <class-info> ] } | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  |         { [ 2dup length-accessor? ] [ nip length>> ] } | 
					
						
							|  |  |  |         { [ dup literal?>> ] [ literal>> literal-info-slot ] } | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  |         [ [ 1- ] [ slots>> ] bi* ?nth ] | 
					
						
							| 
									
										
										
										
											2008-07-30 16:37:40 -04:00
										 |  |  |     } cond [ object-info ] unless* ;
 |