| 1 |
(* |
| 2 |
* This is a revamping of the MIPS32 instruction selection module |
| 3 |
* using the new MLTREE and instruction representation. I've dropped |
| 4 |
* the suffix 32 since we now support 64 bit datatypes. |
| 5 |
* |
| 6 |
* o How to simulate 32-bit in 64-mode |
| 7 |
* All 32-bit values are sign extended to 64-bits. |
| 8 |
* The working is similar to the Alpha and the Sparc architecture. |
| 9 |
* o I'm using the native multiplication instructions for even simple |
| 10 |
* multiply with a constant. Too lazy to add the multiply module for now. |
| 11 |
* |
| 12 |
* -- Allen |
| 13 |
*) |
| 14 |
|
| 15 |
functor MIPS |
| 16 |
(structure MIPSInstr : MIPSINSTR |
| 17 |
structure PseudoInstrs : MIPS_PSEUDO_INSTR |
| 18 |
structure ExtensionComp : MLTREE_EXTENSION_COMP |
| 19 |
where I = MIPSInstr |
| 20 |
sharing PseudoInstrs.I = MIPSInstr |
| 21 |
|
| 22 |
(* |
| 23 |
* MIPS architecture version |
| 24 |
*) |
| 25 |
datatype mipsVersion = I | II | III | IV |
| 26 |
val mipsVersion : mipsVersion ref |
| 27 |
) : MLTREECOMP = |
| 28 |
struct |
| 29 |
|
| 30 |
structure I = MIPSInstr |
| 31 |
structure T = I.T |
| 32 |
structure S = T.Stream |
| 33 |
structure R = T.Region |
| 34 |
structure C = MIPSInstr.C |
| 35 |
structure LE = I.LabelExp |
| 36 |
structure P = PseudoInstrs |
| 37 |
structure A = MLRiscAnnotations |
| 38 |
|
| 39 |
fun error msg = MLRiscErrorMsg.error("MIPS",msg) |
| 40 |
|
| 41 |
type instrStream = (I.instruction,C.cellset) T.stream |
| 42 |
type mltreeStream = (T.stm,T.mlrisc list) T.stream |
| 43 |
|
| 44 |
(* |
| 45 |
* This module is used to simulate operations of non-standard widths. |
| 46 |
*) |
| 47 |
structure Gen = MLTreeGen(structure T = T |
| 48 |
val intTy = 64 |
| 49 |
val naturalWidths = [32,64] |
| 50 |
datatype rep = SE | ZE | NEITHER |
| 51 |
val rep = SE (* sign extended? XXX *) |
| 52 |
) |
| 53 |
|
| 54 |
val zeroR = C.r0 |
| 55 |
val zeroOpnd = I.Reg zeroR |
| 56 |
val zeroImm = I.Imm 0 |
| 57 |
val zero = IntInf.fromInt 0 |
| 58 |
fun toInt i = T.I.toInt(32, i) |
| 59 |
|
| 60 |
fun selectInstructions |
| 61 |
(instrStream as |
| 62 |
S.STREAM{emit,beginCluster,endCluster,getAnnotations, |
| 63 |
defineLabel,entryLabel,pseudoOp,annotation, |
| 64 |
exitBlock,comment,...}) = |
| 65 |
let |
| 66 |
(* jmp+label were a trap is generated -- one per cluster *) |
| 67 |
val trapLabel = ref (NONE : (I.instruction * Label.label) option) |
| 68 |
|
| 69 |
(* Add an overflow trap *) |
| 70 |
fun trap() = () |
| 71 |
|
| 72 |
val newReg = C.newReg |
| 73 |
val newFreg = C.newFreg |
| 74 |
|
| 75 |
fun mark'(i,[]) = i |
| 76 |
| mark'(i,a::an) = mark'(I.ANNOTATION{i=i,a=a},an) |
| 77 |
fun mark(i,an) = emit(mark'(i,an)) |
| 78 |
|
| 79 |
fun move(s,d,an) = |
| 80 |
if C.sameCell(s,d) orelse C.sameCell(d,zeroR) then () else |
| 81 |
mark(I.COPY{dst=[d],src=[s],impl=ref NONE,tmp=NONE},an) |
| 82 |
|
| 83 |
fun fmove(s,d,an) = |
| 84 |
if C.sameCell(s,d) then () else |
| 85 |
mark(I.FCOPY{dst=[d],src=[s],impl=ref NONE,tmp=NONE},an) |
| 86 |
|
| 87 |
(* emit a copy *) |
| 88 |
fun copy(dst,src,an) = |
| 89 |
mark(I.COPY{dst=dst,src=src,impl=ref NONE, |
| 90 |
tmp=case dst of |
| 91 |
[_] => NONE | _ => SOME(I.Direct(newReg()))},an) |
| 92 |
|
| 93 |
(* emit a floating point copy *) |
| 94 |
fun fcopy(dst,src,an) = |
| 95 |
mark(I.FCOPY{dst=dst,src=src,impl=ref NONE, |
| 96 |
tmp=case dst of |
| 97 |
[_] => NONE | _ => SOME(I.FDirect(newFreg()))},an) |
| 98 |
|
| 99 |
(* emit load label expression *) |
| 100 |
fun loadLabexp(le,d,an) = |
| 101 |
mark(I.ARITH{oper=I.ADDU,rt=d,rs=zeroR,i=I.Lab le},an) |
| 102 |
|
| 103 |
(* emit load immediate *) |
| 104 |
fun loadImmed(n,d,an) = error "loadImmed" |
| 105 |
|
| 106 |
(* generate an expression and return the register that holds the result *) |
| 107 |
fun expr(T.REG(_,r)) = r |
| 108 |
| expr(e as T.LI i) = if i = zero then zeroR else expr' e |
| 109 |
| expr e = expr' e |
| 110 |
and expr' e = let val r = newReg() in doExpr(e,r,[]); r end |
| 111 |
|
| 112 |
(* convert an operand into a register *) |
| 113 |
and reduceOpn(I.Reg r) = r |
| 114 |
| reduceOpn(I.Imm 0) = zeroR |
| 115 |
| reduceOpn opn = |
| 116 |
let val d = newReg() |
| 117 |
in emit(I.ARITH{oper=I.ADDU,rt=d,rs=zeroR,i=opn}); d end (* XXX *) |
| 118 |
|
| 119 |
(* generate an operand *) |
| 120 |
and opn(T.REG(_,r)) = I.Reg r |
| 121 |
| opn(T.LI i) = if i = zero then zeroOpnd else error "opn" |
| 122 |
| opn(T.LABEXP le) = I.Lab le |
| 123 |
| opn e = I.Reg(expr e) |
| 124 |
|
| 125 |
(* compute base+displacement from an expression *) |
| 126 |
and addr exp = |
| 127 |
let fun toLexp(I.Imm i) = T.LI(IntInf.fromInt i) |
| 128 |
| toLexp(I.Lab le) = le |
| 129 |
| toLexp _ = error "addr.toLexp" |
| 130 |
|
| 131 |
fun add(t,n,I.Imm m) = |
| 132 |
I.Imm(toInt(T.I.ADD(t,n,IntInf.fromInt m))) |
| 133 |
| add(t,n,I.Lab le) = I.Lab(T.ADD(t,T.LI n,le)) |
| 134 |
| add(t,n,_) = error "addr.add" |
| 135 |
|
| 136 |
fun addLe(ty,le,I.Imm 0) = I.Lab le |
| 137 |
| addLe(ty,le,disp) = I.Lab(T.ADD(ty,le,toLexp disp)) |
| 138 |
|
| 139 |
fun sub(t,n,I.Imm m) = |
| 140 |
I.Imm(toInt(T.I.SUB(t,IntInf.fromInt m,n))) |
| 141 |
| sub(t,n,I.Lab le) = I.Lab(T.SUB(t,le,T.LI n)) |
| 142 |
| sub(t,n,_) = error "addr.sub" |
| 143 |
|
| 144 |
fun subLe(ty,le,I.Imm 0) = I.Lab le |
| 145 |
| subLe(ty,le,disp) = I.Lab(T.SUB(ty,le,toLexp disp)) |
| 146 |
|
| 147 |
(* Should really take into account of the address width XXX *) |
| 148 |
fun fold(T.ADD(t,e,T.LI n),disp) = fold(e,add(t,n,disp)) |
| 149 |
| fold(T.ADD(t,e,x as T.CONST _),disp) = fold(e,addLe(t,x,disp)) | fold(T.ADD(t,e,x as T.LABEL _),disp) = fold(e,addLe(t,x,disp)) | fold(T.ADD(t,e,T.LABEXP l),disp) = fold(e,addLe(t,l,disp)) |
| 150 |
| fold(T.ADD(t,T.LI n,e),disp) = fold(e, add(t,n,disp)) |
| 151 |
| fold(T.ADD(t,x as T.CONST _,e),disp) = fold(e,addLe(t,x,disp)) | fold(T.ADD(t,x as T.LABEL _,e),disp) = fold(e,addLe(t,x,disp)) | fold(T.ADD(t,T.LABEXP l,e),disp) = fold(e,addLe(t,l,disp)) |
| 152 |
| fold(T.SUB(t,e,T.LI n),disp) = fold(e,sub(t,n,disp)) |
| 153 |
| fold(T.SUB(t,e,x as T.CONST _),disp) = fold(e,subLe(t,x,disp)) | fold(T.SUB(t,e,x as T.LABEL _),disp) = fold(e,subLe(t,x,disp)) | fold(T.SUB(t,e,T.LABEXP l),disp) = fold(e,subLe(t,l,disp)) |
| 154 |
| fold(e,disp) = (expr e,disp) |
| 155 |
|
| 156 |
in fold(exp, zeroImm) |
| 157 |
end |
| 158 |
|
| 159 |
(* compute addressing mode for floating point. |
| 160 |
* In MIPS IV mode we also support register+register mode. |
| 161 |
*) |
| 162 |
and faddr exp = |
| 163 |
case !mipsVersion of |
| 164 |
IV => |
| 165 |
(case exp of |
| 166 |
T.ADD(_,T.REG(_,b),T.REG(_,i)) => (b, I.Reg i) |
| 167 |
| _ => addr exp |
| 168 |
) |
| 169 |
| _ => addr exp |
| 170 |
|
| 171 |
(* generate an arithmetic operator *) |
| 172 |
and arith(oper,a,b,d,an) = |
| 173 |
mark(I.ARITH{oper=oper,rt=d,rs=expr a,i=I.Reg(expr b)},an) |
| 174 |
|
| 175 |
(* generate a commutative arithmetic operator |
| 176 |
* that can take an immediate operand |
| 177 |
*) |
| 178 |
and commarithi(oper,a,b,d,an) = |
| 179 |
let val (a, b) = |
| 180 |
case b of |
| 181 |
(T.LI _ | T.CONST _ | T.LABEXP _ | T.LABEL _) => (b, a) |
| 182 |
| _ => (a, b) |
| 183 |
in mark(I.ARITH{oper=oper,rt=d,rs=expr a,i=opn b},an) |
| 184 |
end |
| 185 |
|
| 186 |
(* generate an unary arithmetic operator *) |
| 187 |
and unary(oper,a,d,an) = |
| 188 |
mark(I.UNARY{oper=oper,rt=d,rs=expr a},an) |
| 189 |
|
| 190 |
(* generate a load *) |
| 191 |
and load(ld,ea,rt,mem,an) = |
| 192 |
let val (base,offset) = addr ea |
| 193 |
in mark(I.LOAD{l=ld,rt=rt,b=base,d=offset,mem=mem},an) end |
| 194 |
|
| 195 |
(* generate a store *) |
| 196 |
and store(st,ea,data,mem,an) = |
| 197 |
let val (base,offset) = addr ea |
| 198 |
in mark(I.STORE{s=st,rs=expr data,b=base,d=offset,mem=mem},an) end |
| 199 |
|
| 200 |
(* generate multiply. |
| 201 |
* Note: low order result is in the LO register |
| 202 |
*) |
| 203 |
and multiply(oper,a,b,d,an) = |
| 204 |
(mark(I.MULTIPLY{oper=oper,rs=expr a,rt=expr b},an); |
| 205 |
emit(I.MFLO d) |
| 206 |
) |
| 207 |
|
| 208 |
(* generate divide |
| 209 |
* Note: quotient in LO; remainder is in HI |
| 210 |
*) |
| 211 |
and divide(oper,a,b,d,an) = |
| 212 |
(mark(I.DIVIDE{oper=oper,rs=expr a,rt=expr b},an); |
| 213 |
emit(I.MFLO d) |
| 214 |
) |
| 215 |
|
| 216 |
and rem(oper,a,b,d,an) = |
| 217 |
(mark(I.DIVIDE{oper=oper,rs=expr a,rt=expr b},an); |
| 218 |
emit(I.MFHI d) |
| 219 |
) |
| 220 |
|
| 221 |
(* generate an expression that targets register d *) |
| 222 |
and doExpr(exp,d,an) = |
| 223 |
case exp of |
| 224 |
T.REG(_,r) => move(r,d,an) |
| 225 |
| T.LI n => loadImmed(n,d,an) |
| 226 |
| T.LABEL _ => loadLabexp(exp,d,an) |
| 227 |
| T.CONST _ => loadLabexp(exp,d,an) |
| 228 |
| T.LABEXP le => loadLabexp(le,d,an) |
| 229 |
|
| 230 |
(* 32 bit support *) |
| 231 |
| T.NEG(32, a) => unary(I.NEGU,a,d,an) |
| 232 |
| T.ADD(32, a, b) => commarithi(I.ADDU,a,b,d,an) |
| 233 |
| T.SUB(32, a, b) => arith(I.SUBU,a,b,d,an) |
| 234 |
| T.MULS(32, a, b) => multiply(I.MULT,a,b,d,an) |
| 235 |
| T.MULU(32, a, b) => multiply(I.MULTU,a,b,d,an) |
| 236 |
| T.DIVS(32, a, b) => divide(I.DIV,a,b,d,an) |
| 237 |
| T.DIVU(32, a, b) => divide(I.DIVU,a,b,d,an) |
| 238 |
| T.QUOTS(32, a, b) => error "quots" |
| 239 |
| T.REMS(32, a, b) => rem(I.DIV,a,b,d,an) |
| 240 |
| T.REMU(32, a, b) => rem(I.DIVU,a,b,d,an) |
| 241 |
|
| 242 |
| T.NEGT(32, a) => unary(I.NEG,a,d,an) |
| 243 |
| T.ADDT(32, a, b) => commarithi(I.ADD,a,b,d,an) |
| 244 |
| T.SUBT(32, a, b) => arith(I.SUB,a,b,d,an) |
| 245 |
| T.MULT(32, a, b) => error "mult" |
| 246 |
| T.DIVT(32, a, b) => error "divt" |
| 247 |
| T.QUOTT(32, a, b) => error "quott" |
| 248 |
| T.REMT(32, a, b) => error "remt" |
| 249 |
|
| 250 |
| T.SLL(32, a, b) => arith(I.SLL,a,b,d,an) |
| 251 |
| T.SRL(32, a, b) => arith(I.SRL,a,b,d,an) |
| 252 |
| T.SRA(32, a, b) => arith(I.SRA,a,b,d,an) |
| 253 |
|
| 254 |
(* 64 bit support *) |
| 255 |
| T.NEG(64, a) => unary(I.DNEGU,a,d,an) |
| 256 |
| T.ADD(64, a, b) => arith(I.DADDU,a,b,d,an) |
| 257 |
| T.SUB(64, a, b) => arith(I.DSUBU,a,b,d,an) |
| 258 |
| T.MULS(64, a, b) => multiply(I.DMULT,a,b,d,an) |
| 259 |
| T.MULU(64, a, b) => multiply(I.DMULTU,a,b,d,an) |
| 260 |
| T.DIVS(64, a, b) => divide(I.DDIV,a,b,d,an) |
| 261 |
| T.DIVU(64, a, b) => divide(I.DDIVU,a,b,d,an) |
| 262 |
| T.QUOTS(64, a, b) => error "quots" |
| 263 |
| T.REMS(64, a, b) => rem(I.DDIV,a,b,d,an) |
| 264 |
| T.REMU(64, a, b) => rem(I.DDIVU,a,b,d,an) |
| 265 |
|
| 266 |
| T.NEGT(64, a) => unary(I.DNEG,a,d,an) |
| 267 |
| T.ADDT(64, a, b) => commarithi(I.DADD,a,b,d,an) |
| 268 |
| T.SUBT(64, a, b) => arith(I.DSUB,a,b,d,an) |
| 269 |
| T.MULT(64, a, b) => error "mult" |
| 270 |
| T.DIVT(64, a, b) => error "divt" |
| 271 |
| T.QUOTT(64, a, b) => error "quott" |
| 272 |
| T.REMT(64, a, b) => error "remt" |
| 273 |
|
| 274 |
| T.SLL(64, a, b) => arith(I.DSLL,a,b,d,an) |
| 275 |
| T.SRL(64, a, b) => arith(I.DSRL,a,b,d,an) |
| 276 |
| T.SRA(64, a, b) => arith(I.DSRA,a,b,d,an) |
| 277 |
|
| 278 |
(* Bit ops *) |
| 279 |
| T.ANDB(_, a, b) => commarithi(I.AND,a,b,d,an) |
| 280 |
| T.ORB(_, a, b) => commarithi(I.OR,a,b,d,an) |
| 281 |
| T.XORB(_, a, b) => commarithi(I.XOR,a,b,d,an) |
| 282 |
|
| 283 |
(* Conditional move *) |
| 284 |
| T.COND(_, cc, yes, no) => error "cond" |
| 285 |
|
| 286 |
(* Loads *) |
| 287 |
| T.SX(_,_,T.LOAD(8,ea,mem)) => load(I.LB, ea, d, mem, an) |
| 288 |
| T.SX(_,_,T.LOAD(16,ea,mem)) => load(I.LH, ea, d, mem, an) |
| 289 |
| T.SX(_,_,T.LOAD(32,ea,mem)) => load(I.LW, ea, d, mem, an) |
| 290 |
| T.ZX(_,_,T.LOAD(8,ea,mem)) => load(I.LBU, ea, d, mem, an) |
| 291 |
| T.ZX(_,_,T.LOAD(16,ea,mem)) => load(I.LHU, ea, d, mem, an) |
| 292 |
| T.LOAD(8, ea, mem) => load(I.LBU, ea, d, mem, an) |
| 293 |
| T.LOAD(16, ea, mem) => load(I.LHU, ea, d, mem, an) |
| 294 |
| T.LOAD(32, ea, mem) => load(I.LW, ea, d, mem, an) |
| 295 |
| T.LOAD(64, ea, mem) => load(I.LD, ea, d, mem, an) |
| 296 |
|
| 297 |
(* Annotations *) |
| 298 |
| T.MARK(e, A.MARKREG f) => (f d; doExpr(e, d, an)) |
| 299 |
| T.MARK(e, a) => doExpr(e, d, a::an) |
| 300 |
|
| 301 |
(* Control dependence *) |
| 302 |
| T.PRED(e,c) => doExpr(e, d, A.CTRLUSE c::an) |
| 303 |
|
| 304 |
(* Extension *) |
| 305 |
| T.REXT e => ExtensionComp.compileRext (reducer()) {e=e, rd=d, an=an} |
| 306 |
|
| 307 |
(* Defaults *) |
| 308 |
| e => doExpr(Gen.compileRexp e,d,an) |
| 309 |
|
| 310 |
(* generate a floating point expression |
| 311 |
* return the register that holds the result |
| 312 |
*) |
| 313 |
and fexpr(T.FREG(_,r)) = r |
| 314 |
| fexpr e = let val d = newFreg() in doFexpr(e,d,[]); d end |
| 315 |
|
| 316 |
and farith(oper,a,b,d,an) = |
| 317 |
mark(I.FARITH{oper=oper,fs1=fexpr a,fs2=fexpr b,ft=d},an) |
| 318 |
and funary(oper,a,d,an) = |
| 319 |
mark(I.FUNARY{oper=oper,fs=fexpr a,ft=d},an) |
| 320 |
and farith3(oper,a,b,c,d,an) = |
| 321 |
mark(I.FARITH3{oper=oper,fs1=fexpr a,fs2=fexpr b,fs3=fexpr c,ft=d},an) |
| 322 |
and fload(ld,ea,fd,mem,an) = |
| 323 |
let val (base,offset) = faddr ea |
| 324 |
in mark(I.FLOAD{l=ld,ft=fd,b=base,d=offset,mem=mem},an) end |
| 325 |
and fstore(st,ea,fs,mem,an) = |
| 326 |
let val (base,offset) = faddr ea |
| 327 |
in mark(I.FSTORE{s=st,fs=fexpr fs,b=base,d=offset,mem=mem},an) end |
| 328 |
|
| 329 |
(* generate a floating point expression that targets register d *) |
| 330 |
and doFexpr(e,d,an) = |
| 331 |
case e of |
| 332 |
T.FREG(_,f) => fmove(f,d,an) |
| 333 |
|
| 334 |
(* single precision support *) |
| 335 |
| T.FADD(32,a,b) => farith(I.ADD_S,a,b,d,an) |
| 336 |
| T.FSUB(32,a,b) => farith(I.SUB_S,a,b,d,an) |
| 337 |
| T.FMUL(32,a,b) => farith(I.MUL_S,a,b,d,an) |
| 338 |
| T.FDIV(32,a,b) => farith(I.DIV_S,a,b,d,an) |
| 339 |
| T.FABS(32,a) => funary(I.ABS_S,a,d,an) |
| 340 |
| T.FNEG(32,a) => funary(I.NEG_S,a,d,an) |
| 341 |
| T.FSQRT(32,a) => funary(I.SQRT_S,a,d,an) |
| 342 |
|
| 343 |
(* double precision support *) |
| 344 |
| T.FADD(64,a,b) => farith(I.ADD_D,a,b,d,an) |
| 345 |
| T.FSUB(64,a,b) => farith(I.SUB_D,a,b,d,an) |
| 346 |
| T.FMUL(64,a,b) => farith(I.MUL_D,a,b,d,an) |
| 347 |
| T.FDIV(64,a,b) => farith(I.DIV_D,a,b,d,an) |
| 348 |
| T.FABS(64,a) => funary(I.ABS_D,a,d,an) |
| 349 |
| T.FNEG(64,a) => funary(I.NEG_D,a,d,an) |
| 350 |
| T.FSQRT(64,a) => funary(I.SQRT_D,a,d,an) |
| 351 |
|
| 352 |
(* copy sign *) |
| 353 |
| T.FCOPYSIGN _ => error "fcopysign" |
| 354 |
|
| 355 |
(* loads *) |
| 356 |
| T.FLOAD(32,ea,mem) => fload(I.LWC1,ea,d,mem,an) |
| 357 |
| T.FLOAD(64,ea,mem) => fload(I.LDC1,ea,d,mem,an) |
| 358 |
|
| 359 |
(* floating/floating conversion |
| 360 |
* Note: it is not necessary to convert single precision |
| 361 |
* to double on the alpha. |
| 362 |
*) |
| 363 |
| T.CVTF2F(to,from,e) => |
| 364 |
if from = to then doFexpr(e, d, an) |
| 365 |
else |
| 366 |
(case (to,from) of |
| 367 |
(32,64) => funary(I.CVT_SD,e,d,an) |
| 368 |
| (64,32) => funary(I.CVT_DS,e,d,an) (* use normal rounding *) |
| 369 |
| _ => error "CVTF2F" |
| 370 |
) |
| 371 |
|
| 372 |
(* integer -> floating point conversion *) |
| 373 |
| T.CVTI2F(fty,ty,e) => error "cvti2f" |
| 374 |
|
| 375 |
| T.FMARK(e,A.MARKREG f) => (f d; doFexpr(e,d,an)) |
| 376 |
| T.FMARK(e,a) => doFexpr(e,d,a::an) |
| 377 |
| T.FPRED(e,c) => doFexpr(e, d, A.CTRLUSE c::an) |
| 378 |
| T.FEXT e => ExtensionComp.compileFext (reducer()) {e=e, fd=d, an=an} |
| 379 |
| _ => error "doFexpr" |
| 380 |
|
| 381 |
(* generate an unconditional branch *) |
| 382 |
and goto(lab,an) = mark(I.J{lab=lab,nop=true},an) |
| 383 |
|
| 384 |
(* generate an unconditional jump *) |
| 385 |
and jmp(e,labels,an) = mark(I.JR{rs=expr e,labels=labels,nop=true},an) |
| 386 |
|
| 387 |
(* generate a call instruction *) |
| 388 |
and call(ea,flow,defs,uses,cutsTo,mem,an) = |
| 389 |
let val defs=cellset defs |
| 390 |
val uses=cellset uses |
| 391 |
val instr = |
| 392 |
case ea of |
| 393 |
(T.LABEL lab) => |
| 394 |
I.JAL{lab=lab,defs=defs,uses=uses,cutsTo=cutsTo, |
| 395 |
mem=mem,nop=true} |
| 396 |
| _ => I.JALR{rt=C.linkR, rs=expr ea, |
| 397 |
defs=defs,uses=uses,cutsTo=cutsTo,mem=mem,nop=true} |
| 398 |
in mark(instr,an) |
| 399 |
end |
| 400 |
|
| 401 |
(* generate a return instruction *) |
| 402 |
and ret(an) = mark(I.RET{nop=true},an) |
| 403 |
|
| 404 |
(* generate an branch instruction *) |
| 405 |
and branch(e,label,an) = error "branch" |
| 406 |
|
| 407 |
(* generate a comparison *) |
| 408 |
and cmp(ty,cond,e1,e2,d,an) = error "cmp" |
| 409 |
|
| 410 |
and doCCexpr(T.CC(_,r),d,an) = move(r,d,an) |
| 411 |
| doCCexpr(T.FCC(_,r),d,an) = fmove(r,d,an) |
| 412 |
| doCCexpr(T.CMP(ty,cond,e1,e2),d,an) = cmp(ty,cond,e1,e2,d,an) |
| 413 |
| doCCexpr(T.FCMP(fty,cond,e1,e2),d,an) = error "doCCexpr.fcmp" |
| 414 |
| doCCexpr(T.CCMARK(e,A.MARKREG f),d,an) = (f d; doCCexpr(e,d,an)) |
| 415 |
| doCCexpr(T.CCMARK(e,a),d,an) = doCCexpr(e,d,a::an) |
| 416 |
| doCCexpr(T.CCEXT e,d,an) = |
| 417 |
ExtensionComp.compileCCext (reducer()) {e=e, ccd=d, an=an} |
| 418 |
| doCCexpr _ = error "doCCexpr" |
| 419 |
|
| 420 |
and ccExpr(T.CC(_,r)) = r |
| 421 |
| ccExpr(T.FCC(_,r)) = r |
| 422 |
| ccExpr e = let val d = newReg() |
| 423 |
in doCCexpr(e,d,[]); d end |
| 424 |
|
| 425 |
(* compile a statement *) |
| 426 |
and stmt(s,an) = |
| 427 |
case s of |
| 428 |
T.MV(ty,r,e) => doExpr(e,r,an) |
| 429 |
| T.FMV(ty,r,e) => doFexpr(e,r,an) |
| 430 |
| T.CCMV(r,e) => doCCexpr(e,r,an) |
| 431 |
| T.COPY(ty,dst,src) => copy(dst,src,an) |
| 432 |
| T.FCOPY(ty,dst,src) => fcopy(dst,src,an) |
| 433 |
| T.JMP(T.LABEL lab,_) => goto(lab,an) |
| 434 |
| T.JMP(e,labs) => jmp(e,labs,an) |
| 435 |
| T.BCC(cc,lab) => branch(cc,lab,an) |
| 436 |
| T.CALL{funct,targets,defs,uses,region} => |
| 437 |
call(funct,targets,defs,uses,[],region,an) |
| 438 |
| T.FLOW_TO(T.CALL{funct,targets,defs,uses,region},cuts)=> |
| 439 |
call(funct,targets,defs,uses,cuts,region,an) |
| 440 |
| T.RET _ => ret(an) |
| 441 |
| T.STORE(8,ea,data,mem) => store(I.SB,ea,data,mem,an) |
| 442 |
| T.STORE(16,ea,data,mem) => store(I.SH,ea,data,mem,an) |
| 443 |
| T.STORE(32,ea,data,mem) => store(I.SW,ea,data,mem,an) |
| 444 |
| T.STORE(64,ea,data,mem) => store(I.SD,ea,data,mem,an) |
| 445 |
| T.FSTORE(32,ea,data,mem) => fstore(I.SWC1,ea,data,mem,an) |
| 446 |
| T.FSTORE(64,ea,data,mem) => fstore(I.SDC1,ea,data,mem,an) |
| 447 |
| T.DEFINE l => defineLabel l |
| 448 |
| T.LIVE S => mark'(I.LIVE{regs=cellset S,spilled=C.empty},an) |
| 449 |
| T.KILL S => mark'(I.KILL{regs=cellset S,spilled=C.empty},an) |
| 450 |
| T.ANNOTATION(s,a) => stmt(s,a::an) |
| 451 |
| T.EXT s => ExtensionComp.compileSext (reducer()) {stm=s,an=an} |
| 452 |
| s => doStmts (Gen.compileStm s) |
| 453 |
|
| 454 |
and reducer() = |
| 455 |
T.REDUCER{reduceRexp = expr, |
| 456 |
reduceFexp = fexpr, |
| 457 |
reduceCCexp = ccExpr, |
| 458 |
reduceStm = stmt, |
| 459 |
operand = opn, |
| 460 |
reduceOperand = reduceOpn, |
| 461 |
addressOf = addr, |
| 462 |
emit = mark, |
| 463 |
instrStream = instrStream, |
| 464 |
mltreeStream = self() |
| 465 |
} |
| 466 |
|
| 467 |
and doStmt s = stmt(s,[]) |
| 468 |
and doStmts ss = app doStmt ss |
| 469 |
|
| 470 |
(* convert mlrisc to cellset: |
| 471 |
* condition code registers are mapped onto general registers |
| 472 |
*) |
| 473 |
and cellset mlrisc = |
| 474 |
let fun g([],acc) = acc |
| 475 |
| g(T.GPR(T.REG(_,r))::regs,acc) = g(regs,C.addReg(r,acc)) |
| 476 |
| g(T.FPR(T.FREG(_,f))::regs,acc) = g(regs,C.addFreg(f,acc)) |
| 477 |
| g(T.CCR(T.CC(_,cc))::regs,acc) = g(regs,C.addReg(cc,acc)) |
| 478 |
| g(T.CCR(T.FCC(_,cc))::regs,acc) = g(regs,C.addReg(cc,acc)) |
| 479 |
| g(_::regs, acc) = g(regs, acc) |
| 480 |
in g(mlrisc, C.empty) end |
| 481 |
|
| 482 |
and beginCluster' n = |
| 483 |
(trapLabel := NONE; |
| 484 |
beginCluster n |
| 485 |
) |
| 486 |
|
| 487 |
and endCluster' a = |
| 488 |
(case !trapLabel of |
| 489 |
NONE => () |
| 490 |
| SOME(_, lab) => (defineLabel lab) (* XXX *) |
| 491 |
(*esac*); |
| 492 |
endCluster a |
| 493 |
) |
| 494 |
|
| 495 |
and self() = |
| 496 |
S.STREAM |
| 497 |
{ beginCluster = beginCluster', |
| 498 |
endCluster = endCluster', |
| 499 |
emit = doStmt, |
| 500 |
pseudoOp = pseudoOp, |
| 501 |
defineLabel = defineLabel, |
| 502 |
entryLabel = entryLabel, |
| 503 |
comment = comment, |
| 504 |
annotation = annotation, |
| 505 |
getAnnotations = getAnnotations, |
| 506 |
exitBlock = fn regs => exitBlock(cellset regs) |
| 507 |
} |
| 508 |
in self() |
| 509 |
end |
| 510 |
|
| 511 |
end |
| 512 |
|