| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | ! Copyright (C) 2006, 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  | USING: kernel assocs match fry accessors namespaces make effects | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | sequences sequences.private quotations generic macros arrays | 
					
						
							| 
									
										
										
										
											2008-12-08 15:58:00 -05:00
										 |  |  | prettyprint prettyprint.backend prettyprint.custom | 
					
						
							|  |  |  | prettyprint.sections math words combinators | 
					
						
							| 
									
										
										
										
											2008-12-17 19:10:01 -05:00
										 |  |  | combinators.short-circuit io sorting hints | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | compiler.tree | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  | compiler.tree.recursive | 
					
						
							|  |  |  | compiler.tree.normalization | 
					
						
							|  |  |  | compiler.tree.cleanup | 
					
						
							|  |  |  | compiler.tree.propagation | 
					
						
							|  |  |  | compiler.tree.propagation.info | 
					
						
							|  |  |  | compiler.tree.def-use | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | compiler.tree.builder | 
					
						
							|  |  |  | compiler.tree.optimizer | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  | compiler.tree.combinators | 
					
						
							|  |  |  | compiler.tree.checker ;
 | 
					
						
							|  |  |  | RENAME: _ match => __ | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | IN: compiler.tree.debugger | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! A simple tool for turning tree IR into quotations and | 
					
						
							|  |  |  | ! printing reports, for debugging purposes. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: node>quot ( node -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MACRO: match-choose ( alist -- )
 | 
					
						
							| 
									
										
										
										
											2008-10-20 02:56:28 -04:00
										 |  |  |     [ '[ _ ] ] assoc-map '[ _ match-cond ] ;
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | MATCH-VARS: ?a ?b ?c ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-12 00:30:18 -04:00
										 |  |  | : pretty-shuffle ( effect -- word/f )
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  |     [ in>> ] [ out>> ] bi 2array { | 
					
						
							| 
									
										
										
										
											2008-08-11 03:49:37 -04:00
										 |  |  |         { { { } { } } [ ] } | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  |         { { { ?a } { ?a } } [ ] } | 
					
						
							|  |  |  |         { { { ?a ?b } { ?a ?b } } [ ] } | 
					
						
							|  |  |  |         { { { ?a ?b ?c } { ?a ?b ?c } } [ ] } | 
					
						
							|  |  |  |         { { { ?a } { } } [ drop ] } | 
					
						
							|  |  |  |         { { { ?a ?b } { } } [ 2drop ] } | 
					
						
							|  |  |  |         { { { ?a ?b ?c } { } } [ 3drop ] } | 
					
						
							|  |  |  |         { { { ?a } { ?a ?a } } [ dup ] } | 
					
						
							|  |  |  |         { { { ?a ?b } { ?a ?b ?a ?b } } [ 2dup ] } | 
					
						
							|  |  |  |         { { { ?a ?b ?c } { ?a ?b ?c ?a ?b ?c } } [ 3dup ] } | 
					
						
							|  |  |  |         { { { ?a ?b } { ?a ?b ?a } } [ over ] } | 
					
						
							|  |  |  |         { { { ?b ?a } { ?a ?b } } [ swap ] } | 
					
						
							| 
									
										
										
										
											2008-08-11 03:49:37 -04:00
										 |  |  |         { { { ?b ?a ?c } { ?a ?b ?c } } [ swapd ] } | 
					
						
							|  |  |  |         { { { ?a ?b } { ?a ?a ?b } } [ dupd ] } | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  |         { { { ?a ?b } { ?b ?a ?b } } [ tuck ] } | 
					
						
							|  |  |  |         { { { ?a ?b ?c } { ?a ?b ?c ?a } } [ pick ] } | 
					
						
							|  |  |  |         { { { ?a ?b ?c } { ?c ?a ?b } } [ -rot ] } | 
					
						
							|  |  |  |         { { { ?a ?b ?c } { ?b ?c ?a } } [ rot ] } | 
					
						
							|  |  |  |         { { { ?a ?b } { ?b } } [ nip ] } | 
					
						
							|  |  |  |         { { { ?a ?b ?c } { ?c } } [ 2nip ] } | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  |         { __ f } | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  |     } match-choose ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-14 00:52:49 -04:00
										 |  |  | TUPLE: shuffle-node { effect effect } ;
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-13 15:17:04 -04:00
										 |  |  | M: shuffle-node pprint* effect>> effect>string text ;
 | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2008-11-11 19:46:31 -05:00
										 |  |  | : (shuffle-effect) ( in out #shuffle -- effect )
 | 
					
						
							|  |  |  |     mapping>> '[ _ at ] map <effect> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : shuffle-effect ( #shuffle -- effect )
 | 
					
						
							|  |  |  |     [ in-d>> ] [ out-d>> ] [ ] tri (shuffle-effect) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : #>r? ( #shuffle -- ? )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ in-d>> length 1 = ] | 
					
						
							|  |  |  |         [ out-r>> length 1 = ] | 
					
						
							|  |  |  |         [ in-r>> empty? ] | 
					
						
							|  |  |  |         [ out-d>> empty? ] | 
					
						
							|  |  |  |     } 1&& ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : #r>? ( #shuffle -- ? )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ in-d>> empty? ] | 
					
						
							|  |  |  |         [ out-r>> empty? ] | 
					
						
							|  |  |  |         [ in-r>> length 1 = ] | 
					
						
							|  |  |  |         [ out-d>> length 1 = ] | 
					
						
							|  |  |  |     } 1&& ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:17:37 -05:00
										 |  |  | SYMBOLS: >R R> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | M: #shuffle node>quot | 
					
						
							| 
									
										
										
										
											2008-11-11 19:46:31 -05:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2008-12-17 20:17:37 -05:00
										 |  |  |         { [ dup #>r? ] [ drop \ >R , ] } | 
					
						
							|  |  |  |         { [ dup #r>? ] [ drop \ R> , ] } | 
					
						
							| 
									
										
										
										
											2008-11-11 19:46:31 -05:00
										 |  |  |         { | 
					
						
							|  |  |  |             [ dup [ in-r>> empty? ] [ out-r>> empty? ] bi and ] | 
					
						
							|  |  |  |             [ | 
					
						
							| 
									
										
										
										
											2008-11-11 20:51:12 -05:00
										 |  |  |                 shuffle-effect dup pretty-shuffle | 
					
						
							| 
									
										
										
										
											2008-11-11 19:46:31 -05:00
										 |  |  |                 [ % ] [ shuffle-node boa , ] ?if
 | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         [ drop "COMPLEX SHUFFLE" , ] | 
					
						
							|  |  |  |     } cond ;
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-29 01:37:26 -05:00
										 |  |  | M: #push node>quot literal>> literalize , ;
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: #call node>quot word>> , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #call-recursive node>quot label>> id>> , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DEFER: nodes>quot | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DEFER: label | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #recursive node>quot | 
					
						
							|  |  |  |     [ label>> id>> literalize , ] | 
					
						
							|  |  |  |     [ child>> nodes>quot , \ label , ] | 
					
						
							|  |  |  |     bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #if node>quot | 
					
						
							|  |  |  |     children>> [ nodes>quot ] map % \ if , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #dispatch node>quot | 
					
						
							|  |  |  |     children>> [ nodes>quot ] map , \ dispatch , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  | M: #alien-invoke node>quot params>> , \ #alien-invoke , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #alien-indirect node>quot params>> , \ #alien-indirect , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: #alien-callback node>quot params>> , \ #alien-callback , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | M: node node>quot drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : nodes>quot ( node -- quot )
 | 
					
						
							|  |  |  |     [ [ node>quot ] each ] [ ] make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-03 11:44:21 -05:00
										 |  |  | GENERIC: optimized. ( quot/word -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: method-spec optimized. first2 method optimized. ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: word optimized. specialized-def optimized. ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: callable optimized. build-tree optimize-tree nodes>quot . ;
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: words-called | 
					
						
							|  |  |  | SYMBOL: generics-called | 
					
						
							|  |  |  | SYMBOL: methods-called | 
					
						
							|  |  |  | SYMBOL: intrinsics-called | 
					
						
							|  |  |  | SYMBOL: node-count | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : make-report ( word/quot -- assoc )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         dup word? [ build-tree-from-word nip ] [ build-tree ] if
 | 
					
						
							|  |  |  |         optimize-tree | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         H{ } clone words-called set
 | 
					
						
							|  |  |  |         H{ } clone generics-called set
 | 
					
						
							|  |  |  |         H{ } clone methods-called set
 | 
					
						
							|  |  |  |         H{ } clone intrinsics-called set
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         0 swap [ | 
					
						
							| 
									
										
										
										
											2008-12-09 17:54:48 -05:00
										 |  |  |             [ 1+ ] dip
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  |             dup #call? [ | 
					
						
							|  |  |  |                 word>> { | 
					
						
							|  |  |  |                     { [ dup "intrinsics" word-prop over "if-intrinsics" word-prop or ] [ intrinsics-called ] } | 
					
						
							|  |  |  |                     { [ dup generic? ] [ generics-called ] } | 
					
						
							|  |  |  |                     { [ dup method-body? ] [ methods-called ] } | 
					
						
							|  |  |  |                     [ words-called ] | 
					
						
							| 
									
										
										
										
											2008-12-09 17:54:48 -05:00
										 |  |  |                 } cond inc-at
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  |             ] [ drop ] if
 | 
					
						
							|  |  |  |         ] each-node | 
					
						
							|  |  |  |         node-count set
 | 
					
						
							|  |  |  |     ] H{ } make-assoc ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : report. ( report -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         "==== Total number of IR nodes:" print
 | 
					
						
							|  |  |  |         node-count get .
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             { generics-called "==== Generic word calls:" } | 
					
						
							|  |  |  |             { words-called "==== Ordinary word calls:" } | 
					
						
							|  |  |  |             { methods-called "==== Non-inlined method calls:" } | 
					
						
							|  |  |  |             { intrinsics-called "==== Open-coded intrinsic calls:" } | 
					
						
							|  |  |  |         } [ | 
					
						
							|  |  |  |             nl print get keys natural-sort stack. | 
					
						
							|  |  |  |         ] assoc-each
 | 
					
						
							|  |  |  |     ] bind ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : optimizer-report. ( word -- )
 | 
					
						
							|  |  |  |     make-report report. ;
 | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! More utilities | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : final-info ( quot -- seq )
 | 
					
						
							|  |  |  |     build-tree | 
					
						
							|  |  |  |     analyze-recursive | 
					
						
							|  |  |  |     normalize | 
					
						
							|  |  |  |     propagate | 
					
						
							|  |  |  |     compute-def-use | 
					
						
							|  |  |  |     dup check-nodes | 
					
						
							|  |  |  |     peek node-input-infos ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : final-classes ( quot -- seq )
 | 
					
						
							|  |  |  |     final-info [ class>> ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : final-literals ( quot -- seq )
 | 
					
						
							|  |  |  |     final-info [ literal>> ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : cleaned-up-tree ( quot -- nodes )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         check-optimizer? on
 | 
					
						
							|  |  |  |         build-tree optimize-tree  | 
					
						
							|  |  |  |     ] with-scope ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : inlined? ( quot seq/word -- ? )
 | 
					
						
							|  |  |  |     [ cleaned-up-tree ] dip
 | 
					
						
							|  |  |  |     dup word? [ 1array ] when
 | 
					
						
							|  |  |  |     '[ dup #call? [ word>> _ member? ] [ drop f ] if ] | 
					
						
							|  |  |  |     contains-node? not ;
 |