blob: 857536f48418ebeb9d1a04ac9d44f11fd787b950 [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)
211/* Prevent certain opcodes from being jitted */
212static inline bool selfVerificationPuntOps(OpCode op)
213{
214 return (op == OP_MONITOR_ENTER || op == OP_MONITOR_EXIT ||
215 op == OP_NEW_INSTANCE || op == OP_NEW_ARRAY);
216}
217
218/*
219 * The following are used to keep compiled loads and stores from modifying
220 * memory during self verification mode.
221 *
222 * Stores do not modify memory. Instead, the address and value pair are stored
223 * into heapSpace. Addresses within heapSpace are unique. For accesses smaller
224 * than a word, the word containing the address is loaded first before being
225 * updated.
226 *
227 * Loads check heapSpace first and return data from there if an entry exists.
228 * Otherwise, data is loaded from memory as usual.
229 */
230
231/* Decode contents of heapArgSpace to determine addr to load from */
232static void selfVerificationLoadDecode(HeapArgSpace* heapArgSpace, int* addr)
233{
Bill Buzbee1465db52009-09-23 17:17:35 -0700234 int reg = heapArgSpace->regMap & 0xFF;
235 if (!FPREG(reg)) {
236 assert(reg < 16);
237 *addr = heapArgSpace->coreRegs[reg];
238 } else {
239 assert(!DOUBLEREG(reg));
240 *addr = heapArgSpace->fpRegs[(reg & FP_REG_MASK)];
Jeff Hao97319a82009-08-12 16:57:15 -0700241 }
242}
243
244/* Decode contents of heapArgSpace to determine reg to load into */
245static void selfVerificationLoadDecodeData(HeapArgSpace* heapArgSpace,
246 int data, int reg)
247{
Bill Buzbee1465db52009-09-23 17:17:35 -0700248 if (!FPREG(reg)) {
249 assert(reg < 16);
250 heapArgSpace->coreRegs[reg] = data;
251 } else {
252 assert(!DOUBLEREG(reg));
253 heapArgSpace->fpRegs[(reg & FP_REG_MASK)] = data;
Jeff Hao97319a82009-08-12 16:57:15 -0700254 }
255}
256
257static void selfVerificationLoad(InterpState* interpState)
258{
259 Thread *self = dvmThreadSelf();
260 ShadowHeap *heapSpacePtr;
261 ShadowSpace *shadowSpace = self->shadowSpace;
262 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
263
264 int addr, data;
265 selfVerificationLoadDecode(heapArgSpace, &addr);
266
267 for (heapSpacePtr = shadowSpace->heapSpace;
268 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
269 if (heapSpacePtr->addr == addr) {
270 data = heapSpacePtr->data;
271 break;
272 }
273 }
274
275 if (heapSpacePtr == shadowSpace->heapSpaceTail)
276 data = *((unsigned int*) addr);
277
Bill Buzbee1465db52009-09-23 17:17:35 -0700278 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Ben Chengd7d426a2009-09-22 11:23:36 -0700279
Bill Buzbee1465db52009-09-23 17:17:35 -0700280 // LOGD("*** HEAP LOAD: Reg:%d Addr: 0x%x Data: 0x%x", reg, addr, data);
Ben Chengd7d426a2009-09-22 11:23:36 -0700281
Jeff Hao97319a82009-08-12 16:57:15 -0700282 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
283}
284
285static void selfVerificationLoadByte(InterpState* interpState)
286{
287 Thread *self = dvmThreadSelf();
288 ShadowHeap *heapSpacePtr;
289 ShadowSpace *shadowSpace = self->shadowSpace;
290 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
291
292 int addr, data;
293 selfVerificationLoadDecode(heapArgSpace, &addr);
294
295 int maskedAddr = addr & 0xFFFFFFFC;
296 int alignment = addr & 0x3;
297
298 for (heapSpacePtr = shadowSpace->heapSpace;
299 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
300 if (heapSpacePtr->addr == maskedAddr) {
301 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
302 data = *((unsigned char*) addr);
303 break;
304 }
305 }
306
307 if (heapSpacePtr == shadowSpace->heapSpaceTail)
308 data = *((unsigned char*) addr);
309
310 //LOGD("*** HEAP LOAD BYTE: Addr: 0x%x Data: 0x%x", addr, data);
311
Bill Buzbee1465db52009-09-23 17:17:35 -0700312 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700313 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
314}
315
316static void selfVerificationLoadHalfword(InterpState* interpState)
317{
318 Thread *self = dvmThreadSelf();
319 ShadowHeap *heapSpacePtr;
320 ShadowSpace *shadowSpace = self->shadowSpace;
321 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
322
323 int addr, data;
324 selfVerificationLoadDecode(heapArgSpace, &addr);
325
326 int maskedAddr = addr & 0xFFFFFFFC;
327 int alignment = addr & 0x2;
328
329 for (heapSpacePtr = shadowSpace->heapSpace;
330 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
331 if (heapSpacePtr->addr == maskedAddr) {
332 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
333 data = *((unsigned short*) addr);
334 break;
335 }
336 }
337
338 if (heapSpacePtr == shadowSpace->heapSpaceTail)
339 data = *((unsigned short*) addr);
340
Bill Buzbee1465db52009-09-23 17:17:35 -0700341 //LOGD("*** HEAP LOAD kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700342
Bill Buzbee1465db52009-09-23 17:17:35 -0700343 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700344 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
345}
346
347static void selfVerificationLoadSignedByte(InterpState* interpState)
348{
349 Thread *self = dvmThreadSelf();
350 ShadowHeap* heapSpacePtr;
351 ShadowSpace* shadowSpace = self->shadowSpace;
352 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
353
354 int addr, data;
355 selfVerificationLoadDecode(heapArgSpace, &addr);
356
357 int maskedAddr = addr & 0xFFFFFFFC;
358 int alignment = addr & 0x3;
359
360 for (heapSpacePtr = shadowSpace->heapSpace;
361 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
362 if (heapSpacePtr->addr == maskedAddr) {
363 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
364 data = *((signed char*) addr);
365 break;
366 }
367 }
368
369 if (heapSpacePtr == shadowSpace->heapSpaceTail)
370 data = *((signed char*) addr);
371
372 //LOGD("*** HEAP LOAD SIGNED BYTE: Addr: 0x%x Data: 0x%x", addr, data);
373
Bill Buzbee1465db52009-09-23 17:17:35 -0700374 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700375 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
376}
377
378static void selfVerificationLoadSignedHalfword(InterpState* interpState)
379{
380 Thread *self = dvmThreadSelf();
381 ShadowHeap* heapSpacePtr;
382 ShadowSpace* shadowSpace = self->shadowSpace;
383 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
384
385 int addr, data;
386 selfVerificationLoadDecode(heapArgSpace, &addr);
387
388 int maskedAddr = addr & 0xFFFFFFFC;
389 int alignment = addr & 0x2;
390
391 for (heapSpacePtr = shadowSpace->heapSpace;
392 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
393 if (heapSpacePtr->addr == maskedAddr) {
394 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
395 data = *((signed short*) addr);
396 break;
397 }
398 }
399
400 if (heapSpacePtr == shadowSpace->heapSpaceTail)
401 data = *((signed short*) addr);
402
Bill Buzbee1465db52009-09-23 17:17:35 -0700403 //LOGD("*** HEAP LOAD SIGNED kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700404
Bill Buzbee1465db52009-09-23 17:17:35 -0700405 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700406 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
407}
408
409static void selfVerificationLoadDoubleword(InterpState* interpState)
410{
411 Thread *self = dvmThreadSelf();
412 ShadowHeap* heapSpacePtr;
413 ShadowSpace* shadowSpace = self->shadowSpace;
414 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
415
416 int addr;
417 selfVerificationLoadDecode(heapArgSpace, &addr);
418
419 int addr2 = addr+4;
420 unsigned int data = *((unsigned int*) addr);
421 unsigned int data2 = *((unsigned int*) addr2);
422
423 for (heapSpacePtr = shadowSpace->heapSpace;
424 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
425 if (heapSpacePtr->addr == addr) {
426 data = heapSpacePtr->data;
427 } else if (heapSpacePtr->addr == addr2) {
428 data2 = heapSpacePtr->data;
429 }
430 }
431
Bill Buzbee1465db52009-09-23 17:17:35 -0700432 // LOGD("*** HEAP LOAD DOUBLEWORD: Addr: 0x%x Data: 0x%x Data2: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700433 // addr, data, data2);
434
Bill Buzbee1465db52009-09-23 17:17:35 -0700435 int reg = (heapArgSpace->regMap >> 8) & 0xFF;
436 int reg2 = (heapArgSpace->regMap >> 16) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700437 selfVerificationLoadDecodeData(heapArgSpace, data, reg);
438 selfVerificationLoadDecodeData(heapArgSpace, data2, reg2);
439}
440
441/* Decode contents of heapArgSpace to determine arguments to store. */
442static void selfVerificationStoreDecode(HeapArgSpace* heapArgSpace,
443 int* value, int reg)
444{
Bill Buzbee1465db52009-09-23 17:17:35 -0700445 if (!FPREG(reg)) {
446 assert(reg < 16);
447 *value = heapArgSpace->coreRegs[reg];
448 } else {
449 assert(!DOUBLEREG(reg));
450 *value = heapArgSpace->fpRegs[(reg & FP_REG_MASK)];
Jeff Hao97319a82009-08-12 16:57:15 -0700451 }
452}
453
454static void selfVerificationStore(InterpState* interpState)
455{
456 Thread *self = dvmThreadSelf();
457 ShadowHeap *heapSpacePtr;
458 ShadowSpace *shadowSpace = self->shadowSpace;
459 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
460
461 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700462 int reg0 = heapArgSpace->regMap & 0xFF;
463 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700464 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
465 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
466
467 //LOGD("*** HEAP STORE: Addr: 0x%x Data: 0x%x", addr, data);
468
469 for (heapSpacePtr = shadowSpace->heapSpace;
470 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
471 if (heapSpacePtr->addr == addr) break;
472 }
473
474 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
475 heapSpacePtr->addr = addr;
476 shadowSpace->heapSpaceTail++;
477 }
478
479 heapSpacePtr->data = data;
480}
481
482static void selfVerificationStoreByte(InterpState* interpState)
483{
484 Thread *self = dvmThreadSelf();
485 ShadowHeap *heapSpacePtr;
486 ShadowSpace *shadowSpace = self->shadowSpace;
487 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
488
489 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700490 int reg0 = heapArgSpace->regMap & 0xFF;
491 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700492 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
493 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
494
495 int maskedAddr = addr & 0xFFFFFFFC;
496 int alignment = addr & 0x3;
497
498 //LOGD("*** HEAP STORE BYTE: Addr: 0x%x Data: 0x%x", addr, data);
499
500 for (heapSpacePtr = shadowSpace->heapSpace;
501 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
502 if (heapSpacePtr->addr == maskedAddr) break;
503 }
504
505 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
506 heapSpacePtr->addr = maskedAddr;
507 heapSpacePtr->data = *((unsigned int*) maskedAddr);
508 shadowSpace->heapSpaceTail++;
509 }
510
511 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
512 *((unsigned char*) addr) = (char) data;
513
514 //LOGD("*** HEAP STORE BYTE: Addr: 0x%x Final Data: 0x%x",
515 // addr, heapSpacePtr->data);
516}
517
518static void selfVerificationStoreHalfword(InterpState* interpState)
519{
520 Thread *self = dvmThreadSelf();
521 ShadowHeap *heapSpacePtr;
522 ShadowSpace *shadowSpace = self->shadowSpace;
523 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
524
525 int addr, data;
Bill Buzbee1465db52009-09-23 17:17:35 -0700526 int reg0 = heapArgSpace->regMap & 0xFF;
527 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700528 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
529 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
530
531 int maskedAddr = addr & 0xFFFFFFFC;
532 int alignment = addr & 0x2;
533
Bill Buzbee1465db52009-09-23 17:17:35 -0700534 //LOGD("*** HEAP STORE kHalfWord: Addr: 0x%x Data: 0x%x", addr, data);
Jeff Hao97319a82009-08-12 16:57:15 -0700535
536 for (heapSpacePtr = shadowSpace->heapSpace;
537 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
538 if (heapSpacePtr->addr == maskedAddr) break;
539 }
540
541 if (heapSpacePtr == shadowSpace->heapSpaceTail) {
542 heapSpacePtr->addr = maskedAddr;
543 heapSpacePtr->data = *((unsigned int*) maskedAddr);
544 shadowSpace->heapSpaceTail++;
545 }
546
547 addr = ((unsigned int) &(heapSpacePtr->data)) | alignment;
548 *((unsigned short*) addr) = (short) data;
549
Bill Buzbee1465db52009-09-23 17:17:35 -0700550 //LOGD("*** HEAP STORE kHalfWord: Addr: 0x%x Final Data: 0x%x",
Jeff Hao97319a82009-08-12 16:57:15 -0700551 // addr, heapSpacePtr->data);
552}
553
554static void selfVerificationStoreDoubleword(InterpState* interpState)
555{
556 Thread *self = dvmThreadSelf();
557 ShadowHeap *heapSpacePtr;
558 ShadowSpace *shadowSpace = self->shadowSpace;
559 HeapArgSpace *heapArgSpace = &(interpState->heapArgSpace);
560
561 int addr, data, data2;
Bill Buzbee1465db52009-09-23 17:17:35 -0700562 int reg0 = heapArgSpace->regMap & 0xFF;
563 int reg1 = (heapArgSpace->regMap >> 8) & 0xFF;
564 int reg2 = (heapArgSpace->regMap >> 16) & 0xFF;
Jeff Hao97319a82009-08-12 16:57:15 -0700565 selfVerificationStoreDecode(heapArgSpace, &addr, reg0);
566 selfVerificationStoreDecode(heapArgSpace, &data, reg1);
567 selfVerificationStoreDecode(heapArgSpace, &data2, reg2);
568
569 int addr2 = addr+4;
570 bool store1 = false, store2 = false;
571
572 //LOGD("*** HEAP STORE DOUBLEWORD: Addr: 0x%x Data: 0x%x, Data2: 0x%x",
573 // addr, data, data2);
574
575 for (heapSpacePtr = shadowSpace->heapSpace;
576 heapSpacePtr != shadowSpace->heapSpaceTail; heapSpacePtr++) {
577 if (heapSpacePtr->addr == addr) {
578 heapSpacePtr->data = data;
579 store1 = true;
580 } else if (heapSpacePtr->addr == addr2) {
581 heapSpacePtr->data = data2;
582 store2 = true;
583 }
584 }
585
586 if (!store1) {
587 shadowSpace->heapSpaceTail->addr = addr;
588 shadowSpace->heapSpaceTail->data = data;
589 shadowSpace->heapSpaceTail++;
590 }
591 if (!store2) {
592 shadowSpace->heapSpaceTail->addr = addr2;
593 shadowSpace->heapSpaceTail->data = data2;
594 shadowSpace->heapSpaceTail++;
595 }
596}
597
598/* Common wrapper function for all memory operations */
599static void selfVerificationMemOpWrapper(CompilationUnit *cUnit, int regMap,
600 void* funct)
601{
Bill Buzbee1465db52009-09-23 17:17:35 -0700602 /* push r0 and r7 to give us a foothold */
603 newLIR1(cUnit, kThumbPush, (1 << r0) | (1 << r7));
Jeff Hao97319a82009-08-12 16:57:15 -0700604
Bill Buzbee1465db52009-09-23 17:17:35 -0700605 /* Let the save handler know where the save record is */
606 loadConstant(cUnit, r0, offsetof(InterpState, heapArgSpace));
Jeff Hao97319a82009-08-12 16:57:15 -0700607
Bill Buzbee1465db52009-09-23 17:17:35 -0700608 /* Load the regMap and call the save handler [note: handler pops r0/r7] */
609 loadConstant(cUnit, r7, regMap);
610 genDispatchToHandler(cUnit, TEMPLATE_SAVE_STATE);
Jeff Hao97319a82009-08-12 16:57:15 -0700611
Bill Buzbee1465db52009-09-23 17:17:35 -0700612 /* Set function pointer, pass rGLUE and branch */
Jeff Hao97319a82009-08-12 16:57:15 -0700613 loadConstant(cUnit, r1, (int) funct);
Bill Buzbee1465db52009-09-23 17:17:35 -0700614 newLIR2(cUnit, kThumbMovRR, r0, rGLUE);
615 newLIR1(cUnit, kThumbBlxR, r1);
Jeff Hao97319a82009-08-12 16:57:15 -0700616
Bill Buzbee1465db52009-09-23 17:17:35 -0700617 /* Let the recover handler know where coreRegs[0] and restore regs */
618 loadConstant(cUnit, r0, offsetof(InterpState, heapArgSpace) +
619 offsetof(HeapArgSpace, coreRegs));
620 genDispatchToHandler(cUnit, TEMPLATE_RESTORE_STATE);
Jeff Hao97319a82009-08-12 16:57:15 -0700621}
Ben Cheng5d90c202009-11-22 23:31:11 -0800622
Jeff Hao97319a82009-08-12 16:57:15 -0700623#endif
624
Ben Chengba4fc8b2009-06-01 13:00:29 -0700625/* Generate a unconditional branch to go to the interpreter */
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700626static inline ArmLIR *genTrap(CompilationUnit *cUnit, int dOffset,
627 ArmLIR *pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700628{
Bill Buzbee1465db52009-09-23 17:17:35 -0700629 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700630 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
631}
632
633/* Load a wide field from an object instance */
634static void genIGetWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
635{
636 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700637 RegLocation rlObj = getSrcLoc(cUnit, mir, 0);
638 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
639 RegLocation rlResult;
640 rlObj = loadValue(cUnit, rlObj, kCoreReg);
641 int regPtr = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700642
Bill Buzbee1465db52009-09-23 17:17:35 -0700643 assert(rlDest.wide);
Ben Chenge9695e52009-06-16 16:11:47 -0700644
Bill Buzbee1465db52009-09-23 17:17:35 -0700645 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
646 NULL);/* null object? */
647 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
648 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Jeff Hao97319a82009-08-12 16:57:15 -0700649#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700650 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -0700651#else
Bill Buzbee1465db52009-09-23 17:17:35 -0700652 int regMap = rlResult.highReg << 16 | rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700653 selfVerificationMemOpWrapper(cUnit, regMap,
654 &selfVerificationLoadDoubleword);
Jeff Hao97319a82009-08-12 16:57:15 -0700655#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700656 freeTemp(cUnit, regPtr);
657 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700658}
659
660/* Store a wide field to an object instance */
661static void genIPutWide(CompilationUnit *cUnit, MIR *mir, int fieldOffset)
662{
663 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700664 RegLocation rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
665 RegLocation rlObj = getSrcLoc(cUnit, mir, 2);
666 rlObj = loadValue(cUnit, rlObj, kCoreReg);
667 int regPtr;
668 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
669 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
670 NULL);/* null object? */
671 regPtr = allocTemp(cUnit);
672 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700673#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700674 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -0700675#else
Bill Buzbee1465db52009-09-23 17:17:35 -0700676 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700677 selfVerificationMemOpWrapper(cUnit, regMap,
678 &selfVerificationStoreDoubleword);
679#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700680 freeTemp(cUnit, regPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700681}
682
683/*
684 * Load a field from an object instance
685 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700686 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700687static void genIGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700688 int fieldOffset)
689{
Bill Buzbee1465db52009-09-23 17:17:35 -0700690 int regPtr;
691 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700692 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700693 RegLocation rlObj = getSrcLoc(cUnit, mir, 0);
694 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
695 rlObj = loadValue(cUnit, rlObj, kCoreReg);
696 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Bill Buzbee1465db52009-09-23 17:17:35 -0700697 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
698 NULL);/* null object? */
Ben Cheng5d90c202009-11-22 23:31:11 -0800699#if !defined(WITH_SELF_VERIFICATION)
700 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
701 size, rlObj.sRegLow);
702#else
Jeff Hao97319a82009-08-12 16:57:15 -0700703 /* Combine address and offset */
Bill Buzbee1465db52009-09-23 17:17:35 -0700704 regPtr = allocTemp(cUnit);
705 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700706
Bill Buzbee1465db52009-09-23 17:17:35 -0700707 int regMap = rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700708 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationLoad);
Bill Buzbee1465db52009-09-23 17:17:35 -0700709 freeTemp(cUnit, regPtr);
Jeff Hao97319a82009-08-12 16:57:15 -0700710#endif
Bill Buzbee1465db52009-09-23 17:17:35 -0700711 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700712}
713
714/*
715 * Store a field to an object instance
716 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700717 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700718static void genIPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700719 int fieldOffset)
720{
721 DecodedInstruction *dInsn = &mir->dalvikInsn;
Bill Buzbee1465db52009-09-23 17:17:35 -0700722 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
723 RegLocation rlObj = getSrcLoc(cUnit, mir, 1);
724 rlObj = loadValue(cUnit, rlObj, kCoreReg);
725 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
726 int regPtr;
727 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
728 NULL);/* null object? */
Jeff Hao97319a82009-08-12 16:57:15 -0700729#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700730 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
Jeff Hao97319a82009-08-12 16:57:15 -0700731#else
732 /* Combine address and offset */
Bill Buzbee1465db52009-09-23 17:17:35 -0700733 regPtr = allocTemp(cUnit);
734 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
Jeff Hao97319a82009-08-12 16:57:15 -0700735
Bill Buzbee1465db52009-09-23 17:17:35 -0700736 int regMap = rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700737 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationStore);
Jeff Hao97319a82009-08-12 16:57:15 -0700738#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700739}
740
741
Ben Chengba4fc8b2009-06-01 13:00:29 -0700742/*
743 * Generate array load
Ben Chengba4fc8b2009-06-01 13:00:29 -0700744 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700745static void genArrayGet(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700746 RegLocation rlArray, RegLocation rlIndex,
747 RegLocation rlDest, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700748{
749 int lenOffset = offsetof(ArrayObject, length);
750 int dataOffset = offsetof(ArrayObject, contents);
Bill Buzbee1465db52009-09-23 17:17:35 -0700751 RegLocation rlResult;
752 rlArray = loadValue(cUnit, rlArray, kCoreReg);
753 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
754 int regPtr;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700755
756 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700757 ArmLIR * pcrLabel = NULL;
758
759 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700760 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
761 rlArray.lowReg, mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700762 }
763
Bill Buzbee1465db52009-09-23 17:17:35 -0700764 regPtr = allocTemp(cUnit);
765
Ben Cheng4238ec22009-08-24 16:32:22 -0700766 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700767 int regLen = allocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -0700768 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700769 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
770 /* regPtr -> array data */
771 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
772 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
773 pcrLabel);
774 freeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700775 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700776 /* regPtr -> array data */
777 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700778 }
Jeff Hao97319a82009-08-12 16:57:15 -0700779#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700780 if ((size == kLong) || (size == kDouble)) {
781 if (scale) {
782 int rNewIndex = allocTemp(cUnit);
783 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
784 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
785 freeTemp(cUnit, rNewIndex);
786 } else {
787 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
788 }
789 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
790 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
791 freeTemp(cUnit, regPtr);
792 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700793 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700794 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
795 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
796 scale, size);
797 freeTemp(cUnit, regPtr);
798 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700799 }
Jeff Hao97319a82009-08-12 16:57:15 -0700800#else
Bill Buzbee270c1d62009-08-13 16:58:07 -0700801 //TODO: probably want to move this into loadBaseIndexed
802 void *funct = NULL;
803 switch(size) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700804 case kLong:
805 case kDouble:
Jeff Hao97319a82009-08-12 16:57:15 -0700806 funct = (void*) &selfVerificationLoadDoubleword;
807 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700808 case kWord:
Jeff Hao97319a82009-08-12 16:57:15 -0700809 funct = (void*) &selfVerificationLoad;
810 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700811 case kUnsignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700812 funct = (void*) &selfVerificationLoadHalfword;
813 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700814 case kSignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700815 funct = (void*) &selfVerificationLoadSignedHalfword;
816 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700817 case kUnsignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700818 funct = (void*) &selfVerificationLoadByte;
819 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700820 case kSignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700821 funct = (void*) &selfVerificationLoadSignedByte;
822 break;
823 default:
824 assert(0);
825 dvmAbort();
Jeff Hao97319a82009-08-12 16:57:15 -0700826 }
Bill Buzbee270c1d62009-08-13 16:58:07 -0700827 /* Combine address and index */
Bill Buzbee1465db52009-09-23 17:17:35 -0700828 if (scale) {
829 int regTmp = allocTemp(cUnit);
830 opRegRegImm(cUnit, kOpLsl, regTmp, rlIndex.lowReg, scale);
831 opRegReg(cUnit, kOpAdd, regPtr, regTmp);
832 freeTemp(cUnit, regTmp);
833 } else {
834 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
835 }
Jeff Hao97319a82009-08-12 16:57:15 -0700836
Bill Buzbee1465db52009-09-23 17:17:35 -0700837 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
838 int regMap = rlResult.highReg << 16 | rlResult.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700839 selfVerificationMemOpWrapper(cUnit, regMap, funct);
840
Bill Buzbee1465db52009-09-23 17:17:35 -0700841 freeTemp(cUnit, regPtr);
842 if ((size == kLong) || (size == kDouble))
843 storeValueWide(cUnit, rlDest, rlResult);
Jeff Hao97319a82009-08-12 16:57:15 -0700844 else
Bill Buzbee1465db52009-09-23 17:17:35 -0700845 storeValue(cUnit, rlDest, rlResult);
Jeff Hao97319a82009-08-12 16:57:15 -0700846#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700847}
848
Ben Chengba4fc8b2009-06-01 13:00:29 -0700849/*
850 * Generate array store
851 *
Ben Chengba4fc8b2009-06-01 13:00:29 -0700852 */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700853static void genArrayPut(CompilationUnit *cUnit, MIR *mir, OpSize size,
Bill Buzbee1465db52009-09-23 17:17:35 -0700854 RegLocation rlArray, RegLocation rlIndex,
855 RegLocation rlSrc, int scale)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700856{
857 int lenOffset = offsetof(ArrayObject, length);
858 int dataOffset = offsetof(ArrayObject, contents);
859
Bill Buzbee1465db52009-09-23 17:17:35 -0700860 int regPtr;
861 rlArray = loadValue(cUnit, rlArray, kCoreReg);
862 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
Ben Chenge9695e52009-06-16 16:11:47 -0700863
Bill Buzbee1465db52009-09-23 17:17:35 -0700864 if (isTemp(cUnit, rlArray.lowReg)) {
865 clobberReg(cUnit, rlArray.lowReg);
866 regPtr = rlArray.lowReg;
867 } else {
868 regPtr = allocTemp(cUnit);
869 genRegCopy(cUnit, regPtr, rlArray.lowReg);
870 }
Ben Chenge9695e52009-06-16 16:11:47 -0700871
Ben Cheng1efc9c52009-06-08 18:25:27 -0700872 /* null object? */
Ben Cheng4238ec22009-08-24 16:32:22 -0700873 ArmLIR * pcrLabel = NULL;
874
875 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700876 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
877 mir->offset, NULL);
Ben Cheng4238ec22009-08-24 16:32:22 -0700878 }
879
880 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700881 int regLen = allocTemp(cUnit);
882 //NOTE: max live temps(4) here.
Ben Cheng4238ec22009-08-24 16:32:22 -0700883 /* Get len */
Bill Buzbee1465db52009-09-23 17:17:35 -0700884 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
885 /* regPtr -> array data */
886 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
887 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
888 pcrLabel);
889 freeTemp(cUnit, regLen);
Ben Cheng4238ec22009-08-24 16:32:22 -0700890 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700891 /* regPtr -> array data */
892 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
Ben Cheng4238ec22009-08-24 16:32:22 -0700893 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700894 /* at this point, regPtr points to array, 2 live temps */
Bill Buzbee270c1d62009-08-13 16:58:07 -0700895#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -0700896 if ((size == kLong) || (size == kDouble)) {
897 //TODO: need specific wide routine that can handle fp regs
898 if (scale) {
899 int rNewIndex = allocTemp(cUnit);
900 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
901 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
902 freeTemp(cUnit, rNewIndex);
903 } else {
904 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
905 }
906 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
907 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
908 freeTemp(cUnit, regPtr);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700909 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700910 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
911 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
912 scale, size);
Bill Buzbee270c1d62009-08-13 16:58:07 -0700913 }
914#else
915 //TODO: probably want to move this into storeBaseIndexed
916 void *funct = NULL;
917 switch(size) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700918 case kLong:
919 case kDouble:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700920 funct = (void*) &selfVerificationStoreDoubleword;
921 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700922 case kWord:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700923 funct = (void*) &selfVerificationStore;
924 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700925 case kSignedHalf:
926 case kUnsignedHalf:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700927 funct = (void*) &selfVerificationStoreHalfword;
928 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700929 case kSignedByte:
930 case kUnsignedByte:
Bill Buzbee270c1d62009-08-13 16:58:07 -0700931 funct = (void*) &selfVerificationStoreByte;
932 break;
933 default:
934 assert(0);
935 dvmAbort();
936 }
937
Bill Buzbee1465db52009-09-23 17:17:35 -0700938 if (scale) {
939 int regTmpIndex = allocTemp(cUnit);
940 // 3 live temps
941 opRegRegImm(cUnit, kOpLsl, regTmpIndex, rlIndex.lowReg, scale);
942 opRegReg(cUnit, kOpAdd, regPtr, regTmpIndex);
943 freeTemp(cUnit, regTmpIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700944 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -0700945 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700946 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700947 /* Combine address and index */
948 if ((size == kLong) || (size == kDouble)) {
949 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
950 } else {
951 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
952 }
Jeff Hao97319a82009-08-12 16:57:15 -0700953
Bill Buzbee1465db52009-09-23 17:17:35 -0700954 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | regPtr;
Jeff Hao97319a82009-08-12 16:57:15 -0700955 selfVerificationMemOpWrapper(cUnit, regMap, funct);
956
Jeff Hao97319a82009-08-12 16:57:15 -0700957#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -0700958}
959
Ben Cheng5d90c202009-11-22 23:31:11 -0800960static bool genShiftOpLong(CompilationUnit *cUnit, MIR *mir,
961 RegLocation rlDest, RegLocation rlSrc1,
962 RegLocation rlShift)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700963{
Ben Chenge9695e52009-06-16 16:11:47 -0700964 /*
965 * Don't mess with the regsiters here as there is a particular calling
966 * convention to the out-of-line handler.
967 */
Bill Buzbee1465db52009-09-23 17:17:35 -0700968 RegLocation rlResult;
969
970 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
971 loadValueDirect(cUnit, rlShift, r2);
Ben Chenge9695e52009-06-16 16:11:47 -0700972 switch( mir->dalvikInsn.opCode) {
973 case OP_SHL_LONG:
974 case OP_SHL_LONG_2ADDR:
975 genDispatchToHandler(cUnit, TEMPLATE_SHL_LONG);
976 break;
977 case OP_SHR_LONG:
978 case OP_SHR_LONG_2ADDR:
979 genDispatchToHandler(cUnit, TEMPLATE_SHR_LONG);
980 break;
981 case OP_USHR_LONG:
982 case OP_USHR_LONG_2ADDR:
983 genDispatchToHandler(cUnit, TEMPLATE_USHR_LONG);
984 break;
985 default:
986 return true;
987 }
Bill Buzbee1465db52009-09-23 17:17:35 -0700988 rlResult = getReturnLocWide(cUnit);
989 storeValueWide(cUnit, rlDest, rlResult);
Ben Chenge9695e52009-06-16 16:11:47 -0700990 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700991}
Ben Chenge9695e52009-06-16 16:11:47 -0700992
Ben Cheng5d90c202009-11-22 23:31:11 -0800993static bool genArithOpLong(CompilationUnit *cUnit, MIR *mir,
994 RegLocation rlDest, RegLocation rlSrc1,
995 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700996{
Bill Buzbee1465db52009-09-23 17:17:35 -0700997 RegLocation rlResult;
998 OpKind firstOp = kOpBkpt;
999 OpKind secondOp = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001000 bool callOut = false;
1001 void *callTgt;
1002 int retReg = r0;
1003 /* TODO - find proper .h file to declare these */
1004 long long __aeabi_ldivmod(long long op1, long long op2);
1005
1006 switch (mir->dalvikInsn.opCode) {
1007 case OP_NOT_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07001008 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1009 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1010 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1011 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1012 storeValueWide(cUnit, rlDest, rlResult);
1013 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001014 break;
1015 case OP_ADD_LONG:
1016 case OP_ADD_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001017 firstOp = kOpAdd;
1018 secondOp = kOpAdc;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001019 break;
1020 case OP_SUB_LONG:
1021 case OP_SUB_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001022 firstOp = kOpSub;
1023 secondOp = kOpSbc;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001024 break;
1025 case OP_MUL_LONG:
1026 case OP_MUL_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001027 genMulLong(cUnit, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001028 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001029 case OP_DIV_LONG:
1030 case OP_DIV_LONG_2ADDR:
1031 callOut = true;
1032 retReg = r0;
1033 callTgt = (void*)__aeabi_ldivmod;
1034 break;
1035 /* NOTE - result is in r2/r3 instead of r0/r1 */
1036 case OP_REM_LONG:
1037 case OP_REM_LONG_2ADDR:
1038 callOut = true;
1039 callTgt = (void*)__aeabi_ldivmod;
1040 retReg = r2;
1041 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001042 case OP_AND_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001043 case OP_AND_LONG:
1044 firstOp = kOpAnd;
1045 secondOp = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001046 break;
1047 case OP_OR_LONG:
1048 case OP_OR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001049 firstOp = kOpOr;
1050 secondOp = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001051 break;
1052 case OP_XOR_LONG:
1053 case OP_XOR_LONG_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001054 firstOp = kOpXor;
1055 secondOp = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001056 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001057 case OP_NEG_LONG: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001058 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1059 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1060 loadConstantValue(cUnit, rlResult.highReg, 0);
1061 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1062 rlResult.highReg, rlSrc2.lowReg);
1063 opRegReg(cUnit, kOpSbc, rlResult.highReg, rlSrc2.highReg);
1064 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001065 return false;
Ben Chenge9695e52009-06-16 16:11:47 -07001066 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001067 default:
1068 LOGE("Invalid long arith op");
1069 dvmAbort();
1070 }
1071 if (!callOut) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001072 genLong3Addr(cUnit, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001073 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001074 // Adjust return regs in to handle case of rem returning r2/r3
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001075 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001076 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1077 loadConstant(cUnit, rlr, (int) callTgt);
1078 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1079 opReg(cUnit, kOpBlx, rlr);
1080 clobberCallRegs(cUnit);
1081 if (retReg == r0)
1082 rlResult = getReturnLocWide(cUnit);
1083 else
1084 rlResult = getReturnLocWideAlt(cUnit);
1085 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001086 }
1087 return false;
1088}
1089
Ben Cheng5d90c202009-11-22 23:31:11 -08001090static bool genArithOpInt(CompilationUnit *cUnit, MIR *mir,
1091 RegLocation rlDest, RegLocation rlSrc1,
1092 RegLocation rlSrc2)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001093{
Bill Buzbee1465db52009-09-23 17:17:35 -07001094 OpKind op = kOpBkpt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001095 bool callOut = false;
1096 bool checkZero = false;
Bill Buzbee1465db52009-09-23 17:17:35 -07001097 bool unary = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001098 int retReg = r0;
1099 void *callTgt;
Bill Buzbee1465db52009-09-23 17:17:35 -07001100 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001101
1102 /* TODO - find proper .h file to declare these */
1103 int __aeabi_idivmod(int op1, int op2);
1104 int __aeabi_idiv(int op1, int op2);
1105
1106 switch (mir->dalvikInsn.opCode) {
1107 case OP_NEG_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001108 op = kOpNeg;
1109 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001110 break;
1111 case OP_NOT_INT:
Bill Buzbee1465db52009-09-23 17:17:35 -07001112 op = kOpMvn;
1113 unary = true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001114 break;
1115 case OP_ADD_INT:
1116 case OP_ADD_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001117 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001118 break;
1119 case OP_SUB_INT:
1120 case OP_SUB_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001121 op = kOpSub;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001122 break;
1123 case OP_MUL_INT:
1124 case OP_MUL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001125 op = kOpMul;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001126 break;
1127 case OP_DIV_INT:
1128 case OP_DIV_INT_2ADDR:
1129 callOut = true;
1130 checkZero = true;
1131 callTgt = __aeabi_idiv;
1132 retReg = r0;
1133 break;
1134 /* NOTE: returns in r1 */
1135 case OP_REM_INT:
1136 case OP_REM_INT_2ADDR:
1137 callOut = true;
1138 checkZero = true;
1139 callTgt = __aeabi_idivmod;
1140 retReg = r1;
1141 break;
1142 case OP_AND_INT:
1143 case OP_AND_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001144 op = kOpAnd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001145 break;
1146 case OP_OR_INT:
1147 case OP_OR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001148 op = kOpOr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001149 break;
1150 case OP_XOR_INT:
1151 case OP_XOR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001152 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001153 break;
1154 case OP_SHL_INT:
1155 case OP_SHL_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001156 op = kOpLsl;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001157 break;
1158 case OP_SHR_INT:
1159 case OP_SHR_INT_2ADDR:
Bill Buzbee1465db52009-09-23 17:17:35 -07001160 op = kOpAsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001161 break;
1162 case OP_USHR_INT:
1163 case OP_USHR_INT_2ADDR:
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);
1179 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1180 opRegRegReg(cUnit, op, rlResult.lowReg,
1181 rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chenge9695e52009-06-16 16:11:47 -07001182 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001183 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001184 } else {
Bill Buzbee1465db52009-09-23 17:17:35 -07001185 RegLocation rlResult;
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001186 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001187 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001188 loadConstant(cUnit, r2, (int) callTgt);
Bill Buzbee1465db52009-09-23 17:17:35 -07001189 loadValueDirectFixed(cUnit, rlSrc1, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001190 if (checkZero) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001191 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001192 }
Bill Buzbee1465db52009-09-23 17:17:35 -07001193 opReg(cUnit, kOpBlx, r2);
1194 clobberCallRegs(cUnit);
1195 if (retReg == r0)
1196 rlResult = getReturnLoc(cUnit);
1197 else
1198 rlResult = getReturnLocAlt(cUnit);
1199 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001200 }
1201 return false;
1202}
1203
Ben Cheng5d90c202009-11-22 23:31:11 -08001204static bool genArithOp(CompilationUnit *cUnit, MIR *mir)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001205{
1206 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07001207 RegLocation rlDest;
1208 RegLocation rlSrc1;
1209 RegLocation rlSrc2;
1210 /* Deduce sizes of operands */
1211 if (mir->ssaRep->numUses == 2) {
1212 rlSrc1 = getSrcLoc(cUnit, mir, 0);
1213 rlSrc2 = getSrcLoc(cUnit, mir, 1);
1214 } else if (mir->ssaRep->numUses == 3) {
1215 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
1216 rlSrc2 = getSrcLoc(cUnit, mir, 2);
1217 } else {
1218 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
1219 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
1220 assert(mir->ssaRep->numUses == 4);
1221 }
1222 if (mir->ssaRep->numDefs == 1) {
1223 rlDest = getDestLoc(cUnit, mir, 0);
1224 } else {
1225 assert(mir->ssaRep->numDefs == 2);
1226 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1227 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001228
1229 if ((opCode >= OP_ADD_LONG_2ADDR) && (opCode <= OP_XOR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001230 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001231 }
1232 if ((opCode >= OP_ADD_LONG) && (opCode <= OP_XOR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001233 return genArithOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001234 }
1235 if ((opCode >= OP_SHL_LONG_2ADDR) && (opCode <= OP_USHR_LONG_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001236 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001237 }
1238 if ((opCode >= OP_SHL_LONG) && (opCode <= OP_USHR_LONG)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001239 return genShiftOpLong(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001240 }
1241 if ((opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_USHR_INT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001242 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001243 }
1244 if ((opCode >= OP_ADD_INT) && (opCode <= OP_USHR_INT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001245 return genArithOpInt(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001246 }
1247 if ((opCode >= OP_ADD_FLOAT_2ADDR) && (opCode <= OP_REM_FLOAT_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001248 return genArithOpFloat(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001249 }
1250 if ((opCode >= OP_ADD_FLOAT) && (opCode <= OP_REM_FLOAT)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001251 return genArithOpFloat(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001252 }
1253 if ((opCode >= OP_ADD_DOUBLE_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001254 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001255 }
1256 if ((opCode >= OP_ADD_DOUBLE) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08001257 return genArithOpDouble(cUnit,mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001258 }
1259 return true;
1260}
1261
Bill Buzbee1465db52009-09-23 17:17:35 -07001262/* Generate conditional branch instructions */
1263static ArmLIR *genConditionalBranch(CompilationUnit *cUnit,
1264 ArmConditionCode cond,
1265 ArmLIR *target)
1266{
1267 ArmLIR *branch = opCondBranch(cUnit, cond);
1268 branch->generic.target = (LIR *) target;
1269 return branch;
1270}
1271
1272/* Generate unconditional branch instructions */
1273static ArmLIR *genUnconditionalBranch(CompilationUnit *cUnit, ArmLIR *target)
1274{
1275 ArmLIR *branch = opNone(cUnit, kOpUncondBr);
1276 branch->generic.target = (LIR *) target;
1277 return branch;
1278}
1279
Bill Buzbee1465db52009-09-23 17:17:35 -07001280/* Perform the actual operation for OP_RETURN_* */
1281static void genReturnCommon(CompilationUnit *cUnit, MIR *mir)
1282{
1283 genDispatchToHandler(cUnit, TEMPLATE_RETURN);
1284#if defined(INVOKE_STATS)
1285 gDvmJit.returnOp++;
1286#endif
1287 int dPC = (int) (cUnit->method->insns + mir->offset);
1288 /* Insert branch, but defer setting of target */
1289 ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
1290 /* Set up the place holder to reconstruct this Dalvik PC */
1291 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
1292 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
1293 pcrLabel->operands[0] = dPC;
1294 pcrLabel->operands[1] = mir->offset;
1295 /* Insert the place holder to the growable list */
1296 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1297 /* Branch to the PC reconstruction code */
1298 branch->generic.target = (LIR *) pcrLabel;
1299}
1300
Ben Chengba4fc8b2009-06-01 13:00:29 -07001301static void genProcessArgsNoRange(CompilationUnit *cUnit, MIR *mir,
1302 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001303 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001304{
1305 unsigned int i;
1306 unsigned int regMask = 0;
Bill Buzbee1465db52009-09-23 17:17:35 -07001307 RegLocation rlArg;
1308 int numDone = 0;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001309
Bill Buzbee1465db52009-09-23 17:17:35 -07001310 /*
1311 * Load arguments to r0..r4. Note that these registers may contain
1312 * live values, so we clobber them immediately after loading to prevent
1313 * them from being used as sources for subsequent loads.
1314 */
1315 lockAllTemps(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001316 for (i = 0; i < dInsn->vA; i++) {
1317 regMask |= 1 << i;
Bill Buzbee1465db52009-09-23 17:17:35 -07001318 rlArg = getSrcLoc(cUnit, mir, numDone++);
1319 loadValueDirectFixed(cUnit, rlArg, i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001320 }
1321 if (regMask) {
1322 /* Up to 5 args are pushed on top of FP - sizeofStackSaveArea */
Bill Buzbee1465db52009-09-23 17:17:35 -07001323 opRegRegImm(cUnit, kOpSub, r7, rFP,
1324 sizeof(StackSaveArea) + (dInsn->vA << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001325 /* generate null check */
1326 if (pcrLabel) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001327 *pcrLabel = genNullCheck(cUnit, getSrcSSAName(mir, 0), r0,
1328 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001329 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001330 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001331 }
1332}
1333
1334static void genProcessArgsRange(CompilationUnit *cUnit, MIR *mir,
1335 DecodedInstruction *dInsn,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001336 ArmLIR **pcrLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001337{
1338 int srcOffset = dInsn->vC << 2;
1339 int numArgs = dInsn->vA;
1340 int regMask;
Bill Buzbee1465db52009-09-23 17:17:35 -07001341
1342 /*
1343 * Note: here, all promoted registers will have been flushed
1344 * back to the Dalvik base locations, so register usage restrictins
1345 * are lifted. All parms loaded from original Dalvik register
1346 * region - even though some might conceivably have valid copies
1347 * cached in a preserved register.
1348 */
1349 lockAllTemps(cUnit);
1350
Ben Chengba4fc8b2009-06-01 13:00:29 -07001351 /*
1352 * r4PC : &rFP[vC]
1353 * r7: &newFP[0]
1354 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001355 opRegRegImm(cUnit, kOpAdd, r4PC, rFP, srcOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001356 /* load [r0 .. min(numArgs,4)] */
1357 regMask = (1 << ((numArgs < 4) ? numArgs : 4)) - 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001358 /*
1359 * Protect the loadMultiple instruction from being reordered with other
1360 * Dalvik stack accesses.
1361 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001362 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001363
Bill Buzbee1465db52009-09-23 17:17:35 -07001364 opRegRegImm(cUnit, kOpSub, r7, rFP,
1365 sizeof(StackSaveArea) + (numArgs << 2));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001366 /* generate null check */
1367 if (pcrLabel) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001368 *pcrLabel = genNullCheck(cUnit, getSrcSSAName(mir, 0), r0,
1369 mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001370 }
1371
1372 /*
1373 * Handle remaining 4n arguments:
1374 * store previously loaded 4 values and load the next 4 values
1375 */
1376 if (numArgs >= 8) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001377 ArmLIR *loopLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001378 /*
1379 * r0 contains "this" and it will be used later, so push it to the stack
Bill Buzbee270c1d62009-08-13 16:58:07 -07001380 * first. Pushing r5 (rFP) is just for stack alignment purposes.
Ben Chengba4fc8b2009-06-01 13:00:29 -07001381 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001382 opImm(cUnit, kOpPush, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001383 /* No need to generate the loop structure if numArgs <= 11 */
1384 if (numArgs > 11) {
1385 loadConstant(cUnit, 5, ((numArgs - 4) >> 2) << 2);
Bill Buzbee1465db52009-09-23 17:17:35 -07001386 loopLabel = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07001387 loopLabel->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001388 }
Bill Buzbee270c1d62009-08-13 16:58:07 -07001389 storeMultiple(cUnit, r7, regMask);
Ben Chengd7d426a2009-09-22 11:23:36 -07001390 /*
1391 * Protect the loadMultiple instruction from being reordered with other
1392 * Dalvik stack accesses.
1393 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001394 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001395 /* No need to generate the loop structure if numArgs <= 11 */
1396 if (numArgs > 11) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001397 opRegImm(cUnit, kOpSub, rFP, 4);
1398 genConditionalBranch(cUnit, kArmCondNe, loopLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001399 }
1400 }
1401
1402 /* Save the last batch of loaded values */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001403 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001404
1405 /* Generate the loop epilogue - don't use r0 */
1406 if ((numArgs > 4) && (numArgs % 4)) {
1407 regMask = ((1 << (numArgs & 0x3)) - 1) << 1;
Ben Chengd7d426a2009-09-22 11:23:36 -07001408 /*
1409 * Protect the loadMultiple instruction from being reordered with other
1410 * Dalvik stack accesses.
1411 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001412 loadMultiple(cUnit, r4PC, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001413 }
1414 if (numArgs >= 8)
Bill Buzbee1465db52009-09-23 17:17:35 -07001415 opImm(cUnit, kOpPop, (1 << r0 | 1 << rFP));
Ben Chengba4fc8b2009-06-01 13:00:29 -07001416
1417 /* Save the modulo 4 arguments */
1418 if ((numArgs > 4) && (numArgs % 4)) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07001419 storeMultiple(cUnit, r7, regMask);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001420 }
1421}
1422
Ben Cheng38329f52009-07-07 14:19:20 -07001423/*
1424 * Generate code to setup the call stack then jump to the chaining cell if it
1425 * is not a native method.
1426 */
1427static void genInvokeSingletonCommon(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001428 BasicBlock *bb, ArmLIR *labelList,
1429 ArmLIR *pcrLabel,
Ben Cheng38329f52009-07-07 14:19:20 -07001430 const Method *calleeMethod)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001431{
Bill Buzbee1465db52009-09-23 17:17:35 -07001432 /*
1433 * Note: all Dalvik register state should be flushed to
1434 * memory by the point, so register usage restrictions no
1435 * longer apply. All temp & preserved registers may be used.
1436 */
1437 lockAllTemps(cUnit);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001438 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07001439
1440 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001441 lockTemp(cUnit, r1);
1442 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001443 /* r4PC = dalvikCallsite */
1444 loadConstant(cUnit, r4PC,
1445 (int) (cUnit->method->insns + mir->offset));
1446 addrRetChain->generic.target = (LIR *) retChainingCell;
1447 /*
Ben Cheng38329f52009-07-07 14:19:20 -07001448 * r0 = calleeMethod (loaded upon calling genInvokeSingletonCommon)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001449 * r1 = &ChainingCell
1450 * r4PC = callsiteDPC
1451 */
1452 if (dvmIsNativeMethod(calleeMethod)) {
Ben Cheng38329f52009-07-07 14:19:20 -07001453 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NATIVE);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001454#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07001455 gDvmJit.invokeNative++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07001456#endif
1457 } else {
1458 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_CHAIN);
1459#if defined(INVOKE_STATS)
1460 gDvmJit.invokeChain++;
1461#endif
Ben Cheng38329f52009-07-07 14:19:20 -07001462 /* Branch to the chaining cell */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001463 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1464 }
1465 /* Handle exceptions using the interpreter */
1466 genTrap(cUnit, mir->offset, pcrLabel);
1467}
1468
Ben Cheng38329f52009-07-07 14:19:20 -07001469/*
1470 * Generate code to check the validity of a predicted chain and take actions
1471 * based on the result.
1472 *
1473 * 0x426a99aa : ldr r4, [pc, #72] --> r4 <- dalvikPC of this invoke
1474 * 0x426a99ac : add r1, pc, #32 --> r1 <- &retChainingCell
1475 * 0x426a99ae : add r2, pc, #40 --> r2 <- &predictedChainingCell
1476 * 0x426a99b0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN
1477 * 0x426a99b2 : blx_2 see above --+
1478 * 0x426a99b4 : b 0x426a99d8 --> off to the predicted chain
1479 * 0x426a99b6 : b 0x426a99c8 --> punt to the interpreter
1480 * 0x426a99b8 : ldr r0, [r7, #44] --> r0 <- this->class->vtable[methodIdx]
1481 * 0x426a99ba : cmp r1, #0 --> compare r1 (rechain count) against 0
1482 * 0x426a99bc : bgt 0x426a99c2 --> >=0? don't rechain
1483 * 0x426a99be : ldr r7, [r6, #96] --+ dvmJitToPatchPredictedChain
1484 * 0x426a99c0 : blx r7 --+
1485 * 0x426a99c2 : add r1, pc, #12 --> r1 <- &retChainingCell
1486 * 0x426a99c4 : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
1487 * 0x426a99c6 : blx_2 see above --+
1488 */
1489static void genInvokeVirtualCommon(CompilationUnit *cUnit, MIR *mir,
1490 int methodIndex,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001491 ArmLIR *retChainingCell,
1492 ArmLIR *predChainingCell,
1493 ArmLIR *pcrLabel)
Ben Cheng38329f52009-07-07 14:19:20 -07001494{
Bill Buzbee1465db52009-09-23 17:17:35 -07001495 /*
1496 * Note: all Dalvik register state should be flushed to
1497 * memory by the point, so register usage restrictions no
1498 * longer apply. Lock temps to prevent them from being
1499 * allocated by utility routines.
1500 */
1501 lockAllTemps(cUnit);
1502
Ben Cheng38329f52009-07-07 14:19:20 -07001503 /* "this" is already left in r0 by genProcessArgs* */
1504
1505 /* r4PC = dalvikCallsite */
1506 loadConstant(cUnit, r4PC,
1507 (int) (cUnit->method->insns + mir->offset));
1508
1509 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001510 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001511 addrRetChain->generic.target = (LIR *) retChainingCell;
1512
1513 /* r2 = &predictedChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001514 ArmLIR *predictedChainingCell = opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001515 predictedChainingCell->generic.target = (LIR *) predChainingCell;
1516
1517 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
1518
1519 /* return through lr - jump to the chaining cell */
1520 genUnconditionalBranch(cUnit, predChainingCell);
1521
1522 /*
1523 * null-check on "this" may have been eliminated, but we still need a PC-
1524 * reconstruction label for stack overflow bailout.
1525 */
1526 if (pcrLabel == NULL) {
1527 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001528 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07001529 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07001530 pcrLabel->operands[0] = dPC;
1531 pcrLabel->operands[1] = mir->offset;
1532 /* Insert the place holder to the growable list */
1533 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
1534 }
1535
1536 /* return through lr+2 - punt to the interpreter */
1537 genUnconditionalBranch(cUnit, pcrLabel);
1538
1539 /*
1540 * return through lr+4 - fully resolve the callee method.
1541 * r1 <- count
1542 * r2 <- &predictedChainCell
1543 * r3 <- this->class
1544 * r4 <- dPC
1545 * r7 <- this->class->vtable
1546 */
1547
1548 /* r0 <- calleeMethod */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001549 loadWordDisp(cUnit, r7, methodIndex * 4, r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001550
1551 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07001552 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001553
Bill Buzbee1465db52009-09-23 17:17:35 -07001554 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07001555
Bill Buzbee270c1d62009-08-13 16:58:07 -07001556 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1557 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001558
1559 /*
1560 * r0 = calleeMethod
1561 * r2 = &predictedChainingCell
1562 * r3 = class
1563 *
1564 * &returnChainingCell has been loaded into r1 but is not needed
1565 * when patching the chaining cell and will be clobbered upon
1566 * returning so it will be reconstructed again.
1567 */
Bill Buzbee1465db52009-09-23 17:17:35 -07001568 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001569
1570 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001571 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001572 addrRetChain->generic.target = (LIR *) retChainingCell;
1573
1574 bypassRechaining->generic.target = (LIR *) addrRetChain;
1575 /*
1576 * r0 = calleeMethod,
1577 * r1 = &ChainingCell,
1578 * r4PC = callsiteDPC,
1579 */
1580 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
1581#if defined(INVOKE_STATS)
1582 gDvmJit.invokePredictedChain++;
1583#endif
1584 /* Handle exceptions using the interpreter */
1585 genTrap(cUnit, mir->offset, pcrLabel);
1586}
1587
1588/*
1589 * Up calling this function, "this" is stored in r0. The actual class will be
1590 * chased down off r0 and the predicted one will be retrieved through
1591 * predictedChainingCell then a comparison is performed to see whether the
1592 * previously established chaining is still valid.
1593 *
1594 * The return LIR is a branch based on the comparison result. The actual branch
1595 * target will be setup in the caller.
1596 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001597static ArmLIR *genCheckPredictedChain(CompilationUnit *cUnit,
1598 ArmLIR *predChainingCell,
1599 ArmLIR *retChainingCell,
Ben Cheng38329f52009-07-07 14:19:20 -07001600 MIR *mir)
1601{
Bill Buzbee1465db52009-09-23 17:17:35 -07001602 /*
1603 * Note: all Dalvik register state should be flushed to
1604 * memory by the point, so register usage restrictions no
1605 * longer apply. All temp & preserved registers may be used.
1606 */
1607 lockAllTemps(cUnit);
1608
Ben Cheng38329f52009-07-07 14:19:20 -07001609 /* r3 now contains this->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07001610 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001611
1612 /*
1613 * r2 now contains predicted class. The starting offset of the
1614 * cached value is 4 bytes into the chaining cell.
1615 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001616 ArmLIR *getPredictedClass =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001617 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, clazz), r2);
Ben Cheng38329f52009-07-07 14:19:20 -07001618 getPredictedClass->generic.target = (LIR *) predChainingCell;
1619
1620 /*
1621 * r0 now contains predicted method. The starting offset of the
1622 * cached value is 8 bytes into the chaining cell.
1623 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001624 ArmLIR *getPredictedMethod =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001625 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, method), r0);
Ben Cheng38329f52009-07-07 14:19:20 -07001626 getPredictedMethod->generic.target = (LIR *) predChainingCell;
1627
1628 /* Load the stats counter to see if it is time to unchain and refresh */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001629 ArmLIR *getRechainingRequestCount =
Bill Buzbee270c1d62009-08-13 16:58:07 -07001630 loadWordDisp(cUnit, rpc, offsetof(PredictedChainingCell, counter), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07001631 getRechainingRequestCount->generic.target =
1632 (LIR *) predChainingCell;
1633
1634 /* r4PC = dalvikCallsite */
1635 loadConstant(cUnit, r4PC,
1636 (int) (cUnit->method->insns + mir->offset));
1637
1638 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07001639 ArmLIR *addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07001640 addrRetChain->generic.target = (LIR *) retChainingCell;
1641
1642 /* Check if r2 (predicted class) == r3 (actual class) */
Bill Buzbee1465db52009-09-23 17:17:35 -07001643 opRegReg(cUnit, kOpCmp, r2, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07001644
Bill Buzbee1465db52009-09-23 17:17:35 -07001645 return opCondBranch(cUnit, kArmCondEq);
Ben Cheng38329f52009-07-07 14:19:20 -07001646}
1647
Ben Chengba4fc8b2009-06-01 13:00:29 -07001648/* Geneate a branch to go back to the interpreter */
1649static void genPuntToInterp(CompilationUnit *cUnit, unsigned int offset)
1650{
1651 /* r0 = dalvik pc */
Bill Buzbee1465db52009-09-23 17:17:35 -07001652 flushAllRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001653 loadConstant(cUnit, r0, (int) (cUnit->method->insns + offset));
Bill Buzbee270c1d62009-08-13 16:58:07 -07001654 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r3);
1655 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
1656 jitToInterpEntries.dvmJitToInterpPunt), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07001657 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001658}
1659
1660/*
1661 * Attempt to single step one instruction using the interpreter and return
1662 * to the compiled code for the next Dalvik instruction
1663 */
1664static void genInterpSingleStep(CompilationUnit *cUnit, MIR *mir)
1665{
1666 int flags = dexGetInstrFlags(gDvm.instrFlags, mir->dalvikInsn.opCode);
1667 int flagsToCheck = kInstrCanBranch | kInstrCanSwitch | kInstrCanReturn |
1668 kInstrCanThrow;
Bill Buzbee1465db52009-09-23 17:17:35 -07001669
1670 //Ugly, but necessary. Flush all Dalvik regs so Interp can find them
1671 flushAllRegs(cUnit);
1672
Ben Chengba4fc8b2009-06-01 13:00:29 -07001673 if ((mir->next == NULL) || (flags & flagsToCheck)) {
1674 genPuntToInterp(cUnit, mir->offset);
1675 return;
1676 }
1677 int entryAddr = offsetof(InterpState,
1678 jitToInterpEntries.dvmJitToInterpSingleStep);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001679 loadWordDisp(cUnit, rGLUE, entryAddr, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001680 /* r0 = dalvik pc */
1681 loadConstant(cUnit, r0, (int) (cUnit->method->insns + mir->offset));
1682 /* r1 = dalvik pc of following instruction */
1683 loadConstant(cUnit, r1, (int) (cUnit->method->insns + mir->next->offset));
Bill Buzbee1465db52009-09-23 17:17:35 -07001684 opReg(cUnit, kOpBlx, r2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001685}
1686
Ben Cheng5d90c202009-11-22 23:31:11 -08001687static void genMonitorPortable(CompilationUnit *cUnit, MIR *mir)
Bill Buzbee270c1d62009-08-13 16:58:07 -07001688{
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001689 bool isEnter = (mir->dalvikInsn.opCode == OP_MONITOR_ENTER);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001690 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001691 genExportPC(cUnit, mir);
1692 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
1693 loadValueDirectFixed(cUnit, rlSrc, r1);
1694 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001695 if (isEnter) {
Bill Buzbee1465db52009-09-23 17:17:35 -07001696 loadConstant(cUnit, r2, (int)dvmLockObject);
1697 } else {
1698 loadConstant(cUnit, r2, (int)dvmUnlockObject);
1699 }
1700 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
1701 /* Do the call */
1702 opReg(cUnit, kOpBlx, r2);
Bill Buzbeeefbd3c52009-11-04 22:18:40 -08001703#if defined(WITH_DEADLOCK_PREDICTION)
1704 if (isEnter) {
1705 loadWordDisp(cUnit, rGLUE, offsetof(InterpState, self), r0);
1706 loadWordDisp(cUnit, r0, offsetof(Thread, exception), r1);
1707 opRegImm(cUnit, kOpCmp, r1, 0);
1708 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondEq);
1709 loadConstant(cUnit, r0,
1710 (int) (cUnit->method->insns + mir->offset));
1711 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1712 /* noreturn */
1713 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
1714 target->defMask = ENCODE_ALL;
1715 branchOver->generic.target = (LIR *) target;
1716 }
1717#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001718 clobberCallRegs(cUnit);
Bill Buzbee270c1d62009-08-13 16:58:07 -07001719}
1720
Ben Chengba4fc8b2009-06-01 13:00:29 -07001721/*
1722 * The following are the first-level codegen routines that analyze the format
1723 * of each bytecode then either dispatch special purpose codegen routines
1724 * or produce corresponding Thumb instructions directly.
1725 */
1726
1727static bool handleFmt10t_Fmt20t_Fmt30t(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07001728 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07001729{
1730 /* For OP_GOTO, OP_GOTO_16, and OP_GOTO_32 */
1731 genUnconditionalBranch(cUnit, &labelList[bb->taken->id]);
1732 return false;
1733}
1734
1735static bool handleFmt10x(CompilationUnit *cUnit, MIR *mir)
1736{
1737 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
1738 if (((dalvikOpCode >= OP_UNUSED_3E) && (dalvikOpCode <= OP_UNUSED_43)) ||
Andy McFadden96516932009-10-28 17:39:02 -07001739 ((dalvikOpCode >= OP_UNUSED_E3) && (dalvikOpCode <= OP_UNUSED_EB))) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001740 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1741 return true;
1742 }
1743 switch (dalvikOpCode) {
1744 case OP_RETURN_VOID:
1745 genReturnCommon(cUnit,mir);
1746 break;
1747 case OP_UNUSED_73:
1748 case OP_UNUSED_79:
1749 case OP_UNUSED_7A:
1750 LOGE("Codegen: got unused opcode 0x%x\n",dalvikOpCode);
1751 return true;
1752 case OP_NOP:
1753 break;
1754 default:
1755 return true;
1756 }
1757 return false;
1758}
1759
1760static bool handleFmt11n_Fmt31i(CompilationUnit *cUnit, MIR *mir)
1761{
Bill Buzbee1465db52009-09-23 17:17:35 -07001762 RegLocation rlDest;
1763 RegLocation rlResult;
1764 if (mir->ssaRep->numDefs == 2) {
1765 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1766 } else {
1767 rlDest = getDestLoc(cUnit, mir, 0);
1768 }
Ben Chenge9695e52009-06-16 16:11:47 -07001769
Ben Chengba4fc8b2009-06-01 13:00:29 -07001770 switch (mir->dalvikInsn.opCode) {
1771 case OP_CONST:
Ben Chenge9695e52009-06-16 16:11:47 -07001772 case OP_CONST_4: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001773 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1774 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1775 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001776 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001777 }
1778 case OP_CONST_WIDE_32: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001779 //TUNING: single routine to load constant pair for support doubles
1780 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1781 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB);
1782 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
1783 rlResult.lowReg, 31);
1784 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001785 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001786 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001787 default:
1788 return true;
1789 }
1790 return false;
1791}
1792
1793static bool handleFmt21h(CompilationUnit *cUnit, MIR *mir)
1794{
Bill Buzbee1465db52009-09-23 17:17:35 -07001795 RegLocation rlDest;
1796 RegLocation rlResult;
1797 if (mir->ssaRep->numDefs == 2) {
1798 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1799 } else {
1800 rlDest = getDestLoc(cUnit, mir, 0);
1801 }
1802 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
Ben Chenge9695e52009-06-16 16:11:47 -07001803
Ben Chengba4fc8b2009-06-01 13:00:29 -07001804 switch (mir->dalvikInsn.opCode) {
Ben Chenge9695e52009-06-16 16:11:47 -07001805 case OP_CONST_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001806 loadConstantValue(cUnit, rlResult.lowReg, mir->dalvikInsn.vB << 16);
1807 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001808 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001809 }
1810 case OP_CONST_WIDE_HIGH16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001811 loadConstantValueWide(cUnit, rlResult.lowReg, rlResult.highReg,
1812 0, mir->dalvikInsn.vB << 16);
1813 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001814 break;
Ben Chenge9695e52009-06-16 16:11:47 -07001815 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001816 default:
1817 return true;
1818 }
1819 return false;
1820}
1821
1822static bool handleFmt20bc(CompilationUnit *cUnit, MIR *mir)
1823{
1824 /* For OP_THROW_VERIFICATION_ERROR */
1825 genInterpSingleStep(cUnit, mir);
1826 return false;
1827}
1828
1829static bool handleFmt21c_Fmt31c(CompilationUnit *cUnit, MIR *mir)
1830{
Bill Buzbee1465db52009-09-23 17:17:35 -07001831 RegLocation rlResult;
1832 RegLocation rlDest;
1833 RegLocation rlSrc;
Ben Chenge9695e52009-06-16 16:11:47 -07001834
Ben Chengba4fc8b2009-06-01 13:00:29 -07001835 switch (mir->dalvikInsn.opCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07001836 case OP_CONST_STRING_JUMBO:
1837 case OP_CONST_STRING: {
1838 void *strPtr = (void*)
1839 (cUnit->method->clazz->pDvmDex->pResStrings[mir->dalvikInsn.vB]);
1840 assert(strPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001841 rlDest = getDestLoc(cUnit, mir, 0);
1842 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1843 loadConstantValue(cUnit, rlResult.lowReg, (int) strPtr );
1844 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001845 break;
1846 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07001847 case OP_CONST_CLASS: {
1848 void *classPtr = (void*)
1849 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1850 assert(classPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001851 rlDest = getDestLoc(cUnit, mir, 0);
1852 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
1853 loadConstantValue(cUnit, rlResult.lowReg, (int) classPtr );
1854 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001855 break;
1856 }
1857 case OP_SGET_OBJECT:
1858 case OP_SGET_BOOLEAN:
1859 case OP_SGET_CHAR:
1860 case OP_SGET_BYTE:
1861 case OP_SGET_SHORT:
1862 case OP_SGET: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001863 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001864 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001865 void *fieldPtr = (void*)
1866 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
1867 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001868 rlDest = getDestLoc(cUnit, mir, 0);
1869 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1870 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001871#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001872 loadWordDisp(cUnit, tReg, 0, rlResult.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001873#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001874 int regMap = rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001875 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationLoad);
1876
Jeff Hao97319a82009-08-12 16:57:15 -07001877#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001878 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001879 break;
1880 }
1881 case OP_SGET_WIDE: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001882 int valOffset = offsetof(StaticField, value);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001883 void *fieldPtr = (void*)
1884 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Bill Buzbee1465db52009-09-23 17:17:35 -07001885 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001886 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001887 rlDest = getDestLocWide(cUnit, mir, 0, 1);
1888 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
1889 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001890#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001891 loadPair(cUnit, tReg, rlResult.lowReg, rlResult.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001892#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001893 int regMap = rlResult.highReg << 16 |
1894 rlResult.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001895 selfVerificationMemOpWrapper(cUnit, regMap,
1896 &selfVerificationLoadDoubleword);
1897
Jeff Hao97319a82009-08-12 16:57:15 -07001898#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07001899 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001900 break;
1901 }
1902 case OP_SPUT_OBJECT:
1903 case OP_SPUT_BOOLEAN:
1904 case OP_SPUT_CHAR:
1905 case OP_SPUT_BYTE:
1906 case OP_SPUT_SHORT:
1907 case OP_SPUT: {
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001908 int valOffset = offsetof(StaticField, value);
Bill Buzbee1465db52009-09-23 17:17:35 -07001909 int tReg = allocTemp(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001910 void *fieldPtr = (void*)
1911 (cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vB]);
Ben Chenge9695e52009-06-16 16:11:47 -07001912
Ben Chengba4fc8b2009-06-01 13:00:29 -07001913 assert(fieldPtr != NULL);
Bill Buzbee1465db52009-09-23 17:17:35 -07001914 rlSrc = getSrcLoc(cUnit, mir, 0);
1915 rlSrc = loadValue(cUnit, rlSrc, kAnyReg);
1916 loadConstant(cUnit, tReg, (int) fieldPtr + valOffset);
Jeff Hao97319a82009-08-12 16:57:15 -07001917#if !defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07001918 storeWordDisp(cUnit, tReg, 0 ,rlSrc.lowReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001919#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001920 int regMap = rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001921 selfVerificationMemOpWrapper(cUnit, regMap, &selfVerificationStore);
1922#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001923 break;
1924 }
1925 case OP_SPUT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07001926 int tReg = allocTemp(cUnit);
Bill Buzbee50a6bf22009-07-08 13:08:04 -07001927 int valOffset = offsetof(StaticField, value);
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 = getSrcLocWide(cUnit, mir, 0, 1);
1933 rlSrc = loadValueWide(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 storePair(cUnit, tReg, rlSrc.lowReg, rlSrc.highReg);
Jeff Hao97319a82009-08-12 16:57:15 -07001937#else
Bill Buzbee1465db52009-09-23 17:17:35 -07001938 int regMap = rlSrc.highReg << 16 | rlSrc.lowReg << 8 | tReg;
Jeff Hao97319a82009-08-12 16:57:15 -07001939 selfVerificationMemOpWrapper(cUnit, regMap,
1940 &selfVerificationStoreDoubleword);
1941#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07001942 break;
1943 }
1944 case OP_NEW_INSTANCE: {
Ben Chenge9695e52009-06-16 16:11:47 -07001945 /*
1946 * Obey the calling convention and don't mess with the register
1947 * usage.
1948 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001949 ClassObject *classPtr = (void*)
1950 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
1951 assert(classPtr != NULL);
1952 assert(classPtr->status & CLASS_INITIALIZED);
Ben Cheng79d173c2009-09-29 16:12:51 -07001953 /*
1954 * If it is going to throw, it should not make to the trace to begin
Bill Buzbee1465db52009-09-23 17:17:35 -07001955 * with. However, Alloc might throw, so we need to genExportPC()
Ben Cheng79d173c2009-09-29 16:12:51 -07001956 */
1957 assert((classPtr->accessFlags & (ACC_INTERFACE|ACC_ABSTRACT)) == 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08001958 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07001959 genExportPC(cUnit, mir);
1960 loadConstant(cUnit, r2, (int)dvmAllocObject);
Ben Chenge9695e52009-06-16 16:11:47 -07001961 loadConstant(cUnit, r0, (int) classPtr);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001962 loadConstant(cUnit, r1, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07001963 opReg(cUnit, kOpBlx, r2);
1964 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07001965 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07001966 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
1967 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07001968 /*
1969 * OOM exception needs to be thrown here and cannot re-execute
1970 */
1971 loadConstant(cUnit, r0,
1972 (int) (cUnit->method->insns + mir->offset));
1973 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
1974 /* noreturn */
1975
Bill Buzbee1465db52009-09-23 17:17:35 -07001976 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07001977 target->defMask = ENCODE_ALL;
1978 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07001979 rlDest = getDestLoc(cUnit, mir, 0);
1980 rlResult = getReturnLoc(cUnit);
1981 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07001982 break;
1983 }
1984 case OP_CHECK_CAST: {
Ben Chenge9695e52009-06-16 16:11:47 -07001985 /*
1986 * Obey the calling convention and don't mess with the register
1987 * usage.
1988 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07001989 ClassObject *classPtr =
1990 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vB]);
Bill Buzbee4df41a52009-11-12 17:07:16 -08001991 /*
1992 * Note: It is possible that classPtr is NULL at this point,
1993 * even though this instruction has been successfully interpreted.
1994 * If the previous interpretation had a null source, the
1995 * interpreter would not have bothered to resolve the clazz.
1996 * Bail out to the interpreter in this case, and log it
1997 * so that we can tell if it happens frequently.
1998 */
1999 if (classPtr == NULL) {
2000 LOGD("null clazz in OP_CHECK_CAST, single-stepping");
2001 genInterpSingleStep(cUnit, mir);
2002 return false;
2003 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002004 flushAllRegs(cUnit); /* Send everything to home location */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002005 loadConstant(cUnit, r1, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002006 rlSrc = getSrcLoc(cUnit, mir, 0);
2007 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2008 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0); /* Null? */
2009 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
2010 /*
2011 * rlSrc.lowReg now contains object->clazz. Note that
2012 * it could have been allocated r0, but we're okay so long
2013 * as we don't do anything desctructive until r0 is loaded
2014 * with clazz.
2015 */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002016 /* r0 now contains object->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07002017 loadWordDisp(cUnit, rlSrc.lowReg, offsetof(Object, clazz), r0);
2018 loadConstant(cUnit, r2, (int)dvmInstanceofNonTrivial);
2019 opRegReg(cUnit, kOpCmp, r0, r1);
2020 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2021 opReg(cUnit, kOpBlx, r2);
2022 clobberCallRegs(cUnit);
2023 /*
2024 * If null, check cast failed - punt to the interpreter. Because
2025 * interpreter will be the one throwing, we don't need to
2026 * genExportPC() here.
2027 */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002028 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002029 /* check cast passed - branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002030 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002031 target->defMask = ENCODE_ALL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002032 branch1->generic.target = (LIR *)target;
2033 branch2->generic.target = (LIR *)target;
2034 break;
2035 }
2036 default:
2037 return true;
2038 }
2039 return false;
2040}
2041
2042static bool handleFmt11x(CompilationUnit *cUnit, MIR *mir)
2043{
2044 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002045 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002046 switch (dalvikOpCode) {
2047 case OP_MOVE_EXCEPTION: {
2048 int offset = offsetof(InterpState, self);
2049 int exOffset = offsetof(Thread, exception);
Bill Buzbee1465db52009-09-23 17:17:35 -07002050 int selfReg = allocTemp(cUnit);
2051 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2052 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2053 loadWordDisp(cUnit, rGLUE, offset, selfReg);
2054 loadWordDisp(cUnit, selfReg, exOffset, rlResult.lowReg);
2055 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002056 break;
2057 }
2058 case OP_MOVE_RESULT:
2059 case OP_MOVE_RESULT_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002060 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2061 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL;
2062 rlSrc.fp = rlDest.fp;
2063 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002064 break;
2065 }
2066 case OP_MOVE_RESULT_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002067 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
2068 RegLocation rlSrc = LOC_DALVIK_RETURN_VAL_WIDE;
2069 rlSrc.fp = rlDest.fp;
2070 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002071 break;
2072 }
2073 case OP_RETURN_WIDE: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002074 RegLocation rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2075 RegLocation rlDest = LOC_DALVIK_RETURN_VAL_WIDE;
2076 rlDest.fp = rlSrc.fp;
2077 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002078 genReturnCommon(cUnit,mir);
2079 break;
2080 }
2081 case OP_RETURN:
2082 case OP_RETURN_OBJECT: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002083 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2084 RegLocation rlDest = LOC_DALVIK_RETURN_VAL;
2085 rlDest.fp = rlSrc.fp;
2086 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002087 genReturnCommon(cUnit,mir);
2088 break;
2089 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002090 case OP_MONITOR_EXIT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002091 case OP_MONITOR_ENTER:
Bill Buzbee1465db52009-09-23 17:17:35 -07002092#if defined(WITH_DEADLOCK_PREDICTION) || defined(WITH_MONITOR_TRACKING)
Ben Cheng5d90c202009-11-22 23:31:11 -08002093 genMonitorPortable(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002094#else
Ben Cheng5d90c202009-11-22 23:31:11 -08002095 genMonitor(cUnit, mir);
Bill Buzbee1465db52009-09-23 17:17:35 -07002096#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07002097 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002098 case OP_THROW: {
2099 genInterpSingleStep(cUnit, mir);
2100 break;
2101 }
2102 default:
2103 return true;
2104 }
2105 return false;
2106}
2107
Bill Buzbeed45ba372009-06-15 17:00:57 -07002108static bool handleFmt12x(CompilationUnit *cUnit, MIR *mir)
2109{
2110 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002111 RegLocation rlDest;
2112 RegLocation rlSrc;
2113 RegLocation rlResult;
Bill Buzbeed45ba372009-06-15 17:00:57 -07002114
Ben Chengba4fc8b2009-06-01 13:00:29 -07002115 if ( (opCode >= OP_ADD_INT_2ADDR) && (opCode <= OP_REM_DOUBLE_2ADDR)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002116 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002117 }
2118
Bill Buzbee1465db52009-09-23 17:17:35 -07002119 if (mir->ssaRep->numUses == 2)
2120 rlSrc = getSrcLocWide(cUnit, mir, 0, 1);
2121 else
2122 rlSrc = getSrcLoc(cUnit, mir, 0);
2123 if (mir->ssaRep->numDefs == 2)
2124 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2125 else
2126 rlDest = getDestLoc(cUnit, mir, 0);
Ben Chenge9695e52009-06-16 16:11:47 -07002127
Ben Chengba4fc8b2009-06-01 13:00:29 -07002128 switch (opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002129 case OP_DOUBLE_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002130 case OP_INT_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002131 case OP_FLOAT_TO_INT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002132 case OP_DOUBLE_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002133 case OP_FLOAT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002134 case OP_INT_TO_DOUBLE:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002135 case OP_FLOAT_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002136 case OP_LONG_TO_FLOAT:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002137 case OP_DOUBLE_TO_LONG:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002138 case OP_LONG_TO_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002139 return genConversion(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002140 case OP_NEG_INT:
2141 case OP_NOT_INT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002142 return genArithOpInt(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002143 case OP_NEG_LONG:
2144 case OP_NOT_LONG:
Ben Cheng5d90c202009-11-22 23:31:11 -08002145 return genArithOpLong(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002146 case OP_NEG_FLOAT:
Ben Cheng5d90c202009-11-22 23:31:11 -08002147 return genArithOpFloat(cUnit, mir, rlDest, rlSrc, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002148 case OP_NEG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002149 return genArithOpDouble(cUnit, mir, rlDest, rlSrc, rlSrc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002150 case OP_MOVE_WIDE:
2151 storeValueWide(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002152 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002153 case OP_INT_TO_LONG:
2154 rlSrc = updateLoc(cUnit, rlSrc);
2155 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2156 if (rlSrc.location == kLocPhysReg) {
2157 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2158 } else {
2159 loadValueDirect(cUnit, rlSrc, rlResult.lowReg);
2160 }
2161 opRegRegImm(cUnit, kOpAsr, rlResult.highReg,
2162 rlResult.lowReg, 31);
2163 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002164 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07002165 case OP_LONG_TO_INT:
2166 rlSrc = updateLocWide(cUnit, rlSrc);
2167 rlSrc = wideToNarrowLoc(cUnit, rlSrc);
2168 // Intentional fallthrough
Ben Chengba4fc8b2009-06-01 13:00:29 -07002169 case OP_MOVE:
2170 case OP_MOVE_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002171 storeValue(cUnit, rlDest, rlSrc);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002172 break;
2173 case OP_INT_TO_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002174 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2175 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2176 opRegReg(cUnit, kOp2Byte, rlResult.lowReg, rlSrc.lowReg);
2177 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002178 break;
2179 case OP_INT_TO_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002180 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2181 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2182 opRegReg(cUnit, kOp2Short, rlResult.lowReg, rlSrc.lowReg);
2183 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002184 break;
2185 case OP_INT_TO_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002186 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2187 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2188 opRegReg(cUnit, kOp2Char, rlResult.lowReg, rlSrc.lowReg);
2189 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002190 break;
2191 case OP_ARRAY_LENGTH: {
2192 int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07002193 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2194 genNullCheck(cUnit, rlSrc.sRegLow, rlSrc.lowReg,
2195 mir->offset, NULL);
2196 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2197 loadWordDisp(cUnit, rlSrc.lowReg, lenOffset,
2198 rlResult.lowReg);
2199 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002200 break;
2201 }
2202 default:
2203 return true;
2204 }
2205 return false;
2206}
2207
2208static bool handleFmt21s(CompilationUnit *cUnit, MIR *mir)
2209{
2210 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002211 RegLocation rlDest;
2212 RegLocation rlResult;
2213 int BBBB = mir->dalvikInsn.vB;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002214 if (dalvikOpCode == OP_CONST_WIDE_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002215 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2216 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2217 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
2218 opRegRegImm(cUnit, kOpAsr, rlResult.highReg, rlResult.lowReg, 31);
2219 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002220 } else if (dalvikOpCode == OP_CONST_16) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002221 rlDest = getDestLoc(cUnit, mir, 0);
2222 rlResult = evalLoc(cUnit, rlDest, kAnyReg, true);
2223 loadConstantValue(cUnit, rlResult.lowReg, BBBB);
2224 storeValue(cUnit, rlDest, rlResult);
2225 } else
Ben Chengba4fc8b2009-06-01 13:00:29 -07002226 return true;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002227 return false;
2228}
2229
2230/* Compare agaist zero */
2231static bool handleFmt21t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002232 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002233{
2234 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002235 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002236 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2237 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2238 opRegImm(cUnit, kOpCmp, rlSrc.lowReg, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002239
Bill Buzbee270c1d62009-08-13 16:58:07 -07002240//TUNING: break this out to allow use of Thumb2 CB[N]Z
Ben Chengba4fc8b2009-06-01 13:00:29 -07002241 switch (dalvikOpCode) {
2242 case OP_IF_EQZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002243 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002244 break;
2245 case OP_IF_NEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002246 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002247 break;
2248 case OP_IF_LTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002249 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002250 break;
2251 case OP_IF_GEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002252 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002253 break;
2254 case OP_IF_GTZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002255 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002256 break;
2257 case OP_IF_LEZ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002258 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002259 break;
2260 default:
2261 cond = 0;
2262 LOGE("Unexpected opcode (%d) for Fmt21t\n", dalvikOpCode);
2263 dvmAbort();
2264 }
2265 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2266 /* This mostly likely will be optimized away in a later phase */
2267 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2268 return false;
2269}
2270
2271static bool handleFmt22b_Fmt22s(CompilationUnit *cUnit, MIR *mir)
2272{
2273 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002274 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2275 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2276 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002277 int lit = mir->dalvikInsn.vC;
Ben Cheng4f489172009-09-27 17:08:35 -07002278 OpKind op = 0; /* Make gcc happy */
Bill Buzbee1465db52009-09-23 17:17:35 -07002279 int shiftOp = false;
2280 bool isDiv = false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002281
Ben Chengba4fc8b2009-06-01 13:00:29 -07002282 int __aeabi_idivmod(int op1, int op2);
2283 int __aeabi_idiv(int op1, int op2);
2284
2285 switch (dalvikOpCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07002286 case OP_RSUB_INT_LIT8:
2287 case OP_RSUB_INT: {
2288 int tReg;
2289 //TUNING: add support for use of Arm rsub op
2290 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2291 tReg = allocTemp(cUnit);
2292 loadConstant(cUnit, tReg, lit);
2293 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2294 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
2295 tReg, rlSrc.lowReg);
2296 storeValue(cUnit, rlDest, rlResult);
2297 return false;
2298 break;
2299 }
2300
Ben Chengba4fc8b2009-06-01 13:00:29 -07002301 case OP_ADD_INT_LIT8:
2302 case OP_ADD_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002303 op = kOpAdd;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002304 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002305 case OP_MUL_INT_LIT8:
2306 case OP_MUL_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002307 op = kOpMul;
2308 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002309 case OP_AND_INT_LIT8:
2310 case OP_AND_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002311 op = kOpAnd;
2312 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002313 case OP_OR_INT_LIT8:
2314 case OP_OR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002315 op = kOpOr;
2316 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002317 case OP_XOR_INT_LIT8:
2318 case OP_XOR_INT_LIT16:
Bill Buzbee1465db52009-09-23 17:17:35 -07002319 op = kOpXor;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002320 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002321 case OP_SHL_INT_LIT8:
Bill Buzbee1465db52009-09-23 17:17:35 -07002322 shiftOp = true;
2323 op = kOpLsl;
2324 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002325 case OP_SHR_INT_LIT8:
Bill Buzbee1465db52009-09-23 17:17:35 -07002326 shiftOp = true;
2327 op = kOpAsr;
2328 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002329 case OP_USHR_INT_LIT8:
Bill Buzbee1465db52009-09-23 17:17:35 -07002330 shiftOp = true;
2331 op = kOpLsr;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002332 break;
2333
2334 case OP_DIV_INT_LIT8:
2335 case OP_DIV_INT_LIT16:
Ben Chengba4fc8b2009-06-01 13:00:29 -07002336 case OP_REM_INT_LIT8:
2337 case OP_REM_INT_LIT16:
2338 if (lit == 0) {
2339 /* Let the interpreter deal with div by 0 */
2340 genInterpSingleStep(cUnit, mir);
2341 return false;
2342 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002343 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002344 loadValueDirectFixed(cUnit, rlSrc, r0);
2345 clobberReg(cUnit, r0);
2346 if ((dalvikOpCode == OP_DIV_INT_LIT8) ||
2347 (dalvikOpCode == OP_DIV_INT_LIT16)) {
2348 loadConstant(cUnit, r2, (int)__aeabi_idiv);
2349 isDiv = true;
2350 } else {
2351 loadConstant(cUnit, r2, (int)__aeabi_idivmod);
2352 isDiv = false;
2353 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002354 loadConstant(cUnit, r1, lit);
Bill Buzbee1465db52009-09-23 17:17:35 -07002355 opReg(cUnit, kOpBlx, r2);
2356 clobberCallRegs(cUnit);
2357 if (isDiv)
2358 rlResult = getReturnLoc(cUnit);
2359 else
2360 rlResult = getReturnLocAlt(cUnit);
2361 storeValue(cUnit, rlDest, rlResult);
2362 return false;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002363 break;
2364 default:
2365 return true;
2366 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002367 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2368 rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
2369 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2370 if (shiftOp && (lit == 0)) {
2371 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2372 } else {
2373 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2374 }
2375 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002376 return false;
2377}
2378
2379static bool handleFmt22c(CompilationUnit *cUnit, MIR *mir)
2380{
2381 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2382 int fieldOffset;
2383
2384 if (dalvikOpCode >= OP_IGET && dalvikOpCode <= OP_IPUT_SHORT) {
2385 InstField *pInstField = (InstField *)
2386 cUnit->method->clazz->pDvmDex->pResFields[mir->dalvikInsn.vC];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002387
2388 assert(pInstField != NULL);
2389 fieldOffset = pInstField->byteOffset;
2390 } else {
Ben Chenga0e7b602009-10-13 23:09:01 -07002391 /* Deliberately break the code while make the compiler happy */
2392 fieldOffset = -1;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002393 }
2394 switch (dalvikOpCode) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07002395 case OP_NEW_ARRAY: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002396 // Generates a call - use explicit registers
2397 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2398 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2399 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002400 void *classPtr = (void*)
2401 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2402 assert(classPtr != NULL);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002403 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002404 genExportPC(cUnit, mir);
2405 loadValueDirectFixed(cUnit, rlSrc, r1); /* Len */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002406 loadConstant(cUnit, r0, (int) classPtr );
Bill Buzbee1465db52009-09-23 17:17:35 -07002407 loadConstant(cUnit, r3, (int)dvmAllocArrayByClass);
Ben Cheng4f489172009-09-27 17:08:35 -07002408 /*
2409 * "len < 0": bail to the interpreter to re-execute the
2410 * instruction
2411 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002412 ArmLIR *pcrLabel =
Bill Buzbee1465db52009-09-23 17:17:35 -07002413 genRegImmCheck(cUnit, kArmCondMi, r1, 0, mir->offset, NULL);
Bill Buzbee270c1d62009-08-13 16:58:07 -07002414 loadConstant(cUnit, r2, ALLOC_DONT_TRACK);
Bill Buzbee1465db52009-09-23 17:17:35 -07002415 opReg(cUnit, kOpBlx, r3);
2416 clobberCallRegs(cUnit);
Ben Cheng4f489172009-09-27 17:08:35 -07002417 /* generate a branch over if allocation is successful */
Bill Buzbee1465db52009-09-23 17:17:35 -07002418 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2419 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
Ben Cheng4f489172009-09-27 17:08:35 -07002420 /*
2421 * OOM exception needs to be thrown here and cannot re-execute
2422 */
2423 loadConstant(cUnit, r0,
2424 (int) (cUnit->method->insns + mir->offset));
2425 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2426 /* noreturn */
2427
Bill Buzbee1465db52009-09-23 17:17:35 -07002428 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Cheng4f489172009-09-27 17:08:35 -07002429 target->defMask = ENCODE_ALL;
2430 branchOver->generic.target = (LIR *) target;
Bill Buzbee1465db52009-09-23 17:17:35 -07002431 rlResult = getReturnLoc(cUnit);
2432 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002433 break;
2434 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002435 case OP_INSTANCE_OF: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002436 // May generate a call - use explicit registers
2437 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2438 RegLocation rlDest = getDestLoc(cUnit, mir, 0);
2439 RegLocation rlResult;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002440 ClassObject *classPtr =
2441 (cUnit->method->clazz->pDvmDex->pResClasses[mir->dalvikInsn.vC]);
2442 assert(classPtr != NULL);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002443 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002444 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
Ben Chengba4fc8b2009-06-01 13:00:29 -07002445 loadConstant(cUnit, r2, (int) classPtr );
Bill Buzbee270c1d62009-08-13 16:58:07 -07002446//TUNING: compare to 0 primative to allow use of CB[N]Z
Bill Buzbee1465db52009-09-23 17:17:35 -07002447 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
Ben Cheng752c7942009-06-22 10:50:07 -07002448 /* When taken r0 has NULL which can be used for store directly */
Bill Buzbee1465db52009-09-23 17:17:35 -07002449 ArmLIR *branch1 = opCondBranch(cUnit, kArmCondEq);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002450 /* r1 now contains object->clazz */
Bill Buzbee270c1d62009-08-13 16:58:07 -07002451 loadWordDisp(cUnit, r0, offsetof(Object, clazz), r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07002452 /* r1 now contains object->clazz */
2453 loadConstant(cUnit, r3, (int)dvmInstanceofNonTrivial);
Ben Cheng752c7942009-06-22 10:50:07 -07002454 loadConstant(cUnit, r0, 1); /* Assume true */
Bill Buzbee1465db52009-09-23 17:17:35 -07002455 opRegReg(cUnit, kOpCmp, r1, r2);
2456 ArmLIR *branch2 = opCondBranch(cUnit, kArmCondEq);
2457 genRegCopy(cUnit, r0, r1);
2458 genRegCopy(cUnit, r1, r2);
2459 opReg(cUnit, kOpBlx, r3);
2460 clobberCallRegs(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002461 /* branch target here */
Bill Buzbee1465db52009-09-23 17:17:35 -07002462 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
Ben Chengd7d426a2009-09-22 11:23:36 -07002463 target->defMask = ENCODE_ALL;
Bill Buzbee1465db52009-09-23 17:17:35 -07002464 rlResult = getReturnLoc(cUnit);
2465 storeValue(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002466 branch1->generic.target = (LIR *)target;
2467 branch2->generic.target = (LIR *)target;
2468 break;
2469 }
2470 case OP_IGET_WIDE:
2471 genIGetWide(cUnit, mir, fieldOffset);
2472 break;
2473 case OP_IGET:
2474 case OP_IGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002475 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002476 break;
2477 case OP_IGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002478 genIGet(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002479 break;
2480 case OP_IGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002481 genIGet(cUnit, mir, kSignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002482 break;
2483 case OP_IGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002484 genIGet(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002485 break;
2486 case OP_IGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002487 genIGet(cUnit, mir, kSignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002488 break;
2489 case OP_IPUT_WIDE:
2490 genIPutWide(cUnit, mir, fieldOffset);
2491 break;
2492 case OP_IPUT:
2493 case OP_IPUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002494 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002495 break;
2496 case OP_IPUT_SHORT:
2497 case OP_IPUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002498 genIPut(cUnit, mir, kUnsignedHalf, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002499 break;
2500 case OP_IPUT_BYTE:
2501 case OP_IPUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002502 genIPut(cUnit, mir, kUnsignedByte, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002503 break;
2504 default:
2505 return true;
2506 }
2507 return false;
2508}
2509
2510static bool handleFmt22cs(CompilationUnit *cUnit, MIR *mir)
2511{
2512 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2513 int fieldOffset = mir->dalvikInsn.vC;
2514 switch (dalvikOpCode) {
2515 case OP_IGET_QUICK:
2516 case OP_IGET_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002517 genIGet(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002518 break;
2519 case OP_IPUT_QUICK:
2520 case OP_IPUT_OBJECT_QUICK:
Bill Buzbee1465db52009-09-23 17:17:35 -07002521 genIPut(cUnit, mir, kWord, fieldOffset);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002522 break;
2523 case OP_IGET_WIDE_QUICK:
2524 genIGetWide(cUnit, mir, fieldOffset);
2525 break;
2526 case OP_IPUT_WIDE_QUICK:
2527 genIPutWide(cUnit, mir, fieldOffset);
2528 break;
2529 default:
2530 return true;
2531 }
2532 return false;
2533
2534}
2535
2536/* Compare agaist zero */
2537static bool handleFmt22t(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002538 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002539{
2540 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002541 ArmConditionCode cond;
Bill Buzbee1465db52009-09-23 17:17:35 -07002542 RegLocation rlSrc1 = getSrcLoc(cUnit, mir, 0);
2543 RegLocation rlSrc2 = getSrcLoc(cUnit, mir, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002544
Bill Buzbee1465db52009-09-23 17:17:35 -07002545 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
2546 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
2547 opRegReg(cUnit, kOpCmp, rlSrc1.lowReg, rlSrc2.lowReg);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002548
2549 switch (dalvikOpCode) {
2550 case OP_IF_EQ:
Bill Buzbee1465db52009-09-23 17:17:35 -07002551 cond = kArmCondEq;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002552 break;
2553 case OP_IF_NE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002554 cond = kArmCondNe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002555 break;
2556 case OP_IF_LT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002557 cond = kArmCondLt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002558 break;
2559 case OP_IF_GE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002560 cond = kArmCondGe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002561 break;
2562 case OP_IF_GT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002563 cond = kArmCondGt;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002564 break;
2565 case OP_IF_LE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002566 cond = kArmCondLe;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002567 break;
2568 default:
2569 cond = 0;
2570 LOGE("Unexpected opcode (%d) for Fmt22t\n", dalvikOpCode);
2571 dvmAbort();
2572 }
2573 genConditionalBranch(cUnit, cond, &labelList[bb->taken->id]);
2574 /* This mostly likely will be optimized away in a later phase */
2575 genUnconditionalBranch(cUnit, &labelList[bb->fallThrough->id]);
2576 return false;
2577}
2578
2579static bool handleFmt22x_Fmt32x(CompilationUnit *cUnit, MIR *mir)
2580{
2581 OpCode opCode = mir->dalvikInsn.opCode;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002582
2583 switch (opCode) {
2584 case OP_MOVE_16:
2585 case OP_MOVE_OBJECT_16:
2586 case OP_MOVE_FROM16:
Ben Chenge9695e52009-06-16 16:11:47 -07002587 case OP_MOVE_OBJECT_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002588 storeValue(cUnit, getDestLoc(cUnit, mir, 0),
2589 getSrcLoc(cUnit, mir, 0));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002590 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002591 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002592 case OP_MOVE_WIDE_16:
Ben Chenge9695e52009-06-16 16:11:47 -07002593 case OP_MOVE_WIDE_FROM16: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002594 storeValueWide(cUnit, getDestLocWide(cUnit, mir, 0, 1),
2595 getSrcLocWide(cUnit, mir, 0, 1));
Ben Chengba4fc8b2009-06-01 13:00:29 -07002596 break;
Ben Chenge9695e52009-06-16 16:11:47 -07002597 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07002598 default:
2599 return true;
2600 }
2601 return false;
2602}
2603
2604static bool handleFmt23x(CompilationUnit *cUnit, MIR *mir)
2605{
2606 OpCode opCode = mir->dalvikInsn.opCode;
Bill Buzbee1465db52009-09-23 17:17:35 -07002607 RegLocation rlSrc1;
2608 RegLocation rlSrc2;
2609 RegLocation rlDest;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002610
2611 if ( (opCode >= OP_ADD_INT) && (opCode <= OP_REM_DOUBLE)) {
Ben Cheng5d90c202009-11-22 23:31:11 -08002612 return genArithOp( cUnit, mir );
Ben Chengba4fc8b2009-06-01 13:00:29 -07002613 }
2614
Bill Buzbee1465db52009-09-23 17:17:35 -07002615 /* APUTs have 3 sources and no targets */
2616 if (mir->ssaRep->numDefs == 0) {
2617 if (mir->ssaRep->numUses == 3) {
2618 rlDest = getSrcLoc(cUnit, mir, 0);
2619 rlSrc1 = getSrcLoc(cUnit, mir, 1);
2620 rlSrc2 = getSrcLoc(cUnit, mir, 2);
2621 } else {
2622 assert(mir->ssaRep->numUses == 4);
2623 rlDest = getSrcLocWide(cUnit, mir, 0, 1);
2624 rlSrc1 = getSrcLoc(cUnit, mir, 2);
2625 rlSrc2 = getSrcLoc(cUnit, mir, 3);
2626 }
2627 } else {
2628 /* Two sources and 1 dest. Deduce the operand sizes */
2629 if (mir->ssaRep->numUses == 4) {
2630 rlSrc1 = getSrcLocWide(cUnit, mir, 0, 1);
2631 rlSrc2 = getSrcLocWide(cUnit, mir, 2, 3);
2632 } else {
2633 assert(mir->ssaRep->numUses == 2);
2634 rlSrc1 = getSrcLoc(cUnit, mir, 0);
2635 rlSrc2 = getSrcLoc(cUnit, mir, 1);
2636 }
2637 if (mir->ssaRep->numDefs == 2) {
2638 rlDest = getDestLocWide(cUnit, mir, 0, 1);
2639 } else {
2640 assert(mir->ssaRep->numDefs == 1);
2641 rlDest = getDestLoc(cUnit, mir, 0);
2642 }
2643 }
2644
2645
Ben Chengba4fc8b2009-06-01 13:00:29 -07002646 switch (opCode) {
Bill Buzbeed45ba372009-06-15 17:00:57 -07002647 case OP_CMPL_FLOAT:
2648 case OP_CMPG_FLOAT:
2649 case OP_CMPL_DOUBLE:
2650 case OP_CMPG_DOUBLE:
Ben Cheng5d90c202009-11-22 23:31:11 -08002651 return genCmpFP(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002652 case OP_CMP_LONG:
Bill Buzbee1465db52009-09-23 17:17:35 -07002653 genCmpLong(cUnit, mir, rlDest, rlSrc1, rlSrc2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002654 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002655 case OP_AGET_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002656 genArrayGet(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002657 break;
2658 case OP_AGET:
2659 case OP_AGET_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002660 genArrayGet(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002661 break;
2662 case OP_AGET_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002663 genArrayGet(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002664 break;
2665 case OP_AGET_BYTE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002666 genArrayGet(cUnit, mir, kSignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002667 break;
2668 case OP_AGET_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002669 genArrayGet(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002670 break;
2671 case OP_AGET_SHORT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002672 genArrayGet(cUnit, mir, kSignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002673 break;
2674 case OP_APUT_WIDE:
Bill Buzbee1465db52009-09-23 17:17:35 -07002675 genArrayPut(cUnit, mir, kLong, rlSrc1, rlSrc2, rlDest, 3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002676 break;
2677 case OP_APUT:
2678 case OP_APUT_OBJECT:
Bill Buzbee1465db52009-09-23 17:17:35 -07002679 genArrayPut(cUnit, mir, kWord, rlSrc1, rlSrc2, rlDest, 2);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002680 break;
2681 case OP_APUT_SHORT:
2682 case OP_APUT_CHAR:
Bill Buzbee1465db52009-09-23 17:17:35 -07002683 genArrayPut(cUnit, mir, kUnsignedHalf, rlSrc1, rlSrc2, rlDest, 1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002684 break;
2685 case OP_APUT_BYTE:
2686 case OP_APUT_BOOLEAN:
Bill Buzbee1465db52009-09-23 17:17:35 -07002687 genArrayPut(cUnit, mir, kUnsignedByte, rlSrc1, rlSrc2, rlDest, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002688 break;
2689 default:
2690 return true;
2691 }
2692 return false;
2693}
2694
Ben Cheng6c10a972009-10-29 14:39:18 -07002695/*
2696 * Find the matching case.
2697 *
2698 * return values:
2699 * r0 (low 32-bit): pc of the chaining cell corresponding to the resolved case,
2700 * including default which is placed at MIN(size, MAX_CHAINED_SWITCH_CASES).
2701 * r1 (high 32-bit): the branch offset of the matching case (only for indexes
2702 * above MAX_CHAINED_SWITCH_CASES).
2703 *
2704 * Instructions around the call are:
2705 *
2706 * mov r2, pc
2707 * blx &findPackedSwitchIndex
2708 * mov pc, r0
2709 * .align4
2710 * chaining cell for case 0 [8 bytes]
2711 * chaining cell for case 1 [8 bytes]
2712 * :
2713 * chaining cell for case MIN(size, MAX_CHAINED_SWITCH_CASES)-1 [8 bytes]
2714 * chaining cell for case default [8 bytes]
2715 * noChain exit
2716 */
2717s8 findPackedSwitchIndex(const u2* switchData, int testVal, int pc)
2718{
2719 int size;
2720 int firstKey;
2721 const int *entries;
2722 int index;
2723 int jumpIndex;
2724 int caseDPCOffset = 0;
2725 /* In Thumb mode pc is 4 ahead of the "mov r2, pc" instruction */
2726 int chainingPC = (pc + 4) & ~3;
2727
2728 /*
2729 * Packed switch data format:
2730 * ushort ident = 0x0100 magic value
2731 * ushort size number of entries in the table
2732 * int first_key first (and lowest) switch case value
2733 * int targets[size] branch targets, relative to switch opcode
2734 *
2735 * Total size is (4+size*2) 16-bit code units.
2736 */
2737 size = switchData[1];
2738 assert(size > 0);
2739
2740 firstKey = switchData[2];
2741 firstKey |= switchData[3] << 16;
2742
2743
2744 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2745 * we can treat them as a native int array.
2746 */
2747 entries = (const int*) &switchData[4];
2748 assert(((u4)entries & 0x3) == 0);
2749
2750 index = testVal - firstKey;
2751
2752 /* Jump to the default cell */
2753 if (index < 0 || index >= size) {
2754 jumpIndex = MIN(size, MAX_CHAINED_SWITCH_CASES);
2755 /* Jump to the non-chaining exit point */
2756 } else if (index >= MAX_CHAINED_SWITCH_CASES) {
2757 jumpIndex = MAX_CHAINED_SWITCH_CASES + 1;
2758 caseDPCOffset = entries[index];
2759 /* Jump to the inline chaining cell */
2760 } else {
2761 jumpIndex = index;
2762 }
2763
2764 chainingPC += jumpIndex * 8;
2765 return (((s8) caseDPCOffset) << 32) | (u8) chainingPC;
2766}
2767
2768/* See comments for findPackedSwitchIndex */
2769s8 findSparseSwitchIndex(const u2* switchData, int testVal, int pc)
2770{
2771 int size;
2772 const int *keys;
2773 const int *entries;
2774 int chainingPC = (pc + 4) & ~3;
2775 int i;
2776
2777 /*
2778 * Sparse switch data format:
2779 * ushort ident = 0x0200 magic value
2780 * ushort size number of entries in the table; > 0
2781 * int keys[size] keys, sorted low-to-high; 32-bit aligned
2782 * int targets[size] branch targets, relative to switch opcode
2783 *
2784 * Total size is (2+size*4) 16-bit code units.
2785 */
2786
2787 size = switchData[1];
2788 assert(size > 0);
2789
2790 /* The keys are guaranteed to be aligned on a 32-bit boundary;
2791 * we can treat them as a native int array.
2792 */
2793 keys = (const int*) &switchData[2];
2794 assert(((u4)keys & 0x3) == 0);
2795
2796 /* The entries are guaranteed to be aligned on a 32-bit boundary;
2797 * we can treat them as a native int array.
2798 */
2799 entries = keys + size;
2800 assert(((u4)entries & 0x3) == 0);
2801
2802 /*
2803 * Run through the list of keys, which are guaranteed to
2804 * be sorted low-to-high.
2805 *
2806 * Most tables have 3-4 entries. Few have more than 10. A binary
2807 * search here is probably not useful.
2808 */
2809 for (i = 0; i < size; i++) {
2810 int k = keys[i];
2811 if (k == testVal) {
2812 /* MAX_CHAINED_SWITCH_CASES + 1 is the start of the overflow case */
2813 int jumpIndex = (i < MAX_CHAINED_SWITCH_CASES) ?
2814 i : MAX_CHAINED_SWITCH_CASES + 1;
2815 chainingPC += jumpIndex * 8;
2816 return (((s8) entries[i]) << 32) | (u8) chainingPC;
2817 } else if (k > testVal) {
2818 break;
2819 }
2820 }
2821 return chainingPC + MIN(size, MAX_CHAINED_SWITCH_CASES) * 8;
2822}
2823
Ben Chengba4fc8b2009-06-01 13:00:29 -07002824static bool handleFmt31t(CompilationUnit *cUnit, MIR *mir)
2825{
2826 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
2827 switch (dalvikOpCode) {
2828 case OP_FILL_ARRAY_DATA: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002829 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
2830 // Making a call - use explicit registers
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002831 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002832 genExportPC(cUnit, mir);
2833 loadValueDirectFixed(cUnit, rlSrc, r0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002834 loadConstant(cUnit, r2, (int)dvmInterpHandleFillArrayData);
Ben Cheng6c10a972009-10-29 14:39:18 -07002835 loadConstant(cUnit, r1,
2836 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
Bill Buzbee1465db52009-09-23 17:17:35 -07002837 opReg(cUnit, kOpBlx, r2);
2838 clobberCallRegs(cUnit);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002839 /* generate a branch over if successful */
2840 opRegImm(cUnit, kOpCmp, r0, 0); /* NULL? */
2841 ArmLIR *branchOver = opCondBranch(cUnit, kArmCondNe);
2842 loadConstant(cUnit, r0,
2843 (int) (cUnit->method->insns + mir->offset));
2844 genDispatchToHandler(cUnit, TEMPLATE_THROW_EXCEPTION_COMMON);
2845 ArmLIR *target = newLIR0(cUnit, kArmPseudoTargetLabel);
2846 target->defMask = ENCODE_ALL;
2847 branchOver->generic.target = (LIR *) target;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002848 break;
2849 }
2850 /*
Ben Cheng6c10a972009-10-29 14:39:18 -07002851 * Compute the goto target of up to
2852 * MIN(switchSize, MAX_CHAINED_SWITCH_CASES) + 1 chaining cells.
2853 * See the comment before findPackedSwitchIndex for the code layout.
Ben Chengba4fc8b2009-06-01 13:00:29 -07002854 */
2855 case OP_PACKED_SWITCH:
2856 case OP_SPARSE_SWITCH: {
Bill Buzbee1465db52009-09-23 17:17:35 -07002857 RegLocation rlSrc = getSrcLoc(cUnit, mir, 0);
Bill Buzbeefd023aa2009-11-02 09:23:49 -08002858 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07002859 loadValueDirectFixed(cUnit, rlSrc, r1);
2860 lockAllTemps(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002861 const u2 *switchData =
2862 cUnit->method->insns + mir->offset + mir->dalvikInsn.vB;
2863 u2 size = switchData[1];
2864
Ben Chengba4fc8b2009-06-01 13:00:29 -07002865 if (dalvikOpCode == OP_PACKED_SWITCH) {
Ben Cheng6c10a972009-10-29 14:39:18 -07002866 loadConstant(cUnit, r4PC, (int)findPackedSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002867 } else {
Ben Cheng6c10a972009-10-29 14:39:18 -07002868 loadConstant(cUnit, r4PC, (int)findSparseSwitchIndex);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002869 }
Ben Cheng6c10a972009-10-29 14:39:18 -07002870 /* r0 <- Addr of the switch data */
2871 loadConstant(cUnit, r0,
2872 (int) (cUnit->method->insns + mir->offset + mir->dalvikInsn.vB));
2873 /* r2 <- pc of the instruction following the blx */
2874 opRegReg(cUnit, kOpMov, r2, rpc);
Bill Buzbee1465db52009-09-23 17:17:35 -07002875 opReg(cUnit, kOpBlx, r4PC);
2876 clobberCallRegs(cUnit);
Ben Cheng6c10a972009-10-29 14:39:18 -07002877 /* pc <- computed goto target */
2878 opRegReg(cUnit, kOpMov, rpc, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002879 break;
2880 }
2881 default:
2882 return true;
2883 }
2884 return false;
2885}
2886
2887static bool handleFmt35c_3rc(CompilationUnit *cUnit, MIR *mir, BasicBlock *bb,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002888 ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07002889{
Bill Buzbee9bc3df32009-07-30 10:52:29 -07002890 ArmLIR *retChainingCell = NULL;
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002891 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07002892
Bill Buzbeef4ce16f2009-07-28 13:28:25 -07002893 if (bb->fallThrough != NULL)
2894 retChainingCell = &labelList[bb->fallThrough->id];
2895
Ben Chengba4fc8b2009-06-01 13:00:29 -07002896 DecodedInstruction *dInsn = &mir->dalvikInsn;
2897 switch (mir->dalvikInsn.opCode) {
2898 /*
2899 * calleeMethod = this->clazz->vtable[
2900 * method->clazz->pDvmDex->pResMethods[BBBB]->methodIndex
2901 * ]
2902 */
2903 case OP_INVOKE_VIRTUAL:
2904 case OP_INVOKE_VIRTUAL_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07002905 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07002906 int methodIndex =
2907 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB]->
2908 methodIndex;
2909
2910 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL)
2911 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2912 else
2913 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2914
Ben Cheng38329f52009-07-07 14:19:20 -07002915 genInvokeVirtualCommon(cUnit, mir, methodIndex,
2916 retChainingCell,
2917 predChainingCell,
2918 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002919 break;
2920 }
2921 /*
2922 * calleeMethod = method->clazz->super->vtable[method->clazz->pDvmDex
2923 * ->pResMethods[BBBB]->methodIndex]
2924 */
2925 /* TODO - not excersized in RunPerf.jar */
2926 case OP_INVOKE_SUPER:
2927 case OP_INVOKE_SUPER_RANGE: {
2928 int mIndex = cUnit->method->clazz->pDvmDex->
2929 pResMethods[dInsn->vB]->methodIndex;
2930 const Method *calleeMethod =
2931 cUnit->method->clazz->super->vtable[mIndex];
2932
2933 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER)
2934 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2935 else
2936 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2937
2938 /* r0 = calleeMethod */
2939 loadConstant(cUnit, r0, (int) calleeMethod);
2940
Ben Cheng38329f52009-07-07 14:19:20 -07002941 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2942 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002943 break;
2944 }
2945 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2946 case OP_INVOKE_DIRECT:
2947 case OP_INVOKE_DIRECT_RANGE: {
2948 const Method *calleeMethod =
2949 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2950
2951 if (mir->dalvikInsn.opCode == OP_INVOKE_DIRECT)
2952 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
2953 else
2954 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
2955
2956 /* r0 = calleeMethod */
2957 loadConstant(cUnit, r0, (int) calleeMethod);
2958
Ben Cheng38329f52009-07-07 14:19:20 -07002959 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
2960 calleeMethod);
Ben Chengba4fc8b2009-06-01 13:00:29 -07002961 break;
2962 }
2963 /* calleeMethod = method->clazz->pDvmDex->pResMethods[BBBB] */
2964 case OP_INVOKE_STATIC:
2965 case OP_INVOKE_STATIC_RANGE: {
2966 const Method *calleeMethod =
2967 cUnit->method->clazz->pDvmDex->pResMethods[dInsn->vB];
2968
2969 if (mir->dalvikInsn.opCode == OP_INVOKE_STATIC)
2970 genProcessArgsNoRange(cUnit, mir, dInsn,
2971 NULL /* no null check */);
2972 else
2973 genProcessArgsRange(cUnit, mir, dInsn,
2974 NULL /* no null check */);
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 }
Bill Buzbee1465db52009-09-23 17:17:35 -07002983 /*
Ben Chengba4fc8b2009-06-01 13:00:29 -07002984 * calleeMethod = dvmFindInterfaceMethodInCache(this->clazz,
2985 * BBBB, method, method->clazz->pDvmDex)
Ben Cheng38329f52009-07-07 14:19:20 -07002986 *
2987 * Given "invoke-interface {v0}", the following is the generated code:
2988 *
2989 * 0x426a9abe : ldr r0, [r5, #0] --+
2990 * 0x426a9ac0 : mov r7, r5 |
2991 * 0x426a9ac2 : sub r7, #24 |
2992 * 0x426a9ac4 : cmp r0, #0 | genProcessArgsNoRange
2993 * 0x426a9ac6 : beq 0x426a9afe |
2994 * 0x426a9ac8 : stmia r7, <r0> --+
2995 * 0x426a9aca : ldr r4, [pc, #104] --> r4 <- dalvikPC of this invoke
2996 * 0x426a9acc : add r1, pc, #52 --> r1 <- &retChainingCell
2997 * 0x426a9ace : add r2, pc, #60 --> r2 <- &predictedChainingCell
2998 * 0x426a9ad0 : blx_1 0x426a918c --+ TEMPLATE_INVOKE_METHOD_
2999 * 0x426a9ad2 : blx_2 see above --+ PREDICTED_CHAIN
3000 * 0x426a9ad4 : b 0x426a9b0c --> off to the predicted chain
3001 * 0x426a9ad6 : b 0x426a9afe --> punt to the interpreter
Ben Chenga8e64a72009-10-20 13:01:36 -07003002 * 0x426a9ad8 : mov r8, r1 --+
3003 * 0x426a9ada : mov r9, r2 |
3004 * 0x426a9adc : mov r10, r3 |
Ben Cheng38329f52009-07-07 14:19:20 -07003005 * 0x426a9ade : mov r0, r3 |
3006 * 0x426a9ae0 : mov r1, #74 | dvmFindInterfaceMethodInCache
3007 * 0x426a9ae2 : ldr r2, [pc, #76] |
3008 * 0x426a9ae4 : ldr r3, [pc, #68] |
3009 * 0x426a9ae6 : ldr r7, [pc, #64] |
3010 * 0x426a9ae8 : blx r7 --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003011 * 0x426a9aea : mov r1, r8 --> r1 <- rechain count
Ben Cheng38329f52009-07-07 14:19:20 -07003012 * 0x426a9aec : cmp r1, #0 --> compare against 0
3013 * 0x426a9aee : bgt 0x426a9af8 --> >=0? don't rechain
3014 * 0x426a9af0 : ldr r7, [r6, #96] --+
Ben Chenga8e64a72009-10-20 13:01:36 -07003015 * 0x426a9af2 : mov r2, r9 | dvmJitToPatchPredictedChain
3016 * 0x426a9af4 : mov r3, r10 |
Ben Cheng38329f52009-07-07 14:19:20 -07003017 * 0x426a9af6 : blx r7 --+
3018 * 0x426a9af8 : add r1, pc, #8 --> r1 <- &retChainingCell
3019 * 0x426a9afa : blx_1 0x426a9098 --+ TEMPLATE_INVOKE_METHOD_NO_OPT
3020 * 0x426a9afc : blx_2 see above --+
3021 * -------- reconstruct dalvik PC : 0x428b786c @ +0x001e
3022 * 0x426a9afe (0042): ldr r0, [pc, #52]
3023 * Exception_Handling:
3024 * 0x426a9b00 (0044): ldr r1, [r6, #84]
3025 * 0x426a9b02 (0046): blx r1
3026 * 0x426a9b04 (0048): .align4
3027 * -------- chaining cell (hot): 0x0021
3028 * 0x426a9b04 (0048): ldr r0, [r6, #92]
3029 * 0x426a9b06 (004a): blx r0
3030 * 0x426a9b08 (004c): data 0x7872(30834)
3031 * 0x426a9b0a (004e): data 0x428b(17035)
3032 * 0x426a9b0c (0050): .align4
3033 * -------- chaining cell (predicted)
3034 * 0x426a9b0c (0050): data 0x0000(0) --> will be patched into bx
3035 * 0x426a9b0e (0052): data 0x0000(0)
3036 * 0x426a9b10 (0054): data 0x0000(0) --> class
3037 * 0x426a9b12 (0056): data 0x0000(0)
3038 * 0x426a9b14 (0058): data 0x0000(0) --> method
3039 * 0x426a9b16 (005a): data 0x0000(0)
3040 * 0x426a9b18 (005c): data 0x0000(0) --> reset count
3041 * 0x426a9b1a (005e): data 0x0000(0)
3042 * 0x426a9b28 (006c): .word (0xad0392a5)
3043 * 0x426a9b2c (0070): .word (0x6e750)
3044 * 0x426a9b30 (0074): .word (0x4109a618)
3045 * 0x426a9b34 (0078): .word (0x428b786c)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003046 */
3047 case OP_INVOKE_INTERFACE:
3048 case OP_INVOKE_INTERFACE_RANGE: {
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003049 ArmLIR *predChainingCell = &labelList[bb->taken->id];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003050 int methodIndex = dInsn->vB;
3051
Bill Buzbee1465db52009-09-23 17:17:35 -07003052 /* Ensure that nothing is both live and dirty */
3053 flushAllRegs(cUnit);
3054
Ben Chengba4fc8b2009-06-01 13:00:29 -07003055 if (mir->dalvikInsn.opCode == OP_INVOKE_INTERFACE)
3056 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3057 else
3058 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3059
Ben Cheng38329f52009-07-07 14:19:20 -07003060 /* "this" is already left in r0 by genProcessArgs* */
3061
3062 /* r4PC = dalvikCallsite */
3063 loadConstant(cUnit, r4PC,
3064 (int) (cUnit->method->insns + mir->offset));
3065
3066 /* r1 = &retChainingCell */
Bill Buzbee270c1d62009-08-13 16:58:07 -07003067 ArmLIR *addrRetChain =
Bill Buzbee1465db52009-09-23 17:17:35 -07003068 opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003069 addrRetChain->generic.target = (LIR *) retChainingCell;
3070
3071 /* r2 = &predictedChainingCell */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003072 ArmLIR *predictedChainingCell =
Bill Buzbee1465db52009-09-23 17:17:35 -07003073 opRegRegImm(cUnit, kOpAdd, r2, rpc, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003074 predictedChainingCell->generic.target = (LIR *) predChainingCell;
3075
3076 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_PREDICTED_CHAIN);
3077
3078 /* return through lr - jump to the chaining cell */
3079 genUnconditionalBranch(cUnit, predChainingCell);
3080
3081 /*
3082 * null-check on "this" may have been eliminated, but we still need
3083 * a PC-reconstruction label for stack overflow bailout.
3084 */
3085 if (pcrLabel == NULL) {
3086 int dPC = (int) (cUnit->method->insns + mir->offset);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003087 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003088 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng38329f52009-07-07 14:19:20 -07003089 pcrLabel->operands[0] = dPC;
3090 pcrLabel->operands[1] = mir->offset;
3091 /* Insert the place holder to the growable list */
3092 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3093 }
3094
3095 /* return through lr+2 - punt to the interpreter */
3096 genUnconditionalBranch(cUnit, pcrLabel);
3097
3098 /*
3099 * return through lr+4 - fully resolve the callee method.
3100 * r1 <- count
3101 * r2 <- &predictedChainCell
3102 * r3 <- this->class
3103 * r4 <- dPC
3104 * r7 <- this->class->vtable
3105 */
3106
3107 /* Save count, &predictedChainCell, and class to high regs first */
Bill Buzbee1465db52009-09-23 17:17:35 -07003108 genRegCopy(cUnit, r8, r1);
3109 genRegCopy(cUnit, r9, r2);
3110 genRegCopy(cUnit, r10, r3);
Ben Cheng38329f52009-07-07 14:19:20 -07003111
Ben Chengba4fc8b2009-06-01 13:00:29 -07003112 /* r0 now contains this->clazz */
Bill Buzbee1465db52009-09-23 17:17:35 -07003113 genRegCopy(cUnit, r0, r3);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003114
3115 /* r1 = BBBB */
3116 loadConstant(cUnit, r1, dInsn->vB);
3117
3118 /* r2 = method (caller) */
3119 loadConstant(cUnit, r2, (int) cUnit->method);
3120
3121 /* r3 = pDvmDex */
3122 loadConstant(cUnit, r3, (int) cUnit->method->clazz->pDvmDex);
3123
3124 loadConstant(cUnit, r7,
3125 (intptr_t) dvmFindInterfaceMethodInCache);
Bill Buzbee1465db52009-09-23 17:17:35 -07003126 opReg(cUnit, kOpBlx, r7);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003127
3128 /* r0 = calleeMethod (returned from dvmFindInterfaceMethodInCache */
3129
Bill Buzbee1465db52009-09-23 17:17:35 -07003130 genRegCopy(cUnit, r1, r8);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003131
Ben Cheng38329f52009-07-07 14:19:20 -07003132 /* Check if rechain limit is reached */
Bill Buzbee1465db52009-09-23 17:17:35 -07003133 opRegImm(cUnit, kOpCmp, r1, 0);
Ben Cheng38329f52009-07-07 14:19:20 -07003134
Bill Buzbee1465db52009-09-23 17:17:35 -07003135 ArmLIR *bypassRechaining = opCondBranch(cUnit, kArmCondGt);
Ben Cheng38329f52009-07-07 14:19:20 -07003136
Bill Buzbee270c1d62009-08-13 16:58:07 -07003137 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3138 jitToInterpEntries.dvmJitToPatchPredictedChain), r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003139
Bill Buzbee1465db52009-09-23 17:17:35 -07003140 genRegCopy(cUnit, r2, r9);
3141 genRegCopy(cUnit, r3, r10);
Ben Cheng38329f52009-07-07 14:19:20 -07003142
3143 /*
3144 * r0 = calleeMethod
3145 * r2 = &predictedChainingCell
3146 * r3 = class
3147 *
3148 * &returnChainingCell has been loaded into r1 but is not needed
3149 * when patching the chaining cell and will be clobbered upon
3150 * returning so it will be reconstructed again.
3151 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003152 opReg(cUnit, kOpBlx, r7);
Ben Cheng38329f52009-07-07 14:19:20 -07003153
3154 /* r1 = &retChainingCell */
Bill Buzbee1465db52009-09-23 17:17:35 -07003155 addrRetChain = opRegRegImm(cUnit, kOpAdd, r1, rpc, 0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003156 addrRetChain->generic.target = (LIR *) retChainingCell;
Ben Cheng38329f52009-07-07 14:19:20 -07003157
3158 bypassRechaining->generic.target = (LIR *) addrRetChain;
3159
Ben Chengba4fc8b2009-06-01 13:00:29 -07003160 /*
3161 * r0 = this, r1 = calleeMethod,
3162 * r1 = &ChainingCell,
3163 * r4PC = callsiteDPC,
3164 */
3165 genDispatchToHandler(cUnit, TEMPLATE_INVOKE_METHOD_NO_OPT);
3166#if defined(INVOKE_STATS)
Ben Cheng38329f52009-07-07 14:19:20 -07003167 gDvmJit.invokePredictedChain++;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003168#endif
3169 /* Handle exceptions using the interpreter */
3170 genTrap(cUnit, mir->offset, pcrLabel);
3171 break;
3172 }
3173 /* NOP */
3174 case OP_INVOKE_DIRECT_EMPTY: {
3175 return false;
3176 }
3177 case OP_FILLED_NEW_ARRAY:
3178 case OP_FILLED_NEW_ARRAY_RANGE: {
3179 /* Just let the interpreter deal with these */
3180 genInterpSingleStep(cUnit, mir);
3181 break;
3182 }
3183 default:
3184 return true;
3185 }
3186 return false;
3187}
3188
3189static bool handleFmt35ms_3rms(CompilationUnit *cUnit, MIR *mir,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003190 BasicBlock *bb, ArmLIR *labelList)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003191{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003192 ArmLIR *retChainingCell = &labelList[bb->fallThrough->id];
3193 ArmLIR *predChainingCell = &labelList[bb->taken->id];
3194 ArmLIR *pcrLabel = NULL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003195
3196 DecodedInstruction *dInsn = &mir->dalvikInsn;
3197 switch (mir->dalvikInsn.opCode) {
3198 /* calleeMethod = this->clazz->vtable[BBBB] */
3199 case OP_INVOKE_VIRTUAL_QUICK_RANGE:
3200 case OP_INVOKE_VIRTUAL_QUICK: {
3201 int methodIndex = dInsn->vB;
3202 if (mir->dalvikInsn.opCode == OP_INVOKE_VIRTUAL_QUICK)
3203 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3204 else
3205 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3206
Ben Cheng38329f52009-07-07 14:19:20 -07003207 genInvokeVirtualCommon(cUnit, mir, methodIndex,
3208 retChainingCell,
3209 predChainingCell,
3210 pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003211 break;
3212 }
3213 /* calleeMethod = method->clazz->super->vtable[BBBB] */
3214 case OP_INVOKE_SUPER_QUICK:
3215 case OP_INVOKE_SUPER_QUICK_RANGE: {
3216 const Method *calleeMethod =
3217 cUnit->method->clazz->super->vtable[dInsn->vB];
3218
3219 if (mir->dalvikInsn.opCode == OP_INVOKE_SUPER_QUICK)
3220 genProcessArgsNoRange(cUnit, mir, dInsn, &pcrLabel);
3221 else
3222 genProcessArgsRange(cUnit, mir, dInsn, &pcrLabel);
3223
3224 /* r0 = calleeMethod */
3225 loadConstant(cUnit, r0, (int) calleeMethod);
3226
Ben Cheng38329f52009-07-07 14:19:20 -07003227 genInvokeSingletonCommon(cUnit, mir, bb, labelList, pcrLabel,
3228 calleeMethod);
3229 /* Handle exceptions using the interpreter */
3230 genTrap(cUnit, mir->offset, pcrLabel);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003231 break;
3232 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003233 default:
3234 return true;
3235 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07003236 return false;
3237}
3238
3239/*
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003240 * This operation is complex enough that we'll do it partly inline
3241 * and partly with a handler. NOTE: the handler uses hardcoded
3242 * values for string object offsets and must be revisitied if the
3243 * layout changes.
3244 */
3245static bool genInlinedCompareTo(CompilationUnit *cUnit, MIR *mir)
3246{
3247#if defined(USE_GLOBAL_STRING_DEFS)
3248 return false;
3249#else
3250 ArmLIR *rollback;
3251 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3252 RegLocation rlComp = getSrcLoc(cUnit, mir, 1);
3253
3254 loadValueDirectFixed(cUnit, rlThis, r0);
3255 loadValueDirectFixed(cUnit, rlComp, r1);
3256 /* Test objects for NULL */
3257 rollback = genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3258 genNullCheck(cUnit, rlComp.sRegLow, r1, mir->offset, rollback);
3259 /*
3260 * TUNING: we could check for object pointer equality before invoking
3261 * handler. Unclear whether the gain would be worth the added code size
3262 * expansion.
3263 */
3264 genDispatchToHandler(cUnit, TEMPLATE_STRING_COMPARETO);
3265 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3266 return true;
3267#endif
3268}
3269
3270static bool genInlinedIndexOf(CompilationUnit *cUnit, MIR *mir, bool singleI)
3271{
3272#if defined(USE_GLOBAL_STRING_DEFS)
3273 return false;
3274#else
3275 RegLocation rlThis = getSrcLoc(cUnit, mir, 0);
3276 RegLocation rlChar = getSrcLoc(cUnit, mir, 1);
3277
3278 loadValueDirectFixed(cUnit, rlThis, r0);
3279 loadValueDirectFixed(cUnit, rlChar, r1);
3280 if (!singleI) {
3281 RegLocation rlStart = getSrcLoc(cUnit, mir, 2);
3282 loadValueDirectFixed(cUnit, rlStart, r2);
3283 } else {
3284 loadConstant(cUnit, r2, 0);
3285 }
3286 /* Test objects for NULL */
3287 genNullCheck(cUnit, rlThis.sRegLow, r0, mir->offset, NULL);
3288 genDispatchToHandler(cUnit, TEMPLATE_STRING_INDEXOF);
3289 storeValue(cUnit, inlinedTarget(cUnit, mir, false), getReturnLoc(cUnit));
3290 return true;
3291#endif
3292}
3293
3294
3295/*
Ben Chengba4fc8b2009-06-01 13:00:29 -07003296 * NOTE: We assume here that the special native inline routines
3297 * are side-effect free. By making this assumption, we can safely
3298 * re-execute the routine from the interpreter if it decides it
3299 * wants to throw an exception. We still need to EXPORT_PC(), though.
3300 */
3301static bool handleFmt3inline(CompilationUnit *cUnit, MIR *mir)
3302{
3303 DecodedInstruction *dInsn = &mir->dalvikInsn;
3304 switch( mir->dalvikInsn.opCode) {
3305 case OP_EXECUTE_INLINE: {
3306 unsigned int i;
3307 const InlineOperation* inLineTable = dvmGetInlineOpsTable();
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003308 int offset = offsetof(InterpState, retval);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003309 int operation = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003310 int tReg1;
3311 int tReg2;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003312 switch (operation) {
3313 case INLINE_EMPTYINLINEMETHOD:
3314 return false; /* Nop */
3315 case INLINE_STRING_LENGTH:
3316 return genInlinedStringLength(cUnit, mir);
3317 case INLINE_MATH_ABS_INT:
3318 return genInlinedAbsInt(cUnit, mir);
3319 case INLINE_MATH_ABS_LONG:
3320 return genInlinedAbsLong(cUnit, mir);
3321 case INLINE_MATH_MIN_INT:
3322 return genInlinedMinMaxInt(cUnit, mir, true);
3323 case INLINE_MATH_MAX_INT:
3324 return genInlinedMinMaxInt(cUnit, mir, false);
3325 case INLINE_STRING_CHARAT:
3326 return genInlinedStringCharAt(cUnit, mir);
3327 case INLINE_MATH_SQRT:
3328 if (genInlineSqrt(cUnit, mir))
Bill Buzbee9727c3d2009-08-01 11:32:36 -07003329 return false;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003330 else
3331 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003332 case INLINE_MATH_ABS_FLOAT:
Bill Buzbee1465db52009-09-23 17:17:35 -07003333 if (genInlinedAbsFloat(cUnit, mir))
3334 return false;
3335 else
3336 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003337 case INLINE_MATH_ABS_DOUBLE:
Bill Buzbee1465db52009-09-23 17:17:35 -07003338 if (genInlinedAbsDouble(cUnit, mir))
3339 return false;
3340 else
3341 break;
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003342 case INLINE_STRING_COMPARETO:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003343 if (genInlinedCompareTo(cUnit, mir))
3344 return false;
3345 else
3346 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003347 case INLINE_STRING_INDEXOF_I:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003348 if (genInlinedIndexOf(cUnit, mir, true /* I */))
3349 return false;
3350 else
3351 break;
Bill Buzbee12ba0152009-09-03 14:03:09 -07003352 case INLINE_STRING_INDEXOF_II:
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003353 if (genInlinedIndexOf(cUnit, mir, false /* I */))
3354 return false;
3355 else
3356 break;
3357 case INLINE_STRING_EQUALS:
3358 case INLINE_MATH_COS:
3359 case INLINE_MATH_SIN:
3360 break; /* Handle with C routine */
Bill Buzbee50a6bf22009-07-08 13:08:04 -07003361 default:
3362 dvmAbort();
Ben Chengba4fc8b2009-06-01 13:00:29 -07003363 }
Bill Buzbeefd023aa2009-11-02 09:23:49 -08003364 flushAllRegs(cUnit); /* Send everything to home location */
Bill Buzbee1465db52009-09-23 17:17:35 -07003365 clobberCallRegs(cUnit);
3366 clobberReg(cUnit, r4PC);
3367 clobberReg(cUnit, r7);
3368 opRegRegImm(cUnit, kOpAdd, r4PC, rGLUE, offset);
3369 opImm(cUnit, kOpPush, (1<<r4PC) | (1<<r7));
Ben Chengba4fc8b2009-06-01 13:00:29 -07003370 loadConstant(cUnit, r4PC, (int)inLineTable[operation].func);
Bill Buzbee1465db52009-09-23 17:17:35 -07003371 genExportPC(cUnit, mir);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003372 for (i=0; i < dInsn->vA; i++) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003373 loadValueDirect(cUnit, getSrcLoc(cUnit, mir, i), i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003374 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003375 opReg(cUnit, kOpBlx, r4PC);
3376 opRegImm(cUnit, kOpAdd, r13, 8);
Ben Chenge9695e52009-06-16 16:11:47 -07003377 genZeroCheck(cUnit, r0, mir->offset, NULL);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003378 break;
3379 }
3380 default:
3381 return true;
3382 }
3383 return false;
3384}
3385
Andy McFaddenb0a05412009-11-19 10:23:41 -08003386static bool handleFmt3rinline(CompilationUnit *cUnit, MIR *mir)
3387{
3388 /* For OP_EXECUTE_INLINE_RANGE */
3389 genInterpSingleStep(cUnit, mir);
3390 return false;
3391}
3392
Ben Chengba4fc8b2009-06-01 13:00:29 -07003393static bool handleFmt51l(CompilationUnit *cUnit, MIR *mir)
3394{
Bill Buzbee1465db52009-09-23 17:17:35 -07003395 //TUNING: We're using core regs here - not optimal when target is a double
3396 RegLocation rlDest = getDestLocWide(cUnit, mir, 0, 1);
3397 RegLocation rlResult = evalLoc(cUnit, rlDest, kCoreReg, true);
3398 loadConstantValue(cUnit, rlResult.lowReg,
3399 mir->dalvikInsn.vB_wide & 0xFFFFFFFFUL);
3400 loadConstantValue(cUnit, rlResult.highReg,
3401 (mir->dalvikInsn.vB_wide>>32) & 0xFFFFFFFFUL);
3402 storeValueWide(cUnit, rlDest, rlResult);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003403 return false;
3404}
3405
Ben Chengba4fc8b2009-06-01 13:00:29 -07003406/*
3407 * The following are special processing routines that handle transfer of
3408 * controls between compiled code and the interpreter. Certain VM states like
3409 * Dalvik PC and special-purpose registers are reconstructed here.
3410 */
3411
Ben Cheng1efc9c52009-06-08 18:25:27 -07003412/* Chaining cell for code that may need warmup. */
3413static void handleNormalChainingCell(CompilationUnit *cUnit,
3414 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003415{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003416 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3417 jitToInterpEntries.dvmJitToInterpNormal), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003418 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003419 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3420}
3421
3422/*
Ben Cheng1efc9c52009-06-08 18:25:27 -07003423 * Chaining cell for instructions that immediately following already translated
3424 * code.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003425 */
Ben Cheng1efc9c52009-06-08 18:25:27 -07003426static void handleHotChainingCell(CompilationUnit *cUnit,
3427 unsigned int offset)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003428{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003429 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3430 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003431 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003432 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3433}
3434
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003435#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Jeff Hao97319a82009-08-12 16:57:15 -07003436/* Chaining cell for branches that branch back into the same basic block */
3437static void handleBackwardBranchChainingCell(CompilationUnit *cUnit,
3438 unsigned int offset)
3439{
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003440#if defined(WITH_SELF_VERIFICATION)
Bill Buzbee1465db52009-09-23 17:17:35 -07003441 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Jeff Hao97319a82009-08-12 16:57:15 -07003442 offsetof(InterpState, jitToInterpEntries.dvmJitToBackwardBranch) >> 2);
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003443#else
Bill Buzbee1465db52009-09-23 17:17:35 -07003444 newLIR3(cUnit, kThumbLdrRRI5, r0, rGLUE,
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003445 offsetof(InterpState, jitToInterpEntries.dvmJitToInterpNormal) >> 2);
3446#endif
Bill Buzbee1465db52009-09-23 17:17:35 -07003447 newLIR1(cUnit, kThumbBlxR, r0);
Jeff Hao97319a82009-08-12 16:57:15 -07003448 addWordData(cUnit, (int) (cUnit->method->insns + offset), true);
3449}
3450
3451#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003452/* Chaining cell for monomorphic method invocations. */
Ben Cheng38329f52009-07-07 14:19:20 -07003453static void handleInvokeSingletonChainingCell(CompilationUnit *cUnit,
3454 const Method *callee)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003455{
Bill Buzbee270c1d62009-08-13 16:58:07 -07003456 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3457 jitToInterpEntries.dvmJitToTraceSelect), r0);
Bill Buzbee1465db52009-09-23 17:17:35 -07003458 opReg(cUnit, kOpBlx, r0);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003459 addWordData(cUnit, (int) (callee->insns), true);
3460}
3461
Ben Cheng38329f52009-07-07 14:19:20 -07003462/* Chaining cell for monomorphic method invocations. */
3463static void handleInvokePredictedChainingCell(CompilationUnit *cUnit)
3464{
3465
3466 /* Should not be executed in the initial state */
3467 addWordData(cUnit, PREDICTED_CHAIN_BX_PAIR_INIT, true);
3468 /* To be filled: class */
3469 addWordData(cUnit, PREDICTED_CHAIN_CLAZZ_INIT, true);
3470 /* To be filled: method */
3471 addWordData(cUnit, PREDICTED_CHAIN_METHOD_INIT, true);
3472 /*
3473 * Rechain count. The initial value of 0 here will trigger chaining upon
3474 * the first invocation of this callsite.
3475 */
3476 addWordData(cUnit, PREDICTED_CHAIN_COUNTER_INIT, true);
3477}
3478
Ben Chengba4fc8b2009-06-01 13:00:29 -07003479/* Load the Dalvik PC into r0 and jump to the specified target */
3480static void handlePCReconstruction(CompilationUnit *cUnit,
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003481 ArmLIR *targetLabel)
Ben Chengba4fc8b2009-06-01 13:00:29 -07003482{
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003483 ArmLIR **pcrLabel =
3484 (ArmLIR **) cUnit->pcReconstructionList.elemList;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003485 int numElems = cUnit->pcReconstructionList.numUsed;
3486 int i;
3487 for (i = 0; i < numElems; i++) {
3488 dvmCompilerAppendLIR(cUnit, (LIR *) pcrLabel[i]);
3489 /* r0 = dalvik PC */
3490 loadConstant(cUnit, r0, pcrLabel[i]->operands[0]);
3491 genUnconditionalBranch(cUnit, targetLabel);
3492 }
3493}
3494
Bill Buzbee1465db52009-09-23 17:17:35 -07003495static char *extendedMIROpNames[kMirOpLast - kMirOpFirst] = {
3496 "kMirOpPhi",
3497 "kMirOpNullNRangeUpCheck",
3498 "kMirOpNullNRangeDownCheck",
3499 "kMirOpLowerBound",
3500 "kMirOpPunt",
Ben Cheng4238ec22009-08-24 16:32:22 -07003501};
3502
3503/*
3504 * vA = arrayReg;
3505 * vB = idxReg;
3506 * vC = endConditionReg;
3507 * arg[0] = maxC
3508 * arg[1] = minC
3509 * arg[2] = loopBranchConditionCode
3510 */
3511static void genHoistedChecksForCountUpLoop(CompilationUnit *cUnit, MIR *mir)
3512{
Bill Buzbee1465db52009-09-23 17:17:35 -07003513 /*
3514 * NOTE: these synthesized blocks don't have ssa names assigned
3515 * for Dalvik registers. However, because they dominate the following
3516 * blocks we can simply use the Dalvik name w/ subscript 0 as the
3517 * ssa name.
3518 */
Ben Cheng4238ec22009-08-24 16:32:22 -07003519 DecodedInstruction *dInsn = &mir->dalvikInsn;
3520 const int lenOffset = offsetof(ArrayObject, length);
Ben Cheng4238ec22009-08-24 16:32:22 -07003521 const int maxC = dInsn->arg[0];
3522 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003523 int regLength;
3524 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3525 RegLocation rlIdxEnd = cUnit->regLocation[mir->dalvikInsn.vC];
Ben Cheng4238ec22009-08-24 16:32:22 -07003526
3527 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003528 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3529 rlIdxEnd = loadValue(cUnit, rlIdxEnd, kCoreReg);
3530 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003531 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3532
3533 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003534 regLength = allocTemp(cUnit);
3535 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003536
3537 int delta = maxC;
3538 /*
3539 * If the loop end condition is ">=" instead of ">", then the largest value
3540 * of the index is "endCondition - 1".
3541 */
3542 if (dInsn->arg[2] == OP_IF_GE) {
3543 delta--;
3544 }
3545
3546 if (delta) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003547 int tReg = allocTemp(cUnit);
3548 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxEnd.lowReg, delta);
3549 rlIdxEnd.lowReg = tReg;
3550 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003551 }
3552 /* Punt if "regIdxEnd < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003553 genRegRegCheck(cUnit, kArmCondGe, rlIdxEnd.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003554 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003555}
3556
3557/*
3558 * vA = arrayReg;
3559 * vB = idxReg;
3560 * vC = endConditionReg;
3561 * arg[0] = maxC
3562 * arg[1] = minC
3563 * arg[2] = loopBranchConditionCode
3564 */
3565static void genHoistedChecksForCountDownLoop(CompilationUnit *cUnit, MIR *mir)
3566{
3567 DecodedInstruction *dInsn = &mir->dalvikInsn;
3568 const int lenOffset = offsetof(ArrayObject, length);
Bill Buzbee1465db52009-09-23 17:17:35 -07003569 const int regLength = allocTemp(cUnit);
Ben Cheng4238ec22009-08-24 16:32:22 -07003570 const int maxC = dInsn->arg[0];
3571 const int minC = dInsn->arg[1];
Bill Buzbee1465db52009-09-23 17:17:35 -07003572 RegLocation rlArray = cUnit->regLocation[mir->dalvikInsn.vA];
3573 RegLocation rlIdxInit = cUnit->regLocation[mir->dalvikInsn.vB];
Ben Cheng4238ec22009-08-24 16:32:22 -07003574
3575 /* regArray <- arrayRef */
Bill Buzbee1465db52009-09-23 17:17:35 -07003576 rlArray = loadValue(cUnit, rlArray, kCoreReg);
3577 rlIdxInit = loadValue(cUnit, rlIdxInit, kCoreReg);
3578 genRegImmCheck(cUnit, kArmCondEq, rlArray.lowReg, 0, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003579 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3580
3581 /* regLength <- len(arrayRef) */
Bill Buzbee1465db52009-09-23 17:17:35 -07003582 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLength);
Ben Cheng4238ec22009-08-24 16:32:22 -07003583
3584 if (maxC) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003585 int tReg = allocTemp(cUnit);
3586 opRegRegImm(cUnit, kOpAdd, tReg, rlIdxInit.lowReg, maxC);
3587 rlIdxInit.lowReg = tReg;
3588 freeTemp(cUnit, tReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003589 }
3590
3591 /* Punt if "regIdxInit < len(Array)" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003592 genRegRegCheck(cUnit, kArmCondGe, rlIdxInit.lowReg, regLength, 0,
Ben Cheng0fd31e42009-09-03 14:40:16 -07003593 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003594}
3595
3596/*
3597 * vA = idxReg;
3598 * vB = minC;
3599 */
3600static void genHoistedLowerBoundCheck(CompilationUnit *cUnit, MIR *mir)
3601{
3602 DecodedInstruction *dInsn = &mir->dalvikInsn;
Ben Cheng4238ec22009-08-24 16:32:22 -07003603 const int minC = dInsn->vB;
Bill Buzbee1465db52009-09-23 17:17:35 -07003604 RegLocation rlIdx = cUnit->regLocation[mir->dalvikInsn.vA];
Ben Cheng4238ec22009-08-24 16:32:22 -07003605
3606 /* regIdx <- initial index value */
Bill Buzbee1465db52009-09-23 17:17:35 -07003607 rlIdx = loadValue(cUnit, rlIdx, kCoreReg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003608
3609 /* Punt if "regIdxInit + minC >= 0" is false */
Bill Buzbee1465db52009-09-23 17:17:35 -07003610 genRegImmCheck(cUnit, kArmCondLt, rlIdx.lowReg, -minC, 0,
Ben Cheng4238ec22009-08-24 16:32:22 -07003611 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3612}
3613
3614/* Extended MIR instructions like PHI */
3615static void handleExtendedMIR(CompilationUnit *cUnit, MIR *mir)
3616{
Bill Buzbee1465db52009-09-23 17:17:35 -07003617 int opOffset = mir->dalvikInsn.opCode - kMirOpFirst;
Ben Cheng4238ec22009-08-24 16:32:22 -07003618 char *msg = dvmCompilerNew(strlen(extendedMIROpNames[opOffset]) + 1,
3619 false);
3620 strcpy(msg, extendedMIROpNames[opOffset]);
Bill Buzbee1465db52009-09-23 17:17:35 -07003621 newLIR1(cUnit, kArmPseudoExtended, (int) msg);
Ben Cheng4238ec22009-08-24 16:32:22 -07003622
3623 switch (mir->dalvikInsn.opCode) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003624 case kMirOpPhi: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003625 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003626 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003627 break;
3628 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003629 case kMirOpNullNRangeUpCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003630 genHoistedChecksForCountUpLoop(cUnit, mir);
3631 break;
3632 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003633 case kMirOpNullNRangeDownCheck: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003634 genHoistedChecksForCountDownLoop(cUnit, mir);
3635 break;
3636 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003637 case kMirOpLowerBound: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003638 genHoistedLowerBoundCheck(cUnit, mir);
3639 break;
3640 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003641 case kMirOpPunt: {
Ben Cheng4238ec22009-08-24 16:32:22 -07003642 genUnconditionalBranch(cUnit,
3643 (ArmLIR *) cUnit->loopAnalysis->branchToPCR);
3644 break;
3645 }
3646 default:
3647 break;
3648 }
3649}
3650
3651/*
3652 * Create a PC-reconstruction cell for the starting offset of this trace.
3653 * Since the PCR cell is placed near the end of the compiled code which is
3654 * usually out of range for a conditional branch, we put two branches (one
3655 * branch over to the loop body and one layover branch to the actual PCR) at the
3656 * end of the entry block.
3657 */
3658static void setupLoopEntryBlock(CompilationUnit *cUnit, BasicBlock *entry,
3659 ArmLIR *bodyLabel)
3660{
3661 /* Set up the place holder to reconstruct this Dalvik PC */
3662 ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003663 pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
Ben Cheng4238ec22009-08-24 16:32:22 -07003664 pcrLabel->operands[0] =
3665 (int) (cUnit->method->insns + entry->startOffset);
3666 pcrLabel->operands[1] = entry->startOffset;
3667 /* Insert the place holder to the growable list */
3668 dvmInsertGrowableList(&cUnit->pcReconstructionList, pcrLabel);
3669
3670 /*
3671 * Next, create two branches - one branch over to the loop body and the
3672 * other branch to the PCR cell to punt.
3673 */
3674 ArmLIR *branchToBody = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003675 branchToBody->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003676 branchToBody->generic.target = (LIR *) bodyLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003677 setupResourceMasks(branchToBody);
Ben Cheng4238ec22009-08-24 16:32:22 -07003678 cUnit->loopAnalysis->branchToBody = (LIR *) branchToBody;
3679
3680 ArmLIR *branchToPCR = dvmCompilerNew(sizeof(ArmLIR), true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003681 branchToPCR->opCode = kThumbBUncond;
Ben Cheng4238ec22009-08-24 16:32:22 -07003682 branchToPCR->generic.target = (LIR *) pcrLabel;
Ben Chengdcf3e5d2009-09-11 13:42:05 -07003683 setupResourceMasks(branchToPCR);
Ben Cheng4238ec22009-08-24 16:32:22 -07003684 cUnit->loopAnalysis->branchToPCR = (LIR *) branchToPCR;
3685}
3686
Ben Chengba4fc8b2009-06-01 13:00:29 -07003687void dvmCompilerMIR2LIR(CompilationUnit *cUnit)
3688{
3689 /* Used to hold the labels of each block */
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003690 ArmLIR *labelList =
3691 dvmCompilerNew(sizeof(ArmLIR) * cUnit->numBlocks, true);
Bill Buzbee1465db52009-09-23 17:17:35 -07003692 GrowableList chainingListByType[kChainingCellLast];
Ben Chengba4fc8b2009-06-01 13:00:29 -07003693 int i;
3694
3695 /*
Ben Cheng38329f52009-07-07 14:19:20 -07003696 * Initialize various types chaining lists.
Ben Chengba4fc8b2009-06-01 13:00:29 -07003697 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003698 for (i = 0; i < kChainingCellLast; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003699 dvmInitGrowableList(&chainingListByType[i], 2);
3700 }
3701
3702 BasicBlock **blockList = cUnit->blockList;
3703
Bill Buzbee6e963e12009-06-17 16:56:19 -07003704 if (cUnit->executionCount) {
3705 /*
3706 * Reserve 6 bytes at the beginning of the trace
3707 * +----------------------------+
3708 * | execution count (4 bytes) |
3709 * +----------------------------+
3710 * | chain cell offset (2 bytes)|
3711 * +----------------------------+
3712 * ...and then code to increment the execution
3713 * count:
3714 * mov r0, pc @ move adr of "mov r0,pc" + 4 to r0
3715 * sub r0, #10 @ back up to addr of executionCount
3716 * ldr r1, [r0]
3717 * add r1, #1
3718 * str r1, [r0]
3719 */
Bill Buzbee1465db52009-09-23 17:17:35 -07003720 newLIR1(cUnit, kArm16BitData, 0);
3721 newLIR1(cUnit, kArm16BitData, 0);
Ben Chengcc6600c2009-06-22 14:45:16 -07003722 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003723 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003724 cUnit->headerSize = 6;
Bill Buzbee270c1d62009-08-13 16:58:07 -07003725 /* Thumb instruction used directly here to ensure correct size */
Bill Buzbee1465db52009-09-23 17:17:35 -07003726 newLIR2(cUnit, kThumbMovRR_H2L, r0, rpc);
3727 newLIR2(cUnit, kThumbSubRI8, r0, 10);
3728 newLIR3(cUnit, kThumbLdrRRI5, r1, r0, 0);
3729 newLIR2(cUnit, kThumbAddRI8, r1, 1);
3730 newLIR3(cUnit, kThumbStrRRI5, r1, r0, 0);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003731 } else {
3732 /* Just reserve 2 bytes for the chain cell offset */
Ben Chengcc6600c2009-06-22 14:45:16 -07003733 cUnit->chainCellOffsetLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003734 (LIR *) newLIR1(cUnit, kArm16BitData, CHAIN_CELL_OFFSET_TAG);
Bill Buzbee6e963e12009-06-17 16:56:19 -07003735 cUnit->headerSize = 2;
3736 }
Ben Cheng1efc9c52009-06-08 18:25:27 -07003737
Ben Chengba4fc8b2009-06-01 13:00:29 -07003738 /* Handle the content in each basic block */
3739 for (i = 0; i < cUnit->numBlocks; i++) {
3740 blockList[i]->visited = true;
3741 MIR *mir;
3742
3743 labelList[i].operands[0] = blockList[i]->startOffset;
3744
Bill Buzbee1465db52009-09-23 17:17:35 -07003745 if (blockList[i]->blockType >= kChainingCellLast) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07003746 /*
3747 * Append the label pseudo LIR first. Chaining cells will be handled
3748 * separately afterwards.
3749 */
3750 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[i]);
3751 }
3752
Bill Buzbee1465db52009-09-23 17:17:35 -07003753 if (blockList[i]->blockType == kEntryBlock) {
3754 labelList[i].opCode = ARM_PSEUDO_kEntryBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003755 if (blockList[i]->firstMIRInsn == NULL) {
3756 continue;
3757 } else {
3758 setupLoopEntryBlock(cUnit, blockList[i],
3759 &labelList[blockList[i]->fallThrough->id]);
3760 }
Bill Buzbee1465db52009-09-23 17:17:35 -07003761 } else if (blockList[i]->blockType == kExitBlock) {
3762 labelList[i].opCode = ARM_PSEUDO_kExitBlock;
Ben Cheng4238ec22009-08-24 16:32:22 -07003763 goto gen_fallthrough;
Bill Buzbee1465db52009-09-23 17:17:35 -07003764 } else if (blockList[i]->blockType == kDalvikByteCode) {
3765 labelList[i].opCode = kArmPseudoNormalBlockLabel;
Ben Chenge9695e52009-06-16 16:11:47 -07003766 /* Reset the register state */
Bill Buzbee1465db52009-09-23 17:17:35 -07003767 resetRegPool(cUnit);
3768 clobberAllRegs(cUnit);
3769 resetNullCheckTracker(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003770 } else {
3771 switch (blockList[i]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003772 case kChainingCellNormal:
3773 labelList[i].opCode = ARM_PSEUDO_kChainingCellNormal;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003774 /* handle the codegen later */
3775 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003776 &chainingListByType[kChainingCellNormal], (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003777 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003778 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07003779 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003780 ARM_PSEUDO_kChainingCellInvokeSingleton;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003781 labelList[i].operands[0] =
3782 (int) blockList[i]->containingMethod;
3783 /* handle the codegen later */
3784 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003785 &chainingListByType[kChainingCellInvokeSingleton],
Ben Cheng38329f52009-07-07 14:19:20 -07003786 (void *) i);
3787 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003788 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07003789 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003790 ARM_PSEUDO_kChainingCellInvokePredicted;
Ben Cheng38329f52009-07-07 14:19:20 -07003791 /* handle the codegen later */
3792 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003793 &chainingListByType[kChainingCellInvokePredicted],
Ben Cheng38329f52009-07-07 14:19:20 -07003794 (void *) i);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003795 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003796 case kChainingCellHot:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003797 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003798 ARM_PSEUDO_kChainingCellHot;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003799 /* handle the codegen later */
3800 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003801 &chainingListByType[kChainingCellHot],
Ben Chengba4fc8b2009-06-01 13:00:29 -07003802 (void *) i);
3803 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003804 case kPCReconstruction:
Ben Chengba4fc8b2009-06-01 13:00:29 -07003805 /* Make sure exception handling block is next */
3806 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003807 ARM_PSEUDO_kPCReconstruction_BLOCK_LABEL;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003808 assert (i == cUnit->numBlocks - 2);
3809 handlePCReconstruction(cUnit, &labelList[i+1]);
3810 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07003811 case kExceptionHandling:
3812 labelList[i].opCode = kArmPseudoEHBlockLabel;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003813 if (cUnit->pcReconstructionList.numUsed) {
Bill Buzbee270c1d62009-08-13 16:58:07 -07003814 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
3815 jitToInterpEntries.dvmJitToInterpPunt),
3816 r1);
Bill Buzbee1465db52009-09-23 17:17:35 -07003817 opReg(cUnit, kOpBlx, r1);
Ben Chengba4fc8b2009-06-01 13:00:29 -07003818 }
3819 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07003820#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07003821 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07003822 labelList[i].opCode =
Bill Buzbee1465db52009-09-23 17:17:35 -07003823 ARM_PSEUDO_kChainingCellBackwardBranch;
Jeff Hao97319a82009-08-12 16:57:15 -07003824 /* handle the codegen later */
3825 dvmInsertGrowableList(
Bill Buzbee1465db52009-09-23 17:17:35 -07003826 &chainingListByType[kChainingCellBackwardBranch],
Jeff Hao97319a82009-08-12 16:57:15 -07003827 (void *) i);
3828 break;
3829#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003830 default:
3831 break;
3832 }
3833 continue;
3834 }
Ben Chenge9695e52009-06-16 16:11:47 -07003835
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003836 ArmLIR *headLIR = NULL;
Ben Chenge9695e52009-06-16 16:11:47 -07003837
Ben Chengba4fc8b2009-06-01 13:00:29 -07003838 for (mir = blockList[i]->firstMIRInsn; mir; mir = mir->next) {
Bill Buzbee1465db52009-09-23 17:17:35 -07003839
3840 resetRegPool(cUnit);
3841 if (gDvmJit.disableOpt & (1 << kTrackLiveTemps)) {
3842 clobberAllRegs(cUnit);
3843 }
3844
3845 if (gDvmJit.disableOpt & (1 << kSuppressLoads)) {
3846 resetDefTracking(cUnit);
3847 }
3848
3849 if (mir->dalvikInsn.opCode >= kMirOpFirst) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003850 handleExtendedMIR(cUnit, mir);
3851 continue;
3852 }
3853
Bill Buzbee1465db52009-09-23 17:17:35 -07003854
Ben Chengba4fc8b2009-06-01 13:00:29 -07003855 OpCode dalvikOpCode = mir->dalvikInsn.opCode;
3856 InstructionFormat dalvikFormat =
3857 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
Bill Buzbee89efc3d2009-07-28 11:22:22 -07003858 ArmLIR *boundaryLIR =
Bill Buzbee1465db52009-09-23 17:17:35 -07003859 newLIR2(cUnit, ARM_PSEUDO_kDalvikByteCode_BOUNDARY,
Ben Chengccd6c012009-10-15 14:52:45 -07003860 mir->offset,
3861 (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
3862 );
Ben Cheng4238ec22009-08-24 16:32:22 -07003863 if (mir->ssaRep) {
3864 char *ssaString = dvmCompilerGetSSAString(cUnit, mir->ssaRep);
Bill Buzbee1465db52009-09-23 17:17:35 -07003865 newLIR1(cUnit, kArmPseudoSSARep, (int) ssaString);
Ben Cheng4238ec22009-08-24 16:32:22 -07003866 }
3867
Ben Chenge9695e52009-06-16 16:11:47 -07003868 /* Remember the first LIR for this block */
3869 if (headLIR == NULL) {
3870 headLIR = boundaryLIR;
Ben Chengd7d426a2009-09-22 11:23:36 -07003871 /* Set the first boundaryLIR as a scheduling barrier */
3872 headLIR->defMask = ENCODE_ALL;
Ben Chenge9695e52009-06-16 16:11:47 -07003873 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003874
Ben Chengba4fc8b2009-06-01 13:00:29 -07003875 bool notHandled;
3876 /*
3877 * Debugging: screen the opcode first to see if it is in the
3878 * do[-not]-compile list
3879 */
3880 bool singleStepMe =
3881 gDvmJit.includeSelectedOp !=
3882 ((gDvmJit.opList[dalvikOpCode >> 3] &
3883 (1 << (dalvikOpCode & 0x7))) !=
3884 0);
Jeff Hao97319a82009-08-12 16:57:15 -07003885#if defined(WITH_SELF_VERIFICATION)
3886 /* Punt on opcodes we can't replay */
3887 if (selfVerificationPuntOps(dalvikOpCode))
3888 singleStepMe = true;
3889#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07003890 if (singleStepMe || cUnit->allSingleStep) {
3891 notHandled = false;
3892 genInterpSingleStep(cUnit, mir);
3893 } else {
3894 opcodeCoverage[dalvikOpCode]++;
3895 switch (dalvikFormat) {
3896 case kFmt10t:
3897 case kFmt20t:
3898 case kFmt30t:
3899 notHandled = handleFmt10t_Fmt20t_Fmt30t(cUnit,
3900 mir, blockList[i], labelList);
3901 break;
3902 case kFmt10x:
3903 notHandled = handleFmt10x(cUnit, mir);
3904 break;
3905 case kFmt11n:
3906 case kFmt31i:
3907 notHandled = handleFmt11n_Fmt31i(cUnit, mir);
3908 break;
3909 case kFmt11x:
3910 notHandled = handleFmt11x(cUnit, mir);
3911 break;
3912 case kFmt12x:
3913 notHandled = handleFmt12x(cUnit, mir);
3914 break;
3915 case kFmt20bc:
3916 notHandled = handleFmt20bc(cUnit, mir);
3917 break;
3918 case kFmt21c:
3919 case kFmt31c:
3920 notHandled = handleFmt21c_Fmt31c(cUnit, mir);
3921 break;
3922 case kFmt21h:
3923 notHandled = handleFmt21h(cUnit, mir);
3924 break;
3925 case kFmt21s:
3926 notHandled = handleFmt21s(cUnit, mir);
3927 break;
3928 case kFmt21t:
3929 notHandled = handleFmt21t(cUnit, mir, blockList[i],
3930 labelList);
3931 break;
3932 case kFmt22b:
3933 case kFmt22s:
3934 notHandled = handleFmt22b_Fmt22s(cUnit, mir);
3935 break;
3936 case kFmt22c:
3937 notHandled = handleFmt22c(cUnit, mir);
3938 break;
3939 case kFmt22cs:
3940 notHandled = handleFmt22cs(cUnit, mir);
3941 break;
3942 case kFmt22t:
3943 notHandled = handleFmt22t(cUnit, mir, blockList[i],
3944 labelList);
3945 break;
3946 case kFmt22x:
3947 case kFmt32x:
3948 notHandled = handleFmt22x_Fmt32x(cUnit, mir);
3949 break;
3950 case kFmt23x:
3951 notHandled = handleFmt23x(cUnit, mir);
3952 break;
3953 case kFmt31t:
3954 notHandled = handleFmt31t(cUnit, mir);
3955 break;
3956 case kFmt3rc:
3957 case kFmt35c:
3958 notHandled = handleFmt35c_3rc(cUnit, mir, blockList[i],
3959 labelList);
3960 break;
3961 case kFmt3rms:
3962 case kFmt35ms:
3963 notHandled = handleFmt35ms_3rms(cUnit, mir,blockList[i],
3964 labelList);
3965 break;
3966 case kFmt3inline:
3967 notHandled = handleFmt3inline(cUnit, mir);
3968 break;
Andy McFaddenb0a05412009-11-19 10:23:41 -08003969 case kFmt3rinline:
3970 notHandled = handleFmt3rinline(cUnit, mir);
3971 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003972 case kFmt51l:
3973 notHandled = handleFmt51l(cUnit, mir);
3974 break;
3975 default:
3976 notHandled = true;
3977 break;
3978 }
3979 }
3980 if (notHandled) {
3981 LOGE("%#06x: Opcode 0x%x (%s) / Fmt %d not handled\n",
3982 mir->offset,
3983 dalvikOpCode, getOpcodeName(dalvikOpCode),
3984 dalvikFormat);
3985 dvmAbort();
3986 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -07003987 }
3988 }
Ben Cheng4238ec22009-08-24 16:32:22 -07003989
Bill Buzbee1465db52009-09-23 17:17:35 -07003990 if (blockList[i]->blockType == kEntryBlock) {
Ben Cheng4238ec22009-08-24 16:32:22 -07003991 dvmCompilerAppendLIR(cUnit,
3992 (LIR *) cUnit->loopAnalysis->branchToBody);
3993 dvmCompilerAppendLIR(cUnit,
3994 (LIR *) cUnit->loopAnalysis->branchToPCR);
3995 }
3996
3997 if (headLIR) {
3998 /*
3999 * Eliminate redundant loads/stores and delay stores into later
4000 * slots
4001 */
4002 dvmCompilerApplyLocalOptimizations(cUnit, (LIR *) headLIR,
4003 cUnit->lastLIRInsn);
4004 }
4005
4006gen_fallthrough:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004007 /*
4008 * Check if the block is terminated due to trace length constraint -
4009 * insert an unconditional branch to the chaining cell.
4010 */
4011 if (blockList[i]->needFallThroughBranch) {
4012 genUnconditionalBranch(cUnit,
4013 &labelList[blockList[i]->fallThrough->id]);
4014 }
4015
Ben Chengba4fc8b2009-06-01 13:00:29 -07004016 }
4017
Ben Chenge9695e52009-06-16 16:11:47 -07004018 /* Handle the chaining cells in predefined order */
Bill Buzbee1465db52009-09-23 17:17:35 -07004019 for (i = 0; i < kChainingCellLast; i++) {
Ben Chengba4fc8b2009-06-01 13:00:29 -07004020 size_t j;
4021 int *blockIdList = (int *) chainingListByType[i].elemList;
4022
4023 cUnit->numChainingCells[i] = chainingListByType[i].numUsed;
4024
4025 /* No chaining cells of this type */
4026 if (cUnit->numChainingCells[i] == 0)
4027 continue;
4028
4029 /* Record the first LIR for a new type of chaining cell */
4030 cUnit->firstChainingLIR[i] = (LIR *) &labelList[blockIdList[0]];
4031
4032 for (j = 0; j < chainingListByType[i].numUsed; j++) {
4033 int blockId = blockIdList[j];
4034
4035 /* Align this chaining cell first */
Bill Buzbee1465db52009-09-23 17:17:35 -07004036 newLIR0(cUnit, kArmPseudoPseudoAlign4);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004037
4038 /* Insert the pseudo chaining instruction */
4039 dvmCompilerAppendLIR(cUnit, (LIR *) &labelList[blockId]);
4040
4041
4042 switch (blockList[blockId]->blockType) {
Bill Buzbee1465db52009-09-23 17:17:35 -07004043 case kChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004044 handleNormalChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004045 blockList[blockId]->startOffset);
4046 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004047 case kChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -07004048 handleInvokeSingletonChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004049 blockList[blockId]->containingMethod);
4050 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004051 case kChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -07004052 handleInvokePredictedChainingCell(cUnit);
4053 break;
Bill Buzbee1465db52009-09-23 17:17:35 -07004054 case kChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -07004055 handleHotChainingCell(cUnit,
Ben Chengba4fc8b2009-06-01 13:00:29 -07004056 blockList[blockId]->startOffset);
4057 break;
Bill Buzbee9c4b7c82009-09-10 10:10:38 -07004058#if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
Bill Buzbee1465db52009-09-23 17:17:35 -07004059 case kChainingCellBackwardBranch:
Jeff Hao97319a82009-08-12 16:57:15 -07004060 handleBackwardBranchChainingCell(cUnit,
4061 blockList[blockId]->startOffset);
4062 break;
4063#endif
Ben Chengba4fc8b2009-06-01 13:00:29 -07004064 default:
Bill Buzbee1465db52009-09-23 17:17:35 -07004065 LOGE("Bad blocktype %d", blockList[blockId]->blockType);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004066 dvmAbort();
4067 break;
4068 }
4069 }
4070 }
Ben Chenge9695e52009-06-16 16:11:47 -07004071
Ben Cheng6c10a972009-10-29 14:39:18 -07004072 /*
4073 * Generate the branch to the dvmJitToInterpNoChain entry point at the end
4074 * of all chaining cells for the overflow cases.
4075 */
4076 if (cUnit->switchOverflowPad) {
4077 loadConstant(cUnit, r0, (int) cUnit->switchOverflowPad);
4078 loadWordDisp(cUnit, rGLUE, offsetof(InterpState,
4079 jitToInterpEntries.dvmJitToInterpNoChain), r2);
4080 opRegReg(cUnit, kOpAdd, r1, r1);
4081 opRegRegReg(cUnit, kOpAdd, r4PC, r0, r1);
4082#if defined(EXIT_STATS)
4083 loadConstant(cUnit, r0, kSwitchOverflow);
4084#endif
4085 opReg(cUnit, kOpBlx, r2);
4086 }
4087
Ben Chenge9695e52009-06-16 16:11:47 -07004088 dvmCompilerApplyGlobalOptimizations(cUnit);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004089}
4090
4091/* Accept the work and start compiling */
Bill Buzbee716f1202009-07-23 13:22:09 -07004092bool dvmCompilerDoWork(CompilerWorkOrder *work)
Ben Chengba4fc8b2009-06-01 13:00:29 -07004093{
Ben Chengccd6c012009-10-15 14:52:45 -07004094 bool res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004095
Ben Chengccd6c012009-10-15 14:52:45 -07004096 if (gDvmJit.codeCacheFull) {
4097 return false;
4098 }
Ben Chengba4fc8b2009-06-01 13:00:29 -07004099
Ben Chengccd6c012009-10-15 14:52:45 -07004100 switch (work->kind) {
4101 case kWorkOrderMethod:
4102 res = dvmCompileMethod(work->info, &work->result);
4103 break;
4104 case kWorkOrderTrace:
4105 /* Start compilation with maximally allowed trace length */
4106 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4107 break;
4108 case kWorkOrderTraceDebug: {
4109 bool oldPrintMe = gDvmJit.printMe;
4110 gDvmJit.printMe = true;
4111 /* Start compilation with maximally allowed trace length */
4112 res = dvmCompileTrace(work->info, JIT_MAX_TRACE_LEN, &work->result);
4113 gDvmJit.printMe = oldPrintMe;;
4114 break;
4115 }
4116 default:
4117 res = false;
4118 dvmAbort();
4119 }
4120 return res;
Ben Chengba4fc8b2009-06-01 13:00:29 -07004121}
4122
Ben Chengba4fc8b2009-06-01 13:00:29 -07004123/* Architectural-specific debugging helpers go here */
4124void dvmCompilerArchDump(void)
4125{
4126 /* Print compiled opcode in this VM instance */
4127 int i, start, streak;
4128 char buf[1024];
4129
4130 streak = i = 0;
4131 buf[0] = 0;
4132 while (opcodeCoverage[i] == 0 && i < 256) {
4133 i++;
4134 }
4135 if (i == 256) {
4136 return;
4137 }
4138 for (start = i++, streak = 1; i < 256; i++) {
4139 if (opcodeCoverage[i]) {
4140 streak++;
4141 } else {
4142 if (streak == 1) {
4143 sprintf(buf+strlen(buf), "%x,", start);
4144 } else {
4145 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
4146 }
4147 streak = 0;
4148 while (opcodeCoverage[i] == 0 && i < 256) {
4149 i++;
4150 }
4151 if (i < 256) {
4152 streak = 1;
4153 start = i;
4154 }
4155 }
4156 }
4157 if (streak) {
4158 if (streak == 1) {
4159 sprintf(buf+strlen(buf), "%x", start);
4160 } else {
4161 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
4162 }
4163 }
4164 if (strlen(buf)) {
Ben Cheng8b258bf2009-06-24 17:27:07 -07004165 LOGD("dalvik.vm.jit.op = %s", buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -07004166 }
4167}
Ben Chengd7d426a2009-09-22 11:23:36 -07004168
4169/* Common initialization routine for an architecture family */
4170bool dvmCompilerArchInit()
4171{
4172 int i;
4173
Bill Buzbee1465db52009-09-23 17:17:35 -07004174 for (i = 0; i < kArmLast; i++) {
Ben Chengd7d426a2009-09-22 11:23:36 -07004175 if (EncodingMap[i].opCode != i) {
4176 LOGE("Encoding order for %s is wrong: expecting %d, seeing %d",
4177 EncodingMap[i].name, i, EncodingMap[i].opCode);
4178 dvmAbort();
4179 }
4180 }
4181
Ben Cheng5d90c202009-11-22 23:31:11 -08004182 return dvmCompilerArchVariantInit();
4183}
4184
4185void *dvmCompilerGetInterpretTemplate()
4186{
4187 return (void*) ((int)gDvmJit.codeCache +
4188 templateEntryOffsets[TEMPLATE_INTERPRET]);
4189}
4190
4191/* Needed by the ld/st optmizatons */
4192ArmLIR* dvmCompilerRegCopyNoInsert(CompilationUnit *cUnit, int rDest, int rSrc)
4193{
4194 return genRegCopyNoInsert(cUnit, rDest, rSrc);
4195}
4196
4197/* Needed by the register allocator */
4198ArmLIR* dvmCompilerRegCopy(CompilationUnit *cUnit, int rDest, int rSrc)
4199{
4200 return genRegCopy(cUnit, rDest, rSrc);
4201}
4202
4203/* Needed by the register allocator */
4204void dvmCompilerRegCopyWide(CompilationUnit *cUnit, int destLo, int destHi,
4205 int srcLo, int srcHi)
4206{
4207 genRegCopyWide(cUnit, destLo, destHi, srcLo, srcHi);
4208}
4209
4210void dvmCompilerFlushRegImpl(CompilationUnit *cUnit, int rBase,
4211 int displacement, int rSrc, OpSize size)
4212{
4213 storeBaseDisp(cUnit, rBase, displacement, rSrc, size);
4214}
4215
4216void dvmCompilerFlushRegWideImpl(CompilationUnit *cUnit, int rBase,
4217 int displacement, int rSrcLo, int rSrcHi)
4218{
4219 storeBaseDispWide(cUnit, rBase, displacement, rSrcLo, rSrcHi);
Ben Chengd7d426a2009-09-22 11:23:36 -07004220}