blob: 7d7f119f612743ed8daa62b1b8e4bf79bafb352f [file] [log] [blame]
Ben Chengba4fc8b2009-06-01 13:00:29 -07001/*
2 * Copyright (C) 2009 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 "dexdump/OpCodeNames.h"
19#include "Armv5teLIR.h"
20
21/* Decode and print a ARM register name */
22static char * decodeRegList(int vector, char *buf)
23{
24 int i;
25 bool printed = false;
26 buf[0] = 0;
27 for (i = 0; i < 8; i++, vector >>= 1) {
28 if (vector & 0x1) {
29 if (printed) {
30 sprintf(buf + strlen(buf), ", r%d", i);
31 } else {
32 printed = true;
33 sprintf(buf, "r%d", i);
34 }
35 }
36 }
37 return buf;
38}
39
40/*
41 * Interpret a format string and build a string no longer than size
42 * See format key in Assemble.c.
43 */
44static void buildInsnString(char *fmt, Armv5teLIR *lir, char* buf,
45 unsigned char *baseAddr, int size)
46{
47 int i;
48 char *bufEnd = &buf[size-1];
49 char *fmtEnd = &fmt[strlen(fmt)];
50 char tbuf[256];
51 char nc;
52 while (fmt < fmtEnd) {
53 int operand;
54 if (*fmt == '!') {
55 fmt++;
56 assert(fmt < fmtEnd);
57 nc = *fmt++;
58 if (nc=='!') {
59 strcpy(tbuf, "!");
60 } else {
61 assert(fmt < fmtEnd);
62 assert((unsigned)(nc-'0') < 3);
63 operand = lir->operands[nc-'0'];
64 switch(*fmt++) {
65 case 'h':
66 sprintf(tbuf,"%04x", operand);
67 break;
68 case 'd':
69 sprintf(tbuf,"%d", operand);
70 break;
71 case 'D':
72 sprintf(tbuf,"%d", operand+8);
73 break;
74 case 'E':
75 sprintf(tbuf,"%d", operand*4);
76 break;
77 case 'F':
78 sprintf(tbuf,"%d", operand*2);
79 break;
80 case 'c':
81 switch (operand) {
82 case ARM_COND_EQ:
83 strcpy(tbuf, "beq");
84 break;
85 case ARM_COND_NE:
86 strcpy(tbuf, "bne");
87 break;
88 case ARM_COND_LT:
89 strcpy(tbuf, "blt");
90 break;
91 case ARM_COND_GE:
92 strcpy(tbuf, "bge");
93 break;
94 case ARM_COND_GT:
95 strcpy(tbuf, "bgt");
96 break;
97 case ARM_COND_LE:
98 strcpy(tbuf, "ble");
99 break;
100 case ARM_COND_CS:
101 strcpy(tbuf, "bcs");
102 break;
103 default:
104 strcpy(tbuf, "");
105 break;
106 }
107 break;
108 case 't':
109 sprintf(tbuf,"0x%08x",
110 (int) baseAddr + lir->generic.offset + 4 +
111 (operand << 1));
112 break;
113 case 'u': {
114 int offset_1 = lir->operands[0];
115 int offset_2 = NEXT_LIR(lir)->operands[0];
116 intptr_t target =
117 ((((intptr_t) baseAddr + lir->generic.offset + 4) &
118 ~3) + (offset_1 << 21 >> 9) + (offset_2 << 1)) &
119 0xfffffffc;
120 sprintf(tbuf, "%p", (void *) target);
121 break;
122 }
123
124 /* Nothing to print for BLX_2 */
125 case 'v':
126 strcpy(tbuf, "see above");
127 break;
128 case 'R':
129 decodeRegList(operand, tbuf);
130 break;
131 default:
132 strcpy(tbuf,"DecodeError");
133 break;
134 }
135 if (buf+strlen(tbuf) <= bufEnd) {
136 strcpy(buf, tbuf);
137 buf += strlen(tbuf);
138 } else {
139 break;
140 }
141 }
142 } else {
143 *buf++ = *fmt++;
144 }
145 if (buf == bufEnd)
146 break;
147 }
148 *buf = 0;
149}
150
151/* Pretty-print a LIR instruction */
152static void dumpLIRInsn(LIR *arg, unsigned char *baseAddr)
153{
154 Armv5teLIR *lir = (Armv5teLIR *) arg;
155 char buf[256];
156 char opName[256];
157 int offset = lir->generic.offset;
158 int dest = lir->operands[0];
159 u2 *cPtr = (u2*)baseAddr;
160 /* Handle pseudo-ops individually, and all regular insns as a group */
161 switch(lir->opCode) {
162 case ARMV5TE_PSEUDO_TARGET_LABEL:
163 break;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700164 case ARMV5TE_PSEUDO_CHAINING_CELL_NORMAL:
165 LOGD("-------- chaining cell (normal): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700166 break;
Ben Cheng1efc9c52009-06-08 18:25:27 -0700167 case ARMV5TE_PSEUDO_CHAINING_CELL_HOT:
168 LOGD("-------- chaining cell (hot): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700169 break;
170 case ARMV5TE_PSEUDO_CHAINING_CELL_INVOKE:
171 LOGD("-------- chaining cell (invoke): %s/%p\n",
172 ((Method *)dest)->name,
173 ((Method *)dest)->insns);
174 break;
175 case ARMV5TE_PSEUDO_DALVIK_BYTECODE_BOUNDARY:
176 LOGD("-------- dalvik offset: 0x%04x @ %s\n", dest,
177 getOpcodeName(lir->operands[1]));
178 break;
179 case ARMV5TE_PSEUDO_ALIGN4:
180 LOGD("%p (%04x): .align4\n", baseAddr + offset, offset);
181 break;
182 case ARMV5TE_PSEUDO_PC_RECONSTRUCTION_CELL:
183 LOGD("-------- reconstruct dalvik PC : 0x%04x @ +0x%04x\n", dest,
184 lir->operands[1]);
185 break;
186 case ARMV5TE_PSEUDO_PC_RECONSTRUCTION_BLOCK_LABEL:
187 /* Do nothing */
188 break;
189 case ARMV5TE_PSEUDO_EH_BLOCK_LABEL:
190 LOGD("Exception_Handling:\n");
191 break;
192 case ARMV5TE_PSEUDO_NORMAL_BLOCK_LABEL:
193 LOGD("L%#06x:\n", dest);
194 break;
195 default:
Ben Chenge9695e52009-06-16 16:11:47 -0700196 if (lir->isNop) {
197 break;
198 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700199 buildInsnString(EncodingMap[lir->opCode].name, lir, opName,
200 baseAddr, 256);
201 buildInsnString(EncodingMap[lir->opCode].fmt, lir, buf, baseAddr,
202 256);
Ben Chenge9695e52009-06-16 16:11:47 -0700203 LOGD("%p (%04x): %-8s%s\n",
204 baseAddr + offset, offset, opName, buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700205 break;
206 }
207}
208
209/* Dump instructions and constant pool contents */
210void dvmCompilerCodegenDump(CompilationUnit *cUnit)
211{
212 LOGD("Dumping LIR insns\n");
213 LIR *lirInsn;
214 Armv5teLIR *armLIR;
215
216 LOGD("installed code is at %p\n", cUnit->baseAddr);
217 LOGD("total size is %d bytes\n", cUnit->totalSize);
218 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
219 dumpLIRInsn(lirInsn, cUnit->baseAddr);
220 }
221 for (lirInsn = cUnit->wordList; lirInsn; lirInsn = lirInsn->next) {
222 armLIR = (Armv5teLIR *) lirInsn;
223 LOGD("%p (%04x): .word (0x%x)\n",
224 cUnit->baseAddr + armLIR->generic.offset, armLIR->generic.offset,
225 armLIR->operands[0]);
226 }
227}