Path: blob/main/contrib/llvm-project/llvm/lib/Target/Lanai/MCTargetDesc/LanaiAsmBackend.cpp
35295 views
//===-- LanaiAsmBackend.cpp - Lanai 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 "LanaiFixupKinds.h"9#include "MCTargetDesc/LanaiMCTargetDesc.h"10#include "llvm/MC/MCAsmBackend.h"11#include "llvm/MC/MCAssembler.h"12#include "llvm/MC/MCDirectives.h"13#include "llvm/MC/MCELFObjectWriter.h"14#include "llvm/MC/MCFixupKindInfo.h"15#include "llvm/MC/MCObjectWriter.h"16#include "llvm/MC/MCSubtargetInfo.h"17#include "llvm/Support/ErrorHandling.h"18#include "llvm/Support/raw_ostream.h"1920using namespace llvm;2122// Prepare value for the target space23static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {24switch (Kind) {25case FK_Data_1:26case FK_Data_2:27case FK_Data_4:28case FK_Data_8:29return Value;30case Lanai::FIXUP_LANAI_21:31case Lanai::FIXUP_LANAI_21_F:32case Lanai::FIXUP_LANAI_25:33case Lanai::FIXUP_LANAI_32:34case Lanai::FIXUP_LANAI_HI16:35case Lanai::FIXUP_LANAI_LO16:36return Value;37default:38llvm_unreachable("Unknown fixup kind!");39}40}4142namespace {43class LanaiAsmBackend : public MCAsmBackend {44Triple::OSType OSType;4546public:47LanaiAsmBackend(const Target &T, Triple::OSType OST)48: MCAsmBackend(llvm::endianness::big), OSType(OST) {}4950void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,51const MCValue &Target, MutableArrayRef<char> Data,52uint64_t Value, bool IsResolved,53const MCSubtargetInfo *STI) const override;5455std::unique_ptr<MCObjectTargetWriter>56createObjectTargetWriter() const override;5758const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;5960unsigned getNumFixupKinds() const override {61return Lanai::NumTargetFixupKinds;62}6364bool writeNopData(raw_ostream &OS, uint64_t Count,65const MCSubtargetInfo *STI) const override;66};6768bool LanaiAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,69const MCSubtargetInfo *STI) const {70if ((Count % 4) != 0)71return false;7273for (uint64_t i = 0; i < Count; i += 4)74OS.write("\x15\0\0\0", 4);7576return true;77}7879void LanaiAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,80const MCValue &Target,81MutableArrayRef<char> Data, uint64_t Value,82bool /*IsResolved*/,83const MCSubtargetInfo * /*STI*/) const {84MCFixupKind Kind = Fixup.getKind();85Value = adjustFixupValue(static_cast<unsigned>(Kind), Value);8687if (!Value)88return; // This value doesn't change the encoding8990// Where in the object and where the number of bytes that need91// fixing up92unsigned Offset = Fixup.getOffset();93unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;94unsigned FullSize = 4;9596// Grab current value, if any, from bits.97uint64_t CurVal = 0;9899// Load instruction and apply value100for (unsigned i = 0; i != NumBytes; ++i) {101unsigned Idx = (FullSize - 1 - i);102CurVal |= static_cast<uint64_t>(static_cast<uint8_t>(Data[Offset + Idx]))103<< (i * 8);104}105106uint64_t Mask =107(static_cast<uint64_t>(-1) >> (64 - getFixupKindInfo(Kind).TargetSize));108CurVal |= Value & Mask;109110// Write out the fixed up bytes back to the code/data bits.111for (unsigned i = 0; i != NumBytes; ++i) {112unsigned Idx = (FullSize - 1 - i);113Data[Offset + Idx] = static_cast<uint8_t>((CurVal >> (i * 8)) & 0xff);114}115}116117std::unique_ptr<MCObjectTargetWriter>118LanaiAsmBackend::createObjectTargetWriter() const {119return createLanaiELFObjectWriter(MCELFObjectTargetWriter::getOSABI(OSType));120}121122const MCFixupKindInfo &123LanaiAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {124static const MCFixupKindInfo Infos[Lanai::NumTargetFixupKinds] = {125// This table *must* be in same the order of fixup_* kinds in126// LanaiFixupKinds.h.127// Note: The number of bits indicated here are assumed to be contiguous.128// This does not hold true for LANAI_21 and LANAI_21_F which are applied129// to bits 0x7cffff and 0x7cfffc, respectively. Since the 'bits' counts130// here are used only for cosmetic purposes, we set the size to 16 bits131// for these 21-bit relocation as llvm/lib/MC/MCAsmStreamer.cpp checks132// no bits are set in the fixup range.133//134// name offset bits flags135{"FIXUP_LANAI_NONE", 0, 32, 0},136{"FIXUP_LANAI_21", 16, 16 /*21*/, 0},137{"FIXUP_LANAI_21_F", 16, 16 /*21*/, 0},138{"FIXUP_LANAI_25", 7, 25, 0},139{"FIXUP_LANAI_32", 0, 32, 0},140{"FIXUP_LANAI_HI16", 16, 16, 0},141{"FIXUP_LANAI_LO16", 16, 16, 0}};142143if (Kind < FirstTargetFixupKind)144return MCAsmBackend::getFixupKindInfo(Kind);145146assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&147"Invalid kind!");148return Infos[Kind - FirstTargetFixupKind];149}150151} // namespace152153MCAsmBackend *llvm::createLanaiAsmBackend(const Target &T,154const MCSubtargetInfo &STI,155const MCRegisterInfo & /*MRI*/,156const MCTargetOptions & /*Options*/) {157const Triple &TT = STI.getTargetTriple();158if (!TT.isOSBinFormatELF())159llvm_unreachable("OS not supported");160161return new LanaiAsmBackend(T, TT.getOS());162}163164165