1 ! Copyright (C) 2011 Alex Vondrak.
2 ! See https://factorcode.org/license.txt for BSD license
4 USING: accessors fry io io.directories io.pathnames
5 io.streams.string kernel math math.parser namespaces
6 prettyprint sequences splitting strings tools.annotations
11 compiler.cfg.linearization
12 compiler.cfg.finalization
13 compiler.cfg.optimizer
16 compiler.cfg.value-numbering
17 compiler.cfg.value-numbering.graph
23 FROM: compiler.cfg.linearization => number-blocks ;
24 IN: compiler.cfg.graphviz
26 : left-justify ( str -- str' )
27 split-lines "\\l" join ;
29 : left-justified ( quot -- str )
30 with-string-writer left-justify ; inline
32 : bb-label ( bb -- str )
33 [ number>> number>string ]
35 [ instructions>> [ insn. ] each ] left-justified
38 : add-cfg-vertex ( graph bb -- graph' )
41 [ kill-block?>> [ "grey" =color "filled" =style ] when ]
44 : add-cfg-edges ( graph bb -- graph' )
49 : cfgviz ( cfg -- graph )
51 [graph "t" =labelloc ];
52 [node "box" =shape "Courier" =fontname 10 =fontsize ];
54 [ add-cfg-vertex ] [ add-cfg-edges ] bi
57 : perform-pass ( cfg pass pass# -- )
58 drop def>> call( cfg -- ) ;
60 : draw-cfg ( cfg pass pass# -- cfg )
62 [ name>> "-" prepend ]
63 [ number>string prepend svg ]
68 : watch-pass ( cfg pass pass# -- cfg' )
69 [ perform-pass ] 3keep draw-cfg ;
71 : begin-watching-passes ( cfg -- cfg )
72 \ build-cfg 0 draw-cfg ;
74 : watch-passes ( cfg -- cfg' )
75 passes get [ 1 + watch-pass ] each-index ;
77 : finish-watching-passes ( cfg -- )
82 : watch-cfg ( path cfg -- )
88 finish-watching-passes
90 ] curry with-directory ;
92 : watch-cfgs ( path cfgs -- )
94 number>string "cfg" prepend append-path
98 : watch-optimizer* ( path quot -- )
100 dup length 1 = [ first watch-cfg ] [ watch-cfgs ] if ;
102 : watch-optimizer ( quot -- )
103 [ "" ] dip watch-optimizer* ;
105 : ssa. ( quot -- ) test-ssa [ cfgviz preview ] each ;
106 : flat. ( quot -- ) test-flat [ cfgviz preview ] each ;
107 : regs. ( quot -- ) test-regs [ cfgviz preview ] each ;