blob: 5049eecf5ba2341d696c3fefb8acb1ad2156380b [file] [log] [blame]
buzbeeefc63692012-11-14 16:31:52 -08001/*
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 "oat/runtime/oat_support_entrypoints.h"
buzbee1bc37c62012-11-20 13:35:41 -080020#include "mips_lir.h"
21#include "../codegen_util.h"
22#include "../ralloc_util.h"
buzbeeefc63692012-11-14 16:31:52 -080023
24namespace art {
25
26void genSpecialCase(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
27 SpecialCaseHandler specialCase)
28{
29 // TODO
30}
31
32/*
33 * The lack of pc-relative loads on Mips presents somewhat of a challenge
34 * for our PIC switch table strategy. To materialize the current location
35 * we'll do a dummy JAL and reference our tables using r_RA as the
36 * base register. Note that r_RA will be used both as the base to
37 * locate the switch table data and as the reference base for the switch
38 * target offsets stored in the table. We'll use a special pseudo-instruction
39 * to represent the jal and trigger the construction of the
40 * switch table offsets (which will happen after final assembly and all
41 * labels are fixed).
42 *
43 * The test loop will look something like:
44 *
45 * ori rEnd, r_ZERO, #tableSize ; size in bytes
46 * jal BaseLabel ; stores "return address" (BaseLabel) in r_RA
47 * nop ; opportunistically fill
48 * BaseLabel:
49 * addiu rBase, r_RA, <table> - <BaseLabel> ; table relative to BaseLabel
50 addu rEnd, rEnd, rBase ; end of table
51 * lw rVal, [rSP, vRegOff] ; Test Value
52 * loop:
53 * beq rBase, rEnd, done
54 * lw rKey, 0(rBase)
55 * addu rBase, 8
56 * bne rVal, rKey, loop
57 * lw rDisp, -4(rBase)
58 * addu r_RA, rDisp
59 * jr r_RA
60 * done:
61 *
62 */
63void genSparseSwitch(CompilationUnit* cUnit, uint32_t tableOffset,
64 RegLocation rlSrc)
65{
buzbeeeaf09bc2012-11-15 14:51:41 -080066 const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
buzbeeefc63692012-11-14 16:31:52 -080067 if (cUnit->printMe) {
68 dumpSparseSwitchTable(table);
69 }
70 // Add the table to the list - we'll process it later
buzbeecbd6d442012-11-17 14:11:25 -080071 SwitchTable *tabRec =
72 static_cast<SwitchTable*>(oatNew(cUnit, sizeof(SwitchTable), true, kAllocData));
buzbeeefc63692012-11-14 16:31:52 -080073 tabRec->table = table;
74 tabRec->vaddr = cUnit->currentDalvikOffset;
75 int elements = table[1];
buzbeecbd6d442012-11-17 14:11:25 -080076 tabRec->targets =
77 static_cast<LIR**>(oatNew(cUnit, elements * sizeof(LIR*), true, kAllocLIR));
78 oatInsertGrowableList(cUnit, &cUnit->switchTables, reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -080079
80 // The table is composed of 8-byte key/disp pairs
81 int byteSize = elements * 8;
82
83 int sizeHi = byteSize >> 16;
84 int sizeLo = byteSize & 0xffff;
85
86 int rEnd = oatAllocTemp(cUnit);
87 if (sizeHi) {
88 newLIR2(cUnit, kMipsLui, rEnd, sizeHi);
89 }
90 // Must prevent code motion for the curr pc pair
91 genBarrier(cUnit); // Scheduling barrier
92 newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8
93 // Now, fill the branch delay slot
94 if (sizeHi) {
95 newLIR3(cUnit, kMipsOri, rEnd, rEnd, sizeLo);
96 } else {
97 newLIR3(cUnit, kMipsOri, rEnd, r_ZERO, sizeLo);
98 }
99 genBarrier(cUnit); // Scheduling barrier
100
101 // Construct BaseLabel and set up table base register
102 LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel);
103 // Remember base label so offsets can be computed later
104 tabRec->anchor = baseLabel;
105 int rBase = oatAllocTemp(cUnit);
buzbeecbd6d442012-11-17 14:11:25 -0800106 newLIR4(cUnit, kMipsDelta, rBase, 0, reinterpret_cast<uintptr_t>(baseLabel),
107 reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -0800108 opRegRegReg(cUnit, kOpAdd, rEnd, rEnd, rBase);
109
110 // Grab switch test value
111 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
112
113 // Test loop
114 int rKey = oatAllocTemp(cUnit);
115 LIR* loopLabel = newLIR0(cUnit, kPseudoTargetLabel);
116 LIR* exitBranch = opCmpBranch(cUnit , kCondEq, rBase, rEnd, NULL);
117 loadWordDisp(cUnit, rBase, 0, rKey);
118 opRegImm(cUnit, kOpAdd, rBase, 8);
119 opCmpBranch(cUnit, kCondNe, rlSrc.lowReg, rKey, loopLabel);
120 int rDisp = oatAllocTemp(cUnit);
121 loadWordDisp(cUnit, rBase, -4, rDisp);
122 opRegRegReg(cUnit, kOpAdd, r_RA, r_RA, rDisp);
123 opReg(cUnit, kOpBx, r_RA);
124
125 // Loop exit
126 LIR* exitLabel = newLIR0(cUnit, kPseudoTargetLabel);
127 exitBranch->target = exitLabel;
128}
129
130/*
131 * Code pattern will look something like:
132 *
133 * lw rVal
134 * jal BaseLabel ; stores "return address" (BaseLabel) in r_RA
135 * nop ; opportunistically fill
136 * [subiu rVal, bias] ; Remove bias if lowVal != 0
137 * bound check -> done
138 * lw rDisp, [r_RA, rVal]
139 * addu r_RA, rDisp
140 * jr r_RA
141 * done:
142 */
143void genPackedSwitch(CompilationUnit* cUnit, uint32_t tableOffset,
144 RegLocation rlSrc)
145{
buzbeeeaf09bc2012-11-15 14:51:41 -0800146 const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
buzbeeefc63692012-11-14 16:31:52 -0800147 if (cUnit->printMe) {
148 dumpPackedSwitchTable(table);
149 }
150 // Add the table to the list - we'll process it later
buzbeecbd6d442012-11-17 14:11:25 -0800151 SwitchTable *tabRec =
152 static_cast<SwitchTable*>(oatNew(cUnit, sizeof(SwitchTable), true, kAllocData));
buzbeeefc63692012-11-14 16:31:52 -0800153 tabRec->table = table;
154 tabRec->vaddr = cUnit->currentDalvikOffset;
155 int size = table[1];
buzbeecbd6d442012-11-17 14:11:25 -0800156 tabRec->targets = static_cast<LIR**>(oatNew(cUnit, size * sizeof(LIR*), true, kAllocLIR));
157 oatInsertGrowableList(cUnit, &cUnit->switchTables, reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -0800158
159 // Get the switch value
160 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
161
162 // Prepare the bias. If too big, handle 1st stage here
163 int lowKey = s4FromSwitchData(&table[2]);
164 bool largeBias = false;
165 int rKey;
166 if (lowKey == 0) {
167 rKey = rlSrc.lowReg;
168 } else if ((lowKey & 0xffff) != lowKey) {
169 rKey = oatAllocTemp(cUnit);
170 loadConstant(cUnit, rKey, lowKey);
171 largeBias = true;
172 } else {
173 rKey = oatAllocTemp(cUnit);
174 }
175
176 // Must prevent code motion for the curr pc pair
177 genBarrier(cUnit);
178 newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8
179 // Now, fill the branch delay slot with bias strip
180 if (lowKey == 0) {
181 newLIR0(cUnit, kMipsNop);
182 } else {
183 if (largeBias) {
184 opRegRegReg(cUnit, kOpSub, rKey, rlSrc.lowReg, rKey);
185 } else {
186 opRegRegImm(cUnit, kOpSub, rKey, rlSrc.lowReg, lowKey);
187 }
188 }
189 genBarrier(cUnit); // Scheduling barrier
190
191 // Construct BaseLabel and set up table base register
192 LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel);
193 // Remember base label so offsets can be computed later
194 tabRec->anchor = baseLabel;
195
196 // Bounds check - if < 0 or >= size continue following switch
197 LIR* branchOver = opCmpImmBranch(cUnit, kCondHi, rKey, size-1, NULL);
198
199 // Materialize the table base pointer
200 int rBase = oatAllocTemp(cUnit);
buzbeecbd6d442012-11-17 14:11:25 -0800201 newLIR4(cUnit, kMipsDelta, rBase, 0, reinterpret_cast<uintptr_t>(baseLabel),
202 reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -0800203
204 // Load the displacement from the switch table
205 int rDisp = oatAllocTemp(cUnit);
206 loadBaseIndexed(cUnit, rBase, rKey, rDisp, 2, kWord);
207
208 // Add to r_AP and go
209 opRegRegReg(cUnit, kOpAdd, r_RA, r_RA, rDisp);
210 opReg(cUnit, kOpBx, r_RA);
211
212 /* branchOver target here */
213 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800214 branchOver->target = target;
buzbeeefc63692012-11-14 16:31:52 -0800215}
216
217/*
218 * Array data table format:
219 * ushort ident = 0x0300 magic value
220 * ushort width width of each element in the table
221 * uint size number of elements in the table
222 * ubyte data[size*width] table of data values (may contain a single-byte
223 * padding at the end)
224 *
225 * Total size is 4+(width * size + 1)/2 16-bit code units.
226 */
227void genFillArrayData(CompilationUnit* cUnit, uint32_t tableOffset,
228 RegLocation rlSrc)
229{
buzbeeeaf09bc2012-11-15 14:51:41 -0800230 const uint16_t* table = cUnit->insns + cUnit->currentDalvikOffset + tableOffset;
buzbeeefc63692012-11-14 16:31:52 -0800231 // Add the table to the list - we'll process it later
buzbeecbd6d442012-11-17 14:11:25 -0800232 FillArrayData *tabRec =
233 reinterpret_cast<FillArrayData*>(oatNew(cUnit, sizeof(FillArrayData), true, kAllocData));
buzbeeefc63692012-11-14 16:31:52 -0800234 tabRec->table = table;
235 tabRec->vaddr = cUnit->currentDalvikOffset;
buzbeeeaf09bc2012-11-15 14:51:41 -0800236 uint16_t width = tabRec->table[1];
237 uint32_t size = tabRec->table[2] | ((static_cast<uint32_t>(tabRec->table[3])) << 16);
buzbeeefc63692012-11-14 16:31:52 -0800238 tabRec->size = (size * width) + 8;
239
buzbeecbd6d442012-11-17 14:11:25 -0800240 oatInsertGrowableList(cUnit, &cUnit->fillArrayData, reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -0800241
242 // Making a call - use explicit registers
243 oatFlushAllRegs(cUnit); /* Everything to home location */
244 oatLockCallTemps(cUnit);
245 loadValueDirectFixed(cUnit, rlSrc, rMIPS_ARG0);
246
247 // Must prevent code motion for the curr pc pair
248 genBarrier(cUnit);
249 newLIR0(cUnit, kMipsCurrPC); // Really a jal to .+8
250 // Now, fill the branch delay slot with the helper load
251 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pHandleFillArrayDataFromCode));
252 genBarrier(cUnit); // Scheduling barrier
253
254 // Construct BaseLabel and set up table base register
255 LIR* baseLabel = newLIR0(cUnit, kPseudoTargetLabel);
256
257 // Materialize a pointer to the fill data image
buzbeecbd6d442012-11-17 14:11:25 -0800258 newLIR4(cUnit, kMipsDelta, rMIPS_ARG1, 0, reinterpret_cast<uintptr_t>(baseLabel),
259 reinterpret_cast<uintptr_t>(tabRec));
buzbeeefc63692012-11-14 16:31:52 -0800260
261 // And go...
262 oatClobberCalleeSave(cUnit);
263 LIR* callInst = opReg(cUnit, kOpBlx, rTgt); // ( array*, fill_data* )
264 markSafepointPC(cUnit, callInst);
265}
266
267/*
268 * TODO: implement fast path to short-circuit thin-lock case
269 */
270void genMonitorEnter(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc)
271{
272 oatFlushAllRegs(cUnit);
273 loadValueDirectFixed(cUnit, rlSrc, rMIPS_ARG0); // Get obj
274 oatLockCallTemps(cUnit); // Prepare for explicit register usage
275 genNullCheck(cUnit, rlSrc.sRegLow, rMIPS_ARG0, optFlags);
276 // Go expensive route - artLockObjectFromCode(self, obj);
277 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pLockObjectFromCode));
278 oatClobberCalleeSave(cUnit);
279 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
280 markSafepointPC(cUnit, callInst);
281}
282
283/*
284 * TODO: implement fast path to short-circuit thin-lock case
285 */
286void genMonitorExit(CompilationUnit* cUnit, int optFlags, RegLocation rlSrc)
287{
288 oatFlushAllRegs(cUnit);
289 loadValueDirectFixed(cUnit, rlSrc, rMIPS_ARG0); // Get obj
290 oatLockCallTemps(cUnit); // Prepare for explicit register usage
291 genNullCheck(cUnit, rlSrc.sRegLow, rMIPS_ARG0, optFlags);
292 // Go expensive route - UnlockObjectFromCode(obj);
293 int rTgt = loadHelper(cUnit, ENTRYPOINT_OFFSET(pUnlockObjectFromCode));
294 oatClobberCalleeSave(cUnit);
295 LIR* callInst = opReg(cUnit, kOpBlx, rTgt);
296 markSafepointPC(cUnit, callInst);
297}
298
299/*
300 * Mark garbage collection card. Skip if the value we're storing is null.
301 */
302void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg)
303{
304 int regCardBase = oatAllocTemp(cUnit);
305 int regCardNo = oatAllocTemp(cUnit);
306 LIR* branchOver = opCmpImmBranch(cUnit, kCondEq, valReg, 0, NULL);
307 loadWordDisp(cUnit, rMIPS_SELF, Thread::CardTableOffset().Int32Value(), regCardBase);
308 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, CardTable::kCardShift);
309 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
310 kUnsignedByte);
311 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800312 branchOver->target = target;
buzbeeefc63692012-11-14 16:31:52 -0800313 oatFreeTemp(cUnit, regCardBase);
314 oatFreeTemp(cUnit, regCardNo);
315}
316void genEntrySequence(CompilationUnit* cUnit, RegLocation* argLocs,
317 RegLocation rlMethod)
318{
319 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
320 /*
321 * On entry, rMIPS_ARG0, rMIPS_ARG1, rMIPS_ARG2 & rMIPS_ARG3 are live. Let the register
322 * allocation mechanism know so it doesn't try to use any of them when
323 * expanding the frame or flushing. This leaves the utility
324 * code with a single temp: r12. This should be enough.
325 */
326 oatLockTemp(cUnit, rMIPS_ARG0);
327 oatLockTemp(cUnit, rMIPS_ARG1);
328 oatLockTemp(cUnit, rMIPS_ARG2);
329 oatLockTemp(cUnit, rMIPS_ARG3);
330
331 /*
332 * We can safely skip the stack overflow check if we're
333 * a leaf *and* our frame size < fudge factor.
334 */
335 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
buzbeecbd6d442012-11-17 14:11:25 -0800336 (static_cast<size_t>(cUnit->frameSize) < Thread::kStackOverflowReservedBytes));
buzbeeefc63692012-11-14 16:31:52 -0800337 newLIR0(cUnit, kPseudoMethodEntry);
338 int checkReg = oatAllocTemp(cUnit);
339 int newSP = oatAllocTemp(cUnit);
340 if (!skipOverflowCheck) {
341 /* Load stack limit */
342 loadWordDisp(cUnit, rMIPS_SELF, Thread::StackEndOffset().Int32Value(), checkReg);
343 }
344 /* Spill core callee saves */
345 spillCoreRegs(cUnit);
346 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
347 DCHECK_EQ(cUnit->numFPSpills, 0);
348 if (!skipOverflowCheck) {
349 opRegRegImm(cUnit, kOpSub, newSP, rMIPS_SP, cUnit->frameSize - (spillCount * 4));
350 genRegRegCheck(cUnit, kCondCc, newSP, checkReg, kThrowStackOverflow);
351 opRegCopy(cUnit, rMIPS_SP, newSP); // Establish stack
352 } else {
353 opRegImm(cUnit, kOpSub, rMIPS_SP, cUnit->frameSize - (spillCount * 4));
354 }
355
356 flushIns(cUnit, argLocs, rlMethod);
357
358 oatFreeTemp(cUnit, rMIPS_ARG0);
359 oatFreeTemp(cUnit, rMIPS_ARG1);
360 oatFreeTemp(cUnit, rMIPS_ARG2);
361 oatFreeTemp(cUnit, rMIPS_ARG3);
362}
363
364void genExitSequence(CompilationUnit* cUnit)
365{
366 /*
367 * In the exit path, rMIPS_RET0/rMIPS_RET1 are live - make sure they aren't
368 * allocated by the register utilities as temps.
369 */
370 oatLockTemp(cUnit, rMIPS_RET0);
371 oatLockTemp(cUnit, rMIPS_RET1);
372
373 newLIR0(cUnit, kPseudoMethodExit);
374 unSpillCoreRegs(cUnit);
375 opReg(cUnit, kOpBx, r_RA);
376}
377
378} // namespace art