Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -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 | /* This file contains codegen for the Thumb2 ISA. */ |
| 18 | |
| 19 | #include "arm_lir.h" |
| 20 | #include "codegen_arm.h" |
| 21 | #include "dex/quick/mir_to_lir-inl.h" |
| 22 | #include "oat/runtime/oat_support_entrypoints.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | |
| 27 | /* Return the position of an ssa name within the argument list */ |
| 28 | int ArmMir2Lir::InPosition(int s_reg) |
| 29 | { |
| 30 | int v_reg = mir_graph_->SRegToVReg(s_reg); |
| 31 | return v_reg - cu_->num_regs; |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | * Describe an argument. If it's already in an arg register, just leave it |
| 36 | * there. NOTE: all live arg registers must be locked prior to this call |
| 37 | * to avoid having them allocated as a temp by downstream utilities. |
| 38 | */ |
| 39 | RegLocation ArmMir2Lir::ArgLoc(RegLocation loc) |
| 40 | { |
| 41 | int arg_num = InPosition(loc.s_reg_low); |
| 42 | if (loc.wide) { |
| 43 | if (arg_num == 2) { |
| 44 | // Bad case - half in register, half in frame. Just punt |
| 45 | loc.location = kLocInvalid; |
| 46 | } else if (arg_num < 2) { |
| 47 | loc.low_reg = rARM_ARG1 + arg_num; |
| 48 | loc.high_reg = loc.low_reg + 1; |
| 49 | loc.location = kLocPhysReg; |
| 50 | } else { |
| 51 | loc.location = kLocDalvikFrame; |
| 52 | } |
| 53 | } else { |
| 54 | if (arg_num < 3) { |
| 55 | loc.low_reg = rARM_ARG1 + arg_num; |
| 56 | loc.location = kLocPhysReg; |
| 57 | } else { |
| 58 | loc.location = kLocDalvikFrame; |
| 59 | } |
| 60 | } |
| 61 | return loc; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Load an argument. If already in a register, just return. If in |
| 66 | * the frame, we can't use the normal LoadValue() because it assumed |
| 67 | * a proper frame - and we're frameless. |
| 68 | */ |
| 69 | RegLocation ArmMir2Lir::LoadArg(RegLocation loc) |
| 70 | { |
| 71 | if (loc.location == kLocDalvikFrame) { |
| 72 | int start = (InPosition(loc.s_reg_low) + 1) * sizeof(uint32_t); |
| 73 | loc.low_reg = AllocTemp(); |
| 74 | LoadWordDisp(rARM_SP, start, loc.low_reg); |
| 75 | if (loc.wide) { |
| 76 | loc.high_reg = AllocTemp(); |
| 77 | LoadWordDisp(rARM_SP, start + sizeof(uint32_t), loc.high_reg); |
| 78 | } |
| 79 | loc.location = kLocPhysReg; |
| 80 | } |
| 81 | return loc; |
| 82 | } |
| 83 | |
| 84 | /* Lock any referenced arguments that arrive in registers */ |
| 85 | void ArmMir2Lir::LockLiveArgs(MIR* mir) |
| 86 | { |
| 87 | int first_in = cu_->num_regs; |
| 88 | const int num_arg_regs = 3; // TODO: generalize & move to RegUtil.cc |
| 89 | for (int i = 0; i < mir->ssa_rep->num_uses; i++) { |
| 90 | int v_reg = mir_graph_->SRegToVReg(mir->ssa_rep->uses[i]); |
| 91 | int InPosition = v_reg - first_in; |
| 92 | if (InPosition < num_arg_regs) { |
| 93 | LockTemp(rARM_ARG1 + InPosition); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /* Find the next MIR, which may be in a following basic block */ |
| 99 | // TODO: should this be a utility in mir_graph? |
| 100 | MIR* ArmMir2Lir::GetNextMir(BasicBlock** p_bb, MIR* mir) |
| 101 | { |
| 102 | BasicBlock* bb = *p_bb; |
| 103 | MIR* orig_mir = mir; |
| 104 | while (bb != NULL) { |
| 105 | if (mir != NULL) { |
| 106 | mir = mir->next; |
| 107 | } |
| 108 | if (mir != NULL) { |
| 109 | return mir; |
| 110 | } else { |
| 111 | bb = bb->fall_through; |
| 112 | *p_bb = bb; |
| 113 | if (bb) { |
| 114 | mir = bb->first_mir_insn; |
| 115 | if (mir != NULL) { |
| 116 | return mir; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | return orig_mir; |
| 122 | } |
| 123 | |
| 124 | /* Used for the "verbose" listing */ |
| 125 | //TODO: move to common code |
| 126 | void ArmMir2Lir::GenPrintLabel(MIR* mir) |
| 127 | { |
| 128 | /* Mark the beginning of a Dalvik instruction for line tracking */ |
| 129 | char* inst_str = cu_->verbose ? |
| 130 | mir_graph_->GetDalvikDisassembly(mir) : NULL; |
| 131 | MarkBoundary(mir->offset, inst_str); |
| 132 | } |
| 133 | |
| 134 | MIR* ArmMir2Lir::SpecialIGet(BasicBlock** bb, MIR* mir, |
| 135 | OpSize size, bool long_or_double, bool is_object) |
| 136 | { |
| 137 | int field_offset; |
| 138 | bool is_volatile; |
| 139 | uint32_t field_idx = mir->dalvikInsn.vC; |
| 140 | bool fast_path = FastInstance(field_idx, field_offset, is_volatile, false); |
| 141 | if (!fast_path || !(mir->optimization_flags & MIR_IGNORE_NULL_CHECK)) { |
| 142 | return NULL; |
| 143 | } |
| 144 | RegLocation rl_obj = mir_graph_->GetSrc(mir, 0); |
| 145 | LockLiveArgs(mir); |
| 146 | rl_obj = ArmMir2Lir::ArgLoc(rl_obj); |
| 147 | RegLocation rl_dest; |
| 148 | if (long_or_double) { |
| 149 | rl_dest = GetReturnWide(false); |
| 150 | } else { |
| 151 | rl_dest = GetReturn(false); |
| 152 | } |
| 153 | // Point of no return - no aborts after this |
| 154 | ArmMir2Lir::GenPrintLabel(mir); |
| 155 | rl_obj = LoadArg(rl_obj); |
| 156 | GenIGet(field_idx, mir->optimization_flags, size, rl_dest, rl_obj, long_or_double, is_object); |
| 157 | return GetNextMir(bb, mir); |
| 158 | } |
| 159 | |
| 160 | MIR* ArmMir2Lir::SpecialIPut(BasicBlock** bb, MIR* mir, |
| 161 | OpSize size, bool long_or_double, bool is_object) |
| 162 | { |
| 163 | int field_offset; |
| 164 | bool is_volatile; |
| 165 | uint32_t field_idx = mir->dalvikInsn.vC; |
| 166 | bool fast_path = FastInstance(field_idx, field_offset, is_volatile, false); |
| 167 | if (!fast_path || !(mir->optimization_flags & MIR_IGNORE_NULL_CHECK)) { |
| 168 | return NULL; |
| 169 | } |
| 170 | RegLocation rl_src; |
| 171 | RegLocation rl_obj; |
| 172 | LockLiveArgs(mir); |
| 173 | if (long_or_double) { |
| 174 | rl_src = mir_graph_->GetSrcWide(mir, 0); |
| 175 | rl_obj = mir_graph_->GetSrc(mir, 2); |
| 176 | } else { |
| 177 | rl_src = mir_graph_->GetSrc(mir, 0); |
| 178 | rl_obj = mir_graph_->GetSrc(mir, 1); |
| 179 | } |
| 180 | rl_src = ArmMir2Lir::ArgLoc(rl_src); |
| 181 | rl_obj = ArmMir2Lir::ArgLoc(rl_obj); |
| 182 | // Reject if source is split across registers & frame |
| 183 | if (rl_obj.location == kLocInvalid) { |
| 184 | ResetRegPool(); |
| 185 | return NULL; |
| 186 | } |
| 187 | // Point of no return - no aborts after this |
| 188 | ArmMir2Lir::GenPrintLabel(mir); |
| 189 | rl_obj = LoadArg(rl_obj); |
| 190 | rl_src = LoadArg(rl_src); |
| 191 | GenIPut(field_idx, mir->optimization_flags, size, rl_src, rl_obj, long_or_double, is_object); |
| 192 | return GetNextMir(bb, mir); |
| 193 | } |
| 194 | |
| 195 | MIR* ArmMir2Lir::SpecialIdentity(MIR* mir) |
| 196 | { |
| 197 | RegLocation rl_src; |
| 198 | RegLocation rl_dest; |
| 199 | bool wide = (mir->ssa_rep->num_uses == 2); |
| 200 | if (wide) { |
| 201 | rl_src = mir_graph_->GetSrcWide(mir, 0); |
| 202 | rl_dest = GetReturnWide(false); |
| 203 | } else { |
| 204 | rl_src = mir_graph_->GetSrc(mir, 0); |
| 205 | rl_dest = GetReturn(false); |
| 206 | } |
| 207 | LockLiveArgs(mir); |
| 208 | rl_src = ArmMir2Lir::ArgLoc(rl_src); |
| 209 | if (rl_src.location == kLocInvalid) { |
| 210 | ResetRegPool(); |
| 211 | return NULL; |
| 212 | } |
| 213 | // Point of no return - no aborts after this |
| 214 | ArmMir2Lir::GenPrintLabel(mir); |
| 215 | rl_src = LoadArg(rl_src); |
| 216 | if (wide) { |
| 217 | StoreValueWide(rl_dest, rl_src); |
| 218 | } else { |
| 219 | StoreValue(rl_dest, rl_src); |
| 220 | } |
| 221 | return mir; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Special-case code genration for simple non-throwing leaf methods. |
| 226 | */ |
| 227 | void ArmMir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, |
| 228 | SpecialCaseHandler special_case) |
| 229 | { |
| 230 | current_dalvik_offset_ = mir->offset; |
| 231 | MIR* next_mir = NULL; |
| 232 | switch (special_case) { |
| 233 | case kNullMethod: |
| 234 | DCHECK(mir->dalvikInsn.opcode == Instruction::RETURN_VOID); |
| 235 | next_mir = mir; |
| 236 | break; |
| 237 | case kConstFunction: |
| 238 | ArmMir2Lir::GenPrintLabel(mir); |
| 239 | LoadConstant(rARM_RET0, mir->dalvikInsn.vB); |
| 240 | next_mir = GetNextMir(&bb, mir); |
| 241 | break; |
| 242 | case kIGet: |
| 243 | next_mir = SpecialIGet(&bb, mir, kWord, false, false); |
| 244 | break; |
| 245 | case kIGetBoolean: |
| 246 | case kIGetByte: |
| 247 | next_mir = SpecialIGet(&bb, mir, kUnsignedByte, false, false); |
| 248 | break; |
| 249 | case kIGetObject: |
| 250 | next_mir = SpecialIGet(&bb, mir, kWord, false, true); |
| 251 | break; |
| 252 | case kIGetChar: |
| 253 | next_mir = SpecialIGet(&bb, mir, kUnsignedHalf, false, false); |
| 254 | break; |
| 255 | case kIGetShort: |
| 256 | next_mir = SpecialIGet(&bb, mir, kSignedHalf, false, false); |
| 257 | break; |
| 258 | case kIGetWide: |
| 259 | next_mir = SpecialIGet(&bb, mir, kLong, true, false); |
| 260 | break; |
| 261 | case kIPut: |
| 262 | next_mir = SpecialIPut(&bb, mir, kWord, false, false); |
| 263 | break; |
| 264 | case kIPutBoolean: |
| 265 | case kIPutByte: |
| 266 | next_mir = SpecialIPut(&bb, mir, kUnsignedByte, false, false); |
| 267 | break; |
| 268 | case kIPutObject: |
| 269 | next_mir = SpecialIPut(&bb, mir, kWord, false, true); |
| 270 | break; |
| 271 | case kIPutChar: |
| 272 | next_mir = SpecialIPut(&bb, mir, kUnsignedHalf, false, false); |
| 273 | break; |
| 274 | case kIPutShort: |
| 275 | next_mir = SpecialIPut(&bb, mir, kSignedHalf, false, false); |
| 276 | break; |
| 277 | case kIPutWide: |
| 278 | next_mir = SpecialIPut(&bb, mir, kLong, true, false); |
| 279 | break; |
| 280 | case kIdentity: |
| 281 | next_mir = SpecialIdentity(mir); |
| 282 | break; |
| 283 | default: |
| 284 | return; |
| 285 | } |
| 286 | if (next_mir != NULL) { |
| 287 | current_dalvik_offset_ = next_mir->offset; |
| 288 | if (special_case != kIdentity) { |
| 289 | ArmMir2Lir::GenPrintLabel(next_mir); |
| 290 | } |
| 291 | NewLIR1(kThumbBx, rARM_LR); |
| 292 | core_spill_mask_ = 0; |
| 293 | num_core_spills_ = 0; |
| 294 | fp_spill_mask_ = 0; |
| 295 | num_fp_spills_ = 0; |
| 296 | frame_size_ = 0; |
| 297 | core_vmap_table_.clear(); |
| 298 | fp_vmap_table_.clear(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /* |
| 303 | * The sparse table in the literal pool is an array of <key,displacement> |
| 304 | * pairs. For each set, we'll load them as a pair using ldmia. |
| 305 | * This means that the register number of the temp we use for the key |
| 306 | * must be lower than the reg for the displacement. |
| 307 | * |
| 308 | * The test loop will look something like: |
| 309 | * |
| 310 | * adr rBase, <table> |
| 311 | * ldr r_val, [rARM_SP, v_reg_off] |
| 312 | * mov r_idx, #table_size |
| 313 | * lp: |
| 314 | * ldmia rBase!, {r_key, r_disp} |
| 315 | * sub r_idx, #1 |
| 316 | * cmp r_val, r_key |
| 317 | * ifeq |
| 318 | * add rARM_PC, r_disp ; This is the branch from which we compute displacement |
| 319 | * cbnz r_idx, lp |
| 320 | */ |
| 321 | void ArmMir2Lir::GenSparseSwitch(MIR* mir, uint32_t table_offset, |
| 322 | RegLocation rl_src) |
| 323 | { |
| 324 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 325 | if (cu_->verbose) { |
| 326 | DumpSparseSwitchTable(table); |
| 327 | } |
| 328 | // Add the table to the list - we'll process it later |
| 329 | SwitchTable *tab_rec = |
| 330 | static_cast<SwitchTable*>(arena_->NewMem(sizeof(SwitchTable), true, |
| 331 | ArenaAllocator::kAllocData)); |
| 332 | tab_rec->table = table; |
| 333 | tab_rec->vaddr = current_dalvik_offset_; |
| 334 | int size = table[1]; |
| 335 | tab_rec->targets = static_cast<LIR**>(arena_->NewMem(size * sizeof(LIR*), true, |
| 336 | ArenaAllocator::kAllocLIR)); |
| 337 | switch_tables_.Insert(tab_rec); |
| 338 | |
| 339 | // Get the switch value |
| 340 | rl_src = LoadValue(rl_src, kCoreReg); |
| 341 | int rBase = AllocTemp(); |
| 342 | /* Allocate key and disp temps */ |
| 343 | int r_key = AllocTemp(); |
| 344 | int r_disp = AllocTemp(); |
| 345 | // Make sure r_key's register number is less than r_disp's number for ldmia |
| 346 | if (r_key > r_disp) { |
| 347 | int tmp = r_disp; |
| 348 | r_disp = r_key; |
| 349 | r_key = tmp; |
| 350 | } |
| 351 | // Materialize a pointer to the switch table |
| 352 | NewLIR3(kThumb2Adr, rBase, 0, reinterpret_cast<uintptr_t>(tab_rec)); |
| 353 | // Set up r_idx |
| 354 | int r_idx = AllocTemp(); |
| 355 | LoadConstant(r_idx, size); |
| 356 | // Establish loop branch target |
| 357 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 358 | // Load next key/disp |
| 359 | NewLIR2(kThumb2LdmiaWB, rBase, (1 << r_key) | (1 << r_disp)); |
| 360 | OpRegReg(kOpCmp, r_key, rl_src.low_reg); |
| 361 | // Go if match. NOTE: No instruction set switch here - must stay Thumb2 |
| 362 | OpIT(kCondEq, ""); |
| 363 | LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp); |
| 364 | tab_rec->anchor = switch_branch; |
| 365 | // Needs to use setflags encoding here |
| 366 | NewLIR3(kThumb2SubsRRI12, r_idx, r_idx, 1); |
| 367 | OpCondBranch(kCondNe, target); |
| 368 | } |
| 369 | |
| 370 | |
| 371 | void ArmMir2Lir::GenPackedSwitch(MIR* mir, uint32_t table_offset, |
| 372 | RegLocation rl_src) |
| 373 | { |
| 374 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 375 | if (cu_->verbose) { |
| 376 | DumpPackedSwitchTable(table); |
| 377 | } |
| 378 | // Add the table to the list - we'll process it later |
| 379 | SwitchTable *tab_rec = |
| 380 | static_cast<SwitchTable*>(arena_->NewMem(sizeof(SwitchTable), true, |
| 381 | ArenaAllocator::kAllocData)); |
| 382 | tab_rec->table = table; |
| 383 | tab_rec->vaddr = current_dalvik_offset_; |
| 384 | int size = table[1]; |
| 385 | tab_rec->targets = |
| 386 | static_cast<LIR**>(arena_->NewMem(size * sizeof(LIR*), true, ArenaAllocator::kAllocLIR)); |
| 387 | switch_tables_.Insert(tab_rec); |
| 388 | |
| 389 | // Get the switch value |
| 390 | rl_src = LoadValue(rl_src, kCoreReg); |
| 391 | int table_base = AllocTemp(); |
| 392 | // Materialize a pointer to the switch table |
| 393 | NewLIR3(kThumb2Adr, table_base, 0, reinterpret_cast<uintptr_t>(tab_rec)); |
| 394 | int low_key = s4FromSwitchData(&table[2]); |
| 395 | int keyReg; |
| 396 | // Remove the bias, if necessary |
| 397 | if (low_key == 0) { |
| 398 | keyReg = rl_src.low_reg; |
| 399 | } else { |
| 400 | keyReg = AllocTemp(); |
| 401 | OpRegRegImm(kOpSub, keyReg, rl_src.low_reg, low_key); |
| 402 | } |
| 403 | // Bounds check - if < 0 or >= size continue following switch |
| 404 | OpRegImm(kOpCmp, keyReg, size-1); |
| 405 | LIR* branch_over = OpCondBranch(kCondHi, NULL); |
| 406 | |
| 407 | // Load the displacement from the switch table |
| 408 | int disp_reg = AllocTemp(); |
| 409 | LoadBaseIndexed(table_base, keyReg, disp_reg, 2, kWord); |
| 410 | |
| 411 | // ..and go! NOTE: No instruction set switch here - must stay Thumb2 |
| 412 | LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg); |
| 413 | tab_rec->anchor = switch_branch; |
| 414 | |
| 415 | /* branch_over target here */ |
| 416 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 417 | branch_over->target = target; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Array data table format: |
| 422 | * ushort ident = 0x0300 magic value |
| 423 | * ushort width width of each element in the table |
| 424 | * uint size number of elements in the table |
| 425 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 426 | * padding at the end) |
| 427 | * |
| 428 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 429 | */ |
| 430 | void ArmMir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src) |
| 431 | { |
| 432 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 433 | // Add the table to the list - we'll process it later |
| 434 | FillArrayData *tab_rec = |
| 435 | static_cast<FillArrayData*>(arena_->NewMem(sizeof(FillArrayData), true, |
| 436 | ArenaAllocator::kAllocData)); |
| 437 | tab_rec->table = table; |
| 438 | tab_rec->vaddr = current_dalvik_offset_; |
| 439 | uint16_t width = tab_rec->table[1]; |
| 440 | uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16); |
| 441 | tab_rec->size = (size * width) + 8; |
| 442 | |
| 443 | fill_array_data_.Insert(tab_rec); |
| 444 | |
| 445 | // Making a call - use explicit registers |
| 446 | FlushAllRegs(); /* Everything to home location */ |
| 447 | LoadValueDirectFixed(rl_src, r0); |
| 448 | LoadWordDisp(rARM_SELF, ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode), |
| 449 | rARM_LR); |
| 450 | // Materialize a pointer to the fill data image |
| 451 | NewLIR3(kThumb2Adr, r1, 0, reinterpret_cast<uintptr_t>(tab_rec)); |
| 452 | ClobberCalleeSave(); |
| 453 | LIR* call_inst = OpReg(kOpBlx, rARM_LR); |
| 454 | MarkSafepointPC(call_inst); |
| 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Handle simple case (thin lock) inline. If it's complicated, bail |
| 459 | * out to the heavyweight lock/unlock routines. We'll use dedicated |
| 460 | * registers here in order to be in the right position in case we |
| 461 | * to bail to oat[Lock/Unlock]Object(self, object) |
| 462 | * |
| 463 | * r0 -> self pointer [arg0 for oat[Lock/Unlock]Object |
| 464 | * r1 -> object [arg1 for oat[Lock/Unlock]Object |
| 465 | * r2 -> intial contents of object->lock, later result of strex |
| 466 | * r3 -> self->thread_id |
| 467 | * r12 -> allow to be used by utilities as general temp |
| 468 | * |
| 469 | * The result of the strex is 0 if we acquire the lock. |
| 470 | * |
| 471 | * See comments in monitor.cc for the layout of the lock word. |
| 472 | * Of particular interest to this code is the test for the |
| 473 | * simple case - which we handle inline. For monitor enter, the |
| 474 | * simple case is thin lock, held by no-one. For monitor exit, |
| 475 | * the simple case is thin lock, held by the unlocking thread with |
| 476 | * a recurse count of 0. |
| 477 | * |
| 478 | * A minor complication is that there is a field in the lock word |
| 479 | * unrelated to locking: the hash state. This field must be ignored, but |
| 480 | * preserved. |
| 481 | * |
| 482 | */ |
| 483 | void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) |
| 484 | { |
| 485 | FlushAllRegs(); |
| 486 | DCHECK_EQ(LW_SHAPE_THIN, 0); |
| 487 | LoadValueDirectFixed(rl_src, r0); // Get obj |
| 488 | LockCallTemps(); // Prepare for explicit register usage |
| 489 | GenNullCheck(rl_src.s_reg_low, r0, opt_flags); |
| 490 | LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
| 491 | NewLIR3(kThumb2Ldrex, r1, r0, |
| 492 | mirror::Object::MonitorOffset().Int32Value() >> 2); // Get object->lock |
| 493 | // Align owner |
| 494 | OpRegImm(kOpLsl, r2, LW_LOCK_OWNER_SHIFT); |
| 495 | // Is lock unheld on lock or held by us (==thread_id) on unlock? |
| 496 | NewLIR4(kThumb2Bfi, r2, r1, 0, LW_LOCK_OWNER_SHIFT - 1); |
| 497 | NewLIR3(kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1); |
| 498 | OpRegImm(kOpCmp, r1, 0); |
| 499 | OpIT(kCondEq, ""); |
| 500 | NewLIR4(kThumb2Strex, r1, r2, r0, |
| 501 | mirror::Object::MonitorOffset().Int32Value() >> 2); |
| 502 | OpRegImm(kOpCmp, r1, 0); |
| 503 | OpIT(kCondNe, "T"); |
| 504 | // Go expensive route - artLockObjectFromCode(self, obj); |
| 505 | LoadWordDisp(rARM_SELF, ENTRYPOINT_OFFSET(pLockObjectFromCode), rARM_LR); |
| 506 | ClobberCalleeSave(); |
| 507 | LIR* call_inst = OpReg(kOpBlx, rARM_LR); |
| 508 | MarkSafepointPC(call_inst); |
| 509 | GenMemBarrier(kLoadLoad); |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * For monitor unlock, we don't have to use ldrex/strex. Once |
| 514 | * we've determined that the lock is thin and that we own it with |
| 515 | * a zero recursion count, it's safe to punch it back to the |
| 516 | * initial, unlock thin state with a store word. |
| 517 | */ |
| 518 | void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) |
| 519 | { |
| 520 | DCHECK_EQ(LW_SHAPE_THIN, 0); |
| 521 | FlushAllRegs(); |
| 522 | LoadValueDirectFixed(rl_src, r0); // Get obj |
| 523 | LockCallTemps(); // Prepare for explicit register usage |
| 524 | GenNullCheck(rl_src.s_reg_low, r0, opt_flags); |
| 525 | LoadWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r1); // Get lock |
| 526 | LoadWordDisp(rARM_SELF, Thread::ThinLockIdOffset().Int32Value(), r2); |
| 527 | // Is lock unheld on lock or held by us (==thread_id) on unlock? |
| 528 | OpRegRegImm(kOpAnd, r3, r1, |
| 529 | (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT)); |
| 530 | // Align owner |
| 531 | OpRegImm(kOpLsl, r2, LW_LOCK_OWNER_SHIFT); |
| 532 | NewLIR3(kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1); |
| 533 | OpRegReg(kOpSub, r1, r2); |
| 534 | OpIT(kCondEq, "EE"); |
| 535 | StoreWordDisp(r0, mirror::Object::MonitorOffset().Int32Value(), r3); |
| 536 | // Go expensive route - UnlockObjectFromCode(obj); |
| 537 | LoadWordDisp(rARM_SELF, ENTRYPOINT_OFFSET(pUnlockObjectFromCode), rARM_LR); |
| 538 | ClobberCalleeSave(); |
| 539 | LIR* call_inst = OpReg(kOpBlx, rARM_LR); |
| 540 | MarkSafepointPC(call_inst); |
| 541 | GenMemBarrier(kStoreLoad); |
| 542 | } |
| 543 | |
| 544 | void ArmMir2Lir::GenMoveException(RegLocation rl_dest) |
| 545 | { |
| 546 | int ex_offset = Thread::ExceptionOffset().Int32Value(); |
| 547 | RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true); |
| 548 | int reset_reg = AllocTemp(); |
| 549 | LoadWordDisp(rARM_SELF, ex_offset, rl_result.low_reg); |
| 550 | LoadConstant(reset_reg, 0); |
| 551 | StoreWordDisp(rARM_SELF, ex_offset, reset_reg); |
| 552 | FreeTemp(reset_reg); |
| 553 | StoreValue(rl_dest, rl_result); |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Mark garbage collection card. Skip if the value we're storing is null. |
| 558 | */ |
| 559 | void ArmMir2Lir::MarkGCCard(int val_reg, int tgt_addr_reg) |
| 560 | { |
| 561 | int reg_card_base = AllocTemp(); |
| 562 | int reg_card_no = AllocTemp(); |
| 563 | LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL); |
| 564 | LoadWordDisp(rARM_SELF, Thread::CardTableOffset().Int32Value(), reg_card_base); |
| 565 | OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift); |
| 566 | StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, |
| 567 | kUnsignedByte); |
| 568 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 569 | branch_over->target = target; |
| 570 | FreeTemp(reg_card_base); |
| 571 | FreeTemp(reg_card_no); |
| 572 | } |
| 573 | |
| 574 | void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) |
| 575 | { |
| 576 | int spill_count = num_core_spills_ + num_fp_spills_; |
| 577 | /* |
| 578 | * On entry, r0, r1, r2 & r3 are live. Let the register allocation |
| 579 | * mechanism know so it doesn't try to use any of them when |
| 580 | * expanding the frame or flushing. This leaves the utility |
| 581 | * code with a single temp: r12. This should be enough. |
| 582 | */ |
| 583 | LockTemp(r0); |
| 584 | LockTemp(r1); |
| 585 | LockTemp(r2); |
| 586 | LockTemp(r3); |
| 587 | |
| 588 | /* |
| 589 | * We can safely skip the stack overflow check if we're |
| 590 | * a leaf *and* our frame size < fudge factor. |
| 591 | */ |
| 592 | bool skip_overflow_check = (mir_graph_->MethodIsLeaf() && |
| 593 | (static_cast<size_t>(frame_size_) < |
| 594 | Thread::kStackOverflowReservedBytes)); |
| 595 | NewLIR0(kPseudoMethodEntry); |
| 596 | if (!skip_overflow_check) { |
| 597 | /* Load stack limit */ |
| 598 | LoadWordDisp(rARM_SELF, Thread::StackEndOffset().Int32Value(), r12); |
| 599 | } |
| 600 | /* Spill core callee saves */ |
| 601 | NewLIR1(kThumb2Push, core_spill_mask_); |
| 602 | /* Need to spill any FP regs? */ |
| 603 | if (num_fp_spills_) { |
| 604 | /* |
| 605 | * NOTE: fp spills are a little different from core spills in that |
| 606 | * they are pushed as a contiguous block. When promoting from |
| 607 | * the fp set, we must allocate all singles from s16..highest-promoted |
| 608 | */ |
| 609 | NewLIR1(kThumb2VPushCS, num_fp_spills_); |
| 610 | } |
| 611 | if (!skip_overflow_check) { |
| 612 | OpRegRegImm(kOpSub, rARM_LR, rARM_SP, frame_size_ - (spill_count * 4)); |
| 613 | GenRegRegCheck(kCondCc, rARM_LR, r12, kThrowStackOverflow); |
| 614 | OpRegCopy(rARM_SP, rARM_LR); // Establish stack |
| 615 | } else { |
| 616 | OpRegImm(kOpSub, rARM_SP, frame_size_ - (spill_count * 4)); |
| 617 | } |
| 618 | |
| 619 | FlushIns(ArgLocs, rl_method); |
| 620 | |
| 621 | FreeTemp(r0); |
| 622 | FreeTemp(r1); |
| 623 | FreeTemp(r2); |
| 624 | FreeTemp(r3); |
| 625 | } |
| 626 | |
| 627 | void ArmMir2Lir::GenExitSequence() |
| 628 | { |
| 629 | int spill_count = num_core_spills_ + num_fp_spills_; |
| 630 | /* |
| 631 | * In the exit path, r0/r1 are live - make sure they aren't |
| 632 | * allocated by the register utilities as temps. |
| 633 | */ |
| 634 | LockTemp(r0); |
| 635 | LockTemp(r1); |
| 636 | |
| 637 | NewLIR0(kPseudoMethodExit); |
| 638 | OpRegImm(kOpAdd, rARM_SP, frame_size_ - (spill_count * 4)); |
| 639 | /* Need to restore any FP callee saves? */ |
| 640 | if (num_fp_spills_) { |
| 641 | NewLIR1(kThumb2VPopCS, num_fp_spills_); |
| 642 | } |
| 643 | if (core_spill_mask_ & (1 << rARM_LR)) { |
| 644 | /* Unspill rARM_LR to rARM_PC */ |
| 645 | core_spill_mask_ &= ~(1 << rARM_LR); |
| 646 | core_spill_mask_ |= (1 << rARM_PC); |
| 647 | } |
| 648 | NewLIR1(kThumb2Pop, core_spill_mask_); |
| 649 | if (!(core_spill_mask_ & (1 << rARM_PC))) { |
| 650 | /* We didn't pop to rARM_PC, so must do a bv rARM_LR */ |
| 651 | NewLIR1(kThumbBx, rARM_LR); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | } // namespace art |