| 
									
										
										
										
											2011-05-22 20:15:56 -04:00
										 |  |  | ! Copyright (C) 2011 Alex Vondrak. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  | USING: accessors fry io io.directories io.pathnames | 
					
						
							|  |  |  | io.streams.string kernel math math.parser namespaces | 
					
						
							|  |  |  | prettyprint sequences splitting strings tools.annotations | 
					
						
							| 
									
										
										
										
											2011-05-22 20:15:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | compiler.cfg | 
					
						
							|  |  |  | compiler.cfg.builder | 
					
						
							|  |  |  | compiler.cfg.debugger | 
					
						
							|  |  |  | compiler.cfg.linearization | 
					
						
							|  |  |  | compiler.cfg.finalization | 
					
						
							|  |  |  | compiler.cfg.optimizer | 
					
						
							|  |  |  | compiler.cfg.rpo | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | compiler.cfg.value-numbering | 
					
						
							|  |  |  | compiler.cfg.value-numbering.graph | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | graphviz | 
					
						
							|  |  |  | graphviz.notation | 
					
						
							|  |  |  | graphviz.render | 
					
						
							|  |  |  | ;
 | 
					
						
							|  |  |  | FROM: compiler.cfg.linearization => number-blocks ;
 | 
					
						
							|  |  |  | IN: compiler.cfg.graphviz | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : left-justify ( str -- str' )
 | 
					
						
							|  |  |  |     string-lines "\\l" join ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-27 18:08:55 -04:00
										 |  |  | : left-justified ( quot -- str )
 | 
					
						
							|  |  |  |     with-string-writer left-justify ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-22 20:15:56 -04:00
										 |  |  | : bb-label ( bb -- str )
 | 
					
						
							| 
									
										
										
										
											2011-06-27 18:08:55 -04:00
										 |  |  |     [ number>> number>string ] | 
					
						
							| 
									
										
										
										
											2011-05-22 20:15:56 -04:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2011-06-27 18:08:55 -04:00
										 |  |  |         [ instructions>> [ insn. ] each ] left-justified | 
					
						
							|  |  |  |     ] bi "\\n" glue ;
 | 
					
						
							| 
									
										
										
										
											2011-05-22 20:15:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : add-cfg-vertex ( graph bb -- graph' )
 | 
					
						
							|  |  |  |     [ number>> <node> ] | 
					
						
							|  |  |  |     [ bb-label =label ] | 
					
						
							|  |  |  |     [ kill-block?>> [ "grey" =color "filled" =style ] when ] | 
					
						
							|  |  |  |     tri add ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : add-cfg-edges ( graph bb -- graph' )
 | 
					
						
							|  |  |  |     dup successors>> [ | 
					
						
							|  |  |  |         [ number>> ] bi@ -> | 
					
						
							|  |  |  |     ] with each ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-04 15:38:42 -04:00
										 |  |  | : cfgviz ( cfg -- graph )
 | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  |     <digraph> | 
					
						
							| 
									
										
										
										
											2015-08-06 18:18:38 -04:00
										 |  |  |         [graph "t" =labelloc ]; | 
					
						
							|  |  |  |         [node "box" =shape "Courier" =fontname 10 =fontsize ]; | 
					
						
							| 
									
										
										
										
											2011-06-04 15:38:42 -04:00
										 |  |  |         swap [ | 
					
						
							|  |  |  |             [ add-cfg-vertex ] [ add-cfg-edges ] bi
 | 
					
						
							|  |  |  |         ] each-basic-block ;
 | 
					
						
							| 
									
										
										
										
											2011-05-22 20:15:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-11 15:48:43 -05:00
										 |  |  | : perform-pass ( cfg pass pass# -- )
 | 
					
						
							|  |  |  |     drop def>> call( cfg -- ) ;
 | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-04 15:38:42 -04:00
										 |  |  | : draw-cfg ( cfg pass pass# -- cfg )
 | 
					
						
							|  |  |  |     [ dup cfgviz ] | 
					
						
							| 
									
										
										
										
											2011-09-08 13:01:42 -04:00
										 |  |  |     [ name>> "-" prepend ] | 
					
						
							|  |  |  |     [ number>string prepend svg ] | 
					
						
							| 
									
										
										
										
											2011-06-04 15:38:42 -04:00
										 |  |  |     tri* ;
 | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-09 15:08:25 -04:00
										 |  |  | SYMBOL: passes | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  | : watch-pass ( cfg pass pass# -- cfg' )
 | 
					
						
							| 
									
										
										
										
											2014-12-11 15:48:43 -05:00
										 |  |  |     [ perform-pass ] 3keep draw-cfg ;
 | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : begin-watching-passes ( cfg -- cfg )
 | 
					
						
							| 
									
										
										
										
											2011-06-04 15:38:42 -04:00
										 |  |  |     \ build-cfg 0 draw-cfg ;
 | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : watch-passes ( cfg -- cfg' )
 | 
					
						
							| 
									
										
										
										
											2011-06-09 15:08:25 -04:00
										 |  |  |     passes get [ 1 + watch-pass ] each-index ;
 | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : finish-watching-passes ( cfg -- )
 | 
					
						
							|  |  |  |     \ finalize-cfg | 
					
						
							| 
									
										
										
										
											2011-06-09 15:08:25 -04:00
										 |  |  |     passes get length 1 +
 | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  |     watch-pass drop ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : watch-cfg ( path cfg -- )
 | 
					
						
							|  |  |  |     over make-directories | 
					
						
							| 
									
										
										
										
											2011-05-22 20:15:56 -04:00
										 |  |  |     [ | 
					
						
							|  |  |  |         [ | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  |             begin-watching-passes | 
					
						
							|  |  |  |             watch-passes | 
					
						
							|  |  |  |             finish-watching-passes | 
					
						
							| 
									
										
										
										
											2011-05-22 20:15:56 -04:00
										 |  |  |         ] with-cfg | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  |     ] curry with-directory ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : watch-cfgs ( path cfgs -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         number>string "cfg" prepend append-path | 
					
						
							|  |  |  |         swap watch-cfg | 
					
						
							|  |  |  |     ] with each-index ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : watch-optimizer* ( path quot -- )
 | 
					
						
							|  |  |  |     test-builder | 
					
						
							|  |  |  |     dup length 1 = [ first watch-cfg ] [ watch-cfgs ] if ;
 | 
					
						
							| 
									
										
										
										
											2011-05-22 20:15:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-03 21:11:08 -04:00
										 |  |  | : watch-optimizer ( quot -- )
 | 
					
						
							|  |  |  |     [ "" ] dip watch-optimizer* ;
 | 
					
						
							| 
									
										
										
										
											2013-11-27 16:36:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : ssa. ( quot -- ) test-ssa [ cfgviz preview ] each ;
 | 
					
						
							|  |  |  | : flat. ( quot -- ) test-flat [ cfgviz preview ] each ;
 | 
					
						
							|  |  |  | : regs. ( quot -- ) test-regs [ cfgviz preview ] each ;
 |