| 
									
										
										
										
											2010-01-06 23:39:22 -05:00
										 |  |  | ! Copyright (C) 2006, 2010 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2014-12-13 19:10:21 -05:00
										 |  |  | USING: accessors arrays assocs combinators | 
					
						
							|  |  |  | combinators.short-circuit compiler.tree compiler.tree.builder | 
					
						
							|  |  |  | compiler.tree.checker compiler.tree.cleanup | 
					
						
							|  |  |  | compiler.tree.combinators compiler.tree.dead-code | 
					
						
							|  |  |  | compiler.tree.def-use compiler.tree.escape-analysis | 
					
						
							|  |  |  | compiler.tree.identities compiler.tree.modular-arithmetic | 
					
						
							|  |  |  | compiler.tree.normalization compiler.tree.optimizer | 
					
						
							|  |  |  | compiler.tree.propagation compiler.tree.propagation.info | 
					
						
							|  |  |  | compiler.tree.recursive compiler.tree.tuple-unboxing effects fry | 
					
						
							|  |  |  | generic hints io kernel macros make match math namespaces | 
					
						
							|  |  |  | prettyprint prettyprint.config prettyprint.custom | 
					
						
							|  |  |  | prettyprint.sections quotations sequences sequences.private sets | 
					
						
							|  |  |  | sorting words ;
 | 
					
						
							| 
									
										
										
										
											2009-05-14 17:54:16 -04:00
										 |  |  | FROM: fry => _ ;
 | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  | 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 -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-19 01:16:11 -04:00
										 |  |  | MACRO: match-choose ( alist -- quot )
 | 
					
						
							| 
									
										
										
										
											2010-02-01 11:50:13 -05: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 { | 
					
						
							| 
									
										
										
										
											2010-02-01 11:50:13 -05: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 ] } | 
					
						
							|  |  |  |         { { { ?b ?a ?c } { ?a ?b ?c } } [ swapd ] } | 
					
						
							|  |  |  |         { { { ?a ?b } { ?a ?a ?b } } [ dupd ] } | 
					
						
							|  |  |  |         { { { ?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 ] } | 
					
						
							|  |  |  |         { __ f } | 
					
						
							|  |  |  |     } match-choose ;
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 ;
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 19:46:31 -05:00
										 |  |  | : (shuffle-effect) ( in out #shuffle -- effect )
 | 
					
						
							| 
									
										
										
										
											2010-01-14 10:10:13 -05:00
										 |  |  |     mapping>> '[ _ at ] map [ >array ] bi@ <effect> ;
 | 
					
						
							| 
									
										
										
										
											2008-11-11 19:46:31 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : 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 , ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-13 01:04:26 -05:00
										 |  |  | M: #alien-invoke node>quot params>> , #alien-invoke , ;
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-13 01:04:26 -05:00
										 |  |  | M: #alien-indirect node>quot params>> , #alien-indirect , ;
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-13 01:04:26 -05:00
										 |  |  | M: #alien-assembly node>quot params>> , #alien-assembly , ;
 | 
					
						
							| 
									
										
										
										
											2010-01-06 23:39:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-28 00:49:26 -04:00
										 |  |  | M: #alien-callback node>quot | 
					
						
							| 
									
										
										
										
											2011-11-13 01:04:26 -05:00
										 |  |  |     [ params>> , ] [ child>> nodes>quot , ] bi #alien-callback , ;
 | 
					
						
							| 
									
										
										
										
											2008-08-15 00:35:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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: word optimized. specialized-def optimized. ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-10-29 19:24:28 -04:00
										 |  |  | M: callable optimized. | 
					
						
							|  |  |  |     build-tree optimize-tree nodes>quot | 
					
						
							| 
									
										
										
										
											2012-07-19 20:48:02 -04:00
										 |  |  |     f length-limit [ . ] with-variable ;
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-04-22 00:02:00 -04:00
										 |  |  |         build-tree optimize-tree | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-07-19 20:48:02 -04:00
										 |  |  |         H{ } clone words-called ,, | 
					
						
							|  |  |  |         H{ } clone generics-called ,, | 
					
						
							|  |  |  |         H{ } clone methods-called ,, | 
					
						
							|  |  |  |         H{ } clone intrinsics-called ,, | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         0 swap [ | 
					
						
							| 
									
										
										
										
											2009-08-13 20:21:44 -04:00
										 |  |  |             [ 1 + ] dip
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  |             dup #call? [ | 
					
						
							|  |  |  |                 word>> { | 
					
						
							| 
									
										
										
										
											2009-04-30 01:27:51 -04:00
										 |  |  |                     { [ dup "intrinsic" word-prop ] [ intrinsics-called ] } | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  |                     { [ dup generic? ] [ generics-called ] } | 
					
						
							| 
									
										
										
										
											2010-02-01 02:08:24 -05:00
										 |  |  |                     { [ dup method? ] [ methods-called ] } | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  |                     [ words-called ] | 
					
						
							| 
									
										
										
										
											2012-07-19 20:48:02 -04:00
										 |  |  |                 } cond building get at inc-at
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  |             ] [ drop ] if
 | 
					
						
							|  |  |  |         ] each-node | 
					
						
							| 
									
										
										
										
											2012-07-19 20:48:02 -04:00
										 |  |  |         node-count ,, | 
					
						
							|  |  |  |     ] H{ } make ;
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : 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
 | 
					
						
							| 
									
										
										
										
											2012-07-19 03:02:47 -04:00
										 |  |  |     ] with-variables ;
 | 
					
						
							| 
									
										
										
										
											2008-08-11 00:37:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : 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 | 
					
						
							| 
									
										
										
										
											2009-05-25 17:38:33 -04:00
										 |  |  |     last node-input-infos ;
 | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : final-classes ( quot -- seq )
 | 
					
						
							|  |  |  |     final-info [ class>> ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : final-literals ( quot -- seq )
 | 
					
						
							|  |  |  |     final-info [ literal>> ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : cleaned-up-tree ( quot -- nodes )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-07-16 19:29:40 -04:00
										 |  |  |         build-tree | 
					
						
							|  |  |  |         analyze-recursive | 
					
						
							|  |  |  |         normalize | 
					
						
							|  |  |  |         propagate | 
					
						
							| 
									
										
										
										
											2015-06-06 02:03:42 -04:00
										 |  |  |         cleanup-tree | 
					
						
							| 
									
										
										
										
											2009-08-15 19:42:41 -04:00
										 |  |  |         escape-analysis | 
					
						
							|  |  |  |         unbox-tuples | 
					
						
							| 
									
										
										
										
											2009-08-10 02:16:49 -04:00
										 |  |  |         apply-identities | 
					
						
							| 
									
										
										
										
											2009-07-16 19:29:40 -04:00
										 |  |  |         compute-def-use | 
					
						
							|  |  |  |         remove-dead-code | 
					
						
							|  |  |  |         compute-def-use | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  |         optimize-modular-arithmetic | 
					
						
							| 
									
										
										
										
											2008-09-12 19:08:38 -04:00
										 |  |  |     ] with-scope ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : inlined? ( quot seq/word -- ? )
 | 
					
						
							| 
									
										
										
										
											2010-07-28 00:49:26 -04:00
										 |  |  |     dup word? [ 1array ] when swap
 | 
					
						
							|  |  |  |     [ cleaned-up-tree [ dup #call? [ word>> , ] [ drop ] if ] each-node ] V{ } make | 
					
						
							|  |  |  |     intersect empty? ;
 |