| 
									
										
										
										
											2010-04-21 03:08:52 -04:00
										 |  |  | ! Copyright (C) 2008, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-10-19 02:10:21 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2014-12-13 19:10:21 -05:00
										 |  |  | USING: accessors arrays classes.algebra combinators | 
					
						
							|  |  |  | compiler.cfg.instructions compiler.cfg.instructions.syntax | 
					
						
							|  |  |  | compiler.cfg.value-numbering.graph generic.parser kernel make | 
					
						
							| 
									
										
										
										
											2015-08-12 09:49:02 -04:00
										 |  |  | math namespaces quotations sequences sequences.private sets | 
					
						
							|  |  |  | slots words ;
 | 
					
						
							| 
									
										
										
										
											2008-10-19 02:10:21 -04:00
										 |  |  | IN: compiler.cfg.value-numbering.expressions | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-24 06:06:16 -04:00
										 |  |  | << | 
					
						
							| 
									
										
										
										
											2008-10-22 22:59:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-24 06:06:16 -04:00
										 |  |  | GENERIC: >expr ( insn -- expr )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : input-values ( slot-specs -- slot-specs' )
 | 
					
						
							|  |  |  |     [ type>> { use literal } member-eq? ] filter ;
 | 
					
						
							| 
									
										
										
										
											2009-07-14 20:17:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-24 06:06:16 -04:00
										 |  |  | : slot->expr-quot ( slot-spec -- quot )
 | 
					
						
							|  |  |  |     [ name>> reader-word 1quotation ] | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         type>> { | 
					
						
							|  |  |  |             { use [ [ vreg>vn ] ] } | 
					
						
							|  |  |  |             { literal [ [ ] ] } | 
					
						
							|  |  |  |         } case
 | 
					
						
							|  |  |  |     ] bi append ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : narray-quot ( length -- quot )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ , [ f <array> ] % ] | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  |         [ | 
					
						
							| 
									
										
										
										
											2010-04-24 06:06:16 -04:00
										 |  |  |             dup iota [ | 
					
						
							|  |  |  |                 - 1 - , [ swap [ set-array-nth ] keep ] % | 
					
						
							|  |  |  |             ] with each
 | 
					
						
							|  |  |  |         ] bi
 | 
					
						
							|  |  |  |     ] [ ] make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >expr-quot ( insn slot-specs -- quot )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ literalize , \ swap , ] | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             [ [ slot->expr-quot ] map cleave>quot % ] | 
					
						
							|  |  |  |             [ length 1 + narray-quot % ] | 
					
						
							|  |  |  |             bi
 | 
					
						
							|  |  |  |         ] bi*
 | 
					
						
							|  |  |  |     ] [ ] make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : define->expr-method ( insn slot-specs -- )
 | 
					
						
							|  |  |  |     [ drop \ >expr create-method-in ] [ >expr-quot ] 2bi define ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | insn-classes get
 | 
					
						
							| 
									
										
										
										
											2010-07-13 07:40:14 -04:00
										 |  |  | [ foldable-insn class<= ] filter
 | 
					
						
							|  |  |  | { ##copy ##load-integer ##load-reference } diff | 
					
						
							| 
									
										
										
										
											2010-04-24 06:06:16 -04:00
										 |  |  | [ | 
					
						
							|  |  |  |     dup "insn-slots" word-prop input-values | 
					
						
							|  |  |  |     define->expr-method | 
					
						
							|  |  |  | ] each
 | 
					
						
							| 
									
										
										
										
											2010-04-24 02:38:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-24 06:06:16 -04:00
										 |  |  | >> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: integer-expr value ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <integer-expr> integer-expr | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: reference-expr value ;
 | 
					
						
							| 
									
										
										
										
											2009-09-02 07:22:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-21 03:08:52 -04:00
										 |  |  | C: <reference-expr> reference-expr | 
					
						
							| 
									
										
										
										
											2009-07-14 20:17:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: reference-expr equal? | 
					
						
							| 
									
										
										
										
											2010-04-21 03:08:52 -04:00
										 |  |  |     over reference-expr? [ | 
					
						
							|  |  |  |         [ value>> ] bi@
 | 
					
						
							|  |  |  |         2dup [ float? ] both?
 | 
					
						
							|  |  |  |         [ fp-bitwise= ] [ eq? ] if
 | 
					
						
							|  |  |  |     ] [ 2drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-10-22 22:59:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:06:36 -05:00
										 |  |  | M: reference-expr hashcode* | 
					
						
							| 
									
										
										
										
											2010-04-21 03:08:52 -04:00
										 |  |  |     nip value>> dup float? [ double>bits ] [ identity-hashcode ] if ;
 | 
					
						
							| 
									
										
										
										
											2009-11-10 22:06:36 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-24 06:15:41 -04:00
										 |  |  | M: insn >expr drop input-expr-counter counter neg ;
 | 
					
						
							| 
									
										
										
										
											2009-09-02 07:22:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-24 02:38:43 -04:00
										 |  |  | M: ##copy >expr "Fail" throw ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-04-21 03:08:52 -04:00
										 |  |  | M: ##load-integer >expr val>> <integer-expr> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: ##load-reference >expr obj>> <reference-expr> ;
 |