2009-06-19 02:47:33 +02: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 22:49:34 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2012-01-27 00:20:11 +01:00
|
|
|
#include "llvm/Support/SMLoc.h"
|
2009-06-19 02:47:33 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
2009-08-27 09:57:12 +02:00
|
|
|
class raw_ostream;
|
2009-09-03 07:46:51 +02:00
|
|
|
class MCAsmInfo;
|
2010-03-22 22:49:34 +01:00
|
|
|
class MCInstPrinter;
|
2009-08-31 10:08:38 +02:00
|
|
|
class MCExpr;
|
2012-01-19 20:24:37 +01:00
|
|
|
class MCInst;
|
2009-06-19 02:47:33 +02: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 09:03:18 +02:00
|
|
|
kImmediate, ///< Immediate operand.
|
2010-09-15 20:47:08 +02:00
|
|
|
kFPImmediate, ///< Floating-point immediate operand.
|
2012-01-19 20:24:37 +01:00
|
|
|
kExpr, ///< Relocatable immediate operand.
|
|
|
|
kInst ///< Sub-instruction operand.
|
2009-06-19 02:47:33 +02:00
|
|
|
};
|
|
|
|
unsigned char Kind;
|
2010-09-15 18:24:01 +02:00
|
|
|
|
2009-06-19 02:47:33 +02:00
|
|
|
union {
|
|
|
|
unsigned RegVal;
|
2009-06-20 02:47:37 +02:00
|
|
|
int64_t ImmVal;
|
2010-09-16 05:45:21 +02:00
|
|
|
double FPImmVal;
|
2009-08-31 10:08:38 +02:00
|
|
|
const MCExpr *ExprVal;
|
2012-01-19 20:24:37 +01:00
|
|
|
const MCInst *InstVal;
|
2009-06-19 02:47:33 +02:00
|
|
|
};
|
|
|
|
public:
|
2010-09-15 18:24:01 +02:00
|
|
|
|
2010-09-15 20:47:08 +02:00
|
|
|
MCOperand() : Kind(kInvalid), FPImmVal(0.0) {}
|
2009-06-19 02:47:33 +02:00
|
|
|
|
2009-07-01 01:37:44 +02:00
|
|
|
bool isValid() const { return Kind != kInvalid; }
|
2009-06-19 02:47:33 +02:00
|
|
|
bool isReg() const { return Kind == kRegister; }
|
|
|
|
bool isImm() const { return Kind == kImmediate; }
|
2010-09-15 20:47:08 +02:00
|
|
|
bool isFPImm() const { return Kind == kFPImmediate; }
|
2009-08-31 10:08:38 +02:00
|
|
|
bool isExpr() const { return Kind == kExpr; }
|
2012-01-19 20:24:37 +01:00
|
|
|
bool isInst() const { return Kind == kInst; }
|
2010-09-15 18:24:01 +02:00
|
|
|
|
2009-06-19 02:47:33 +02: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 18:24:01 +02:00
|
|
|
|
2009-06-20 02:47:37 +02:00
|
|
|
int64_t getImm() const {
|
2009-06-19 02:47:33 +02:00
|
|
|
assert(isImm() && "This is not an immediate");
|
|
|
|
return ImmVal;
|
|
|
|
}
|
2009-06-20 02:47:37 +02:00
|
|
|
void setImm(int64_t Val) {
|
2009-06-19 02:47:33 +02:00
|
|
|
assert(isImm() && "This is not an immediate");
|
|
|
|
ImmVal = Val;
|
|
|
|
}
|
2010-09-15 18:24:01 +02:00
|
|
|
|
2010-09-16 19:48:08 +02:00
|
|
|
double getFPImm() const {
|
2010-09-15 20:47:08 +02:00
|
|
|
assert(isFPImm() && "This is not an FP immediate");
|
|
|
|
return FPImmVal;
|
|
|
|
}
|
|
|
|
|
2010-09-16 05:45:21 +02:00
|
|
|
void setFPImm(double Val) {
|
2010-09-15 20:47:08 +02:00
|
|
|
assert(isFPImm() && "This is not an FP immediate");
|
|
|
|
FPImmVal = Val;
|
|
|
|
}
|
|
|
|
|
2009-08-31 10:08:38 +02:00
|
|
|
const MCExpr *getExpr() const {
|
|
|
|
assert(isExpr() && "This is not an expression");
|
|
|
|
return ExprVal;
|
2009-07-01 01:37:44 +02:00
|
|
|
}
|
2009-08-31 10:08:38 +02:00
|
|
|
void setExpr(const MCExpr *Val) {
|
|
|
|
assert(isExpr() && "This is not an expression");
|
|
|
|
ExprVal = Val;
|
2009-07-01 01:37:44 +02:00
|
|
|
}
|
2010-09-15 18:24:01 +02:00
|
|
|
|
2012-01-19 20:24:37 +01: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 02:09:22 +02:00
|
|
|
static MCOperand CreateReg(unsigned Reg) {
|
|
|
|
MCOperand Op;
|
|
|
|
Op.Kind = kRegister;
|
|
|
|
Op.RegVal = Reg;
|
|
|
|
return Op;
|
2009-06-19 02:47:33 +02:00
|
|
|
}
|
2009-08-02 02:09:22 +02:00
|
|
|
static MCOperand CreateImm(int64_t Val) {
|
|
|
|
MCOperand Op;
|
|
|
|
Op.Kind = kImmediate;
|
|
|
|
Op.ImmVal = Val;
|
|
|
|
return Op;
|
2009-06-19 02:47:33 +02:00
|
|
|
}
|
2010-09-16 05:45:21 +02:00
|
|
|
static MCOperand CreateFPImm(double Val) {
|
2010-09-15 20:47:08 +02:00
|
|
|
MCOperand Op;
|
|
|
|
Op.Kind = kFPImmediate;
|
|
|
|
Op.FPImmVal = Val;
|
|
|
|
return Op;
|
|
|
|
}
|
2009-08-31 10:08:38 +02:00
|
|
|
static MCOperand CreateExpr(const MCExpr *Val) {
|
2009-08-02 02:09:22 +02:00
|
|
|
MCOperand Op;
|
2009-08-31 10:08:38 +02:00
|
|
|
Op.Kind = kExpr;
|
|
|
|
Op.ExprVal = Val;
|
2009-08-02 02:09:22 +02:00
|
|
|
return Op;
|
2009-07-01 01:37:44 +02:00
|
|
|
}
|
2012-01-19 20:24:37 +01:00
|
|
|
static MCOperand CreateInst(const MCInst *Val) {
|
|
|
|
MCOperand Op;
|
|
|
|
Op.Kind = kInst;
|
|
|
|
Op.InstVal = Val;
|
|
|
|
return Op;
|
|
|
|
}
|
2009-08-27 09:57:12 +02:00
|
|
|
|
2009-09-03 07:46:51 +02:00
|
|
|
void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
|
2009-08-27 09:57:12 +02:00
|
|
|
void dump() const;
|
2009-06-19 02:47:33 +02:00
|
|
|
};
|
|
|
|
|
2011-10-17 23:18:03 +02:00
|
|
|
template <> struct isPodLike<MCOperand> { static const bool value = true; };
|
2010-09-15 18:24:01 +02:00
|
|
|
|
2009-06-19 02:47:33 +02:00
|
|
|
/// MCInst - Instances of this class represent a single low-level machine
|
2010-09-15 18:24:01 +02:00
|
|
|
/// instruction.
|
2009-06-19 02:47:33 +02:00
|
|
|
class MCInst {
|
|
|
|
unsigned Opcode;
|
2012-01-27 00:20:11 +01:00
|
|
|
SMLoc Loc;
|
2009-06-19 02:47:33 +02:00
|
|
|
SmallVector<MCOperand, 8> Operands;
|
|
|
|
public:
|
2009-09-11 18:33:58 +02:00
|
|
|
MCInst() : Opcode(0) {}
|
2010-09-15 18:24:01 +02:00
|
|
|
|
2009-06-20 02:47:37 +02:00
|
|
|
void setOpcode(unsigned Op) { Opcode = Op; }
|
2009-06-20 01:59:57 +02:00
|
|
|
unsigned getOpcode() const { return Opcode; }
|
2009-09-09 22:45:42 +02:00
|
|
|
|
2012-01-27 00:20:11 +01:00
|
|
|
void setLoc(SMLoc loc) { Loc = loc; }
|
|
|
|
SMLoc getLoc() const { return Loc; }
|
|
|
|
|
2009-06-20 01:59:57 +02:00
|
|
|
const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
|
2009-06-20 09:03:18 +02:00
|
|
|
MCOperand &getOperand(unsigned i) { return Operands[i]; }
|
2009-06-23 19:50:34 +02:00
|
|
|
unsigned getNumOperands() const { return Operands.size(); }
|
2010-09-15 18:24:01 +02:00
|
|
|
|
2009-06-20 02:47:37 +02:00
|
|
|
void addOperand(const MCOperand &Op) {
|
|
|
|
Operands.push_back(Op);
|
|
|
|
}
|
2009-08-27 09:57:12 +02:00
|
|
|
|
2011-09-16 01:38:46 +02:00
|
|
|
void clear() { Operands.clear(); }
|
2011-08-03 23:52:15 +02:00
|
|
|
size_t size() { return Operands.size(); }
|
|
|
|
|
2013-04-15 19:40:45 +02:00
|
|
|
typedef SmallVectorImpl<MCOperand>::iterator iterator;
|
2011-08-03 23:52:15 +02: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 20:43:18 +02:00
|
|
|
|
2009-09-03 07:46:51 +02:00
|
|
|
void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
|
2009-08-27 09:57:12 +02:00
|
|
|
void dump() const;
|
2010-03-22 22:49:34 +01:00
|
|
|
|
|
|
|
/// \brief Dump the MCInst as prettily as possible using the additional MC
|
2012-09-14 16:57:36 +02:00
|
|
|
/// structures, if given. Operators are separated by the \p Separator
|
2010-03-22 22:49:34 +01:00
|
|
|
/// string.
|
|
|
|
void dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI = 0,
|
|
|
|
const MCInstPrinter *Printer = 0,
|
|
|
|
StringRef Separator = " ") const;
|
2009-06-19 02:47:33 +02:00
|
|
|
};
|
|
|
|
|
2010-10-08 23:36:13 +02:00
|
|
|
inline raw_ostream& operator<<(raw_ostream &OS, const MCOperand &MO) {
|
|
|
|
MO.print(OS, 0);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2010-10-07 22:38:37 +02:00
|
|
|
inline raw_ostream& operator<<(raw_ostream &OS, const MCInst &MI) {
|
|
|
|
MI.print(OS, 0);
|
|
|
|
return OS;
|
|
|
|
}
|
2009-06-19 02:47:33 +02:00
|
|
|
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|