blob: f5407ae9ba934a53954cef1624819d1aa623a3f6 [file] [log] [blame]
Matteo Franchin43ec8732014-03-31 15:00:14 +01001/*
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
Matteo Franchin43ec8732014-03-31 15:00:14 +010019#include "codegen_arm64.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080020
21#include "arm64_lir.h"
22#include "base/logging.h"
23#include "dex/mir_graph.h"
Matteo Franchin43ec8732014-03-31 15:00:14 +010024#include "dex/quick/mir_to_lir-inl.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080025#include "driver/compiler_driver.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070026#include "gc/accounting/card_table.h"
Matteo Franchin43ec8732014-03-31 15:00:14 +010027#include "entrypoints/quick/quick_entrypoints.h"
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +010028#include "mirror/art_method.h"
29#include "mirror/object_array-inl.h"
Matteo Franchin43ec8732014-03-31 15:00:14 +010030
31namespace art {
32
33/*
34 * The sparse table in the literal pool is an array of <key,displacement>
Matteo Franchine45fb9e2014-05-06 10:10:30 +010035 * pairs. For each set, we'll load them as a pair using ldp.
Matteo Franchin43ec8732014-03-31 15:00:14 +010036 * The test loop will look something like:
37 *
38 * adr r_base, <table>
Matteo Franchine45fb9e2014-05-06 10:10:30 +010039 * ldr r_val, [rA64_SP, v_reg_off]
Matteo Franchin43ec8732014-03-31 15:00:14 +010040 * mov r_idx, #table_size
Matteo Franchine45fb9e2014-05-06 10:10:30 +010041 * loop:
42 * cbz r_idx, quit
43 * ldp r_key, r_disp, [r_base], #8
Matteo Franchin43ec8732014-03-31 15:00:14 +010044 * sub r_idx, #1
45 * cmp r_val, r_key
Matteo Franchine45fb9e2014-05-06 10:10:30 +010046 * b.ne loop
47 * adr r_base, #0 ; This is the instruction from which we compute displacements
48 * add r_base, r_disp
49 * br r_base
50 * quit:
Matteo Franchin43ec8732014-03-31 15:00:14 +010051 */
Andreas Gampe48971b32014-08-06 10:09:01 -070052void Arm64Mir2Lir::GenLargeSparseSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070053 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Matteo Franchin43ec8732014-03-31 15:00:14 +010054 // Add the table to the list - we'll process it later
55 SwitchTable *tab_rec =
56 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -080057 tab_rec->switch_mir = mir;
Matteo Franchin43ec8732014-03-31 15:00:14 +010058 tab_rec->table = table;
59 tab_rec->vaddr = current_dalvik_offset_;
60 uint32_t size = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +010061 switch_tables_.push_back(tab_rec);
Matteo Franchin43ec8732014-03-31 15:00:14 +010062
63 // Get the switch value
64 rl_src = LoadValue(rl_src, kCoreReg);
Matteo Franchin5acc8b02014-06-05 15:10:35 +010065 RegStorage r_base = AllocTempWide();
Matteo Franchine45fb9e2014-05-06 10:10:30 +010066 // Allocate key and disp temps.
Matteo Franchin43ec8732014-03-31 15:00:14 +010067 RegStorage r_key = AllocTemp();
68 RegStorage r_disp = AllocTemp();
Matteo Franchin43ec8732014-03-31 15:00:14 +010069 // Materialize a pointer to the switch table
Matteo Franchine45fb9e2014-05-06 10:10:30 +010070 NewLIR3(kA64Adr2xd, r_base.GetReg(), 0, WrapPointer(tab_rec));
Matteo Franchin43ec8732014-03-31 15:00:14 +010071 // Set up r_idx
72 RegStorage r_idx = AllocTemp();
73 LoadConstant(r_idx, size);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010074
75 // Entry of loop.
76 LIR* loop_entry = NewLIR0(kPseudoTargetLabel);
77 LIR* branch_out = NewLIR2(kA64Cbz2rt, r_idx.GetReg(), 0);
78
79 // Load next key/disp.
80 NewLIR4(kA64LdpPost4rrXD, r_key.GetReg(), r_disp.GetReg(), r_base.GetReg(), 2);
81 OpRegRegImm(kOpSub, r_idx, r_idx, 1);
82
83 // Go to next case, if key does not match.
Matteo Franchin43ec8732014-03-31 15:00:14 +010084 OpRegReg(kOpCmp, r_key, rl_src.reg);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010085 OpCondBranch(kCondNe, loop_entry);
86
87 // Key does match: branch to case label.
88 LIR* switch_label = NewLIR3(kA64Adr2xd, r_base.GetReg(), 0, -1);
89 tab_rec->anchor = switch_label;
90
91 // Add displacement to base branch address and go!
Andreas Gampe47b31aa2014-06-19 01:10:07 -070092 OpRegRegRegExtend(kOpAdd, r_base, r_base, As64BitReg(r_disp), kA64Sxtw, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010093 NewLIR1(kA64Br1x, r_base.GetReg());
94
95 // Loop exit label.
96 LIR* loop_exit = NewLIR0(kPseudoTargetLabel);
97 branch_out->target = loop_exit;
Matteo Franchin43ec8732014-03-31 15:00:14 +010098}
99
100
Andreas Gampe48971b32014-08-06 10:09:01 -0700101void Arm64Mir2Lir::GenLargePackedSwitch(MIR* mir, uint32_t table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700102 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100103 // Add the table to the list - we'll process it later
104 SwitchTable *tab_rec =
105 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800106 tab_rec->switch_mir = mir;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100107 tab_rec->table = table;
108 tab_rec->vaddr = current_dalvik_offset_;
109 uint32_t size = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100110 switch_tables_.push_back(tab_rec);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100111
112 // Get the switch value
113 rl_src = LoadValue(rl_src, kCoreReg);
Matteo Franchin5acc8b02014-06-05 15:10:35 +0100114 RegStorage table_base = AllocTempWide();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100115 // Materialize a pointer to the switch table
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100116 NewLIR3(kA64Adr2xd, table_base.GetReg(), 0, WrapPointer(tab_rec));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100117 int low_key = s4FromSwitchData(&table[2]);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100118 RegStorage key_reg;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100119 // Remove the bias, if necessary
120 if (low_key == 0) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100121 key_reg = rl_src.reg;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100122 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100123 key_reg = AllocTemp();
124 OpRegRegImm(kOpSub, key_reg, rl_src.reg, low_key);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100125 }
126 // Bounds check - if < 0 or >= size continue following switch
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100127 OpRegImm(kOpCmp, key_reg, size - 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100128 LIR* branch_over = OpCondBranch(kCondHi, NULL);
129
130 // Load the displacement from the switch table
131 RegStorage disp_reg = AllocTemp();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700132 LoadBaseIndexed(table_base, As64BitReg(key_reg), disp_reg, 2, k32);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100133
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100134 // Get base branch address.
Matteo Franchin5acc8b02014-06-05 15:10:35 +0100135 RegStorage branch_reg = AllocTempWide();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100136 LIR* switch_label = NewLIR3(kA64Adr2xd, branch_reg.GetReg(), 0, -1);
137 tab_rec->anchor = switch_label;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100138
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100139 // Add displacement to base branch address and go!
Andreas Gampe47b31aa2014-06-19 01:10:07 -0700140 OpRegRegRegExtend(kOpAdd, branch_reg, branch_reg, As64BitReg(disp_reg), kA64Sxtw, 0U);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100141 NewLIR1(kA64Br1x, branch_reg.GetReg());
142
143 // branch_over target here
Matteo Franchin43ec8732014-03-31 15:00:14 +0100144 LIR* target = NewLIR0(kPseudoTargetLabel);
145 branch_over->target = target;
146}
147
148/*
Matteo Franchin43ec8732014-03-31 15:00:14 +0100149 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
150 * details see monitor.cc.
151 */
152void Arm64Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
Zheng Xuc8304302014-05-15 17:21:01 +0100153 // x0/w0 = object
154 // w1 = thin lock thread id
155 // x2 = address of lock word
156 // w3 = lock word / store failure
157 // TUNING: How much performance we get when we inline this?
158 // Since we've already flush all register.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100159 FlushAllRegs();
Andreas Gampeccc60262014-07-04 18:02:38 -0700160 LoadValueDirectFixed(rl_src, rs_x0); // = TargetReg(kArg0, kRef)
Matteo Franchin43ec8732014-03-31 15:00:14 +0100161 LockCallTemps(); // Prepare for explicit register usage
Zheng Xuc8304302014-05-15 17:21:01 +0100162 LIR* null_check_branch = nullptr;
163 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
164 null_check_branch = nullptr; // No null check.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100165 } else {
Zheng Xuc8304302014-05-15 17:21:01 +0100166 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000167 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Zheng Xuc8304302014-05-15 17:21:01 +0100168 null_check_branch = OpCmpImmBranch(kCondEq, rs_x0, 0, NULL);
169 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100170 }
Zheng Xubaa7c882014-06-30 14:26:50 +0800171 Load32Disp(rs_xSELF, Thread::ThinLockIdOffset<8>().Int32Value(), rs_w1);
Zheng Xuc8304302014-05-15 17:21:01 +0100172 OpRegRegImm(kOpAdd, rs_x2, rs_x0, mirror::Object::MonitorOffset().Int32Value());
173 NewLIR2(kA64Ldxr2rX, rw3, rx2);
174 MarkPossibleNullPointerException(opt_flags);
buzbee5d13f122014-08-19 16:47:06 -0700175 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_w3, 0, NULL);
Zheng Xuc8304302014-05-15 17:21:01 +0100176 NewLIR3(kA64Stxr3wrX, rw3, rw1, rx2);
buzbee5d13f122014-08-19 16:47:06 -0700177 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_w3, 0, NULL);
Zheng Xuc8304302014-05-15 17:21:01 +0100178
179 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
180 not_unlocked_branch->target = slow_path_target;
181 if (null_check_branch != nullptr) {
182 null_check_branch->target = slow_path_target;
183 }
184 // TODO: move to a slow path.
185 // Go expensive route - artLockObjectFromCode(obj);
Zheng Xubaa7c882014-06-30 14:26:50 +0800186 LoadWordDisp(rs_xSELF, QUICK_ENTRYPOINT_OFFSET(8, pLockObject).Int32Value(), rs_xLR);
Zheng Xuc8304302014-05-15 17:21:01 +0100187 ClobberCallerSave();
Zheng Xubaa7c882014-06-30 14:26:50 +0800188 LIR* call_inst = OpReg(kOpBlx, rs_xLR);
Zheng Xuc8304302014-05-15 17:21:01 +0100189 MarkSafepointPC(call_inst);
190
191 LIR* success_target = NewLIR0(kPseudoTargetLabel);
192 lock_success_branch->target = success_target;
Hans Boehm48f5c472014-06-27 14:50:10 -0700193 GenMemBarrier(kLoadAny);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100194}
195
196/*
197 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
Zheng Xuc8304302014-05-15 17:21:01 +0100198 * details see monitor.cc. Note the code below doesn't use ldxr/stxr as the code holds the lock
Matteo Franchin43ec8732014-03-31 15:00:14 +0100199 * and can only give away ownership if its suspended.
200 */
201void Arm64Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
Zheng Xuc8304302014-05-15 17:21:01 +0100202 // x0/w0 = object
203 // w1 = thin lock thread id
204 // w2 = lock word
205 // TUNING: How much performance we get when we inline this?
206 // Since we've already flush all register.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100207 FlushAllRegs();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700208 LoadValueDirectFixed(rl_src, rs_x0); // Get obj
Matteo Franchin43ec8732014-03-31 15:00:14 +0100209 LockCallTemps(); // Prepare for explicit register usage
210 LIR* null_check_branch = nullptr;
Zheng Xuc8304302014-05-15 17:21:01 +0100211 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
212 null_check_branch = nullptr; // No null check.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100213 } else {
Zheng Xuc8304302014-05-15 17:21:01 +0100214 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
Dave Allison69dfe512014-07-11 17:11:58 +0000215 if (!cu_->compiler_driver->GetCompilerOptions().GetImplicitNullChecks()) {
Zheng Xuc8304302014-05-15 17:21:01 +0100216 null_check_branch = OpCmpImmBranch(kCondEq, rs_x0, 0, NULL);
217 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100218 }
Zheng Xubaa7c882014-06-30 14:26:50 +0800219 Load32Disp(rs_xSELF, Thread::ThinLockIdOffset<8>().Int32Value(), rs_w1);
Zheng Xuc8304302014-05-15 17:21:01 +0100220 Load32Disp(rs_x0, mirror::Object::MonitorOffset().Int32Value(), rs_w2);
221 MarkPossibleNullPointerException(opt_flags);
222 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_w1, rs_w2, NULL);
Hans Boehm48f5c472014-06-27 14:50:10 -0700223 GenMemBarrier(kAnyStore);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000224 Store32Disp(rs_x0, mirror::Object::MonitorOffset().Int32Value(), rs_wzr);
Zheng Xuc8304302014-05-15 17:21:01 +0100225 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
226
227 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
228 slow_unlock_branch->target = slow_path_target;
229 if (null_check_branch != nullptr) {
230 null_check_branch->target = slow_path_target;
231 }
232 // TODO: move to a slow path.
233 // Go expensive route - artUnlockObjectFromCode(obj);
Zheng Xubaa7c882014-06-30 14:26:50 +0800234 LoadWordDisp(rs_xSELF, QUICK_ENTRYPOINT_OFFSET(8, pUnlockObject).Int32Value(), rs_xLR);
Zheng Xuc8304302014-05-15 17:21:01 +0100235 ClobberCallerSave();
Zheng Xubaa7c882014-06-30 14:26:50 +0800236 LIR* call_inst = OpReg(kOpBlx, rs_xLR);
Zheng Xuc8304302014-05-15 17:21:01 +0100237 MarkSafepointPC(call_inst);
238
239 LIR* success_target = NewLIR0(kPseudoTargetLabel);
240 unlock_success_branch->target = success_target;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100241}
242
243void Arm64Mir2Lir::GenMoveException(RegLocation rl_dest) {
Andreas Gampe2f244e92014-05-08 03:35:25 -0700244 int ex_offset = Thread::ExceptionOffset<8>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700245 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
Zheng Xubaa7c882014-06-30 14:26:50 +0800246 LoadRefDisp(rs_xSELF, ex_offset, rl_result.reg, kNotVolatile);
247 StoreRefDisp(rs_xSELF, ex_offset, rs_xzr, kNotVolatile);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100248 StoreValue(rl_dest, rl_result);
249}
250
Vladimir Markobf535be2014-11-19 18:52:35 +0000251void Arm64Mir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100252 RegStorage reg_card_base = AllocTempWide();
Andreas Gampe4b537a82014-06-30 22:24:53 -0700253 RegStorage reg_card_no = AllocTempWide(); // Needs to be wide as addr is ref=64b
Zheng Xubaa7c882014-06-30 14:26:50 +0800254 LoadWordDisp(rs_xSELF, Thread::CardTableOffset<8>().Int32Value(), reg_card_base);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100255 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100256 // TODO(Arm64): generate "strb wB, [xB, wC, uxtw]" rather than "strb wB, [xB, xC]"?
Andreas Gampe4b537a82014-06-30 22:24:53 -0700257 StoreBaseIndexed(reg_card_base, reg_card_no, As32BitReg(reg_card_base),
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100258 0, kUnsignedByte);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100259 FreeTemp(reg_card_base);
260 FreeTemp(reg_card_no);
261}
262
263void Arm64Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100264 /*
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100265 * On entry, x0 to x7 are live. Let the register allocation
Matteo Franchin43ec8732014-03-31 15:00:14 +0100266 * mechanism know so it doesn't try to use any of them when
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100267 * expanding the frame or flushing.
268 * Reserve x8 & x9 for temporaries.
Matteo Franchin43ec8732014-03-31 15:00:14 +0100269 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100270 LockTemp(rs_x0);
271 LockTemp(rs_x1);
272 LockTemp(rs_x2);
273 LockTemp(rs_x3);
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100274 LockTemp(rs_x4);
275 LockTemp(rs_x5);
276 LockTemp(rs_x6);
277 LockTemp(rs_x7);
Zheng Xub551fdc2014-07-25 11:49:42 +0800278 LockTemp(rs_xIP0);
279 LockTemp(rs_xIP1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100280
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100281 /* TUNING:
282 * Use AllocTemp() and reuse LR if possible to give us the freedom on adjusting the number
283 * of temp registers.
284 */
285
Matteo Franchin43ec8732014-03-31 15:00:14 +0100286 /*
287 * We can safely skip the stack overflow check if we're
288 * a leaf *and* our frame size < fudge factor.
289 */
Matteo Franchin24314522014-11-12 18:06:14 +0000290 bool skip_overflow_check = mir_graph_->MethodIsLeaf() &&
291 !FrameNeedsStackCheck(frame_size_, kArm64);
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100292
Matteo Franchin43ec8732014-03-31 15:00:14 +0100293 NewLIR0(kPseudoMethodEntry);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100294
Dave Allison648d7112014-07-25 16:15:27 -0700295 const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(kArm64);
296 const bool large_frame = static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes;
297 bool generate_explicit_stack_overflow_check = large_frame ||
298 !cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks();
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100299 const int spill_count = num_core_spills_ + num_fp_spills_;
300 const int spill_size = (spill_count * kArm64PointerSize + 15) & ~0xf; // SP 16 byte alignment.
301 const int frame_size_without_spills = frame_size_ - spill_size;
302
Matteo Franchin43ec8732014-03-31 15:00:14 +0100303 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700304 if (generate_explicit_stack_overflow_check) {
Andreas Gampef29ecd62014-07-29 00:35:00 -0700305 // Load stack limit
306 LoadWordDisp(rs_xSELF, Thread::StackEndOffset<8>().Int32Value(), rs_xIP1);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100307 } else {
308 // Implicit stack overflow check.
309 // Generate a load from [sp, #-framesize]. If this is in the stack
310 // redzone we will get a segmentation fault.
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100311
Andreas Gampef29ecd62014-07-29 00:35:00 -0700312 // TODO: If the frame size is small enough, is it possible to make this a pre-indexed load,
313 // so that we can avoid the following "sub sp" when spilling?
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100314 OpRegRegImm(kOpSub, rs_x8, rs_sp, GetStackOverflowReservedBytes(kArm64));
Matteo Franchin24314522014-11-12 18:06:14 +0000315 Load32Disp(rs_x8, 0, rs_wzr);
Stuart Monteithd5c78f42014-06-11 16:44:46 +0100316 MarkPossibleStackOverflowException();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100317 }
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100318 }
319
Andreas Gampef29ecd62014-07-29 00:35:00 -0700320 int spilled_already = 0;
321 if (spill_size > 0) {
322 spilled_already = SpillRegs(rs_sp, core_spill_mask_, fp_spill_mask_, frame_size_);
323 DCHECK(spill_size == spilled_already || frame_size_ == spilled_already);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100324 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100325
Andreas Gampef29ecd62014-07-29 00:35:00 -0700326 if (spilled_already != frame_size_) {
327 OpRegImm(kOpSub, rs_sp, frame_size_without_spills);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100328 }
329
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100330 if (!skip_overflow_check) {
Dave Allison648d7112014-07-25 16:15:27 -0700331 if (generate_explicit_stack_overflow_check) {
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100332 class StackOverflowSlowPath: public LIRSlowPath {
333 public:
334 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace) :
335 LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr),
336 sp_displace_(sp_displace) {
337 }
338 void Compile() OVERRIDE {
339 m2l_->ResetRegPool();
340 m2l_->ResetDefTracking();
341 GenerateTargetLabel(kPseudoThrowTarget);
342 // Unwinds stack.
Zheng Xubaa7c882014-06-30 14:26:50 +0800343 m2l_->OpRegImm(kOpAdd, rs_sp, sp_displace_);
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100344 m2l_->ClobberCallerSave();
345 ThreadOffset<8> func_offset = QUICK_ENTRYPOINT_OFFSET(8, pThrowStackOverflow);
Zheng Xub551fdc2014-07-25 11:49:42 +0800346 m2l_->LockTemp(rs_xIP0);
347 m2l_->LoadWordDisp(rs_xSELF, func_offset.Int32Value(), rs_xIP0);
348 m2l_->NewLIR1(kA64Br1x, rs_xIP0.GetReg());
349 m2l_->FreeTemp(rs_xIP0);
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100350 }
351
352 private:
353 const size_t sp_displace_;
354 };
355
Andreas Gampef29ecd62014-07-29 00:35:00 -0700356 LIR* branch = OpCmpBranch(kCondUlt, rs_sp, rs_xIP1, nullptr);
357 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, frame_size_));
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100358 }
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100359 }
360
Matteo Franchin43ec8732014-03-31 15:00:14 +0100361 FlushIns(ArgLocs, rl_method);
362
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100363 FreeTemp(rs_x0);
364 FreeTemp(rs_x1);
365 FreeTemp(rs_x2);
366 FreeTemp(rs_x3);
Stuart Monteithf8ec48e2014-06-06 17:05:08 +0100367 FreeTemp(rs_x4);
368 FreeTemp(rs_x5);
369 FreeTemp(rs_x6);
370 FreeTemp(rs_x7);
Zheng Xub551fdc2014-07-25 11:49:42 +0800371 FreeTemp(rs_xIP0);
372 FreeTemp(rs_xIP1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100373}
374
375void Arm64Mir2Lir::GenExitSequence() {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100376 /*
377 * In the exit path, r0/r1 are live - make sure they aren't
378 * allocated by the register utilities as temps.
379 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100380 LockTemp(rs_x0);
381 LockTemp(rs_x1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100382
383 NewLIR0(kPseudoMethodExit);
Matteo Franchinbc6d1972014-05-13 12:33:28 +0100384
Andreas Gampef29ecd62014-07-29 00:35:00 -0700385 UnspillRegs(rs_sp, core_spill_mask_, fp_spill_mask_, frame_size_);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100386
buzbeeb5860fb2014-06-21 15:31:01 -0700387 // Finally return.
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100388 NewLIR0(kA64Ret);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100389}
390
391void Arm64Mir2Lir::GenSpecialExitSequence() {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100392 NewLIR0(kA64Ret);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100393}
394
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100395static bool Arm64UseRelativeCall(CompilationUnit* cu, const MethodReference& target_method) {
Brian Carlstrom14247b62015-01-31 21:35:32 -0800396 UNUSED(cu, target_method);
397 // Always emit relative calls.
398 return true;
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100399}
400
401/*
402 * Bit of a hack here - in the absence of a real scheduling pass,
403 * emit the next instruction in static & direct invoke sequences.
404 */
405static int Arm64NextSDCallInsn(CompilationUnit* cu, CallInfo* info,
406 int state, const MethodReference& target_method,
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700407 uint32_t unused_idx,
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100408 uintptr_t direct_code, uintptr_t direct_method,
409 InvokeType type) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700410 UNUSED(info, unused_idx);
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100411 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
412 if (direct_code != 0 && direct_method != 0) {
413 switch (state) {
414 case 0: // Get the current Method* [sets kArg0]
415 if (direct_code != static_cast<uintptr_t>(-1)) {
416 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
417 } else if (Arm64UseRelativeCall(cu, target_method)) {
418 // Defer to linker patch.
419 } else {
420 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
421 }
422 if (direct_method != static_cast<uintptr_t>(-1)) {
423 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
424 } else {
425 cg->LoadMethodAddress(target_method, type, kArg0);
426 }
427 break;
428 default:
429 return -1;
430 }
431 } else {
432 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
433 switch (state) {
434 case 0: // Get the current Method* [sets kArg0]
435 // TUNING: we can save a reg copy if Method* has been promoted.
436 cg->LoadCurrMethodDirect(arg0_ref);
437 break;
438 case 1: // Get method->dex_cache_resolved_methods_
439 cg->LoadRefDisp(arg0_ref,
440 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
441 arg0_ref,
442 kNotVolatile);
443 // Set up direct code if known.
444 if (direct_code != 0) {
445 if (direct_code != static_cast<uintptr_t>(-1)) {
446 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
447 } else if (Arm64UseRelativeCall(cu, target_method)) {
448 // Defer to linker patch.
449 } else {
450 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
451 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
452 }
453 }
454 break;
455 case 2: // Grab target method*
456 CHECK_EQ(cu->dex_file, target_method.dex_file);
457 cg->LoadRefDisp(arg0_ref,
458 mirror::ObjectArray<mirror::Object>::OffsetOfElement(
459 target_method.dex_method_index).Int32Value(),
460 arg0_ref,
461 kNotVolatile);
462 break;
463 case 3: // Grab the code from the method*
464 if (direct_code == 0) {
465 // kInvokeTgt := arg0_ref->entrypoint
466 cg->LoadWordDisp(arg0_ref,
Mathieu Chartier2d721012014-11-10 11:08:06 -0800467 mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
468 kArm64PointerSize).Int32Value(), cg->TargetPtrReg(kInvokeTgt));
Vladimir Marko7c2ad5a2014-09-24 12:42:55 +0100469 }
470 break;
471 default:
472 return -1;
473 }
474 }
475 return state + 1;
476}
477
478NextCallInsn Arm64Mir2Lir::GetNextSDCallInsn() {
479 return Arm64NextSDCallInsn;
480}
481
482LIR* Arm64Mir2Lir::CallWithLinkerFixup(const MethodReference& target_method, InvokeType type) {
483 // For ARM64, just generate a relative BL instruction that will be filled in at 'link time'.
484 // If the target turns out to be too far, the linker will generate a thunk for dispatch.
485 int target_method_idx = target_method.dex_method_index;
486 const DexFile* target_dex_file = target_method.dex_file;
487
488 // Generate the call instruction and save index, dex_file, and type.
489 // NOTE: Method deduplication takes linker patches into account, so we can just pass 0
490 // as a placeholder for the offset.
491 LIR* call = RawLIR(current_dalvik_offset_, kA64Bl1t, 0,
492 target_method_idx, WrapPointer(const_cast<DexFile*>(target_dex_file)), type);
493 AppendLIR(call);
494 call_method_insns_.push_back(call);
495 return call;
496}
497
498LIR* Arm64Mir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info) {
499 LIR* call_insn;
500 if (method_info.FastPath() && Arm64UseRelativeCall(cu_, method_info.GetTargetMethod()) &&
501 (method_info.GetSharpType() == kDirect || method_info.GetSharpType() == kStatic) &&
502 method_info.DirectCode() == static_cast<uintptr_t>(-1)) {
503 call_insn = CallWithLinkerFixup(method_info.GetTargetMethod(), method_info.GetSharpType());
504 } else {
505 call_insn = OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
506 }
507 return call_insn;
508}
509
Matteo Franchin43ec8732014-03-31 15:00:14 +0100510} // namespace art