Debian Codegraph @

lib/mlrisc-lib/MLRISC/mltree/mltree-utils.sml

raw
1 (*
2 * Common operations on MLTREE
3 *
4 * -- Allen
5 *)
6 functor MLTreeUtils
7 (structure T : MLTREE
8 (* Hashing extensions *)
9 val hashSext : T.hasher -> T.sext -> word
10 val hashRext : T.hasher -> T.rext -> word
11 val hashFext : T.hasher -> T.fext -> word
12 val hashCCext : T.hasher -> T.ccext -> word
13
14 (* Equality extensions *)
15 val eqSext : T.equality -> T.sext * T.sext -> bool
16 val eqRext : T.equality -> T.rext * T.rext -> bool
17 val eqFext : T.equality -> T.fext * T.fext -> bool
18 val eqCCext : T.equality -> T.ccext * T.ccext -> bool
19
20 (* Pretty printing extensions *)
21 val showSext : T.printer -> T.sext -> string
22 val showRext : T.printer -> T.ty * T.rext -> string
23 val showFext : T.printer -> T.fty * T.fext -> string
24 val showCCext : T.printer -> T.ty * T.ccext -> string
25 ) : MLTREE_UTILS =
26 struct
27
28 structure T = T
29 structure I = T.I
30 structure Constant = T.Constant
31 structure Region = T.Region
32 structure B = T.Basis
33 structure C = CellsBasis
34 structure W = Word
35
36
37 val w = W.fromInt
38 val i2s = Int.toString
39 val toLower = String.map Char.toLower
40
41 fun error msg = MLRiscErrorMsg.error("MLTreeUtils",msg)
42 fun wv(C.CELL{id, ...}) = w id
43 fun wvs is =
44 let fun f([],h) = h
45 | f(i::is,h) = f(is,wv i+h)
46 in f(is,0w0) end
47
48
49 (*
50 * Hashing
51 *)
52 val hashLabel = Label.hash
53 fun hasher() = {stm=hashStm, rexp=hashRexp, fexp=hashFexp, ccexp=hashCCexp}
54 and hashCtrl ctrl = wv ctrl
55 and hashStm stm =
56 case stm of
57 T.MV(t,dst,rexp) => 0w123 + w t + wv dst + hashRexp rexp
58 | T.CCMV(dst,ccexp) => 0w1234 + wv dst + hashCCexp ccexp
59 | T.FMV(fty,dst,fexp) => 0w12345 + w fty + wv dst + hashFexp fexp
60 | T.COPY(ty,dst,src) => 0w234 + w ty + wvs dst + wvs src
61 | T.FCOPY(fty,dst,src) => 0w456 + w fty + wvs dst + wvs src
62 | T.JMP(ea,labels) => 0w45 + hashRexp ea
63 | T.CALL{funct,targets,defs,uses,region,pops} =>
64 hashRexp funct + hashMlriscs defs + hashMlriscs uses
65 | T.FLOW_TO(stm, _) => hashStm stm
66 | T.RET _ => 0w567
67 | T.STORE(ty,ea,data,mem) => 0w888 + w ty + hashRexp ea + hashRexp data
68 | T.FSTORE(fty,ea,data,mem) => 0w7890 + w fty + hashRexp ea + hashFexp data
69 | T.BCC(a,lab) => 0w233 + hashCCexp a + hashLabel lab
70 | T.IF(a,b,c) => 0w233 + hashCCexp a + hashStm b + hashStm c
71 | T.ANNOTATION(stm, a) => hashStm stm
72 | T.PHI{preds,block} => w block
73 | T.SOURCE => 0w123
74 | T.SINK => 0w423
75 | T.REGION(stm,ctrl) => hashStm stm + hashCtrl ctrl
76 | T.RTL{hash,...} => hash
77 | T.SEQ ss => hashStms(ss, 0w23)
78 | T.ASSIGN(ty,lhs,rhs) => w ty + hashRexp lhs + hashRexp rhs
79 | _ => error "hashStm"
80
81 and hashStms([],h) = h
82 | hashStms(s::ss,h) = hashStms(ss,hashStm s + h)
83
84 and hashMlrisc(T.CCR ccexp) = hashCCexp ccexp
85 | hashMlrisc(T.GPR rexp) = hashRexp rexp
86 | hashMlrisc(T.FPR fexp) = hashFexp fexp
87
88 and hashMlriscs [] = 0w123
89 | hashMlriscs(m::ms) = hashMlrisc m + hashMlriscs ms
90
91 and hash2(ty,x,y) = w ty + hashRexp x + hashRexp y
92
93 and hashm T.DIV_TO_ZERO = 0w158
94 | hashm T.DIV_TO_NEGINF = 0w159
95
96 and hash3(m,ty,x,y) = hashm m + w ty + hashRexp x + hashRexp y
97
98 and hashRexp rexp =
99 case rexp of
100 T.REG(ty, src) => w ty + wv src
101 | T.LI i => I.hash i
102 | T.LABEL l => hashLabel l
103 | T.LABEXP le => hashRexp rexp
104 | T.CONST c => Constant.hash c
105 | T.NEG(ty, x) => w ty + hashRexp x + 0w24
106 | T.ADD x => hash2 x + 0w234
107 | T.SUB x => hash2 x + 0w456
108 | T.MULS x => hash2 x + 0w2131
109 | T.DIVS x => hash3 x + 0w156
110 | T.REMS x => hash3 x + 0w231
111 | T.MULU x => hash2 x + 0w123
112 | T.DIVU x => hash2 x + 0w1234
113 | T.REMU x => hash2 x + 0w211
114 | T.NEGT(ty, x) => w ty + hashRexp x + 0w1224
115 | T.ADDT x => hash2 x + 0w1219
116 | T.SUBT x => hash2 x + 0w999
117 | T.MULT x => hash2 x + 0w7887
118 | T.DIVT x => hash3 x + 0w88884
119 | T.ANDB x => hash2 x + 0w12312
120 | T.ORB x => hash2 x + 0w558
121 | T.XORB x => hash2 x + 0w234
122 | T.EQVB x => hash2 x + 0w734
123 | T.NOTB(ty, x) => w ty + hashRexp x
124 | T.SRA x => hash2 x + 0w874
125 | T.SRL x => hash2 x + 0w223
126 | T.SLL x => hash2 x + 0w499
127 | T.COND(ty,e,e1,e2) => w ty + hashCCexp e + hashRexp e1 + hashRexp e2
128 | T.SX(ty, ty', rexp) => 0w232 + w ty + w ty' + hashRexp rexp
129 | T.ZX(ty, ty', rexp) => 0w737 + w ty + w ty' + hashRexp rexp
130 | T.CVTF2I(ty, round, ty', fexp) =>
131 w ty + B.hashRoundingMode round + w ty' + hashFexp fexp
132 | T.LOAD(ty, ea, mem) => w ty + hashRexp ea + 0w342
133 | T.LET(stm, rexp) => hashStm stm + hashRexp rexp
134 | T.PRED(e, ctrl) => hashRexp e + hashCtrl ctrl
135 | T.MARK(e, _) => hashRexp e
136 | T.REXT(ty, rext) => w ty + hashRext (hasher()) rext
137 | T.??? => 0w485
138 | T.OP(ty,oper,es) => hashRexps(es, w ty + hashOper oper)
139 | T.ARG _ => 0w23
140 | T.$(ty, k, e) => w ty + hashRexp e
141 | T.PARAM n => w n
142 | T.BITSLICE(ty, sl, e) => w ty + hashRexp e
143
144 and hashOper(T.OPER{hash, ...}) = hash
145
146 and hashRexps([],h) = h
147 | hashRexps(e::es,h) = hashRexps(es,hashRexp e + h)
148
149 and hash2'(ty,x,y) = w ty + hashFexp x + hashFexp y
150
151 and hashFexp fexp =
152 case fexp of
153 T.FREG(fty, src) => w fty + wv src
154 | T.FLOAD(fty, ea, mem) => w fty + hashRexp ea
155 | T.FADD x => hash2' x + 0w123
156 | T.FMUL x => hash2' x + 0w1234
157 | T.FSUB x => hash2' x + 0w12345
158 | T.FDIV x => hash2' x + 0w234
159 | T.FCOPYSIGN x => hash2' x + 0w883
160 | T.FCOND(fty,c,x,y) => w fty + hashCCexp c + hashFexp x + hashFexp y
161 | T.FABS(fty, fexp) => w fty + hashFexp fexp + 0w2345
162 | T.FNEG(fty, fexp) => w fty + hashFexp fexp + 0w23456
163 | T.FSQRT(fty, fexp) => w fty + hashFexp fexp + 0w345
164 | T.CVTI2F(fty, ty, rexp) => w fty + w ty + hashRexp rexp
165 | T.CVTF2F(fty, fty', fexp) => w fty + hashFexp fexp + w fty'
166 | T.FMARK(e, _) => hashFexp e
167 | T.FPRED(e, ctrl) => hashFexp e + hashCtrl ctrl
168 | T.FEXT(fty, fext) => w fty + hashFext (hasher()) fext
169
170 and hashFexps([],h) = h
171 | hashFexps(e::es,h) = hashFexps(es,hashFexp e + h)
172
173 and hashCCexp ccexp =
174 case ccexp of
175 T.CC(cc, src) => B.hashCond cc + wv src
176 | T.FCC(fcc, src) => B.hashFcond fcc + wv src
177 | T.CMP(ty, cond, x, y) =>
178 w ty + B.hashCond cond + hashRexp x + hashRexp y
179 | T.FCMP(fty, fcond, x, y) =>
180 w fty + B.hashFcond fcond + hashFexp x + hashFexp y
181 | T.NOT x => 0w2321 + hashCCexp x
182 | T.AND(x,y) => 0w2321 + hashCCexp x + hashCCexp y
183 | T.OR(x,y) => 0w8721 + hashCCexp x + hashCCexp y
184 | T.XOR(x,y) => 0w6178 + hashCCexp x + hashCCexp y
185 | T.EQV(x,y) => 0w178 + hashCCexp x + hashCCexp y
186 | T.TRUE => 0w0
187 | T.FALSE => 0w1232
188 | T.CCMARK(e, _) => hashCCexp e
189 | T.CCEXT(ty,ccext) => w ty + hashCCext (hasher()) ccext
190
191 and hashCCexps([],h) = h
192 | hashCCexps(e::es,h) = hashCCexps(es,hashCCexp e + h)
193
194 (*
195 * Equality
196 *)
197
198 val eqLabel = Label.same
199 fun eqLabels([],[]) = true
200 | eqLabels(a::b,c::d) = eqLabel(a,c) andalso eqLabels(b,d)
201 | eqLabels _ = false
202 and eqCell(C.CELL{id=x, ...},C.CELL{id=y, ...}) = x=y
203 and eqCells([], []) = true
204 | eqCells(x::xs,y::ys) = eqCell(x,y) andalso eqCells(xs,ys)
205 | eqCells _ = false
206 and eqCopy((t1,dst1,src1),(t2,dst2,src2)) =
207 t1=t2 andalso eqCells(dst1,dst2) andalso eqCells(src1,src2)
208 and eqCtrl(c1,c2) = eqCell(c1,c2)
209 and eqCtrls(c1,c2) = eqCells(c1,c2)
210
211 (* statements *)
212 and equality() = {stm=eqStm, rexp=eqRexp, fexp=eqFexp, ccexp=eqCCexp}
213 and eqStm(T.MV(a,b,c),T.MV(d,e,f)) =
214 a=d andalso eqCell(b,e) andalso eqRexp(c,f)
215 | eqStm(T.CCMV(a,b),T.CCMV(c,d)) = eqCell(a,c) andalso eqCCexp(b,d)
216 | eqStm(T.FMV(a,b,c),T.FMV(d,e,f)) =
217 a=d andalso eqCell(b,e) andalso eqFexp(c,f)
218 | eqStm(T.COPY x,T.COPY y) = eqCopy(x,y)
219 | eqStm(T.FCOPY x,T.FCOPY y) = eqCopy(x,y)
220 | eqStm(T.JMP(a,b),T.JMP(a',b')) = eqRexp(a,a')
221 | eqStm(T.CALL{funct=a,defs=b,uses=c,...},
222 T.CALL{funct=d,defs=e,uses=f,...}) =
223 eqRexp(a,d) andalso eqMlriscs(b,e) andalso eqMlriscs(c,f)
224 | eqStm(T.FLOW_TO(x,a), T.FLOW_TO(y,b)) =
225 eqStm(x,y) andalso eqLabels(a,b)
226 | eqStm(T.RET _,T.RET _) = true
227 | eqStm(T.STORE(a,b,c,_),T.STORE(d,e,f,_)) =
228 a=d andalso eqRexp(b,e) andalso eqRexp(c,f)
229 | eqStm(T.FSTORE(a,b,c,_),T.FSTORE(d,e,f,_)) =
230 a=d andalso eqRexp(b,e) andalso eqFexp(c,f)
231 | eqStm(T.ANNOTATION(s1, _),s2) = eqStm(s1,s2)
232 | eqStm(s1,T.ANNOTATION(s2, _)) = eqStm(s1,s2)
233 | eqStm(T.PHI x,T.PHI y) = x=y
234 | eqStm(T.SOURCE,T.SOURCE) = true
235 | eqStm(T.SINK,T.SINK) = true
236 | eqStm(T.BCC(b,c),T.BCC(b',c')) =
237 eqCCexp(b,b') andalso eqLabel(c,c')
238 | eqStm(T.IF(b,c,d),T.IF(b',c',d')) =
239 eqCCexp(b,b') andalso eqStm(c,c') andalso eqStm(d,d')
240 | eqStm(T.RTL{attribs=x,...},T.RTL{attribs=y,...}) = x=y
241 | eqStm(T.REGION(a,b),T.REGION(a',b')) = eqCtrl(b,b') andalso eqStm(a,a')
242 | eqStm(T.EXT a,T.EXT a') = eqSext (equality()) (a,a')
243 | eqStm _ = false
244
245 and eqStms([],[]) = true
246 | eqStms(a::b,c::d) = eqStm(a,c) andalso eqStms(b,d)
247 | eqStms _ = false
248
249 and eqMlrisc(T.CCR a,T.CCR b) = eqCCexp(a,b)
250 | eqMlrisc(T.GPR a,T.GPR b) = eqRexp(a,b)
251 | eqMlrisc(T.FPR a,T.FPR b) = eqFexp(a,b)
252 | eqMlrisc _ = false
253
254 and eqMlriscs([],[]) = true
255 | eqMlriscs(a::b,c::d) = eqMlrisc(a,c) andalso eqMlriscs(b,d)
256 | eqMlriscs _ = false
257
258 and eq2((a,b,c),(d,e,f)) = a=d andalso eqRexp(b,e) andalso eqRexp(c,f)
259
260 and eq3((m,a,b,c),(n,d,e,f)) =
261 m=n andalso a=d andalso eqRexp(b,e) andalso eqRexp(c,f)
262
263 and eqRexp(T.REG(a,b),T.REG(c,d)) = a=c andalso eqCell(b,d)
264 | eqRexp(T.LI a,T.LI b) = a = b
265 | eqRexp(T.LABEL a,T.LABEL b) = eqLabel(a,b)
266 | eqRexp(T.LABEXP a,T.LABEXP b) = eqRexp(a,b)
267 | eqRexp(T.CONST a,T.CONST b) = Constant.==(a,b)
268 | eqRexp(T.NEG(t,x),T.NEG(t',x')) = t = t' andalso eqRexp(x,x')
269 | eqRexp(T.ADD x,T.ADD y) = eq2(x,y)
270 | eqRexp(T.SUB x,T.SUB y) = eq2(x,y)
271 | eqRexp(T.MULS x,T.MULS y) = eq2(x,y)
272 | eqRexp(T.DIVS x,T.DIVS y) = eq3(x,y)
273 | eqRexp(T.REMS x,T.REMS y) = eq3(x,y)
274 | eqRexp(T.MULU x,T.MULU y) = eq2(x,y)
275 | eqRexp(T.DIVU x,T.DIVU y) = eq2(x,y)
276 | eqRexp(T.REMU x,T.REMU y) = eq2(x,y)
277 | eqRexp(T.NEGT(t,x),T.NEGT(t',x')) = t = t' andalso eqRexp(x,x')
278 | eqRexp(T.ADDT x,T.ADDT y) = eq2(x,y)
279 | eqRexp(T.SUBT x,T.SUBT y) = eq2(x,y)
280 | eqRexp(T.MULT x,T.MULT y) = eq2(x,y)
281 | eqRexp(T.DIVT x,T.DIVT y) = eq3(x,y)
282 | eqRexp(T.ANDB x,T.ANDB y) = eq2(x,y)
283 | eqRexp(T.ORB x,T.ORB y) = eq2(x,y)
284 | eqRexp(T.XORB x,T.XORB y) = eq2(x,y)
285 | eqRexp(T.EQVB x,T.EQVB y) = eq2(x,y)
286 | eqRexp(T.NOTB(a,b),T.NOTB(c,d)) = a=c andalso eqRexp(b,d)
287 | eqRexp(T.SRA x,T.SRA y) = eq2(x,y)
288 | eqRexp(T.SRL x,T.SRL y) = eq2(x,y)
289 | eqRexp(T.SLL x,T.SLL y) = eq2(x,y)
290 | eqRexp(T.COND(a,b,c,d),T.COND(e,f,g,h)) =
291 a=e andalso eqCCexp(b,f) andalso eqRexp(c,g) andalso eqRexp(d,h)
292 | eqRexp(T.SX(a,b,c),T.SX(a',b',c')) =
293 a=a' andalso b=b' andalso eqRexp(c,c')
294 | eqRexp(T.ZX(a,b,c),T.ZX(a',b',c')) =
295 a=a' andalso b=b' andalso eqRexp(c,c')
296 | eqRexp(T.CVTF2I(a,b,c,d),T.CVTF2I(e,f,g,h)) =
297 a=e andalso b=f andalso c=g andalso eqFexp(d,h)
298 | eqRexp(T.LOAD(a,b,_),T.LOAD(c,d,_)) = a=c andalso eqRexp(b,d)
299 | eqRexp(T.LET(a,b),T.LET(c,d)) = eqStm(a,c) andalso eqRexp(b,d)
300 | eqRexp(T.ARG x,T.ARG y) = x = y
301 | eqRexp(T.PARAM x,T.PARAM y) = x = y
302 | eqRexp(T.???,T.???) = true
303 | eqRexp(T.$(t1,k1,e1),T.$(t2,k2,e2)) =
304 t1=t2 andalso k1=k2 andalso eqRexp(e1,e2)
305 | eqRexp(T.BITSLICE(t1,s1,e1),T.BITSLICE(t2,s2,e2)) =
306 t1=t2 andalso s1=s2 andalso eqRexp(e1,e2)
307 | eqRexp(T.MARK(a,_),b) = eqRexp(a,b)
308 | eqRexp(a,T.MARK(b,_)) = eqRexp(a,b)
309 | eqRexp(T.PRED(a,b),T.PRED(a',b')) = eqCtrl(b,b') andalso eqRexp(a,a')
310 | eqRexp(T.REXT(a,b),T.REXT(a',b')) =
311 a=a' andalso eqRext (equality()) (b,b')
312 | eqRexp _ = false
313
314 and eqRexps([],[]) = true
315 | eqRexps(a::b,c::d) = eqRexp(a,c) andalso eqRexps(b,d)
316 | eqRexps _ = false
317
318 and eq2'((a,b,c),(d,e,f)) = a=d andalso eqFexp(b,e) andalso eqFexp(c,f)
319 and eq1'((a,b),(d,e)) = a=d andalso eqFexp(b,e)
320
321 and eqFexp(T.FREG(t1,x),T.FREG(t2,y)) = t1=t2 andalso eqCell(x,y)
322 | eqFexp(T.FLOAD(a,b,_),T.FLOAD(c,d,_)) = a=c andalso eqRexp(b,d)
323 | eqFexp(T.FADD x,T.FADD y) = eq2'(x,y)
324 | eqFexp(T.FMUL x,T.FMUL y) = eq2'(x,y)
325 | eqFexp(T.FSUB x,T.FSUB y) = eq2'(x,y)
326 | eqFexp(T.FDIV x,T.FDIV y) = eq2'(x,y)
327 | eqFexp(T.FCOPYSIGN x, T.FCOPYSIGN y) = eq2'(x,y)
328 | eqFexp(T.FCOND(t,x,y,z), T.FCOND(t',x',y',z')) =
329 t=t' andalso eqCCexp(x,x') andalso eqFexp(y,y') andalso eqFexp(z,z')
330 | eqFexp(T.FABS x,T.FABS y) = eq1'(x,y)
331 | eqFexp(T.FNEG x,T.FNEG y) = eq1'(x,y)
332 | eqFexp(T.FSQRT x,T.FSQRT y) = eq1'(x,y)
333 | eqFexp(T.CVTI2F(a,b,c),T.CVTI2F(a',b',c')) =
334 a=a' andalso b=b' andalso eqRexp(c,c')
335 | eqFexp(T.CVTF2F(a,b,c),T.CVTF2F(a',b',c')) =
336 a=a' andalso b=b' andalso eqFexp(c,c')
337 | eqFexp(T.FEXT(a,f),T.FEXT(b,g)) = a=b andalso eqFext (equality()) (f,g)
338 | eqFexp(T.FMARK(a,_),b) = eqFexp(a,b)
339 | eqFexp(a,T.FMARK(b,_)) = eqFexp(a,b)
340 | eqFexp(T.FPRED(a,b),T.FPRED(a',b')) = eqCtrl(b,b') andalso eqFexp(a,a')
341 | eqFexp _ = false
342
343 and eqFexps([],[]) = true
344 | eqFexps(a::b,c::d) = eqFexp(a,c) andalso eqFexps(b,d)
345 | eqFexps _ = false
346
347 and eqCCexp(T.CC(c1,x),T.CC(c2,y)) = c1=c2 andalso eqCell(x,y)
348 | eqCCexp(T.FCC(c1,x),T.FCC(c2,y)) = c1=c2 andalso eqCell(x,y)
349 | eqCCexp(T.CMP(x,a,b,c),T.CMP(y,d,e,f)) =
350 a=d andalso eqRexp(b,e) andalso eqRexp(c,f) andalso x = y
351 | eqCCexp(T.FCMP(x,a,b,c),T.FCMP(y,d,e,f)) =
352 a=d andalso eqFexp(b,e) andalso eqFexp(c,f) andalso x = y
353 | eqCCexp(T.NOT x, T.NOT y) = eqCCexp(x,y)
354 | eqCCexp(T.AND x, T.AND y) = eqCCexp2(x,y)
355 | eqCCexp(T.OR x, T.OR y) = eqCCexp2(x,y)
356 | eqCCexp(T.XOR x, T.XOR y) = eqCCexp2(x,y)
357 | eqCCexp(T.EQV x, T.EQV y) = eqCCexp2(x,y)
358 | eqCCexp(T.CCMARK(a,_),b) = eqCCexp(a,b)
359 | eqCCexp(a,T.CCMARK(b,_)) = eqCCexp(a,b)
360 | eqCCexp(T.CCEXT(t,a),T.CCEXT(t',b)) =
361 t=t' andalso eqCCext (equality()) (a,b)
362 | eqCCexp(T.TRUE, T.TRUE) = true
363 | eqCCexp(T.FALSE, T.FALSE) = true
364 | eqCCexp _ = false
365
366 and eqCCexp2((x,y),(x',y')) = eqCCexp(x,x') andalso eqCCexp(y,y')
367
368 and eqCCexps([],[]) = true
369 | eqCCexps(a::b,c::d) = eqCCexp(a,c) andalso eqCCexps(b,d)
370 | eqCCexps _ = false
371
372 (*
373 * Pretty printing
374 *)
375 fun show {def,use,regionDef,regionUse} =
376 let fun ty t = "."^i2s t
377 fun fty 32 = ".s"
378 | fty 64 = ".d"
379 | fty 128 = ".q"
380 | fty t = ty t
381
382 fun reg(t,v) = C.toString v^ty t
383 fun freg(t,v) = C.toString v^fty t
384 fun ccreg v = C.toString v
385 fun ctrlreg v = C.toString v
386
387 fun srcReg(t,v) = reg(t,v)
388 fun srcFreg(t,v) = freg(t,v)
389 fun srcCCreg v = ccreg v
390 fun srcCtrlreg v = ctrlreg v
391
392 fun dstReg(t,v) = reg(t,v)
393 fun dstFreg(t,v) = freg(t,v)
394 fun dstCCreg v = ccreg v
395 fun dstCtrlreg v = ctrlreg v
396
397 fun srcParam(i) = def i handle _ => "<"^i2s i^">"
398 fun dstParam(i) = use i handle _ => "<"^i2s i^">"
399
400 fun listify f =
401 let fun g(t,[]) = ""
402 | g(t,[r]) = f(t,r)
403 | g(t,r::rs) = f(t,r)^","^g(t,rs)
404 in g end
405
406 fun listify' f = (String.concatWith ",") o (List.map f)
407
408 val srcRegs = listify srcReg
409 val dstRegs = listify dstReg
410 val srcFregs = listify srcFreg
411 val dstFregs = listify dstFreg
412 val srcCCregs = listify' srcCCreg
413 val dstCCregs = listify' dstCCreg
414 val srcCtrlregs = listify' srcCtrlreg
415 val dstCtrlregs = listify' dstCtrlreg
416 fun usectrl cr = " ["^srcCtrlreg cr^"]"
417 fun usectrls [] = ""
418 | usectrls cr = " ["^srcCtrlregs cr^"]"
419 fun defctrl cr = ""^dstCtrlreg cr^" <- "
420 fun defctrls [] = ""
421 | defctrls cr = ""^dstCtrlregs cr^" <- "
422
423 fun copy(t,dst,src) = dstRegs(t, dst)^" := "^srcRegs(t, src)
424 fun fcopy(t,dst,src) = dstFregs(t, dst)^" := "^srcFregs(t, src)
425
426 fun shower() = {stm=stm, rexp=rexp, fexp=fexp, ccexp=ccexp,
427 dstReg=dstReg, srcReg=srcReg}
428 (* pretty print a statement *)
429 and stm(T.MV(t,dst,e)) = dstReg(t,dst)^" := "^rexp e
430 | stm(T.CCMV(dst,e)) = dstCCreg dst^" := "^ccexp e
431 | stm(T.FMV(fty,dst,e)) = dstFreg(fty,dst)^" := "^fexp e
432 | stm(T.COPY(ty,dst,src)) = copy(ty,dst,src)
433 | stm(T.FCOPY(fty,dst,src)) = fcopy(fty,dst,src)
434 | stm(T.JMP(ea,labels)) = "jmp "^rexp ea
435 | stm(T.BCC(a,lab)) =
436 "bcc "^ccexp a^" "^Label.toString lab
437 | stm(T.CALL{funct,targets,defs,uses,region,pops}) =
438 "call "^rexp funct
439 | stm(T.FLOW_TO(s, targets)) =
440 stm s^" ["^listify' Label.toString targets^"]"
441 | stm(T.RET(flow)) = "ret"
442 | stm(T.IF(a,b,T.SEQ [])) = "if "^ccexp a^" then "^stm b
443 | stm(T.IF(a,b,c)) = "if "^ccexp a^" then "^stm b^" else "^stm c
444 | stm(T.STORE(ty,ea,e,mem)) = store(ty,"",ea,mem,e)
445 | stm(T.FSTORE(fty,ea,e,mem)) = fstore(fty,"",ea,mem,e)
446 | stm(T.REGION(s,cr)) = stm s^usectrl cr
447 | stm(T.SEQ []) = "skip"
448 | stm(T.SEQ s) = stms(";\n",s)
449 | stm(T.DEFINE lab) = Label.toString lab ^ ":"
450 | stm(T.ANNOTATION(s, a)) = stm s
451 | stm(T.EXT x) = showSext (shower()) x
452 | stm(T.LIVE exps) = "live: " ^ mlriscs exps
453 | stm(T.KILL exps) = "kill: " ^ mlriscs exps
454 | stm(T.PHI{preds, block}) = "phi["^i2s block^"]"
455 | stm(T.ASSIGN(ty,lhs,T.???)) = "define "^rexp lhs
456 | stm(T.ASSIGN(ty,T.???,rhs)) = "use "^rexp rhs
457 | stm(T.ASSIGN(ty,x,rhs)) = lhs x^" := "^rexp rhs
458 | stm(T.SOURCE) = "source"
459 | stm(T.SINK) = "sink"
460 | stm(T.RTL{e,...}) = stm e
461
462 and stms(sep,[]) = ""
463 | stms(sep,[s]) = stm s
464 | stms(sep,s::ss) = stm s^sep^stms(sep,ss)
465
466 and lhs(T.PARAM i) = dstParam i
467 | lhs(T.$(ty,k,T.PARAM i)) = dstParam i
468 | lhs(e) = rexp e
469
470 (* pretty print an expression *)
471 and rexp(T.REG(ty, src)) = srcReg(ty,src)
472 | rexp(T.LI i) = IntInf.toString i
473 | rexp(T.LABEL l) = Label.toString l
474 | rexp(T.CONST c) = Constant.toString c
475 | rexp(T.LABEXP le) = rexp le
476 | rexp(T.NEG x) = unary("~",x)
477 | rexp(T.ADD x) = binary("+",x)
478 | rexp(T.SUB x) = binary("-",x)
479 | rexp(T.MULS x) = two("muls",x)
480 | rexp(T.DIVS x) = three("divs",x)
481 | rexp(T.REMS x) = three("rems",x)
482 | rexp(T.MULU x) = two("mulu",x)
483 | rexp(T.DIVU x) = two("divu",x)
484 | rexp(T.REMU x) = two("remu",x)
485 | rexp(T.NEGT x) = one("negt",x)
486 | rexp(T.ADDT x) = two("addt",x)
487 | rexp(T.SUBT x) = two("subt",x)
488 | rexp(T.MULT x) = two("mult",x)
489 | rexp(T.DIVT x) = three("divt",x)
490 | rexp(T.ANDB x) = binary("&",x)
491 | rexp(T.ORB x) = binary("|",x)
492 | rexp(T.XORB x) = binary("^",x)
493 | rexp(T.EQVB x) = binary("eqvb",x)
494 | rexp(T.NOTB x) = unary("!",x)
495 | rexp(T.SRA x) = binary("~>>",x)
496 | rexp(T.SRL x) = binary(">>",x)
497 | rexp(T.SLL x) = binary("<<",x)
498 | rexp(T.COND(t,cc,e1,e2)) =
499 "cond"^ty t^"("^ccexp cc^","^rexp e1^","^rexp e2^")"
500 | rexp(T.SX(t, t', e)) = "sx"^ty t^ty t'^" "^rexp e
501 | rexp(T.ZX(t, t', e)) = "zx"^ty t^ty t'^" "^rexp e
502 | rexp(T.CVTF2I(t, round, t', e)) =
503 "cvtf2i"^ty t^toLower(B.roundingModeToString round)^
504 fty t'^" "^fexp e
505 | rexp(T.LOAD(ty, ea, mem)) = load(ty,"",ea,mem)
506 | rexp(T.LET(s, e)) = stm s^";"^rexp e
507 | rexp(T.PRED(e, cr)) = rexp e^usectrl cr
508 | rexp(T.MARK(e, _)) = rexp e
509 | rexp(T.REXT e) = showRext (shower()) e
510 | rexp(T.???) = "???"
511 | rexp(T.OP(t,opc,es)) = oper opc^ty t^" "^rexps es
512 | rexp(T.ARG(t,ref(T.REP kind),name)) =
513 name^":"^kind^(if t = 0 then "" else ty t)
514 | rexp(T.PARAM n) = srcParam n
515 | rexp(T.$(ty,k,e)) =
516 "$"^C.cellkindToNickname k^"["^rexp e^"]"
517 | rexp(T.BITSLICE(ty,sl,e)) = rexp e^" at "^slices sl
518
519 and oper(T.OPER{name,...}) = name
520
521 and parenRexp
522 (e as (T.REG _ | T.LI _ | T.$ _ | T.ARG _)) =
523 rexp e
524 | parenRexp e = "("^rexp e^")"
525
526 and slices sc = listify' (fn (from,to) => i2s from^".."^i2s to) sc
527
528 (* pretty print a real expression *)
529 and fexp(T.FREG f) = srcFreg f
530 | fexp(T.FLOAD(fty, ea, mem)) = fload(fty,"",ea,mem)
531 | fexp(T.FADD x) = two'("fadd",x)
532 | fexp(T.FMUL x) = two'("fmul",x)
533 | fexp(T.FSUB x) = two'("fsub",x)
534 | fexp(T.FDIV x) = two'("fdiv",x)
535 | fexp(T.FCOPYSIGN x) = two'("fcopysign",x)
536 | fexp(T.FABS x) = one'("fabs",x)
537 | fexp(T.FNEG x) = one'("fneg",x)
538 | fexp(T.FSQRT x) = one'("fsqrt",x)
539 | fexp(T.FCOND(t,cc,e1,e2)) =
540 "fcond"^fty t^ccexp cc^"("^fexp e1^","^fexp e2^")"
541 | fexp(T.CVTI2F(t, t', e)) = "cvti2f"^ty t'^" "^rexp e
542 | fexp(T.CVTF2F(t, t', e)) = "cvtf2f"^fty t^fty t'^" "^fexp e
543 | fexp(T.FPRED(e, cr)) = fexp e^usectrl cr
544 | fexp(T.FMARK(e, _)) = fexp e
545 | fexp(T.FEXT e) = showFext (shower()) e
546
547 and ccexp(T.CC(cc,r)) = srcCCreg r^toLower(B.condToString cc)
548 | ccexp(T.FCC(fcc,r)) = srcCCreg r^toLower(B.fcondToString fcc)
549 | ccexp(T.CMP(t,T.SETCC,x,y)) = "setcc"^ty t^pair(x,y)
550 | ccexp(T.CMP(t,cc,x,y)) =
551 "cmp"^toLower(B.condToString cc)^ty t^pair(x,y)
552 | ccexp(T.FCMP(t,T.SETFCC,x,y)) = "setfcc"^ty t^pair'(x,y)
553 | ccexp(T.FCMP(t,fcc,x,y)) =
554 "fcmp"^toLower(B.fcondToString fcc)^fty t^pair'(x,y)
555 | ccexp(T.NOT x) = "not "^ccexp x
556 | ccexp(T.AND(x,y)) = two''(" and ",x,y)
557 | ccexp(T.OR(x,y)) = two''(" or ",x,y)
558 | ccexp(T.XOR(x,y)) = two''(" xor ",x,y)
559 | ccexp(T.EQV(x,y)) = two''(" eqv ",x,y)
560 | ccexp(T.CCMARK(e, _)) = ccexp e
561 | ccexp(T.TRUE) = "true"
562 | ccexp(T.FALSE) = "false"
563 | ccexp(T.CCEXT(e)) = showCCext (shower()) e
564
565 and mlrisc(T.GPR e) = rexp e
566 | mlrisc(T.FPR e) = fexp e
567 | mlrisc(T.CCR e) = ccexp e
568
569 and mlriscs l = listify' mlrisc l
570
571 (* Auxiliary functions *)
572 and one(opcode,(t,x)) = opcode^ty t^"("^rexp x^")"
573 and two(opcode,(t,x,y)) = opcode^ty t^pair(x,y)
574 and three(opcode,(m,t,x,y)) = opcode^dmr m^ty t^pair(x,y)
575 and dmr T.DIV_TO_ZERO = "{0}"
576 | dmr T.DIV_TO_NEGINF = "{-inf}"
577 and binary(opcode,(t,x,y)) = parenRexp x^" "^opcode^ty t^" "^parenRexp y
578 and unary(opcode,(t,x)) = opcode^ty t^" "^parenRexp x
579 and pair(x,y) = "("^rexp x^","^rexp y^")"
580 and one'(opcode,(t,x)) = opcode^fty t^"("^fexp x^")"
581 and two'(opcode,(t,x,y)) = opcode^fty t^pair'(x,y)
582 and two''(c,x,y) = "("^ccexp x^ c ^ ccexp y^")"
583 and pair'(x,y) = "("^fexp x^","^fexp y^")"
584 and rexps es = "("^foldr (fn (e,"") => rexp e
585 | (e,x) => rexp e^","^x) "" es^")"
586 and fexps es = "("^foldr (fn (e,"") => fexp e
587 | (e,x) => fexp e^","^x) "" es^")"
588 and ccexps es = "("^foldr (fn (e,"") => ccexp e
589 | (e,x) => ccexp e^","^x) "" es^")"
590 and store(t,u,ea,m,e) = memdef(t,u,ea,m)^" := "^rexp e
591 and fstore(t,u,ea,m,e) = fmemdef(t,u,ea,m)^" := "^fexp e
592 and ccstore(u,ea,m,e) = ccmemdef(u,ea,m)^" := "^ccexp e
593 and load(t,u,ea,m) = memuse(t,u,ea,m)
594 and fload(t,u,ea,m) = fmemuse(t,u,ea,m)
595 and ccload(u,ea,m) = ccmemuse(u,ea,m)
596 and addr(u,ea,m,show) =
597 let val r = show m handle _ => Region.toString m
598 val r = if r = "" then r else ":"^r
599 in u^"["^rexp ea^r^"]" end
600 and mem(t,u,ea,m,show) = "mem"^ty t^addr(u,ea,m,show)
601 and fmem(t,u,ea,m,show) = "mem"^fty t^addr(u,ea,m,show)
602 and ccmem(u,ea,m,show) = "mem"^addr(u,ea,m,show)
603 and memdef(t,u,ea,m) = mem(t,u,ea,m,regionDef)
604 and fmemdef(t,u,ea,m) = fmem(t,u,ea,m,regionDef)
605 and ccmemdef(u,ea,m) = ccmem(u,ea,m,regionDef)
606 and memuse(t,u,ea,m) = mem(t,u,ea,m,regionUse)
607 and fmemuse(t,u,ea,m) = fmem(t,u,ea,m,regionUse)
608 and ccmemuse(u,ea,m) = ccmem(u,ea,m,regionUse)
609 in shower()
610 end
611
612 exception Nothing
613
614 fun dummy _ = raise Nothing
615 val dummy = {def=dummy, use=dummy, regionDef=dummy, regionUse=dummy}
616
617 fun stmToString s = #stm(show dummy) s
618 fun rexpToString s = #rexp(show dummy) s
619 fun fexpToString s = #fexp(show dummy) s
620 fun ccexpToString s = #ccexp(show dummy) s
621
622 end