blob: 8f83cbb78aaa030958a3d4e6853e895e054340d3 [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
buzbee34cd9e52011-09-08 14:31:52 -070025#define SLOW_FIELD_PATH 0
26#define SLOW_INVOKE_PATH 0
buzbee34cd9e52011-09-08 14:31:52 -070027//#define EXERCISE_SLOWEST_FIELD_PATH
28
29std::string fieldNameFromIndex(const Method* method, uint32_t fieldIdx)
30{
31 art::ClassLinker* class_linker = art::Runtime::Current()->GetClassLinker();
32 const art::DexFile& dex_file = class_linker->FindDexFile(
33 method->GetDeclaringClass()->GetDexCache());
34 const art::DexFile::FieldId& field_id = dex_file.GetFieldId(fieldIdx);
Elliott Hughes2bb97f92011-09-11 15:43:37 -070035 std::string class_name = dex_file.dexStringByTypeIdx(field_id.class_idx_);
buzbee34cd9e52011-09-08 14:31:52 -070036 std::string field_name = dex_file.dexStringById(field_id.name_idx_);
37 return class_name + "." + field_name;
38}
39
buzbee67bf8852011-08-17 17:51:35 -070040/*
41 * Construct an s4 from two consecutive half-words of switch data.
42 * This needs to check endianness because the DEX optimizer only swaps
43 * half-words in instruction stream.
44 *
45 * "switchData" must be 32-bit aligned.
46 */
47#if __BYTE_ORDER == __LITTLE_ENDIAN
48static inline s4 s4FromSwitchData(const void* switchData) {
49 return *(s4*) switchData;
50}
51#else
52static inline s4 s4FromSwitchData(const void* switchData) {
53 u2* data = switchData;
54 return data[0] | (((s4) data[1]) << 16);
55}
56#endif
57
Ian Rogersff1ed472011-09-20 13:46:24 -070058static ArmLIR* callRuntimeHelper(CompilationUnit* cUnit, int reg)
buzbeeec5adf32011-09-11 15:25:43 -070059{
60 return opReg(cUnit, kOpBlx, reg);
61}
62
buzbee1b4c8592011-08-31 10:43:51 -070063/* Generate unconditional branch instructions */
64static ArmLIR* genUnconditionalBranch(CompilationUnit* cUnit, ArmLIR* target)
65{
66 ArmLIR* branch = opNone(cUnit, kOpUncondBr);
67 branch->generic.target = (LIR*) target;
68 return branch;
69}
70
buzbee67bf8852011-08-17 17:51:35 -070071/*
72 * Generate a Thumb2 IT instruction, which can nullify up to
73 * four subsequent instructions based on a condition and its
74 * inverse. The condition applies to the first instruction, which
75 * is executed if the condition is met. The string "guide" consists
76 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
77 * A "T" means the instruction is executed if the condition is
78 * met, and an "E" means the instruction is executed if the condition
79 * is not met.
80 */
81static ArmLIR* genIT(CompilationUnit* cUnit, ArmConditionCode code,
82 const char* guide)
83{
84 int mask;
85 int condBit = code & 1;
86 int altBit = condBit ^ 1;
87 int mask3 = 0;
88 int mask2 = 0;
89 int mask1 = 0;
90
91 //Note: case fallthroughs intentional
92 switch(strlen(guide)) {
93 case 3:
94 mask1 = (guide[2] == 'T') ? condBit : altBit;
95 case 2:
96 mask2 = (guide[1] == 'T') ? condBit : altBit;
97 case 1:
98 mask3 = (guide[0] == 'T') ? condBit : altBit;
99 break;
100 case 0:
101 break;
102 default:
103 LOG(FATAL) << "OAT: bad case in genIT";
104 }
105 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
106 (1 << (3 - strlen(guide)));
107 return newLIR2(cUnit, kThumb2It, code, mask);
108}
109
110/*
111 * Insert a kArmPseudoCaseLabel at the beginning of the Dalvik
112 * offset vaddr. This label will be used to fix up the case
113 * branch table during the assembly phase. Be sure to set
114 * all resource flags on this to prevent code motion across
115 * target boundaries. KeyVal is just there for debugging.
116 */
117static ArmLIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
118{
119 ArmLIR* lir;
120 for (lir = (ArmLIR*)cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
121 if ((lir->opcode == kArmPseudoDalvikByteCodeBoundary) &&
122 (lir->generic.dalvikOffset == vaddr)) {
123 ArmLIR* newLabel = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
124 newLabel->generic.dalvikOffset = vaddr;
125 newLabel->opcode = kArmPseudoCaseLabel;
126 newLabel->operands[0] = keyVal;
127 oatInsertLIRAfter((LIR*)lir, (LIR*)newLabel);
128 return newLabel;
129 }
130 }
131 oatCodegenDump(cUnit);
132 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
133 return NULL; // Quiet gcc
134}
135
136static void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
137{
138 const u2* table = tabRec->table;
139 int baseVaddr = tabRec->vaddr;
140 int *targets = (int*)&table[4];
141 int entries = table[1];
142 int lowKey = s4FromSwitchData(&table[2]);
143 for (int i = 0; i < entries; i++) {
144 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
145 i + lowKey);
146 }
147}
148
149static void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
150{
151 const u2* table = tabRec->table;
152 int baseVaddr = tabRec->vaddr;
153 int entries = table[1];
154 int* keys = (int*)&table[2];
155 int* targets = &keys[entries];
156 for (int i = 0; i < entries; i++) {
157 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
158 keys[i]);
159 }
160}
161
162void oatProcessSwitchTables(CompilationUnit* cUnit)
163{
164 GrowableListIterator iterator;
165 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
166 while (true) {
167 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
168 &iterator);
169 if (tabRec == NULL) break;
170 if (tabRec->table[0] == kPackedSwitchSignature)
171 markPackedCaseLabels(cUnit, tabRec);
172 else if (tabRec->table[0] == kSparseSwitchSignature)
173 markSparseCaseLabels(cUnit, tabRec);
174 else {
175 LOG(FATAL) << "Invalid switch table";
176 }
177 }
178}
179
180static void dumpSparseSwitchTable(const u2* table)
181 /*
182 * Sparse switch data format:
183 * ushort ident = 0x0200 magic value
184 * ushort size number of entries in the table; > 0
185 * int keys[size] keys, sorted low-to-high; 32-bit aligned
186 * int targets[size] branch targets, relative to switch opcode
187 *
188 * Total size is (2+size*4) 16-bit code units.
189 */
190{
191 u2 ident = table[0];
192 int entries = table[1];
193 int* keys = (int*)&table[2];
194 int* targets = &keys[entries];
195 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
196 ", entries: " << std::dec << entries;
197 for (int i = 0; i < entries; i++) {
198 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
199 targets[i];
200 }
201}
202
203static void dumpPackedSwitchTable(const u2* table)
204 /*
205 * Packed switch data format:
206 * ushort ident = 0x0100 magic value
207 * ushort size number of entries in the table
208 * int first_key first (and lowest) switch case value
209 * int targets[size] branch targets, relative to switch opcode
210 *
211 * Total size is (4+size*2) 16-bit code units.
212 */
213{
214 u2 ident = table[0];
215 int* targets = (int*)&table[4];
216 int entries = table[1];
217 int lowKey = s4FromSwitchData(&table[2]);
218 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
219 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
220 for (int i = 0; i < entries; i++) {
221 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
222 targets[i];
223 }
224}
225
226/*
227 * The sparse table in the literal pool is an array of <key,displacement>
228 * pairs. For each set, we'll load them as a pair using ldmia.
229 * This means that the register number of the temp we use for the key
230 * must be lower than the reg for the displacement.
231 *
232 * The test loop will look something like:
233 *
234 * adr rBase, <table>
235 * ldr rVal, [rSP, vRegOff]
236 * mov rIdx, #tableSize
237 * lp:
238 * ldmia rBase!, {rKey, rDisp}
239 * sub rIdx, #1
240 * cmp rVal, rKey
241 * ifeq
242 * add rPC, rDisp ; This is the branch from which we compute displacement
243 * cbnz rIdx, lp
244 */
245static void genSparseSwitch(CompilationUnit* cUnit, MIR* mir,
246 RegLocation rlSrc)
247{
248 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
249 if (cUnit->printMe) {
250 dumpSparseSwitchTable(table);
251 }
252 // Add the table to the list - we'll process it later
253 SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable),
254 true);
255 tabRec->table = table;
256 tabRec->vaddr = mir->offset;
257 int size = table[1];
258 tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true);
259 oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec);
260
261 // Get the switch value
262 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
263 int rBase = oatAllocTemp(cUnit);
264 /* Allocate key and disp temps */
265 int rKey = oatAllocTemp(cUnit);
266 int rDisp = oatAllocTemp(cUnit);
267 // Make sure rKey's register number is less than rDisp's number for ldmia
268 if (rKey > rDisp) {
269 int tmp = rDisp;
270 rDisp = rKey;
271 rKey = tmp;
272 }
273 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700274 newLIR3(cUnit, kThumb2Adr, rBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700275 // Set up rIdx
276 int rIdx = oatAllocTemp(cUnit);
277 loadConstant(cUnit, rIdx, size);
278 // Establish loop branch target
279 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
280 target->defMask = ENCODE_ALL;
281 // Load next key/disp
282 newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp));
283 opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg);
284 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
285 genIT(cUnit, kArmCondEq, "");
286 ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp);
287 tabRec->bxInst = switchBranch;
288 // Needs to use setflags encoding here
289 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
290 ArmLIR* branch = opCondBranch(cUnit, kArmCondNe);
291 branch->generic.target = (LIR*)target;
292}
293
294
295static void genPackedSwitch(CompilationUnit* cUnit, MIR* mir,
296 RegLocation rlSrc)
297{
298 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
299 if (cUnit->printMe) {
300 dumpPackedSwitchTable(table);
301 }
302 // Add the table to the list - we'll process it later
303 SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable),
304 true);
305 tabRec->table = table;
306 tabRec->vaddr = mir->offset;
307 int size = table[1];
308 tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true);
309 oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec);
310
311 // Get the switch value
312 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
313 int tableBase = oatAllocTemp(cUnit);
314 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700315 newLIR3(cUnit, kThumb2Adr, tableBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700316 int lowKey = s4FromSwitchData(&table[2]);
317 int keyReg;
318 // Remove the bias, if necessary
319 if (lowKey == 0) {
320 keyReg = rlSrc.lowReg;
321 } else {
322 keyReg = oatAllocTemp(cUnit);
323 opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
324 }
325 // Bounds check - if < 0 or >= size continue following switch
326 opRegImm(cUnit, kOpCmp, keyReg, size-1);
327 ArmLIR* branchOver = opCondBranch(cUnit, kArmCondHi);
328
329 // Load the displacement from the switch table
330 int dispReg = oatAllocTemp(cUnit);
331 loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord);
332
333 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
334 ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg);
335 tabRec->bxInst = switchBranch;
336
337 /* branchOver target here */
338 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
339 target->defMask = ENCODE_ALL;
340 branchOver->generic.target = (LIR*)target;
341}
342
343/*
344 * Array data table format:
345 * ushort ident = 0x0300 magic value
346 * ushort width width of each element in the table
347 * uint size number of elements in the table
348 * ubyte data[size*width] table of data values (may contain a single-byte
349 * padding at the end)
350 *
351 * Total size is 4+(width * size + 1)/2 16-bit code units.
352 */
353static void genFillArrayData(CompilationUnit* cUnit, MIR* mir,
354 RegLocation rlSrc)
355{
356 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
357 // Add the table to the list - we'll process it later
358 FillArrayData *tabRec = (FillArrayData *)
359 oatNew(sizeof(FillArrayData), true);
360 tabRec->table = table;
361 tabRec->vaddr = mir->offset;
362 u2 width = tabRec->table[1];
363 u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16);
364 tabRec->size = (size * width) + 8;
365
366 oatInsertGrowableList(&cUnit->fillArrayData, (intptr_t)tabRec);
367
368 // Making a call - use explicit registers
369 oatFlushAllRegs(cUnit); /* Everything to home location */
370 loadValueDirectFixed(cUnit, rlSrc, r0);
371 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700372 OFFSETOF_MEMBER(Thread, pHandleFillArrayDataFromCode), rLR);
buzbeee6d61962011-08-27 11:58:19 -0700373 // Materialize a pointer to the fill data image
buzbee03fa2632011-09-20 17:10:57 -0700374 newLIR3(cUnit, kThumb2Adr, r1, 0, (intptr_t)tabRec);
Ian Rogersff1ed472011-09-20 13:46:24 -0700375 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700376 oatClobberCallRegs(cUnit);
377}
378
379/*
380 * Mark garbage collection card. Skip if the value we're storing is null.
381 */
382static void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg)
383{
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700384#ifdef CONCURRENT_GARBAGE_COLLECTOR
buzbee0d966cf2011-09-08 17:34:58 -0700385 // TODO: re-enable when concurrent collector is active
buzbee67bf8852011-08-17 17:51:35 -0700386 int regCardBase = oatAllocTemp(cUnit);
387 int regCardNo = oatAllocTemp(cUnit);
388 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondEq, valReg, 0);
buzbeec143c552011-08-20 17:38:58 -0700389 loadWordDisp(cUnit, rSELF, Thread::CardTableOffset().Int32Value(),
buzbee67bf8852011-08-17 17:51:35 -0700390 regCardBase);
391 opRegRegImm(cUnit, kOpLsr, regCardNo, tgtAddrReg, GC_CARD_SHIFT);
392 storeBaseIndexed(cUnit, regCardBase, regCardNo, regCardBase, 0,
393 kUnsignedByte);
394 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
395 target->defMask = ENCODE_ALL;
396 branchOver->generic.target = (LIR*)target;
397 oatFreeTemp(cUnit, regCardBase);
398 oatFreeTemp(cUnit, regCardNo);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700399#endif
buzbee67bf8852011-08-17 17:51:35 -0700400}
401
buzbee34cd9e52011-09-08 14:31:52 -0700402/*
403 * Helper function for Iget/put when field not resolved at compile time.
404 * Will trash call temps and return with the field offset in r0.
405 */
406static void getFieldOffset(CompilationUnit* cUnit, MIR* mir)
407{
408 int fieldIdx = mir->dalvikInsn.vC;
409 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
410 << " unresolved at compile time";
411 oatLockCallTemps(cUnit); // Explicit register usage
412 loadCurrMethodDirect(cUnit, r1); // arg1 <= Method*
413 loadWordDisp(cUnit, r1,
414 Method::DexCacheResolvedFieldsOffset().Int32Value(), r0);
415 loadWordDisp(cUnit, r0, art::Array::DataOffset().Int32Value() +
416 sizeof(int32_t*)* fieldIdx, r0);
417 /*
418 * For testing, omit the test for run-time resolution. This will
419 * force all accesses to go through the runtime resolution path.
420 */
421#ifndef EXERCISE_SLOWEST_FIELD_PATH
422 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
423#endif
424 // Resolve
425 loadWordDisp(cUnit, rSELF,
Brian Carlstrom845490b2011-09-19 15:56:53 -0700426 OFFSETOF_MEMBER(Thread, pFindInstanceFieldFromCode), rLR);
buzbee34cd9e52011-09-08 14:31:52 -0700427 loadConstant(cUnit, r0, fieldIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700428 callRuntimeHelper(cUnit, rLR); // resolveTypeFromCode(idx, method)
buzbee34cd9e52011-09-08 14:31:52 -0700429 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
430 target->defMask = ENCODE_ALL;
431#ifndef EXERCISE_SLOWEST_FIELD_PATH
432 branchOver->generic.target = (LIR*)target;
433#endif
434 // Free temps (except for r0)
435 oatFreeTemp(cUnit, r1);
436 oatFreeTemp(cUnit, r2);
437 oatFreeTemp(cUnit, r3);
438 loadWordDisp(cUnit, r0, art::Field::OffsetOffset().Int32Value(), r0);
439}
440
buzbee43a36422011-09-14 14:00:13 -0700441static void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -0700442 RegLocation rlDest, RegLocation rlObj)
443{
buzbeec143c552011-08-20 17:38:58 -0700444 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
445 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700446 RegLocation rlResult;
447 RegisterClass regClass = oatRegClassBySize(size);
buzbee34cd9e52011-09-08 14:31:52 -0700448 if (SLOW_FIELD_PATH || fieldPtr == NULL) {
449 getFieldOffset(cUnit, mir);
450 // Field offset in r0
451 rlObj = loadValue(cUnit, rlObj, kCoreReg);
452 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee5ade1d22011-09-09 14:44:52 -0700453 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee34cd9e52011-09-08 14:31:52 -0700454 loadBaseIndexed(cUnit, rlObj.lowReg, r0, rlResult.lowReg, 0, size);
buzbee67bf8852011-08-17 17:51:35 -0700455 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700456 storeValue(cUnit, rlDest, rlResult);
457 } else {
458#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700459 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700460#else
461 bool isVolatile = false;
462#endif
463 int fieldOffset = fieldPtr->GetOffset().Int32Value();
464 rlObj = loadValue(cUnit, rlObj, kCoreReg);
465 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee5ade1d22011-09-09 14:44:52 -0700466 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee34cd9e52011-09-08 14:31:52 -0700467 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
468 size, rlObj.sRegLow);
469 if (isVolatile) {
470 oatGenMemBarrier(cUnit, kSY);
471 }
472 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -0700473 }
buzbee67bf8852011-08-17 17:51:35 -0700474}
475
buzbee43a36422011-09-14 14:00:13 -0700476static void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -0700477 RegLocation rlSrc, RegLocation rlObj, bool isObject)
478{
buzbeec143c552011-08-20 17:38:58 -0700479 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
480 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700481 RegisterClass regClass = oatRegClassBySize(size);
buzbee34cd9e52011-09-08 14:31:52 -0700482 if (SLOW_FIELD_PATH || fieldPtr == NULL) {
483 getFieldOffset(cUnit, mir);
484 // Field offset in r0
485 rlObj = loadValue(cUnit, rlObj, kCoreReg);
486 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee5ade1d22011-09-09 14:44:52 -0700487 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee67bf8852011-08-17 17:51:35 -0700488 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700489 storeBaseIndexed(cUnit, rlObj.lowReg, r0, rlSrc.lowReg, 0, size);
490 } else {
491#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700492 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700493#else
494 bool isVolatile = false;
495#endif
496 int fieldOffset = fieldPtr->GetOffset().Int32Value();
497 rlObj = loadValue(cUnit, rlObj, kCoreReg);
498 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee5ade1d22011-09-09 14:44:52 -0700499 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700500
501 if (isVolatile) {
502 oatGenMemBarrier(cUnit, kSY);
503 }
504 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
buzbee67bf8852011-08-17 17:51:35 -0700505 }
buzbee67bf8852011-08-17 17:51:35 -0700506 if (isObject) {
507 /* NOTE: marking card based on object head */
508 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
509 }
510}
511
buzbee43a36422011-09-14 14:00:13 -0700512static void genIGetWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700513 RegLocation rlObj)
514{
buzbeec143c552011-08-20 17:38:58 -0700515 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
516 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700517 RegLocation rlResult;
buzbee34cd9e52011-09-08 14:31:52 -0700518 if (fieldPtr == NULL) {
519 getFieldOffset(cUnit, mir);
520 // Field offset in r0
521 rlObj = loadValue(cUnit, rlObj, kCoreReg);
522 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee5ade1d22011-09-09 14:44:52 -0700523 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700524 opRegReg(cUnit, kOpAdd, r0, rlObj.lowReg);
525 loadPair(cUnit, r0, rlResult.lowReg, rlResult.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700526 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700527 storeValue(cUnit, rlDest, rlResult);
528 } else {
529#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700530 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700531#else
532 bool isVolatile = false;
533#endif
534 int fieldOffset = fieldPtr->GetOffset().Int32Value();
535 rlObj = loadValue(cUnit, rlObj, kCoreReg);
536 int regPtr = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700537
buzbee34cd9e52011-09-08 14:31:52 -0700538 assert(rlDest.wide);
539
buzbee5ade1d22011-09-09 14:44:52 -0700540 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700541 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
542 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
543
544 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
545
546 if (isVolatile) {
547 oatGenMemBarrier(cUnit, kSY);
548 }
549
550 oatFreeTemp(cUnit, regPtr);
551 storeValueWide(cUnit, rlDest, rlResult);
552 }
buzbee67bf8852011-08-17 17:51:35 -0700553}
554
buzbee43a36422011-09-14 14:00:13 -0700555static void genIPutWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700556 RegLocation rlObj)
557{
buzbeec143c552011-08-20 17:38:58 -0700558 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
559 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700560 if (fieldPtr == NULL) {
buzbee34cd9e52011-09-08 14:31:52 -0700561 getFieldOffset(cUnit, mir);
562 // Field offset in r0
563 rlObj = loadValue(cUnit, rlObj, kCoreReg);
564 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee5ade1d22011-09-09 14:44:52 -0700565 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700566 opRegReg(cUnit, kOpAdd, r0, rlObj.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700567 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700568 storePair(cUnit, r0, rlSrc.lowReg, rlSrc.highReg);
569 } else {
570#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700571 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700572#else
573 bool isVolatile = false;
574#endif
575 int fieldOffset = fieldPtr->GetOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700576
buzbee34cd9e52011-09-08 14:31:52 -0700577 rlObj = loadValue(cUnit, rlObj, kCoreReg);
578 int regPtr;
579 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee5ade1d22011-09-09 14:44:52 -0700580 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700581 regPtr = oatAllocTemp(cUnit);
582 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
583
584 if (isVolatile) {
585 oatGenMemBarrier(cUnit, kSY);
586 }
587 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
588
589 oatFreeTemp(cUnit, regPtr);
590 }
buzbee67bf8852011-08-17 17:51:35 -0700591}
592
593static void genConstClass(CompilationUnit* cUnit, MIR* mir,
594 RegLocation rlDest, RegLocation rlSrc)
595{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700596 art::Class* classPtr = cUnit->method->GetDexCacheResolvedTypes()->
buzbee1b4c8592011-08-31 10:43:51 -0700597 Get(mir->dalvikInsn.vB);
598 int mReg = loadCurrMethod(cUnit);
599 int resReg = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700600 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee2a475e72011-09-07 17:19:17 -0700601 loadWordDisp(cUnit, mReg, Method::DexCacheResolvedTypesOffset().Int32Value(),
buzbee1b4c8592011-08-31 10:43:51 -0700602 resReg);
603 loadWordDisp(cUnit, resReg, Array::DataOffset().Int32Value() +
604 (sizeof(String*) * mir->dalvikInsn.vB), rlResult.lowReg);
605 if (classPtr != NULL) {
606 // Fast path, we're done - just store result
607 storeValue(cUnit, rlDest, rlResult);
608 } else {
609 // Slow path. Must test at runtime
610 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, rlResult.lowReg,
611 0);
612 // Resolved, store and hop over following code
613 storeValue(cUnit, rlDest, rlResult);
614 ArmLIR* branch2 = genUnconditionalBranch(cUnit,0);
615 // TUNING: move slow path to end & remove unconditional branch
616 ArmLIR* target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
617 target1->defMask = ENCODE_ALL;
618 // Call out to helper, which will return resolved type in r0
619 loadWordDisp(cUnit, rSELF,
620 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
621 genRegCopy(cUnit, r1, mReg);
622 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700623 callRuntimeHelper(cUnit, rLR);
buzbee1b4c8592011-08-31 10:43:51 -0700624 oatClobberCallRegs(cUnit);
625 RegLocation rlResult = oatGetReturn(cUnit);
626 storeValue(cUnit, rlDest, rlResult);
627 // Rejoin code paths
628 ArmLIR* target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
629 target2->defMask = ENCODE_ALL;
630 branch1->generic.target = (LIR*)target1;
631 branch2->generic.target = (LIR*)target2;
632 }
buzbee67bf8852011-08-17 17:51:35 -0700633}
634
635static void genConstString(CompilationUnit* cUnit, MIR* mir,
636 RegLocation rlDest, RegLocation rlSrc)
637{
buzbee1b4c8592011-08-31 10:43:51 -0700638 /* All strings should be available at compile time */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700639 const art::String* str = cUnit->method->GetDexCacheStrings()->
buzbee1b4c8592011-08-31 10:43:51 -0700640 Get(mir->dalvikInsn.vB);
641 DCHECK(str != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700642
buzbee1b4c8592011-08-31 10:43:51 -0700643 int mReg = loadCurrMethod(cUnit);
644 int resReg = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700645 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700646 loadWordDisp(cUnit, mReg, Method::DexCacheStringsOffset().Int32Value(),
buzbee1b4c8592011-08-31 10:43:51 -0700647 resReg);
648 loadWordDisp(cUnit, resReg, Array::DataOffset().Int32Value() +
649 (sizeof(String*) * mir->dalvikInsn.vB), rlResult.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700650 storeValue(cUnit, rlDest, rlResult);
651}
652
buzbeedfd3d702011-08-28 12:56:51 -0700653/*
654 * Let helper function take care of everything. Will
655 * call Class::NewInstanceFromCode(type_idx, method);
656 */
buzbee67bf8852011-08-17 17:51:35 -0700657static void genNewInstance(CompilationUnit* cUnit, MIR* mir,
658 RegLocation rlDest)
659{
buzbeedfd3d702011-08-28 12:56:51 -0700660 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee67bf8852011-08-17 17:51:35 -0700661 loadWordDisp(cUnit, rSELF,
Brian Carlstrom1f870082011-08-23 16:02:11 -0700662 OFFSETOF_MEMBER(Thread, pAllocObjectFromCode), rLR);
buzbeedfd3d702011-08-28 12:56:51 -0700663 loadCurrMethodDirect(cUnit, r1); // arg1 <= Method*
664 loadConstant(cUnit, r0, mir->dalvikInsn.vB); // arg0 <- type_id
Ian Rogersff1ed472011-09-20 13:46:24 -0700665 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700666 oatClobberCallRegs(cUnit);
667 RegLocation rlResult = oatGetReturn(cUnit);
668 storeValue(cUnit, rlDest, rlResult);
669}
670
671void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
672{
673 loadWordDisp(cUnit, rSELF,
Ian Rogers67375ac2011-09-14 00:55:44 -0700674 OFFSETOF_MEMBER(Thread, pDeliverException), rLR);
Ian Rogersbdb03912011-09-14 00:55:44 -0700675 loadValueDirectFixed(cUnit, rlSrc, r0); // Get exception object
Ian Rogersff1ed472011-09-20 13:46:24 -0700676 callRuntimeHelper(cUnit, rLR); // art_deliver_exception(exception);
buzbee67bf8852011-08-17 17:51:35 -0700677}
678
679static void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
680 RegLocation rlSrc)
681{
buzbee2a475e72011-09-07 17:19:17 -0700682 // May generate a call - use explicit registers
683 oatLockCallTemps(cUnit);
684 art::Class* classPtr = cUnit->method->GetDexCacheResolvedTypes()->
685 Get(mir->dalvikInsn.vC);
686 int classReg = r2; // Fixed usage
687 loadCurrMethodDirect(cUnit, r1); // r1 <= current Method*
688 loadWordDisp(cUnit, r1, Method::DexCacheResolvedTypesOffset().Int32Value(),
689 classReg);
690 loadWordDisp(cUnit, classReg, Array::DataOffset().Int32Value() +
691 (sizeof(String*) * mir->dalvikInsn.vC), classReg);
buzbee67bf8852011-08-17 17:51:35 -0700692 if (classPtr == NULL) {
buzbee2a475e72011-09-07 17:19:17 -0700693 // Generate a runtime test
694 ArmLIR* hopBranch = genCmpImmBranch(cUnit, kArmCondNe, classReg, 0);
695 // Not resolved
696 // Call out to helper, which will return resolved type in r0
697 loadWordDisp(cUnit, rSELF,
698 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
699 loadConstant(cUnit, r0, mir->dalvikInsn.vC);
Ian Rogersff1ed472011-09-20 13:46:24 -0700700 callRuntimeHelper(cUnit, rLR); // resolveTypeFromCode(idx, method)
buzbee2a475e72011-09-07 17:19:17 -0700701 genRegCopy(cUnit, r2, r0); // Align usage with fast path
702 // Rejoin code paths
703 ArmLIR* hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
704 hopTarget->defMask = ENCODE_ALL;
705 hopBranch->generic.target = (LIR*)hopTarget;
buzbee67bf8852011-08-17 17:51:35 -0700706 }
buzbee2a475e72011-09-07 17:19:17 -0700707 // At this point, r2 has class
708 loadValueDirectFixed(cUnit, rlSrc, r3); /* Ref */
buzbee67bf8852011-08-17 17:51:35 -0700709 /* When taken r0 has NULL which can be used for store directly */
buzbee2a475e72011-09-07 17:19:17 -0700710 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r3, 0);
711 /* load object->clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700712 assert(Object::ClassOffset().Int32Value() == 0);
buzbee2a475e72011-09-07 17:19:17 -0700713 loadWordDisp(cUnit, r3, Object::ClassOffset().Int32Value(), r1);
buzbee67bf8852011-08-17 17:51:35 -0700714 /* r1 now contains object->clazz */
715 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700716 OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700717 loadConstant(cUnit, r0, 1); /* Assume true */
718 opRegReg(cUnit, kOpCmp, r1, r2);
719 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondEq);
buzbee2a475e72011-09-07 17:19:17 -0700720 genRegCopy(cUnit, r0, r3);
buzbee67bf8852011-08-17 17:51:35 -0700721 genRegCopy(cUnit, r1, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -0700722 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700723 oatClobberCallRegs(cUnit);
724 /* branch target here */
725 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
726 target->defMask = ENCODE_ALL;
buzbee2a475e72011-09-07 17:19:17 -0700727 RegLocation rlResult = oatGetReturn(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700728 storeValue(cUnit, rlDest, rlResult);
729 branch1->generic.target = (LIR*)target;
730 branch2->generic.target = (LIR*)target;
731}
732
733static void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
734{
buzbee2a475e72011-09-07 17:19:17 -0700735 // May generate a call - use explicit registers
736 oatLockCallTemps(cUnit);
737 art::Class* classPtr = cUnit->method->GetDexCacheResolvedTypes()->
738 Get(mir->dalvikInsn.vB);
739 int classReg = r2; // Fixed usage
740 loadCurrMethodDirect(cUnit, r1); // r1 <= current Method*
741 loadWordDisp(cUnit, r1, Method::DexCacheResolvedTypesOffset().Int32Value(),
742 classReg);
743 loadWordDisp(cUnit, classReg, Array::DataOffset().Int32Value() +
744 (sizeof(String*) * mir->dalvikInsn.vB), classReg);
buzbee67bf8852011-08-17 17:51:35 -0700745 if (classPtr == NULL) {
buzbee2a475e72011-09-07 17:19:17 -0700746 // Generate a runtime test
747 ArmLIR* hopBranch = genCmpImmBranch(cUnit, kArmCondNe, classReg, 0);
748 // Not resolved
749 // Call out to helper, which will return resolved type in r0
750 loadWordDisp(cUnit, rSELF,
751 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
752 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700753 callRuntimeHelper(cUnit, rLR); // resolveTypeFromCode(idx, method)
buzbee2a475e72011-09-07 17:19:17 -0700754 genRegCopy(cUnit, r2, r0); // Align usage with fast path
755 // Rejoin code paths
756 ArmLIR* hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
757 hopTarget->defMask = ENCODE_ALL;
758 hopBranch->generic.target = (LIR*)hopTarget;
buzbee67bf8852011-08-17 17:51:35 -0700759 }
buzbee2a475e72011-09-07 17:19:17 -0700760 // At this point, r2 has class
761 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
762 /* Null is OK - continue */
763 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
764 /* load object->clazz */
765 assert(Object::ClassOffset().Int32Value() == 0);
766 loadWordDisp(cUnit, r0, Object::ClassOffset().Int32Value(), r1);
767 /* r1 now contains object->clazz */
buzbee67bf8852011-08-17 17:51:35 -0700768 loadWordDisp(cUnit, rSELF,
buzbee2a475e72011-09-07 17:19:17 -0700769 OFFSETOF_MEMBER(Thread, pCheckCastFromCode), rLR);
770 opRegReg(cUnit, kOpCmp, r1, r2);
771 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondEq); /* If equal, trivial yes */
772 genRegCopy(cUnit, r0, r1);
773 genRegCopy(cUnit, r1, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -0700774 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700775 oatClobberCallRegs(cUnit);
buzbee2a475e72011-09-07 17:19:17 -0700776 /* branch target here */
buzbee67bf8852011-08-17 17:51:35 -0700777 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
778 target->defMask = ENCODE_ALL;
779 branch1->generic.target = (LIR*)target;
780 branch2->generic.target = (LIR*)target;
781}
782
783static void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest,
784 RegLocation rlSrc)
785{
786 RegLocation rlResult;
787 rlSrc = loadValue(cUnit, rlSrc, kFPReg);
788 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
789 newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg);
790 storeValue(cUnit, rlDest, rlResult);
791}
792
793static void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest,
794 RegLocation rlSrc)
795{
796 RegLocation rlResult;
797 rlSrc = loadValueWide(cUnit, rlSrc, kFPReg);
798 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
799 newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg),
800 S2D(rlSrc.lowReg, rlSrc.highReg));
801 storeValueWide(cUnit, rlDest, rlResult);
802}
803
buzbee439c4fa2011-08-27 15:59:07 -0700804static void freeRegLocTemps(CompilationUnit* cUnit, RegLocation rlKeep,
805 RegLocation rlFree)
buzbee67bf8852011-08-17 17:51:35 -0700806{
buzbee439c4fa2011-08-27 15:59:07 -0700807 if ((rlFree.lowReg != rlKeep.lowReg) && (rlFree.lowReg != rlKeep.highReg))
808 oatFreeTemp(cUnit, rlFree.lowReg);
809 if ((rlFree.highReg != rlKeep.lowReg) && (rlFree.highReg != rlKeep.highReg))
810 oatFreeTemp(cUnit, rlFree.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700811}
812
813static void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
814 OpKind secondOp, RegLocation rlDest,
815 RegLocation rlSrc1, RegLocation rlSrc2)
816{
buzbee9e0f9b02011-08-24 15:32:46 -0700817 /*
818 * NOTE: This is the one place in the code in which we might have
819 * as many as six live temporary registers. There are 5 in the normal
820 * set for Arm. Until we have spill capabilities, temporarily add
821 * lr to the temp set. It is safe to do this locally, but note that
822 * lr is used explicitly elsewhere in the code generator and cannot
823 * normally be used as a general temp register.
824 */
buzbee67bf8852011-08-17 17:51:35 -0700825 RegLocation rlResult;
buzbee9e0f9b02011-08-24 15:32:46 -0700826 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
827 oatFreeTemp(cUnit, rLR); // and make it available
buzbee67bf8852011-08-17 17:51:35 -0700828 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
829 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
830 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
831 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
832 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
833 rlSrc2.highReg);
buzbee439c4fa2011-08-27 15:59:07 -0700834 /*
835 * NOTE: If rlDest refers to a frame variable in a large frame, the
836 * following storeValueWide might need to allocate a temp register.
837 * To further work around the lack of a spill capability, explicitly
838 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
839 * Remove when spill is functional.
840 */
841 freeRegLocTemps(cUnit, rlResult, rlSrc1);
842 freeRegLocTemps(cUnit, rlResult, rlSrc2);
buzbee67bf8852011-08-17 17:51:35 -0700843 storeValueWide(cUnit, rlDest, rlResult);
buzbee9e0f9b02011-08-24 15:32:46 -0700844 oatClobber(cUnit, rLR);
845 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee67bf8852011-08-17 17:51:35 -0700846}
847
848void oatInitializeRegAlloc(CompilationUnit* cUnit)
849{
850 int numRegs = sizeof(coreRegs)/sizeof(*coreRegs);
851 int numReserved = sizeof(reservedRegs)/sizeof(*reservedRegs);
852 int numTemps = sizeof(coreTemps)/sizeof(*coreTemps);
853 int numFPRegs = sizeof(fpRegs)/sizeof(*fpRegs);
854 int numFPTemps = sizeof(fpTemps)/sizeof(*fpTemps);
855 RegisterPool *pool = (RegisterPool *)oatNew(sizeof(*pool), true);
856 cUnit->regPool = pool;
857 pool->numCoreRegs = numRegs;
858 pool->coreRegs = (RegisterInfo *)
859 oatNew(numRegs * sizeof(*cUnit->regPool->coreRegs), true);
860 pool->numFPRegs = numFPRegs;
861 pool->FPRegs = (RegisterInfo *)
862 oatNew(numFPRegs * sizeof(*cUnit->regPool->FPRegs), true);
863 oatInitPool(pool->coreRegs, coreRegs, pool->numCoreRegs);
864 oatInitPool(pool->FPRegs, fpRegs, pool->numFPRegs);
865 // Keep special registers from being allocated
866 for (int i = 0; i < numReserved; i++) {
867 oatMarkInUse(cUnit, reservedRegs[i]);
868 }
869 // Mark temp regs - all others not in use can be used for promotion
870 for (int i = 0; i < numTemps; i++) {
871 oatMarkTemp(cUnit, coreTemps[i]);
872 }
873 for (int i = 0; i < numFPTemps; i++) {
874 oatMarkTemp(cUnit, fpTemps[i]);
875 }
buzbee67bf8852011-08-17 17:51:35 -0700876}
877
878/*
879 * Handle simple case (thin lock) inline. If it's complicated, bail
880 * out to the heavyweight lock/unlock routines. We'll use dedicated
881 * registers here in order to be in the right position in case we
882 * to bail to dvm[Lock/Unlock]Object(self, object)
883 *
884 * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object
885 * r1 -> object [arg1 for dvm[Lock/Unlock]Object
886 * r2 -> intial contents of object->lock, later result of strex
887 * r3 -> self->threadId
888 * r12 -> allow to be used by utilities as general temp
889 *
890 * The result of the strex is 0 if we acquire the lock.
891 *
892 * See comments in Sync.c for the layout of the lock word.
893 * Of particular interest to this code is the test for the
894 * simple case - which we handle inline. For monitor enter, the
895 * simple case is thin lock, held by no-one. For monitor exit,
896 * the simple case is thin lock, held by the unlocking thread with
897 * a recurse count of 0.
898 *
899 * A minor complication is that there is a field in the lock word
900 * unrelated to locking: the hash state. This field must be ignored, but
901 * preserved.
902 *
903 */
904static void genMonitorEnter(CompilationUnit* cUnit, MIR* mir,
905 RegLocation rlSrc)
906{
907 ArmLIR* target;
908 ArmLIR* hopTarget;
909 ArmLIR* branch;
910 ArmLIR* hopBranch;
911
912 oatFlushAllRegs(cUnit);
Elliott Hughes5f791332011-09-15 17:45:30 -0700913 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700914 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700915 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee5ade1d22011-09-09 14:44:52 -0700916 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700917 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -0700918 newLIR3(cUnit, kThumb2Ldrex, r2, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700919 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
buzbeec143c552011-08-20 17:38:58 -0700920 // Align owner
Elliott Hughes5f791332011-09-15 17:45:30 -0700921 opRegImm(cUnit, kOpLsl, r3, LW_LOCK_OWNER_SHIFT);
buzbee67bf8852011-08-17 17:51:35 -0700922 // Is lock unheld on lock or held by us (==threadId) on unlock?
Elliott Hughes5f791332011-09-15 17:45:30 -0700923 newLIR4(cUnit, kThumb2Bfi, r3, r2, 0, LW_LOCK_OWNER_SHIFT - 1);
924 newLIR3(cUnit, kThumb2Bfc, r2, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee67bf8852011-08-17 17:51:35 -0700925 hopBranch = newLIR2(cUnit, kThumb2Cbnz, r2, 0);
buzbeec143c552011-08-20 17:38:58 -0700926 newLIR4(cUnit, kThumb2Strex, r2, r3, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700927 Object::MonitorOffset().Int32Value() >> 2);
buzbee67bf8852011-08-17 17:51:35 -0700928 oatGenMemBarrier(cUnit, kSY);
929 branch = newLIR2(cUnit, kThumb2Cbz, r2, 0);
930
931 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
932 hopTarget->defMask = ENCODE_ALL;
933 hopBranch->generic.target = (LIR*)hopTarget;
934
buzbee1b4c8592011-08-31 10:43:51 -0700935 // Go expensive route - artLockObjectFromCode(self, obj);
936 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700937 rLR);
938 genRegCopy(cUnit, r0, rSELF);
Ian Rogersff1ed472011-09-20 13:46:24 -0700939 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700940
941 // Resume here
942 target = newLIR0(cUnit, kArmPseudoTargetLabel);
943 target->defMask = ENCODE_ALL;
944 branch->generic.target = (LIR*)target;
945}
946
947/*
948 * For monitor unlock, we don't have to use ldrex/strex. Once
949 * we've determined that the lock is thin and that we own it with
950 * a zero recursion count, it's safe to punch it back to the
951 * initial, unlock thin state with a store word.
952 */
953static void genMonitorExit(CompilationUnit* cUnit, MIR* mir,
954 RegLocation rlSrc)
955{
956 ArmLIR* target;
957 ArmLIR* branch;
958 ArmLIR* hopTarget;
959 ArmLIR* hopBranch;
960
Elliott Hughes5f791332011-09-15 17:45:30 -0700961 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700962 oatFlushAllRegs(cUnit);
963 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700964 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee5ade1d22011-09-09 14:44:52 -0700965 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700966 loadWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r2); // Get lock
Elliott Hughes54e7df12011-09-16 11:47:04 -0700967 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -0700968 // Is lock unheld on lock or held by us (==threadId) on unlock?
Elliott Hughes5f791332011-09-15 17:45:30 -0700969 opRegRegImm(cUnit, kOpAnd, r12, r2, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
buzbeec143c552011-08-20 17:38:58 -0700970 // Align owner
Elliott Hughes5f791332011-09-15 17:45:30 -0700971 opRegImm(cUnit, kOpLsl, r3, LW_LOCK_OWNER_SHIFT);
972 newLIR3(cUnit, kThumb2Bfc, r2, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee67bf8852011-08-17 17:51:35 -0700973 opRegReg(cUnit, kOpSub, r2, r3);
974 hopBranch = opCondBranch(cUnit, kArmCondNe);
975 oatGenMemBarrier(cUnit, kSY);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700976 storeWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r12);
buzbee67bf8852011-08-17 17:51:35 -0700977 branch = opNone(cUnit, kOpUncondBr);
978
979 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
980 hopTarget->defMask = ENCODE_ALL;
981 hopBranch->generic.target = (LIR*)hopTarget;
982
buzbee1b4c8592011-08-31 10:43:51 -0700983 // Go expensive route - UnlockObjectFromCode(self, obj);
984 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700985 rLR);
986 genRegCopy(cUnit, r0, rSELF);
Ian Rogersff1ed472011-09-20 13:46:24 -0700987 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700988
989 // Resume here
990 target = newLIR0(cUnit, kArmPseudoTargetLabel);
991 target->defMask = ENCODE_ALL;
992 branch->generic.target = (LIR*)target;
993}
994
995/*
996 * 64-bit 3way compare function.
997 * mov rX, #-1
998 * cmp op1hi, op2hi
999 * blt done
1000 * bgt flip
1001 * sub rX, op1lo, op2lo (treat as unsigned)
1002 * beq done
1003 * ite hi
1004 * mov(hi) rX, #-1
1005 * mov(!hi) rX, #1
1006 * flip:
1007 * neg rX
1008 * done:
1009 */
1010static void genCmpLong(CompilationUnit* cUnit, MIR* mir,
1011 RegLocation rlDest, RegLocation rlSrc1,
1012 RegLocation rlSrc2)
1013{
1014 RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change
1015 ArmLIR* target1;
1016 ArmLIR* target2;
1017 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1018 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1019 rlTemp.lowReg = oatAllocTemp(cUnit);
1020 loadConstant(cUnit, rlTemp.lowReg, -1);
1021 opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg);
1022 ArmLIR* branch1 = opCondBranch(cUnit, kArmCondLt);
1023 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondGt);
1024 opRegRegReg(cUnit, kOpSub, rlTemp.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1025 ArmLIR* branch3 = opCondBranch(cUnit, kArmCondEq);
1026
1027 genIT(cUnit, kArmCondHi, "E");
1028 newLIR2(cUnit, kThumb2MovImmShift, rlTemp.lowReg, modifiedImmediate(-1));
1029 loadConstant(cUnit, rlTemp.lowReg, 1);
1030 genBarrier(cUnit);
1031
1032 target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
1033 target2->defMask = -1;
1034 opRegReg(cUnit, kOpNeg, rlTemp.lowReg, rlTemp.lowReg);
1035
1036 target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
1037 target1->defMask = -1;
1038
1039 storeValue(cUnit, rlDest, rlTemp);
1040
1041 branch1->generic.target = (LIR*)target1;
1042 branch2->generic.target = (LIR*)target2;
1043 branch3->generic.target = branch1->generic.target;
1044}
1045
1046static void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit,
1047 RegLocation rlSrc, RegLocation rlResult, int lit,
1048 int firstBit, int secondBit)
1049{
1050 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1051 encodeShift(kArmLsl, secondBit - firstBit));
1052 if (firstBit != 0) {
1053 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1054 }
1055}
1056
1057static bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
1058 int srcSize, int tgtSize)
1059{
1060 /*
1061 * Don't optimize the register usage since it calls out to support
1062 * functions
1063 */
1064 RegLocation rlSrc;
1065 RegLocation rlDest;
1066 oatFlushAllRegs(cUnit); /* Send everything to home location */
1067 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1068 if (srcSize == 1) {
1069 rlSrc = oatGetSrc(cUnit, mir, 0);
1070 loadValueDirectFixed(cUnit, rlSrc, r0);
1071 } else {
1072 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
1073 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
1074 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001075 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001076 oatClobberCallRegs(cUnit);
1077 if (tgtSize == 1) {
1078 RegLocation rlResult;
1079 rlDest = oatGetDest(cUnit, mir, 0);
1080 rlResult = oatGetReturn(cUnit);
1081 storeValue(cUnit, rlDest, rlResult);
1082 } else {
1083 RegLocation rlResult;
1084 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1085 rlResult = oatGetReturnWide(cUnit);
1086 storeValueWide(cUnit, rlDest, rlResult);
1087 }
1088 return false;
1089}
1090
1091static bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
1092 RegLocation rlDest, RegLocation rlSrc1,
1093 RegLocation rlSrc2)
1094{
1095 RegLocation rlResult;
1096 int funcOffset;
1097
1098 switch (mir->dalvikInsn.opcode) {
1099 case OP_ADD_FLOAT_2ADDR:
1100 case OP_ADD_FLOAT:
1101 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
1102 break;
1103 case OP_SUB_FLOAT_2ADDR:
1104 case OP_SUB_FLOAT:
1105 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
1106 break;
1107 case OP_DIV_FLOAT_2ADDR:
1108 case OP_DIV_FLOAT:
1109 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
1110 break;
1111 case OP_MUL_FLOAT_2ADDR:
1112 case OP_MUL_FLOAT:
1113 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
1114 break;
1115 case OP_REM_FLOAT_2ADDR:
1116 case OP_REM_FLOAT:
1117 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
1118 break;
1119 case OP_NEG_FLOAT: {
1120 genNegFloat(cUnit, rlDest, rlSrc1);
1121 return false;
1122 }
1123 default:
1124 return true;
1125 }
1126 oatFlushAllRegs(cUnit); /* Send everything to home location */
1127 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1128 loadValueDirectFixed(cUnit, rlSrc1, r0);
1129 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001130 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001131 oatClobberCallRegs(cUnit);
1132 rlResult = oatGetReturn(cUnit);
1133 storeValue(cUnit, rlDest, rlResult);
1134 return false;
1135}
1136
1137static bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
1138 RegLocation rlDest, RegLocation rlSrc1,
1139 RegLocation rlSrc2)
1140{
1141 RegLocation rlResult;
1142 int funcOffset;
1143
1144 switch (mir->dalvikInsn.opcode) {
1145 case OP_ADD_DOUBLE_2ADDR:
1146 case OP_ADD_DOUBLE:
1147 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
1148 break;
1149 case OP_SUB_DOUBLE_2ADDR:
1150 case OP_SUB_DOUBLE:
1151 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
1152 break;
1153 case OP_DIV_DOUBLE_2ADDR:
1154 case OP_DIV_DOUBLE:
1155 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
1156 break;
1157 case OP_MUL_DOUBLE_2ADDR:
1158 case OP_MUL_DOUBLE:
1159 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
1160 break;
1161 case OP_REM_DOUBLE_2ADDR:
1162 case OP_REM_DOUBLE:
1163 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
1164 break;
1165 case OP_NEG_DOUBLE: {
1166 genNegDouble(cUnit, rlDest, rlSrc1);
1167 return false;
1168 }
1169 default:
1170 return true;
1171 }
1172 oatFlushAllRegs(cUnit); /* Send everything to home location */
1173 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1174 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1175 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -07001176 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001177 oatClobberCallRegs(cUnit);
1178 rlResult = oatGetReturnWide(cUnit);
1179 storeValueWide(cUnit, rlDest, rlResult);
1180 return false;
1181}
1182
1183static bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
1184{
1185 Opcode opcode = mir->dalvikInsn.opcode;
1186
1187 switch (opcode) {
1188 case OP_INT_TO_FLOAT:
1189 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
1190 1, 1);
1191 case OP_FLOAT_TO_INT:
1192 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
1193 1, 1);
1194 case OP_DOUBLE_TO_FLOAT:
1195 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
1196 2, 1);
1197 case OP_FLOAT_TO_DOUBLE:
1198 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
1199 1, 2);
1200 case OP_INT_TO_DOUBLE:
1201 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
1202 1, 2);
1203 case OP_DOUBLE_TO_INT:
1204 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
1205 2, 1);
1206 case OP_FLOAT_TO_LONG:
1207 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001208 pF2l), 1, 2);
buzbee67bf8852011-08-17 17:51:35 -07001209 case OP_LONG_TO_FLOAT:
1210 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
1211 2, 1);
1212 case OP_DOUBLE_TO_LONG:
1213 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001214 pD2l), 2, 2);
buzbee67bf8852011-08-17 17:51:35 -07001215 case OP_LONG_TO_DOUBLE:
1216 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
1217 2, 2);
1218 default:
1219 return true;
1220 }
1221 return false;
1222}
1223
1224/* Generate conditional branch instructions */
1225static ArmLIR* genConditionalBranch(CompilationUnit* cUnit,
1226 ArmConditionCode cond,
1227 ArmLIR* target)
1228{
1229 ArmLIR* branch = opCondBranch(cUnit, cond);
1230 branch->generic.target = (LIR*) target;
1231 return branch;
1232}
1233
buzbee67bf8852011-08-17 17:51:35 -07001234/*
1235 * Generate array store
1236 *
1237 */
buzbee1b4c8592011-08-31 10:43:51 -07001238static void genArrayObjPut(CompilationUnit* cUnit, MIR* mir,
1239 RegLocation rlArray, RegLocation rlIndex,
1240 RegLocation rlSrc, int scale)
buzbee67bf8852011-08-17 17:51:35 -07001241{
1242 RegisterClass regClass = oatRegClassBySize(kWord);
buzbeec143c552011-08-20 17:38:58 -07001243 int lenOffset = Array::LengthOffset().Int32Value();
1244 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001245
1246 /* Make sure it's a legal object Put. Use direct regs at first */
1247 loadValueDirectFixed(cUnit, rlArray, r1);
1248 loadValueDirectFixed(cUnit, rlSrc, r0);
1249
1250 /* null array object? */
buzbee43a36422011-09-14 14:00:13 -07001251 genNullCheck(cUnit, rlArray.sRegLow, r1, mir);
buzbee67bf8852011-08-17 17:51:35 -07001252 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -07001253 OFFSETOF_MEMBER(Thread, pCanPutArrayElementFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -07001254 /* Get the array's clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001255 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001256 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001257 oatClobberCallRegs(cUnit);
1258
1259 // Now, redo loadValues in case they didn't survive the call
1260
1261 int regPtr;
1262 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1263 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1264
1265 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1266 oatClobber(cUnit, rlArray.lowReg);
1267 regPtr = rlArray.lowReg;
1268 } else {
1269 regPtr = oatAllocTemp(cUnit);
1270 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1271 }
1272
buzbee43a36422011-09-14 14:00:13 -07001273 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001274 int regLen = oatAllocTemp(cUnit);
1275 //NOTE: max live temps(4) here.
1276 /* Get len */
1277 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1278 /* regPtr -> array data */
1279 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001280 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001281 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001282 oatFreeTemp(cUnit, regLen);
1283 } else {
1284 /* regPtr -> array data */
1285 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1286 }
1287 /* at this point, regPtr points to array, 2 live temps */
1288 rlSrc = loadValue(cUnit, rlSrc, regClass);
1289 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1290 scale, kWord);
1291}
1292
1293/*
1294 * Generate array load
1295 */
1296static void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
1297 RegLocation rlArray, RegLocation rlIndex,
1298 RegLocation rlDest, int scale)
1299{
1300 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001301 int lenOffset = Array::LengthOffset().Int32Value();
1302 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001303 RegLocation rlResult;
1304 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1305 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1306 int regPtr;
1307
1308 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001309 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001310
1311 regPtr = oatAllocTemp(cUnit);
1312
buzbee43a36422011-09-14 14:00:13 -07001313 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001314 int regLen = oatAllocTemp(cUnit);
1315 /* Get len */
1316 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1317 /* regPtr -> array data */
1318 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001319 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001320 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001321 oatFreeTemp(cUnit, regLen);
1322 } else {
1323 /* regPtr -> array data */
1324 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1325 }
buzbeee9a72f62011-09-04 17:59:07 -07001326 oatFreeTemp(cUnit, rlArray.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001327 if ((size == kLong) || (size == kDouble)) {
1328 if (scale) {
1329 int rNewIndex = oatAllocTemp(cUnit);
1330 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1331 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1332 oatFreeTemp(cUnit, rNewIndex);
1333 } else {
1334 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1335 }
buzbeee9a72f62011-09-04 17:59:07 -07001336 oatFreeTemp(cUnit, rlIndex.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001337 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1338
1339 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1340
1341 oatFreeTemp(cUnit, regPtr);
1342 storeValueWide(cUnit, rlDest, rlResult);
1343 } else {
1344 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1345
1346 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1347 scale, size);
1348
1349 oatFreeTemp(cUnit, regPtr);
1350 storeValue(cUnit, rlDest, rlResult);
1351 }
1352}
1353
1354/*
1355 * Generate array store
1356 *
1357 */
1358static void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
1359 RegLocation rlArray, RegLocation rlIndex,
1360 RegLocation rlSrc, int scale)
1361{
1362 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001363 int lenOffset = Array::LengthOffset().Int32Value();
1364 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001365
1366 int regPtr;
1367 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1368 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1369
1370 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1371 oatClobber(cUnit, rlArray.lowReg);
1372 regPtr = rlArray.lowReg;
1373 } else {
1374 regPtr = oatAllocTemp(cUnit);
1375 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1376 }
1377
1378 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001379 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001380
buzbee43a36422011-09-14 14:00:13 -07001381 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001382 int regLen = oatAllocTemp(cUnit);
1383 //NOTE: max live temps(4) here.
1384 /* Get len */
1385 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1386 /* regPtr -> array data */
1387 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001388 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001389 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001390 oatFreeTemp(cUnit, regLen);
1391 } else {
1392 /* regPtr -> array data */
1393 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1394 }
1395 /* at this point, regPtr points to array, 2 live temps */
1396 if ((size == kLong) || (size == kDouble)) {
buzbee5ade1d22011-09-09 14:44:52 -07001397 //TUNING: specific wide routine that can handle fp regs
buzbee67bf8852011-08-17 17:51:35 -07001398 if (scale) {
1399 int rNewIndex = oatAllocTemp(cUnit);
1400 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1401 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1402 oatFreeTemp(cUnit, rNewIndex);
1403 } else {
1404 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1405 }
1406 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1407
1408 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1409
1410 oatFreeTemp(cUnit, regPtr);
1411 } else {
1412 rlSrc = loadValue(cUnit, rlSrc, regClass);
1413
1414 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1415 scale, size);
1416 }
1417}
1418
1419static bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir,
1420 RegLocation rlDest, RegLocation rlSrc1,
1421 RegLocation rlShift)
1422{
buzbee54330722011-08-23 16:46:55 -07001423 int funcOffset;
buzbee67bf8852011-08-17 17:51:35 -07001424
buzbee67bf8852011-08-17 17:51:35 -07001425 switch( mir->dalvikInsn.opcode) {
1426 case OP_SHL_LONG:
1427 case OP_SHL_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001428 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
buzbee67bf8852011-08-17 17:51:35 -07001429 break;
1430 case OP_SHR_LONG:
1431 case OP_SHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001432 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
buzbee67bf8852011-08-17 17:51:35 -07001433 break;
1434 case OP_USHR_LONG:
1435 case OP_USHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001436 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
buzbee67bf8852011-08-17 17:51:35 -07001437 break;
1438 default:
buzbee54330722011-08-23 16:46:55 -07001439 LOG(FATAL) << "Unexpected case";
buzbee67bf8852011-08-17 17:51:35 -07001440 return true;
1441 }
buzbee54330722011-08-23 16:46:55 -07001442 oatFlushAllRegs(cUnit); /* Send everything to home location */
1443 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1444 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1445 loadValueDirect(cUnit, rlShift, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -07001446 callRuntimeHelper(cUnit, rLR);
buzbee54330722011-08-23 16:46:55 -07001447 oatClobberCallRegs(cUnit);
1448 RegLocation rlResult = oatGetReturnWide(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001449 storeValueWide(cUnit, rlDest, rlResult);
1450 return false;
1451}
1452
1453static bool genArithOpLong(CompilationUnit* cUnit, MIR* mir,
1454 RegLocation rlDest, RegLocation rlSrc1,
1455 RegLocation rlSrc2)
1456{
1457 RegLocation rlResult;
1458 OpKind firstOp = kOpBkpt;
1459 OpKind secondOp = kOpBkpt;
1460 bool callOut = false;
1461 int funcOffset;
1462 int retReg = r0;
1463
1464 switch (mir->dalvikInsn.opcode) {
1465 case OP_NOT_LONG:
1466 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1467 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1468 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1469 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1470 storeValueWide(cUnit, rlDest, rlResult);
1471 return false;
1472 break;
1473 case OP_ADD_LONG:
1474 case OP_ADD_LONG_2ADDR:
1475 firstOp = kOpAdd;
1476 secondOp = kOpAdc;
1477 break;
1478 case OP_SUB_LONG:
1479 case OP_SUB_LONG_2ADDR:
1480 firstOp = kOpSub;
1481 secondOp = kOpSbc;
1482 break;
1483 case OP_MUL_LONG:
1484 case OP_MUL_LONG_2ADDR:
buzbee439c4fa2011-08-27 15:59:07 -07001485 callOut = true;
1486 retReg = r0;
1487 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1488 break;
buzbee67bf8852011-08-17 17:51:35 -07001489 case OP_DIV_LONG:
1490 case OP_DIV_LONG_2ADDR:
1491 callOut = true;
1492 retReg = r0;
1493 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1494 break;
1495 /* NOTE - result is in r2/r3 instead of r0/r1 */
1496 case OP_REM_LONG:
1497 case OP_REM_LONG_2ADDR:
1498 callOut = true;
1499 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1500 retReg = r2;
1501 break;
1502 case OP_AND_LONG_2ADDR:
1503 case OP_AND_LONG:
1504 firstOp = kOpAnd;
1505 secondOp = kOpAnd;
1506 break;
1507 case OP_OR_LONG:
1508 case OP_OR_LONG_2ADDR:
1509 firstOp = kOpOr;
1510 secondOp = kOpOr;
1511 break;
1512 case OP_XOR_LONG:
1513 case OP_XOR_LONG_2ADDR:
1514 firstOp = kOpXor;
1515 secondOp = kOpXor;
1516 break;
1517 case OP_NEG_LONG: {
1518 //TUNING: can improve this using Thumb2 code
1519 int tReg = oatAllocTemp(cUnit);
1520 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1521 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1522 loadConstantNoClobber(cUnit, tReg, 0);
1523 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1524 tReg, rlSrc2.lowReg);
1525 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
1526 genRegCopy(cUnit, rlResult.highReg, tReg);
1527 storeValueWide(cUnit, rlDest, rlResult);
1528 return false;
1529 }
1530 default:
1531 LOG(FATAL) << "Invalid long arith op";
1532 }
1533 if (!callOut) {
1534 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1535 } else {
1536 // Adjust return regs in to handle case of rem returning r2/r3
1537 oatFlushAllRegs(cUnit); /* Send everything to home location */
1538 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1539 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1540 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -07001541 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001542 oatClobberCallRegs(cUnit);
1543 if (retReg == r0)
1544 rlResult = oatGetReturnWide(cUnit);
1545 else
1546 rlResult = oatGetReturnWideAlt(cUnit);
1547 storeValueWide(cUnit, rlDest, rlResult);
1548 }
1549 return false;
1550}
1551
1552static bool genArithOpInt(CompilationUnit* cUnit, MIR* mir,
1553 RegLocation rlDest, RegLocation rlSrc1,
1554 RegLocation rlSrc2)
1555{
1556 OpKind op = kOpBkpt;
1557 bool callOut = false;
1558 bool checkZero = false;
1559 bool unary = false;
1560 int retReg = r0;
1561 int funcOffset;
1562 RegLocation rlResult;
1563 bool shiftOp = false;
1564
1565 switch (mir->dalvikInsn.opcode) {
1566 case OP_NEG_INT:
1567 op = kOpNeg;
1568 unary = true;
1569 break;
1570 case OP_NOT_INT:
1571 op = kOpMvn;
1572 unary = true;
1573 break;
1574 case OP_ADD_INT:
1575 case OP_ADD_INT_2ADDR:
1576 op = kOpAdd;
1577 break;
1578 case OP_SUB_INT:
1579 case OP_SUB_INT_2ADDR:
1580 op = kOpSub;
1581 break;
1582 case OP_MUL_INT:
1583 case OP_MUL_INT_2ADDR:
1584 op = kOpMul;
1585 break;
1586 case OP_DIV_INT:
1587 case OP_DIV_INT_2ADDR:
1588 callOut = true;
1589 checkZero = true;
1590 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1591 retReg = r0;
1592 break;
1593 /* NOTE: returns in r1 */
1594 case OP_REM_INT:
1595 case OP_REM_INT_2ADDR:
1596 callOut = true;
1597 checkZero = true;
1598 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1599 retReg = r1;
1600 break;
1601 case OP_AND_INT:
1602 case OP_AND_INT_2ADDR:
1603 op = kOpAnd;
1604 break;
1605 case OP_OR_INT:
1606 case OP_OR_INT_2ADDR:
1607 op = kOpOr;
1608 break;
1609 case OP_XOR_INT:
1610 case OP_XOR_INT_2ADDR:
1611 op = kOpXor;
1612 break;
1613 case OP_SHL_INT:
1614 case OP_SHL_INT_2ADDR:
1615 shiftOp = true;
1616 op = kOpLsl;
1617 break;
1618 case OP_SHR_INT:
1619 case OP_SHR_INT_2ADDR:
1620 shiftOp = true;
1621 op = kOpAsr;
1622 break;
1623 case OP_USHR_INT:
1624 case OP_USHR_INT_2ADDR:
1625 shiftOp = true;
1626 op = kOpLsr;
1627 break;
1628 default:
1629 LOG(FATAL) << "Invalid word arith op: " <<
1630 (int)mir->dalvikInsn.opcode;
1631 }
1632 if (!callOut) {
1633 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1634 if (unary) {
1635 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1636 opRegReg(cUnit, op, rlResult.lowReg,
1637 rlSrc1.lowReg);
1638 } else {
1639 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1640 if (shiftOp) {
1641 int tReg = oatAllocTemp(cUnit);
1642 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1643 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1644 opRegRegReg(cUnit, op, rlResult.lowReg,
1645 rlSrc1.lowReg, tReg);
1646 oatFreeTemp(cUnit, tReg);
1647 } else {
1648 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1649 opRegRegReg(cUnit, op, rlResult.lowReg,
1650 rlSrc1.lowReg, rlSrc2.lowReg);
1651 }
1652 }
1653 storeValue(cUnit, rlDest, rlResult);
1654 } else {
1655 RegLocation rlResult;
1656 oatFlushAllRegs(cUnit); /* Send everything to home location */
1657 loadValueDirectFixed(cUnit, rlSrc2, r1);
1658 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1659 loadValueDirectFixed(cUnit, rlSrc1, r0);
1660 if (checkZero) {
buzbee5ade1d22011-09-09 14:44:52 -07001661 genImmedCheck(cUnit, kArmCondEq, r1, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07001662 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001663 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001664 oatClobberCallRegs(cUnit);
1665 if (retReg == r0)
1666 rlResult = oatGetReturn(cUnit);
1667 else
1668 rlResult = oatGetReturnAlt(cUnit);
1669 storeValue(cUnit, rlDest, rlResult);
1670 }
1671 return false;
1672}
1673
buzbeec1f45042011-09-21 16:03:19 -07001674/* Check if we need to check for pending suspend request */
1675static void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
1676{
1677 if (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK) {
1678 return;
1679 }
1680 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
1681 ArmLIR* branch = opCondBranch(cUnit, kArmCondEq);
1682 ArmLIR* retLab = newLIR0(cUnit, kArmPseudoTargetLabel);
1683 retLab->defMask = ENCODE_ALL;
1684 ArmLIR* target = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
1685 target->generic.dalvikOffset = cUnit->currentDalvikOffset;
1686 target->opcode = kArmPseudoSuspendTarget;
1687 target->operands[0] = (intptr_t)retLab;
1688 target->operands[1] = mir->offset;
1689 branch->generic.target = (LIR*)target;
1690 oatInsertGrowableList(&cUnit->suspendLaunchpads, (intptr_t)target);
1691}
1692
buzbee0d966cf2011-09-08 17:34:58 -07001693/* Check for pending suspend request. */
buzbee67bf8852011-08-17 17:51:35 -07001694static void genSuspendPoll(CompilationUnit* cUnit, MIR* mir)
1695{
buzbeec1f45042011-09-21 16:03:19 -07001696 if (mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK) {
1697 return;
1698 }
buzbee0d966cf2011-09-08 17:34:58 -07001699 oatLockCallTemps(cUnit); // Explicit register usage
1700 int rSuspendCount = r1;
buzbee67bf8852011-08-17 17:51:35 -07001701 ArmLIR* ld;
buzbee0d966cf2011-09-08 17:34:58 -07001702 ld = loadWordDisp(cUnit, rSELF,
1703 art::Thread::SuspendCountOffset().Int32Value(), rSuspendCount);
buzbee67bf8852011-08-17 17:51:35 -07001704 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
buzbee0d966cf2011-09-08 17:34:58 -07001705 loadWordDisp(cUnit, rSELF,
1706 OFFSETOF_MEMBER(Thread, pCheckSuspendFromCode), rLR);
1707 genRegCopy(cUnit, r0, rSELF);
1708 opRegImm(cUnit, kOpCmp, rSuspendCount, 0);
buzbeeb0ebba02011-09-17 10:52:59 -07001709 /*
1710 * FIXME: for efficiency we should use an if-converted suspend
1711 * test here. However, support for IT is a bit weak at the
1712 * moment, and requires knowledge of the exact number of instructions
1713 * to fall in the skip shadow. While the exception mechanism
1714 * remains in flux, use a compare and branch sequence. Once
1715 * things firm up, restore the conditional skip (and perhaps
1716 * fix the utility to handle variable-sized shadows).
1717 */
1718#if 0
buzbee0d966cf2011-09-08 17:34:58 -07001719 genIT(cUnit, kArmCondNe, "");
buzbeeec5adf32011-09-11 15:25:43 -07001720 callUnwindableHelper(cUnit, rLR); // CheckSuspendFromCode(self)
buzbeeb0ebba02011-09-17 10:52:59 -07001721#else
1722 ArmLIR* branch = opCondBranch(cUnit, kArmCondEq);
Ian Rogersff1ed472011-09-20 13:46:24 -07001723 callRuntimeHelper(cUnit, rLR); // CheckSuspendFromCode(self)
buzbeeb0ebba02011-09-17 10:52:59 -07001724 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
1725 target->defMask = ENCODE_ALL;
1726 branch->generic.target = (LIR*)target;
1727#endif
buzbee0d966cf2011-09-08 17:34:58 -07001728 oatFreeCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001729}
1730
1731/*
1732 * The following are the first-level codegen routines that analyze the format
1733 * of each bytecode then either dispatch special purpose codegen routines
1734 * or produce corresponding Thumb instructions directly.
1735 */
1736
1737static bool isPowerOfTwo(int x)
1738{
1739 return (x & (x - 1)) == 0;
1740}
1741
1742// Returns true if no more than two bits are set in 'x'.
1743static bool isPopCountLE2(unsigned int x)
1744{
1745 x &= x - 1;
1746 return (x & (x - 1)) == 0;
1747}
1748
1749// Returns the index of the lowest set bit in 'x'.
1750static int lowestSetBit(unsigned int x) {
1751 int bit_posn = 0;
1752 while ((x & 0xf) == 0) {
1753 bit_posn += 4;
1754 x >>= 4;
1755 }
1756 while ((x & 1) == 0) {
1757 bit_posn++;
1758 x >>= 1;
1759 }
1760 return bit_posn;
1761}
1762
1763// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1764// and store the result in 'rlDest'.
1765static bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode,
1766 RegLocation rlSrc, RegLocation rlDest, int lit)
1767{
1768 if (lit < 2 || !isPowerOfTwo(lit)) {
1769 return false;
1770 }
1771 int k = lowestSetBit(lit);
1772 if (k >= 30) {
1773 // Avoid special cases.
1774 return false;
1775 }
1776 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 ||
1777 dalvikOpcode == OP_DIV_INT_LIT16);
1778 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1779 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1780 if (div) {
1781 int tReg = oatAllocTemp(cUnit);
1782 if (lit == 2) {
1783 // Division by 2 is by far the most common division by constant.
1784 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1785 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1786 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1787 } else {
1788 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1789 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1790 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1791 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1792 }
1793 } else {
1794 int cReg = oatAllocTemp(cUnit);
1795 loadConstant(cUnit, cReg, lit - 1);
1796 int tReg1 = oatAllocTemp(cUnit);
1797 int tReg2 = oatAllocTemp(cUnit);
1798 if (lit == 2) {
1799 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1800 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1801 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1802 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1803 } else {
1804 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1805 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1806 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1807 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1808 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1809 }
1810 }
1811 storeValue(cUnit, rlDest, rlResult);
1812 return true;
1813}
1814
1815// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1816// and store the result in 'rlDest'.
1817static bool handleEasyMultiply(CompilationUnit* cUnit,
1818 RegLocation rlSrc, RegLocation rlDest, int lit)
1819{
1820 // Can we simplify this multiplication?
1821 bool powerOfTwo = false;
1822 bool popCountLE2 = false;
1823 bool powerOfTwoMinusOne = false;
1824 if (lit < 2) {
1825 // Avoid special cases.
1826 return false;
1827 } else if (isPowerOfTwo(lit)) {
1828 powerOfTwo = true;
1829 } else if (isPopCountLE2(lit)) {
1830 popCountLE2 = true;
1831 } else if (isPowerOfTwo(lit + 1)) {
1832 powerOfTwoMinusOne = true;
1833 } else {
1834 return false;
1835 }
1836 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1837 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1838 if (powerOfTwo) {
1839 // Shift.
1840 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1841 lowestSetBit(lit));
1842 } else if (popCountLE2) {
1843 // Shift and add and shift.
1844 int firstBit = lowestSetBit(lit);
1845 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1846 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1847 firstBit, secondBit);
1848 } else {
1849 // Reverse subtract: (src << (shift + 1)) - src.
1850 assert(powerOfTwoMinusOne);
buzbee5ade1d22011-09-09 14:44:52 -07001851 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
buzbee67bf8852011-08-17 17:51:35 -07001852 int tReg = oatAllocTemp(cUnit);
1853 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1854 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1855 }
1856 storeValue(cUnit, rlDest, rlResult);
1857 return true;
1858}
1859
1860static bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir,
1861 RegLocation rlDest, RegLocation rlSrc,
1862 int lit)
1863{
1864 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1865 RegLocation rlResult;
1866 OpKind op = (OpKind)0; /* Make gcc happy */
1867 int shiftOp = false;
1868 bool isDiv = false;
1869 int funcOffset;
1870
1871 switch (dalvikOpcode) {
1872 case OP_RSUB_INT_LIT8:
1873 case OP_RSUB_INT: {
1874 int tReg;
1875 //TUNING: add support for use of Arm rsub op
1876 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1877 tReg = oatAllocTemp(cUnit);
1878 loadConstant(cUnit, tReg, lit);
1879 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1880 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1881 tReg, rlSrc.lowReg);
1882 storeValue(cUnit, rlDest, rlResult);
1883 return false;
1884 break;
1885 }
1886
1887 case OP_ADD_INT_LIT8:
1888 case OP_ADD_INT_LIT16:
1889 op = kOpAdd;
1890 break;
1891 case OP_MUL_INT_LIT8:
1892 case OP_MUL_INT_LIT16: {
1893 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1894 return false;
1895 }
1896 op = kOpMul;
1897 break;
1898 }
1899 case OP_AND_INT_LIT8:
1900 case OP_AND_INT_LIT16:
1901 op = kOpAnd;
1902 break;
1903 case OP_OR_INT_LIT8:
1904 case OP_OR_INT_LIT16:
1905 op = kOpOr;
1906 break;
1907 case OP_XOR_INT_LIT8:
1908 case OP_XOR_INT_LIT16:
1909 op = kOpXor;
1910 break;
1911 case OP_SHL_INT_LIT8:
1912 lit &= 31;
1913 shiftOp = true;
1914 op = kOpLsl;
1915 break;
1916 case OP_SHR_INT_LIT8:
1917 lit &= 31;
1918 shiftOp = true;
1919 op = kOpAsr;
1920 break;
1921 case OP_USHR_INT_LIT8:
1922 lit &= 31;
1923 shiftOp = true;
1924 op = kOpLsr;
1925 break;
1926
1927 case OP_DIV_INT_LIT8:
1928 case OP_DIV_INT_LIT16:
1929 case OP_REM_INT_LIT8:
1930 case OP_REM_INT_LIT16:
1931 if (lit == 0) {
buzbee5ade1d22011-09-09 14:44:52 -07001932 genImmedCheck(cUnit, kArmCondAl, 0, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07001933 return false;
1934 }
1935 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
1936 return false;
1937 }
1938 oatFlushAllRegs(cUnit); /* Everything to home location */
1939 loadValueDirectFixed(cUnit, rlSrc, r0);
1940 oatClobber(cUnit, r0);
1941 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
1942 (dalvikOpcode == OP_DIV_INT_LIT16)) {
1943 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1944 isDiv = true;
1945 } else {
1946 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1947 isDiv = false;
1948 }
1949 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1950 loadConstant(cUnit, r1, lit);
Ian Rogersff1ed472011-09-20 13:46:24 -07001951 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001952 oatClobberCallRegs(cUnit);
1953 if (isDiv)
1954 rlResult = oatGetReturn(cUnit);
1955 else
1956 rlResult = oatGetReturnAlt(cUnit);
1957 storeValue(cUnit, rlDest, rlResult);
1958 return false;
1959 break;
1960 default:
1961 return true;
1962 }
1963 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1964 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1965 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
1966 if (shiftOp && (lit == 0)) {
1967 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
1968 } else {
1969 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
1970 }
1971 storeValue(cUnit, rlDest, rlResult);
1972 return false;
1973}
1974
1975/* Architectural-specific debugging helpers go here */
1976void oatArchDump(void)
1977{
1978 /* Print compiled opcode in this VM instance */
1979 int i, start, streak;
1980 char buf[1024];
1981
1982 streak = i = 0;
1983 buf[0] = 0;
1984 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
1985 i++;
1986 }
1987 if (i == kNumPackedOpcodes) {
1988 return;
1989 }
1990 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
1991 if (opcodeCoverage[i]) {
1992 streak++;
1993 } else {
1994 if (streak == 1) {
1995 sprintf(buf+strlen(buf), "%x,", start);
1996 } else {
1997 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
1998 }
1999 streak = 0;
2000 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2001 i++;
2002 }
2003 if (i < kNumPackedOpcodes) {
2004 streak = 1;
2005 start = i;
2006 }
2007 }
2008 }
2009 if (streak) {
2010 if (streak == 1) {
2011 sprintf(buf+strlen(buf), "%x", start);
2012 } else {
2013 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
2014 }
2015 }
2016 if (strlen(buf)) {
2017 LOG(INFO) << "dalvik.vm.oat.op = " << buf;
2018 }
2019}