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: |
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, |
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> |
46 | createObjectTargetWriter() const override { |
47 | return createMSP430ELFObjectWriter(OSABI); |
48 | } |
49 | |
50 | bool fixupNeedsRelaxationAdvanced(const MCAssembler &Asm, |
51 | const MCFixup &Fixup, bool Resolved, |
52 | uint64_t Value, |
53 | const MCRelaxableFragment *DF, |
54 | const bool WasForced) const override { |
55 | return false; |
56 | } |
57 | |
58 | unsigned getNumFixupKinds() const override { |
59 | return MSP430::NumTargetFixupKinds; |
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 | {.Name: "fixup_32" , .TargetOffset: 0, .TargetSize: 32, .Flags: 0}, |
68 | {.Name: "fixup_10_pcrel" , .TargetOffset: 0, .TargetSize: 10, .Flags: MCFixupKindInfo::FKF_IsPCRel}, |
69 | {.Name: "fixup_16" , .TargetOffset: 0, .TargetSize: 16, .Flags: 0}, |
70 | {.Name: "fixup_16_pcrel" , .TargetOffset: 0, .TargetSize: 16, .Flags: MCFixupKindInfo::FKF_IsPCRel}, |
71 | {.Name: "fixup_16_byte" , .TargetOffset: 0, .TargetSize: 16, .Flags: 0}, |
72 | {.Name: "fixup_16_pcrel_byte" , .TargetOffset: 0, .TargetSize: 16, .Flags: MCFixupKindInfo::FKF_IsPCRel}, |
73 | {.Name: "fixup_2x_pcrel" , .TargetOffset: 0, .TargetSize: 10, .Flags: MCFixupKindInfo::FKF_IsPCRel}, |
74 | {.Name: "fixup_rl_pcrel" , .TargetOffset: 0, .TargetSize: 16, .Flags: MCFixupKindInfo::FKF_IsPCRel}, |
75 | {.Name: "fixup_8" , .TargetOffset: 0, .TargetSize: 8, .Flags: 0}, |
76 | {.Name: "fixup_sym_diff" , .TargetOffset: 0, .TargetSize: 32, .Flags: 0}, |
77 | }; |
78 | static_assert((std::size(Infos)) == MSP430::NumTargetFixupKinds, |
79 | "Not all fixup kinds added to Infos array" ); |
80 | |
81 | if (Kind < FirstTargetFixupKind) |
82 | return MCAsmBackend::getFixupKindInfo(Kind); |
83 | |
84 | return Infos[Kind - FirstTargetFixupKind]; |
85 | } |
86 | |
87 | bool writeNopData(raw_ostream &OS, uint64_t Count, |
88 | const MCSubtargetInfo *STI) const override; |
89 | }; |
90 | |
91 | uint64_t MSP430AsmBackend::adjustFixupValue(const MCFixup &Fixup, |
92 | uint64_t Value, |
93 | MCContext &Ctx) const { |
94 | unsigned Kind = Fixup.getKind(); |
95 | switch (Kind) { |
96 | case MSP430::fixup_10_pcrel: { |
97 | if (Value & 0x1) |
98 | Ctx.reportError(L: Fixup.getLoc(), Msg: "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(L: Fixup.getLoc(), Msg: "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 | |
120 | void MSP430AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, |
121 | const MCValue &Target, |
122 | MutableArrayRef<char> Data, |
123 | uint64_t Value, bool IsResolved, |
124 | const MCSubtargetInfo *STI) const { |
125 | Value = adjustFixupValue(Fixup, Value, Ctx&: Asm.getContext()); |
126 | MCFixupKindInfo Info = getFixupKindInfo(Kind: 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(Value: Info.TargetSize + Info.TargetOffset, Align: 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 | |
145 | bool 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(Ptr: "\x03\x43" , Size: 2); |
154 | |
155 | return true; |
156 | } |
157 | |
158 | } // end anonymous namespace |
159 | |
160 | MCAsmBackend *llvm::createMSP430MCAsmBackend(const Target &T, |
161 | const MCSubtargetInfo &STI, |
162 | const MCRegisterInfo &MRI, |
163 | const MCTargetOptions &Options) { |
164 | return new MSP430AsmBackend(STI, ELF::ELFOSABI_STANDALONE); |
165 | } |
166 | |