2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "../../CompilerInternals.h"
18 #include "libdex/OpCodeNames.h"
21 /* Decode and print a ARM register name */
22 static char * decodeRegList(int vector, char *buf)
27 for (i = 0; i < 8; i++, vector >>= 1) {
30 sprintf(buf + strlen(buf), ", r%d", i);
33 sprintf(buf, "r%d", i);
40 static int expandImmediate(int value)
42 int mode = (value & 0xf00) >> 8;
43 u4 bits = value & 0xff;
48 return (bits << 16) | bits;
50 return (bits << 24) | (bits << 8);
52 return (bits << 24) | (bits << 16) | (bits << 8) | bits;
56 bits = (bits | 0x80) << 24;
57 return bits >> (((value & 0xf80) >> 7) - 8);
61 * Interpret a format string and build a string no longer than size
62 * See format key in Assemble.c.
64 static void buildInsnString(char *fmt, ArmLIR *lir, char* buf,
65 unsigned char *baseAddr, int size)
68 char *bufEnd = &buf[size-1];
69 char *fmtEnd = &fmt[strlen(fmt)];
73 while (fmt < fmtEnd) {
83 assert((unsigned)(nc-'0') < 4);
84 operand = lir->operands[nc-'0'];
107 name = "DecodeError";
114 for (i=3; i>= 0; i--) {
115 tbuf[i] += operand & 1;
120 operand = ~expandImmediate(operand);
121 sprintf(tbuf,"%d [0x%x]", operand, operand);
124 operand = expandImmediate(operand);
125 sprintf(tbuf,"%d [0x%x]", operand, operand);
128 sprintf(tbuf,"s%d",operand & FP_REG_MASK);
131 sprintf(tbuf,"d%d",(operand & FP_REG_MASK) >> 1);
134 sprintf(tbuf,"%04x", operand);
138 sprintf(tbuf,"%d", operand);
141 sprintf(tbuf,"%d", operand*4);
144 sprintf(tbuf,"%d", operand*2);
178 sprintf(tbuf,"0x%08x",
179 (int) baseAddr + lir->generic.offset + 4 +
183 int offset_1 = lir->operands[0];
184 int offset_2 = NEXT_LIR(lir)->operands[0];
186 ((((intptr_t) baseAddr + lir->generic.offset + 4) &
187 ~3) + (offset_1 << 21 >> 9) + (offset_2 << 1)) &
189 sprintf(tbuf, "%p", (void *) target);
193 /* Nothing to print for BLX_2 */
195 strcpy(tbuf, "see above");
198 decodeRegList(operand, tbuf);
201 strcpy(tbuf,"DecodeError");
204 if (buf+strlen(tbuf) <= bufEnd) {
220 void dvmDumpResourceMask(LIR *lir, u8 mask, const char *prefix)
224 ArmLIR *armLIR = (ArmLIR *) lir;
226 if (mask == ENCODE_ALL) {
232 for (i = 0; i < kRegEnd; i++) {
233 if (mask & (1ULL << i)) {
234 sprintf(num, "%d ", i);
239 if (mask & ENCODE_CCODE) {
242 if (mask & ENCODE_FP_STATUS) {
243 strcat(buf, "fpcc ");
245 if (armLIR && (mask & ENCODE_DALVIK_REG)) {
246 sprintf(buf + strlen(buf), "dr%d%s", armLIR->aliasInfo & 0xffff,
247 (armLIR->aliasInfo & 0x80000000) ? "(+1)" : "");
251 LOGD("%s: %s", prefix, buf);
258 #define DUMP_RESOURCE_MASK(X)
259 #define DUMP_SSA_REP(X)
261 /* Pretty-print a LIR instruction */
262 void dvmDumpLIRInsn(LIR *arg, unsigned char *baseAddr)
264 ArmLIR *lir = (ArmLIR *) arg;
267 int offset = lir->generic.offset;
268 int dest = lir->operands[0];
269 const bool dumpNop = false;
271 /* Handle pseudo-ops individually, and all regular insns as a group */
272 switch(lir->opCode) {
273 case kArmChainingCellBottom:
274 LOGD("-------- end of chaining cells (0x%04x)\n", offset);
276 case kArmPseudoBarrier:
277 LOGD("-------- BARRIER");
279 case kArmPseudoExtended:
280 /* intentional fallthrough */
281 case kArmPseudoSSARep:
282 DUMP_SSA_REP(LOGD("-------- %s\n", (char *) dest));
284 case kArmPseudoTargetLabel:
286 case kArmPseudoChainingCellBackwardBranch:
287 LOGD("-------- chaining cell (backward branch): 0x%04x\n", dest);
289 case kArmPseudoChainingCellNormal:
290 LOGD("-------- chaining cell (normal): 0x%04x\n", dest);
292 case kArmPseudoChainingCellHot:
293 LOGD("-------- chaining cell (hot): 0x%04x\n", dest);
295 case kArmPseudoChainingCellInvokePredicted:
296 LOGD("-------- chaining cell (predicted)\n");
298 case kArmPseudoChainingCellInvokeSingleton:
299 LOGD("-------- chaining cell (invoke singleton): %s/%p\n",
300 ((Method *)dest)->name,
301 ((Method *)dest)->insns);
303 case kArmPseudoEntryBlock:
304 LOGD("-------- entry offset: 0x%04x\n", dest);
306 case kArmPseudoDalvikByteCodeBoundary:
307 LOGD("-------- dalvik offset: 0x%04x @ %s\n", dest,
308 (char *) lir->operands[1]);
310 case kArmPseudoExitBlock:
311 LOGD("-------- exit offset: 0x%04x\n", dest);
313 case kArmPseudoPseudoAlign4:
314 LOGD("%p (%04x): .align4\n", baseAddr + offset, offset);
316 case kArmPseudoPCReconstructionCell:
317 LOGD("-------- reconstruct dalvik PC : 0x%04x @ +0x%04x\n", dest,
320 case kArmPseudoPCReconstructionBlockLabel:
323 case kArmPseudoEHBlockLabel:
324 LOGD("Exception_Handling:\n");
326 case kArmPseudoNormalBlockLabel:
327 LOGD("L%#06x:\n", dest);
330 if (lir->isNop && !dumpNop) {
333 buildInsnString(EncodingMap[lir->opCode].name, lir, opName,
335 buildInsnString(EncodingMap[lir->opCode].fmt, lir, buf, baseAddr,
337 LOGD("%p (%04x): %-8s%s%s\n",
338 baseAddr + offset, offset, opName, buf,
339 lir->isNop ? "(nop)" : "");
343 if (lir->useMask && (!lir->isNop || dumpNop)) {
344 DUMP_RESOURCE_MASK(dvmDumpResourceMask((LIR *) lir,
345 lir->useMask, "use"));
347 if (lir->defMask && (!lir->isNop || dumpNop)) {
348 DUMP_RESOURCE_MASK(dvmDumpResourceMask((LIR *) lir,
349 lir->defMask, "def"));
353 /* Dump instructions and constant pool contents */
354 void dvmCompilerCodegenDump(CompilationUnit *cUnit)
356 LOGD("Dumping LIR insns\n");
360 LOGD("installed code is at %p\n", cUnit->baseAddr);
361 LOGD("total size is %d bytes\n", cUnit->totalSize);
362 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
363 dvmDumpLIRInsn(lirInsn, cUnit->baseAddr);
365 for (lirInsn = cUnit->wordList; lirInsn; lirInsn = lirInsn->next) {
366 armLIR = (ArmLIR *) lirInsn;
367 LOGD("%p (%04x): .word (0x%x)\n",
368 (char*)cUnit->baseAddr + armLIR->generic.offset,
369 armLIR->generic.offset,
370 armLIR->operands[0]);