Debian Codegraph @

lib/mlrisc-lib/MLRISC/Tools/MDL/mdl-rtl-tools.sml

raw
1 (*
2 * Process rtl descriptions
3 *)
4 functor MDLRTLTools
5 (structure AstUtil : MDL_AST_UTIL
6 structure MLTreeRTL : MLTREE_RTL
7 ) : MDL_RTL_TOOLS =
8 struct
9 structure Ast = AstUtil.Ast
10 structure RTL = MLTreeRTL
11 structure T = RTL.T
12 structure A = Ast
13 structure U = AstUtil
14 structure CellsBasis = CellsBasis
15
16 fun error msg = MLRiscErrorMsg.error("MDRTLTools",msg)
17
18 (*========================================================================
19 *
20 * Simplify an RTL expression
21 *
22 *========================================================================*)
23 fun simplify rtl =
24 let fun stm reduce (T.SEQ [s]) = s
25 | stm reduce (T.IF(T.TRUE, y, n)) = y
26 | stm reduce (T.IF(T.FALSE, y, n)) = n
27 | stm reduce s = s
28
29 and (* rexp reduce (T.ADD(_,T.LI 0,x)) = x
30 | rexp reduce (T.ADD(_,x,T.LI 0)) = x
31 | rexp reduce (T.SUB(_,x,T.LI 0)) = x
32 | rexp reduce (T.MULS(_,_,zero as T.LI 0)) = zero
33 | rexp reduce (T.MULU(_,_,zero as T.LI 0)) = zero
34 | rexp reduce (T.MULT(_,_,zero as T.LI 0)) = zero
35 | rexp reduce (T.MULS(_,zero as T.LI 0, _)) = zero
36 | rexp reduce (T.MULU(_,zero as T.LI 0, _)) = zero
37 | rexp reduce (T.MULT(_,zero as T.LI 0, _)) = zero
38 | rexp reduce (T.MULS(_,x,T.LI 1)) = x
39 | rexp reduce (T.MULU(_,x,T.LI 1)) = x
40 | rexp reduce (T.MULT(_,x,T.LI 1)) = x
41 | rexp reduce (T.DIVS(_,x,T.LI 1)) = x
42 | rexp reduce (T.DIVU(_,x,T.LI 1)) = x
43 | rexp reduce (T.DIVT(_,x,T.LI 1)) = x
44 | rexp reduce (T.ANDB(_,_,zero as T.LI 0)) = zero
45 | rexp reduce (T.ANDB(_,zero as T.LI 0,_)) = zero
46 | *) rexp reduce (e as T.ANDB(_,x,y)) =
47 if RTL.Util.eqRexp(x,y) then x else e
48 (* | rexp reduce (T.ORB(_,x,T.LI 0)) = x
49 | rexp reduce (T.ORB(_,T.LI 0,x)) = x *)
50 | rexp reduce (e as T.ORB(_,x,y)) =
51 if RTL.Util.eqRexp(x,y) then x else e
52 | rexp reduce (T.NOTB(_,T.NOTB(_,x))) = x
53 | rexp reduce (e as T.SX(t1,t2,x)) = if t1 = t2 then x else e
54 | rexp reduce (e as T.ZX(t1,t2,x)) = if t1 = t2 then x else e
55 | rexp reduce e = e
56 and fexp reduce e = e
57
58 and ccexp reduce (T.NOT T.TRUE) = T.FALSE
59 | ccexp reduce (T.NOT T.FALSE) = T.TRUE
60 | ccexp reduce (T.AND(T.FALSE,_)) = T.FALSE
61 | ccexp reduce (T.AND(_,T.FALSE)) = T.FALSE
62 | ccexp reduce (T.AND(T.TRUE, x)) = x
63 | ccexp reduce (T.AND(x,T.TRUE)) = x
64 | ccexp reduce (T.OR(T.FALSE,x)) = x
65 | ccexp reduce (T.OR(x,T.FALSE)) = x
66 | ccexp reduce (T.OR(T.TRUE, _)) = T.TRUE
67 | ccexp reduce (T.OR(_,T.TRUE)) = T.TRUE
68 | ccexp reduce (e as T.CMP(_,T.EQ,x,y)) =
69 if RTL.Util.eqRexp(x,y) then T.TRUE else e
70 | ccexp reduce (e as T.CMP(_,T.NE,x,y)) =
71 if RTL.Util.eqRexp(x,y) then T.FALSE else e
72 | ccexp reduce e = e
73
74 val rewriter =
75 RTL.Rewrite.rewrite{rexp=rexp, fexp=fexp, ccexp=ccexp, stm=stm}
76 in #stm rewriter rtl
77 end
78
79 (*========================================================================
80 *
81 * Translate an RTL into something else
82 *
83 *========================================================================*)
84 fun transRTL
85 {app,id,int,intinf,word32,string,list,Nil,tuple,record,arg,
86 cellkind,oper,region}
87 rtl =
88 let fun word w = word32(Word32.fromLargeWord(Word.toLargeWord w))
89 fun ternOp n (x,ty,y,z) = app(n,[x, int ty, rexp y, rexp z])
90 and binOp n (ty,x,y) = app(n,[int ty,rexp x,rexp y])
91 and unaryOp n (ty,x) = app(n,[int ty,rexp x])
92 and rexp(T.LI i) = app("LI",[intinf i])
93 | rexp(T.NEG x) = unaryOp "NEG" x
94 | rexp(T.ADD x) = binOp "ADD" x
95 | rexp(T.SUB x) = binOp "SUB" x
96 | rexp(T.MULS x) = binOp "MULS" x
97 (* FIXME
98 | rexp(T.DIVS x) = ternOp "DIVS" x
99 | rexp(T.REMS x) = ternOp "REMS" x
100 *)
101 | rexp(T.MULU x) = binOp "MULU" x
102 | rexp(T.DIVU x) = binOp "DIVU" x
103 | rexp(T.REMU x) = binOp "REMU" x
104 | rexp(T.NEGT x) = unaryOp "NEGT" x
105 | rexp(T.ADDT x) = binOp "ADDT" x
106 | rexp(T.SUBT x) = binOp "SUBT" x
107 | rexp(T.MULT x) = binOp "MULT" x
108 (* FIXME
109 | rexp(T.DIVT x) = ternOp "DIVT" x
110 *)
111 | rexp(T.NOTB x) = unaryOp "NOTB" x
112 | rexp(T.ANDB x) = binOp "ANDB" x
113 | rexp(T.ORB x) = binOp "ORB" x
114 | rexp(T.XORB x) = binOp "XORB" x
115 | rexp(T.EQVB x) = binOp "EQVB" x
116 | rexp(T.SLL x) = binOp "SLL" x
117 | rexp(T.SRL x) = binOp "SRL" x
118 | rexp(T.SRA x) = binOp "SRA" x
119 | rexp(T.SX(t1,t2,x)) = app("SX",[int t1,int t2,rexp x])
120 | rexp(T.ZX(t1,t2,x)) = app("ZX",[int t1,int t2,rexp x])
121 | rexp(T.CVTF2I(t1,r,t2,x)) =
122 app("CVTF2I",[int t1,id(T.Basis.roundingModeToString r),
123 int t2,fexp x])
124 | rexp(T.COND(ty,cc,a,b)) =
125 app("COND",[int ty,ccexp cc,rexp a,rexp b])
126 | rexp(T.$(ty,k,e)) = app("$",[int ty,cellkind k,rexp e])
127 | rexp(T.ARG(ty,a,b)) = arg(ty,a,b)
128 | rexp(T.PARAM(i)) = app("PARAM",[int i])
129 | rexp(T.???) = id "???"
130 | rexp(T.OP(ty,opc,es)) =
131 app("OP",[int ty,oper opc,list(map rexp es, NONE)])
132 | rexp(T.BITSLICE(ty,sl,e)) =
133 app("BITSLICE",[int ty,slice sl,rexp e])
134 | rexp e = error("transRTL: "^RTL.Util.rexpToString e)
135 and slice sl = list(map (fn (x,y) => tuple[int x,int y]) sl, NONE)
136 and fbinOp n (ty,x,y) = app(n,[int ty,fexp x,fexp y])
137 and funaryOp n (ty,x) = app(n,[int ty,fexp x])
138 and fexp(T.FADD x) = fbinOp "FADD" x
139 | fexp(T.FSUB x) = fbinOp "FSUB" x
140 | fexp(T.FMUL x) = fbinOp "FMUL" x
141 | fexp(T.FDIV x) = fbinOp "FDIV" x
142 | fexp(T.FCOPYSIGN x) = fbinOp "FCOPYSIGN" x
143 | fexp(T.FNEG x) = funaryOp "FNEG" x
144 | fexp(T.FABS x) = funaryOp "FABS" x
145 | fexp(T.FSQRT x) = funaryOp "FSQRT" x
146 | fexp(T.FCOND(ty,cc,x,y)) =
147 app("FCOND",[int ty,ccexp cc,fexp x,fexp y])
148 | fexp(T.CVTI2F(t1,t2,x)) = app("CVTI2F",[int t1,int t2,rexp x])
149 | fexp(T.CVTF2F(t1,t2,x)) = app("CVTF2F",[int t1,int t2,fexp x])
150 | fexp e = error("transRTL: "^RTL.Util.fexpToString e)
151
152 and stm(T.ASSIGN(ty,x,y)) = app("ASSIGN",[int ty,rexp x,rexp y])
153 | stm(T.JMP(e,_)) = app("JMP",[rexp e,Nil])
154 | stm(T.RET _) = app("RET",[Nil])
155 | stm(T.IF(x,y,z)) = app("IF",[ccexp x,stm y,stm z])
156 | stm(T.SEQ ss) = app("SEQ",[list(map stm ss,NONE)])
157 | stm(T.RTL{e, ...}) = stm e
158 | stm(T.CALL{funct,...}) = app("CALL",
159 [record[("defs",Nil),
160 ("uses",Nil),
161 ("funct",rexp funct),
162 ("targets",Nil),
163 ("region",region)]
164 ]
165 )
166 | stm s = error("transRTL: "^RTL.Util.stmToString s)
167
168 and ccexp(T.CMP(ty,cc,x,y)) =
169 app("CMP",[int ty,id(T.Basis.condToString cc),rexp x,rexp y])
170 | ccexp(T.FCMP(ty,cc,x,y)) =
171 app("FCMP",[int ty,id(T.Basis.fcondToString cc),fexp x,fexp y])
172 | ccexp(T.TRUE) = id "TRUE"
173 | ccexp(T.FALSE) = id "FALSE"
174 | ccexp(T.AND(x,y)) = app("AND",[ccexp x,ccexp y])
175 | ccexp(T.OR(x,y)) = app("OR",[ccexp x,ccexp y])
176 | ccexp(T.XOR(x,y)) = app("XOR",[ccexp x,ccexp y])
177 | ccexp(T.EQV(x,y)) = app("EQV",[ccexp x,ccexp y])
178 | ccexp(T.NOT x) = app("NOT",[ccexp x])
179 | ccexp e = error("transRTL: "^RTL.Util.ccexpToString e)
180 in stm rtl
181 end
182
183 (*========================================================================
184 * Translate an RTL to an expression
185 *========================================================================*)
186 fun rtlToExp rtl =
187 let fun id name = A.IDexp(A.IDENT(["T"],name))
188 fun app(n, es) = A.APPexp(id n, A.TUPLEexp es)
189 val int = U.INTexp
190 val string= U.STRINGexp
191 fun arg(ty,a,name) = A.IDexp(A.IDENT([],name))
192 fun cellkind k = A.IDexp(A.IDENT(["C"],CellsBasis.cellkindToString k))
193 fun oper(T.OPER{name,...}) = A.IDexp(A.IDENT(["P"],name))
194 val region=A.IDexp(A.IDENT(["T","Region"],"memory"))
195 in transRTL{id=id, app=app, list=A.LISTexp, string=string,
196 int=int, intinf=U.INTINFexp,
197 word32=U.WORD32exp, Nil=A.LISTexp([],NONE),
198 tuple=A.TUPLEexp, record=A.RECORDexp,
199 region=region, arg=arg, cellkind=cellkind, oper=oper
200 } rtl
201 end
202
203 (*========================================================================
204 * Translate an RTL to a pattern
205 *========================================================================*)
206 fun rtlToPat rtl =
207 let fun mkId name = A.IDENT(["T"],name)
208 fun id name = A.CONSpat(mkId name,NONE)
209 fun app(n, [x]) = A.CONSpat(mkId n, SOME x)
210 | app(n, es) = A.CONSpat(mkId n, SOME(A.TUPLEpat es))
211 fun record ps = A.RECORDpat(ps,false)
212 val int = U.INTpat
213 val intinf= U.INTINFpat
214 val string= U.STRINGpat
215 fun arg(ty,a,name) = A.IDpat name
216 fun cellkind k = A.IDpat(CellsBasis.cellkindToString k)
217 fun oper(T.OPER{name,...}) =
218 A.CONSpat(A.IDENT(["T"],"OPER"),
219 SOME(A.RECORDpat([("name",U.STRINGpat name)],true)))
220 val region=A.WILDpat
221 in transRTL{id=id, app=app, list=A.LISTpat, string=string,
222 int=int, intinf=intinf,
223 word32=U.WORD32pat, Nil=A.LISTpat([],NONE),
224 tuple=A.TUPLEpat, record=record, region=region,
225 arg=arg, cellkind=cellkind, oper=oper
226 } rtl
227 end
228
229 (*========================================================================
230 * Translate an RTL to a function with arguments
231 *========================================================================*)
232 fun rtlToFun(rtlName, rtlArgs, rtl) =
233 let val body = rtlToExp rtl
234 val args = A.RECORDpat(map (fn id => (id,A.IDpat id)) rtlArgs,false)
235 in A.FUNdecl
236 [A.FUNbind(rtlName, [A.CLAUSE([args], NONE, body)])]
237 end
238
239 (*========================================================================
240 * Create a new_op
241 *========================================================================*)
242 fun createNewOp{name, hash, attribs} =
243 A.VALdecl[
244 A.VALbind(A.IDpat name,
245 A.APPexp(A.IDexp(A.IDENT(["T"],"OPER")),
246 A.APPexp(A.IDexp(A.IDENT(["RTL"],"newOp")),
247 A.RECORDexp[("name",U.STRINGexp name),
248 ("attribs",U.WORDexp (!attribs))
249 ])))
250 ]
251
252 end
253