blob: f2449e50d9cba6df8e2c732ffe323b3ab2f0b6ef [file] [log] [blame]
buzbeee3acd072012-02-25 17:03:10 -08001/*
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
17namespace art {
18
buzbee31a4a6f2012-02-28 15:36:15 -080019void setMemRefType(LIR* lir, bool isLoad, int memType)
20{
21 u8 *maskPtr;
22 u8 mask = ENCODE_MEM;;
23 DCHECK(EncodingMap[lir->opcode].flags & (IS_LOAD | IS_STORE));
24 if (isLoad) {
25 maskPtr = &lir->useMask;
26 } else {
27 maskPtr = &lir->defMask;
28 }
29 /* Clear out the memref flags */
30 *maskPtr &= ~mask;
31 /* ..and then add back the one we need */
32 switch(memType) {
33 case kLiteral:
34 DCHECK(isLoad);
35 *maskPtr |= ENCODE_LITERAL;
36 break;
37 case kDalvikReg:
38 *maskPtr |= ENCODE_DALVIK_REG;
39 break;
40 case kHeapRef:
41 *maskPtr |= ENCODE_HEAP_REF;
42 break;
43 case kMustNotAlias:
44 /* Currently only loads can be marked as kMustNotAlias */
45 DCHECK(!(EncodingMap[lir->opcode].flags & IS_STORE));
46 *maskPtr |= ENCODE_MUST_NOT_ALIAS;
47 break;
48 default:
49 LOG(FATAL) << "Oat: invalid memref kind - " << memType;
50 }
51}
52
53/*
Ian Rogersb5d09b22012-03-06 22:14:17 -080054 * Mark load/store instructions that access Dalvik registers through the stack.
buzbee31a4a6f2012-02-28 15:36:15 -080055 */
Ian Rogersb5d09b22012-03-06 22:14:17 -080056void annotateDalvikRegAccess(LIR* lir, int regId, bool isLoad, bool is64bit)
buzbee31a4a6f2012-02-28 15:36:15 -080057{
58 setMemRefType(lir, isLoad, kDalvikReg);
59
60 /*
Ian Rogersb5d09b22012-03-06 22:14:17 -080061 * Store the Dalvik register id in aliasInfo. Mark the MSB if it is a 64-bit
buzbee31a4a6f2012-02-28 15:36:15 -080062 * access.
63 */
64 lir->aliasInfo = regId;
Ian Rogersb5d09b22012-03-06 22:14:17 -080065 if (is64bit) {
buzbee31a4a6f2012-02-28 15:36:15 -080066 lir->aliasInfo |= 0x80000000;
67 }
68}
69
70/*
71 * Decode the register id.
72 */
73inline u8 getRegMaskCommon(int reg)
74{
75 u8 seed;
76 int shift;
77 int regId = reg & 0x1f;
78
79 /*
80 * Each double register is equal to a pair of single-precision FP registers
81 */
82 seed = DOUBLEREG(reg) ? 3 : 1;
83 /* FP register starts at bit position 16 */
84 shift = FPREG(reg) ? kFPReg0 : 0;
85 /* Expand the double register id into single offset */
86 shift += regId;
87 return (seed << shift);
88}
89
90/*
91 * Mark the corresponding bit(s).
92 */
93inline void setupRegMask(u8* mask, int reg)
94{
95 *mask |= getRegMaskCommon(reg);
96}
97
98/*
99 * Set up the proper fields in the resource mask
100 */
101void setupResourceMasks(LIR* lir)
102{
103 int opcode = lir->opcode;
104 int flags;
105
106 if (opcode <= 0) {
107 lir->useMask = lir->defMask = 0;
108 return;
109 }
110
111 flags = EncodingMap[lir->opcode].flags;
112
113 if (flags & NEEDS_FIXUP) {
114 lir->flags.pcRelFixup = true;
115 }
116
buzbeee88dfbf2012-03-05 11:19:57 -0800117 /* Get the starting size of the instruction's template */
118 lir->flags.size = oatGetInsnSize(lir);
119
buzbee31a4a6f2012-02-28 15:36:15 -0800120 /* Set up the mask for resources that are updated */
121 if (flags & (IS_LOAD | IS_STORE)) {
122 /* Default to heap - will catch specialized classes later */
123 setMemRefType(lir, flags & IS_LOAD, kHeapRef);
124 }
125
126 /*
127 * Conservatively assume the branch here will call out a function that in
128 * turn will trash everything.
129 */
130 if (flags & IS_BRANCH) {
131 lir->defMask = lir->useMask = ENCODE_ALL;
132 return;
133 }
134
135 if (flags & REG_DEF0) {
136 setupRegMask(&lir->defMask, lir->operands[0]);
137 }
138
139 if (flags & REG_DEF1) {
140 setupRegMask(&lir->defMask, lir->operands[1]);
141 }
142
143 if (flags & REG_DEF_SP) {
144 lir->defMask |= ENCODE_REG_SP;
145 }
146
buzbeea7678db2012-03-05 15:35:46 -0800147#if !defined(TARGET_X86)
buzbee31a4a6f2012-02-28 15:36:15 -0800148 if (flags & REG_DEF_LR) {
149 lir->defMask |= ENCODE_REG_LR;
150 }
buzbeea7678db2012-03-05 15:35:46 -0800151#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800152
153 if (flags & REG_DEF_LIST0) {
154 lir->defMask |= ENCODE_REG_LIST(lir->operands[0]);
155 }
156
157 if (flags & REG_DEF_LIST1) {
158 lir->defMask |= ENCODE_REG_LIST(lir->operands[1]);
159 }
160
buzbee5de34942012-03-01 14:51:57 -0800161#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800162 if (flags & REG_DEF_FPCS_LIST0) {
163 lir->defMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
164 }
165
166 if (flags & REG_DEF_FPCS_LIST2) {
167 for (int i = 0; i < lir->operands[2]; i++) {
168 setupRegMask(&lir->defMask, lir->operands[1] + i);
169 }
170 }
buzbee5de34942012-03-01 14:51:57 -0800171#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800172
173 if (flags & SETS_CCODES) {
174 lir->defMask |= ENCODE_CCODE;
175 }
176
177#if defined(TARGET_ARM)
178 /* Conservatively treat the IT block */
179 if (flags & IS_IT) {
180 lir->defMask = ENCODE_ALL;
181 }
182#endif
183
184 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
185 int i;
186
187 for (i = 0; i < 4; i++) {
188 if (flags & (1 << (kRegUse0 + i))) {
189 setupRegMask(&lir->useMask, lir->operands[i]);
190 }
191 }
192 }
193
buzbeea7678db2012-03-05 15:35:46 -0800194#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800195 if (flags & REG_USE_PC) {
196 lir->useMask |= ENCODE_REG_PC;
197 }
buzbeea7678db2012-03-05 15:35:46 -0800198#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800199
200 if (flags & REG_USE_SP) {
201 lir->useMask |= ENCODE_REG_SP;
202 }
203
204 if (flags & REG_USE_LIST0) {
205 lir->useMask |= ENCODE_REG_LIST(lir->operands[0]);
206 }
207
208 if (flags & REG_USE_LIST1) {
209 lir->useMask |= ENCODE_REG_LIST(lir->operands[1]);
210 }
211
buzbee5de34942012-03-01 14:51:57 -0800212#if defined(TARGET_ARM)
buzbee31a4a6f2012-02-28 15:36:15 -0800213 if (flags & REG_USE_FPCS_LIST0) {
214 lir->useMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
215 }
216
217 if (flags & REG_USE_FPCS_LIST2) {
218 for (int i = 0; i < lir->operands[2]; i++) {
219 setupRegMask(&lir->useMask, lir->operands[1] + i);
220 }
221 }
buzbee5de34942012-03-01 14:51:57 -0800222#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800223
224 if (flags & USES_CCODES) {
225 lir->useMask |= ENCODE_CCODE;
226 }
227
228#if defined(TARGET_ARM)
229 /* Fixup for kThumbPush/lr and kThumbPop/pc */
230 if (opcode == kThumbPush || opcode == kThumbPop) {
231 u8 r8Mask = getRegMaskCommon(r8);
232 if ((opcode == kThumbPush) && (lir->useMask & r8Mask)) {
233 lir->useMask &= ~r8Mask;
234 lir->useMask |= ENCODE_REG_LR;
235 } else if ((opcode == kThumbPop) && (lir->defMask & r8Mask)) {
236 lir->defMask &= ~r8Mask;
237 lir->defMask |= ENCODE_REG_PC;
238 }
239 }
240#endif
241}
242
243/*
buzbee5de34942012-03-01 14:51:57 -0800244 * Debugging macros
245 */
246#define DUMP_RESOURCE_MASK(X)
247#define DUMP_SSA_REP(X)
248
249/* Pretty-print a LIR instruction */
250void oatDumpLIRInsn(CompilationUnit* cUnit, LIR* arg, unsigned char* baseAddr)
251{
252 LIR* lir = (LIR*) arg;
253 int offset = lir->offset;
254 int dest = lir->operands[0];
buzbee86a4bce2012-03-06 18:15:00 -0800255 const bool dumpNop = (cUnit->enableDebug & (1 << kDebugShowNops));
buzbee5de34942012-03-01 14:51:57 -0800256
257 /* Handle pseudo-ops individually, and all regular insns as a group */
258 switch(lir->opcode) {
259 case kPseudoMethodEntry:
260 LOG(INFO) << "-------- method entry " <<
261 PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
262 break;
263 case kPseudoMethodExit:
264 LOG(INFO) << "-------- Method_Exit";
265 break;
266 case kPseudoBarrier:
267 LOG(INFO) << "-------- BARRIER";
268 break;
269 case kPseudoExtended:
270 LOG(INFO) << "-------- " << (char* ) dest;
271 break;
272 case kPseudoSSARep:
273 DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << (char* ) dest);
274 break;
275 case kPseudoEntryBlock:
276 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
277 break;
278 case kPseudoDalvikByteCodeBoundary:
279 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex <<
280 lir->dalvikOffset << " @ " << (char* )lir->operands[0];
281 break;
282 case kPseudoExitBlock:
283 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
284 break;
285 case kPseudoPseudoAlign4:
286 LOG(INFO) << (intptr_t)baseAddr + offset << " (0x" << std::hex <<
287 offset << "): .align4";
288 break;
289 case kPseudoEHBlockLabel:
290 LOG(INFO) << "Exception_Handling:";
291 break;
292 case kPseudoTargetLabel:
293 case kPseudoNormalBlockLabel:
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700294 LOG(INFO) << "L" << (void*)lir << ":";
buzbee5de34942012-03-01 14:51:57 -0800295 break;
296 case kPseudoThrowTarget:
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700297 LOG(INFO) << "LT" << (void*)lir << ":";
buzbee5de34942012-03-01 14:51:57 -0800298 break;
299 case kPseudoSuspendTarget:
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700300 LOG(INFO) << "LS" << (void*)lir << ":";
buzbee5de34942012-03-01 14:51:57 -0800301 break;
302 case kPseudoCaseLabel:
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700303 LOG(INFO) << "LC" << (void*)lir << ": Case target 0x" <<
buzbee5de34942012-03-01 14:51:57 -0800304 std::hex << lir->operands[0] << "|" << std::dec <<
305 lir->operands[0];
306 break;
307 default:
308 if (lir->flags.isNop && !dumpNop) {
309 break;
310 } else {
311 std::string op_name(buildInsnString(EncodingMap[lir->opcode].name, lir, baseAddr));
312 std::string op_operands(buildInsnString(EncodingMap[lir->opcode].fmt, lir, baseAddr));
buzbeebe003642012-03-02 15:28:37 -0800313 LOG(INFO) << StringPrintf("%05x: %-9s%s%s", (unsigned int)(baseAddr + offset),
buzbee5de34942012-03-01 14:51:57 -0800314 op_name.c_str(), op_operands.c_str(), lir->flags.isNop ? "(nop)" : "");
315 }
316 break;
317 }
318
319 if (lir->useMask && (!lir->flags.isNop || dumpNop)) {
320 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir,
321 lir->useMask, "use"));
322 }
323 if (lir->defMask && (!lir->flags.isNop || dumpNop)) {
324 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir,
325 lir->defMask, "def"));
326 }
327}
328
329void oatDumpPromotionMap(CompilationUnit *cUnit)
330{
buzbee9c044ce2012-03-18 13:24:07 -0700331 int numRegs = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
332 for (int i = 0; i < numRegs; i++) {
buzbee5de34942012-03-01 14:51:57 -0800333 PromotionMap vRegMap = cUnit->promotionMap[i];
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700334 std::string buf;
buzbee5de34942012-03-01 14:51:57 -0800335 if (vRegMap.fpLocation == kLocPhysReg) {
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700336 StringAppendF(&buf, " : s%d", vRegMap.fpReg & FP_REG_MASK);
buzbee9c044ce2012-03-18 13:24:07 -0700337 }
338
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700339 std::string buf3;
340 if (i < cUnit->numDalvikRegisters) {
341 StringAppendF(&buf3, "%02d", i);
342 } else if (i == cUnit->methodSReg) {
343 buf3 = "Method*";
344 } else {
345 StringAppendF(&buf3, "ct%d", i - cUnit->numDalvikRegisters);
346 }
347
348 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
buzbee5de34942012-03-01 14:51:57 -0800349 vRegMap.coreLocation == kLocPhysReg ?
350 "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ?
Elliott Hughesbdf6c3d2012-03-20 13:43:53 -0700351 vRegMap.coreReg : oatSRegOffset(cUnit, i), buf.c_str());
buzbee5de34942012-03-01 14:51:57 -0800352 }
353}
354
buzbee5de34942012-03-01 14:51:57 -0800355/* Dump instructions and constant pool contents */
356void oatCodegenDump(CompilationUnit* cUnit)
357{
358 LOG(INFO) << "/*";
359 LOG(INFO) << "Dumping LIR insns for "
360 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
361 LIR* lirInsn;
362 LIR* thisLIR;
363 int insnsSize = cUnit->insnsSize;
364
365 LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs;
366 LOG(INFO) << "Ins : " << cUnit->numIns;
367 LOG(INFO) << "Outs : " << cUnit->numOuts;
368 LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills;
369 LOG(INFO) << "FPSpills : " << cUnit->numFPSpills;
buzbee239c4e72012-03-16 08:42:29 -0700370 LOG(INFO) << "CompilerTemps : " << cUnit->numCompilerTemps;
buzbee5de34942012-03-01 14:51:57 -0800371 LOG(INFO) << "Frame size : " << cUnit->frameSize;
buzbee5de34942012-03-01 14:51:57 -0800372 LOG(INFO) << "code size is " << cUnit->totalSize <<
373 " bytes, Dalvik size is " << insnsSize * 2;
374 LOG(INFO) << "expansion factor: " <<
375 (float)cUnit->totalSize / (float)(insnsSize * 2);
376 oatDumpPromotionMap(cUnit);
377 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
378 oatDumpLIRInsn(cUnit, lirInsn, 0);
379 }
380 for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) {
381 thisLIR = (LIR*) lirInsn;
382 LOG(INFO) << StringPrintf("%x (%04x): .class (%s)",
383 thisLIR->offset, thisLIR->offset,
384 ((CallsiteInfo *) thisLIR->operands[0])->classDescriptor);
385 }
386 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
387 thisLIR = (LIR*) lirInsn;
388 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)",
389 thisLIR->offset, thisLIR->offset, thisLIR->operands[0]);
390 }
391
392 const DexFile::MethodId& method_id =
393 cUnit->dex_file->GetMethodId(cUnit->method_idx);
394 std::string signature(cUnit->dex_file->GetMethodSignature(method_id));
395 std::string name(cUnit->dex_file->GetMethodName(method_id));
396 std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id));
397
398 // Dump mapping table
399 if (cUnit->mappingTable.size() > 0) {
400 std::string line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {",
401 descriptor.c_str(), name.c_str(), signature.c_str(), cUnit->mappingTable.size()));
402 std::replace(line.begin(), line.end(), ';', '_');
403 LOG(INFO) << line;
404 for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) {
buzbee82488f52012-03-02 08:20:26 -0800405 line = StringPrintf(" {0x%05x, 0x%04x},",
buzbee5de34942012-03-01 14:51:57 -0800406 cUnit->mappingTable[i], cUnit->mappingTable[i+1]);
407 LOG(INFO) << line;
408 }
409 LOG(INFO) <<" };\n\n";
410 }
411}
412
buzbeea2ebdd72012-03-04 14:57:06 -0800413
414LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800415 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800416{
417 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
418 insn->dalvikOffset = dalvikOffset;
419 insn->opcode = opcode;
420 insn->operands[0] = op0;
421 insn->operands[1] = op1;
422 insn->operands[2] = op2;
423 insn->operands[3] = op3;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800424 insn->operands[4] = op4;
buzbeea2ebdd72012-03-04 14:57:06 -0800425 insn->target = target;
426 oatSetupResourceMasks(insn);
427 if (opcode == kPseudoTargetLabel) {
428 // Always make labels scheduling barriers
429 insn->defMask = ENCODE_ALL;
430 }
431 return insn;
432}
433
buzbee5de34942012-03-01 14:51:57 -0800434/*
buzbee31a4a6f2012-02-28 15:36:15 -0800435 * The following are building blocks to construct low-level IRs with 0 - 4
436 * operands.
437 */
buzbee5de34942012-03-01 14:51:57 -0800438LIR* newLIR0(CompilationUnit* cUnit, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800439{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800440 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND))
441 << EncodingMap[opcode].name << " " << (int)opcode << " "
442 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
443 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800444 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode);
buzbee31a4a6f2012-02-28 15:36:15 -0800445 oatAppendLIR(cUnit, (LIR*) insn);
446 return insn;
447}
448
buzbee5de34942012-03-01 14:51:57 -0800449LIR* newLIR1(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800450 int dest)
451{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800452 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP))
453 << EncodingMap[opcode].name << " " << (int)opcode << " "
454 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
455 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800456 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest);
buzbee31a4a6f2012-02-28 15:36:15 -0800457 oatAppendLIR(cUnit, (LIR*) insn);
458 return insn;
459}
460
buzbee5de34942012-03-01 14:51:57 -0800461LIR* newLIR2(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800462 int dest, int src1)
463{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800464 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_BINARY_OP))
465 << EncodingMap[opcode].name << " " << (int)opcode << " "
466 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
467 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800468 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1);
buzbee31a4a6f2012-02-28 15:36:15 -0800469 oatAppendLIR(cUnit, (LIR*) insn);
470 return insn;
471}
472
buzbee5de34942012-03-01 14:51:57 -0800473LIR* newLIR3(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800474 int dest, int src1, int src2)
475{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800476 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_TERTIARY_OP))
477 << EncodingMap[opcode].name << " " << (int)opcode << " "
478 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
479 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800480 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
481 src2);
buzbee31a4a6f2012-02-28 15:36:15 -0800482 oatAppendLIR(cUnit, (LIR*) insn);
483 return insn;
484}
485
buzbee5de34942012-03-01 14:51:57 -0800486LIR* newLIR4(CompilationUnit* cUnit, int opcode,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800487 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800488{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800489 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUAD_OP))
490 << EncodingMap[opcode].name << " " << (int)opcode << " "
491 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
492 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800493 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
494 src2, info);
buzbee31a4a6f2012-02-28 15:36:15 -0800495 oatAppendLIR(cUnit, (LIR*) insn);
496 return insn;
497}
buzbee31a4a6f2012-02-28 15:36:15 -0800498
Ian Rogersb5d09b22012-03-06 22:14:17 -0800499LIR* newLIR5(CompilationUnit* cUnit, int opcode,
500 int dest, int src1, int src2, int info1, int info2)
501{
502 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUIN_OP))
503 << EncodingMap[opcode].name << " " << (int)opcode << " "
504 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
505 << cUnit->currentDalvikOffset;
506 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
507 src2, info1, info2);
508 oatAppendLIR(cUnit, (LIR*) insn);
509 return insn;
510}
511
buzbee31a4a6f2012-02-28 15:36:15 -0800512/*
513 * Search the existing constants in the literal pool for an exact or close match
514 * within specified delta (greater or equal to 0).
515 */
516LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
517{
518 while (dataTarget) {
519 if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <=
520 delta)
521 return (LIR* ) dataTarget;
522 dataTarget = dataTarget->next;
523 }
524 return NULL;
525}
526
527/* Search the existing constants in the literal pool for an exact wide match */
528LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
529{
530 bool loMatch = false;
531 LIR* loTarget = NULL;
532 while (dataTarget) {
533 if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) {
534 return (LIR*)loTarget;
535 }
536 loMatch = false;
537 if (((LIR*)dataTarget)->operands[0] == valLo) {
538 loMatch = true;
539 loTarget = dataTarget;
540 }
541 dataTarget = dataTarget->next;
542 }
543 return NULL;
544}
545
546/*
547 * The following are building blocks to insert constants into the pool or
548 * instruction streams.
549 */
550
buzbee5de34942012-03-01 14:51:57 -0800551/* Add a 32-bit constant either in the constant pool */
Ian Rogers3fa13792012-03-18 15:53:45 -0700552LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800553{
554 /* Add the constant to the literal pool */
555 if (constantListP) {
556 LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true,
557 kAllocData);
558 newValue->operands[0] = value;
559 newValue->next = *constantListP;
560 *constantListP = (LIR*) newValue;
561 return newValue;
buzbee31a4a6f2012-02-28 15:36:15 -0800562 }
563 return NULL;
564}
565
566/* Add a 64-bit constant to the constant pool or mixed with code */
567LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
568 int valLo, int valHi)
569{
buzbee31a4a6f2012-02-28 15:36:15 -0800570 //FIXME: hard-coded little endian, need BE variant
buzbee5de34942012-03-01 14:51:57 -0800571 // Insert high word into list first
572 addWordData(cUnit, constantListP, valHi);
573 return addWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800574}
575
Ian Rogersab058bb2012-03-11 22:19:38 -0700576void pushWord(std::vector<uint8_t>&buf, int data) {
577 buf.push_back( data & 0xff);
578 buf.push_back( (data >> 8) & 0xff);
579 buf.push_back( (data >> 16) & 0xff);
580 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800581}
582
Ian Rogersab058bb2012-03-11 22:19:38 -0700583void alignBuffer(std::vector<uint8_t>&buf, size_t offset) {
584 while (buf.size() < offset) {
buzbeee3acd072012-02-25 17:03:10 -0800585 buf.push_back(0);
Ian Rogersab058bb2012-03-11 22:19:38 -0700586 }
buzbeee3acd072012-02-25 17:03:10 -0800587}
588
Brian Carlstromf5822582012-03-19 22:34:31 -0700589bool IsDirect(int invokeType) {
590 InvokeType type = static_cast<InvokeType>(invokeType);
591 return type == kStatic || type == kDirect;
592}
593
buzbeee3acd072012-02-25 17:03:10 -0800594/* Write the literal pool to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800595void installLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800596{
597 alignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
Ian Rogers3fa13792012-03-18 15:53:45 -0700598 LIR* dataLIR = cUnit->literalList;
buzbeee3acd072012-02-25 17:03:10 -0800599 while (dataLIR != NULL) {
600 pushWord(cUnit->codeBuffer, dataLIR->operands[0]);
601 dataLIR = NEXT_LIR(dataLIR);
602 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700603 // Push code and method literals, record offsets for the compiler to patch.
604 dataLIR = cUnit->codeLiteralList;
605 if (dataLIR != NULL) {
606 while (dataLIR != NULL) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700607 uint32_t target = dataLIR->operands[0];
Ian Rogers3fa13792012-03-18 15:53:45 -0700608 cUnit->compiler->AddCodePatch(cUnit->dex_cache, cUnit->dex_file,
609 cUnit->method_idx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700610 cUnit->access_flags,
611 target,
612 IsDirect(dataLIR->operands[1]),
Ian Rogers3fa13792012-03-18 15:53:45 -0700613 cUnit->codeBuffer.size());
Brian Carlstromf5822582012-03-19 22:34:31 -0700614 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
615 // unique based on target to ensure code deduplication works
616 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
617 pushWord(cUnit->codeBuffer, unique_patch_value);
Ian Rogers3fa13792012-03-18 15:53:45 -0700618 dataLIR = NEXT_LIR(dataLIR);
619 }
620 dataLIR = cUnit->methodLiteralList;
621 while (dataLIR != NULL) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700622 uint32_t target = dataLIR->operands[0];
Ian Rogers3fa13792012-03-18 15:53:45 -0700623 cUnit->compiler->AddMethodPatch(cUnit->dex_cache, cUnit->dex_file,
624 cUnit->method_idx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700625 cUnit->access_flags,
626 target,
627 IsDirect(dataLIR->operands[1]),
Ian Rogers3fa13792012-03-18 15:53:45 -0700628 cUnit->codeBuffer.size());
Brian Carlstromf5822582012-03-19 22:34:31 -0700629 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
630 // unique based on target to ensure code deduplication works
631 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
632 pushWord(cUnit->codeBuffer, unique_patch_value);
Ian Rogers3fa13792012-03-18 15:53:45 -0700633 dataLIR = NEXT_LIR(dataLIR);
634 }
635 }
636
buzbeee3acd072012-02-25 17:03:10 -0800637}
638
639/* Write the switch tables to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800640void installSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800641{
642 GrowableListIterator iterator;
643 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
644 while (true) {
645 SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext(
646 &iterator);
647 if (tabRec == NULL) break;
648 alignBuffer(cUnit->codeBuffer, tabRec->offset);
buzbeec5159d52012-03-03 11:48:39 -0800649 /*
650 * For Arm, our reference point is the address of the bx
651 * instruction that does the launch, so we have to subtract
652 * the auto pc-advance. For other targets the reference point
653 * is a label, so we can use the offset as-is.
654 */
655#if defined(TARGET_ARM)
656 int bxOffset = tabRec->anchor->offset + 4;
657#else
658 int bxOffset = tabRec->anchor->offset;
659#endif
buzbeee3acd072012-02-25 17:03:10 -0800660 if (cUnit->printMe) {
661 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
662 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800663 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeee3acd072012-02-25 17:03:10 -0800664 int* keys = (int*)&(tabRec->table[2]);
665 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800666 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800667 if (cUnit->printMe) {
668 LOG(INFO) << " Case[" << elems << "] key: 0x" <<
669 std::hex << keys[elems] << ", disp: 0x" <<
670 std::hex << disp;
671 }
672 pushWord(cUnit->codeBuffer, keys[elems]);
673 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800674 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800675 }
676 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800677 DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeee3acd072012-02-25 17:03:10 -0800678 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800679 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800680 if (cUnit->printMe) {
681 LOG(INFO) << " Case[" << elems << "] disp: 0x" <<
682 std::hex << disp;
683 }
684 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800685 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800686 }
687 }
688 }
689}
690
691/* Write the fill array dta to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800692void installFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800693{
694 GrowableListIterator iterator;
695 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
696 while (true) {
697 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
698 &iterator);
699 if (tabRec == NULL) break;
700 alignBuffer(cUnit->codeBuffer, tabRec->offset);
Ian Rogersab058bb2012-03-11 22:19:38 -0700701 for (int i = 0; i < (tabRec->size + 1) / 2; i++) {
702 cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF);
703 cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800704 }
705 }
706}
707
buzbee31a4a6f2012-02-28 15:36:15 -0800708int assignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800709{
710 for (;lir != NULL; lir = lir->next) {
711 lir->offset = offset;
712 offset += 4;
713 }
714 return offset;
715}
716
buzbee31a4a6f2012-02-28 15:36:15 -0800717void createMappingTable(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800718{
buzbee31a4a6f2012-02-28 15:36:15 -0800719 LIR* tgtLIR;
buzbeee3acd072012-02-25 17:03:10 -0800720 int currentDalvikOffset = -1;
721
buzbee31a4a6f2012-02-28 15:36:15 -0800722 for (tgtLIR = (LIR *) cUnit->firstLIRInsn;
buzbeee3acd072012-02-25 17:03:10 -0800723 tgtLIR;
724 tgtLIR = NEXT_LIR(tgtLIR)) {
725 if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop &&
buzbee31a4a6f2012-02-28 15:36:15 -0800726 (currentDalvikOffset != tgtLIR->dalvikOffset)) {
buzbeee3acd072012-02-25 17:03:10 -0800727 // Changed - need to emit a record
buzbee31a4a6f2012-02-28 15:36:15 -0800728 cUnit->mappingTable.push_back(tgtLIR->offset);
729 cUnit->mappingTable.push_back(tgtLIR->dalvikOffset);
730 currentDalvikOffset = tgtLIR->dalvikOffset;
buzbeee3acd072012-02-25 17:03:10 -0800731 }
732 }
733}
734
735/* Determine the offset of each literal field */
buzbee31a4a6f2012-02-28 15:36:15 -0800736int assignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800737{
738 offset = assignLiteralOffsetCommon(cUnit->literalList, offset);
Ian Rogers3fa13792012-03-18 15:53:45 -0700739 offset = assignLiteralOffsetCommon(cUnit->codeLiteralList, offset);
740 offset = assignLiteralOffsetCommon(cUnit->methodLiteralList, offset);
buzbeee3acd072012-02-25 17:03:10 -0800741 return offset;
742}
743
buzbee31a4a6f2012-02-28 15:36:15 -0800744int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800745{
746 GrowableListIterator iterator;
747 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
748 while (true) {
749 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
750 &iterator);
751 if (tabRec == NULL) break;
752 tabRec->offset = offset;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800753 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeee3acd072012-02-25 17:03:10 -0800754 offset += tabRec->table[1] * (sizeof(int) * 2);
755 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800756 DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeee3acd072012-02-25 17:03:10 -0800757 offset += tabRec->table[1] * sizeof(int);
758 }
759 }
760 return offset;
761}
762
buzbee31a4a6f2012-02-28 15:36:15 -0800763int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800764{
765 GrowableListIterator iterator;
766 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
767 while (true) {
768 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
769 &iterator);
770 if (tabRec == NULL) break;
771 tabRec->offset = offset;
772 offset += tabRec->size;
773 // word align
774 offset = (offset + 3) & ~3;
775 }
776 return offset;
777}
778
779/*
780 * Walk the compilation unit and assign offsets to instructions
781 * and literals and compute the total size of the compiled unit.
782 */
783void oatAssignOffsets(CompilationUnit* cUnit)
784{
785 int offset = oatAssignInsnOffsets(cUnit);
786
787 /* Const values have to be word aligned */
788 offset = (offset + 3) & ~3;
789
790 /* Set up offsets for literals */
791 cUnit->dataOffset = offset;
792
793 offset = assignLiteralOffset(cUnit, offset);
794
795 offset = assignSwitchTablesOffset(cUnit, offset);
796
797 offset = assignFillArrayDataOffset(cUnit, offset);
798
799 cUnit->totalSize = offset;
800}
801
802/*
803 * Go over each instruction in the list and calculate the offset from the top
804 * before sending them off to the assembler. If out-of-range branch distance is
805 * seen rearrange the instructions a bit to correct it.
806 */
807void oatAssembleLIR(CompilationUnit* cUnit)
808{
809 oatAssignOffsets(cUnit);
810 /*
811 * Assemble here. Note that we generate code with optimistic assumptions
812 * and if found now to work, we'll have to redo the sequence and retry.
813 */
814
815 while (true) {
816 AssemblerStatus res = oatAssembleInstructions(cUnit, 0);
817 if (res == kSuccess) {
818 break;
819 } else {
820 cUnit->assemblerRetries++;
821 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
Ian Rogersb41b33b2012-03-20 14:22:54 -0700822 oatCodegenDump(cUnit);
823 LOG(FATAL) << "Assembler error - too many retries";
buzbeee3acd072012-02-25 17:03:10 -0800824 }
825 // Redo offsets and try again
826 oatAssignOffsets(cUnit);
827 cUnit->codeBuffer.clear();
828 }
829 }
830
831 // Install literals
832 installLiteralPools(cUnit);
833
834 // Install switch tables
835 installSwitchTables(cUnit);
836
837 // Install fill array data
838 installFillArrayData(cUnit);
839
840 /*
841 * Create the mapping table
842 */
843 createMappingTable(cUnit);
844}
845
buzbee31a4a6f2012-02-28 15:36:15 -0800846/*
847 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
848 * offset vaddr. This label will be used to fix up the case
849 * branch table during the assembly phase. Be sure to set
850 * all resource flags on this to prevent code motion across
851 * target boundaries. KeyVal is just there for debugging.
852 */
853LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
854{
855 std::map<unsigned int, LIR*>::iterator it;
856 it = cUnit->boundaryMap.find(vaddr);
857 if (it == cUnit->boundaryMap.end()) {
858 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
859 }
860 LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
861 newLabel->dalvikOffset = vaddr;
862 newLabel->opcode = kPseudoCaseLabel;
863 newLabel->operands[0] = keyVal;
864 oatInsertLIRAfter(it->second, (LIR*)newLabel);
865 return newLabel;
866}
867
868void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
869{
870 const u2* table = tabRec->table;
871 int baseVaddr = tabRec->vaddr;
872 int *targets = (int*)&table[4];
873 int entries = table[1];
874 int lowKey = s4FromSwitchData(&table[2]);
875 for (int i = 0; i < entries; i++) {
876 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
877 i + lowKey);
878 }
879}
880
881void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
882{
883 const u2* table = tabRec->table;
884 int baseVaddr = tabRec->vaddr;
885 int entries = table[1];
886 int* keys = (int*)&table[2];
887 int* targets = &keys[entries];
888 for (int i = 0; i < entries; i++) {
889 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
890 keys[i]);
891 }
892}
893
894void oatProcessSwitchTables(CompilationUnit* cUnit)
895{
896 GrowableListIterator iterator;
897 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
898 while (true) {
899 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
900 &iterator);
901 if (tabRec == NULL) break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800902 if (tabRec->table[0] == Instruction::kPackedSwitchSignature) {
buzbee31a4a6f2012-02-28 15:36:15 -0800903 markPackedCaseLabels(cUnit, tabRec);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800904 } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee31a4a6f2012-02-28 15:36:15 -0800905 markSparseCaseLabels(cUnit, tabRec);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800906 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800907 LOG(FATAL) << "Invalid switch table";
908 }
909 }
910}
911
912//FIXME: Do we have endian issues here?
913
914void dumpSparseSwitchTable(const u2* table)
915 /*
916 * Sparse switch data format:
917 * ushort ident = 0x0200 magic value
918 * ushort size number of entries in the table; > 0
919 * int keys[size] keys, sorted low-to-high; 32-bit aligned
920 * int targets[size] branch targets, relative to switch opcode
921 *
922 * Total size is (2+size*4) 16-bit code units.
923 */
924{
925 u2 ident = table[0];
926 int entries = table[1];
927 int* keys = (int*)&table[2];
928 int* targets = &keys[entries];
929 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
930 ", entries: " << std::dec << entries;
931 for (int i = 0; i < entries; i++) {
932 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
933 targets[i];
934 }
935}
936
937void dumpPackedSwitchTable(const u2* table)
938 /*
939 * Packed switch data format:
940 * ushort ident = 0x0100 magic value
941 * ushort size number of entries in the table
942 * int first_key first (and lowest) switch case value
943 * int targets[size] branch targets, relative to switch opcode
944 *
945 * Total size is (4+size*2) 16-bit code units.
946 */
947{
948 u2 ident = table[0];
949 int* targets = (int*)&table[4];
950 int entries = table[1];
951 int lowKey = s4FromSwitchData(&table[2]);
952 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
953 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
954 for (int i = 0; i < entries; i++) {
955 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
956 targets[i];
957 }
958}
buzbeee3acd072012-02-25 17:03:10 -0800959
960
961} // namespace art