blob: b9e135c3edc18e884f455e5b16617c7f3f39ae65 [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 }
buzbeee9a72f62011-09-04 17:59:07 -07001235 oatFreeTemp(cUnit, rlArray.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001236 if ((size == kLong) || (size == kDouble)) {
1237 if (scale) {
1238 int rNewIndex = oatAllocTemp(cUnit);
1239 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1240 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1241 oatFreeTemp(cUnit, rNewIndex);
1242 } else {
1243 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1244 }
buzbeee9a72f62011-09-04 17:59:07 -07001245 oatFreeTemp(cUnit, rlIndex.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001246 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1247
1248 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1249
1250 oatFreeTemp(cUnit, regPtr);
1251 storeValueWide(cUnit, rlDest, rlResult);
1252 } else {
1253 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1254
1255 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1256 scale, size);
1257
1258 oatFreeTemp(cUnit, regPtr);
1259 storeValue(cUnit, rlDest, rlResult);
1260 }
1261}
1262
1263/*
1264 * Generate array store
1265 *
1266 */
1267static void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1268 RegLocation rlArray, RegLocation rlIndex,
1269 RegLocation rlSrc, int scale)
1270{
1271 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001272 int lenOffset = Array::LengthOffset().Int32Value();
1273 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001274
1275 int regPtr;
1276 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1277 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1278
1279 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1280 oatClobber(cUnit, rlArray.lowReg);
1281 regPtr = rlArray.lowReg;
1282 } else {
1283 regPtr = oatAllocTemp(cUnit);
1284 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1285 }
1286
1287 /* null object? */
1288 ArmLIR* pcrLabel = NULL;
1289
1290 if (!(mir->OptimizationFlags & MIR_IGNORE_NULL_CHECK)) {
1291 pcrLabel = genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg,
1292 mir->offset, NULL);
1293 }
1294
1295 if (!(mir->OptimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
1296 int regLen = oatAllocTemp(cUnit);
1297 //NOTE: max live temps(4) here.
1298 /* Get len */
1299 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1300 /* regPtr -> array data */
1301 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1302 genBoundsCheck(cUnit, rlIndex.lowReg, regLen, mir->offset,
1303 pcrLabel);
1304 oatFreeTemp(cUnit, regLen);
1305 } else {
1306 /* regPtr -> array data */
1307 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1308 }
1309 /* at this point, regPtr points to array, 2 live temps */
1310 if ((size == kLong) || (size == kDouble)) {
1311 //TODO: need specific wide routine that can handle fp regs
1312 if (scale) {
1313 int rNewIndex = oatAllocTemp(cUnit);
1314 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1315 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1316 oatFreeTemp(cUnit, rNewIndex);
1317 } else {
1318 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1319 }
1320 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1321
1322 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1323
1324 oatFreeTemp(cUnit, regPtr);
1325 } else {
1326 rlSrc = loadValue(cUnit, rlSrc, regClass);
1327
1328 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1329 scale, size);
1330 }
1331}
1332
1333static bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir,
1334 RegLocation rlDest, RegLocation rlSrc1,
1335 RegLocation rlShift)
1336{
buzbee54330722011-08-23 16:46:55 -07001337 int funcOffset;
buzbee67bf8852011-08-17 17:51:35 -07001338
buzbee67bf8852011-08-17 17:51:35 -07001339 switch( mir->dalvikInsn.opcode) {
1340 case OP_SHL_LONG:
1341 case OP_SHL_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001342 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
buzbee67bf8852011-08-17 17:51:35 -07001343 break;
1344 case OP_SHR_LONG:
1345 case OP_SHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001346 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
buzbee67bf8852011-08-17 17:51:35 -07001347 break;
1348 case OP_USHR_LONG:
1349 case OP_USHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001350 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
buzbee67bf8852011-08-17 17:51:35 -07001351 break;
1352 default:
buzbee54330722011-08-23 16:46:55 -07001353 LOG(FATAL) << "Unexpected case";
buzbee67bf8852011-08-17 17:51:35 -07001354 return true;
1355 }
buzbee54330722011-08-23 16:46:55 -07001356 oatFlushAllRegs(cUnit); /* Send everything to home location */
1357 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1358 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1359 loadValueDirect(cUnit, rlShift, r2);
1360 opReg(cUnit, kOpBlx, rLR);
1361 oatClobberCallRegs(cUnit);
1362 RegLocation rlResult = oatGetReturnWide(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001363 storeValueWide(cUnit, rlDest, rlResult);
1364 return false;
1365}
1366
1367static bool genArithOpLong(CompilationUnit* cUnit, MIR* mir,
1368 RegLocation rlDest, RegLocation rlSrc1,
1369 RegLocation rlSrc2)
1370{
1371 RegLocation rlResult;
1372 OpKind firstOp = kOpBkpt;
1373 OpKind secondOp = kOpBkpt;
1374 bool callOut = false;
1375 int funcOffset;
1376 int retReg = r0;
1377
1378 switch (mir->dalvikInsn.opcode) {
1379 case OP_NOT_LONG:
1380 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1381 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1382 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1383 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1384 storeValueWide(cUnit, rlDest, rlResult);
1385 return false;
1386 break;
1387 case OP_ADD_LONG:
1388 case OP_ADD_LONG_2ADDR:
1389 firstOp = kOpAdd;
1390 secondOp = kOpAdc;
1391 break;
1392 case OP_SUB_LONG:
1393 case OP_SUB_LONG_2ADDR:
1394 firstOp = kOpSub;
1395 secondOp = kOpSbc;
1396 break;
1397 case OP_MUL_LONG:
1398 case OP_MUL_LONG_2ADDR:
buzbee439c4fa2011-08-27 15:59:07 -07001399 callOut = true;
1400 retReg = r0;
1401 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1402 break;
buzbee67bf8852011-08-17 17:51:35 -07001403 case OP_DIV_LONG:
1404 case OP_DIV_LONG_2ADDR:
1405 callOut = true;
1406 retReg = r0;
1407 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1408 break;
1409 /* NOTE - result is in r2/r3 instead of r0/r1 */
1410 case OP_REM_LONG:
1411 case OP_REM_LONG_2ADDR:
1412 callOut = true;
1413 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1414 retReg = r2;
1415 break;
1416 case OP_AND_LONG_2ADDR:
1417 case OP_AND_LONG:
1418 firstOp = kOpAnd;
1419 secondOp = kOpAnd;
1420 break;
1421 case OP_OR_LONG:
1422 case OP_OR_LONG_2ADDR:
1423 firstOp = kOpOr;
1424 secondOp = kOpOr;
1425 break;
1426 case OP_XOR_LONG:
1427 case OP_XOR_LONG_2ADDR:
1428 firstOp = kOpXor;
1429 secondOp = kOpXor;
1430 break;
1431 case OP_NEG_LONG: {
1432 //TUNING: can improve this using Thumb2 code
1433 int tReg = oatAllocTemp(cUnit);
1434 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1435 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1436 loadConstantNoClobber(cUnit, tReg, 0);
1437 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1438 tReg, rlSrc2.lowReg);
1439 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
1440 genRegCopy(cUnit, rlResult.highReg, tReg);
1441 storeValueWide(cUnit, rlDest, rlResult);
1442 return false;
1443 }
1444 default:
1445 LOG(FATAL) << "Invalid long arith op";
1446 }
1447 if (!callOut) {
1448 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1449 } else {
1450 // Adjust return regs in to handle case of rem returning r2/r3
1451 oatFlushAllRegs(cUnit); /* Send everything to home location */
1452 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1453 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1454 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1455 opReg(cUnit, kOpBlx, rLR);
1456 oatClobberCallRegs(cUnit);
1457 if (retReg == r0)
1458 rlResult = oatGetReturnWide(cUnit);
1459 else
1460 rlResult = oatGetReturnWideAlt(cUnit);
1461 storeValueWide(cUnit, rlDest, rlResult);
1462 }
1463 return false;
1464}
1465
1466static bool genArithOpInt(CompilationUnit* cUnit, MIR* mir,
1467 RegLocation rlDest, RegLocation rlSrc1,
1468 RegLocation rlSrc2)
1469{
1470 OpKind op = kOpBkpt;
1471 bool callOut = false;
1472 bool checkZero = false;
1473 bool unary = false;
1474 int retReg = r0;
1475 int funcOffset;
1476 RegLocation rlResult;
1477 bool shiftOp = false;
1478
1479 switch (mir->dalvikInsn.opcode) {
1480 case OP_NEG_INT:
1481 op = kOpNeg;
1482 unary = true;
1483 break;
1484 case OP_NOT_INT:
1485 op = kOpMvn;
1486 unary = true;
1487 break;
1488 case OP_ADD_INT:
1489 case OP_ADD_INT_2ADDR:
1490 op = kOpAdd;
1491 break;
1492 case OP_SUB_INT:
1493 case OP_SUB_INT_2ADDR:
1494 op = kOpSub;
1495 break;
1496 case OP_MUL_INT:
1497 case OP_MUL_INT_2ADDR:
1498 op = kOpMul;
1499 break;
1500 case OP_DIV_INT:
1501 case OP_DIV_INT_2ADDR:
1502 callOut = true;
1503 checkZero = true;
1504 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1505 retReg = r0;
1506 break;
1507 /* NOTE: returns in r1 */
1508 case OP_REM_INT:
1509 case OP_REM_INT_2ADDR:
1510 callOut = true;
1511 checkZero = true;
1512 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1513 retReg = r1;
1514 break;
1515 case OP_AND_INT:
1516 case OP_AND_INT_2ADDR:
1517 op = kOpAnd;
1518 break;
1519 case OP_OR_INT:
1520 case OP_OR_INT_2ADDR:
1521 op = kOpOr;
1522 break;
1523 case OP_XOR_INT:
1524 case OP_XOR_INT_2ADDR:
1525 op = kOpXor;
1526 break;
1527 case OP_SHL_INT:
1528 case OP_SHL_INT_2ADDR:
1529 shiftOp = true;
1530 op = kOpLsl;
1531 break;
1532 case OP_SHR_INT:
1533 case OP_SHR_INT_2ADDR:
1534 shiftOp = true;
1535 op = kOpAsr;
1536 break;
1537 case OP_USHR_INT:
1538 case OP_USHR_INT_2ADDR:
1539 shiftOp = true;
1540 op = kOpLsr;
1541 break;
1542 default:
1543 LOG(FATAL) << "Invalid word arith op: " <<
1544 (int)mir->dalvikInsn.opcode;
1545 }
1546 if (!callOut) {
1547 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1548 if (unary) {
1549 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1550 opRegReg(cUnit, op, rlResult.lowReg,
1551 rlSrc1.lowReg);
1552 } else {
1553 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1554 if (shiftOp) {
1555 int tReg = oatAllocTemp(cUnit);
1556 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1557 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1558 opRegRegReg(cUnit, op, rlResult.lowReg,
1559 rlSrc1.lowReg, tReg);
1560 oatFreeTemp(cUnit, tReg);
1561 } else {
1562 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1563 opRegRegReg(cUnit, op, rlResult.lowReg,
1564 rlSrc1.lowReg, rlSrc2.lowReg);
1565 }
1566 }
1567 storeValue(cUnit, rlDest, rlResult);
1568 } else {
1569 RegLocation rlResult;
1570 oatFlushAllRegs(cUnit); /* Send everything to home location */
1571 loadValueDirectFixed(cUnit, rlSrc2, r1);
1572 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1573 loadValueDirectFixed(cUnit, rlSrc1, r0);
1574 if (checkZero) {
1575 genNullCheck(cUnit, rlSrc2.sRegLow, r1, mir->offset, NULL);
1576 }
1577 opReg(cUnit, kOpBlx, rLR);
1578 oatClobberCallRegs(cUnit);
1579 if (retReg == r0)
1580 rlResult = oatGetReturn(cUnit);
1581 else
1582 rlResult = oatGetReturnAlt(cUnit);
1583 storeValue(cUnit, rlDest, rlResult);
1584 }
1585 return false;
1586}
1587
buzbee67bf8852011-08-17 17:51:35 -07001588/*
1589 * Fetch *self->info.breakFlags. If the breakFlags are non-zero,
1590 * punt to the interpreter.
1591 */
1592static void genSuspendPoll(CompilationUnit* cUnit, MIR* mir)
1593{
1594 UNIMPLEMENTED(WARNING);
1595#if 0
1596 int rTemp = oatAllocTemp(cUnit);
1597 ArmLIR* ld;
1598 ld = loadBaseDisp(cUnit, NULL, rSELF,
1599 offsetof(Thread, interpBreak.ctl.breakFlags),
1600 rTemp, kUnsignedByte, INVALID_SREG);
1601 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
1602 genRegImmCheck(cUnit, kArmCondNe, rTemp, 0, mir->offset, NULL);
1603#endif
1604}
1605
1606/*
1607 * The following are the first-level codegen routines that analyze the format
1608 * of each bytecode then either dispatch special purpose codegen routines
1609 * or produce corresponding Thumb instructions directly.
1610 */
1611
1612static bool isPowerOfTwo(int x)
1613{
1614 return (x & (x - 1)) == 0;
1615}
1616
1617// Returns true if no more than two bits are set in 'x'.
1618static bool isPopCountLE2(unsigned int x)
1619{
1620 x &= x - 1;
1621 return (x & (x - 1)) == 0;
1622}
1623
1624// Returns the index of the lowest set bit in 'x'.
1625static int lowestSetBit(unsigned int x) {
1626 int bit_posn = 0;
1627 while ((x & 0xf) == 0) {
1628 bit_posn += 4;
1629 x >>= 4;
1630 }
1631 while ((x & 1) == 0) {
1632 bit_posn++;
1633 x >>= 1;
1634 }
1635 return bit_posn;
1636}
1637
1638// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1639// and store the result in 'rlDest'.
1640static bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode,
1641 RegLocation rlSrc, RegLocation rlDest, int lit)
1642{
1643 if (lit < 2 || !isPowerOfTwo(lit)) {
1644 return false;
1645 }
1646 int k = lowestSetBit(lit);
1647 if (k >= 30) {
1648 // Avoid special cases.
1649 return false;
1650 }
1651 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 ||
1652 dalvikOpcode == OP_DIV_INT_LIT16);
1653 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1654 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1655 if (div) {
1656 int tReg = oatAllocTemp(cUnit);
1657 if (lit == 2) {
1658 // Division by 2 is by far the most common division by constant.
1659 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1660 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1661 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1662 } else {
1663 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1664 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1665 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1666 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1667 }
1668 } else {
1669 int cReg = oatAllocTemp(cUnit);
1670 loadConstant(cUnit, cReg, lit - 1);
1671 int tReg1 = oatAllocTemp(cUnit);
1672 int tReg2 = oatAllocTemp(cUnit);
1673 if (lit == 2) {
1674 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1675 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1676 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1677 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1678 } else {
1679 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1680 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1681 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1682 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1683 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1684 }
1685 }
1686 storeValue(cUnit, rlDest, rlResult);
1687 return true;
1688}
1689
1690// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1691// and store the result in 'rlDest'.
1692static bool handleEasyMultiply(CompilationUnit* cUnit,
1693 RegLocation rlSrc, RegLocation rlDest, int lit)
1694{
1695 // Can we simplify this multiplication?
1696 bool powerOfTwo = false;
1697 bool popCountLE2 = false;
1698 bool powerOfTwoMinusOne = false;
1699 if (lit < 2) {
1700 // Avoid special cases.
1701 return false;
1702 } else if (isPowerOfTwo(lit)) {
1703 powerOfTwo = true;
1704 } else if (isPopCountLE2(lit)) {
1705 popCountLE2 = true;
1706 } else if (isPowerOfTwo(lit + 1)) {
1707 powerOfTwoMinusOne = true;
1708 } else {
1709 return false;
1710 }
1711 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1712 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1713 if (powerOfTwo) {
1714 // Shift.
1715 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1716 lowestSetBit(lit));
1717 } else if (popCountLE2) {
1718 // Shift and add and shift.
1719 int firstBit = lowestSetBit(lit);
1720 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1721 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1722 firstBit, secondBit);
1723 } else {
1724 // Reverse subtract: (src << (shift + 1)) - src.
1725 assert(powerOfTwoMinusOne);
1726 // TODO: rsb dst, src, src lsl#lowestSetBit(lit + 1)
1727 int tReg = oatAllocTemp(cUnit);
1728 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1729 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1730 }
1731 storeValue(cUnit, rlDest, rlResult);
1732 return true;
1733}
1734
1735static bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir,
1736 RegLocation rlDest, RegLocation rlSrc,
1737 int lit)
1738{
1739 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1740 RegLocation rlResult;
1741 OpKind op = (OpKind)0; /* Make gcc happy */
1742 int shiftOp = false;
1743 bool isDiv = false;
1744 int funcOffset;
1745
1746 switch (dalvikOpcode) {
1747 case OP_RSUB_INT_LIT8:
1748 case OP_RSUB_INT: {
1749 int tReg;
1750 //TUNING: add support for use of Arm rsub op
1751 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1752 tReg = oatAllocTemp(cUnit);
1753 loadConstant(cUnit, tReg, lit);
1754 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1755 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1756 tReg, rlSrc.lowReg);
1757 storeValue(cUnit, rlDest, rlResult);
1758 return false;
1759 break;
1760 }
1761
1762 case OP_ADD_INT_LIT8:
1763 case OP_ADD_INT_LIT16:
1764 op = kOpAdd;
1765 break;
1766 case OP_MUL_INT_LIT8:
1767 case OP_MUL_INT_LIT16: {
1768 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1769 return false;
1770 }
1771 op = kOpMul;
1772 break;
1773 }
1774 case OP_AND_INT_LIT8:
1775 case OP_AND_INT_LIT16:
1776 op = kOpAnd;
1777 break;
1778 case OP_OR_INT_LIT8:
1779 case OP_OR_INT_LIT16:
1780 op = kOpOr;
1781 break;
1782 case OP_XOR_INT_LIT8:
1783 case OP_XOR_INT_LIT16:
1784 op = kOpXor;
1785 break;
1786 case OP_SHL_INT_LIT8:
1787 lit &= 31;
1788 shiftOp = true;
1789 op = kOpLsl;
1790 break;
1791 case OP_SHR_INT_LIT8:
1792 lit &= 31;
1793 shiftOp = true;
1794 op = kOpAsr;
1795 break;
1796 case OP_USHR_INT_LIT8:
1797 lit &= 31;
1798 shiftOp = true;
1799 op = kOpLsr;
1800 break;
1801
1802 case OP_DIV_INT_LIT8:
1803 case OP_DIV_INT_LIT16:
1804 case OP_REM_INT_LIT8:
1805 case OP_REM_INT_LIT16:
1806 if (lit == 0) {
1807 UNIMPLEMENTED(FATAL);
1808 // FIXME: generate an explicit throw here
1809 return false;
1810 }
1811 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
1812 return false;
1813 }
1814 oatFlushAllRegs(cUnit); /* Everything to home location */
1815 loadValueDirectFixed(cUnit, rlSrc, r0);
1816 oatClobber(cUnit, r0);
1817 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
1818 (dalvikOpcode == OP_DIV_INT_LIT16)) {
1819 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1820 isDiv = true;
1821 } else {
1822 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1823 isDiv = false;
1824 }
1825 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1826 loadConstant(cUnit, r1, lit);
1827 opReg(cUnit, kOpBlx, rLR);
1828 oatClobberCallRegs(cUnit);
1829 if (isDiv)
1830 rlResult = oatGetReturn(cUnit);
1831 else
1832 rlResult = oatGetReturnAlt(cUnit);
1833 storeValue(cUnit, rlDest, rlResult);
1834 return false;
1835 break;
1836 default:
1837 return true;
1838 }
1839 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1840 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1841 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
1842 if (shiftOp && (lit == 0)) {
1843 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1844 } else {
1845 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
1846 }
1847 storeValue(cUnit, rlDest, rlResult);
1848 return false;
1849}
1850
1851/* Architectural-specific debugging helpers go here */
1852void oatArchDump(void)
1853{
1854 /* Print compiled opcode in this VM instance */
1855 int i, start, streak;
1856 char buf[1024];
1857
1858 streak = i = 0;
1859 buf[0] = 0;
1860 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
1861 i++;
1862 }
1863 if (i == kNumPackedOpcodes) {
1864 return;
1865 }
1866 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
1867 if (opcodeCoverage[i]) {
1868 streak++;
1869 } else {
1870 if (streak == 1) {
1871 sprintf(buf+strlen(buf), "%x,", start);
1872 } else {
1873 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
1874 }
1875 streak = 0;
1876 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
1877 i++;
1878 }
1879 if (i < kNumPackedOpcodes) {
1880 streak = 1;
1881 start = i;
1882 }
1883 }
1884 }
1885 if (streak) {
1886 if (streak == 1) {
1887 sprintf(buf+strlen(buf), "%x", start);
1888 } else {
1889 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
1890 }
1891 }
1892 if (strlen(buf)) {
1893 LOG(INFO) << "dalvik.vm.oat.op = " << buf;
1894 }
1895}