blob: 953ce4a8990bd24e7abaf0fc68d8a01dc4e14546 [file] [log] [blame]
buzbeee88dfbf2012-03-05 11:19:57 -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 "X86LIR.h"
19#include "../Ralloc.h"
20
21#include <string>
22
23namespace art {
24
buzbeeec137432012-11-13 12:13:16 -080025/*
26 * Decode the register id.
27 */
28u8 getRegMaskCommon(CompilationUnit* cUnit, int reg)
29{
30 u8 seed;
31 int shift;
32 int regId;
33
34 regId = reg & 0xf;
35 /* Double registers in x86 are just a single FP register */
36 seed = 1;
37 /* FP register starts at bit position 16 */
38 shift = FPREG(reg) ? kX86FPReg0 : 0;
39 /* Expand the double register id into single offset */
40 shift += regId;
41 return (seed << shift);
42}
43
44uint64_t getPCUseDefEncoding()
45{
46 /*
47 * FIXME: might make sense to use a virtual resource encoding bit for pc. Might be
48 * able to clean up some of the x86/Arm_Mips differences
49 */
50 LOG(FATAL) << "Unexpected call to getPCUseDefEncoding for x86";
51 return 0ULL;
52}
53
buzbeeb046e162012-10-30 15:48:42 -070054void setupTargetResourceMasks(CompilationUnit* cUnit, LIR* lir)
55{
56 DCHECK_EQ(cUnit->instructionSet, kX86);
57
58 // X86-specific resource map setup here.
buzbeeec137432012-11-13 12:13:16 -080059 uint64_t flags = EncodingMap[lir->opcode].flags;
60
61 if (flags & REG_USE_SP) {
62 lir->useMask |= ENCODE_X86_REG_SP;
63 }
64
65 if (flags & REG_DEF_SP) {
66 lir->defMask |= ENCODE_X86_REG_SP;
67 }
68
buzbeeb046e162012-10-30 15:48:42 -070069 if (flags & REG_DEFA) {
70 oatSetupRegMask(cUnit, &lir->defMask, rAX);
71 }
72
73 if (flags & REG_DEFD) {
74 oatSetupRegMask(cUnit, &lir->defMask, rDX);
75 }
76 if (flags & REG_USEA) {
77 oatSetupRegMask(cUnit, &lir->useMask, rAX);
78 }
79
80 if (flags & REG_USEC) {
81 oatSetupRegMask(cUnit, &lir->useMask, rCX);
82 }
83
84 if (flags & REG_USED) {
85 oatSetupRegMask(cUnit, &lir->useMask, rDX);
86 }
87}
88
buzbeee88dfbf2012-03-05 11:19:57 -080089/* For dumping instructions */
Ian Rogersb5d09b22012-03-06 22:14:17 -080090static const char* x86RegName[] = {
Bill Buzbeea114add2012-05-03 15:00:40 -070091 "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
92 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
buzbeee88dfbf2012-03-05 11:19:57 -080093};
94
Ian Rogersb5d09b22012-03-06 22:14:17 -080095static const char* x86CondName[] = {
Bill Buzbeea114add2012-05-03 15:00:40 -070096 "O",
97 "NO",
98 "B/NAE/C",
99 "NB/AE/NC",
100 "Z/EQ",
101 "NZ/NE",
102 "BE/NA",
103 "NBE/A",
104 "S",
105 "NS",
106 "P/PE",
107 "NP/PO",
108 "L/NGE",
109 "NL/GE",
110 "LE/NG",
111 "NLE/G"
Ian Rogersb5d09b22012-03-06 22:14:17 -0800112};
113
buzbeee88dfbf2012-03-05 11:19:57 -0800114/*
115 * Interpret a format string and build a string no longer than size
Ian Rogers7caad772012-03-30 01:07:54 -0700116 * See format key in Assemble.cc.
buzbeee88dfbf2012-03-05 11:19:57 -0800117 */
Ian Rogersb5d09b22012-03-06 22:14:17 -0800118std::string buildInsnString(const char *fmt, LIR *lir, unsigned char* baseAddr) {
119 std::string buf;
120 size_t i = 0;
121 size_t fmt_len = strlen(fmt);
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700122 while (i < fmt_len) {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800123 if (fmt[i] != '!') {
124 buf += fmt[i];
125 i++;
126 } else {
127 i++;
128 DCHECK_LT(i, fmt_len);
129 char operand_number_ch = fmt[i];
130 i++;
131 if (operand_number_ch == '!') {
132 buf += "!";
133 } else {
134 int operand_number = operand_number_ch - '0';
135 DCHECK_LT(operand_number, 6); // Expect upto 6 LIR operands.
136 DCHECK_LT(i, fmt_len);
137 int operand = lir->operands[operand_number];
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700138 switch (fmt[i]) {
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700139 case 'c':
140 DCHECK_LT(static_cast<size_t>(operand), sizeof(x86CondName));
141 buf += x86CondName[operand];
142 break;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800143 case 'd':
144 buf += StringPrintf("%d", operand);
145 break;
Ian Rogers7caad772012-03-30 01:07:54 -0700146 case 'p': {
147 SwitchTable *tabRec = reinterpret_cast<SwitchTable*>(operand);
148 buf += StringPrintf("0x%08x", tabRec->offset);
149 break;
150 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800151 case 'r':
152 if (FPREG(operand) || DOUBLEREG(operand)) {
153 int fp_reg = operand & FP_REG_MASK;
154 buf += StringPrintf("xmm%d", fp_reg);
buzbeee88dfbf2012-03-05 11:19:57 -0800155 } else {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800156 DCHECK_LT(static_cast<size_t>(operand), sizeof(x86RegName));
157 buf += x86RegName[operand];
buzbeee88dfbf2012-03-05 11:19:57 -0800158 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800159 break;
Ian Rogersb3ab25b2012-03-19 01:12:01 -0700160 case 't':
161 buf += StringPrintf("0x%08x (L%p)",
Bill Buzbeea114add2012-05-03 15:00:40 -0700162 reinterpret_cast<uint32_t>(baseAddr)
163 + lir->offset + operand, lir->target);
Ian Rogersb5d09b22012-03-06 22:14:17 -0800164 break;
165 default:
166 buf += StringPrintf("DecodeError '%c'", fmt[i]);
167 break;
buzbeee88dfbf2012-03-05 11:19:57 -0800168 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800169 i++;
170 }
buzbeee88dfbf2012-03-05 11:19:57 -0800171 }
Ian Rogersb5d09b22012-03-06 22:14:17 -0800172 }
173 return buf;
buzbeee88dfbf2012-03-05 11:19:57 -0800174}
175
176void oatDumpResourceMask(LIR *lir, u8 mask, const char *prefix)
177{
Bill Buzbeea114add2012-05-03 15:00:40 -0700178 char buf[256];
179 buf[0] = 0;
180 LIR *x86LIR = (LIR *) lir;
buzbeee88dfbf2012-03-05 11:19:57 -0800181
Bill Buzbeea114add2012-05-03 15:00:40 -0700182 if (mask == ENCODE_ALL) {
183 strcpy(buf, "all");
184 } else {
185 char num[8];
186 int i;
buzbeee88dfbf2012-03-05 11:19:57 -0800187
buzbeeec137432012-11-13 12:13:16 -0800188 for (i = 0; i < kX86RegEnd; i++) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700189 if (mask & (1ULL << i)) {
190 sprintf(num, "%d ", i);
191 strcat(buf, num);
192 }
buzbeee88dfbf2012-03-05 11:19:57 -0800193 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700194
195 if (mask & ENCODE_CCODE) {
196 strcat(buf, "cc ");
buzbeee88dfbf2012-03-05 11:19:57 -0800197 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700198 /* Memory bits */
199 if (x86LIR && (mask & ENCODE_DALVIK_REG)) {
200 sprintf(buf + strlen(buf), "dr%d%s", x86LIR->aliasInfo & 0xffff,
201 (x86LIR->aliasInfo & 0x80000000) ? "(+1)" : "");
202 }
203 if (mask & ENCODE_LITERAL) {
204 strcat(buf, "lit ");
205 }
206
207 if (mask & ENCODE_HEAP_REF) {
208 strcat(buf, "heap ");
209 }
210 if (mask & ENCODE_MUST_NOT_ALIAS) {
211 strcat(buf, "noalias ");
212 }
213 }
214 if (buf[0]) {
215 LOG(INFO) << prefix << ": " << buf;
216 }
buzbeee88dfbf2012-03-05 11:19:57 -0800217}
218
219} // namespace art