blob: eec1cbd375d5a169c1bef09eaa5c42042ec8042a [file] [log] [blame]
buzbeee88dfbf2012-03-05 11:19:57 -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 x86-specific codegen factory support.
19 * It is included by
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 */
24
25namespace art {
26
27bool genAddLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
28 RegLocation rlSrc1, RegLocation rlSrc2)
29{
buzbeee88dfbf2012-03-05 11:19:57 -080030 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
31 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
32 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
33 /*
34 * [v1 v0] = [a1 a0] + [a3 a2];
Ian Rogersf7d9ad32012-03-13 18:45:39 -070035 * add v0,a2,a0
36 * adc v1,a3,a1
buzbeee88dfbf2012-03-05 11:19:57 -080037 */
38
39 opRegRegReg(cUnit, kOpAdd, rlResult.lowReg, rlSrc2.lowReg, rlSrc1.lowReg);
Ian Rogersf7d9ad32012-03-13 18:45:39 -070040 opRegRegReg(cUnit, kOpAdc, rlResult.highReg, rlSrc2.highReg, rlSrc1.highReg);
buzbeee88dfbf2012-03-05 11:19:57 -080041 storeValueWide(cUnit, rlDest, rlResult);
buzbeee88dfbf2012-03-05 11:19:57 -080042 return false;
43}
44
45bool genSubLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
46 RegLocation rlSrc1, RegLocation rlSrc2)
47{
buzbeee88dfbf2012-03-05 11:19:57 -080048 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
49 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
50 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
51 /*
52 * [v1 v0] = [a1 a0] - [a3 a2];
Ian Rogersf7d9ad32012-03-13 18:45:39 -070053 * sub v0,a0,a2
54 * sbb v1,a1,a3
buzbeee88dfbf2012-03-05 11:19:57 -080055 */
56
57 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
Ian Rogersf7d9ad32012-03-13 18:45:39 -070058 opRegRegReg(cUnit, kOpSbc, rlResult.highReg, rlSrc1.highReg, rlSrc2.highReg);
buzbeee88dfbf2012-03-05 11:19:57 -080059 storeValueWide(cUnit, rlDest, rlResult);
buzbeee88dfbf2012-03-05 11:19:57 -080060 return false;
61}
62
63bool genNegLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
64 RegLocation rlSrc)
65{
66 UNIMPLEMENTED(WARNING) << "genNegLong";
67#if 0
68 rlSrc = loadValueWide(cUnit, rlSrc, kCoreReg);
69 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
70 /*
71 * [v1 v0] = -[a1 a0]
72 * negu v0,a0
73 * negu v1,a1
74 * sltu t1,r_zero
75 * subu v1,v1,t1
76 */
77
78 opRegReg(cUnit, kOpNeg, rlResult.lowReg, rlSrc.lowReg);
79 opRegReg(cUnit, kOpNeg, rlResult.highReg, rlSrc.highReg);
80 int tReg = oatAllocTemp(cUnit);
buzbeea7678db2012-03-05 15:35:46 -080081 newLIR3(cUnit, kX86Sltu, tReg, r_ZERO, rlResult.lowReg);
buzbeee88dfbf2012-03-05 11:19:57 -080082 opRegRegReg(cUnit, kOpSub, rlResult.highReg, rlResult.highReg, tReg);
83 oatFreeTemp(cUnit, tReg);
84 storeValueWide(cUnit, rlDest, rlResult);
85#endif
86 return false;
87}
88
89void genDebuggerUpdate(CompilationUnit* cUnit, int32_t offset);
90
Ian Rogersf7d9ad32012-03-13 18:45:39 -070091void spillCoreRegs(CompilationUnit* cUnit) {
92 if (cUnit->numCoreSpills == 0) {
93 return;
94 }
95 // Spill mask not including fake return address register
96 uint32_t mask = cUnit->coreSpillMask & ~(1 << rRET);
97 int offset = cUnit->frameSize - 4;
98 for (int reg = 0; mask; mask >>= 1, reg++) {
99 if (mask & 0x1) {
100 offset -= 4;
101 storeWordDisp(cUnit, rSP, offset, reg);
buzbeee88dfbf2012-03-05 11:19:57 -0800102 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700103 }
buzbeee88dfbf2012-03-05 11:19:57 -0800104}
105
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700106void unSpillCoreRegs(CompilationUnit* cUnit) {
107 if (cUnit->numCoreSpills == 0) {
108 return;
109 }
110 // Spill mask not including fake return address register
111 uint32_t mask = cUnit->coreSpillMask & ~(1 << rRET);
112 int offset = cUnit->frameSize - 4;
113 for (int reg = 0; mask; mask >>= 1, reg++) {
114 if (mask & 0x1) {
115 offset -= 4;
116 loadWordDisp(cUnit, rSP, offset, reg);
buzbeee88dfbf2012-03-05 11:19:57 -0800117 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700118 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800119}
120
121void opRegThreadMem(CompilationUnit* cUnit, OpKind op, int rDest, int threadOffset) {
122 X86OpCode opcode = kX86Bkpt;
123 switch (op) {
124 case kOpCmp: opcode = kX86Cmp32RT; break;
125 default:
126 LOG(FATAL) << "Bad opcode: " << op;
127 break;
128 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800129 newLIR2(cUnit, opcode, rDest, threadOffset);
buzbeee88dfbf2012-03-05 11:19:57 -0800130}
131
132void genEntrySequence(CompilationUnit* cUnit, BasicBlock* bb)
133{
buzbeee88dfbf2012-03-05 11:19:57 -0800134 /*
Ian Rogersb5d09b22012-03-06 22:14:17 -0800135 * On entry, rARG0, rARG1, rARG2 are live. Let the register
buzbeee88dfbf2012-03-05 11:19:57 -0800136 * allocation mechanism know so it doesn't try to use any of them when
137 * expanding the frame or flushing. This leaves the utility
Ian Rogersb5d09b22012-03-06 22:14:17 -0800138 * code with no spare temps.
buzbeee88dfbf2012-03-05 11:19:57 -0800139 */
140 oatLockTemp(cUnit, rARG0);
141 oatLockTemp(cUnit, rARG1);
142 oatLockTemp(cUnit, rARG2);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800143
144 /* Build frame, return address already on stack */
145 opRegImm(cUnit, kOpSub, rSP, cUnit->frameSize - 4);
buzbeee88dfbf2012-03-05 11:19:57 -0800146
147 /*
148 * We can safely skip the stack overflow check if we're
149 * a leaf *and* our frame size < fudge factor.
150 */
151 bool skipOverflowCheck = ((cUnit->attrs & METHOD_IS_LEAF) &&
152 ((size_t)cUnit->frameSize <
153 Thread::kStackOverflowReservedBytes));
154 newLIR0(cUnit, kPseudoMethodEntry);
buzbeee88dfbf2012-03-05 11:19:57 -0800155 /* Spill core callee saves */
156 spillCoreRegs(cUnit);
157 /* NOTE: promotion of FP regs currently unsupported, thus no FP spill */
158 DCHECK_EQ(cUnit->numFPSpills, 0);
159 if (!skipOverflowCheck) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800160 // cmp rSP, fs:[stack_end_]; jcc throw_launchpad
161 LIR* tgt = rawLIR(cUnit, 0, kPseudoThrowTarget, kThrowStackOverflow, 0, 0, 0, 0);
162 opRegThreadMem(cUnit, kOpCmp, rSP, Thread::StackEndOffset().Int32Value());
163 opCondBranch(cUnit, kCondUlt, tgt);
164 // Remember branch target - will process later
165 oatInsertGrowableList(cUnit, &cUnit->throwLaunchpads, (intptr_t)tgt);
buzbeee88dfbf2012-03-05 11:19:57 -0800166 }
buzbee9c044ce2012-03-18 13:24:07 -0700167
buzbeee88dfbf2012-03-05 11:19:57 -0800168 flushIns(cUnit);
169
170 if (cUnit->genDebugger) {
171 // Refresh update debugger callout
Ian Rogersb5d09b22012-03-06 22:14:17 -0800172 UNIMPLEMENTED(WARNING) << "genDebugger";
173#if 0
buzbeee88dfbf2012-03-05 11:19:57 -0800174 loadWordDisp(cUnit, rSELF,
175 OFFSETOF_MEMBER(Thread, pUpdateDebuggerFromCode), rSUSPEND);
176 genDebuggerUpdate(cUnit, DEBUGGER_METHOD_ENTRY);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800177#endif
buzbeee88dfbf2012-03-05 11:19:57 -0800178 }
179
180 oatFreeTemp(cUnit, rARG0);
181 oatFreeTemp(cUnit, rARG1);
182 oatFreeTemp(cUnit, rARG2);
buzbeee88dfbf2012-03-05 11:19:57 -0800183}
184
Ian Rogersb5d09b22012-03-06 22:14:17 -0800185void genExitSequence(CompilationUnit* cUnit, BasicBlock* bb) {
186 /*
187 * In the exit path, rRET0/rRET1 are live - make sure they aren't
188 * allocated by the register utilities as temps.
189 */
190 oatLockTemp(cUnit, rRET0);
191 oatLockTemp(cUnit, rRET1);
buzbeee88dfbf2012-03-05 11:19:57 -0800192
Ian Rogersb5d09b22012-03-06 22:14:17 -0800193 newLIR0(cUnit, kPseudoMethodExit);
194 /* If we're compiling for the debugger, generate an update callout */
195 if (cUnit->genDebugger) {
196 genDebuggerUpdate(cUnit, DEBUGGER_METHOD_EXIT);
197 }
198 unSpillCoreRegs(cUnit);
199 /* Remove frame except for return address */
200 opRegImm(cUnit, kOpAdd, rSP, cUnit->frameSize - 4);
201 newLIR0(cUnit, kX86Ret);
buzbeee88dfbf2012-03-05 11:19:57 -0800202}
203
204/*
205 * Nop any unconditional branches that go to the next instruction.
206 * Note: new redundant branches may be inserted later, and we'll
207 * use a check in final instruction assembly to nop those out.
208 */
Ian Rogersb5d09b22012-03-06 22:14:17 -0800209void removeRedundantBranches(CompilationUnit* cUnit) {
210 LIR* thisLIR;
buzbeee88dfbf2012-03-05 11:19:57 -0800211
Ian Rogersb5d09b22012-03-06 22:14:17 -0800212 for (thisLIR = (LIR*) cUnit->firstLIRInsn;
213 thisLIR != (LIR*) cUnit->lastLIRInsn;
214 thisLIR = NEXT_LIR(thisLIR)) {
buzbeee88dfbf2012-03-05 11:19:57 -0800215
Ian Rogersb5d09b22012-03-06 22:14:17 -0800216 /* Branch to the next instruction */
Ian Rogersb41b33b2012-03-20 14:22:54 -0700217 if (thisLIR->opcode == kX86Jmp8 || thisLIR->opcode == kX86Jmp32) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800218 LIR* nextLIR = thisLIR;
buzbeee88dfbf2012-03-05 11:19:57 -0800219
Ian Rogersb5d09b22012-03-06 22:14:17 -0800220 while (true) {
221 nextLIR = NEXT_LIR(nextLIR);
buzbeee88dfbf2012-03-05 11:19:57 -0800222
Ian Rogersb5d09b22012-03-06 22:14:17 -0800223 /*
224 * Is the branch target the next instruction?
225 */
226 if (nextLIR == (LIR*) thisLIR->target) {
227 thisLIR->flags.isNop = true;
228 break;
buzbeee88dfbf2012-03-05 11:19:57 -0800229 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800230
231 /*
232 * Found real useful stuff between the branch and the target.
233 * Need to explicitly check the lastLIRInsn here because it
234 * might be the last real instruction.
235 */
236 if (!isPseudoOpcode(nextLIR->opcode) ||
237 (nextLIR = (LIR*) cUnit->lastLIRInsn))
238 break;
239 }
buzbeee88dfbf2012-03-05 11:19:57 -0800240 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800241 }
buzbeee88dfbf2012-03-05 11:19:57 -0800242}
243
244
245/* Common initialization routine for an architecture family */
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700246bool oatArchInit() {
247 int i;
buzbeee88dfbf2012-03-05 11:19:57 -0800248
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700249 for (i = 0; i < kX86Last; i++) {
250 if (EncodingMap[i].opcode != i) {
251 LOG(FATAL) << "Encoding order for " << EncodingMap[i].name
252 << " is wrong: expecting " << i << ", seeing " << (int)EncodingMap[i].opcode;
buzbeee88dfbf2012-03-05 11:19:57 -0800253 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700254 }
buzbeee88dfbf2012-03-05 11:19:57 -0800255
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700256 return oatArchVariantInit();
buzbeee88dfbf2012-03-05 11:19:57 -0800257}
258
259} // namespace art