buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 17 | namespace art { |
| 18 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 19 | /* |
| 20 | * This file contains target-independent codegen and support, and is |
| 21 | * included by: |
| 22 | * |
| 23 | * $(TARGET_ARCH)/Codegen-$(TARGET_ARCH_VARIANT).c |
| 24 | * |
| 25 | * which combines this common code with specific support found in the |
| 26 | * applicable directories below this one. |
| 27 | * |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 28 | */ |
| 29 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 30 | /* |
| 31 | * Load an immediate value into a fixed or temp register. Target |
| 32 | * register is clobbered, and marked inUse. |
| 33 | */ |
| 34 | LIR* loadConstant(CompilationUnit* cUnit, int rDest, int value) |
| 35 | { |
| 36 | if (oatIsTemp(cUnit, rDest)) { |
| 37 | oatClobber(cUnit, rDest); |
| 38 | oatMarkInUse(cUnit, rDest); |
| 39 | } |
| 40 | return loadConstantNoClobber(cUnit, rDest, value); |
| 41 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 42 | |
| 43 | /* Load a word at base + displacement. Displacement must be word multiple */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 44 | LIR* loadWordDisp(CompilationUnit* cUnit, int rBase, int displacement, |
| 45 | int rDest) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 46 | { |
| 47 | return loadBaseDisp(cUnit, NULL, rBase, displacement, rDest, kWord, |
| 48 | INVALID_SREG); |
| 49 | } |
| 50 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 51 | LIR* storeWordDisp(CompilationUnit* cUnit, int rBase, int displacement, |
| 52 | int rSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 53 | { |
| 54 | return storeBaseDisp(cUnit, rBase, displacement, rSrc, kWord); |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Load a Dalvik register into a physical register. Take care when |
| 59 | * using this routine, as it doesn't perform any bookkeeping regarding |
| 60 | * register liveness. That is the responsibility of the caller. |
| 61 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 62 | void loadValueDirect(CompilationUnit* cUnit, RegLocation rlSrc, int reg1) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 63 | { |
| 64 | rlSrc = oatUpdateLoc(cUnit, rlSrc); |
| 65 | if (rlSrc.location == kLocPhysReg) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 66 | opRegCopy(cUnit, reg1, rlSrc.lowReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 67 | } else { |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 68 | DCHECK((rlSrc.location == kLocDalvikFrame) || |
| 69 | (rlSrc.location == kLocCompilerTemp)); |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 70 | loadWordDisp(cUnit, rSP, oatSRegOffset(cUnit, rlSrc.sRegLow), reg1); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Similar to loadValueDirect, but clobbers and allocates the target |
| 76 | * register. Should be used when loading to a fixed register (for example, |
| 77 | * loading arguments to an out of line call. |
| 78 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 79 | void loadValueDirectFixed(CompilationUnit* cUnit, RegLocation rlSrc, int reg1) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 80 | { |
| 81 | oatClobber(cUnit, reg1); |
| 82 | oatMarkInUse(cUnit, reg1); |
| 83 | loadValueDirect(cUnit, rlSrc, reg1); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Load a Dalvik register pair into a physical register[s]. Take care when |
| 88 | * using this routine, as it doesn't perform any bookkeeping regarding |
| 89 | * register liveness. That is the responsibility of the caller. |
| 90 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 91 | void loadValueDirectWide(CompilationUnit* cUnit, RegLocation rlSrc, int regLo, |
| 92 | int regHi) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 93 | { |
| 94 | rlSrc = oatUpdateLocWide(cUnit, rlSrc); |
| 95 | if (rlSrc.location == kLocPhysReg) { |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 96 | opRegCopyWide(cUnit, regLo, regHi, rlSrc.lowReg, rlSrc.highReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 97 | } else { |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 98 | DCHECK((rlSrc.location == kLocDalvikFrame) || |
| 99 | (rlSrc.location == kLocCompilerTemp)); |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 100 | loadBaseDispWide(cUnit, NULL, rSP, |
| 101 | oatSRegOffset(cUnit, rlSrc.sRegLow), |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 102 | regLo, regHi, INVALID_SREG); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Similar to loadValueDirect, but clobbers and allocates the target |
| 108 | * registers. Should be used when loading to a fixed registers (for example, |
| 109 | * loading arguments to an out of line call. |
| 110 | */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 111 | void loadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc, |
| 112 | int regLo, int regHi) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 113 | { |
| 114 | oatClobber(cUnit, regLo); |
| 115 | oatClobber(cUnit, regHi); |
| 116 | oatMarkInUse(cUnit, regLo); |
| 117 | oatMarkInUse(cUnit, regHi); |
| 118 | loadValueDirectWide(cUnit, rlSrc, regLo, regHi); |
| 119 | } |
| 120 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 121 | RegLocation loadValue(CompilationUnit* cUnit, RegLocation rlSrc, |
| 122 | RegisterClass opKind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 123 | { |
| 124 | rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 125 | if (rlSrc.location != kLocPhysReg) { |
| 126 | DCHECK((rlSrc.location == kLocDalvikFrame) || |
| 127 | (rlSrc.location == kLocCompilerTemp)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 128 | loadValueDirect(cUnit, rlSrc, rlSrc.lowReg); |
| 129 | rlSrc.location = kLocPhysReg; |
| 130 | oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow); |
| 131 | } |
| 132 | return rlSrc; |
| 133 | } |
| 134 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 135 | void storeValue(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 136 | { |
| 137 | LIR* defStart; |
| 138 | LIR* defEnd; |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 139 | DCHECK(!rlDest.wide); |
| 140 | DCHECK(!rlSrc.wide); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 141 | rlSrc = oatUpdateLoc(cUnit, rlSrc); |
| 142 | rlDest = oatUpdateLoc(cUnit, rlDest); |
| 143 | if (rlSrc.location == kLocPhysReg) { |
| 144 | if (oatIsLive(cUnit, rlSrc.lowReg) || |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 145 | oatIsPromoted(cUnit, rlSrc.lowReg) || |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 146 | (rlDest.location == kLocPhysReg)) { |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 147 | // Src is live/promoted or Dest has assigned reg. |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 148 | rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 149 | opRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 150 | } else { |
| 151 | // Just re-assign the registers. Dest gets Src's regs |
| 152 | rlDest.lowReg = rlSrc.lowReg; |
| 153 | oatClobber(cUnit, rlSrc.lowReg); |
| 154 | } |
| 155 | } else { |
| 156 | // Load Src either into promoted Dest or temps allocated for Dest |
| 157 | rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false); |
| 158 | loadValueDirect(cUnit, rlSrc, rlDest.lowReg); |
| 159 | } |
| 160 | |
| 161 | // Dest is now live and dirty (until/if we flush it to home location) |
| 162 | oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow); |
| 163 | oatMarkDirty(cUnit, rlDest); |
| 164 | |
| 165 | |
| 166 | oatResetDefLoc(cUnit, rlDest); |
| 167 | if (oatIsDirty(cUnit, rlDest.lowReg) && |
| 168 | oatLiveOut(cUnit, rlDest.sRegLow)) { |
| 169 | defStart = (LIR* )cUnit->lastLIRInsn; |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 170 | storeBaseDisp(cUnit, rSP, oatSRegOffset(cUnit, rlDest.sRegLow), |
| 171 | rlDest.lowReg, kWord); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 172 | oatMarkClean(cUnit, rlDest); |
| 173 | defEnd = (LIR* )cUnit->lastLIRInsn; |
| 174 | oatMarkDef(cUnit, rlDest, defStart, defEnd); |
| 175 | } |
| 176 | } |
| 177 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 178 | RegLocation loadValueWide(CompilationUnit* cUnit, RegLocation rlSrc, |
| 179 | RegisterClass opKind) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 180 | { |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 181 | DCHECK(rlSrc.wide); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 182 | rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false); |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 183 | if (rlSrc.location != kLocPhysReg) { |
| 184 | DCHECK((rlSrc.location == kLocDalvikFrame) || |
| 185 | (rlSrc.location == kLocCompilerTemp)); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 186 | loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg); |
| 187 | rlSrc.location = kLocPhysReg; |
| 188 | oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow); |
| 189 | oatMarkLive(cUnit, rlSrc.highReg, |
| 190 | oatSRegHi(rlSrc.sRegLow)); |
| 191 | } |
| 192 | return rlSrc; |
| 193 | } |
| 194 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 195 | void storeValueWide(CompilationUnit* cUnit, RegLocation rlDest, |
| 196 | RegLocation rlSrc) |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 197 | { |
| 198 | LIR* defStart; |
| 199 | LIR* defEnd; |
buzbee | ed3e930 | 2011-09-23 17:34:19 -0700 | [diff] [blame] | 200 | DCHECK_EQ(FPREG(rlSrc.lowReg), FPREG(rlSrc.highReg)); |
| 201 | DCHECK(rlDest.wide); |
| 202 | DCHECK(rlSrc.wide); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 203 | if (rlSrc.location == kLocPhysReg) { |
| 204 | if (oatIsLive(cUnit, rlSrc.lowReg) || |
| 205 | oatIsLive(cUnit, rlSrc.highReg) || |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 206 | oatIsPromoted(cUnit, rlSrc.lowReg) || |
| 207 | oatIsPromoted(cUnit, rlSrc.highReg) || |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 208 | (rlDest.location == kLocPhysReg)) { |
buzbee | b29e4d1 | 2011-09-26 15:05:48 -0700 | [diff] [blame] | 209 | // Src is live or promoted or Dest has assigned reg. |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 210 | rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 211 | opRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg, |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 212 | rlSrc.lowReg, rlSrc.highReg); |
| 213 | } else { |
| 214 | // Just re-assign the registers. Dest gets Src's regs |
| 215 | rlDest.lowReg = rlSrc.lowReg; |
| 216 | rlDest.highReg = rlSrc.highReg; |
| 217 | oatClobber(cUnit, rlSrc.lowReg); |
| 218 | oatClobber(cUnit, rlSrc.highReg); |
| 219 | } |
| 220 | } else { |
| 221 | // Load Src either into promoted Dest or temps allocated for Dest |
| 222 | rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false); |
| 223 | loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg, |
| 224 | rlDest.highReg); |
| 225 | } |
| 226 | |
| 227 | // Dest is now live and dirty (until/if we flush it to home location) |
| 228 | oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow); |
| 229 | oatMarkLive(cUnit, rlDest.highReg, |
| 230 | oatSRegHi(rlDest.sRegLow)); |
| 231 | oatMarkDirty(cUnit, rlDest); |
| 232 | oatMarkPair(cUnit, rlDest.lowReg, rlDest.highReg); |
| 233 | |
| 234 | |
| 235 | oatResetDefLocWide(cUnit, rlDest); |
| 236 | if ((oatIsDirty(cUnit, rlDest.lowReg) || |
| 237 | oatIsDirty(cUnit, rlDest.highReg)) && |
| 238 | (oatLiveOut(cUnit, rlDest.sRegLow) || |
| 239 | oatLiveOut(cUnit, oatSRegHi(rlDest.sRegLow)))) { |
| 240 | defStart = (LIR*)cUnit->lastLIRInsn; |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 241 | DCHECK_EQ((SRegToVReg(cUnit, rlDest.sRegLow)+1), |
| 242 | SRegToVReg(cUnit, oatSRegHi(rlDest.sRegLow))); |
buzbee | 67bc236 | 2011-10-11 18:08:40 -0700 | [diff] [blame] | 243 | storeBaseDispWide(cUnit, rSP, oatSRegOffset(cUnit, rlDest.sRegLow), |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 244 | rlDest.lowReg, rlDest.highReg); |
| 245 | oatMarkClean(cUnit, rlDest); |
| 246 | defEnd = (LIR*)cUnit->lastLIRInsn; |
| 247 | oatMarkDefWide(cUnit, rlDest, defStart, defEnd); |
| 248 | } |
| 249 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 250 | |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 251 | /* |
| 252 | * Mark garbage collection card. Skip if the value we're storing is null. |
| 253 | */ |
| 254 | void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg) |
| 255 | { |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame] | 256 | #if defined(TARGET_X86) |
| 257 | UNIMPLEMENTED(WARNING) << "markGCCard"; |
| 258 | #else |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 259 | int regCardBase = oatAllocTemp(cUnit); |
| 260 | int regCardNo = oatAllocTemp(cUnit); |
buzbee | 82488f5 | 2012-03-02 08:20:26 -0800 | [diff] [blame] | 261 | LIR* branchOver = opCmpImmBranch(cUnit, kCondEq, valReg, 0, NULL); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 262 | loadWordDisp(cUnit, rSELF, Thread::CardTableOffset().Int32Value(), |
| 263 | regCardBase); |
| 264 | opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT); |
| 265 | storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0, |
| 266 | kUnsignedByte); |
| 267 | LIR* target = newLIR0(cUnit, kPseudoTargetLabel); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 268 | branchOver->target = (LIR*)target; |
| 269 | oatFreeTemp(cUnit, regCardBase); |
| 270 | oatFreeTemp(cUnit, regCardNo); |
buzbee | a7678db | 2012-03-05 15:35:46 -0800 | [diff] [blame] | 271 | #endif |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 272 | } |
| 273 | |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 274 | /* Utilities to load the current Method* */ |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 275 | void loadCurrMethodDirect(CompilationUnit *cUnit, int rTgt) |
| 276 | { |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 277 | loadValueDirectFixed(cUnit, cUnit->regLocation[cUnit->methodSReg], rTgt); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 278 | } |
| 279 | |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 280 | RegLocation loadCurrMethod(CompilationUnit *cUnit) |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 281 | { |
buzbee | e196567 | 2012-03-11 18:39:19 -0700 | [diff] [blame^] | 282 | return loadValue(cUnit, cUnit->regLocation[cUnit->methodSReg], kCoreReg); |
buzbee | 31a4a6f | 2012-02-28 15:36:15 -0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 286 | } // namespace art |