Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [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 X86 ISA */ |
| 18 | |
| 19 | #include "codegen_x86.h" |
| 20 | #include "dex/quick/mir_to_lir-inl.h" |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 21 | #include "gc/accounting/card_table.h" |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 22 | #include "mirror/art_method.h" |
| 23 | #include "mirror/object_array-inl.h" |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 24 | #include "x86_lir.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 28 | /* |
| 29 | * The sparse table in the literal pool is an array of <key,displacement> |
| 30 | * pairs. |
| 31 | */ |
Andreas Gampe | 48971b3 | 2014-08-06 10:09:01 -0700 | [diff] [blame] | 32 | void X86Mir2Lir::GenLargeSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) { |
Chao-ying Fu | da96aed | 2014-10-27 14:42:00 -0700 | [diff] [blame^] | 33 | GenSmallSparseSwitch(mir, table_offset, rl_src); |
| 34 | } |
| 35 | |
| 36 | /* |
| 37 | * We override InsertCaseLabel, because the first parameter represents |
| 38 | * a basic block id, instead of a dex offset. |
| 39 | */ |
| 40 | LIR* X86Mir2Lir::InsertCaseLabel(DexOffset bbid, int keyVal) { |
| 41 | LIR* boundary_lir = &block_label_list_[bbid]; |
| 42 | LIR* res = boundary_lir; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 43 | if (cu_->verbose) { |
Chao-ying Fu | da96aed | 2014-10-27 14:42:00 -0700 | [diff] [blame^] | 44 | // Only pay the expense if we're pretty-printing. |
| 45 | LIR* new_label = static_cast<LIR*>(arena_->Alloc(sizeof(LIR), kArenaAllocLIR)); |
| 46 | BasicBlock* bb = mir_graph_->GetBasicBlock(bbid); |
| 47 | DCHECK(bb != nullptr); |
| 48 | new_label->dalvik_offset = bb->start_offset;; |
| 49 | new_label->opcode = kPseudoCaseLabel; |
| 50 | new_label->operands[0] = keyVal; |
| 51 | new_label->flags.fixup = kFixupLabel; |
| 52 | DCHECK(!new_label->flags.use_def_invalid); |
| 53 | new_label->u.m.def_mask = &kEncodeAll; |
| 54 | InsertLIRAfter(boundary_lir, new_label); |
| 55 | res = new_label; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 56 | } |
Chao-ying Fu | da96aed | 2014-10-27 14:42:00 -0700 | [diff] [blame^] | 57 | return res; |
| 58 | } |
| 59 | |
| 60 | void X86Mir2Lir::MarkPackedCaseLabels(Mir2Lir::SwitchTable* tab_rec) { |
| 61 | const uint16_t* table = tab_rec->table; |
| 62 | const int32_t *targets = reinterpret_cast<const int32_t*>(&table[4]); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 63 | int entries = table[1]; |
Chao-ying Fu | da96aed | 2014-10-27 14:42:00 -0700 | [diff] [blame^] | 64 | int low_key = s4FromSwitchData(&table[2]); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 65 | for (int i = 0; i < entries; i++) { |
Chao-ying Fu | da96aed | 2014-10-27 14:42:00 -0700 | [diff] [blame^] | 66 | // The value at targets[i] is a basic block id, instead of a dex offset. |
| 67 | tab_rec->targets[i] = InsertCaseLabel(targets[i], i + low_key); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | /* |
Chao-ying Fu | da96aed | 2014-10-27 14:42:00 -0700 | [diff] [blame^] | 72 | * We convert and create a new packed switch table that stores |
| 73 | * basic block ids to targets[] by examining successor blocks. |
| 74 | * Note that the original packed switch table stores dex offsets to targets[]. |
| 75 | */ |
| 76 | const uint16_t* X86Mir2Lir::ConvertPackedSwitchTable(MIR* mir, const uint16_t* table) { |
| 77 | /* |
| 78 | * The original packed switch data format: |
| 79 | * ushort ident = 0x0100 magic value |
| 80 | * ushort size number of entries in the table |
| 81 | * int first_key first (and lowest) switch case value |
| 82 | * int targets[size] branch targets, relative to switch opcode |
| 83 | * |
| 84 | * Total size is (4+size*2) 16-bit code units. |
| 85 | * |
| 86 | * Note that the new packed switch data format is the same as the original |
| 87 | * format, except that targets[] are basic block ids. |
| 88 | * |
| 89 | */ |
| 90 | BasicBlock* bb = mir_graph_->GetBasicBlock(mir->bb); |
| 91 | DCHECK(bb != nullptr); |
| 92 | // Get the number of entries. |
| 93 | int entries = table[1]; |
| 94 | const int32_t* as_int32 = reinterpret_cast<const int32_t*>(&table[2]); |
| 95 | int32_t starting_key = as_int32[0]; |
| 96 | // Create a new table. |
| 97 | int size = sizeof(uint16_t) * (4 + entries * 2); |
| 98 | uint16_t* new_table = reinterpret_cast<uint16_t*>(arena_->Alloc(size, kArenaAllocMisc)); |
| 99 | // Copy ident, size, and first_key to the new table. |
| 100 | memcpy(new_table, table, sizeof(uint16_t) * 4); |
| 101 | // Get the new targets. |
| 102 | int32_t* new_targets = reinterpret_cast<int32_t*>(&new_table[4]); |
| 103 | // Find out targets for each entry. |
| 104 | int i = 0; |
| 105 | for (SuccessorBlockInfo* successor_block_info : bb->successor_blocks) { |
| 106 | DCHECK_EQ(starting_key + i, successor_block_info->key); |
| 107 | // Save target basic block id. |
| 108 | new_targets[i++] = successor_block_info->block; |
| 109 | } |
| 110 | DCHECK_EQ(i, entries); |
| 111 | return new_table; |
| 112 | } |
| 113 | |
| 114 | /* |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 115 | * Code pattern will look something like: |
| 116 | * |
| 117 | * mov r_val, .. |
| 118 | * call 0 |
| 119 | * pop r_start_of_method |
| 120 | * sub r_start_of_method, .. |
| 121 | * mov r_key_reg, r_val |
| 122 | * sub r_key_reg, low_key |
| 123 | * cmp r_key_reg, size-1 ; bound check |
| 124 | * ja done |
| 125 | * mov r_disp, [r_start_of_method + r_key_reg * 4 + table_offset] |
| 126 | * add r_start_of_method, r_disp |
| 127 | * jmp r_start_of_method |
| 128 | * done: |
| 129 | */ |
Andreas Gampe | 48971b3 | 2014-08-06 10:09:01 -0700 | [diff] [blame] | 130 | void X86Mir2Lir::GenLargePackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) { |
Chao-ying Fu | da96aed | 2014-10-27 14:42:00 -0700 | [diff] [blame^] | 131 | const uint16_t* old_table = mir_graph_->GetTable(mir, table_offset); |
| 132 | const uint16_t* table = ConvertPackedSwitchTable(mir, old_table); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 133 | // Add the table to the list - we'll process it later |
buzbee | 0d82948 | 2013-10-11 15:24:55 -0700 | [diff] [blame] | 134 | SwitchTable* tab_rec = |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 135 | static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 136 | tab_rec->table = table; |
| 137 | tab_rec->vaddr = current_dalvik_offset_; |
| 138 | int size = table[1]; |
Mathieu Chartier | f6c4b3b | 2013-08-24 16:11:37 -0700 | [diff] [blame] | 139 | tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), |
Vladimir Marko | 83cc7ae | 2014-02-12 18:02:05 +0000 | [diff] [blame] | 140 | kArenaAllocLIR)); |
Vladimir Marko | e39c54e | 2014-09-22 14:50:02 +0100 | [diff] [blame] | 141 | switch_tables_.push_back(tab_rec); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 142 | |
| 143 | // Get the switch value |
| 144 | rl_src = LoadValue(rl_src, kCoreReg); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 145 | // NewLIR0(kX86Bkpt); |
Mark Mendell | 67c39c4 | 2014-01-31 17:28:00 -0800 | [diff] [blame] | 146 | |
| 147 | // Materialize a pointer to the switch table |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 148 | RegStorage start_of_method_reg; |
Mark Mendell | 67c39c4 | 2014-01-31 17:28:00 -0800 | [diff] [blame] | 149 | if (base_of_code_ != nullptr) { |
| 150 | // We can use the saved value. |
| 151 | RegLocation rl_method = mir_graph_->GetRegLocation(base_of_code_->s_reg_low); |
Chao-ying Fu | e0ccdc0 | 2014-06-06 17:32:37 -0700 | [diff] [blame] | 152 | if (rl_method.wide) { |
| 153 | rl_method = LoadValueWide(rl_method, kCoreReg); |
| 154 | } else { |
| 155 | rl_method = LoadValue(rl_method, kCoreReg); |
| 156 | } |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 157 | start_of_method_reg = rl_method.reg; |
Mark Mendell | 55d0eac | 2014-02-06 11:02:52 -0800 | [diff] [blame] | 158 | store_method_addr_used_ = true; |
Mark Mendell | 67c39c4 | 2014-01-31 17:28:00 -0800 | [diff] [blame] | 159 | } else { |
Serguei Katkov | 407a9d2 | 2014-07-05 03:09:32 +0700 | [diff] [blame] | 160 | start_of_method_reg = AllocTempRef(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 161 | NewLIR1(kX86StartOfMethod, start_of_method_reg.GetReg()); |
Mark Mendell | 67c39c4 | 2014-01-31 17:28:00 -0800 | [diff] [blame] | 162 | } |
Serguei Katkov | 407a9d2 | 2014-07-05 03:09:32 +0700 | [diff] [blame] | 163 | DCHECK_EQ(start_of_method_reg.Is64Bit(), cu_->target64); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 164 | int low_key = s4FromSwitchData(&table[2]); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 165 | RegStorage keyReg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 166 | // Remove the bias, if necessary |
| 167 | if (low_key == 0) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 168 | keyReg = rl_src.reg; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 169 | } else { |
| 170 | keyReg = AllocTemp(); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 171 | OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 172 | } |
| 173 | // Bounds check - if < 0 or >= size continue following switch |
Serguei Katkov | 407a9d2 | 2014-07-05 03:09:32 +0700 | [diff] [blame] | 174 | OpRegImm(kOpCmp, keyReg, size - 1); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 175 | LIR* branch_over = OpCondBranch(kCondHi, NULL); |
| 176 | |
| 177 | // Load the displacement from the switch table |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 178 | RegStorage disp_reg = AllocTemp(); |
Chao-ying Fu | e0ccdc0 | 2014-06-06 17:32:37 -0700 | [diff] [blame] | 179 | NewLIR5(kX86PcRelLoadRA, disp_reg.GetReg(), start_of_method_reg.GetReg(), keyReg.GetReg(), |
| 180 | 2, WrapPointer(tab_rec)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 181 | // Add displacement to start of method |
Serguei Katkov | 407a9d2 | 2014-07-05 03:09:32 +0700 | [diff] [blame] | 182 | OpRegReg(kOpAdd, start_of_method_reg, cu_->target64 ? As64BitReg(disp_reg) : disp_reg); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 183 | // ..and go! |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 184 | LIR* switch_branch = NewLIR1(kX86JmpR, start_of_method_reg.GetReg()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 185 | tab_rec->anchor = switch_branch; |
| 186 | |
| 187 | /* branch_over target here */ |
| 188 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 189 | branch_over->target = target; |
| 190 | } |
| 191 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 192 | void X86Mir2Lir::GenMoveException(RegLocation rl_dest) { |
buzbee | 33ae558 | 2014-06-12 14:56:32 -0700 | [diff] [blame] | 193 | int ex_offset = cu_->target64 ? |
Andreas Gampe | 2f244e9 | 2014-05-08 03:35:25 -0700 | [diff] [blame] | 194 | Thread::ExceptionOffset<8>().Int32Value() : |
| 195 | Thread::ExceptionOffset<4>().Int32Value(); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 196 | RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true); |
Serguei Katkov | 407a9d2 | 2014-07-05 03:09:32 +0700 | [diff] [blame] | 197 | NewLIR2(cu_->target64 ? kX86Mov64RT : kX86Mov32RT, rl_result.reg.GetReg(), ex_offset); |
| 198 | NewLIR2(cu_->target64 ? kX86Mov64TI : kX86Mov32TI, ex_offset, 0); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 199 | StoreValue(rl_dest, rl_result); |
| 200 | } |
| 201 | |
Vladimir Marko | bf535be | 2014-11-19 18:52:35 +0000 | [diff] [blame] | 202 | void X86Mir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) { |
Serguei Katkov | 407a9d2 | 2014-07-05 03:09:32 +0700 | [diff] [blame] | 203 | DCHECK_EQ(tgt_addr_reg.Is64Bit(), cu_->target64); |
Serguei Katkov | 407a9d2 | 2014-07-05 03:09:32 +0700 | [diff] [blame] | 204 | RegStorage reg_card_base = AllocTempRef(); |
| 205 | RegStorage reg_card_no = AllocTempRef(); |
buzbee | 33ae558 | 2014-06-12 14:56:32 -0700 | [diff] [blame] | 206 | int ct_offset = cu_->target64 ? |
Andreas Gampe | 2f244e9 | 2014-05-08 03:35:25 -0700 | [diff] [blame] | 207 | Thread::CardTableOffset<8>().Int32Value() : |
| 208 | Thread::CardTableOffset<4>().Int32Value(); |
Serguei Katkov | 407a9d2 | 2014-07-05 03:09:32 +0700 | [diff] [blame] | 209 | NewLIR2(cu_->target64 ? kX86Mov64RT : kX86Mov32RT, reg_card_base.GetReg(), ct_offset); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 210 | OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 211 | StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 212 | FreeTemp(reg_card_base); |
| 213 | FreeTemp(reg_card_no); |
| 214 | } |
| 215 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 216 | void X86Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 217 | /* |
| 218 | * On entry, rX86_ARG0, rX86_ARG1, rX86_ARG2 are live. Let the register |
| 219 | * allocation mechanism know so it doesn't try to use any of them when |
| 220 | * expanding the frame or flushing. This leaves the utility |
| 221 | * code with no spare temps. |
| 222 | */ |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 223 | const RegStorage arg0 = TargetReg32(kArg0); |
| 224 | const RegStorage arg1 = TargetReg32(kArg1); |
| 225 | const RegStorage arg2 = TargetReg32(kArg2); |
| 226 | LockTemp(arg0); |
| 227 | LockTemp(arg1); |
| 228 | LockTemp(arg2); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 229 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 230 | /* |
| 231 | * We can safely skip the stack overflow check if we're |
| 232 | * a leaf *and* our frame size < fudge factor. |
| 233 | */ |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 234 | const InstructionSet isa = cu_->target64 ? kX86_64 : kX86; |
Dave Allison | 648d711 | 2014-07-25 16:15:27 -0700 | [diff] [blame] | 235 | bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, isa); |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 236 | const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 237 | |
| 238 | // If we doing an implicit stack overflow check, perform the load immediately |
| 239 | // before the stack pointer is decremented and anything is saved. |
| 240 | if (!skip_overflow_check && |
| 241 | cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) { |
| 242 | // Implicit stack overflow check. |
| 243 | // test eax,[esp + -overflow] |
| 244 | int overflow = GetStackOverflowReservedBytes(isa); |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 245 | NewLIR3(kX86Test32RM, rs_rAX.GetReg(), rs_rSP.GetReg(), -overflow); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 246 | MarkPossibleStackOverflowException(); |
| 247 | } |
| 248 | |
| 249 | /* Build frame, return address already on stack */ |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 250 | stack_decrement_ = OpRegImm(kOpSub, rs_rSP, frame_size_ - |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 251 | GetInstructionSetPointerSize(cu_->instruction_set)); |
| 252 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 253 | NewLIR0(kPseudoMethodEntry); |
| 254 | /* Spill core callee saves */ |
| 255 | SpillCoreRegs(); |
Serguei Katkov | c380191 | 2014-07-08 17:21:53 +0700 | [diff] [blame] | 256 | SpillFPRegs(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 257 | if (!skip_overflow_check) { |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 258 | class StackOverflowSlowPath : public LIRSlowPath { |
| 259 | public: |
| 260 | StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace) |
| 261 | : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), sp_displace_(sp_displace) { |
| 262 | } |
| 263 | void Compile() OVERRIDE { |
| 264 | m2l_->ResetRegPool(); |
| 265 | m2l_->ResetDefTracking(); |
Mingyao Yang | 6ffcfa0 | 2014-04-25 11:06:00 -0700 | [diff] [blame] | 266 | GenerateTargetLabel(kPseudoThrowTarget); |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 267 | const RegStorage local_rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32; |
| 268 | m2l_->OpRegImm(kOpAdd, local_rs_rSP, sp_displace_); |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 269 | m2l_->ClobberCallerSave(); |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 270 | // Assumes codegen and target are in thumb2 mode. |
Andreas Gampe | 9843059 | 2014-07-27 19:44:50 -0700 | [diff] [blame] | 271 | m2l_->CallHelper(RegStorage::InvalidReg(), kQuickThrowStackOverflow, |
| 272 | false /* MarkSafepointPC */, false /* UseLink */); |
Mathieu Chartier | 0d507d1 | 2014-03-19 10:17:28 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | private: |
| 276 | const size_t sp_displace_; |
| 277 | }; |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 278 | if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) { |
| 279 | // TODO: for large frames we should do something like: |
| 280 | // spill ebp |
| 281 | // lea ebp, [esp + frame_size] |
| 282 | // cmp ebp, fs:[stack_end_] |
| 283 | // jcc stack_overflow_exception |
| 284 | // mov esp, ebp |
| 285 | // in case a signal comes in that's not using an alternate signal stack and the large frame |
| 286 | // may have moved us outside of the reserved area at the end of the stack. |
| 287 | // cmp rs_rX86_SP, fs:[stack_end_]; jcc throw_slowpath |
| 288 | if (cu_->target64) { |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 289 | OpRegThreadMem(kOpCmp, rs_rX86_SP_64, Thread::StackEndOffset<8>()); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 290 | } else { |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 291 | OpRegThreadMem(kOpCmp, rs_rX86_SP_32, Thread::StackEndOffset<4>()); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 292 | } |
| 293 | LIR* branch = OpCondBranch(kCondUlt, nullptr); |
| 294 | AddSlowPath( |
Chao-ying Fu | e0ccdc0 | 2014-06-06 17:32:37 -0700 | [diff] [blame] | 295 | new(arena_)StackOverflowSlowPath(this, branch, |
| 296 | frame_size_ - |
| 297 | GetInstructionSetPointerSize(cu_->instruction_set))); |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 298 | } |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | FlushIns(ArgLocs, rl_method); |
| 302 | |
Mark Mendell | 67c39c4 | 2014-01-31 17:28:00 -0800 | [diff] [blame] | 303 | if (base_of_code_ != nullptr) { |
Chao-ying Fu | a77ee51 | 2014-07-01 17:43:41 -0700 | [diff] [blame] | 304 | RegStorage method_start = TargetPtrReg(kArg0); |
Mark Mendell | 67c39c4 | 2014-01-31 17:28:00 -0800 | [diff] [blame] | 305 | // We have been asked to save the address of the method start for later use. |
Chao-ying Fu | a77ee51 | 2014-07-01 17:43:41 -0700 | [diff] [blame] | 306 | setup_method_address_[0] = NewLIR1(kX86StartOfMethod, method_start.GetReg()); |
Mark Mendell | 67c39c4 | 2014-01-31 17:28:00 -0800 | [diff] [blame] | 307 | int displacement = SRegOffset(base_of_code_->s_reg_low); |
buzbee | 695d13a | 2014-04-19 13:32:20 -0700 | [diff] [blame] | 308 | // Native pointer - must be natural word size. |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 309 | setup_method_address_[1] = StoreBaseDisp(rs_rSP, displacement, method_start, |
Elena Sayapina | dd64450 | 2014-07-01 18:39:52 +0700 | [diff] [blame] | 310 | cu_->target64 ? k64 : k32, kNotVolatile); |
Mark Mendell | 67c39c4 | 2014-01-31 17:28:00 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 313 | FreeTemp(arg0); |
| 314 | FreeTemp(arg1); |
| 315 | FreeTemp(arg2); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | void X86Mir2Lir::GenExitSequence() { |
| 319 | /* |
| 320 | * In the exit path, rX86_RET0/rX86_RET1 are live - make sure they aren't |
| 321 | * allocated by the register utilities as temps. |
| 322 | */ |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 323 | LockTemp(rs_rX86_RET0); |
| 324 | LockTemp(rs_rX86_RET1); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 325 | |
| 326 | NewLIR0(kPseudoMethodExit); |
| 327 | UnSpillCoreRegs(); |
Serguei Katkov | c380191 | 2014-07-08 17:21:53 +0700 | [diff] [blame] | 328 | UnSpillFPRegs(); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 329 | /* Remove frame except for return address */ |
Ian Rogers | b28c1c0 | 2014-11-08 11:21:21 -0800 | [diff] [blame] | 330 | const RegStorage rs_rSP = cu_->target64 ? rs_rX86_SP_64 : rs_rX86_SP_32; |
| 331 | stack_increment_ = OpRegImm(kOpAdd, rs_rSP, |
| 332 | frame_size_ - GetInstructionSetPointerSize(cu_->instruction_set)); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 333 | NewLIR0(kX86Ret); |
| 334 | } |
| 335 | |
Razvan A Lupusoru | 3bc0174 | 2014-02-06 13:18:43 -0800 | [diff] [blame] | 336 | void X86Mir2Lir::GenSpecialExitSequence() { |
| 337 | NewLIR0(kX86Ret); |
| 338 | } |
| 339 | |
Dave Allison | 69dfe51 | 2014-07-11 17:11:58 +0000 | [diff] [blame] | 340 | void X86Mir2Lir::GenImplicitNullCheck(RegStorage reg, int opt_flags) { |
| 341 | if (!(cu_->disable_opt & (1 << kNullCheckElimination)) && (opt_flags & MIR_IGNORE_NULL_CHECK)) { |
| 342 | return; |
| 343 | } |
| 344 | // Implicit null pointer check. |
| 345 | // test eax,[arg1+0] |
| 346 | NewLIR3(kX86Test32RM, rs_rAX.GetReg(), reg.GetReg(), 0); |
| 347 | MarkPossibleNullPointerException(opt_flags); |
| 348 | } |
| 349 | |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 350 | /* |
| 351 | * Bit of a hack here - in the absence of a real scheduling pass, |
| 352 | * emit the next instruction in static & direct invoke sequences. |
| 353 | */ |
| 354 | static int X86NextSDCallInsn(CompilationUnit* cu, CallInfo* info, |
| 355 | int state, const MethodReference& target_method, |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 356 | uint32_t, |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 357 | uintptr_t direct_code, uintptr_t direct_method, |
| 358 | InvokeType type) { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 359 | UNUSED(info, direct_code); |
Vladimir Marko | f4da675 | 2014-08-01 19:04:18 +0100 | [diff] [blame] | 360 | Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get()); |
| 361 | if (direct_method != 0) { |
| 362 | switch (state) { |
| 363 | case 0: // Get the current Method* [sets kArg0] |
| 364 | if (direct_method != static_cast<uintptr_t>(-1)) { |
| 365 | cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method); |
| 366 | } else { |
| 367 | cg->LoadMethodAddress(target_method, type, kArg0); |
| 368 | } |
| 369 | break; |
| 370 | default: |
| 371 | return -1; |
| 372 | } |
| 373 | } else { |
| 374 | RegStorage arg0_ref = cg->TargetReg(kArg0, kRef); |
| 375 | switch (state) { |
| 376 | case 0: // Get the current Method* [sets kArg0] |
| 377 | // TUNING: we can save a reg copy if Method* has been promoted. |
| 378 | cg->LoadCurrMethodDirect(arg0_ref); |
| 379 | break; |
| 380 | case 1: // Get method->dex_cache_resolved_methods_ |
| 381 | cg->LoadRefDisp(arg0_ref, |
| 382 | mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(), |
| 383 | arg0_ref, |
| 384 | kNotVolatile); |
| 385 | break; |
| 386 | case 2: // Grab target method* |
| 387 | CHECK_EQ(cu->dex_file, target_method.dex_file); |
| 388 | cg->LoadRefDisp(arg0_ref, |
| 389 | mirror::ObjectArray<mirror::Object>::OffsetOfElement( |
| 390 | target_method.dex_method_index).Int32Value(), |
| 391 | arg0_ref, |
| 392 | kNotVolatile); |
| 393 | break; |
| 394 | default: |
| 395 | return -1; |
| 396 | } |
| 397 | } |
| 398 | return state + 1; |
| 399 | } |
| 400 | |
| 401 | NextCallInsn X86Mir2Lir::GetNextSDCallInsn() { |
| 402 | return X86NextSDCallInsn; |
| 403 | } |
| 404 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 405 | } // namespace art |