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 | |
| 17 | /* |
| 18 | * This file contains target-independent codegen and support, and is |
| 19 | * included by: |
| 20 | * |
| 21 | * $(TARGET_ARCH)/Codegen-$(TARGET_ARCH_VARIANT).c |
| 22 | * |
| 23 | * which combines this common code with specific support found in the |
| 24 | * applicable directories below this one. |
| 25 | * |
| 26 | * Prior to including this file, TGT_LIR should be #defined. |
| 27 | * For example, for arm: |
| 28 | * #define TGT_LIR ArmLIR |
| 29 | * and for x86: |
| 30 | * #define TGT_LIR X86LIR |
| 31 | */ |
| 32 | |
| 33 | |
| 34 | /* Load a word at base + displacement. Displacement must be word multiple */ |
| 35 | static TGT_LIR* loadWordDisp(CompilationUnit* cUnit, int rBase, |
| 36 | int displacement, int rDest) |
| 37 | { |
| 38 | return loadBaseDisp(cUnit, NULL, rBase, displacement, rDest, kWord, |
| 39 | INVALID_SREG); |
| 40 | } |
| 41 | |
| 42 | static TGT_LIR* storeWordDisp(CompilationUnit* cUnit, int rBase, |
| 43 | int displacement, int rSrc) |
| 44 | { |
| 45 | return storeBaseDisp(cUnit, rBase, displacement, rSrc, kWord); |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * Load a Dalvik register into a physical register. Take care when |
| 50 | * using this routine, as it doesn't perform any bookkeeping regarding |
| 51 | * register liveness. That is the responsibility of the caller. |
| 52 | */ |
| 53 | static void loadValueDirect(CompilationUnit* cUnit, RegLocation rlSrc, |
| 54 | int reg1) |
| 55 | { |
| 56 | rlSrc = oatUpdateLoc(cUnit, rlSrc); |
| 57 | if (rlSrc.location == kLocPhysReg) { |
| 58 | genRegCopy(cUnit, reg1, rlSrc.lowReg); |
| 59 | } else { |
| 60 | assert(rlSrc.location == kLocDalvikFrame); |
| 61 | loadWordDisp(cUnit, rSP, rlSrc.spOffset, reg1); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Similar to loadValueDirect, but clobbers and allocates the target |
| 67 | * register. Should be used when loading to a fixed register (for example, |
| 68 | * loading arguments to an out of line call. |
| 69 | */ |
| 70 | static void loadValueDirectFixed(CompilationUnit* cUnit, RegLocation rlSrc, |
| 71 | int reg1) |
| 72 | { |
| 73 | oatClobber(cUnit, reg1); |
| 74 | oatMarkInUse(cUnit, reg1); |
| 75 | loadValueDirect(cUnit, rlSrc, reg1); |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Load a Dalvik register pair into a physical register[s]. Take care when |
| 80 | * using this routine, as it doesn't perform any bookkeeping regarding |
| 81 | * register liveness. That is the responsibility of the caller. |
| 82 | */ |
| 83 | static void loadValueDirectWide(CompilationUnit* cUnit, RegLocation rlSrc, |
| 84 | int regLo, int regHi) |
| 85 | { |
| 86 | rlSrc = oatUpdateLocWide(cUnit, rlSrc); |
| 87 | if (rlSrc.location == kLocPhysReg) { |
| 88 | genRegCopyWide(cUnit, regLo, regHi, rlSrc.lowReg, rlSrc.highReg); |
| 89 | } else { |
| 90 | assert(rlSrc.location == kLocDalvikFrame); |
| 91 | loadBaseDispWide(cUnit, NULL, rSP, rlSrc.spOffset, |
| 92 | regLo, regHi, INVALID_SREG); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Similar to loadValueDirect, but clobbers and allocates the target |
| 98 | * registers. Should be used when loading to a fixed registers (for example, |
| 99 | * loading arguments to an out of line call. |
| 100 | */ |
| 101 | static void loadValueDirectWideFixed(CompilationUnit* cUnit, RegLocation rlSrc, |
| 102 | int regLo, int regHi) |
| 103 | { |
| 104 | oatClobber(cUnit, regLo); |
| 105 | oatClobber(cUnit, regHi); |
| 106 | oatMarkInUse(cUnit, regLo); |
| 107 | oatMarkInUse(cUnit, regHi); |
| 108 | loadValueDirectWide(cUnit, rlSrc, regLo, regHi); |
| 109 | } |
| 110 | |
| 111 | static RegLocation loadValue(CompilationUnit* cUnit, RegLocation rlSrc, |
| 112 | RegisterClass opKind) |
| 113 | { |
| 114 | rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false); |
| 115 | if (rlSrc.location == kLocDalvikFrame) { |
| 116 | loadValueDirect(cUnit, rlSrc, rlSrc.lowReg); |
| 117 | rlSrc.location = kLocPhysReg; |
| 118 | oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow); |
| 119 | } |
| 120 | return rlSrc; |
| 121 | } |
| 122 | |
| 123 | static void storeValue(CompilationUnit* cUnit, RegLocation rlDest, |
| 124 | RegLocation rlSrc) |
| 125 | { |
| 126 | LIR* defStart; |
| 127 | LIR* defEnd; |
| 128 | assert(!rlDest.wide); |
| 129 | assert(!rlSrc.wide); |
| 130 | oatKillNullCheckedLoc(cUnit, rlDest); |
| 131 | rlSrc = oatUpdateLoc(cUnit, rlSrc); |
| 132 | rlDest = oatUpdateLoc(cUnit, rlDest); |
| 133 | if (rlSrc.location == kLocPhysReg) { |
| 134 | if (oatIsLive(cUnit, rlSrc.lowReg) || |
| 135 | (rlDest.location == kLocPhysReg)) { |
| 136 | // Src is live or Dest has assigned reg. |
| 137 | rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false); |
| 138 | genRegCopy(cUnit, rlDest.lowReg, rlSrc.lowReg); |
| 139 | } else { |
| 140 | // Just re-assign the registers. Dest gets Src's regs |
| 141 | rlDest.lowReg = rlSrc.lowReg; |
| 142 | oatClobber(cUnit, rlSrc.lowReg); |
| 143 | } |
| 144 | } else { |
| 145 | // Load Src either into promoted Dest or temps allocated for Dest |
| 146 | rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false); |
| 147 | loadValueDirect(cUnit, rlSrc, rlDest.lowReg); |
| 148 | } |
| 149 | |
| 150 | // Dest is now live and dirty (until/if we flush it to home location) |
| 151 | oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow); |
| 152 | oatMarkDirty(cUnit, rlDest); |
| 153 | |
| 154 | |
| 155 | oatResetDefLoc(cUnit, rlDest); |
| 156 | if (oatIsDirty(cUnit, rlDest.lowReg) && |
| 157 | oatLiveOut(cUnit, rlDest.sRegLow)) { |
| 158 | defStart = (LIR* )cUnit->lastLIRInsn; |
| 159 | storeBaseDisp(cUnit, rSP, rlDest.spOffset, rlDest.lowReg, kWord); |
| 160 | oatMarkClean(cUnit, rlDest); |
| 161 | defEnd = (LIR* )cUnit->lastLIRInsn; |
| 162 | oatMarkDef(cUnit, rlDest, defStart, defEnd); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static RegLocation loadValueWide(CompilationUnit* cUnit, RegLocation rlSrc, |
| 167 | RegisterClass opKind) |
| 168 | { |
| 169 | assert(rlSrc.wide); |
| 170 | rlSrc = oatEvalLoc(cUnit, rlSrc, opKind, false); |
| 171 | if (rlSrc.location == kLocDalvikFrame) { |
| 172 | loadValueDirectWide(cUnit, rlSrc, rlSrc.lowReg, rlSrc.highReg); |
| 173 | rlSrc.location = kLocPhysReg; |
| 174 | oatMarkLive(cUnit, rlSrc.lowReg, rlSrc.sRegLow); |
| 175 | oatMarkLive(cUnit, rlSrc.highReg, |
| 176 | oatSRegHi(rlSrc.sRegLow)); |
| 177 | } |
| 178 | return rlSrc; |
| 179 | } |
| 180 | |
| 181 | static void storeValueWide(CompilationUnit* cUnit, RegLocation rlDest, |
| 182 | RegLocation rlSrc) |
| 183 | { |
| 184 | LIR* defStart; |
| 185 | LIR* defEnd; |
buzbee | 3ea4ec5 | 2011-08-22 17:37:19 -0700 | [diff] [blame^] | 186 | if(FPREG(rlSrc.lowReg)!=FPREG(rlSrc.highReg)) { |
| 187 | LOG(WARNING) << "rlSrc.lowreg:" << rlSrc.lowReg << ", rlSrc.highReg:" |
| 188 | << rlSrc.highReg; |
| 189 | } |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 190 | assert(FPREG(rlSrc.lowReg)==FPREG(rlSrc.highReg)); |
| 191 | assert(rlDest.wide); |
| 192 | assert(rlSrc.wide); |
| 193 | oatKillNullCheckedLoc(cUnit, rlDest); |
| 194 | if (rlSrc.location == kLocPhysReg) { |
| 195 | if (oatIsLive(cUnit, rlSrc.lowReg) || |
| 196 | oatIsLive(cUnit, rlSrc.highReg) || |
| 197 | (rlDest.location == kLocPhysReg)) { |
| 198 | // Src is live or Dest has assigned reg. |
| 199 | rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false); |
| 200 | genRegCopyWide(cUnit, rlDest.lowReg, rlDest.highReg, |
| 201 | rlSrc.lowReg, rlSrc.highReg); |
| 202 | } else { |
| 203 | // Just re-assign the registers. Dest gets Src's regs |
| 204 | rlDest.lowReg = rlSrc.lowReg; |
| 205 | rlDest.highReg = rlSrc.highReg; |
| 206 | oatClobber(cUnit, rlSrc.lowReg); |
| 207 | oatClobber(cUnit, rlSrc.highReg); |
| 208 | } |
| 209 | } else { |
| 210 | // Load Src either into promoted Dest or temps allocated for Dest |
| 211 | rlDest = oatEvalLoc(cUnit, rlDest, kAnyReg, false); |
| 212 | loadValueDirectWide(cUnit, rlSrc, rlDest.lowReg, |
| 213 | rlDest.highReg); |
| 214 | } |
| 215 | |
| 216 | // Dest is now live and dirty (until/if we flush it to home location) |
| 217 | oatMarkLive(cUnit, rlDest.lowReg, rlDest.sRegLow); |
| 218 | oatMarkLive(cUnit, rlDest.highReg, |
| 219 | oatSRegHi(rlDest.sRegLow)); |
| 220 | oatMarkDirty(cUnit, rlDest); |
| 221 | oatMarkPair(cUnit, rlDest.lowReg, rlDest.highReg); |
| 222 | |
| 223 | |
| 224 | oatResetDefLocWide(cUnit, rlDest); |
| 225 | if ((oatIsDirty(cUnit, rlDest.lowReg) || |
| 226 | oatIsDirty(cUnit, rlDest.highReg)) && |
| 227 | (oatLiveOut(cUnit, rlDest.sRegLow) || |
| 228 | oatLiveOut(cUnit, oatSRegHi(rlDest.sRegLow)))) { |
| 229 | defStart = (LIR*)cUnit->lastLIRInsn; |
| 230 | assert((oatS2VReg(cUnit, rlDest.sRegLow)+1) == |
| 231 | oatS2VReg(cUnit, oatSRegHi(rlDest.sRegLow))); |
| 232 | storeBaseDispWide(cUnit, rSP, rlDest.spOffset, |
| 233 | rlDest.lowReg, rlDest.highReg); |
| 234 | oatMarkClean(cUnit, rlDest); |
| 235 | defEnd = (LIR*)cUnit->lastLIRInsn; |
| 236 | oatMarkDefWide(cUnit, rlDest, defStart, defEnd); |
| 237 | } |
| 238 | } |