blob: b63c37fdb2c4cd82e3407d96a981ecd4f911a9b9 [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))
33
34STATIC RegLocation getRetLoc(CompilationUnit* cUnit);
buzbee34cd9e52011-09-08 14:31:52 -070035
36std::string fieldNameFromIndex(const Method* method, uint32_t fieldIdx)
37{
38 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
39 const art::DexFile& dex_file = class_linker->FindDexFile(
40 method->GetDeclaringClass()->GetDexCache());
41 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
Elliott Hughes2bb97f92011-09-11 15:43:37 -070042 std::string class_name = dex_file.dexStringByTypeIdx(field_id.class_idx_);
buzbee34cd9e52011-09-08 14:31:52 -070043 std::string field_name = dex_file.dexStringById(field_id.name_idx_);
44 return class_name + "." + field_name;
45}
46
Elliott Hughes81bc5092011-09-30 17:25:59 -070047void warnIfUnresolved(CompilationUnit* cUnit, int fieldIdx, Field* field) {
48 if (field == NULL) {
49 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
50 << " unresolved at compile time";
51 } else {
52 // We also use the slow path for wide volatile fields.
53 }
54}
55
buzbee67bf8852011-08-17 17:51:35 -070056/*
57 * Construct an s4 from two consecutive half-words of switch data.
58 * This needs to check endianness because the DEX optimizer only swaps
59 * half-words in instruction stream.
60 *
61 * "switchData" must be 32-bit aligned.
62 */
63#if __BYTE_ORDER == __LITTLE_ENDIAN
buzbeeed3e9302011-09-23 17:34:19 -070064STATIC inline s4 s4FromSwitchData(const void* switchData) {
buzbee67bf8852011-08-17 17:51:35 -070065 return *(s4*) switchData;
66}
67#else
buzbeeed3e9302011-09-23 17:34:19 -070068STATIC inline s4 s4FromSwitchData(const void* switchData) {
buzbee67bf8852011-08-17 17:51:35 -070069 u2* data = switchData;
70 return data[0] | (((s4) data[1]) << 16);
71}
72#endif
73
buzbeeed3e9302011-09-23 17:34:19 -070074STATIC ArmLIR* callRuntimeHelper(CompilationUnit* cUnit, int reg)
buzbeeec5adf32011-09-11 15:25:43 -070075{
buzbee6181f792011-09-29 11:14:04 -070076 oatClobberCalleeSave(cUnit);
buzbeeec5adf32011-09-11 15:25:43 -070077 return opReg(cUnit, kOpBlx, reg);
78}
79
buzbee1b4c8592011-08-31 10:43:51 -070080/* Generate unconditional branch instructions */
buzbeeed3e9302011-09-23 17:34:19 -070081STATIC ArmLIR* genUnconditionalBranch(CompilationUnit* cUnit, ArmLIR* target)
buzbee1b4c8592011-08-31 10:43:51 -070082{
83 ArmLIR* branch = opNone(cUnit, kOpUncondBr);
84 branch->generic.target = (LIR*) target;
85 return branch;
86}
87
buzbee67bf8852011-08-17 17:51:35 -070088/*
89 * Generate a Thumb2 IT instruction, which can nullify up to
90 * four subsequent instructions based on a condition and its
91 * inverse. The condition applies to the first instruction, which
92 * is executed if the condition is met. The string "guide" consists
93 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
94 * A "T" means the instruction is executed if the condition is
95 * met, and an "E" means the instruction is executed if the condition
96 * is not met.
97 */
buzbeeed3e9302011-09-23 17:34:19 -070098STATIC ArmLIR* genIT(CompilationUnit* cUnit, ArmConditionCode code,
buzbee67bf8852011-08-17 17:51:35 -070099 const char* guide)
100{
101 int mask;
102 int condBit = code & 1;
103 int altBit = condBit ^ 1;
104 int mask3 = 0;
105 int mask2 = 0;
106 int mask1 = 0;
107
108 //Note: case fallthroughs intentional
109 switch(strlen(guide)) {
110 case 3:
111 mask1 = (guide[2] == 'T') ? condBit : altBit;
112 case 2:
113 mask2 = (guide[1] == 'T') ? condBit : altBit;
114 case 1:
115 mask3 = (guide[0] == 'T') ? condBit : altBit;
116 break;
117 case 0:
118 break;
119 default:
120 LOG(FATAL) << "OAT: bad case in genIT";
121 }
122 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
123 (1 << (3 - strlen(guide)));
124 return newLIR2(cUnit, kThumb2It, code, mask);
125}
126
127/*
128 * Insert a kArmPseudoCaseLabel at the beginning of the Dalvik
129 * offset vaddr. This label will be used to fix up the case
130 * branch table during the assembly phase. Be sure to set
131 * all resource flags on this to prevent code motion across
132 * target boundaries. KeyVal is just there for debugging.
133 */
buzbeeed3e9302011-09-23 17:34:19 -0700134STATIC ArmLIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
buzbee67bf8852011-08-17 17:51:35 -0700135{
136 ArmLIR* lir;
137 for (lir = (ArmLIR*)cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
138 if ((lir->opcode == kArmPseudoDalvikByteCodeBoundary) &&
139 (lir->generic.dalvikOffset == vaddr)) {
140 ArmLIR* newLabel = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
141 newLabel->generic.dalvikOffset = vaddr;
142 newLabel->opcode = kArmPseudoCaseLabel;
143 newLabel->operands[0] = keyVal;
144 oatInsertLIRAfter((LIR*)lir, (LIR*)newLabel);
145 return newLabel;
146 }
147 }
148 oatCodegenDump(cUnit);
149 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
150 return NULL; // Quiet gcc
151}
152
buzbeeed3e9302011-09-23 17:34:19 -0700153STATIC void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
buzbee67bf8852011-08-17 17:51:35 -0700154{
155 const u2* table = tabRec->table;
156 int baseVaddr = tabRec->vaddr;
157 int *targets = (int*)&table[4];
158 int entries = table[1];
159 int lowKey = s4FromSwitchData(&table[2]);
160 for (int i = 0; i < entries; i++) {
161 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
162 i + lowKey);
163 }
164}
165
buzbeeed3e9302011-09-23 17:34:19 -0700166STATIC void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
buzbee67bf8852011-08-17 17:51:35 -0700167{
168 const u2* table = tabRec->table;
169 int baseVaddr = tabRec->vaddr;
170 int entries = table[1];
171 int* keys = (int*)&table[2];
172 int* targets = &keys[entries];
173 for (int i = 0; i < entries; i++) {
174 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
175 keys[i]);
176 }
177}
178
179void oatProcessSwitchTables(CompilationUnit* cUnit)
180{
181 GrowableListIterator iterator;
182 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
183 while (true) {
184 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
185 &iterator);
186 if (tabRec == NULL) break;
187 if (tabRec->table[0] == kPackedSwitchSignature)
188 markPackedCaseLabels(cUnit, tabRec);
189 else if (tabRec->table[0] == kSparseSwitchSignature)
190 markSparseCaseLabels(cUnit, tabRec);
191 else {
192 LOG(FATAL) << "Invalid switch table";
193 }
194 }
195}
196
buzbeeed3e9302011-09-23 17:34:19 -0700197STATIC void dumpSparseSwitchTable(const u2* table)
buzbee67bf8852011-08-17 17:51:35 -0700198 /*
199 * Sparse switch data format:
200 * ushort ident = 0x0200 magic value
201 * ushort size number of entries in the table; > 0
202 * int keys[size] keys, sorted low-to-high; 32-bit aligned
203 * int targets[size] branch targets, relative to switch opcode
204 *
205 * Total size is (2+size*4) 16-bit code units.
206 */
207{
208 u2 ident = table[0];
209 int entries = table[1];
210 int* keys = (int*)&table[2];
211 int* targets = &keys[entries];
212 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
213 ", entries: " << std::dec << entries;
214 for (int i = 0; i < entries; i++) {
215 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
216 targets[i];
217 }
218}
219
buzbeeed3e9302011-09-23 17:34:19 -0700220STATIC void dumpPackedSwitchTable(const u2* table)
buzbee67bf8852011-08-17 17:51:35 -0700221 /*
222 * Packed switch data format:
223 * ushort ident = 0x0100 magic value
224 * ushort size number of entries in the table
225 * int first_key first (and lowest) switch case value
226 * int targets[size] branch targets, relative to switch opcode
227 *
228 * Total size is (4+size*2) 16-bit code units.
229 */
230{
231 u2 ident = table[0];
232 int* targets = (int*)&table[4];
233 int entries = table[1];
234 int lowKey = s4FromSwitchData(&table[2]);
235 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
236 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
237 for (int i = 0; i < entries; i++) {
238 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
239 targets[i];
240 }
241}
242
243/*
244 * The sparse table in the literal pool is an array of <key,displacement>
245 * pairs. For each set, we'll load them as a pair using ldmia.
246 * This means that the register number of the temp we use for the key
247 * must be lower than the reg for the displacement.
248 *
249 * The test loop will look something like:
250 *
251 * adr rBase, <table>
252 * ldr rVal, [rSP, vRegOff]
253 * mov rIdx, #tableSize
254 * lp:
255 * ldmia rBase!, {rKey, rDisp}
256 * sub rIdx, #1
257 * cmp rVal, rKey
258 * ifeq
259 * add rPC, rDisp ; This is the branch from which we compute displacement
260 * cbnz rIdx, lp
261 */
buzbeeed3e9302011-09-23 17:34:19 -0700262STATIC void genSparseSwitch(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700263 RegLocation rlSrc)
264{
265 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
266 if (cUnit->printMe) {
267 dumpSparseSwitchTable(table);
268 }
269 // Add the table to the list - we'll process it later
270 SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable),
271 true);
272 tabRec->table = table;
273 tabRec->vaddr = mir->offset;
274 int size = table[1];
275 tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true);
276 oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec);
277
278 // Get the switch value
279 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
280 int rBase = oatAllocTemp(cUnit);
281 /* Allocate key and disp temps */
282 int rKey = oatAllocTemp(cUnit);
283 int rDisp = oatAllocTemp(cUnit);
284 // Make sure rKey's register number is less than rDisp's number for ldmia
285 if (rKey > rDisp) {
286 int tmp = rDisp;
287 rDisp = rKey;
288 rKey = tmp;
289 }
290 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700291 newLIR3(cUnit, kThumb2Adr, rBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700292 // Set up rIdx
293 int rIdx = oatAllocTemp(cUnit);
294 loadConstant(cUnit, rIdx, size);
295 // Establish loop branch target
296 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
297 target->defMask = ENCODE_ALL;
298 // Load next key/disp
299 newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp));
300 opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg);
301 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
302 genIT(cUnit, kArmCondEq, "");
303 ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp);
304 tabRec->bxInst = switchBranch;
305 // Needs to use setflags encoding here
306 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
307 ArmLIR* branch = opCondBranch(cUnit, kArmCondNe);
308 branch->generic.target = (LIR*)target;
309}
310
311
buzbeeed3e9302011-09-23 17:34:19 -0700312STATIC void genPackedSwitch(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700313 RegLocation rlSrc)
314{
315 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
316 if (cUnit->printMe) {
317 dumpPackedSwitchTable(table);
318 }
319 // Add the table to the list - we'll process it later
320 SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable),
321 true);
322 tabRec->table = table;
323 tabRec->vaddr = mir->offset;
324 int size = table[1];
325 tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true);
326 oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec);
327
328 // Get the switch value
329 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
330 int tableBase = oatAllocTemp(cUnit);
331 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700332 newLIR3(cUnit, kThumb2Adr, tableBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700333 int lowKey = s4FromSwitchData(&table[2]);
334 int keyReg;
335 // Remove the bias, if necessary
336 if (lowKey == 0) {
337 keyReg = rlSrc.lowReg;
338 } else {
339 keyReg = oatAllocTemp(cUnit);
340 opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
341 }
342 // Bounds check - if < 0 or >= size continue following switch
343 opRegImm(cUnit, kOpCmp, keyReg, size-1);
344 ArmLIR* branchOver = opCondBranch(cUnit, kArmCondHi);
345
346 // Load the displacement from the switch table
347 int dispReg = oatAllocTemp(cUnit);
348 loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord);
349
350 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
351 ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg);
352 tabRec->bxInst = switchBranch;
353
354 /* branchOver target here */
355 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
356 target->defMask = ENCODE_ALL;
357 branchOver->generic.target = (LIR*)target;
358}
359
360/*
361 * Array data table format:
362 * ushort ident = 0x0300 magic value
363 * ushort width width of each element in the table
364 * uint size number of elements in the table
365 * ubyte data[size*width] table of data values (may contain a single-byte
366 * padding at the end)
367 *
368 * Total size is 4+(width * size + 1)/2 16-bit code units.
369 */
buzbeeed3e9302011-09-23 17:34:19 -0700370STATIC void genFillArrayData(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700371 RegLocation rlSrc)
372{
373 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
374 // Add the table to the list - we'll process it later
375 FillArrayData *tabRec = (FillArrayData *)
376 oatNew(sizeof(FillArrayData), true);
377 tabRec->table = table;
378 tabRec->vaddr = mir->offset;
379 u2 width = tabRec->table[1];
380 u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16);
381 tabRec->size = (size * width) + 8;
382
383 oatInsertGrowableList(&cUnit->fillArrayData, (intptr_t)tabRec);
384
385 // Making a call - use explicit registers
386 oatFlushAllRegs(cUnit); /* Everything to home location */
387 loadValueDirectFixed(cUnit, rlSrc, r0);
388 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700389 OFFSETOF_MEMBER(Thread, pHandleFillArrayDataFromCode), rLR);
buzbeee6d61962011-08-27 11:58:19 -0700390 // Materialize a pointer to the fill data image
buzbee03fa2632011-09-20 17:10:57 -0700391 newLIR3(cUnit, kThumb2Adr, r1, 0, (intptr_t)tabRec);
Ian Rogersff1ed472011-09-20 13:46:24 -0700392 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700393}
394
395/*
396 * Mark garbage collection card. Skip if the value we're storing is null.
397 */
buzbeeed3e9302011-09-23 17:34:19 -0700398STATIC void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg)
buzbee67bf8852011-08-17 17:51:35 -0700399{
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700400#ifdef CONCURRENT_GARBAGE_COLLECTOR
buzbee0d966cf2011-09-08 17:34:58 -0700401 // TODO: re-enable when concurrent collector is active
buzbee67bf8852011-08-17 17:51:35 -0700402 int regCardBase = oatAllocTemp(cUnit);
403 int regCardNo = oatAllocTemp(cUnit);
404 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
buzbeec143c552011-08-20 17:38:58 -0700405 loadWordDisp(cUnit, rSELF, Thread::CardTableOffset().Int32Value(),
buzbee67bf8852011-08-17 17:51:35 -0700406 regCardBase);
407 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
408 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
409 kUnsignedByte);
410 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
411 target->defMask = ENCODE_ALL;
412 branchOver->generic.target = (LIR*)target;
413 oatFreeTemp(cUnit, regCardBase);
414 oatFreeTemp(cUnit, regCardNo);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700415#endif
buzbee67bf8852011-08-17 17:51:35 -0700416}
417
buzbee34cd9e52011-09-08 14:31:52 -0700418/*
419 * Helper function for Iget/put when field not resolved at compile time.
420 * Will trash call temps and return with the field offset in r0.
421 */
Elliott Hughes81bc5092011-09-30 17:25:59 -0700422STATIC void getFieldOffset(CompilationUnit* cUnit, MIR* mir, Field* fieldPtr)
buzbee34cd9e52011-09-08 14:31:52 -0700423{
424 int fieldIdx = mir->dalvikInsn.vC;
buzbee6181f792011-09-29 11:14:04 -0700425 oatFlushAllRegs(cUnit);
Elliott Hughes81bc5092011-09-30 17:25:59 -0700426 warnIfUnresolved(cUnit, fieldIdx, fieldPtr);
buzbee34cd9e52011-09-08 14:31:52 -0700427 oatLockCallTemps(cUnit); // Explicit register usage
428 loadCurrMethodDirect(cUnit, r1); // arg1 <= Method*
429 loadWordDisp(cUnit, r1,
430 Method::DexCacheResolvedFieldsOffset().Int32Value(), r0);
431 loadWordDisp(cUnit, r0, art::Array::DataOffset().Int32Value() +
432 sizeof(int32_t*)* fieldIdx, r0);
433 /*
434 * For testing, omit the test for run-time resolution. This will
435 * force all accesses to go through the runtime resolution path.
436 */
buzbeece302932011-10-04 14:32:18 -0700437 ArmLIR* branchOver = NULL;
438 if (!EXERCISE_SLOWEST_FIELD_PATH) {
439 branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
440 }
buzbee34cd9e52011-09-08 14:31:52 -0700441 // Resolve
442 loadWordDisp(cUnit, rSELF,
Brian Carlstrom845490b2011-09-19 15:56:53 -0700443 OFFSETOF_MEMBER(Thread, pFindInstanceFieldFromCode), rLR);
buzbee34cd9e52011-09-08 14:31:52 -0700444 loadConstant(cUnit, r0, fieldIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700445 callRuntimeHelper(cUnit, rLR); // resolveTypeFromCode(idx, method)
buzbee34cd9e52011-09-08 14:31:52 -0700446 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
447 target->defMask = ENCODE_ALL;
buzbeece302932011-10-04 14:32:18 -0700448 if (!EXERCISE_SLOWEST_FIELD_PATH) {
449 branchOver->generic.target = (LIR*)target;
450 }
buzbee34cd9e52011-09-08 14:31:52 -0700451 // Free temps (except for r0)
452 oatFreeTemp(cUnit, r1);
453 oatFreeTemp(cUnit, r2);
454 oatFreeTemp(cUnit, r3);
455 loadWordDisp(cUnit, r0, art::Field::OffsetOffset().Int32Value(), r0);
456}
457
buzbeeed3e9302011-09-23 17:34:19 -0700458STATIC void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -0700459 RegLocation rlDest, RegLocation rlObj)
460{
buzbeec143c552011-08-20 17:38:58 -0700461 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
462 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700463 RegLocation rlResult;
464 RegisterClass regClass = oatRegClassBySize(size);
buzbee34cd9e52011-09-08 14:31:52 -0700465 if (SLOW_FIELD_PATH || fieldPtr == NULL) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700466 getFieldOffset(cUnit, mir, fieldPtr);
buzbee34cd9e52011-09-08 14:31:52 -0700467 // Field offset in r0
468 rlObj = loadValue(cUnit, rlObj, kCoreReg);
469 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee5ade1d22011-09-09 14:44:52 -0700470 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee58f92742011-10-01 11:22:17 -0700471 loadBaseIndexed(cUnit, rlObj.lowReg, r0, rlResult.lowReg, 0, kWord);
buzbee67bf8852011-08-17 17:51:35 -0700472 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700473 storeValue(cUnit, rlDest, rlResult);
474 } else {
475#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700476 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700477#else
478 bool isVolatile = false;
479#endif
480 int fieldOffset = fieldPtr->GetOffset().Int32Value();
481 rlObj = loadValue(cUnit, rlObj, kCoreReg);
482 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee5ade1d22011-09-09 14:44:52 -0700483 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee34cd9e52011-09-08 14:31:52 -0700484 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
buzbee58f92742011-10-01 11:22:17 -0700485 kWord, rlObj.sRegLow);
buzbee34cd9e52011-09-08 14:31:52 -0700486 if (isVolatile) {
487 oatGenMemBarrier(cUnit, kSY);
488 }
489 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -0700490 }
buzbee67bf8852011-08-17 17:51:35 -0700491}
492
buzbeeed3e9302011-09-23 17:34:19 -0700493STATIC void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -0700494 RegLocation rlSrc, RegLocation rlObj, bool isObject)
495{
buzbeec143c552011-08-20 17:38:58 -0700496 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
497 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700498 RegisterClass regClass = oatRegClassBySize(size);
buzbee34cd9e52011-09-08 14:31:52 -0700499 if (SLOW_FIELD_PATH || fieldPtr == NULL) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700500 getFieldOffset(cUnit, mir, fieldPtr);
buzbee34cd9e52011-09-08 14:31:52 -0700501 // Field offset in r0
502 rlObj = loadValue(cUnit, rlObj, kCoreReg);
503 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee5ade1d22011-09-09 14:44:52 -0700504 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee67bf8852011-08-17 17:51:35 -0700505 oatGenMemBarrier(cUnit, kSY);
buzbee58f92742011-10-01 11:22:17 -0700506 storeBaseIndexed(cUnit, rlObj.lowReg, r0, rlSrc.lowReg, 0, kWord);
buzbee34cd9e52011-09-08 14:31:52 -0700507 } else {
508#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700509 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700510#else
511 bool isVolatile = false;
512#endif
513 int fieldOffset = fieldPtr->GetOffset().Int32Value();
514 rlObj = loadValue(cUnit, rlObj, kCoreReg);
515 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee5ade1d22011-09-09 14:44:52 -0700516 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700517
518 if (isVolatile) {
buzbee12246b82011-09-29 14:15:05 -0700519 oatGenMemBarrier(cUnit, kST);
buzbee34cd9e52011-09-08 14:31:52 -0700520 }
buzbee58f92742011-10-01 11:22:17 -0700521 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, kWord);
buzbee12246b82011-09-29 14:15:05 -0700522 if (isVolatile) {
523 oatGenMemBarrier(cUnit, kSY);
524 }
buzbee67bf8852011-08-17 17:51:35 -0700525 }
buzbee67bf8852011-08-17 17:51:35 -0700526 if (isObject) {
527 /* NOTE: marking card based on object head */
528 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
529 }
530}
531
buzbeeed3e9302011-09-23 17:34:19 -0700532STATIC void genIGetWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700533 RegLocation rlObj)
534{
buzbee12246b82011-09-29 14:15:05 -0700535 RegLocation rlResult;
buzbeec143c552011-08-20 17:38:58 -0700536 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
537 GetResolvedField(mir->dalvikInsn.vC);
buzbee12246b82011-09-29 14:15:05 -0700538#if ANDROID_SMP != 0
539 bool isVolatile = (fieldPtr == NULL) || fieldPtr->IsVolatile();
540#else
541 bool isVolatile = false;
542#endif
buzbeece302932011-10-04 14:32:18 -0700543 if (SLOW_FIELD_PATH || (fieldPtr == NULL) || isVolatile) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700544 getFieldOffset(cUnit, mir, fieldPtr);
buzbee34cd9e52011-09-08 14:31:52 -0700545 // Field offset in r0
546 rlObj = loadValue(cUnit, rlObj, kCoreReg);
547 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee5ade1d22011-09-09 14:44:52 -0700548 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700549 opRegReg(cUnit, kOpAdd, r0, rlObj.lowReg);
550 loadPair(cUnit, r0, rlResult.lowReg, rlResult.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700551 oatGenMemBarrier(cUnit, kSY);
buzbee12246b82011-09-29 14:15:05 -0700552 storeValueWide(cUnit, rlDest, rlResult);
buzbee34cd9e52011-09-08 14:31:52 -0700553 } else {
buzbee34cd9e52011-09-08 14:31:52 -0700554 int fieldOffset = fieldPtr->GetOffset().Int32Value();
555 rlObj = loadValue(cUnit, rlObj, kCoreReg);
556 int regPtr = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700557
buzbeeed3e9302011-09-23 17:34:19 -0700558 DCHECK(rlDest.wide);
buzbee34cd9e52011-09-08 14:31:52 -0700559
buzbee5ade1d22011-09-09 14:44:52 -0700560 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700561 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
562 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
563
564 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
565
buzbee34cd9e52011-09-08 14:31:52 -0700566 oatFreeTemp(cUnit, regPtr);
567 storeValueWide(cUnit, rlDest, rlResult);
568 }
buzbee67bf8852011-08-17 17:51:35 -0700569}
570
buzbeeed3e9302011-09-23 17:34:19 -0700571STATIC void genIPutWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700572 RegLocation rlObj)
573{
buzbeec143c552011-08-20 17:38:58 -0700574 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
575 GetResolvedField(mir->dalvikInsn.vC);
buzbee12246b82011-09-29 14:15:05 -0700576#if ANDROID_SMP != 0
577 bool isVolatile = (fieldPtr == NULL) || fieldPtr->IsVolatile();
578#else
579 bool isVolatile = false;
580#endif
buzbeece302932011-10-04 14:32:18 -0700581 if (SLOW_FIELD_PATH || (fieldPtr == NULL) || isVolatile) {
Elliott Hughes81bc5092011-09-30 17:25:59 -0700582 getFieldOffset(cUnit, mir, fieldPtr);
buzbee34cd9e52011-09-08 14:31:52 -0700583 // Field offset in r0
584 rlObj = loadValue(cUnit, rlObj, kCoreReg);
585 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee5ade1d22011-09-09 14:44:52 -0700586 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700587 opRegReg(cUnit, kOpAdd, r0, rlObj.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700588 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700589 storePair(cUnit, r0, rlSrc.lowReg, rlSrc.highReg);
590 } else {
buzbee34cd9e52011-09-08 14:31:52 -0700591 int fieldOffset = fieldPtr->GetOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700592
buzbee34cd9e52011-09-08 14:31:52 -0700593 rlObj = loadValue(cUnit, rlObj, kCoreReg);
594 int regPtr;
595 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee5ade1d22011-09-09 14:44:52 -0700596 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700597 regPtr = oatAllocTemp(cUnit);
598 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
599
buzbee34cd9e52011-09-08 14:31:52 -0700600 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
601
602 oatFreeTemp(cUnit, regPtr);
603 }
buzbee67bf8852011-08-17 17:51:35 -0700604}
605
buzbeeed3e9302011-09-23 17:34:19 -0700606STATIC void genConstClass(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700607 RegLocation rlDest, RegLocation rlSrc)
608{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700609 art::Class* classPtr = cUnit->method->GetDexCacheResolvedTypes()->
buzbee1b4c8592011-08-31 10:43:51 -0700610 Get(mir->dalvikInsn.vB);
611 int mReg = loadCurrMethod(cUnit);
612 int resReg = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700613 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee2a475e72011-09-07 17:19:17 -0700614 loadWordDisp(cUnit, mReg, Method::DexCacheResolvedTypesOffset().Int32Value(),
buzbee1b4c8592011-08-31 10:43:51 -0700615 resReg);
616 loadWordDisp(cUnit, resReg, Array::DataOffset().Int32Value() +
617 (sizeof(String*) * mir->dalvikInsn.vB), rlResult.lowReg);
buzbeece302932011-10-04 14:32:18 -0700618 if (SLOW_TYPE_PATH || (classPtr == NULL)) {
buzbee1b4c8592011-08-31 10:43:51 -0700619 // Fast path, we're done - just store result
620 storeValue(cUnit, rlDest, rlResult);
621 } else {
622 // Slow path. Must test at runtime
buzbee6181f792011-09-29 11:14:04 -0700623 oatFlushAllRegs(cUnit);
buzbee1b4c8592011-08-31 10:43:51 -0700624 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, rlResult.lowReg,
625 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,
634 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
635 genRegCopy(cUnit, r1, mReg);
636 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700637 callRuntimeHelper(cUnit, rLR);
buzbee1b4c8592011-08-31 10:43:51 -0700638 RegLocation rlResult = oatGetReturn(cUnit);
639 storeValue(cUnit, rlDest, rlResult);
640 // Rejoin code paths
641 ArmLIR* target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
642 target2->defMask = ENCODE_ALL;
643 branch1->generic.target = (LIR*)target1;
644 branch2->generic.target = (LIR*)target2;
645 }
buzbee67bf8852011-08-17 17:51:35 -0700646}
647
buzbeeed3e9302011-09-23 17:34:19 -0700648STATIC void genConstString(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700649 RegLocation rlDest, RegLocation rlSrc)
650{
buzbeece302932011-10-04 14:32:18 -0700651 /* NOTE: Most strings should be available at compile time */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700652 const art::String* str = cUnit->method->GetDexCacheStrings()->
buzbee1b4c8592011-08-31 10:43:51 -0700653 Get(mir->dalvikInsn.vB);
buzbeece302932011-10-04 14:32:18 -0700654 if (SLOW_STRING_PATH || (str == NULL)) {
655 oatFlushAllRegs(cUnit);
656 oatLockCallTemps(cUnit); // Using explicit registers
657 loadCurrMethodDirect(cUnit, r2);
658 loadWordDisp(cUnit, r2, Method::DexCacheStringsOffset().Int32Value(),
659 r0);
660 // Might call out to helper, which will return resolved string in r0
661 loadWordDisp(cUnit, rSELF,
662 OFFSETOF_MEMBER(Thread, pResolveStringFromCode), rLR);
663 loadWordDisp(cUnit, r0, Array::DataOffset().Int32Value() +
664 (sizeof(String*) * mir->dalvikInsn.vB), r0);
665 loadConstant(cUnit, r1, mir->dalvikInsn.vB);
666 opRegImm(cUnit, kOpCmp, r0, 0); // Is resolved?
667 genBarrier(cUnit);
668 // For testing, always force through helper
669 if (!EXERCISE_SLOWEST_STRING_PATH) {
670 genIT(cUnit, kArmCondEq, "T");
671 }
672 genRegCopy(cUnit, r0, r2); // .eq
673 opReg(cUnit, kOpBlx, rLR); // .eq, helper(Method*, string_idx)
674 genBarrier(cUnit);
675 storeValue(cUnit, rlDest, getRetLoc(cUnit));
676 } else {
677 int mReg = loadCurrMethod(cUnit);
678 int resReg = oatAllocTemp(cUnit);
679 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
680 loadWordDisp(cUnit, mReg, Method::DexCacheStringsOffset().Int32Value(),
681 resReg);
682 loadWordDisp(cUnit, resReg, Array::DataOffset().Int32Value() +
683 (sizeof(String*) * mir->dalvikInsn.vB), rlResult.lowReg);
684 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 */
buzbee67bf8852011-08-17 17:51:35 -0700696 loadWordDisp(cUnit, rSELF,
Brian Carlstrom1f870082011-08-23 16:02:11 -0700697 OFFSETOF_MEMBER(Thread, pAllocObjectFromCode), rLR);
buzbeedfd3d702011-08-28 12:56:51 -0700698 loadCurrMethodDirect(cUnit, r1); // arg1 <= Method*
699 loadConstant(cUnit, r0, mir->dalvikInsn.vB); // arg0 <- type_id
Ian Rogersff1ed472011-09-20 13:46:24 -0700700 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700701 RegLocation rlResult = oatGetReturn(cUnit);
702 storeValue(cUnit, rlDest, rlResult);
703}
704
705void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
706{
buzbee6181f792011-09-29 11:14:04 -0700707 oatFlushAllRegs(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700708 loadWordDisp(cUnit, rSELF,
Ian Rogers67375ac2011-09-14 00:55:44 -0700709 OFFSETOF_MEMBER(Thread, pDeliverException), rLR);
Ian Rogersbdb03912011-09-14 00:55:44 -0700710 loadValueDirectFixed(cUnit, rlSrc, r0); // Get exception object
Ian Rogersff1ed472011-09-20 13:46:24 -0700711 callRuntimeHelper(cUnit, rLR); // art_deliver_exception(exception);
buzbee67bf8852011-08-17 17:51:35 -0700712}
713
buzbeeed3e9302011-09-23 17:34:19 -0700714STATIC void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700715 RegLocation rlSrc)
716{
buzbee6181f792011-09-29 11:14:04 -0700717 oatFlushAllRegs(cUnit);
buzbee2a475e72011-09-07 17:19:17 -0700718 // May generate a call - use explicit registers
719 oatLockCallTemps(cUnit);
720 art::Class* classPtr = cUnit->method->GetDexCacheResolvedTypes()->
721 Get(mir->dalvikInsn.vC);
722 int classReg = r2; // Fixed usage
723 loadCurrMethodDirect(cUnit, r1); // r1 <= current Method*
buzbee991e3ac2011-09-29 15:44:22 -0700724 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
buzbee2a475e72011-09-07 17:19:17 -0700725 loadWordDisp(cUnit, r1, Method::DexCacheResolvedTypesOffset().Int32Value(),
726 classReg);
727 loadWordDisp(cUnit, classReg, Array::DataOffset().Int32Value() +
728 (sizeof(String*) * mir->dalvikInsn.vC), classReg);
buzbee67bf8852011-08-17 17:51:35 -0700729 if (classPtr == NULL) {
buzbee2a475e72011-09-07 17:19:17 -0700730 // Generate a runtime test
731 ArmLIR* hopBranch = genCmpImmBranch(cUnit, kArmCondNe, classReg, 0);
732 // Not resolved
733 // Call out to helper, which will return resolved type in r0
734 loadWordDisp(cUnit, rSELF,
735 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
736 loadConstant(cUnit, r0, mir->dalvikInsn.vC);
Ian Rogersff1ed472011-09-20 13:46:24 -0700737 callRuntimeHelper(cUnit, rLR); // resolveTypeFromCode(idx, method)
buzbee2a475e72011-09-07 17:19:17 -0700738 genRegCopy(cUnit, r2, r0); // Align usage with fast path
buzbee991e3ac2011-09-29 15:44:22 -0700739 loadValueDirectFixed(cUnit, rlSrc, r0); /* reload Ref */
buzbee2a475e72011-09-07 17:19:17 -0700740 // Rejoin code paths
741 ArmLIR* hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
742 hopTarget->defMask = ENCODE_ALL;
743 hopBranch->generic.target = (LIR*)hopTarget;
buzbee67bf8852011-08-17 17:51:35 -0700744 }
buzbee991e3ac2011-09-29 15:44:22 -0700745 /* r0 is ref, r2 is class. If ref==null, use directly as bool result */
746 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
buzbee2a475e72011-09-07 17:19:17 -0700747 /* load object->clazz */
buzbeeed3e9302011-09-23 17:34:19 -0700748 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbee991e3ac2011-09-29 15:44:22 -0700749 loadWordDisp(cUnit, r0, Object::ClassOffset().Int32Value(), r1);
750 /* r0 is ref, r1 is ref->clazz, r2 is class */
buzbee67bf8852011-08-17 17:51:35 -0700751 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700752 OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode), rLR);
buzbee991e3ac2011-09-29 15:44:22 -0700753 opRegReg(cUnit, kOpCmp, r1, r2); // Same?
754 genBarrier(cUnit);
755 genIT(cUnit, kArmCondEq, "EE"); // if-convert the test
756 loadConstant(cUnit, r0, 1); // .eq case - load true
757 genRegCopy(cUnit, r0, r2); // .ne case - arg0 <= class
758 opReg(cUnit, kOpBlx, rLR); // .ne case: helper(class, ref->class)
759 genBarrier(cUnit);
760 oatClobberCalleeSave(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700761 /* branch target here */
762 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
763 target->defMask = ENCODE_ALL;
buzbee2a475e72011-09-07 17:19:17 -0700764 RegLocation rlResult = oatGetReturn(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700765 storeValue(cUnit, rlDest, rlResult);
766 branch1->generic.target = (LIR*)target;
buzbee67bf8852011-08-17 17:51:35 -0700767}
768
buzbeeed3e9302011-09-23 17:34:19 -0700769STATIC void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700770{
buzbee6181f792011-09-29 11:14:04 -0700771 oatFlushAllRegs(cUnit);
buzbee2a475e72011-09-07 17:19:17 -0700772 // May generate a call - use explicit registers
773 oatLockCallTemps(cUnit);
774 art::Class* classPtr = cUnit->method->GetDexCacheResolvedTypes()->
775 Get(mir->dalvikInsn.vB);
776 int classReg = r2; // Fixed usage
777 loadCurrMethodDirect(cUnit, r1); // r1 <= current Method*
778 loadWordDisp(cUnit, r1, Method::DexCacheResolvedTypesOffset().Int32Value(),
779 classReg);
780 loadWordDisp(cUnit, classReg, Array::DataOffset().Int32Value() +
781 (sizeof(String*) * mir->dalvikInsn.vB), classReg);
buzbee67bf8852011-08-17 17:51:35 -0700782 if (classPtr == NULL) {
buzbee2a475e72011-09-07 17:19:17 -0700783 // Generate a runtime test
784 ArmLIR* hopBranch = genCmpImmBranch(cUnit, kArmCondNe, classReg, 0);
785 // Not resolved
786 // Call out to helper, which will return resolved type in r0
787 loadWordDisp(cUnit, rSELF,
788 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
789 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700790 callRuntimeHelper(cUnit, rLR); // resolveTypeFromCode(idx, method)
buzbee2a475e72011-09-07 17:19:17 -0700791 genRegCopy(cUnit, r2, r0); // Align usage with fast path
792 // Rejoin code paths
793 ArmLIR* hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
794 hopTarget->defMask = ENCODE_ALL;
795 hopBranch->generic.target = (LIR*)hopTarget;
buzbee67bf8852011-08-17 17:51:35 -0700796 }
buzbee2a475e72011-09-07 17:19:17 -0700797 // At this point, r2 has class
798 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
799 /* Null is OK - continue */
800 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
801 /* load object->clazz */
buzbeeed3e9302011-09-23 17:34:19 -0700802 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbee2a475e72011-09-07 17:19:17 -0700803 loadWordDisp(cUnit, r0, Object::ClassOffset().Int32Value(), r1);
804 /* r1 now contains object->clazz */
buzbee67bf8852011-08-17 17:51:35 -0700805 loadWordDisp(cUnit, rSELF,
buzbee2a475e72011-09-07 17:19:17 -0700806 OFFSETOF_MEMBER(Thread, pCheckCastFromCode), rLR);
807 opRegReg(cUnit, kOpCmp, r1, r2);
808 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondEq); /* If equal, trivial yes */
809 genRegCopy(cUnit, r0, r1);
810 genRegCopy(cUnit, r1, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -0700811 callRuntimeHelper(cUnit, rLR);
buzbee2a475e72011-09-07 17:19:17 -0700812 /* branch target here */
buzbee67bf8852011-08-17 17:51:35 -0700813 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
814 target->defMask = ENCODE_ALL;
815 branch1->generic.target = (LIR*)target;
816 branch2->generic.target = (LIR*)target;
817}
818
buzbeeed3e9302011-09-23 17:34:19 -0700819STATIC void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700820 RegLocation rlSrc)
821{
822 RegLocation rlResult;
823 rlSrc = loadValue(cUnit, rlSrc, kFPReg);
824 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
825 newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg);
826 storeValue(cUnit, rlDest, rlResult);
827}
828
buzbeeed3e9302011-09-23 17:34:19 -0700829STATIC void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700830 RegLocation rlSrc)
831{
832 RegLocation rlResult;
833 rlSrc = loadValueWide(cUnit, rlSrc, kFPReg);
834 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
835 newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg),
836 S2D(rlSrc.lowReg, rlSrc.highReg));
837 storeValueWide(cUnit, rlDest, rlResult);
838}
839
buzbeeed3e9302011-09-23 17:34:19 -0700840STATIC void freeRegLocTemps(CompilationUnit* cUnit, RegLocation rlKeep,
buzbee439c4fa2011-08-27 15:59:07 -0700841 RegLocation rlFree)
buzbee67bf8852011-08-17 17:51:35 -0700842{
buzbee6181f792011-09-29 11:14:04 -0700843 if ((rlFree.lowReg != rlKeep.lowReg) && (rlFree.lowReg != rlKeep.highReg) &&
844 (rlFree.highReg != rlKeep.lowReg) && (rlFree.highReg != rlKeep.highReg)) {
845 // No overlap, free both
buzbee439c4fa2011-08-27 15:59:07 -0700846 oatFreeTemp(cUnit, rlFree.lowReg);
buzbee6181f792011-09-29 11:14:04 -0700847 oatFreeTemp(cUnit, rlFree.highReg);
848 }
buzbee67bf8852011-08-17 17:51:35 -0700849}
850
buzbeeed3e9302011-09-23 17:34:19 -0700851STATIC void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
buzbee67bf8852011-08-17 17:51:35 -0700852 OpKind secondOp, RegLocation rlDest,
853 RegLocation rlSrc1, RegLocation rlSrc2)
854{
buzbee9e0f9b02011-08-24 15:32:46 -0700855 /*
856 * NOTE: This is the one place in the code in which we might have
857 * as many as six live temporary registers. There are 5 in the normal
858 * set for Arm. Until we have spill capabilities, temporarily add
859 * lr to the temp set. It is safe to do this locally, but note that
860 * lr is used explicitly elsewhere in the code generator and cannot
861 * normally be used as a general temp register.
862 */
buzbee67bf8852011-08-17 17:51:35 -0700863 RegLocation rlResult;
buzbee9e0f9b02011-08-24 15:32:46 -0700864 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
865 oatFreeTemp(cUnit, rLR); // and make it available
buzbee67bf8852011-08-17 17:51:35 -0700866 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
867 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
868 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeec0ecd652011-09-25 18:11:54 -0700869 // The longs may overlap - use intermediate temp if so
870 if (rlResult.lowReg == rlSrc1.highReg) {
buzbeec0ecd652011-09-25 18:11:54 -0700871 int tReg = oatAllocTemp(cUnit);
872 genRegCopy(cUnit, tReg, rlSrc1.highReg);
873 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
874 rlSrc2.lowReg);
875 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
876 rlSrc2.highReg);
877 oatFreeTemp(cUnit, tReg);
878 } else {
879 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
880 rlSrc2.lowReg);
881 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
882 rlSrc2.highReg);
883 }
buzbee439c4fa2011-08-27 15:59:07 -0700884 /*
885 * NOTE: If rlDest refers to a frame variable in a large frame, the
886 * following storeValueWide might need to allocate a temp register.
887 * To further work around the lack of a spill capability, explicitly
888 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
889 * Remove when spill is functional.
890 */
891 freeRegLocTemps(cUnit, rlResult, rlSrc1);
892 freeRegLocTemps(cUnit, rlResult, rlSrc2);
buzbee67bf8852011-08-17 17:51:35 -0700893 storeValueWide(cUnit, rlDest, rlResult);
buzbee9e0f9b02011-08-24 15:32:46 -0700894 oatClobber(cUnit, rLR);
895 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee67bf8852011-08-17 17:51:35 -0700896}
897
898void oatInitializeRegAlloc(CompilationUnit* cUnit)
899{
900 int numRegs = sizeof(coreRegs)/sizeof(*coreRegs);
901 int numReserved = sizeof(reservedRegs)/sizeof(*reservedRegs);
902 int numTemps = sizeof(coreTemps)/sizeof(*coreTemps);
903 int numFPRegs = sizeof(fpRegs)/sizeof(*fpRegs);
904 int numFPTemps = sizeof(fpTemps)/sizeof(*fpTemps);
905 RegisterPool *pool = (RegisterPool *)oatNew(sizeof(*pool), true);
906 cUnit->regPool = pool;
907 pool->numCoreRegs = numRegs;
908 pool->coreRegs = (RegisterInfo *)
909 oatNew(numRegs * sizeof(*cUnit->regPool->coreRegs), true);
910 pool->numFPRegs = numFPRegs;
911 pool->FPRegs = (RegisterInfo *)
912 oatNew(numFPRegs * sizeof(*cUnit->regPool->FPRegs), true);
913 oatInitPool(pool->coreRegs, coreRegs, pool->numCoreRegs);
914 oatInitPool(pool->FPRegs, fpRegs, pool->numFPRegs);
915 // Keep special registers from being allocated
916 for (int i = 0; i < numReserved; i++) {
buzbeec0ecd652011-09-25 18:11:54 -0700917 if (NO_SUSPEND && (reservedRegs[i] == rSUSPEND)) {
918 //To measure cost of suspend check
919 continue;
920 }
buzbee67bf8852011-08-17 17:51:35 -0700921 oatMarkInUse(cUnit, reservedRegs[i]);
922 }
923 // Mark temp regs - all others not in use can be used for promotion
924 for (int i = 0; i < numTemps; i++) {
925 oatMarkTemp(cUnit, coreTemps[i]);
926 }
927 for (int i = 0; i < numFPTemps; i++) {
928 oatMarkTemp(cUnit, fpTemps[i]);
929 }
buzbeec0ecd652011-09-25 18:11:54 -0700930 // Construct the alias map.
931 cUnit->phiAliasMap = (int*)oatNew(cUnit->numSSARegs *
932 sizeof(cUnit->phiAliasMap[0]), false);
933 for (int i = 0; i < cUnit->numSSARegs; i++) {
934 cUnit->phiAliasMap[i] = i;
935 }
936 for (MIR* phi = cUnit->phiList; phi; phi = phi->meta.phiNext) {
937 int defReg = phi->ssaRep->defs[0];
938 for (int i = 0; i < phi->ssaRep->numUses; i++) {
939 for (int j = 0; j < cUnit->numSSARegs; j++) {
940 if (cUnit->phiAliasMap[j] == phi->ssaRep->uses[i]) {
941 cUnit->phiAliasMap[j] = defReg;
942 }
943 }
944 }
945 }
buzbee67bf8852011-08-17 17:51:35 -0700946}
947
948/*
949 * Handle simple case (thin lock) inline. If it's complicated, bail
950 * out to the heavyweight lock/unlock routines. We'll use dedicated
951 * registers here in order to be in the right position in case we
952 * to bail to dvm[Lock/Unlock]Object(self, object)
953 *
954 * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object
955 * r1 -> object [arg1 for dvm[Lock/Unlock]Object
956 * r2 -> intial contents of object->lock, later result of strex
957 * r3 -> self->threadId
958 * r12 -> allow to be used by utilities as general temp
959 *
960 * The result of the strex is 0 if we acquire the lock.
961 *
962 * See comments in Sync.c for the layout of the lock word.
963 * Of particular interest to this code is the test for the
964 * simple case - which we handle inline. For monitor enter, the
965 * simple case is thin lock, held by no-one. For monitor exit,
966 * the simple case is thin lock, held by the unlocking thread with
967 * a recurse count of 0.
968 *
969 * A minor complication is that there is a field in the lock word
970 * unrelated to locking: the hash state. This field must be ignored, but
971 * preserved.
972 *
973 */
buzbeeed3e9302011-09-23 17:34:19 -0700974STATIC void genMonitorEnter(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700975 RegLocation rlSrc)
976{
977 ArmLIR* target;
978 ArmLIR* hopTarget;
979 ArmLIR* branch;
980 ArmLIR* hopBranch;
981
982 oatFlushAllRegs(cUnit);
Elliott Hughes5f791332011-09-15 17:45:30 -0700983 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700984 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700985 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee5ade1d22011-09-09 14:44:52 -0700986 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700987 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -0700988 newLIR3(cUnit, kThumb2Ldrex, r2, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700989 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
buzbeec143c552011-08-20 17:38:58 -0700990 // Align owner
Elliott Hughes5f791332011-09-15 17:45:30 -0700991 opRegImm(cUnit, kOpLsl, r3, LW_LOCK_OWNER_SHIFT);
buzbee67bf8852011-08-17 17:51:35 -0700992 // Is lock unheld on lock or held by us (==threadId) on unlock?
Elliott Hughes5f791332011-09-15 17:45:30 -0700993 newLIR4(cUnit, kThumb2Bfi, r3, r2, 0, LW_LOCK_OWNER_SHIFT - 1);
994 newLIR3(cUnit, kThumb2Bfc, r2, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee67bf8852011-08-17 17:51:35 -0700995 hopBranch = newLIR2(cUnit, kThumb2Cbnz, r2, 0);
buzbeec143c552011-08-20 17:38:58 -0700996 newLIR4(cUnit, kThumb2Strex, r2, r3, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700997 Object::MonitorOffset().Int32Value() >> 2);
buzbee67bf8852011-08-17 17:51:35 -0700998 oatGenMemBarrier(cUnit, kSY);
999 branch = newLIR2(cUnit, kThumb2Cbz, r2, 0);
1000
1001 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
1002 hopTarget->defMask = ENCODE_ALL;
1003 hopBranch->generic.target = (LIR*)hopTarget;
1004
buzbee1b4c8592011-08-31 10:43:51 -07001005 // Go expensive route - artLockObjectFromCode(self, obj);
1006 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -07001007 rLR);
1008 genRegCopy(cUnit, r0, rSELF);
Ian Rogersff1ed472011-09-20 13:46:24 -07001009 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001010
1011 // Resume here
1012 target = newLIR0(cUnit, kArmPseudoTargetLabel);
1013 target->defMask = ENCODE_ALL;
1014 branch->generic.target = (LIR*)target;
1015}
1016
1017/*
1018 * For monitor unlock, we don't have to use ldrex/strex. Once
1019 * we've determined that the lock is thin and that we own it with
1020 * a zero recursion count, it's safe to punch it back to the
1021 * initial, unlock thin state with a store word.
1022 */
buzbeeed3e9302011-09-23 17:34:19 -07001023STATIC void genMonitorExit(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001024 RegLocation rlSrc)
1025{
1026 ArmLIR* target;
1027 ArmLIR* branch;
1028 ArmLIR* hopTarget;
1029 ArmLIR* hopBranch;
1030
Elliott Hughes5f791332011-09-15 17:45:30 -07001031 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -07001032 oatFlushAllRegs(cUnit);
1033 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -07001034 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee5ade1d22011-09-09 14:44:52 -07001035 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001036 loadWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r2); // Get lock
Elliott Hughes54e7df12011-09-16 11:47:04 -07001037 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -07001038 // Is lock unheld on lock or held by us (==threadId) on unlock?
Elliott Hughes5f791332011-09-15 17:45:30 -07001039 opRegRegImm(cUnit, kOpAnd, r12, r2, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
buzbeec143c552011-08-20 17:38:58 -07001040 // Align owner
Elliott Hughes5f791332011-09-15 17:45:30 -07001041 opRegImm(cUnit, kOpLsl, r3, LW_LOCK_OWNER_SHIFT);
1042 newLIR3(cUnit, kThumb2Bfc, r2, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee67bf8852011-08-17 17:51:35 -07001043 opRegReg(cUnit, kOpSub, r2, r3);
1044 hopBranch = opCondBranch(cUnit, kArmCondNe);
1045 oatGenMemBarrier(cUnit, kSY);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001046 storeWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r12);
buzbee67bf8852011-08-17 17:51:35 -07001047 branch = opNone(cUnit, kOpUncondBr);
1048
1049 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
1050 hopTarget->defMask = ENCODE_ALL;
1051 hopBranch->generic.target = (LIR*)hopTarget;
1052
buzbee1b4c8592011-08-31 10:43:51 -07001053 // Go expensive route - UnlockObjectFromCode(self, obj);
1054 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -07001055 rLR);
1056 genRegCopy(cUnit, r0, rSELF);
Ian Rogersff1ed472011-09-20 13:46:24 -07001057 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001058
1059 // Resume here
1060 target = newLIR0(cUnit, kArmPseudoTargetLabel);
1061 target->defMask = ENCODE_ALL;
1062 branch->generic.target = (LIR*)target;
1063}
1064
1065/*
1066 * 64-bit 3way compare function.
1067 * mov rX, #-1
1068 * cmp op1hi, op2hi
1069 * blt done
1070 * bgt flip
1071 * sub rX, op1lo, op2lo (treat as unsigned)
1072 * beq done
1073 * ite hi
1074 * mov(hi) rX, #-1
1075 * mov(!hi) rX, #1
1076 * flip:
1077 * neg rX
1078 * done:
1079 */
buzbeeed3e9302011-09-23 17:34:19 -07001080STATIC void genCmpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001081 RegLocation rlDest, RegLocation rlSrc1,
1082 RegLocation rlSrc2)
1083{
buzbee67bf8852011-08-17 17:51:35 -07001084 ArmLIR* target1;
1085 ArmLIR* target2;
1086 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1087 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
buzbeeb29e4d12011-09-26 15:05:48 -07001088 int tReg = oatAllocTemp(cUnit);
1089 loadConstant(cUnit, tReg, -1);
buzbee67bf8852011-08-17 17:51:35 -07001090 opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg);
1091 ArmLIR* branch1 = opCondBranch(cUnit, kArmCondLt);
1092 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondGt);
buzbeeb29e4d12011-09-26 15:05:48 -07001093 opRegRegReg(cUnit, kOpSub, tReg, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001094 ArmLIR* branch3 = opCondBranch(cUnit, kArmCondEq);
1095
1096 genIT(cUnit, kArmCondHi, "E");
buzbeeb29e4d12011-09-26 15:05:48 -07001097 newLIR2(cUnit, kThumb2MovImmShift, tReg, modifiedImmediate(-1));
1098 loadConstant(cUnit, tReg, 1);
buzbee67bf8852011-08-17 17:51:35 -07001099 genBarrier(cUnit);
1100
1101 target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
1102 target2->defMask = -1;
buzbeeb29e4d12011-09-26 15:05:48 -07001103 opRegReg(cUnit, kOpNeg, tReg, tReg);
buzbee67bf8852011-08-17 17:51:35 -07001104
1105 target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
1106 target1->defMask = -1;
1107
buzbeeb29e4d12011-09-26 15:05:48 -07001108 RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change
1109 rlTemp.lowReg = tReg;
buzbee67bf8852011-08-17 17:51:35 -07001110 storeValue(cUnit, rlDest, rlTemp);
buzbeeb29e4d12011-09-26 15:05:48 -07001111 oatFreeTemp(cUnit, tReg);
buzbee67bf8852011-08-17 17:51:35 -07001112
1113 branch1->generic.target = (LIR*)target1;
1114 branch2->generic.target = (LIR*)target2;
1115 branch3->generic.target = branch1->generic.target;
1116}
1117
buzbeeed3e9302011-09-23 17:34:19 -07001118STATIC void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001119 RegLocation rlSrc, RegLocation rlResult, int lit,
1120 int firstBit, int secondBit)
1121{
1122 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1123 encodeShift(kArmLsl, secondBit - firstBit));
1124 if (firstBit != 0) {
1125 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1126 }
1127}
1128
buzbeeed3e9302011-09-23 17:34:19 -07001129STATIC bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
buzbee67bf8852011-08-17 17:51:35 -07001130 int srcSize, int tgtSize)
1131{
1132 /*
1133 * Don't optimize the register usage since it calls out to support
1134 * functions
1135 */
1136 RegLocation rlSrc;
1137 RegLocation rlDest;
1138 oatFlushAllRegs(cUnit); /* Send everything to home location */
1139 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1140 if (srcSize == 1) {
1141 rlSrc = oatGetSrc(cUnit, mir, 0);
1142 loadValueDirectFixed(cUnit, rlSrc, r0);
1143 } else {
1144 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
1145 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
1146 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001147 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001148 if (tgtSize == 1) {
1149 RegLocation rlResult;
1150 rlDest = oatGetDest(cUnit, mir, 0);
1151 rlResult = oatGetReturn(cUnit);
1152 storeValue(cUnit, rlDest, rlResult);
1153 } else {
1154 RegLocation rlResult;
1155 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1156 rlResult = oatGetReturnWide(cUnit);
1157 storeValueWide(cUnit, rlDest, rlResult);
1158 }
1159 return false;
1160}
1161
buzbeeed3e9302011-09-23 17:34:19 -07001162STATIC bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001163 RegLocation rlDest, RegLocation rlSrc1,
1164 RegLocation rlSrc2)
1165{
1166 RegLocation rlResult;
1167 int funcOffset;
1168
1169 switch (mir->dalvikInsn.opcode) {
1170 case OP_ADD_FLOAT_2ADDR:
1171 case OP_ADD_FLOAT:
1172 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
1173 break;
1174 case OP_SUB_FLOAT_2ADDR:
1175 case OP_SUB_FLOAT:
1176 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
1177 break;
1178 case OP_DIV_FLOAT_2ADDR:
1179 case OP_DIV_FLOAT:
1180 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
1181 break;
1182 case OP_MUL_FLOAT_2ADDR:
1183 case OP_MUL_FLOAT:
1184 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
1185 break;
1186 case OP_REM_FLOAT_2ADDR:
1187 case OP_REM_FLOAT:
1188 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
1189 break;
1190 case OP_NEG_FLOAT: {
1191 genNegFloat(cUnit, rlDest, rlSrc1);
1192 return false;
1193 }
1194 default:
1195 return true;
1196 }
1197 oatFlushAllRegs(cUnit); /* Send everything to home location */
1198 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1199 loadValueDirectFixed(cUnit, rlSrc1, r0);
1200 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001201 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001202 rlResult = oatGetReturn(cUnit);
1203 storeValue(cUnit, rlDest, rlResult);
1204 return false;
1205}
1206
buzbeeed3e9302011-09-23 17:34:19 -07001207STATIC bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001208 RegLocation rlDest, RegLocation rlSrc1,
1209 RegLocation rlSrc2)
1210{
1211 RegLocation rlResult;
1212 int funcOffset;
1213
1214 switch (mir->dalvikInsn.opcode) {
1215 case OP_ADD_DOUBLE_2ADDR:
1216 case OP_ADD_DOUBLE:
1217 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
1218 break;
1219 case OP_SUB_DOUBLE_2ADDR:
1220 case OP_SUB_DOUBLE:
1221 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
1222 break;
1223 case OP_DIV_DOUBLE_2ADDR:
1224 case OP_DIV_DOUBLE:
1225 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
1226 break;
1227 case OP_MUL_DOUBLE_2ADDR:
1228 case OP_MUL_DOUBLE:
1229 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
1230 break;
1231 case OP_REM_DOUBLE_2ADDR:
1232 case OP_REM_DOUBLE:
1233 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
1234 break;
1235 case OP_NEG_DOUBLE: {
1236 genNegDouble(cUnit, rlDest, rlSrc1);
1237 return false;
1238 }
1239 default:
1240 return true;
1241 }
1242 oatFlushAllRegs(cUnit); /* Send everything to home location */
1243 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1244 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1245 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -07001246 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001247 rlResult = oatGetReturnWide(cUnit);
1248 storeValueWide(cUnit, rlDest, rlResult);
1249 return false;
1250}
1251
buzbeeed3e9302011-09-23 17:34:19 -07001252STATIC bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001253{
1254 Opcode opcode = mir->dalvikInsn.opcode;
1255
1256 switch (opcode) {
1257 case OP_INT_TO_FLOAT:
1258 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
1259 1, 1);
1260 case OP_FLOAT_TO_INT:
1261 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
1262 1, 1);
1263 case OP_DOUBLE_TO_FLOAT:
1264 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
1265 2, 1);
1266 case OP_FLOAT_TO_DOUBLE:
1267 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
1268 1, 2);
1269 case OP_INT_TO_DOUBLE:
1270 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
1271 1, 2);
1272 case OP_DOUBLE_TO_INT:
1273 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
1274 2, 1);
1275 case OP_FLOAT_TO_LONG:
1276 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001277 pF2l), 1, 2);
buzbee67bf8852011-08-17 17:51:35 -07001278 case OP_LONG_TO_FLOAT:
1279 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
1280 2, 1);
1281 case OP_DOUBLE_TO_LONG:
1282 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001283 pD2l), 2, 2);
buzbee67bf8852011-08-17 17:51:35 -07001284 case OP_LONG_TO_DOUBLE:
1285 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
1286 2, 2);
1287 default:
1288 return true;
1289 }
1290 return false;
1291}
1292
1293/* Generate conditional branch instructions */
buzbeeed3e9302011-09-23 17:34:19 -07001294STATIC ArmLIR* genConditionalBranch(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001295 ArmConditionCode cond,
1296 ArmLIR* target)
1297{
1298 ArmLIR* branch = opCondBranch(cUnit, cond);
1299 branch->generic.target = (LIR*) target;
1300 return branch;
1301}
1302
buzbee67bf8852011-08-17 17:51:35 -07001303/*
1304 * Generate array store
1305 *
1306 */
buzbeeed3e9302011-09-23 17:34:19 -07001307STATIC void genArrayObjPut(CompilationUnit* cUnit, MIR* mir,
buzbee1b4c8592011-08-31 10:43:51 -07001308 RegLocation rlArray, RegLocation rlIndex,
1309 RegLocation rlSrc, int scale)
buzbee67bf8852011-08-17 17:51:35 -07001310{
1311 RegisterClass regClass = oatRegClassBySize(kWord);
buzbeec143c552011-08-20 17:38:58 -07001312 int lenOffset = Array::LengthOffset().Int32Value();
1313 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001314
buzbee6181f792011-09-29 11:14:04 -07001315 oatFlushAllRegs(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001316 /* Make sure it's a legal object Put. Use direct regs at first */
1317 loadValueDirectFixed(cUnit, rlArray, r1);
1318 loadValueDirectFixed(cUnit, rlSrc, r0);
1319
1320 /* null array object? */
buzbee43a36422011-09-14 14:00:13 -07001321 genNullCheck(cUnit, rlArray.sRegLow, r1, mir);
buzbee67bf8852011-08-17 17:51:35 -07001322 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -07001323 OFFSETOF_MEMBER(Thread, pCanPutArrayElementFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -07001324 /* Get the array's clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001325 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001326 callRuntimeHelper(cUnit, rLR);
buzbee6181f792011-09-29 11:14:04 -07001327 oatFreeTemp(cUnit, r0);
1328 oatFreeTemp(cUnit, r1);
buzbee67bf8852011-08-17 17:51:35 -07001329
1330 // Now, redo loadValues in case they didn't survive the call
1331
1332 int regPtr;
1333 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1334 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1335
1336 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1337 oatClobber(cUnit, rlArray.lowReg);
1338 regPtr = rlArray.lowReg;
1339 } else {
1340 regPtr = oatAllocTemp(cUnit);
1341 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1342 }
1343
buzbee43a36422011-09-14 14:00:13 -07001344 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001345 int regLen = oatAllocTemp(cUnit);
1346 //NOTE: max live temps(4) here.
1347 /* Get len */
1348 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1349 /* regPtr -> array data */
1350 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001351 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001352 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001353 oatFreeTemp(cUnit, regLen);
1354 } else {
1355 /* regPtr -> array data */
1356 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1357 }
1358 /* at this point, regPtr points to array, 2 live temps */
1359 rlSrc = loadValue(cUnit, rlSrc, regClass);
1360 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1361 scale, kWord);
1362}
1363
1364/*
1365 * Generate array load
1366 */
buzbeeed3e9302011-09-23 17:34:19 -07001367STATIC void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -07001368 RegLocation rlArray, RegLocation rlIndex,
1369 RegLocation rlDest, int scale)
1370{
1371 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001372 int lenOffset = Array::LengthOffset().Int32Value();
1373 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001374 RegLocation rlResult;
1375 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1376 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1377 int regPtr;
1378
1379 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001380 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001381
1382 regPtr = oatAllocTemp(cUnit);
1383
buzbee43a36422011-09-14 14:00:13 -07001384 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001385 int regLen = oatAllocTemp(cUnit);
1386 /* Get len */
1387 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1388 /* regPtr -> array data */
1389 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001390 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001391 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001392 oatFreeTemp(cUnit, regLen);
1393 } else {
1394 /* regPtr -> array data */
1395 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1396 }
buzbeee9a72f62011-09-04 17:59:07 -07001397 oatFreeTemp(cUnit, rlArray.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001398 if ((size == kLong) || (size == kDouble)) {
1399 if (scale) {
1400 int rNewIndex = oatAllocTemp(cUnit);
1401 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1402 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1403 oatFreeTemp(cUnit, rNewIndex);
1404 } else {
1405 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1406 }
buzbeee9a72f62011-09-04 17:59:07 -07001407 oatFreeTemp(cUnit, rlIndex.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001408 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1409
1410 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1411
1412 oatFreeTemp(cUnit, regPtr);
1413 storeValueWide(cUnit, rlDest, rlResult);
1414 } else {
1415 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1416
1417 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1418 scale, size);
1419
1420 oatFreeTemp(cUnit, regPtr);
1421 storeValue(cUnit, rlDest, rlResult);
1422 }
1423}
1424
1425/*
1426 * Generate array store
1427 *
1428 */
buzbeeed3e9302011-09-23 17:34:19 -07001429STATIC void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -07001430 RegLocation rlArray, RegLocation rlIndex,
1431 RegLocation rlSrc, int scale)
1432{
1433 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001434 int lenOffset = Array::LengthOffset().Int32Value();
1435 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001436
1437 int regPtr;
1438 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1439 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1440
1441 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1442 oatClobber(cUnit, rlArray.lowReg);
1443 regPtr = rlArray.lowReg;
1444 } else {
1445 regPtr = oatAllocTemp(cUnit);
1446 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1447 }
1448
1449 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001450 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001451
buzbee43a36422011-09-14 14:00:13 -07001452 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001453 int regLen = oatAllocTemp(cUnit);
1454 //NOTE: max live temps(4) here.
1455 /* Get len */
1456 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1457 /* regPtr -> array data */
1458 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001459 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001460 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001461 oatFreeTemp(cUnit, regLen);
1462 } else {
1463 /* regPtr -> array data */
1464 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1465 }
1466 /* at this point, regPtr points to array, 2 live temps */
1467 if ((size == kLong) || (size == kDouble)) {
buzbee5ade1d22011-09-09 14:44:52 -07001468 //TUNING: specific wide routine that can handle fp regs
buzbee67bf8852011-08-17 17:51:35 -07001469 if (scale) {
1470 int rNewIndex = oatAllocTemp(cUnit);
1471 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1472 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1473 oatFreeTemp(cUnit, rNewIndex);
1474 } else {
1475 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1476 }
1477 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1478
1479 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1480
1481 oatFreeTemp(cUnit, regPtr);
1482 } else {
1483 rlSrc = loadValue(cUnit, rlSrc, regClass);
1484
1485 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1486 scale, size);
1487 }
1488}
1489
buzbeeed3e9302011-09-23 17:34:19 -07001490STATIC bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001491 RegLocation rlDest, RegLocation rlSrc1,
1492 RegLocation rlShift)
1493{
buzbee54330722011-08-23 16:46:55 -07001494 int funcOffset;
buzbee67bf8852011-08-17 17:51:35 -07001495
buzbee67bf8852011-08-17 17:51:35 -07001496 switch( mir->dalvikInsn.opcode) {
1497 case OP_SHL_LONG:
1498 case OP_SHL_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001499 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
buzbee67bf8852011-08-17 17:51:35 -07001500 break;
1501 case OP_SHR_LONG:
1502 case OP_SHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001503 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
buzbee67bf8852011-08-17 17:51:35 -07001504 break;
1505 case OP_USHR_LONG:
1506 case OP_USHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001507 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
buzbee67bf8852011-08-17 17:51:35 -07001508 break;
1509 default:
buzbee54330722011-08-23 16:46:55 -07001510 LOG(FATAL) << "Unexpected case";
buzbee67bf8852011-08-17 17:51:35 -07001511 return true;
1512 }
buzbee54330722011-08-23 16:46:55 -07001513 oatFlushAllRegs(cUnit); /* Send everything to home location */
1514 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1515 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1516 loadValueDirect(cUnit, rlShift, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -07001517 callRuntimeHelper(cUnit, rLR);
buzbee54330722011-08-23 16:46:55 -07001518 RegLocation rlResult = oatGetReturnWide(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001519 storeValueWide(cUnit, rlDest, rlResult);
1520 return false;
1521}
1522
buzbeeed3e9302011-09-23 17:34:19 -07001523STATIC bool genArithOpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001524 RegLocation rlDest, RegLocation rlSrc1,
1525 RegLocation rlSrc2)
1526{
1527 RegLocation rlResult;
1528 OpKind firstOp = kOpBkpt;
1529 OpKind secondOp = kOpBkpt;
1530 bool callOut = false;
buzbee58f92742011-10-01 11:22:17 -07001531 bool checkZero = false;
buzbee67bf8852011-08-17 17:51:35 -07001532 int funcOffset;
1533 int retReg = r0;
1534
1535 switch (mir->dalvikInsn.opcode) {
1536 case OP_NOT_LONG:
1537 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1538 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeeb29e4d12011-09-26 15:05:48 -07001539 // Check for destructive overlap
1540 if (rlResult.lowReg == rlSrc2.highReg) {
1541 int tReg = oatAllocTemp(cUnit);
1542 genRegCopy(cUnit, tReg, rlSrc2.highReg);
1543 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1544 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
1545 oatFreeTemp(cUnit, tReg);
1546 } else {
1547 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1548 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1549 }
buzbee67bf8852011-08-17 17:51:35 -07001550 storeValueWide(cUnit, rlDest, rlResult);
1551 return false;
1552 break;
1553 case OP_ADD_LONG:
1554 case OP_ADD_LONG_2ADDR:
1555 firstOp = kOpAdd;
1556 secondOp = kOpAdc;
1557 break;
1558 case OP_SUB_LONG:
1559 case OP_SUB_LONG_2ADDR:
1560 firstOp = kOpSub;
1561 secondOp = kOpSbc;
1562 break;
1563 case OP_MUL_LONG:
1564 case OP_MUL_LONG_2ADDR:
buzbee439c4fa2011-08-27 15:59:07 -07001565 callOut = true;
1566 retReg = r0;
1567 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1568 break;
buzbee67bf8852011-08-17 17:51:35 -07001569 case OP_DIV_LONG:
1570 case OP_DIV_LONG_2ADDR:
1571 callOut = true;
buzbee58f92742011-10-01 11:22:17 -07001572 checkZero = true;
buzbee67bf8852011-08-17 17:51:35 -07001573 retReg = r0;
1574 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1575 break;
1576 /* NOTE - result is in r2/r3 instead of r0/r1 */
1577 case OP_REM_LONG:
1578 case OP_REM_LONG_2ADDR:
1579 callOut = true;
buzbee58f92742011-10-01 11:22:17 -07001580 checkZero = true;
buzbee67bf8852011-08-17 17:51:35 -07001581 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1582 retReg = r2;
1583 break;
1584 case OP_AND_LONG_2ADDR:
1585 case OP_AND_LONG:
1586 firstOp = kOpAnd;
1587 secondOp = kOpAnd;
1588 break;
1589 case OP_OR_LONG:
1590 case OP_OR_LONG_2ADDR:
1591 firstOp = kOpOr;
1592 secondOp = kOpOr;
1593 break;
1594 case OP_XOR_LONG:
1595 case OP_XOR_LONG_2ADDR:
1596 firstOp = kOpXor;
1597 secondOp = kOpXor;
1598 break;
1599 case OP_NEG_LONG: {
buzbee67bf8852011-08-17 17:51:35 -07001600 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1601 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeeb29e4d12011-09-26 15:05:48 -07001602 int zReg = oatAllocTemp(cUnit);
1603 loadConstantNoClobber(cUnit, zReg, 0);
1604 // Check for destructive overlap
1605 if (rlResult.lowReg == rlSrc2.highReg) {
1606 int tReg = oatAllocTemp(cUnit);
1607 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1608 zReg, rlSrc2.lowReg);
1609 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1610 zReg, tReg);
1611 oatFreeTemp(cUnit, tReg);
1612 } else {
1613 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1614 zReg, rlSrc2.lowReg);
1615 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1616 zReg, rlSrc2.highReg);
1617 }
1618 oatFreeTemp(cUnit, zReg);
buzbee67bf8852011-08-17 17:51:35 -07001619 storeValueWide(cUnit, rlDest, rlResult);
1620 return false;
1621 }
1622 default:
1623 LOG(FATAL) << "Invalid long arith op";
1624 }
1625 if (!callOut) {
1626 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1627 } else {
buzbee67bf8852011-08-17 17:51:35 -07001628 oatFlushAllRegs(cUnit); /* Send everything to home location */
buzbee58f92742011-10-01 11:22:17 -07001629 if (checkZero) {
1630 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1631 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1632 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1633 int tReg = oatAllocTemp(cUnit);
1634 newLIR4(cUnit, kThumb2OrrRRRs, tReg, r2, r3, 0);
1635 oatFreeTemp(cUnit, tReg);
1636 genCheck(cUnit, kArmCondEq, mir, kArmThrowDivZero);
1637 } else {
1638 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1639 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1640 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
1641 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001642 callRuntimeHelper(cUnit, rLR);
buzbee58f92742011-10-01 11:22:17 -07001643 // Adjust return regs in to handle case of rem returning r2/r3
buzbee67bf8852011-08-17 17:51:35 -07001644 if (retReg == r0)
1645 rlResult = oatGetReturnWide(cUnit);
1646 else
1647 rlResult = oatGetReturnWideAlt(cUnit);
1648 storeValueWide(cUnit, rlDest, rlResult);
1649 }
1650 return false;
1651}
1652
buzbeeed3e9302011-09-23 17:34:19 -07001653STATIC bool genArithOpInt(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001654 RegLocation rlDest, RegLocation rlSrc1,
1655 RegLocation rlSrc2)
1656{
1657 OpKind op = kOpBkpt;
1658 bool callOut = false;
1659 bool checkZero = false;
1660 bool unary = false;
1661 int retReg = r0;
1662 int funcOffset;
1663 RegLocation rlResult;
1664 bool shiftOp = false;
1665
1666 switch (mir->dalvikInsn.opcode) {
1667 case OP_NEG_INT:
1668 op = kOpNeg;
1669 unary = true;
1670 break;
1671 case OP_NOT_INT:
1672 op = kOpMvn;
1673 unary = true;
1674 break;
1675 case OP_ADD_INT:
1676 case OP_ADD_INT_2ADDR:
1677 op = kOpAdd;
1678 break;
1679 case OP_SUB_INT:
1680 case OP_SUB_INT_2ADDR:
1681 op = kOpSub;
1682 break;
1683 case OP_MUL_INT:
1684 case OP_MUL_INT_2ADDR:
1685 op = kOpMul;
1686 break;
1687 case OP_DIV_INT:
1688 case OP_DIV_INT_2ADDR:
1689 callOut = true;
1690 checkZero = true;
1691 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1692 retReg = r0;
1693 break;
1694 /* NOTE: returns in r1 */
1695 case OP_REM_INT:
1696 case OP_REM_INT_2ADDR:
1697 callOut = true;
1698 checkZero = true;
1699 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1700 retReg = r1;
1701 break;
1702 case OP_AND_INT:
1703 case OP_AND_INT_2ADDR:
1704 op = kOpAnd;
1705 break;
1706 case OP_OR_INT:
1707 case OP_OR_INT_2ADDR:
1708 op = kOpOr;
1709 break;
1710 case OP_XOR_INT:
1711 case OP_XOR_INT_2ADDR:
1712 op = kOpXor;
1713 break;
1714 case OP_SHL_INT:
1715 case OP_SHL_INT_2ADDR:
1716 shiftOp = true;
1717 op = kOpLsl;
1718 break;
1719 case OP_SHR_INT:
1720 case OP_SHR_INT_2ADDR:
1721 shiftOp = true;
1722 op = kOpAsr;
1723 break;
1724 case OP_USHR_INT:
1725 case OP_USHR_INT_2ADDR:
1726 shiftOp = true;
1727 op = kOpLsr;
1728 break;
1729 default:
1730 LOG(FATAL) << "Invalid word arith op: " <<
1731 (int)mir->dalvikInsn.opcode;
1732 }
1733 if (!callOut) {
1734 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1735 if (unary) {
1736 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1737 opRegReg(cUnit, op, rlResult.lowReg,
1738 rlSrc1.lowReg);
1739 } else {
1740 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1741 if (shiftOp) {
1742 int tReg = oatAllocTemp(cUnit);
1743 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1744 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1745 opRegRegReg(cUnit, op, rlResult.lowReg,
1746 rlSrc1.lowReg, tReg);
1747 oatFreeTemp(cUnit, tReg);
1748 } else {
1749 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1750 opRegRegReg(cUnit, op, rlResult.lowReg,
1751 rlSrc1.lowReg, rlSrc2.lowReg);
1752 }
1753 }
1754 storeValue(cUnit, rlDest, rlResult);
1755 } else {
1756 RegLocation rlResult;
1757 oatFlushAllRegs(cUnit); /* Send everything to home location */
1758 loadValueDirectFixed(cUnit, rlSrc2, r1);
1759 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1760 loadValueDirectFixed(cUnit, rlSrc1, r0);
1761 if (checkZero) {
buzbee5ade1d22011-09-09 14:44:52 -07001762 genImmedCheck(cUnit, kArmCondEq, r1, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07001763 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001764 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001765 if (retReg == r0)
1766 rlResult = oatGetReturn(cUnit);
1767 else
1768 rlResult = oatGetReturnAlt(cUnit);
1769 storeValue(cUnit, rlDest, rlResult);
1770 }
1771 return false;
1772}
1773
buzbeec1f45042011-09-21 16:03:19 -07001774/* Check if we need to check for pending suspend request */
buzbeeed3e9302011-09-23 17:34:19 -07001775STATIC void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
buzbeec1f45042011-09-21 16:03:19 -07001776{
buzbeec0ecd652011-09-25 18:11:54 -07001777 if (NO_SUSPEND || mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK) {
buzbeec1f45042011-09-21 16:03:19 -07001778 return;
1779 }
buzbee6181f792011-09-29 11:14:04 -07001780 oatFlushAllRegs(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07001781 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
1782 ArmLIR* branch = opCondBranch(cUnit, kArmCondEq);
1783 ArmLIR* retLab = newLIR0(cUnit, kArmPseudoTargetLabel);
1784 retLab->defMask = ENCODE_ALL;
1785 ArmLIR* target = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
1786 target->generic.dalvikOffset = cUnit->currentDalvikOffset;
1787 target->opcode = kArmPseudoSuspendTarget;
1788 target->operands[0] = (intptr_t)retLab;
1789 target->operands[1] = mir->offset;
1790 branch->generic.target = (LIR*)target;
1791 oatInsertGrowableList(&cUnit->suspendLaunchpads, (intptr_t)target);
1792}
1793
buzbee0d966cf2011-09-08 17:34:58 -07001794/* Check for pending suspend request. */
buzbeeed3e9302011-09-23 17:34:19 -07001795STATIC void genSuspendPoll(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001796{
buzbeec0ecd652011-09-25 18:11:54 -07001797 if (NO_SUSPEND || mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK) {
buzbeec1f45042011-09-21 16:03:19 -07001798 return;
1799 }
buzbee6181f792011-09-29 11:14:04 -07001800 oatFlushAllRegs(cUnit);
buzbee0d966cf2011-09-08 17:34:58 -07001801 oatLockCallTemps(cUnit); // Explicit register usage
1802 int rSuspendCount = r1;
buzbee67bf8852011-08-17 17:51:35 -07001803 ArmLIR* ld;
buzbee0d966cf2011-09-08 17:34:58 -07001804 ld = loadWordDisp(cUnit, rSELF,
1805 art::Thread::SuspendCountOffset().Int32Value(), rSuspendCount);
buzbee67bf8852011-08-17 17:51:35 -07001806 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
buzbee0d966cf2011-09-08 17:34:58 -07001807 loadWordDisp(cUnit, rSELF,
1808 OFFSETOF_MEMBER(Thread, pCheckSuspendFromCode), rLR);
1809 genRegCopy(cUnit, r0, rSELF);
1810 opRegImm(cUnit, kOpCmp, rSuspendCount, 0);
1811 genIT(cUnit, kArmCondNe, "");
buzbeece302932011-10-04 14:32:18 -07001812 opReg(cUnit, kOpBlx, rLR);
buzbee0d966cf2011-09-08 17:34:58 -07001813 oatFreeCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001814}
1815
1816/*
1817 * The following are the first-level codegen routines that analyze the format
1818 * of each bytecode then either dispatch special purpose codegen routines
1819 * or produce corresponding Thumb instructions directly.
1820 */
1821
buzbeeed3e9302011-09-23 17:34:19 -07001822STATIC bool isPowerOfTwo(int x)
buzbee67bf8852011-08-17 17:51:35 -07001823{
1824 return (x & (x - 1)) == 0;
1825}
1826
1827// Returns true if no more than two bits are set in 'x'.
buzbeeed3e9302011-09-23 17:34:19 -07001828STATIC bool isPopCountLE2(unsigned int x)
buzbee67bf8852011-08-17 17:51:35 -07001829{
1830 x &= x - 1;
1831 return (x & (x - 1)) == 0;
1832}
1833
1834// Returns the index of the lowest set bit in 'x'.
buzbeeed3e9302011-09-23 17:34:19 -07001835STATIC int lowestSetBit(unsigned int x) {
buzbee67bf8852011-08-17 17:51:35 -07001836 int bit_posn = 0;
1837 while ((x & 0xf) == 0) {
1838 bit_posn += 4;
1839 x >>= 4;
1840 }
1841 while ((x & 1) == 0) {
1842 bit_posn++;
1843 x >>= 1;
1844 }
1845 return bit_posn;
1846}
1847
1848// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1849// and store the result in 'rlDest'.
buzbeeed3e9302011-09-23 17:34:19 -07001850STATIC bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode,
buzbee67bf8852011-08-17 17:51:35 -07001851 RegLocation rlSrc, RegLocation rlDest, int lit)
1852{
1853 if (lit < 2 || !isPowerOfTwo(lit)) {
1854 return false;
1855 }
1856 int k = lowestSetBit(lit);
1857 if (k >= 30) {
1858 // Avoid special cases.
1859 return false;
1860 }
1861 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 ||
1862 dalvikOpcode == OP_DIV_INT_LIT16);
1863 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1864 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1865 if (div) {
1866 int tReg = oatAllocTemp(cUnit);
1867 if (lit == 2) {
1868 // Division by 2 is by far the most common division by constant.
1869 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1870 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1871 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1872 } else {
1873 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1874 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1875 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1876 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1877 }
1878 } else {
1879 int cReg = oatAllocTemp(cUnit);
1880 loadConstant(cUnit, cReg, lit - 1);
1881 int tReg1 = oatAllocTemp(cUnit);
1882 int tReg2 = oatAllocTemp(cUnit);
1883 if (lit == 2) {
1884 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1885 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1886 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1887 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1888 } else {
1889 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1890 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1891 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1892 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1893 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1894 }
1895 }
1896 storeValue(cUnit, rlDest, rlResult);
1897 return true;
1898}
1899
1900// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1901// and store the result in 'rlDest'.
buzbeeed3e9302011-09-23 17:34:19 -07001902STATIC bool handleEasyMultiply(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001903 RegLocation rlSrc, RegLocation rlDest, int lit)
1904{
1905 // Can we simplify this multiplication?
1906 bool powerOfTwo = false;
1907 bool popCountLE2 = false;
1908 bool powerOfTwoMinusOne = false;
1909 if (lit < 2) {
1910 // Avoid special cases.
1911 return false;
1912 } else if (isPowerOfTwo(lit)) {
1913 powerOfTwo = true;
1914 } else if (isPopCountLE2(lit)) {
1915 popCountLE2 = true;
1916 } else if (isPowerOfTwo(lit + 1)) {
1917 powerOfTwoMinusOne = true;
1918 } else {
1919 return false;
1920 }
1921 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1922 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1923 if (powerOfTwo) {
1924 // Shift.
1925 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1926 lowestSetBit(lit));
1927 } else if (popCountLE2) {
1928 // Shift and add and shift.
1929 int firstBit = lowestSetBit(lit);
1930 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1931 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1932 firstBit, secondBit);
1933 } else {
1934 // Reverse subtract: (src << (shift + 1)) - src.
buzbeeed3e9302011-09-23 17:34:19 -07001935 DCHECK(powerOfTwoMinusOne);
buzbee5ade1d22011-09-09 14:44:52 -07001936 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
buzbee67bf8852011-08-17 17:51:35 -07001937 int tReg = oatAllocTemp(cUnit);
1938 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1939 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1940 }
1941 storeValue(cUnit, rlDest, rlResult);
1942 return true;
1943}
1944
buzbeeed3e9302011-09-23 17:34:19 -07001945STATIC bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001946 RegLocation rlDest, RegLocation rlSrc,
1947 int lit)
1948{
1949 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1950 RegLocation rlResult;
1951 OpKind op = (OpKind)0; /* Make gcc happy */
1952 int shiftOp = false;
1953 bool isDiv = false;
1954 int funcOffset;
1955
1956 switch (dalvikOpcode) {
1957 case OP_RSUB_INT_LIT8:
1958 case OP_RSUB_INT: {
1959 int tReg;
1960 //TUNING: add support for use of Arm rsub op
1961 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1962 tReg = oatAllocTemp(cUnit);
1963 loadConstant(cUnit, tReg, lit);
1964 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1965 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1966 tReg, rlSrc.lowReg);
1967 storeValue(cUnit, rlDest, rlResult);
1968 return false;
1969 break;
1970 }
1971
1972 case OP_ADD_INT_LIT8:
1973 case OP_ADD_INT_LIT16:
1974 op = kOpAdd;
1975 break;
1976 case OP_MUL_INT_LIT8:
1977 case OP_MUL_INT_LIT16: {
1978 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1979 return false;
1980 }
1981 op = kOpMul;
1982 break;
1983 }
1984 case OP_AND_INT_LIT8:
1985 case OP_AND_INT_LIT16:
1986 op = kOpAnd;
1987 break;
1988 case OP_OR_INT_LIT8:
1989 case OP_OR_INT_LIT16:
1990 op = kOpOr;
1991 break;
1992 case OP_XOR_INT_LIT8:
1993 case OP_XOR_INT_LIT16:
1994 op = kOpXor;
1995 break;
1996 case OP_SHL_INT_LIT8:
1997 lit &= 31;
1998 shiftOp = true;
1999 op = kOpLsl;
2000 break;
2001 case OP_SHR_INT_LIT8:
2002 lit &= 31;
2003 shiftOp = true;
2004 op = kOpAsr;
2005 break;
2006 case OP_USHR_INT_LIT8:
2007 lit &= 31;
2008 shiftOp = true;
2009 op = kOpLsr;
2010 break;
2011
2012 case OP_DIV_INT_LIT8:
2013 case OP_DIV_INT_LIT16:
2014 case OP_REM_INT_LIT8:
2015 case OP_REM_INT_LIT16:
2016 if (lit == 0) {
buzbee5ade1d22011-09-09 14:44:52 -07002017 genImmedCheck(cUnit, kArmCondAl, 0, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07002018 return false;
2019 }
2020 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
2021 return false;
2022 }
2023 oatFlushAllRegs(cUnit); /* Everything to home location */
2024 loadValueDirectFixed(cUnit, rlSrc, r0);
2025 oatClobber(cUnit, r0);
2026 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
2027 (dalvikOpcode == OP_DIV_INT_LIT16)) {
2028 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
2029 isDiv = true;
2030 } else {
2031 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
2032 isDiv = false;
2033 }
2034 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
2035 loadConstant(cUnit, r1, lit);
Ian Rogersff1ed472011-09-20 13:46:24 -07002036 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07002037 if (isDiv)
2038 rlResult = oatGetReturn(cUnit);
2039 else
2040 rlResult = oatGetReturnAlt(cUnit);
2041 storeValue(cUnit, rlDest, rlResult);
2042 return false;
2043 break;
2044 default:
2045 return true;
2046 }
2047 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2048 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2049 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2050 if (shiftOp && (lit == 0)) {
2051 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2052 } else {
2053 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2054 }
2055 storeValue(cUnit, rlDest, rlResult);
2056 return false;
2057}
2058
2059/* Architectural-specific debugging helpers go here */
2060void oatArchDump(void)
2061{
2062 /* Print compiled opcode in this VM instance */
2063 int i, start, streak;
2064 char buf[1024];
2065
2066 streak = i = 0;
2067 buf[0] = 0;
2068 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2069 i++;
2070 }
2071 if (i == kNumPackedOpcodes) {
2072 return;
2073 }
2074 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
2075 if (opcodeCoverage[i]) {
2076 streak++;
2077 } else {
2078 if (streak == 1) {
2079 sprintf(buf+strlen(buf), "%x,", start);
2080 } else {
2081 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
2082 }
2083 streak = 0;
2084 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2085 i++;
2086 }
2087 if (i < kNumPackedOpcodes) {
2088 streak = 1;
2089 start = i;
2090 }
2091 }
2092 }
2093 if (streak) {
2094 if (streak == 1) {
2095 sprintf(buf+strlen(buf), "%x", start);
2096 } else {
2097 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
2098 }
2099 }
2100 if (strlen(buf)) {
2101 LOG(INFO) << "dalvik.vm.oat.op = " << buf;
2102 }
2103}