| 
									
										
										
										
											2009-07-18 23:27:42 -04:00
										 |  |  | ! Copyright (C) 2009 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-08-03 08:08:28 -04:00
										 |  |  | USING: namespaces assocs kernel fry accessors sequences make math locals | 
					
						
							| 
									
										
										
										
											2009-07-21 02:24:19 -04:00
										 |  |  | combinators compiler.cfg compiler.cfg.hats compiler.cfg.instructions | 
					
						
							| 
									
										
										
										
											2009-07-23 21:54:38 -04:00
										 |  |  | compiler.cfg.utilities compiler.cfg.rpo compiler.cfg.stacks.local | 
					
						
							| 
									
										
										
										
											2009-08-08 21:02:56 -04:00
										 |  |  | compiler.cfg.stacks.global compiler.cfg.stacks.height | 
					
						
							|  |  |  | compiler.cfg.predecessors ;
 | 
					
						
							| 
									
										
										
										
											2009-07-23 21:54:38 -04:00
										 |  |  | IN: compiler.cfg.stacks.finalize | 
					
						
							| 
									
										
										
										
											2009-07-18 23:27:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 21:54:38 -04:00
										 |  |  | ! This pass inserts peeks and replaces. | 
					
						
							| 
									
										
										
										
											2009-07-18 23:27:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-03 08:08:28 -04:00
										 |  |  | :: inserting-peeks ( from to -- assoc )
 | 
					
						
							|  |  |  |     ! A peek is inserted on an edge if the destination anticipates | 
					
						
							|  |  |  |     ! the stack location, the source does not anticipate it and | 
					
						
							|  |  |  |     ! it is not available from the source in a register. | 
					
						
							|  |  |  |     to anticip-in | 
					
						
							|  |  |  |     from anticip-out from avail-out assoc-union
 | 
					
						
							|  |  |  |     assoc-diff ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: inserting-replaces ( from to -- assoc )
 | 
					
						
							|  |  |  |     ! A replace is inserted on an edge if two conditions hold: | 
					
						
							|  |  |  |     ! - the location is not dead at the destination, OR | 
					
						
							|  |  |  |     !   the location is live at the destination but not available | 
					
						
							|  |  |  |     !   at the destination | 
					
						
							|  |  |  |     ! - the location is pending in the source but not the destination | 
					
						
							|  |  |  |     from pending-out to pending-in assoc-diff
 | 
					
						
							|  |  |  |     to dead-in to live-in to anticip-in assoc-diff assoc-diff
 | 
					
						
							|  |  |  |     assoc-diff ;
 | 
					
						
							| 
									
										
										
										
											2009-07-21 02:24:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-21 23:24:50 -04:00
										 |  |  | : each-insertion ( assoc bb quot: ( vreg loc -- ) -- )
 | 
					
						
							|  |  |  |     '[ drop [ loc>vreg ] [ _ untranslate-loc ] bi @ ] assoc-each ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ERROR: bad-peek dst loc ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : insert-peeks ( from to -- )
 | 
					
						
							|  |  |  |     [ inserting-peeks ] keep
 | 
					
						
							|  |  |  |     [ dup n>> 0 < [ bad-peek ] [ ##peek ] if ] each-insertion ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : insert-replaces ( from to -- )
 | 
					
						
							|  |  |  |     [ inserting-replaces ] keep
 | 
					
						
							|  |  |  |     [ dup n>> 0 < [ 2drop ] [ ##replace ] if ] each-insertion ;
 | 
					
						
							| 
									
										
										
										
											2009-07-21 02:24:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : visit-edge ( from to -- )
 | 
					
						
							| 
									
										
										
										
											2009-08-02 10:16:21 -04:00
										 |  |  |     ! If both blocks are subroutine calls, don't bother | 
					
						
							|  |  |  |     ! computing anything. | 
					
						
							|  |  |  |     2dup [ kill-block? ] both? [ 2drop ] [ | 
					
						
							| 
									
										
										
										
											2009-08-03 08:08:28 -04:00
										 |  |  |         2dup [ [ insert-replaces ] [ insert-peeks ] 2bi ] V{ } make | 
					
						
							| 
									
										
										
										
											2009-08-14 20:41:41 -04:00
										 |  |  |         [ 2drop ] [ insert-simple-basic-block ] if-empty
 | 
					
						
							| 
									
										
										
										
											2009-08-02 10:16:21 -04:00
										 |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-07-21 02:24:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : visit-block ( bb -- )
 | 
					
						
							| 
									
										
										
										
											2009-07-23 21:54:38 -04:00
										 |  |  |     [ predecessors>> ] keep '[ _ visit-edge ] each ;
 | 
					
						
							| 
									
										
										
										
											2009-07-21 02:24:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 21:54:38 -04:00
										 |  |  | : finalize-stack-shuffling ( cfg -- cfg' )
 | 
					
						
							| 
									
										
										
										
											2009-08-08 21:02:56 -04:00
										 |  |  |     needs-predecessors | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-23 21:54:38 -04:00
										 |  |  |     dup [ visit-block ] each-basic-block | 
					
						
							| 
									
										
										
										
											2009-08-08 21:02:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-14 20:41:41 -04:00
										 |  |  |     cfg-changed ;
 |