blob: c9d1bb9a6f5b3a78f5c7a8dfb7930e9506293926 [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) {
buzbeec0ecd652011-09-25 18:11:54 -0700833 int tReg = oatAllocTemp(cUnit);
834 genRegCopy(cUnit, tReg, rlSrc1.highReg);
835 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
836 rlSrc2.lowReg);
837 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
838 rlSrc2.highReg);
839 oatFreeTemp(cUnit, tReg);
840 } else {
841 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
842 rlSrc2.lowReg);
843 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
844 rlSrc2.highReg);
845 }
buzbee439c4fa2011-08-27 15:59:07 -0700846 /*
847 * NOTE: If rlDest refers to a frame variable in a large frame, the
848 * following storeValueWide might need to allocate a temp register.
849 * To further work around the lack of a spill capability, explicitly
850 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
851 * Remove when spill is functional.
852 */
853 freeRegLocTemps(cUnit, rlResult, rlSrc1);
854 freeRegLocTemps(cUnit, rlResult, rlSrc2);
buzbee67bf8852011-08-17 17:51:35 -0700855 storeValueWide(cUnit, rlDest, rlResult);
buzbee9e0f9b02011-08-24 15:32:46 -0700856 oatClobber(cUnit, rLR);
857 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee67bf8852011-08-17 17:51:35 -0700858}
859
860void oatInitializeRegAlloc(CompilationUnit* cUnit)
861{
862 int numRegs = sizeof(coreRegs)/sizeof(*coreRegs);
863 int numReserved = sizeof(reservedRegs)/sizeof(*reservedRegs);
864 int numTemps = sizeof(coreTemps)/sizeof(*coreTemps);
865 int numFPRegs = sizeof(fpRegs)/sizeof(*fpRegs);
866 int numFPTemps = sizeof(fpTemps)/sizeof(*fpTemps);
867 RegisterPool *pool = (RegisterPool *)oatNew(sizeof(*pool), true);
868 cUnit->regPool = pool;
869 pool->numCoreRegs = numRegs;
870 pool->coreRegs = (RegisterInfo *)
871 oatNew(numRegs * sizeof(*cUnit->regPool->coreRegs), true);
872 pool->numFPRegs = numFPRegs;
873 pool->FPRegs = (RegisterInfo *)
874 oatNew(numFPRegs * sizeof(*cUnit->regPool->FPRegs), true);
875 oatInitPool(pool->coreRegs, coreRegs, pool->numCoreRegs);
876 oatInitPool(pool->FPRegs, fpRegs, pool->numFPRegs);
877 // Keep special registers from being allocated
878 for (int i = 0; i < numReserved; i++) {
buzbeec0ecd652011-09-25 18:11:54 -0700879 if (NO_SUSPEND && (reservedRegs[i] == rSUSPEND)) {
880 //To measure cost of suspend check
881 continue;
882 }
buzbee67bf8852011-08-17 17:51:35 -0700883 oatMarkInUse(cUnit, reservedRegs[i]);
884 }
885 // Mark temp regs - all others not in use can be used for promotion
886 for (int i = 0; i < numTemps; i++) {
887 oatMarkTemp(cUnit, coreTemps[i]);
888 }
889 for (int i = 0; i < numFPTemps; i++) {
890 oatMarkTemp(cUnit, fpTemps[i]);
891 }
buzbeec0ecd652011-09-25 18:11:54 -0700892 // Construct the alias map.
893 cUnit->phiAliasMap = (int*)oatNew(cUnit->numSSARegs *
894 sizeof(cUnit->phiAliasMap[0]), false);
895 for (int i = 0; i < cUnit->numSSARegs; i++) {
896 cUnit->phiAliasMap[i] = i;
897 }
898 for (MIR* phi = cUnit->phiList; phi; phi = phi->meta.phiNext) {
899 int defReg = phi->ssaRep->defs[0];
900 for (int i = 0; i < phi->ssaRep->numUses; i++) {
901 for (int j = 0; j < cUnit->numSSARegs; j++) {
902 if (cUnit->phiAliasMap[j] == phi->ssaRep->uses[i]) {
903 cUnit->phiAliasMap[j] = defReg;
904 }
905 }
906 }
907 }
buzbee67bf8852011-08-17 17:51:35 -0700908}
909
910/*
911 * Handle simple case (thin lock) inline. If it's complicated, bail
912 * out to the heavyweight lock/unlock routines. We'll use dedicated
913 * registers here in order to be in the right position in case we
914 * to bail to dvm[Lock/Unlock]Object(self, object)
915 *
916 * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object
917 * r1 -> object [arg1 for dvm[Lock/Unlock]Object
918 * r2 -> intial contents of object->lock, later result of strex
919 * r3 -> self->threadId
920 * r12 -> allow to be used by utilities as general temp
921 *
922 * The result of the strex is 0 if we acquire the lock.
923 *
924 * See comments in Sync.c for the layout of the lock word.
925 * Of particular interest to this code is the test for the
926 * simple case - which we handle inline. For monitor enter, the
927 * simple case is thin lock, held by no-one. For monitor exit,
928 * the simple case is thin lock, held by the unlocking thread with
929 * a recurse count of 0.
930 *
931 * A minor complication is that there is a field in the lock word
932 * unrelated to locking: the hash state. This field must be ignored, but
933 * preserved.
934 *
935 */
buzbeeed3e9302011-09-23 17:34:19 -0700936STATIC void genMonitorEnter(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700937 RegLocation rlSrc)
938{
939 ArmLIR* target;
940 ArmLIR* hopTarget;
941 ArmLIR* branch;
942 ArmLIR* hopBranch;
943
944 oatFlushAllRegs(cUnit);
Elliott Hughes5f791332011-09-15 17:45:30 -0700945 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700946 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700947 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee5ade1d22011-09-09 14:44:52 -0700948 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700949 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -0700950 newLIR3(cUnit, kThumb2Ldrex, r2, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700951 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
buzbeec143c552011-08-20 17:38:58 -0700952 // Align owner
Elliott Hughes5f791332011-09-15 17:45:30 -0700953 opRegImm(cUnit, kOpLsl, r3, LW_LOCK_OWNER_SHIFT);
buzbee67bf8852011-08-17 17:51:35 -0700954 // Is lock unheld on lock or held by us (==threadId) on unlock?
Elliott Hughes5f791332011-09-15 17:45:30 -0700955 newLIR4(cUnit, kThumb2Bfi, r3, r2, 0, LW_LOCK_OWNER_SHIFT - 1);
956 newLIR3(cUnit, kThumb2Bfc, r2, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee67bf8852011-08-17 17:51:35 -0700957 hopBranch = newLIR2(cUnit, kThumb2Cbnz, r2, 0);
buzbeec143c552011-08-20 17:38:58 -0700958 newLIR4(cUnit, kThumb2Strex, r2, r3, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700959 Object::MonitorOffset().Int32Value() >> 2);
buzbee67bf8852011-08-17 17:51:35 -0700960 oatGenMemBarrier(cUnit, kSY);
961 branch = newLIR2(cUnit, kThumb2Cbz, r2, 0);
962
963 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
964 hopTarget->defMask = ENCODE_ALL;
965 hopBranch->generic.target = (LIR*)hopTarget;
966
buzbee1b4c8592011-08-31 10:43:51 -0700967 // Go expensive route - artLockObjectFromCode(self, obj);
968 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700969 rLR);
970 genRegCopy(cUnit, r0, rSELF);
Ian Rogersff1ed472011-09-20 13:46:24 -0700971 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700972
973 // Resume here
974 target = newLIR0(cUnit, kArmPseudoTargetLabel);
975 target->defMask = ENCODE_ALL;
976 branch->generic.target = (LIR*)target;
977}
978
979/*
980 * For monitor unlock, we don't have to use ldrex/strex. Once
981 * we've determined that the lock is thin and that we own it with
982 * a zero recursion count, it's safe to punch it back to the
983 * initial, unlock thin state with a store word.
984 */
buzbeeed3e9302011-09-23 17:34:19 -0700985STATIC void genMonitorExit(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700986 RegLocation rlSrc)
987{
988 ArmLIR* target;
989 ArmLIR* branch;
990 ArmLIR* hopTarget;
991 ArmLIR* hopBranch;
992
Elliott Hughes5f791332011-09-15 17:45:30 -0700993 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700994 oatFlushAllRegs(cUnit);
995 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700996 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee5ade1d22011-09-09 14:44:52 -0700997 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700998 loadWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r2); // Get lock
Elliott Hughes54e7df12011-09-16 11:47:04 -0700999 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -07001000 // Is lock unheld on lock or held by us (==threadId) on unlock?
Elliott Hughes5f791332011-09-15 17:45:30 -07001001 opRegRegImm(cUnit, kOpAnd, r12, r2, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
buzbeec143c552011-08-20 17:38:58 -07001002 // Align owner
Elliott Hughes5f791332011-09-15 17:45:30 -07001003 opRegImm(cUnit, kOpLsl, r3, LW_LOCK_OWNER_SHIFT);
1004 newLIR3(cUnit, kThumb2Bfc, r2, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee67bf8852011-08-17 17:51:35 -07001005 opRegReg(cUnit, kOpSub, r2, r3);
1006 hopBranch = opCondBranch(cUnit, kArmCondNe);
1007 oatGenMemBarrier(cUnit, kSY);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001008 storeWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r12);
buzbee67bf8852011-08-17 17:51:35 -07001009 branch = opNone(cUnit, kOpUncondBr);
1010
1011 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
1012 hopTarget->defMask = ENCODE_ALL;
1013 hopBranch->generic.target = (LIR*)hopTarget;
1014
buzbee1b4c8592011-08-31 10:43:51 -07001015 // Go expensive route - UnlockObjectFromCode(self, obj);
1016 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -07001017 rLR);
1018 genRegCopy(cUnit, r0, rSELF);
Ian Rogersff1ed472011-09-20 13:46:24 -07001019 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001020
1021 // Resume here
1022 target = newLIR0(cUnit, kArmPseudoTargetLabel);
1023 target->defMask = ENCODE_ALL;
1024 branch->generic.target = (LIR*)target;
1025}
1026
1027/*
1028 * 64-bit 3way compare function.
1029 * mov rX, #-1
1030 * cmp op1hi, op2hi
1031 * blt done
1032 * bgt flip
1033 * sub rX, op1lo, op2lo (treat as unsigned)
1034 * beq done
1035 * ite hi
1036 * mov(hi) rX, #-1
1037 * mov(!hi) rX, #1
1038 * flip:
1039 * neg rX
1040 * done:
1041 */
buzbeeed3e9302011-09-23 17:34:19 -07001042STATIC void genCmpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001043 RegLocation rlDest, RegLocation rlSrc1,
1044 RegLocation rlSrc2)
1045{
buzbee67bf8852011-08-17 17:51:35 -07001046 ArmLIR* target1;
1047 ArmLIR* target2;
1048 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1049 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
buzbeeb29e4d12011-09-26 15:05:48 -07001050 int tReg = oatAllocTemp(cUnit);
1051 loadConstant(cUnit, tReg, -1);
buzbee67bf8852011-08-17 17:51:35 -07001052 opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg);
1053 ArmLIR* branch1 = opCondBranch(cUnit, kArmCondLt);
1054 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondGt);
buzbeeb29e4d12011-09-26 15:05:48 -07001055 opRegRegReg(cUnit, kOpSub, tReg, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001056 ArmLIR* branch3 = opCondBranch(cUnit, kArmCondEq);
1057
1058 genIT(cUnit, kArmCondHi, "E");
buzbeeb29e4d12011-09-26 15:05:48 -07001059 newLIR2(cUnit, kThumb2MovImmShift, tReg, modifiedImmediate(-1));
1060 loadConstant(cUnit, tReg, 1);
buzbee67bf8852011-08-17 17:51:35 -07001061 genBarrier(cUnit);
1062
1063 target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
1064 target2->defMask = -1;
buzbeeb29e4d12011-09-26 15:05:48 -07001065 opRegReg(cUnit, kOpNeg, tReg, tReg);
buzbee67bf8852011-08-17 17:51:35 -07001066
1067 target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
1068 target1->defMask = -1;
1069
buzbeeb29e4d12011-09-26 15:05:48 -07001070 RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change
1071 rlTemp.lowReg = tReg;
buzbee67bf8852011-08-17 17:51:35 -07001072 storeValue(cUnit, rlDest, rlTemp);
buzbeeb29e4d12011-09-26 15:05:48 -07001073 oatFreeTemp(cUnit, tReg);
buzbee67bf8852011-08-17 17:51:35 -07001074
1075 branch1->generic.target = (LIR*)target1;
1076 branch2->generic.target = (LIR*)target2;
1077 branch3->generic.target = branch1->generic.target;
1078}
1079
buzbeeed3e9302011-09-23 17:34:19 -07001080STATIC void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001081 RegLocation rlSrc, RegLocation rlResult, int lit,
1082 int firstBit, int secondBit)
1083{
1084 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1085 encodeShift(kArmLsl, secondBit - firstBit));
1086 if (firstBit != 0) {
1087 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1088 }
1089}
1090
buzbeeed3e9302011-09-23 17:34:19 -07001091STATIC bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
buzbee67bf8852011-08-17 17:51:35 -07001092 int srcSize, int tgtSize)
1093{
1094 /*
1095 * Don't optimize the register usage since it calls out to support
1096 * functions
1097 */
1098 RegLocation rlSrc;
1099 RegLocation rlDest;
1100 oatFlushAllRegs(cUnit); /* Send everything to home location */
1101 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1102 if (srcSize == 1) {
1103 rlSrc = oatGetSrc(cUnit, mir, 0);
1104 loadValueDirectFixed(cUnit, rlSrc, r0);
1105 } else {
1106 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
1107 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
1108 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001109 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001110 oatClobberCallRegs(cUnit);
1111 if (tgtSize == 1) {
1112 RegLocation rlResult;
1113 rlDest = oatGetDest(cUnit, mir, 0);
1114 rlResult = oatGetReturn(cUnit);
1115 storeValue(cUnit, rlDest, rlResult);
1116 } else {
1117 RegLocation rlResult;
1118 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1119 rlResult = oatGetReturnWide(cUnit);
1120 storeValueWide(cUnit, rlDest, rlResult);
1121 }
1122 return false;
1123}
1124
buzbeeed3e9302011-09-23 17:34:19 -07001125STATIC bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001126 RegLocation rlDest, RegLocation rlSrc1,
1127 RegLocation rlSrc2)
1128{
1129 RegLocation rlResult;
1130 int funcOffset;
1131
1132 switch (mir->dalvikInsn.opcode) {
1133 case OP_ADD_FLOAT_2ADDR:
1134 case OP_ADD_FLOAT:
1135 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
1136 break;
1137 case OP_SUB_FLOAT_2ADDR:
1138 case OP_SUB_FLOAT:
1139 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
1140 break;
1141 case OP_DIV_FLOAT_2ADDR:
1142 case OP_DIV_FLOAT:
1143 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
1144 break;
1145 case OP_MUL_FLOAT_2ADDR:
1146 case OP_MUL_FLOAT:
1147 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
1148 break;
1149 case OP_REM_FLOAT_2ADDR:
1150 case OP_REM_FLOAT:
1151 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
1152 break;
1153 case OP_NEG_FLOAT: {
1154 genNegFloat(cUnit, rlDest, rlSrc1);
1155 return false;
1156 }
1157 default:
1158 return true;
1159 }
1160 oatFlushAllRegs(cUnit); /* Send everything to home location */
1161 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1162 loadValueDirectFixed(cUnit, rlSrc1, r0);
1163 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001164 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001165 oatClobberCallRegs(cUnit);
1166 rlResult = oatGetReturn(cUnit);
1167 storeValue(cUnit, rlDest, rlResult);
1168 return false;
1169}
1170
buzbeeed3e9302011-09-23 17:34:19 -07001171STATIC bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001172 RegLocation rlDest, RegLocation rlSrc1,
1173 RegLocation rlSrc2)
1174{
1175 RegLocation rlResult;
1176 int funcOffset;
1177
1178 switch (mir->dalvikInsn.opcode) {
1179 case OP_ADD_DOUBLE_2ADDR:
1180 case OP_ADD_DOUBLE:
1181 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
1182 break;
1183 case OP_SUB_DOUBLE_2ADDR:
1184 case OP_SUB_DOUBLE:
1185 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
1186 break;
1187 case OP_DIV_DOUBLE_2ADDR:
1188 case OP_DIV_DOUBLE:
1189 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
1190 break;
1191 case OP_MUL_DOUBLE_2ADDR:
1192 case OP_MUL_DOUBLE:
1193 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
1194 break;
1195 case OP_REM_DOUBLE_2ADDR:
1196 case OP_REM_DOUBLE:
1197 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
1198 break;
1199 case OP_NEG_DOUBLE: {
1200 genNegDouble(cUnit, rlDest, rlSrc1);
1201 return false;
1202 }
1203 default:
1204 return true;
1205 }
1206 oatFlushAllRegs(cUnit); /* Send everything to home location */
1207 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1208 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1209 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -07001210 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001211 oatClobberCallRegs(cUnit);
1212 rlResult = oatGetReturnWide(cUnit);
1213 storeValueWide(cUnit, rlDest, rlResult);
1214 return false;
1215}
1216
buzbeeed3e9302011-09-23 17:34:19 -07001217STATIC bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001218{
1219 Opcode opcode = mir->dalvikInsn.opcode;
1220
1221 switch (opcode) {
1222 case OP_INT_TO_FLOAT:
1223 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
1224 1, 1);
1225 case OP_FLOAT_TO_INT:
1226 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
1227 1, 1);
1228 case OP_DOUBLE_TO_FLOAT:
1229 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
1230 2, 1);
1231 case OP_FLOAT_TO_DOUBLE:
1232 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
1233 1, 2);
1234 case OP_INT_TO_DOUBLE:
1235 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
1236 1, 2);
1237 case OP_DOUBLE_TO_INT:
1238 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
1239 2, 1);
1240 case OP_FLOAT_TO_LONG:
1241 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001242 pF2l), 1, 2);
buzbee67bf8852011-08-17 17:51:35 -07001243 case OP_LONG_TO_FLOAT:
1244 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
1245 2, 1);
1246 case OP_DOUBLE_TO_LONG:
1247 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001248 pD2l), 2, 2);
buzbee67bf8852011-08-17 17:51:35 -07001249 case OP_LONG_TO_DOUBLE:
1250 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
1251 2, 2);
1252 default:
1253 return true;
1254 }
1255 return false;
1256}
1257
1258/* Generate conditional branch instructions */
buzbeeed3e9302011-09-23 17:34:19 -07001259STATIC ArmLIR* genConditionalBranch(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001260 ArmConditionCode cond,
1261 ArmLIR* target)
1262{
1263 ArmLIR* branch = opCondBranch(cUnit, cond);
1264 branch->generic.target = (LIR*) target;
1265 return branch;
1266}
1267
buzbee67bf8852011-08-17 17:51:35 -07001268/*
1269 * Generate array store
1270 *
1271 */
buzbeeed3e9302011-09-23 17:34:19 -07001272STATIC void genArrayObjPut(CompilationUnit* cUnit, MIR* mir,
buzbee1b4c8592011-08-31 10:43:51 -07001273 RegLocation rlArray, RegLocation rlIndex,
1274 RegLocation rlSrc, int scale)
buzbee67bf8852011-08-17 17:51:35 -07001275{
1276 RegisterClass regClass = oatRegClassBySize(kWord);
buzbeec143c552011-08-20 17:38:58 -07001277 int lenOffset = Array::LengthOffset().Int32Value();
1278 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001279
1280 /* Make sure it's a legal object Put. Use direct regs at first */
1281 loadValueDirectFixed(cUnit, rlArray, r1);
1282 loadValueDirectFixed(cUnit, rlSrc, r0);
1283
1284 /* null array object? */
buzbee43a36422011-09-14 14:00:13 -07001285 genNullCheck(cUnit, rlArray.sRegLow, r1, mir);
buzbee67bf8852011-08-17 17:51:35 -07001286 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -07001287 OFFSETOF_MEMBER(Thread, pCanPutArrayElementFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -07001288 /* Get the array's clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001289 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001290 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001291 oatClobberCallRegs(cUnit);
1292
1293 // Now, redo loadValues in case they didn't survive the call
1294
1295 int regPtr;
1296 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1297 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1298
1299 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1300 oatClobber(cUnit, rlArray.lowReg);
1301 regPtr = rlArray.lowReg;
1302 } else {
1303 regPtr = oatAllocTemp(cUnit);
1304 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1305 }
1306
buzbee43a36422011-09-14 14:00:13 -07001307 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001308 int regLen = oatAllocTemp(cUnit);
1309 //NOTE: max live temps(4) here.
1310 /* Get len */
1311 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1312 /* regPtr -> array data */
1313 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001314 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001315 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001316 oatFreeTemp(cUnit, regLen);
1317 } else {
1318 /* regPtr -> array data */
1319 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1320 }
1321 /* at this point, regPtr points to array, 2 live temps */
1322 rlSrc = loadValue(cUnit, rlSrc, regClass);
1323 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1324 scale, kWord);
1325}
1326
1327/*
1328 * Generate array load
1329 */
buzbeeed3e9302011-09-23 17:34:19 -07001330STATIC void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -07001331 RegLocation rlArray, RegLocation rlIndex,
1332 RegLocation rlDest, int scale)
1333{
1334 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001335 int lenOffset = Array::LengthOffset().Int32Value();
1336 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001337 RegLocation rlResult;
1338 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1339 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1340 int regPtr;
1341
1342 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001343 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001344
1345 regPtr = oatAllocTemp(cUnit);
1346
buzbee43a36422011-09-14 14:00:13 -07001347 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001348 int regLen = oatAllocTemp(cUnit);
1349 /* Get len */
1350 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1351 /* regPtr -> array data */
1352 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001353 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001354 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001355 oatFreeTemp(cUnit, regLen);
1356 } else {
1357 /* regPtr -> array data */
1358 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1359 }
buzbeee9a72f62011-09-04 17:59:07 -07001360 oatFreeTemp(cUnit, rlArray.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001361 if ((size == kLong) || (size == kDouble)) {
1362 if (scale) {
1363 int rNewIndex = oatAllocTemp(cUnit);
1364 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1365 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1366 oatFreeTemp(cUnit, rNewIndex);
1367 } else {
1368 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1369 }
buzbeee9a72f62011-09-04 17:59:07 -07001370 oatFreeTemp(cUnit, rlIndex.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001371 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1372
1373 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1374
1375 oatFreeTemp(cUnit, regPtr);
1376 storeValueWide(cUnit, rlDest, rlResult);
1377 } else {
1378 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1379
1380 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1381 scale, size);
1382
1383 oatFreeTemp(cUnit, regPtr);
1384 storeValue(cUnit, rlDest, rlResult);
1385 }
1386}
1387
1388/*
1389 * Generate array store
1390 *
1391 */
buzbeeed3e9302011-09-23 17:34:19 -07001392STATIC void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -07001393 RegLocation rlArray, RegLocation rlIndex,
1394 RegLocation rlSrc, int scale)
1395{
1396 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001397 int lenOffset = Array::LengthOffset().Int32Value();
1398 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001399
1400 int regPtr;
1401 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1402 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1403
1404 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1405 oatClobber(cUnit, rlArray.lowReg);
1406 regPtr = rlArray.lowReg;
1407 } else {
1408 regPtr = oatAllocTemp(cUnit);
1409 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1410 }
1411
1412 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001413 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001414
buzbee43a36422011-09-14 14:00:13 -07001415 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001416 int regLen = oatAllocTemp(cUnit);
1417 //NOTE: max live temps(4) here.
1418 /* Get len */
1419 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1420 /* regPtr -> array data */
1421 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001422 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001423 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001424 oatFreeTemp(cUnit, regLen);
1425 } else {
1426 /* regPtr -> array data */
1427 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1428 }
1429 /* at this point, regPtr points to array, 2 live temps */
1430 if ((size == kLong) || (size == kDouble)) {
buzbee5ade1d22011-09-09 14:44:52 -07001431 //TUNING: specific wide routine that can handle fp regs
buzbee67bf8852011-08-17 17:51:35 -07001432 if (scale) {
1433 int rNewIndex = oatAllocTemp(cUnit);
1434 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1435 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1436 oatFreeTemp(cUnit, rNewIndex);
1437 } else {
1438 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1439 }
1440 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1441
1442 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1443
1444 oatFreeTemp(cUnit, regPtr);
1445 } else {
1446 rlSrc = loadValue(cUnit, rlSrc, regClass);
1447
1448 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1449 scale, size);
1450 }
1451}
1452
buzbeeed3e9302011-09-23 17:34:19 -07001453STATIC bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001454 RegLocation rlDest, RegLocation rlSrc1,
1455 RegLocation rlShift)
1456{
buzbee54330722011-08-23 16:46:55 -07001457 int funcOffset;
buzbee67bf8852011-08-17 17:51:35 -07001458
buzbee67bf8852011-08-17 17:51:35 -07001459 switch( mir->dalvikInsn.opcode) {
1460 case OP_SHL_LONG:
1461 case OP_SHL_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001462 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
buzbee67bf8852011-08-17 17:51:35 -07001463 break;
1464 case OP_SHR_LONG:
1465 case OP_SHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001466 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
buzbee67bf8852011-08-17 17:51:35 -07001467 break;
1468 case OP_USHR_LONG:
1469 case OP_USHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001470 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
buzbee67bf8852011-08-17 17:51:35 -07001471 break;
1472 default:
buzbee54330722011-08-23 16:46:55 -07001473 LOG(FATAL) << "Unexpected case";
buzbee67bf8852011-08-17 17:51:35 -07001474 return true;
1475 }
buzbee54330722011-08-23 16:46:55 -07001476 oatFlushAllRegs(cUnit); /* Send everything to home location */
1477 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1478 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1479 loadValueDirect(cUnit, rlShift, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -07001480 callRuntimeHelper(cUnit, rLR);
buzbee54330722011-08-23 16:46:55 -07001481 oatClobberCallRegs(cUnit);
1482 RegLocation rlResult = oatGetReturnWide(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001483 storeValueWide(cUnit, rlDest, rlResult);
1484 return false;
1485}
1486
buzbeeed3e9302011-09-23 17:34:19 -07001487STATIC bool genArithOpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001488 RegLocation rlDest, RegLocation rlSrc1,
1489 RegLocation rlSrc2)
1490{
1491 RegLocation rlResult;
1492 OpKind firstOp = kOpBkpt;
1493 OpKind secondOp = kOpBkpt;
1494 bool callOut = false;
1495 int funcOffset;
1496 int retReg = r0;
1497
1498 switch (mir->dalvikInsn.opcode) {
1499 case OP_NOT_LONG:
1500 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1501 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeeb29e4d12011-09-26 15:05:48 -07001502 // Check for destructive overlap
1503 if (rlResult.lowReg == rlSrc2.highReg) {
1504 int tReg = oatAllocTemp(cUnit);
1505 genRegCopy(cUnit, tReg, rlSrc2.highReg);
1506 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1507 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
1508 oatFreeTemp(cUnit, tReg);
1509 } else {
1510 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1511 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1512 }
buzbee67bf8852011-08-17 17:51:35 -07001513 storeValueWide(cUnit, rlDest, rlResult);
1514 return false;
1515 break;
1516 case OP_ADD_LONG:
1517 case OP_ADD_LONG_2ADDR:
1518 firstOp = kOpAdd;
1519 secondOp = kOpAdc;
1520 break;
1521 case OP_SUB_LONG:
1522 case OP_SUB_LONG_2ADDR:
1523 firstOp = kOpSub;
1524 secondOp = kOpSbc;
1525 break;
1526 case OP_MUL_LONG:
1527 case OP_MUL_LONG_2ADDR:
buzbee439c4fa2011-08-27 15:59:07 -07001528 callOut = true;
1529 retReg = r0;
1530 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1531 break;
buzbee67bf8852011-08-17 17:51:35 -07001532 case OP_DIV_LONG:
1533 case OP_DIV_LONG_2ADDR:
1534 callOut = true;
1535 retReg = r0;
1536 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1537 break;
1538 /* NOTE - result is in r2/r3 instead of r0/r1 */
1539 case OP_REM_LONG:
1540 case OP_REM_LONG_2ADDR:
1541 callOut = true;
1542 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1543 retReg = r2;
1544 break;
1545 case OP_AND_LONG_2ADDR:
1546 case OP_AND_LONG:
1547 firstOp = kOpAnd;
1548 secondOp = kOpAnd;
1549 break;
1550 case OP_OR_LONG:
1551 case OP_OR_LONG_2ADDR:
1552 firstOp = kOpOr;
1553 secondOp = kOpOr;
1554 break;
1555 case OP_XOR_LONG:
1556 case OP_XOR_LONG_2ADDR:
1557 firstOp = kOpXor;
1558 secondOp = kOpXor;
1559 break;
1560 case OP_NEG_LONG: {
buzbee67bf8852011-08-17 17:51:35 -07001561 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1562 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeeb29e4d12011-09-26 15:05:48 -07001563 int zReg = oatAllocTemp(cUnit);
1564 loadConstantNoClobber(cUnit, zReg, 0);
1565 // Check for destructive overlap
1566 if (rlResult.lowReg == rlSrc2.highReg) {
1567 int tReg = oatAllocTemp(cUnit);
1568 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1569 zReg, rlSrc2.lowReg);
1570 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1571 zReg, tReg);
1572 oatFreeTemp(cUnit, tReg);
1573 } else {
1574 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1575 zReg, rlSrc2.lowReg);
1576 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1577 zReg, rlSrc2.highReg);
1578 }
1579 oatFreeTemp(cUnit, zReg);
buzbee67bf8852011-08-17 17:51:35 -07001580 storeValueWide(cUnit, rlDest, rlResult);
1581 return false;
1582 }
1583 default:
1584 LOG(FATAL) << "Invalid long arith op";
1585 }
1586 if (!callOut) {
1587 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1588 } else {
1589 // Adjust return regs in to handle case of rem returning r2/r3
1590 oatFlushAllRegs(cUnit); /* Send everything to home location */
1591 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1592 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1593 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -07001594 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001595 oatClobberCallRegs(cUnit);
1596 if (retReg == r0)
1597 rlResult = oatGetReturnWide(cUnit);
1598 else
1599 rlResult = oatGetReturnWideAlt(cUnit);
1600 storeValueWide(cUnit, rlDest, rlResult);
1601 }
1602 return false;
1603}
1604
buzbeeed3e9302011-09-23 17:34:19 -07001605STATIC bool genArithOpInt(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001606 RegLocation rlDest, RegLocation rlSrc1,
1607 RegLocation rlSrc2)
1608{
1609 OpKind op = kOpBkpt;
1610 bool callOut = false;
1611 bool checkZero = false;
1612 bool unary = false;
1613 int retReg = r0;
1614 int funcOffset;
1615 RegLocation rlResult;
1616 bool shiftOp = false;
1617
1618 switch (mir->dalvikInsn.opcode) {
1619 case OP_NEG_INT:
1620 op = kOpNeg;
1621 unary = true;
1622 break;
1623 case OP_NOT_INT:
1624 op = kOpMvn;
1625 unary = true;
1626 break;
1627 case OP_ADD_INT:
1628 case OP_ADD_INT_2ADDR:
1629 op = kOpAdd;
1630 break;
1631 case OP_SUB_INT:
1632 case OP_SUB_INT_2ADDR:
1633 op = kOpSub;
1634 break;
1635 case OP_MUL_INT:
1636 case OP_MUL_INT_2ADDR:
1637 op = kOpMul;
1638 break;
1639 case OP_DIV_INT:
1640 case OP_DIV_INT_2ADDR:
1641 callOut = true;
1642 checkZero = true;
1643 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1644 retReg = r0;
1645 break;
1646 /* NOTE: returns in r1 */
1647 case OP_REM_INT:
1648 case OP_REM_INT_2ADDR:
1649 callOut = true;
1650 checkZero = true;
1651 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1652 retReg = r1;
1653 break;
1654 case OP_AND_INT:
1655 case OP_AND_INT_2ADDR:
1656 op = kOpAnd;
1657 break;
1658 case OP_OR_INT:
1659 case OP_OR_INT_2ADDR:
1660 op = kOpOr;
1661 break;
1662 case OP_XOR_INT:
1663 case OP_XOR_INT_2ADDR:
1664 op = kOpXor;
1665 break;
1666 case OP_SHL_INT:
1667 case OP_SHL_INT_2ADDR:
1668 shiftOp = true;
1669 op = kOpLsl;
1670 break;
1671 case OP_SHR_INT:
1672 case OP_SHR_INT_2ADDR:
1673 shiftOp = true;
1674 op = kOpAsr;
1675 break;
1676 case OP_USHR_INT:
1677 case OP_USHR_INT_2ADDR:
1678 shiftOp = true;
1679 op = kOpLsr;
1680 break;
1681 default:
1682 LOG(FATAL) << "Invalid word arith op: " <<
1683 (int)mir->dalvikInsn.opcode;
1684 }
1685 if (!callOut) {
1686 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1687 if (unary) {
1688 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1689 opRegReg(cUnit, op, rlResult.lowReg,
1690 rlSrc1.lowReg);
1691 } else {
1692 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1693 if (shiftOp) {
1694 int tReg = oatAllocTemp(cUnit);
1695 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1696 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1697 opRegRegReg(cUnit, op, rlResult.lowReg,
1698 rlSrc1.lowReg, tReg);
1699 oatFreeTemp(cUnit, tReg);
1700 } else {
1701 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1702 opRegRegReg(cUnit, op, rlResult.lowReg,
1703 rlSrc1.lowReg, rlSrc2.lowReg);
1704 }
1705 }
1706 storeValue(cUnit, rlDest, rlResult);
1707 } else {
1708 RegLocation rlResult;
1709 oatFlushAllRegs(cUnit); /* Send everything to home location */
1710 loadValueDirectFixed(cUnit, rlSrc2, r1);
1711 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1712 loadValueDirectFixed(cUnit, rlSrc1, r0);
1713 if (checkZero) {
buzbee5ade1d22011-09-09 14:44:52 -07001714 genImmedCheck(cUnit, kArmCondEq, r1, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07001715 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001716 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001717 oatClobberCallRegs(cUnit);
1718 if (retReg == r0)
1719 rlResult = oatGetReturn(cUnit);
1720 else
1721 rlResult = oatGetReturnAlt(cUnit);
1722 storeValue(cUnit, rlDest, rlResult);
1723 }
1724 return false;
1725}
1726
buzbeec1f45042011-09-21 16:03:19 -07001727/* Check if we need to check for pending suspend request */
buzbeeed3e9302011-09-23 17:34:19 -07001728STATIC void genSuspendTest(CompilationUnit* cUnit, MIR* mir)
buzbeec1f45042011-09-21 16:03:19 -07001729{
buzbeec0ecd652011-09-25 18:11:54 -07001730 if (NO_SUSPEND || mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK) {
buzbeec1f45042011-09-21 16:03:19 -07001731 return;
1732 }
1733 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
1734 ArmLIR* branch = opCondBranch(cUnit, kArmCondEq);
1735 ArmLIR* retLab = newLIR0(cUnit, kArmPseudoTargetLabel);
1736 retLab->defMask = ENCODE_ALL;
1737 ArmLIR* target = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
1738 target->generic.dalvikOffset = cUnit->currentDalvikOffset;
1739 target->opcode = kArmPseudoSuspendTarget;
1740 target->operands[0] = (intptr_t)retLab;
1741 target->operands[1] = mir->offset;
1742 branch->generic.target = (LIR*)target;
1743 oatInsertGrowableList(&cUnit->suspendLaunchpads, (intptr_t)target);
1744}
1745
buzbee0d966cf2011-09-08 17:34:58 -07001746/* Check for pending suspend request. */
buzbeeed3e9302011-09-23 17:34:19 -07001747STATIC void genSuspendPoll(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001748{
buzbeec0ecd652011-09-25 18:11:54 -07001749 if (NO_SUSPEND || mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK) {
buzbeec1f45042011-09-21 16:03:19 -07001750 return;
1751 }
buzbee0d966cf2011-09-08 17:34:58 -07001752 oatLockCallTemps(cUnit); // Explicit register usage
1753 int rSuspendCount = r1;
buzbee67bf8852011-08-17 17:51:35 -07001754 ArmLIR* ld;
buzbee0d966cf2011-09-08 17:34:58 -07001755 ld = loadWordDisp(cUnit, rSELF,
1756 art::Thread::SuspendCountOffset().Int32Value(), rSuspendCount);
buzbee67bf8852011-08-17 17:51:35 -07001757 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
buzbee0d966cf2011-09-08 17:34:58 -07001758 loadWordDisp(cUnit, rSELF,
1759 OFFSETOF_MEMBER(Thread, pCheckSuspendFromCode), rLR);
1760 genRegCopy(cUnit, r0, rSELF);
1761 opRegImm(cUnit, kOpCmp, rSuspendCount, 0);
buzbeeb0ebba02011-09-17 10:52:59 -07001762 /*
1763 * FIXME: for efficiency we should use an if-converted suspend
1764 * test here. However, support for IT is a bit weak at the
1765 * moment, and requires knowledge of the exact number of instructions
1766 * to fall in the skip shadow. While the exception mechanism
1767 * remains in flux, use a compare and branch sequence. Once
1768 * things firm up, restore the conditional skip (and perhaps
1769 * fix the utility to handle variable-sized shadows).
1770 */
1771#if 0
buzbee0d966cf2011-09-08 17:34:58 -07001772 genIT(cUnit, kArmCondNe, "");
buzbeeec5adf32011-09-11 15:25:43 -07001773 callUnwindableHelper(cUnit, rLR); // CheckSuspendFromCode(self)
buzbeeb0ebba02011-09-17 10:52:59 -07001774#else
1775 ArmLIR* branch = opCondBranch(cUnit, kArmCondEq);
Ian Rogersff1ed472011-09-20 13:46:24 -07001776 callRuntimeHelper(cUnit, rLR); // CheckSuspendFromCode(self)
buzbeeb0ebba02011-09-17 10:52:59 -07001777 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
1778 target->defMask = ENCODE_ALL;
1779 branch->generic.target = (LIR*)target;
1780#endif
buzbee0d966cf2011-09-08 17:34:58 -07001781 oatFreeCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001782}
1783
1784/*
1785 * The following are the first-level codegen routines that analyze the format
1786 * of each bytecode then either dispatch special purpose codegen routines
1787 * or produce corresponding Thumb instructions directly.
1788 */
1789
buzbeeed3e9302011-09-23 17:34:19 -07001790STATIC bool isPowerOfTwo(int x)
buzbee67bf8852011-08-17 17:51:35 -07001791{
1792 return (x & (x - 1)) == 0;
1793}
1794
1795// Returns true if no more than two bits are set in 'x'.
buzbeeed3e9302011-09-23 17:34:19 -07001796STATIC bool isPopCountLE2(unsigned int x)
buzbee67bf8852011-08-17 17:51:35 -07001797{
1798 x &= x - 1;
1799 return (x & (x - 1)) == 0;
1800}
1801
1802// Returns the index of the lowest set bit in 'x'.
buzbeeed3e9302011-09-23 17:34:19 -07001803STATIC int lowestSetBit(unsigned int x) {
buzbee67bf8852011-08-17 17:51:35 -07001804 int bit_posn = 0;
1805 while ((x & 0xf) == 0) {
1806 bit_posn += 4;
1807 x >>= 4;
1808 }
1809 while ((x & 1) == 0) {
1810 bit_posn++;
1811 x >>= 1;
1812 }
1813 return bit_posn;
1814}
1815
1816// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1817// and store the result in 'rlDest'.
buzbeeed3e9302011-09-23 17:34:19 -07001818STATIC bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode,
buzbee67bf8852011-08-17 17:51:35 -07001819 RegLocation rlSrc, RegLocation rlDest, int lit)
1820{
1821 if (lit < 2 || !isPowerOfTwo(lit)) {
1822 return false;
1823 }
1824 int k = lowestSetBit(lit);
1825 if (k >= 30) {
1826 // Avoid special cases.
1827 return false;
1828 }
1829 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 ||
1830 dalvikOpcode == OP_DIV_INT_LIT16);
1831 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1832 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1833 if (div) {
1834 int tReg = oatAllocTemp(cUnit);
1835 if (lit == 2) {
1836 // Division by 2 is by far the most common division by constant.
1837 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1838 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1839 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1840 } else {
1841 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1842 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1843 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1844 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1845 }
1846 } else {
1847 int cReg = oatAllocTemp(cUnit);
1848 loadConstant(cUnit, cReg, lit - 1);
1849 int tReg1 = oatAllocTemp(cUnit);
1850 int tReg2 = oatAllocTemp(cUnit);
1851 if (lit == 2) {
1852 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1853 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1854 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1855 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1856 } else {
1857 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1858 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1859 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1860 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1861 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1862 }
1863 }
1864 storeValue(cUnit, rlDest, rlResult);
1865 return true;
1866}
1867
1868// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1869// and store the result in 'rlDest'.
buzbeeed3e9302011-09-23 17:34:19 -07001870STATIC bool handleEasyMultiply(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001871 RegLocation rlSrc, RegLocation rlDest, int lit)
1872{
1873 // Can we simplify this multiplication?
1874 bool powerOfTwo = false;
1875 bool popCountLE2 = false;
1876 bool powerOfTwoMinusOne = false;
1877 if (lit < 2) {
1878 // Avoid special cases.
1879 return false;
1880 } else if (isPowerOfTwo(lit)) {
1881 powerOfTwo = true;
1882 } else if (isPopCountLE2(lit)) {
1883 popCountLE2 = true;
1884 } else if (isPowerOfTwo(lit + 1)) {
1885 powerOfTwoMinusOne = true;
1886 } else {
1887 return false;
1888 }
1889 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1890 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1891 if (powerOfTwo) {
1892 // Shift.
1893 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1894 lowestSetBit(lit));
1895 } else if (popCountLE2) {
1896 // Shift and add and shift.
1897 int firstBit = lowestSetBit(lit);
1898 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1899 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1900 firstBit, secondBit);
1901 } else {
1902 // Reverse subtract: (src << (shift + 1)) - src.
buzbeeed3e9302011-09-23 17:34:19 -07001903 DCHECK(powerOfTwoMinusOne);
buzbee5ade1d22011-09-09 14:44:52 -07001904 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
buzbee67bf8852011-08-17 17:51:35 -07001905 int tReg = oatAllocTemp(cUnit);
1906 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1907 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1908 }
1909 storeValue(cUnit, rlDest, rlResult);
1910 return true;
1911}
1912
buzbeeed3e9302011-09-23 17:34:19 -07001913STATIC bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001914 RegLocation rlDest, RegLocation rlSrc,
1915 int lit)
1916{
1917 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1918 RegLocation rlResult;
1919 OpKind op = (OpKind)0; /* Make gcc happy */
1920 int shiftOp = false;
1921 bool isDiv = false;
1922 int funcOffset;
1923
1924 switch (dalvikOpcode) {
1925 case OP_RSUB_INT_LIT8:
1926 case OP_RSUB_INT: {
1927 int tReg;
1928 //TUNING: add support for use of Arm rsub op
1929 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1930 tReg = oatAllocTemp(cUnit);
1931 loadConstant(cUnit, tReg, lit);
1932 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1933 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1934 tReg, rlSrc.lowReg);
1935 storeValue(cUnit, rlDest, rlResult);
1936 return false;
1937 break;
1938 }
1939
1940 case OP_ADD_INT_LIT8:
1941 case OP_ADD_INT_LIT16:
1942 op = kOpAdd;
1943 break;
1944 case OP_MUL_INT_LIT8:
1945 case OP_MUL_INT_LIT16: {
1946 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1947 return false;
1948 }
1949 op = kOpMul;
1950 break;
1951 }
1952 case OP_AND_INT_LIT8:
1953 case OP_AND_INT_LIT16:
1954 op = kOpAnd;
1955 break;
1956 case OP_OR_INT_LIT8:
1957 case OP_OR_INT_LIT16:
1958 op = kOpOr;
1959 break;
1960 case OP_XOR_INT_LIT8:
1961 case OP_XOR_INT_LIT16:
1962 op = kOpXor;
1963 break;
1964 case OP_SHL_INT_LIT8:
1965 lit &= 31;
1966 shiftOp = true;
1967 op = kOpLsl;
1968 break;
1969 case OP_SHR_INT_LIT8:
1970 lit &= 31;
1971 shiftOp = true;
1972 op = kOpAsr;
1973 break;
1974 case OP_USHR_INT_LIT8:
1975 lit &= 31;
1976 shiftOp = true;
1977 op = kOpLsr;
1978 break;
1979
1980 case OP_DIV_INT_LIT8:
1981 case OP_DIV_INT_LIT16:
1982 case OP_REM_INT_LIT8:
1983 case OP_REM_INT_LIT16:
1984 if (lit == 0) {
buzbee5ade1d22011-09-09 14:44:52 -07001985 genImmedCheck(cUnit, kArmCondAl, 0, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07001986 return false;
1987 }
1988 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
1989 return false;
1990 }
1991 oatFlushAllRegs(cUnit); /* Everything to home location */
1992 loadValueDirectFixed(cUnit, rlSrc, r0);
1993 oatClobber(cUnit, r0);
1994 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
1995 (dalvikOpcode == OP_DIV_INT_LIT16)) {
1996 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1997 isDiv = true;
1998 } else {
1999 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
2000 isDiv = false;
2001 }
2002 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
2003 loadConstant(cUnit, r1, lit);
Ian Rogersff1ed472011-09-20 13:46:24 -07002004 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07002005 oatClobberCallRegs(cUnit);
2006 if (isDiv)
2007 rlResult = oatGetReturn(cUnit);
2008 else
2009 rlResult = oatGetReturnAlt(cUnit);
2010 storeValue(cUnit, rlDest, rlResult);
2011 return false;
2012 break;
2013 default:
2014 return true;
2015 }
2016 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2017 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2018 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2019 if (shiftOp && (lit == 0)) {
2020 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2021 } else {
2022 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2023 }
2024 storeValue(cUnit, rlDest, rlResult);
2025 return false;
2026}
2027
2028/* Architectural-specific debugging helpers go here */
2029void oatArchDump(void)
2030{
2031 /* Print compiled opcode in this VM instance */
2032 int i, start, streak;
2033 char buf[1024];
2034
2035 streak = i = 0;
2036 buf[0] = 0;
2037 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2038 i++;
2039 }
2040 if (i == kNumPackedOpcodes) {
2041 return;
2042 }
2043 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
2044 if (opcodeCoverage[i]) {
2045 streak++;
2046 } else {
2047 if (streak == 1) {
2048 sprintf(buf+strlen(buf), "%x,", start);
2049 } else {
2050 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
2051 }
2052 streak = 0;
2053 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2054 i++;
2055 }
2056 if (i < kNumPackedOpcodes) {
2057 streak = 1;
2058 start = i;
2059 }
2060 }
2061 }
2062 if (streak) {
2063 if (streak == 1) {
2064 sprintf(buf+strlen(buf), "%x", start);
2065 } else {
2066 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
2067 }
2068 }
2069 if (strlen(buf)) {
2070 LOG(INFO) << "dalvik.vm.oat.op = " << buf;
2071 }
2072}