Coverage Report

Created: 2025-07-15 06:22

/src/keystone/llvm/include/llvm/MC/MCInst.h
Line
Count
Source (jump to first uncovered line)
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/ADT/StringRef.h"
21
#include "llvm/Support/DataTypes.h"
22
#include "llvm/Support/SMLoc.h"
23
24
namespace llvm_ks {
25
class raw_ostream;
26
class MCAsmInfo;
27
class MCExpr;
28
class MCInst;
29
30
/// \brief Instances of this class represent operands of the MCInst class.
31
/// This is a simple discriminated union.
32
class MCOperand {
33
  enum MachineOperandType : unsigned char {
34
    kInvalid,     ///< Uninitialized.
35
    kRegister,    ///< Register operand.
36
    kImmediate,   ///< Immediate operand.
37
    kFPImmediate, ///< Floating-point immediate operand.
38
    kExpr,        ///< Relocatable immediate operand.
39
    kInst         ///< Sub-instruction operand.
40
  };
41
  MachineOperandType Kind;
42
43
  union {
44
    unsigned RegVal;
45
    int64_t ImmVal;
46
    double FPImmVal;
47
    const MCExpr *ExprVal;
48
    const MCInst *InstVal;
49
  };
50
51
public:
52
797k
  MCOperand() : Kind(kInvalid), FPImmVal(0.0) {}
53
54
0
  bool isValid() const { return Kind != kInvalid; }
55
839k
  bool isReg() const { return Kind == kRegister; }
56
754k
  bool isImm() const { return Kind == kImmediate; }
57
1.14k
  bool isFPImm() const { return Kind == kFPImmediate; }
58
961k
  bool isExpr() const { return Kind == kExpr; }
59
102k
  bool isInst() const { return Kind == kInst; }
60
61
  /// \brief Returns the register number.
62
477k
  unsigned getReg() const {
63
477k
    assert(isReg() && "This is not a register operand!");
64
477k
    return RegVal;
65
477k
  }
66
67
  /// \brief Set the register number.
68
1
  void setReg(unsigned Reg) {
69
1
    assert(isReg() && "This is not a register operand!");
70
1
    RegVal = Reg;
71
1
  }
72
73
318k
  int64_t getImm() const {
74
318k
    assert(isImm() && "This is not an immediate");
75
318k
    return ImmVal;
76
318k
  }
77
3.56k
  void setImm(int64_t Val) {
78
3.56k
    assert(isImm() && "This is not an immediate");
79
3.56k
    ImmVal = Val;
80
3.56k
  }
81
82
0
  double getFPImm() const {
83
0
    assert(isFPImm() && "This is not an FP immediate");
84
0
    return FPImmVal;
85
0
  }
86
87
0
  void setFPImm(double Val) {
88
0
    assert(isFPImm() && "This is not an FP immediate");
89
0
    FPImmVal = Val;
90
0
  }
91
92
209k
  const MCExpr *getExpr() const {
93
209k
    assert(isExpr() && "This is not an expression");
94
209k
    return ExprVal;
95
209k
  }
96
0
  void setExpr(const MCExpr *Val) {
97
0
    assert(isExpr() && "This is not an expression");
98
0
    ExprVal = Val;
99
0
  }
100
101
102k
  const MCInst *getInst() const {
102
102k
    assert(isInst() && "This is not a sub-instruction");
103
102k
    return InstVal;
104
102k
  }
105
17
  void setInst(const MCInst *Val) {
106
17
    assert(isInst() && "This is not a sub-instruction");
107
17
    InstVal = Val;
108
17
  }
109
110
342k
  static MCOperand createReg(unsigned Reg) {
111
342k
    MCOperand Op;
112
342k
    Op.Kind = kRegister;
113
342k
    Op.RegVal = Reg;
114
342k
    return Op;
115
342k
  }
116
310k
  static MCOperand createImm(int64_t Val) {
117
310k
    MCOperand Op;
118
310k
    Op.Kind = kImmediate;
119
310k
    Op.ImmVal = Val;
120
310k
    return Op;
121
310k
  }
122
0
  static MCOperand createFPImm(double Val) {
123
0
    MCOperand Op;
124
0
    Op.Kind = kFPImmediate;
125
0
    Op.FPImmVal = Val;
126
0
    return Op;
127
0
  }
128
117k
  static MCOperand createExpr(const MCExpr *Val) {
129
117k
    MCOperand Op;
130
117k
    Op.Kind = kExpr;
131
117k
    Op.ExprVal = Val;
132
117k
    return Op;
133
117k
  }
134
18.6k
  static MCOperand createInst(const MCInst *Val) {
135
18.6k
    MCOperand Op;
136
18.6k
    Op.Kind = kInst;
137
18.6k
    Op.InstVal = Val;
138
18.6k
    return Op;
139
18.6k
  }
140
141
  void print(raw_ostream &OS) const;
142
  void dump() const;
143
  bool isBareSymbolRef() const;
144
  bool evaluateAsConstantImm(int64_t &Imm) const;
145
};
146
147
template <> struct isPodLike<MCOperand> { static const bool value = true; };
148
149
/// \brief Instances of this class represent a single low-level machine
150
/// instruction.
151
class MCInst {
152
  unsigned Opcode;
153
  uint64_t Address; // address of this instruction - keystone
154
  SMLoc Loc;
155
  SmallVector<MCOperand, 8> Operands;
156
157
public:
158
270k
  MCInst(uint64_t addr = 0) : Opcode(0), Address(addr) {}
159
160
337k
  void setOpcode(unsigned Op) { Opcode = Op; }
161
2.84M
  unsigned getOpcode() const { return Opcode; }
162
163
201k
  void setAddress(uint64_t addr) { Address = addr; }
164
428k
  uint64_t getAddress() const { return Address; }
165
166
212k
  void setLoc(SMLoc loc) { Loc = loc; }
167
85.4k
  SMLoc getLoc() const { return Loc; }
168
169
937k
  const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
170
1.00M
  MCOperand &getOperand(unsigned i) { return Operands[i]; }
171
234k
  unsigned getNumOperands() const { return Operands.size(); }
172
173
822k
  void addOperand(const MCOperand &Op) { Operands.push_back(Op); }
174
175
  typedef SmallVectorImpl<MCOperand>::iterator iterator;
176
  typedef SmallVectorImpl<MCOperand>::const_iterator const_iterator;
177
292k
  void clear() { Operands.clear(); }
178
17
  void erase(iterator I) { Operands.erase(I); }
179
182k
  size_t size() const { return Operands.size(); }
180
18.8k
  iterator begin() { return Operands.begin(); }
181
32.6k
  const_iterator begin() const { return Operands.begin(); }
182
25.6k
  iterator end() { return Operands.end(); }
183
32.6k
  const_iterator end() const { return Operands.end(); }
184
0
  iterator insert(iterator I, const MCOperand &Op) {
185
0
    return Operands.insert(I, Op);
186
0
  }
187
188
  void print(raw_ostream &OS) const;
189
  void dump() const;
190
};
191
192
0
inline raw_ostream& operator<<(raw_ostream &OS, const MCOperand &MO) {
193
0
  MO.print(OS);
194
0
  return OS;
195
0
}
196
197
0
inline raw_ostream& operator<<(raw_ostream &OS, const MCInst &MI) {
198
0
  MI.print(OS);
199
0
  return OS;
200
0
}
201
202
} // end namespace llvm_ks
203
204
#endif