Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -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 "disassembler_arm.h" |
| 18 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 19 | #include <iostream> |
| 20 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 21 | #include "base/logging.h" |
Elliott Hughes | e222ee0 | 2012-12-13 14:41:43 -0800 | [diff] [blame] | 22 | #include "base/stringprintf.h" |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 23 | #include "thread.h" |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 24 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 25 | namespace art { |
| 26 | namespace arm { |
| 27 | |
| 28 | DisassemblerArm::DisassemblerArm() { |
| 29 | } |
| 30 | |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 31 | size_t DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin) { |
| 32 | if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) { |
| 33 | DumpArm(os, begin); |
| 34 | return 4; |
| 35 | } else { |
| 36 | // remove thumb specifier bits |
| 37 | begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1); |
| 38 | return DumpThumb16(os, begin); |
| 39 | } |
| 40 | } |
| 41 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 42 | void DisassemblerArm::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) { |
| 43 | if ((reinterpret_cast<intptr_t>(begin) & 1) == 0) { |
| 44 | for (const uint8_t* cur = begin; cur < end; cur += 4) { |
| 45 | DumpArm(os, cur); |
| 46 | } |
| 47 | } else { |
| 48 | // remove thumb specifier bits |
| 49 | begin = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(begin) & ~1); |
| 50 | end = reinterpret_cast<const uint8_t*>(reinterpret_cast<uintptr_t>(end) & ~1); |
| 51 | for (const uint8_t* cur = begin; cur < end;) { |
| 52 | cur += DumpThumb16(os, cur); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 57 | static const char* kConditionCodeNames[] = { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 58 | "eq", // 0000 - equal |
| 59 | "ne", // 0001 - not-equal |
| 60 | "cs", // 0010 - carry-set, greater than, equal or unordered |
| 61 | "cc", // 0011 - carry-clear, less than |
| 62 | "mi", // 0100 - minus, negative |
| 63 | "pl", // 0101 - plus, positive or zero |
| 64 | "vs", // 0110 - overflow |
| 65 | "vc", // 0111 - no overflow |
| 66 | "hi", // 1000 - unsigned higher |
| 67 | "ls", // 1001 - unsigned lower or same |
| 68 | "ge", // 1010 - signed greater than or equal |
| 69 | "lt", // 1011 - signed less than |
| 70 | "gt", // 1100 - signed greater than |
| 71 | "le", // 1101 - signed less than or equal |
| 72 | "", // 1110 - always |
| 73 | "nv", // 1111 - never (mostly obsolete, but might be a clue that we're mistranslating) |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | void DisassemblerArm::DumpCond(std::ostream& os, uint32_t cond) { |
| 77 | if (cond < 15) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 78 | os << kConditionCodeNames[cond]; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 79 | } else { |
| 80 | os << "Unexpected condition: " << cond; |
| 81 | } |
| 82 | } |
| 83 | |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 84 | void DisassemblerArm::DumpBranchTarget(std::ostream& os, const uint8_t* instr_ptr, int32_t imm32) { |
Elliott Hughes | 1ca9849 | 2012-04-12 17:21:02 -0700 | [diff] [blame] | 85 | os << StringPrintf("%+d (%p)", imm32, instr_ptr + imm32); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static uint32_t ReadU16(const uint8_t* ptr) { |
| 89 | return ptr[0] | (ptr[1] << 8); |
| 90 | } |
| 91 | |
| 92 | static uint32_t ReadU32(const uint8_t* ptr) { |
| 93 | return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24); |
| 94 | } |
| 95 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 96 | static const char* kDataProcessingOperations[] = { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 97 | "and", "eor", "sub", "rsb", "add", "adc", "sbc", "rsc", |
| 98 | "tst", "teq", "cmp", "cmn", "orr", "mov", "bic", "mvn", |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Ian Rogers | ad03ef5 | 2012-03-18 19:34:47 -0700 | [diff] [blame] | 101 | static const char* kThumbDataProcessingOperations[] = { |
| 102 | "and", "eor", "lsl", "lsr", "asr", "adc", "sbc", "ror", |
| 103 | "tst", "rsb", "cmp", "cmn", "orr", "mul", "bic", "mvn", |
| 104 | }; |
| 105 | |
Vladimir Marko | a8b4caf | 2013-10-24 15:08:57 +0100 | [diff] [blame] | 106 | static const char* kThumbReverseOperations[] = { |
| 107 | "rev", "rev16", "rbit", "revsh" |
| 108 | }; |
| 109 | |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 110 | struct ArmRegister { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 111 | explicit ArmRegister(uint32_t r) : r(r) { CHECK_LE(r, 15U); } |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 112 | ArmRegister(uint32_t instruction, uint32_t at_bit) : r((instruction >> at_bit) & 0xf) { CHECK_LE(r, 15U); } |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 113 | uint32_t r; |
| 114 | }; |
| 115 | std::ostream& operator<<(std::ostream& os, const ArmRegister& r) { |
| 116 | if (r.r == 13) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 117 | os << "sp"; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 118 | } else if (r.r == 14) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 119 | os << "lr"; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 120 | } else if (r.r == 15) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 121 | os << "pc"; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 122 | } else { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 123 | os << "r" << r.r; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 124 | } |
| 125 | return os; |
| 126 | } |
| 127 | |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 128 | struct ThumbRegister : ArmRegister { |
| 129 | ThumbRegister(uint16_t instruction, uint16_t at_bit) : ArmRegister((instruction >> at_bit) & 0x7) {} |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | struct Rm { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 133 | explicit Rm(uint32_t instruction) : shift((instruction >> 4) & 0xff), rm(instruction & 0xf) {} |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 134 | uint32_t shift; |
| 135 | ArmRegister rm; |
| 136 | }; |
| 137 | std::ostream& operator<<(std::ostream& os, const Rm& r) { |
| 138 | os << r.rm; |
| 139 | if (r.shift != 0) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 140 | os << "-shift-" << r.shift; // TODO |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 141 | } |
| 142 | return os; |
| 143 | } |
| 144 | |
Elliott Hughes | 1ca9849 | 2012-04-12 17:21:02 -0700 | [diff] [blame] | 145 | struct ShiftedImmediate { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 146 | explicit ShiftedImmediate(uint32_t instruction) { |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 147 | uint32_t rotate = ((instruction >> 8) & 0xf); |
| 148 | uint32_t imm = (instruction & 0xff); |
| 149 | value = (imm >> (2 * rotate)) | (imm << (32 - (2 * rotate))); |
| 150 | } |
| 151 | uint32_t value; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 152 | }; |
Elliott Hughes | 1ca9849 | 2012-04-12 17:21:02 -0700 | [diff] [blame] | 153 | std::ostream& operator<<(std::ostream& os, const ShiftedImmediate& rhs) { |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 154 | os << "#" << rhs.value; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 155 | return os; |
| 156 | } |
| 157 | |
| 158 | struct RegisterList { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 159 | explicit RegisterList(uint32_t instruction) : register_list(instruction & 0xffff) {} |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 160 | uint32_t register_list; |
| 161 | }; |
| 162 | std::ostream& operator<<(std::ostream& os, const RegisterList& rhs) { |
| 163 | if (rhs.register_list == 0) { |
| 164 | os << "<no register list?>"; |
| 165 | return os; |
| 166 | } |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 167 | os << "{"; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 168 | bool first = true; |
| 169 | for (size_t i = 0; i < 16; i++) { |
| 170 | if ((rhs.register_list & (1 << i)) != 0) { |
| 171 | if (first) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 172 | first = false; |
| 173 | } else { |
| 174 | os << ", "; |
| 175 | } |
| 176 | os << ArmRegister(i); |
| 177 | } |
| 178 | } |
| 179 | os << "}"; |
| 180 | return os; |
| 181 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 182 | |
| 183 | void DisassemblerArm::DumpArm(std::ostream& os, const uint8_t* instr_ptr) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 184 | uint32_t instruction = ReadU32(instr_ptr); |
| 185 | uint32_t cond = (instruction >> 28) & 0xf; |
| 186 | uint32_t op1 = (instruction >> 25) & 0x7; |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 187 | std::string opcode; |
| 188 | std::string suffixes; |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 189 | std::ostringstream args; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 190 | switch (op1) { |
| 191 | case 0: |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 192 | case 1: // Data processing instructions. |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 193 | { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 194 | if ((instruction & 0x0ff000f0) == 0x01200070) { // BKPT |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 195 | opcode = "bkpt"; |
| 196 | uint32_t imm12 = (instruction >> 8) & 0xfff; |
| 197 | uint32_t imm4 = (instruction & 0xf); |
| 198 | args << '#' << ((imm12 << 4) | imm4); |
| 199 | break; |
| 200 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 201 | if ((instruction & 0x0fffffd0) == 0x012fff10) { // BX and BLX (register) |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 202 | opcode = (((instruction >> 5) & 1) ? "blx" : "bx"); |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 203 | args << ArmRegister(instruction & 0xf); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 204 | break; |
| 205 | } |
| 206 | bool i = (instruction & (1 << 25)) != 0; |
| 207 | bool s = (instruction & (1 << 20)) != 0; |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 208 | uint32_t op = (instruction >> 21) & 0xf; |
| 209 | opcode = kDataProcessingOperations[op]; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 210 | bool implicit_s = ((op & ~3) == 8); // TST, TEQ, CMP, and CMN. |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 211 | if (implicit_s) { |
| 212 | // Rd is unused (and not shown), and we don't show the 's' suffix either. |
| 213 | } else { |
| 214 | if (s) { |
| 215 | suffixes += 's'; |
| 216 | } |
| 217 | args << ArmRegister(instruction, 12) << ", "; |
| 218 | } |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 219 | if (i) { |
Elliott Hughes | 1ca9849 | 2012-04-12 17:21:02 -0700 | [diff] [blame] | 220 | args << ArmRegister(instruction, 16) << ", " << ShiftedImmediate(instruction); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 221 | } else { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 222 | args << Rm(instruction); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 226 | case 2: // Load/store word and unsigned byte. |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 227 | { |
| 228 | bool p = (instruction & (1 << 24)) != 0; |
| 229 | bool b = (instruction & (1 << 22)) != 0; |
| 230 | bool w = (instruction & (1 << 21)) != 0; |
| 231 | bool l = (instruction & (1 << 20)) != 0; |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 232 | opcode = StringPrintf("%s%s", (l ? "ldr" : "str"), (b ? "b" : "")); |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 233 | args << ArmRegister(instruction, 12) << ", "; |
| 234 | ArmRegister rn(instruction, 16); |
| 235 | if (rn.r == 0xf) { |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 236 | UNIMPLEMENTED(FATAL) << "literals"; |
| 237 | } else { |
| 238 | bool wback = !p || w; |
Elliott Hughes | 1ca9849 | 2012-04-12 17:21:02 -0700 | [diff] [blame] | 239 | uint32_t offset = (instruction & 0xfff); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 240 | if (p && !wback) { |
Elliott Hughes | 1ca9849 | 2012-04-12 17:21:02 -0700 | [diff] [blame] | 241 | args << "[" << rn << ", #" << offset << "]"; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 242 | } else if (p && wback) { |
Elliott Hughes | 1ca9849 | 2012-04-12 17:21:02 -0700 | [diff] [blame] | 243 | args << "[" << rn << ", #" << offset << "]!"; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 244 | } else if (!p && wback) { |
Elliott Hughes | 1ca9849 | 2012-04-12 17:21:02 -0700 | [diff] [blame] | 245 | args << "[" << rn << "], #" << offset; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 246 | } else { |
| 247 | LOG(FATAL) << p << " " << w; |
| 248 | } |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 249 | if (rn.r == 9) { |
| 250 | args << " ; "; |
Elliott Hughes | 1ca9849 | 2012-04-12 17:21:02 -0700 | [diff] [blame] | 251 | Thread::DumpThreadOffset(args, offset, 4); |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 252 | } |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 256 | case 4: // Load/store multiple. |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 257 | { |
| 258 | bool p = (instruction & (1 << 24)) != 0; |
| 259 | bool u = (instruction & (1 << 23)) != 0; |
| 260 | bool w = (instruction & (1 << 21)) != 0; |
| 261 | bool l = (instruction & (1 << 20)) != 0; |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 262 | opcode = StringPrintf("%s%c%c", (l ? "ldm" : "stm"), (u ? 'i' : 'd'), (p ? 'b' : 'a')); |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 263 | args << ArmRegister(instruction, 16) << (w ? "!" : "") << ", " << RegisterList(instruction); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 264 | } |
| 265 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 266 | case 5: // Branch/branch with link. |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 267 | { |
| 268 | bool bl = (instruction & (1 << 24)) != 0; |
| 269 | opcode = (bl ? "bl" : "b"); |
Elliott Hughes | d86261e | 2012-04-11 11:23:23 -0700 | [diff] [blame] | 270 | int32_t imm26 = (instruction & 0xffffff) << 2; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 271 | int32_t imm32 = (imm26 << 6) >> 6; // Sign extend. |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 272 | DumpBranchTarget(args, instr_ptr + 8, imm32); |
| 273 | } |
| 274 | break; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 275 | default: |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 276 | opcode = "???"; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 277 | break; |
| 278 | } |
Elliott Hughes | 3d71d07 | 2012-04-10 18:28:35 -0700 | [diff] [blame] | 279 | opcode += kConditionCodeNames[cond]; |
| 280 | opcode += suffixes; |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 281 | // TODO: a more complete ARM disassembler could generate wider opcodes. |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 282 | os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str()) << args.str() << '\n'; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Ian Rogers | a9650dd | 2013-10-04 08:23:32 -0700 | [diff] [blame] | 285 | int32_t ThumbExpand(int32_t imm12) { |
| 286 | if ((imm12 & 0xC00) == 0) { |
| 287 | switch ((imm12 >> 8) & 3) { |
| 288 | case 0: |
| 289 | return imm12 & 0xFF; |
| 290 | case 1: |
| 291 | return ((imm12 & 0xFF) << 16) | (imm12 & 0xFF); |
| 292 | case 2: |
| 293 | return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 8); |
| 294 | default: // 3 |
| 295 | return ((imm12 & 0xFF) << 24) | ((imm12 & 0xFF) << 16) | ((imm12 & 0xFF) << 8) | |
| 296 | (imm12 & 0xFF); |
| 297 | } |
| 298 | } else { |
| 299 | uint32_t val = 0x80 | (imm12 & 0x7F); |
| 300 | int32_t rotate = (imm12 >> 7) & 0x1F; |
| 301 | return (val >> rotate) | (val << (32 - rotate)); |
| 302 | } |
| 303 | } |
| 304 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 305 | size_t DisassemblerArm::DumpThumb32(std::ostream& os, const uint8_t* instr_ptr) { |
| 306 | uint32_t instr = (ReadU16(instr_ptr) << 16) | ReadU16(instr_ptr + 2); |
| 307 | // |111|1 1|1000000|0000|1111110000000000| |
| 308 | // |5 3|2 1|0987654|3 0|5 0 5 0| |
| 309 | // |---|---|-------|----|----------------| |
| 310 | // |332|2 2|2222222|1111|1111110000000000| |
| 311 | // |1 9|8 7|6543210|9 6|5 0 5 0| |
| 312 | // |---|---|-------|----|----------------| |
| 313 | // |111|op1| op2 | | | |
| 314 | uint32_t op1 = (instr >> 27) & 3; |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 315 | if (op1 == 0) { |
| 316 | return DumpThumb16(os, instr_ptr); |
| 317 | } |
| 318 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 319 | uint32_t op2 = (instr >> 20) & 0x7F; |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 320 | std::ostringstream opcode; |
| 321 | std::ostringstream args; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 322 | switch (op1) { |
| 323 | case 0: |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 324 | break; |
| 325 | case 1: |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 326 | if ((op2 & 0x64) == 0) { // 00x x0xx |
| 327 | // |111|11|10|00|0|00|0000|1111110000000000| |
| 328 | // |5 3|21|09|87|6|54|3 0|5 0 5 0| |
| 329 | // |---|--|--|--|-|--|----|----------------| |
| 330 | // |332|22|22|22|2|22|1111|1111110000000000| |
| 331 | // |1 9|87|65|43|2|10|9 6|5 0 5 0| |
| 332 | // |---|--|--|--|-|--|----|----------------| |
| 333 | // |111|01|00|op|0|WL| Rn | | |
| 334 | // |111|01| op2 | | | |
| 335 | // STM - 111 01 00-01-0-W0 nnnn rrrrrrrrrrrrrrrr |
| 336 | // LDM - 111 01 00-01-0-W1 nnnn rrrrrrrrrrrrrrrr |
| 337 | // PUSH- 111 01 00-01-0-10 1101 0M0rrrrrrrrrrrrr |
| 338 | // POP - 111 01 00-01-0-11 1101 PM0rrrrrrrrrrrrr |
| 339 | uint32_t op = (instr >> 23) & 3; |
| 340 | uint32_t W = (instr >> 21) & 1; |
| 341 | uint32_t L = (instr >> 20) & 1; |
| 342 | ArmRegister Rn(instr, 16); |
| 343 | if (op == 1 || op == 2) { |
| 344 | if (op == 1) { |
| 345 | if (L == 0) { |
| 346 | opcode << "stm"; |
| 347 | args << Rn << (W == 0 ? "" : "!") << ", "; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 348 | } else { |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 349 | if (Rn.r != 13) { |
| 350 | opcode << "ldm"; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 351 | args << Rn << (W == 0 ? "" : "!") << ", "; |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 352 | } else { |
| 353 | opcode << "pop"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 356 | } else { |
| 357 | if (L == 0) { |
| 358 | if (Rn.r != 13) { |
| 359 | opcode << "stmdb"; |
| 360 | args << Rn << (W == 0 ? "" : "!") << ", "; |
| 361 | } else { |
| 362 | opcode << "push"; |
| 363 | } |
| 364 | } else { |
| 365 | opcode << "ldmdb"; |
| 366 | args << Rn << (W == 0 ? "" : "!") << ", "; |
| 367 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 368 | } |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 369 | args << RegisterList(instr); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 370 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 371 | } else if ((op2 & 0x64) == 4) { // 00x x1xx |
Ian Rogers | 9af8940 | 2012-09-07 11:29:35 -0700 | [diff] [blame] | 372 | uint32_t op3 = (instr >> 23) & 3; |
| 373 | uint32_t op4 = (instr >> 20) & 3; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 374 | // uint32_t op5 = (instr >> 4) & 0xF; |
Ian Rogers | 9af8940 | 2012-09-07 11:29:35 -0700 | [diff] [blame] | 375 | ArmRegister Rn(instr, 16); |
| 376 | ArmRegister Rt(instr, 12); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame^] | 377 | ArmRegister Rd(instr, 8); |
Ian Rogers | 9af8940 | 2012-09-07 11:29:35 -0700 | [diff] [blame] | 378 | uint32_t imm8 = instr & 0xFF; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame^] | 379 | if ((op3 & 2) == 2) { // 1x |
| 380 | int W = (instr >> 21) & 1; |
| 381 | int U = (instr >> 23) & 1; |
| 382 | int P = (instr >> 24) & 1; |
| 383 | |
| 384 | if ((op4 & 1) == 1) { |
| 385 | opcode << "ldrd"; |
| 386 | } else { |
| 387 | opcode << "strd"; |
| 388 | } |
| 389 | args << Rt << "," << Rd << ", [" << Rn; |
| 390 | const char *sign = U ? "+" : "-"; |
| 391 | if (P == 0 && W == 1) { |
| 392 | args << "], #" << sign << imm8; |
| 393 | } else { |
| 394 | args << ", #" << sign << imm8 << "]"; |
| 395 | if (W == 1) { |
| 396 | args << "!"; |
| 397 | } |
| 398 | } |
| 399 | } else { // 0x |
| 400 | switch (op4) { |
| 401 | case 0: |
| 402 | if (op3 == 0) { // op3 is 00, op4 is 00 |
| 403 | opcode << "strex"; |
| 404 | args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]"; |
| 405 | } else { // op3 is 01, op4 is 00 |
| 406 | // this is one of strexb, strexh or strexd |
| 407 | int op5 = (instr >> 4) & 0xf; |
| 408 | switch (op5) { |
| 409 | case 4: |
| 410 | opcode << "strexb"; |
| 411 | break; |
| 412 | case 5: |
| 413 | opcode << "strexh"; |
| 414 | break; |
| 415 | case 7: |
| 416 | opcode << "strexd"; |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | break; |
| 421 | case 1: |
| 422 | if (op3 == 0) { // op3 is 00, op4 is 01 |
| 423 | opcode << "ldrex"; |
| 424 | args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]"; |
| 425 | } else { // op3 is 01, op4 is 01 |
| 426 | // this is one of strexb, strexh or strexd |
| 427 | int op5 = (instr >> 4) & 0xf; |
| 428 | switch (op5) { |
| 429 | case 0: |
| 430 | opcode << "tbb"; |
| 431 | break; |
| 432 | case 1: |
| 433 | opcode << "tbh"; |
| 434 | break; |
| 435 | case 4: |
| 436 | opcode << "ldrexb"; |
| 437 | break; |
| 438 | case 5: |
| 439 | opcode << "ldrexh"; |
| 440 | break; |
| 441 | case 7: |
| 442 | opcode << "ldrexd"; |
| 443 | break; |
| 444 | } |
| 445 | } |
| 446 | break; |
| 447 | case 2: // op3 is 0x, op4 is 10 |
| 448 | case 3: // op3 is 0x, op4 is 11 |
| 449 | if (op4 == 2) { |
| 450 | opcode << "strd"; |
| 451 | } else { |
| 452 | opcode << "ldrd"; |
| 453 | } |
| 454 | int W = (instr >> 21) & 1; |
| 455 | int U = (instr >> 23) & 1; |
| 456 | int P = (instr >> 24) & 1; |
| 457 | |
| 458 | args << Rt << "," << Rd << ", [" << Rn; |
| 459 | const char *sign = U ? "+" : "-"; |
| 460 | if (P == 0 && W == 1) { |
| 461 | args << "], #" << sign << imm8; |
| 462 | } else { |
| 463 | args << ", #" << sign << imm8 << "]"; |
| 464 | if (W == 1) { |
| 465 | args << "!"; |
| 466 | } |
| 467 | } |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | |
Ian Rogers | 9af8940 | 2012-09-07 11:29:35 -0700 | [diff] [blame] | 473 | if (op3 == 0 && op4 == 0) { // STREX |
| 474 | ArmRegister Rd(instr, 8); |
| 475 | opcode << "strex"; |
| 476 | args << Rd << ", " << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]"; |
| 477 | } else if (op3 == 0 && op4 == 1) { // LDREX |
| 478 | opcode << "ldrex"; |
| 479 | args << Rt << ", [" << Rn << ", #" << (imm8 << 2) << "]"; |
| 480 | } |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 481 | } else if ((op2 & 0x60) == 0x20) { // 01x xxxx |
| 482 | // Data-processing (shifted register) |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 483 | // |111|1110|0000|0|0000|1111|1100|00|00|0000| |
| 484 | // |5 3|2109|8765|4|3 0|5 |10 8|7 |5 |3 0| |
| 485 | // |---|----|----|-|----|----|----|--|--|----| |
| 486 | // |332|2222|2222|2|1111|1111|1100|00|00|0000| |
| 487 | // |1 9|8765|4321|0|9 6|5 |10 8|7 |5 |3 0| |
| 488 | // |---|----|----|-|----|----|----|--|--|----| |
| 489 | // |111|0101| op3|S| Rn |imm3| Rd |i2|ty| Rm | |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 490 | uint32_t op3 = (instr >> 21) & 0xF; |
| 491 | uint32_t S = (instr >> 20) & 1; |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 492 | uint32_t imm3 = ((instr >> 12) & 0x7); |
| 493 | uint32_t imm2 = ((instr >> 6) & 0x3); |
| 494 | uint32_t imm5 = ((imm3 << 3) | imm2) & 0x1F; |
| 495 | uint32_t shift_type = ((instr >> 4) & 0x2); |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 496 | ArmRegister Rd(instr, 8); |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 497 | ArmRegister Rn(instr, 16); |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 498 | ArmRegister Rm(instr, 0); |
| 499 | switch (op3) { |
| 500 | case 0x0: |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 501 | if (Rd.r != 0xF) { |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 502 | opcode << "and"; |
| 503 | } else { |
Brian Carlstrom | 4a999e2 | 2013-03-11 16:57:09 -0700 | [diff] [blame] | 504 | if (S != 1U) { |
| 505 | opcode << "UNKNOWN TST-" << S; |
| 506 | break; |
| 507 | } |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 508 | opcode << "tst"; |
| 509 | S = 0; // don't print 's' |
| 510 | } |
| 511 | break; |
| 512 | case 0x1: opcode << "bic"; break; |
| 513 | case 0x2: |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 514 | if (Rn.r != 0xF) { |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 515 | opcode << "orr"; |
| 516 | } else { |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 517 | // TODO: use canonical form if there is a shift (lsl, ...). |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 518 | opcode << "mov"; |
| 519 | } |
| 520 | break; |
| 521 | case 0x3: |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 522 | if (Rn.r != 0xF) { |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 523 | opcode << "orn"; |
| 524 | } else { |
| 525 | opcode << "mvn"; |
| 526 | } |
| 527 | break; |
| 528 | case 0x4: |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 529 | if (Rd.r != 0xF) { |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 530 | opcode << "eor"; |
| 531 | } else { |
Brian Carlstrom | 4a999e2 | 2013-03-11 16:57:09 -0700 | [diff] [blame] | 532 | if (S != 1U) { |
| 533 | opcode << "UNKNOWN TEQ-" << S; |
| 534 | break; |
| 535 | } |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 536 | opcode << "teq"; |
| 537 | S = 0; // don't print 's' |
| 538 | } |
| 539 | break; |
| 540 | case 0x6: opcode << "pkh"; break; |
| 541 | case 0x8: |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 542 | if (Rd.r != 0xF) { |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 543 | opcode << "add"; |
| 544 | } else { |
Brian Carlstrom | 4a999e2 | 2013-03-11 16:57:09 -0700 | [diff] [blame] | 545 | if (S != 1U) { |
| 546 | opcode << "UNKNOWN CMN-" << S; |
| 547 | break; |
| 548 | } |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 549 | opcode << "cmn"; |
| 550 | S = 0; // don't print 's' |
| 551 | } |
| 552 | break; |
| 553 | case 0xA: opcode << "adc"; break; |
| 554 | case 0xB: opcode << "sbc"; break; |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 555 | case 0xD: |
| 556 | if (Rd.r != 0xF) { |
| 557 | opcode << "sub"; |
| 558 | } else { |
Brian Carlstrom | 4a999e2 | 2013-03-11 16:57:09 -0700 | [diff] [blame] | 559 | if (S != 1U) { |
| 560 | opcode << "UNKNOWN CMP-" << S; |
| 561 | break; |
| 562 | } |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 563 | opcode << "cmp"; |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 564 | S = 0; // don't print 's' |
| 565 | } |
| 566 | break; |
| 567 | case 0xE: opcode << "rsb"; break; |
| 568 | default: opcode << "UNKNOWN DPSR-" << op3; break; |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 569 | } |
Ian Rogers | 087b241 | 2012-03-21 01:30:32 -0700 | [diff] [blame] | 570 | |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 571 | if (S == 1) { |
| 572 | opcode << "s"; |
Ian Rogers | 087b241 | 2012-03-21 01:30:32 -0700 | [diff] [blame] | 573 | } |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 574 | opcode << ".w"; |
Sebastien Hertz | d9e63c0 | 2013-02-22 15:19:55 +0100 | [diff] [blame] | 575 | |
| 576 | if (Rd.r != 0xF) { |
| 577 | args << Rd << ", "; |
| 578 | } |
| 579 | if (Rn.r != 0xF) { |
| 580 | args << Rn << ", "; |
| 581 | } |
| 582 | args << Rm; |
| 583 | |
| 584 | // Shift operand. |
| 585 | bool noShift = (imm5 == 0 && shift_type != 0x3); |
| 586 | if (!noShift) { |
| 587 | args << ", "; |
| 588 | switch (shift_type) { |
| 589 | case 0x0: args << "lsl"; break; |
| 590 | case 0x1: args << "lsr"; break; |
| 591 | case 0x2: args << "asr"; break; |
| 592 | case 0x3: |
| 593 | if (imm5 == 0) { |
| 594 | args << "rrx"; |
| 595 | } else { |
| 596 | args << "ror"; |
| 597 | } |
| 598 | break; |
| 599 | } |
| 600 | if (shift_type != 0x3 /* rrx */) { |
| 601 | args << StringPrintf(" #%d", imm5); |
| 602 | } |
| 603 | } |
| 604 | |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 605 | } else if ((op2 & 0x40) == 0x40) { // 1xx xxxx |
| 606 | // Co-processor instructions |
| 607 | // |111|1|11|000000|0000|1111|1100|000|0 |0000| |
| 608 | // |5 3|2|10|987654|3 0|54 2|10 8|7 5|4 | 0| |
| 609 | // |---|-|--|------|----|----|----|---|---|----| |
| 610 | // |332|2|22|222222|1111|1111|1100|000|0 |0000| |
| 611 | // |1 9|8|76|543210|9 6|54 2|10 8|7 5|4 | 0| |
| 612 | // |---|-|--|------|----|----|----|---|---|----| |
| 613 | // |111| |11| op3 | Rn | |copr| |op4| | |
| 614 | uint32_t op3 = (instr >> 20) & 0x3F; |
| 615 | uint32_t coproc = (instr >> 8) & 0xF; |
| 616 | uint32_t op4 = (instr >> 4) & 0x1; |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame^] | 617 | |
| 618 | if (coproc == 10 || coproc == 11) { // 101x |
| 619 | if (op3 < 0x20 && (op3 >> 1) != 2) { // 0xxxxx and not 00010x |
| 620 | // extension load/store instructions |
| 621 | int op = op3 & 0x1f; |
Ian Rogers | 9af8940 | 2012-09-07 11:29:35 -0700 | [diff] [blame] | 622 | uint32_t P = (instr >> 24) & 1; |
| 623 | uint32_t U = (instr >> 23) & 1; |
| 624 | uint32_t D = (instr >> 22) & 1; |
| 625 | uint32_t W = (instr >> 21) & 1; |
| 626 | uint32_t S = (instr >> 8) & 1; |
| 627 | ArmRegister Rn(instr, 16); |
| 628 | uint32_t Vd = (instr >> 12) & 0xF; |
| 629 | uint32_t imm8 = instr & 0xFF; |
| 630 | uint32_t d = (S == 0 ? ((Vd << 1) | D) : (Vd | (D << 4))); |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame^] | 631 | ArmRegister Rd(d, 0); |
| 632 | |
| 633 | if (op == 8 || op == 12 || op == 10 || op == 14 || |
| 634 | op == 18 || op == 22) { // 01x00 or 01x10 |
| 635 | // vector store multiple or vpush |
| 636 | if (P == 1 && U == 0 && W == 1 && Rn.r == 13) { |
| 637 | opcode << "vpush" << (S == 0 ? ".f64" : ".f32"); |
| 638 | args << Rd << " .. " << (Rd.r + imm8); |
| 639 | } else { |
| 640 | opcode << "vstm" << (S == 0 ? ".f64" : ".f32"); |
| 641 | args << Rn << ", " << Rd << " .. " << (Rd.r + imm8); |
| 642 | } |
| 643 | } else if (op == 16 || op == 20 || op == 24 || op == 28) { |
| 644 | // 1xx00 |
| 645 | // vector store register |
| 646 | opcode << "vstr" << (S == 0 ? ".f64" : ".f32"); |
| 647 | args << Rd << ", [" << Rn << ", #" << imm8 << "]"; |
| 648 | } else if (op == 17 || op == 21 || op == 25 || op == 29) { |
| 649 | // 1xx01 |
| 650 | // vector load register |
| 651 | opcode << "vldr" << (S == 0 ? ".f64" : ".f32"); |
| 652 | args << Rd << ", [" << Rn << ", #" << imm8 << "]"; |
| 653 | } else if (op == 9 || op == 13 || op == 11 || op == 15 || |
| 654 | op == 19 || op == 23 ) { // 01x11 10x11 |
| 655 | // vldm or vpop |
| 656 | if (P == 1 && U == 0 && W == 1 && Rn.r == 13) { |
| 657 | opcode << "vpop" << (S == 0 ? ".f64" : ".f32"); |
| 658 | args << Rd << " .. " << (Rd.r + imm8); |
| 659 | } else { |
| 660 | opcode << "vldm" << (S == 0 ? ".f64" : ".f32"); |
| 661 | args << Rn << ", " << Rd << " .. " << (Rd.r + imm8); |
| 662 | } |
Ian Rogers | 9af8940 | 2012-09-07 11:29:35 -0700 | [diff] [blame] | 663 | } |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame^] | 664 | } else if ((op3 >> 1) == 2) { // 00010x |
| 665 | // 64 bit transfers |
| 666 | } else if ((op3 >> 4) == 2 && op4 == 0) { // 10xxxx, op = 0 |
| 667 | // fp data processing |
| 668 | } else if ((op3 >> 4) == 2 && op4 == 1) { // 10xxxx, op = 1 |
| 669 | // 8,16,32 bit transfers |
Ian Rogers | 9af8940 | 2012-09-07 11:29:35 -0700 | [diff] [blame] | 670 | } |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame^] | 671 | } |
| 672 | |
| 673 | if ((op3 & 0x30) == 0x20 && op4 == 0) { // 10 xxxx ... 0 |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 674 | if ((coproc & 0xE) == 0xA) { |
| 675 | // VFP data-processing instructions |
| 676 | // |111|1|1100|0000|0000|1111|110|0|00 |0|0|0000| |
| 677 | // |5 3|2|1098|7654|3 0|54 2|10 |8|76 |5|4|3 0| |
| 678 | // |---|-|----|----|----|----|---|-|----|-|-|----| |
| 679 | // |332|2|2222|2222|1111|1111|110|0|00 |0|0|0000| |
| 680 | // |1 9|8|7654|3210|9 6|54 2|109|8|76 |5|4|3 0| |
| 681 | // |---|-|----|----|----|----|---|-|----|-|-|----| |
| 682 | // |111|T|1110|opc1|opc2| |101| |opc3| | | | |
| 683 | // 111 0 1110|1111 0100 1110 101 0 01 1 0 1001 - eef4ea69 |
| 684 | uint32_t opc1 = (instr >> 20) & 0xF; |
| 685 | uint32_t opc2 = (instr >> 16) & 0xF; |
Ian Rogers | 0183dd7 | 2012-09-17 23:06:51 -0700 | [diff] [blame] | 686 | uint32_t opc3 = (instr >> 6) & 0x3; |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 687 | if ((opc1 & 0xB) == 0xB) { // 1x11 |
| 688 | // Other VFP data-processing instructions. |
Ian Rogers | 0183dd7 | 2012-09-17 23:06:51 -0700 | [diff] [blame] | 689 | uint32_t D = (instr >> 22) & 0x1; |
| 690 | uint32_t Vd = (instr >> 12) & 0xF; |
| 691 | uint32_t sz = (instr >> 8) & 1; |
| 692 | uint32_t M = (instr >> 5) & 1; |
| 693 | uint32_t Vm = instr & 0xF; |
| 694 | bool dp_operation = sz == 1; |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 695 | switch (opc2) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 696 | case 0x1: // Vneg/Vsqrt |
Ian Rogers | 0183dd7 | 2012-09-17 23:06:51 -0700 | [diff] [blame] | 697 | // 1110 11101 D 11 0001 dddd 101s o1M0 mmmm |
| 698 | opcode << (opc3 == 1 ? "vneg" : "vsqrt") << (dp_operation ? ".f64" : ".f32"); |
| 699 | if (dp_operation) { |
| 700 | args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm); |
| 701 | } else { |
| 702 | args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M); |
| 703 | } |
| 704 | break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 705 | case 0x4: case 0x5: { // Vector compare |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 706 | // 1110 11101 D 11 0100 dddd 101 sE1M0 mmmm |
Ian Rogers | 0183dd7 | 2012-09-17 23:06:51 -0700 | [diff] [blame] | 707 | opcode << (opc3 == 1 ? "vcmp" : "vcmpe") << (dp_operation ? ".f64" : ".f32"); |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 708 | if (dp_operation) { |
| 709 | args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm); |
| 710 | } else { |
| 711 | args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M); |
| 712 | } |
| 713 | break; |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | } |
Ian Rogers | 0183dd7 | 2012-09-17 23:06:51 -0700 | [diff] [blame] | 718 | } else if ((op3 & 0x30) == 0x30) { // 11 xxxx |
| 719 | // Advanced SIMD |
| 720 | if ((instr & 0xFFBF0ED0) == 0xeeb10ac0) { // Vsqrt |
| 721 | // 1110 11101 D 11 0001 dddd 101S 11M0 mmmm |
| 722 | // 1110 11101 0 11 0001 1101 1011 1100 1000 - eeb1dbc8 |
| 723 | uint32_t D = (instr >> 22) & 1; |
| 724 | uint32_t Vd = (instr >> 12) & 0xF; |
| 725 | uint32_t sz = (instr >> 8) & 1; |
| 726 | uint32_t M = (instr >> 5) & 1; |
| 727 | uint32_t Vm = instr & 0xF; |
| 728 | bool dp_operation = sz == 1; |
| 729 | opcode << "vsqrt" << (dp_operation ? ".f64" : ".f32"); |
| 730 | if (dp_operation) { |
| 731 | args << "f" << ((D << 4) | Vd) << ", " << "f" << ((M << 4) | Vm); |
| 732 | } else { |
| 733 | args << "f" << ((Vd << 1) | D) << ", " << "f" << ((Vm << 1) | M); |
| 734 | } |
| 735 | } |
Ian Rogers | c7fe4e0 | 2012-03-29 21:36:21 -0700 | [diff] [blame] | 736 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 737 | } |
| 738 | break; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 739 | case 2: |
| 740 | if ((instr & 0x8000) == 0 && (op2 & 0x20) == 0) { |
| 741 | // Data-processing (modified immediate) |
| 742 | // |111|11|10|0000|0|0000|1|111|1100|00000000| |
| 743 | // |5 3|21|09|8765|4|3 0|5|4 2|10 8|7 5 0| |
| 744 | // |---|--|--|----|-|----|-|---|----|--------| |
| 745 | // |332|22|22|2222|2|1111|1|111|1100|00000000| |
| 746 | // |1 9|87|65|4321|0|9 6|5|4 2|10 8|7 5 0| |
| 747 | // |---|--|--|----|-|----|-|---|----|--------| |
| 748 | // |111|10|i0| op3|S| Rn |0|iii| Rd |iiiiiiii| |
| 749 | // 111 10 x0 xxxx x xxxx opxxx xxxx xxxxxxxx |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 750 | uint32_t i = (instr >> 26) & 1; |
| 751 | uint32_t op3 = (instr >> 21) & 0xF; |
| 752 | uint32_t S = (instr >> 20) & 1; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 753 | ArmRegister Rn(instr, 16); |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 754 | uint32_t imm3 = (instr >> 12) & 7; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 755 | ArmRegister Rd(instr, 8); |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 756 | uint32_t imm8 = instr & 0xFF; |
Jeff Hao | 7cb0f9c | 2013-02-04 16:15:27 -0800 | [diff] [blame] | 757 | int32_t imm32 = (i << 11) | (imm3 << 8) | imm8; |
| 758 | if (Rn.r == 0xF && (op3 == 0x2 || op3 == 0x3)) { |
| 759 | if (op3 == 0x2) { |
| 760 | opcode << "mov"; |
| 761 | if (S == 1) { |
| 762 | opcode << "s"; |
| 763 | } |
| 764 | opcode << ".w"; |
| 765 | } else { |
| 766 | opcode << "mvn"; |
| 767 | if (S == 1) { |
| 768 | opcode << "s"; |
| 769 | } |
| 770 | } |
Ian Rogers | a9650dd | 2013-10-04 08:23:32 -0700 | [diff] [blame] | 771 | args << Rd << ", #" << ThumbExpand(imm32); |
Jeff Hao | 7cb0f9c | 2013-02-04 16:15:27 -0800 | [diff] [blame] | 772 | } else if (Rd.r == 0xF && S == 1 && |
| 773 | (op3 == 0x0 || op3 == 0x4 || op3 == 0x8 || op3 == 0xD)) { |
| 774 | if (op3 == 0x0) { |
| 775 | opcode << "tst"; |
| 776 | } else if (op3 == 0x4) { |
| 777 | opcode << "teq"; |
| 778 | } else if (op3 == 0x8) { |
| 779 | opcode << "cmw"; |
| 780 | } else { |
| 781 | opcode << "cmp.w"; |
| 782 | } |
Ian Rogers | a9650dd | 2013-10-04 08:23:32 -0700 | [diff] [blame] | 783 | args << Rn << ", #" << ThumbExpand(imm32); |
Jeff Hao | 7cb0f9c | 2013-02-04 16:15:27 -0800 | [diff] [blame] | 784 | } else { |
| 785 | switch (op3) { |
| 786 | case 0x0: opcode << "and"; break; |
| 787 | case 0x1: opcode << "bic"; break; |
| 788 | case 0x2: opcode << "orr"; break; |
| 789 | case 0x3: opcode << "orn"; break; |
| 790 | case 0x4: opcode << "eor"; break; |
| 791 | case 0x8: opcode << "add"; break; |
| 792 | case 0xA: opcode << "adc"; break; |
| 793 | case 0xB: opcode << "sbc"; break; |
| 794 | case 0xD: opcode << "sub"; break; |
| 795 | case 0xE: opcode << "rsb"; break; |
| 796 | default: opcode << "UNKNOWN DPMI-" << op3; break; |
| 797 | } |
| 798 | if (S == 1) { |
| 799 | opcode << "s"; |
| 800 | } |
Ian Rogers | a9650dd | 2013-10-04 08:23:32 -0700 | [diff] [blame] | 801 | args << Rd << ", " << Rn << ", #" << ThumbExpand(imm32); |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 802 | } |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 803 | } else if ((instr & 0x8000) == 0 && (op2 & 0x20) != 0) { |
| 804 | // Data-processing (plain binary immediate) |
| 805 | // |111|11|10|00000|0000|1|111110000000000| |
| 806 | // |5 3|21|09|87654|3 0|5|4 0 5 0| |
| 807 | // |---|--|--|-----|----|-|---------------| |
| 808 | // |332|22|22|22222|1111|1|111110000000000| |
| 809 | // |1 9|87|65|43210|9 6|5|4 0 5 0| |
| 810 | // |---|--|--|-----|----|-|---------------| |
| 811 | // |111|10|x1| op3 | Rn |0|xxxxxxxxxxxxxxx| |
| 812 | uint32_t op3 = (instr >> 20) & 0x1F; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 813 | switch (op3) { |
Ian Rogers | 5501913 | 2013-02-08 01:05:23 -0800 | [diff] [blame] | 814 | case 0x00: case 0x0A: { |
| 815 | // ADD/SUB.W Rd, Rn #imm12 - 111 10 i1 0101 0 nnnn 0 iii dddd iiiiiiii |
Ian Rogers | 66a3fca | 2012-04-09 19:51:34 -0700 | [diff] [blame] | 816 | ArmRegister Rd(instr, 8); |
| 817 | ArmRegister Rn(instr, 16); |
| 818 | uint32_t i = (instr >> 26) & 1; |
| 819 | uint32_t imm3 = (instr >> 12) & 0x7; |
| 820 | uint32_t imm8 = instr & 0xFF; |
| 821 | uint32_t imm12 = (i << 11) | (imm3 << 8) | imm8; |
| 822 | if (Rn.r != 0xF) { |
Ian Rogers | 5501913 | 2013-02-08 01:05:23 -0800 | [diff] [blame] | 823 | opcode << (op3 == 0 ? "addw" : "subw"); |
Ian Rogers | 66a3fca | 2012-04-09 19:51:34 -0700 | [diff] [blame] | 824 | args << Rd << ", " << Rn << ", #" << imm12; |
| 825 | } else { |
| 826 | opcode << "adr"; |
| 827 | args << Rd << ", "; |
Ian Rogers | 5501913 | 2013-02-08 01:05:23 -0800 | [diff] [blame] | 828 | DumpBranchTarget(args, instr_ptr + 4, (op3 == 0) ? imm12 : -imm12); |
Ian Rogers | 66a3fca | 2012-04-09 19:51:34 -0700 | [diff] [blame] | 829 | } |
| 830 | break; |
| 831 | } |
Ian Rogers | 5501913 | 2013-02-08 01:05:23 -0800 | [diff] [blame] | 832 | case 0x04: case 0x0C: { |
| 833 | // MOVW/T Rd, #imm16 - 111 10 i0 0010 0 iiii 0 iii dddd iiiiiiii |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 834 | ArmRegister Rd(instr, 8); |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 835 | uint32_t i = (instr >> 26) & 1; |
| 836 | uint32_t imm3 = (instr >> 12) & 0x7; |
| 837 | uint32_t imm8 = instr & 0xFF; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 838 | uint32_t Rn = (instr >> 16) & 0xF; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 839 | uint32_t imm16 = (Rn << 12) | (i << 11) | (imm3 << 8) | imm8; |
Ian Rogers | 5501913 | 2013-02-08 01:05:23 -0800 | [diff] [blame] | 840 | opcode << (op3 == 0x04 ? "movw" : "movt"); |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 841 | args << Rd << ", #" << imm16; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 842 | break; |
| 843 | } |
jeffhao | eae2691 | 2013-01-28 16:29:54 -0800 | [diff] [blame] | 844 | case 0x16: { |
| 845 | // BFI Rd, Rn, #lsb, #width - 111 10 0 11 011 0 nnnn 0 iii dddd ii 0 iiiii |
| 846 | ArmRegister Rd(instr, 8); |
| 847 | ArmRegister Rn(instr, 16); |
| 848 | uint32_t msb = instr & 0x1F; |
| 849 | uint32_t imm2 = (instr >> 6) & 0x3; |
| 850 | uint32_t imm3 = (instr >> 12) & 0x7; |
| 851 | uint32_t lsb = (imm3 << 2) | imm2; |
| 852 | uint32_t width = msb - lsb + 1; |
| 853 | if (Rn.r != 0xF) { |
| 854 | opcode << "bfi"; |
| 855 | args << Rd << ", " << Rn << ", #" << lsb << ", #" << width; |
| 856 | } else { |
| 857 | opcode << "bfc"; |
| 858 | args << Rd << ", #" << lsb << ", #" << width; |
| 859 | } |
| 860 | break; |
| 861 | } |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 862 | default: |
| 863 | break; |
| 864 | } |
| 865 | } else { |
| 866 | // Branches and miscellaneous control |
| 867 | // |111|11|1000000|0000|1|111|1100|00000000| |
| 868 | // |5 3|21|0987654|3 0|5|4 2|10 8|7 5 0| |
| 869 | // |---|--|-------|----|-|---|----|--------| |
| 870 | // |332|22|2222222|1111|1|111|1100|00000000| |
| 871 | // |1 9|87|6543210|9 6|5|4 2|10 8|7 5 0| |
| 872 | // |---|--|-------|----|-|---|----|--------| |
| 873 | // |111|10| op2 | |1|op3|op4 | | |
| 874 | |
| 875 | uint32_t op3 = (instr >> 12) & 7; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 876 | // uint32_t op4 = (instr >> 8) & 0xF; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 877 | switch (op3) { |
| 878 | case 0: |
| 879 | if ((op2 & 0x38) != 0x38) { |
| 880 | // Conditional branch |
| 881 | // |111|11|1|0000|000000|1|1|1 |1|1 |10000000000| |
| 882 | // |5 3|21|0|9876|543 0|5|4|3 |2|1 |0 5 0| |
| 883 | // |---|--|-|----|------|-|-|--|-|--|-----------| |
| 884 | // |332|22|2|2222|221111|1|1|1 |1|1 |10000000000| |
| 885 | // |1 9|87|6|5432|109 6|5|4|3 |2|1 |0 5 0| |
| 886 | // |---|--|-|----|------|-|-|--|-|--|-----------| |
| 887 | // |111|10|S|cond| imm6 |1|0|J1|0|J2| imm11 | |
| 888 | uint32_t S = (instr >> 26) & 1; |
| 889 | uint32_t J2 = (instr >> 11) & 1; |
| 890 | uint32_t J1 = (instr >> 13) & 1; |
| 891 | uint32_t imm6 = (instr >> 16) & 0x3F; |
| 892 | uint32_t imm11 = instr & 0x7FF; |
| 893 | uint32_t cond = (instr >> 22) & 0xF; |
| 894 | int32_t imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1); |
| 895 | imm32 = (imm32 << 11) >> 11; // sign extend 21bit immediate |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 896 | opcode << "b"; |
| 897 | DumpCond(opcode, cond); |
| 898 | opcode << ".w"; |
| 899 | DumpBranchTarget(args, instr_ptr + 4, imm32); |
Ian Rogers | 9af8940 | 2012-09-07 11:29:35 -0700 | [diff] [blame] | 900 | } else if (op2 == 0x3B) { |
| 901 | // Miscellaneous control instructions |
| 902 | uint32_t op5 = (instr >> 4) & 0xF; |
| 903 | switch (op5) { |
| 904 | case 4: opcode << "dsb"; break; |
| 905 | case 5: opcode << "dmb"; break; |
| 906 | case 6: opcode << "isb"; break; |
| 907 | } |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 908 | } |
| 909 | break; |
| 910 | case 2: |
Ian Rogers | d0876a9 | 2013-02-08 11:30:38 -0800 | [diff] [blame] | 911 | if ((op2 & 0x38) == 0x38) { |
| 912 | if (op2 == 0x7F) { |
| 913 | opcode << "udf"; |
| 914 | } |
| 915 | break; |
| 916 | } |
| 917 | // Else deliberate fall-through to B. |
| 918 | case 1: case 3: { |
| 919 | // B |
| 920 | // |111|11|1|0000|000000|11|1 |1|1 |10000000000| |
| 921 | // |5 3|21|0|9876|543 0|54|3 |2|1 |0 5 0| |
| 922 | // |---|--|-|----|------|--|--|-|--|-----------| |
| 923 | // |332|22|2|2222|221111|11|1 |1|1 |10000000000| |
| 924 | // |1 9|87|6|5 2|10 6|54|3 |2|1 |0 5 0| |
| 925 | // |---|--|-|----|------|--|--|-|--|-----------| |
| 926 | // |111|10|S|cond| imm6 |10|J1|0|J2| imm11 | |
| 927 | // |111|10|S| imm10 |10|J1|1|J2| imm11 | |
| 928 | uint32_t S = (instr >> 26) & 1; |
| 929 | uint32_t cond = (instr >> 22) & 0xF; |
| 930 | uint32_t J2 = (instr >> 11) & 1; |
| 931 | uint32_t form = (instr >> 12) & 1; |
| 932 | uint32_t J1 = (instr >> 13) & 1; |
| 933 | uint32_t imm10 = (instr >> 16) & 0x3FF; |
| 934 | uint32_t imm6 = (instr >> 16) & 0x3F; |
| 935 | uint32_t imm11 = instr & 0x7FF; |
| 936 | opcode << "b"; |
| 937 | int32_t imm32; |
| 938 | if (form == 0) { |
| 939 | DumpCond(opcode, cond); |
| 940 | imm32 = (S << 20) | (J2 << 19) | (J1 << 18) | (imm6 << 12) | (imm11 << 1); |
| 941 | imm32 = (imm32 << 11) >> 11; // sign extend 21 bit immediate. |
| 942 | } else { |
| 943 | uint32_t I1 = ~(J1 ^ S); |
| 944 | uint32_t I2 = ~(J2 ^ S); |
| 945 | imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1); |
| 946 | imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate. |
| 947 | } |
| 948 | opcode << ".w"; |
| 949 | DumpBranchTarget(args, instr_ptr + 4, imm32); |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 950 | break; |
Ian Rogers | d0876a9 | 2013-02-08 11:30:38 -0800 | [diff] [blame] | 951 | } |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 952 | case 4: case 6: case 5: case 7: { |
| 953 | // BL, BLX (immediate) |
| 954 | // |111|11|1|0000000000|11|1 |1|1 |10000000000| |
| 955 | // |5 3|21|0|9876543 0|54|3 |2|1 |0 5 0| |
| 956 | // |---|--|-|----------|--|--|-|--|-----------| |
| 957 | // |332|22|2|2222221111|11|1 |1|1 |10000000000| |
| 958 | // |1 9|87|6|5 0 6|54|3 |2|1 |0 5 0| |
| 959 | // |---|--|-|----------|--|--|-|--|-----------| |
| 960 | // |111|10|S| imm10 |11|J1|L|J2| imm11 | |
| 961 | uint32_t S = (instr >> 26) & 1; |
| 962 | uint32_t J2 = (instr >> 11) & 1; |
| 963 | uint32_t L = (instr >> 12) & 1; |
| 964 | uint32_t J1 = (instr >> 13) & 1; |
| 965 | uint32_t imm10 = (instr >> 16) & 0x3FF; |
| 966 | uint32_t imm11 = instr & 0x7FF; |
| 967 | if (L == 0) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 968 | opcode << "bx"; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 969 | } else { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 970 | opcode << "blx"; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 971 | } |
| 972 | uint32_t I1 = ~(J1 ^ S); |
| 973 | uint32_t I2 = ~(J2 ^ S); |
| 974 | int32_t imm32 = (S << 24) | (I1 << 23) | (I2 << 22) | (imm10 << 12) | (imm11 << 1); |
| 975 | imm32 = (imm32 << 8) >> 8; // sign extend 24 bit immediate. |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 976 | DumpBranchTarget(args, instr_ptr + 4, imm32); |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 977 | break; |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | break; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 982 | case 3: |
| 983 | switch (op2) { |
| 984 | case 0x00: case 0x02: case 0x04: case 0x06: // 000xxx0 |
| 985 | case 0x08: case 0x0A: case 0x0C: case 0x0E: { |
| 986 | // Store single data item |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 987 | // |111|11|100|000|0|0000|1111|110000|000000| |
| 988 | // |5 3|21|098|765|4|3 0|5 2|10 6|5 0| |
| 989 | // |---|--|---|---|-|----|----|------|------| |
| 990 | // |332|22|222|222|2|1111|1111|110000|000000| |
| 991 | // |1 9|87|654|321|0|9 6|5 2|10 6|5 0| |
| 992 | // |---|--|---|---|-|----|----|------|------| |
| 993 | // |111|11|000|op3|0| | | op4 | | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 994 | uint32_t op3 = (instr >> 21) & 7; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 995 | // uint32_t op4 = (instr >> 6) & 0x3F; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 996 | switch (op3) { |
Ian Rogers | 087b241 | 2012-03-21 01:30:32 -0700 | [diff] [blame] | 997 | case 0x0: case 0x4: { |
| 998 | // STRB Rt,[Rn,#+/-imm8] - 111 11 00 0 0 00 0 nnnn tttt 1 PUWii ii iiii |
| 999 | // STRB Rt,[Rn,Rm,lsl #imm2] - 111 11 00 0 0 00 0 nnnn tttt 0 00000 ii mmmm |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1000 | ArmRegister Rn(instr, 16); |
| 1001 | ArmRegister Rt(instr, 12); |
Ian Rogers | 087b241 | 2012-03-21 01:30:32 -0700 | [diff] [blame] | 1002 | opcode << "strb"; |
| 1003 | if ((instr & 0x800) != 0) { |
| 1004 | uint32_t imm8 = instr & 0xFF; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1005 | args << Rt << ", [" << Rn << ",#" << imm8 << "]"; |
Ian Rogers | 087b241 | 2012-03-21 01:30:32 -0700 | [diff] [blame] | 1006 | } else { |
| 1007 | uint32_t imm2 = (instr >> 4) & 3; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1008 | ArmRegister Rm(instr, 0); |
| 1009 | args << Rt << ", [" << Rn << ", " << Rm; |
Ian Rogers | 087b241 | 2012-03-21 01:30:32 -0700 | [diff] [blame] | 1010 | if (imm2 != 0) { |
| 1011 | args << ", " << "lsl #" << imm2; |
| 1012 | } |
| 1013 | args << "]"; |
| 1014 | } |
| 1015 | break; |
| 1016 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1017 | case 0x2: case 0x6: { |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1018 | ArmRegister Rn(instr, 16); |
| 1019 | ArmRegister Rt(instr, 12); |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 1020 | if (op3 == 2) { |
Ian Rogers | 66a3fca | 2012-04-09 19:51:34 -0700 | [diff] [blame] | 1021 | if ((instr & 0x800) != 0) { |
| 1022 | // STR Rt, [Rn, #imm8] - 111 11 000 010 0 nnnn tttt 1PUWiiiiiiii |
| 1023 | uint32_t P = (instr >> 10) & 1; |
| 1024 | uint32_t U = (instr >> 9) & 1; |
| 1025 | uint32_t W = (instr >> 8) & 1; |
| 1026 | uint32_t imm8 = instr & 0xFF; |
| 1027 | int32_t imm32 = (imm8 << 24) >> 24; // sign-extend imm8 |
| 1028 | if (Rn.r == 13 && P == 1 && U == 0 && W == 1 && imm32 == 4) { |
| 1029 | opcode << "push"; |
| 1030 | args << Rt; |
| 1031 | } else if (Rn.r == 15 || (P == 0 && W == 0)) { |
| 1032 | opcode << "UNDEFINED"; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 1033 | } else { |
Ian Rogers | 66a3fca | 2012-04-09 19:51:34 -0700 | [diff] [blame] | 1034 | if (P == 1 && U == 1 && W == 0) { |
| 1035 | opcode << "strt"; |
| 1036 | } else { |
| 1037 | opcode << "str"; |
| 1038 | } |
| 1039 | args << Rt << ", [" << Rn; |
| 1040 | if (P == 0 && W == 1) { |
| 1041 | args << "], #" << imm32; |
| 1042 | } else { |
| 1043 | args << ", #" << imm32 << "]"; |
| 1044 | if (W == 1) { |
| 1045 | args << "!"; |
| 1046 | } |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 1047 | } |
| 1048 | } |
Ian Rogers | 66a3fca | 2012-04-09 19:51:34 -0700 | [diff] [blame] | 1049 | } else { |
| 1050 | // STR Rt, [Rn, Rm, LSL #imm2] - 111 11 000 010 0 nnnn tttt 000000iimmmm |
| 1051 | ArmRegister Rn(instr, 16); |
| 1052 | ArmRegister Rt(instr, 12); |
| 1053 | ArmRegister Rm(instr, 0); |
| 1054 | uint32_t imm2 = (instr >> 4) & 3; |
| 1055 | opcode << "str.w"; |
| 1056 | args << Rt << ", [" << Rn << ", " << Rm; |
| 1057 | if (imm2 != 0) { |
| 1058 | args << ", lsl #" << imm2; |
| 1059 | } |
| 1060 | args << "]"; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 1061 | } |
| 1062 | } else if (op3 == 6) { |
Ian Rogers | 66a3fca | 2012-04-09 19:51:34 -0700 | [diff] [blame] | 1063 | // STR.W Rt, [Rn, #imm12] - 111 11 000 110 0 nnnn tttt iiiiiiiiiiii |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1064 | uint32_t imm12 = instr & 0xFFF; |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1065 | opcode << "str.w"; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1066 | args << Rt << ", [" << Rn << ", #" << imm12 << "]"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1067 | } |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 1068 | break; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | break; |
| 1073 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1074 | case 0x03: case 0x0B: case 0x13: case 0x1B: { // 00xx011 |
jeffhao | eae2691 | 2013-01-28 16:29:54 -0800 | [diff] [blame] | 1075 | // Load halfword |
| 1076 | // |111|11|10|0 0|00|0|0000|1111|110000|000000| |
| 1077 | // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0| |
| 1078 | // |---|--|--|---|--|-|----|----|------|------| |
| 1079 | // |332|22|22|2 2|22|2|1111|1111|110000|000000| |
| 1080 | // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0| |
| 1081 | // |---|--|--|---|--|-|----|----|------|------| |
| 1082 | // |111|11|00|op3|01|1| Rn | Rt | op4 | | |
| 1083 | // |111|11| op2 | | | imm12 | |
| 1084 | uint32_t op3 = (instr >> 23) & 3; |
| 1085 | ArmRegister Rn(instr, 16); |
| 1086 | ArmRegister Rt(instr, 12); |
| 1087 | if (Rt.r != 15) { |
| 1088 | if (op3 == 1) { |
| 1089 | // LDRH.W Rt, [Rn, #imm12] - 111 11 00 01 011 nnnn tttt iiiiiiiiiiii |
| 1090 | uint32_t imm12 = instr & 0xFFF; |
| 1091 | opcode << "ldrh.w"; |
| 1092 | args << Rt << ", [" << Rn << ", #" << imm12 << "]"; |
| 1093 | if (Rn.r == 9) { |
| 1094 | args << " ; "; |
| 1095 | Thread::DumpThreadOffset(args, imm12, 4); |
| 1096 | } else if (Rn.r == 15) { |
| 1097 | intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr); |
| 1098 | lit_adr = RoundDown(lit_adr, 4) + 4 + imm12; |
| 1099 | args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr)); |
| 1100 | } |
| 1101 | } else if (op3 == 3) { |
| 1102 | // LDRSH.W Rt, [Rn, #imm12] - 111 11 00 11 011 nnnn tttt iiiiiiiiiiii |
| 1103 | uint32_t imm12 = instr & 0xFFF; |
| 1104 | opcode << "ldrsh.w"; |
| 1105 | args << Rt << ", [" << Rn << ", #" << imm12 << "]"; |
| 1106 | if (Rn.r == 9) { |
| 1107 | args << " ; "; |
| 1108 | Thread::DumpThreadOffset(args, imm12, 4); |
| 1109 | } else if (Rn.r == 15) { |
| 1110 | intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr); |
| 1111 | lit_adr = RoundDown(lit_adr, 4) + 4 + imm12; |
| 1112 | args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr)); |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | break; |
| 1117 | } |
Vladimir Marko | a8b4caf | 2013-10-24 15:08:57 +0100 | [diff] [blame] | 1118 | case 0x29: { // 0101001 |
| 1119 | // |111|11|1000000|0000|1111|1100|00|0 0|0000| |
| 1120 | // |5 3|21|0 4|3 0|5 2|1 8|76|5 4|3 0| |
| 1121 | // |---|--|-------|----|----|----|--|---|----| |
| 1122 | // |332|22|2222222|1111|1111|1100|00|0 0|0000| |
| 1123 | // |1 9|87|6 0|9 6|5 2|1 8|76|5 4|3 0| |
| 1124 | // |---|--|-------|----|----|----|--|---|----| |
| 1125 | // |111|11|0101001| Rm |1111| Rd |11|op3| Rm | |
| 1126 | // REV - 111 11 0101001 mmmm 1111 dddd 1000 mmmm |
| 1127 | // REV16 - 111 11 0101001 mmmm 1111 dddd 1001 mmmm |
| 1128 | // RBIT - 111 11 0101001 mmmm 1111 dddd 1010 mmmm |
| 1129 | // REVSH - 111 11 0101001 mmmm 1111 dddd 1011 mmmm |
| 1130 | if ((instr & 0xf0c0) == 0xf080) { |
| 1131 | uint32_t op3 = (instr >> 4) & 3; |
| 1132 | opcode << kThumbReverseOperations[op3]; |
| 1133 | ArmRegister Rm(instr, 0); |
| 1134 | ArmRegister Rd(instr, 8); |
| 1135 | args << Rd << ", " << Rm; |
| 1136 | ArmRegister Rm2(instr, 16); |
| 1137 | if (Rm.r != Rm2.r || Rm.r == 13 || Rm.r == 15 || Rd.r == 13 || Rd.r == 15) { |
| 1138 | args << " (UNPREDICTABLE)"; |
| 1139 | } |
Vladimir Marko | 1f6754d | 2013-10-28 20:27:17 +0000 | [diff] [blame] | 1140 | } // else unknown instruction |
Vladimir Marko | a8b4caf | 2013-10-24 15:08:57 +0100 | [diff] [blame] | 1141 | break; |
| 1142 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1143 | case 0x05: case 0x0D: case 0x15: case 0x1D: { // 00xx101 |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1144 | // Load word |
| 1145 | // |111|11|10|0 0|00|0|0000|1111|110000|000000| |
| 1146 | // |5 3|21|09|8 7|65|4|3 0|5 2|10 6|5 0| |
| 1147 | // |---|--|--|---|--|-|----|----|------|------| |
| 1148 | // |332|22|22|2 2|22|2|1111|1111|110000|000000| |
| 1149 | // |1 9|87|65|4 3|21|0|9 6|5 2|10 6|5 0| |
| 1150 | // |---|--|--|---|--|-|----|----|------|------| |
| 1151 | // |111|11|00|op3|10|1| Rn | Rt | op4 | | |
| 1152 | // |111|11| op2 | | | imm12 | |
| 1153 | uint32_t op3 = (instr >> 23) & 3; |
| 1154 | uint32_t op4 = (instr >> 6) & 0x3F; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1155 | ArmRegister Rn(instr, 16); |
| 1156 | ArmRegister Rt(instr, 12); |
| 1157 | if (op3 == 1 || Rn.r == 15) { |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1158 | // LDR.W Rt, [Rn, #imm12] - 111 11 00 00 101 nnnn tttt iiiiiiiiiiii |
| 1159 | // LDR.W Rt, [PC, #imm12] - 111 11 00 0x 101 1111 tttt iiiiiiiiiiii |
| 1160 | uint32_t imm12 = instr & 0xFFF; |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1161 | opcode << "ldr.w"; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1162 | args << Rt << ", [" << Rn << ", #" << imm12 << "]"; |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1163 | if (Rn.r == 9) { |
| 1164 | args << " ; "; |
| 1165 | Thread::DumpThreadOffset(args, imm12, 4); |
Ian Rogers | 5b9b1bc | 2012-04-09 22:51:43 -0700 | [diff] [blame] | 1166 | } else if (Rn.r == 15) { |
| 1167 | intptr_t lit_adr = reinterpret_cast<intptr_t>(instr_ptr); |
| 1168 | lit_adr = RoundDown(lit_adr, 4) + 4 + imm12; |
| 1169 | args << " ; " << reinterpret_cast<void*>(*reinterpret_cast<int32_t*>(lit_adr)); |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1170 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1171 | } else if (op4 == 0) { |
| 1172 | // LDR.W Rt, [Rn, Rm{, LSL #imm2}] - 111 11 00 00 101 nnnn tttt 000000iimmmm |
| 1173 | uint32_t imm2 = (instr >> 4) & 0xF; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1174 | ArmRegister rm(instr, 0); |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1175 | opcode << "ldr.w"; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1176 | args << Rt << ", [" << Rn << ", " << rm; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1177 | if (imm2 != 0) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1178 | args << ", lsl #" << imm2; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1179 | } |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1180 | args << "]"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1181 | } else { |
| 1182 | // LDRT Rt, [Rn, #imm8] - 111 11 00 00 101 nnnn tttt 1110iiiiiiii |
| 1183 | uint32_t imm8 = instr & 0xFF; |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1184 | opcode << "ldrt"; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1185 | args << Rt << ", [" << Rn << ", #" << imm8 << "]"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1186 | } |
| 1187 | break; |
| 1188 | } |
Dave Allison | 7020278 | 2013-10-22 17:52:19 -0700 | [diff] [blame^] | 1189 | default: // more formats |
| 1190 | if ((op2 >> 4) == 2) { // 010xxxx |
| 1191 | // data processing (register) |
| 1192 | } else if ((op2 >> 3) == 6) { // 0110xxx |
| 1193 | // Multiply, multiply accumulate, and absolute difference |
| 1194 | op1 = (instr >> 20) & 0x7; |
| 1195 | op2 = (instr >> 4) & 0x2; |
| 1196 | ArmRegister Ra(instr, 12); |
| 1197 | ArmRegister Rn(instr, 16); |
| 1198 | ArmRegister Rm(instr, 0); |
| 1199 | ArmRegister Rd(instr, 8); |
| 1200 | switch (op1) { |
| 1201 | case 0: |
| 1202 | if (op2 == 0) { |
| 1203 | if (Ra.r == 0xf) { |
| 1204 | opcode << "mul"; |
| 1205 | args << Rd << ", " << Rn << ", " << Rm; |
| 1206 | } else { |
| 1207 | opcode << "mla"; |
| 1208 | args << Rd << ", " << Rn << ", " << Rm << ", " << Ra; |
| 1209 | } |
| 1210 | } else { |
| 1211 | opcode << "mls"; |
| 1212 | args << Rd << ", " << Rn << ", " << Rm << ", " << Ra; |
| 1213 | } |
| 1214 | break; |
| 1215 | case 1: |
| 1216 | case 2: |
| 1217 | case 3: |
| 1218 | case 4: |
| 1219 | case 5: |
| 1220 | case 6: |
| 1221 | break; // do these sometime |
| 1222 | } |
| 1223 | } else if ((op2 >> 3) == 7) { // 0111xxx |
| 1224 | // Long multiply, long multiply accumulate, and divide |
| 1225 | op1 = (instr >> 20) & 0x7; |
| 1226 | op2 = (instr >> 4) & 0xf; |
| 1227 | ArmRegister Rn(instr, 16); |
| 1228 | ArmRegister Rm(instr, 0); |
| 1229 | ArmRegister Rd(instr, 8); |
| 1230 | ArmRegister RdHi(instr, 8); |
| 1231 | ArmRegister RdLo(instr, 12); |
| 1232 | switch (op1) { |
| 1233 | case 0: |
| 1234 | opcode << "smull"; |
| 1235 | args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm; |
| 1236 | break; |
| 1237 | case 1: |
| 1238 | opcode << "sdiv"; |
| 1239 | args << Rd << ", " << Rn << ", " << Rm; |
| 1240 | break; |
| 1241 | case 2: |
| 1242 | opcode << "umull"; |
| 1243 | args << RdLo << ", " << RdHi << ", " << Rn << ", " << Rm; |
| 1244 | break; |
| 1245 | case 3: |
| 1246 | opcode << "udiv"; |
| 1247 | args << Rd << ", " << Rn << ", " << Rm; |
| 1248 | break; |
| 1249 | case 4: |
| 1250 | case 5: |
| 1251 | case 6: |
| 1252 | break; // TODO: when we generate these... |
| 1253 | } |
| 1254 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1255 | } |
| 1256 | default: |
| 1257 | break; |
| 1258 | } |
Ian Rogers | 9af8940 | 2012-09-07 11:29:35 -0700 | [diff] [blame] | 1259 | |
| 1260 | // Apply any IT-block conditions to the opcode if necessary. |
| 1261 | if (!it_conditions_.empty()) { |
| 1262 | opcode << it_conditions_.back(); |
| 1263 | it_conditions_.pop_back(); |
| 1264 | } |
| 1265 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1266 | os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n'; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1267 | return 4; |
Brian Carlstrom | 1895ea3 | 2013-07-18 13:28:37 -0700 | [diff] [blame] | 1268 | } // NOLINT(readability/fn_size) |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1269 | |
| 1270 | size_t DisassemblerArm::DumpThumb16(std::ostream& os, const uint8_t* instr_ptr) { |
| 1271 | uint16_t instr = ReadU16(instr_ptr); |
| 1272 | bool is_32bit = ((instr & 0xF000) == 0xF000) || ((instr & 0xF800) == 0xE800); |
| 1273 | if (is_32bit) { |
| 1274 | return DumpThumb32(os, instr_ptr); |
| 1275 | } else { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1276 | std::ostringstream opcode; |
| 1277 | std::ostringstream args; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1278 | uint16_t opcode1 = instr >> 10; |
| 1279 | if (opcode1 < 0x10) { |
| 1280 | // shift (immediate), add, subtract, move, and compare |
| 1281 | uint16_t opcode2 = instr >> 9; |
| 1282 | switch (opcode2) { |
| 1283 | case 0x0: case 0x1: case 0x2: case 0x3: case 0x4: case 0x5: case 0x6: case 0x7: |
| 1284 | case 0x8: case 0x9: case 0xA: case 0xB: { |
Sebastien Hertz | e78500c | 2013-02-19 14:29:52 +0100 | [diff] [blame] | 1285 | // Logical shift left - 00 000xx iii mmm ddd |
| 1286 | // Logical shift right - 00 001xx iii mmm ddd |
| 1287 | // Arithmetic shift right - 00 010xx iii mmm ddd |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1288 | uint16_t imm5 = (instr >> 6) & 0x1F; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1289 | ThumbRegister rm(instr, 3); |
Sebastien Hertz | e78500c | 2013-02-19 14:29:52 +0100 | [diff] [blame] | 1290 | ThumbRegister Rd(instr, 0); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1291 | if (opcode2 <= 3) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1292 | opcode << "lsls"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1293 | } else if (opcode2 <= 7) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1294 | opcode << "lsrs"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1295 | } else { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1296 | opcode << "asrs"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1297 | } |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1298 | args << Rd << ", " << rm << ", #" << imm5; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1299 | break; |
| 1300 | } |
| 1301 | case 0xC: case 0xD: case 0xE: case 0xF: { |
| 1302 | // Add register - 00 01100 mmm nnn ddd |
| 1303 | // Sub register - 00 01101 mmm nnn ddd |
| 1304 | // Add 3-bit immediate - 00 01110 iii nnn ddd |
| 1305 | // Sub 3-bit immediate - 00 01111 iii nnn ddd |
| 1306 | uint16_t imm3_or_Rm = (instr >> 6) & 7; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1307 | ThumbRegister Rn(instr, 3); |
| 1308 | ThumbRegister Rd(instr, 0); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1309 | if ((opcode2 & 2) != 0 && imm3_or_Rm == 0) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1310 | opcode << "mov"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1311 | } else { |
| 1312 | if ((opcode2 & 1) == 0) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1313 | opcode << "adds"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1314 | } else { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1315 | opcode << "subs"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1316 | } |
| 1317 | } |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1318 | args << Rd << ", " << Rn; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1319 | if ((opcode2 & 2) == 0) { |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1320 | ArmRegister Rm(imm3_or_Rm); |
| 1321 | args << ", " << Rm; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1322 | } else if (imm3_or_Rm != 0) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1323 | args << ", #" << imm3_or_Rm; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1324 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1325 | break; |
| 1326 | } |
| 1327 | case 0x10: case 0x11: case 0x12: case 0x13: |
| 1328 | case 0x14: case 0x15: case 0x16: case 0x17: |
| 1329 | case 0x18: case 0x19: case 0x1A: case 0x1B: |
| 1330 | case 0x1C: case 0x1D: case 0x1E: case 0x1F: { |
| 1331 | // MOVS Rd, #imm8 - 00100 ddd iiiiiiii |
| 1332 | // CMP Rn, #imm8 - 00101 nnn iiiiiiii |
| 1333 | // ADDS Rn, #imm8 - 00110 nnn iiiiiiii |
| 1334 | // SUBS Rn, #imm8 - 00111 nnn iiiiiiii |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1335 | ThumbRegister Rn(instr, 8); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1336 | uint16_t imm8 = instr & 0xFF; |
| 1337 | switch (opcode2 >> 2) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1338 | case 4: opcode << "movs"; break; |
| 1339 | case 5: opcode << "cmp"; break; |
| 1340 | case 6: opcode << "adds"; break; |
| 1341 | case 7: opcode << "subs"; break; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1342 | } |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1343 | args << Rn << ", #" << imm8; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1344 | break; |
| 1345 | } |
| 1346 | default: |
| 1347 | break; |
| 1348 | } |
Ian Rogers | ad03ef5 | 2012-03-18 19:34:47 -0700 | [diff] [blame] | 1349 | } else if (opcode1 == 0x10) { |
| 1350 | // Data-processing |
| 1351 | uint16_t opcode2 = (instr >> 6) & 0xF; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1352 | ThumbRegister rm(instr, 3); |
| 1353 | ThumbRegister rdn(instr, 0); |
Ian Rogers | ad03ef5 | 2012-03-18 19:34:47 -0700 | [diff] [blame] | 1354 | opcode << kThumbDataProcessingOperations[opcode2]; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1355 | args << rdn << ", " << rm; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1356 | } else if (opcode1 == 0x11) { |
| 1357 | // Special data instructions and branch and exchange |
| 1358 | uint16_t opcode2 = (instr >> 6) & 0x0F; |
| 1359 | switch (opcode2) { |
| 1360 | case 0x0: case 0x1: case 0x2: case 0x3: { |
| 1361 | // Add low registers - 010001 0000 xxxxxx |
| 1362 | // Add high registers - 010001 0001/001x xxxxxx |
| 1363 | uint16_t DN = (instr >> 7) & 1; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1364 | ArmRegister rm(instr, 3); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1365 | uint16_t Rdn = instr & 7; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1366 | ArmRegister DN_Rdn((DN << 3) | Rdn); |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1367 | opcode << "add"; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1368 | args << DN_Rdn << ", " << rm; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1369 | break; |
| 1370 | } |
| 1371 | case 0x8: case 0x9: case 0xA: case 0xB: { |
| 1372 | // Move low registers - 010001 1000 xxxxxx |
| 1373 | // Move high registers - 010001 1001/101x xxxxxx |
| 1374 | uint16_t DN = (instr >> 7) & 1; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1375 | ArmRegister rm(instr, 3); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1376 | uint16_t Rdn = instr & 7; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1377 | ArmRegister DN_Rdn((DN << 3) | Rdn); |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1378 | opcode << "mov"; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1379 | args << DN_Rdn << ", " << rm; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1380 | break; |
| 1381 | } |
| 1382 | case 0x5: case 0x6: case 0x7: { |
| 1383 | // Compare high registers - 010001 0101/011x xxxxxx |
| 1384 | uint16_t N = (instr >> 7) & 1; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1385 | ArmRegister rm(instr, 3); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1386 | uint16_t Rn = instr & 7; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1387 | ArmRegister N_Rn((N << 3) | Rn); |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1388 | opcode << "cmp"; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1389 | args << N_Rn << ", " << rm; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1390 | break; |
| 1391 | } |
| 1392 | case 0xC: case 0xD: case 0xE: case 0xF: { |
| 1393 | // Branch and exchange - 010001 110x xxxxxx |
| 1394 | // Branch with link and exchange - 010001 111x xxxxxx |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1395 | ArmRegister rm(instr, 3); |
| 1396 | opcode << ((opcode2 & 0x2) == 0 ? "bx" : "blx"); |
| 1397 | args << rm; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1398 | break; |
| 1399 | } |
| 1400 | default: |
| 1401 | break; |
| 1402 | } |
jeffhao | eae2691 | 2013-01-28 16:29:54 -0800 | [diff] [blame] | 1403 | } else if (opcode1 == 0x12 || opcode1 == 0x13) { // 01001x |
| 1404 | ThumbRegister Rt(instr, 8); |
| 1405 | uint16_t imm8 = instr & 0xFF; |
| 1406 | opcode << "ldr"; |
| 1407 | args << Rt << ", [pc, #" << (imm8 << 2) << "]"; |
Ian Rogers | d83bc36 | 2012-09-07 17:43:13 -0700 | [diff] [blame] | 1408 | } else if ((opcode1 >= 0x14 && opcode1 <= 0x17) || // 0101xx |
| 1409 | (opcode1 >= 0x18 && opcode1 <= 0x1f) || // 011xxx |
| 1410 | (opcode1 >= 0x20 && opcode1 <= 0x27)) { // 100xxx |
| 1411 | // Load/store single data item |
| 1412 | uint16_t opA = (instr >> 12) & 0xF; |
| 1413 | if (opA == 0x5) { |
| 1414 | uint16_t opB = (instr >> 9) & 0x7; |
| 1415 | ThumbRegister Rm(instr, 6); |
| 1416 | ThumbRegister Rn(instr, 3); |
| 1417 | ThumbRegister Rt(instr, 0); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1418 | switch (opB) { |
Ian Rogers | d83bc36 | 2012-09-07 17:43:13 -0700 | [diff] [blame] | 1419 | case 0: opcode << "str"; break; |
| 1420 | case 1: opcode << "strh"; break; |
| 1421 | case 2: opcode << "strb"; break; |
| 1422 | case 3: opcode << "ldrsb"; break; |
| 1423 | case 4: opcode << "ldr"; break; |
| 1424 | case 5: opcode << "ldrh"; break; |
| 1425 | case 6: opcode << "ldrb"; break; |
| 1426 | case 7: opcode << "ldrsh"; break; |
| 1427 | } |
| 1428 | args << Rt << ", [" << Rn << ", " << Rm << "]"; |
| 1429 | } else if (opA == 9) { |
| 1430 | uint16_t opB = (instr >> 11) & 1; |
| 1431 | ThumbRegister Rt(instr, 8); |
| 1432 | uint16_t imm8 = instr & 0xFF; |
| 1433 | opcode << (opB == 0 ? "str" : "ldr"); |
Ian Rogers | 137e88f | 2012-10-08 17:46:47 -0700 | [diff] [blame] | 1434 | args << Rt << ", [sp, #" << (imm8 << 2) << "]"; |
Ian Rogers | d83bc36 | 2012-09-07 17:43:13 -0700 | [diff] [blame] | 1435 | } else { |
| 1436 | uint16_t imm5 = (instr >> 6) & 0x1F; |
| 1437 | uint16_t opB = (instr >> 11) & 1; |
| 1438 | ThumbRegister Rn(instr, 3); |
| 1439 | ThumbRegister Rt(instr, 0); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1440 | switch (opA) { |
Ian Rogers | d83bc36 | 2012-09-07 17:43:13 -0700 | [diff] [blame] | 1441 | case 6: |
| 1442 | imm5 <<= 2; |
| 1443 | opcode << (opB == 0 ? "str" : "ldr"); |
| 1444 | break; |
| 1445 | case 7: |
| 1446 | imm5 <<= 0; |
| 1447 | opcode << (opB == 0 ? "strb" : "ldrb"); |
| 1448 | break; |
| 1449 | case 8: |
| 1450 | imm5 <<= 1; |
| 1451 | opcode << (opB == 0 ? "strh" : "ldrh"); |
| 1452 | break; |
| 1453 | } |
| 1454 | args << Rt << ", [" << Rn << ", #" << imm5 << "]"; |
| 1455 | } |
jeffhao | eae2691 | 2013-01-28 16:29:54 -0800 | [diff] [blame] | 1456 | } else if (opcode1 >= 0x34 && opcode1 <= 0x37) { // 1101xx |
Ian Rogers | 7761cb6 | 2013-06-17 14:10:46 -0700 | [diff] [blame] | 1457 | int8_t imm8 = instr & 0xFF; |
jeffhao | eae2691 | 2013-01-28 16:29:54 -0800 | [diff] [blame] | 1458 | uint32_t cond = (instr >> 8) & 0xF; |
| 1459 | opcode << "b"; |
| 1460 | DumpCond(opcode, cond); |
| 1461 | DumpBranchTarget(args, instr_ptr + 4, (imm8 << 1)); |
Ian Rogers | 9af8940 | 2012-09-07 11:29:35 -0700 | [diff] [blame] | 1462 | } else if ((instr & 0xF800) == 0xA800) { |
| 1463 | // Generate SP-relative address |
| 1464 | ThumbRegister rd(instr, 8); |
| 1465 | int imm8 = instr & 0xFF; |
| 1466 | opcode << "add"; |
| 1467 | args << rd << ", sp, #" << (imm8 << 2); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1468 | } else if ((instr & 0xF000) == 0xB000) { |
| 1469 | // Miscellaneous 16-bit instructions |
| 1470 | uint16_t opcode2 = (instr >> 5) & 0x7F; |
| 1471 | switch (opcode2) { |
| 1472 | case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: { |
| 1473 | // Add immediate to SP - 1011 00000 ii iiiii |
| 1474 | // Subtract immediate from SP - 1011 00001 ii iiiii |
| 1475 | int imm7 = instr & 0x7F; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1476 | opcode << ((opcode2 & 4) == 0 ? "add" : "sub"); |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1477 | args << "sp, sp, #" << (imm7 << 2); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1478 | break; |
| 1479 | } |
Ian Rogers | 087b241 | 2012-03-21 01:30:32 -0700 | [diff] [blame] | 1480 | case 0x08: case 0x09: case 0x0A: case 0x0B: // 0001xxx |
Ian Rogers | ebbc577 | 2012-04-11 17:00:08 -0700 | [diff] [blame] | 1481 | case 0x0C: case 0x0D: case 0x0E: case 0x0F: |
Ian Rogers | 5501913 | 2013-02-08 01:05:23 -0800 | [diff] [blame] | 1482 | case 0x18: case 0x19: case 0x1A: case 0x1B: // 0011xxx |
| 1483 | case 0x1C: case 0x1D: case 0x1E: case 0x1F: |
Ian Rogers | ebbc577 | 2012-04-11 17:00:08 -0700 | [diff] [blame] | 1484 | case 0x48: case 0x49: case 0x4A: case 0x4B: // 1001xxx |
Ian Rogers | 5501913 | 2013-02-08 01:05:23 -0800 | [diff] [blame] | 1485 | case 0x4C: case 0x4D: case 0x4E: case 0x4F: |
| 1486 | case 0x58: case 0x59: case 0x5A: case 0x5B: // 1011xxx |
| 1487 | case 0x5C: case 0x5D: case 0x5E: case 0x5F: { |
Ian Rogers | 087b241 | 2012-03-21 01:30:32 -0700 | [diff] [blame] | 1488 | // CBNZ, CBZ |
| 1489 | uint16_t op = (instr >> 11) & 1; |
| 1490 | uint16_t i = (instr >> 9) & 1; |
| 1491 | uint16_t imm5 = (instr >> 3) & 0x1F; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1492 | ThumbRegister Rn(instr, 0); |
Ian Rogers | 087b241 | 2012-03-21 01:30:32 -0700 | [diff] [blame] | 1493 | opcode << (op != 0 ? "cbnz" : "cbz"); |
Ian Rogers | 828a07f | 2013-06-18 22:27:34 -0700 | [diff] [blame] | 1494 | uint32_t imm32 = (i << 6) | (imm5 << 1); |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1495 | args << Rn << ", "; |
Ian Rogers | 087b241 | 2012-03-21 01:30:32 -0700 | [diff] [blame] | 1496 | DumpBranchTarget(args, instr_ptr + 4, imm32); |
| 1497 | break; |
| 1498 | } |
Vladimir Marko | a8b4caf | 2013-10-24 15:08:57 +0100 | [diff] [blame] | 1499 | case 0x50: case 0x51: // 101000x |
| 1500 | case 0x52: case 0x53: // 101001x |
| 1501 | case 0x56: case 0x57: { // 101011x |
| 1502 | uint16_t op = (instr >> 6) & 3; |
| 1503 | opcode << kThumbReverseOperations[op]; |
| 1504 | ThumbRegister Rm(instr, 3); |
| 1505 | ThumbRegister Rd(instr, 0); |
| 1506 | args << Rd << ", " << Rm; |
| 1507 | break; |
| 1508 | } |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 1509 | case 0x78: case 0x79: case 0x7A: case 0x7B: // 1111xxx |
| 1510 | case 0x7C: case 0x7D: case 0x7E: case 0x7F: { |
| 1511 | // If-Then, and hints |
| 1512 | uint16_t opA = (instr >> 4) & 0xF; |
| 1513 | uint16_t opB = instr & 0xF; |
| 1514 | if (opB == 0) { |
| 1515 | switch (opA) { |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1516 | case 0: opcode << "nop"; break; |
| 1517 | case 1: opcode << "yield"; break; |
| 1518 | case 2: opcode << "wfe"; break; |
| 1519 | case 3: opcode << "sev"; break; |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 1520 | default: break; |
| 1521 | } |
| 1522 | } else { |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 1523 | uint32_t first_cond = opA; |
| 1524 | uint32_t mask = opB; |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1525 | opcode << "it"; |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 1526 | |
| 1527 | // Flesh out the base "it" opcode with the specific collection of 't's and 'e's, |
| 1528 | // and store up the actual condition codes we'll want to add to the next few opcodes. |
| 1529 | size_t count = 3 - CTZ(mask); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1530 | it_conditions_.resize(count + 2); // Plus the implicit 't', plus the "" for the IT itself. |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 1531 | for (size_t i = 0; i < count; ++i) { |
| 1532 | bool positive_cond = ((first_cond & 1) != 0); |
| 1533 | bool positive_mask = ((mask & (1 << (3 - i))) != 0); |
| 1534 | if (positive_mask == positive_cond) { |
| 1535 | opcode << 't'; |
| 1536 | it_conditions_[i] = kConditionCodeNames[first_cond]; |
| 1537 | } else { |
| 1538 | opcode << 'e'; |
| 1539 | it_conditions_[i] = kConditionCodeNames[first_cond ^ 1]; |
| 1540 | } |
| 1541 | } |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1542 | it_conditions_[count] = kConditionCodeNames[first_cond]; // The implicit 't'. |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 1543 | |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1544 | it_conditions_[count + 1] = ""; // No condition code for the IT itself... |
| 1545 | DumpCond(args, first_cond); // ...because it's considered an argument. |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 1546 | } |
| 1547 | break; |
| 1548 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1549 | default: |
| 1550 | break; |
| 1551 | } |
| 1552 | } else if (((instr & 0xF000) == 0x5000) || ((instr & 0xE000) == 0x6000) || |
| 1553 | ((instr & 0xE000) == 0x8000)) { |
| 1554 | // Load/store single data item |
| 1555 | uint16_t opA = instr >> 12; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 1556 | // uint16_t opB = (instr >> 9) & 7; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1557 | switch (opA) { |
| 1558 | case 0x6: { |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1559 | // STR Rt, [Rn, #imm] - 01100 iiiii nnn ttt |
| 1560 | // LDR Rt, [Rn, #imm] - 01101 iiiii nnn ttt |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1561 | uint16_t imm5 = (instr >> 6) & 0x1F; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1562 | ThumbRegister Rn(instr, 3); |
Elliott Hughes | 28fa76d | 2012-04-09 17:31:46 -0700 | [diff] [blame] | 1563 | ThumbRegister Rt(instr, 0); |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1564 | opcode << ((instr & 0x800) == 0 ? "str" : "ldr"); |
| 1565 | args << Rt << ", [" << Rn << ", #" << (imm5 << 2) << "]"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1566 | break; |
| 1567 | } |
| 1568 | case 0x9: { |
| 1569 | // STR Rt, [SP, #imm] - 01100 ttt iiiiiiii |
| 1570 | // LDR Rt, [SP, #imm] - 01101 ttt iiiiiiii |
| 1571 | uint16_t imm8 = instr & 0xFF; |
Elliott Hughes | 630e77d | 2012-03-22 19:20:56 -0700 | [diff] [blame] | 1572 | ThumbRegister Rt(instr, 8); |
| 1573 | opcode << ((instr & 0x800) == 0 ? "str" : "ldr"); |
| 1574 | args << Rt << ", [sp, #" << (imm8 << 2) << "]"; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1575 | break; |
| 1576 | } |
| 1577 | default: |
| 1578 | break; |
| 1579 | } |
Ian Rogers | 40627db | 2012-03-04 17:31:09 -0800 | [diff] [blame] | 1580 | } else if (opcode1 == 0x38 || opcode1 == 0x39) { |
| 1581 | uint16_t imm11 = instr & 0x7FFF; |
| 1582 | int32_t imm32 = imm11 << 1; |
| 1583 | imm32 = (imm32 << 20) >> 20; // sign extend 12 bit immediate |
Elliott Hughes | cbf0b61 | 2012-03-15 16:23:47 -0700 | [diff] [blame] | 1584 | opcode << "b"; |
| 1585 | DumpBranchTarget(args, instr_ptr + 4, imm32); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1586 | } |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 1587 | |
| 1588 | // Apply any IT-block conditions to the opcode if necessary. |
| 1589 | if (!it_conditions_.empty()) { |
| 1590 | opcode << it_conditions_.back(); |
| 1591 | it_conditions_.pop_back(); |
| 1592 | } |
| 1593 | |
Ian Rogers | 2bcb4a4 | 2012-11-08 10:39:18 -0800 | [diff] [blame] | 1594 | os << StringPrintf("%p: %04x \t%-7s ", instr_ptr, instr, opcode.str().c_str()) << args.str() << '\n'; |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1595 | } |
| 1596 | return 2; |
| 1597 | } |
| 1598 | |
| 1599 | } // namespace arm |
| 1600 | } // namespace art |