| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | USING: kernel sequences accessors arrays fry math.intervals | 
					
						
							|  |  |  | combinators | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  | stack-checker.inlining | 
					
						
							| 
									
										
										
										
											2008-07-22 05:45:03 -04:00
										 |  |  | compiler.tree | 
					
						
							|  |  |  | 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 ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | IN: compiler.tree.propagation.recursive | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | : check-fixed-point ( node infos1 infos2 -- node )
 | 
					
						
							| 
									
										
										
										
											2008-07-27 23:47:40 -04:00
										 |  |  |     sequence= [ dup label>> f >>fixed-point drop ] unless ; inline
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : recursive-stacks ( #enter-recursive -- stacks initial )
 | 
					
						
							| 
									
										
										
										
											2008-07-27 23:47:40 -04:00
										 |  |  |     [ label>> calls>> [ node-input-infos ] map flip ] | 
					
						
							|  |  |  |     [ in-d>> [ value-info ] map ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 23:47:40 -04:00
										 |  |  | : generalize-counter-interval ( interval initial-interval -- interval' )
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-07-27 23:47:40 -04:00
										 |  |  |         { [ 2dup = ] [ empty-interval ] } | 
					
						
							|  |  |  |         { [ over empty-interval eq? ] [ empty-interval ] } | 
					
						
							|  |  |  |         { [ 2dup interval>= t eq? ] [ 1./0. [a,a] ] } | 
					
						
							|  |  |  |         { [ 2dup interval<= t eq? ] [ -1./0. [a,a] ] } | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  |         [ [-inf,inf] ] | 
					
						
							|  |  |  |     } cond nip interval-union ;
 | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | : generalize-counter ( info' initial -- info )
 | 
					
						
							|  |  |  |     [ drop clone ] [ [ interval>> ] bi@ ] 2bi
 | 
					
						
							| 
									
										
										
										
											2008-07-27 23:47:40 -04:00
										 |  |  |     generalize-counter-interval >>interval ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : unify-recursive-stacks ( stacks initial -- infos )
 | 
					
						
							|  |  |  |     over empty? [ nip ] [ | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             [ sift value-infos-union ] dip
 | 
					
						
							|  |  |  |             [ 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 -- )
 | 
					
						
							|  |  |  |     [ ] [ recursive-stacks unify-recursive-stacks ] [ ] tri
 | 
					
						
							|  |  |  |     [ node-output-infos check-fixed-point drop ] 2keep
 | 
					
						
							|  |  |  |     out-d>> set-value-infos ;
 | 
					
						
							| 
									
										
										
										
											2008-07-22 05:45:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  | USING: namespaces math ;
 | 
					
						
							|  |  |  | SYMBOL: iter-counter | 
					
						
							|  |  |  | 0 iter-counter set-global
 | 
					
						
							| 
									
										
										
										
											2008-07-22 05:45:03 -04:00
										 |  |  | M: #recursive propagate-around ( #recursive -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-26 20:01:43 -04:00
										 |  |  |     iter-counter inc
 | 
					
						
							|  |  |  |     iter-counter get 10 > [ "Oops" throw ] when
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  |     dup label>> t >>fixed-point drop [ | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             child>> | 
					
						
							|  |  |  |             [ first propagate-recursive-phi ] | 
					
						
							|  |  |  |             [ (propagate) ] | 
					
						
							|  |  |  |             bi
 | 
					
						
							|  |  |  |         ] save-constraints | 
					
						
							|  |  |  |     ] [ dup label>> fixed-point>> [ drop ] [ propagate-around ] if ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : generalize-return-interval ( info -- info' )
 | 
					
						
							|  |  |  |     dup literal?>> [ | 
					
						
							|  |  |  |         clone [-inf,inf] >>interval | 
					
						
							|  |  |  |     ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : generalize-return ( infos -- infos' )
 | 
					
						
							|  |  |  |     [ generalize-return-interval ] map ;
 | 
					
						
							| 
									
										
										
										
											2008-07-22 05:45:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #call-recursive propagate-before ( #call-label -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-27 23:47:40 -04:00
										 |  |  |     dup [ node-output-infos ] [ label>> return>> node-input-infos ] bi
 | 
					
						
							|  |  |  |     [ check-fixed-point ] keep
 | 
					
						
							|  |  |  |     generalize-return swap out-d>> set-value-infos ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #return-recursive propagate-before ( #return-recursive -- )
 | 
					
						
							|  |  |  |     dup [ node-input-infos ] [ in-d>> [ value-info ] map ] bi
 | 
					
						
							|  |  |  |     check-fixed-point drop ;
 |