#ifndef LLVM_MC_MCSYMBOL_H
#define LLVM_MC_MCSYMBOL_H
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/StringMapEntry.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFragment.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MathExtras.h"
#include <cassert>
#include <cstddef>
#include <cstdint>
namespace llvm {
class MCAsmInfo;
class MCContext;
class MCSection;
class raw_ostream;
class MCSymbol {
protected:
enum SymbolKind {
SymbolKindUnset,
SymbolKindCOFF,
SymbolKindELF,
SymbolKindGOFF,
SymbolKindMachO,
SymbolKindWasm,
SymbolKindXCOFF,
};
enum Contents : uint8_t {
SymContentsUnset,
SymContentsOffset,
SymContentsVariable,
SymContentsCommon,
SymContentsTargetCommon, };
static MCFragment *AbsolutePseudoFragment;
mutable PointerIntPair<MCFragment *, 1> FragmentAndHasName;
unsigned IsTemporary : 1;
unsigned IsRedefinable : 1;
mutable unsigned IsUsed : 1;
mutable unsigned IsRegistered : 1;
mutable unsigned IsExternal : 1;
mutable unsigned IsPrivateExtern : 1;
unsigned Kind : 3;
mutable unsigned IsUsedInReloc : 1;
unsigned SymbolContents : 3;
enum : unsigned { NumCommonAlignmentBits = 5 };
unsigned CommonAlignLog2 : NumCommonAlignmentBits;
enum : unsigned { NumFlagsBits = 16 };
mutable uint32_t Flags : NumFlagsBits;
mutable uint32_t Index = 0;
union {
uint64_t Offset;
uint64_t CommonSize;
const MCExpr *Value;
};
friend class MCExpr;
friend class MCContext;
using NameEntryStorageTy = union {
const StringMapEntry<bool> *NameEntry;
uint64_t AlignmentPadding;
};
MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
: IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
Kind(Kind), IsUsedInReloc(false), SymbolContents(SymContentsUnset),
CommonAlignLog2(0), Flags(0) {
Offset = 0;
FragmentAndHasName.setInt(!!Name);
if (Name)
getNameEntryPtr() = Name;
}
void *operator new(size_t s, const StringMapEntry<bool> *Name,
MCContext &Ctx);
private:
void operator delete(void *);
void operator delete(void*, unsigned) {
llvm_unreachable("Constructor throws?");
}
void operator delete(void*, unsigned, bool) {
llvm_unreachable("Constructor throws?");
}
const StringMapEntry<bool> *&getNameEntryPtr() {
assert(FragmentAndHasName.getInt() && "Name is required");
NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
return (*(Name - 1)).NameEntry;
}
const StringMapEntry<bool> *&getNameEntryPtr() const {
return const_cast<MCSymbol*>(this)->getNameEntryPtr();
}
public:
MCSymbol(const MCSymbol &) = delete;
MCSymbol &operator=(const MCSymbol &) = delete;
StringRef getName() const {
if (!FragmentAndHasName.getInt())
return StringRef();
return getNameEntryPtr()->first();
}
bool isRegistered() const { return IsRegistered; }
void setIsRegistered(bool Value) const { IsRegistered = Value; }
void setUsedInReloc() const { IsUsedInReloc = true; }
bool isUsedInReloc() const { return IsUsedInReloc; }
bool isTemporary() const { return IsTemporary; }
bool isUsed() const { return IsUsed; }
bool isRedefinable() const { return IsRedefinable; }
void setRedefinable(bool Value) { IsRedefinable = Value; }
void redefineIfPossible() {
if (IsRedefinable) {
if (SymbolContents == SymContentsVariable) {
Value = nullptr;
SymbolContents = SymContentsUnset;
}
setUndefined();
IsRedefinable = false;
}
}
bool isDefined() const { return !isUndefined(); }
bool isInSection() const {
return isDefined() && !isAbsolute();
}
bool isUndefined(bool SetUsed = true) const {
return getFragment(SetUsed) == nullptr;
}
bool isAbsolute() const {
return getFragment() == AbsolutePseudoFragment;
}
MCSection &getSection() const {
assert(isInSection() && "Invalid accessor!");
return *getFragment()->getParent();
}
void setFragment(MCFragment *F) const {
assert(!isVariable() && "Cannot set fragment of variable");
FragmentAndHasName.setPointer(F);
}
void setUndefined() { FragmentAndHasName.setPointer(nullptr); }
bool isELF() const { return Kind == SymbolKindELF; }
bool isCOFF() const { return Kind == SymbolKindCOFF; }
bool isGOFF() const { return Kind == SymbolKindGOFF; }
bool isMachO() const { return Kind == SymbolKindMachO; }
bool isWasm() const { return Kind == SymbolKindWasm; }
bool isXCOFF() const { return Kind == SymbolKindXCOFF; }
bool isVariable() const {
return SymbolContents == SymContentsVariable;
}
const MCExpr *getVariableValue(bool SetUsed = true) const {
assert(isVariable() && "Invalid accessor!");
IsUsed |= SetUsed;
return Value;
}
void setVariableValue(const MCExpr *Value);
uint32_t getIndex() const {
return Index;
}
void setIndex(uint32_t Value) const {
Index = Value;
}
bool isUnset() const { return SymbolContents == SymContentsUnset; }
uint64_t getOffset() const {
assert((SymbolContents == SymContentsUnset ||
SymbolContents == SymContentsOffset) &&
"Cannot get offset for a common/variable symbol");
return Offset;
}
void setOffset(uint64_t Value) {
assert((SymbolContents == SymContentsUnset ||
SymbolContents == SymContentsOffset) &&
"Cannot set offset for a common/variable symbol");
Offset = Value;
SymbolContents = SymContentsOffset;
}
uint64_t getCommonSize() const {
assert(isCommon() && "Not a 'common' symbol!");
return CommonSize;
}
void setCommon(uint64_t Size, unsigned Align, bool Target = false) {
assert(getOffset() == 0);
CommonSize = Size;
SymbolContents = Target ? SymContentsTargetCommon : SymContentsCommon;
assert((!Align || isPowerOf2_32(Align)) &&
"Alignment must be a power of 2");
unsigned Log2Align = Log2_32(Align) + 1;
assert(Log2Align < (1U << NumCommonAlignmentBits) &&
"Out of range alignment");
CommonAlignLog2 = Log2Align;
}
unsigned getCommonAlignment() const {
assert(isCommon() && "Not a 'common' symbol!");
return CommonAlignLog2 ? (1U << (CommonAlignLog2 - 1)) : 0;
}
bool declareCommon(uint64_t Size, unsigned Align, bool Target = false) {
assert(isCommon() || getOffset() == 0);
if(isCommon()) {
if (CommonSize != Size || getCommonAlignment() != Align ||
isTargetCommon() != Target)
return true;
} else
setCommon(Size, Align, Target);
return false;
}
bool isCommon() const {
return SymbolContents == SymContentsCommon ||
SymbolContents == SymContentsTargetCommon;
}
bool isTargetCommon() const {
return SymbolContents == SymContentsTargetCommon;
}
MCFragment *getFragment(bool SetUsed = true) const {
MCFragment *Fragment = FragmentAndHasName.getPointer();
if (Fragment || !isVariable())
return Fragment;
Fragment = getVariableValue(SetUsed)->findAssociatedFragment();
FragmentAndHasName.setPointer(Fragment);
return Fragment;
}
bool isExternal() const { return IsExternal; }
void setExternal(bool Value) const { IsExternal = Value; }
bool isPrivateExtern() const { return IsPrivateExtern; }
void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
void dump() const;
protected:
uint32_t getFlags() const { return Flags; }
void setFlags(uint32_t Value) const {
assert(Value < (1U << NumFlagsBits) && "Out of range flags");
Flags = Value;
}
void modifyFlags(uint32_t Value, uint32_t Mask) const {
assert(Value < (1U << NumFlagsBits) && "Out of range flags");
Flags = (Flags & ~Mask) | Value;
}
};
inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
Sym.print(OS, nullptr);
return OS;
}
}
#endif