LLVM API Documentation

XCoreDisassembler.cpp
Go to the documentation of this file.
00001 //===- XCoreDisassembler.cpp - Disassembler for XCore -----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 ///
00010 /// \file
00011 /// \brief This file is part of the XCore Disassembler.
00012 ///
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "XCore.h"
00016 #include "XCoreRegisterInfo.h"
00017 #include "llvm/MC/MCContext.h"
00018 #include "llvm/MC/MCDisassembler.h"
00019 #include "llvm/MC/MCFixedLenDisassembler.h"
00020 #include "llvm/MC/MCInst.h"
00021 #include "llvm/MC/MCSubtargetInfo.h"
00022 #include "llvm/Support/MemoryObject.h"
00023 #include "llvm/Support/TargetRegistry.h"
00024 
00025 using namespace llvm;
00026 
00027 #define DEBUG_TYPE "xcore-disassembler"
00028 
00029 typedef MCDisassembler::DecodeStatus DecodeStatus;
00030 
00031 namespace {
00032 
00033 /// \brief A disassembler class for XCore.
00034 class XCoreDisassembler : public MCDisassembler {
00035 public:
00036   XCoreDisassembler(const MCSubtargetInfo &STI, MCContext &Ctx) :
00037     MCDisassembler(STI, Ctx) {}
00038 
00039   /// \brief See MCDisassembler.
00040   DecodeStatus getInstruction(MCInst &instr, uint64_t &size,
00041                               const MemoryObject &region, uint64_t address,
00042                               raw_ostream &vStream,
00043                               raw_ostream &cStream) const override;
00044 };
00045 }
00046 
00047 static bool readInstruction16(const MemoryObject &region,
00048                               uint64_t address,
00049                               uint64_t &size,
00050                               uint16_t &insn) {
00051   uint8_t Bytes[4];
00052 
00053   // We want to read exactly 2 Bytes of data.
00054   if (region.readBytes(address, 2, Bytes) == -1) {
00055     size = 0;
00056     return false;
00057   }
00058   // Encoded as a little-endian 16-bit word in the stream.
00059   insn = (Bytes[0] <<  0) | (Bytes[1] <<  8);
00060   return true;
00061 }
00062 
00063 static bool readInstruction32(const MemoryObject &region,
00064                               uint64_t address,
00065                               uint64_t &size,
00066                               uint32_t &insn) {
00067   uint8_t Bytes[4];
00068 
00069   // We want to read exactly 4 Bytes of data.
00070   if (region.readBytes(address, 4, Bytes) == -1) {
00071     size = 0;
00072     return false;
00073   }
00074   // Encoded as a little-endian 32-bit word in the stream.
00075   insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
00076          (Bytes[3] << 24);
00077   return true;
00078 }
00079 
00080 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
00081   const XCoreDisassembler *Dis = static_cast<const XCoreDisassembler*>(D);
00082   const MCRegisterInfo *RegInfo = Dis->getContext().getRegisterInfo();
00083   return *(RegInfo->getRegClass(RC).begin() + RegNo);
00084 }
00085 
00086 static DecodeStatus DecodeGRRegsRegisterClass(MCInst &Inst,
00087                                               unsigned RegNo,
00088                                               uint64_t Address,
00089                                               const void *Decoder);
00090 
00091 static DecodeStatus DecodeRRegsRegisterClass(MCInst &Inst,
00092                                              unsigned RegNo,
00093                                              uint64_t Address,
00094                                              const void *Decoder);
00095 
00096 static DecodeStatus DecodeBitpOperand(MCInst &Inst, unsigned Val,
00097                                       uint64_t Address, const void *Decoder);
00098 
00099 static DecodeStatus DecodeNegImmOperand(MCInst &Inst, unsigned Val,
00100                                         uint64_t Address, const void *Decoder);
00101 
00102 static DecodeStatus Decode2RInstruction(MCInst &Inst,
00103                                         unsigned Insn,
00104                                         uint64_t Address,
00105                                         const void *Decoder);
00106 
00107 static DecodeStatus Decode2RImmInstruction(MCInst &Inst,
00108                                            unsigned Insn,
00109                                            uint64_t Address,
00110                                            const void *Decoder);
00111 
00112 static DecodeStatus DecodeR2RInstruction(MCInst &Inst,
00113                                          unsigned Insn,
00114                                          uint64_t Address,
00115                                          const void *Decoder);
00116 
00117 static DecodeStatus Decode2RSrcDstInstruction(MCInst &Inst,
00118                                               unsigned Insn,
00119                                               uint64_t Address,
00120                                               const void *Decoder);
00121 
00122 static DecodeStatus DecodeRUSInstruction(MCInst &Inst,
00123                                          unsigned Insn,
00124                                          uint64_t Address,
00125                                          const void *Decoder);
00126 
00127 static DecodeStatus DecodeRUSBitpInstruction(MCInst &Inst,
00128                                              unsigned Insn,
00129                                              uint64_t Address,
00130                                              const void *Decoder);
00131 
00132 static DecodeStatus DecodeRUSSrcDstBitpInstruction(MCInst &Inst,
00133                                                    unsigned Insn,
00134                                                    uint64_t Address,
00135                                                    const void *Decoder);
00136 
00137 static DecodeStatus DecodeL2RInstruction(MCInst &Inst,
00138                                          unsigned Insn,
00139                                          uint64_t Address,
00140                                          const void *Decoder);
00141 
00142 static DecodeStatus DecodeLR2RInstruction(MCInst &Inst,
00143                                           unsigned Insn,
00144                                           uint64_t Address,
00145                                           const void *Decoder);
00146 
00147 static DecodeStatus Decode3RInstruction(MCInst &Inst,
00148                                         unsigned Insn,
00149                                         uint64_t Address,
00150                                         const void *Decoder);
00151 
00152 static DecodeStatus Decode3RImmInstruction(MCInst &Inst,
00153                                            unsigned Insn,
00154                                            uint64_t Address,
00155                                            const void *Decoder);
00156 
00157 static DecodeStatus Decode2RUSInstruction(MCInst &Inst,
00158                                           unsigned Insn,
00159                                           uint64_t Address,
00160                                           const void *Decoder);
00161 
00162 static DecodeStatus Decode2RUSBitpInstruction(MCInst &Inst,
00163                                               unsigned Insn,
00164                                               uint64_t Address,
00165                                               const void *Decoder);
00166 
00167 static DecodeStatus DecodeL3RInstruction(MCInst &Inst,
00168                                          unsigned Insn,
00169                                          uint64_t Address,
00170                                          const void *Decoder);
00171 
00172 static DecodeStatus DecodeL3RSrcDstInstruction(MCInst &Inst,
00173                                                unsigned Insn,
00174                                                uint64_t Address,
00175                                                const void *Decoder);
00176 
00177 static DecodeStatus DecodeL2RUSInstruction(MCInst &Inst,
00178                                            unsigned Insn,
00179                                            uint64_t Address,
00180                                            const void *Decoder);
00181 
00182 static DecodeStatus DecodeL2RUSBitpInstruction(MCInst &Inst,
00183                                                unsigned Insn,
00184                                                uint64_t Address,
00185                                                const void *Decoder);
00186 
00187 static DecodeStatus DecodeL6RInstruction(MCInst &Inst,
00188                                          unsigned Insn,
00189                                          uint64_t Address,
00190                                          const void *Decoder);
00191 
00192 static DecodeStatus DecodeL5RInstruction(MCInst &Inst,
00193                                          unsigned Insn,
00194                                          uint64_t Address,
00195                                          const void *Decoder);
00196 
00197 static DecodeStatus DecodeL4RSrcDstInstruction(MCInst &Inst,
00198                                                unsigned Insn,
00199                                                uint64_t Address,
00200                                                const void *Decoder);
00201 
00202 static DecodeStatus DecodeL4RSrcDstSrcDstInstruction(MCInst &Inst,
00203                                                      unsigned Insn,
00204                                                      uint64_t Address,
00205                                                      const void *Decoder);
00206 
00207 #include "XCoreGenDisassemblerTables.inc"
00208 
00209 static DecodeStatus DecodeGRRegsRegisterClass(MCInst &Inst,
00210                                               unsigned RegNo,
00211                                               uint64_t Address,
00212                                               const void *Decoder)
00213 {
00214   if (RegNo > 11)
00215     return MCDisassembler::Fail;
00216   unsigned Reg = getReg(Decoder, XCore::GRRegsRegClassID, RegNo);
00217   Inst.addOperand(MCOperand::CreateReg(Reg));
00218   return MCDisassembler::Success;
00219 }
00220 
00221 static DecodeStatus DecodeRRegsRegisterClass(MCInst &Inst,
00222                                              unsigned RegNo,
00223                                              uint64_t Address,
00224                                              const void *Decoder)
00225 {
00226   if (RegNo > 15)
00227     return MCDisassembler::Fail;
00228   unsigned Reg = getReg(Decoder, XCore::RRegsRegClassID, RegNo);
00229   Inst.addOperand(MCOperand::CreateReg(Reg));
00230   return MCDisassembler::Success;
00231 }
00232 
00233 static DecodeStatus DecodeBitpOperand(MCInst &Inst, unsigned Val,
00234                                       uint64_t Address, const void *Decoder) {
00235   if (Val > 11)
00236     return MCDisassembler::Fail;
00237   static unsigned Values[] = {
00238     32 /*bpw*/, 1, 2, 3, 4, 5, 6, 7, 8, 16, 24, 32
00239   };
00240   Inst.addOperand(MCOperand::CreateImm(Values[Val]));
00241   return MCDisassembler::Success;
00242 }
00243 
00244 static DecodeStatus DecodeNegImmOperand(MCInst &Inst, unsigned Val,
00245                                         uint64_t Address, const void *Decoder) {
00246   Inst.addOperand(MCOperand::CreateImm(-(int64_t)Val));
00247   return MCDisassembler::Success;
00248 }
00249 
00250 static DecodeStatus
00251 Decode2OpInstruction(unsigned Insn, unsigned &Op1, unsigned &Op2) {
00252   unsigned Combined = fieldFromInstruction(Insn, 6, 5);
00253   if (Combined < 27)
00254     return MCDisassembler::Fail;
00255   if (fieldFromInstruction(Insn, 5, 1)) {
00256     if (Combined == 31)
00257       return MCDisassembler::Fail;
00258     Combined += 5;
00259   }
00260   Combined -= 27;
00261   unsigned Op1High = Combined % 3;
00262   unsigned Op2High = Combined / 3;
00263   Op1 = (Op1High << 2) | fieldFromInstruction(Insn, 2, 2);
00264   Op2 = (Op2High << 2) | fieldFromInstruction(Insn, 0, 2);
00265   return MCDisassembler::Success;
00266 }
00267 
00268 static DecodeStatus
00269 Decode3OpInstruction(unsigned Insn, unsigned &Op1, unsigned &Op2,
00270                      unsigned &Op3) {
00271   unsigned Combined = fieldFromInstruction(Insn, 6, 5);
00272   if (Combined >= 27)
00273     return MCDisassembler::Fail;
00274 
00275   unsigned Op1High = Combined % 3;
00276   unsigned Op2High = (Combined / 3) % 3;
00277   unsigned Op3High = Combined / 9;
00278   Op1 = (Op1High << 2) | fieldFromInstruction(Insn, 4, 2);
00279   Op2 = (Op2High << 2) | fieldFromInstruction(Insn, 2, 2);
00280   Op3 = (Op3High << 2) | fieldFromInstruction(Insn, 0, 2);
00281   return MCDisassembler::Success;
00282 }
00283 
00284 static DecodeStatus
00285 Decode2OpInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
00286                          const void *Decoder) {
00287   // Try and decode as a 3R instruction.
00288   unsigned Opcode = fieldFromInstruction(Insn, 11, 5);
00289   switch (Opcode) {
00290   case 0x0:
00291     Inst.setOpcode(XCore::STW_2rus);
00292     return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
00293   case 0x1:
00294     Inst.setOpcode(XCore::LDW_2rus);
00295     return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
00296   case 0x2:
00297     Inst.setOpcode(XCore::ADD_3r);
00298     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00299   case 0x3:
00300     Inst.setOpcode(XCore::SUB_3r);
00301     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00302   case 0x4:
00303     Inst.setOpcode(XCore::SHL_3r);
00304     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00305   case 0x5:
00306     Inst.setOpcode(XCore::SHR_3r);
00307     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00308   case 0x6:
00309     Inst.setOpcode(XCore::EQ_3r);
00310     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00311   case 0x7:
00312     Inst.setOpcode(XCore::AND_3r);
00313     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00314   case 0x8:
00315     Inst.setOpcode(XCore::OR_3r);
00316     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00317   case 0x9:
00318     Inst.setOpcode(XCore::LDW_3r);
00319     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00320   case 0x10:
00321     Inst.setOpcode(XCore::LD16S_3r);
00322     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00323   case 0x11:
00324     Inst.setOpcode(XCore::LD8U_3r);
00325     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00326   case 0x12:
00327     Inst.setOpcode(XCore::ADD_2rus);
00328     return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
00329   case 0x13:
00330     Inst.setOpcode(XCore::SUB_2rus);
00331     return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
00332   case 0x14:
00333     Inst.setOpcode(XCore::SHL_2rus);
00334     return Decode2RUSBitpInstruction(Inst, Insn, Address, Decoder);
00335   case 0x15:
00336     Inst.setOpcode(XCore::SHR_2rus);
00337     return Decode2RUSBitpInstruction(Inst, Insn, Address, Decoder);
00338   case 0x16:
00339     Inst.setOpcode(XCore::EQ_2rus);
00340     return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
00341   case 0x17:
00342     Inst.setOpcode(XCore::TSETR_3r);
00343     return Decode3RImmInstruction(Inst, Insn, Address, Decoder);
00344   case 0x18:
00345     Inst.setOpcode(XCore::LSS_3r);
00346     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00347   case 0x19:
00348     Inst.setOpcode(XCore::LSU_3r);
00349     return Decode3RInstruction(Inst, Insn, Address, Decoder);
00350   }
00351   return MCDisassembler::Fail;
00352 }
00353 
00354 static DecodeStatus
00355 Decode2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00356                     const void *Decoder) {
00357   unsigned Op1, Op2;
00358   DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
00359   if (S != MCDisassembler::Success)
00360     return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
00361 
00362   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00363   DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00364   return S;
00365 }
00366 
00367 static DecodeStatus
00368 Decode2RImmInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00369                        const void *Decoder) {
00370   unsigned Op1, Op2;
00371   DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
00372   if (S != MCDisassembler::Success)
00373     return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
00374 
00375   Inst.addOperand(MCOperand::CreateImm(Op1));
00376   DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00377   return S;
00378 }
00379 
00380 static DecodeStatus
00381 DecodeR2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00382                      const void *Decoder) {
00383   unsigned Op1, Op2;
00384   DecodeStatus S = Decode2OpInstruction(Insn, Op2, Op1);
00385   if (S != MCDisassembler::Success)
00386     return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
00387 
00388   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00389   DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00390   return S;
00391 }
00392 
00393 static DecodeStatus
00394 Decode2RSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00395                           const void *Decoder) {
00396   unsigned Op1, Op2;
00397   DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
00398   if (S != MCDisassembler::Success)
00399     return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
00400 
00401   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00402   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00403   DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00404   return S;
00405 }
00406 
00407 static DecodeStatus
00408 DecodeRUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00409                      const void *Decoder) {
00410   unsigned Op1, Op2;
00411   DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
00412   if (S != MCDisassembler::Success)
00413     return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
00414 
00415   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00416   Inst.addOperand(MCOperand::CreateImm(Op2));
00417   return S;
00418 }
00419 
00420 static DecodeStatus
00421 DecodeRUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00422                          const void *Decoder) {
00423   unsigned Op1, Op2;
00424   DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
00425   if (S != MCDisassembler::Success)
00426     return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
00427 
00428   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00429   DecodeBitpOperand(Inst, Op2, Address, Decoder);
00430   return S;
00431 }
00432 
00433 static DecodeStatus
00434 DecodeRUSSrcDstBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00435                                const void *Decoder) {
00436   unsigned Op1, Op2;
00437   DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
00438   if (S != MCDisassembler::Success)
00439     return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
00440 
00441   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00442   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00443   DecodeBitpOperand(Inst, Op2, Address, Decoder);
00444   return S;
00445 }
00446 
00447 static DecodeStatus
00448 DecodeL2OpInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
00449                           const void *Decoder) {
00450   // Try and decode as a L3R / L2RUS instruction.
00451   unsigned Opcode = fieldFromInstruction(Insn, 16, 4) |
00452                     fieldFromInstruction(Insn, 27, 5) << 4;
00453   switch (Opcode) {
00454   case 0x0c:
00455     Inst.setOpcode(XCore::STW_l3r);
00456     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00457   case 0x1c:
00458     Inst.setOpcode(XCore::XOR_l3r);
00459     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00460   case 0x2c:
00461     Inst.setOpcode(XCore::ASHR_l3r);
00462     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00463   case 0x3c:
00464     Inst.setOpcode(XCore::LDAWF_l3r);
00465     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00466   case 0x4c:
00467     Inst.setOpcode(XCore::LDAWB_l3r);
00468     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00469   case 0x5c:
00470     Inst.setOpcode(XCore::LDA16F_l3r);
00471     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00472   case 0x6c:
00473     Inst.setOpcode(XCore::LDA16B_l3r);
00474     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00475   case 0x7c:
00476     Inst.setOpcode(XCore::MUL_l3r);
00477     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00478   case 0x8c:
00479     Inst.setOpcode(XCore::DIVS_l3r);
00480     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00481   case 0x9c:
00482     Inst.setOpcode(XCore::DIVU_l3r);
00483     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00484   case 0x10c:
00485     Inst.setOpcode(XCore::ST16_l3r);
00486     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00487   case 0x11c:
00488     Inst.setOpcode(XCore::ST8_l3r);
00489     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00490   case 0x12c:
00491     Inst.setOpcode(XCore::ASHR_l2rus);
00492     return DecodeL2RUSBitpInstruction(Inst, Insn, Address, Decoder);
00493   case 0x12d:
00494     Inst.setOpcode(XCore::OUTPW_l2rus);
00495     return DecodeL2RUSBitpInstruction(Inst, Insn, Address, Decoder);
00496   case 0x12e:
00497     Inst.setOpcode(XCore::INPW_l2rus);
00498     return DecodeL2RUSBitpInstruction(Inst, Insn, Address, Decoder);
00499   case 0x13c:
00500     Inst.setOpcode(XCore::LDAWF_l2rus);
00501     return DecodeL2RUSInstruction(Inst, Insn, Address, Decoder);
00502   case 0x14c:
00503     Inst.setOpcode(XCore::LDAWB_l2rus);
00504     return DecodeL2RUSInstruction(Inst, Insn, Address, Decoder);
00505   case 0x15c:
00506     Inst.setOpcode(XCore::CRC_l3r);
00507     return DecodeL3RSrcDstInstruction(Inst, Insn, Address, Decoder);
00508   case 0x18c:
00509     Inst.setOpcode(XCore::REMS_l3r);
00510     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00511   case 0x19c:
00512     Inst.setOpcode(XCore::REMU_l3r);
00513     return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
00514   }
00515   return MCDisassembler::Fail;
00516 }
00517 
00518 static DecodeStatus
00519 DecodeL2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00520                                const void *Decoder) {
00521   unsigned Op1, Op2;
00522   DecodeStatus S = Decode2OpInstruction(fieldFromInstruction(Insn, 0, 16),
00523                                         Op1, Op2);
00524   if (S != MCDisassembler::Success)
00525     return DecodeL2OpInstructionFail(Inst, Insn, Address, Decoder);
00526 
00527   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00528   DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00529   return S;
00530 }
00531 
00532 static DecodeStatus
00533 DecodeLR2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00534                                const void *Decoder) {
00535   unsigned Op1, Op2;
00536   DecodeStatus S = Decode2OpInstruction(fieldFromInstruction(Insn, 0, 16),
00537                                         Op1, Op2);
00538   if (S != MCDisassembler::Success)
00539     return DecodeL2OpInstructionFail(Inst, Insn, Address, Decoder);
00540 
00541   DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00542   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00543   return S;
00544 }
00545 
00546 static DecodeStatus
00547 Decode3RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00548                     const void *Decoder) {
00549   unsigned Op1, Op2, Op3;
00550   DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
00551   if (S == MCDisassembler::Success) {
00552     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00553     DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00554     DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
00555   }
00556   return S;
00557 }
00558 
00559 static DecodeStatus
00560 Decode3RImmInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00561                        const void *Decoder) {
00562   unsigned Op1, Op2, Op3;
00563   DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
00564   if (S == MCDisassembler::Success) {
00565     Inst.addOperand(MCOperand::CreateImm(Op1));
00566     DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00567     DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
00568   }
00569   return S;
00570 }
00571 
00572 static DecodeStatus
00573 Decode2RUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00574                       const void *Decoder) {
00575   unsigned Op1, Op2, Op3;
00576   DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
00577   if (S == MCDisassembler::Success) {
00578     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00579     DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00580     Inst.addOperand(MCOperand::CreateImm(Op3));
00581   }
00582   return S;
00583 }
00584 
00585 static DecodeStatus
00586 Decode2RUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00587                       const void *Decoder) {
00588   unsigned Op1, Op2, Op3;
00589   DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
00590   if (S == MCDisassembler::Success) {
00591     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00592     DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00593     DecodeBitpOperand(Inst, Op3, Address, Decoder);
00594   }
00595   return S;
00596 }
00597 
00598 static DecodeStatus
00599 DecodeL3RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00600                      const void *Decoder) {
00601   unsigned Op1, Op2, Op3;
00602   DecodeStatus S =
00603     Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
00604   if (S == MCDisassembler::Success) {
00605     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00606     DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00607     DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
00608   }
00609   return S;
00610 }
00611 
00612 static DecodeStatus
00613 DecodeL3RSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00614                            const void *Decoder) {
00615   unsigned Op1, Op2, Op3;
00616   DecodeStatus S =
00617   Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
00618   if (S == MCDisassembler::Success) {
00619     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00620     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00621     DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00622     DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
00623   }
00624   return S;
00625 }
00626 
00627 static DecodeStatus
00628 DecodeL2RUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00629                        const void *Decoder) {
00630   unsigned Op1, Op2, Op3;
00631   DecodeStatus S =
00632   Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
00633   if (S == MCDisassembler::Success) {
00634     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00635     DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00636     Inst.addOperand(MCOperand::CreateImm(Op3));
00637   }
00638   return S;
00639 }
00640 
00641 static DecodeStatus
00642 DecodeL2RUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00643                            const void *Decoder) {
00644   unsigned Op1, Op2, Op3;
00645   DecodeStatus S =
00646   Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
00647   if (S == MCDisassembler::Success) {
00648     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00649     DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00650     DecodeBitpOperand(Inst, Op3, Address, Decoder);
00651   }
00652   return S;
00653 }
00654 
00655 static DecodeStatus
00656 DecodeL6RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00657                      const void *Decoder) {
00658   unsigned Op1, Op2, Op3, Op4, Op5, Op6;
00659   DecodeStatus S =
00660     Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
00661   if (S != MCDisassembler::Success)
00662     return S;
00663   S = Decode3OpInstruction(fieldFromInstruction(Insn, 16, 16), Op4, Op5, Op6);
00664   if (S != MCDisassembler::Success)
00665     return S;
00666   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00667   DecodeGRRegsRegisterClass(Inst, Op4, Address, Decoder);
00668   DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00669   DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
00670   DecodeGRRegsRegisterClass(Inst, Op5, Address, Decoder);
00671   DecodeGRRegsRegisterClass(Inst, Op6, Address, Decoder);
00672   return S;
00673 }
00674 
00675 static DecodeStatus
00676 DecodeL5RInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
00677                      const void *Decoder) {
00678   // Try and decode as a L6R instruction.
00679   Inst.clear();
00680   unsigned Opcode = fieldFromInstruction(Insn, 27, 5);
00681   switch (Opcode) {
00682   case 0x00:
00683     Inst.setOpcode(XCore::LMUL_l6r);
00684     return DecodeL6RInstruction(Inst, Insn, Address, Decoder);
00685   }
00686   return MCDisassembler::Fail;
00687 }
00688 
00689 static DecodeStatus
00690 DecodeL5RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00691                      const void *Decoder) {
00692   unsigned Op1, Op2, Op3, Op4, Op5;
00693   DecodeStatus S =
00694     Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
00695   if (S != MCDisassembler::Success)
00696     return DecodeL5RInstructionFail(Inst, Insn, Address, Decoder);
00697   S = Decode2OpInstruction(fieldFromInstruction(Insn, 16, 16), Op4, Op5);
00698   if (S != MCDisassembler::Success)
00699     return DecodeL5RInstructionFail(Inst, Insn, Address, Decoder);
00700 
00701   DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00702   DecodeGRRegsRegisterClass(Inst, Op4, Address, Decoder);
00703   DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00704   DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
00705   DecodeGRRegsRegisterClass(Inst, Op5, Address, Decoder);
00706   return S;
00707 }
00708 
00709 static DecodeStatus
00710 DecodeL4RSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00711                            const void *Decoder) {
00712   unsigned Op1, Op2, Op3;
00713   unsigned Op4 = fieldFromInstruction(Insn, 16, 4);
00714   DecodeStatus S =
00715     Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
00716   if (S == MCDisassembler::Success) {
00717     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00718     S = DecodeGRRegsRegisterClass(Inst, Op4, Address, Decoder);
00719   }
00720   if (S == MCDisassembler::Success) {
00721     DecodeGRRegsRegisterClass(Inst, Op4, Address, Decoder);
00722     DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00723     DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
00724   }
00725   return S;
00726 }
00727 
00728 static DecodeStatus
00729 DecodeL4RSrcDstSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
00730                                  const void *Decoder) {
00731   unsigned Op1, Op2, Op3;
00732   unsigned Op4 = fieldFromInstruction(Insn, 16, 4);
00733   DecodeStatus S =
00734   Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
00735   if (S == MCDisassembler::Success) {
00736     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00737     S = DecodeGRRegsRegisterClass(Inst, Op4, Address, Decoder);
00738   }
00739   if (S == MCDisassembler::Success) {
00740     DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
00741     DecodeGRRegsRegisterClass(Inst, Op4, Address, Decoder);
00742     DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
00743     DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
00744   }
00745   return S;
00746 }
00747 
00748 MCDisassembler::DecodeStatus
00749 XCoreDisassembler::getInstruction(MCInst &instr,
00750                                   uint64_t &Size,
00751                                   const MemoryObject &Region,
00752                                   uint64_t Address,
00753                                   raw_ostream &vStream,
00754                                   raw_ostream &cStream) const {
00755   uint16_t insn16;
00756 
00757   if (!readInstruction16(Region, Address, Size, insn16)) {
00758     return Fail;
00759   }
00760 
00761   // Calling the auto-generated decoder function.
00762   DecodeStatus Result = decodeInstruction(DecoderTable16, instr, insn16,
00763                                           Address, this, STI);
00764   if (Result != Fail) {
00765     Size = 2;
00766     return Result;
00767   }
00768 
00769   uint32_t insn32;
00770 
00771   if (!readInstruction32(Region, Address, Size, insn32)) {
00772     return Fail;
00773   }
00774 
00775   // Calling the auto-generated decoder function.
00776   Result = decodeInstruction(DecoderTable32, instr, insn32, Address, this, STI);
00777   if (Result != Fail) {
00778     Size = 4;
00779     return Result;
00780   }
00781 
00782   return Fail;
00783 }
00784 
00785 namespace llvm {
00786   extern Target TheXCoreTarget;
00787 }
00788 
00789 static MCDisassembler *createXCoreDisassembler(const Target &T,
00790                                                const MCSubtargetInfo &STI,
00791                                                MCContext &Ctx) {
00792   return new XCoreDisassembler(STI, Ctx);
00793 }
00794 
00795 extern "C" void LLVMInitializeXCoreDisassembler() {
00796   // Register the disassembler.
00797   TargetRegistry::RegisterMCDisassembler(TheXCoreTarget,
00798                                          createXCoreDisassembler);
00799 }