From 759e3fa641d0ad01012d16d913015c9f69c8d2ab Mon Sep 17 00:00:00 2001 From: Roman Divacky Date: Wed, 19 Dec 2012 19:55:47 +0000 Subject: Remove edis - the enhanced disassembler. Fixes PR14654. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@170578 91177308-0d34-0410-b5e6-96231b3b80d8 --- Makefile.rules | 5 - include/llvm-c/EnhancedDisassembly.h | 530 ---------- include/llvm/MC/MCDisassembler.h | 10 - lib/MC/MCDisassembler/CMakeLists.txt | 5 - lib/MC/MCDisassembler/EDDisassembler.cpp | 400 -------- lib/MC/MCDisassembler/EDDisassembler.h | 269 ------ lib/MC/MCDisassembler/EDInfo.h | 84 -- lib/MC/MCDisassembler/EDInst.cpp | 211 ---- lib/MC/MCDisassembler/EDInst.h | 182 ---- lib/MC/MCDisassembler/EDMain.cpp | 276 ------ lib/MC/MCDisassembler/EDOperand.cpp | 315 ------ lib/MC/MCDisassembler/EDOperand.h | 91 -- lib/MC/MCDisassembler/EDToken.cpp | 214 ----- lib/MC/MCDisassembler/EDToken.h | 139 --- lib/Target/ARM/CMakeLists.txt | 1 - lib/Target/ARM/Disassembler/ARMDisassembler.cpp | 16 - lib/Target/ARM/Makefile | 2 +- lib/Target/MBlaze/CMakeLists.txt | 1 - .../MBlaze/Disassembler/MBlazeDisassembler.cpp | 6 - .../MBlaze/Disassembler/MBlazeDisassembler.h | 5 - lib/Target/MBlaze/Makefile | 3 +- lib/Target/Mips/CMakeLists.txt | 1 - lib/Target/Mips/Disassembler/MipsDisassembler.cpp | 11 - lib/Target/Mips/Makefile | 2 +- lib/Target/X86/CMakeLists.txt | 1 - lib/Target/X86/Disassembler/X86Disassembler.cpp | 6 - lib/Target/X86/Disassembler/X86Disassembler.h | 4 - lib/Target/X86/Makefile | 3 +- test/MC/Disassembler/X86/enhanced.txt | 10 - tools/llvm-mc/Disassembler.cpp | 176 ---- tools/llvm-mc/Disassembler.h | 5 - tools/llvm-mc/llvm-mc.cpp | 6 - utils/TableGen/CMakeLists.txt | 1 - utils/TableGen/EDEmitter.cpp | 1012 -------------------- utils/TableGen/TableGen.cpp | 6 - utils/TableGen/TableGenBackends.h | 1 - 36 files changed, 4 insertions(+), 4006 deletions(-) delete mode 100644 include/llvm-c/EnhancedDisassembly.h delete mode 100644 lib/MC/MCDisassembler/EDDisassembler.cpp delete mode 100644 lib/MC/MCDisassembler/EDDisassembler.h delete mode 100644 lib/MC/MCDisassembler/EDInfo.h delete mode 100644 lib/MC/MCDisassembler/EDInst.cpp delete mode 100644 lib/MC/MCDisassembler/EDInst.h delete mode 100644 lib/MC/MCDisassembler/EDMain.cpp delete mode 100644 lib/MC/MCDisassembler/EDOperand.cpp delete mode 100644 lib/MC/MCDisassembler/EDOperand.h delete mode 100644 lib/MC/MCDisassembler/EDToken.cpp delete mode 100644 lib/MC/MCDisassembler/EDToken.h delete mode 100644 test/MC/Disassembler/X86/enhanced.txt delete mode 100644 utils/TableGen/EDEmitter.cpp diff --git a/Makefile.rules b/Makefile.rules index b2b02c25d4..727f8ed3fb 100644 --- a/Makefile.rules +++ b/Makefile.rules @@ -1869,11 +1869,6 @@ $(ObjDir)/%GenDisassemblerTables.inc.tmp : %.td $(ObjDir)/.dir $(LLVM_TBLGEN) $(Echo) "Building $(second; - } - - EDDisassembler *sdd = new EDDisassembler(key); - if (!sdd->valid()) { - delete sdd; - return NULL; - } - - sDisassemblers[key] = sdd; - - return sdd; -} - -EDDisassembler::EDDisassembler(CPUKey &key) : - Valid(false), - HasSemantics(false), - ErrorStream(nulls()), - Key(key), - TgtTriple(key.Triple.c_str()) { - - LLVMSyntaxVariant = getLLVMSyntaxVariant(TgtTriple.getArch(), key.Syntax); - - if (LLVMSyntaxVariant < 0) - return; - - std::string tripleString(key.Triple); - std::string errorString; - - Tgt = TargetRegistry::lookupTarget(key.Triple, - errorString); - - if (!Tgt) - return; - - MRI.reset(Tgt->createMCRegInfo(tripleString)); - - if (!MRI) - return; - - initMaps(*MRI); - - AsmInfo.reset(Tgt->createMCAsmInfo(tripleString)); - - if (!AsmInfo) - return; - - STI.reset(Tgt->createMCSubtargetInfo(tripleString, "", "")); - - if (!STI) - return; - - Disassembler.reset(Tgt->createMCDisassembler(*STI)); - - if (!Disassembler) - return; - - InstInfos = Disassembler->getEDInfo(); - - MII.reset(Tgt->createMCInstrInfo()); - - if (!MII) - return; - - InstString.reset(new std::string); - InstStream.reset(new raw_string_ostream(*InstString)); - InstPrinter.reset(Tgt->createMCInstPrinter(LLVMSyntaxVariant, *AsmInfo, - *MII, *MRI, *STI)); - - if (!InstPrinter) - return; - - GenericAsmLexer.reset(new AsmLexer(*AsmInfo)); - SpecificAsmLexer.reset(Tgt->createMCAsmLexer(*MRI, *AsmInfo)); - SpecificAsmLexer->InstallLexer(*GenericAsmLexer); - - initMaps(*MRI); - - Valid = true; -} - -EDDisassembler::~EDDisassembler() { - if (!valid()) - return; -} - -namespace { - /// EDMemoryObject - a subclass of MemoryObject that allows use of a callback - /// as provided by the sd interface. See MemoryObject. - class EDMemoryObject : public llvm::MemoryObject { - private: - EDByteReaderCallback Callback; - void *Arg; - public: - EDMemoryObject(EDByteReaderCallback callback, - void *arg) : Callback(callback), Arg(arg) { } - ~EDMemoryObject() { } - uint64_t getBase() const { return 0x0; } - uint64_t getExtent() const { return (uint64_t)-1; } - int readByte(uint64_t address, uint8_t *ptr) const { - if (!Callback) - return -1; - - if (Callback(ptr, address, Arg)) - return -1; - - return 0; - } - }; -} - -EDInst *EDDisassembler::createInst(EDByteReaderCallback byteReader, - uint64_t address, - void *arg) { - EDMemoryObject memoryObject(byteReader, arg); - - MCInst* inst = new MCInst; - uint64_t byteSize; - - MCDisassembler::DecodeStatus S; - S = Disassembler->getInstruction(*inst, byteSize, memoryObject, address, - ErrorStream, nulls()); - switch (S) { - case MCDisassembler::Fail: - case MCDisassembler::SoftFail: - // FIXME: Do something different on soft failure mode? - delete inst; - return NULL; - - case MCDisassembler::Success: { - const llvm::EDInstInfo *thisInstInfo = NULL; - - if (InstInfos) { - thisInstInfo = &InstInfos[inst->getOpcode()]; - } - - EDInst* sdInst = new EDInst(inst, byteSize, *this, thisInstInfo); - return sdInst; - } - } - return NULL; -} - -void EDDisassembler::initMaps(const MCRegisterInfo ®isterInfo) { - unsigned numRegisters = registerInfo.getNumRegs(); - unsigned registerIndex; - - for (registerIndex = 0; registerIndex < numRegisters; ++registerIndex) { - const char* registerName = registerInfo.getName(registerIndex); - - RegVec.push_back(registerName); - RegRMap[registerName] = registerIndex; - } - - switch (TgtTriple.getArch()) { - default: - break; - case Triple::x86: - case Triple::x86_64: - stackPointers.insert(registerIDWithName("SP")); - stackPointers.insert(registerIDWithName("ESP")); - stackPointers.insert(registerIDWithName("RSP")); - - programCounters.insert(registerIDWithName("IP")); - programCounters.insert(registerIDWithName("EIP")); - programCounters.insert(registerIDWithName("RIP")); - break; - case Triple::arm: - case Triple::thumb: - stackPointers.insert(registerIDWithName("SP")); - - programCounters.insert(registerIDWithName("PC")); - break; - } -} - -const char *EDDisassembler::nameWithRegisterID(unsigned registerID) const { - if (registerID >= RegVec.size()) - return NULL; - else - return RegVec[registerID].c_str(); -} - -unsigned EDDisassembler::registerIDWithName(const char *name) const { - regrmap_t::const_iterator iter = RegRMap.find(std::string(name)); - if (iter == RegRMap.end()) - return 0; - else - return (*iter).second; -} - -bool EDDisassembler::registerIsStackPointer(unsigned registerID) { - return (stackPointers.find(registerID) != stackPointers.end()); -} - -bool EDDisassembler::registerIsProgramCounter(unsigned registerID) { - return (programCounters.find(registerID) != programCounters.end()); -} - -int EDDisassembler::printInst(std::string &str, MCInst &inst) { - PrinterMutex.acquire(); - - InstPrinter->printInst(&inst, *InstStream, ""); - InstStream->flush(); - str = *InstString; - InstString->clear(); - - PrinterMutex.release(); - - return 0; -} - -static void diag_handler(const SMDiagnostic &diag, void *context) { - if (context) - diag.print("", static_cast(context)->ErrorStream); -} - -int EDDisassembler::parseInst(SmallVectorImpl &operands, - SmallVectorImpl &tokens, - const std::string &str) { - int ret = 0; - - switch (TgtTriple.getArch()) { - default: - return -1; - case Triple::x86: - case Triple::x86_64: - case Triple::arm: - case Triple::thumb: - break; - } - - const char *cStr = str.c_str(); - MemoryBuffer *buf = MemoryBuffer::getMemBuffer(cStr, cStr + strlen(cStr)); - - StringRef instName; - SMLoc instLoc; - - SourceMgr sourceMgr; - sourceMgr.setDiagHandler(diag_handler, static_cast(this)); - sourceMgr.AddNewSourceBuffer(buf, SMLoc()); // ownership of buf handed over - MCContext context(*AsmInfo, *MRI, NULL); - OwningPtr streamer(createNullStreamer(context)); - OwningPtr genericParser(createMCAsmParser(sourceMgr, - context, *streamer, - *AsmInfo)); - - OwningPtr STI(Tgt->createMCSubtargetInfo(Key.Triple.c_str(), "", "")); - OwningPtr - TargetParser(Tgt->createMCAsmParser(*STI, *genericParser)); - - AsmToken OpcodeToken = genericParser->Lex(); - AsmToken NextToken = genericParser->Lex(); // consume next token, because specificParser expects us to - - if (OpcodeToken.is(AsmToken::Identifier)) { - instName = OpcodeToken.getString(); - instLoc = OpcodeToken.getLoc(); - - ParseInstructionInfo Info; - if (NextToken.isNot(AsmToken::Eof) && - TargetParser->ParseInstruction(Info, instName, instLoc, operands)) - ret = -1; - } else { - ret = -1; - } - - ParserMutex.acquire(); - - if (!ret) { - GenericAsmLexer->setBuffer(buf); - - while (SpecificAsmLexer->Lex(), - SpecificAsmLexer->isNot(AsmToken::Eof) && - SpecificAsmLexer->isNot(AsmToken::EndOfStatement)) { - if (SpecificAsmLexer->is(AsmToken::Error)) { - ret = -1; - break; - } - tokens.push_back(SpecificAsmLexer->getTok()); - } - } - - ParserMutex.release(); - - return ret; -} - -int EDDisassembler::llvmSyntaxVariant() const { - return LLVMSyntaxVariant; -} diff --git a/lib/MC/MCDisassembler/EDDisassembler.h b/lib/MC/MCDisassembler/EDDisassembler.h deleted file mode 100644 index 942b9067e6..0000000000 --- a/lib/MC/MCDisassembler/EDDisassembler.h +++ /dev/null @@ -1,269 +0,0 @@ -//===-- EDDisassembler.h - LLVM Enhanced Disassembler -----------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines the interface for the Enhanced Disassembly library's -// disassembler class. The disassembler is responsible for vending individual -// instructions according to a given architecture and disassembly syntax. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_EDDISASSEMBLER_H -#define LLVM_EDDISASSEMBLER_H - -#include "EDInfo.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/ADT/Triple.h" -#include "llvm/Support/Mutex.h" -#include "llvm/Support/raw_ostream.h" -#include -#include -#include -#include - -namespace llvm { -class AsmLexer; -class AsmParser; -class AsmToken; -class MCContext; -class MCAsmInfo; -class MCAsmLexer; -class MCDisassembler; -class MCInst; -class MCInstPrinter; -class MCInstrInfo; -class MCParsedAsmOperand; -class MCRegisterInfo; -class MCStreamer; -class MCSubtargetInfo; -class MCTargetAsmLexer; -class MCTargetAsmParser; -template class SmallVectorImpl; -class SourceMgr; -class Target; - -struct EDInstInfo; -struct EDInst; -struct EDOperand; -struct EDToken; - -typedef int (*EDByteReaderCallback)(uint8_t *byte, uint64_t address, void *arg); - -/// EDDisassembler - Encapsulates a disassembler for a single architecture and -/// disassembly syntax. Also manages the static disassembler registry. -struct EDDisassembler { - typedef enum { - /*! @constant kEDAssemblySyntaxX86Intel Intel syntax for i386 and x86_64. */ - kEDAssemblySyntaxX86Intel = 0, - /*! @constant kEDAssemblySyntaxX86ATT AT&T syntax for i386 and x86_64. */ - kEDAssemblySyntaxX86ATT = 1, - kEDAssemblySyntaxARMUAL = 2 - } AssemblySyntax; - - - //////////////////// - // Static members // - //////////////////// - - /// CPUKey - Encapsulates the descriptor of an architecture/disassembly-syntax - /// pair - struct CPUKey { - /// The architecture type - std::string Triple; - - /// The assembly syntax - AssemblySyntax Syntax; - - /// operator== - Equality operator - bool operator==(const CPUKey &key) const { - return (Triple == key.Triple && - Syntax == key.Syntax); - } - - /// operator< - Less-than operator - bool operator<(const CPUKey &key) const { - return ((Triple < key.Triple) || - ((Triple == key.Triple) && Syntax < (key.Syntax))); - } - }; - - typedef std::map DisassemblerMap_t; - - /// A map from disassembler specifications to disassemblers. Populated - /// lazily. - static DisassemblerMap_t sDisassemblers; - - /// getDisassembler - Returns the specified disassemble, or NULL on failure - /// - /// @arg arch - The desired architecture - /// @arg syntax - The desired disassembly syntax - static EDDisassembler *getDisassembler(llvm::Triple::ArchType arch, - AssemblySyntax syntax); - - /// getDisassembler - Returns the disassembler for a given combination of - /// CPU type, CPU subtype, and assembly syntax, or NULL on failure - /// - /// @arg str - The string representation of the architecture triple, e.g., - /// "x86_64-apple-darwin" - /// @arg syntax - The disassembly syntax for the required disassembler - static EDDisassembler *getDisassembler(llvm::StringRef str, - AssemblySyntax syntax); - - //////////////////////// - // Per-object members // - //////////////////////// - - /// True only if the object has been successfully initialized - bool Valid; - /// True if the disassembler can provide semantic information - bool HasSemantics; - - /// The stream to write errors to - llvm::raw_ostream &ErrorStream; - - /// The triple/syntax pair for the current architecture - CPUKey Key; - /// The Triple fur the current architecture - Triple TgtTriple; - /// The LLVM target corresponding to the disassembler - const llvm::Target *Tgt; - /// The assembly information for the target architecture - llvm::OwningPtr AsmInfo; - /// The subtarget information for the target architecture - llvm::OwningPtr STI; - // The instruction information for the target architecture. - llvm::OwningPtr MII; - // The register information for the target architecture. - llvm::OwningPtr MRI; - /// The disassembler for the target architecture - llvm::OwningPtr Disassembler; - /// The output string for the instruction printer; must be guarded with - /// PrinterMutex - llvm::OwningPtr InstString; - /// The output stream for the disassembler; must be guarded with - /// PrinterMutex - llvm::OwningPtr InstStream; - /// The instruction printer for the target architecture; must be guarded with - /// PrinterMutex when printing - llvm::OwningPtr InstPrinter; - /// The mutex that guards the instruction printer's printing functions, which - /// use a shared stream - llvm::sys::Mutex PrinterMutex; - /// The array of instruction information provided by the TableGen backend for - /// the target architecture - const llvm::EDInstInfo *InstInfos; - /// The target-specific lexer for use in tokenizing strings, in - /// target-independent and target-specific portions - llvm::OwningPtr GenericAsmLexer; - llvm::OwningPtr SpecificAsmLexer; - /// The guard for the above - llvm::sys::Mutex ParserMutex; - /// The LLVM number used for the target disassembly syntax variant - int LLVMSyntaxVariant; - - typedef std::vector regvec_t; - typedef std::map regrmap_t; - - /// A vector of registers for quick mapping from LLVM register IDs to names - regvec_t RegVec; - /// A map of registers for quick mapping from register names to LLVM IDs - regrmap_t RegRMap; - - /// A set of register IDs for aliases of the stack pointer for the current - /// architecture - std::set stackPointers; - /// A set of register IDs for aliases of the program counter for the current - /// architecture - std::set programCounters; - - /// Constructor - initializes a disassembler with all the necessary objects, - /// which come pre-allocated from the registry accessor function - /// - /// @arg key - the architecture and disassembly syntax for the - /// disassembler - EDDisassembler(CPUKey& key); - - /// valid - reports whether there was a failure in the constructor. - bool valid() { - return Valid; - } - - /// hasSemantics - reports whether the disassembler can provide operands and - /// tokens. - bool hasSemantics() { - return HasSemantics; - } - - ~EDDisassembler(); - - /// createInst - creates and returns an instruction given a callback and - /// memory address, or NULL on failure - /// - /// @arg byteReader - A callback function that provides machine code bytes - /// @arg address - The address of the first byte of the instruction, - /// suitable for passing to byteReader - /// @arg arg - An opaque argument for byteReader - EDInst *createInst(EDByteReaderCallback byteReader, - uint64_t address, - void *arg); - - /// initMaps - initializes regVec and regRMap using the provided register - /// info - /// - /// @arg registerInfo - the register information to use as a source - void initMaps(const llvm::MCRegisterInfo ®isterInfo); - /// nameWithRegisterID - Returns the name (owned by the EDDisassembler) of a - /// register for a given register ID, or NULL on failure - /// - /// @arg registerID - the ID of the register to be queried - const char *nameWithRegisterID(unsigned registerID) const; - /// registerIDWithName - Returns the ID of a register for a given register - /// name, or (unsigned)-1 on failure - /// - /// @arg name - The name of the register - unsigned registerIDWithName(const char *name) const; - - /// registerIsStackPointer - reports whether a register ID is an alias for the - /// stack pointer register - /// - /// @arg registerID - The LLVM register ID - bool registerIsStackPointer(unsigned registerID); - /// registerIsStackPointer - reports whether a register ID is an alias for the - /// stack pointer register - /// - /// @arg registerID - The LLVM register ID - bool registerIsProgramCounter(unsigned registerID); - - /// printInst - prints an MCInst to a string, returning 0 on success, or -1 - /// otherwise - /// - /// @arg str - A reference to a string which is filled in with the string - /// representation of the instruction - /// @arg inst - A reference to the MCInst to be printed - int printInst(std::string& str, - llvm::MCInst& inst); - - /// parseInst - extracts operands and tokens from a string for use in - /// tokenizing the string. Returns 0 on success, or -1 otherwise. - /// - /// @arg operands - A reference to a vector that will be filled in with the - /// parsed operands - /// @arg tokens - A reference to a vector that will be filled in with the - /// tokens - /// @arg str - The string representation of the instruction - int parseInst(llvm::SmallVectorImpl &operands, - llvm::SmallVectorImpl &tokens, - const std::string &str); - - /// llvmSyntaxVariant - returns the LLVM syntax variant for this disassembler - int llvmSyntaxVariant() const; -}; - -} // end namespace llvm - -#endif diff --git a/lib/MC/MCDisassembler/EDInfo.h b/lib/MC/MCDisassembler/EDInfo.h deleted file mode 100644 index e43ad16352..0000000000 --- a/lib/MC/MCDisassembler/EDInfo.h +++ /dev/null @@ -1,84 +0,0 @@ -//===-- EDInfo.h - LLVM Enhanced Disassembler -------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_EDINFO_H -#define LLVM_EDINFO_H - -enum { - EDIS_MAX_OPERANDS = 13, - EDIS_MAX_SYNTAXES = 2 -}; - -enum OperandTypes { - kOperandTypeNone, - kOperandTypeImmediate, - kOperandTypeRegister, - kOperandTypeX86Memory, - kOperandTypeX86EffectiveAddress, - kOperandTypeX86PCRelative, - kOperandTypeARMBranchTarget, - kOperandTypeARMSoReg, - kOperandTypeARMSoImm, - kOperandTypeARMRotImm, - kOperandTypeARMSoImm2Part, - kOperandTypeARMPredicate, - kOperandTypeAddrModeImm12, - kOperandTypeLdStSOReg, - kOperandTypeARMAddrMode2, - kOperandTypeARMAddrMode2Offset, - kOperandTypeARMAddrMode3, - kOperandTypeARMAddrMode3Offset, - kOperandTypeARMAddrMode4, - kOperandTypeARMAddrMode5, - kOperandTypeARMAddrMode6, - kOperandTypeARMAddrMode6Offset, - kOperandTypeARMAddrMode7, - kOperandTypeARMAddrModePC, - kOperandTypeARMRegisterList, - kOperandTypeARMDPRRegisterList, - kOperandTypeARMSPRRegisterList, - kOperandTypeARMTBAddrMode, - kOperandTypeThumbITMask, - kOperandTypeThumbAddrModeRegS1, - kOperandTypeThumbAddrModeRegS2, - kOperandTypeThumbAddrModeRegS4, - kOperandTypeThumbAddrModeImmS1, - kOperandTypeThumbAddrModeImmS2, - kOperandTypeThumbAddrModeImmS4, - kOperandTypeThumbAddrModeRR, - kOperandTypeThumbAddrModeSP, - kOperandTypeThumbAddrModePC, - kOperandTypeThumb2AddrModeReg, - kOperandTypeThumb2SoReg, - kOperandTypeThumb2SoImm, - kOperandTypeThumb2AddrModeImm8, - kOperandTypeThumb2AddrModeImm8Offset, - kOperandTypeThumb2AddrModeImm12, - kOperandTypeThumb2AddrModeSoReg, - kOperandTypeThumb2AddrModeImm8s4, - kOperandTypeThumb2AddrModeImm8s4Offset -}; - -enum OperandFlags { - kOperandFlagSource = 0x1, - kOperandFlagTarget = 0x2 -}; - -enum InstructionTypes { - kInstructionTypeNone, - kInstructionTypeMove, - kInstructionTypeBranch, - kInstructionTypePush, - kInstructionTypePop, - kInstructionTypeCall, - kInstructionTypeReturn -}; - - -#endif diff --git a/lib/MC/MCDisassembler/EDInst.cpp b/lib/MC/MCDisassembler/EDInst.cpp deleted file mode 100644 index 4c4fdd2568..0000000000 --- a/lib/MC/MCDisassembler/EDInst.cpp +++ /dev/null @@ -1,211 +0,0 @@ -//===-EDInst.cpp - LLVM Enhanced Disassembler -----------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the Enhanced Disassembly library's instruction class. -// The instruction is responsible for vending the string representation, -// individual tokens, and operands for a single instruction. -// -//===----------------------------------------------------------------------===// - -#include "EDInst.h" -#include "EDDisassembler.h" -#include "EDOperand.h" -#include "EDToken.h" -#include "llvm/MC/EDInstInfo.h" -#include "llvm/MC/MCInst.h" - -using namespace llvm; - -EDInst::EDInst(llvm::MCInst *inst, - uint64_t byteSize, - EDDisassembler &disassembler, - const llvm::EDInstInfo *info) : - Disassembler(disassembler), - Inst(inst), - ThisInstInfo(info), - ByteSize(byteSize), - BranchTarget(-1), - MoveSource(-1), - MoveTarget(-1) { - OperandOrder = ThisInstInfo->operandOrders[Disassembler.llvmSyntaxVariant()]; -} - -EDInst::~EDInst() { - unsigned int index; - unsigned int numOperands = Operands.size(); - - for (index = 0; index < numOperands; ++index) - delete Operands[index]; - - unsigned int numTokens = Tokens.size(); - - for (index = 0; index < numTokens; ++index) - delete Tokens[index]; - - delete Inst; -} - -uint64_t EDInst::byteSize() { - return ByteSize; -} - -int EDInst::stringify() { - if (StringifyResult.valid()) - return StringifyResult.result(); - - if (Disassembler.printInst(String, *Inst)) - return StringifyResult.setResult(-1); - - String.push_back('\n'); - - return StringifyResult.setResult(0); -} - -int EDInst::getString(const char*& str) { - if (stringify()) - return -1; - - str = String.c_str(); - - return 0; -} - -unsigned EDInst::instID() { - return Inst->getOpcode(); -} - -bool EDInst::isBranch() { - if (ThisInstInfo) - return - ThisInstInfo->instructionType == kInstructionTypeBranch || - ThisInstInfo->instructionType == kInstructionTypeCall; - else - return false; -} - -bool EDInst::isMove() { - if (ThisInstInfo) - return ThisInstInfo->instructionType == kInstructionTypeMove; - else - return false; -} - -int EDInst::parseOperands() { - if (ParseResult.valid()) - return ParseResult.result(); - - if (!ThisInstInfo) - return ParseResult.setResult(-1); - - unsigned int opIndex; - unsigned int mcOpIndex = 0; - - for (opIndex = 0; opIndex < ThisInstInfo->numOperands; ++opIndex) { - if (isBranch() && - (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)) { - BranchTarget = opIndex; - } - else if (isMove()) { - if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagSource) - MoveSource = opIndex; - else if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget) - MoveTarget = opIndex; - } - - EDOperand *operand = new EDOperand(Disassembler, *this, opIndex, mcOpIndex); - - Operands.push_back(operand); - } - - return ParseResult.setResult(0); -} - -int EDInst::branchTargetID() { - if (parseOperands()) - return -1; - return BranchTarget; -} - -int EDInst::moveSourceID() { - if (parseOperands()) - return -1; - return MoveSource; -} - -int EDInst::moveTargetID() { - if (parseOperands()) - return -1; - return MoveTarget; -} - -int EDInst::numOperands() { - if (parseOperands()) - return -1; - return Operands.size(); -} - -int EDInst::getOperand(EDOperand *&operand, unsigned int index) { - if (parseOperands()) - return -1; - - if (index >= Operands.size()) - return -1; - - operand = Operands[index]; - return 0; -} - -int EDInst::tokenize() { - if (TokenizeResult.valid()) - return TokenizeResult.result(); - - if (ThisInstInfo == NULL) - return TokenizeResult.setResult(-1); - - if (stringify()) - return TokenizeResult.setResult(-1); - - return TokenizeResult.setResult(EDToken::tokenize(Tokens, - String, - OperandOrder, - Disassembler)); - -} - -int EDInst::numTokens() { - if (tokenize()) - return -1; - return Tokens.size(); -} - -int EDInst::getToken(EDToken *&token, unsigned int index) { - if (tokenize()) - return -1; - token = Tokens[index]; - return 0; -} - -#ifdef __BLOCKS__ -int EDInst::visitTokens(EDTokenVisitor_t visitor) { - if (tokenize()) - return -1; - - tokvec_t::iterator iter; - - for (iter = Tokens.begin(); iter != Tokens.end(); ++iter) { - int ret = visitor(*iter); - if (ret == 1) - return 0; - if (ret != 0) - return -1; - } - - return 0; -} -#endif diff --git a/lib/MC/MCDisassembler/EDInst.h b/lib/MC/MCDisassembler/EDInst.h deleted file mode 100644 index cc0b562130..0000000000 --- a/lib/MC/MCDisassembler/EDInst.h +++ /dev/null @@ -1,182 +0,0 @@ -//===-- EDInst.h - LLVM Enhanced Disassembler -------------------*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines the interface for the Enhanced Disassembly library's -// instruction class. The instruction is responsible for vending the string -// representation, individual tokens and operands for a single instruction. -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_EDINST_H -#define LLVM_EDINST_H - -#include "llvm/ADT/SmallVector.h" -#include "llvm/Support/DataTypes.h" -#include -#include - -namespace llvm { - class MCInst; - struct EDInstInfo; - struct EDToken; - struct EDDisassembler; - struct EDOperand; - -#ifdef __BLOCKS__ - typedef int (^EDTokenVisitor_t)(EDToken *token); -#endif - -/// CachedResult - Encapsulates the result of a function along with the validity -/// of that result, so that slow functions don't need to run twice -struct CachedResult { - /// True if the result has been obtained by executing the function - bool Valid; - /// The result last obtained from the function - int Result; - - /// Constructor - Initializes an invalid result - CachedResult() : Valid(false) { } - /// valid - Returns true if the result has been obtained by executing the - /// function and false otherwise - bool valid() { return Valid; } - /// result - Returns the result of the function or an undefined value if - /// valid() is false - int result() { return Result; } - /// setResult - Sets the result of the function and declares it valid - /// returning the result (so that setResult() can be called from inside a - /// return statement) - /// @arg result - The result of the function - int setResult(int result) { Result = result; Valid = true; return result; } -}; - -/// EDInst - Encapsulates a single instruction, which can be queried for its -/// string representation, as well as its operands and tokens -struct EDInst { - /// The parent disassembler - EDDisassembler &Disassembler; - /// The containing MCInst - llvm::MCInst *Inst; - /// The instruction information provided by TableGen for this instruction - const llvm::EDInstInfo *ThisInstInfo; - /// The number of bytes for the machine code representation of the instruction - uint64_t ByteSize; - - /// The result of the stringify() function - CachedResult StringifyResult; - /// The string representation of the instruction - std::string String; - /// The order in which operands from the InstInfo's operand information appear - /// in String - const signed char* OperandOrder; - - /// The result of the parseOperands() function - CachedResult ParseResult; - typedef llvm::SmallVector opvec_t; - /// The instruction's operands - opvec_t Operands; - /// The operand corresponding to the target, if the instruction is a branch - int BranchTarget; - /// The operand corresponding to the source, if the instruction is a move - int MoveSource; - /// The operand corresponding to the target, if the instruction is a move - int MoveTarget; - - /// The result of the tokenize() function - CachedResult TokenizeResult; - typedef std::vector tokvec_t; - /// The instruction's tokens - tokvec_t Tokens; - - /// Constructor - initializes an instruction given the output of the LLVM - /// C++ disassembler - /// - /// @arg inst - The MCInst, which will now be owned by this object - /// @arg byteSize - The size of the consumed instruction, in bytes - /// @arg disassembler - The parent disassembler - /// @arg instInfo - The instruction information produced by the table - /// generator for this instruction - EDInst(llvm::MCInst *inst, - uint64_t byteSize, - EDDisassembler &disassembler, - const llvm::EDInstInfo *instInfo); - ~EDInst(); - - /// byteSize - returns the number of bytes consumed by the machine code - /// representation of the instruction - uint64_t byteSize(); - /// instID - returns the LLVM instruction ID of the instruction - unsigned instID(); - - /// stringify - populates the String and AsmString members of the instruction, - /// returning 0 on success or -1 otherwise - int stringify(); - /// getString - retrieves a pointer to the string representation of the - /// instructinon, returning 0 on success or -1 otherwise - /// - /// @arg str - A reference to a pointer that, on success, is set to point to - /// the string representation of the instruction; this string is still owned - /// by the instruction and will be deleted when it is - int getString(const char *&str); - - /// isBranch - Returns true if the instruction is a branch - bool isBranch(); - /// isMove - Returns true if the instruction is a move - bool isMove(); - - /// parseOperands - populates the Operands member of the instruction, - /// returning 0 on success or -1 otherwise - int parseOperands(); - /// branchTargetID - returns the ID (suitable for use with getOperand()) of - /// the target operand if the instruction is a branch, or -1 otherwise - int branchTargetID(); - /// moveSourceID - returns the ID of the source operand if the instruction - /// is a move, or -1 otherwise - int moveSourceID(); - /// moveTargetID - returns the ID of the target operand if the instruction - /// is a move, or -1 otherwise - int moveTargetID(); - - /// numOperands - returns the number of operands available to retrieve, or -1 - /// on error - int numOperands(); - /// getOperand - retrieves an operand from the instruction's operand list by - /// index, returning 0 on success or -1 on error - /// - /// @arg operand - A reference whose target is pointed at the operand on - /// success, although the operand is still owned by the EDInst - /// @arg index - The index of the operand in the instruction - int getOperand(EDOperand *&operand, unsigned int index); - - /// tokenize - populates the Tokens member of the instruction, returning 0 on - /// success or -1 otherwise - int tokenize(); - /// numTokens - returns the number of tokens in the instruction, or -1 on - /// error - int numTokens(); - /// getToken - retrieves a token from the instruction's token list by index, - /// returning 0 on success or -1 on error - /// - /// @arg token - A reference whose target is pointed at the token on success, - /// although the token is still owned by the EDInst - /// @arg index - The index of the token in the instrcutino - int getToken(EDToken *&token, unsigned int index); - -#ifdef __BLOCKS__ - /// visitTokens - Visits each token in turn and applies a block to it, - /// returning 0 if all blocks are visited and/or the block signals - /// termination by returning 1; returns -1 on error - /// - /// @arg visitor - The visitor block to apply to all tokens. - int visitTokens(EDTokenVisitor_t visitor); -#endif -}; - -} // end namespace llvm - -#endif diff --git a/lib/MC/MCDisassembler/EDMain.cpp b/lib/MC/MCDisassembler/EDMain.cpp deleted file mode 100644 index 5c065dbf0c..0000000000 --- a/lib/MC/MCDisassembler/EDMain.cpp +++ /dev/null @@ -1,276 +0,0 @@ -//===-- EDMain.cpp - LLVM Enhanced Disassembly C API ----------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file implements the enhanced disassembler's public C API. -// -//===----------------------------------------------------------------------===// - -#include "EDDisassembler.h" -#include "EDInst.h" -#include "EDOperand.h" -#include "EDToken.h" -#include "llvm-c/EnhancedDisassembly.h" -using namespace llvm; - -int EDGetDisassembler(EDDisassemblerRef *disassembler, - const char *triple, - EDAssemblySyntax_t syntax) { - EDDisassembler::AssemblySyntax Syntax; - switch (syntax) { - default: llvm_unreachable("Unknown assembly syntax!"); - case kEDAssemblySyntaxX86Intel: - Syntax = EDDisassembler::kEDAssemblySyntaxX86Intel; - break; - case kEDAssemblySyntaxX86ATT: - Syntax = EDDisassembler::kEDAssemblySyntaxX86ATT; - break; - case kEDAssemblySyntaxARMUAL: - Syntax = EDDisassembler::kEDAssemblySyntaxARMUAL; - break; - } - - EDDisassemblerRef ret = EDDisassembler::getDisassembler(triple, Syntax); - - if (!ret) - return -1; - *disassembler = ret; - return 0; -} - -int EDGetRegisterName(const char** regName, - EDDisassemblerRef disassembler, - unsigned regID) { - const char *name = ((EDDisassembler*)disassembler)->nameWithRegisterID(regID); - if (!name) - return -1; - *regName = name; - return 0; -} - -int EDRegisterIsStackPointer(EDDisassemblerRef disassembler, - unsigned regID) { - return ((EDDisassembler*)disassembler)->registerIsStackPointer(regID) ? 1 : 0; -} - -int EDRegisterIsProgramCounter(EDDisassemblerRef disassembler, - unsigned regID) { - return ((EDDisassembler*)disassembler)->registerIsProgramCounter(regID) ? 1:0; -} - -unsigned int EDCreateInsts(EDInstRef *insts, - unsigned int count, - EDDisassemblerRef disassembler, - ::EDByteReaderCallback byteReader, - uint64_t address, - void *arg) { - unsigned int index; - - for (index = 0; index < count; ++index) { - EDInst *inst = ((EDDisassembler*)disassembler)->createInst(byteReader, - address, arg); - - if (!inst) - return index; - - insts[index] = inst; - address += inst->byteSize(); - } - - return count; -} - -void EDReleaseInst(EDInstRef inst) { - delete ((EDInst*)inst); -} - -int EDInstByteSize(EDInstRef inst) { - return ((EDInst*)inst)->byteSize(); -} - -int EDGetInstString(const char **buf, - EDInstRef inst) { - return ((EDInst*)inst)->getString(*buf); -} - -int EDInstID(unsigned *instID, EDInstRef inst) { - *instID = ((EDInst*)inst)->instID(); - return 0; -} - -int EDInstIsBranch(EDInstRef inst) { - return ((EDInst*)inst)->isBranch(); -} - -int EDInstIsMove(EDInstRef inst) { - return ((EDInst*)inst)->isMove(); -} - -int EDBranchTargetID(EDInstRef inst) { - return ((EDInst*)inst)->branchTargetID(); -} - -int EDMoveSourceID(EDInstRef inst) { - return ((EDInst*)inst)->moveSourceID(); -} - -int EDMoveTargetID(EDInstRef inst) { - return ((EDInst*)