blob: 9a5ca2ce67f15b045eaebe7fa859485c22fd8189 [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"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "mips_lir.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070023
24namespace art {
25
26void MipsMir2Lir::GenSpecialCase(BasicBlock* bb, MIR* mir,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070027 SpecialCaseHandler special_case) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070028 // TODO
29}
30
31/*
32 * The lack of pc-relative loads on Mips presents somewhat of a challenge
33 * for our PIC switch table strategy. To materialize the current location
34 * we'll do a dummy JAL and reference our tables using r_RA as the
35 * base register. Note that r_RA will be used both as the base to
36 * locate the switch table data and as the reference base for the switch
37 * target offsets stored in the table. We'll use a special pseudo-instruction
38 * to represent the jal and trigger the construction of the
39 * switch table offsets (which will happen after final assembly and all
40 * labels are fixed).
41 *
42 * The test loop will look something like:
43 *
44 * ori rEnd, r_ZERO, #table_size ; size in bytes
45 * jal BaseLabel ; stores "return address" (BaseLabel) in r_RA
46 * nop ; opportunistically fill
47 * BaseLabel:
48 * addiu rBase, r_RA, <table> - <BaseLabel> ; table relative to BaseLabel
49 addu rEnd, rEnd, rBase ; end of table
50 * lw r_val, [rSP, v_reg_off] ; Test Value
51 * loop:
52 * beq rBase, rEnd, done
53 * lw r_key, 0(rBase)
54 * addu rBase, 8
55 * bne r_val, r_key, loop
56 * lw r_disp, -4(rBase)
57 * addu r_RA, r_disp
58 * jr r_RA
59 * done:
60 *
61 */
62void MipsMir2Lir::GenSparseSwitch(MIR* mir, uint32_t table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070063 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070064 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
65 if (cu_->verbose) {
66 DumpSparseSwitchTable(table);
67 }
68 // Add the table to the list - we'll process it later
69 SwitchTable *tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070070 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 tab_rec->table = table;
72 tab_rec->vaddr = current_dalvik_offset_;
73 int elements = table[1];
74 tab_rec->targets =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -070075 static_cast<LIR**>(arena_->Alloc(elements * sizeof(LIR*), ArenaAllocator::kAllocLIR));
Brian Carlstrom7940e442013-07-12 13:46:57 -070076 switch_tables_.Insert(tab_rec);
77
78 // The table is composed of 8-byte key/disp pairs
79 int byte_size = elements * 8;
80
81 int size_hi = byte_size >> 16;
82 int size_lo = byte_size & 0xffff;
83
84 int rEnd = AllocTemp();
85 if (size_hi) {
86 NewLIR2(kMipsLui, rEnd, size_hi);
87 }
88 // Must prevent code motion for the curr pc pair
89 GenBarrier(); // Scheduling barrier
90 NewLIR0(kMipsCurrPC); // Really a jal to .+8
91 // Now, fill the branch delay slot
92 if (size_hi) {
93 NewLIR3(kMipsOri, rEnd, rEnd, size_lo);
94 } else {
95 NewLIR3(kMipsOri, rEnd, r_ZERO, size_lo);
96 }
97 GenBarrier(); // Scheduling barrier
98
99 // Construct BaseLabel and set up table base register
100 LIR* base_label = NewLIR0(kPseudoTargetLabel);
101 // Remember base label so offsets can be computed later
102 tab_rec->anchor = base_label;
103 int rBase = AllocTemp();
104 NewLIR4(kMipsDelta, rBase, 0, reinterpret_cast<uintptr_t>(base_label),
105 reinterpret_cast<uintptr_t>(tab_rec));
106 OpRegRegReg(kOpAdd, rEnd, rEnd, rBase);
107
108 // Grab switch test value
109 rl_src = LoadValue(rl_src, kCoreReg);
110
111 // Test loop
112 int r_key = AllocTemp();
113 LIR* loop_label = NewLIR0(kPseudoTargetLabel);
114 LIR* exit_branch = OpCmpBranch(kCondEq, rBase, rEnd, NULL);
115 LoadWordDisp(rBase, 0, r_key);
116 OpRegImm(kOpAdd, rBase, 8);
117 OpCmpBranch(kCondNe, rl_src.low_reg, r_key, loop_label);
118 int r_disp = AllocTemp();
119 LoadWordDisp(rBase, -4, r_disp);
120 OpRegRegReg(kOpAdd, r_RA, r_RA, r_disp);
121 OpReg(kOpBx, r_RA);
122
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
132 * jal BaseLabel ; stores "return address" (BaseLabel) in r_RA
133 * nop ; opportunistically fill
134 * [subiu r_val, bias] ; Remove bias if low_val != 0
135 * bound check -> done
136 * lw r_disp, [r_RA, r_val]
137 * addu r_RA, r_disp
138 * jr r_RA
139 * done:
140 */
141void MipsMir2Lir::GenPackedSwitch(MIR* mir, uint32_t table_offset,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700142 RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
144 if (cu_->verbose) {
145 DumpPackedSwitchTable(table);
146 }
147 // Add the table to the list - we'll process it later
148 SwitchTable *tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700149 static_cast<SwitchTable*>(arena_->Alloc(sizeof(SwitchTable), ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700150 tab_rec->table = table;
151 tab_rec->vaddr = current_dalvik_offset_;
152 int size = table[1];
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700153 tab_rec->targets = static_cast<LIR**>(arena_->Alloc(size * sizeof(LIR*),
Brian Carlstrom7940e442013-07-12 13:46:57 -0700154 ArenaAllocator::kAllocLIR));
155 switch_tables_.Insert(tab_rec);
156
157 // Get the switch value
158 rl_src = LoadValue(rl_src, kCoreReg);
159
160 // Prepare the bias. If too big, handle 1st stage here
161 int low_key = s4FromSwitchData(&table[2]);
162 bool large_bias = false;
163 int r_key;
164 if (low_key == 0) {
165 r_key = rl_src.low_reg;
166 } 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
174 // Must prevent code motion for the curr pc pair
175 GenBarrier();
176 NewLIR0(kMipsCurrPC); // Really a jal to .+8
177 // Now, fill the branch delay slot with bias strip
178 if (low_key == 0) {
179 NewLIR0(kMipsNop);
180 } else {
181 if (large_bias) {
182 OpRegRegReg(kOpSub, r_key, rl_src.low_reg, r_key);
183 } else {
184 OpRegRegImm(kOpSub, r_key, rl_src.low_reg, low_key);
185 }
186 }
187 GenBarrier(); // Scheduling barrier
188
189 // Construct BaseLabel and set up table base register
190 LIR* base_label = NewLIR0(kPseudoTargetLabel);
191 // Remember base label so offsets can be computed later
192 tab_rec->anchor = base_label;
193
194 // Bounds check - if < 0 or >= size continue following switch
195 LIR* branch_over = OpCmpImmBranch(kCondHi, r_key, size-1, NULL);
196
197 // Materialize the table base pointer
198 int rBase = AllocTemp();
199 NewLIR4(kMipsDelta, rBase, 0, reinterpret_cast<uintptr_t>(base_label),
200 reinterpret_cast<uintptr_t>(tab_rec));
201
202 // Load the displacement from the switch table
203 int r_disp = AllocTemp();
204 LoadBaseIndexed(rBase, r_key, r_disp, 2, kWord);
205
206 // Add to r_AP and go
207 OpRegRegReg(kOpAdd, r_RA, r_RA, r_disp);
208 OpReg(kOpBx, r_RA);
209
210 /* branch_over target here */
211 LIR* target = NewLIR0(kPseudoTargetLabel);
212 branch_over->target = target;
213}
214
215/*
216 * Array data table format:
217 * ushort ident = 0x0300 magic value
218 * ushort width width of each element in the table
219 * uint size number of elements in the table
220 * ubyte data[size*width] table of data values (may contain a single-byte
221 * padding at the end)
222 *
223 * Total size is 4+(width * size + 1)/2 16-bit code units.
224 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700225void MipsMir2Lir::GenFillArrayData(uint32_t table_offset, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700226 const uint16_t* table = cu_->insns + current_dalvik_offset_ + table_offset;
227 // Add the table to the list - we'll process it later
228 FillArrayData *tab_rec =
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700229 reinterpret_cast<FillArrayData*>(arena_->Alloc(sizeof(FillArrayData),
230 ArenaAllocator::kAllocData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700231 tab_rec->table = table;
232 tab_rec->vaddr = current_dalvik_offset_;
233 uint16_t width = tab_rec->table[1];
234 uint32_t size = tab_rec->table[2] | ((static_cast<uint32_t>(tab_rec->table[3])) << 16);
235 tab_rec->size = (size * width) + 8;
236
237 fill_array_data_.Insert(tab_rec);
238
239 // Making a call - use explicit registers
240 FlushAllRegs(); /* Everything to home location */
241 LockCallTemps();
242 LoadValueDirectFixed(rl_src, rMIPS_ARG0);
243
244 // Must prevent code motion for the curr pc pair
245 GenBarrier();
246 NewLIR0(kMipsCurrPC); // Really a jal to .+8
247 // Now, fill the branch delay slot with the helper load
Ian Rogers468532e2013-08-05 10:56:33 -0700248 int r_tgt = LoadHelper(QUICK_ENTRYPOINT_OFFSET(pHandleFillArrayData));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700249 GenBarrier(); // Scheduling barrier
250
251 // Construct BaseLabel and set up table base register
252 LIR* base_label = NewLIR0(kPseudoTargetLabel);
253
254 // Materialize a pointer to the fill data image
255 NewLIR4(kMipsDelta, rMIPS_ARG1, 0, reinterpret_cast<uintptr_t>(base_label),
256 reinterpret_cast<uintptr_t>(tab_rec));
257
258 // And go...
259 ClobberCalleeSave();
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700260 LIR* call_inst = OpReg(kOpBlx, r_tgt); // ( array*, fill_data* )
Brian Carlstrom7940e442013-07-12 13:46:57 -0700261 MarkSafepointPC(call_inst);
262}
263
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700264void MipsMir2Lir::GenMoveException(RegLocation rl_dest) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700265 int ex_offset = Thread::ExceptionOffset().Int32Value();
266 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
267 int reset_reg = AllocTemp();
268 LoadWordDisp(rMIPS_SELF, ex_offset, rl_result.low_reg);
269 LoadConstant(reset_reg, 0);
270 StoreWordDisp(rMIPS_SELF, ex_offset, reset_reg);
271 FreeTemp(reset_reg);
272 StoreValue(rl_dest, rl_result);
273}
274
275/*
276 * Mark garbage collection card. Skip if the value we're storing is null.
277 */
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700278void MipsMir2Lir::MarkGCCard(int val_reg, int tgt_addr_reg) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700279 int reg_card_base = AllocTemp();
280 int reg_card_no = AllocTemp();
281 LIR* branch_over = OpCmpImmBranch(kCondEq, val_reg, 0, NULL);
282 LoadWordDisp(rMIPS_SELF, Thread::CardTableOffset().Int32Value(), reg_card_base);
283 OpRegRegImm(kOpLsr, reg_card_no, tgt_addr_reg, gc::accounting::CardTable::kCardShift);
284 StoreBaseIndexed(reg_card_base, reg_card_no, reg_card_base, 0,
285 kUnsignedByte);
286 LIR* target = NewLIR0(kPseudoTargetLabel);
287 branch_over->target = target;
288 FreeTemp(reg_card_base);
289 FreeTemp(reg_card_no);
290}
Ian Rogersd9c4fc92013-10-01 19:45:43 -0700291
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700292void MipsMir2Lir::GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 int spill_count = num_core_spills_ + num_fp_spills_;
294 /*
295 * On entry, rMIPS_ARG0, rMIPS_ARG1, rMIPS_ARG2 & rMIPS_ARG3 are live. Let the register
296 * allocation mechanism know so it doesn't try to use any of them when
297 * expanding the frame or flushing. This leaves the utility
298 * code with a single temp: r12. This should be enough.
299 */
300 LockTemp(rMIPS_ARG0);
301 LockTemp(rMIPS_ARG1);
302 LockTemp(rMIPS_ARG2);
303 LockTemp(rMIPS_ARG3);
304
305 /*
306 * We can safely skip the stack overflow check if we're
307 * a leaf *and* our frame size < fudge factor.
308 */
309 bool skip_overflow_check = (mir_graph_->MethodIsLeaf() &&
310 (static_cast<size_t>(frame_size_) < Thread::kStackOverflowReservedBytes));
311 NewLIR0(kPseudoMethodEntry);
312 int check_reg = AllocTemp();
313 int new_sp = AllocTemp();
314 if (!skip_overflow_check) {
315 /* Load stack limit */
316 LoadWordDisp(rMIPS_SELF, Thread::StackEndOffset().Int32Value(), check_reg);
317 }
318 /* Spill core callee saves */
319 SpillCoreRegs();
320 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
321 DCHECK_EQ(num_fp_spills_, 0);
322 if (!skip_overflow_check) {
323 OpRegRegImm(kOpSub, new_sp, rMIPS_SP, frame_size_ - (spill_count * 4));
324 GenRegRegCheck(kCondCc, new_sp, check_reg, kThrowStackOverflow);
325 OpRegCopy(rMIPS_SP, new_sp); // Establish stack
326 } else {
327 OpRegImm(kOpSub, rMIPS_SP, frame_size_ - (spill_count * 4));
328 }
329
330 FlushIns(ArgLocs, rl_method);
331
332 FreeTemp(rMIPS_ARG0);
333 FreeTemp(rMIPS_ARG1);
334 FreeTemp(rMIPS_ARG2);
335 FreeTemp(rMIPS_ARG3);
336}
337
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700338void MipsMir2Lir::GenExitSequence() {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339 /*
340 * In the exit path, rMIPS_RET0/rMIPS_RET1 are live - make sure they aren't
341 * allocated by the register utilities as temps.
342 */
343 LockTemp(rMIPS_RET0);
344 LockTemp(rMIPS_RET1);
345
346 NewLIR0(kPseudoMethodExit);
347 UnSpillCoreRegs();
348 OpReg(kOpBx, r_RA);
349}
350
351} // namespace art