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