blob: 8863c058a16787296e73ac51d4d3eccec21da765 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 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 Mips ISA */
18
19#include "codegen_mips.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080020
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080022#include "base/logging.h"
23#include "dex/mir_graph.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080024#include "dex/quick/dex_file_to_method_inliner_map.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "dex/quick/mir_to_lir-inl.h"
Jeff Hao848f70a2014-01-15 13:49:50 -080026#include "driver/compiler_driver.h"
Douglas Leung22bb5a22015-07-02 16:42:08 -070027#include "driver/compiler_options.h"
Ian Rogers166db042013-07-26 12:05:57 -070028#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070029#include "gc/accounting/card_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070030#include "mips_lir.h"
Andreas Gamped500b532015-01-16 22:09:55 -080031#include "mirror/object_array-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070032
33namespace art {
34
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070035bool MipsMir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, const InlineMethod& special) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080036 // TODO
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070037 UNUSED(bb, mir, special);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080038 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -070039}
40
41/*
42 * The lack of pc-relative loads on Mips presents somewhat of a challenge
43 * for our PIC switch table strategy. To materialize the current location
buzbee2700f7e2014-03-07 09:46:20 -080044 * we'll do a dummy JAL and reference our tables using rRA as the
45 * base register. Note that rRA will be used both as the base to
Brian Carlstrom7940e442013-07-12 13:46:57 -070046 * locate the switch table data and as the reference base for the switch
47 * target offsets stored in the table. We'll use a special pseudo-instruction
48 * to represent the jal and trigger the construction of the
49 * switch table offsets (which will happen after final assembly and all
50 * labels are fixed).
51 *
52 * The test loop will look something like:
53 *
buzbee2700f7e2014-03-07 09:46:20 -080054 * ori r_end, rZERO, #table_size ; size in bytes
55 * jal BaseLabel ; stores "return address" (BaseLabel) in rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 * nop ; opportunistically fill
57 * BaseLabel:
buzbee2700f7e2014-03-07 09:46:20 -080058 * addiu r_base, rRA, <table> - <BaseLabel> ; table relative to BaseLabel
59 addu r_end, r_end, r_base ; end of table
Brian Carlstrom7940e442013-07-12 13:46:57 -070060 * lw r_val, [rSP, v_reg_off] ; Test Value
61 * loop:
buzbee2700f7e2014-03-07 09:46:20 -080062 * beq r_base, r_end, done
63 * lw r_key, 0(r_base)
64 * addu r_base, 8
Brian Carlstrom7940e442013-07-12 13:46:57 -070065 * bne r_val, r_key, loop
buzbee2700f7e2014-03-07 09:46:20 -080066 * lw r_disp, -4(r_base)
67 * addu rRA, r_disp
Andreas Gampe8d365912015-01-13 11:32:32 -080068 * jalr rZERO, rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -070069 * done:
70 *
71 */
Andreas Gampe48971b32014-08-06 10:09:01 -070072void MipsMir2Lir::GenLargeSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070073 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Goran Jakovljevic10957932015-03-24 18:42:56 +010074 // Add the table to the list - we'll process it later.
buzbee0d829482013-10-11 15:24:55 -070075 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000076 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -080077 tab_rec->switch_mir = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -070078 tab_rec->table = table;
79 tab_rec->vaddr = current_dalvik_offset_;
80 int elements = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +010081 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070082
Goran Jakovljevic10957932015-03-24 18:42:56 +010083 // The table is composed of 8-byte key/disp pairs.
Brian Carlstrom7940e442013-07-12 13:46:57 -070084 int byte_size = elements * 8;
85
86 int size_hi = byte_size >> 16;
87 int size_lo = byte_size & 0xffff;
88
Goran Jakovljevic10957932015-03-24 18:42:56 +010089 RegStorage r_end = AllocPtrSizeTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070090 if (size_hi) {
buzbee2700f7e2014-03-07 09:46:20 -080091 NewLIR2(kMipsLui, r_end.GetReg(), size_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 }
Goran Jakovljevic10957932015-03-24 18:42:56 +010093 // Must prevent code motion for the curr pc pair.
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 GenBarrier(); // Scheduling barrier
Goran Jakovljevic10957932015-03-24 18:42:56 +010095 NewLIR0(kMipsCurrPC); // Really a jal to .+8.
96 // Now, fill the branch delay slot.
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 if (size_hi) {
buzbee2700f7e2014-03-07 09:46:20 -080098 NewLIR3(kMipsOri, r_end.GetReg(), r_end.GetReg(), size_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -070099 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800100 NewLIR3(kMipsOri, r_end.GetReg(), rZERO, size_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700101 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100102 GenBarrier(); // Scheduling barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103
Goran Jakovljevic10957932015-03-24 18:42:56 +0100104 // Construct BaseLabel and set up table base register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700105 LIR* base_label = NewLIR0(kPseudoTargetLabel);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100106 // Remember base label so offsets can be computed later.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 tab_rec->anchor = base_label;
Goran Jakovljevic10957932015-03-24 18:42:56 +0100108 RegStorage r_base = AllocPtrSizeTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800109 NewLIR4(kMipsDelta, r_base.GetReg(), 0, WrapPointer(base_label), WrapPointer(tab_rec));
110 OpRegRegReg(kOpAdd, r_end, r_end, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111
Goran Jakovljevic10957932015-03-24 18:42:56 +0100112 // Grab switch test value.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 rl_src = LoadValue(rl_src, kCoreReg);
114
Goran Jakovljevic10957932015-03-24 18:42:56 +0100115 // Test loop.
buzbee2700f7e2014-03-07 09:46:20 -0800116 RegStorage r_key = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700117 LIR* loop_label = NewLIR0(kPseudoTargetLabel);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700118 LIR* exit_branch = OpCmpBranch(kCondEq, r_base, r_end, nullptr);
buzbee695d13a2014-04-19 13:32:20 -0700119 Load32Disp(r_base, 0, r_key);
buzbee2700f7e2014-03-07 09:46:20 -0800120 OpRegImm(kOpAdd, r_base, 8);
121 OpCmpBranch(kCondNe, rl_src.reg, r_key, loop_label);
122 RegStorage r_disp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700123 Load32Disp(r_base, -4, r_disp);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100124 const RegStorage rs_ra = TargetPtrReg(kLr);
125 OpRegRegReg(kOpAdd, rs_ra, rs_ra, r_disp);
126 OpReg(kOpBx, rs_ra);
127 // Loop exit.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 LIR* exit_label = NewLIR0(kPseudoTargetLabel);
129 exit_branch->target = exit_label;
130}
131
132/*
133 * Code pattern will look something like:
134 *
135 * lw r_val
buzbee2700f7e2014-03-07 09:46:20 -0800136 * jal BaseLabel ; stores "return address" (BaseLabel) in rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -0700137 * nop ; opportunistically fill
138 * [subiu r_val, bias] ; Remove bias if low_val != 0
139 * bound check -> done
buzbee2700f7e2014-03-07 09:46:20 -0800140 * lw r_disp, [rRA, r_val]
141 * addu rRA, r_disp
Andreas Gampe8d365912015-01-13 11:32:32 -0800142 * jalr rZERO, rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 * done:
144 */
Andreas Gampe48971b32014-08-06 10:09:01 -0700145void MipsMir2Lir::GenLargePackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700146 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100147 // Add the table to the list - we'll process it later.
buzbee0d829482013-10-11 15:24:55 -0700148 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000149 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800150 tab_rec->switch_mir = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700151 tab_rec->table = table;
152 tab_rec->vaddr = current_dalvik_offset_;
153 int size = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100154 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155
Goran Jakovljevic10957932015-03-24 18:42:56 +0100156 // Get the switch value.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 rl_src = LoadValue(rl_src, kCoreReg);
158
Goran Jakovljevic10957932015-03-24 18:42:56 +0100159 // Prepare the bias. If too big, handle 1st stage here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 int low_key = s4FromSwitchData(&table[2]);
161 bool large_bias = false;
buzbee2700f7e2014-03-07 09:46:20 -0800162 RegStorage r_key;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800164 r_key = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700165 } else if ((low_key & 0xffff) != low_key) {
166 r_key = AllocTemp();
167 LoadConstant(r_key, low_key);
168 large_bias = true;
169 } else {
170 r_key = AllocTemp();
171 }
172
Goran Jakovljevic10957932015-03-24 18:42:56 +0100173 // Must prevent code motion for the curr pc pair.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 GenBarrier();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100175 NewLIR0(kMipsCurrPC); // Really a jal to .+8.
176 // Now, fill the branch delay slot with bias strip.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700177 if (low_key == 0) {
178 NewLIR0(kMipsNop);
179 } else {
180 if (large_bias) {
buzbee2700f7e2014-03-07 09:46:20 -0800181 OpRegRegReg(kOpSub, r_key, rl_src.reg, r_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700182 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800183 OpRegRegImm(kOpSub, r_key, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184 }
185 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100186 GenBarrier(); // Scheduling barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187
Goran Jakovljevic10957932015-03-24 18:42:56 +0100188 // Construct BaseLabel and set up table base register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700189 LIR* base_label = NewLIR0(kPseudoTargetLabel);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100190 // Remember base label so offsets can be computed later.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 tab_rec->anchor = base_label;
192
Goran Jakovljevic10957932015-03-24 18:42:56 +0100193 // Bounds check - if < 0 or >= size continue following switch.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700194 LIR* branch_over = OpCmpImmBranch(kCondHi, r_key, size-1, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700195
Goran Jakovljevic10957932015-03-24 18:42:56 +0100196 // Materialize the table base pointer.
197 RegStorage r_base = AllocPtrSizeTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800198 NewLIR4(kMipsDelta, r_base.GetReg(), 0, WrapPointer(base_label), WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199
Goran Jakovljevic10957932015-03-24 18:42:56 +0100200 // Load the displacement from the switch table.
buzbee2700f7e2014-03-07 09:46:20 -0800201 RegStorage r_disp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700202 LoadBaseIndexed(r_base, r_key, r_disp, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700203
Goran Jakovljevic10957932015-03-24 18:42:56 +0100204 // Add to rRA and go.
205 const RegStorage rs_ra = TargetPtrReg(kLr);
206 OpRegRegReg(kOpAdd, rs_ra, rs_ra, r_disp);
207 OpReg(kOpBx, rs_ra);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700208
Goran Jakovljevic10957932015-03-24 18:42:56 +0100209 // Branch_over target here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700210 LIR* target = NewLIR0(kPseudoTargetLabel);
211 branch_over->target = target;
212}
213
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700214void MipsMir2Lir::GenMoveException(RegLocation rl_dest) {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100215 int ex_offset = cu_->target64 ? Thread::ExceptionOffset<8>().Int32Value() :
216 Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700217 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
218 RegStorage reset_reg = AllocTempRef();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100219 LoadRefDisp(TargetPtrReg(kSelf), ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 LoadConstant(reset_reg, 0);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100221 StoreRefDisp(TargetPtrReg(kSelf), ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700222 FreeTemp(reset_reg);
223 StoreValue(rl_dest, rl_result);
224}
225
Vladimir Markobf535be2014-11-19 18:52:35 +0000226void MipsMir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100227 RegStorage reg_card_base = AllocPtrSizeTemp();
228 RegStorage reg_card_no = AllocPtrSizeTemp();
229 if (cu_->target64) {
230 // NOTE: native pointer.
231 LoadWordDisp(TargetPtrReg(kSelf), Thread::CardTableOffset<8>().Int32Value(), reg_card_base);
232 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
233 StoreBaseIndexed(reg_card_base, reg_card_no, As32BitReg(reg_card_base), 0, kUnsignedByte);
234 } else {
235 // NOTE: native pointer.
236 LoadWordDisp(TargetPtrReg(kSelf), Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
237 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
238 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
239 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240 FreeTemp(reg_card_base);
241 FreeTemp(reg_card_no);
242}
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700243
David Srbecky1109fb32015-04-07 20:21:06 +0100244static dwarf::Reg DwarfCoreReg(int num) {
245 return dwarf::Reg::MipsCore(num);
246}
247
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700248void MipsMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
David Srbecky1109fb32015-04-07 20:21:06 +0100249 DCHECK_EQ(cfi_.GetCurrentCFAOffset(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700250 int spill_count = num_core_spills_ + num_fp_spills_;
251 /*
Goran Jakovljevic10957932015-03-24 18:42:56 +0100252 * On entry, A0, A1, A2 & A3 are live. On Mips64, A4, A5, A6 & A7 are also live.
253 * Let the register allocation mechanism know so it doesn't try to use any of them when
254 * expanding the frame or flushing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700255 */
Goran Jakovljevic10957932015-03-24 18:42:56 +0100256 const RegStorage arg0 = TargetReg(kArg0);
257 const RegStorage arg1 = TargetReg(kArg1);
258 const RegStorage arg2 = TargetReg(kArg2);
259 const RegStorage arg3 = TargetReg(kArg3);
260 const RegStorage arg4 = TargetReg(kArg4);
261 const RegStorage arg5 = TargetReg(kArg5);
262 const RegStorage arg6 = TargetReg(kArg6);
263 const RegStorage arg7 = TargetReg(kArg7);
264
265 LockTemp(arg0);
266 LockTemp(arg1);
267 LockTemp(arg2);
268 LockTemp(arg3);
269 if (cu_->target64) {
270 LockTemp(arg4);
271 LockTemp(arg5);
272 LockTemp(arg6);
273 LockTemp(arg7);
274 }
275
276 bool skip_overflow_check;
277 InstructionSet target = (cu_->target64) ? kMips64 : kMips;
278 int ptr_size = cu_->target64 ? 8 : 4;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279
280 /*
281 * We can safely skip the stack overflow check if we're
282 * a leaf *and* our frame size < fudge factor.
283 */
Goran Jakovljevic10957932015-03-24 18:42:56 +0100284
285 skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, target);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100286 RegStorage check_reg = AllocPtrSizeTemp();
287 RegStorage new_sp = AllocPtrSizeTemp();
288 const RegStorage rs_sp = TargetPtrReg(kSp);
Douglas Leung22bb5a22015-07-02 16:42:08 -0700289 const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(target);
290 const bool large_frame = static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes;
291 bool generate_explicit_stack_overflow_check = large_frame ||
292 !cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks();
293
Brian Carlstrom7940e442013-07-12 13:46:57 -0700294 if (!skip_overflow_check) {
Douglas Leung22bb5a22015-07-02 16:42:08 -0700295 if (generate_explicit_stack_overflow_check) {
296 // Load stack limit.
297 if (cu_->target64) {
298 LoadWordDisp(TargetPtrReg(kSelf), Thread::StackEndOffset<8>().Int32Value(), check_reg);
299 } else {
300 Load32Disp(TargetPtrReg(kSelf), Thread::StackEndOffset<4>().Int32Value(), check_reg);
301 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100302 } else {
Douglas Leung22bb5a22015-07-02 16:42:08 -0700303 // Implicit stack overflow check.
304 // Generate a load from [sp, #-overflowsize]. If this is in the stack
305 // redzone we will get a segmentation fault.
306 Load32Disp(rs_sp, -kStackOverflowReservedUsableBytes, rs_rZERO);
307 MarkPossibleStackOverflowException();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100308 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700309 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100310 // Spill core callee saves.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 SpillCoreRegs();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100312 // NOTE: promotion of FP regs currently unsupported, thus no FP spill.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700313 DCHECK_EQ(num_fp_spills_, 0);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100314 const int frame_sub = frame_size_ - spill_count * ptr_size;
Douglas Leung22bb5a22015-07-02 16:42:08 -0700315 if (!skip_overflow_check && generate_explicit_stack_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700316 class StackOverflowSlowPath : public LIRSlowPath {
317 public:
318 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000319 : LIRSlowPath(m2l, branch), sp_displace_(sp_displace) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700320 }
321 void Compile() OVERRIDE {
322 m2l_->ResetRegPool();
323 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700324 GenerateTargetLabel(kPseudoThrowTarget);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100325 // RA is offset 0 since we push in reverse order.
326 m2l_->LoadWordDisp(m2l_->TargetPtrReg(kSp), 0, m2l_->TargetPtrReg(kLr));
327 m2l_->OpRegImm(kOpAdd, m2l_->TargetPtrReg(kSp), sp_displace_);
David Srbecky1109fb32015-04-07 20:21:06 +0100328 m2l_->cfi().AdjustCFAOffset(-sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700329 m2l_->ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700330 RegStorage r_tgt = m2l_->CallHelperSetup(kQuickThrowStackOverflow); // Doesn't clobber LR.
331 m2l_->CallHelper(r_tgt, kQuickThrowStackOverflow, false /* MarkSafepointPC */,
332 false /* UseLink */);
David Srbecky1109fb32015-04-07 20:21:06 +0100333 m2l_->cfi().AdjustCFAOffset(sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700334 }
335
336 private:
337 const size_t sp_displace_;
338 };
Goran Jakovljevic10957932015-03-24 18:42:56 +0100339 OpRegRegImm(kOpSub, new_sp, rs_sp, frame_sub);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700340 LIR* branch = OpCmpBranch(kCondUlt, new_sp, check_reg, nullptr);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100341 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, spill_count * ptr_size));
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700342 // TODO: avoid copy for small frame sizes.
Goran Jakovljevic10957932015-03-24 18:42:56 +0100343 OpRegCopy(rs_sp, new_sp); // Establish stack.
David Srbecky1109fb32015-04-07 20:21:06 +0100344 cfi_.AdjustCFAOffset(frame_sub);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700345 } else {
Douglas Leung22bb5a22015-07-02 16:42:08 -0700346 // Here if skip_overflow_check or doing implicit stack overflow check.
347 // Just make room on the stack for the frame now.
Goran Jakovljevic10957932015-03-24 18:42:56 +0100348 OpRegImm(kOpSub, rs_sp, frame_sub);
David Srbecky1109fb32015-04-07 20:21:06 +0100349 cfi_.AdjustCFAOffset(frame_sub);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 }
351
352 FlushIns(ArgLocs, rl_method);
353
Goran Jakovljevic10957932015-03-24 18:42:56 +0100354 FreeTemp(arg0);
355 FreeTemp(arg1);
356 FreeTemp(arg2);
357 FreeTemp(arg3);
358 if (cu_->target64) {
359 FreeTemp(arg4);
360 FreeTemp(arg5);
361 FreeTemp(arg6);
362 FreeTemp(arg7);
363 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700364}
365
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700366void MipsMir2Lir::GenExitSequence() {
David Srbecky1109fb32015-04-07 20:21:06 +0100367 cfi_.RememberState();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 /*
369 * In the exit path, rMIPS_RET0/rMIPS_RET1 are live - make sure they aren't
370 * allocated by the register utilities as temps.
371 */
Goran Jakovljevic10957932015-03-24 18:42:56 +0100372 LockTemp(TargetPtrReg(kRet0));
373 LockTemp(TargetPtrReg(kRet1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 UnSpillCoreRegs();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100376 OpReg(kOpBx, TargetPtrReg(kLr));
David Srbecky1109fb32015-04-07 20:21:06 +0100377 // The CFI should be restored for any code that follows the exit block.
378 cfi_.RestoreState();
379 cfi_.DefCFAOffset(frame_size_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700380}
381
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800382void MipsMir2Lir::GenSpecialExitSequence() {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100383 OpReg(kOpBx, TargetPtrReg(kLr));
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800384}
385
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000386void MipsMir2Lir::GenSpecialEntryForSuspend() {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100387 // Keep 16-byte stack alignment - push A0, i.e. ArtMethod*, 2 filler words and RA for mips32,
388 // but A0 and RA for mips64.
389 core_spill_mask_ = (1u << TargetPtrReg(kLr).GetRegNum());
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000390 num_core_spills_ = 1u;
391 fp_spill_mask_ = 0u;
392 num_fp_spills_ = 0u;
393 frame_size_ = 16u;
394 core_vmap_table_.clear();
395 fp_vmap_table_.clear();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100396 const RegStorage rs_sp = TargetPtrReg(kSp);
397 OpRegImm(kOpSub, rs_sp, frame_size_);
David Srbecky1109fb32015-04-07 20:21:06 +0100398 cfi_.AdjustCFAOffset(frame_size_);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100399 StoreWordDisp(rs_sp, frame_size_ - (cu_->target64 ? 8 : 4), TargetPtrReg(kLr));
David Srbecky1109fb32015-04-07 20:21:06 +0100400 cfi_.RelOffset(DwarfCoreReg(rRA), frame_size_ - (cu_->target64 ? 8 : 4));
Goran Jakovljevic10957932015-03-24 18:42:56 +0100401 StoreWordDisp(rs_sp, 0, TargetPtrReg(kArg0));
David Srbecky1109fb32015-04-07 20:21:06 +0100402 // Do not generate CFI for scratch register A0.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000403}
404
405void MipsMir2Lir::GenSpecialExitForSuspend() {
406 // Pop the frame. Don't pop ArtMethod*, it's no longer needed.
Goran Jakovljevic10957932015-03-24 18:42:56 +0100407 const RegStorage rs_sp = TargetPtrReg(kSp);
408 LoadWordDisp(rs_sp, frame_size_ - (cu_->target64 ? 8 : 4), TargetPtrReg(kLr));
David Srbecky1109fb32015-04-07 20:21:06 +0100409 cfi_.Restore(DwarfCoreReg(rRA));
Goran Jakovljevic10957932015-03-24 18:42:56 +0100410 OpRegImm(kOpAdd, rs_sp, frame_size_);
David Srbecky1109fb32015-04-07 20:21:06 +0100411 cfi_.AdjustCFAOffset(-frame_size_);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000412}
413
Andreas Gamped500b532015-01-16 22:09:55 -0800414/*
415 * Bit of a hack here - in the absence of a real scheduling pass,
416 * emit the next instruction in static & direct invoke sequences.
417 */
Vladimir Marko05792b92015-08-03 11:56:49 +0100418int MipsMir2Lir::MipsNextSDCallInsn(CompilationUnit* cu, CallInfo* info, int state,
419 const MethodReference& target_method, uint32_t,
420 uintptr_t direct_code, uintptr_t direct_method,
421 InvokeType type) {
422 MipsMir2Lir* cg = static_cast<MipsMir2Lir*>(cu->cg.get());
Jeff Hao848f70a2014-01-15 13:49:50 -0800423 if (info->string_init_offset != 0) {
424 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
425 switch (state) {
426 case 0: { // Grab target method* from thread pointer
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), info->string_init_offset, arg0_ref);
Jeff Hao848f70a2014-01-15 13:49:50 -0800428 break;
429 }
430 case 1: // Grab the code from the method*
431 if (direct_code == 0) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 int32_t offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Jeff Hao848f70a2014-01-15 13:49:50 -0800433 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
434 cg->LoadWordDisp(arg0_ref, offset, cg->TargetPtrReg(kInvokeTgt));
435 }
436 break;
437 default:
438 return -1;
439 }
440 } else if (direct_code != 0 && direct_method != 0) {
Andreas Gamped500b532015-01-16 22:09:55 -0800441 switch (state) {
Andreas Gampe8f486f32015-04-09 15:30:51 -0700442 case 0: // Get the current Method* [sets kArg0]
Andreas Gamped500b532015-01-16 22:09:55 -0800443 if (direct_code != static_cast<uintptr_t>(-1)) {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100444 if (cu->target64) {
445 cg->LoadConstantWide(cg->TargetPtrReg(kInvokeTgt), direct_code);
446 } else {
447 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
448 }
Andreas Gamped500b532015-01-16 22:09:55 -0800449 } else {
Andreas Gamped500b532015-01-16 22:09:55 -0800450 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
451 }
Andreas Gampe8f486f32015-04-09 15:30:51 -0700452 if (direct_method != static_cast<uintptr_t>(-1)) {
453 if (cu->target64) {
454 cg->LoadConstantWide(cg->TargetReg(kArg0, kRef), direct_method);
455 } else {
456 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
457 }
458 } else {
459 cg->LoadMethodAddress(target_method, type, kArg0);
460 }
461 break;
462 default:
463 return -1;
464 }
465 } else {
466 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
467 switch (state) {
468 case 0: // Get the current Method* [sets kArg0]
469 // TUNING: we can save a reg copy if Method* has been promoted.
470 cg->LoadCurrMethodDirect(arg0_ref);
471 break;
472 case 1: // Get method->dex_cache_resolved_methods_
Vladimir Marko05792b92015-08-03 11:56:49 +0100473 cg->LoadBaseDisp(arg0_ref,
474 ArtMethod::DexCacheResolvedMethodsOffset(
475 cu->target64 ? kMips64PointerSize : kMipsPointerSize).Int32Value(),
476 arg0_ref,
477 cu->target64 ? k64 : k32,
478 kNotVolatile);
Andreas Gampe8f486f32015-04-09 15:30:51 -0700479 // Set up direct code if known.
480 if (direct_code != 0) {
481 if (direct_code != static_cast<uintptr_t>(-1)) {
482 if (cu->target64) {
483 cg->LoadConstantWide(cg->TargetPtrReg(kInvokeTgt), direct_code);
484 } else {
485 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
486 }
487 } else {
488 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
489 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
490 }
491 }
492 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700493 case 2: {
494 // Grab target method*
Andreas Gampe8f486f32015-04-09 15:30:51 -0700495 CHECK_EQ(cu->dex_file, target_method.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700496 const size_t pointer_size = GetInstructionSetPointerSize(cu->instruction_set);
497 cg->LoadWordDisp(arg0_ref,
Vladimir Marko05792b92015-08-03 11:56:49 +0100498 cg->GetCachePointerOffset(target_method.dex_method_index,
499 pointer_size),
500 arg0_ref);
Andreas Gampe8f486f32015-04-09 15:30:51 -0700501 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700502 }
Andreas Gampe8f486f32015-04-09 15:30:51 -0700503 case 3: // Grab the code from the method*
504 if (direct_code == 0) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700505 int32_t offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe8f486f32015-04-09 15:30:51 -0700506 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
507 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
508 cg->LoadWordDisp(arg0_ref, offset, cg->TargetPtrReg(kInvokeTgt));
509 }
510 break;
511 default:
512 return -1;
Andreas Gamped500b532015-01-16 22:09:55 -0800513 }
514 }
515 return state + 1;
516}
517
518NextCallInsn MipsMir2Lir::GetNextSDCallInsn() {
Vladimir Marko05792b92015-08-03 11:56:49 +0100519 return MipsNextSDCallInsn;
Andreas Gamped500b532015-01-16 22:09:55 -0800520}
521
522LIR* MipsMir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info ATTRIBUTE_UNUSED) {
523 return OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
524}
525
Brian Carlstrom7940e442013-07-12 13:46:57 -0700526} // namespace art