| 
									
										
										
										
											2008-09-12 09:18:44 -04:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: accessors kernel sequences fry words math | 
					
						
							|  |  |  | math.partial-dispatch combinators arrays hashtables | 
					
						
							|  |  |  | compiler.tree | 
					
						
							|  |  |  | compiler.tree.combinators | 
					
						
							|  |  |  | compiler.tree.propagation.info ;
 | 
					
						
							|  |  |  | IN: compiler.tree.identities | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define-identities ( word identities -- )
 | 
					
						
							| 
									
										
										
										
											2011-10-09 16:51:03 -04:00
										 |  |  |     [ integer-derived-ops dup empty? f assert= ] dip
 | 
					
						
							| 
									
										
										
										
											2008-09-12 09:18:44 -04:00
										 |  |  |     '[ _ "identities" set-word-prop ] each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: X | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ + { | 
					
						
							|  |  |  |     { { X 0 } drop } | 
					
						
							|  |  |  |     { { 0 X } nip } | 
					
						
							|  |  |  | } define-identities | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ - { | 
					
						
							|  |  |  |     { { X 0 } drop } | 
					
						
							|  |  |  | } define-identities | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ * { | 
					
						
							|  |  |  |     { { X 1 } drop } | 
					
						
							|  |  |  |     { { 1 X } nip } | 
					
						
							|  |  |  |     { { X 0 } nip } | 
					
						
							|  |  |  |     { { 0 X } drop } | 
					
						
							|  |  |  | } define-identities | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ bitand { | 
					
						
							|  |  |  |     { { X -1 } drop } | 
					
						
							|  |  |  |     { { -1 X } nip } | 
					
						
							|  |  |  |     { { X 0 } nip } | 
					
						
							|  |  |  |     { { 0 X } drop } | 
					
						
							|  |  |  | } define-identities | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ bitor { | 
					
						
							|  |  |  |     { { X 0 } drop } | 
					
						
							|  |  |  |     { { 0 X } nip } | 
					
						
							|  |  |  |     { { X -1 } nip } | 
					
						
							|  |  |  |     { { -1 X } drop } | 
					
						
							|  |  |  | } define-identities | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ bitxor { | 
					
						
							|  |  |  |     { { X 0 } drop } | 
					
						
							|  |  |  |     { { 0 X } nip } | 
					
						
							|  |  |  | } define-identities | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | \ shift { | 
					
						
							|  |  |  |     { { 0 X } drop } | 
					
						
							|  |  |  |     { { X 0 } drop } | 
					
						
							|  |  |  | } define-identities | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : matches? ( pattern infos -- ? )
 | 
					
						
							|  |  |  |     [ over X eq? [ 2drop t ] [ literal>> = ] if ] 2all? ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : find-identity ( patterns infos -- result )
 | 
					
						
							|  |  |  |     '[ first _ matches? ] find swap [ second ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: apply-identities* ( node -- node )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : simplify-to-constant ( #call constant -- nodes )
 | 
					
						
							| 
									
										
										
										
											2011-11-06 23:41:31 -05:00
										 |  |  |     [ [ in-d>> <#drop> ] [ out-d>> first ] bi ] dip swap <#push> | 
					
						
							| 
									
										
										
										
											2008-09-12 09:18:44 -04:00
										 |  |  |     2array ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : select-input ( node n -- #shuffle )
 | 
					
						
							|  |  |  |     [ [ in-d>> ] [ out-d>> ] bi ] dip
 | 
					
						
							| 
									
										
										
										
											2011-11-06 23:41:31 -05:00
										 |  |  |     pick nth over first associate <#data-shuffle> ;
 | 
					
						
							| 
									
										
										
										
											2008-09-12 09:18:44 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #call apply-identities* | 
					
						
							|  |  |  |     dup word>> "identities" word-prop [ | 
					
						
							|  |  |  |         over node-input-infos find-identity [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 { \ drop [ 0 select-input ] } | 
					
						
							|  |  |  |                 { \ nip [ 1 select-input ] } | 
					
						
							|  |  |  |                 [ simplify-to-constant ] | 
					
						
							|  |  |  |             } case
 | 
					
						
							|  |  |  |         ] when*
 | 
					
						
							|  |  |  |     ] when* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: node apply-identities* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : apply-identities ( nodes -- nodes' )
 | 
					
						
							|  |  |  |     [ apply-identities* ] map-nodes ;
 |