blob: 9ea9e705d120ccefe82567fee1e3ead68d9d09ca [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 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
17/*
18 * This file contains codegen for the Thumb2 ISA and is intended to be
19 * includes by:
20 *
21 * Codegen-$(TARGET_ARCH_VARIANT).c
22 *
23 */
24
25/*
26 * Construct an s4 from two consecutive half-words of switch data.
27 * This needs to check endianness because the DEX optimizer only swaps
28 * half-words in instruction stream.
29 *
30 * "switchData" must be 32-bit aligned.
31 */
32#if __BYTE_ORDER == __LITTLE_ENDIAN
33static inline s4 s4FromSwitchData(const void* switchData) {
34 return *(s4*) switchData;
35}
36#else
37static inline s4 s4FromSwitchData(const void* switchData) {
38 u2* data = switchData;
39 return data[0] | (((s4) data[1]) << 16);
40}
41#endif
42
buzbee1b4c8592011-08-31 10:43:51 -070043/* Generate unconditional branch instructions */
44static ArmLIR* genUnconditionalBranch(CompilationUnit* cUnit, ArmLIR* target)
45{
46 ArmLIR* branch = opNone(cUnit, kOpUncondBr);
47 branch->generic.target = (LIR*) target;
48 return branch;
49}
50
buzbee67bf8852011-08-17 17:51:35 -070051/*
52 * Generate a Thumb2 IT instruction, which can nullify up to
53 * four subsequent instructions based on a condition and its
54 * inverse. The condition applies to the first instruction, which
55 * is executed if the condition is met. The string "guide" consists
56 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
57 * A "T" means the instruction is executed if the condition is
58 * met, and an "E" means the instruction is executed if the condition
59 * is not met.
60 */
61static ArmLIR* genIT(CompilationUnit* cUnit, ArmConditionCode code,
62 const char* guide)
63{
64 int mask;
65 int condBit = code & 1;
66 int altBit = condBit ^ 1;
67 int mask3 = 0;
68 int mask2 = 0;
69 int mask1 = 0;
70
71 //Note: case fallthroughs intentional
72 switch(strlen(guide)) {
73 case 3:
74 mask1 = (guide[2] == 'T') ? condBit : altBit;
75 case 2:
76 mask2 = (guide[1] == 'T') ? condBit : altBit;
77 case 1:
78 mask3 = (guide[0] == 'T') ? condBit : altBit;
79 break;
80 case 0:
81 break;
82 default:
83 LOG(FATAL) << "OAT: bad case in genIT";
84 }
85 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
86 (1 << (3 - strlen(guide)));
87 return newLIR2(cUnit, kThumb2It, code, mask);
88}
89
90/*
91 * Insert a kArmPseudoCaseLabel at the beginning of the Dalvik
92 * offset vaddr. This label will be used to fix up the case
93 * branch table during the assembly phase. Be sure to set
94 * all resource flags on this to prevent code motion across
95 * target boundaries. KeyVal is just there for debugging.
96 */
97static ArmLIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
98{
99 ArmLIR* lir;
100 for (lir = (ArmLIR*)cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
101 if ((lir->opcode == kArmPseudoDalvikByteCodeBoundary) &&
102 (lir->generic.dalvikOffset == vaddr)) {
103 ArmLIR* newLabel = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
104 newLabel->generic.dalvikOffset = vaddr;
105 newLabel->opcode = kArmPseudoCaseLabel;
106 newLabel->operands[0] = keyVal;
107 oatInsertLIRAfter((LIR*)lir, (LIR*)newLabel);
108 return newLabel;
109 }
110 }
111 oatCodegenDump(cUnit);
112 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
113 return NULL; // Quiet gcc
114}
115
116static void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
117{
118 const u2* table = tabRec->table;
119 int baseVaddr = tabRec->vaddr;
120 int *targets = (int*)&table[4];
121 int entries = table[1];
122 int lowKey = s4FromSwitchData(&table[2]);
123 for (int i = 0; i < entries; i++) {
124 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
125 i + lowKey);
126 }
127}
128
129static void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
130{
131 const u2* table = tabRec->table;
132 int baseVaddr = tabRec->vaddr;
133 int entries = table[1];
134 int* keys = (int*)&table[2];
135 int* targets = &keys[entries];
136 for (int i = 0; i < entries; i++) {
137 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
138 keys[i]);
139 }
140}
141
142void oatProcessSwitchTables(CompilationUnit* cUnit)
143{
144 GrowableListIterator iterator;
145 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
146 while (true) {
147 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
148 &iterator);
149 if (tabRec == NULL) break;
150 if (tabRec->table[0] == kPackedSwitchSignature)
151 markPackedCaseLabels(cUnit, tabRec);
152 else if (tabRec->table[0] == kSparseSwitchSignature)
153 markSparseCaseLabels(cUnit, tabRec);
154 else {
155 LOG(FATAL) << "Invalid switch table";
156 }
157 }
158}
159
160static void dumpSparseSwitchTable(const u2* table)
161 /*
162 * Sparse switch data format:
163 * ushort ident = 0x0200 magic value
164 * ushort size number of entries in the table; > 0
165 * int keys[size] keys, sorted low-to-high; 32-bit aligned
166 * int targets[size] branch targets, relative to switch opcode
167 *
168 * Total size is (2+size*4) 16-bit code units.
169 */
170{
171 u2 ident = table[0];
172 int entries = table[1];
173 int* keys = (int*)&table[2];
174 int* targets = &keys[entries];
175 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
176 ", entries: " << std::dec << entries;
177 for (int i = 0; i < entries; i++) {
178 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
179 targets[i];
180 }
181}
182
183static void dumpPackedSwitchTable(const u2* table)
184 /*
185 * Packed switch data format:
186 * ushort ident = 0x0100 magic value
187 * ushort size number of entries in the table
188 * int first_key first (and lowest) switch case value
189 * int targets[size] branch targets, relative to switch opcode
190 *
191 * Total size is (4+size*2) 16-bit code units.
192 */
193{
194 u2 ident = table[0];
195 int* targets = (int*)&table[4];
196 int entries = table[1];
197 int lowKey = s4FromSwitchData(&table[2]);
198 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
199 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
200 for (int i = 0; i < entries; i++) {
201 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
202 targets[i];
203 }
204}
205
206/*
207 * The sparse table in the literal pool is an array of <key,displacement>
208 * pairs. For each set, we'll load them as a pair using ldmia.
209 * This means that the register number of the temp we use for the key
210 * must be lower than the reg for the displacement.
211 *
212 * The test loop will look something like:
213 *
214 * adr rBase, <table>
215 * ldr rVal, [rSP, vRegOff]
216 * mov rIdx, #tableSize
217 * lp:
218 * ldmia rBase!, {rKey, rDisp}
219 * sub rIdx, #1
220 * cmp rVal, rKey
221 * ifeq
222 * add rPC, rDisp ; This is the branch from which we compute displacement
223 * cbnz rIdx, lp
224 */
225static void genSparseSwitch(CompilationUnit* cUnit, MIR* mir,
226 RegLocation rlSrc)
227{
228 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
229 if (cUnit->printMe) {
230 dumpSparseSwitchTable(table);
231 }
232 // Add the table to the list - we'll process it later
233 SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable),
234 true);
235 tabRec->table = table;
236 tabRec->vaddr = mir->offset;
237 int size = table[1];
238 tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true);
239 oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec);
240
241 // Get the switch value
242 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
243 int rBase = oatAllocTemp(cUnit);
244 /* Allocate key and disp temps */
245 int rKey = oatAllocTemp(cUnit);
246 int rDisp = oatAllocTemp(cUnit);
247 // Make sure rKey's register number is less than rDisp's number for ldmia
248 if (rKey > rDisp) {
249 int tmp = rDisp;
250 rDisp = rKey;
251 rKey = tmp;
252 }
253 // Materialize a pointer to the switch table
254 newLIR3(cUnit, kThumb2AdrST, rBase, 0, (intptr_t)tabRec);
255 // Set up rIdx
256 int rIdx = oatAllocTemp(cUnit);
257 loadConstant(cUnit, rIdx, size);
258 // Establish loop branch target
259 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
260 target->defMask = ENCODE_ALL;
261 // Load next key/disp
262 newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp));
263 opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg);
264 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
265 genIT(cUnit, kArmCondEq, "");
266 ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp);
267 tabRec->bxInst = switchBranch;
268 // Needs to use setflags encoding here
269 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
270 ArmLIR* branch = opCondBranch(cUnit, kArmCondNe);
271 branch->generic.target = (LIR*)target;
272}
273
274
275static void genPackedSwitch(CompilationUnit* cUnit, MIR* mir,
276 RegLocation rlSrc)
277{
278 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
279 if (cUnit->printMe) {
280 dumpPackedSwitchTable(table);
281 }
282 // Add the table to the list - we'll process it later
283 SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable),
284 true);
285 tabRec->table = table;
286 tabRec->vaddr = mir->offset;
287 int size = table[1];
288 tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true);
289 oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec);
290
291 // Get the switch value
292 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
293 int tableBase = oatAllocTemp(cUnit);
294 // Materialize a pointer to the switch table
295 newLIR3(cUnit, kThumb2AdrST, tableBase, 0, (intptr_t)tabRec);
296 int lowKey = s4FromSwitchData(&table[2]);
297 int keyReg;
298 // Remove the bias, if necessary
299 if (lowKey == 0) {
300 keyReg = rlSrc.lowReg;
301 } else {
302 keyReg = oatAllocTemp(cUnit);
303 opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
304 }
305 // Bounds check - if < 0 or >= size continue following switch
306 opRegImm(cUnit, kOpCmp, keyReg, size-1);
307 ArmLIR* branchOver = opCondBranch(cUnit, kArmCondHi);
308
309 // Load the displacement from the switch table
310 int dispReg = oatAllocTemp(cUnit);
311 loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord);
312
313 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
314 ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg);
315 tabRec->bxInst = switchBranch;
316
317 /* branchOver target here */
318 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
319 target->defMask = ENCODE_ALL;
320 branchOver->generic.target = (LIR*)target;
321}
322
323/*
324 * Array data table format:
325 * ushort ident = 0x0300 magic value
326 * ushort width width of each element in the table
327 * uint size number of elements in the table
328 * ubyte data[size*width] table of data values (may contain a single-byte
329 * padding at the end)
330 *
331 * Total size is 4+(width * size + 1)/2 16-bit code units.
332 */
333static void genFillArrayData(CompilationUnit* cUnit, MIR* mir,
334 RegLocation rlSrc)
335{
336 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
337 // Add the table to the list - we'll process it later
338 FillArrayData *tabRec = (FillArrayData *)
339 oatNew(sizeof(FillArrayData), true);
340 tabRec->table = table;
341 tabRec->vaddr = mir->offset;
342 u2 width = tabRec->table[1];
343 u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16);
344 tabRec->size = (size * width) + 8;
345
346 oatInsertGrowableList(&cUnit->fillArrayData, (intptr_t)tabRec);
347
348 // Making a call - use explicit registers
349 oatFlushAllRegs(cUnit); /* Everything to home location */
350 loadValueDirectFixed(cUnit, rlSrc, r0);
351 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700352 OFFSETOF_MEMBER(Thread, pHandleFillArrayDataFromCode), rLR);
buzbeee6d61962011-08-27 11:58:19 -0700353 // Materialize a pointer to the fill data image
buzbee67bf8852011-08-17 17:51:35 -0700354 newLIR3(cUnit, kThumb2AdrST, r1, 0, (intptr_t)tabRec);
355 opReg(cUnit, kOpBlx, rLR);
356 oatClobberCallRegs(cUnit);
357}
358
359/*
360 * Mark garbage collection card. Skip if the value we're storing is null.
361 */
362static void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg)
363{
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700364#if 1
365 UNIMPLEMENTED(WARNING);
366#else
buzbee67bf8852011-08-17 17:51:35 -0700367 int regCardBase = oatAllocTemp(cUnit);
368 int regCardNo = oatAllocTemp(cUnit);
369 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
buzbeec143c552011-08-20 17:38:58 -0700370 loadWordDisp(cUnit, rSELF, Thread::CardTableOffset().Int32Value(),
buzbee67bf8852011-08-17 17:51:35 -0700371 regCardBase);
372 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
373 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
374 kUnsignedByte);
375 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
376 target->defMask = ENCODE_ALL;
377 branchOver->generic.target = (LIR*)target;
378 oatFreeTemp(cUnit, regCardBase);
379 oatFreeTemp(cUnit, regCardNo);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700380#endif
buzbee67bf8852011-08-17 17:51:35 -0700381}
382
383static void genIGetX(CompilationUnit* cUnit, MIR* mir, OpSize size,
384 RegLocation rlDest, RegLocation rlObj)
385{
buzbeec143c552011-08-20 17:38:58 -0700386 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
387 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700388 if (fieldPtr == NULL) {
buzbeedd3efae2011-08-28 14:39:07 -0700389 UNIMPLEMENTED(FATAL) << "Need to handle unresolved field";
buzbee67bf8852011-08-17 17:51:35 -0700390 }
391#if ANDROID_SMP != 0
392 bool isVolatile = dvmIsVolatileField(fieldPtr);
393#else
394 bool isVolatile = false;
395#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700396 int fieldOffset = fieldPtr->GetOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700397 RegLocation rlResult;
398 RegisterClass regClass = oatRegClassBySize(size);
399 rlObj = loadValue(cUnit, rlObj, kCoreReg);
400 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
401 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
402 NULL);/* null object? */
403 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
404 size, rlObj.sRegLow);
405 if (isVolatile) {
406 oatGenMemBarrier(cUnit, kSY);
407 }
408
409 storeValue(cUnit, rlDest, rlResult);
410}
411
412static void genIPutX(CompilationUnit* cUnit, MIR* mir, OpSize size,
413 RegLocation rlSrc, RegLocation rlObj, bool isObject)
414{
buzbeec143c552011-08-20 17:38:58 -0700415 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
416 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700417 if (fieldPtr == NULL) {
buzbeedd3efae2011-08-28 14:39:07 -0700418 UNIMPLEMENTED(FATAL) << "Need to handle unresolved field";
buzbee67bf8852011-08-17 17:51:35 -0700419 }
420#if ANDROID_SMP != 0
421 bool isVolatile = dvmIsVolatileField(fieldPtr);
422#else
423 bool isVolatile = false;
424#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700425 int fieldOffset = fieldPtr->GetOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700426 RegisterClass regClass = oatRegClassBySize(size);
427 rlObj = loadValue(cUnit, rlObj, kCoreReg);
428 rlSrc = loadValue(cUnit, rlSrc, regClass);
429 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
430 NULL);/* null object? */
431
432 if (isVolatile) {
433 oatGenMemBarrier(cUnit, kSY);
434 }
435 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
436 if (isObject) {
437 /* NOTE: marking card based on object head */
438 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
439 }
440}
441
442static void genIGetWideX(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
443 RegLocation rlObj)
444{
buzbeec143c552011-08-20 17:38:58 -0700445 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
446 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700447 if (fieldPtr == NULL) {
buzbeedd3efae2011-08-28 14:39:07 -0700448 UNIMPLEMENTED(FATAL) << "Need to handle unresolved field";
buzbee67bf8852011-08-17 17:51:35 -0700449 }
450#if ANDROID_SMP != 0
451 bool isVolatile = dvmIsVolatileField(fieldPtr);
452#else
453 bool isVolatile = false;
454#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700455 int fieldOffset = fieldPtr->GetOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700456 RegLocation rlResult;
457 rlObj = loadValue(cUnit, rlObj, kCoreReg);
458 int regPtr = oatAllocTemp(cUnit);
459
460 assert(rlDest.wide);
461
462 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
463 NULL);/* null object? */
464 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
465 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
466
467 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
468
469 if (isVolatile) {
470 oatGenMemBarrier(cUnit, kSY);
471 }
472
473 oatFreeTemp(cUnit, regPtr);
474 storeValueWide(cUnit, rlDest, rlResult);
475}
476
477static void genIPutWideX(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
478 RegLocation rlObj)
479{
buzbeec143c552011-08-20 17:38:58 -0700480 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
481 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700482 if (fieldPtr == NULL) {
buzbeedd3efae2011-08-28 14:39:07 -0700483 UNIMPLEMENTED(FATAL) << "Need to handle unresolved field";
buzbee67bf8852011-08-17 17:51:35 -0700484 }
485#if ANDROID_SMP != 0
486 bool isVolatile = dvmIsVolatileField(fieldPtr);
487#else
488 bool isVolatile = false;
489#endif
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700490 int fieldOffset = fieldPtr->GetOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700491
492 rlObj = loadValue(cUnit, rlObj, kCoreReg);
493 int regPtr;
494 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
495 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir->offset,
496 NULL);/* null object? */
497 regPtr = oatAllocTemp(cUnit);
498 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
499
500 if (isVolatile) {
501 oatGenMemBarrier(cUnit, kSY);
502 }
503 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
504
505 oatFreeTemp(cUnit, regPtr);
506}
507
508static void genConstClass(CompilationUnit* cUnit, MIR* mir,
509 RegLocation rlDest, RegLocation rlSrc)
510{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700511 art::Class* classPtr = cUnit->method->GetDexCacheResolvedTypes()->
buzbee1b4c8592011-08-31 10:43:51 -0700512 Get(mir->dalvikInsn.vB);
513 int mReg = loadCurrMethod(cUnit);
514 int resReg = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700515 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700516 loadWordDisp(cUnit, mReg, Method::DexCacheStringsOffset().Int32Value(),
buzbee1b4c8592011-08-31 10:43:51 -0700517 resReg);
518 loadWordDisp(cUnit, resReg, Array::DataOffset().Int32Value() +
519 (sizeof(String*) * mir->dalvikInsn.vB), rlResult.lowReg);
520 if (classPtr != NULL) {
521 // Fast path, we're done - just store result
522 storeValue(cUnit, rlDest, rlResult);
523 } else {
524 // Slow path. Must test at runtime
525 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, rlResult.lowReg,
526 0);
527 // Resolved, store and hop over following code
528 storeValue(cUnit, rlDest, rlResult);
529 ArmLIR* branch2 = genUnconditionalBranch(cUnit,0);
530 // TUNING: move slow path to end & remove unconditional branch
531 ArmLIR* target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
532 target1->defMask = ENCODE_ALL;
533 // Call out to helper, which will return resolved type in r0
534 loadWordDisp(cUnit, rSELF,
535 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
536 genRegCopy(cUnit, r1, mReg);
537 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
538 opReg(cUnit, kOpBlx, rLR); // resolveTypeFromCode(idx, method)
539 oatClobberCallRegs(cUnit);
540 RegLocation rlResult = oatGetReturn(cUnit);
541 storeValue(cUnit, rlDest, rlResult);
542 // Rejoin code paths
543 ArmLIR* target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
544 target2->defMask = ENCODE_ALL;
545 branch1->generic.target = (LIR*)target1;
546 branch2->generic.target = (LIR*)target2;
547 }
buzbee67bf8852011-08-17 17:51:35 -0700548}
549
550static void genConstString(CompilationUnit* cUnit, MIR* mir,
551 RegLocation rlDest, RegLocation rlSrc)
552{
buzbee1b4c8592011-08-31 10:43:51 -0700553 /* All strings should be available at compile time */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700554 const art::String* str = cUnit->method->GetDexCacheStrings()->
buzbee1b4c8592011-08-31 10:43:51 -0700555 Get(mir->dalvikInsn.vB);
556 DCHECK(str != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700557
buzbee1b4c8592011-08-31 10:43:51 -0700558 int mReg = loadCurrMethod(cUnit);
559 int resReg = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700560 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700561 loadWordDisp(cUnit, mReg, Method::DexCacheStringsOffset().Int32Value(),
buzbee1b4c8592011-08-31 10:43:51 -0700562 resReg);
563 loadWordDisp(cUnit, resReg, Array::DataOffset().Int32Value() +
564 (sizeof(String*) * mir->dalvikInsn.vB), rlResult.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700565 storeValue(cUnit, rlDest, rlResult);
566}
567
buzbeedfd3d702011-08-28 12:56:51 -0700568/*
569 * Let helper function take care of everything. Will
570 * call Class::NewInstanceFromCode(type_idx, method);
571 */
buzbee67bf8852011-08-17 17:51:35 -0700572static void genNewInstance(CompilationUnit* cUnit, MIR* mir,
573 RegLocation rlDest)
574{
buzbeedfd3d702011-08-28 12:56:51 -0700575 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee67bf8852011-08-17 17:51:35 -0700576 loadWordDisp(cUnit, rSELF,
Brian Carlstrom1f870082011-08-23 16:02:11 -0700577 OFFSETOF_MEMBER(Thread, pAllocObjectFromCode), rLR);
buzbeedfd3d702011-08-28 12:56:51 -0700578 loadCurrMethodDirect(cUnit, r1); // arg1 <= Method*
579 loadConstant(cUnit, r0, mir->dalvikInsn.vB); // arg0 <- type_id
buzbee67bf8852011-08-17 17:51:35 -0700580 opReg(cUnit, kOpBlx, rLR);
581 oatClobberCallRegs(cUnit);
582 RegLocation rlResult = oatGetReturn(cUnit);
583 storeValue(cUnit, rlDest, rlResult);
584}
585
586void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
587{
588 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700589 OFFSETOF_MEMBER(Thread, pThrowException), rLR);
590 loadValueDirectFixed(cUnit, rlSrc, r1); // Get exception object
buzbee67bf8852011-08-17 17:51:35 -0700591 genRegCopy(cUnit, r0, rSELF);
buzbee1b4c8592011-08-31 10:43:51 -0700592 opReg(cUnit, kOpBlx, rLR); // artThrowException(thread, exception);
buzbee67bf8852011-08-17 17:51:35 -0700593}
594
595static void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
596 RegLocation rlSrc)
597{
598 // May generate a call - use explicit registers
599 RegLocation rlResult;
buzbeec143c552011-08-20 17:38:58 -0700600 Class* classPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700601 GetResolvedType(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700602 if (classPtr == NULL) {
buzbee1da522d2011-09-04 11:22:20 -0700603 UNIMPLEMENTED(FATAL) << "Handle null class pointer";
buzbee67bf8852011-08-17 17:51:35 -0700604 }
605 oatFlushAllRegs(cUnit); /* Everything to home location */
606 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
607 loadConstant(cUnit, r2, (int) classPtr );
608 /* When taken r0 has NULL which can be used for store directly */
609 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
610 /* r1 now contains object->clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700611 assert(Object::ClassOffset().Int32Value() == 0);
612 loadWordDisp(cUnit, r0, Object::ClassOffset().Int32Value(), r1);
buzbee67bf8852011-08-17 17:51:35 -0700613 /* r1 now contains object->clazz */
614 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700615 OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700616 loadConstant(cUnit, r0, 1); /* Assume true */
617 opRegReg(cUnit, kOpCmp, r1, r2);
618 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondEq);
619 genRegCopy(cUnit, r0, r1);
620 genRegCopy(cUnit, r1, r2);
621 opReg(cUnit, kOpBlx, rLR);
622 oatClobberCallRegs(cUnit);
623 /* branch target here */
624 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
625 target->defMask = ENCODE_ALL;
626 rlResult = oatGetReturn(cUnit);
627 storeValue(cUnit, rlDest, rlResult);
628 branch1->generic.target = (LIR*)target;
629 branch2->generic.target = (LIR*)target;
630}
631
632static void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
633{
buzbeec143c552011-08-20 17:38:58 -0700634 Class* classPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700635 GetResolvedType(mir->dalvikInsn.vB);
buzbee67bf8852011-08-17 17:51:35 -0700636 if (classPtr == NULL) {
buzbee1da522d2011-09-04 11:22:20 -0700637 UNIMPLEMENTED(FATAL) << "Unimplemented null class pointer";
buzbee67bf8852011-08-17 17:51:35 -0700638 }
639 oatFlushAllRegs(cUnit); /* Everything to home location */
640 loadConstant(cUnit, r1, (int) classPtr );
641 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
642 /* Null? */
643 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq,
644 rlSrc.lowReg, 0);
645 /*
646 * rlSrc.lowReg now contains object->clazz. Note that
647 * it could have been allocated r0, but we're okay so long
648 * as we don't do anything desctructive until r0 is loaded
649 * with clazz.
650 */
651 /* r0 now contains object->clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700652 loadWordDisp(cUnit, rlSrc.lowReg, Object::ClassOffset().Int32Value(), r0);
buzbee67bf8852011-08-17 17:51:35 -0700653 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700654 OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700655 opRegReg(cUnit, kOpCmp, r0, r1);
656 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondEq);
657 // Assume success - if not, artInstanceOfNonTrivial will handle throw
658 opReg(cUnit, kOpBlx, rLR);
659 oatClobberCallRegs(cUnit);
660 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
661 target->defMask = ENCODE_ALL;
662 branch1->generic.target = (LIR*)target;
663 branch2->generic.target = (LIR*)target;
664}
665
666static void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest,
667 RegLocation rlSrc)
668{
669 RegLocation rlResult;
670 rlSrc = loadValue(cUnit, rlSrc, kFPReg);
671 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
672 newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg);
673 storeValue(cUnit, rlDest, rlResult);
674}
675
676static void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest,
677 RegLocation rlSrc)
678{
679 RegLocation rlResult;
680 rlSrc = loadValueWide(cUnit, rlSrc, kFPReg);
681 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
682 newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg),
683 S2D(rlSrc.lowReg, rlSrc.highReg));
684 storeValueWide(cUnit, rlDest, rlResult);
685}
686
buzbee439c4fa2011-08-27 15:59:07 -0700687static void freeRegLocTemps(CompilationUnit* cUnit, RegLocation rlKeep,
688 RegLocation rlFree)
buzbee67bf8852011-08-17 17:51:35 -0700689{
buzbee439c4fa2011-08-27 15:59:07 -0700690 if ((rlFree.lowReg != rlKeep.lowReg) && (rlFree.lowReg != rlKeep.highReg))
691 oatFreeTemp(cUnit, rlFree.lowReg);
692 if ((rlFree.highReg != rlKeep.lowReg) && (rlFree.highReg != rlKeep.highReg))
693 oatFreeTemp(cUnit, rlFree.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700694}
695
696static void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
697 OpKind secondOp, RegLocation rlDest,
698 RegLocation rlSrc1, RegLocation rlSrc2)
699{
buzbee9e0f9b02011-08-24 15:32:46 -0700700 /*
701 * NOTE: This is the one place in the code in which we might have
702 * as many as six live temporary registers. There are 5 in the normal
703 * set for Arm. Until we have spill capabilities, temporarily add
704 * lr to the temp set. It is safe to do this locally, but note that
705 * lr is used explicitly elsewhere in the code generator and cannot
706 * normally be used as a general temp register.
707 */
buzbee67bf8852011-08-17 17:51:35 -0700708 RegLocation rlResult;
buzbee9e0f9b02011-08-24 15:32:46 -0700709 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
710 oatFreeTemp(cUnit, rLR); // and make it available
buzbee67bf8852011-08-17 17:51:35 -0700711 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
712 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
713 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
714 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
715 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
716 rlSrc2.highReg);
buzbee439c4fa2011-08-27 15:59:07 -0700717 /*
718 * NOTE: If rlDest refers to a frame variable in a large frame, the
719 * following storeValueWide might need to allocate a temp register.
720 * To further work around the lack of a spill capability, explicitly
721 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
722 * Remove when spill is functional.
723 */
724 freeRegLocTemps(cUnit, rlResult, rlSrc1);
725 freeRegLocTemps(cUnit, rlResult, rlSrc2);
buzbee67bf8852011-08-17 17:51:35 -0700726 storeValueWide(cUnit, rlDest, rlResult);
buzbee9e0f9b02011-08-24 15:32:46 -0700727 oatClobber(cUnit, rLR);
728 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee67bf8852011-08-17 17:51:35 -0700729}
730
731void oatInitializeRegAlloc(CompilationUnit* cUnit)
732{
733 int numRegs = sizeof(coreRegs)/sizeof(*coreRegs);
734 int numReserved = sizeof(reservedRegs)/sizeof(*reservedRegs);
735 int numTemps = sizeof(coreTemps)/sizeof(*coreTemps);
736 int numFPRegs = sizeof(fpRegs)/sizeof(*fpRegs);
737 int numFPTemps = sizeof(fpTemps)/sizeof(*fpTemps);
738 RegisterPool *pool = (RegisterPool *)oatNew(sizeof(*pool), true);
739 cUnit->regPool = pool;
740 pool->numCoreRegs = numRegs;
741 pool->coreRegs = (RegisterInfo *)
742 oatNew(numRegs * sizeof(*cUnit->regPool->coreRegs), true);
743 pool->numFPRegs = numFPRegs;
744 pool->FPRegs = (RegisterInfo *)
745 oatNew(numFPRegs * sizeof(*cUnit->regPool->FPRegs), true);
746 oatInitPool(pool->coreRegs, coreRegs, pool->numCoreRegs);
747 oatInitPool(pool->FPRegs, fpRegs, pool->numFPRegs);
748 // Keep special registers from being allocated
749 for (int i = 0; i < numReserved; i++) {
750 oatMarkInUse(cUnit, reservedRegs[i]);
751 }
752 // Mark temp regs - all others not in use can be used for promotion
753 for (int i = 0; i < numTemps; i++) {
754 oatMarkTemp(cUnit, coreTemps[i]);
755 }
756 for (int i = 0; i < numFPTemps; i++) {
757 oatMarkTemp(cUnit, fpTemps[i]);
758 }
759 pool->nullCheckedRegs =
760 oatAllocBitVector(cUnit->numSSARegs, false);
761}
762
763/*
764 * Handle simple case (thin lock) inline. If it's complicated, bail
765 * out to the heavyweight lock/unlock routines. We'll use dedicated
766 * registers here in order to be in the right position in case we
767 * to bail to dvm[Lock/Unlock]Object(self, object)
768 *
769 * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object
770 * r1 -> object [arg1 for dvm[Lock/Unlock]Object
771 * r2 -> intial contents of object->lock, later result of strex
772 * r3 -> self->threadId
773 * r12 -> allow to be used by utilities as general temp
774 *
775 * The result of the strex is 0 if we acquire the lock.
776 *
777 * See comments in Sync.c for the layout of the lock word.
778 * Of particular interest to this code is the test for the
779 * simple case - which we handle inline. For monitor enter, the
780 * simple case is thin lock, held by no-one. For monitor exit,
781 * the simple case is thin lock, held by the unlocking thread with
782 * a recurse count of 0.
783 *
784 * A minor complication is that there is a field in the lock word
785 * unrelated to locking: the hash state. This field must be ignored, but
786 * preserved.
787 *
788 */
789static void genMonitorEnter(CompilationUnit* cUnit, MIR* mir,
790 RegLocation rlSrc)
791{
792 ArmLIR* target;
793 ArmLIR* hopTarget;
794 ArmLIR* branch;
795 ArmLIR* hopBranch;
796
797 oatFlushAllRegs(cUnit);
buzbeec143c552011-08-20 17:38:58 -0700798 assert(art::Monitor::kLwShapeThin == 0);
buzbee67bf8852011-08-17 17:51:35 -0700799 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700800 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee67bf8852011-08-17 17:51:35 -0700801 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
buzbeec143c552011-08-20 17:38:58 -0700802 loadWordDisp(cUnit, rSELF, Thread::IdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -0700803 newLIR3(cUnit, kThumb2Ldrex, r2, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700804 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
buzbeec143c552011-08-20 17:38:58 -0700805 // Align owner
806 opRegImm(cUnit, kOpLsl, r3, art::Monitor::kLwLockOwnerShift);
buzbee67bf8852011-08-17 17:51:35 -0700807 // Is lock unheld on lock or held by us (==threadId) on unlock?
buzbeec143c552011-08-20 17:38:58 -0700808 newLIR4(cUnit, kThumb2Bfi, r3, r2, 0, art::Monitor::kLwLockOwnerShift
809 - 1);
810 newLIR3(cUnit, kThumb2Bfc, r2, art::Monitor::kLwHashStateShift,
811 art::Monitor::kLwLockOwnerShift - 1);
buzbee67bf8852011-08-17 17:51:35 -0700812 hopBranch = newLIR2(cUnit, kThumb2Cbnz, r2, 0);
buzbeec143c552011-08-20 17:38:58 -0700813 newLIR4(cUnit, kThumb2Strex, r2, r3, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700814 Object::MonitorOffset().Int32Value() >> 2);
buzbee67bf8852011-08-17 17:51:35 -0700815 oatGenMemBarrier(cUnit, kSY);
816 branch = newLIR2(cUnit, kThumb2Cbz, r2, 0);
817
818 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
819 hopTarget->defMask = ENCODE_ALL;
820 hopBranch->generic.target = (LIR*)hopTarget;
821
buzbee1b4c8592011-08-31 10:43:51 -0700822 // Go expensive route - artLockObjectFromCode(self, obj);
823 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700824 rLR);
825 genRegCopy(cUnit, r0, rSELF);
826 newLIR1(cUnit, kThumbBlxR, rLR);
827
828 // Resume here
829 target = newLIR0(cUnit, kArmPseudoTargetLabel);
830 target->defMask = ENCODE_ALL;
831 branch->generic.target = (LIR*)target;
832}
833
834/*
835 * For monitor unlock, we don't have to use ldrex/strex. Once
836 * we've determined that the lock is thin and that we own it with
837 * a zero recursion count, it's safe to punch it back to the
838 * initial, unlock thin state with a store word.
839 */
840static void genMonitorExit(CompilationUnit* cUnit, MIR* mir,
841 RegLocation rlSrc)
842{
843 ArmLIR* target;
844 ArmLIR* branch;
845 ArmLIR* hopTarget;
846 ArmLIR* hopBranch;
847
buzbeec143c552011-08-20 17:38:58 -0700848 assert(art::Monitor::kLwShapeThin == 0);
buzbee67bf8852011-08-17 17:51:35 -0700849 oatFlushAllRegs(cUnit);
850 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700851 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee67bf8852011-08-17 17:51:35 -0700852 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir->offset, NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700853 loadWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r2); // Get lock
buzbeec143c552011-08-20 17:38:58 -0700854 loadWordDisp(cUnit, rSELF, Thread::IdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -0700855 // Is lock unheld on lock or held by us (==threadId) on unlock?
buzbeec143c552011-08-20 17:38:58 -0700856 opRegRegImm(cUnit, kOpAnd, r12, r2, (art::Monitor::kLwHashStateMask <<
857 art::Monitor::kLwHashStateShift));
858 // Align owner
859 opRegImm(cUnit, kOpLsl, r3, art::Monitor::kLwLockOwnerShift);
860 newLIR3(cUnit, kThumb2Bfc, r2, art::Monitor::kLwHashStateShift,
861 art::Monitor::kLwLockOwnerShift - 1);
buzbee67bf8852011-08-17 17:51:35 -0700862 opRegReg(cUnit, kOpSub, r2, r3);
863 hopBranch = opCondBranch(cUnit, kArmCondNe);
864 oatGenMemBarrier(cUnit, kSY);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700865 storeWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r12);
buzbee67bf8852011-08-17 17:51:35 -0700866 branch = opNone(cUnit, kOpUncondBr);
867
868 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
869 hopTarget->defMask = ENCODE_ALL;
870 hopBranch->generic.target = (LIR*)hopTarget;
871
buzbee1b4c8592011-08-31 10:43:51 -0700872 // Go expensive route - UnlockObjectFromCode(self, obj);
873 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700874 rLR);
875 genRegCopy(cUnit, r0, rSELF);
876 newLIR1(cUnit, kThumbBlxR, rLR);
877
878 // Resume here
879 target = newLIR0(cUnit, kArmPseudoTargetLabel);
880 target->defMask = ENCODE_ALL;
881 branch->generic.target = (LIR*)target;
882}
883
884/*
885 * 64-bit 3way compare function.
886 * mov rX, #-1
887 * cmp op1hi, op2hi
888 * blt done
889 * bgt flip
890 * sub rX, op1lo, op2lo (treat as unsigned)
891 * beq done
892 * ite hi
893 * mov(hi) rX, #-1
894 * mov(!hi) rX, #1
895 * flip:
896 * neg rX
897 * done:
898 */
899static void genCmpLong(CompilationUnit* cUnit, MIR* mir,
900 RegLocation rlDest, RegLocation rlSrc1,
901 RegLocation rlSrc2)
902{
903 RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change
904 ArmLIR* target1;
905 ArmLIR* target2;
906 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
907 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
908 rlTemp.lowReg = oatAllocTemp(cUnit);
909 loadConstant(cUnit, rlTemp.lowReg, -1);
910 opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg);
911 ArmLIR* branch1 = opCondBranch(cUnit, kArmCondLt);
912 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondGt);
913 opRegRegReg(cUnit, kOpSub, rlTemp.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
914 ArmLIR* branch3 = opCondBranch(cUnit, kArmCondEq);
915
916 genIT(cUnit, kArmCondHi, "E");
917 newLIR2(cUnit, kThumb2MovImmShift, rlTemp.lowReg, modifiedImmediate(-1));
918 loadConstant(cUnit, rlTemp.lowReg, 1);
919 genBarrier(cUnit);
920
921 target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
922 target2->defMask = -1;
923 opRegReg(cUnit, kOpNeg, rlTemp.lowReg, rlTemp.lowReg);
924
925 target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
926 target1->defMask = -1;
927
928 storeValue(cUnit, rlDest, rlTemp);
929
930 branch1->generic.target = (LIR*)target1;
931 branch2->generic.target = (LIR*)target2;
932 branch3->generic.target = branch1->generic.target;
933}
934
935static void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit,
936 RegLocation rlSrc, RegLocation rlResult, int lit,
937 int firstBit, int secondBit)
938{
939 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
940 encodeShift(kArmLsl, secondBit - firstBit));
941 if (firstBit != 0) {
942 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
943 }
944}
945
946static bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
947 int srcSize, int tgtSize)
948{
949 /*
950 * Don't optimize the register usage since it calls out to support
951 * functions
952 */
953 RegLocation rlSrc;
954 RegLocation rlDest;
955 oatFlushAllRegs(cUnit); /* Send everything to home location */
956 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
957 if (srcSize == 1) {
958 rlSrc = oatGetSrc(cUnit, mir, 0);
959 loadValueDirectFixed(cUnit, rlSrc, r0);
960 } else {
961 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
962 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
963 }
964 opReg(cUnit, kOpBlx, rLR);
965 oatClobberCallRegs(cUnit);
966 if (tgtSize == 1) {
967 RegLocation rlResult;
968 rlDest = oatGetDest(cUnit, mir, 0);
969 rlResult = oatGetReturn(cUnit);
970 storeValue(cUnit, rlDest, rlResult);
971 } else {
972 RegLocation rlResult;
973 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
974 rlResult = oatGetReturnWide(cUnit);
975 storeValueWide(cUnit, rlDest, rlResult);
976 }
977 return false;
978}
979
980static bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
981 RegLocation rlDest, RegLocation rlSrc1,
982 RegLocation rlSrc2)
983{
984 RegLocation rlResult;
985 int funcOffset;
986
987 switch (mir->dalvikInsn.opcode) {
988 case OP_ADD_FLOAT_2ADDR:
989 case OP_ADD_FLOAT:
990 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
991 break;
992 case OP_SUB_FLOAT_2ADDR:
993 case OP_SUB_FLOAT:
994 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
995 break;
996 case OP_DIV_FLOAT_2ADDR:
997 case OP_DIV_FLOAT:
998 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
999 break;
1000 case OP_MUL_FLOAT_2ADDR:
1001 case OP_MUL_FLOAT:
1002 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
1003 break;
1004 case OP_REM_FLOAT_2ADDR:
1005 case OP_REM_FLOAT:
1006 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
1007 break;
1008 case OP_NEG_FLOAT: {
1009 genNegFloat(cUnit, rlDest, rlSrc1);
1010 return false;
1011 }
1012 default:
1013 return true;
1014 }
1015 oatFlushAllRegs(cUnit); /* Send everything to home location */
1016 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1017 loadValueDirectFixed(cUnit, rlSrc1, r0);
1018 loadValueDirectFixed(cUnit, rlSrc2, r1);
1019 opReg(cUnit, kOpBlx, rLR);
1020 oatClobberCallRegs(cUnit);
1021 rlResult = oatGetReturn(cUnit);
1022 storeValue(cUnit, rlDest, rlResult);
1023 return false;
1024}
1025
1026static bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
1027 RegLocation rlDest, RegLocation rlSrc1,
1028 RegLocation rlSrc2)
1029{
1030 RegLocation rlResult;
1031 int funcOffset;
1032
1033 switch (mir->dalvikInsn.opcode) {
1034 case OP_ADD_DOUBLE_2ADDR:
1035 case OP_ADD_DOUBLE:
1036 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
1037 break;
1038 case OP_SUB_DOUBLE_2ADDR:
1039 case OP_SUB_DOUBLE:
1040 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
1041 break;
1042 case OP_DIV_DOUBLE_2ADDR:
1043 case OP_DIV_DOUBLE:
1044 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
1045 break;
1046 case OP_MUL_DOUBLE_2ADDR:
1047 case OP_MUL_DOUBLE:
1048 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
1049 break;
1050 case OP_REM_DOUBLE_2ADDR:
1051 case OP_REM_DOUBLE:
1052 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
1053 break;
1054 case OP_NEG_DOUBLE: {
1055 genNegDouble(cUnit, rlDest, rlSrc1);
1056 return false;
1057 }
1058 default:
1059 return true;
1060 }
1061 oatFlushAllRegs(cUnit); /* Send everything to home location */
1062 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1063 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1064 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1065 opReg(cUnit, kOpBlx, rLR);
1066 oatClobberCallRegs(cUnit);
1067 rlResult = oatGetReturnWide(cUnit);
1068 storeValueWide(cUnit, rlDest, rlResult);
1069 return false;
1070}
1071
1072static bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
1073{
1074 Opcode opcode = mir->dalvikInsn.opcode;
1075
1076 switch (opcode) {
1077 case OP_INT_TO_FLOAT:
1078 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
1079 1, 1);
1080 case OP_FLOAT_TO_INT:
1081 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
1082 1, 1);
1083 case OP_DOUBLE_TO_FLOAT:
1084 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
1085 2, 1);
1086 case OP_FLOAT_TO_DOUBLE:
1087 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
1088 1, 2);
1089 case OP_INT_TO_DOUBLE:
1090 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
1091 1, 2);
1092 case OP_DOUBLE_TO_INT:
1093 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
1094 2, 1);
1095 case OP_FLOAT_TO_LONG:
1096 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001097 pF2l), 1, 2);
buzbee67bf8852011-08-17 17:51:35 -07001098 case OP_LONG_TO_FLOAT:
1099 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
1100 2, 1);
1101 case OP_DOUBLE_TO_LONG:
1102 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001103 pD2l), 2, 2);
buzbee67bf8852011-08-17 17:51:35 -07001104 case OP_LONG_TO_DOUBLE:
1105 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
1106 2, 2);
1107 default:
1108 return true;
1109 }
1110 return false;
1111}
1112
1113/* Generate conditional branch instructions */
1114static ArmLIR* genConditionalBranch(CompilationUnit* cUnit,
1115 ArmConditionCode cond,
1116 ArmLIR* target)
1117{
1118 ArmLIR* branch = opCondBranch(cUnit, cond);
1119 branch->generic.target = (LIR*) target;
1120 return branch;
1121}
1122
1123/* Generate a unconditional branch to go to the interpreter */
1124static inline ArmLIR* genTrap(CompilationUnit* cUnit, int dOffset,
1125 ArmLIR* pcrLabel)
1126{
1127 ArmLIR* branch = opNone(cUnit, kOpUncondBr);
1128 return genCheckCommon(cUnit, dOffset, branch, pcrLabel);
1129}
1130
1131/*
1132 * Generate array store
1133 *
1134 */
buzbee1b4c8592011-08-31 10:43:51 -07001135static void genArrayObjPut(CompilationUnit* cUnit, MIR* mir,
1136 RegLocation rlArray, RegLocation rlIndex,
1137 RegLocation rlSrc, int scale)
buzbee67bf8852011-08-17 17:51:35 -07001138{
1139 RegisterClass regClass = oatRegClassBySize(kWord);
buzbeec143c552011-08-20 17:38:58 -07001140 int lenOffset = Array::LengthOffset().Int32Value();
1141 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001142
1143 /* Make sure it's a legal object Put. Use direct regs at first */
1144 loadValueDirectFixed(cUnit, rlArray, r1);
1145 loadValueDirectFixed(cUnit, rlSrc, r0);
1146
1147 /* null array object? */
1148 ArmLIR* pcrLabel = NULL;
1149
1150 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
1151 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, r1,
1152 mir->offset, NULL);
1153 }
1154 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -07001155 OFFSETOF_MEMBER(Thread, pCanPutArrayElementFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -07001156 /* Get the array's clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001157 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), r1);
buzbee67bf8852011-08-17 17:51:35 -07001158 /* Get the object's clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001159 loadWordDisp(cUnit, r0, Object::ClassOffset().Int32Value(), r0);
buzbee67bf8852011-08-17 17:51:35 -07001160 opReg(cUnit, kOpBlx, rLR);
1161 oatClobberCallRegs(cUnit);
1162
1163 // Now, redo loadValues in case they didn't survive the call
1164
1165 int regPtr;
1166 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1167 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1168
1169 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1170 oatClobber(cUnit, rlArray.lowReg);
1171 regPtr = rlArray.lowReg;
1172 } else {
1173 regPtr = oatAllocTemp(cUnit);
1174 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1175 }
1176
1177 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1178 int regLen = oatAllocTemp(cUnit);
1179 //NOTE: max live temps(4) here.
1180 /* Get len */
1181 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1182 /* regPtr -> array data */
1183 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1184 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
1185 pcrLabel);
1186 oatFreeTemp(cUnit, regLen);
1187 } else {
1188 /* regPtr -> array data */
1189 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1190 }
1191 /* at this point, regPtr points to array, 2 live temps */
1192 rlSrc = loadValue(cUnit, rlSrc, regClass);
1193 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1194 scale, kWord);
1195}
1196
1197/*
1198 * Generate array load
1199 */
1200static void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
1201 RegLocation rlArray, RegLocation rlIndex,
1202 RegLocation rlDest, int scale)
1203{
1204 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001205 int lenOffset = Array::LengthOffset().Int32Value();
1206 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001207 RegLocation rlResult;
1208 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1209 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1210 int regPtr;
1211
1212 /* null object? */
1213 ArmLIR* pcrLabel = NULL;
1214
1215 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
1216 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow,
1217 rlArray.lowReg, mir->offset, NULL);
1218 }
1219
1220 regPtr = oatAllocTemp(cUnit);
1221
1222 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1223 int regLen = oatAllocTemp(cUnit);
1224 /* Get len */
1225 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1226 /* regPtr -> array data */
1227 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1228 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
1229 pcrLabel);
1230 oatFreeTemp(cUnit, regLen);
1231 } else {
1232 /* regPtr -> array data */
1233 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1234 }
1235 if ((size == kLong) || (size == kDouble)) {
1236 if (scale) {
1237 int rNewIndex = oatAllocTemp(cUnit);
1238 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1239 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1240 oatFreeTemp(cUnit, rNewIndex);
1241 } else {
1242 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1243 }
1244 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1245
1246 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1247
1248 oatFreeTemp(cUnit, regPtr);
1249 storeValueWide(cUnit, rlDest, rlResult);
1250 } else {
1251 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1252
1253 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1254 scale, size);
1255
1256 oatFreeTemp(cUnit, regPtr);
1257 storeValue(cUnit, rlDest, rlResult);
1258 }
1259}
1260
1261/*
1262 * Generate array store
1263 *
1264 */
1265static void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1266 RegLocation rlArray, RegLocation rlIndex,
1267 RegLocation rlSrc, int scale)
1268{
1269 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001270 int lenOffset = Array::LengthOffset().Int32Value();
1271 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001272
1273 int regPtr;
1274 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1275 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1276
1277 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1278 oatClobber(cUnit, rlArray.lowReg);
1279 regPtr = rlArray.lowReg;
1280 } else {
1281 regPtr = oatAllocTemp(cUnit);
1282 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1283 }
1284
1285 /* null object? */
1286 ArmLIR* pcrLabel = NULL;
1287
1288 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
1289 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
1290 mir->offset, NULL);
1291 }
1292
1293 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1294 int regLen = oatAllocTemp(cUnit);
1295 //NOTE: max live temps(4) here.
1296 /* Get len */
1297 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1298 /* regPtr -> array data */
1299 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1300 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
1301 pcrLabel);
1302 oatFreeTemp(cUnit, regLen);
1303 } else {
1304 /* regPtr -> array data */
1305 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1306 }
1307 /* at this point, regPtr points to array, 2 live temps */
1308 if ((size == kLong) || (size == kDouble)) {
1309 //TODO: need specific wide routine that can handle fp regs
1310 if (scale) {
1311 int rNewIndex = oatAllocTemp(cUnit);
1312 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1313 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1314 oatFreeTemp(cUnit, rNewIndex);
1315 } else {
1316 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1317 }
1318 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1319
1320 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1321
1322 oatFreeTemp(cUnit, regPtr);
1323 } else {
1324 rlSrc = loadValue(cUnit, rlSrc, regClass);
1325
1326 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1327 scale, size);
1328 }
1329}
1330
1331static bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir,
1332 RegLocation rlDest, RegLocation rlSrc1,
1333 RegLocation rlShift)
1334{
buzbee54330722011-08-23 16:46:55 -07001335 int funcOffset;
buzbee67bf8852011-08-17 17:51:35 -07001336
buzbee67bf8852011-08-17 17:51:35 -07001337 switch( mir->dalvikInsn.opcode) {
1338 case OP_SHL_LONG:
1339 case OP_SHL_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001340 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
buzbee67bf8852011-08-17 17:51:35 -07001341 break;
1342 case OP_SHR_LONG:
1343 case OP_SHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001344 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
buzbee67bf8852011-08-17 17:51:35 -07001345 break;
1346 case OP_USHR_LONG:
1347 case OP_USHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001348 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
buzbee67bf8852011-08-17 17:51:35 -07001349 break;
1350 default:
buzbee54330722011-08-23 16:46:55 -07001351 LOG(FATAL) << "Unexpected case";
buzbee67bf8852011-08-17 17:51:35 -07001352 return true;
1353 }
buzbee54330722011-08-23 16:46:55 -07001354 oatFlushAllRegs(cUnit); /* Send everything to home location */
1355 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1356 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1357 loadValueDirect(cUnit, rlShift, r2);
1358 opReg(cUnit, kOpBlx, rLR);
1359 oatClobberCallRegs(cUnit);
1360 RegLocation rlResult = oatGetReturnWide(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001361 storeValueWide(cUnit, rlDest, rlResult);
1362 return false;
1363}
1364
1365static bool genArithOpLong(CompilationUnit* cUnit, MIR* mir,
1366 RegLocation rlDest, RegLocation rlSrc1,
1367 RegLocation rlSrc2)
1368{
1369 RegLocation rlResult;
1370 OpKind firstOp = kOpBkpt;
1371 OpKind secondOp = kOpBkpt;
1372 bool callOut = false;
1373 int funcOffset;
1374 int retReg = r0;
1375
1376 switch (mir->dalvikInsn.opcode) {
1377 case OP_NOT_LONG:
1378 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1379 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1380 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1381 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1382 storeValueWide(cUnit, rlDest, rlResult);
1383 return false;
1384 break;
1385 case OP_ADD_LONG:
1386 case OP_ADD_LONG_2ADDR:
1387 firstOp = kOpAdd;
1388 secondOp = kOpAdc;
1389 break;
1390 case OP_SUB_LONG:
1391 case OP_SUB_LONG_2ADDR:
1392 firstOp = kOpSub;
1393 secondOp = kOpSbc;
1394 break;
1395 case OP_MUL_LONG:
1396 case OP_MUL_LONG_2ADDR:
buzbee439c4fa2011-08-27 15:59:07 -07001397 callOut = true;
1398 retReg = r0;
1399 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1400 break;
buzbee67bf8852011-08-17 17:51:35 -07001401 case OP_DIV_LONG:
1402 case OP_DIV_LONG_2ADDR:
1403 callOut = true;
1404 retReg = r0;
1405 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1406 break;
1407 /* NOTE - result is in r2/r3 instead of r0/r1 */
1408 case OP_REM_LONG:
1409 case OP_REM_LONG_2ADDR:
1410 callOut = true;
1411 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1412 retReg = r2;
1413 break;
1414 case OP_AND_LONG_2ADDR:
1415 case OP_AND_LONG:
1416 firstOp = kOpAnd;
1417 secondOp = kOpAnd;
1418 break;
1419 case OP_OR_LONG:
1420 case OP_OR_LONG_2ADDR:
1421 firstOp = kOpOr;
1422 secondOp = kOpOr;
1423 break;
1424 case OP_XOR_LONG:
1425 case OP_XOR_LONG_2ADDR:
1426 firstOp = kOpXor;
1427 secondOp = kOpXor;
1428 break;
1429 case OP_NEG_LONG: {
1430 //TUNING: can improve this using Thumb2 code
1431 int tReg = oatAllocTemp(cUnit);
1432 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1433 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1434 loadConstantNoClobber(cUnit, tReg, 0);
1435 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1436 tReg, rlSrc2.lowReg);
1437 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
1438 genRegCopy(cUnit, rlResult.highReg, tReg);
1439 storeValueWide(cUnit, rlDest, rlResult);
1440 return false;
1441 }
1442 default:
1443 LOG(FATAL) << "Invalid long arith op";
1444 }
1445 if (!callOut) {
1446 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1447 } else {
1448 // Adjust return regs in to handle case of rem returning r2/r3
1449 oatFlushAllRegs(cUnit); /* Send everything to home location */
1450 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1451 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1452 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1453 opReg(cUnit, kOpBlx, rLR);
1454 oatClobberCallRegs(cUnit);
1455 if (retReg == r0)
1456 rlResult = oatGetReturnWide(cUnit);
1457 else
1458 rlResult = oatGetReturnWideAlt(cUnit);
1459 storeValueWide(cUnit, rlDest, rlResult);
1460 }
1461 return false;
1462}
1463
1464static bool genArithOpInt(CompilationUnit* cUnit, MIR* mir,
1465 RegLocation rlDest, RegLocation rlSrc1,
1466 RegLocation rlSrc2)
1467{
1468 OpKind op = kOpBkpt;
1469 bool callOut = false;
1470 bool checkZero = false;
1471 bool unary = false;
1472 int retReg = r0;
1473 int funcOffset;
1474 RegLocation rlResult;
1475 bool shiftOp = false;
1476
1477 switch (mir->dalvikInsn.opcode) {
1478 case OP_NEG_INT:
1479 op = kOpNeg;
1480 unary = true;
1481 break;
1482 case OP_NOT_INT:
1483 op = kOpMvn;
1484 unary = true;
1485 break;
1486 case OP_ADD_INT:
1487 case OP_ADD_INT_2ADDR:
1488 op = kOpAdd;
1489 break;
1490 case OP_SUB_INT:
1491 case OP_SUB_INT_2ADDR:
1492 op = kOpSub;
1493 break;
1494 case OP_MUL_INT:
1495 case OP_MUL_INT_2ADDR:
1496 op = kOpMul;
1497 break;
1498 case OP_DIV_INT:
1499 case OP_DIV_INT_2ADDR:
1500 callOut = true;
1501 checkZero = true;
1502 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1503 retReg = r0;
1504 break;
1505 /* NOTE: returns in r1 */
1506 case OP_REM_INT:
1507 case OP_REM_INT_2ADDR:
1508 callOut = true;
1509 checkZero = true;
1510 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1511 retReg = r1;
1512 break;
1513 case OP_AND_INT:
1514 case OP_AND_INT_2ADDR:
1515 op = kOpAnd;
1516 break;
1517 case OP_OR_INT:
1518 case OP_OR_INT_2ADDR:
1519 op = kOpOr;
1520 break;
1521 case OP_XOR_INT:
1522 case OP_XOR_INT_2ADDR:
1523 op = kOpXor;
1524 break;
1525 case OP_SHL_INT:
1526 case OP_SHL_INT_2ADDR:
1527 shiftOp = true;
1528 op = kOpLsl;
1529 break;
1530 case OP_SHR_INT:
1531 case OP_SHR_INT_2ADDR:
1532 shiftOp = true;
1533 op = kOpAsr;
1534 break;
1535 case OP_USHR_INT:
1536 case OP_USHR_INT_2ADDR:
1537 shiftOp = true;
1538 op = kOpLsr;
1539 break;
1540 default:
1541 LOG(FATAL) << "Invalid word arith op: " <<
1542 (int)mir->dalvikInsn.opcode;
1543 }
1544 if (!callOut) {
1545 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1546 if (unary) {
1547 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1548 opRegReg(cUnit, op, rlResult.lowReg,
1549 rlSrc1.lowReg);
1550 } else {
1551 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1552 if (shiftOp) {
1553 int tReg = oatAllocTemp(cUnit);
1554 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1555 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1556 opRegRegReg(cUnit, op, rlResult.lowReg,
1557 rlSrc1.lowReg, tReg);
1558 oatFreeTemp(cUnit, tReg);
1559 } else {
1560 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1561 opRegRegReg(cUnit, op, rlResult.lowReg,
1562 rlSrc1.lowReg, rlSrc2.lowReg);
1563 }
1564 }
1565 storeValue(cUnit, rlDest, rlResult);
1566 } else {
1567 RegLocation rlResult;
1568 oatFlushAllRegs(cUnit); /* Send everything to home location */
1569 loadValueDirectFixed(cUnit, rlSrc2, r1);
1570 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1571 loadValueDirectFixed(cUnit, rlSrc1, r0);
1572 if (checkZero) {
1573 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
1574 }
1575 opReg(cUnit, kOpBlx, rLR);
1576 oatClobberCallRegs(cUnit);
1577 if (retReg == r0)
1578 rlResult = oatGetReturn(cUnit);
1579 else
1580 rlResult = oatGetReturnAlt(cUnit);
1581 storeValue(cUnit, rlDest, rlResult);
1582 }
1583 return false;
1584}
1585
buzbee67bf8852011-08-17 17:51:35 -07001586/*
1587 * Fetch *self->info.breakFlags. If the breakFlags are non-zero,
1588 * punt to the interpreter.
1589 */
1590static void genSuspendPoll(CompilationUnit* cUnit, MIR* mir)
1591{
1592 UNIMPLEMENTED(WARNING);
1593#if 0
1594 int rTemp = oatAllocTemp(cUnit);
1595 ArmLIR* ld;
1596 ld = loadBaseDisp(cUnit, NULL, rSELF,
1597 offsetof(Thread, interpBreak.ctl.breakFlags),
1598 rTemp, kUnsignedByte, INVALID_SREG);
1599 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
1600 genRegImmCheck(cUnit, kArmCondNe, rTemp, 0, mir->offset, NULL);
1601#endif
1602}
1603
1604/*
1605 * The following are the first-level codegen routines that analyze the format
1606 * of each bytecode then either dispatch special purpose codegen routines
1607 * or produce corresponding Thumb instructions directly.
1608 */
1609
1610static bool isPowerOfTwo(int x)
1611{
1612 return (x & (x - 1)) == 0;
1613}
1614
1615// Returns true if no more than two bits are set in 'x'.
1616static bool isPopCountLE2(unsigned int x)
1617{
1618 x &= x - 1;
1619 return (x & (x - 1)) == 0;
1620}
1621
1622// Returns the index of the lowest set bit in 'x'.
1623static int lowestSetBit(unsigned int x) {
1624 int bit_posn = 0;
1625 while ((x & 0xf) == 0) {
1626 bit_posn += 4;
1627 x >>= 4;
1628 }
1629 while ((x & 1) == 0) {
1630 bit_posn++;
1631 x >>= 1;
1632 }
1633 return bit_posn;
1634}
1635
1636// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1637// and store the result in 'rlDest'.
1638static bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode,
1639 RegLocation rlSrc, RegLocation rlDest, int lit)
1640{
1641 if (lit < 2 || !isPowerOfTwo(lit)) {
1642 return false;
1643 }
1644 int k = lowestSetBit(lit);
1645 if (k >= 30) {
1646 // Avoid special cases.
1647 return false;
1648 }
1649 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 ||
1650 dalvikOpcode == OP_DIV_INT_LIT16);
1651 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1652 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1653 if (div) {
1654 int tReg = oatAllocTemp(cUnit);
1655 if (lit == 2) {
1656 // Division by 2 is by far the most common division by constant.
1657 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1658 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1659 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1660 } else {
1661 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1662 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1663 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1664 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1665 }
1666 } else {
1667 int cReg = oatAllocTemp(cUnit);
1668 loadConstant(cUnit, cReg, lit - 1);
1669 int tReg1 = oatAllocTemp(cUnit);
1670 int tReg2 = oatAllocTemp(cUnit);
1671 if (lit == 2) {
1672 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1673 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1674 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1675 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1676 } else {
1677 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1678 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1679 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1680 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1681 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1682 }
1683 }
1684 storeValue(cUnit, rlDest, rlResult);
1685 return true;
1686}
1687
1688// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1689// and store the result in 'rlDest'.
1690static bool handleEasyMultiply(CompilationUnit* cUnit,
1691 RegLocation rlSrc, RegLocation rlDest, int lit)
1692{
1693 // Can we simplify this multiplication?
1694 bool powerOfTwo = false;
1695 bool popCountLE2 = false;
1696 bool powerOfTwoMinusOne = false;
1697 if (lit < 2) {
1698 // Avoid special cases.
1699 return false;
1700 } else if (isPowerOfTwo(lit)) {
1701 powerOfTwo = true;
1702 } else if (isPopCountLE2(lit)) {
1703 popCountLE2 = true;
1704 } else if (isPowerOfTwo(lit + 1)) {
1705 powerOfTwoMinusOne = true;
1706 } else {
1707 return false;
1708 }
1709 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1710 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1711 if (powerOfTwo) {
1712 // Shift.
1713 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1714 lowestSetBit(lit));
1715 } else if (popCountLE2) {
1716 // Shift and add and shift.
1717 int firstBit = lowestSetBit(lit);
1718 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1719 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1720 firstBit, secondBit);
1721 } else {
1722 // Reverse subtract: (src << (shift + 1)) - src.
1723 assert(powerOfTwoMinusOne);
1724 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1725 int tReg = oatAllocTemp(cUnit);
1726 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1727 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1728 }
1729 storeValue(cUnit, rlDest, rlResult);
1730 return true;
1731}
1732
1733static bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir,
1734 RegLocation rlDest, RegLocation rlSrc,
1735 int lit)
1736{
1737 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1738 RegLocation rlResult;
1739 OpKind op = (OpKind)0; /* Make gcc happy */
1740 int shiftOp = false;
1741 bool isDiv = false;
1742 int funcOffset;
1743
1744 switch (dalvikOpcode) {
1745 case OP_RSUB_INT_LIT8:
1746 case OP_RSUB_INT: {
1747 int tReg;
1748 //TUNING: add support for use of Arm rsub op
1749 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1750 tReg = oatAllocTemp(cUnit);
1751 loadConstant(cUnit, tReg, lit);
1752 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1753 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1754 tReg, rlSrc.lowReg);
1755 storeValue(cUnit, rlDest, rlResult);
1756 return false;
1757 break;
1758 }
1759
1760 case OP_ADD_INT_LIT8:
1761 case OP_ADD_INT_LIT16:
1762 op = kOpAdd;
1763 break;
1764 case OP_MUL_INT_LIT8:
1765 case OP_MUL_INT_LIT16: {
1766 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1767 return false;
1768 }
1769 op = kOpMul;
1770 break;
1771 }
1772 case OP_AND_INT_LIT8:
1773 case OP_AND_INT_LIT16:
1774 op = kOpAnd;
1775 break;
1776 case OP_OR_INT_LIT8:
1777 case OP_OR_INT_LIT16:
1778 op = kOpOr;
1779 break;
1780 case OP_XOR_INT_LIT8:
1781 case OP_XOR_INT_LIT16:
1782 op = kOpXor;
1783 break;
1784 case OP_SHL_INT_LIT8:
1785 lit &= 31;
1786 shiftOp = true;
1787 op = kOpLsl;
1788 break;
1789 case OP_SHR_INT_LIT8:
1790 lit &= 31;
1791 shiftOp = true;
1792 op = kOpAsr;
1793 break;
1794 case OP_USHR_INT_LIT8:
1795 lit &= 31;
1796 shiftOp = true;
1797 op = kOpLsr;
1798 break;
1799
1800 case OP_DIV_INT_LIT8:
1801 case OP_DIV_INT_LIT16:
1802 case OP_REM_INT_LIT8:
1803 case OP_REM_INT_LIT16:
1804 if (lit == 0) {
1805 UNIMPLEMENTED(FATAL);
1806 // FIXME: generate an explicit throw here
1807 return false;
1808 }
1809 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
1810 return false;
1811 }
1812 oatFlushAllRegs(cUnit); /* Everything to home location */
1813 loadValueDirectFixed(cUnit, rlSrc, r0);
1814 oatClobber(cUnit, r0);
1815 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
1816 (dalvikOpcode == OP_DIV_INT_LIT16)) {
1817 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1818 isDiv = true;
1819 } else {
1820 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1821 isDiv = false;
1822 }
1823 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1824 loadConstant(cUnit, r1, lit);
1825 opReg(cUnit, kOpBlx, rLR);
1826 oatClobberCallRegs(cUnit);
1827 if (isDiv)
1828 rlResult = oatGetReturn(cUnit);
1829 else
1830 rlResult = oatGetReturnAlt(cUnit);
1831 storeValue(cUnit, rlDest, rlResult);
1832 return false;
1833 break;
1834 default:
1835 return true;
1836 }
1837 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1838 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1839 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
1840 if (shiftOp && (lit == 0)) {
1841 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1842 } else {
1843 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
1844 }
1845 storeValue(cUnit, rlDest, rlResult);
1846 return false;
1847}
1848
1849/* Architectural-specific debugging helpers go here */
1850void oatArchDump(void)
1851{
1852 /* Print compiled opcode in this VM instance */
1853 int i, start, streak;
1854 char buf[1024];
1855
1856 streak = i = 0;
1857 buf[0] = 0;
1858 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
1859 i++;
1860 }
1861 if (i == kNumPackedOpcodes) {
1862 return;
1863 }
1864 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
1865 if (opcodeCoverage[i]) {
1866 streak++;
1867 } else {
1868 if (streak == 1) {
1869 sprintf(buf+strlen(buf), "%x,", start);
1870 } else {
1871 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
1872 }
1873 streak = 0;
1874 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
1875 i++;
1876 }
1877 if (i < kNumPackedOpcodes) {
1878 streak = 1;
1879 start = i;
1880 }
1881 }
1882 }
1883 if (streak) {
1884 if (streak == 1) {
1885 sprintf(buf+strlen(buf), "%x", start);
1886 } else {
1887 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
1888 }
1889 }
1890 if (strlen(buf)) {
1891 LOG(INFO) << "dalvik.vm.oat.op = " << buf;
1892 }
1893}