blob: 7f7aeb3b9060302bc28446931ccf8abe923bc144 [file] [log] [blame]
buzbeee3acd072012-02-25 17:03:10 -08001/*
2 * Copyright (C) 2012 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 "MipsLIR.h"
19#include "../Ralloc.h"
20
21#include <string>
22
23namespace art {
24
buzbee5de34942012-03-01 14:51:57 -080025MipsConditionCode oatMipsConditionEncoding(ConditionCode code)
26{
27 MipsConditionCode res;
28 switch(code) {
29 case kCondEq: res = kMipsCondEq; break;
30 case kCondNe: res = kMipsCondNe; break;
31 case kCondCs: res = kMipsCondCs; break;
32 case kCondCc: res = kMipsCondCc; break;
33 case kCondMi: res = kMipsCondMi; break;
34 case kCondPl: res = kMipsCondPl; break;
35 case kCondVs: res = kMipsCondVs; break;
36 case kCondVc: res = kMipsCondVc; break;
37 case kCondHi: res = kMipsCondHi; break;
38 case kCondLs: res = kMipsCondLs; break;
39 case kCondGe: res = kMipsCondGe; break;
40 case kCondLt: res = kMipsCondLt; break;
41 case kCondGt: res = kMipsCondGt; break;
42 case kCondLe: res = kMipsCondLe; break;
43 case kCondAl: res = kMipsCondAl; break;
44 case kCondNv: res = kMipsCondNv; break;
45 default:
46 LOG(FATAL) << "Bad condition code" << (int)code;
47 res = (MipsConditionCode)0; // Quiet gcc
48 }
49 return res;
50}
51
buzbeee3acd072012-02-25 17:03:10 -080052/* For dumping instructions */
53#define MIPS_REG_COUNT 32
54static const char *mipsRegName[MIPS_REG_COUNT] = {
55 "zero", "at", "v0", "v1", "a0", "a1", "a2", "a3",
56 "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7",
57 "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7",
58 "t8", "t9", "k0", "k1", "gp", "sp", "fp", "ra"
59};
60
61/*
62 * Interpret a format string and build a string no longer than size
63 * See format key in Assemble.c.
64 */
buzbee5de34942012-03-01 14:51:57 -080065std::string buildInsnString(const char *fmt, LIR *lir, unsigned char* baseAddr)
buzbeee3acd072012-02-25 17:03:10 -080066{
67 std::string buf;
68 int i;
69 const 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 DCHECK_LT(fmt, fmtEnd);
77 nc = *fmt++;
78 if (nc=='!') {
79 strcpy(tbuf, "!");
80 } else {
81 DCHECK_LT(fmt, fmtEnd);
82 DCHECK_LT((unsigned)(nc-'0'), 4u);
83 operand = lir->operands[nc-'0'];
84 switch(*fmt++) {
85 case 'b':
86 strcpy(tbuf,"0000");
87 for (i=3; i>= 0; i--) {
88 tbuf[i] += operand & 1;
89 operand >>= 1;
90 }
91 break;
92 case 's':
93 sprintf(tbuf,"$f%d",operand & FP_REG_MASK);
94 break;
95 case 'S':
96 DCHECK_EQ(((operand & FP_REG_MASK) & 1), 0);
97 sprintf(tbuf,"$f%d",operand & FP_REG_MASK);
98 break;
99 case 'h':
100 sprintf(tbuf,"%04x", operand);
101 break;
102 case 'M':
103 case 'd':
104 sprintf(tbuf,"%d", operand);
105 break;
106 case 'D':
107 sprintf(tbuf,"%d", operand+1);
108 break;
109 case 'E':
110 sprintf(tbuf,"%d", operand*4);
111 break;
112 case 'F':
113 sprintf(tbuf,"%d", operand*2);
114 break;
115 case 'c':
116 switch (operand) {
117 case kMipsCondEq:
118 strcpy(tbuf, "eq");
119 break;
120 case kMipsCondNe:
121 strcpy(tbuf, "ne");
122 break;
123 case kMipsCondLt:
124 strcpy(tbuf, "lt");
125 break;
126 case kMipsCondGe:
127 strcpy(tbuf, "ge");
128 break;
129 case kMipsCondGt:
130 strcpy(tbuf, "gt");
131 break;
132 case kMipsCondLe:
133 strcpy(tbuf, "le");
134 break;
135 case kMipsCondCs:
136 strcpy(tbuf, "cs");
137 break;
138 case kMipsCondMi:
139 strcpy(tbuf, "mi");
140 break;
141 default:
142 strcpy(tbuf, "");
143 break;
144 }
145 break;
146 case 't':
147 sprintf(tbuf,"0x%08x (L%p)",
buzbee5de34942012-03-01 14:51:57 -0800148 (int) baseAddr + lir->offset + 4 +
buzbeee3acd072012-02-25 17:03:10 -0800149 (operand << 2),
buzbee5de34942012-03-01 14:51:57 -0800150 lir->target);
buzbeee3acd072012-02-25 17:03:10 -0800151 break;
152 case 'T':
153 sprintf(tbuf,"0x%08x",
154 (int) (operand << 2));
155 break;
156 case 'u': {
157 int offset_1 = lir->operands[0];
158 int offset_2 = NEXT_LIR(lir)->operands[0];
159 intptr_t target =
buzbee5de34942012-03-01 14:51:57 -0800160 ((((intptr_t) baseAddr + lir->offset + 4) &
buzbeee3acd072012-02-25 17:03:10 -0800161 ~3) + (offset_1 << 21 >> 9) + (offset_2 << 1)) &
162 0xfffffffc;
163 sprintf(tbuf, "%p", (void *) target);
164 break;
165 }
166
167 /* Nothing to print for BLX_2 */
168 case 'v':
169 strcpy(tbuf, "see above");
170 break;
171 case 'r':
172 DCHECK(operand >= 0 && operand < MIPS_REG_COUNT);
173 strcpy(tbuf, mipsRegName[operand]);
174 break;
175 default:
176 strcpy(tbuf,"DecodeError");
177 break;
178 }
179 buf += tbuf;
180 }
181 } else {
182 buf += *fmt++;
183 }
184 }
185 return buf;
186}
187
188// FIXME: need to redo resourse maps for MIPS - fix this at that time
189void oatDumpResourceMask(LIR *lir, u8 mask, const char *prefix)
190{
191 char buf[256];
192 buf[0] = 0;
buzbee5de34942012-03-01 14:51:57 -0800193 LIR *mipsLIR = (LIR *) lir;
buzbeee3acd072012-02-25 17:03:10 -0800194
195 if (mask == ENCODE_ALL) {
196 strcpy(buf, "all");
197 } else {
198 char num[8];
199 int i;
200
201 for (i = 0; i < kRegEnd; i++) {
202 if (mask & (1ULL << i)) {
203 sprintf(num, "%d ", i);
204 strcat(buf, num);
205 }
206 }
207
208 if (mask & ENCODE_CCODE) {
209 strcat(buf, "cc ");
210 }
211 if (mask & ENCODE_FP_STATUS) {
212 strcat(buf, "fpcc ");
213 }
214 /* Memory bits */
215 if (mipsLIR && (mask & ENCODE_DALVIK_REG)) {
216 sprintf(buf + strlen(buf), "dr%d%s", mipsLIR->aliasInfo & 0xffff,
217 (mipsLIR->aliasInfo & 0x80000000) ? "(+1)" : "");
218 }
219 if (mask & ENCODE_LITERAL) {
220 strcat(buf, "lit ");
221 }
222
223 if (mask & ENCODE_HEAP_REF) {
224 strcat(buf, "heap ");
225 }
226 if (mask & ENCODE_MUST_NOT_ALIAS) {
227 strcat(buf, "noalias ");
228 }
229 }
230 if (buf[0]) {
231 LOG(INFO) << prefix << ": " << buf;
232 }
233}
234
buzbeee3acd072012-02-25 17:03:10 -0800235} // namespace art