Path: blob/main/contrib/llvm-project/llvm/lib/Target/AVR/MCTargetDesc/AVRAsmBackend.cpp
35294 views
//===-- AVRAsmBackend.cpp - AVR Asm 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//===----------------------------------------------------------------------===//7//8// This file implements the AVRAsmBackend class.9//10//===----------------------------------------------------------------------===//1112#include "MCTargetDesc/AVRAsmBackend.h"13#include "MCTargetDesc/AVRFixupKinds.h"14#include "MCTargetDesc/AVRMCTargetDesc.h"15#include "llvm/ADT/StringSwitch.h"16#include "llvm/MC/MCAsmBackend.h"17#include "llvm/MC/MCAssembler.h"18#include "llvm/MC/MCContext.h"19#include "llvm/MC/MCDirectives.h"20#include "llvm/MC/MCELFObjectWriter.h"21#include "llvm/MC/MCExpr.h"22#include "llvm/MC/MCFixupKindInfo.h"23#include "llvm/MC/MCObjectWriter.h"24#include "llvm/MC/MCSubtargetInfo.h"25#include "llvm/MC/MCValue.h"26#include "llvm/Support/ErrorHandling.h"27#include "llvm/Support/MathExtras.h"28#include "llvm/Support/raw_ostream.h"2930// FIXME: we should be doing checks to make sure asm operands31// are not out of bounds.3233namespace adjust {3435using namespace llvm;3637static void signed_width(unsigned Width, uint64_t Value,38std::string Description, const MCFixup &Fixup,39MCContext *Ctx = nullptr) {40if (!isIntN(Width, Value)) {41std::string Diagnostic = "out of range " + Description;4243int64_t Min = minIntN(Width);44int64_t Max = maxIntN(Width);4546Diagnostic += " (expected an integer in the range " + std::to_string(Min) +47" to " + std::to_string(Max) + ")";4849if (Ctx) {50Ctx->reportError(Fixup.getLoc(), Diagnostic);51} else {52llvm_unreachable(Diagnostic.c_str());53}54}55}5657static void unsigned_width(unsigned Width, uint64_t Value,58std::string Description, const MCFixup &Fixup,59MCContext *Ctx = nullptr) {60if (!isUIntN(Width, Value)) {61std::string Diagnostic = "out of range " + Description;6263int64_t Max = maxUIntN(Width);6465Diagnostic +=66" (expected an integer in the range 0 to " + std::to_string(Max) + ")";6768if (Ctx) {69Ctx->reportError(Fixup.getLoc(), Diagnostic);70} else {71llvm_unreachable(Diagnostic.c_str());72}73}74}7576/// Adjusts the value of a branch target before fixup application.77static void adjustBranch(unsigned Size, const MCFixup &Fixup, uint64_t &Value,78MCContext *Ctx = nullptr) {79// We have one extra bit of precision because the value is rightshifted by80// one.81unsigned_width(Size + 1, Value, std::string("branch target"), Fixup, Ctx);8283// Rightshifts the value by one.84AVR::fixups::adjustBranchTarget(Value);85}8687/// Adjusts the value of a relative branch target before fixup application.88static void adjustRelativeBranch(unsigned Size, const MCFixup &Fixup,89uint64_t &Value, MCContext *Ctx = nullptr) {90// Jumps are relative to the current instruction.91Value -= 2;9293// We have one extra bit of precision because the value is rightshifted by94// one.95signed_width(Size + 1, Value, std::string("branch target"), Fixup, Ctx);9697// Rightshifts the value by one.98AVR::fixups::adjustBranchTarget(Value);99}100101/// 22-bit absolute fixup.102///103/// Resolves to:104/// 1001 kkkk 010k kkkk kkkk kkkk 111k kkkk105///106/// Offset of 0 (so the result is left shifted by 3 bits before application).107static void fixup_call(unsigned Size, const MCFixup &Fixup, uint64_t &Value,108MCContext *Ctx = nullptr) {109adjustBranch(Size, Fixup, Value, Ctx);110111auto top = Value & (0xf00000 << 6); // the top four bits112auto middle = Value & (0x1ffff << 5); // the middle 13 bits113auto bottom = Value & 0x1f; // end bottom 5 bits114115Value = (top << 6) | (middle << 3) | (bottom << 0);116}117118/// 7-bit PC-relative fixup.119///120/// Resolves to:121/// 0000 00kk kkkk k000122/// Offset of 0 (so the result is left shifted by 3 bits before application).123static void fixup_7_pcrel(unsigned Size, const MCFixup &Fixup, uint64_t &Value,124MCContext *Ctx = nullptr) {125adjustRelativeBranch(Size, Fixup, Value, Ctx);126127// Because the value may be negative, we must mask out the sign bits128Value &= 0x7f;129}130131/// 12-bit PC-relative fixup.132/// Yes, the fixup is 12 bits even though the name says otherwise.133///134/// Resolves to:135/// 0000 kkkk kkkk kkkk136/// Offset of 0 (so the result isn't left-shifted before application).137static void fixup_13_pcrel(unsigned Size, const MCFixup &Fixup, uint64_t &Value,138MCContext *Ctx = nullptr) {139adjustRelativeBranch(Size, Fixup, Value, Ctx);140141// Because the value may be negative, we must mask out the sign bits142Value &= 0xfff;143}144145/// 6-bit fixup for the immediate operand of the STD/LDD family of146/// instructions.147///148/// Resolves to:149/// 10q0 qq10 0000 1qqq150static void fixup_6(const MCFixup &Fixup, uint64_t &Value,151MCContext *Ctx = nullptr) {152unsigned_width(6, Value, std::string("immediate"), Fixup, Ctx);153154Value = ((Value & 0x20) << 8) | ((Value & 0x18) << 7) | (Value & 0x07);155}156157/// 6-bit fixup for the immediate operand of the ADIW family of158/// instructions.159///160/// Resolves to:161/// 0000 0000 kk00 kkkk162static void fixup_6_adiw(const MCFixup &Fixup, uint64_t &Value,163MCContext *Ctx = nullptr) {164unsigned_width(6, Value, std::string("immediate"), Fixup, Ctx);165166Value = ((Value & 0x30) << 2) | (Value & 0x0f);167}168169/// 5-bit port number fixup on the SBIC family of instructions.170///171/// Resolves to:172/// 0000 0000 AAAA A000173static void fixup_port5(const MCFixup &Fixup, uint64_t &Value,174MCContext *Ctx = nullptr) {175unsigned_width(5, Value, std::string("port number"), Fixup, Ctx);176177Value &= 0x1f;178179Value <<= 3;180}181182/// 6-bit port number fixup on the `IN` family of instructions.183///184/// Resolves to:185/// 1011 0AAd dddd AAAA186static void fixup_port6(const MCFixup &Fixup, uint64_t &Value,187MCContext *Ctx = nullptr) {188unsigned_width(6, Value, std::string("port number"), Fixup, Ctx);189190Value = ((Value & 0x30) << 5) | (Value & 0x0f);191}192193/// 7-bit data space address fixup for the LDS/STS instructions on AVRTiny.194///195/// Resolves to:196/// 1010 ikkk dddd kkkk197static void fixup_lds_sts_16(const MCFixup &Fixup, uint64_t &Value,198MCContext *Ctx = nullptr) {199unsigned_width(7, Value, std::string("immediate"), Fixup, Ctx);200Value = ((Value & 0x70) << 8) | (Value & 0x0f);201}202203/// Adjusts a program memory address.204/// This is a simple right-shift.205static void pm(uint64_t &Value) { Value >>= 1; }206207/// Fixups relating to the LDI instruction.208namespace ldi {209210/// Adjusts a value to fix up the immediate of an `LDI Rd, K` instruction.211///212/// Resolves to:213/// 0000 KKKK 0000 KKKK214/// Offset of 0 (so the result isn't left-shifted before application).215static void fixup(unsigned Size, const MCFixup &Fixup, uint64_t &Value,216MCContext *Ctx = nullptr) {217uint64_t upper = Value & 0xf0;218uint64_t lower = Value & 0x0f;219220Value = (upper << 4) | lower;221}222223static void neg(uint64_t &Value) { Value *= -1; }224225static void lo8(unsigned Size, const MCFixup &Fixup, uint64_t &Value,226MCContext *Ctx = nullptr) {227Value &= 0xff;228ldi::fixup(Size, Fixup, Value, Ctx);229}230231static void hi8(unsigned Size, const MCFixup &Fixup, uint64_t &Value,232MCContext *Ctx = nullptr) {233Value = (Value & 0xff00) >> 8;234ldi::fixup(Size, Fixup, Value, Ctx);235}236237static void hh8(unsigned Size, const MCFixup &Fixup, uint64_t &Value,238MCContext *Ctx = nullptr) {239Value = (Value & 0xff0000) >> 16;240ldi::fixup(Size, Fixup, Value, Ctx);241}242243static void ms8(unsigned Size, const MCFixup &Fixup, uint64_t &Value,244MCContext *Ctx = nullptr) {245Value = (Value & 0xff000000) >> 24;246ldi::fixup(Size, Fixup, Value, Ctx);247}248249} // namespace ldi250} // namespace adjust251252namespace llvm {253254// Prepare value for the target space for it255void AVRAsmBackend::adjustFixupValue(const MCFixup &Fixup,256const MCValue &Target, uint64_t &Value,257MCContext *Ctx) const {258// The size of the fixup in bits.259uint64_t Size = AVRAsmBackend::getFixupKindInfo(Fixup.getKind()).TargetSize;260261unsigned Kind = Fixup.getKind();262switch (Kind) {263default:264llvm_unreachable("unhandled fixup");265case AVR::fixup_7_pcrel:266adjust::fixup_7_pcrel(Size, Fixup, Value, Ctx);267break;268case AVR::fixup_13_pcrel:269adjust::fixup_13_pcrel(Size, Fixup, Value, Ctx);270break;271case AVR::fixup_call:272adjust::fixup_call(Size, Fixup, Value, Ctx);273break;274case AVR::fixup_ldi:275adjust::ldi::fixup(Size, Fixup, Value, Ctx);276break;277case AVR::fixup_lo8_ldi:278adjust::ldi::lo8(Size, Fixup, Value, Ctx);279break;280case AVR::fixup_lo8_ldi_pm:281case AVR::fixup_lo8_ldi_gs:282adjust::pm(Value);283adjust::ldi::lo8(Size, Fixup, Value, Ctx);284break;285case AVR::fixup_hi8_ldi:286adjust::ldi::hi8(Size, Fixup, Value, Ctx);287break;288case AVR::fixup_hi8_ldi_pm:289case AVR::fixup_hi8_ldi_gs:290adjust::pm(Value);291adjust::ldi::hi8(Size, Fixup, Value, Ctx);292break;293case AVR::fixup_hh8_ldi:294case AVR::fixup_hh8_ldi_pm:295if (Kind == AVR::fixup_hh8_ldi_pm)296adjust::pm(Value);297298adjust::ldi::hh8(Size, Fixup, Value, Ctx);299break;300case AVR::fixup_ms8_ldi:301adjust::ldi::ms8(Size, Fixup, Value, Ctx);302break;303304case AVR::fixup_lo8_ldi_neg:305case AVR::fixup_lo8_ldi_pm_neg:306if (Kind == AVR::fixup_lo8_ldi_pm_neg)307adjust::pm(Value);308309adjust::ldi::neg(Value);310adjust::ldi::lo8(Size, Fixup, Value, Ctx);311break;312case AVR::fixup_hi8_ldi_neg:313case AVR::fixup_hi8_ldi_pm_neg:314if (Kind == AVR::fixup_hi8_ldi_pm_neg)315adjust::pm(Value);316317adjust::ldi::neg(Value);318adjust::ldi::hi8(Size, Fixup, Value, Ctx);319break;320case AVR::fixup_hh8_ldi_neg:321case AVR::fixup_hh8_ldi_pm_neg:322if (Kind == AVR::fixup_hh8_ldi_pm_neg)323adjust::pm(Value);324325adjust::ldi::neg(Value);326adjust::ldi::hh8(Size, Fixup, Value, Ctx);327break;328case AVR::fixup_ms8_ldi_neg:329adjust::ldi::neg(Value);330adjust::ldi::ms8(Size, Fixup, Value, Ctx);331break;332case AVR::fixup_16:333adjust::unsigned_width(16, Value, std::string("port number"), Fixup, Ctx);334335Value &= 0xffff;336break;337case AVR::fixup_16_pm:338Value >>= 1; // Flash addresses are always shifted.339adjust::unsigned_width(16, Value, std::string("port number"), Fixup, Ctx);340341Value &= 0xffff;342break;343344case AVR::fixup_6:345adjust::fixup_6(Fixup, Value, Ctx);346break;347case AVR::fixup_6_adiw:348adjust::fixup_6_adiw(Fixup, Value, Ctx);349break;350351case AVR::fixup_port5:352adjust::fixup_port5(Fixup, Value, Ctx);353break;354355case AVR::fixup_port6:356adjust::fixup_port6(Fixup, Value, Ctx);357break;358359case AVR::fixup_lds_sts_16:360adjust::fixup_lds_sts_16(Fixup, Value, Ctx);361break;362363// Fixups which do not require adjustments.364case FK_Data_1:365case FK_Data_2:366case FK_Data_4:367case FK_Data_8:368break;369370case FK_GPRel_4:371llvm_unreachable("don't know how to adjust this fixup");372break;373}374}375376std::unique_ptr<MCObjectTargetWriter>377AVRAsmBackend::createObjectTargetWriter() const {378return createAVRELFObjectWriter(MCELFObjectTargetWriter::getOSABI(OSType));379}380381void AVRAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,382const MCValue &Target,383MutableArrayRef<char> Data, uint64_t Value,384bool IsResolved,385const MCSubtargetInfo *STI) const {386if (Fixup.getKind() >= FirstLiteralRelocationKind)387return;388adjustFixupValue(Fixup, Target, Value, &Asm.getContext());389if (Value == 0)390return; // Doesn't change encoding.391392MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());393394// The number of bits in the fixup mask395auto NumBits = Info.TargetSize + Info.TargetOffset;396auto NumBytes = (NumBits / 8) + ((NumBits % 8) == 0 ? 0 : 1);397398// Shift the value into position.399Value <<= Info.TargetOffset;400401unsigned Offset = Fixup.getOffset();402assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");403404// For each byte of the fragment that the fixup touches, mask in the405// bits from the fixup value.406for (unsigned i = 0; i < NumBytes; ++i) {407uint8_t mask = (((Value >> (i * 8)) & 0xff));408Data[Offset + i] |= mask;409}410}411412std::optional<MCFixupKind> AVRAsmBackend::getFixupKind(StringRef Name) const {413unsigned Type;414Type = llvm::StringSwitch<unsigned>(Name)415#define ELF_RELOC(X, Y) .Case(#X, Y)416#include "llvm/BinaryFormat/ELFRelocs/AVR.def"417#undef ELF_RELOC418.Case("BFD_RELOC_NONE", ELF::R_AVR_NONE)419.Case("BFD_RELOC_16", ELF::R_AVR_16)420.Case("BFD_RELOC_32", ELF::R_AVR_32)421.Default(-1u);422if (Type != -1u)423return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);424return std::nullopt;425}426427MCFixupKindInfo const &AVRAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {428// NOTE: Many AVR fixups work on sets of non-contignous bits. We work around429// this by saying that the fixup is the size of the entire instruction.430const static MCFixupKindInfo Infos[AVR::NumTargetFixupKinds] = {431// This table *must* be in same the order of fixup_* kinds in432// AVRFixupKinds.h.433//434// name offset bits flags435{"fixup_32", 0, 32, 0},436437{"fixup_7_pcrel", 3, 7, MCFixupKindInfo::FKF_IsPCRel},438{"fixup_13_pcrel", 0, 12, MCFixupKindInfo::FKF_IsPCRel},439440{"fixup_16", 0, 16, 0},441{"fixup_16_pm", 0, 16, 0},442443{"fixup_ldi", 0, 8, 0},444445{"fixup_lo8_ldi", 0, 8, 0},446{"fixup_hi8_ldi", 0, 8, 0},447{"fixup_hh8_ldi", 0, 8, 0},448{"fixup_ms8_ldi", 0, 8, 0},449450{"fixup_lo8_ldi_neg", 0, 8, 0},451{"fixup_hi8_ldi_neg", 0, 8, 0},452{"fixup_hh8_ldi_neg", 0, 8, 0},453{"fixup_ms8_ldi_neg", 0, 8, 0},454455{"fixup_lo8_ldi_pm", 0, 8, 0},456{"fixup_hi8_ldi_pm", 0, 8, 0},457{"fixup_hh8_ldi_pm", 0, 8, 0},458459{"fixup_lo8_ldi_pm_neg", 0, 8, 0},460{"fixup_hi8_ldi_pm_neg", 0, 8, 0},461{"fixup_hh8_ldi_pm_neg", 0, 8, 0},462463{"fixup_call", 0, 22, 0},464465{"fixup_6", 0, 16, 0}, // non-contiguous466{"fixup_6_adiw", 0, 6, 0},467468{"fixup_lo8_ldi_gs", 0, 8, 0},469{"fixup_hi8_ldi_gs", 0, 8, 0},470471{"fixup_8", 0, 8, 0},472{"fixup_8_lo8", 0, 8, 0},473{"fixup_8_hi8", 0, 8, 0},474{"fixup_8_hlo8", 0, 8, 0},475476{"fixup_diff8", 0, 8, 0},477{"fixup_diff16", 0, 16, 0},478{"fixup_diff32", 0, 32, 0},479480{"fixup_lds_sts_16", 0, 16, 0},481482{"fixup_port6", 0, 16, 0}, // non-contiguous483{"fixup_port5", 3, 5, 0},484};485486// Fixup kinds from .reloc directive are like R_AVR_NONE. They do not require487// any extra processing.488if (Kind >= FirstLiteralRelocationKind)489return MCAsmBackend::getFixupKindInfo(FK_NONE);490491if (Kind < FirstTargetFixupKind)492return MCAsmBackend::getFixupKindInfo(Kind);493494assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&495"Invalid kind!");496497return Infos[Kind - FirstTargetFixupKind];498}499500bool AVRAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,501const MCSubtargetInfo *STI) const {502// If the count is not 2-byte aligned, we must be writing data into the text503// section (otherwise we have unaligned instructions, and thus have far504// bigger problems), so just write zeros instead.505assert((Count % 2) == 0 && "NOP instructions must be 2 bytes");506507OS.write_zeros(Count);508return true;509}510511bool AVRAsmBackend::shouldForceRelocation(const MCAssembler &Asm,512const MCFixup &Fixup,513const MCValue &Target,514const MCSubtargetInfo *STI) {515switch ((unsigned)Fixup.getKind()) {516default:517return Fixup.getKind() >= FirstLiteralRelocationKind;518case AVR::fixup_7_pcrel:519case AVR::fixup_13_pcrel:520// Always resolve relocations for PC-relative branches521return false;522case AVR::fixup_call:523return true;524}525}526527MCAsmBackend *createAVRAsmBackend(const Target &T, const MCSubtargetInfo &STI,528const MCRegisterInfo &MRI,529const llvm::MCTargetOptions &TO) {530return new AVRAsmBackend(STI.getTargetTriple().getOS());531}532533} // end of namespace llvm534535536