| 
									
										
										
										
											2010-04-26 05:37:48 -04:00
										 |  |  | ! Copyright (C) 2009, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2009-06-11 18:55:14 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-07-09 00:07:06 -04:00
										 |  |  | USING: accessors arrays assocs combinators | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | combinators.short-circuit fry kernel locals namespaces | 
					
						
							|  |  |  | make math sequences hashtables | 
					
						
							| 
									
										
										
										
											2010-04-26 05:37:48 -04:00
										 |  |  | cpu.architecture | 
					
						
							| 
									
										
										
										
											2009-08-08 21:02:56 -04:00
										 |  |  | compiler.cfg | 
					
						
							| 
									
										
										
										
											2009-07-22 04:08:28 -04:00
										 |  |  | compiler.cfg.rpo | 
					
						
							| 
									
										
										
										
											2009-07-22 07:07:28 -04:00
										 |  |  | compiler.cfg.liveness | 
					
						
							| 
									
										
										
										
											2009-08-08 05:02:18 -04:00
										 |  |  | compiler.cfg.registers | 
					
						
							| 
									
										
										
										
											2009-07-13 00:00:33 -04:00
										 |  |  | compiler.cfg.utilities | 
					
						
							| 
									
										
										
										
											2009-07-09 00:07:06 -04:00
										 |  |  | compiler.cfg.instructions | 
					
						
							| 
									
										
										
										
											2009-08-09 00:06:57 -04:00
										 |  |  | compiler.cfg.predecessors | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | compiler.cfg.parallel-copy | 
					
						
							| 
									
										
										
										
											2010-04-28 05:03:12 -04:00
										 |  |  | compiler.cfg.ssa.destruction | 
					
						
							| 
									
										
										
										
											2009-07-09 00:07:06 -04:00
										 |  |  | compiler.cfg.linear-scan.assignment | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | compiler.cfg.linear-scan.allocation.state ;
 | 
					
						
							| 
									
										
										
										
											2009-06-11 18:55:14 -04:00
										 |  |  | IN: compiler.cfg.linear-scan.resolve | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-26 05:37:48 -04:00
										 |  |  | TUPLE: location | 
					
						
							|  |  |  | { reg read-only } | 
					
						
							|  |  |  | { rep read-only } | 
					
						
							|  |  |  | { reg-class read-only } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : <location> ( reg rep -- location )
 | 
					
						
							|  |  |  |     dup reg-class-of location boa ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: location equal? | 
					
						
							|  |  |  |     over location? [ | 
					
						
							| 
									
										
										
										
											2012-07-21 13:22:44 -04:00
										 |  |  |         { [ [ reg>> ] same? ] [ [ reg-class>> ] same? ] } 2&& | 
					
						
							| 
									
										
										
										
											2010-04-26 05:37:48 -04:00
										 |  |  |     ] [ 2drop f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: location hashcode* | 
					
						
							|  |  |  |     reg>> hashcode* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | SYMBOL: spill-temps | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-07 18:44:50 -04:00
										 |  |  | : spill-temp ( rep -- n )
 | 
					
						
							| 
									
										
										
										
											2010-04-30 18:19:56 -04:00
										 |  |  |     rep-size spill-temps get [ next-spill-slot ] cache ;
 | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-07 18:44:50 -04:00
										 |  |  | : add-mapping ( from to rep -- )
 | 
					
						
							| 
									
										
										
										
											2010-04-26 05:37:48 -04:00
										 |  |  |     '[ _ <location> ] bi@ 2array , ;
 | 
					
						
							| 
									
										
										
										
											2009-06-11 18:55:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-28 05:03:12 -04:00
										 |  |  | :: resolve-value-data-flow ( vreg live-out live-in edge-live-in -- )
 | 
					
						
							|  |  |  |     vreg live-out ?at [ bad-vreg ] unless
 | 
					
						
							|  |  |  |     vreg live-in ?at [ edge-live-in ?at [ bad-vreg ] unless ] unless
 | 
					
						
							| 
									
										
										
										
											2009-08-08 05:02:18 -04:00
										 |  |  |     2dup = [ 2drop ] [ vreg rep-of add-mapping ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-06-21 01:20:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-28 05:03:12 -04:00
										 |  |  | :: compute-mappings ( bb to -- mappings )
 | 
					
						
							|  |  |  |     bb machine-live-out :> live-out | 
					
						
							|  |  |  |     to machine-live-in :> live-in | 
					
						
							|  |  |  |     bb to machine-edge-live-in :> edge-live-in | 
					
						
							|  |  |  |     live-out assoc-empty? [ f ] [ | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             live-in keys edge-live-in keys append [ | 
					
						
							|  |  |  |                 live-out live-in edge-live-in | 
					
						
							|  |  |  |                 resolve-value-data-flow | 
					
						
							|  |  |  |             ] each
 | 
					
						
							|  |  |  |         ] { } make | 
					
						
							| 
									
										
										
										
											2009-08-02 10:16:21 -04:00
										 |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-06-21 01:20:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | : memory->register ( from to -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-11 22:48:38 -05:00
										 |  |  |     swap [ reg>> ] [ [ rep>> ] [ reg>> ] bi ] bi* ##reload, ;
 | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : register->memory ( from to -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-11 22:48:38 -05:00
										 |  |  |     [ [ reg>> ] [ rep>> ] bi ] [ reg>> ] bi* ##spill, ;
 | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : temp->register ( from to -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-11 22:48:38 -05:00
										 |  |  |     nip [ reg>> ] [ rep>> ] [ rep>> spill-temp ] tri ##reload, ;
 | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : register->temp ( from to -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-11 22:48:38 -05:00
										 |  |  |     drop [ [ reg>> ] [ rep>> ] bi ] [ rep>> spill-temp ] bi ##spill, ;
 | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : register->register ( from to -- )
 | 
					
						
							| 
									
										
										
										
											2011-11-11 22:48:38 -05:00
										 |  |  |     swap [ reg>> ] [ [ reg>> ] [ rep>> ] bi ] bi* ##copy, ;
 | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: temp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >insn ( from to -- )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { [ over temp eq? ] [ temp->register ] } | 
					
						
							|  |  |  |         { [ dup temp eq? ] [ register->temp ] } | 
					
						
							| 
									
										
										
										
											2010-04-26 05:37:48 -04:00
										 |  |  |         { [ over reg>> spill-slot? ] [ memory->register ] } | 
					
						
							|  |  |  |         { [ dup reg>> spill-slot? ] [ register->memory ] } | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  |         [ register->register ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : mapping-instructions ( alist -- insns )
 | 
					
						
							| 
									
										
										
										
											2009-07-28 09:47:35 -04:00
										 |  |  |     [ swap ] H{ } assoc-map-as
 | 
					
						
							| 
									
										
										
										
											2011-11-11 22:48:38 -05:00
										 |  |  |     [ temp [ swap >insn ] parallel-mapping ##branch, ] { } make ;
 | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-13 00:00:33 -04:00
										 |  |  | : perform-mappings ( bb to mappings -- )
 | 
					
						
							|  |  |  |     dup empty? [ 3drop ] [ | 
					
						
							| 
									
										
										
										
											2010-04-27 10:51:00 -04:00
										 |  |  |         mapping-instructions insert-basic-block | 
					
						
							| 
									
										
										
										
											2009-08-08 21:02:56 -04:00
										 |  |  |         cfg get cfg-changed drop
 | 
					
						
							| 
									
										
										
										
											2009-06-21 01:20:01 -04:00
										 |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-06-11 18:55:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : resolve-edge-data-flow ( bb to -- )
 | 
					
						
							| 
									
										
										
										
											2009-07-13 00:00:33 -04:00
										 |  |  |     2dup compute-mappings perform-mappings ;
 | 
					
						
							| 
									
										
										
										
											2009-06-11 18:55:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : resolve-block-data-flow ( bb -- )
 | 
					
						
							| 
									
										
										
										
											2010-07-27 12:40:31 -04:00
										 |  |  |     dup kill-block?>> [ drop ] [ | 
					
						
							|  |  |  |         dup successors>> [ resolve-edge-data-flow ] with each
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-06-11 18:55:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-22 04:08:28 -04:00
										 |  |  | : resolve-data-flow ( cfg -- )
 | 
					
						
							| 
									
										
										
										
											2009-08-09 00:06:57 -04:00
										 |  |  |     needs-predecessors | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 08:39:46 -04:00
										 |  |  |     H{ } clone spill-temps set
 | 
					
						
							| 
									
										
										
										
											2009-07-22 04:08:28 -04:00
										 |  |  |     [ resolve-block-data-flow ] each-basic-block ;
 |