Debian Codegraph @

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

raw
1 (* labelExp.sml -- expressions involving labels
2 *
3 * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
4 *
5 *)
6
7 functor LabelExp
8 (structure T : MLTREE
9 (* Hashing extensions *)
10 val hashSext : T.hasher -> T.sext -> word
11 val hashRext : T.hasher -> T.rext -> word
12 val hashFext : T.hasher -> T.fext -> word
13 val hashCCext : T.hasher -> T.ccext -> word
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 (* assembly output *)
20 val labelFmt : {gPrefix : string, aPrefix: string}
21 ) : LABELEXP =
22 struct
23
24 structure T = T
25 structure I = T.I
26 structure Constant = T.Constant
27 structure B = T.Basis
28 structure C = CellsBasis
29 structure W = Word
30
31 val w = W.fromInt
32 val i2s = Int.toString
33 val toLower = String.map Char.toLower
34
35 fun error msg = MLRiscErrorMsg.error("LabelExp",msg)
36 fun wv(C.CELL{id, ...}) = w id
37 fun wvs is =
38 let fun f([],h) = h
39 | f(i::is,h) = f(is,wv i+h)
40 in f(is,0w0) end
41
42 (*
43 * Hashing
44 *)
45 val hashLabel = Label.hash
46 fun hasher() = {stm=hashStm, rexp=hashRexp, fexp=hashFexp, ccexp=hashCCexp}
47 and hashCtrl ctrl = wv ctrl
48 and hashStm stm =
49 case stm of
50 T.MV(t,dst,rexp) => 0w123 + w t + wv dst + hashRexp rexp
51 | T.CCMV(dst,ccexp) => 0w1234 + wv dst + hashCCexp ccexp
52 | T.FMV(fty,dst,fexp) => 0w12345 + w fty + wv dst + hashFexp fexp
53 | T.COPY(ty,dst,src) => 0w234 + w ty + wvs dst + wvs src
54 | T.FCOPY(fty,dst,src) => 0w456 + w fty + wvs dst + wvs src
55 | T.JMP(ea,labels) => 0w45 + hashRexp ea
56 | T.CALL{funct,targets,defs,uses,region,pops} =>
57 hashRexp funct + hashMlriscs defs + hashMlriscs uses
58 | T.RET _ => 0w567
59 | T.STORE(ty,ea,data,mem) => 0w888 + w ty + hashRexp ea + hashRexp data
60 | T.FSTORE(fty,ea,data,mem) => 0w7890 + w fty + hashRexp ea + hashFexp data
61 | T.BCC(a,lab) => 0w233 + hashCCexp a + hashLabel lab
62 | T.IF(a,b,c) => 0w233 + hashCCexp a + hashStm b + hashStm c
63 | T.ANNOTATION(stm, a) => hashStm stm
64 | T.PHI{preds,block} => w block
65 | T.SOURCE => 0w123
66 | T.SINK => 0w423
67 | T.REGION(stm,ctrl) => hashStm stm + hashCtrl ctrl
68 | T.RTL{hash,...} => hash
69 | T.SEQ ss => hashStms(ss, 0w23)
70 | T.ASSIGN(ty,lhs,rhs) => w ty + hashRexp lhs + hashRexp rhs
71 | _ => error "hashStm"
72
73 and hashStms([],h) = h
74 | hashStms(s::ss,h) = hashStms(ss,hashStm s + h)
75
76 and hashMlrisc(T.CCR ccexp) = hashCCexp ccexp
77 | hashMlrisc(T.GPR rexp) = hashRexp rexp
78 | hashMlrisc(T.FPR fexp) = hashFexp fexp
79
80 and hashMlriscs [] = 0w123
81 | hashMlriscs(m::ms) = hashMlrisc m + hashMlriscs ms
82
83 and hash2(ty,x,y) = w ty + hashRexp x + hashRexp y
84
85 and hashRexp rexp =
86 case rexp of
87 T.REG(ty, src) => w ty + wv src
88 | T.LI i => I.hash i
89 | T.LABEL l => hashLabel l
90 | T.LABEXP le => hashRexp rexp
91 | T.CONST c => Constant.hash c
92 | T.NEG(ty, x) => w ty + hashRexp x + 0w24
93 | T.ADD x => hash2 x + 0w234
94 | T.SUB x => hash2 x + 0w456
95 | T.MULS x => hash2 x + 0w2131
96 | T.DIVS x => hash2 x + 0w156
97 | T.QUOTS x => hash2 x + 0w1565
98 | T.REMS x => hash2 x + 0w231
99 | T.MULU x => hash2 x + 0w123
100 | T.DIVU x => hash2 x + 0w1234
101 | T.REMU x => hash2 x + 0w211
102 | T.NEGT(ty, x) => w ty + hashRexp x + 0w1224
103 | T.ADDT x => hash2 x + 0w1219
104 | T.SUBT x => hash2 x + 0w999
105 | T.MULT x => hash2 x + 0w7887
106 | T.DIVT x => hash2 x + 0w88884
107 | T.QUOTT x => hash2 x + 0w8884
108 | T.REMT x => hash2 x + 0w99
109 | T.ANDB x => hash2 x + 0w12312
110 | T.ORB x => hash2 x + 0w558
111 | T.XORB x => hash2 x + 0w234
112 | T.EQVB x => hash2 x + 0w734
113 | T.NOTB(ty, x) => w ty + hashRexp x
114 | T.SRA x => hash2 x + 0w874
115 | T.SRL x => hash2 x + 0w223
116 | T.SLL x => hash2 x + 0w499
117 | T.COND(ty,e,e1,e2) => w ty + hashCCexp e + hashRexp e1 + hashRexp e2
118 | T.SX(ty, ty', rexp) => 0w232 + w ty + w ty' + hashRexp rexp
119 | T.ZX(ty, ty', rexp) => 0w737 + w ty + w ty' + hashRexp rexp
120 | T.CVTF2I(ty, round, ty', fexp) =>
121 w ty + B.hashRoundingMode round + w ty' + hashFexp fexp
122 | T.LOAD(ty, ea, mem) => w ty + hashRexp ea + 0w342
123 | T.LET(stm, rexp) => hashStm stm + hashRexp rexp
124 | T.PRED(e, ctrl) => hashRexp e + hashCtrl ctrl
125 | T.MARK(e, _) => hashRexp e
126 | T.REXT(ty, rext) => w ty + hashRext (hasher()) rext
127 | T.??? => 0w485
128 | T.OP(ty,oper,es) => hashRexps(es, w ty + hashOper oper)
129 | T.ARG _ => 0w23
130 | T.$(ty, k, e) => w ty + hashRexp e
131 | T.PARAM n => w n
132 | T.BITSLICE(ty, sl, e) => w ty + hashRexp e
133
134 and hashOper(T.OPER{hash, ...}) = hash
135
136 and hashRexps([],h) = h
137 | hashRexps(e::es,h) = hashRexps(es,hashRexp e + h)
138
139 and hash2'(ty,x,y) = w ty + hashFexp x + hashFexp y
140
141 and hashFexp fexp =
142 case fexp of
143 T.FREG(fty, src) => w fty + wv src
144 | T.FLOAD(fty, ea, mem) => w fty + hashRexp ea
145 | T.FADD x => hash2' x + 0w123
146 | T.FMUL x => hash2' x + 0w1234
147 | T.FSUB x => hash2' x + 0w12345
148 | T.FDIV x => hash2' x + 0w234
149 | T.FCOPYSIGN x => hash2' x + 0w883
150 | T.FCOND(fty,c,x,y) => w fty + hashCCexp c + hashFexp x + hashFexp y
151 | T.FABS(fty, fexp) => w fty + hashFexp fexp + 0w2345
152 | T.FNEG(fty, fexp) => w fty + hashFexp fexp + 0w23456
153 | T.FSQRT(fty, fexp) => w fty + hashFexp fexp + 0w345
154 | T.CVTI2F(fty, ty, rexp) => w fty + w ty + hashRexp rexp
155 | T.CVTF2F(fty, fty', fexp) => w fty + hashFexp fexp + w fty'
156 | T.FMARK(e, _) => hashFexp e
157 | T.FPRED(e, ctrl) => hashFexp e + hashCtrl ctrl
158 | T.FEXT(fty, fext) => w fty + hashFext (hasher()) fext
159
160 and hashFexps([],h) = h
161 | hashFexps(e::es,h) = hashFexps(es,hashFexp e + h)
162
163 and hashCCexp ccexp =
164 case ccexp of
165 T.CC(cc, src) => B.hashCond cc + wv src
166 | T.FCC(fcc, src) => B.hashFcond fcc + wv src
167 | T.CMP(ty, cond, x, y) =>
168 w ty + B.hashCond cond + hashRexp x + hashRexp y
169 | T.FCMP(fty, fcond, x, y) =>
170 w fty + B.hashFcond fcond + hashFexp x + hashFexp y
171 | T.NOT x => 0w2321 + hashCCexp x
172 | T.AND(x,y) => 0w2321 + hashCCexp x + hashCCexp y
173 | T.OR(x,y) => 0w8721 + hashCCexp x + hashCCexp y
174 | T.XOR(x,y) => 0w6178 + hashCCexp x + hashCCexp y
175 | T.EQV(x,y) => 0w178 + hashCCexp x + hashCCexp y
176 | T.TRUE => 0w0
177 | T.FALSE => 0w1232
178 | T.CCMARK(e, _) => hashCCexp e
179 | T.CCEXT(ty,ccext) => w ty + hashCCext (hasher()) ccext
180
181 and hashCCexps([],h) = h
182 | hashCCexps(e::es,h) = hashCCexps(es,hashCCexp e + h)
183
184 val eqLabel = Label.same
185 fun eqLabels([],[]) = true
186 | eqLabels(a::b,c::d) = eqLabel(a,c) andalso eqLabels(b,d)
187 | eqLabels _ = false
188 and eqCell(C.CELL{id=x, ...},C.CELL{id=y, ...}) = x=y
189 and eqCells([], []) = true
190 | eqCells(x::xs,y::ys) = eqCell(x,y) andalso eqCells(xs,ys)
191 | eqCells _ = false
192 and eqCopy((t1,dst1,src1),(t2,dst2,src2)) =
193 t1=t2 andalso eqCells(dst1,dst2) andalso eqCells(src1,src2)
194 and eqCtrl(c1,c2) = eqCell(c1,c2)
195 and eqCtrls(c1,c2) = eqCells(c1,c2)
196
197 (* statements *)
198 and equality() = {stm=eqStm, rexp=eqRexp, fexp=eqFexp, ccexp=eqCCexp}
199 and eqStm(T.MV(a,b,c),T.MV(d,e,f)) =
200 a=d andalso eqCell(b,e) andalso eqRexp(c,f)
201 | eqStm(T.CCMV(a,b),T.CCMV(c,d)) = eqCell(a,c) andalso eqCCexp(b,d)
202 | eqStm(T.FMV(a,b,c),T.FMV(d,e,f)) =
203 a=d andalso eqCell(b,e) andalso eqFexp(c,f)
204 | eqStm(T.COPY x,T.COPY y) = eqCopy(x,y)
205 | eqStm(T.FCOPY x,T.FCOPY y) = eqCopy(x,y)
206 | eqStm(T.JMP(a,b),T.JMP(a',b')) = eqRexp(a,a')
207 | eqStm(T.CALL{funct=a,defs=b,uses=c,...},
208 T.CALL{funct=d,defs=e,uses=f,...}) =
209 eqRexp(a,d) andalso eqMlriscs(b,e) andalso eqMlriscs(c,f)
210 | eqStm(T.RET _,T.RET _) = true
211 | eqStm(T.STORE(a,b,c,_),T.STORE(d,e,f,_)) =
212 a=d andalso eqRexp(b,e) andalso eqRexp(c,f)
213 | eqStm(T.FSTORE(a,b,c,_),T.FSTORE(d,e,f,_)) =
214 a=d andalso eqRexp(b,e) andalso eqFexp(c,f)
215 | eqStm(T.ANNOTATION(s1, _),s2) = eqStm(s1,s2)
216 | eqStm(s1,T.ANNOTATION(s2, _)) = eqStm(s1,s2)
217 | eqStm(T.PHI x,T.PHI y) = x=y
218 | eqStm(T.SOURCE,T.SOURCE) = true
219 | eqStm(T.SINK,T.SINK) = true
220 | eqStm(T.BCC(b,c),T.BCC(b',c')) =
221 eqCCexp(b,b') andalso eqLabel(c,c')
222 | eqStm(T.IF(b,c,d),T.IF(b',c',d')) =
223 eqCCexp(b,b') andalso eqStm(c,c') andalso eqStm(d,d')
224 | eqStm(T.RTL{attribs=x,...},T.RTL{attribs=y,...}) = x=y
225 | eqStm(T.REGION(a,b),T.REGION(a',b')) = eqCtrl(b,b') andalso eqStm(a,a')
226 | eqStm(T.EXT a,T.EXT a') = eqSext (equality()) (a,a')
227 | eqStm _ = false
228
229 and eqStms([],[]) = true
230 | eqStms(a::b,c::d) = eqStm(a,c) andalso eqStms(b,d)
231 | eqStms _ = false
232
233 and eqMlrisc(T.CCR a,T.CCR b) = eqCCexp(a,b)
234 | eqMlrisc(T.GPR a,T.GPR b) = eqRexp(a,b)
235 | eqMlrisc(T.FPR a,T.FPR b) = eqFexp(a,b)
236 | eqMlrisc _ = false
237
238 and eqMlriscs([],[]) = true
239 | eqMlriscs(a::b,c::d) = eqMlrisc(a,c) andalso eqMlriscs(b,d)
240 | eqMlriscs _ = false
241
242 and eq2((a,b,c),(d,e,f)) = a=d andalso eqRexp(b,e) andalso eqRexp(c,f)
243
244 and eqRexp(T.REG(a,b),T.REG(c,d)) = a=c andalso eqCell(b,d)
245 | eqRexp(T.LI a,T.LI b) = a=b
246 | eqRexp(T.LABEL a,T.LABEL b) = eqLabel(a,b)
247 | eqRexp(T.LABEXP a,T.LABEXP b) = eqRexp(a,b)
248 | eqRexp(T.CONST a,T.CONST b) = Constant.==(a,b)
249 | eqRexp(T.NEG(t,x),T.NEG(t',x')) = t = t' andalso eqRexp(x,x')
250 | eqRexp(T.ADD x,T.ADD y) = eq2(x,y)
251 | eqRexp(T.SUB x,T.SUB y) = eq2(x,y)
252 | eqRexp(T.MULS x,T.MULS y) = eq2(x,y)
253 | eqRexp(T.DIVS x,T.DIVS y) = eq2(x,y)
254 | eqRexp(T.QUOTS x,T.QUOTS y) = eq2(x,y)
255 | eqRexp(T.REMS x,T.REMS y) = eq2(x,y)
256 | eqRexp(T.MULU x,T.MULU y) = eq2(x,y)
257 | eqRexp(T.DIVU x,T.DIVU y) = eq2(x,y)
258 | eqRexp(T.REMU x,T.REMU y) = eq2(x,y)
259 | eqRexp(T.NEGT(t,x),T.NEGT(t',x')) = t = t' andalso eqRexp(x,x')
260 | eqRexp(T.ADDT x,T.ADDT y) = eq2(x,y)
261 | eqRexp(T.SUBT x,T.SUBT y) = eq2(x,y)
262 | eqRexp(T.MULT x,T.MULT y) = eq2(x,y)
263 | eqRexp(T.DIVT x,T.DIVT y) = eq2(x,y)
264 | eqRexp(T.QUOTT x,T.QUOTT y) = eq2(x,y)
265 | eqRexp(T.REMT x,T.REMT y) = eq2(x,y)
266 | eqRexp(T.ANDB x,T.ANDB y) = eq2(x,y)
267 | eqRexp(T.ORB x,T.ORB y) = eq2(x,y)
268 | eqRexp(T.XORB x,T.XORB y) = eq2(x,y)
269 | eqRexp(T.EQVB x,T.EQVB y) = eq2(x,y)
270 | eqRexp(T.NOTB(a,b),T.NOTB(c,d)) = a=c andalso eqRexp(b,d)
271 | eqRexp(T.SRA x,T.SRA y) = eq2(x,y)
272 | eqRexp(T.SRL x,T.SRL y) = eq2(x,y)
273 | eqRexp(T.SLL x,T.SLL y) = eq2(x,y)
274 | eqRexp(T.COND(a,b,c,d),T.COND(e,f,g,h)) =
275 a=e andalso eqCCexp(b,f) andalso eqRexp(c,g) andalso eqRexp(d,h)
276 | eqRexp(T.SX(a,b,c),T.SX(a',b',c')) =
277 a=a' andalso b=b' andalso eqRexp(c,c')
278 | eqRexp(T.ZX(a,b,c),T.ZX(a',b',c')) =
279 a=a' andalso b=b' andalso eqRexp(c,c')
280 | eqRexp(T.CVTF2I(a,b,c,d),T.CVTF2I(e,f,g,h)) =
281 a=e andalso b=f andalso c=g andalso eqFexp(d,h)
282 | eqRexp(T.LOAD(a,b,_),T.LOAD(c,d,_)) = a=c andalso eqRexp(b,d)
283 | eqRexp(T.LET(a,b),T.LET(c,d)) = eqStm(a,c) andalso eqRexp(b,d)
284 | eqRexp(T.ARG x,T.ARG y) = x = y
285 | eqRexp(T.PARAM x,T.PARAM y) = x = y
286 | eqRexp(T.???,T.???) = true
287 | eqRexp(T.$(t1,k1,e1),T.$(t2,k2,e2)) =
288 t1=t2 andalso k1=k2 andalso eqRexp(e1,e2)
289 | eqRexp(T.BITSLICE(t1,s1,e1),T.BITSLICE(t2,s2,e2)) =
290 t1=t2 andalso s1=s2 andalso eqRexp(e1,e2)
291 | eqRexp(T.MARK(a,_),b) = eqRexp(a,b)
292 | eqRexp(a,T.MARK(b,_)) = eqRexp(a,b)
293 | eqRexp(T.PRED(a,b),T.PRED(a',b')) = eqCtrl(b,b') andalso eqRexp(a,a')
294 | eqRexp(T.REXT(a,b),T.REXT(a',b')) =
295 a=a' andalso eqRext (equality()) (b,b')
296 | eqRexp _ = false
297
298 and eqRexps([],[]) = true
299 | eqRexps(a::b,c::d) = eqRexp(a,c) andalso eqRexps(b,d)
300 | eqRexps _ = false
301
302 and eq2'((a,b,c),(d,e,f)) = a=d andalso eqFexp(b,e) andalso eqFexp(c,f)
303 and eq1'((a,b),(d,e)) = a=d andalso eqFexp(b,e)
304
305 and eqFexp(T.FREG(t1,x),T.FREG(t2,y)) = t1=t2 andalso eqCell(x,y)
306 | eqFexp(T.FLOAD(a,b,_),T.FLOAD(c,d,_)) = a=c andalso eqRexp(b,d)
307 | eqFexp(T.FADD x,T.FADD y) = eq2'(x,y)
308 | eqFexp(T.FMUL x,T.FMUL y) = eq2'(x,y)
309 | eqFexp(T.FSUB x,T.FSUB y) = eq2'(x,y)
310 | eqFexp(T.FDIV x,T.FDIV y) = eq2'(x,y)
311 | eqFexp(T.FCOPYSIGN x, T.FCOPYSIGN y) = eq2'(x,y)
312 | eqFexp(T.FCOND(t,x,y,z), T.FCOND(t',x',y',z')) =
313 t=t' andalso eqCCexp(x,x') andalso eqFexp(y,y') andalso eqFexp(z,z')
314 | eqFexp(T.FABS x,T.FABS y) = eq1'(x,y)
315 | eqFexp(T.FNEG x,T.FNEG y) = eq1'(x,y)
316 | eqFexp(T.FSQRT x,T.FSQRT y) = eq1'(x,y)
317 | eqFexp(T.CVTI2F(a,b,c),T.CVTI2F(a',b',c')) =
318 a=a' andalso b=b' andalso eqRexp(c,c')
319 | eqFexp(T.CVTF2F(a,b,c),T.CVTF2F(a',b',c')) =
320 a=a' andalso b=b' andalso eqFexp(c,c')
321 | eqFexp(T.FEXT(a,f),T.FEXT(b,g)) = a=b andalso eqFext (equality()) (f,g)
322 | eqFexp(T.FMARK(a,_),b) = eqFexp(a,b)
323 | eqFexp(a,T.FMARK(b,_)) = eqFexp(a,b)
324 | eqFexp(T.FPRED(a,b),T.FPRED(a',b')) = eqCtrl(b,b') andalso eqFexp(a,a')
325 | eqFexp _ = false
326
327 and eqFexps([],[]) = true
328 | eqFexps(a::b,c::d) = eqFexp(a,c) andalso eqFexps(b,d)
329 | eqFexps _ = false
330
331 and eqCCexp(T.CC(c1,x),T.CC(c2,y)) = c1=c2 andalso eqCell(x,y)
332 | eqCCexp(T.FCC(c1,x),T.FCC(c2,y)) = c1=c2 andalso eqCell(x,y)
333 | eqCCexp(T.CMP(x,a,b,c),T.CMP(y,d,e,f)) =
334 a=d andalso eqRexp(b,e) andalso eqRexp(c,f) andalso x = y
335 | eqCCexp(T.FCMP(x,a,b,c),T.FCMP(y,d,e,f)) =
336 a=d andalso eqFexp(b,e) andalso eqFexp(c,f) andalso x = y
337 | eqCCexp(T.NOT x, T.NOT y) = eqCCexp(x,y)
338 | eqCCexp(T.AND x, T.AND y) = eqCCexp2(x,y)
339 | eqCCexp(T.OR x, T.OR y) = eqCCexp2(x,y)
340 | eqCCexp(T.XOR x, T.XOR y) = eqCCexp2(x,y)
341 | eqCCexp(T.EQV x, T.EQV y) = eqCCexp2(x,y)
342 | eqCCexp(T.CCMARK(a,_),b) = eqCCexp(a,b)
343 | eqCCexp(a,T.CCMARK(b,_)) = eqCCexp(a,b)
344 | eqCCexp(T.CCEXT(t,a),T.CCEXT(t',b)) =
345 t=t' andalso eqCCext (equality()) (a,b)
346 | eqCCexp(T.TRUE, T.TRUE) = true
347 | eqCCexp(T.FALSE, T.FALSE) = true
348 | eqCCexp _ = false
349
350 and eqCCexp2((x,y),(x',y')) = eqCCexp(x,x') andalso eqCCexp(y,y')
351
352 and eqCCexps([],[]) = true
353 | eqCCexps(a::b,c::d) = eqCCexp(a,c) andalso eqCCexps(b,d)
354 | eqCCexps _ = false
355
356 exception NonConst
357
358 fun eval{label, const} =
359 let fun rexp(T.LI i) = i
360 | rexp(T.CONST c) = const c
361 | rexp(T.LABEL l) = IntInf.fromInt(label l)
362 | rexp(T.LABEXP e) = rexp e
363
364 | rexp(T.NEG(sz,x)) = I.NEG(sz,rexp x)
365 | rexp(T.ADD(sz,x,y)) = I.ADD(sz,rexp x,rexp y)
366 | rexp(T.SUB(sz,x,y)) = I.SUB(sz,rexp x,rexp y)
367
368 | rexp(T.MULS(sz,x,y)) = I.MULS(sz,rexp x,rexp y)
369 | rexp(T.DIVS(sz,x,y)) = I.DIVS(sz,rexp x,rexp y)
370 | rexp(T.QUOTS(sz,x,y)) = I.QUOTS(sz,rexp x,rexp y)
371 | rexp(T.REMS(sz,x,y)) = I.REMS(sz,rexp x,rexp y)
372
373 | rexp(T.MULU(sz,x,y)) = I.MULU(sz,rexp x,rexp y)
374 | rexp(T.DIVU(sz,x,y)) = I.DIVU(sz,rexp x,rexp y)
375 | rexp(T.REMU(sz,x,y)) = I.REMU(sz,rexp x,rexp y)
376
377 | rexp(T.NEGT(sz,x)) = I.NEGT(sz,rexp x)
378 | rexp(T.ADDT(sz,x,y)) = I.ADDT(sz,rexp x,rexp y)
379 | rexp(T.SUBT(sz,x,y)) = I.SUBT(sz,rexp x,rexp y)
380 | rexp(T.MULT(sz,x,y)) = I.MULT(sz,rexp x,rexp y)
381 | rexp(T.DIVT(sz,x,y)) = I.DIVT(sz,rexp x,rexp y)
382 | rexp(T.QUOTT(sz,x,y)) = I.QUOTT(sz,rexp x,rexp y)
383 | rexp(T.REMT(sz,x,y)) = I.REMT(sz,rexp x,rexp y)
384
385 | rexp(T.NOTB(sz,x)) = I.NOTB(sz,rexp x)
386 | rexp(T.ANDB(sz,x,y)) = I.ANDB(sz,rexp x,rexp y)
387 | rexp(T.ORB(sz,x,y)) = I.ORB(sz,rexp x,rexp y)
388 | rexp(T.XORB(sz,x,y)) = I.XORB(sz,rexp x,rexp y)
389 | rexp(T.EQVB(sz,x,y)) = I.EQVB(sz,rexp x,rexp y)
390 | rexp(T.SLL(sz,x,y)) = I.SLL(sz,rexp x,rexp y)
391 | rexp(T.SRL(sz,x,y)) = I.SRL(sz,rexp x,rexp y)
392 | rexp(T.SRA(sz,x,y)) = I.SRA(sz,rexp x,rexp y)
393 | rexp(T.BITSLICE(sz,x,y)) = I.BITSLICE(sz,x,rexp y)
394
395 | rexp(T.COND(sz,cc,x,y)) = if ccexp cc then rexp x else rexp y
396 | rexp(T.SX(a,b,x)) = I.SX(a,b,rexp x)
397 | rexp(T.ZX(a,b,x)) = I.ZX(a,b,rexp x)
398 | rexp(T.MARK(e,_)) = rexp e
399
400 | rexp _ = raise NonConst
401 and ccexp(T.TRUE) = true
402 | ccexp(T.FALSE) = false
403 | ccexp(T.CMP(sz,T.EQ,x,y)) = I.EQ(sz,rexp x,rexp y)
404 | ccexp(T.CMP(sz,T.NE,x,y)) = I.NE(sz,rexp x,rexp y)
405 | ccexp(T.CMP(sz,T.GT,x,y)) = I.GT(sz,rexp x,rexp y)
406 | ccexp(T.CMP(sz,T.GE,x,y)) = I.GE(sz,rexp x,rexp y)
407 | ccexp(T.CMP(sz,T.LT,x,y)) = I.LT(sz,rexp x,rexp y)
408 | ccexp(T.CMP(sz,T.LE,x,y)) = I.LE(sz,rexp x,rexp y)
409 | ccexp(T.CMP(sz,T.GTU,x,y)) = I.GTU(sz,rexp x,rexp y)
410 | ccexp(T.CMP(sz,T.LTU,x,y)) = I.LTU(sz,rexp x,rexp y)
411 | ccexp(T.CMP(sz,T.GEU,x,y)) = I.GEU(sz,rexp x,rexp y)
412 | ccexp(T.CMP(sz,T.LEU,x,y)) = I.LEU(sz,rexp x,rexp y)
413 | ccexp(T.NOT x) = not(ccexp x)
414 | ccexp(T.AND(x,y)) = ccexp x andalso ccexp y
415 | ccexp(T.OR(x,y)) = ccexp x orelse ccexp y
416 | ccexp(T.XOR(x,y)) = ccexp x <> ccexp y
417 | ccexp(T.EQV(x,y)) = ccexp x = ccexp y
418 | ccexp(T.CCMARK(e,_)) = ccexp e
419 | ccexp _ = raise NonConst
420 in {rexp=rexp, ccexp=ccexp}
421 end
422
423 fun valueOf e =
424 IntInf.toInt
425 (#rexp(eval{const=fn c => IntInf.fromInt(Constant.valueOf c),
426 label=Label.addrOf}) e)
427 val == = eqRexp
428 val hash = hashRexp
429
430 val resolveConstants = MLRiscControl.getFlag "asm-resolve-constants"
431 val _ = resolveConstants := true
432
433 (* This module should be parameterised, in order to generate
434 * target label expressions for assembly code purposes.
435 *)
436 (* operator precedences:
437 (Note: these differ from C's precedences)
438 2 MULT, DIV, LSHIFT, RSHIFT
439 1 AND, OR
440 0 PLUS, MINUS
441 *)
442
443 fun parens (str, prec, op_prec) =
444 if prec > op_prec then "(" ^ str ^ ")" else str
445
446 fun prInt i = if i < 0 then "-"^Int.toString(~i) else Int.toString i
447 fun prIntInf i = if IntInf.sign i < 0 then "-"^IntInf.toString(IntInf.~ i)
448 else IntInf.toString i
449
450 fun toString le = toStr(le, 0)
451
452 and toStr(T.LABEL lab, _) = Label.fmt labelFmt lab
453 | toStr(T.LABEXP le, p) = toStr(le, p)
454 | toStr(T.NEG(_, T.CONST c), _) =
455 if !resolveConstants then prInt(~(Constant.valueOf c))
456 else "(-" ^ Constant.toString c ^ ")"
457 | toStr(T.NEG(_, T.LI i), _) = prIntInf(~i)
458 | toStr(T.NEG(_, lexp), prec) = parens(toStr(lexp, 3), prec, 3)
459 | toStr(T.CONST c, _) =
460 if !resolveConstants then prInt(Constant.valueOf c)
461 else Constant.toString c
462 | toStr(T.LI i, _) = prIntInf i
463 | toStr(T.MULS(_,lexp1, lexp2), prec) =
464 parens(toStr(lexp1, 2) ^ "*" ^ toStr(lexp2,2), prec, 2)
465 | toStr(T.DIVS(_,lexp1, lexp2), prec) =
466 parens(toStr(lexp1, 2) ^ "/" ^ toStr(lexp2,2), prec, 2)
467 | toStr(T.SLL(_,lexp, cnt), prec) =
468 parens(toStr(lexp,2) ^ "<<" ^ toStr(cnt,2), prec, 2)
469 | toStr(T.SRL(_,lexp, cnt), prec) =
470 parens(toStr(lexp,2) ^ ">>" ^ toStr(cnt,2), prec, 2)
471 | toStr(T.ANDB(_,lexp, mask), prec) =
472 parens(toStr(lexp,1) ^ "&" ^ toStr(mask, 1), prec, 1)
473 | toStr(T.ORB(_,lexp, mask), prec) =
474 parens(toStr(lexp, 1) ^ "|" ^ toStr(mask, 1), prec, 1)
475 | toStr(T.ADD(_,lexp1, lexp2), prec) =
476 parens(toStr(lexp1, 0) ^ "+" ^ toStr(lexp2, 0), prec, 0)
477 | toStr(T.SUB(_,lexp1, lexp2), prec) =
478 parens(toStr(lexp1, 0) ^ "-" ^ toStr(lexp2, 0), prec, 0)
479 | toStr _ = error "toStr"
480
481 end