Path: blob/main/contrib/llvm-project/llvm/lib/Target/VE/MCTargetDesc/VEAsmBackend.cpp
35294 views
//===-- VEAsmBackend.cpp - VE Assembler Backend ---------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "MCTargetDesc/VEFixupKinds.h"9#include "MCTargetDesc/VEMCTargetDesc.h"10#include "llvm/MC/MCAsmBackend.h"11#include "llvm/MC/MCELFObjectWriter.h"12#include "llvm/MC/MCExpr.h"13#include "llvm/MC/MCFixupKindInfo.h"14#include "llvm/MC/MCObjectWriter.h"15#include "llvm/MC/MCSubtargetInfo.h"16#include "llvm/MC/MCValue.h"17#include "llvm/MC/TargetRegistry.h"18#include "llvm/Support/EndianStream.h"1920using namespace llvm;2122static uint64_t adjustFixupValue(unsigned Kind, uint64_t Value) {23switch (Kind) {24default:25llvm_unreachable("Unknown fixup kind!");26case FK_Data_1:27case FK_Data_2:28case FK_Data_4:29case FK_Data_8:30case FK_PCRel_1:31case FK_PCRel_2:32case FK_PCRel_4:33case FK_PCRel_8:34return Value;35case VE::fixup_ve_hi32:36case VE::fixup_ve_pc_hi32:37case VE::fixup_ve_got_hi32:38case VE::fixup_ve_gotoff_hi32:39case VE::fixup_ve_plt_hi32:40case VE::fixup_ve_tls_gd_hi32:41case VE::fixup_ve_tpoff_hi32:42return (Value >> 32) & 0xffffffff;43case VE::fixup_ve_reflong:44case VE::fixup_ve_srel32:45case VE::fixup_ve_lo32:46case VE::fixup_ve_pc_lo32:47case VE::fixup_ve_got_lo32:48case VE::fixup_ve_gotoff_lo32:49case VE::fixup_ve_plt_lo32:50case VE::fixup_ve_tls_gd_lo32:51case VE::fixup_ve_tpoff_lo32:52return Value & 0xffffffff;53}54}5556/// getFixupKindNumBytes - The number of bytes the fixup may change.57static unsigned getFixupKindNumBytes(unsigned Kind) {58switch (Kind) {59default:60llvm_unreachable("Unknown fixup kind!");61case FK_Data_1:62case FK_PCRel_1:63return 1;64case FK_Data_2:65case FK_PCRel_2:66return 2;67return 4;68case FK_Data_4:69case FK_PCRel_4:70case VE::fixup_ve_reflong:71case VE::fixup_ve_srel32:72case VE::fixup_ve_hi32:73case VE::fixup_ve_lo32:74case VE::fixup_ve_pc_hi32:75case VE::fixup_ve_pc_lo32:76case VE::fixup_ve_got_hi32:77case VE::fixup_ve_got_lo32:78case VE::fixup_ve_gotoff_hi32:79case VE::fixup_ve_gotoff_lo32:80case VE::fixup_ve_plt_hi32:81case VE::fixup_ve_plt_lo32:82case VE::fixup_ve_tls_gd_hi32:83case VE::fixup_ve_tls_gd_lo32:84case VE::fixup_ve_tpoff_hi32:85case VE::fixup_ve_tpoff_lo32:86return 4;87case FK_Data_8:88case FK_PCRel_8:89return 8;90}91}9293namespace {94class VEAsmBackend : public MCAsmBackend {95protected:96const Target &TheTarget;9798public:99VEAsmBackend(const Target &T)100: MCAsmBackend(llvm::endianness::little), TheTarget(T) {}101102unsigned getNumFixupKinds() const override { return VE::NumTargetFixupKinds; }103104const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {105const static MCFixupKindInfo Infos[VE::NumTargetFixupKinds] = {106// name, offset, bits, flags107{"fixup_ve_reflong", 0, 32, 0},108{"fixup_ve_srel32", 0, 32, MCFixupKindInfo::FKF_IsPCRel},109{"fixup_ve_hi32", 0, 32, 0},110{"fixup_ve_lo32", 0, 32, 0},111{"fixup_ve_pc_hi32", 0, 32, MCFixupKindInfo::FKF_IsPCRel},112{"fixup_ve_pc_lo32", 0, 32, MCFixupKindInfo::FKF_IsPCRel},113{"fixup_ve_got_hi32", 0, 32, 0},114{"fixup_ve_got_lo32", 0, 32, 0},115{"fixup_ve_gotoff_hi32", 0, 32, 0},116{"fixup_ve_gotoff_lo32", 0, 32, 0},117{"fixup_ve_plt_hi32", 0, 32, 0},118{"fixup_ve_plt_lo32", 0, 32, 0},119{"fixup_ve_tls_gd_hi32", 0, 32, 0},120{"fixup_ve_tls_gd_lo32", 0, 32, 0},121{"fixup_ve_tpoff_hi32", 0, 32, 0},122{"fixup_ve_tpoff_lo32", 0, 32, 0},123};124125if (Kind < FirstTargetFixupKind)126return MCAsmBackend::getFixupKindInfo(Kind);127128assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&129"Invalid kind!");130return Infos[Kind - FirstTargetFixupKind];131}132133bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,134const MCValue &Target,135const MCSubtargetInfo *STI) override {136switch ((VE::Fixups)Fixup.getKind()) {137default:138return false;139case VE::fixup_ve_tls_gd_hi32:140case VE::fixup_ve_tls_gd_lo32:141case VE::fixup_ve_tpoff_hi32:142case VE::fixup_ve_tpoff_lo32:143return true;144}145}146147bool mayNeedRelaxation(const MCInst &Inst,148const MCSubtargetInfo &STI) const override {149// Not implemented yet. For example, if we have a branch with150// lager than SIMM32 immediate value, we want to relaxation such151// branch instructions.152return false;153}154155void relaxInstruction(MCInst &Inst,156const MCSubtargetInfo &STI) const override {157// Aurora VE doesn't support relaxInstruction yet.158llvm_unreachable("relaxInstruction() should not be called");159}160161bool writeNopData(raw_ostream &OS, uint64_t Count,162const MCSubtargetInfo *STI) const override {163if ((Count % 8) != 0)164return false;165166for (uint64_t i = 0; i < Count; i += 8)167support::endian::write<uint64_t>(OS, 0x7900000000000000ULL,168llvm::endianness::little);169170return true;171}172};173174class ELFVEAsmBackend : public VEAsmBackend {175Triple::OSType OSType;176177public:178ELFVEAsmBackend(const Target &T, Triple::OSType OSType)179: VEAsmBackend(T), OSType(OSType) {}180181void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,182const MCValue &Target, MutableArrayRef<char> Data,183uint64_t Value, bool IsResolved,184const MCSubtargetInfo *STI) const override {185Value = adjustFixupValue(Fixup.getKind(), Value);186if (!Value)187return; // Doesn't change encoding.188189MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());190191// Shift the value into position.192Value <<= Info.TargetOffset;193194unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());195unsigned Offset = Fixup.getOffset();196assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");197// For each byte of the fragment that the fixup touches, mask in the bits198// from the fixup value. The Value has been "split up" into the199// appropriate bitfields above.200for (unsigned i = 0; i != NumBytes; ++i) {201unsigned Idx =202Endian == llvm::endianness::little ? i : (NumBytes - 1) - i;203Data[Offset + Idx] |= static_cast<uint8_t>((Value >> (i * 8)) & 0xff);204}205}206207std::unique_ptr<MCObjectTargetWriter>208createObjectTargetWriter() const override {209uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);210return createVEELFObjectWriter(OSABI);211}212};213} // end anonymous namespace214215MCAsmBackend *llvm::createVEAsmBackend(const Target &T,216const MCSubtargetInfo &STI,217const MCRegisterInfo &MRI,218const MCTargetOptions &Options) {219return new ELFVEAsmBackend(T, STI.getTargetTriple().getOS());220}221222223