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