blob: 4319ec874f28d2c0dd887744b187b3a7362a95e6 [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
Logan Chien4dd96f52012-02-29 01:26:58 +080025#include "oat_compilation_unit.h"
26
Elliott Hughes11d1b0c2012-01-23 16:57:47 -080027namespace art {
28
buzbeee62076c2012-03-21 14:26:16 -070029
30/* Return the position of an ssa name within the argument list */
31int inPosition(CompilationUnit* cUnit, int sReg)
buzbee16da88c2012-03-20 10:38:17 -070032{
buzbeee62076c2012-03-21 14:26:16 -070033 int vReg = SRegToVReg(cUnit, sReg);
34 return vReg - cUnit->numRegs;
35}
36
37/*
38 * Describe an argument. If it's already in an arg register, just leave it
39 * there. NOTE: all live arg registers must be locked prior to this call
40 * to avoid having them allocated as a temp by downstream utilities.
41 */
42RegLocation argLoc(CompilationUnit* cUnit, RegLocation loc)
43{
44 int argNum = inPosition(cUnit, loc.sRegLow);
buzbee16da88c2012-03-20 10:38:17 -070045 if (loc.wide) {
buzbeee62076c2012-03-21 14:26:16 -070046 if (argNum == 2) {
47 // Bad case - half in register, half in frame. Just punt
48 loc.location = kLocInvalid;
49 } else if (argNum < 2) {
50 loc.lowReg = rARG1 + argNum;
51 loc.highReg = loc.lowReg + 1;
52 loc.location = kLocPhysReg;
53 } else {
54 loc.location = kLocDalvikFrame;
55 }
buzbee16da88c2012-03-20 10:38:17 -070056 } else {
buzbeee62076c2012-03-21 14:26:16 -070057 if (argNum < 3) {
58 loc.lowReg = rARG1 + argNum;
59 loc.location = kLocPhysReg;
60 } else {
61 loc.location = kLocDalvikFrame;
62 }
buzbee16da88c2012-03-20 10:38:17 -070063 }
64 return loc;
65}
66
buzbeee62076c2012-03-21 14:26:16 -070067/*
68 * Load an argument. If already in a register, just return. If in
69 * the frame, we can't use the normal loadValue() because it assumed
70 * a proper frame - and we're frameless.
71 */
72RegLocation loadArg(CompilationUnit* cUnit, RegLocation loc)
73{
74 if (loc.location == kLocDalvikFrame) {
75 int start = (inPosition(cUnit, loc.sRegLow) + 1) * sizeof(uint32_t);
76 loc.lowReg = oatAllocTemp(cUnit);
77 loadWordDisp(cUnit, rSP, start, loc.lowReg);
78 if (loc.wide) {
79 loc.highReg = oatAllocTemp(cUnit);
80 loadWordDisp(cUnit, rSP, start + sizeof(uint32_t), loc.highReg);
81 }
82 loc.location = kLocPhysReg;
83 }
84 return loc;
85}
86
87/* Lock any referenced arguments that arrive in registers */
88void lockLiveArgs(CompilationUnit* cUnit, MIR* mir)
89{
90 int firstIn = cUnit->numRegs;
91 const int numArgRegs = 3; // TODO: generalize & move to RegUtil.cc
92 for (int i = 0; i < mir->ssaRep->numUses; i++) {
93 int vReg = SRegToVReg(cUnit, mir->ssaRep->uses[i]);
94 int inPosition = vReg - firstIn;
95 if (inPosition < numArgRegs) {
96 oatLockTemp(cUnit, rARG1 + inPosition);
97 }
98 }
99}
100
buzbee16da88c2012-03-20 10:38:17 -0700101/* Find the next MIR, which may be in a following basic block */
102MIR* getNextMir(CompilationUnit* cUnit, BasicBlock** pBb, MIR* mir)
103{
104 BasicBlock* bb = *pBb;
105 MIR* origMir = mir;
106 while (bb != NULL) {
107 if (mir != NULL) {
108 mir = mir->next;
109 }
110 if (mir != NULL) {
111 return mir;
112 } else {
113 bb = bb->fallThrough;
114 *pBb = bb;
115 if (bb) {
116 mir = bb->firstMIRInsn;
117 if (mir != NULL) {
118 return mir;
119 }
120 }
121 }
122 }
123 return origMir;
124}
125
126/* Used for the "printMe" listing */
127void genPrintLabel(CompilationUnit *cUnit, MIR* mir)
128{
129 LIR* boundaryLIR;
130 /* Mark the beginning of a Dalvik instruction for line tracking */
131 char* instStr = cUnit->printMe ?
132 oatGetDalvikDisassembly(cUnit, mir->dalvikInsn, "") : NULL;
133 boundaryLIR = newLIR1(cUnit, kPseudoDalvikByteCodeBoundary,
134 (intptr_t) instStr);
135 cUnit->boundaryMap.insert(std::make_pair(mir->offset,
136 (LIR*)boundaryLIR));
137 /* Don't generate the SSA annotation unless verbose mode is on */
138 if (cUnit->printMe && mir->ssaRep) {
139 char* ssaString = oatGetSSAString(cUnit, mir->ssaRep);
140 newLIR1(cUnit, kPseudoSSARep, (int) ssaString);
141 }
142}
143
144MIR* specialIGet(CompilationUnit* cUnit, BasicBlock** bb, MIR* mir,
145 OpSize size, bool longOrDouble, bool isObject)
146{
147 int fieldOffset;
148 bool isVolatile;
149 uint32_t fieldIdx = mir->dalvikInsn.vC;
150 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
151 false);
buzbee97df07f2012-03-27 16:13:20 -0700152 if (!fastPath || !(mir->optimizationFlags & MIR_IGNORE_NULL_CHECK)) {
buzbee16da88c2012-03-20 10:38:17 -0700153 return NULL;
154 }
buzbee16da88c2012-03-20 10:38:17 -0700155 RegLocation rlObj = oatGetSrc(cUnit, mir, 0);
buzbeee62076c2012-03-21 14:26:16 -0700156 lockLiveArgs(cUnit, mir);
157 rlObj = argLoc(cUnit, rlObj);
buzbee16da88c2012-03-20 10:38:17 -0700158 RegLocation rlDest;
159 if (longOrDouble) {
160 rlDest = oatGetReturnWide(cUnit, false);
161 } else {
162 rlDest = oatGetReturn(cUnit, false);
163 }
buzbeee62076c2012-03-21 14:26:16 -0700164 // Point of no return - no aborts after this
buzbeee62076c2012-03-21 14:26:16 -0700165 genPrintLabel(cUnit, mir);
166 rlObj = loadArg(cUnit, rlObj);
buzbee16da88c2012-03-20 10:38:17 -0700167 genIGet(cUnit, mir, size, rlDest, rlObj, longOrDouble, isObject);
168 return getNextMir(cUnit, bb, mir);
169}
170
171MIR* specialIPut(CompilationUnit* cUnit, BasicBlock** bb, MIR* mir,
172 OpSize size, bool longOrDouble, bool isObject)
173{
174 int fieldOffset;
175 bool isVolatile;
176 uint32_t fieldIdx = mir->dalvikInsn.vC;
177 bool fastPath = fastInstance(cUnit, fieldIdx, fieldOffset, isVolatile,
178 false);
buzbee97df07f2012-03-27 16:13:20 -0700179 if (!fastPath || !(mir->optimizationFlags & MIR_IGNORE_NULL_CHECK)) {
buzbee16da88c2012-03-20 10:38:17 -0700180 return NULL;
181 }
buzbee16da88c2012-03-20 10:38:17 -0700182 RegLocation rlSrc;
183 RegLocation rlObj;
buzbeee62076c2012-03-21 14:26:16 -0700184 lockLiveArgs(cUnit, mir);
buzbee16da88c2012-03-20 10:38:17 -0700185 if (longOrDouble) {
186 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
187 rlObj = oatGetSrc(cUnit, mir, 2);
buzbee16da88c2012-03-20 10:38:17 -0700188 } else {
189 rlSrc = oatGetSrc(cUnit, mir, 0);
190 rlObj = oatGetSrc(cUnit, mir, 1);
buzbee16da88c2012-03-20 10:38:17 -0700191 }
buzbeee62076c2012-03-21 14:26:16 -0700192 rlSrc = argLoc(cUnit, rlSrc);
193 rlObj = argLoc(cUnit, rlObj);
buzbee97df07f2012-03-27 16:13:20 -0700194 // Reject if source is split across registers & frame
195 if (rlObj.location == kLocInvalid) {
buzbeee62076c2012-03-21 14:26:16 -0700196 oatResetRegPool(cUnit);
197 return NULL;
198 }
199 // Point of no return - no aborts after this
buzbeee62076c2012-03-21 14:26:16 -0700200 genPrintLabel(cUnit, mir);
201 rlObj = loadArg(cUnit, rlObj);
202 rlSrc = loadArg(cUnit, rlSrc);
buzbee16da88c2012-03-20 10:38:17 -0700203 genIPut(cUnit, mir, size, rlSrc, rlObj, longOrDouble, isObject);
204 return getNextMir(cUnit, bb, mir);
205}
206
buzbeee62076c2012-03-21 14:26:16 -0700207MIR* specialIdentity(CompilationUnit* cUnit, MIR* mir)
208{
209 RegLocation rlSrc;
210 RegLocation rlDest;
211 bool wide = (mir->ssaRep->numUses == 2);
212 if (wide) {
213 rlSrc = oatGetSrcWide(cUnit, mir, 0, 1);
214 rlDest = oatGetReturnWide(cUnit, false);
215 } else {
216 rlSrc = oatGetSrc(cUnit, mir, 0);
217 rlDest = oatGetReturn(cUnit, false);
218 }
219 lockLiveArgs(cUnit, mir);
220 rlSrc = argLoc(cUnit, rlSrc);
221 if (rlSrc.location == kLocInvalid) {
222 oatResetRegPool(cUnit);
223 return NULL;
224 }
225 // Point of no return - no aborts after this
226 genPrintLabel(cUnit, mir);
227 rlSrc = loadArg(cUnit, rlSrc);
228 if (wide) {
229 storeValueWide(cUnit, rlDest, rlSrc);
230 } else {
231 storeValue(cUnit, rlDest, rlSrc);
232 }
233 return mir;
234}
235
buzbee16da88c2012-03-20 10:38:17 -0700236/*
237 * Special-case code genration for simple non-throwing leaf methods.
238 */
239void genSpecialCase(CompilationUnit* cUnit, BasicBlock* bb, MIR* mir,
240 SpecialCaseHandler specialCase)
241{
242 cUnit->currentDalvikOffset = mir->offset;
243 MIR* nextMir = NULL;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700244 switch (specialCase) {
buzbee16da88c2012-03-20 10:38:17 -0700245 case kNullMethod:
246 DCHECK(mir->dalvikInsn.opcode == Instruction::RETURN_VOID);
247 nextMir = mir;
248 break;
249 case kConstFunction:
250 genPrintLabel(cUnit, mir);
251 loadConstant(cUnit, rRET0, mir->dalvikInsn.vB);
252 nextMir = getNextMir(cUnit, &bb, mir);
253 break;
254 case kIGet:
255 nextMir = specialIGet(cUnit, &bb, mir, kWord, false, false);
buzbeee62076c2012-03-21 14:26:16 -0700256 break;
buzbee16da88c2012-03-20 10:38:17 -0700257 case kIGetBoolean:
258 case kIGetByte:
259 nextMir = specialIGet(cUnit, &bb, mir, kUnsignedByte, false, false);
buzbeee62076c2012-03-21 14:26:16 -0700260 break;
buzbee16da88c2012-03-20 10:38:17 -0700261 case kIGetObject:
262 nextMir = specialIGet(cUnit, &bb, mir, kWord, false, true);
buzbeee62076c2012-03-21 14:26:16 -0700263 break;
buzbee16da88c2012-03-20 10:38:17 -0700264 case kIGetChar:
265 nextMir = specialIGet(cUnit, &bb, mir, kUnsignedHalf, false, false);
buzbeee62076c2012-03-21 14:26:16 -0700266 break;
buzbee16da88c2012-03-20 10:38:17 -0700267 case kIGetShort:
268 nextMir = specialIGet(cUnit, &bb, mir, kSignedHalf, false, false);
buzbeee62076c2012-03-21 14:26:16 -0700269 break;
buzbee16da88c2012-03-20 10:38:17 -0700270 case kIGetWide:
271 nextMir = specialIGet(cUnit, &bb, mir, kLong, true, false);
buzbeee62076c2012-03-21 14:26:16 -0700272 break;
buzbee16da88c2012-03-20 10:38:17 -0700273 case kIPut:
274 nextMir = specialIPut(cUnit, &bb, mir, kWord, false, false);
buzbeee62076c2012-03-21 14:26:16 -0700275 break;
buzbee16da88c2012-03-20 10:38:17 -0700276 case kIPutBoolean:
277 case kIPutByte:
278 nextMir = specialIPut(cUnit, &bb, mir, kUnsignedByte, false, false);
buzbeee62076c2012-03-21 14:26:16 -0700279 break;
buzbee16da88c2012-03-20 10:38:17 -0700280 case kIPutObject:
281 nextMir = specialIPut(cUnit, &bb, mir, kWord, false, true);
buzbeee62076c2012-03-21 14:26:16 -0700282 break;
buzbee16da88c2012-03-20 10:38:17 -0700283 case kIPutChar:
284 nextMir = specialIPut(cUnit, &bb, mir, kUnsignedHalf, false, false);
buzbeee62076c2012-03-21 14:26:16 -0700285 break;
buzbee16da88c2012-03-20 10:38:17 -0700286 case kIPutShort:
287 nextMir = specialIPut(cUnit, &bb, mir, kSignedHalf, false, false);
buzbeee62076c2012-03-21 14:26:16 -0700288 break;
buzbee16da88c2012-03-20 10:38:17 -0700289 case kIPutWide:
290 nextMir = specialIPut(cUnit, &bb, mir, kLong, true, false);
buzbeee62076c2012-03-21 14:26:16 -0700291 break;
292 case kIdentity:
293 nextMir = specialIdentity(cUnit, mir);
294 break;
buzbee16da88c2012-03-20 10:38:17 -0700295 default:
296 return;
297 }
298 if (nextMir != NULL) {
299 cUnit->currentDalvikOffset = nextMir->offset;
buzbeee62076c2012-03-21 14:26:16 -0700300 if (specialCase != kIdentity) {
301 genPrintLabel(cUnit, nextMir);
302 }
buzbee16da88c2012-03-20 10:38:17 -0700303 newLIR1(cUnit, kThumbBx, rLR);
304 cUnit->coreSpillMask = 0;
305 cUnit->numCoreSpills = 0;
306 cUnit->fpSpillMask = 0;
307 cUnit->numFPSpills = 0;
308 cUnit->frameSize = 0;
309 cUnit->coreVmapTable.clear();
310 cUnit->fpVmapTable.clear();
311 }
312}
buzbee67bf8852011-08-17 17:51:35 -0700313
314/*
315 * Generate a Thumb2 IT instruction, which can nullify up to
316 * four subsequent instructions based on a condition and its
317 * inverse. The condition applies to the first instruction, which
318 * is executed if the condition is met. The string "guide" consists
319 * of 0 to 3 chars, and applies to the 2nd through 4th instruction.
320 * A "T" means the instruction is executed if the condition is
321 * met, and an "E" means the instruction is executed if the condition
322 * is not met.
323 */
buzbee82488f52012-03-02 08:20:26 -0800324LIR* opIT(CompilationUnit* cUnit, ArmConditionCode code, const char* guide)
buzbee67bf8852011-08-17 17:51:35 -0700325{
326 int mask;
327 int condBit = code & 1;
328 int altBit = condBit ^ 1;
329 int mask3 = 0;
330 int mask2 = 0;
331 int mask1 = 0;
332
333 //Note: case fallthroughs intentional
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700334 switch (strlen(guide)) {
buzbee67bf8852011-08-17 17:51:35 -0700335 case 3:
336 mask1 = (guide[2] == 'T') ? condBit : altBit;
337 case 2:
338 mask2 = (guide[1] == 'T') ? condBit : altBit;
339 case 1:
340 mask3 = (guide[0] == 'T') ? condBit : altBit;
341 break;
342 case 0:
343 break;
344 default:
buzbee82488f52012-03-02 08:20:26 -0800345 LOG(FATAL) << "OAT: bad case in opIT";
buzbee67bf8852011-08-17 17:51:35 -0700346 }
347 mask = (mask3 << 3) | (mask2 << 2) | (mask1 << 1) |
348 (1 << (3 - strlen(guide)));
349 return newLIR2(cUnit, kThumb2It, code, mask);
350}
351
352/*
buzbee67bf8852011-08-17 17:51:35 -0700353 * The sparse table in the literal pool is an array of <key,displacement>
354 * pairs. For each set, we'll load them as a pair using ldmia.
355 * This means that the register number of the temp we use for the key
356 * must be lower than the reg for the displacement.
357 *
358 * The test loop will look something like:
359 *
360 * adr rBase, <table>
361 * ldr rVal, [rSP, vRegOff]
362 * mov rIdx, #tableSize
363 * lp:
364 * ldmia rBase!, {rKey, rDisp}
365 * sub rIdx, #1
366 * cmp rVal, rKey
367 * ifeq
368 * add rPC, rDisp ; This is the branch from which we compute displacement
369 * cbnz rIdx, lp
370 */
buzbee31a4a6f2012-02-28 15:36:15 -0800371void genSparseSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700372{
373 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
374 if (cUnit->printMe) {
375 dumpSparseSwitchTable(table);
376 }
377 // Add the table to the list - we'll process it later
buzbeeba938cb2012-02-03 14:47:55 -0800378 SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable),
buzbee5abfa3e2012-01-31 17:01:43 -0800379 true, kAllocData);
buzbee67bf8852011-08-17 17:51:35 -0700380 tabRec->table = table;
381 tabRec->vaddr = mir->offset;
382 int size = table[1];
buzbee31a4a6f2012-02-28 15:36:15 -0800383 tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true,
384 kAllocLIR);
buzbeeba938cb2012-02-03 14:47:55 -0800385 oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700386
387 // Get the switch value
388 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
389 int rBase = oatAllocTemp(cUnit);
390 /* Allocate key and disp temps */
391 int rKey = oatAllocTemp(cUnit);
392 int rDisp = oatAllocTemp(cUnit);
393 // Make sure rKey's register number is less than rDisp's number for ldmia
394 if (rKey > rDisp) {
395 int tmp = rDisp;
396 rDisp = rKey;
397 rKey = tmp;
398 }
399 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700400 newLIR3(cUnit, kThumb2Adr, rBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700401 // Set up rIdx
402 int rIdx = oatAllocTemp(cUnit);
403 loadConstant(cUnit, rIdx, size);
404 // Establish loop branch target
buzbee31a4a6f2012-02-28 15:36:15 -0800405 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee67bf8852011-08-17 17:51:35 -0700406 // Load next key/disp
407 newLIR2(cUnit, kThumb2LdmiaWB, rBase, (1 << rKey) | (1 << rDisp));
408 opRegReg(cUnit, kOpCmp, rKey, rlSrc.lowReg);
409 // Go if match. NOTE: No instruction set switch here - must stay Thumb2
buzbee82488f52012-03-02 08:20:26 -0800410 opIT(cUnit, kArmCondEq, "");
buzbee31a4a6f2012-02-28 15:36:15 -0800411 LIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, rDisp);
buzbeec5159d52012-03-03 11:48:39 -0800412 tabRec->anchor = switchBranch;
buzbee67bf8852011-08-17 17:51:35 -0700413 // Needs to use setflags encoding here
414 newLIR3(cUnit, kThumb2SubsRRI12, rIdx, rIdx, 1);
buzbee82488f52012-03-02 08:20:26 -0800415 opCondBranch(cUnit, kCondNe, target);
buzbee67bf8852011-08-17 17:51:35 -0700416}
417
418
buzbee31a4a6f2012-02-28 15:36:15 -0800419void genPackedSwitch(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700420{
421 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
422 if (cUnit->printMe) {
423 dumpPackedSwitchTable(table);
424 }
425 // Add the table to the list - we'll process it later
buzbeeba938cb2012-02-03 14:47:55 -0800426 SwitchTable *tabRec = (SwitchTable *)oatNew(cUnit, sizeof(SwitchTable),
buzbee5abfa3e2012-01-31 17:01:43 -0800427 true, kAllocData);
buzbee67bf8852011-08-17 17:51:35 -0700428 tabRec->table = table;
429 tabRec->vaddr = mir->offset;
430 int size = table[1];
buzbee31a4a6f2012-02-28 15:36:15 -0800431 tabRec->targets = (LIR* *)oatNew(cUnit, size * sizeof(LIR*), true,
buzbee5abfa3e2012-01-31 17:01:43 -0800432 kAllocLIR);
buzbeeba938cb2012-02-03 14:47:55 -0800433 oatInsertGrowableList(cUnit, &cUnit->switchTables, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700434
435 // Get the switch value
436 rlSrc = loadValue(cUnit, rlSrc, kCoreReg);
437 int tableBase = oatAllocTemp(cUnit);
438 // Materialize a pointer to the switch table
buzbee03fa2632011-09-20 17:10:57 -0700439 newLIR3(cUnit, kThumb2Adr, tableBase, 0, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700440 int lowKey = s4FromSwitchData(&table[2]);
441 int keyReg;
442 // Remove the bias, if necessary
443 if (lowKey == 0) {
444 keyReg = rlSrc.lowReg;
445 } else {
446 keyReg = oatAllocTemp(cUnit);
447 opRegRegImm(cUnit, kOpSub, keyReg, rlSrc.lowReg, lowKey);
448 }
449 // Bounds check - if < 0 or >= size continue following switch
450 opRegImm(cUnit, kOpCmp, keyReg, size-1);
buzbee82488f52012-03-02 08:20:26 -0800451 LIR* branchOver = opCondBranch(cUnit, kCondHi, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700452
453 // Load the displacement from the switch table
454 int dispReg = oatAllocTemp(cUnit);
455 loadBaseIndexed(cUnit, tableBase, keyReg, dispReg, 2, kWord);
456
457 // ..and go! NOTE: No instruction set switch here - must stay Thumb2
buzbee31a4a6f2012-02-28 15:36:15 -0800458 LIR* switchBranch = newLIR1(cUnit, kThumb2AddPCR, dispReg);
buzbeec5159d52012-03-03 11:48:39 -0800459 tabRec->anchor = switchBranch;
buzbee67bf8852011-08-17 17:51:35 -0700460
461 /* branchOver target here */
buzbee31a4a6f2012-02-28 15:36:15 -0800462 LIR* target = newLIR0(cUnit, kPseudoTargetLabel);
buzbee31a4a6f2012-02-28 15:36:15 -0800463 branchOver->target = (LIR*)target;
buzbee67bf8852011-08-17 17:51:35 -0700464}
465
466/*
467 * Array data table format:
468 * ushort ident = 0x0300 magic value
469 * ushort width width of each element in the table
470 * uint size number of elements in the table
471 * ubyte data[size*width] table of data values (may contain a single-byte
472 * padding at the end)
473 *
474 * Total size is 4+(width * size + 1)/2 16-bit code units.
475 */
buzbee31a4a6f2012-02-28 15:36:15 -0800476void genFillArrayData(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700477{
478 const u2* table = cUnit->insns + mir->offset + mir->dalvikInsn.vB;
479 // Add the table to the list - we'll process it later
480 FillArrayData *tabRec = (FillArrayData *)
buzbeeba938cb2012-02-03 14:47:55 -0800481 oatNew(cUnit, sizeof(FillArrayData), true, kAllocData);
buzbee67bf8852011-08-17 17:51:35 -0700482 tabRec->table = table;
483 tabRec->vaddr = mir->offset;
484 u2 width = tabRec->table[1];
485 u4 size = tabRec->table[2] | (((u4)tabRec->table[3]) << 16);
486 tabRec->size = (size * width) + 8;
487
buzbeeba938cb2012-02-03 14:47:55 -0800488 oatInsertGrowableList(cUnit, &cUnit->fillArrayData, (intptr_t)tabRec);
buzbee67bf8852011-08-17 17:51:35 -0700489
490 // Making a call - use explicit registers
491 oatFlushAllRegs(cUnit); /* Everything to home location */
492 loadValueDirectFixed(cUnit, rlSrc, r0);
493 loadWordDisp(cUnit, rSELF,
buzbee1b4c8592011-08-31 10:43:51 -0700494 OFFSETOF_MEMBER(Thread, pHandleFillArrayDataFromCode), rLR);
buzbeee6d61962011-08-27 11:58:19 -0700495 // Materialize a pointer to the fill data image
buzbee03fa2632011-09-20 17:10:57 -0700496 newLIR3(cUnit, kThumb2Adr, r1, 0, (intptr_t)tabRec);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700497 oatClobberCalleeSave(cUnit);
498 opReg(cUnit, kOpBlx, rLR);
buzbee67bf8852011-08-17 17:51:35 -0700499}
500
buzbee31a4a6f2012-02-28 15:36:15 -0800501void genNegFloat(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700502{
503 RegLocation rlResult;
504 rlSrc = loadValue(cUnit, rlSrc, kFPReg);
505 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
506 newLIR2(cUnit, kThumb2Vnegs, rlResult.lowReg, rlSrc.lowReg);
507 storeValue(cUnit, rlDest, rlResult);
508}
509
buzbee31a4a6f2012-02-28 15:36:15 -0800510void genNegDouble(CompilationUnit* cUnit, RegLocation rlDest, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700511{
512 RegLocation rlResult;
513 rlSrc = loadValueWide(cUnit, rlSrc, kFPReg);
514 rlResult = oatEvalLoc(cUnit, rlDest, kFPReg, true);
515 newLIR2(cUnit, kThumb2Vnegd, S2D(rlResult.lowReg, rlResult.highReg),
516 S2D(rlSrc.lowReg, rlSrc.highReg));
517 storeValueWide(cUnit, rlDest, rlResult);
518}
519
buzbee67bf8852011-08-17 17:51:35 -0700520/*
521 * Handle simple case (thin lock) inline. If it's complicated, bail
522 * out to the heavyweight lock/unlock routines. We'll use dedicated
523 * registers here in order to be in the right position in case we
524 * to bail to dvm[Lock/Unlock]Object(self, object)
525 *
526 * r0 -> self pointer [arg0 for dvm[Lock/Unlock]Object
527 * r1 -> object [arg1 for dvm[Lock/Unlock]Object
528 * r2 -> intial contents of object->lock, later result of strex
529 * r3 -> self->threadId
530 * r12 -> allow to be used by utilities as general temp
531 *
532 * The result of the strex is 0 if we acquire the lock.
533 *
534 * See comments in Sync.c for the layout of the lock word.
535 * Of particular interest to this code is the test for the
536 * simple case - which we handle inline. For monitor enter, the
537 * simple case is thin lock, held by no-one. For monitor exit,
538 * the simple case is thin lock, held by the unlocking thread with
539 * a recurse count of 0.
540 *
541 * A minor complication is that there is a field in the lock word
542 * unrelated to locking: the hash state. This field must be ignored, but
543 * preserved.
544 *
545 */
buzbee31a4a6f2012-02-28 15:36:15 -0800546void genMonitorEnter(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700547{
buzbee67bf8852011-08-17 17:51:35 -0700548 oatFlushAllRegs(cUnit);
Elliott Hughes5f791332011-09-15 17:45:30 -0700549 DCHECK_EQ(LW_SHAPE_THIN, 0);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700550 loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700551 oatLockCallTemps(cUnit); // Prepare for explicit register usage
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700552 genNullCheck(cUnit, rlSrc.sRegLow, r0, mir);
553 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2);
554 newLIR3(cUnit, kThumb2Ldrex, r1, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700555 Object::MonitorOffset().Int32Value() >> 2); // Get object->lock
buzbeec143c552011-08-20 17:38:58 -0700556 // Align owner
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700557 opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT);
buzbee67bf8852011-08-17 17:51:35 -0700558 // Is lock unheld on lock or held by us (==threadId) on unlock?
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700559 newLIR4(cUnit, kThumb2Bfi, r2, r1, 0, LW_LOCK_OWNER_SHIFT - 1);
560 newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
buzbee05eba362012-03-10 20:11:27 -0800561 opRegImm(cUnit, kOpCmp, r1, 0);
562 opIT(cUnit, kArmCondEq, "");
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700563 newLIR4(cUnit, kThumb2Strex, r1, r2, r0,
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700564 Object::MonitorOffset().Int32Value() >> 2);
buzbee05eba362012-03-10 20:11:27 -0800565 opRegImm(cUnit, kOpCmp, r1, 0);
566 opIT(cUnit, kArmCondNe, "T");
buzbee1b4c8592011-08-31 10:43:51 -0700567 // Go expensive route - artLockObjectFromCode(self, obj);
568 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pLockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700569 rLR);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700570 oatClobberCalleeSave(cUnit);
571 opReg(cUnit, kOpBlx, rLR);
buzbee05eba362012-03-10 20:11:27 -0800572 oatGenMemBarrier(cUnit, kSY);
buzbee67bf8852011-08-17 17:51:35 -0700573}
574
575/*
576 * For monitor unlock, we don't have to use ldrex/strex. Once
577 * we've determined that the lock is thin and that we own it with
578 * a zero recursion count, it's safe to punch it back to the
579 * initial, unlock thin state with a store word.
580 */
buzbee31a4a6f2012-02-28 15:36:15 -0800581void genMonitorExit(CompilationUnit* cUnit, MIR* mir, RegLocation rlSrc)
buzbee67bf8852011-08-17 17:51:35 -0700582{
Elliott Hughes5f791332011-09-15 17:45:30 -0700583 DCHECK_EQ(LW_SHAPE_THIN, 0);
buzbee67bf8852011-08-17 17:51:35 -0700584 oatFlushAllRegs(cUnit);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700585 loadValueDirectFixed(cUnit, rlSrc, r0); // Get obj
buzbee2e748f32011-08-29 21:02:19 -0700586 oatLockCallTemps(cUnit); // Prepare for explicit register usage
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700587 genNullCheck(cUnit, rlSrc.sRegLow, r0, mir);
588 loadWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r1); // Get lock
589 loadWordDisp(cUnit, rSELF, Thread::ThinLockIdOffset().Int32Value(), r2);
buzbee67bf8852011-08-17 17:51:35 -0700590 // Is lock unheld on lock or held by us (==threadId) on unlock?
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700591 opRegRegImm(cUnit, kOpAnd, r3, r1, (LW_HASH_STATE_MASK << LW_HASH_STATE_SHIFT));
buzbeec143c552011-08-20 17:38:58 -0700592 // Align owner
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700593 opRegImm(cUnit, kOpLsl, r2, LW_LOCK_OWNER_SHIFT);
594 newLIR3(cUnit, kThumb2Bfc, r1, LW_HASH_STATE_SHIFT, LW_LOCK_OWNER_SHIFT - 1);
595 opRegReg(cUnit, kOpSub, r1, r2);
buzbee05eba362012-03-10 20:11:27 -0800596 opIT(cUnit, kArmCondEq, "EE");
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700597 storeWordDisp(cUnit, r0, Object::MonitorOffset().Int32Value(), r3);
Ian Rogers4f0d07c2011-10-06 23:38:47 -0700598 // Go expensive route - UnlockObjectFromCode(obj);
buzbee1b4c8592011-08-31 10:43:51 -0700599 loadWordDisp(cUnit, rSELF, OFFSETOF_MEMBER(Thread, pUnlockObjectFromCode),
buzbee67bf8852011-08-17 17:51:35 -0700600 rLR);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700601 oatClobberCalleeSave(cUnit);
602 opReg(cUnit, kOpBlx, rLR);
buzbee05eba362012-03-10 20:11:27 -0800603 oatGenMemBarrier(cUnit, kSY);
buzbee67bf8852011-08-17 17:51:35 -0700604}
605
606/*
607 * 64-bit 3way compare function.
608 * mov rX, #-1
609 * cmp op1hi, op2hi
610 * blt done
611 * bgt flip
612 * sub rX, op1lo, op2lo (treat as unsigned)
613 * beq done
614 * ite hi
615 * mov(hi) rX, #-1
616 * mov(!hi) rX, #1
617 * flip:
618 * neg rX
619 * done:
620 */
buzbee31a4a6f2012-02-28 15:36:15 -0800621void genCmpLong(CompilationUnit* cUnit, MIR* mir, RegLocation rlDest,
622 RegLocation rlSrc1, RegLocation rlSrc2)
buzbee67bf8852011-08-17 17:51:35 -0700623{
buzbee31a4a6f2012-02-28 15:36:15 -0800624 LIR* target1;
625 LIR* target2;
buzbee67bf8852011-08-17 17:51:35 -0700626 rlSrc1 = loadValueWide(cUnit, rlSrc1, kCoreReg);
627 rlSrc2 = loadValueWide(cUnit, rlSrc2, kCoreReg);
buzbeeb29e4d12011-09-26 15:05:48 -0700628 int tReg = oatAllocTemp(cUnit);
629 loadConstant(cUnit, tReg, -1);
buzbee67bf8852011-08-17 17:51:35 -0700630 opRegReg(cUnit, kOpCmp, rlSrc1.highReg, rlSrc2.highReg);
buzbee82488f52012-03-02 08:20:26 -0800631 LIR* branch1 = opCondBranch(cUnit, kCondLt, NULL);
632 LIR* branch2 = opCondBranch(cUnit, kCondGt, NULL);
buzbeeb29e4d12011-09-26 15:05:48 -0700633 opRegRegReg(cUnit, kOpSub, tReg, rlSrc1.lowReg, rlSrc2.lowReg);
buzbee82488f52012-03-02 08:20:26 -0800634 LIR* branch3 = opCondBranch(cUnit, kCondEq, NULL);
buzbee67bf8852011-08-17 17:51:35 -0700635
buzbee82488f52012-03-02 08:20:26 -0800636 opIT(cUnit, kArmCondHi, "E");
buzbeeb29e4d12011-09-26 15:05:48 -0700637 newLIR2(cUnit, kThumb2MovImmShift, tReg, modifiedImmediate(-1));
638 loadConstant(cUnit, tReg, 1);
buzbee67bf8852011-08-17 17:51:35 -0700639 genBarrier(cUnit);
640
buzbee31a4a6f2012-02-28 15:36:15 -0800641 target2 = newLIR0(cUnit, kPseudoTargetLabel);
buzbeeb29e4d12011-09-26 15:05:48 -0700642 opRegReg(cUnit, kOpNeg, tReg, tReg);
buzbee67bf8852011-08-17 17:51:35 -0700643
buzbee31a4a6f2012-02-28 15:36:15 -0800644 target1 = newLIR0(cUnit, kPseudoTargetLabel);
buzbee67bf8852011-08-17 17:51:35 -0700645
buzbeeb29e4d12011-09-26 15:05:48 -0700646 RegLocation rlTemp = LOC_C_RETURN; // Just using as template, will change
647 rlTemp.lowReg = tReg;
buzbee67bf8852011-08-17 17:51:35 -0700648 storeValue(cUnit, rlDest, rlTemp);
buzbeeb29e4d12011-09-26 15:05:48 -0700649 oatFreeTemp(cUnit, tReg);
buzbee67bf8852011-08-17 17:51:35 -0700650
buzbee31a4a6f2012-02-28 15:36:15 -0800651 branch1->target = (LIR*)target1;
652 branch2->target = (LIR*)target2;
653 branch3->target = branch1->target;
buzbee67bf8852011-08-17 17:51:35 -0700654}
655
buzbee67bf8852011-08-17 17:51:35 -0700656/*
buzbee31a4a6f2012-02-28 15:36:15 -0800657 * Generate a register comparison to an immediate and branch. Caller
658 * is responsible for setting branch target field.
buzbee67bf8852011-08-17 17:51:35 -0700659 */
buzbee82488f52012-03-02 08:20:26 -0800660LIR* opCmpImmBranch(CompilationUnit* cUnit, ConditionCode cond, int reg,
661 int checkValue, LIR* target)
buzbee67bf8852011-08-17 17:51:35 -0700662{
buzbee31a4a6f2012-02-28 15:36:15 -0800663 LIR* branch;
664 int modImm;
665 ArmConditionCode armCond = oatArmConditionEncoding(cond);
666 if ((LOWREG(reg)) && (checkValue == 0) &&
667 ((armCond == kArmCondEq) || (armCond == kArmCondNe))) {
668 branch = newLIR2(cUnit,
669 (armCond == kArmCondEq) ? kThumb2Cbz : kThumb2Cbnz,
670 reg, 0);
buzbee67bf8852011-08-17 17:51:35 -0700671 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800672 modImm = modifiedImmediate(checkValue);
673 if (LOWREG(reg) && ((checkValue & 0xff) == checkValue)) {
674 newLIR2(cUnit, kThumbCmpRI8, reg, checkValue);
675 } else if (modImm >= 0) {
676 newLIR2(cUnit, kThumb2CmpRI8, reg, modImm);
buzbee67bf8852011-08-17 17:51:35 -0700677 } else {
buzbee58f92742011-10-01 11:22:17 -0700678 int tReg = oatAllocTemp(cUnit);
buzbee31a4a6f2012-02-28 15:36:15 -0800679 loadConstant(cUnit, tReg, checkValue);
680 opRegReg(cUnit, kOpCmp, reg, tReg);
buzbee58f92742011-10-01 11:22:17 -0700681 }
buzbee31a4a6f2012-02-28 15:36:15 -0800682 branch = newLIR2(cUnit, kThumbBCond, 0, armCond);
buzbee67bf8852011-08-17 17:51:35 -0700683 }
buzbee82488f52012-03-02 08:20:26 -0800684 branch->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -0800685 return branch;
686}
buzbee82488f52012-03-02 08:20:26 -0800687LIR* opRegCopyNoInsert(CompilationUnit* cUnit, int rDest, int rSrc)
buzbee31a4a6f2012-02-28 15:36:15 -0800688{
689 LIR* res;
690 ArmOpcode opcode;
691 if (FPREG(rDest) || FPREG(rSrc))
692 return fpRegCopy(cUnit, rDest, rSrc);
buzbee31a4a6f2012-02-28 15:36:15 -0800693 if (LOWREG(rDest) && LOWREG(rSrc))
694 opcode = kThumbMovRR;
695 else if (!LOWREG(rDest) && !LOWREG(rSrc))
696 opcode = kThumbMovRR_H2H;
697 else if (LOWREG(rDest))
698 opcode = kThumbMovRR_H2L;
699 else
700 opcode = kThumbMovRR_L2H;
buzbeea2ebdd72012-03-04 14:57:06 -0800701 res = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, rDest, rSrc);
buzbee86a4bce2012-03-06 18:15:00 -0800702 if (!(cUnit->disableOpt & (1 << kSafeOptimizations)) && rDest == rSrc) {
buzbee31a4a6f2012-02-28 15:36:15 -0800703 res->flags.isNop = true;
704 }
705 return res;
buzbee67bf8852011-08-17 17:51:35 -0700706}
707
buzbee82488f52012-03-02 08:20:26 -0800708LIR* opRegCopy(CompilationUnit* cUnit, int rDest, int rSrc)
buzbee67bf8852011-08-17 17:51:35 -0700709{
buzbee82488f52012-03-02 08:20:26 -0800710 LIR* res = opRegCopyNoInsert(cUnit, rDest, rSrc);
buzbee31a4a6f2012-02-28 15:36:15 -0800711 oatAppendLIR(cUnit, (LIR*)res);
712 return res;
713}
buzbee67bf8852011-08-17 17:51:35 -0700714
buzbee82488f52012-03-02 08:20:26 -0800715void opRegCopyWide(CompilationUnit* cUnit, int destLo, int destHi,
buzbee31a4a6f2012-02-28 15:36:15 -0800716 int srcLo, int srcHi)
717{
718 bool destFP = FPREG(destLo) && FPREG(destHi);
719 bool srcFP = FPREG(srcLo) && FPREG(srcHi);
720 DCHECK_EQ(FPREG(srcLo), FPREG(srcHi));
721 DCHECK_EQ(FPREG(destLo), FPREG(destHi));
722 if (destFP) {
723 if (srcFP) {
buzbee82488f52012-03-02 08:20:26 -0800724 opRegCopy(cUnit, S2D(destLo, destHi), S2D(srcLo, srcHi));
buzbee67bf8852011-08-17 17:51:35 -0700725 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800726 newLIR3(cUnit, kThumb2Fmdrr, S2D(destLo, destHi), srcLo, srcHi);
727 }
728 } else {
729 if (srcFP) {
730 newLIR3(cUnit, kThumb2Fmrrd, destLo, destHi, S2D(srcLo, srcHi));
731 } else {
732 // Handle overlap
733 if (srcHi == destLo) {
buzbee82488f52012-03-02 08:20:26 -0800734 opRegCopy(cUnit, destHi, srcHi);
735 opRegCopy(cUnit, destLo, srcLo);
buzbee67bf8852011-08-17 17:51:35 -0700736 } else {
buzbee82488f52012-03-02 08:20:26 -0800737 opRegCopy(cUnit, destLo, srcLo);
738 opRegCopy(cUnit, destHi, srcHi);
buzbee67bf8852011-08-17 17:51:35 -0700739 }
740 }
741 }
buzbee67bf8852011-08-17 17:51:35 -0700742}
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800743
buzbee31a4a6f2012-02-28 15:36:15 -0800744
Elliott Hughes11d1b0c2012-01-23 16:57:47 -0800745} // namespace art