blob: 5c836600d1776f948bf7bcdf9daad527e5e57f30 [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{
buzbee6181f792011-09-29 11:14:04 -070060 oatClobberCalleeSave(cUnit);
buzbeeec5adf32011-09-11 15:25:43 -070061 return opReg(cUnit, kOpBlx, reg);
62}
63
buzbee1b4c8592011-08-31 10:43:51 -070064/* Generate unconditional branch instructions */
buzbeeed3e9302011-09-23 17:34:19 -070065STATIC ArmLIR* genUnconditionalBranch(CompilationUnit* cUnit, ArmLIR* target)
buzbee1b4c8592011-08-31 10:43:51 -070066{
67 ArmLIR* branch = opNone(cUnit, kOpUncondBr);
68 branch->generic.target = (LIR*) target;
69 return branch;
70}
71
buzbee67bf8852011-08-17 17:51:35 -070072/*
73 * Generate a Thumb2 IT instruction, which can nullify up to
74 * four subsequent instructions based on a condition and its
75 * inverse. The condition applies to the first instruction, which
76 * is executed if the condition is met. The string "guide" consists
77 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
78 * A "T" means the instruction is executed if the condition is
79 * met, and an "E" means the instruction is executed if the condition
80 * is not met.
81 */
buzbeeed3e9302011-09-23 17:34:19 -070082STATIC ArmLIR* genIT(CompilationUnit* cUnit, ArmConditionCode code,
buzbee67bf8852011-08-17 17:51:35 -070083 const char* guide)
84{
85 int mask;
86 int condBit = code & 1;
87 int altBit = condBit ^ 1;
88 int mask3 = 0;
89 int mask2 = 0;
90 int mask1 = 0;
91
92 //Note: case fallthroughs intentional
93 switch(strlen(guide)) {
94 case 3:
95 mask1 = (guide[2] == 'T') ? condBit : altBit;
96 case 2:
97 mask2 = (guide[1] == 'T') ? condBit : altBit;
98 case 1:
99 mask3 = (guide[0] == 'T') ? condBit : altBit;
100 break;
101 case 0:
102 break;
103 default:
104 LOG(FATAL) << "OAT: bad case in genIT";
105 }
106 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
107 (1 << (3 - strlen(guide)));
108 return newLIR2(cUnit, kThumb2It, code, mask);
109}
110
111/*
112 * Insert a kArmPseudoCaseLabel at the beginning of the Dalvik
113 * offset vaddr. This label will be used to fix up the case
114 * branch table during the assembly phase. Be sure to set
115 * all resource flags on this to prevent code motion across
116 * target boundaries. KeyVal is just there for debugging.
117 */
buzbeeed3e9302011-09-23 17:34:19 -0700118STATIC ArmLIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
buzbee67bf8852011-08-17 17:51:35 -0700119{
120 ArmLIR* lir;
121 for (lir = (ArmLIR*)cUnit->firstLIRInsn; lir; lir = NEXT_LIR(lir)) {
122 if ((lir->opcode == kArmPseudoDalvikByteCodeBoundary) &&
123 (lir->generic.dalvikOffset == vaddr)) {
124 ArmLIR* newLabel = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
125 newLabel->generic.dalvikOffset = vaddr;
126 newLabel->opcode = kArmPseudoCaseLabel;
127 newLabel->operands[0] = keyVal;
128 oatInsertLIRAfter((LIR*)lir, (LIR*)newLabel);
129 return newLabel;
130 }
131 }
132 oatCodegenDump(cUnit);
133 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
134 return NULL; // Quiet gcc
135}
136
buzbeeed3e9302011-09-23 17:34:19 -0700137STATIC void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
buzbee67bf8852011-08-17 17:51:35 -0700138{
139 const u2* table = tabRec->table;
140 int baseVaddr = tabRec->vaddr;
141 int *targets = (int*)&table[4];
142 int entries = table[1];
143 int lowKey = s4FromSwitchData(&table[2]);
144 for (int i = 0; i < entries; i++) {
145 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
146 i + lowKey);
147 }
148}
149
buzbeeed3e9302011-09-23 17:34:19 -0700150STATIC void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
buzbee67bf8852011-08-17 17:51:35 -0700151{
152 const u2* table = tabRec->table;
153 int baseVaddr = tabRec->vaddr;
154 int entries = table[1];
155 int* keys = (int*)&table[2];
156 int* targets = &keys[entries];
157 for (int i = 0; i < entries; i++) {
158 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
159 keys[i]);
160 }
161}
162
163void oatProcessSwitchTables(CompilationUnit* cUnit)
164{
165 GrowableListIterator iterator;
166 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
167 while (true) {
168 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
169 &iterator);
170 if (tabRec == NULL) break;
171 if (tabRec->table[0] == kPackedSwitchSignature)
172 markPackedCaseLabels(cUnit, tabRec);
173 else if (tabRec->table[0] == kSparseSwitchSignature)
174 markSparseCaseLabels(cUnit, tabRec);
175 else {
176 LOG(FATAL) << "Invalid switch table";
177 }
178 }
179}
180
buzbeeed3e9302011-09-23 17:34:19 -0700181STATIC void dumpSparseSwitchTable(const u2* table)
buzbee67bf8852011-08-17 17:51:35 -0700182 /*
183 * Sparse switch data format:
184 * ushort ident = 0x0200 magic value
185 * ushort size number of entries in the table; > 0
186 * int keys[size] keys, sorted low-to-high; 32-bit aligned
187 * int targets[size] branch targets, relative to switch opcode
188 *
189 * Total size is (2+size*4) 16-bit code units.
190 */
191{
192 u2 ident = table[0];
193 int entries = table[1];
194 int* keys = (int*)&table[2];
195 int* targets = &keys[entries];
196 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
197 ", entries: " << std::dec << entries;
198 for (int i = 0; i < entries; i++) {
199 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
200 targets[i];
201 }
202}
203
buzbeeed3e9302011-09-23 17:34:19 -0700204STATIC void dumpPackedSwitchTable(const u2* table)
buzbee67bf8852011-08-17 17:51:35 -0700205 /*
206 * Packed switch data format:
207 * ushort ident = 0x0100 magic value
208 * ushort size number of entries in the table
209 * int first_key first (and lowest) switch case value
210 * int targets[size] branch targets, relative to switch opcode
211 *
212 * Total size is (4+size*2) 16-bit code units.
213 */
214{
215 u2 ident = table[0];
216 int* targets = (int*)&table[4];
217 int entries = table[1];
218 int lowKey = s4FromSwitchData(&table[2]);
219 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
220 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
221 for (int i = 0; i < entries; i++) {
222 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
223 targets[i];
224 }
225}
226
227/*
228 * The sparse table in the literal pool is an array of <key,displacement>
229 * pairs. For each set, we'll load them as a pair using ldmia.
230 * This means that the register number of the temp we use for the key
231 * must be lower than the reg for the displacement.
232 *
233 * The test loop will look something like:
234 *
235 * adr rBase, <table>
236 * ldr rVal, [rSP, vRegOff]
237 * mov rIdx, #tableSize
238 * lp:
239 * ldmia rBase!, {rKey, rDisp}
240 * sub rIdx, #1
241 * cmp rVal, rKey
242 * ifeq
243 * add rPC, rDisp ; This is the branch from which we compute displacement
244 * cbnz rIdx, lp
245 */
buzbeeed3e9302011-09-23 17:34:19 -0700246STATIC void genSparseSwitch(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700247 RegLocation rlSrc)
248{
249 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
250 if (cUnit->printMe) {
251 dumpSparseSwitchTable(table);
252 }
253 // Add the table to the list - we'll process it later
254 SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable),
255 true);
256 tabRec->table = table;
257 tabRec->vaddr = mir->offset;
258 int size = table[1];
259 tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true);
260 oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec);
261
262 // Get the switch value
263 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
264 int rBase = oatAllocTemp(cUnit);
265 /* Allocate key and disp temps */
266 int rKey = oatAllocTemp(cUnit);
267 int rDisp = oatAllocTemp(cUnit);
268 // Make sure rKey's register number is less than rDisp's number for ldmia
269 if (rKey > rDisp) {
270 int tmp = rDisp;
271 rDisp = rKey;
272 rKey = tmp;
273 }
274 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700275 newLIR3(cUnit, kThumb2Adr, rBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700276 // Set up rIdx
277 int rIdx = oatAllocTemp(cUnit);
278 loadConstant(cUnit, rIdx, size);
279 // Establish loop branch target
280 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
281 target->defMask = ENCODE_ALL;
282 // Load next key/disp
283 newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp));
284 opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg);
285 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
286 genIT(cUnit, kArmCondEq, "");
287 ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp);
288 tabRec->bxInst = switchBranch;
289 // Needs to use setflags encoding here
290 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
291 ArmLIR* branch = opCondBranch(cUnit, kArmCondNe);
292 branch->generic.target = (LIR*)target;
293}
294
295
buzbeeed3e9302011-09-23 17:34:19 -0700296STATIC void genPackedSwitch(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700297 RegLocation rlSrc)
298{
299 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
300 if (cUnit->printMe) {
301 dumpPackedSwitchTable(table);
302 }
303 // Add the table to the list - we'll process it later
304 SwitchTable *tabRec = (SwitchTable *)oatNew(sizeof(SwitchTable),
305 true);
306 tabRec->table = table;
307 tabRec->vaddr = mir->offset;
308 int size = table[1];
309 tabRec->targets = (ArmLIR* *)oatNew(size * sizeof(ArmLIR*), true);
310 oatInsertGrowableList(&cUnit->switchTables, (intptr_t)tabRec);
311
312 // Get the switch value
313 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
314 int tableBase = oatAllocTemp(cUnit);
315 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700316 newLIR3(cUnit, kThumb2Adr, tableBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700317 int lowKey = s4FromSwitchData(&table[2]);
318 int keyReg;
319 // Remove the bias, if necessary
320 if (lowKey == 0) {
321 keyReg = rlSrc.lowReg;
322 } else {
323 keyReg = oatAllocTemp(cUnit);
324 opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
325 }
326 // Bounds check - if < 0 or >= size continue following switch
327 opRegImm(cUnit, kOpCmp, keyReg, size-1);
328 ArmLIR* branchOver = opCondBranch(cUnit, kArmCondHi);
329
330 // Load the displacement from the switch table
331 int dispReg = oatAllocTemp(cUnit);
332 loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord);
333
334 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
335 ArmLIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg);
336 tabRec->bxInst = switchBranch;
337
338 /* branchOver target here */
339 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
340 target->defMask = ENCODE_ALL;
341 branchOver->generic.target = (LIR*)target;
342}
343
344/*
345 * Array data table format:
346 * ushort ident = 0x0300 magic value
347 * ushort width width of each element in the table
348 * uint size number of elements in the table
349 * ubyte data[size*width] table of data values (may contain a single-byte
350 * padding at the end)
351 *
352 * Total size is 4+(width * size + 1)/2 16-bit code units.
353 */
buzbeeed3e9302011-09-23 17:34:19 -0700354STATIC void genFillArrayData(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700355 RegLocation rlSrc)
356{
357 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
358 // Add the table to the list - we'll process it later
359 FillArrayData *tabRec = (FillArrayData *)
360 oatNew(sizeof(FillArrayData), true);
361 tabRec->table = table;
362 tabRec->vaddr = mir->offset;
363 u2 width = tabRec->table[1];
364 u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16);
365 tabRec->size = (size * width) + 8;
366
367 oatInsertGrowableList(&cUnit->fillArrayData, (intptr_t)tabRec);
368
369 // Making a call - use explicit registers
370 oatFlushAllRegs(cUnit); /* Everything to home location */
371 loadValueDirectFixed(cUnit, rlSrc, r0);
372 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700373 OFFSETOF_MEMBER(Thread, pHandleFillArrayDataFromCode), rLR);
buzbeee6d61962011-08-27 11:58:19 -0700374 // Materialize a pointer to the fill data image
buzbee03fa2632011-09-20 17:10:57 -0700375 newLIR3(cUnit, kThumb2Adr, r1, 0, (intptr_t)tabRec);
Ian Rogersff1ed472011-09-20 13:46:24 -0700376 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700377}
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;
buzbee6181f792011-09-29 11:14:04 -0700409 oatFlushAllRegs(cUnit);
buzbee34cd9e52011-09-08 14:31:52 -0700410 LOG(INFO) << "Field " << fieldNameFromIndex(cUnit->method, fieldIdx)
411 << " unresolved at compile time";
412 oatLockCallTemps(cUnit); // Explicit register usage
413 loadCurrMethodDirect(cUnit, r1); // arg1 <= Method*
414 loadWordDisp(cUnit, r1,
415 Method::DexCacheResolvedFieldsOffset().Int32Value(), r0);
416 loadWordDisp(cUnit, r0, art::Array::DataOffset().Int32Value() +
417 sizeof(int32_t*)* fieldIdx, r0);
418 /*
419 * For testing, omit the test for run-time resolution. This will
420 * force all accesses to go through the runtime resolution path.
421 */
422#ifndef EXERCISE_SLOWEST_FIELD_PATH
423 ArmLIR* branchOver = genCmpImmBranch(cUnit, kArmCondNe, r0, 0);
424#endif
425 // Resolve
426 loadWordDisp(cUnit, rSELF,
Brian Carlstrom845490b2011-09-19 15:56:53 -0700427 OFFSETOF_MEMBER(Thread, pFindInstanceFieldFromCode), rLR);
buzbee34cd9e52011-09-08 14:31:52 -0700428 loadConstant(cUnit, r0, fieldIdx);
Ian Rogersff1ed472011-09-20 13:46:24 -0700429 callRuntimeHelper(cUnit, rLR); // resolveTypeFromCode(idx, method)
buzbee34cd9e52011-09-08 14:31:52 -0700430 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
431 target->defMask = ENCODE_ALL;
432#ifndef EXERCISE_SLOWEST_FIELD_PATH
433 branchOver->generic.target = (LIR*)target;
434#endif
435 // Free temps (except for r0)
436 oatFreeTemp(cUnit, r1);
437 oatFreeTemp(cUnit, r2);
438 oatFreeTemp(cUnit, r3);
439 loadWordDisp(cUnit, r0, art::Field::OffsetOffset().Int32Value(), r0);
440}
441
buzbeeed3e9302011-09-23 17:34:19 -0700442STATIC void genIGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -0700443 RegLocation rlDest, RegLocation rlObj)
444{
buzbeec143c552011-08-20 17:38:58 -0700445 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
446 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700447 RegLocation rlResult;
448 RegisterClass regClass = oatRegClassBySize(size);
buzbee34cd9e52011-09-08 14:31:52 -0700449 if (SLOW_FIELD_PATH || fieldPtr == NULL) {
450 getFieldOffset(cUnit, mir);
451 // Field offset in r0
452 rlObj = loadValue(cUnit, rlObj, kCoreReg);
453 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee5ade1d22011-09-09 14:44:52 -0700454 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee34cd9e52011-09-08 14:31:52 -0700455 loadBaseIndexed(cUnit, rlObj.lowReg, r0, rlResult.lowReg, 0, size);
buzbee67bf8852011-08-17 17:51:35 -0700456 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700457 storeValue(cUnit, rlDest, rlResult);
458 } else {
459#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700460 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700461#else
462 bool isVolatile = false;
463#endif
464 int fieldOffset = fieldPtr->GetOffset().Int32Value();
465 rlObj = loadValue(cUnit, rlObj, kCoreReg);
466 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
buzbee5ade1d22011-09-09 14:44:52 -0700467 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee34cd9e52011-09-08 14:31:52 -0700468 loadBaseDisp(cUnit, mir, rlObj.lowReg, fieldOffset, rlResult.lowReg,
469 size, rlObj.sRegLow);
470 if (isVolatile) {
471 oatGenMemBarrier(cUnit, kSY);
472 }
473 storeValue(cUnit, rlDest, rlResult);
buzbee67bf8852011-08-17 17:51:35 -0700474 }
buzbee67bf8852011-08-17 17:51:35 -0700475}
476
buzbeeed3e9302011-09-23 17:34:19 -0700477STATIC void genIPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -0700478 RegLocation rlSrc, RegLocation rlObj, bool isObject)
479{
buzbeec143c552011-08-20 17:38:58 -0700480 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
481 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700482 RegisterClass regClass = oatRegClassBySize(size);
buzbee34cd9e52011-09-08 14:31:52 -0700483 if (SLOW_FIELD_PATH || fieldPtr == NULL) {
484 getFieldOffset(cUnit, mir);
485 // Field offset in r0
486 rlObj = loadValue(cUnit, rlObj, kCoreReg);
487 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee5ade1d22011-09-09 14:44:52 -0700488 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null object? */
buzbee67bf8852011-08-17 17:51:35 -0700489 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700490 storeBaseIndexed(cUnit, rlObj.lowReg, r0, rlSrc.lowReg, 0, size);
491 } else {
492#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700493 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700494#else
495 bool isVolatile = false;
496#endif
497 int fieldOffset = fieldPtr->GetOffset().Int32Value();
498 rlObj = loadValue(cUnit, rlObj, kCoreReg);
499 rlSrc = loadValue(cUnit, rlSrc, regClass);
buzbee5ade1d22011-09-09 14:44:52 -0700500 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700501
502 if (isVolatile) {
503 oatGenMemBarrier(cUnit, kSY);
504 }
505 storeBaseDisp(cUnit, rlObj.lowReg, fieldOffset, rlSrc.lowReg, size);
buzbee67bf8852011-08-17 17:51:35 -0700506 }
buzbee67bf8852011-08-17 17:51:35 -0700507 if (isObject) {
508 /* NOTE: marking card based on object head */
509 markGCCard(cUnit, rlSrc.lowReg, rlObj.lowReg);
510 }
511}
512
buzbeeed3e9302011-09-23 17:34:19 -0700513STATIC void genIGetWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700514 RegLocation rlObj)
515{
buzbeec143c552011-08-20 17:38:58 -0700516 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
517 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700518 RegLocation rlResult;
buzbee34cd9e52011-09-08 14:31:52 -0700519 if (fieldPtr == NULL) {
520 getFieldOffset(cUnit, mir);
521 // Field offset in r0
522 rlObj = loadValue(cUnit, rlObj, kCoreReg);
523 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
buzbee5ade1d22011-09-09 14:44:52 -0700524 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700525 opRegReg(cUnit, kOpAdd, r0, rlObj.lowReg);
526 loadPair(cUnit, r0, rlResult.lowReg, rlResult.highReg);
buzbee67bf8852011-08-17 17:51:35 -0700527 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700528 storeValue(cUnit, rlDest, rlResult);
529 } else {
530#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700531 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700532#else
533 bool isVolatile = false;
534#endif
535 int fieldOffset = fieldPtr->GetOffset().Int32Value();
536 rlObj = loadValue(cUnit, rlObj, kCoreReg);
537 int regPtr = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700538
buzbeeed3e9302011-09-23 17:34:19 -0700539 DCHECK(rlDest.wide);
buzbee34cd9e52011-09-08 14:31:52 -0700540
buzbee5ade1d22011-09-09 14:44:52 -0700541 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700542 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
543 rlResult = oatEvalLoc(cUnit, rlDest, kAnyReg, true);
544
545 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
546
547 if (isVolatile) {
548 oatGenMemBarrier(cUnit, kSY);
549 }
550
551 oatFreeTemp(cUnit, regPtr);
552 storeValueWide(cUnit, rlDest, rlResult);
553 }
buzbee67bf8852011-08-17 17:51:35 -0700554}
555
buzbeeed3e9302011-09-23 17:34:19 -0700556STATIC void genIPutWide(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc,
buzbee67bf8852011-08-17 17:51:35 -0700557 RegLocation rlObj)
558{
buzbeec143c552011-08-20 17:38:58 -0700559 Field* fieldPtr = cUnit->method->GetDeclaringClass()->GetDexCache()->
560 GetResolvedField(mir->dalvikInsn.vC);
buzbee67bf8852011-08-17 17:51:35 -0700561 if (fieldPtr == NULL) {
buzbee34cd9e52011-09-08 14:31:52 -0700562 getFieldOffset(cUnit, mir);
563 // Field offset in r0
564 rlObj = loadValue(cUnit, rlObj, kCoreReg);
565 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee5ade1d22011-09-09 14:44:52 -0700566 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700567 opRegReg(cUnit, kOpAdd, r0, rlObj.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700568 oatGenMemBarrier(cUnit, kSY);
buzbee34cd9e52011-09-08 14:31:52 -0700569 storePair(cUnit, r0, rlSrc.lowReg, rlSrc.highReg);
570 } else {
571#if ANDROID_SMP != 0
Elliott Hughes1d3f1142011-09-13 12:00:00 -0700572 bool isVolatile = fieldPtr->IsVolatile();
buzbee34cd9e52011-09-08 14:31:52 -0700573#else
574 bool isVolatile = false;
575#endif
576 int fieldOffset = fieldPtr->GetOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -0700577
buzbee34cd9e52011-09-08 14:31:52 -0700578 rlObj = loadValue(cUnit, rlObj, kCoreReg);
579 int regPtr;
580 rlSrc = loadValueWide(cUnit, rlSrc, kAnyReg);
buzbee5ade1d22011-09-09 14:44:52 -0700581 genNullCheck(cUnit, rlObj.sRegLow, rlObj.lowReg, mir);/* null obj? */
buzbee34cd9e52011-09-08 14:31:52 -0700582 regPtr = oatAllocTemp(cUnit);
583 opRegRegImm(cUnit, kOpAdd, regPtr, rlObj.lowReg, fieldOffset);
584
585 if (isVolatile) {
586 oatGenMemBarrier(cUnit, kSY);
587 }
588 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
589
590 oatFreeTemp(cUnit, regPtr);
591 }
buzbee67bf8852011-08-17 17:51:35 -0700592}
593
buzbeeed3e9302011-09-23 17:34:19 -0700594STATIC void genConstClass(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700595 RegLocation rlDest, RegLocation rlSrc)
596{
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700597 art::Class* classPtr = cUnit->method->GetDexCacheResolvedTypes()->
buzbee1b4c8592011-08-31 10:43:51 -0700598 Get(mir->dalvikInsn.vB);
599 int mReg = loadCurrMethod(cUnit);
600 int resReg = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700601 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbee2a475e72011-09-07 17:19:17 -0700602 loadWordDisp(cUnit, mReg, Method::DexCacheResolvedTypesOffset().Int32Value(),
buzbee1b4c8592011-08-31 10:43:51 -0700603 resReg);
604 loadWordDisp(cUnit, resReg, Array::DataOffset().Int32Value() +
605 (sizeof(String*) * mir->dalvikInsn.vB), rlResult.lowReg);
606 if (classPtr != NULL) {
607 // Fast path, we're done - just store result
608 storeValue(cUnit, rlDest, rlResult);
609 } else {
610 // Slow path. Must test at runtime
buzbee6181f792011-09-29 11:14:04 -0700611 oatFlushAllRegs(cUnit);
buzbee1b4c8592011-08-31 10:43:51 -0700612 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, rlResult.lowReg,
613 0);
614 // Resolved, store and hop over following code
615 storeValue(cUnit, rlDest, rlResult);
616 ArmLIR* branch2 = genUnconditionalBranch(cUnit,0);
617 // TUNING: move slow path to end & remove unconditional branch
618 ArmLIR* target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
619 target1->defMask = ENCODE_ALL;
620 // Call out to helper, which will return resolved type in r0
621 loadWordDisp(cUnit, rSELF,
622 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
623 genRegCopy(cUnit, r1, mReg);
624 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700625 callRuntimeHelper(cUnit, rLR);
buzbee1b4c8592011-08-31 10:43:51 -0700626 RegLocation rlResult = oatGetReturn(cUnit);
627 storeValue(cUnit, rlDest, rlResult);
628 // Rejoin code paths
629 ArmLIR* target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
630 target2->defMask = ENCODE_ALL;
631 branch1->generic.target = (LIR*)target1;
632 branch2->generic.target = (LIR*)target2;
633 }
buzbee67bf8852011-08-17 17:51:35 -0700634}
635
buzbeeed3e9302011-09-23 17:34:19 -0700636STATIC void genConstString(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700637 RegLocation rlDest, RegLocation rlSrc)
638{
buzbee1b4c8592011-08-31 10:43:51 -0700639 /* All strings should be available at compile time */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700640 const art::String* str = cUnit->method->GetDexCacheStrings()->
buzbee1b4c8592011-08-31 10:43:51 -0700641 Get(mir->dalvikInsn.vB);
642 DCHECK(str != NULL);
buzbee67bf8852011-08-17 17:51:35 -0700643
buzbee1b4c8592011-08-31 10:43:51 -0700644 int mReg = loadCurrMethod(cUnit);
645 int resReg = oatAllocTemp(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700646 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700647 loadWordDisp(cUnit, mReg, Method::DexCacheStringsOffset().Int32Value(),
buzbee1b4c8592011-08-31 10:43:51 -0700648 resReg);
649 loadWordDisp(cUnit, resReg, Array::DataOffset().Int32Value() +
650 (sizeof(String*) * mir->dalvikInsn.vB), rlResult.lowReg);
buzbee67bf8852011-08-17 17:51:35 -0700651 storeValue(cUnit, rlDest, rlResult);
652}
653
buzbeedfd3d702011-08-28 12:56:51 -0700654/*
655 * Let helper function take care of everything. Will
656 * call Class::NewInstanceFromCode(type_idx, method);
657 */
buzbeeed3e9302011-09-23 17:34:19 -0700658STATIC void genNewInstance(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700659 RegLocation rlDest)
660{
buzbeedfd3d702011-08-28 12:56:51 -0700661 oatFlushAllRegs(cUnit); /* Everything to home location */
buzbee67bf8852011-08-17 17:51:35 -0700662 loadWordDisp(cUnit, rSELF,
Brian Carlstrom1f870082011-08-23 16:02:11 -0700663 OFFSETOF_MEMBER(Thread, pAllocObjectFromCode), rLR);
buzbeedfd3d702011-08-28 12:56:51 -0700664 loadCurrMethodDirect(cUnit, r1); // arg1 <= Method*
665 loadConstant(cUnit, r0, mir->dalvikInsn.vB); // arg0 <- type_id
Ian Rogersff1ed472011-09-20 13:46:24 -0700666 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700667 RegLocation rlResult = oatGetReturn(cUnit);
668 storeValue(cUnit, rlDest, rlResult);
669}
670
671void genThrow(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
672{
buzbee6181f792011-09-29 11:14:04 -0700673 oatFlushAllRegs(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700674 loadWordDisp(cUnit, rSELF,
Ian Rogers67375ac2011-09-14 00:55:44 -0700675 OFFSETOF_MEMBER(Thread, pDeliverException), rLR);
Ian Rogersbdb03912011-09-14 00:55:44 -0700676 loadValueDirectFixed(cUnit, rlSrc, r0); // Get exception object
Ian Rogersff1ed472011-09-20 13:46:24 -0700677 callRuntimeHelper(cUnit, rLR); // art_deliver_exception(exception);
buzbee67bf8852011-08-17 17:51:35 -0700678}
679
buzbeeed3e9302011-09-23 17:34:19 -0700680STATIC void genInstanceof(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700681 RegLocation rlSrc)
682{
buzbee6181f792011-09-29 11:14:04 -0700683 oatFlushAllRegs(cUnit);
buzbee2a475e72011-09-07 17:19:17 -0700684 // May generate a call - use explicit registers
685 oatLockCallTemps(cUnit);
686 art::Class* classPtr = cUnit->method->GetDexCacheResolvedTypes()->
687 Get(mir->dalvikInsn.vC);
688 int classReg = r2; // Fixed usage
689 loadCurrMethodDirect(cUnit, r1); // r1 <= current Method*
690 loadWordDisp(cUnit, r1, Method::DexCacheResolvedTypesOffset().Int32Value(),
691 classReg);
692 loadWordDisp(cUnit, classReg, Array::DataOffset().Int32Value() +
693 (sizeof(String*) * mir->dalvikInsn.vC), classReg);
buzbee67bf8852011-08-17 17:51:35 -0700694 if (classPtr == NULL) {
buzbee2a475e72011-09-07 17:19:17 -0700695 // Generate a runtime test
696 ArmLIR* hopBranch = genCmpImmBranch(cUnit, kArmCondNe, classReg, 0);
697 // Not resolved
698 // Call out to helper, which will return resolved type in r0
699 loadWordDisp(cUnit, rSELF,
700 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
701 loadConstant(cUnit, r0, mir->dalvikInsn.vC);
Ian Rogersff1ed472011-09-20 13:46:24 -0700702 callRuntimeHelper(cUnit, rLR); // resolveTypeFromCode(idx, method)
buzbee2a475e72011-09-07 17:19:17 -0700703 genRegCopy(cUnit, r2, r0); // Align usage with fast path
704 // Rejoin code paths
705 ArmLIR* hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
706 hopTarget->defMask = ENCODE_ALL;
707 hopBranch->generic.target = (LIR*)hopTarget;
buzbee67bf8852011-08-17 17:51:35 -0700708 }
buzbee2a475e72011-09-07 17:19:17 -0700709 // At this point, r2 has class
710 loadValueDirectFixed(cUnit, rlSrc, r3); /* Ref */
buzbee67bf8852011-08-17 17:51:35 -0700711 /* When taken r0 has NULL which can be used for store directly */
Brian Carlstrom5d40f182011-09-26 22:29:18 -0700712 loadConstant(cUnit, r0, 0); /* Assume false */
buzbee2a475e72011-09-07 17:19:17 -0700713 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r3, 0);
714 /* load object->clazz */
buzbeeed3e9302011-09-23 17:34:19 -0700715 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbee2a475e72011-09-07 17:19:17 -0700716 loadWordDisp(cUnit, r3, Object::ClassOffset().Int32Value(), r1);
buzbee67bf8852011-08-17 17:51:35 -0700717 /* r1 now contains object->clazz */
718 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700719 OFFSETOF_MEMBER(Thread, pInstanceofNonTrivialFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -0700720 loadConstant(cUnit, r0, 1); /* Assume true */
721 opRegReg(cUnit, kOpCmp, r1, r2);
722 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondEq);
buzbee2a475e72011-09-07 17:19:17 -0700723 genRegCopy(cUnit, r0, r3);
buzbee67bf8852011-08-17 17:51:35 -0700724 genRegCopy(cUnit, r1, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -0700725 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700726 /* branch target here */
727 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
728 target->defMask = ENCODE_ALL;
buzbee2a475e72011-09-07 17:19:17 -0700729 RegLocation rlResult = oatGetReturn(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700730 storeValue(cUnit, rlDest, rlResult);
731 branch1->generic.target = (LIR*)target;
732 branch2->generic.target = (LIR*)target;
733}
734
buzbeeed3e9302011-09-23 17:34:19 -0700735STATIC void genCheckCast(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700736{
buzbee6181f792011-09-29 11:14:04 -0700737 oatFlushAllRegs(cUnit);
buzbee2a475e72011-09-07 17:19:17 -0700738 // May generate a call - use explicit registers
739 oatLockCallTemps(cUnit);
740 art::Class* classPtr = cUnit->method->GetDexCacheResolvedTypes()->
741 Get(mir->dalvikInsn.vB);
742 int classReg = r2; // Fixed usage
743 loadCurrMethodDirect(cUnit, r1); // r1 <= current Method*
744 loadWordDisp(cUnit, r1, Method::DexCacheResolvedTypesOffset().Int32Value(),
745 classReg);
746 loadWordDisp(cUnit, classReg, Array::DataOffset().Int32Value() +
747 (sizeof(String*) * mir->dalvikInsn.vB), classReg);
buzbee67bf8852011-08-17 17:51:35 -0700748 if (classPtr == NULL) {
buzbee2a475e72011-09-07 17:19:17 -0700749 // Generate a runtime test
750 ArmLIR* hopBranch = genCmpImmBranch(cUnit, kArmCondNe, classReg, 0);
751 // Not resolved
752 // Call out to helper, which will return resolved type in r0
753 loadWordDisp(cUnit, rSELF,
754 OFFSETOF_MEMBER(Thread, pInitializeTypeFromCode), rLR);
755 loadConstant(cUnit, r0, mir->dalvikInsn.vB);
Ian Rogersff1ed472011-09-20 13:46:24 -0700756 callRuntimeHelper(cUnit, rLR); // resolveTypeFromCode(idx, method)
buzbee2a475e72011-09-07 17:19:17 -0700757 genRegCopy(cUnit, r2, r0); // Align usage with fast path
758 // Rejoin code paths
759 ArmLIR* hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
760 hopTarget->defMask = ENCODE_ALL;
761 hopBranch->generic.target = (LIR*)hopTarget;
buzbee67bf8852011-08-17 17:51:35 -0700762 }
buzbee2a475e72011-09-07 17:19:17 -0700763 // At this point, r2 has class
764 loadValueDirectFixed(cUnit, rlSrc, r0); /* Ref */
765 /* Null is OK - continue */
766 ArmLIR* branch1 = genCmpImmBranch(cUnit, kArmCondEq, r0, 0);
767 /* load object->clazz */
buzbeeed3e9302011-09-23 17:34:19 -0700768 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbee2a475e72011-09-07 17:19:17 -0700769 loadWordDisp(cUnit, r0, Object::ClassOffset().Int32Value(), r1);
770 /* r1 now contains object->clazz */
buzbee67bf8852011-08-17 17:51:35 -0700771 loadWordDisp(cUnit, rSELF,
buzbee2a475e72011-09-07 17:19:17 -0700772 OFFSETOF_MEMBER(Thread, pCheckCastFromCode), rLR);
773 opRegReg(cUnit, kOpCmp, r1, r2);
774 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondEq); /* If equal, trivial yes */
775 genRegCopy(cUnit, r0, r1);
776 genRegCopy(cUnit, r1, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -0700777 callRuntimeHelper(cUnit, rLR);
buzbee2a475e72011-09-07 17:19:17 -0700778 /* branch target here */
buzbee67bf8852011-08-17 17:51:35 -0700779 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
780 target->defMask = ENCODE_ALL;
781 branch1->generic.target = (LIR*)target;
782 branch2->generic.target = (LIR*)target;
783}
784
buzbeeed3e9302011-09-23 17:34:19 -0700785STATIC void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700786 RegLocation rlSrc)
787{
788 RegLocation rlResult;
789 rlSrc = loadValue(cUnit, rlSrc, kFPReg);
790 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
791 newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg);
792 storeValue(cUnit, rlDest, rlResult);
793}
794
buzbeeed3e9302011-09-23 17:34:19 -0700795STATIC void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest,
buzbee67bf8852011-08-17 17:51:35 -0700796 RegLocation rlSrc)
797{
798 RegLocation rlResult;
799 rlSrc = loadValueWide(cUnit, rlSrc, kFPReg);
800 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
801 newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg),
802 S2D(rlSrc.lowReg, rlSrc.highReg));
803 storeValueWide(cUnit, rlDest, rlResult);
804}
805
buzbeeed3e9302011-09-23 17:34:19 -0700806STATIC void freeRegLocTemps(CompilationUnit* cUnit, RegLocation rlKeep,
buzbee439c4fa2011-08-27 15:59:07 -0700807 RegLocation rlFree)
buzbee67bf8852011-08-17 17:51:35 -0700808{
buzbee6181f792011-09-29 11:14:04 -0700809 if ((rlFree.lowReg != rlKeep.lowReg) && (rlFree.lowReg != rlKeep.highReg) &&
810 (rlFree.highReg != rlKeep.lowReg) && (rlFree.highReg != rlKeep.highReg)) {
811 // No overlap, free both
buzbee439c4fa2011-08-27 15:59:07 -0700812 oatFreeTemp(cUnit, rlFree.lowReg);
buzbee6181f792011-09-29 11:14:04 -0700813 oatFreeTemp(cUnit, rlFree.highReg);
814 }
buzbee67bf8852011-08-17 17:51:35 -0700815}
816
buzbeeed3e9302011-09-23 17:34:19 -0700817STATIC void genLong3Addr(CompilationUnit* cUnit, MIR* mir, OpKind firstOp,
buzbee67bf8852011-08-17 17:51:35 -0700818 OpKind secondOp, RegLocation rlDest,
819 RegLocation rlSrc1, RegLocation rlSrc2)
820{
buzbee9e0f9b02011-08-24 15:32:46 -0700821 /*
822 * NOTE: This is the one place in the code in which we might have
823 * as many as six live temporary registers. There are 5 in the normal
824 * set for Arm. Until we have spill capabilities, temporarily add
825 * lr to the temp set. It is safe to do this locally, but note that
826 * lr is used explicitly elsewhere in the code generator and cannot
827 * normally be used as a general temp register.
828 */
buzbee67bf8852011-08-17 17:51:35 -0700829 RegLocation rlResult;
buzbee9e0f9b02011-08-24 15:32:46 -0700830 oatMarkTemp(cUnit, rLR); // Add lr to the temp pool
831 oatFreeTemp(cUnit, rLR); // and make it available
buzbee67bf8852011-08-17 17:51:35 -0700832 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
833 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
834 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeec0ecd652011-09-25 18:11:54 -0700835 // The longs may overlap - use intermediate temp if so
836 if (rlResult.lowReg == rlSrc1.highReg) {
buzbeec0ecd652011-09-25 18:11:54 -0700837 int tReg = oatAllocTemp(cUnit);
838 genRegCopy(cUnit, tReg, rlSrc1.highReg);
839 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
840 rlSrc2.lowReg);
841 opRegRegReg(cUnit, secondOp, rlResult.highReg, tReg,
842 rlSrc2.highReg);
843 oatFreeTemp(cUnit, tReg);
844 } else {
845 opRegRegReg(cUnit, firstOp, rlResult.lowReg, rlSrc1.lowReg,
846 rlSrc2.lowReg);
847 opRegRegReg(cUnit, secondOp, rlResult.highReg, rlSrc1.highReg,
848 rlSrc2.highReg);
849 }
buzbee439c4fa2011-08-27 15:59:07 -0700850 /*
851 * NOTE: If rlDest refers to a frame variable in a large frame, the
852 * following storeValueWide might need to allocate a temp register.
853 * To further work around the lack of a spill capability, explicitly
854 * free any temps from rlSrc1 & rlSrc2 that aren't still live in rlResult.
855 * Remove when spill is functional.
856 */
857 freeRegLocTemps(cUnit, rlResult, rlSrc1);
858 freeRegLocTemps(cUnit, rlResult, rlSrc2);
buzbee67bf8852011-08-17 17:51:35 -0700859 storeValueWide(cUnit, rlDest, rlResult);
buzbee9e0f9b02011-08-24 15:32:46 -0700860 oatClobber(cUnit, rLR);
861 oatUnmarkTemp(cUnit, rLR); // Remove lr from the temp pool
buzbee67bf8852011-08-17 17:51:35 -0700862}
863
864void oatInitializeRegAlloc(CompilationUnit* cUnit)
865{
866 int numRegs = sizeof(coreRegs)/sizeof(*coreRegs);
867 int numReserved = sizeof(reservedRegs)/sizeof(*reservedRegs);
868 int numTemps = sizeof(coreTemps)/sizeof(*coreTemps);
869 int numFPRegs = sizeof(fpRegs)/sizeof(*fpRegs);
870 int numFPTemps = sizeof(fpTemps)/sizeof(*fpTemps);
871 RegisterPool *pool = (RegisterPool *)oatNew(sizeof(*pool), true);
872 cUnit->regPool = pool;
873 pool->numCoreRegs = numRegs;
874 pool->coreRegs = (RegisterInfo *)
875 oatNew(numRegs * sizeof(*cUnit->regPool->coreRegs), true);
876 pool->numFPRegs = numFPRegs;
877 pool->FPRegs = (RegisterInfo *)
878 oatNew(numFPRegs * sizeof(*cUnit->regPool->FPRegs), true);
879 oatInitPool(pool->coreRegs, coreRegs, pool->numCoreRegs);
880 oatInitPool(pool->FPRegs, fpRegs, pool->numFPRegs);
881 // Keep special registers from being allocated
882 for (int i = 0; i < numReserved; i++) {
buzbeec0ecd652011-09-25 18:11:54 -0700883 if (NO_SUSPEND && (reservedRegs[i] == rSUSPEND)) {
884 //To measure cost of suspend check
885 continue;
886 }
buzbee67bf8852011-08-17 17:51:35 -0700887 oatMarkInUse(cUnit, reservedRegs[i]);
888 }
889 // Mark temp regs - all others not in use can be used for promotion
890 for (int i = 0; i < numTemps; i++) {
891 oatMarkTemp(cUnit, coreTemps[i]);
892 }
893 for (int i = 0; i < numFPTemps; i++) {
894 oatMarkTemp(cUnit, fpTemps[i]);
895 }
buzbeec0ecd652011-09-25 18:11:54 -0700896 // Construct the alias map.
897 cUnit->phiAliasMap = (int*)oatNew(cUnit->numSSARegs *
898 sizeof(cUnit->phiAliasMap[0]), false);
899 for (int i = 0; i < cUnit->numSSARegs; i++) {
900 cUnit->phiAliasMap[i] = i;
901 }
902 for (MIR* phi = cUnit->phiList; phi; phi = phi->meta.phiNext) {
903 int defReg = phi->ssaRep->defs[0];
904 for (int i = 0; i < phi->ssaRep->numUses; i++) {
905 for (int j = 0; j < cUnit->numSSARegs; j++) {
906 if (cUnit->phiAliasMap[j] == phi->ssaRep->uses[i]) {
907 cUnit->phiAliasMap[j] = defReg;
908 }
909 }
910 }
911 }
buzbee67bf8852011-08-17 17:51:35 -0700912}
913
914/*
915 * Handle simple case (thin lock) inline. If it's complicated, bail
916 * out to the heavyweight lock/unlock routines. We'll use dedicated
917 * registers here in order to be in the right position in case we
918 * to bail to dvm[Lock/Unlock]Object(self, object)
919 *
920 * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object
921 * r1 -> object [arg1 for dvm[Lock/Unlock]Object
922 * r2 -> intial contents of object->lock, later result of strex
923 * r3 -> self->threadId
924 * r12 -> allow to be used by utilities as general temp
925 *
926 * The result of the strex is 0 if we acquire the lock.
927 *
928 * See comments in Sync.c for the layout of the lock word.
929 * Of particular interest to this code is the test for the
930 * simple case - which we handle inline. For monitor enter, the
931 * simple case is thin lock, held by no-one. For monitor exit,
932 * the simple case is thin lock, held by the unlocking thread with
933 * a recurse count of 0.
934 *
935 * A minor complication is that there is a field in the lock word
936 * unrelated to locking: the hash state. This field must be ignored, but
937 * preserved.
938 *
939 */
buzbeeed3e9302011-09-23 17:34:19 -0700940STATIC void genMonitorEnter(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700941 RegLocation rlSrc)
942{
943 ArmLIR* target;
944 ArmLIR* hopTarget;
945 ArmLIR* branch;
946 ArmLIR* hopBranch;
947
948 oatFlushAllRegs(cUnit);
Elliott Hughes5f791332011-09-15 17:45:30 -0700949 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700950 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700951 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee5ade1d22011-09-09 14:44:52 -0700952 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700953 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -0700954 newLIR3(cUnit, kThumb2Ldrex, r2, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700955 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
buzbeec143c552011-08-20 17:38:58 -0700956 // Align owner
Elliott Hughes5f791332011-09-15 17:45:30 -0700957 opRegImm(cUnit, kOpLsl, r3, LW_LOCK_OWNER_SHIFT);
buzbee67bf8852011-08-17 17:51:35 -0700958 // Is lock unheld on lock or held by us (==threadId) on unlock?
Elliott Hughes5f791332011-09-15 17:45:30 -0700959 newLIR4(cUnit, kThumb2Bfi, r3, r2, 0, LW_LOCK_OWNER_SHIFT - 1);
960 newLIR3(cUnit, kThumb2Bfc, r2, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee67bf8852011-08-17 17:51:35 -0700961 hopBranch = newLIR2(cUnit, kThumb2Cbnz, r2, 0);
buzbeec143c552011-08-20 17:38:58 -0700962 newLIR4(cUnit, kThumb2Strex, r2, r3, r1,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700963 Object::MonitorOffset().Int32Value() >> 2);
buzbee67bf8852011-08-17 17:51:35 -0700964 oatGenMemBarrier(cUnit, kSY);
965 branch = newLIR2(cUnit, kThumb2Cbz, r2, 0);
966
967 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
968 hopTarget->defMask = ENCODE_ALL;
969 hopBranch->generic.target = (LIR*)hopTarget;
970
buzbee1b4c8592011-08-31 10:43:51 -0700971 // Go expensive route - artLockObjectFromCode(self, obj);
972 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700973 rLR);
974 genRegCopy(cUnit, r0, rSELF);
Ian Rogersff1ed472011-09-20 13:46:24 -0700975 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700976
977 // Resume here
978 target = newLIR0(cUnit, kArmPseudoTargetLabel);
979 target->defMask = ENCODE_ALL;
980 branch->generic.target = (LIR*)target;
981}
982
983/*
984 * For monitor unlock, we don't have to use ldrex/strex. Once
985 * we've determined that the lock is thin and that we own it with
986 * a zero recursion count, it's safe to punch it back to the
987 * initial, unlock thin state with a store word.
988 */
buzbeeed3e9302011-09-23 17:34:19 -0700989STATIC void genMonitorExit(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -0700990 RegLocation rlSrc)
991{
992 ArmLIR* target;
993 ArmLIR* branch;
994 ArmLIR* hopTarget;
995 ArmLIR* hopBranch;
996
Elliott Hughes5f791332011-09-15 17:45:30 -0700997 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700998 oatFlushAllRegs(cUnit);
999 loadValueDirectFixed(cUnit, rlSrc, r1); // Get obj
buzbee2e748f32011-08-29 21:02:19 -07001000 oatLockCallTemps(cUnit); // Prepare for explicit register usage
buzbee5ade1d22011-09-09 14:44:52 -07001001 genNullCheck(cUnit, rlSrc.sRegLow, r1, mir);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001002 loadWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r2); // Get lock
Elliott Hughes54e7df12011-09-16 11:47:04 -07001003 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r3);
buzbee67bf8852011-08-17 17:51:35 -07001004 // Is lock unheld on lock or held by us (==threadId) on unlock?
Elliott Hughes5f791332011-09-15 17:45:30 -07001005 opRegRegImm(cUnit, kOpAnd, r12, r2, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
buzbeec143c552011-08-20 17:38:58 -07001006 // Align owner
Elliott Hughes5f791332011-09-15 17:45:30 -07001007 opRegImm(cUnit, kOpLsl, r3, LW_LOCK_OWNER_SHIFT);
1008 newLIR3(cUnit, kThumb2Bfc, r2, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee67bf8852011-08-17 17:51:35 -07001009 opRegReg(cUnit, kOpSub, r2, r3);
1010 hopBranch = opCondBranch(cUnit, kArmCondNe);
1011 oatGenMemBarrier(cUnit, kSY);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001012 storeWordDisp(cUnit, r1, Object::MonitorOffset().Int32Value(), r12);
buzbee67bf8852011-08-17 17:51:35 -07001013 branch = opNone(cUnit, kOpUncondBr);
1014
1015 hopTarget = newLIR0(cUnit, kArmPseudoTargetLabel);
1016 hopTarget->defMask = ENCODE_ALL;
1017 hopBranch->generic.target = (LIR*)hopTarget;
1018
buzbee1b4c8592011-08-31 10:43:51 -07001019 // Go expensive route - UnlockObjectFromCode(self, obj);
1020 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -07001021 rLR);
1022 genRegCopy(cUnit, r0, rSELF);
Ian Rogersff1ed472011-09-20 13:46:24 -07001023 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001024
1025 // Resume here
1026 target = newLIR0(cUnit, kArmPseudoTargetLabel);
1027 target->defMask = ENCODE_ALL;
1028 branch->generic.target = (LIR*)target;
1029}
1030
1031/*
1032 * 64-bit 3way compare function.
1033 * mov rX, #-1
1034 * cmp op1hi, op2hi
1035 * blt done
1036 * bgt flip
1037 * sub rX, op1lo, op2lo (treat as unsigned)
1038 * beq done
1039 * ite hi
1040 * mov(hi) rX, #-1
1041 * mov(!hi) rX, #1
1042 * flip:
1043 * neg rX
1044 * done:
1045 */
buzbeeed3e9302011-09-23 17:34:19 -07001046STATIC void genCmpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001047 RegLocation rlDest, RegLocation rlSrc1,
1048 RegLocation rlSrc2)
1049{
buzbee67bf8852011-08-17 17:51:35 -07001050 ArmLIR* target1;
1051 ArmLIR* target2;
1052 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
1053 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
buzbeeb29e4d12011-09-26 15:05:48 -07001054 int tReg = oatAllocTemp(cUnit);
1055 loadConstant(cUnit, tReg, -1);
buzbee67bf8852011-08-17 17:51:35 -07001056 opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg);
1057 ArmLIR* branch1 = opCondBranch(cUnit, kArmCondLt);
1058 ArmLIR* branch2 = opCondBranch(cUnit, kArmCondGt);
buzbeeb29e4d12011-09-26 15:05:48 -07001059 opRegRegReg(cUnit, kOpSub, tReg, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001060 ArmLIR* branch3 = opCondBranch(cUnit, kArmCondEq);
1061
1062 genIT(cUnit, kArmCondHi, "E");
buzbeeb29e4d12011-09-26 15:05:48 -07001063 newLIR2(cUnit, kThumb2MovImmShift, tReg, modifiedImmediate(-1));
1064 loadConstant(cUnit, tReg, 1);
buzbee67bf8852011-08-17 17:51:35 -07001065 genBarrier(cUnit);
1066
1067 target2 = newLIR0(cUnit, kArmPseudoTargetLabel);
1068 target2->defMask = -1;
buzbeeb29e4d12011-09-26 15:05:48 -07001069 opRegReg(cUnit, kOpNeg, tReg, tReg);
buzbee67bf8852011-08-17 17:51:35 -07001070
1071 target1 = newLIR0(cUnit, kArmPseudoTargetLabel);
1072 target1->defMask = -1;
1073
buzbeeb29e4d12011-09-26 15:05:48 -07001074 RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change
1075 rlTemp.lowReg = tReg;
buzbee67bf8852011-08-17 17:51:35 -07001076 storeValue(cUnit, rlDest, rlTemp);
buzbeeb29e4d12011-09-26 15:05:48 -07001077 oatFreeTemp(cUnit, tReg);
buzbee67bf8852011-08-17 17:51:35 -07001078
1079 branch1->generic.target = (LIR*)target1;
1080 branch2->generic.target = (LIR*)target2;
1081 branch3->generic.target = branch1->generic.target;
1082}
1083
buzbeeed3e9302011-09-23 17:34:19 -07001084STATIC void genMultiplyByTwoBitMultiplier(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001085 RegLocation rlSrc, RegLocation rlResult, int lit,
1086 int firstBit, int secondBit)
1087{
1088 opRegRegRegShift(cUnit, kOpAdd, rlResult.lowReg, rlSrc.lowReg, rlSrc.lowReg,
1089 encodeShift(kArmLsl, secondBit - firstBit));
1090 if (firstBit != 0) {
1091 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlResult.lowReg, firstBit);
1092 }
1093}
1094
buzbeeed3e9302011-09-23 17:34:19 -07001095STATIC bool genConversionCall(CompilationUnit* cUnit, MIR* mir, int funcOffset,
buzbee67bf8852011-08-17 17:51:35 -07001096 int srcSize, int tgtSize)
1097{
1098 /*
1099 * Don't optimize the register usage since it calls out to support
1100 * functions
1101 */
1102 RegLocation rlSrc;
1103 RegLocation rlDest;
1104 oatFlushAllRegs(cUnit); /* Send everything to home location */
1105 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1106 if (srcSize == 1) {
1107 rlSrc = oatGetSrc(cUnit, mir, 0);
1108 loadValueDirectFixed(cUnit, rlSrc, r0);
1109 } else {
1110 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
1111 loadValueDirectWideFixed(cUnit, rlSrc, r0, r1);
1112 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001113 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001114 if (tgtSize == 1) {
1115 RegLocation rlResult;
1116 rlDest = oatGetDest(cUnit, mir, 0);
1117 rlResult = oatGetReturn(cUnit);
1118 storeValue(cUnit, rlDest, rlResult);
1119 } else {
1120 RegLocation rlResult;
1121 rlDest = oatGetDestWide(cUnit, mir, 0, 1);
1122 rlResult = oatGetReturnWide(cUnit);
1123 storeValueWide(cUnit, rlDest, rlResult);
1124 }
1125 return false;
1126}
1127
buzbeeed3e9302011-09-23 17:34:19 -07001128STATIC bool genArithOpFloatPortable(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001129 RegLocation rlDest, RegLocation rlSrc1,
1130 RegLocation rlSrc2)
1131{
1132 RegLocation rlResult;
1133 int funcOffset;
1134
1135 switch (mir->dalvikInsn.opcode) {
1136 case OP_ADD_FLOAT_2ADDR:
1137 case OP_ADD_FLOAT:
1138 funcOffset = OFFSETOF_MEMBER(Thread, pFadd);
1139 break;
1140 case OP_SUB_FLOAT_2ADDR:
1141 case OP_SUB_FLOAT:
1142 funcOffset = OFFSETOF_MEMBER(Thread, pFsub);
1143 break;
1144 case OP_DIV_FLOAT_2ADDR:
1145 case OP_DIV_FLOAT:
1146 funcOffset = OFFSETOF_MEMBER(Thread, pFdiv);
1147 break;
1148 case OP_MUL_FLOAT_2ADDR:
1149 case OP_MUL_FLOAT:
1150 funcOffset = OFFSETOF_MEMBER(Thread, pFmul);
1151 break;
1152 case OP_REM_FLOAT_2ADDR:
1153 case OP_REM_FLOAT:
1154 funcOffset = OFFSETOF_MEMBER(Thread, pFmodf);
1155 break;
1156 case OP_NEG_FLOAT: {
1157 genNegFloat(cUnit, rlDest, rlSrc1);
1158 return false;
1159 }
1160 default:
1161 return true;
1162 }
1163 oatFlushAllRegs(cUnit); /* Send everything to home location */
1164 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1165 loadValueDirectFixed(cUnit, rlSrc1, r0);
1166 loadValueDirectFixed(cUnit, rlSrc2, r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001167 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001168 rlResult = oatGetReturn(cUnit);
1169 storeValue(cUnit, rlDest, rlResult);
1170 return false;
1171}
1172
buzbeeed3e9302011-09-23 17:34:19 -07001173STATIC bool genArithOpDoublePortable(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001174 RegLocation rlDest, RegLocation rlSrc1,
1175 RegLocation rlSrc2)
1176{
1177 RegLocation rlResult;
1178 int funcOffset;
1179
1180 switch (mir->dalvikInsn.opcode) {
1181 case OP_ADD_DOUBLE_2ADDR:
1182 case OP_ADD_DOUBLE:
1183 funcOffset = OFFSETOF_MEMBER(Thread, pDadd);
1184 break;
1185 case OP_SUB_DOUBLE_2ADDR:
1186 case OP_SUB_DOUBLE:
1187 funcOffset = OFFSETOF_MEMBER(Thread, pDsub);
1188 break;
1189 case OP_DIV_DOUBLE_2ADDR:
1190 case OP_DIV_DOUBLE:
1191 funcOffset = OFFSETOF_MEMBER(Thread, pDdiv);
1192 break;
1193 case OP_MUL_DOUBLE_2ADDR:
1194 case OP_MUL_DOUBLE:
1195 funcOffset = OFFSETOF_MEMBER(Thread, pDmul);
1196 break;
1197 case OP_REM_DOUBLE_2ADDR:
1198 case OP_REM_DOUBLE:
1199 funcOffset = OFFSETOF_MEMBER(Thread, pFmod);
1200 break;
1201 case OP_NEG_DOUBLE: {
1202 genNegDouble(cUnit, rlDest, rlSrc1);
1203 return false;
1204 }
1205 default:
1206 return true;
1207 }
1208 oatFlushAllRegs(cUnit); /* Send everything to home location */
1209 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1210 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1211 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -07001212 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001213 rlResult = oatGetReturnWide(cUnit);
1214 storeValueWide(cUnit, rlDest, rlResult);
1215 return false;
1216}
1217
buzbeeed3e9302011-09-23 17:34:19 -07001218STATIC bool genConversionPortable(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001219{
1220 Opcode opcode = mir->dalvikInsn.opcode;
1221
1222 switch (opcode) {
1223 case OP_INT_TO_FLOAT:
1224 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2f),
1225 1, 1);
1226 case OP_FLOAT_TO_INT:
1227 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2iz),
1228 1, 1);
1229 case OP_DOUBLE_TO_FLOAT:
1230 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2f),
1231 2, 1);
1232 case OP_FLOAT_TO_DOUBLE:
1233 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pF2d),
1234 1, 2);
1235 case OP_INT_TO_DOUBLE:
1236 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pI2d),
1237 1, 2);
1238 case OP_DOUBLE_TO_INT:
1239 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pD2iz),
1240 2, 1);
1241 case OP_FLOAT_TO_LONG:
1242 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001243 pF2l), 1, 2);
buzbee67bf8852011-08-17 17:51:35 -07001244 case OP_LONG_TO_FLOAT:
1245 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2f),
1246 2, 1);
1247 case OP_DOUBLE_TO_LONG:
1248 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread,
buzbee1b4c8592011-08-31 10:43:51 -07001249 pD2l), 2, 2);
buzbee67bf8852011-08-17 17:51:35 -07001250 case OP_LONG_TO_DOUBLE:
1251 return genConversionCall(cUnit, mir, OFFSETOF_MEMBER(Thread, pL2d),
1252 2, 2);
1253 default:
1254 return true;
1255 }
1256 return false;
1257}
1258
1259/* Generate conditional branch instructions */
buzbeeed3e9302011-09-23 17:34:19 -07001260STATIC ArmLIR* genConditionalBranch(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001261 ArmConditionCode cond,
1262 ArmLIR* target)
1263{
1264 ArmLIR* branch = opCondBranch(cUnit, cond);
1265 branch->generic.target = (LIR*) target;
1266 return branch;
1267}
1268
buzbee67bf8852011-08-17 17:51:35 -07001269/*
1270 * Generate array store
1271 *
1272 */
buzbeeed3e9302011-09-23 17:34:19 -07001273STATIC void genArrayObjPut(CompilationUnit* cUnit, MIR* mir,
buzbee1b4c8592011-08-31 10:43:51 -07001274 RegLocation rlArray, RegLocation rlIndex,
1275 RegLocation rlSrc, int scale)
buzbee67bf8852011-08-17 17:51:35 -07001276{
1277 RegisterClass regClass = oatRegClassBySize(kWord);
buzbeec143c552011-08-20 17:38:58 -07001278 int lenOffset = Array::LengthOffset().Int32Value();
1279 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001280
buzbee6181f792011-09-29 11:14:04 -07001281 oatFlushAllRegs(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001282 /* Make sure it's a legal object Put. Use direct regs at first */
1283 loadValueDirectFixed(cUnit, rlArray, r1);
1284 loadValueDirectFixed(cUnit, rlSrc, r0);
1285
1286 /* null array object? */
buzbee43a36422011-09-14 14:00:13 -07001287 genNullCheck(cUnit, rlArray.sRegLow, r1, mir);
buzbee67bf8852011-08-17 17:51:35 -07001288 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -07001289 OFFSETOF_MEMBER(Thread, pCanPutArrayElementFromCode), rLR);
buzbee67bf8852011-08-17 17:51:35 -07001290 /* Get the array's clazz */
Ian Rogers0cfe1fb2011-08-26 03:29:44 -07001291 loadWordDisp(cUnit, r1, Object::ClassOffset().Int32Value(), r1);
Ian Rogersff1ed472011-09-20 13:46:24 -07001292 callRuntimeHelper(cUnit, rLR);
buzbee6181f792011-09-29 11:14:04 -07001293 oatFreeTemp(cUnit, r0);
1294 oatFreeTemp(cUnit, r1);
buzbee67bf8852011-08-17 17:51:35 -07001295
1296 // Now, redo loadValues in case they didn't survive the call
1297
1298 int regPtr;
1299 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1300 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1301
1302 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1303 oatClobber(cUnit, rlArray.lowReg);
1304 regPtr = rlArray.lowReg;
1305 } else {
1306 regPtr = oatAllocTemp(cUnit);
1307 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1308 }
1309
buzbee43a36422011-09-14 14:00:13 -07001310 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001311 int regLen = oatAllocTemp(cUnit);
1312 //NOTE: max live temps(4) here.
1313 /* Get len */
1314 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1315 /* regPtr -> array data */
1316 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001317 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001318 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001319 oatFreeTemp(cUnit, regLen);
1320 } else {
1321 /* regPtr -> array data */
1322 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1323 }
1324 /* at this point, regPtr points to array, 2 live temps */
1325 rlSrc = loadValue(cUnit, rlSrc, regClass);
1326 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1327 scale, kWord);
1328}
1329
1330/*
1331 * Generate array load
1332 */
buzbeeed3e9302011-09-23 17:34:19 -07001333STATIC void genArrayGet(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -07001334 RegLocation rlArray, RegLocation rlIndex,
1335 RegLocation rlDest, int scale)
1336{
1337 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001338 int lenOffset = Array::LengthOffset().Int32Value();
1339 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001340 RegLocation rlResult;
1341 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1342 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1343 int regPtr;
1344
1345 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001346 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001347
1348 regPtr = oatAllocTemp(cUnit);
1349
buzbee43a36422011-09-14 14:00:13 -07001350 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001351 int regLen = oatAllocTemp(cUnit);
1352 /* Get len */
1353 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1354 /* regPtr -> array data */
1355 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001356 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001357 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001358 oatFreeTemp(cUnit, regLen);
1359 } else {
1360 /* regPtr -> array data */
1361 opRegRegImm(cUnit, kOpAdd, regPtr, rlArray.lowReg, dataOffset);
1362 }
buzbeee9a72f62011-09-04 17:59:07 -07001363 oatFreeTemp(cUnit, rlArray.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001364 if ((size == kLong) || (size == kDouble)) {
1365 if (scale) {
1366 int rNewIndex = oatAllocTemp(cUnit);
1367 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1368 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1369 oatFreeTemp(cUnit, rNewIndex);
1370 } else {
1371 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1372 }
buzbeee9a72f62011-09-04 17:59:07 -07001373 oatFreeTemp(cUnit, rlIndex.lowReg);
buzbee67bf8852011-08-17 17:51:35 -07001374 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1375
1376 loadPair(cUnit, regPtr, rlResult.lowReg, rlResult.highReg);
1377
1378 oatFreeTemp(cUnit, regPtr);
1379 storeValueWide(cUnit, rlDest, rlResult);
1380 } else {
1381 rlResult = oatEvalLoc(cUnit, rlDest, regClass, true);
1382
1383 loadBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlResult.lowReg,
1384 scale, size);
1385
1386 oatFreeTemp(cUnit, regPtr);
1387 storeValue(cUnit, rlDest, rlResult);
1388 }
1389}
1390
1391/*
1392 * Generate array store
1393 *
1394 */
buzbeeed3e9302011-09-23 17:34:19 -07001395STATIC void genArrayPut(CompilationUnit* cUnit, MIR* mir, OpSize size,
buzbee67bf8852011-08-17 17:51:35 -07001396 RegLocation rlArray, RegLocation rlIndex,
1397 RegLocation rlSrc, int scale)
1398{
1399 RegisterClass regClass = oatRegClassBySize(size);
buzbeec143c552011-08-20 17:38:58 -07001400 int lenOffset = Array::LengthOffset().Int32Value();
1401 int dataOffset = Array::DataOffset().Int32Value();
buzbee67bf8852011-08-17 17:51:35 -07001402
1403 int regPtr;
1404 rlArray = loadValue(cUnit, rlArray, kCoreReg);
1405 rlIndex = loadValue(cUnit, rlIndex, kCoreReg);
1406
1407 if (oatIsTemp(cUnit, rlArray.lowReg)) {
1408 oatClobber(cUnit, rlArray.lowReg);
1409 regPtr = rlArray.lowReg;
1410 } else {
1411 regPtr = oatAllocTemp(cUnit);
1412 genRegCopy(cUnit, regPtr, rlArray.lowReg);
1413 }
1414
1415 /* null object? */
buzbee43a36422011-09-14 14:00:13 -07001416 genNullCheck(cUnit, rlArray.sRegLow, rlArray.lowReg, mir);
buzbee67bf8852011-08-17 17:51:35 -07001417
buzbee43a36422011-09-14 14:00:13 -07001418 if (!(mir->optimizationFlags & MIR_IGNORE_RANGE_CHECK)) {
buzbee67bf8852011-08-17 17:51:35 -07001419 int regLen = oatAllocTemp(cUnit);
1420 //NOTE: max live temps(4) here.
1421 /* Get len */
1422 loadWordDisp(cUnit, rlArray.lowReg, lenOffset, regLen);
1423 /* regPtr -> array data */
1424 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
buzbeeec5adf32011-09-11 15:25:43 -07001425 genRegRegCheck(cUnit, kArmCondCs, rlIndex.lowReg, regLen, mir,
buzbee5ade1d22011-09-09 14:44:52 -07001426 kArmThrowArrayBounds);
buzbee67bf8852011-08-17 17:51:35 -07001427 oatFreeTemp(cUnit, regLen);
1428 } else {
1429 /* regPtr -> array data */
1430 opRegImm(cUnit, kOpAdd, regPtr, dataOffset);
1431 }
1432 /* at this point, regPtr points to array, 2 live temps */
1433 if ((size == kLong) || (size == kDouble)) {
buzbee5ade1d22011-09-09 14:44:52 -07001434 //TUNING: specific wide routine that can handle fp regs
buzbee67bf8852011-08-17 17:51:35 -07001435 if (scale) {
1436 int rNewIndex = oatAllocTemp(cUnit);
1437 opRegRegImm(cUnit, kOpLsl, rNewIndex, rlIndex.lowReg, scale);
1438 opRegReg(cUnit, kOpAdd, regPtr, rNewIndex);
1439 oatFreeTemp(cUnit, rNewIndex);
1440 } else {
1441 opRegReg(cUnit, kOpAdd, regPtr, rlIndex.lowReg);
1442 }
1443 rlSrc = loadValueWide(cUnit, rlSrc, regClass);
1444
1445 storePair(cUnit, regPtr, rlSrc.lowReg, rlSrc.highReg);
1446
1447 oatFreeTemp(cUnit, regPtr);
1448 } else {
1449 rlSrc = loadValue(cUnit, rlSrc, regClass);
1450
1451 storeBaseIndexed(cUnit, regPtr, rlIndex.lowReg, rlSrc.lowReg,
1452 scale, size);
1453 }
1454}
1455
buzbeeed3e9302011-09-23 17:34:19 -07001456STATIC bool genShiftOpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001457 RegLocation rlDest, RegLocation rlSrc1,
1458 RegLocation rlShift)
1459{
buzbee54330722011-08-23 16:46:55 -07001460 int funcOffset;
buzbee67bf8852011-08-17 17:51:35 -07001461
buzbee67bf8852011-08-17 17:51:35 -07001462 switch( mir->dalvikInsn.opcode) {
1463 case OP_SHL_LONG:
1464 case OP_SHL_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001465 funcOffset = OFFSETOF_MEMBER(Thread, pShlLong);
buzbee67bf8852011-08-17 17:51:35 -07001466 break;
1467 case OP_SHR_LONG:
1468 case OP_SHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001469 funcOffset = OFFSETOF_MEMBER(Thread, pShrLong);
buzbee67bf8852011-08-17 17:51:35 -07001470 break;
1471 case OP_USHR_LONG:
1472 case OP_USHR_LONG_2ADDR:
buzbee54330722011-08-23 16:46:55 -07001473 funcOffset = OFFSETOF_MEMBER(Thread, pUshrLong);
buzbee67bf8852011-08-17 17:51:35 -07001474 break;
1475 default:
buzbee54330722011-08-23 16:46:55 -07001476 LOG(FATAL) << "Unexpected case";
buzbee67bf8852011-08-17 17:51:35 -07001477 return true;
1478 }
buzbee54330722011-08-23 16:46:55 -07001479 oatFlushAllRegs(cUnit); /* Send everything to home location */
1480 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1481 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1482 loadValueDirect(cUnit, rlShift, r2);
Ian Rogersff1ed472011-09-20 13:46:24 -07001483 callRuntimeHelper(cUnit, rLR);
buzbee54330722011-08-23 16:46:55 -07001484 RegLocation rlResult = oatGetReturnWide(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001485 storeValueWide(cUnit, rlDest, rlResult);
1486 return false;
1487}
1488
buzbeeed3e9302011-09-23 17:34:19 -07001489STATIC bool genArithOpLong(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001490 RegLocation rlDest, RegLocation rlSrc1,
1491 RegLocation rlSrc2)
1492{
1493 RegLocation rlResult;
1494 OpKind firstOp = kOpBkpt;
1495 OpKind secondOp = kOpBkpt;
1496 bool callOut = false;
1497 int funcOffset;
1498 int retReg = r0;
1499
1500 switch (mir->dalvikInsn.opcode) {
1501 case OP_NOT_LONG:
1502 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1503 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeeb29e4d12011-09-26 15:05:48 -07001504 // Check for destructive overlap
1505 if (rlResult.lowReg == rlSrc2.highReg) {
1506 int tReg = oatAllocTemp(cUnit);
1507 genRegCopy(cUnit, tReg, rlSrc2.highReg);
1508 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1509 opRegReg(cUnit, kOpMvn, rlResult.highReg, tReg);
1510 oatFreeTemp(cUnit, tReg);
1511 } else {
1512 opRegReg(cUnit, kOpMvn, rlResult.lowReg, rlSrc2.lowReg);
1513 opRegReg(cUnit, kOpMvn, rlResult.highReg, rlSrc2.highReg);
1514 }
buzbee67bf8852011-08-17 17:51:35 -07001515 storeValueWide(cUnit, rlDest, rlResult);
1516 return false;
1517 break;
1518 case OP_ADD_LONG:
1519 case OP_ADD_LONG_2ADDR:
1520 firstOp = kOpAdd;
1521 secondOp = kOpAdc;
1522 break;
1523 case OP_SUB_LONG:
1524 case OP_SUB_LONG_2ADDR:
1525 firstOp = kOpSub;
1526 secondOp = kOpSbc;
1527 break;
1528 case OP_MUL_LONG:
1529 case OP_MUL_LONG_2ADDR:
buzbee439c4fa2011-08-27 15:59:07 -07001530 callOut = true;
1531 retReg = r0;
1532 funcOffset = OFFSETOF_MEMBER(Thread, pLmul);
1533 break;
buzbee67bf8852011-08-17 17:51:35 -07001534 case OP_DIV_LONG:
1535 case OP_DIV_LONG_2ADDR:
1536 callOut = true;
1537 retReg = r0;
1538 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1539 break;
1540 /* NOTE - result is in r2/r3 instead of r0/r1 */
1541 case OP_REM_LONG:
1542 case OP_REM_LONG_2ADDR:
1543 callOut = true;
1544 funcOffset = OFFSETOF_MEMBER(Thread, pLdivmod);
1545 retReg = r2;
1546 break;
1547 case OP_AND_LONG_2ADDR:
1548 case OP_AND_LONG:
1549 firstOp = kOpAnd;
1550 secondOp = kOpAnd;
1551 break;
1552 case OP_OR_LONG:
1553 case OP_OR_LONG_2ADDR:
1554 firstOp = kOpOr;
1555 secondOp = kOpOr;
1556 break;
1557 case OP_XOR_LONG:
1558 case OP_XOR_LONG_2ADDR:
1559 firstOp = kOpXor;
1560 secondOp = kOpXor;
1561 break;
1562 case OP_NEG_LONG: {
buzbee67bf8852011-08-17 17:51:35 -07001563 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
1564 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
buzbeeb29e4d12011-09-26 15:05:48 -07001565 int zReg = oatAllocTemp(cUnit);
1566 loadConstantNoClobber(cUnit, zReg, 0);
1567 // Check for destructive overlap
1568 if (rlResult.lowReg == rlSrc2.highReg) {
1569 int tReg = oatAllocTemp(cUnit);
1570 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1571 zReg, rlSrc2.lowReg);
1572 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1573 zReg, tReg);
1574 oatFreeTemp(cUnit, tReg);
1575 } else {
1576 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1577 zReg, rlSrc2.lowReg);
1578 opRegRegReg(cUnit, kOpSbc, rlResult.highReg,
1579 zReg, rlSrc2.highReg);
1580 }
1581 oatFreeTemp(cUnit, zReg);
buzbee67bf8852011-08-17 17:51:35 -07001582 storeValueWide(cUnit, rlDest, rlResult);
1583 return false;
1584 }
1585 default:
1586 LOG(FATAL) << "Invalid long arith op";
1587 }
1588 if (!callOut) {
1589 genLong3Addr(cUnit, mir, firstOp, secondOp, rlDest, rlSrc1, rlSrc2);
1590 } else {
1591 // Adjust return regs in to handle case of rem returning r2/r3
1592 oatFlushAllRegs(cUnit); /* Send everything to home location */
1593 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1594 loadValueDirectWideFixed(cUnit, rlSrc1, r0, r1);
1595 loadValueDirectWideFixed(cUnit, rlSrc2, r2, r3);
Ian Rogersff1ed472011-09-20 13:46:24 -07001596 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001597 if (retReg == r0)
1598 rlResult = oatGetReturnWide(cUnit);
1599 else
1600 rlResult = oatGetReturnWideAlt(cUnit);
1601 storeValueWide(cUnit, rlDest, rlResult);
1602 }
1603 return false;
1604}
1605
buzbeeed3e9302011-09-23 17:34:19 -07001606STATIC bool genArithOpInt(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001607 RegLocation rlDest, RegLocation rlSrc1,
1608 RegLocation rlSrc2)
1609{
1610 OpKind op = kOpBkpt;
1611 bool callOut = false;
1612 bool checkZero = false;
1613 bool unary = false;
1614 int retReg = r0;
1615 int funcOffset;
1616 RegLocation rlResult;
1617 bool shiftOp = false;
1618
1619 switch (mir->dalvikInsn.opcode) {
1620 case OP_NEG_INT:
1621 op = kOpNeg;
1622 unary = true;
1623 break;
1624 case OP_NOT_INT:
1625 op = kOpMvn;
1626 unary = true;
1627 break;
1628 case OP_ADD_INT:
1629 case OP_ADD_INT_2ADDR:
1630 op = kOpAdd;
1631 break;
1632 case OP_SUB_INT:
1633 case OP_SUB_INT_2ADDR:
1634 op = kOpSub;
1635 break;
1636 case OP_MUL_INT:
1637 case OP_MUL_INT_2ADDR:
1638 op = kOpMul;
1639 break;
1640 case OP_DIV_INT:
1641 case OP_DIV_INT_2ADDR:
1642 callOut = true;
1643 checkZero = true;
1644 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1645 retReg = r0;
1646 break;
1647 /* NOTE: returns in r1 */
1648 case OP_REM_INT:
1649 case OP_REM_INT_2ADDR:
1650 callOut = true;
1651 checkZero = true;
1652 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
1653 retReg = r1;
1654 break;
1655 case OP_AND_INT:
1656 case OP_AND_INT_2ADDR:
1657 op = kOpAnd;
1658 break;
1659 case OP_OR_INT:
1660 case OP_OR_INT_2ADDR:
1661 op = kOpOr;
1662 break;
1663 case OP_XOR_INT:
1664 case OP_XOR_INT_2ADDR:
1665 op = kOpXor;
1666 break;
1667 case OP_SHL_INT:
1668 case OP_SHL_INT_2ADDR:
1669 shiftOp = true;
1670 op = kOpLsl;
1671 break;
1672 case OP_SHR_INT:
1673 case OP_SHR_INT_2ADDR:
1674 shiftOp = true;
1675 op = kOpAsr;
1676 break;
1677 case OP_USHR_INT:
1678 case OP_USHR_INT_2ADDR:
1679 shiftOp = true;
1680 op = kOpLsr;
1681 break;
1682 default:
1683 LOG(FATAL) << "Invalid word arith op: " <<
1684 (int)mir->dalvikInsn.opcode;
1685 }
1686 if (!callOut) {
1687 rlSrc1 = loadValue(cUnit, rlSrc1, kCoreReg);
1688 if (unary) {
1689 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1690 opRegReg(cUnit, op, rlResult.lowReg,
1691 rlSrc1.lowReg);
1692 } else {
1693 rlSrc2 = loadValue(cUnit, rlSrc2, kCoreReg);
1694 if (shiftOp) {
1695 int tReg = oatAllocTemp(cUnit);
1696 opRegRegImm(cUnit, kOpAnd, tReg, rlSrc2.lowReg, 31);
1697 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1698 opRegRegReg(cUnit, op, rlResult.lowReg,
1699 rlSrc1.lowReg, tReg);
1700 oatFreeTemp(cUnit, tReg);
1701 } else {
1702 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1703 opRegRegReg(cUnit, op, rlResult.lowReg,
1704 rlSrc1.lowReg, rlSrc2.lowReg);
1705 }
1706 }
1707 storeValue(cUnit, rlDest, rlResult);
1708 } else {
1709 RegLocation rlResult;
1710 oatFlushAllRegs(cUnit); /* Send everything to home location */
1711 loadValueDirectFixed(cUnit, rlSrc2, r1);
1712 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
1713 loadValueDirectFixed(cUnit, rlSrc1, r0);
1714 if (checkZero) {
buzbee5ade1d22011-09-09 14:44:52 -07001715 genImmedCheck(cUnit, kArmCondEq, r1, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07001716 }
Ian Rogersff1ed472011-09-20 13:46:24 -07001717 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07001718 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 }
buzbee6181f792011-09-29 11:14:04 -07001733 oatFlushAllRegs(cUnit);
buzbeec1f45042011-09-21 16:03:19 -07001734 newLIR2(cUnit, kThumbSubRI8, rSUSPEND, 1);
1735 ArmLIR* branch = opCondBranch(cUnit, kArmCondEq);
1736 ArmLIR* retLab = newLIR0(cUnit, kArmPseudoTargetLabel);
1737 retLab->defMask = ENCODE_ALL;
1738 ArmLIR* target = (ArmLIR*)oatNew(sizeof(ArmLIR), true);
1739 target->generic.dalvikOffset = cUnit->currentDalvikOffset;
1740 target->opcode = kArmPseudoSuspendTarget;
1741 target->operands[0] = (intptr_t)retLab;
1742 target->operands[1] = mir->offset;
1743 branch->generic.target = (LIR*)target;
1744 oatInsertGrowableList(&cUnit->suspendLaunchpads, (intptr_t)target);
1745}
1746
buzbee0d966cf2011-09-08 17:34:58 -07001747/* Check for pending suspend request. */
buzbeeed3e9302011-09-23 17:34:19 -07001748STATIC void genSuspendPoll(CompilationUnit* cUnit, MIR* mir)
buzbee67bf8852011-08-17 17:51:35 -07001749{
buzbeec0ecd652011-09-25 18:11:54 -07001750 if (NO_SUSPEND || mir->optimizationFlags & MIR_IGNORE_SUSPEND_CHECK) {
buzbeec1f45042011-09-21 16:03:19 -07001751 return;
1752 }
buzbee6181f792011-09-29 11:14:04 -07001753 oatFlushAllRegs(cUnit);
buzbee0d966cf2011-09-08 17:34:58 -07001754 oatLockCallTemps(cUnit); // Explicit register usage
1755 int rSuspendCount = r1;
buzbee67bf8852011-08-17 17:51:35 -07001756 ArmLIR* ld;
buzbee0d966cf2011-09-08 17:34:58 -07001757 ld = loadWordDisp(cUnit, rSELF,
1758 art::Thread::SuspendCountOffset().Int32Value(), rSuspendCount);
buzbee67bf8852011-08-17 17:51:35 -07001759 setMemRefType(ld, true /* isLoad */, kMustNotAlias);
buzbee0d966cf2011-09-08 17:34:58 -07001760 loadWordDisp(cUnit, rSELF,
1761 OFFSETOF_MEMBER(Thread, pCheckSuspendFromCode), rLR);
1762 genRegCopy(cUnit, r0, rSELF);
1763 opRegImm(cUnit, kOpCmp, rSuspendCount, 0);
buzbeeb0ebba02011-09-17 10:52:59 -07001764 /*
1765 * FIXME: for efficiency we should use an if-converted suspend
1766 * test here. However, support for IT is a bit weak at the
1767 * moment, and requires knowledge of the exact number of instructions
1768 * to fall in the skip shadow. While the exception mechanism
1769 * remains in flux, use a compare and branch sequence. Once
1770 * things firm up, restore the conditional skip (and perhaps
1771 * fix the utility to handle variable-sized shadows).
1772 */
1773#if 0
buzbee0d966cf2011-09-08 17:34:58 -07001774 genIT(cUnit, kArmCondNe, "");
buzbeeec5adf32011-09-11 15:25:43 -07001775 callUnwindableHelper(cUnit, rLR); // CheckSuspendFromCode(self)
buzbeeb0ebba02011-09-17 10:52:59 -07001776#else
1777 ArmLIR* branch = opCondBranch(cUnit, kArmCondEq);
Ian Rogersff1ed472011-09-20 13:46:24 -07001778 callRuntimeHelper(cUnit, rLR); // CheckSuspendFromCode(self)
buzbeeb0ebba02011-09-17 10:52:59 -07001779 ArmLIR* target = newLIR0(cUnit, kArmPseudoTargetLabel);
1780 target->defMask = ENCODE_ALL;
1781 branch->generic.target = (LIR*)target;
1782#endif
buzbee0d966cf2011-09-08 17:34:58 -07001783 oatFreeCallTemps(cUnit);
buzbee67bf8852011-08-17 17:51:35 -07001784}
1785
1786/*
1787 * The following are the first-level codegen routines that analyze the format
1788 * of each bytecode then either dispatch special purpose codegen routines
1789 * or produce corresponding Thumb instructions directly.
1790 */
1791
buzbeeed3e9302011-09-23 17:34:19 -07001792STATIC bool isPowerOfTwo(int x)
buzbee67bf8852011-08-17 17:51:35 -07001793{
1794 return (x & (x - 1)) == 0;
1795}
1796
1797// Returns true if no more than two bits are set in 'x'.
buzbeeed3e9302011-09-23 17:34:19 -07001798STATIC bool isPopCountLE2(unsigned int x)
buzbee67bf8852011-08-17 17:51:35 -07001799{
1800 x &= x - 1;
1801 return (x & (x - 1)) == 0;
1802}
1803
1804// Returns the index of the lowest set bit in 'x'.
buzbeeed3e9302011-09-23 17:34:19 -07001805STATIC int lowestSetBit(unsigned int x) {
buzbee67bf8852011-08-17 17:51:35 -07001806 int bit_posn = 0;
1807 while ((x & 0xf) == 0) {
1808 bit_posn += 4;
1809 x >>= 4;
1810 }
1811 while ((x & 1) == 0) {
1812 bit_posn++;
1813 x >>= 1;
1814 }
1815 return bit_posn;
1816}
1817
1818// Returns true if it added instructions to 'cUnit' to divide 'rlSrc' by 'lit'
1819// and store the result in 'rlDest'.
buzbeeed3e9302011-09-23 17:34:19 -07001820STATIC bool handleEasyDivide(CompilationUnit* cUnit, Opcode dalvikOpcode,
buzbee67bf8852011-08-17 17:51:35 -07001821 RegLocation rlSrc, RegLocation rlDest, int lit)
1822{
1823 if (lit < 2 || !isPowerOfTwo(lit)) {
1824 return false;
1825 }
1826 int k = lowestSetBit(lit);
1827 if (k >= 30) {
1828 // Avoid special cases.
1829 return false;
1830 }
1831 bool div = (dalvikOpcode == OP_DIV_INT_LIT8 ||
1832 dalvikOpcode == OP_DIV_INT_LIT16);
1833 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1834 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1835 if (div) {
1836 int tReg = oatAllocTemp(cUnit);
1837 if (lit == 2) {
1838 // Division by 2 is by far the most common division by constant.
1839 opRegRegImm(cUnit, kOpLsr, tReg, rlSrc.lowReg, 32 - k);
1840 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1841 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1842 } else {
1843 opRegRegImm(cUnit, kOpAsr, tReg, rlSrc.lowReg, 31);
1844 opRegRegImm(cUnit, kOpLsr, tReg, tReg, 32 - k);
1845 opRegRegReg(cUnit, kOpAdd, tReg, tReg, rlSrc.lowReg);
1846 opRegRegImm(cUnit, kOpAsr, rlResult.lowReg, tReg, k);
1847 }
1848 } else {
1849 int cReg = oatAllocTemp(cUnit);
1850 loadConstant(cUnit, cReg, lit - 1);
1851 int tReg1 = oatAllocTemp(cUnit);
1852 int tReg2 = oatAllocTemp(cUnit);
1853 if (lit == 2) {
1854 opRegRegImm(cUnit, kOpLsr, tReg1, rlSrc.lowReg, 32 - k);
1855 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1856 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1857 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1858 } else {
1859 opRegRegImm(cUnit, kOpAsr, tReg1, rlSrc.lowReg, 31);
1860 opRegRegImm(cUnit, kOpLsr, tReg1, tReg1, 32 - k);
1861 opRegRegReg(cUnit, kOpAdd, tReg2, tReg1, rlSrc.lowReg);
1862 opRegRegReg(cUnit, kOpAnd, tReg2, tReg2, cReg);
1863 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg2, tReg1);
1864 }
1865 }
1866 storeValue(cUnit, rlDest, rlResult);
1867 return true;
1868}
1869
1870// Returns true if it added instructions to 'cUnit' to multiply 'rlSrc' by 'lit'
1871// and store the result in 'rlDest'.
buzbeeed3e9302011-09-23 17:34:19 -07001872STATIC bool handleEasyMultiply(CompilationUnit* cUnit,
buzbee67bf8852011-08-17 17:51:35 -07001873 RegLocation rlSrc, RegLocation rlDest, int lit)
1874{
1875 // Can we simplify this multiplication?
1876 bool powerOfTwo = false;
1877 bool popCountLE2 = false;
1878 bool powerOfTwoMinusOne = false;
1879 if (lit < 2) {
1880 // Avoid special cases.
1881 return false;
1882 } else if (isPowerOfTwo(lit)) {
1883 powerOfTwo = true;
1884 } else if (isPopCountLE2(lit)) {
1885 popCountLE2 = true;
1886 } else if (isPowerOfTwo(lit + 1)) {
1887 powerOfTwoMinusOne = true;
1888 } else {
1889 return false;
1890 }
1891 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1892 RegLocation rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1893 if (powerOfTwo) {
1894 // Shift.
1895 opRegRegImm(cUnit, kOpLsl, rlResult.lowReg, rlSrc.lowReg,
1896 lowestSetBit(lit));
1897 } else if (popCountLE2) {
1898 // Shift and add and shift.
1899 int firstBit = lowestSetBit(lit);
1900 int secondBit = lowestSetBit(lit ^ (1 << firstBit));
1901 genMultiplyByTwoBitMultiplier(cUnit, rlSrc, rlResult, lit,
1902 firstBit, secondBit);
1903 } else {
1904 // Reverse subtract: (src << (shift + 1)) - src.
buzbeeed3e9302011-09-23 17:34:19 -07001905 DCHECK(powerOfTwoMinusOne);
buzbee5ade1d22011-09-09 14:44:52 -07001906 // TUNING: rsb dst, src, src lsl#lowestSetBit(lit + 1)
buzbee67bf8852011-08-17 17:51:35 -07001907 int tReg = oatAllocTemp(cUnit);
1908 opRegRegImm(cUnit, kOpLsl, tReg, rlSrc.lowReg, lowestSetBit(lit + 1));
1909 opRegRegReg(cUnit, kOpSub, rlResult.lowReg, tReg, rlSrc.lowReg);
1910 }
1911 storeValue(cUnit, rlDest, rlResult);
1912 return true;
1913}
1914
buzbeeed3e9302011-09-23 17:34:19 -07001915STATIC bool genArithOpIntLit(CompilationUnit* cUnit, MIR* mir,
buzbee67bf8852011-08-17 17:51:35 -07001916 RegLocation rlDest, RegLocation rlSrc,
1917 int lit)
1918{
1919 Opcode dalvikOpcode = mir->dalvikInsn.opcode;
1920 RegLocation rlResult;
1921 OpKind op = (OpKind)0; /* Make gcc happy */
1922 int shiftOp = false;
1923 bool isDiv = false;
1924 int funcOffset;
1925
1926 switch (dalvikOpcode) {
1927 case OP_RSUB_INT_LIT8:
1928 case OP_RSUB_INT: {
1929 int tReg;
1930 //TUNING: add support for use of Arm rsub op
1931 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
1932 tReg = oatAllocTemp(cUnit);
1933 loadConstant(cUnit, tReg, lit);
1934 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
1935 opRegRegReg(cUnit, kOpSub, rlResult.lowReg,
1936 tReg, rlSrc.lowReg);
1937 storeValue(cUnit, rlDest, rlResult);
1938 return false;
1939 break;
1940 }
1941
1942 case OP_ADD_INT_LIT8:
1943 case OP_ADD_INT_LIT16:
1944 op = kOpAdd;
1945 break;
1946 case OP_MUL_INT_LIT8:
1947 case OP_MUL_INT_LIT16: {
1948 if (handleEasyMultiply(cUnit, rlSrc, rlDest, lit)) {
1949 return false;
1950 }
1951 op = kOpMul;
1952 break;
1953 }
1954 case OP_AND_INT_LIT8:
1955 case OP_AND_INT_LIT16:
1956 op = kOpAnd;
1957 break;
1958 case OP_OR_INT_LIT8:
1959 case OP_OR_INT_LIT16:
1960 op = kOpOr;
1961 break;
1962 case OP_XOR_INT_LIT8:
1963 case OP_XOR_INT_LIT16:
1964 op = kOpXor;
1965 break;
1966 case OP_SHL_INT_LIT8:
1967 lit &= 31;
1968 shiftOp = true;
1969 op = kOpLsl;
1970 break;
1971 case OP_SHR_INT_LIT8:
1972 lit &= 31;
1973 shiftOp = true;
1974 op = kOpAsr;
1975 break;
1976 case OP_USHR_INT_LIT8:
1977 lit &= 31;
1978 shiftOp = true;
1979 op = kOpLsr;
1980 break;
1981
1982 case OP_DIV_INT_LIT8:
1983 case OP_DIV_INT_LIT16:
1984 case OP_REM_INT_LIT8:
1985 case OP_REM_INT_LIT16:
1986 if (lit == 0) {
buzbee5ade1d22011-09-09 14:44:52 -07001987 genImmedCheck(cUnit, kArmCondAl, 0, 0, mir, kArmThrowDivZero);
buzbee67bf8852011-08-17 17:51:35 -07001988 return false;
1989 }
1990 if (handleEasyDivide(cUnit, dalvikOpcode, rlSrc, rlDest, lit)) {
1991 return false;
1992 }
1993 oatFlushAllRegs(cUnit); /* Everything to home location */
1994 loadValueDirectFixed(cUnit, rlSrc, r0);
1995 oatClobber(cUnit, r0);
1996 if ((dalvikOpcode == OP_DIV_INT_LIT8) ||
1997 (dalvikOpcode == OP_DIV_INT_LIT16)) {
1998 funcOffset = OFFSETOF_MEMBER(Thread, pIdiv);
1999 isDiv = true;
2000 } else {
2001 funcOffset = OFFSETOF_MEMBER(Thread, pIdivmod);
2002 isDiv = false;
2003 }
2004 loadWordDisp(cUnit, rSELF, funcOffset, rLR);
2005 loadConstant(cUnit, r1, lit);
Ian Rogersff1ed472011-09-20 13:46:24 -07002006 callRuntimeHelper(cUnit, rLR);
buzbee67bf8852011-08-17 17:51:35 -07002007 if (isDiv)
2008 rlResult = oatGetReturn(cUnit);
2009 else
2010 rlResult = oatGetReturnAlt(cUnit);
2011 storeValue(cUnit, rlDest, rlResult);
2012 return false;
2013 break;
2014 default:
2015 return true;
2016 }
2017 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
2018 rlResult = oatEvalLoc(cUnit, rlDest, kCoreReg, true);
2019 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
2020 if (shiftOp && (lit == 0)) {
2021 genRegCopy(cUnit, rlResult.lowReg, rlSrc.lowReg);
2022 } else {
2023 opRegRegImm(cUnit, op, rlResult.lowReg, rlSrc.lowReg, lit);
2024 }
2025 storeValue(cUnit, rlDest, rlResult);
2026 return false;
2027}
2028
2029/* Architectural-specific debugging helpers go here */
2030void oatArchDump(void)
2031{
2032 /* Print compiled opcode in this VM instance */
2033 int i, start, streak;
2034 char buf[1024];
2035
2036 streak = i = 0;
2037 buf[0] = 0;
2038 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2039 i++;
2040 }
2041 if (i == kNumPackedOpcodes) {
2042 return;
2043 }
2044 for (start = i++, streak = 1; i < kNumPackedOpcodes; i++) {
2045 if (opcodeCoverage[i]) {
2046 streak++;
2047 } else {
2048 if (streak == 1) {
2049 sprintf(buf+strlen(buf), "%x,", start);
2050 } else {
2051 sprintf(buf+strlen(buf), "%x-%x,", start, start + streak - 1);
2052 }
2053 streak = 0;
2054 while (opcodeCoverage[i] == 0 && i < kNumPackedOpcodes) {
2055 i++;
2056 }
2057 if (i < kNumPackedOpcodes) {
2058 streak = 1;
2059 start = i;
2060 }
2061 }
2062 }
2063 if (streak) {
2064 if (streak == 1) {
2065 sprintf(buf+strlen(buf), "%x", start);
2066 } else {
2067 sprintf(buf+strlen(buf), "%x-%x", start, start + streak - 1);
2068 }
2069 }
2070 if (strlen(buf)) {
2071 LOG(INFO) << "dalvik.vm.oat.op = " << buf;
2072 }
2073}