SCM Repository
Annotation of /sml/trunk/src/MLRISC/Tools/MDL/mdl-rtl-tools.sml
Parent Directory
|
Revision Log
Revision 744 - (view) (download)
1 : | leunga | 744 | (* |
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 (e as T.LI32 w) = | ||
30 : | (T.LI(Word32.toIntX w) handle Overflow => e) | ||
31 : | | rexp reduce (T.ADD(_,T.LI 0,x)) = x | ||
32 : | | rexp reduce (T.ADD(_,x,T.LI 0)) = x | ||
33 : | | rexp reduce (T.SUB(_,x,T.LI 0)) = x | ||
34 : | | rexp reduce (T.MULS(_,_,zero as T.LI 0)) = zero | ||
35 : | | rexp reduce (T.MULU(_,_,zero as T.LI 0)) = zero | ||
36 : | | rexp reduce (T.MULT(_,_,zero as T.LI 0)) = zero | ||
37 : | | rexp reduce (T.MULS(_,zero as T.LI 0, _)) = zero | ||
38 : | | rexp reduce (T.MULU(_,zero as T.LI 0, _)) = zero | ||
39 : | | rexp reduce (T.MULT(_,zero as T.LI 0, _)) = zero | ||
40 : | | rexp reduce (T.MULS(_,x,T.LI 1)) = x | ||
41 : | | rexp reduce (T.MULU(_,x,T.LI 1)) = x | ||
42 : | | rexp reduce (T.MULT(_,x,T.LI 1)) = x | ||
43 : | | rexp reduce (T.DIVS(_,x,T.LI 1)) = x | ||
44 : | | rexp reduce (T.DIVU(_,x,T.LI 1)) = x | ||
45 : | | rexp reduce (T.DIVT(_,x,T.LI 1)) = x | ||
46 : | | rexp reduce (T.ANDB(_,_,zero as T.LI 0)) = zero | ||
47 : | | rexp reduce (T.ANDB(_,zero as T.LI 0,_)) = zero | ||
48 : | | rexp reduce (e as T.ANDB(_,x,y)) = | ||
49 : | if RTL.Util.eqRexp(x,y) then x else e | ||
50 : | | rexp reduce (T.ORB(_,x,T.LI 0)) = x | ||
51 : | | rexp reduce (T.ORB(_,T.LI 0,x)) = x | ||
52 : | | rexp reduce (e as T.ORB(_,x,y)) = | ||
53 : | if RTL.Util.eqRexp(x,y) then x else e | ||
54 : | | rexp reduce (T.NOTB(_,T.NOTB(_,x))) = x | ||
55 : | | rexp reduce (e as T.SX(t1,t2,x)) = if t1 = t2 then x else e | ||
56 : | | rexp reduce (e as T.ZX(t1,t2,x)) = if t1 = t2 then x else e | ||
57 : | | rexp reduce e = e | ||
58 : | and fexp reduce e = e | ||
59 : | |||
60 : | and ccexp reduce (T.NOT T.TRUE) = T.FALSE | ||
61 : | | ccexp reduce (T.NOT T.FALSE) = T.TRUE | ||
62 : | | ccexp reduce (T.AND(T.FALSE,_)) = T.FALSE | ||
63 : | | ccexp reduce (T.AND(_,T.FALSE)) = T.FALSE | ||
64 : | | ccexp reduce (T.AND(T.TRUE, x)) = x | ||
65 : | | ccexp reduce (T.AND(x,T.TRUE)) = x | ||
66 : | | ccexp reduce (T.OR(T.FALSE,x)) = x | ||
67 : | | ccexp reduce (T.OR(x,T.FALSE)) = x | ||
68 : | | ccexp reduce (T.OR(T.TRUE, _)) = T.TRUE | ||
69 : | | ccexp reduce (T.OR(_,T.TRUE)) = T.TRUE | ||
70 : | | ccexp reduce (e as T.CMP(_,T.EQ,x,y)) = | ||
71 : | if RTL.Util.eqRexp(x,y) then T.TRUE else e | ||
72 : | | ccexp reduce (e as T.CMP(_,T.NE,x,y)) = | ||
73 : | if RTL.Util.eqRexp(x,y) then T.FALSE else e | ||
74 : | | ccexp reduce e = e | ||
75 : | |||
76 : | val rewriter = | ||
77 : | RTL.Rewrite.rewrite{rexp=rexp, fexp=fexp, ccexp=ccexp, stm=stm} | ||
78 : | in #stm rewriter rtl | ||
79 : | end | ||
80 : | |||
81 : | (*======================================================================== | ||
82 : | * | ||
83 : | * Translate an RTL into something else | ||
84 : | * | ||
85 : | *========================================================================*) | ||
86 : | fun transRTL | ||
87 : | {app,id,int,word32,string,list,Nil,tuple,record,arg, | ||
88 : | cellkind,oper,region} | ||
89 : | rtl = | ||
90 : | let fun word w = word32(Word.toLargeWord w) | ||
91 : | fun binOp n (ty,x,y) = app(n,[int ty,rexp x,rexp y]) | ||
92 : | and unaryOp n (ty,x) = app(n,[int ty,rexp x]) | ||
93 : | and rexp(T.LI i) = app("LI",[int i]) | ||
94 : | | rexp(T.LI32 w) = app("LI32",[word32 w]) | ||
95 : | | rexp(T.NEG x) = unaryOp "NEG" x | ||
96 : | | rexp(T.ADD x) = binOp "ADD" x | ||
97 : | | rexp(T.SUB x) = binOp "SUB" x | ||
98 : | | rexp(T.MULS x) = binOp "MULS" x | ||
99 : | | rexp(T.DIVS x) = binOp "DIVS" x | ||
100 : | | rexp(T.REMS x) = binOp "REMS" x | ||
101 : | | rexp(T.QUOTS x) = binOp "QUOTS" x | ||
102 : | | rexp(T.MULU x) = binOp "MULU" x | ||
103 : | | rexp(T.DIVU x) = binOp "DIVU" x | ||
104 : | | rexp(T.REMU x) = binOp "REMU" x | ||
105 : | | rexp(T.NEGT x) = unaryOp "NEGT" x | ||
106 : | | rexp(T.ADDT x) = binOp "ADDT" x | ||
107 : | | rexp(T.SUBT x) = binOp "SUBT" x | ||
108 : | | rexp(T.MULT x) = binOp "MULT" x | ||
109 : | | rexp(T.DIVT x) = binOp "DIVT" x | ||
110 : | | rexp(T.REMT x) = binOp "REMT" x | ||
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, word32=U.WORD32exp, Nil=A.LISTexp([],NONE), | ||
197 : | tuple=A.TUPLEexp, record=A.RECORDexp, | ||
198 : | region=region, arg=arg, cellkind=cellkind, oper=oper | ||
199 : | } rtl | ||
200 : | end | ||
201 : | |||
202 : | (*======================================================================== | ||
203 : | * Translate an RTL to a pattern | ||
204 : | *========================================================================*) | ||
205 : | fun rtlToPat rtl = | ||
206 : | let fun mkId name = A.IDENT(["T"],name) | ||
207 : | fun id name = A.CONSpat(mkId name,NONE) | ||
208 : | fun app(n, [x]) = A.CONSpat(mkId n, SOME x) | ||
209 : | | app(n, es) = A.CONSpat(mkId n, SOME(A.TUPLEpat es)) | ||
210 : | fun record ps = A.RECORDpat(ps,false) | ||
211 : | val int = U.INTpat | ||
212 : | val string= U.STRINGpat | ||
213 : | fun arg(ty,a,name) = A.IDpat name | ||
214 : | fun cellkind k = A.IDpat(CellsBasis.cellkindToString k) | ||
215 : | fun oper(T.OPER{name,...}) = | ||
216 : | A.CONSpat(A.IDENT(["T"],"OPER"), | ||
217 : | SOME(A.RECORDpat([("name",U.STRINGpat name)],true))) | ||
218 : | val region=A.WILDpat | ||
219 : | in transRTL{id=id, app=app, list=A.LISTpat, string=string, | ||
220 : | int=int, word32=U.WORD32pat, Nil=A.LISTpat([],NONE), | ||
221 : | tuple=A.TUPLEpat, record=record, region=region, | ||
222 : | arg=arg, cellkind=cellkind, oper=oper | ||
223 : | } rtl | ||
224 : | end | ||
225 : | |||
226 : | (*======================================================================== | ||
227 : | * Translate an RTL to a function with arguments | ||
228 : | *========================================================================*) | ||
229 : | fun rtlToFun(rtlName, rtlArgs, rtl) = | ||
230 : | let val body = rtlToExp rtl | ||
231 : | val args = A.RECORDpat(map (fn id => (id,A.IDpat id)) rtlArgs,false) | ||
232 : | in A.FUNdecl | ||
233 : | [A.FUNbind(rtlName, [A.CLAUSE([args], NONE, body)])] | ||
234 : | end | ||
235 : | |||
236 : | (*======================================================================== | ||
237 : | * Create a new_op | ||
238 : | *========================================================================*) | ||
239 : | fun createNewOp{name, hash, attribs} = | ||
240 : | A.VALdecl[ | ||
241 : | A.VALbind(A.IDpat name, | ||
242 : | A.APPexp(A.IDexp(A.IDENT(["T"],"OPER")), | ||
243 : | A.APPexp(A.IDexp(A.IDENT(["RTL"],"newOp")), | ||
244 : | A.RECORDexp[("name",U.STRINGexp name), | ||
245 : | ("attribs",U.WORDexp (!attribs)) | ||
246 : | ]))) | ||
247 : | ] | ||
248 : | |||
249 : | end | ||
250 : |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |