| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | ! Copyright (C) 2004, 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-08-13 15:17:04 -04:00
										 |  |  | USING: fry arrays generic assocs kernel math namespaces parser | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | sequences words vectors math.intervals effects classes | 
					
						
							| 
									
										
										
										
											2008-08-22 04:12:15 -04:00
										 |  |  | accessors combinators stack-checker.state stack-checker.visitor | 
					
						
							|  |  |  | stack-checker.inlining ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | IN: compiler.tree | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! High-level tree SSA form. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-24 15:02:33 -04:00
										 |  |  | TUPLE: node < identity-tuple ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 21:25:42 -04:00
										 |  |  | M: node hashcode* drop node hashcode* ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | TUPLE: #introduce < node out-d ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | : #introduce ( out-d -- node )
 | 
					
						
							|  |  |  |     \ #introduce new swap >>out-d ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 03:31:27 -04:00
										 |  |  | TUPLE: #call < node word in-d out-d body method class info ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #call ( inputs outputs word -- node )
 | 
					
						
							|  |  |  |     \ #call new
 | 
					
						
							|  |  |  |         swap >>word | 
					
						
							|  |  |  |         swap >>out-d | 
					
						
							|  |  |  |         swap >>in-d ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-24 15:02:33 -04:00
										 |  |  | TUPLE: #call-recursive < node label in-d out-d info ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #call-recursive ( inputs outputs label -- node )
 | 
					
						
							|  |  |  |     \ #call-recursive new
 | 
					
						
							|  |  |  |         swap >>label | 
					
						
							|  |  |  |         swap >>out-d | 
					
						
							|  |  |  |         swap >>in-d ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 21:25:42 -04:00
										 |  |  | TUPLE: #push < node literal out-d ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #push ( literal value -- node )
 | 
					
						
							|  |  |  |     \ #push new
 | 
					
						
							|  |  |  |         swap 1array >>out-d | 
					
						
							|  |  |  |         swap >>literal ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 07:34:28 -04:00
										 |  |  | TUPLE: #renaming < node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: #shuffle < #renaming mapping in-d out-d ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #shuffle ( inputs outputs mapping -- node )
 | 
					
						
							|  |  |  |     \ #shuffle new
 | 
					
						
							|  |  |  |         swap >>mapping | 
					
						
							|  |  |  |         swap >>out-d | 
					
						
							|  |  |  |         swap >>in-d ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : #drop ( inputs -- node )
 | 
					
						
							|  |  |  |     { } { } #shuffle ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 07:34:28 -04:00
										 |  |  | TUPLE: #>r < #renaming in-d out-r ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #>r ( inputs outputs -- node )
 | 
					
						
							|  |  |  |     \ #>r new
 | 
					
						
							|  |  |  |         swap >>out-r | 
					
						
							|  |  |  |         swap >>in-d ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 07:34:28 -04:00
										 |  |  | TUPLE: #r> < #renaming in-r out-d ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #r> ( inputs outputs -- node )
 | 
					
						
							|  |  |  |     \ #r> new
 | 
					
						
							|  |  |  |         swap >>out-d | 
					
						
							|  |  |  |         swap >>in-r ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  | TUPLE: #terminate < node in-d in-r ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  | : #terminate ( in-d in-r -- node )
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  |     \ #terminate new
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  |         swap >>in-r | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  |         swap >>in-d ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-30 04:38:10 -04:00
										 |  |  | TUPLE: #branch < node in-d children live-branches ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : new-branch ( value children class -- node )
 | 
					
						
							|  |  |  |     new
 | 
					
						
							|  |  |  |         swap >>children | 
					
						
							|  |  |  |         swap 1array >>in-d ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: #if < #branch ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : #if ( ? true false -- node )
 | 
					
						
							|  |  |  |     2array \ #if new-branch ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: #dispatch < #branch ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : #dispatch ( n branches -- node )
 | 
					
						
							|  |  |  |     \ #dispatch new-branch ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-18 21:49:03 -04:00
										 |  |  | TUPLE: #phi < node phi-in-d phi-info-d out-d terminated ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-18 21:49:03 -04:00
										 |  |  | : #phi ( d-phi-in d-phi-out terminated -- node )
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  |     \ #phi new
 | 
					
						
							| 
									
										
										
										
											2008-08-01 21:04:36 -04:00
										 |  |  |         swap >>terminated | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  |         swap >>out-d | 
					
						
							|  |  |  |         swap >>phi-in-d ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: #declare < node declaration ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-24 00:50:21 -04:00
										 |  |  | : #declare ( declaration -- node )
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  |     \ #declare new
 | 
					
						
							| 
									
										
										
										
											2008-07-24 00:50:21 -04:00
										 |  |  |         swap >>declaration ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-24 15:02:33 -04:00
										 |  |  | TUPLE: #return < node in-d info ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | : #return ( stack -- node )
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  |     \ #return new
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  |         swap >>in-d ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-15 05:09:23 -04:00
										 |  |  | TUPLE: #recursive < node in-d word label loop? child ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-15 05:09:23 -04:00
										 |  |  | : #recursive ( label inputs child -- node )
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  |     \ #recursive new
 | 
					
						
							| 
									
										
										
										
											2008-07-27 21:25:42 -04:00
										 |  |  |         swap >>child | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  |         swap >>in-d | 
					
						
							| 
									
										
										
										
											2008-08-15 05:09:23 -04:00
										 |  |  |         swap >>label ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-24 15:02:33 -04:00
										 |  |  | TUPLE: #enter-recursive < node in-d out-d label info ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #enter-recursive ( label inputs outputs -- node )
 | 
					
						
							|  |  |  |     \ #enter-recursive new
 | 
					
						
							|  |  |  |         swap >>out-d | 
					
						
							|  |  |  |         swap >>in-d | 
					
						
							|  |  |  |         swap >>label ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-24 15:02:33 -04:00
										 |  |  | TUPLE: #return-recursive < #renaming in-d out-d label info ;
 | 
					
						
							| 
									
										
										
										
											2008-07-27 03:32:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #return-recursive ( label inputs outputs -- node )
 | 
					
						
							|  |  |  |     \ #return-recursive new
 | 
					
						
							|  |  |  |         swap >>out-d | 
					
						
							|  |  |  |         swap >>in-d | 
					
						
							|  |  |  |         swap >>label ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 07:34:28 -04:00
										 |  |  | TUPLE: #copy < #renaming in-d out-d ;
 | 
					
						
							| 
									
										
										
										
											2008-07-20 05:24:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : #copy ( inputs outputs -- node )
 | 
					
						
							|  |  |  |     \ #copy new
 | 
					
						
							|  |  |  |         swap >>out-d | 
					
						
							|  |  |  |         swap >>in-d ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-12 03:41:18 -04:00
										 |  |  | TUPLE: #alien-node < node params ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : new-alien-node ( params class -- node )
 | 
					
						
							|  |  |  |     new
 | 
					
						
							|  |  |  |         over in-d>> >>in-d | 
					
						
							|  |  |  |         over out-d>> >>out-d | 
					
						
							|  |  |  |         swap >>params ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: #alien-invoke < #alien-node in-d out-d ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : #alien-invoke ( params -- node )
 | 
					
						
							|  |  |  |     \ #alien-invoke new-alien-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: #alien-indirect < #alien-node in-d out-d ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : #alien-indirect ( params -- node )
 | 
					
						
							|  |  |  |     \ #alien-indirect new-alien-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: #alien-callback < #alien-node ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : #alien-callback ( params -- node )
 | 
					
						
							|  |  |  |     \ #alien-callback new
 | 
					
						
							|  |  |  |         swap >>params ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 21:25:42 -04:00
										 |  |  | : node, ( node -- ) stack-visitor get push ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-07 07:34:28 -04:00
										 |  |  | GENERIC: inputs/outputs ( #renaming -- inputs outputs )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #shuffle inputs/outputs mapping>> unzip swap ;
 | 
					
						
							|  |  |  | M: #>r inputs/outputs [ in-d>> ] [ out-r>> ] bi ;
 | 
					
						
							|  |  |  | M: #r> inputs/outputs [ in-r>> ] [ out-d>> ] bi ;
 | 
					
						
							|  |  |  | M: #copy inputs/outputs [ in-d>> ] [ out-d>> ] bi ;
 | 
					
						
							|  |  |  | M: #return-recursive inputs/outputs [ in-d>> ] [ out-d>> ] bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-12 00:30:18 -04:00
										 |  |  | : shuffle-effect ( #shuffle -- effect )
 | 
					
						
							|  |  |  |     [ in-d>> ] [ out-d>> ] [ mapping>> ] tri
 | 
					
						
							| 
									
										
										
										
											2008-08-13 15:17:04 -04:00
										 |  |  |     '[ , at ] map
 | 
					
						
							| 
									
										
										
										
											2008-08-12 00:30:18 -04:00
										 |  |  |     <effect> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-13 15:17:04 -04:00
										 |  |  | : recursive-phi-in ( #enter-recursive -- seq )
 | 
					
						
							|  |  |  |     [ label>> calls>> [ in-d>> ] map ] [ in-d>> ] bi suffix ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-18 22:30:10 -04:00
										 |  |  | : ends-with-terminate? ( nodes -- ? )
 | 
					
						
							|  |  |  |     dup empty? [ drop f ] [ peek #terminate? ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-27 21:25:42 -04:00
										 |  |  | M: vector child-visitor V{ } clone ;
 | 
					
						
							|  |  |  | M: vector #introduce, #introduce node, ;
 | 
					
						
							|  |  |  | M: vector #call, #call node, ;
 | 
					
						
							|  |  |  | M: vector #push, #push node, ;
 | 
					
						
							|  |  |  | M: vector #shuffle, #shuffle node, ;
 | 
					
						
							|  |  |  | M: vector #drop, #drop node, ;
 | 
					
						
							|  |  |  | M: vector #>r, #>r node, ;
 | 
					
						
							|  |  |  | M: vector #r>, #r> node, ;
 | 
					
						
							|  |  |  | M: vector #return, #return node, ;
 | 
					
						
							|  |  |  | M: vector #enter-recursive, #enter-recursive node, ;
 | 
					
						
							|  |  |  | M: vector #return-recursive, #return-recursive node, ;
 | 
					
						
							|  |  |  | M: vector #call-recursive, #call-recursive node, ;
 | 
					
						
							|  |  |  | M: vector #terminate, #terminate node, ;
 | 
					
						
							|  |  |  | M: vector #if, #if node, ;
 | 
					
						
							|  |  |  | M: vector #dispatch, #dispatch node, ;
 | 
					
						
							|  |  |  | M: vector #phi, #phi node, ;
 | 
					
						
							|  |  |  | M: vector #declare, #declare node, ;
 | 
					
						
							|  |  |  | M: vector #recursive, #recursive node, ;
 | 
					
						
							|  |  |  | M: vector #copy, #copy node, ;
 | 
					
						
							| 
									
										
										
										
											2008-08-12 03:41:18 -04:00
										 |  |  | M: vector #alien-invoke, #alien-invoke node, ;
 | 
					
						
							|  |  |  | M: vector #alien-indirect, #alien-indirect node, ;
 | 
					
						
							|  |  |  | M: vector #alien-callback, #alien-callback node, ;
 |