blob: c9c9c5ec9563b6a57f6f17bcd06e1b12e7c27490 [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
buzbee9c044ce2012-03-18 13:24:07 -0700329#define BSZ 100
buzbee5de34942012-03-01 14:51:57 -0800330void oatDumpPromotionMap(CompilationUnit *cUnit)
331{
buzbee9c044ce2012-03-18 13:24:07 -0700332 int numRegs = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
333 for (int i = 0; i < numRegs; i++) {
buzbee5de34942012-03-01 14:51:57 -0800334 PromotionMap vRegMap = cUnit->promotionMap[i];
buzbee9c044ce2012-03-18 13:24:07 -0700335 char buf[BSZ];
buzbee5de34942012-03-01 14:51:57 -0800336 if (vRegMap.fpLocation == kLocPhysReg) {
337 snprintf(buf, 100, " : s%d", vRegMap.fpReg & FP_REG_MASK);
338 } else {
339 buf[0] = 0;
340 }
buzbee9c044ce2012-03-18 13:24:07 -0700341 char buf2[BSZ];
342 char buf3[BSZ];
343 if (i < cUnit->numDalvikRegisters) {
344 snprintf(buf3, BSZ, "%02d", i);
345 } else if (i == cUnit->methodSReg) {
346 strncpy(buf3, "Method*", BSZ);
347 } else {
348 snprintf(buf3, BSZ, "ct%d", i - cUnit->numDalvikRegisters);
349 }
350
351 snprintf(buf2, BSZ, "V[%s] -> %s%d%s", buf3,
buzbee5de34942012-03-01 14:51:57 -0800352 vRegMap.coreLocation == kLocPhysReg ?
353 "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ?
354 vRegMap.coreReg : oatSRegOffset(cUnit, i), buf);
355 LOG(INFO) << buf2;
356 }
357}
358
buzbee5de34942012-03-01 14:51:57 -0800359/* Dump instructions and constant pool contents */
360void oatCodegenDump(CompilationUnit* cUnit)
361{
362 LOG(INFO) << "/*";
363 LOG(INFO) << "Dumping LIR insns for "
364 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
365 LIR* lirInsn;
366 LIR* thisLIR;
367 int insnsSize = cUnit->insnsSize;
368
369 LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs;
370 LOG(INFO) << "Ins : " << cUnit->numIns;
371 LOG(INFO) << "Outs : " << cUnit->numOuts;
372 LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills;
373 LOG(INFO) << "FPSpills : " << cUnit->numFPSpills;
buzbee239c4e72012-03-16 08:42:29 -0700374 LOG(INFO) << "CompilerTemps : " << cUnit->numCompilerTemps;
buzbee5de34942012-03-01 14:51:57 -0800375 LOG(INFO) << "Frame size : " << cUnit->frameSize;
buzbee5de34942012-03-01 14:51:57 -0800376 LOG(INFO) << "code size is " << cUnit->totalSize <<
377 " bytes, Dalvik size is " << insnsSize * 2;
378 LOG(INFO) << "expansion factor: " <<
379 (float)cUnit->totalSize / (float)(insnsSize * 2);
380 oatDumpPromotionMap(cUnit);
381 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
382 oatDumpLIRInsn(cUnit, lirInsn, 0);
383 }
384 for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) {
385 thisLIR = (LIR*) lirInsn;
386 LOG(INFO) << StringPrintf("%x (%04x): .class (%s)",
387 thisLIR->offset, thisLIR->offset,
388 ((CallsiteInfo *) thisLIR->operands[0])->classDescriptor);
389 }
390 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
391 thisLIR = (LIR*) lirInsn;
392 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)",
393 thisLIR->offset, thisLIR->offset, thisLIR->operands[0]);
394 }
395
396 const DexFile::MethodId& method_id =
397 cUnit->dex_file->GetMethodId(cUnit->method_idx);
398 std::string signature(cUnit->dex_file->GetMethodSignature(method_id));
399 std::string name(cUnit->dex_file->GetMethodName(method_id));
400 std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id));
401
402 // Dump mapping table
403 if (cUnit->mappingTable.size() > 0) {
404 std::string line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {",
405 descriptor.c_str(), name.c_str(), signature.c_str(), cUnit->mappingTable.size()));
406 std::replace(line.begin(), line.end(), ';', '_');
407 LOG(INFO) << line;
408 for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) {
buzbee82488f52012-03-02 08:20:26 -0800409 line = StringPrintf(" {0x%05x, 0x%04x},",
buzbee5de34942012-03-01 14:51:57 -0800410 cUnit->mappingTable[i], cUnit->mappingTable[i+1]);
411 LOG(INFO) << line;
412 }
413 LOG(INFO) <<" };\n\n";
414 }
415}
416
buzbeea2ebdd72012-03-04 14:57:06 -0800417
418LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800419 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800420{
421 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
422 insn->dalvikOffset = dalvikOffset;
423 insn->opcode = opcode;
424 insn->operands[0] = op0;
425 insn->operands[1] = op1;
426 insn->operands[2] = op2;
427 insn->operands[3] = op3;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800428 insn->operands[4] = op4;
buzbeea2ebdd72012-03-04 14:57:06 -0800429 insn->target = target;
430 oatSetupResourceMasks(insn);
431 if (opcode == kPseudoTargetLabel) {
432 // Always make labels scheduling barriers
433 insn->defMask = ENCODE_ALL;
434 }
435 return insn;
436}
437
buzbee5de34942012-03-01 14:51:57 -0800438/*
buzbee31a4a6f2012-02-28 15:36:15 -0800439 * The following are building blocks to construct low-level IRs with 0 - 4
440 * operands.
441 */
buzbee5de34942012-03-01 14:51:57 -0800442LIR* newLIR0(CompilationUnit* cUnit, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800443{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800444 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND))
445 << EncodingMap[opcode].name << " " << (int)opcode << " "
446 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
447 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800448 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode);
buzbee31a4a6f2012-02-28 15:36:15 -0800449 oatAppendLIR(cUnit, (LIR*) insn);
450 return insn;
451}
452
buzbee5de34942012-03-01 14:51:57 -0800453LIR* newLIR1(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800454 int dest)
455{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800456 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP))
457 << EncodingMap[opcode].name << " " << (int)opcode << " "
458 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
459 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800460 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest);
buzbee31a4a6f2012-02-28 15:36:15 -0800461 oatAppendLIR(cUnit, (LIR*) insn);
462 return insn;
463}
464
buzbee5de34942012-03-01 14:51:57 -0800465LIR* newLIR2(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800466 int dest, int src1)
467{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800468 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_BINARY_OP))
469 << EncodingMap[opcode].name << " " << (int)opcode << " "
470 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
471 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800472 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1);
buzbee31a4a6f2012-02-28 15:36:15 -0800473 oatAppendLIR(cUnit, (LIR*) insn);
474 return insn;
475}
476
buzbee5de34942012-03-01 14:51:57 -0800477LIR* newLIR3(CompilationUnit* cUnit, int opcode,
buzbee31a4a6f2012-02-28 15:36:15 -0800478 int dest, int src1, int src2)
479{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800480 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_TERTIARY_OP))
481 << EncodingMap[opcode].name << " " << (int)opcode << " "
482 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
483 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800484 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
485 src2);
buzbee31a4a6f2012-02-28 15:36:15 -0800486 oatAppendLIR(cUnit, (LIR*) insn);
487 return insn;
488}
489
buzbee5de34942012-03-01 14:51:57 -0800490LIR* newLIR4(CompilationUnit* cUnit, int opcode,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800491 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800492{
Ian Rogersb5d09b22012-03-06 22:14:17 -0800493 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUAD_OP))
494 << EncodingMap[opcode].name << " " << (int)opcode << " "
495 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
496 << cUnit->currentDalvikOffset;
buzbeea2ebdd72012-03-04 14:57:06 -0800497 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
498 src2, info);
buzbee31a4a6f2012-02-28 15:36:15 -0800499 oatAppendLIR(cUnit, (LIR*) insn);
500 return insn;
501}
buzbee31a4a6f2012-02-28 15:36:15 -0800502
Ian Rogersb5d09b22012-03-06 22:14:17 -0800503LIR* newLIR5(CompilationUnit* cUnit, int opcode,
504 int dest, int src1, int src2, int info1, int info2)
505{
506 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUIN_OP))
507 << EncodingMap[opcode].name << " " << (int)opcode << " "
508 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
509 << cUnit->currentDalvikOffset;
510 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
511 src2, info1, info2);
512 oatAppendLIR(cUnit, (LIR*) insn);
513 return insn;
514}
515
buzbee31a4a6f2012-02-28 15:36:15 -0800516/*
517 * Search the existing constants in the literal pool for an exact or close match
518 * within specified delta (greater or equal to 0).
519 */
520LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
521{
522 while (dataTarget) {
523 if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <=
524 delta)
525 return (LIR* ) dataTarget;
526 dataTarget = dataTarget->next;
527 }
528 return NULL;
529}
530
531/* Search the existing constants in the literal pool for an exact wide match */
532LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
533{
534 bool loMatch = false;
535 LIR* loTarget = NULL;
536 while (dataTarget) {
537 if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) {
538 return (LIR*)loTarget;
539 }
540 loMatch = false;
541 if (((LIR*)dataTarget)->operands[0] == valLo) {
542 loMatch = true;
543 loTarget = dataTarget;
544 }
545 dataTarget = dataTarget->next;
546 }
547 return NULL;
548}
549
550/*
551 * The following are building blocks to insert constants into the pool or
552 * instruction streams.
553 */
554
buzbee5de34942012-03-01 14:51:57 -0800555/* Add a 32-bit constant either in the constant pool */
Ian Rogers3fa13792012-03-18 15:53:45 -0700556LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800557{
558 /* Add the constant to the literal pool */
559 if (constantListP) {
560 LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true,
561 kAllocData);
562 newValue->operands[0] = value;
563 newValue->next = *constantListP;
564 *constantListP = (LIR*) newValue;
565 return newValue;
buzbee31a4a6f2012-02-28 15:36:15 -0800566 }
567 return NULL;
568}
569
570/* Add a 64-bit constant to the constant pool or mixed with code */
571LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
572 int valLo, int valHi)
573{
buzbee31a4a6f2012-02-28 15:36:15 -0800574 //FIXME: hard-coded little endian, need BE variant
buzbee5de34942012-03-01 14:51:57 -0800575 // Insert high word into list first
576 addWordData(cUnit, constantListP, valHi);
577 return addWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800578}
579
Ian Rogersab058bb2012-03-11 22:19:38 -0700580void pushWord(std::vector<uint8_t>&buf, int data) {
581 buf.push_back( data & 0xff);
582 buf.push_back( (data >> 8) & 0xff);
583 buf.push_back( (data >> 16) & 0xff);
584 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800585}
586
Ian Rogersab058bb2012-03-11 22:19:38 -0700587void alignBuffer(std::vector<uint8_t>&buf, size_t offset) {
588 while (buf.size() < offset) {
buzbeee3acd072012-02-25 17:03:10 -0800589 buf.push_back(0);
Ian Rogersab058bb2012-03-11 22:19:38 -0700590 }
buzbeee3acd072012-02-25 17:03:10 -0800591}
592
Brian Carlstromf5822582012-03-19 22:34:31 -0700593bool IsDirect(int invokeType) {
594 InvokeType type = static_cast<InvokeType>(invokeType);
595 return type == kStatic || type == kDirect;
596}
597
buzbeee3acd072012-02-25 17:03:10 -0800598/* Write the literal pool to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800599void installLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800600{
601 alignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
Ian Rogers3fa13792012-03-18 15:53:45 -0700602 LIR* dataLIR = cUnit->literalList;
buzbeee3acd072012-02-25 17:03:10 -0800603 while (dataLIR != NULL) {
604 pushWord(cUnit->codeBuffer, dataLIR->operands[0]);
605 dataLIR = NEXT_LIR(dataLIR);
606 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700607 // Push code and method literals, record offsets for the compiler to patch.
608 dataLIR = cUnit->codeLiteralList;
609 if (dataLIR != NULL) {
610 while (dataLIR != NULL) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700611 uint32_t target = dataLIR->operands[0];
Ian Rogers3fa13792012-03-18 15:53:45 -0700612 cUnit->compiler->AddCodePatch(cUnit->dex_cache, cUnit->dex_file,
613 cUnit->method_idx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700614 cUnit->access_flags,
615 target,
616 IsDirect(dataLIR->operands[1]),
Ian Rogers3fa13792012-03-18 15:53:45 -0700617 cUnit->codeBuffer.size());
Brian Carlstromf5822582012-03-19 22:34:31 -0700618 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
619 // unique based on target to ensure code deduplication works
620 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
621 pushWord(cUnit->codeBuffer, unique_patch_value);
Ian Rogers3fa13792012-03-18 15:53:45 -0700622 dataLIR = NEXT_LIR(dataLIR);
623 }
624 dataLIR = cUnit->methodLiteralList;
625 while (dataLIR != NULL) {
Brian Carlstromf5822582012-03-19 22:34:31 -0700626 uint32_t target = dataLIR->operands[0];
Ian Rogers3fa13792012-03-18 15:53:45 -0700627 cUnit->compiler->AddMethodPatch(cUnit->dex_cache, cUnit->dex_file,
628 cUnit->method_idx,
Brian Carlstromf5822582012-03-19 22:34:31 -0700629 cUnit->access_flags,
630 target,
631 IsDirect(dataLIR->operands[1]),
Ian Rogers3fa13792012-03-18 15:53:45 -0700632 cUnit->codeBuffer.size());
Brian Carlstromf5822582012-03-19 22:34:31 -0700633 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
634 // unique based on target to ensure code deduplication works
635 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
636 pushWord(cUnit->codeBuffer, unique_patch_value);
Ian Rogers3fa13792012-03-18 15:53:45 -0700637 dataLIR = NEXT_LIR(dataLIR);
638 }
639 }
640
buzbeee3acd072012-02-25 17:03:10 -0800641}
642
643/* Write the switch tables to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800644void installSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800645{
646 GrowableListIterator iterator;
647 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
648 while (true) {
649 SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext(
650 &iterator);
651 if (tabRec == NULL) break;
652 alignBuffer(cUnit->codeBuffer, tabRec->offset);
buzbeec5159d52012-03-03 11:48:39 -0800653 /*
654 * For Arm, our reference point is the address of the bx
655 * instruction that does the launch, so we have to subtract
656 * the auto pc-advance. For other targets the reference point
657 * is a label, so we can use the offset as-is.
658 */
659#if defined(TARGET_ARM)
660 int bxOffset = tabRec->anchor->offset + 4;
661#else
662 int bxOffset = tabRec->anchor->offset;
663#endif
buzbeee3acd072012-02-25 17:03:10 -0800664 if (cUnit->printMe) {
665 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
666 }
Elliott Hughesadb8c672012-03-06 16:49:32 -0800667 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeee3acd072012-02-25 17:03:10 -0800668 int* keys = (int*)&(tabRec->table[2]);
669 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800670 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800671 if (cUnit->printMe) {
672 LOG(INFO) << " Case[" << elems << "] key: 0x" <<
673 std::hex << keys[elems] << ", disp: 0x" <<
674 std::hex << disp;
675 }
676 pushWord(cUnit->codeBuffer, keys[elems]);
677 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800678 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800679 }
680 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800681 DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeee3acd072012-02-25 17:03:10 -0800682 for (int elems = 0; elems < tabRec->table[1]; elems++) {
buzbee31a4a6f2012-02-28 15:36:15 -0800683 int disp = tabRec->targets[elems]->offset - bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800684 if (cUnit->printMe) {
685 LOG(INFO) << " Case[" << elems << "] disp: 0x" <<
686 std::hex << disp;
687 }
688 pushWord(cUnit->codeBuffer,
buzbee31a4a6f2012-02-28 15:36:15 -0800689 tabRec->targets[elems]->offset - bxOffset);
buzbeee3acd072012-02-25 17:03:10 -0800690 }
691 }
692 }
693}
694
695/* Write the fill array dta to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800696void installFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800697{
698 GrowableListIterator iterator;
699 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
700 while (true) {
701 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
702 &iterator);
703 if (tabRec == NULL) break;
704 alignBuffer(cUnit->codeBuffer, tabRec->offset);
Ian Rogersab058bb2012-03-11 22:19:38 -0700705 for (int i = 0; i < (tabRec->size + 1) / 2; i++) {
706 cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF);
707 cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800708 }
709 }
710}
711
buzbee31a4a6f2012-02-28 15:36:15 -0800712int assignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800713{
714 for (;lir != NULL; lir = lir->next) {
715 lir->offset = offset;
716 offset += 4;
717 }
718 return offset;
719}
720
buzbee31a4a6f2012-02-28 15:36:15 -0800721void createMappingTable(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800722{
buzbee31a4a6f2012-02-28 15:36:15 -0800723 LIR* tgtLIR;
buzbeee3acd072012-02-25 17:03:10 -0800724 int currentDalvikOffset = -1;
725
buzbee31a4a6f2012-02-28 15:36:15 -0800726 for (tgtLIR = (LIR *) cUnit->firstLIRInsn;
buzbeee3acd072012-02-25 17:03:10 -0800727 tgtLIR;
728 tgtLIR = NEXT_LIR(tgtLIR)) {
729 if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop &&
buzbee31a4a6f2012-02-28 15:36:15 -0800730 (currentDalvikOffset != tgtLIR->dalvikOffset)) {
buzbeee3acd072012-02-25 17:03:10 -0800731 // Changed - need to emit a record
buzbee31a4a6f2012-02-28 15:36:15 -0800732 cUnit->mappingTable.push_back(tgtLIR->offset);
733 cUnit->mappingTable.push_back(tgtLIR->dalvikOffset);
734 currentDalvikOffset = tgtLIR->dalvikOffset;
buzbeee3acd072012-02-25 17:03:10 -0800735 }
736 }
737}
738
739/* Determine the offset of each literal field */
buzbee31a4a6f2012-02-28 15:36:15 -0800740int assignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800741{
742 offset = assignLiteralOffsetCommon(cUnit->literalList, offset);
Ian Rogers3fa13792012-03-18 15:53:45 -0700743 offset = assignLiteralOffsetCommon(cUnit->codeLiteralList, offset);
744 offset = assignLiteralOffsetCommon(cUnit->methodLiteralList, offset);
buzbeee3acd072012-02-25 17:03:10 -0800745 return offset;
746}
747
buzbee31a4a6f2012-02-28 15:36:15 -0800748int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800749{
750 GrowableListIterator iterator;
751 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
752 while (true) {
753 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
754 &iterator);
755 if (tabRec == NULL) break;
756 tabRec->offset = offset;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800757 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbeee3acd072012-02-25 17:03:10 -0800758 offset += tabRec->table[1] * (sizeof(int) * 2);
759 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800760 DCHECK_EQ(static_cast<int>(tabRec->table[0]), static_cast<int>(Instruction::kPackedSwitchSignature));
buzbeee3acd072012-02-25 17:03:10 -0800761 offset += tabRec->table[1] * sizeof(int);
762 }
763 }
764 return offset;
765}
766
buzbee31a4a6f2012-02-28 15:36:15 -0800767int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800768{
769 GrowableListIterator iterator;
770 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
771 while (true) {
772 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
773 &iterator);
774 if (tabRec == NULL) break;
775 tabRec->offset = offset;
776 offset += tabRec->size;
777 // word align
778 offset = (offset + 3) & ~3;
779 }
780 return offset;
781}
782
783/*
784 * Walk the compilation unit and assign offsets to instructions
785 * and literals and compute the total size of the compiled unit.
786 */
787void oatAssignOffsets(CompilationUnit* cUnit)
788{
789 int offset = oatAssignInsnOffsets(cUnit);
790
791 /* Const values have to be word aligned */
792 offset = (offset + 3) & ~3;
793
794 /* Set up offsets for literals */
795 cUnit->dataOffset = offset;
796
797 offset = assignLiteralOffset(cUnit, offset);
798
799 offset = assignSwitchTablesOffset(cUnit, offset);
800
801 offset = assignFillArrayDataOffset(cUnit, offset);
802
803 cUnit->totalSize = offset;
804}
805
806/*
807 * Go over each instruction in the list and calculate the offset from the top
808 * before sending them off to the assembler. If out-of-range branch distance is
809 * seen rearrange the instructions a bit to correct it.
810 */
811void oatAssembleLIR(CompilationUnit* cUnit)
812{
813 oatAssignOffsets(cUnit);
814 /*
815 * Assemble here. Note that we generate code with optimistic assumptions
816 * and if found now to work, we'll have to redo the sequence and retry.
817 */
818
819 while (true) {
820 AssemblerStatus res = oatAssembleInstructions(cUnit, 0);
821 if (res == kSuccess) {
822 break;
823 } else {
824 cUnit->assemblerRetries++;
825 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
826 LOG(FATAL) << "Assembler error - too many retries";
827 }
828 // Redo offsets and try again
829 oatAssignOffsets(cUnit);
830 cUnit->codeBuffer.clear();
831 }
832 }
833
834 // Install literals
835 installLiteralPools(cUnit);
836
837 // Install switch tables
838 installSwitchTables(cUnit);
839
840 // Install fill array data
841 installFillArrayData(cUnit);
842
843 /*
844 * Create the mapping table
845 */
846 createMappingTable(cUnit);
847}
848
buzbee31a4a6f2012-02-28 15:36:15 -0800849/*
850 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
851 * offset vaddr. This label will be used to fix up the case
852 * branch table during the assembly phase. Be sure to set
853 * all resource flags on this to prevent code motion across
854 * target boundaries. KeyVal is just there for debugging.
855 */
856LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
857{
858 std::map<unsigned int, LIR*>::iterator it;
859 it = cUnit->boundaryMap.find(vaddr);
860 if (it == cUnit->boundaryMap.end()) {
861 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
862 }
863 LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
864 newLabel->dalvikOffset = vaddr;
865 newLabel->opcode = kPseudoCaseLabel;
866 newLabel->operands[0] = keyVal;
867 oatInsertLIRAfter(it->second, (LIR*)newLabel);
868 return newLabel;
869}
870
871void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
872{
873 const u2* table = tabRec->table;
874 int baseVaddr = tabRec->vaddr;
875 int *targets = (int*)&table[4];
876 int entries = table[1];
877 int lowKey = s4FromSwitchData(&table[2]);
878 for (int i = 0; i < entries; i++) {
879 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
880 i + lowKey);
881 }
882}
883
884void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
885{
886 const u2* table = tabRec->table;
887 int baseVaddr = tabRec->vaddr;
888 int entries = table[1];
889 int* keys = (int*)&table[2];
890 int* targets = &keys[entries];
891 for (int i = 0; i < entries; i++) {
892 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
893 keys[i]);
894 }
895}
896
897void oatProcessSwitchTables(CompilationUnit* cUnit)
898{
899 GrowableListIterator iterator;
900 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
901 while (true) {
902 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
903 &iterator);
904 if (tabRec == NULL) break;
Elliott Hughesadb8c672012-03-06 16:49:32 -0800905 if (tabRec->table[0] == Instruction::kPackedSwitchSignature) {
buzbee31a4a6f2012-02-28 15:36:15 -0800906 markPackedCaseLabels(cUnit, tabRec);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800907 } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
buzbee31a4a6f2012-02-28 15:36:15 -0800908 markSparseCaseLabels(cUnit, tabRec);
Elliott Hughesadb8c672012-03-06 16:49:32 -0800909 } else {
buzbee31a4a6f2012-02-28 15:36:15 -0800910 LOG(FATAL) << "Invalid switch table";
911 }
912 }
913}
914
915//FIXME: Do we have endian issues here?
916
917void dumpSparseSwitchTable(const u2* table)
918 /*
919 * Sparse switch data format:
920 * ushort ident = 0x0200 magic value
921 * ushort size number of entries in the table; > 0
922 * int keys[size] keys, sorted low-to-high; 32-bit aligned
923 * int targets[size] branch targets, relative to switch opcode
924 *
925 * Total size is (2+size*4) 16-bit code units.
926 */
927{
928 u2 ident = table[0];
929 int entries = table[1];
930 int* keys = (int*)&table[2];
931 int* targets = &keys[entries];
932 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident <<
933 ", entries: " << std::dec << entries;
934 for (int i = 0; i < entries; i++) {
935 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex <<
936 targets[i];
937 }
938}
939
940void dumpPackedSwitchTable(const u2* table)
941 /*
942 * Packed switch data format:
943 * ushort ident = 0x0100 magic value
944 * ushort size number of entries in the table
945 * int first_key first (and lowest) switch case value
946 * int targets[size] branch targets, relative to switch opcode
947 *
948 * Total size is (4+size*2) 16-bit code units.
949 */
950{
951 u2 ident = table[0];
952 int* targets = (int*)&table[4];
953 int entries = table[1];
954 int lowKey = s4FromSwitchData(&table[2]);
955 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident <<
956 ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
957 for (int i = 0; i < entries; i++) {
958 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex <<
959 targets[i];
960 }
961}
buzbeee3acd072012-02-25 17:03:10 -0800962
963
964} // namespace art