| 
									
										
										
										
											2010-04-18 16:26:59 -04:00
										 |  |  | ! Copyright (C) 2008, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2010-04-18 16:26:59 -04:00
										 |  |  | USING: kernel classes.algebra sequences accessors arrays fry | 
					
						
							|  |  |  | math math.intervals layouts combinators namespaces locals | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  | stack-checker.inlining | 
					
						
							| 
									
										
										
										
											2008-07-22 05:45:03 -04:00
										 |  |  | compiler.tree | 
					
						
							| 
									
										
										
										
											2008-08-02 00:31:43 -04:00
										 |  |  | compiler.tree.combinators | 
					
						
							| 
									
										
										
										
											2008-08-07 07:34:28 -04:00
										 |  |  | compiler.tree.propagation.copy | 
					
						
							| 
									
										
										
										
											2008-07-22 05:45:03 -04:00
										 |  |  | compiler.tree.propagation.info | 
					
						
							|  |  |  | compiler.tree.propagation.nodes | 
					
						
							|  |  |  | compiler.tree.propagation.simple | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  | compiler.tree.propagation.branches | 
					
						
							|  |  |  | compiler.tree.propagation.constraints ;
 | 
					
						
							| 
									
										
										
										
											2010-04-18 16:26:59 -04:00
										 |  |  | FROM: sequences.private => array-capacity ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | IN: compiler.tree.propagation.recursive | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 02:08:11 -04:00
										 |  |  | : check-fixed-point ( node infos1 infos2 -- )
 | 
					
						
							|  |  |  |     [ value-info<= ] 2all?
 | 
					
						
							|  |  |  |     [ drop ] [ label>> f >>fixed-point drop ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  | : latest-input-infos ( node -- infos )
 | 
					
						
							|  |  |  |     in-d>> [ value-info ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | : recursive-stacks ( #enter-recursive -- stacks initial )
 | 
					
						
							| 
									
										
										
										
											2009-08-04 20:18:40 -04:00
										 |  |  |     [ label>> calls>> [ node>> node-input-infos ] map flip ] | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  |     [ latest-input-infos ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-18 16:26:59 -04:00
										 |  |  | : counter-class ( interval class -- class' )
 | 
					
						
							|  |  |  |     dup fixnum class<= [ | 
					
						
							|  |  |  |         swap array-capacity-interval interval-subset? | 
					
						
							|  |  |  |         [ drop array-capacity ] when
 | 
					
						
							|  |  |  |     ] [ nip ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-10 02:16:49 -04:00
										 |  |  | :: generalize-counter-interval ( interval initial-interval class -- interval' )
 | 
					
						
							| 
									
										
										
										
											2010-04-18 16:26:59 -04:00
										 |  |  |     interval class counter-class :> class | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-08-10 02:16:49 -04:00
										 |  |  |         { [ interval initial-interval interval-subset? ] [ initial-interval ] } | 
					
						
							|  |  |  |         { [ interval empty-interval eq? ] [ initial-interval ] } | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             [ interval initial-interval interval>= t eq? ] | 
					
						
							|  |  |  |             [ class max-value [a,a] initial-interval interval-union ] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             [ interval initial-interval interval<= t eq? ] | 
					
						
							|  |  |  |             [ class min-value [a,a] initial-interval interval-union ] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         [ class class-interval ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | : generalize-counter ( info' initial -- info )
 | 
					
						
							| 
									
										
										
										
											2009-03-12 18:30:24 -04:00
										 |  |  |     2dup [ not ] either? [ drop ] [ | 
					
						
							|  |  |  |         2dup [ class>> null-class? ] either? [ drop ] [ | 
					
						
							|  |  |  |             [ clone ] dip
 | 
					
						
							| 
									
										
										
										
											2009-08-10 02:16:49 -04:00
										 |  |  |             [ [ drop ] [ [ [ interval>> ] bi@ ] [ drop class>> ] 2bi generalize-counter-interval ] 2bi >>interval ] | 
					
						
							| 
									
										
										
										
											2009-03-12 18:30:24 -04:00
										 |  |  |             [ [ drop ] [ [ slots>> ] bi@ [ generalize-counter ] 2map ] 2bi >>slots ] | 
					
						
							| 
									
										
										
										
											2010-03-09 15:58:44 -05:00
										 |  |  |             bi
 | 
					
						
							| 
									
										
										
										
											2009-03-12 18:30:24 -04:00
										 |  |  |         ] if
 | 
					
						
							| 
									
										
										
										
											2008-07-30 16:37:40 -04:00
										 |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : unify-recursive-stacks ( stacks initial -- infos )
 | 
					
						
							|  |  |  |     over empty? [ nip ] [ | 
					
						
							|  |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-08-10 00:00:27 -04:00
										 |  |  |             [ value-infos-union ] dip
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  |             [ generalize-counter ] keep
 | 
					
						
							|  |  |  |             value-info-union | 
					
						
							|  |  |  |         ] 2map
 | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-07-22 05:45:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | : propagate-recursive-phi ( #enter-recursive -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  |     [ recursive-stacks unify-recursive-stacks ] keep
 | 
					
						
							|  |  |  |     out-d>> set-value-infos ;
 | 
					
						
							| 
									
										
										
										
											2008-07-22 05:45:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #recursive propagate-around ( #recursive -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  |     constraints [ H{ } clone suffix ] change
 | 
					
						
							| 
									
										
										
										
											2008-08-22 16:30:57 -04:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-12-04 08:05:59 -05:00
										 |  |  |         loop-nesting inc
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  |         constraints [ but-last H{ } clone suffix ] change
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-02 00:31:43 -04:00
										 |  |  |         child>> | 
					
						
							| 
									
										
										
										
											2008-08-07 02:08:11 -04:00
										 |  |  |         [ first compute-copy-equiv ] | 
					
						
							| 
									
										
										
										
											2008-08-02 00:31:43 -04:00
										 |  |  |         [ first propagate-recursive-phi ] | 
					
						
							|  |  |  |         [ (propagate) ] | 
					
						
							| 
									
										
										
										
											2008-08-07 02:08:11 -04:00
										 |  |  |         tri
 | 
					
						
							| 
									
										
										
										
											2008-12-04 08:05:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         loop-nesting dec
 | 
					
						
							| 
									
										
										
										
											2008-08-22 16:30:57 -04:00
										 |  |  |     ] until-fixed-point ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  | : recursive-phi-infos ( node -- infos )
 | 
					
						
							|  |  |  |     label>> enter-recursive>> node-output-infos ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | : generalize-return-interval ( info -- info' )
 | 
					
						
							| 
									
										
										
										
											2008-08-02 00:31:43 -04:00
										 |  |  |     dup [ literal?>> ] [ class>> null-class? ] bi or
 | 
					
						
							| 
									
										
										
										
											2013-01-01 21:47:23 -05:00
										 |  |  |     [ clone dup class>> class-interval >>interval ] unless ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : generalize-return ( infos -- infos' )
 | 
					
						
							|  |  |  |     [ generalize-return-interval ] map ;
 | 
					
						
							| 
									
										
										
										
											2008-07-22 05:45:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 02:08:11 -04:00
										 |  |  | : return-infos ( node -- infos )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  |     label>> return>> node-input-infos generalize-return ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : save-return-infos ( node infos -- )
 | 
					
						
							|  |  |  |     swap out-d>> set-value-infos ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 10:10:33 -05:00
										 |  |  | : unless-loop ( node quot -- )
 | 
					
						
							|  |  |  |     [ dup label>> loop?>> [ drop ] ] dip if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-24 15:02:33 -04:00
										 |  |  | M: #call-recursive propagate-before ( #call-recursive -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         [ ] [ latest-input-infos ] [ recursive-phi-infos ] tri
 | 
					
						
							|  |  |  |         check-fixed-point | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-11-11 10:10:33 -05:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  |             [ ] [ return-infos ] [ node-output-infos ] tri
 | 
					
						
							|  |  |  |             [ check-fixed-point ] [ drop save-return-infos ] 3bi
 | 
					
						
							| 
									
										
										
										
											2008-11-11 10:10:33 -05:00
										 |  |  |         ] unless-loop | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  |     ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-08-24 15:02:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #call-recursive annotate-node | 
					
						
							|  |  |  |     dup [ in-d>> ] [ out-d>> ] bi append (annotate-node) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #enter-recursive annotate-node | 
					
						
							|  |  |  |     dup out-d>> (annotate-node) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  | M: #return-recursive propagate-before ( #return-recursive -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 10:10:33 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  |         [ ] [ latest-input-infos ] [ node-input-infos ] tri
 | 
					
						
							|  |  |  |         check-fixed-point | 
					
						
							| 
									
										
										
										
											2008-11-11 10:10:33 -05:00
										 |  |  |     ] unless-loop ;
 | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-24 15:02:33 -04:00
										 |  |  | M: #return-recursive annotate-node | 
					
						
							|  |  |  |     dup in-d>> (annotate-node) ;
 |