#ifndef LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
#define LLVM_MC_MCLINKEROPTIMIZATIONHINT_H
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include <cassert>
#include <cstdint>
namespace llvm {
class MachObjectWriter;
class MCAsmLayout;
class MCSymbol;
class raw_ostream;
enum MCLOHType {
MCLOH_AdrpAdrp = 0x1u, MCLOH_AdrpLdr = 0x2u, MCLOH_AdrpAddLdr = 0x3u, MCLOH_AdrpLdrGotLdr = 0x4u, MCLOH_AdrpAddStr = 0x5u, MCLOH_AdrpLdrGotStr = 0x6u, MCLOH_AdrpAdd = 0x7u, MCLOH_AdrpLdrGot = 0x8u };
static inline StringRef MCLOHDirectiveName() {
return StringRef(".loh");
}
static inline bool isValidMCLOHType(unsigned Kind) {
return Kind >= MCLOH_AdrpAdrp && Kind <= MCLOH_AdrpLdrGot;
}
static inline int MCLOHNameToId(StringRef Name) {
#define MCLOHCaseNameToId(Name) .Case(#Name, MCLOH_ ## Name)
return StringSwitch<int>(Name)
MCLOHCaseNameToId(AdrpAdrp)
MCLOHCaseNameToId(AdrpLdr)
MCLOHCaseNameToId(AdrpAddLdr)
MCLOHCaseNameToId(AdrpLdrGotLdr)
MCLOHCaseNameToId(AdrpAddStr)
MCLOHCaseNameToId(AdrpLdrGotStr)
MCLOHCaseNameToId(AdrpAdd)
MCLOHCaseNameToId(AdrpLdrGot)
.Default(-1);
#undef MCLOHCaseNameToId
}
static inline StringRef MCLOHIdToName(MCLOHType Kind) {
#define MCLOHCaseIdToName(Name) case MCLOH_ ## Name: return StringRef(#Name);
switch (Kind) {
MCLOHCaseIdToName(AdrpAdrp);
MCLOHCaseIdToName(AdrpLdr);
MCLOHCaseIdToName(AdrpAddLdr);
MCLOHCaseIdToName(AdrpLdrGotLdr);
MCLOHCaseIdToName(AdrpAddStr);
MCLOHCaseIdToName(AdrpLdrGotStr);
MCLOHCaseIdToName(AdrpAdd);
MCLOHCaseIdToName(AdrpLdrGot);
}
return StringRef();
#undef MCLOHCaseIdToName
}
static inline int MCLOHIdToNbArgs(MCLOHType Kind) {
switch (Kind) {
case MCLOH_AdrpAdrp:
case MCLOH_AdrpLdr:
case MCLOH_AdrpAdd:
case MCLOH_AdrpLdrGot:
return 2;
case MCLOH_AdrpAddLdr:
case MCLOH_AdrpLdrGotLdr:
case MCLOH_AdrpAddStr:
case MCLOH_AdrpLdrGotStr:
return 3;
}
return -1;
}
class MCLOHDirective {
MCLOHType Kind;
SmallVector<MCSymbol *, 3> Args;
void emit_impl(raw_ostream &OutStream, const MachObjectWriter &ObjWriter,
const MCAsmLayout &Layout) const;
public:
using LOHArgs = SmallVectorImpl<MCSymbol *>;
MCLOHDirective(MCLOHType Kind, const LOHArgs &Args)
: Kind(Kind), Args(Args.begin(), Args.end()) {
assert(isValidMCLOHType(Kind) && "Invalid LOH directive type!");
}
MCLOHType getKind() const { return Kind; }
const LOHArgs &getArgs() const { return Args; }
void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const;
uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
const MCAsmLayout &Layout) const;
};
class MCLOHContainer {
mutable uint64_t EmitSize = 0;
SmallVector<MCLOHDirective, 32> Directives;
public:
using LOHDirectives = SmallVectorImpl<MCLOHDirective>;
MCLOHContainer() = default;
const LOHDirectives &getDirectives() const {
return Directives;
}
void addDirective(MCLOHType Kind, const MCLOHDirective::LOHArgs &Args) {
Directives.push_back(MCLOHDirective(Kind, Args));
}
uint64_t getEmitSize(const MachObjectWriter &ObjWriter,
const MCAsmLayout &Layout) const {
if (!EmitSize) {
for (const MCLOHDirective &D : Directives)
EmitSize += D.getEmitSize(ObjWriter, Layout);
}
return EmitSize;
}
void emit(MachObjectWriter &ObjWriter, const MCAsmLayout &Layout) const {
for (const MCLOHDirective &D : Directives)
D.emit(ObjWriter, Layout);
}
void reset() {
Directives.clear();
EmitSize = 0;
}
};
using MCLOHArgs = MCLOHDirective::LOHArgs;
using MCLOHDirectives = MCLOHContainer::LOHDirectives;
}
#endif