Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
5.4 KiB

  1. //===-- llvm/MC/MCInst.h - MCInst class -------------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains the declaration of the MCInst and MCOperand classes, which
  11. // is the basic representation used to represent low-level machine code
  12. // instructions.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_MC_MCINST_H
  16. #define LLVM_MC_MCINST_H
  17. #include "llvm/ADT/SmallVector.h"
  18. #include "llvm/ADT/StringRef.h"
  19. #include "llvm/Support/DataTypes.h"
  20. #include "llvm/Support/SMLoc.h"
  21. namespace llvm {
  22. class raw_ostream;
  23. class MCAsmInfo;
  24. class MCInstPrinter;
  25. class MCExpr;
  26. class MCInst;
  27. /// MCOperand - Instances of this class represent operands of the MCInst class.
  28. /// This is a simple discriminated union.
  29. class MCOperand {
  30. enum MachineOperandType {
  31. kInvalid, ///< Uninitialized.
  32. kRegister, ///< Register operand.
  33. kImmediate, ///< Immediate operand.
  34. kFPImmediate, ///< Floating-point immediate operand.
  35. kExpr, ///< Relocatable immediate operand.
  36. kInst ///< Sub-instruction operand.
  37. };
  38. unsigned char Kind;
  39. union {
  40. unsigned RegVal;
  41. int64_t ImmVal;
  42. double FPImmVal;
  43. const MCExpr *ExprVal;
  44. const MCInst *InstVal;
  45. };
  46. public:
  47. MCOperand() : Kind(kInvalid), FPImmVal(0.0) {}
  48. bool isValid() const { return Kind != kInvalid; }
  49. bool isReg() const { return Kind == kRegister; }
  50. bool isImm() const { return Kind == kImmediate; }
  51. bool isFPImm() const { return Kind == kFPImmediate; }
  52. bool isExpr() const { return Kind == kExpr; }
  53. bool isInst() const { return Kind == kInst; }
  54. /// getReg - Returns the register number.
  55. unsigned getReg() const {
  56. assert(isReg() && "This is not a register operand!");
  57. return RegVal;
  58. }
  59. /// setReg - Set the register number.
  60. void setReg(unsigned Reg) {
  61. assert(isReg() && "This is not a register operand!");
  62. RegVal = Reg;
  63. }
  64. int64_t getImm() const {
  65. assert(isImm() && "This is not an immediate");
  66. return ImmVal;
  67. }
  68. void setImm(int64_t Val) {
  69. assert(isImm() && "This is not an immediate");
  70. ImmVal = Val;
  71. }
  72. double getFPImm() const {
  73. assert(isFPImm() && "This is not an FP immediate");
  74. return FPImmVal;
  75. }
  76. void setFPImm(double Val) {
  77. assert(isFPImm() && "This is not an FP immediate");
  78. FPImmVal = Val;
  79. }
  80. const MCExpr *getExpr() const {
  81. assert(isExpr() && "This is not an expression");
  82. return ExprVal;
  83. }
  84. void setExpr(const MCExpr *Val) {
  85. assert(isExpr() && "This is not an expression");
  86. ExprVal = Val;
  87. }
  88. const MCInst *getInst() const {
  89. assert(isInst() && "This is not a sub-instruction");
  90. return InstVal;
  91. }
  92. void setInst(const MCInst *Val) {
  93. assert(isInst() && "This is not a sub-instruction");
  94. InstVal = Val;
  95. }
  96. static MCOperand CreateReg(unsigned Reg) {
  97. MCOperand Op;
  98. Op.Kind = kRegister;
  99. Op.RegVal = Reg;
  100. return Op;
  101. }
  102. static MCOperand CreateImm(int64_t Val) {
  103. MCOperand Op;
  104. Op.Kind = kImmediate;
  105. Op.ImmVal = Val;
  106. return Op;
  107. }
  108. static MCOperand CreateFPImm(double Val) {
  109. MCOperand Op;
  110. Op.Kind = kFPImmediate;
  111. Op.FPImmVal = Val;
  112. return Op;
  113. }
  114. static MCOperand CreateExpr(const MCExpr *Val) {
  115. MCOperand Op;
  116. Op.Kind = kExpr;
  117. Op.ExprVal = Val;
  118. return Op;
  119. }
  120. static MCOperand CreateInst(const MCInst *Val) {
  121. MCOperand Op;
  122. Op.Kind = kInst;
  123. Op.InstVal = Val;
  124. return Op;
  125. }
  126. void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
  127. void dump() const;
  128. };
  129. template <> struct isPodLike<MCOperand> { static const bool value = true; };
  130. /// MCInst - Instances of this class represent a single low-level machine
  131. /// instruction.
  132. class MCInst {
  133. unsigned Opcode;
  134. SMLoc Loc;
  135. SmallVector<MCOperand, 8> Operands;
  136. public:
  137. MCInst() : Opcode(0) {}
  138. void setOpcode(unsigned Op) { Opcode = Op; }
  139. unsigned getOpcode() const { return Opcode; }
  140. void setLoc(SMLoc loc) { Loc = loc; }
  141. SMLoc getLoc() const { return Loc; }
  142. const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
  143. MCOperand &getOperand(unsigned i) { return Operands[i]; }
  144. unsigned getNumOperands() const { return Operands.size(); }
  145. void addOperand(const MCOperand &Op) {
  146. Operands.push_back(Op);
  147. }
  148. void clear() { Operands.clear(); }
  149. size_t size() { return Operands.size(); }
  150. typedef SmallVectorImpl<MCOperand>::iterator iterator;
  151. iterator begin() { return Operands.begin(); }
  152. iterator end() { return Operands.end(); }
  153. iterator insert(iterator I, const MCOperand &Op) {
  154. return Operands.insert(I, Op);
  155. }
  156. void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
  157. void dump() const;
  158. /// \brief Dump the MCInst as prettily as possible using the additional MC
  159. /// structures, if given. Operators are separated by the \p Separator
  160. /// string.
  161. void dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI = 0,
  162. const MCInstPrinter *Printer = 0,
  163. StringRef Separator = " ") const;
  164. };
  165. inline raw_ostream& operator<<(raw_ostream &OS, const MCOperand &MO) {
  166. MO.print(OS, 0);
  167. return OS;
  168. }
  169. inline raw_ostream& operator<<(raw_ostream &OS, const MCInst &MI) {
  170. MI.print(OS, 0);
  171. return OS;
  172. }
  173. } // end namespace llvm
  174. #endif