1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- EDInst.h - LLVM Enhanced Disassembler -------------------*- C++ -*-===// 2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// The LLVM Compiler Infrastructure 4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source 6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details. 7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file defines the interface for the Enhanced Disassembly library's 11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// instruction class. The instruction is responsible for vending the string 12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// representation, individual tokens and operands for a single instruction. 13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// 14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===// 15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_EDINST_H 17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_EDINST_H 18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 1919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Support/DataTypes.h" 20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallVector.h" 21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <string> 22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include <vector> 23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm { 25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman class MCInst; 26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman struct EDInstInfo; 27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman struct EDToken; 28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman struct EDDisassembler; 29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman struct EDOperand; 30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifdef __BLOCKS__ 32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef int (^EDTokenVisitor_t)(EDToken *token); 33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// CachedResult - Encapsulates the result of a function along with the validity 36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// of that result, so that slow functions don't need to run twice 37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct CachedResult { 38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// True if the result has been obtained by executing the function 39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool Valid; 40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The result last obtained from the function 41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int Result; 42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// Constructor - Initializes an invalid result 44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CachedResult() : Valid(false) { } 45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// valid - Returns true if the result has been obtained by executing the 46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// function and false otherwise 47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool valid() { return Valid; } 48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// result - Returns the result of the function or an undefined value if 49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// valid() is false 50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int result() { return Result; } 51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// setResult - Sets the result of the function and declares it valid 52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// returning the result (so that setResult() can be called from inside a 53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// return statement) 54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg result - The result of the function 55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int setResult(int result) { Result = result; Valid = true; return result; } 56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// EDInst - Encapsulates a single instruction, which can be queried for its 59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// string representation, as well as its operands and tokens 60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstruct EDInst { 61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The parent disassembler 62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman EDDisassembler &Disassembler; 63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The containing MCInst 64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman llvm::MCInst *Inst; 65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The instruction information provided by TableGen for this instruction 66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const llvm::EDInstInfo *ThisInstInfo; 67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The number of bytes for the machine code representation of the instruction 68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint64_t ByteSize; 69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The result of the stringify() function 71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CachedResult StringifyResult; 72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The string representation of the instruction 73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman std::string String; 74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The order in which operands from the InstInfo's operand information appear 75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// in String 7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman const signed char* OperandOrder; 77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The result of the parseOperands() function 79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CachedResult ParseResult; 80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef llvm::SmallVector<EDOperand*, 5> opvec_t; 81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The instruction's operands 82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman opvec_t Operands; 83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The operand corresponding to the target, if the instruction is a branch 84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int BranchTarget; 85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The operand corresponding to the source, if the instruction is a move 86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int MoveSource; 87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The operand corresponding to the target, if the instruction is a move 88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int MoveTarget; 89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The result of the tokenize() function 91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman CachedResult TokenizeResult; 92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman typedef std::vector<EDToken*> tokvec_t; 93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// The instruction's tokens 94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman tokvec_t Tokens; 95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// Constructor - initializes an instruction given the output of the LLVM 97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// C++ disassembler 98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg inst - The MCInst, which will now be owned by this object 100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg byteSize - The size of the consumed instruction, in bytes 101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg disassembler - The parent disassembler 102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg instInfo - The instruction information produced by the table 103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// generator for this instruction 104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman EDInst(llvm::MCInst *inst, 105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint64_t byteSize, 106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman EDDisassembler &disassembler, 107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman const llvm::EDInstInfo *instInfo); 108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman ~EDInst(); 109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// byteSize - returns the number of bytes consumed by the machine code 111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// representation of the instruction 112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman uint64_t byteSize(); 113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// instID - returns the LLVM instruction ID of the instruction 114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman unsigned instID(); 115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// stringify - populates the String and AsmString members of the instruction, 117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// returning 0 on success or -1 otherwise 118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int stringify(); 119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getString - retrieves a pointer to the string representation of the 120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// instructinon, returning 0 on success or -1 otherwise 121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg str - A reference to a pointer that, on success, is set to point to 123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// the string representation of the instruction; this string is still owned 124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// by the instruction and will be deleted when it is 125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int getString(const char *&str); 126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// isBranch - Returns true if the instruction is a branch 128894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isBranch(); 129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// isMove - Returns true if the instruction is a move 130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman bool isMove(); 131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// parseOperands - populates the Operands member of the instruction, 133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// returning 0 on success or -1 otherwise 134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int parseOperands(); 135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// branchTargetID - returns the ID (suitable for use with getOperand()) of 136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// the target operand if the instruction is a branch, or -1 otherwise 137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int branchTargetID(); 138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// moveSourceID - returns the ID of the source operand if the instruction 139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// is a move, or -1 otherwise 140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int moveSourceID(); 141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// moveTargetID - returns the ID of the target operand if the instruction 142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// is a move, or -1 otherwise 143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int moveTargetID(); 144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// numOperands - returns the number of operands available to retrieve, or -1 146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// on error 147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int numOperands(); 148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getOperand - retrieves an operand from the instruction's operand list by 149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// index, returning 0 on success or -1 on error 150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg operand - A reference whose target is pointed at the operand on 152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// success, although the operand is still owned by the EDInst 153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg index - The index of the operand in the instruction 154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int getOperand(EDOperand *&operand, unsigned int index); 155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// tokenize - populates the Tokens member of the instruction, returning 0 on 157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// success or -1 otherwise 158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int tokenize(); 159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// numTokens - returns the number of tokens in the instruction, or -1 on 160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// error 161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int numTokens(); 162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// getToken - retrieves a token from the instruction's token list by index, 163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// returning 0 on success or -1 on error 164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg token - A reference whose target is pointed at the token on success, 166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// although the token is still owned by the EDInst 167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg index - The index of the token in the instrcutino 168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int getToken(EDToken *&token, unsigned int index); 169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifdef __BLOCKS__ 171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// visitTokens - Visits each token in turn and applies a block to it, 172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// returning 0 if all blocks are visited and/or the block signals 173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// termination by returning 1; returns -1 on error 174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// 175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman /// @arg visitor - The visitor block to apply to all tokens. 176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman int visitTokens(EDTokenVisitor_t visitor); 177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}; 179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // end namespace llvm 181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman 182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif 183