LLVM 19.0.0git
MSP430AsmBackend.cpp
Go to the documentation of this file.
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-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include "llvm/ADT/APInt.h"
13#include "llvm/MC/MCAssembler.h"
14#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCSymbol.h"
25
26using namespace llvm;
27
28namespace {
29class MSP430AsmBackend : public MCAsmBackend {
30 uint8_t OSABI;
31
33 MCContext &Ctx) const;
34
35public:
36 MSP430AsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI)
37 : MCAsmBackend(llvm::endianness::little), OSABI(OSABI) {}
38 ~MSP430AsmBackend() override = default;
39
40 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
42 uint64_t Value, bool IsResolved,
43 const MCSubtargetInfo *STI) const override;
44
45 std::unique_ptr<MCObjectTargetWriter>
46 createObjectTargetWriter() const override {
47 return createMSP430ELFObjectWriter(OSABI);
48 }
49
51 const MCFixup &Fixup, bool Resolved,
54 const bool WasForced) const override {
55 return false;
56 }
57
58 unsigned getNumFixupKinds() const override {
60 }
61
62 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
63 const static MCFixupKindInfo Infos[MSP430::NumTargetFixupKinds] = {
64 // This table must be in the same order of enum in MSP430FixupKinds.h.
65 //
66 // name offset bits flags
67 {"fixup_32", 0, 32, 0},
68 {"fixup_10_pcrel", 0, 10, MCFixupKindInfo::FKF_IsPCRel},
69 {"fixup_16", 0, 16, 0},
70 {"fixup_16_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
71 {"fixup_16_byte", 0, 16, 0},
72 {"fixup_16_pcrel_byte", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
73 {"fixup_2x_pcrel", 0, 10, MCFixupKindInfo::FKF_IsPCRel},
74 {"fixup_rl_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
75 {"fixup_8", 0, 8, 0},
76 {"fixup_sym_diff", 0, 32, 0},
77 };
78 static_assert((std::size(Infos)) == MSP430::NumTargetFixupKinds,
79 "Not all fixup kinds added to Infos array");
80
81 if (Kind < FirstTargetFixupKind)
83
84 return Infos[Kind - FirstTargetFixupKind];
85 }
86
88 const MCSubtargetInfo *STI) const override;
89};
90
91uint64_t MSP430AsmBackend::adjustFixupValue(const MCFixup &Fixup,
93 MCContext &Ctx) const {
94 unsigned Kind = Fixup.getKind();
95 switch (Kind) {
97 if (Value & 0x1)
98 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
99
100 // Offset is signed
101 int16_t Offset = Value;
102 // Jumps are in words
103 Offset >>= 1;
104 // PC points to the next instruction so decrement by one
105 --Offset;
106
107 if (Offset < -512 || Offset > 511)
108 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
109
110 // Mask 10 bits
111 Offset &= 0x3ff;
112
113 return Offset;
114 }
115 default:
116 return Value;
117 }
118}
119
120void MSP430AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
121 const MCValue &Target,
123 uint64_t Value, bool IsResolved,
124 const MCSubtargetInfo *STI) const {
125 Value = adjustFixupValue(Fixup, Value, Asm.getContext());
126 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
127 if (!Value)
128 return; // Doesn't change encoding.
129
130 // Shift the value into position.
131 Value <<= Info.TargetOffset;
132
133 unsigned Offset = Fixup.getOffset();
134 unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
135
136 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
137
138 // For each byte of the fragment that the fixup touches, mask in the
139 // bits from the fixup value.
140 for (unsigned i = 0; i != NumBytes; ++i) {
141 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
142 }
143}
144
145bool MSP430AsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
146 const MCSubtargetInfo *STI) const {
147 if ((Count % 2) != 0)
148 return false;
149
150 // The canonical nop on MSP430 is mov #0, r3
151 uint64_t NopCount = Count / 2;
152 while (NopCount--)
153 OS.write("\x03\x43", 2);
154
155 return true;
156}
157
158} // end anonymous namespace
159
161 const MCSubtargetInfo &STI,
162 const MCRegisterInfo &MRI,
163 const MCTargetOptions &Options) {
164 return new MSP430AsmBackend(STI, ELF::ELFOSABI_STANDALONE);
165}
unsigned const MachineRegisterInfo * MRI
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
This file implements a class to represent arbitrary precision integral constant values and operations...
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
static LVOptions Options
Definition: LVOptions.cpp:25
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
virtual bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
virtual bool fixupNeedsRelaxationAdvanced(const MCAssembler &Asm, const MCFixup &Fixup, bool Resolved, uint64_t Value, const MCRelaxableFragment *DF, const bool WasForced) const
Target specific predicate for whether a given fixup requires the associated instruction to be relaxed...
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual unsigned getNumFixupKinds() const =0
Get the number of target specific fixup kinds.
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
Context object for machine code objects.
Definition: MCContext.h:83
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1069
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:261
Generic base class for all target subtargets.
This represents an "assembler immediate".
Definition: MCValue.h:36
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write(unsigned char C)
@ ELFOSABI_STANDALONE
Definition: ELF.h:369
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ FirstTargetFixupKind
Definition: MCFixup.h:45
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
MCAsmBackend * createMSP430MCAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
std::unique_ptr< MCObjectTargetWriter > createMSP430ELFObjectWriter(uint8_t OSABI)
endianness
Definition: bit.h:70
Target independent information on a fixup kind.
@ FKF_IsPCRel
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...