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
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/MCDirectives.h"
16 #include "llvm/MC/MCELFObjectWriter.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCFixupKindInfo.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/MC/MCSymbol.h"
22 #include "llvm/MC/MCTargetOptions.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 namespace {
29 class MSP430AsmBackend : public MCAsmBackend {
30 uint8_t OSABI;
31
32 uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
33 MCContext &Ctx) const;
34
35 public:
MSP430AsmBackend(const MCSubtargetInfo & STI,uint8_t OSABI)36 MSP430AsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI)
37 : MCAsmBackend(support::little), OSABI(OSABI) {}
~MSP430AsmBackend()38 ~MSP430AsmBackend() override {}
39
40 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
41 const MCValue &Target, MutableArrayRef<char> Data,
42 uint64_t Value, bool IsResolved,
43 const MCSubtargetInfo *STI) const override;
44
45 std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const46 createObjectTargetWriter() const override {
47 return createMSP430ELFObjectWriter(OSABI);
48 }
49
fixupNeedsRelaxation(const MCFixup & Fixup,uint64_t Value,const MCRelaxableFragment * DF,const MCAsmLayout & Layout) const50 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
51 const MCRelaxableFragment *DF,
52 const MCAsmLayout &Layout) const override {
53 return false;
54 }
55
fixupNeedsRelaxationAdvanced(const MCFixup & Fixup,bool Resolved,uint64_t Value,const MCRelaxableFragment * DF,const MCAsmLayout & Layout,const bool WasForced) const56 bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
57 uint64_t Value,
58 const MCRelaxableFragment *DF,
59 const MCAsmLayout &Layout,
60 const bool WasForced) const override {
61 return false;
62 }
63
getNumFixupKinds() const64 unsigned getNumFixupKinds() const override {
65 return MSP430::NumTargetFixupKinds;
66 }
67
getFixupKindInfo(MCFixupKind Kind) const68 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
69 const static MCFixupKindInfo Infos[MSP430::NumTargetFixupKinds] = {
70 // This table must be in the same order of enum in MSP430FixupKinds.h.
71 //
72 // name offset bits flags
73 {"fixup_32", 0, 32, 0},
74 {"fixup_10_pcrel", 0, 10, MCFixupKindInfo::FKF_IsPCRel},
75 {"fixup_16", 0, 16, 0},
76 {"fixup_16_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
77 {"fixup_16_byte", 0, 16, 0},
78 {"fixup_16_pcrel_byte", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
79 {"fixup_2x_pcrel", 0, 10, MCFixupKindInfo::FKF_IsPCRel},
80 {"fixup_rl_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
81 {"fixup_8", 0, 8, 0},
82 {"fixup_sym_diff", 0, 32, 0},
83 };
84 static_assert((array_lengthof(Infos)) == MSP430::NumTargetFixupKinds,
85 "Not all fixup kinds added to Infos array");
86
87 if (Kind < FirstTargetFixupKind)
88 return MCAsmBackend::getFixupKindInfo(Kind);
89
90 return Infos[Kind - FirstTargetFixupKind];
91 }
92
93 bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
94 };
95
adjustFixupValue(const MCFixup & Fixup,uint64_t Value,MCContext & Ctx) const96 uint64_t MSP430AsmBackend::adjustFixupValue(const MCFixup &Fixup,
97 uint64_t Value,
98 MCContext &Ctx) const {
99 unsigned Kind = Fixup.getKind();
100 switch (Kind) {
101 case MSP430::fixup_10_pcrel: {
102 if (Value & 0x1)
103 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
104
105 // Offset is signed
106 int16_t Offset = Value;
107 // Jumps are in words
108 Offset >>= 1;
109 // PC points to the next instruction so decrement by one
110 --Offset;
111
112 if (Offset < -512 || Offset > 511)
113 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
114
115 // Mask 10 bits
116 Offset &= 0x3ff;
117
118 return Offset;
119 }
120 default:
121 return Value;
122 }
123 }
124
applyFixup(const MCAssembler & Asm,const MCFixup & Fixup,const MCValue & Target,MutableArrayRef<char> Data,uint64_t Value,bool IsResolved,const MCSubtargetInfo * STI) const125 void MSP430AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
126 const MCValue &Target,
127 MutableArrayRef<char> Data,
128 uint64_t Value, bool IsResolved,
129 const MCSubtargetInfo *STI) const {
130 Value = adjustFixupValue(Fixup, Value, Asm.getContext());
131 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
132 if (!Value)
133 return; // Doesn't change encoding.
134
135 // Shift the value into position.
136 Value <<= Info.TargetOffset;
137
138 unsigned Offset = Fixup.getOffset();
139 unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
140
141 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
142
143 // For each byte of the fragment that the fixup touches, mask in the
144 // bits from the fixup value.
145 for (unsigned i = 0; i != NumBytes; ++i) {
146 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
147 }
148 }
149
writeNopData(raw_ostream & OS,uint64_t Count) const150 bool MSP430AsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
151 if ((Count % 2) != 0)
152 return false;
153
154 // The canonical nop on MSP430 is mov #0, r3
155 uint64_t NopCount = Count / 2;
156 while (NopCount--)
157 OS.write("\x03\x43", 2);
158
159 return true;
160 }
161
162 } // end anonymous namespace
163
createMSP430MCAsmBackend(const Target & T,const MCSubtargetInfo & STI,const MCRegisterInfo & MRI,const MCTargetOptions & Options)164 MCAsmBackend *llvm::createMSP430MCAsmBackend(const Target &T,
165 const MCSubtargetInfo &STI,
166 const MCRegisterInfo &MRI,
167 const MCTargetOptions &Options) {
168 return new MSP430AsmBackend(STI, ELF::ELFOSABI_STANDALONE);
169 }
170