Debian Codegraph @

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

raw
1 (* mltree-eval.sml
2 *
3 * COPYRIGHT (c) 2001 Lucent Technologies, Bell Laboratories.
4 *
5 * Utilites to evaluate and compare mltree expressions.
6 *)
7
8 functor MLTreeEval
9 (structure T : MLTREE
10 (* equality extensions *)
11 val eqSext : T.equality -> T.sext * T.sext -> bool
12 val eqRext : T.equality -> T.rext * T.rext -> bool
13 val eqFext : T.equality -> T.fext * T.fext -> bool
14 val eqCCext : T.equality -> T.ccext * T.ccext -> bool
15 ) : MLTREE_EVAL =
16 struct
17 structure T = T
18 structure I = T.I
19 structure Constant = T.Constant
20 structure C = CellsBasis
21
22 val eqLabel = Label.same
23 fun eqLabels([],[]) = true
24 | eqLabels(a::b,c::d) = eqLabel(a,c) andalso eqLabels(b,d)
25 | eqLabels _ = false
26 and eqCell(C.CELL{id=x, ...},C.CELL{id=y, ...}) = x=y
27 and eqCells([], []) = true
28 | eqCells(x::xs,y::ys) = eqCell(x,y) andalso eqCells(xs,ys)
29 | eqCells _ = false
30 and eqCopy((t1,dst1,src1),(t2,dst2,src2)) =
31 t1=t2 andalso eqCells(dst1,dst2) andalso eqCells(src1,src2)
32 and eqCtrl(c1,c2) = eqCell(c1,c2)
33 and eqCtrls(c1,c2) = eqCells(c1,c2)
34
35 (* statements *)
36 and equality() = {stm=eqStm, rexp=eqRexp, fexp=eqFexp, ccexp=eqCCexp}
37 and eqStm(T.MV(a,b,c),T.MV(d,e,f)) =
38 a=d andalso eqCell(b,e) andalso eqRexp(c,f)
39 | eqStm(T.CCMV(a,b),T.CCMV(c,d)) = eqCell(a,c) andalso eqCCexp(b,d)
40 | eqStm(T.FMV(a,b,c),T.FMV(d,e,f)) =
41 a=d andalso eqCell(b,e) andalso eqFexp(c,f)
42 | eqStm(T.COPY x,T.COPY y) = eqCopy(x,y)
43 | eqStm(T.FCOPY x,T.FCOPY y) = eqCopy(x,y)
44 | eqStm(T.JMP(a,b),T.JMP(a',b')) = eqRexp(a,a')
45 | eqStm(T.CALL{funct=a,defs=b,uses=c,...},
46 T.CALL{funct=d,defs=e,uses=f,...}) =
47 eqRexp(a,d) andalso eqMlriscs(b,e) andalso eqMlriscs(c,f)
48 | eqStm(T.RET _,T.RET _) = true
49 | eqStm(T.STORE(a,b,c,_),T.STORE(d,e,f,_)) =
50 a=d andalso eqRexp(b,e) andalso eqRexp(c,f)
51 | eqStm(T.FSTORE(a,b,c,_),T.FSTORE(d,e,f,_)) =
52 a=d andalso eqRexp(b,e) andalso eqFexp(c,f)
53 | eqStm(T.ANNOTATION(s1, _),s2) = eqStm(s1,s2)
54 | eqStm(s1,T.ANNOTATION(s2, _)) = eqStm(s1,s2)
55 | eqStm(T.PHI x,T.PHI y) = x=y
56 | eqStm(T.SOURCE,T.SOURCE) = true
57 | eqStm(T.SINK,T.SINK) = true
58 | eqStm(T.BCC(b,c),T.BCC(b',c')) =
59 eqCCexp(b,b') andalso eqLabel(c,c')
60 | eqStm(T.IF(b,c,d),T.IF(b',c',d')) =
61 eqCCexp(b,b') andalso eqStm(c,c') andalso eqStm(d,d')
62 | eqStm(T.RTL{attribs=x,...},T.RTL{attribs=y,...}) = x=y
63 | eqStm(T.REGION(a,b),T.REGION(a',b')) = eqCtrl(b,b') andalso eqStm(a,a')
64 | eqStm(T.EXT a,T.EXT a') = eqSext (equality()) (a,a')
65 | eqStm _ = false
66
67 and eqStms([],[]) = true
68 | eqStms(a::b,c::d) = eqStm(a,c) andalso eqStms(b,d)
69 | eqStms _ = false
70
71 and eqMlrisc(T.CCR a,T.CCR b) = eqCCexp(a,b)
72 | eqMlrisc(T.GPR a,T.GPR b) = eqRexp(a,b)
73 | eqMlrisc(T.FPR a,T.FPR b) = eqFexp(a,b)
74 | eqMlrisc _ = false
75
76 and eqMlriscs([],[]) = true
77 | eqMlriscs(a::b,c::d) = eqMlrisc(a,c) andalso eqMlriscs(b,d)
78 | eqMlriscs _ = false
79
80 and eq2((a,b,c),(d,e,f)) = a=d andalso eqRexp(b,e) andalso eqRexp(c,f)
81 and eq3((m,a,b,c),(n,d,e,f)) =
82 m = n andalso a=d andalso eqRexp(b,e) andalso eqRexp(c,f)
83
84 and eqRexp(T.REG(a,b),T.REG(c,d)) = a=c andalso eqCell(b,d)
85 | eqRexp(T.LI a,T.LI b) = a = b
86 | eqRexp(T.LABEL a,T.LABEL b) = eqLabel(a,b)
87 | eqRexp(T.LABEXP a,T.LABEXP b) = eqRexp(a,b)
88 | eqRexp(T.CONST a,T.CONST b) = Constant.==(a,b)
89 | eqRexp(T.NEG(t,x),T.NEG(t',x')) = t = t' andalso eqRexp(x,x')
90 | eqRexp(T.ADD x,T.ADD y) = eq2(x,y)
91 | eqRexp(T.SUB x,T.SUB y) = eq2(x,y)
92 | eqRexp(T.MULS x,T.MULS y) = eq2(x,y)
93 | eqRexp(T.DIVS x,T.DIVS y) = eq3(x,y)
94 | eqRexp(T.REMS x,T.REMS y) = eq3(x,y)
95 | eqRexp(T.MULU x,T.MULU y) = eq2(x,y)
96 | eqRexp(T.DIVU x,T.DIVU y) = eq2(x,y)
97 | eqRexp(T.REMU x,T.REMU y) = eq2(x,y)
98 | eqRexp(T.NEGT(t,x),T.NEGT(t',x')) = t = t' andalso eqRexp(x,x')
99 | eqRexp(T.ADDT x,T.ADDT y) = eq2(x,y)
100 | eqRexp(T.SUBT x,T.SUBT y) = eq2(x,y)
101 | eqRexp(T.MULT x,T.MULT y) = eq2(x,y)
102 | eqRexp(T.DIVT x,T.DIVT y) = eq3(x,y)
103 | eqRexp(T.ANDB x,T.ANDB y) = eq2(x,y)
104 | eqRexp(T.ORB x,T.ORB y) = eq2(x,y)
105 | eqRexp(T.XORB x,T.XORB y) = eq2(x,y)
106 | eqRexp(T.EQVB x,T.EQVB y) = eq2(x,y)
107 | eqRexp(T.NOTB(a,b),T.NOTB(c,d)) = a=c andalso eqRexp(b,d)
108 | eqRexp(T.SRA x,T.SRA y) = eq2(x,y)
109 | eqRexp(T.SRL x,T.SRL y) = eq2(x,y)
110 | eqRexp(T.SLL x,T.SLL y) = eq2(x,y)
111 | eqRexp(T.COND(a,b,c,d),T.COND(e,f,g,h)) =
112 a=e andalso eqCCexp(b,f) andalso eqRexp(c,g) andalso eqRexp(d,h)
113 | eqRexp(T.SX(a,b,c),T.SX(a',b',c')) =
114 a=a' andalso b=b' andalso eqRexp(c,c')
115 | eqRexp(T.ZX(a,b,c),T.ZX(a',b',c')) =
116 a=a' andalso b=b' andalso eqRexp(c,c')
117 | eqRexp(T.CVTF2I(a,b,c,d),T.CVTF2I(e,f,g,h)) =
118 a=e andalso b=f andalso c=g andalso eqFexp(d,h)
119 | eqRexp(T.LOAD(a,b,_),T.LOAD(c,d,_)) = a=c andalso eqRexp(b,d)
120 | eqRexp(T.LET(a,b),T.LET(c,d)) = eqStm(a,c) andalso eqRexp(b,d)
121 | eqRexp(T.ARG x,T.ARG y) = x = y
122 | eqRexp(T.PARAM x,T.PARAM y) = x = y
123 | eqRexp(T.???,T.???) = true
124 | eqRexp(T.$(t1,k1,e1),T.$(t2,k2,e2)) =
125 t1=t2 andalso k1=k2 andalso eqRexp(e1,e2)
126 | eqRexp(T.BITSLICE(t1,s1,e1),T.BITSLICE(t2,s2,e2)) =
127 t1=t2 andalso s1=s2 andalso eqRexp(e1,e2)
128 | eqRexp(T.MARK(a,_),b) = eqRexp(a,b)
129 | eqRexp(a,T.MARK(b,_)) = eqRexp(a,b)
130 | eqRexp(T.PRED(a,b),T.PRED(a',b')) = eqCtrl(b,b') andalso eqRexp(a,a')
131 | eqRexp(T.REXT(a,b),T.REXT(a',b')) =
132 a=a' andalso eqRext (equality()) (b,b')
133 | eqRexp _ = false
134
135 and eqRexps([],[]) = true
136 | eqRexps(a::b,c::d) = eqRexp(a,c) andalso eqRexps(b,d)
137 | eqRexps _ = false
138
139 and eq2'((a,b,c),(d,e,f)) = a=d andalso eqFexp(b,e) andalso eqFexp(c,f)
140 and eq1'((a,b),(d,e)) = a=d andalso eqFexp(b,e)
141
142 and eqFexp(T.FREG(t1,x),T.FREG(t2,y)) = t1=t2 andalso eqCell(x,y)
143 | eqFexp(T.FLOAD(a,b,_),T.FLOAD(c,d,_)) = a=c andalso eqRexp(b,d)
144 | eqFexp(T.FADD x,T.FADD y) = eq2'(x,y)
145 | eqFexp(T.FMUL x,T.FMUL y) = eq2'(x,y)
146 | eqFexp(T.FSUB x,T.FSUB y) = eq2'(x,y)
147 | eqFexp(T.FDIV x,T.FDIV y) = eq2'(x,y)
148 | eqFexp(T.FCOPYSIGN x, T.FCOPYSIGN y) = eq2'(x,y)
149 | eqFexp(T.FCOND(t,x,y,z), T.FCOND(t',x',y',z')) =
150 t=t' andalso eqCCexp(x,x') andalso eqFexp(y,y') andalso eqFexp(z,z')
151 | eqFexp(T.FABS x,T.FABS y) = eq1'(x,y)
152 | eqFexp(T.FNEG x,T.FNEG y) = eq1'(x,y)
153 | eqFexp(T.FSQRT x,T.FSQRT y) = eq1'(x,y)
154 | eqFexp(T.CVTI2F(a,b,c),T.CVTI2F(a',b',c')) =
155 a=a' andalso b=b' andalso eqRexp(c,c')
156 | eqFexp(T.CVTF2F(a,b,c),T.CVTF2F(a',b',c')) =
157 a=a' andalso b=b' andalso eqFexp(c,c')
158 | eqFexp(T.FEXT(a,f),T.FEXT(b,g)) = a=b andalso eqFext (equality()) (f,g)
159 | eqFexp(T.FMARK(a,_),b) = eqFexp(a,b)
160 | eqFexp(a,T.FMARK(b,_)) = eqFexp(a,b)
161 | eqFexp(T.FPRED(a,b),T.FPRED(a',b')) = eqCtrl(b,b') andalso eqFexp(a,a')
162 | eqFexp _ = false
163
164 and eqFexps([],[]) = true
165 | eqFexps(a::b,c::d) = eqFexp(a,c) andalso eqFexps(b,d)
166 | eqFexps _ = false
167
168 and eqCCexp(T.CC(c1,x),T.CC(c2,y)) = c1=c2 andalso eqCell(x,y)
169 | eqCCexp(T.FCC(c1,x),T.FCC(c2,y)) = c1=c2 andalso eqCell(x,y)
170 | eqCCexp(T.CMP(x,a,b,c),T.CMP(y,d,e,f)) =
171 a=d andalso eqRexp(b,e) andalso eqRexp(c,f) andalso x = y
172 | eqCCexp(T.FCMP(x,a,b,c),T.FCMP(y,d,e,f)) =
173 a=d andalso eqFexp(b,e) andalso eqFexp(c,f) andalso x = y
174 | eqCCexp(T.NOT x, T.NOT y) = eqCCexp(x,y)
175 | eqCCexp(T.AND x, T.AND y) = eqCCexp2(x,y)
176 | eqCCexp(T.OR x, T.OR y) = eqCCexp2(x,y)
177 | eqCCexp(T.XOR x, T.XOR y) = eqCCexp2(x,y)
178 | eqCCexp(T.EQV x, T.EQV y) = eqCCexp2(x,y)
179 | eqCCexp(T.CCMARK(a,_),b) = eqCCexp(a,b)
180 | eqCCexp(a,T.CCMARK(b,_)) = eqCCexp(a,b)
181 | eqCCexp(T.CCEXT(t,a),T.CCEXT(t',b)) =
182 t=t' andalso eqCCext (equality()) (a,b)
183 | eqCCexp(T.TRUE, T.TRUE) = true
184 | eqCCexp(T.FALSE, T.FALSE) = true
185 | eqCCexp _ = false
186
187 and eqCCexp2((x,y),(x',y')) = eqCCexp(x,x') andalso eqCCexp(y,y')
188
189 and eqCCexps([],[]) = true
190 | eqCCexps(a::b,c::d) = eqCCexp(a,c) andalso eqCCexps(b,d)
191 | eqCCexps _ = false
192
193 exception NonConst
194
195 fun eval{label, const} =
196 let fun drm T.DIV_TO_ZERO = I.DIV_TO_ZERO
197 | drm T.DIV_TO_NEGINF = I.DIV_TO_NEGINF
198 fun rexp(T.LI i) = i
199 | rexp(T.CONST c) = const c
200 | rexp(T.LABEL l) = IntInf.fromInt(label l)
201 | rexp(T.LABEXP e) = rexp e
202
203 | rexp(T.NEG(sz,x)) = I.NEG(sz,rexp x)
204 | rexp(T.ADD(sz,x,y)) = I.ADD(sz,rexp x,rexp y)
205 | rexp(T.SUB(sz,x,y)) = I.SUB(sz,rexp x,rexp y)
206
207 | rexp(T.MULS(sz,x,y)) = I.MULS(sz,rexp x,rexp y)
208 | rexp(T.DIVS(m,sz,x,y)) = I.DIVS(drm m,sz,rexp x,rexp y)
209 | rexp(T.REMS(m,sz,x,y)) = I.REMS(drm m,sz,rexp x,rexp y)
210
211 | rexp(T.MULU(sz,x,y)) = I.MULU(sz,rexp x,rexp y)
212 | rexp(T.DIVU(sz,x,y)) = I.DIVU(sz,rexp x,rexp y)
213 | rexp(T.REMU(sz,x,y)) = I.REMU(sz,rexp x,rexp y)
214
215 | rexp(T.NEGT(sz,x)) = I.NEGT(sz,rexp x)
216 | rexp(T.ADDT(sz,x,y)) = I.ADDT(sz,rexp x,rexp y)
217 | rexp(T.SUBT(sz,x,y)) = I.SUBT(sz,rexp x,rexp y)
218 | rexp(T.MULT(sz,x,y)) = I.MULT(sz,rexp x,rexp y)
219 | rexp(T.DIVT(m,sz,x,y)) = I.DIVT(drm m,sz,rexp x,rexp y)
220
221 | rexp(T.NOTB(sz,x)) = I.NOTB(sz,rexp x)
222 | rexp(T.ANDB(sz,x,y)) = I.ANDB(sz,rexp x,rexp y)
223 | rexp(T.ORB(sz,x,y)) = I.ORB(sz,rexp x,rexp y)
224 | rexp(T.XORB(sz,x,y)) = I.XORB(sz,rexp x,rexp y)
225 | rexp(T.EQVB(sz,x,y)) = I.EQVB(sz,rexp x,rexp y)
226 | rexp(T.SLL(sz,x,y)) = I.SLL(sz,rexp x,rexp y)
227 | rexp(T.SRL(sz,x,y)) = I.SRL(sz,rexp x,rexp y)
228 | rexp(T.SRA(sz,x,y)) = I.SRA(sz,rexp x,rexp y)
229 | rexp(T.BITSLICE(sz,x,y)) = I.BITSLICE(sz,x,rexp y)
230
231 | rexp(T.COND(sz,cc,x,y)) = if ccexp cc then rexp x else rexp y
232 | rexp(T.SX(a,b,x)) = I.SX(a,b,rexp x)
233 | rexp(T.ZX(a,b,x)) = I.ZX(a,b,rexp x)
234 | rexp(T.MARK(e,_)) = rexp e
235
236 | rexp _ = raise NonConst
237 and ccexp(T.TRUE) = true
238 | ccexp(T.FALSE) = false
239 | ccexp(T.CMP(sz,T.EQ,x,y)) = I.EQ(sz,rexp x,rexp y)
240 | ccexp(T.CMP(sz,T.NE,x,y)) = I.NE(sz,rexp x,rexp y)
241 | ccexp(T.CMP(sz,T.GT,x,y)) = I.GT(sz,rexp x,rexp y)
242 | ccexp(T.CMP(sz,T.GE,x,y)) = I.GE(sz,rexp x,rexp y)
243 | ccexp(T.CMP(sz,T.LT,x,y)) = I.LT(sz,rexp x,rexp y)
244 | ccexp(T.CMP(sz,T.LE,x,y)) = I.LE(sz,rexp x,rexp y)
245 | ccexp(T.CMP(sz,T.GTU,x,y)) = I.GTU(sz,rexp x,rexp y)
246 | ccexp(T.CMP(sz,T.LTU,x,y)) = I.LTU(sz,rexp x,rexp y)
247 | ccexp(T.CMP(sz,T.GEU,x,y)) = I.GEU(sz,rexp x,rexp y)
248 | ccexp(T.CMP(sz,T.LEU,x,y)) = I.LEU(sz,rexp x,rexp y)
249 | ccexp(T.NOT x) = not(ccexp x)
250 | ccexp(T.AND(x,y)) = ccexp x andalso ccexp y
251 | ccexp(T.OR(x,y)) = ccexp x orelse ccexp y
252 | ccexp(T.XOR(x,y)) = ccexp x <> ccexp y
253 | ccexp(T.EQV(x,y)) = ccexp x = ccexp y
254 | ccexp(T.CCMARK(e,_)) = ccexp e
255 | ccexp _ = raise NonConst
256 in {rexp=rexp, ccexp=ccexp}
257 end
258
259 fun valueOf e =
260 IntInf.toInt
261 (#rexp(eval{const=fn c => IntInf.fromInt(Constant.valueOf c),
262 label=Label.addrOf}) e)
263 val == = eqRexp
264 end