| 
									
										
										
										
											2009-07-17 23:57:28 -04:00
										 |  |  | ! Copyright (C) 2008, 2009 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-08-31 06:40:29 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-12-06 10:16:29 -05:00
										 |  |  | USING: kernel accessors sequences words memoize combinators | 
					
						
							| 
									
										
										
										
											2009-08-18 00:59:24 -04:00
										 |  |  | classes classes.builtin classes.tuple classes.singleton | 
					
						
							|  |  |  | math.partial-dispatch fry assocs combinators.short-circuit | 
					
						
							| 
									
										
										
										
											2008-09-02 03:02:05 -04:00
										 |  |  | compiler.tree | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  | compiler.tree.combinators | 
					
						
							| 
									
										
										
										
											2008-09-02 23:59:49 -04:00
										 |  |  | compiler.tree.propagation.info | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  | compiler.tree.late-optimizations ;
 | 
					
						
							| 
									
										
										
										
											2008-08-31 06:40:29 -04:00
										 |  |  | IN: compiler.tree.finalization | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  | ! This is a late-stage optimization. | 
					
						
							|  |  |  | ! See the comment in compiler.tree.late-optimizations. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-02 23:59:49 -04:00
										 |  |  | ! This pass runs after propagation, so that it can expand | 
					
						
							| 
									
										
										
										
											2008-12-06 10:16:29 -05:00
										 |  |  | ! type predicates; these cannot be expanded before | 
					
						
							| 
									
										
										
										
											2008-10-23 06:55:50 -04:00
										 |  |  | ! propagation since we need to see 'fixnum?' instead of | 
					
						
							|  |  |  | ! 'tag 0 eq?' and so on, for semantic reasoning. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-18 04:33:24 -04:00
										 |  |  | ! We also delete empty stack shuffles and copies to facilitate | 
					
						
							|  |  |  | ! tail call optimization in the code generator. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-31 06:40:29 -04:00
										 |  |  | GENERIC: finalize* ( node -- nodes )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 04:51:44 -04:00
										 |  |  | : finalize ( nodes -- nodes' ) [ finalize* ] map-nodes ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : splice-final ( quot -- nodes ) splice-quot finalize ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-18 04:33:24 -04:00
										 |  |  | M: #copy finalize* drop f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #shuffle finalize* | 
					
						
							|  |  |  |     dup { | 
					
						
							|  |  |  |         [ [ in-d>> length ] [ out-d>> length ] bi = ] | 
					
						
							|  |  |  |         [ [ in-r>> length ] [ out-r>> length ] bi = ] | 
					
						
							|  |  |  |         [ [ in-d>> ] [ out-d>> ] [ mapping>> ] tri '[ _ at = ] 2all? ] | 
					
						
							|  |  |  |         [ [ in-r>> ] [ out-r>> ] [ mapping>> ] tri '[ _ at = ] 2all? ] | 
					
						
							|  |  |  |     } 1&& [ drop f ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-06 10:16:29 -05:00
										 |  |  | MEMO: cached-expansion ( word -- nodes )
 | 
					
						
							| 
									
										
										
										
											2008-10-08 04:51:44 -04:00
										 |  |  |     def>> splice-final ;
 | 
					
						
							| 
									
										
										
										
											2008-09-02 23:59:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-06 10:16:29 -05:00
										 |  |  | GENERIC: finalize-word ( #call word -- nodes )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: predicate finalize-word | 
					
						
							|  |  |  |     "predicating" word-prop { | 
					
						
							|  |  |  |         { [ dup builtin-class? ] [ drop word>> cached-expansion ] } | 
					
						
							|  |  |  |         { [ dup tuple-class? ] [ drop word>> def>> splice-final ] } | 
					
						
							| 
									
										
										
										
											2009-08-18 00:59:24 -04:00
										 |  |  |         { [ dup singleton-class? ] [ drop word>> def>> splice-final ] } | 
					
						
							| 
									
										
										
										
											2008-12-06 10:16:29 -05:00
										 |  |  |         [ drop ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-16 19:29:40 -04:00
										 |  |  | M: math-partial finalize-word | 
					
						
							|  |  |  |     dup primitive? [ drop ] [ nip cached-expansion ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-06 10:16:29 -05:00
										 |  |  | M: word finalize-word drop ;
 | 
					
						
							| 
									
										
										
										
											2008-09-02 23:59:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-02 03:02:05 -04:00
										 |  |  | M: #call finalize* | 
					
						
							| 
									
										
										
										
											2008-12-06 10:16:29 -05:00
										 |  |  |     dup word>> finalize-word ;
 | 
					
						
							| 
									
										
										
										
											2008-09-02 03:02:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-31 06:40:29 -04:00
										 |  |  | M: node finalize* ;
 |