blob: b70268230860fdb4c80c5d3b6c2f07c23a7ddc94 [file] [log] [blame]
buzbee67bf8852011-08-17 17:51:35 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "../../CompilerInternals.h"
18#include "ArmLIR.h"
buzbee67bc2362011-10-11 18:08:40 -070019#include "../Ralloc.h"
buzbee67bf8852011-08-17 17:51:35 -070020
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070021#include <string>
22
buzbee67bf8852011-08-17 17:51:35 -070023static const char* coreRegNames[16] = {
24 "r0",
25 "r1",
26 "r2",
27 "r3",
28 "r4",
29 "r5",
30 "r6",
31 "r7",
32 "r8",
33 "rSELF",
34 "r10",
35 "r11",
36 "r12",
37 "sp",
38 "lr",
39 "pc",
40};
41
42
43static const char* shiftNames[4] = {
44 "lsl",
45 "lsr",
46 "asr",
47 "ror"};
48
49/* Decode and print a ARM register name */
buzbeeed3e9302011-09-23 17:34:19 -070050STATIC char* decodeRegList(ArmOpcode opcode, int vector, char* buf)
buzbee67bf8852011-08-17 17:51:35 -070051{
52 int i;
53 bool printed = false;
54 buf[0] = 0;
55 for (i = 0; i < 16; i++, vector >>= 1) {
56 if (vector & 0x1) {
57 int regId = i;
58 if (opcode == kThumbPush && i == 8) {
59 regId = r14lr;
60 } else if (opcode == kThumbPop && i == 8) {
61 regId = r15pc;
62 }
63 if (printed) {
64 sprintf(buf + strlen(buf), ", r%d", regId);
65 } else {
66 printed = true;
67 sprintf(buf, "r%d", regId);
68 }
69 }
70 }
71 return buf;
72}
73
buzbeeed3e9302011-09-23 17:34:19 -070074STATIC char* decodeFPCSRegList(int count, int base, char* buf)
buzbee67bf8852011-08-17 17:51:35 -070075{
76 sprintf(buf, "s%d", base);
77 for (int i = 1; i < count; i++) {
78 sprintf(buf + strlen(buf), ", s%d",base + i);
79 }
80 return buf;
81}
82
buzbeeed3e9302011-09-23 17:34:19 -070083STATIC int expandImmediate(int value)
buzbee67bf8852011-08-17 17:51:35 -070084{
85 int mode = (value & 0xf00) >> 8;
86 u4 bits = value & 0xff;
87 switch(mode) {
88 case 0:
89 return bits;
90 case 1:
91 return (bits << 16) | bits;
92 case 2:
93 return (bits << 24) | (bits << 8);
94 case 3:
95 return (bits << 24) | (bits << 16) | (bits << 8) | bits;
96 default:
97 break;
98 }
99 bits = (bits | 0x80) << 24;
100 return bits >> (((value & 0xf80) >> 7) - 8);
101}
102
103const char* ccNames[] = {"eq","ne","cs","cc","mi","pl","vs","vc",
104 "hi","ls","ge","lt","gt","le","al","nv"};
105/*
106 * Interpret a format string and build a string no longer than size
107 * See format key in Assemble.c.
108 */
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700109STATIC std::string buildInsnString(const char* fmt, ArmLIR* lir, unsigned char* baseAddr)
buzbee67bf8852011-08-17 17:51:35 -0700110{
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700111 std::string buf;
buzbee67bf8852011-08-17 17:51:35 -0700112 int i;
buzbee67bf8852011-08-17 17:51:35 -0700113 const char* fmtEnd = &fmt[strlen(fmt)];
114 char tbuf[256];
115 const char* name;
116 char nc;
117 while (fmt < fmtEnd) {
118 int operand;
119 if (*fmt == '!') {
120 fmt++;
buzbeeed3e9302011-09-23 17:34:19 -0700121 DCHECK_LT(fmt, fmtEnd);
buzbee67bf8852011-08-17 17:51:35 -0700122 nc = *fmt++;
123 if (nc=='!') {
124 strcpy(tbuf, "!");
125 } else {
buzbeeed3e9302011-09-23 17:34:19 -0700126 DCHECK_LT(fmt, fmtEnd);
Elliott Hughesf5a7a472011-10-07 14:31:02 -0700127 DCHECK_LT((unsigned)(nc-'0'), 4U);
buzbee67bf8852011-08-17 17:51:35 -0700128 operand = lir->operands[nc-'0'];
129 switch(*fmt++) {
130 case 'H':
131 if (operand != 0) {
132 sprintf(tbuf, ", %s %d",shiftNames[operand & 0x3],
133 operand >> 2);
134 } else {
135 strcpy(tbuf,"");
136 }
137 break;
138 case 'B':
139 switch (operand) {
140 case kSY:
141 name = "sy";
142 break;
143 case kST:
144 name = "st";
145 break;
146 case kISH:
147 name = "ish";
148 break;
149 case kISHST:
150 name = "ishst";
151 break;
152 case kNSH:
153 name = "nsh";
154 break;
155 case kNSHST:
156 name = "shst";
157 break;
158 default:
159 name = "DecodeError2";
160 break;
161 }
162 strcpy(tbuf, name);
163 break;
164 case 'b':
165 strcpy(tbuf,"0000");
166 for (i=3; i>= 0; i--) {
167 tbuf[i] += operand & 1;
168 operand >>= 1;
169 }
170 break;
171 case 'n':
172 operand = ~expandImmediate(operand);
173 sprintf(tbuf,"%d [%#x]", operand, operand);
174 break;
175 case 'm':
176 operand = expandImmediate(operand);
177 sprintf(tbuf,"%d [%#x]", operand, operand);
178 break;
179 case 's':
180 sprintf(tbuf,"s%d",operand & FP_REG_MASK);
181 break;
182 case 'S':
183 sprintf(tbuf,"d%d",(operand & FP_REG_MASK) >> 1);
184 break;
185 case 'h':
186 sprintf(tbuf,"%04x", operand);
187 break;
188 case 'M':
189 case 'd':
190 sprintf(tbuf,"%d", operand);
191 break;
192 case 'C':
193 sprintf(tbuf,"%s",coreRegNames[operand]);
194 break;
195 case 'E':
196 sprintf(tbuf,"%d", operand*4);
197 break;
198 case 'F':
199 sprintf(tbuf,"%d", operand*2);
200 break;
201 case 'c':
202 strcpy(tbuf, ccNames[operand]);
203 break;
204 case 't':
205 sprintf(tbuf,"0x%08x (L%p)",
206 (int) baseAddr + lir->generic.offset + 4 +
207 (operand << 1),
208 lir->generic.target);
209 break;
210 case 'u': {
211 int offset_1 = lir->operands[0];
212 int offset_2 = NEXT_LIR(lir)->operands[0];
213 intptr_t target =
214 ((((intptr_t) baseAddr + lir->generic.offset + 4) &
215 ~3) + (offset_1 << 21 >> 9) + (offset_2 << 1)) &
216 0xfffffffc;
217 sprintf(tbuf, "%p", (void *) target);
218 break;
219 }
220
221 /* Nothing to print for BLX_2 */
222 case 'v':
223 strcpy(tbuf, "see above");
224 break;
225 case 'R':
226 decodeRegList(lir->opcode, operand, tbuf);
227 break;
228 case 'P':
229 decodeFPCSRegList(operand, 16, tbuf);
230 break;
231 case 'Q':
232 decodeFPCSRegList(operand, 0, tbuf);
233 break;
234 default:
235 strcpy(tbuf,"DecodeError1");
236 break;
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700237 }
238 buf += tbuf;
buzbee67bf8852011-08-17 17:51:35 -0700239 }
240 } else {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700241 buf += *fmt++;
buzbee67bf8852011-08-17 17:51:35 -0700242 }
buzbee67bf8852011-08-17 17:51:35 -0700243 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700244 return buf;
buzbee67bf8852011-08-17 17:51:35 -0700245}
246
247void oatDumpResourceMask(LIR* lir, u8 mask, const char* prefix)
248{
249 char buf[256];
250 buf[0] = 0;
251 ArmLIR* armLIR = (ArmLIR*) lir;
252
253 if (mask == ENCODE_ALL) {
254 strcpy(buf, "all");
255 } else {
256 char num[8];
257 int i;
258
259 for (i = 0; i < kRegEnd; i++) {
260 if (mask & (1ULL << i)) {
261 sprintf(num, "%d ", i);
262 strcat(buf, num);
263 }
264 }
265
266 if (mask & ENCODE_CCODE) {
267 strcat(buf, "cc ");
268 }
269 if (mask & ENCODE_FP_STATUS) {
270 strcat(buf, "fpcc ");
271 }
272
273 /* Memory bits */
274 if (armLIR && (mask & ENCODE_DALVIK_REG)) {
275 sprintf(buf + strlen(buf), "dr%d%s", armLIR->aliasInfo & 0xffff,
276 (armLIR->aliasInfo & 0x80000000) ? "(+1)" : "");
277 }
278 if (mask & ENCODE_LITERAL) {
279 strcat(buf, "lit ");
280 }
281
282 if (mask & ENCODE_HEAP_REF) {
283 strcat(buf, "heap ");
284 }
285 if (mask & ENCODE_MUST_NOT_ALIAS) {
286 strcat(buf, "noalias ");
287 }
288 }
289 if (buf[0]) {
290 LOG(INFO) << prefix << ": " << buf;
291 }
292}
293
294/*
295 * Debugging macros
296 */
297#define DUMP_RESOURCE_MASK(X)
298#define DUMP_SSA_REP(X)
299
300/* Pretty-print a LIR instruction */
301void oatDumpLIRInsn(CompilationUnit* cUnit, LIR* arg, unsigned char* baseAddr)
302{
303 ArmLIR* lir = (ArmLIR*) arg;
buzbee67bf8852011-08-17 17:51:35 -0700304 int offset = lir->generic.offset;
305 int dest = lir->operands[0];
306 const bool dumpNop = false;
307
308 /* Handle pseudo-ops individually, and all regular insns as a group */
309 switch(lir->opcode) {
310 case kArmPseudoMethodEntry:
311 LOG(INFO) << "-------- method entry " <<
Ian Rogersa3760aa2011-11-14 14:32:37 -0800312 art::PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
buzbee67bf8852011-08-17 17:51:35 -0700313 break;
314 case kArmPseudoMethodExit:
315 LOG(INFO) << "-------- Method_Exit";
316 break;
317 case kArmPseudoBarrier:
318 LOG(INFO) << "-------- BARRIER";
319 break;
320 case kArmPseudoExtended:
321 LOG(INFO) << "-------- " << (char* ) dest;
322 break;
323 case kArmPseudoSSARep:
324 DUMP_SSA_REP(LOG(INFO) << "-------- kMirOpPhi: " << (char* ) dest);
325 break;
326 case kArmPseudoEntryBlock:
327 LOG(INFO) << "-------- entry offset: 0x" << std::hex << dest;
328 break;
329 case kArmPseudoDalvikByteCodeBoundary:
330 LOG(INFO) << "-------- dalvik offset: 0x" << std::hex <<
331 lir->generic.dalvikOffset << " @ " << (char* )lir->operands[0];
332 break;
333 case kArmPseudoExitBlock:
334 LOG(INFO) << "-------- exit offset: 0x" << std::hex << dest;
335 break;
336 case kArmPseudoPseudoAlign4:
337 LOG(INFO) << (intptr_t)baseAddr + offset << " (0x" << std::hex <<
338 offset << "): .align4";
339 break;
340 case kArmPseudoEHBlockLabel:
341 LOG(INFO) << "Exception_Handling:";
342 break;
343 case kArmPseudoTargetLabel:
344 case kArmPseudoNormalBlockLabel:
345 LOG(INFO) << "L" << (intptr_t)lir << ":";
346 break;
buzbee5ade1d22011-09-09 14:44:52 -0700347 case kArmPseudoThrowTarget:
348 LOG(INFO) << "LT" << (intptr_t)lir << ":";
349 break;
buzbeec1f45042011-09-21 16:03:19 -0700350 case kArmPseudoSuspendTarget:
351 LOG(INFO) << "LS" << (intptr_t)lir << ":";
352 break;
buzbee67bf8852011-08-17 17:51:35 -0700353 case kArmPseudoCaseLabel:
354 LOG(INFO) << "LC" << (intptr_t)lir << ": Case target 0x" <<
355 std::hex << lir->operands[0] << "|" << std::dec <<
356 lir->operands[0];
357 break;
358 default:
359 if (lir->flags.isNop && !dumpNop) {
360 break;
buzbee3ea4ec52011-08-22 17:37:19 -0700361 } else {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700362 std::string op_name(buildInsnString(EncodingMap[lir->opcode].name, lir, baseAddr));
363 std::string op_operands(buildInsnString(EncodingMap[lir->opcode].fmt, lir, baseAddr));
364 LOG(INFO) << StringPrintf("%p (%04x): %-9s%s%s%s", baseAddr + offset, offset,
365 op_name.c_str(), op_operands.c_str(), lir->flags.isNop ? "(nop)" : "",
366 lir->flags.squashed ? "(squashed)" : "");
buzbee67bf8852011-08-17 17:51:35 -0700367 }
buzbee67bf8852011-08-17 17:51:35 -0700368 break;
369 }
370
371 if (lir->useMask && (!lir->flags.isNop || dumpNop)) {
372 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir,
373 lir->useMask, "use"));
374 }
375 if (lir->defMask && (!lir->flags.isNop || dumpNop)) {
376 DUMP_RESOURCE_MASK(oatDumpResourceMask((LIR* ) lir,
377 lir->defMask, "def"));
378 }
379}
380
buzbee67bc2362011-10-11 18:08:40 -0700381void oatDumpPromotionMap(CompilationUnit *cUnit)
382{
Ian Rogersa3760aa2011-11-14 14:32:37 -0800383 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
buzbee67bc2362011-10-11 18:08:40 -0700384 PromotionMap vRegMap = cUnit->promotionMap[i];
385 char buf[100];
386 if (vRegMap.fpLocation == kLocPhysReg) {
387 snprintf(buf, 100, " : s%d", vRegMap.fpReg & FP_REG_MASK);
388 } else {
389 buf[0] = 0;
390 }
391 char buf2[100];
392 snprintf(buf2, 100, "V[%02d] -> %s%d%s", i,
393 vRegMap.coreLocation == kLocPhysReg ?
394 "r" : "SP+", vRegMap.coreLocation == kLocPhysReg ?
395 vRegMap.coreReg : oatSRegOffset(cUnit, i), buf);
396 LOG(INFO) << buf2;
397 }
398}
399
400void oatDumpFullPromotionMap(CompilationUnit *cUnit)
401{
Ian Rogersa3760aa2011-11-14 14:32:37 -0800402 for (int i = 0; i < cUnit->numDalvikRegisters; i++) {
buzbee67bc2362011-10-11 18:08:40 -0700403 PromotionMap vRegMap = cUnit->promotionMap[i];
404 LOG(INFO) << i << " -> " << "CL:" << (int)vRegMap.coreLocation <<
405 ", CR:" << (int)vRegMap.coreReg << ", FL:" <<
406 (int)vRegMap.fpLocation << ", FR:" << (int)vRegMap.fpReg <<
407 ", - " << (int)vRegMap.firstInPair;
408 }
409}
410
buzbee67bf8852011-08-17 17:51:35 -0700411/* Dump instructions and constant pool contents */
412void oatCodegenDump(CompilationUnit* cUnit)
413{
buzbee67bf8852011-08-17 17:51:35 -0700414 LOG(INFO) << "/*";
Ian Rogersa3760aa2011-11-14 14:32:37 -0800415 LOG(INFO) << "Dumping LIR insns for "
416 << art::PrettyMethod(cUnit->method_idx, *cUnit->dex_file);
buzbee67bf8852011-08-17 17:51:35 -0700417 LIR* lirInsn;
418 ArmLIR* armLIR;
419 int insnsSize = cUnit->insnsSize;
420
421 LOG(INFO) << "Regs (excluding ins) : " << cUnit->numRegs;
422 LOG(INFO) << "Ins : " << cUnit->numIns;
423 LOG(INFO) << "Outs : " << cUnit->numOuts;
buzbeebbaf8942011-10-02 13:08:29 -0700424 LOG(INFO) << "CoreSpills : " << cUnit->numCoreSpills;
425 LOG(INFO) << "FPSpills : " << cUnit->numFPSpills;
buzbee67bf8852011-08-17 17:51:35 -0700426 LOG(INFO) << "Padding : " << cUnit->numPadding;
427 LOG(INFO) << "Frame size : " << cUnit->frameSize;
428 LOG(INFO) << "Start of ins : " << cUnit->insOffset;
429 LOG(INFO) << "Start of regs : " << cUnit->regsOffset;
430 LOG(INFO) << "code size is " << cUnit->totalSize <<
431 " bytes, Dalvik size is " << insnsSize * 2;
432 LOG(INFO) << "expansion factor: " <<
433 (float)cUnit->totalSize / (float)(insnsSize * 2);
buzbee67bc2362011-10-11 18:08:40 -0700434 oatDumpPromotionMap(cUnit);
buzbee67bf8852011-08-17 17:51:35 -0700435 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
buzbeec143c552011-08-20 17:38:58 -0700436 oatDumpLIRInsn(cUnit, lirInsn, 0);
buzbee67bf8852011-08-17 17:51:35 -0700437 }
438 for (lirInsn = cUnit->classPointerList; lirInsn; lirInsn = lirInsn->next) {
439 armLIR = (ArmLIR*) lirInsn;
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700440 LOG(INFO) << StringPrintf("%x (%04x): .class (%s)",
441 armLIR->generic.offset, armLIR->generic.offset,
442 ((CallsiteInfo *) armLIR->operands[0])->classDescriptor);
buzbee67bf8852011-08-17 17:51:35 -0700443 }
444 for (lirInsn = cUnit->literalList; lirInsn; lirInsn = lirInsn->next) {
445 armLIR = (ArmLIR*) lirInsn;
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700446 LOG(INFO) << StringPrintf("%x (%04x): .word (%#x)",
447 armLIR->generic.offset, armLIR->generic.offset, armLIR->operands[0]);
buzbee67bf8852011-08-17 17:51:35 -0700448 }
449
Ian Rogersa3760aa2011-11-14 14:32:37 -0800450 const art::DexFile::MethodId& method_id =
451 cUnit->dex_file->GetMethodId(cUnit->method_idx);
452 std::string signature = cUnit->dex_file->GetMethodSignature(method_id);
453 std::string name = cUnit->dex_file->GetMethodName(method_id);
454 std::string descriptor =
455 cUnit->dex_file->GetMethodDeclaringClassDescriptor(method_id);
buzbeec143c552011-08-20 17:38:58 -0700456
buzbee67bf8852011-08-17 17:51:35 -0700457 // Dump mapping table
buzbee4ef76522011-09-08 10:00:32 -0700458 if (cUnit->mappingTable.size() > 0) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700459 std::string line(StringPrintf("\n MappingTable %s%s_%s_mappingTable[%d] = {",
460 descriptor.c_str(), name.c_str(), signature.c_str(), cUnit->mappingTable.size()));
461 std::replace(line.begin(), line.end(), ';', '_');
462 LOG(INFO) << line;
buzbee4ef76522011-09-08 10:00:32 -0700463 for (uint32_t i = 0; i < cUnit->mappingTable.size(); i+=2) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700464 line = StringPrintf(" {0x%08x, 0x%04x},",
buzbee4ef76522011-09-08 10:00:32 -0700465 cUnit->mappingTable[i], cUnit->mappingTable[i+1]);
Elliott Hughes3b6baaa2011-10-14 19:13:56 -0700466 LOG(INFO) << line;
buzbee67bf8852011-08-17 17:51:35 -0700467 }
468 LOG(INFO) <<" };\n\n";
469 }
buzbee67bf8852011-08-17 17:51:35 -0700470}