blob: c210816dfdbe86a68ae0261aa81826c067fad13c [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
19#include "arm64_lir.h"
20#include "codegen_arm64.h"
21#include "dex/quick/mir_to_lir-inl.h"
22#include "entrypoints/quick/quick_entrypoints.h"
23
24namespace art {
25
Matteo Franchine45fb9e2014-05-06 10:10:30 +010026bool Arm64Mir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir,
27 const InlineMethod& special) {
28 return Mir2Lir::GenSpecialCase(bb, mir, special);
29}
30
Matteo Franchin43ec8732014-03-31 15:00:14 +010031/*
32 * The sparse table in the literal pool is an array of <key,displacement>
Matteo Franchine45fb9e2014-05-06 10:10:30 +010033 * pairs. For each set, we'll load them as a pair using ldp.
Matteo Franchin43ec8732014-03-31 15:00:14 +010034 * The test loop will look something like:
35 *
36 * adr r_base, <table>
Matteo Franchine45fb9e2014-05-06 10:10:30 +010037 * ldr r_val, [rA64_SP, v_reg_off]
Matteo Franchin43ec8732014-03-31 15:00:14 +010038 * mov r_idx, #table_size
Matteo Franchine45fb9e2014-05-06 10:10:30 +010039 * loop:
40 * cbz r_idx, quit
41 * ldp r_key, r_disp, [r_base], #8
Matteo Franchin43ec8732014-03-31 15:00:14 +010042 * sub r_idx, #1
43 * cmp r_val, r_key
Matteo Franchine45fb9e2014-05-06 10:10:30 +010044 * b.ne loop
45 * adr r_base, #0 ; This is the instruction from which we compute displacements
46 * add r_base, r_disp
47 * br r_base
48 * quit:
Matteo Franchin43ec8732014-03-31 15:00:14 +010049 */
50void Arm64Mir2Lir::GenSparseSwitch(MIR* mir, uint32_t table_offset,
Matteo Franchine45fb9e2014-05-06 10:10:30 +010051 RegLocation rl_src) {
Matteo Franchin43ec8732014-03-31 15:00:14 +010052 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
53 if (cu_->verbose) {
54 DumpSparseSwitchTable(table);
55 }
56 // Add the table to the list - we'll process it later
57 SwitchTable *tab_rec =
58 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
59 tab_rec->table = table;
60 tab_rec->vaddr = current_dalvik_offset_;
61 uint32_t size = table[1];
62 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
63 switch_tables_.Insert(tab_rec);
64
65 // Get the switch value
66 rl_src = LoadValue(rl_src, kCoreReg);
67 RegStorage r_base = AllocTemp();
Matteo Franchine45fb9e2014-05-06 10:10:30 +010068 // Allocate key and disp temps.
Matteo Franchin43ec8732014-03-31 15:00:14 +010069 RegStorage r_key = AllocTemp();
70 RegStorage r_disp = AllocTemp();
Matteo Franchin43ec8732014-03-31 15:00:14 +010071 // Materialize a pointer to the switch table
Matteo Franchine45fb9e2014-05-06 10:10:30 +010072 NewLIR3(kA64Adr2xd, r_base.GetReg(), 0, WrapPointer(tab_rec));
Matteo Franchin43ec8732014-03-31 15:00:14 +010073 // Set up r_idx
74 RegStorage r_idx = AllocTemp();
75 LoadConstant(r_idx, size);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010076
77 // Entry of loop.
78 LIR* loop_entry = NewLIR0(kPseudoTargetLabel);
79 LIR* branch_out = NewLIR2(kA64Cbz2rt, r_idx.GetReg(), 0);
80
81 // Load next key/disp.
82 NewLIR4(kA64LdpPost4rrXD, r_key.GetReg(), r_disp.GetReg(), r_base.GetReg(), 2);
83 OpRegRegImm(kOpSub, r_idx, r_idx, 1);
84
85 // Go to next case, if key does not match.
Matteo Franchin43ec8732014-03-31 15:00:14 +010086 OpRegReg(kOpCmp, r_key, rl_src.reg);
Matteo Franchine45fb9e2014-05-06 10:10:30 +010087 OpCondBranch(kCondNe, loop_entry);
88
89 // Key does match: branch to case label.
90 LIR* switch_label = NewLIR3(kA64Adr2xd, r_base.GetReg(), 0, -1);
91 tab_rec->anchor = switch_label;
92
93 // Add displacement to base branch address and go!
94 OpRegRegRegShift(kOpAdd, r_base.GetReg(), r_base.GetReg(), r_disp.GetReg(),
95 ENCODE_NO_SHIFT, true);
96 NewLIR1(kA64Br1x, r_base.GetReg());
97
98 // Loop exit label.
99 LIR* loop_exit = NewLIR0(kPseudoTargetLabel);
100 branch_out->target = loop_exit;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100101}
102
103
104void Arm64Mir2Lir::GenPackedSwitch(MIR* mir, uint32_t table_offset,
105 RegLocation rl_src) {
106 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
107 if (cu_->verbose) {
108 DumpPackedSwitchTable(table);
109 }
110 // Add the table to the list - we'll process it later
111 SwitchTable *tab_rec =
112 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
113 tab_rec->table = table;
114 tab_rec->vaddr = current_dalvik_offset_;
115 uint32_t size = table[1];
116 tab_rec->targets =
117 static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*), kArenaAllocLIR));
118 switch_tables_.Insert(tab_rec);
119
120 // Get the switch value
121 rl_src = LoadValue(rl_src, kCoreReg);
122 RegStorage table_base = AllocTemp();
123 // Materialize a pointer to the switch table
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100124 NewLIR3(kA64Adr2xd, table_base.GetReg(), 0, WrapPointer(tab_rec));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100125 int low_key = s4FromSwitchData(&table[2]);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100126 RegStorage key_reg;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100127 // Remove the bias, if necessary
128 if (low_key == 0) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100129 key_reg = rl_src.reg;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100130 } else {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100131 key_reg = AllocTemp();
132 OpRegRegImm(kOpSub, key_reg, rl_src.reg, low_key);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100133 }
134 // Bounds check - if < 0 or >= size continue following switch
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100135 OpRegImm(kOpCmp, key_reg, size - 1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100136 LIR* branch_over = OpCondBranch(kCondHi, NULL);
137
138 // Load the displacement from the switch table
139 RegStorage disp_reg = AllocTemp();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100140 LoadBaseIndexed(table_base, key_reg, disp_reg, 2, k32);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100141
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100142 // Get base branch address.
143 RegStorage branch_reg = AllocTemp();
144 LIR* switch_label = NewLIR3(kA64Adr2xd, branch_reg.GetReg(), 0, -1);
145 tab_rec->anchor = switch_label;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100146
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100147 // Add displacement to base branch address and go!
148 OpRegRegRegShift(kOpAdd, branch_reg.GetReg(), branch_reg.GetReg(), disp_reg.GetReg(),
149 ENCODE_NO_SHIFT, true);
150 NewLIR1(kA64Br1x, branch_reg.GetReg());
151
152 // branch_over target here
Matteo Franchin43ec8732014-03-31 15:00:14 +0100153 LIR* target = NewLIR0(kPseudoTargetLabel);
154 branch_over->target = target;
155}
156
157/*
158 * Array data table format:
159 * ushort ident = 0x0300 magic value
160 * ushort width width of each element in the table
161 * uint size number of elements in the table
162 * ubyte data[size*width] table of data values (may contain a single-byte
163 * padding at the end)
164 *
165 * Total size is 4+(width * size + 1)/2 16-bit code units.
166 */
167void Arm64Mir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src) {
168 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
169 // Add the table to the list - we'll process it later
170 FillArrayData *tab_rec =
171 static_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData), kArenaAllocData));
172 tab_rec->table = table;
173 tab_rec->vaddr = current_dalvik_offset_;
174 uint16_t width = tab_rec->table[1];
175 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
176 tab_rec->size = (size * width) + 8;
177
178 fill_array_data_.Insert(tab_rec);
179
180 // Making a call - use explicit registers
181 FlushAllRegs(); /* Everything to home location */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100182 LoadValueDirectFixed(rl_src, rs_x0);
183 LoadWordDisp(rs_rA64_SELF, A64_QUICK_ENTRYPOINT_INT_OFFS(pHandleFillArrayData),
184 rs_rA64_LR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100185 // Materialize a pointer to the fill data image
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100186 NewLIR3(kA64Adr2xd, rx1, 0, WrapPointer(tab_rec));
Matteo Franchin43ec8732014-03-31 15:00:14 +0100187 ClobberCallerSave();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100188 LIR* call_inst = OpReg(kOpBlx, rs_rA64_LR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100189 MarkSafepointPC(call_inst);
190}
191
192/*
193 * Handle unlocked -> thin locked transition inline or else call out to quick entrypoint. For more
194 * details see monitor.cc.
195 */
196void Arm64Mir2Lir::GenMonitorEnter(int opt_flags, RegLocation rl_src) {
197 FlushAllRegs();
198 // FIXME: need separate LoadValues for object references.
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100199 LoadValueDirectFixed(rl_src, rs_x0); // Get obj
Matteo Franchin43ec8732014-03-31 15:00:14 +0100200 LockCallTemps(); // Prepare for explicit register usage
201 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
202 if (kArchVariantHasGoodBranchPredictor) {
203 LIR* null_check_branch = nullptr;
204 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
205 null_check_branch = nullptr; // No null check.
206 } else {
207 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
208 if (Runtime::Current()->ExplicitNullChecks()) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100209 null_check_branch = OpCmpImmBranch(kCondEq, rs_x0, 0, NULL);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100210 }
211 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100212 Load32Disp(rs_rA64_SELF, A64_THREAD_THIN_LOCK_ID_OFFSET, rs_x2);
213 NewLIR3(kA64Ldxr2rX, rx1, rx0, mirror::Object::MonitorOffset().Int32Value() >> 2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100214 MarkPossibleNullPointerException(opt_flags);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100215 LIR* not_unlocked_branch = OpCmpImmBranch(kCondNe, rs_x1, 0, NULL);
216 NewLIR4(kA64Stxr3wrX, rx1, rx2, rx0, mirror::Object::MonitorOffset().Int32Value() >> 2);
217 LIR* lock_success_branch = OpCmpImmBranch(kCondEq, rs_x1, 0, NULL);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100218
219
220 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
221 not_unlocked_branch->target = slow_path_target;
222 if (null_check_branch != nullptr) {
223 null_check_branch->target = slow_path_target;
224 }
225 // TODO: move to a slow path.
226 // Go expensive route - artLockObjectFromCode(obj);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100227 LoadWordDisp(rs_rA64_SELF, A64_QUICK_ENTRYPOINT_INT_OFFS(pLockObject), rs_rA64_LR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100228 ClobberCallerSave();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100229 LIR* call_inst = OpReg(kOpBlx, rs_rA64_LR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100230 MarkSafepointPC(call_inst);
231
232 LIR* success_target = NewLIR0(kPseudoTargetLabel);
233 lock_success_branch->target = success_target;
234 GenMemBarrier(kLoadLoad);
235 } else {
236 // Explicit null-check as slow-path is entered using an IT.
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100237 GenNullCheck(rs_x0, opt_flags);
238 Load32Disp(rs_rA64_SELF, A64_THREAD_THIN_LOCK_ID_OFFSET, rs_x2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100239 MarkPossibleNullPointerException(opt_flags);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100240 NewLIR3(kA64Ldxr2rX, rx1, rx0, mirror::Object::MonitorOffset().Int32Value() >> 2);
241 OpRegImm(kOpCmp, rs_x1, 0);
242 OpIT(kCondEq, "");
243 NewLIR4(kA64Stxr3wrX/*eq*/, rx1, rx2, rx0, mirror::Object::MonitorOffset().Int32Value() >> 2);
244 OpRegImm(kOpCmp, rs_x1, 0);
245 OpIT(kCondNe, "T");
Matteo Franchin43ec8732014-03-31 15:00:14 +0100246 // Go expensive route - artLockObjectFromCode(self, obj);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100247 LoadWordDisp/*ne*/(rs_rA64_SELF, A64_QUICK_ENTRYPOINT_INT_OFFS(pLockObject), rs_rA64_LR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100248 ClobberCallerSave();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100249 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rA64_LR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100250 MarkSafepointPC(call_inst);
251 GenMemBarrier(kLoadLoad);
252 }
253}
254
255/*
256 * Handle thin locked -> unlocked transition inline or else call out to quick entrypoint. For more
257 * details see monitor.cc. Note the code below doesn't use ldrex/strex as the code holds the lock
258 * and can only give away ownership if its suspended.
259 */
260void Arm64Mir2Lir::GenMonitorExit(int opt_flags, RegLocation rl_src) {
261 FlushAllRegs();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100262 LoadValueDirectFixed(rl_src, rs_x0); // Get obj
Matteo Franchin43ec8732014-03-31 15:00:14 +0100263 LockCallTemps(); // Prepare for explicit register usage
264 LIR* null_check_branch = nullptr;
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100265 Load32Disp(rs_rA64_SELF, A64_THREAD_THIN_LOCK_ID_OFFSET, rs_x2);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100266 constexpr bool kArchVariantHasGoodBranchPredictor = false; // TODO: true if cortex-A15.
267 if (kArchVariantHasGoodBranchPredictor) {
268 if ((opt_flags & MIR_IGNORE_NULL_CHECK) && !(cu_->disable_opt & (1 << kNullCheckElimination))) {
269 null_check_branch = nullptr; // No null check.
270 } else {
271 // If the null-check fails its handled by the slow-path to reduce exception related meta-data.
272 if (Runtime::Current()->ExplicitNullChecks()) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100273 null_check_branch = OpCmpImmBranch(kCondEq, rs_x0, 0, NULL);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100274 }
275 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100276 Load32Disp(rs_x0, mirror::Object::MonitorOffset().Int32Value(), rs_x1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100277 MarkPossibleNullPointerException(opt_flags);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100278 LoadConstantNoClobber(rs_x3, 0);
279 LIR* slow_unlock_branch = OpCmpBranch(kCondNe, rs_x1, rs_x2, NULL);
280 Store32Disp(rs_x0, mirror::Object::MonitorOffset().Int32Value(), rs_x3);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100281 LIR* unlock_success_branch = OpUnconditionalBranch(NULL);
282
283 LIR* slow_path_target = NewLIR0(kPseudoTargetLabel);
284 slow_unlock_branch->target = slow_path_target;
285 if (null_check_branch != nullptr) {
286 null_check_branch->target = slow_path_target;
287 }
288 // TODO: move to a slow path.
289 // Go expensive route - artUnlockObjectFromCode(obj);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100290 LoadWordDisp(rs_rA64_SELF, A64_QUICK_ENTRYPOINT_INT_OFFS(pUnlockObject), rs_rA64_LR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100291 ClobberCallerSave();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100292 LIR* call_inst = OpReg(kOpBlx, rs_rA64_LR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100293 MarkSafepointPC(call_inst);
294
295 LIR* success_target = NewLIR0(kPseudoTargetLabel);
296 unlock_success_branch->target = success_target;
297 GenMemBarrier(kStoreLoad);
298 } else {
299 // Explicit null-check as slow-path is entered using an IT.
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100300 GenNullCheck(rs_x0, opt_flags);
301 Load32Disp(rs_x0, mirror::Object::MonitorOffset().Int32Value(), rs_x1); // Get lock
Matteo Franchin43ec8732014-03-31 15:00:14 +0100302 MarkPossibleNullPointerException(opt_flags);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100303 Load32Disp(rs_rA64_SELF, Thread::ThinLockIdOffset<4>().Int32Value(), rs_x2);
304 LoadConstantNoClobber(rs_x3, 0);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100305 // Is lock unheld on lock or held by us (==thread_id) on unlock?
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100306 OpRegReg(kOpCmp, rs_x1, rs_x2);
307 OpIT(kCondEq, "EE");
308 Store32Disp/*eq*/(rs_x0, mirror::Object::MonitorOffset().Int32Value(), rs_x3);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100309 // Go expensive route - UnlockObjectFromCode(obj);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100310 LoadWordDisp/*ne*/(rs_rA64_SELF, A64_QUICK_ENTRYPOINT_INT_OFFS(pUnlockObject), rs_rA64_LR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100311 ClobberCallerSave();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100312 LIR* call_inst = OpReg(kOpBlx/*ne*/, rs_rA64_LR);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100313 MarkSafepointPC(call_inst);
314 GenMemBarrier(kStoreLoad);
315 }
316}
317
318void Arm64Mir2Lir::GenMoveException(RegLocation rl_dest) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100319 int ex_offset = A64_THREAD_EXCEPTION_INT_OFFS;
Matteo Franchin43ec8732014-03-31 15:00:14 +0100320 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
321 RegStorage reset_reg = AllocTemp();
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100322 Load32Disp(rs_rA64_SELF, ex_offset, rl_result.reg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100323 LoadConstant(reset_reg, 0);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100324 Store32Disp(rs_rA64_SELF, ex_offset, reset_reg);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100325 FreeTemp(reset_reg);
326 StoreValue(rl_dest, rl_result);
327}
328
329/*
330 * Mark garbage collection card. Skip if the value we're storing is null.
331 */
332void Arm64Mir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) {
333 RegStorage reg_card_base = AllocTemp();
334 RegStorage reg_card_no = AllocTemp();
335 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100336 LoadWordDisp(rs_rA64_SELF, A64_THREAD_CARD_TABLE_INT_OFFS, reg_card_base);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100337 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
338 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
339 LIR* target = NewLIR0(kPseudoTargetLabel);
340 branch_over->target = target;
341 FreeTemp(reg_card_base);
342 FreeTemp(reg_card_no);
343}
344
345void Arm64Mir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100346 /*
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100347 * On entry, x0, x1, x2 & x3 are live. Let the register allocation
Matteo Franchin43ec8732014-03-31 15:00:14 +0100348 * mechanism know so it doesn't try to use any of them when
349 * expanding the frame or flushing. This leaves the utility
350 * code with a single temp: r12. This should be enough.
351 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100352 LockTemp(rs_x0);
353 LockTemp(rs_x1);
354 LockTemp(rs_x2);
355 LockTemp(rs_x3);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100356
357 /*
358 * We can safely skip the stack overflow check if we're
359 * a leaf *and* our frame size < fudge factor.
360 */
361 bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
362 (static_cast<size_t>(frame_size_) <
363 Thread::kStackOverflowReservedBytes));
364 NewLIR0(kPseudoMethodEntry);
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100365
Matteo Franchin43ec8732014-03-31 15:00:14 +0100366 if (!skip_overflow_check) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100367 LoadWordDisp(rs_rA64_SELF, A64_THREAD_STACK_END_INT_OFFS, rs_x12);
368 OpRegImm64(kOpSub, rs_rA64_SP, frame_size_, /*is_wide*/true);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100369 if (Runtime::Current()->ExplicitStackOverflowChecks()) {
370 /* Load stack limit */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100371 // TODO(Arm64): fix the line below:
372 // GenRegRegCheck(kCondUlt, rA64_SP, r12, kThrowStackOverflow);
373 } else {
374 // Implicit stack overflow check.
375 // Generate a load from [sp, #-framesize]. If this is in the stack
376 // redzone we will get a segmentation fault.
377 // TODO(Arm64): does the following really work or do we need a reg != rA64_ZR?
378 Load32Disp(rs_rA64_SP, 0, rs_wzr);
379 MarkPossibleStackOverflowException();
Matteo Franchin43ec8732014-03-31 15:00:14 +0100380 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100381 } else if (frame_size_ > 0) {
382 OpRegImm64(kOpSub, rs_rA64_SP, frame_size_, /*is_wide*/true);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100383 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100384
Matteo Franchin43ec8732014-03-31 15:00:14 +0100385 /* Spill core callee saves */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100386 if (core_spill_mask_) {
387 SpillCoreRegs(rs_rA64_SP, frame_size_, core_spill_mask_);
388 }
Matteo Franchin43ec8732014-03-31 15:00:14 +0100389 /* 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 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100396 // TODO(Arm64): SpillFPRegs(rA64_SP, frame_size_, core_spill_mask_);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100397 }
398
399 FlushIns(ArgLocs, rl_method);
400
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100401 FreeTemp(rs_x0);
402 FreeTemp(rs_x1);
403 FreeTemp(rs_x2);
404 FreeTemp(rs_x3);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100405}
406
407void Arm64Mir2Lir::GenExitSequence() {
Matteo Franchin43ec8732014-03-31 15:00:14 +0100408 /*
409 * In the exit path, r0/r1 are live - make sure they aren't
410 * allocated by the register utilities as temps.
411 */
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100412 LockTemp(rs_x0);
413 LockTemp(rs_x1);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100414
415 NewLIR0(kPseudoMethodExit);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100416 /* Need to restore any FP callee saves? */
417 if (num_fp_spills_) {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100418 // TODO(Arm64): UnspillFPRegs(num_fp_spills_);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100419 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100420 if (core_spill_mask_) {
421 UnSpillCoreRegs(rs_rA64_SP, frame_size_, core_spill_mask_);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100422 }
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100423
424 OpRegImm64(kOpAdd, rs_rA64_SP, frame_size_, /*is_wide*/true);
425 NewLIR0(kA64Ret);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100426}
427
428void Arm64Mir2Lir::GenSpecialExitSequence() {
Matteo Franchine45fb9e2014-05-06 10:10:30 +0100429 NewLIR0(kA64Ret);
Matteo Franchin43ec8732014-03-31 15:00:14 +0100430}
431
432} // namespace art