buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 23 | namespace art { |
| 24 | |
buzbee | ec13743 | 2012-11-13 12:13:16 -0800 | [diff] [blame^] | 25 | /* |
| 26 | * Decode the register id. |
| 27 | */ |
| 28 | u8 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 | |
| 44 | uint64_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 | |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 54 | void setupTargetResourceMasks(CompilationUnit* cUnit, LIR* lir) |
| 55 | { |
| 56 | DCHECK_EQ(cUnit->instructionSet, kX86); |
| 57 | |
| 58 | // X86-specific resource map setup here. |
buzbee | ec13743 | 2012-11-13 12:13:16 -0800 | [diff] [blame^] | 59 | 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 | |
buzbee | b046e16 | 2012-10-30 15:48:42 -0700 | [diff] [blame] | 69 | 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 | |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 89 | /* For dumping instructions */ |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 90 | static const char* x86RegName[] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 91 | "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi", |
| 92 | "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 95 | static const char* x86CondName[] = { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 96 | "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 Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 114 | /* |
| 115 | * Interpret a format string and build a string no longer than size |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 116 | * See format key in Assemble.cc. |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 117 | */ |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 118 | std::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 Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 122 | while (i < fmt_len) { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 123 | 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 Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 138 | switch (fmt[i]) { |
Ian Rogers | b3ab25b | 2012-03-19 01:12:01 -0700 | [diff] [blame] | 139 | case 'c': |
| 140 | DCHECK_LT(static_cast<size_t>(operand), sizeof(x86CondName)); |
| 141 | buf += x86CondName[operand]; |
| 142 | break; |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 143 | case 'd': |
| 144 | buf += StringPrintf("%d", operand); |
| 145 | break; |
Ian Rogers | 7caad77 | 2012-03-30 01:07:54 -0700 | [diff] [blame] | 146 | case 'p': { |
| 147 | SwitchTable *tabRec = reinterpret_cast<SwitchTable*>(operand); |
| 148 | buf += StringPrintf("0x%08x", tabRec->offset); |
| 149 | break; |
| 150 | } |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 151 | case 'r': |
| 152 | if (FPREG(operand) || DOUBLEREG(operand)) { |
| 153 | int fp_reg = operand & FP_REG_MASK; |
| 154 | buf += StringPrintf("xmm%d", fp_reg); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 155 | } else { |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 156 | DCHECK_LT(static_cast<size_t>(operand), sizeof(x86RegName)); |
| 157 | buf += x86RegName[operand]; |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 158 | } |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 159 | break; |
Ian Rogers | b3ab25b | 2012-03-19 01:12:01 -0700 | [diff] [blame] | 160 | case 't': |
| 161 | buf += StringPrintf("0x%08x (L%p)", |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 162 | reinterpret_cast<uint32_t>(baseAddr) |
| 163 | + lir->offset + operand, lir->target); |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 164 | break; |
| 165 | default: |
| 166 | buf += StringPrintf("DecodeError '%c'", fmt[i]); |
| 167 | break; |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 168 | } |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 169 | i++; |
| 170 | } |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 171 | } |
Ian Rogers | b5d09b2 | 2012-03-06 22:14:17 -0800 | [diff] [blame] | 172 | } |
| 173 | return buf; |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void oatDumpResourceMask(LIR *lir, u8 mask, const char *prefix) |
| 177 | { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 178 | char buf[256]; |
| 179 | buf[0] = 0; |
| 180 | LIR *x86LIR = (LIR *) lir; |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 181 | |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 182 | if (mask == ENCODE_ALL) { |
| 183 | strcpy(buf, "all"); |
| 184 | } else { |
| 185 | char num[8]; |
| 186 | int i; |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 187 | |
buzbee | ec13743 | 2012-11-13 12:13:16 -0800 | [diff] [blame^] | 188 | for (i = 0; i < kX86RegEnd; i++) { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 189 | if (mask & (1ULL << i)) { |
| 190 | sprintf(num, "%d ", i); |
| 191 | strcat(buf, num); |
| 192 | } |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 193 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 194 | |
| 195 | if (mask & ENCODE_CCODE) { |
| 196 | strcat(buf, "cc "); |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 197 | } |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 198 | /* 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 | } |
buzbee | e88dfbf | 2012-03-05 11:19:57 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | } // namespace art |