| 
									
										
										
										
											2011-01-17 18:16:17 -05:00
										 |  |  | ! Copyright (C) 2008, 2011 Slava Pestov, Daniel Ehrenberg. | 
					
						
							| 
									
										
										
										
											2008-10-20 02:56:28 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2009-09-04 04:01:18 -04:00
										 |  |  | USING: accessors assocs arrays classes combinators | 
					
						
							| 
									
										
										
										
											2010-05-18 18:36:47 -04:00
										 |  |  | compiler.units fry generalizations sequences.generalizations | 
					
						
							|  |  |  | generic kernel locals namespaces quotations sequences sets slots | 
					
						
							|  |  |  | words compiler.cfg.instructions compiler.cfg.instructions.syntax | 
					
						
							| 
									
										
										
										
											2010-07-27 12:40:31 -04:00
										 |  |  | compiler.cfg.rpo compiler.cfg ;
 | 
					
						
							| 
									
										
										
										
											2010-02-26 16:01:01 -05:00
										 |  |  | FROM: namespaces => set ;
 | 
					
						
							| 
									
										
										
										
											2010-03-02 19:23:34 -05:00
										 |  |  | FROM: sets => members ;
 | 
					
						
							| 
									
										
										
										
											2008-10-20 02:56:28 -04:00
										 |  |  | IN: compiler.cfg.def-use | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-17 18:16:17 -05:00
										 |  |  | ! Utilities for iterating over instruction operands | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Def-use protocol | 
					
						
							| 
									
										
										
										
											2010-07-13 07:40:14 -04:00
										 |  |  | GENERIC: defs-vregs ( insn -- seq )
 | 
					
						
							| 
									
										
										
										
											2009-05-29 14:11:34 -04:00
										 |  |  | GENERIC: temp-vregs ( insn -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-10-20 02:56:28 -04:00
										 |  |  | GENERIC: uses-vregs ( insn -- seq )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-13 07:40:14 -04:00
										 |  |  | M: insn defs-vregs drop { } ;
 | 
					
						
							| 
									
										
										
										
											2009-11-02 21:11:29 -05:00
										 |  |  | M: insn temp-vregs drop { } ;
 | 
					
						
							|  |  |  | M: insn uses-vregs drop { } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-17 18:16:17 -05:00
										 |  |  | ! Instructions with unusual operands, also see these passes | 
					
						
							|  |  |  | ! for special behavior: | 
					
						
							|  |  |  | ! - compiler.cfg.renaming.functor | 
					
						
							|  |  |  | ! - compiler.cfg.representations.preferred | 
					
						
							|  |  |  | CONSTANT: special-vreg-insns { | 
					
						
							|  |  |  |     ##parallel-copy | 
					
						
							|  |  |  |     ##phi | 
					
						
							|  |  |  |     ##alien-invoke | 
					
						
							|  |  |  |     ##alien-indirect | 
					
						
							|  |  |  |     ##alien-assembly | 
					
						
							|  |  |  |     ##callback-inputs | 
					
						
							|  |  |  |     ##callback-outputs | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Special defs-vregs methods | 
					
						
							|  |  |  | M: ##parallel-copy defs-vregs values>> [ first ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: ##phi defs-vregs dst>> 1array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: alien-call-insn defs-vregs | 
					
						
							|  |  |  |     reg-outputs>> [ first ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: ##callback-inputs defs-vregs | 
					
						
							|  |  |  |     [ reg-outputs>> ] [ stack-outputs>> ] bi append [ first ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: ##callback-outputs defs-vregs drop { } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Special uses-vregs methods | 
					
						
							|  |  |  | M: ##parallel-copy uses-vregs values>> [ second ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: ##phi uses-vregs inputs>> values ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: alien-call-insn uses-vregs | 
					
						
							|  |  |  |     [ reg-inputs>> ] [ stack-inputs>> ] bi append [ first ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: ##alien-indirect uses-vregs | 
					
						
							|  |  |  |     [ call-next-method ] [ src>> ] bi prefix ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: ##callback-inputs uses-vregs | 
					
						
							|  |  |  |     drop { } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: ##callback-outputs uses-vregs | 
					
						
							|  |  |  |     reg-inputs>> [ first ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Generate defs-vregs, uses-vregs and temp-vregs for everything | 
					
						
							|  |  |  | ! else | 
					
						
							| 
									
										
										
										
											2009-09-02 07:22:37 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : slot-array-quot ( slots -- quot )
 | 
					
						
							| 
									
										
										
										
											2009-09-04 04:01:18 -04:00
										 |  |  |     [ reader-word 1quotation ] map dup length { | 
					
						
							|  |  |  |         { 0 [ drop [ drop f ] ] } | 
					
						
							|  |  |  |         { 1 [ first [ 1array ] compose ] } | 
					
						
							|  |  |  |         { 2 [ first2 '[ _ _ bi 2array ] ] } | 
					
						
							|  |  |  |         [ '[ _ cleave _ narray ] ] | 
					
						
							|  |  |  |     } case ;
 | 
					
						
							| 
									
										
										
										
											2009-09-02 07:22:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-13 07:40:14 -04:00
										 |  |  | : define-vregs-method ( insn slots word -- )
 | 
					
						
							|  |  |  |     [ [ drop ] ] dip '[ | 
					
						
							|  |  |  |         [ _ create-method ] | 
					
						
							|  |  |  |         [ [ name>> ] map slot-array-quot ] bi*
 | 
					
						
							| 
									
										
										
										
											2009-11-02 21:11:29 -05:00
										 |  |  |         define | 
					
						
							| 
									
										
										
										
											2010-07-13 07:40:14 -04:00
										 |  |  |     ] if-empty ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-defs-vregs-method ( insn -- )
 | 
					
						
							|  |  |  |     dup insn-def-slots \ defs-vregs define-vregs-method ;
 | 
					
						
							| 
									
										
										
										
											2009-09-02 07:22:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : define-uses-vregs-method ( insn -- )
 | 
					
						
							| 
									
										
										
										
											2010-07-13 07:40:14 -04:00
										 |  |  |     dup insn-use-slots \ uses-vregs define-vregs-method ;
 | 
					
						
							| 
									
										
										
										
											2009-09-02 07:22:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : define-temp-vregs-method ( insn -- )
 | 
					
						
							| 
									
										
										
										
											2010-07-13 07:40:14 -04:00
										 |  |  |     dup insn-temp-slots \ temp-vregs define-vregs-method ;
 | 
					
						
							| 
									
										
										
										
											2009-09-02 07:22:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [ | 
					
						
							|  |  |  |     insn-classes get
 | 
					
						
							| 
									
										
										
										
											2010-07-13 07:40:14 -04:00
										 |  |  |     [ special-vreg-insns diff [ define-defs-vregs-method ] each ] | 
					
						
							|  |  |  |     [ special-vreg-insns diff [ define-uses-vregs-method ] each ] | 
					
						
							| 
									
										
										
										
											2009-09-02 07:22:37 -04:00
										 |  |  |     [ [ define-temp-vregs-method ] each ] | 
					
						
							|  |  |  |     tri
 | 
					
						
							|  |  |  | ] with-compilation-unit | 
					
						
							| 
									
										
										
										
											2009-07-26 22:10:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-17 18:16:17 -05:00
										 |  |  | ! Computing vreg -> insn -> bb mapping | 
					
						
							| 
									
										
										
										
											2010-07-19 09:27:10 -04:00
										 |  |  | SYMBOLS: defs insns ;
 | 
					
						
							| 
									
										
										
										
											2009-07-28 13:29:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : def-of ( vreg -- node ) defs get at ;
 | 
					
						
							| 
									
										
										
										
											2009-07-26 22:10:14 -04:00
										 |  |  | : insn-of ( vreg -- insn ) insns get at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 13:29:07 -04:00
										 |  |  | : set-def-of ( obj insn assoc -- )
 | 
					
						
							| 
									
										
										
										
											2010-07-13 07:40:14 -04:00
										 |  |  |     swap defs-vregs [ swap set-at ] with with each ;
 | 
					
						
							| 
									
										
										
										
											2009-07-26 22:10:14 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-28 13:29:07 -04:00
										 |  |  | : compute-defs ( cfg -- )
 | 
					
						
							| 
									
										
										
										
											2009-07-26 22:10:14 -04:00
										 |  |  |     H{ } clone [ | 
					
						
							|  |  |  |         '[ | 
					
						
							| 
									
										
										
										
											2010-07-27 12:40:31 -04:00
										 |  |  |             [ basic-block get ] dip [ | 
					
						
							| 
									
										
										
										
											2009-07-28 13:29:07 -04:00
										 |  |  |                 _ set-def-of | 
					
						
							| 
									
										
										
										
											2009-07-26 22:10:14 -04:00
										 |  |  |             ] with each
 | 
					
						
							| 
									
										
										
										
											2010-07-27 12:40:31 -04:00
										 |  |  |         ] simple-analysis | 
					
						
							| 
									
										
										
										
											2010-07-19 09:27:10 -04:00
										 |  |  |     ] keep defs set ;
 | 
					
						
							| 
									
										
										
										
											2009-07-26 22:10:14 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : compute-insns ( cfg -- )
 | 
					
						
							|  |  |  |     H{ } clone [ | 
					
						
							|  |  |  |         '[ | 
					
						
							| 
									
										
										
										
											2010-07-27 12:40:31 -04:00
										 |  |  |             [ | 
					
						
							| 
									
										
										
										
											2009-07-28 13:29:07 -04:00
										 |  |  |                 dup _ set-def-of | 
					
						
							| 
									
										
										
										
											2009-07-26 22:10:14 -04:00
										 |  |  |             ] each
 | 
					
						
							| 
									
										
										
										
											2010-07-27 12:40:31 -04:00
										 |  |  |         ] simple-analysis | 
					
						
							| 
									
										
										
										
											2009-07-26 22:10:14 -04:00
										 |  |  |     ] keep insns set ;
 |