blob: 83172157bd855cb8a8d327cf4072310971ec63a7 [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{
Bill Buzbeea114add2012-05-03 15:00:40 -070032 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 */
buzbeec5159d52012-03-03 11:48:39 -080042
Bill Buzbeea114add2012-05-03 15:00:40 -070043 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;
buzbeec5159d52012-03-03 11:48:39 -080051}
52
53bool genSubLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
Bill Buzbeea114add2012-05-03 15:00:40 -070054 RegLocation rlSrc1, RegLocation rlSrc2)
buzbeec5159d52012-03-03 11:48:39 -080055{
Bill Buzbeea114add2012-05-03 15:00:40 -070056 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 */
buzbeec5159d52012-03-03 11:48:39 -080066
Bill Buzbeea114add2012-05-03 15:00:40 -070067 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;
buzbeec5159d52012-03-03 11:48:39 -080075}
76
77bool genNegLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
78 RegLocation rlSrc)
79{
Bill Buzbeea114add2012-05-03 15:00:40 -070080 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 */
buzbeec5159d52012-03-03 11:48:39 -080089
Bill Buzbeea114add2012-05-03 15:00:40 -070090 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;
buzbeec5159d52012-03-03 11:48:39 -080098}
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{
Bill Buzbeea114add2012-05-03 15:00:40 -0700110 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{
Bill Buzbeea114add2012-05-03 15:00:40 -0700117 if (cUnit->numCoreSpills == 0) {
118 return;
119 }
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);
buzbee5de34942012-03-01 14:51:57 -0800127 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 }
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{
Bill Buzbeea114add2012-05-03 15:00:40 -0700133 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);
buzbee5de34942012-03-01 14:51:57 -0800142 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700143 }
144 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize);
buzbee5de34942012-03-01 14:51:57 -0800145}
146
147void genEntrySequence(CompilationUnit* cUnit, BasicBlock* bb)
148{
Bill Buzbeea114add2012-05-03 15:00:40 -0700149 int spillCount = cUnit->numCoreSpills + cUnit->numFPSpills;
150 /*
151 * 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
153 * expanding the frame or flushing. This leaves the utility
154 * code with a single temp: r12. This should be enough.
155 */
156 oatLockTemp(cUnit, rARG0);
157 oatLockTemp(cUnit, rARG1);
158 oatLockTemp(cUnit, rARG2);
159 oatLockTemp(cUnit, rARG3);
buzbeee3acd072012-02-25 17:03:10 -0800160
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 /*
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 < Thread::kStackOverflowReservedBytes));
167 newLIR0(cUnit, kPseudoMethodEntry);
168 int checkReg = oatAllocTemp(cUnit);
169 int newSP = oatAllocTemp(cUnit);
170 if (!skipOverflowCheck) {
171 /* Load stack limit */
172 loadWordDisp(cUnit, rSELF, Thread::StackEndOffset().Int32Value(), checkReg);
173 }
174 /* Spill core callee saves */
175 spillCoreRegs(cUnit);
176 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
177 DCHECK_EQ(cUnit->numFPSpills, 0);
178 if (!skipOverflowCheck) {
179 opRegRegImm(cUnit, kOpSub, newSP, rSP, cUnit->frameSize - (spillCount * 4));
180 genRegRegCheck(cUnit, kCondCc, newSP, checkReg, NULL, kThrowStackOverflow);
181 opRegCopy(cUnit, rSP, newSP); // Establish stack
182 } else {
183 opRegImm(cUnit, kOpSub, rSP, cUnit->frameSize - (spillCount * 4));
184 }
buzbee9c044ce2012-03-18 13:24:07 -0700185
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 flushIns(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800187
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 if (cUnit->genDebugger) {
189 // Refresh update debugger callout
190 loadWordDisp(cUnit, rSELF,
191 ENTRYPOINT_OFFSET(pUpdateDebuggerFromCode), rSUSPEND);
192 genDebuggerUpdate(cUnit, DEBUGGER_METHOD_ENTRY);
193 }
buzbeee3acd072012-02-25 17:03:10 -0800194
Bill Buzbeea114add2012-05-03 15:00:40 -0700195 oatFreeTemp(cUnit, rARG0);
196 oatFreeTemp(cUnit, rARG1);
197 oatFreeTemp(cUnit, rARG2);
198 oatFreeTemp(cUnit, rARG3);
buzbeee3acd072012-02-25 17:03:10 -0800199}
200
buzbee5de34942012-03-01 14:51:57 -0800201void genExitSequence(CompilationUnit* cUnit, BasicBlock* bb)
buzbeee3acd072012-02-25 17:03:10 -0800202{
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 /*
204 * In the exit path, rRET0/rRET1 are live - make sure they aren't
205 * allocated by the register utilities as temps.
206 */
207 oatLockTemp(cUnit, rRET0);
208 oatLockTemp(cUnit, rRET1);
buzbeee3acd072012-02-25 17:03:10 -0800209
Bill Buzbeea114add2012-05-03 15:00:40 -0700210 newLIR0(cUnit, kPseudoMethodExit);
211 /* If we're compiling for the debugger, generate an update callout */
212 if (cUnit->genDebugger) {
213 genDebuggerUpdate(cUnit, DEBUGGER_METHOD_EXIT);
214 }
215 unSpillCoreRegs(cUnit);
216 opReg(cUnit, kOpBx, r_RA);
buzbeee3acd072012-02-25 17:03:10 -0800217}
218
219/*
220 * Nop any unconditional branches that go to the next instruction.
221 * Note: new redundant branches may be inserted later, and we'll
222 * use a check in final instruction assembly to nop those out.
223 */
224void removeRedundantBranches(CompilationUnit* cUnit)
225{
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 LIR* thisLIR;
buzbeee3acd072012-02-25 17:03:10 -0800227
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 for (thisLIR = (LIR*) cUnit->firstLIRInsn;
229 thisLIR != (LIR*) cUnit->lastLIRInsn;
230 thisLIR = NEXT_LIR(thisLIR)) {
buzbeee3acd072012-02-25 17:03:10 -0800231
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 /* Branch to the next instruction */
233 if (thisLIR->opcode == kMipsB) {
234 LIR* nextLIR = thisLIR;
buzbeee3acd072012-02-25 17:03:10 -0800235
Bill Buzbeea114add2012-05-03 15:00:40 -0700236 while (true) {
237 nextLIR = NEXT_LIR(nextLIR);
buzbeee3acd072012-02-25 17:03:10 -0800238
Bill Buzbeea114add2012-05-03 15:00:40 -0700239 /*
240 * Is the branch target the next instruction?
241 */
242 if (nextLIR == (LIR*) thisLIR->target) {
243 thisLIR->flags.isNop = true;
244 break;
buzbeee3acd072012-02-25 17:03:10 -0800245 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700246
247 /*
248 * Found real useful stuff between the branch and the target.
249 * Need to explicitly check the lastLIRInsn here because it
250 * might be the last real instruction.
251 */
252 if (!isPseudoOpcode(nextLIR->opcode) ||
253 (nextLIR = (LIR*) cUnit->lastLIRInsn))
254 break;
255 }
buzbeee3acd072012-02-25 17:03:10 -0800256 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700257 }
buzbeee3acd072012-02-25 17:03:10 -0800258}
259
buzbeee3acd072012-02-25 17:03:10 -0800260
261/* Common initialization routine for an architecture family */
262bool oatArchInit()
263{
Bill Buzbeea114add2012-05-03 15:00:40 -0700264 int i;
buzbeee3acd072012-02-25 17:03:10 -0800265
Bill Buzbeea114add2012-05-03 15:00:40 -0700266 for (i = 0; i < kMipsLast; i++) {
267 if (EncodingMap[i].opcode != i) {
268 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name <<
269 " is wrong: expecting " << i << ", seeing " <<
270 (int)EncodingMap[i].opcode;
buzbeee3acd072012-02-25 17:03:10 -0800271 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700272 }
buzbeee3acd072012-02-25 17:03:10 -0800273
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 return oatArchVariantInit();
buzbeee3acd072012-02-25 17:03:10 -0800275}
276
buzbeee3acd072012-02-25 17:03:10 -0800277} // namespace art