Debian Codegraph @

lib/mlrisc-lib/MLRISC/backpatch/backpatch.sml

raw
1 (* bbsched2.sml
2 *
3 * COPYRIGHT (c) 1996 Bell Laboratories.
4 *
5 *)
6
7 (** bbsched2.sml - invoke scheduling after span dependent resolution **)
8
9 functor BBSched2
10 (structure Emitter : INSTRUCTION_EMITTER
11 structure CFG : CONTROL_FLOW_GRAPH (* where I = Emitter.I and P = Emitter.S.P *)
12 where type I.addressing_mode = Emitter.I.addressing_mode
13 and type I.ea = Emitter.I.ea
14 and type I.instr = Emitter.I.instr
15 and type I.instruction = Emitter.I.instruction
16 and type I.operand = Emitter.I.operand
17 where type P.Client.pseudo_op = Emitter.S.P.Client.pseudo_op
18 and type P.T.Basis.cond = Emitter.S.P.T.Basis.cond
19 and type P.T.Basis.div_rounding_mode = Emitter.S.P.T.Basis.div_rounding_mode
20 and type P.T.Basis.ext = Emitter.S.P.T.Basis.ext
21 and type P.T.Basis.fcond = Emitter.S.P.T.Basis.fcond
22 and type P.T.Basis.rounding_mode = Emitter.S.P.T.Basis.rounding_mode
23 and type P.T.Constant.const = Emitter.S.P.T.Constant.const
24 and type ('s,'r,'f,'c) P.T.Extension.ccx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.ccx
25 and type ('s,'r,'f,'c) P.T.Extension.fx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.fx
26 and type ('s,'r,'f,'c) P.T.Extension.rx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.rx
27 and type ('s,'r,'f,'c) P.T.Extension.sx = ('s,'r,'f,'c) Emitter.S.P.T.Extension.sx
28 and type P.T.I.div_rounding_mode = Emitter.S.P.T.I.div_rounding_mode
29 and type P.T.Region.region = Emitter.S.P.T.Region.region
30 and type P.T.ccexp = Emitter.S.P.T.ccexp
31 and type P.T.fexp = Emitter.S.P.T.fexp
32 (* and type P.T.labexp = Emitter.S.P.T.labexp *)
33 and type P.T.mlrisc = Emitter.S.P.T.mlrisc
34 and type P.T.oper = Emitter.S.P.T.oper
35 and type P.T.rep = Emitter.S.P.T.rep
36 and type P.T.rexp = Emitter.S.P.T.rexp
37 and type P.T.stm = Emitter.S.P.T.stm
38 structure Jumps : SDI_JUMPS (* where I = CFG.I *)
39 where type I.addressing_mode = CFG.I.addressing_mode
40 and type I.ea = CFG.I.ea
41 and type I.instr = CFG.I.instr
42 and type I.instruction = CFG.I.instruction
43 and type I.operand = CFG.I.operand
44 structure Props : INSN_PROPERTIES (* where I = CFG.I *)
45 where type I.addressing_mode = CFG.I.addressing_mode
46 and type I.ea = CFG.I.ea
47 and type I.instr = CFG.I.instr
48 and type I.instruction = CFG.I.instruction
49 and type I.operand = CFG.I.operand
50 ) =
51 struct
52
53 structure CFG = CFG
54 structure G = Graph
55 structure I = CFG.I
56 structure C = I.C
57 structure E = Emitter
58 structure J = Jumps
59 structure P = CFG.P
60
61 fun error msg = MLRiscErrorMsg.error("BBSched",msg)
62
63 datatype code =
64 SDI of {size : int ref, (* variable sized *)
65 insn : I.instruction}
66 | FIXED of {size: int, (* size of fixed instructions *)
67 insns: I.instruction list}
68
69 datatype compressed =
70 PSEUDO of P.pseudo_op
71 | LABEL of Label.label
72 | CODE of code list
73
74 datatype cluster = CLUSTER of compressed list
75
76 val clusterList : cluster list ref = ref []
77 val dataList : P.pseudo_op list ref = ref []
78 fun cleanUp() = (clusterList := []; dataList := [])
79
80 fun bbsched(G.GRAPH{graph_info=CFG.INFO{data, ...}, ...}, blocks) = let
81 fun compress [] = []
82 | compress((_, CFG.BLOCK{align, labels, insns, ...}) :: rest) = let
83 fun alignIt(chunks) =
84 (case !align of NONE => chunks | SOME p => PSEUDO(p)::chunks)
85
86 fun mkCode(0, [], [], code) = code
87 | mkCode(size, insns, [], code) = FIXED{size=size, insns=insns}:: code
88 | mkCode(size, insns, instr::instrs, code) = let
89 val s = J.minSize instr
90 in
91 if J.isSdi instr then let
92 val sdi = SDI{size=ref s, insn=instr}
93 in
94 if size = 0 then
95 mkCode(0, [], instrs, sdi::code)
96 else
97 mkCode(0, [], instrs,
98 sdi::FIXED{size=size, insns=insns}::code)
99 end
100 else mkCode(size+s, instr::insns, instrs, code)
101 end
102 in
103 alignIt
104 (map LABEL (!labels) @
105 CODE(mkCode(0, [], !insns, [])) :: compress rest)
106 end
107 in
108 clusterList:=CLUSTER(compress blocks):: (!clusterList);
109 dataList := !data @ !dataList
110 end
111
112
113
114 fun finish() = let
115 fun labels(PSEUDO pOp::rest, pos, chgd) =
116 (P.adjustLabels(pOp, pos); labels(rest, pos+P.sizeOf(pOp,pos), chgd))
117 | labels(LABEL lab::rest, pos, chgd) =
118 if Label.addrOf(lab) = pos then labels(rest, pos, chgd)
119 else (Label.setAddr(lab, pos); labels(rest, pos, true))
120 | labels(CODE code::rest, pos, chgd) = let
121 fun doCode(FIXED{size, ...}::rest, pos, changed) =
122 doCode(rest, pos+size, changed)
123 | doCode(SDI{size, insn}::rest, pos, changed) = let
124 val newSize = J.sdiSize(insn, Label.addrOf, pos)
125 in
126 if newSize <= !size then doCode(rest, !size + pos, changed)
127 else (size:=newSize; doCode(rest, newSize+pos, true))
128 end
129 | doCode([], pos, changed) = labels(rest, pos, changed)
130 in doCode(code, pos, chgd)
131 end
132 | labels([], pos,chgd) = (pos, chgd)
133
134 fun clusterLabels clusters = let
135 fun f (CLUSTER cl, (pos, chgd)) = labels(cl, pos, chgd)
136 in List.foldl f (0, false) clusters
137 end
138
139 fun fixpoint zl = let
140 val (size, changed) = clusterLabels zl
141 in if changed then fixpoint zl else size
142 end
143
144 val Emitter.S.STREAM{emit,defineLabel,beginCluster,pseudoOp,...} =
145 Emitter.makeStream []
146
147 fun emitCluster(CLUSTER(comp),loc) = let
148 fun process(PSEUDO pOp,loc) = (pseudoOp pOp; loc + P.sizeOf(pOp,loc))
149 | process(LABEL lab,loc) = (defineLabel lab; loc)
150 | process(CODE code,loc) = let
151 fun emitInstrs insns = app emit insns
152 fun e(FIXED{insns, size,...},loc) = (emitInstrs insns; loc+size)
153 | e(SDI{size, insn},loc) =
154 (emitInstrs(J.expand(insn, !size, loc)); !size + loc)
155 in foldl e loc code
156 end
157 in foldl process loc comp
158 end
159
160 fun initLabels(clusters) = let
161 fun init(PSEUDO(p)::rest, loc) =
162 (P.adjustLabels(p, loc); init(rest, loc + P.sizeOf(p, loc)))
163 | init(LABEL lab::rest, loc) = (Label.setAddr(lab,loc); init(rest, loc))
164 | init(CODE code::rest, loc) = let
165 fun size(FIXED{size, ...}) = size
166 | size(SDI{size, ...}) = !size
167 in
168 init(rest, List.foldl (fn (c, b) => size(c) + b) loc code)
169 end
170 | init([], loc) = loc
171
172 in
173 List.foldl
174 (fn (CLUSTER(cl), loc) => init(cl, loc)) 0 clusters
175 end
176
177 (* The dataList is in reverse order and the clusters are in reverse *)
178 fun dataCluster([], acc) = CLUSTER(acc)
179 | dataCluster(d::dl, acc) = dataCluster(dl, PSEUDO d::acc)
180 val compressed =
181 rev (dataCluster(!dataList, []) :: !clusterList) before cleanUp()
182 in
183 initLabels(compressed);
184 beginCluster(fixpoint (compressed));
185 foldl emitCluster 0 compressed;
186 ()
187 end (*finish*)
188 end (* bbsched2 *)
189
190