blob: d6cb5d6c105b593147b17d32c25121b3c270bb03 [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
Ben Cheng5d90c202009-11-22 23:31:11 -08001696static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001697{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001698 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001699 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001700 genExportPC(cUnit, mir);
1701 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
1702 loadValueDirectFixed(cUnit, rlSrc, r1);
1703 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001704 if (isEnter) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001705 loadConstant(cUnit, r2, (int)dvmLockObject);
1706 } else {
1707 loadConstant(cUnit, r2, (int)dvmUnlockObject);
1708 }
1709 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
1710 /* Do the call */
1711 opReg(cUnit, kOpBlx, r2);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001712#if defined(WITH_DEADLOCK_PREDICTION)
1713 if (isEnter) {
1714 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
1715 loadWordDisp(cUnit, r0, offsetof(Thread, exception), r1);
1716 opRegImm(cUnit, kOpCmp, r1, 0);
1717 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
1718 loadConstant(cUnit, r0,
1719 (int) (cUnit->method->insns + mir->offset));
1720 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1721 /* noreturn */
1722 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1723 target->defMask = ENCODE_ALL;
1724 branchOver->generic.target = (LIR *) target;
1725 }
1726#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001727 clobberCallRegs(cUnit);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001728}
1729
Ben Chengba4fc8b2009-06-01 13:00:29 -07001730/*
1731 * The following are the first-level codegen routines that analyze the format
1732 * of each bytecode then either dispatch special purpose codegen routines
1733 * or produce corresponding Thumb instructions directly.
1734 */
1735
1736static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001737 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001738{
1739 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1740 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1741 return false;
1742}
1743
1744static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1745{
1746 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1747 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden96516932009-10-28 17:39:02 -07001748 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_EB))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001749 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1750 return true;
1751 }
1752 switch (dalvikOpCode) {
1753 case OP_RETURN_VOID:
1754 genReturnCommon(cUnit,mir);
1755 break;
1756 case OP_UNUSED_73:
1757 case OP_UNUSED_79:
1758 case OP_UNUSED_7A:
1759 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1760 return true;
1761 case OP_NOP:
1762 break;
1763 default:
1764 return true;
1765 }
1766 return false;
1767}
1768
1769static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1770{
Bill Buzbee1465db52009-09-23 17:17:35 -07001771 RegLocation rlDest;
1772 RegLocation rlResult;
1773 if (mir->ssaRep->numDefs == 2) {
1774 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1775 } else {
1776 rlDest = getDestLoc(cUnit, mir, 0);
1777 }
Ben Chenge9695e52009-06-16 16:11:47 -07001778
Ben Chengba4fc8b2009-06-01 13:00:29 -07001779 switch (mir->dalvikInsn.opCode) {
1780 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001781 case OP_CONST_4: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001782 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1783 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1784 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001785 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001786 }
1787 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001788 //TUNING: single routine to load constant pair for support doubles
1789 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1790 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1791 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1792 rlResult.lowReg, 31);
1793 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001794 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001795 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001796 default:
1797 return true;
1798 }
1799 return false;
1800}
1801
1802static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1803{
Bill Buzbee1465db52009-09-23 17:17:35 -07001804 RegLocation rlDest;
1805 RegLocation rlResult;
1806 if (mir->ssaRep->numDefs == 2) {
1807 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1808 } else {
1809 rlDest = getDestLoc(cUnit, mir, 0);
1810 }
1811 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001812
Ben Chengba4fc8b2009-06-01 13:00:29 -07001813 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001814 case OP_CONST_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001815 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB << 16);
1816 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001817 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001818 }
1819 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001820 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1821 0, mir->dalvikInsn.vB << 16);
1822 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001823 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001824 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001825 default:
1826 return true;
1827 }
1828 return false;
1829}
1830
1831static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1832{
1833 /* For OP_THROW_VERIFICATION_ERROR */
1834 genInterpSingleStep(cUnit, mir);
1835 return false;
1836}
1837
1838static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1839{
Bill Buzbee1465db52009-09-23 17:17:35 -07001840 RegLocation rlResult;
1841 RegLocation rlDest;
1842 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001843
Ben Chengba4fc8b2009-06-01 13:00:29 -07001844 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001845 case OP_CONST_STRING_JUMBO:
1846 case OP_CONST_STRING: {
1847 void *strPtr = (void*)
1848 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
1849 assert(strPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001850 rlDest = getDestLoc(cUnit, mir, 0);
1851 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1852 loadConstantValue(cUnit, rlResult.lowReg, (int) strPtr );
1853 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 break;
1855 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001856 case OP_CONST_CLASS: {
1857 void *classPtr = (void*)
1858 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1859 assert(classPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001860 rlDest = getDestLoc(cUnit, mir, 0);
1861 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1862 loadConstantValue(cUnit, rlResult.lowReg, (int) classPtr );
1863 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001864 break;
1865 }
1866 case OP_SGET_OBJECT:
1867 case OP_SGET_BOOLEAN:
1868 case OP_SGET_CHAR:
1869 case OP_SGET_BYTE:
1870 case OP_SGET_SHORT:
1871 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001872 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001873 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001874 void *fieldPtr = (void*)
1875 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
1876 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001877 rlDest = getDestLoc(cUnit, mir, 0);
1878 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1879 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001880#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001881 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001882#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001883 int regMap = rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001884 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationLoad);
1885
Jeff Hao97319a82009-08-12 16:57:15 -07001886#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001887 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001888 break;
1889 }
1890 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001891 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001892 void *fieldPtr = (void*)
1893 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Bill Buzbee1465db52009-09-23 17:17:35 -07001894 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001895 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001896 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1897 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1898 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001899#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001900 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001901#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001902 int regMap = rlResult.highReg << 16 |
1903 rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001904 selfVerificationMemOpWrapper(cUnit, regMap,
1905 &selfVerificationLoadDoubleword);
1906
Jeff Hao97319a82009-08-12 16:57:15 -07001907#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001908 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001909 break;
1910 }
1911 case OP_SPUT_OBJECT:
1912 case OP_SPUT_BOOLEAN:
1913 case OP_SPUT_CHAR:
1914 case OP_SPUT_BYTE:
1915 case OP_SPUT_SHORT:
1916 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001917 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001918 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001919 void *fieldPtr = (void*)
1920 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001921
Ben Chengba4fc8b2009-06-01 13:00:29 -07001922 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001923 rlSrc = getSrcLoc(cUnit, mir, 0);
1924 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1925 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001926#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001927 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001928#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001929 int regMap = rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001930 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationStore);
1931#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001932 break;
1933 }
1934 case OP_SPUT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001935 int tReg = allocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001936 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001937 void *fieldPtr = (void*)
1938 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001939
Ben Chengba4fc8b2009-06-01 13:00:29 -07001940 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001941 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
1942 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1943 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001944#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001945 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001946#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001947 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001948 selfVerificationMemOpWrapper(cUnit, regMap,
1949 &selfVerificationStoreDoubleword);
1950#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001951 break;
1952 }
1953 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001954 /*
1955 * Obey the calling convention and don't mess with the register
1956 * usage.
1957 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001958 ClassObject *classPtr = (void*)
1959 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1960 assert(classPtr != NULL);
1961 assert(classPtr->status & CLASS_INITIALIZED);
Ben Cheng79d173c2009-09-29 16:12:51 -07001962 /*
1963 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001964 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001965 */
1966 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001967 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001968 genExportPC(cUnit, mir);
1969 loadConstant(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001970 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001971 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001972 opReg(cUnit, kOpBlx, r2);
1973 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001974 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001975 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1976 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001977 /*
1978 * OOM exception needs to be thrown here and cannot re-execute
1979 */
1980 loadConstant(cUnit, r0,
1981 (int) (cUnit->method->insns + mir->offset));
1982 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1983 /* noreturn */
1984
Bill Buzbee1465db52009-09-23 17:17:35 -07001985 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001986 target->defMask = ENCODE_ALL;
1987 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07001988 rlDest = getDestLoc(cUnit, mir, 0);
1989 rlResult = getReturnLoc(cUnit);
1990 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001991 break;
1992 }
1993 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001994 /*
1995 * Obey the calling convention and don't mess with the register
1996 * usage.
1997 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001998 ClassObject *classPtr =
1999 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08002000 /*
2001 * Note: It is possible that classPtr is NULL at this point,
2002 * even though this instruction has been successfully interpreted.
2003 * If the previous interpretation had a null source, the
2004 * interpreter would not have bothered to resolve the clazz.
2005 * Bail out to the interpreter in this case, and log it
2006 * so that we can tell if it happens frequently.
2007 */
2008 if (classPtr == NULL) {
2009 LOGD("null clazz in OP_CHECK_CAST, single-stepping");
2010 genInterpSingleStep(cUnit, mir);
2011 return false;
2012 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002013 flushAllRegs(cUnit); /* Send everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002014 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002015 rlSrc = getSrcLoc(cUnit, mir, 0);
2016 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2017 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
2018 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
2019 /*
2020 * rlSrc.lowReg now contains object->clazz. Note that
2021 * it could have been allocated r0, but we're okay so long
2022 * as we don't do anything desctructive until r0 is loaded
2023 * with clazz.
2024 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002025 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002026 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
2027 loadConstant(cUnit, r2, (int)dvmInstanceofNonTrivial);
2028 opRegReg(cUnit, kOpCmp, r0, r1);
2029 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2030 opReg(cUnit, kOpBlx, r2);
2031 clobberCallRegs(cUnit);
2032 /*
2033 * If null, check cast failed - punt to the interpreter. Because
2034 * interpreter will be the one throwing, we don't need to
2035 * genExportPC() here.
2036 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002037 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002038 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002039 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002040 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002041 branch1->generic.target = (LIR *)target;
2042 branch2->generic.target = (LIR *)target;
2043 break;
2044 }
2045 default:
2046 return true;
2047 }
2048 return false;
2049}
2050
2051static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
2052{
2053 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002054 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002055 switch (dalvikOpCode) {
2056 case OP_MOVE_EXCEPTION: {
2057 int offset = offsetof(InterpState, self);
2058 int exOffset = offsetof(Thread, exception);
Bill Buzbee1465db52009-09-23 17:17:35 -07002059 int selfReg = allocTemp(cUnit);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002060 int resetReg = allocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002061 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2062 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2063 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002064 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002065 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002066 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07002067 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002068 break;
2069 }
2070 case OP_MOVE_RESULT:
2071 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002072 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2073 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
2074 rlSrc.fp = rlDest.fp;
2075 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002076 break;
2077 }
2078 case OP_MOVE_RESULT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002079 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
2080 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
2081 rlSrc.fp = rlDest.fp;
2082 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002083 break;
2084 }
2085 case OP_RETURN_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002086 RegLocation rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2087 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
2088 rlDest.fp = rlSrc.fp;
2089 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002090 genReturnCommon(cUnit,mir);
2091 break;
2092 }
2093 case OP_RETURN:
2094 case OP_RETURN_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002095 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2096 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
2097 rlDest.fp = rlSrc.fp;
2098 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002099 genReturnCommon(cUnit,mir);
2100 break;
2101 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002102 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002103 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08002104#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08002105 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002106#else
Ben Cheng5d90c202009-11-22 23:31:11 -08002107 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002108#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07002109 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002110 case OP_THROW: {
2111 genInterpSingleStep(cUnit, mir);
2112 break;
2113 }
2114 default:
2115 return true;
2116 }
2117 return false;
2118}
2119
Bill Buzbeed45ba372009-06-15 17:00:57 -07002120static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
2121{
2122 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002123 RegLocation rlDest;
2124 RegLocation rlSrc;
2125 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07002126
Ben Chengba4fc8b2009-06-01 13:00:29 -07002127 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002128 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002129 }
2130
Bill Buzbee1465db52009-09-23 17:17:35 -07002131 if (mir->ssaRep->numUses == 2)
2132 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2133 else
2134 rlSrc = getSrcLoc(cUnit, mir, 0);
2135 if (mir->ssaRep->numDefs == 2)
2136 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2137 else
2138 rlDest = getDestLoc(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07002139
Ben Chengba4fc8b2009-06-01 13:00:29 -07002140 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002141 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002142 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002143 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002144 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002145 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002146 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002147 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002148 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002149 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002150 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002151 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152 case OP_NEG_INT:
2153 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002154 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002155 case OP_NEG_LONG:
2156 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08002157 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002158 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002159 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002160 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002161 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002162 case OP_MOVE_WIDE:
2163 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002164 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002165 case OP_INT_TO_LONG:
2166 rlSrc = updateLoc(cUnit, rlSrc);
2167 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2168 if (rlSrc.location == kLocPhysReg) {
2169 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2170 } else {
2171 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2172 }
2173 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2174 rlResult.lowReg, 31);
2175 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002176 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002177 case OP_LONG_TO_INT:
2178 rlSrc = updateLocWide(cUnit, rlSrc);
2179 rlSrc = wideToNarrowLoc(cUnit, rlSrc);
2180 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002181 case OP_MOVE:
2182 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002183 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002184 break;
2185 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002186 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2187 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2188 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2189 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002190 break;
2191 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002192 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2193 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2194 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2195 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002196 break;
2197 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002198 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2199 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2200 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2201 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002202 break;
2203 case OP_ARRAY_LENGTH: {
2204 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002205 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2206 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2207 mir->offset, NULL);
2208 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2209 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2210 rlResult.lowReg);
2211 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002212 break;
2213 }
2214 default:
2215 return true;
2216 }
2217 return false;
2218}
2219
2220static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2221{
2222 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002223 RegLocation rlDest;
2224 RegLocation rlResult;
2225 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002226 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002227 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2228 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2229 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
2230 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2231 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002232 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002233 rlDest = getDestLoc(cUnit, mir, 0);
2234 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
2235 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
2236 storeValue(cUnit, rlDest, rlResult);
2237 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002238 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002239 return false;
2240}
2241
2242/* Compare agaist zero */
2243static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002244 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002245{
2246 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002247 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002248 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2249 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2250 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002251
Bill Buzbee270c1d62009-08-13 16:58:07 -07002252//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07002253 switch (dalvikOpCode) {
2254 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002255 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002256 break;
2257 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002258 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002259 break;
2260 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002261 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002262 break;
2263 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002264 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002265 break;
2266 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002267 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002268 break;
2269 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002270 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002271 break;
2272 default:
2273 cond = 0;
2274 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
2275 dvmAbort();
2276 }
2277 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2278 /* This mostly likely will be optimized away in a later phase */
2279 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2280 return false;
2281}
2282
2283static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2284{
2285 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002286 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2287 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2288 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002289 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002290 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002291 int shiftOp = false;
2292 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002293
Ben Chengba4fc8b2009-06-01 13:00:29 -07002294 int __aeabi_idivmod(int op1, int op2);
2295 int __aeabi_idiv(int op1, int op2);
2296
2297 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002298 case OP_RSUB_INT_LIT8:
2299 case OP_RSUB_INT: {
2300 int tReg;
2301 //TUNING: add support for use of Arm rsub op
2302 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2303 tReg = allocTemp(cUnit);
2304 loadConstant(cUnit, tReg, lit);
2305 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2306 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2307 tReg, rlSrc.lowReg);
2308 storeValue(cUnit, rlDest, rlResult);
2309 return false;
2310 break;
2311 }
2312
Ben Chengba4fc8b2009-06-01 13:00:29 -07002313 case OP_ADD_INT_LIT8:
2314 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002315 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002316 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002317 case OP_MUL_INT_LIT8:
2318 case OP_MUL_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002319 op = kOpMul;
2320 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002321 case OP_AND_INT_LIT8:
2322 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002323 op = kOpAnd;
2324 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002325 case OP_OR_INT_LIT8:
2326 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002327 op = kOpOr;
2328 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002329 case OP_XOR_INT_LIT8:
2330 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002331 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002332 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002333 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002334 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002335 shiftOp = true;
2336 op = kOpLsl;
2337 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002338 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002339 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002340 shiftOp = true;
2341 op = kOpAsr;
2342 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002343 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002344 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002345 shiftOp = true;
2346 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002347 break;
2348
2349 case OP_DIV_INT_LIT8:
2350 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002351 case OP_REM_INT_LIT8:
2352 case OP_REM_INT_LIT16:
2353 if (lit == 0) {
2354 /* Let the interpreter deal with div by 0 */
2355 genInterpSingleStep(cUnit, mir);
2356 return false;
2357 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002358 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002359 loadValueDirectFixed(cUnit, rlSrc, r0);
2360 clobberReg(cUnit, r0);
2361 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2362 (dalvikOpCode == OP_DIV_INT_LIT16)) {
2363 loadConstant(cUnit, r2, (int)__aeabi_idiv);
2364 isDiv = true;
2365 } else {
2366 loadConstant(cUnit, r2, (int)__aeabi_idivmod);
2367 isDiv = false;
2368 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002369 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002370 opReg(cUnit, kOpBlx, r2);
2371 clobberCallRegs(cUnit);
2372 if (isDiv)
2373 rlResult = getReturnLoc(cUnit);
2374 else
2375 rlResult = getReturnLocAlt(cUnit);
2376 storeValue(cUnit, rlDest, rlResult);
2377 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002378 break;
2379 default:
2380 return true;
2381 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002382 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2383 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2384 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2385 if (shiftOp && (lit == 0)) {
2386 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2387 } else {
2388 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2389 }
2390 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002391 return false;
2392}
2393
2394static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2395{
2396 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2397 int fieldOffset;
2398
2399 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2400 InstField *pInstField = (InstField *)
2401 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002402
2403 assert(pInstField != NULL);
2404 fieldOffset = pInstField->byteOffset;
2405 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002406 /* Deliberately break the code while make the compiler happy */
2407 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002408 }
2409 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002410 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002411 // Generates a call - use explicit registers
2412 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2413 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2414 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002415 void *classPtr = (void*)
2416 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2417 assert(classPtr != NULL);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002418 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002419 genExportPC(cUnit, mir);
2420 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002421 loadConstant(cUnit, r0, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002422 loadConstant(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002423 /*
2424 * "len < 0": bail to the interpreter to re-execute the
2425 * instruction
2426 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002427 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002428 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002429 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002430 opReg(cUnit, kOpBlx, r3);
2431 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002432 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002433 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2434 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002435 /*
2436 * OOM exception needs to be thrown here and cannot re-execute
2437 */
2438 loadConstant(cUnit, r0,
2439 (int) (cUnit->method->insns + mir->offset));
2440 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2441 /* noreturn */
2442
Bill Buzbee1465db52009-09-23 17:17:35 -07002443 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002444 target->defMask = ENCODE_ALL;
2445 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07002446 rlResult = getReturnLoc(cUnit);
2447 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002448 break;
2449 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002450 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002451 // May generate a call - use explicit registers
2452 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2453 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2454 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002455 ClassObject *classPtr =
2456 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2457 assert(classPtr != NULL);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002458 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002459 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002460 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002461//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002462 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002463 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002464 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002465 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002466 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002467 /* r1 now contains object->clazz */
2468 loadConstant(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002469 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002470 opRegReg(cUnit, kOpCmp, r1, r2);
2471 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2472 genRegCopy(cUnit, r0, r1);
2473 genRegCopy(cUnit, r1, r2);
2474 opReg(cUnit, kOpBlx, r3);
2475 clobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002476 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002477 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002478 target->defMask = ENCODE_ALL;
Bill Buzbee1465db52009-09-23 17:17:35 -07002479 rlResult = getReturnLoc(cUnit);
2480 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002481 branch1->generic.target = (LIR *)target;
2482 branch2->generic.target = (LIR *)target;
2483 break;
2484 }
2485 case OP_IGET_WIDE:
2486 genIGetWide(cUnit, mir, fieldOffset);
2487 break;
2488 case OP_IGET:
2489 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002490 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002491 break;
2492 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002493 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002494 break;
2495 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002496 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002497 break;
2498 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002499 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002500 break;
2501 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002502 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002503 break;
2504 case OP_IPUT_WIDE:
2505 genIPutWide(cUnit, mir, fieldOffset);
2506 break;
2507 case OP_IPUT:
2508 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002509 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002510 break;
2511 case OP_IPUT_SHORT:
2512 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002513 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002514 break;
2515 case OP_IPUT_BYTE:
2516 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002517 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002518 break;
2519 default:
2520 return true;
2521 }
2522 return false;
2523}
2524
2525static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2526{
2527 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2528 int fieldOffset = mir->dalvikInsn.vC;
2529 switch (dalvikOpCode) {
2530 case OP_IGET_QUICK:
2531 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002532 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002533 break;
2534 case OP_IPUT_QUICK:
2535 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002536 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002537 break;
2538 case OP_IGET_WIDE_QUICK:
2539 genIGetWide(cUnit, mir, fieldOffset);
2540 break;
2541 case OP_IPUT_WIDE_QUICK:
2542 genIPutWide(cUnit, mir, fieldOffset);
2543 break;
2544 default:
2545 return true;
2546 }
2547 return false;
2548
2549}
2550
2551/* Compare agaist zero */
2552static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002553 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002554{
2555 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002556 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002557 RegLocation rlSrc1 = getSrcLoc(cUnit, mir, 0);
2558 RegLocation rlSrc2 = getSrcLoc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002559
Bill Buzbee1465db52009-09-23 17:17:35 -07002560 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2561 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2562 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002563
2564 switch (dalvikOpCode) {
2565 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002566 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002567 break;
2568 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002569 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002570 break;
2571 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002572 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002573 break;
2574 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002575 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002576 break;
2577 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002578 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002579 break;
2580 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002581 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002582 break;
2583 default:
2584 cond = 0;
2585 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
2586 dvmAbort();
2587 }
2588 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2589 /* This mostly likely will be optimized away in a later phase */
2590 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2591 return false;
2592}
2593
2594static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2595{
2596 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002597
2598 switch (opCode) {
2599 case OP_MOVE_16:
2600 case OP_MOVE_OBJECT_16:
2601 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002602 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002603 storeValue(cUnit, getDestLoc(cUnit, mir, 0),
2604 getSrcLoc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002605 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002606 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002607 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002608 case OP_MOVE_WIDE_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002609 storeValueWide(cUnit, getDestLocWide(cUnit, mir, 0, 1),
2610 getSrcLocWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002611 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002612 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002613 default:
2614 return true;
2615 }
2616 return false;
2617}
2618
2619static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2620{
2621 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002622 RegLocation rlSrc1;
2623 RegLocation rlSrc2;
2624 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002625
2626 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002627 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002628 }
2629
Bill Buzbee1465db52009-09-23 17:17:35 -07002630 /* APUTs have 3 sources and no targets */
2631 if (mir->ssaRep->numDefs == 0) {
2632 if (mir->ssaRep->numUses == 3) {
2633 rlDest = getSrcLoc(cUnit, mir, 0);
2634 rlSrc1 = getSrcLoc(cUnit, mir, 1);
2635 rlSrc2 = getSrcLoc(cUnit, mir, 2);
2636 } else {
2637 assert(mir->ssaRep->numUses == 4);
2638 rlDest = getSrcLocWide(cUnit, mir, 0, 1);
2639 rlSrc1 = getSrcLoc(cUnit, mir, 2);
2640 rlSrc2 = getSrcLoc(cUnit, mir, 3);
2641 }
2642 } else {
2643 /* Two sources and 1 dest. Deduce the operand sizes */
2644 if (mir->ssaRep->numUses == 4) {
2645 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
2646 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
2647 } else {
2648 assert(mir->ssaRep->numUses == 2);
2649 rlSrc1 = getSrcLoc(cUnit, mir, 0);
2650 rlSrc2 = getSrcLoc(cUnit, mir, 1);
2651 }
2652 if (mir->ssaRep->numDefs == 2) {
2653 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2654 } else {
2655 assert(mir->ssaRep->numDefs == 1);
2656 rlDest = getDestLoc(cUnit, mir, 0);
2657 }
2658 }
2659
2660
Ben Chengba4fc8b2009-06-01 13:00:29 -07002661 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002662 case OP_CMPL_FLOAT:
2663 case OP_CMPG_FLOAT:
2664 case OP_CMPL_DOUBLE:
2665 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002666 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002667 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002668 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002669 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002670 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002671 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002672 break;
2673 case OP_AGET:
2674 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002675 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002676 break;
2677 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002678 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002679 break;
2680 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002681 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002682 break;
2683 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002684 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002685 break;
2686 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002687 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002688 break;
2689 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002690 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002691 break;
2692 case OP_APUT:
2693 case OP_APUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002694 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002695 break;
2696 case OP_APUT_SHORT:
2697 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002698 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002699 break;
2700 case OP_APUT_BYTE:
2701 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002702 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002703 break;
2704 default:
2705 return true;
2706 }
2707 return false;
2708}
2709
Ben Cheng6c10a972009-10-29 14:39:18 -07002710/*
2711 * Find the matching case.
2712 *
2713 * return values:
2714 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2715 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2716 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2717 * above MAX_CHAINED_SWITCH_CASES).
2718 *
2719 * Instructions around the call are:
2720 *
2721 * mov r2, pc
2722 * blx &findPackedSwitchIndex
2723 * mov pc, r0
2724 * .align4
2725 * chaining cell for case 0 [8 bytes]
2726 * chaining cell for case 1 [8 bytes]
2727 * :
2728 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2729 * chaining cell for case default [8 bytes]
2730 * noChain exit
2731 */
2732s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
2733{
2734 int size;
2735 int firstKey;
2736 const int *entries;
2737 int index;
2738 int jumpIndex;
2739 int caseDPCOffset = 0;
2740 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2741 int chainingPC = (pc + 4) & ~3;
2742
2743 /*
2744 * Packed switch data format:
2745 * ushort ident = 0x0100 magic value
2746 * ushort size number of entries in the table
2747 * int first_key first (and lowest) switch case value
2748 * int targets[size] branch targets, relative to switch opcode
2749 *
2750 * Total size is (4+size*2) 16-bit code units.
2751 */
2752 size = switchData[1];
2753 assert(size > 0);
2754
2755 firstKey = switchData[2];
2756 firstKey |= switchData[3] << 16;
2757
2758
2759 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2760 * we can treat them as a native int array.
2761 */
2762 entries = (const int*) &switchData[4];
2763 assert(((u4)entries & 0x3) == 0);
2764
2765 index = testVal - firstKey;
2766
2767 /* Jump to the default cell */
2768 if (index < 0 || index >= size) {
2769 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2770 /* Jump to the non-chaining exit point */
2771 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2772 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2773 caseDPCOffset = entries[index];
2774 /* Jump to the inline chaining cell */
2775 } else {
2776 jumpIndex = index;
2777 }
2778
2779 chainingPC += jumpIndex * 8;
2780 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2781}
2782
2783/* See comments for findPackedSwitchIndex */
2784s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
2785{
2786 int size;
2787 const int *keys;
2788 const int *entries;
2789 int chainingPC = (pc + 4) & ~3;
2790 int i;
2791
2792 /*
2793 * Sparse switch data format:
2794 * ushort ident = 0x0200 magic value
2795 * ushort size number of entries in the table; > 0
2796 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2797 * int targets[size] branch targets, relative to switch opcode
2798 *
2799 * Total size is (2+size*4) 16-bit code units.
2800 */
2801
2802 size = switchData[1];
2803 assert(size > 0);
2804
2805 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2806 * we can treat them as a native int array.
2807 */
2808 keys = (const int*) &switchData[2];
2809 assert(((u4)keys & 0x3) == 0);
2810
2811 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2812 * we can treat them as a native int array.
2813 */
2814 entries = keys + size;
2815 assert(((u4)entries & 0x3) == 0);
2816
2817 /*
2818 * Run through the list of keys, which are guaranteed to
2819 * be sorted low-to-high.
2820 *
2821 * Most tables have 3-4 entries. Few have more than 10. A binary
2822 * search here is probably not useful.
2823 */
2824 for (i = 0; i < size; i++) {
2825 int k = keys[i];
2826 if (k == testVal) {
2827 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2828 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2829 i : MAX_CHAINED_SWITCH_CASES + 1;
2830 chainingPC += jumpIndex * 8;
2831 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2832 } else if (k > testVal) {
2833 break;
2834 }
2835 }
2836 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2837}
2838
Ben Chengba4fc8b2009-06-01 13:00:29 -07002839static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2840{
2841 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2842 switch (dalvikOpCode) {
2843 case OP_FILL_ARRAY_DATA: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002844 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2845 // Making a call - use explicit registers
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002846 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002847 genExportPC(cUnit, mir);
2848 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002849 loadConstant(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002850 loadConstant(cUnit, r1,
2851 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002852 opReg(cUnit, kOpBlx, r2);
2853 clobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002854 /* generate a branch over if successful */
2855 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2856 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2857 loadConstant(cUnit, r0,
2858 (int) (cUnit->method->insns + mir->offset));
2859 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2860 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2861 target->defMask = ENCODE_ALL;
2862 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002863 break;
2864 }
2865 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002866 * Compute the goto target of up to
2867 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2868 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002869 */
2870 case OP_PACKED_SWITCH:
2871 case OP_SPARSE_SWITCH: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002872 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002873 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002874 loadValueDirectFixed(cUnit, rlSrc, r1);
2875 lockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002876 const u2 *switchData =
2877 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2878 u2 size = switchData[1];
2879
Ben Chengba4fc8b2009-06-01 13:00:29 -07002880 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Cheng6c10a972009-10-29 14:39:18 -07002881 loadConstant(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002882 } else {
Ben Cheng6c10a972009-10-29 14:39:18 -07002883 loadConstant(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002884 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002885 /* r0 <- Addr of the switch data */
2886 loadConstant(cUnit, r0,
2887 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2888 /* r2 <- pc of the instruction following the blx */
2889 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002890 opReg(cUnit, kOpBlx, r4PC);
2891 clobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002892 /* pc <- computed goto target */
2893 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002894 break;
2895 }
2896 default:
2897 return true;
2898 }
2899 return false;
2900}
2901
2902static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002903 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002904{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002905 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002906 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002907
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002908 if (bb->fallThrough != NULL)
2909 retChainingCell = &labelList[bb->fallThrough->id];
2910
Ben Chengba4fc8b2009-06-01 13:00:29 -07002911 DecodedInstruction *dInsn = &mir->dalvikInsn;
2912 switch (mir->dalvikInsn.opCode) {
2913 /*
2914 * calleeMethod = this->clazz->vtable[
2915 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2916 * ]
2917 */
2918 case OP_INVOKE_VIRTUAL:
2919 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002920 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002921 int methodIndex =
2922 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2923 methodIndex;
2924
2925 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2926 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2927 else
2928 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2929
Ben Cheng38329f52009-07-07 14:19:20 -07002930 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2931 retChainingCell,
2932 predChainingCell,
2933 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002934 break;
2935 }
2936 /*
2937 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2938 * ->pResMethods[BBBB]->methodIndex]
2939 */
2940 /* TODO - not excersized in RunPerf.jar */
2941 case OP_INVOKE_SUPER:
2942 case OP_INVOKE_SUPER_RANGE: {
2943 int mIndex = cUnit->method->clazz->pDvmDex->
2944 pResMethods[dInsn->vB]->methodIndex;
2945 const Method *calleeMethod =
2946 cUnit->method->clazz->super->vtable[mIndex];
2947
2948 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2949 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2950 else
2951 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2952
2953 /* r0 = calleeMethod */
2954 loadConstant(cUnit, r0, (int) calleeMethod);
2955
Ben Cheng38329f52009-07-07 14:19:20 -07002956 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2957 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002958 break;
2959 }
2960 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2961 case OP_INVOKE_DIRECT:
2962 case OP_INVOKE_DIRECT_RANGE: {
2963 const Method *calleeMethod =
2964 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2965
2966 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
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_STATIC:
2980 case OP_INVOKE_STATIC_RANGE: {
2981 const Method *calleeMethod =
2982 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2983
2984 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2985 genProcessArgsNoRange(cUnit, mir, dInsn,
2986 NULL /* no null check */);
2987 else
2988 genProcessArgsRange(cUnit, mir, dInsn,
2989 NULL /* no null check */);
2990
2991 /* r0 = calleeMethod */
2992 loadConstant(cUnit, r0, (int) calleeMethod);
2993
Ben Cheng38329f52009-07-07 14:19:20 -07002994 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2995 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002996 break;
2997 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002998 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002999 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3000 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003001 *
3002 * Given "invoke-interface {v0}", the following is the generated code:
3003 *
3004 * 0x426a9abe : ldr r0, [r5, #0] --+
3005 * 0x426a9ac0 : mov r7, r5 |
3006 * 0x426a9ac2 : sub r7, #24 |
3007 * 0x426a9ac4 : cmp r0, #0 | genProcessArgsNoRange
3008 * 0x426a9ac6 : beq 0x426a9afe |
3009 * 0x426a9ac8 : stmia r7, <r0> --+
3010 * 0x426a9aca : ldr r4, [pc, #104] --> r4 <- dalvikPC of this invoke
3011 * 0x426a9acc : add r1, pc, #52 --> r1 <- &retChainingCell
3012 * 0x426a9ace : add r2, pc, #60 --> r2 <- &predictedChainingCell
3013 * 0x426a9ad0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_
3014 * 0x426a9ad2 : blx_2 see above --+ PREDICTED_CHAIN
3015 * 0x426a9ad4 : b 0x426a9b0c --> off to the predicted chain
3016 * 0x426a9ad6 : b 0x426a9afe --> punt to the interpreter
Ben Chenga8e64a72009-10-20 13:01:36 -07003017 * 0x426a9ad8 : mov r8, r1 --+
3018 * 0x426a9ada : mov r9, r2 |
3019 * 0x426a9adc : mov r10, r3 |
Ben Cheng38329f52009-07-07 14:19:20 -07003020 * 0x426a9ade : mov r0, r3 |
3021 * 0x426a9ae0 : mov r1, #74 | dvmFindInterfaceMethodInCache
3022 * 0x426a9ae2 : ldr r2, [pc, #76] |
3023 * 0x426a9ae4 : ldr r3, [pc, #68] |
3024 * 0x426a9ae6 : ldr r7, [pc, #64] |
3025 * 0x426a9ae8 : blx r7 --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003026 * 0x426a9aea : mov r1, r8 --> r1 <- rechain count
Ben Cheng38329f52009-07-07 14:19:20 -07003027 * 0x426a9aec : cmp r1, #0 --> compare against 0
3028 * 0x426a9aee : bgt 0x426a9af8 --> >=0? don't rechain
3029 * 0x426a9af0 : ldr r7, [r6, #96] --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003030 * 0x426a9af2 : mov r2, r9 | dvmJitToPatchPredictedChain
3031 * 0x426a9af4 : mov r3, r10 |
Ben Cheng38329f52009-07-07 14:19:20 -07003032 * 0x426a9af6 : blx r7 --+
3033 * 0x426a9af8 : add r1, pc, #8 --> r1 <- &retChainingCell
3034 * 0x426a9afa : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3035 * 0x426a9afc : blx_2 see above --+
3036 * -------- reconstruct dalvik PC : 0x428b786c @ +0x001e
3037 * 0x426a9afe (0042): ldr r0, [pc, #52]
3038 * Exception_Handling:
3039 * 0x426a9b00 (0044): ldr r1, [r6, #84]
3040 * 0x426a9b02 (0046): blx r1
3041 * 0x426a9b04 (0048): .align4
3042 * -------- chaining cell (hot): 0x0021
3043 * 0x426a9b04 (0048): ldr r0, [r6, #92]
3044 * 0x426a9b06 (004a): blx r0
3045 * 0x426a9b08 (004c): data 0x7872(30834)
3046 * 0x426a9b0a (004e): data 0x428b(17035)
3047 * 0x426a9b0c (0050): .align4
3048 * -------- chaining cell (predicted)
3049 * 0x426a9b0c (0050): data 0x0000(0) --> will be patched into bx
3050 * 0x426a9b0e (0052): data 0x0000(0)
3051 * 0x426a9b10 (0054): data 0x0000(0) --> class
3052 * 0x426a9b12 (0056): data 0x0000(0)
3053 * 0x426a9b14 (0058): data 0x0000(0) --> method
3054 * 0x426a9b16 (005a): data 0x0000(0)
3055 * 0x426a9b18 (005c): data 0x0000(0) --> reset count
3056 * 0x426a9b1a (005e): data 0x0000(0)
3057 * 0x426a9b28 (006c): .word (0xad0392a5)
3058 * 0x426a9b2c (0070): .word (0x6e750)
3059 * 0x426a9b30 (0074): .word (0x4109a618)
3060 * 0x426a9b34 (0078): .word (0x428b786c)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003061 */
3062 case OP_INVOKE_INTERFACE:
3063 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003064 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003065 int methodIndex = dInsn->vB;
3066
Bill Buzbee1465db52009-09-23 17:17:35 -07003067 /* Ensure that nothing is both live and dirty */
3068 flushAllRegs(cUnit);
3069
Ben Chengba4fc8b2009-06-01 13:00:29 -07003070 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
3071 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3072 else
3073 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3074
Ben Cheng38329f52009-07-07 14:19:20 -07003075 /* "this" is already left in r0 by genProcessArgs* */
3076
3077 /* r4PC = dalvikCallsite */
3078 loadConstant(cUnit, r4PC,
3079 (int) (cUnit->method->insns + mir->offset));
3080
3081 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003082 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003083 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003084 addrRetChain->generic.target = (LIR *) retChainingCell;
3085
3086 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003087 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003088 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003089 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3090
3091 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
3092
3093 /* return through lr - jump to the chaining cell */
3094 genUnconditionalBranch(cUnit, predChainingCell);
3095
3096 /*
3097 * null-check on "this" may have been eliminated, but we still need
3098 * a PC-reconstruction label for stack overflow bailout.
3099 */
3100 if (pcrLabel == NULL) {
3101 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003102 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003103 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07003104 pcrLabel->operands[0] = dPC;
3105 pcrLabel->operands[1] = mir->offset;
3106 /* Insert the place holder to the growable list */
3107 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3108 }
3109
3110 /* return through lr+2 - punt to the interpreter */
3111 genUnconditionalBranch(cUnit, pcrLabel);
3112
3113 /*
3114 * return through lr+4 - fully resolve the callee method.
3115 * r1 <- count
3116 * r2 <- &predictedChainCell
3117 * r3 <- this->class
3118 * r4 <- dPC
3119 * r7 <- this->class->vtable
3120 */
3121
3122 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003123 genRegCopy(cUnit, r8, r1);
3124 genRegCopy(cUnit, r9, r2);
3125 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003126
Ben Chengba4fc8b2009-06-01 13:00:29 -07003127 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003128 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003129
3130 /* r1 = BBBB */
3131 loadConstant(cUnit, r1, dInsn->vB);
3132
3133 /* r2 = method (caller) */
3134 loadConstant(cUnit, r2, (int) cUnit->method);
3135
3136 /* r3 = pDvmDex */
3137 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3138
3139 loadConstant(cUnit, r7,
3140 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003141 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003142
3143 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3144
Bill Buzbee1465db52009-09-23 17:17:35 -07003145 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003146
Ben Cheng38329f52009-07-07 14:19:20 -07003147 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07003148 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003149
Bill Buzbee1465db52009-09-23 17:17:35 -07003150 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07003151
Bill Buzbee270c1d62009-08-13 16:58:07 -07003152 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3153 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003154
Bill Buzbee1465db52009-09-23 17:17:35 -07003155 genRegCopy(cUnit, r2, r9);
3156 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003157
3158 /*
3159 * r0 = calleeMethod
3160 * r2 = &predictedChainingCell
3161 * r3 = class
3162 *
3163 * &returnChainingCell has been loaded into r1 but is not needed
3164 * when patching the chaining cell and will be clobbered upon
3165 * returning so it will be reconstructed again.
3166 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003167 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003168
3169 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003170 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003171 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003172
3173 bypassRechaining->generic.target = (LIR *) addrRetChain;
3174
Ben Chengba4fc8b2009-06-01 13:00:29 -07003175 /*
3176 * r0 = this, r1 = calleeMethod,
3177 * r1 = &ChainingCell,
3178 * r4PC = callsiteDPC,
3179 */
3180 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
3181#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07003182 gDvmJit.invokePredictedChain++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003183#endif
3184 /* Handle exceptions using the interpreter */
3185 genTrap(cUnit, mir->offset, pcrLabel);
3186 break;
3187 }
3188 /* NOP */
3189 case OP_INVOKE_DIRECT_EMPTY: {
3190 return false;
3191 }
3192 case OP_FILLED_NEW_ARRAY:
3193 case OP_FILLED_NEW_ARRAY_RANGE: {
3194 /* Just let the interpreter deal with these */
3195 genInterpSingleStep(cUnit, mir);
3196 break;
3197 }
3198 default:
3199 return true;
3200 }
3201 return false;
3202}
3203
3204static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003205 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003206{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003207 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3208 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3209 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003210
3211 DecodedInstruction *dInsn = &mir->dalvikInsn;
3212 switch (mir->dalvikInsn.opCode) {
3213 /* calleeMethod = this->clazz->vtable[BBBB] */
3214 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3215 case OP_INVOKE_VIRTUAL_QUICK: {
3216 int methodIndex = dInsn->vB;
3217 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3218 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3219 else
3220 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3221
Ben Cheng38329f52009-07-07 14:19:20 -07003222 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3223 retChainingCell,
3224 predChainingCell,
3225 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003226 break;
3227 }
3228 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3229 case OP_INVOKE_SUPER_QUICK:
3230 case OP_INVOKE_SUPER_QUICK_RANGE: {
3231 const Method *calleeMethod =
3232 cUnit->method->clazz->super->vtable[dInsn->vB];
3233
3234 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3235 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3236 else
3237 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3238
3239 /* r0 = calleeMethod */
3240 loadConstant(cUnit, r0, (int) calleeMethod);
3241
Ben Cheng38329f52009-07-07 14:19:20 -07003242 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3243 calleeMethod);
3244 /* Handle exceptions using the interpreter */
3245 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003246 break;
3247 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003248 default:
3249 return true;
3250 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003251 return false;
3252}
3253
3254/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003255 * This operation is complex enough that we'll do it partly inline
3256 * and partly with a handler. NOTE: the handler uses hardcoded
3257 * values for string object offsets and must be revisitied if the
3258 * layout changes.
3259 */
3260static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3261{
3262#if defined(USE_GLOBAL_STRING_DEFS)
3263 return false;
3264#else
3265 ArmLIR *rollback;
3266 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3267 RegLocation rlComp = getSrcLoc(cUnit, mir, 1);
3268
3269 loadValueDirectFixed(cUnit, rlThis, r0);
3270 loadValueDirectFixed(cUnit, rlComp, r1);
3271 /* Test objects for NULL */
3272 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3273 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3274 /*
3275 * TUNING: we could check for object pointer equality before invoking
3276 * handler. Unclear whether the gain would be worth the added code size
3277 * expansion.
3278 */
3279 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
3280 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3281 return true;
3282#endif
3283}
3284
3285static bool genInlinedIndexOf(CompilationUnit *cUnit, MIR *mir, bool singleI)
3286{
3287#if defined(USE_GLOBAL_STRING_DEFS)
3288 return false;
3289#else
3290 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3291 RegLocation rlChar = getSrcLoc(cUnit, mir, 1);
3292
3293 loadValueDirectFixed(cUnit, rlThis, r0);
3294 loadValueDirectFixed(cUnit, rlChar, r1);
3295 if (!singleI) {
3296 RegLocation rlStart = getSrcLoc(cUnit, mir, 2);
3297 loadValueDirectFixed(cUnit, rlStart, r2);
3298 } else {
3299 loadConstant(cUnit, r2, 0);
3300 }
3301 /* Test objects for NULL */
3302 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3303 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
3304 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3305 return true;
3306#endif
3307}
3308
3309
3310/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003311 * NOTE: Handles both range and non-range versions (arguments
3312 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003313 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003314static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003315{
3316 DecodedInstruction *dInsn = &mir->dalvikInsn;
3317 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003318 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003319 case OP_EXECUTE_INLINE: {
3320 unsigned int i;
3321 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003322 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003323 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003324 int tReg1;
3325 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003326 switch (operation) {
3327 case INLINE_EMPTYINLINEMETHOD:
3328 return false; /* Nop */
3329 case INLINE_STRING_LENGTH:
3330 return genInlinedStringLength(cUnit, mir);
3331 case INLINE_MATH_ABS_INT:
3332 return genInlinedAbsInt(cUnit, mir);
3333 case INLINE_MATH_ABS_LONG:
3334 return genInlinedAbsLong(cUnit, mir);
3335 case INLINE_MATH_MIN_INT:
3336 return genInlinedMinMaxInt(cUnit, mir, true);
3337 case INLINE_MATH_MAX_INT:
3338 return genInlinedMinMaxInt(cUnit, mir, false);
3339 case INLINE_STRING_CHARAT:
3340 return genInlinedStringCharAt(cUnit, mir);
3341 case INLINE_MATH_SQRT:
3342 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003343 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003344 else
3345 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003346 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003347 if (genInlinedAbsFloat(cUnit, mir))
3348 return false;
3349 else
3350 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003351 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003352 if (genInlinedAbsDouble(cUnit, mir))
3353 return false;
3354 else
3355 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003356 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003357 if (genInlinedCompareTo(cUnit, mir))
3358 return false;
3359 else
3360 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003361 case INLINE_STRING_INDEXOF_I:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003362 if (genInlinedIndexOf(cUnit, mir, true /* I */))
3363 return false;
3364 else
3365 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003366 case INLINE_STRING_INDEXOF_II:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003367 if (genInlinedIndexOf(cUnit, mir, false /* I */))
3368 return false;
3369 else
3370 break;
3371 case INLINE_STRING_EQUALS:
3372 case INLINE_MATH_COS:
3373 case INLINE_MATH_SIN:
3374 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003375 default:
3376 dvmAbort();
Ben Chengba4fc8b2009-06-01 13:00:29 -07003377 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003378 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07003379 clobberCallRegs(cUnit);
3380 clobberReg(cUnit, r4PC);
3381 clobberReg(cUnit, r7);
3382 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3383 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003384 loadConstant(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003385 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003386 for (i=0; i < dInsn->vA; i++) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003387 loadValueDirect(cUnit, getSrcLoc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003388 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003389 opReg(cUnit, kOpBlx, r4PC);
3390 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003391 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3392 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3393 loadConstant(cUnit, r0,
3394 (int) (cUnit->method->insns + mir->offset));
3395 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3396 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3397 target->defMask = ENCODE_ALL;
3398 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003399 break;
3400 }
3401 default:
3402 return true;
3403 }
3404 return false;
3405}
3406
3407static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3408{
Bill Buzbee1465db52009-09-23 17:17:35 -07003409 //TUNING: We're using core regs here - not optimal when target is a double
3410 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
3411 RegLocation rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
3412 loadConstantValue(cUnit, rlResult.lowReg,
3413 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3414 loadConstantValue(cUnit, rlResult.highReg,
3415 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
3416 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003417 return false;
3418}
3419
Ben Chengba4fc8b2009-06-01 13:00:29 -07003420/*
3421 * The following are special processing routines that handle transfer of
3422 * controls between compiled code and the interpreter. Certain VM states like
3423 * Dalvik PC and special-purpose registers are reconstructed here.
3424 */
3425
Ben Cheng1efc9c52009-06-08 18:25:27 -07003426/* Chaining cell for code that may need warmup. */
3427static void handleNormalChainingCell(CompilationUnit *cUnit,
3428 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003429{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003430 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3431 jitToInterpEntries.dvmJitToInterpNormal), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003432 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003433 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3434}
3435
3436/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003437 * Chaining cell for instructions that immediately following already translated
3438 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003439 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003440static void handleHotChainingCell(CompilationUnit *cUnit,
3441 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003442{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003443 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3444 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003445 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003446 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3447}
3448
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003449#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003450/* Chaining cell for branches that branch back into the same basic block */
3451static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3452 unsigned int offset)
3453{
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003454#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003455 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Jeff Hao97319a82009-08-12 16:57:15 -07003456 offsetof(InterpState, jitToInterpEntries.dvmJitToBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003457#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003458 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003459 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3460#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003461 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003462 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3463}
3464
3465#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003466/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003467static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3468 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003469{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003470 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3471 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003472 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003473 addWordData(cUnit, (int) (callee->insns), true);
3474}
3475
Ben Cheng38329f52009-07-07 14:19:20 -07003476/* Chaining cell for monomorphic method invocations. */
3477static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3478{
3479
3480 /* Should not be executed in the initial state */
3481 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3482 /* To be filled: class */
3483 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3484 /* To be filled: method */
3485 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3486 /*
3487 * Rechain count. The initial value of 0 here will trigger chaining upon
3488 * the first invocation of this callsite.
3489 */
3490 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3491}
3492
Ben Chengba4fc8b2009-06-01 13:00:29 -07003493/* Load the Dalvik PC into r0 and jump to the specified target */
3494static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003495 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003496{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003497 ArmLIR **pcrLabel =
3498 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003499 int numElems = cUnit->pcReconstructionList.numUsed;
3500 int i;
3501 for (i = 0; i < numElems; i++) {
3502 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3503 /* r0 = dalvik PC */
3504 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3505 genUnconditionalBranch(cUnit, targetLabel);
3506 }
3507}
3508
Bill Buzbee1465db52009-09-23 17:17:35 -07003509static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3510 "kMirOpPhi",
3511 "kMirOpNullNRangeUpCheck",
3512 "kMirOpNullNRangeDownCheck",
3513 "kMirOpLowerBound",
3514 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003515};
3516
3517/*
3518 * vA = arrayReg;
3519 * vB = idxReg;
3520 * vC = endConditionReg;
3521 * arg[0] = maxC
3522 * arg[1] = minC
3523 * arg[2] = loopBranchConditionCode
3524 */
3525static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3526{
Bill Buzbee1465db52009-09-23 17:17:35 -07003527 /*
3528 * NOTE: these synthesized blocks don't have ssa names assigned
3529 * for Dalvik registers. However, because they dominate the following
3530 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3531 * ssa name.
3532 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003533 DecodedInstruction *dInsn = &mir->dalvikInsn;
3534 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003535 const int maxC = dInsn->arg[0];
3536 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003537 int regLength;
3538 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3539 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003540
3541 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003542 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3543 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3544 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003545 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3546
3547 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003548 regLength = allocTemp(cUnit);
3549 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003550
3551 int delta = maxC;
3552 /*
3553 * If the loop end condition is ">=" instead of ">", then the largest value
3554 * of the index is "endCondition - 1".
3555 */
3556 if (dInsn->arg[2] == OP_IF_GE) {
3557 delta--;
3558 }
3559
3560 if (delta) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003561 int tReg = allocTemp(cUnit);
3562 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3563 rlIdxEnd.lowReg = tReg;
3564 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003565 }
3566 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003567 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003568 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003569}
3570
3571/*
3572 * vA = arrayReg;
3573 * vB = idxReg;
3574 * vC = endConditionReg;
3575 * arg[0] = maxC
3576 * arg[1] = minC
3577 * arg[2] = loopBranchConditionCode
3578 */
3579static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3580{
3581 DecodedInstruction *dInsn = &mir->dalvikInsn;
3582 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07003583 const int regLength = allocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003584 const int maxC = dInsn->arg[0];
3585 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003586 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3587 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003588
3589 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003590 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3591 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3592 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003593 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3594
3595 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003596 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003597
3598 if (maxC) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003599 int tReg = allocTemp(cUnit);
3600 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3601 rlIdxInit.lowReg = tReg;
3602 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003603 }
3604
3605 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003606 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003607 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003608}
3609
3610/*
3611 * vA = idxReg;
3612 * vB = minC;
3613 */
3614static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3615{
3616 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003617 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003618 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003619
3620 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003621 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003622
3623 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003624 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003625 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3626}
3627
3628/* Extended MIR instructions like PHI */
3629static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3630{
Bill Buzbee1465db52009-09-23 17:17:35 -07003631 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003632 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3633 false);
3634 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003635 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003636
3637 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003638 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003639 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003640 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003641 break;
3642 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003643 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003644 genHoistedChecksForCountUpLoop(cUnit, mir);
3645 break;
3646 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003647 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003648 genHoistedChecksForCountDownLoop(cUnit, mir);
3649 break;
3650 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003651 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003652 genHoistedLowerBoundCheck(cUnit, mir);
3653 break;
3654 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003655 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003656 genUnconditionalBranch(cUnit,
3657 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3658 break;
3659 }
3660 default:
3661 break;
3662 }
3663}
3664
3665/*
3666 * Create a PC-reconstruction cell for the starting offset of this trace.
3667 * Since the PCR cell is placed near the end of the compiled code which is
3668 * usually out of range for a conditional branch, we put two branches (one
3669 * branch over to the loop body and one layover branch to the actual PCR) at the
3670 * end of the entry block.
3671 */
3672static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3673 ArmLIR *bodyLabel)
3674{
3675 /* Set up the place holder to reconstruct this Dalvik PC */
3676 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003677 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng4238ec22009-08-24 16:32:22 -07003678 pcrLabel->operands[0] =
3679 (int) (cUnit->method->insns + entry->startOffset);
3680 pcrLabel->operands[1] = entry->startOffset;
3681 /* Insert the place holder to the growable list */
3682 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3683
3684 /*
3685 * Next, create two branches - one branch over to the loop body and the
3686 * other branch to the PCR cell to punt.
3687 */
3688 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003689 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003690 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003691 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003692 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3693
3694 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003695 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003696 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003697 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003698 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3699}
3700
Ben Chengba4fc8b2009-06-01 13:00:29 -07003701void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3702{
3703 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003704 ArmLIR *labelList =
3705 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003706 GrowableList chainingListByType[kChainingCellLast];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003707 int i;
3708
3709 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003710 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003711 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003712 for (i = 0; i < kChainingCellLast; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003713 dvmInitGrowableList(&chainingListByType[i], 2);
3714 }
3715
3716 BasicBlock **blockList = cUnit->blockList;
3717
Bill Buzbee6e963e12009-06-17 16:56:19 -07003718 if (cUnit->executionCount) {
3719 /*
3720 * Reserve 6 bytes at the beginning of the trace
3721 * +----------------------------+
3722 * | execution count (4 bytes) |
3723 * +----------------------------+
3724 * | chain cell offset (2 bytes)|
3725 * +----------------------------+
3726 * ...and then code to increment the execution
3727 * count:
3728 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3729 * sub r0, #10 @ back up to addr of executionCount
3730 * ldr r1, [r0]
3731 * add r1, #1
3732 * str r1, [r0]
3733 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003734 newLIR1(cUnit, kArm16BitData, 0);
3735 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003736 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003737 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003738 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003739 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003740 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3741 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3742 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3743 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3744 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003745 } else {
3746 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003747 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003748 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003749 cUnit->headerSize = 2;
3750 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003751
Ben Chengba4fc8b2009-06-01 13:00:29 -07003752 /* Handle the content in each basic block */
3753 for (i = 0; i < cUnit->numBlocks; i++) {
3754 blockList[i]->visited = true;
3755 MIR *mir;
3756
3757 labelList[i].operands[0] = blockList[i]->startOffset;
3758
Bill Buzbee1465db52009-09-23 17:17:35 -07003759 if (blockList[i]->blockType >= kChainingCellLast) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003760 /*
3761 * Append the label pseudo LIR first. Chaining cells will be handled
3762 * separately afterwards.
3763 */
3764 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3765 }
3766
Bill Buzbee1465db52009-09-23 17:17:35 -07003767 if (blockList[i]->blockType == kEntryBlock) {
3768 labelList[i].opCode = ARM_PSEUDO_kEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003769 if (blockList[i]->firstMIRInsn == NULL) {
3770 continue;
3771 } else {
3772 setupLoopEntryBlock(cUnit, blockList[i],
3773 &labelList[blockList[i]->fallThrough->id]);
3774 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003775 } else if (blockList[i]->blockType == kExitBlock) {
3776 labelList[i].opCode = ARM_PSEUDO_kExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003777 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003778 } else if (blockList[i]->blockType == kDalvikByteCode) {
3779 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003780 /* Reset the register state */
Bill Buzbee1465db52009-09-23 17:17:35 -07003781 resetRegPool(cUnit);
3782 clobberAllRegs(cUnit);
3783 resetNullCheckTracker(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003784 } else {
3785 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003786 case kChainingCellNormal:
3787 labelList[i].opCode = ARM_PSEUDO_kChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003788 /* handle the codegen later */
3789 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003790 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003791 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003792 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003793 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003794 ARM_PSEUDO_kChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003795 labelList[i].operands[0] =
3796 (int) blockList[i]->containingMethod;
3797 /* handle the codegen later */
3798 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003799 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003800 (void *) i);
3801 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003802 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003803 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003804 ARM_PSEUDO_kChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003805 /* handle the codegen later */
3806 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003807 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003808 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003809 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003810 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003811 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003812 ARM_PSEUDO_kChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003813 /* handle the codegen later */
3814 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003815 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003816 (void *) i);
3817 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003818 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003819 /* Make sure exception handling block is next */
3820 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003821 ARM_PSEUDO_kPCReconstruction_BLOCK_LABEL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003822 assert (i == cUnit->numBlocks - 2);
3823 handlePCReconstruction(cUnit, &labelList[i+1]);
3824 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003825 case kExceptionHandling:
3826 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003827 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003828 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3829 jitToInterpEntries.dvmJitToInterpPunt),
3830 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003831 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003832 }
3833 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003834#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003835 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003836 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003837 ARM_PSEUDO_kChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003838 /* handle the codegen later */
3839 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003840 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003841 (void *) i);
3842 break;
3843#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003844 default:
3845 break;
3846 }
3847 continue;
3848 }
Ben Chenge9695e52009-06-16 16:11:47 -07003849
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003850 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003851
Ben Chengba4fc8b2009-06-01 13:00:29 -07003852 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003853
3854 resetRegPool(cUnit);
3855 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
3856 clobberAllRegs(cUnit);
3857 }
3858
3859 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
3860 resetDefTracking(cUnit);
3861 }
3862
3863 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003864 handleExtendedMIR(cUnit, mir);
3865 continue;
3866 }
3867
Bill Buzbee1465db52009-09-23 17:17:35 -07003868
Ben Chengba4fc8b2009-06-01 13:00:29 -07003869 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3870 InstructionFormat dalvikFormat =
3871 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003872 ArmLIR *boundaryLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003873 newLIR2(cUnit, ARM_PSEUDO_kDalvikByteCode_BOUNDARY,
Ben Chengccd6c012009-10-15 14:52:45 -07003874 mir->offset,
3875 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3876 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003877 if (mir->ssaRep) {
3878 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003879 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003880 }
3881
Ben Chenge9695e52009-06-16 16:11:47 -07003882 /* Remember the first LIR for this block */
3883 if (headLIR == NULL) {
3884 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003885 /* Set the first boundaryLIR as a scheduling barrier */
3886 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003887 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003888
Ben Chengba4fc8b2009-06-01 13:00:29 -07003889 bool notHandled;
3890 /*
3891 * Debugging: screen the opcode first to see if it is in the
3892 * do[-not]-compile list
3893 */
3894 bool singleStepMe =
3895 gDvmJit.includeSelectedOp !=
3896 ((gDvmJit.opList[dalvikOpCode >> 3] &
3897 (1 << (dalvikOpCode & 0x7))) !=
3898 0);
3899 if (singleStepMe || cUnit->allSingleStep) {
3900 notHandled = false;
3901 genInterpSingleStep(cUnit, mir);
3902 } else {
3903 opcodeCoverage[dalvikOpCode]++;
3904 switch (dalvikFormat) {
3905 case kFmt10t:
3906 case kFmt20t:
3907 case kFmt30t:
3908 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3909 mir, blockList[i], labelList);
3910 break;
3911 case kFmt10x:
3912 notHandled = handleFmt10x(cUnit, mir);
3913 break;
3914 case kFmt11n:
3915 case kFmt31i:
3916 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3917 break;
3918 case kFmt11x:
3919 notHandled = handleFmt11x(cUnit, mir);
3920 break;
3921 case kFmt12x:
3922 notHandled = handleFmt12x(cUnit, mir);
3923 break;
3924 case kFmt20bc:
3925 notHandled = handleFmt20bc(cUnit, mir);
3926 break;
3927 case kFmt21c:
3928 case kFmt31c:
3929 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3930 break;
3931 case kFmt21h:
3932 notHandled = handleFmt21h(cUnit, mir);
3933 break;
3934 case kFmt21s:
3935 notHandled = handleFmt21s(cUnit, mir);
3936 break;
3937 case kFmt21t:
3938 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3939 labelList);
3940 break;
3941 case kFmt22b:
3942 case kFmt22s:
3943 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3944 break;
3945 case kFmt22c:
3946 notHandled = handleFmt22c(cUnit, mir);
3947 break;
3948 case kFmt22cs:
3949 notHandled = handleFmt22cs(cUnit, mir);
3950 break;
3951 case kFmt22t:
3952 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3953 labelList);
3954 break;
3955 case kFmt22x:
3956 case kFmt32x:
3957 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3958 break;
3959 case kFmt23x:
3960 notHandled = handleFmt23x(cUnit, mir);
3961 break;
3962 case kFmt31t:
3963 notHandled = handleFmt31t(cUnit, mir);
3964 break;
3965 case kFmt3rc:
3966 case kFmt35c:
3967 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3968 labelList);
3969 break;
3970 case kFmt3rms:
3971 case kFmt35ms:
3972 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3973 labelList);
3974 break;
3975 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003976 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003977 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003978 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003979 case kFmt51l:
3980 notHandled = handleFmt51l(cUnit, mir);
3981 break;
3982 default:
3983 notHandled = true;
3984 break;
3985 }
3986 }
3987 if (notHandled) {
3988 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3989 mir->offset,
3990 dalvikOpCode, getOpcodeName(dalvikOpCode),
3991 dalvikFormat);
3992 dvmAbort();
3993 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003994 }
3995 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003996
Bill Buzbee1465db52009-09-23 17:17:35 -07003997 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003998 dvmCompilerAppendLIR(cUnit,
3999 (LIR *) cUnit->loopAnalysis->branchToBody);
4000 dvmCompilerAppendLIR(cUnit,
4001 (LIR *) cUnit->loopAnalysis->branchToPCR);
4002 }
4003
4004 if (headLIR) {
4005 /*
4006 * Eliminate redundant loads/stores and delay stores into later
4007 * slots
4008 */
4009 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4010 cUnit->lastLIRInsn);
4011 }
4012
4013gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004014 /*
4015 * Check if the block is terminated due to trace length constraint -
4016 * insert an unconditional branch to the chaining cell.
4017 */
4018 if (blockList[i]->needFallThroughBranch) {
4019 genUnconditionalBranch(cUnit,
4020 &labelList[blockList[i]->fallThrough->id]);
4021 }
4022
Ben Chengba4fc8b2009-06-01 13:00:29 -07004023 }
4024
Ben Chenge9695e52009-06-16 16:11:47 -07004025 /* Handle the chaining cells in predefined order */
Bill Buzbee1465db52009-09-23 17:17:35 -07004026 for (i = 0; i < kChainingCellLast; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004027 size_t j;
4028 int *blockIdList = (int *) chainingListByType[i].elemList;
4029
4030 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4031
4032 /* No chaining cells of this type */
4033 if (cUnit->numChainingCells[i] == 0)
4034 continue;
4035
4036 /* Record the first LIR for a new type of chaining cell */
4037 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4038
4039 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4040 int blockId = blockIdList[j];
4041
4042 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004043 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004044
4045 /* Insert the pseudo chaining instruction */
4046 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4047
4048
4049 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004050 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004051 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004052 blockList[blockId]->startOffset);
4053 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004054 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004055 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004056 blockList[blockId]->containingMethod);
4057 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004058 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004059 handleInvokePredictedChainingCell(cUnit);
4060 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004061 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004062 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004063 blockList[blockId]->startOffset);
4064 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004065#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004066 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004067 handleBackwardBranchChainingCell(cUnit,
4068 blockList[blockId]->startOffset);
4069 break;
4070#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004071 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004072 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004073 dvmAbort();
4074 break;
4075 }
4076 }
4077 }
Ben Chenge9695e52009-06-16 16:11:47 -07004078
Ben Cheng6c10a972009-10-29 14:39:18 -07004079 /*
4080 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4081 * of all chaining cells for the overflow cases.
4082 */
4083 if (cUnit->switchOverflowPad) {
4084 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4085 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4086 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4087 opRegReg(cUnit, kOpAdd, r1, r1);
4088 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
4089#if defined(EXIT_STATS)
4090 loadConstant(cUnit, r0, kSwitchOverflow);
4091#endif
4092 opReg(cUnit, kOpBlx, r2);
4093 }
4094
Ben Chenge9695e52009-06-16 16:11:47 -07004095 dvmCompilerApplyGlobalOptimizations(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004096}
4097
4098/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004099bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004100{
Ben Chengccd6c012009-10-15 14:52:45 -07004101 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004102
Ben Chengccd6c012009-10-15 14:52:45 -07004103 if (gDvmJit.codeCacheFull) {
4104 return false;
4105 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004106
Ben Chengccd6c012009-10-15 14:52:45 -07004107 switch (work->kind) {
4108 case kWorkOrderMethod:
4109 res = dvmCompileMethod(work->info, &work->result);
4110 break;
4111 case kWorkOrderTrace:
4112 /* Start compilation with maximally allowed trace length */
4113 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4114 break;
4115 case kWorkOrderTraceDebug: {
4116 bool oldPrintMe = gDvmJit.printMe;
4117 gDvmJit.printMe = true;
4118 /* Start compilation with maximally allowed trace length */
4119 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4120 gDvmJit.printMe = oldPrintMe;;
4121 break;
4122 }
Ben Cheng60c24f42010-01-04 12:29:56 -08004123 case kWorkOrderICPatch:
4124 res = dvmJitPatchInlineCache((void *) work->pc, work->info);
4125 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004126 default:
4127 res = false;
4128 dvmAbort();
4129 }
4130 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004131}
4132
Ben Chengba4fc8b2009-06-01 13:00:29 -07004133/* Architectural-specific debugging helpers go here */
4134void dvmCompilerArchDump(void)
4135{
4136 /* Print compiled opcode in this VM instance */
4137 int i, start, streak;
4138 char buf[1024];
4139
4140 streak = i = 0;
4141 buf[0] = 0;
4142 while (opcodeCoverage[i] == 0 && i < 256) {
4143 i++;
4144 }
4145 if (i == 256) {
4146 return;
4147 }
4148 for (start = i++, streak = 1; i < 256; i++) {
4149 if (opcodeCoverage[i]) {
4150 streak++;
4151 } else {
4152 if (streak == 1) {
4153 sprintf(buf+strlen(buf), "%x,", start);
4154 } else {
4155 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4156 }
4157 streak = 0;
4158 while (opcodeCoverage[i] == 0 && i < 256) {
4159 i++;
4160 }
4161 if (i < 256) {
4162 streak = 1;
4163 start = i;
4164 }
4165 }
4166 }
4167 if (streak) {
4168 if (streak == 1) {
4169 sprintf(buf+strlen(buf), "%x", start);
4170 } else {
4171 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4172 }
4173 }
4174 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004175 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004176 }
4177}
Ben Chengd7d426a2009-09-22 11:23:36 -07004178
4179/* Common initialization routine for an architecture family */
4180bool dvmCompilerArchInit()
4181{
4182 int i;
4183
Bill Buzbee1465db52009-09-23 17:17:35 -07004184 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004185 if (EncodingMap[i].opCode != i) {
4186 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4187 EncodingMap[i].name, i, EncodingMap[i].opCode);
4188 dvmAbort();
4189 }
4190 }
4191
Ben Cheng5d90c202009-11-22 23:31:11 -08004192 return dvmCompilerArchVariantInit();
4193}
4194
4195void *dvmCompilerGetInterpretTemplate()
4196{
4197 return (void*) ((int)gDvmJit.codeCache +
4198 templateEntryOffsets[TEMPLATE_INTERPRET]);
4199}
4200
4201/* Needed by the ld/st optmizatons */
4202ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4203{
4204 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4205}
4206
4207/* Needed by the register allocator */
4208ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4209{
4210 return genRegCopy(cUnit, rDest, rSrc);
4211}
4212
4213/* Needed by the register allocator */
4214void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4215 int srcLo, int srcHi)
4216{
4217 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4218}
4219
4220void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4221 int displacement, int rSrc, OpSize size)
4222{
4223 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4224}
4225
4226void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4227 int displacement, int rSrcLo, int rSrcHi)
4228{
4229 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004230}