Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/MLRISC/mltree/mltree-utils.sml
ViewVC logotype

Annotation of /sml/trunk/src/MLRISC/mltree/mltree-utils.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 555 - (view) (download)

1 : monnier 473 (*
2 :     * Common operations on MLTREE
3 :     *
4 :     * -- Allen
5 :     *)
6 : george 555 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 : monnier 473 struct
27 :    
28 :     structure T = T
29 :     structure Constant = T.Constant
30 : george 545 structure LabelExp = T.LabelExp
31 : monnier 473 structure Region = T.Region
32 : george 545 structure B = T.Basis
33 : monnier 473 structure W = Word
34 :    
35 :     val w = W.fromInt
36 :    
37 :     fun error msg = MLRiscErrorMsg.error("MLTreeUtils",msg)
38 :     fun ws is =
39 :     let fun f([],h) = h
40 :     | f(i::is,h) = f(is,w i+h)
41 :     in f(is,0w0) end
42 :    
43 :     (*
44 :     * Hashing
45 :     *)
46 : george 545 fun hashLabel(Label.Label{id,...}) = w id
47 : george 555 and hashCtrls ctrl = ws ctrl
48 :     and hashCtrl ctrl = w ctrl
49 :     and hasher() = {stm=hashStm, rexp=hashRexp, fexp=hashFexp, ccexp=hashCCexp}
50 : george 545 and hashStm stm =
51 : monnier 473 case stm of
52 :     T.MV(t,dst,rexp) => 0w123 + w t + w dst + hashRexp rexp
53 :     | T.CCMV(dst,ccexp) => 0w1234 + w dst + hashCCexp ccexp
54 :     | T.FMV(fty,dst,fexp) => 0w12345 + w fty + w dst + hashFexp fexp
55 :     | T.COPY(ty,dst,src) => 0w234 + w ty + ws dst + ws src
56 :     | T.FCOPY(fty,dst,src) => 0w456 + w fty + ws dst + ws src
57 : george 545 | T.JMP(ctrl,ea,labels) => 0w45 + hashRexp ea
58 :     | T.CALL(ea,flow,defs,uses,cdefs,cuses,mem) =>
59 :     hashRexp ea + hashMlriscs defs + hashMlriscs uses +
60 :     hashCtrls cdefs + hashCtrls cuses
61 :     | T.RET _ => 0w567
62 :     | T.STORE(ty,ea,data,mem) => 0w888 + w ty + hashRexp ea + hashRexp data
63 :     | T.FSTORE(fty,ea,data,mem) => 0w7890 + w fty + hashRexp ea + hashFexp data
64 :     | T.BCC(ctrl,a,lab) => 0w233 + hashCtrls ctrl + hashCCexp a + hashLabel lab
65 :     | T.IF(ctrl,a,b,c) =>
66 :     0w233 + hashCtrls ctrl + hashCCexp a + hashStm b + hashStm c
67 : monnier 473 | T.ANNOTATION(stm, a) => hashStm stm
68 : george 545 | T.PHI block => w block
69 :     | T.PINNED stm => 0w12312 + hashStm stm
70 :     | T.REGION(stm,ctrl) => hashStm stm + hashCtrl ctrl
71 :     | T.RTL{hash,...} => !hash
72 :     | T.SEQ ss => hashStms(ss, 0w23)
73 :     | _ => error "hashStm"
74 : monnier 473
75 : george 545 and hashStms([],h) = h
76 :     | hashStms(s::ss,h) = hashStms(ss,hashStm s + h)
77 :    
78 : monnier 473 and hashMlrisc(T.CCR ccexp) = hashCCexp ccexp
79 :     | hashMlrisc(T.GPR rexp) = hashRexp rexp
80 :     | hashMlrisc(T.FPR fexp) = hashFexp fexp
81 :    
82 :     and hashMlriscs [] = 0w123
83 :     | hashMlriscs(m::ms) = hashMlrisc m + hashMlriscs ms
84 :    
85 :     and hash2(ty,x,y) = w ty + hashRexp x + hashRexp y
86 :    
87 :     and hashRexp rexp =
88 :     case rexp of
89 :     T.REG(ty, src) => w ty + w src
90 :     | T.LI i => w i
91 :     | T.LI32 w => W.fromLargeWord w
92 :     | T.LI64 w => W.fromLargeWord(Word64.toLargeWord w)
93 :     | T.LABEL le => LabelExp.hash le
94 :     | T.CONST c => Constant.hash c
95 : george 545 | T.NEG(ty, x) => w ty + hashRexp x + 0w24
96 : monnier 473 | T.ADD x => hash2 x + 0w234
97 :     | T.SUB x => hash2 x + 0w456
98 :     | T.MULS x => hash2 x + 0w2131
99 :     | T.DIVS x => hash2 x + 0w156
100 : george 545 | T.QUOTS x => hash2 x + 0w1565
101 : monnier 473 | T.REMS x => hash2 x + 0w231
102 :     | T.MULU x => hash2 x + 0w123
103 :     | T.DIVU x => hash2 x + 0w1234
104 :     | T.REMU x => hash2 x + 0w211
105 : george 545 | T.NEGT(ty, x) => w ty + hashRexp x + 0w1224
106 : monnier 473 | T.ADDT x => hash2 x + 0w1219
107 :     | T.SUBT x => hash2 x + 0w999
108 :     | T.MULT x => hash2 x + 0w7887
109 :     | T.DIVT x => hash2 x + 0w88884
110 : george 545 | T.QUOTT x => hash2 x + 0w8884
111 : monnier 473 | T.REMT x => hash2 x + 0w99
112 :     | T.ANDB x => hash2 x + 0w12312
113 :     | T.ORB x => hash2 x + 0w558
114 :     | T.XORB x => hash2 x + 0w234
115 :     | T.NOTB(ty, x) => w ty + hashRexp x
116 :     | T.SRA x => hash2 x + 0w874
117 :     | T.SRL x => hash2 x + 0w223
118 :     | T.SLL x => hash2 x + 0w499
119 :     | T.COND(ty,e,e1,e2) => w ty + hashCCexp e + hashRexp e1 + hashRexp e2
120 : george 545 | T.CVTI2I(ty, T.SIGN_EXTEND, ty', rexp) =>
121 :     0w232 + w ty + w ty' + hashRexp rexp
122 :     | T.CVTI2I(ty, T.ZERO_EXTEND, ty', rexp) =>
123 :     0w737 + w ty + w ty' + hashRexp rexp
124 : monnier 473 | T.CVTF2I(ty, round, ty', fexp) =>
125 : george 545 w ty + B.hashRoundingMode round + w ty' + hashFexp fexp
126 : monnier 473 | T.LOAD(ty, ea, mem) => w ty + hashRexp ea + 0w342
127 : george 545 | T.LET(stm, rexp) => hashStm stm + hashRexp rexp
128 :     | T.PRED(e, ctrl) => hashRexp e + hashCtrl ctrl
129 : monnier 473 | T.MARK(e, _) => hashRexp e
130 : george 545 | T.REXT(ty, rext) => w ty + hashRext (hasher()) rext
131 : monnier 473
132 :     and hashRexps([],h) = h
133 :     | hashRexps(e::es,h) = hashRexps(es,hashRexp e + h)
134 :    
135 :     and hash2'(ty,x,y) = w ty + hashFexp x + hashFexp y
136 :    
137 :     and hashFexp fexp =
138 :     case fexp of
139 :     T.FREG(fty, src) => w fty + w src
140 :     | T.FLOAD(fty, ea, mem) => w fty + hashRexp ea
141 :     | T.FADD x => hash2' x + 0w123
142 :     | T.FMUL x => hash2' x + 0w1234
143 :     | T.FSUB x => hash2' x + 0w12345
144 :     | T.FDIV x => hash2' x + 0w234
145 : george 545 | T.FCOPYSIGN x => hash2' x + 0w883
146 :     | T.FCOND(fty,c,x,y) => w fty + hashCCexp c + hashFexp x + hashFexp y
147 : monnier 473 | T.FABS(fty, fexp) => w fty + hashFexp fexp + 0w2345
148 :     | T.FNEG(fty, fexp) => w fty + hashFexp fexp + 0w23456
149 :     | T.FSQRT(fty, fexp) => w fty + hashFexp fexp + 0w345
150 : george 545 | T.CVTI2F(fty, ty, rexp) => w fty + w ty + hashRexp rexp
151 :     | T.CVTF2F(fty, fty', fexp) => w fty + hashFexp fexp + w fty'
152 : monnier 473 | T.FMARK(e, _) => hashFexp e
153 : george 545 | T.FPRED(e, ctrl) => hashFexp e + hashCtrl ctrl
154 :     | T.FEXT(fty, fext) => w fty + hashFext (hasher()) fext
155 : monnier 473
156 :     and hashFexps([],h) = h
157 :     | hashFexps(e::es,h) = hashFexps(es,hashFexp e + h)
158 :    
159 :     and hashCCexp ccexp =
160 :     case ccexp of
161 : george 545 T.CC(cc, src) => B.hashCond cc + w src
162 :     | T.FCC(fcc, src) => B.hashFcond fcc + w src
163 : monnier 473 | T.CMP(ty, cond, x, y) =>
164 : george 545 w ty + B.hashCond cond + hashRexp x + hashRexp y
165 : monnier 473 | T.FCMP(fty, fcond, x, y) =>
166 : george 545 w fty + B.hashFcond fcond + hashFexp x + hashFexp y
167 :     | T.NOT x => 0w2321 + hashCCexp x
168 :     | T.AND(x,y) => 0w2321 + hashCCexp x + hashCCexp y
169 :     | T.OR(x,y) => 0w8721 + hashCCexp x + hashCCexp y
170 :     | T.XOR(x,y) => 0w6178 + hashCCexp x + hashCCexp y
171 :     | T.TRUE => 0w0
172 :     | T.FALSE => 0w1232
173 : monnier 473 | T.CCMARK(e, _) => hashCCexp e
174 : george 545 | T.CCEXT(ty,ccext) => w ty + hashCCext (hasher()) ccext
175 : monnier 473
176 :     and hashCCexps([],h) = h
177 :     | hashCCexps(e::es,h) = hashCCexps(es,hashCCexp e + h)
178 :    
179 :     (*
180 :     * Equality
181 :     *)
182 :    
183 : george 545 fun eqLabel(Label.Label{id=x,...},Label.Label{id=y,...}) = x=y
184 : george 555 and eqLabels([],[]) = true
185 : george 545 | eqLabels(a::b,c::d) = eqLabel(a,c) andalso eqLabels(b,d)
186 :     | eqLabels _ = false
187 : monnier 473
188 :     (* statements *)
189 : george 555 and equality() = {stm=eqStm, rexp=eqRexp, fexp=eqFexp, ccexp=eqCCexp}
190 : george 545 and eqStm(T.MV(a,b,c),T.MV(d,e,f)) = b=e andalso a=d andalso eqRexp(c,f)
191 : monnier 473 | eqStm(T.CCMV(a,b),T.CCMV(c,d)) = a=c andalso eqCCexp(b,d)
192 :     | eqStm(T.FMV(a,b,c),T.FMV(d,e,f)) = b=e andalso a=d andalso eqFexp(c,f)
193 :     | eqStm(T.COPY x,T.COPY y) = x = y
194 :     | eqStm(T.FCOPY x,T.FCOPY y) = x = y
195 : george 545 | eqStm(T.JMP(a,b,c),T.JMP(a',b',c')) =
196 :     a=a' andalso eqRexp(b,b') andalso eqLabels(c,c')
197 :     | eqStm(T.CALL(a,_,b,c,_,_,_),T.CALL(d,_,e,f,_,_,_)) =
198 : monnier 473 eqRexp(a,d) andalso eqMlriscs(b,e) andalso eqMlriscs(c,f)
199 : george 545 | eqStm(T.RET _,T.RET _) = true
200 : monnier 473 | eqStm(T.STORE(a,b,c,_),T.STORE(d,e,f,_)) =
201 :     a=d andalso eqRexp(b,e) andalso eqRexp(c,f)
202 :     | eqStm(T.FSTORE(a,b,c,_),T.FSTORE(d,e,f,_)) =
203 :     a=d andalso eqRexp(b,e) andalso eqFexp(c,f)
204 :     | eqStm(T.ANNOTATION(s1, _),s2) = eqStm(s1,s2)
205 :     | eqStm(s1,T.ANNOTATION(s2, _)) = eqStm(s1,s2)
206 : george 545 | eqStm(T.PHI x,T.PHI y) = x=y
207 :     | eqStm(T.BCC(a,b,c),T.BCC(a',b',c')) =
208 :     a=a' andalso eqCCexp(b,b') andalso eqLabel(c,c')
209 :     | eqStm(T.IF(a,b,c,d),T.IF(a',b',c',d')) =
210 :     a=a' andalso eqCCexp(b,b') andalso eqStm(c,c') andalso eqStm(d,d')
211 :     | eqStm(T.PINNED x,T.PINNED y) = eqStm(x,y)
212 :     | eqStm(T.RTL{hash=x,...},T.RTL{hash=y,...}) = x=y
213 :     | eqStm(T.REGION(a,b),T.REGION(a',b')) = b = b' andalso eqStm(a,a')
214 :     | eqStm(T.EXT a,T.EXT a') = eqSext (equality()) (a,a')
215 : monnier 473 | eqStm _ = false
216 :    
217 :     and eqStms([],[]) = true
218 :     | eqStms(a::b,c::d) = eqStm(a,c) andalso eqStms(b,d)
219 :     | eqStms _ = false
220 :    
221 :     and eqMlrisc(T.CCR a,T.CCR b) = eqCCexp(a,b)
222 :     | eqMlrisc(T.GPR a,T.GPR b) = eqRexp(a,b)
223 :     | eqMlrisc(T.FPR a,T.FPR b) = eqFexp(a,b)
224 :     | eqMlrisc _ = false
225 :    
226 :     and eqMlriscs([],[]) = true
227 :     | eqMlriscs(a::b,c::d) = eqMlrisc(a,c) andalso eqMlriscs(b,d)
228 :     | eqMlriscs _ = false
229 :    
230 :     and eq2((a,b,c),(d,e,f)) = a=d andalso eqRexp(b,e) andalso eqRexp(c,f)
231 :    
232 :     and eqRexp(T.REG(a,b),T.REG(c,d)) = a=c andalso b=d
233 :     | eqRexp(T.LI a,T.LI b) = a=b
234 :     | eqRexp(T.LI32 a,T.LI32 b) = a=b
235 :     | eqRexp(T.LI64 a,T.LI64 b) = a=b
236 :     | eqRexp(T.LABEL a,T.LABEL b) = LabelExp.==(a,b)
237 :     | eqRexp(T.CONST a,T.CONST b) = Constant.==(a,b)
238 : george 545 | eqRexp(T.NEG(t,x),T.NEG(t',x')) = t = t' andalso eqRexp(x,x')
239 : monnier 473 | eqRexp(T.ADD x,T.ADD y) = eq2(x,y)
240 :     | eqRexp(T.SUB x,T.SUB y) = eq2(x,y)
241 :     | eqRexp(T.MULS x,T.MULS y) = eq2(x,y)
242 :     | eqRexp(T.DIVS x,T.DIVS y) = eq2(x,y)
243 : george 545 | eqRexp(T.QUOTS x,T.QUOTS y) = eq2(x,y)
244 : monnier 473 | eqRexp(T.REMS x,T.REMS y) = eq2(x,y)
245 :     | eqRexp(T.MULU x,T.MULU y) = eq2(x,y)
246 :     | eqRexp(T.DIVU x,T.DIVU y) = eq2(x,y)
247 :     | eqRexp(T.REMU x,T.REMU y) = eq2(x,y)
248 : george 545 | eqRexp(T.NEGT(t,x),T.NEGT(t',x')) = t = t' andalso eqRexp(x,x')
249 : monnier 473 | eqRexp(T.ADDT x,T.ADDT y) = eq2(x,y)
250 :     | eqRexp(T.SUBT x,T.SUBT y) = eq2(x,y)
251 :     | eqRexp(T.MULT x,T.MULT y) = eq2(x,y)
252 :     | eqRexp(T.DIVT x,T.DIVT y) = eq2(x,y)
253 : george 545 | eqRexp(T.QUOTT x,T.QUOTT y) = eq2(x,y)
254 : monnier 473 | eqRexp(T.REMT x,T.REMT y) = eq2(x,y)
255 :     | eqRexp(T.ANDB x,T.ANDB y) = eq2(x,y)
256 :     | eqRexp(T.ORB x,T.ORB y) = eq2(x,y)
257 :     | eqRexp(T.XORB x,T.XORB y) = eq2(x,y)
258 :     | eqRexp(T.NOTB(a,b),T.NOTB(c,d)) = a=c andalso eqRexp(b,d)
259 :     | eqRexp(T.SRA x,T.SRA y) = eq2(x,y)
260 :     | eqRexp(T.SRL x,T.SRL y) = eq2(x,y)
261 :     | eqRexp(T.SLL x,T.SLL y) = eq2(x,y)
262 :     | eqRexp(T.COND(a,b,c,d),T.COND(e,f,g,h)) =
263 :     a=e andalso eqCCexp(b,f) andalso eqRexp(c,g) andalso eqRexp(d,h)
264 : george 545 | eqRexp(T.CVTI2I(a,b,c,d),T.CVTI2I(a',b',c',d')) =
265 :     a=a' andalso b=b' andalso c=c' andalso eqRexp(d,d')
266 : monnier 473 | eqRexp(T.CVTF2I(a,b,c,d),T.CVTF2I(e,f,g,h)) =
267 :     a=e andalso b=f andalso c=g andalso eqFexp(d,h)
268 :     | eqRexp(T.LOAD(a,b,_),T.LOAD(c,d,_)) = a=c andalso eqRexp(b,d)
269 : george 545 | eqRexp(T.LET(a,b),T.LET(c,d)) = eqStm(a,c) andalso eqRexp(b,d)
270 : monnier 473 | eqRexp(T.MARK(a,_),b) = eqRexp(a,b)
271 :     | eqRexp(a,T.MARK(b,_)) = eqRexp(a,b)
272 : george 545 | eqRexp(T.PRED(a,b),T.PRED(a',b')) = b = b' andalso eqRexp(a,a')
273 :     | eqRexp(T.REXT(a,b),T.REXT(a',b')) =
274 :     a=a' andalso eqRext (equality()) (b,b')
275 : monnier 473 | eqRexp _ = false
276 :    
277 :     and eqRexps([],[]) = true
278 :     | eqRexps(a::b,c::d) = eqRexp(a,c) andalso eqRexps(b,d)
279 :     | eqRexps _ = false
280 :    
281 :     and eq2'((a,b,c),(d,e,f)) = a=d andalso eqFexp(b,e) andalso eqFexp(c,f)
282 :     and eq1'((a,b),(d,e)) = a=d andalso eqFexp(b,e)
283 :    
284 :     and eqFexp(T.FREG x,T.FREG y) = x = y
285 :     | eqFexp(T.FLOAD(a,b,_),T.FLOAD(c,d,_)) = a=c andalso eqRexp(b,d)
286 :     | eqFexp(T.FADD x,T.FADD y) = eq2'(x,y)
287 :     | eqFexp(T.FMUL x,T.FMUL y) = eq2'(x,y)
288 :     | eqFexp(T.FSUB x,T.FSUB y) = eq2'(x,y)
289 :     | eqFexp(T.FDIV x,T.FDIV y) = eq2'(x,y)
290 : george 545 | eqFexp(T.FCOPYSIGN x, T.FCOPYSIGN y) = eq2'(x,y)
291 :     | eqFexp(T.FCOND(t,x,y,z), T.FCOND(t',x',y',z')) =
292 :     t=t' andalso eqCCexp(x,x') andalso eqFexp(y,y') andalso eqFexp(z,z')
293 : monnier 473 | eqFexp(T.FABS x,T.FABS y) = eq1'(x,y)
294 :     | eqFexp(T.FNEG x,T.FNEG y) = eq1'(x,y)
295 :     | eqFexp(T.FSQRT x,T.FSQRT y) = eq1'(x,y)
296 : george 545 | eqFexp(T.CVTI2F(a,b,c),T.CVTI2F(a',b',c')) =
297 :     a=a' andalso b=b' andalso eqRexp(c,c')
298 :     | eqFexp(T.CVTF2F(a,b,c),T.CVTF2F(a',b',c')) =
299 :     a=a' andalso b=b' andalso eqFexp(c,c')
300 :     | eqFexp(T.FEXT(a,f),T.FEXT(b,g)) = a=b andalso eqFext (equality()) (f,g)
301 : monnier 473 | eqFexp(T.FMARK(a,_),b) = eqFexp(a,b)
302 :     | eqFexp(a,T.FMARK(b,_)) = eqFexp(a,b)
303 : george 545 | eqFexp(T.FPRED(a,b),T.FPRED(a',b')) = b = b' andalso eqFexp(a,a')
304 : monnier 473 | eqFexp _ = false
305 :    
306 :     and eqFexps([],[]) = true
307 :     | eqFexps(a::b,c::d) = eqFexp(a,c) andalso eqFexps(b,d)
308 :     | eqFexps _ = false
309 :    
310 :     and eqCCexp(T.CC x,T.CC y) = x=y
311 : george 545 | eqCCexp(T.FCC x,T.FCC y) = x=y
312 : monnier 473 | eqCCexp(T.CMP(x,a,b,c),T.CMP(y,d,e,f)) =
313 :     a=d andalso eqRexp(b,e) andalso eqRexp(c,f) andalso x = y
314 :     | eqCCexp(T.FCMP(x,a,b,c),T.FCMP(y,d,e,f)) =
315 :     a=d andalso eqFexp(b,e) andalso eqFexp(c,f) andalso x = y
316 : george 545 | eqCCexp(T.NOT x, T.NOT y) = eqCCexp(x,y)
317 :     | eqCCexp(T.AND x, T.AND y) = eqCCexp2(x,y)
318 :     | eqCCexp(T.OR x, T.OR y) = eqCCexp2(x,y)
319 :     | eqCCexp(T.XOR x, T.XOR y) = eqCCexp2(x,y)
320 : monnier 473 | eqCCexp(T.CCMARK(a,_),b) = eqCCexp(a,b)
321 :     | eqCCexp(a,T.CCMARK(b,_)) = eqCCexp(a,b)
322 : george 545 | eqCCexp(T.CCEXT(t,a),T.CCEXT(t',b)) =
323 :     t=t' andalso eqCCext (equality()) (a,b)
324 :     | eqCCexp(T.TRUE, T.TRUE) = true
325 :     | eqCCexp(T.FALSE, T.FALSE) = true
326 : monnier 473 | eqCCexp _ = false
327 :    
328 : george 545 and eqCCexp2((x,y),(x',y')) = eqCCexp(x,x') andalso eqCCexp(y,y')
329 :    
330 : monnier 473 and eqCCexps([],[]) = true
331 :     | eqCCexps(a::b,c::d) = eqCCexp(a,c) andalso eqCCexps(b,d)
332 :     | eqCCexps _ = false
333 : george 545
334 : monnier 473 (*
335 :     * Pretty printing
336 :     *)
337 : george 555 fun show (dst,src) =
338 : monnier 473 let fun ty t = "."^Int.toString t
339 :     fun fty 32 = ".s"
340 :     | fty 64 = ".d"
341 :     | fty 128 = ".q"
342 :     | fty t = ty t
343 :    
344 :     fun reg(t,v) = "r"^Int.toString v^ty t
345 :     fun freg(t,v) = "f"^Int.toString v^fty t
346 :     fun ccreg v = "cc"^Int.toString v
347 : george 545 fun ctrlreg v = "p"^Int.toString v
348 :    
349 : monnier 473 fun srcReg(t,v) = List.nth(src,v) handle _ => reg(t,v)
350 :     fun srcFreg(t,v) = List.nth(src,v) handle _ => freg(t,v)
351 :     fun srcCCreg v = List.nth(src,v) handle _ => ccreg v
352 : george 545 fun srcCtrlreg v = List.nth(src,v) handle _ => ctrlreg v
353 : monnier 473
354 :     fun dstReg(t,v) = List.nth(dst,v) handle _ => reg(t,v)
355 :     fun dstFreg(t,v) = List.nth(dst,v) handle _ => freg(t,v)
356 :     fun dstCCreg v = List.nth(dst,v) handle _ => ccreg v
357 : george 545 fun dstCtrlreg v = List.nth(dst,v) handle _ => ctrlreg v
358 : monnier 473
359 : george 545 fun listify f =
360 :     let fun g(t,[]) = ""
361 :     | g(t,[r]) = f(t,r)
362 :     | g(t,r::rs) = f(t,r)^","^g(t,rs)
363 :     in g end
364 : monnier 473
365 : george 545 fun listify' f =
366 :     let fun g([]) = ""
367 :     | g([r]) = f(r)
368 :     | g(r::rs) = f(r)^","^g(rs)
369 :     in g end
370 : monnier 473
371 : george 545 val srcRegs = listify srcReg
372 :     val dstRegs = listify dstReg
373 :     val srcFregs = listify srcFreg
374 :     val dstFregs = listify dstFreg
375 :     val srcCCregs = listify' srcCCreg
376 :     val dstCCregs = listify' dstCCreg
377 :     val srcCtrlregs = listify' srcCtrlreg
378 :     val dstCtrlregs = listify' dstCtrlreg
379 :     fun usectrl cr = " ["^srcCtrlreg cr^"]"
380 :     fun usectrls [] = ""
381 :     | usectrls cr = " ["^srcCtrlregs cr^"]"
382 :     fun defctrl cr = ""^dstCtrlreg cr^" <- "
383 :     fun defctrls [] = ""
384 :     | defctrls cr = ""^dstCtrlregs cr^" <- "
385 : monnier 473
386 : george 545 fun copy(t,dst,src) = dstRegs(t, dst)^" := "^srcRegs(t, src)
387 : monnier 473 fun fcopy(t,dst,src) = dstFregs(t, dst)^" := "^srcFregs(t, src)
388 :    
389 : george 545 fun shower() = {stm=stm, rexp=rexp, fexp=fexp, ccexp=ccexp,
390 : george 555 dstReg=dstReg, srcReg=srcReg}
391 : monnier 473 (* pretty print a statement *)
392 : george 545 and stm(T.MV(t,dst,e)) = dstReg(t,dst)^" := "^rexp e
393 : monnier 473 | stm(T.CCMV(dst,e)) = dstCCreg dst^" := "^ccexp e
394 :     | stm(T.FMV(fty,dst,e)) = dstFreg(fty,dst)^" := "^fexp e
395 :     | stm(T.COPY(ty,dst,src)) = copy(ty,dst,src)
396 :     | stm(T.FCOPY(fty,dst,src)) = fcopy(fty,dst,src)
397 : george 545 | stm(T.JMP(cr,ea,labels)) = defctrls cr^"jmp "^rexp ea
398 :     | stm(T.CALL(ea,flow,defs,uses,cdef,cuse,mem)) =
399 :     defctrls cdef^"call "^rexp ea^usectrls cuse
400 :     | stm(T.RET(cr,flow)) = defctrls cr^"ret"
401 : monnier 473 | stm(T.STORE(ty,ea,e,mem)) = store(ty,"",ea,mem,e)
402 :     | stm(T.FSTORE(fty,ea,e,mem)) = fstore(fty,"",ea,mem,e)
403 : george 545 | stm(T.BCC(cr,a,lab)) =
404 :     defctrls cr^"bcc "^ccexp a^" "^Label.nameOf lab
405 :     | stm(T.IF(cr,a,b,c)) =
406 :     defctrls cr^"if "^ccexp a^" then "^stm b^" else "^stm c
407 :     | stm(T.SEQ []) = "skip"
408 :     | stm(T.SEQ s) = stms(";",s)
409 :     | stm(T.REGION(s,cr)) = stm s^usectrl cr
410 : monnier 473 | stm(T.ANNOTATION(s, a)) = stm s
411 : george 545 | stm(T.PHI b) = "phi["^Int.toString b^"]"
412 :     | stm(T.PINNED s) = "pinned "^stm s
413 :     | stm(T.RTL{e,...}) = stm e
414 : george 555 | stm(T.EXT x) = showSext (shower()) x
415 : george 545 | stm _ = error "stm"
416 : monnier 473
417 :     and stms(sep,[]) = ""
418 :     | stms(sep,[s]) = stm s
419 :     | stms(sep,s::ss) = stm s^sep^stms(sep,ss)
420 :    
421 :     (* pretty print an expression *)
422 :     and rexp(T.REG(ty, src)) = srcReg(ty,src)
423 : george 545 | rexp(T.LI i) = Int.toString i
424 : monnier 473 | rexp(T.LI32 w) = Word32.toString w
425 :     | rexp(T.LI64 w) = Word64.toString w
426 :     | rexp(T.LABEL le) = LabelExp.toString le
427 :     | rexp(T.CONST c) = Constant.toString c
428 : george 545 | rexp(T.NEG x) = one("neg",x)
429 : monnier 473 | rexp(T.ADD x) = two("add",x)
430 :     | rexp(T.SUB x) = two("sub",x)
431 :     | rexp(T.MULS x) = two("muls",x)
432 :     | rexp(T.DIVS x) = two("divs",x)
433 : george 545 | rexp(T.QUOTS x) = two("quots",x)
434 : monnier 473 | rexp(T.REMS x) = two("rems",x)
435 :     | rexp(T.MULU x) = two("mulu",x)
436 :     | rexp(T.DIVU x) = two("divu",x)
437 :     | rexp(T.REMU x) = two("remu",x)
438 : george 545 | rexp(T.NEGT x) = one("negt",x)
439 : monnier 473 | rexp(T.ADDT x) = two("addt",x)
440 :     | rexp(T.SUBT x) = two("subt",x)
441 :     | rexp(T.MULT x) = two("mult",x)
442 :     | rexp(T.DIVT x) = two("divt",x)
443 : george 545 | rexp(T.QUOTT x) = two("quott",x)
444 : monnier 473 | rexp(T.REMT x) = two("remt",x)
445 :     | rexp(T.ANDB x) = two("andb",x)
446 :     | rexp(T.ORB x) = two("orb",x)
447 :     | rexp(T.XORB x) = two("xorb",x)
448 :     | rexp(T.NOTB x) = one("notb",x)
449 :     | rexp(T.SRA x) = two("sra",x)
450 :     | rexp(T.SRL x) = two("srl",x)
451 :     | rexp(T.SLL x) = two("sll",x)
452 :     | rexp(T.COND(t,cc,e1,e2)) =
453 :     "cond"^ty t^ccexp cc^"("^rexp e1^","^rexp e2^")"
454 : george 545 | rexp(T.CVTI2I(t, T.SIGN_EXTEND, t', e)) = "sx"^ty t^ty t'^" "^rexp e
455 :     | rexp(T.CVTI2I(t, T.ZERO_EXTEND, t', e)) = "zx"^ty t^ty t'^" "^rexp e
456 : monnier 473 | rexp(T.CVTF2I(t, round, t', e)) =
457 : george 545 "cvtf2i"^ty t^B.roundingModeToString round^fty t'^" "^fexp e
458 : monnier 473 | rexp(T.LOAD(ty, ea, mem)) = load(ty,"",ea,mem)
459 : george 545 | rexp(T.LET(s, e)) = stm s^";"^rexp e
460 :     | rexp(T.PRED(e, cr)) = rexp e^usectrl cr
461 : monnier 473 | rexp(T.MARK(e, _)) = rexp e
462 : george 555 | rexp(T.REXT e) = showRext (shower()) e
463 : monnier 473
464 : george 545 and slices sc = listify' (fn {from,to} => rexp from^".."^rexp to) sc
465 :    
466 : monnier 473 (* pretty print a real expression *)
467 :     and fexp(T.FREG f) = srcFreg f
468 :     | fexp(T.FLOAD(fty, ea, mem)) = fload(fty,"",ea,mem)
469 :     | fexp(T.FADD x) = two'("fadd",x)
470 :     | fexp(T.FMUL x) = two'("fmul",x)
471 :     | fexp(T.FSUB x) = two'("fsub",x)
472 :     | fexp(T.FDIV x) = two'("fdiv",x)
473 : george 545 | fexp(T.FCOPYSIGN x) = two'("fcopysign",x)
474 : monnier 473 | fexp(T.FABS x) = one'("fabs",x)
475 :     | fexp(T.FNEG x) = one'("fneg",x)
476 :     | fexp(T.FSQRT x) = one'("fsqrt",x)
477 : george 545 | fexp(T.FCOND(t,cc,e1,e2)) =
478 :     "fcond"^fty t^ccexp cc^"("^fexp e1^","^fexp e2^")"
479 :     | fexp(T.CVTI2F(t, t', e)) = "cvti2f"^ty t'^" "^rexp e
480 :     | fexp(T.CVTF2F(t, t', e)) = "cvtf2f"^fty t^fty t'^" "^fexp e
481 :     | fexp(T.FPRED(e, cr)) = fexp e^usectrl cr
482 : monnier 473 | fexp(T.FMARK(e, _)) = fexp e
483 : george 555 | fexp(T.FEXT e) = showFext (shower()) e
484 : monnier 473
485 : george 545 and ccexp(T.CC(cc,r)) = srcCCreg r^B.condToString cc
486 :     | ccexp(T.FCC(fcc,r)) = srcCCreg r^B.fcondToString fcc
487 :     | ccexp(T.CMP(t,cc,x,y)) = "cmp"^B.condToString cc^ty t^pair(x,y)
488 :     | ccexp(T.FCMP(t,fcc,x,y)) = "fcmp"^B.fcondToString fcc^fty t^pair'(x,y)
489 :     | ccexp(T.NOT x) = "not "^ccexp x
490 :     | ccexp(T.AND(x,y)) = two''(" and ",x,y)
491 :     | ccexp(T.OR(x,y)) = two''(" or ",x,y)
492 :     | ccexp(T.XOR(x,y)) = two''(" xor ",x,y)
493 : monnier 473 | ccexp(T.CCMARK(e, _)) = ccexp e
494 : george 545 | ccexp(T.TRUE) = "true"
495 :     | ccexp(T.FALSE) = "false"
496 : george 555 | ccexp(T.CCEXT(e)) = showCCext (shower()) e
497 : monnier 473
498 : george 545 and mlrisc(T.GPR e) = rexp e
499 :     | mlrisc(T.FPR e) = fexp e
500 :     | mlrisc(T.CCR e) = ccexp e
501 :    
502 :     and mlriscs l = listify' mlrisc l
503 :    
504 : monnier 473 (* Auxiliary functions *)
505 :     and one(opcode,(t,x)) = opcode^ty t^"("^rexp x^")"
506 :     and two(opcode,(t,x,y)) = opcode^ty t^pair(x,y)
507 :     and pair(x,y) = "("^rexp x^","^rexp y^")"
508 :     and one'(opcode,(t,x)) = opcode^fty t^"("^fexp x^")"
509 :     and two'(opcode,(t,x,y)) = opcode^fty t^pair'(x,y)
510 : george 545 and two''(c,x,y) = "("^ccexp x^ c ^ ccexp y^")"
511 : monnier 473 and pair'(x,y) = "("^fexp x^","^fexp y^")"
512 :     and rexps es = "("^foldr (fn (e,"") => rexp e
513 :     | (e,x) => rexp e^","^x) "" es^")"
514 :     and fexps es = "("^foldr (fn (e,"") => fexp e
515 :     | (e,x) => fexp e^","^x) "" es^")"
516 :     and ccexps es = "("^foldr (fn (e,"") => ccexp e
517 :     | (e,x) => ccexp e^","^x) "" es^")"
518 :     and store(t,u,ea,m,e) = mem(t,u,ea,m)^" := "^rexp e
519 :     and fstore(t,u,ea,m,e) = fmem(t,u,ea,m)^" := "^fexp e
520 :     and ccstore(u,ea,m,e) = ccmem(u,ea,m)^" := "^ccexp e
521 :     and load(t,u,ea,m) = mem(t,u,ea,m)
522 :     and fload(t,u,ea,m) = fmem(t,u,ea,m)
523 :     and ccload(u,ea,m) = ccmem(u,ea,m)
524 : george 545 and addr(u,ea,m) =
525 :     let val r = Region.toString m
526 :     val r = if r = "" then r else ","^r
527 :     in u^"["^rexp ea^r^"]" end
528 : monnier 473 and mem(t,u,ea,m) = "mem"^ty t^addr(u,ea,m)
529 :     and fmem(t,u,ea,m) = "mem"^fty t^addr(u,ea,m)
530 :     and ccmem(u,ea,m) = "mem"^addr(u,ea,m)
531 :    
532 : george 545 in shower()
533 : monnier 473 end
534 :    
535 : george 555 fun stmToString s = #stm(show ([],[])) s
536 :     fun rexpToString s = #rexp(show ([],[])) s
537 :     fun fexpToString s = #fexp(show ([],[])) s
538 :     fun ccexpToString s = #ccexp(show ([],[])) s
539 : monnier 473
540 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0