blob: 0afbe41c7ec487bebae92c613eb375aee42aa26c [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
buzbeece302932011-10-04 14:32:18 -070025#define SLOW_FIELD_PATH (cUnit->enableDebug & (1 << kDebugSlowFieldPath))
26#define SLOW_INVOKE_PATH (cUnit->enableDebug & (1 << kDebugSlowInvokePath))
27#define SLOW_STRING_PATH (cUnit->enableDebug & (1 << kDebugSlowStringPath))
28#define SLOW_TYPE_PATH (cUnit->enableDebug & (1 << kDebugSlowTypePath))
29#define EXERCISE_SLOWEST_FIELD_PATH (cUnit->enableDebug & \
30 (1 << kDebugSlowestFieldPath))
31#define EXERCISE_SLOWEST_STRING_PATH (cUnit->enableDebug & \
32 (1 << kDebugSlowestStringPath))
buzbee34c77ad2012-01-11 13:01:32 -080033#define EXERCISE_RESOLVE_METHOD (cUnit->enableDebug & \
34 (1 << kDebugExerciseResolveMethod))
buzbeece302932011-10-04 14:32:18 -070035
36STATIC RegLocation getRetLoc(CompilationUnit* cUnit);
buzbee34cd9e52011-09-08 14:31:52 -070037
Elliott Hughes81bc5092011-09-30 17:25:59 -070038void warnIfUnresolved(CompilationUnit* cUnit, int fieldIdx, Field* field) {
39 if (field == NULL) {
Ian Rogersa3760aa2011-11-14 14:32:37 -080040 const art::DexFile::FieldId& field_id = cUnit->dex_file->GetFieldId(fieldIdx);
Elliott Hughes95572412011-12-13 18:14:20 -080041 std::string class_name(cUnit->dex_file->GetFieldDeclaringClassDescriptor(field_id));
42 std::string field_name(cUnit->dex_file->GetFieldName(field_id));
43 LOG(INFO) << "Field " << art::PrettyDescriptor(class_name) << "." << field_name
44 << " unresolved at compile time";
Elliott Hughes81bc5092011-09-30 17:25:59 -070045 } else {
46 // We also use the slow path for wide volatile fields.
47 }
48}
49
buzbee67bf8852011-08-17 17:51:35 -070050/*
51 * Construct an s4 from two consecutive half-words of switch data.
52 * This needs to check endianness because the DEX optimizer only swaps
53 * half-words in instruction stream.
54 *
55 * "switchData" must be 32-bit aligned.
56 */
57#if __BYTE_ORDER == __LITTLE_ENDIAN
buzbeeed3e9302011-09-23 17:34:19 -070058STATIC inline s4 s4FromSwitchData(const void* switchData) {
buzbee67bf8852011-08-17 17:51:35 -070059 return *(s4*) switchData;
60}
61#else
buzbeeed3e9302011-09-23 17:34:19 -070062STATIC inline s4 s4FromSwitchData(const void* switchData) {
buzbee67bf8852011-08-17 17:51:35 -070063 u2* data = switchData;
64 return data[0] | (((s4) data[1]) << 16);
65}
66#endif
67
buzbeeed3e9302011-09-23 17:34:19 -070068STATIC ArmLIR* callRuntimeHelper(CompilationUnit* cUnit, int reg)
buzbeeec5adf32011-09-11 15:25:43 -070069{
buzbee6181f792011-09-29 11:14:04 -070070 oatClobberCalleeSave(cUnit);
buzbeeec5adf32011-09-11 15:25:43 -070071 return opReg(cUnit, kOpBlx, reg);
72}
73
buzbee1b4c8592011-08-31 10:43:51 -070074/* Generate unconditional branch instructions */
buzbeeed3e9302011-09-23 17:34:19 -070075STATIC ArmLIR* genUnconditionalBranch(CompilationUnit* cUnit, ArmLIR* target)
buzbee1b4c8592011-08-31 10:43:51 -070076{
77 ArmLIR* branch = opNone(cUnit, kOpUncondBr);
78 branch->generic.target = (LIR*) target;
79 return branch;
80}
81
buzbee67bf8852011-08-17 17:51:35 -070082/*
83 * Generate a Thumb2 IT instruction, which can nullify up to
84 * four subsequent instructions based on a condition and its
85 * inverse. The condition applies to the first instruction, which
86 * is executed if the condition is met. The string "guide" consists
87 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
88 * A "T" means the instruction is executed if the condition is
89 * met, and an "E" means the instruction is executed if the condition
90 * is not met.
91 */
buzbeeed3e9302011-09-23 17:34:19 -070092STATIC ArmLIR* genIT(CompilationUnit* cUnit, ArmConditionCode code,
buzbee67bf8852011-08-17 17:51:35 -070093 const char* guide)
94{
95 int mask;
96 int condBit = code & 1;
97 int altBit = condBit ^ 1;
98 int mask3 = 0;
99 int mask2 = 0;
100 int mask1 = 0;
101
102 //Note: case fallthroughs intentional
103 switch(strlen(guide)) {
104 case 3:
105 mask1 = (guide[2] == 'T') ? condBit : altBit;
106 case 2:
107 mask2 = (guide[1] == 'T') ? condBit : altBit;
108 case 1:
109 mask3 = (guide[0] == 'T') ? condBit : altBit;
110 break;
111 case 0:
112 break;
113 default:
114 LOG(FATAL) << "OAT: bad case in genIT";
115 }
116 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
117 (1 << (3 - strlen(guide)));
118 return newLIR2(cUnit, kThumb2It, code, mask);
119}
120
121/*
122 * Insert a kArmPseudoCaseLabel at the beginning of the Dalvik
123 * offset vaddr. This label will be used to fix up the case
124 * branch table during the assembly phase. Be sure to set
125 * all resource flags on this to prevent code motion across
126 * target boundaries. KeyVal is just there for debugging.
127 */
buzbeeed3e9302011-09-23 17:34:19 -0700128STATIC ArmLIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
buzbee67bf8852011-08-17 17:51:35 -0700129{
130 ArmLIR* lir;
131 for (lir = (ArmLIR*)cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
132 if ((lir->opcode == kArmPseudoDalvikByteCodeBoundary) &&
133 (lir->generic.dalvikOffset == vaddr)) {
134 ArmLIR* newLabel = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
135 newLabel->generic.dalvikOffset = vaddr;
136 newLabel->opcode = kArmPseudoCaseLabel;
137 newLabel->operands[0] = keyVal;
138 oatInsertLIRAfter((LIR*)lir, (LIR*)newLabel);
139 return newLabel;
140 }
141 }
142 oatCodegenDump(cUnit);
143 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
144 return NULL; // Quiet gcc
145}
146
buzbeeed3e9302011-09-23 17:34:19 -0700147STATIC void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
buzbee67bf8852011-08-17 17:51:35 -0700148{
149 const u2* table = tabRec->table;
150 int baseVaddr = tabRec->vaddr;
151 int *targets = (int*)&table[4];
152 int entries = table[1];
153 int lowKey = s4FromSwitchData(&table[2]);
154 for (int i = 0; i < entries; i++) {
155 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
156 i + lowKey);
157 }
158}
159
buzbeeed3e9302011-09-23 17:34:19 -0700160STATIC void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
buzbee67bf8852011-08-17 17:51:35 -0700161{
162 const u2* table = tabRec->table;
163 int baseVaddr = tabRec->vaddr;
164 int entries = table[1];
165 int* keys = (int*)&table[2];
166 int* targets = &keys[entries];
167 for (int i = 0; i < entries; i++) {
168 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
169 keys[i]);
170 }
171}
172
173void oatProcessSwitchTables(CompilationUnit* cUnit)
174{
175 GrowableListIterator iterator;
176 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
177 while (true) {
178 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
179 &iterator);
180 if (tabRec == NULL) break;
181 if (tabRec->table[0] == kPackedSwitchSignature)
182 markPackedCaseLabels(cUnit, tabRec);
183 else if (tabRec->table[0] == kSparseSwitchSignature)
184 markSparseCaseLabels(cUnit, tabRec);
185 else {
186 LOG(FATAL) << "Invalid switch table";
187 }
188 }
189}
190
buzbeeed3e9302011-09-23 17:34:19 -0700191STATIC void dumpSparseSwitchTable(const u2* table)
buzbee67bf8852011-08-17 17:51:35 -0700192 /*
193 * Sparse switch data format:
194 * ushort ident = 0x0200 magic value
195 * ushort size number of entries in the table; > 0
196 * int keys[size] keys, sorted low-to-high; 32-bit aligned
197 * int targets[size] branch targets, relative to switch opcode
198 *
199 * Total size is (2+size*4) 16-bit code units.
200 */
201{
202 u2 ident = table[0];
203 int entries = table[1];
204 int* keys = (int*)&table[2];
205 int* targets = &keys[entries];
206 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
207 ", entries: " << std::dec << entries;
208 for (int i = 0; i < entries; i++) {
209 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
210 targets[i];
211 }
212}
213
buzbeeed3e9302011-09-23 17:34:19 -0700214STATIC void dumpPackedSwitchTable(const u2* table)
buzbee67bf8852011-08-17 17:51:35 -0700215 /*
216 * Packed switch data format:
217 * ushort ident = 0x0100 magic value
218 * ushort size number of entries in the table
219 * int first_key first (and lowest) switch case value
220 * int targets[size] branch targets, relative to switch opcode
221 *
222 * Total size is (4+size*2) 16-bit code units.
223 */
224{
225 u2 ident = table[0];
226 int* targets = (int*)&table[4];
227 int entries = table[1];
228 int lowKey = s4FromSwitchData(&table[2]);
229 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
230 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
231 for (int i = 0; i < entries; i++) {
232 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
233 targets[i];
234 }
235}
236
237/*
238 * The sparse table in the literal pool is an array of <key,displacement>
239 * pairs. For each set, we'll load them as a pair using ldmia.
240 * This means that the register number of the temp we use for the key
241 * must be lower than the reg for the displacement.
242 *
243 * The test loop will look something like:
244 *
245 * adr rBase, <table>
246 * ldr rVal, [rSP, vRegOff]
247 * mov rIdx, #tableSize
248 * lp:
249 * ldmia rBase!, {rKey, rDisp}
250 * sub rIdx, #1
251 * cmp rVal, rKey
252 * ifeq
253 * add rPC, rDisp ; This is the branch from which we compute displacement
254 * cbnz rIdx, lp
255 */
buzbeeed3e9302011-09-23 17:34:19 -0700256STATIC void genSparseSwitch(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700257 RegLocation rlSrc)
258{
259 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
260 if (cUnit->printMe) {
261 dumpSparseSwitchTable(table);
262 }
263 // Add the table to the list - we'll process it later
264 SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable),
265 true);
266 tabRec->table = table;
267 tabRec->vaddr = mir->offset;
268 int size = table[1];
269 tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true);
270 oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec);
271
272 // Get the switch value
273 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
274 int rBase = oatAllocTemp(cUnit);
275 /* Allocate key and disp temps */
276 int rKey = oatAllocTemp(cUnit);
277 int rDisp = oatAllocTemp(cUnit);
278 // Make sure rKey's register number is less than rDisp's number for ldmia
279 if (rKey > rDisp) {
280 int tmp = rDisp;
281 rDisp = rKey;
282 rKey = tmp;
283 }
284 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700285 newLIR3(cUnit, kThumb2Adr, rBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700286 // Set up rIdx
287 int rIdx = oatAllocTemp(cUnit);
288 loadConstant(cUnit, rIdx, size);
289 // Establish loop branch target
290 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
291 target->defMask = ENCODE_ALL;
292 // Load next key/disp
293 newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp));
294 opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg);
295 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
296 genIT(cUnit, kArmCondEq, "");
297 ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp);
298 tabRec->bxInst = switchBranch;
299 // Needs to use setflags encoding here
300 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
301 ArmLIR* branch = opCondBranch(cUnit, kArmCondNe);
302 branch->generic.target = (LIR*)target;
303}
304
305
buzbeeed3e9302011-09-23 17:34:19 -0700306STATIC void genPackedSwitch(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700307 RegLocation rlSrc)
308{
309 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
310 if (cUnit->printMe) {
311 dumpPackedSwitchTable(table);
312 }
313 // Add the table to the list - we'll process it later
314 SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable),
315 true);
316 tabRec->table = table;
317 tabRec->vaddr = mir->offset;
318 int size = table[1];
319 tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true);
320 oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec);
321
322 // Get the switch value
323 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
324 int tableBase = oatAllocTemp(cUnit);
325 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700326 newLIR3(cUnit, kThumb2Adr, tableBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700327 int lowKey = s4FromSwitchData(&table[2]);
328 int keyReg;
329 // Remove the bias, if necessary
330 if (lowKey == 0) {
331 keyReg = rlSrc.lowReg;
332 } else {
333 keyReg = oatAllocTemp(cUnit);
334 opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
335 }
336 // Bounds check - if < 0 or >= size continue following switch
337 opRegImm(cUnit, kOpCmp, keyReg, size-1);
338 ArmLIR* branchOver = opCondBranch(cUnit, kArmCondHi);
339
340 // Load the displacement from the switch table
341 int dispReg = oatAllocTemp(cUnit);
342 loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord);
343
344 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
345 ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg);
346 tabRec->bxInst = switchBranch;
347
348 /* branchOver target here */
349 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
350 target->defMask = ENCODE_ALL;
351 branchOver->generic.target = (LIR*)target;
352}
353
354/*
355 * Array data table format:
356 * ushort ident = 0x0300 magic value
357 * ushort width width of each element in the table
358 * uint size number of elements in the table
359 * ubyte data[size*width] table of data values (may contain a single-byte
360 * padding at the end)
361 *
362 * Total size is 4+(width * size + 1)/2 16-bit code units.
363 */
buzbeeed3e9302011-09-23 17:34:19 -0700364STATIC void genFillArrayData(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700365 RegLocation rlSrc)
366{
367 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
368 // Add the table to the list - we'll process it later
369 FillArrayData *tabRec = (FillArrayData *)
370 oatNew(sizeof(FillArrayData), true);
371 tabRec->table = table;
372 tabRec->vaddr = mir->offset;
373 u2 width = tabRec->table[1];
374 u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16);
375 tabRec->size = (size * width) + 8;
376
377 oatInsertGrowableList(&cUnit->fillArrayData, (intptr_t)tabRec);
378
379 // Making a call - use explicit registers
380 oatFlushAllRegs(cUnit); /* Everything to home location */
381 loadValueDirectFixed(cUnit, rlSrc, r0);
382 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700383 OFFSETOF_MEMBER(Thread, pHandleFillArrayDataFromCode), rLR);
buzbeee6d61962011-08-27 11:58:19 -0700384 // Materialize a pointer to the fill data image
buzbee03fa2632011-09-20 17:10:57 -0700385 newLIR3(cUnit, kThumb2Adr, r1, 0, (intptr_t)tabRec);
Ian Rogersff1ed472011-09-20 13:46:24 -0700386 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700387}
388
389/*
390 * Mark garbage collection card. Skip if the value we're storing is null.
391 */
buzbeeed3e9302011-09-23 17:34:19 -0700392STATIC void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg)
buzbee67bf8852011-08-17 17:51:35 -0700393{
394 int regCardBase = oatAllocTemp(cUnit);
395 int regCardNo = oatAllocTemp(cUnit);
396 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
buzbeec143c552011-08-20 17:38:58 -0700397 loadWordDisp(cUnit, rSELF, Thread::CardTableOffset().Int32Value(),
buzbee67bf8852011-08-17 17:51:35 -0700398 regCardBase);
399 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
400 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
401 kUnsignedByte);
402 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
403 target->defMask = ENCODE_ALL;
404 branchOver->generic.target = (LIR*)target;
405 oatFreeTemp(cUnit, regCardBase);
406 oatFreeTemp(cUnit, regCardNo);
407}
408
buzbee34cd9e52011-09-08 14:31:52 -0700409/*
410 * Helper function for Iget/put when field not resolved at compile time.
411 * Will trash call temps and return with the field offset in r0.
412 */
Elliott Hughes81bc5092011-09-30 17:25:59 -0700413STATIC void getFieldOffset(CompilationUnit* cUnit, MIR* mir, Field* fieldPtr)
buzbee34cd9e52011-09-08 14:31:52 -0700414{
415 int fieldIdx = mir->dalvikInsn.vC;
buzbee6181f792011-09-29 11:14:04 -0700416 oatFlushAllRegs(cUnit);
Elliott Hughes81bc5092011-09-30 17:25:59 -0700417 warnIfUnresolved(cUnit, fieldIdx, fieldPtr);
buzbee34cd9e52011-09-08 14:31:52 -0700418 oatLockCallTemps(cUnit); // Explicit register usage
419 loadCurrMethodDirect(cUnit, r1); // arg1 <= Method*
420 loadWordDisp(cUnit, r1,
421 Method::DexCacheResolvedFieldsOffset().Int32Value(), r0);
422 loadWordDisp(cUnit, r0, art::Array::DataOffset().Int32Value() +
423 sizeof(int32_t*)* fieldIdx, r0);
424 /*
425 * For testing, omit the test for run-time resolution. This will
426 * force all accesses to go through the runtime resolution path.
427 */
buzbeece302932011-10-04 14:32:18 -0700428 ArmLIR* branchOver = NULL;
429 if (!EXERCISE_SLOWEST_FIELD_PATH) {
430 branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
431 }
buzbee34cd9e52011-09-08 14:31:52 -0700432 // Resolve
433 loadWordDisp(cUnit, rSELF,
Brian Carlstrom845490b2011-09-19 15:56:53 -0700434 OFFSETOF_MEMBER(Thread, pFindInstanceFieldFromCode), rLR);
buzbee34cd9e52011-09-08 14:31:52 -0700435 loadConstant(cUnit, r0, fieldIdx);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700436 callRuntimeHelper(cUnit, rLR); // FindInstanceFieldFromCoderesolveTypeFromCode(idx, method)
buzbee34cd9e52011-09-08 14:31:52 -0700437 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
438 target->defMask = ENCODE_ALL;
buzbeece302932011-10-04 14:32:18 -0700439 if (!EXERCISE_SLOWEST_FIELD_PATH) {
440 branchOver->generic.target = (LIR*)target;
441 }
buzbee34cd9e52011-09-08 14:31:52 -0700442 // Free temps (except for r0)
443 oatFreeTemp(cUnit, r1);
444 oatFreeTemp(cUnit, r2);
445 oatFreeTemp(cUnit, r3);
446 loadWordDisp(cUnit, r0, art::Field::OffsetOffset().Int32Value(), r0);
447}
448
buzbeeed3e9302011-09-23 17:34:19 -0700449STATIC void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -0700450 RegLocation rlDest, RegLocation rlObj)
451{
Ian Rogersa3760aa2011-11-14 14:32:37 -0800452 Field* fieldPtr = cUnit->dex_cache->GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700453 RegLocation rlResult;
454 RegisterClass regClass = oatRegClassBySize(size);
buzbee34cd9e52011-09-08 14:31:52 -0700455 if (SLOW_FIELD_PATH || fieldPtr == NULL) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700456 getFieldOffset(cUnit, mir, fieldPtr);
buzbee34cd9e52011-09-08 14:31:52 -0700457 // Field offset in r0
458 rlObj = loadValue(cUnit, rlObj, kCoreReg);
459 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee5ade1d22011-09-09 14:44:52 -0700460 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee58f92742011-10-01 11:22:17 -0700461 loadBaseIndexed(cUnit, rlObj.lowReg, r0, rlResult.lowReg, 0, kWord);
buzbee67bf8852011-08-17 17:51:35 -0700462 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700463 storeValue(cUnit, rlDest, rlResult);
464 } else {
465#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700466 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700467#else
468 bool isVolatile = false;
469#endif
470 int fieldOffset = fieldPtr->GetOffset().Int32Value();
471 rlObj = loadValue(cUnit, rlObj, kCoreReg);
472 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee5ade1d22011-09-09 14:44:52 -0700473 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee34cd9e52011-09-08 14:31:52 -0700474 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
buzbee58f92742011-10-01 11:22:17 -0700475 kWord, rlObj.sRegLow);
buzbee34cd9e52011-09-08 14:31:52 -0700476 if (isVolatile) {
477 oatGenMemBarrier(cUnit, kSY);
478 }
479 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -0700480 }
buzbee67bf8852011-08-17 17:51:35 -0700481}
482
buzbeeed3e9302011-09-23 17:34:19 -0700483STATIC void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -0700484 RegLocation rlSrc, RegLocation rlObj, bool isObject)
485{
Ian Rogersa3760aa2011-11-14 14:32:37 -0800486 Field* fieldPtr = cUnit->dex_cache->GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700487 RegisterClass regClass = oatRegClassBySize(size);
buzbee34cd9e52011-09-08 14:31:52 -0700488 if (SLOW_FIELD_PATH || fieldPtr == NULL) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700489 getFieldOffset(cUnit, mir, fieldPtr);
buzbee34cd9e52011-09-08 14:31:52 -0700490 // Field offset in r0
491 rlObj = loadValue(cUnit, rlObj, kCoreReg);
492 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee5ade1d22011-09-09 14:44:52 -0700493 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee67bf8852011-08-17 17:51:35 -0700494 oatGenMemBarrier(cUnit, kSY);
buzbee58f92742011-10-01 11:22:17 -0700495 storeBaseIndexed(cUnit, rlObj.lowReg, r0, rlSrc.lowReg, 0, kWord);
buzbee34cd9e52011-09-08 14:31:52 -0700496 } else {
497#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700498 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700499#else
500 bool isVolatile = false;
501#endif
502 int fieldOffset = fieldPtr->GetOffset().Int32Value();
503 rlObj = loadValue(cUnit, rlObj, kCoreReg);
504 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee5ade1d22011-09-09 14:44:52 -0700505 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700506
507 if (isVolatile) {
buzbee12246b82011-09-29 14:15:05 -0700508 oatGenMemBarrier(cUnit, kST);
buzbee34cd9e52011-09-08 14:31:52 -0700509 }
buzbee58f92742011-10-01 11:22:17 -0700510 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
buzbee12246b82011-09-29 14:15:05 -0700511 if (isVolatile) {
512 oatGenMemBarrier(cUnit, kSY);
513 }
buzbee67bf8852011-08-17 17:51:35 -0700514 }
buzbee67bf8852011-08-17 17:51:35 -0700515 if (isObject) {
516 /* NOTE: marking card based on object head */
517 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
518 }
519}
520
buzbeeed3e9302011-09-23 17:34:19 -0700521STATIC void genIGetWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700522 RegLocation rlObj)
523{
buzbee12246b82011-09-29 14:15:05 -0700524 RegLocation rlResult;
Ian Rogersa3760aa2011-11-14 14:32:37 -0800525 Field* fieldPtr = cUnit->dex_cache->GetResolvedField(mir->dalvikInsn.vC);
buzbee12246b82011-09-29 14:15:05 -0700526#if ANDROID_SMP != 0
527 bool isVolatile = (fieldPtr == NULL) || fieldPtr->IsVolatile();
528#else
529 bool isVolatile = false;
530#endif
buzbeece302932011-10-04 14:32:18 -0700531 if (SLOW_FIELD_PATH || (fieldPtr == NULL) || isVolatile) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700532 getFieldOffset(cUnit, mir, fieldPtr);
buzbee34cd9e52011-09-08 14:31:52 -0700533 // Field offset in r0
534 rlObj = loadValue(cUnit, rlObj, kCoreReg);
535 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee5ade1d22011-09-09 14:44:52 -0700536 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700537 opRegReg(cUnit, kOpAdd, r0, rlObj.lowReg);
538 loadPair(cUnit, r0, rlResult.lowReg, rlResult.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700539 oatGenMemBarrier(cUnit, kSY);
buzbee12246b82011-09-29 14:15:05 -0700540 storeValueWide(cUnit, rlDest, rlResult);
buzbee34cd9e52011-09-08 14:31:52 -0700541 } else {
buzbee34cd9e52011-09-08 14:31:52 -0700542 int fieldOffset = fieldPtr->GetOffset().Int32Value();
543 rlObj = loadValue(cUnit, rlObj, kCoreReg);
544 int regPtr = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700545
buzbeeed3e9302011-09-23 17:34:19 -0700546 DCHECK(rlDest.wide);
buzbee34cd9e52011-09-08 14:31:52 -0700547
buzbee5ade1d22011-09-09 14:44:52 -0700548 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700549 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
550 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
551
552 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
553
buzbee34cd9e52011-09-08 14:31:52 -0700554 oatFreeTemp(cUnit, regPtr);
555 storeValueWide(cUnit, rlDest, rlResult);
556 }
buzbee67bf8852011-08-17 17:51:35 -0700557}
558
buzbeeed3e9302011-09-23 17:34:19 -0700559STATIC void genIPutWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700560 RegLocation rlObj)
561{
Ian Rogersa3760aa2011-11-14 14:32:37 -0800562 Field* fieldPtr = cUnit->dex_cache->GetResolvedField(mir->dalvikInsn.vC);
buzbee12246b82011-09-29 14:15:05 -0700563#if ANDROID_SMP != 0
564 bool isVolatile = (fieldPtr == NULL) || fieldPtr->IsVolatile();
565#else
566 bool isVolatile = false;
567#endif
buzbeece302932011-10-04 14:32:18 -0700568 if (SLOW_FIELD_PATH || (fieldPtr == NULL) || isVolatile) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700569 getFieldOffset(cUnit, mir, fieldPtr);
buzbee34cd9e52011-09-08 14:31:52 -0700570 // Field offset in r0
571 rlObj = loadValue(cUnit, rlObj, kCoreReg);
572 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee5ade1d22011-09-09 14:44:52 -0700573 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700574 opRegReg(cUnit, kOpAdd, r0, rlObj.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700575 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700576 storePair(cUnit, r0, rlSrc.lowReg, rlSrc.highReg);
577 } else {
buzbee34cd9e52011-09-08 14:31:52 -0700578 int fieldOffset = fieldPtr->GetOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700579
buzbee34cd9e52011-09-08 14:31:52 -0700580 rlObj = loadValue(cUnit, rlObj, kCoreReg);
581 int regPtr;
582 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee5ade1d22011-09-09 14:44:52 -0700583 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700584 regPtr = oatAllocTemp(cUnit);
585 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
586
buzbee34cd9e52011-09-08 14:31:52 -0700587 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
588
589 oatFreeTemp(cUnit, regPtr);
590 }
buzbee67bf8852011-08-17 17:51:35 -0700591}
592
buzbeeed3e9302011-09-23 17:34:19 -0700593STATIC void genConstClass(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700594 RegLocation rlDest, RegLocation rlSrc)
595{
Ian Rogers28ad40d2011-10-27 15:19:26 -0700596 uint32_t type_idx = mir->dalvikInsn.vB;
buzbee1b4c8592011-08-31 10:43:51 -0700597 int mReg = loadCurrMethod(cUnit);
598 int resReg = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700599 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
Ian Rogersa3760aa2011-11-14 14:32:37 -0800600 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
601 cUnit->dex_cache,
602 *cUnit->dex_file,
603 type_idx)) {
604 // Call out to helper which resolves type and verifies access.
605 // Resolved type returned in r0.
606 loadWordDisp(cUnit, rSELF,
607 OFFSETOF_MEMBER(Thread, pInitializeTypeAndVerifyAccessFromCode),
608 rLR);
609 genRegCopy(cUnit, r1, mReg);
610 loadConstant(cUnit, r0, type_idx);
611 callRuntimeHelper(cUnit, rLR);
612 RegLocation rlResult = oatGetReturn(cUnit);
613 storeValue(cUnit, rlDest, rlResult);
buzbee1b4c8592011-08-31 10:43:51 -0700614 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700615 // We're don't need access checks, load type from dex cache
616 int32_t dex_cache_offset = Method::DexCacheResolvedTypesOffset().Int32Value();
617 loadWordDisp(cUnit, mReg, dex_cache_offset, resReg);
618 int32_t offset_of_type = Array::DataOffset().Int32Value() + (sizeof(Class*) * type_idx);
619 loadWordDisp(cUnit, resReg, offset_of_type, rlResult.lowReg);
Ian Rogersa3760aa2011-11-14 14:32:37 -0800620 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache,
621 type_idx) ||
Ian Rogers28ad40d2011-10-27 15:19:26 -0700622 SLOW_TYPE_PATH) {
623 // Slow path, at runtime test if the type is null and if so initialize
624 oatFlushAllRegs(cUnit);
625 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, rlResult.lowReg, 0);
626 // Resolved, store and hop over following code
627 storeValue(cUnit, rlDest, rlResult);
628 ArmLIR* branch2 = genUnconditionalBranch(cUnit,0);
629 // TUNING: move slow path to end & remove unconditional branch
630 ArmLIR* target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
631 target1->defMask = ENCODE_ALL;
632 // Call out to helper, which will return resolved type in r0
633 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
634 genRegCopy(cUnit, r1, mReg);
635 loadConstant(cUnit, r0, type_idx);
636 callRuntimeHelper(cUnit, rLR);
637 RegLocation rlResult = oatGetReturn(cUnit);
638 storeValue(cUnit, rlDest, rlResult);
639 // Rejoin code paths
640 ArmLIR* target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
641 target2->defMask = ENCODE_ALL;
642 branch1->generic.target = (LIR*)target1;
643 branch2->generic.target = (LIR*)target2;
644 } else {
645 // Fast path, we're done - just store result
646 storeValue(cUnit, rlDest, rlResult);
647 }
buzbee1b4c8592011-08-31 10:43:51 -0700648 }
buzbee67bf8852011-08-17 17:51:35 -0700649}
650
buzbeeed3e9302011-09-23 17:34:19 -0700651STATIC void genConstString(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700652 RegLocation rlDest, RegLocation rlSrc)
653{
buzbeece302932011-10-04 14:32:18 -0700654 /* NOTE: Most strings should be available at compile time */
Ian Rogers28ad40d2011-10-27 15:19:26 -0700655 uint32_t string_idx = mir->dalvikInsn.vB;
656 int32_t offset_of_string = Array::DataOffset().Int32Value() + (sizeof(String*) * string_idx);
Ian Rogersa3760aa2011-11-14 14:32:37 -0800657 if (!cUnit->compiler->CanAssumeStringIsPresentInDexCache(cUnit->dex_cache, string_idx) ||
Ian Rogers28ad40d2011-10-27 15:19:26 -0700658 SLOW_STRING_PATH) {
659 // slow path, resolve string if not in dex cache
buzbeece302932011-10-04 14:32:18 -0700660 oatFlushAllRegs(cUnit);
661 oatLockCallTemps(cUnit); // Using explicit registers
662 loadCurrMethodDirect(cUnit, r2);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700663 loadWordDisp(cUnit, r2, Method::DexCacheStringsOffset().Int32Value(), r0);
buzbeece302932011-10-04 14:32:18 -0700664 // Might call out to helper, which will return resolved string in r0
Ian Rogers28ad40d2011-10-27 15:19:26 -0700665 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pResolveStringFromCode), rLR);
666 loadWordDisp(cUnit, r0, offset_of_string, r0);
667 loadConstant(cUnit, r1, string_idx);
buzbeece302932011-10-04 14:32:18 -0700668 opRegImm(cUnit, kOpCmp, r0, 0); // Is resolved?
669 genBarrier(cUnit);
670 // For testing, always force through helper
671 if (!EXERCISE_SLOWEST_STRING_PATH) {
672 genIT(cUnit, kArmCondEq, "T");
673 }
674 genRegCopy(cUnit, r0, r2); // .eq
675 opReg(cUnit, kOpBlx, rLR); // .eq, helper(Method*, string_idx)
676 genBarrier(cUnit);
677 storeValue(cUnit, rlDest, getRetLoc(cUnit));
678 } else {
679 int mReg = loadCurrMethod(cUnit);
680 int resReg = oatAllocTemp(cUnit);
681 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700682 loadWordDisp(cUnit, mReg, Method::DexCacheStringsOffset().Int32Value(), resReg);
683 loadWordDisp(cUnit, resReg, offset_of_string, rlResult.lowReg);
buzbeece302932011-10-04 14:32:18 -0700684 storeValue(cUnit, rlDest, rlResult);
685 }
buzbee67bf8852011-08-17 17:51:35 -0700686}
687
buzbeedfd3d702011-08-28 12:56:51 -0700688/*
689 * Let helper function take care of everything. Will
690 * call Class::NewInstanceFromCode(type_idx, method);
691 */
buzbeeed3e9302011-09-23 17:34:19 -0700692STATIC void genNewInstance(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700693 RegLocation rlDest)
694{
buzbeedfd3d702011-08-28 12:56:51 -0700695 oatFlushAllRegs(cUnit); /* Everything to home location */
Ian Rogers28ad40d2011-10-27 15:19:26 -0700696 uint32_t type_idx = mir->dalvikInsn.vB;
697 // alloc will always check for resolution, do we also need to verify access because the
698 // verifier was unable to?
Ian Rogersa3760aa2011-11-14 14:32:37 -0800699 if (cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
700 cUnit->dex_cache,
701 *cUnit->dex_file,
702 type_idx)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700703 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pAllocObjectFromCode), rLR);
704 } else {
705 loadWordDisp(cUnit, rSELF,
706 OFFSETOF_MEMBER(Thread, pAllocObjectFromCodeWithAccessCheck), rLR);
707 }
708 loadCurrMethodDirect(cUnit, r1); // arg1 <= Method*
709 loadConstant(cUnit, r0, type_idx); // arg0 <- type_idx
Ian Rogersff1ed472011-09-20 13:46:24 -0700710 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700711 RegLocation rlResult = oatGetReturn(cUnit);
712 storeValue(cUnit, rlDest, rlResult);
713}
714
715void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
716{
buzbee6181f792011-09-29 11:14:04 -0700717 oatFlushAllRegs(cUnit);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700718 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pDeliverException), rLR);
Ian Rogersbdb03912011-09-14 00:55:44 -0700719 loadValueDirectFixed(cUnit, rlSrc, r0); // Get exception object
Ian Rogersff1ed472011-09-20 13:46:24 -0700720 callRuntimeHelper(cUnit, rLR); // art_deliver_exception(exception);
buzbee67bf8852011-08-17 17:51:35 -0700721}
722
buzbeeed3e9302011-09-23 17:34:19 -0700723STATIC void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700724 RegLocation rlSrc)
725{
buzbee6181f792011-09-29 11:14:04 -0700726 oatFlushAllRegs(cUnit);
buzbee2a475e72011-09-07 17:19:17 -0700727 // May generate a call - use explicit registers
728 oatLockCallTemps(cUnit);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700729 uint32_t type_idx = mir->dalvikInsn.vC;
buzbee2a475e72011-09-07 17:19:17 -0700730 loadCurrMethodDirect(cUnit, r1); // r1 <= current Method*
Ian Rogers28ad40d2011-10-27 15:19:26 -0700731 int classReg = r2; // r2 will hold the Class*
Ian Rogersa3760aa2011-11-14 14:32:37 -0800732 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
733 cUnit->dex_cache,
734 *cUnit->dex_file,
735 type_idx)) {
Ian Rogersb093c6b2011-10-31 16:19:55 -0700736 // Check we have access to type_idx and if not throw IllegalAccessError,
737 // returns Class* in r0
738 loadWordDisp(cUnit, rSELF,
739 OFFSETOF_MEMBER(Thread, pInitializeTypeAndVerifyAccessFromCode),
740 rLR);
741 loadConstant(cUnit, r0, type_idx);
742 callRuntimeHelper(cUnit, rLR); // InitializeTypeAndVerifyAccess(idx, method)
743 genRegCopy(cUnit, classReg, r0); // Align usage with fast path
Ian Rogers6a996782011-10-31 17:06:39 -0700744 loadValueDirectFixed(cUnit, rlSrc, r0); // r0 <= ref
Ian Rogers28ad40d2011-10-27 15:19:26 -0700745 } else {
746 // Load dex cache entry into classReg (r2)
Ian Rogers6a996782011-10-31 17:06:39 -0700747 loadValueDirectFixed(cUnit, rlSrc, r0); // r0 <= ref
Ian Rogers28ad40d2011-10-27 15:19:26 -0700748 loadWordDisp(cUnit, r1, Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
749 int32_t offset_of_type = Array::DataOffset().Int32Value() + (sizeof(Class*) * type_idx);
750 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
Ian Rogersa3760aa2011-11-14 14:32:37 -0800751 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache, type_idx)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700752 // Need to test presence of type in dex cache at runtime
753 ArmLIR* hopBranch = genCmpImmBranch(cUnit, kArmCondNe, classReg, 0);
754 // Not resolved
755 // Call out to helper, which will return resolved type in r0
756 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
757 loadConstant(cUnit, r0, type_idx);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700758 callRuntimeHelper(cUnit, rLR); // InitializeTypeFromCode(idx, method)
Ian Rogers28ad40d2011-10-27 15:19:26 -0700759 genRegCopy(cUnit, r2, r0); // Align usage with fast path
760 loadValueDirectFixed(cUnit, rlSrc, r0); /* reload Ref */
761 // Rejoin code paths
762 ArmLIR* hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
763 hopTarget->defMask = ENCODE_ALL;
764 hopBranch->generic.target = (LIR*)hopTarget;
765 }
buzbee67bf8852011-08-17 17:51:35 -0700766 }
buzbee991e3ac2011-09-29 15:44:22 -0700767 /* r0 is ref, r2 is class. If ref==null, use directly as bool result */
768 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
buzbee2a475e72011-09-07 17:19:17 -0700769 /* load object->clazz */
buzbeeed3e9302011-09-23 17:34:19 -0700770 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbee991e3ac2011-09-29 15:44:22 -0700771 loadWordDisp(cUnit, r0, Object::ClassOffset().Int32Value(), r1);
772 /* r0 is ref, r1 is ref->clazz, r2 is class */
Ian Rogers28ad40d2011-10-27 15:19:26 -0700773 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode), rLR);
buzbee991e3ac2011-09-29 15:44:22 -0700774 opRegReg(cUnit, kOpCmp, r1, r2); // Same?
775 genBarrier(cUnit);
776 genIT(cUnit, kArmCondEq, "EE"); // if-convert the test
777 loadConstant(cUnit, r0, 1); // .eq case - load true
778 genRegCopy(cUnit, r0, r2); // .ne case - arg0 <= class
779 opReg(cUnit, kOpBlx, rLR); // .ne case: helper(class, ref->class)
780 genBarrier(cUnit);
781 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700782 /* branch target here */
783 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
784 target->defMask = ENCODE_ALL;
buzbee2a475e72011-09-07 17:19:17 -0700785 RegLocation rlResult = oatGetReturn(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700786 storeValue(cUnit, rlDest, rlResult);
787 branch1->generic.target = (LIR*)target;
buzbee67bf8852011-08-17 17:51:35 -0700788}
789
buzbeeed3e9302011-09-23 17:34:19 -0700790STATIC void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700791{
buzbee6181f792011-09-29 11:14:04 -0700792 oatFlushAllRegs(cUnit);
buzbee2a475e72011-09-07 17:19:17 -0700793 // May generate a call - use explicit registers
794 oatLockCallTemps(cUnit);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700795 uint32_t type_idx = mir->dalvikInsn.vB;
buzbee2a475e72011-09-07 17:19:17 -0700796 loadCurrMethodDirect(cUnit, r1); // r1 <= current Method*
Ian Rogers28ad40d2011-10-27 15:19:26 -0700797 int classReg = r2; // r2 will hold the Class*
Ian Rogersa3760aa2011-11-14 14:32:37 -0800798 if (!cUnit->compiler->CanAccessTypeWithoutChecks(cUnit->method_idx,
799 cUnit->dex_cache,
800 *cUnit->dex_file,
801 type_idx)) {
Ian Rogersb093c6b2011-10-31 16:19:55 -0700802 // Check we have access to type_idx and if not throw IllegalAccessError,
803 // returns Class* in r0
804 loadWordDisp(cUnit, rSELF,
805 OFFSETOF_MEMBER(Thread, pInitializeTypeAndVerifyAccessFromCode),
806 rLR);
807 loadConstant(cUnit, r0, type_idx);
808 callRuntimeHelper(cUnit, rLR); // InitializeTypeAndVerifyAccess(idx, method)
809 genRegCopy(cUnit, classReg, r0); // Align usage with fast path
Ian Rogers28ad40d2011-10-27 15:19:26 -0700810 } else {
811 // Load dex cache entry into classReg (r2)
812 loadWordDisp(cUnit, r1, Method::DexCacheResolvedTypesOffset().Int32Value(), classReg);
813 int32_t offset_of_type = Array::DataOffset().Int32Value() + (sizeof(Class*) * type_idx);
814 loadWordDisp(cUnit, classReg, offset_of_type, classReg);
Ian Rogersa3760aa2011-11-14 14:32:37 -0800815 if (!cUnit->compiler->CanAssumeTypeIsPresentInDexCache(cUnit->dex_cache, type_idx)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -0700816 // Need to test presence of type in dex cache at runtime
817 ArmLIR* hopBranch = genCmpImmBranch(cUnit, kArmCondNe, classReg, 0);
818 // Not resolved
819 // Call out to helper, which will return resolved type in r0
820 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
821 loadConstant(cUnit, r0, type_idx);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700822 callRuntimeHelper(cUnit, rLR); // InitializeTypeFromCode(idx, method)
823 genRegCopy(cUnit, classReg, r0); // Align usage with fast path
Ian Rogers28ad40d2011-10-27 15:19:26 -0700824 // Rejoin code paths
825 ArmLIR* hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
826 hopTarget->defMask = ENCODE_ALL;
827 hopBranch->generic.target = (LIR*)hopTarget;
828 }
buzbee67bf8852011-08-17 17:51:35 -0700829 }
Ian Rogers28ad40d2011-10-27 15:19:26 -0700830 // At this point, classReg (r2) has class
831 loadValueDirectFixed(cUnit, rlSrc, r0); // r0 <= ref
buzbee2a475e72011-09-07 17:19:17 -0700832 /* Null is OK - continue */
833 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
834 /* load object->clazz */
buzbeeed3e9302011-09-23 17:34:19 -0700835 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbee2a475e72011-09-07 17:19:17 -0700836 loadWordDisp(cUnit, r0, Object::ClassOffset().Int32Value(), r1);
837 /* r1 now contains object->clazz */
Ian Rogers28ad40d2011-10-27 15:19:26 -0700838 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pCheckCastFromCode), rLR);
Ian Rogersb093c6b2011-10-31 16:19:55 -0700839 opRegReg(cUnit, kOpCmp, r1, classReg);
buzbee2a475e72011-09-07 17:19:17 -0700840 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondEq); /* If equal, trivial yes */
841 genRegCopy(cUnit, r0, r1);
842 genRegCopy(cUnit, r1, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -0700843 callRuntimeHelper(cUnit, rLR);
buzbee2a475e72011-09-07 17:19:17 -0700844 /* branch target here */
buzbee67bf8852011-08-17 17:51:35 -0700845 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
846 target->defMask = ENCODE_ALL;
847 branch1->generic.target = (LIR*)target;
848 branch2->generic.target = (LIR*)target;
849}
850
buzbeeed3e9302011-09-23 17:34:19 -0700851STATIC void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700852 RegLocation rlSrc)
853{
854 RegLocation rlResult;
855 rlSrc = loadValue(cUnit, rlSrc, kFPReg);
856 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
857 newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg);
858 storeValue(cUnit, rlDest, rlResult);
859}
860
buzbeeed3e9302011-09-23 17:34:19 -0700861STATIC void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700862 RegLocation rlSrc)
863{
864 RegLocation rlResult;
865 rlSrc = loadValueWide(cUnit, rlSrc, kFPReg);
866 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
867 newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg),
868 S2D(rlSrc.lowReg, rlSrc.highReg));
869 storeValueWide(cUnit, rlDest, rlResult);
870}
871
buzbeeed3e9302011-09-23 17:34:19 -0700872STATIC void freeRegLocTemps(CompilationUnit* cUnit, RegLocation rlKeep,
buzbee439c4fa2011-08-27 15:59:07 -0700873 RegLocation rlFree)
buzbee67bf8852011-08-17 17:51:35 -0700874{
buzbee6181f792011-09-29 11:14:04 -0700875 if ((rlFree.lowReg != rlKeep.lowReg) && (rlFree.lowReg != rlKeep.highReg) &&
876 (rlFree.highReg != rlKeep.lowReg) && (rlFree.highReg != rlKeep.highReg)) {
877 // No overlap, free both
buzbee439c4fa2011-08-27 15:59:07 -0700878 oatFreeTemp(cUnit, rlFree.lowReg);
buzbee6181f792011-09-29 11:14:04 -0700879 oatFreeTemp(cUnit, rlFree.highReg);
880 }
buzbee67bf8852011-08-17 17:51:35 -0700881}
882
buzbeeed3e9302011-09-23 17:34:19 -0700883STATIC void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
buzbee67bf8852011-08-17 17:51:35 -0700884 OpKind secondOp, RegLocation rlDest,
885 RegLocation rlSrc1, RegLocation rlSrc2)
886{
buzbee9e0f9b02011-08-24 15:32:46 -0700887 /*
888 * NOTE: This is the one place in the code in which we might have
889 * as many as six live temporary registers. There are 5 in the normal
890 * set for Arm. Until we have spill capabilities, temporarily add
891 * lr to the temp set. It is safe to do this locally, but note that
892 * lr is used explicitly elsewhere in the code generator and cannot
893 * normally be used as a general temp register.
894 */
buzbee67bf8852011-08-17 17:51:35 -0700895 RegLocation rlResult;
buzbee9e0f9b02011-08-24 15:32:46 -0700896 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
897 oatFreeTemp(cUnit, rLR); // and make it available
buzbee67bf8852011-08-17 17:51:35 -0700898 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
899 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
900 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeec0ecd652011-09-25 18:11:54 -0700901 // The longs may overlap - use intermediate temp if so
902 if (rlResult.lowReg == rlSrc1.highReg) {
buzbeec0ecd652011-09-25 18:11:54 -0700903 int tReg = oatAllocTemp(cUnit);
904 genRegCopy(cUnit, tReg, rlSrc1.highReg);
905 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
906 rlSrc2.lowReg);
907 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
908 rlSrc2.highReg);
909 oatFreeTemp(cUnit, tReg);
910 } else {
911 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
912 rlSrc2.lowReg);
913 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
914 rlSrc2.highReg);
915 }
buzbee439c4fa2011-08-27 15:59:07 -0700916 /*
917 * NOTE: If rlDest refers to a frame variable in a large frame, the
918 * following storeValueWide might need to allocate a temp register.
919 * To further work around the lack of a spill capability, explicitly
920 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
921 * Remove when spill is functional.
922 */
923 freeRegLocTemps(cUnit, rlResult, rlSrc1);
924 freeRegLocTemps(cUnit, rlResult, rlSrc2);
buzbee67bf8852011-08-17 17:51:35 -0700925 storeValueWide(cUnit, rlDest, rlResult);
buzbee9e0f9b02011-08-24 15:32:46 -0700926 oatClobber(cUnit, rLR);
927 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee67bf8852011-08-17 17:51:35 -0700928}
929
930void oatInitializeRegAlloc(CompilationUnit* cUnit)
931{
932 int numRegs = sizeof(coreRegs)/sizeof(*coreRegs);
933 int numReserved = sizeof(reservedRegs)/sizeof(*reservedRegs);
934 int numTemps = sizeof(coreTemps)/sizeof(*coreTemps);
935 int numFPRegs = sizeof(fpRegs)/sizeof(*fpRegs);
936 int numFPTemps = sizeof(fpTemps)/sizeof(*fpTemps);
937 RegisterPool *pool = (RegisterPool *)oatNew(sizeof(*pool), true);
938 cUnit->regPool = pool;
939 pool->numCoreRegs = numRegs;
940 pool->coreRegs = (RegisterInfo *)
941 oatNew(numRegs * sizeof(*cUnit->regPool->coreRegs), true);
942 pool->numFPRegs = numFPRegs;
943 pool->FPRegs = (RegisterInfo *)
944 oatNew(numFPRegs * sizeof(*cUnit->regPool->FPRegs), true);
945 oatInitPool(pool->coreRegs, coreRegs, pool->numCoreRegs);
946 oatInitPool(pool->FPRegs, fpRegs, pool->numFPRegs);
947 // Keep special registers from being allocated
948 for (int i = 0; i < numReserved; i++) {
buzbeec0ecd652011-09-25 18:11:54 -0700949 if (NO_SUSPEND && (reservedRegs[i] == rSUSPEND)) {
950 //To measure cost of suspend check
951 continue;
952 }
buzbee67bf8852011-08-17 17:51:35 -0700953 oatMarkInUse(cUnit, reservedRegs[i]);
954 }
955 // Mark temp regs - all others not in use can be used for promotion
956 for (int i = 0; i < numTemps; i++) {
957 oatMarkTemp(cUnit, coreTemps[i]);
958 }
959 for (int i = 0; i < numFPTemps; i++) {
960 oatMarkTemp(cUnit, fpTemps[i]);
961 }
buzbeec0ecd652011-09-25 18:11:54 -0700962 // Construct the alias map.
963 cUnit->phiAliasMap = (int*)oatNew(cUnit->numSSARegs *
964 sizeof(cUnit->phiAliasMap[0]), false);
965 for (int i = 0; i < cUnit->numSSARegs; i++) {
966 cUnit->phiAliasMap[i] = i;
967 }
968 for (MIR* phi = cUnit->phiList; phi; phi = phi->meta.phiNext) {
969 int defReg = phi->ssaRep->defs[0];
970 for (int i = 0; i < phi->ssaRep->numUses; i++) {
971 for (int j = 0; j < cUnit->numSSARegs; j++) {
972 if (cUnit->phiAliasMap[j] == phi->ssaRep->uses[i]) {
973 cUnit->phiAliasMap[j] = defReg;
974 }
975 }
976 }
977 }
buzbee67bf8852011-08-17 17:51:35 -0700978}
979
980/*
981 * Handle simple case (thin lock) inline. If it's complicated, bail
982 * out to the heavyweight lock/unlock routines. We'll use dedicated
983 * registers here in order to be in the right position in case we
984 * to bail to dvm[Lock/Unlock]Object(self, object)
985 *
986 * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object
987 * r1 -> object [arg1 for dvm[Lock/Unlock]Object
988 * r2 -> intial contents of object->lock, later result of strex
989 * r3 -> self->threadId
990 * r12 -> allow to be used by utilities as general temp
991 *
992 * The result of the strex is 0 if we acquire the lock.
993 *
994 * See comments in Sync.c for the layout of the lock word.
995 * Of particular interest to this code is the test for the
996 * simple case - which we handle inline. For monitor enter, the
997 * simple case is thin lock, held by no-one. For monitor exit,
998 * the simple case is thin lock, held by the unlocking thread with
999 * a recurse count of 0.
1000 *
1001 * A minor complication is that there is a field in the lock word
1002 * unrelated to locking: the hash state. This field must be ignored, but
1003 * preserved.
1004 *
1005 */
buzbeeed3e9302011-09-23 17:34:19 -07001006STATIC void genMonitorEnter(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001007 RegLocation rlSrc)
1008{
1009 ArmLIR* target;
1010 ArmLIR* hopTarget;
1011 ArmLIR* branch;
1012 ArmLIR* hopBranch;
1013
1014 oatFlushAllRegs(cUnit);
Elliott Hughes5f791332011-09-15 17:45:30 -07001015 DCHECK_EQ(LW_SHAPE_THIN, 0);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001016 loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj
buzbee2e748f32011-08-29 21:02:19 -07001017 oatLockCallTemps(cUnit); // Prepare for explicit register usage
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001018 genNullCheck(cUnit, rlSrc.sRegLow, r0, mir);
1019 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2);
1020 newLIR3(cUnit, kThumb2Ldrex, r1, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001021 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
buzbeec143c552011-08-20 17:38:58 -07001022 // Align owner
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001023 opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT);
buzbee67bf8852011-08-17 17:51:35 -07001024 // Is lock unheld on lock or held by us (==threadId) on unlock?
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001025 newLIR4(cUnit, kThumb2Bfi, r2, r1, 0, LW_LOCK_OWNER_SHIFT - 1);
1026 newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
1027 hopBranch = newLIR2(cUnit, kThumb2Cbnz, r1, 0);
1028 newLIR4(cUnit, kThumb2Strex, r1, r2, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001029 Object::MonitorOffset().Int32Value() >> 2);
buzbee67bf8852011-08-17 17:51:35 -07001030 oatGenMemBarrier(cUnit, kSY);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001031 branch = newLIR2(cUnit, kThumb2Cbz, r1, 0);
buzbee67bf8852011-08-17 17:51:35 -07001032
1033 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
1034 hopTarget->defMask = ENCODE_ALL;
1035 hopBranch->generic.target = (LIR*)hopTarget;
1036
buzbee1b4c8592011-08-31 10:43:51 -07001037 // Go expensive route - artLockObjectFromCode(self, obj);
1038 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -07001039 rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -07001040 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001041
1042 // Resume here
1043 target = newLIR0(cUnit, kArmPseudoTargetLabel);
1044 target->defMask = ENCODE_ALL;
1045 branch->generic.target = (LIR*)target;
1046}
1047
1048/*
1049 * For monitor unlock, we don't have to use ldrex/strex. Once
1050 * we've determined that the lock is thin and that we own it with
1051 * a zero recursion count, it's safe to punch it back to the
1052 * initial, unlock thin state with a store word.
1053 */
buzbeeed3e9302011-09-23 17:34:19 -07001054STATIC void genMonitorExit(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001055 RegLocation rlSrc)
1056{
1057 ArmLIR* target;
1058 ArmLIR* branch;
1059 ArmLIR* hopTarget;
1060 ArmLIR* hopBranch;
1061
Elliott Hughes5f791332011-09-15 17:45:30 -07001062 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -07001063 oatFlushAllRegs(cUnit);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001064 loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj
buzbee2e748f32011-08-29 21:02:19 -07001065 oatLockCallTemps(cUnit); // Prepare for explicit register usage
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001066 genNullCheck(cUnit, rlSrc.sRegLow, r0, mir);
1067 loadWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r1); // Get lock
1068 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2);
buzbee67bf8852011-08-17 17:51:35 -07001069 // Is lock unheld on lock or held by us (==threadId) on unlock?
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001070 opRegRegImm(cUnit, kOpAnd, r3, r1, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
buzbeec143c552011-08-20 17:38:58 -07001071 // Align owner
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001072 opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT);
1073 newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
1074 opRegReg(cUnit, kOpSub, r1, r2);
buzbee67bf8852011-08-17 17:51:35 -07001075 hopBranch = opCondBranch(cUnit, kArmCondNe);
1076 oatGenMemBarrier(cUnit, kSY);
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001077 storeWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -07001078 branch = opNone(cUnit, kOpUncondBr);
1079
1080 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
1081 hopTarget->defMask = ENCODE_ALL;
1082 hopBranch->generic.target = (LIR*)hopTarget;
1083
Ian Rogers4f0d07c2011-10-06 23:38:47 -07001084 // Go expensive route - UnlockObjectFromCode(obj);
buzbee1b4c8592011-08-31 10:43:51 -07001085 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -07001086 rLR);
Ian Rogersff1ed472011-09-20 13:46:24 -07001087 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001088
1089 // Resume here
1090 target = newLIR0(cUnit, kArmPseudoTargetLabel);
1091 target->defMask = ENCODE_ALL;
1092 branch->generic.target = (LIR*)target;
1093}
1094
1095/*
1096 * 64-bit 3way compare function.
1097 * mov rX, #-1
1098 * cmp op1hi, op2hi
1099 * blt done
1100 * bgt flip
1101 * sub rX, op1lo, op2lo (treat as unsigned)
1102 * beq done
1103 * ite hi
1104 * mov(hi) rX, #-1
1105 * mov(!hi) rX, #1
1106 * flip:
1107 * neg rX
1108 * done:
1109 */
buzbeeed3e9302011-09-23 17:34:19 -07001110STATIC void genCmpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001111 RegLocation rlDest, RegLocation rlSrc1,
1112 RegLocation rlSrc2)
1113{
buzbee67bf8852011-08-17 17:51:35 -07001114 ArmLIR* target1;
1115 ArmLIR* target2;
1116 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1117 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
buzbeeb29e4d12011-09-26 15:05:48 -07001118 int tReg = oatAllocTemp(cUnit);
1119 loadConstant(cUnit, tReg, -1);
buzbee67bf8852011-08-17 17:51:35 -07001120 opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg);
1121 ArmLIR* branch1 = opCondBranch(cUnit, kArmCondLt);
1122 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondGt);
buzbeeb29e4d12011-09-26 15:05:48 -07001123 opRegRegReg(cUnit, kOpSub, tReg, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001124 ArmLIR* branch3 = opCondBranch(cUnit, kArmCondEq);
1125
1126 genIT(cUnit, kArmCondHi, "E");
buzbeeb29e4d12011-09-26 15:05:48 -07001127 newLIR2(cUnit, kThumb2MovImmShift, tReg, modifiedImmediate(-1));
1128 loadConstant(cUnit, tReg, 1);
buzbee67bf8852011-08-17 17:51:35 -07001129 genBarrier(cUnit);
1130
1131 target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
1132 target2->defMask = -1;
buzbeeb29e4d12011-09-26 15:05:48 -07001133 opRegReg(cUnit, kOpNeg, tReg, tReg);
buzbee67bf8852011-08-17 17:51:35 -07001134
1135 target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
1136 target1->defMask = -1;
1137
buzbeeb29e4d12011-09-26 15:05:48 -07001138 RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change
1139 rlTemp.lowReg = tReg;
buzbee67bf8852011-08-17 17:51:35 -07001140 storeValue(cUnit, rlDest, rlTemp);
buzbeeb29e4d12011-09-26 15:05:48 -07001141 oatFreeTemp(cUnit, tReg);
buzbee67bf8852011-08-17 17:51:35 -07001142
1143 branch1->generic.target = (LIR*)target1;
1144 branch2->generic.target = (LIR*)target2;
1145 branch3->generic.target = branch1->generic.target;
1146}
1147
buzbeeed3e9302011-09-23 17:34:19 -07001148STATIC void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001149 RegLocation rlSrc, RegLocation rlResult, int lit,
1150 int firstBit, int secondBit)
1151{
1152 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1153 encodeShift(kArmLsl, secondBit - firstBit));
1154 if (firstBit != 0) {
1155 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1156 }
1157}
1158
buzbeeed3e9302011-09-23 17:34:19 -07001159STATIC bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
buzbee67bf8852011-08-17 17:51:35 -07001160 int srcSize, int tgtSize)
1161{
1162 /*
1163 * Don't optimize the register usage since it calls out to support
1164 * functions
1165 */
1166 RegLocation rlSrc;
1167 RegLocation rlDest;
1168 oatFlushAllRegs(cUnit); /* Send everything to home location */
1169 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1170 if (srcSize == 1) {
1171 rlSrc = oatGetSrc(cUnit, mir, 0);
1172 loadValueDirectFixed(cUnit, rlSrc, r0);
1173 } else {
1174 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
1175 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
1176 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001177 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001178 if (tgtSize == 1) {
1179 RegLocation rlResult;
1180 rlDest = oatGetDest(cUnit, mir, 0);
1181 rlResult = oatGetReturn(cUnit);
1182 storeValue(cUnit, rlDest, rlResult);
1183 } else {
1184 RegLocation rlResult;
1185 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1186 rlResult = oatGetReturnWide(cUnit);
1187 storeValueWide(cUnit, rlDest, rlResult);
1188 }
1189 return false;
1190}
1191
buzbeeed3e9302011-09-23 17:34:19 -07001192STATIC bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001193 RegLocation rlDest, RegLocation rlSrc1,
1194 RegLocation rlSrc2)
1195{
1196 RegLocation rlResult;
1197 int funcOffset;
1198
1199 switch (mir->dalvikInsn.opcode) {
1200 case OP_ADD_FLOAT_2ADDR:
1201 case OP_ADD_FLOAT:
1202 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
1203 break;
1204 case OP_SUB_FLOAT_2ADDR:
1205 case OP_SUB_FLOAT:
1206 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
1207 break;
1208 case OP_DIV_FLOAT_2ADDR:
1209 case OP_DIV_FLOAT:
1210 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
1211 break;
1212 case OP_MUL_FLOAT_2ADDR:
1213 case OP_MUL_FLOAT:
1214 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
1215 break;
1216 case OP_REM_FLOAT_2ADDR:
1217 case OP_REM_FLOAT:
1218 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
1219 break;
1220 case OP_NEG_FLOAT: {
1221 genNegFloat(cUnit, rlDest, rlSrc1);
1222 return false;
1223 }
1224 default:
1225 return true;
1226 }
1227 oatFlushAllRegs(cUnit); /* Send everything to home location */
1228 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1229 loadValueDirectFixed(cUnit, rlSrc1, r0);
1230 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001231 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001232 rlResult = oatGetReturn(cUnit);
1233 storeValue(cUnit, rlDest, rlResult);
1234 return false;
1235}
1236
buzbeeed3e9302011-09-23 17:34:19 -07001237STATIC bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001238 RegLocation rlDest, RegLocation rlSrc1,
1239 RegLocation rlSrc2)
1240{
1241 RegLocation rlResult;
1242 int funcOffset;
1243
1244 switch (mir->dalvikInsn.opcode) {
1245 case OP_ADD_DOUBLE_2ADDR:
1246 case OP_ADD_DOUBLE:
1247 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
1248 break;
1249 case OP_SUB_DOUBLE_2ADDR:
1250 case OP_SUB_DOUBLE:
1251 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
1252 break;
1253 case OP_DIV_DOUBLE_2ADDR:
1254 case OP_DIV_DOUBLE:
1255 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
1256 break;
1257 case OP_MUL_DOUBLE_2ADDR:
1258 case OP_MUL_DOUBLE:
1259 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
1260 break;
1261 case OP_REM_DOUBLE_2ADDR:
1262 case OP_REM_DOUBLE:
1263 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
1264 break;
1265 case OP_NEG_DOUBLE: {
1266 genNegDouble(cUnit, rlDest, rlSrc1);
1267 return false;
1268 }
1269 default:
1270 return true;
1271 }
1272 oatFlushAllRegs(cUnit); /* Send everything to home location */
1273 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1274 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1275 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -07001276 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001277 rlResult = oatGetReturnWide(cUnit);
1278 storeValueWide(cUnit, rlDest, rlResult);
1279 return false;
1280}
1281
buzbeeed3e9302011-09-23 17:34:19 -07001282STATIC bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001283{
1284 Opcode opcode = mir->dalvikInsn.opcode;
1285
1286 switch (opcode) {
1287 case OP_INT_TO_FLOAT:
1288 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
1289 1, 1);
1290 case OP_FLOAT_TO_INT:
1291 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
1292 1, 1);
1293 case OP_DOUBLE_TO_FLOAT:
1294 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
1295 2, 1);
1296 case OP_FLOAT_TO_DOUBLE:
1297 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
1298 1, 2);
1299 case OP_INT_TO_DOUBLE:
1300 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
1301 1, 2);
1302 case OP_DOUBLE_TO_INT:
1303 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
1304 2, 1);
1305 case OP_FLOAT_TO_LONG:
1306 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001307 pF2l), 1, 2);
buzbee67bf8852011-08-17 17:51:35 -07001308 case OP_LONG_TO_FLOAT:
1309 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
1310 2, 1);
1311 case OP_DOUBLE_TO_LONG:
1312 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001313 pD2l), 2, 2);
buzbee67bf8852011-08-17 17:51:35 -07001314 case OP_LONG_TO_DOUBLE:
1315 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
1316 2, 2);
1317 default:
1318 return true;
1319 }
1320 return false;
1321}
1322
1323/* Generate conditional branch instructions */
buzbeeed3e9302011-09-23 17:34:19 -07001324STATIC ArmLIR* genConditionalBranch(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001325 ArmConditionCode cond,
1326 ArmLIR* target)
1327{
1328 ArmLIR* branch = opCondBranch(cUnit, cond);
1329 branch->generic.target = (LIR*) target;
1330 return branch;
1331}
1332
buzbee67bf8852011-08-17 17:51:35 -07001333/*
1334 * Generate array store
1335 *
1336 */
buzbeeed3e9302011-09-23 17:34:19 -07001337STATIC void genArrayObjPut(CompilationUnit* cUnit, MIR* mir,
buzbee1b4c8592011-08-31 10:43:51 -07001338 RegLocation rlArray, RegLocation rlIndex,
1339 RegLocation rlSrc, int scale)
buzbee67bf8852011-08-17 17:51:35 -07001340{
1341 RegisterClass regClass = oatRegClassBySize(kWord);
buzbeec143c552011-08-20 17:38:58 -07001342 int lenOffset = Array::LengthOffset().Int32Value();
1343 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001344
buzbee6181f792011-09-29 11:14:04 -07001345 oatFlushAllRegs(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001346 /* Make sure it's a legal object Put. Use direct regs at first */
1347 loadValueDirectFixed(cUnit, rlArray, r1);
1348 loadValueDirectFixed(cUnit, rlSrc, r0);
1349
1350 /* null array object? */
buzbee43a36422011-09-14 14:00:13 -07001351 genNullCheck(cUnit, rlArray.sRegLow, r1, mir);
buzbee67bf8852011-08-17 17:51:35 -07001352 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -07001353 OFFSETOF_MEMBER(Thread, pCanPutArrayElementFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -07001354 /* Get the array's clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001355 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001356 callRuntimeHelper(cUnit, rLR);
buzbee6181f792011-09-29 11:14:04 -07001357 oatFreeTemp(cUnit, r0);
1358 oatFreeTemp(cUnit, r1);
buzbee67bf8852011-08-17 17:51:35 -07001359
1360 // Now, redo loadValues in case they didn't survive the call
1361
1362 int regPtr;
1363 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1364 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1365
1366 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1367 oatClobber(cUnit, rlArray.lowReg);
1368 regPtr = rlArray.lowReg;
1369 } else {
1370 regPtr = oatAllocTemp(cUnit);
1371 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1372 }
1373
buzbee43a36422011-09-14 14:00:13 -07001374 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001375 int regLen = oatAllocTemp(cUnit);
1376 //NOTE: max live temps(4) here.
1377 /* Get len */
1378 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1379 /* regPtr -> array data */
1380 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001381 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001382 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001383 oatFreeTemp(cUnit, regLen);
1384 } else {
1385 /* regPtr -> array data */
1386 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1387 }
1388 /* at this point, regPtr points to array, 2 live temps */
1389 rlSrc = loadValue(cUnit, rlSrc, regClass);
1390 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1391 scale, kWord);
1392}
1393
1394/*
1395 * Generate array load
1396 */
buzbeeed3e9302011-09-23 17:34:19 -07001397STATIC void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -07001398 RegLocation rlArray, RegLocation rlIndex,
1399 RegLocation rlDest, int scale)
1400{
1401 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001402 int lenOffset = Array::LengthOffset().Int32Value();
1403 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001404 RegLocation rlResult;
1405 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1406 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1407 int regPtr;
1408
1409 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001410 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001411
1412 regPtr = oatAllocTemp(cUnit);
1413
buzbee43a36422011-09-14 14:00:13 -07001414 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001415 int regLen = oatAllocTemp(cUnit);
1416 /* Get len */
1417 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1418 /* regPtr -> array data */
1419 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001420 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001421 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001422 oatFreeTemp(cUnit, regLen);
1423 } else {
1424 /* regPtr -> array data */
1425 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1426 }
buzbeee9a72f62011-09-04 17:59:07 -07001427 oatFreeTemp(cUnit, rlArray.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001428 if ((size == kLong) || (size == kDouble)) {
1429 if (scale) {
1430 int rNewIndex = oatAllocTemp(cUnit);
1431 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1432 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1433 oatFreeTemp(cUnit, rNewIndex);
1434 } else {
1435 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1436 }
buzbeee9a72f62011-09-04 17:59:07 -07001437 oatFreeTemp(cUnit, rlIndex.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001438 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1439
1440 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1441
1442 oatFreeTemp(cUnit, regPtr);
1443 storeValueWide(cUnit, rlDest, rlResult);
1444 } else {
1445 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1446
1447 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1448 scale, size);
1449
1450 oatFreeTemp(cUnit, regPtr);
1451 storeValue(cUnit, rlDest, rlResult);
1452 }
1453}
1454
1455/*
1456 * Generate array store
1457 *
1458 */
buzbeeed3e9302011-09-23 17:34:19 -07001459STATIC void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -07001460 RegLocation rlArray, RegLocation rlIndex,
1461 RegLocation rlSrc, int scale)
1462{
1463 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001464 int lenOffset = Array::LengthOffset().Int32Value();
1465 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001466
1467 int regPtr;
1468 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1469 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1470
1471 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1472 oatClobber(cUnit, rlArray.lowReg);
1473 regPtr = rlArray.lowReg;
1474 } else {
1475 regPtr = oatAllocTemp(cUnit);
1476 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1477 }
1478
1479 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001480 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001481
buzbee43a36422011-09-14 14:00:13 -07001482 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001483 int regLen = oatAllocTemp(cUnit);
1484 //NOTE: max live temps(4) here.
1485 /* Get len */
1486 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1487 /* regPtr -> array data */
1488 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001489 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001490 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001491 oatFreeTemp(cUnit, regLen);
1492 } else {
1493 /* regPtr -> array data */
1494 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1495 }
1496 /* at this point, regPtr points to array, 2 live temps */
1497 if ((size == kLong) || (size == kDouble)) {
buzbee5ade1d22011-09-09 14:44:52 -07001498 //TUNING: specific wide routine that can handle fp regs
buzbee67bf8852011-08-17 17:51:35 -07001499 if (scale) {
1500 int rNewIndex = oatAllocTemp(cUnit);
1501 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1502 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1503 oatFreeTemp(cUnit, rNewIndex);
1504 } else {
1505 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1506 }
1507 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1508
1509 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1510
1511 oatFreeTemp(cUnit, regPtr);
1512 } else {
1513 rlSrc = loadValue(cUnit, rlSrc, regClass);
1514
1515 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1516 scale, size);
1517 }
1518}
1519
buzbeeed3e9302011-09-23 17:34:19 -07001520STATIC bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001521 RegLocation rlDest, RegLocation rlSrc1,
1522 RegLocation rlShift)
1523{
buzbee54330722011-08-23 16:46:55 -07001524 int funcOffset;
buzbee67bf8852011-08-17 17:51:35 -07001525
buzbee67bf8852011-08-17 17:51:35 -07001526 switch( mir->dalvikInsn.opcode) {
1527 case OP_SHL_LONG:
1528 case OP_SHL_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001529 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
buzbee67bf8852011-08-17 17:51:35 -07001530 break;
1531 case OP_SHR_LONG:
1532 case OP_SHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001533 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
buzbee67bf8852011-08-17 17:51:35 -07001534 break;
1535 case OP_USHR_LONG:
1536 case OP_USHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001537 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
buzbee67bf8852011-08-17 17:51:35 -07001538 break;
1539 default:
buzbee54330722011-08-23 16:46:55 -07001540 LOG(FATAL) << "Unexpected case";
buzbee67bf8852011-08-17 17:51:35 -07001541 return true;
1542 }
buzbee54330722011-08-23 16:46:55 -07001543 oatFlushAllRegs(cUnit); /* Send everything to home location */
1544 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1545 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1546 loadValueDirect(cUnit, rlShift, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -07001547 callRuntimeHelper(cUnit, rLR);
buzbee54330722011-08-23 16:46:55 -07001548 RegLocation rlResult = oatGetReturnWide(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001549 storeValueWide(cUnit, rlDest, rlResult);
1550 return false;
1551}
1552
buzbeeed3e9302011-09-23 17:34:19 -07001553STATIC bool genArithOpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001554 RegLocation rlDest, RegLocation rlSrc1,
1555 RegLocation rlSrc2)
1556{
1557 RegLocation rlResult;
1558 OpKind firstOp = kOpBkpt;
1559 OpKind secondOp = kOpBkpt;
1560 bool callOut = false;
buzbee58f92742011-10-01 11:22:17 -07001561 bool checkZero = false;
buzbee67bf8852011-08-17 17:51:35 -07001562 int funcOffset;
1563 int retReg = r0;
1564
1565 switch (mir->dalvikInsn.opcode) {
1566 case OP_NOT_LONG:
1567 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1568 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeeb29e4d12011-09-26 15:05:48 -07001569 // Check for destructive overlap
1570 if (rlResult.lowReg == rlSrc2.highReg) {
1571 int tReg = oatAllocTemp(cUnit);
1572 genRegCopy(cUnit, tReg, rlSrc2.highReg);
1573 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1574 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
1575 oatFreeTemp(cUnit, tReg);
1576 } else {
1577 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1578 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1579 }
buzbee67bf8852011-08-17 17:51:35 -07001580 storeValueWide(cUnit, rlDest, rlResult);
1581 return false;
1582 break;
1583 case OP_ADD_LONG:
1584 case OP_ADD_LONG_2ADDR:
1585 firstOp = kOpAdd;
1586 secondOp = kOpAdc;
1587 break;
1588 case OP_SUB_LONG:
1589 case OP_SUB_LONG_2ADDR:
1590 firstOp = kOpSub;
1591 secondOp = kOpSbc;
1592 break;
1593 case OP_MUL_LONG:
1594 case OP_MUL_LONG_2ADDR:
buzbee439c4fa2011-08-27 15:59:07 -07001595 callOut = true;
1596 retReg = r0;
1597 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1598 break;
buzbee67bf8852011-08-17 17:51:35 -07001599 case OP_DIV_LONG:
1600 case OP_DIV_LONG_2ADDR:
1601 callOut = true;
buzbee58f92742011-10-01 11:22:17 -07001602 checkZero = true;
buzbee67bf8852011-08-17 17:51:35 -07001603 retReg = r0;
1604 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1605 break;
1606 /* NOTE - result is in r2/r3 instead of r0/r1 */
1607 case OP_REM_LONG:
1608 case OP_REM_LONG_2ADDR:
1609 callOut = true;
buzbee58f92742011-10-01 11:22:17 -07001610 checkZero = true;
buzbee67bf8852011-08-17 17:51:35 -07001611 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1612 retReg = r2;
1613 break;
1614 case OP_AND_LONG_2ADDR:
1615 case OP_AND_LONG:
1616 firstOp = kOpAnd;
1617 secondOp = kOpAnd;
1618 break;
1619 case OP_OR_LONG:
1620 case OP_OR_LONG_2ADDR:
1621 firstOp = kOpOr;
1622 secondOp = kOpOr;
1623 break;
1624 case OP_XOR_LONG:
1625 case OP_XOR_LONG_2ADDR:
1626 firstOp = kOpXor;
1627 secondOp = kOpXor;
1628 break;
1629 case OP_NEG_LONG: {
buzbee67bf8852011-08-17 17:51:35 -07001630 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1631 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeeb29e4d12011-09-26 15:05:48 -07001632 int zReg = oatAllocTemp(cUnit);
1633 loadConstantNoClobber(cUnit, zReg, 0);
1634 // Check for destructive overlap
1635 if (rlResult.lowReg == rlSrc2.highReg) {
1636 int tReg = oatAllocTemp(cUnit);
1637 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1638 zReg, rlSrc2.lowReg);
1639 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1640 zReg, tReg);
1641 oatFreeTemp(cUnit, tReg);
1642 } else {
1643 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1644 zReg, rlSrc2.lowReg);
1645 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1646 zReg, rlSrc2.highReg);
1647 }
1648 oatFreeTemp(cUnit, zReg);
buzbee67bf8852011-08-17 17:51:35 -07001649 storeValueWide(cUnit, rlDest, rlResult);
1650 return false;
1651 }
1652 default:
1653 LOG(FATAL) << "Invalid long arith op";
1654 }
1655 if (!callOut) {
1656 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1657 } else {
buzbee67bf8852011-08-17 17:51:35 -07001658 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee58f92742011-10-01 11:22:17 -07001659 if (checkZero) {
1660 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1661 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1662 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1663 int tReg = oatAllocTemp(cUnit);
1664 newLIR4(cUnit, kThumb2OrrRRRs, tReg, r2, r3, 0);
1665 oatFreeTemp(cUnit, tReg);
1666 genCheck(cUnit, kArmCondEq, mir, kArmThrowDivZero);
1667 } else {
1668 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1669 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1670 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1671 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001672 callRuntimeHelper(cUnit, rLR);
buzbee58f92742011-10-01 11:22:17 -07001673 // Adjust return regs in to handle case of rem returning r2/r3
buzbee67bf8852011-08-17 17:51:35 -07001674 if (retReg == r0)
1675 rlResult = oatGetReturnWide(cUnit);
1676 else
1677 rlResult = oatGetReturnWideAlt(cUnit);
1678 storeValueWide(cUnit, rlDest, rlResult);
1679 }
1680 return false;
1681}
1682
buzbeeed3e9302011-09-23 17:34:19 -07001683STATIC bool genArithOpInt(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001684 RegLocation rlDest, RegLocation rlSrc1,
1685 RegLocation rlSrc2)
1686{
1687 OpKind op = kOpBkpt;
1688 bool callOut = false;
1689 bool checkZero = false;
1690 bool unary = false;
1691 int retReg = r0;
1692 int funcOffset;
1693 RegLocation rlResult;
1694 bool shiftOp = false;
1695
1696 switch (mir->dalvikInsn.opcode) {
1697 case OP_NEG_INT:
1698 op = kOpNeg;
1699 unary = true;
1700 break;
1701 case OP_NOT_INT:
1702 op = kOpMvn;
1703 unary = true;
1704 break;
1705 case OP_ADD_INT:
1706 case OP_ADD_INT_2ADDR:
1707 op = kOpAdd;
1708 break;
1709 case OP_SUB_INT:
1710 case OP_SUB_INT_2ADDR:
1711 op = kOpSub;
1712 break;
1713 case OP_MUL_INT:
1714 case OP_MUL_INT_2ADDR:
1715 op = kOpMul;
1716 break;
1717 case OP_DIV_INT:
1718 case OP_DIV_INT_2ADDR:
1719 callOut = true;
1720 checkZero = true;
1721 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1722 retReg = r0;
1723 break;
1724 /* NOTE: returns in r1 */
1725 case OP_REM_INT:
1726 case OP_REM_INT_2ADDR:
1727 callOut = true;
1728 checkZero = true;
1729 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1730 retReg = r1;
1731 break;
1732 case OP_AND_INT:
1733 case OP_AND_INT_2ADDR:
1734 op = kOpAnd;
1735 break;
1736 case OP_OR_INT:
1737 case OP_OR_INT_2ADDR:
1738 op = kOpOr;
1739 break;
1740 case OP_XOR_INT:
1741 case OP_XOR_INT_2ADDR:
1742 op = kOpXor;
1743 break;
1744 case OP_SHL_INT:
1745 case OP_SHL_INT_2ADDR:
1746 shiftOp = true;
1747 op = kOpLsl;
1748 break;
1749 case OP_SHR_INT:
1750 case OP_SHR_INT_2ADDR:
1751 shiftOp = true;
1752 op = kOpAsr;
1753 break;
1754 case OP_USHR_INT:
1755 case OP_USHR_INT_2ADDR:
1756 shiftOp = true;
1757 op = kOpLsr;
1758 break;
1759 default:
1760 LOG(FATAL) << "Invalid word arith op: " <<
1761 (int)mir->dalvikInsn.opcode;
1762 }
1763 if (!callOut) {
1764 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1765 if (unary) {
1766 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1767 opRegReg(cUnit, op, rlResult.lowReg,
1768 rlSrc1.lowReg);
1769 } else {
1770 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1771 if (shiftOp) {
1772 int tReg = oatAllocTemp(cUnit);
1773 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1774 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1775 opRegRegReg(cUnit, op, rlResult.lowReg,
1776 rlSrc1.lowReg, tReg);
1777 oatFreeTemp(cUnit, tReg);
1778 } else {
1779 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1780 opRegRegReg(cUnit, op, rlResult.lowReg,
1781 rlSrc1.lowReg, rlSrc2.lowReg);
1782 }
1783 }
1784 storeValue(cUnit, rlDest, rlResult);
1785 } else {
1786 RegLocation rlResult;
1787 oatFlushAllRegs(cUnit); /* Send everything to home location */
1788 loadValueDirectFixed(cUnit, rlSrc2, r1);
1789 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1790 loadValueDirectFixed(cUnit, rlSrc1, r0);
1791 if (checkZero) {
buzbee5ade1d22011-09-09 14:44:52 -07001792 genImmedCheck(cUnit, kArmCondEq, r1, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07001793 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001794 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001795 if (retReg == r0)
1796 rlResult = oatGetReturn(cUnit);
1797 else
1798 rlResult = oatGetReturnAlt(cUnit);
1799 storeValue(cUnit, rlDest, rlResult);
1800 }
1801 return false;
1802}
1803
buzbeec1f45042011-09-21 16:03:19 -07001804/* Check if we need to check for pending suspend request */
buzbeeed3e9302011-09-23 17:34:19 -07001805STATIC void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
buzbeec1f45042011-09-21 16:03:19 -07001806{
Ian Rogersa3760aa2011-11-14 14:32:37 -08001807 if (NO_SUSPEND || (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK)) {
buzbeec1f45042011-09-21 16:03:19 -07001808 return;
1809 }
buzbee6181f792011-09-29 11:14:04 -07001810 oatFlushAllRegs(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07001811 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
1812 ArmLIR* branch = opCondBranch(cUnit, kArmCondEq);
1813 ArmLIR* retLab = newLIR0(cUnit, kArmPseudoTargetLabel);
1814 retLab->defMask = ENCODE_ALL;
1815 ArmLIR* target = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
1816 target->generic.dalvikOffset = cUnit->currentDalvikOffset;
1817 target->opcode = kArmPseudoSuspendTarget;
1818 target->operands[0] = (intptr_t)retLab;
1819 target->operands[1] = mir->offset;
1820 branch->generic.target = (LIR*)target;
1821 oatInsertGrowableList(&cUnit->suspendLaunchpads, (intptr_t)target);
1822}
1823
buzbee67bf8852011-08-17 17:51:35 -07001824/*
1825 * The following are the first-level codegen routines that analyze the format
1826 * of each bytecode then either dispatch special purpose codegen routines
1827 * or produce corresponding Thumb instructions directly.
1828 */
1829
buzbeeed3e9302011-09-23 17:34:19 -07001830STATIC bool isPowerOfTwo(int x)
buzbee67bf8852011-08-17 17:51:35 -07001831{
1832 return (x & (x - 1)) == 0;
1833}
1834
1835// Returns true if no more than two bits are set in 'x'.
buzbeeed3e9302011-09-23 17:34:19 -07001836STATIC bool isPopCountLE2(unsigned int x)
buzbee67bf8852011-08-17 17:51:35 -07001837{
1838 x &= x - 1;
1839 return (x & (x - 1)) == 0;
1840}
1841
1842// Returns the index of the lowest set bit in 'x'.
buzbeeed3e9302011-09-23 17:34:19 -07001843STATIC int lowestSetBit(unsigned int x) {
buzbee67bf8852011-08-17 17:51:35 -07001844 int bit_posn = 0;
1845 while ((x & 0xf) == 0) {
1846 bit_posn += 4;
1847 x >>= 4;
1848 }
1849 while ((x & 1) == 0) {
1850 bit_posn++;
1851 x >>= 1;
1852 }
1853 return bit_posn;
1854}
1855
1856// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1857// and store the result in 'rlDest'.
buzbeeed3e9302011-09-23 17:34:19 -07001858STATIC bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode,
buzbee67bf8852011-08-17 17:51:35 -07001859 RegLocation rlSrc, RegLocation rlDest, int lit)
1860{
1861 if (lit < 2 || !isPowerOfTwo(lit)) {
1862 return false;
1863 }
1864 int k = lowestSetBit(lit);
1865 if (k >= 30) {
1866 // Avoid special cases.
1867 return false;
1868 }
1869 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 ||
1870 dalvikOpcode == OP_DIV_INT_LIT16);
1871 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1872 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1873 if (div) {
1874 int tReg = oatAllocTemp(cUnit);
1875 if (lit == 2) {
1876 // Division by 2 is by far the most common division by constant.
1877 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1878 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1879 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1880 } else {
1881 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1882 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1883 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1884 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1885 }
1886 } else {
1887 int cReg = oatAllocTemp(cUnit);
1888 loadConstant(cUnit, cReg, lit - 1);
1889 int tReg1 = oatAllocTemp(cUnit);
1890 int tReg2 = oatAllocTemp(cUnit);
1891 if (lit == 2) {
1892 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1893 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1894 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1895 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1896 } else {
1897 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1898 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1899 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1900 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1901 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1902 }
1903 }
1904 storeValue(cUnit, rlDest, rlResult);
1905 return true;
1906}
1907
1908// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1909// and store the result in 'rlDest'.
buzbeeed3e9302011-09-23 17:34:19 -07001910STATIC bool handleEasyMultiply(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001911 RegLocation rlSrc, RegLocation rlDest, int lit)
1912{
1913 // Can we simplify this multiplication?
1914 bool powerOfTwo = false;
1915 bool popCountLE2 = false;
1916 bool powerOfTwoMinusOne = false;
1917 if (lit < 2) {
1918 // Avoid special cases.
1919 return false;
1920 } else if (isPowerOfTwo(lit)) {
1921 powerOfTwo = true;
1922 } else if (isPopCountLE2(lit)) {
1923 popCountLE2 = true;
1924 } else if (isPowerOfTwo(lit + 1)) {
1925 powerOfTwoMinusOne = true;
1926 } else {
1927 return false;
1928 }
1929 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1930 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1931 if (powerOfTwo) {
1932 // Shift.
1933 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1934 lowestSetBit(lit));
1935 } else if (popCountLE2) {
1936 // Shift and add and shift.
1937 int firstBit = lowestSetBit(lit);
1938 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1939 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1940 firstBit, secondBit);
1941 } else {
1942 // Reverse subtract: (src << (shift + 1)) - src.
buzbeeed3e9302011-09-23 17:34:19 -07001943 DCHECK(powerOfTwoMinusOne);
buzbee5ade1d22011-09-09 14:44:52 -07001944 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
buzbee67bf8852011-08-17 17:51:35 -07001945 int tReg = oatAllocTemp(cUnit);
1946 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1947 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1948 }
1949 storeValue(cUnit, rlDest, rlResult);
1950 return true;
1951}
1952
buzbeeed3e9302011-09-23 17:34:19 -07001953STATIC bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001954 RegLocation rlDest, RegLocation rlSrc,
1955 int lit)
1956{
1957 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1958 RegLocation rlResult;
1959 OpKind op = (OpKind)0; /* Make gcc happy */
1960 int shiftOp = false;
1961 bool isDiv = false;
1962 int funcOffset;
1963
1964 switch (dalvikOpcode) {
1965 case OP_RSUB_INT_LIT8:
1966 case OP_RSUB_INT: {
1967 int tReg;
1968 //TUNING: add support for use of Arm rsub op
1969 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1970 tReg = oatAllocTemp(cUnit);
1971 loadConstant(cUnit, tReg, lit);
1972 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1973 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1974 tReg, rlSrc.lowReg);
1975 storeValue(cUnit, rlDest, rlResult);
1976 return false;
1977 break;
1978 }
1979
1980 case OP_ADD_INT_LIT8:
1981 case OP_ADD_INT_LIT16:
1982 op = kOpAdd;
1983 break;
1984 case OP_MUL_INT_LIT8:
1985 case OP_MUL_INT_LIT16: {
1986 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1987 return false;
1988 }
1989 op = kOpMul;
1990 break;
1991 }
1992 case OP_AND_INT_LIT8:
1993 case OP_AND_INT_LIT16:
1994 op = kOpAnd;
1995 break;
1996 case OP_OR_INT_LIT8:
1997 case OP_OR_INT_LIT16:
1998 op = kOpOr;
1999 break;
2000 case OP_XOR_INT_LIT8:
2001 case OP_XOR_INT_LIT16:
2002 op = kOpXor;
2003 break;
2004 case OP_SHL_INT_LIT8:
2005 lit &= 31;
2006 shiftOp = true;
2007 op = kOpLsl;
2008 break;
2009 case OP_SHR_INT_LIT8:
2010 lit &= 31;
2011 shiftOp = true;
2012 op = kOpAsr;
2013 break;
2014 case OP_USHR_INT_LIT8:
2015 lit &= 31;
2016 shiftOp = true;
2017 op = kOpLsr;
2018 break;
2019
2020 case OP_DIV_INT_LIT8:
2021 case OP_DIV_INT_LIT16:
2022 case OP_REM_INT_LIT8:
2023 case OP_REM_INT_LIT16:
2024 if (lit == 0) {
buzbee5ade1d22011-09-09 14:44:52 -07002025 genImmedCheck(cUnit, kArmCondAl, 0, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07002026 return false;
2027 }
2028 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
2029 return false;
2030 }
2031 oatFlushAllRegs(cUnit); /* Everything to home location */
2032 loadValueDirectFixed(cUnit, rlSrc, r0);
2033 oatClobber(cUnit, r0);
2034 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2035 (dalvikOpcode == OP_DIV_INT_LIT16)) {
2036 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
2037 isDiv = true;
2038 } else {
2039 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
2040 isDiv = false;
2041 }
2042 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
2043 loadConstant(cUnit, r1, lit);
Ian Rogersff1ed472011-09-20 13:46:24 -07002044 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07002045 if (isDiv)
2046 rlResult = oatGetReturn(cUnit);
2047 else
2048 rlResult = oatGetReturnAlt(cUnit);
2049 storeValue(cUnit, rlDest, rlResult);
2050 return false;
2051 break;
2052 default:
2053 return true;
2054 }
2055 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2056 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2057 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2058 if (shiftOp && (lit == 0)) {
2059 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2060 } else {
2061 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2062 }
2063 storeValue(cUnit, rlDest, rlResult);
2064 return false;
2065}
2066
2067/* Architectural-specific debugging helpers go here */
2068void oatArchDump(void)
2069{
2070 /* Print compiled opcode in this VM instance */
2071 int i, start, streak;
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07002072 std::string buf;
buzbee67bf8852011-08-17 17:51:35 -07002073
2074 streak = i = 0;
buzbee67bf8852011-08-17 17:51:35 -07002075 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2076 i++;
2077 }
2078 if (i == kNumPackedOpcodes) {
2079 return;
2080 }
2081 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
2082 if (opcodeCoverage[i]) {
2083 streak++;
2084 } else {
2085 if (streak == 1) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07002086 StringAppendF(&buf, "%x,", start);
buzbee67bf8852011-08-17 17:51:35 -07002087 } else {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07002088 StringAppendF(&buf, "%x-%x,", start, start + streak - 1);
buzbee67bf8852011-08-17 17:51:35 -07002089 }
2090 streak = 0;
2091 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2092 i++;
2093 }
2094 if (i < kNumPackedOpcodes) {
2095 streak = 1;
2096 start = i;
2097 }
2098 }
2099 }
2100 if (streak) {
2101 if (streak == 1) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07002102 StringAppendF(&buf, "%x", start);
buzbee67bf8852011-08-17 17:51:35 -07002103 } else {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07002104 StringAppendF(&buf, "%x-%x", start, start + streak - 1);
buzbee67bf8852011-08-17 17:51:35 -07002105 }
2106 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -07002107 if (!buf.empty()) {
buzbee67bf8852011-08-17 17:51:35 -07002108 LOG(INFO) << "dalvik.vm.oat.op = " << buf;
2109 }
2110}