blob: 3d18af6169e46968e2442992cf99e7560eb67455 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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
Brian Carlstrom7940e442013-07-12 13:46:57 -070019#include "codegen_arm.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020
21#include "arm_lir.h"
22#include "base/logging.h"
23#include "dex/mir_graph.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024#include "dex/quick/mir_to_lir-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080025#include "driver/compiler_driver.h"
Vladimir Marko20f85592015-03-19 10:07:02 +000026#include "driver/compiler_options.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070027#include "gc/accounting/card_table.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010028#include "mirror/art_method.h"
29#include "mirror/object_array-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070030#include "entrypoints/quick/quick_entrypoints.h"
Andreas Gampe7e499922015-01-06 08:28:12 -080031#include "utils.h"
Vladimir Markoe5c76c52015-04-06 12:10:19 +010032#include "utils/dex_cache_arrays_layout-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070033
34namespace art {
35
Brian Carlstrom7940e442013-07-12 13:46:57 -070036/*
37 * The sparse table in the literal pool is an array of <key,displacement>
38 * pairs. For each set, we'll load them as a pair using ldmia.
39 * This means that the register number of the temp we use for the key
40 * must be lower than the reg for the displacement.
41 *
42 * The test loop will look something like:
43 *
buzbee2700f7e2014-03-07 09:46:20 -080044 * adr r_base, <table>
Brian Carlstrom7940e442013-07-12 13:46:57 -070045 * ldr r_val, [rARM_SP, v_reg_off]
46 * mov r_idx, #table_size
47 * lp:
buzbee2700f7e2014-03-07 09:46:20 -080048 * ldmia r_base!, {r_key, r_disp}
Brian Carlstrom7940e442013-07-12 13:46:57 -070049 * sub r_idx, #1
50 * cmp r_val, r_key
51 * ifeq
52 * add rARM_PC, r_disp ; This is the branch from which we compute displacement
53 * cbnz r_idx, lp
54 */
Andreas Gampe48971b32014-08-06 10:09:01 -070055void ArmMir2Lir::GenLargeSparseSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070056 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 // Add the table to the list - we'll process it later
58 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000059 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -080060 tab_rec->switch_mir = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 tab_rec->table = table;
62 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -070063 uint32_t size = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +010064 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070065
66 // Get the switch value
67 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080068 RegStorage r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 /* Allocate key and disp temps */
buzbee2700f7e2014-03-07 09:46:20 -080070 RegStorage r_key = AllocTemp();
71 RegStorage r_disp = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 // Make sure r_key's register number is less than r_disp's number for ldmia
buzbee2700f7e2014-03-07 09:46:20 -080073 if (r_key.GetReg() > r_disp.GetReg()) {
74 RegStorage tmp = r_disp;
Brian Carlstrom7940e442013-07-12 13:46:57 -070075 r_disp = r_key;
76 r_key = tmp;
77 }
78 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -080079 NewLIR3(kThumb2Adr, r_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 // Set up r_idx
buzbee2700f7e2014-03-07 09:46:20 -080081 RegStorage r_idx = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 LoadConstant(r_idx, size);
83 // Establish loop branch target
84 LIR* target = NewLIR0(kPseudoTargetLabel);
85 // Load next key/disp
buzbee091cc402014-03-31 10:14:40 -070086 NewLIR2(kThumb2LdmiaWB, r_base.GetReg(), (1 << r_key.GetRegNum()) | (1 << r_disp.GetRegNum()));
buzbee2700f7e2014-03-07 09:46:20 -080087 OpRegReg(kOpCmp, r_key, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
Dave Allison3da67a52014-04-02 17:03:45 -070089 LIR* it = OpIT(kCondEq, "");
buzbee2700f7e2014-03-07 09:46:20 -080090 LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp.GetReg());
Dave Allison3da67a52014-04-02 17:03:45 -070091 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 tab_rec->anchor = switch_branch;
93 // Needs to use setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +000094 OpRegRegImm(kOpSub, r_idx, r_idx, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +010095 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 OpCondBranch(kCondNe, target);
97}
98
99
Andreas Gampe48971b32014-08-06 10:09:01 -0700100void ArmMir2Lir::GenLargePackedSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700101 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102 // Add the table to the list - we'll process it later
103 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000104 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800105 tab_rec->switch_mir = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 tab_rec->table = table;
107 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -0700108 uint32_t size = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100109 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110
111 // Get the switch value
112 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800113 RegStorage table_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800115 NewLIR3(kThumb2Adr, table_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 int low_key = s4FromSwitchData(&table[2]);
buzbee2700f7e2014-03-07 09:46:20 -0800117 RegStorage keyReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 // Remove the bias, if necessary
119 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800120 keyReg = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700121 } else {
122 keyReg = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800123 OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 }
125 // Bounds check - if < 0 or >= size continue following switch
126 OpRegImm(kOpCmp, keyReg, size-1);
127 LIR* branch_over = OpCondBranch(kCondHi, NULL);
128
129 // Load the displacement from the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800130 RegStorage disp_reg = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700131 LoadBaseIndexed(table_base, keyReg, disp_reg, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132
133 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
buzbee2700f7e2014-03-07 09:46:20 -0800134 LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 tab_rec->anchor = switch_branch;
136
137 /* branch_over target here */
138 LIR* target = NewLIR0(kPseudoTargetLabel);
139 branch_over->target = target;
140}
141
142/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700143 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
144 * details see monitor.cc.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700145 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700146void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 FlushAllRegs();
buzbee695d13a2014-04-19 13:32:20 -0700148 // FIXME: need separate LoadValues for object references.
buzbee2700f7e2014-03-07 09:46:20 -0800149 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700151 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
152 if (kArchVariantHasGoodBranchPredictor) {
Dave Allisonf9439142014-03-27 15:10:22 -0700153 LIR* null_check_branch = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700154 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
155 null_check_branch = nullptr; // No null check.
156 } else {
157 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000158 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700159 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
160 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700161 }
buzbee695d13a2014-04-19 13:32:20 -0700162 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700163 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
164 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700165 MarkPossibleNullPointerException(opt_flags);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800166 // Zero out the read barrier bits.
167 OpRegRegImm(kOpAnd, rs_r3, rs_r1, LockWord::kReadBarrierStateMaskShiftedToggled);
168 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_r3, 0, NULL);
169 // r1 is zero except for the rb bits here. Copy the read barrier bits into r2.
170 OpRegRegReg(kOpOr, rs_r2, rs_r2, rs_r1);
buzbee091cc402014-03-31 10:14:40 -0700171 NewLIR4(kThumb2Strex, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
172 mirror::Object::MonitorOffset().Int32Value() >> 2);
buzbee2700f7e2014-03-07 09:46:20 -0800173 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_r1, 0, NULL);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700174
175
176 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
177 not_unlocked_branch->target = slow_path_target;
178 if (null_check_branch != nullptr) {
179 null_check_branch->target = slow_path_target;
180 }
181 // TODO: move to a slow path.
182 // Go expensive route - artLockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700183 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000184 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800185 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700186 MarkSafepointPC(call_inst);
187
188 LIR* success_target = NewLIR0(kPseudoTargetLabel);
189 lock_success_branch->target = success_target;
Hans Boehm48f5c472014-06-27 14:50:10 -0700190 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700191 } else {
192 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800193 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700194 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700195 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
196 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700197 MarkPossibleNullPointerException(opt_flags);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800198 // Zero out the read barrier bits.
199 OpRegRegImm(kOpAnd, rs_r3, rs_r1, LockWord::kReadBarrierStateMaskShiftedToggled);
200 // r1 will be zero except for the rb bits if the following
201 // cmp-and-branch branches to eq where r2 will be used. Copy the
202 // read barrier bits into r2.
203 OpRegRegReg(kOpOr, rs_r2, rs_r2, rs_r1);
204 OpRegImm(kOpCmp, rs_r3, 0);
205
Dave Allison3da67a52014-04-02 17:03:45 -0700206 LIR* it = OpIT(kCondEq, "");
buzbee091cc402014-03-31 10:14:40 -0700207 NewLIR4(kThumb2Strex/*eq*/, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
208 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allison3da67a52014-04-02 17:03:45 -0700209 OpEndIT(it);
buzbee2700f7e2014-03-07 09:46:20 -0800210 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700211 it = OpIT(kCondNe, "T");
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700212 // Go expensive route - artLockObjectFromCode(self, obj);
buzbee091cc402014-03-31 10:14:40 -0700213 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(),
214 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000215 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800216 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700217 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700218 MarkSafepointPC(call_inst);
Hans Boehm48f5c472014-06-27 14:50:10 -0700219 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700220 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221}
222
223/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700224 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
225 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
226 * and can only give away ownership if its suspended.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700227 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700228void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700229 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800230 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 LockCallTemps(); // Prepare for explicit register usage
Dave Allisonf9439142014-03-27 15:10:22 -0700232 LIR* null_check_branch = nullptr;
buzbee695d13a2014-04-19 13:32:20 -0700233 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700234 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
235 if (kArchVariantHasGoodBranchPredictor) {
236 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
237 null_check_branch = nullptr; // No null check.
238 } else {
239 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000240 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700241 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
242 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700243 }
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800244 if (!kUseReadBarrier) {
245 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); // Get lock
246 } else {
247 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
248 mirror::Object::MonitorOffset().Int32Value() >> 2);
249 }
Dave Allisonf9439142014-03-27 15:10:22 -0700250 MarkPossibleNullPointerException(opt_flags);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800251 // Zero out the read barrier bits.
252 OpRegRegImm(kOpAnd, rs_r3, rs_r1, LockWord::kReadBarrierStateMaskShiftedToggled);
253 // Zero out except the read barrier bits.
254 OpRegRegImm(kOpAnd, rs_r1, rs_r1, LockWord::kReadBarrierStateMaskShifted);
255 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_r3, rs_r2, NULL);
Hans Boehm48f5c472014-06-27 14:50:10 -0700256 GenMemBarrier(kAnyStore);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800257 LIR* unlock_success_branch;
258 if (!kUseReadBarrier) {
259 Store32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1);
260 unlock_success_branch = OpUnconditionalBranch(NULL);
261 } else {
262 NewLIR4(kThumb2Strex, rs_r2.GetReg(), rs_r1.GetReg(), rs_r0.GetReg(),
263 mirror::Object::MonitorOffset().Int32Value() >> 2);
264 unlock_success_branch = OpCmpImmBranch(kCondEq, rs_r2, 0, NULL);
265 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700266 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
267 slow_unlock_branch->target = slow_path_target;
268 if (null_check_branch != nullptr) {
269 null_check_branch->target = slow_path_target;
270 }
271 // TODO: move to a slow path.
272 // Go expensive route - artUnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700273 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000274 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800275 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700276 MarkSafepointPC(call_inst);
277
278 LIR* success_target = NewLIR0(kPseudoTargetLabel);
279 unlock_success_branch->target = success_target;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700280 } else {
281 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800282 GenNullCheck(rs_r0, opt_flags);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800283 if (!kUseReadBarrier) {
284 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); // Get lock
285 } else {
286 // If we use read barriers, we need to use atomic instructions.
287 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
288 mirror::Object::MonitorOffset().Int32Value() >> 2);
289 }
Dave Allisonb373e092014-02-20 16:06:36 -0800290 MarkPossibleNullPointerException(opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700291 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800292 // Zero out the read barrier bits.
293 OpRegRegImm(kOpAnd, rs_r3, rs_r1, LockWord::kReadBarrierStateMaskShiftedToggled);
294 // Zero out except the read barrier bits.
295 OpRegRegImm(kOpAnd, rs_r1, rs_r1, LockWord::kReadBarrierStateMaskShifted);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700296 // Is lock unheld on lock or held by us (==thread_id) on unlock?
Hiroshi Yamauchie15ea082015-02-09 17:11:42 -0800297 OpRegReg(kOpCmp, rs_r3, rs_r2);
298 if (!kUseReadBarrier) {
299 LIR* it = OpIT(kCondEq, "EE");
300 if (GenMemBarrier(kAnyStore)) {
301 UpdateIT(it, "TEE");
302 }
303 Store32Disp/*eq*/(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1);
304 // Go expensive route - UnlockObjectFromCode(obj);
305 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(),
306 rs_rARM_LR);
307 ClobberCallerSave();
308 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
309 OpEndIT(it);
310 MarkSafepointPC(call_inst);
311 } else {
312 // If we use read barriers, we need to use atomic instructions.
313 LIR* it = OpIT(kCondEq, "");
314 if (GenMemBarrier(kAnyStore)) {
315 UpdateIT(it, "T");
316 }
317 NewLIR4/*eq*/(kThumb2Strex, rs_r2.GetReg(), rs_r1.GetReg(), rs_r0.GetReg(),
318 mirror::Object::MonitorOffset().Int32Value() >> 2);
319 OpEndIT(it);
320 // Since we know r2 wasn't zero before the above it instruction,
321 // if r2 is zero here, we know r3 was equal to r2 and the strex
322 // suceeded (we're done). Otherwise (either r3 wasn't equal to r2
323 // or the strex failed), call the entrypoint.
324 OpRegImm(kOpCmp, rs_r2, 0);
325 LIR* it2 = OpIT(kCondNe, "T");
326 // Go expensive route - UnlockObjectFromCode(obj);
327 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(),
328 rs_rARM_LR);
329 ClobberCallerSave();
330 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
331 OpEndIT(it2);
332 MarkSafepointPC(call_inst);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700333 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700334 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335}
336
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700337void ArmMir2Lir::GenMoveException(RegLocation rl_dest) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700338 int ex_offset = Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700339 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
340 RegStorage reset_reg = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000341 LoadRefDisp(rs_rARM_SELF, ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 LoadConstant(reset_reg, 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000343 StoreRefDisp(rs_rARM_SELF, ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700344 FreeTemp(reset_reg);
345 StoreValue(rl_dest, rl_result);
346}
347
Vladimir Markobf535be2014-11-19 18:52:35 +0000348void ArmMir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800349 RegStorage reg_card_base = AllocTemp();
350 RegStorage reg_card_no = AllocTemp();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700351 LoadWordDisp(rs_rARM_SELF, Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800353 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 FreeTemp(reg_card_base);
355 FreeTemp(reg_card_no);
356}
357
David Srbecky1109fb32015-04-07 20:21:06 +0100358static dwarf::Reg DwarfCoreReg(int num) {
359 return dwarf::Reg::ArmCore(num);
360}
361
362static dwarf::Reg DwarfFpReg(int num) {
363 return dwarf::Reg::ArmFp(num);
364}
365
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700366void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
David Srbecky1109fb32015-04-07 20:21:06 +0100367 DCHECK_EQ(cfi_.GetCurrentCFAOffset(), 0); // empty stack.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 int spill_count = num_core_spills_ + num_fp_spills_;
369 /*
370 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
371 * mechanism know so it doesn't try to use any of them when
372 * expanding the frame or flushing. This leaves the utility
373 * code with a single temp: r12. This should be enough.
374 */
buzbee091cc402014-03-31 10:14:40 -0700375 LockTemp(rs_r0);
376 LockTemp(rs_r1);
377 LockTemp(rs_r2);
378 LockTemp(rs_r3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379
380 /*
381 * We can safely skip the stack overflow check if we're
382 * a leaf *and* our frame size < fudge factor.
383 */
Dave Allison648d7112014-07-25 16:15:27 -0700384 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, kArm);
Dave Allison648d7112014-07-25 16:15:27 -0700385 const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(kArm);
Andreas Gampe7cd26f32014-06-18 17:01:15 -0700386 bool large_frame = (static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes);
Dave Allison648d7112014-07-25 16:15:27 -0700387 bool generate_explicit_stack_overflow_check = large_frame ||
388 !cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700389 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700390 if (generate_explicit_stack_overflow_check) {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000391 if (!large_frame) {
392 /* Load stack limit */
393 LockTemp(rs_r12);
394 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
395 }
Dave Allison5cd33752014-04-15 15:57:58 -0700396 } else {
397 // Implicit stack overflow check.
398 // Generate a load from [sp, #-overflowsize]. If this is in the stack
399 // redzone we will get a segmentation fault.
400 //
401 // Caveat coder: if someone changes the kStackOverflowReservedBytes value
402 // we need to make sure that it's loadable in an immediate field of
403 // a sub instruction. Otherwise we will get a temp allocation and the
404 // code size will increase.
405 //
406 // This is done before the callee save instructions to avoid any possibility
407 // of these overflowing. This uses r12 and that's never saved in a callee
408 // save.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700409 OpRegRegImm(kOpSub, rs_r12, rs_rARM_SP, GetStackOverflowReservedBytes(kArm));
Dave Allison5cd33752014-04-15 15:57:58 -0700410 Load32Disp(rs_r12, 0, rs_r12);
411 MarkPossibleStackOverflowException();
Dave Allisonb373e092014-02-20 16:06:36 -0800412 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700413 }
414 /* Spill core callee saves */
David Srbecky1109fb32015-04-07 20:21:06 +0100415 if (core_spill_mask_ != 0u) {
416 if ((core_spill_mask_ & ~(0xffu | (1u << rs_rARM_LR.GetRegNum()))) == 0u) {
417 // Spilling only low regs and/or LR, use 16-bit PUSH.
418 constexpr int lr_bit_shift = rs_rARM_LR.GetRegNum() - 8;
419 NewLIR1(kThumbPush,
420 (core_spill_mask_ & ~(1u << rs_rARM_LR.GetRegNum())) |
421 ((core_spill_mask_ & (1u << rs_rARM_LR.GetRegNum())) >> lr_bit_shift));
422 } else if (IsPowerOfTwo(core_spill_mask_)) {
423 // kThumb2Push cannot be used to spill a single register.
424 NewLIR1(kThumb2Push1, CTZ(core_spill_mask_));
425 } else {
426 NewLIR1(kThumb2Push, core_spill_mask_);
427 }
428 cfi_.AdjustCFAOffset(num_core_spills_ * kArmPointerSize);
429 cfi_.RelOffsetForMany(DwarfCoreReg(0), 0, core_spill_mask_, kArmPointerSize);
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000430 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700431 /* Need to spill any FP regs? */
David Srbecky1109fb32015-04-07 20:21:06 +0100432 if (num_fp_spills_ != 0u) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700433 /*
434 * NOTE: fp spills are a little different from core spills in that
435 * they are pushed as a contiguous block. When promoting from
436 * the fp set, we must allocate all singles from s16..highest-promoted
437 */
438 NewLIR1(kThumb2VPushCS, num_fp_spills_);
David Srbecky1109fb32015-04-07 20:21:06 +0100439 cfi_.AdjustCFAOffset(num_fp_spills_ * kArmPointerSize);
440 cfi_.RelOffsetForMany(DwarfFpReg(0), 0, fp_spill_mask_, kArmPointerSize);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700441 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700442
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700443 const int spill_size = spill_count * 4;
444 const int frame_size_without_spills = frame_size_ - spill_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700446 if (generate_explicit_stack_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700447 class StackOverflowSlowPath : public LIRSlowPath {
448 public:
449 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, bool restore_lr, size_t sp_displace)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000450 : LIRSlowPath(m2l, branch), restore_lr_(restore_lr),
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700451 sp_displace_(sp_displace) {
452 }
453 void Compile() OVERRIDE {
454 m2l_->ResetRegPool();
455 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700456 GenerateTargetLabel(kPseudoThrowTarget);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700457 if (restore_lr_) {
buzbee2700f7e2014-03-07 09:46:20 -0800458 m2l_->LoadWordDisp(rs_rARM_SP, sp_displace_ - 4, rs_rARM_LR);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700459 }
buzbee2700f7e2014-03-07 09:46:20 -0800460 m2l_->OpRegImm(kOpAdd, rs_rARM_SP, sp_displace_);
David Srbecky1109fb32015-04-07 20:21:06 +0100461 m2l_->cfi().AdjustCFAOffset(-sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700462 m2l_->ClobberCallerSave();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700463 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700464 // Load the entrypoint directly into the pc instead of doing a load + branch. Assumes
465 // codegen and target are in thumb2 mode.
buzbee695d13a2014-04-19 13:32:20 -0700466 // NOTE: native pointer.
buzbee2700f7e2014-03-07 09:46:20 -0800467 m2l_->LoadWordDisp(rs_rARM_SELF, func_offset.Int32Value(), rs_rARM_PC);
David Srbecky1109fb32015-04-07 20:21:06 +0100468 m2l_->cfi().AdjustCFAOffset(sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700469 }
470
471 private:
472 const bool restore_lr_;
473 const size_t sp_displace_;
474 };
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000475 if (large_frame) {
476 // Note: may need a temp reg, and we only have r12 free at this point.
buzbee2700f7e2014-03-07 09:46:20 -0800477 OpRegRegImm(kOpSub, rs_rARM_LR, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000478 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
buzbee2700f7e2014-03-07 09:46:20 -0800479 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_LR, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700480 // Need to restore LR since we used it as a temp.
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700481 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, true, spill_size));
buzbee2700f7e2014-03-07 09:46:20 -0800482 OpRegCopy(rs_rARM_SP, rs_rARM_LR); // Establish stack
David Srbecky1109fb32015-04-07 20:21:06 +0100483 cfi_.AdjustCFAOffset(frame_size_without_spills);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700484 } else {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000485 /*
486 * If the frame is small enough we are guaranteed to have enough space that remains to
487 * handle signals on the user stack. However, we may not have any free temp
488 * registers at this point, so we'll temporarily add LR to the temp pool.
489 */
490 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
491 MarkTemp(rs_rARM_LR);
492 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800493 OpRegRegImm(kOpSub, rs_rARM_SP, rs_rARM_SP, frame_size_without_spills);
David Srbecky1109fb32015-04-07 20:21:06 +0100494 cfi_.AdjustCFAOffset(frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000495 Clobber(rs_rARM_LR);
496 UnmarkTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800497 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_SP, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700498 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, false, frame_size_));
499 }
Dave Allisonb373e092014-02-20 16:06:36 -0800500 } else {
Dave Allison5cd33752014-04-15 15:57:58 -0700501 // Implicit stack overflow check has already been done. Just make room on the
502 // stack for the frame now.
Dave Allisonf9439142014-03-27 15:10:22 -0700503 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
David Srbecky1109fb32015-04-07 20:21:06 +0100504 cfi_.AdjustCFAOffset(frame_size_without_spills);
Dave Allisonb373e092014-02-20 16:06:36 -0800505 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800507 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
David Srbecky1109fb32015-04-07 20:21:06 +0100508 cfi_.AdjustCFAOffset(frame_size_without_spills);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 }
510
511 FlushIns(ArgLocs, rl_method);
512
Vladimir Markocc234812015-04-07 09:36:09 +0100513 // We can promote a PC-relative reference to dex cache arrays to a register
514 // if it's used at least twice. Without investigating where we should lazily
515 // load the reference, we conveniently load it after flushing inputs.
516 if (dex_cache_arrays_base_reg_.Valid()) {
517 OpPcRelDexCacheArrayAddr(cu_->dex_file, dex_cache_arrays_min_offset_,
518 dex_cache_arrays_base_reg_);
519 }
520
buzbee091cc402014-03-31 10:14:40 -0700521 FreeTemp(rs_r0);
522 FreeTemp(rs_r1);
523 FreeTemp(rs_r2);
524 FreeTemp(rs_r3);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000525 FreeTemp(rs_r12);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526}
527
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700528void ArmMir2Lir::GenExitSequence() {
David Srbecky1109fb32015-04-07 20:21:06 +0100529 cfi_.RememberState();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700530 int spill_count = num_core_spills_ + num_fp_spills_;
David Srbecky1109fb32015-04-07 20:21:06 +0100531
Brian Carlstrom7940e442013-07-12 13:46:57 -0700532 /*
533 * In the exit path, r0/r1 are live - make sure they aren't
534 * allocated by the register utilities as temps.
535 */
buzbee091cc402014-03-31 10:14:40 -0700536 LockTemp(rs_r0);
537 LockTemp(rs_r1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700538
David Srbecky1109fb32015-04-07 20:21:06 +0100539 int adjust = frame_size_ - (spill_count * kArmPointerSize);
540 OpRegImm(kOpAdd, rs_rARM_SP, adjust);
541 cfi_.AdjustCFAOffset(-adjust);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700542 /* Need to restore any FP callee saves? */
543 if (num_fp_spills_) {
544 NewLIR1(kThumb2VPopCS, num_fp_spills_);
David Srbecky1109fb32015-04-07 20:21:06 +0100545 cfi_.AdjustCFAOffset(-num_fp_spills_ * kArmPointerSize);
546 cfi_.RestoreMany(DwarfFpReg(0), fp_spill_mask_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700547 }
David Srbecky1109fb32015-04-07 20:21:06 +0100548 bool unspill_LR_to_PC = (core_spill_mask_ & (1 << rs_rARM_LR.GetRegNum())) != 0;
549 if (unspill_LR_to_PC) {
buzbee091cc402014-03-31 10:14:40 -0700550 core_spill_mask_ &= ~(1 << rs_rARM_LR.GetRegNum());
551 core_spill_mask_ |= (1 << rs_rARM_PC.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700552 }
David Srbecky1109fb32015-04-07 20:21:06 +0100553 if (core_spill_mask_ != 0u) {
554 if ((core_spill_mask_ & ~(0xffu | (1u << rs_rARM_PC.GetRegNum()))) == 0u) {
555 // Unspilling only low regs and/or PC, use 16-bit POP.
556 constexpr int pc_bit_shift = rs_rARM_PC.GetRegNum() - 8;
557 NewLIR1(kThumbPop,
558 (core_spill_mask_ & ~(1u << rs_rARM_PC.GetRegNum())) |
559 ((core_spill_mask_ & (1u << rs_rARM_PC.GetRegNum())) >> pc_bit_shift));
560 } else if (IsPowerOfTwo(core_spill_mask_)) {
561 // kThumb2Pop cannot be used to unspill a single register.
562 NewLIR1(kThumb2Pop1, CTZ(core_spill_mask_));
563 } else {
564 NewLIR1(kThumb2Pop, core_spill_mask_);
565 }
566 // If we pop to PC, there is no further epilogue code.
567 if (!unspill_LR_to_PC) {
568 cfi_.AdjustCFAOffset(-num_core_spills_ * kArmPointerSize);
569 cfi_.RestoreMany(DwarfCoreReg(0), core_spill_mask_);
570 DCHECK_EQ(cfi_.GetCurrentCFAOffset(), 0); // empty stack.
571 }
Vladimir Marko9d5c25a2014-11-26 15:42:32 +0000572 }
David Srbecky1109fb32015-04-07 20:21:06 +0100573 if (!unspill_LR_to_PC) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700574 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
buzbee091cc402014-03-31 10:14:40 -0700575 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700576 }
David Srbecky1109fb32015-04-07 20:21:06 +0100577 // The CFI should be restored for any code that follows the exit block.
578 cfi_.RestoreState();
579 cfi_.DefCFAOffset(frame_size_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700580}
581
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800582void ArmMir2Lir::GenSpecialExitSequence() {
buzbee091cc402014-03-31 10:14:40 -0700583 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800584}
585
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000586void ArmMir2Lir::GenSpecialEntryForSuspend() {
587 // Keep 16-byte stack alignment - push r0, i.e. ArtMethod*, r5, r6, lr.
588 DCHECK(!IsTemp(rs_r5));
589 DCHECK(!IsTemp(rs_r6));
590 core_spill_mask_ =
591 (1u << rs_r5.GetRegNum()) | (1u << rs_r6.GetRegNum()) | (1u << rs_rARM_LR.GetRegNum());
592 num_core_spills_ = 3u;
593 fp_spill_mask_ = 0u;
594 num_fp_spills_ = 0u;
595 frame_size_ = 16u;
596 core_vmap_table_.clear();
597 fp_vmap_table_.clear();
598 NewLIR1(kThumbPush, (1u << rs_r0.GetRegNum()) | // ArtMethod*
599 (core_spill_mask_ & ~(1u << rs_rARM_LR.GetRegNum())) | // Spills other than LR.
600 (1u << 8)); // LR encoded for 16-bit push.
David Srbecky1109fb32015-04-07 20:21:06 +0100601 cfi_.AdjustCFAOffset(frame_size_);
602 // Do not generate CFI for scratch register r0.
603 cfi_.RelOffsetForMany(DwarfCoreReg(0), 4, core_spill_mask_, kArmPointerSize);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000604}
605
606void ArmMir2Lir::GenSpecialExitForSuspend() {
607 // Pop the frame. (ArtMethod* no longer needed but restore it anyway.)
608 NewLIR1(kThumb2Pop, (1u << rs_r0.GetRegNum()) | core_spill_mask_); // 32-bit because of LR.
David Srbecky1109fb32015-04-07 20:21:06 +0100609 cfi_.AdjustCFAOffset(-frame_size_);
610 cfi_.RestoreMany(DwarfCoreReg(0), core_spill_mask_);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000611}
612
Vladimir Markof4da6752014-08-01 19:04:18 +0100613static bool ArmUseRelativeCall(CompilationUnit* cu, const MethodReference& target_method) {
614 // Emit relative calls only within a dex file due to the limited range of the BL insn.
615 return cu->dex_file == target_method.dex_file;
616}
617
618/*
619 * Bit of a hack here - in the absence of a real scheduling pass,
620 * emit the next instruction in static & direct invoke sequences.
621 */
Vladimir Markoe5c76c52015-04-06 12:10:19 +0100622int ArmMir2Lir::ArmNextSDCallInsn(CompilationUnit* cu, CallInfo* info ATTRIBUTE_UNUSED,
623 int state, const MethodReference& target_method,
624 uint32_t unused_idx ATTRIBUTE_UNUSED,
625 uintptr_t direct_code, uintptr_t direct_method,
626 InvokeType type) {
627 ArmMir2Lir* cg = static_cast<ArmMir2Lir*>(cu->cg.get());
Vladimir Markof4da6752014-08-01 19:04:18 +0100628 if (direct_code != 0 && direct_method != 0) {
629 switch (state) {
630 case 0: // Get the current Method* [sets kArg0]
631 if (direct_code != static_cast<uintptr_t>(-1)) {
632 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
633 } else if (ArmUseRelativeCall(cu, target_method)) {
634 // Defer to linker patch.
635 } else {
636 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
637 }
638 if (direct_method != static_cast<uintptr_t>(-1)) {
639 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
640 } else {
641 cg->LoadMethodAddress(target_method, type, kArg0);
642 }
643 break;
644 default:
645 return -1;
646 }
647 } else {
Vladimir Markoe5c76c52015-04-06 12:10:19 +0100648 bool use_pc_rel = cg->CanUseOpPcRelDexCacheArrayLoad();
Vladimir Markof4da6752014-08-01 19:04:18 +0100649 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
650 switch (state) {
651 case 0: // Get the current Method* [sets kArg0]
652 // TUNING: we can save a reg copy if Method* has been promoted.
Vladimir Markoe5c76c52015-04-06 12:10:19 +0100653 if (!use_pc_rel) {
654 cg->LoadCurrMethodDirect(arg0_ref);
655 break;
656 }
657 ++state;
658 FALLTHROUGH_INTENDED;
Vladimir Markof4da6752014-08-01 19:04:18 +0100659 case 1: // Get method->dex_cache_resolved_methods_
Vladimir Markoe5c76c52015-04-06 12:10:19 +0100660 if (!use_pc_rel) {
661 cg->LoadRefDisp(arg0_ref,
662 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
663 arg0_ref,
664 kNotVolatile);
665 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100666 // Set up direct code if known.
667 if (direct_code != 0) {
668 if (direct_code != static_cast<uintptr_t>(-1)) {
669 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
670 } else if (ArmUseRelativeCall(cu, target_method)) {
671 // Defer to linker patch.
672 } else {
673 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
674 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
675 }
676 }
Vladimir Markoe5c76c52015-04-06 12:10:19 +0100677 if (!use_pc_rel || direct_code != 0) {
678 break;
679 }
680 ++state;
681 FALLTHROUGH_INTENDED;
Vladimir Markof4da6752014-08-01 19:04:18 +0100682 case 2: // Grab target method*
683 CHECK_EQ(cu->dex_file, target_method.dex_file);
Vladimir Markoe5c76c52015-04-06 12:10:19 +0100684 if (!use_pc_rel) {
685 cg->LoadRefDisp(arg0_ref,
686 mirror::ObjectArray<mirror::Object>::OffsetOfElement(
687 target_method.dex_method_index).Int32Value(),
688 arg0_ref,
689 kNotVolatile);
690 } else {
691 size_t offset = cg->dex_cache_arrays_layout_.MethodOffset(target_method.dex_method_index);
692 cg->OpPcRelDexCacheArrayLoad(cu->dex_file, offset, arg0_ref);
693 }
Vladimir Markof4da6752014-08-01 19:04:18 +0100694 break;
695 case 3: // Grab the code from the method*
696 if (direct_code == 0) {
697 // kInvokeTgt := arg0_ref->entrypoint
698 cg->LoadWordDisp(arg0_ref,
Mathieu Chartier2d721012014-11-10 11:08:06 -0800699 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
700 kArmPointerSize).Int32Value(), cg->TargetPtrReg(kInvokeTgt));
Vladimir Markof4da6752014-08-01 19:04:18 +0100701 }
702 break;
703 default:
704 return -1;
705 }
706 }
707 return state + 1;
708}
709
710NextCallInsn ArmMir2Lir::GetNextSDCallInsn() {
711 return ArmNextSDCallInsn;
712}
713
714LIR* ArmMir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
715 // For ARM, just generate a relative BL instruction that will be filled in at 'link time'.
716 // If the target turns out to be too far, the linker will generate a thunk for dispatch.
717 int target_method_idx = target_method.dex_method_index;
718 const DexFile* target_dex_file = target_method.dex_file;
719
720 // Generate the call instruction and save index, dex_file, and type.
721 // NOTE: Method deduplication takes linker patches into account, so we can just pass 0
722 // as a placeholder for the offset.
723 LIR* call = RawLIR(current_dalvik_offset_, kThumb2Bl, 0,
Vladimir Markof6737f72015-03-23 17:05:14 +0000724 target_method_idx, WrapPointer(target_dex_file), type);
Vladimir Markof4da6752014-08-01 19:04:18 +0100725 AppendLIR(call);
726 call_method_insns_.push_back(call);
727 return call;
728}
729
730LIR* ArmMir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info) {
731 LIR* call_insn;
732 if (method_info.FastPath() && ArmUseRelativeCall(cu_, method_info.GetTargetMethod()) &&
733 (method_info.GetSharpType() == kDirect || method_info.GetSharpType() == kStatic) &&
734 method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
735 call_insn = CallWithLinkerFixup(method_info.GetTargetMethod(), method_info.GetSharpType());
736 } else {
737 call_insn = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
738 }
739 return call_insn;
740}
741
Brian Carlstrom7940e442013-07-12 13:46:57 -0700742} // namespace art