blob: dd7daba1d61061ce7803ca7e349b94e838d5d6e2 [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{
Bill Buzbeea114add2012-05-03 15:00:40 -070021 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 }
buzbee31a4a6f2012-02-28 15:36:15 -080051}
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{
Bill Buzbeea114add2012-05-03 15:00:40 -070058 setMemRefType(lir, isLoad, kDalvikReg);
buzbee31a4a6f2012-02-28 15:36:15 -080059
Bill Buzbeea114add2012-05-03 15:00:40 -070060 /*
61 * Store the Dalvik register id in aliasInfo. Mark the MSB if it is a 64-bit
62 * access.
63 */
64 lir->aliasInfo = regId;
65 if (is64bit) {
66 lir->aliasInfo |= 0x80000000;
67 }
buzbee31a4a6f2012-02-28 15:36:15 -080068}
69
70/*
71 * Decode the register id.
72 */
73inline u8 getRegMaskCommon(int reg)
74{
Bill Buzbeea114add2012-05-03 15:00:40 -070075 u8 seed;
76 int shift;
buzbee31a4a6f2012-02-28 15:36:15 -080077
jeffhaoe2962482012-06-28 11:29:57 -070078#if defined(TARGET_X86)
jeffhao854029c2012-07-23 17:31:30 -070079 int regId = reg & 0xf;
jeffhaoe2962482012-06-28 11:29:57 -070080 /*
81 * Double registers in x86 are just a single FP register
82 */
83 seed = 1;
84#else
jeffhao854029c2012-07-23 17:31:30 -070085 int regId = reg & 0x1f;
Bill Buzbeea114add2012-05-03 15:00:40 -070086 /*
87 * Each double register is equal to a pair of single-precision FP registers
88 */
89 seed = DOUBLEREG(reg) ? 3 : 1;
jeffhaoe2962482012-06-28 11:29:57 -070090#endif
Bill Buzbeea114add2012-05-03 15:00:40 -070091 /* FP register starts at bit position 16 */
92 shift = FPREG(reg) ? kFPReg0 : 0;
93 /* Expand the double register id into single offset */
94 shift += regId;
95 return (seed << shift);
buzbee31a4a6f2012-02-28 15:36:15 -080096}
97
98/*
99 * Mark the corresponding bit(s).
100 */
101inline void setupRegMask(u8* mask, int reg)
102{
Bill Buzbeea114add2012-05-03 15:00:40 -0700103 *mask |= getRegMaskCommon(reg);
buzbee31a4a6f2012-02-28 15:36:15 -0800104}
105
106/*
107 * Set up the proper fields in the resource mask
108 */
109void setupResourceMasks(LIR* lir)
110{
Bill Buzbeea114add2012-05-03 15:00:40 -0700111 int opcode = lir->opcode;
112 int flags;
buzbee31a4a6f2012-02-28 15:36:15 -0800113
Bill Buzbeea114add2012-05-03 15:00:40 -0700114 if (opcode <= 0) {
115 lir->useMask = lir->defMask = 0;
116 return;
117 }
buzbee31a4a6f2012-02-28 15:36:15 -0800118
Bill Buzbeea114add2012-05-03 15:00:40 -0700119 flags = EncodingMap[lir->opcode].flags;
buzbee31a4a6f2012-02-28 15:36:15 -0800120
Bill Buzbeea114add2012-05-03 15:00:40 -0700121 if (flags & NEEDS_FIXUP) {
122 lir->flags.pcRelFixup = true;
123 }
buzbee31a4a6f2012-02-28 15:36:15 -0800124
Bill Buzbeea114add2012-05-03 15:00:40 -0700125 /* Get the starting size of the instruction's template */
126 lir->flags.size = oatGetInsnSize(lir);
buzbeee88dfbf2012-03-05 11:19:57 -0800127
Bill Buzbeea114add2012-05-03 15:00:40 -0700128 /* Set up the mask for resources that are updated */
129 if (flags & (IS_LOAD | IS_STORE)) {
130 /* Default to heap - will catch specialized classes later */
131 setMemRefType(lir, flags & IS_LOAD, kHeapRef);
132 }
buzbee31a4a6f2012-02-28 15:36:15 -0800133
Bill Buzbeea114add2012-05-03 15:00:40 -0700134 /*
135 * Conservatively assume the branch here will call out a function that in
136 * turn will trash everything.
137 */
138 if (flags & IS_BRANCH) {
139 lir->defMask = lir->useMask = ENCODE_ALL;
140 return;
141 }
buzbee31a4a6f2012-02-28 15:36:15 -0800142
Bill Buzbeea114add2012-05-03 15:00:40 -0700143 if (flags & REG_DEF0) {
144 setupRegMask(&lir->defMask, lir->operands[0]);
145 }
buzbee31a4a6f2012-02-28 15:36:15 -0800146
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 if (flags & REG_DEF1) {
148 setupRegMask(&lir->defMask, lir->operands[1]);
149 }
buzbee31a4a6f2012-02-28 15:36:15 -0800150
jeffhaoe2962482012-06-28 11:29:57 -0700151#if defined(TARGET_X86)
152 if (flags & REG_DEFA) {
153 setupRegMask(&lir->defMask, rAX);
154 }
155
156 if (flags & REG_DEFD) {
157 setupRegMask(&lir->defMask, rDX);
158 }
159#endif
160
Bill Buzbeea114add2012-05-03 15:00:40 -0700161 if (flags & REG_DEF_SP) {
162 lir->defMask |= ENCODE_REG_SP;
163 }
buzbee31a4a6f2012-02-28 15:36:15 -0800164
buzbeea7678db2012-03-05 15:35:46 -0800165#if !defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700166 if (flags & REG_DEF_LR) {
167 lir->defMask |= ENCODE_REG_LR;
168 }
buzbeea7678db2012-03-05 15:35:46 -0800169#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800170
jeffhaoe2962482012-06-28 11:29:57 -0700171#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700172 if (flags & REG_DEF_LIST0) {
173 lir->defMask |= ENCODE_REG_LIST(lir->operands[0]);
174 }
buzbee31a4a6f2012-02-28 15:36:15 -0800175
Bill Buzbeea114add2012-05-03 15:00:40 -0700176 if (flags & REG_DEF_LIST1) {
177 lir->defMask |= ENCODE_REG_LIST(lir->operands[1]);
178 }
buzbee31a4a6f2012-02-28 15:36:15 -0800179
Bill Buzbeea114add2012-05-03 15:00:40 -0700180 if (flags & REG_DEF_FPCS_LIST0) {
181 lir->defMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
182 }
buzbee31a4a6f2012-02-28 15:36:15 -0800183
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 if (flags & REG_DEF_FPCS_LIST2) {
185 for (int i = 0; i < lir->operands[2]; i++) {
186 setupRegMask(&lir->defMask, lir->operands[1] + i);
buzbee31a4a6f2012-02-28 15:36:15 -0800187 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700188 }
buzbee5de34942012-03-01 14:51:57 -0800189#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800190
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 if (flags & SETS_CCODES) {
192 lir->defMask |= ENCODE_CCODE;
193 }
buzbee31a4a6f2012-02-28 15:36:15 -0800194
195#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700196 /* Conservatively treat the IT block */
197 if (flags & IS_IT) {
198 lir->defMask = ENCODE_ALL;
199 }
buzbee31a4a6f2012-02-28 15:36:15 -0800200#endif
201
Bill Buzbeea114add2012-05-03 15:00:40 -0700202 if (flags & (REG_USE0 | REG_USE1 | REG_USE2 | REG_USE3)) {
203 int i;
buzbee31a4a6f2012-02-28 15:36:15 -0800204
Bill Buzbeea114add2012-05-03 15:00:40 -0700205 for (i = 0; i < 4; i++) {
206 if (flags & (1 << (kRegUse0 + i))) {
207 setupRegMask(&lir->useMask, lir->operands[i]);
208 }
buzbee31a4a6f2012-02-28 15:36:15 -0800209 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700210 }
buzbee31a4a6f2012-02-28 15:36:15 -0800211
jeffhaoe2962482012-06-28 11:29:57 -0700212#if defined(TARGET_X86)
213 if (flags & REG_USEA) {
214 setupRegMask(&lir->useMask, rAX);
215 }
216
217 if (flags & REG_USEC) {
218 setupRegMask(&lir->useMask, rCX);
219 }
220
221 if (flags & REG_USED) {
222 setupRegMask(&lir->useMask, rDX);
223 }
224#endif
225
buzbeea7678db2012-03-05 15:35:46 -0800226#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700227 if (flags & REG_USE_PC) {
228 lir->useMask |= ENCODE_REG_PC;
229 }
buzbeea7678db2012-03-05 15:35:46 -0800230#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800231
Bill Buzbeea114add2012-05-03 15:00:40 -0700232 if (flags & REG_USE_SP) {
233 lir->useMask |= ENCODE_REG_SP;
234 }
buzbee31a4a6f2012-02-28 15:36:15 -0800235
jeffhaoe2962482012-06-28 11:29:57 -0700236#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700237 if (flags & REG_USE_LIST0) {
238 lir->useMask |= ENCODE_REG_LIST(lir->operands[0]);
239 }
buzbee31a4a6f2012-02-28 15:36:15 -0800240
Bill Buzbeea114add2012-05-03 15:00:40 -0700241 if (flags & REG_USE_LIST1) {
242 lir->useMask |= ENCODE_REG_LIST(lir->operands[1]);
243 }
buzbee31a4a6f2012-02-28 15:36:15 -0800244
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 if (flags & REG_USE_FPCS_LIST0) {
246 lir->useMask |= ENCODE_REG_FPCS_LIST(lir->operands[0]);
247 }
buzbee31a4a6f2012-02-28 15:36:15 -0800248
Bill Buzbeea114add2012-05-03 15:00:40 -0700249 if (flags & REG_USE_FPCS_LIST2) {
250 for (int i = 0; i < lir->operands[2]; i++) {
251 setupRegMask(&lir->useMask, lir->operands[1] + i);
buzbee31a4a6f2012-02-28 15:36:15 -0800252 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700253 }
buzbee5de34942012-03-01 14:51:57 -0800254#endif
buzbee31a4a6f2012-02-28 15:36:15 -0800255
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 if (flags & USES_CCODES) {
257 lir->useMask |= ENCODE_CCODE;
258 }
buzbee31a4a6f2012-02-28 15:36:15 -0800259
260#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700261 /* Fixup for kThumbPush/lr and kThumbPop/pc */
262 if (opcode == kThumbPush || opcode == kThumbPop) {
263 u8 r8Mask = getRegMaskCommon(r8);
264 if ((opcode == kThumbPush) && (lir->useMask & r8Mask)) {
265 lir->useMask &= ~r8Mask;
266 lir->useMask |= ENCODE_REG_LR;
267 } else if ((opcode == kThumbPop) && (lir->defMask & r8Mask)) {
268 lir->defMask &= ~r8Mask;
269 lir->defMask |= ENCODE_REG_PC;
buzbee31a4a6f2012-02-28 15:36:15 -0800270 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 }
buzbee31a4a6f2012-02-28 15:36:15 -0800272#endif
273}
274
275/*
buzbee5de34942012-03-01 14:51:57 -0800276 * Debugging macros
277 */
278#define DUMP_RESOURCE_MASK(X)
279#define DUMP_SSA_REP(X)
280
281/* Pretty-print a LIR instruction */
282void oatDumpLIRInsn(CompilationUnit* cUnit, LIR* arg, unsigned char* baseAddr)
283{
Bill Buzbeea114add2012-05-03 15:00:40 -0700284 LIR* lir = (LIR*) arg;
285 int offset = lir->offset;
286 int dest = lir->operands[0];
287 const bool dumpNop = (cUnit->enableDebug & (1 << kDebugShowNops));
buzbee5de34942012-03-01 14:51:57 -0800288
Bill Buzbeea114add2012-05-03 15:00:40 -0700289 /* Handle pseudo-ops individually, and all regular insns as a group */
290 switch (lir->opcode) {
291 case kPseudoMethodEntry:
292 LOG(INFO) << "-------- method entry "
293 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
294 break;
295 case kPseudoMethodExit:
296 LOG(INFO) << "-------- Method_Exit";
297 break;
298 case kPseudoBarrier:
299 LOG(INFO) << "-------- BARRIER";
300 break;
301 case kPseudoExtended:
302 LOG(INFO) << "-------- " << (char* ) dest;
303 break;
304 case kPseudoSSARep:
305 DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << (char* ) dest);
306 break;
307 case kPseudoEntryBlock:
308 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
309 break;
310 case kPseudoDalvikByteCodeBoundary:
311 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex
312 << lir->dalvikOffset << " @ " << (char* )lir->operands[0];
313 break;
314 case kPseudoExitBlock:
315 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
316 break;
317 case kPseudoPseudoAlign4:
318 LOG(INFO) << (intptr_t)baseAddr + offset << " (0x" << std::hex
319 << offset << "): .align4";
320 break;
321 case kPseudoEHBlockLabel:
322 LOG(INFO) << "Exception_Handling:";
323 break;
324 case kPseudoTargetLabel:
325 case kPseudoNormalBlockLabel:
326 LOG(INFO) << "L" << (void*)lir << ":";
327 break;
328 case kPseudoThrowTarget:
329 LOG(INFO) << "LT" << (void*)lir << ":";
330 break;
331 case kPseudoIntrinsicRetry:
332 LOG(INFO) << "IR" << (void*)lir << ":";
333 break;
334 case kPseudoSuspendTarget:
335 LOG(INFO) << "LS" << (void*)lir << ":";
336 break;
337 case kPseudoCaseLabel:
338 LOG(INFO) << "LC" << (void*)lir << ": Case target 0x"
339 << std::hex << lir->operands[0] << "|" << std::dec <<
340 lir->operands[0];
341 break;
342 default:
343 if (lir->flags.isNop && !dumpNop) {
344 break;
345 } else {
346 std::string op_name(buildInsnString(EncodingMap[lir->opcode].name,
347 lir, baseAddr));
348 std::string op_operands(buildInsnString(EncodingMap[lir->opcode].fmt
349 , lir, baseAddr));
350 LOG(INFO) << StringPrintf("%05x: %-9s%s%s",
351 (unsigned int)(baseAddr + offset),
352 op_name.c_str(), op_operands.c_str(),
353 lir->flags.isNop ? "(nop)" : "");
354 }
355 break;
356 }
buzbee5de34942012-03-01 14:51:57 -0800357
Bill Buzbeea114add2012-05-03 15:00:40 -0700358 if (lir->useMask && (!lir->flags.isNop || dumpNop)) {
359 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir, lir->useMask, "use"));
360 }
361 if (lir->defMask && (!lir->flags.isNop || dumpNop)) {
362 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir, lir->defMask, "def"));
363 }
buzbee5de34942012-03-01 14:51:57 -0800364}
365
366void oatDumpPromotionMap(CompilationUnit *cUnit)
367{
Bill Buzbeea114add2012-05-03 15:00:40 -0700368 int numRegs = cUnit->numDalvikRegisters + cUnit->numCompilerTemps + 1;
369 for (int i = 0; i < numRegs; i++) {
370 PromotionMap vRegMap = cUnit->promotionMap[i];
371 std::string buf;
372 if (vRegMap.fpLocation == kLocPhysReg) {
373 StringAppendF(&buf, " : s%d", vRegMap.fpReg & FP_REG_MASK);
buzbee5de34942012-03-01 14:51:57 -0800374 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700375
376 std::string buf3;
377 if (i < cUnit->numDalvikRegisters) {
378 StringAppendF(&buf3, "%02d", i);
379 } else if (i == cUnit->methodSReg) {
380 buf3 = "Method*";
381 } else {
382 StringAppendF(&buf3, "ct%d", i - cUnit->numDalvikRegisters);
383 }
384
385 LOG(INFO) << StringPrintf("V[%s] -> %s%d%s", buf3.c_str(),
386 vRegMap.coreLocation == kLocPhysReg ?
387 "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ?
388 vRegMap.coreReg : oatSRegOffset(cUnit, i),
389 buf.c_str());
390 }
buzbee5de34942012-03-01 14:51:57 -0800391}
392
buzbee5de34942012-03-01 14:51:57 -0800393/* Dump instructions and constant pool contents */
394void oatCodegenDump(CompilationUnit* cUnit)
395{
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 LOG(INFO) << "Dumping LIR insns for "
397 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
398 LIR* lirInsn;
399 LIR* thisLIR;
400 int insnsSize = cUnit->insnsSize;
buzbee5de34942012-03-01 14:51:57 -0800401
Bill Buzbeea114add2012-05-03 15:00:40 -0700402 LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs;
403 LOG(INFO) << "Ins : " << cUnit->numIns;
404 LOG(INFO) << "Outs : " << cUnit->numOuts;
405 LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills;
406 LOG(INFO) << "FPSpills : " << cUnit->numFPSpills;
407 LOG(INFO) << "CompilerTemps : " << cUnit->numCompilerTemps;
408 LOG(INFO) << "Frame size : " << cUnit->frameSize;
409 LOG(INFO) << "code size is " << cUnit->totalSize <<
410 " bytes, Dalvik size is " << insnsSize * 2;
411 LOG(INFO) << "expansion factor: "
412 << (float)cUnit->totalSize / (float)(insnsSize * 2);
413 oatDumpPromotionMap(cUnit);
414 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
415 oatDumpLIRInsn(cUnit, lirInsn, 0);
416 }
417 for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) {
418 thisLIR = (LIR*) lirInsn;
419 LOG(INFO) << StringPrintf("%x (%04x): .class (%s)",
420 thisLIR->offset, thisLIR->offset,
421 ((CallsiteInfo *)
422 thisLIR->operands[0])->classDescriptor);
423 }
424 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
425 thisLIR = (LIR*) lirInsn;
426 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)",
427 thisLIR->offset, thisLIR->offset,
428 thisLIR->operands[0]);
429 }
buzbee5de34942012-03-01 14:51:57 -0800430
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 const DexFile::MethodId& method_id =
432 cUnit->dex_file->GetMethodId(cUnit->method_idx);
433 std::string signature(cUnit->dex_file->GetMethodSignature(method_id));
434 std::string name(cUnit->dex_file->GetMethodName(method_id));
435 std::string descriptor(cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id));
buzbee5de34942012-03-01 14:51:57 -0800436
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 // Dump mapping table
438 if (cUnit->mappingTable.size() > 0) {
439 std::string
440 line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%zu] = {",
441 descriptor.c_str(), name.c_str(), signature.c_str(),
442 cUnit->mappingTable.size()));
443 std::replace(line.begin(), line.end(), ';', '_');
444 LOG(INFO) << line;
445 for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) {
446 line = StringPrintf(" {0x%05x, 0x%04x},",
447 cUnit->mappingTable[i], cUnit->mappingTable[i+1]);
448 LOG(INFO) << line;
buzbee5de34942012-03-01 14:51:57 -0800449 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700450 LOG(INFO) <<" };\n\n";
451 }
buzbee5de34942012-03-01 14:51:57 -0800452}
453
buzbeea2ebdd72012-03-04 14:57:06 -0800454
455LIR* rawLIR(CompilationUnit* cUnit, int dalvikOffset, int opcode, int op0,
Bill Buzbeea114add2012-05-03 15:00:40 -0700456 int op1, int op2, int op3, int op4, LIR* target)
buzbeea2ebdd72012-03-04 14:57:06 -0800457{
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 LIR* insn = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
459 insn->dalvikOffset = dalvikOffset;
460 insn->opcode = opcode;
461 insn->operands[0] = op0;
462 insn->operands[1] = op1;
463 insn->operands[2] = op2;
464 insn->operands[3] = op3;
465 insn->operands[4] = op4;
466 insn->target = target;
467 oatSetupResourceMasks(insn);
468 if (opcode == kPseudoTargetLabel) {
469 // Always make labels scheduling barriers
470 insn->defMask = ENCODE_ALL;
471 }
472 return insn;
buzbeea2ebdd72012-03-04 14:57:06 -0800473}
474
buzbee5de34942012-03-01 14:51:57 -0800475/*
buzbee31a4a6f2012-02-28 15:36:15 -0800476 * The following are building blocks to construct low-level IRs with 0 - 4
477 * operands.
478 */
buzbee5de34942012-03-01 14:51:57 -0800479LIR* newLIR0(CompilationUnit* cUnit, int opcode)
buzbee31a4a6f2012-02-28 15:36:15 -0800480{
Bill Buzbeea114add2012-05-03 15:00:40 -0700481 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & NO_OPERAND))
482 << EncodingMap[opcode].name << " " << (int)opcode << " "
483 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
484 << cUnit->currentDalvikOffset;
485 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode);
486 oatAppendLIR(cUnit, (LIR*) insn);
487 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800488}
489
buzbee5de34942012-03-01 14:51:57 -0800490LIR* newLIR1(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700491 int dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800492{
Bill Buzbeea114add2012-05-03 15:00:40 -0700493 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_UNARY_OP))
494 << EncodingMap[opcode].name << " " << (int)opcode << " "
495 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
496 << cUnit->currentDalvikOffset;
497 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest);
498 oatAppendLIR(cUnit, (LIR*) insn);
499 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800500}
501
buzbee5de34942012-03-01 14:51:57 -0800502LIR* newLIR2(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 int dest, int src1)
buzbee31a4a6f2012-02-28 15:36:15 -0800504{
Bill Buzbeea114add2012-05-03 15:00:40 -0700505 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_BINARY_OP))
506 << EncodingMap[opcode].name << " " << (int)opcode << " "
507 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
508 << cUnit->currentDalvikOffset;
509 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1);
510 oatAppendLIR(cUnit, (LIR*) insn);
511 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800512}
513
buzbee5de34942012-03-01 14:51:57 -0800514LIR* newLIR3(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700515 int dest, int src1, int src2)
buzbee31a4a6f2012-02-28 15:36:15 -0800516{
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_TERTIARY_OP))
518 << EncodingMap[opcode].name << " " << (int)opcode << " "
519 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
520 << cUnit->currentDalvikOffset;
521 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
522 src2);
523 oatAppendLIR(cUnit, (LIR*) insn);
524 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800525}
526
buzbee5de34942012-03-01 14:51:57 -0800527LIR* newLIR4(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700528 int dest, int src1, int src2, int info)
buzbee31a4a6f2012-02-28 15:36:15 -0800529{
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUAD_OP))
531 << EncodingMap[opcode].name << " " << (int)opcode << " "
532 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
533 << cUnit->currentDalvikOffset;
534 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
535 src2, info);
536 oatAppendLIR(cUnit, (LIR*) insn);
537 return insn;
buzbee31a4a6f2012-02-28 15:36:15 -0800538}
buzbee31a4a6f2012-02-28 15:36:15 -0800539
Ian Rogersb5d09b22012-03-06 22:14:17 -0800540LIR* newLIR5(CompilationUnit* cUnit, int opcode,
Bill Buzbeea114add2012-05-03 15:00:40 -0700541 int dest, int src1, int src2, int info1, int info2)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800542{
Bill Buzbeea114add2012-05-03 15:00:40 -0700543 DCHECK(isPseudoOpcode(opcode) || (EncodingMap[opcode].flags & IS_QUIN_OP))
544 << EncodingMap[opcode].name << " " << (int)opcode << " "
545 << PrettyMethod(cUnit->method_idx, *cUnit->dex_file) << " "
546 << cUnit->currentDalvikOffset;
547 LIR* insn = rawLIR(cUnit, cUnit->currentDalvikOffset, opcode, dest, src1,
548 src2, info1, info2);
549 oatAppendLIR(cUnit, (LIR*) insn);
550 return insn;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800551}
552
buzbee31a4a6f2012-02-28 15:36:15 -0800553/*
554 * Search the existing constants in the literal pool for an exact or close match
555 * within specified delta (greater or equal to 0).
556 */
557LIR* scanLiteralPool(LIR* dataTarget, int value, unsigned int delta)
558{
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 while (dataTarget) {
560 if (((unsigned) (value - ((LIR* ) dataTarget)->operands[0])) <= delta)
561 return (LIR* ) dataTarget;
562 dataTarget = dataTarget->next;
563 }
564 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800565}
566
567/* Search the existing constants in the literal pool for an exact wide match */
568LIR* scanLiteralPoolWide(LIR* dataTarget, int valLo, int valHi)
569{
Bill Buzbeea114add2012-05-03 15:00:40 -0700570 bool loMatch = false;
571 LIR* loTarget = NULL;
572 while (dataTarget) {
573 if (loMatch && (((LIR*)dataTarget)->operands[0] == valHi)) {
574 return (LIR*)loTarget;
buzbee31a4a6f2012-02-28 15:36:15 -0800575 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700576 loMatch = false;
577 if (((LIR*)dataTarget)->operands[0] == valLo) {
578 loMatch = true;
579 loTarget = dataTarget;
580 }
581 dataTarget = dataTarget->next;
582 }
583 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800584}
585
586/*
587 * The following are building blocks to insert constants into the pool or
588 * instruction streams.
589 */
590
buzbee5de34942012-03-01 14:51:57 -0800591/* Add a 32-bit constant either in the constant pool */
Ian Rogers3fa13792012-03-18 15:53:45 -0700592LIR* addWordData(CompilationUnit* cUnit, LIR* *constantListP, int value)
buzbee31a4a6f2012-02-28 15:36:15 -0800593{
Bill Buzbeea114add2012-05-03 15:00:40 -0700594 /* Add the constant to the literal pool */
595 if (constantListP) {
596 LIR* newValue = (LIR* ) oatNew(cUnit, sizeof(LIR), true, kAllocData);
597 newValue->operands[0] = value;
598 newValue->next = *constantListP;
599 *constantListP = (LIR*) newValue;
600 return newValue;
601 }
602 return NULL;
buzbee31a4a6f2012-02-28 15:36:15 -0800603}
604
605/* Add a 64-bit constant to the constant pool or mixed with code */
606LIR* addWideData(CompilationUnit* cUnit, LIR* *constantListP,
Bill Buzbeea114add2012-05-03 15:00:40 -0700607 int valLo, int valHi)
buzbee31a4a6f2012-02-28 15:36:15 -0800608{
Bill Buzbeea114add2012-05-03 15:00:40 -0700609 //FIXME: hard-coded little endian, need BE variant
610 // Insert high word into list first
611 addWordData(cUnit, constantListP, valHi);
612 return addWordData(cUnit, constantListP, valLo);
buzbee31a4a6f2012-02-28 15:36:15 -0800613}
614
Ian Rogersab058bb2012-03-11 22:19:38 -0700615void pushWord(std::vector<uint8_t>&buf, int data) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700616 buf.push_back( data & 0xff);
617 buf.push_back( (data >> 8) & 0xff);
618 buf.push_back( (data >> 16) & 0xff);
619 buf.push_back( (data >> 24) & 0xff);
buzbeee3acd072012-02-25 17:03:10 -0800620}
621
Ian Rogersab058bb2012-03-11 22:19:38 -0700622void alignBuffer(std::vector<uint8_t>&buf, size_t offset) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700623 while (buf.size() < offset) {
624 buf.push_back(0);
625 }
buzbeee3acd072012-02-25 17:03:10 -0800626}
627
Brian Carlstromf5822582012-03-19 22:34:31 -0700628bool IsDirect(int invokeType) {
629 InvokeType type = static_cast<InvokeType>(invokeType);
630 return type == kStatic || type == kDirect;
631}
632
buzbeee3acd072012-02-25 17:03:10 -0800633/* Write the literal pool to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800634void installLiteralPools(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800635{
Bill Buzbeea114add2012-05-03 15:00:40 -0700636 alignBuffer(cUnit->codeBuffer, cUnit->dataOffset);
637 LIR* dataLIR = cUnit->literalList;
638 while (dataLIR != NULL) {
639 pushWord(cUnit->codeBuffer, dataLIR->operands[0]);
640 dataLIR = NEXT_LIR(dataLIR);
641 }
642 // Push code and method literals, record offsets for the compiler to patch.
643 dataLIR = cUnit->codeLiteralList;
644 if (dataLIR != NULL) {
buzbeee3acd072012-02-25 17:03:10 -0800645 while (dataLIR != NULL) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700646 uint32_t target = dataLIR->operands[0];
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700647 cUnit->compiler->AddCodePatch(cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 cUnit->method_idx,
649 cUnit->access_flags,
650 target,
651 IsDirect(dataLIR->operands[1]),
652 cUnit->codeBuffer.size());
653 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
654 // unique based on target to ensure code deduplication works
655 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
656 pushWord(cUnit->codeBuffer, unique_patch_value);
657 dataLIR = NEXT_LIR(dataLIR);
buzbeee3acd072012-02-25 17:03:10 -0800658 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700659 dataLIR = cUnit->methodLiteralList;
660 while (dataLIR != NULL) {
661 uint32_t target = dataLIR->operands[0];
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700662 cUnit->compiler->AddMethodPatch(cUnit->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700663 cUnit->method_idx,
664 cUnit->access_flags,
665 target,
666 IsDirect(dataLIR->operands[1]),
667 cUnit->codeBuffer.size());
668 const DexFile::MethodId& id = cUnit->dex_file->GetMethodId(target);
669 // unique based on target to ensure code deduplication works
670 uint32_t unique_patch_value = reinterpret_cast<uint32_t>(&id);
671 pushWord(cUnit->codeBuffer, unique_patch_value);
672 dataLIR = NEXT_LIR(dataLIR);
Ian Rogers3fa13792012-03-18 15:53:45 -0700673 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700674 }
Ian Rogers3fa13792012-03-18 15:53:45 -0700675
buzbeee3acd072012-02-25 17:03:10 -0800676}
677
678/* Write the switch tables to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800679void installSwitchTables(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800680{
Bill Buzbeea114add2012-05-03 15:00:40 -0700681 GrowableListIterator iterator;
682 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
683 while (true) {
684 SwitchTable* tabRec = (SwitchTable *) oatGrowableListIteratorNext(
685 &iterator);
686 if (tabRec == NULL) break;
687 alignBuffer(cUnit->codeBuffer, tabRec->offset);
688 /*
689 * For Arm, our reference point is the address of the bx
690 * instruction that does the launch, so we have to subtract
691 * the auto pc-advance. For other targets the reference point
692 * is a label, so we can use the offset as-is.
693 */
buzbeec5159d52012-03-03 11:48:39 -0800694#if defined(TARGET_ARM)
Bill Buzbeea114add2012-05-03 15:00:40 -0700695 int bxOffset = tabRec->anchor->offset + 4;
Ian Rogers7caad772012-03-30 01:07:54 -0700696#elif defined(TARGET_X86)
Bill Buzbeea114add2012-05-03 15:00:40 -0700697 int bxOffset = 0;
buzbeec5159d52012-03-03 11:48:39 -0800698#else
Bill Buzbeea114add2012-05-03 15:00:40 -0700699 int bxOffset = tabRec->anchor->offset;
buzbeec5159d52012-03-03 11:48:39 -0800700#endif
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 if (cUnit->printMe) {
702 LOG(INFO) << "Switch table for offset 0x" << std::hex << bxOffset;
buzbeee3acd072012-02-25 17:03:10 -0800703 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700704 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
705 int* keys = (int*)&(tabRec->table[2]);
706 for (int elems = 0; elems < tabRec->table[1]; elems++) {
707 int disp = tabRec->targets[elems]->offset - bxOffset;
708 if (cUnit->printMe) {
709 LOG(INFO) << " Case[" << elems << "] key: 0x"
710 << std::hex << keys[elems] << ", disp: 0x"
711 << std::hex << disp;
712 }
713 pushWord(cUnit->codeBuffer, keys[elems]);
714 pushWord(cUnit->codeBuffer,
715 tabRec->targets[elems]->offset - bxOffset);
716 }
717 } else {
718 DCHECK_EQ(static_cast<int>(tabRec->table[0]),
719 static_cast<int>(Instruction::kPackedSwitchSignature));
720 for (int elems = 0; elems < tabRec->table[1]; elems++) {
721 int disp = tabRec->targets[elems]->offset - bxOffset;
722 if (cUnit->printMe) {
723 LOG(INFO) << " Case[" << elems << "] disp: 0x"
724 << std::hex << disp;
725 }
726 pushWord(cUnit->codeBuffer, tabRec->targets[elems]->offset - bxOffset);
727 }
728 }
729 }
buzbeee3acd072012-02-25 17:03:10 -0800730}
731
732/* Write the fill array dta to the output stream */
buzbee31a4a6f2012-02-28 15:36:15 -0800733void installFillArrayData(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800734{
Bill Buzbeea114add2012-05-03 15:00:40 -0700735 GrowableListIterator iterator;
736 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
737 while (true) {
738 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
739 &iterator);
740 if (tabRec == NULL) break;
741 alignBuffer(cUnit->codeBuffer, tabRec->offset);
742 for (int i = 0; i < (tabRec->size + 1) / 2; i++) {
743 cUnit->codeBuffer.push_back( tabRec->table[i] & 0xFF);
744 cUnit->codeBuffer.push_back( (tabRec->table[i] >> 8) & 0xFF);
buzbeee3acd072012-02-25 17:03:10 -0800745 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700746 }
buzbeee3acd072012-02-25 17:03:10 -0800747}
748
buzbee31a4a6f2012-02-28 15:36:15 -0800749int assignLiteralOffsetCommon(LIR* lir, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800750{
Bill Buzbeea114add2012-05-03 15:00:40 -0700751 for (;lir != NULL; lir = lir->next) {
752 lir->offset = offset;
753 offset += 4;
754 }
755 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800756}
757
buzbee31a4a6f2012-02-28 15:36:15 -0800758void createMappingTable(CompilationUnit* cUnit)
buzbeee3acd072012-02-25 17:03:10 -0800759{
Bill Buzbeea114add2012-05-03 15:00:40 -0700760 LIR* tgtLIR;
761 int currentDalvikOffset = -1;
buzbeee3acd072012-02-25 17:03:10 -0800762
Bill Buzbeea114add2012-05-03 15:00:40 -0700763 for (tgtLIR = (LIR *) cUnit->firstLIRInsn;
764 tgtLIR;
765 tgtLIR = NEXT_LIR(tgtLIR)) {
766 if ((tgtLIR->opcode >= 0) && !tgtLIR->flags.isNop &&
767 (currentDalvikOffset != tgtLIR->dalvikOffset)) {
768 // Changed - need to emit a record
769 cUnit->mappingTable.push_back(tgtLIR->offset);
770 cUnit->mappingTable.push_back(tgtLIR->dalvikOffset);
771 currentDalvikOffset = tgtLIR->dalvikOffset;
buzbeee3acd072012-02-25 17:03:10 -0800772 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700773 }
buzbeee3acd072012-02-25 17:03:10 -0800774}
775
776/* Determine the offset of each literal field */
buzbee31a4a6f2012-02-28 15:36:15 -0800777int assignLiteralOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800778{
Bill Buzbeea114add2012-05-03 15:00:40 -0700779 offset = assignLiteralOffsetCommon(cUnit->literalList, offset);
780 offset = assignLiteralOffsetCommon(cUnit->codeLiteralList, offset);
781 offset = assignLiteralOffsetCommon(cUnit->methodLiteralList, offset);
782 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800783}
784
buzbee31a4a6f2012-02-28 15:36:15 -0800785int assignSwitchTablesOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800786{
Bill Buzbeea114add2012-05-03 15:00:40 -0700787 GrowableListIterator iterator;
788 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
789 while (true) {
790 SwitchTable *tabRec = (SwitchTable *) oatGrowableListIteratorNext(
791 &iterator);
792 if (tabRec == NULL) break;
793 tabRec->offset = offset;
794 if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
795 offset += tabRec->table[1] * (sizeof(int) * 2);
796 } else {
797 DCHECK_EQ(static_cast<int>(tabRec->table[0]),
798 static_cast<int>(Instruction::kPackedSwitchSignature));
799 offset += tabRec->table[1] * sizeof(int);
buzbeee3acd072012-02-25 17:03:10 -0800800 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700801 }
802 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800803}
804
buzbee31a4a6f2012-02-28 15:36:15 -0800805int assignFillArrayDataOffset(CompilationUnit* cUnit, int offset)
buzbeee3acd072012-02-25 17:03:10 -0800806{
Bill Buzbeea114add2012-05-03 15:00:40 -0700807 GrowableListIterator iterator;
808 oatGrowableListIteratorInit(&cUnit->fillArrayData, &iterator);
809 while (true) {
810 FillArrayData *tabRec = (FillArrayData *) oatGrowableListIteratorNext(
811 &iterator);
812 if (tabRec == NULL) break;
813 tabRec->offset = offset;
814 offset += tabRec->size;
815 // word align
816 offset = (offset + 3) & ~3;
817 }
818 return offset;
buzbeee3acd072012-02-25 17:03:10 -0800819}
820
821/*
822 * Walk the compilation unit and assign offsets to instructions
823 * and literals and compute the total size of the compiled unit.
824 */
825void oatAssignOffsets(CompilationUnit* cUnit)
826{
Bill Buzbeea114add2012-05-03 15:00:40 -0700827 int offset = oatAssignInsnOffsets(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800828
Bill Buzbeea114add2012-05-03 15:00:40 -0700829 /* Const values have to be word aligned */
830 offset = (offset + 3) & ~3;
buzbeee3acd072012-02-25 17:03:10 -0800831
Bill Buzbeea114add2012-05-03 15:00:40 -0700832 /* Set up offsets for literals */
833 cUnit->dataOffset = offset;
buzbeee3acd072012-02-25 17:03:10 -0800834
Bill Buzbeea114add2012-05-03 15:00:40 -0700835 offset = assignLiteralOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800836
Bill Buzbeea114add2012-05-03 15:00:40 -0700837 offset = assignSwitchTablesOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800838
Bill Buzbeea114add2012-05-03 15:00:40 -0700839 offset = assignFillArrayDataOffset(cUnit, offset);
buzbeee3acd072012-02-25 17:03:10 -0800840
Bill Buzbeea114add2012-05-03 15:00:40 -0700841 cUnit->totalSize = offset;
buzbeee3acd072012-02-25 17:03:10 -0800842}
843
844/*
845 * Go over each instruction in the list and calculate the offset from the top
846 * before sending them off to the assembler. If out-of-range branch distance is
847 * seen rearrange the instructions a bit to correct it.
848 */
849void oatAssembleLIR(CompilationUnit* cUnit)
850{
Bill Buzbeea114add2012-05-03 15:00:40 -0700851 oatAssignOffsets(cUnit);
852 /*
853 * Assemble here. Note that we generate code with optimistic assumptions
854 * and if found now to work, we'll have to redo the sequence and retry.
855 */
buzbeee3acd072012-02-25 17:03:10 -0800856
Bill Buzbeea114add2012-05-03 15:00:40 -0700857 while (true) {
858 AssemblerStatus res = oatAssembleInstructions(cUnit, 0);
859 if (res == kSuccess) {
860 break;
861 } else {
862 cUnit->assemblerRetries++;
863 if (cUnit->assemblerRetries > MAX_ASSEMBLER_RETRIES) {
864 oatCodegenDump(cUnit);
865 LOG(FATAL) << "Assembler error - too many retries";
866 }
867 // Redo offsets and try again
868 oatAssignOffsets(cUnit);
869 cUnit->codeBuffer.clear();
buzbeee3acd072012-02-25 17:03:10 -0800870 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700871 }
buzbeee3acd072012-02-25 17:03:10 -0800872
Bill Buzbeea114add2012-05-03 15:00:40 -0700873 // Install literals
874 installLiteralPools(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800875
Bill Buzbeea114add2012-05-03 15:00:40 -0700876 // Install switch tables
877 installSwitchTables(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800878
Bill Buzbeea114add2012-05-03 15:00:40 -0700879 // Install fill array data
880 installFillArrayData(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800881
Bill Buzbeea114add2012-05-03 15:00:40 -0700882 /*
883 * Create the mapping table
884 */
885 createMappingTable(cUnit);
buzbeee3acd072012-02-25 17:03:10 -0800886}
887
buzbee31a4a6f2012-02-28 15:36:15 -0800888/*
889 * Insert a kPseudoCaseLabel at the beginning of the Dalvik
890 * offset vaddr. This label will be used to fix up the case
891 * branch table during the assembly phase. Be sure to set
892 * all resource flags on this to prevent code motion across
893 * target boundaries. KeyVal is just there for debugging.
894 */
895LIR* insertCaseLabel(CompilationUnit* cUnit, int vaddr, int keyVal)
896{
Bill Buzbeea114add2012-05-03 15:00:40 -0700897 SafeMap<unsigned int, LIR*>::iterator it;
898 it = cUnit->boundaryMap.find(vaddr);
899 if (it == cUnit->boundaryMap.end()) {
900 LOG(FATAL) << "Error: didn't find vaddr 0x" << std::hex << vaddr;
901 }
902 LIR* newLabel = (LIR*)oatNew(cUnit, sizeof(LIR), true, kAllocLIR);
903 newLabel->dalvikOffset = vaddr;
904 newLabel->opcode = kPseudoCaseLabel;
905 newLabel->operands[0] = keyVal;
906 oatInsertLIRAfter(it->second, (LIR*)newLabel);
907 return newLabel;
buzbee31a4a6f2012-02-28 15:36:15 -0800908}
909
910void markPackedCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
911{
Bill Buzbeea114add2012-05-03 15:00:40 -0700912 const u2* table = tabRec->table;
913 int baseVaddr = tabRec->vaddr;
914 int *targets = (int*)&table[4];
915 int entries = table[1];
916 int lowKey = s4FromSwitchData(&table[2]);
917 for (int i = 0; i < entries; i++) {
918 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
919 i + lowKey);
920 }
buzbee31a4a6f2012-02-28 15:36:15 -0800921}
922
923void markSparseCaseLabels(CompilationUnit* cUnit, SwitchTable *tabRec)
924{
Bill Buzbeea114add2012-05-03 15:00:40 -0700925 const u2* table = tabRec->table;
926 int baseVaddr = tabRec->vaddr;
927 int entries = table[1];
928 int* keys = (int*)&table[2];
929 int* targets = &keys[entries];
930 for (int i = 0; i < entries; i++) {
931 tabRec->targets[i] = insertCaseLabel(cUnit, baseVaddr + targets[i],
932 keys[i]);
933 }
buzbee31a4a6f2012-02-28 15:36:15 -0800934}
935
936void oatProcessSwitchTables(CompilationUnit* cUnit)
937{
Bill Buzbeea114add2012-05-03 15:00:40 -0700938 GrowableListIterator iterator;
939 oatGrowableListIteratorInit(&cUnit->switchTables, &iterator);
940 while (true) {
941 SwitchTable *tabRec =
942 (SwitchTable *) oatGrowableListIteratorNext(&iterator);
943 if (tabRec == NULL) break;
944 if (tabRec->table[0] == Instruction::kPackedSwitchSignature) {
945 markPackedCaseLabels(cUnit, tabRec);
946 } else if (tabRec->table[0] == Instruction::kSparseSwitchSignature) {
947 markSparseCaseLabels(cUnit, tabRec);
948 } else {
949 LOG(FATAL) << "Invalid switch table";
buzbee31a4a6f2012-02-28 15:36:15 -0800950 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700951 }
buzbee31a4a6f2012-02-28 15:36:15 -0800952}
953
954//FIXME: Do we have endian issues here?
955
956void dumpSparseSwitchTable(const u2* table)
Bill Buzbeea114add2012-05-03 15:00:40 -0700957 /*
958 * Sparse switch data format:
959 * ushort ident = 0x0200 magic value
960 * ushort size number of entries in the table; > 0
961 * int keys[size] keys, sorted low-to-high; 32-bit aligned
962 * int targets[size] branch targets, relative to switch opcode
963 *
964 * Total size is (2+size*4) 16-bit code units.
965 */
buzbee31a4a6f2012-02-28 15:36:15 -0800966{
Bill Buzbeea114add2012-05-03 15:00:40 -0700967 u2 ident = table[0];
968 int entries = table[1];
969 int* keys = (int*)&table[2];
970 int* targets = &keys[entries];
971 LOG(INFO) << "Sparse switch table - ident:0x" << std::hex << ident
972 << ", entries: " << std::dec << entries;
973 for (int i = 0; i < entries; i++) {
974 LOG(INFO) << " Key[" << keys[i] << "] -> 0x" << std::hex << targets[i];
975 }
buzbee31a4a6f2012-02-28 15:36:15 -0800976}
977
978void dumpPackedSwitchTable(const u2* table)
Bill Buzbeea114add2012-05-03 15:00:40 -0700979 /*
980 * Packed switch data format:
981 * ushort ident = 0x0100 magic value
982 * ushort size number of entries in the table
983 * int first_key first (and lowest) switch case value
984 * int targets[size] branch targets, relative to switch opcode
985 *
986 * Total size is (4+size*2) 16-bit code units.
987 */
buzbee31a4a6f2012-02-28 15:36:15 -0800988{
Bill Buzbeea114add2012-05-03 15:00:40 -0700989 u2 ident = table[0];
990 int* targets = (int*)&table[4];
991 int entries = table[1];
992 int lowKey = s4FromSwitchData(&table[2]);
993 LOG(INFO) << "Packed switch table - ident:0x" << std::hex << ident
994 << ", entries: " << std::dec << entries << ", lowKey: " << lowKey;
995 for (int i = 0; i < entries; i++) {
996 LOG(INFO) << " Key[" << (i + lowKey) << "] -> 0x" << std::hex
997 << targets[i];
998 }
buzbee31a4a6f2012-02-28 15:36:15 -0800999}
buzbeee3acd072012-02-25 17:03:10 -08001000
1001
1002} // namespace art