blob: b1339916f0e724b996fd24374417aa2a2c15faf7 [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"
Ian Rogers166db042013-07-26 12:05:57 -070023#include "entrypoints/quick/quick_entrypoints.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
25namespace art {
26
Brian Carlstrom7940e442013-07-12 13:46:57 -070027/*
28 * The sparse table in the literal pool is an array of <key,displacement>
29 * pairs. For each set, we'll load them as a pair using ldmia.
30 * This means that the register number of the temp we use for the key
31 * must be lower than the reg for the displacement.
32 *
33 * The test loop will look something like:
34 *
buzbee2700f7e2014-03-07 09:46:20 -080035 * adr r_base, <table>
Brian Carlstrom7940e442013-07-12 13:46:57 -070036 * ldr r_val, [rARM_SP, v_reg_off]
37 * mov r_idx, #table_size
38 * lp:
buzbee2700f7e2014-03-07 09:46:20 -080039 * ldmia r_base!, {r_key, r_disp}
Brian Carlstrom7940e442013-07-12 13:46:57 -070040 * sub r_idx, #1
41 * cmp r_val, r_key
42 * ifeq
43 * add rARM_PC, r_disp ; This is the branch from which we compute displacement
44 * cbnz r_idx, lp
45 */
Andreas Gampe48971b32014-08-06 10:09:01 -070046void ArmMir2Lir::GenLargeSparseSwitch(MIR* mir, uint32_t table_offset, 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];
buzbee091cc402014-03-31 10:14:40 -070057 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -070058 switch_tables_.Insert(tab_rec);
59
60 // Get the switch value
61 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -080062 RegStorage r_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070063 /* Allocate key and disp temps */
buzbee2700f7e2014-03-07 09:46:20 -080064 RegStorage r_key = AllocTemp();
65 RegStorage r_disp = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 // Make sure r_key's register number is less than r_disp's number for ldmia
buzbee2700f7e2014-03-07 09:46:20 -080067 if (r_key.GetReg() > r_disp.GetReg()) {
68 RegStorage tmp = r_disp;
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 r_disp = r_key;
70 r_key = tmp;
71 }
72 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -080073 NewLIR3(kThumb2Adr, r_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -070074 // Set up r_idx
buzbee2700f7e2014-03-07 09:46:20 -080075 RegStorage r_idx = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 LoadConstant(r_idx, size);
77 // Establish loop branch target
78 LIR* target = NewLIR0(kPseudoTargetLabel);
79 // Load next key/disp
buzbee091cc402014-03-31 10:14:40 -070080 NewLIR2(kThumb2LdmiaWB, r_base.GetReg(), (1 << r_key.GetRegNum()) | (1 << r_disp.GetRegNum()));
buzbee2700f7e2014-03-07 09:46:20 -080081 OpRegReg(kOpCmp, r_key, rl_src.reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070082 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
Dave Allison3da67a52014-04-02 17:03:45 -070083 LIR* it = OpIT(kCondEq, "");
buzbee2700f7e2014-03-07 09:46:20 -080084 LIR* switch_branch = NewLIR1(kThumb2AddPCR, r_disp.GetReg());
Dave Allison3da67a52014-04-02 17:03:45 -070085 OpEndIT(it);
Brian Carlstrom7940e442013-07-12 13:46:57 -070086 tab_rec->anchor = switch_branch;
87 // Needs to use setflags encoding here
Vladimir Markodbb8c492014-02-28 17:36:39 +000088 OpRegRegImm(kOpSub, r_idx, r_idx, 1); // For value == 1, this should set flags.
Vladimir Marko8dea81c2014-06-06 14:50:36 +010089 DCHECK(last_lir_insn_->u.m.def_mask->HasBit(ResourceMask::kCCode));
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 OpCondBranch(kCondNe, target);
91}
92
93
Andreas Gampe48971b32014-08-06 10:09:01 -070094void ArmMir2Lir::GenLargePackedSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
96 if (cu_->verbose) {
97 DumpPackedSwitchTable(table);
98 }
99 // Add the table to the list - we'll process it later
100 SwitchTable *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000101 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102 tab_rec->table = table;
103 tab_rec->vaddr = current_dalvik_offset_;
buzbee0d829482013-10-11 15:24:55 -0700104 uint32_t size = table[1];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 tab_rec->targets =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000106 static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 switch_tables_.Insert(tab_rec);
108
109 // Get the switch value
110 rl_src = LoadValue(rl_src, kCoreReg);
buzbee2700f7e2014-03-07 09:46:20 -0800111 RegStorage table_base = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112 // Materialize a pointer to the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800113 NewLIR3(kThumb2Adr, table_base.GetReg(), 0, WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 int low_key = s4FromSwitchData(&table[2]);
buzbee2700f7e2014-03-07 09:46:20 -0800115 RegStorage keyReg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 // Remove the bias, if necessary
117 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800118 keyReg = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700119 } else {
120 keyReg = AllocTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800121 OpRegRegImm(kOpSub, keyReg, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122 }
123 // Bounds check - if < 0 or >= size continue following switch
124 OpRegImm(kOpCmp, keyReg, size-1);
125 LIR* branch_over = OpCondBranch(kCondHi, NULL);
126
127 // Load the displacement from the switch table
buzbee2700f7e2014-03-07 09:46:20 -0800128 RegStorage disp_reg = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700129 LoadBaseIndexed(table_base, keyReg, disp_reg, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700130
131 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
buzbee2700f7e2014-03-07 09:46:20 -0800132 LIR* switch_branch = NewLIR1(kThumb2AddPCR, disp_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 tab_rec->anchor = switch_branch;
134
135 /* branch_over target here */
136 LIR* target = NewLIR0(kPseudoTargetLabel);
137 branch_over->target = target;
138}
139
140/*
141 * Array data table format:
142 * ushort ident = 0x0300 magic value
143 * ushort width width of each element in the table
144 * uint size number of elements in the table
145 * ubyte data[size*width] table of data values (may contain a single-byte
146 * padding at the end)
147 *
148 * Total size is 4+(width * size + 1)/2 16-bit code units.
149 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700150void ArmMir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
152 // Add the table to the list - we'll process it later
153 FillArrayData *tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000154 static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155 tab_rec->table = table;
156 tab_rec->vaddr = current_dalvik_offset_;
157 uint16_t width = tab_rec->table[1];
158 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
159 tab_rec->size = (size * width) + 8;
160
161 fill_array_data_.Insert(tab_rec);
162
163 // Making a call - use explicit registers
164 FlushAllRegs(); /* Everything to home location */
buzbee2700f7e2014-03-07 09:46:20 -0800165 LoadValueDirectFixed(rl_src, rs_r0);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700166 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pHandleFillArrayData).Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -0800167 rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 // Materialize a pointer to the fill data image
buzbee091cc402014-03-31 10:14:40 -0700169 NewLIR3(kThumb2Adr, rs_r1.GetReg(), 0, WrapPointer(tab_rec));
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000170 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800171 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700172 MarkSafepointPC(call_inst);
173}
174
175/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700176 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
177 * details see monitor.cc.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700179void ArmMir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700180 FlushAllRegs();
buzbee695d13a2014-04-19 13:32:20 -0700181 // FIXME: need separate LoadValues for object references.
buzbee2700f7e2014-03-07 09:46:20 -0800182 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 LockCallTemps(); // Prepare for explicit register usage
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700184 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
185 if (kArchVariantHasGoodBranchPredictor) {
Dave Allisonf9439142014-03-27 15:10:22 -0700186 LIR* null_check_branch = nullptr;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700187 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
188 null_check_branch = nullptr; // No null check.
189 } else {
190 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000191 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700192 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
193 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700194 }
buzbee695d13a2014-04-19 13:32:20 -0700195 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee091cc402014-03-31 10:14:40 -0700196 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
197 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700198 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800199 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_r1, 0, NULL);
buzbee091cc402014-03-31 10:14:40 -0700200 NewLIR4(kThumb2Strex, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
201 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;
Hans Boehm48f5c472014-06-27 14:50:10 -0700219 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700220 } 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);
buzbee091cc402014-03-31 10:14:40 -0700224 NewLIR3(kThumb2Ldrex, rs_r1.GetReg(), rs_r0.GetReg(),
225 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allisonf9439142014-03-27 15:10:22 -0700226 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800227 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700228 LIR* it = OpIT(kCondEq, "");
buzbee091cc402014-03-31 10:14:40 -0700229 NewLIR4(kThumb2Strex/*eq*/, rs_r1.GetReg(), rs_r2.GetReg(), rs_r0.GetReg(),
230 mirror::Object::MonitorOffset().Int32Value() >> 2);
Dave Allison3da67a52014-04-02 17:03:45 -0700231 OpEndIT(it);
buzbee2700f7e2014-03-07 09:46:20 -0800232 OpRegImm(kOpCmp, rs_r1, 0);
Dave Allison3da67a52014-04-02 17:03:45 -0700233 it = OpIT(kCondNe, "T");
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700234 // Go expensive route - artLockObjectFromCode(self, obj);
buzbee091cc402014-03-31 10:14:40 -0700235 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pLockObject).Int32Value(),
236 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000237 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800238 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700239 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700240 MarkSafepointPC(call_inst);
Hans Boehm48f5c472014-06-27 14:50:10 -0700241 GenMemBarrier(kLoadAny);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700242 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700243}
244
245/*
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700246 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
247 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
248 * and can only give away ownership if its suspended.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700250void ArmMir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 FlushAllRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800252 LoadValueDirectFixed(rl_src, rs_r0); // Get obj
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253 LockCallTemps(); // Prepare for explicit register usage
Dave Allisonf9439142014-03-27 15:10:22 -0700254 LIR* null_check_branch = nullptr;
buzbee695d13a2014-04-19 13:32:20 -0700255 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700256 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
257 if (kArchVariantHasGoodBranchPredictor) {
258 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
259 null_check_branch = nullptr; // No null check.
260 } else {
261 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000262 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Dave Allisonf9439142014-03-27 15:10:22 -0700263 null_check_branch = OpCmpImmBranch(kCondEq, rs_r0, 0, NULL);
264 }
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700265 }
buzbee695d13a2014-04-19 13:32:20 -0700266 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1);
Dave Allisonf9439142014-03-27 15:10:22 -0700267 MarkPossibleNullPointerException(opt_flags);
buzbee2700f7e2014-03-07 09:46:20 -0800268 LoadConstantNoClobber(rs_r3, 0);
269 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_r1, rs_r2, NULL);
Hans Boehm48f5c472014-06-27 14:50:10 -0700270 GenMemBarrier(kAnyStore);
buzbee695d13a2014-04-19 13:32:20 -0700271 Store32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700272 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
273
274 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
275 slow_unlock_branch->target = slow_path_target;
276 if (null_check_branch != nullptr) {
277 null_check_branch->target = slow_path_target;
278 }
279 // TODO: move to a slow path.
280 // Go expensive route - artUnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700281 LoadWordDisp(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(), rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000282 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800283 LIR* call_inst = OpReg(kOpBlx, rs_rARM_LR);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700284 MarkSafepointPC(call_inst);
285
286 LIR* success_target = NewLIR0(kPseudoTargetLabel);
287 unlock_success_branch->target = success_target;
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700288 } else {
289 // Explicit null-check as slow-path is entered using an IT.
buzbee2700f7e2014-03-07 09:46:20 -0800290 GenNullCheck(rs_r0, opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700291 Load32Disp(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r1); // Get lock
Dave Allisonb373e092014-02-20 16:06:36 -0800292 MarkPossibleNullPointerException(opt_flags);
buzbee695d13a2014-04-19 13:32:20 -0700293 Load32Disp(rs_rARM_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_r2);
buzbee2700f7e2014-03-07 09:46:20 -0800294 LoadConstantNoClobber(rs_r3, 0);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700295 // Is lock unheld on lock or held by us (==thread_id) on unlock?
buzbee2700f7e2014-03-07 09:46:20 -0800296 OpRegReg(kOpCmp, rs_r1, rs_r2);
Andreas Gampeb14329f2014-05-15 11:16:06 -0700297
298 LIR* it = OpIT(kCondEq, "EE");
Hans Boehm48f5c472014-06-27 14:50:10 -0700299 if (GenMemBarrier(kAnyStore)) {
Andreas Gampeb14329f2014-05-15 11:16:06 -0700300 UpdateIT(it, "TEE");
301 }
buzbee695d13a2014-04-19 13:32:20 -0700302 Store32Disp/*eq*/(rs_r0, mirror::Object::MonitorOffset().Int32Value(), rs_r3);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700303 // Go expensive route - UnlockObjectFromCode(obj);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700304 LoadWordDisp/*ne*/(rs_rARM_SELF, QUICK_ENTRYPOINT_OFFSET(4, pUnlockObject).Int32Value(),
buzbee2700f7e2014-03-07 09:46:20 -0800305 rs_rARM_LR);
Vladimir Marko31c2aac2013-12-09 16:31:19 +0000306 ClobberCallerSave();
buzbee2700f7e2014-03-07 09:46:20 -0800307 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rARM_LR);
Dave Allison3da67a52014-04-02 17:03:45 -0700308 OpEndIT(it);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700309 MarkSafepointPC(call_inst);
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700310 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311}
312
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700313void ArmMir2Lir::GenMoveException(RegLocation rl_dest) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700314 int ex_offset = Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700315 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
316 RegStorage reset_reg = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000317 LoadRefDisp(rs_rARM_SELF, ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700318 LoadConstant(reset_reg, 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000319 StoreRefDisp(rs_rARM_SELF, ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700320 FreeTemp(reset_reg);
321 StoreValue(rl_dest, rl_result);
322}
323
324/*
325 * Mark garbage collection card. Skip if the value we're storing is null.
326 */
buzbee2700f7e2014-03-07 09:46:20 -0800327void ArmMir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) {
328 RegStorage reg_card_base = AllocTemp();
329 RegStorage reg_card_no = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700330 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700331 LoadWordDisp(rs_rARM_SELF, Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800333 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 LIR* target = NewLIR0(kPseudoTargetLabel);
335 branch_over->target = target;
336 FreeTemp(reg_card_base);
337 FreeTemp(reg_card_no);
338}
339
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700340void ArmMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700341 int spill_count = num_core_spills_ + num_fp_spills_;
342 /*
343 * On entry, r0, r1, r2 & r3 are live. Let the register allocation
344 * mechanism know so it doesn't try to use any of them when
345 * expanding the frame or flushing. This leaves the utility
346 * code with a single temp: r12. This should be enough.
347 */
buzbee091cc402014-03-31 10:14:40 -0700348 LockTemp(rs_r0);
349 LockTemp(rs_r1);
350 LockTemp(rs_r2);
351 LockTemp(rs_r3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700352
353 /*
354 * We can safely skip the stack overflow check if we're
355 * a leaf *and* our frame size < fudge factor.
356 */
Andreas Gampe7cd26f32014-06-18 17:01:15 -0700357 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !IsLargeFrame(frame_size_, kArm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700358 NewLIR0(kPseudoMethodEntry);
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700359 const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(kArm) -
Andreas Gampe7cd26f32014-06-18 17:01:15 -0700360 Thread::kStackOverflowSignalReservedBytes;
361 bool large_frame = (static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362 if (!skip_overflow_check) {
Dave Allison69dfe512014-07-11 17:11:58 +0000363 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000364 if (!large_frame) {
365 /* Load stack limit */
366 LockTemp(rs_r12);
367 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
368 }
Dave Allison5cd33752014-04-15 15:57:58 -0700369 } else {
370 // Implicit stack overflow check.
371 // Generate a load from [sp, #-overflowsize]. If this is in the stack
372 // redzone we will get a segmentation fault.
373 //
374 // Caveat coder: if someone changes the kStackOverflowReservedBytes value
375 // we need to make sure that it's loadable in an immediate field of
376 // a sub instruction. Otherwise we will get a temp allocation and the
377 // code size will increase.
378 //
379 // This is done before the callee save instructions to avoid any possibility
380 // of these overflowing. This uses r12 and that's never saved in a callee
381 // save.
Andreas Gampe7ea6f792014-07-14 16:21:44 -0700382 OpRegRegImm(kOpSub, rs_r12, rs_rARM_SP, GetStackOverflowReservedBytes(kArm));
Dave Allison5cd33752014-04-15 15:57:58 -0700383 Load32Disp(rs_r12, 0, rs_r12);
384 MarkPossibleStackOverflowException();
Dave Allisonb373e092014-02-20 16:06:36 -0800385 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 }
387 /* Spill core callee saves */
388 NewLIR1(kThumb2Push, core_spill_mask_);
389 /* Need to spill any FP regs? */
390 if (num_fp_spills_) {
391 /*
392 * NOTE: fp spills are a little different from core spills in that
393 * they are pushed as a contiguous block. When promoting from
394 * the fp set, we must allocate all singles from s16..highest-promoted
395 */
396 NewLIR1(kThumb2VPushCS, num_fp_spills_);
397 }
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700398
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700399 const int spill_size = spill_count * 4;
400 const int frame_size_without_spills = frame_size_ - spill_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 if (!skip_overflow_check) {
Dave Allison69dfe512014-07-11 17:11:58 +0000402 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks()) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700403 class StackOverflowSlowPath : public LIRSlowPath {
404 public:
405 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, bool restore_lr, size_t sp_displace)
406 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), restore_lr_(restore_lr),
407 sp_displace_(sp_displace) {
408 }
409 void Compile() OVERRIDE {
410 m2l_->ResetRegPool();
411 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700412 GenerateTargetLabel(kPseudoThrowTarget);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700413 if (restore_lr_) {
buzbee2700f7e2014-03-07 09:46:20 -0800414 m2l_->LoadWordDisp(rs_rARM_SP, sp_displace_ - 4, rs_rARM_LR);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700415 }
buzbee2700f7e2014-03-07 09:46:20 -0800416 m2l_->OpRegImm(kOpAdd, rs_rARM_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700417 m2l_->ClobberCallerSave();
Ian Rogersdd7624d2014-03-14 17:43:00 -0700418 ThreadOffset<4> func_offset = QUICK_ENTRYPOINT_OFFSET(4, pThrowStackOverflow);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700419 // Load the entrypoint directly into the pc instead of doing a load + branch. Assumes
420 // codegen and target are in thumb2 mode.
buzbee695d13a2014-04-19 13:32:20 -0700421 // NOTE: native pointer.
buzbee2700f7e2014-03-07 09:46:20 -0800422 m2l_->LoadWordDisp(rs_rARM_SELF, func_offset.Int32Value(), rs_rARM_PC);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700423 }
424
425 private:
426 const bool restore_lr_;
427 const size_t sp_displace_;
428 };
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000429 if (large_frame) {
430 // Note: may need a temp reg, and we only have r12 free at this point.
buzbee2700f7e2014-03-07 09:46:20 -0800431 OpRegRegImm(kOpSub, rs_rARM_LR, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000432 Load32Disp(rs_rARM_SELF, Thread::StackEndOffset<4>().Int32Value(), rs_r12);
buzbee2700f7e2014-03-07 09:46:20 -0800433 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_LR, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700434 // Need to restore LR since we used it as a temp.
Mathieu Chartier05a48b12014-03-31 16:11:41 -0700435 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, true, spill_size));
buzbee2700f7e2014-03-07 09:46:20 -0800436 OpRegCopy(rs_rARM_SP, rs_rARM_LR); // Establish stack
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700437 } else {
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000438 /*
439 * If the frame is small enough we are guaranteed to have enough space that remains to
440 * handle signals on the user stack. However, we may not have any free temp
441 * registers at this point, so we'll temporarily add LR to the temp pool.
442 */
443 DCHECK(!GetRegInfo(rs_rARM_LR)->IsTemp());
444 MarkTemp(rs_rARM_LR);
445 FreeTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800446 OpRegRegImm(kOpSub, rs_rARM_SP, rs_rARM_SP, frame_size_without_spills);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000447 Clobber(rs_rARM_LR);
448 UnmarkTemp(rs_rARM_LR);
buzbee2700f7e2014-03-07 09:46:20 -0800449 LIR* branch = OpCmpBranch(kCondUlt, rs_rARM_SP, rs_r12, nullptr);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700450 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, false, frame_size_));
451 }
Dave Allisonb373e092014-02-20 16:06:36 -0800452 } else {
Dave Allison5cd33752014-04-15 15:57:58 -0700453 // Implicit stack overflow check has already been done. Just make room on the
454 // stack for the frame now.
Dave Allisonf9439142014-03-27 15:10:22 -0700455 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Dave Allisonb373e092014-02-20 16:06:36 -0800456 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700457 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800458 OpRegImm(kOpSub, rs_rARM_SP, frame_size_without_spills);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 }
460
461 FlushIns(ArgLocs, rl_method);
462
buzbee091cc402014-03-31 10:14:40 -0700463 FreeTemp(rs_r0);
464 FreeTemp(rs_r1);
465 FreeTemp(rs_r2);
466 FreeTemp(rs_r3);
Bill Buzbeefe8cf8b2014-05-15 13:57:54 +0000467 FreeTemp(rs_r12);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700468}
469
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700470void ArmMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700471 int spill_count = num_core_spills_ + num_fp_spills_;
472 /*
473 * In the exit path, r0/r1 are live - make sure they aren't
474 * allocated by the register utilities as temps.
475 */
buzbee091cc402014-03-31 10:14:40 -0700476 LockTemp(rs_r0);
477 LockTemp(rs_r1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478
479 NewLIR0(kPseudoMethodExit);
buzbee2700f7e2014-03-07 09:46:20 -0800480 OpRegImm(kOpAdd, rs_rARM_SP, frame_size_ - (spill_count * 4));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 /* Need to restore any FP callee saves? */
482 if (num_fp_spills_) {
483 NewLIR1(kThumb2VPopCS, num_fp_spills_);
484 }
buzbee091cc402014-03-31 10:14:40 -0700485 if (core_spill_mask_ & (1 << rs_rARM_LR.GetRegNum())) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486 /* Unspill rARM_LR to rARM_PC */
buzbee091cc402014-03-31 10:14:40 -0700487 core_spill_mask_ &= ~(1 << rs_rARM_LR.GetRegNum());
488 core_spill_mask_ |= (1 << rs_rARM_PC.GetRegNum());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700489 }
490 NewLIR1(kThumb2Pop, core_spill_mask_);
buzbee091cc402014-03-31 10:14:40 -0700491 if (!(core_spill_mask_ & (1 << rs_rARM_PC.GetRegNum()))) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700492 /* We didn't pop to rARM_PC, so must do a bv rARM_LR */
buzbee091cc402014-03-31 10:14:40 -0700493 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700494 }
495}
496
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800497void ArmMir2Lir::GenSpecialExitSequence() {
buzbee091cc402014-03-31 10:14:40 -0700498 NewLIR1(kThumbBx, rs_rARM_LR.GetReg());
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800499}
500
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501} // namespace art