blob: b0e16b82f69a38492c30e6b6719f0a7a3675555b [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 Buzbee51ecf602010-01-14 14:27:52 -08001051 //TUNING: can improve this using Thumb2 code
1052 int tReg = allocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001053 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1054 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee51ecf602010-01-14 14:27:52 -08001055 loadConstantValue(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001056 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -08001057 tReg, rlSrc2.lowReg);
1058 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
1059 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001060 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001061 return false;
Ben Chenge9695e52009-06-16 16:11:47 -07001062 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001063 default:
1064 LOGE("Invalid long arith op");
1065 dvmAbort();
1066 }
1067 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001068 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001069 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001070 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001071 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001072 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1073 loadConstant(cUnit, rlr, (int) callTgt);
1074 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1075 opReg(cUnit, kOpBlx, rlr);
1076 clobberCallRegs(cUnit);
1077 if (retReg == r0)
1078 rlResult = getReturnLocWide(cUnit);
1079 else
1080 rlResult = getReturnLocWideAlt(cUnit);
1081 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001082 }
1083 return false;
1084}
1085
Ben Cheng5d90c202009-11-22 23:31:11 -08001086static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
1087 RegLocation rlDest, RegLocation rlSrc1,
1088 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001089{
Bill Buzbee1465db52009-09-23 17:17:35 -07001090 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001091 bool callOut = false;
1092 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -07001093 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001094 int retReg = r0;
1095 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -07001096 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -08001097 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001098
1099 /* TODO - find proper .h file to declare these */
1100 int __aeabi_idivmod(int op1, int op2);
1101 int __aeabi_idiv(int op1, int op2);
1102
1103 switch (mir->dalvikInsn.opCode) {
1104 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001105 op = kOpNeg;
1106 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001107 break;
1108 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001109 op = kOpMvn;
1110 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001111 break;
1112 case OP_ADD_INT:
1113 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001114 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001115 break;
1116 case OP_SUB_INT:
1117 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001118 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001119 break;
1120 case OP_MUL_INT:
1121 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001122 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001123 break;
1124 case OP_DIV_INT:
1125 case OP_DIV_INT_2ADDR:
1126 callOut = true;
1127 checkZero = true;
1128 callTgt = __aeabi_idiv;
1129 retReg = r0;
1130 break;
1131 /* NOTE: returns in r1 */
1132 case OP_REM_INT:
1133 case OP_REM_INT_2ADDR:
1134 callOut = true;
1135 checkZero = true;
1136 callTgt = __aeabi_idivmod;
1137 retReg = r1;
1138 break;
1139 case OP_AND_INT:
1140 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001141 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001142 break;
1143 case OP_OR_INT:
1144 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001145 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001146 break;
1147 case OP_XOR_INT:
1148 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001149 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001150 break;
1151 case OP_SHL_INT:
1152 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -08001153 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -07001154 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001155 break;
1156 case OP_SHR_INT:
1157 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -08001158 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -07001159 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001160 break;
1161 case OP_USHR_INT:
1162 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -08001163 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -07001164 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001165 break;
1166 default:
1167 LOGE("Invalid word arith op: 0x%x(%d)",
1168 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
1169 dvmAbort();
1170 }
1171 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001172 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1173 if (unary) {
1174 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1175 opRegReg(cUnit, op, rlResult.lowReg,
1176 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -07001177 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001178 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -08001179 if (shiftOp) {
1180 int tReg = allocTemp(cUnit);
1181 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1182 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1183 opRegRegReg(cUnit, op, rlResult.lowReg,
1184 rlSrc1.lowReg, tReg);
1185 freeTemp(cUnit, tReg);
1186 } else {
1187 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1188 opRegRegReg(cUnit, op, rlResult.lowReg,
1189 rlSrc1.lowReg, rlSrc2.lowReg);
1190 }
Ben Chenge9695e52009-06-16 16:11:47 -07001191 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001192 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001193 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001194 RegLocation rlResult;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001195 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001196 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001197 loadConstant(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -07001198 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001199 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001200 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001201 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001202 opReg(cUnit, kOpBlx, r2);
1203 clobberCallRegs(cUnit);
1204 if (retReg == r0)
1205 rlResult = getReturnLoc(cUnit);
1206 else
1207 rlResult = getReturnLocAlt(cUnit);
1208 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001209 }
1210 return false;
1211}
1212
Ben Cheng5d90c202009-11-22 23:31:11 -08001213static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001214{
1215 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001216 RegLocation rlDest;
1217 RegLocation rlSrc1;
1218 RegLocation rlSrc2;
1219 /* Deduce sizes of operands */
1220 if (mir->ssaRep->numUses == 2) {
1221 rlSrc1 = getSrcLoc(cUnit, mir, 0);
1222 rlSrc2 = getSrcLoc(cUnit, mir, 1);
1223 } else if (mir->ssaRep->numUses == 3) {
1224 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
1225 rlSrc2 = getSrcLoc(cUnit, mir, 2);
1226 } else {
1227 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
1228 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
1229 assert(mir->ssaRep->numUses == 4);
1230 }
1231 if (mir->ssaRep->numDefs == 1) {
1232 rlDest = getDestLoc(cUnit, mir, 0);
1233 } else {
1234 assert(mir->ssaRep->numDefs == 2);
1235 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1236 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001237
1238 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001239 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001240 }
1241 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001242 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001243 }
1244 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001245 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001246 }
1247 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001248 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001249 }
1250 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001251 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001252 }
1253 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001254 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001255 }
1256 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001257 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001258 }
1259 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001260 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001261 }
1262 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001263 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001264 }
1265 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001266 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001267 }
1268 return true;
1269}
1270
Bill Buzbee1465db52009-09-23 17:17:35 -07001271/* Generate conditional branch instructions */
1272static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
1273 ArmConditionCode cond,
1274 ArmLIR *target)
1275{
1276 ArmLIR *branch = opCondBranch(cUnit, cond);
1277 branch->generic.target = (LIR *) target;
1278 return branch;
1279}
1280
1281/* Generate unconditional branch instructions */
1282static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
1283{
1284 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
1285 branch->generic.target = (LIR *) target;
1286 return branch;
1287}
1288
Bill Buzbee1465db52009-09-23 17:17:35 -07001289/* Perform the actual operation for OP_RETURN_* */
1290static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
1291{
1292 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
1293#if defined(INVOKE_STATS)
1294 gDvmJit.returnOp++;
1295#endif
1296 int dPC = (int) (cUnit->method->insns + mir->offset);
1297 /* Insert branch, but defer setting of target */
1298 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
1299 /* Set up the place holder to reconstruct this Dalvik PC */
1300 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
1301 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
1302 pcrLabel->operands[0] = dPC;
1303 pcrLabel->operands[1] = mir->offset;
1304 /* Insert the place holder to the growable list */
1305 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1306 /* Branch to the PC reconstruction code */
1307 branch->generic.target = (LIR *) pcrLabel;
1308}
1309
Ben Chengba4fc8b2009-06-01 13:00:29 -07001310static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
1311 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001312 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001313{
1314 unsigned int i;
1315 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -07001316 RegLocation rlArg;
1317 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001318
Bill Buzbee1465db52009-09-23 17:17:35 -07001319 /*
1320 * Load arguments to r0..r4. Note that these registers may contain
1321 * live values, so we clobber them immediately after loading to prevent
1322 * them from being used as sources for subsequent loads.
1323 */
1324 lockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001325 for (i = 0; i < dInsn->vA; i++) {
1326 regMask |= 1 << i;
Bill Buzbee1465db52009-09-23 17:17:35 -07001327 rlArg = getSrcLoc(cUnit, mir, numDone++);
1328 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001329 }
1330 if (regMask) {
1331 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -07001332 opRegRegImm(cUnit, kOpSub, r7, rFP,
1333 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001334 /* generate null check */
1335 if (pcrLabel) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001336 *pcrLabel = genNullCheck(cUnit, getSrcSSAName(mir, 0), r0,
1337 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001338 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001339 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001340 }
1341}
1342
1343static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1344 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001345 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001346{
1347 int srcOffset = dInsn->vC << 2;
1348 int numArgs = dInsn->vA;
1349 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001350
1351 /*
1352 * Note: here, all promoted registers will have been flushed
1353 * back to the Dalvik base locations, so register usage restrictins
1354 * are lifted. All parms loaded from original Dalvik register
1355 * region - even though some might conceivably have valid copies
1356 * cached in a preserved register.
1357 */
1358 lockAllTemps(cUnit);
1359
Ben Chengba4fc8b2009-06-01 13:00:29 -07001360 /*
1361 * r4PC : &rFP[vC]
1362 * r7: &newFP[0]
1363 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001364 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001365 /* load [r0 .. min(numArgs,4)] */
1366 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001367 /*
1368 * Protect the loadMultiple instruction from being reordered with other
1369 * Dalvik stack accesses.
1370 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001371 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001372
Bill Buzbee1465db52009-09-23 17:17:35 -07001373 opRegRegImm(cUnit, kOpSub, r7, rFP,
1374 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001375 /* generate null check */
1376 if (pcrLabel) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001377 *pcrLabel = genNullCheck(cUnit, getSrcSSAName(mir, 0), r0,
1378 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001379 }
1380
1381 /*
1382 * Handle remaining 4n arguments:
1383 * store previously loaded 4 values and load the next 4 values
1384 */
1385 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001386 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001387 /*
1388 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001389 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001390 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001391 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001392 /* No need to generate the loop structure if numArgs <= 11 */
1393 if (numArgs > 11) {
1394 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001395 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001396 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001397 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001398 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001399 /*
1400 * Protect the loadMultiple instruction from being reordered with other
1401 * Dalvik stack accesses.
1402 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001403 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001404 /* No need to generate the loop structure if numArgs <= 11 */
1405 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001406 opRegImm(cUnit, kOpSub, rFP, 4);
1407 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001408 }
1409 }
1410
1411 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001412 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001413
1414 /* Generate the loop epilogue - don't use r0 */
1415 if ((numArgs > 4) && (numArgs % 4)) {
1416 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001417 /*
1418 * Protect the loadMultiple instruction from being reordered with other
1419 * Dalvik stack accesses.
1420 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001421 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001422 }
1423 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001424 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001425
1426 /* Save the modulo 4 arguments */
1427 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001428 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001429 }
1430}
1431
Ben Cheng38329f52009-07-07 14:19:20 -07001432/*
1433 * Generate code to setup the call stack then jump to the chaining cell if it
1434 * is not a native method.
1435 */
1436static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001437 BasicBlock *bb, ArmLIR *labelList,
1438 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001439 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001440{
Bill Buzbee1465db52009-09-23 17:17:35 -07001441 /*
1442 * Note: all Dalvik register state should be flushed to
1443 * memory by the point, so register usage restrictions no
1444 * longer apply. All temp & preserved registers may be used.
1445 */
1446 lockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001447 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001448
1449 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001450 lockTemp(cUnit, r1);
1451 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001452 /* r4PC = dalvikCallsite */
1453 loadConstant(cUnit, r4PC,
1454 (int) (cUnit->method->insns + mir->offset));
1455 addrRetChain->generic.target = (LIR *) retChainingCell;
1456 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001457 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001458 * r1 = &ChainingCell
1459 * r4PC = callsiteDPC
1460 */
1461 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001462 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001463#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07001464 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001465#endif
1466 } else {
1467 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
1468#if defined(INVOKE_STATS)
1469 gDvmJit.invokeChain++;
1470#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001471 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001472 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1473 }
1474 /* Handle exceptions using the interpreter */
1475 genTrap(cUnit, mir->offset, pcrLabel);
1476}
1477
Ben Cheng38329f52009-07-07 14:19:20 -07001478/*
1479 * Generate code to check the validity of a predicted chain and take actions
1480 * based on the result.
1481 *
1482 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1483 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1484 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1485 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1486 * 0x426a99b2 : blx_2 see above --+
1487 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1488 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1489 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1490 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1491 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1492 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1493 * 0x426a99c0 : blx r7 --+
1494 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1495 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1496 * 0x426a99c6 : blx_2 see above --+
1497 */
1498static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1499 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001500 ArmLIR *retChainingCell,
1501 ArmLIR *predChainingCell,
1502 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001503{
Bill Buzbee1465db52009-09-23 17:17:35 -07001504 /*
1505 * Note: all Dalvik register state should be flushed to
1506 * memory by the point, so register usage restrictions no
1507 * longer apply. Lock temps to prevent them from being
1508 * allocated by utility routines.
1509 */
1510 lockAllTemps(cUnit);
1511
Ben Cheng38329f52009-07-07 14:19:20 -07001512 /* "this" is already left in r0 by genProcessArgs* */
1513
1514 /* r4PC = dalvikCallsite */
1515 loadConstant(cUnit, r4PC,
1516 (int) (cUnit->method->insns + mir->offset));
1517
1518 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001519 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001520 addrRetChain->generic.target = (LIR *) retChainingCell;
1521
1522 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001523 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001524 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1525
1526 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1527
1528 /* return through lr - jump to the chaining cell */
1529 genUnconditionalBranch(cUnit, predChainingCell);
1530
1531 /*
1532 * null-check on "this" may have been eliminated, but we still need a PC-
1533 * reconstruction label for stack overflow bailout.
1534 */
1535 if (pcrLabel == NULL) {
1536 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001537 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001538 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07001539 pcrLabel->operands[0] = dPC;
1540 pcrLabel->operands[1] = mir->offset;
1541 /* Insert the place holder to the growable list */
1542 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1543 }
1544
1545 /* return through lr+2 - punt to the interpreter */
1546 genUnconditionalBranch(cUnit, pcrLabel);
1547
1548 /*
1549 * return through lr+4 - fully resolve the callee method.
1550 * r1 <- count
1551 * r2 <- &predictedChainCell
1552 * r3 <- this->class
1553 * r4 <- dPC
1554 * r7 <- this->class->vtable
1555 */
1556
1557 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001558 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001559
1560 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001561 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001562
Bill Buzbee1465db52009-09-23 17:17:35 -07001563 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001564
Bill Buzbee270c1d62009-08-13 16:58:07 -07001565 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1566 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001567
1568 /*
1569 * r0 = calleeMethod
1570 * r2 = &predictedChainingCell
1571 * r3 = class
1572 *
1573 * &returnChainingCell has been loaded into r1 but is not needed
1574 * when patching the chaining cell and will be clobbered upon
1575 * returning so it will be reconstructed again.
1576 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001577 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001578
1579 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001580 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001581 addrRetChain->generic.target = (LIR *) retChainingCell;
1582
1583 bypassRechaining->generic.target = (LIR *) addrRetChain;
1584 /*
1585 * r0 = calleeMethod,
1586 * r1 = &ChainingCell,
1587 * r4PC = callsiteDPC,
1588 */
1589 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
1590#if defined(INVOKE_STATS)
1591 gDvmJit.invokePredictedChain++;
1592#endif
1593 /* Handle exceptions using the interpreter */
1594 genTrap(cUnit, mir->offset, pcrLabel);
1595}
1596
1597/*
1598 * Up calling this function, "this" is stored in r0. The actual class will be
1599 * chased down off r0 and the predicted one will be retrieved through
1600 * predictedChainingCell then a comparison is performed to see whether the
1601 * previously established chaining is still valid.
1602 *
1603 * The return LIR is a branch based on the comparison result. The actual branch
1604 * target will be setup in the caller.
1605 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001606static ArmLIR *genCheckPredictedChain(CompilationUnit *cUnit,
1607 ArmLIR *predChainingCell,
1608 ArmLIR *retChainingCell,
Ben Cheng38329f52009-07-07 14:19:20 -07001609 MIR *mir)
1610{
Bill Buzbee1465db52009-09-23 17:17:35 -07001611 /*
1612 * Note: all Dalvik register state should be flushed to
1613 * memory by the point, so register usage restrictions no
1614 * longer apply. All temp & preserved registers may be used.
1615 */
1616 lockAllTemps(cUnit);
1617
Ben Cheng38329f52009-07-07 14:19:20 -07001618 /* r3 now contains this->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001619 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001620
1621 /*
1622 * r2 now contains predicted class. The starting offset of the
1623 * cached value is 4 bytes into the chaining cell.
1624 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001625 ArmLIR *getPredictedClass =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001626 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, clazz), r2);
Ben Cheng38329f52009-07-07 14:19:20 -07001627 getPredictedClass->generic.target = (LIR *) predChainingCell;
1628
1629 /*
1630 * r0 now contains predicted method. The starting offset of the
1631 * cached value is 8 bytes into the chaining cell.
1632 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001633 ArmLIR *getPredictedMethod =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001634 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, method), r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001635 getPredictedMethod->generic.target = (LIR *) predChainingCell;
1636
1637 /* Load the stats counter to see if it is time to unchain and refresh */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001638 ArmLIR *getRechainingRequestCount =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001639 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, counter), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001640 getRechainingRequestCount->generic.target =
1641 (LIR *) predChainingCell;
1642
1643 /* r4PC = dalvikCallsite */
1644 loadConstant(cUnit, r4PC,
1645 (int) (cUnit->method->insns + mir->offset));
1646
1647 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001648 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001649 addrRetChain->generic.target = (LIR *) retChainingCell;
1650
1651 /* Check if r2 (predicted class) == r3 (actual class) */
Bill Buzbee1465db52009-09-23 17:17:35 -07001652 opRegReg(cUnit, kOpCmp, r2, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001653
Bill Buzbee1465db52009-09-23 17:17:35 -07001654 return opCondBranch(cUnit, kArmCondEq);
Ben Cheng38329f52009-07-07 14:19:20 -07001655}
1656
Ben Chengba4fc8b2009-06-01 13:00:29 -07001657/* Geneate a branch to go back to the interpreter */
1658static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1659{
1660 /* r0 = dalvik pc */
Bill Buzbee1465db52009-09-23 17:17:35 -07001661 flushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001662 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001663 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1664 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1665 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001666 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001667}
1668
1669/*
1670 * Attempt to single step one instruction using the interpreter and return
1671 * to the compiled code for the next Dalvik instruction
1672 */
1673static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1674{
1675 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1676 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1677 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001678
1679 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
1680 flushAllRegs(cUnit);
1681
Ben Chengba4fc8b2009-06-01 13:00:29 -07001682 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1683 genPuntToInterp(cUnit, mir->offset);
1684 return;
1685 }
1686 int entryAddr = offsetof(InterpState,
1687 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001688 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001689 /* r0 = dalvik pc */
1690 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1691 /* r1 = dalvik pc of following instruction */
1692 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001693 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001694}
1695
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001696/*
1697 * To prevent a thread in a monitor wait from blocking the Jit from
1698 * resetting the code cache, heavyweight monitor lock will not
1699 * be allowed to return to an existing translation. Instead, we will
1700 * handle them by branching to a handler, which will in turn call the
1701 * runtime lock routine and then branch directly back to the
1702 * interpreter main loop. Given the high cost of the heavyweight
1703 * lock operation, this additional cost should be slight (especially when
1704 * considering that we expect the vast majority of lock operations to
1705 * use the fast-path thin lock bypass).
1706 */
Ben Cheng5d90c202009-11-22 23:31:11 -08001707static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001708{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001709 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbee1465db52009-09-23 17:17:35 -07001710 genExportPC(cUnit, mir);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001711 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001712 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
1713 loadValueDirectFixed(cUnit, rlSrc, r1);
1714 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001715 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001716 if (isEnter) {
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001717 /* Get dPC of next insn */
1718 loadConstant(cUnit, r4PC, (int)(cUnit->method->insns + mir->offset +
1719 dexGetInstrWidthAbs(gDvm.instrWidth, OP_MONITOR_ENTER)));
1720#if defined(WITH_DEADLOCK_PREDICTION)
1721 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER_DEBUG);
1722#else
1723 genDispatchToHandler(cUnit, TEMPLATE_MONITOR_ENTER);
1724#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001725 } else {
1726 loadConstant(cUnit, r2, (int)dvmUnlockObject);
Bill Buzbeec1d9ed42010-02-02 11:04:33 -08001727 /* Do the call */
1728 opReg(cUnit, kOpBlx, r2);
1729 clobberCallRegs(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001730 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001731}
1732
Ben Chengba4fc8b2009-06-01 13:00:29 -07001733/*
1734 * The following are the first-level codegen routines that analyze the format
1735 * of each bytecode then either dispatch special purpose codegen routines
1736 * or produce corresponding Thumb instructions directly.
1737 */
1738
1739static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001740 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001741{
1742 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1743 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1744 return false;
1745}
1746
1747static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1748{
1749 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1750 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden96516932009-10-28 17:39:02 -07001751 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_EB))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001752 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1753 return true;
1754 }
1755 switch (dalvikOpCode) {
1756 case OP_RETURN_VOID:
1757 genReturnCommon(cUnit,mir);
1758 break;
1759 case OP_UNUSED_73:
1760 case OP_UNUSED_79:
1761 case OP_UNUSED_7A:
1762 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1763 return true;
1764 case OP_NOP:
1765 break;
1766 default:
1767 return true;
1768 }
1769 return false;
1770}
1771
1772static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1773{
Bill Buzbee1465db52009-09-23 17:17:35 -07001774 RegLocation rlDest;
1775 RegLocation rlResult;
1776 if (mir->ssaRep->numDefs == 2) {
1777 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1778 } else {
1779 rlDest = getDestLoc(cUnit, mir, 0);
1780 }
Ben Chenge9695e52009-06-16 16:11:47 -07001781
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 switch (mir->dalvikInsn.opCode) {
1783 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001784 case OP_CONST_4: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001785 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1786 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1787 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001788 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001789 }
1790 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001791 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001792 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001793 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1794 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1795 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1796 rlResult.lowReg, 31);
1797 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001798 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001799 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001800 default:
1801 return true;
1802 }
1803 return false;
1804}
1805
1806static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1807{
Bill Buzbee1465db52009-09-23 17:17:35 -07001808 RegLocation rlDest;
1809 RegLocation rlResult;
1810 if (mir->ssaRep->numDefs == 2) {
1811 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1812 } else {
1813 rlDest = getDestLoc(cUnit, mir, 0);
1814 }
1815 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001816
Ben Chengba4fc8b2009-06-01 13:00:29 -07001817 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001818 case OP_CONST_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001819 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB << 16);
1820 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001821 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001822 }
1823 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001824 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1825 0, mir->dalvikInsn.vB << 16);
1826 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001827 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001828 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001829 default:
1830 return true;
1831 }
1832 return false;
1833}
1834
1835static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1836{
1837 /* For OP_THROW_VERIFICATION_ERROR */
1838 genInterpSingleStep(cUnit, mir);
1839 return false;
1840}
1841
1842static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1843{
Bill Buzbee1465db52009-09-23 17:17:35 -07001844 RegLocation rlResult;
1845 RegLocation rlDest;
1846 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001847
Ben Chengba4fc8b2009-06-01 13:00:29 -07001848 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001849 case OP_CONST_STRING_JUMBO:
1850 case OP_CONST_STRING: {
1851 void *strPtr = (void*)
1852 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
1853 assert(strPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001854 rlDest = getDestLoc(cUnit, mir, 0);
1855 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1856 loadConstantValue(cUnit, rlResult.lowReg, (int) strPtr );
1857 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001858 break;
1859 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001860 case OP_CONST_CLASS: {
1861 void *classPtr = (void*)
1862 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1863 assert(classPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001864 rlDest = getDestLoc(cUnit, mir, 0);
1865 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1866 loadConstantValue(cUnit, rlResult.lowReg, (int) classPtr );
1867 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001868 break;
1869 }
1870 case OP_SGET_OBJECT:
1871 case OP_SGET_BOOLEAN:
1872 case OP_SGET_CHAR:
1873 case OP_SGET_BYTE:
1874 case OP_SGET_SHORT:
1875 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001876 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001877 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001878 void *fieldPtr = (void*)
1879 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
1880 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001881 rlDest = getDestLoc(cUnit, mir, 0);
1882 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1883 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001884#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001885 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001886#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001887 int regMap = rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001888 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationLoad);
1889
Jeff Hao97319a82009-08-12 16:57:15 -07001890#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001891 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001892 break;
1893 }
1894 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001895 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001896 void *fieldPtr = (void*)
1897 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Bill Buzbee1465db52009-09-23 17:17:35 -07001898 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001899 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001900 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1901 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1902 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001903#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001904 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001905#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001906 int regMap = rlResult.highReg << 16 |
1907 rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001908 selfVerificationMemOpWrapper(cUnit, regMap,
1909 &selfVerificationLoadDoubleword);
1910
Jeff Hao97319a82009-08-12 16:57:15 -07001911#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001912 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001913 break;
1914 }
1915 case OP_SPUT_OBJECT:
1916 case OP_SPUT_BOOLEAN:
1917 case OP_SPUT_CHAR:
1918 case OP_SPUT_BYTE:
1919 case OP_SPUT_SHORT:
1920 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001921 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001922 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001923 void *fieldPtr = (void*)
1924 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001925
Ben Chengba4fc8b2009-06-01 13:00:29 -07001926 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001927 rlSrc = getSrcLoc(cUnit, mir, 0);
1928 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1929 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001930#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001931 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001932#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001933 int regMap = rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001934 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationStore);
1935#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001936 break;
1937 }
1938 case OP_SPUT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001939 int tReg = allocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001940 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001941 void *fieldPtr = (void*)
1942 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001943
Ben Chengba4fc8b2009-06-01 13:00:29 -07001944 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001945 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
1946 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1947 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001948#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001949 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001950#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001951 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001952 selfVerificationMemOpWrapper(cUnit, regMap,
1953 &selfVerificationStoreDoubleword);
1954#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001955 break;
1956 }
1957 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001958 /*
1959 * Obey the calling convention and don't mess with the register
1960 * usage.
1961 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001962 ClassObject *classPtr = (void*)
1963 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1964 assert(classPtr != NULL);
1965 assert(classPtr->status & CLASS_INITIALIZED);
Ben Cheng79d173c2009-09-29 16:12:51 -07001966 /*
1967 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001968 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001969 */
1970 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001971 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001972 genExportPC(cUnit, mir);
1973 loadConstant(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001974 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001975 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001976 opReg(cUnit, kOpBlx, r2);
1977 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001978 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001979 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1980 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001981 /*
1982 * OOM exception needs to be thrown here and cannot re-execute
1983 */
1984 loadConstant(cUnit, r0,
1985 (int) (cUnit->method->insns + mir->offset));
1986 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1987 /* noreturn */
1988
Bill Buzbee1465db52009-09-23 17:17:35 -07001989 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001990 target->defMask = ENCODE_ALL;
1991 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07001992 rlDest = getDestLoc(cUnit, mir, 0);
1993 rlResult = getReturnLoc(cUnit);
1994 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001995 break;
1996 }
1997 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001998 /*
1999 * Obey the calling convention and don't mess with the register
2000 * usage.
2001 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002002 ClassObject *classPtr =
2003 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08002004 /*
2005 * Note: It is possible that classPtr is NULL at this point,
2006 * even though this instruction has been successfully interpreted.
2007 * If the previous interpretation had a null source, the
2008 * interpreter would not have bothered to resolve the clazz.
2009 * Bail out to the interpreter in this case, and log it
2010 * so that we can tell if it happens frequently.
2011 */
2012 if (classPtr == NULL) {
2013 LOGD("null clazz in OP_CHECK_CAST, single-stepping");
2014 genInterpSingleStep(cUnit, mir);
2015 return false;
2016 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002017 flushAllRegs(cUnit); /* Send everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002018 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002019 rlSrc = getSrcLoc(cUnit, mir, 0);
2020 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2021 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
2022 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
2023 /*
2024 * rlSrc.lowReg now contains object->clazz. Note that
2025 * it could have been allocated r0, but we're okay so long
2026 * as we don't do anything desctructive until r0 is loaded
2027 * with clazz.
2028 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002029 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002030 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
2031 loadConstant(cUnit, r2, (int)dvmInstanceofNonTrivial);
2032 opRegReg(cUnit, kOpCmp, r0, r1);
2033 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2034 opReg(cUnit, kOpBlx, r2);
2035 clobberCallRegs(cUnit);
2036 /*
2037 * If null, check cast failed - punt to the interpreter. Because
2038 * interpreter will be the one throwing, we don't need to
2039 * genExportPC() here.
2040 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002041 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002042 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002043 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002044 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002045 branch1->generic.target = (LIR *)target;
2046 branch2->generic.target = (LIR *)target;
2047 break;
2048 }
2049 default:
2050 return true;
2051 }
2052 return false;
2053}
2054
2055static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
2056{
2057 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002058 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002059 switch (dalvikOpCode) {
2060 case OP_MOVE_EXCEPTION: {
2061 int offset = offsetof(InterpState, self);
2062 int exOffset = offsetof(Thread, exception);
Bill Buzbee1465db52009-09-23 17:17:35 -07002063 int selfReg = allocTemp(cUnit);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002064 int resetReg = allocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002065 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2066 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2067 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002068 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002069 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002070 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07002071 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002072 break;
2073 }
2074 case OP_MOVE_RESULT:
2075 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002076 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2077 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
2078 rlSrc.fp = rlDest.fp;
2079 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002080 break;
2081 }
2082 case OP_MOVE_RESULT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002083 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
2084 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
2085 rlSrc.fp = rlDest.fp;
2086 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002087 break;
2088 }
2089 case OP_RETURN_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002090 RegLocation rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2091 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
2092 rlDest.fp = rlSrc.fp;
2093 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002094 genReturnCommon(cUnit,mir);
2095 break;
2096 }
2097 case OP_RETURN:
2098 case OP_RETURN_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002099 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2100 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
2101 rlDest.fp = rlSrc.fp;
2102 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002103 genReturnCommon(cUnit,mir);
2104 break;
2105 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002106 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002107 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08002108#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08002109 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002110#else
Ben Cheng5d90c202009-11-22 23:31:11 -08002111 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002112#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07002113 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002114 case OP_THROW: {
2115 genInterpSingleStep(cUnit, mir);
2116 break;
2117 }
2118 default:
2119 return true;
2120 }
2121 return false;
2122}
2123
Bill Buzbeed45ba372009-06-15 17:00:57 -07002124static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
2125{
2126 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002127 RegLocation rlDest;
2128 RegLocation rlSrc;
2129 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07002130
Ben Chengba4fc8b2009-06-01 13:00:29 -07002131 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002132 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002133 }
2134
Bill Buzbee1465db52009-09-23 17:17:35 -07002135 if (mir->ssaRep->numUses == 2)
2136 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2137 else
2138 rlSrc = getSrcLoc(cUnit, mir, 0);
2139 if (mir->ssaRep->numDefs == 2)
2140 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2141 else
2142 rlDest = getDestLoc(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07002143
Ben Chengba4fc8b2009-06-01 13:00:29 -07002144 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002145 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002146 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002147 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002148 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002149 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002150 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002151 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002153 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002154 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002155 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002156 case OP_NEG_INT:
2157 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002158 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002159 case OP_NEG_LONG:
2160 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08002161 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002162 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002163 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002164 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002165 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002166 case OP_MOVE_WIDE:
2167 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002168 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002169 case OP_INT_TO_LONG:
2170 rlSrc = updateLoc(cUnit, rlSrc);
2171 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002172 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07002173 if (rlSrc.location == kLocPhysReg) {
2174 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2175 } else {
2176 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2177 }
2178 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2179 rlResult.lowReg, 31);
2180 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002181 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002182 case OP_LONG_TO_INT:
2183 rlSrc = updateLocWide(cUnit, rlSrc);
2184 rlSrc = wideToNarrowLoc(cUnit, rlSrc);
2185 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002186 case OP_MOVE:
2187 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002188 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002189 break;
2190 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002191 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2192 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2193 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2194 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002195 break;
2196 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002197 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2198 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2199 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2200 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002201 break;
2202 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002203 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2204 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2205 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2206 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002207 break;
2208 case OP_ARRAY_LENGTH: {
2209 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002210 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2211 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2212 mir->offset, NULL);
2213 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2214 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2215 rlResult.lowReg);
2216 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002217 break;
2218 }
2219 default:
2220 return true;
2221 }
2222 return false;
2223}
2224
2225static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2226{
2227 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002228 RegLocation rlDest;
2229 RegLocation rlResult;
2230 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002231 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002232 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2233 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2234 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002235 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07002236 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2237 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002238 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002239 rlDest = getDestLoc(cUnit, mir, 0);
2240 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
2241 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
2242 storeValue(cUnit, rlDest, rlResult);
2243 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002244 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002245 return false;
2246}
2247
2248/* Compare agaist zero */
2249static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002250 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002251{
2252 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002253 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002254 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2255 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2256 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002257
Bill Buzbee270c1d62009-08-13 16:58:07 -07002258//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07002259 switch (dalvikOpCode) {
2260 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002261 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002262 break;
2263 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002264 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002265 break;
2266 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002267 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002268 break;
2269 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002270 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002271 break;
2272 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002273 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002274 break;
2275 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002276 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002277 break;
2278 default:
2279 cond = 0;
2280 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
2281 dvmAbort();
2282 }
2283 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2284 /* This mostly likely will be optimized away in a later phase */
2285 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2286 return false;
2287}
2288
2289static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2290{
2291 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002292 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2293 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2294 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002295 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002296 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002297 int shiftOp = false;
2298 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002299
Ben Chengba4fc8b2009-06-01 13:00:29 -07002300 int __aeabi_idivmod(int op1, int op2);
2301 int __aeabi_idiv(int op1, int op2);
2302
2303 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002304 case OP_RSUB_INT_LIT8:
2305 case OP_RSUB_INT: {
2306 int tReg;
2307 //TUNING: add support for use of Arm rsub op
2308 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2309 tReg = allocTemp(cUnit);
2310 loadConstant(cUnit, tReg, lit);
2311 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2312 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2313 tReg, rlSrc.lowReg);
2314 storeValue(cUnit, rlDest, rlResult);
2315 return false;
2316 break;
2317 }
2318
Ben Chengba4fc8b2009-06-01 13:00:29 -07002319 case OP_ADD_INT_LIT8:
2320 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002321 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002322 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002323 case OP_MUL_INT_LIT8:
2324 case OP_MUL_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002325 op = kOpMul;
2326 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002327 case OP_AND_INT_LIT8:
2328 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002329 op = kOpAnd;
2330 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002331 case OP_OR_INT_LIT8:
2332 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002333 op = kOpOr;
2334 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002335 case OP_XOR_INT_LIT8:
2336 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002337 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002338 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002339 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002340 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002341 shiftOp = true;
2342 op = kOpLsl;
2343 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002344 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002345 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002346 shiftOp = true;
2347 op = kOpAsr;
2348 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002349 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002350 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002351 shiftOp = true;
2352 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002353 break;
2354
2355 case OP_DIV_INT_LIT8:
2356 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002357 case OP_REM_INT_LIT8:
2358 case OP_REM_INT_LIT16:
2359 if (lit == 0) {
2360 /* Let the interpreter deal with div by 0 */
2361 genInterpSingleStep(cUnit, mir);
2362 return false;
2363 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002364 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002365 loadValueDirectFixed(cUnit, rlSrc, r0);
2366 clobberReg(cUnit, r0);
2367 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2368 (dalvikOpCode == OP_DIV_INT_LIT16)) {
2369 loadConstant(cUnit, r2, (int)__aeabi_idiv);
2370 isDiv = true;
2371 } else {
2372 loadConstant(cUnit, r2, (int)__aeabi_idivmod);
2373 isDiv = false;
2374 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002375 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002376 opReg(cUnit, kOpBlx, r2);
2377 clobberCallRegs(cUnit);
2378 if (isDiv)
2379 rlResult = getReturnLoc(cUnit);
2380 else
2381 rlResult = getReturnLocAlt(cUnit);
2382 storeValue(cUnit, rlDest, rlResult);
2383 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002384 break;
2385 default:
2386 return true;
2387 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002388 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2389 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2390 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2391 if (shiftOp && (lit == 0)) {
2392 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2393 } else {
2394 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2395 }
2396 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002397 return false;
2398}
2399
2400static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2401{
2402 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2403 int fieldOffset;
2404
2405 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2406 InstField *pInstField = (InstField *)
2407 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002408
2409 assert(pInstField != NULL);
2410 fieldOffset = pInstField->byteOffset;
2411 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002412 /* Deliberately break the code while make the compiler happy */
2413 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002414 }
2415 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002416 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002417 // Generates a call - use explicit registers
2418 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2419 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2420 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002421 void *classPtr = (void*)
2422 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2423 assert(classPtr != NULL);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002424 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002425 genExportPC(cUnit, mir);
2426 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002427 loadConstant(cUnit, r0, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002428 loadConstant(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002429 /*
2430 * "len < 0": bail to the interpreter to re-execute the
2431 * instruction
2432 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002433 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002434 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002435 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002436 opReg(cUnit, kOpBlx, r3);
2437 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002438 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002439 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2440 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002441 /*
2442 * OOM exception needs to be thrown here and cannot re-execute
2443 */
2444 loadConstant(cUnit, r0,
2445 (int) (cUnit->method->insns + mir->offset));
2446 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2447 /* noreturn */
2448
Bill Buzbee1465db52009-09-23 17:17:35 -07002449 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002450 target->defMask = ENCODE_ALL;
2451 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07002452 rlResult = getReturnLoc(cUnit);
2453 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002454 break;
2455 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002456 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002457 // May generate a call - use explicit registers
2458 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2459 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2460 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002461 ClassObject *classPtr =
2462 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002463 /*
2464 * Note: It is possible that classPtr is NULL at this point,
2465 * even though this instruction has been successfully interpreted.
2466 * If the previous interpretation had a null source, the
2467 * interpreter would not have bothered to resolve the clazz.
2468 * Bail out to the interpreter in this case, and log it
2469 * so that we can tell if it happens frequently.
2470 */
2471 if (classPtr == NULL) {
2472 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2473 genInterpSingleStep(cUnit, mir);
2474 break;
2475 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002476 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002477 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002478 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002479//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002480 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002481 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002482 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002483 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002484 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002485 /* r1 now contains object->clazz */
2486 loadConstant(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002487 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002488 opRegReg(cUnit, kOpCmp, r1, r2);
2489 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2490 genRegCopy(cUnit, r0, r1);
2491 genRegCopy(cUnit, r1, r2);
2492 opReg(cUnit, kOpBlx, r3);
2493 clobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002494 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002495 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002496 target->defMask = ENCODE_ALL;
Bill Buzbee1465db52009-09-23 17:17:35 -07002497 rlResult = getReturnLoc(cUnit);
2498 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002499 branch1->generic.target = (LIR *)target;
2500 branch2->generic.target = (LIR *)target;
2501 break;
2502 }
2503 case OP_IGET_WIDE:
2504 genIGetWide(cUnit, mir, fieldOffset);
2505 break;
2506 case OP_IGET:
2507 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002508 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002509 break;
2510 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002511 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002512 break;
2513 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002514 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002515 break;
2516 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002517 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002518 break;
2519 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002520 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002521 break;
2522 case OP_IPUT_WIDE:
2523 genIPutWide(cUnit, mir, fieldOffset);
2524 break;
2525 case OP_IPUT:
2526 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002527 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002528 break;
2529 case OP_IPUT_SHORT:
2530 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002531 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002532 break;
2533 case OP_IPUT_BYTE:
2534 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002535 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002536 break;
2537 default:
2538 return true;
2539 }
2540 return false;
2541}
2542
2543static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2544{
2545 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2546 int fieldOffset = mir->dalvikInsn.vC;
2547 switch (dalvikOpCode) {
2548 case OP_IGET_QUICK:
2549 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002550 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002551 break;
2552 case OP_IPUT_QUICK:
2553 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002554 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002555 break;
2556 case OP_IGET_WIDE_QUICK:
2557 genIGetWide(cUnit, mir, fieldOffset);
2558 break;
2559 case OP_IPUT_WIDE_QUICK:
2560 genIPutWide(cUnit, mir, fieldOffset);
2561 break;
2562 default:
2563 return true;
2564 }
2565 return false;
2566
2567}
2568
2569/* Compare agaist zero */
2570static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002571 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002572{
2573 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002574 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002575 RegLocation rlSrc1 = getSrcLoc(cUnit, mir, 0);
2576 RegLocation rlSrc2 = getSrcLoc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002577
Bill Buzbee1465db52009-09-23 17:17:35 -07002578 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2579 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2580 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002581
2582 switch (dalvikOpCode) {
2583 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002584 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002585 break;
2586 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002587 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002588 break;
2589 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002590 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002591 break;
2592 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002593 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002594 break;
2595 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002596 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002597 break;
2598 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002599 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002600 break;
2601 default:
2602 cond = 0;
2603 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
2604 dvmAbort();
2605 }
2606 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2607 /* This mostly likely will be optimized away in a later phase */
2608 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2609 return false;
2610}
2611
2612static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2613{
2614 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002615
2616 switch (opCode) {
2617 case OP_MOVE_16:
2618 case OP_MOVE_OBJECT_16:
2619 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002620 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002621 storeValue(cUnit, getDestLoc(cUnit, mir, 0),
2622 getSrcLoc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002623 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002624 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002625 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002626 case OP_MOVE_WIDE_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002627 storeValueWide(cUnit, getDestLocWide(cUnit, mir, 0, 1),
2628 getSrcLocWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002629 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002630 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002631 default:
2632 return true;
2633 }
2634 return false;
2635}
2636
2637static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2638{
2639 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002640 RegLocation rlSrc1;
2641 RegLocation rlSrc2;
2642 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002643
2644 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002645 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002646 }
2647
Bill Buzbee1465db52009-09-23 17:17:35 -07002648 /* APUTs have 3 sources and no targets */
2649 if (mir->ssaRep->numDefs == 0) {
2650 if (mir->ssaRep->numUses == 3) {
2651 rlDest = getSrcLoc(cUnit, mir, 0);
2652 rlSrc1 = getSrcLoc(cUnit, mir, 1);
2653 rlSrc2 = getSrcLoc(cUnit, mir, 2);
2654 } else {
2655 assert(mir->ssaRep->numUses == 4);
2656 rlDest = getSrcLocWide(cUnit, mir, 0, 1);
2657 rlSrc1 = getSrcLoc(cUnit, mir, 2);
2658 rlSrc2 = getSrcLoc(cUnit, mir, 3);
2659 }
2660 } else {
2661 /* Two sources and 1 dest. Deduce the operand sizes */
2662 if (mir->ssaRep->numUses == 4) {
2663 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
2664 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
2665 } else {
2666 assert(mir->ssaRep->numUses == 2);
2667 rlSrc1 = getSrcLoc(cUnit, mir, 0);
2668 rlSrc2 = getSrcLoc(cUnit, mir, 1);
2669 }
2670 if (mir->ssaRep->numDefs == 2) {
2671 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2672 } else {
2673 assert(mir->ssaRep->numDefs == 1);
2674 rlDest = getDestLoc(cUnit, mir, 0);
2675 }
2676 }
2677
2678
Ben Chengba4fc8b2009-06-01 13:00:29 -07002679 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002680 case OP_CMPL_FLOAT:
2681 case OP_CMPG_FLOAT:
2682 case OP_CMPL_DOUBLE:
2683 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002684 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002685 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002686 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002687 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002688 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002689 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002690 break;
2691 case OP_AGET:
2692 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002693 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002694 break;
2695 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002696 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002697 break;
2698 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002699 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002700 break;
2701 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002702 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002703 break;
2704 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002705 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002706 break;
2707 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002708 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002709 break;
2710 case OP_APUT:
2711 case OP_APUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002712 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002713 break;
2714 case OP_APUT_SHORT:
2715 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002716 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002717 break;
2718 case OP_APUT_BYTE:
2719 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002720 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002721 break;
2722 default:
2723 return true;
2724 }
2725 return false;
2726}
2727
Ben Cheng6c10a972009-10-29 14:39:18 -07002728/*
2729 * Find the matching case.
2730 *
2731 * return values:
2732 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2733 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2734 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2735 * above MAX_CHAINED_SWITCH_CASES).
2736 *
2737 * Instructions around the call are:
2738 *
2739 * mov r2, pc
2740 * blx &findPackedSwitchIndex
2741 * mov pc, r0
2742 * .align4
2743 * chaining cell for case 0 [8 bytes]
2744 * chaining cell for case 1 [8 bytes]
2745 * :
2746 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2747 * chaining cell for case default [8 bytes]
2748 * noChain exit
2749 */
2750s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
2751{
2752 int size;
2753 int firstKey;
2754 const int *entries;
2755 int index;
2756 int jumpIndex;
2757 int caseDPCOffset = 0;
2758 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2759 int chainingPC = (pc + 4) & ~3;
2760
2761 /*
2762 * Packed switch data format:
2763 * ushort ident = 0x0100 magic value
2764 * ushort size number of entries in the table
2765 * int first_key first (and lowest) switch case value
2766 * int targets[size] branch targets, relative to switch opcode
2767 *
2768 * Total size is (4+size*2) 16-bit code units.
2769 */
2770 size = switchData[1];
2771 assert(size > 0);
2772
2773 firstKey = switchData[2];
2774 firstKey |= switchData[3] << 16;
2775
2776
2777 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2778 * we can treat them as a native int array.
2779 */
2780 entries = (const int*) &switchData[4];
2781 assert(((u4)entries & 0x3) == 0);
2782
2783 index = testVal - firstKey;
2784
2785 /* Jump to the default cell */
2786 if (index < 0 || index >= size) {
2787 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2788 /* Jump to the non-chaining exit point */
2789 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2790 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2791 caseDPCOffset = entries[index];
2792 /* Jump to the inline chaining cell */
2793 } else {
2794 jumpIndex = index;
2795 }
2796
2797 chainingPC += jumpIndex * 8;
2798 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2799}
2800
2801/* See comments for findPackedSwitchIndex */
2802s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
2803{
2804 int size;
2805 const int *keys;
2806 const int *entries;
2807 int chainingPC = (pc + 4) & ~3;
2808 int i;
2809
2810 /*
2811 * Sparse switch data format:
2812 * ushort ident = 0x0200 magic value
2813 * ushort size number of entries in the table; > 0
2814 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2815 * int targets[size] branch targets, relative to switch opcode
2816 *
2817 * Total size is (2+size*4) 16-bit code units.
2818 */
2819
2820 size = switchData[1];
2821 assert(size > 0);
2822
2823 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2824 * we can treat them as a native int array.
2825 */
2826 keys = (const int*) &switchData[2];
2827 assert(((u4)keys & 0x3) == 0);
2828
2829 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2830 * we can treat them as a native int array.
2831 */
2832 entries = keys + size;
2833 assert(((u4)entries & 0x3) == 0);
2834
2835 /*
2836 * Run through the list of keys, which are guaranteed to
2837 * be sorted low-to-high.
2838 *
2839 * Most tables have 3-4 entries. Few have more than 10. A binary
2840 * search here is probably not useful.
2841 */
2842 for (i = 0; i < size; i++) {
2843 int k = keys[i];
2844 if (k == testVal) {
2845 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2846 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2847 i : MAX_CHAINED_SWITCH_CASES + 1;
2848 chainingPC += jumpIndex * 8;
2849 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2850 } else if (k > testVal) {
2851 break;
2852 }
2853 }
2854 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2855}
2856
Ben Chengba4fc8b2009-06-01 13:00:29 -07002857static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2858{
2859 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2860 switch (dalvikOpCode) {
2861 case OP_FILL_ARRAY_DATA: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002862 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2863 // Making a call - use explicit registers
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002864 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002865 genExportPC(cUnit, mir);
2866 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002867 loadConstant(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002868 loadConstant(cUnit, r1,
2869 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002870 opReg(cUnit, kOpBlx, r2);
2871 clobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002872 /* generate a branch over if successful */
2873 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2874 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2875 loadConstant(cUnit, r0,
2876 (int) (cUnit->method->insns + mir->offset));
2877 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2878 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2879 target->defMask = ENCODE_ALL;
2880 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002881 break;
2882 }
2883 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002884 * Compute the goto target of up to
2885 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2886 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002887 */
2888 case OP_PACKED_SWITCH:
2889 case OP_SPARSE_SWITCH: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002890 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002891 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002892 loadValueDirectFixed(cUnit, rlSrc, r1);
2893 lockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002894 const u2 *switchData =
2895 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2896 u2 size = switchData[1];
2897
Ben Chengba4fc8b2009-06-01 13:00:29 -07002898 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Cheng6c10a972009-10-29 14:39:18 -07002899 loadConstant(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002900 } else {
Ben Cheng6c10a972009-10-29 14:39:18 -07002901 loadConstant(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002902 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002903 /* r0 <- Addr of the switch data */
2904 loadConstant(cUnit, r0,
2905 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2906 /* r2 <- pc of the instruction following the blx */
2907 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002908 opReg(cUnit, kOpBlx, r4PC);
2909 clobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002910 /* pc <- computed goto target */
2911 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002912 break;
2913 }
2914 default:
2915 return true;
2916 }
2917 return false;
2918}
2919
2920static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002921 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002922{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002923 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002924 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002925
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002926 if (bb->fallThrough != NULL)
2927 retChainingCell = &labelList[bb->fallThrough->id];
2928
Ben Chengba4fc8b2009-06-01 13:00:29 -07002929 DecodedInstruction *dInsn = &mir->dalvikInsn;
2930 switch (mir->dalvikInsn.opCode) {
2931 /*
2932 * calleeMethod = this->clazz->vtable[
2933 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2934 * ]
2935 */
2936 case OP_INVOKE_VIRTUAL:
2937 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002938 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002939 int methodIndex =
2940 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2941 methodIndex;
2942
2943 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2944 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2945 else
2946 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2947
Ben Cheng38329f52009-07-07 14:19:20 -07002948 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2949 retChainingCell,
2950 predChainingCell,
2951 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002952 break;
2953 }
2954 /*
2955 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2956 * ->pResMethods[BBBB]->methodIndex]
2957 */
2958 /* TODO - not excersized in RunPerf.jar */
2959 case OP_INVOKE_SUPER:
2960 case OP_INVOKE_SUPER_RANGE: {
2961 int mIndex = cUnit->method->clazz->pDvmDex->
2962 pResMethods[dInsn->vB]->methodIndex;
2963 const Method *calleeMethod =
2964 cUnit->method->clazz->super->vtable[mIndex];
2965
2966 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2967 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2968 else
2969 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2970
2971 /* r0 = calleeMethod */
2972 loadConstant(cUnit, r0, (int) calleeMethod);
2973
Ben Cheng38329f52009-07-07 14:19:20 -07002974 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2975 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002976 break;
2977 }
2978 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2979 case OP_INVOKE_DIRECT:
2980 case OP_INVOKE_DIRECT_RANGE: {
2981 const Method *calleeMethod =
2982 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2983
2984 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2985 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2986 else
2987 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2988
2989 /* r0 = calleeMethod */
2990 loadConstant(cUnit, r0, (int) calleeMethod);
2991
Ben Cheng38329f52009-07-07 14:19:20 -07002992 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2993 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002994 break;
2995 }
2996 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2997 case OP_INVOKE_STATIC:
2998 case OP_INVOKE_STATIC_RANGE: {
2999 const Method *calleeMethod =
3000 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
3001
3002 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
3003 genProcessArgsNoRange(cUnit, mir, dInsn,
3004 NULL /* no null check */);
3005 else
3006 genProcessArgsRange(cUnit, mir, dInsn,
3007 NULL /* no null check */);
3008
3009 /* r0 = calleeMethod */
3010 loadConstant(cUnit, r0, (int) calleeMethod);
3011
Ben Cheng38329f52009-07-07 14:19:20 -07003012 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3013 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003014 break;
3015 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003016 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003017 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3018 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003019 *
3020 * Given "invoke-interface {v0}", the following is the generated code:
3021 *
3022 * 0x426a9abe : ldr r0, [r5, #0] --+
3023 * 0x426a9ac0 : mov r7, r5 |
3024 * 0x426a9ac2 : sub r7, #24 |
3025 * 0x426a9ac4 : cmp r0, #0 | genProcessArgsNoRange
3026 * 0x426a9ac6 : beq 0x426a9afe |
3027 * 0x426a9ac8 : stmia r7, <r0> --+
3028 * 0x426a9aca : ldr r4, [pc, #104] --> r4 <- dalvikPC of this invoke
3029 * 0x426a9acc : add r1, pc, #52 --> r1 <- &retChainingCell
3030 * 0x426a9ace : add r2, pc, #60 --> r2 <- &predictedChainingCell
3031 * 0x426a9ad0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_
3032 * 0x426a9ad2 : blx_2 see above --+ PREDICTED_CHAIN
3033 * 0x426a9ad4 : b 0x426a9b0c --> off to the predicted chain
3034 * 0x426a9ad6 : b 0x426a9afe --> punt to the interpreter
Ben Chenga8e64a72009-10-20 13:01:36 -07003035 * 0x426a9ad8 : mov r8, r1 --+
3036 * 0x426a9ada : mov r9, r2 |
3037 * 0x426a9adc : mov r10, r3 |
Ben Cheng38329f52009-07-07 14:19:20 -07003038 * 0x426a9ade : mov r0, r3 |
3039 * 0x426a9ae0 : mov r1, #74 | dvmFindInterfaceMethodInCache
3040 * 0x426a9ae2 : ldr r2, [pc, #76] |
3041 * 0x426a9ae4 : ldr r3, [pc, #68] |
3042 * 0x426a9ae6 : ldr r7, [pc, #64] |
3043 * 0x426a9ae8 : blx r7 --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003044 * 0x426a9aea : mov r1, r8 --> r1 <- rechain count
Ben Cheng38329f52009-07-07 14:19:20 -07003045 * 0x426a9aec : cmp r1, #0 --> compare against 0
3046 * 0x426a9aee : bgt 0x426a9af8 --> >=0? don't rechain
3047 * 0x426a9af0 : ldr r7, [r6, #96] --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003048 * 0x426a9af2 : mov r2, r9 | dvmJitToPatchPredictedChain
3049 * 0x426a9af4 : mov r3, r10 |
Ben Cheng38329f52009-07-07 14:19:20 -07003050 * 0x426a9af6 : blx r7 --+
3051 * 0x426a9af8 : add r1, pc, #8 --> r1 <- &retChainingCell
3052 * 0x426a9afa : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3053 * 0x426a9afc : blx_2 see above --+
3054 * -------- reconstruct dalvik PC : 0x428b786c @ +0x001e
3055 * 0x426a9afe (0042): ldr r0, [pc, #52]
3056 * Exception_Handling:
3057 * 0x426a9b00 (0044): ldr r1, [r6, #84]
3058 * 0x426a9b02 (0046): blx r1
3059 * 0x426a9b04 (0048): .align4
3060 * -------- chaining cell (hot): 0x0021
3061 * 0x426a9b04 (0048): ldr r0, [r6, #92]
3062 * 0x426a9b06 (004a): blx r0
3063 * 0x426a9b08 (004c): data 0x7872(30834)
3064 * 0x426a9b0a (004e): data 0x428b(17035)
3065 * 0x426a9b0c (0050): .align4
3066 * -------- chaining cell (predicted)
3067 * 0x426a9b0c (0050): data 0x0000(0) --> will be patched into bx
3068 * 0x426a9b0e (0052): data 0x0000(0)
3069 * 0x426a9b10 (0054): data 0x0000(0) --> class
3070 * 0x426a9b12 (0056): data 0x0000(0)
3071 * 0x426a9b14 (0058): data 0x0000(0) --> method
3072 * 0x426a9b16 (005a): data 0x0000(0)
3073 * 0x426a9b18 (005c): data 0x0000(0) --> reset count
3074 * 0x426a9b1a (005e): data 0x0000(0)
3075 * 0x426a9b28 (006c): .word (0xad0392a5)
3076 * 0x426a9b2c (0070): .word (0x6e750)
3077 * 0x426a9b30 (0074): .word (0x4109a618)
3078 * 0x426a9b34 (0078): .word (0x428b786c)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003079 */
3080 case OP_INVOKE_INTERFACE:
3081 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003082 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003083 int methodIndex = dInsn->vB;
3084
Bill Buzbee1465db52009-09-23 17:17:35 -07003085 /* Ensure that nothing is both live and dirty */
3086 flushAllRegs(cUnit);
3087
Ben Chengba4fc8b2009-06-01 13:00:29 -07003088 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
3089 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3090 else
3091 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3092
Ben Cheng38329f52009-07-07 14:19:20 -07003093 /* "this" is already left in r0 by genProcessArgs* */
3094
3095 /* r4PC = dalvikCallsite */
3096 loadConstant(cUnit, r4PC,
3097 (int) (cUnit->method->insns + mir->offset));
3098
3099 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003100 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003101 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003102 addrRetChain->generic.target = (LIR *) retChainingCell;
3103
3104 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003105 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003106 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003107 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3108
3109 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
3110
3111 /* return through lr - jump to the chaining cell */
3112 genUnconditionalBranch(cUnit, predChainingCell);
3113
3114 /*
3115 * null-check on "this" may have been eliminated, but we still need
3116 * a PC-reconstruction label for stack overflow bailout.
3117 */
3118 if (pcrLabel == NULL) {
3119 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003120 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003121 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07003122 pcrLabel->operands[0] = dPC;
3123 pcrLabel->operands[1] = mir->offset;
3124 /* Insert the place holder to the growable list */
3125 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3126 }
3127
3128 /* return through lr+2 - punt to the interpreter */
3129 genUnconditionalBranch(cUnit, pcrLabel);
3130
3131 /*
3132 * return through lr+4 - fully resolve the callee method.
3133 * r1 <- count
3134 * r2 <- &predictedChainCell
3135 * r3 <- this->class
3136 * r4 <- dPC
3137 * r7 <- this->class->vtable
3138 */
3139
3140 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003141 genRegCopy(cUnit, r8, r1);
3142 genRegCopy(cUnit, r9, r2);
3143 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003144
Ben Chengba4fc8b2009-06-01 13:00:29 -07003145 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003146 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003147
3148 /* r1 = BBBB */
3149 loadConstant(cUnit, r1, dInsn->vB);
3150
3151 /* r2 = method (caller) */
3152 loadConstant(cUnit, r2, (int) cUnit->method);
3153
3154 /* r3 = pDvmDex */
3155 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3156
3157 loadConstant(cUnit, r7,
3158 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003159 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003160
3161 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3162
Bill Buzbee1465db52009-09-23 17:17:35 -07003163 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003164
Ben Cheng38329f52009-07-07 14:19:20 -07003165 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07003166 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003167
Bill Buzbee1465db52009-09-23 17:17:35 -07003168 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07003169
Bill Buzbee270c1d62009-08-13 16:58:07 -07003170 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3171 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003172
Bill Buzbee1465db52009-09-23 17:17:35 -07003173 genRegCopy(cUnit, r2, r9);
3174 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003175
3176 /*
3177 * r0 = calleeMethod
3178 * r2 = &predictedChainingCell
3179 * r3 = class
3180 *
3181 * &returnChainingCell has been loaded into r1 but is not needed
3182 * when patching the chaining cell and will be clobbered upon
3183 * returning so it will be reconstructed again.
3184 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003185 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003186
3187 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003188 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003189 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003190
3191 bypassRechaining->generic.target = (LIR *) addrRetChain;
3192
Ben Chengba4fc8b2009-06-01 13:00:29 -07003193 /*
3194 * r0 = this, r1 = calleeMethod,
3195 * r1 = &ChainingCell,
3196 * r4PC = callsiteDPC,
3197 */
3198 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
3199#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07003200 gDvmJit.invokePredictedChain++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003201#endif
3202 /* Handle exceptions using the interpreter */
3203 genTrap(cUnit, mir->offset, pcrLabel);
3204 break;
3205 }
3206 /* NOP */
3207 case OP_INVOKE_DIRECT_EMPTY: {
3208 return false;
3209 }
3210 case OP_FILLED_NEW_ARRAY:
3211 case OP_FILLED_NEW_ARRAY_RANGE: {
3212 /* Just let the interpreter deal with these */
3213 genInterpSingleStep(cUnit, mir);
3214 break;
3215 }
3216 default:
3217 return true;
3218 }
3219 return false;
3220}
3221
3222static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003223 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003224{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003225 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3226 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3227 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003228
3229 DecodedInstruction *dInsn = &mir->dalvikInsn;
3230 switch (mir->dalvikInsn.opCode) {
3231 /* calleeMethod = this->clazz->vtable[BBBB] */
3232 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3233 case OP_INVOKE_VIRTUAL_QUICK: {
3234 int methodIndex = dInsn->vB;
3235 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3236 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3237 else
3238 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3239
Ben Cheng38329f52009-07-07 14:19:20 -07003240 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3241 retChainingCell,
3242 predChainingCell,
3243 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003244 break;
3245 }
3246 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3247 case OP_INVOKE_SUPER_QUICK:
3248 case OP_INVOKE_SUPER_QUICK_RANGE: {
3249 const Method *calleeMethod =
3250 cUnit->method->clazz->super->vtable[dInsn->vB];
3251
3252 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3253 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3254 else
3255 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3256
3257 /* r0 = calleeMethod */
3258 loadConstant(cUnit, r0, (int) calleeMethod);
3259
Ben Cheng38329f52009-07-07 14:19:20 -07003260 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3261 calleeMethod);
3262 /* Handle exceptions using the interpreter */
3263 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003264 break;
3265 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003266 default:
3267 return true;
3268 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003269 return false;
3270}
3271
3272/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003273 * This operation is complex enough that we'll do it partly inline
3274 * and partly with a handler. NOTE: the handler uses hardcoded
3275 * values for string object offsets and must be revisitied if the
3276 * layout changes.
3277 */
3278static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3279{
3280#if defined(USE_GLOBAL_STRING_DEFS)
3281 return false;
3282#else
3283 ArmLIR *rollback;
3284 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3285 RegLocation rlComp = getSrcLoc(cUnit, mir, 1);
3286
3287 loadValueDirectFixed(cUnit, rlThis, r0);
3288 loadValueDirectFixed(cUnit, rlComp, r1);
3289 /* Test objects for NULL */
3290 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3291 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3292 /*
3293 * TUNING: we could check for object pointer equality before invoking
3294 * handler. Unclear whether the gain would be worth the added code size
3295 * expansion.
3296 */
3297 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
3298 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3299 return true;
3300#endif
3301}
3302
3303static bool genInlinedIndexOf(CompilationUnit *cUnit, MIR *mir, bool singleI)
3304{
3305#if defined(USE_GLOBAL_STRING_DEFS)
3306 return false;
3307#else
3308 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3309 RegLocation rlChar = getSrcLoc(cUnit, mir, 1);
3310
3311 loadValueDirectFixed(cUnit, rlThis, r0);
3312 loadValueDirectFixed(cUnit, rlChar, r1);
3313 if (!singleI) {
3314 RegLocation rlStart = getSrcLoc(cUnit, mir, 2);
3315 loadValueDirectFixed(cUnit, rlStart, r2);
3316 } else {
3317 loadConstant(cUnit, r2, 0);
3318 }
3319 /* Test objects for NULL */
3320 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3321 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
3322 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3323 return true;
3324#endif
3325}
3326
3327
3328/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003329 * NOTE: Handles both range and non-range versions (arguments
3330 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003331 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003332static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003333{
3334 DecodedInstruction *dInsn = &mir->dalvikInsn;
3335 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003336 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003337 case OP_EXECUTE_INLINE: {
3338 unsigned int i;
3339 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003340 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003341 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003342 int tReg1;
3343 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003344 switch (operation) {
3345 case INLINE_EMPTYINLINEMETHOD:
3346 return false; /* Nop */
3347 case INLINE_STRING_LENGTH:
3348 return genInlinedStringLength(cUnit, mir);
3349 case INLINE_MATH_ABS_INT:
3350 return genInlinedAbsInt(cUnit, mir);
3351 case INLINE_MATH_ABS_LONG:
3352 return genInlinedAbsLong(cUnit, mir);
3353 case INLINE_MATH_MIN_INT:
3354 return genInlinedMinMaxInt(cUnit, mir, true);
3355 case INLINE_MATH_MAX_INT:
3356 return genInlinedMinMaxInt(cUnit, mir, false);
3357 case INLINE_STRING_CHARAT:
3358 return genInlinedStringCharAt(cUnit, mir);
3359 case INLINE_MATH_SQRT:
3360 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003361 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003362 else
3363 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003364 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003365 if (genInlinedAbsFloat(cUnit, mir))
3366 return false;
3367 else
3368 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003369 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003370 if (genInlinedAbsDouble(cUnit, mir))
3371 return false;
3372 else
3373 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003374 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003375 if (genInlinedCompareTo(cUnit, mir))
3376 return false;
3377 else
3378 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003379 case INLINE_STRING_INDEXOF_I:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003380 if (genInlinedIndexOf(cUnit, mir, true /* I */))
3381 return false;
3382 else
3383 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003384 case INLINE_STRING_INDEXOF_II:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003385 if (genInlinedIndexOf(cUnit, mir, false /* I */))
3386 return false;
3387 else
3388 break;
3389 case INLINE_STRING_EQUALS:
3390 case INLINE_MATH_COS:
3391 case INLINE_MATH_SIN:
3392 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003393 default:
3394 dvmAbort();
Ben Chengba4fc8b2009-06-01 13:00:29 -07003395 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003396 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07003397 clobberCallRegs(cUnit);
3398 clobberReg(cUnit, r4PC);
3399 clobberReg(cUnit, r7);
3400 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3401 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003402 loadConstant(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003403 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003404 for (i=0; i < dInsn->vA; i++) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003405 loadValueDirect(cUnit, getSrcLoc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003406 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003407 opReg(cUnit, kOpBlx, r4PC);
3408 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003409 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3410 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3411 loadConstant(cUnit, r0,
3412 (int) (cUnit->method->insns + mir->offset));
3413 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3414 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3415 target->defMask = ENCODE_ALL;
3416 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003417 break;
3418 }
3419 default:
3420 return true;
3421 }
3422 return false;
3423}
3424
3425static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3426{
Bill Buzbee1465db52009-09-23 17:17:35 -07003427 //TUNING: We're using core regs here - not optimal when target is a double
3428 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
3429 RegLocation rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
3430 loadConstantValue(cUnit, rlResult.lowReg,
3431 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3432 loadConstantValue(cUnit, rlResult.highReg,
3433 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
3434 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003435 return false;
3436}
3437
Ben Chengba4fc8b2009-06-01 13:00:29 -07003438/*
3439 * The following are special processing routines that handle transfer of
3440 * controls between compiled code and the interpreter. Certain VM states like
3441 * Dalvik PC and special-purpose registers are reconstructed here.
3442 */
3443
Ben Cheng1efc9c52009-06-08 18:25:27 -07003444/* Chaining cell for code that may need warmup. */
3445static void handleNormalChainingCell(CompilationUnit *cUnit,
3446 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003447{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003448 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3449 jitToInterpEntries.dvmJitToInterpNormal), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003450 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003451 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3452}
3453
3454/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003455 * Chaining cell for instructions that immediately following already translated
3456 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003457 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003458static void handleHotChainingCell(CompilationUnit *cUnit,
3459 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003460{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003461 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3462 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003463 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003464 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3465}
3466
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003467#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003468/* Chaining cell for branches that branch back into the same basic block */
3469static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3470 unsigned int offset)
3471{
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003472#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003473 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Jeff Hao97319a82009-08-12 16:57:15 -07003474 offsetof(InterpState, jitToInterpEntries.dvmJitToBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003475#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003476 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003477 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3478#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003479 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003480 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3481}
3482
3483#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003484/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003485static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3486 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003487{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003488 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3489 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003490 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003491 addWordData(cUnit, (int) (callee->insns), true);
3492}
3493
Ben Cheng38329f52009-07-07 14:19:20 -07003494/* Chaining cell for monomorphic method invocations. */
3495static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3496{
3497
3498 /* Should not be executed in the initial state */
3499 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3500 /* To be filled: class */
3501 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3502 /* To be filled: method */
3503 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3504 /*
3505 * Rechain count. The initial value of 0 here will trigger chaining upon
3506 * the first invocation of this callsite.
3507 */
3508 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3509}
3510
Ben Chengba4fc8b2009-06-01 13:00:29 -07003511/* Load the Dalvik PC into r0 and jump to the specified target */
3512static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003513 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003514{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003515 ArmLIR **pcrLabel =
3516 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003517 int numElems = cUnit->pcReconstructionList.numUsed;
3518 int i;
3519 for (i = 0; i < numElems; i++) {
3520 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3521 /* r0 = dalvik PC */
3522 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3523 genUnconditionalBranch(cUnit, targetLabel);
3524 }
3525}
3526
Bill Buzbee1465db52009-09-23 17:17:35 -07003527static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3528 "kMirOpPhi",
3529 "kMirOpNullNRangeUpCheck",
3530 "kMirOpNullNRangeDownCheck",
3531 "kMirOpLowerBound",
3532 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003533};
3534
3535/*
3536 * vA = arrayReg;
3537 * vB = idxReg;
3538 * vC = endConditionReg;
3539 * arg[0] = maxC
3540 * arg[1] = minC
3541 * arg[2] = loopBranchConditionCode
3542 */
3543static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3544{
Bill Buzbee1465db52009-09-23 17:17:35 -07003545 /*
3546 * NOTE: these synthesized blocks don't have ssa names assigned
3547 * for Dalvik registers. However, because they dominate the following
3548 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3549 * ssa name.
3550 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003551 DecodedInstruction *dInsn = &mir->dalvikInsn;
3552 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003553 const int maxC = dInsn->arg[0];
3554 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003555 int regLength;
3556 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3557 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003558
3559 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003560 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3561 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3562 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003563 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3564
3565 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003566 regLength = allocTemp(cUnit);
3567 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003568
3569 int delta = maxC;
3570 /*
3571 * If the loop end condition is ">=" instead of ">", then the largest value
3572 * of the index is "endCondition - 1".
3573 */
3574 if (dInsn->arg[2] == OP_IF_GE) {
3575 delta--;
3576 }
3577
3578 if (delta) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003579 int tReg = allocTemp(cUnit);
3580 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3581 rlIdxEnd.lowReg = tReg;
3582 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003583 }
3584 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003585 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003586 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003587}
3588
3589/*
3590 * vA = arrayReg;
3591 * vB = idxReg;
3592 * vC = endConditionReg;
3593 * arg[0] = maxC
3594 * arg[1] = minC
3595 * arg[2] = loopBranchConditionCode
3596 */
3597static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3598{
3599 DecodedInstruction *dInsn = &mir->dalvikInsn;
3600 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07003601 const int regLength = allocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003602 const int maxC = dInsn->arg[0];
3603 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003604 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3605 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003606
3607 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003608 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3609 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3610 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003611 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3612
3613 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003614 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003615
3616 if (maxC) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003617 int tReg = allocTemp(cUnit);
3618 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3619 rlIdxInit.lowReg = tReg;
3620 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003621 }
3622
3623 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003624 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003625 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003626}
3627
3628/*
3629 * vA = idxReg;
3630 * vB = minC;
3631 */
3632static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3633{
3634 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003635 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003636 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003637
3638 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003639 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003640
3641 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003642 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003643 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3644}
3645
3646/* Extended MIR instructions like PHI */
3647static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3648{
Bill Buzbee1465db52009-09-23 17:17:35 -07003649 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003650 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3651 false);
3652 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003653 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003654
3655 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003656 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003657 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003658 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003659 break;
3660 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003661 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003662 genHoistedChecksForCountUpLoop(cUnit, mir);
3663 break;
3664 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003665 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003666 genHoistedChecksForCountDownLoop(cUnit, mir);
3667 break;
3668 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003669 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003670 genHoistedLowerBoundCheck(cUnit, mir);
3671 break;
3672 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003673 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003674 genUnconditionalBranch(cUnit,
3675 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3676 break;
3677 }
3678 default:
3679 break;
3680 }
3681}
3682
3683/*
3684 * Create a PC-reconstruction cell for the starting offset of this trace.
3685 * Since the PCR cell is placed near the end of the compiled code which is
3686 * usually out of range for a conditional branch, we put two branches (one
3687 * branch over to the loop body and one layover branch to the actual PCR) at the
3688 * end of the entry block.
3689 */
3690static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3691 ArmLIR *bodyLabel)
3692{
3693 /* Set up the place holder to reconstruct this Dalvik PC */
3694 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003695 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng4238ec22009-08-24 16:32:22 -07003696 pcrLabel->operands[0] =
3697 (int) (cUnit->method->insns + entry->startOffset);
3698 pcrLabel->operands[1] = entry->startOffset;
3699 /* Insert the place holder to the growable list */
3700 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3701
3702 /*
3703 * Next, create two branches - one branch over to the loop body and the
3704 * other branch to the PCR cell to punt.
3705 */
3706 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003707 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003708 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003709 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003710 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3711
3712 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003713 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003714 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003715 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003716 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3717}
3718
Ben Chengba4fc8b2009-06-01 13:00:29 -07003719void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3720{
3721 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003722 ArmLIR *labelList =
3723 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003724 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003725 int i;
3726
3727 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003728 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003729 */
Ben Chengcec26f62010-01-15 15:29:33 -08003730 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003731 dvmInitGrowableList(&chainingListByType[i], 2);
3732 }
3733
3734 BasicBlock **blockList = cUnit->blockList;
3735
Bill Buzbee6e963e12009-06-17 16:56:19 -07003736 if (cUnit->executionCount) {
3737 /*
3738 * Reserve 6 bytes at the beginning of the trace
3739 * +----------------------------+
3740 * | execution count (4 bytes) |
3741 * +----------------------------+
3742 * | chain cell offset (2 bytes)|
3743 * +----------------------------+
3744 * ...and then code to increment the execution
3745 * count:
3746 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3747 * sub r0, #10 @ back up to addr of executionCount
3748 * ldr r1, [r0]
3749 * add r1, #1
3750 * str r1, [r0]
3751 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003752 newLIR1(cUnit, kArm16BitData, 0);
3753 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003754 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003755 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003756 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003757 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003758 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3759 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3760 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3761 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3762 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003763 } else {
3764 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003765 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003766 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003767 cUnit->headerSize = 2;
3768 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003769
Ben Chengba4fc8b2009-06-01 13:00:29 -07003770 /* Handle the content in each basic block */
3771 for (i = 0; i < cUnit->numBlocks; i++) {
3772 blockList[i]->visited = true;
3773 MIR *mir;
3774
3775 labelList[i].operands[0] = blockList[i]->startOffset;
3776
Ben Chengcec26f62010-01-15 15:29:33 -08003777 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003778 /*
3779 * Append the label pseudo LIR first. Chaining cells will be handled
3780 * separately afterwards.
3781 */
3782 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3783 }
3784
Bill Buzbee1465db52009-09-23 17:17:35 -07003785 if (blockList[i]->blockType == kEntryBlock) {
3786 labelList[i].opCode = ARM_PSEUDO_kEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003787 if (blockList[i]->firstMIRInsn == NULL) {
3788 continue;
3789 } else {
3790 setupLoopEntryBlock(cUnit, blockList[i],
3791 &labelList[blockList[i]->fallThrough->id]);
3792 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003793 } else if (blockList[i]->blockType == kExitBlock) {
3794 labelList[i].opCode = ARM_PSEUDO_kExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003795 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003796 } else if (blockList[i]->blockType == kDalvikByteCode) {
3797 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003798 /* Reset the register state */
Bill Buzbee1465db52009-09-23 17:17:35 -07003799 resetRegPool(cUnit);
3800 clobberAllRegs(cUnit);
3801 resetNullCheckTracker(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003802 } else {
3803 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003804 case kChainingCellNormal:
3805 labelList[i].opCode = ARM_PSEUDO_kChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003806 /* handle the codegen later */
3807 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003808 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003809 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003810 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003811 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003812 ARM_PSEUDO_kChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003813 labelList[i].operands[0] =
3814 (int) blockList[i]->containingMethod;
3815 /* handle the codegen later */
3816 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003817 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003818 (void *) i);
3819 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003820 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003821 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003822 ARM_PSEUDO_kChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003823 /* handle the codegen later */
3824 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003825 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003826 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003827 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003828 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003829 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003830 ARM_PSEUDO_kChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003831 /* handle the codegen later */
3832 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003833 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003834 (void *) i);
3835 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003836 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003837 /* Make sure exception handling block is next */
3838 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003839 ARM_PSEUDO_kPCReconstruction_BLOCK_LABEL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003840 assert (i == cUnit->numBlocks - 2);
3841 handlePCReconstruction(cUnit, &labelList[i+1]);
3842 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003843 case kExceptionHandling:
3844 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003845 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003846 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3847 jitToInterpEntries.dvmJitToInterpPunt),
3848 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003849 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003850 }
3851 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003852#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003853 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003854 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003855 ARM_PSEUDO_kChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003856 /* handle the codegen later */
3857 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003858 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003859 (void *) i);
3860 break;
3861#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003862 default:
3863 break;
3864 }
3865 continue;
3866 }
Ben Chenge9695e52009-06-16 16:11:47 -07003867
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003868 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003869
Ben Chengba4fc8b2009-06-01 13:00:29 -07003870 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003871
3872 resetRegPool(cUnit);
3873 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
3874 clobberAllRegs(cUnit);
3875 }
3876
3877 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
3878 resetDefTracking(cUnit);
3879 }
3880
3881 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003882 handleExtendedMIR(cUnit, mir);
3883 continue;
3884 }
3885
Bill Buzbee1465db52009-09-23 17:17:35 -07003886
Ben Chengba4fc8b2009-06-01 13:00:29 -07003887 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3888 InstructionFormat dalvikFormat =
3889 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003890 ArmLIR *boundaryLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003891 newLIR2(cUnit, ARM_PSEUDO_kDalvikByteCode_BOUNDARY,
Ben Chengccd6c012009-10-15 14:52:45 -07003892 mir->offset,
3893 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3894 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003895 if (mir->ssaRep) {
3896 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003897 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003898 }
3899
Ben Chenge9695e52009-06-16 16:11:47 -07003900 /* Remember the first LIR for this block */
3901 if (headLIR == NULL) {
3902 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003903 /* Set the first boundaryLIR as a scheduling barrier */
3904 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003905 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003906
Ben Chengba4fc8b2009-06-01 13:00:29 -07003907 bool notHandled;
3908 /*
3909 * Debugging: screen the opcode first to see if it is in the
3910 * do[-not]-compile list
3911 */
3912 bool singleStepMe =
3913 gDvmJit.includeSelectedOp !=
3914 ((gDvmJit.opList[dalvikOpCode >> 3] &
3915 (1 << (dalvikOpCode & 0x7))) !=
3916 0);
3917 if (singleStepMe || cUnit->allSingleStep) {
3918 notHandled = false;
3919 genInterpSingleStep(cUnit, mir);
3920 } else {
3921 opcodeCoverage[dalvikOpCode]++;
3922 switch (dalvikFormat) {
3923 case kFmt10t:
3924 case kFmt20t:
3925 case kFmt30t:
3926 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3927 mir, blockList[i], labelList);
3928 break;
3929 case kFmt10x:
3930 notHandled = handleFmt10x(cUnit, mir);
3931 break;
3932 case kFmt11n:
3933 case kFmt31i:
3934 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3935 break;
3936 case kFmt11x:
3937 notHandled = handleFmt11x(cUnit, mir);
3938 break;
3939 case kFmt12x:
3940 notHandled = handleFmt12x(cUnit, mir);
3941 break;
3942 case kFmt20bc:
3943 notHandled = handleFmt20bc(cUnit, mir);
3944 break;
3945 case kFmt21c:
3946 case kFmt31c:
3947 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3948 break;
3949 case kFmt21h:
3950 notHandled = handleFmt21h(cUnit, mir);
3951 break;
3952 case kFmt21s:
3953 notHandled = handleFmt21s(cUnit, mir);
3954 break;
3955 case kFmt21t:
3956 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3957 labelList);
3958 break;
3959 case kFmt22b:
3960 case kFmt22s:
3961 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3962 break;
3963 case kFmt22c:
3964 notHandled = handleFmt22c(cUnit, mir);
3965 break;
3966 case kFmt22cs:
3967 notHandled = handleFmt22cs(cUnit, mir);
3968 break;
3969 case kFmt22t:
3970 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3971 labelList);
3972 break;
3973 case kFmt22x:
3974 case kFmt32x:
3975 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3976 break;
3977 case kFmt23x:
3978 notHandled = handleFmt23x(cUnit, mir);
3979 break;
3980 case kFmt31t:
3981 notHandled = handleFmt31t(cUnit, mir);
3982 break;
3983 case kFmt3rc:
3984 case kFmt35c:
3985 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3986 labelList);
3987 break;
3988 case kFmt3rms:
3989 case kFmt35ms:
3990 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3991 labelList);
3992 break;
3993 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003994 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003995 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003996 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003997 case kFmt51l:
3998 notHandled = handleFmt51l(cUnit, mir);
3999 break;
4000 default:
4001 notHandled = true;
4002 break;
4003 }
4004 }
4005 if (notHandled) {
4006 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4007 mir->offset,
4008 dalvikOpCode, getOpcodeName(dalvikOpCode),
4009 dalvikFormat);
4010 dvmAbort();
4011 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004012 }
4013 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004014
Bill Buzbee1465db52009-09-23 17:17:35 -07004015 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004016 dvmCompilerAppendLIR(cUnit,
4017 (LIR *) cUnit->loopAnalysis->branchToBody);
4018 dvmCompilerAppendLIR(cUnit,
4019 (LIR *) cUnit->loopAnalysis->branchToPCR);
4020 }
4021
4022 if (headLIR) {
4023 /*
4024 * Eliminate redundant loads/stores and delay stores into later
4025 * slots
4026 */
4027 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4028 cUnit->lastLIRInsn);
4029 }
4030
4031gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004032 /*
4033 * Check if the block is terminated due to trace length constraint -
4034 * insert an unconditional branch to the chaining cell.
4035 */
4036 if (blockList[i]->needFallThroughBranch) {
4037 genUnconditionalBranch(cUnit,
4038 &labelList[blockList[i]->fallThrough->id]);
4039 }
4040
Ben Chengba4fc8b2009-06-01 13:00:29 -07004041 }
4042
Ben Chenge9695e52009-06-16 16:11:47 -07004043 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004044 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004045 size_t j;
4046 int *blockIdList = (int *) chainingListByType[i].elemList;
4047
4048 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4049
4050 /* No chaining cells of this type */
4051 if (cUnit->numChainingCells[i] == 0)
4052 continue;
4053
4054 /* Record the first LIR for a new type of chaining cell */
4055 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4056
4057 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4058 int blockId = blockIdList[j];
4059
4060 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004061 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004062
4063 /* Insert the pseudo chaining instruction */
4064 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4065
4066
4067 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004068 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004069 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004070 blockList[blockId]->startOffset);
4071 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004072 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004073 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004074 blockList[blockId]->containingMethod);
4075 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004076 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004077 handleInvokePredictedChainingCell(cUnit);
4078 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004079 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004080 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004081 blockList[blockId]->startOffset);
4082 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004083#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004084 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004085 handleBackwardBranchChainingCell(cUnit,
4086 blockList[blockId]->startOffset);
4087 break;
4088#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004089 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004090 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004091 dvmAbort();
4092 break;
4093 }
4094 }
4095 }
Ben Chenge9695e52009-06-16 16:11:47 -07004096
Ben Chengcec26f62010-01-15 15:29:33 -08004097 /* Mark the bottom of chaining cells */
4098 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4099
Ben Cheng6c10a972009-10-29 14:39:18 -07004100 /*
4101 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4102 * of all chaining cells for the overflow cases.
4103 */
4104 if (cUnit->switchOverflowPad) {
4105 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4106 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4107 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4108 opRegReg(cUnit, kOpAdd, r1, r1);
4109 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
4110#if defined(EXIT_STATS)
4111 loadConstant(cUnit, r0, kSwitchOverflow);
4112#endif
4113 opReg(cUnit, kOpBlx, r2);
4114 }
4115
Ben Chenge9695e52009-06-16 16:11:47 -07004116 dvmCompilerApplyGlobalOptimizations(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004117}
4118
4119/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004120bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004121{
Ben Chengccd6c012009-10-15 14:52:45 -07004122 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004123
Ben Cheng6999d842010-01-26 16:46:15 -08004124 if (gDvmJit.codeCacheFull) {
Ben Chengccd6c012009-10-15 14:52:45 -07004125 return false;
4126 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004127
Ben Chengccd6c012009-10-15 14:52:45 -07004128 switch (work->kind) {
4129 case kWorkOrderMethod:
4130 res = dvmCompileMethod(work->info, &work->result);
4131 break;
4132 case kWorkOrderTrace:
4133 /* Start compilation with maximally allowed trace length */
4134 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4135 break;
4136 case kWorkOrderTraceDebug: {
4137 bool oldPrintMe = gDvmJit.printMe;
4138 gDvmJit.printMe = true;
4139 /* Start compilation with maximally allowed trace length */
4140 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4141 gDvmJit.printMe = oldPrintMe;;
4142 break;
4143 }
4144 default:
4145 res = false;
4146 dvmAbort();
4147 }
4148 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004149}
4150
Ben Chengba4fc8b2009-06-01 13:00:29 -07004151/* Architectural-specific debugging helpers go here */
4152void dvmCompilerArchDump(void)
4153{
4154 /* Print compiled opcode in this VM instance */
4155 int i, start, streak;
4156 char buf[1024];
4157
4158 streak = i = 0;
4159 buf[0] = 0;
4160 while (opcodeCoverage[i] == 0 && i < 256) {
4161 i++;
4162 }
4163 if (i == 256) {
4164 return;
4165 }
4166 for (start = i++, streak = 1; i < 256; i++) {
4167 if (opcodeCoverage[i]) {
4168 streak++;
4169 } else {
4170 if (streak == 1) {
4171 sprintf(buf+strlen(buf), "%x,", start);
4172 } else {
4173 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4174 }
4175 streak = 0;
4176 while (opcodeCoverage[i] == 0 && i < 256) {
4177 i++;
4178 }
4179 if (i < 256) {
4180 streak = 1;
4181 start = i;
4182 }
4183 }
4184 }
4185 if (streak) {
4186 if (streak == 1) {
4187 sprintf(buf+strlen(buf), "%x", start);
4188 } else {
4189 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4190 }
4191 }
4192 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004193 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004194 }
4195}
Ben Chengd7d426a2009-09-22 11:23:36 -07004196
4197/* Common initialization routine for an architecture family */
4198bool dvmCompilerArchInit()
4199{
4200 int i;
4201
Bill Buzbee1465db52009-09-23 17:17:35 -07004202 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004203 if (EncodingMap[i].opCode != i) {
4204 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4205 EncodingMap[i].name, i, EncodingMap[i].opCode);
4206 dvmAbort();
4207 }
4208 }
4209
Ben Cheng5d90c202009-11-22 23:31:11 -08004210 return dvmCompilerArchVariantInit();
4211}
4212
4213void *dvmCompilerGetInterpretTemplate()
4214{
4215 return (void*) ((int)gDvmJit.codeCache +
4216 templateEntryOffsets[TEMPLATE_INTERPRET]);
4217}
4218
4219/* Needed by the ld/st optmizatons */
4220ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4221{
4222 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4223}
4224
4225/* Needed by the register allocator */
4226ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4227{
4228 return genRegCopy(cUnit, rDest, rSrc);
4229}
4230
4231/* Needed by the register allocator */
4232void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4233 int srcLo, int srcHi)
4234{
4235 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4236}
4237
4238void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4239 int displacement, int rSrc, OpSize size)
4240{
4241 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4242}
4243
4244void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4245 int displacement, int rSrcLo, int rSrcHi)
4246{
4247 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004248}