Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1 | //===- MipsDisassembler.cpp - Disassembler for Mips -------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is part of the Mips Disassembler. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Mips.h" |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 15 | #include "MipsRegisterInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "MipsSubtarget.h" |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCContext.h" |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCDisassembler.h" |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCFixedLenDisassembler.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCInst.h" |
| 21 | #include "llvm/MC/MCSubtargetInfo.h" |
| 22 | #include "llvm/Support/MathExtras.h" |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 23 | #include "llvm/Support/TargetRegistry.h" |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 24 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "mips-disassembler" |
| 28 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 29 | typedef MCDisassembler::DecodeStatus DecodeStatus; |
| 30 | |
Benjamin Kramer | cb3e98c | 2012-05-01 14:34:24 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 33 | /// A disasembler class for Mips. |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 34 | class MipsDisassemblerBase : public MCDisassembler { |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 35 | public: |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 36 | MipsDisassemblerBase(const MCSubtargetInfo &STI, MCContext &Ctx, |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 37 | bool IsBigEndian) |
| 38 | : MCDisassembler(STI, Ctx), |
| 39 | IsN64(STI.getFeatureBits() & Mips::FeatureN64), |
| 40 | IsBigEndian(IsBigEndian) {} |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 41 | |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 42 | virtual ~MipsDisassemblerBase() {} |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 43 | |
Akira Hatanaka | 9bfa2e2 | 2013-08-28 00:55:15 +0000 | [diff] [blame] | 44 | bool isN64() const { return IsN64; } |
| 45 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 46 | private: |
Akira Hatanaka | 9bfa2e2 | 2013-08-28 00:55:15 +0000 | [diff] [blame] | 47 | bool IsN64; |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 48 | protected: |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 49 | bool IsBigEndian; |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 52 | /// A disasembler class for Mips32. |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 53 | class MipsDisassembler : public MipsDisassemblerBase { |
Vladimir Medic | dde3d58 | 2013-09-06 12:30:36 +0000 | [diff] [blame] | 54 | bool IsMicroMips; |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 55 | public: |
Daniel Sanders | c171f65 | 2014-06-13 13:15:59 +0000 | [diff] [blame] | 56 | MipsDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx, bool bigEndian) |
| 57 | : MipsDisassemblerBase(STI, Ctx, bigEndian) { |
| 58 | IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips; |
| 59 | } |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 60 | |
Daniel Sanders | c171f65 | 2014-06-13 13:15:59 +0000 | [diff] [blame] | 61 | bool hasMips3() const { return STI.getFeatureBits() & Mips::FeatureMips3; } |
| 62 | bool hasMips32() const { return STI.getFeatureBits() & Mips::FeatureMips32; } |
| 63 | bool hasMips32r6() const { |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 64 | return STI.getFeatureBits() & Mips::FeatureMips32r6; |
| 65 | } |
| 66 | |
Daniel Sanders | 0fa6041 | 2014-06-12 13:39:06 +0000 | [diff] [blame] | 67 | bool isGP64() const { return STI.getFeatureBits() & Mips::FeatureGP64Bit; } |
| 68 | |
Daniel Sanders | c171f65 | 2014-06-13 13:15:59 +0000 | [diff] [blame] | 69 | bool hasCOP3() const { |
| 70 | // Only present in MIPS-I and MIPS-II |
| 71 | return !hasMips32() && !hasMips3(); |
| 72 | } |
| 73 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 74 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 75 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 76 | raw_ostream &VStream, |
| 77 | raw_ostream &CStream) const override; |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 80 | /// A disasembler class for Mips64. |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 81 | class Mips64Disassembler : public MipsDisassemblerBase { |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 82 | public: |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 83 | Mips64Disassembler(const MCSubtargetInfo &STI, MCContext &Ctx, |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 84 | bool bigEndian) : |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 85 | MipsDisassemblerBase(STI, Ctx, bigEndian) {} |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 86 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 87 | DecodeStatus getInstruction(MCInst &Instr, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 88 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 89 | raw_ostream &VStream, |
| 90 | raw_ostream &CStream) const override; |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Benjamin Kramer | cb3e98c | 2012-05-01 14:34:24 +0000 | [diff] [blame] | 93 | } // end anonymous namespace |
| 94 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 95 | // Forward declare these because the autogenerated code will reference them. |
| 96 | // Definitions are further down. |
Akira Hatanaka | 13e6ccf | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 97 | static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst, |
| 98 | unsigned RegNo, |
| 99 | uint64_t Address, |
| 100 | const void *Decoder); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 101 | |
Reed Kotler | ec8a549 | 2013-02-14 03:05:25 +0000 | [diff] [blame] | 102 | static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst, |
| 103 | unsigned RegNo, |
| 104 | uint64_t Address, |
| 105 | const void *Decoder); |
| 106 | |
Zoran Jovanovic | b0852e5 | 2014-10-21 08:23:11 +0000 | [diff] [blame] | 107 | static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst, |
| 108 | unsigned RegNo, |
| 109 | uint64_t Address, |
| 110 | const void *Decoder); |
| 111 | |
Akira Hatanaka | 13e6ccf | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 112 | static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, |
| 113 | unsigned RegNo, |
| 114 | uint64_t Address, |
| 115 | const void *Decoder); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 116 | |
Akira Hatanaka | 9bfa2e2 | 2013-08-28 00:55:15 +0000 | [diff] [blame] | 117 | static DecodeStatus DecodePtrRegisterClass(MCInst &Inst, |
| 118 | unsigned Insn, |
| 119 | uint64_t Address, |
| 120 | const void *Decoder); |
| 121 | |
Akira Hatanaka | 654655f | 2013-08-14 00:53:38 +0000 | [diff] [blame] | 122 | static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst, |
| 123 | unsigned RegNo, |
| 124 | uint64_t Address, |
| 125 | const void *Decoder); |
Akira Hatanaka | ecabd1a | 2012-09-27 02:01:10 +0000 | [diff] [blame] | 126 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 127 | static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst, |
| 128 | unsigned RegNo, |
| 129 | uint64_t Address, |
| 130 | const void *Decoder); |
| 131 | |
| 132 | static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst, |
| 133 | unsigned RegNo, |
| 134 | uint64_t Address, |
| 135 | const void *Decoder); |
| 136 | |
| 137 | static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst, |
| 138 | unsigned RegNo, |
| 139 | uint64_t Address, |
| 140 | const void *Decoder); |
| 141 | |
Akira Hatanaka | 1fb1b8b | 2013-07-26 20:13:47 +0000 | [diff] [blame] | 142 | static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst, |
| 143 | unsigned RegNo, |
| 144 | uint64_t Address, |
| 145 | const void *Decoder); |
| 146 | |
Daniel Sanders | 0fa6041 | 2014-06-12 13:39:06 +0000 | [diff] [blame] | 147 | static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo, |
| 148 | uint64_t Address, |
| 149 | const void *Decoder); |
| 150 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 151 | static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst, |
| 152 | unsigned Insn, |
| 153 | uint64_t Address, |
| 154 | const void *Decoder); |
| 155 | |
| 156 | static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst, |
| 157 | unsigned RegNo, |
| 158 | uint64_t Address, |
| 159 | const void *Decoder); |
| 160 | |
Akira Hatanaka | 00fcf2e | 2013-08-08 21:54:26 +0000 | [diff] [blame] | 161 | static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst, |
| 162 | unsigned RegNo, |
| 163 | uint64_t Address, |
| 164 | const void *Decoder); |
Akira Hatanaka | ecabd1a | 2012-09-27 02:01:10 +0000 | [diff] [blame] | 165 | |
Akira Hatanaka | 8002a3f | 2013-08-14 00:47:08 +0000 | [diff] [blame] | 166 | static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst, |
| 167 | unsigned RegNo, |
| 168 | uint64_t Address, |
| 169 | const void *Decoder); |
Akira Hatanaka | 59bfaf7 | 2013-04-18 00:52:44 +0000 | [diff] [blame] | 170 | |
Akira Hatanaka | 8002a3f | 2013-08-14 00:47:08 +0000 | [diff] [blame] | 171 | static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst, |
| 172 | unsigned RegNo, |
| 173 | uint64_t Address, |
| 174 | const void *Decoder); |
Akira Hatanaka | 59bfaf7 | 2013-04-18 00:52:44 +0000 | [diff] [blame] | 175 | |
Jack Carter | 3eb663b | 2013-09-26 00:09:46 +0000 | [diff] [blame] | 176 | static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst, |
| 177 | unsigned RegNo, |
| 178 | uint64_t Address, |
| 179 | const void *Decoder); |
| 180 | |
Jack Carter | 5dc8ac9 | 2013-09-25 23:50:44 +0000 | [diff] [blame] | 181 | static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst, |
| 182 | unsigned RegNo, |
| 183 | uint64_t Address, |
| 184 | const void *Decoder); |
| 185 | |
| 186 | static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst, |
| 187 | unsigned RegNo, |
| 188 | uint64_t Address, |
| 189 | const void *Decoder); |
| 190 | |
| 191 | static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst, |
| 192 | unsigned RegNo, |
| 193 | uint64_t Address, |
| 194 | const void *Decoder); |
| 195 | |
Matheus Almeida | a591fdc | 2013-10-21 12:26:50 +0000 | [diff] [blame] | 196 | static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst, |
| 197 | unsigned RegNo, |
| 198 | uint64_t Address, |
| 199 | const void *Decoder); |
| 200 | |
Daniel Sanders | 2a83d68 | 2014-05-21 12:56:39 +0000 | [diff] [blame] | 201 | static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst, |
| 202 | unsigned RegNo, |
| 203 | uint64_t Address, |
| 204 | const void *Decoder); |
| 205 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 206 | static DecodeStatus DecodeBranchTarget(MCInst &Inst, |
| 207 | unsigned Offset, |
| 208 | uint64_t Address, |
| 209 | const void *Decoder); |
| 210 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 211 | static DecodeStatus DecodeJumpTarget(MCInst &Inst, |
| 212 | unsigned Insn, |
| 213 | uint64_t Address, |
| 214 | const void *Decoder); |
| 215 | |
Zoran Jovanovic | 3c8869d | 2014-05-16 11:03:45 +0000 | [diff] [blame] | 216 | static DecodeStatus DecodeBranchTarget21(MCInst &Inst, |
| 217 | unsigned Offset, |
| 218 | uint64_t Address, |
| 219 | const void *Decoder); |
| 220 | |
| 221 | static DecodeStatus DecodeBranchTarget26(MCInst &Inst, |
| 222 | unsigned Offset, |
| 223 | uint64_t Address, |
| 224 | const void *Decoder); |
| 225 | |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 226 | // DecodeBranchTargetMM - Decode microMIPS branch offset, which is |
| 227 | // shifted left by 1 bit. |
| 228 | static DecodeStatus DecodeBranchTargetMM(MCInst &Inst, |
| 229 | unsigned Offset, |
| 230 | uint64_t Address, |
| 231 | const void *Decoder); |
| 232 | |
Zoran Jovanovic | 507e084 | 2013-10-29 16:38:59 +0000 | [diff] [blame] | 233 | // DecodeJumpTargetMM - Decode microMIPS jump target, which is |
| 234 | // shifted left by 1 bit. |
| 235 | static DecodeStatus DecodeJumpTargetMM(MCInst &Inst, |
| 236 | unsigned Insn, |
| 237 | uint64_t Address, |
| 238 | const void *Decoder); |
| 239 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 240 | static DecodeStatus DecodeMem(MCInst &Inst, |
| 241 | unsigned Insn, |
| 242 | uint64_t Address, |
| 243 | const void *Decoder); |
| 244 | |
Daniel Sanders | 92db6b7 | 2014-10-01 08:26:55 +0000 | [diff] [blame] | 245 | static DecodeStatus DecodeCacheOp(MCInst &Inst, |
| 246 | unsigned Insn, |
| 247 | uint64_t Address, |
| 248 | const void *Decoder); |
| 249 | |
Matheus Almeida | fe0bf9f | 2013-10-21 13:07:13 +0000 | [diff] [blame] | 250 | static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn, |
| 251 | uint64_t Address, const void *Decoder); |
| 252 | |
Vladimir Medic | dde3d58 | 2013-09-06 12:30:36 +0000 | [diff] [blame] | 253 | static DecodeStatus DecodeMemMMImm12(MCInst &Inst, |
| 254 | unsigned Insn, |
| 255 | uint64_t Address, |
| 256 | const void *Decoder); |
| 257 | |
| 258 | static DecodeStatus DecodeMemMMImm16(MCInst &Inst, |
| 259 | unsigned Insn, |
| 260 | uint64_t Address, |
| 261 | const void *Decoder); |
| 262 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 263 | static DecodeStatus DecodeFMem(MCInst &Inst, unsigned Insn, |
| 264 | uint64_t Address, |
| 265 | const void *Decoder); |
| 266 | |
Daniel Sanders | 92db6b7 | 2014-10-01 08:26:55 +0000 | [diff] [blame] | 267 | static DecodeStatus DecodeFMem2(MCInst &Inst, unsigned Insn, |
| 268 | uint64_t Address, |
| 269 | const void *Decoder); |
| 270 | |
| 271 | static DecodeStatus DecodeFMem3(MCInst &Inst, unsigned Insn, |
| 272 | uint64_t Address, |
| 273 | const void *Decoder); |
| 274 | |
Daniel Sanders | 6a803f6 | 2014-06-16 13:13:03 +0000 | [diff] [blame] | 275 | static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst, |
| 276 | unsigned Insn, |
| 277 | uint64_t Address, |
| 278 | const void *Decoder); |
| 279 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 280 | static DecodeStatus DecodeSimm16(MCInst &Inst, |
| 281 | unsigned Insn, |
| 282 | uint64_t Address, |
| 283 | const void *Decoder); |
| 284 | |
Matheus Almeida | 779c593 | 2013-11-18 12:32:49 +0000 | [diff] [blame] | 285 | // Decode the immediate field of an LSA instruction which |
| 286 | // is off by one. |
| 287 | static DecodeStatus DecodeLSAImm(MCInst &Inst, |
| 288 | unsigned Insn, |
| 289 | uint64_t Address, |
| 290 | const void *Decoder); |
| 291 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 292 | static DecodeStatus DecodeInsSize(MCInst &Inst, |
| 293 | unsigned Insn, |
| 294 | uint64_t Address, |
| 295 | const void *Decoder); |
| 296 | |
| 297 | static DecodeStatus DecodeExtSize(MCInst &Inst, |
| 298 | unsigned Insn, |
| 299 | uint64_t Address, |
| 300 | const void *Decoder); |
| 301 | |
Daniel Sanders | b59e1a4 | 2014-05-15 10:45:58 +0000 | [diff] [blame] | 302 | static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn, |
| 303 | uint64_t Address, const void *Decoder); |
| 304 | |
Zoran Jovanovic | 2855142 | 2014-06-09 09:49:51 +0000 | [diff] [blame] | 305 | static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn, |
| 306 | uint64_t Address, const void *Decoder); |
| 307 | |
Daniel Sanders | b50ccf8 | 2014-04-01 10:35:28 +0000 | [diff] [blame] | 308 | /// INSVE_[BHWD] have an implicit operand that the generated decoder doesn't |
| 309 | /// handle. |
| 310 | template <typename InsnType> |
| 311 | static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address, |
| 312 | const void *Decoder); |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 313 | |
| 314 | template <typename InsnType> |
| 315 | static DecodeStatus |
| 316 | DecodeAddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address, |
| 317 | const void *Decoder); |
| 318 | |
| 319 | template <typename InsnType> |
| 320 | static DecodeStatus |
| 321 | DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, uint64_t Address, |
| 322 | const void *Decoder); |
| 323 | |
| 324 | template <typename InsnType> |
| 325 | static DecodeStatus |
| 326 | DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address, |
| 327 | const void *Decoder); |
| 328 | |
| 329 | template <typename InsnType> |
| 330 | static DecodeStatus |
| 331 | DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, uint64_t Address, |
| 332 | const void *Decoder); |
| 333 | |
| 334 | template <typename InsnType> |
| 335 | static DecodeStatus |
| 336 | DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, uint64_t Address, |
| 337 | const void *Decoder); |
| 338 | |
Zoran Jovanovic | 28a0ca0 | 2014-06-12 11:47:44 +0000 | [diff] [blame] | 339 | template <typename InsnType> |
| 340 | static DecodeStatus |
| 341 | DecodeBlezGroupBranch(MCInst &MI, InsnType insn, uint64_t Address, |
| 342 | const void *Decoder); |
| 343 | |
Zoran Jovanovic | a4c4b5f | 2014-11-19 16:44:02 +0000 | [diff] [blame] | 344 | static DecodeStatus DecodeRegListOperand(MCInst &Inst, unsigned Insn, |
| 345 | uint64_t Address, |
| 346 | const void *Decoder); |
| 347 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 348 | namespace llvm { |
| 349 | extern Target TheMipselTarget, TheMipsTarget, TheMips64Target, |
| 350 | TheMips64elTarget; |
| 351 | } |
| 352 | |
| 353 | static MCDisassembler *createMipsDisassembler( |
| 354 | const Target &T, |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 355 | const MCSubtargetInfo &STI, |
| 356 | MCContext &Ctx) { |
| 357 | return new MipsDisassembler(STI, Ctx, true); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | static MCDisassembler *createMipselDisassembler( |
| 361 | const Target &T, |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 362 | const MCSubtargetInfo &STI, |
| 363 | MCContext &Ctx) { |
| 364 | return new MipsDisassembler(STI, Ctx, false); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | static MCDisassembler *createMips64Disassembler( |
| 368 | const Target &T, |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 369 | const MCSubtargetInfo &STI, |
| 370 | MCContext &Ctx) { |
| 371 | return new Mips64Disassembler(STI, Ctx, true); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static MCDisassembler *createMips64elDisassembler( |
| 375 | const Target &T, |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 376 | const MCSubtargetInfo &STI, |
| 377 | MCContext &Ctx) { |
| 378 | return new Mips64Disassembler(STI, Ctx, false); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | extern "C" void LLVMInitializeMipsDisassembler() { |
| 382 | // Register the disassembler. |
| 383 | TargetRegistry::RegisterMCDisassembler(TheMipsTarget, |
| 384 | createMipsDisassembler); |
| 385 | TargetRegistry::RegisterMCDisassembler(TheMipselTarget, |
| 386 | createMipselDisassembler); |
| 387 | TargetRegistry::RegisterMCDisassembler(TheMips64Target, |
| 388 | createMips64Disassembler); |
| 389 | TargetRegistry::RegisterMCDisassembler(TheMips64elTarget, |
| 390 | createMips64elDisassembler); |
| 391 | } |
| 392 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 393 | #include "MipsGenDisassemblerTables.inc" |
| 394 | |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 395 | static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) { |
| 396 | const MipsDisassemblerBase *Dis = static_cast<const MipsDisassemblerBase*>(D); |
| 397 | const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo(); |
| 398 | return *(RegInfo->getRegClass(RC).begin() + RegNo); |
| 399 | } |
| 400 | |
Daniel Sanders | b50ccf8 | 2014-04-01 10:35:28 +0000 | [diff] [blame] | 401 | template <typename InsnType> |
| 402 | static DecodeStatus DecodeINSVE_DF(MCInst &MI, InsnType insn, uint64_t Address, |
| 403 | const void *Decoder) { |
| 404 | typedef DecodeStatus (*DecodeFN)(MCInst &, unsigned, uint64_t, const void *); |
| 405 | // The size of the n field depends on the element size |
| 406 | // The register class also depends on this. |
| 407 | InsnType tmp = fieldFromInstruction(insn, 17, 5); |
| 408 | unsigned NSize = 0; |
| 409 | DecodeFN RegDecoder = nullptr; |
| 410 | if ((tmp & 0x18) == 0x00) { // INSVE_B |
| 411 | NSize = 4; |
| 412 | RegDecoder = DecodeMSA128BRegisterClass; |
| 413 | } else if ((tmp & 0x1c) == 0x10) { // INSVE_H |
| 414 | NSize = 3; |
| 415 | RegDecoder = DecodeMSA128HRegisterClass; |
| 416 | } else if ((tmp & 0x1e) == 0x18) { // INSVE_W |
| 417 | NSize = 2; |
| 418 | RegDecoder = DecodeMSA128WRegisterClass; |
| 419 | } else if ((tmp & 0x1f) == 0x1c) { // INSVE_D |
| 420 | NSize = 1; |
| 421 | RegDecoder = DecodeMSA128DRegisterClass; |
| 422 | } else |
| 423 | llvm_unreachable("Invalid encoding"); |
| 424 | |
| 425 | assert(NSize != 0 && RegDecoder != nullptr); |
| 426 | |
| 427 | // $wd |
| 428 | tmp = fieldFromInstruction(insn, 6, 5); |
| 429 | if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail) |
| 430 | return MCDisassembler::Fail; |
| 431 | // $wd_in |
| 432 | if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail) |
| 433 | return MCDisassembler::Fail; |
| 434 | // $n |
| 435 | tmp = fieldFromInstruction(insn, 16, NSize); |
| 436 | MI.addOperand(MCOperand::CreateImm(tmp)); |
| 437 | // $ws |
| 438 | tmp = fieldFromInstruction(insn, 11, 5); |
| 439 | if (RegDecoder(MI, tmp, Address, Decoder) == MCDisassembler::Fail) |
| 440 | return MCDisassembler::Fail; |
| 441 | // $n2 |
| 442 | MI.addOperand(MCOperand::CreateImm(0)); |
| 443 | |
| 444 | return MCDisassembler::Success; |
| 445 | } |
| 446 | |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 447 | template <typename InsnType> |
| 448 | static DecodeStatus DecodeAddiGroupBranch(MCInst &MI, InsnType insn, |
| 449 | uint64_t Address, |
| 450 | const void *Decoder) { |
| 451 | // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled |
| 452 | // (otherwise we would have matched the ADDI instruction from the earlier |
| 453 | // ISA's instead). |
| 454 | // |
| 455 | // We have: |
| 456 | // 0b001000 sssss ttttt iiiiiiiiiiiiiiii |
| 457 | // BOVC if rs >= rt |
| 458 | // BEQZALC if rs == 0 && rt != 0 |
| 459 | // BEQC if rs < rt && rs != 0 |
| 460 | |
| 461 | InsnType Rs = fieldFromInstruction(insn, 21, 5); |
| 462 | InsnType Rt = fieldFromInstruction(insn, 16, 5); |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 463 | InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4; |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 464 | bool HasRs = false; |
| 465 | |
| 466 | if (Rs >= Rt) { |
| 467 | MI.setOpcode(Mips::BOVC); |
| 468 | HasRs = true; |
| 469 | } else if (Rs != 0 && Rs < Rt) { |
| 470 | MI.setOpcode(Mips::BEQC); |
| 471 | HasRs = true; |
| 472 | } else |
| 473 | MI.setOpcode(Mips::BEQZALC); |
| 474 | |
| 475 | if (HasRs) |
| 476 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 477 | Rs))); |
| 478 | |
| 479 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 480 | Rt))); |
| 481 | MI.addOperand(MCOperand::CreateImm(Imm)); |
| 482 | |
| 483 | return MCDisassembler::Success; |
| 484 | } |
| 485 | |
| 486 | template <typename InsnType> |
| 487 | static DecodeStatus DecodeDaddiGroupBranch(MCInst &MI, InsnType insn, |
| 488 | uint64_t Address, |
| 489 | const void *Decoder) { |
| 490 | // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled |
| 491 | // (otherwise we would have matched the ADDI instruction from the earlier |
| 492 | // ISA's instead). |
| 493 | // |
| 494 | // We have: |
| 495 | // 0b011000 sssss ttttt iiiiiiiiiiiiiiii |
| 496 | // BNVC if rs >= rt |
| 497 | // BNEZALC if rs == 0 && rt != 0 |
| 498 | // BNEC if rs < rt && rs != 0 |
| 499 | |
| 500 | InsnType Rs = fieldFromInstruction(insn, 21, 5); |
| 501 | InsnType Rt = fieldFromInstruction(insn, 16, 5); |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 502 | InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4; |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 503 | bool HasRs = false; |
| 504 | |
| 505 | if (Rs >= Rt) { |
| 506 | MI.setOpcode(Mips::BNVC); |
| 507 | HasRs = true; |
| 508 | } else if (Rs != 0 && Rs < Rt) { |
| 509 | MI.setOpcode(Mips::BNEC); |
| 510 | HasRs = true; |
| 511 | } else |
| 512 | MI.setOpcode(Mips::BNEZALC); |
| 513 | |
| 514 | if (HasRs) |
| 515 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 516 | Rs))); |
| 517 | |
| 518 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 519 | Rt))); |
| 520 | MI.addOperand(MCOperand::CreateImm(Imm)); |
| 521 | |
| 522 | return MCDisassembler::Success; |
| 523 | } |
| 524 | |
| 525 | template <typename InsnType> |
| 526 | static DecodeStatus DecodeBlezlGroupBranch(MCInst &MI, InsnType insn, |
| 527 | uint64_t Address, |
| 528 | const void *Decoder) { |
| 529 | // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled |
| 530 | // (otherwise we would have matched the BLEZL instruction from the earlier |
| 531 | // ISA's instead). |
| 532 | // |
| 533 | // We have: |
| 534 | // 0b010110 sssss ttttt iiiiiiiiiiiiiiii |
| 535 | // Invalid if rs == 0 |
| 536 | // BLEZC if rs == 0 && rt != 0 |
| 537 | // BGEZC if rs == rt && rt != 0 |
| 538 | // BGEC if rs != rt && rs != 0 && rt != 0 |
| 539 | |
| 540 | InsnType Rs = fieldFromInstruction(insn, 21, 5); |
| 541 | InsnType Rt = fieldFromInstruction(insn, 16, 5); |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 542 | InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4; |
Zoran Jovanovic | 28a0ca0 | 2014-06-12 11:47:44 +0000 | [diff] [blame] | 543 | bool HasRs = false; |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 544 | |
| 545 | if (Rt == 0) |
| 546 | return MCDisassembler::Fail; |
| 547 | else if (Rs == 0) |
| 548 | MI.setOpcode(Mips::BLEZC); |
| 549 | else if (Rs == Rt) |
| 550 | MI.setOpcode(Mips::BGEZC); |
Zoran Jovanovic | 28a0ca0 | 2014-06-12 11:47:44 +0000 | [diff] [blame] | 551 | else { |
| 552 | HasRs = true; |
| 553 | MI.setOpcode(Mips::BGEC); |
| 554 | } |
| 555 | |
| 556 | if (HasRs) |
| 557 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 558 | Rs))); |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 559 | |
| 560 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 561 | Rt))); |
| 562 | |
| 563 | MI.addOperand(MCOperand::CreateImm(Imm)); |
| 564 | |
| 565 | return MCDisassembler::Success; |
| 566 | } |
| 567 | |
| 568 | template <typename InsnType> |
| 569 | static DecodeStatus DecodeBgtzlGroupBranch(MCInst &MI, InsnType insn, |
| 570 | uint64_t Address, |
| 571 | const void *Decoder) { |
| 572 | // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled |
| 573 | // (otherwise we would have matched the BGTZL instruction from the earlier |
| 574 | // ISA's instead). |
| 575 | // |
| 576 | // We have: |
| 577 | // 0b010111 sssss ttttt iiiiiiiiiiiiiiii |
| 578 | // Invalid if rs == 0 |
| 579 | // BGTZC if rs == 0 && rt != 0 |
| 580 | // BLTZC if rs == rt && rt != 0 |
| 581 | // BLTC if rs != rt && rs != 0 && rt != 0 |
| 582 | |
Zoran Jovanovic | 5c14b06 | 2014-06-18 14:36:00 +0000 | [diff] [blame] | 583 | bool HasRs = false; |
| 584 | |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 585 | InsnType Rs = fieldFromInstruction(insn, 21, 5); |
| 586 | InsnType Rt = fieldFromInstruction(insn, 16, 5); |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 587 | InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4; |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 588 | |
| 589 | if (Rt == 0) |
| 590 | return MCDisassembler::Fail; |
| 591 | else if (Rs == 0) |
| 592 | MI.setOpcode(Mips::BGTZC); |
| 593 | else if (Rs == Rt) |
| 594 | MI.setOpcode(Mips::BLTZC); |
Zoran Jovanovic | 5c14b06 | 2014-06-18 14:36:00 +0000 | [diff] [blame] | 595 | else { |
| 596 | MI.setOpcode(Mips::BLTC); |
| 597 | HasRs = true; |
| 598 | } |
| 599 | |
| 600 | if (HasRs) |
| 601 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 602 | Rs))); |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 603 | |
| 604 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 605 | Rt))); |
| 606 | |
| 607 | MI.addOperand(MCOperand::CreateImm(Imm)); |
| 608 | |
| 609 | return MCDisassembler::Success; |
| 610 | } |
| 611 | |
| 612 | template <typename InsnType> |
| 613 | static DecodeStatus DecodeBgtzGroupBranch(MCInst &MI, InsnType insn, |
| 614 | uint64_t Address, |
| 615 | const void *Decoder) { |
| 616 | // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled |
| 617 | // (otherwise we would have matched the BGTZ instruction from the earlier |
| 618 | // ISA's instead). |
| 619 | // |
| 620 | // We have: |
| 621 | // 0b000111 sssss ttttt iiiiiiiiiiiiiiii |
| 622 | // BGTZ if rt == 0 |
| 623 | // BGTZALC if rs == 0 && rt != 0 |
| 624 | // BLTZALC if rs != 0 && rs == rt |
| 625 | // BLTUC if rs != 0 && rs != rt |
| 626 | |
| 627 | InsnType Rs = fieldFromInstruction(insn, 21, 5); |
| 628 | InsnType Rt = fieldFromInstruction(insn, 16, 5); |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 629 | InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4; |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 630 | bool HasRs = false; |
| 631 | bool HasRt = false; |
| 632 | |
| 633 | if (Rt == 0) { |
| 634 | MI.setOpcode(Mips::BGTZ); |
| 635 | HasRs = true; |
| 636 | } else if (Rs == 0) { |
| 637 | MI.setOpcode(Mips::BGTZALC); |
| 638 | HasRt = true; |
| 639 | } else if (Rs == Rt) { |
| 640 | MI.setOpcode(Mips::BLTZALC); |
| 641 | HasRs = true; |
Zoran Jovanovic | 5c14b06 | 2014-06-18 14:36:00 +0000 | [diff] [blame] | 642 | } else { |
| 643 | MI.setOpcode(Mips::BLTUC); |
| 644 | HasRs = true; |
| 645 | HasRt = true; |
| 646 | } |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 647 | |
| 648 | if (HasRs) |
| 649 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 650 | Rs))); |
| 651 | |
| 652 | if (HasRt) |
| 653 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 654 | Rt))); |
| 655 | |
| 656 | MI.addOperand(MCOperand::CreateImm(Imm)); |
| 657 | |
| 658 | return MCDisassembler::Success; |
| 659 | } |
| 660 | |
Zoran Jovanovic | 28a0ca0 | 2014-06-12 11:47:44 +0000 | [diff] [blame] | 661 | template <typename InsnType> |
| 662 | static DecodeStatus DecodeBlezGroupBranch(MCInst &MI, InsnType insn, |
| 663 | uint64_t Address, |
| 664 | const void *Decoder) { |
| 665 | // If we are called then we can assume that MIPS32r6/MIPS64r6 is enabled |
| 666 | // (otherwise we would have matched the BLEZL instruction from the earlier |
| 667 | // ISA's instead). |
| 668 | // |
| 669 | // We have: |
| 670 | // 0b000110 sssss ttttt iiiiiiiiiiiiiiii |
| 671 | // Invalid if rs == 0 |
| 672 | // BLEZALC if rs == 0 && rt != 0 |
| 673 | // BGEZALC if rs == rt && rt != 0 |
| 674 | // BGEUC if rs != rt && rs != 0 && rt != 0 |
| 675 | |
| 676 | InsnType Rs = fieldFromInstruction(insn, 21, 5); |
| 677 | InsnType Rt = fieldFromInstruction(insn, 16, 5); |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 678 | InsnType Imm = SignExtend64(fieldFromInstruction(insn, 0, 16), 16) * 4; |
Zoran Jovanovic | 28a0ca0 | 2014-06-12 11:47:44 +0000 | [diff] [blame] | 679 | bool HasRs = false; |
| 680 | |
| 681 | if (Rt == 0) |
| 682 | return MCDisassembler::Fail; |
| 683 | else if (Rs == 0) |
| 684 | MI.setOpcode(Mips::BLEZALC); |
| 685 | else if (Rs == Rt) |
| 686 | MI.setOpcode(Mips::BGEZALC); |
| 687 | else { |
| 688 | HasRs = true; |
| 689 | MI.setOpcode(Mips::BGEUC); |
| 690 | } |
| 691 | |
| 692 | if (HasRs) |
| 693 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 694 | Rs))); |
| 695 | MI.addOperand(MCOperand::CreateReg(getReg(Decoder, Mips::GPR32RegClassID, |
| 696 | Rt))); |
| 697 | |
| 698 | MI.addOperand(MCOperand::CreateImm(Imm)); |
| 699 | |
| 700 | return MCDisassembler::Success; |
| 701 | } |
| 702 | |
Jozef Kolek | ea22c4c | 2014-11-24 13:29:59 +0000 | [diff] [blame] | 703 | /// Read two bytes from the ArrayRef and return 16 bit halfword sorted |
| 704 | /// according to the given endianess. |
| 705 | static DecodeStatus readInstruction16(ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 706 | uint64_t &Size, uint32_t &Insn, |
| 707 | bool IsBigEndian) { |
| 708 | // We want to read exactly 2 Bytes of data. |
| 709 | if (Bytes.size() < 2) { |
| 710 | Size = 0; |
| 711 | return MCDisassembler::Fail; |
| 712 | } |
| 713 | |
| 714 | if (IsBigEndian) { |
| 715 | Insn = (Bytes[0] << 8) | Bytes[1]; |
| 716 | } else { |
| 717 | Insn = (Bytes[1] << 8) | Bytes[0]; |
| 718 | } |
| 719 | |
| 720 | return MCDisassembler::Success; |
| 721 | } |
| 722 | |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 723 | /// Read four bytes from the ArrayRef and return 32 bit word sorted |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 724 | /// according to the given endianess |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 725 | static DecodeStatus readInstruction32(ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 726 | uint64_t &Size, uint32_t &Insn, |
| 727 | bool IsBigEndian, bool IsMicroMips) { |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 728 | // We want to read exactly 4 Bytes of data. |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 729 | if (Bytes.size() < 4) { |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 730 | Size = 0; |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 731 | return MCDisassembler::Fail; |
| 732 | } |
| 733 | |
Jozef Kolek | ea22c4c | 2014-11-24 13:29:59 +0000 | [diff] [blame] | 734 | // High 16 bits of a 32-bit microMIPS instruction (where the opcode is) |
| 735 | // always precede the low 16 bits in the instruction stream (that is, they |
| 736 | // are placed at lower addresses in the instruction stream). |
| 737 | // |
| 738 | // microMIPS byte ordering: |
| 739 | // Big-endian: 0 | 1 | 2 | 3 |
| 740 | // Little-endian: 1 | 0 | 3 | 2 |
| 741 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 742 | if (IsBigEndian) { |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 743 | // Encoded as a big-endian 32-bit word in the stream. |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 744 | Insn = |
| 745 | (Bytes[3] << 0) | (Bytes[2] << 8) | (Bytes[1] << 16) | (Bytes[0] << 24); |
| 746 | } else { |
Vladimir Medic | dde3d58 | 2013-09-06 12:30:36 +0000 | [diff] [blame] | 747 | if (IsMicroMips) { |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 748 | Insn = (Bytes[2] << 0) | (Bytes[3] << 8) | (Bytes[0] << 16) | |
Vladimir Medic | dde3d58 | 2013-09-06 12:30:36 +0000 | [diff] [blame] | 749 | (Bytes[1] << 24); |
| 750 | } else { |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 751 | Insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) | |
Vladimir Medic | dde3d58 | 2013-09-06 12:30:36 +0000 | [diff] [blame] | 752 | (Bytes[3] << 24); |
| 753 | } |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | return MCDisassembler::Success; |
| 757 | } |
| 758 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 759 | DecodeStatus MipsDisassembler::getInstruction(MCInst &Instr, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 760 | ArrayRef<uint8_t> Bytes, |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 761 | uint64_t Address, |
| 762 | raw_ostream &VStream, |
| 763 | raw_ostream &CStream) const { |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 764 | uint32_t Insn; |
Jozef Kolek | ea22c4c | 2014-11-24 13:29:59 +0000 | [diff] [blame] | 765 | DecodeStatus Result; |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 766 | |
Vladimir Medic | dde3d58 | 2013-09-06 12:30:36 +0000 | [diff] [blame] | 767 | if (IsMicroMips) { |
Jozef Kolek | ea22c4c | 2014-11-24 13:29:59 +0000 | [diff] [blame] | 768 | Result = readInstruction16(Bytes, Address, Size, Insn, IsBigEndian); |
| 769 | |
| 770 | DEBUG(dbgs() << "Trying MicroMips16 table (16-bit instructions):\n"); |
| 771 | // Calling the auto-generated decoder function. |
| 772 | Result = decodeInstruction(DecoderTableMicroMips16, Instr, Insn, Address, |
| 773 | this, STI); |
| 774 | if (Result != MCDisassembler::Fail) { |
| 775 | Size = 2; |
| 776 | return Result; |
| 777 | } |
| 778 | |
| 779 | Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, true); |
| 780 | if (Result == MCDisassembler::Fail) |
| 781 | return MCDisassembler::Fail; |
| 782 | |
| 783 | DEBUG(dbgs() << "Trying MicroMips32 table (32-bit instructions):\n"); |
Vladimir Medic | dde3d58 | 2013-09-06 12:30:36 +0000 | [diff] [blame] | 784 | // Calling the auto-generated decoder function. |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 785 | Result = decodeInstruction(DecoderTableMicroMips32, Instr, Insn, Address, |
Vladimir Medic | dde3d58 | 2013-09-06 12:30:36 +0000 | [diff] [blame] | 786 | this, STI); |
| 787 | if (Result != MCDisassembler::Fail) { |
| 788 | Size = 4; |
| 789 | return Result; |
| 790 | } |
| 791 | return MCDisassembler::Fail; |
| 792 | } |
| 793 | |
Jozef Kolek | ea22c4c | 2014-11-24 13:29:59 +0000 | [diff] [blame] | 794 | Result = readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false); |
| 795 | if (Result == MCDisassembler::Fail) |
| 796 | return MCDisassembler::Fail; |
| 797 | |
Daniel Sanders | c171f65 | 2014-06-13 13:15:59 +0000 | [diff] [blame] | 798 | if (hasCOP3()) { |
| 799 | DEBUG(dbgs() << "Trying COP3_ table (32-bit opcodes):\n"); |
| 800 | Result = |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 801 | decodeInstruction(DecoderTableCOP3_32, Instr, Insn, Address, this, STI); |
Daniel Sanders | c171f65 | 2014-06-13 13:15:59 +0000 | [diff] [blame] | 802 | if (Result != MCDisassembler::Fail) { |
| 803 | Size = 4; |
| 804 | return Result; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | if (hasMips32r6() && isGP64()) { |
Daniel Sanders | 0fa6041 | 2014-06-12 13:39:06 +0000 | [diff] [blame] | 809 | DEBUG(dbgs() << "Trying Mips32r6_64r6 (GPR64) table (32-bit opcodes):\n"); |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 810 | Result = decodeInstruction(DecoderTableMips32r6_64r6_GP6432, Instr, Insn, |
Daniel Sanders | 0fa6041 | 2014-06-12 13:39:06 +0000 | [diff] [blame] | 811 | Address, this, STI); |
| 812 | if (Result != MCDisassembler::Fail) { |
| 813 | Size = 4; |
| 814 | return Result; |
| 815 | } |
| 816 | } |
| 817 | |
Daniel Sanders | c171f65 | 2014-06-13 13:15:59 +0000 | [diff] [blame] | 818 | if (hasMips32r6()) { |
Daniel Sanders | 0fa6041 | 2014-06-12 13:39:06 +0000 | [diff] [blame] | 819 | DEBUG(dbgs() << "Trying Mips32r6_64r6 table (32-bit opcodes):\n"); |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 820 | Result = decodeInstruction(DecoderTableMips32r6_64r632, Instr, Insn, |
Daniel Sanders | 5c582b2 | 2014-05-22 11:23:21 +0000 | [diff] [blame] | 821 | Address, this, STI); |
| 822 | if (Result != MCDisassembler::Fail) { |
| 823 | Size = 4; |
| 824 | return Result; |
| 825 | } |
| 826 | } |
| 827 | |
Daniel Sanders | 0fa6041 | 2014-06-12 13:39:06 +0000 | [diff] [blame] | 828 | DEBUG(dbgs() << "Trying Mips table (32-bit opcodes):\n"); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 829 | // Calling the auto-generated decoder function. |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 830 | Result = |
| 831 | decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 832 | if (Result != MCDisassembler::Fail) { |
| 833 | Size = 4; |
| 834 | return Result; |
| 835 | } |
| 836 | |
| 837 | return MCDisassembler::Fail; |
| 838 | } |
| 839 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 840 | DecodeStatus Mips64Disassembler::getInstruction(MCInst &Instr, uint64_t &Size, |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 841 | ArrayRef<uint8_t> Bytes, |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 842 | uint64_t Address, |
| 843 | raw_ostream &VStream, |
| 844 | raw_ostream &CStream) const { |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 845 | uint32_t Insn; |
| 846 | |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 847 | DecodeStatus Result = |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 848 | readInstruction32(Bytes, Address, Size, Insn, IsBigEndian, false); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 849 | if (Result == MCDisassembler::Fail) |
| 850 | return MCDisassembler::Fail; |
| 851 | |
| 852 | // Calling the auto-generated decoder function. |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 853 | Result = |
| 854 | decodeInstruction(DecoderTableMips6432, Instr, Insn, Address, this, STI); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 855 | if (Result != MCDisassembler::Fail) { |
| 856 | Size = 4; |
| 857 | return Result; |
| 858 | } |
| 859 | // If we fail to decode in Mips64 decoder space we can try in Mips32 |
Rafael Espindola | 4aa6bea | 2014-11-10 18:11:10 +0000 | [diff] [blame] | 860 | Result = |
| 861 | decodeInstruction(DecoderTableMips32, Instr, Insn, Address, this, STI); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 862 | if (Result != MCDisassembler::Fail) { |
| 863 | Size = 4; |
| 864 | return Result; |
| 865 | } |
| 866 | |
| 867 | return MCDisassembler::Fail; |
| 868 | } |
| 869 | |
Reed Kotler | ec8a549 | 2013-02-14 03:05:25 +0000 | [diff] [blame] | 870 | static DecodeStatus DecodeCPU16RegsRegisterClass(MCInst &Inst, |
| 871 | unsigned RegNo, |
| 872 | uint64_t Address, |
| 873 | const void *Decoder) { |
| 874 | |
| 875 | return MCDisassembler::Fail; |
| 876 | |
| 877 | } |
| 878 | |
Akira Hatanaka | 13e6ccf | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 879 | static DecodeStatus DecodeGPR64RegisterClass(MCInst &Inst, |
| 880 | unsigned RegNo, |
| 881 | uint64_t Address, |
| 882 | const void *Decoder) { |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 883 | |
| 884 | if (RegNo > 31) |
| 885 | return MCDisassembler::Fail; |
| 886 | |
Akira Hatanaka | 13e6ccf | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 887 | unsigned Reg = getReg(Decoder, Mips::GPR64RegClassID, RegNo); |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 888 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 889 | return MCDisassembler::Success; |
| 890 | } |
| 891 | |
Zoran Jovanovic | b0852e5 | 2014-10-21 08:23:11 +0000 | [diff] [blame] | 892 | static DecodeStatus DecodeGPRMM16RegisterClass(MCInst &Inst, |
| 893 | unsigned RegNo, |
| 894 | uint64_t Address, |
| 895 | const void *Decoder) { |
Jozef Kolek | ea22c4c | 2014-11-24 13:29:59 +0000 | [diff] [blame] | 896 | if (RegNo > 7) |
| 897 | return MCDisassembler::Fail; |
| 898 | unsigned Reg = getReg(Decoder, Mips::GPRMM16RegClassID, RegNo); |
| 899 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 900 | return MCDisassembler::Success; |
Zoran Jovanovic | b0852e5 | 2014-10-21 08:23:11 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Akira Hatanaka | 13e6ccf | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 903 | static DecodeStatus DecodeGPR32RegisterClass(MCInst &Inst, |
| 904 | unsigned RegNo, |
| 905 | uint64_t Address, |
| 906 | const void *Decoder) { |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 907 | if (RegNo > 31) |
| 908 | return MCDisassembler::Fail; |
Akira Hatanaka | 13e6ccf | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 909 | unsigned Reg = getReg(Decoder, Mips::GPR32RegClassID, RegNo); |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 910 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 911 | return MCDisassembler::Success; |
| 912 | } |
| 913 | |
Akira Hatanaka | 9bfa2e2 | 2013-08-28 00:55:15 +0000 | [diff] [blame] | 914 | static DecodeStatus DecodePtrRegisterClass(MCInst &Inst, |
| 915 | unsigned RegNo, |
| 916 | uint64_t Address, |
| 917 | const void *Decoder) { |
| 918 | if (static_cast<const MipsDisassembler *>(Decoder)->isN64()) |
| 919 | return DecodeGPR64RegisterClass(Inst, RegNo, Address, Decoder); |
| 920 | |
| 921 | return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder); |
| 922 | } |
| 923 | |
Akira Hatanaka | 654655f | 2013-08-14 00:53:38 +0000 | [diff] [blame] | 924 | static DecodeStatus DecodeDSPRRegisterClass(MCInst &Inst, |
| 925 | unsigned RegNo, |
| 926 | uint64_t Address, |
| 927 | const void *Decoder) { |
Akira Hatanaka | 13e6ccf | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 928 | return DecodeGPR32RegisterClass(Inst, RegNo, Address, Decoder); |
Akira Hatanaka | ecabd1a | 2012-09-27 02:01:10 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 931 | static DecodeStatus DecodeFGR64RegisterClass(MCInst &Inst, |
| 932 | unsigned RegNo, |
| 933 | uint64_t Address, |
| 934 | const void *Decoder) { |
| 935 | if (RegNo > 31) |
| 936 | return MCDisassembler::Fail; |
| 937 | |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 938 | unsigned Reg = getReg(Decoder, Mips::FGR64RegClassID, RegNo); |
| 939 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 940 | return MCDisassembler::Success; |
| 941 | } |
| 942 | |
| 943 | static DecodeStatus DecodeFGR32RegisterClass(MCInst &Inst, |
| 944 | unsigned RegNo, |
| 945 | uint64_t Address, |
| 946 | const void *Decoder) { |
| 947 | if (RegNo > 31) |
| 948 | return MCDisassembler::Fail; |
| 949 | |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 950 | unsigned Reg = getReg(Decoder, Mips::FGR32RegClassID, RegNo); |
| 951 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 952 | return MCDisassembler::Success; |
| 953 | } |
| 954 | |
| 955 | static DecodeStatus DecodeCCRRegisterClass(MCInst &Inst, |
| 956 | unsigned RegNo, |
| 957 | uint64_t Address, |
| 958 | const void *Decoder) { |
Chad Rosier | 253777f | 2013-06-26 22:23:32 +0000 | [diff] [blame] | 959 | if (RegNo > 31) |
| 960 | return MCDisassembler::Fail; |
| 961 | unsigned Reg = getReg(Decoder, Mips::CCRRegClassID, RegNo); |
| 962 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 963 | return MCDisassembler::Success; |
| 964 | } |
| 965 | |
Akira Hatanaka | 1fb1b8b | 2013-07-26 20:13:47 +0000 | [diff] [blame] | 966 | static DecodeStatus DecodeFCCRegisterClass(MCInst &Inst, |
| 967 | unsigned RegNo, |
| 968 | uint64_t Address, |
| 969 | const void *Decoder) { |
| 970 | if (RegNo > 7) |
| 971 | return MCDisassembler::Fail; |
| 972 | unsigned Reg = getReg(Decoder, Mips::FCCRegClassID, RegNo); |
| 973 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 974 | return MCDisassembler::Success; |
| 975 | } |
| 976 | |
Daniel Sanders | 0fa6041 | 2014-06-12 13:39:06 +0000 | [diff] [blame] | 977 | static DecodeStatus DecodeFGRCCRegisterClass(MCInst &Inst, unsigned RegNo, |
| 978 | uint64_t Address, |
| 979 | const void *Decoder) { |
| 980 | if (RegNo > 31) |
| 981 | return MCDisassembler::Fail; |
| 982 | |
| 983 | unsigned Reg = getReg(Decoder, Mips::FGRCCRegClassID, RegNo); |
| 984 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 985 | return MCDisassembler::Success; |
| 986 | } |
| 987 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 988 | static DecodeStatus DecodeMem(MCInst &Inst, |
| 989 | unsigned Insn, |
| 990 | uint64_t Address, |
| 991 | const void *Decoder) { |
| 992 | int Offset = SignExtend32<16>(Insn & 0xffff); |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 993 | unsigned Reg = fieldFromInstruction(Insn, 16, 5); |
| 994 | unsigned Base = fieldFromInstruction(Insn, 21, 5); |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 995 | |
Akira Hatanaka | 13e6ccf | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 996 | Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg); |
| 997 | Base = getReg(Decoder, Mips::GPR32RegClassID, Base); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 998 | |
| 999 | if(Inst.getOpcode() == Mips::SC){ |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 1000 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 1003 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1004 | Inst.addOperand(MCOperand::CreateReg(Base)); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1005 | Inst.addOperand(MCOperand::CreateImm(Offset)); |
| 1006 | |
| 1007 | return MCDisassembler::Success; |
| 1008 | } |
| 1009 | |
Daniel Sanders | 92db6b7 | 2014-10-01 08:26:55 +0000 | [diff] [blame] | 1010 | static DecodeStatus DecodeCacheOp(MCInst &Inst, |
| 1011 | unsigned Insn, |
| 1012 | uint64_t Address, |
| 1013 | const void *Decoder) { |
| 1014 | int Offset = SignExtend32<16>(Insn & 0xffff); |
| 1015 | unsigned Hint = fieldFromInstruction(Insn, 16, 5); |
| 1016 | unsigned Base = fieldFromInstruction(Insn, 21, 5); |
| 1017 | |
| 1018 | Base = getReg(Decoder, Mips::GPR32RegClassID, Base); |
| 1019 | |
| 1020 | Inst.addOperand(MCOperand::CreateReg(Base)); |
| 1021 | Inst.addOperand(MCOperand::CreateImm(Offset)); |
| 1022 | Inst.addOperand(MCOperand::CreateImm(Hint)); |
| 1023 | |
| 1024 | return MCDisassembler::Success; |
| 1025 | } |
| 1026 | |
Matheus Almeida | fe0bf9f | 2013-10-21 13:07:13 +0000 | [diff] [blame] | 1027 | static DecodeStatus DecodeMSA128Mem(MCInst &Inst, unsigned Insn, |
| 1028 | uint64_t Address, const void *Decoder) { |
| 1029 | int Offset = SignExtend32<10>(fieldFromInstruction(Insn, 16, 10)); |
| 1030 | unsigned Reg = fieldFromInstruction(Insn, 6, 5); |
| 1031 | unsigned Base = fieldFromInstruction(Insn, 11, 5); |
| 1032 | |
| 1033 | Reg = getReg(Decoder, Mips::MSA128BRegClassID, Reg); |
| 1034 | Base = getReg(Decoder, Mips::GPR32RegClassID, Base); |
| 1035 | |
| 1036 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1037 | Inst.addOperand(MCOperand::CreateReg(Base)); |
Matheus Almeida | 6b59c44 | 2013-12-05 11:06:22 +0000 | [diff] [blame] | 1038 | |
| 1039 | // The immediate field of an LD/ST instruction is scaled which means it must |
| 1040 | // be multiplied (when decoding) by the size (in bytes) of the instructions' |
| 1041 | // data format. |
| 1042 | // .b - 1 byte |
| 1043 | // .h - 2 bytes |
| 1044 | // .w - 4 bytes |
| 1045 | // .d - 8 bytes |
| 1046 | switch(Inst.getOpcode()) |
| 1047 | { |
| 1048 | default: |
| 1049 | assert (0 && "Unexpected instruction"); |
| 1050 | return MCDisassembler::Fail; |
| 1051 | break; |
| 1052 | case Mips::LD_B: |
| 1053 | case Mips::ST_B: |
| 1054 | Inst.addOperand(MCOperand::CreateImm(Offset)); |
| 1055 | break; |
| 1056 | case Mips::LD_H: |
| 1057 | case Mips::ST_H: |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 1058 | Inst.addOperand(MCOperand::CreateImm(Offset * 2)); |
Matheus Almeida | 6b59c44 | 2013-12-05 11:06:22 +0000 | [diff] [blame] | 1059 | break; |
| 1060 | case Mips::LD_W: |
| 1061 | case Mips::ST_W: |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 1062 | Inst.addOperand(MCOperand::CreateImm(Offset * 4)); |
Matheus Almeida | 6b59c44 | 2013-12-05 11:06:22 +0000 | [diff] [blame] | 1063 | break; |
| 1064 | case Mips::LD_D: |
| 1065 | case Mips::ST_D: |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 1066 | Inst.addOperand(MCOperand::CreateImm(Offset * 8)); |
Matheus Almeida | 6b59c44 | 2013-12-05 11:06:22 +0000 | [diff] [blame] | 1067 | break; |
| 1068 | } |
Matheus Almeida | fe0bf9f | 2013-10-21 13:07:13 +0000 | [diff] [blame] | 1069 | |
| 1070 | return MCDisassembler::Success; |
| 1071 | } |
| 1072 | |
Vladimir Medic | dde3d58 | 2013-09-06 12:30:36 +0000 | [diff] [blame] | 1073 | static DecodeStatus DecodeMemMMImm12(MCInst &Inst, |
| 1074 | unsigned Insn, |
| 1075 | uint64_t Address, |
| 1076 | const void *Decoder) { |
| 1077 | int Offset = SignExtend32<12>(Insn & 0x0fff); |
| 1078 | unsigned Reg = fieldFromInstruction(Insn, 21, 5); |
| 1079 | unsigned Base = fieldFromInstruction(Insn, 16, 5); |
| 1080 | |
| 1081 | Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg); |
| 1082 | Base = getReg(Decoder, Mips::GPR32RegClassID, Base); |
| 1083 | |
Zoran Jovanovic | a4c4b5f | 2014-11-19 16:44:02 +0000 | [diff] [blame] | 1084 | switch (Inst.getOpcode()) { |
| 1085 | case Mips::SWM32_MM: |
| 1086 | case Mips::LWM32_MM: |
| 1087 | if (DecodeRegListOperand(Inst, Insn, Address, Decoder) |
| 1088 | == MCDisassembler::Fail) |
| 1089 | return MCDisassembler::Fail; |
| 1090 | Inst.addOperand(MCOperand::CreateReg(Base)); |
| 1091 | Inst.addOperand(MCOperand::CreateImm(Offset)); |
| 1092 | break; |
| 1093 | case Mips::SC_MM: |
Zoran Jovanovic | 285cc28 | 2014-02-28 18:22:56 +0000 | [diff] [blame] | 1094 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
Zoran Jovanovic | a4c4b5f | 2014-11-19 16:44:02 +0000 | [diff] [blame] | 1095 | // fallthrough |
| 1096 | default: |
| 1097 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1098 | Inst.addOperand(MCOperand::CreateReg(Base)); |
| 1099 | Inst.addOperand(MCOperand::CreateImm(Offset)); |
| 1100 | } |
Vladimir Medic | dde3d58 | 2013-09-06 12:30:36 +0000 | [diff] [blame] | 1101 | |
| 1102 | return MCDisassembler::Success; |
| 1103 | } |
| 1104 | |
| 1105 | static DecodeStatus DecodeMemMMImm16(MCInst &Inst, |
| 1106 | unsigned Insn, |
| 1107 | uint64_t Address, |
| 1108 | const void *Decoder) { |
| 1109 | int Offset = SignExtend32<16>(Insn & 0xffff); |
| 1110 | unsigned Reg = fieldFromInstruction(Insn, 21, 5); |
| 1111 | unsigned Base = fieldFromInstruction(Insn, 16, 5); |
| 1112 | |
| 1113 | Reg = getReg(Decoder, Mips::GPR32RegClassID, Reg); |
| 1114 | Base = getReg(Decoder, Mips::GPR32RegClassID, Base); |
| 1115 | |
| 1116 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1117 | Inst.addOperand(MCOperand::CreateReg(Base)); |
| 1118 | Inst.addOperand(MCOperand::CreateImm(Offset)); |
| 1119 | |
| 1120 | return MCDisassembler::Success; |
| 1121 | } |
| 1122 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1123 | static DecodeStatus DecodeFMem(MCInst &Inst, |
| 1124 | unsigned Insn, |
| 1125 | uint64_t Address, |
| 1126 | const void *Decoder) { |
| 1127 | int Offset = SignExtend32<16>(Insn & 0xffff); |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1128 | unsigned Reg = fieldFromInstruction(Insn, 16, 5); |
| 1129 | unsigned Base = fieldFromInstruction(Insn, 21, 5); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1130 | |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 1131 | Reg = getReg(Decoder, Mips::FGR64RegClassID, Reg); |
Akira Hatanaka | 13e6ccf | 2013-08-06 23:08:38 +0000 | [diff] [blame] | 1132 | Base = getReg(Decoder, Mips::GPR32RegClassID, Base); |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 1133 | |
| 1134 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1135 | Inst.addOperand(MCOperand::CreateReg(Base)); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1136 | Inst.addOperand(MCOperand::CreateImm(Offset)); |
| 1137 | |
| 1138 | return MCDisassembler::Success; |
| 1139 | } |
| 1140 | |
Daniel Sanders | 92db6b7 | 2014-10-01 08:26:55 +0000 | [diff] [blame] | 1141 | static DecodeStatus DecodeFMem2(MCInst &Inst, |
| 1142 | unsigned Insn, |
| 1143 | uint64_t Address, |
| 1144 | const void *Decoder) { |
| 1145 | int Offset = SignExtend32<16>(Insn & 0xffff); |
| 1146 | unsigned Reg = fieldFromInstruction(Insn, 16, 5); |
| 1147 | unsigned Base = fieldFromInstruction(Insn, 21, 5); |
| 1148 | |
| 1149 | Reg = getReg(Decoder, Mips::COP2RegClassID, Reg); |
| 1150 | Base = getReg(Decoder, Mips::GPR32RegClassID, Base); |
| 1151 | |
| 1152 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1153 | Inst.addOperand(MCOperand::CreateReg(Base)); |
| 1154 | Inst.addOperand(MCOperand::CreateImm(Offset)); |
| 1155 | |
| 1156 | return MCDisassembler::Success; |
| 1157 | } |
| 1158 | |
| 1159 | static DecodeStatus DecodeFMem3(MCInst &Inst, |
| 1160 | unsigned Insn, |
| 1161 | uint64_t Address, |
| 1162 | const void *Decoder) { |
| 1163 | int Offset = SignExtend32<16>(Insn & 0xffff); |
| 1164 | unsigned Reg = fieldFromInstruction(Insn, 16, 5); |
| 1165 | unsigned Base = fieldFromInstruction(Insn, 21, 5); |
| 1166 | |
| 1167 | Reg = getReg(Decoder, Mips::COP3RegClassID, Reg); |
| 1168 | Base = getReg(Decoder, Mips::GPR32RegClassID, Base); |
| 1169 | |
| 1170 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1171 | Inst.addOperand(MCOperand::CreateReg(Base)); |
| 1172 | Inst.addOperand(MCOperand::CreateImm(Offset)); |
| 1173 | |
| 1174 | return MCDisassembler::Success; |
| 1175 | } |
| 1176 | |
Daniel Sanders | 6a803f6 | 2014-06-16 13:13:03 +0000 | [diff] [blame] | 1177 | static DecodeStatus DecodeSpecial3LlSc(MCInst &Inst, |
| 1178 | unsigned Insn, |
| 1179 | uint64_t Address, |
| 1180 | const void *Decoder) { |
| 1181 | int64_t Offset = SignExtend64<9>((Insn >> 7) & 0x1ff); |
| 1182 | unsigned Rt = fieldFromInstruction(Insn, 16, 5); |
| 1183 | unsigned Base = fieldFromInstruction(Insn, 21, 5); |
| 1184 | |
| 1185 | Rt = getReg(Decoder, Mips::GPR32RegClassID, Rt); |
| 1186 | Base = getReg(Decoder, Mips::GPR32RegClassID, Base); |
| 1187 | |
| 1188 | if(Inst.getOpcode() == Mips::SC_R6 || Inst.getOpcode() == Mips::SCD_R6){ |
| 1189 | Inst.addOperand(MCOperand::CreateReg(Rt)); |
| 1190 | } |
| 1191 | |
| 1192 | Inst.addOperand(MCOperand::CreateReg(Rt)); |
| 1193 | Inst.addOperand(MCOperand::CreateReg(Base)); |
| 1194 | Inst.addOperand(MCOperand::CreateImm(Offset)); |
| 1195 | |
| 1196 | return MCDisassembler::Success; |
| 1197 | } |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1198 | |
| 1199 | static DecodeStatus DecodeHWRegsRegisterClass(MCInst &Inst, |
| 1200 | unsigned RegNo, |
| 1201 | uint64_t Address, |
| 1202 | const void *Decoder) { |
| 1203 | // Currently only hardware register 29 is supported. |
| 1204 | if (RegNo != 29) |
| 1205 | return MCDisassembler::Fail; |
| 1206 | Inst.addOperand(MCOperand::CreateReg(Mips::HWR29)); |
| 1207 | return MCDisassembler::Success; |
| 1208 | } |
| 1209 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1210 | static DecodeStatus DecodeAFGR64RegisterClass(MCInst &Inst, |
| 1211 | unsigned RegNo, |
| 1212 | uint64_t Address, |
| 1213 | const void *Decoder) { |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 1214 | if (RegNo > 30 || RegNo %2) |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1215 | return MCDisassembler::Fail; |
| 1216 | |
Akira Hatanaka | 9bf2b56 | 2012-07-09 18:46:47 +0000 | [diff] [blame] | 1217 | ; |
| 1218 | unsigned Reg = getReg(Decoder, Mips::AFGR64RegClassID, RegNo /2); |
| 1219 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1220 | return MCDisassembler::Success; |
| 1221 | } |
| 1222 | |
Akira Hatanaka | 00fcf2e | 2013-08-08 21:54:26 +0000 | [diff] [blame] | 1223 | static DecodeStatus DecodeACC64DSPRegisterClass(MCInst &Inst, |
| 1224 | unsigned RegNo, |
| 1225 | uint64_t Address, |
| 1226 | const void *Decoder) { |
Akira Hatanaka | ecabd1a | 2012-09-27 02:01:10 +0000 | [diff] [blame] | 1227 | if (RegNo >= 4) |
| 1228 | return MCDisassembler::Fail; |
| 1229 | |
Akira Hatanaka | 00fcf2e | 2013-08-08 21:54:26 +0000 | [diff] [blame] | 1230 | unsigned Reg = getReg(Decoder, Mips::ACC64DSPRegClassID, RegNo); |
Akira Hatanaka | ecabd1a | 2012-09-27 02:01:10 +0000 | [diff] [blame] | 1231 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1232 | return MCDisassembler::Success; |
| 1233 | } |
| 1234 | |
Akira Hatanaka | 8002a3f | 2013-08-14 00:47:08 +0000 | [diff] [blame] | 1235 | static DecodeStatus DecodeHI32DSPRegisterClass(MCInst &Inst, |
| 1236 | unsigned RegNo, |
| 1237 | uint64_t Address, |
| 1238 | const void *Decoder) { |
Akira Hatanaka | 59bfaf7 | 2013-04-18 00:52:44 +0000 | [diff] [blame] | 1239 | if (RegNo >= 4) |
| 1240 | return MCDisassembler::Fail; |
| 1241 | |
Akira Hatanaka | 8002a3f | 2013-08-14 00:47:08 +0000 | [diff] [blame] | 1242 | unsigned Reg = getReg(Decoder, Mips::HI32DSPRegClassID, RegNo); |
Akira Hatanaka | 59bfaf7 | 2013-04-18 00:52:44 +0000 | [diff] [blame] | 1243 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1244 | return MCDisassembler::Success; |
| 1245 | } |
| 1246 | |
Akira Hatanaka | 8002a3f | 2013-08-14 00:47:08 +0000 | [diff] [blame] | 1247 | static DecodeStatus DecodeLO32DSPRegisterClass(MCInst &Inst, |
| 1248 | unsigned RegNo, |
| 1249 | uint64_t Address, |
| 1250 | const void *Decoder) { |
Akira Hatanaka | 59bfaf7 | 2013-04-18 00:52:44 +0000 | [diff] [blame] | 1251 | if (RegNo >= 4) |
| 1252 | return MCDisassembler::Fail; |
| 1253 | |
Akira Hatanaka | 8002a3f | 2013-08-14 00:47:08 +0000 | [diff] [blame] | 1254 | unsigned Reg = getReg(Decoder, Mips::LO32DSPRegClassID, RegNo); |
Akira Hatanaka | 59bfaf7 | 2013-04-18 00:52:44 +0000 | [diff] [blame] | 1255 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1256 | return MCDisassembler::Success; |
| 1257 | } |
| 1258 | |
Jack Carter | 3eb663b | 2013-09-26 00:09:46 +0000 | [diff] [blame] | 1259 | static DecodeStatus DecodeMSA128BRegisterClass(MCInst &Inst, |
| 1260 | unsigned RegNo, |
| 1261 | uint64_t Address, |
| 1262 | const void *Decoder) { |
| 1263 | if (RegNo > 31) |
| 1264 | return MCDisassembler::Fail; |
| 1265 | |
| 1266 | unsigned Reg = getReg(Decoder, Mips::MSA128BRegClassID, RegNo); |
| 1267 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1268 | return MCDisassembler::Success; |
| 1269 | } |
| 1270 | |
Jack Carter | 5dc8ac9 | 2013-09-25 23:50:44 +0000 | [diff] [blame] | 1271 | static DecodeStatus DecodeMSA128HRegisterClass(MCInst &Inst, |
| 1272 | unsigned RegNo, |
| 1273 | uint64_t Address, |
| 1274 | const void *Decoder) { |
| 1275 | if (RegNo > 31) |
| 1276 | return MCDisassembler::Fail; |
| 1277 | |
| 1278 | unsigned Reg = getReg(Decoder, Mips::MSA128HRegClassID, RegNo); |
| 1279 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1280 | return MCDisassembler::Success; |
| 1281 | } |
| 1282 | |
| 1283 | static DecodeStatus DecodeMSA128WRegisterClass(MCInst &Inst, |
| 1284 | unsigned RegNo, |
| 1285 | uint64_t Address, |
| 1286 | const void *Decoder) { |
| 1287 | if (RegNo > 31) |
| 1288 | return MCDisassembler::Fail; |
| 1289 | |
| 1290 | unsigned Reg = getReg(Decoder, Mips::MSA128WRegClassID, RegNo); |
| 1291 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1292 | return MCDisassembler::Success; |
| 1293 | } |
| 1294 | |
| 1295 | static DecodeStatus DecodeMSA128DRegisterClass(MCInst &Inst, |
| 1296 | unsigned RegNo, |
| 1297 | uint64_t Address, |
| 1298 | const void *Decoder) { |
| 1299 | if (RegNo > 31) |
| 1300 | return MCDisassembler::Fail; |
| 1301 | |
| 1302 | unsigned Reg = getReg(Decoder, Mips::MSA128DRegClassID, RegNo); |
| 1303 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1304 | return MCDisassembler::Success; |
| 1305 | } |
| 1306 | |
Matheus Almeida | a591fdc | 2013-10-21 12:26:50 +0000 | [diff] [blame] | 1307 | static DecodeStatus DecodeMSACtrlRegisterClass(MCInst &Inst, |
| 1308 | unsigned RegNo, |
| 1309 | uint64_t Address, |
| 1310 | const void *Decoder) { |
| 1311 | if (RegNo > 7) |
| 1312 | return MCDisassembler::Fail; |
| 1313 | |
| 1314 | unsigned Reg = getReg(Decoder, Mips::MSACtrlRegClassID, RegNo); |
| 1315 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1316 | return MCDisassembler::Success; |
| 1317 | } |
| 1318 | |
Daniel Sanders | 2a83d68 | 2014-05-21 12:56:39 +0000 | [diff] [blame] | 1319 | static DecodeStatus DecodeCOP2RegisterClass(MCInst &Inst, |
| 1320 | unsigned RegNo, |
| 1321 | uint64_t Address, |
| 1322 | const void *Decoder) { |
| 1323 | if (RegNo > 31) |
| 1324 | return MCDisassembler::Fail; |
| 1325 | |
| 1326 | unsigned Reg = getReg(Decoder, Mips::COP2RegClassID, RegNo); |
| 1327 | Inst.addOperand(MCOperand::CreateReg(Reg)); |
| 1328 | return MCDisassembler::Success; |
| 1329 | } |
| 1330 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1331 | static DecodeStatus DecodeBranchTarget(MCInst &Inst, |
| 1332 | unsigned Offset, |
| 1333 | uint64_t Address, |
| 1334 | const void *Decoder) { |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 1335 | int32_t BranchOffset = (SignExtend32<16>(Offset) * 4) + 4; |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1336 | Inst.addOperand(MCOperand::CreateImm(BranchOffset)); |
| 1337 | return MCDisassembler::Success; |
| 1338 | } |
| 1339 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1340 | static DecodeStatus DecodeJumpTarget(MCInst &Inst, |
| 1341 | unsigned Insn, |
| 1342 | uint64_t Address, |
| 1343 | const void *Decoder) { |
| 1344 | |
Jim Grosbach | ecaef49 | 2012-08-14 19:06:05 +0000 | [diff] [blame] | 1345 | unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 2; |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1346 | Inst.addOperand(MCOperand::CreateImm(JumpOffset)); |
| 1347 | return MCDisassembler::Success; |
| 1348 | } |
| 1349 | |
Zoran Jovanovic | 3c8869d | 2014-05-16 11:03:45 +0000 | [diff] [blame] | 1350 | static DecodeStatus DecodeBranchTarget21(MCInst &Inst, |
| 1351 | unsigned Offset, |
| 1352 | uint64_t Address, |
| 1353 | const void *Decoder) { |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 1354 | int32_t BranchOffset = SignExtend32<21>(Offset) * 4; |
Zoran Jovanovic | 3c8869d | 2014-05-16 11:03:45 +0000 | [diff] [blame] | 1355 | |
| 1356 | Inst.addOperand(MCOperand::CreateImm(BranchOffset)); |
| 1357 | return MCDisassembler::Success; |
| 1358 | } |
| 1359 | |
| 1360 | static DecodeStatus DecodeBranchTarget26(MCInst &Inst, |
| 1361 | unsigned Offset, |
| 1362 | uint64_t Address, |
| 1363 | const void *Decoder) { |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 1364 | int32_t BranchOffset = SignExtend32<26>(Offset) * 4; |
Zoran Jovanovic | 3c8869d | 2014-05-16 11:03:45 +0000 | [diff] [blame] | 1365 | |
| 1366 | Inst.addOperand(MCOperand::CreateImm(BranchOffset)); |
| 1367 | return MCDisassembler::Success; |
| 1368 | } |
| 1369 | |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 1370 | static DecodeStatus DecodeBranchTargetMM(MCInst &Inst, |
| 1371 | unsigned Offset, |
| 1372 | uint64_t Address, |
| 1373 | const void *Decoder) { |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 1374 | int32_t BranchOffset = SignExtend32<16>(Offset) * 2; |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 1375 | Inst.addOperand(MCOperand::CreateImm(BranchOffset)); |
| 1376 | return MCDisassembler::Success; |
| 1377 | } |
| 1378 | |
Zoran Jovanovic | 507e084 | 2013-10-29 16:38:59 +0000 | [diff] [blame] | 1379 | static DecodeStatus DecodeJumpTargetMM(MCInst &Inst, |
| 1380 | unsigned Insn, |
| 1381 | uint64_t Address, |
| 1382 | const void *Decoder) { |
| 1383 | unsigned JumpOffset = fieldFromInstruction(Insn, 0, 26) << 1; |
| 1384 | Inst.addOperand(MCOperand::CreateImm(JumpOffset)); |
| 1385 | return MCDisassembler::Success; |
| 1386 | } |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1387 | |
| 1388 | static DecodeStatus DecodeSimm16(MCInst &Inst, |
| 1389 | unsigned Insn, |
| 1390 | uint64_t Address, |
| 1391 | const void *Decoder) { |
| 1392 | Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Insn))); |
| 1393 | return MCDisassembler::Success; |
| 1394 | } |
| 1395 | |
Matheus Almeida | 779c593 | 2013-11-18 12:32:49 +0000 | [diff] [blame] | 1396 | static DecodeStatus DecodeLSAImm(MCInst &Inst, |
| 1397 | unsigned Insn, |
| 1398 | uint64_t Address, |
| 1399 | const void *Decoder) { |
| 1400 | // We add one to the immediate field as it was encoded as 'imm - 1'. |
| 1401 | Inst.addOperand(MCOperand::CreateImm(Insn + 1)); |
| 1402 | return MCDisassembler::Success; |
| 1403 | } |
| 1404 | |
Akira Hatanaka | 71928e6 | 2012-04-17 18:03:21 +0000 | [diff] [blame] | 1405 | static DecodeStatus DecodeInsSize(MCInst &Inst, |
| 1406 | unsigned Insn, |
| 1407 | uint64_t Address, |
| 1408 | const void *Decoder) { |
| 1409 | // First we need to grab the pos(lsb) from MCInst. |
| 1410 | int Pos = Inst.getOperand(2).getImm(); |
| 1411 | int Size = (int) Insn - Pos + 1; |
| 1412 | Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size))); |
| 1413 | return MCDisassembler::Success; |
| 1414 | } |
| 1415 | |
| 1416 | static DecodeStatus DecodeExtSize(MCInst &Inst, |
| 1417 | unsigned Insn, |
| 1418 | uint64_t Address, |
| 1419 | const void *Decoder) { |
| 1420 | int Size = (int) Insn + 1; |
| 1421 | Inst.addOperand(MCOperand::CreateImm(SignExtend32<16>(Size))); |
| 1422 | return MCDisassembler::Success; |
| 1423 | } |
Daniel Sanders | b59e1a4 | 2014-05-15 10:45:58 +0000 | [diff] [blame] | 1424 | |
| 1425 | static DecodeStatus DecodeSimm19Lsl2(MCInst &Inst, unsigned Insn, |
| 1426 | uint64_t Address, const void *Decoder) { |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 1427 | Inst.addOperand(MCOperand::CreateImm(SignExtend32<19>(Insn) * 4)); |
Daniel Sanders | b59e1a4 | 2014-05-15 10:45:58 +0000 | [diff] [blame] | 1428 | return MCDisassembler::Success; |
| 1429 | } |
Zoran Jovanovic | 2855142 | 2014-06-09 09:49:51 +0000 | [diff] [blame] | 1430 | |
| 1431 | static DecodeStatus DecodeSimm18Lsl3(MCInst &Inst, unsigned Insn, |
| 1432 | uint64_t Address, const void *Decoder) { |
Alexey Samsonov | d37bab6 | 2014-09-02 17:49:16 +0000 | [diff] [blame] | 1433 | Inst.addOperand(MCOperand::CreateImm(SignExtend32<18>(Insn) * 8)); |
Zoran Jovanovic | 2855142 | 2014-06-09 09:49:51 +0000 | [diff] [blame] | 1434 | return MCDisassembler::Success; |
| 1435 | } |
Zoran Jovanovic | a4c4b5f | 2014-11-19 16:44:02 +0000 | [diff] [blame] | 1436 | |
| 1437 | static DecodeStatus DecodeRegListOperand(MCInst &Inst, |
| 1438 | unsigned Insn, |
| 1439 | uint64_t Address, |
| 1440 | const void *Decoder) { |
| 1441 | unsigned Regs[] = {Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5, |
| 1442 | Mips::S6, Mips::FP}; |
| 1443 | unsigned RegNum; |
| 1444 | |
| 1445 | unsigned RegLst = fieldFromInstruction(Insn, 21, 5); |
| 1446 | // Empty register lists are not allowed. |
| 1447 | if (RegLst == 0) |
| 1448 | return MCDisassembler::Fail; |
| 1449 | |
| 1450 | RegNum = RegLst & 0xf; |
| 1451 | for (unsigned i = 0; i < RegNum; i++) |
| 1452 | Inst.addOperand(MCOperand::CreateReg(Regs[i])); |
| 1453 | |
| 1454 | if (RegLst & 0x10) |
| 1455 | Inst.addOperand(MCOperand::CreateReg(Mips::RA)); |
| 1456 | |
| 1457 | return MCDisassembler::Success; |
| 1458 | } |