| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: fry kernel sequences assocs accessors namespaces | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | math.intervals arrays classes.algebra combinators columns | 
					
						
							| 
									
										
										
										
											2008-08-10 00:00:27 -04:00
										 |  |  | stack-checker.branches | 
					
						
							| 
									
										
										
										
											2008-12-06 12:17:19 -05:00
										 |  |  | compiler.utilities | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | compiler.tree | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | compiler.tree.combinators | 
					
						
							| 
									
										
										
										
											2008-07-22 05:45:03 -04:00
										 |  |  | compiler.tree.propagation.info | 
					
						
							|  |  |  | compiler.tree.propagation.nodes | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | compiler.tree.propagation.simple | 
					
						
							|  |  |  | compiler.tree.propagation.constraints ;
 | 
					
						
							|  |  |  | IN: compiler.tree.propagation.branches | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! For conditionals, an assoc of child node # --> constraint | 
					
						
							|  |  |  | GENERIC: child-constraints ( node -- seq )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #if child-constraints | 
					
						
							| 
									
										
										
										
											2008-07-24 00:50:21 -04:00
										 |  |  |     in-d>> first [ =t ] [ =f ] bi 2array ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-24 01:14:13 -04:00
										 |  |  | M: #dispatch child-constraints | 
					
						
							|  |  |  |     children>> length f <repetition> ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | GENERIC: live-branches ( #branch -- indices )
 | 
					
						
							| 
									
										
										
										
											2008-07-24 00:50:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | M: #if live-branches | 
					
						
							|  |  |  |     in-d>> first value-info class>> { | 
					
						
							| 
									
										
										
										
											2008-07-30 16:37:40 -04:00
										 |  |  |         { [ dup null-class? ] [ { f f } ] } | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  |         { [ dup true-class? ] [ { t f } ] } | 
					
						
							|  |  |  |         { [ dup false-class? ] [ { f t } ] } | 
					
						
							|  |  |  |         [ { t t } ] | 
					
						
							|  |  |  |     } cond nip ;
 | 
					
						
							| 
									
										
										
										
											2008-07-24 00:50:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | M: #dispatch live-branches | 
					
						
							|  |  |  |     [ children>> length ] [ in-d>> first value-info interval>> ] bi
 | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  |     '[ _ interval-contains? ] map ;
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : live-children ( #branch -- children )
 | 
					
						
							|  |  |  |     [ children>> ] [ live-branches>> ] bi select-children ;
 | 
					
						
							| 
									
										
										
										
											2008-07-24 00:50:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 21:25:42 -04:00
										 |  |  | SYMBOL: infer-children-data | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  | : copy-value-info ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 09:49:00 -05:00
										 |  |  |     value-infos [ H{ } clone suffix ] change
 | 
					
						
							|  |  |  |     constraints [ H{ } clone suffix ] change ;
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-02 00:31:43 -04:00
										 |  |  | : no-value-info ( -- )
 | 
					
						
							|  |  |  |     value-infos off
 | 
					
						
							|  |  |  |     constraints off ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 21:25:42 -04:00
										 |  |  | : infer-children ( node -- )
 | 
					
						
							| 
									
										
										
										
											2008-07-24 00:50:21 -04:00
										 |  |  |     [ live-children ] [ child-constraints ] bi [ | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-08-02 00:31:43 -04:00
										 |  |  |             over
 | 
					
						
							|  |  |  |             [ copy-value-info assume (propagate) ] | 
					
						
							|  |  |  |             [ 2drop no-value-info ] | 
					
						
							|  |  |  |             if
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  |         ] H{ } make-assoc
 | 
					
						
							| 
									
										
										
										
											2008-07-27 21:25:42 -04:00
										 |  |  |     ] 2map infer-children-data set ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  | : compute-phi-input-infos ( phi-in -- phi-info )
 | 
					
						
							|  |  |  |     infer-children-data get
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         '[ | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  |             _ [ | 
					
						
							| 
									
										
										
										
											2008-08-10 00:00:27 -04:00
										 |  |  |                 dup +bottom+ eq?
 | 
					
						
							|  |  |  |                 [ drop null-info ] [ value-info ] if
 | 
					
						
							|  |  |  |             ] bind | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  |         ] map
 | 
					
						
							|  |  |  |     ] 2map ;
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | : annotate-phi-inputs ( #phi -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-18 21:49:03 -04:00
										 |  |  |     dup phi-in-d>> compute-phi-input-infos >>phi-info-d drop ;
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : merge-value-infos ( infos outputs -- )
 | 
					
						
							|  |  |  |     [ [ value-infos-union ] map ] dip set-value-infos ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  | SYMBOL: condition-value | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 21:25:42 -04:00
										 |  |  | M: #phi propagate-before ( #phi -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-18 21:49:03 -04:00
										 |  |  |     [ annotate-phi-inputs ] | 
					
						
							| 
									
										
										
										
											2008-12-06 12:17:19 -05:00
										 |  |  |     [ [ phi-info-d>> flip ] [ out-d>> ] bi merge-value-infos ] | 
					
						
							| 
									
										
										
										
											2008-08-24 15:02:33 -04:00
										 |  |  |     bi ;
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : branch-phi-constraints ( output values booleans -- )
 | 
					
						
							|  |  |  |      { | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             { { t } { f } } | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 drop condition-value get
 | 
					
						
							|  |  |  |                 [ [ =t ] [ =t ] bi* <--> ] | 
					
						
							| 
									
										
										
										
											2008-08-07 02:08:11 -04:00
										 |  |  |                 [ [ =f ] [ =f ] bi* <--> ] 2bi /\ | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  |             ] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             { { f } { t } } | 
					
						
							|  |  |  |             [ | 
					
						
							|  |  |  |                 drop condition-value get
 | 
					
						
							|  |  |  |                 [ [ =t ] [ =f ] bi* <--> ] | 
					
						
							| 
									
										
										
										
											2008-08-07 02:08:11 -04:00
										 |  |  |                 [ [ =f ] [ =t ] bi* <--> ] 2bi /\ | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  |             ] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             { { t f } { f } } | 
					
						
							| 
									
										
										
										
											2008-08-07 02:08:11 -04:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 first =t | 
					
						
							|  |  |  |                 condition-value get =t /\ | 
					
						
							|  |  |  |                 swap t--> | 
					
						
							|  |  |  |             ] | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             { { f } { t f } } | 
					
						
							| 
									
										
										
										
											2008-08-07 02:08:11 -04:00
										 |  |  |             [ | 
					
						
							|  |  |  |                 second =t | 
					
						
							|  |  |  |                 condition-value get =f /\ | 
					
						
							|  |  |  |                 swap t--> | 
					
						
							|  |  |  |             ] | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2008-08-07 02:08:11 -04:00
										 |  |  |         ! { | 
					
						
							|  |  |  |         !     { { t f } { } } | 
					
						
							|  |  |  |         !     [ B | 
					
						
							|  |  |  |         !         first | 
					
						
							|  |  |  |         !         [ [ =t ] bi@ <--> ] | 
					
						
							|  |  |  |         !         [ [ =f ] bi@ <--> ] 2bi /\ | 
					
						
							|  |  |  |         !     ] | 
					
						
							|  |  |  |         ! } | 
					
						
							|  |  |  |         ! { | 
					
						
							|  |  |  |         !     { { } { t f } } | 
					
						
							|  |  |  |         !     [ | 
					
						
							|  |  |  |         !         second | 
					
						
							|  |  |  |         !         [ [ =t ] bi@ <--> ] | 
					
						
							|  |  |  |         !         [ [ =f ] bi@ <--> ] 2bi /\ | 
					
						
							|  |  |  |         !     ] | 
					
						
							|  |  |  |         ! } | 
					
						
							|  |  |  |         [ 3drop f ] | 
					
						
							|  |  |  |     } case assume ;
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #phi propagate-after ( #phi -- )
 | 
					
						
							|  |  |  |     condition-value get [ | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  |         [ out-d>> ] | 
					
						
							| 
									
										
										
										
											2008-12-06 12:17:19 -05:00
										 |  |  |         [ phi-in-d>> flip ] | 
					
						
							|  |  |  |         [ phi-info-d>> flip ] tri
 | 
					
						
							| 
									
										
										
										
											2008-08-10 00:00:27 -04:00
										 |  |  |         [ | 
					
						
							|  |  |  |             [ possible-boolean-values ] map
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  |             branch-phi-constraints | 
					
						
							| 
									
										
										
										
											2008-08-10 00:00:27 -04:00
										 |  |  |         ] 3each
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  |     ] [ drop ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #phi propagate-around ( #phi -- )
 | 
					
						
							|  |  |  |     [ propagate-before ] [ propagate-after ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #branch propagate-around | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  |     dup live-branches >>live-branches | 
					
						
							| 
									
										
										
										
											2008-07-27 21:25:42 -04:00
										 |  |  |     [ infer-children ] [ annotate-node ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-07-28 07:31:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #if propagate-around | 
					
						
							|  |  |  |     [ in-d>> first condition-value set ] [ call-next-method ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #dispatch propagate-around | 
					
						
							|  |  |  |     condition-value off call-next-method ;
 |