Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [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 "arm64_lir.h" |
| 20 | #include "codegen_arm64.h" |
| 21 | #include "dex/quick/mir_to_lir-inl.h" |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 22 | #include "gc/accounting/card_table.h" |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 23 | #include "entrypoints/quick/quick_entrypoints.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 27 | bool Arm64Mir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, |
| 28 | const InlineMethod& special) { |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 29 | // TODO(Arm64): re-enable this, once hard-float ABI is implemented. |
| 30 | // (this currently does not work, as GetArgMappingToPhysicalReg returns InvalidReg()). |
| 31 | // return Mir2Lir::GenSpecialCase(bb, mir, special); |
| 32 | return false; |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 33 | } |
| 34 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 35 | /* |
| 36 | * The sparse table in the literal pool is an array of <key,displacement> |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 37 | * pairs. For each set, we'll load them as a pair using ldp. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 38 | * The test loop will look something like: |
| 39 | * |
| 40 | * adr r_base, <table> |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 41 | * ldr r_val, [rA64_SP, v_reg_off] |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 42 | * mov r_idx, #table_size |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 43 | * loop: |
| 44 | * cbz r_idx, quit |
| 45 | * ldp r_key, r_disp, [r_base], #8 |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 46 | * sub r_idx, #1 |
| 47 | * cmp r_val, r_key |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 48 | * b.ne loop |
| 49 | * adr r_base, #0 ; This is the instruction from which we compute displacements |
| 50 | * add r_base, r_disp |
| 51 | * br r_base |
| 52 | * quit: |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 53 | */ |
| 54 | void Arm64Mir2Lir::GenSparseSwitch(MIR* mir, uint32_t table_offset, |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 55 | RegLocation rl_src) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 56 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 57 | if (cu_->verbose) { |
| 58 | DumpSparseSwitchTable(table); |
| 59 | } |
| 60 | // Add the table to the list - we'll process it later |
| 61 | SwitchTable *tab_rec = |
| 62 | static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData)); |
| 63 | tab_rec->table = table; |
| 64 | tab_rec->vaddr = current_dalvik_offset_; |
| 65 | uint32_t size = table[1]; |
| 66 | tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR)); |
| 67 | switch_tables_.Insert(tab_rec); |
| 68 | |
| 69 | // Get the switch value |
| 70 | rl_src = LoadValue(rl_src, kCoreReg); |
Matteo Franchin | 5acc8b0 | 2014-06-05 15:10:35 +0100 | [diff] [blame] | 71 | RegStorage r_base = AllocTempWide(); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 72 | // Allocate key and disp temps. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 73 | RegStorage r_key = AllocTemp(); |
| 74 | RegStorage r_disp = AllocTemp(); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 75 | // Materialize a pointer to the switch table |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 76 | NewLIR3(kA64Adr2xd, r_base.GetReg(), 0, WrapPointer(tab_rec)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 77 | // Set up r_idx |
| 78 | RegStorage r_idx = AllocTemp(); |
| 79 | LoadConstant(r_idx, size); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 80 | |
| 81 | // Entry of loop. |
| 82 | LIR* loop_entry = NewLIR0(kPseudoTargetLabel); |
| 83 | LIR* branch_out = NewLIR2(kA64Cbz2rt, r_idx.GetReg(), 0); |
| 84 | |
| 85 | // Load next key/disp. |
| 86 | NewLIR4(kA64LdpPost4rrXD, r_key.GetReg(), r_disp.GetReg(), r_base.GetReg(), 2); |
| 87 | OpRegRegImm(kOpSub, r_idx, r_idx, 1); |
| 88 | |
| 89 | // Go to next case, if key does not match. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 90 | OpRegReg(kOpCmp, r_key, rl_src.reg); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 91 | OpCondBranch(kCondNe, loop_entry); |
| 92 | |
| 93 | // Key does match: branch to case label. |
| 94 | LIR* switch_label = NewLIR3(kA64Adr2xd, r_base.GetReg(), 0, -1); |
| 95 | tab_rec->anchor = switch_label; |
| 96 | |
| 97 | // Add displacement to base branch address and go! |
Matteo Franchin | 5acc8b0 | 2014-06-05 15:10:35 +0100 | [diff] [blame] | 98 | // TODO(Arm64): generate "add x1, x1, w3, sxtw" rather than "add x1, x1, x3"? |
| 99 | OpRegRegRegShift(kOpAdd, r_base, r_base, As64BitReg(r_disp), ENCODE_NO_SHIFT); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 100 | NewLIR1(kA64Br1x, r_base.GetReg()); |
| 101 | |
| 102 | // Loop exit label. |
| 103 | LIR* loop_exit = NewLIR0(kPseudoTargetLabel); |
| 104 | branch_out->target = loop_exit; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | |
| 108 | void Arm64Mir2Lir::GenPackedSwitch(MIR* mir, uint32_t table_offset, |
Matteo Franchin | 5acc8b0 | 2014-06-05 15:10:35 +0100 | [diff] [blame] | 109 | RegLocation rl_src) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 110 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 111 | if (cu_->verbose) { |
| 112 | DumpPackedSwitchTable(table); |
| 113 | } |
| 114 | // Add the table to the list - we'll process it later |
| 115 | SwitchTable *tab_rec = |
| 116 | static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData)); |
| 117 | tab_rec->table = table; |
| 118 | tab_rec->vaddr = current_dalvik_offset_; |
| 119 | uint32_t size = table[1]; |
| 120 | tab_rec->targets = |
| 121 | static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR)); |
| 122 | switch_tables_.Insert(tab_rec); |
| 123 | |
| 124 | // Get the switch value |
| 125 | rl_src = LoadValue(rl_src, kCoreReg); |
Matteo Franchin | 5acc8b0 | 2014-06-05 15:10:35 +0100 | [diff] [blame] | 126 | RegStorage table_base = AllocTempWide(); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 127 | // Materialize a pointer to the switch table |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 128 | NewLIR3(kA64Adr2xd, table_base.GetReg(), 0, WrapPointer(tab_rec)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 129 | int low_key = s4FromSwitchData(&table[2]); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 130 | RegStorage key_reg; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 131 | // Remove the bias, if necessary |
| 132 | if (low_key == 0) { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 133 | key_reg = rl_src.reg; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 134 | } else { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 135 | key_reg = AllocTemp(); |
| 136 | OpRegRegImm(kOpSub, key_reg, rl_src.reg, low_key); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 137 | } |
| 138 | // Bounds check - if < 0 or >= size continue following switch |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 139 | OpRegImm(kOpCmp, key_reg, size - 1); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 140 | LIR* branch_over = OpCondBranch(kCondHi, NULL); |
| 141 | |
| 142 | // Load the displacement from the switch table |
| 143 | RegStorage disp_reg = AllocTemp(); |
Matteo Franchin | 5acc8b0 | 2014-06-05 15:10:35 +0100 | [diff] [blame] | 144 | // TODO(Arm64): generate "ldr w3, [x1,w2,sxtw #2]" rather than "ldr w3, [x1,x2,lsl #2]"? |
| 145 | LoadBaseIndexed(table_base, key_reg, As64BitReg(disp_reg), 2, k32); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 146 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 147 | // Get base branch address. |
Matteo Franchin | 5acc8b0 | 2014-06-05 15:10:35 +0100 | [diff] [blame] | 148 | RegStorage branch_reg = AllocTempWide(); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 149 | LIR* switch_label = NewLIR3(kA64Adr2xd, branch_reg.GetReg(), 0, -1); |
| 150 | tab_rec->anchor = switch_label; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 151 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 152 | // Add displacement to base branch address and go! |
Matteo Franchin | 5acc8b0 | 2014-06-05 15:10:35 +0100 | [diff] [blame] | 153 | // TODO(Arm64): generate "add x4, x4, w3, sxtw" rather than "add x4, x4, x3"? |
| 154 | OpRegRegRegShift(kOpAdd, branch_reg, branch_reg, As64BitReg(disp_reg), ENCODE_NO_SHIFT); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 155 | NewLIR1(kA64Br1x, branch_reg.GetReg()); |
| 156 | |
| 157 | // branch_over target here |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 158 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 159 | branch_over->target = target; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Array data table format: |
| 164 | * ushort ident = 0x0300 magic value |
| 165 | * ushort width width of each element in the table |
| 166 | * uint size number of elements in the table |
| 167 | * ubyte data[size*width] table of data values (may contain a single-byte |
| 168 | * padding at the end) |
| 169 | * |
| 170 | * Total size is 4+(width * size + 1)/2 16-bit code units. |
| 171 | */ |
| 172 | void Arm64Mir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src) { |
| 173 | const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset; |
| 174 | // Add the table to the list - we'll process it later |
| 175 | FillArrayData *tab_rec = |
| 176 | static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), kArenaAllocData)); |
| 177 | tab_rec->table = table; |
| 178 | tab_rec->vaddr = current_dalvik_offset_; |
| 179 | uint16_t width = tab_rec->table[1]; |
| 180 | uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16); |
| 181 | tab_rec->size = (size * width) + 8; |
| 182 | |
| 183 | fill_array_data_.Insert(tab_rec); |
| 184 | |
| 185 | // Making a call - use explicit registers |
| 186 | FlushAllRegs(); /* Everything to home location */ |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 187 | LoadValueDirectFixed(rl_src, rs_x0); |
Andreas Gampe | 2f244e9 | 2014-05-08 03:35:25 -0700 | [diff] [blame] | 188 | LoadWordDisp(rs_rA64_SELF, QUICK_ENTRYPOINT_OFFSET(8, pHandleFillArrayData).Int32Value(), |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 189 | rs_rA64_LR); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 190 | // Materialize a pointer to the fill data image |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 191 | NewLIR3(kA64Adr2xd, rx1, 0, WrapPointer(tab_rec)); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 192 | ClobberCallerSave(); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 193 | LIR* call_inst = OpReg(kOpBlx, rs_rA64_LR); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 194 | MarkSafepointPC(call_inst); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more |
| 199 | * details see monitor.cc. |
| 200 | */ |
| 201 | void Arm64Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) { |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 202 | // x0/w0 = object |
| 203 | // w1 = thin lock thread id |
| 204 | // x2 = address of lock word |
| 205 | // w3 = lock word / store failure |
| 206 | // TUNING: How much performance we get when we inline this? |
| 207 | // Since we've already flush all register. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 208 | FlushAllRegs(); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 209 | LoadValueDirectFixed(rl_src, rs_w0); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 210 | LockCallTemps(); // Prepare for explicit register usage |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 211 | LIR* null_check_branch = nullptr; |
| 212 | if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) { |
| 213 | null_check_branch = nullptr; // No null check. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 214 | } else { |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 215 | // If the null-check fails its handled by the slow-path to reduce exception related meta-data. |
| 216 | if (Runtime::Current()->ExplicitNullChecks()) { |
| 217 | null_check_branch = OpCmpImmBranch(kCondEq, rs_x0, 0, NULL); |
| 218 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 219 | } |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 220 | Load32Disp(rs_rA64_SELF, Thread::ThinLockIdOffset<8>().Int32Value(), rs_w1); |
| 221 | OpRegRegImm(kOpAdd, rs_x2, rs_x0, mirror::Object::MonitorOffset().Int32Value()); |
| 222 | NewLIR2(kA64Ldxr2rX, rw3, rx2); |
| 223 | MarkPossibleNullPointerException(opt_flags); |
| 224 | LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_x1, 0, NULL); |
| 225 | NewLIR3(kA64Stxr3wrX, rw3, rw1, rx2); |
| 226 | LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_x1, 0, NULL); |
| 227 | |
| 228 | LIR* slow_path_target = NewLIR0(kPseudoTargetLabel); |
| 229 | not_unlocked_branch->target = slow_path_target; |
| 230 | if (null_check_branch != nullptr) { |
| 231 | null_check_branch->target = slow_path_target; |
| 232 | } |
| 233 | // TODO: move to a slow path. |
| 234 | // Go expensive route - artLockObjectFromCode(obj); |
| 235 | LoadWordDisp(rs_rA64_SELF, QUICK_ENTRYPOINT_OFFSET(8, pLockObject).Int32Value(), rs_rA64_LR); |
| 236 | ClobberCallerSave(); |
| 237 | LIR* call_inst = OpReg(kOpBlx, rs_rA64_LR); |
| 238 | MarkSafepointPC(call_inst); |
| 239 | |
| 240 | LIR* success_target = NewLIR0(kPseudoTargetLabel); |
| 241 | lock_success_branch->target = success_target; |
| 242 | GenMemBarrier(kLoadLoad); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* |
| 246 | * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 247 | * details see monitor.cc. Note the code below doesn't use ldxr/stxr as the code holds the lock |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 248 | * and can only give away ownership if its suspended. |
| 249 | */ |
| 250 | void Arm64Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) { |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 251 | // x0/w0 = object |
| 252 | // w1 = thin lock thread id |
| 253 | // w2 = lock word |
| 254 | // TUNING: How much performance we get when we inline this? |
| 255 | // Since we've already flush all register. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 256 | FlushAllRegs(); |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 257 | LoadValueDirectFixed(rl_src, rs_w0); // Get obj |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 258 | LockCallTemps(); // Prepare for explicit register usage |
| 259 | LIR* null_check_branch = nullptr; |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 260 | if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) { |
| 261 | null_check_branch = nullptr; // No null check. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 262 | } else { |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 263 | // If the null-check fails its handled by the slow-path to reduce exception related meta-data. |
| 264 | if (Runtime::Current()->ExplicitNullChecks()) { |
| 265 | null_check_branch = OpCmpImmBranch(kCondEq, rs_x0, 0, NULL); |
| 266 | } |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 267 | } |
Zheng Xu | c830430 | 2014-05-15 17:21:01 +0100 | [diff] [blame] | 268 | Load32Disp(rs_rA64_SELF, Thread::ThinLockIdOffset<8>().Int32Value(), rs_w1); |
| 269 | Load32Disp(rs_x0, mirror::Object::MonitorOffset().Int32Value(), rs_w2); |
| 270 | MarkPossibleNullPointerException(opt_flags); |
| 271 | LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_w1, rs_w2, NULL); |
| 272 | GenMemBarrier(kStoreLoad); |
| 273 | Store32Disp(rs_x0, mirror::Object::MonitorOffset().Int32Value(), rs_xzr); |
| 274 | LIR* unlock_success_branch = OpUnconditionalBranch(NULL); |
| 275 | |
| 276 | LIR* slow_path_target = NewLIR0(kPseudoTargetLabel); |
| 277 | slow_unlock_branch->target = slow_path_target; |
| 278 | if (null_check_branch != nullptr) { |
| 279 | null_check_branch->target = slow_path_target; |
| 280 | } |
| 281 | // TODO: move to a slow path. |
| 282 | // Go expensive route - artUnlockObjectFromCode(obj); |
| 283 | LoadWordDisp(rs_rA64_SELF, QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject).Int32Value(), rs_rA64_LR); |
| 284 | ClobberCallerSave(); |
| 285 | LIR* call_inst = OpReg(kOpBlx, rs_rA64_LR); |
| 286 | MarkSafepointPC(call_inst); |
| 287 | |
| 288 | LIR* success_target = NewLIR0(kPseudoTargetLabel); |
| 289 | unlock_success_branch->target = success_target; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void Arm64Mir2Lir::GenMoveException(RegLocation rl_dest) { |
Andreas Gampe | 2f244e9 | 2014-05-08 03:35:25 -0700 | [diff] [blame] | 293 | int ex_offset = Thread::ExceptionOffset<8>().Int32Value(); |
buzbee | a0cd2d7 | 2014-06-01 09:33:49 -0700 | [diff] [blame] | 294 | RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true); |
| 295 | LoadRefDisp(rs_rA64_SELF, ex_offset, rl_result.reg); |
| 296 | StoreRefDisp(rs_rA64_SELF, ex_offset, rs_xzr); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 297 | StoreValue(rl_dest, rl_result); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Mark garbage collection card. Skip if the value we're storing is null. |
| 302 | */ |
| 303 | void Arm64Mir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) { |
| 304 | RegStorage reg_card_base = AllocTemp(); |
| 305 | RegStorage reg_card_no = AllocTemp(); |
| 306 | LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL); |
Andreas Gampe | 2f244e9 | 2014-05-08 03:35:25 -0700 | [diff] [blame] | 307 | LoadWordDisp(rs_rA64_SELF, Thread::CardTableOffset<8>().Int32Value(), reg_card_base); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 308 | OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift); |
| 309 | StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte); |
| 310 | LIR* target = NewLIR0(kPseudoTargetLabel); |
| 311 | branch_over->target = target; |
| 312 | FreeTemp(reg_card_base); |
| 313 | FreeTemp(reg_card_no); |
| 314 | } |
| 315 | |
| 316 | void Arm64Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 317 | /* |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 318 | * On entry, x0 to x7 are live. Let the register allocation |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 319 | * mechanism know so it doesn't try to use any of them when |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 320 | * expanding the frame or flushing. |
| 321 | * Reserve x8 & x9 for temporaries. |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 322 | */ |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 323 | LockTemp(rs_x0); |
| 324 | LockTemp(rs_x1); |
| 325 | LockTemp(rs_x2); |
| 326 | LockTemp(rs_x3); |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 327 | LockTemp(rs_x4); |
| 328 | LockTemp(rs_x5); |
| 329 | LockTemp(rs_x6); |
| 330 | LockTemp(rs_x7); |
| 331 | LockTemp(rs_x8); |
| 332 | LockTemp(rs_x9); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 333 | |
| 334 | /* |
| 335 | * We can safely skip the stack overflow check if we're |
| 336 | * a leaf *and* our frame size < fudge factor. |
| 337 | */ |
| 338 | bool skip_overflow_check = (mir_graph_->MethodIsLeaf() && |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 339 | (static_cast<size_t>(frame_size_) < |
| 340 | Thread::kStackOverflowReservedBytes)); |
| 341 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 342 | NewLIR0(kPseudoMethodEntry); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 343 | |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 344 | const bool large_frame = (static_cast<size_t>(frame_size_) > Thread::kStackOverflowReservedUsableBytes); |
| 345 | const int spill_count = num_core_spills_ + num_fp_spills_; |
| 346 | const int spill_size = (spill_count * kArm64PointerSize + 15) & ~0xf; // SP 16 byte alignment. |
| 347 | const int frame_size_without_spills = frame_size_ - spill_size; |
| 348 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 349 | if (!skip_overflow_check) { |
| 350 | if (Runtime::Current()->ExplicitStackOverflowChecks()) { |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 351 | if (!large_frame) { |
| 352 | // Load stack limit |
| 353 | LoadWordDisp(rs_rA64_SELF, Thread::StackEndOffset<8>().Int32Value(), rs_x9); |
| 354 | } |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 355 | } else { |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 356 | // TODO(Arm64) Implement implicit checks. |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 357 | // Implicit stack overflow check. |
| 358 | // Generate a load from [sp, #-framesize]. If this is in the stack |
| 359 | // redzone we will get a segmentation fault. |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 360 | // Load32Disp(rs_rA64_SP, -Thread::kStackOverflowReservedBytes, rs_wzr); |
| 361 | // MarkPossibleStackOverflowException(); |
| 362 | LOG(FATAL) << "Implicit stack overflow checks not implemented."; |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 363 | } |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 364 | } |
| 365 | |
| 366 | if (frame_size_ > 0) { |
| 367 | OpRegImm64(kOpSub, rs_rA64_SP, spill_size); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 368 | } |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 369 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 370 | /* Need to spill any FP regs? */ |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 371 | if (fp_spill_mask_) { |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 372 | int spill_offset = spill_size - kArm64PointerSize*(num_fp_spills_ + num_core_spills_); |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 373 | SpillFPRegs(rs_rA64_SP, spill_offset, fp_spill_mask_); |
| 374 | } |
| 375 | |
| 376 | /* Spill core callee saves. */ |
| 377 | if (core_spill_mask_) { |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 378 | int spill_offset = spill_size - kArm64PointerSize*num_core_spills_; |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 379 | SpillCoreRegs(rs_rA64_SP, spill_offset, core_spill_mask_); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 382 | if (!skip_overflow_check) { |
| 383 | if (Runtime::Current()->ExplicitStackOverflowChecks()) { |
| 384 | class StackOverflowSlowPath: public LIRSlowPath { |
| 385 | public: |
| 386 | StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace) : |
| 387 | LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), |
| 388 | sp_displace_(sp_displace) { |
| 389 | } |
| 390 | void Compile() OVERRIDE { |
| 391 | m2l_->ResetRegPool(); |
| 392 | m2l_->ResetDefTracking(); |
| 393 | GenerateTargetLabel(kPseudoThrowTarget); |
| 394 | // Unwinds stack. |
| 395 | m2l_->OpRegImm(kOpAdd, rs_rA64_SP, sp_displace_); |
| 396 | m2l_->ClobberCallerSave(); |
| 397 | ThreadOffset<8> func_offset = QUICK_ENTRYPOINT_OFFSET(8, pThrowStackOverflow); |
| 398 | m2l_->LockTemp(rs_x8); |
| 399 | m2l_->LoadWordDisp(rs_rA64_SELF, func_offset.Int32Value(), rs_x8); |
| 400 | m2l_->NewLIR1(kA64Br1x, rs_x8.GetReg()); |
| 401 | m2l_->FreeTemp(rs_x8); |
| 402 | } |
| 403 | |
| 404 | private: |
| 405 | const size_t sp_displace_; |
| 406 | }; |
| 407 | |
| 408 | if (large_frame) { |
| 409 | // Compare Expected SP against bottom of stack. |
| 410 | // Branch to throw target if there is not enough room. |
| 411 | OpRegRegImm(kOpSub, rs_x9, rs_rA64_SP, frame_size_without_spills); |
| 412 | LoadWordDisp(rs_rA64_SELF, Thread::StackEndOffset<8>().Int32Value(), rs_x8); |
| 413 | LIR* branch = OpCmpBranch(kCondUlt, rs_rA64_SP, rs_x8, nullptr); |
| 414 | AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, spill_size)); |
| 415 | OpRegCopy(rs_rA64_SP, rs_x9); // Establish stack after checks. |
| 416 | } else { |
| 417 | /* |
| 418 | * If the frame is small enough we are guaranteed to have enough space that remains to |
| 419 | * handle signals on the user stack. |
| 420 | * Establishes stack before checks. |
| 421 | */ |
| 422 | OpRegRegImm(kOpSub, rs_rA64_SP, rs_rA64_SP, frame_size_without_spills); |
| 423 | LIR* branch = OpCmpBranch(kCondUlt, rs_rA64_SP, rs_x9, nullptr); |
| 424 | AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, frame_size_)); |
| 425 | } |
| 426 | } else { |
| 427 | OpRegImm(kOpSub, rs_rA64_SP, frame_size_without_spills); |
| 428 | } |
| 429 | } else { |
| 430 | OpRegImm(kOpSub, rs_rA64_SP, frame_size_without_spills); |
| 431 | } |
| 432 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 433 | FlushIns(ArgLocs, rl_method); |
| 434 | |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 435 | FreeTemp(rs_x0); |
| 436 | FreeTemp(rs_x1); |
| 437 | FreeTemp(rs_x2); |
| 438 | FreeTemp(rs_x3); |
Stuart Monteith | f8ec48e | 2014-06-06 17:05:08 +0100 | [diff] [blame^] | 439 | FreeTemp(rs_x4); |
| 440 | FreeTemp(rs_x5); |
| 441 | FreeTemp(rs_x6); |
| 442 | FreeTemp(rs_x7); |
| 443 | FreeTemp(rs_x8); |
| 444 | FreeTemp(rs_x9); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void Arm64Mir2Lir::GenExitSequence() { |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 448 | /* |
| 449 | * In the exit path, r0/r1 are live - make sure they aren't |
| 450 | * allocated by the register utilities as temps. |
| 451 | */ |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 452 | LockTemp(rs_x0); |
| 453 | LockTemp(rs_x1); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 454 | |
| 455 | NewLIR0(kPseudoMethodExit); |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 456 | |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 457 | /* Need to restore any FP callee saves? */ |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 458 | if (fp_spill_mask_) { |
| 459 | int spill_offset = frame_size_ - kArm64PointerSize*(num_fp_spills_ + num_core_spills_); |
| 460 | UnSpillFPRegs(rs_rA64_SP, spill_offset, fp_spill_mask_); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 461 | } |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 462 | if (core_spill_mask_) { |
Matteo Franchin | bc6d197 | 2014-05-13 12:33:28 +0100 | [diff] [blame] | 463 | int spill_offset = frame_size_ - kArm64PointerSize*num_core_spills_; |
| 464 | UnSpillCoreRegs(rs_rA64_SP, spill_offset, core_spill_mask_); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 465 | } |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 466 | |
Serban Constantinescu | ed65c5e | 2014-05-22 15:10:18 +0100 | [diff] [blame] | 467 | OpRegImm64(kOpAdd, rs_rA64_SP, frame_size_); |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 468 | NewLIR0(kA64Ret); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void Arm64Mir2Lir::GenSpecialExitSequence() { |
Matteo Franchin | e45fb9e | 2014-05-06 10:10:30 +0100 | [diff] [blame] | 472 | NewLIR0(kA64Ret); |
Matteo Franchin | 43ec873 | 2014-03-31 15:00:14 +0100 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | } // namespace art |