blob: 648d8f4d62d732e16efda13792866d509c9512f5 [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bill Buzbee50a6bf22009-07-08 13:08:04 -070017/*
18 * This file contains codegen and support common to all supported
19 * ARM variants. It is included by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 * which combines this common code with specific support found in the
24 * applicable directory below this one.
25 */
26
Ben Cheng5d90c202009-11-22 23:31:11 -080027static bool genConversionCall(CompilationUnit *cUnit, MIR *mir, void *funct,
28 int srcSize, int tgtSize)
29{
30 /*
31 * Don't optimize the register usage since it calls out to template
32 * functions
33 */
34 RegLocation rlSrc;
35 RegLocation rlDest;
36 flushAllRegs(cUnit); /* Send everything to home location */
37 if (srcSize == 1) {
38 rlSrc = getSrcLoc(cUnit, mir, 0);
39 loadValueDirectFixed(cUnit, rlSrc, r0);
40 } else {
41 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
42 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
43 }
44 loadConstant(cUnit, r2, (int)funct);
45 opReg(cUnit, kOpBlx, r2);
46 clobberCallRegs(cUnit);
47 if (tgtSize == 1) {
48 RegLocation rlResult;
49 rlDest = getDestLoc(cUnit, mir, 0);
50 rlResult = getReturnLoc(cUnit);
51 storeValue(cUnit, rlDest, rlResult);
52 } else {
53 RegLocation rlResult;
54 rlDest = getDestLocWide(cUnit, mir, 0, 1);
55 rlResult = getReturnLocWide(cUnit);
56 storeValueWide(cUnit, rlDest, rlResult);
57 }
58 return false;
59}
Ben Chengba4fc8b2009-06-01 13:00:29 -070060
Ben Chengba4fc8b2009-06-01 13:00:29 -070061
Ben Cheng5d90c202009-11-22 23:31:11 -080062static bool genArithOpFloatPortable(CompilationUnit *cUnit, MIR *mir,
63 RegLocation rlDest, RegLocation rlSrc1,
64 RegLocation rlSrc2)
65{
66 RegLocation rlResult;
67 void* funct;
68
69 /* TODO: use a proper include file to define these */
70 float __aeabi_fadd(float a, float b);
71 float __aeabi_fsub(float a, float b);
72 float __aeabi_fdiv(float a, float b);
73 float __aeabi_fmul(float a, float b);
74 float fmodf(float a, float b);
75
76 switch (mir->dalvikInsn.opCode) {
77 case OP_ADD_FLOAT_2ADDR:
78 case OP_ADD_FLOAT:
79 funct = (void*) __aeabi_fadd;
80 break;
81 case OP_SUB_FLOAT_2ADDR:
82 case OP_SUB_FLOAT:
83 funct = (void*) __aeabi_fsub;
84 break;
85 case OP_DIV_FLOAT_2ADDR:
86 case OP_DIV_FLOAT:
87 funct = (void*) __aeabi_fdiv;
88 break;
89 case OP_MUL_FLOAT_2ADDR:
90 case OP_MUL_FLOAT:
91 funct = (void*) __aeabi_fmul;
92 break;
93 case OP_REM_FLOAT_2ADDR:
94 case OP_REM_FLOAT:
95 funct = (void*) fmodf;
96 break;
97 case OP_NEG_FLOAT: {
98 genNegFloat(cUnit, rlDest, rlSrc1);
99 return false;
100 }
101 default:
102 return true;
103 }
104 flushAllRegs(cUnit); /* Send everything to home location */
105 loadValueDirectFixed(cUnit, rlSrc1, r0);
106 loadValueDirectFixed(cUnit, rlSrc2, r1);
107 loadConstant(cUnit, r2, (int)funct);
108 opReg(cUnit, kOpBlx, r2);
109 clobberCallRegs(cUnit);
110 rlResult = getReturnLoc(cUnit);
111 storeValue(cUnit, rlDest, rlResult);
112 return false;
113}
114
115static bool genArithOpDoublePortable(CompilationUnit *cUnit, MIR *mir,
116 RegLocation rlDest, RegLocation rlSrc1,
117 RegLocation rlSrc2)
118{
119 RegLocation rlResult;
120 void* funct;
121
122 /* TODO: use a proper include file to define these */
123 double __aeabi_dadd(double a, double b);
124 double __aeabi_dsub(double a, double b);
125 double __aeabi_ddiv(double a, double b);
126 double __aeabi_dmul(double a, double b);
127 double fmod(double a, double b);
128
129 switch (mir->dalvikInsn.opCode) {
130 case OP_ADD_DOUBLE_2ADDR:
131 case OP_ADD_DOUBLE:
132 funct = (void*) __aeabi_dadd;
133 break;
134 case OP_SUB_DOUBLE_2ADDR:
135 case OP_SUB_DOUBLE:
136 funct = (void*) __aeabi_dsub;
137 break;
138 case OP_DIV_DOUBLE_2ADDR:
139 case OP_DIV_DOUBLE:
140 funct = (void*) __aeabi_ddiv;
141 break;
142 case OP_MUL_DOUBLE_2ADDR:
143 case OP_MUL_DOUBLE:
144 funct = (void*) __aeabi_dmul;
145 break;
146 case OP_REM_DOUBLE_2ADDR:
147 case OP_REM_DOUBLE:
148 funct = (void*) fmod;
149 break;
150 case OP_NEG_DOUBLE: {
151 genNegDouble(cUnit, rlDest, rlSrc1);
152 return false;
153 }
154 default:
155 return true;
156 }
157 flushAllRegs(cUnit); /* Send everything to home location */
158 loadConstant(cUnit, rlr, (int)funct);
159 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
160 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
161 opReg(cUnit, kOpBlx, rlr);
162 clobberCallRegs(cUnit);
163 rlResult = getReturnLocWide(cUnit);
164 storeValueWide(cUnit, rlDest, rlResult);
165 return false;
166}
167
168static bool genConversionPortable(CompilationUnit *cUnit, MIR *mir)
169{
170 OpCode opCode = mir->dalvikInsn.opCode;
171
172 float __aeabi_i2f( int op1 );
173 int __aeabi_f2iz( float op1 );
174 float __aeabi_d2f( double op1 );
175 double __aeabi_f2d( float op1 );
176 double __aeabi_i2d( int op1 );
177 int __aeabi_d2iz( double op1 );
178 float __aeabi_l2f( long op1 );
179 double __aeabi_l2d( long op1 );
180 s8 dvmJitf2l( float op1 );
181 s8 dvmJitd2l( double op1 );
182
183 switch (opCode) {
184 case OP_INT_TO_FLOAT:
185 return genConversionCall(cUnit, mir, (void*)__aeabi_i2f, 1, 1);
186 case OP_FLOAT_TO_INT:
187 return genConversionCall(cUnit, mir, (void*)__aeabi_f2iz, 1, 1);
188 case OP_DOUBLE_TO_FLOAT:
189 return genConversionCall(cUnit, mir, (void*)__aeabi_d2f, 2, 1);
190 case OP_FLOAT_TO_DOUBLE:
191 return genConversionCall(cUnit, mir, (void*)__aeabi_f2d, 1, 2);
192 case OP_INT_TO_DOUBLE:
193 return genConversionCall(cUnit, mir, (void*)__aeabi_i2d, 1, 2);
194 case OP_DOUBLE_TO_INT:
195 return genConversionCall(cUnit, mir, (void*)__aeabi_d2iz, 2, 1);
196 case OP_FLOAT_TO_LONG:
197 return genConversionCall(cUnit, mir, (void*)dvmJitf2l, 1, 2);
198 case OP_LONG_TO_FLOAT:
199 return genConversionCall(cUnit, mir, (void*)__aeabi_l2f, 2, 1);
200 case OP_DOUBLE_TO_LONG:
201 return genConversionCall(cUnit, mir, (void*)dvmJitd2l, 2, 2);
202 case OP_LONG_TO_DOUBLE:
203 return genConversionCall(cUnit, mir, (void*)__aeabi_l2d, 2, 2);
204 default:
205 return true;
206 }
207 return false;
208}
Ben Chengba4fc8b2009-06-01 13:00:29 -0700209
Jeff Hao97319a82009-08-12 16:57:15 -0700210#if defined(WITH_SELF_VERIFICATION)
Jeff Hao97319a82009-08-12 16:57:15 -0700211/*
212 * The following are used to keep compiled loads and stores from modifying
213 * memory during self verification mode.
214 *
215 * Stores do not modify memory. Instead, the address and value pair are stored
216 * into heapSpace. Addresses within heapSpace are unique. For accesses smaller
217 * than a word, the word containing the address is loaded first before being
218 * updated.
219 *
220 * Loads check heapSpace first and return data from there if an entry exists.
221 * Otherwise, data is loaded from memory as usual.
222 */
223
224/* Decode contents of heapArgSpace to determine addr to load from */
225static void selfVerificationLoadDecode(HeapArgSpace* heapArgSpace, int* addr)
226{
Bill Buzbee1465db52009-09-23 17:17:35 -0700227 int reg = heapArgSpace->regMap & 0xFF;
228 if (!FPREG(reg)) {
229 assert(reg < 16);
230 *addr = heapArgSpace->coreRegs[reg];
231 } else {
232 assert(!DOUBLEREG(reg));
233 *addr = heapArgSpace->fpRegs[(reg & FP_REG_MASK)];
Jeff Hao97319a82009-08-12 16:57:15 -0700234 }
235}
236
237/* Decode contents of heapArgSpace to determine reg to load into */
238static void selfVerificationLoadDecodeData(HeapArgSpace* heapArgSpace,
239 int data, int reg)
240{
Bill Buzbee1465db52009-09-23 17:17:35 -0700241 if (!FPREG(reg)) {
242 assert(reg < 16);
243 heapArgSpace->coreRegs[reg] = data;
244 } else {
245 assert(!DOUBLEREG(reg));
246 heapArgSpace->fpRegs[(reg & FP_REG_MASK)] = data;
Jeff Hao97319a82009-08-12 16:57:15 -0700247 }
248}
249
250static void selfVerificationLoad(InterpState* interpState)
251{
252 Thread *self = dvmThreadSelf();
253 ShadowHeap *heapSpacePtr;
254 ShadowSpace *shadowSpace = self->shadowSpace;
255 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
256
257 int addr, data;
258 selfVerificationLoadDecode(heapArgSpace, &addr);
259
260 for (heapSpacePtr = shadowSpace->heapSpace;
261 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
262 if (heapSpacePtr->addr == addr) {
263 data = heapSpacePtr->data;
264 break;
265 }
266 }
267
268 if (heapSpacePtr == shadowSpace->heapSpaceTail)
269 data = *((unsigned int*) addr);
270
Bill Buzbee1465db52009-09-23 17:17:35 -0700271 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Ben Chengd7d426a2009-09-22 11:23:36 -0700272
Bill Buzbee1465db52009-09-23 17:17:35 -0700273 // LOGD("*** HEAP LOAD: Reg:%d Addr: 0x%x Data: 0x%x", reg, addr, data);
Ben Chengd7d426a2009-09-22 11:23:36 -0700274
Jeff Hao97319a82009-08-12 16:57:15 -0700275 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
276}
277
278static void selfVerificationLoadByte(InterpState* interpState)
279{
280 Thread *self = dvmThreadSelf();
281 ShadowHeap *heapSpacePtr;
282 ShadowSpace *shadowSpace = self->shadowSpace;
283 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
284
285 int addr, data;
286 selfVerificationLoadDecode(heapArgSpace, &addr);
287
288 int maskedAddr = addr & 0xFFFFFFFC;
289 int alignment = addr & 0x3;
290
291 for (heapSpacePtr = shadowSpace->heapSpace;
292 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
293 if (heapSpacePtr->addr == maskedAddr) {
294 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
295 data = *((unsigned char*) addr);
296 break;
297 }
298 }
299
300 if (heapSpacePtr == shadowSpace->heapSpaceTail)
301 data = *((unsigned char*) addr);
302
303 //LOGD("*** HEAP LOAD BYTE: Addr: 0x%x Data: 0x%x", addr, data);
304
Bill Buzbee1465db52009-09-23 17:17:35 -0700305 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700306 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
307}
308
309static void selfVerificationLoadHalfword(InterpState* interpState)
310{
311 Thread *self = dvmThreadSelf();
312 ShadowHeap *heapSpacePtr;
313 ShadowSpace *shadowSpace = self->shadowSpace;
314 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
315
316 int addr, data;
317 selfVerificationLoadDecode(heapArgSpace, &addr);
318
319 int maskedAddr = addr & 0xFFFFFFFC;
320 int alignment = addr & 0x2;
321
322 for (heapSpacePtr = shadowSpace->heapSpace;
323 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
324 if (heapSpacePtr->addr == maskedAddr) {
325 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
326 data = *((unsigned short*) addr);
327 break;
328 }
329 }
330
331 if (heapSpacePtr == shadowSpace->heapSpaceTail)
332 data = *((unsigned short*) addr);
333
Bill Buzbee1465db52009-09-23 17:17:35 -0700334 //LOGD("*** HEAP LOAD kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700335
Bill Buzbee1465db52009-09-23 17:17:35 -0700336 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700337 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
338}
339
340static void selfVerificationLoadSignedByte(InterpState* interpState)
341{
342 Thread *self = dvmThreadSelf();
343 ShadowHeap* heapSpacePtr;
344 ShadowSpace* shadowSpace = self->shadowSpace;
345 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
346
347 int addr, data;
348 selfVerificationLoadDecode(heapArgSpace, &addr);
349
350 int maskedAddr = addr & 0xFFFFFFFC;
351 int alignment = addr & 0x3;
352
353 for (heapSpacePtr = shadowSpace->heapSpace;
354 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
355 if (heapSpacePtr->addr == maskedAddr) {
356 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
357 data = *((signed char*) addr);
358 break;
359 }
360 }
361
362 if (heapSpacePtr == shadowSpace->heapSpaceTail)
363 data = *((signed char*) addr);
364
365 //LOGD("*** HEAP LOAD SIGNED BYTE: Addr: 0x%x Data: 0x%x", addr, data);
366
Bill Buzbee1465db52009-09-23 17:17:35 -0700367 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700368 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
369}
370
371static void selfVerificationLoadSignedHalfword(InterpState* interpState)
372{
373 Thread *self = dvmThreadSelf();
374 ShadowHeap* heapSpacePtr;
375 ShadowSpace* shadowSpace = self->shadowSpace;
376 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
377
378 int addr, data;
379 selfVerificationLoadDecode(heapArgSpace, &addr);
380
381 int maskedAddr = addr & 0xFFFFFFFC;
382 int alignment = addr & 0x2;
383
384 for (heapSpacePtr = shadowSpace->heapSpace;
385 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
386 if (heapSpacePtr->addr == maskedAddr) {
387 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
388 data = *((signed short*) addr);
389 break;
390 }
391 }
392
393 if (heapSpacePtr == shadowSpace->heapSpaceTail)
394 data = *((signed short*) addr);
395
Bill Buzbee1465db52009-09-23 17:17:35 -0700396 //LOGD("*** HEAP LOAD SIGNED kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700397
Bill Buzbee1465db52009-09-23 17:17:35 -0700398 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700399 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
400}
401
402static void selfVerificationLoadDoubleword(InterpState* interpState)
403{
404 Thread *self = dvmThreadSelf();
405 ShadowHeap* heapSpacePtr;
406 ShadowSpace* shadowSpace = self->shadowSpace;
407 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
408
409 int addr;
410 selfVerificationLoadDecode(heapArgSpace, &addr);
411
412 int addr2 = addr+4;
413 unsigned int data = *((unsigned int*) addr);
414 unsigned int data2 = *((unsigned int*) addr2);
415
416 for (heapSpacePtr = shadowSpace->heapSpace;
417 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
418 if (heapSpacePtr->addr == addr) {
419 data = heapSpacePtr->data;
420 } else if (heapSpacePtr->addr == addr2) {
421 data2 = heapSpacePtr->data;
422 }
423 }
424
Bill Buzbee1465db52009-09-23 17:17:35 -0700425 // LOGD("*** HEAP LOAD DOUBLEWORD: Addr: 0x%x Data: 0x%x Data2: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700426 // addr, data, data2);
427
Bill Buzbee1465db52009-09-23 17:17:35 -0700428 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
429 int reg2 = (heapArgSpace->regMap >> 16) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700430 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
431 selfVerificationLoadDecodeData(heapArgSpace, data2, reg2);
432}
433
434/* Decode contents of heapArgSpace to determine arguments to store. */
435static void selfVerificationStoreDecode(HeapArgSpace* heapArgSpace,
436 int* value, int reg)
437{
Bill Buzbee1465db52009-09-23 17:17:35 -0700438 if (!FPREG(reg)) {
439 assert(reg < 16);
440 *value = heapArgSpace->coreRegs[reg];
441 } else {
442 assert(!DOUBLEREG(reg));
443 *value = heapArgSpace->fpRegs[(reg & FP_REG_MASK)];
Jeff Hao97319a82009-08-12 16:57:15 -0700444 }
445}
446
447static void selfVerificationStore(InterpState* interpState)
448{
449 Thread *self = dvmThreadSelf();
450 ShadowHeap *heapSpacePtr;
451 ShadowSpace *shadowSpace = self->shadowSpace;
452 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
453
454 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700455 int reg0 = heapArgSpace->regMap & 0xFF;
456 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700457 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
458 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
459
460 //LOGD("*** HEAP STORE: Addr: 0x%x Data: 0x%x", addr, data);
461
462 for (heapSpacePtr = shadowSpace->heapSpace;
463 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
464 if (heapSpacePtr->addr == addr) break;
465 }
466
467 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
468 heapSpacePtr->addr = addr;
469 shadowSpace->heapSpaceTail++;
470 }
471
472 heapSpacePtr->data = data;
473}
474
475static void selfVerificationStoreByte(InterpState* interpState)
476{
477 Thread *self = dvmThreadSelf();
478 ShadowHeap *heapSpacePtr;
479 ShadowSpace *shadowSpace = self->shadowSpace;
480 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
481
482 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700483 int reg0 = heapArgSpace->regMap & 0xFF;
484 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700485 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
486 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
487
488 int maskedAddr = addr & 0xFFFFFFFC;
489 int alignment = addr & 0x3;
490
491 //LOGD("*** HEAP STORE BYTE: Addr: 0x%x Data: 0x%x", addr, data);
492
493 for (heapSpacePtr = shadowSpace->heapSpace;
494 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
495 if (heapSpacePtr->addr == maskedAddr) break;
496 }
497
498 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
499 heapSpacePtr->addr = maskedAddr;
500 heapSpacePtr->data = *((unsigned int*) maskedAddr);
501 shadowSpace->heapSpaceTail++;
502 }
503
504 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
505 *((unsigned char*) addr) = (char) data;
506
507 //LOGD("*** HEAP STORE BYTE: Addr: 0x%x Final Data: 0x%x",
508 // addr, heapSpacePtr->data);
509}
510
511static void selfVerificationStoreHalfword(InterpState* interpState)
512{
513 Thread *self = dvmThreadSelf();
514 ShadowHeap *heapSpacePtr;
515 ShadowSpace *shadowSpace = self->shadowSpace;
516 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
517
518 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700519 int reg0 = heapArgSpace->regMap & 0xFF;
520 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700521 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
522 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
523
524 int maskedAddr = addr & 0xFFFFFFFC;
525 int alignment = addr & 0x2;
526
Bill Buzbee1465db52009-09-23 17:17:35 -0700527 //LOGD("*** HEAP STORE kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700528
529 for (heapSpacePtr = shadowSpace->heapSpace;
530 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
531 if (heapSpacePtr->addr == maskedAddr) break;
532 }
533
534 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
535 heapSpacePtr->addr = maskedAddr;
536 heapSpacePtr->data = *((unsigned int*) maskedAddr);
537 shadowSpace->heapSpaceTail++;
538 }
539
540 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
541 *((unsigned short*) addr) = (short) data;
542
Bill Buzbee1465db52009-09-23 17:17:35 -0700543 //LOGD("*** HEAP STORE kHalfWord: Addr: 0x%x Final Data: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700544 // addr, heapSpacePtr->data);
545}
546
547static void selfVerificationStoreDoubleword(InterpState* interpState)
548{
549 Thread *self = dvmThreadSelf();
550 ShadowHeap *heapSpacePtr;
551 ShadowSpace *shadowSpace = self->shadowSpace;
552 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
553
554 int addr, data, data2;
Bill Buzbee1465db52009-09-23 17:17:35 -0700555 int reg0 = heapArgSpace->regMap & 0xFF;
556 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
557 int reg2 = (heapArgSpace->regMap >> 16) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700558 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
559 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
560 selfVerificationStoreDecode(heapArgSpace, &data2, reg2);
561
562 int addr2 = addr+4;
563 bool store1 = false, store2 = false;
564
565 //LOGD("*** HEAP STORE DOUBLEWORD: Addr: 0x%x Data: 0x%x, Data2: 0x%x",
566 // addr, data, data2);
567
568 for (heapSpacePtr = shadowSpace->heapSpace;
569 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
570 if (heapSpacePtr->addr == addr) {
571 heapSpacePtr->data = data;
572 store1 = true;
573 } else if (heapSpacePtr->addr == addr2) {
574 heapSpacePtr->data = data2;
575 store2 = true;
576 }
577 }
578
579 if (!store1) {
580 shadowSpace->heapSpaceTail->addr = addr;
581 shadowSpace->heapSpaceTail->data = data;
582 shadowSpace->heapSpaceTail++;
583 }
584 if (!store2) {
585 shadowSpace->heapSpaceTail->addr = addr2;
586 shadowSpace->heapSpaceTail->data = data2;
587 shadowSpace->heapSpaceTail++;
588 }
589}
590
591/* Common wrapper function for all memory operations */
592static void selfVerificationMemOpWrapper(CompilationUnit *cUnit, int regMap,
593 void* funct)
594{
Bill Buzbee1465db52009-09-23 17:17:35 -0700595 /* push r0 and r7 to give us a foothold */
596 newLIR1(cUnit, kThumbPush, (1 << r0) | (1 << r7));
Jeff Hao97319a82009-08-12 16:57:15 -0700597
Bill Buzbee1465db52009-09-23 17:17:35 -0700598 /* Let the save handler know where the save record is */
599 loadConstant(cUnit, r0, offsetof(InterpState, heapArgSpace));
Jeff Hao97319a82009-08-12 16:57:15 -0700600
Bill Buzbee1465db52009-09-23 17:17:35 -0700601 /* Load the regMap and call the save handler [note: handler pops r0/r7] */
602 loadConstant(cUnit, r7, regMap);
603 genDispatchToHandler(cUnit, TEMPLATE_SAVE_STATE);
Jeff Hao97319a82009-08-12 16:57:15 -0700604
Bill Buzbee1465db52009-09-23 17:17:35 -0700605 /* Set function pointer, pass rGLUE and branch */
Jeff Hao97319a82009-08-12 16:57:15 -0700606 loadConstant(cUnit, r1, (int) funct);
Bill Buzbee1465db52009-09-23 17:17:35 -0700607 newLIR2(cUnit, kThumbMovRR, r0, rGLUE);
608 newLIR1(cUnit, kThumbBlxR, r1);
Jeff Hao97319a82009-08-12 16:57:15 -0700609
Bill Buzbee1465db52009-09-23 17:17:35 -0700610 /* Let the recover handler know where coreRegs[0] and restore regs */
611 loadConstant(cUnit, r0, offsetof(InterpState, heapArgSpace) +
612 offsetof(HeapArgSpace, coreRegs));
613 genDispatchToHandler(cUnit, TEMPLATE_RESTORE_STATE);
Jeff Hao97319a82009-08-12 16:57:15 -0700614}
Ben Cheng5d90c202009-11-22 23:31:11 -0800615
Jeff Hao97319a82009-08-12 16:57:15 -0700616#endif
617
Ben Chengba4fc8b2009-06-01 13:00:29 -0700618/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700619static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
620 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700621{
Bill Buzbee1465db52009-09-23 17:17:35 -0700622 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700623 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
624}
625
626/* Load a wide field from an object instance */
627static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
628{
629 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700630 RegLocation rlObj = getSrcLoc(cUnit, mir, 0);
631 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
632 RegLocation rlResult;
633 rlObj = loadValue(cUnit, rlObj, kCoreReg);
634 int regPtr = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700635
Bill Buzbee1465db52009-09-23 17:17:35 -0700636 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700637
Bill Buzbee1465db52009-09-23 17:17:35 -0700638 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
639 NULL);/* null object? */
640 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
641 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Jeff Hao97319a82009-08-12 16:57:15 -0700642#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700643 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -0700644#else
Bill Buzbee1465db52009-09-23 17:17:35 -0700645 int regMap = rlResult.highReg << 16 | rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700646 selfVerificationMemOpWrapper(cUnit, regMap,
647 &selfVerificationLoadDoubleword);
Jeff Hao97319a82009-08-12 16:57:15 -0700648#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700649 freeTemp(cUnit, regPtr);
650 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700651}
652
653/* Store a wide field to an object instance */
654static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
655{
656 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700657 RegLocation rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
658 RegLocation rlObj = getSrcLoc(cUnit, mir, 2);
659 rlObj = loadValue(cUnit, rlObj, kCoreReg);
660 int regPtr;
661 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
662 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
663 NULL);/* null object? */
664 regPtr = allocTemp(cUnit);
665 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700666#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700667 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -0700668#else
Bill Buzbee1465db52009-09-23 17:17:35 -0700669 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700670 selfVerificationMemOpWrapper(cUnit, regMap,
671 &selfVerificationStoreDoubleword);
672#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700673 freeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700674}
675
676/*
677 * Load a field from an object instance
678 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700679 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700680static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700681 int fieldOffset)
682{
Bill Buzbee1465db52009-09-23 17:17:35 -0700683 int regPtr;
684 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700685 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700686 RegLocation rlObj = getSrcLoc(cUnit, mir, 0);
687 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
688 rlObj = loadValue(cUnit, rlObj, kCoreReg);
689 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700690 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
691 NULL);/* null object? */
Ben Cheng5d90c202009-11-22 23:31:11 -0800692#if !defined(WITH_SELF_VERIFICATION)
693 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
694 size, rlObj.sRegLow);
695#else
Jeff Hao97319a82009-08-12 16:57:15 -0700696 /* Combine address and offset */
Bill Buzbee1465db52009-09-23 17:17:35 -0700697 regPtr = allocTemp(cUnit);
698 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700699
Bill Buzbee1465db52009-09-23 17:17:35 -0700700 int regMap = rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700701 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationLoad);
Bill Buzbee1465db52009-09-23 17:17:35 -0700702 freeTemp(cUnit, regPtr);
Jeff Hao97319a82009-08-12 16:57:15 -0700703#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700704 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700705}
706
707/*
708 * Store a field to an object instance
709 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700710 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700711static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700712 int fieldOffset)
713{
714 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700715 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
716 RegLocation rlObj = getSrcLoc(cUnit, mir, 1);
717 rlObj = loadValue(cUnit, rlObj, kCoreReg);
718 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
719 int regPtr;
720 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
721 NULL);/* null object? */
Jeff Hao97319a82009-08-12 16:57:15 -0700722#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700723 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Jeff Hao97319a82009-08-12 16:57:15 -0700724#else
725 /* Combine address and offset */
Bill Buzbee1465db52009-09-23 17:17:35 -0700726 regPtr = allocTemp(cUnit);
727 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700728
Bill Buzbee1465db52009-09-23 17:17:35 -0700729 int regMap = rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700730 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationStore);
Jeff Hao97319a82009-08-12 16:57:15 -0700731#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700732}
733
734
Ben Chengba4fc8b2009-06-01 13:00:29 -0700735/*
736 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700737 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700738static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700739 RegLocation rlArray, RegLocation rlIndex,
740 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700741{
742 int lenOffset = offsetof(ArrayObject, length);
743 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700744 RegLocation rlResult;
745 rlArray = loadValue(cUnit, rlArray, kCoreReg);
746 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
747 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700748
749 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700750 ArmLIR * pcrLabel = NULL;
751
752 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700753 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
754 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700755 }
756
Bill Buzbee1465db52009-09-23 17:17:35 -0700757 regPtr = allocTemp(cUnit);
758
Ben Cheng4238ec22009-08-24 16:32:22 -0700759 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700760 int regLen = allocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700761 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700762 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
763 /* regPtr -> array data */
764 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
765 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
766 pcrLabel);
767 freeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700768 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700769 /* regPtr -> array data */
770 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700771 }
Jeff Hao97319a82009-08-12 16:57:15 -0700772#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700773 if ((size == kLong) || (size == kDouble)) {
774 if (scale) {
775 int rNewIndex = allocTemp(cUnit);
776 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
777 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
778 freeTemp(cUnit, rNewIndex);
779 } else {
780 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
781 }
782 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
783 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
784 freeTemp(cUnit, regPtr);
785 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700786 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700787 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
788 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
789 scale, size);
790 freeTemp(cUnit, regPtr);
791 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700792 }
Jeff Hao97319a82009-08-12 16:57:15 -0700793#else
Bill Buzbee270c1d62009-08-13 16:58:07 -0700794 //TODO: probably want to move this into loadBaseIndexed
795 void *funct = NULL;
796 switch(size) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700797 case kLong:
798 case kDouble:
Jeff Hao97319a82009-08-12 16:57:15 -0700799 funct = (void*) &selfVerificationLoadDoubleword;
800 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700801 case kWord:
Jeff Hao97319a82009-08-12 16:57:15 -0700802 funct = (void*) &selfVerificationLoad;
803 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700804 case kUnsignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700805 funct = (void*) &selfVerificationLoadHalfword;
806 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700807 case kSignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700808 funct = (void*) &selfVerificationLoadSignedHalfword;
809 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700810 case kUnsignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700811 funct = (void*) &selfVerificationLoadByte;
812 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700813 case kSignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700814 funct = (void*) &selfVerificationLoadSignedByte;
815 break;
816 default:
817 assert(0);
818 dvmAbort();
Jeff Hao97319a82009-08-12 16:57:15 -0700819 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700820 /* Combine address and index */
Bill Buzbee1465db52009-09-23 17:17:35 -0700821 if (scale) {
822 int regTmp = allocTemp(cUnit);
823 opRegRegImm(cUnit, kOpLsl, regTmp, rlIndex.lowReg, scale);
824 opRegReg(cUnit, kOpAdd, regPtr, regTmp);
825 freeTemp(cUnit, regTmp);
826 } else {
827 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
828 }
Jeff Hao97319a82009-08-12 16:57:15 -0700829
Bill Buzbee1465db52009-09-23 17:17:35 -0700830 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
831 int regMap = rlResult.highReg << 16 | rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700832 selfVerificationMemOpWrapper(cUnit, regMap, funct);
833
Bill Buzbee1465db52009-09-23 17:17:35 -0700834 freeTemp(cUnit, regPtr);
835 if ((size == kLong) || (size == kDouble))
836 storeValueWide(cUnit, rlDest, rlResult);
Jeff Hao97319a82009-08-12 16:57:15 -0700837 else
Bill Buzbee1465db52009-09-23 17:17:35 -0700838 storeValue(cUnit, rlDest, rlResult);
Jeff Hao97319a82009-08-12 16:57:15 -0700839#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700840}
841
Ben Chengba4fc8b2009-06-01 13:00:29 -0700842/*
843 * Generate array store
844 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700845 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700846static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700847 RegLocation rlArray, RegLocation rlIndex,
848 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700849{
850 int lenOffset = offsetof(ArrayObject, length);
851 int dataOffset = offsetof(ArrayObject, contents);
852
Bill Buzbee1465db52009-09-23 17:17:35 -0700853 int regPtr;
854 rlArray = loadValue(cUnit, rlArray, kCoreReg);
855 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700856
Bill Buzbee1465db52009-09-23 17:17:35 -0700857 if (isTemp(cUnit, rlArray.lowReg)) {
858 clobberReg(cUnit, rlArray.lowReg);
859 regPtr = rlArray.lowReg;
860 } else {
861 regPtr = allocTemp(cUnit);
862 genRegCopy(cUnit, regPtr, rlArray.lowReg);
863 }
Ben Chenge9695e52009-06-16 16:11:47 -0700864
Ben Cheng1efc9c52009-06-08 18:25:27 -0700865 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700866 ArmLIR * pcrLabel = NULL;
867
868 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700869 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
870 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700871 }
872
873 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700874 int regLen = allocTemp(cUnit);
875 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700876 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700877 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
878 /* regPtr -> array data */
879 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
880 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
881 pcrLabel);
882 freeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700883 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700884 /* regPtr -> array data */
885 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700886 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700887 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700888#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700889 if ((size == kLong) || (size == kDouble)) {
890 //TODO: need specific wide routine that can handle fp regs
891 if (scale) {
892 int rNewIndex = allocTemp(cUnit);
893 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
894 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
895 freeTemp(cUnit, rNewIndex);
896 } else {
897 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
898 }
899 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
900 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
901 freeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700902 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700903 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
904 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
905 scale, size);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700906 }
907#else
908 //TODO: probably want to move this into storeBaseIndexed
909 void *funct = NULL;
910 switch(size) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700911 case kLong:
912 case kDouble:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700913 funct = (void*) &selfVerificationStoreDoubleword;
914 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700915 case kWord:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700916 funct = (void*) &selfVerificationStore;
917 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700918 case kSignedHalf:
919 case kUnsignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700920 funct = (void*) &selfVerificationStoreHalfword;
921 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700922 case kSignedByte:
923 case kUnsignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700924 funct = (void*) &selfVerificationStoreByte;
925 break;
926 default:
927 assert(0);
928 dvmAbort();
929 }
930
Bill Buzbee1465db52009-09-23 17:17:35 -0700931 if (scale) {
932 int regTmpIndex = allocTemp(cUnit);
933 // 3 live temps
934 opRegRegImm(cUnit, kOpLsl, regTmpIndex, rlIndex.lowReg, scale);
935 opRegReg(cUnit, kOpAdd, regPtr, regTmpIndex);
936 freeTemp(cUnit, regTmpIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700937 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700938 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700939 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700940 /* Combine address and index */
941 if ((size == kLong) || (size == kDouble)) {
942 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
943 } else {
944 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
945 }
Jeff Hao97319a82009-08-12 16:57:15 -0700946
Bill Buzbee1465db52009-09-23 17:17:35 -0700947 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700948 selfVerificationMemOpWrapper(cUnit, regMap, funct);
949
Jeff Hao97319a82009-08-12 16:57:15 -0700950#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700951}
952
Ben Cheng5d90c202009-11-22 23:31:11 -0800953static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
954 RegLocation rlDest, RegLocation rlSrc1,
955 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700956{
Ben Chenge9695e52009-06-16 16:11:47 -0700957 /*
958 * Don't mess with the regsiters here as there is a particular calling
959 * convention to the out-of-line handler.
960 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700961 RegLocation rlResult;
962
963 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
964 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700965 switch( mir->dalvikInsn.opCode) {
966 case OP_SHL_LONG:
967 case OP_SHL_LONG_2ADDR:
968 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
969 break;
970 case OP_SHR_LONG:
971 case OP_SHR_LONG_2ADDR:
972 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
973 break;
974 case OP_USHR_LONG:
975 case OP_USHR_LONG_2ADDR:
976 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
977 break;
978 default:
979 return true;
980 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700981 rlResult = getReturnLocWide(cUnit);
982 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700983 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700984}
Ben Chenge9695e52009-06-16 16:11:47 -0700985
Ben Cheng5d90c202009-11-22 23:31:11 -0800986static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
987 RegLocation rlDest, RegLocation rlSrc1,
988 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700989{
Bill Buzbee1465db52009-09-23 17:17:35 -0700990 RegLocation rlResult;
991 OpKind firstOp = kOpBkpt;
992 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700993 bool callOut = false;
994 void *callTgt;
995 int retReg = r0;
996 /* TODO - find proper .h file to declare these */
997 long long __aeabi_ldivmod(long long op1, long long op2);
998
999 switch (mir->dalvikInsn.opCode) {
1000 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07001001 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1002 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1003 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1004 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1005 storeValueWide(cUnit, rlDest, rlResult);
1006 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001007 break;
1008 case OP_ADD_LONG:
1009 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001010 firstOp = kOpAdd;
1011 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001012 break;
1013 case OP_SUB_LONG:
1014 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001015 firstOp = kOpSub;
1016 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001017 break;
1018 case OP_MUL_LONG:
1019 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001020 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001021 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001022 case OP_DIV_LONG:
1023 case OP_DIV_LONG_2ADDR:
1024 callOut = true;
1025 retReg = r0;
1026 callTgt = (void*)__aeabi_ldivmod;
1027 break;
1028 /* NOTE - result is in r2/r3 instead of r0/r1 */
1029 case OP_REM_LONG:
1030 case OP_REM_LONG_2ADDR:
1031 callOut = true;
1032 callTgt = (void*)__aeabi_ldivmod;
1033 retReg = r2;
1034 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001035 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001036 case OP_AND_LONG:
1037 firstOp = kOpAnd;
1038 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001039 break;
1040 case OP_OR_LONG:
1041 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001042 firstOp = kOpOr;
1043 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001044 break;
1045 case OP_XOR_LONG:
1046 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001047 firstOp = kOpXor;
1048 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001049 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001050 case OP_NEG_LONG: {
Bill Buzbee51ecf602010-01-14 14:27:52 -08001051 //TUNING: can improve this using Thumb2 code
1052 int tReg = allocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07001053 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1054 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee51ecf602010-01-14 14:27:52 -08001055 loadConstantValue(cUnit, tReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07001056 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
Bill Buzbee51ecf602010-01-14 14:27:52 -08001057 tReg, rlSrc2.lowReg);
1058 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
1059 genRegCopy(cUnit, rlResult.highReg, tReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07001060 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001061 return false;
Ben Chenge9695e52009-06-16 16:11:47 -07001062 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001063 default:
1064 LOGE("Invalid long arith op");
1065 dvmAbort();
1066 }
1067 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001068 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001069 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001070 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001071 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001072 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1073 loadConstant(cUnit, rlr, (int) callTgt);
1074 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1075 opReg(cUnit, kOpBlx, rlr);
1076 clobberCallRegs(cUnit);
1077 if (retReg == r0)
1078 rlResult = getReturnLocWide(cUnit);
1079 else
1080 rlResult = getReturnLocWideAlt(cUnit);
1081 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001082 }
1083 return false;
1084}
1085
Ben Cheng5d90c202009-11-22 23:31:11 -08001086static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
1087 RegLocation rlDest, RegLocation rlSrc1,
1088 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001089{
Bill Buzbee1465db52009-09-23 17:17:35 -07001090 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001091 bool callOut = false;
1092 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -07001093 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001094 int retReg = r0;
1095 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -07001096 RegLocation rlResult;
Bill Buzbee0e605272009-12-01 14:28:05 -08001097 bool shiftOp = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001098
1099 /* TODO - find proper .h file to declare these */
1100 int __aeabi_idivmod(int op1, int op2);
1101 int __aeabi_idiv(int op1, int op2);
1102
1103 switch (mir->dalvikInsn.opCode) {
1104 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001105 op = kOpNeg;
1106 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001107 break;
1108 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001109 op = kOpMvn;
1110 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001111 break;
1112 case OP_ADD_INT:
1113 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001114 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001115 break;
1116 case OP_SUB_INT:
1117 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001118 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001119 break;
1120 case OP_MUL_INT:
1121 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001122 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001123 break;
1124 case OP_DIV_INT:
1125 case OP_DIV_INT_2ADDR:
1126 callOut = true;
1127 checkZero = true;
1128 callTgt = __aeabi_idiv;
1129 retReg = r0;
1130 break;
1131 /* NOTE: returns in r1 */
1132 case OP_REM_INT:
1133 case OP_REM_INT_2ADDR:
1134 callOut = true;
1135 checkZero = true;
1136 callTgt = __aeabi_idivmod;
1137 retReg = r1;
1138 break;
1139 case OP_AND_INT:
1140 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001141 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001142 break;
1143 case OP_OR_INT:
1144 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001145 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001146 break;
1147 case OP_XOR_INT:
1148 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001149 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001150 break;
1151 case OP_SHL_INT:
1152 case OP_SHL_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -08001153 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -07001154 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001155 break;
1156 case OP_SHR_INT:
1157 case OP_SHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -08001158 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -07001159 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001160 break;
1161 case OP_USHR_INT:
1162 case OP_USHR_INT_2ADDR:
Bill Buzbee0e605272009-12-01 14:28:05 -08001163 shiftOp = true;
Bill Buzbee1465db52009-09-23 17:17:35 -07001164 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001165 break;
1166 default:
1167 LOGE("Invalid word arith op: 0x%x(%d)",
1168 mir->dalvikInsn.opCode, mir->dalvikInsn.opCode);
1169 dvmAbort();
1170 }
1171 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001172 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1173 if (unary) {
1174 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1175 opRegReg(cUnit, op, rlResult.lowReg,
1176 rlSrc1.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -07001177 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001178 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
Bill Buzbee0e605272009-12-01 14:28:05 -08001179 if (shiftOp) {
1180 int tReg = allocTemp(cUnit);
1181 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1182 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1183 opRegRegReg(cUnit, op, rlResult.lowReg,
1184 rlSrc1.lowReg, tReg);
1185 freeTemp(cUnit, tReg);
1186 } else {
1187 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1188 opRegRegReg(cUnit, op, rlResult.lowReg,
1189 rlSrc1.lowReg, rlSrc2.lowReg);
1190 }
Ben Chenge9695e52009-06-16 16:11:47 -07001191 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001192 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001193 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001194 RegLocation rlResult;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001195 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001196 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001197 loadConstant(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -07001198 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001199 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001200 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001201 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001202 opReg(cUnit, kOpBlx, r2);
1203 clobberCallRegs(cUnit);
1204 if (retReg == r0)
1205 rlResult = getReturnLoc(cUnit);
1206 else
1207 rlResult = getReturnLocAlt(cUnit);
1208 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001209 }
1210 return false;
1211}
1212
Ben Cheng5d90c202009-11-22 23:31:11 -08001213static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001214{
1215 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001216 RegLocation rlDest;
1217 RegLocation rlSrc1;
1218 RegLocation rlSrc2;
1219 /* Deduce sizes of operands */
1220 if (mir->ssaRep->numUses == 2) {
1221 rlSrc1 = getSrcLoc(cUnit, mir, 0);
1222 rlSrc2 = getSrcLoc(cUnit, mir, 1);
1223 } else if (mir->ssaRep->numUses == 3) {
1224 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
1225 rlSrc2 = getSrcLoc(cUnit, mir, 2);
1226 } else {
1227 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
1228 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
1229 assert(mir->ssaRep->numUses == 4);
1230 }
1231 if (mir->ssaRep->numDefs == 1) {
1232 rlDest = getDestLoc(cUnit, mir, 0);
1233 } else {
1234 assert(mir->ssaRep->numDefs == 2);
1235 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1236 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001237
1238 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001239 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001240 }
1241 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001242 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001243 }
1244 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001245 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001246 }
1247 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001248 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001249 }
1250 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001251 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001252 }
1253 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001254 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001255 }
1256 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001257 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001258 }
1259 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001260 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001261 }
1262 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001263 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001264 }
1265 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001266 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001267 }
1268 return true;
1269}
1270
Bill Buzbee1465db52009-09-23 17:17:35 -07001271/* Generate conditional branch instructions */
1272static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
1273 ArmConditionCode cond,
1274 ArmLIR *target)
1275{
1276 ArmLIR *branch = opCondBranch(cUnit, cond);
1277 branch->generic.target = (LIR *) target;
1278 return branch;
1279}
1280
1281/* Generate unconditional branch instructions */
1282static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
1283{
1284 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
1285 branch->generic.target = (LIR *) target;
1286 return branch;
1287}
1288
Bill Buzbee1465db52009-09-23 17:17:35 -07001289/* Perform the actual operation for OP_RETURN_* */
1290static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
1291{
1292 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
1293#if defined(INVOKE_STATS)
1294 gDvmJit.returnOp++;
1295#endif
1296 int dPC = (int) (cUnit->method->insns + mir->offset);
1297 /* Insert branch, but defer setting of target */
1298 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
1299 /* Set up the place holder to reconstruct this Dalvik PC */
1300 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
1301 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
1302 pcrLabel->operands[0] = dPC;
1303 pcrLabel->operands[1] = mir->offset;
1304 /* Insert the place holder to the growable list */
1305 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1306 /* Branch to the PC reconstruction code */
1307 branch->generic.target = (LIR *) pcrLabel;
1308}
1309
Ben Chengba4fc8b2009-06-01 13:00:29 -07001310static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
1311 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001312 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001313{
1314 unsigned int i;
1315 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -07001316 RegLocation rlArg;
1317 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001318
Bill Buzbee1465db52009-09-23 17:17:35 -07001319 /*
1320 * Load arguments to r0..r4. Note that these registers may contain
1321 * live values, so we clobber them immediately after loading to prevent
1322 * them from being used as sources for subsequent loads.
1323 */
1324 lockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001325 for (i = 0; i < dInsn->vA; i++) {
1326 regMask |= 1 << i;
Bill Buzbee1465db52009-09-23 17:17:35 -07001327 rlArg = getSrcLoc(cUnit, mir, numDone++);
1328 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001329 }
1330 if (regMask) {
1331 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -07001332 opRegRegImm(cUnit, kOpSub, r7, rFP,
1333 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001334 /* generate null check */
1335 if (pcrLabel) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001336 *pcrLabel = genNullCheck(cUnit, getSrcSSAName(mir, 0), r0,
1337 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001338 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001339 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001340 }
1341}
1342
1343static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1344 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001345 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001346{
1347 int srcOffset = dInsn->vC << 2;
1348 int numArgs = dInsn->vA;
1349 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001350
1351 /*
1352 * Note: here, all promoted registers will have been flushed
1353 * back to the Dalvik base locations, so register usage restrictins
1354 * are lifted. All parms loaded from original Dalvik register
1355 * region - even though some might conceivably have valid copies
1356 * cached in a preserved register.
1357 */
1358 lockAllTemps(cUnit);
1359
Ben Chengba4fc8b2009-06-01 13:00:29 -07001360 /*
1361 * r4PC : &rFP[vC]
1362 * r7: &newFP[0]
1363 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001364 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001365 /* load [r0 .. min(numArgs,4)] */
1366 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001367 /*
1368 * Protect the loadMultiple instruction from being reordered with other
1369 * Dalvik stack accesses.
1370 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001371 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001372
Bill Buzbee1465db52009-09-23 17:17:35 -07001373 opRegRegImm(cUnit, kOpSub, r7, rFP,
1374 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001375 /* generate null check */
1376 if (pcrLabel) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001377 *pcrLabel = genNullCheck(cUnit, getSrcSSAName(mir, 0), r0,
1378 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001379 }
1380
1381 /*
1382 * Handle remaining 4n arguments:
1383 * store previously loaded 4 values and load the next 4 values
1384 */
1385 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001386 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001387 /*
1388 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001389 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001390 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001391 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001392 /* No need to generate the loop structure if numArgs <= 11 */
1393 if (numArgs > 11) {
1394 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001395 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001396 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001397 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001398 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001399 /*
1400 * Protect the loadMultiple instruction from being reordered with other
1401 * Dalvik stack accesses.
1402 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001403 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001404 /* No need to generate the loop structure if numArgs <= 11 */
1405 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001406 opRegImm(cUnit, kOpSub, rFP, 4);
1407 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001408 }
1409 }
1410
1411 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001412 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001413
1414 /* Generate the loop epilogue - don't use r0 */
1415 if ((numArgs > 4) && (numArgs % 4)) {
1416 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001417 /*
1418 * Protect the loadMultiple instruction from being reordered with other
1419 * Dalvik stack accesses.
1420 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001421 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001422 }
1423 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001424 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001425
1426 /* Save the modulo 4 arguments */
1427 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001428 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001429 }
1430}
1431
Ben Cheng38329f52009-07-07 14:19:20 -07001432/*
1433 * Generate code to setup the call stack then jump to the chaining cell if it
1434 * is not a native method.
1435 */
1436static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001437 BasicBlock *bb, ArmLIR *labelList,
1438 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001439 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001440{
Bill Buzbee1465db52009-09-23 17:17:35 -07001441 /*
1442 * Note: all Dalvik register state should be flushed to
1443 * memory by the point, so register usage restrictions no
1444 * longer apply. All temp & preserved registers may be used.
1445 */
1446 lockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001447 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001448
1449 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001450 lockTemp(cUnit, r1);
1451 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001452 /* r4PC = dalvikCallsite */
1453 loadConstant(cUnit, r4PC,
1454 (int) (cUnit->method->insns + mir->offset));
1455 addrRetChain->generic.target = (LIR *) retChainingCell;
1456 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001457 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001458 * r1 = &ChainingCell
1459 * r4PC = callsiteDPC
1460 */
1461 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001462 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001463#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07001464 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001465#endif
1466 } else {
1467 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
1468#if defined(INVOKE_STATS)
1469 gDvmJit.invokeChain++;
1470#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001471 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001472 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1473 }
1474 /* Handle exceptions using the interpreter */
1475 genTrap(cUnit, mir->offset, pcrLabel);
1476}
1477
Ben Cheng38329f52009-07-07 14:19:20 -07001478/*
1479 * Generate code to check the validity of a predicted chain and take actions
1480 * based on the result.
1481 *
1482 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1483 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1484 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1485 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1486 * 0x426a99b2 : blx_2 see above --+
1487 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1488 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1489 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1490 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1491 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1492 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1493 * 0x426a99c0 : blx r7 --+
1494 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1495 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1496 * 0x426a99c6 : blx_2 see above --+
1497 */
1498static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1499 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001500 ArmLIR *retChainingCell,
1501 ArmLIR *predChainingCell,
1502 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001503{
Bill Buzbee1465db52009-09-23 17:17:35 -07001504 /*
1505 * Note: all Dalvik register state should be flushed to
1506 * memory by the point, so register usage restrictions no
1507 * longer apply. Lock temps to prevent them from being
1508 * allocated by utility routines.
1509 */
1510 lockAllTemps(cUnit);
1511
Ben Cheng38329f52009-07-07 14:19:20 -07001512 /* "this" is already left in r0 by genProcessArgs* */
1513
1514 /* r4PC = dalvikCallsite */
1515 loadConstant(cUnit, r4PC,
1516 (int) (cUnit->method->insns + mir->offset));
1517
1518 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001519 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001520 addrRetChain->generic.target = (LIR *) retChainingCell;
1521
1522 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001523 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001524 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1525
1526 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1527
1528 /* return through lr - jump to the chaining cell */
1529 genUnconditionalBranch(cUnit, predChainingCell);
1530
1531 /*
1532 * null-check on "this" may have been eliminated, but we still need a PC-
1533 * reconstruction label for stack overflow bailout.
1534 */
1535 if (pcrLabel == NULL) {
1536 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001537 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001538 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07001539 pcrLabel->operands[0] = dPC;
1540 pcrLabel->operands[1] = mir->offset;
1541 /* Insert the place holder to the growable list */
1542 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1543 }
1544
1545 /* return through lr+2 - punt to the interpreter */
1546 genUnconditionalBranch(cUnit, pcrLabel);
1547
1548 /*
1549 * return through lr+4 - fully resolve the callee method.
1550 * r1 <- count
1551 * r2 <- &predictedChainCell
1552 * r3 <- this->class
1553 * r4 <- dPC
1554 * r7 <- this->class->vtable
1555 */
1556
1557 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001558 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001559
1560 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001561 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001562
Bill Buzbee1465db52009-09-23 17:17:35 -07001563 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001564
Bill Buzbee270c1d62009-08-13 16:58:07 -07001565 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1566 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001567
1568 /*
1569 * r0 = calleeMethod
1570 * r2 = &predictedChainingCell
1571 * r3 = class
1572 *
1573 * &returnChainingCell has been loaded into r1 but is not needed
1574 * when patching the chaining cell and will be clobbered upon
1575 * returning so it will be reconstructed again.
1576 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001577 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001578
1579 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001580 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001581 addrRetChain->generic.target = (LIR *) retChainingCell;
1582
1583 bypassRechaining->generic.target = (LIR *) addrRetChain;
1584 /*
1585 * r0 = calleeMethod,
1586 * r1 = &ChainingCell,
1587 * r4PC = callsiteDPC,
1588 */
1589 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
1590#if defined(INVOKE_STATS)
1591 gDvmJit.invokePredictedChain++;
1592#endif
1593 /* Handle exceptions using the interpreter */
1594 genTrap(cUnit, mir->offset, pcrLabel);
1595}
1596
1597/*
1598 * Up calling this function, "this" is stored in r0. The actual class will be
1599 * chased down off r0 and the predicted one will be retrieved through
1600 * predictedChainingCell then a comparison is performed to see whether the
1601 * previously established chaining is still valid.
1602 *
1603 * The return LIR is a branch based on the comparison result. The actual branch
1604 * target will be setup in the caller.
1605 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001606static ArmLIR *genCheckPredictedChain(CompilationUnit *cUnit,
1607 ArmLIR *predChainingCell,
1608 ArmLIR *retChainingCell,
Ben Cheng38329f52009-07-07 14:19:20 -07001609 MIR *mir)
1610{
Bill Buzbee1465db52009-09-23 17:17:35 -07001611 /*
1612 * Note: all Dalvik register state should be flushed to
1613 * memory by the point, so register usage restrictions no
1614 * longer apply. All temp & preserved registers may be used.
1615 */
1616 lockAllTemps(cUnit);
1617
Ben Cheng38329f52009-07-07 14:19:20 -07001618 /* r3 now contains this->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001619 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001620
1621 /*
1622 * r2 now contains predicted class. The starting offset of the
1623 * cached value is 4 bytes into the chaining cell.
1624 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001625 ArmLIR *getPredictedClass =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001626 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, clazz), r2);
Ben Cheng38329f52009-07-07 14:19:20 -07001627 getPredictedClass->generic.target = (LIR *) predChainingCell;
1628
1629 /*
1630 * r0 now contains predicted method. The starting offset of the
1631 * cached value is 8 bytes into the chaining cell.
1632 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001633 ArmLIR *getPredictedMethod =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001634 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, method), r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001635 getPredictedMethod->generic.target = (LIR *) predChainingCell;
1636
1637 /* Load the stats counter to see if it is time to unchain and refresh */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001638 ArmLIR *getRechainingRequestCount =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001639 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, counter), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001640 getRechainingRequestCount->generic.target =
1641 (LIR *) predChainingCell;
1642
1643 /* r4PC = dalvikCallsite */
1644 loadConstant(cUnit, r4PC,
1645 (int) (cUnit->method->insns + mir->offset));
1646
1647 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001648 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001649 addrRetChain->generic.target = (LIR *) retChainingCell;
1650
1651 /* Check if r2 (predicted class) == r3 (actual class) */
Bill Buzbee1465db52009-09-23 17:17:35 -07001652 opRegReg(cUnit, kOpCmp, r2, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001653
Bill Buzbee1465db52009-09-23 17:17:35 -07001654 return opCondBranch(cUnit, kArmCondEq);
Ben Cheng38329f52009-07-07 14:19:20 -07001655}
1656
Ben Chengba4fc8b2009-06-01 13:00:29 -07001657/* Geneate a branch to go back to the interpreter */
1658static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1659{
1660 /* r0 = dalvik pc */
Bill Buzbee1465db52009-09-23 17:17:35 -07001661 flushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001662 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001663 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1664 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1665 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001666 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001667}
1668
1669/*
1670 * Attempt to single step one instruction using the interpreter and return
1671 * to the compiled code for the next Dalvik instruction
1672 */
1673static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1674{
1675 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1676 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1677 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001678
1679 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
1680 flushAllRegs(cUnit);
1681
Ben Chengba4fc8b2009-06-01 13:00:29 -07001682 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1683 genPuntToInterp(cUnit, mir->offset);
1684 return;
1685 }
1686 int entryAddr = offsetof(InterpState,
1687 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001688 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001689 /* r0 = dalvik pc */
1690 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1691 /* r1 = dalvik pc of following instruction */
1692 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001693 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001694}
1695
Ben Cheng5d90c202009-11-22 23:31:11 -08001696static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001697{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001698 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001699 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001700 genExportPC(cUnit, mir);
1701 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
1702 loadValueDirectFixed(cUnit, rlSrc, r1);
1703 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001704 if (isEnter) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001705 loadConstant(cUnit, r2, (int)dvmLockObject);
1706 } else {
1707 loadConstant(cUnit, r2, (int)dvmUnlockObject);
1708 }
1709 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
1710 /* Do the call */
1711 opReg(cUnit, kOpBlx, r2);
Bill Buzbee964a7b02010-01-28 12:54:19 -08001712 /*
1713 * Refresh Jit's on/off status, which may have changed if we were
1714 * sent to VM_MONITOR state above.
1715 * TUNING: pointer chase, but must reload following call
1716 */
1717 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, ppJitProfTable), r0);
1718 loadWordDisp(cUnit, r0, 0, r0);
1719 storeWordDisp(cUnit, rGLUE, offsetof(InterpState, pJitProfTable), r0);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001720#if defined(WITH_DEADLOCK_PREDICTION)
1721 if (isEnter) {
1722 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
1723 loadWordDisp(cUnit, r0, offsetof(Thread, exception), r1);
1724 opRegImm(cUnit, kOpCmp, r1, 0);
1725 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
1726 loadConstant(cUnit, r0,
1727 (int) (cUnit->method->insns + mir->offset));
1728 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1729 /* noreturn */
1730 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1731 target->defMask = ENCODE_ALL;
1732 branchOver->generic.target = (LIR *) target;
1733 }
1734#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001735 clobberCallRegs(cUnit);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001736}
1737
Ben Chengba4fc8b2009-06-01 13:00:29 -07001738/*
1739 * The following are the first-level codegen routines that analyze the format
1740 * of each bytecode then either dispatch special purpose codegen routines
1741 * or produce corresponding Thumb instructions directly.
1742 */
1743
1744static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001745 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001746{
1747 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1748 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1749 return false;
1750}
1751
1752static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1753{
1754 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1755 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden96516932009-10-28 17:39:02 -07001756 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_EB))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001757 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1758 return true;
1759 }
1760 switch (dalvikOpCode) {
1761 case OP_RETURN_VOID:
1762 genReturnCommon(cUnit,mir);
1763 break;
1764 case OP_UNUSED_73:
1765 case OP_UNUSED_79:
1766 case OP_UNUSED_7A:
1767 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1768 return true;
1769 case OP_NOP:
1770 break;
1771 default:
1772 return true;
1773 }
1774 return false;
1775}
1776
1777static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1778{
Bill Buzbee1465db52009-09-23 17:17:35 -07001779 RegLocation rlDest;
1780 RegLocation rlResult;
1781 if (mir->ssaRep->numDefs == 2) {
1782 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1783 } else {
1784 rlDest = getDestLoc(cUnit, mir, 0);
1785 }
Ben Chenge9695e52009-06-16 16:11:47 -07001786
Ben Chengba4fc8b2009-06-01 13:00:29 -07001787 switch (mir->dalvikInsn.opCode) {
1788 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001789 case OP_CONST_4: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001790 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1791 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1792 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001793 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001794 }
1795 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001796 //TUNING: single routine to load constant pair for support doubles
Bill Buzbee964a7b02010-01-28 12:54:19 -08001797 //TUNING: load 0/-1 separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07001798 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1799 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1800 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1801 rlResult.lowReg, 31);
1802 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001803 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001804 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001805 default:
1806 return true;
1807 }
1808 return false;
1809}
1810
1811static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1812{
Bill Buzbee1465db52009-09-23 17:17:35 -07001813 RegLocation rlDest;
1814 RegLocation rlResult;
1815 if (mir->ssaRep->numDefs == 2) {
1816 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1817 } else {
1818 rlDest = getDestLoc(cUnit, mir, 0);
1819 }
1820 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001821
Ben Chengba4fc8b2009-06-01 13:00:29 -07001822 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001823 case OP_CONST_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001824 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB << 16);
1825 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001826 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001827 }
1828 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001829 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1830 0, mir->dalvikInsn.vB << 16);
1831 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001832 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001833 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001834 default:
1835 return true;
1836 }
1837 return false;
1838}
1839
1840static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1841{
1842 /* For OP_THROW_VERIFICATION_ERROR */
1843 genInterpSingleStep(cUnit, mir);
1844 return false;
1845}
1846
1847static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1848{
Bill Buzbee1465db52009-09-23 17:17:35 -07001849 RegLocation rlResult;
1850 RegLocation rlDest;
1851 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001852
Ben Chengba4fc8b2009-06-01 13:00:29 -07001853 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001854 case OP_CONST_STRING_JUMBO:
1855 case OP_CONST_STRING: {
1856 void *strPtr = (void*)
1857 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
1858 assert(strPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001859 rlDest = getDestLoc(cUnit, mir, 0);
1860 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1861 loadConstantValue(cUnit, rlResult.lowReg, (int) strPtr );
1862 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001863 break;
1864 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001865 case OP_CONST_CLASS: {
1866 void *classPtr = (void*)
1867 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1868 assert(classPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001869 rlDest = getDestLoc(cUnit, mir, 0);
1870 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1871 loadConstantValue(cUnit, rlResult.lowReg, (int) classPtr );
1872 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001873 break;
1874 }
1875 case OP_SGET_OBJECT:
1876 case OP_SGET_BOOLEAN:
1877 case OP_SGET_CHAR:
1878 case OP_SGET_BYTE:
1879 case OP_SGET_SHORT:
1880 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001881 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001882 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001883 void *fieldPtr = (void*)
1884 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
1885 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001886 rlDest = getDestLoc(cUnit, mir, 0);
1887 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1888 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001889#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001890 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001891#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001892 int regMap = rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001893 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationLoad);
1894
Jeff Hao97319a82009-08-12 16:57:15 -07001895#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001896 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001897 break;
1898 }
1899 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001900 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001901 void *fieldPtr = (void*)
1902 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Bill Buzbee1465db52009-09-23 17:17:35 -07001903 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001904 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001905 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1906 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1907 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001908#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001909 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001910#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001911 int regMap = rlResult.highReg << 16 |
1912 rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001913 selfVerificationMemOpWrapper(cUnit, regMap,
1914 &selfVerificationLoadDoubleword);
1915
Jeff Hao97319a82009-08-12 16:57:15 -07001916#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001917 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001918 break;
1919 }
1920 case OP_SPUT_OBJECT:
1921 case OP_SPUT_BOOLEAN:
1922 case OP_SPUT_CHAR:
1923 case OP_SPUT_BYTE:
1924 case OP_SPUT_SHORT:
1925 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001926 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001927 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001928 void *fieldPtr = (void*)
1929 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001930
Ben Chengba4fc8b2009-06-01 13:00:29 -07001931 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001932 rlSrc = getSrcLoc(cUnit, mir, 0);
1933 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1934 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001935#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001936 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001937#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001938 int regMap = rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001939 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationStore);
1940#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001941 break;
1942 }
1943 case OP_SPUT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001944 int tReg = allocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001945 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001946 void *fieldPtr = (void*)
1947 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001948
Ben Chengba4fc8b2009-06-01 13:00:29 -07001949 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001950 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
1951 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
1952 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001953#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001954 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001955#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001956 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001957 selfVerificationMemOpWrapper(cUnit, regMap,
1958 &selfVerificationStoreDoubleword);
1959#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001960 break;
1961 }
1962 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001963 /*
1964 * Obey the calling convention and don't mess with the register
1965 * usage.
1966 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001967 ClassObject *classPtr = (void*)
1968 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1969 assert(classPtr != NULL);
1970 assert(classPtr->status & CLASS_INITIALIZED);
Ben Cheng79d173c2009-09-29 16:12:51 -07001971 /*
1972 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001973 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001974 */
1975 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001976 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001977 genExportPC(cUnit, mir);
1978 loadConstant(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001979 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001980 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001981 opReg(cUnit, kOpBlx, r2);
1982 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001983 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001984 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1985 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001986 /*
1987 * OOM exception needs to be thrown here and cannot re-execute
1988 */
1989 loadConstant(cUnit, r0,
1990 (int) (cUnit->method->insns + mir->offset));
1991 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1992 /* noreturn */
1993
Bill Buzbee1465db52009-09-23 17:17:35 -07001994 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001995 target->defMask = ENCODE_ALL;
1996 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07001997 rlDest = getDestLoc(cUnit, mir, 0);
1998 rlResult = getReturnLoc(cUnit);
1999 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002000 break;
2001 }
2002 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07002003 /*
2004 * Obey the calling convention and don't mess with the register
2005 * usage.
2006 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002007 ClassObject *classPtr =
2008 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08002009 /*
2010 * Note: It is possible that classPtr is NULL at this point,
2011 * even though this instruction has been successfully interpreted.
2012 * If the previous interpretation had a null source, the
2013 * interpreter would not have bothered to resolve the clazz.
2014 * Bail out to the interpreter in this case, and log it
2015 * so that we can tell if it happens frequently.
2016 */
2017 if (classPtr == NULL) {
2018 LOGD("null clazz in OP_CHECK_CAST, single-stepping");
2019 genInterpSingleStep(cUnit, mir);
2020 return false;
2021 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002022 flushAllRegs(cUnit); /* Send everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002023 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002024 rlSrc = getSrcLoc(cUnit, mir, 0);
2025 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2026 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
2027 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
2028 /*
2029 * rlSrc.lowReg now contains object->clazz. Note that
2030 * it could have been allocated r0, but we're okay so long
2031 * as we don't do anything desctructive until r0 is loaded
2032 * with clazz.
2033 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002034 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002035 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
2036 loadConstant(cUnit, r2, (int)dvmInstanceofNonTrivial);
2037 opRegReg(cUnit, kOpCmp, r0, r1);
2038 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2039 opReg(cUnit, kOpBlx, r2);
2040 clobberCallRegs(cUnit);
2041 /*
2042 * If null, check cast failed - punt to the interpreter. Because
2043 * interpreter will be the one throwing, we don't need to
2044 * genExportPC() here.
2045 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002046 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002047 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002048 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002049 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002050 branch1->generic.target = (LIR *)target;
2051 branch2->generic.target = (LIR *)target;
2052 break;
2053 }
2054 default:
2055 return true;
2056 }
2057 return false;
2058}
2059
2060static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
2061{
2062 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002063 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002064 switch (dalvikOpCode) {
2065 case OP_MOVE_EXCEPTION: {
2066 int offset = offsetof(InterpState, self);
2067 int exOffset = offsetof(Thread, exception);
Bill Buzbee1465db52009-09-23 17:17:35 -07002068 int selfReg = allocTemp(cUnit);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002069 int resetReg = allocTemp(cUnit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002070 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2071 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2072 loadWordDisp(cUnit, rGLUE, offset, selfReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002073 loadConstant(cUnit, resetReg, 0);
Bill Buzbee1465db52009-09-23 17:17:35 -07002074 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
Bill Buzbeef9f33282009-11-22 12:45:30 -08002075 storeWordDisp(cUnit, selfReg, exOffset, resetReg);
Bill Buzbee1465db52009-09-23 17:17:35 -07002076 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002077 break;
2078 }
2079 case OP_MOVE_RESULT:
2080 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002081 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2082 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
2083 rlSrc.fp = rlDest.fp;
2084 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002085 break;
2086 }
2087 case OP_MOVE_RESULT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002088 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
2089 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
2090 rlSrc.fp = rlDest.fp;
2091 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002092 break;
2093 }
2094 case OP_RETURN_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002095 RegLocation rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2096 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
2097 rlDest.fp = rlSrc.fp;
2098 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002099 genReturnCommon(cUnit,mir);
2100 break;
2101 }
2102 case OP_RETURN:
2103 case OP_RETURN_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002104 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2105 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
2106 rlDest.fp = rlSrc.fp;
2107 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002108 genReturnCommon(cUnit,mir);
2109 break;
2110 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002111 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002112 case OP_MONITOR_ENTER:
Bill Buzbeed0937ef2009-12-22 16:15:39 -08002113#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08002114 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002115#else
Ben Cheng5d90c202009-11-22 23:31:11 -08002116 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002117#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07002118 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002119 case OP_THROW: {
2120 genInterpSingleStep(cUnit, mir);
2121 break;
2122 }
2123 default:
2124 return true;
2125 }
2126 return false;
2127}
2128
Bill Buzbeed45ba372009-06-15 17:00:57 -07002129static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
2130{
2131 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002132 RegLocation rlDest;
2133 RegLocation rlSrc;
2134 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07002135
Ben Chengba4fc8b2009-06-01 13:00:29 -07002136 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002137 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002138 }
2139
Bill Buzbee1465db52009-09-23 17:17:35 -07002140 if (mir->ssaRep->numUses == 2)
2141 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2142 else
2143 rlSrc = getSrcLoc(cUnit, mir, 0);
2144 if (mir->ssaRep->numDefs == 2)
2145 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2146 else
2147 rlDest = getDestLoc(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07002148
Ben Chengba4fc8b2009-06-01 13:00:29 -07002149 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002150 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002151 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002153 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002154 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002155 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002156 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002157 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002158 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002159 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002160 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002161 case OP_NEG_INT:
2162 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002163 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002164 case OP_NEG_LONG:
2165 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08002166 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002167 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002168 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002169 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002170 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002171 case OP_MOVE_WIDE:
2172 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002173 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002174 case OP_INT_TO_LONG:
2175 rlSrc = updateLoc(cUnit, rlSrc);
2176 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002177 //TUNING: shouldn't loadValueDirect already check for phys reg?
Bill Buzbee1465db52009-09-23 17:17:35 -07002178 if (rlSrc.location == kLocPhysReg) {
2179 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2180 } else {
2181 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2182 }
2183 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2184 rlResult.lowReg, 31);
2185 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002186 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002187 case OP_LONG_TO_INT:
2188 rlSrc = updateLocWide(cUnit, rlSrc);
2189 rlSrc = wideToNarrowLoc(cUnit, rlSrc);
2190 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002191 case OP_MOVE:
2192 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002193 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002194 break;
2195 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002196 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2197 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2198 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2199 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002200 break;
2201 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002202 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2203 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2204 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2205 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002206 break;
2207 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002208 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2209 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2210 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2211 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002212 break;
2213 case OP_ARRAY_LENGTH: {
2214 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002215 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2216 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2217 mir->offset, NULL);
2218 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2219 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2220 rlResult.lowReg);
2221 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002222 break;
2223 }
2224 default:
2225 return true;
2226 }
2227 return false;
2228}
2229
2230static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2231{
2232 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002233 RegLocation rlDest;
2234 RegLocation rlResult;
2235 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002236 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002237 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2238 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2239 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
Bill Buzbee964a7b02010-01-28 12:54:19 -08002240 //TUNING: do high separately to avoid load dependency
Bill Buzbee1465db52009-09-23 17:17:35 -07002241 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2242 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002243 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002244 rlDest = getDestLoc(cUnit, mir, 0);
2245 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
2246 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
2247 storeValue(cUnit, rlDest, rlResult);
2248 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002249 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002250 return false;
2251}
2252
2253/* Compare agaist zero */
2254static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002255 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002256{
2257 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002258 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002259 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2260 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2261 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002262
Bill Buzbee270c1d62009-08-13 16:58:07 -07002263//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07002264 switch (dalvikOpCode) {
2265 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002266 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002267 break;
2268 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002269 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002270 break;
2271 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002272 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002273 break;
2274 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002275 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002276 break;
2277 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002278 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002279 break;
2280 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002281 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002282 break;
2283 default:
2284 cond = 0;
2285 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
2286 dvmAbort();
2287 }
2288 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2289 /* This mostly likely will be optimized away in a later phase */
2290 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2291 return false;
2292}
2293
2294static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2295{
2296 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002297 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2298 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2299 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002300 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002301 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002302 int shiftOp = false;
2303 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002304
Ben Chengba4fc8b2009-06-01 13:00:29 -07002305 int __aeabi_idivmod(int op1, int op2);
2306 int __aeabi_idiv(int op1, int op2);
2307
2308 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002309 case OP_RSUB_INT_LIT8:
2310 case OP_RSUB_INT: {
2311 int tReg;
2312 //TUNING: add support for use of Arm rsub op
2313 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2314 tReg = allocTemp(cUnit);
2315 loadConstant(cUnit, tReg, lit);
2316 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2317 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2318 tReg, rlSrc.lowReg);
2319 storeValue(cUnit, rlDest, rlResult);
2320 return false;
2321 break;
2322 }
2323
Ben Chengba4fc8b2009-06-01 13:00:29 -07002324 case OP_ADD_INT_LIT8:
2325 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002326 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002327 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002328 case OP_MUL_INT_LIT8:
2329 case OP_MUL_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002330 op = kOpMul;
2331 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002332 case OP_AND_INT_LIT8:
2333 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002334 op = kOpAnd;
2335 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002336 case OP_OR_INT_LIT8:
2337 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002338 op = kOpOr;
2339 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002340 case OP_XOR_INT_LIT8:
2341 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002342 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002343 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002344 case OP_SHL_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002345 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002346 shiftOp = true;
2347 op = kOpLsl;
2348 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002349 case OP_SHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002350 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002351 shiftOp = true;
2352 op = kOpAsr;
2353 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002354 case OP_USHR_INT_LIT8:
Bill Buzbee0e605272009-12-01 14:28:05 -08002355 lit &= 31;
Bill Buzbee1465db52009-09-23 17:17:35 -07002356 shiftOp = true;
2357 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002358 break;
2359
2360 case OP_DIV_INT_LIT8:
2361 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002362 case OP_REM_INT_LIT8:
2363 case OP_REM_INT_LIT16:
2364 if (lit == 0) {
2365 /* Let the interpreter deal with div by 0 */
2366 genInterpSingleStep(cUnit, mir);
2367 return false;
2368 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002369 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002370 loadValueDirectFixed(cUnit, rlSrc, r0);
2371 clobberReg(cUnit, r0);
2372 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2373 (dalvikOpCode == OP_DIV_INT_LIT16)) {
2374 loadConstant(cUnit, r2, (int)__aeabi_idiv);
2375 isDiv = true;
2376 } else {
2377 loadConstant(cUnit, r2, (int)__aeabi_idivmod);
2378 isDiv = false;
2379 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002380 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002381 opReg(cUnit, kOpBlx, r2);
2382 clobberCallRegs(cUnit);
2383 if (isDiv)
2384 rlResult = getReturnLoc(cUnit);
2385 else
2386 rlResult = getReturnLocAlt(cUnit);
2387 storeValue(cUnit, rlDest, rlResult);
2388 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002389 break;
2390 default:
2391 return true;
2392 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002393 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2394 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2395 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2396 if (shiftOp && (lit == 0)) {
2397 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2398 } else {
2399 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2400 }
2401 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002402 return false;
2403}
2404
2405static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2406{
2407 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2408 int fieldOffset;
2409
2410 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2411 InstField *pInstField = (InstField *)
2412 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002413
2414 assert(pInstField != NULL);
2415 fieldOffset = pInstField->byteOffset;
2416 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002417 /* Deliberately break the code while make the compiler happy */
2418 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002419 }
2420 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002421 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002422 // Generates a call - use explicit registers
2423 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2424 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2425 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002426 void *classPtr = (void*)
2427 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2428 assert(classPtr != NULL);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002429 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002430 genExportPC(cUnit, mir);
2431 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002432 loadConstant(cUnit, r0, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002433 loadConstant(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002434 /*
2435 * "len < 0": bail to the interpreter to re-execute the
2436 * instruction
2437 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002438 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002439 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002440 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002441 opReg(cUnit, kOpBlx, r3);
2442 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002443 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002444 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2445 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002446 /*
2447 * OOM exception needs to be thrown here and cannot re-execute
2448 */
2449 loadConstant(cUnit, r0,
2450 (int) (cUnit->method->insns + mir->offset));
2451 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2452 /* noreturn */
2453
Bill Buzbee1465db52009-09-23 17:17:35 -07002454 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002455 target->defMask = ENCODE_ALL;
2456 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07002457 rlResult = getReturnLoc(cUnit);
2458 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002459 break;
2460 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002461 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002462 // May generate a call - use explicit registers
2463 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2464 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2465 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002466 ClassObject *classPtr =
2467 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
Bill Buzbee480e6782010-01-27 15:43:08 -08002468 /*
2469 * Note: It is possible that classPtr is NULL at this point,
2470 * even though this instruction has been successfully interpreted.
2471 * If the previous interpretation had a null source, the
2472 * interpreter would not have bothered to resolve the clazz.
2473 * Bail out to the interpreter in this case, and log it
2474 * so that we can tell if it happens frequently.
2475 */
2476 if (classPtr == NULL) {
2477 LOGD("null clazz in OP_INSTANCE_OF, single-stepping");
2478 genInterpSingleStep(cUnit, mir);
2479 break;
2480 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002481 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002482 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002483 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002484//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002485 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002486 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002487 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002488 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002489 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002490 /* r1 now contains object->clazz */
2491 loadConstant(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002492 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002493 opRegReg(cUnit, kOpCmp, r1, r2);
2494 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2495 genRegCopy(cUnit, r0, r1);
2496 genRegCopy(cUnit, r1, r2);
2497 opReg(cUnit, kOpBlx, r3);
2498 clobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002499 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002500 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002501 target->defMask = ENCODE_ALL;
Bill Buzbee1465db52009-09-23 17:17:35 -07002502 rlResult = getReturnLoc(cUnit);
2503 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002504 branch1->generic.target = (LIR *)target;
2505 branch2->generic.target = (LIR *)target;
2506 break;
2507 }
2508 case OP_IGET_WIDE:
2509 genIGetWide(cUnit, mir, fieldOffset);
2510 break;
2511 case OP_IGET:
2512 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002513 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002514 break;
2515 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002516 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002517 break;
2518 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002519 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002520 break;
2521 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002522 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002523 break;
2524 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002525 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002526 break;
2527 case OP_IPUT_WIDE:
2528 genIPutWide(cUnit, mir, fieldOffset);
2529 break;
2530 case OP_IPUT:
2531 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002532 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002533 break;
2534 case OP_IPUT_SHORT:
2535 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002536 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002537 break;
2538 case OP_IPUT_BYTE:
2539 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002540 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002541 break;
2542 default:
2543 return true;
2544 }
2545 return false;
2546}
2547
2548static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2549{
2550 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2551 int fieldOffset = mir->dalvikInsn.vC;
2552 switch (dalvikOpCode) {
2553 case OP_IGET_QUICK:
2554 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002555 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002556 break;
2557 case OP_IPUT_QUICK:
2558 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002559 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002560 break;
2561 case OP_IGET_WIDE_QUICK:
2562 genIGetWide(cUnit, mir, fieldOffset);
2563 break;
2564 case OP_IPUT_WIDE_QUICK:
2565 genIPutWide(cUnit, mir, fieldOffset);
2566 break;
2567 default:
2568 return true;
2569 }
2570 return false;
2571
2572}
2573
2574/* Compare agaist zero */
2575static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002576 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002577{
2578 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002579 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002580 RegLocation rlSrc1 = getSrcLoc(cUnit, mir, 0);
2581 RegLocation rlSrc2 = getSrcLoc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002582
Bill Buzbee1465db52009-09-23 17:17:35 -07002583 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2584 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2585 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002586
2587 switch (dalvikOpCode) {
2588 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002589 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002590 break;
2591 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002592 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002593 break;
2594 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002595 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002596 break;
2597 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002598 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002599 break;
2600 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002601 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002602 break;
2603 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002604 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002605 break;
2606 default:
2607 cond = 0;
2608 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
2609 dvmAbort();
2610 }
2611 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2612 /* This mostly likely will be optimized away in a later phase */
2613 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2614 return false;
2615}
2616
2617static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2618{
2619 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002620
2621 switch (opCode) {
2622 case OP_MOVE_16:
2623 case OP_MOVE_OBJECT_16:
2624 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002625 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002626 storeValue(cUnit, getDestLoc(cUnit, mir, 0),
2627 getSrcLoc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002628 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002629 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002630 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002631 case OP_MOVE_WIDE_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002632 storeValueWide(cUnit, getDestLocWide(cUnit, mir, 0, 1),
2633 getSrcLocWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002634 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002635 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002636 default:
2637 return true;
2638 }
2639 return false;
2640}
2641
2642static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2643{
2644 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002645 RegLocation rlSrc1;
2646 RegLocation rlSrc2;
2647 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002648
2649 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002650 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002651 }
2652
Bill Buzbee1465db52009-09-23 17:17:35 -07002653 /* APUTs have 3 sources and no targets */
2654 if (mir->ssaRep->numDefs == 0) {
2655 if (mir->ssaRep->numUses == 3) {
2656 rlDest = getSrcLoc(cUnit, mir, 0);
2657 rlSrc1 = getSrcLoc(cUnit, mir, 1);
2658 rlSrc2 = getSrcLoc(cUnit, mir, 2);
2659 } else {
2660 assert(mir->ssaRep->numUses == 4);
2661 rlDest = getSrcLocWide(cUnit, mir, 0, 1);
2662 rlSrc1 = getSrcLoc(cUnit, mir, 2);
2663 rlSrc2 = getSrcLoc(cUnit, mir, 3);
2664 }
2665 } else {
2666 /* Two sources and 1 dest. Deduce the operand sizes */
2667 if (mir->ssaRep->numUses == 4) {
2668 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
2669 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
2670 } else {
2671 assert(mir->ssaRep->numUses == 2);
2672 rlSrc1 = getSrcLoc(cUnit, mir, 0);
2673 rlSrc2 = getSrcLoc(cUnit, mir, 1);
2674 }
2675 if (mir->ssaRep->numDefs == 2) {
2676 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2677 } else {
2678 assert(mir->ssaRep->numDefs == 1);
2679 rlDest = getDestLoc(cUnit, mir, 0);
2680 }
2681 }
2682
2683
Ben Chengba4fc8b2009-06-01 13:00:29 -07002684 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002685 case OP_CMPL_FLOAT:
2686 case OP_CMPG_FLOAT:
2687 case OP_CMPL_DOUBLE:
2688 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002689 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002690 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002691 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002692 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002693 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002694 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002695 break;
2696 case OP_AGET:
2697 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002698 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002699 break;
2700 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002701 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002702 break;
2703 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002704 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002705 break;
2706 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002707 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002708 break;
2709 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002710 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002711 break;
2712 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002713 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002714 break;
2715 case OP_APUT:
2716 case OP_APUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002717 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002718 break;
2719 case OP_APUT_SHORT:
2720 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002721 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002722 break;
2723 case OP_APUT_BYTE:
2724 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002725 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002726 break;
2727 default:
2728 return true;
2729 }
2730 return false;
2731}
2732
Ben Cheng6c10a972009-10-29 14:39:18 -07002733/*
2734 * Find the matching case.
2735 *
2736 * return values:
2737 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2738 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2739 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2740 * above MAX_CHAINED_SWITCH_CASES).
2741 *
2742 * Instructions around the call are:
2743 *
2744 * mov r2, pc
2745 * blx &findPackedSwitchIndex
2746 * mov pc, r0
2747 * .align4
2748 * chaining cell for case 0 [8 bytes]
2749 * chaining cell for case 1 [8 bytes]
2750 * :
2751 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2752 * chaining cell for case default [8 bytes]
2753 * noChain exit
2754 */
2755s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
2756{
2757 int size;
2758 int firstKey;
2759 const int *entries;
2760 int index;
2761 int jumpIndex;
2762 int caseDPCOffset = 0;
2763 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2764 int chainingPC = (pc + 4) & ~3;
2765
2766 /*
2767 * Packed switch data format:
2768 * ushort ident = 0x0100 magic value
2769 * ushort size number of entries in the table
2770 * int first_key first (and lowest) switch case value
2771 * int targets[size] branch targets, relative to switch opcode
2772 *
2773 * Total size is (4+size*2) 16-bit code units.
2774 */
2775 size = switchData[1];
2776 assert(size > 0);
2777
2778 firstKey = switchData[2];
2779 firstKey |= switchData[3] << 16;
2780
2781
2782 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2783 * we can treat them as a native int array.
2784 */
2785 entries = (const int*) &switchData[4];
2786 assert(((u4)entries & 0x3) == 0);
2787
2788 index = testVal - firstKey;
2789
2790 /* Jump to the default cell */
2791 if (index < 0 || index >= size) {
2792 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2793 /* Jump to the non-chaining exit point */
2794 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2795 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2796 caseDPCOffset = entries[index];
2797 /* Jump to the inline chaining cell */
2798 } else {
2799 jumpIndex = index;
2800 }
2801
2802 chainingPC += jumpIndex * 8;
2803 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2804}
2805
2806/* See comments for findPackedSwitchIndex */
2807s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
2808{
2809 int size;
2810 const int *keys;
2811 const int *entries;
2812 int chainingPC = (pc + 4) & ~3;
2813 int i;
2814
2815 /*
2816 * Sparse switch data format:
2817 * ushort ident = 0x0200 magic value
2818 * ushort size number of entries in the table; > 0
2819 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2820 * int targets[size] branch targets, relative to switch opcode
2821 *
2822 * Total size is (2+size*4) 16-bit code units.
2823 */
2824
2825 size = switchData[1];
2826 assert(size > 0);
2827
2828 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2829 * we can treat them as a native int array.
2830 */
2831 keys = (const int*) &switchData[2];
2832 assert(((u4)keys & 0x3) == 0);
2833
2834 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2835 * we can treat them as a native int array.
2836 */
2837 entries = keys + size;
2838 assert(((u4)entries & 0x3) == 0);
2839
2840 /*
2841 * Run through the list of keys, which are guaranteed to
2842 * be sorted low-to-high.
2843 *
2844 * Most tables have 3-4 entries. Few have more than 10. A binary
2845 * search here is probably not useful.
2846 */
2847 for (i = 0; i < size; i++) {
2848 int k = keys[i];
2849 if (k == testVal) {
2850 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2851 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2852 i : MAX_CHAINED_SWITCH_CASES + 1;
2853 chainingPC += jumpIndex * 8;
2854 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2855 } else if (k > testVal) {
2856 break;
2857 }
2858 }
2859 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2860}
2861
Ben Chengba4fc8b2009-06-01 13:00:29 -07002862static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2863{
2864 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2865 switch (dalvikOpCode) {
2866 case OP_FILL_ARRAY_DATA: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002867 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2868 // Making a call - use explicit registers
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002869 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002870 genExportPC(cUnit, mir);
2871 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002872 loadConstant(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002873 loadConstant(cUnit, r1,
2874 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002875 opReg(cUnit, kOpBlx, r2);
2876 clobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002877 /* generate a branch over if successful */
2878 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2879 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2880 loadConstant(cUnit, r0,
2881 (int) (cUnit->method->insns + mir->offset));
2882 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2883 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2884 target->defMask = ENCODE_ALL;
2885 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002886 break;
2887 }
2888 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002889 * Compute the goto target of up to
2890 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2891 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002892 */
2893 case OP_PACKED_SWITCH:
2894 case OP_SPARSE_SWITCH: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002895 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002896 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002897 loadValueDirectFixed(cUnit, rlSrc, r1);
2898 lockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002899 const u2 *switchData =
2900 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2901 u2 size = switchData[1];
2902
Ben Chengba4fc8b2009-06-01 13:00:29 -07002903 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Cheng6c10a972009-10-29 14:39:18 -07002904 loadConstant(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002905 } else {
Ben Cheng6c10a972009-10-29 14:39:18 -07002906 loadConstant(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002907 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002908 /* r0 <- Addr of the switch data */
2909 loadConstant(cUnit, r0,
2910 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2911 /* r2 <- pc of the instruction following the blx */
2912 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002913 opReg(cUnit, kOpBlx, r4PC);
2914 clobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002915 /* pc <- computed goto target */
2916 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002917 break;
2918 }
2919 default:
2920 return true;
2921 }
2922 return false;
2923}
2924
2925static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002926 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002927{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002928 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002929 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002930
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002931 if (bb->fallThrough != NULL)
2932 retChainingCell = &labelList[bb->fallThrough->id];
2933
Ben Chengba4fc8b2009-06-01 13:00:29 -07002934 DecodedInstruction *dInsn = &mir->dalvikInsn;
2935 switch (mir->dalvikInsn.opCode) {
2936 /*
2937 * calleeMethod = this->clazz->vtable[
2938 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2939 * ]
2940 */
2941 case OP_INVOKE_VIRTUAL:
2942 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002943 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002944 int methodIndex =
2945 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2946 methodIndex;
2947
2948 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2949 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2950 else
2951 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2952
Ben Cheng38329f52009-07-07 14:19:20 -07002953 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2954 retChainingCell,
2955 predChainingCell,
2956 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002957 break;
2958 }
2959 /*
2960 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2961 * ->pResMethods[BBBB]->methodIndex]
2962 */
2963 /* TODO - not excersized in RunPerf.jar */
2964 case OP_INVOKE_SUPER:
2965 case OP_INVOKE_SUPER_RANGE: {
2966 int mIndex = cUnit->method->clazz->pDvmDex->
2967 pResMethods[dInsn->vB]->methodIndex;
2968 const Method *calleeMethod =
2969 cUnit->method->clazz->super->vtable[mIndex];
2970
2971 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2972 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2973 else
2974 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2975
2976 /* r0 = calleeMethod */
2977 loadConstant(cUnit, r0, (int) calleeMethod);
2978
Ben Cheng38329f52009-07-07 14:19:20 -07002979 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2980 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002981 break;
2982 }
2983 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2984 case OP_INVOKE_DIRECT:
2985 case OP_INVOKE_DIRECT_RANGE: {
2986 const Method *calleeMethod =
2987 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2988
2989 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2990 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2991 else
2992 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2993
2994 /* r0 = calleeMethod */
2995 loadConstant(cUnit, r0, (int) calleeMethod);
2996
Ben Cheng38329f52009-07-07 14:19:20 -07002997 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2998 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002999 break;
3000 }
3001 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
3002 case OP_INVOKE_STATIC:
3003 case OP_INVOKE_STATIC_RANGE: {
3004 const Method *calleeMethod =
3005 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
3006
3007 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
3008 genProcessArgsNoRange(cUnit, mir, dInsn,
3009 NULL /* no null check */);
3010 else
3011 genProcessArgsRange(cUnit, mir, dInsn,
3012 NULL /* no null check */);
3013
3014 /* r0 = calleeMethod */
3015 loadConstant(cUnit, r0, (int) calleeMethod);
3016
Ben Cheng38329f52009-07-07 14:19:20 -07003017 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3018 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003019 break;
3020 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003021 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003022 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
3023 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07003024 *
3025 * Given "invoke-interface {v0}", the following is the generated code:
3026 *
3027 * 0x426a9abe : ldr r0, [r5, #0] --+
3028 * 0x426a9ac0 : mov r7, r5 |
3029 * 0x426a9ac2 : sub r7, #24 |
3030 * 0x426a9ac4 : cmp r0, #0 | genProcessArgsNoRange
3031 * 0x426a9ac6 : beq 0x426a9afe |
3032 * 0x426a9ac8 : stmia r7, <r0> --+
3033 * 0x426a9aca : ldr r4, [pc, #104] --> r4 <- dalvikPC of this invoke
3034 * 0x426a9acc : add r1, pc, #52 --> r1 <- &retChainingCell
3035 * 0x426a9ace : add r2, pc, #60 --> r2 <- &predictedChainingCell
3036 * 0x426a9ad0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_
3037 * 0x426a9ad2 : blx_2 see above --+ PREDICTED_CHAIN
3038 * 0x426a9ad4 : b 0x426a9b0c --> off to the predicted chain
3039 * 0x426a9ad6 : b 0x426a9afe --> punt to the interpreter
Ben Chenga8e64a72009-10-20 13:01:36 -07003040 * 0x426a9ad8 : mov r8, r1 --+
3041 * 0x426a9ada : mov r9, r2 |
3042 * 0x426a9adc : mov r10, r3 |
Ben Cheng38329f52009-07-07 14:19:20 -07003043 * 0x426a9ade : mov r0, r3 |
3044 * 0x426a9ae0 : mov r1, #74 | dvmFindInterfaceMethodInCache
3045 * 0x426a9ae2 : ldr r2, [pc, #76] |
3046 * 0x426a9ae4 : ldr r3, [pc, #68] |
3047 * 0x426a9ae6 : ldr r7, [pc, #64] |
3048 * 0x426a9ae8 : blx r7 --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003049 * 0x426a9aea : mov r1, r8 --> r1 <- rechain count
Ben Cheng38329f52009-07-07 14:19:20 -07003050 * 0x426a9aec : cmp r1, #0 --> compare against 0
3051 * 0x426a9aee : bgt 0x426a9af8 --> >=0? don't rechain
3052 * 0x426a9af0 : ldr r7, [r6, #96] --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003053 * 0x426a9af2 : mov r2, r9 | dvmJitToPatchPredictedChain
3054 * 0x426a9af4 : mov r3, r10 |
Ben Cheng38329f52009-07-07 14:19:20 -07003055 * 0x426a9af6 : blx r7 --+
3056 * 0x426a9af8 : add r1, pc, #8 --> r1 <- &retChainingCell
3057 * 0x426a9afa : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3058 * 0x426a9afc : blx_2 see above --+
3059 * -------- reconstruct dalvik PC : 0x428b786c @ +0x001e
3060 * 0x426a9afe (0042): ldr r0, [pc, #52]
3061 * Exception_Handling:
3062 * 0x426a9b00 (0044): ldr r1, [r6, #84]
3063 * 0x426a9b02 (0046): blx r1
3064 * 0x426a9b04 (0048): .align4
3065 * -------- chaining cell (hot): 0x0021
3066 * 0x426a9b04 (0048): ldr r0, [r6, #92]
3067 * 0x426a9b06 (004a): blx r0
3068 * 0x426a9b08 (004c): data 0x7872(30834)
3069 * 0x426a9b0a (004e): data 0x428b(17035)
3070 * 0x426a9b0c (0050): .align4
3071 * -------- chaining cell (predicted)
3072 * 0x426a9b0c (0050): data 0x0000(0) --> will be patched into bx
3073 * 0x426a9b0e (0052): data 0x0000(0)
3074 * 0x426a9b10 (0054): data 0x0000(0) --> class
3075 * 0x426a9b12 (0056): data 0x0000(0)
3076 * 0x426a9b14 (0058): data 0x0000(0) --> method
3077 * 0x426a9b16 (005a): data 0x0000(0)
3078 * 0x426a9b18 (005c): data 0x0000(0) --> reset count
3079 * 0x426a9b1a (005e): data 0x0000(0)
3080 * 0x426a9b28 (006c): .word (0xad0392a5)
3081 * 0x426a9b2c (0070): .word (0x6e750)
3082 * 0x426a9b30 (0074): .word (0x4109a618)
3083 * 0x426a9b34 (0078): .word (0x428b786c)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003084 */
3085 case OP_INVOKE_INTERFACE:
3086 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003087 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003088 int methodIndex = dInsn->vB;
3089
Bill Buzbee1465db52009-09-23 17:17:35 -07003090 /* Ensure that nothing is both live and dirty */
3091 flushAllRegs(cUnit);
3092
Ben Chengba4fc8b2009-06-01 13:00:29 -07003093 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
3094 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3095 else
3096 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3097
Ben Cheng38329f52009-07-07 14:19:20 -07003098 /* "this" is already left in r0 by genProcessArgs* */
3099
3100 /* r4PC = dalvikCallsite */
3101 loadConstant(cUnit, r4PC,
3102 (int) (cUnit->method->insns + mir->offset));
3103
3104 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003105 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003106 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003107 addrRetChain->generic.target = (LIR *) retChainingCell;
3108
3109 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003110 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003111 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003112 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3113
3114 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
3115
3116 /* return through lr - jump to the chaining cell */
3117 genUnconditionalBranch(cUnit, predChainingCell);
3118
3119 /*
3120 * null-check on "this" may have been eliminated, but we still need
3121 * a PC-reconstruction label for stack overflow bailout.
3122 */
3123 if (pcrLabel == NULL) {
3124 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003125 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003126 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07003127 pcrLabel->operands[0] = dPC;
3128 pcrLabel->operands[1] = mir->offset;
3129 /* Insert the place holder to the growable list */
3130 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3131 }
3132
3133 /* return through lr+2 - punt to the interpreter */
3134 genUnconditionalBranch(cUnit, pcrLabel);
3135
3136 /*
3137 * return through lr+4 - fully resolve the callee method.
3138 * r1 <- count
3139 * r2 <- &predictedChainCell
3140 * r3 <- this->class
3141 * r4 <- dPC
3142 * r7 <- this->class->vtable
3143 */
3144
3145 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003146 genRegCopy(cUnit, r8, r1);
3147 genRegCopy(cUnit, r9, r2);
3148 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003149
Ben Chengba4fc8b2009-06-01 13:00:29 -07003150 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003151 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003152
3153 /* r1 = BBBB */
3154 loadConstant(cUnit, r1, dInsn->vB);
3155
3156 /* r2 = method (caller) */
3157 loadConstant(cUnit, r2, (int) cUnit->method);
3158
3159 /* r3 = pDvmDex */
3160 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3161
3162 loadConstant(cUnit, r7,
3163 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003164 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003165
3166 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3167
Bill Buzbee1465db52009-09-23 17:17:35 -07003168 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003169
Ben Cheng38329f52009-07-07 14:19:20 -07003170 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07003171 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003172
Bill Buzbee1465db52009-09-23 17:17:35 -07003173 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07003174
Bill Buzbee270c1d62009-08-13 16:58:07 -07003175 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3176 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003177
Bill Buzbee1465db52009-09-23 17:17:35 -07003178 genRegCopy(cUnit, r2, r9);
3179 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003180
3181 /*
3182 * r0 = calleeMethod
3183 * r2 = &predictedChainingCell
3184 * r3 = class
3185 *
3186 * &returnChainingCell has been loaded into r1 but is not needed
3187 * when patching the chaining cell and will be clobbered upon
3188 * returning so it will be reconstructed again.
3189 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003190 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003191
3192 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003193 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003194 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003195
3196 bypassRechaining->generic.target = (LIR *) addrRetChain;
3197
Ben Chengba4fc8b2009-06-01 13:00:29 -07003198 /*
3199 * r0 = this, r1 = calleeMethod,
3200 * r1 = &ChainingCell,
3201 * r4PC = callsiteDPC,
3202 */
3203 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
3204#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07003205 gDvmJit.invokePredictedChain++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003206#endif
3207 /* Handle exceptions using the interpreter */
3208 genTrap(cUnit, mir->offset, pcrLabel);
3209 break;
3210 }
3211 /* NOP */
3212 case OP_INVOKE_DIRECT_EMPTY: {
3213 return false;
3214 }
3215 case OP_FILLED_NEW_ARRAY:
3216 case OP_FILLED_NEW_ARRAY_RANGE: {
3217 /* Just let the interpreter deal with these */
3218 genInterpSingleStep(cUnit, mir);
3219 break;
3220 }
3221 default:
3222 return true;
3223 }
3224 return false;
3225}
3226
3227static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003228 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003229{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003230 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3231 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3232 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003233
3234 DecodedInstruction *dInsn = &mir->dalvikInsn;
3235 switch (mir->dalvikInsn.opCode) {
3236 /* calleeMethod = this->clazz->vtable[BBBB] */
3237 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3238 case OP_INVOKE_VIRTUAL_QUICK: {
3239 int methodIndex = dInsn->vB;
3240 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3241 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3242 else
3243 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3244
Ben Cheng38329f52009-07-07 14:19:20 -07003245 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3246 retChainingCell,
3247 predChainingCell,
3248 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003249 break;
3250 }
3251 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3252 case OP_INVOKE_SUPER_QUICK:
3253 case OP_INVOKE_SUPER_QUICK_RANGE: {
3254 const Method *calleeMethod =
3255 cUnit->method->clazz->super->vtable[dInsn->vB];
3256
3257 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3258 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3259 else
3260 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3261
3262 /* r0 = calleeMethod */
3263 loadConstant(cUnit, r0, (int) calleeMethod);
3264
Ben Cheng38329f52009-07-07 14:19:20 -07003265 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3266 calleeMethod);
3267 /* Handle exceptions using the interpreter */
3268 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003269 break;
3270 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003271 default:
3272 return true;
3273 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003274 return false;
3275}
3276
3277/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003278 * This operation is complex enough that we'll do it partly inline
3279 * and partly with a handler. NOTE: the handler uses hardcoded
3280 * values for string object offsets and must be revisitied if the
3281 * layout changes.
3282 */
3283static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3284{
3285#if defined(USE_GLOBAL_STRING_DEFS)
3286 return false;
3287#else
3288 ArmLIR *rollback;
3289 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3290 RegLocation rlComp = getSrcLoc(cUnit, mir, 1);
3291
3292 loadValueDirectFixed(cUnit, rlThis, r0);
3293 loadValueDirectFixed(cUnit, rlComp, r1);
3294 /* Test objects for NULL */
3295 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3296 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3297 /*
3298 * TUNING: we could check for object pointer equality before invoking
3299 * handler. Unclear whether the gain would be worth the added code size
3300 * expansion.
3301 */
3302 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
3303 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3304 return true;
3305#endif
3306}
3307
3308static bool genInlinedIndexOf(CompilationUnit *cUnit, MIR *mir, bool singleI)
3309{
3310#if defined(USE_GLOBAL_STRING_DEFS)
3311 return false;
3312#else
3313 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3314 RegLocation rlChar = getSrcLoc(cUnit, mir, 1);
3315
3316 loadValueDirectFixed(cUnit, rlThis, r0);
3317 loadValueDirectFixed(cUnit, rlChar, r1);
3318 if (!singleI) {
3319 RegLocation rlStart = getSrcLoc(cUnit, mir, 2);
3320 loadValueDirectFixed(cUnit, rlStart, r2);
3321 } else {
3322 loadConstant(cUnit, r2, 0);
3323 }
3324 /* Test objects for NULL */
3325 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3326 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
3327 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3328 return true;
3329#endif
3330}
3331
3332
3333/*
Bill Buzbeece46c942009-11-20 15:41:34 -08003334 * NOTE: Handles both range and non-range versions (arguments
3335 * have already been normalized by this point).
Ben Chengba4fc8b2009-06-01 13:00:29 -07003336 */
Bill Buzbeece46c942009-11-20 15:41:34 -08003337static bool handleExecuteInline(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003338{
3339 DecodedInstruction *dInsn = &mir->dalvikInsn;
3340 switch( mir->dalvikInsn.opCode) {
Bill Buzbeece46c942009-11-20 15:41:34 -08003341 case OP_EXECUTE_INLINE_RANGE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003342 case OP_EXECUTE_INLINE: {
3343 unsigned int i;
3344 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003345 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003346 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003347 int tReg1;
3348 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003349 switch (operation) {
3350 case INLINE_EMPTYINLINEMETHOD:
3351 return false; /* Nop */
3352 case INLINE_STRING_LENGTH:
3353 return genInlinedStringLength(cUnit, mir);
3354 case INLINE_MATH_ABS_INT:
3355 return genInlinedAbsInt(cUnit, mir);
3356 case INLINE_MATH_ABS_LONG:
3357 return genInlinedAbsLong(cUnit, mir);
3358 case INLINE_MATH_MIN_INT:
3359 return genInlinedMinMaxInt(cUnit, mir, true);
3360 case INLINE_MATH_MAX_INT:
3361 return genInlinedMinMaxInt(cUnit, mir, false);
3362 case INLINE_STRING_CHARAT:
3363 return genInlinedStringCharAt(cUnit, mir);
3364 case INLINE_MATH_SQRT:
3365 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003366 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003367 else
3368 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003369 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003370 if (genInlinedAbsFloat(cUnit, mir))
3371 return false;
3372 else
3373 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003374 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003375 if (genInlinedAbsDouble(cUnit, mir))
3376 return false;
3377 else
3378 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003379 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003380 if (genInlinedCompareTo(cUnit, mir))
3381 return false;
3382 else
3383 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003384 case INLINE_STRING_INDEXOF_I:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003385 if (genInlinedIndexOf(cUnit, mir, true /* I */))
3386 return false;
3387 else
3388 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003389 case INLINE_STRING_INDEXOF_II:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003390 if (genInlinedIndexOf(cUnit, mir, false /* I */))
3391 return false;
3392 else
3393 break;
3394 case INLINE_STRING_EQUALS:
3395 case INLINE_MATH_COS:
3396 case INLINE_MATH_SIN:
3397 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003398 default:
3399 dvmAbort();
Ben Chengba4fc8b2009-06-01 13:00:29 -07003400 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003401 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07003402 clobberCallRegs(cUnit);
3403 clobberReg(cUnit, r4PC);
3404 clobberReg(cUnit, r7);
3405 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3406 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003407 loadConstant(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003408 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003409 for (i=0; i < dInsn->vA; i++) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003410 loadValueDirect(cUnit, getSrcLoc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003411 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003412 opReg(cUnit, kOpBlx, r4PC);
3413 opRegImm(cUnit, kOpAdd, r13, 8);
Bill Buzbeece46c942009-11-20 15:41:34 -08003414 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
3415 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
3416 loadConstant(cUnit, r0,
3417 (int) (cUnit->method->insns + mir->offset));
3418 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
3419 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
3420 target->defMask = ENCODE_ALL;
3421 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003422 break;
3423 }
3424 default:
3425 return true;
3426 }
3427 return false;
3428}
3429
3430static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3431{
Bill Buzbee1465db52009-09-23 17:17:35 -07003432 //TUNING: We're using core regs here - not optimal when target is a double
3433 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
3434 RegLocation rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
3435 loadConstantValue(cUnit, rlResult.lowReg,
3436 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3437 loadConstantValue(cUnit, rlResult.highReg,
3438 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
3439 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003440 return false;
3441}
3442
Ben Chengba4fc8b2009-06-01 13:00:29 -07003443/*
3444 * The following are special processing routines that handle transfer of
3445 * controls between compiled code and the interpreter. Certain VM states like
3446 * Dalvik PC and special-purpose registers are reconstructed here.
3447 */
3448
Ben Cheng1efc9c52009-06-08 18:25:27 -07003449/* Chaining cell for code that may need warmup. */
3450static void handleNormalChainingCell(CompilationUnit *cUnit,
3451 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003452{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003453 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3454 jitToInterpEntries.dvmJitToInterpNormal), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003455 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003456 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3457}
3458
3459/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003460 * Chaining cell for instructions that immediately following already translated
3461 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003462 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003463static void handleHotChainingCell(CompilationUnit *cUnit,
3464 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003465{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003466 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3467 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003468 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003469 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3470}
3471
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003472#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003473/* Chaining cell for branches that branch back into the same basic block */
3474static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3475 unsigned int offset)
3476{
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003477#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003478 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Jeff Hao97319a82009-08-12 16:57:15 -07003479 offsetof(InterpState, jitToInterpEntries.dvmJitToBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003480#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003481 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003482 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3483#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003484 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003485 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3486}
3487
3488#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003489/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003490static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3491 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003492{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003493 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3494 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003495 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003496 addWordData(cUnit, (int) (callee->insns), true);
3497}
3498
Ben Cheng38329f52009-07-07 14:19:20 -07003499/* Chaining cell for monomorphic method invocations. */
3500static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3501{
3502
3503 /* Should not be executed in the initial state */
3504 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3505 /* To be filled: class */
3506 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3507 /* To be filled: method */
3508 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3509 /*
3510 * Rechain count. The initial value of 0 here will trigger chaining upon
3511 * the first invocation of this callsite.
3512 */
3513 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3514}
3515
Ben Chengba4fc8b2009-06-01 13:00:29 -07003516/* Load the Dalvik PC into r0 and jump to the specified target */
3517static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003518 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003519{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003520 ArmLIR **pcrLabel =
3521 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003522 int numElems = cUnit->pcReconstructionList.numUsed;
3523 int i;
3524 for (i = 0; i < numElems; i++) {
3525 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3526 /* r0 = dalvik PC */
3527 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3528 genUnconditionalBranch(cUnit, targetLabel);
3529 }
3530}
3531
Bill Buzbee1465db52009-09-23 17:17:35 -07003532static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3533 "kMirOpPhi",
3534 "kMirOpNullNRangeUpCheck",
3535 "kMirOpNullNRangeDownCheck",
3536 "kMirOpLowerBound",
3537 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003538};
3539
3540/*
3541 * vA = arrayReg;
3542 * vB = idxReg;
3543 * vC = endConditionReg;
3544 * arg[0] = maxC
3545 * arg[1] = minC
3546 * arg[2] = loopBranchConditionCode
3547 */
3548static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3549{
Bill Buzbee1465db52009-09-23 17:17:35 -07003550 /*
3551 * NOTE: these synthesized blocks don't have ssa names assigned
3552 * for Dalvik registers. However, because they dominate the following
3553 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3554 * ssa name.
3555 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003556 DecodedInstruction *dInsn = &mir->dalvikInsn;
3557 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003558 const int maxC = dInsn->arg[0];
3559 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003560 int regLength;
3561 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3562 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003563
3564 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003565 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3566 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3567 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003568 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3569
3570 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003571 regLength = allocTemp(cUnit);
3572 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003573
3574 int delta = maxC;
3575 /*
3576 * If the loop end condition is ">=" instead of ">", then the largest value
3577 * of the index is "endCondition - 1".
3578 */
3579 if (dInsn->arg[2] == OP_IF_GE) {
3580 delta--;
3581 }
3582
3583 if (delta) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003584 int tReg = allocTemp(cUnit);
3585 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3586 rlIdxEnd.lowReg = tReg;
3587 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003588 }
3589 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003590 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003591 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003592}
3593
3594/*
3595 * vA = arrayReg;
3596 * vB = idxReg;
3597 * vC = endConditionReg;
3598 * arg[0] = maxC
3599 * arg[1] = minC
3600 * arg[2] = loopBranchConditionCode
3601 */
3602static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3603{
3604 DecodedInstruction *dInsn = &mir->dalvikInsn;
3605 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07003606 const int regLength = allocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003607 const int maxC = dInsn->arg[0];
3608 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003609 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3610 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003611
3612 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003613 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3614 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3615 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003616 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3617
3618 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003619 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003620
3621 if (maxC) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003622 int tReg = allocTemp(cUnit);
3623 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3624 rlIdxInit.lowReg = tReg;
3625 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003626 }
3627
3628 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003629 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003630 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003631}
3632
3633/*
3634 * vA = idxReg;
3635 * vB = minC;
3636 */
3637static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3638{
3639 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003640 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003641 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003642
3643 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003644 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003645
3646 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003647 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003648 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3649}
3650
3651/* Extended MIR instructions like PHI */
3652static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3653{
Bill Buzbee1465db52009-09-23 17:17:35 -07003654 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003655 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3656 false);
3657 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003658 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003659
3660 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003661 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003662 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003663 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003664 break;
3665 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003666 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003667 genHoistedChecksForCountUpLoop(cUnit, mir);
3668 break;
3669 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003670 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003671 genHoistedChecksForCountDownLoop(cUnit, mir);
3672 break;
3673 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003674 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003675 genHoistedLowerBoundCheck(cUnit, mir);
3676 break;
3677 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003678 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003679 genUnconditionalBranch(cUnit,
3680 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3681 break;
3682 }
3683 default:
3684 break;
3685 }
3686}
3687
3688/*
3689 * Create a PC-reconstruction cell for the starting offset of this trace.
3690 * Since the PCR cell is placed near the end of the compiled code which is
3691 * usually out of range for a conditional branch, we put two branches (one
3692 * branch over to the loop body and one layover branch to the actual PCR) at the
3693 * end of the entry block.
3694 */
3695static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3696 ArmLIR *bodyLabel)
3697{
3698 /* Set up the place holder to reconstruct this Dalvik PC */
3699 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003700 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng4238ec22009-08-24 16:32:22 -07003701 pcrLabel->operands[0] =
3702 (int) (cUnit->method->insns + entry->startOffset);
3703 pcrLabel->operands[1] = entry->startOffset;
3704 /* Insert the place holder to the growable list */
3705 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3706
3707 /*
3708 * Next, create two branches - one branch over to the loop body and the
3709 * other branch to the PCR cell to punt.
3710 */
3711 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003712 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003713 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003714 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003715 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3716
3717 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003718 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003719 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003720 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003721 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3722}
3723
Ben Chengba4fc8b2009-06-01 13:00:29 -07003724void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3725{
3726 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003727 ArmLIR *labelList =
3728 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Ben Chengcec26f62010-01-15 15:29:33 -08003729 GrowableList chainingListByType[kChainingCellGap];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003730 int i;
3731
3732 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003733 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003734 */
Ben Chengcec26f62010-01-15 15:29:33 -08003735 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003736 dvmInitGrowableList(&chainingListByType[i], 2);
3737 }
3738
3739 BasicBlock **blockList = cUnit->blockList;
3740
Bill Buzbee6e963e12009-06-17 16:56:19 -07003741 if (cUnit->executionCount) {
3742 /*
3743 * Reserve 6 bytes at the beginning of the trace
3744 * +----------------------------+
3745 * | execution count (4 bytes) |
3746 * +----------------------------+
3747 * | chain cell offset (2 bytes)|
3748 * +----------------------------+
3749 * ...and then code to increment the execution
3750 * count:
3751 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3752 * sub r0, #10 @ back up to addr of executionCount
3753 * ldr r1, [r0]
3754 * add r1, #1
3755 * str r1, [r0]
3756 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003757 newLIR1(cUnit, kArm16BitData, 0);
3758 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003759 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003760 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003761 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003762 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003763 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3764 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3765 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3766 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3767 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003768 } else {
3769 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003770 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003771 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003772 cUnit->headerSize = 2;
3773 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003774
Ben Chengba4fc8b2009-06-01 13:00:29 -07003775 /* Handle the content in each basic block */
3776 for (i = 0; i < cUnit->numBlocks; i++) {
3777 blockList[i]->visited = true;
3778 MIR *mir;
3779
3780 labelList[i].operands[0] = blockList[i]->startOffset;
3781
Ben Chengcec26f62010-01-15 15:29:33 -08003782 if (blockList[i]->blockType >= kChainingCellGap) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003783 /*
3784 * Append the label pseudo LIR first. Chaining cells will be handled
3785 * separately afterwards.
3786 */
3787 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3788 }
3789
Bill Buzbee1465db52009-09-23 17:17:35 -07003790 if (blockList[i]->blockType == kEntryBlock) {
3791 labelList[i].opCode = ARM_PSEUDO_kEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003792 if (blockList[i]->firstMIRInsn == NULL) {
3793 continue;
3794 } else {
3795 setupLoopEntryBlock(cUnit, blockList[i],
3796 &labelList[blockList[i]->fallThrough->id]);
3797 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003798 } else if (blockList[i]->blockType == kExitBlock) {
3799 labelList[i].opCode = ARM_PSEUDO_kExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003800 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003801 } else if (blockList[i]->blockType == kDalvikByteCode) {
3802 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003803 /* Reset the register state */
Bill Buzbee1465db52009-09-23 17:17:35 -07003804 resetRegPool(cUnit);
3805 clobberAllRegs(cUnit);
3806 resetNullCheckTracker(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003807 } else {
3808 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003809 case kChainingCellNormal:
3810 labelList[i].opCode = ARM_PSEUDO_kChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003811 /* handle the codegen later */
3812 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003813 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003814 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003815 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003816 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003817 ARM_PSEUDO_kChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003818 labelList[i].operands[0] =
3819 (int) blockList[i]->containingMethod;
3820 /* handle the codegen later */
3821 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003822 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003823 (void *) i);
3824 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003825 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003826 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003827 ARM_PSEUDO_kChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003828 /* handle the codegen later */
3829 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003830 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003831 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003832 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003833 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003834 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003835 ARM_PSEUDO_kChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003836 /* handle the codegen later */
3837 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003838 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003839 (void *) i);
3840 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003841 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003842 /* Make sure exception handling block is next */
3843 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003844 ARM_PSEUDO_kPCReconstruction_BLOCK_LABEL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003845 assert (i == cUnit->numBlocks - 2);
3846 handlePCReconstruction(cUnit, &labelList[i+1]);
3847 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003848 case kExceptionHandling:
3849 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003850 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003851 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3852 jitToInterpEntries.dvmJitToInterpPunt),
3853 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003854 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003855 }
3856 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003857#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003858 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003859 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003860 ARM_PSEUDO_kChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003861 /* handle the codegen later */
3862 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003863 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003864 (void *) i);
3865 break;
3866#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003867 default:
3868 break;
3869 }
3870 continue;
3871 }
Ben Chenge9695e52009-06-16 16:11:47 -07003872
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003873 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003874
Ben Chengba4fc8b2009-06-01 13:00:29 -07003875 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003876
3877 resetRegPool(cUnit);
3878 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
3879 clobberAllRegs(cUnit);
3880 }
3881
3882 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
3883 resetDefTracking(cUnit);
3884 }
3885
3886 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003887 handleExtendedMIR(cUnit, mir);
3888 continue;
3889 }
3890
Bill Buzbee1465db52009-09-23 17:17:35 -07003891
Ben Chengba4fc8b2009-06-01 13:00:29 -07003892 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3893 InstructionFormat dalvikFormat =
3894 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003895 ArmLIR *boundaryLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003896 newLIR2(cUnit, ARM_PSEUDO_kDalvikByteCode_BOUNDARY,
Ben Chengccd6c012009-10-15 14:52:45 -07003897 mir->offset,
3898 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3899 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003900 if (mir->ssaRep) {
3901 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003902 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003903 }
3904
Ben Chenge9695e52009-06-16 16:11:47 -07003905 /* Remember the first LIR for this block */
3906 if (headLIR == NULL) {
3907 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003908 /* Set the first boundaryLIR as a scheduling barrier */
3909 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003910 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003911
Ben Chengba4fc8b2009-06-01 13:00:29 -07003912 bool notHandled;
3913 /*
3914 * Debugging: screen the opcode first to see if it is in the
3915 * do[-not]-compile list
3916 */
3917 bool singleStepMe =
3918 gDvmJit.includeSelectedOp !=
3919 ((gDvmJit.opList[dalvikOpCode >> 3] &
3920 (1 << (dalvikOpCode & 0x7))) !=
3921 0);
3922 if (singleStepMe || cUnit->allSingleStep) {
3923 notHandled = false;
3924 genInterpSingleStep(cUnit, mir);
3925 } else {
3926 opcodeCoverage[dalvikOpCode]++;
3927 switch (dalvikFormat) {
3928 case kFmt10t:
3929 case kFmt20t:
3930 case kFmt30t:
3931 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3932 mir, blockList[i], labelList);
3933 break;
3934 case kFmt10x:
3935 notHandled = handleFmt10x(cUnit, mir);
3936 break;
3937 case kFmt11n:
3938 case kFmt31i:
3939 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3940 break;
3941 case kFmt11x:
3942 notHandled = handleFmt11x(cUnit, mir);
3943 break;
3944 case kFmt12x:
3945 notHandled = handleFmt12x(cUnit, mir);
3946 break;
3947 case kFmt20bc:
3948 notHandled = handleFmt20bc(cUnit, mir);
3949 break;
3950 case kFmt21c:
3951 case kFmt31c:
3952 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3953 break;
3954 case kFmt21h:
3955 notHandled = handleFmt21h(cUnit, mir);
3956 break;
3957 case kFmt21s:
3958 notHandled = handleFmt21s(cUnit, mir);
3959 break;
3960 case kFmt21t:
3961 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3962 labelList);
3963 break;
3964 case kFmt22b:
3965 case kFmt22s:
3966 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3967 break;
3968 case kFmt22c:
3969 notHandled = handleFmt22c(cUnit, mir);
3970 break;
3971 case kFmt22cs:
3972 notHandled = handleFmt22cs(cUnit, mir);
3973 break;
3974 case kFmt22t:
3975 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3976 labelList);
3977 break;
3978 case kFmt22x:
3979 case kFmt32x:
3980 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3981 break;
3982 case kFmt23x:
3983 notHandled = handleFmt23x(cUnit, mir);
3984 break;
3985 case kFmt31t:
3986 notHandled = handleFmt31t(cUnit, mir);
3987 break;
3988 case kFmt3rc:
3989 case kFmt35c:
3990 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3991 labelList);
3992 break;
3993 case kFmt3rms:
3994 case kFmt35ms:
3995 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3996 labelList);
3997 break;
3998 case kFmt3inline:
Andy McFaddenb0a05412009-11-19 10:23:41 -08003999 case kFmt3rinline:
Bill Buzbeece46c942009-11-20 15:41:34 -08004000 notHandled = handleExecuteInline(cUnit, mir);
Andy McFaddenb0a05412009-11-19 10:23:41 -08004001 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004002 case kFmt51l:
4003 notHandled = handleFmt51l(cUnit, mir);
4004 break;
4005 default:
4006 notHandled = true;
4007 break;
4008 }
4009 }
4010 if (notHandled) {
4011 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
4012 mir->offset,
4013 dalvikOpCode, getOpcodeName(dalvikOpCode),
4014 dalvikFormat);
4015 dvmAbort();
4016 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004017 }
4018 }
Ben Cheng4238ec22009-08-24 16:32:22 -07004019
Bill Buzbee1465db52009-09-23 17:17:35 -07004020 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07004021 dvmCompilerAppendLIR(cUnit,
4022 (LIR *) cUnit->loopAnalysis->branchToBody);
4023 dvmCompilerAppendLIR(cUnit,
4024 (LIR *) cUnit->loopAnalysis->branchToPCR);
4025 }
4026
4027 if (headLIR) {
4028 /*
4029 * Eliminate redundant loads/stores and delay stores into later
4030 * slots
4031 */
4032 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4033 cUnit->lastLIRInsn);
4034 }
4035
4036gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004037 /*
4038 * Check if the block is terminated due to trace length constraint -
4039 * insert an unconditional branch to the chaining cell.
4040 */
4041 if (blockList[i]->needFallThroughBranch) {
4042 genUnconditionalBranch(cUnit,
4043 &labelList[blockList[i]->fallThrough->id]);
4044 }
4045
Ben Chengba4fc8b2009-06-01 13:00:29 -07004046 }
4047
Ben Chenge9695e52009-06-16 16:11:47 -07004048 /* Handle the chaining cells in predefined order */
Ben Chengcec26f62010-01-15 15:29:33 -08004049 for (i = 0; i < kChainingCellGap; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004050 size_t j;
4051 int *blockIdList = (int *) chainingListByType[i].elemList;
4052
4053 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4054
4055 /* No chaining cells of this type */
4056 if (cUnit->numChainingCells[i] == 0)
4057 continue;
4058
4059 /* Record the first LIR for a new type of chaining cell */
4060 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4061
4062 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4063 int blockId = blockIdList[j];
4064
4065 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004066 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004067
4068 /* Insert the pseudo chaining instruction */
4069 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4070
4071
4072 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004073 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004074 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004075 blockList[blockId]->startOffset);
4076 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004077 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004078 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004079 blockList[blockId]->containingMethod);
4080 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004081 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004082 handleInvokePredictedChainingCell(cUnit);
4083 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004084 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004085 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004086 blockList[blockId]->startOffset);
4087 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004088#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004089 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004090 handleBackwardBranchChainingCell(cUnit,
4091 blockList[blockId]->startOffset);
4092 break;
4093#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004094 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004095 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004096 dvmAbort();
4097 break;
4098 }
4099 }
4100 }
Ben Chenge9695e52009-06-16 16:11:47 -07004101
Ben Chengcec26f62010-01-15 15:29:33 -08004102 /* Mark the bottom of chaining cells */
4103 cUnit->chainingCellBottom = (LIR *) newLIR0(cUnit, kArmChainingCellBottom);
4104
Ben Cheng6c10a972009-10-29 14:39:18 -07004105 /*
4106 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4107 * of all chaining cells for the overflow cases.
4108 */
4109 if (cUnit->switchOverflowPad) {
4110 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4111 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4112 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4113 opRegReg(cUnit, kOpAdd, r1, r1);
4114 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
4115#if defined(EXIT_STATS)
4116 loadConstant(cUnit, r0, kSwitchOverflow);
4117#endif
4118 opReg(cUnit, kOpBlx, r2);
4119 }
4120
Ben Chenge9695e52009-06-16 16:11:47 -07004121 dvmCompilerApplyGlobalOptimizations(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004122}
4123
4124/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004125bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004126{
Ben Chengccd6c012009-10-15 14:52:45 -07004127 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004128
Ben Cheng7a0bcd02010-01-22 16:45:45 -08004129 if (gDvmJit.codeCacheFull &&
4130 (work->kind != kWorkOrderICPatch)) {
Ben Chengccd6c012009-10-15 14:52:45 -07004131 return false;
4132 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004133
Ben Chengccd6c012009-10-15 14:52:45 -07004134 switch (work->kind) {
4135 case kWorkOrderMethod:
4136 res = dvmCompileMethod(work->info, &work->result);
4137 break;
4138 case kWorkOrderTrace:
4139 /* Start compilation with maximally allowed trace length */
4140 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4141 break;
4142 case kWorkOrderTraceDebug: {
4143 bool oldPrintMe = gDvmJit.printMe;
4144 gDvmJit.printMe = true;
4145 /* Start compilation with maximally allowed trace length */
4146 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4147 gDvmJit.printMe = oldPrintMe;;
4148 break;
4149 }
Ben Cheng60c24f42010-01-04 12:29:56 -08004150 case kWorkOrderICPatch:
4151 res = dvmJitPatchInlineCache((void *) work->pc, work->info);
4152 break;
Ben Chengccd6c012009-10-15 14:52:45 -07004153 default:
4154 res = false;
4155 dvmAbort();
4156 }
4157 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004158}
4159
Ben Chengba4fc8b2009-06-01 13:00:29 -07004160/* Architectural-specific debugging helpers go here */
4161void dvmCompilerArchDump(void)
4162{
4163 /* Print compiled opcode in this VM instance */
4164 int i, start, streak;
4165 char buf[1024];
4166
4167 streak = i = 0;
4168 buf[0] = 0;
4169 while (opcodeCoverage[i] == 0 && i < 256) {
4170 i++;
4171 }
4172 if (i == 256) {
4173 return;
4174 }
4175 for (start = i++, streak = 1; i < 256; i++) {
4176 if (opcodeCoverage[i]) {
4177 streak++;
4178 } else {
4179 if (streak == 1) {
4180 sprintf(buf+strlen(buf), "%x,", start);
4181 } else {
4182 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4183 }
4184 streak = 0;
4185 while (opcodeCoverage[i] == 0 && i < 256) {
4186 i++;
4187 }
4188 if (i < 256) {
4189 streak = 1;
4190 start = i;
4191 }
4192 }
4193 }
4194 if (streak) {
4195 if (streak == 1) {
4196 sprintf(buf+strlen(buf), "%x", start);
4197 } else {
4198 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4199 }
4200 }
4201 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004202 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004203 }
4204}
Ben Chengd7d426a2009-09-22 11:23:36 -07004205
4206/* Common initialization routine for an architecture family */
4207bool dvmCompilerArchInit()
4208{
4209 int i;
4210
Bill Buzbee1465db52009-09-23 17:17:35 -07004211 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004212 if (EncodingMap[i].opCode != i) {
4213 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4214 EncodingMap[i].name, i, EncodingMap[i].opCode);
4215 dvmAbort();
4216 }
4217 }
4218
Ben Cheng5d90c202009-11-22 23:31:11 -08004219 return dvmCompilerArchVariantInit();
4220}
4221
4222void *dvmCompilerGetInterpretTemplate()
4223{
4224 return (void*) ((int)gDvmJit.codeCache +
4225 templateEntryOffsets[TEMPLATE_INTERPRET]);
4226}
4227
4228/* Needed by the ld/st optmizatons */
4229ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4230{
4231 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4232}
4233
4234/* Needed by the register allocator */
4235ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4236{
4237 return genRegCopy(cUnit, rDest, rSrc);
4238}
4239
4240/* Needed by the register allocator */
4241void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4242 int srcLo, int srcHi)
4243{
4244 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4245}
4246
4247void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4248 int displacement, int rSrc, OpSize size)
4249{
4250 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4251}
4252
4253void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4254 int displacement, int rSrcLo, int rSrcHi)
4255{
4256 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004257}