blob: b4eebb320e6378758355f1d72864166cd2a83840 [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
19#include "arm_lir.h"
20#include "codegen_arm.h"
21#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070022#include "gc/accounting/card_table.h"
Vladimir Markof4da6752014-08-01 19:04:18 +010023#include "mirror/art_method.h"
24#include "mirror/object_array-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070025#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026
27namespace art {
28
Brian Carlstrom7940e442013-07-12 13:46:57 -070029/*
30 * The sparse table in the literal pool is an array of <key,displacement>
31 * pairs. For each set, we'll load them as a pair using ldmia.
32 * This means that the register number of the temp we use for the key
33 * must be lower than the reg for the displacement.
34 *
35 * The test loop will look something like:
36 *
buzbee2700f7e2014-03-07 09:46:20 -080037 * adr r_base, <table>
Brian Carlstrom7940e442013-07-12 13:46:57 -070038 * ldr r_val, [rARM_SP, v_reg_off]
39 * mov r_idx, #table_size
40 * lp:
buzbee2700f7e2014-03-07 09:46:20 -080041 * ldmia r_base!, {r_key, r_disp}
Brian Carlstrom7940e442013-07-12 13:46:57 -070042 * sub r_idx, #1
43 * cmp r_val, r_key
44 * ifeq
45 * add rARM_PC, r_disp ; This is the branch from which we compute displacement
46 * cbnz r_idx, lp
47 */
Andreas Gampe48971b32014-08-06 10:09:01 -070048void ArmMir2Lir::GenLargeSparseSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070049 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070050 if (cu_->verbose) {
51 DumpSparseSwitchTable(table);
52 }
53 // Add the table to the list - we'll process it later
54 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000055 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 tab_rec->table = table;
57 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -070058 uint32_t size = table[1];
buzbee091cc402014-03-31 10:14:40 -070059 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010060 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070061
62 // Get the switch value
63 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080064 RegStorage r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070065 /* Allocate key and disp temps */
buzbee2700f7e2014-03-07 09:46:20 -080066 RegStorage r_key = AllocTemp();
67 RegStorage r_disp = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070068 // Make sure r_key's register number is less than r_disp's number for ldmia
buzbee2700f7e2014-03-07 09:46:20 -080069 if (r_key.GetReg() > r_disp.GetReg()) {
70 RegStorage tmp = r_disp;
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 r_disp = r_key;
72 r_key = tmp;
73 }
74 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -080075 NewLIR3(kThumb2Adr, r_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 // Set up r_idx
buzbee2700f7e2014-03-07 09:46:20 -080077 RegStorage r_idx = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 LoadConstant(r_idx, size);
79 // Establish loop branch target
80 LIR* target = NewLIR0(kPseudoTargetLabel);
81 // Load next key/disp
buzbee091cc402014-03-31 10:14:40 -070082 NewLIR2(kThumb2LdmiaWB, r_base.GetReg(), (1 << r_key.GetRegNum()) | (1 << r_disp.GetRegNum()));
buzbee2700f7e2014-03-07 09:46:20 -080083 OpRegReg(kOpCmp, r_key, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
Dave Allison3da67a52014-04-02 17:03:45 -070085 LIR* it = OpIT(kCondEq, "");
buzbee2700f7e2014-03-07 09:46:20 -080086 LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp.GetReg());
Dave Allison3da67a52014-04-02 17:03:45 -070087 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 tab_rec->anchor = switch_branch;
89 // Needs to use setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +000090 OpRegRegImm(kOpSub, r_idx, r_idx, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +010091 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 OpCondBranch(kCondNe, target);
93}
94
95
Andreas Gampe48971b32014-08-06 10:09:01 -070096void ArmMir2Lir::GenLargePackedSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070097 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070098 if (cu_->verbose) {
99 DumpPackedSwitchTable(table);
100 }
101 // Add the table to the list - we'll process it later
102 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000103 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104 tab_rec->table = table;
105 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -0700106 uint32_t size = table[1];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 tab_rec->targets =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000108 static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
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);
buzbee2700f7e2014-03-07 09:46:20 -0800166 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_r1, 0, NULL);
buzbee091cc402014-03-31 10:14:40 -0700167 NewLIR4(kThumb2Strex, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
168 mirror::Object::MonitorOffset().Int32Value() >> 2);
buzbee2700f7e2014-03-07 09:46:20 -0800169 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_r1, 0, NULL);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700170
171
172 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
173 not_unlocked_branch->target = slow_path_target;
174 if (null_check_branch != nullptr) {
175 null_check_branch->target = slow_path_target;
176 }
177 // TODO: move to a slow path.
178 // Go expensive route - artLockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700179 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000180 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800181 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700182 MarkSafepointPC(call_inst);
183
184 LIR* success_target = NewLIR0(kPseudoTargetLabel);
185 lock_success_branch->target = success_target;
Hans Boehm48f5c472014-06-27 14:50:10 -0700186 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700187 } else {
188 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800189 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700190 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700191 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
192 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700193 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800194 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700195 LIR* it = OpIT(kCondEq, "");
buzbee091cc402014-03-31 10:14:40 -0700196 NewLIR4(kThumb2Strex/*eq*/, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
197 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allison3da67a52014-04-02 17:03:45 -0700198 OpEndIT(it);
buzbee2700f7e2014-03-07 09:46:20 -0800199 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700200 it = OpIT(kCondNe, "T");
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700201 // Go expensive route - artLockObjectFromCode(self, obj);
buzbee091cc402014-03-31 10:14:40 -0700202 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(),
203 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000204 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800205 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700206 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700207 MarkSafepointPC(call_inst);
Hans Boehm48f5c472014-06-27 14:50:10 -0700208 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700209 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210}
211
212/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700213 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
214 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
215 * and can only give away ownership if its suspended.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700216 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700217void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800219 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 LockCallTemps(); // Prepare for explicit register usage
Dave Allisonf9439142014-03-27 15:10:22 -0700221 LIR* null_check_branch = nullptr;
buzbee695d13a2014-04-19 13:32:20 -0700222 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700223 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
224 if (kArchVariantHasGoodBranchPredictor) {
225 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
226 null_check_branch = nullptr; // No null check.
227 } else {
228 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000229 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700230 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
231 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700232 }
buzbee695d13a2014-04-19 13:32:20 -0700233 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1);
Dave Allisonf9439142014-03-27 15:10:22 -0700234 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800235 LoadConstantNoClobber(rs_r3, 0);
236 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_r1, rs_r2, NULL);
Hans Boehm48f5c472014-06-27 14:50:10 -0700237 GenMemBarrier(kAnyStore);
buzbee695d13a2014-04-19 13:32:20 -0700238 Store32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700239 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
240
241 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
242 slow_unlock_branch->target = slow_path_target;
243 if (null_check_branch != nullptr) {
244 null_check_branch->target = slow_path_target;
245 }
246 // TODO: move to a slow path.
247 // Go expensive route - artUnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700248 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000249 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800250 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700251 MarkSafepointPC(call_inst);
252
253 LIR* success_target = NewLIR0(kPseudoTargetLabel);
254 unlock_success_branch->target = success_target;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700255 } else {
256 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800257 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700258 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); // Get lock
Dave Allisonb373e092014-02-20 16:06:36 -0800259 MarkPossibleNullPointerException(opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700260 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee2700f7e2014-03-07 09:46:20 -0800261 LoadConstantNoClobber(rs_r3, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700262 // Is lock unheld on lock or held by us (==thread_id) on unlock?
buzbee2700f7e2014-03-07 09:46:20 -0800263 OpRegReg(kOpCmp, rs_r1, rs_r2);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700264
265 LIR* it = OpIT(kCondEq, "EE");
Hans Boehm48f5c472014-06-27 14:50:10 -0700266 if (GenMemBarrier(kAnyStore)) {
Andreas Gampeb14329f2014-05-15 11:16:06 -0700267 UpdateIT(it, "TEE");
268 }
buzbee695d13a2014-04-19 13:32:20 -0700269 Store32Disp/*eq*/(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700270 // Go expensive route - UnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700271 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -0800272 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000273 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800274 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700275 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700276 MarkSafepointPC(call_inst);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700277 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700278}
279
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700280void ArmMir2Lir::GenMoveException(RegLocation rl_dest) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700281 int ex_offset = Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700282 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
283 RegStorage reset_reg = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000284 LoadRefDisp(rs_rARM_SELF, ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285 LoadConstant(reset_reg, 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000286 StoreRefDisp(rs_rARM_SELF, ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 FreeTemp(reset_reg);
288 StoreValue(rl_dest, rl_result);
289}
290
291/*
292 * Mark garbage collection card. Skip if the value we're storing is null.
293 */
buzbee2700f7e2014-03-07 09:46:20 -0800294void ArmMir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) {
295 RegStorage reg_card_base = AllocTemp();
296 RegStorage reg_card_no = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700298 LoadWordDisp(rs_rARM_SELF, Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800300 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 LIR* target = NewLIR0(kPseudoTargetLabel);
302 branch_over->target = target;
303 FreeTemp(reg_card_base);
304 FreeTemp(reg_card_no);
305}
306
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700307void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308 int spill_count = num_core_spills_ + num_fp_spills_;
309 /*
310 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
311 * mechanism know so it doesn't try to use any of them when
312 * expanding the frame or flushing. This leaves the utility
313 * code with a single temp: r12. This should be enough.
314 */
buzbee091cc402014-03-31 10:14:40 -0700315 LockTemp(rs_r0);
316 LockTemp(rs_r1);
317 LockTemp(rs_r2);
318 LockTemp(rs_r3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700319
320 /*
321 * We can safely skip the stack overflow check if we're
322 * a leaf *and* our frame size < fudge factor.
323 */
Dave Allison648d7112014-07-25 16:15:27 -0700324 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, kArm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700325 NewLIR0(kPseudoMethodEntry);
Dave Allison648d7112014-07-25 16:15:27 -0700326 const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(kArm);
Andreas Gampe7cd26f32014-06-18 17:01:15 -0700327 bool large_frame = (static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes);
Dave Allison648d7112014-07-25 16:15:27 -0700328 bool generate_explicit_stack_overflow_check = large_frame ||
329 !cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700331 if (generate_explicit_stack_overflow_check) {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000332 if (!large_frame) {
333 /* Load stack limit */
334 LockTemp(rs_r12);
335 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
336 }
Dave Allison5cd33752014-04-15 15:57:58 -0700337 } else {
338 // Implicit stack overflow check.
339 // Generate a load from [sp, #-overflowsize]. If this is in the stack
340 // redzone we will get a segmentation fault.
341 //
342 // Caveat coder: if someone changes the kStackOverflowReservedBytes value
343 // we need to make sure that it's loadable in an immediate field of
344 // a sub instruction. Otherwise we will get a temp allocation and the
345 // code size will increase.
346 //
347 // This is done before the callee save instructions to avoid any possibility
348 // of these overflowing. This uses r12 and that's never saved in a callee
349 // save.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700350 OpRegRegImm(kOpSub, rs_r12, rs_rARM_SP, GetStackOverflowReservedBytes(kArm));
Dave Allison5cd33752014-04-15 15:57:58 -0700351 Load32Disp(rs_r12, 0, rs_r12);
352 MarkPossibleStackOverflowException();
Dave Allisonb373e092014-02-20 16:06:36 -0800353 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700354 }
355 /* Spill core callee saves */
356 NewLIR1(kThumb2Push, core_spill_mask_);
357 /* Need to spill any FP regs? */
358 if (num_fp_spills_) {
359 /*
360 * NOTE: fp spills are a little different from core spills in that
361 * they are pushed as a contiguous block. When promoting from
362 * the fp set, we must allocate all singles from s16..highest-promoted
363 */
364 NewLIR1(kThumb2VPushCS, num_fp_spills_);
365 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700366
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700367 const int spill_size = spill_count * 4;
368 const int frame_size_without_spills = frame_size_ - spill_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700370 if (generate_explicit_stack_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700371 class StackOverflowSlowPath : public LIRSlowPath {
372 public:
373 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, bool restore_lr, size_t sp_displace)
374 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), restore_lr_(restore_lr),
375 sp_displace_(sp_displace) {
376 }
377 void Compile() OVERRIDE {
378 m2l_->ResetRegPool();
379 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700380 GenerateTargetLabel(kPseudoThrowTarget);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700381 if (restore_lr_) {
buzbee2700f7e2014-03-07 09:46:20 -0800382 m2l_->LoadWordDisp(rs_rARM_SP, sp_displace_ - 4, rs_rARM_LR);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700383 }
buzbee2700f7e2014-03-07 09:46:20 -0800384 m2l_->OpRegImm(kOpAdd, rs_rARM_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700385 m2l_->ClobberCallerSave();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700386 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700387 // Load the entrypoint directly into the pc instead of doing a load + branch. Assumes
388 // codegen and target are in thumb2 mode.
buzbee695d13a2014-04-19 13:32:20 -0700389 // NOTE: native pointer.
buzbee2700f7e2014-03-07 09:46:20 -0800390 m2l_->LoadWordDisp(rs_rARM_SELF, func_offset.Int32Value(), rs_rARM_PC);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700391 }
392
393 private:
394 const bool restore_lr_;
395 const size_t sp_displace_;
396 };
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000397 if (large_frame) {
398 // Note: may need a temp reg, and we only have r12 free at this point.
buzbee2700f7e2014-03-07 09:46:20 -0800399 OpRegRegImm(kOpSub, rs_rARM_LR, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000400 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
buzbee2700f7e2014-03-07 09:46:20 -0800401 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_LR, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700402 // Need to restore LR since we used it as a temp.
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700403 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, true, spill_size));
buzbee2700f7e2014-03-07 09:46:20 -0800404 OpRegCopy(rs_rARM_SP, rs_rARM_LR); // Establish stack
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700405 } else {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000406 /*
407 * If the frame is small enough we are guaranteed to have enough space that remains to
408 * handle signals on the user stack. However, we may not have any free temp
409 * registers at this point, so we'll temporarily add LR to the temp pool.
410 */
411 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
412 MarkTemp(rs_rARM_LR);
413 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800414 OpRegRegImm(kOpSub, rs_rARM_SP, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000415 Clobber(rs_rARM_LR);
416 UnmarkTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800417 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_SP, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700418 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, false, frame_size_));
419 }
Dave Allisonb373e092014-02-20 16:06:36 -0800420 } else {
Dave Allison5cd33752014-04-15 15:57:58 -0700421 // Implicit stack overflow check has already been done. Just make room on the
422 // stack for the frame now.
Dave Allisonf9439142014-03-27 15:10:22 -0700423 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Dave Allisonb373e092014-02-20 16:06:36 -0800424 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700425 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800426 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 }
428
429 FlushIns(ArgLocs, rl_method);
430
buzbee091cc402014-03-31 10:14:40 -0700431 FreeTemp(rs_r0);
432 FreeTemp(rs_r1);
433 FreeTemp(rs_r2);
434 FreeTemp(rs_r3);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000435 FreeTemp(rs_r12);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700436}
437
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700438void ArmMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700439 int spill_count = num_core_spills_ + num_fp_spills_;
440 /*
441 * In the exit path, r0/r1 are live - make sure they aren't
442 * allocated by the register utilities as temps.
443 */
buzbee091cc402014-03-31 10:14:40 -0700444 LockTemp(rs_r0);
445 LockTemp(rs_r1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446
447 NewLIR0(kPseudoMethodExit);
buzbee2700f7e2014-03-07 09:46:20 -0800448 OpRegImm(kOpAdd, rs_rARM_SP, frame_size_ - (spill_count * 4));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700449 /* Need to restore any FP callee saves? */
450 if (num_fp_spills_) {
451 NewLIR1(kThumb2VPopCS, num_fp_spills_);
452 }
buzbee091cc402014-03-31 10:14:40 -0700453 if (core_spill_mask_ & (1 << rs_rARM_LR.GetRegNum())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 /* Unspill rARM_LR to rARM_PC */
buzbee091cc402014-03-31 10:14:40 -0700455 core_spill_mask_ &= ~(1 << rs_rARM_LR.GetRegNum());
456 core_spill_mask_ |= (1 << rs_rARM_PC.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 }
458 NewLIR1(kThumb2Pop, core_spill_mask_);
buzbee091cc402014-03-31 10:14:40 -0700459 if (!(core_spill_mask_ & (1 << rs_rARM_PC.GetRegNum()))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700460 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
buzbee091cc402014-03-31 10:14:40 -0700461 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700462 }
463}
464
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800465void ArmMir2Lir::GenSpecialExitSequence() {
buzbee091cc402014-03-31 10:14:40 -0700466 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800467}
468
Vladimir Markof4da6752014-08-01 19:04:18 +0100469static bool ArmUseRelativeCall(CompilationUnit* cu, const MethodReference& target_method) {
470 // Emit relative calls only within a dex file due to the limited range of the BL insn.
471 return cu->dex_file == target_method.dex_file;
472}
473
474/*
475 * Bit of a hack here - in the absence of a real scheduling pass,
476 * emit the next instruction in static & direct invoke sequences.
477 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700478static int ArmNextSDCallInsn(CompilationUnit* cu, CallInfo* info ATTRIBUTE_UNUSED,
Vladimir Markof4da6752014-08-01 19:04:18 +0100479 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700480 uint32_t unused_idx ATTRIBUTE_UNUSED,
Vladimir Markof4da6752014-08-01 19:04:18 +0100481 uintptr_t direct_code, uintptr_t direct_method,
482 InvokeType type) {
483 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
484 if (direct_code != 0 && direct_method != 0) {
485 switch (state) {
486 case 0: // Get the current Method* [sets kArg0]
487 if (direct_code != static_cast<uintptr_t>(-1)) {
488 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
489 } else if (ArmUseRelativeCall(cu, target_method)) {
490 // Defer to linker patch.
491 } else {
492 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
493 }
494 if (direct_method != static_cast<uintptr_t>(-1)) {
495 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
496 } else {
497 cg->LoadMethodAddress(target_method, type, kArg0);
498 }
499 break;
500 default:
501 return -1;
502 }
503 } else {
504 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
505 switch (state) {
506 case 0: // Get the current Method* [sets kArg0]
507 // TUNING: we can save a reg copy if Method* has been promoted.
508 cg->LoadCurrMethodDirect(arg0_ref);
509 break;
510 case 1: // Get method->dex_cache_resolved_methods_
511 cg->LoadRefDisp(arg0_ref,
512 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
513 arg0_ref,
514 kNotVolatile);
515 // Set up direct code if known.
516 if (direct_code != 0) {
517 if (direct_code != static_cast<uintptr_t>(-1)) {
518 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
519 } else if (ArmUseRelativeCall(cu, target_method)) {
520 // Defer to linker patch.
521 } else {
522 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
523 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
524 }
525 }
526 break;
527 case 2: // Grab target method*
528 CHECK_EQ(cu->dex_file, target_method.dex_file);
529 cg->LoadRefDisp(arg0_ref,
530 mirror::ObjectArray<mirror::Object>::OffsetOfElement(
531 target_method.dex_method_index).Int32Value(),
532 arg0_ref,
533 kNotVolatile);
534 break;
535 case 3: // Grab the code from the method*
536 if (direct_code == 0) {
537 // kInvokeTgt := arg0_ref->entrypoint
538 cg->LoadWordDisp(arg0_ref,
539 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset().Int32Value(),
540 cg->TargetPtrReg(kInvokeTgt));
541 }
542 break;
543 default:
544 return -1;
545 }
546 }
547 return state + 1;
548}
549
550NextCallInsn ArmMir2Lir::GetNextSDCallInsn() {
551 return ArmNextSDCallInsn;
552}
553
554LIR* ArmMir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
555 // For ARM, just generate a relative BL instruction that will be filled in at 'link time'.
556 // If the target turns out to be too far, the linker will generate a thunk for dispatch.
557 int target_method_idx = target_method.dex_method_index;
558 const DexFile* target_dex_file = target_method.dex_file;
559
560 // Generate the call instruction and save index, dex_file, and type.
561 // NOTE: Method deduplication takes linker patches into account, so we can just pass 0
562 // as a placeholder for the offset.
563 LIR* call = RawLIR(current_dalvik_offset_, kThumb2Bl, 0,
564 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
565 AppendLIR(call);
566 call_method_insns_.push_back(call);
567 return call;
568}
569
570LIR* ArmMir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info) {
571 LIR* call_insn;
572 if (method_info.FastPath() && ArmUseRelativeCall(cu_, method_info.GetTargetMethod()) &&
573 (method_info.GetSharpType() == kDirect || method_info.GetSharpType() == kStatic) &&
574 method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
575 call_insn = CallWithLinkerFixup(method_info.GetTargetMethod(), method_info.GetSharpType());
576 } else {
577 call_insn = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
578 }
579 return call_insn;
580}
581
Brian Carlstrom7940e442013-07-12 13:46:57 -0700582} // namespace art