blob: abcb2eb2d8605f4c9a5bdcef39581be5962921f6 [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
40/*
41 * Interpret a format string and build a string no longer than size
42 * See format key in Assemble.c.
43 */
Bill Buzbee89efc3d2009-07-28 11:22:22 -070044static void buildInsnString(char *fmt, ArmLIR *lir, char* buf,
Ben Chengba4fc8b2009-06-01 13:00:29 -070045 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++) {
Bill Buzbee9727c3d2009-08-01 11:32:36 -070065 case 's':
66 sprintf(tbuf,"s%d",operand & FP_REG_MASK);
67 break;
68 case 'S':
69 sprintf(tbuf,"d%d",(operand & FP_REG_MASK) >> 1);
70 break;
Ben Chengba4fc8b2009-06-01 13:00:29 -070071 case 'h':
72 sprintf(tbuf,"%04x", operand);
73 break;
74 case 'd':
75 sprintf(tbuf,"%d", operand);
76 break;
77 case 'D':
78 sprintf(tbuf,"%d", operand+8);
79 break;
80 case 'E':
81 sprintf(tbuf,"%d", operand*4);
82 break;
83 case 'F':
84 sprintf(tbuf,"%d", operand*2);
85 break;
86 case 'c':
87 switch (operand) {
88 case ARM_COND_EQ:
89 strcpy(tbuf, "beq");
90 break;
91 case ARM_COND_NE:
92 strcpy(tbuf, "bne");
93 break;
94 case ARM_COND_LT:
95 strcpy(tbuf, "blt");
96 break;
97 case ARM_COND_GE:
98 strcpy(tbuf, "bge");
99 break;
100 case ARM_COND_GT:
101 strcpy(tbuf, "bgt");
102 break;
103 case ARM_COND_LE:
104 strcpy(tbuf, "ble");
105 break;
106 case ARM_COND_CS:
107 strcpy(tbuf, "bcs");
108 break;
109 default:
110 strcpy(tbuf, "");
111 break;
112 }
113 break;
114 case 't':
115 sprintf(tbuf,"0x%08x",
116 (int) baseAddr + lir->generic.offset + 4 +
117 (operand << 1));
118 break;
119 case 'u': {
120 int offset_1 = lir->operands[0];
121 int offset_2 = NEXT_LIR(lir)->operands[0];
122 intptr_t target =
123 ((((intptr_t) baseAddr + lir->generic.offset + 4) &
124 ~3) + (offset_1 << 21 >> 9) + (offset_2 << 1)) &
125 0xfffffffc;
126 sprintf(tbuf, "%p", (void *) target);
127 break;
128 }
129
130 /* Nothing to print for BLX_2 */
131 case 'v':
132 strcpy(tbuf, "see above");
133 break;
134 case 'R':
135 decodeRegList(operand, tbuf);
136 break;
137 default:
138 strcpy(tbuf,"DecodeError");
139 break;
140 }
141 if (buf+strlen(tbuf) <= bufEnd) {
142 strcpy(buf, tbuf);
143 buf += strlen(tbuf);
144 } else {
145 break;
146 }
147 }
148 } else {
149 *buf++ = *fmt++;
150 }
151 if (buf == bufEnd)
152 break;
153 }
154 *buf = 0;
155}
156
157/* Pretty-print a LIR instruction */
158static void dumpLIRInsn(LIR *arg, unsigned char *baseAddr)
159{
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700160 ArmLIR *lir = (ArmLIR *) arg;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700161 char buf[256];
162 char opName[256];
163 int offset = lir->generic.offset;
164 int dest = lir->operands[0];
165 u2 *cPtr = (u2*)baseAddr;
166 /* Handle pseudo-ops individually, and all regular insns as a group */
167 switch(lir->opCode) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700168 case ARM_PSEUDO_TARGET_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700169 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700170 case ARM_PSEUDO_CHAINING_CELL_NORMAL:
Ben Cheng1efc9c52009-06-08 18:25:27 -0700171 LOGD("-------- chaining cell (normal): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700172 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700173 case ARM_PSEUDO_CHAINING_CELL_HOT:
Ben Cheng1efc9c52009-06-08 18:25:27 -0700174 LOGD("-------- chaining cell (hot): 0x%04x\n", dest);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700175 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700176 case ARM_PSEUDO_CHAINING_CELL_INVOKE_PREDICTED:
Ben Cheng38329f52009-07-07 14:19:20 -0700177 LOGD("-------- chaining cell (predicted)\n");
178 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700179 case ARM_PSEUDO_CHAINING_CELL_INVOKE_SINGLETON:
Ben Cheng38329f52009-07-07 14:19:20 -0700180 LOGD("-------- chaining cell (invoke singleton): %s/%p\n",
Ben Chengba4fc8b2009-06-01 13:00:29 -0700181 ((Method *)dest)->name,
182 ((Method *)dest)->insns);
183 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700184 case ARM_PSEUDO_DALVIK_BYTECODE_BOUNDARY:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700185 LOGD("-------- dalvik offset: 0x%04x @ %s\n", dest,
186 getOpcodeName(lir->operands[1]));
187 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700188 case ARM_PSEUDO_ALIGN4:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700189 LOGD("%p (%04x): .align4\n", baseAddr + offset, offset);
190 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700191 case ARM_PSEUDO_PC_RECONSTRUCTION_CELL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700192 LOGD("-------- reconstruct dalvik PC : 0x%04x @ +0x%04x\n", dest,
193 lir->operands[1]);
194 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700195 case ARM_PSEUDO_PC_RECONSTRUCTION_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700196 /* Do nothing */
197 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700198 case ARM_PSEUDO_EH_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700199 LOGD("Exception_Handling:\n");
200 break;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700201 case ARM_PSEUDO_NORMAL_BLOCK_LABEL:
Ben Chengba4fc8b2009-06-01 13:00:29 -0700202 LOGD("L%#06x:\n", dest);
203 break;
204 default:
Ben Chenge9695e52009-06-16 16:11:47 -0700205 if (lir->isNop) {
206 break;
207 }
Ben Chengba4fc8b2009-06-01 13:00:29 -0700208 buildInsnString(EncodingMap[lir->opCode].name, lir, opName,
209 baseAddr, 256);
210 buildInsnString(EncodingMap[lir->opCode].fmt, lir, buf, baseAddr,
211 256);
Ben Chenge9695e52009-06-16 16:11:47 -0700212 LOGD("%p (%04x): %-8s%s\n",
213 baseAddr + offset, offset, opName, buf);
Ben Chengba4fc8b2009-06-01 13:00:29 -0700214 break;
215 }
216}
217
218/* Dump instructions and constant pool contents */
219void dvmCompilerCodegenDump(CompilationUnit *cUnit)
220{
221 LOGD("Dumping LIR insns\n");
222 LIR *lirInsn;
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700223 ArmLIR *armLIR;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700224
225 LOGD("installed code is at %p\n", cUnit->baseAddr);
226 LOGD("total size is %d bytes\n", cUnit->totalSize);
227 for (lirInsn = cUnit->firstLIRInsn; lirInsn; lirInsn = lirInsn->next) {
228 dumpLIRInsn(lirInsn, cUnit->baseAddr);
229 }
230 for (lirInsn = cUnit->wordList; lirInsn; lirInsn = lirInsn->next) {
Bill Buzbee89efc3d2009-07-28 11:22:22 -0700231 armLIR = (ArmLIR *) lirInsn;
Ben Chengba4fc8b2009-06-01 13:00:29 -0700232 LOGD("%p (%04x): .word (0x%x)\n",
Bill Buzbee716f1202009-07-23 13:22:09 -0700233 (char*)cUnit->baseAddr + armLIR->generic.offset, armLIR->generic.offset,
Ben Chengba4fc8b2009-06-01 13:00:29 -0700234 armLIR->operands[0]);
235 }
236}