2012-12-12 05:25:42 +08:00
|
|
|
//===-- AMDGPUAsmBackend.cpp - AMDGPU Assembler Backend -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
|
2014-07-21 22:01:08 +08:00
|
|
|
#include "MCTargetDesc/AMDGPUFixupKinds.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
|
|
|
#include "llvm/MC/MCAssembler.h"
|
2014-07-21 22:01:08 +08:00
|
|
|
#include "llvm/MC/MCFixupKindInfo.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
|
|
|
#include "llvm/MC/MCValue.h"
|
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AMDGPUMCObjectWriter : public MCObjectWriter {
|
|
|
|
public:
|
2015-04-15 06:14:34 +08:00
|
|
|
AMDGPUMCObjectWriter(raw_pwrite_stream &OS) : MCObjectWriter(OS, true) {}
|
2015-06-05 07:25:54 +08:00
|
|
|
void executePostLayoutBinding(MCAssembler &Asm,
|
2014-04-29 15:57:24 +08:00
|
|
|
const MCAsmLayout &Layout) override {
|
2012-12-12 05:25:42 +08:00
|
|
|
//XXX: Implement if necessary.
|
|
|
|
}
|
2015-06-05 06:24:41 +08:00
|
|
|
void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
|
2014-03-29 14:26:49 +08:00
|
|
|
const MCFragment *Fragment, const MCFixup &Fixup,
|
|
|
|
MCValue Target, bool &IsPCRel,
|
|
|
|
uint64_t &FixedValue) override {
|
2012-12-12 05:25:42 +08:00
|
|
|
assert(!"Not implemented");
|
|
|
|
}
|
|
|
|
|
2015-06-05 06:24:41 +08:00
|
|
|
void writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class AMDGPUAsmBackend : public MCAsmBackend {
|
|
|
|
public:
|
|
|
|
AMDGPUAsmBackend(const Target &T)
|
|
|
|
: MCAsmBackend() {}
|
|
|
|
|
2014-07-21 22:01:14 +08:00
|
|
|
unsigned getNumFixupKinds() const override { return AMDGPU::NumTargetFixupKinds; };
|
2014-04-29 15:57:24 +08:00
|
|
|
void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
|
|
|
|
uint64_t Value, bool IsPCRel) const override;
|
|
|
|
bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
|
|
|
|
const MCRelaxableFragment *DF,
|
|
|
|
const MCAsmLayout &Layout) const override {
|
2012-12-12 05:25:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-29 15:57:24 +08:00
|
|
|
void relaxInstruction(const MCInst &Inst, MCInst &Res) const override {
|
2012-12-12 05:25:42 +08:00
|
|
|
assert(!"Not implemented");
|
|
|
|
}
|
2014-04-29 15:57:24 +08:00
|
|
|
bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
|
2014-10-04 03:02:02 +08:00
|
|
|
bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
|
2014-07-21 22:01:08 +08:00
|
|
|
|
|
|
|
const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
|
2012-12-12 05:25:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} //End anonymous namespace
|
|
|
|
|
2015-06-05 06:24:41 +08:00
|
|
|
void AMDGPUMCObjectWriter::writeObject(MCAssembler &Asm,
|
2012-12-12 05:25:42 +08:00
|
|
|
const MCAsmLayout &Layout) {
|
|
|
|
for (MCAssembler::iterator I = Asm.begin(), E = Asm.end(); I != E; ++I) {
|
2015-05-26 10:17:21 +08:00
|
|
|
Asm.writeSectionData(&*I, Layout);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 01:03:08 +08:00
|
|
|
static unsigned getFixupKindNumBytes(unsigned Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case FK_Data_1:
|
|
|
|
return 1;
|
|
|
|
case FK_Data_2:
|
|
|
|
return 2;
|
|
|
|
case FK_Data_4:
|
|
|
|
return 4;
|
|
|
|
case FK_Data_8:
|
|
|
|
return 8;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown fixup kind!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-17 23:14:54 +08:00
|
|
|
void AMDGPUAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
|
2014-03-29 14:26:49 +08:00
|
|
|
unsigned DataSize, uint64_t Value,
|
|
|
|
bool IsPCRel) const {
|
2012-12-17 23:14:54 +08:00
|
|
|
|
2014-07-21 22:01:14 +08:00
|
|
|
switch ((unsigned)Fixup.getKind()) {
|
|
|
|
case AMDGPU::fixup_si_sopp_br: {
|
|
|
|
uint16_t *Dst = (uint16_t*)(Data + Fixup.getOffset());
|
|
|
|
*Dst = (Value - 4) / 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case AMDGPU::fixup_si_rodata: {
|
|
|
|
uint32_t *Dst = (uint32_t*)(Data + Fixup.getOffset());
|
|
|
|
*Dst = Value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case AMDGPU::fixup_si_end_of_text: {
|
|
|
|
uint32_t *Dst = (uint32_t*)(Data + Fixup.getOffset());
|
|
|
|
// The value points to the last instruction in the text section, so we
|
|
|
|
// need to add 4 bytes to get to the start of the constants.
|
|
|
|
*Dst = Value + 4;
|
|
|
|
break;
|
|
|
|
}
|
2015-07-31 01:03:08 +08:00
|
|
|
default: {
|
|
|
|
// FIXME: Copied from AArch64
|
|
|
|
unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
|
|
|
|
if (!Value)
|
|
|
|
return; // Doesn't change encoding.
|
|
|
|
MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
|
|
|
|
|
|
|
|
// Shift the value into position.
|
|
|
|
Value <<= Info.TargetOffset;
|
|
|
|
|
|
|
|
unsigned Offset = Fixup.getOffset();
|
|
|
|
assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
|
|
|
|
|
|
|
|
// For each byte of the fragment that the fixup touches, mask in the
|
|
|
|
// bits from the fixup value.
|
|
|
|
for (unsigned i = 0; i != NumBytes; ++i)
|
|
|
|
Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
|
|
|
|
}
|
2014-07-21 22:01:14 +08:00
|
|
|
}
|
2012-12-17 23:14:54 +08:00
|
|
|
}
|
2013-04-16 01:51:21 +08:00
|
|
|
|
2014-07-21 22:01:08 +08:00
|
|
|
const MCFixupKindInfo &AMDGPUAsmBackend::getFixupKindInfo(
|
|
|
|
MCFixupKind Kind) const {
|
|
|
|
const static MCFixupKindInfo Infos[AMDGPU::NumTargetFixupKinds] = {
|
|
|
|
// name offset bits flags
|
2014-07-21 22:01:14 +08:00
|
|
|
{ "fixup_si_sopp_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
|
|
|
|
{ "fixup_si_rodata", 0, 32, 0 },
|
|
|
|
{ "fixup_si_end_of_text", 0, 32, MCFixupKindInfo::FKF_IsPCRel }
|
2014-07-21 22:01:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Kind < FirstTargetFixupKind)
|
|
|
|
return MCAsmBackend::getFixupKindInfo(Kind);
|
|
|
|
|
|
|
|
return Infos[Kind - FirstTargetFixupKind];
|
|
|
|
}
|
|
|
|
|
2014-10-04 03:02:02 +08:00
|
|
|
bool AMDGPUAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
|
2015-04-17 19:12:43 +08:00
|
|
|
OW->WriteZeros(Count);
|
2014-10-04 03:02:02 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-16 01:51:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ELFAMDGPUAsmBackend class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-05-23 23:43:05 +08:00
|
|
|
namespace {
|
|
|
|
|
2013-04-16 01:51:21 +08:00
|
|
|
class ELFAMDGPUAsmBackend : public AMDGPUAsmBackend {
|
2015-06-23 05:03:54 +08:00
|
|
|
bool Is64Bit;
|
|
|
|
|
2013-04-16 01:51:21 +08:00
|
|
|
public:
|
2015-06-23 05:03:54 +08:00
|
|
|
ELFAMDGPUAsmBackend(const Target &T, bool Is64Bit) :
|
|
|
|
AMDGPUAsmBackend(T), Is64Bit(Is64Bit) { }
|
2013-04-16 01:51:21 +08:00
|
|
|
|
2015-04-15 06:14:34 +08:00
|
|
|
MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
|
2015-06-23 05:03:54 +08:00
|
|
|
return createAMDGPUELFObjectWriter(Is64Bit, OS);
|
2013-04-16 01:51:21 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-05-23 23:43:05 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2013-09-09 10:37:14 +08:00
|
|
|
MCAsmBackend *llvm::createAMDGPUAsmBackend(const Target &T,
|
|
|
|
const MCRegisterInfo &MRI,
|
2015-09-16 00:17:27 +08:00
|
|
|
const Triple &TT, StringRef CPU) {
|
|
|
|
Triple TargetTriple(TT);
|
|
|
|
|
2015-06-23 05:03:54 +08:00
|
|
|
// Use 64-bit ELF for amdgcn
|
2015-09-16 00:17:27 +08:00
|
|
|
return new ELFAMDGPUAsmBackend(T, TargetTriple.getArch() == Triple::amdgcn);
|
2013-04-16 01:51:21 +08:00
|
|
|
}
|