blob: 9cb56cfb6396d86bc88e982a984d560d8f7394fc [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 Rogers166db042013-07-26 12:05:57 -070022#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023
24namespace art {
25
Brian Carlstrom7940e442013-07-12 13:46:57 -070026/*
27 * The sparse table in the literal pool is an array of <key,displacement>
28 * pairs. For each set, we'll load them as a pair using ldmia.
29 * This means that the register number of the temp we use for the key
30 * must be lower than the reg for the displacement.
31 *
32 * The test loop will look something like:
33 *
buzbee2700f7e2014-03-07 09:46:20 -080034 * adr r_base, <table>
Brian Carlstrom7940e442013-07-12 13:46:57 -070035 * ldr r_val, [rARM_SP, v_reg_off]
36 * mov r_idx, #table_size
37 * lp:
buzbee2700f7e2014-03-07 09:46:20 -080038 * ldmia r_base!, {r_key, r_disp}
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 * sub r_idx, #1
40 * cmp r_val, r_key
41 * ifeq
42 * add rARM_PC, r_disp ; This is the branch from which we compute displacement
43 * cbnz r_idx, lp
44 */
45void ArmMir2Lir::GenSparseSwitch(MIR* mir, uint32_t table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070046 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
48 if (cu_->verbose) {
49 DumpSparseSwitchTable(table);
50 }
51 // Add the table to the list - we'll process it later
52 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000053 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -070054 tab_rec->table = table;
55 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -070056 uint32_t size = table[1];
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070057 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000058 kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -070059 switch_tables_.Insert(tab_rec);
60
61 // Get the switch value
62 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080063 RegStorage r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 /* Allocate key and disp temps */
buzbee2700f7e2014-03-07 09:46:20 -080065 RegStorage r_key = AllocTemp();
66 RegStorage r_disp = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070067 // Make sure r_key's register number is less than r_disp's number for ldmia
buzbee2700f7e2014-03-07 09:46:20 -080068 if (r_key.GetReg() > r_disp.GetReg()) {
69 RegStorage tmp = r_disp;
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 r_disp = r_key;
71 r_key = tmp;
72 }
73 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -080074 NewLIR3(kThumb2Adr, r_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -070075 // Set up r_idx
buzbee2700f7e2014-03-07 09:46:20 -080076 RegStorage r_idx = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 LoadConstant(r_idx, size);
78 // Establish loop branch target
79 LIR* target = NewLIR0(kPseudoTargetLabel);
80 // Load next key/disp
buzbee2700f7e2014-03-07 09:46:20 -080081 NewLIR2(kThumb2LdmiaWB, r_base.GetReg(), (1 << r_key.GetReg()) | (1 << r_disp.GetReg()));
82 OpRegReg(kOpCmp, r_key, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070083 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
Dave Allison3da67a52014-04-02 17:03:45 -070084 LIR* it = OpIT(kCondEq, "");
buzbee2700f7e2014-03-07 09:46:20 -080085 LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp.GetReg());
Dave Allison3da67a52014-04-02 17:03:45 -070086 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 tab_rec->anchor = switch_branch;
88 // Needs to use setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +000089 OpRegRegImm(kOpSub, r_idx, r_idx, 1); // For value == 1, this should set flags.
90 DCHECK(last_lir_insn_->u.m.def_mask & ENCODE_CCODE);
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 OpCondBranch(kCondNe, target);
92}
93
94
95void ArmMir2Lir::GenPackedSwitch(MIR* mir, uint32_t table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070096 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
98 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));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 switch_tables_.Insert(tab_rec);
110
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/*
143 * Array data table format:
144 * ushort ident = 0x0300 magic value
145 * ushort width width of each element in the table
146 * uint size number of elements in the table
147 * ubyte data[size*width] table of data values (may contain a single-byte
148 * padding at the end)
149 *
150 * Total size is 4+(width * size + 1)/2 16-bit code units.
151 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700152void ArmMir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700153 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
154 // Add the table to the list - we'll process it later
155 FillArrayData *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000156 static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 tab_rec->table = table;
158 tab_rec->vaddr = current_dalvik_offset_;
159 uint16_t width = tab_rec->table[1];
160 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
161 tab_rec->size = (size * width) + 8;
162
163 fill_array_data_.Insert(tab_rec);
164
165 // Making a call - use explicit registers
166 FlushAllRegs(); /* Everything to home location */
buzbee2700f7e2014-03-07 09:46:20 -0800167 LoadValueDirectFixed(rl_src, rs_r0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700168 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pHandleFillArrayData).Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -0800169 rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700170 // Materialize a pointer to the fill data image
buzbee0d829482013-10-11 15:24:55 -0700171 NewLIR3(kThumb2Adr, r1, 0, WrapPointer(tab_rec));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000172 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800173 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 MarkSafepointPC(call_inst);
175}
176
177/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700178 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
179 * details see monitor.cc.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700181void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 FlushAllRegs();
buzbee695d13a2014-04-19 13:32:20 -0700183 // FIXME: need separate LoadValues for object references.
buzbee2700f7e2014-03-07 09:46:20 -0800184 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700186 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
187 if (kArchVariantHasGoodBranchPredictor) {
Dave Allisonf9439142014-03-27 15:10:22 -0700188 LIR* null_check_branch = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700189 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
190 null_check_branch = nullptr; // No null check.
191 } else {
192 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allisonf9439142014-03-27 15:10:22 -0700193 if (Runtime::Current()->ExplicitNullChecks()) {
194 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
195 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700196 }
buzbee695d13a2014-04-19 13:32:20 -0700197 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700198 NewLIR3(kThumb2Ldrex, r1, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700199 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800200 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_r1, 0, NULL);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700201 NewLIR4(kThumb2Strex, r1, r2, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
buzbee2700f7e2014-03-07 09:46:20 -0800202 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_r1, 0, NULL);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700203
204
205 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
206 not_unlocked_branch->target = slow_path_target;
207 if (null_check_branch != nullptr) {
208 null_check_branch->target = slow_path_target;
209 }
210 // TODO: move to a slow path.
211 // Go expensive route - artLockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700212 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000213 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800214 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700215 MarkSafepointPC(call_inst);
216
217 LIR* success_target = NewLIR0(kPseudoTargetLabel);
218 lock_success_branch->target = success_target;
219 GenMemBarrier(kLoadLoad);
220 } else {
221 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800222 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700223 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700224 NewLIR3(kThumb2Ldrex, r1, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700225 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800226 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700227 LIR* it = OpIT(kCondEq, "");
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700228 NewLIR4(kThumb2Strex/*eq*/, r1, r2, r0, mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allison3da67a52014-04-02 17:03:45 -0700229 OpEndIT(it);
buzbee2700f7e2014-03-07 09:46:20 -0800230 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700231 it = OpIT(kCondNe, "T");
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700232 // Go expensive route - artLockObjectFromCode(self, obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700233 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000234 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800235 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700236 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700237 MarkSafepointPC(call_inst);
238 GenMemBarrier(kLoadLoad);
239 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240}
241
242/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700243 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
244 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
245 * and can only give away ownership if its suspended.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700246 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700247void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800249 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 LockCallTemps(); // Prepare for explicit register usage
Dave Allisonf9439142014-03-27 15:10:22 -0700251 LIR* null_check_branch = nullptr;
buzbee695d13a2014-04-19 13:32:20 -0700252 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700253 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
254 if (kArchVariantHasGoodBranchPredictor) {
255 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
256 null_check_branch = nullptr; // No null check.
257 } else {
258 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allisonf9439142014-03-27 15:10:22 -0700259 if (Runtime::Current()->ExplicitNullChecks()) {
260 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
261 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700262 }
buzbee695d13a2014-04-19 13:32:20 -0700263 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1);
Dave Allisonf9439142014-03-27 15:10:22 -0700264 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800265 LoadConstantNoClobber(rs_r3, 0);
266 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_r1, rs_r2, NULL);
buzbee695d13a2014-04-19 13:32:20 -0700267 Store32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700268 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
269
270 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
271 slow_unlock_branch->target = slow_path_target;
272 if (null_check_branch != nullptr) {
273 null_check_branch->target = slow_path_target;
274 }
275 // TODO: move to a slow path.
276 // Go expensive route - artUnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700277 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000278 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800279 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700280 MarkSafepointPC(call_inst);
281
282 LIR* success_target = NewLIR0(kPseudoTargetLabel);
283 unlock_success_branch->target = success_target;
284 GenMemBarrier(kStoreLoad);
285 } else {
286 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800287 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700288 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); // Get lock
Dave Allisonb373e092014-02-20 16:06:36 -0800289 MarkPossibleNullPointerException(opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700290 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee2700f7e2014-03-07 09:46:20 -0800291 LoadConstantNoClobber(rs_r3, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700292 // Is lock unheld on lock or held by us (==thread_id) on unlock?
buzbee2700f7e2014-03-07 09:46:20 -0800293 OpRegReg(kOpCmp, rs_r1, rs_r2);
Dave Allison3da67a52014-04-02 17:03:45 -0700294 LIR* it = OpIT(kCondEq, "EE");
buzbee695d13a2014-04-19 13:32:20 -0700295 Store32Disp/*eq*/(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700296 // Go expensive route - UnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700297 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -0800298 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000299 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800300 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700301 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700302 MarkSafepointPC(call_inst);
303 GenMemBarrier(kStoreLoad);
304 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305}
306
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700307void ArmMir2Lir::GenMoveException(RegLocation rl_dest) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700308 int ex_offset = Thread::ExceptionOffset<4>().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
buzbee2700f7e2014-03-07 09:46:20 -0800310 RegStorage reset_reg = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700311 Load32Disp(rs_rARM_SELF, ex_offset, rl_result.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 LoadConstant(reset_reg, 0);
buzbee695d13a2014-04-19 13:32:20 -0700313 Store32Disp(rs_rARM_SELF, ex_offset, reset_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 FreeTemp(reset_reg);
315 StoreValue(rl_dest, rl_result);
316}
317
318/*
319 * Mark garbage collection card. Skip if the value we're storing is null.
320 */
buzbee2700f7e2014-03-07 09:46:20 -0800321void ArmMir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) {
322 RegStorage reg_card_base = AllocTemp();
323 RegStorage reg_card_no = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700324 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700325 LoadWordDisp(rs_rARM_SELF, Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800327 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 LIR* target = NewLIR0(kPseudoTargetLabel);
329 branch_over->target = target;
330 FreeTemp(reg_card_base);
331 FreeTemp(reg_card_no);
332}
333
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700334void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 int spill_count = num_core_spills_ + num_fp_spills_;
336 /*
337 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
338 * mechanism know so it doesn't try to use any of them when
339 * expanding the frame or flushing. This leaves the utility
340 * code with a single temp: r12. This should be enough.
341 */
342 LockTemp(r0);
343 LockTemp(r1);
344 LockTemp(r2);
345 LockTemp(r3);
346
347 /*
348 * We can safely skip the stack overflow check if we're
349 * a leaf *and* our frame size < fudge factor.
350 */
351 bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
352 (static_cast<size_t>(frame_size_) <
353 Thread::kStackOverflowReservedBytes));
354 NewLIR0(kPseudoMethodEntry);
355 if (!skip_overflow_check) {
Dave Allisonb373e092014-02-20 16:06:36 -0800356 if (Runtime::Current()->ExplicitStackOverflowChecks()) {
357 /* Load stack limit */
buzbee695d13a2014-04-19 13:32:20 -0700358 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
Dave Allisonb373e092014-02-20 16:06:36 -0800359 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700360 }
361 /* Spill core callee saves */
362 NewLIR1(kThumb2Push, core_spill_mask_);
363 /* Need to spill any FP regs? */
364 if (num_fp_spills_) {
365 /*
366 * NOTE: fp spills are a little different from core spills in that
367 * they are pushed as a contiguous block. When promoting from
368 * the fp set, we must allocate all singles from s16..highest-promoted
369 */
370 NewLIR1(kThumb2VPushCS, num_fp_spills_);
371 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700372
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700373 const int spill_size = spill_count * 4;
374 const int frame_size_without_spills = frame_size_ - spill_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 if (!skip_overflow_check) {
Dave Allisonb373e092014-02-20 16:06:36 -0800376 if (Runtime::Current()->ExplicitStackOverflowChecks()) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700377 class StackOverflowSlowPath : public LIRSlowPath {
378 public:
379 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, bool restore_lr, size_t sp_displace)
380 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), restore_lr_(restore_lr),
381 sp_displace_(sp_displace) {
382 }
383 void Compile() OVERRIDE {
384 m2l_->ResetRegPool();
385 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700386 GenerateTargetLabel(kPseudoThrowTarget);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700387 if (restore_lr_) {
buzbee2700f7e2014-03-07 09:46:20 -0800388 m2l_->LoadWordDisp(rs_rARM_SP, sp_displace_ - 4, rs_rARM_LR);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700389 }
buzbee2700f7e2014-03-07 09:46:20 -0800390 m2l_->OpRegImm(kOpAdd, rs_rARM_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700391 m2l_->ClobberCallerSave();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700392 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700393 // Load the entrypoint directly into the pc instead of doing a load + branch. Assumes
394 // codegen and target are in thumb2 mode.
buzbee695d13a2014-04-19 13:32:20 -0700395 // NOTE: native pointer.
buzbee2700f7e2014-03-07 09:46:20 -0800396 m2l_->LoadWordDisp(rs_rARM_SELF, func_offset.Int32Value(), rs_rARM_PC);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700397 }
398
399 private:
400 const bool restore_lr_;
401 const size_t sp_displace_;
402 };
403 if (static_cast<size_t>(frame_size_) > Thread::kStackOverflowReservedUsableBytes) {
buzbee2700f7e2014-03-07 09:46:20 -0800404 OpRegRegImm(kOpSub, rs_rARM_LR, rs_rARM_SP, frame_size_without_spills);
405 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_LR, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700406 // Need to restore LR since we used it as a temp.
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700407 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, true, spill_size));
buzbee2700f7e2014-03-07 09:46:20 -0800408 OpRegCopy(rs_rARM_SP, rs_rARM_LR); // Establish stack
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700409 } else {
410 // If the frame is small enough we are guaranteed to have enough space that remains to
411 // handle signals on the user stack.
buzbee2700f7e2014-03-07 09:46:20 -0800412 OpRegRegImm(kOpSub, rs_rARM_SP, rs_rARM_SP, frame_size_without_spills);
413 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_SP, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700414 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, false, frame_size_));
415 }
Dave Allisonb373e092014-02-20 16:06:36 -0800416 } else {
417 // Implicit stack overflow check.
Dave Allisonf9439142014-03-27 15:10:22 -0700418 // Generate a load from [sp, #-overflowsize]. If this is in the stack
Dave Allisonb373e092014-02-20 16:06:36 -0800419 // redzone we will get a segmentation fault.
Dave Allisonf9439142014-03-27 15:10:22 -0700420 //
421 // Caveat coder: if someone changes the kStackOverflowReservedBytes value
422 // we need to make sure that it's loadable in an immediate field of
423 // a sub instruction. Otherwise we will get a temp allocation and the
424 // code size will increase.
425 OpRegRegImm(kOpSub, rs_r12, rs_rARM_SP, Thread::kStackOverflowReservedBytes);
buzbee695d13a2014-04-19 13:32:20 -0700426 Load32Disp(rs_r12, 0, rs_r12);
Dave Allisonb373e092014-02-20 16:06:36 -0800427 MarkPossibleStackOverflowException();
Dave Allisonf9439142014-03-27 15:10:22 -0700428 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Dave Allisonb373e092014-02-20 16:06:36 -0800429 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800431 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700432 }
433
434 FlushIns(ArgLocs, rl_method);
435
436 FreeTemp(r0);
437 FreeTemp(r1);
438 FreeTemp(r2);
439 FreeTemp(r3);
440}
441
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700442void ArmMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700443 int spill_count = num_core_spills_ + num_fp_spills_;
444 /*
445 * In the exit path, r0/r1 are live - make sure they aren't
446 * allocated by the register utilities as temps.
447 */
448 LockTemp(r0);
449 LockTemp(r1);
450
451 NewLIR0(kPseudoMethodExit);
buzbee2700f7e2014-03-07 09:46:20 -0800452 OpRegImm(kOpAdd, rs_rARM_SP, frame_size_ - (spill_count * 4));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 /* Need to restore any FP callee saves? */
454 if (num_fp_spills_) {
455 NewLIR1(kThumb2VPopCS, num_fp_spills_);
456 }
457 if (core_spill_mask_ & (1 << rARM_LR)) {
458 /* Unspill rARM_LR to rARM_PC */
459 core_spill_mask_ &= ~(1 << rARM_LR);
460 core_spill_mask_ |= (1 << rARM_PC);
461 }
462 NewLIR1(kThumb2Pop, core_spill_mask_);
463 if (!(core_spill_mask_ & (1 << rARM_PC))) {
464 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
465 NewLIR1(kThumbBx, rARM_LR);
466 }
467}
468
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800469void ArmMir2Lir::GenSpecialExitSequence() {
470 NewLIR1(kThumbBx, rARM_LR);
471}
472
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473} // namespace art