blob: f3648b9bdec01d9ac8cf72d4cd30d0b93059e8e9 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
Ben Cheng5d90c202009-11-22 23:31:11 -080027static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
28 int srcSize, int tgtSize)
29{
30 /*
31 * Don't optimize the register usage since it calls out to template
32 * functions
33 */
34 RegLocation rlSrc;
35 RegLocation rlDest;
36 flushAllRegs(cUnit); /* Send everything to home location */
37 if (srcSize == 1) {
38 rlSrc = getSrcLoc(cUnit, mir, 0);
39 loadValueDirectFixed(cUnit, rlSrc, r0);
40 } else {
41 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
42 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
43 }
44 loadConstant(cUnit, r2, (int)funct);
45 opReg(cUnit, kOpBlx, r2);
46 clobberCallRegs(cUnit);
47 if (tgtSize == 1) {
48 RegLocation rlResult;
49 rlDest = getDestLoc(cUnit, mir, 0);
50 rlResult = getReturnLoc(cUnit);
51 storeValue(cUnit, rlDest, rlResult);
52 } else {
53 RegLocation rlResult;
54 rlDest = getDestLocWide(cUnit, mir, 0, 1);
55 rlResult = getReturnLocWide(cUnit);
56 storeValueWide(cUnit, rlDest, rlResult);
57 }
58 return false;
59}
Ben Chengba4fc8b2009-06-01 13:00:29 -070060
Ben Chengba4fc8b2009-06-01 13:00:29 -070061
Ben Cheng5d90c202009-11-22 23:31:11 -080062static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
63 RegLocation rlDest, RegLocation rlSrc1,
64 RegLocation rlSrc2)
65{
66 RegLocation rlResult;
67 void* funct;
68
69 /* TODO: use a proper include file to define these */
70 float __aeabi_fadd(float a, float b);
71 float __aeabi_fsub(float a, float b);
72 float __aeabi_fdiv(float a, float b);
73 float __aeabi_fmul(float a, float b);
74 float fmodf(float a, float b);
75
76 switch (mir->dalvikInsn.opCode) {
77 case OP_ADD_FLOAT_2ADDR:
78 case OP_ADD_FLOAT:
79 funct = (void*) __aeabi_fadd;
80 break;
81 case OP_SUB_FLOAT_2ADDR:
82 case OP_SUB_FLOAT:
83 funct = (void*) __aeabi_fsub;
84 break;
85 case OP_DIV_FLOAT_2ADDR:
86 case OP_DIV_FLOAT:
87 funct = (void*) __aeabi_fdiv;
88 break;
89 case OP_MUL_FLOAT_2ADDR:
90 case OP_MUL_FLOAT:
91 funct = (void*) __aeabi_fmul;
92 break;
93 case OP_REM_FLOAT_2ADDR:
94 case OP_REM_FLOAT:
95 funct = (void*) fmodf;
96 break;
97 case OP_NEG_FLOAT: {
98 genNegFloat(cUnit, rlDest, rlSrc1);
99 return false;
100 }
101 default:
102 return true;
103 }
104 flushAllRegs(cUnit); /* Send everything to home location */
105 loadValueDirectFixed(cUnit, rlSrc1, r0);
106 loadValueDirectFixed(cUnit, rlSrc2, r1);
107 loadConstant(cUnit, r2, (int)funct);
108 opReg(cUnit, kOpBlx, r2);
109 clobberCallRegs(cUnit);
110 rlResult = getReturnLoc(cUnit);
111 storeValue(cUnit, rlDest, rlResult);
112 return false;
113}
114
115static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
116 RegLocation rlDest, RegLocation rlSrc1,
117 RegLocation rlSrc2)
118{
119 RegLocation rlResult;
120 void* funct;
121
122 /* TODO: use a proper include file to define these */
123 double __aeabi_dadd(double a, double b);
124 double __aeabi_dsub(double a, double b);
125 double __aeabi_ddiv(double a, double b);
126 double __aeabi_dmul(double a, double b);
127 double fmod(double a, double b);
128
129 switch (mir->dalvikInsn.opCode) {
130 case OP_ADD_DOUBLE_2ADDR:
131 case OP_ADD_DOUBLE:
132 funct = (void*) __aeabi_dadd;
133 break;
134 case OP_SUB_DOUBLE_2ADDR:
135 case OP_SUB_DOUBLE:
136 funct = (void*) __aeabi_dsub;
137 break;
138 case OP_DIV_DOUBLE_2ADDR:
139 case OP_DIV_DOUBLE:
140 funct = (void*) __aeabi_ddiv;
141 break;
142 case OP_MUL_DOUBLE_2ADDR:
143 case OP_MUL_DOUBLE:
144 funct = (void*) __aeabi_dmul;
145 break;
146 case OP_REM_DOUBLE_2ADDR:
147 case OP_REM_DOUBLE:
148 funct = (void*) fmod;
149 break;
150 case OP_NEG_DOUBLE: {
151 genNegDouble(cUnit, rlDest, rlSrc1);
152 return false;
153 }
154 default:
155 return true;
156 }
157 flushAllRegs(cUnit); /* Send everything to home location */
158 loadConstant(cUnit, rlr, (int)funct);
159 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
160 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
161 opReg(cUnit, kOpBlx, rlr);
162 clobberCallRegs(cUnit);
163 rlResult = getReturnLocWide(cUnit);
164 storeValueWide(cUnit, rlDest, rlResult);
165 return false;
166}
167
168static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
169{
170 OpCode opCode = mir->dalvikInsn.opCode;
171
172 float __aeabi_i2f( int op1 );
173 int __aeabi_f2iz( float op1 );
174 float __aeabi_d2f( double op1 );
175 double __aeabi_f2d( float op1 );
176 double __aeabi_i2d( int op1 );
177 int __aeabi_d2iz( double op1 );
178 float __aeabi_l2f( long op1 );
179 double __aeabi_l2d( long op1 );
180 s8 dvmJitf2l( float op1 );
181 s8 dvmJitd2l( double op1 );
182
183 switch (opCode) {
184 case OP_INT_TO_FLOAT:
185 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
186 case OP_FLOAT_TO_INT:
187 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
188 case OP_DOUBLE_TO_FLOAT:
189 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
190 case OP_FLOAT_TO_DOUBLE:
191 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
192 case OP_INT_TO_DOUBLE:
193 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
194 case OP_DOUBLE_TO_INT:
195 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
196 case OP_FLOAT_TO_LONG:
197 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
198 case OP_LONG_TO_FLOAT:
199 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
200 case OP_DOUBLE_TO_LONG:
201 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
202 case OP_LONG_TO_DOUBLE:
203 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
204 default:
205 return true;
206 }
207 return false;
208}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700209
Jeff Hao97319a82009-08-12 16:57:15 -0700210#if defined(WITH_SELF_VERIFICATION)
Jeff Hao97319a82009-08-12 16:57:15 -0700211/*
212 * The following are used to keep compiled loads and stores from modifying
213 * memory during self verification mode.
214 *
215 * Stores do not modify memory. Instead, the address and value pair are stored
216 * into heapSpace. Addresses within heapSpace are unique. For accesses smaller
217 * than a word, the word containing the address is loaded first before being
218 * updated.
219 *
220 * Loads check heapSpace first and return data from there if an entry exists.
221 * Otherwise, data is loaded from memory as usual.
222 */
223
224/* Decode contents of heapArgSpace to determine addr to load from */
225static void selfVerificationLoadDecode(HeapArgSpace* heapArgSpace, int* addr)
226{
Bill Buzbee1465db52009-09-23 17:17:35 -0700227 int reg = heapArgSpace->regMap & 0xFF;
228 if (!FPREG(reg)) {
229 assert(reg < 16);
230 *addr = heapArgSpace->coreRegs[reg];
231 } else {
232 assert(!DOUBLEREG(reg));
233 *addr = heapArgSpace->fpRegs[(reg & FP_REG_MASK)];
Jeff Hao97319a82009-08-12 16:57:15 -0700234 }
235}
236
237/* Decode contents of heapArgSpace to determine reg to load into */
238static void selfVerificationLoadDecodeData(HeapArgSpace* heapArgSpace,
239 int data, int reg)
240{
Bill Buzbee1465db52009-09-23 17:17:35 -0700241 if (!FPREG(reg)) {
242 assert(reg < 16);
243 heapArgSpace->coreRegs[reg] = data;
244 } else {
245 assert(!DOUBLEREG(reg));
246 heapArgSpace->fpRegs[(reg & FP_REG_MASK)] = data;
Jeff Hao97319a82009-08-12 16:57:15 -0700247 }
248}
249
250static void selfVerificationLoad(InterpState* interpState)
251{
252 Thread *self = dvmThreadSelf();
253 ShadowHeap *heapSpacePtr;
254 ShadowSpace *shadowSpace = self->shadowSpace;
255 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
256
257 int addr, data;
258 selfVerificationLoadDecode(heapArgSpace, &addr);
259
260 for (heapSpacePtr = shadowSpace->heapSpace;
261 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
262 if (heapSpacePtr->addr == addr) {
263 data = heapSpacePtr->data;
264 break;
265 }
266 }
267
268 if (heapSpacePtr == shadowSpace->heapSpaceTail)
269 data = *((unsigned int*) addr);
270
Bill Buzbee1465db52009-09-23 17:17:35 -0700271 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Ben Chengd7d426a2009-09-22 11:23:36 -0700272
Bill Buzbee1465db52009-09-23 17:17:35 -0700273 // LOGD("*** HEAP LOAD: Reg:%d Addr: 0x%x Data: 0x%x", reg, addr, data);
Ben Chengd7d426a2009-09-22 11:23:36 -0700274
Jeff Hao97319a82009-08-12 16:57:15 -0700275 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
276}
277
278static void selfVerificationLoadByte(InterpState* interpState)
279{
280 Thread *self = dvmThreadSelf();
281 ShadowHeap *heapSpacePtr;
282 ShadowSpace *shadowSpace = self->shadowSpace;
283 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
284
285 int addr, data;
286 selfVerificationLoadDecode(heapArgSpace, &addr);
287
288 int maskedAddr = addr & 0xFFFFFFFC;
289 int alignment = addr & 0x3;
290
291 for (heapSpacePtr = shadowSpace->heapSpace;
292 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
293 if (heapSpacePtr->addr == maskedAddr) {
294 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
295 data = *((unsigned char*) addr);
296 break;
297 }
298 }
299
300 if (heapSpacePtr == shadowSpace->heapSpaceTail)
301 data = *((unsigned char*) addr);
302
303 //LOGD("*** HEAP LOAD BYTE: Addr: 0x%x Data: 0x%x", addr, data);
304
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700306 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
307}
308
309static void selfVerificationLoadHalfword(InterpState* interpState)
310{
311 Thread *self = dvmThreadSelf();
312 ShadowHeap *heapSpacePtr;
313 ShadowSpace *shadowSpace = self->shadowSpace;
314 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
315
316 int addr, data;
317 selfVerificationLoadDecode(heapArgSpace, &addr);
318
319 int maskedAddr = addr & 0xFFFFFFFC;
320 int alignment = addr & 0x2;
321
322 for (heapSpacePtr = shadowSpace->heapSpace;
323 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
324 if (heapSpacePtr->addr == maskedAddr) {
325 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
326 data = *((unsigned short*) addr);
327 break;
328 }
329 }
330
331 if (heapSpacePtr == shadowSpace->heapSpaceTail)
332 data = *((unsigned short*) addr);
333
Bill Buzbee1465db52009-09-23 17:17:35 -0700334 //LOGD("*** HEAP LOAD kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700335
Bill Buzbee1465db52009-09-23 17:17:35 -0700336 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700337 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
338}
339
340static void selfVerificationLoadSignedByte(InterpState* interpState)
341{
342 Thread *self = dvmThreadSelf();
343 ShadowHeap* heapSpacePtr;
344 ShadowSpace* shadowSpace = self->shadowSpace;
345 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
346
347 int addr, data;
348 selfVerificationLoadDecode(heapArgSpace, &addr);
349
350 int maskedAddr = addr & 0xFFFFFFFC;
351 int alignment = addr & 0x3;
352
353 for (heapSpacePtr = shadowSpace->heapSpace;
354 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
355 if (heapSpacePtr->addr == maskedAddr) {
356 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
357 data = *((signed char*) addr);
358 break;
359 }
360 }
361
362 if (heapSpacePtr == shadowSpace->heapSpaceTail)
363 data = *((signed char*) addr);
364
365 //LOGD("*** HEAP LOAD SIGNED BYTE: Addr: 0x%x Data: 0x%x", addr, data);
366
Bill Buzbee1465db52009-09-23 17:17:35 -0700367 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700368 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
369}
370
371static void selfVerificationLoadSignedHalfword(InterpState* interpState)
372{
373 Thread *self = dvmThreadSelf();
374 ShadowHeap* heapSpacePtr;
375 ShadowSpace* shadowSpace = self->shadowSpace;
376 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
377
378 int addr, data;
379 selfVerificationLoadDecode(heapArgSpace, &addr);
380
381 int maskedAddr = addr & 0xFFFFFFFC;
382 int alignment = addr & 0x2;
383
384 for (heapSpacePtr = shadowSpace->heapSpace;
385 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
386 if (heapSpacePtr->addr == maskedAddr) {
387 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
388 data = *((signed short*) addr);
389 break;
390 }
391 }
392
393 if (heapSpacePtr == shadowSpace->heapSpaceTail)
394 data = *((signed short*) addr);
395
Bill Buzbee1465db52009-09-23 17:17:35 -0700396 //LOGD("*** HEAP LOAD SIGNED kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700397
Bill Buzbee1465db52009-09-23 17:17:35 -0700398 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700399 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
400}
401
402static void selfVerificationLoadDoubleword(InterpState* interpState)
403{
404 Thread *self = dvmThreadSelf();
405 ShadowHeap* heapSpacePtr;
406 ShadowSpace* shadowSpace = self->shadowSpace;
407 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
408
409 int addr;
410 selfVerificationLoadDecode(heapArgSpace, &addr);
411
412 int addr2 = addr+4;
413 unsigned int data = *((unsigned int*) addr);
414 unsigned int data2 = *((unsigned int*) addr2);
415
416 for (heapSpacePtr = shadowSpace->heapSpace;
417 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
418 if (heapSpacePtr->addr == addr) {
419 data = heapSpacePtr->data;
420 } else if (heapSpacePtr->addr == addr2) {
421 data2 = heapSpacePtr->data;
422 }
423 }
424
Bill Buzbee1465db52009-09-23 17:17:35 -0700425 // LOGD("*** HEAP LOAD DOUBLEWORD: Addr: 0x%x Data: 0x%x Data2: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700426 // addr, data, data2);
427
Bill Buzbee1465db52009-09-23 17:17:35 -0700428 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
429 int reg2 = (heapArgSpace->regMap >> 16) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700430 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
431 selfVerificationLoadDecodeData(heapArgSpace, data2, reg2);
432}
433
434/* Decode contents of heapArgSpace to determine arguments to store. */
435static void selfVerificationStoreDecode(HeapArgSpace* heapArgSpace,
436 int* value, int reg)
437{
Bill Buzbee1465db52009-09-23 17:17:35 -0700438 if (!FPREG(reg)) {
439 assert(reg < 16);
440 *value = heapArgSpace->coreRegs[reg];
441 } else {
442 assert(!DOUBLEREG(reg));
443 *value = heapArgSpace->fpRegs[(reg & FP_REG_MASK)];
Jeff Hao97319a82009-08-12 16:57:15 -0700444 }
445}
446
447static void selfVerificationStore(InterpState* interpState)
448{
449 Thread *self = dvmThreadSelf();
450 ShadowHeap *heapSpacePtr;
451 ShadowSpace *shadowSpace = self->shadowSpace;
452 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
453
454 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700455 int reg0 = heapArgSpace->regMap & 0xFF;
456 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700457 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
458 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
459
460 //LOGD("*** HEAP STORE: Addr: 0x%x Data: 0x%x", addr, data);
461
462 for (heapSpacePtr = shadowSpace->heapSpace;
463 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
464 if (heapSpacePtr->addr == addr) break;
465 }
466
467 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
468 heapSpacePtr->addr = addr;
469 shadowSpace->heapSpaceTail++;
470 }
471
472 heapSpacePtr->data = data;
473}
474
475static void selfVerificationStoreByte(InterpState* interpState)
476{
477 Thread *self = dvmThreadSelf();
478 ShadowHeap *heapSpacePtr;
479 ShadowSpace *shadowSpace = self->shadowSpace;
480 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
481
482 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700483 int reg0 = heapArgSpace->regMap & 0xFF;
484 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700485 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
486 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
487
488 int maskedAddr = addr & 0xFFFFFFFC;
489 int alignment = addr & 0x3;
490
491 //LOGD("*** HEAP STORE BYTE: Addr: 0x%x Data: 0x%x", addr, data);
492
493 for (heapSpacePtr = shadowSpace->heapSpace;
494 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
495 if (heapSpacePtr->addr == maskedAddr) break;
496 }
497
498 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
499 heapSpacePtr->addr = maskedAddr;
500 heapSpacePtr->data = *((unsigned int*) maskedAddr);
501 shadowSpace->heapSpaceTail++;
502 }
503
504 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
505 *((unsigned char*) addr) = (char) data;
506
507 //LOGD("*** HEAP STORE BYTE: Addr: 0x%x Final Data: 0x%x",
508 // addr, heapSpacePtr->data);
509}
510
511static void selfVerificationStoreHalfword(InterpState* interpState)
512{
513 Thread *self = dvmThreadSelf();
514 ShadowHeap *heapSpacePtr;
515 ShadowSpace *shadowSpace = self->shadowSpace;
516 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
517
518 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700519 int reg0 = heapArgSpace->regMap & 0xFF;
520 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700521 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
522 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
523
524 int maskedAddr = addr & 0xFFFFFFFC;
525 int alignment = addr & 0x2;
526
Bill Buzbee1465db52009-09-23 17:17:35 -0700527 //LOGD("*** HEAP STORE kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700528
529 for (heapSpacePtr = shadowSpace->heapSpace;
530 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
531 if (heapSpacePtr->addr == maskedAddr) break;
532 }
533
534 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
535 heapSpacePtr->addr = maskedAddr;
536 heapSpacePtr->data = *((unsigned int*) maskedAddr);
537 shadowSpace->heapSpaceTail++;
538 }
539
540 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
541 *((unsigned short*) addr) = (short) data;
542
Bill Buzbee1465db52009-09-23 17:17:35 -0700543 //LOGD("*** HEAP STORE kHalfWord: Addr: 0x%x Final Data: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700544 // addr, heapSpacePtr->data);
545}
546
547static void selfVerificationStoreDoubleword(InterpState* interpState)
548{
549 Thread *self = dvmThreadSelf();
550 ShadowHeap *heapSpacePtr;
551 ShadowSpace *shadowSpace = self->shadowSpace;
552 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
553
554 int addr, data, data2;
Bill Buzbee1465db52009-09-23 17:17:35 -0700555 int reg0 = heapArgSpace->regMap & 0xFF;
556 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
557 int reg2 = (heapArgSpace->regMap >> 16) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700558 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
559 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
560 selfVerificationStoreDecode(heapArgSpace, &data2, reg2);
561
562 int addr2 = addr+4;
563 bool store1 = false, store2 = false;
564
565 //LOGD("*** HEAP STORE DOUBLEWORD: Addr: 0x%x Data: 0x%x, Data2: 0x%x",
566 // addr, data, data2);
567
568 for (heapSpacePtr = shadowSpace->heapSpace;
569 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
570 if (heapSpacePtr->addr == addr) {
571 heapSpacePtr->data = data;
572 store1 = true;
573 } else if (heapSpacePtr->addr == addr2) {
574 heapSpacePtr->data = data2;
575 store2 = true;
576 }
577 }
578
579 if (!store1) {
580 shadowSpace->heapSpaceTail->addr = addr;
581 shadowSpace->heapSpaceTail->data = data;
582 shadowSpace->heapSpaceTail++;
583 }
584 if (!store2) {
585 shadowSpace->heapSpaceTail->addr = addr2;
586 shadowSpace->heapSpaceTail->data = data2;
587 shadowSpace->heapSpaceTail++;
588 }
589}
590
591/* Common wrapper function for all memory operations */
592static void selfVerificationMemOpWrapper(CompilationUnit *cUnit, int regMap,
593 void* funct)
594{
Bill Buzbee1465db52009-09-23 17:17:35 -0700595 /* push r0 and r7 to give us a foothold */
596 newLIR1(cUnit, kThumbPush, (1 << r0) | (1 << r7));
Jeff Hao97319a82009-08-12 16:57:15 -0700597
Bill Buzbee1465db52009-09-23 17:17:35 -0700598 /* Let the save handler know where the save record is */
599 loadConstant(cUnit, r0, offsetof(InterpState, heapArgSpace));
Jeff Hao97319a82009-08-12 16:57:15 -0700600
Bill Buzbee1465db52009-09-23 17:17:35 -0700601 /* Load the regMap and call the save handler [note: handler pops r0/r7] */
602 loadConstant(cUnit, r7, regMap);
603 genDispatchToHandler(cUnit, TEMPLATE_SAVE_STATE);
Jeff Hao97319a82009-08-12 16:57:15 -0700604
Bill Buzbee1465db52009-09-23 17:17:35 -0700605 /* Set function pointer, pass rGLUE and branch */
Jeff Hao97319a82009-08-12 16:57:15 -0700606 loadConstant(cUnit, r1, (int) funct);
Bill Buzbee1465db52009-09-23 17:17:35 -0700607 newLIR2(cUnit, kThumbMovRR, r0, rGLUE);
608 newLIR1(cUnit, kThumbBlxR, r1);
Jeff Hao97319a82009-08-12 16:57:15 -0700609
Bill Buzbee1465db52009-09-23 17:17:35 -0700610 /* Let the recover handler know where coreRegs[0] and restore regs */
611 loadConstant(cUnit, r0, offsetof(InterpState, heapArgSpace) +
612 offsetof(HeapArgSpace, coreRegs));
613 genDispatchToHandler(cUnit, TEMPLATE_RESTORE_STATE);
Jeff Hao97319a82009-08-12 16:57:15 -0700614}
Ben Cheng5d90c202009-11-22 23:31:11 -0800615
Jeff Hao97319a82009-08-12 16:57:15 -0700616#endif
617
Ben Chengba4fc8b2009-06-01 13:00:29 -0700618/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700619static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
620 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700621{
Bill Buzbee1465db52009-09-23 17:17:35 -0700622 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700623 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
624}
625
626/* Load a wide field from an object instance */
627static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
628{
629 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700630 RegLocation rlObj = getSrcLoc(cUnit, mir, 0);
631 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
632 RegLocation rlResult;
633 rlObj = loadValue(cUnit, rlObj, kCoreReg);
634 int regPtr = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700635
Bill Buzbee1465db52009-09-23 17:17:35 -0700636 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700637
Bill Buzbee1465db52009-09-23 17:17:35 -0700638 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
639 NULL);/* null object? */
640 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
641 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Jeff Hao97319a82009-08-12 16:57:15 -0700642#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700643 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -0700644#else
Bill Buzbee1465db52009-09-23 17:17:35 -0700645 int regMap = rlResult.highReg << 16 | rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700646 selfVerificationMemOpWrapper(cUnit, regMap,
647 &selfVerificationLoadDoubleword);
Jeff Hao97319a82009-08-12 16:57:15 -0700648#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 freeTemp(cUnit, regPtr);
650 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700651}
652
653/* Store a wide field to an object instance */
654static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
655{
656 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700657 RegLocation rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
658 RegLocation rlObj = getSrcLoc(cUnit, mir, 2);
659 rlObj = loadValue(cUnit, rlObj, kCoreReg);
660 int regPtr;
661 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
662 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
663 NULL);/* null object? */
664 regPtr = allocTemp(cUnit);
665 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700666#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700667 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -0700668#else
Bill Buzbee1465db52009-09-23 17:17:35 -0700669 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700670 selfVerificationMemOpWrapper(cUnit, regMap,
671 &selfVerificationStoreDoubleword);
672#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700673 freeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700674}
675
676/*
677 * Load a field from an object instance
678 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700679 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700680static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700681 int fieldOffset)
682{
Bill Buzbee1465db52009-09-23 17:17:35 -0700683 int regPtr;
684 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700685 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700686 RegLocation rlObj = getSrcLoc(cUnit, mir, 0);
687 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
688 rlObj = loadValue(cUnit, rlObj, kCoreReg);
689 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700690 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
691 NULL);/* null object? */
Ben Cheng5d90c202009-11-22 23:31:11 -0800692#if !defined(WITH_SELF_VERIFICATION)
693 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
694 size, rlObj.sRegLow);
695#else
Jeff Hao97319a82009-08-12 16:57:15 -0700696 /* Combine address and offset */
Bill Buzbee1465db52009-09-23 17:17:35 -0700697 regPtr = allocTemp(cUnit);
698 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700699
Bill Buzbee1465db52009-09-23 17:17:35 -0700700 int regMap = rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700701 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationLoad);
Bill Buzbee1465db52009-09-23 17:17:35 -0700702 freeTemp(cUnit, regPtr);
Jeff Hao97319a82009-08-12 16:57:15 -0700703#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700704 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700705}
706
707/*
708 * Store a field to an object instance
709 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700710 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700711static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700712 int fieldOffset)
713{
714 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700715 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
716 RegLocation rlObj = getSrcLoc(cUnit, mir, 1);
717 rlObj = loadValue(cUnit, rlObj, kCoreReg);
718 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
719 int regPtr;
720 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
721 NULL);/* null object? */
Jeff Hao97319a82009-08-12 16:57:15 -0700722#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700723 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Jeff Hao97319a82009-08-12 16:57:15 -0700724#else
725 /* Combine address and offset */
Bill Buzbee1465db52009-09-23 17:17:35 -0700726 regPtr = allocTemp(cUnit);
727 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700728
Bill Buzbee1465db52009-09-23 17:17:35 -0700729 int regMap = rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700730 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationStore);
Jeff Hao97319a82009-08-12 16:57:15 -0700731#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700732}
733
734
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735/*
736 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700737 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700738static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700739 RegLocation rlArray, RegLocation rlIndex,
740 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700741{
742 int lenOffset = offsetof(ArrayObject, length);
743 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 RegLocation rlResult;
745 rlArray = loadValue(cUnit, rlArray, kCoreReg);
746 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
747 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700748
749 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700750 ArmLIR * pcrLabel = NULL;
751
752 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700753 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
754 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700755 }
756
Bill Buzbee1465db52009-09-23 17:17:35 -0700757 regPtr = allocTemp(cUnit);
758
Ben Cheng4238ec22009-08-24 16:32:22 -0700759 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700760 int regLen = allocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700761 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700762 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
763 /* regPtr -> array data */
764 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
765 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
766 pcrLabel);
767 freeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700768 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700769 /* regPtr -> array data */
770 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700771 }
Jeff Hao97319a82009-08-12 16:57:15 -0700772#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700773 if ((size == kLong) || (size == kDouble)) {
774 if (scale) {
775 int rNewIndex = allocTemp(cUnit);
776 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
777 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
778 freeTemp(cUnit, rNewIndex);
779 } else {
780 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
781 }
782 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
783 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
784 freeTemp(cUnit, regPtr);
785 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700786 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
788 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
789 scale, size);
790 freeTemp(cUnit, regPtr);
791 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792 }
Jeff Hao97319a82009-08-12 16:57:15 -0700793#else
Bill Buzbee270c1d62009-08-13 16:58:07 -0700794 //TODO: probably want to move this into loadBaseIndexed
795 void *funct = NULL;
796 switch(size) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700797 case kLong:
798 case kDouble:
Jeff Hao97319a82009-08-12 16:57:15 -0700799 funct = (void*) &selfVerificationLoadDoubleword;
800 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700801 case kWord:
Jeff Hao97319a82009-08-12 16:57:15 -0700802 funct = (void*) &selfVerificationLoad;
803 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700804 case kUnsignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700805 funct = (void*) &selfVerificationLoadHalfword;
806 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700807 case kSignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700808 funct = (void*) &selfVerificationLoadSignedHalfword;
809 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700810 case kUnsignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700811 funct = (void*) &selfVerificationLoadByte;
812 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700813 case kSignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700814 funct = (void*) &selfVerificationLoadSignedByte;
815 break;
816 default:
817 assert(0);
818 dvmAbort();
Jeff Hao97319a82009-08-12 16:57:15 -0700819 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700820 /* Combine address and index */
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 if (scale) {
822 int regTmp = allocTemp(cUnit);
823 opRegRegImm(cUnit, kOpLsl, regTmp, rlIndex.lowReg, scale);
824 opRegReg(cUnit, kOpAdd, regPtr, regTmp);
825 freeTemp(cUnit, regTmp);
826 } else {
827 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
828 }
Jeff Hao97319a82009-08-12 16:57:15 -0700829
Bill Buzbee1465db52009-09-23 17:17:35 -0700830 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
831 int regMap = rlResult.highReg << 16 | rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700832 selfVerificationMemOpWrapper(cUnit, regMap, funct);
833
Bill Buzbee1465db52009-09-23 17:17:35 -0700834 freeTemp(cUnit, regPtr);
835 if ((size == kLong) || (size == kDouble))
836 storeValueWide(cUnit, rlDest, rlResult);
Jeff Hao97319a82009-08-12 16:57:15 -0700837 else
Bill Buzbee1465db52009-09-23 17:17:35 -0700838 storeValue(cUnit, rlDest, rlResult);
Jeff Hao97319a82009-08-12 16:57:15 -0700839#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700840}
841
Ben Chengba4fc8b2009-06-01 13:00:29 -0700842/*
843 * Generate array store
844 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700845 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700846static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700847 RegLocation rlArray, RegLocation rlIndex,
848 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700849{
850 int lenOffset = offsetof(ArrayObject, length);
851 int dataOffset = offsetof(ArrayObject, contents);
852
Bill Buzbee1465db52009-09-23 17:17:35 -0700853 int regPtr;
854 rlArray = loadValue(cUnit, rlArray, kCoreReg);
855 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700856
Bill Buzbee1465db52009-09-23 17:17:35 -0700857 if (isTemp(cUnit, rlArray.lowReg)) {
858 clobberReg(cUnit, rlArray.lowReg);
859 regPtr = rlArray.lowReg;
860 } else {
861 regPtr = allocTemp(cUnit);
862 genRegCopy(cUnit, regPtr, rlArray.lowReg);
863 }
Ben Chenge9695e52009-06-16 16:11:47 -0700864
Ben Cheng1efc9c52009-06-08 18:25:27 -0700865 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700866 ArmLIR * pcrLabel = NULL;
867
868 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700869 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
870 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700871 }
872
873 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700874 int regLen = allocTemp(cUnit);
875 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700876 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700877 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
878 /* regPtr -> array data */
879 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
880 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
881 pcrLabel);
882 freeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700883 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700884 /* regPtr -> array data */
885 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700886 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700887 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700888#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700889 if ((size == kLong) || (size == kDouble)) {
890 //TODO: need specific wide routine that can handle fp regs
891 if (scale) {
892 int rNewIndex = allocTemp(cUnit);
893 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
894 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
895 freeTemp(cUnit, rNewIndex);
896 } else {
897 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
898 }
899 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
900 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
901 freeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700902 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700903 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
904 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
905 scale, size);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700906 }
907#else
908 //TODO: probably want to move this into storeBaseIndexed
909 void *funct = NULL;
910 switch(size) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700911 case kLong:
912 case kDouble:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700913 funct = (void*) &selfVerificationStoreDoubleword;
914 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700915 case kWord:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700916 funct = (void*) &selfVerificationStore;
917 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700918 case kSignedHalf:
919 case kUnsignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700920 funct = (void*) &selfVerificationStoreHalfword;
921 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700922 case kSignedByte:
923 case kUnsignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700924 funct = (void*) &selfVerificationStoreByte;
925 break;
926 default:
927 assert(0);
928 dvmAbort();
929 }
930
Bill Buzbee1465db52009-09-23 17:17:35 -0700931 if (scale) {
932 int regTmpIndex = allocTemp(cUnit);
933 // 3 live temps
934 opRegRegImm(cUnit, kOpLsl, regTmpIndex, rlIndex.lowReg, scale);
935 opRegReg(cUnit, kOpAdd, regPtr, regTmpIndex);
936 freeTemp(cUnit, regTmpIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700937 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700938 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700939 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700940 /* Combine address and index */
941 if ((size == kLong) || (size == kDouble)) {
942 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
943 } else {
944 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
945 }
Jeff Hao97319a82009-08-12 16:57:15 -0700946
Bill Buzbee1465db52009-09-23 17:17:35 -0700947 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700948 selfVerificationMemOpWrapper(cUnit, regMap, funct);
949
Jeff Hao97319a82009-08-12 16:57:15 -0700950#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700951}
952
Ben Cheng5d90c202009-11-22 23:31:11 -0800953static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
954 RegLocation rlDest, RegLocation rlSrc1,
955 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700956{
Ben Chenge9695e52009-06-16 16:11:47 -0700957 /*
958 * Don't mess with the regsiters here as there is a particular calling
959 * convention to the out-of-line handler.
960 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700961 RegLocation rlResult;
962
963 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
964 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700965 switch( mir->dalvikInsn.opCode) {
966 case OP_SHL_LONG:
967 case OP_SHL_LONG_2ADDR:
968 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
969 break;
970 case OP_SHR_LONG:
971 case OP_SHR_LONG_2ADDR:
972 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
973 break;
974 case OP_USHR_LONG:
975 case OP_USHR_LONG_2ADDR:
976 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
977 break;
978 default:
979 return true;
980 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700981 rlResult = getReturnLocWide(cUnit);
982 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700983 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700984}
Ben Chenge9695e52009-06-16 16:11:47 -0700985
Ben Cheng5d90c202009-11-22 23:31:11 -0800986static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
987 RegLocation rlDest, RegLocation rlSrc1,
988 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700989{
Bill Buzbee1465db52009-09-23 17:17:35 -0700990 RegLocation rlResult;
991 OpKind firstOp = kOpBkpt;
992 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700993 bool callOut = false;
994 void *callTgt;
995 int retReg = r0;
996 /* TODO - find proper .h file to declare these */
997 long long __aeabi_ldivmod(long long op1, long long op2);
998
999 switch (mir->dalvikInsn.opCode) {
1000 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07001001 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1002 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1003 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1004 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1005 storeValueWide(cUnit, rlDest, rlResult);
1006 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007 break;
1008 case OP_ADD_LONG:
1009 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001010 firstOp = kOpAdd;
1011 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001012 break;
1013 case OP_SUB_LONG:
1014 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001015 firstOp = kOpSub;
1016 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001017 break;
1018 case OP_MUL_LONG:
1019 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001020 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001021 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001022 case OP_DIV_LONG:
1023 case OP_DIV_LONG_2ADDR:
1024 callOut = true;
1025 retReg = r0;
1026 callTgt = (void*)__aeabi_ldivmod;
1027 break;
1028 /* NOTE - result is in r2/r3 instead of r0/r1 */
1029 case OP_REM_LONG:
1030 case OP_REM_LONG_2ADDR:
1031 callOut = true;
1032 callTgt = (void*)__aeabi_ldivmod;
1033 retReg = r2;
1034 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001035 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001036 case OP_AND_LONG:
1037 firstOp = kOpAnd;
1038 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001039 break;
1040 case OP_OR_LONG:
1041 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001042 firstOp = kOpOr;
1043 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001044 break;
1045 case OP_XOR_LONG:
1046 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001047 firstOp = kOpXor;
1048 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001049 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001050 case OP_NEG_LONG: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001051 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1052 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1053 loadConstantValue(cUnit, rlResult.highReg, 0);
1054 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1055 rlResult.highReg, rlSrc2.lowReg);
1056 opRegReg(cUnit, kOpSbc, rlResult.highReg, rlSrc2.highReg);
1057 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001058 return false;
Ben Chenge9695e52009-06-16 16:11:47 -07001059 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001060 default:
1061 LOGE("Invalid long arith op");
1062 dvmAbort();
1063 }
1064 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001065 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001066 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001067 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001068 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001069 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1070 loadConstant(cUnit, rlr, (int) callTgt);
1071 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1072 opReg(cUnit, kOpBlx, rlr);
1073 clobberCallRegs(cUnit);
1074 if (retReg == r0)
1075 rlResult = getReturnLocWide(cUnit);
1076 else
1077 rlResult = getReturnLocWideAlt(cUnit);
1078 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001079 }
1080 return false;
1081}
1082
Ben Cheng5d90c202009-11-22 23:31:11 -08001083static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
1084 RegLocation rlDest, RegLocation rlSrc1,
1085 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001086{
Bill Buzbee1465db52009-09-23 17:17:35 -07001087 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001088 bool callOut = false;
1089 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -07001090 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001091 int retReg = r0;
1092 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -07001093 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -08001094 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001095
1096 /* TODO - find proper .h file to declare these */
1097 int __aeabi_idivmod(int op1, int op2);
1098 int __aeabi_idiv(int op1, int op2);
1099
1100 switch (mir->dalvikInsn.opCode) {
1101 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001102 op = kOpNeg;
1103 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001104 break;
1105 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001106 op = kOpMvn;
1107 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001108 break;
1109 case OP_ADD_INT:
1110 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001111 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001112 break;
1113 case OP_SUB_INT:
1114 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001115 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001116 break;
1117 case OP_MUL_INT:
1118 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001119 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001120 break;
1121 case OP_DIV_INT:
1122 case OP_DIV_INT_2ADDR:
1123 callOut = true;
1124 checkZero = true;
1125 callTgt = __aeabi_idiv;
1126 retReg = r0;
1127 break;
1128 /* NOTE: returns in r1 */
1129 case OP_REM_INT:
1130 case OP_REM_INT_2ADDR:
1131 callOut = true;
1132 checkZero = true;
1133 callTgt = __aeabi_idivmod;
1134 retReg = r1;
1135 break;
1136 case OP_AND_INT:
1137 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001138 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001139 break;
1140 case OP_OR_INT:
1141 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001142 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001143 break;
1144 case OP_XOR_INT:
1145 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001146 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001147 break;
1148 case OP_SHL_INT:
1149 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -08001150 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -07001151 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001152 break;
1153 case OP_SHR_INT:
1154 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -08001155 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -07001156 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001157 break;
1158 case OP_USHR_INT:
1159 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -08001160 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -07001161 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001162 break;
1163 default:
1164 LOGE("Invalid word arith op: 0x%x(%d)",
1165 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
1166 dvmAbort();
1167 }
1168 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001169 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1170 if (unary) {
1171 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1172 opRegReg(cUnit, op, rlResult.lowReg,
1173 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -07001174 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001175 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -08001176 if (shiftOp) {
1177 int tReg = allocTemp(cUnit);
1178 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1179 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1180 opRegRegReg(cUnit, op, rlResult.lowReg,
1181 rlSrc1.lowReg, tReg);
1182 freeTemp(cUnit, tReg);
1183 } else {
1184 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1185 opRegRegReg(cUnit, op, rlResult.lowReg,
1186 rlSrc1.lowReg, rlSrc2.lowReg);
1187 }
Ben Chenge9695e52009-06-16 16:11:47 -07001188 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001189 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001190 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001191 RegLocation rlResult;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001192 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001193 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001194 loadConstant(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -07001195 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001196 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001197 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001198 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001199 opReg(cUnit, kOpBlx, r2);
1200 clobberCallRegs(cUnit);
1201 if (retReg == r0)
1202 rlResult = getReturnLoc(cUnit);
1203 else
1204 rlResult = getReturnLocAlt(cUnit);
1205 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001206 }
1207 return false;
1208}
1209
Ben Cheng5d90c202009-11-22 23:31:11 -08001210static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001211{
1212 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001213 RegLocation rlDest;
1214 RegLocation rlSrc1;
1215 RegLocation rlSrc2;
1216 /* Deduce sizes of operands */
1217 if (mir->ssaRep->numUses == 2) {
1218 rlSrc1 = getSrcLoc(cUnit, mir, 0);
1219 rlSrc2 = getSrcLoc(cUnit, mir, 1);
1220 } else if (mir->ssaRep->numUses == 3) {
1221 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
1222 rlSrc2 = getSrcLoc(cUnit, mir, 2);
1223 } else {
1224 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
1225 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
1226 assert(mir->ssaRep->numUses == 4);
1227 }
1228 if (mir->ssaRep->numDefs == 1) {
1229 rlDest = getDestLoc(cUnit, mir, 0);
1230 } else {
1231 assert(mir->ssaRep->numDefs == 2);
1232 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1233 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001234
1235 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001236 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001237 }
1238 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
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_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001242 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001243 }
1244 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
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_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001248 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001249 }
1250 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
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_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001254 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001255 }
1256 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
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_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001260 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001261 }
1262 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001263 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001264 }
1265 return true;
1266}
1267
Bill Buzbee1465db52009-09-23 17:17:35 -07001268/* Generate conditional branch instructions */
1269static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
1270 ArmConditionCode cond,
1271 ArmLIR *target)
1272{
1273 ArmLIR *branch = opCondBranch(cUnit, cond);
1274 branch->generic.target = (LIR *) target;
1275 return branch;
1276}
1277
1278/* Generate unconditional branch instructions */
1279static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
1280{
1281 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
1282 branch->generic.target = (LIR *) target;
1283 return branch;
1284}
1285
Bill Buzbee1465db52009-09-23 17:17:35 -07001286/* Perform the actual operation for OP_RETURN_* */
1287static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
1288{
1289 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
1290#if defined(INVOKE_STATS)
1291 gDvmJit.returnOp++;
1292#endif
1293 int dPC = (int) (cUnit->method->insns + mir->offset);
1294 /* Insert branch, but defer setting of target */
1295 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
1296 /* Set up the place holder to reconstruct this Dalvik PC */
1297 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
1298 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
1299 pcrLabel->operands[0] = dPC;
1300 pcrLabel->operands[1] = mir->offset;
1301 /* Insert the place holder to the growable list */
1302 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1303 /* Branch to the PC reconstruction code */
1304 branch->generic.target = (LIR *) pcrLabel;
1305}
1306
Ben Chengba4fc8b2009-06-01 13:00:29 -07001307static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
1308 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001309 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001310{
1311 unsigned int i;
1312 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -07001313 RegLocation rlArg;
1314 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001315
Bill Buzbee1465db52009-09-23 17:17:35 -07001316 /*
1317 * Load arguments to r0..r4. Note that these registers may contain
1318 * live values, so we clobber them immediately after loading to prevent
1319 * them from being used as sources for subsequent loads.
1320 */
1321 lockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001322 for (i = 0; i < dInsn->vA; i++) {
1323 regMask |= 1 << i;
Bill Buzbee1465db52009-09-23 17:17:35 -07001324 rlArg = getSrcLoc(cUnit, mir, numDone++);
1325 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001326 }
1327 if (regMask) {
1328 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -07001329 opRegRegImm(cUnit, kOpSub, r7, rFP,
1330 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001331 /* generate null check */
1332 if (pcrLabel) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001333 *pcrLabel = genNullCheck(cUnit, getSrcSSAName(mir, 0), r0,
1334 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001335 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001336 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001337 }
1338}
1339
1340static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1341 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001342 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001343{
1344 int srcOffset = dInsn->vC << 2;
1345 int numArgs = dInsn->vA;
1346 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001347
1348 /*
1349 * Note: here, all promoted registers will have been flushed
1350 * back to the Dalvik base locations, so register usage restrictins
1351 * are lifted. All parms loaded from original Dalvik register
1352 * region - even though some might conceivably have valid copies
1353 * cached in a preserved register.
1354 */
1355 lockAllTemps(cUnit);
1356
Ben Chengba4fc8b2009-06-01 13:00:29 -07001357 /*
1358 * r4PC : &rFP[vC]
1359 * r7: &newFP[0]
1360 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001361 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001362 /* load [r0 .. min(numArgs,4)] */
1363 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001364 /*
1365 * Protect the loadMultiple instruction from being reordered with other
1366 * Dalvik stack accesses.
1367 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001368 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001369
Bill Buzbee1465db52009-09-23 17:17:35 -07001370 opRegRegImm(cUnit, kOpSub, r7, rFP,
1371 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001372 /* generate null check */
1373 if (pcrLabel) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001374 *pcrLabel = genNullCheck(cUnit, getSrcSSAName(mir, 0), r0,
1375 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001376 }
1377
1378 /*
1379 * Handle remaining 4n arguments:
1380 * store previously loaded 4 values and load the next 4 values
1381 */
1382 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001383 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001384 /*
1385 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001386 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001387 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001388 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001389 /* No need to generate the loop structure if numArgs <= 11 */
1390 if (numArgs > 11) {
1391 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001392 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001393 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001394 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001395 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001396 /*
1397 * Protect the loadMultiple instruction from being reordered with other
1398 * Dalvik stack accesses.
1399 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001400 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001401 /* No need to generate the loop structure if numArgs <= 11 */
1402 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001403 opRegImm(cUnit, kOpSub, rFP, 4);
1404 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001405 }
1406 }
1407
1408 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001409 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001410
1411 /* Generate the loop epilogue - don't use r0 */
1412 if ((numArgs > 4) && (numArgs % 4)) {
1413 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001414 /*
1415 * Protect the loadMultiple instruction from being reordered with other
1416 * Dalvik stack accesses.
1417 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001418 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001419 }
1420 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001421 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001422
1423 /* Save the modulo 4 arguments */
1424 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001425 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001426 }
1427}
1428
Ben Cheng38329f52009-07-07 14:19:20 -07001429/*
1430 * Generate code to setup the call stack then jump to the chaining cell if it
1431 * is not a native method.
1432 */
1433static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001434 BasicBlock *bb, ArmLIR *labelList,
1435 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001436 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001437{
Bill Buzbee1465db52009-09-23 17:17:35 -07001438 /*
1439 * Note: all Dalvik register state should be flushed to
1440 * memory by the point, so register usage restrictions no
1441 * longer apply. All temp & preserved registers may be used.
1442 */
1443 lockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001444 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001445
1446 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001447 lockTemp(cUnit, r1);
1448 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001449 /* r4PC = dalvikCallsite */
1450 loadConstant(cUnit, r4PC,
1451 (int) (cUnit->method->insns + mir->offset));
1452 addrRetChain->generic.target = (LIR *) retChainingCell;
1453 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001454 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001455 * r1 = &ChainingCell
1456 * r4PC = callsiteDPC
1457 */
1458 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001459 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001460#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07001461 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001462#endif
1463 } else {
1464 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
1465#if defined(INVOKE_STATS)
1466 gDvmJit.invokeChain++;
1467#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001468 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001469 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1470 }
1471 /* Handle exceptions using the interpreter */
1472 genTrap(cUnit, mir->offset, pcrLabel);
1473}
1474
Ben Cheng38329f52009-07-07 14:19:20 -07001475/*
1476 * Generate code to check the validity of a predicted chain and take actions
1477 * based on the result.
1478 *
1479 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1480 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1481 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1482 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1483 * 0x426a99b2 : blx_2 see above --+
1484 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1485 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1486 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1487 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1488 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1489 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1490 * 0x426a99c0 : blx r7 --+
1491 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1492 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1493 * 0x426a99c6 : blx_2 see above --+
1494 */
1495static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1496 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001497 ArmLIR *retChainingCell,
1498 ArmLIR *predChainingCell,
1499 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001500{
Bill Buzbee1465db52009-09-23 17:17:35 -07001501 /*
1502 * Note: all Dalvik register state should be flushed to
1503 * memory by the point, so register usage restrictions no
1504 * longer apply. Lock temps to prevent them from being
1505 * allocated by utility routines.
1506 */
1507 lockAllTemps(cUnit);
1508
Ben Cheng38329f52009-07-07 14:19:20 -07001509 /* "this" is already left in r0 by genProcessArgs* */
1510
1511 /* r4PC = dalvikCallsite */
1512 loadConstant(cUnit, r4PC,
1513 (int) (cUnit->method->insns + mir->offset));
1514
1515 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001516 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001517 addrRetChain->generic.target = (LIR *) retChainingCell;
1518
1519 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001520 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001521 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1522
1523 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1524
1525 /* return through lr - jump to the chaining cell */
1526 genUnconditionalBranch(cUnit, predChainingCell);
1527
1528 /*
1529 * null-check on "this" may have been eliminated, but we still need a PC-
1530 * reconstruction label for stack overflow bailout.
1531 */
1532 if (pcrLabel == NULL) {
1533 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001534 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001535 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07001536 pcrLabel->operands[0] = dPC;
1537 pcrLabel->operands[1] = mir->offset;
1538 /* Insert the place holder to the growable list */
1539 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1540 }
1541
1542 /* return through lr+2 - punt to the interpreter */
1543 genUnconditionalBranch(cUnit, pcrLabel);
1544
1545 /*
1546 * return through lr+4 - fully resolve the callee method.
1547 * r1 <- count
1548 * r2 <- &predictedChainCell
1549 * r3 <- this->class
1550 * r4 <- dPC
1551 * r7 <- this->class->vtable
1552 */
1553
1554 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001555 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001556
1557 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001558 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001559
Bill Buzbee1465db52009-09-23 17:17:35 -07001560 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001561
Bill Buzbee270c1d62009-08-13 16:58:07 -07001562 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1563 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001564
1565 /*
1566 * r0 = calleeMethod
1567 * r2 = &predictedChainingCell
1568 * r3 = class
1569 *
1570 * &returnChainingCell has been loaded into r1 but is not needed
1571 * when patching the chaining cell and will be clobbered upon
1572 * returning so it will be reconstructed again.
1573 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001574 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001575
1576 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001577 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001578 addrRetChain->generic.target = (LIR *) retChainingCell;
1579
1580 bypassRechaining->generic.target = (LIR *) addrRetChain;
1581 /*
1582 * r0 = calleeMethod,
1583 * r1 = &ChainingCell,
1584 * r4PC = callsiteDPC,
1585 */
1586 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
1587#if defined(INVOKE_STATS)
1588 gDvmJit.invokePredictedChain++;
1589#endif
1590 /* Handle exceptions using the interpreter */
1591 genTrap(cUnit, mir->offset, pcrLabel);
1592}
1593
1594/*
1595 * Up calling this function, "this" is stored in r0. The actual class will be
1596 * chased down off r0 and the predicted one will be retrieved through
1597 * predictedChainingCell then a comparison is performed to see whether the
1598 * previously established chaining is still valid.
1599 *
1600 * The return LIR is a branch based on the comparison result. The actual branch
1601 * target will be setup in the caller.
1602 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001603static ArmLIR *genCheckPredictedChain(CompilationUnit *cUnit,
1604 ArmLIR *predChainingCell,
1605 ArmLIR *retChainingCell,
Ben Cheng38329f52009-07-07 14:19:20 -07001606 MIR *mir)
1607{
Bill Buzbee1465db52009-09-23 17:17:35 -07001608 /*
1609 * Note: all Dalvik register state should be flushed to
1610 * memory by the point, so register usage restrictions no
1611 * longer apply. All temp & preserved registers may be used.
1612 */
1613 lockAllTemps(cUnit);
1614
Ben Cheng38329f52009-07-07 14:19:20 -07001615 /* r3 now contains this->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001616 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001617
1618 /*
1619 * r2 now contains predicted class. The starting offset of the
1620 * cached value is 4 bytes into the chaining cell.
1621 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001622 ArmLIR *getPredictedClass =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001623 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, clazz), r2);
Ben Cheng38329f52009-07-07 14:19:20 -07001624 getPredictedClass->generic.target = (LIR *) predChainingCell;
1625
1626 /*
1627 * r0 now contains predicted method. The starting offset of the
1628 * cached value is 8 bytes into the chaining cell.
1629 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001630 ArmLIR *getPredictedMethod =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001631 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, method), r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001632 getPredictedMethod->generic.target = (LIR *) predChainingCell;
1633
1634 /* Load the stats counter to see if it is time to unchain and refresh */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001635 ArmLIR *getRechainingRequestCount =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001636 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, counter), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001637 getRechainingRequestCount->generic.target =
1638 (LIR *) predChainingCell;
1639
1640 /* r4PC = dalvikCallsite */
1641 loadConstant(cUnit, r4PC,
1642 (int) (cUnit->method->insns + mir->offset));
1643
1644 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001645 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001646 addrRetChain->generic.target = (LIR *) retChainingCell;
1647
1648 /* Check if r2 (predicted class) == r3 (actual class) */
Bill Buzbee1465db52009-09-23 17:17:35 -07001649 opRegReg(cUnit, kOpCmp, r2, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001650
Bill Buzbee1465db52009-09-23 17:17:35 -07001651 return opCondBranch(cUnit, kArmCondEq);
Ben Cheng38329f52009-07-07 14:19:20 -07001652}
1653
Ben Chengba4fc8b2009-06-01 13:00:29 -07001654/* Geneate a branch to go back to the interpreter */
1655static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1656{
1657 /* r0 = dalvik pc */
Bill Buzbee1465db52009-09-23 17:17:35 -07001658 flushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001659 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001660 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1661 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1662 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001663 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001664}
1665
1666/*
1667 * Attempt to single step one instruction using the interpreter and return
1668 * to the compiled code for the next Dalvik instruction
1669 */
1670static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1671{
1672 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1673 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1674 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001675
1676 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
1677 flushAllRegs(cUnit);
1678
Ben Chengba4fc8b2009-06-01 13:00:29 -07001679 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1680 genPuntToInterp(cUnit, mir->offset);
1681 return;
1682 }
1683 int entryAddr = offsetof(InterpState,
1684 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001685 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001686 /* r0 = dalvik pc */
1687 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1688 /* r1 = dalvik pc of following instruction */
1689 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001690 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001691}
1692
Ben Cheng5d90c202009-11-22 23:31:11 -08001693static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001694{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001695 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001696 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001697 genExportPC(cUnit, mir);
1698 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
1699 loadValueDirectFixed(cUnit, rlSrc, r1);
1700 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001701 if (isEnter) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001702 loadConstant(cUnit, r2, (int)dvmLockObject);
1703 } else {
1704 loadConstant(cUnit, r2, (int)dvmUnlockObject);
1705 }
1706 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
1707 /* Do the call */
1708 opReg(cUnit, kOpBlx, r2);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001709#if defined(WITH_DEADLOCK_PREDICTION)
1710 if (isEnter) {
1711 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
1712 loadWordDisp(cUnit, r0, offsetof(Thread, exception), r1);
1713 opRegImm(cUnit, kOpCmp, r1, 0);
1714 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
1715 loadConstant(cUnit, r0,
1716 (int) (cUnit->method->insns + mir->offset));
1717 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1718 /* noreturn */
1719 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1720 target->defMask = ENCODE_ALL;
1721 branchOver->generic.target = (LIR *) target;
1722 }
1723#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001724 clobberCallRegs(cUnit);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001725}
1726
Ben Chengba4fc8b2009-06-01 13:00:29 -07001727/*
1728 * The following are the first-level codegen routines that analyze the format
1729 * of each bytecode then either dispatch special purpose codegen routines
1730 * or produce corresponding Thumb instructions directly.
1731 */
1732
1733static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001734 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001735{
1736 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1737 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1738 return false;
1739}
1740
1741static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1742{
1743 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1744 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden96516932009-10-28 17:39:02 -07001745 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_EB))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001746 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1747 return true;
1748 }
1749 switch (dalvikOpCode) {
1750 case OP_RETURN_VOID:
1751 genReturnCommon(cUnit,mir);
1752 break;
1753 case OP_UNUSED_73:
1754 case OP_UNUSED_79:
1755 case OP_UNUSED_7A:
1756 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1757 return true;
1758 case OP_NOP:
1759 break;
1760 default:
1761 return true;
1762 }
1763 return false;
1764}
1765
1766static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1767{
Bill Buzbee1465db52009-09-23 17:17:35 -07001768 RegLocation rlDest;
1769 RegLocation rlResult;
1770 if (mir->ssaRep->numDefs == 2) {
1771 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1772 } else {
1773 rlDest = getDestLoc(cUnit, mir, 0);
1774 }
Ben Chenge9695e52009-06-16 16:11:47 -07001775
Ben Chengba4fc8b2009-06-01 13:00:29 -07001776 switch (mir->dalvikInsn.opCode) {
1777 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001778 case OP_CONST_4: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001779 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1780 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1781 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001782 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001783 }
1784 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001785 //TUNING: single routine to load constant pair for support doubles
1786 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1787 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1788 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1789 rlResult.lowReg, 31);
1790 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001791 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001792 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001793 default:
1794 return true;
1795 }
1796 return false;
1797}
1798
1799static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1800{
Bill Buzbee1465db52009-09-23 17:17:35 -07001801 RegLocation rlDest;
1802 RegLocation rlResult;
1803 if (mir->ssaRep->numDefs == 2) {
1804 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1805 } else {
1806 rlDest = getDestLoc(cUnit, mir, 0);
1807 }
1808 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001809
Ben Chengba4fc8b2009-06-01 13:00:29 -07001810 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001811 case OP_CONST_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001812 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB << 16);
1813 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001814 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001815 }
1816 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001817 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1818 0, mir->dalvikInsn.vB << 16);
1819 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001820 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001821 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001822 default:
1823 return true;
1824 }
1825 return false;
1826}
1827
1828static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1829{
1830 /* For OP_THROW_VERIFICATION_ERROR */
1831 genInterpSingleStep(cUnit, mir);
1832 return false;
1833}
1834
1835static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1836{
Bill Buzbee1465db52009-09-23 17:17:35 -07001837 RegLocation rlResult;
1838 RegLocation rlDest;
1839 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001840
Ben Chengba4fc8b2009-06-01 13:00:29 -07001841 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001842 case OP_CONST_STRING_JUMBO:
1843 case OP_CONST_STRING: {
1844 void *strPtr = (void*)
1845 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
1846 assert(strPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001847 rlDest = getDestLoc(cUnit, mir, 0);
1848 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1849 loadConstantValue(cUnit, rlResult.lowReg, (int) strPtr );
1850 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001851 break;
1852 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001853 case OP_CONST_CLASS: {
1854 void *classPtr = (void*)
1855 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1856 assert(classPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001857 rlDest = getDestLoc(cUnit, mir, 0);
1858 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1859 loadConstantValue(cUnit, rlResult.lowReg, (int) classPtr );
1860 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001861 break;
1862 }
1863 case OP_SGET_OBJECT:
1864 case OP_SGET_BOOLEAN:
1865 case OP_SGET_CHAR:
1866 case OP_SGET_BYTE:
1867 case OP_SGET_SHORT:
1868 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001869 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001870 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001871 void *fieldPtr = (void*)
1872 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
1873 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001874 rlDest = getDestLoc(cUnit, mir, 0);
1875 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1876 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001877#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001878 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001879#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001880 int regMap = rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001881 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationLoad);
1882
Jeff Hao97319a82009-08-12 16:57:15 -07001883#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001884 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001885 break;
1886 }
1887 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001888 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001889 void *fieldPtr = (void*)
1890 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Bill Buzbee1465db52009-09-23 17:17:35 -07001891 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001892 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001893 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1894 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1895 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001896#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001897 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001898#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001899 int regMap = rlResult.highReg << 16 |
1900 rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001901 selfVerificationMemOpWrapper(cUnit, regMap,
1902 &selfVerificationLoadDoubleword);
1903
Jeff Hao97319a82009-08-12 16:57:15 -07001904#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001905 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001906 break;
1907 }
1908 case OP_SPUT_OBJECT:
1909 case OP_SPUT_BOOLEAN:
1910 case OP_SPUT_CHAR:
1911 case OP_SPUT_BYTE:
1912 case OP_SPUT_SHORT:
1913 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001914 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001915 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001916 void *fieldPtr = (void*)
1917 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001918
Ben Chengba4fc8b2009-06-01 13:00:29 -07001919 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001920 rlSrc = getSrcLoc(cUnit, mir, 0);
1921 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1922 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001923#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001924 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001925#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001926 int regMap = rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001927 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationStore);
1928#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001929 break;
1930 }
1931 case OP_SPUT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001932 int tReg = allocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001933 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001934 void *fieldPtr = (void*)
1935 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001936
Ben Chengba4fc8b2009-06-01 13:00:29 -07001937 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001938 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
1939 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1940 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001941#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001942 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001943#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001944 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001945 selfVerificationMemOpWrapper(cUnit, regMap,
1946 &selfVerificationStoreDoubleword);
1947#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001948 break;
1949 }
1950 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001951 /*
1952 * Obey the calling convention and don't mess with the register
1953 * usage.
1954 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001955 ClassObject *classPtr = (void*)
1956 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1957 assert(classPtr != NULL);
1958 assert(classPtr->status & CLASS_INITIALIZED);
Ben Cheng79d173c2009-09-29 16:12:51 -07001959 /*
1960 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001961 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001962 */
1963 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001964 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001965 genExportPC(cUnit, mir);
1966 loadConstant(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001967 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001968 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001969 opReg(cUnit, kOpBlx, r2);
1970 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001971 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001972 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1973 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001974 /*
1975 * OOM exception needs to be thrown here and cannot re-execute
1976 */
1977 loadConstant(cUnit, r0,
1978 (int) (cUnit->method->insns + mir->offset));
1979 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1980 /* noreturn */
1981
Bill Buzbee1465db52009-09-23 17:17:35 -07001982 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001983 target->defMask = ENCODE_ALL;
1984 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07001985 rlDest = getDestLoc(cUnit, mir, 0);
1986 rlResult = getReturnLoc(cUnit);
1987 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001988 break;
1989 }
1990 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001991 /*
1992 * Obey the calling convention and don't mess with the register
1993 * usage.
1994 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001995 ClassObject *classPtr =
1996 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001997 /*
1998 * Note: It is possible that classPtr is NULL at this point,
1999 * even though this instruction has been successfully interpreted.
2000 * If the previous interpretation had a null source, the
2001 * interpreter would not have bothered to resolve the clazz.
2002 * Bail out to the interpreter in this case, and log it
2003 * so that we can tell if it happens frequently.
2004 */
2005 if (classPtr == NULL) {
2006 LOGD("null clazz in OP_CHECK_CAST, single-stepping");
2007 genInterpSingleStep(cUnit, mir);
2008 return false;
2009 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002010 flushAllRegs(cUnit); /* Send everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002011 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002012 rlSrc = getSrcLoc(cUnit, mir, 0);
2013 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2014 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
2015 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
2016 /*
2017 * rlSrc.lowReg now contains object->clazz. Note that
2018 * it could have been allocated r0, but we're okay so long
2019 * as we don't do anything desctructive until r0 is loaded
2020 * with clazz.
2021 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002022 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002023 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
2024 loadConstant(cUnit, r2, (int)dvmInstanceofNonTrivial);
2025 opRegReg(cUnit, kOpCmp, r0, r1);
2026 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2027 opReg(cUnit, kOpBlx, r2);
2028 clobberCallRegs(cUnit);
2029 /*
2030 * If null, check cast failed - punt to the interpreter. Because
2031 * interpreter will be the one throwing, we don't need to
2032 * genExportPC() here.
2033 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002034 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002035 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002036 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002037 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002038 branch1->generic.target = (LIR *)target;
2039 branch2->generic.target = (LIR *)target;
2040 break;
2041 }
2042 default:
2043 return true;
2044 }
2045 return false;
2046}
2047
2048static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
2049{
2050 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002051 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002052 switch (dalvikOpCode) {
2053 case OP_MOVE_EXCEPTION: {
2054 int offset = offsetof(InterpState, self);
2055 int exOffset = offsetof(Thread, exception);
Bill Buzbee1465db52009-09-23 17:17:35 -07002056 int selfReg = allocTemp(cUnit);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002057 int resetReg = allocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002058 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2059 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2060 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002061 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002062 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002063 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07002064 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002065 break;
2066 }
2067 case OP_MOVE_RESULT:
2068 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002069 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2070 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
2071 rlSrc.fp = rlDest.fp;
2072 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002073 break;
2074 }
2075 case OP_MOVE_RESULT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002076 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
2077 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
2078 rlSrc.fp = rlDest.fp;
2079 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002080 break;
2081 }
2082 case OP_RETURN_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002083 RegLocation rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2084 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
2085 rlDest.fp = rlSrc.fp;
2086 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002087 genReturnCommon(cUnit,mir);
2088 break;
2089 }
2090 case OP_RETURN:
2091 case OP_RETURN_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002092 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2093 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
2094 rlDest.fp = rlSrc.fp;
2095 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002096 genReturnCommon(cUnit,mir);
2097 break;
2098 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002099 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002100 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08002101#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08002102 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002103#else
Ben Cheng5d90c202009-11-22 23:31:11 -08002104 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002105#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07002106 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002107 case OP_THROW: {
2108 genInterpSingleStep(cUnit, mir);
2109 break;
2110 }
2111 default:
2112 return true;
2113 }
2114 return false;
2115}
2116
Bill Buzbeed45ba372009-06-15 17:00:57 -07002117static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
2118{
2119 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002120 RegLocation rlDest;
2121 RegLocation rlSrc;
2122 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07002123
Ben Chengba4fc8b2009-06-01 13:00:29 -07002124 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002125 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002126 }
2127
Bill Buzbee1465db52009-09-23 17:17:35 -07002128 if (mir->ssaRep->numUses == 2)
2129 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2130 else
2131 rlSrc = getSrcLoc(cUnit, mir, 0);
2132 if (mir->ssaRep->numDefs == 2)
2133 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2134 else
2135 rlDest = getDestLoc(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07002136
Ben Chengba4fc8b2009-06-01 13:00:29 -07002137 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002138 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002139 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002140 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002141 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002142 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002143 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002144 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002145 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002146 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002147 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002148 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002149 case OP_NEG_INT:
2150 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002151 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152 case OP_NEG_LONG:
2153 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08002154 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002155 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002156 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002157 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002158 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002159 case OP_MOVE_WIDE:
2160 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002161 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002162 case OP_INT_TO_LONG:
2163 rlSrc = updateLoc(cUnit, rlSrc);
2164 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2165 if (rlSrc.location == kLocPhysReg) {
2166 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2167 } else {
2168 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2169 }
2170 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2171 rlResult.lowReg, 31);
2172 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002173 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002174 case OP_LONG_TO_INT:
2175 rlSrc = updateLocWide(cUnit, rlSrc);
2176 rlSrc = wideToNarrowLoc(cUnit, rlSrc);
2177 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002178 case OP_MOVE:
2179 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002180 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002181 break;
2182 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002183 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2184 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2185 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2186 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002187 break;
2188 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002189 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2190 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2191 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2192 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002193 break;
2194 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002195 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2196 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2197 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2198 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002199 break;
2200 case OP_ARRAY_LENGTH: {
2201 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002202 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2203 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2204 mir->offset, NULL);
2205 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2206 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2207 rlResult.lowReg);
2208 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002209 break;
2210 }
2211 default:
2212 return true;
2213 }
2214 return false;
2215}
2216
2217static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2218{
2219 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002220 RegLocation rlDest;
2221 RegLocation rlResult;
2222 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002223 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002224 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2225 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2226 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
2227 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2228 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002229 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002230 rlDest = getDestLoc(cUnit, mir, 0);
2231 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
2232 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
2233 storeValue(cUnit, rlDest, rlResult);
2234 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002235 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002236 return false;
2237}
2238
2239/* Compare agaist zero */
2240static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002241 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002242{
2243 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002244 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002245 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2246 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2247 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002248
Bill Buzbee270c1d62009-08-13 16:58:07 -07002249//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07002250 switch (dalvikOpCode) {
2251 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002252 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002253 break;
2254 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002255 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002256 break;
2257 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002258 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002259 break;
2260 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002261 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002262 break;
2263 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002264 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002265 break;
2266 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002267 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002268 break;
2269 default:
2270 cond = 0;
2271 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
2272 dvmAbort();
2273 }
2274 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2275 /* This mostly likely will be optimized away in a later phase */
2276 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2277 return false;
2278}
2279
2280static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2281{
2282 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002283 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2284 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2285 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002286 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002287 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002288 int shiftOp = false;
2289 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002290
Ben Chengba4fc8b2009-06-01 13:00:29 -07002291 int __aeabi_idivmod(int op1, int op2);
2292 int __aeabi_idiv(int op1, int op2);
2293
2294 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002295 case OP_RSUB_INT_LIT8:
2296 case OP_RSUB_INT: {
2297 int tReg;
2298 //TUNING: add support for use of Arm rsub op
2299 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2300 tReg = allocTemp(cUnit);
2301 loadConstant(cUnit, tReg, lit);
2302 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2303 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2304 tReg, rlSrc.lowReg);
2305 storeValue(cUnit, rlDest, rlResult);
2306 return false;
2307 break;
2308 }
2309
Ben Chengba4fc8b2009-06-01 13:00:29 -07002310 case OP_ADD_INT_LIT8:
2311 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002312 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002313 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002314 case OP_MUL_INT_LIT8:
2315 case OP_MUL_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002316 op = kOpMul;
2317 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002318 case OP_AND_INT_LIT8:
2319 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002320 op = kOpAnd;
2321 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002322 case OP_OR_INT_LIT8:
2323 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002324 op = kOpOr;
2325 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002326 case OP_XOR_INT_LIT8:
2327 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002328 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002329 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002330 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002331 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002332 shiftOp = true;
2333 op = kOpLsl;
2334 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002335 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002336 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002337 shiftOp = true;
2338 op = kOpAsr;
2339 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002340 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002341 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002342 shiftOp = true;
2343 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002344 break;
2345
2346 case OP_DIV_INT_LIT8:
2347 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002348 case OP_REM_INT_LIT8:
2349 case OP_REM_INT_LIT16:
2350 if (lit == 0) {
2351 /* Let the interpreter deal with div by 0 */
2352 genInterpSingleStep(cUnit, mir);
2353 return false;
2354 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002355 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002356 loadValueDirectFixed(cUnit, rlSrc, r0);
2357 clobberReg(cUnit, r0);
2358 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2359 (dalvikOpCode == OP_DIV_INT_LIT16)) {
2360 loadConstant(cUnit, r2, (int)__aeabi_idiv);
2361 isDiv = true;
2362 } else {
2363 loadConstant(cUnit, r2, (int)__aeabi_idivmod);
2364 isDiv = false;
2365 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002366 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002367 opReg(cUnit, kOpBlx, r2);
2368 clobberCallRegs(cUnit);
2369 if (isDiv)
2370 rlResult = getReturnLoc(cUnit);
2371 else
2372 rlResult = getReturnLocAlt(cUnit);
2373 storeValue(cUnit, rlDest, rlResult);
2374 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002375 break;
2376 default:
2377 return true;
2378 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002379 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2380 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2381 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2382 if (shiftOp && (lit == 0)) {
2383 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2384 } else {
2385 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2386 }
2387 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002388 return false;
2389}
2390
2391static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2392{
2393 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2394 int fieldOffset;
2395
2396 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2397 InstField *pInstField = (InstField *)
2398 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002399
2400 assert(pInstField != NULL);
2401 fieldOffset = pInstField->byteOffset;
2402 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002403 /* Deliberately break the code while make the compiler happy */
2404 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002405 }
2406 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002407 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002408 // Generates a call - use explicit registers
2409 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2410 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2411 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002412 void *classPtr = (void*)
2413 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2414 assert(classPtr != NULL);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002415 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002416 genExportPC(cUnit, mir);
2417 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002418 loadConstant(cUnit, r0, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002419 loadConstant(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002420 /*
2421 * "len < 0": bail to the interpreter to re-execute the
2422 * instruction
2423 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002424 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002425 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002426 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002427 opReg(cUnit, kOpBlx, r3);
2428 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002429 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002430 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2431 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002432 /*
2433 * OOM exception needs to be thrown here and cannot re-execute
2434 */
2435 loadConstant(cUnit, r0,
2436 (int) (cUnit->method->insns + mir->offset));
2437 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2438 /* noreturn */
2439
Bill Buzbee1465db52009-09-23 17:17:35 -07002440 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002441 target->defMask = ENCODE_ALL;
2442 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07002443 rlResult = getReturnLoc(cUnit);
2444 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002445 break;
2446 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002447 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002448 // May generate a call - use explicit registers
2449 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2450 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2451 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002452 ClassObject *classPtr =
2453 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2454 assert(classPtr != NULL);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002455 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002456 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002457 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002458//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002459 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002460 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002461 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002462 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002463 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002464 /* r1 now contains object->clazz */
2465 loadConstant(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002466 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002467 opRegReg(cUnit, kOpCmp, r1, r2);
2468 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2469 genRegCopy(cUnit, r0, r1);
2470 genRegCopy(cUnit, r1, r2);
2471 opReg(cUnit, kOpBlx, r3);
2472 clobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002473 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002474 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002475 target->defMask = ENCODE_ALL;
Bill Buzbee1465db52009-09-23 17:17:35 -07002476 rlResult = getReturnLoc(cUnit);
2477 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002478 branch1->generic.target = (LIR *)target;
2479 branch2->generic.target = (LIR *)target;
2480 break;
2481 }
2482 case OP_IGET_WIDE:
2483 genIGetWide(cUnit, mir, fieldOffset);
2484 break;
2485 case OP_IGET:
2486 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002487 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002488 break;
2489 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002490 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002491 break;
2492 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002493 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002494 break;
2495 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002496 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002497 break;
2498 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002499 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002500 break;
2501 case OP_IPUT_WIDE:
2502 genIPutWide(cUnit, mir, fieldOffset);
2503 break;
2504 case OP_IPUT:
2505 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002506 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002507 break;
2508 case OP_IPUT_SHORT:
2509 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002510 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002511 break;
2512 case OP_IPUT_BYTE:
2513 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002514 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002515 break;
2516 default:
2517 return true;
2518 }
2519 return false;
2520}
2521
2522static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2523{
2524 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2525 int fieldOffset = mir->dalvikInsn.vC;
2526 switch (dalvikOpCode) {
2527 case OP_IGET_QUICK:
2528 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002529 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002530 break;
2531 case OP_IPUT_QUICK:
2532 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002533 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002534 break;
2535 case OP_IGET_WIDE_QUICK:
2536 genIGetWide(cUnit, mir, fieldOffset);
2537 break;
2538 case OP_IPUT_WIDE_QUICK:
2539 genIPutWide(cUnit, mir, fieldOffset);
2540 break;
2541 default:
2542 return true;
2543 }
2544 return false;
2545
2546}
2547
2548/* Compare agaist zero */
2549static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002550 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002551{
2552 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002553 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002554 RegLocation rlSrc1 = getSrcLoc(cUnit, mir, 0);
2555 RegLocation rlSrc2 = getSrcLoc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002556
Bill Buzbee1465db52009-09-23 17:17:35 -07002557 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2558 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2559 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002560
2561 switch (dalvikOpCode) {
2562 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002563 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002564 break;
2565 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002566 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002567 break;
2568 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002569 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002570 break;
2571 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002572 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002573 break;
2574 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002575 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002576 break;
2577 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002578 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002579 break;
2580 default:
2581 cond = 0;
2582 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
2583 dvmAbort();
2584 }
2585 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2586 /* This mostly likely will be optimized away in a later phase */
2587 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2588 return false;
2589}
2590
2591static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2592{
2593 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002594
2595 switch (opCode) {
2596 case OP_MOVE_16:
2597 case OP_MOVE_OBJECT_16:
2598 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002599 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002600 storeValue(cUnit, getDestLoc(cUnit, mir, 0),
2601 getSrcLoc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002602 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002603 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002604 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002605 case OP_MOVE_WIDE_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002606 storeValueWide(cUnit, getDestLocWide(cUnit, mir, 0, 1),
2607 getSrcLocWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002608 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002609 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002610 default:
2611 return true;
2612 }
2613 return false;
2614}
2615
2616static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2617{
2618 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002619 RegLocation rlSrc1;
2620 RegLocation rlSrc2;
2621 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002622
2623 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002624 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002625 }
2626
Bill Buzbee1465db52009-09-23 17:17:35 -07002627 /* APUTs have 3 sources and no targets */
2628 if (mir->ssaRep->numDefs == 0) {
2629 if (mir->ssaRep->numUses == 3) {
2630 rlDest = getSrcLoc(cUnit, mir, 0);
2631 rlSrc1 = getSrcLoc(cUnit, mir, 1);
2632 rlSrc2 = getSrcLoc(cUnit, mir, 2);
2633 } else {
2634 assert(mir->ssaRep->numUses == 4);
2635 rlDest = getSrcLocWide(cUnit, mir, 0, 1);
2636 rlSrc1 = getSrcLoc(cUnit, mir, 2);
2637 rlSrc2 = getSrcLoc(cUnit, mir, 3);
2638 }
2639 } else {
2640 /* Two sources and 1 dest. Deduce the operand sizes */
2641 if (mir->ssaRep->numUses == 4) {
2642 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
2643 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
2644 } else {
2645 assert(mir->ssaRep->numUses == 2);
2646 rlSrc1 = getSrcLoc(cUnit, mir, 0);
2647 rlSrc2 = getSrcLoc(cUnit, mir, 1);
2648 }
2649 if (mir->ssaRep->numDefs == 2) {
2650 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2651 } else {
2652 assert(mir->ssaRep->numDefs == 1);
2653 rlDest = getDestLoc(cUnit, mir, 0);
2654 }
2655 }
2656
2657
Ben Chengba4fc8b2009-06-01 13:00:29 -07002658 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002659 case OP_CMPL_FLOAT:
2660 case OP_CMPG_FLOAT:
2661 case OP_CMPL_DOUBLE:
2662 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002663 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002664 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002665 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002666 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002667 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002668 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002669 break;
2670 case OP_AGET:
2671 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002672 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002673 break;
2674 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002675 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002676 break;
2677 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002678 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002679 break;
2680 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002681 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002682 break;
2683 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002684 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002685 break;
2686 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002687 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002688 break;
2689 case OP_APUT:
2690 case OP_APUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002691 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002692 break;
2693 case OP_APUT_SHORT:
2694 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002695 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002696 break;
2697 case OP_APUT_BYTE:
2698 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002699 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002700 break;
2701 default:
2702 return true;
2703 }
2704 return false;
2705}
2706
Ben Cheng6c10a972009-10-29 14:39:18 -07002707/*
2708 * Find the matching case.
2709 *
2710 * return values:
2711 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2712 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2713 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2714 * above MAX_CHAINED_SWITCH_CASES).
2715 *
2716 * Instructions around the call are:
2717 *
2718 * mov r2, pc
2719 * blx &findPackedSwitchIndex
2720 * mov pc, r0
2721 * .align4
2722 * chaining cell for case 0 [8 bytes]
2723 * chaining cell for case 1 [8 bytes]
2724 * :
2725 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2726 * chaining cell for case default [8 bytes]
2727 * noChain exit
2728 */
2729s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
2730{
2731 int size;
2732 int firstKey;
2733 const int *entries;
2734 int index;
2735 int jumpIndex;
2736 int caseDPCOffset = 0;
2737 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2738 int chainingPC = (pc + 4) & ~3;
2739
2740 /*
2741 * Packed switch data format:
2742 * ushort ident = 0x0100 magic value
2743 * ushort size number of entries in the table
2744 * int first_key first (and lowest) switch case value
2745 * int targets[size] branch targets, relative to switch opcode
2746 *
2747 * Total size is (4+size*2) 16-bit code units.
2748 */
2749 size = switchData[1];
2750 assert(size > 0);
2751
2752 firstKey = switchData[2];
2753 firstKey |= switchData[3] << 16;
2754
2755
2756 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2757 * we can treat them as a native int array.
2758 */
2759 entries = (const int*) &switchData[4];
2760 assert(((u4)entries & 0x3) == 0);
2761
2762 index = testVal - firstKey;
2763
2764 /* Jump to the default cell */
2765 if (index < 0 || index >= size) {
2766 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2767 /* Jump to the non-chaining exit point */
2768 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2769 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2770 caseDPCOffset = entries[index];
2771 /* Jump to the inline chaining cell */
2772 } else {
2773 jumpIndex = index;
2774 }
2775
2776 chainingPC += jumpIndex * 8;
2777 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2778}
2779
2780/* See comments for findPackedSwitchIndex */
2781s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
2782{
2783 int size;
2784 const int *keys;
2785 const int *entries;
2786 int chainingPC = (pc + 4) & ~3;
2787 int i;
2788
2789 /*
2790 * Sparse switch data format:
2791 * ushort ident = 0x0200 magic value
2792 * ushort size number of entries in the table; > 0
2793 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2794 * int targets[size] branch targets, relative to switch opcode
2795 *
2796 * Total size is (2+size*4) 16-bit code units.
2797 */
2798
2799 size = switchData[1];
2800 assert(size > 0);
2801
2802 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2803 * we can treat them as a native int array.
2804 */
2805 keys = (const int*) &switchData[2];
2806 assert(((u4)keys & 0x3) == 0);
2807
2808 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2809 * we can treat them as a native int array.
2810 */
2811 entries = keys + size;
2812 assert(((u4)entries & 0x3) == 0);
2813
2814 /*
2815 * Run through the list of keys, which are guaranteed to
2816 * be sorted low-to-high.
2817 *
2818 * Most tables have 3-4 entries. Few have more than 10. A binary
2819 * search here is probably not useful.
2820 */
2821 for (i = 0; i < size; i++) {
2822 int k = keys[i];
2823 if (k == testVal) {
2824 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2825 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2826 i : MAX_CHAINED_SWITCH_CASES + 1;
2827 chainingPC += jumpIndex * 8;
2828 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2829 } else if (k > testVal) {
2830 break;
2831 }
2832 }
2833 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2834}
2835
Ben Chengba4fc8b2009-06-01 13:00:29 -07002836static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2837{
2838 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2839 switch (dalvikOpCode) {
2840 case OP_FILL_ARRAY_DATA: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002841 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2842 // Making a call - use explicit registers
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002843 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002844 genExportPC(cUnit, mir);
2845 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002846 loadConstant(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002847 loadConstant(cUnit, r1,
2848 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002849 opReg(cUnit, kOpBlx, r2);
2850 clobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002851 /* generate a branch over if successful */
2852 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2853 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2854 loadConstant(cUnit, r0,
2855 (int) (cUnit->method->insns + mir->offset));
2856 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2857 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2858 target->defMask = ENCODE_ALL;
2859 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002860 break;
2861 }
2862 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002863 * Compute the goto target of up to
2864 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2865 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002866 */
2867 case OP_PACKED_SWITCH:
2868 case OP_SPARSE_SWITCH: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002869 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002870 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002871 loadValueDirectFixed(cUnit, rlSrc, r1);
2872 lockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002873 const u2 *switchData =
2874 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2875 u2 size = switchData[1];
2876
Ben Chengba4fc8b2009-06-01 13:00:29 -07002877 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Cheng6c10a972009-10-29 14:39:18 -07002878 loadConstant(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002879 } else {
Ben Cheng6c10a972009-10-29 14:39:18 -07002880 loadConstant(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002881 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002882 /* r0 <- Addr of the switch data */
2883 loadConstant(cUnit, r0,
2884 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2885 /* r2 <- pc of the instruction following the blx */
2886 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002887 opReg(cUnit, kOpBlx, r4PC);
2888 clobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002889 /* pc <- computed goto target */
2890 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002891 break;
2892 }
2893 default:
2894 return true;
2895 }
2896 return false;
2897}
2898
2899static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002900 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002901{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002902 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002903 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002904
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002905 if (bb->fallThrough != NULL)
2906 retChainingCell = &labelList[bb->fallThrough->id];
2907
Ben Chengba4fc8b2009-06-01 13:00:29 -07002908 DecodedInstruction *dInsn = &mir->dalvikInsn;
2909 switch (mir->dalvikInsn.opCode) {
2910 /*
2911 * calleeMethod = this->clazz->vtable[
2912 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2913 * ]
2914 */
2915 case OP_INVOKE_VIRTUAL:
2916 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002917 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002918 int methodIndex =
2919 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2920 methodIndex;
2921
2922 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2923 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2924 else
2925 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2926
Ben Cheng38329f52009-07-07 14:19:20 -07002927 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2928 retChainingCell,
2929 predChainingCell,
2930 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002931 break;
2932 }
2933 /*
2934 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2935 * ->pResMethods[BBBB]->methodIndex]
2936 */
2937 /* TODO - not excersized in RunPerf.jar */
2938 case OP_INVOKE_SUPER:
2939 case OP_INVOKE_SUPER_RANGE: {
2940 int mIndex = cUnit->method->clazz->pDvmDex->
2941 pResMethods[dInsn->vB]->methodIndex;
2942 const Method *calleeMethod =
2943 cUnit->method->clazz->super->vtable[mIndex];
2944
2945 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2946 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2947 else
2948 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2949
2950 /* r0 = calleeMethod */
2951 loadConstant(cUnit, r0, (int) calleeMethod);
2952
Ben Cheng38329f52009-07-07 14:19:20 -07002953 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2954 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002955 break;
2956 }
2957 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2958 case OP_INVOKE_DIRECT:
2959 case OP_INVOKE_DIRECT_RANGE: {
2960 const Method *calleeMethod =
2961 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2962
2963 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2964 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2965 else
2966 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2967
2968 /* r0 = calleeMethod */
2969 loadConstant(cUnit, r0, (int) calleeMethod);
2970
Ben Cheng38329f52009-07-07 14:19:20 -07002971 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2972 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002973 break;
2974 }
2975 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2976 case OP_INVOKE_STATIC:
2977 case OP_INVOKE_STATIC_RANGE: {
2978 const Method *calleeMethod =
2979 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2980
2981 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2982 genProcessArgsNoRange(cUnit, mir, dInsn,
2983 NULL /* no null check */);
2984 else
2985 genProcessArgsRange(cUnit, mir, dInsn,
2986 NULL /* no null check */);
2987
2988 /* r0 = calleeMethod */
2989 loadConstant(cUnit, r0, (int) calleeMethod);
2990
Ben Cheng38329f52009-07-07 14:19:20 -07002991 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2992 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002993 break;
2994 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002995 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002996 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2997 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002998 *
2999 * Given "invoke-interface {v0}", the following is the generated code:
3000 *
3001 * 0x426a9abe : ldr r0, [r5, #0] --+
3002 * 0x426a9ac0 : mov r7, r5 |
3003 * 0x426a9ac2 : sub r7, #24 |
3004 * 0x426a9ac4 : cmp r0, #0 | genProcessArgsNoRange
3005 * 0x426a9ac6 : beq 0x426a9afe |
3006 * 0x426a9ac8 : stmia r7, <r0> --+
3007 * 0x426a9aca : ldr r4, [pc, #104] --> r4 <- dalvikPC of this invoke
3008 * 0x426a9acc : add r1, pc, #52 --> r1 <- &retChainingCell
3009 * 0x426a9ace : add r2, pc, #60 --> r2 <- &predictedChainingCell
3010 * 0x426a9ad0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_
3011 * 0x426a9ad2 : blx_2 see above --+ PREDICTED_CHAIN
3012 * 0x426a9ad4 : b 0x426a9b0c --> off to the predicted chain
3013 * 0x426a9ad6 : b 0x426a9afe --> punt to the interpreter
Ben Chenga8e64a72009-10-20 13:01:36 -07003014 * 0x426a9ad8 : mov r8, r1 --+
3015 * 0x426a9ada : mov r9, r2 |
3016 * 0x426a9adc : mov r10, r3 |
Ben Cheng38329f52009-07-07 14:19:20 -07003017 * 0x426a9ade : mov r0, r3 |
3018 * 0x426a9ae0 : mov r1, #74 | dvmFindInterfaceMethodInCache
3019 * 0x426a9ae2 : ldr r2, [pc, #76] |
3020 * 0x426a9ae4 : ldr r3, [pc, #68] |
3021 * 0x426a9ae6 : ldr r7, [pc, #64] |
3022 * 0x426a9ae8 : blx r7 --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003023 * 0x426a9aea : mov r1, r8 --> r1 <- rechain count
Ben Cheng38329f52009-07-07 14:19:20 -07003024 * 0x426a9aec : cmp r1, #0 --> compare against 0
3025 * 0x426a9aee : bgt 0x426a9af8 --> >=0? don't rechain
3026 * 0x426a9af0 : ldr r7, [r6, #96] --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003027 * 0x426a9af2 : mov r2, r9 | dvmJitToPatchPredictedChain
3028 * 0x426a9af4 : mov r3, r10 |
Ben Cheng38329f52009-07-07 14:19:20 -07003029 * 0x426a9af6 : blx r7 --+
3030 * 0x426a9af8 : add r1, pc, #8 --> r1 <- &retChainingCell
3031 * 0x426a9afa : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3032 * 0x426a9afc : blx_2 see above --+
3033 * -------- reconstruct dalvik PC : 0x428b786c @ +0x001e
3034 * 0x426a9afe (0042): ldr r0, [pc, #52]
3035 * Exception_Handling:
3036 * 0x426a9b00 (0044): ldr r1, [r6, #84]
3037 * 0x426a9b02 (0046): blx r1
3038 * 0x426a9b04 (0048): .align4
3039 * -------- chaining cell (hot): 0x0021
3040 * 0x426a9b04 (0048): ldr r0, [r6, #92]
3041 * 0x426a9b06 (004a): blx r0
3042 * 0x426a9b08 (004c): data 0x7872(30834)
3043 * 0x426a9b0a (004e): data 0x428b(17035)
3044 * 0x426a9b0c (0050): .align4
3045 * -------- chaining cell (predicted)
3046 * 0x426a9b0c (0050): data 0x0000(0) --> will be patched into bx
3047 * 0x426a9b0e (0052): data 0x0000(0)
3048 * 0x426a9b10 (0054): data 0x0000(0) --> class
3049 * 0x426a9b12 (0056): data 0x0000(0)
3050 * 0x426a9b14 (0058): data 0x0000(0) --> method
3051 * 0x426a9b16 (005a): data 0x0000(0)
3052 * 0x426a9b18 (005c): data 0x0000(0) --> reset count
3053 * 0x426a9b1a (005e): data 0x0000(0)
3054 * 0x426a9b28 (006c): .word (0xad0392a5)
3055 * 0x426a9b2c (0070): .word (0x6e750)
3056 * 0x426a9b30 (0074): .word (0x4109a618)
3057 * 0x426a9b34 (0078): .word (0x428b786c)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003058 */
3059 case OP_INVOKE_INTERFACE:
3060 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003061 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003062 int methodIndex = dInsn->vB;
3063
Bill Buzbee1465db52009-09-23 17:17:35 -07003064 /* Ensure that nothing is both live and dirty */
3065 flushAllRegs(cUnit);
3066
Ben Chengba4fc8b2009-06-01 13:00:29 -07003067 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
3068 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3069 else
3070 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3071
Ben Cheng38329f52009-07-07 14:19:20 -07003072 /* "this" is already left in r0 by genProcessArgs* */
3073
3074 /* r4PC = dalvikCallsite */
3075 loadConstant(cUnit, r4PC,
3076 (int) (cUnit->method->insns + mir->offset));
3077
3078 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003079 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003080 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003081 addrRetChain->generic.target = (LIR *) retChainingCell;
3082
3083 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003084 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003085 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003086 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3087
3088 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
3089
3090 /* return through lr - jump to the chaining cell */
3091 genUnconditionalBranch(cUnit, predChainingCell);
3092
3093 /*
3094 * null-check on "this" may have been eliminated, but we still need
3095 * a PC-reconstruction label for stack overflow bailout.
3096 */
3097 if (pcrLabel == NULL) {
3098 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003099 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003100 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07003101 pcrLabel->operands[0] = dPC;
3102 pcrLabel->operands[1] = mir->offset;
3103 /* Insert the place holder to the growable list */
3104 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3105 }
3106
3107 /* return through lr+2 - punt to the interpreter */
3108 genUnconditionalBranch(cUnit, pcrLabel);
3109
3110 /*
3111 * return through lr+4 - fully resolve the callee method.
3112 * r1 <- count
3113 * r2 <- &predictedChainCell
3114 * r3 <- this->class
3115 * r4 <- dPC
3116 * r7 <- this->class->vtable
3117 */
3118
3119 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003120 genRegCopy(cUnit, r8, r1);
3121 genRegCopy(cUnit, r9, r2);
3122 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003123
Ben Chengba4fc8b2009-06-01 13:00:29 -07003124 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003125 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003126
3127 /* r1 = BBBB */
3128 loadConstant(cUnit, r1, dInsn->vB);
3129
3130 /* r2 = method (caller) */
3131 loadConstant(cUnit, r2, (int) cUnit->method);
3132
3133 /* r3 = pDvmDex */
3134 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3135
3136 loadConstant(cUnit, r7,
3137 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003138 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003139
3140 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3141
Bill Buzbee1465db52009-09-23 17:17:35 -07003142 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003143
Ben Cheng38329f52009-07-07 14:19:20 -07003144 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07003145 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003146
Bill Buzbee1465db52009-09-23 17:17:35 -07003147 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07003148
Bill Buzbee270c1d62009-08-13 16:58:07 -07003149 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3150 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003151
Bill Buzbee1465db52009-09-23 17:17:35 -07003152 genRegCopy(cUnit, r2, r9);
3153 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003154
3155 /*
3156 * r0 = calleeMethod
3157 * r2 = &predictedChainingCell
3158 * r3 = class
3159 *
3160 * &returnChainingCell has been loaded into r1 but is not needed
3161 * when patching the chaining cell and will be clobbered upon
3162 * returning so it will be reconstructed again.
3163 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003164 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003165
3166 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003167 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003168 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003169
3170 bypassRechaining->generic.target = (LIR *) addrRetChain;
3171
Ben Chengba4fc8b2009-06-01 13:00:29 -07003172 /*
3173 * r0 = this, r1 = calleeMethod,
3174 * r1 = &ChainingCell,
3175 * r4PC = callsiteDPC,
3176 */
3177 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
3178#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07003179 gDvmJit.invokePredictedChain++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003180#endif
3181 /* Handle exceptions using the interpreter */
3182 genTrap(cUnit, mir->offset, pcrLabel);
3183 break;
3184 }
3185 /* NOP */
3186 case OP_INVOKE_DIRECT_EMPTY: {
3187 return false;
3188 }
3189 case OP_FILLED_NEW_ARRAY:
3190 case OP_FILLED_NEW_ARRAY_RANGE: {
3191 /* Just let the interpreter deal with these */
3192 genInterpSingleStep(cUnit, mir);
3193 break;
3194 }
3195 default:
3196 return true;
3197 }
3198 return false;
3199}
3200
3201static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003202 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003203{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003204 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3205 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3206 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003207
3208 DecodedInstruction *dInsn = &mir->dalvikInsn;
3209 switch (mir->dalvikInsn.opCode) {
3210 /* calleeMethod = this->clazz->vtable[BBBB] */
3211 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3212 case OP_INVOKE_VIRTUAL_QUICK: {
3213 int methodIndex = dInsn->vB;
3214 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3215 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3216 else
3217 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3218
Ben Cheng38329f52009-07-07 14:19:20 -07003219 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3220 retChainingCell,
3221 predChainingCell,
3222 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003223 break;
3224 }
3225 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3226 case OP_INVOKE_SUPER_QUICK:
3227 case OP_INVOKE_SUPER_QUICK_RANGE: {
3228 const Method *calleeMethod =
3229 cUnit->method->clazz->super->vtable[dInsn->vB];
3230
3231 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3232 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3233 else
3234 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3235
3236 /* r0 = calleeMethod */
3237 loadConstant(cUnit, r0, (int) calleeMethod);
3238
Ben Cheng38329f52009-07-07 14:19:20 -07003239 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3240 calleeMethod);
3241 /* Handle exceptions using the interpreter */
3242 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003243 break;
3244 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003245 default:
3246 return true;
3247 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003248 return false;
3249}
3250
3251/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003252 * This operation is complex enough that we'll do it partly inline
3253 * and partly with a handler. NOTE: the handler uses hardcoded
3254 * values for string object offsets and must be revisitied if the
3255 * layout changes.
3256 */
3257static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3258{
3259#if defined(USE_GLOBAL_STRING_DEFS)
3260 return false;
3261#else
3262 ArmLIR *rollback;
3263 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3264 RegLocation rlComp = getSrcLoc(cUnit, mir, 1);
3265
3266 loadValueDirectFixed(cUnit, rlThis, r0);
3267 loadValueDirectFixed(cUnit, rlComp, r1);
3268 /* Test objects for NULL */
3269 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3270 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3271 /*
3272 * TUNING: we could check for object pointer equality before invoking
3273 * handler. Unclear whether the gain would be worth the added code size
3274 * expansion.
3275 */
3276 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
3277 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3278 return true;
3279#endif
3280}
3281
3282static bool genInlinedIndexOf(CompilationUnit *cUnit, MIR *mir, bool singleI)
3283{
3284#if defined(USE_GLOBAL_STRING_DEFS)
3285 return false;
3286#else
3287 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3288 RegLocation rlChar = getSrcLoc(cUnit, mir, 1);
3289
3290 loadValueDirectFixed(cUnit, rlThis, r0);
3291 loadValueDirectFixed(cUnit, rlChar, r1);
3292 if (!singleI) {
3293 RegLocation rlStart = getSrcLoc(cUnit, mir, 2);
3294 loadValueDirectFixed(cUnit, rlStart, r2);
3295 } else {
3296 loadConstant(cUnit, r2, 0);
3297 }
3298 /* Test objects for NULL */
3299 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3300 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
3301 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3302 return true;
3303#endif
3304}
3305
3306
3307/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003308 * NOTE: Handles both range and non-range versions (arguments
3309 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003310 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003311static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003312{
3313 DecodedInstruction *dInsn = &mir->dalvikInsn;
3314 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003315 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003316 case OP_EXECUTE_INLINE: {
3317 unsigned int i;
3318 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003319 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003320 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003321 int tReg1;
3322 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003323 switch (operation) {
3324 case INLINE_EMPTYINLINEMETHOD:
3325 return false; /* Nop */
3326 case INLINE_STRING_LENGTH:
3327 return genInlinedStringLength(cUnit, mir);
3328 case INLINE_MATH_ABS_INT:
3329 return genInlinedAbsInt(cUnit, mir);
3330 case INLINE_MATH_ABS_LONG:
3331 return genInlinedAbsLong(cUnit, mir);
3332 case INLINE_MATH_MIN_INT:
3333 return genInlinedMinMaxInt(cUnit, mir, true);
3334 case INLINE_MATH_MAX_INT:
3335 return genInlinedMinMaxInt(cUnit, mir, false);
3336 case INLINE_STRING_CHARAT:
3337 return genInlinedStringCharAt(cUnit, mir);
3338 case INLINE_MATH_SQRT:
3339 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003340 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003341 else
3342 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003343 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003344 if (genInlinedAbsFloat(cUnit, mir))
3345 return false;
3346 else
3347 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003348 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003349 if (genInlinedAbsDouble(cUnit, mir))
3350 return false;
3351 else
3352 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003353 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003354 if (genInlinedCompareTo(cUnit, mir))
3355 return false;
3356 else
3357 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003358 case INLINE_STRING_INDEXOF_I:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003359 if (genInlinedIndexOf(cUnit, mir, true /* I */))
3360 return false;
3361 else
3362 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003363 case INLINE_STRING_INDEXOF_II:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003364 if (genInlinedIndexOf(cUnit, mir, false /* I */))
3365 return false;
3366 else
3367 break;
3368 case INLINE_STRING_EQUALS:
3369 case INLINE_MATH_COS:
3370 case INLINE_MATH_SIN:
3371 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003372 default:
3373 dvmAbort();
Ben Chengba4fc8b2009-06-01 13:00:29 -07003374 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003375 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07003376 clobberCallRegs(cUnit);
3377 clobberReg(cUnit, r4PC);
3378 clobberReg(cUnit, r7);
3379 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3380 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003381 loadConstant(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003382 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003383 for (i=0; i < dInsn->vA; i++) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003384 loadValueDirect(cUnit, getSrcLoc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003385 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003386 opReg(cUnit, kOpBlx, r4PC);
3387 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003388 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3389 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3390 loadConstant(cUnit, r0,
3391 (int) (cUnit->method->insns + mir->offset));
3392 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3393 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3394 target->defMask = ENCODE_ALL;
3395 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003396 break;
3397 }
3398 default:
3399 return true;
3400 }
3401 return false;
3402}
3403
3404static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3405{
Bill Buzbee1465db52009-09-23 17:17:35 -07003406 //TUNING: We're using core regs here - not optimal when target is a double
3407 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
3408 RegLocation rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
3409 loadConstantValue(cUnit, rlResult.lowReg,
3410 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3411 loadConstantValue(cUnit, rlResult.highReg,
3412 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
3413 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003414 return false;
3415}
3416
Ben Chengba4fc8b2009-06-01 13:00:29 -07003417/*
3418 * The following are special processing routines that handle transfer of
3419 * controls between compiled code and the interpreter. Certain VM states like
3420 * Dalvik PC and special-purpose registers are reconstructed here.
3421 */
3422
Ben Cheng1efc9c52009-06-08 18:25:27 -07003423/* Chaining cell for code that may need warmup. */
3424static void handleNormalChainingCell(CompilationUnit *cUnit,
3425 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003426{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003427 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3428 jitToInterpEntries.dvmJitToInterpNormal), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003429 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003430 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3431}
3432
3433/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003434 * Chaining cell for instructions that immediately following already translated
3435 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003436 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003437static void handleHotChainingCell(CompilationUnit *cUnit,
3438 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003439{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003440 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3441 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003442 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003443 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3444}
3445
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003446#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003447/* Chaining cell for branches that branch back into the same basic block */
3448static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3449 unsigned int offset)
3450{
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003451#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003452 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Jeff Hao97319a82009-08-12 16:57:15 -07003453 offsetof(InterpState, jitToInterpEntries.dvmJitToBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003454#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003455 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003456 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3457#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003458 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003459 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3460}
3461
3462#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003463/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003464static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3465 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003466{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003467 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3468 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003469 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003470 addWordData(cUnit, (int) (callee->insns), true);
3471}
3472
Ben Cheng38329f52009-07-07 14:19:20 -07003473/* Chaining cell for monomorphic method invocations. */
3474static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3475{
3476
3477 /* Should not be executed in the initial state */
3478 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3479 /* To be filled: class */
3480 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3481 /* To be filled: method */
3482 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3483 /*
3484 * Rechain count. The initial value of 0 here will trigger chaining upon
3485 * the first invocation of this callsite.
3486 */
3487 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3488}
3489
Ben Chengba4fc8b2009-06-01 13:00:29 -07003490/* Load the Dalvik PC into r0 and jump to the specified target */
3491static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003492 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003493{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003494 ArmLIR **pcrLabel =
3495 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003496 int numElems = cUnit->pcReconstructionList.numUsed;
3497 int i;
3498 for (i = 0; i < numElems; i++) {
3499 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3500 /* r0 = dalvik PC */
3501 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3502 genUnconditionalBranch(cUnit, targetLabel);
3503 }
3504}
3505
Bill Buzbee1465db52009-09-23 17:17:35 -07003506static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3507 "kMirOpPhi",
3508 "kMirOpNullNRangeUpCheck",
3509 "kMirOpNullNRangeDownCheck",
3510 "kMirOpLowerBound",
3511 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003512};
3513
3514/*
3515 * vA = arrayReg;
3516 * vB = idxReg;
3517 * vC = endConditionReg;
3518 * arg[0] = maxC
3519 * arg[1] = minC
3520 * arg[2] = loopBranchConditionCode
3521 */
3522static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3523{
Bill Buzbee1465db52009-09-23 17:17:35 -07003524 /*
3525 * NOTE: these synthesized blocks don't have ssa names assigned
3526 * for Dalvik registers. However, because they dominate the following
3527 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3528 * ssa name.
3529 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003530 DecodedInstruction *dInsn = &mir->dalvikInsn;
3531 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003532 const int maxC = dInsn->arg[0];
3533 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003534 int regLength;
3535 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3536 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003537
3538 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003539 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3540 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3541 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003542 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3543
3544 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003545 regLength = allocTemp(cUnit);
3546 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003547
3548 int delta = maxC;
3549 /*
3550 * If the loop end condition is ">=" instead of ">", then the largest value
3551 * of the index is "endCondition - 1".
3552 */
3553 if (dInsn->arg[2] == OP_IF_GE) {
3554 delta--;
3555 }
3556
3557 if (delta) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003558 int tReg = allocTemp(cUnit);
3559 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3560 rlIdxEnd.lowReg = tReg;
3561 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003562 }
3563 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003564 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003565 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003566}
3567
3568/*
3569 * vA = arrayReg;
3570 * vB = idxReg;
3571 * vC = endConditionReg;
3572 * arg[0] = maxC
3573 * arg[1] = minC
3574 * arg[2] = loopBranchConditionCode
3575 */
3576static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3577{
3578 DecodedInstruction *dInsn = &mir->dalvikInsn;
3579 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07003580 const int regLength = allocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003581 const int maxC = dInsn->arg[0];
3582 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003583 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3584 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003585
3586 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003587 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3588 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3589 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003590 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3591
3592 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003593 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003594
3595 if (maxC) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003596 int tReg = allocTemp(cUnit);
3597 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3598 rlIdxInit.lowReg = tReg;
3599 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003600 }
3601
3602 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003603 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003604 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003605}
3606
3607/*
3608 * vA = idxReg;
3609 * vB = minC;
3610 */
3611static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3612{
3613 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003614 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003615 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003616
3617 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003618 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003619
3620 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003621 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003622 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3623}
3624
3625/* Extended MIR instructions like PHI */
3626static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3627{
Bill Buzbee1465db52009-09-23 17:17:35 -07003628 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003629 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3630 false);
3631 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003632 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003633
3634 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003635 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003636 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003637 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003638 break;
3639 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003640 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003641 genHoistedChecksForCountUpLoop(cUnit, mir);
3642 break;
3643 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003644 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003645 genHoistedChecksForCountDownLoop(cUnit, mir);
3646 break;
3647 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003648 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003649 genHoistedLowerBoundCheck(cUnit, mir);
3650 break;
3651 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003652 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003653 genUnconditionalBranch(cUnit,
3654 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3655 break;
3656 }
3657 default:
3658 break;
3659 }
3660}
3661
3662/*
3663 * Create a PC-reconstruction cell for the starting offset of this trace.
3664 * Since the PCR cell is placed near the end of the compiled code which is
3665 * usually out of range for a conditional branch, we put two branches (one
3666 * branch over to the loop body and one layover branch to the actual PCR) at the
3667 * end of the entry block.
3668 */
3669static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3670 ArmLIR *bodyLabel)
3671{
3672 /* Set up the place holder to reconstruct this Dalvik PC */
3673 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003674 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng4238ec22009-08-24 16:32:22 -07003675 pcrLabel->operands[0] =
3676 (int) (cUnit->method->insns + entry->startOffset);
3677 pcrLabel->operands[1] = entry->startOffset;
3678 /* Insert the place holder to the growable list */
3679 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3680
3681 /*
3682 * Next, create two branches - one branch over to the loop body and the
3683 * other branch to the PCR cell to punt.
3684 */
3685 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003686 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003687 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003688 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003689 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3690
3691 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003692 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003693 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003694 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003695 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3696}
3697
Ben Chengba4fc8b2009-06-01 13:00:29 -07003698void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3699{
3700 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003701 ArmLIR *labelList =
3702 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003703 GrowableList chainingListByType[kChainingCellLast];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003704 int i;
3705
3706 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003707 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003708 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003709 for (i = 0; i < kChainingCellLast; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003710 dvmInitGrowableList(&chainingListByType[i], 2);
3711 }
3712
3713 BasicBlock **blockList = cUnit->blockList;
3714
Bill Buzbee6e963e12009-06-17 16:56:19 -07003715 if (cUnit->executionCount) {
3716 /*
3717 * Reserve 6 bytes at the beginning of the trace
3718 * +----------------------------+
3719 * | execution count (4 bytes) |
3720 * +----------------------------+
3721 * | chain cell offset (2 bytes)|
3722 * +----------------------------+
3723 * ...and then code to increment the execution
3724 * count:
3725 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3726 * sub r0, #10 @ back up to addr of executionCount
3727 * ldr r1, [r0]
3728 * add r1, #1
3729 * str r1, [r0]
3730 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003731 newLIR1(cUnit, kArm16BitData, 0);
3732 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003733 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003734 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003735 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003736 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003737 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3738 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3739 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3740 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3741 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003742 } else {
3743 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003744 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003745 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003746 cUnit->headerSize = 2;
3747 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003748
Ben Chengba4fc8b2009-06-01 13:00:29 -07003749 /* Handle the content in each basic block */
3750 for (i = 0; i < cUnit->numBlocks; i++) {
3751 blockList[i]->visited = true;
3752 MIR *mir;
3753
3754 labelList[i].operands[0] = blockList[i]->startOffset;
3755
Bill Buzbee1465db52009-09-23 17:17:35 -07003756 if (blockList[i]->blockType >= kChainingCellLast) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003757 /*
3758 * Append the label pseudo LIR first. Chaining cells will be handled
3759 * separately afterwards.
3760 */
3761 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3762 }
3763
Bill Buzbee1465db52009-09-23 17:17:35 -07003764 if (blockList[i]->blockType == kEntryBlock) {
3765 labelList[i].opCode = ARM_PSEUDO_kEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003766 if (blockList[i]->firstMIRInsn == NULL) {
3767 continue;
3768 } else {
3769 setupLoopEntryBlock(cUnit, blockList[i],
3770 &labelList[blockList[i]->fallThrough->id]);
3771 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003772 } else if (blockList[i]->blockType == kExitBlock) {
3773 labelList[i].opCode = ARM_PSEUDO_kExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003774 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003775 } else if (blockList[i]->blockType == kDalvikByteCode) {
3776 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003777 /* Reset the register state */
Bill Buzbee1465db52009-09-23 17:17:35 -07003778 resetRegPool(cUnit);
3779 clobberAllRegs(cUnit);
3780 resetNullCheckTracker(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003781 } else {
3782 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003783 case kChainingCellNormal:
3784 labelList[i].opCode = ARM_PSEUDO_kChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003785 /* handle the codegen later */
3786 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003787 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003788 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003789 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003790 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003791 ARM_PSEUDO_kChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003792 labelList[i].operands[0] =
3793 (int) blockList[i]->containingMethod;
3794 /* handle the codegen later */
3795 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003796 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003797 (void *) i);
3798 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003799 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003800 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003801 ARM_PSEUDO_kChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003802 /* handle the codegen later */
3803 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003804 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003805 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003806 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003807 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003808 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003809 ARM_PSEUDO_kChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003810 /* handle the codegen later */
3811 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003812 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003813 (void *) i);
3814 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003815 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003816 /* Make sure exception handling block is next */
3817 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003818 ARM_PSEUDO_kPCReconstruction_BLOCK_LABEL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003819 assert (i == cUnit->numBlocks - 2);
3820 handlePCReconstruction(cUnit, &labelList[i+1]);
3821 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003822 case kExceptionHandling:
3823 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003824 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003825 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3826 jitToInterpEntries.dvmJitToInterpPunt),
3827 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003828 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003829 }
3830 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003831#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003832 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003833 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003834 ARM_PSEUDO_kChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003835 /* handle the codegen later */
3836 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003837 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003838 (void *) i);
3839 break;
3840#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003841 default:
3842 break;
3843 }
3844 continue;
3845 }
Ben Chenge9695e52009-06-16 16:11:47 -07003846
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003847 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003848
Ben Chengba4fc8b2009-06-01 13:00:29 -07003849 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003850
3851 resetRegPool(cUnit);
3852 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
3853 clobberAllRegs(cUnit);
3854 }
3855
3856 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
3857 resetDefTracking(cUnit);
3858 }
3859
3860 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003861 handleExtendedMIR(cUnit, mir);
3862 continue;
3863 }
3864
Bill Buzbee1465db52009-09-23 17:17:35 -07003865
Ben Chengba4fc8b2009-06-01 13:00:29 -07003866 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3867 InstructionFormat dalvikFormat =
3868 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003869 ArmLIR *boundaryLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003870 newLIR2(cUnit, ARM_PSEUDO_kDalvikByteCode_BOUNDARY,
Ben Chengccd6c012009-10-15 14:52:45 -07003871 mir->offset,
3872 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3873 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003874 if (mir->ssaRep) {
3875 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003876 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003877 }
3878
Ben Chenge9695e52009-06-16 16:11:47 -07003879 /* Remember the first LIR for this block */
3880 if (headLIR == NULL) {
3881 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003882 /* Set the first boundaryLIR as a scheduling barrier */
3883 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003884 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003885
Ben Chengba4fc8b2009-06-01 13:00:29 -07003886 bool notHandled;
3887 /*
3888 * Debugging: screen the opcode first to see if it is in the
3889 * do[-not]-compile list
3890 */
3891 bool singleStepMe =
3892 gDvmJit.includeSelectedOp !=
3893 ((gDvmJit.opList[dalvikOpCode >> 3] &
3894 (1 << (dalvikOpCode & 0x7))) !=
3895 0);
3896 if (singleStepMe || cUnit->allSingleStep) {
3897 notHandled = false;
3898 genInterpSingleStep(cUnit, mir);
3899 } else {
3900 opcodeCoverage[dalvikOpCode]++;
3901 switch (dalvikFormat) {
3902 case kFmt10t:
3903 case kFmt20t:
3904 case kFmt30t:
3905 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3906 mir, blockList[i], labelList);
3907 break;
3908 case kFmt10x:
3909 notHandled = handleFmt10x(cUnit, mir);
3910 break;
3911 case kFmt11n:
3912 case kFmt31i:
3913 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3914 break;
3915 case kFmt11x:
3916 notHandled = handleFmt11x(cUnit, mir);
3917 break;
3918 case kFmt12x:
3919 notHandled = handleFmt12x(cUnit, mir);
3920 break;
3921 case kFmt20bc:
3922 notHandled = handleFmt20bc(cUnit, mir);
3923 break;
3924 case kFmt21c:
3925 case kFmt31c:
3926 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3927 break;
3928 case kFmt21h:
3929 notHandled = handleFmt21h(cUnit, mir);
3930 break;
3931 case kFmt21s:
3932 notHandled = handleFmt21s(cUnit, mir);
3933 break;
3934 case kFmt21t:
3935 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3936 labelList);
3937 break;
3938 case kFmt22b:
3939 case kFmt22s:
3940 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3941 break;
3942 case kFmt22c:
3943 notHandled = handleFmt22c(cUnit, mir);
3944 break;
3945 case kFmt22cs:
3946 notHandled = handleFmt22cs(cUnit, mir);
3947 break;
3948 case kFmt22t:
3949 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3950 labelList);
3951 break;
3952 case kFmt22x:
3953 case kFmt32x:
3954 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3955 break;
3956 case kFmt23x:
3957 notHandled = handleFmt23x(cUnit, mir);
3958 break;
3959 case kFmt31t:
3960 notHandled = handleFmt31t(cUnit, mir);
3961 break;
3962 case kFmt3rc:
3963 case kFmt35c:
3964 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3965 labelList);
3966 break;
3967 case kFmt3rms:
3968 case kFmt35ms:
3969 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3970 labelList);
3971 break;
3972 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003973 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08003974 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08003975 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003976 case kFmt51l:
3977 notHandled = handleFmt51l(cUnit, mir);
3978 break;
3979 default:
3980 notHandled = true;
3981 break;
3982 }
3983 }
3984 if (notHandled) {
3985 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3986 mir->offset,
3987 dalvikOpCode, getOpcodeName(dalvikOpCode),
3988 dalvikFormat);
3989 dvmAbort();
3990 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003991 }
3992 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003993
Bill Buzbee1465db52009-09-23 17:17:35 -07003994 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003995 dvmCompilerAppendLIR(cUnit,
3996 (LIR *) cUnit->loopAnalysis->branchToBody);
3997 dvmCompilerAppendLIR(cUnit,
3998 (LIR *) cUnit->loopAnalysis->branchToPCR);
3999 }
4000
4001 if (headLIR) {
4002 /*
4003 * Eliminate redundant loads/stores and delay stores into later
4004 * slots
4005 */
4006 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4007 cUnit->lastLIRInsn);
4008 }
4009
4010gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004011 /*
4012 * Check if the block is terminated due to trace length constraint -
4013 * insert an unconditional branch to the chaining cell.
4014 */
4015 if (blockList[i]->needFallThroughBranch) {
4016 genUnconditionalBranch(cUnit,
4017 &labelList[blockList[i]->fallThrough->id]);
4018 }
4019
Ben Chengba4fc8b2009-06-01 13:00:29 -07004020 }
4021
Ben Chenge9695e52009-06-16 16:11:47 -07004022 /* Handle the chaining cells in predefined order */
Bill Buzbee1465db52009-09-23 17:17:35 -07004023 for (i = 0; i < kChainingCellLast; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004024 size_t j;
4025 int *blockIdList = (int *) chainingListByType[i].elemList;
4026
4027 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4028
4029 /* No chaining cells of this type */
4030 if (cUnit->numChainingCells[i] == 0)
4031 continue;
4032
4033 /* Record the first LIR for a new type of chaining cell */
4034 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4035
4036 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4037 int blockId = blockIdList[j];
4038
4039 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004040 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004041
4042 /* Insert the pseudo chaining instruction */
4043 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4044
4045
4046 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004047 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004048 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004049 blockList[blockId]->startOffset);
4050 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004051 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004052 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004053 blockList[blockId]->containingMethod);
4054 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004055 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004056 handleInvokePredictedChainingCell(cUnit);
4057 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004058 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004059 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004060 blockList[blockId]->startOffset);
4061 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004062#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004063 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004064 handleBackwardBranchChainingCell(cUnit,
4065 blockList[blockId]->startOffset);
4066 break;
4067#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004068 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004069 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004070 dvmAbort();
4071 break;
4072 }
4073 }
4074 }
Ben Chenge9695e52009-06-16 16:11:47 -07004075
Ben Cheng6c10a972009-10-29 14:39:18 -07004076 /*
4077 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4078 * of all chaining cells for the overflow cases.
4079 */
4080 if (cUnit->switchOverflowPad) {
4081 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4082 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4083 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4084 opRegReg(cUnit, kOpAdd, r1, r1);
4085 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
4086#if defined(EXIT_STATS)
4087 loadConstant(cUnit, r0, kSwitchOverflow);
4088#endif
4089 opReg(cUnit, kOpBlx, r2);
4090 }
4091
Ben Chenge9695e52009-06-16 16:11:47 -07004092 dvmCompilerApplyGlobalOptimizations(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004093}
4094
4095/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004096bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004097{
Ben Chengccd6c012009-10-15 14:52:45 -07004098 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004099
Ben Chengccd6c012009-10-15 14:52:45 -07004100 if (gDvmJit.codeCacheFull) {
4101 return false;
4102 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004103
Ben Chengccd6c012009-10-15 14:52:45 -07004104 switch (work->kind) {
4105 case kWorkOrderMethod:
4106 res = dvmCompileMethod(work->info, &work->result);
4107 break;
4108 case kWorkOrderTrace:
4109 /* Start compilation with maximally allowed trace length */
4110 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4111 break;
4112 case kWorkOrderTraceDebug: {
4113 bool oldPrintMe = gDvmJit.printMe;
4114 gDvmJit.printMe = true;
4115 /* Start compilation with maximally allowed trace length */
4116 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4117 gDvmJit.printMe = oldPrintMe;;
4118 break;
4119 }
4120 default:
4121 res = false;
4122 dvmAbort();
4123 }
4124 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004125}
4126
Ben Chengba4fc8b2009-06-01 13:00:29 -07004127/* Architectural-specific debugging helpers go here */
4128void dvmCompilerArchDump(void)
4129{
4130 /* Print compiled opcode in this VM instance */
4131 int i, start, streak;
4132 char buf[1024];
4133
4134 streak = i = 0;
4135 buf[0] = 0;
4136 while (opcodeCoverage[i] == 0 && i < 256) {
4137 i++;
4138 }
4139 if (i == 256) {
4140 return;
4141 }
4142 for (start = i++, streak = 1; i < 256; i++) {
4143 if (opcodeCoverage[i]) {
4144 streak++;
4145 } else {
4146 if (streak == 1) {
4147 sprintf(buf+strlen(buf), "%x,", start);
4148 } else {
4149 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4150 }
4151 streak = 0;
4152 while (opcodeCoverage[i] == 0 && i < 256) {
4153 i++;
4154 }
4155 if (i < 256) {
4156 streak = 1;
4157 start = i;
4158 }
4159 }
4160 }
4161 if (streak) {
4162 if (streak == 1) {
4163 sprintf(buf+strlen(buf), "%x", start);
4164 } else {
4165 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4166 }
4167 }
4168 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004169 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004170 }
4171}
Ben Chengd7d426a2009-09-22 11:23:36 -07004172
4173/* Common initialization routine for an architecture family */
4174bool dvmCompilerArchInit()
4175{
4176 int i;
4177
Bill Buzbee1465db52009-09-23 17:17:35 -07004178 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004179 if (EncodingMap[i].opCode != i) {
4180 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4181 EncodingMap[i].name, i, EncodingMap[i].opCode);
4182 dvmAbort();
4183 }
4184 }
4185
Ben Cheng5d90c202009-11-22 23:31:11 -08004186 return dvmCompilerArchVariantInit();
4187}
4188
4189void *dvmCompilerGetInterpretTemplate()
4190{
4191 return (void*) ((int)gDvmJit.codeCache +
4192 templateEntryOffsets[TEMPLATE_INTERPRET]);
4193}
4194
4195/* Needed by the ld/st optmizatons */
4196ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4197{
4198 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4199}
4200
4201/* Needed by the register allocator */
4202ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4203{
4204 return genRegCopy(cUnit, rDest, rSrc);
4205}
4206
4207/* Needed by the register allocator */
4208void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4209 int srcLo, int srcHi)
4210{
4211 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4212}
4213
4214void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4215 int displacement, int rSrc, OpSize size)
4216{
4217 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4218}
4219
4220void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4221 int displacement, int rSrcLo, int rSrcHi)
4222{
4223 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004224}