blob: 4a736f3d933fbce3ec8b95939ce235bc7b9aa68d [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
Roland Levillain4b8f1ec2015-08-26 18:34:03 +010035bool MipsMir2Lir::GenSpecialCase(BasicBlock* bb ATTRIBUTE_UNUSED,
36 MIR* mir ATTRIBUTE_UNUSED,
37 const InlineMethod& special ATTRIBUTE_UNUSED) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080038 // TODO
39 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -070040}
41
42/*
43 * The lack of pc-relative loads on Mips presents somewhat of a challenge
44 * for our PIC switch table strategy. To materialize the current location
buzbee2700f7e2014-03-07 09:46:20 -080045 * we'll do a dummy JAL and reference our tables using rRA as the
46 * base register. Note that rRA will be used both as the base to
Brian Carlstrom7940e442013-07-12 13:46:57 -070047 * locate the switch table data and as the reference base for the switch
48 * target offsets stored in the table. We'll use a special pseudo-instruction
49 * to represent the jal and trigger the construction of the
50 * switch table offsets (which will happen after final assembly and all
51 * labels are fixed).
52 *
53 * The test loop will look something like:
54 *
buzbee2700f7e2014-03-07 09:46:20 -080055 * ori r_end, rZERO, #table_size ; size in bytes
56 * jal BaseLabel ; stores "return address" (BaseLabel) in rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 * nop ; opportunistically fill
58 * BaseLabel:
buzbee2700f7e2014-03-07 09:46:20 -080059 * addiu r_base, rRA, <table> - <BaseLabel> ; table relative to BaseLabel
60 addu r_end, r_end, r_base ; end of table
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 * lw r_val, [rSP, v_reg_off] ; Test Value
62 * loop:
buzbee2700f7e2014-03-07 09:46:20 -080063 * beq r_base, r_end, done
64 * lw r_key, 0(r_base)
65 * addu r_base, 8
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 * bne r_val, r_key, loop
buzbee2700f7e2014-03-07 09:46:20 -080067 * lw r_disp, -4(r_base)
68 * addu rRA, r_disp
Andreas Gampe8d365912015-01-13 11:32:32 -080069 * jalr rZERO, rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 * done:
71 *
72 */
Andreas Gampe48971b32014-08-06 10:09:01 -070073void MipsMir2Lir::GenLargeSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070074 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Goran Jakovljevic10957932015-03-24 18:42:56 +010075 // Add the table to the list - we'll process it later.
buzbee0d829482013-10-11 15:24:55 -070076 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000077 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -080078 tab_rec->switch_mir = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 tab_rec->table = table;
80 tab_rec->vaddr = current_dalvik_offset_;
81 int elements = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +010082 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070083
Goran Jakovljevic10957932015-03-24 18:42:56 +010084 // The table is composed of 8-byte key/disp pairs.
Brian Carlstrom7940e442013-07-12 13:46:57 -070085 int byte_size = elements * 8;
86
87 int size_hi = byte_size >> 16;
88 int size_lo = byte_size & 0xffff;
89
Goran Jakovljevic10957932015-03-24 18:42:56 +010090 RegStorage r_end = AllocPtrSizeTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 if (size_hi) {
buzbee2700f7e2014-03-07 09:46:20 -080092 NewLIR2(kMipsLui, r_end.GetReg(), size_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 }
Goran Jakovljevic10957932015-03-24 18:42:56 +010094 // Must prevent code motion for the curr pc pair.
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 GenBarrier(); // Scheduling barrier
Goran Jakovljevic10957932015-03-24 18:42:56 +010096 NewLIR0(kMipsCurrPC); // Really a jal to .+8.
97 // Now, fill the branch delay slot.
Brian Carlstrom7940e442013-07-12 13:46:57 -070098 if (size_hi) {
buzbee2700f7e2014-03-07 09:46:20 -080099 NewLIR3(kMipsOri, r_end.GetReg(), r_end.GetReg(), size_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800101 NewLIR3(kMipsOri, r_end.GetReg(), rZERO, size_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100103 GenBarrier(); // Scheduling barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104
Goran Jakovljevic10957932015-03-24 18:42:56 +0100105 // Construct BaseLabel and set up table base register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 LIR* base_label = NewLIR0(kPseudoTargetLabel);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100107 // Remember base label so offsets can be computed later.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 tab_rec->anchor = base_label;
Goran Jakovljevic10957932015-03-24 18:42:56 +0100109 RegStorage r_base = AllocPtrSizeTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800110 NewLIR4(kMipsDelta, r_base.GetReg(), 0, WrapPointer(base_label), WrapPointer(tab_rec));
111 OpRegRegReg(kOpAdd, r_end, r_end, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700112
Goran Jakovljevic10957932015-03-24 18:42:56 +0100113 // Grab switch test value.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 rl_src = LoadValue(rl_src, kCoreReg);
115
Goran Jakovljevic10957932015-03-24 18:42:56 +0100116 // Test loop.
buzbee2700f7e2014-03-07 09:46:20 -0800117 RegStorage r_key = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700118 LIR* loop_label = NewLIR0(kPseudoTargetLabel);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700119 LIR* exit_branch = OpCmpBranch(kCondEq, r_base, r_end, nullptr);
buzbee695d13a2014-04-19 13:32:20 -0700120 Load32Disp(r_base, 0, r_key);
buzbee2700f7e2014-03-07 09:46:20 -0800121 OpRegImm(kOpAdd, r_base, 8);
122 OpCmpBranch(kCondNe, rl_src.reg, r_key, loop_label);
123 RegStorage r_disp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700124 Load32Disp(r_base, -4, r_disp);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100125 const RegStorage rs_ra = TargetPtrReg(kLr);
126 OpRegRegReg(kOpAdd, rs_ra, rs_ra, r_disp);
127 OpReg(kOpBx, rs_ra);
128 // Loop exit.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 LIR* exit_label = NewLIR0(kPseudoTargetLabel);
130 exit_branch->target = exit_label;
131}
132
133/*
134 * Code pattern will look something like:
135 *
136 * lw r_val
buzbee2700f7e2014-03-07 09:46:20 -0800137 * jal BaseLabel ; stores "return address" (BaseLabel) in rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -0700138 * nop ; opportunistically fill
139 * [subiu r_val, bias] ; Remove bias if low_val != 0
140 * bound check -> done
buzbee2700f7e2014-03-07 09:46:20 -0800141 * lw r_disp, [rRA, r_val]
142 * addu rRA, r_disp
Andreas Gampe8d365912015-01-13 11:32:32 -0800143 * jalr rZERO, rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -0700144 * done:
145 */
Andreas Gampe48971b32014-08-06 10:09:01 -0700146void MipsMir2Lir::GenLargePackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700147 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100148 // Add the table to the list - we'll process it later.
buzbee0d829482013-10-11 15:24:55 -0700149 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000150 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800151 tab_rec->switch_mir = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152 tab_rec->table = table;
153 tab_rec->vaddr = current_dalvik_offset_;
154 int size = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100155 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700156
Goran Jakovljevic10957932015-03-24 18:42:56 +0100157 // Get the switch value.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700158 rl_src = LoadValue(rl_src, kCoreReg);
159
Goran Jakovljevic10957932015-03-24 18:42:56 +0100160 // Prepare the bias. If too big, handle 1st stage here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700161 int low_key = s4FromSwitchData(&table[2]);
162 bool large_bias = false;
buzbee2700f7e2014-03-07 09:46:20 -0800163 RegStorage r_key;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700164 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800165 r_key = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 } else if ((low_key & 0xffff) != low_key) {
167 r_key = AllocTemp();
168 LoadConstant(r_key, low_key);
169 large_bias = true;
170 } else {
171 r_key = AllocTemp();
172 }
173
Goran Jakovljevic10957932015-03-24 18:42:56 +0100174 // Must prevent code motion for the curr pc pair.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700175 GenBarrier();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100176 NewLIR0(kMipsCurrPC); // Really a jal to .+8.
177 // Now, fill the branch delay slot with bias strip.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700178 if (low_key == 0) {
179 NewLIR0(kMipsNop);
180 } else {
181 if (large_bias) {
buzbee2700f7e2014-03-07 09:46:20 -0800182 OpRegRegReg(kOpSub, r_key, rl_src.reg, r_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700183 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800184 OpRegRegImm(kOpSub, r_key, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700185 }
186 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100187 GenBarrier(); // Scheduling barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188
Goran Jakovljevic10957932015-03-24 18:42:56 +0100189 // Construct BaseLabel and set up table base register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700190 LIR* base_label = NewLIR0(kPseudoTargetLabel);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100191 // Remember base label so offsets can be computed later.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700192 tab_rec->anchor = base_label;
193
Goran Jakovljevic10957932015-03-24 18:42:56 +0100194 // Bounds check - if < 0 or >= size continue following switch.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700195 LIR* branch_over = OpCmpImmBranch(kCondHi, r_key, size-1, nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196
Goran Jakovljevic10957932015-03-24 18:42:56 +0100197 // Materialize the table base pointer.
198 RegStorage r_base = AllocPtrSizeTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800199 NewLIR4(kMipsDelta, r_base.GetReg(), 0, WrapPointer(base_label), WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200
Goran Jakovljevic10957932015-03-24 18:42:56 +0100201 // Load the displacement from the switch table.
buzbee2700f7e2014-03-07 09:46:20 -0800202 RegStorage r_disp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700203 LoadBaseIndexed(r_base, r_key, r_disp, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700204
Goran Jakovljevic10957932015-03-24 18:42:56 +0100205 // Add to rRA and go.
206 const RegStorage rs_ra = TargetPtrReg(kLr);
207 OpRegRegReg(kOpAdd, rs_ra, rs_ra, r_disp);
208 OpReg(kOpBx, rs_ra);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700209
Goran Jakovljevic10957932015-03-24 18:42:56 +0100210 // Branch_over target here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211 LIR* target = NewLIR0(kPseudoTargetLabel);
212 branch_over->target = target;
213}
214
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700215void MipsMir2Lir::GenMoveException(RegLocation rl_dest) {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100216 int ex_offset = cu_->target64 ? Thread::ExceptionOffset<8>().Int32Value() :
217 Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700218 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
219 RegStorage reset_reg = AllocTempRef();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100220 LoadRefDisp(TargetPtrReg(kSelf), ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 LoadConstant(reset_reg, 0);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100222 StoreRefDisp(TargetPtrReg(kSelf), ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 FreeTemp(reset_reg);
224 StoreValue(rl_dest, rl_result);
225}
226
Vladimir Markobf535be2014-11-19 18:52:35 +0000227void MipsMir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100228 RegStorage reg_card_base = AllocPtrSizeTemp();
229 RegStorage reg_card_no = AllocPtrSizeTemp();
230 if (cu_->target64) {
231 // NOTE: native pointer.
232 LoadWordDisp(TargetPtrReg(kSelf), Thread::CardTableOffset<8>().Int32Value(), reg_card_base);
233 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
234 StoreBaseIndexed(reg_card_base, reg_card_no, As32BitReg(reg_card_base), 0, kUnsignedByte);
235 } else {
236 // NOTE: native pointer.
237 LoadWordDisp(TargetPtrReg(kSelf), Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
238 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
239 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
240 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700241 FreeTemp(reg_card_base);
242 FreeTemp(reg_card_no);
243}
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700244
David Srbecky1109fb32015-04-07 20:21:06 +0100245static dwarf::Reg DwarfCoreReg(int num) {
246 return dwarf::Reg::MipsCore(num);
247}
248
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700249void MipsMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
David Srbecky1109fb32015-04-07 20:21:06 +0100250 DCHECK_EQ(cfi_.GetCurrentCFAOffset(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700251 int spill_count = num_core_spills_ + num_fp_spills_;
252 /*
Goran Jakovljevic10957932015-03-24 18:42:56 +0100253 * On entry, A0, A1, A2 & A3 are live. On Mips64, A4, A5, A6 & A7 are also live.
254 * Let the register allocation mechanism know so it doesn't try to use any of them when
255 * expanding the frame or flushing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700256 */
Goran Jakovljevic10957932015-03-24 18:42:56 +0100257 const RegStorage arg0 = TargetReg(kArg0);
258 const RegStorage arg1 = TargetReg(kArg1);
259 const RegStorage arg2 = TargetReg(kArg2);
260 const RegStorage arg3 = TargetReg(kArg3);
261 const RegStorage arg4 = TargetReg(kArg4);
262 const RegStorage arg5 = TargetReg(kArg5);
263 const RegStorage arg6 = TargetReg(kArg6);
264 const RegStorage arg7 = TargetReg(kArg7);
265
266 LockTemp(arg0);
267 LockTemp(arg1);
268 LockTemp(arg2);
269 LockTemp(arg3);
270 if (cu_->target64) {
271 LockTemp(arg4);
272 LockTemp(arg5);
273 LockTemp(arg6);
274 LockTemp(arg7);
275 }
276
277 bool skip_overflow_check;
278 InstructionSet target = (cu_->target64) ? kMips64 : kMips;
279 int ptr_size = cu_->target64 ? 8 : 4;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700280
281 /*
282 * We can safely skip the stack overflow check if we're
283 * a leaf *and* our frame size < fudge factor.
284 */
Goran Jakovljevic10957932015-03-24 18:42:56 +0100285
286 skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, target);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100287 RegStorage check_reg = AllocPtrSizeTemp();
288 RegStorage new_sp = AllocPtrSizeTemp();
289 const RegStorage rs_sp = TargetPtrReg(kSp);
Douglas Leung22bb5a22015-07-02 16:42:08 -0700290 const size_t kStackOverflowReservedUsableBytes = GetStackOverflowReservedBytes(target);
291 const bool large_frame = static_cast<size_t>(frame_size_) > kStackOverflowReservedUsableBytes;
292 bool generate_explicit_stack_overflow_check = large_frame ||
293 !cu_->compiler_driver->GetCompilerOptions().GetImplicitStackOverflowChecks();
294
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 if (!skip_overflow_check) {
Douglas Leung22bb5a22015-07-02 16:42:08 -0700296 if (generate_explicit_stack_overflow_check) {
297 // Load stack limit.
298 if (cu_->target64) {
299 LoadWordDisp(TargetPtrReg(kSelf), Thread::StackEndOffset<8>().Int32Value(), check_reg);
300 } else {
301 Load32Disp(TargetPtrReg(kSelf), Thread::StackEndOffset<4>().Int32Value(), check_reg);
302 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100303 } else {
Douglas Leung22bb5a22015-07-02 16:42:08 -0700304 // Implicit stack overflow check.
305 // Generate a load from [sp, #-overflowsize]. If this is in the stack
306 // redzone we will get a segmentation fault.
307 Load32Disp(rs_sp, -kStackOverflowReservedUsableBytes, rs_rZERO);
308 MarkPossibleStackOverflowException();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100309 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700310 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100311 // Spill core callee saves.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700312 SpillCoreRegs();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100313 // NOTE: promotion of FP regs currently unsupported, thus no FP spill.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 DCHECK_EQ(num_fp_spills_, 0);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100315 const int frame_sub = frame_size_ - spill_count * ptr_size;
Douglas Leung22bb5a22015-07-02 16:42:08 -0700316 if (!skip_overflow_check && generate_explicit_stack_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700317 class StackOverflowSlowPath : public LIRSlowPath {
318 public:
319 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000320 : LIRSlowPath(m2l, branch), sp_displace_(sp_displace) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700321 }
322 void Compile() OVERRIDE {
323 m2l_->ResetRegPool();
324 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700325 GenerateTargetLabel(kPseudoThrowTarget);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100326 // RA is offset 0 since we push in reverse order.
327 m2l_->LoadWordDisp(m2l_->TargetPtrReg(kSp), 0, m2l_->TargetPtrReg(kLr));
328 m2l_->OpRegImm(kOpAdd, m2l_->TargetPtrReg(kSp), sp_displace_);
David Srbecky1109fb32015-04-07 20:21:06 +0100329 m2l_->cfi().AdjustCFAOffset(-sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700330 m2l_->ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700331 RegStorage r_tgt = m2l_->CallHelperSetup(kQuickThrowStackOverflow); // Doesn't clobber LR.
332 m2l_->CallHelper(r_tgt, kQuickThrowStackOverflow, false /* MarkSafepointPC */,
333 false /* UseLink */);
David Srbecky1109fb32015-04-07 20:21:06 +0100334 m2l_->cfi().AdjustCFAOffset(sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700335 }
336
337 private:
338 const size_t sp_displace_;
339 };
Goran Jakovljevic10957932015-03-24 18:42:56 +0100340 OpRegRegImm(kOpSub, new_sp, rs_sp, frame_sub);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700341 LIR* branch = OpCmpBranch(kCondUlt, new_sp, check_reg, nullptr);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100342 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, spill_count * ptr_size));
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700343 // TODO: avoid copy for small frame sizes.
Goran Jakovljevic10957932015-03-24 18:42:56 +0100344 OpRegCopy(rs_sp, new_sp); // Establish stack.
David Srbecky1109fb32015-04-07 20:21:06 +0100345 cfi_.AdjustCFAOffset(frame_sub);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346 } else {
Douglas Leung22bb5a22015-07-02 16:42:08 -0700347 // Here if skip_overflow_check or doing implicit stack overflow check.
348 // Just make room on the stack for the frame now.
Goran Jakovljevic10957932015-03-24 18:42:56 +0100349 OpRegImm(kOpSub, rs_sp, frame_sub);
David Srbecky1109fb32015-04-07 20:21:06 +0100350 cfi_.AdjustCFAOffset(frame_sub);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700351 }
352
353 FlushIns(ArgLocs, rl_method);
354
Goran Jakovljevic10957932015-03-24 18:42:56 +0100355 FreeTemp(arg0);
356 FreeTemp(arg1);
357 FreeTemp(arg2);
358 FreeTemp(arg3);
359 if (cu_->target64) {
360 FreeTemp(arg4);
361 FreeTemp(arg5);
362 FreeTemp(arg6);
363 FreeTemp(arg7);
364 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700365}
366
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700367void MipsMir2Lir::GenExitSequence() {
David Srbecky1109fb32015-04-07 20:21:06 +0100368 cfi_.RememberState();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700369 /*
370 * In the exit path, rMIPS_RET0/rMIPS_RET1 are live - make sure they aren't
371 * allocated by the register utilities as temps.
372 */
Goran Jakovljevic10957932015-03-24 18:42:56 +0100373 LockTemp(TargetPtrReg(kRet0));
374 LockTemp(TargetPtrReg(kRet1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375
Brian Carlstrom7940e442013-07-12 13:46:57 -0700376 UnSpillCoreRegs();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100377 OpReg(kOpBx, TargetPtrReg(kLr));
David Srbecky1109fb32015-04-07 20:21:06 +0100378 // The CFI should be restored for any code that follows the exit block.
379 cfi_.RestoreState();
380 cfi_.DefCFAOffset(frame_size_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700381}
382
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800383void MipsMir2Lir::GenSpecialExitSequence() {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100384 OpReg(kOpBx, TargetPtrReg(kLr));
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800385}
386
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000387void MipsMir2Lir::GenSpecialEntryForSuspend() {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100388 // Keep 16-byte stack alignment - push A0, i.e. ArtMethod*, 2 filler words and RA for mips32,
389 // but A0 and RA for mips64.
390 core_spill_mask_ = (1u << TargetPtrReg(kLr).GetRegNum());
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000391 num_core_spills_ = 1u;
392 fp_spill_mask_ = 0u;
393 num_fp_spills_ = 0u;
394 frame_size_ = 16u;
395 core_vmap_table_.clear();
396 fp_vmap_table_.clear();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100397 const RegStorage rs_sp = TargetPtrReg(kSp);
398 OpRegImm(kOpSub, rs_sp, frame_size_);
David Srbecky1109fb32015-04-07 20:21:06 +0100399 cfi_.AdjustCFAOffset(frame_size_);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100400 StoreWordDisp(rs_sp, frame_size_ - (cu_->target64 ? 8 : 4), TargetPtrReg(kLr));
David Srbecky1109fb32015-04-07 20:21:06 +0100401 cfi_.RelOffset(DwarfCoreReg(rRA), frame_size_ - (cu_->target64 ? 8 : 4));
Goran Jakovljevic10957932015-03-24 18:42:56 +0100402 StoreWordDisp(rs_sp, 0, TargetPtrReg(kArg0));
David Srbecky1109fb32015-04-07 20:21:06 +0100403 // Do not generate CFI for scratch register A0.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000404}
405
406void MipsMir2Lir::GenSpecialExitForSuspend() {
407 // Pop the frame. Don't pop ArtMethod*, it's no longer needed.
Goran Jakovljevic10957932015-03-24 18:42:56 +0100408 const RegStorage rs_sp = TargetPtrReg(kSp);
409 LoadWordDisp(rs_sp, frame_size_ - (cu_->target64 ? 8 : 4), TargetPtrReg(kLr));
David Srbecky1109fb32015-04-07 20:21:06 +0100410 cfi_.Restore(DwarfCoreReg(rRA));
Goran Jakovljevic10957932015-03-24 18:42:56 +0100411 OpRegImm(kOpAdd, rs_sp, frame_size_);
David Srbecky1109fb32015-04-07 20:21:06 +0100412 cfi_.AdjustCFAOffset(-frame_size_);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000413}
414
Andreas Gamped500b532015-01-16 22:09:55 -0800415/*
416 * Bit of a hack here - in the absence of a real scheduling pass,
417 * emit the next instruction in static & direct invoke sequences.
418 */
Vladimir Marko05792b92015-08-03 11:56:49 +0100419int MipsMir2Lir::MipsNextSDCallInsn(CompilationUnit* cu, CallInfo* info, int state,
420 const MethodReference& target_method, uint32_t,
421 uintptr_t direct_code, uintptr_t direct_method,
422 InvokeType type) {
423 MipsMir2Lir* cg = static_cast<MipsMir2Lir*>(cu->cg.get());
Jeff Hao848f70a2014-01-15 13:49:50 -0800424 if (info->string_init_offset != 0) {
425 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
426 switch (state) {
427 case 0: { // Grab target method* from thread pointer
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428 cg->LoadWordDisp(cg->TargetPtrReg(kSelf), info->string_init_offset, arg0_ref);
Jeff Hao848f70a2014-01-15 13:49:50 -0800429 break;
430 }
431 case 1: // Grab the code from the method*
432 if (direct_code == 0) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433 int32_t offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Jeff Hao848f70a2014-01-15 13:49:50 -0800434 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
435 cg->LoadWordDisp(arg0_ref, offset, cg->TargetPtrReg(kInvokeTgt));
436 }
437 break;
438 default:
439 return -1;
440 }
441 } else if (direct_code != 0 && direct_method != 0) {
Andreas Gamped500b532015-01-16 22:09:55 -0800442 switch (state) {
Andreas Gampe8f486f32015-04-09 15:30:51 -0700443 case 0: // Get the current Method* [sets kArg0]
Andreas Gamped500b532015-01-16 22:09:55 -0800444 if (direct_code != static_cast<uintptr_t>(-1)) {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100445 if (cu->target64) {
446 cg->LoadConstantWide(cg->TargetPtrReg(kInvokeTgt), direct_code);
447 } else {
448 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
449 }
Andreas Gamped500b532015-01-16 22:09:55 -0800450 } else {
Andreas Gamped500b532015-01-16 22:09:55 -0800451 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
452 }
Andreas Gampe8f486f32015-04-09 15:30:51 -0700453 if (direct_method != static_cast<uintptr_t>(-1)) {
454 if (cu->target64) {
455 cg->LoadConstantWide(cg->TargetReg(kArg0, kRef), direct_method);
456 } else {
457 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
458 }
459 } else {
460 cg->LoadMethodAddress(target_method, type, kArg0);
461 }
462 break;
463 default:
464 return -1;
465 }
466 } else {
467 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
468 switch (state) {
469 case 0: // Get the current Method* [sets kArg0]
470 // TUNING: we can save a reg copy if Method* has been promoted.
471 cg->LoadCurrMethodDirect(arg0_ref);
472 break;
473 case 1: // Get method->dex_cache_resolved_methods_
Vladimir Marko05792b92015-08-03 11:56:49 +0100474 cg->LoadBaseDisp(arg0_ref,
475 ArtMethod::DexCacheResolvedMethodsOffset(
476 cu->target64 ? kMips64PointerSize : kMipsPointerSize).Int32Value(),
477 arg0_ref,
478 cu->target64 ? k64 : k32,
479 kNotVolatile);
Andreas Gampe8f486f32015-04-09 15:30:51 -0700480 // Set up direct code if known.
481 if (direct_code != 0) {
482 if (direct_code != static_cast<uintptr_t>(-1)) {
483 if (cu->target64) {
484 cg->LoadConstantWide(cg->TargetPtrReg(kInvokeTgt), direct_code);
485 } else {
486 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
487 }
488 } else {
489 CHECK_LT(target_method.dex_method_index, target_method.dex_file->NumMethodIds());
490 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
491 }
492 }
493 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700494 case 2: {
495 // Grab target method*
Andreas Gampe8f486f32015-04-09 15:30:51 -0700496 CHECK_EQ(cu->dex_file, target_method.dex_file);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700497 const size_t pointer_size = GetInstructionSetPointerSize(cu->instruction_set);
498 cg->LoadWordDisp(arg0_ref,
Vladimir Marko05792b92015-08-03 11:56:49 +0100499 cg->GetCachePointerOffset(target_method.dex_method_index,
500 pointer_size),
501 arg0_ref);
Andreas Gampe8f486f32015-04-09 15:30:51 -0700502 break;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700503 }
Andreas Gampe8f486f32015-04-09 15:30:51 -0700504 case 3: // Grab the code from the method*
505 if (direct_code == 0) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700506 int32_t offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe8f486f32015-04-09 15:30:51 -0700507 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
508 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
509 cg->LoadWordDisp(arg0_ref, offset, cg->TargetPtrReg(kInvokeTgt));
510 }
511 break;
512 default:
513 return -1;
Andreas Gamped500b532015-01-16 22:09:55 -0800514 }
515 }
516 return state + 1;
517}
518
519NextCallInsn MipsMir2Lir::GetNextSDCallInsn() {
Vladimir Marko05792b92015-08-03 11:56:49 +0100520 return MipsNextSDCallInsn;
Andreas Gamped500b532015-01-16 22:09:55 -0800521}
522
523LIR* MipsMir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info ATTRIBUTE_UNUSED) {
524 return OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
525}
526
Brian Carlstrom7940e442013-07-12 13:46:57 -0700527} // namespace art