blob: 01784e2fec178f04b8dac197aa321015984dc89d [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"
20#include "dex/quick/mir_to_lir-inl.h"
Ian Rogers166db042013-07-26 12:05:57 -070021#include "entrypoints/quick/quick_entrypoints.h"
Ian Rogers576ca0c2014-06-06 15:58:22 -070022#include "gc/accounting/card_table.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023#include "mips_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070024
25namespace art {
26
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070027bool MipsMir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir, const InlineMethod& special) {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080028 // TODO
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070029 UNUSED(bb, mir, special);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -080030 return false;
Brian Carlstrom7940e442013-07-12 13:46:57 -070031}
32
33/*
34 * The lack of pc-relative loads on Mips presents somewhat of a challenge
35 * for our PIC switch table strategy. To materialize the current location
buzbee2700f7e2014-03-07 09:46:20 -080036 * we'll do a dummy JAL and reference our tables using rRA as the
37 * base register. Note that rRA will be used both as the base to
Brian Carlstrom7940e442013-07-12 13:46:57 -070038 * locate the switch table data and as the reference base for the switch
39 * target offsets stored in the table. We'll use a special pseudo-instruction
40 * to represent the jal and trigger the construction of the
41 * switch table offsets (which will happen after final assembly and all
42 * labels are fixed).
43 *
44 * The test loop will look something like:
45 *
buzbee2700f7e2014-03-07 09:46:20 -080046 * ori r_end, rZERO, #table_size ; size in bytes
47 * jal BaseLabel ; stores "return address" (BaseLabel) in rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -070048 * nop ; opportunistically fill
49 * BaseLabel:
buzbee2700f7e2014-03-07 09:46:20 -080050 * addiu r_base, rRA, <table> - <BaseLabel> ; table relative to BaseLabel
51 addu r_end, r_end, r_base ; end of table
Brian Carlstrom7940e442013-07-12 13:46:57 -070052 * lw r_val, [rSP, v_reg_off] ; Test Value
53 * loop:
buzbee2700f7e2014-03-07 09:46:20 -080054 * beq r_base, r_end, done
55 * lw r_key, 0(r_base)
56 * addu r_base, 8
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 * bne r_val, r_key, loop
buzbee2700f7e2014-03-07 09:46:20 -080058 * lw r_disp, -4(r_base)
59 * addu rRA, r_disp
60 * jr rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -070061 * done:
62 *
63 */
Andreas Gampe48971b32014-08-06 10:09:01 -070064void MipsMir2Lir::GenLargeSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -070065 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 if (cu_->verbose) {
67 DumpSparseSwitchTable(table);
68 }
69 // Add the table to the list - we'll process it later
buzbee0d829482013-10-11 15:24:55 -070070 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000071 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -070072 tab_rec->table = table;
73 tab_rec->vaddr = current_dalvik_offset_;
74 int elements = table[1];
75 tab_rec->targets =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +000076 static_cast<LIR**>(arena_->Alloc(elements * sizeof(LIR*), kArenaAllocLIR));
Vladimir Markoe39c54e2014-09-22 14:50:02 +010077 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -070078
79 // The table is composed of 8-byte key/disp pairs
80 int byte_size = elements * 8;
81
82 int size_hi = byte_size >> 16;
83 int size_lo = byte_size & 0xffff;
84
buzbee2700f7e2014-03-07 09:46:20 -080085 RegStorage r_end = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -070086 if (size_hi) {
buzbee2700f7e2014-03-07 09:46:20 -080087 NewLIR2(kMipsLui, r_end.GetReg(), size_hi);
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 }
89 // Must prevent code motion for the curr pc pair
90 GenBarrier(); // Scheduling barrier
91 NewLIR0(kMipsCurrPC); // Really a jal to .+8
92 // Now, fill the branch delay slot
93 if (size_hi) {
buzbee2700f7e2014-03-07 09:46:20 -080094 NewLIR3(kMipsOri, r_end.GetReg(), r_end.GetReg(), size_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 } else {
buzbee2700f7e2014-03-07 09:46:20 -080096 NewLIR3(kMipsOri, r_end.GetReg(), rZERO, size_lo);
Brian Carlstrom7940e442013-07-12 13:46:57 -070097 }
98 GenBarrier(); // Scheduling barrier
99
100 // Construct BaseLabel and set up table base register
101 LIR* base_label = NewLIR0(kPseudoTargetLabel);
102 // Remember base label so offsets can be computed later
103 tab_rec->anchor = base_label;
buzbee2700f7e2014-03-07 09:46:20 -0800104 RegStorage r_base = AllocTemp();
105 NewLIR4(kMipsDelta, r_base.GetReg(), 0, WrapPointer(base_label), WrapPointer(tab_rec));
106 OpRegRegReg(kOpAdd, r_end, r_end, r_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107
108 // Grab switch test value
109 rl_src = LoadValue(rl_src, kCoreReg);
110
111 // Test loop
buzbee2700f7e2014-03-07 09:46:20 -0800112 RegStorage r_key = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700113 LIR* loop_label = NewLIR0(kPseudoTargetLabel);
buzbee2700f7e2014-03-07 09:46:20 -0800114 LIR* exit_branch = OpCmpBranch(kCondEq, r_base, r_end, NULL);
buzbee695d13a2014-04-19 13:32:20 -0700115 Load32Disp(r_base, 0, r_key);
buzbee2700f7e2014-03-07 09:46:20 -0800116 OpRegImm(kOpAdd, r_base, 8);
117 OpCmpBranch(kCondNe, rl_src.reg, r_key, loop_label);
118 RegStorage r_disp = AllocTemp();
buzbee695d13a2014-04-19 13:32:20 -0700119 Load32Disp(r_base, -4, r_disp);
buzbee2700f7e2014-03-07 09:46:20 -0800120 OpRegRegReg(kOpAdd, rs_rRA, rs_rRA, r_disp);
121 OpReg(kOpBx, rs_rRA);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700122
123 // Loop exit
124 LIR* exit_label = NewLIR0(kPseudoTargetLabel);
125 exit_branch->target = exit_label;
126}
127
128/*
129 * Code pattern will look something like:
130 *
131 * lw r_val
buzbee2700f7e2014-03-07 09:46:20 -0800132 * jal BaseLabel ; stores "return address" (BaseLabel) in rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -0700133 * nop ; opportunistically fill
134 * [subiu r_val, bias] ; Remove bias if low_val != 0
135 * bound check -> done
buzbee2700f7e2014-03-07 09:46:20 -0800136 * lw r_disp, [rRA, r_val]
137 * addu rRA, r_disp
138 * jr rRA
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 * done:
140 */
Andreas Gampe48971b32014-08-06 10:09:01 -0700141void MipsMir2Lir::GenLargePackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700142 const uint16_t* table = mir_graph_->GetTable(mir, table_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 if (cu_->verbose) {
144 DumpPackedSwitchTable(table);
145 }
146 // Add the table to the list - we'll process it later
buzbee0d829482013-10-11 15:24:55 -0700147 SwitchTable* tab_rec =
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000148 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), kArenaAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700149 tab_rec->table = table;
150 tab_rec->vaddr = current_dalvik_offset_;
151 int size = table[1];
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700152 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000153 kArenaAllocLIR));
Vladimir Markoe39c54e2014-09-22 14:50:02 +0100154 switch_tables_.push_back(tab_rec);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700155
156 // Get the switch value
157 rl_src = LoadValue(rl_src, kCoreReg);
158
159 // Prepare the bias. If too big, handle 1st stage here
160 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
173 // Must prevent code motion for the curr pc pair
174 GenBarrier();
175 NewLIR0(kMipsCurrPC); // Really a jal to .+8
176 // Now, fill the branch delay slot with bias strip
177 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 }
186 GenBarrier(); // Scheduling barrier
187
188 // Construct BaseLabel and set up table base register
189 LIR* base_label = NewLIR0(kPseudoTargetLabel);
190 // Remember base label so offsets can be computed later
191 tab_rec->anchor = base_label;
192
193 // Bounds check - if < 0 or >= size continue following switch
194 LIR* branch_over = OpCmpImmBranch(kCondHi, r_key, size-1, NULL);
195
196 // Materialize the table base pointer
buzbee2700f7e2014-03-07 09:46:20 -0800197 RegStorage r_base = AllocTemp();
198 NewLIR4(kMipsDelta, r_base.GetReg(), 0, WrapPointer(base_label), WrapPointer(tab_rec));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700199
200 // 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
buzbee2700f7e2014-03-07 09:46:20 -0800204 // Add to rAP and go
205 OpRegRegReg(kOpAdd, rs_rRA, rs_rRA, r_disp);
206 OpReg(kOpBx, rs_rRA);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700207
208 /* branch_over target here */
209 LIR* target = NewLIR0(kPseudoTargetLabel);
210 branch_over->target = target;
211}
212
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700213void MipsMir2Lir::GenMoveException(RegLocation rl_dest) {
Ian Rogersdd7624d2014-03-14 17:43:00 -0700214 int ex_offset = Thread::ExceptionOffset<4>().Int32Value();
buzbeea0cd2d72014-06-01 09:33:49 -0700215 RegLocation rl_result = EvalLoc(rl_dest, kRefReg, true);
216 RegStorage reset_reg = AllocTempRef();
Andreas Gampe3c12c512014-06-24 18:46:29 +0000217 LoadRefDisp(rs_rMIPS_SELF, ex_offset, rl_result.reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 LoadConstant(reset_reg, 0);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000219 StoreRefDisp(rs_rMIPS_SELF, ex_offset, reset_reg, kNotVolatile);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700220 FreeTemp(reset_reg);
221 StoreValue(rl_dest, rl_result);
222}
223
224/*
225 * Mark garbage collection card. Skip if the value we're storing is null.
226 */
buzbee2700f7e2014-03-07 09:46:20 -0800227void MipsMir2Lir::MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg) {
228 RegStorage reg_card_base = AllocTemp();
229 RegStorage reg_card_no = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700230 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
buzbee695d13a2014-04-19 13:32:20 -0700231 // NOTE: native pointer.
Ian Rogersdd7624d2014-03-14 17:43:00 -0700232 LoadWordDisp(rs_rMIPS_SELF, Thread::CardTableOffset<4>().Int32Value(), reg_card_base);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700233 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
buzbee2700f7e2014-03-07 09:46:20 -0800234 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0, kUnsignedByte);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700235 LIR* target = NewLIR0(kPseudoTargetLabel);
236 branch_over->target = target;
237 FreeTemp(reg_card_base);
238 FreeTemp(reg_card_no);
239}
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700240
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700241void MipsMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700242 int spill_count = num_core_spills_ + num_fp_spills_;
243 /*
244 * On entry, rMIPS_ARG0, rMIPS_ARG1, rMIPS_ARG2 & rMIPS_ARG3 are live. Let the register
245 * allocation mechanism know so it doesn't try to use any of them when
246 * expanding the frame or flushing. This leaves the utility
247 * code with a single temp: r12. This should be enough.
248 */
buzbee091cc402014-03-31 10:14:40 -0700249 LockTemp(rs_rMIPS_ARG0);
250 LockTemp(rs_rMIPS_ARG1);
251 LockTemp(rs_rMIPS_ARG2);
252 LockTemp(rs_rMIPS_ARG3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700253
254 /*
255 * We can safely skip the stack overflow check if we're
256 * a leaf *and* our frame size < fudge factor.
257 */
Dave Allison648d7112014-07-25 16:15:27 -0700258 bool skip_overflow_check = mir_graph_->MethodIsLeaf() && !FrameNeedsStackCheck(frame_size_, kMips);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700259 NewLIR0(kPseudoMethodEntry);
buzbee2700f7e2014-03-07 09:46:20 -0800260 RegStorage check_reg = AllocTemp();
261 RegStorage new_sp = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700262 if (!skip_overflow_check) {
263 /* Load stack limit */
buzbee695d13a2014-04-19 13:32:20 -0700264 Load32Disp(rs_rMIPS_SELF, Thread::StackEndOffset<4>().Int32Value(), check_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 }
266 /* Spill core callee saves */
267 SpillCoreRegs();
268 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
269 DCHECK_EQ(num_fp_spills_, 0);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700270 const int frame_sub = frame_size_ - spill_count * 4;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700271 if (!skip_overflow_check) {
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700272 class StackOverflowSlowPath : public LIRSlowPath {
273 public:
274 StackOverflowSlowPath(Mir2Lir* m2l, LIR* branch, size_t sp_displace)
275 : LIRSlowPath(m2l, m2l->GetCurrentDexPc(), branch, nullptr), sp_displace_(sp_displace) {
276 }
277 void Compile() OVERRIDE {
278 m2l_->ResetRegPool();
279 m2l_->ResetDefTracking();
Mingyao Yang6ffcfa02014-04-25 11:06:00 -0700280 GenerateTargetLabel(kPseudoThrowTarget);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700281 // LR is offset 0 since we push in reverse order.
buzbee695d13a2014-04-19 13:32:20 -0700282 m2l_->Load32Disp(rs_rMIPS_SP, 0, rs_rRA);
buzbee2700f7e2014-03-07 09:46:20 -0800283 m2l_->OpRegImm(kOpAdd, rs_rMIPS_SP, sp_displace_);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700284 m2l_->ClobberCallerSave();
Andreas Gampe98430592014-07-27 19:44:50 -0700285 RegStorage r_tgt = m2l_->CallHelperSetup(kQuickThrowStackOverflow); // Doesn't clobber LR.
286 m2l_->CallHelper(r_tgt, kQuickThrowStackOverflow, false /* MarkSafepointPC */,
287 false /* UseLink */);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700288 }
289
290 private:
291 const size_t sp_displace_;
292 };
buzbee2700f7e2014-03-07 09:46:20 -0800293 OpRegRegImm(kOpSub, new_sp, rs_rMIPS_SP, frame_sub);
Mathieu Chartier0d507d12014-03-19 10:17:28 -0700294 LIR* branch = OpCmpBranch(kCondUlt, new_sp, check_reg, nullptr);
295 AddSlowPath(new(arena_)StackOverflowSlowPath(this, branch, spill_count * 4));
296 // TODO: avoid copy for small frame sizes.
buzbee2700f7e2014-03-07 09:46:20 -0800297 OpRegCopy(rs_rMIPS_SP, new_sp); // Establish stack
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800299 OpRegImm(kOpSub, rs_rMIPS_SP, frame_sub);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700300 }
301
302 FlushIns(ArgLocs, rl_method);
303
buzbee091cc402014-03-31 10:14:40 -0700304 FreeTemp(rs_rMIPS_ARG0);
305 FreeTemp(rs_rMIPS_ARG1);
306 FreeTemp(rs_rMIPS_ARG2);
307 FreeTemp(rs_rMIPS_ARG3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700308}
309
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700310void MipsMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700311 /*
312 * In the exit path, rMIPS_RET0/rMIPS_RET1 are live - make sure they aren't
313 * allocated by the register utilities as temps.
314 */
buzbee091cc402014-03-31 10:14:40 -0700315 LockTemp(rs_rMIPS_RET0);
316 LockTemp(rs_rMIPS_RET1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700317
318 NewLIR0(kPseudoMethodExit);
319 UnSpillCoreRegs();
buzbee2700f7e2014-03-07 09:46:20 -0800320 OpReg(kOpBx, rs_rRA);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700321}
322
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800323void MipsMir2Lir::GenSpecialExitSequence() {
buzbee2700f7e2014-03-07 09:46:20 -0800324 OpReg(kOpBx, rs_rRA);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800325}
326
Brian Carlstrom7940e442013-07-12 13:46:57 -0700327} // namespace art