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