150 lines · cpp
1//===-- MSP430AsmBackend.cpp - MSP430 Assembler Backend -------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "MCTargetDesc/MSP430FixupKinds.h"10#include "MCTargetDesc/MSP430MCTargetDesc.h"11#include "llvm/ADT/APInt.h"12#include "llvm/MC/MCAsmBackend.h"13#include "llvm/MC/MCAssembler.h"14#include "llvm/MC/MCContext.h"15#include "llvm/MC/MCELFObjectWriter.h"16#include "llvm/MC/MCExpr.h"17#include "llvm/MC/MCObjectWriter.h"18#include "llvm/MC/MCSubtargetInfo.h"19#include "llvm/MC/MCSymbol.h"20#include "llvm/MC/MCTargetOptions.h"21#include "llvm/Support/ErrorHandling.h"22#include "llvm/Support/raw_ostream.h"23 24using namespace llvm;25 26namespace {27class MSP430AsmBackend : public MCAsmBackend {28 uint8_t OSABI;29 30 uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,31 MCContext &Ctx) const;32 33public:34 MSP430AsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI)35 : MCAsmBackend(llvm::endianness::little), OSABI(OSABI) {}36 ~MSP430AsmBackend() override = default;37 38 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,39 uint8_t *Data, uint64_t Value, bool IsResolved) override;40 41 std::unique_ptr<MCObjectTargetWriter>42 createObjectTargetWriter() const override {43 return createMSP430ELFObjectWriter(OSABI);44 }45 46 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override {47 // clang-format off48 const static MCFixupKindInfo Infos[MSP430::NumTargetFixupKinds] = {49 // This table must be in the same order of enum in MSP430FixupKinds.h.50 //51 // name offset bits flags52 {"fixup_32", 0, 32, 0},53 {"fixup_10_pcrel", 0, 10, 0},54 {"fixup_16", 0, 16, 0},55 {"fixup_16_pcrel", 0, 16, 0},56 {"fixup_16_byte", 0, 16, 0},57 {"fixup_16_pcrel_byte", 0, 16, 0},58 {"fixup_2x_pcrel", 0, 10, 0},59 {"fixup_rl_pcrel", 0, 16, 0},60 {"fixup_8", 0, 8, 0},61 {"fixup_sym_diff", 0, 32, 0},62 };63 // clang-format on64 static_assert((std::size(Infos)) == MSP430::NumTargetFixupKinds,65 "Not all fixup kinds added to Infos array");66 67 if (Kind < FirstTargetFixupKind)68 return MCAsmBackend::getFixupKindInfo(Kind);69 70 return Infos[Kind - FirstTargetFixupKind];71 }72 73 bool writeNopData(raw_ostream &OS, uint64_t Count,74 const MCSubtargetInfo *STI) const override;75};76 77uint64_t MSP430AsmBackend::adjustFixupValue(const MCFixup &Fixup,78 uint64_t Value,79 MCContext &Ctx) const {80 unsigned Kind = Fixup.getKind();81 switch (Kind) {82 case MSP430::fixup_10_pcrel: {83 if (Value & 0x1)84 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");85 86 // Offset is signed87 int16_t Offset = Value;88 // Jumps are in words89 Offset >>= 1;90 // PC points to the next instruction so decrement by one91 --Offset;92 93 if (Offset < -512 || Offset > 511)94 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");95 96 // Mask 10 bits97 Offset &= 0x3ff;98 99 return Offset;100 }101 default:102 return Value;103 }104}105 106void MSP430AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,107 const MCValue &Target, uint8_t *Data,108 uint64_t Value, bool IsResolved) {109 maybeAddReloc(F, Fixup, Target, Value, IsResolved);110 Value = adjustFixupValue(Fixup, Value, getContext());111 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());112 if (!Value)113 return; // Doesn't change encoding.114 115 // Shift the value into position.116 Value <<= Info.TargetOffset;117 118 unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;119 assert(Fixup.getOffset() + NumBytes <= F.getSize() &&120 "Invalid fixup offset!");121 122 // For each byte of the fragment that the fixup touches, mask in the123 // bits from the fixup value.124 for (unsigned i = 0; i != NumBytes; ++i) {125 Data[i] |= uint8_t((Value >> (i * 8)) & 0xff);126 }127}128 129bool MSP430AsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,130 const MCSubtargetInfo *STI) const {131 if ((Count % 2) != 0)132 return false;133 134 // The canonical nop on MSP430 is mov #0, r3135 uint64_t NopCount = Count / 2;136 while (NopCount--)137 OS.write("\x03\x43", 2);138 139 return true;140}141 142} // end anonymous namespace143 144MCAsmBackend *llvm::createMSP430MCAsmBackend(const Target &T,145 const MCSubtargetInfo &STI,146 const MCRegisterInfo &MRI,147 const MCTargetOptions &Options) {148 return new MSP430AsmBackend(STI, ELF::ELFOSABI_STANDALONE);149}150