LLVM API Documentation
00001 //===-- llvm/MC/MCInst.h - MCInst class -------------------------*- 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 // This file contains the declaration of the MCInst and MCOperand classes, which 00011 // is the basic representation used to represent low-level machine code 00012 // instructions. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_MC_MCINST_H 00017 #define LLVM_MC_MCINST_H 00018 00019 #include "llvm/MC/MCValue.h" 00020 #include "llvm/ADT/SmallVector.h" 00021 #include "llvm/Support/DataTypes.h" 00022 #include "llvm/Support/DebugLoc.h" 00023 00024 namespace llvm { 00025 00026 /// MCOperand - Instances of this class represent operands of the MCInst class. 00027 /// This is a simple discriminated union. 00028 class MCOperand { 00029 enum MachineOperandType { 00030 kInvalid, ///< Uninitialized. 00031 kRegister, ///< Register operand. 00032 kImmediate, ///< Immediate operand. 00033 kMBBLabel, ///< Basic block label. 00034 kMCValue ///< Relocatable immediate operand. 00035 }; 00036 unsigned char Kind; 00037 00038 union { 00039 unsigned RegVal; 00040 int64_t ImmVal; 00041 MCValue MCValueVal; 00042 struct { 00043 unsigned FunctionNo; 00044 unsigned BlockNo; 00045 } MBBLabel; 00046 }; 00047 public: 00048 00049 MCOperand() : Kind(kInvalid) {} 00050 MCOperand(const MCOperand &RHS) { *this = RHS; } 00051 00052 bool isValid() const { return Kind != kInvalid; } 00053 bool isReg() const { return Kind == kRegister; } 00054 bool isImm() const { return Kind == kImmediate; } 00055 bool isMBBLabel() const { return Kind == kMBBLabel; } 00056 bool isMCValue() const { return Kind == kMCValue; } 00057 00058 /// getReg - Returns the register number. 00059 unsigned getReg() const { 00060 assert(isReg() && "This is not a register operand!"); 00061 return RegVal; 00062 } 00063 00064 /// setReg - Set the register number. 00065 void setReg(unsigned Reg) { 00066 assert(isReg() && "This is not a register operand!"); 00067 RegVal = Reg; 00068 } 00069 00070 int64_t getImm() const { 00071 assert(isImm() && "This is not an immediate"); 00072 return ImmVal; 00073 } 00074 void setImm(int64_t Val) { 00075 assert(isImm() && "This is not an immediate"); 00076 ImmVal = Val; 00077 } 00078 00079 unsigned getMBBLabelFunction() const { 00080 assert(isMBBLabel() && "Wrong accessor"); 00081 return MBBLabel.FunctionNo; 00082 } 00083 unsigned getMBBLabelBlock() const { 00084 assert(isMBBLabel() && "Wrong accessor"); 00085 return MBBLabel.BlockNo; 00086 } 00087 00088 const MCValue &getMCValue() const { 00089 assert(isMCValue() && "This is not an MCValue"); 00090 return MCValueVal; 00091 } 00092 void setMCValue(const MCValue &Val) { 00093 assert(isMCValue() && "This is not an MCValue"); 00094 MCValueVal = Val; 00095 } 00096 00097 static MCOperand CreateReg(unsigned Reg) { 00098 MCOperand Op; 00099 Op.Kind = kRegister; 00100 Op.RegVal = Reg; 00101 return Op; 00102 } 00103 static MCOperand CreateImm(int64_t Val) { 00104 MCOperand Op; 00105 Op.Kind = kImmediate; 00106 Op.ImmVal = Val; 00107 return Op; 00108 } 00109 static MCOperand CreateMBBLabel(unsigned Fn, unsigned MBB) { 00110 MCOperand Op; 00111 Op.Kind = kMBBLabel; 00112 Op.MBBLabel.FunctionNo = Fn; 00113 Op.MBBLabel.BlockNo = MBB; 00114 return Op; 00115 } 00116 static MCOperand CreateMCValue(const MCValue &Val) { 00117 MCOperand Op; 00118 Op.Kind = kMCValue; 00119 Op.MCValueVal = Val; 00120 return Op; 00121 } 00122 }; 00123 00124 00125 /// MCInst - Instances of this class represent a single low-level machine 00126 /// instruction. 00127 class MCInst { 00128 unsigned Opcode; 00129 SmallVector<MCOperand, 8> Operands; 00130 public: 00131 MCInst() : Opcode(~0U) {} 00132 00133 void setOpcode(unsigned Op) { Opcode = Op; } 00134 00135 unsigned getOpcode() const { return Opcode; } 00136 DebugLoc getDebugLoc() const { return DebugLoc(); } 00137 00138 const MCOperand &getOperand(unsigned i) const { return Operands[i]; } 00139 MCOperand &getOperand(unsigned i) { return Operands[i]; } 00140 unsigned getNumOperands() const { return Operands.size(); } 00141 00142 void addOperand(const MCOperand &Op) { 00143 Operands.push_back(Op); 00144 } 00145 }; 00146 00147 00148 } // end namespace llvm 00149 00150 #endif