MCInst.h revision 8c2eebe4074ef218b30d94358f6b2e45c079605c
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
16#ifndef LLVM_MC_MCINST_H
17#define LLVM_MC_MCINST_H
18
19#include "llvm/ADT/SmallVector.h"
20#include "llvm/Support/DataTypes.h"
21#include "llvm/Support/DebugLoc.h"
22
23namespace llvm {
24class raw_ostream;
25class MCExpr;
26
27/// MCOperand - Instances of this class represent operands of the MCInst class.
28/// This is a simple discriminated union.
29class MCOperand {
30  enum MachineOperandType {
31    kInvalid,                 ///< Uninitialized.
32    kRegister,                ///< Register operand.
33    kImmediate,               ///< Immediate operand.
34    kMBBLabel,                ///< Basic block label.
35    kExpr                     ///< Relocatable immediate operand.
36  };
37  unsigned char Kind;
38
39  union {
40    unsigned RegVal;
41    int64_t ImmVal;
42    const MCExpr *ExprVal;
43    struct {
44      unsigned FunctionNo;
45      unsigned BlockNo;
46    } MBBLabel;
47  };
48public:
49
50  MCOperand() : Kind(kInvalid) {}
51  MCOperand(const MCOperand &RHS) { *this = RHS; }
52
53  bool isValid() const { return Kind != kInvalid; }
54  bool isReg() const { return Kind == kRegister; }
55  bool isImm() const { return Kind == kImmediate; }
56  bool isMBBLabel() const { return Kind == kMBBLabel; }
57  bool isExpr() const { return Kind == kExpr; }
58
59  /// getReg - Returns the register number.
60  unsigned getReg() const {
61    assert(isReg() && "This is not a register operand!");
62    return RegVal;
63  }
64
65  /// setReg - Set the register number.
66  void setReg(unsigned Reg) {
67    assert(isReg() && "This is not a register operand!");
68    RegVal = Reg;
69  }
70
71  int64_t getImm() const {
72    assert(isImm() && "This is not an immediate");
73    return ImmVal;
74  }
75  void setImm(int64_t Val) {
76    assert(isImm() && "This is not an immediate");
77    ImmVal = Val;
78  }
79
80  unsigned getMBBLabelFunction() const {
81    assert(isMBBLabel() && "This is not a machine basic block");
82    return MBBLabel.FunctionNo;
83  }
84  unsigned getMBBLabelBlock() const {
85    assert(isMBBLabel() && "This is not a machine basic block");
86    return MBBLabel.BlockNo;
87  }
88
89  const MCExpr *getExpr() const {
90    assert(isExpr() && "This is not an expression");
91    return ExprVal;
92  }
93  void setExpr(const MCExpr *Val) {
94    assert(isExpr() && "This is not an expression");
95    ExprVal = Val;
96  }
97
98  static MCOperand CreateReg(unsigned Reg) {
99    MCOperand Op;
100    Op.Kind = kRegister;
101    Op.RegVal = Reg;
102    return Op;
103  }
104  static MCOperand CreateImm(int64_t Val) {
105    MCOperand Op;
106    Op.Kind = kImmediate;
107    Op.ImmVal = Val;
108    return Op;
109  }
110  static MCOperand CreateMBBLabel(unsigned Fn, unsigned MBB) {
111    MCOperand Op;
112    Op.Kind = kMBBLabel;
113    Op.MBBLabel.FunctionNo = Fn;
114    Op.MBBLabel.BlockNo = MBB;
115    return Op;
116  }
117  static MCOperand CreateExpr(const MCExpr *Val) {
118    MCOperand Op;
119    Op.Kind = kExpr;
120    Op.ExprVal = Val;
121    return Op;
122  }
123
124  void print(raw_ostream &OS) const;
125  void dump() const;
126};
127
128
129/// MCInst - Instances of this class represent a single low-level machine
130/// instruction.
131class MCInst {
132  unsigned Opcode;
133  SmallVector<MCOperand, 8> Operands;
134public:
135  MCInst() : Opcode(~0U) {}
136
137  void setOpcode(unsigned Op) { Opcode = Op; }
138
139  unsigned getOpcode() const { return Opcode; }
140  DebugLoc getDebugLoc() const { return DebugLoc(); }
141
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
146  void addOperand(const MCOperand &Op) {
147    Operands.push_back(Op);
148  }
149
150  void print(raw_ostream &OS) const;
151  void dump() const;
152};
153
154
155} // end namespace llvm
156
157#endif
158