blob: 551e1f7532569ea51659ac874b5412d4b141e1bc [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;
113 case 'D':
114 sprintf(tbuf,"%d", operand+8);
115 break;
116 case 'E':
117 sprintf(tbuf,"%d", operand*4);
118 break;
119 case 'F':
120 sprintf(tbuf,"%d", operand*2);
121 break;
122 case 'c':
123 switch (operand) {
124 case ARM_COND_EQ:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700125 strcpy(tbuf, "eq");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700126 break;
127 case ARM_COND_NE:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700128 strcpy(tbuf, "ne");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700129 break;
130 case ARM_COND_LT:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700131 strcpy(tbuf, "lt");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700132 break;
133 case ARM_COND_GE:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700134 strcpy(tbuf, "ge");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700135 break;
136 case ARM_COND_GT:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700137 strcpy(tbuf, "gt");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700138 break;
139 case ARM_COND_LE:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700140 strcpy(tbuf, "le");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700141 break;
142 case ARM_COND_CS:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700143 strcpy(tbuf, "cs");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700144 break;
Bill Buzbee7ea0f642009-08-10 17:06:51 -0700145 case ARM_COND_MI:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700146 strcpy(tbuf, "mi");
Bill Buzbee7ea0f642009-08-10 17:06:51 -0700147 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700148 default:
149 strcpy(tbuf, "");
150 break;
151 }
152 break;
153 case 't':
154 sprintf(tbuf,"0x%08x",
155 (int) baseAddr + lir->generic.offset + 4 +
156 (operand << 1));
157 break;
158 case 'u': {
159 int offset_1 = lir->operands[0];
160 int offset_2 = NEXT_LIR(lir)->operands[0];
161 intptr_t target =
162 ((((intptr_t) baseAddr + lir->generic.offset + 4) &
163 ~3) + (offset_1 << 21 >> 9) + (offset_2 << 1)) &
164 0xfffffffc;
165 sprintf(tbuf, "%p", (void *) target);
166 break;
167 }
168
169 /* Nothing to print for BLX_2 */
170 case 'v':
171 strcpy(tbuf, "see above");
172 break;
173 case 'R':
174 decodeRegList(operand, tbuf);
175 break;
176 default:
177 strcpy(tbuf,"DecodeError");
178 break;
179 }
180 if (buf+strlen(tbuf) <= bufEnd) {
181 strcpy(buf, tbuf);
182 buf += strlen(tbuf);
183 } else {
184 break;
185 }
186 }
187 } else {
188 *buf++ = *fmt++;
189 }
190 if (buf == bufEnd)
191 break;
192 }
193 *buf = 0;
194}
195
196/* Pretty-print a LIR instruction */
197static void dumpLIRInsn(LIR *arg, unsigned char *baseAddr)
198{
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700199 ArmLIR *lir = (ArmLIR *) arg;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700200 char buf[256];
201 char opName[256];
202 int offset = lir->generic.offset;
203 int dest = lir->operands[0];
204 u2 *cPtr = (u2*)baseAddr;
205 /* Handle pseudo-ops individually, and all regular insns as a group */
206 switch(lir->opCode) {
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700207 case ARM_PSEUDO_IT_BOTTOM:
208 LOGD("-------- IT_Bottom");
209 break;
Ben Cheng4238ec22009-08-24 16:32:22 -0700210 case ARM_PSEUDO_EXTENDED_MIR:
211 /* intentional fallthrough */
212 case ARM_PSEUDO_SSA_REP:
213 LOGD("-------- %s\n", (char *) dest);
214 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700215 case ARM_PSEUDO_TARGET_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700216 break;
Ben Cheng4238ec22009-08-24 16:32:22 -0700217 case ARM_PSEUDO_CHAINING_CELL_BACKWARD_BRANCH:
218 LOGD("-------- chaining cell (backward branch): 0x%04x\n", dest);
219 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700220 case ARM_PSEUDO_CHAINING_CELL_NORMAL:
Ben Cheng1efc9c52009-06-08 18:25:27 -0700221 LOGD("-------- chaining cell (normal): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700222 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700223 case ARM_PSEUDO_CHAINING_CELL_HOT:
Ben Cheng1efc9c52009-06-08 18:25:27 -0700224 LOGD("-------- chaining cell (hot): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700225 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700226 case ARM_PSEUDO_CHAINING_CELL_INVOKE_PREDICTED:
Ben Cheng38329f52009-07-07 14:19:20 -0700227 LOGD("-------- chaining cell (predicted)\n");
228 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700229 case ARM_PSEUDO_CHAINING_CELL_INVOKE_SINGLETON:
Ben Cheng38329f52009-07-07 14:19:20 -0700230 LOGD("-------- chaining cell (invoke singleton): %s/%p\n",
Ben Chengba4fc8b2009-06-01 13:00:29 -0700231 ((Method *)dest)->name,
232 ((Method *)dest)->insns);
233 break;
Ben Cheng4238ec22009-08-24 16:32:22 -0700234 case ARM_PSEUDO_ENTRY_BLOCK:
235 LOGD("-------- entry offset: 0x%04x\n", dest);
Jeff Hao97319a82009-08-12 16:57:15 -0700236 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700237 case ARM_PSEUDO_DALVIK_BYTECODE_BOUNDARY:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700238 LOGD("-------- dalvik offset: 0x%04x @ %s\n", dest,
239 getOpcodeName(lir->operands[1]));
240 break;
Ben Cheng4238ec22009-08-24 16:32:22 -0700241 case ARM_PSEUDO_EXIT_BLOCK:
242 LOGD("-------- exit offset: 0x%04x\n", dest);
243 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700244 case ARM_PSEUDO_ALIGN4:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700245 LOGD("%p (%04x): .align4\n", baseAddr + offset, offset);
246 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700247 case ARM_PSEUDO_PC_RECONSTRUCTION_CELL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700248 LOGD("-------- reconstruct dalvik PC : 0x%04x @ +0x%04x\n", dest,
249 lir->operands[1]);
250 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700251 case ARM_PSEUDO_PC_RECONSTRUCTION_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700252 /* Do nothing */
253 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700254 case ARM_PSEUDO_EH_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700255 LOGD("Exception_Handling:\n");
256 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700257 case ARM_PSEUDO_NORMAL_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700258 LOGD("L%#06x:\n", dest);
259 break;
260 default:
Ben Chenge9695e52009-06-16 16:11:47 -0700261 if (lir->isNop) {
262 break;
263 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700264 buildInsnString(EncodingMap[lir->opCode].name, lir, opName,
265 baseAddr, 256);
266 buildInsnString(EncodingMap[lir->opCode].fmt, lir, buf, baseAddr,
267 256);
Ben Chenge9695e52009-06-16 16:11:47 -0700268 LOGD("%p (%04x): %-8s%s\n",
269 baseAddr + offset, offset, opName, buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700270 break;
271 }
272}
273
274/* Dump instructions and constant pool contents */
275void dvmCompilerCodegenDump(CompilationUnit *cUnit)
276{
277 LOGD("Dumping LIR insns\n");
278 LIR *lirInsn;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700279 ArmLIR *armLIR;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700280
281 LOGD("installed code is at %p\n", cUnit->baseAddr);
282 LOGD("total size is %d bytes\n", cUnit->totalSize);
283 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
284 dumpLIRInsn(lirInsn, cUnit->baseAddr);
285 }
286 for (lirInsn = cUnit->wordList; lirInsn; lirInsn = lirInsn->next) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700287 armLIR = (ArmLIR *) lirInsn;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700288 LOGD("%p (%04x): .word (0x%x)\n",
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700289 (char*)cUnit->baseAddr + armLIR->generic.offset,
290 armLIR->generic.offset,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700291 armLIR->operands[0]);
292 }
293}