blob: 7f6d28464a2d5280aff3203f4f565855d24b1def [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);
82 assert((unsigned)(nc-'0') < 3);
83 operand = lir->operands[nc-'0'];
84 switch(*fmt++) {
Bill Buzbee7ea0f642009-08-10 17:06:51 -070085 case 'm':
86 operand = expandImmediate(operand);
87 sprintf(tbuf,"%d [0x%x]", operand, operand);
88 break;
Bill Buzbee9727c3d2009-08-01 11:32:36 -070089 case 's':
90 sprintf(tbuf,"s%d",operand & FP_REG_MASK);
91 break;
92 case 'S':
93 sprintf(tbuf,"d%d",(operand & FP_REG_MASK) >> 1);
94 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -070095 case 'h':
96 sprintf(tbuf,"%04x", operand);
97 break;
Bill Buzbee7ea0f642009-08-10 17:06:51 -070098 case 'M':
Ben Chengba4fc8b2009-06-01 13:00:29 -070099 case 'd':
100 sprintf(tbuf,"%d", operand);
101 break;
102 case 'D':
103 sprintf(tbuf,"%d", operand+8);
104 break;
105 case 'E':
106 sprintf(tbuf,"%d", operand*4);
107 break;
108 case 'F':
109 sprintf(tbuf,"%d", operand*2);
110 break;
111 case 'c':
112 switch (operand) {
113 case ARM_COND_EQ:
114 strcpy(tbuf, "beq");
115 break;
116 case ARM_COND_NE:
117 strcpy(tbuf, "bne");
118 break;
119 case ARM_COND_LT:
120 strcpy(tbuf, "blt");
121 break;
122 case ARM_COND_GE:
123 strcpy(tbuf, "bge");
124 break;
125 case ARM_COND_GT:
126 strcpy(tbuf, "bgt");
127 break;
128 case ARM_COND_LE:
129 strcpy(tbuf, "ble");
130 break;
131 case ARM_COND_CS:
132 strcpy(tbuf, "bcs");
133 break;
Bill Buzbee7ea0f642009-08-10 17:06:51 -0700134 case ARM_COND_MI:
135 strcpy(tbuf, "bmi");
136 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700137 default:
138 strcpy(tbuf, "");
139 break;
140 }
141 break;
142 case 't':
143 sprintf(tbuf,"0x%08x",
144 (int) baseAddr + lir->generic.offset + 4 +
145 (operand << 1));
146 break;
147 case 'u': {
148 int offset_1 = lir->operands[0];
149 int offset_2 = NEXT_LIR(lir)->operands[0];
150 intptr_t target =
151 ((((intptr_t) baseAddr + lir->generic.offset + 4) &
152 ~3) + (offset_1 << 21 >> 9) + (offset_2 << 1)) &
153 0xfffffffc;
154 sprintf(tbuf, "%p", (void *) target);
155 break;
156 }
157
158 /* Nothing to print for BLX_2 */
159 case 'v':
160 strcpy(tbuf, "see above");
161 break;
162 case 'R':
163 decodeRegList(operand, tbuf);
164 break;
165 default:
166 strcpy(tbuf,"DecodeError");
167 break;
168 }
169 if (buf+strlen(tbuf) <= bufEnd) {
170 strcpy(buf, tbuf);
171 buf += strlen(tbuf);
172 } else {
173 break;
174 }
175 }
176 } else {
177 *buf++ = *fmt++;
178 }
179 if (buf == bufEnd)
180 break;
181 }
182 *buf = 0;
183}
184
185/* Pretty-print a LIR instruction */
186static void dumpLIRInsn(LIR *arg, unsigned char *baseAddr)
187{
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700188 ArmLIR *lir = (ArmLIR *) arg;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700189 char buf[256];
190 char opName[256];
191 int offset = lir->generic.offset;
192 int dest = lir->operands[0];
193 u2 *cPtr = (u2*)baseAddr;
194 /* Handle pseudo-ops individually, and all regular insns as a group */
195 switch(lir->opCode) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700196 case ARM_PSEUDO_TARGET_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700197 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700198 case ARM_PSEUDO_CHAINING_CELL_NORMAL:
Ben Cheng1efc9c52009-06-08 18:25:27 -0700199 LOGD("-------- chaining cell (normal): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700200 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700201 case ARM_PSEUDO_CHAINING_CELL_HOT:
Ben Cheng1efc9c52009-06-08 18:25:27 -0700202 LOGD("-------- chaining cell (hot): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700203 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700204 case ARM_PSEUDO_CHAINING_CELL_INVOKE_PREDICTED:
Ben Cheng38329f52009-07-07 14:19:20 -0700205 LOGD("-------- chaining cell (predicted)\n");
206 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700207 case ARM_PSEUDO_CHAINING_CELL_INVOKE_SINGLETON:
Ben Cheng38329f52009-07-07 14:19:20 -0700208 LOGD("-------- chaining cell (invoke singleton): %s/%p\n",
Ben Chengba4fc8b2009-06-01 13:00:29 -0700209 ((Method *)dest)->name,
210 ((Method *)dest)->insns);
211 break;
Jeff Hao97319a82009-08-12 16:57:15 -0700212 case ARM_PSEUDO_CHAINING_CELL_BACKWARD_BRANCH:
213 LOGD("-------- chaining cell (backward branch): 0x%04x\n", dest);
214 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700215 case ARM_PSEUDO_DALVIK_BYTECODE_BOUNDARY:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700216 LOGD("-------- dalvik offset: 0x%04x @ %s\n", dest,
217 getOpcodeName(lir->operands[1]));
218 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700219 case ARM_PSEUDO_ALIGN4:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700220 LOGD("%p (%04x): .align4\n", baseAddr + offset, offset);
221 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700222 case ARM_PSEUDO_PC_RECONSTRUCTION_CELL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700223 LOGD("-------- reconstruct dalvik PC : 0x%04x @ +0x%04x\n", dest,
224 lir->operands[1]);
225 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700226 case ARM_PSEUDO_PC_RECONSTRUCTION_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700227 /* Do nothing */
228 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700229 case ARM_PSEUDO_EH_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700230 LOGD("Exception_Handling:\n");
231 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700232 case ARM_PSEUDO_NORMAL_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700233 LOGD("L%#06x:\n", dest);
234 break;
235 default:
Ben Chenge9695e52009-06-16 16:11:47 -0700236 if (lir->isNop) {
237 break;
238 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700239 buildInsnString(EncodingMap[lir->opCode].name, lir, opName,
240 baseAddr, 256);
241 buildInsnString(EncodingMap[lir->opCode].fmt, lir, buf, baseAddr,
242 256);
Ben Chenge9695e52009-06-16 16:11:47 -0700243 LOGD("%p (%04x): %-8s%s\n",
244 baseAddr + offset, offset, opName, buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700245 break;
246 }
247}
248
249/* Dump instructions and constant pool contents */
250void dvmCompilerCodegenDump(CompilationUnit *cUnit)
251{
252 LOGD("Dumping LIR insns\n");
253 LIR *lirInsn;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700254 ArmLIR *armLIR;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700255
256 LOGD("installed code is at %p\n", cUnit->baseAddr);
257 LOGD("total size is %d bytes\n", cUnit->totalSize);
258 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
259 dumpLIRInsn(lirInsn, cUnit->baseAddr);
260 }
261 for (lirInsn = cUnit->wordList; lirInsn; lirInsn = lirInsn->next) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700262 armLIR = (ArmLIR *) lirInsn;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700263 LOGD("%p (%04x): .word (0x%x)\n",
Bill Buzbee716f1202009-07-23 13:22:09 -0700264 (char*)cUnit->baseAddr + armLIR->generic.offset, armLIR->generic.offset,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700265 armLIR->operands[0]);
266 }
267}