MCInst.h revision f28d6311896d7de6221465c3918fdc5cbd7d5a6f
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
17#ifndef LLVM_MC_MCINST_H
18#define LLVM_MC_MCINST_H
19
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  };
35  unsigned char Kind;
36
37  union {
38    unsigned RegVal;
39    int64_t ImmVal;
40    struct {
41      unsigned FunctionNo;
42      unsigned BlockNo;
43    } MBBLabel;
44  };
45public:
46
47  MCOperand() : Kind(kInvalid) {}
48  MCOperand(const MCOperand &RHS) { *this = RHS; }
49
50  bool isReg() const { return Kind == kRegister; }
51  bool isImm() const { return Kind == kImmediate; }
52  bool isMBBLabel() const { return Kind == kMBBLabel; }
53
54  /// getReg - Returns the register number.
55  unsigned getReg() const {
56    assert(isReg() && "This is not a register operand!");
57    return RegVal;
58  }
59
60  /// setReg - Set the register number.
61  void setReg(unsigned Reg) {
62    assert(isReg() && "This is not a register operand!");
63    RegVal = Reg;
64  }
65
66  int64_t getImm() const {
67    assert(isImm() && "This is not an immediate");
68    return ImmVal;
69  }
70  void setImm(int64_t Val) {
71    assert(isImm() && "This is not an immediate");
72    ImmVal = Val;
73  }
74
75  unsigned getMBBLabelFunction() const {
76    assert(isMBBLabel() && "Wrong accessor");
77    return MBBLabel.FunctionNo;
78  }
79  unsigned getMBBLabelBlock() const {
80    assert(isMBBLabel() && "Wrong accessor");
81    return MBBLabel.BlockNo;
82  }
83
84  void MakeReg(unsigned Reg) {
85    Kind = kRegister;
86    RegVal = Reg;
87  }
88  void MakeImm(int64_t Val) {
89    Kind = kImmediate;
90    ImmVal = Val;
91  }
92  void MakeMBBLabel(unsigned Fn, unsigned MBB) {
93    Kind = kMBBLabel;
94    MBBLabel.FunctionNo = Fn;
95    MBBLabel.BlockNo = MBB;
96  }
97};
98
99
100/// MCInst - Instances of this class represent a single low-level machine
101/// instruction.
102class MCInst {
103  unsigned Opcode;
104  SmallVector<MCOperand, 8> Operands;
105public:
106  MCInst() : Opcode(~0U) {}
107
108  void setOpcode(unsigned Op) { Opcode = Op; }
109
110  unsigned getOpcode() const { return Opcode; }
111  DebugLoc getDebugLoc() const { return DebugLoc(); }
112
113  const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
114  MCOperand &getOperand(unsigned i) { return Operands[i]; }
115  unsigned getNumOperands() const { return Operands.size(); }
116
117  void addOperand(const MCOperand &Op) {
118    Operands.push_back(Op);
119  }
120
121};
122
123
124} // end namespace llvm
125
126#endif
127