#include "LanaiFixupKinds.h"
#include "MCTargetDesc/LanaiMCTargetDesc.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCDirectives.h"
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
switch (Kind) {
case FK_Data_1:
case FK_Data_2:
case FK_Data_4:
case FK_Data_8:
return Value;
case Lanai::FIXUP_LANAI_21:
case Lanai::FIXUP_LANAI_21_F:
case Lanai::FIXUP_LANAI_25:
case Lanai::FIXUP_LANAI_32:
case Lanai::FIXUP_LANAI_HI16:
case Lanai::FIXUP_LANAI_LO16:
return Value;
default:
llvm_unreachable("Unknown fixup kind!");
}
}
namespace {
class LanaiAsmBackend : public MCAsmBackend {
Triple::OSType OSType;
public:
LanaiAsmBackend(const Target &T, Triple::OSType OST)
: MCAsmBackend(support::big), OSType(OST) {}
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target, MutableArrayRef<char> Data,
uint64_t Value, bool IsResolved,
const MCSubtargetInfo *STI) const override;
std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override;
bool fixupNeedsRelaxation(const MCFixup & , uint64_t ,
const MCRelaxableFragment * ,
const MCAsmLayout & ) const override {
return false;
}
const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
unsigned getNumFixupKinds() const override {
return Lanai::NumTargetFixupKinds;
}
bool writeNopData(raw_ostream &OS, uint64_t Count,
const MCSubtargetInfo *STI) const override;
};
bool LanaiAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
const MCSubtargetInfo *STI) const {
if ((Count % 4) != 0)
return false;
for (uint64_t i = 0; i < Count; i += 4)
OS.write("\x15\0\0\0", 4);
return true;
}
void LanaiAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
const MCValue &Target,
MutableArrayRef<char> Data, uint64_t Value,
bool ,
const MCSubtargetInfo * ) const {
MCFixupKind Kind = Fixup.getKind();
Value = adjustFixupValue(static_cast<unsigned>(Kind), Value);
if (!Value)
return;
unsigned Offset = Fixup.getOffset();
unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
unsigned FullSize = 4;
uint64_t CurVal = 0;
for (unsigned i = 0; i != NumBytes; ++i) {
unsigned Idx = (FullSize - 1 - i);
CurVal |= static_cast<uint64_t>(static_cast<uint8_t>(Data[Offset + Idx]))
<< (i * 8);
}
uint64_t Mask =
(static_cast<uint64_t>(-1) >> (64 - getFixupKindInfo(Kind).TargetSize));
CurVal |= Value & Mask;
for (unsigned i = 0; i != NumBytes; ++i) {
unsigned Idx = (FullSize - 1 - i);
Data[Offset + Idx] = static_cast<uint8_t>((CurVal >> (i * 8)) & 0xff);
}
}
std::unique_ptr<MCObjectTargetWriter>
LanaiAsmBackend::createObjectTargetWriter() const {
return createLanaiELFObjectWriter(MCELFObjectTargetWriter::getOSABI(OSType));
}
const MCFixupKindInfo &
LanaiAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
static const MCFixupKindInfo Infos[Lanai::NumTargetFixupKinds] = {
{"FIXUP_LANAI_NONE", 0, 32, 0},
{"FIXUP_LANAI_21", 16, 16 , 0},
{"FIXUP_LANAI_21_F", 16, 16 , 0},
{"FIXUP_LANAI_25", 7, 25, 0},
{"FIXUP_LANAI_32", 0, 32, 0},
{"FIXUP_LANAI_HI16", 16, 16, 0},
{"FIXUP_LANAI_LO16", 16, 16, 0}};
if (Kind < FirstTargetFixupKind)
return MCAsmBackend::getFixupKindInfo(Kind);
assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
"Invalid kind!");
return Infos[Kind - FirstTargetFixupKind];
}
}
MCAsmBackend *llvm::createLanaiAsmBackend(const Target &T,
const MCSubtargetInfo &STI,
const MCRegisterInfo & ,
const MCTargetOptions & ) {
const Triple &TT = STI.getTargetTriple();
if (!TT.isOSBinFormatELF())
llvm_unreachable("OS not supported");
return new LanaiAsmBackend(T, TT.getOS());
}