2009-06-19 00:47:33 +00:00
|
|
|
//===-- llvm/MC/MCInst.h - MCInst class -------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains the declaration of the MCInst and MCOperand classes, which
|
|
|
|
// is the basic representation used to represent low-level machine code
|
|
|
|
// instructions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_MC_MCINST_H
|
|
|
|
#define LLVM_MC_MCINST_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-03-22 21:49:34 +00:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2010-11-29 18:16:10 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2012-01-26 23:20:11 +00:00
|
|
|
#include "llvm/Support/SMLoc.h"
|
2009-06-19 00:47:33 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2009-08-27 07:57:12 +00:00
|
|
|
class raw_ostream;
|
2009-09-03 05:46:51 +00:00
|
|
|
class MCAsmInfo;
|
2010-03-22 21:49:34 +00:00
|
|
|
class MCInstPrinter;
|
2009-08-31 08:08:38 +00:00
|
|
|
class MCExpr;
|
2012-01-19 19:24:37 +00:00
|
|
|
class MCInst;
|
2009-06-19 00:47:33 +00:00
|
|
|
|
|
|
|
/// MCOperand - Instances of this class represent operands of the MCInst class.
|
|
|
|
/// This is a simple discriminated union.
|
|
|
|
class MCOperand {
|
|
|
|
enum MachineOperandType {
|
|
|
|
kInvalid, ///< Uninitialized.
|
|
|
|
kRegister, ///< Register operand.
|
2009-06-20 07:03:18 +00:00
|
|
|
kImmediate, ///< Immediate operand.
|
2010-09-15 18:47:08 +00:00
|
|
|
kFPImmediate, ///< Floating-point immediate operand.
|
2012-01-19 19:24:37 +00:00
|
|
|
kExpr, ///< Relocatable immediate operand.
|
|
|
|
kInst ///< Sub-instruction operand.
|
2009-06-19 00:47:33 +00:00
|
|
|
};
|
|
|
|
unsigned char Kind;
|
2010-09-15 16:24:01 +00:00
|
|
|
|
2009-06-19 00:47:33 +00:00
|
|
|
union {
|
|
|
|
unsigned RegVal;
|
2009-06-20 00:47:37 +00:00
|
|
|
int64_t ImmVal;
|
2010-09-16 03:45:21 +00:00
|
|
|
double FPImmVal;
|
2009-08-31 08:08:38 +00:00
|
|
|
const MCExpr *ExprVal;
|
2012-01-19 19:24:37 +00:00
|
|
|
const MCInst *InstVal;
|
2009-06-19 00:47:33 +00:00
|
|
|
};
|
|
|
|
public:
|
2010-09-15 16:24:01 +00:00
|
|
|
|
2010-09-15 18:47:08 +00:00
|
|
|
MCOperand() : Kind(kInvalid), FPImmVal(0.0) {}
|
2009-06-19 00:47:33 +00:00
|
|
|
|
2009-06-30 23:37:44 +00:00
|
|
|
bool isValid() const { return Kind != kInvalid; }
|
2009-06-19 00:47:33 +00:00
|
|
|
bool isReg() const { return Kind == kRegister; }
|
|
|
|
bool isImm() const { return Kind == kImmediate; }
|
2010-09-15 18:47:08 +00:00
|
|
|
bool isFPImm() const { return Kind == kFPImmediate; }
|
2009-08-31 08:08:38 +00:00
|
|
|
bool isExpr() const { return Kind == kExpr; }
|
2012-01-19 19:24:37 +00:00
|
|
|
bool isInst() const { return Kind == kInst; }
|
2010-09-15 16:24:01 +00:00
|
|
|
|
2009-06-19 00:47:33 +00:00
|
|
|
/// getReg - Returns the register number.
|
|
|
|
unsigned getReg() const {
|
|
|
|
assert(isReg() && "This is not a register operand!");
|
|
|
|
return RegVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// setReg - Set the register number.
|
|
|
|
void setReg(unsigned Reg) {
|
|
|
|
assert(isReg() && "This is not a register operand!");
|
|
|
|
RegVal = Reg;
|
|
|
|
}
|
2010-09-15 16:24:01 +00:00
|
|
|
|
2009-06-20 00:47:37 +00:00
|
|
|
int64_t getImm() const {
|
2009-06-19 00:47:33 +00:00
|
|
|
assert(isImm() && "This is not an immediate");
|
|
|
|
return ImmVal;
|
|
|
|
}
|
2009-06-20 00:47:37 +00:00
|
|
|
void setImm(int64_t Val) {
|
2009-06-19 00:47:33 +00:00
|
|
|
assert(isImm() && "This is not an immediate");
|
|
|
|
ImmVal = Val;
|
|
|
|
}
|
2010-09-15 16:24:01 +00:00
|
|
|
|
2010-09-16 17:48:08 +00:00
|
|
|
double getFPImm() const {
|
2010-09-15 18:47:08 +00:00
|
|
|
assert(isFPImm() && "This is not an FP immediate");
|
|
|
|
return FPImmVal;
|
|
|
|
}
|
|
|
|
|
2010-09-16 03:45:21 +00:00
|
|
|
void setFPImm(double Val) {
|
2010-09-15 18:47:08 +00:00
|
|
|
assert(isFPImm() && "This is not an FP immediate");
|
|
|
|
FPImmVal = Val;
|
|
|
|
}
|
|
|
|
|
2009-08-31 08:08:38 +00:00
|
|
|
const MCExpr *getExpr() const {
|
|
|
|
assert(isExpr() && "This is not an expression");
|
|
|
|
return ExprVal;
|
2009-06-30 23:37:44 +00:00
|
|
|
}
|
2009-08-31 08:08:38 +00:00
|
|
|
void setExpr(const MCExpr *Val) {
|
|
|
|
assert(isExpr() && "This is not an expression");
|
|
|
|
ExprVal = Val;
|
2009-06-30 23:37:44 +00:00
|
|
|
}
|
2010-09-15 16:24:01 +00:00
|
|
|
|
2012-01-19 19:24:37 +00:00
|
|
|
const MCInst *getInst() const {
|
|
|
|
assert(isInst() && "This is not a sub-instruction");
|
|
|
|
return InstVal;
|
|
|
|
}
|
|
|
|
void setInst(const MCInst *Val) {
|
|
|
|
assert(isInst() && "This is not a sub-instruction");
|
|
|
|
InstVal = Val;
|
|
|
|
}
|
|
|
|
|
2009-08-02 00:09:22 +00:00
|
|
|
static MCOperand CreateReg(unsigned Reg) {
|
|
|
|
MCOperand Op;
|
|
|
|
Op.Kind = kRegister;
|
|
|
|
Op.RegVal = Reg;
|
|
|
|
return Op;
|
2009-06-19 00:47:33 +00:00
|
|
|
}
|
2009-08-02 00:09:22 +00:00
|
|
|
static MCOperand CreateImm(int64_t Val) {
|
|
|
|
MCOperand Op;
|
|
|
|
Op.Kind = kImmediate;
|
|
|
|
Op.ImmVal = Val;
|
|
|
|
return Op;
|
2009-06-19 00:47:33 +00:00
|
|
|
}
|
2010-09-16 03:45:21 +00:00
|
|
|
static MCOperand CreateFPImm(double Val) {
|
2010-09-15 18:47:08 +00:00
|
|
|
MCOperand Op;
|
|
|
|
Op.Kind = kFPImmediate;
|
|
|
|
Op.FPImmVal = Val;
|
|
|
|
return Op;
|
|
|
|
}
|
2009-08-31 08:08:38 +00:00
|
|
|
static MCOperand CreateExpr(const MCExpr *Val) {
|
2009-08-02 00:09:22 +00:00
|
|
|
MCOperand Op;
|
2009-08-31 08:08:38 +00:00
|
|
|
Op.Kind = kExpr;
|
|
|
|
Op.ExprVal = Val;
|
2009-08-02 00:09:22 +00:00
|
|
|
return Op;
|
2009-06-30 23:37:44 +00:00
|
|
|
}
|
2012-01-19 19:24:37 +00:00
|
|
|
static MCOperand CreateInst(const MCInst *Val) {
|
|
|
|
MCOperand Op;
|
|
|
|
Op.Kind = kInst;
|
|
|
|
Op.InstVal = Val;
|
|
|
|
return Op;
|
|
|
|
}
|
2009-08-27 07:57:12 +00:00
|
|
|
|
2009-09-03 05:46:51 +00:00
|
|
|
void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
|
2009-08-27 07:57:12 +00:00
|
|
|
void dump() const;
|
2009-06-19 00:47:33 +00:00
|
|
|
};
|
|
|
|
|
2011-10-17 21:18:03 +00:00
|
|
|
template <> struct isPodLike<MCOperand> { static const bool value = true; };
|
2010-09-15 16:24:01 +00:00
|
|
|
|
2009-06-19 00:47:33 +00:00
|
|
|
/// MCInst - Instances of this class represent a single low-level machine
|
2010-09-15 16:24:01 +00:00
|
|
|
/// instruction.
|
2009-06-19 00:47:33 +00:00
|
|
|
class MCInst {
|
|
|
|
unsigned Opcode;
|
2012-01-26 23:20:11 +00:00
|
|
|
SMLoc Loc;
|
2009-06-19 00:47:33 +00:00
|
|
|
SmallVector<MCOperand, 8> Operands;
|
|
|
|
public:
|
2009-09-11 16:33:58 +00:00
|
|
|
MCInst() : Opcode(0) {}
|
2010-09-15 16:24:01 +00:00
|
|
|
|
2009-06-20 00:47:37 +00:00
|
|
|
void setOpcode(unsigned Op) { Opcode = Op; }
|
2009-06-19 23:59:57 +00:00
|
|
|
unsigned getOpcode() const { return Opcode; }
|
2009-09-09 20:45:42 +00:00
|
|
|
|
2012-01-26 23:20:11 +00:00
|
|
|
void setLoc(SMLoc loc) { Loc = loc; }
|
|
|
|
SMLoc getLoc() const { return Loc; }
|
|
|
|
|
2009-06-19 23:59:57 +00:00
|
|
|
const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
|
2009-06-20 07:03:18 +00:00
|
|
|
MCOperand &getOperand(unsigned i) { return Operands[i]; }
|
2009-06-23 17:50:34 +00:00
|
|
|
unsigned getNumOperands() const { return Operands.size(); }
|
2010-09-15 16:24:01 +00:00
|
|
|
|
2009-06-20 00:47:37 +00:00
|
|
|
void addOperand(const MCOperand &Op) {
|
|
|
|
Operands.push_back(Op);
|
|
|
|
}
|
2009-08-27 07:57:12 +00:00
|
|
|
|
2011-09-15 23:38:46 +00:00
|
|
|
void clear() { Operands.clear(); }
|
2011-08-03 21:52:15 +00:00
|
|
|
size_t size() { return Operands.size(); }
|
|
|
|
|
2013-04-15 17:40:45 +00:00
|
|
|
typedef SmallVectorImpl<MCOperand>::iterator iterator;
|
2011-08-03 21:52:15 +00:00
|
|
|
iterator begin() { return Operands.begin(); }
|
|
|
|
iterator end() { return Operands.end(); }
|
|
|
|
iterator insert(iterator I, const MCOperand &Op) {
|
|
|
|
return Operands.insert(I, Op);
|
|
|
|
}
|
2011-08-01 18:43:18 +00:00
|
|
|
|
2009-09-03 05:46:51 +00:00
|
|
|
void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
|
2009-08-27 07:57:12 +00:00
|
|
|
void dump() const;
|
2010-03-22 21:49:34 +00:00
|
|
|
|
|
|
|
/// \brief Dump the MCInst as prettily as possible using the additional MC
|
2012-09-14 14:57:36 +00:00
|
|
|
/// structures, if given. Operators are separated by the \p Separator
|
2010-03-22 21:49:34 +00:00
|
|
|
/// string.
|
2014-04-13 04:57:38 +00:00
|
|
|
void dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI = nullptr,
|
|
|
|
const MCInstPrinter *Printer = nullptr,
|
2010-03-22 21:49:34 +00:00
|
|
|
StringRef Separator = " ") const;
|
2009-06-19 00:47:33 +00:00
|
|
|
};
|
|
|
|
|
2010-10-08 21:36:13 +00:00
|
|
|
inline raw_ostream& operator<<(raw_ostream &OS, const MCOperand &MO) {
|
2014-04-13 04:57:38 +00:00
|
|
|
MO.print(OS, nullptr);
|
2010-10-08 21:36:13 +00:00
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2010-10-07 20:38:37 +00:00
|
|
|
inline raw_ostream& operator<<(raw_ostream &OS, const MCInst &MI) {
|
2014-04-13 04:57:38 +00:00
|
|
|
MI.print(OS, nullptr);
|
2010-10-07 20:38:37 +00:00
|
|
|
return OS;
|
|
|
|
}
|
2009-06-19 00:47:33 +00:00
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|