blob: fb9bdc922769a1774e92ff8029f31075bde5a3e3 [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#include "object_utils.h"
18
19namespace art {
20
21#define DISPLAY_MISSING_TARGETS (cUnit->enableDebug & \
22 (1 << kDebugDisplayMissingTargets))
23
buzbee31a4a6f2012-02-28 15:36:15 -080024const RegLocation badLoc = {kLocDalvikFrame, 0, 0, 0, 0, 0, 0,
25 INVALID_REG, INVALID_REG, INVALID_SREG};
buzbeee3acd072012-02-25 17:03:10 -080026
27/* Mark register usage state and return long retloc */
Ian Rogersf7d9ad32012-03-13 18:45:39 -070028RegLocation oatGetReturnWide(CompilationUnit* cUnit, bool isDouble)
buzbeee3acd072012-02-25 17:03:10 -080029{
Ian Rogersf7d9ad32012-03-13 18:45:39 -070030 RegLocation gpr_res = LOC_C_RETURN_WIDE;
31 RegLocation fpr_res = LOC_C_RETURN_WIDE_DOUBLE;
32 RegLocation res = isDouble ? fpr_res : gpr_res;
buzbee86a4bce2012-03-06 18:15:00 -080033 oatClobber(cUnit, res.lowReg);
34 oatClobber(cUnit, res.highReg);
buzbeee3acd072012-02-25 17:03:10 -080035 oatLockTemp(cUnit, res.lowReg);
36 oatLockTemp(cUnit, res.highReg);
37 oatMarkPair(cUnit, res.lowReg, res.highReg);
38 return res;
39}
40
Ian Rogersf7d9ad32012-03-13 18:45:39 -070041RegLocation oatGetReturn(CompilationUnit* cUnit, bool isFloat)
buzbeee3acd072012-02-25 17:03:10 -080042{
Ian Rogersf7d9ad32012-03-13 18:45:39 -070043 RegLocation gpr_res = LOC_C_RETURN;
44 RegLocation fpr_res = LOC_C_RETURN_FLOAT;
45 RegLocation res = isFloat ? fpr_res : gpr_res;
buzbee86a4bce2012-03-06 18:15:00 -080046 oatClobber(cUnit, res.lowReg);
Elliott Hughesb3cd1222012-03-09 16:00:38 -080047 if (cUnit->instructionSet == kMips) {
48 oatMarkInUse(cUnit, res.lowReg);
49 } else {
50 oatLockTemp(cUnit, res.lowReg);
51 }
buzbeee3acd072012-02-25 17:03:10 -080052 return res;
53}
54
buzbeefc9e6fa2012-03-23 15:14:29 -070055void genInvoke(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
56 InvokeType type, bool isRange)
buzbeee3acd072012-02-25 17:03:10 -080057{
buzbeefc9e6fa2012-03-23 15:14:29 -070058 if (genIntrinsic(cUnit, bb, mir, type, isRange)) {
59 return;
60 }
buzbeee3acd072012-02-25 17:03:10 -080061 DecodedInstruction* dInsn = &mir->dalvikInsn;
Brian Carlstromf5822582012-03-19 22:34:31 -070062 InvokeType originalType = type; // avoiding mutation by ComputeInvokeInfo
buzbeee3acd072012-02-25 17:03:10 -080063 int callState = 0;
buzbee31a4a6f2012-02-28 15:36:15 -080064 LIR* nullCk;
65 LIR** pNullCk = NULL;
buzbeee3acd072012-02-25 17:03:10 -080066 NextCallInsn nextCallInsn;
67 oatFlushAllRegs(cUnit); /* Everything to home location */
68 // Explicit register usage
69 oatLockCallTemps(cUnit);
70
Logan Chien4dd96f52012-02-29 01:26:58 +080071 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
buzbee31a4a6f2012-02-28 15:36:15 -080072 *cUnit->dex_file, *cUnit->dex_cache,
73 cUnit->code_item, cUnit->method_idx,
74 cUnit->access_flags);
Logan Chien4dd96f52012-02-29 01:26:58 +080075
buzbeee3acd072012-02-25 17:03:10 -080076 uint32_t dexMethodIdx = dInsn->vB;
77 int vtableIdx;
Ian Rogers2ed3b952012-03-17 11:49:39 -070078 uintptr_t directCode;
79 uintptr_t directMethod;
buzbeee3acd072012-02-25 17:03:10 -080080 bool skipThis;
81 bool fastPath =
Logan Chien4dd96f52012-02-29 01:26:58 +080082 cUnit->compiler->ComputeInvokeInfo(dexMethodIdx, &mUnit, type,
Ian Rogers2ed3b952012-03-17 11:49:39 -070083 vtableIdx, directCode,
84 directMethod)
buzbeee3acd072012-02-25 17:03:10 -080085 && !SLOW_INVOKE_PATH;
86 if (type == kInterface) {
87 nextCallInsn = fastPath ? nextInterfaceCallInsn
88 : nextInterfaceCallInsnWithAccessCheck;
89 skipThis = false;
90 } else if (type == kDirect) {
91 if (fastPath) {
92 pNullCk = &nullCk;
93 }
94 nextCallInsn = fastPath ? nextSDCallInsn : nextDirectCallInsnSP;
95 skipThis = false;
96 } else if (type == kStatic) {
97 nextCallInsn = fastPath ? nextSDCallInsn : nextStaticCallInsnSP;
98 skipThis = false;
99 } else if (type == kSuper) {
Ian Rogers2ed3b952012-03-17 11:49:39 -0700100 DCHECK(!fastPath); // Fast path is a direct call.
101 nextCallInsn = nextSuperCallInsnSP;
102 skipThis = false;
buzbeee3acd072012-02-25 17:03:10 -0800103 } else {
104 DCHECK_EQ(type, kVirtual);
105 nextCallInsn = fastPath ? nextVCallInsn : nextVCallInsnSP;
106 skipThis = fastPath;
107 }
108 if (!isRange) {
109 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
110 nextCallInsn, dexMethodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700111 vtableIdx, directCode, directMethod,
112 originalType, skipThis);
buzbeee3acd072012-02-25 17:03:10 -0800113 } else {
114 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
115 nextCallInsn, dexMethodIdx, vtableIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700116 directCode, directMethod, originalType,
117 skipThis);
buzbeee3acd072012-02-25 17:03:10 -0800118 }
119 // Finish up any of the call sequence not interleaved in arg loading
120 while (callState >= 0) {
121 callState = nextCallInsn(cUnit, mir, callState, dexMethodIdx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700122 vtableIdx, directCode, directMethod,
123 originalType);
buzbeee3acd072012-02-25 17:03:10 -0800124 }
125 if (DISPLAY_MISSING_TARGETS) {
126 genShowTarget(cUnit);
127 }
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700128#if !defined(TARGET_X86)
buzbee0398c422012-03-02 15:22:47 -0800129 opReg(cUnit, kOpBlx, rINVOKE_TGT);
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700130#else
131 if (fastPath) {
132 opMem(cUnit, kOpBlx, rARG0, Method::GetCodeOffset().Int32Value());
133 } else {
134 UNIMPLEMENTED(FATAL) << "compute trampoline";
135 opThreadMem(cUnit, kOpBlx, 0);
136 }
buzbeea7678db2012-03-05 15:35:46 -0800137#endif
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700138
139 oatClobberCalleeSave(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800140}
141
142/*
143 * Target-independent code generation. Use only high-level
144 * load/store utilities here, or target-dependent genXX() handlers
145 * when necessary.
146 */
buzbee31a4a6f2012-02-28 15:36:15 -0800147bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
148 BasicBlock* bb, LIR* labelList)
buzbeee3acd072012-02-25 17:03:10 -0800149{
150 bool res = false; // Assume success
151 RegLocation rlSrc[3];
152 RegLocation rlDest = badLoc;
153 RegLocation rlResult = badLoc;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800154 Instruction::Code opcode = mir->dalvikInsn.opcode;
buzbeee3acd072012-02-25 17:03:10 -0800155
156 /* Prep Src and Dest locations */
157 int nextSreg = 0;
158 int nextLoc = 0;
159 int attrs = oatDataFlowAttributes[opcode];
160 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
161 if (attrs & DF_UA) {
162 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
163 nextSreg++;
164 } else if (attrs & DF_UA_WIDE) {
165 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
166 nextSreg + 1);
167 nextSreg+= 2;
168 }
169 if (attrs & DF_UB) {
170 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
171 nextSreg++;
172 } else if (attrs & DF_UB_WIDE) {
173 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
174 nextSreg + 1);
175 nextSreg+= 2;
176 }
177 if (attrs & DF_UC) {
178 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
179 } else if (attrs & DF_UC_WIDE) {
180 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
181 nextSreg + 1);
182 }
183 if (attrs & DF_DA) {
184 rlDest = oatGetDest(cUnit, mir, 0);
185 } else if (attrs & DF_DA_WIDE) {
186 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
187 }
188
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700189 switch (opcode) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800190 case Instruction::NOP:
buzbeee3acd072012-02-25 17:03:10 -0800191 break;
192
Ian Rogersc6f3bb82012-03-21 20:40:33 -0700193 case Instruction::MOVE_EXCEPTION: {
194 int exOffset = Thread::ExceptionOffset().Int32Value();
buzbeee3acd072012-02-25 17:03:10 -0800195 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
Ian Rogersc6f3bb82012-03-21 20:40:33 -0700196#if defined(TARGET_X86)
197 newLIR2(cUnit, kX86Mov32RT, rlResult.lowReg, exOffset);
198 newLIR2(cUnit, kX86Mov32TI, exOffset, 0);
199#else
200 int resetReg = oatAllocTemp(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800201 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
202 loadConstant(cUnit, resetReg, 0);
203 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
204 storeValue(cUnit, rlDest, rlResult);
Ian Rogersc6f3bb82012-03-21 20:40:33 -0700205 oatFreeTemp(cUnit, resetReg);
buzbeea7678db2012-03-05 15:35:46 -0800206#endif
buzbeee3acd072012-02-25 17:03:10 -0800207 break;
Ian Rogersc6f3bb82012-03-21 20:40:33 -0700208 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800209 case Instruction::RETURN_VOID:
buzbee86a4bce2012-03-06 18:15:00 -0800210 if (!cUnit->attrs & METHOD_IS_LEAF) {
211 genSuspendTest(cUnit, mir);
212 }
buzbeee3acd072012-02-25 17:03:10 -0800213 break;
214
Elliott Hughesadb8c672012-03-06 16:49:32 -0800215 case Instruction::RETURN:
216 case Instruction::RETURN_OBJECT:
buzbee86a4bce2012-03-06 18:15:00 -0800217 if (!cUnit->attrs & METHOD_IS_LEAF) {
218 genSuspendTest(cUnit, mir);
219 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700220 storeValue(cUnit, oatGetReturn(cUnit, cUnit->shorty[0] == 'F'),
221 rlSrc[0]);
buzbeee3acd072012-02-25 17:03:10 -0800222 break;
223
Elliott Hughesadb8c672012-03-06 16:49:32 -0800224 case Instruction::RETURN_WIDE:
buzbee86a4bce2012-03-06 18:15:00 -0800225 if (!cUnit->attrs & METHOD_IS_LEAF) {
226 genSuspendTest(cUnit, mir);
227 }
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700228 storeValueWide(cUnit, oatGetReturnWide(cUnit,
229 cUnit->shorty[0] == 'D'), rlSrc[0]);
buzbeee3acd072012-02-25 17:03:10 -0800230 break;
231
Elliott Hughesadb8c672012-03-06 16:49:32 -0800232 case Instruction::MOVE_RESULT_WIDE:
buzbeee3acd072012-02-25 17:03:10 -0800233 if (mir->optimizationFlags & MIR_INLINED)
234 break; // Nop - combined w/ previous invoke
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700235 storeValueWide(cUnit, rlDest, oatGetReturnWide(cUnit, rlDest.fp));
buzbeee3acd072012-02-25 17:03:10 -0800236 break;
237
Elliott Hughesadb8c672012-03-06 16:49:32 -0800238 case Instruction::MOVE_RESULT:
239 case Instruction::MOVE_RESULT_OBJECT:
buzbeee3acd072012-02-25 17:03:10 -0800240 if (mir->optimizationFlags & MIR_INLINED)
241 break; // Nop - combined w/ previous invoke
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700242 storeValue(cUnit, rlDest, oatGetReturn(cUnit, rlDest.fp));
buzbeee3acd072012-02-25 17:03:10 -0800243 break;
244
Elliott Hughesadb8c672012-03-06 16:49:32 -0800245 case Instruction::MOVE:
246 case Instruction::MOVE_OBJECT:
247 case Instruction::MOVE_16:
248 case Instruction::MOVE_OBJECT_16:
249 case Instruction::MOVE_FROM16:
250 case Instruction::MOVE_OBJECT_FROM16:
buzbeee3acd072012-02-25 17:03:10 -0800251 storeValue(cUnit, rlDest, rlSrc[0]);
252 break;
253
Elliott Hughesadb8c672012-03-06 16:49:32 -0800254 case Instruction::MOVE_WIDE:
255 case Instruction::MOVE_WIDE_16:
256 case Instruction::MOVE_WIDE_FROM16:
buzbeee3acd072012-02-25 17:03:10 -0800257 storeValueWide(cUnit, rlDest, rlSrc[0]);
258 break;
259
Elliott Hughesadb8c672012-03-06 16:49:32 -0800260 case Instruction::CONST:
261 case Instruction::CONST_4:
262 case Instruction::CONST_16:
buzbeee3acd072012-02-25 17:03:10 -0800263 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
264 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
265 storeValue(cUnit, rlDest, rlResult);
266 break;
267
Elliott Hughesadb8c672012-03-06 16:49:32 -0800268 case Instruction::CONST_HIGH16:
buzbeee3acd072012-02-25 17:03:10 -0800269 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
270 loadConstantNoClobber(cUnit, rlResult.lowReg,
271 mir->dalvikInsn.vB << 16);
272 storeValue(cUnit, rlDest, rlResult);
273 break;
274
Elliott Hughesadb8c672012-03-06 16:49:32 -0800275 case Instruction::CONST_WIDE_16:
276 case Instruction::CONST_WIDE_32:
buzbeee3acd072012-02-25 17:03:10 -0800277 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
278 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
279 mir->dalvikInsn.vB,
280 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
281 storeValueWide(cUnit, rlDest, rlResult);
282 break;
283
Elliott Hughesadb8c672012-03-06 16:49:32 -0800284 case Instruction::CONST_WIDE:
buzbeee3acd072012-02-25 17:03:10 -0800285 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
286 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
287 mir->dalvikInsn.vB_wide & 0xffffffff,
288 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
289 storeValueWide(cUnit, rlDest, rlResult);
290 break;
291
Elliott Hughesadb8c672012-03-06 16:49:32 -0800292 case Instruction::CONST_WIDE_HIGH16:
buzbeee3acd072012-02-25 17:03:10 -0800293 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
294 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
295 0, mir->dalvikInsn.vB << 16);
296 storeValueWide(cUnit, rlDest, rlResult);
297 break;
298
Elliott Hughesadb8c672012-03-06 16:49:32 -0800299 case Instruction::MONITOR_ENTER:
buzbeee3acd072012-02-25 17:03:10 -0800300 genMonitorEnter(cUnit, mir, rlSrc[0]);
301 break;
302
Elliott Hughesadb8c672012-03-06 16:49:32 -0800303 case Instruction::MONITOR_EXIT:
buzbeee3acd072012-02-25 17:03:10 -0800304 genMonitorExit(cUnit, mir, rlSrc[0]);
305 break;
306
Elliott Hughesadb8c672012-03-06 16:49:32 -0800307 case Instruction::CHECK_CAST:
buzbeee3acd072012-02-25 17:03:10 -0800308 genCheckCast(cUnit, mir, rlSrc[0]);
309 break;
310
Elliott Hughesadb8c672012-03-06 16:49:32 -0800311 case Instruction::INSTANCE_OF:
buzbeee3acd072012-02-25 17:03:10 -0800312 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
313 break;
314
Elliott Hughesadb8c672012-03-06 16:49:32 -0800315 case Instruction::NEW_INSTANCE:
buzbeee3acd072012-02-25 17:03:10 -0800316 genNewInstance(cUnit, mir, rlDest);
317 break;
318
Elliott Hughesadb8c672012-03-06 16:49:32 -0800319 case Instruction::THROW:
buzbeee3acd072012-02-25 17:03:10 -0800320 genThrow(cUnit, mir, rlSrc[0]);
321 break;
322
Elliott Hughesadb8c672012-03-06 16:49:32 -0800323 case Instruction::THROW_VERIFICATION_ERROR:
buzbeee3acd072012-02-25 17:03:10 -0800324 genThrowVerificationError(cUnit, mir);
325 break;
326
Elliott Hughesadb8c672012-03-06 16:49:32 -0800327 case Instruction::ARRAY_LENGTH:
buzbeee3acd072012-02-25 17:03:10 -0800328 int lenOffset;
329 lenOffset = Array::LengthOffset().Int32Value();
330 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
331 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
332 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
333 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
334 rlResult.lowReg);
335 storeValue(cUnit, rlDest, rlResult);
336 break;
337
Elliott Hughesadb8c672012-03-06 16:49:32 -0800338 case Instruction::CONST_STRING:
339 case Instruction::CONST_STRING_JUMBO:
buzbeee3acd072012-02-25 17:03:10 -0800340 genConstString(cUnit, mir, rlDest, rlSrc[0]);
341 break;
342
Elliott Hughesadb8c672012-03-06 16:49:32 -0800343 case Instruction::CONST_CLASS:
buzbeee3acd072012-02-25 17:03:10 -0800344 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
345 break;
346
Elliott Hughesadb8c672012-03-06 16:49:32 -0800347 case Instruction::FILL_ARRAY_DATA:
buzbeee3acd072012-02-25 17:03:10 -0800348 genFillArrayData(cUnit, mir, rlSrc[0]);
349 break;
350
Elliott Hughesadb8c672012-03-06 16:49:32 -0800351 case Instruction::FILLED_NEW_ARRAY:
buzbeee3acd072012-02-25 17:03:10 -0800352 genFilledNewArray(cUnit, mir, false /* not range */);
353 break;
354
Elliott Hughesadb8c672012-03-06 16:49:32 -0800355 case Instruction::FILLED_NEW_ARRAY_RANGE:
buzbeee3acd072012-02-25 17:03:10 -0800356 genFilledNewArray(cUnit, mir, true /* range */);
357 break;
358
Elliott Hughesadb8c672012-03-06 16:49:32 -0800359 case Instruction::NEW_ARRAY:
buzbeee3acd072012-02-25 17:03:10 -0800360 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
361 break;
362
Elliott Hughesadb8c672012-03-06 16:49:32 -0800363 case Instruction::GOTO:
364 case Instruction::GOTO_16:
365 case Instruction::GOTO_32:
buzbeee3acd072012-02-25 17:03:10 -0800366 if (bb->taken->startOffset <= mir->offset) {
367 genSuspendTest(cUnit, mir);
368 }
buzbee82488f52012-03-02 08:20:26 -0800369 opUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
buzbeee3acd072012-02-25 17:03:10 -0800370 break;
371
Elliott Hughesadb8c672012-03-06 16:49:32 -0800372 case Instruction::PACKED_SWITCH:
buzbeee3acd072012-02-25 17:03:10 -0800373 genPackedSwitch(cUnit, mir, rlSrc[0]);
374 break;
375
Elliott Hughesadb8c672012-03-06 16:49:32 -0800376 case Instruction::SPARSE_SWITCH:
buzbeee3acd072012-02-25 17:03:10 -0800377 genSparseSwitch(cUnit, mir, rlSrc[0]);
378 break;
379
Elliott Hughesadb8c672012-03-06 16:49:32 -0800380 case Instruction::CMPL_FLOAT:
381 case Instruction::CMPG_FLOAT:
382 case Instruction::CMPL_DOUBLE:
383 case Instruction::CMPG_DOUBLE:
buzbeee3acd072012-02-25 17:03:10 -0800384 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
385 break;
386
Elliott Hughesadb8c672012-03-06 16:49:32 -0800387 case Instruction::CMP_LONG:
buzbeee3acd072012-02-25 17:03:10 -0800388 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
389 break;
390
Elliott Hughesadb8c672012-03-06 16:49:32 -0800391 case Instruction::IF_EQ:
392 case Instruction::IF_NE:
393 case Instruction::IF_LT:
394 case Instruction::IF_GE:
395 case Instruction::IF_GT:
396 case Instruction::IF_LE: {
buzbeee3acd072012-02-25 17:03:10 -0800397 bool backwardBranch;
398 backwardBranch = (bb->taken->startOffset <= mir->offset);
399 if (backwardBranch) {
400 genSuspendTest(cUnit, mir);
401 }
402 genCompareAndBranch(cUnit, bb, mir, rlSrc[0], rlSrc[1], labelList);
403 break;
404 }
405
Elliott Hughesadb8c672012-03-06 16:49:32 -0800406 case Instruction::IF_EQZ:
407 case Instruction::IF_NEZ:
408 case Instruction::IF_LTZ:
409 case Instruction::IF_GEZ:
410 case Instruction::IF_GTZ:
411 case Instruction::IF_LEZ: {
buzbeee3acd072012-02-25 17:03:10 -0800412 bool backwardBranch;
413 backwardBranch = (bb->taken->startOffset <= mir->offset);
414 if (backwardBranch) {
415 genSuspendTest(cUnit, mir);
416 }
417 genCompareZeroAndBranch(cUnit, bb, mir, rlSrc[0], labelList);
418 break;
419 }
420
Elliott Hughesadb8c672012-03-06 16:49:32 -0800421 case Instruction::AGET_WIDE:
buzbeee3acd072012-02-25 17:03:10 -0800422 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
423 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800424 case Instruction::AGET:
425 case Instruction::AGET_OBJECT:
buzbeee3acd072012-02-25 17:03:10 -0800426 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
427 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800428 case Instruction::AGET_BOOLEAN:
buzbeee3acd072012-02-25 17:03:10 -0800429 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
430 rlDest, 0);
431 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800432 case Instruction::AGET_BYTE:
buzbeee3acd072012-02-25 17:03:10 -0800433 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
434 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800435 case Instruction::AGET_CHAR:
buzbeee3acd072012-02-25 17:03:10 -0800436 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
437 rlDest, 1);
438 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800439 case Instruction::AGET_SHORT:
buzbeee3acd072012-02-25 17:03:10 -0800440 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
441 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800442 case Instruction::APUT_WIDE:
buzbeee3acd072012-02-25 17:03:10 -0800443 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
444 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800445 case Instruction::APUT:
buzbeee3acd072012-02-25 17:03:10 -0800446 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
447 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800448 case Instruction::APUT_OBJECT:
buzbeee3acd072012-02-25 17:03:10 -0800449 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
450 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800451 case Instruction::APUT_SHORT:
452 case Instruction::APUT_CHAR:
buzbeee3acd072012-02-25 17:03:10 -0800453 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
454 rlSrc[0], 1);
455 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800456 case Instruction::APUT_BYTE:
457 case Instruction::APUT_BOOLEAN:
buzbeee3acd072012-02-25 17:03:10 -0800458 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
459 rlSrc[0], 0);
460 break;
461
Elliott Hughesadb8c672012-03-06 16:49:32 -0800462 case Instruction::IGET_OBJECT:
463 //case Instruction::IGET_OBJECT_VOLATILE:
buzbeee3acd072012-02-25 17:03:10 -0800464 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0], false, true);
465 break;
466
Elliott Hughesadb8c672012-03-06 16:49:32 -0800467 case Instruction::IGET_WIDE:
468 //case Instruction::IGET_WIDE_VOLATILE:
buzbeee3acd072012-02-25 17:03:10 -0800469 genIGet(cUnit, mir, kLong, rlDest, rlSrc[0], true, false);
470 break;
471
Elliott Hughesadb8c672012-03-06 16:49:32 -0800472 case Instruction::IGET:
473 //case Instruction::IGET_VOLATILE:
buzbeee3acd072012-02-25 17:03:10 -0800474 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0], false, false);
475 break;
476
Elliott Hughesadb8c672012-03-06 16:49:32 -0800477 case Instruction::IGET_CHAR:
buzbeee3acd072012-02-25 17:03:10 -0800478 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0], false, false);
479 break;
480
Elliott Hughesadb8c672012-03-06 16:49:32 -0800481 case Instruction::IGET_SHORT:
buzbeee3acd072012-02-25 17:03:10 -0800482 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0], false, false);
483 break;
484
Elliott Hughesadb8c672012-03-06 16:49:32 -0800485 case Instruction::IGET_BOOLEAN:
486 case Instruction::IGET_BYTE:
buzbeee3acd072012-02-25 17:03:10 -0800487 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0], false, false);
488 break;
489
Elliott Hughesadb8c672012-03-06 16:49:32 -0800490 case Instruction::IPUT_WIDE:
491 //case Instruction::IPUT_WIDE_VOLATILE:
buzbeee3acd072012-02-25 17:03:10 -0800492 genIPut(cUnit, mir, kLong, rlSrc[0], rlSrc[1], true, false);
493 break;
494
Elliott Hughesadb8c672012-03-06 16:49:32 -0800495 case Instruction::IPUT_OBJECT:
496 //case Instruction::IPUT_OBJECT_VOLATILE:
buzbeee3acd072012-02-25 17:03:10 -0800497 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false, true);
498 break;
499
Elliott Hughesadb8c672012-03-06 16:49:32 -0800500 case Instruction::IPUT:
501 //case Instruction::IPUT_VOLATILE:
buzbeee3acd072012-02-25 17:03:10 -0800502 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false, false);
503 break;
504
Elliott Hughesadb8c672012-03-06 16:49:32 -0800505 case Instruction::IPUT_BOOLEAN:
506 case Instruction::IPUT_BYTE:
buzbeee3acd072012-02-25 17:03:10 -0800507 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false, false);
508 break;
509
Elliott Hughesadb8c672012-03-06 16:49:32 -0800510 case Instruction::IPUT_CHAR:
buzbeee3acd072012-02-25 17:03:10 -0800511 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false, false);
512 break;
513
Elliott Hughesadb8c672012-03-06 16:49:32 -0800514 case Instruction::IPUT_SHORT:
buzbeee3acd072012-02-25 17:03:10 -0800515 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false, false);
516 break;
517
Elliott Hughesadb8c672012-03-06 16:49:32 -0800518 case Instruction::SGET_OBJECT:
buzbeee3acd072012-02-25 17:03:10 -0800519 genSget(cUnit, mir, rlDest, false, true);
520 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800521 case Instruction::SGET:
522 case Instruction::SGET_BOOLEAN:
523 case Instruction::SGET_BYTE:
524 case Instruction::SGET_CHAR:
525 case Instruction::SGET_SHORT:
buzbeee3acd072012-02-25 17:03:10 -0800526 genSget(cUnit, mir, rlDest, false, false);
527 break;
528
Elliott Hughesadb8c672012-03-06 16:49:32 -0800529 case Instruction::SGET_WIDE:
buzbeee3acd072012-02-25 17:03:10 -0800530 genSget(cUnit, mir, rlDest, true, false);
531 break;
532
Elliott Hughesadb8c672012-03-06 16:49:32 -0800533 case Instruction::SPUT_OBJECT:
buzbeee3acd072012-02-25 17:03:10 -0800534 genSput(cUnit, mir, rlSrc[0], false, true);
535 break;
536
Elliott Hughesadb8c672012-03-06 16:49:32 -0800537 case Instruction::SPUT:
538 case Instruction::SPUT_BOOLEAN:
539 case Instruction::SPUT_BYTE:
540 case Instruction::SPUT_CHAR:
541 case Instruction::SPUT_SHORT:
buzbeee3acd072012-02-25 17:03:10 -0800542 genSput(cUnit, mir, rlSrc[0], false, false);
543 break;
544
Elliott Hughesadb8c672012-03-06 16:49:32 -0800545 case Instruction::SPUT_WIDE:
buzbeee3acd072012-02-25 17:03:10 -0800546 genSput(cUnit, mir, rlSrc[0], true, false);
547 break;
548
Elliott Hughesadb8c672012-03-06 16:49:32 -0800549 case Instruction::INVOKE_STATIC_RANGE:
buzbeefc9e6fa2012-03-23 15:14:29 -0700550 genInvoke(cUnit, bb, mir, kStatic, true /*range*/);
buzbeee3acd072012-02-25 17:03:10 -0800551 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800552 case Instruction::INVOKE_STATIC:
buzbeefc9e6fa2012-03-23 15:14:29 -0700553 genInvoke(cUnit, bb, mir, kStatic, false /*range*/);
buzbeee3acd072012-02-25 17:03:10 -0800554 break;
555
Elliott Hughesadb8c672012-03-06 16:49:32 -0800556 case Instruction::INVOKE_DIRECT:
buzbeefc9e6fa2012-03-23 15:14:29 -0700557 genInvoke(cUnit, bb, mir, kDirect, false /*range*/);
buzbeee3acd072012-02-25 17:03:10 -0800558 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800559 case Instruction::INVOKE_DIRECT_RANGE:
buzbeefc9e6fa2012-03-23 15:14:29 -0700560 genInvoke(cUnit, bb, mir, kDirect, true /*range*/);
buzbeee3acd072012-02-25 17:03:10 -0800561 break;
562
Elliott Hughesadb8c672012-03-06 16:49:32 -0800563 case Instruction::INVOKE_VIRTUAL:
buzbeefc9e6fa2012-03-23 15:14:29 -0700564 genInvoke(cUnit, bb, mir, kVirtual, false /*range*/);
buzbeee3acd072012-02-25 17:03:10 -0800565 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800566 case Instruction::INVOKE_VIRTUAL_RANGE:
buzbeefc9e6fa2012-03-23 15:14:29 -0700567 genInvoke(cUnit, bb, mir, kVirtual, true /*range*/);
buzbeee3acd072012-02-25 17:03:10 -0800568 break;
569
Elliott Hughesadb8c672012-03-06 16:49:32 -0800570 case Instruction::INVOKE_SUPER:
buzbeefc9e6fa2012-03-23 15:14:29 -0700571 genInvoke(cUnit, bb, mir, kSuper, false /*range*/);
buzbeee3acd072012-02-25 17:03:10 -0800572 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800573 case Instruction::INVOKE_SUPER_RANGE:
buzbeefc9e6fa2012-03-23 15:14:29 -0700574 genInvoke(cUnit, bb, mir, kSuper, true /*range*/);
buzbeee3acd072012-02-25 17:03:10 -0800575 break;
576
Elliott Hughesadb8c672012-03-06 16:49:32 -0800577 case Instruction::INVOKE_INTERFACE:
buzbeefc9e6fa2012-03-23 15:14:29 -0700578 genInvoke(cUnit, bb, mir, kInterface, false /*range*/);
buzbeee3acd072012-02-25 17:03:10 -0800579 break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800580 case Instruction::INVOKE_INTERFACE_RANGE:
buzbeefc9e6fa2012-03-23 15:14:29 -0700581 genInvoke(cUnit, bb, mir, kInterface, true /*range*/);
buzbeee3acd072012-02-25 17:03:10 -0800582 break;
583
Elliott Hughesadb8c672012-03-06 16:49:32 -0800584 case Instruction::NEG_INT:
585 case Instruction::NOT_INT:
buzbeee3acd072012-02-25 17:03:10 -0800586 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
587 break;
588
Elliott Hughesadb8c672012-03-06 16:49:32 -0800589 case Instruction::NEG_LONG:
590 case Instruction::NOT_LONG:
buzbeee3acd072012-02-25 17:03:10 -0800591 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
592 break;
593
Elliott Hughesadb8c672012-03-06 16:49:32 -0800594 case Instruction::NEG_FLOAT:
buzbeee3acd072012-02-25 17:03:10 -0800595 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
596 break;
597
Elliott Hughesadb8c672012-03-06 16:49:32 -0800598 case Instruction::NEG_DOUBLE:
buzbeee3acd072012-02-25 17:03:10 -0800599 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
600 break;
601
Elliott Hughesadb8c672012-03-06 16:49:32 -0800602 case Instruction::INT_TO_LONG:
buzbeee3acd072012-02-25 17:03:10 -0800603 genIntToLong(cUnit, mir, rlDest, rlSrc[0]);
604 break;
605
Elliott Hughesadb8c672012-03-06 16:49:32 -0800606 case Instruction::LONG_TO_INT:
buzbeee3acd072012-02-25 17:03:10 -0800607 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
608 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
609 storeValue(cUnit, rlDest, rlSrc[0]);
610 break;
611
Elliott Hughesadb8c672012-03-06 16:49:32 -0800612 case Instruction::INT_TO_BYTE:
613 case Instruction::INT_TO_SHORT:
614 case Instruction::INT_TO_CHAR:
buzbeee3acd072012-02-25 17:03:10 -0800615 genIntNarrowing(cUnit, mir, rlDest, rlSrc[0]);
616 break;
617
Elliott Hughesadb8c672012-03-06 16:49:32 -0800618 case Instruction::INT_TO_FLOAT:
619 case Instruction::INT_TO_DOUBLE:
620 case Instruction::LONG_TO_FLOAT:
621 case Instruction::LONG_TO_DOUBLE:
622 case Instruction::FLOAT_TO_INT:
623 case Instruction::FLOAT_TO_LONG:
624 case Instruction::FLOAT_TO_DOUBLE:
625 case Instruction::DOUBLE_TO_INT:
626 case Instruction::DOUBLE_TO_LONG:
627 case Instruction::DOUBLE_TO_FLOAT:
buzbeee3acd072012-02-25 17:03:10 -0800628 genConversion(cUnit, mir);
629 break;
630
Elliott Hughesadb8c672012-03-06 16:49:32 -0800631 case Instruction::ADD_INT:
632 case Instruction::SUB_INT:
633 case Instruction::MUL_INT:
634 case Instruction::DIV_INT:
635 case Instruction::REM_INT:
636 case Instruction::AND_INT:
637 case Instruction::OR_INT:
638 case Instruction::XOR_INT:
639 case Instruction::SHL_INT:
640 case Instruction::SHR_INT:
641 case Instruction::USHR_INT:
642 case Instruction::ADD_INT_2ADDR:
643 case Instruction::SUB_INT_2ADDR:
644 case Instruction::MUL_INT_2ADDR:
645 case Instruction::DIV_INT_2ADDR:
646 case Instruction::REM_INT_2ADDR:
647 case Instruction::AND_INT_2ADDR:
648 case Instruction::OR_INT_2ADDR:
649 case Instruction::XOR_INT_2ADDR:
650 case Instruction::SHL_INT_2ADDR:
651 case Instruction::SHR_INT_2ADDR:
652 case Instruction::USHR_INT_2ADDR:
buzbeee3acd072012-02-25 17:03:10 -0800653 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
654 break;
655
Elliott Hughesadb8c672012-03-06 16:49:32 -0800656 case Instruction::ADD_LONG:
657 case Instruction::SUB_LONG:
658 case Instruction::MUL_LONG:
659 case Instruction::DIV_LONG:
660 case Instruction::REM_LONG:
661 case Instruction::AND_LONG:
662 case Instruction::OR_LONG:
663 case Instruction::XOR_LONG:
664 case Instruction::ADD_LONG_2ADDR:
665 case Instruction::SUB_LONG_2ADDR:
666 case Instruction::MUL_LONG_2ADDR:
667 case Instruction::DIV_LONG_2ADDR:
668 case Instruction::REM_LONG_2ADDR:
669 case Instruction::AND_LONG_2ADDR:
670 case Instruction::OR_LONG_2ADDR:
671 case Instruction::XOR_LONG_2ADDR:
buzbeee3acd072012-02-25 17:03:10 -0800672 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
673 break;
674
Elliott Hughesadb8c672012-03-06 16:49:32 -0800675 case Instruction::SHL_LONG:
676 case Instruction::SHR_LONG:
677 case Instruction::USHR_LONG:
678 case Instruction::SHL_LONG_2ADDR:
679 case Instruction::SHR_LONG_2ADDR:
680 case Instruction::USHR_LONG_2ADDR:
buzbeee3acd072012-02-25 17:03:10 -0800681 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
682 break;
683
Elliott Hughesadb8c672012-03-06 16:49:32 -0800684 case Instruction::ADD_FLOAT:
685 case Instruction::SUB_FLOAT:
686 case Instruction::MUL_FLOAT:
687 case Instruction::DIV_FLOAT:
688 case Instruction::REM_FLOAT:
689 case Instruction::ADD_FLOAT_2ADDR:
690 case Instruction::SUB_FLOAT_2ADDR:
691 case Instruction::MUL_FLOAT_2ADDR:
692 case Instruction::DIV_FLOAT_2ADDR:
693 case Instruction::REM_FLOAT_2ADDR:
buzbeee3acd072012-02-25 17:03:10 -0800694 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
695 break;
696
Elliott Hughesadb8c672012-03-06 16:49:32 -0800697 case Instruction::ADD_DOUBLE:
698 case Instruction::SUB_DOUBLE:
699 case Instruction::MUL_DOUBLE:
700 case Instruction::DIV_DOUBLE:
701 case Instruction::REM_DOUBLE:
702 case Instruction::ADD_DOUBLE_2ADDR:
703 case Instruction::SUB_DOUBLE_2ADDR:
704 case Instruction::MUL_DOUBLE_2ADDR:
705 case Instruction::DIV_DOUBLE_2ADDR:
706 case Instruction::REM_DOUBLE_2ADDR:
buzbeee3acd072012-02-25 17:03:10 -0800707 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
708 break;
709
Elliott Hughesadb8c672012-03-06 16:49:32 -0800710 case Instruction::RSUB_INT:
711 case Instruction::ADD_INT_LIT16:
712 case Instruction::MUL_INT_LIT16:
713 case Instruction::DIV_INT_LIT16:
714 case Instruction::REM_INT_LIT16:
715 case Instruction::AND_INT_LIT16:
716 case Instruction::OR_INT_LIT16:
717 case Instruction::XOR_INT_LIT16:
718 case Instruction::ADD_INT_LIT8:
719 case Instruction::RSUB_INT_LIT8:
720 case Instruction::MUL_INT_LIT8:
721 case Instruction::DIV_INT_LIT8:
722 case Instruction::REM_INT_LIT8:
723 case Instruction::AND_INT_LIT8:
724 case Instruction::OR_INT_LIT8:
725 case Instruction::XOR_INT_LIT8:
726 case Instruction::SHL_INT_LIT8:
727 case Instruction::SHR_INT_LIT8:
728 case Instruction::USHR_INT_LIT8:
buzbeee3acd072012-02-25 17:03:10 -0800729 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
730 break;
731
732 default:
733 res = true;
734 }
735 return res;
736}
737
buzbee31a4a6f2012-02-28 15:36:15 -0800738const char* extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
buzbeee3acd072012-02-25 17:03:10 -0800739 "kMirOpPhi",
buzbeee1965672012-03-11 18:39:19 -0700740 "kMirOpCopy",
buzbee84fd6932012-03-29 16:44:16 -0700741 "kMirFusedCmplFloat",
742 "kMirFusedCmpgFloat",
743 "kMirFusedCmplDouble",
744 "kMirFusedCmpgDouble",
745 "kMirFusedCmpLong",
746 "kMirNop",
buzbeea2e39d92012-03-30 09:11:45 -0700747 "kMirOpNullNRangeUpCheck",
748 "kMirOpNullNRangeDownCheck",
749 "kMirOpLowerBound",
buzbeee3acd072012-02-25 17:03:10 -0800750};
751
752/* Extended MIR instructions like PHI */
buzbee84fd6932012-03-29 16:44:16 -0700753void handleExtendedMethodMIR(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir)
buzbeee3acd072012-02-25 17:03:10 -0800754{
755 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
756 char* msg = NULL;
757 if (cUnit->printMe) {
758 msg = (char*)oatNew(cUnit, strlen(extendedMIROpNames[opOffset]) + 1,
759 false, kAllocDebugInfo);
760 strcpy(msg, extendedMIROpNames[opOffset]);
761 }
buzbee31a4a6f2012-02-28 15:36:15 -0800762 LIR* op = newLIR1(cUnit, kPseudoExtended, (int) msg);
buzbeee3acd072012-02-25 17:03:10 -0800763
764 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
765 case kMirOpPhi: {
766 char* ssaString = NULL;
767 if (cUnit->printMe) {
768 ssaString = oatGetSSAString(cUnit, mir->ssaRep);
769 }
770 op->flags.isNop = true;
buzbee31a4a6f2012-02-28 15:36:15 -0800771 newLIR1(cUnit, kPseudoSSARep, (int) ssaString);
buzbeee3acd072012-02-25 17:03:10 -0800772 break;
773 }
buzbee239c4e72012-03-16 08:42:29 -0700774 case kMirOpCopy: {
775 RegLocation rlSrc = oatGetSrc(cUnit, mir, 0);
776 RegLocation rlDest = oatGetDest(cUnit, mir, 0);
777 storeValue(cUnit, rlDest, rlSrc);
buzbeee1965672012-03-11 18:39:19 -0700778 break;
buzbee239c4e72012-03-16 08:42:29 -0700779 }
buzbee84fd6932012-03-29 16:44:16 -0700780#if defined(TARGET_ARM)
781 case kMirOpFusedCmplFloat:
782 genFusedFPCmpBranch(cUnit, bb, mir, false /*gt bias*/, false /*double*/);
783 break;
784 case kMirOpFusedCmpgFloat:
785 genFusedFPCmpBranch(cUnit, bb, mir, true /*gt bias*/, false /*double*/);
786 break;
787 case kMirOpFusedCmplDouble:
788 genFusedFPCmpBranch(cUnit, bb, mir, false /*gt bias*/, true /*double*/);
789 break;
790 case kMirOpFusedCmpgDouble:
791 genFusedFPCmpBranch(cUnit, bb, mir, true /*gt bias*/, true /*double*/);
792 break;
793 case kMirOpFusedCmpLong:
794 genFusedLongCmpBranch(cUnit, bb, mir);
795 break;
796#endif
buzbeee3acd072012-02-25 17:03:10 -0800797 default:
798 break;
799 }
800}
801
802/* Handle the content in each basic block */
buzbee31a4a6f2012-02-28 15:36:15 -0800803bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbeee3acd072012-02-25 17:03:10 -0800804{
805 MIR* mir;
buzbee31a4a6f2012-02-28 15:36:15 -0800806 LIR* labelList = (LIR*) cUnit->blockLabelList;
buzbeee3acd072012-02-25 17:03:10 -0800807 int blockId = bb->id;
808
809 cUnit->curBlock = bb;
810 labelList[blockId].operands[0] = bb->startOffset;
811
812 /* Insert the block label */
buzbee31a4a6f2012-02-28 15:36:15 -0800813 labelList[blockId].opcode = kPseudoNormalBlockLabel;
buzbeee3acd072012-02-25 17:03:10 -0800814 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
815
buzbeee1965672012-03-11 18:39:19 -0700816 /* Free temp registers and reset redundant store tracking */
buzbeee3acd072012-02-25 17:03:10 -0800817 oatResetRegPool(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800818 oatResetDefTracking(cUnit);
819
buzbeee1965672012-03-11 18:39:19 -0700820 /*
821 * If control reached us from our immediate predecessor via
822 * fallthrough and we have no other incoming arcs we can
823 * reuse existing liveness. Otherwise, reset.
824 */
825 if (!bb->fallThroughTarget || bb->predecessors->numUsed != 1) {
826 oatClobberAllRegs(cUnit);
827 }
828
buzbee31a4a6f2012-02-28 15:36:15 -0800829 LIR* headLIR = NULL;
buzbeee3acd072012-02-25 17:03:10 -0800830
831 if (bb->blockType == kEntryBlock) {
832 genEntrySequence(cUnit, bb);
833 } else if (bb->blockType == kExitBlock) {
834 genExitSequence(cUnit, bb);
835 }
836
837 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
838
839 oatResetRegPool(cUnit);
840 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
841 oatClobberAllRegs(cUnit);
842 }
843
844 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
845 oatResetDefTracking(cUnit);
846 }
847
buzbee3d661942012-03-14 17:37:27 -0700848#ifndef NDEBUG
849 /* Reset temp tracking sanity check */
850 cUnit->liveSReg = INVALID_SREG;
851#endif
852
buzbeee3acd072012-02-25 17:03:10 -0800853 cUnit->currentDalvikOffset = mir->offset;
854
Elliott Hughesadb8c672012-03-06 16:49:32 -0800855 Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode;
856 Instruction::Format dalvikFormat = Instruction::FormatOf(dalvikOpcode);
buzbeee3acd072012-02-25 17:03:10 -0800857
buzbee31a4a6f2012-02-28 15:36:15 -0800858 LIR* boundaryLIR;
buzbeee3acd072012-02-25 17:03:10 -0800859
860 /* Mark the beginning of a Dalvik instruction for line tracking */
861 char* instStr = cUnit->printMe ?
Elliott Hughesadb8c672012-03-06 16:49:32 -0800862 oatGetDalvikDisassembly(cUnit, mir->dalvikInsn, "") : NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800863 boundaryLIR = newLIR1(cUnit, kPseudoDalvikByteCodeBoundary,
buzbeee3acd072012-02-25 17:03:10 -0800864 (intptr_t) instStr);
865 cUnit->boundaryMap.insert(std::make_pair(mir->offset,
866 (LIR*)boundaryLIR));
867 /* Remember the first LIR for this block */
868 if (headLIR == NULL) {
869 headLIR = boundaryLIR;
870 /* Set the first boundaryLIR as a scheduling barrier */
871 headLIR->defMask = ENCODE_ALL;
872 }
873
874 /* If we're compiling for the debugger, generate an update callout */
875 if (cUnit->genDebugger) {
876 genDebuggerUpdate(cUnit, mir->offset);
877 }
878
879 /* Don't generate the SSA annotation unless verbose mode is on */
880 if (cUnit->printMe && mir->ssaRep) {
881 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
buzbee31a4a6f2012-02-28 15:36:15 -0800882 newLIR1(cUnit, kPseudoSSARep, (int) ssaString);
buzbeee3acd072012-02-25 17:03:10 -0800883 }
884
buzbee84fd6932012-03-29 16:44:16 -0700885 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
886 handleExtendedMethodMIR(cUnit, bb, mir);
887 continue;
888 }
889
buzbeee3acd072012-02-25 17:03:10 -0800890 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
buzbeee3acd072012-02-25 17:03:10 -0800891 if (notHandled) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800892 LOG(FATAL) << StringPrintf("%#06x: Opcode %#x (%s) / Fmt %d not handled",
893 mir->offset, dalvikOpcode, Instruction::Name(dalvikOpcode), dalvikFormat);
894
buzbeee3acd072012-02-25 17:03:10 -0800895 }
896 }
897
898 if (headLIR) {
899 /*
900 * Eliminate redundant loads/stores and delay stores into later
901 * slots
902 */
903 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
904 cUnit->lastLIRInsn);
905
906 /*
907 * Generate an unconditional branch to the fallthrough block.
908 */
909 if (bb->fallThrough) {
buzbee82488f52012-03-02 08:20:26 -0800910 opUnconditionalBranch(cUnit,
911 &labelList[bb->fallThrough->id]);
buzbeee3acd072012-02-25 17:03:10 -0800912 }
913 }
914 return false;
915}
916
buzbee16da88c2012-03-20 10:38:17 -0700917/* Set basic block labels */
918bool labelBlocks(CompilationUnit* cUnit, BasicBlock* bb)
919{
920 LIR* labelList = (LIR*) cUnit->blockLabelList;
921 int blockId = bb->id;
922
923 cUnit->curBlock = bb;
924 labelList[blockId].operands[0] = bb->startOffset;
925
926 /* Insert the block label */
927 labelList[blockId].opcode = kPseudoNormalBlockLabel;
928 return false;
929}
930
931void oatSpecialMIR2LIR(CompilationUnit* cUnit, SpecialCaseHandler specialCase)
932{
933 /* Find the first DalvikByteCode block */
934 int numReachableBlocks = cUnit->numReachableBlocks;
935 const GrowableList *blockList = &cUnit->blockList;
936 BasicBlock*bb = NULL;
937 for (int idx = 0; idx < numReachableBlocks; idx++) {
938 int dfsIndex = cUnit->dfsOrder.elemList[idx];
939 bb = (BasicBlock*)oatGrowableListGetElement(blockList, dfsIndex);
940 if (bb->blockType == kDalvikByteCode) {
941 break;
942 }
943 }
944 if (bb == NULL) {
945 return;
946 }
947 DCHECK_EQ(bb->startOffset, 0);
948 DCHECK(bb->firstMIRInsn != 0);
949
950 /* Get the first instruction */
951 MIR* mir = bb->firstMIRInsn;
952
953 /* Free temp registers and reset redundant store tracking */
954 oatResetRegPool(cUnit);
955 oatResetDefTracking(cUnit);
956 oatClobberAllRegs(cUnit);
957
958 genSpecialCase(cUnit, bb, mir, specialCase);
959}
960
buzbeee3acd072012-02-25 17:03:10 -0800961void oatMethodMIR2LIR(CompilationUnit* cUnit)
962{
963 /* Used to hold the labels of each block */
964 cUnit->blockLabelList =
buzbee31a4a6f2012-02-28 15:36:15 -0800965 (void *) oatNew(cUnit, sizeof(LIR) * cUnit->numBlocks, true,
buzbeee3acd072012-02-25 17:03:10 -0800966 kAllocLIR);
967
968 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
969 kPreOrderDFSTraversal, false /* Iterative */);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700970
buzbeee3acd072012-02-25 17:03:10 -0800971 handleSuspendLaunchpads(cUnit);
972
973 handleThrowLaunchpads(cUnit);
974
buzbeefc9e6fa2012-03-23 15:14:29 -0700975 handleIntrinsicLaunchpads(cUnit);
976
buzbee86a4bce2012-03-06 18:15:00 -0800977 if (!(cUnit->disableOpt & (1 << kSafeOptimizations))) {
978 removeRedundantBranches(cUnit);
979 }
buzbeee3acd072012-02-25 17:03:10 -0800980}
981
982/* Needed by the ld/st optmizatons */
buzbee31a4a6f2012-02-28 15:36:15 -0800983LIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
buzbeee3acd072012-02-25 17:03:10 -0800984{
buzbee82488f52012-03-02 08:20:26 -0800985 return opRegCopyNoInsert(cUnit, rDest, rSrc);
buzbeee3acd072012-02-25 17:03:10 -0800986}
987
988/* Needed by the register allocator */
989void oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
990{
buzbee82488f52012-03-02 08:20:26 -0800991 opRegCopy(cUnit, rDest, rSrc);
buzbeee3acd072012-02-25 17:03:10 -0800992}
993
994/* Needed by the register allocator */
995void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
996 int srcLo, int srcHi)
997{
buzbee82488f52012-03-02 08:20:26 -0800998 opRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
buzbeee3acd072012-02-25 17:03:10 -0800999}
1000
1001void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
1002 int displacement, int rSrc, OpSize size)
1003{
1004 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
1005}
1006
1007void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
1008 int displacement, int rSrcLo, int rSrcHi)
1009{
1010 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
1011}
1012
1013} // namespace art