blob: 87a86eb6d4881bde35fec219ce5c6bea53b50541 [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
189 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",
740 "kMirOpNullNRangeUpCheck",
741 "kMirOpNullNRangeDownCheck",
742 "kMirOpLowerBound",
buzbeee1965672012-03-11 18:39:19 -0700743 "kMirOpCopy",
buzbeee3acd072012-02-25 17:03:10 -0800744};
745
746/* Extended MIR instructions like PHI */
buzbee31a4a6f2012-02-28 15:36:15 -0800747void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
buzbeee3acd072012-02-25 17:03:10 -0800748{
749 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
750 char* msg = NULL;
751 if (cUnit->printMe) {
752 msg = (char*)oatNew(cUnit, strlen(extendedMIROpNames[opOffset]) + 1,
753 false, kAllocDebugInfo);
754 strcpy(msg, extendedMIROpNames[opOffset]);
755 }
buzbee31a4a6f2012-02-28 15:36:15 -0800756 LIR* op = newLIR1(cUnit, kPseudoExtended, (int) msg);
buzbeee3acd072012-02-25 17:03:10 -0800757
758 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
759 case kMirOpPhi: {
760 char* ssaString = NULL;
761 if (cUnit->printMe) {
762 ssaString = oatGetSSAString(cUnit, mir->ssaRep);
763 }
764 op->flags.isNop = true;
buzbee31a4a6f2012-02-28 15:36:15 -0800765 newLIR1(cUnit, kPseudoSSARep, (int) ssaString);
buzbeee3acd072012-02-25 17:03:10 -0800766 break;
767 }
buzbee239c4e72012-03-16 08:42:29 -0700768 case kMirOpCopy: {
769 RegLocation rlSrc = oatGetSrc(cUnit, mir, 0);
770 RegLocation rlDest = oatGetDest(cUnit, mir, 0);
771 storeValue(cUnit, rlDest, rlSrc);
buzbeee1965672012-03-11 18:39:19 -0700772 break;
buzbee239c4e72012-03-16 08:42:29 -0700773 }
buzbeee3acd072012-02-25 17:03:10 -0800774 default:
775 break;
776 }
777}
778
779/* Handle the content in each basic block */
buzbee31a4a6f2012-02-28 15:36:15 -0800780bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbeee3acd072012-02-25 17:03:10 -0800781{
782 MIR* mir;
buzbee31a4a6f2012-02-28 15:36:15 -0800783 LIR* labelList = (LIR*) cUnit->blockLabelList;
buzbeee3acd072012-02-25 17:03:10 -0800784 int blockId = bb->id;
785
786 cUnit->curBlock = bb;
787 labelList[blockId].operands[0] = bb->startOffset;
788
789 /* Insert the block label */
buzbee31a4a6f2012-02-28 15:36:15 -0800790 labelList[blockId].opcode = kPseudoNormalBlockLabel;
buzbeee3acd072012-02-25 17:03:10 -0800791 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
792
buzbeee1965672012-03-11 18:39:19 -0700793 /* Free temp registers and reset redundant store tracking */
buzbeee3acd072012-02-25 17:03:10 -0800794 oatResetRegPool(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800795 oatResetDefTracking(cUnit);
796
buzbeee1965672012-03-11 18:39:19 -0700797 /*
798 * If control reached us from our immediate predecessor via
799 * fallthrough and we have no other incoming arcs we can
800 * reuse existing liveness. Otherwise, reset.
801 */
802 if (!bb->fallThroughTarget || bb->predecessors->numUsed != 1) {
803 oatClobberAllRegs(cUnit);
804 }
805
buzbee31a4a6f2012-02-28 15:36:15 -0800806 LIR* headLIR = NULL;
buzbeee3acd072012-02-25 17:03:10 -0800807
808 if (bb->blockType == kEntryBlock) {
809 genEntrySequence(cUnit, bb);
810 } else if (bb->blockType == kExitBlock) {
811 genExitSequence(cUnit, bb);
812 }
813
814 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
815
816 oatResetRegPool(cUnit);
817 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
818 oatClobberAllRegs(cUnit);
819 }
820
821 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
822 oatResetDefTracking(cUnit);
823 }
824
buzbee3d661942012-03-14 17:37:27 -0700825#ifndef NDEBUG
826 /* Reset temp tracking sanity check */
827 cUnit->liveSReg = INVALID_SREG;
828#endif
829
buzbeee3acd072012-02-25 17:03:10 -0800830 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
831 handleExtendedMethodMIR(cUnit, mir);
832 continue;
833 }
834
835 cUnit->currentDalvikOffset = mir->offset;
836
Elliott Hughesadb8c672012-03-06 16:49:32 -0800837 Instruction::Code dalvikOpcode = mir->dalvikInsn.opcode;
838 Instruction::Format dalvikFormat = Instruction::FormatOf(dalvikOpcode);
buzbeee3acd072012-02-25 17:03:10 -0800839
buzbee31a4a6f2012-02-28 15:36:15 -0800840 LIR* boundaryLIR;
buzbeee3acd072012-02-25 17:03:10 -0800841
842 /* Mark the beginning of a Dalvik instruction for line tracking */
843 char* instStr = cUnit->printMe ?
Elliott Hughesadb8c672012-03-06 16:49:32 -0800844 oatGetDalvikDisassembly(cUnit, mir->dalvikInsn, "") : NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800845 boundaryLIR = newLIR1(cUnit, kPseudoDalvikByteCodeBoundary,
buzbeee3acd072012-02-25 17:03:10 -0800846 (intptr_t) instStr);
847 cUnit->boundaryMap.insert(std::make_pair(mir->offset,
848 (LIR*)boundaryLIR));
849 /* Remember the first LIR for this block */
850 if (headLIR == NULL) {
851 headLIR = boundaryLIR;
852 /* Set the first boundaryLIR as a scheduling barrier */
853 headLIR->defMask = ENCODE_ALL;
854 }
855
856 /* If we're compiling for the debugger, generate an update callout */
857 if (cUnit->genDebugger) {
858 genDebuggerUpdate(cUnit, mir->offset);
859 }
860
861 /* Don't generate the SSA annotation unless verbose mode is on */
862 if (cUnit->printMe && mir->ssaRep) {
863 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
buzbee31a4a6f2012-02-28 15:36:15 -0800864 newLIR1(cUnit, kPseudoSSARep, (int) ssaString);
buzbeee3acd072012-02-25 17:03:10 -0800865 }
866
867 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
buzbeee3acd072012-02-25 17:03:10 -0800868 if (notHandled) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800869 LOG(FATAL) << StringPrintf("%#06x: Opcode %#x (%s) / Fmt %d not handled",
870 mir->offset, dalvikOpcode, Instruction::Name(dalvikOpcode), dalvikFormat);
871
buzbeee3acd072012-02-25 17:03:10 -0800872 }
873 }
874
875 if (headLIR) {
876 /*
877 * Eliminate redundant loads/stores and delay stores into later
878 * slots
879 */
880 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
881 cUnit->lastLIRInsn);
882
883 /*
884 * Generate an unconditional branch to the fallthrough block.
885 */
886 if (bb->fallThrough) {
buzbee82488f52012-03-02 08:20:26 -0800887 opUnconditionalBranch(cUnit,
888 &labelList[bb->fallThrough->id]);
buzbeee3acd072012-02-25 17:03:10 -0800889 }
890 }
891 return false;
892}
893
buzbee16da88c2012-03-20 10:38:17 -0700894/* Set basic block labels */
895bool labelBlocks(CompilationUnit* cUnit, BasicBlock* bb)
896{
897 LIR* labelList = (LIR*) cUnit->blockLabelList;
898 int blockId = bb->id;
899
900 cUnit->curBlock = bb;
901 labelList[blockId].operands[0] = bb->startOffset;
902
903 /* Insert the block label */
904 labelList[blockId].opcode = kPseudoNormalBlockLabel;
905 return false;
906}
907
908void oatSpecialMIR2LIR(CompilationUnit* cUnit, SpecialCaseHandler specialCase)
909{
910 /* Find the first DalvikByteCode block */
911 int numReachableBlocks = cUnit->numReachableBlocks;
912 const GrowableList *blockList = &cUnit->blockList;
913 BasicBlock*bb = NULL;
914 for (int idx = 0; idx < numReachableBlocks; idx++) {
915 int dfsIndex = cUnit->dfsOrder.elemList[idx];
916 bb = (BasicBlock*)oatGrowableListGetElement(blockList, dfsIndex);
917 if (bb->blockType == kDalvikByteCode) {
918 break;
919 }
920 }
921 if (bb == NULL) {
922 return;
923 }
924 DCHECK_EQ(bb->startOffset, 0);
925 DCHECK(bb->firstMIRInsn != 0);
926
927 /* Get the first instruction */
928 MIR* mir = bb->firstMIRInsn;
929
930 /* Free temp registers and reset redundant store tracking */
931 oatResetRegPool(cUnit);
932 oatResetDefTracking(cUnit);
933 oatClobberAllRegs(cUnit);
934
935 genSpecialCase(cUnit, bb, mir, specialCase);
936}
937
buzbeee3acd072012-02-25 17:03:10 -0800938void oatMethodMIR2LIR(CompilationUnit* cUnit)
939{
940 /* Used to hold the labels of each block */
941 cUnit->blockLabelList =
buzbee31a4a6f2012-02-28 15:36:15 -0800942 (void *) oatNew(cUnit, sizeof(LIR) * cUnit->numBlocks, true,
buzbeee3acd072012-02-25 17:03:10 -0800943 kAllocLIR);
944
945 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
946 kPreOrderDFSTraversal, false /* Iterative */);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700947
buzbeee3acd072012-02-25 17:03:10 -0800948 handleSuspendLaunchpads(cUnit);
949
950 handleThrowLaunchpads(cUnit);
951
buzbeefc9e6fa2012-03-23 15:14:29 -0700952 handleIntrinsicLaunchpads(cUnit);
953
buzbee86a4bce2012-03-06 18:15:00 -0800954 if (!(cUnit->disableOpt & (1 << kSafeOptimizations))) {
955 removeRedundantBranches(cUnit);
956 }
buzbeee3acd072012-02-25 17:03:10 -0800957}
958
959/* Needed by the ld/st optmizatons */
buzbee31a4a6f2012-02-28 15:36:15 -0800960LIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
buzbeee3acd072012-02-25 17:03:10 -0800961{
buzbee82488f52012-03-02 08:20:26 -0800962 return opRegCopyNoInsert(cUnit, rDest, rSrc);
buzbeee3acd072012-02-25 17:03:10 -0800963}
964
965/* Needed by the register allocator */
966void oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
967{
buzbee82488f52012-03-02 08:20:26 -0800968 opRegCopy(cUnit, rDest, rSrc);
buzbeee3acd072012-02-25 17:03:10 -0800969}
970
971/* Needed by the register allocator */
972void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
973 int srcLo, int srcHi)
974{
buzbee82488f52012-03-02 08:20:26 -0800975 opRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
buzbeee3acd072012-02-25 17:03:10 -0800976}
977
978void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
979 int displacement, int rSrc, OpSize size)
980{
981 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
982}
983
984void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
985 int displacement, int rSrcLo, int rSrcHi)
986{
987 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
988}
989
990} // namespace art