blob: 6de3a9c3d94a7917fbcd4054fd545dc7de67e09d [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 */
buzbee31a4a6f2012-02-28 15:36:15 -080028RegLocation getRetLocWide(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -080029{
30 RegLocation res = LOC_C_RETURN_WIDE;
31 oatLockTemp(cUnit, res.lowReg);
32 oatLockTemp(cUnit, res.highReg);
33 oatMarkPair(cUnit, res.lowReg, res.highReg);
34 return res;
35}
36
buzbee31a4a6f2012-02-28 15:36:15 -080037RegLocation getRetLoc(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -080038{
39 RegLocation res = LOC_C_RETURN;
40 oatLockTemp(cUnit, res.lowReg);
41 return res;
42}
43
buzbee31a4a6f2012-02-28 15:36:15 -080044void genInvoke(CompilationUnit* cUnit, MIR* mir, InvokeType type, bool isRange)
buzbeee3acd072012-02-25 17:03:10 -080045{
46 DecodedInstruction* dInsn = &mir->dalvikInsn;
47 int callState = 0;
buzbee31a4a6f2012-02-28 15:36:15 -080048 LIR* nullCk;
49 LIR** pNullCk = NULL;
buzbeee3acd072012-02-25 17:03:10 -080050 NextCallInsn nextCallInsn;
51 oatFlushAllRegs(cUnit); /* Everything to home location */
52 // Explicit register usage
53 oatLockCallTemps(cUnit);
54
Logan Chien4dd96f52012-02-29 01:26:58 +080055 OatCompilationUnit mUnit(cUnit->class_loader, cUnit->class_linker,
buzbee31a4a6f2012-02-28 15:36:15 -080056 *cUnit->dex_file, *cUnit->dex_cache,
57 cUnit->code_item, cUnit->method_idx,
58 cUnit->access_flags);
Logan Chien4dd96f52012-02-29 01:26:58 +080059
buzbeee3acd072012-02-25 17:03:10 -080060 uint32_t dexMethodIdx = dInsn->vB;
61 int vtableIdx;
62 bool skipThis;
63 bool fastPath =
Logan Chien4dd96f52012-02-29 01:26:58 +080064 cUnit->compiler->ComputeInvokeInfo(dexMethodIdx, &mUnit, type,
buzbeee3acd072012-02-25 17:03:10 -080065 vtableIdx)
66 && !SLOW_INVOKE_PATH;
67 if (type == kInterface) {
68 nextCallInsn = fastPath ? nextInterfaceCallInsn
69 : nextInterfaceCallInsnWithAccessCheck;
70 skipThis = false;
71 } else if (type == kDirect) {
72 if (fastPath) {
73 pNullCk = &nullCk;
74 }
75 nextCallInsn = fastPath ? nextSDCallInsn : nextDirectCallInsnSP;
76 skipThis = false;
77 } else if (type == kStatic) {
78 nextCallInsn = fastPath ? nextSDCallInsn : nextStaticCallInsnSP;
79 skipThis = false;
80 } else if (type == kSuper) {
81 nextCallInsn = fastPath ? nextSuperCallInsn : nextSuperCallInsnSP;
82 skipThis = fastPath;
83 } else {
84 DCHECK_EQ(type, kVirtual);
85 nextCallInsn = fastPath ? nextVCallInsn : nextVCallInsnSP;
86 skipThis = fastPath;
87 }
88 if (!isRange) {
89 callState = genDalvikArgsNoRange(cUnit, mir, dInsn, callState, pNullCk,
90 nextCallInsn, dexMethodIdx,
91 vtableIdx, skipThis);
92 } else {
93 callState = genDalvikArgsRange(cUnit, mir, dInsn, callState, pNullCk,
94 nextCallInsn, dexMethodIdx, vtableIdx,
95 skipThis);
96 }
97 // Finish up any of the call sequence not interleaved in arg loading
98 while (callState >= 0) {
99 callState = nextCallInsn(cUnit, mir, callState, dexMethodIdx,
100 vtableIdx);
101 }
102 if (DISPLAY_MISSING_TARGETS) {
103 genShowTarget(cUnit);
104 }
105 opReg(cUnit, kOpBlx, rLR);
106 oatClobberCalleeSave(cUnit);
107}
108
109/*
110 * Target-independent code generation. Use only high-level
111 * load/store utilities here, or target-dependent genXX() handlers
112 * when necessary.
113 */
buzbee31a4a6f2012-02-28 15:36:15 -0800114bool compileDalvikInstruction(CompilationUnit* cUnit, MIR* mir,
115 BasicBlock* bb, LIR* labelList)
buzbeee3acd072012-02-25 17:03:10 -0800116{
117 bool res = false; // Assume success
118 RegLocation rlSrc[3];
119 RegLocation rlDest = badLoc;
120 RegLocation rlResult = badLoc;
121 Opcode opcode = mir->dalvikInsn.opcode;
122
123 /* Prep Src and Dest locations */
124 int nextSreg = 0;
125 int nextLoc = 0;
126 int attrs = oatDataFlowAttributes[opcode];
127 rlSrc[0] = rlSrc[1] = rlSrc[2] = badLoc;
128 if (attrs & DF_UA) {
129 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
130 nextSreg++;
131 } else if (attrs & DF_UA_WIDE) {
132 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
133 nextSreg + 1);
134 nextSreg+= 2;
135 }
136 if (attrs & DF_UB) {
137 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
138 nextSreg++;
139 } else if (attrs & DF_UB_WIDE) {
140 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
141 nextSreg + 1);
142 nextSreg+= 2;
143 }
144 if (attrs & DF_UC) {
145 rlSrc[nextLoc++] = oatGetSrc(cUnit, mir, nextSreg);
146 } else if (attrs & DF_UC_WIDE) {
147 rlSrc[nextLoc++] = oatGetSrcWide(cUnit, mir, nextSreg,
148 nextSreg + 1);
149 }
150 if (attrs & DF_DA) {
151 rlDest = oatGetDest(cUnit, mir, 0);
152 } else if (attrs & DF_DA_WIDE) {
153 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
154 }
155
156 switch(opcode) {
157 case OP_NOP:
158 break;
159
160 case OP_MOVE_EXCEPTION:
161 int exOffset;
162 int resetReg;
163 exOffset = Thread::ExceptionOffset().Int32Value();
164 resetReg = oatAllocTemp(cUnit);
165 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
166 loadWordDisp(cUnit, rSELF, exOffset, rlResult.lowReg);
167 loadConstant(cUnit, resetReg, 0);
168 storeWordDisp(cUnit, rSELF, exOffset, resetReg);
169 storeValue(cUnit, rlDest, rlResult);
170 break;
171
172 case OP_RETURN_VOID:
173 genSuspendTest(cUnit, mir);
174 break;
175
176 case OP_RETURN:
177 case OP_RETURN_OBJECT:
178 genSuspendTest(cUnit, mir);
179 storeValue(cUnit, getRetLoc(cUnit), rlSrc[0]);
180 break;
181
182 case OP_RETURN_WIDE:
183 genSuspendTest(cUnit, mir);
184 storeValueWide(cUnit, getRetLocWide(cUnit), rlSrc[0]);
185 break;
186
187 case OP_MOVE_RESULT_WIDE:
188 if (mir->optimizationFlags & MIR_INLINED)
189 break; // Nop - combined w/ previous invoke
190 storeValueWide(cUnit, rlDest, getRetLocWide(cUnit));
191 break;
192
193 case OP_MOVE_RESULT:
194 case OP_MOVE_RESULT_OBJECT:
195 if (mir->optimizationFlags & MIR_INLINED)
196 break; // Nop - combined w/ previous invoke
197 storeValue(cUnit, rlDest, getRetLoc(cUnit));
198 break;
199
200 case OP_MOVE:
201 case OP_MOVE_OBJECT:
202 case OP_MOVE_16:
203 case OP_MOVE_OBJECT_16:
204 case OP_MOVE_FROM16:
205 case OP_MOVE_OBJECT_FROM16:
206 storeValue(cUnit, rlDest, rlSrc[0]);
207 break;
208
209 case OP_MOVE_WIDE:
210 case OP_MOVE_WIDE_16:
211 case OP_MOVE_WIDE_FROM16:
212 storeValueWide(cUnit, rlDest, rlSrc[0]);
213 break;
214
215 case OP_CONST:
216 case OP_CONST_4:
217 case OP_CONST_16:
218 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
219 loadConstantNoClobber(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
220 storeValue(cUnit, rlDest, rlResult);
221 break;
222
223 case OP_CONST_HIGH16:
224 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
225 loadConstantNoClobber(cUnit, rlResult.lowReg,
226 mir->dalvikInsn.vB << 16);
227 storeValue(cUnit, rlDest, rlResult);
228 break;
229
230 case OP_CONST_WIDE_16:
231 case OP_CONST_WIDE_32:
232 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
233 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
234 mir->dalvikInsn.vB,
235 (mir->dalvikInsn.vB & 0x80000000) ? -1 : 0);
236 storeValueWide(cUnit, rlDest, rlResult);
237 break;
238
239 case OP_CONST_WIDE:
240 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
241 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
242 mir->dalvikInsn.vB_wide & 0xffffffff,
243 (mir->dalvikInsn.vB_wide >> 32) & 0xffffffff);
244 storeValueWide(cUnit, rlDest, rlResult);
245 break;
246
247 case OP_CONST_WIDE_HIGH16:
248 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
249 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
250 0, mir->dalvikInsn.vB << 16);
251 storeValueWide(cUnit, rlDest, rlResult);
252 break;
253
254 case OP_MONITOR_ENTER:
255 genMonitorEnter(cUnit, mir, rlSrc[0]);
256 break;
257
258 case OP_MONITOR_EXIT:
259 genMonitorExit(cUnit, mir, rlSrc[0]);
260 break;
261
262 case OP_CHECK_CAST:
263 genCheckCast(cUnit, mir, rlSrc[0]);
264 break;
265
266 case OP_INSTANCE_OF:
267 genInstanceof(cUnit, mir, rlDest, rlSrc[0]);
268 break;
269
270 case OP_NEW_INSTANCE:
271 genNewInstance(cUnit, mir, rlDest);
272 break;
273
274 case OP_THROW:
275 genThrow(cUnit, mir, rlSrc[0]);
276 break;
277
278 case OP_THROW_VERIFICATION_ERROR:
279 genThrowVerificationError(cUnit, mir);
280 break;
281
282 case OP_ARRAY_LENGTH:
283 int lenOffset;
284 lenOffset = Array::LengthOffset().Int32Value();
285 rlSrc[0] = loadValue(cUnit, rlSrc[0], kCoreReg);
286 genNullCheck(cUnit, rlSrc[0].sRegLow, rlSrc[0].lowReg, mir);
287 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
288 loadWordDisp(cUnit, rlSrc[0].lowReg, lenOffset,
289 rlResult.lowReg);
290 storeValue(cUnit, rlDest, rlResult);
291 break;
292
293 case OP_CONST_STRING:
294 case OP_CONST_STRING_JUMBO:
295 genConstString(cUnit, mir, rlDest, rlSrc[0]);
296 break;
297
298 case OP_CONST_CLASS:
299 genConstClass(cUnit, mir, rlDest, rlSrc[0]);
300 break;
301
302 case OP_FILL_ARRAY_DATA:
303 genFillArrayData(cUnit, mir, rlSrc[0]);
304 break;
305
306 case OP_FILLED_NEW_ARRAY:
307 genFilledNewArray(cUnit, mir, false /* not range */);
308 break;
309
310 case OP_FILLED_NEW_ARRAY_RANGE:
311 genFilledNewArray(cUnit, mir, true /* range */);
312 break;
313
314 case OP_NEW_ARRAY:
315 genNewArray(cUnit, mir, rlDest, rlSrc[0]);
316 break;
317
318 case OP_GOTO:
319 case OP_GOTO_16:
320 case OP_GOTO_32:
321 if (bb->taken->startOffset <= mir->offset) {
322 genSuspendTest(cUnit, mir);
323 }
324 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
325 break;
326
327 case OP_PACKED_SWITCH:
328 genPackedSwitch(cUnit, mir, rlSrc[0]);
329 break;
330
331 case OP_SPARSE_SWITCH:
332 genSparseSwitch(cUnit, mir, rlSrc[0]);
333 break;
334
335 case OP_CMPL_FLOAT:
336 case OP_CMPG_FLOAT:
337 case OP_CMPL_DOUBLE:
338 case OP_CMPG_DOUBLE:
339 res = genCmpFP(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
340 break;
341
342 case OP_CMP_LONG:
343 genCmpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
344 break;
345
346 case OP_IF_EQ:
347 case OP_IF_NE:
348 case OP_IF_LT:
349 case OP_IF_GE:
350 case OP_IF_GT:
351 case OP_IF_LE: {
352 bool backwardBranch;
353 backwardBranch = (bb->taken->startOffset <= mir->offset);
354 if (backwardBranch) {
355 genSuspendTest(cUnit, mir);
356 }
357 genCompareAndBranch(cUnit, bb, mir, rlSrc[0], rlSrc[1], labelList);
358 break;
359 }
360
361 case OP_IF_EQZ:
362 case OP_IF_NEZ:
363 case OP_IF_LTZ:
364 case OP_IF_GEZ:
365 case OP_IF_GTZ:
366 case OP_IF_LEZ: {
367 bool backwardBranch;
368 backwardBranch = (bb->taken->startOffset <= mir->offset);
369 if (backwardBranch) {
370 genSuspendTest(cUnit, mir);
371 }
372 genCompareZeroAndBranch(cUnit, bb, mir, rlSrc[0], labelList);
373 break;
374 }
375
376 case OP_AGET_WIDE:
377 genArrayGet(cUnit, mir, kLong, rlSrc[0], rlSrc[1], rlDest, 3);
378 break;
379 case OP_AGET:
380 case OP_AGET_OBJECT:
381 genArrayGet(cUnit, mir, kWord, rlSrc[0], rlSrc[1], rlDest, 2);
382 break;
383 case OP_AGET_BOOLEAN:
384 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1],
385 rlDest, 0);
386 break;
387 case OP_AGET_BYTE:
388 genArrayGet(cUnit, mir, kSignedByte, rlSrc[0], rlSrc[1], rlDest, 0);
389 break;
390 case OP_AGET_CHAR:
391 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1],
392 rlDest, 1);
393 break;
394 case OP_AGET_SHORT:
395 genArrayGet(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], rlDest, 1);
396 break;
397 case OP_APUT_WIDE:
398 genArrayPut(cUnit, mir, kLong, rlSrc[1], rlSrc[2], rlSrc[0], 3);
399 break;
400 case OP_APUT:
401 genArrayPut(cUnit, mir, kWord, rlSrc[1], rlSrc[2], rlSrc[0], 2);
402 break;
403 case OP_APUT_OBJECT:
404 genArrayObjPut(cUnit, mir, rlSrc[1], rlSrc[2], rlSrc[0], 2);
405 break;
406 case OP_APUT_SHORT:
407 case OP_APUT_CHAR:
408 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc[1], rlSrc[2],
409 rlSrc[0], 1);
410 break;
411 case OP_APUT_BYTE:
412 case OP_APUT_BOOLEAN:
413 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc[1], rlSrc[2],
414 rlSrc[0], 0);
415 break;
416
417 case OP_IGET_OBJECT:
418 case OP_IGET_OBJECT_VOLATILE:
419 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0], false, true);
420 break;
421
422 case OP_IGET_WIDE:
423 case OP_IGET_WIDE_VOLATILE:
424 genIGet(cUnit, mir, kLong, rlDest, rlSrc[0], true, false);
425 break;
426
427 case OP_IGET:
428 case OP_IGET_VOLATILE:
429 genIGet(cUnit, mir, kWord, rlDest, rlSrc[0], false, false);
430 break;
431
432 case OP_IGET_CHAR:
433 genIGet(cUnit, mir, kUnsignedHalf, rlDest, rlSrc[0], false, false);
434 break;
435
436 case OP_IGET_SHORT:
437 genIGet(cUnit, mir, kSignedHalf, rlDest, rlSrc[0], false, false);
438 break;
439
440 case OP_IGET_BOOLEAN:
441 case OP_IGET_BYTE:
442 genIGet(cUnit, mir, kUnsignedByte, rlDest, rlSrc[0], false, false);
443 break;
444
445 case OP_IPUT_WIDE:
446 case OP_IPUT_WIDE_VOLATILE:
447 genIPut(cUnit, mir, kLong, rlSrc[0], rlSrc[1], true, false);
448 break;
449
450 case OP_IPUT_OBJECT:
451 case OP_IPUT_OBJECT_VOLATILE:
452 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false, true);
453 break;
454
455 case OP_IPUT:
456 case OP_IPUT_VOLATILE:
457 genIPut(cUnit, mir, kWord, rlSrc[0], rlSrc[1], false, false);
458 break;
459
460 case OP_IPUT_BOOLEAN:
461 case OP_IPUT_BYTE:
462 genIPut(cUnit, mir, kUnsignedByte, rlSrc[0], rlSrc[1], false, false);
463 break;
464
465 case OP_IPUT_CHAR:
466 genIPut(cUnit, mir, kUnsignedHalf, rlSrc[0], rlSrc[1], false, false);
467 break;
468
469 case OP_IPUT_SHORT:
470 genIPut(cUnit, mir, kSignedHalf, rlSrc[0], rlSrc[1], false, false);
471 break;
472
473 case OP_SGET_OBJECT:
474 genSget(cUnit, mir, rlDest, false, true);
475 break;
476 case OP_SGET:
477 case OP_SGET_BOOLEAN:
478 case OP_SGET_BYTE:
479 case OP_SGET_CHAR:
480 case OP_SGET_SHORT:
481 genSget(cUnit, mir, rlDest, false, false);
482 break;
483
484 case OP_SGET_WIDE:
485 genSget(cUnit, mir, rlDest, true, false);
486 break;
487
488 case OP_SPUT_OBJECT:
489 genSput(cUnit, mir, rlSrc[0], false, true);
490 break;
491
492 case OP_SPUT:
493 case OP_SPUT_BOOLEAN:
494 case OP_SPUT_BYTE:
495 case OP_SPUT_CHAR:
496 case OP_SPUT_SHORT:
497 genSput(cUnit, mir, rlSrc[0], false, false);
498 break;
499
500 case OP_SPUT_WIDE:
501 genSput(cUnit, mir, rlSrc[0], true, false);
502 break;
503
504 case OP_INVOKE_STATIC_RANGE:
505 genInvoke(cUnit, mir, kStatic, true /*range*/);
506 break;
507 case OP_INVOKE_STATIC:
508 genInvoke(cUnit, mir, kStatic, false /*range*/);
509 break;
510
511 case OP_INVOKE_DIRECT:
512 genInvoke(cUnit, mir, kDirect, false /*range*/);
513 break;
514 case OP_INVOKE_DIRECT_RANGE:
515 genInvoke(cUnit, mir, kDirect, true /*range*/);
516 break;
517
518 case OP_INVOKE_VIRTUAL:
519 genInvoke(cUnit, mir, kVirtual, false /*range*/);
520 break;
521 case OP_INVOKE_VIRTUAL_RANGE:
522 genInvoke(cUnit, mir, kVirtual, true /*range*/);
523 break;
524
525 case OP_INVOKE_SUPER:
526 genInvoke(cUnit, mir, kSuper, false /*range*/);
527 break;
528 case OP_INVOKE_SUPER_RANGE:
529 genInvoke(cUnit, mir, kSuper, true /*range*/);
530 break;
531
532 case OP_INVOKE_INTERFACE:
533 genInvoke(cUnit, mir, kInterface, false /*range*/);
534 break;
535 case OP_INVOKE_INTERFACE_RANGE:
536 genInvoke(cUnit, mir, kInterface, true /*range*/);
537 break;
538
539 case OP_NEG_INT:
540 case OP_NOT_INT:
541 res = genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
542 break;
543
544 case OP_NEG_LONG:
545 case OP_NOT_LONG:
546 res = genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
547 break;
548
549 case OP_NEG_FLOAT:
550 res = genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
551 break;
552
553 case OP_NEG_DOUBLE:
554 res = genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[0]);
555 break;
556
557 case OP_INT_TO_LONG:
558 genIntToLong(cUnit, mir, rlDest, rlSrc[0]);
559 break;
560
561 case OP_LONG_TO_INT:
562 rlSrc[0] = oatUpdateLocWide(cUnit, rlSrc[0]);
563 rlSrc[0] = oatWideToNarrow(cUnit, rlSrc[0]);
564 storeValue(cUnit, rlDest, rlSrc[0]);
565 break;
566
567 case OP_INT_TO_BYTE:
568 case OP_INT_TO_SHORT:
569 case OP_INT_TO_CHAR:
570 genIntNarrowing(cUnit, mir, rlDest, rlSrc[0]);
571 break;
572
573 case OP_INT_TO_FLOAT:
574 case OP_INT_TO_DOUBLE:
575 case OP_LONG_TO_FLOAT:
576 case OP_LONG_TO_DOUBLE:
577 case OP_FLOAT_TO_INT:
578 case OP_FLOAT_TO_LONG:
579 case OP_FLOAT_TO_DOUBLE:
580 case OP_DOUBLE_TO_INT:
581 case OP_DOUBLE_TO_LONG:
582 case OP_DOUBLE_TO_FLOAT:
583 genConversion(cUnit, mir);
584 break;
585
586 case OP_ADD_INT:
587 case OP_SUB_INT:
588 case OP_MUL_INT:
589 case OP_DIV_INT:
590 case OP_REM_INT:
591 case OP_AND_INT:
592 case OP_OR_INT:
593 case OP_XOR_INT:
594 case OP_SHL_INT:
595 case OP_SHR_INT:
596 case OP_USHR_INT:
597 case OP_ADD_INT_2ADDR:
598 case OP_SUB_INT_2ADDR:
599 case OP_MUL_INT_2ADDR:
600 case OP_DIV_INT_2ADDR:
601 case OP_REM_INT_2ADDR:
602 case OP_AND_INT_2ADDR:
603 case OP_OR_INT_2ADDR:
604 case OP_XOR_INT_2ADDR:
605 case OP_SHL_INT_2ADDR:
606 case OP_SHR_INT_2ADDR:
607 case OP_USHR_INT_2ADDR:
608 genArithOpInt(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
609 break;
610
611 case OP_ADD_LONG:
612 case OP_SUB_LONG:
613 case OP_MUL_LONG:
614 case OP_DIV_LONG:
615 case OP_REM_LONG:
616 case OP_AND_LONG:
617 case OP_OR_LONG:
618 case OP_XOR_LONG:
619 case OP_ADD_LONG_2ADDR:
620 case OP_SUB_LONG_2ADDR:
621 case OP_MUL_LONG_2ADDR:
622 case OP_DIV_LONG_2ADDR:
623 case OP_REM_LONG_2ADDR:
624 case OP_AND_LONG_2ADDR:
625 case OP_OR_LONG_2ADDR:
626 case OP_XOR_LONG_2ADDR:
627 genArithOpLong(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
628 break;
629
630 case OP_SHL_LONG:
631 case OP_SHR_LONG:
632 case OP_USHR_LONG:
633 case OP_SHL_LONG_2ADDR:
634 case OP_SHR_LONG_2ADDR:
635 case OP_USHR_LONG_2ADDR:
636 genShiftOpLong(cUnit,mir, rlDest, rlSrc[0], rlSrc[1]);
637 break;
638
639 case OP_ADD_FLOAT:
640 case OP_SUB_FLOAT:
641 case OP_MUL_FLOAT:
642 case OP_DIV_FLOAT:
643 case OP_REM_FLOAT:
644 case OP_ADD_FLOAT_2ADDR:
645 case OP_SUB_FLOAT_2ADDR:
646 case OP_MUL_FLOAT_2ADDR:
647 case OP_DIV_FLOAT_2ADDR:
648 case OP_REM_FLOAT_2ADDR:
649 genArithOpFloat(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
650 break;
651
652 case OP_ADD_DOUBLE:
653 case OP_SUB_DOUBLE:
654 case OP_MUL_DOUBLE:
655 case OP_DIV_DOUBLE:
656 case OP_REM_DOUBLE:
657 case OP_ADD_DOUBLE_2ADDR:
658 case OP_SUB_DOUBLE_2ADDR:
659 case OP_MUL_DOUBLE_2ADDR:
660 case OP_DIV_DOUBLE_2ADDR:
661 case OP_REM_DOUBLE_2ADDR:
662 genArithOpDouble(cUnit, mir, rlDest, rlSrc[0], rlSrc[1]);
663 break;
664
665 case OP_RSUB_INT:
666 case OP_ADD_INT_LIT16:
667 case OP_MUL_INT_LIT16:
668 case OP_DIV_INT_LIT16:
669 case OP_REM_INT_LIT16:
670 case OP_AND_INT_LIT16:
671 case OP_OR_INT_LIT16:
672 case OP_XOR_INT_LIT16:
673 case OP_ADD_INT_LIT8:
674 case OP_RSUB_INT_LIT8:
675 case OP_MUL_INT_LIT8:
676 case OP_DIV_INT_LIT8:
677 case OP_REM_INT_LIT8:
678 case OP_AND_INT_LIT8:
679 case OP_OR_INT_LIT8:
680 case OP_XOR_INT_LIT8:
681 case OP_SHL_INT_LIT8:
682 case OP_SHR_INT_LIT8:
683 case OP_USHR_INT_LIT8:
684 genArithOpIntLit(cUnit, mir, rlDest, rlSrc[0], mir->dalvikInsn.vC);
685 break;
686
687 default:
688 res = true;
689 }
690 return res;
691}
692
buzbee31a4a6f2012-02-28 15:36:15 -0800693const char* extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
buzbeee3acd072012-02-25 17:03:10 -0800694 "kMirOpPhi",
695 "kMirOpNullNRangeUpCheck",
696 "kMirOpNullNRangeDownCheck",
697 "kMirOpLowerBound",
698 "kMirOpPunt",
699 "kMirOpCheckInlinePrediction",
700};
701
702/* Extended MIR instructions like PHI */
buzbee31a4a6f2012-02-28 15:36:15 -0800703void handleExtendedMethodMIR(CompilationUnit* cUnit, MIR* mir)
buzbeee3acd072012-02-25 17:03:10 -0800704{
705 int opOffset = mir->dalvikInsn.opcode - kMirOpFirst;
706 char* msg = NULL;
707 if (cUnit->printMe) {
708 msg = (char*)oatNew(cUnit, strlen(extendedMIROpNames[opOffset]) + 1,
709 false, kAllocDebugInfo);
710 strcpy(msg, extendedMIROpNames[opOffset]);
711 }
buzbee31a4a6f2012-02-28 15:36:15 -0800712 LIR* op = newLIR1(cUnit, kPseudoExtended, (int) msg);
buzbeee3acd072012-02-25 17:03:10 -0800713
714 switch ((ExtendedMIROpcode)mir->dalvikInsn.opcode) {
715 case kMirOpPhi: {
716 char* ssaString = NULL;
717 if (cUnit->printMe) {
718 ssaString = oatGetSSAString(cUnit, mir->ssaRep);
719 }
720 op->flags.isNop = true;
buzbee31a4a6f2012-02-28 15:36:15 -0800721 newLIR1(cUnit, kPseudoSSARep, (int) ssaString);
buzbeee3acd072012-02-25 17:03:10 -0800722 break;
723 }
724 default:
725 break;
726 }
727}
728
729/* Handle the content in each basic block */
buzbee31a4a6f2012-02-28 15:36:15 -0800730bool methodBlockCodeGen(CompilationUnit* cUnit, BasicBlock* bb)
buzbeee3acd072012-02-25 17:03:10 -0800731{
732 MIR* mir;
buzbee31a4a6f2012-02-28 15:36:15 -0800733 LIR* labelList = (LIR*) cUnit->blockLabelList;
buzbeee3acd072012-02-25 17:03:10 -0800734 int blockId = bb->id;
735
736 cUnit->curBlock = bb;
737 labelList[blockId].operands[0] = bb->startOffset;
738
739 /* Insert the block label */
buzbee31a4a6f2012-02-28 15:36:15 -0800740 labelList[blockId].opcode = kPseudoNormalBlockLabel;
buzbeee3acd072012-02-25 17:03:10 -0800741 oatAppendLIR(cUnit, (LIR*) &labelList[blockId]);
742
743 /* Reset local optimization data on block boundaries */
744 oatResetRegPool(cUnit);
745 oatClobberAllRegs(cUnit);
746 oatResetDefTracking(cUnit);
747
buzbee31a4a6f2012-02-28 15:36:15 -0800748 LIR* headLIR = NULL;
buzbeee3acd072012-02-25 17:03:10 -0800749
750 if (bb->blockType == kEntryBlock) {
751 genEntrySequence(cUnit, bb);
752 } else if (bb->blockType == kExitBlock) {
753 genExitSequence(cUnit, bb);
754 }
755
756 for (mir = bb->firstMIRInsn; mir; mir = mir->next) {
757
758 oatResetRegPool(cUnit);
759 if (cUnit->disableOpt & (1 << kTrackLiveTemps)) {
760 oatClobberAllRegs(cUnit);
761 }
762
763 if (cUnit->disableOpt & (1 << kSuppressLoads)) {
764 oatResetDefTracking(cUnit);
765 }
766
767 if ((int)mir->dalvikInsn.opcode >= (int)kMirOpFirst) {
768 handleExtendedMethodMIR(cUnit, mir);
769 continue;
770 }
771
772 cUnit->currentDalvikOffset = mir->offset;
773
774 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
775 InstructionFormat dalvikFormat =
776 dexGetFormatFromOpcode(dalvikOpcode);
777
buzbee31a4a6f2012-02-28 15:36:15 -0800778 LIR* boundaryLIR;
buzbeee3acd072012-02-25 17:03:10 -0800779
780 /* Mark the beginning of a Dalvik instruction for line tracking */
781 char* instStr = cUnit->printMe ?
782 oatGetDalvikDisassembly(cUnit, &mir->dalvikInsn, "") : NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800783 boundaryLIR = newLIR1(cUnit, kPseudoDalvikByteCodeBoundary,
buzbeee3acd072012-02-25 17:03:10 -0800784 (intptr_t) instStr);
785 cUnit->boundaryMap.insert(std::make_pair(mir->offset,
786 (LIR*)boundaryLIR));
787 /* Remember the first LIR for this block */
788 if (headLIR == NULL) {
789 headLIR = boundaryLIR;
790 /* Set the first boundaryLIR as a scheduling barrier */
791 headLIR->defMask = ENCODE_ALL;
792 }
793
794 /* If we're compiling for the debugger, generate an update callout */
795 if (cUnit->genDebugger) {
796 genDebuggerUpdate(cUnit, mir->offset);
797 }
798
799 /* Don't generate the SSA annotation unless verbose mode is on */
800 if (cUnit->printMe && mir->ssaRep) {
801 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
buzbee31a4a6f2012-02-28 15:36:15 -0800802 newLIR1(cUnit, kPseudoSSARep, (int) ssaString);
buzbeee3acd072012-02-25 17:03:10 -0800803 }
804
805 bool notHandled = compileDalvikInstruction(cUnit, mir, bb, labelList);
806
807 if (notHandled) {
808 char buf[100];
809 snprintf(buf, 100, "%#06x: Opcode %#x (%s) / Fmt %d not handled",
810 mir->offset,
811 dalvikOpcode, dexGetOpcodeName(dalvikOpcode),
812 dalvikFormat);
813 LOG(FATAL) << buf;
814 }
815 }
816
817 if (headLIR) {
818 /*
819 * Eliminate redundant loads/stores and delay stores into later
820 * slots
821 */
822 oatApplyLocalOptimizations(cUnit, (LIR*) headLIR,
823 cUnit->lastLIRInsn);
824
825 /*
826 * Generate an unconditional branch to the fallthrough block.
827 */
828 if (bb->fallThrough) {
829 genUnconditionalBranch(cUnit,
830 &labelList[bb->fallThrough->id]);
831 }
832 }
833 return false;
834}
835
836void oatMethodMIR2LIR(CompilationUnit* cUnit)
837{
838 /* Used to hold the labels of each block */
839 cUnit->blockLabelList =
buzbee31a4a6f2012-02-28 15:36:15 -0800840 (void *) oatNew(cUnit, sizeof(LIR) * cUnit->numBlocks, true,
buzbeee3acd072012-02-25 17:03:10 -0800841 kAllocLIR);
842
843 oatDataFlowAnalysisDispatcher(cUnit, methodBlockCodeGen,
844 kPreOrderDFSTraversal, false /* Iterative */);
845 handleSuspendLaunchpads(cUnit);
846
847 handleThrowLaunchpads(cUnit);
848
849 removeRedundantBranches(cUnit);
850}
851
852/* Needed by the ld/st optmizatons */
buzbee31a4a6f2012-02-28 15:36:15 -0800853LIR* oatRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
buzbeee3acd072012-02-25 17:03:10 -0800854{
855 return genRegCopyNoInsert(cUnit, rDest, rSrc);
856}
857
858/* Needed by the register allocator */
859void oatRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
860{
861 genRegCopy(cUnit, rDest, rSrc);
862}
863
864/* Needed by the register allocator */
865void oatRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
866 int srcLo, int srcHi)
867{
868 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
869}
870
871void oatFlushRegImpl(CompilationUnit* cUnit, int rBase,
872 int displacement, int rSrc, OpSize size)
873{
874 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
875}
876
877void oatFlushRegWideImpl(CompilationUnit* cUnit, int rBase,
878 int displacement, int rSrcLo, int rSrcHi)
879{
880 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
881}
882
883} // namespace art