2014-01-05 02:13:48 +00:00
|
|
|
//===-- SparcAsmBackend.cpp - Sparc Assembler Backend ---------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
|
|
|
#include "MCTargetDesc/SparcFixupKinds.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "MCTargetDesc/SparcMCTargetDesc.h"
|
2014-01-06 01:22:54 +00:00
|
|
|
#include "llvm/MC/MCELFObjectWriter.h"
|
2014-03-01 05:07:21 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2014-01-05 02:13:48 +00:00
|
|
|
#include "llvm/MC/MCFixupKindInfo.h"
|
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
2014-03-01 05:07:21 +00:00
|
|
|
#include "llvm/MC/MCValue.h"
|
2014-01-05 02:13:48 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-01-06 01:22:54 +00:00
|
|
|
static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
|
|
|
|
switch (Kind) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown fixup kind!");
|
|
|
|
case FK_Data_1:
|
|
|
|
case FK_Data_2:
|
|
|
|
case FK_Data_4:
|
|
|
|
case FK_Data_8:
|
|
|
|
return Value;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
2014-02-07 04:24:35 +00:00
|
|
|
case Sparc::fixup_sparc_wplt30:
|
2014-01-06 01:22:54 +00:00
|
|
|
case Sparc::fixup_sparc_call30:
|
2014-01-07 08:00:49 +00:00
|
|
|
return (Value >> 2) & 0x3fffffff;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
2014-01-06 01:22:54 +00:00
|
|
|
case Sparc::fixup_sparc_br22:
|
2014-01-07 08:00:49 +00:00
|
|
|
return (Value >> 2) & 0x3fffff;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
2014-01-06 01:22:54 +00:00
|
|
|
case Sparc::fixup_sparc_br19:
|
2014-01-08 06:46:51 +00:00
|
|
|
return (Value >> 2) & 0x7ffff;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
2014-03-02 09:46:56 +00:00
|
|
|
case Sparc::fixup_sparc_br16_2:
|
|
|
|
return (Value >> 2) & 0xc000;
|
|
|
|
|
|
|
|
case Sparc::fixup_sparc_br16_14:
|
|
|
|
return (Value >> 2) & 0x3fff;
|
|
|
|
|
2014-02-07 04:24:35 +00:00
|
|
|
case Sparc::fixup_sparc_pc22:
|
|
|
|
case Sparc::fixup_sparc_got22:
|
2014-02-07 05:54:20 +00:00
|
|
|
case Sparc::fixup_sparc_tls_gd_hi22:
|
|
|
|
case Sparc::fixup_sparc_tls_ldm_hi22:
|
|
|
|
case Sparc::fixup_sparc_tls_ie_hi22:
|
2014-01-06 01:22:54 +00:00
|
|
|
case Sparc::fixup_sparc_hi22:
|
|
|
|
return (Value >> 10) & 0x3fffff;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
2014-02-07 04:24:35 +00:00
|
|
|
case Sparc::fixup_sparc_pc10:
|
|
|
|
case Sparc::fixup_sparc_got10:
|
2014-02-07 05:54:20 +00:00
|
|
|
case Sparc::fixup_sparc_tls_gd_lo10:
|
|
|
|
case Sparc::fixup_sparc_tls_ldm_lo10:
|
|
|
|
case Sparc::fixup_sparc_tls_ie_lo10:
|
2014-01-06 01:22:54 +00:00
|
|
|
case Sparc::fixup_sparc_lo10:
|
|
|
|
return Value & 0x3ff;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
|
|
|
case Sparc::fixup_sparc_tls_ldo_hix22:
|
|
|
|
case Sparc::fixup_sparc_tls_le_hix22:
|
|
|
|
return (~Value >> 10) & 0x3fffff;
|
|
|
|
|
|
|
|
case Sparc::fixup_sparc_tls_ldo_lox10:
|
|
|
|
case Sparc::fixup_sparc_tls_le_lox10:
|
|
|
|
return (~(~Value & 0x3ff)) & 0x1fff;
|
|
|
|
|
2014-01-06 01:22:54 +00:00
|
|
|
case Sparc::fixup_sparc_h44:
|
|
|
|
return (Value >> 22) & 0x3fffff;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
2014-01-06 01:22:54 +00:00
|
|
|
case Sparc::fixup_sparc_m44:
|
|
|
|
return (Value >> 12) & 0x3ff;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
2014-01-06 01:22:54 +00:00
|
|
|
case Sparc::fixup_sparc_l44:
|
|
|
|
return Value & 0xfff;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
2014-01-06 01:22:54 +00:00
|
|
|
case Sparc::fixup_sparc_hh:
|
|
|
|
return (Value >> 42) & 0x3fffff;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
2014-01-06 01:22:54 +00:00
|
|
|
case Sparc::fixup_sparc_hm:
|
2014-01-07 08:00:49 +00:00
|
|
|
return (Value >> 32) & 0x3ff;
|
2014-02-07 05:54:20 +00:00
|
|
|
|
|
|
|
case Sparc::fixup_sparc_tls_gd_add:
|
|
|
|
case Sparc::fixup_sparc_tls_gd_call:
|
|
|
|
case Sparc::fixup_sparc_tls_ldm_add:
|
|
|
|
case Sparc::fixup_sparc_tls_ldm_call:
|
|
|
|
case Sparc::fixup_sparc_tls_ldo_add:
|
|
|
|
case Sparc::fixup_sparc_tls_ie_ld:
|
|
|
|
case Sparc::fixup_sparc_tls_ie_ldx:
|
|
|
|
case Sparc::fixup_sparc_tls_ie_add:
|
|
|
|
return 0;
|
2014-01-06 01:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-05 02:13:48 +00:00
|
|
|
namespace {
|
|
|
|
class SparcAsmBackend : public MCAsmBackend {
|
2014-01-06 01:22:54 +00:00
|
|
|
const Target &TheTarget;
|
2014-01-05 02:13:48 +00:00
|
|
|
public:
|
2014-01-06 01:22:54 +00:00
|
|
|
SparcAsmBackend(const Target &T) : MCAsmBackend(), TheTarget(T) {}
|
2014-01-05 02:13:48 +00:00
|
|
|
|
|
|
|
unsigned getNumFixupKinds() const {
|
|
|
|
return Sparc::NumTargetFixupKinds;
|
|
|
|
}
|
|
|
|
|
|
|
|
const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
|
|
|
|
const static MCFixupKindInfo Infos[Sparc::NumTargetFixupKinds] = {
|
|
|
|
// name offset bits flags
|
2014-01-07 08:00:49 +00:00
|
|
|
{ "fixup_sparc_call30", 2, 30, MCFixupKindInfo::FKF_IsPCRel },
|
|
|
|
{ "fixup_sparc_br22", 10, 22, MCFixupKindInfo::FKF_IsPCRel },
|
|
|
|
{ "fixup_sparc_br19", 13, 19, MCFixupKindInfo::FKF_IsPCRel },
|
2014-03-02 09:46:56 +00:00
|
|
|
{ "fixup_sparc_br16_2", 10, 2, MCFixupKindInfo::FKF_IsPCRel },
|
|
|
|
{ "fixup_sparc_br16_14", 18, 14, MCFixupKindInfo::FKF_IsPCRel },
|
2014-01-07 08:00:49 +00:00
|
|
|
{ "fixup_sparc_hi22", 10, 22, 0 },
|
|
|
|
{ "fixup_sparc_lo10", 22, 10, 0 },
|
|
|
|
{ "fixup_sparc_h44", 10, 22, 0 },
|
|
|
|
{ "fixup_sparc_m44", 22, 10, 0 },
|
|
|
|
{ "fixup_sparc_l44", 20, 12, 0 },
|
|
|
|
{ "fixup_sparc_hh", 10, 22, 0 },
|
|
|
|
{ "fixup_sparc_hm", 22, 10, 0 },
|
2014-02-07 04:24:35 +00:00
|
|
|
{ "fixup_sparc_pc22", 10, 22, MCFixupKindInfo::FKF_IsPCRel },
|
|
|
|
{ "fixup_sparc_pc10", 22, 10, MCFixupKindInfo::FKF_IsPCRel },
|
|
|
|
{ "fixup_sparc_got22", 10, 22, 0 },
|
|
|
|
{ "fixup_sparc_got10", 22, 10, 0 },
|
2014-02-07 05:54:20 +00:00
|
|
|
{ "fixup_sparc_wplt30", 2, 30, MCFixupKindInfo::FKF_IsPCRel },
|
|
|
|
{ "fixup_sparc_tls_gd_hi22", 10, 22, 0 },
|
|
|
|
{ "fixup_sparc_tls_gd_lo10", 22, 10, 0 },
|
|
|
|
{ "fixup_sparc_tls_gd_add", 0, 0, 0 },
|
|
|
|
{ "fixup_sparc_tls_gd_call", 0, 0, 0 },
|
|
|
|
{ "fixup_sparc_tls_ldm_hi22", 10, 22, 0 },
|
|
|
|
{ "fixup_sparc_tls_ldm_lo10", 22, 10, 0 },
|
|
|
|
{ "fixup_sparc_tls_ldm_add", 0, 0, 0 },
|
|
|
|
{ "fixup_sparc_tls_ldm_call", 0, 0, 0 },
|
|
|
|
{ "fixup_sparc_tls_ldo_hix22", 10, 22, 0 },
|
|
|
|
{ "fixup_sparc_tls_ldo_lox10", 22, 10, 0 },
|
|
|
|
{ "fixup_sparc_tls_ldo_add", 0, 0, 0 },
|
|
|
|
{ "fixup_sparc_tls_ie_hi22", 10, 22, 0 },
|
|
|
|
{ "fixup_sparc_tls_ie_lo10", 22, 10, 0 },
|
|
|
|
{ "fixup_sparc_tls_ie_ld", 0, 0, 0 },
|
|
|
|
{ "fixup_sparc_tls_ie_ldx", 0, 0, 0 },
|
|
|
|
{ "fixup_sparc_tls_ie_add", 0, 0, 0 },
|
|
|
|
{ "fixup_sparc_tls_le_hix22", 0, 0, 0 },
|
|
|
|
{ "fixup_sparc_tls_le_lox10", 0, 0, 0 }
|
2014-01-05 02:13:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Kind < FirstTargetFixupKind)
|
|
|
|
return MCAsmBackend::getFixupKindInfo(Kind);
|
|
|
|
|
|
|
|
assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
|
|
|
|
"Invalid kind!");
|
|
|
|
return Infos[Kind - FirstTargetFixupKind];
|
|
|
|
}
|
|
|
|
|
2014-03-28 16:14:12 +00:00
|
|
|
void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
|
|
|
|
const MCFixup &Fixup, const MCFragment *DF,
|
|
|
|
const MCValue &Target, uint64_t &Value,
|
|
|
|
bool &IsResolved) override {
|
2014-02-07 04:24:35 +00:00
|
|
|
switch ((Sparc::Fixups)Fixup.getKind()) {
|
|
|
|
default: break;
|
2014-02-07 05:54:20 +00:00
|
|
|
case Sparc::fixup_sparc_wplt30:
|
2014-03-01 05:07:21 +00:00
|
|
|
if (Target.getSymA()->getSymbol().isTemporary())
|
|
|
|
return;
|
2014-02-07 05:54:20 +00:00
|
|
|
case Sparc::fixup_sparc_tls_gd_hi22:
|
|
|
|
case Sparc::fixup_sparc_tls_gd_lo10:
|
|
|
|
case Sparc::fixup_sparc_tls_gd_add:
|
|
|
|
case Sparc::fixup_sparc_tls_gd_call:
|
|
|
|
case Sparc::fixup_sparc_tls_ldm_hi22:
|
|
|
|
case Sparc::fixup_sparc_tls_ldm_lo10:
|
|
|
|
case Sparc::fixup_sparc_tls_ldm_add:
|
|
|
|
case Sparc::fixup_sparc_tls_ldm_call:
|
|
|
|
case Sparc::fixup_sparc_tls_ldo_hix22:
|
|
|
|
case Sparc::fixup_sparc_tls_ldo_lox10:
|
|
|
|
case Sparc::fixup_sparc_tls_ldo_add:
|
|
|
|
case Sparc::fixup_sparc_tls_ie_hi22:
|
|
|
|
case Sparc::fixup_sparc_tls_ie_lo10:
|
|
|
|
case Sparc::fixup_sparc_tls_ie_ld:
|
|
|
|
case Sparc::fixup_sparc_tls_ie_ldx:
|
|
|
|
case Sparc::fixup_sparc_tls_ie_add:
|
|
|
|
case Sparc::fixup_sparc_tls_le_hix22:
|
|
|
|
case Sparc::fixup_sparc_tls_le_lox10: IsResolved = false; break;
|
2014-02-07 04:24:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-05 02:13:48 +00:00
|
|
|
bool mayNeedRelaxation(const MCInst &Inst) const {
|
|
|
|
// FIXME.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// fixupNeedsRelaxation - Target specific predicate for whether a given
|
|
|
|
/// fixup requires the associated instruction to be relaxed.
|
|
|
|
bool fixupNeedsRelaxation(const MCFixup &Fixup,
|
|
|
|
uint64_t Value,
|
|
|
|
const MCRelaxableFragment *DF,
|
|
|
|
const MCAsmLayout &Layout) const {
|
|
|
|
// FIXME.
|
|
|
|
assert(0 && "fixupNeedsRelaxation() unimplemented");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
|
|
|
|
// FIXME.
|
|
|
|
assert(0 && "relaxInstruction() unimplemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
|
2014-03-01 05:45:09 +00:00
|
|
|
// Cannot emit NOP with size not multiple of 32 bits.
|
|
|
|
if (Count % 4 != 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
uint64_t NumNops = Count / 4;
|
|
|
|
for (uint64_t i = 0; i != NumNops; ++i)
|
|
|
|
OW->Write32(0x01000000);
|
|
|
|
|
2014-01-05 02:13:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-01-06 01:22:54 +00:00
|
|
|
|
|
|
|
bool is64Bit() const {
|
|
|
|
StringRef name = TheTarget.getName();
|
|
|
|
return name == "sparcv9";
|
|
|
|
}
|
2014-01-05 02:13:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ELFSparcAsmBackend : public SparcAsmBackend {
|
2014-01-06 01:22:54 +00:00
|
|
|
Triple::OSType OSType;
|
2014-01-05 02:13:48 +00:00
|
|
|
public:
|
|
|
|
ELFSparcAsmBackend(const Target &T, Triple::OSType OSType) :
|
2014-01-06 01:22:54 +00:00
|
|
|
SparcAsmBackend(T), OSType(OSType) { }
|
2014-01-05 02:13:48 +00:00
|
|
|
|
|
|
|
void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
|
2014-03-29 06:26:49 +00:00
|
|
|
uint64_t Value, bool IsPCRel) const {
|
2014-01-06 01:22:54 +00:00
|
|
|
|
|
|
|
Value = adjustFixupValue(Fixup.getKind(), Value);
|
|
|
|
if (!Value) return; // Doesn't change encoding.
|
|
|
|
|
|
|
|
unsigned Offset = Fixup.getOffset();
|
|
|
|
|
|
|
|
// For each byte of the fragment that the fixup touches, mask in the bits
|
|
|
|
// from the fixup value. The Value has been "split up" into the
|
|
|
|
// appropriate bitfields above.
|
|
|
|
for (unsigned i = 0; i != 4; ++i)
|
|
|
|
Data[Offset + i] |= uint8_t((Value >> ((4 - i - 1)*8)) & 0xff);
|
|
|
|
|
2014-01-05 02:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
|
2014-01-06 01:22:54 +00:00
|
|
|
uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
|
|
|
|
return createSparcELFObjectWriter(OS, is64Bit(), OSABI);
|
2014-01-05 02:13:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
|
|
|
MCAsmBackend *llvm::createSparcAsmBackend(const Target &T,
|
|
|
|
const MCRegisterInfo &MRI,
|
|
|
|
StringRef TT,
|
|
|
|
StringRef CPU) {
|
|
|
|
return new ELFSparcAsmBackend(T, Triple(TT).getOS());
|
|
|
|
}
|