blob: 37fd65fdb92215729ccbcae5bbc329c4329e91b5 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 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
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
Ben Cheng5d90c202009-11-22 23:31:11 -080027static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
28 int srcSize, int tgtSize)
29{
30 /*
31 * Don't optimize the register usage since it calls out to template
32 * functions
33 */
34 RegLocation rlSrc;
35 RegLocation rlDest;
36 flushAllRegs(cUnit); /* Send everything to home location */
37 if (srcSize == 1) {
38 rlSrc = getSrcLoc(cUnit, mir, 0);
39 loadValueDirectFixed(cUnit, rlSrc, r0);
40 } else {
41 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
42 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
43 }
44 loadConstant(cUnit, r2, (int)funct);
45 opReg(cUnit, kOpBlx, r2);
46 clobberCallRegs(cUnit);
47 if (tgtSize == 1) {
48 RegLocation rlResult;
49 rlDest = getDestLoc(cUnit, mir, 0);
50 rlResult = getReturnLoc(cUnit);
51 storeValue(cUnit, rlDest, rlResult);
52 } else {
53 RegLocation rlResult;
54 rlDest = getDestLocWide(cUnit, mir, 0, 1);
55 rlResult = getReturnLocWide(cUnit);
56 storeValueWide(cUnit, rlDest, rlResult);
57 }
58 return false;
59}
Ben Chengba4fc8b2009-06-01 13:00:29 -070060
Ben Chengba4fc8b2009-06-01 13:00:29 -070061
Ben Cheng5d90c202009-11-22 23:31:11 -080062static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
63 RegLocation rlDest, RegLocation rlSrc1,
64 RegLocation rlSrc2)
65{
66 RegLocation rlResult;
67 void* funct;
68
69 /* TODO: use a proper include file to define these */
70 float __aeabi_fadd(float a, float b);
71 float __aeabi_fsub(float a, float b);
72 float __aeabi_fdiv(float a, float b);
73 float __aeabi_fmul(float a, float b);
74 float fmodf(float a, float b);
75
76 switch (mir->dalvikInsn.opCode) {
77 case OP_ADD_FLOAT_2ADDR:
78 case OP_ADD_FLOAT:
79 funct = (void*) __aeabi_fadd;
80 break;
81 case OP_SUB_FLOAT_2ADDR:
82 case OP_SUB_FLOAT:
83 funct = (void*) __aeabi_fsub;
84 break;
85 case OP_DIV_FLOAT_2ADDR:
86 case OP_DIV_FLOAT:
87 funct = (void*) __aeabi_fdiv;
88 break;
89 case OP_MUL_FLOAT_2ADDR:
90 case OP_MUL_FLOAT:
91 funct = (void*) __aeabi_fmul;
92 break;
93 case OP_REM_FLOAT_2ADDR:
94 case OP_REM_FLOAT:
95 funct = (void*) fmodf;
96 break;
97 case OP_NEG_FLOAT: {
98 genNegFloat(cUnit, rlDest, rlSrc1);
99 return false;
100 }
101 default:
102 return true;
103 }
104 flushAllRegs(cUnit); /* Send everything to home location */
105 loadValueDirectFixed(cUnit, rlSrc1, r0);
106 loadValueDirectFixed(cUnit, rlSrc2, r1);
107 loadConstant(cUnit, r2, (int)funct);
108 opReg(cUnit, kOpBlx, r2);
109 clobberCallRegs(cUnit);
110 rlResult = getReturnLoc(cUnit);
111 storeValue(cUnit, rlDest, rlResult);
112 return false;
113}
114
115static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
116 RegLocation rlDest, RegLocation rlSrc1,
117 RegLocation rlSrc2)
118{
119 RegLocation rlResult;
120 void* funct;
121
122 /* TODO: use a proper include file to define these */
123 double __aeabi_dadd(double a, double b);
124 double __aeabi_dsub(double a, double b);
125 double __aeabi_ddiv(double a, double b);
126 double __aeabi_dmul(double a, double b);
127 double fmod(double a, double b);
128
129 switch (mir->dalvikInsn.opCode) {
130 case OP_ADD_DOUBLE_2ADDR:
131 case OP_ADD_DOUBLE:
132 funct = (void*) __aeabi_dadd;
133 break;
134 case OP_SUB_DOUBLE_2ADDR:
135 case OP_SUB_DOUBLE:
136 funct = (void*) __aeabi_dsub;
137 break;
138 case OP_DIV_DOUBLE_2ADDR:
139 case OP_DIV_DOUBLE:
140 funct = (void*) __aeabi_ddiv;
141 break;
142 case OP_MUL_DOUBLE_2ADDR:
143 case OP_MUL_DOUBLE:
144 funct = (void*) __aeabi_dmul;
145 break;
146 case OP_REM_DOUBLE_2ADDR:
147 case OP_REM_DOUBLE:
148 funct = (void*) fmod;
149 break;
150 case OP_NEG_DOUBLE: {
151 genNegDouble(cUnit, rlDest, rlSrc1);
152 return false;
153 }
154 default:
155 return true;
156 }
157 flushAllRegs(cUnit); /* Send everything to home location */
158 loadConstant(cUnit, rlr, (int)funct);
159 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
160 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
161 opReg(cUnit, kOpBlx, rlr);
162 clobberCallRegs(cUnit);
163 rlResult = getReturnLocWide(cUnit);
164 storeValueWide(cUnit, rlDest, rlResult);
165 return false;
166}
167
168static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
169{
170 OpCode opCode = mir->dalvikInsn.opCode;
171
172 float __aeabi_i2f( int op1 );
173 int __aeabi_f2iz( float op1 );
174 float __aeabi_d2f( double op1 );
175 double __aeabi_f2d( float op1 );
176 double __aeabi_i2d( int op1 );
177 int __aeabi_d2iz( double op1 );
178 float __aeabi_l2f( long op1 );
179 double __aeabi_l2d( long op1 );
180 s8 dvmJitf2l( float op1 );
181 s8 dvmJitd2l( double op1 );
182
183 switch (opCode) {
184 case OP_INT_TO_FLOAT:
185 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
186 case OP_FLOAT_TO_INT:
187 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
188 case OP_DOUBLE_TO_FLOAT:
189 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
190 case OP_FLOAT_TO_DOUBLE:
191 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
192 case OP_INT_TO_DOUBLE:
193 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
194 case OP_DOUBLE_TO_INT:
195 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
196 case OP_FLOAT_TO_LONG:
197 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
198 case OP_LONG_TO_FLOAT:
199 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
200 case OP_DOUBLE_TO_LONG:
201 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
202 case OP_LONG_TO_DOUBLE:
203 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
204 default:
205 return true;
206 }
207 return false;
208}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700209
Jeff Hao97319a82009-08-12 16:57:15 -0700210#if defined(WITH_SELF_VERIFICATION)
Jeff Hao97319a82009-08-12 16:57:15 -0700211/*
212 * The following are used to keep compiled loads and stores from modifying
213 * memory during self verification mode.
214 *
215 * Stores do not modify memory. Instead, the address and value pair are stored
216 * into heapSpace. Addresses within heapSpace are unique. For accesses smaller
217 * than a word, the word containing the address is loaded first before being
218 * updated.
219 *
220 * Loads check heapSpace first and return data from there if an entry exists.
221 * Otherwise, data is loaded from memory as usual.
222 */
223
224/* Decode contents of heapArgSpace to determine addr to load from */
225static void selfVerificationLoadDecode(HeapArgSpace* heapArgSpace, int* addr)
226{
Bill Buzbee1465db52009-09-23 17:17:35 -0700227 int reg = heapArgSpace->regMap & 0xFF;
228 if (!FPREG(reg)) {
229 assert(reg < 16);
230 *addr = heapArgSpace->coreRegs[reg];
231 } else {
232 assert(!DOUBLEREG(reg));
233 *addr = heapArgSpace->fpRegs[(reg & FP_REG_MASK)];
Jeff Hao97319a82009-08-12 16:57:15 -0700234 }
235}
236
237/* Decode contents of heapArgSpace to determine reg to load into */
238static void selfVerificationLoadDecodeData(HeapArgSpace* heapArgSpace,
239 int data, int reg)
240{
Bill Buzbee1465db52009-09-23 17:17:35 -0700241 if (!FPREG(reg)) {
242 assert(reg < 16);
243 heapArgSpace->coreRegs[reg] = data;
244 } else {
245 assert(!DOUBLEREG(reg));
246 heapArgSpace->fpRegs[(reg & FP_REG_MASK)] = data;
Jeff Hao97319a82009-08-12 16:57:15 -0700247 }
248}
249
250static void selfVerificationLoad(InterpState* interpState)
251{
252 Thread *self = dvmThreadSelf();
253 ShadowHeap *heapSpacePtr;
254 ShadowSpace *shadowSpace = self->shadowSpace;
255 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
256
257 int addr, data;
258 selfVerificationLoadDecode(heapArgSpace, &addr);
259
260 for (heapSpacePtr = shadowSpace->heapSpace;
261 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
262 if (heapSpacePtr->addr == addr) {
263 data = heapSpacePtr->data;
264 break;
265 }
266 }
267
268 if (heapSpacePtr == shadowSpace->heapSpaceTail)
269 data = *((unsigned int*) addr);
270
Bill Buzbee1465db52009-09-23 17:17:35 -0700271 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Ben Chengd7d426a2009-09-22 11:23:36 -0700272
Bill Buzbee1465db52009-09-23 17:17:35 -0700273 // LOGD("*** HEAP LOAD: Reg:%d Addr: 0x%x Data: 0x%x", reg, addr, data);
Ben Chengd7d426a2009-09-22 11:23:36 -0700274
Jeff Hao97319a82009-08-12 16:57:15 -0700275 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
276}
277
278static void selfVerificationLoadByte(InterpState* interpState)
279{
280 Thread *self = dvmThreadSelf();
281 ShadowHeap *heapSpacePtr;
282 ShadowSpace *shadowSpace = self->shadowSpace;
283 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
284
285 int addr, data;
286 selfVerificationLoadDecode(heapArgSpace, &addr);
287
288 int maskedAddr = addr & 0xFFFFFFFC;
289 int alignment = addr & 0x3;
290
291 for (heapSpacePtr = shadowSpace->heapSpace;
292 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
293 if (heapSpacePtr->addr == maskedAddr) {
294 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
295 data = *((unsigned char*) addr);
296 break;
297 }
298 }
299
300 if (heapSpacePtr == shadowSpace->heapSpaceTail)
301 data = *((unsigned char*) addr);
302
303 //LOGD("*** HEAP LOAD BYTE: Addr: 0x%x Data: 0x%x", addr, data);
304
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700306 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
307}
308
309static void selfVerificationLoadHalfword(InterpState* interpState)
310{
311 Thread *self = dvmThreadSelf();
312 ShadowHeap *heapSpacePtr;
313 ShadowSpace *shadowSpace = self->shadowSpace;
314 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
315
316 int addr, data;
317 selfVerificationLoadDecode(heapArgSpace, &addr);
318
319 int maskedAddr = addr & 0xFFFFFFFC;
320 int alignment = addr & 0x2;
321
322 for (heapSpacePtr = shadowSpace->heapSpace;
323 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
324 if (heapSpacePtr->addr == maskedAddr) {
325 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
326 data = *((unsigned short*) addr);
327 break;
328 }
329 }
330
331 if (heapSpacePtr == shadowSpace->heapSpaceTail)
332 data = *((unsigned short*) addr);
333
Bill Buzbee1465db52009-09-23 17:17:35 -0700334 //LOGD("*** HEAP LOAD kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700335
Bill Buzbee1465db52009-09-23 17:17:35 -0700336 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700337 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
338}
339
340static void selfVerificationLoadSignedByte(InterpState* interpState)
341{
342 Thread *self = dvmThreadSelf();
343 ShadowHeap* heapSpacePtr;
344 ShadowSpace* shadowSpace = self->shadowSpace;
345 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
346
347 int addr, data;
348 selfVerificationLoadDecode(heapArgSpace, &addr);
349
350 int maskedAddr = addr & 0xFFFFFFFC;
351 int alignment = addr & 0x3;
352
353 for (heapSpacePtr = shadowSpace->heapSpace;
354 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
355 if (heapSpacePtr->addr == maskedAddr) {
356 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
357 data = *((signed char*) addr);
358 break;
359 }
360 }
361
362 if (heapSpacePtr == shadowSpace->heapSpaceTail)
363 data = *((signed char*) addr);
364
365 //LOGD("*** HEAP LOAD SIGNED BYTE: Addr: 0x%x Data: 0x%x", addr, data);
366
Bill Buzbee1465db52009-09-23 17:17:35 -0700367 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700368 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
369}
370
371static void selfVerificationLoadSignedHalfword(InterpState* interpState)
372{
373 Thread *self = dvmThreadSelf();
374 ShadowHeap* heapSpacePtr;
375 ShadowSpace* shadowSpace = self->shadowSpace;
376 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
377
378 int addr, data;
379 selfVerificationLoadDecode(heapArgSpace, &addr);
380
381 int maskedAddr = addr & 0xFFFFFFFC;
382 int alignment = addr & 0x2;
383
384 for (heapSpacePtr = shadowSpace->heapSpace;
385 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
386 if (heapSpacePtr->addr == maskedAddr) {
387 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
388 data = *((signed short*) addr);
389 break;
390 }
391 }
392
393 if (heapSpacePtr == shadowSpace->heapSpaceTail)
394 data = *((signed short*) addr);
395
Bill Buzbee1465db52009-09-23 17:17:35 -0700396 //LOGD("*** HEAP LOAD SIGNED kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700397
Bill Buzbee1465db52009-09-23 17:17:35 -0700398 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700399 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
400}
401
402static void selfVerificationLoadDoubleword(InterpState* interpState)
403{
404 Thread *self = dvmThreadSelf();
405 ShadowHeap* heapSpacePtr;
406 ShadowSpace* shadowSpace = self->shadowSpace;
407 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
408
409 int addr;
410 selfVerificationLoadDecode(heapArgSpace, &addr);
411
412 int addr2 = addr+4;
413 unsigned int data = *((unsigned int*) addr);
414 unsigned int data2 = *((unsigned int*) addr2);
415
416 for (heapSpacePtr = shadowSpace->heapSpace;
417 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
418 if (heapSpacePtr->addr == addr) {
419 data = heapSpacePtr->data;
420 } else if (heapSpacePtr->addr == addr2) {
421 data2 = heapSpacePtr->data;
422 }
423 }
424
Bill Buzbee1465db52009-09-23 17:17:35 -0700425 // LOGD("*** HEAP LOAD DOUBLEWORD: Addr: 0x%x Data: 0x%x Data2: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700426 // addr, data, data2);
427
Bill Buzbee1465db52009-09-23 17:17:35 -0700428 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
429 int reg2 = (heapArgSpace->regMap >> 16) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700430 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
431 selfVerificationLoadDecodeData(heapArgSpace, data2, reg2);
432}
433
434/* Decode contents of heapArgSpace to determine arguments to store. */
435static void selfVerificationStoreDecode(HeapArgSpace* heapArgSpace,
436 int* value, int reg)
437{
Bill Buzbee1465db52009-09-23 17:17:35 -0700438 if (!FPREG(reg)) {
439 assert(reg < 16);
440 *value = heapArgSpace->coreRegs[reg];
441 } else {
442 assert(!DOUBLEREG(reg));
443 *value = heapArgSpace->fpRegs[(reg & FP_REG_MASK)];
Jeff Hao97319a82009-08-12 16:57:15 -0700444 }
445}
446
447static void selfVerificationStore(InterpState* interpState)
448{
449 Thread *self = dvmThreadSelf();
450 ShadowHeap *heapSpacePtr;
451 ShadowSpace *shadowSpace = self->shadowSpace;
452 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
453
454 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700455 int reg0 = heapArgSpace->regMap & 0xFF;
456 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700457 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
458 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
459
460 //LOGD("*** HEAP STORE: Addr: 0x%x Data: 0x%x", addr, data);
461
462 for (heapSpacePtr = shadowSpace->heapSpace;
463 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
464 if (heapSpacePtr->addr == addr) break;
465 }
466
467 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
468 heapSpacePtr->addr = addr;
469 shadowSpace->heapSpaceTail++;
470 }
471
472 heapSpacePtr->data = data;
473}
474
475static void selfVerificationStoreByte(InterpState* interpState)
476{
477 Thread *self = dvmThreadSelf();
478 ShadowHeap *heapSpacePtr;
479 ShadowSpace *shadowSpace = self->shadowSpace;
480 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
481
482 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700483 int reg0 = heapArgSpace->regMap & 0xFF;
484 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700485 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
486 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
487
488 int maskedAddr = addr & 0xFFFFFFFC;
489 int alignment = addr & 0x3;
490
491 //LOGD("*** HEAP STORE BYTE: Addr: 0x%x Data: 0x%x", addr, data);
492
493 for (heapSpacePtr = shadowSpace->heapSpace;
494 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
495 if (heapSpacePtr->addr == maskedAddr) break;
496 }
497
498 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
499 heapSpacePtr->addr = maskedAddr;
500 heapSpacePtr->data = *((unsigned int*) maskedAddr);
501 shadowSpace->heapSpaceTail++;
502 }
503
504 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
505 *((unsigned char*) addr) = (char) data;
506
507 //LOGD("*** HEAP STORE BYTE: Addr: 0x%x Final Data: 0x%x",
508 // addr, heapSpacePtr->data);
509}
510
511static void selfVerificationStoreHalfword(InterpState* interpState)
512{
513 Thread *self = dvmThreadSelf();
514 ShadowHeap *heapSpacePtr;
515 ShadowSpace *shadowSpace = self->shadowSpace;
516 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
517
518 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700519 int reg0 = heapArgSpace->regMap & 0xFF;
520 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700521 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
522 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
523
524 int maskedAddr = addr & 0xFFFFFFFC;
525 int alignment = addr & 0x2;
526
Bill Buzbee1465db52009-09-23 17:17:35 -0700527 //LOGD("*** HEAP STORE kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700528
529 for (heapSpacePtr = shadowSpace->heapSpace;
530 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
531 if (heapSpacePtr->addr == maskedAddr) break;
532 }
533
534 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
535 heapSpacePtr->addr = maskedAddr;
536 heapSpacePtr->data = *((unsigned int*) maskedAddr);
537 shadowSpace->heapSpaceTail++;
538 }
539
540 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
541 *((unsigned short*) addr) = (short) data;
542
Bill Buzbee1465db52009-09-23 17:17:35 -0700543 //LOGD("*** HEAP STORE kHalfWord: Addr: 0x%x Final Data: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700544 // addr, heapSpacePtr->data);
545}
546
547static void selfVerificationStoreDoubleword(InterpState* interpState)
548{
549 Thread *self = dvmThreadSelf();
550 ShadowHeap *heapSpacePtr;
551 ShadowSpace *shadowSpace = self->shadowSpace;
552 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
553
554 int addr, data, data2;
Bill Buzbee1465db52009-09-23 17:17:35 -0700555 int reg0 = heapArgSpace->regMap & 0xFF;
556 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
557 int reg2 = (heapArgSpace->regMap >> 16) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700558 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
559 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
560 selfVerificationStoreDecode(heapArgSpace, &data2, reg2);
561
562 int addr2 = addr+4;
563 bool store1 = false, store2 = false;
564
565 //LOGD("*** HEAP STORE DOUBLEWORD: Addr: 0x%x Data: 0x%x, Data2: 0x%x",
566 // addr, data, data2);
567
568 for (heapSpacePtr = shadowSpace->heapSpace;
569 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
570 if (heapSpacePtr->addr == addr) {
571 heapSpacePtr->data = data;
572 store1 = true;
573 } else if (heapSpacePtr->addr == addr2) {
574 heapSpacePtr->data = data2;
575 store2 = true;
576 }
577 }
578
579 if (!store1) {
580 shadowSpace->heapSpaceTail->addr = addr;
581 shadowSpace->heapSpaceTail->data = data;
582 shadowSpace->heapSpaceTail++;
583 }
584 if (!store2) {
585 shadowSpace->heapSpaceTail->addr = addr2;
586 shadowSpace->heapSpaceTail->data = data2;
587 shadowSpace->heapSpaceTail++;
588 }
589}
590
591/* Common wrapper function for all memory operations */
592static void selfVerificationMemOpWrapper(CompilationUnit *cUnit, int regMap,
593 void* funct)
594{
Bill Buzbee1465db52009-09-23 17:17:35 -0700595 /* push r0 and r7 to give us a foothold */
596 newLIR1(cUnit, kThumbPush, (1 << r0) | (1 << r7));
Jeff Hao97319a82009-08-12 16:57:15 -0700597
Bill Buzbee1465db52009-09-23 17:17:35 -0700598 /* Let the save handler know where the save record is */
599 loadConstant(cUnit, r0, offsetof(InterpState, heapArgSpace));
Jeff Hao97319a82009-08-12 16:57:15 -0700600
Bill Buzbee1465db52009-09-23 17:17:35 -0700601 /* Load the regMap and call the save handler [note: handler pops r0/r7] */
602 loadConstant(cUnit, r7, regMap);
603 genDispatchToHandler(cUnit, TEMPLATE_SAVE_STATE);
Jeff Hao97319a82009-08-12 16:57:15 -0700604
Bill Buzbee1465db52009-09-23 17:17:35 -0700605 /* Set function pointer, pass rGLUE and branch */
Jeff Hao97319a82009-08-12 16:57:15 -0700606 loadConstant(cUnit, r1, (int) funct);
Bill Buzbee1465db52009-09-23 17:17:35 -0700607 newLIR2(cUnit, kThumbMovRR, r0, rGLUE);
608 newLIR1(cUnit, kThumbBlxR, r1);
Jeff Hao97319a82009-08-12 16:57:15 -0700609
Bill Buzbee1465db52009-09-23 17:17:35 -0700610 /* Let the recover handler know where coreRegs[0] and restore regs */
611 loadConstant(cUnit, r0, offsetof(InterpState, heapArgSpace) +
612 offsetof(HeapArgSpace, coreRegs));
613 genDispatchToHandler(cUnit, TEMPLATE_RESTORE_STATE);
Jeff Hao97319a82009-08-12 16:57:15 -0700614}
Ben Cheng5d90c202009-11-22 23:31:11 -0800615
Jeff Hao97319a82009-08-12 16:57:15 -0700616#endif
617
Ben Chengba4fc8b2009-06-01 13:00:29 -0700618/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700619static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
620 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700621{
Bill Buzbee1465db52009-09-23 17:17:35 -0700622 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700623 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
624}
625
626/* Load a wide field from an object instance */
627static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
628{
629 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700630 RegLocation rlObj = getSrcLoc(cUnit, mir, 0);
631 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
632 RegLocation rlResult;
633 rlObj = loadValue(cUnit, rlObj, kCoreReg);
634 int regPtr = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700635
Bill Buzbee1465db52009-09-23 17:17:35 -0700636 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700637
Bill Buzbee1465db52009-09-23 17:17:35 -0700638 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
639 NULL);/* null object? */
640 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
641 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Jeff Hao97319a82009-08-12 16:57:15 -0700642#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700643 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -0700644#else
Bill Buzbee1465db52009-09-23 17:17:35 -0700645 int regMap = rlResult.highReg << 16 | rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700646 selfVerificationMemOpWrapper(cUnit, regMap,
647 &selfVerificationLoadDoubleword);
Jeff Hao97319a82009-08-12 16:57:15 -0700648#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 freeTemp(cUnit, regPtr);
650 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700651}
652
653/* Store a wide field to an object instance */
654static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
655{
656 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700657 RegLocation rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
658 RegLocation rlObj = getSrcLoc(cUnit, mir, 2);
659 rlObj = loadValue(cUnit, rlObj, kCoreReg);
660 int regPtr;
661 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
662 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
663 NULL);/* null object? */
664 regPtr = allocTemp(cUnit);
665 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700666#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700667 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -0700668#else
Bill Buzbee1465db52009-09-23 17:17:35 -0700669 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700670 selfVerificationMemOpWrapper(cUnit, regMap,
671 &selfVerificationStoreDoubleword);
672#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700673 freeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700674}
675
676/*
677 * Load a field from an object instance
678 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700679 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700680static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700681 int fieldOffset)
682{
Bill Buzbee1465db52009-09-23 17:17:35 -0700683 int regPtr;
684 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700685 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700686 RegLocation rlObj = getSrcLoc(cUnit, mir, 0);
687 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
688 rlObj = loadValue(cUnit, rlObj, kCoreReg);
689 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700690 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
691 NULL);/* null object? */
Ben Cheng5d90c202009-11-22 23:31:11 -0800692#if !defined(WITH_SELF_VERIFICATION)
693 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
694 size, rlObj.sRegLow);
695#else
Jeff Hao97319a82009-08-12 16:57:15 -0700696 /* Combine address and offset */
Bill Buzbee1465db52009-09-23 17:17:35 -0700697 regPtr = allocTemp(cUnit);
698 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700699
Bill Buzbee1465db52009-09-23 17:17:35 -0700700 int regMap = rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700701 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationLoad);
Bill Buzbee1465db52009-09-23 17:17:35 -0700702 freeTemp(cUnit, regPtr);
Jeff Hao97319a82009-08-12 16:57:15 -0700703#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700704 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700705}
706
707/*
708 * Store a field to an object instance
709 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700710 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700711static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700712 int fieldOffset)
713{
714 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700715 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
716 RegLocation rlObj = getSrcLoc(cUnit, mir, 1);
717 rlObj = loadValue(cUnit, rlObj, kCoreReg);
718 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
719 int regPtr;
720 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
721 NULL);/* null object? */
Jeff Hao97319a82009-08-12 16:57:15 -0700722#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700723 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Jeff Hao97319a82009-08-12 16:57:15 -0700724#else
725 /* Combine address and offset */
Bill Buzbee1465db52009-09-23 17:17:35 -0700726 regPtr = allocTemp(cUnit);
727 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700728
Bill Buzbee1465db52009-09-23 17:17:35 -0700729 int regMap = rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700730 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationStore);
Jeff Hao97319a82009-08-12 16:57:15 -0700731#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700732}
733
734
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735/*
736 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700737 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700738static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700739 RegLocation rlArray, RegLocation rlIndex,
740 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700741{
742 int lenOffset = offsetof(ArrayObject, length);
743 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 RegLocation rlResult;
745 rlArray = loadValue(cUnit, rlArray, kCoreReg);
746 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
747 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700748
749 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700750 ArmLIR * pcrLabel = NULL;
751
752 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700753 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
754 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700755 }
756
Bill Buzbee1465db52009-09-23 17:17:35 -0700757 regPtr = allocTemp(cUnit);
758
Ben Cheng4238ec22009-08-24 16:32:22 -0700759 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700760 int regLen = allocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700761 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700762 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
763 /* regPtr -> array data */
764 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
765 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
766 pcrLabel);
767 freeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700768 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700769 /* regPtr -> array data */
770 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700771 }
Jeff Hao97319a82009-08-12 16:57:15 -0700772#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700773 if ((size == kLong) || (size == kDouble)) {
774 if (scale) {
775 int rNewIndex = allocTemp(cUnit);
776 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
777 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
778 freeTemp(cUnit, rNewIndex);
779 } else {
780 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
781 }
782 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
783 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
784 freeTemp(cUnit, regPtr);
785 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700786 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
788 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
789 scale, size);
790 freeTemp(cUnit, regPtr);
791 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792 }
Jeff Hao97319a82009-08-12 16:57:15 -0700793#else
Bill Buzbee270c1d62009-08-13 16:58:07 -0700794 //TODO: probably want to move this into loadBaseIndexed
795 void *funct = NULL;
796 switch(size) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700797 case kLong:
798 case kDouble:
Jeff Hao97319a82009-08-12 16:57:15 -0700799 funct = (void*) &selfVerificationLoadDoubleword;
800 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700801 case kWord:
Jeff Hao97319a82009-08-12 16:57:15 -0700802 funct = (void*) &selfVerificationLoad;
803 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700804 case kUnsignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700805 funct = (void*) &selfVerificationLoadHalfword;
806 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700807 case kSignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700808 funct = (void*) &selfVerificationLoadSignedHalfword;
809 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700810 case kUnsignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700811 funct = (void*) &selfVerificationLoadByte;
812 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700813 case kSignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700814 funct = (void*) &selfVerificationLoadSignedByte;
815 break;
816 default:
817 assert(0);
818 dvmAbort();
Jeff Hao97319a82009-08-12 16:57:15 -0700819 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700820 /* Combine address and index */
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 if (scale) {
822 int regTmp = allocTemp(cUnit);
823 opRegRegImm(cUnit, kOpLsl, regTmp, rlIndex.lowReg, scale);
824 opRegReg(cUnit, kOpAdd, regPtr, regTmp);
825 freeTemp(cUnit, regTmp);
826 } else {
827 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
828 }
Jeff Hao97319a82009-08-12 16:57:15 -0700829
Bill Buzbee1465db52009-09-23 17:17:35 -0700830 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
831 int regMap = rlResult.highReg << 16 | rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700832 selfVerificationMemOpWrapper(cUnit, regMap, funct);
833
Bill Buzbee1465db52009-09-23 17:17:35 -0700834 freeTemp(cUnit, regPtr);
835 if ((size == kLong) || (size == kDouble))
836 storeValueWide(cUnit, rlDest, rlResult);
Jeff Hao97319a82009-08-12 16:57:15 -0700837 else
Bill Buzbee1465db52009-09-23 17:17:35 -0700838 storeValue(cUnit, rlDest, rlResult);
Jeff Hao97319a82009-08-12 16:57:15 -0700839#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700840}
841
Ben Chengba4fc8b2009-06-01 13:00:29 -0700842/*
843 * Generate array store
844 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700845 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700846static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700847 RegLocation rlArray, RegLocation rlIndex,
848 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700849{
850 int lenOffset = offsetof(ArrayObject, length);
851 int dataOffset = offsetof(ArrayObject, contents);
852
Bill Buzbee1465db52009-09-23 17:17:35 -0700853 int regPtr;
854 rlArray = loadValue(cUnit, rlArray, kCoreReg);
855 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700856
Bill Buzbee1465db52009-09-23 17:17:35 -0700857 if (isTemp(cUnit, rlArray.lowReg)) {
858 clobberReg(cUnit, rlArray.lowReg);
859 regPtr = rlArray.lowReg;
860 } else {
861 regPtr = allocTemp(cUnit);
862 genRegCopy(cUnit, regPtr, rlArray.lowReg);
863 }
Ben Chenge9695e52009-06-16 16:11:47 -0700864
Ben Cheng1efc9c52009-06-08 18:25:27 -0700865 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700866 ArmLIR * pcrLabel = NULL;
867
868 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700869 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
870 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700871 }
872
873 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700874 int regLen = allocTemp(cUnit);
875 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700876 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700877 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
878 /* regPtr -> array data */
879 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
880 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
881 pcrLabel);
882 freeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700883 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700884 /* regPtr -> array data */
885 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700886 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700887 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700888#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700889 if ((size == kLong) || (size == kDouble)) {
890 //TODO: need specific wide routine that can handle fp regs
891 if (scale) {
892 int rNewIndex = allocTemp(cUnit);
893 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
894 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
895 freeTemp(cUnit, rNewIndex);
896 } else {
897 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
898 }
899 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
900 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
901 freeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700902 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700903 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
904 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
905 scale, size);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700906 }
907#else
908 //TODO: probably want to move this into storeBaseIndexed
909 void *funct = NULL;
910 switch(size) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700911 case kLong:
912 case kDouble:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700913 funct = (void*) &selfVerificationStoreDoubleword;
914 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700915 case kWord:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700916 funct = (void*) &selfVerificationStore;
917 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700918 case kSignedHalf:
919 case kUnsignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700920 funct = (void*) &selfVerificationStoreHalfword;
921 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700922 case kSignedByte:
923 case kUnsignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700924 funct = (void*) &selfVerificationStoreByte;
925 break;
926 default:
927 assert(0);
928 dvmAbort();
929 }
930
Bill Buzbee1465db52009-09-23 17:17:35 -0700931 if (scale) {
932 int regTmpIndex = allocTemp(cUnit);
933 // 3 live temps
934 opRegRegImm(cUnit, kOpLsl, regTmpIndex, rlIndex.lowReg, scale);
935 opRegReg(cUnit, kOpAdd, regPtr, regTmpIndex);
936 freeTemp(cUnit, regTmpIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700937 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700938 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700939 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700940 /* Combine address and index */
941 if ((size == kLong) || (size == kDouble)) {
942 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
943 } else {
944 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
945 }
Jeff Hao97319a82009-08-12 16:57:15 -0700946
Bill Buzbee1465db52009-09-23 17:17:35 -0700947 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700948 selfVerificationMemOpWrapper(cUnit, regMap, funct);
949
Jeff Hao97319a82009-08-12 16:57:15 -0700950#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700951}
952
Ben Cheng5d90c202009-11-22 23:31:11 -0800953static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
954 RegLocation rlDest, RegLocation rlSrc1,
955 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700956{
Ben Chenge9695e52009-06-16 16:11:47 -0700957 /*
958 * Don't mess with the regsiters here as there is a particular calling
959 * convention to the out-of-line handler.
960 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700961 RegLocation rlResult;
962
963 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
964 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700965 switch( mir->dalvikInsn.opCode) {
966 case OP_SHL_LONG:
967 case OP_SHL_LONG_2ADDR:
968 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
969 break;
970 case OP_SHR_LONG:
971 case OP_SHR_LONG_2ADDR:
972 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
973 break;
974 case OP_USHR_LONG:
975 case OP_USHR_LONG_2ADDR:
976 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
977 break;
978 default:
979 return true;
980 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700981 rlResult = getReturnLocWide(cUnit);
982 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700983 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700984}
Ben Chenge9695e52009-06-16 16:11:47 -0700985
Ben Cheng5d90c202009-11-22 23:31:11 -0800986static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
987 RegLocation rlDest, RegLocation rlSrc1,
988 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700989{
Bill Buzbee1465db52009-09-23 17:17:35 -0700990 RegLocation rlResult;
991 OpKind firstOp = kOpBkpt;
992 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700993 bool callOut = false;
994 void *callTgt;
995 int retReg = r0;
996 /* TODO - find proper .h file to declare these */
997 long long __aeabi_ldivmod(long long op1, long long op2);
998
999 switch (mir->dalvikInsn.opCode) {
1000 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07001001 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1002 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1003 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1004 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1005 storeValueWide(cUnit, rlDest, rlResult);
1006 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007 break;
1008 case OP_ADD_LONG:
1009 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001010 firstOp = kOpAdd;
1011 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001012 break;
1013 case OP_SUB_LONG:
1014 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001015 firstOp = kOpSub;
1016 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001017 break;
1018 case OP_MUL_LONG:
1019 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001020 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001021 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001022 case OP_DIV_LONG:
1023 case OP_DIV_LONG_2ADDR:
1024 callOut = true;
1025 retReg = r0;
1026 callTgt = (void*)__aeabi_ldivmod;
1027 break;
1028 /* NOTE - result is in r2/r3 instead of r0/r1 */
1029 case OP_REM_LONG:
1030 case OP_REM_LONG_2ADDR:
1031 callOut = true;
1032 callTgt = (void*)__aeabi_ldivmod;
1033 retReg = r2;
1034 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001035 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001036 case OP_AND_LONG:
1037 firstOp = kOpAnd;
1038 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001039 break;
1040 case OP_OR_LONG:
1041 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001042 firstOp = kOpOr;
1043 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001044 break;
1045 case OP_XOR_LONG:
1046 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001047 firstOp = kOpXor;
1048 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001049 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001050 case OP_NEG_LONG: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001051 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1052 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1053 loadConstantValue(cUnit, rlResult.highReg, 0);
1054 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1055 rlResult.highReg, rlSrc2.lowReg);
1056 opRegReg(cUnit, kOpSbc, rlResult.highReg, rlSrc2.highReg);
1057 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001058 return false;
Ben Chenge9695e52009-06-16 16:11:47 -07001059 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001060 default:
1061 LOGE("Invalid long arith op");
1062 dvmAbort();
1063 }
1064 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001065 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001066 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001067 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001068 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001069 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1070 loadConstant(cUnit, rlr, (int) callTgt);
1071 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1072 opReg(cUnit, kOpBlx, rlr);
1073 clobberCallRegs(cUnit);
1074 if (retReg == r0)
1075 rlResult = getReturnLocWide(cUnit);
1076 else
1077 rlResult = getReturnLocWideAlt(cUnit);
1078 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001079 }
1080 return false;
1081}
1082
Ben Cheng5d90c202009-11-22 23:31:11 -08001083static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
1084 RegLocation rlDest, RegLocation rlSrc1,
1085 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001086{
Bill Buzbee1465db52009-09-23 17:17:35 -07001087 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001088 bool callOut = false;
1089 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -07001090 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001091 int retReg = r0;
1092 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -07001093 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001094
1095 /* TODO - find proper .h file to declare these */
1096 int __aeabi_idivmod(int op1, int op2);
1097 int __aeabi_idiv(int op1, int op2);
1098
1099 switch (mir->dalvikInsn.opCode) {
1100 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001101 op = kOpNeg;
1102 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001103 break;
1104 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001105 op = kOpMvn;
1106 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001107 break;
1108 case OP_ADD_INT:
1109 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001110 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001111 break;
1112 case OP_SUB_INT:
1113 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001114 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001115 break;
1116 case OP_MUL_INT:
1117 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001118 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001119 break;
1120 case OP_DIV_INT:
1121 case OP_DIV_INT_2ADDR:
1122 callOut = true;
1123 checkZero = true;
1124 callTgt = __aeabi_idiv;
1125 retReg = r0;
1126 break;
1127 /* NOTE: returns in r1 */
1128 case OP_REM_INT:
1129 case OP_REM_INT_2ADDR:
1130 callOut = true;
1131 checkZero = true;
1132 callTgt = __aeabi_idivmod;
1133 retReg = r1;
1134 break;
1135 case OP_AND_INT:
1136 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001137 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001138 break;
1139 case OP_OR_INT:
1140 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001141 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001142 break;
1143 case OP_XOR_INT:
1144 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001145 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001146 break;
1147 case OP_SHL_INT:
1148 case OP_SHL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001149 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001150 break;
1151 case OP_SHR_INT:
1152 case OP_SHR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001153 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001154 break;
1155 case OP_USHR_INT:
1156 case OP_USHR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001157 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001158 break;
1159 default:
1160 LOGE("Invalid word arith op: 0x%x(%d)",
1161 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
1162 dvmAbort();
1163 }
1164 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001165 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1166 if (unary) {
1167 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1168 opRegReg(cUnit, op, rlResult.lowReg,
1169 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -07001170 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001171 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1172 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1173 opRegRegReg(cUnit, op, rlResult.lowReg,
1174 rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -07001175 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001176 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001177 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001178 RegLocation rlResult;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001179 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001180 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001181 loadConstant(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -07001182 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001183 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001184 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001185 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001186 opReg(cUnit, kOpBlx, r2);
1187 clobberCallRegs(cUnit);
1188 if (retReg == r0)
1189 rlResult = getReturnLoc(cUnit);
1190 else
1191 rlResult = getReturnLocAlt(cUnit);
1192 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001193 }
1194 return false;
1195}
1196
Ben Cheng5d90c202009-11-22 23:31:11 -08001197static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001198{
1199 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001200 RegLocation rlDest;
1201 RegLocation rlSrc1;
1202 RegLocation rlSrc2;
1203 /* Deduce sizes of operands */
1204 if (mir->ssaRep->numUses == 2) {
1205 rlSrc1 = getSrcLoc(cUnit, mir, 0);
1206 rlSrc2 = getSrcLoc(cUnit, mir, 1);
1207 } else if (mir->ssaRep->numUses == 3) {
1208 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
1209 rlSrc2 = getSrcLoc(cUnit, mir, 2);
1210 } else {
1211 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
1212 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
1213 assert(mir->ssaRep->numUses == 4);
1214 }
1215 if (mir->ssaRep->numDefs == 1) {
1216 rlDest = getDestLoc(cUnit, mir, 0);
1217 } else {
1218 assert(mir->ssaRep->numDefs == 2);
1219 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1220 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001221
1222 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001223 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001224 }
1225 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001226 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001227 }
1228 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001229 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001230 }
1231 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001232 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001233 }
1234 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001235 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001236 }
1237 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001238 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001239 }
1240 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001241 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001242 }
1243 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001244 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001245 }
1246 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001247 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001248 }
1249 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001250 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001251 }
1252 return true;
1253}
1254
Bill Buzbee1465db52009-09-23 17:17:35 -07001255/* Generate conditional branch instructions */
1256static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
1257 ArmConditionCode cond,
1258 ArmLIR *target)
1259{
1260 ArmLIR *branch = opCondBranch(cUnit, cond);
1261 branch->generic.target = (LIR *) target;
1262 return branch;
1263}
1264
1265/* Generate unconditional branch instructions */
1266static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
1267{
1268 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
1269 branch->generic.target = (LIR *) target;
1270 return branch;
1271}
1272
Bill Buzbee1465db52009-09-23 17:17:35 -07001273/* Perform the actual operation for OP_RETURN_* */
1274static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
1275{
1276 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
1277#if defined(INVOKE_STATS)
1278 gDvmJit.returnOp++;
1279#endif
1280 int dPC = (int) (cUnit->method->insns + mir->offset);
1281 /* Insert branch, but defer setting of target */
1282 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
1283 /* Set up the place holder to reconstruct this Dalvik PC */
1284 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
1285 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
1286 pcrLabel->operands[0] = dPC;
1287 pcrLabel->operands[1] = mir->offset;
1288 /* Insert the place holder to the growable list */
1289 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1290 /* Branch to the PC reconstruction code */
1291 branch->generic.target = (LIR *) pcrLabel;
1292}
1293
Ben Chengba4fc8b2009-06-01 13:00:29 -07001294static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
1295 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001296 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001297{
1298 unsigned int i;
1299 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -07001300 RegLocation rlArg;
1301 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001302
Bill Buzbee1465db52009-09-23 17:17:35 -07001303 /*
1304 * Load arguments to r0..r4. Note that these registers may contain
1305 * live values, so we clobber them immediately after loading to prevent
1306 * them from being used as sources for subsequent loads.
1307 */
1308 lockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001309 for (i = 0; i < dInsn->vA; i++) {
1310 regMask |= 1 << i;
Bill Buzbee1465db52009-09-23 17:17:35 -07001311 rlArg = getSrcLoc(cUnit, mir, numDone++);
1312 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001313 }
1314 if (regMask) {
1315 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -07001316 opRegRegImm(cUnit, kOpSub, r7, rFP,
1317 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001318 /* generate null check */
1319 if (pcrLabel) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001320 *pcrLabel = genNullCheck(cUnit, getSrcSSAName(mir, 0), r0,
1321 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001322 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001323 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001324 }
1325}
1326
1327static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1328 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001329 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001330{
1331 int srcOffset = dInsn->vC << 2;
1332 int numArgs = dInsn->vA;
1333 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001334
1335 /*
1336 * Note: here, all promoted registers will have been flushed
1337 * back to the Dalvik base locations, so register usage restrictins
1338 * are lifted. All parms loaded from original Dalvik register
1339 * region - even though some might conceivably have valid copies
1340 * cached in a preserved register.
1341 */
1342 lockAllTemps(cUnit);
1343
Ben Chengba4fc8b2009-06-01 13:00:29 -07001344 /*
1345 * r4PC : &rFP[vC]
1346 * r7: &newFP[0]
1347 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001348 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001349 /* load [r0 .. min(numArgs,4)] */
1350 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001351 /*
1352 * Protect the loadMultiple instruction from being reordered with other
1353 * Dalvik stack accesses.
1354 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001355 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001356
Bill Buzbee1465db52009-09-23 17:17:35 -07001357 opRegRegImm(cUnit, kOpSub, r7, rFP,
1358 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001359 /* generate null check */
1360 if (pcrLabel) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001361 *pcrLabel = genNullCheck(cUnit, getSrcSSAName(mir, 0), r0,
1362 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001363 }
1364
1365 /*
1366 * Handle remaining 4n arguments:
1367 * store previously loaded 4 values and load the next 4 values
1368 */
1369 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001370 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001371 /*
1372 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001373 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001374 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001375 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001376 /* No need to generate the loop structure if numArgs <= 11 */
1377 if (numArgs > 11) {
1378 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001379 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001380 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001381 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001382 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001383 /*
1384 * Protect the loadMultiple instruction from being reordered with other
1385 * Dalvik stack accesses.
1386 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001387 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001388 /* No need to generate the loop structure if numArgs <= 11 */
1389 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001390 opRegImm(cUnit, kOpSub, rFP, 4);
1391 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001392 }
1393 }
1394
1395 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001396 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001397
1398 /* Generate the loop epilogue - don't use r0 */
1399 if ((numArgs > 4) && (numArgs % 4)) {
1400 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001401 /*
1402 * Protect the loadMultiple instruction from being reordered with other
1403 * Dalvik stack accesses.
1404 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001405 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001406 }
1407 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001408 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001409
1410 /* Save the modulo 4 arguments */
1411 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001412 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001413 }
1414}
1415
Ben Cheng38329f52009-07-07 14:19:20 -07001416/*
1417 * Generate code to setup the call stack then jump to the chaining cell if it
1418 * is not a native method.
1419 */
1420static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001421 BasicBlock *bb, ArmLIR *labelList,
1422 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001423 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001424{
Bill Buzbee1465db52009-09-23 17:17:35 -07001425 /*
1426 * Note: all Dalvik register state should be flushed to
1427 * memory by the point, so register usage restrictions no
1428 * longer apply. All temp & preserved registers may be used.
1429 */
1430 lockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001431 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001432
1433 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001434 lockTemp(cUnit, r1);
1435 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001436 /* r4PC = dalvikCallsite */
1437 loadConstant(cUnit, r4PC,
1438 (int) (cUnit->method->insns + mir->offset));
1439 addrRetChain->generic.target = (LIR *) retChainingCell;
1440 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001441 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001442 * r1 = &ChainingCell
1443 * r4PC = callsiteDPC
1444 */
1445 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001446 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001447#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07001448 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001449#endif
1450 } else {
1451 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
1452#if defined(INVOKE_STATS)
1453 gDvmJit.invokeChain++;
1454#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001455 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001456 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1457 }
1458 /* Handle exceptions using the interpreter */
1459 genTrap(cUnit, mir->offset, pcrLabel);
1460}
1461
Ben Cheng38329f52009-07-07 14:19:20 -07001462/*
1463 * Generate code to check the validity of a predicted chain and take actions
1464 * based on the result.
1465 *
1466 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1467 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1468 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1469 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1470 * 0x426a99b2 : blx_2 see above --+
1471 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1472 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1473 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1474 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1475 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1476 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1477 * 0x426a99c0 : blx r7 --+
1478 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1479 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1480 * 0x426a99c6 : blx_2 see above --+
1481 */
1482static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1483 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001484 ArmLIR *retChainingCell,
1485 ArmLIR *predChainingCell,
1486 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001487{
Bill Buzbee1465db52009-09-23 17:17:35 -07001488 /*
1489 * Note: all Dalvik register state should be flushed to
1490 * memory by the point, so register usage restrictions no
1491 * longer apply. Lock temps to prevent them from being
1492 * allocated by utility routines.
1493 */
1494 lockAllTemps(cUnit);
1495
Ben Cheng38329f52009-07-07 14:19:20 -07001496 /* "this" is already left in r0 by genProcessArgs* */
1497
1498 /* r4PC = dalvikCallsite */
1499 loadConstant(cUnit, r4PC,
1500 (int) (cUnit->method->insns + mir->offset));
1501
1502 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001503 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001504 addrRetChain->generic.target = (LIR *) retChainingCell;
1505
1506 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001507 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001508 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1509
1510 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1511
1512 /* return through lr - jump to the chaining cell */
1513 genUnconditionalBranch(cUnit, predChainingCell);
1514
1515 /*
1516 * null-check on "this" may have been eliminated, but we still need a PC-
1517 * reconstruction label for stack overflow bailout.
1518 */
1519 if (pcrLabel == NULL) {
1520 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001521 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001522 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07001523 pcrLabel->operands[0] = dPC;
1524 pcrLabel->operands[1] = mir->offset;
1525 /* Insert the place holder to the growable list */
1526 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1527 }
1528
1529 /* return through lr+2 - punt to the interpreter */
1530 genUnconditionalBranch(cUnit, pcrLabel);
1531
1532 /*
1533 * return through lr+4 - fully resolve the callee method.
1534 * r1 <- count
1535 * r2 <- &predictedChainCell
1536 * r3 <- this->class
1537 * r4 <- dPC
1538 * r7 <- this->class->vtable
1539 */
1540
1541 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001542 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001543
1544 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001545 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001546
Bill Buzbee1465db52009-09-23 17:17:35 -07001547 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001548
Bill Buzbee270c1d62009-08-13 16:58:07 -07001549 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1550 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001551
1552 /*
1553 * r0 = calleeMethod
1554 * r2 = &predictedChainingCell
1555 * r3 = class
1556 *
1557 * &returnChainingCell has been loaded into r1 but is not needed
1558 * when patching the chaining cell and will be clobbered upon
1559 * returning so it will be reconstructed again.
1560 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001561 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001562
1563 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001564 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001565 addrRetChain->generic.target = (LIR *) retChainingCell;
1566
1567 bypassRechaining->generic.target = (LIR *) addrRetChain;
1568 /*
1569 * r0 = calleeMethod,
1570 * r1 = &ChainingCell,
1571 * r4PC = callsiteDPC,
1572 */
1573 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
1574#if defined(INVOKE_STATS)
1575 gDvmJit.invokePredictedChain++;
1576#endif
1577 /* Handle exceptions using the interpreter */
1578 genTrap(cUnit, mir->offset, pcrLabel);
1579}
1580
1581/*
1582 * Up calling this function, "this" is stored in r0. The actual class will be
1583 * chased down off r0 and the predicted one will be retrieved through
1584 * predictedChainingCell then a comparison is performed to see whether the
1585 * previously established chaining is still valid.
1586 *
1587 * The return LIR is a branch based on the comparison result. The actual branch
1588 * target will be setup in the caller.
1589 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001590static ArmLIR *genCheckPredictedChain(CompilationUnit *cUnit,
1591 ArmLIR *predChainingCell,
1592 ArmLIR *retChainingCell,
Ben Cheng38329f52009-07-07 14:19:20 -07001593 MIR *mir)
1594{
Bill Buzbee1465db52009-09-23 17:17:35 -07001595 /*
1596 * Note: all Dalvik register state should be flushed to
1597 * memory by the point, so register usage restrictions no
1598 * longer apply. All temp & preserved registers may be used.
1599 */
1600 lockAllTemps(cUnit);
1601
Ben Cheng38329f52009-07-07 14:19:20 -07001602 /* r3 now contains this->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001603 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001604
1605 /*
1606 * r2 now contains predicted class. The starting offset of the
1607 * cached value is 4 bytes into the chaining cell.
1608 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001609 ArmLIR *getPredictedClass =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001610 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, clazz), r2);
Ben Cheng38329f52009-07-07 14:19:20 -07001611 getPredictedClass->generic.target = (LIR *) predChainingCell;
1612
1613 /*
1614 * r0 now contains predicted method. The starting offset of the
1615 * cached value is 8 bytes into the chaining cell.
1616 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001617 ArmLIR *getPredictedMethod =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001618 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, method), r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001619 getPredictedMethod->generic.target = (LIR *) predChainingCell;
1620
1621 /* Load the stats counter to see if it is time to unchain and refresh */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001622 ArmLIR *getRechainingRequestCount =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001623 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, counter), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001624 getRechainingRequestCount->generic.target =
1625 (LIR *) predChainingCell;
1626
1627 /* r4PC = dalvikCallsite */
1628 loadConstant(cUnit, r4PC,
1629 (int) (cUnit->method->insns + mir->offset));
1630
1631 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001632 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001633 addrRetChain->generic.target = (LIR *) retChainingCell;
1634
1635 /* Check if r2 (predicted class) == r3 (actual class) */
Bill Buzbee1465db52009-09-23 17:17:35 -07001636 opRegReg(cUnit, kOpCmp, r2, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001637
Bill Buzbee1465db52009-09-23 17:17:35 -07001638 return opCondBranch(cUnit, kArmCondEq);
Ben Cheng38329f52009-07-07 14:19:20 -07001639}
1640
Ben Chengba4fc8b2009-06-01 13:00:29 -07001641/* Geneate a branch to go back to the interpreter */
1642static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1643{
1644 /* r0 = dalvik pc */
Bill Buzbee1465db52009-09-23 17:17:35 -07001645 flushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001646 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001647 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1648 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1649 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001650 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001651}
1652
1653/*
1654 * Attempt to single step one instruction using the interpreter and return
1655 * to the compiled code for the next Dalvik instruction
1656 */
1657static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1658{
1659 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1660 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1661 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001662
1663 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
1664 flushAllRegs(cUnit);
1665
Ben Chengba4fc8b2009-06-01 13:00:29 -07001666 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1667 genPuntToInterp(cUnit, mir->offset);
1668 return;
1669 }
1670 int entryAddr = offsetof(InterpState,
1671 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001672 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001673 /* r0 = dalvik pc */
1674 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1675 /* r1 = dalvik pc of following instruction */
1676 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001677 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001678}
1679
Ben Cheng5d90c202009-11-22 23:31:11 -08001680static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001681{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001682 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001683 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001684 genExportPC(cUnit, mir);
1685 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
1686 loadValueDirectFixed(cUnit, rlSrc, r1);
1687 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001688 if (isEnter) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001689 loadConstant(cUnit, r2, (int)dvmLockObject);
1690 } else {
1691 loadConstant(cUnit, r2, (int)dvmUnlockObject);
1692 }
1693 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
1694 /* Do the call */
1695 opReg(cUnit, kOpBlx, r2);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001696#if defined(WITH_DEADLOCK_PREDICTION)
1697 if (isEnter) {
1698 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
1699 loadWordDisp(cUnit, r0, offsetof(Thread, exception), r1);
1700 opRegImm(cUnit, kOpCmp, r1, 0);
1701 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
1702 loadConstant(cUnit, r0,
1703 (int) (cUnit->method->insns + mir->offset));
1704 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1705 /* noreturn */
1706 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1707 target->defMask = ENCODE_ALL;
1708 branchOver->generic.target = (LIR *) target;
1709 }
1710#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001711 clobberCallRegs(cUnit);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001712}
1713
Ben Chengba4fc8b2009-06-01 13:00:29 -07001714/*
1715 * The following are the first-level codegen routines that analyze the format
1716 * of each bytecode then either dispatch special purpose codegen routines
1717 * or produce corresponding Thumb instructions directly.
1718 */
1719
1720static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001721 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001722{
1723 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1724 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1725 return false;
1726}
1727
1728static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1729{
1730 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1731 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden96516932009-10-28 17:39:02 -07001732 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_EB))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001733 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1734 return true;
1735 }
1736 switch (dalvikOpCode) {
1737 case OP_RETURN_VOID:
1738 genReturnCommon(cUnit,mir);
1739 break;
1740 case OP_UNUSED_73:
1741 case OP_UNUSED_79:
1742 case OP_UNUSED_7A:
1743 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1744 return true;
1745 case OP_NOP:
1746 break;
1747 default:
1748 return true;
1749 }
1750 return false;
1751}
1752
1753static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1754{
Bill Buzbee1465db52009-09-23 17:17:35 -07001755 RegLocation rlDest;
1756 RegLocation rlResult;
1757 if (mir->ssaRep->numDefs == 2) {
1758 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1759 } else {
1760 rlDest = getDestLoc(cUnit, mir, 0);
1761 }
Ben Chenge9695e52009-06-16 16:11:47 -07001762
Ben Chengba4fc8b2009-06-01 13:00:29 -07001763 switch (mir->dalvikInsn.opCode) {
1764 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001765 case OP_CONST_4: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001766 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1767 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1768 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001769 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001770 }
1771 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001772 //TUNING: single routine to load constant pair for support doubles
1773 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1774 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1775 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1776 rlResult.lowReg, 31);
1777 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001778 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001779 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001780 default:
1781 return true;
1782 }
1783 return false;
1784}
1785
1786static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1787{
Bill Buzbee1465db52009-09-23 17:17:35 -07001788 RegLocation rlDest;
1789 RegLocation rlResult;
1790 if (mir->ssaRep->numDefs == 2) {
1791 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1792 } else {
1793 rlDest = getDestLoc(cUnit, mir, 0);
1794 }
1795 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001796
Ben Chengba4fc8b2009-06-01 13:00:29 -07001797 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001798 case OP_CONST_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001799 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB << 16);
1800 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001801 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001802 }
1803 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001804 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1805 0, mir->dalvikInsn.vB << 16);
1806 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001807 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001808 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001809 default:
1810 return true;
1811 }
1812 return false;
1813}
1814
1815static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1816{
1817 /* For OP_THROW_VERIFICATION_ERROR */
1818 genInterpSingleStep(cUnit, mir);
1819 return false;
1820}
1821
1822static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1823{
Bill Buzbee1465db52009-09-23 17:17:35 -07001824 RegLocation rlResult;
1825 RegLocation rlDest;
1826 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001827
Ben Chengba4fc8b2009-06-01 13:00:29 -07001828 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001829 case OP_CONST_STRING_JUMBO:
1830 case OP_CONST_STRING: {
1831 void *strPtr = (void*)
1832 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
1833 assert(strPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001834 rlDest = getDestLoc(cUnit, mir, 0);
1835 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1836 loadConstantValue(cUnit, rlResult.lowReg, (int) strPtr );
1837 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001838 break;
1839 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001840 case OP_CONST_CLASS: {
1841 void *classPtr = (void*)
1842 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1843 assert(classPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001844 rlDest = getDestLoc(cUnit, mir, 0);
1845 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1846 loadConstantValue(cUnit, rlResult.lowReg, (int) classPtr );
1847 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001848 break;
1849 }
1850 case OP_SGET_OBJECT:
1851 case OP_SGET_BOOLEAN:
1852 case OP_SGET_CHAR:
1853 case OP_SGET_BYTE:
1854 case OP_SGET_SHORT:
1855 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001856 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001857 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001858 void *fieldPtr = (void*)
1859 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
1860 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001861 rlDest = getDestLoc(cUnit, mir, 0);
1862 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1863 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001864#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001865 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001866#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001867 int regMap = rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001868 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationLoad);
1869
Jeff Hao97319a82009-08-12 16:57:15 -07001870#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001871 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001872 break;
1873 }
1874 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001875 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001876 void *fieldPtr = (void*)
1877 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Bill Buzbee1465db52009-09-23 17:17:35 -07001878 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001879 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001880 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1881 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1882 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001883#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001884 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001885#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001886 int regMap = rlResult.highReg << 16 |
1887 rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001888 selfVerificationMemOpWrapper(cUnit, regMap,
1889 &selfVerificationLoadDoubleword);
1890
Jeff Hao97319a82009-08-12 16:57:15 -07001891#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001892 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001893 break;
1894 }
1895 case OP_SPUT_OBJECT:
1896 case OP_SPUT_BOOLEAN:
1897 case OP_SPUT_CHAR:
1898 case OP_SPUT_BYTE:
1899 case OP_SPUT_SHORT:
1900 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001901 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001902 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001903 void *fieldPtr = (void*)
1904 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001905
Ben Chengba4fc8b2009-06-01 13:00:29 -07001906 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001907 rlSrc = getSrcLoc(cUnit, mir, 0);
1908 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1909 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001910#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001911 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001912#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001913 int regMap = rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001914 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationStore);
1915#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001916 break;
1917 }
1918 case OP_SPUT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001919 int tReg = allocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001920 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001921 void *fieldPtr = (void*)
1922 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001923
Ben Chengba4fc8b2009-06-01 13:00:29 -07001924 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001925 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
1926 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1927 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001928#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001929 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001930#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001931 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001932 selfVerificationMemOpWrapper(cUnit, regMap,
1933 &selfVerificationStoreDoubleword);
1934#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001935 break;
1936 }
1937 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001938 /*
1939 * Obey the calling convention and don't mess with the register
1940 * usage.
1941 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001942 ClassObject *classPtr = (void*)
1943 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1944 assert(classPtr != NULL);
1945 assert(classPtr->status & CLASS_INITIALIZED);
Ben Cheng79d173c2009-09-29 16:12:51 -07001946 /*
1947 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001948 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001949 */
1950 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001951 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001952 genExportPC(cUnit, mir);
1953 loadConstant(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001954 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001955 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001956 opReg(cUnit, kOpBlx, r2);
1957 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001958 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001959 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1960 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001961 /*
1962 * OOM exception needs to be thrown here and cannot re-execute
1963 */
1964 loadConstant(cUnit, r0,
1965 (int) (cUnit->method->insns + mir->offset));
1966 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1967 /* noreturn */
1968
Bill Buzbee1465db52009-09-23 17:17:35 -07001969 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001970 target->defMask = ENCODE_ALL;
1971 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07001972 rlDest = getDestLoc(cUnit, mir, 0);
1973 rlResult = getReturnLoc(cUnit);
1974 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001975 break;
1976 }
1977 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001978 /*
1979 * Obey the calling convention and don't mess with the register
1980 * usage.
1981 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001982 ClassObject *classPtr =
1983 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001984 /*
1985 * Note: It is possible that classPtr is NULL at this point,
1986 * even though this instruction has been successfully interpreted.
1987 * If the previous interpretation had a null source, the
1988 * interpreter would not have bothered to resolve the clazz.
1989 * Bail out to the interpreter in this case, and log it
1990 * so that we can tell if it happens frequently.
1991 */
1992 if (classPtr == NULL) {
1993 LOGD("null clazz in OP_CHECK_CAST, single-stepping");
1994 genInterpSingleStep(cUnit, mir);
1995 return false;
1996 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001997 flushAllRegs(cUnit); /* Send everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001998 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07001999 rlSrc = getSrcLoc(cUnit, mir, 0);
2000 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2001 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
2002 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
2003 /*
2004 * rlSrc.lowReg now contains object->clazz. Note that
2005 * it could have been allocated r0, but we're okay so long
2006 * as we don't do anything desctructive until r0 is loaded
2007 * with clazz.
2008 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002009 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002010 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
2011 loadConstant(cUnit, r2, (int)dvmInstanceofNonTrivial);
2012 opRegReg(cUnit, kOpCmp, r0, r1);
2013 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2014 opReg(cUnit, kOpBlx, r2);
2015 clobberCallRegs(cUnit);
2016 /*
2017 * If null, check cast failed - punt to the interpreter. Because
2018 * interpreter will be the one throwing, we don't need to
2019 * genExportPC() here.
2020 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002021 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002022 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002023 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002024 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002025 branch1->generic.target = (LIR *)target;
2026 branch2->generic.target = (LIR *)target;
2027 break;
2028 }
2029 default:
2030 return true;
2031 }
2032 return false;
2033}
2034
2035static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
2036{
2037 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002038 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002039 switch (dalvikOpCode) {
2040 case OP_MOVE_EXCEPTION: {
2041 int offset = offsetof(InterpState, self);
2042 int exOffset = offsetof(Thread, exception);
Bill Buzbee1465db52009-09-23 17:17:35 -07002043 int selfReg = allocTemp(cUnit);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002044 int resetReg = allocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002045 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2046 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2047 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002048 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002049 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002050 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07002051 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002052 break;
2053 }
2054 case OP_MOVE_RESULT:
2055 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002056 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2057 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
2058 rlSrc.fp = rlDest.fp;
2059 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002060 break;
2061 }
2062 case OP_MOVE_RESULT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002063 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
2064 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
2065 rlSrc.fp = rlDest.fp;
2066 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002067 break;
2068 }
2069 case OP_RETURN_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002070 RegLocation rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2071 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
2072 rlDest.fp = rlSrc.fp;
2073 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002074 genReturnCommon(cUnit,mir);
2075 break;
2076 }
2077 case OP_RETURN:
2078 case OP_RETURN_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002079 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2080 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
2081 rlDest.fp = rlSrc.fp;
2082 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002083 genReturnCommon(cUnit,mir);
2084 break;
2085 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002086 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002087 case OP_MONITOR_ENTER:
Bill Buzbee1465db52009-09-23 17:17:35 -07002088#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08002089 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002090#else
Ben Cheng5d90c202009-11-22 23:31:11 -08002091 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002092#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07002093 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002094 case OP_THROW: {
2095 genInterpSingleStep(cUnit, mir);
2096 break;
2097 }
2098 default:
2099 return true;
2100 }
2101 return false;
2102}
2103
Bill Buzbeed45ba372009-06-15 17:00:57 -07002104static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
2105{
2106 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002107 RegLocation rlDest;
2108 RegLocation rlSrc;
2109 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07002110
Ben Chengba4fc8b2009-06-01 13:00:29 -07002111 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002112 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002113 }
2114
Bill Buzbee1465db52009-09-23 17:17:35 -07002115 if (mir->ssaRep->numUses == 2)
2116 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2117 else
2118 rlSrc = getSrcLoc(cUnit, mir, 0);
2119 if (mir->ssaRep->numDefs == 2)
2120 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2121 else
2122 rlDest = getDestLoc(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07002123
Ben Chengba4fc8b2009-06-01 13:00:29 -07002124 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002125 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002126 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002127 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002128 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002129 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002130 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002131 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002132 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002133 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002134 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002135 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002136 case OP_NEG_INT:
2137 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002138 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002139 case OP_NEG_LONG:
2140 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08002141 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002142 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002143 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002144 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002145 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002146 case OP_MOVE_WIDE:
2147 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002148 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002149 case OP_INT_TO_LONG:
2150 rlSrc = updateLoc(cUnit, rlSrc);
2151 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2152 if (rlSrc.location == kLocPhysReg) {
2153 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2154 } else {
2155 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2156 }
2157 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2158 rlResult.lowReg, 31);
2159 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002160 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002161 case OP_LONG_TO_INT:
2162 rlSrc = updateLocWide(cUnit, rlSrc);
2163 rlSrc = wideToNarrowLoc(cUnit, rlSrc);
2164 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002165 case OP_MOVE:
2166 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002167 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002168 break;
2169 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002170 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2171 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2172 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2173 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002174 break;
2175 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002176 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2177 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2178 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2179 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002180 break;
2181 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002182 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2183 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2184 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2185 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002186 break;
2187 case OP_ARRAY_LENGTH: {
2188 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002189 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2190 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2191 mir->offset, NULL);
2192 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2193 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2194 rlResult.lowReg);
2195 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002196 break;
2197 }
2198 default:
2199 return true;
2200 }
2201 return false;
2202}
2203
2204static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2205{
2206 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002207 RegLocation rlDest;
2208 RegLocation rlResult;
2209 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002210 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002211 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2212 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2213 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
2214 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2215 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002216 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002217 rlDest = getDestLoc(cUnit, mir, 0);
2218 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
2219 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
2220 storeValue(cUnit, rlDest, rlResult);
2221 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002222 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002223 return false;
2224}
2225
2226/* Compare agaist zero */
2227static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002228 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002229{
2230 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002231 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002232 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2233 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2234 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002235
Bill Buzbee270c1d62009-08-13 16:58:07 -07002236//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07002237 switch (dalvikOpCode) {
2238 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002239 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002240 break;
2241 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002242 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002243 break;
2244 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002245 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002246 break;
2247 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002248 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002249 break;
2250 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002251 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002252 break;
2253 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002254 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002255 break;
2256 default:
2257 cond = 0;
2258 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
2259 dvmAbort();
2260 }
2261 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2262 /* This mostly likely will be optimized away in a later phase */
2263 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2264 return false;
2265}
2266
2267static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2268{
2269 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002270 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2271 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2272 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002273 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002274 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002275 int shiftOp = false;
2276 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002277
Ben Chengba4fc8b2009-06-01 13:00:29 -07002278 int __aeabi_idivmod(int op1, int op2);
2279 int __aeabi_idiv(int op1, int op2);
2280
2281 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002282 case OP_RSUB_INT_LIT8:
2283 case OP_RSUB_INT: {
2284 int tReg;
2285 //TUNING: add support for use of Arm rsub op
2286 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2287 tReg = allocTemp(cUnit);
2288 loadConstant(cUnit, tReg, lit);
2289 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2290 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2291 tReg, rlSrc.lowReg);
2292 storeValue(cUnit, rlDest, rlResult);
2293 return false;
2294 break;
2295 }
2296
Ben Chengba4fc8b2009-06-01 13:00:29 -07002297 case OP_ADD_INT_LIT8:
2298 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002299 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002300 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002301 case OP_MUL_INT_LIT8:
2302 case OP_MUL_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002303 op = kOpMul;
2304 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002305 case OP_AND_INT_LIT8:
2306 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002307 op = kOpAnd;
2308 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002309 case OP_OR_INT_LIT8:
2310 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002311 op = kOpOr;
2312 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002313 case OP_XOR_INT_LIT8:
2314 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002315 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002316 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002317 case OP_SHL_INT_LIT8:
Bill Buzbee1465db52009-09-23 17:17:35 -07002318 shiftOp = true;
2319 op = kOpLsl;
2320 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002321 case OP_SHR_INT_LIT8:
Bill Buzbee1465db52009-09-23 17:17:35 -07002322 shiftOp = true;
2323 op = kOpAsr;
2324 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002325 case OP_USHR_INT_LIT8:
Bill Buzbee1465db52009-09-23 17:17:35 -07002326 shiftOp = true;
2327 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002328 break;
2329
2330 case OP_DIV_INT_LIT8:
2331 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002332 case OP_REM_INT_LIT8:
2333 case OP_REM_INT_LIT16:
2334 if (lit == 0) {
2335 /* Let the interpreter deal with div by 0 */
2336 genInterpSingleStep(cUnit, mir);
2337 return false;
2338 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002339 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002340 loadValueDirectFixed(cUnit, rlSrc, r0);
2341 clobberReg(cUnit, r0);
2342 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2343 (dalvikOpCode == OP_DIV_INT_LIT16)) {
2344 loadConstant(cUnit, r2, (int)__aeabi_idiv);
2345 isDiv = true;
2346 } else {
2347 loadConstant(cUnit, r2, (int)__aeabi_idivmod);
2348 isDiv = false;
2349 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002350 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002351 opReg(cUnit, kOpBlx, r2);
2352 clobberCallRegs(cUnit);
2353 if (isDiv)
2354 rlResult = getReturnLoc(cUnit);
2355 else
2356 rlResult = getReturnLocAlt(cUnit);
2357 storeValue(cUnit, rlDest, rlResult);
2358 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002359 break;
2360 default:
2361 return true;
2362 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002363 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2364 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2365 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2366 if (shiftOp && (lit == 0)) {
2367 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2368 } else {
2369 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2370 }
2371 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002372 return false;
2373}
2374
2375static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2376{
2377 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2378 int fieldOffset;
2379
2380 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2381 InstField *pInstField = (InstField *)
2382 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002383
2384 assert(pInstField != NULL);
2385 fieldOffset = pInstField->byteOffset;
2386 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002387 /* Deliberately break the code while make the compiler happy */
2388 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002389 }
2390 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002391 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002392 // Generates a call - use explicit registers
2393 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2394 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2395 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002396 void *classPtr = (void*)
2397 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2398 assert(classPtr != NULL);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002399 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002400 genExportPC(cUnit, mir);
2401 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002402 loadConstant(cUnit, r0, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002403 loadConstant(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002404 /*
2405 * "len < 0": bail to the interpreter to re-execute the
2406 * instruction
2407 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002408 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002409 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002410 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002411 opReg(cUnit, kOpBlx, r3);
2412 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002413 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002414 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2415 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002416 /*
2417 * OOM exception needs to be thrown here and cannot re-execute
2418 */
2419 loadConstant(cUnit, r0,
2420 (int) (cUnit->method->insns + mir->offset));
2421 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2422 /* noreturn */
2423
Bill Buzbee1465db52009-09-23 17:17:35 -07002424 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002425 target->defMask = ENCODE_ALL;
2426 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07002427 rlResult = getReturnLoc(cUnit);
2428 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002429 break;
2430 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002431 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002432 // May generate a call - use explicit registers
2433 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2434 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2435 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002436 ClassObject *classPtr =
2437 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2438 assert(classPtr != NULL);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002439 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002440 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002441 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002442//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002443 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002444 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002445 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002446 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002447 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002448 /* r1 now contains object->clazz */
2449 loadConstant(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002450 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002451 opRegReg(cUnit, kOpCmp, r1, r2);
2452 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2453 genRegCopy(cUnit, r0, r1);
2454 genRegCopy(cUnit, r1, r2);
2455 opReg(cUnit, kOpBlx, r3);
2456 clobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002457 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002458 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002459 target->defMask = ENCODE_ALL;
Bill Buzbee1465db52009-09-23 17:17:35 -07002460 rlResult = getReturnLoc(cUnit);
2461 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002462 branch1->generic.target = (LIR *)target;
2463 branch2->generic.target = (LIR *)target;
2464 break;
2465 }
2466 case OP_IGET_WIDE:
2467 genIGetWide(cUnit, mir, fieldOffset);
2468 break;
2469 case OP_IGET:
2470 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002471 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002472 break;
2473 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002474 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002475 break;
2476 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002477 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002478 break;
2479 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002480 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002481 break;
2482 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002483 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002484 break;
2485 case OP_IPUT_WIDE:
2486 genIPutWide(cUnit, mir, fieldOffset);
2487 break;
2488 case OP_IPUT:
2489 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002490 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002491 break;
2492 case OP_IPUT_SHORT:
2493 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002494 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002495 break;
2496 case OP_IPUT_BYTE:
2497 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002498 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002499 break;
2500 default:
2501 return true;
2502 }
2503 return false;
2504}
2505
2506static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2507{
2508 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2509 int fieldOffset = mir->dalvikInsn.vC;
2510 switch (dalvikOpCode) {
2511 case OP_IGET_QUICK:
2512 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002513 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002514 break;
2515 case OP_IPUT_QUICK:
2516 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002517 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002518 break;
2519 case OP_IGET_WIDE_QUICK:
2520 genIGetWide(cUnit, mir, fieldOffset);
2521 break;
2522 case OP_IPUT_WIDE_QUICK:
2523 genIPutWide(cUnit, mir, fieldOffset);
2524 break;
2525 default:
2526 return true;
2527 }
2528 return false;
2529
2530}
2531
2532/* Compare agaist zero */
2533static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002534 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002535{
2536 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002537 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002538 RegLocation rlSrc1 = getSrcLoc(cUnit, mir, 0);
2539 RegLocation rlSrc2 = getSrcLoc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002540
Bill Buzbee1465db52009-09-23 17:17:35 -07002541 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2542 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2543 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002544
2545 switch (dalvikOpCode) {
2546 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002547 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002548 break;
2549 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002550 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002551 break;
2552 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002553 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002554 break;
2555 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002556 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002557 break;
2558 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002559 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002560 break;
2561 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002562 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002563 break;
2564 default:
2565 cond = 0;
2566 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
2567 dvmAbort();
2568 }
2569 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2570 /* This mostly likely will be optimized away in a later phase */
2571 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2572 return false;
2573}
2574
2575static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2576{
2577 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002578
2579 switch (opCode) {
2580 case OP_MOVE_16:
2581 case OP_MOVE_OBJECT_16:
2582 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002583 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002584 storeValue(cUnit, getDestLoc(cUnit, mir, 0),
2585 getSrcLoc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002586 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002587 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002588 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002589 case OP_MOVE_WIDE_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002590 storeValueWide(cUnit, getDestLocWide(cUnit, mir, 0, 1),
2591 getSrcLocWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002592 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002593 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002594 default:
2595 return true;
2596 }
2597 return false;
2598}
2599
2600static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2601{
2602 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002603 RegLocation rlSrc1;
2604 RegLocation rlSrc2;
2605 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002606
2607 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002608 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002609 }
2610
Bill Buzbee1465db52009-09-23 17:17:35 -07002611 /* APUTs have 3 sources and no targets */
2612 if (mir->ssaRep->numDefs == 0) {
2613 if (mir->ssaRep->numUses == 3) {
2614 rlDest = getSrcLoc(cUnit, mir, 0);
2615 rlSrc1 = getSrcLoc(cUnit, mir, 1);
2616 rlSrc2 = getSrcLoc(cUnit, mir, 2);
2617 } else {
2618 assert(mir->ssaRep->numUses == 4);
2619 rlDest = getSrcLocWide(cUnit, mir, 0, 1);
2620 rlSrc1 = getSrcLoc(cUnit, mir, 2);
2621 rlSrc2 = getSrcLoc(cUnit, mir, 3);
2622 }
2623 } else {
2624 /* Two sources and 1 dest. Deduce the operand sizes */
2625 if (mir->ssaRep->numUses == 4) {
2626 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
2627 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
2628 } else {
2629 assert(mir->ssaRep->numUses == 2);
2630 rlSrc1 = getSrcLoc(cUnit, mir, 0);
2631 rlSrc2 = getSrcLoc(cUnit, mir, 1);
2632 }
2633 if (mir->ssaRep->numDefs == 2) {
2634 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2635 } else {
2636 assert(mir->ssaRep->numDefs == 1);
2637 rlDest = getDestLoc(cUnit, mir, 0);
2638 }
2639 }
2640
2641
Ben Chengba4fc8b2009-06-01 13:00:29 -07002642 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002643 case OP_CMPL_FLOAT:
2644 case OP_CMPG_FLOAT:
2645 case OP_CMPL_DOUBLE:
2646 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002647 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002648 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002649 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002650 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002651 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002652 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002653 break;
2654 case OP_AGET:
2655 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002656 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002657 break;
2658 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002659 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002660 break;
2661 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002662 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002663 break;
2664 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002665 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002666 break;
2667 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002668 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002669 break;
2670 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002671 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002672 break;
2673 case OP_APUT:
2674 case OP_APUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002675 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002676 break;
2677 case OP_APUT_SHORT:
2678 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002679 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002680 break;
2681 case OP_APUT_BYTE:
2682 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002683 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002684 break;
2685 default:
2686 return true;
2687 }
2688 return false;
2689}
2690
Ben Cheng6c10a972009-10-29 14:39:18 -07002691/*
2692 * Find the matching case.
2693 *
2694 * return values:
2695 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2696 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2697 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2698 * above MAX_CHAINED_SWITCH_CASES).
2699 *
2700 * Instructions around the call are:
2701 *
2702 * mov r2, pc
2703 * blx &findPackedSwitchIndex
2704 * mov pc, r0
2705 * .align4
2706 * chaining cell for case 0 [8 bytes]
2707 * chaining cell for case 1 [8 bytes]
2708 * :
2709 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2710 * chaining cell for case default [8 bytes]
2711 * noChain exit
2712 */
2713s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
2714{
2715 int size;
2716 int firstKey;
2717 const int *entries;
2718 int index;
2719 int jumpIndex;
2720 int caseDPCOffset = 0;
2721 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2722 int chainingPC = (pc + 4) & ~3;
2723
2724 /*
2725 * Packed switch data format:
2726 * ushort ident = 0x0100 magic value
2727 * ushort size number of entries in the table
2728 * int first_key first (and lowest) switch case value
2729 * int targets[size] branch targets, relative to switch opcode
2730 *
2731 * Total size is (4+size*2) 16-bit code units.
2732 */
2733 size = switchData[1];
2734 assert(size > 0);
2735
2736 firstKey = switchData[2];
2737 firstKey |= switchData[3] << 16;
2738
2739
2740 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2741 * we can treat them as a native int array.
2742 */
2743 entries = (const int*) &switchData[4];
2744 assert(((u4)entries & 0x3) == 0);
2745
2746 index = testVal - firstKey;
2747
2748 /* Jump to the default cell */
2749 if (index < 0 || index >= size) {
2750 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2751 /* Jump to the non-chaining exit point */
2752 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2753 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2754 caseDPCOffset = entries[index];
2755 /* Jump to the inline chaining cell */
2756 } else {
2757 jumpIndex = index;
2758 }
2759
2760 chainingPC += jumpIndex * 8;
2761 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2762}
2763
2764/* See comments for findPackedSwitchIndex */
2765s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
2766{
2767 int size;
2768 const int *keys;
2769 const int *entries;
2770 int chainingPC = (pc + 4) & ~3;
2771 int i;
2772
2773 /*
2774 * Sparse switch data format:
2775 * ushort ident = 0x0200 magic value
2776 * ushort size number of entries in the table; > 0
2777 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2778 * int targets[size] branch targets, relative to switch opcode
2779 *
2780 * Total size is (2+size*4) 16-bit code units.
2781 */
2782
2783 size = switchData[1];
2784 assert(size > 0);
2785
2786 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2787 * we can treat them as a native int array.
2788 */
2789 keys = (const int*) &switchData[2];
2790 assert(((u4)keys & 0x3) == 0);
2791
2792 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2793 * we can treat them as a native int array.
2794 */
2795 entries = keys + size;
2796 assert(((u4)entries & 0x3) == 0);
2797
2798 /*
2799 * Run through the list of keys, which are guaranteed to
2800 * be sorted low-to-high.
2801 *
2802 * Most tables have 3-4 entries. Few have more than 10. A binary
2803 * search here is probably not useful.
2804 */
2805 for (i = 0; i < size; i++) {
2806 int k = keys[i];
2807 if (k == testVal) {
2808 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2809 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2810 i : MAX_CHAINED_SWITCH_CASES + 1;
2811 chainingPC += jumpIndex * 8;
2812 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2813 } else if (k > testVal) {
2814 break;
2815 }
2816 }
2817 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2818}
2819
Ben Chengba4fc8b2009-06-01 13:00:29 -07002820static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2821{
2822 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2823 switch (dalvikOpCode) {
2824 case OP_FILL_ARRAY_DATA: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002825 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2826 // Making a call - use explicit registers
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002827 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002828 genExportPC(cUnit, mir);
2829 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002830 loadConstant(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002831 loadConstant(cUnit, r1,
2832 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002833 opReg(cUnit, kOpBlx, r2);
2834 clobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002835 /* generate a branch over if successful */
2836 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2837 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2838 loadConstant(cUnit, r0,
2839 (int) (cUnit->method->insns + mir->offset));
2840 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2841 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2842 target->defMask = ENCODE_ALL;
2843 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002844 break;
2845 }
2846 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002847 * Compute the goto target of up to
2848 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2849 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002850 */
2851 case OP_PACKED_SWITCH:
2852 case OP_SPARSE_SWITCH: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002853 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002854 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002855 loadValueDirectFixed(cUnit, rlSrc, r1);
2856 lockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002857 const u2 *switchData =
2858 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2859 u2 size = switchData[1];
2860
Ben Chengba4fc8b2009-06-01 13:00:29 -07002861 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Cheng6c10a972009-10-29 14:39:18 -07002862 loadConstant(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002863 } else {
Ben Cheng6c10a972009-10-29 14:39:18 -07002864 loadConstant(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002865 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002866 /* r0 <- Addr of the switch data */
2867 loadConstant(cUnit, r0,
2868 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2869 /* r2 <- pc of the instruction following the blx */
2870 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002871 opReg(cUnit, kOpBlx, r4PC);
2872 clobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002873 /* pc <- computed goto target */
2874 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002875 break;
2876 }
2877 default:
2878 return true;
2879 }
2880 return false;
2881}
2882
2883static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002884 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002885{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002886 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002887 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002888
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002889 if (bb->fallThrough != NULL)
2890 retChainingCell = &labelList[bb->fallThrough->id];
2891
Ben Chengba4fc8b2009-06-01 13:00:29 -07002892 DecodedInstruction *dInsn = &mir->dalvikInsn;
2893 switch (mir->dalvikInsn.opCode) {
2894 /*
2895 * calleeMethod = this->clazz->vtable[
2896 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2897 * ]
2898 */
2899 case OP_INVOKE_VIRTUAL:
2900 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002901 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002902 int methodIndex =
2903 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2904 methodIndex;
2905
2906 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2907 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2908 else
2909 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2910
Ben Cheng38329f52009-07-07 14:19:20 -07002911 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2912 retChainingCell,
2913 predChainingCell,
2914 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002915 break;
2916 }
2917 /*
2918 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2919 * ->pResMethods[BBBB]->methodIndex]
2920 */
2921 /* TODO - not excersized in RunPerf.jar */
2922 case OP_INVOKE_SUPER:
2923 case OP_INVOKE_SUPER_RANGE: {
2924 int mIndex = cUnit->method->clazz->pDvmDex->
2925 pResMethods[dInsn->vB]->methodIndex;
2926 const Method *calleeMethod =
2927 cUnit->method->clazz->super->vtable[mIndex];
2928
2929 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2930 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2931 else
2932 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2933
2934 /* r0 = calleeMethod */
2935 loadConstant(cUnit, r0, (int) calleeMethod);
2936
Ben Cheng38329f52009-07-07 14:19:20 -07002937 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2938 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002939 break;
2940 }
2941 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2942 case OP_INVOKE_DIRECT:
2943 case OP_INVOKE_DIRECT_RANGE: {
2944 const Method *calleeMethod =
2945 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2946
2947 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2948 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2949 else
2950 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2951
2952 /* r0 = calleeMethod */
2953 loadConstant(cUnit, r0, (int) calleeMethod);
2954
Ben Cheng38329f52009-07-07 14:19:20 -07002955 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2956 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002957 break;
2958 }
2959 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2960 case OP_INVOKE_STATIC:
2961 case OP_INVOKE_STATIC_RANGE: {
2962 const Method *calleeMethod =
2963 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2964
2965 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2966 genProcessArgsNoRange(cUnit, mir, dInsn,
2967 NULL /* no null check */);
2968 else
2969 genProcessArgsRange(cUnit, mir, dInsn,
2970 NULL /* no null check */);
2971
2972 /* r0 = calleeMethod */
2973 loadConstant(cUnit, r0, (int) calleeMethod);
2974
Ben Cheng38329f52009-07-07 14:19:20 -07002975 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2976 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002977 break;
2978 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002979 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002980 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2981 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002982 *
2983 * Given "invoke-interface {v0}", the following is the generated code:
2984 *
2985 * 0x426a9abe : ldr r0, [r5, #0] --+
2986 * 0x426a9ac0 : mov r7, r5 |
2987 * 0x426a9ac2 : sub r7, #24 |
2988 * 0x426a9ac4 : cmp r0, #0 | genProcessArgsNoRange
2989 * 0x426a9ac6 : beq 0x426a9afe |
2990 * 0x426a9ac8 : stmia r7, <r0> --+
2991 * 0x426a9aca : ldr r4, [pc, #104] --> r4 <- dalvikPC of this invoke
2992 * 0x426a9acc : add r1, pc, #52 --> r1 <- &retChainingCell
2993 * 0x426a9ace : add r2, pc, #60 --> r2 <- &predictedChainingCell
2994 * 0x426a9ad0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_
2995 * 0x426a9ad2 : blx_2 see above --+ PREDICTED_CHAIN
2996 * 0x426a9ad4 : b 0x426a9b0c --> off to the predicted chain
2997 * 0x426a9ad6 : b 0x426a9afe --> punt to the interpreter
Ben Chenga8e64a72009-10-20 13:01:36 -07002998 * 0x426a9ad8 : mov r8, r1 --+
2999 * 0x426a9ada : mov r9, r2 |
3000 * 0x426a9adc : mov r10, r3 |
Ben Cheng38329f52009-07-07 14:19:20 -07003001 * 0x426a9ade : mov r0, r3 |
3002 * 0x426a9ae0 : mov r1, #74 | dvmFindInterfaceMethodInCache
3003 * 0x426a9ae2 : ldr r2, [pc, #76] |
3004 * 0x426a9ae4 : ldr r3, [pc, #68] |
3005 * 0x426a9ae6 : ldr r7, [pc, #64] |
3006 * 0x426a9ae8 : blx r7 --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003007 * 0x426a9aea : mov r1, r8 --> r1 <- rechain count
Ben Cheng38329f52009-07-07 14:19:20 -07003008 * 0x426a9aec : cmp r1, #0 --> compare against 0
3009 * 0x426a9aee : bgt 0x426a9af8 --> >=0? don't rechain
3010 * 0x426a9af0 : ldr r7, [r6, #96] --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003011 * 0x426a9af2 : mov r2, r9 | dvmJitToPatchPredictedChain
3012 * 0x426a9af4 : mov r3, r10 |
Ben Cheng38329f52009-07-07 14:19:20 -07003013 * 0x426a9af6 : blx r7 --+
3014 * 0x426a9af8 : add r1, pc, #8 --> r1 <- &retChainingCell
3015 * 0x426a9afa : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3016 * 0x426a9afc : blx_2 see above --+
3017 * -------- reconstruct dalvik PC : 0x428b786c @ +0x001e
3018 * 0x426a9afe (0042): ldr r0, [pc, #52]
3019 * Exception_Handling:
3020 * 0x426a9b00 (0044): ldr r1, [r6, #84]
3021 * 0x426a9b02 (0046): blx r1
3022 * 0x426a9b04 (0048): .align4
3023 * -------- chaining cell (hot): 0x0021
3024 * 0x426a9b04 (0048): ldr r0, [r6, #92]
3025 * 0x426a9b06 (004a): blx r0
3026 * 0x426a9b08 (004c): data 0x7872(30834)
3027 * 0x426a9b0a (004e): data 0x428b(17035)
3028 * 0x426a9b0c (0050): .align4
3029 * -------- chaining cell (predicted)
3030 * 0x426a9b0c (0050): data 0x0000(0) --> will be patched into bx
3031 * 0x426a9b0e (0052): data 0x0000(0)
3032 * 0x426a9b10 (0054): data 0x0000(0) --> class
3033 * 0x426a9b12 (0056): data 0x0000(0)
3034 * 0x426a9b14 (0058): data 0x0000(0) --> method
3035 * 0x426a9b16 (005a): data 0x0000(0)
3036 * 0x426a9b18 (005c): data 0x0000(0) --> reset count
3037 * 0x426a9b1a (005e): data 0x0000(0)
3038 * 0x426a9b28 (006c): .word (0xad0392a5)
3039 * 0x426a9b2c (0070): .word (0x6e750)
3040 * 0x426a9b30 (0074): .word (0x4109a618)
3041 * 0x426a9b34 (0078): .word (0x428b786c)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003042 */
3043 case OP_INVOKE_INTERFACE:
3044 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003045 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003046 int methodIndex = dInsn->vB;
3047
Bill Buzbee1465db52009-09-23 17:17:35 -07003048 /* Ensure that nothing is both live and dirty */
3049 flushAllRegs(cUnit);
3050
Ben Chengba4fc8b2009-06-01 13:00:29 -07003051 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
3052 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3053 else
3054 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3055
Ben Cheng38329f52009-07-07 14:19:20 -07003056 /* "this" is already left in r0 by genProcessArgs* */
3057
3058 /* r4PC = dalvikCallsite */
3059 loadConstant(cUnit, r4PC,
3060 (int) (cUnit->method->insns + mir->offset));
3061
3062 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003063 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003064 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003065 addrRetChain->generic.target = (LIR *) retChainingCell;
3066
3067 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003068 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003069 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003070 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3071
3072 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
3073
3074 /* return through lr - jump to the chaining cell */
3075 genUnconditionalBranch(cUnit, predChainingCell);
3076
3077 /*
3078 * null-check on "this" may have been eliminated, but we still need
3079 * a PC-reconstruction label for stack overflow bailout.
3080 */
3081 if (pcrLabel == NULL) {
3082 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003083 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003084 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07003085 pcrLabel->operands[0] = dPC;
3086 pcrLabel->operands[1] = mir->offset;
3087 /* Insert the place holder to the growable list */
3088 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3089 }
3090
3091 /* return through lr+2 - punt to the interpreter */
3092 genUnconditionalBranch(cUnit, pcrLabel);
3093
3094 /*
3095 * return through lr+4 - fully resolve the callee method.
3096 * r1 <- count
3097 * r2 <- &predictedChainCell
3098 * r3 <- this->class
3099 * r4 <- dPC
3100 * r7 <- this->class->vtable
3101 */
3102
3103 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003104 genRegCopy(cUnit, r8, r1);
3105 genRegCopy(cUnit, r9, r2);
3106 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003107
Ben Chengba4fc8b2009-06-01 13:00:29 -07003108 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003109 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003110
3111 /* r1 = BBBB */
3112 loadConstant(cUnit, r1, dInsn->vB);
3113
3114 /* r2 = method (caller) */
3115 loadConstant(cUnit, r2, (int) cUnit->method);
3116
3117 /* r3 = pDvmDex */
3118 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3119
3120 loadConstant(cUnit, r7,
3121 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003122 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003123
3124 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3125
Bill Buzbee1465db52009-09-23 17:17:35 -07003126 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003127
Ben Cheng38329f52009-07-07 14:19:20 -07003128 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07003129 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003130
Bill Buzbee1465db52009-09-23 17:17:35 -07003131 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07003132
Bill Buzbee270c1d62009-08-13 16:58:07 -07003133 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3134 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003135
Bill Buzbee1465db52009-09-23 17:17:35 -07003136 genRegCopy(cUnit, r2, r9);
3137 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003138
3139 /*
3140 * r0 = calleeMethod
3141 * r2 = &predictedChainingCell
3142 * r3 = class
3143 *
3144 * &returnChainingCell has been loaded into r1 but is not needed
3145 * when patching the chaining cell and will be clobbered upon
3146 * returning so it will be reconstructed again.
3147 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003148 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003149
3150 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003151 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003152 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003153
3154 bypassRechaining->generic.target = (LIR *) addrRetChain;
3155
Ben Chengba4fc8b2009-06-01 13:00:29 -07003156 /*
3157 * r0 = this, r1 = calleeMethod,
3158 * r1 = &ChainingCell,
3159 * r4PC = callsiteDPC,
3160 */
3161 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
3162#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07003163 gDvmJit.invokePredictedChain++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003164#endif
3165 /* Handle exceptions using the interpreter */
3166 genTrap(cUnit, mir->offset, pcrLabel);
3167 break;
3168 }
3169 /* NOP */
3170 case OP_INVOKE_DIRECT_EMPTY: {
3171 return false;
3172 }
3173 case OP_FILLED_NEW_ARRAY:
3174 case OP_FILLED_NEW_ARRAY_RANGE: {
3175 /* Just let the interpreter deal with these */
3176 genInterpSingleStep(cUnit, mir);
3177 break;
3178 }
3179 default:
3180 return true;
3181 }
3182 return false;
3183}
3184
3185static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003186 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003187{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003188 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3189 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3190 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003191
3192 DecodedInstruction *dInsn = &mir->dalvikInsn;
3193 switch (mir->dalvikInsn.opCode) {
3194 /* calleeMethod = this->clazz->vtable[BBBB] */
3195 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3196 case OP_INVOKE_VIRTUAL_QUICK: {
3197 int methodIndex = dInsn->vB;
3198 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3199 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3200 else
3201 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3202
Ben Cheng38329f52009-07-07 14:19:20 -07003203 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3204 retChainingCell,
3205 predChainingCell,
3206 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003207 break;
3208 }
3209 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3210 case OP_INVOKE_SUPER_QUICK:
3211 case OP_INVOKE_SUPER_QUICK_RANGE: {
3212 const Method *calleeMethod =
3213 cUnit->method->clazz->super->vtable[dInsn->vB];
3214
3215 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3216 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3217 else
3218 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3219
3220 /* r0 = calleeMethod */
3221 loadConstant(cUnit, r0, (int) calleeMethod);
3222
Ben Cheng38329f52009-07-07 14:19:20 -07003223 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3224 calleeMethod);
3225 /* Handle exceptions using the interpreter */
3226 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003227 break;
3228 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003229 default:
3230 return true;
3231 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003232 return false;
3233}
3234
3235/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003236 * This operation is complex enough that we'll do it partly inline
3237 * and partly with a handler. NOTE: the handler uses hardcoded
3238 * values for string object offsets and must be revisitied if the
3239 * layout changes.
3240 */
3241static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3242{
3243#if defined(USE_GLOBAL_STRING_DEFS)
3244 return false;
3245#else
3246 ArmLIR *rollback;
3247 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3248 RegLocation rlComp = getSrcLoc(cUnit, mir, 1);
3249
3250 loadValueDirectFixed(cUnit, rlThis, r0);
3251 loadValueDirectFixed(cUnit, rlComp, r1);
3252 /* Test objects for NULL */
3253 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3254 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3255 /*
3256 * TUNING: we could check for object pointer equality before invoking
3257 * handler. Unclear whether the gain would be worth the added code size
3258 * expansion.
3259 */
3260 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
3261 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3262 return true;
3263#endif
3264}
3265
3266static bool genInlinedIndexOf(CompilationUnit *cUnit, MIR *mir, bool singleI)
3267{
3268#if defined(USE_GLOBAL_STRING_DEFS)
3269 return false;
3270#else
3271 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3272 RegLocation rlChar = getSrcLoc(cUnit, mir, 1);
3273
3274 loadValueDirectFixed(cUnit, rlThis, r0);
3275 loadValueDirectFixed(cUnit, rlChar, r1);
3276 if (!singleI) {
3277 RegLocation rlStart = getSrcLoc(cUnit, mir, 2);
3278 loadValueDirectFixed(cUnit, rlStart, r2);
3279 } else {
3280 loadConstant(cUnit, r2, 0);
3281 }
3282 /* Test objects for NULL */
3283 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3284 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
3285 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3286 return true;
3287#endif
3288}
3289
3290
3291/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003292 * NOTE: Handles both range and non-range versions (arguments
3293 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003294 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003295static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003296{
3297 DecodedInstruction *dInsn = &mir->dalvikInsn;
3298 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003299 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003300 case OP_EXECUTE_INLINE: {
3301 unsigned int i;
3302 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003303 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003304 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003305 int tReg1;
3306 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003307 switch (operation) {
3308 case INLINE_EMPTYINLINEMETHOD:
3309 return false; /* Nop */
3310 case INLINE_STRING_LENGTH:
3311 return genInlinedStringLength(cUnit, mir);
3312 case INLINE_MATH_ABS_INT:
3313 return genInlinedAbsInt(cUnit, mir);
3314 case INLINE_MATH_ABS_LONG:
3315 return genInlinedAbsLong(cUnit, mir);
3316 case INLINE_MATH_MIN_INT:
3317 return genInlinedMinMaxInt(cUnit, mir, true);
3318 case INLINE_MATH_MAX_INT:
3319 return genInlinedMinMaxInt(cUnit, mir, false);
3320 case INLINE_STRING_CHARAT:
3321 return genInlinedStringCharAt(cUnit, mir);
3322 case INLINE_MATH_SQRT:
3323 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003324 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003325 else
3326 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003327 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003328 if (genInlinedAbsFloat(cUnit, mir))
3329 return false;
3330 else
3331 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003332 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003333 if (genInlinedAbsDouble(cUnit, mir))
3334 return false;
3335 else
3336 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003337 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003338 if (genInlinedCompareTo(cUnit, mir))
3339 return false;
3340 else
3341 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003342 case INLINE_STRING_INDEXOF_I:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003343 if (genInlinedIndexOf(cUnit, mir, true /* I */))
3344 return false;
3345 else
3346 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003347 case INLINE_STRING_INDEXOF_II:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003348 if (genInlinedIndexOf(cUnit, mir, false /* I */))
3349 return false;
3350 else
3351 break;
3352 case INLINE_STRING_EQUALS:
3353 case INLINE_MATH_COS:
3354 case INLINE_MATH_SIN:
3355 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003356 default:
3357 dvmAbort();
Ben Chengba4fc8b2009-06-01 13:00:29 -07003358 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003359 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07003360 clobberCallRegs(cUnit);
3361 clobberReg(cUnit, r4PC);
3362 clobberReg(cUnit, r7);
3363 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3364 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003365 loadConstant(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003366 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003367 for (i=0; i < dInsn->vA; i++) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003368 loadValueDirect(cUnit, getSrcLoc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003369 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003370 opReg(cUnit, kOpBlx, r4PC);
3371 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003372 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3373 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3374 loadConstant(cUnit, r0,
3375 (int) (cUnit->method->insns + mir->offset));
3376 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3377 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3378 target->defMask = ENCODE_ALL;
3379 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003380 break;
3381 }
3382 default:
3383 return true;
3384 }
3385 return false;
3386}
3387
3388static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3389{
Bill Buzbee1465db52009-09-23 17:17:35 -07003390 //TUNING: We're using core regs here - not optimal when target is a double
3391 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
3392 RegLocation rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
3393 loadConstantValue(cUnit, rlResult.lowReg,
3394 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3395 loadConstantValue(cUnit, rlResult.highReg,
3396 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
3397 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003398 return false;
3399}
3400
Ben Chengba4fc8b2009-06-01 13:00:29 -07003401/*
3402 * The following are special processing routines that handle transfer of
3403 * controls between compiled code and the interpreter. Certain VM states like
3404 * Dalvik PC and special-purpose registers are reconstructed here.
3405 */
3406
Ben Cheng1efc9c52009-06-08 18:25:27 -07003407/* Chaining cell for code that may need warmup. */
3408static void handleNormalChainingCell(CompilationUnit *cUnit,
3409 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003410{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003411 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3412 jitToInterpEntries.dvmJitToInterpNormal), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003413 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003414 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3415}
3416
3417/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003418 * Chaining cell for instructions that immediately following already translated
3419 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003420 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003421static void handleHotChainingCell(CompilationUnit *cUnit,
3422 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003423{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003424 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3425 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003426 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003427 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3428}
3429
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003430#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003431/* Chaining cell for branches that branch back into the same basic block */
3432static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3433 unsigned int offset)
3434{
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003435#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003436 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Jeff Hao97319a82009-08-12 16:57:15 -07003437 offsetof(InterpState, jitToInterpEntries.dvmJitToBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003438#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003439 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003440 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3441#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003442 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003443 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3444}
3445
3446#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003447/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003448static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3449 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003450{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003451 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3452 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003453 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003454 addWordData(cUnit, (int) (callee->insns), true);
3455}
3456
Ben Cheng38329f52009-07-07 14:19:20 -07003457/* Chaining cell for monomorphic method invocations. */
3458static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3459{
3460
3461 /* Should not be executed in the initial state */
3462 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3463 /* To be filled: class */
3464 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3465 /* To be filled: method */
3466 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3467 /*
3468 * Rechain count. The initial value of 0 here will trigger chaining upon
3469 * the first invocation of this callsite.
3470 */
3471 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3472}
3473
Ben Chengba4fc8b2009-06-01 13:00:29 -07003474/* Load the Dalvik PC into r0 and jump to the specified target */
3475static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003476 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003477{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003478 ArmLIR **pcrLabel =
3479 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003480 int numElems = cUnit->pcReconstructionList.numUsed;
3481 int i;
3482 for (i = 0; i < numElems; i++) {
3483 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3484 /* r0 = dalvik PC */
3485 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3486 genUnconditionalBranch(cUnit, targetLabel);
3487 }
3488}
3489
Bill Buzbee1465db52009-09-23 17:17:35 -07003490static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3491 "kMirOpPhi",
3492 "kMirOpNullNRangeUpCheck",
3493 "kMirOpNullNRangeDownCheck",
3494 "kMirOpLowerBound",
3495 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003496};
3497
3498/*
3499 * vA = arrayReg;
3500 * vB = idxReg;
3501 * vC = endConditionReg;
3502 * arg[0] = maxC
3503 * arg[1] = minC
3504 * arg[2] = loopBranchConditionCode
3505 */
3506static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3507{
Bill Buzbee1465db52009-09-23 17:17:35 -07003508 /*
3509 * NOTE: these synthesized blocks don't have ssa names assigned
3510 * for Dalvik registers. However, because they dominate the following
3511 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3512 * ssa name.
3513 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003514 DecodedInstruction *dInsn = &mir->dalvikInsn;
3515 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003516 const int maxC = dInsn->arg[0];
3517 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003518 int regLength;
3519 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3520 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003521
3522 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003523 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3524 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3525 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003526 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3527
3528 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003529 regLength = allocTemp(cUnit);
3530 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003531
3532 int delta = maxC;
3533 /*
3534 * If the loop end condition is ">=" instead of ">", then the largest value
3535 * of the index is "endCondition - 1".
3536 */
3537 if (dInsn->arg[2] == OP_IF_GE) {
3538 delta--;
3539 }
3540
3541 if (delta) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003542 int tReg = allocTemp(cUnit);
3543 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3544 rlIdxEnd.lowReg = tReg;
3545 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003546 }
3547 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003548 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003549 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003550}
3551
3552/*
3553 * vA = arrayReg;
3554 * vB = idxReg;
3555 * vC = endConditionReg;
3556 * arg[0] = maxC
3557 * arg[1] = minC
3558 * arg[2] = loopBranchConditionCode
3559 */
3560static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3561{
3562 DecodedInstruction *dInsn = &mir->dalvikInsn;
3563 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07003564 const int regLength = allocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003565 const int maxC = dInsn->arg[0];
3566 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003567 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3568 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003569
3570 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003571 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3572 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3573 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003574 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3575
3576 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003577 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003578
3579 if (maxC) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003580 int tReg = allocTemp(cUnit);
3581 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3582 rlIdxInit.lowReg = tReg;
3583 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003584 }
3585
3586 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003587 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003588 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003589}
3590
3591/*
3592 * vA = idxReg;
3593 * vB = minC;
3594 */
3595static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3596{
3597 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003598 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003599 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003600
3601 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003602 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003603
3604 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003605 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003606 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3607}
3608
3609/* Extended MIR instructions like PHI */
3610static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3611{
Bill Buzbee1465db52009-09-23 17:17:35 -07003612 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003613 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3614 false);
3615 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003616 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003617
3618 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003619 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003620 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003621 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003622 break;
3623 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003624 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003625 genHoistedChecksForCountUpLoop(cUnit, mir);
3626 break;
3627 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003628 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003629 genHoistedChecksForCountDownLoop(cUnit, mir);
3630 break;
3631 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003632 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003633 genHoistedLowerBoundCheck(cUnit, mir);
3634 break;
3635 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003636 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003637 genUnconditionalBranch(cUnit,
3638 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3639 break;
3640 }
3641 default:
3642 break;
3643 }
3644}
3645
3646/*
3647 * Create a PC-reconstruction cell for the starting offset of this trace.
3648 * Since the PCR cell is placed near the end of the compiled code which is
3649 * usually out of range for a conditional branch, we put two branches (one
3650 * branch over to the loop body and one layover branch to the actual PCR) at the
3651 * end of the entry block.
3652 */
3653static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3654 ArmLIR *bodyLabel)
3655{
3656 /* Set up the place holder to reconstruct this Dalvik PC */
3657 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003658 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng4238ec22009-08-24 16:32:22 -07003659 pcrLabel->operands[0] =
3660 (int) (cUnit->method->insns + entry->startOffset);
3661 pcrLabel->operands[1] = entry->startOffset;
3662 /* Insert the place holder to the growable list */
3663 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3664
3665 /*
3666 * Next, create two branches - one branch over to the loop body and the
3667 * other branch to the PCR cell to punt.
3668 */
3669 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003670 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003671 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003672 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003673 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3674
3675 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003676 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003677 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003678 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003679 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3680}
3681
Ben Chengba4fc8b2009-06-01 13:00:29 -07003682void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3683{
3684 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003685 ArmLIR *labelList =
3686 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003687 GrowableList chainingListByType[kChainingCellLast];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003688 int i;
3689
3690 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003691 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003692 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003693 for (i = 0; i < kChainingCellLast; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003694 dvmInitGrowableList(&chainingListByType[i], 2);
3695 }
3696
3697 BasicBlock **blockList = cUnit->blockList;
3698
Bill Buzbee6e963e12009-06-17 16:56:19 -07003699 if (cUnit->executionCount) {
3700 /*
3701 * Reserve 6 bytes at the beginning of the trace
3702 * +----------------------------+
3703 * | execution count (4 bytes) |
3704 * +----------------------------+
3705 * | chain cell offset (2 bytes)|
3706 * +----------------------------+
3707 * ...and then code to increment the execution
3708 * count:
3709 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3710 * sub r0, #10 @ back up to addr of executionCount
3711 * ldr r1, [r0]
3712 * add r1, #1
3713 * str r1, [r0]
3714 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003715 newLIR1(cUnit, kArm16BitData, 0);
3716 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003717 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003718 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003719 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003720 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003721 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3722 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3723 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3724 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3725 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003726 } else {
3727 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003728 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003729 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003730 cUnit->headerSize = 2;
3731 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003732
Ben Chengba4fc8b2009-06-01 13:00:29 -07003733 /* Handle the content in each basic block */
3734 for (i = 0; i < cUnit->numBlocks; i++) {
3735 blockList[i]->visited = true;
3736 MIR *mir;
3737
3738 labelList[i].operands[0] = blockList[i]->startOffset;
3739
Bill Buzbee1465db52009-09-23 17:17:35 -07003740 if (blockList[i]->blockType >= kChainingCellLast) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003741 /*
3742 * Append the label pseudo LIR first. Chaining cells will be handled
3743 * separately afterwards.
3744 */
3745 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3746 }
3747
Bill Buzbee1465db52009-09-23 17:17:35 -07003748 if (blockList[i]->blockType == kEntryBlock) {
3749 labelList[i].opCode = ARM_PSEUDO_kEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003750 if (blockList[i]->firstMIRInsn == NULL) {
3751 continue;
3752 } else {
3753 setupLoopEntryBlock(cUnit, blockList[i],
3754 &labelList[blockList[i]->fallThrough->id]);
3755 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003756 } else if (blockList[i]->blockType == kExitBlock) {
3757 labelList[i].opCode = ARM_PSEUDO_kExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003758 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003759 } else if (blockList[i]->blockType == kDalvikByteCode) {
3760 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003761 /* Reset the register state */
Bill Buzbee1465db52009-09-23 17:17:35 -07003762 resetRegPool(cUnit);
3763 clobberAllRegs(cUnit);
3764 resetNullCheckTracker(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003765 } else {
3766 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003767 case kChainingCellNormal:
3768 labelList[i].opCode = ARM_PSEUDO_kChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003769 /* handle the codegen later */
3770 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003771 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003772 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003773 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003774 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003775 ARM_PSEUDO_kChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003776 labelList[i].operands[0] =
3777 (int) blockList[i]->containingMethod;
3778 /* handle the codegen later */
3779 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003780 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003781 (void *) i);
3782 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003783 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003784 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003785 ARM_PSEUDO_kChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003786 /* handle the codegen later */
3787 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003788 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003789 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003790 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003791 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003792 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003793 ARM_PSEUDO_kChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003794 /* handle the codegen later */
3795 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003796 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003797 (void *) i);
3798 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003799 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003800 /* Make sure exception handling block is next */
3801 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003802 ARM_PSEUDO_kPCReconstruction_BLOCK_LABEL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003803 assert (i == cUnit->numBlocks - 2);
3804 handlePCReconstruction(cUnit, &labelList[i+1]);
3805 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003806 case kExceptionHandling:
3807 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003808 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003809 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3810 jitToInterpEntries.dvmJitToInterpPunt),
3811 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003812 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003813 }
3814 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003815#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003816 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003817 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003818 ARM_PSEUDO_kChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003819 /* handle the codegen later */
3820 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003821 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003822 (void *) i);
3823 break;
3824#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003825 default:
3826 break;
3827 }
3828 continue;
3829 }
Ben Chenge9695e52009-06-16 16:11:47 -07003830
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003831 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003832
Ben Chengba4fc8b2009-06-01 13:00:29 -07003833 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003834
3835 resetRegPool(cUnit);
3836 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
3837 clobberAllRegs(cUnit);
3838 }
3839
3840 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
3841 resetDefTracking(cUnit);
3842 }
3843
3844 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003845 handleExtendedMIR(cUnit, mir);
3846 continue;
3847 }
3848
Bill Buzbee1465db52009-09-23 17:17:35 -07003849
Ben Chengba4fc8b2009-06-01 13:00:29 -07003850 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3851 InstructionFormat dalvikFormat =
3852 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003853 ArmLIR *boundaryLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003854 newLIR2(cUnit, ARM_PSEUDO_kDalvikByteCode_BOUNDARY,
Ben Chengccd6c012009-10-15 14:52:45 -07003855 mir->offset,
3856 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3857 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003858 if (mir->ssaRep) {
3859 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003860 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003861 }
3862
Ben Chenge9695e52009-06-16 16:11:47 -07003863 /* Remember the first LIR for this block */
3864 if (headLIR == NULL) {
3865 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003866 /* Set the first boundaryLIR as a scheduling barrier */
3867 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003868 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003869
Ben Chengba4fc8b2009-06-01 13:00:29 -07003870 bool notHandled;
3871 /*
3872 * Debugging: screen the opcode first to see if it is in the
3873 * do[-not]-compile list
3874 */
3875 bool singleStepMe =
3876 gDvmJit.includeSelectedOp !=
3877 ((gDvmJit.opList[dalvikOpCode >> 3] &
3878 (1 << (dalvikOpCode & 0x7))) !=
3879 0);
3880 if (singleStepMe || cUnit->allSingleStep) {
3881 notHandled = false;
3882 genInterpSingleStep(cUnit, mir);
3883 } else {
3884 opcodeCoverage[dalvikOpCode]++;
3885 switch (dalvikFormat) {
3886 case kFmt10t:
3887 case kFmt20t:
3888 case kFmt30t:
3889 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3890 mir, blockList[i], labelList);
3891 break;
3892 case kFmt10x:
3893 notHandled = handleFmt10x(cUnit, mir);
3894 break;
3895 case kFmt11n:
3896 case kFmt31i:
3897 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3898 break;
3899 case kFmt11x:
3900 notHandled = handleFmt11x(cUnit, mir);
3901 break;
3902 case kFmt12x:
3903 notHandled = handleFmt12x(cUnit, mir);
3904 break;
3905 case kFmt20bc:
3906 notHandled = handleFmt20bc(cUnit, mir);
3907 break;
3908 case kFmt21c:
3909 case kFmt31c:
3910 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3911 break;
3912 case kFmt21h:
3913 notHandled = handleFmt21h(cUnit, mir);
3914 break;
3915 case kFmt21s:
3916 notHandled = handleFmt21s(cUnit, mir);
3917 break;
3918 case kFmt21t:
3919 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3920 labelList);
3921 break;
3922 case kFmt22b:
3923 case kFmt22s:
3924 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3925 break;
3926 case kFmt22c:
3927 notHandled = handleFmt22c(cUnit, mir);
3928 break;
3929 case kFmt22cs:
3930 notHandled = handleFmt22cs(cUnit, mir);
3931 break;
3932 case kFmt22t:
3933 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3934 labelList);
3935 break;
3936 case kFmt22x:
3937 case kFmt32x:
3938 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3939 break;
3940 case kFmt23x:
3941 notHandled = handleFmt23x(cUnit, mir);
3942 break;
3943 case kFmt31t:
3944 notHandled = handleFmt31t(cUnit, mir);
3945 break;
3946 case kFmt3rc:
3947 case kFmt35c:
3948 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3949 labelList);
3950 break;
3951 case kFmt3rms:
3952 case kFmt35ms:
3953 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3954 labelList);
3955 break;
3956 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003957 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003958 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003959 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003960 case kFmt51l:
3961 notHandled = handleFmt51l(cUnit, mir);
3962 break;
3963 default:
3964 notHandled = true;
3965 break;
3966 }
3967 }
3968 if (notHandled) {
3969 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3970 mir->offset,
3971 dalvikOpCode, getOpcodeName(dalvikOpCode),
3972 dalvikFormat);
3973 dvmAbort();
3974 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003975 }
3976 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003977
Bill Buzbee1465db52009-09-23 17:17:35 -07003978 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003979 dvmCompilerAppendLIR(cUnit,
3980 (LIR *) cUnit->loopAnalysis->branchToBody);
3981 dvmCompilerAppendLIR(cUnit,
3982 (LIR *) cUnit->loopAnalysis->branchToPCR);
3983 }
3984
3985 if (headLIR) {
3986 /*
3987 * Eliminate redundant loads/stores and delay stores into later
3988 * slots
3989 */
3990 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
3991 cUnit->lastLIRInsn);
3992 }
3993
3994gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07003995 /*
3996 * Check if the block is terminated due to trace length constraint -
3997 * insert an unconditional branch to the chaining cell.
3998 */
3999 if (blockList[i]->needFallThroughBranch) {
4000 genUnconditionalBranch(cUnit,
4001 &labelList[blockList[i]->fallThrough->id]);
4002 }
4003
Ben Chengba4fc8b2009-06-01 13:00:29 -07004004 }
4005
Ben Chenge9695e52009-06-16 16:11:47 -07004006 /* Handle the chaining cells in predefined order */
Bill Buzbee1465db52009-09-23 17:17:35 -07004007 for (i = 0; i < kChainingCellLast; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004008 size_t j;
4009 int *blockIdList = (int *) chainingListByType[i].elemList;
4010
4011 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4012
4013 /* No chaining cells of this type */
4014 if (cUnit->numChainingCells[i] == 0)
4015 continue;
4016
4017 /* Record the first LIR for a new type of chaining cell */
4018 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4019
4020 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4021 int blockId = blockIdList[j];
4022
4023 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004024 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004025
4026 /* Insert the pseudo chaining instruction */
4027 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4028
4029
4030 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004031 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004032 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004033 blockList[blockId]->startOffset);
4034 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004035 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004036 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004037 blockList[blockId]->containingMethod);
4038 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004039 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004040 handleInvokePredictedChainingCell(cUnit);
4041 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004042 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004043 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004044 blockList[blockId]->startOffset);
4045 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004046#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004047 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004048 handleBackwardBranchChainingCell(cUnit,
4049 blockList[blockId]->startOffset);
4050 break;
4051#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004052 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004053 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004054 dvmAbort();
4055 break;
4056 }
4057 }
4058 }
Ben Chenge9695e52009-06-16 16:11:47 -07004059
Ben Cheng6c10a972009-10-29 14:39:18 -07004060 /*
4061 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4062 * of all chaining cells for the overflow cases.
4063 */
4064 if (cUnit->switchOverflowPad) {
4065 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4066 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4067 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4068 opRegReg(cUnit, kOpAdd, r1, r1);
4069 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
4070#if defined(EXIT_STATS)
4071 loadConstant(cUnit, r0, kSwitchOverflow);
4072#endif
4073 opReg(cUnit, kOpBlx, r2);
4074 }
4075
Ben Chenge9695e52009-06-16 16:11:47 -07004076 dvmCompilerApplyGlobalOptimizations(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004077}
4078
4079/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004080bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004081{
Ben Chengccd6c012009-10-15 14:52:45 -07004082 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004083
Ben Chengccd6c012009-10-15 14:52:45 -07004084 if (gDvmJit.codeCacheFull) {
4085 return false;
4086 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004087
Ben Chengccd6c012009-10-15 14:52:45 -07004088 switch (work->kind) {
4089 case kWorkOrderMethod:
4090 res = dvmCompileMethod(work->info, &work->result);
4091 break;
4092 case kWorkOrderTrace:
4093 /* Start compilation with maximally allowed trace length */
4094 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4095 break;
4096 case kWorkOrderTraceDebug: {
4097 bool oldPrintMe = gDvmJit.printMe;
4098 gDvmJit.printMe = true;
4099 /* Start compilation with maximally allowed trace length */
4100 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4101 gDvmJit.printMe = oldPrintMe;;
4102 break;
4103 }
4104 default:
4105 res = false;
4106 dvmAbort();
4107 }
4108 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004109}
4110
Ben Chengba4fc8b2009-06-01 13:00:29 -07004111/* Architectural-specific debugging helpers go here */
4112void dvmCompilerArchDump(void)
4113{
4114 /* Print compiled opcode in this VM instance */
4115 int i, start, streak;
4116 char buf[1024];
4117
4118 streak = i = 0;
4119 buf[0] = 0;
4120 while (opcodeCoverage[i] == 0 && i < 256) {
4121 i++;
4122 }
4123 if (i == 256) {
4124 return;
4125 }
4126 for (start = i++, streak = 1; i < 256; i++) {
4127 if (opcodeCoverage[i]) {
4128 streak++;
4129 } else {
4130 if (streak == 1) {
4131 sprintf(buf+strlen(buf), "%x,", start);
4132 } else {
4133 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4134 }
4135 streak = 0;
4136 while (opcodeCoverage[i] == 0 && i < 256) {
4137 i++;
4138 }
4139 if (i < 256) {
4140 streak = 1;
4141 start = i;
4142 }
4143 }
4144 }
4145 if (streak) {
4146 if (streak == 1) {
4147 sprintf(buf+strlen(buf), "%x", start);
4148 } else {
4149 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4150 }
4151 }
4152 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004153 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004154 }
4155}
Ben Chengd7d426a2009-09-22 11:23:36 -07004156
4157/* Common initialization routine for an architecture family */
4158bool dvmCompilerArchInit()
4159{
4160 int i;
4161
Bill Buzbee1465db52009-09-23 17:17:35 -07004162 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004163 if (EncodingMap[i].opCode != i) {
4164 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4165 EncodingMap[i].name, i, EncodingMap[i].opCode);
4166 dvmAbort();
4167 }
4168 }
4169
Ben Cheng5d90c202009-11-22 23:31:11 -08004170 return dvmCompilerArchVariantInit();
4171}
4172
4173void *dvmCompilerGetInterpretTemplate()
4174{
4175 return (void*) ((int)gDvmJit.codeCache +
4176 templateEntryOffsets[TEMPLATE_INTERPRET]);
4177}
4178
4179/* Needed by the ld/st optmizatons */
4180ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4181{
4182 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4183}
4184
4185/* Needed by the register allocator */
4186ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4187{
4188 return genRegCopy(cUnit, rDest, rSrc);
4189}
4190
4191/* Needed by the register allocator */
4192void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4193 int srcLo, int srcHi)
4194{
4195 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4196}
4197
4198void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4199 int displacement, int rSrc, OpSize size)
4200{
4201 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4202}
4203
4204void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4205 int displacement, int rSrcLo, int rSrcHi)
4206{
4207 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004208}