#ifndef LLVM_MC_MCEXPR_H
#define LLVM_MC_MCEXPR_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/Support/SMLoc.h"
#include <cstdint>
namespace llvm {
class MCAsmInfo;
class MCAsmLayout;
class MCAssembler;
class MCContext;
class MCFixup;
class MCFragment;
class MCSection;
class MCStreamer;
class MCSymbol;
class MCValue;
class raw_ostream;
class StringRef;
using SectionAddrMap = DenseMap<const MCSection *, uint64_t>;
class MCExpr {
public:
enum ExprKind : uint8_t {
Binary, Constant, SymbolRef, Unary, Target };
private:
static const unsigned NumSubclassDataBits = 24;
static_assert(
NumSubclassDataBits == CHAR_BIT * (sizeof(unsigned) - sizeof(ExprKind)),
"ExprKind and SubclassData together should take up one word");
ExprKind Kind;
unsigned SubclassData : NumSubclassDataBits;
SMLoc Loc;
bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm,
const MCAsmLayout *Layout,
const SectionAddrMap *Addrs, bool InSet) const;
protected:
explicit MCExpr(ExprKind Kind, SMLoc Loc, unsigned SubclassData = 0)
: Kind(Kind), SubclassData(SubclassData), Loc(Loc) {
assert(SubclassData < (1 << NumSubclassDataBits) &&
"Subclass data too large");
}
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
const MCAsmLayout *Layout,
const MCFixup *Fixup,
const SectionAddrMap *Addrs, bool InSet) const;
unsigned getSubclassData() const { return SubclassData; }
public:
MCExpr(const MCExpr &) = delete;
MCExpr &operator=(const MCExpr &) = delete;
ExprKind getKind() const { return Kind; }
SMLoc getLoc() const { return Loc; }
void print(raw_ostream &OS, const MCAsmInfo *MAI,
bool InParens = false) const;
void dump() const;
bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout,
const SectionAddrMap &Addrs) const;
bool evaluateAsAbsolute(int64_t &Res) const;
bool evaluateAsAbsolute(int64_t &Res, const MCAssembler &Asm) const;
bool evaluateAsAbsolute(int64_t &Res, const MCAssembler *Asm) const;
bool evaluateAsAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
bool evaluateKnownAbsolute(int64_t &Res, const MCAsmLayout &Layout) const;
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout,
const MCFixup *Fixup) const;
bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const;
MCFragment *findAssociatedFragment() const;
};
inline raw_ostream &operator<<(raw_ostream &OS, const MCExpr &E) {
E.print(OS, nullptr);
return OS;
}
class MCConstantExpr : public MCExpr {
int64_t Value;
static const unsigned SizeInBytesBits = 8;
static const unsigned SizeInBytesMask = (1 << SizeInBytesBits) - 1;
static const unsigned PrintInHexBit = 1 << SizeInBytesBits;
static unsigned encodeSubclassData(bool PrintInHex, unsigned SizeInBytes) {
assert(SizeInBytes <= sizeof(int64_t) && "Excessive size");
return SizeInBytes | (PrintInHex ? PrintInHexBit : 0);
}
MCConstantExpr(int64_t Value, bool PrintInHex, unsigned SizeInBytes)
: MCExpr(MCExpr::Constant, SMLoc(),
encodeSubclassData(PrintInHex, SizeInBytes)), Value(Value) {}
public:
static const MCConstantExpr *create(int64_t Value, MCContext &Ctx,
bool PrintInHex = false,
unsigned SizeInBytes = 0);
int64_t getValue() const { return Value; }
unsigned getSizeInBytes() const {
return getSubclassData() & SizeInBytesMask;
}
bool useHexFormat() const { return (getSubclassData() & PrintInHexBit) != 0; }
static bool classof(const MCExpr *E) {
return E->getKind() == MCExpr::Constant;
}
};
class MCSymbolRefExpr : public MCExpr {
public:
enum VariantKind : uint16_t {
VK_None,
VK_Invalid,
VK_GOT,
VK_GOTOFF,
VK_GOTREL,
VK_PCREL,
VK_GOTPCREL,
VK_GOTPCREL_NORELAX,
VK_GOTTPOFF,
VK_INDNTPOFF,
VK_NTPOFF,
VK_GOTNTPOFF,
VK_PLT,
VK_TLSGD,
VK_TLSLD,
VK_TLSLDM,
VK_TPOFF,
VK_DTPOFF,
VK_TLSCALL, VK_TLSDESC, VK_TLVP, VK_TLVPPAGE,
VK_TLVPPAGEOFF,
VK_PAGE,
VK_PAGEOFF,
VK_GOTPAGE,
VK_GOTPAGEOFF,
VK_SECREL,
VK_SIZE, VK_WEAKREF,
VK_X86_ABS8,
VK_X86_PLTOFF,
VK_ARM_NONE,
VK_ARM_GOT_PREL,
VK_ARM_TARGET1,
VK_ARM_TARGET2,
VK_ARM_PREL31,
VK_ARM_SBREL, VK_ARM_TLSLDO, VK_ARM_TLSDESCSEQ,
VK_AVR_NONE,
VK_AVR_LO8,
VK_AVR_HI8,
VK_AVR_HLO8,
VK_AVR_DIFF8,
VK_AVR_DIFF16,
VK_AVR_DIFF32,
VK_AVR_PM,
VK_PPC_LO, VK_PPC_HI, VK_PPC_HA, VK_PPC_HIGH, VK_PPC_HIGHA, VK_PPC_HIGHER, VK_PPC_HIGHERA, VK_PPC_HIGHEST, VK_PPC_HIGHESTA, VK_PPC_GOT_LO, VK_PPC_GOT_HI, VK_PPC_GOT_HA, VK_PPC_TOCBASE, VK_PPC_TOC, VK_PPC_TOC_LO, VK_PPC_TOC_HI, VK_PPC_TOC_HA, VK_PPC_U, VK_PPC_L, VK_PPC_DTPMOD, VK_PPC_TPREL_LO, VK_PPC_TPREL_HI, VK_PPC_TPREL_HA, VK_PPC_TPREL_HIGH, VK_PPC_TPREL_HIGHA, VK_PPC_TPREL_HIGHER, VK_PPC_TPREL_HIGHERA, VK_PPC_TPREL_HIGHEST, VK_PPC_TPREL_HIGHESTA, VK_PPC_DTPREL_LO, VK_PPC_DTPREL_HI, VK_PPC_DTPREL_HA, VK_PPC_DTPREL_HIGH, VK_PPC_DTPREL_HIGHA, VK_PPC_DTPREL_HIGHER, VK_PPC_DTPREL_HIGHERA, VK_PPC_DTPREL_HIGHEST, VK_PPC_DTPREL_HIGHESTA, VK_PPC_GOT_TPREL, VK_PPC_GOT_TPREL_LO, VK_PPC_GOT_TPREL_HI, VK_PPC_GOT_TPREL_HA, VK_PPC_GOT_DTPREL, VK_PPC_GOT_DTPREL_LO, VK_PPC_GOT_DTPREL_HI, VK_PPC_GOT_DTPREL_HA, VK_PPC_TLS, VK_PPC_GOT_TLSGD, VK_PPC_GOT_TLSGD_LO, VK_PPC_GOT_TLSGD_HI, VK_PPC_GOT_TLSGD_HA, VK_PPC_TLSGD, VK_PPC_AIX_TLSGD, VK_PPC_AIX_TLSGDM, VK_PPC_GOT_TLSLD, VK_PPC_GOT_TLSLD_LO, VK_PPC_GOT_TLSLD_HI, VK_PPC_GOT_TLSLD_HA, VK_PPC_GOT_PCREL, VK_PPC_GOT_TLSGD_PCREL, VK_PPC_GOT_TLSLD_PCREL, VK_PPC_GOT_TPREL_PCREL, VK_PPC_TLS_PCREL, VK_PPC_TLSLD, VK_PPC_LOCAL, VK_PPC_NOTOC, VK_PPC_PCREL_OPT,
VK_COFF_IMGREL32,
VK_Hexagon_LO16,
VK_Hexagon_HI16,
VK_Hexagon_GPREL,
VK_Hexagon_GD_GOT,
VK_Hexagon_LD_GOT,
VK_Hexagon_GD_PLT,
VK_Hexagon_LD_PLT,
VK_Hexagon_IE,
VK_Hexagon_IE_GOT,
VK_WASM_TYPEINDEX, VK_WASM_TLSREL, VK_WASM_MBREL, VK_WASM_TBREL, VK_WASM_GOT_TLS,
VK_AMDGPU_GOTPCREL32_LO, VK_AMDGPU_GOTPCREL32_HI, VK_AMDGPU_REL32_LO, VK_AMDGPU_REL32_HI, VK_AMDGPU_REL64, VK_AMDGPU_ABS32_LO, VK_AMDGPU_ABS32_HI,
VK_VE_HI32, VK_VE_LO32, VK_VE_PC_HI32, VK_VE_PC_LO32, VK_VE_GOT_HI32, VK_VE_GOT_LO32, VK_VE_GOTOFF_HI32, VK_VE_GOTOFF_LO32, VK_VE_PLT_HI32, VK_VE_PLT_LO32, VK_VE_TLS_GD_HI32, VK_VE_TLS_GD_LO32, VK_VE_TPOFF_HI32, VK_VE_TPOFF_LO32,
VK_TPREL,
VK_DTPREL
};
private:
const MCSymbol *Symbol;
static const unsigned VariantKindBits = 16;
static const unsigned VariantKindMask = (1 << VariantKindBits) - 1;
static const unsigned HasSubsectionsViaSymbolsBit = 1 << VariantKindBits;
static unsigned encodeSubclassData(VariantKind Kind,
bool HasSubsectionsViaSymbols) {
return (unsigned)Kind |
(HasSubsectionsViaSymbols ? HasSubsectionsViaSymbolsBit : 0);
}
explicit MCSymbolRefExpr(const MCSymbol *Symbol, VariantKind Kind,
const MCAsmInfo *MAI, SMLoc Loc = SMLoc());
public:
static const MCSymbolRefExpr *create(const MCSymbol *Symbol, MCContext &Ctx) {
return MCSymbolRefExpr::create(Symbol, VK_None, Ctx);
}
static const MCSymbolRefExpr *create(const MCSymbol *Symbol, VariantKind Kind,
MCContext &Ctx, SMLoc Loc = SMLoc());
static const MCSymbolRefExpr *create(StringRef Name, VariantKind Kind,
MCContext &Ctx);
const MCSymbol &getSymbol() const { return *Symbol; }
VariantKind getKind() const {
return (VariantKind)(getSubclassData() & VariantKindMask);
}
bool hasSubsectionsViaSymbols() const {
return (getSubclassData() & HasSubsectionsViaSymbolsBit) != 0;
}
static StringRef getVariantKindName(VariantKind Kind);
static VariantKind getVariantKindForName(StringRef Name);
static bool classof(const MCExpr *E) {
return E->getKind() == MCExpr::SymbolRef;
}
};
class MCUnaryExpr : public MCExpr {
public:
enum Opcode {
LNot, Minus, Not, Plus };
private:
const MCExpr *Expr;
MCUnaryExpr(Opcode Op, const MCExpr *Expr, SMLoc Loc)
: MCExpr(MCExpr::Unary, Loc, Op), Expr(Expr) {}
public:
static const MCUnaryExpr *create(Opcode Op, const MCExpr *Expr,
MCContext &Ctx, SMLoc Loc = SMLoc());
static const MCUnaryExpr *createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
return create(LNot, Expr, Ctx, Loc);
}
static const MCUnaryExpr *createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
return create(Minus, Expr, Ctx, Loc);
}
static const MCUnaryExpr *createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
return create(Not, Expr, Ctx, Loc);
}
static const MCUnaryExpr *createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc = SMLoc()) {
return create(Plus, Expr, Ctx, Loc);
}
Opcode getOpcode() const { return (Opcode)getSubclassData(); }
const MCExpr *getSubExpr() const { return Expr; }
static bool classof(const MCExpr *E) {
return E->getKind() == MCExpr::Unary;
}
};
class MCBinaryExpr : public MCExpr {
public:
enum Opcode {
Add, And, Div, EQ, GT, GTE, LAnd, LOr, LT, LTE, Mod, Mul, NE, Or, OrNot, Shl, AShr, LShr, Sub, Xor };
private:
const MCExpr *LHS, *RHS;
MCBinaryExpr(Opcode Op, const MCExpr *LHS, const MCExpr *RHS,
SMLoc Loc = SMLoc())
: MCExpr(MCExpr::Binary, Loc, Op), LHS(LHS), RHS(RHS) {}
public:
static const MCBinaryExpr *create(Opcode Op, const MCExpr *LHS,
const MCExpr *RHS, MCContext &Ctx,
SMLoc Loc = SMLoc());
static const MCBinaryExpr *createAdd(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(Add, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createAnd(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(And, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createDiv(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(Div, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createEQ(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(EQ, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createGT(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(GT, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createGTE(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(GTE, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createLAnd(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(LAnd, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createLOr(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(LOr, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createLT(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(LT, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createLTE(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(LTE, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createMod(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(Mod, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createMul(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(Mul, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createNE(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(NE, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createOr(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(Or, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createShl(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(Shl, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createAShr(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(AShr, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createLShr(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(LShr, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createSub(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(Sub, LHS, RHS, Ctx);
}
static const MCBinaryExpr *createXor(const MCExpr *LHS, const MCExpr *RHS,
MCContext &Ctx) {
return create(Xor, LHS, RHS, Ctx);
}
Opcode getOpcode() const { return (Opcode)getSubclassData(); }
const MCExpr *getLHS() const { return LHS; }
const MCExpr *getRHS() const { return RHS; }
static bool classof(const MCExpr *E) {
return E->getKind() == MCExpr::Binary;
}
};
class MCTargetExpr : public MCExpr {
virtual void anchor();
protected:
MCTargetExpr() : MCExpr(Target, SMLoc()) {}
virtual ~MCTargetExpr() = default;
public:
virtual void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const = 0;
virtual bool evaluateAsRelocatableImpl(MCValue &Res,
const MCAsmLayout *Layout,
const MCFixup *Fixup) const = 0;
virtual bool isEqualTo(const MCExpr *x) const { return false; }
virtual bool inlineAssignedExpr() const { return false; }
virtual void visitUsedExpr(MCStreamer& Streamer) const = 0;
virtual MCFragment *findAssociatedFragment() const = 0;
virtual void fixELFSymbolsInTLSFixups(MCAssembler &) const = 0;
static bool classof(const MCExpr *E) {
return E->getKind() == MCExpr::Target;
}
};
}
#endif