buzbee | efc6369 | 2012-11-14 16:31:52 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* This file contains codegen for the Mips ISA */ |
| 18 | |
| 19 | #include "oat/runtime/oat_support_entrypoints.h" |
| 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | /* |
| 24 | * Compare two 64-bit values |
| 25 | * x = y return 0 |
| 26 | * x < y return -1 |
| 27 | * x > y return 1 |
| 28 | * |
| 29 | * slt t0, x.hi, y.hi; # (x.hi < y.hi) ? 1:0 |
| 30 | * sgt t1, x.hi, y.hi; # (y.hi > x.hi) ? 1:0 |
| 31 | * subu res, t0, t1 # res = -1:1:0 for [ < > = ] |
| 32 | * bnez res, finish |
| 33 | * sltu t0, x.lo, y.lo |
| 34 | * sgtu r1, x.lo, y.lo |
| 35 | * subu res, t0, t1 |
| 36 | * finish: |
| 37 | * |
| 38 | */ |
| 39 | void genCmpLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 40 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 41 | { |
| 42 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 43 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 44 | int t0 = oatAllocTemp(cUnit); |
| 45 | int t1 = oatAllocTemp(cUnit); |
| 46 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 47 | newLIR3(cUnit, kMipsSlt, t0, rlSrc1.highReg, rlSrc2.highReg); |
| 48 | newLIR3(cUnit, kMipsSlt, t1, rlSrc2.highReg, rlSrc1.highReg); |
| 49 | newLIR3(cUnit, kMipsSubu, rlResult.lowReg, t1, t0); |
| 50 | LIR* branch = opCmpImmBranch(cUnit, kCondNe, rlResult.lowReg, 0, NULL); |
| 51 | newLIR3(cUnit, kMipsSltu, t0, rlSrc1.lowReg, rlSrc2.lowReg); |
| 52 | newLIR3(cUnit, kMipsSltu, t1, rlSrc2.lowReg, rlSrc1.lowReg); |
| 53 | newLIR3(cUnit, kMipsSubu, rlResult.lowReg, t1, t0); |
| 54 | oatFreeTemp(cUnit, t0); |
| 55 | oatFreeTemp(cUnit, t1); |
| 56 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
| 57 | branch->target = (LIR*)target; |
| 58 | storeValue(cUnit, rlDest, rlResult); |
| 59 | } |
| 60 | |
| 61 | LIR* opCmpBranch(CompilationUnit* cUnit, ConditionCode cond, int src1, |
| 62 | int src2, LIR* target) |
| 63 | { |
| 64 | LIR* branch; |
| 65 | MipsOpCode sltOp; |
| 66 | MipsOpCode brOp; |
| 67 | bool cmpZero = false; |
| 68 | bool swapped = false; |
| 69 | switch (cond) { |
| 70 | case kCondEq: |
| 71 | brOp = kMipsBeq; |
| 72 | cmpZero = true; |
| 73 | break; |
| 74 | case kCondNe: |
| 75 | brOp = kMipsBne; |
| 76 | cmpZero = true; |
| 77 | break; |
| 78 | case kCondCc: |
| 79 | sltOp = kMipsSltu; |
| 80 | brOp = kMipsBnez; |
| 81 | break; |
| 82 | case kCondCs: |
| 83 | sltOp = kMipsSltu; |
| 84 | brOp = kMipsBeqz; |
| 85 | break; |
| 86 | case kCondGe: |
| 87 | sltOp = kMipsSlt; |
| 88 | brOp = kMipsBeqz; |
| 89 | break; |
| 90 | case kCondGt: |
| 91 | sltOp = kMipsSlt; |
| 92 | brOp = kMipsBnez; |
| 93 | swapped = true; |
| 94 | break; |
| 95 | case kCondLe: |
| 96 | sltOp = kMipsSlt; |
| 97 | brOp = kMipsBeqz; |
| 98 | swapped = true; |
| 99 | break; |
| 100 | case kCondLt: |
| 101 | sltOp = kMipsSlt; |
| 102 | brOp = kMipsBnez; |
| 103 | break; |
| 104 | case kCondHi: // Gtu |
| 105 | sltOp = kMipsSltu; |
| 106 | brOp = kMipsBnez; |
| 107 | swapped = true; |
| 108 | break; |
| 109 | default: |
| 110 | LOG(FATAL) << "No support for ConditionCode: " << (int) cond; |
| 111 | return NULL; |
| 112 | } |
| 113 | if (cmpZero) { |
| 114 | branch = newLIR2(cUnit, brOp, src1, src2); |
| 115 | } else { |
| 116 | int tReg = oatAllocTemp(cUnit); |
| 117 | if (swapped) { |
| 118 | newLIR3(cUnit, sltOp, tReg, src2, src1); |
| 119 | } else { |
| 120 | newLIR3(cUnit, sltOp, tReg, src1, src2); |
| 121 | } |
| 122 | branch = newLIR1(cUnit, brOp, tReg); |
| 123 | oatFreeTemp(cUnit, tReg); |
| 124 | } |
| 125 | branch->target = target; |
| 126 | return branch; |
| 127 | } |
| 128 | |
| 129 | LIR* opCmpImmBranch(CompilationUnit* cUnit, ConditionCode cond, int reg, |
| 130 | int checkValue, LIR* target) |
| 131 | { |
| 132 | LIR* branch; |
| 133 | if (checkValue != 0) { |
| 134 | // TUNING: handle s16 & kCondLt/Mi case using slti |
| 135 | int tReg = oatAllocTemp(cUnit); |
| 136 | loadConstant(cUnit, tReg, checkValue); |
| 137 | branch = opCmpBranch(cUnit, cond, reg, tReg, target); |
| 138 | oatFreeTemp(cUnit, tReg); |
| 139 | return branch; |
| 140 | } |
| 141 | MipsOpCode opc; |
| 142 | switch (cond) { |
| 143 | case kCondEq: opc = kMipsBeqz; break; |
| 144 | case kCondGe: opc = kMipsBgez; break; |
| 145 | case kCondGt: opc = kMipsBgtz; break; |
| 146 | case kCondLe: opc = kMipsBlez; break; |
| 147 | //case KCondMi: |
| 148 | case kCondLt: opc = kMipsBltz; break; |
| 149 | case kCondNe: opc = kMipsBnez; break; |
| 150 | default: |
| 151 | // Tuning: use slti when applicable |
| 152 | int tReg = oatAllocTemp(cUnit); |
| 153 | loadConstant(cUnit, tReg, checkValue); |
| 154 | branch = opCmpBranch(cUnit, cond, reg, tReg, target); |
| 155 | oatFreeTemp(cUnit, tReg); |
| 156 | return branch; |
| 157 | } |
| 158 | branch = newLIR1(cUnit, opc, reg); |
| 159 | branch->target = target; |
| 160 | return branch; |
| 161 | } |
| 162 | |
| 163 | LIR* opRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc) |
| 164 | { |
| 165 | #ifdef __mips_hard_float |
| 166 | if (MIPS_FPREG(rDest) || MIPS_FPREG(rSrc)) |
| 167 | return fpRegCopy(cUnit, rDest, rSrc); |
| 168 | #endif |
| 169 | LIR* res = rawLIR(cUnit, cUnit->currentDalvikOffset, kMipsMove, |
| 170 | rDest, rSrc); |
| 171 | if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && rDest == rSrc) { |
| 172 | res->flags.isNop = true; |
| 173 | } |
| 174 | return res; |
| 175 | } |
| 176 | |
| 177 | LIR* opRegCopy(CompilationUnit *cUnit, int rDest, int rSrc) |
| 178 | { |
| 179 | LIR *res = opRegCopyNoInsert(cUnit, rDest, rSrc); |
| 180 | oatAppendLIR(cUnit, (LIR*)res); |
| 181 | return res; |
| 182 | } |
| 183 | |
| 184 | void opRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi, |
| 185 | int srcLo, int srcHi) |
| 186 | { |
| 187 | #ifdef __mips_hard_float |
| 188 | bool destFP = MIPS_FPREG(destLo) && MIPS_FPREG(destHi); |
| 189 | bool srcFP = MIPS_FPREG(srcLo) && MIPS_FPREG(srcHi); |
| 190 | assert(MIPS_FPREG(srcLo) == MIPS_FPREG(srcHi)); |
| 191 | assert(MIPS_FPREG(destLo) == MIPS_FPREG(destHi)); |
| 192 | if (destFP) { |
| 193 | if (srcFP) { |
| 194 | opRegCopy(cUnit, s2d(destLo, destHi), s2d(srcLo, srcHi)); |
| 195 | } else { |
| 196 | /* note the operands are swapped for the mtc1 instr */ |
| 197 | newLIR2(cUnit, kMipsMtc1, srcLo, destLo); |
| 198 | newLIR2(cUnit, kMipsMtc1, srcHi, destHi); |
| 199 | } |
| 200 | } else { |
| 201 | if (srcFP) { |
| 202 | newLIR2(cUnit, kMipsMfc1, destLo, srcLo); |
| 203 | newLIR2(cUnit, kMipsMfc1, destHi, srcHi); |
| 204 | } else { |
| 205 | // Handle overlap |
| 206 | if (srcHi == destLo) { |
| 207 | opRegCopy(cUnit, destHi, srcHi); |
| 208 | opRegCopy(cUnit, destLo, srcLo); |
| 209 | } else { |
| 210 | opRegCopy(cUnit, destLo, srcLo); |
| 211 | opRegCopy(cUnit, destHi, srcHi); |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | #else |
| 216 | // Handle overlap |
| 217 | if (srcHi == destLo) { |
| 218 | opRegCopy(cUnit, destHi, srcHi); |
| 219 | opRegCopy(cUnit, destLo, srcLo); |
| 220 | } else { |
| 221 | opRegCopy(cUnit, destLo, srcLo); |
| 222 | opRegCopy(cUnit, destHi, srcHi); |
| 223 | } |
| 224 | #endif |
| 225 | } |
| 226 | |
| 227 | void genFusedLongCmpBranch(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir) |
| 228 | { |
| 229 | UNIMPLEMENTED(FATAL) << "Need codegen for fused long cmp branch"; |
| 230 | } |
| 231 | |
| 232 | LIR* genRegMemCheck(CompilationUnit* cUnit, ConditionCode cCode, |
| 233 | int reg1, int base, int offset, ThrowKind kind) |
| 234 | { |
| 235 | LOG(FATAL) << "Unexpected use of genRegMemCheck for Arm"; |
| 236 | return NULL; |
| 237 | } |
| 238 | |
| 239 | RegLocation genDivRem(CompilationUnit* cUnit, RegLocation rlDest, int reg1, int reg2, bool isDiv) |
| 240 | { |
| 241 | newLIR4(cUnit, kMipsDiv, r_HI, r_LO, reg1, reg2); |
| 242 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 243 | if (isDiv) { |
| 244 | newLIR2(cUnit, kMipsMflo, rlResult.lowReg, r_LO); |
| 245 | } else { |
| 246 | newLIR2(cUnit, kMipsMfhi, rlResult.lowReg, r_HI); |
| 247 | } |
| 248 | return rlResult; |
| 249 | } |
| 250 | |
| 251 | RegLocation genDivRemLit(CompilationUnit* cUnit, RegLocation rlDest, int reg1, int lit, bool isDiv) |
| 252 | { |
| 253 | int tReg = oatAllocTemp(cUnit); |
| 254 | newLIR3(cUnit, kMipsAddiu, tReg, r_ZERO, lit); |
| 255 | newLIR4(cUnit, kMipsDiv, r_HI, r_LO, reg1, tReg); |
| 256 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 257 | if (isDiv) { |
| 258 | newLIR2(cUnit, kMipsMflo, rlResult.lowReg, r_LO); |
| 259 | } else { |
| 260 | newLIR2(cUnit, kMipsMfhi, rlResult.lowReg, r_HI); |
| 261 | } |
| 262 | oatFreeTemp(cUnit, tReg); |
| 263 | return rlResult; |
| 264 | } |
| 265 | |
| 266 | void opLea(CompilationUnit* cUnit, int rBase, int reg1, int reg2, int scale, int offset) |
| 267 | { |
| 268 | LOG(FATAL) << "Unexpected use of opLea for Arm"; |
| 269 | } |
| 270 | |
| 271 | void opTlsCmp(CompilationUnit* cUnit, int offset, int val) |
| 272 | { |
| 273 | LOG(FATAL) << "Unexpected use of opTlsCmp for Arm"; |
| 274 | } |
| 275 | |
| 276 | bool genInlinedCas32(CompilationUnit* cUnit, CallInfo* info, bool need_write_barrier) { |
| 277 | DCHECK_NE(cUnit->instructionSet, kThumb2); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | bool genInlinedSqrt(CompilationUnit* cUnit, CallInfo* info) { |
| 282 | DCHECK_NE(cUnit->instructionSet, kThumb2); |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | LIR* opPcRelLoad(CompilationUnit* cUnit, int reg, LIR* target) { |
| 287 | LOG(FATAL) << "Unexpected use of opPcRelLoad for Mips"; |
| 288 | return NULL; |
| 289 | } |
| 290 | |
| 291 | LIR* opVldm(CompilationUnit* cUnit, int rBase, int count) |
| 292 | { |
| 293 | LOG(FATAL) << "Unexpected use of opVldm for Mips"; |
| 294 | return NULL; |
| 295 | } |
| 296 | |
| 297 | LIR* opVstm(CompilationUnit* cUnit, int rBase, int count) |
| 298 | { |
| 299 | LOG(FATAL) << "Unexpected use of opVstm for Mips"; |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit, RegLocation rlSrc, |
| 304 | RegLocation rlResult, int lit, |
| 305 | int firstBit, int secondBit) |
| 306 | { |
| 307 | int tReg = oatAllocTemp(cUnit); |
| 308 | opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, secondBit - firstBit); |
| 309 | opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, tReg); |
| 310 | oatFreeTemp(cUnit, tReg); |
| 311 | if (firstBit != 0) { |
| 312 | opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void genDivZeroCheck(CompilationUnit* cUnit, int regLo, int regHi) |
| 317 | { |
| 318 | int tReg = oatAllocTemp(cUnit); |
| 319 | opRegRegReg(cUnit, kOpOr, tReg, regLo, regHi); |
| 320 | genImmedCheck(cUnit, kCondEq, tReg, 0, kThrowDivZero); |
| 321 | oatFreeTemp(cUnit, tReg); |
| 322 | } |
| 323 | |
| 324 | // Test suspend flag, return target of taken suspend branch |
| 325 | LIR* opTestSuspend(CompilationUnit* cUnit, LIR* target) |
| 326 | { |
| 327 | opRegImm(cUnit, kOpSub, rMIPS_SUSPEND, 1); |
| 328 | return opCmpImmBranch(cUnit, (target == NULL) ? kCondEq : kCondNe, rMIPS_SUSPEND, 0, target); |
| 329 | } |
| 330 | |
| 331 | // Decrement register and branch on condition |
| 332 | LIR* opDecAndBranch(CompilationUnit* cUnit, ConditionCode cCode, int reg, LIR* target) |
| 333 | { |
| 334 | opRegImm(cUnit, kOpSub, reg, 1); |
| 335 | return opCmpImmBranch(cUnit, cCode, reg, 0, target); |
| 336 | } |
| 337 | |
| 338 | bool smallLiteralDivide(CompilationUnit* cUnit, Instruction::Code dalvikOpcode, |
| 339 | RegLocation rlSrc, RegLocation rlDest, int lit) |
| 340 | { |
| 341 | LOG(FATAL) << "Unexpected use of smallLiteralDive in Mips"; |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | LIR* opIT(CompilationUnit* cUnit, ArmConditionCode cond, const char* guide) |
| 346 | { |
| 347 | LOG(FATAL) << "Unexpected use of opIT in Mips"; |
| 348 | return NULL; |
| 349 | } |
| 350 | |
| 351 | bool genAddLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 352 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 353 | { |
| 354 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 355 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 356 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 357 | /* |
| 358 | * [v1 v0] = [a1 a0] + [a3 a2]; |
| 359 | * addu v0,a2,a0 |
| 360 | * addu t1,a3,a1 |
| 361 | * sltu v1,v0,a2 |
| 362 | * addu v1,v1,t1 |
| 363 | */ |
| 364 | |
| 365 | opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc2.lowReg, rlSrc1.lowReg); |
| 366 | int tReg = oatAllocTemp(cUnit); |
| 367 | opRegRegReg(cUnit, kOpAdd, tReg, rlSrc2.highReg, rlSrc1.highReg); |
| 368 | newLIR3(cUnit, kMipsSltu, rlResult.highReg, rlResult.lowReg, rlSrc2.lowReg); |
| 369 | opRegRegReg(cUnit, kOpAdd, rlResult.highReg, rlResult.highReg, tReg); |
| 370 | oatFreeTemp(cUnit, tReg); |
| 371 | storeValueWide(cUnit, rlDest, rlResult); |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | bool genSubLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 376 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 377 | { |
| 378 | rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg); |
| 379 | rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg); |
| 380 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 381 | /* |
| 382 | * [v1 v0] = [a1 a0] - [a3 a2]; |
| 383 | * sltu t1,a0,a2 |
| 384 | * subu v0,a0,a2 |
| 385 | * subu v1,a1,a3 |
| 386 | * subu v1,v1,t1 |
| 387 | */ |
| 388 | |
| 389 | int tReg = oatAllocTemp(cUnit); |
| 390 | newLIR3(cUnit, kMipsSltu, tReg, rlSrc1.lowReg, rlSrc2.lowReg); |
| 391 | opRegRegReg(cUnit, kOpSub, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg); |
| 392 | opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlSrc1.highReg, rlSrc2.highReg); |
| 393 | opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlResult.highReg, tReg); |
| 394 | oatFreeTemp(cUnit, tReg); |
| 395 | storeValueWide(cUnit, rlDest, rlResult); |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | bool genNegLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 400 | RegLocation rlSrc) |
| 401 | { |
| 402 | rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg); |
| 403 | RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true); |
| 404 | /* |
| 405 | * [v1 v0] = -[a1 a0] |
| 406 | * negu v0,a0 |
| 407 | * negu v1,a1 |
| 408 | * sltu t1,r_zero |
| 409 | * subu v1,v1,t1 |
| 410 | */ |
| 411 | |
| 412 | opRegReg(cUnit, kOpNeg, rlResult.lowReg, rlSrc.lowReg); |
| 413 | opRegReg(cUnit, kOpNeg, rlResult.highReg, rlSrc.highReg); |
| 414 | int tReg = oatAllocTemp(cUnit); |
| 415 | newLIR3(cUnit, kMipsSltu, tReg, r_ZERO, rlResult.lowReg); |
| 416 | opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlResult.highReg, tReg); |
| 417 | oatFreeTemp(cUnit, tReg); |
| 418 | storeValueWide(cUnit, rlDest, rlResult); |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | bool genAndLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 423 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 424 | { |
| 425 | LOG(FATAL) << "Unexpected use of genAndLong for Mips"; |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | bool genOrLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 430 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 431 | { |
| 432 | LOG(FATAL) << "Unexpected use of genOrLong for Mips"; |
| 433 | return false; |
| 434 | } |
| 435 | |
| 436 | bool genXorLong(CompilationUnit* cUnit, RegLocation rlDest, |
| 437 | RegLocation rlSrc1, RegLocation rlSrc2) |
| 438 | { |
| 439 | LOG(FATAL) << "Unexpected use of genXorLong for Mips"; |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | } // namespace art |