blob: 57d5d74fbfb32429c58e4186bd593e594fa847d8 [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"
Bill Buzbee89efc3d2009-07-28 11:22:22 -070019#include "ArmLIR.h"
Ben Chengba4fc8b2009-06-01 13:00:29 -070020
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
Bill Buzbee7ea0f642009-08-10 17:06:51 -070040static int expandImmediate(int value)
41{
42 int mode = (value & 0xf00) >> 8;
43 u4 bits = value & 0xff;
44 switch(mode) {
45 case 0:
46 return bits;
47 case 1:
48 return (bits << 16) | bits;
49 case 2:
50 return (bits << 24) | (bits << 8);
51 case 3:
52 return (bits << 24) | (bits << 16) | (bits << 8) | bits;
53 default:
54 break;
55 }
56 bits = (bits | 0x80) << 24;
57 return bits >> (((value & 0xf80) >> 7) - 8);
58}
59
Ben Chengba4fc8b2009-06-01 13:00:29 -070060/*
61 * Interpret a format string and build a string no longer than size
62 * See format key in Assemble.c.
63 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -070064static void buildInsnString(char *fmt, ArmLIR *lir, char* buf,
Ben Chengba4fc8b2009-06-01 13:00:29 -070065 unsigned char *baseAddr, int size)
66{
67 int i;
68 char *bufEnd = &buf[size-1];
69 char *fmtEnd = &fmt[strlen(fmt)];
70 char tbuf[256];
71 char nc;
72 while (fmt < fmtEnd) {
73 int operand;
74 if (*fmt == '!') {
75 fmt++;
76 assert(fmt < fmtEnd);
77 nc = *fmt++;
78 if (nc=='!') {
79 strcpy(tbuf, "!");
80 } else {
81 assert(fmt < fmtEnd);
Bill Buzbee270c1d62009-08-13 16:58:07 -070082 assert((unsigned)(nc-'0') < 4);
Ben Chengba4fc8b2009-06-01 13:00:29 -070083 operand = lir->operands[nc-'0'];
84 switch(*fmt++) {
Bill Buzbeea4a7f072009-08-27 13:58:09 -070085 case 'b':
86 strcpy(tbuf,"0000");
87 for (i=3; i>= 0; i--) {
88 tbuf[i] += operand & 1;
89 operand >>= 1;
90 }
91 break;
Bill Buzbee270c1d62009-08-13 16:58:07 -070092 case 'n':
93 operand = ~expandImmediate(operand);
94 sprintf(tbuf,"%d [0x%x]", operand, operand);
95 break;
Bill Buzbee7ea0f642009-08-10 17:06:51 -070096 case 'm':
97 operand = expandImmediate(operand);
98 sprintf(tbuf,"%d [0x%x]", operand, operand);
99 break;
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700100 case 's':
101 sprintf(tbuf,"s%d",operand & FP_REG_MASK);
102 break;
103 case 'S':
104 sprintf(tbuf,"d%d",(operand & FP_REG_MASK) >> 1);
105 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700106 case 'h':
107 sprintf(tbuf,"%04x", operand);
108 break;
Bill Buzbee7ea0f642009-08-10 17:06:51 -0700109 case 'M':
Ben Chengba4fc8b2009-06-01 13:00:29 -0700110 case 'd':
111 sprintf(tbuf,"%d", operand);
112 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700113 case 'E':
114 sprintf(tbuf,"%d", operand*4);
115 break;
116 case 'F':
117 sprintf(tbuf,"%d", operand*2);
118 break;
119 case 'c':
120 switch (operand) {
121 case ARM_COND_EQ:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700122 strcpy(tbuf, "eq");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700123 break;
124 case ARM_COND_NE:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700125 strcpy(tbuf, "ne");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700126 break;
127 case ARM_COND_LT:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700128 strcpy(tbuf, "lt");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700129 break;
130 case ARM_COND_GE:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700131 strcpy(tbuf, "ge");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700132 break;
133 case ARM_COND_GT:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700134 strcpy(tbuf, "gt");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700135 break;
136 case ARM_COND_LE:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700137 strcpy(tbuf, "le");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700138 break;
139 case ARM_COND_CS:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700140 strcpy(tbuf, "cs");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700141 break;
Bill Buzbee7ea0f642009-08-10 17:06:51 -0700142 case ARM_COND_MI:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700143 strcpy(tbuf, "mi");
Bill Buzbee7ea0f642009-08-10 17:06:51 -0700144 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700145 default:
146 strcpy(tbuf, "");
147 break;
148 }
149 break;
150 case 't':
151 sprintf(tbuf,"0x%08x",
152 (int) baseAddr + lir->generic.offset + 4 +
153 (operand << 1));
154 break;
155 case 'u': {
156 int offset_1 = lir->operands[0];
157 int offset_2 = NEXT_LIR(lir)->operands[0];
158 intptr_t target =
159 ((((intptr_t) baseAddr + lir->generic.offset + 4) &
160 ~3) + (offset_1 << 21 >> 9) + (offset_2 << 1)) &
161 0xfffffffc;
162 sprintf(tbuf, "%p", (void *) target);
163 break;
164 }
165
166 /* Nothing to print for BLX_2 */
167 case 'v':
168 strcpy(tbuf, "see above");
169 break;
170 case 'R':
171 decodeRegList(operand, tbuf);
172 break;
173 default:
174 strcpy(tbuf,"DecodeError");
175 break;
176 }
177 if (buf+strlen(tbuf) <= bufEnd) {
178 strcpy(buf, tbuf);
179 buf += strlen(tbuf);
180 } else {
181 break;
182 }
183 }
184 } else {
185 *buf++ = *fmt++;
186 }
187 if (buf == bufEnd)
188 break;
189 }
190 *buf = 0;
191}
192
Ben Chengd7d426a2009-09-22 11:23:36 -0700193void dvmDumpResourceMask(LIR *lir, u8 mask, const char *prefix)
194{
195 char buf[256];
196 buf[0] = 0;
197 ArmLIR *armLIR = (ArmLIR *) lir;
198
199 if (mask == ENCODE_ALL) {
200 strcpy(buf, "all");
201 } else {
202 char num[8];
203 int i;
204
205 for (i = 0; i < kRegEnd; i++) {
206 if (mask & (1ULL << i)) {
207 sprintf(num, "%d ", i);
208 strcat(buf, num);
209 }
210 }
211
212 if (mask & ENCODE_CCODE) {
213 strcat(buf, "cc ");
214 }
215 if (mask & ENCODE_FP_STATUS) {
216 strcat(buf, "fpcc ");
217 }
218 if (armLIR && (mask & ENCODE_DALVIK_REG)) {
219 sprintf(buf + strlen(buf), "dr%d%s", armLIR->aliasInfo & 0xffff,
220 (armLIR->aliasInfo & 0x80000000) ? "(+1)" : "");
221 }
222 }
223 if (buf[0]) {
224 LOGD("%s: %s", prefix, buf);
225 }
226}
227
228/*
229 * Debugging macros
230 */
231#define DUMP_RESOURCE_MASK(X)
232#define DUMP_SSA_REP(X)
233
Ben Chengba4fc8b2009-06-01 13:00:29 -0700234/* Pretty-print a LIR instruction */
Ben Chengd7d426a2009-09-22 11:23:36 -0700235void dvmDumpLIRInsn(LIR *arg, unsigned char *baseAddr)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700236{
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700237 ArmLIR *lir = (ArmLIR *) arg;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700238 char buf[256];
239 char opName[256];
240 int offset = lir->generic.offset;
241 int dest = lir->operands[0];
242 u2 *cPtr = (u2*)baseAddr;
Ben Chengd7d426a2009-09-22 11:23:36 -0700243 const bool dumpNop = false;
244
Ben Chengba4fc8b2009-06-01 13:00:29 -0700245 /* Handle pseudo-ops individually, and all regular insns as a group */
246 switch(lir->opCode) {
Ben Chengd7d426a2009-09-22 11:23:36 -0700247 case ARM_PSEUDO_BARRIER:
248 LOGD("-------- BARRIER");
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700249 break;
Ben Cheng4238ec22009-08-24 16:32:22 -0700250 case ARM_PSEUDO_EXTENDED_MIR:
251 /* intentional fallthrough */
252 case ARM_PSEUDO_SSA_REP:
Ben Chengd7d426a2009-09-22 11:23:36 -0700253 DUMP_SSA_REP(LOGD("-------- %s\n", (char *) dest));
Ben Cheng4238ec22009-08-24 16:32:22 -0700254 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700255 case ARM_PSEUDO_TARGET_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700256 break;
Ben Cheng4238ec22009-08-24 16:32:22 -0700257 case ARM_PSEUDO_CHAINING_CELL_BACKWARD_BRANCH:
258 LOGD("-------- chaining cell (backward branch): 0x%04x\n", dest);
259 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700260 case ARM_PSEUDO_CHAINING_CELL_NORMAL:
Ben Cheng1efc9c52009-06-08 18:25:27 -0700261 LOGD("-------- chaining cell (normal): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700262 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700263 case ARM_PSEUDO_CHAINING_CELL_HOT:
Ben Cheng1efc9c52009-06-08 18:25:27 -0700264 LOGD("-------- chaining cell (hot): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700265 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700266 case ARM_PSEUDO_CHAINING_CELL_INVOKE_PREDICTED:
Ben Cheng38329f52009-07-07 14:19:20 -0700267 LOGD("-------- chaining cell (predicted)\n");
268 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700269 case ARM_PSEUDO_CHAINING_CELL_INVOKE_SINGLETON:
Ben Cheng38329f52009-07-07 14:19:20 -0700270 LOGD("-------- chaining cell (invoke singleton): %s/%p\n",
Ben Chengba4fc8b2009-06-01 13:00:29 -0700271 ((Method *)dest)->name,
272 ((Method *)dest)->insns);
273 break;
Ben Cheng4238ec22009-08-24 16:32:22 -0700274 case ARM_PSEUDO_ENTRY_BLOCK:
275 LOGD("-------- entry offset: 0x%04x\n", dest);
Jeff Hao97319a82009-08-12 16:57:15 -0700276 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700277 case ARM_PSEUDO_DALVIK_BYTECODE_BOUNDARY:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700278 LOGD("-------- dalvik offset: 0x%04x @ %s\n", dest,
Ben Chengccd6c012009-10-15 14:52:45 -0700279 (char *) lir->operands[1]);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700280 break;
Ben Cheng4238ec22009-08-24 16:32:22 -0700281 case ARM_PSEUDO_EXIT_BLOCK:
282 LOGD("-------- exit offset: 0x%04x\n", dest);
283 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700284 case ARM_PSEUDO_ALIGN4:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700285 LOGD("%p (%04x): .align4\n", baseAddr + offset, offset);
286 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700287 case ARM_PSEUDO_PC_RECONSTRUCTION_CELL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700288 LOGD("-------- reconstruct dalvik PC : 0x%04x @ +0x%04x\n", dest,
289 lir->operands[1]);
290 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700291 case ARM_PSEUDO_PC_RECONSTRUCTION_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292 /* Do nothing */
293 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700294 case ARM_PSEUDO_EH_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700295 LOGD("Exception_Handling:\n");
296 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700297 case ARM_PSEUDO_NORMAL_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700298 LOGD("L%#06x:\n", dest);
299 break;
300 default:
Ben Chengd7d426a2009-09-22 11:23:36 -0700301 if (lir->isNop && !dumpNop) {
Ben Chenge9695e52009-06-16 16:11:47 -0700302 break;
303 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700304 buildInsnString(EncodingMap[lir->opCode].name, lir, opName,
305 baseAddr, 256);
306 buildInsnString(EncodingMap[lir->opCode].fmt, lir, buf, baseAddr,
307 256);
Ben Chengd7d426a2009-09-22 11:23:36 -0700308 LOGD("%p (%04x): %-8s%s%s\n",
309 baseAddr + offset, offset, opName, buf,
310 lir->isNop ? "(nop)" : "");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700311 break;
312 }
Ben Chengd7d426a2009-09-22 11:23:36 -0700313
314 if (lir->useMask && (!lir->isNop || dumpNop)) {
315 DUMP_RESOURCE_MASK(dvmDumpResourceMask((LIR *) lir,
316 lir->useMask, "use"));
317 }
318 if (lir->defMask && (!lir->isNop || dumpNop)) {
319 DUMP_RESOURCE_MASK(dvmDumpResourceMask((LIR *) lir,
320 lir->defMask, "def"));
321 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700322}
323
324/* Dump instructions and constant pool contents */
325void dvmCompilerCodegenDump(CompilationUnit *cUnit)
326{
327 LOGD("Dumping LIR insns\n");
328 LIR *lirInsn;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700329 ArmLIR *armLIR;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700330
331 LOGD("installed code is at %p\n", cUnit->baseAddr);
332 LOGD("total size is %d bytes\n", cUnit->totalSize);
333 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
Ben Chengd7d426a2009-09-22 11:23:36 -0700334 dvmDumpLIRInsn(lirInsn, cUnit->baseAddr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700335 }
336 for (lirInsn = cUnit->wordList; lirInsn; lirInsn = lirInsn->next) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700337 armLIR = (ArmLIR *) lirInsn;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700338 LOGD("%p (%04x): .word (0x%x)\n",
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700339 (char*)cUnit->baseAddr + armLIR->generic.offset,
340 armLIR->generic.offset,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700341 armLIR->operands[0]);
342 }
343}