blob: 05570e4bdeebc580bb5262ed2cdbac086b0016a4 [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
Andreas Gampe0b9203e2015-01-22 20:39:27 -080021#include "base/logging.h"
22#include "dex/mir_graph.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070024#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070025#include "gc/accounting/card_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "mips_lir.h"
Andreas Gamped500b532015-01-16 22:09:55 -080027#include "mirror/art_method.h"
28#include "mirror/object_array-inl.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070029
30namespace art {
31
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070032bool MipsMir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, const InlineMethod& special) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080033 // TODO
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070034 UNUSED(bb, mir, special);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080035 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -070036}
37
38/*
39 * The lack of pc-relative loads on Mips presents somewhat of a challenge
40 * for our PIC switch table strategy. To materialize the current location
buzbee2700f7e2014-03-07 09:46:20 -080041 * we'll do a dummy JAL and reference our tables using rRA as the
42 * base register. Note that rRA will be used both as the base to
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 * locate the switch table data and as the reference base for the switch
44 * target offsets stored in the table. We'll use a special pseudo-instruction
45 * to represent the jal and trigger the construction of the
46 * switch table offsets (which will happen after final assembly and all
47 * labels are fixed).
48 *
49 * The test loop will look something like:
50 *
buzbee2700f7e2014-03-07 09:46:20 -080051 * ori r_end, rZERO, #table_size ; size in bytes
52 * jal BaseLabel ; stores "return address" (BaseLabel) in rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -070053 * nop ; opportunistically fill
54 * BaseLabel:
buzbee2700f7e2014-03-07 09:46:20 -080055 * addiu r_base, rRA, <table> - <BaseLabel> ; table relative to BaseLabel
56 addu r_end, r_end, r_base ; end of table
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 * lw r_val, [rSP, v_reg_off] ; Test Value
58 * loop:
buzbee2700f7e2014-03-07 09:46:20 -080059 * beq r_base, r_end, done
60 * lw r_key, 0(r_base)
61 * addu r_base, 8
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 * bne r_val, r_key, loop
buzbee2700f7e2014-03-07 09:46:20 -080063 * lw r_disp, -4(r_base)
64 * addu rRA, r_disp
Andreas Gampe8d365912015-01-13 11:32:32 -080065 * jalr rZERO, rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 * done:
67 *
68 */
Andreas Gampe48971b32014-08-06 10:09:01 -070069void MipsMir2Lir::GenLargeSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070070 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Goran Jakovljevic10957932015-03-24 18:42:56 +010071 // Add the table to the list - we'll process it later.
buzbee0d829482013-10-11 15:24:55 -070072 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000073 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -080074 tab_rec->switch_mir = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -070075 tab_rec->table = table;
76 tab_rec->vaddr = current_dalvik_offset_;
77 int elements = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +010078 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070079
Goran Jakovljevic10957932015-03-24 18:42:56 +010080 // The table is composed of 8-byte key/disp pairs.
Brian Carlstrom7940e442013-07-12 13:46:57 -070081 int byte_size = elements * 8;
82
83 int size_hi = byte_size >> 16;
84 int size_lo = byte_size & 0xffff;
85
Goran Jakovljevic10957932015-03-24 18:42:56 +010086 RegStorage r_end = AllocPtrSizeTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070087 if (size_hi) {
buzbee2700f7e2014-03-07 09:46:20 -080088 NewLIR2(kMipsLui, r_end.GetReg(), size_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 }
Goran Jakovljevic10957932015-03-24 18:42:56 +010090 // Must prevent code motion for the curr pc pair.
Brian Carlstrom7940e442013-07-12 13:46:57 -070091 GenBarrier(); // Scheduling barrier
Goran Jakovljevic10957932015-03-24 18:42:56 +010092 NewLIR0(kMipsCurrPC); // Really a jal to .+8.
93 // Now, fill the branch delay slot.
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 if (size_hi) {
buzbee2700f7e2014-03-07 09:46:20 -080095 NewLIR3(kMipsOri, r_end.GetReg(), r_end.GetReg(), size_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -070096 } else {
buzbee2700f7e2014-03-07 09:46:20 -080097 NewLIR3(kMipsOri, r_end.GetReg(), rZERO, size_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -070098 }
Goran Jakovljevic10957932015-03-24 18:42:56 +010099 GenBarrier(); // Scheduling barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700100
Goran Jakovljevic10957932015-03-24 18:42:56 +0100101 // Construct BaseLabel and set up table base register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700102 LIR* base_label = NewLIR0(kPseudoTargetLabel);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100103 // Remember base label so offsets can be computed later.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700104 tab_rec->anchor = base_label;
Goran Jakovljevic10957932015-03-24 18:42:56 +0100105 RegStorage r_base = AllocPtrSizeTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800106 NewLIR4(kMipsDelta, r_base.GetReg(), 0, WrapPointer(base_label), WrapPointer(tab_rec));
107 OpRegRegReg(kOpAdd, r_end, r_end, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108
Goran Jakovljevic10957932015-03-24 18:42:56 +0100109 // Grab switch test value.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 rl_src = LoadValue(rl_src, kCoreReg);
111
Goran Jakovljevic10957932015-03-24 18:42:56 +0100112 // Test loop.
buzbee2700f7e2014-03-07 09:46:20 -0800113 RegStorage r_key = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 LIR* loop_label = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800115 LIR* exit_branch = OpCmpBranch(kCondEq, r_base, r_end, NULL);
buzbee695d13a2014-04-19 13:32:20 -0700116 Load32Disp(r_base, 0, r_key);
buzbee2700f7e2014-03-07 09:46:20 -0800117 OpRegImm(kOpAdd, r_base, 8);
118 OpCmpBranch(kCondNe, rl_src.reg, r_key, loop_label);
119 RegStorage r_disp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700120 Load32Disp(r_base, -4, r_disp);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100121 const RegStorage rs_ra = TargetPtrReg(kLr);
122 OpRegRegReg(kOpAdd, rs_ra, rs_ra, r_disp);
123 OpReg(kOpBx, rs_ra);
124 // Loop exit.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700125 LIR* exit_label = NewLIR0(kPseudoTargetLabel);
126 exit_branch->target = exit_label;
127}
128
129/*
130 * Code pattern will look something like:
131 *
132 * lw r_val
buzbee2700f7e2014-03-07 09:46:20 -0800133 * jal BaseLabel ; stores "return address" (BaseLabel) in rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 * nop ; opportunistically fill
135 * [subiu r_val, bias] ; Remove bias if low_val != 0
136 * bound check -> done
buzbee2700f7e2014-03-07 09:46:20 -0800137 * lw r_disp, [rRA, r_val]
138 * addu rRA, r_disp
Andreas Gampe8d365912015-01-13 11:32:32 -0800139 * jalr rZERO, rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 * done:
141 */
Andreas Gampe48971b32014-08-06 10:09:01 -0700142void MipsMir2Lir::GenLargePackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700143 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100144 // Add the table to the list - we'll process it later.
buzbee0d829482013-10-11 15:24:55 -0700145 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000146 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Chao-ying Fu72f53af2014-11-11 16:48:40 -0800147 tab_rec->switch_mir = mir;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700148 tab_rec->table = table;
149 tab_rec->vaddr = current_dalvik_offset_;
150 int size = table[1];
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100151 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700152
Goran Jakovljevic10957932015-03-24 18:42:56 +0100153 // Get the switch value.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 rl_src = LoadValue(rl_src, kCoreReg);
155
Goran Jakovljevic10957932015-03-24 18:42:56 +0100156 // Prepare the bias. If too big, handle 1st stage here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700157 int low_key = s4FromSwitchData(&table[2]);
158 bool large_bias = false;
buzbee2700f7e2014-03-07 09:46:20 -0800159 RegStorage r_key;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700160 if (low_key == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800161 r_key = rl_src.reg;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700162 } else if ((low_key & 0xffff) != low_key) {
163 r_key = AllocTemp();
164 LoadConstant(r_key, low_key);
165 large_bias = true;
166 } else {
167 r_key = AllocTemp();
168 }
169
Goran Jakovljevic10957932015-03-24 18:42:56 +0100170 // Must prevent code motion for the curr pc pair.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700171 GenBarrier();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100172 NewLIR0(kMipsCurrPC); // Really a jal to .+8.
173 // Now, fill the branch delay slot with bias strip.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700174 if (low_key == 0) {
175 NewLIR0(kMipsNop);
176 } else {
177 if (large_bias) {
buzbee2700f7e2014-03-07 09:46:20 -0800178 OpRegRegReg(kOpSub, r_key, rl_src.reg, r_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700179 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800180 OpRegRegImm(kOpSub, r_key, rl_src.reg, low_key);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 }
182 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100183 GenBarrier(); // Scheduling barrier.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700184
Goran Jakovljevic10957932015-03-24 18:42:56 +0100185 // Construct BaseLabel and set up table base register.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 LIR* base_label = NewLIR0(kPseudoTargetLabel);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100187 // Remember base label so offsets can be computed later.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700188 tab_rec->anchor = base_label;
189
Goran Jakovljevic10957932015-03-24 18:42:56 +0100190 // Bounds check - if < 0 or >= size continue following switch.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700191 LIR* branch_over = OpCmpImmBranch(kCondHi, r_key, size-1, NULL);
192
Goran Jakovljevic10957932015-03-24 18:42:56 +0100193 // Materialize the table base pointer.
194 RegStorage r_base = AllocPtrSizeTemp();
buzbee2700f7e2014-03-07 09:46:20 -0800195 NewLIR4(kMipsDelta, r_base.GetReg(), 0, WrapPointer(base_label), WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700196
Goran Jakovljevic10957932015-03-24 18:42:56 +0100197 // Load the displacement from the switch table.
buzbee2700f7e2014-03-07 09:46:20 -0800198 RegStorage r_disp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700199 LoadBaseIndexed(r_base, r_key, r_disp, 2, k32);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700200
Goran Jakovljevic10957932015-03-24 18:42:56 +0100201 // Add to rRA and go.
202 const RegStorage rs_ra = TargetPtrReg(kLr);
203 OpRegRegReg(kOpAdd, rs_ra, rs_ra, r_disp);
204 OpReg(kOpBx, rs_ra);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205
Goran Jakovljevic10957932015-03-24 18:42:56 +0100206 // Branch_over target here.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207 LIR* target = NewLIR0(kPseudoTargetLabel);
208 branch_over->target = target;
209}
210
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700211void MipsMir2Lir::GenMoveException(RegLocation rl_dest) {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100212 int ex_offset = cu_->target64 ? Thread::ExceptionOffset<8>().Int32Value() :
213 Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700214 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
215 RegStorage reset_reg = AllocTempRef();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100216 LoadRefDisp(TargetPtrReg(kSelf), ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700217 LoadConstant(reset_reg, 0);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100218 StoreRefDisp(TargetPtrReg(kSelf), ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700219 FreeTemp(reset_reg);
220 StoreValue(rl_dest, rl_result);
221}
222
Vladimir Markobf535be2014-11-19 18:52:35 +0000223void MipsMir2Lir::UnconditionallyMarkGCCard(RegStorage tgt_addr_reg) {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100224 RegStorage reg_card_base = AllocPtrSizeTemp();
225 RegStorage reg_card_no = AllocPtrSizeTemp();
226 if (cu_->target64) {
227 // NOTE: native pointer.
228 LoadWordDisp(TargetPtrReg(kSelf), Thread::CardTableOffset<8>().Int32Value(), reg_card_base);
229 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
230 StoreBaseIndexed(reg_card_base, reg_card_no, As32BitReg(reg_card_base), 0, kUnsignedByte);
231 } else {
232 // NOTE: native pointer.
233 LoadWordDisp(TargetPtrReg(kSelf), Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
234 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
235 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
236 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700237 FreeTemp(reg_card_base);
238 FreeTemp(reg_card_no);
239}
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700240
David Srbecky1109fb32015-04-07 20:21:06 +0100241static dwarf::Reg DwarfCoreReg(int num) {
242 return dwarf::Reg::MipsCore(num);
243}
244
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700245void MipsMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
David Srbecky1109fb32015-04-07 20:21:06 +0100246 DCHECK_EQ(cfi_.GetCurrentCFAOffset(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 int spill_count = num_core_spills_ + num_fp_spills_;
248 /*
Goran Jakovljevic10957932015-03-24 18:42:56 +0100249 * On entry, A0, A1, A2 & A3 are live. On Mips64, A4, A5, A6 & A7 are also live.
250 * Let the register allocation mechanism know so it doesn't try to use any of them when
251 * expanding the frame or flushing.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 */
Goran Jakovljevic10957932015-03-24 18:42:56 +0100253 const RegStorage arg0 = TargetReg(kArg0);
254 const RegStorage arg1 = TargetReg(kArg1);
255 const RegStorage arg2 = TargetReg(kArg2);
256 const RegStorage arg3 = TargetReg(kArg3);
257 const RegStorage arg4 = TargetReg(kArg4);
258 const RegStorage arg5 = TargetReg(kArg5);
259 const RegStorage arg6 = TargetReg(kArg6);
260 const RegStorage arg7 = TargetReg(kArg7);
261
262 LockTemp(arg0);
263 LockTemp(arg1);
264 LockTemp(arg2);
265 LockTemp(arg3);
266 if (cu_->target64) {
267 LockTemp(arg4);
268 LockTemp(arg5);
269 LockTemp(arg6);
270 LockTemp(arg7);
271 }
272
273 bool skip_overflow_check;
274 InstructionSet target = (cu_->target64) ? kMips64 : kMips;
275 int ptr_size = cu_->target64 ? 8 : 4;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700276
277 /*
278 * We can safely skip the stack overflow check if we're
279 * a leaf *and* our frame size < fudge factor.
280 */
Goran Jakovljevic10957932015-03-24 18:42:56 +0100281
282 skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, target);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100283 RegStorage check_reg = AllocPtrSizeTemp();
284 RegStorage new_sp = AllocPtrSizeTemp();
285 const RegStorage rs_sp = TargetPtrReg(kSp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700286 if (!skip_overflow_check) {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100287 // Load stack limit.
288 if (cu_->target64) {
289 LoadWordDisp(TargetPtrReg(kSelf), Thread::StackEndOffset<8>().Int32Value(), check_reg);
290 } else {
291 Load32Disp(TargetPtrReg(kSelf), Thread::StackEndOffset<4>().Int32Value(), check_reg);
292 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 }
Goran Jakovljevic10957932015-03-24 18:42:56 +0100294 // Spill core callee saves.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700295 SpillCoreRegs();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100296 // NOTE: promotion of FP regs currently unsupported, thus no FP spill.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 DCHECK_EQ(num_fp_spills_, 0);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100298 const int frame_sub = frame_size_ - spill_count * ptr_size;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700299 if (!skip_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700300 class StackOverflowSlowPath : public LIRSlowPath {
301 public:
302 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace)
Vladimir Marko0b40ecf2015-03-20 12:08:03 +0000303 : LIRSlowPath(m2l, branch), sp_displace_(sp_displace) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700304 }
305 void Compile() OVERRIDE {
306 m2l_->ResetRegPool();
307 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700308 GenerateTargetLabel(kPseudoThrowTarget);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100309 // RA is offset 0 since we push in reverse order.
310 m2l_->LoadWordDisp(m2l_->TargetPtrReg(kSp), 0, m2l_->TargetPtrReg(kLr));
311 m2l_->OpRegImm(kOpAdd, m2l_->TargetPtrReg(kSp), sp_displace_);
David Srbecky1109fb32015-04-07 20:21:06 +0100312 m2l_->cfi().AdjustCFAOffset(-sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700313 m2l_->ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700314 RegStorage r_tgt = m2l_->CallHelperSetup(kQuickThrowStackOverflow); // Doesn't clobber LR.
315 m2l_->CallHelper(r_tgt, kQuickThrowStackOverflow, false /* MarkSafepointPC */,
316 false /* UseLink */);
David Srbecky1109fb32015-04-07 20:21:06 +0100317 m2l_->cfi().AdjustCFAOffset(sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700318 }
319
320 private:
321 const size_t sp_displace_;
322 };
Goran Jakovljevic10957932015-03-24 18:42:56 +0100323 OpRegRegImm(kOpSub, new_sp, rs_sp, frame_sub);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700324 LIR* branch = OpCmpBranch(kCondUlt, new_sp, check_reg, nullptr);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100325 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, spill_count * ptr_size));
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700326 // TODO: avoid copy for small frame sizes.
Goran Jakovljevic10957932015-03-24 18:42:56 +0100327 OpRegCopy(rs_sp, new_sp); // Establish stack.
David Srbecky1109fb32015-04-07 20:21:06 +0100328 cfi_.AdjustCFAOffset(frame_sub);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700329 } else {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100330 OpRegImm(kOpSub, rs_sp, frame_sub);
David Srbecky1109fb32015-04-07 20:21:06 +0100331 cfi_.AdjustCFAOffset(frame_sub);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 }
333
334 FlushIns(ArgLocs, rl_method);
335
Goran Jakovljevic10957932015-03-24 18:42:56 +0100336 FreeTemp(arg0);
337 FreeTemp(arg1);
338 FreeTemp(arg2);
339 FreeTemp(arg3);
340 if (cu_->target64) {
341 FreeTemp(arg4);
342 FreeTemp(arg5);
343 FreeTemp(arg6);
344 FreeTemp(arg7);
345 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700346}
347
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700348void MipsMir2Lir::GenExitSequence() {
David Srbecky1109fb32015-04-07 20:21:06 +0100349 cfi_.RememberState();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700350 /*
351 * In the exit path, rMIPS_RET0/rMIPS_RET1 are live - make sure they aren't
352 * allocated by the register utilities as temps.
353 */
Goran Jakovljevic10957932015-03-24 18:42:56 +0100354 LockTemp(TargetPtrReg(kRet0));
355 LockTemp(TargetPtrReg(kRet1));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700356
Brian Carlstrom7940e442013-07-12 13:46:57 -0700357 UnSpillCoreRegs();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100358 OpReg(kOpBx, TargetPtrReg(kLr));
David Srbecky1109fb32015-04-07 20:21:06 +0100359 // The CFI should be restored for any code that follows the exit block.
360 cfi_.RestoreState();
361 cfi_.DefCFAOffset(frame_size_);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700362}
363
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800364void MipsMir2Lir::GenSpecialExitSequence() {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100365 OpReg(kOpBx, TargetPtrReg(kLr));
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800366}
367
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000368void MipsMir2Lir::GenSpecialEntryForSuspend() {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100369 // Keep 16-byte stack alignment - push A0, i.e. ArtMethod*, 2 filler words and RA for mips32,
370 // but A0 and RA for mips64.
371 core_spill_mask_ = (1u << TargetPtrReg(kLr).GetRegNum());
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000372 num_core_spills_ = 1u;
373 fp_spill_mask_ = 0u;
374 num_fp_spills_ = 0u;
375 frame_size_ = 16u;
376 core_vmap_table_.clear();
377 fp_vmap_table_.clear();
Goran Jakovljevic10957932015-03-24 18:42:56 +0100378 const RegStorage rs_sp = TargetPtrReg(kSp);
379 OpRegImm(kOpSub, rs_sp, frame_size_);
David Srbecky1109fb32015-04-07 20:21:06 +0100380 cfi_.AdjustCFAOffset(frame_size_);
Goran Jakovljevic10957932015-03-24 18:42:56 +0100381 StoreWordDisp(rs_sp, frame_size_ - (cu_->target64 ? 8 : 4), TargetPtrReg(kLr));
David Srbecky1109fb32015-04-07 20:21:06 +0100382 cfi_.RelOffset(DwarfCoreReg(rRA), frame_size_ - (cu_->target64 ? 8 : 4));
Goran Jakovljevic10957932015-03-24 18:42:56 +0100383 StoreWordDisp(rs_sp, 0, TargetPtrReg(kArg0));
David Srbecky1109fb32015-04-07 20:21:06 +0100384 // Do not generate CFI for scratch register A0.
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000385}
386
387void MipsMir2Lir::GenSpecialExitForSuspend() {
388 // Pop the frame. Don't pop ArtMethod*, it's no longer needed.
Goran Jakovljevic10957932015-03-24 18:42:56 +0100389 const RegStorage rs_sp = TargetPtrReg(kSp);
390 LoadWordDisp(rs_sp, frame_size_ - (cu_->target64 ? 8 : 4), TargetPtrReg(kLr));
David Srbecky1109fb32015-04-07 20:21:06 +0100391 cfi_.Restore(DwarfCoreReg(rRA));
Goran Jakovljevic10957932015-03-24 18:42:56 +0100392 OpRegImm(kOpAdd, rs_sp, frame_size_);
David Srbecky1109fb32015-04-07 20:21:06 +0100393 cfi_.AdjustCFAOffset(-frame_size_);
Vladimir Marko6ce3eba2015-02-16 13:05:59 +0000394}
395
Andreas Gamped500b532015-01-16 22:09:55 -0800396/*
397 * Bit of a hack here - in the absence of a real scheduling pass,
398 * emit the next instruction in static & direct invoke sequences.
399 */
Goran Jakovljevic10957932015-03-24 18:42:56 +0100400static int NextSDCallInsn(CompilationUnit* cu, CallInfo* info ATTRIBUTE_UNUSED, int state,
401 const MethodReference& target_method, uint32_t, uintptr_t direct_code,
402 uintptr_t direct_method, InvokeType type) {
Andreas Gamped500b532015-01-16 22:09:55 -0800403 Mir2Lir* cg = static_cast<Mir2Lir*>(cu->cg.get());
404 if (direct_code != 0 && direct_method != 0) {
405 switch (state) {
Andreas Gampe8f486f32015-04-09 15:30:51 -0700406 case 0: // Get the current Method* [sets kArg0]
Andreas Gamped500b532015-01-16 22:09:55 -0800407 if (direct_code != static_cast<uintptr_t>(-1)) {
Goran Jakovljevic10957932015-03-24 18:42:56 +0100408 if (cu->target64) {
409 cg->LoadConstantWide(cg->TargetPtrReg(kInvokeTgt), direct_code);
410 } else {
411 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
412 }
Andreas Gamped500b532015-01-16 22:09:55 -0800413 } else {
Andreas Gamped500b532015-01-16 22:09:55 -0800414 cg->LoadCodeAddress(target_method, type, kInvokeTgt);
415 }
Andreas Gampe8f486f32015-04-09 15:30:51 -0700416 if (direct_method != static_cast<uintptr_t>(-1)) {
417 if (cu->target64) {
418 cg->LoadConstantWide(cg->TargetReg(kArg0, kRef), direct_method);
419 } else {
420 cg->LoadConstant(cg->TargetReg(kArg0, kRef), direct_method);
421 }
422 } else {
423 cg->LoadMethodAddress(target_method, type, kArg0);
424 }
425 break;
426 default:
427 return -1;
428 }
429 } else {
430 RegStorage arg0_ref = cg->TargetReg(kArg0, kRef);
431 switch (state) {
432 case 0: // Get the current Method* [sets kArg0]
433 // TUNING: we can save a reg copy if Method* has been promoted.
434 cg->LoadCurrMethodDirect(arg0_ref);
435 break;
436 case 1: // Get method->dex_cache_resolved_methods_
437 cg->LoadRefDisp(arg0_ref,
438 mirror::ArtMethod::DexCacheResolvedMethodsOffset().Int32Value(),
439 arg0_ref,
440 kNotVolatile);
441 // Set up direct code if known.
442 if (direct_code != 0) {
443 if (direct_code != static_cast<uintptr_t>(-1)) {
444 if (cu->target64) {
445 cg->LoadConstantWide(cg->TargetPtrReg(kInvokeTgt), direct_code);
446 } else {
447 cg->LoadConstant(cg->TargetPtrReg(kInvokeTgt), direct_code);
448 }
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>::
459 OffsetOfElement(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 int32_t offset = mirror::ArtMethod::EntryPointFromQuickCompiledCodeOffset(
466 InstructionSetPointerSize(cu->instruction_set)).Int32Value();
467 // Get the compiled code address [use *alt_from or kArg0, set kInvokeTgt]
468 cg->LoadWordDisp(arg0_ref, offset, cg->TargetPtrReg(kInvokeTgt));
469 }
470 break;
471 default:
472 return -1;
Andreas Gamped500b532015-01-16 22:09:55 -0800473 }
474 }
475 return state + 1;
476}
477
478NextCallInsn MipsMir2Lir::GetNextSDCallInsn() {
479 return NextSDCallInsn;
480}
481
482LIR* MipsMir2Lir::GenCallInsn(const MirMethodLoweringInfo& method_info ATTRIBUTE_UNUSED) {
483 return OpReg(kOpBlx, TargetPtrReg(kInvokeTgt));
484}
485
Brian Carlstrom7940e442013-07-12 13:46:57 -0700486} // namespace art