1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
|
(* hppaProps.sml
*
* COPYRIGHT (c) 1996 Bell Laboratories.
*
*)
functor HppaProps
( structure HppaInstr : HPPAINSTR
structure MLTreeEval : MLTREE_EVAL (* where T = HppaInstr.T *)
where type T.Basis.cond = HppaInstr.T.Basis.cond
and type T.Basis.div_rounding_mode = HppaInstr.T.Basis.div_rounding_mode
and type T.Basis.ext = HppaInstr.T.Basis.ext
and type T.Basis.fcond = HppaInstr.T.Basis.fcond
and type T.Basis.rounding_mode = HppaInstr.T.Basis.rounding_mode
and type T.Constant.const = HppaInstr.T.Constant.const
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) HppaInstr.T.Extension.ccx
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) HppaInstr.T.Extension.fx
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) HppaInstr.T.Extension.rx
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) HppaInstr.T.Extension.sx
and type T.I.div_rounding_mode = HppaInstr.T.I.div_rounding_mode
and type T.Region.region = HppaInstr.T.Region.region
and type T.ccexp = HppaInstr.T.ccexp
and type T.fexp = HppaInstr.T.fexp
(* and type T.labexp = HppaInstr.T.labexp *)
and type T.mlrisc = HppaInstr.T.mlrisc
and type T.oper = HppaInstr.T.oper
and type T.rep = HppaInstr.T.rep
and type T.rexp = HppaInstr.T.rexp
and type T.stm = HppaInstr.T.stm
structure MLTreeHash : MLTREE_HASH (* where T = HppaInstr.T *)
where type T.Basis.cond = HppaInstr.T.Basis.cond
and type T.Basis.div_rounding_mode = HppaInstr.T.Basis.div_rounding_mode
and type T.Basis.ext = HppaInstr.T.Basis.ext
and type T.Basis.fcond = HppaInstr.T.Basis.fcond
and type T.Basis.rounding_mode = HppaInstr.T.Basis.rounding_mode
and type T.Constant.const = HppaInstr.T.Constant.const
and type ('s,'r,'f,'c) T.Extension.ccx = ('s,'r,'f,'c) HppaInstr.T.Extension.ccx
and type ('s,'r,'f,'c) T.Extension.fx = ('s,'r,'f,'c) HppaInstr.T.Extension.fx
and type ('s,'r,'f,'c) T.Extension.rx = ('s,'r,'f,'c) HppaInstr.T.Extension.rx
and type ('s,'r,'f,'c) T.Extension.sx = ('s,'r,'f,'c) HppaInstr.T.Extension.sx
and type T.I.div_rounding_mode = HppaInstr.T.I.div_rounding_mode
and type T.Region.region = HppaInstr.T.Region.region
and type T.ccexp = HppaInstr.T.ccexp
and type T.fexp = HppaInstr.T.fexp
(* and type T.labexp = HppaInstr.T.labexp *)
and type T.mlrisc = HppaInstr.T.mlrisc
and type T.oper = HppaInstr.T.oper
and type T.rep = HppaInstr.T.rep
and type T.rexp = HppaInstr.T.rexp
and type T.stm = HppaInstr.T.stm
) : INSN_PROPERTIES =
struct
structure I = HppaInstr
structure C = HppaInstr.C
structure CB = CellsBasis
exception NegateConditional
fun error msg = MLRiscErrorMsg.error("HppaProps",msg)
datatype kind = IK_JUMP | IK_NOP | IK_INSTR | IK_COPY | IK_CALL
| IK_CALL_WITH_CUTS | IK_PHI | IK_SOURCE | IK_SINK
datatype target = LABELLED of Label.label | FALLTHROUGH | ESCAPES
val zeroR = Option.valOf(C.zeroReg CB.GP)
val r31 = C.Reg CB.GP 31
(*========================================================================
* Instruction Kinds
*========================================================================*)
(* Note: BLE and BL used to implement calls are not view as branches *)
fun instrKind(I.ANNOTATION{i, ...}) = instrKind i
| instrKind(I.COPY _) = IK_COPY
| instrKind(I.INSTR instr) =
(case instr
of (I.BCOND _) => IK_JUMP
| (I.BCONDI _) => IK_JUMP
| (I.BB _) => IK_JUMP
| (I.B _) => IK_JUMP
| (I.BE _) => IK_JUMP
| (I.FBRANCH _)=> IK_JUMP
| (I.BV _) => IK_JUMP
| (I.BLR _) => IK_JUMP
| (I.BREAK _) => IK_JUMP
| (I.NOP) => IK_NOP
| (I.BL{cutsTo=_::_,...}) => IK_CALL_WITH_CUTS
| (I.BL _) => IK_CALL
| (I.BLE{cutsTo=_::_,...}) => IK_CALL_WITH_CUTS
| (I.BLE _) => IK_CALL
| (I.PHI _) => IK_PHI
| (I.SOURCE _) => IK_SOURCE
| (I.SINK _) => IK_SINK
| _ => IK_INSTR)
| instrKind _ = error "instrKind"
fun moveInstr(I.COPY _) = true
| moveInstr(I.ANNOTATION{i,...}) = moveInstr i
| moveInstr _ = false
fun nop() = I.nop
(*========================================================================
* Parallel Move
*========================================================================*)
fun moveTmpR(I.COPY{tmp, ...}) =
(case tmp
of SOME(I.Direct r) => SOME r
| SOME(I.FDirect f) => SOME f
| _ => NONE
(*esac*))
| moveTmpR(I.ANNOTATION{i,...}) = moveTmpR i
| moveTmpR _ = NONE
fun moveDstSrc(I.COPY{dst, src, ...}) = (dst, src)
| moveDstSrc(I.ANNOTATION{i,...}) = moveDstSrc i
| moveDstSrc _ = error "moveDstSrc"
(*========================================================================
* Branches and Calls/Returns
*========================================================================*)
fun branchTargets(I.ANNOTATION{i,...}) = branchTargets i
| branchTargets(I.INSTR instr) =
(case instr
of (I.BCOND{t, ...}) => [LABELLED t, FALLTHROUGH]
| (I.BCONDI{t, ...}) => [LABELLED t, FALLTHROUGH]
| (I.BB{t, ...}) => [LABELLED t, FALLTHROUGH]
| (I.B{lab, ...}) => [LABELLED lab]
| (I.FBRANCH{t,...}) => [LABELLED t, FALLTHROUGH]
| (I.BE{labs=[],...}) => [ESCAPES]
| (I.BE{labs,...}) => map LABELLED labs
| (I.BV{labs=[],...}) => [ESCAPES]
| (I.BV{labs,...}) => map LABELLED labs
| (I.BLR{labs,...}) => map LABELLED labs
| (I.BL{cutsTo,...}) => FALLTHROUGH::map LABELLED cutsTo
| (I.BLE{cutsTo,...}) => FALLTHROUGH::map LABELLED cutsTo
| (I.BREAK _) => [ESCAPES]
| _ => error "branchTargets"
(*easc*))
| branchTargets _ = error "branchTargets"
fun jump label = I.b{lab=label,n=true}
val immedRange = {lo= ~8192, hi=8191}
fun loadImmed{immed,t} =
I.ldo{i=if #lo immedRange <= immed andalso immed <= #hi immedRange
then I.IMMED immed
else I.LabExp(I.T.LI(I.T.I.fromInt(32,immed)),I.F),b=zeroR,t=t}
fun loadOperand{opn,t} = I.ldo{i=opn,b=zeroR,t=t}
fun setJumpTarget(I.ANNOTATION{a,i}, l) = I.ANNOTATION{a=a, i=setJumpTarget(i,l)}
| setJumpTarget(I.INSTR(I.B{n,...}), L) = I.b{lab=L,n=n}
| setJumpTarget _ = error "setJumpTarget"
fun setBranchTargets{i=I.ANNOTATION{a,i}, t, f} =
I.ANNOTATION{a=a, i=setBranchTargets{i=i, t=t, f=f}}
| setBranchTargets{i=I.INSTR(I.BCOND{cmp,bc,r1,r2,t,f,n,nop}), t=T, f=F} =
I.bcond{cmp=cmp,bc=bc,r1=r1,r2=r2,t=T,f=F,n=n,nop=nop}
| setBranchTargets{i=I.INSTR(I.BCONDI{cmpi,bc,i,r2,t,f,n,nop=nop}),t=T, f=F} =
I.bcondi{cmpi=cmpi,bc=bc,i=i,r2=r2,t=T,f=F,n=n,nop=nop}
| setBranchTargets{i=I.INSTR(I.BB{bc,r,p,t,f,n,nop}), t=T, f=F} =
I.bb{bc=bc,r=r,p=p,t=T,f=F,n=n,nop=nop}
| setBranchTargets{i=I.INSTR(I.FBRANCH{cc,fmt,n,long,f1,f2,...}), t=T, f=F} =
I.fbranch{cc=cc,fmt=fmt,t=T,f=F,n=n,long=long,f1=f1,f2=f2}
| setBranchTargets _ = error "setBranchTargets"
(* negate the branch. Since the HPPA instruction representation tracks both
* the true and false target labels, we set the false label to be the
* old true label and set the true label to be the argument label.
*)
fun negateConditional (br, lab) = let
fun revFcond I.? = I.!?
| revFcond I.!<=> = I.<=>
| revFcond I.== = I.!=
| revFcond I.?= = I.!?=
| revFcond I.!<> = I.<>
| revFcond I.!?>= = I.?>=
| revFcond I.< = I.!<
| revFcond I.?< = I.!?<
| revFcond I.!>= = I.>=
| revFcond I.!?> = I.?>
| revFcond I.<= = I.!<=
| revFcond I.?<= = I.!?<=
| revFcond I.!> = I.>
| revFcond I.!?<= = I.?<=
| revFcond I.> = I.!>
| revFcond I.?> = I.!?>
| revFcond I.!<= = I.<=
| revFcond I.!?< = I.?<
| revFcond I.>= = I.!>=
| revFcond I.?>= = I.!?>=
| revFcond I.!< = I.<
| revFcond I.!?= = I.?=
| revFcond I.<> = I.!<>
| revFcond I.!= = I.==
| revFcond I.!? = I.?
| revFcond I.<=> = I.!<=>
| revFcond _ = error "revFcond"
fun negate (I.INSTR(I.BCOND{cmp,bc,r1,r2,t,f,n,nop})) = I.bcond{
bc=bc, r1=r1, r2=r2, t=lab, f=t, n=n, nop=nop,
cmp=case cmp of I.COMBT => I.COMBF | I.COMBF => I.COMBT
}
| negate (I.INSTR(I.BCONDI{cmpi,bc,i,r2,t,f,n,nop})) = I.bcondi{
bc=bc, i=i, r2=r2, t=lab, f=t, n=n, nop=nop,
cmpi=case cmpi of I.COMIBT => I.COMIBF | I.COMIBF => I.COMIBT
}
| negate (I.INSTR(I.BB{bc,r,p,t,f,n,nop})) = I.bb{
bc=case bc of I.BSET => I.BCLR | I.BCLR => I.BSET,
r=r,p=p,t=lab,f=t,n=n,nop=nop
}
| negate (I.INSTR(I.FBRANCH{cc,fmt,f1,f2,t,f,n,long})) =
I.fbranch{cc=revFcond cc,fmt=fmt,f1=f1,f2=f2,t=lab,f=t,n=n,long=long}
| negate (I.ANNOTATION{i,a}) = I.ANNOTATION{i=negate i,a=a}
| negate _ = raise NegateConditional
in
negate br
end
(*========================================================================
* Equality and hashing for operands
*========================================================================*)
fun hashFieldSel I.F = 0w0
| hashFieldSel I.S = 0w1
| hashFieldSel I.D = 0w2
| hashFieldSel I.R = 0w3
| hashFieldSel I.T = 0w4
| hashFieldSel I.P = 0w5
fun hashOpn(I.IMMED i) = Word.fromInt i
| hashOpn(I.LabExp(l,f)) = MLTreeHash.hash l + hashFieldSel f
| hashOpn(I.HILabExp(l,f)) = MLTreeHash.hash l + hashFieldSel f + 0w10000
| hashOpn(I.LOLabExp(l,f)) = MLTreeHash.hash l + hashFieldSel f + 0w20000
| hashOpn(I.REG r) = CB.hashCell r
fun eqOpn(I.IMMED i,I.IMMED j) = i = j
| eqOpn(I.REG x,I.REG y) = CB.sameColor(x,y)
| eqOpn(I.LabExp(a,b),I.LabExp(c,d)) =
b = d andalso MLTreeEval.==(a,c)
| eqOpn(I.HILabExp(a,b),I.HILabExp(c,d)) =
b = d andalso MLTreeEval.==(a,c)
| eqOpn(I.LOLabExp(a,b),I.LOLabExp(c,d)) =
b = d andalso MLTreeEval.==(a,c)
| eqOpn _ = false
(*========================================================================
* Definition and use (for register allocation mainly)
*========================================================================*)
fun defUseR instr = let
fun hppaDU instr = let
fun trap((I.ADDO | I.SUBO | I.SH1ADDO), d, u) = (d, u)
| trap(_, d, u) = (d, u)
fun trapi((I.ADDIO | I.SUBIO), d, u) = (d, u)
| trapi(_, d, u) = (d, u)
in
case instr
of I.STORE {b, r,...} => ([], [b,r])
| I.LOAD {l, r1, r2, t, ...} => ([t], [r1,r2])
| I.LOADI {li, r, t, ...} => ([t], [r])
| I.ARITH {a, r1, r2, t, ...} => trap(a, [t], [r1,r2])
| I.ARITHI {ai, r, t, ...} => trapi(ai, [t], [r])
| I.COMCLR_LDO{r1, r2, b, t1, t2, ...}=>
if CB.sameColor(t1,t2) then ([t1], [b, r1, r2])
else ([t1, t2], [b, r1, r2, t2])
| I.COMICLR_LDO{i1, r2, b, t1, t2, ...}=>
if CB.sameColor(t1,t2) then ([t1], [b, r2])
else ([t1, t2], [b, r2, t2])
| I.SHIFTV {r, t, ...} => ([t], [r])
| I.SHIFT {r, t, ...} => ([t], [r])
| I.BCOND {r1, r2, ...} => ([], [r1,r2])
| I.BCONDI {r2, ...} => ([], [r2])
| I.BB {r, ...} => ([], [r])
| I.BV {x, b, ...} => ([], [x,b])
| I.BE {b, ...} => ([], [b])
| I.BLR{x, t, ...} => ([t], [x])
| I.BL{defs, uses, ...} => (C.getReg defs, C.getReg uses)
| I.BLE{t, b, defs, uses, ...}=>
(r31 :: t :: C.getReg defs, b :: C.getReg uses)
| I.LDIL{i, t} => ([t], [])
| I.LDO{b, t, ...} => ([t], [b])
| I.MTCTL{r, t} => ([], [r])
| I.FSTORE {b, ...} => ([], [b])
| I.FSTOREX {b, x, ...} => ([], [b,x])
| I.FLOAD {b, ...} => ([], [b])
| I.FLOADX{b, x, ...} => ([], [b,x])
| _ => ([],[])
end
in
case instr
of I.ANNOTATION{i, ...} => defUseR i
| I.LIVE{regs, ...} => ([], C.getReg regs)
| I.KILL{regs, ...} => (C.getReg regs, [])
| I.INSTR(i) => hppaDU(i)
| I.COPY{k, dst, src, tmp, ...} => let
val (d,u) = case k of CB.GP => (dst, src) | _ => ([], [])
in
case tmp
of SOME(I.Direct r) => (r::d, u)
| SOME(I.Displace{base, ...}) => (* (d, base::u) *) (d, u)
| _ => (d,u)
end
end
fun defUseF instr = let
fun hppaDU instr =
case instr
of I.FSTORE {r, ...} => ([], [r])
| I.FSTOREX{r, ...} => ([], [r])
| I.FLOAD{t, ...} => ([t], [])
| I.FLOADX{t, ...} => ([t], [])
| I.FARITH {r1, r2, t, ...} => ([t], [r1,r2])
| I.FUNARY {f, t, ...} => ([t], [f])
| I.FCNV {f, t, ...} => ([t], [f])
| I.FBRANCH{f1, f2,...} => ([], [f1, f2])
| I.BL{defs, uses, ...} => (C.getFreg defs, C.getFreg uses)
| I.BLE{defs, uses, ...} => (C.getFreg defs, C.getFreg uses)
| _ => ([],[])
in
case instr
of I.ANNOTATION{i, ...} => defUseF i
| I.INSTR(i) => hppaDU(i)
| I.LIVE{regs, ...} => ([], C.getFreg regs)
| I.KILL{regs, ...} => (C.getFreg regs, [])
| I.COPY{k, dst, src, tmp, ...} => let
val (d, u) = case k of CB.FP => (dst, src) | _ => ([],[])
in
case tmp
of SOME(I.FDirect f) => (f::d, u)
| _ => (d, u)
end
end
fun defUse CB.GP = defUseR
| defUse CB.FP = defUseF
| defUse _ = error "defUse"
(*========================================================================
* Annotations
*========================================================================*)
fun getAnnotations(I.ANNOTATION{i,a}) =
let val (i,an) = getAnnotations i in (i,a::an) end
| getAnnotations i = (i,[])
fun annotate(i,a) = I.ANNOTATION{i=i,a=a}
(*========================================================================
* Replicate an instruction
*========================================================================*)
fun replicate(I.ANNOTATION{i,a}) = I.ANNOTATION{i=replicate i,a=a}
| replicate(I.COPY{k, sz, tmp=SOME _, dst, src}) = let
val tmp = case k of CB.GP => C.newReg()
| CB.FP => C.newFreg()
| _ => error "replicate: neither GP nor FP"
in
I.COPY{k=k, sz=sz, tmp=SOME(I.Direct(tmp)), dst=dst, src=src}
end
| replicate i = i
end
|