blob: d5acd13ef6f00bd142480b9be1eef14c2bc98049 [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"
Andy McFaddenc6b25c72010-06-22 11:01:20 -070018#include "libdex/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];
buzbeeecf8f6e2010-07-20 14:53:42 -070071 char *name;
Ben Chengba4fc8b2009-06-01 13:00:29 -070072 char nc;
73 while (fmt < fmtEnd) {
74 int operand;
75 if (*fmt == '!') {
76 fmt++;
77 assert(fmt < fmtEnd);
78 nc = *fmt++;
79 if (nc=='!') {
80 strcpy(tbuf, "!");
81 } else {
82 assert(fmt < fmtEnd);
Bill Buzbee270c1d62009-08-13 16:58:07 -070083 assert((unsigned)(nc-'0') < 4);
Ben Chengba4fc8b2009-06-01 13:00:29 -070084 operand = lir->operands[nc-'0'];
85 switch(*fmt++) {
buzbeeecf8f6e2010-07-20 14:53:42 -070086 case 'B':
87 switch (operand) {
88 case kSY:
89 name = "sy";
90 break;
91 case kST:
92 name = "st";
93 break;
94 case kISH:
95 name = "ish";
96 break;
97 case kISHST:
98 name = "ishst";
99 break;
100 case kNSH:
101 name = "nsh";
102 break;
103 case kNSHST:
104 name = "shst";
105 break;
106 default:
107 name = "DecodeError";
108 break;
109 }
110 strcpy(tbuf, name);
111 break;
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700112 case 'b':
113 strcpy(tbuf,"0000");
114 for (i=3; i>= 0; i--) {
115 tbuf[i] += operand & 1;
116 operand >>= 1;
117 }
118 break;
Bill Buzbee270c1d62009-08-13 16:58:07 -0700119 case 'n':
120 operand = ~expandImmediate(operand);
121 sprintf(tbuf,"%d [0x%x]", operand, operand);
122 break;
Bill Buzbee7ea0f642009-08-10 17:06:51 -0700123 case 'm':
124 operand = expandImmediate(operand);
125 sprintf(tbuf,"%d [0x%x]", operand, operand);
126 break;
Bill Buzbee9727c3d2009-08-01 11:32:36 -0700127 case 's':
128 sprintf(tbuf,"s%d",operand & FP_REG_MASK);
129 break;
130 case 'S':
131 sprintf(tbuf,"d%d",(operand & FP_REG_MASK) >> 1);
132 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700133 case 'h':
134 sprintf(tbuf,"%04x", operand);
135 break;
Bill Buzbee7ea0f642009-08-10 17:06:51 -0700136 case 'M':
Ben Chengba4fc8b2009-06-01 13:00:29 -0700137 case 'd':
138 sprintf(tbuf,"%d", operand);
139 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700140 case 'E':
141 sprintf(tbuf,"%d", operand*4);
142 break;
143 case 'F':
144 sprintf(tbuf,"%d", operand*2);
145 break;
146 case 'c':
147 switch (operand) {
Bill Buzbee1465db52009-09-23 17:17:35 -0700148 case kArmCondEq:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700149 strcpy(tbuf, "eq");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700150 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700151 case kArmCondNe:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700152 strcpy(tbuf, "ne");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700153 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700154 case kArmCondLt:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700155 strcpy(tbuf, "lt");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700156 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700157 case kArmCondGe:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700158 strcpy(tbuf, "ge");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700159 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700160 case kArmCondGt:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700161 strcpy(tbuf, "gt");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700162 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700163 case kArmCondLe:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700164 strcpy(tbuf, "le");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700165 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700166 case kArmCondCs:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700167 strcpy(tbuf, "cs");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700168 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700169 case kArmCondMi:
Bill Buzbeea4a7f072009-08-27 13:58:09 -0700170 strcpy(tbuf, "mi");
Bill Buzbee7ea0f642009-08-10 17:06:51 -0700171 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700172 default:
173 strcpy(tbuf, "");
174 break;
175 }
176 break;
177 case 't':
178 sprintf(tbuf,"0x%08x",
179 (int) baseAddr + lir->generic.offset + 4 +
180 (operand << 1));
181 break;
182 case 'u': {
183 int offset_1 = lir->operands[0];
184 int offset_2 = NEXT_LIR(lir)->operands[0];
185 intptr_t target =
186 ((((intptr_t) baseAddr + lir->generic.offset + 4) &
187 ~3) + (offset_1 << 21 >> 9) + (offset_2 << 1)) &
188 0xfffffffc;
189 sprintf(tbuf, "%p", (void *) target);
190 break;
191 }
192
193 /* Nothing to print for BLX_2 */
194 case 'v':
195 strcpy(tbuf, "see above");
196 break;
197 case 'R':
198 decodeRegList(operand, tbuf);
199 break;
200 default:
201 strcpy(tbuf,"DecodeError");
202 break;
203 }
204 if (buf+strlen(tbuf) <= bufEnd) {
205 strcpy(buf, tbuf);
206 buf += strlen(tbuf);
207 } else {
208 break;
209 }
210 }
211 } else {
212 *buf++ = *fmt++;
213 }
214 if (buf == bufEnd)
215 break;
216 }
217 *buf = 0;
218}
219
Ben Chengd7d426a2009-09-22 11:23:36 -0700220void dvmDumpResourceMask(LIR *lir, u8 mask, const char *prefix)
221{
222 char buf[256];
223 buf[0] = 0;
224 ArmLIR *armLIR = (ArmLIR *) lir;
225
226 if (mask == ENCODE_ALL) {
227 strcpy(buf, "all");
228 } else {
229 char num[8];
230 int i;
231
232 for (i = 0; i < kRegEnd; i++) {
233 if (mask & (1ULL << i)) {
234 sprintf(num, "%d ", i);
235 strcat(buf, num);
236 }
237 }
238
239 if (mask & ENCODE_CCODE) {
240 strcat(buf, "cc ");
241 }
242 if (mask & ENCODE_FP_STATUS) {
243 strcat(buf, "fpcc ");
244 }
245 if (armLIR && (mask & ENCODE_DALVIK_REG)) {
246 sprintf(buf + strlen(buf), "dr%d%s", armLIR->aliasInfo & 0xffff,
247 (armLIR->aliasInfo & 0x80000000) ? "(+1)" : "");
248 }
249 }
250 if (buf[0]) {
251 LOGD("%s: %s", prefix, buf);
252 }
253}
254
255/*
256 * Debugging macros
257 */
258#define DUMP_RESOURCE_MASK(X)
259#define DUMP_SSA_REP(X)
260
Ben Chengba4fc8b2009-06-01 13:00:29 -0700261/* Pretty-print a LIR instruction */
Ben Chengd7d426a2009-09-22 11:23:36 -0700262void dvmDumpLIRInsn(LIR *arg, unsigned char *baseAddr)
Ben Chengba4fc8b2009-06-01 13:00:29 -0700263{
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700264 ArmLIR *lir = (ArmLIR *) arg;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700265 char buf[256];
266 char opName[256];
267 int offset = lir->generic.offset;
268 int dest = lir->operands[0];
Ben Chengd7d426a2009-09-22 11:23:36 -0700269 const bool dumpNop = false;
270
Ben Chengba4fc8b2009-06-01 13:00:29 -0700271 /* Handle pseudo-ops individually, and all regular insns as a group */
272 switch(lir->opCode) {
Ben Chengcec26f62010-01-15 15:29:33 -0800273 case kArmChainingCellBottom:
274 LOGD("-------- end of chaining cells (0x%04x)\n", offset);
275 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700276 case kArmPseudoBarrier:
Ben Chengd7d426a2009-09-22 11:23:36 -0700277 LOGD("-------- BARRIER");
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700278 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700279 case kArmPseudoExtended:
Ben Cheng7a2697d2010-06-07 13:44:23 -0700280 LOGD("-------- %s\n", (char *) dest);
281 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700282 case kArmPseudoSSARep:
Ben Chengd7d426a2009-09-22 11:23:36 -0700283 DUMP_SSA_REP(LOGD("-------- %s\n", (char *) dest));
Ben Cheng4238ec22009-08-24 16:32:22 -0700284 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700285 case kArmPseudoTargetLabel:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700286 break;
Ben Chenga4973592010-03-31 11:59:18 -0700287 case kArmPseudoChainingCellBackwardBranch:
Ben Cheng4238ec22009-08-24 16:32:22 -0700288 LOGD("-------- chaining cell (backward branch): 0x%04x\n", dest);
289 break;
Ben Chenga4973592010-03-31 11:59:18 -0700290 case kArmPseudoChainingCellNormal:
Ben Cheng1efc9c52009-06-08 18:25:27 -0700291 LOGD("-------- chaining cell (normal): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700292 break;
Ben Chenga4973592010-03-31 11:59:18 -0700293 case kArmPseudoChainingCellHot:
Ben Cheng1efc9c52009-06-08 18:25:27 -0700294 LOGD("-------- chaining cell (hot): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700295 break;
Ben Chenga4973592010-03-31 11:59:18 -0700296 case kArmPseudoChainingCellInvokePredicted:
Ben Cheng38329f52009-07-07 14:19:20 -0700297 LOGD("-------- chaining cell (predicted)\n");
298 break;
Ben Chenga4973592010-03-31 11:59:18 -0700299 case kArmPseudoChainingCellInvokeSingleton:
Ben Cheng38329f52009-07-07 14:19:20 -0700300 LOGD("-------- chaining cell (invoke singleton): %s/%p\n",
Ben Chengba4fc8b2009-06-01 13:00:29 -0700301 ((Method *)dest)->name,
302 ((Method *)dest)->insns);
303 break;
Ben Chenga4973592010-03-31 11:59:18 -0700304 case kArmPseudoEntryBlock:
Ben Cheng4238ec22009-08-24 16:32:22 -0700305 LOGD("-------- entry offset: 0x%04x\n", dest);
Jeff Hao97319a82009-08-12 16:57:15 -0700306 break;
Ben Chenga4973592010-03-31 11:59:18 -0700307 case kArmPseudoDalvikByteCodeBoundary:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700308 LOGD("-------- dalvik offset: 0x%04x @ %s\n", dest,
Ben Chengccd6c012009-10-15 14:52:45 -0700309 (char *) lir->operands[1]);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700310 break;
Ben Chenga4973592010-03-31 11:59:18 -0700311 case kArmPseudoExitBlock:
Ben Cheng4238ec22009-08-24 16:32:22 -0700312 LOGD("-------- exit offset: 0x%04x\n", dest);
313 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700314 case kArmPseudoPseudoAlign4:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700315 LOGD("%p (%04x): .align4\n", baseAddr + offset, offset);
316 break;
Ben Chenga4973592010-03-31 11:59:18 -0700317 case kArmPseudoPCReconstructionCell:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700318 LOGD("-------- reconstruct dalvik PC : 0x%04x @ +0x%04x\n", dest,
319 lir->operands[1]);
320 break;
Ben Chenga4973592010-03-31 11:59:18 -0700321 case kArmPseudoPCReconstructionBlockLabel:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700322 /* Do nothing */
323 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700324 case kArmPseudoEHBlockLabel:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700325 LOGD("Exception_Handling:\n");
326 break;
Bill Buzbee1465db52009-09-23 17:17:35 -0700327 case kArmPseudoNormalBlockLabel:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700328 LOGD("L%#06x:\n", dest);
329 break;
330 default:
Ben Chengd7d426a2009-09-22 11:23:36 -0700331 if (lir->isNop && !dumpNop) {
Ben Chenge9695e52009-06-16 16:11:47 -0700332 break;
333 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700334 buildInsnString(EncodingMap[lir->opCode].name, lir, opName,
335 baseAddr, 256);
336 buildInsnString(EncodingMap[lir->opCode].fmt, lir, buf, baseAddr,
337 256);
Ben Chengd7d426a2009-09-22 11:23:36 -0700338 LOGD("%p (%04x): %-8s%s%s\n",
339 baseAddr + offset, offset, opName, buf,
340 lir->isNop ? "(nop)" : "");
Ben Chengba4fc8b2009-06-01 13:00:29 -0700341 break;
342 }
Ben Chengd7d426a2009-09-22 11:23:36 -0700343
344 if (lir->useMask && (!lir->isNop || dumpNop)) {
345 DUMP_RESOURCE_MASK(dvmDumpResourceMask((LIR *) lir,
346 lir->useMask, "use"));
347 }
348 if (lir->defMask && (!lir->isNop || dumpNop)) {
349 DUMP_RESOURCE_MASK(dvmDumpResourceMask((LIR *) lir,
350 lir->defMask, "def"));
351 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700352}
353
354/* Dump instructions and constant pool contents */
355void dvmCompilerCodegenDump(CompilationUnit *cUnit)
356{
357 LOGD("Dumping LIR insns\n");
358 LIR *lirInsn;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700359 ArmLIR *armLIR;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700360
361 LOGD("installed code is at %p\n", cUnit->baseAddr);
362 LOGD("total size is %d bytes\n", cUnit->totalSize);
363 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
Ben Chengd7d426a2009-09-22 11:23:36 -0700364 dvmDumpLIRInsn(lirInsn, cUnit->baseAddr);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700365 }
366 for (lirInsn = cUnit->wordList; lirInsn; lirInsn = lirInsn->next) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700367 armLIR = (ArmLIR *) lirInsn;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700368 LOGD("%p (%04x): .word (0x%x)\n",
Ben Chengdcf3e5d2009-09-11 13:42:05 -0700369 (char*)cUnit->baseAddr + armLIR->generic.offset,
370 armLIR->generic.offset,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700371 armLIR->operands[0]);
372 }
373}