Debian Codegraph @

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

raw
1 functor MLTreeRewrite
2 (structure T : MLTREE
3 (* Traversal extensions *)
4 val sext : T.rewriter -> T.sext -> T.sext
5 val rext : T.rewriter -> T.rext -> T.rext
6 val fext : T.rewriter -> T.fext -> T.fext
7 val ccext : T.rewriter -> T.ccext -> T.ccext
8 ) : MLTREE_REWRITE =
9 struct
10 structure T = T
11
12 type rewriters =
13 { stm : T.stm -> T.stm,
14 rexp : T.rexp -> T.rexp,
15 fexp : T.fexp -> T.fexp,
16 ccexp : T.ccexp -> T.ccexp
17 }
18
19 fun rewrite{rexp=doRexp, fexp=doFexp, ccexp=doCCexp, stm=doStm} =
20 let fun stm s =
21 let val s =
22 case s of
23 T.MV(ty,dst,e) => T.MV(ty,dst,rexp e)
24 | T.CCMV(dst,e) => T.CCMV(dst,ccexp e)
25 | T.FMV(fty,dst,e) => T.FMV(fty,dst,fexp e)
26 | T.COPY _ => s
27 | T.FCOPY _ => s
28 | T.JMP(e,cf) => T.JMP(rexp e,cf)
29 | T.BCC(cc,l) => T.BCC(ccexp cc,l)
30 | T.CALL{funct,targets,defs,uses,region,pops} =>
31 T.CALL{funct=rexp funct,targets=targets,
32 defs=mlriscs defs,uses=mlriscs uses,
33 region=region,pops=pops}
34 | T.FLOW_TO(s,controlflow) => T.FLOW_TO(stm s,controlflow)
35 | T.RET _ => s
36 | T.IF(cc,yes,no) => T.IF(ccexp cc,stm yes,stm no)
37 | T.STORE(ty,ea,d,r) => T.STORE(ty,rexp ea,rexp d,r)
38 | T.FSTORE(fty,ea,d,r) => T.FSTORE(fty,rexp ea,fexp d,r)
39 | T.REGION(s,ctrl) => T.REGION(stm s,ctrl)
40 | T.SEQ s => T.SEQ(stms s)
41 | T.DEFINE _ => s
42 | T.ANNOTATION(s,an) => T.ANNOTATION(stm s,an)
43 | T.EXT s =>
44 T.EXT(sext {rexp=rexp, fexp=fexp, ccexp=ccexp, stm=stm} s)
45 | T.PHI _ => s
46 | T.SOURCE => s
47 | T.SINK => s
48 | T.RTL _ => s
49 | T.ASSIGN(ty,x,y) => T.ASSIGN(ty,rexp x, rexp y)
50 | T.LIVE ls => T.LIVE (mlriscs ls)
51 | T.KILL ks => T.KILL (mlriscs ks)
52 in doStm stm s end
53
54 and stms ss = map stm ss
55
56 and rexp e =
57 let val e = case e of
58 T.REG _ => e
59 | T.LI _ => e
60 | T.LABEL _ => e
61 | T.LABEXP _ => e
62 | T.CONST _ => e
63 | T.NEG(ty,x) => T.NEG(ty,rexp x)
64 | T.ADD(ty,x,y) => T.ADD(ty,rexp x,rexp y)
65 | T.SUB(ty,x,y) => T.SUB(ty,rexp x,rexp y)
66 | T.MULS(ty,x,y) => T.MULS(ty,rexp x,rexp y)
67 | T.DIVS(m,ty,x,y) => T.DIVS(m,ty,rexp x,rexp y)
68 | T.REMS(m,ty,x,y) => T.REMS(m,ty,rexp x,rexp y)
69 | T.MULU(ty,x,y) => T.MULU(ty,rexp x,rexp y)
70 | T.DIVU(ty,x,y) => T.DIVU(ty,rexp x,rexp y)
71 | T.REMU(ty,x,y) => T.REMU(ty,rexp x,rexp y)
72 | T.NEGT(ty,x) => T.NEGT(ty,rexp x)
73 | T.ADDT(ty,x,y) => T.ADDT(ty,rexp x,rexp y)
74 | T.SUBT(ty,x,y) => T.SUBT(ty,rexp x,rexp y)
75 | T.MULT(ty,x,y) => T.MULT(ty,rexp x,rexp y)
76 | T.DIVT(m,ty,x,y) => T.DIVT(m,ty,rexp x,rexp y)
77 | T.ANDB(ty,x,y) => T.ANDB(ty,rexp x,rexp y)
78 | T.ORB(ty,x,y) => T.ORB(ty,rexp x,rexp y)
79 | T.XORB(ty,x,y) => T.XORB(ty,rexp x,rexp y)
80 | T.EQVB(ty,x,y) => T.EQVB(ty,rexp x,rexp y)
81 | T.NOTB(ty,x) => T.NOTB(ty,rexp x)
82 | T.SRA(ty,x,y) => T.SRA(ty,rexp x,rexp y)
83 | T.SRL(ty,x,y) => T.SRL(ty,rexp x,rexp y)
84 | T.SLL(ty,x,y) => T.SLL(ty,rexp x,rexp y)
85 | T.SX(t,t',e) => T.SX(t,t',rexp e)
86 | T.ZX(t,t',e) => T.ZX(t,t',rexp e)
87 | T.CVTF2I(ty,mode,fty,e) => T.CVTF2I(ty,mode,fty,fexp e)
88 | T.COND(ty,cc,yes,no) => T.COND(ty,ccexp cc,rexp yes,rexp no)
89 | T.LOAD(ty,ea,r) => T.LOAD(ty,rexp ea,r)
90 | T.PRED(e,ctrl) => T.PRED(rexp e,ctrl)
91 | T.LET(s,e) => T.LET(stm s,rexp e)
92 | T.REXT(ty,e) =>
93 T.REXT(ty,rext {rexp=rexp, fexp=fexp, ccexp=ccexp, stm=stm} e)
94 | T.MARK(e,an) => T.MARK(rexp e,an)
95 | T.$(ty,k,e) => T.$(ty,k,rexp e)
96 | T.ARG _ => e
97 | T.PARAM _ => e
98 | T.BITSLICE(ty,sl,e) => T.BITSLICE(ty,sl,rexp e)
99 | T.??? => T.???
100 | T.OP(ty,oper,es) => T.OP(ty,oper,rexps es)
101 in doRexp rexp e end
102
103 and rexps es = map rexp es
104
105 and fexp e =
106 let val e = case e of
107 T.FREG _ => e
108 | T.FLOAD(fty,e,r) => T.FLOAD(fty,rexp e,r)
109 | T.FADD(fty,x,y) => T.FADD(fty,fexp x,fexp y)
110 | T.FSUB(fty,x,y) => T.FSUB(fty,fexp x,fexp y)
111 | T.FMUL(fty,x,y) => T.FMUL(fty,fexp x,fexp y)
112 | T.FDIV(fty,x,y) => T.FDIV(fty,fexp x,fexp y)
113 | T.FABS(fty,x) => T.FABS(fty,fexp x)
114 | T.FNEG(fty,x) => T.FNEG(fty,fexp x)
115 | T.FSQRT(fty,x) => T.FSQRT(fty,fexp x)
116 | T.FCOPYSIGN(fty,x,y) => T.FCOPYSIGN(fty,fexp x,fexp y)
117 | T.FCOND(fty,c,x,y) => T.FCOND(fty,ccexp c,fexp x,fexp y)
118 | T.CVTI2F(fty,ty,e) => T.CVTI2F(fty,ty,rexp e)
119 | T.CVTF2F(fty,fty',e) => T.CVTF2F(fty,fty',fexp e)
120 | T.FPRED(e,ctrl) => T.FPRED(fexp e,ctrl)
121 | T.FEXT(fty,e) =>
122 T.FEXT(fty,fext {rexp=rexp, fexp=fexp, ccexp=ccexp, stm=stm} e)
123 | T.FMARK(e,an) => T.FMARK(fexp e,an)
124 in doFexp fexp e end
125
126 and fexps es = map fexp es
127
128 and ccexp e =
129 let val e = case e of
130 T.CC _ => e
131 | T.FCC _ => e
132 | T.TRUE => e
133 | T.FALSE => e
134 | T.NOT e => T.NOT(ccexp e)
135 | T.AND(x,y) => T.AND(ccexp x,ccexp y)
136 | T.OR(x,y) => T.OR(ccexp x,ccexp y)
137 | T.XOR(x,y) => T.XOR(ccexp x,ccexp y)
138 | T.EQV(x,y) => T.EQV(ccexp x,ccexp y)
139 | T.CMP(ty,cond,x,y) => T.CMP(ty,cond,rexp x,rexp y)
140 | T.FCMP(ty,fcond,x,y) => T.FCMP(ty,fcond,fexp x,fexp y)
141 | T.CCMARK(e,an) => T.CCMARK(ccexp e,an)
142 | T.CCEXT(ty,e) =>
143 T.CCEXT(ty,ccext {rexp=rexp, fexp=fexp, ccexp=ccexp, stm=stm} e)
144 in doCCexp ccexp e end
145
146 and mlriscs m = map mlrisc m
147
148 and mlrisc m =
149 let val m =
150 case m of
151 T.CCR e => T.CCR(ccexp e)
152 | T.GPR e => T.GPR(rexp e)
153 | T.FPR e => T.FPR(fexp e)
154 in m end
155
156 in { rexp=rexp, fexp=fexp, ccexp=ccexp, stm=stm } end
157 end (* MLTreeFold *)
158