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