blob: c247fe7c2b0930e042dc886e998b71b5042227e7 [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
buzbeeed3e9302011-09-23 17:34:19 -070048STATIC inline s4 s4FromSwitchData(const void* switchData) {
buzbee67bf8852011-08-17 17:51:35 -070049 return *(s4*) switchData;
50}
51#else
buzbeeed3e9302011-09-23 17:34:19 -070052STATIC inline s4 s4FromSwitchData(const void* switchData) {
buzbee67bf8852011-08-17 17:51:35 -070053 u2* data = switchData;
54 return data[0] | (((s4) data[1]) << 16);
55}
56#endif
57
buzbeeed3e9302011-09-23 17:34:19 -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 */
buzbeeed3e9302011-09-23 17:34:19 -070064STATIC ArmLIR* genUnconditionalBranch(CompilationUnit* cUnit, ArmLIR* target)
buzbee1b4c8592011-08-31 10:43:51 -070065{
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 */
buzbeeed3e9302011-09-23 17:34:19 -070081STATIC ArmLIR* genIT(CompilationUnit* cUnit, ArmConditionCode code,
buzbee67bf8852011-08-17 17:51:35 -070082 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 */
buzbeeed3e9302011-09-23 17:34:19 -0700117STATIC ArmLIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
buzbee67bf8852011-08-17 17:51:35 -0700118{
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
buzbeeed3e9302011-09-23 17:34:19 -0700136STATIC void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
buzbee67bf8852011-08-17 17:51:35 -0700137{
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
buzbeeed3e9302011-09-23 17:34:19 -0700149STATIC void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
buzbee67bf8852011-08-17 17:51:35 -0700150{
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
buzbeeed3e9302011-09-23 17:34:19 -0700180STATIC void dumpSparseSwitchTable(const u2* table)
buzbee67bf8852011-08-17 17:51:35 -0700181 /*
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
buzbeeed3e9302011-09-23 17:34:19 -0700203STATIC void dumpPackedSwitchTable(const u2* table)
buzbee67bf8852011-08-17 17:51:35 -0700204 /*
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 */
buzbeeed3e9302011-09-23 17:34:19 -0700245STATIC void genSparseSwitch(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700246 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
buzbeeed3e9302011-09-23 17:34:19 -0700295STATIC void genPackedSwitch(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700296 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 */
buzbeeed3e9302011-09-23 17:34:19 -0700353STATIC void genFillArrayData(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700354 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 */
buzbeeed3e9302011-09-23 17:34:19 -0700382STATIC void markGCCard(CompilationUnit* cUnit, int valReg, int tgtAddrReg)
buzbee67bf8852011-08-17 17:51:35 -0700383{
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 */
buzbeeed3e9302011-09-23 17:34:19 -0700406STATIC void getFieldOffset(CompilationUnit* cUnit, MIR* mir)
buzbee34cd9e52011-09-08 14:31:52 -0700407{
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
buzbeeed3e9302011-09-23 17:34:19 -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
buzbeeed3e9302011-09-23 17:34:19 -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
buzbeeed3e9302011-09-23 17:34:19 -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
buzbeeed3e9302011-09-23 17:34:19 -0700538 DCHECK(rlDest.wide);
buzbee34cd9e52011-09-08 14:31:52 -0700539
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
buzbeeed3e9302011-09-23 17:34:19 -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
buzbeeed3e9302011-09-23 17:34:19 -0700593STATIC void genConstClass(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700594 RegLocation rlDest, RegLocation rlSrc)
595{
Ian 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
buzbeeed3e9302011-09-23 17:34:19 -0700635STATIC void genConstString(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700636 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 */
buzbeeed3e9302011-09-23 17:34:19 -0700657STATIC void genNewInstance(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700658 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
buzbeeed3e9302011-09-23 17:34:19 -0700679STATIC void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700680 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 */
buzbeeed3e9302011-09-23 17:34:19 -0700712 DCHECK_EQ(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
buzbeeed3e9302011-09-23 17:34:19 -0700733STATIC void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700734{
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 */
buzbeeed3e9302011-09-23 17:34:19 -0700765 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbee2a475e72011-09-07 17:19:17 -0700766 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
buzbeeed3e9302011-09-23 17:34:19 -0700783STATIC void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700784 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
buzbeeed3e9302011-09-23 17:34:19 -0700793STATIC void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700794 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
buzbeeed3e9302011-09-23 17:34:19 -0700804STATIC void freeRegLocTemps(CompilationUnit* cUnit, RegLocation rlKeep,
buzbee439c4fa2011-08-27 15:59:07 -0700805 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
buzbeeed3e9302011-09-23 17:34:19 -0700813STATIC void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
buzbee67bf8852011-08-17 17:51:35 -0700814 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);
buzbeec0ecd652011-09-25 18:11:54 -0700831 // The longs may overlap - use intermediate temp if so
832 if (rlResult.lowReg == rlSrc1.highReg) {
833 //FIXME: review all long arithmetic ops - there may be more of these
834 int tReg = oatAllocTemp(cUnit);
835 genRegCopy(cUnit, tReg, rlSrc1.highReg);
836 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
837 rlSrc2.lowReg);
838 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
839 rlSrc2.highReg);
840 oatFreeTemp(cUnit, tReg);
841 } else {
842 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
843 rlSrc2.lowReg);
844 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
845 rlSrc2.highReg);
846 }
buzbee439c4fa2011-08-27 15:59:07 -0700847 /*
848 * NOTE: If rlDest refers to a frame variable in a large frame, the
849 * following storeValueWide might need to allocate a temp register.
850 * To further work around the lack of a spill capability, explicitly
851 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
852 * Remove when spill is functional.
853 */
854 freeRegLocTemps(cUnit, rlResult, rlSrc1);
855 freeRegLocTemps(cUnit, rlResult, rlSrc2);
buzbee67bf8852011-08-17 17:51:35 -0700856 storeValueWide(cUnit, rlDest, rlResult);
buzbee9e0f9b02011-08-24 15:32:46 -0700857 oatClobber(cUnit, rLR);
858 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee67bf8852011-08-17 17:51:35 -0700859}
860
861void oatInitializeRegAlloc(CompilationUnit* cUnit)
862{
863 int numRegs = sizeof(coreRegs)/sizeof(*coreRegs);
864 int numReserved = sizeof(reservedRegs)/sizeof(*reservedRegs);
865 int numTemps = sizeof(coreTemps)/sizeof(*coreTemps);
866 int numFPRegs = sizeof(fpRegs)/sizeof(*fpRegs);
867 int numFPTemps = sizeof(fpTemps)/sizeof(*fpTemps);
868 RegisterPool *pool = (RegisterPool *)oatNew(sizeof(*pool), true);
869 cUnit->regPool = pool;
870 pool->numCoreRegs = numRegs;
871 pool->coreRegs = (RegisterInfo *)
872 oatNew(numRegs * sizeof(*cUnit->regPool->coreRegs), true);
873 pool->numFPRegs = numFPRegs;
874 pool->FPRegs = (RegisterInfo *)
875 oatNew(numFPRegs * sizeof(*cUnit->regPool->FPRegs), true);
876 oatInitPool(pool->coreRegs, coreRegs, pool->numCoreRegs);
877 oatInitPool(pool->FPRegs, fpRegs, pool->numFPRegs);
878 // Keep special registers from being allocated
879 for (int i = 0; i < numReserved; i++) {
buzbeec0ecd652011-09-25 18:11:54 -0700880 if (NO_SUSPEND && (reservedRegs[i] == rSUSPEND)) {
881 //To measure cost of suspend check
882 continue;
883 }
buzbee67bf8852011-08-17 17:51:35 -0700884 oatMarkInUse(cUnit, reservedRegs[i]);
885 }
886 // Mark temp regs - all others not in use can be used for promotion
887 for (int i = 0; i < numTemps; i++) {
888 oatMarkTemp(cUnit, coreTemps[i]);
889 }
890 for (int i = 0; i < numFPTemps; i++) {
891 oatMarkTemp(cUnit, fpTemps[i]);
892 }
buzbeec0ecd652011-09-25 18:11:54 -0700893 // Construct the alias map.
894 cUnit->phiAliasMap = (int*)oatNew(cUnit->numSSARegs *
895 sizeof(cUnit->phiAliasMap[0]), false);
896 for (int i = 0; i < cUnit->numSSARegs; i++) {
897 cUnit->phiAliasMap[i] = i;
898 }
899 for (MIR* phi = cUnit->phiList; phi; phi = phi->meta.phiNext) {
900 int defReg = phi->ssaRep->defs[0];
901 for (int i = 0; i < phi->ssaRep->numUses; i++) {
902 for (int j = 0; j < cUnit->numSSARegs; j++) {
903 if (cUnit->phiAliasMap[j] == phi->ssaRep->uses[i]) {
904 cUnit->phiAliasMap[j] = defReg;
905 }
906 }
907 }
908 }
buzbee67bf8852011-08-17 17:51:35 -0700909}
910
911/*
912 * Handle simple case (thin lock) inline. If it's complicated, bail
913 * out to the heavyweight lock/unlock routines. We'll use dedicated
914 * registers here in order to be in the right position in case we
915 * to bail to dvm[Lock/Unlock]Object(self, object)
916 *
917 * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object
918 * r1 -> object [arg1 for dvm[Lock/Unlock]Object
919 * r2 -> intial contents of object->lock, later result of strex
920 * r3 -> self->threadId
921 * r12 -> allow to be used by utilities as general temp
922 *
923 * The result of the strex is 0 if we acquire the lock.
924 *
925 * See comments in Sync.c for the layout of the lock word.
926 * Of particular interest to this code is the test for the
927 * simple case - which we handle inline. For monitor enter, the
928 * simple case is thin lock, held by no-one. For monitor exit,
929 * the simple case is thin lock, held by the unlocking thread with
930 * a recurse count of 0.
931 *
932 * A minor complication is that there is a field in the lock word
933 * unrelated to locking: the hash state. This field must be ignored, but
934 * preserved.
935 *
936 */
buzbeeed3e9302011-09-23 17:34:19 -0700937STATIC void genMonitorEnter(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700938 RegLocation rlSrc)
939{
940 ArmLIR* target;
941 ArmLIR* hopTarget;
942 ArmLIR* branch;
943 ArmLIR* hopBranch;
944
945 oatFlushAllRegs(cUnit);
Elliott Hughes5f791332011-09-15 17:45:30 -0700946 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700947 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700948 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee5ade1d22011-09-09 14:44:52 -0700949 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700950 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -0700951 newLIR3(cUnit, kThumb2Ldrex, r2, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700952 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
buzbeec143c552011-08-20 17:38:58 -0700953 // Align owner
Elliott Hughes5f791332011-09-15 17:45:30 -0700954 opRegImm(cUnit, kOpLsl, r3, LW_LOCK_OWNER_SHIFT);
buzbee67bf8852011-08-17 17:51:35 -0700955 // Is lock unheld on lock or held by us (==threadId) on unlock?
Elliott Hughes5f791332011-09-15 17:45:30 -0700956 newLIR4(cUnit, kThumb2Bfi, r3, r2, 0, LW_LOCK_OWNER_SHIFT - 1);
957 newLIR3(cUnit, kThumb2Bfc, r2, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee67bf8852011-08-17 17:51:35 -0700958 hopBranch = newLIR2(cUnit, kThumb2Cbnz, r2, 0);
buzbeec143c552011-08-20 17:38:58 -0700959 newLIR4(cUnit, kThumb2Strex, r2, r3, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700960 Object::MonitorOffset().Int32Value() >> 2);
buzbee67bf8852011-08-17 17:51:35 -0700961 oatGenMemBarrier(cUnit, kSY);
962 branch = newLIR2(cUnit, kThumb2Cbz, r2, 0);
963
964 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
965 hopTarget->defMask = ENCODE_ALL;
966 hopBranch->generic.target = (LIR*)hopTarget;
967
buzbee1b4c8592011-08-31 10:43:51 -0700968 // Go expensive route - artLockObjectFromCode(self, obj);
969 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700970 rLR);
971 genRegCopy(cUnit, r0, rSELF);
Ian Rogersff1ed472011-09-20 13:46:24 -0700972 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700973
974 // Resume here
975 target = newLIR0(cUnit, kArmPseudoTargetLabel);
976 target->defMask = ENCODE_ALL;
977 branch->generic.target = (LIR*)target;
978}
979
980/*
981 * For monitor unlock, we don't have to use ldrex/strex. Once
982 * we've determined that the lock is thin and that we own it with
983 * a zero recursion count, it's safe to punch it back to the
984 * initial, unlock thin state with a store word.
985 */
buzbeeed3e9302011-09-23 17:34:19 -0700986STATIC void genMonitorExit(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700987 RegLocation rlSrc)
988{
989 ArmLIR* target;
990 ArmLIR* branch;
991 ArmLIR* hopTarget;
992 ArmLIR* hopBranch;
993
Elliott Hughes5f791332011-09-15 17:45:30 -0700994 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700995 oatFlushAllRegs(cUnit);
996 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700997 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee5ade1d22011-09-09 14:44:52 -0700998 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700999 loadWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r2); // Get lock
Elliott Hughes54e7df12011-09-16 11:47:04 -07001000 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -07001001 // Is lock unheld on lock or held by us (==threadId) on unlock?
Elliott Hughes5f791332011-09-15 17:45:30 -07001002 opRegRegImm(cUnit, kOpAnd, r12, r2, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
buzbeec143c552011-08-20 17:38:58 -07001003 // Align owner
Elliott Hughes5f791332011-09-15 17:45:30 -07001004 opRegImm(cUnit, kOpLsl, r3, LW_LOCK_OWNER_SHIFT);
1005 newLIR3(cUnit, kThumb2Bfc, r2, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee67bf8852011-08-17 17:51:35 -07001006 opRegReg(cUnit, kOpSub, r2, r3);
1007 hopBranch = opCondBranch(cUnit, kArmCondNe);
1008 oatGenMemBarrier(cUnit, kSY);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001009 storeWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r12);
buzbee67bf8852011-08-17 17:51:35 -07001010 branch = opNone(cUnit, kOpUncondBr);
1011
1012 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
1013 hopTarget->defMask = ENCODE_ALL;
1014 hopBranch->generic.target = (LIR*)hopTarget;
1015
buzbee1b4c8592011-08-31 10:43:51 -07001016 // Go expensive route - UnlockObjectFromCode(self, obj);
1017 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -07001018 rLR);
1019 genRegCopy(cUnit, r0, rSELF);
Ian Rogersff1ed472011-09-20 13:46:24 -07001020 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001021
1022 // Resume here
1023 target = newLIR0(cUnit, kArmPseudoTargetLabel);
1024 target->defMask = ENCODE_ALL;
1025 branch->generic.target = (LIR*)target;
1026}
1027
1028/*
1029 * 64-bit 3way compare function.
1030 * mov rX, #-1
1031 * cmp op1hi, op2hi
1032 * blt done
1033 * bgt flip
1034 * sub rX, op1lo, op2lo (treat as unsigned)
1035 * beq done
1036 * ite hi
1037 * mov(hi) rX, #-1
1038 * mov(!hi) rX, #1
1039 * flip:
1040 * neg rX
1041 * done:
1042 */
buzbeeed3e9302011-09-23 17:34:19 -07001043STATIC void genCmpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001044 RegLocation rlDest, RegLocation rlSrc1,
1045 RegLocation rlSrc2)
1046{
1047 RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change
1048 ArmLIR* target1;
1049 ArmLIR* target2;
1050 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1051 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1052 rlTemp.lowReg = oatAllocTemp(cUnit);
1053 loadConstant(cUnit, rlTemp.lowReg, -1);
1054 opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg);
1055 ArmLIR* branch1 = opCondBranch(cUnit, kArmCondLt);
1056 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondGt);
1057 opRegRegReg(cUnit, kOpSub, rlTemp.lowReg, rlSrc1.lowReg, rlSrc2.lowReg);
1058 ArmLIR* branch3 = opCondBranch(cUnit, kArmCondEq);
1059
1060 genIT(cUnit, kArmCondHi, "E");
1061 newLIR2(cUnit, kThumb2MovImmShift, rlTemp.lowReg, modifiedImmediate(-1));
1062 loadConstant(cUnit, rlTemp.lowReg, 1);
1063 genBarrier(cUnit);
1064
1065 target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
1066 target2->defMask = -1;
1067 opRegReg(cUnit, kOpNeg, rlTemp.lowReg, rlTemp.lowReg);
1068
1069 target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
1070 target1->defMask = -1;
1071
1072 storeValue(cUnit, rlDest, rlTemp);
1073
1074 branch1->generic.target = (LIR*)target1;
1075 branch2->generic.target = (LIR*)target2;
1076 branch3->generic.target = branch1->generic.target;
1077}
1078
buzbeeed3e9302011-09-23 17:34:19 -07001079STATIC void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001080 RegLocation rlSrc, RegLocation rlResult, int lit,
1081 int firstBit, int secondBit)
1082{
1083 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1084 encodeShift(kArmLsl, secondBit - firstBit));
1085 if (firstBit != 0) {
1086 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1087 }
1088}
1089
buzbeeed3e9302011-09-23 17:34:19 -07001090STATIC bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
buzbee67bf8852011-08-17 17:51:35 -07001091 int srcSize, int tgtSize)
1092{
1093 /*
1094 * Don't optimize the register usage since it calls out to support
1095 * functions
1096 */
1097 RegLocation rlSrc;
1098 RegLocation rlDest;
1099 oatFlushAllRegs(cUnit); /* Send everything to home location */
1100 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1101 if (srcSize == 1) {
1102 rlSrc = oatGetSrc(cUnit, mir, 0);
1103 loadValueDirectFixed(cUnit, rlSrc, r0);
1104 } else {
1105 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
1106 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
1107 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001108 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001109 oatClobberCallRegs(cUnit);
1110 if (tgtSize == 1) {
1111 RegLocation rlResult;
1112 rlDest = oatGetDest(cUnit, mir, 0);
1113 rlResult = oatGetReturn(cUnit);
1114 storeValue(cUnit, rlDest, rlResult);
1115 } else {
1116 RegLocation rlResult;
1117 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1118 rlResult = oatGetReturnWide(cUnit);
1119 storeValueWide(cUnit, rlDest, rlResult);
1120 }
1121 return false;
1122}
1123
buzbeeed3e9302011-09-23 17:34:19 -07001124STATIC bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001125 RegLocation rlDest, RegLocation rlSrc1,
1126 RegLocation rlSrc2)
1127{
1128 RegLocation rlResult;
1129 int funcOffset;
1130
1131 switch (mir->dalvikInsn.opcode) {
1132 case OP_ADD_FLOAT_2ADDR:
1133 case OP_ADD_FLOAT:
1134 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
1135 break;
1136 case OP_SUB_FLOAT_2ADDR:
1137 case OP_SUB_FLOAT:
1138 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
1139 break;
1140 case OP_DIV_FLOAT_2ADDR:
1141 case OP_DIV_FLOAT:
1142 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
1143 break;
1144 case OP_MUL_FLOAT_2ADDR:
1145 case OP_MUL_FLOAT:
1146 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
1147 break;
1148 case OP_REM_FLOAT_2ADDR:
1149 case OP_REM_FLOAT:
1150 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
1151 break;
1152 case OP_NEG_FLOAT: {
1153 genNegFloat(cUnit, rlDest, rlSrc1);
1154 return false;
1155 }
1156 default:
1157 return true;
1158 }
1159 oatFlushAllRegs(cUnit); /* Send everything to home location */
1160 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1161 loadValueDirectFixed(cUnit, rlSrc1, r0);
1162 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001163 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001164 oatClobberCallRegs(cUnit);
1165 rlResult = oatGetReturn(cUnit);
1166 storeValue(cUnit, rlDest, rlResult);
1167 return false;
1168}
1169
buzbeeed3e9302011-09-23 17:34:19 -07001170STATIC bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001171 RegLocation rlDest, RegLocation rlSrc1,
1172 RegLocation rlSrc2)
1173{
1174 RegLocation rlResult;
1175 int funcOffset;
1176
1177 switch (mir->dalvikInsn.opcode) {
1178 case OP_ADD_DOUBLE_2ADDR:
1179 case OP_ADD_DOUBLE:
1180 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
1181 break;
1182 case OP_SUB_DOUBLE_2ADDR:
1183 case OP_SUB_DOUBLE:
1184 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
1185 break;
1186 case OP_DIV_DOUBLE_2ADDR:
1187 case OP_DIV_DOUBLE:
1188 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
1189 break;
1190 case OP_MUL_DOUBLE_2ADDR:
1191 case OP_MUL_DOUBLE:
1192 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
1193 break;
1194 case OP_REM_DOUBLE_2ADDR:
1195 case OP_REM_DOUBLE:
1196 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
1197 break;
1198 case OP_NEG_DOUBLE: {
1199 genNegDouble(cUnit, rlDest, rlSrc1);
1200 return false;
1201 }
1202 default:
1203 return true;
1204 }
1205 oatFlushAllRegs(cUnit); /* Send everything to home location */
1206 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1207 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1208 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -07001209 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001210 oatClobberCallRegs(cUnit);
1211 rlResult = oatGetReturnWide(cUnit);
1212 storeValueWide(cUnit, rlDest, rlResult);
1213 return false;
1214}
1215
buzbeeed3e9302011-09-23 17:34:19 -07001216STATIC bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001217{
1218 Opcode opcode = mir->dalvikInsn.opcode;
1219
1220 switch (opcode) {
1221 case OP_INT_TO_FLOAT:
1222 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
1223 1, 1);
1224 case OP_FLOAT_TO_INT:
1225 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
1226 1, 1);
1227 case OP_DOUBLE_TO_FLOAT:
1228 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
1229 2, 1);
1230 case OP_FLOAT_TO_DOUBLE:
1231 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
1232 1, 2);
1233 case OP_INT_TO_DOUBLE:
1234 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
1235 1, 2);
1236 case OP_DOUBLE_TO_INT:
1237 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
1238 2, 1);
1239 case OP_FLOAT_TO_LONG:
1240 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001241 pF2l), 1, 2);
buzbee67bf8852011-08-17 17:51:35 -07001242 case OP_LONG_TO_FLOAT:
1243 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
1244 2, 1);
1245 case OP_DOUBLE_TO_LONG:
1246 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001247 pD2l), 2, 2);
buzbee67bf8852011-08-17 17:51:35 -07001248 case OP_LONG_TO_DOUBLE:
1249 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
1250 2, 2);
1251 default:
1252 return true;
1253 }
1254 return false;
1255}
1256
1257/* Generate conditional branch instructions */
buzbeeed3e9302011-09-23 17:34:19 -07001258STATIC ArmLIR* genConditionalBranch(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001259 ArmConditionCode cond,
1260 ArmLIR* target)
1261{
1262 ArmLIR* branch = opCondBranch(cUnit, cond);
1263 branch->generic.target = (LIR*) target;
1264 return branch;
1265}
1266
buzbee67bf8852011-08-17 17:51:35 -07001267/*
1268 * Generate array store
1269 *
1270 */
buzbeeed3e9302011-09-23 17:34:19 -07001271STATIC void genArrayObjPut(CompilationUnit* cUnit, MIR* mir,
buzbee1b4c8592011-08-31 10:43:51 -07001272 RegLocation rlArray, RegLocation rlIndex,
1273 RegLocation rlSrc, int scale)
buzbee67bf8852011-08-17 17:51:35 -07001274{
1275 RegisterClass regClass = oatRegClassBySize(kWord);
buzbeec143c552011-08-20 17:38:58 -07001276 int lenOffset = Array::LengthOffset().Int32Value();
1277 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001278
1279 /* Make sure it's a legal object Put. Use direct regs at first */
1280 loadValueDirectFixed(cUnit, rlArray, r1);
1281 loadValueDirectFixed(cUnit, rlSrc, r0);
1282
1283 /* null array object? */
buzbee43a36422011-09-14 14:00:13 -07001284 genNullCheck(cUnit, rlArray.sRegLow, r1, mir);
buzbee67bf8852011-08-17 17:51:35 -07001285 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -07001286 OFFSETOF_MEMBER(Thread, pCanPutArrayElementFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -07001287 /* Get the array's clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001288 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001289 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001290 oatClobberCallRegs(cUnit);
1291
1292 // Now, redo loadValues in case they didn't survive the call
1293
1294 int regPtr;
1295 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1296 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1297
1298 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1299 oatClobber(cUnit, rlArray.lowReg);
1300 regPtr = rlArray.lowReg;
1301 } else {
1302 regPtr = oatAllocTemp(cUnit);
1303 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1304 }
1305
buzbee43a36422011-09-14 14:00:13 -07001306 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001307 int regLen = oatAllocTemp(cUnit);
1308 //NOTE: max live temps(4) here.
1309 /* Get len */
1310 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1311 /* regPtr -> array data */
1312 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001313 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001314 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001315 oatFreeTemp(cUnit, regLen);
1316 } else {
1317 /* regPtr -> array data */
1318 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1319 }
1320 /* at this point, regPtr points to array, 2 live temps */
1321 rlSrc = loadValue(cUnit, rlSrc, regClass);
1322 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1323 scale, kWord);
1324}
1325
1326/*
1327 * Generate array load
1328 */
buzbeeed3e9302011-09-23 17:34:19 -07001329STATIC void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -07001330 RegLocation rlArray, RegLocation rlIndex,
1331 RegLocation rlDest, int scale)
1332{
1333 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001334 int lenOffset = Array::LengthOffset().Int32Value();
1335 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001336 RegLocation rlResult;
1337 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1338 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1339 int regPtr;
1340
1341 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001342 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001343
1344 regPtr = oatAllocTemp(cUnit);
1345
buzbee43a36422011-09-14 14:00:13 -07001346 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001347 int regLen = oatAllocTemp(cUnit);
1348 /* Get len */
1349 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1350 /* regPtr -> array data */
1351 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001352 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001353 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001354 oatFreeTemp(cUnit, regLen);
1355 } else {
1356 /* regPtr -> array data */
1357 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1358 }
buzbeee9a72f62011-09-04 17:59:07 -07001359 oatFreeTemp(cUnit, rlArray.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001360 if ((size == kLong) || (size == kDouble)) {
1361 if (scale) {
1362 int rNewIndex = oatAllocTemp(cUnit);
1363 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1364 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1365 oatFreeTemp(cUnit, rNewIndex);
1366 } else {
1367 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1368 }
buzbeee9a72f62011-09-04 17:59:07 -07001369 oatFreeTemp(cUnit, rlIndex.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001370 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1371
1372 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1373
1374 oatFreeTemp(cUnit, regPtr);
1375 storeValueWide(cUnit, rlDest, rlResult);
1376 } else {
1377 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1378
1379 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1380 scale, size);
1381
1382 oatFreeTemp(cUnit, regPtr);
1383 storeValue(cUnit, rlDest, rlResult);
1384 }
1385}
1386
1387/*
1388 * Generate array store
1389 *
1390 */
buzbeeed3e9302011-09-23 17:34:19 -07001391STATIC void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -07001392 RegLocation rlArray, RegLocation rlIndex,
1393 RegLocation rlSrc, int scale)
1394{
1395 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001396 int lenOffset = Array::LengthOffset().Int32Value();
1397 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001398
1399 int regPtr;
1400 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1401 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1402
1403 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1404 oatClobber(cUnit, rlArray.lowReg);
1405 regPtr = rlArray.lowReg;
1406 } else {
1407 regPtr = oatAllocTemp(cUnit);
1408 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1409 }
1410
1411 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001412 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001413
buzbee43a36422011-09-14 14:00:13 -07001414 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001415 int regLen = oatAllocTemp(cUnit);
1416 //NOTE: max live temps(4) here.
1417 /* Get len */
1418 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1419 /* regPtr -> array data */
1420 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001421 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001422 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001423 oatFreeTemp(cUnit, regLen);
1424 } else {
1425 /* regPtr -> array data */
1426 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1427 }
1428 /* at this point, regPtr points to array, 2 live temps */
1429 if ((size == kLong) || (size == kDouble)) {
buzbee5ade1d22011-09-09 14:44:52 -07001430 //TUNING: specific wide routine that can handle fp regs
buzbee67bf8852011-08-17 17:51:35 -07001431 if (scale) {
1432 int rNewIndex = oatAllocTemp(cUnit);
1433 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1434 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1435 oatFreeTemp(cUnit, rNewIndex);
1436 } else {
1437 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1438 }
1439 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1440
1441 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1442
1443 oatFreeTemp(cUnit, regPtr);
1444 } else {
1445 rlSrc = loadValue(cUnit, rlSrc, regClass);
1446
1447 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1448 scale, size);
1449 }
1450}
1451
buzbeeed3e9302011-09-23 17:34:19 -07001452STATIC bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001453 RegLocation rlDest, RegLocation rlSrc1,
1454 RegLocation rlShift)
1455{
buzbee54330722011-08-23 16:46:55 -07001456 int funcOffset;
buzbee67bf8852011-08-17 17:51:35 -07001457
buzbee67bf8852011-08-17 17:51:35 -07001458 switch( mir->dalvikInsn.opcode) {
1459 case OP_SHL_LONG:
1460 case OP_SHL_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001461 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
buzbee67bf8852011-08-17 17:51:35 -07001462 break;
1463 case OP_SHR_LONG:
1464 case OP_SHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001465 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
buzbee67bf8852011-08-17 17:51:35 -07001466 break;
1467 case OP_USHR_LONG:
1468 case OP_USHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001469 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
buzbee67bf8852011-08-17 17:51:35 -07001470 break;
1471 default:
buzbee54330722011-08-23 16:46:55 -07001472 LOG(FATAL) << "Unexpected case";
buzbee67bf8852011-08-17 17:51:35 -07001473 return true;
1474 }
buzbee54330722011-08-23 16:46:55 -07001475 oatFlushAllRegs(cUnit); /* Send everything to home location */
1476 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1477 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1478 loadValueDirect(cUnit, rlShift, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -07001479 callRuntimeHelper(cUnit, rLR);
buzbee54330722011-08-23 16:46:55 -07001480 oatClobberCallRegs(cUnit);
1481 RegLocation rlResult = oatGetReturnWide(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001482 storeValueWide(cUnit, rlDest, rlResult);
1483 return false;
1484}
1485
buzbeeed3e9302011-09-23 17:34:19 -07001486STATIC bool genArithOpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001487 RegLocation rlDest, RegLocation rlSrc1,
1488 RegLocation rlSrc2)
1489{
1490 RegLocation rlResult;
1491 OpKind firstOp = kOpBkpt;
1492 OpKind secondOp = kOpBkpt;
1493 bool callOut = false;
1494 int funcOffset;
1495 int retReg = r0;
1496
1497 switch (mir->dalvikInsn.opcode) {
1498 case OP_NOT_LONG:
1499 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1500 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1501 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1502 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1503 storeValueWide(cUnit, rlDest, rlResult);
1504 return false;
1505 break;
1506 case OP_ADD_LONG:
1507 case OP_ADD_LONG_2ADDR:
1508 firstOp = kOpAdd;
1509 secondOp = kOpAdc;
1510 break;
1511 case OP_SUB_LONG:
1512 case OP_SUB_LONG_2ADDR:
1513 firstOp = kOpSub;
1514 secondOp = kOpSbc;
1515 break;
1516 case OP_MUL_LONG:
1517 case OP_MUL_LONG_2ADDR:
buzbee439c4fa2011-08-27 15:59:07 -07001518 callOut = true;
1519 retReg = r0;
1520 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1521 break;
buzbee67bf8852011-08-17 17:51:35 -07001522 case OP_DIV_LONG:
1523 case OP_DIV_LONG_2ADDR:
1524 callOut = true;
1525 retReg = r0;
1526 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1527 break;
1528 /* NOTE - result is in r2/r3 instead of r0/r1 */
1529 case OP_REM_LONG:
1530 case OP_REM_LONG_2ADDR:
1531 callOut = true;
1532 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1533 retReg = r2;
1534 break;
1535 case OP_AND_LONG_2ADDR:
1536 case OP_AND_LONG:
1537 firstOp = kOpAnd;
1538 secondOp = kOpAnd;
1539 break;
1540 case OP_OR_LONG:
1541 case OP_OR_LONG_2ADDR:
1542 firstOp = kOpOr;
1543 secondOp = kOpOr;
1544 break;
1545 case OP_XOR_LONG:
1546 case OP_XOR_LONG_2ADDR:
1547 firstOp = kOpXor;
1548 secondOp = kOpXor;
1549 break;
1550 case OP_NEG_LONG: {
1551 //TUNING: can improve this using Thumb2 code
1552 int tReg = oatAllocTemp(cUnit);
1553 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1554 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1555 loadConstantNoClobber(cUnit, tReg, 0);
1556 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1557 tReg, rlSrc2.lowReg);
1558 opRegReg(cUnit, kOpSbc, tReg, rlSrc2.highReg);
1559 genRegCopy(cUnit, rlResult.highReg, tReg);
1560 storeValueWide(cUnit, rlDest, rlResult);
1561 return false;
1562 }
1563 default:
1564 LOG(FATAL) << "Invalid long arith op";
1565 }
1566 if (!callOut) {
1567 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1568 } else {
1569 // Adjust return regs in to handle case of rem returning r2/r3
1570 oatFlushAllRegs(cUnit); /* Send everything to home location */
1571 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1572 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1573 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -07001574 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001575 oatClobberCallRegs(cUnit);
1576 if (retReg == r0)
1577 rlResult = oatGetReturnWide(cUnit);
1578 else
1579 rlResult = oatGetReturnWideAlt(cUnit);
1580 storeValueWide(cUnit, rlDest, rlResult);
1581 }
1582 return false;
1583}
1584
buzbeeed3e9302011-09-23 17:34:19 -07001585STATIC bool genArithOpInt(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001586 RegLocation rlDest, RegLocation rlSrc1,
1587 RegLocation rlSrc2)
1588{
1589 OpKind op = kOpBkpt;
1590 bool callOut = false;
1591 bool checkZero = false;
1592 bool unary = false;
1593 int retReg = r0;
1594 int funcOffset;
1595 RegLocation rlResult;
1596 bool shiftOp = false;
1597
1598 switch (mir->dalvikInsn.opcode) {
1599 case OP_NEG_INT:
1600 op = kOpNeg;
1601 unary = true;
1602 break;
1603 case OP_NOT_INT:
1604 op = kOpMvn;
1605 unary = true;
1606 break;
1607 case OP_ADD_INT:
1608 case OP_ADD_INT_2ADDR:
1609 op = kOpAdd;
1610 break;
1611 case OP_SUB_INT:
1612 case OP_SUB_INT_2ADDR:
1613 op = kOpSub;
1614 break;
1615 case OP_MUL_INT:
1616 case OP_MUL_INT_2ADDR:
1617 op = kOpMul;
1618 break;
1619 case OP_DIV_INT:
1620 case OP_DIV_INT_2ADDR:
1621 callOut = true;
1622 checkZero = true;
1623 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1624 retReg = r0;
1625 break;
1626 /* NOTE: returns in r1 */
1627 case OP_REM_INT:
1628 case OP_REM_INT_2ADDR:
1629 callOut = true;
1630 checkZero = true;
1631 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1632 retReg = r1;
1633 break;
1634 case OP_AND_INT:
1635 case OP_AND_INT_2ADDR:
1636 op = kOpAnd;
1637 break;
1638 case OP_OR_INT:
1639 case OP_OR_INT_2ADDR:
1640 op = kOpOr;
1641 break;
1642 case OP_XOR_INT:
1643 case OP_XOR_INT_2ADDR:
1644 op = kOpXor;
1645 break;
1646 case OP_SHL_INT:
1647 case OP_SHL_INT_2ADDR:
1648 shiftOp = true;
1649 op = kOpLsl;
1650 break;
1651 case OP_SHR_INT:
1652 case OP_SHR_INT_2ADDR:
1653 shiftOp = true;
1654 op = kOpAsr;
1655 break;
1656 case OP_USHR_INT:
1657 case OP_USHR_INT_2ADDR:
1658 shiftOp = true;
1659 op = kOpLsr;
1660 break;
1661 default:
1662 LOG(FATAL) << "Invalid word arith op: " <<
1663 (int)mir->dalvikInsn.opcode;
1664 }
1665 if (!callOut) {
1666 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1667 if (unary) {
1668 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1669 opRegReg(cUnit, op, rlResult.lowReg,
1670 rlSrc1.lowReg);
1671 } else {
1672 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1673 if (shiftOp) {
1674 int tReg = oatAllocTemp(cUnit);
1675 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1676 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1677 opRegRegReg(cUnit, op, rlResult.lowReg,
1678 rlSrc1.lowReg, tReg);
1679 oatFreeTemp(cUnit, tReg);
1680 } else {
1681 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1682 opRegRegReg(cUnit, op, rlResult.lowReg,
1683 rlSrc1.lowReg, rlSrc2.lowReg);
1684 }
1685 }
1686 storeValue(cUnit, rlDest, rlResult);
1687 } else {
1688 RegLocation rlResult;
1689 oatFlushAllRegs(cUnit); /* Send everything to home location */
1690 loadValueDirectFixed(cUnit, rlSrc2, r1);
1691 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1692 loadValueDirectFixed(cUnit, rlSrc1, r0);
1693 if (checkZero) {
buzbee5ade1d22011-09-09 14:44:52 -07001694 genImmedCheck(cUnit, kArmCondEq, r1, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07001695 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001696 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001697 oatClobberCallRegs(cUnit);
1698 if (retReg == r0)
1699 rlResult = oatGetReturn(cUnit);
1700 else
1701 rlResult = oatGetReturnAlt(cUnit);
1702 storeValue(cUnit, rlDest, rlResult);
1703 }
1704 return false;
1705}
1706
buzbeec1f45042011-09-21 16:03:19 -07001707/* Check if we need to check for pending suspend request */
buzbeeed3e9302011-09-23 17:34:19 -07001708STATIC void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
buzbeec1f45042011-09-21 16:03:19 -07001709{
buzbeec0ecd652011-09-25 18:11:54 -07001710 if (NO_SUSPEND || mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK) {
buzbeec1f45042011-09-21 16:03:19 -07001711 return;
1712 }
1713 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
1714 ArmLIR* branch = opCondBranch(cUnit, kArmCondEq);
1715 ArmLIR* retLab = newLIR0(cUnit, kArmPseudoTargetLabel);
1716 retLab->defMask = ENCODE_ALL;
1717 ArmLIR* target = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
1718 target->generic.dalvikOffset = cUnit->currentDalvikOffset;
1719 target->opcode = kArmPseudoSuspendTarget;
1720 target->operands[0] = (intptr_t)retLab;
1721 target->operands[1] = mir->offset;
1722 branch->generic.target = (LIR*)target;
1723 oatInsertGrowableList(&cUnit->suspendLaunchpads, (intptr_t)target);
1724}
1725
buzbee0d966cf2011-09-08 17:34:58 -07001726/* Check for pending suspend request. */
buzbeeed3e9302011-09-23 17:34:19 -07001727STATIC void genSuspendPoll(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001728{
buzbeec0ecd652011-09-25 18:11:54 -07001729 if (NO_SUSPEND || mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK) {
buzbeec1f45042011-09-21 16:03:19 -07001730 return;
1731 }
buzbee0d966cf2011-09-08 17:34:58 -07001732 oatLockCallTemps(cUnit); // Explicit register usage
1733 int rSuspendCount = r1;
buzbee67bf8852011-08-17 17:51:35 -07001734 ArmLIR* ld;
buzbee0d966cf2011-09-08 17:34:58 -07001735 ld = loadWordDisp(cUnit, rSELF,
1736 art::Thread::SuspendCountOffset().Int32Value(), rSuspendCount);
buzbee67bf8852011-08-17 17:51:35 -07001737 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
buzbee0d966cf2011-09-08 17:34:58 -07001738 loadWordDisp(cUnit, rSELF,
1739 OFFSETOF_MEMBER(Thread, pCheckSuspendFromCode), rLR);
1740 genRegCopy(cUnit, r0, rSELF);
1741 opRegImm(cUnit, kOpCmp, rSuspendCount, 0);
buzbeeb0ebba02011-09-17 10:52:59 -07001742 /*
1743 * FIXME: for efficiency we should use an if-converted suspend
1744 * test here. However, support for IT is a bit weak at the
1745 * moment, and requires knowledge of the exact number of instructions
1746 * to fall in the skip shadow. While the exception mechanism
1747 * remains in flux, use a compare and branch sequence. Once
1748 * things firm up, restore the conditional skip (and perhaps
1749 * fix the utility to handle variable-sized shadows).
1750 */
1751#if 0
buzbee0d966cf2011-09-08 17:34:58 -07001752 genIT(cUnit, kArmCondNe, "");
buzbeeec5adf32011-09-11 15:25:43 -07001753 callUnwindableHelper(cUnit, rLR); // CheckSuspendFromCode(self)
buzbeeb0ebba02011-09-17 10:52:59 -07001754#else
1755 ArmLIR* branch = opCondBranch(cUnit, kArmCondEq);
Ian Rogersff1ed472011-09-20 13:46:24 -07001756 callRuntimeHelper(cUnit, rLR); // CheckSuspendFromCode(self)
buzbeeb0ebba02011-09-17 10:52:59 -07001757 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
1758 target->defMask = ENCODE_ALL;
1759 branch->generic.target = (LIR*)target;
1760#endif
buzbee0d966cf2011-09-08 17:34:58 -07001761 oatFreeCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001762}
1763
1764/*
1765 * The following are the first-level codegen routines that analyze the format
1766 * of each bytecode then either dispatch special purpose codegen routines
1767 * or produce corresponding Thumb instructions directly.
1768 */
1769
buzbeeed3e9302011-09-23 17:34:19 -07001770STATIC bool isPowerOfTwo(int x)
buzbee67bf8852011-08-17 17:51:35 -07001771{
1772 return (x & (x - 1)) == 0;
1773}
1774
1775// Returns true if no more than two bits are set in 'x'.
buzbeeed3e9302011-09-23 17:34:19 -07001776STATIC bool isPopCountLE2(unsigned int x)
buzbee67bf8852011-08-17 17:51:35 -07001777{
1778 x &= x - 1;
1779 return (x & (x - 1)) == 0;
1780}
1781
1782// Returns the index of the lowest set bit in 'x'.
buzbeeed3e9302011-09-23 17:34:19 -07001783STATIC int lowestSetBit(unsigned int x) {
buzbee67bf8852011-08-17 17:51:35 -07001784 int bit_posn = 0;
1785 while ((x & 0xf) == 0) {
1786 bit_posn += 4;
1787 x >>= 4;
1788 }
1789 while ((x & 1) == 0) {
1790 bit_posn++;
1791 x >>= 1;
1792 }
1793 return bit_posn;
1794}
1795
1796// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1797// and store the result in 'rlDest'.
buzbeeed3e9302011-09-23 17:34:19 -07001798STATIC bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode,
buzbee67bf8852011-08-17 17:51:35 -07001799 RegLocation rlSrc, RegLocation rlDest, int lit)
1800{
1801 if (lit < 2 || !isPowerOfTwo(lit)) {
1802 return false;
1803 }
1804 int k = lowestSetBit(lit);
1805 if (k >= 30) {
1806 // Avoid special cases.
1807 return false;
1808 }
1809 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 ||
1810 dalvikOpcode == OP_DIV_INT_LIT16);
1811 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1812 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1813 if (div) {
1814 int tReg = oatAllocTemp(cUnit);
1815 if (lit == 2) {
1816 // Division by 2 is by far the most common division by constant.
1817 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1818 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1819 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1820 } else {
1821 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1822 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1823 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1824 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1825 }
1826 } else {
1827 int cReg = oatAllocTemp(cUnit);
1828 loadConstant(cUnit, cReg, lit - 1);
1829 int tReg1 = oatAllocTemp(cUnit);
1830 int tReg2 = oatAllocTemp(cUnit);
1831 if (lit == 2) {
1832 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1833 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1834 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1835 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1836 } else {
1837 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1838 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1839 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1840 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1841 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1842 }
1843 }
1844 storeValue(cUnit, rlDest, rlResult);
1845 return true;
1846}
1847
1848// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1849// and store the result in 'rlDest'.
buzbeeed3e9302011-09-23 17:34:19 -07001850STATIC bool handleEasyMultiply(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001851 RegLocation rlSrc, RegLocation rlDest, int lit)
1852{
1853 // Can we simplify this multiplication?
1854 bool powerOfTwo = false;
1855 bool popCountLE2 = false;
1856 bool powerOfTwoMinusOne = false;
1857 if (lit < 2) {
1858 // Avoid special cases.
1859 return false;
1860 } else if (isPowerOfTwo(lit)) {
1861 powerOfTwo = true;
1862 } else if (isPopCountLE2(lit)) {
1863 popCountLE2 = true;
1864 } else if (isPowerOfTwo(lit + 1)) {
1865 powerOfTwoMinusOne = true;
1866 } else {
1867 return false;
1868 }
1869 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1870 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1871 if (powerOfTwo) {
1872 // Shift.
1873 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1874 lowestSetBit(lit));
1875 } else if (popCountLE2) {
1876 // Shift and add and shift.
1877 int firstBit = lowestSetBit(lit);
1878 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1879 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1880 firstBit, secondBit);
1881 } else {
1882 // Reverse subtract: (src << (shift + 1)) - src.
buzbeeed3e9302011-09-23 17:34:19 -07001883 DCHECK(powerOfTwoMinusOne);
buzbee5ade1d22011-09-09 14:44:52 -07001884 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
buzbee67bf8852011-08-17 17:51:35 -07001885 int tReg = oatAllocTemp(cUnit);
1886 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1887 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1888 }
1889 storeValue(cUnit, rlDest, rlResult);
1890 return true;
1891}
1892
buzbeeed3e9302011-09-23 17:34:19 -07001893STATIC bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001894 RegLocation rlDest, RegLocation rlSrc,
1895 int lit)
1896{
1897 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1898 RegLocation rlResult;
1899 OpKind op = (OpKind)0; /* Make gcc happy */
1900 int shiftOp = false;
1901 bool isDiv = false;
1902 int funcOffset;
1903
1904 switch (dalvikOpcode) {
1905 case OP_RSUB_INT_LIT8:
1906 case OP_RSUB_INT: {
1907 int tReg;
1908 //TUNING: add support for use of Arm rsub op
1909 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1910 tReg = oatAllocTemp(cUnit);
1911 loadConstant(cUnit, tReg, lit);
1912 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1913 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1914 tReg, rlSrc.lowReg);
1915 storeValue(cUnit, rlDest, rlResult);
1916 return false;
1917 break;
1918 }
1919
1920 case OP_ADD_INT_LIT8:
1921 case OP_ADD_INT_LIT16:
1922 op = kOpAdd;
1923 break;
1924 case OP_MUL_INT_LIT8:
1925 case OP_MUL_INT_LIT16: {
1926 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1927 return false;
1928 }
1929 op = kOpMul;
1930 break;
1931 }
1932 case OP_AND_INT_LIT8:
1933 case OP_AND_INT_LIT16:
1934 op = kOpAnd;
1935 break;
1936 case OP_OR_INT_LIT8:
1937 case OP_OR_INT_LIT16:
1938 op = kOpOr;
1939 break;
1940 case OP_XOR_INT_LIT8:
1941 case OP_XOR_INT_LIT16:
1942 op = kOpXor;
1943 break;
1944 case OP_SHL_INT_LIT8:
1945 lit &= 31;
1946 shiftOp = true;
1947 op = kOpLsl;
1948 break;
1949 case OP_SHR_INT_LIT8:
1950 lit &= 31;
1951 shiftOp = true;
1952 op = kOpAsr;
1953 break;
1954 case OP_USHR_INT_LIT8:
1955 lit &= 31;
1956 shiftOp = true;
1957 op = kOpLsr;
1958 break;
1959
1960 case OP_DIV_INT_LIT8:
1961 case OP_DIV_INT_LIT16:
1962 case OP_REM_INT_LIT8:
1963 case OP_REM_INT_LIT16:
1964 if (lit == 0) {
buzbee5ade1d22011-09-09 14:44:52 -07001965 genImmedCheck(cUnit, kArmCondAl, 0, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07001966 return false;
1967 }
1968 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
1969 return false;
1970 }
1971 oatFlushAllRegs(cUnit); /* Everything to home location */
1972 loadValueDirectFixed(cUnit, rlSrc, r0);
1973 oatClobber(cUnit, r0);
1974 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
1975 (dalvikOpcode == OP_DIV_INT_LIT16)) {
1976 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1977 isDiv = true;
1978 } else {
1979 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1980 isDiv = false;
1981 }
1982 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1983 loadConstant(cUnit, r1, lit);
Ian Rogersff1ed472011-09-20 13:46:24 -07001984 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001985 oatClobberCallRegs(cUnit);
1986 if (isDiv)
1987 rlResult = oatGetReturn(cUnit);
1988 else
1989 rlResult = oatGetReturnAlt(cUnit);
1990 storeValue(cUnit, rlDest, rlResult);
1991 return false;
1992 break;
1993 default:
1994 return true;
1995 }
1996 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1997 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1998 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
1999 if (shiftOp && (lit == 0)) {
2000 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2001 } else {
2002 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2003 }
2004 storeValue(cUnit, rlDest, rlResult);
2005 return false;
2006}
2007
2008/* Architectural-specific debugging helpers go here */
2009void oatArchDump(void)
2010{
2011 /* Print compiled opcode in this VM instance */
2012 int i, start, streak;
2013 char buf[1024];
2014
2015 streak = i = 0;
2016 buf[0] = 0;
2017 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2018 i++;
2019 }
2020 if (i == kNumPackedOpcodes) {
2021 return;
2022 }
2023 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
2024 if (opcodeCoverage[i]) {
2025 streak++;
2026 } else {
2027 if (streak == 1) {
2028 sprintf(buf+strlen(buf), "%x,", start);
2029 } else {
2030 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
2031 }
2032 streak = 0;
2033 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2034 i++;
2035 }
2036 if (i < kNumPackedOpcodes) {
2037 streak = 1;
2038 start = i;
2039 }
2040 }
2041 }
2042 if (streak) {
2043 if (streak == 1) {
2044 sprintf(buf+strlen(buf), "%x", start);
2045 } else {
2046 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
2047 }
2048 }
2049 if (strlen(buf)) {
2050 LOG(INFO) << "dalvik.vm.oat.op = " << buf;
2051 }
2052}