File: printFlowgraph.sml

package info (click to toggle)
mlton 20210117%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 58,464 kB
  • sloc: ansic: 27,682; sh: 4,455; asm: 3,569; lisp: 2,879; makefile: 2,347; perl: 1,169; python: 191; pascal: 68; javascript: 7
file content (141 lines) | stat: -rw-r--r-- 6,854 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(* printFlowgraph.sml -- print flowgraph of target machine instructions. 
 *
 * Copyright (c) 1997 Bell Laboratories.
 *)
signature PRINT_FLOWGRAPH = 
sig
   structure Asm : INSTRUCTION_EMITTER
   structure CFG : CONTROL_FLOW_GRAPH (* where I = Asm.I and P = Asm.S.P *)
                   where type I.addressing_mode = Asm.I.addressing_mode
                     and type I.ea = Asm.I.ea
                     and type I.instr = Asm.I.instr
                     and type I.instruction = Asm.I.instruction
                     and type I.operand = Asm.I.operand
                   where type P.Client.pseudo_op = Asm.S.P.Client.pseudo_op
                     and type P.T.Basis.cond = Asm.S.P.T.Basis.cond
                     and type P.T.Basis.div_rounding_mode = Asm.S.P.T.Basis.div_rounding_mode
                     and type P.T.Basis.ext = Asm.S.P.T.Basis.ext
                     and type P.T.Basis.fcond = Asm.S.P.T.Basis.fcond
                     and type P.T.Basis.rounding_mode = Asm.S.P.T.Basis.rounding_mode
                     and type P.T.Constant.const = Asm.S.P.T.Constant.const
                     and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) Asm.S.P.T.Extension.ccx
                     and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) Asm.S.P.T.Extension.fx
                     and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) Asm.S.P.T.Extension.rx
                     and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) Asm.S.P.T.Extension.sx
                     and type P.T.I.div_rounding_mode = Asm.S.P.T.I.div_rounding_mode
                     and type P.T.Region.region = Asm.S.P.T.Region.region
                     and type P.T.ccexp = Asm.S.P.T.ccexp
                     and type P.T.fexp = Asm.S.P.T.fexp
                     (* and type P.T.labexp = Asm.S.P.T.labexp *)
                     and type P.T.mlrisc = Asm.S.P.T.mlrisc
                     and type P.T.oper = Asm.S.P.T.oper
                     and type P.T.rep = Asm.S.P.T.rep
                     and type P.T.rexp = Asm.S.P.T.rexp
                     and type P.T.stm = Asm.S.P.T.stm

   val printCFG : TextIO.outstream -> string -> CFG.cfg -> unit
end


functor PrintFlowgraph 
   (structure Asm : INSTRUCTION_EMITTER
    structure CFG : CONTROL_FLOW_GRAPH (* where I = Asm.I and P = Asm.S.P *)
                    where type I.addressing_mode = Asm.I.addressing_mode
                      and type I.ea = Asm.I.ea
                      and type I.instr = Asm.I.instr
                      and type I.instruction = Asm.I.instruction
                      and type I.operand = Asm.I.operand
                    where type P.Client.pseudo_op = Asm.S.P.Client.pseudo_op
                      and type P.T.Basis.cond = Asm.S.P.T.Basis.cond
                      and type P.T.Basis.div_rounding_mode = Asm.S.P.T.Basis.div_rounding_mode
                      and type P.T.Basis.ext = Asm.S.P.T.Basis.ext
                      and type P.T.Basis.fcond = Asm.S.P.T.Basis.fcond
                      and type P.T.Basis.rounding_mode = Asm.S.P.T.Basis.rounding_mode
                      and type P.T.Constant.const = Asm.S.P.T.Constant.const
                      and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) Asm.S.P.T.Extension.ccx
                      and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) Asm.S.P.T.Extension.fx
                      and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) Asm.S.P.T.Extension.rx
                      and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) Asm.S.P.T.Extension.sx
                      and type P.T.I.div_rounding_mode = Asm.S.P.T.I.div_rounding_mode
                      and type P.T.Region.region = Asm.S.P.T.Region.region
                      and type P.T.ccexp = Asm.S.P.T.ccexp
                      and type P.T.fexp = Asm.S.P.T.fexp
                      (* and type P.T.labexp = Asm.S.P.T.labexp *)
                      and type P.T.mlrisc = Asm.S.P.T.mlrisc
                      and type P.T.oper = Asm.S.P.T.oper
                      and type P.T.rep = Asm.S.P.T.rep
                      and type P.T.rexp = Asm.S.P.T.rexp
                      and type P.T.stm = Asm.S.P.T.stm
   ) : PRINT_FLOWGRAPH =
struct
   structure Asm = Asm
   structure CFG = CFG
   structure C   = CFG.I.C
   structure Fmt = Format

   val i2s = Int.toString

   fun printList stream list = let
     fun pr str = TextIO.output(stream, str)
     fun iter [] = ()
       | iter [i] = pr i
       | iter (h::t) = (pr (h ^ ", "); iter t)
   in iter list
   end

   fun printCFG stream title (Cfg as Graph.GRAPH cfg) = 
   let fun pr str = TextIO.output(stream, str)
       val prList = printList stream
       val annotations = !(CFG.annotations Cfg)
       val Asm.S.STREAM{emit,pseudoOp,defineLabel,annotation,...} = 
             AsmStream.withStream stream Asm.makeStream annotations

       fun showFreq(ref w) = Fmt.format "[%f]" [Fmt.REAL w] 
       fun showEdge(blknum,e) = 
	   Fmt.format "%d:%s" [Fmt.INT blknum, Fmt.STR(CFG.show_edge e)]
       fun showSucc(_, x, e) = showEdge(x,e)
       fun showPred(x, _, e) = showEdge(x,e) 
       fun showSuccs b =
            (pr "\tsucc:     "; 
             prList (map showSucc (#out_edges cfg b)); 
             pr "\n")
       fun showPreds b =
            (pr "\tpred:     "; 
             prList (map showPred (#in_edges cfg b)); 
             pr "\n")

       fun printBlock(_, CFG.BLOCK{kind=CFG.START, id, freq, ...}) = 
           (pr (Fmt.format "ENTRY %d %s\n" [Fmt.INT id, Fmt.STR(showFreq freq)]);
            showSuccs id)
         | printBlock(_, CFG.BLOCK{kind=CFG.STOP, id, freq, ...}) = 
	   (pr (Fmt.format "EXIT %d %s\n" [Fmt.INT id, Fmt.STR(showFreq freq)]);
            showPreds id)
         | printBlock(_, CFG.BLOCK{id, align, freq, insns, annotations, 
                               labels, ...}) = 
	   (pr (Fmt.format "BLOCK %d %s\n" [Fmt.INT id, Fmt.STR(showFreq freq)]);
	    case !align of NONE => () | SOME p => (pr (CFG.P.toString p ^ "\n"));
            app annotation (!annotations);
            app defineLabel (!labels);
            (*
               pr ("\tlive in:  " ^ CellsBasis.CellSet.toString (!liveIn) ^ "\n");
               pr ("\tlive out: " ^ CellsBasis.CellSet.toString (!liveOut) ^ "\n");
             *)
            showSuccs id;
            showPreds id;
            app emit (rev (!insns)))

       fun printData() = let
         val CFG.INFO{data, ...} = #graph_info cfg
       in List.app (pr o CFG.P.toString) (rev(!data))
       end
   in
       pr(Fmt.format "[ %s ]\n" [Fmt.STR title]);
       app annotation annotations;
       (* printBlock entry; *)
       AsmStream.withStream stream (#forall_nodes cfg) printBlock;
       (* printBlock exit; *)
       AsmStream.withStream stream printData ();
       TextIO.flushOut stream
   end
end