blob: 5e41400b4c71d62c5fb5fb18b99b88e5162d42b8 [file] [log] [blame]
buzbeee3acd072012-02-25 17:03:10 -08001/*
2 * Copyright (C) 2011 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/*
18 * This file contains mips-specific codegen factory support.
19 * It is included by
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 */
24
Ian Rogers57b86d42012-03-27 16:05:41 -070025#include "oat/runtime/oat_support_entrypoints.h"
26
buzbeee3acd072012-02-25 17:03:10 -080027namespace art {
28
buzbeec5159d52012-03-03 11:48:39 -080029bool genAddLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
30 RegLocation rlSrc1, RegLocation rlSrc2)
31{
32 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
33 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
34 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
35 /*
36 * [v1 v0] = [a1 a0] + [a3 a2];
37 * addu v0,a2,a0
38 * addu t1,a3,a1
39 * sltu v1,v0,a2
40 * addu v1,v1,t1
41 */
42
43 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc2.lowReg, rlSrc1.lowReg);
44 int tReg = oatAllocTemp(cUnit);
45 opRegRegReg(cUnit, kOpAdd, tReg, rlSrc2.highReg, rlSrc1.highReg);
46 newLIR3(cUnit, kMipsSltu, rlResult.highReg, rlResult.lowReg, rlSrc2.lowReg);
47 opRegRegReg(cUnit, kOpAdd, rlResult.highReg, rlResult.highReg, tReg);
48 oatFreeTemp(cUnit, tReg);
49 storeValueWide(cUnit, rlDest, rlResult);
50 return false;
51}
52
53bool genSubLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
54 RegLocation rlSrc1, RegLocation rlSrc2)
55{
56 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
57 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
58 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
59 /*
60 * [v1 v0] = [a1 a0] - [a3 a2];
61 * subu v0,a0,a2
62 * subu v1,a1,a3
63 * sltu t1,a0,v0
64 * subu v1,v1,t1
65 */
66
67 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
68 opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlSrc1.highReg, rlSrc2.highReg);
69 int tReg = oatAllocTemp(cUnit);
70 newLIR3(cUnit, kMipsSltu, tReg, rlSrc1.lowReg, rlResult.lowReg);
71 opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlResult.highReg, tReg);
72 oatFreeTemp(cUnit, tReg);
73 storeValueWide(cUnit, rlDest, rlResult);
74 return false;
75}
76
77bool genNegLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
78 RegLocation rlSrc)
79{
80 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
81 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
82 /*
83 * [v1 v0] = -[a1 a0]
84 * negu v0,a0
85 * negu v1,a1
86 * sltu t1,r_zero
87 * subu v1,v1,t1
88 */
89
90 opRegReg(cUnit, kOpNeg, rlResult.lowReg, rlSrc.lowReg);
91 opRegReg(cUnit, kOpNeg, rlResult.highReg, rlSrc.highReg);
92 int tReg = oatAllocTemp(cUnit);
93 newLIR3(cUnit, kMipsSltu, tReg, r_ZERO, rlResult.lowReg);
94 opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlResult.highReg, tReg);
95 oatFreeTemp(cUnit, tReg);
96 storeValueWide(cUnit, rlDest, rlResult);
97 return false;
98}
99
buzbee5de34942012-03-01 14:51:57 -0800100void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset);
buzbeee3acd072012-02-25 17:03:10 -0800101
102/*
buzbee5de34942012-03-01 14:51:57 -0800103 * In the Arm code a it is typical to use the link register
104 * to hold the target address. However, for Mips we must
105 * ensure that all branch instructions can be restarted if
106 * there is a trap in the shadow. Allocate a temp register.
buzbeee3acd072012-02-25 17:03:10 -0800107 */
buzbee5de34942012-03-01 14:51:57 -0800108int loadHelper(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800109{
buzbee5de34942012-03-01 14:51:57 -0800110 int tReg = oatAllocTemp(cUnit);
111 loadWordDisp(cUnit, rSELF, offset, tReg);
112 return tReg;
buzbeee3acd072012-02-25 17:03:10 -0800113}
114
buzbee5de34942012-03-01 14:51:57 -0800115void spillCoreRegs(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800116{
buzbee5de34942012-03-01 14:51:57 -0800117 if (cUnit->numCoreSpills == 0) {
buzbeee3acd072012-02-25 17:03:10 -0800118 return;
buzbee5de34942012-03-01 14:51:57 -0800119 }
120 uint32_t mask = cUnit->coreSpillMask;
121 int offset = cUnit->numCoreSpills * 4;
122 opRegImm(cUnit, kOpSub, rSP, offset);
123 for (int reg = 0; mask; mask >>= 1, reg++) {
124 if (mask & 0x1) {
125 offset -= 4;
126 storeWordDisp(cUnit, rSP, offset, reg);
buzbeee3acd072012-02-25 17:03:10 -0800127 }
128 }
buzbeee3acd072012-02-25 17:03:10 -0800129}
130
buzbee5de34942012-03-01 14:51:57 -0800131void unSpillCoreRegs(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800132{
buzbee5de34942012-03-01 14:51:57 -0800133 if (cUnit->numCoreSpills == 0) {
134 return;
135 }
136 uint32_t mask = cUnit->coreSpillMask;
137 int offset = cUnit->frameSize;
138 for (int reg = 0; mask; mask >>= 1, reg++) {
139 if (mask & 0x1) {
140 offset -= 4;
141 loadWordDisp(cUnit, rSP, offset, reg);
142 }
143 }
144 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize);
145}
146
147void genEntrySequence(CompilationUnit* cUnit, BasicBlock* bb)
148{
buzbeee3acd072012-02-25 17:03:10 -0800149 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
150 /*
buzbee5de34942012-03-01 14:51:57 -0800151 * On entry, rARG0, rARG1, rARG2 & rARG3 are live. Let the register
152 * allocation mechanism know so it doesn't try to use any of them when
buzbeee3acd072012-02-25 17:03:10 -0800153 * expanding the frame or flushing. This leaves the utility
154 * code with a single temp: r12. This should be enough.
155 */
buzbee5de34942012-03-01 14:51:57 -0800156 oatLockTemp(cUnit, rARG0);
157 oatLockTemp(cUnit, rARG1);
158 oatLockTemp(cUnit, rARG2);
159 oatLockTemp(cUnit, rARG3);
buzbeee3acd072012-02-25 17:03:10 -0800160
161 /*
162 * We can safely skip the stack overflow check if we're
163 * a leaf *and* our frame size < fudge factor.
164 */
165 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
166 ((size_t)cUnit->frameSize <
167 Thread::kStackOverflowReservedBytes));
buzbee31a4a6f2012-02-28 15:36:15 -0800168 newLIR0(cUnit, kPseudoMethodEntry);
buzbee5de34942012-03-01 14:51:57 -0800169 int checkReg = oatAllocTemp(cUnit);
170 int newSP = oatAllocTemp(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800171 if (!skipOverflowCheck) {
172 /* Load stack limit */
173 loadWordDisp(cUnit, rSELF,
buzbee5de34942012-03-01 14:51:57 -0800174 Thread::StackEndOffset().Int32Value(), checkReg);
buzbeee3acd072012-02-25 17:03:10 -0800175 }
176 /* Spill core callee saves */
buzbee5de34942012-03-01 14:51:57 -0800177 spillCoreRegs(cUnit);
178 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
179 DCHECK_EQ(cUnit->numFPSpills, 0);
buzbeee3acd072012-02-25 17:03:10 -0800180 if (!skipOverflowCheck) {
buzbee5de34942012-03-01 14:51:57 -0800181 opRegRegImm(cUnit, kOpSub, newSP, rSP,
buzbeee3acd072012-02-25 17:03:10 -0800182 cUnit->frameSize - (spillCount * 4));
buzbee5de34942012-03-01 14:51:57 -0800183 genRegRegCheck(cUnit, kCondCc, newSP, checkReg, NULL,
184 kThrowStackOverflow);
buzbee82488f52012-03-02 08:20:26 -0800185 opRegCopy(cUnit, rSP, newSP); // Establish stack
buzbeee3acd072012-02-25 17:03:10 -0800186 } else {
187 opRegImm(cUnit, kOpSub, rSP,
188 cUnit->frameSize - (spillCount * 4));
189 }
buzbee9c044ce2012-03-18 13:24:07 -0700190
buzbeee3acd072012-02-25 17:03:10 -0800191 flushIns(cUnit);
192
193 if (cUnit->genDebugger) {
194 // Refresh update debugger callout
195 loadWordDisp(cUnit, rSELF,
Ian Rogers57b86d42012-03-27 16:05:41 -0700196 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode), rSUSPEND);
buzbeee3acd072012-02-25 17:03:10 -0800197 genDebuggerUpdate(cUnit, DEBUGGER_METHOD_ENTRY);
198 }
199
buzbee5de34942012-03-01 14:51:57 -0800200 oatFreeTemp(cUnit, rARG0);
201 oatFreeTemp(cUnit, rARG1);
202 oatFreeTemp(cUnit, rARG2);
203 oatFreeTemp(cUnit, rARG3);
buzbeee3acd072012-02-25 17:03:10 -0800204}
205
buzbee5de34942012-03-01 14:51:57 -0800206void genExitSequence(CompilationUnit* cUnit, BasicBlock* bb)
buzbeee3acd072012-02-25 17:03:10 -0800207{
buzbeee3acd072012-02-25 17:03:10 -0800208 /*
buzbee5de34942012-03-01 14:51:57 -0800209 * In the exit path, rRET0/rRET1 are live - make sure they aren't
buzbeee3acd072012-02-25 17:03:10 -0800210 * allocated by the register utilities as temps.
211 */
buzbee5de34942012-03-01 14:51:57 -0800212 oatLockTemp(cUnit, rRET0);
213 oatLockTemp(cUnit, rRET1);
buzbeee3acd072012-02-25 17:03:10 -0800214
buzbee31a4a6f2012-02-28 15:36:15 -0800215 newLIR0(cUnit, kPseudoMethodExit);
buzbeee3acd072012-02-25 17:03:10 -0800216 /* If we're compiling for the debugger, generate an update callout */
217 if (cUnit->genDebugger) {
218 genDebuggerUpdate(cUnit, DEBUGGER_METHOD_EXIT);
219 }
buzbee5de34942012-03-01 14:51:57 -0800220 unSpillCoreRegs(cUnit);
buzbee0398c422012-03-02 15:22:47 -0800221 opReg(cUnit, kOpBx, r_RA);
buzbeee3acd072012-02-25 17:03:10 -0800222}
223
224/*
225 * Nop any unconditional branches that go to the next instruction.
226 * Note: new redundant branches may be inserted later, and we'll
227 * use a check in final instruction assembly to nop those out.
228 */
229void removeRedundantBranches(CompilationUnit* cUnit)
230{
buzbee5de34942012-03-01 14:51:57 -0800231 LIR* thisLIR;
buzbeee3acd072012-02-25 17:03:10 -0800232
buzbee5de34942012-03-01 14:51:57 -0800233 for (thisLIR = (LIR*) cUnit->firstLIRInsn;
234 thisLIR != (LIR*) cUnit->lastLIRInsn;
buzbeee3acd072012-02-25 17:03:10 -0800235 thisLIR = NEXT_LIR(thisLIR)) {
236
237 /* Branch to the next instruction */
buzbee5de34942012-03-01 14:51:57 -0800238 if (thisLIR->opcode == kMipsB) {
239 LIR* nextLIR = thisLIR;
buzbeee3acd072012-02-25 17:03:10 -0800240
241 while (true) {
242 nextLIR = NEXT_LIR(nextLIR);
243
244 /*
245 * Is the branch target the next instruction?
246 */
buzbee5de34942012-03-01 14:51:57 -0800247 if (nextLIR == (LIR*) thisLIR->target) {
buzbeee3acd072012-02-25 17:03:10 -0800248 thisLIR->flags.isNop = true;
249 break;
250 }
251
252 /*
253 * Found real useful stuff between the branch and the target.
254 * Need to explicitly check the lastLIRInsn here because it
255 * might be the last real instruction.
256 */
257 if (!isPseudoOpcode(nextLIR->opcode) ||
buzbee5de34942012-03-01 14:51:57 -0800258 (nextLIR = (LIR*) cUnit->lastLIRInsn))
buzbeee3acd072012-02-25 17:03:10 -0800259 break;
260 }
261 }
262 }
buzbeee3acd072012-02-25 17:03:10 -0800263}
264
buzbeee3acd072012-02-25 17:03:10 -0800265
266/* Common initialization routine for an architecture family */
267bool oatArchInit()
268{
269 int i;
270
271 for (i = 0; i < kMipsLast; i++) {
272 if (EncodingMap[i].opcode != i) {
273 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
274 " is wrong: expecting " << i << ", seeing " <<
275 (int)EncodingMap[i].opcode;
276 }
277 }
278
279 return oatArchVariantInit();
280}
281
buzbeee3acd072012-02-25 17:03:10 -0800282} // namespace art