LLVM 24.0.0git
SparcAsmBackend.cpp
Go to the documentation of this file.
1//===-- SparcAsmBackend.cpp - Sparc 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
14#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCValue.h"
22
23using namespace llvm;
24
25static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
26 switch (Kind) {
27 default:
28 assert(uint16_t(Kind) < FirstTargetFixupKind && "Unknown fixup kind!");
29 return Value;
30 case FK_Data_1:
31 case FK_Data_2:
32 case FK_Data_4:
33 case FK_Data_8:
34 return Value;
35
37 return (Value >> 2) & 0x3fffffff;
38
39 case ELF::R_SPARC_WDISP22:
40 return (Value >> 2) & 0x3fffff;
41
42 case ELF::R_SPARC_WDISP19:
43 return (Value >> 2) & 0x7ffff;
44
45 case ELF::R_SPARC_WDISP16: {
46 // A.3 Branch on Integer Register with Prediction (BPr)
47 // Inst{21-20} = d16hi;
48 // Inst{13-0} = d16lo;
49 unsigned d16hi = (Value >> 16) & 0x3;
50 unsigned d16lo = (Value >> 2) & 0x3fff;
51 return (d16hi << 20) | d16lo;
52 }
53
54 case ELF::R_SPARC_WDISP10: {
55 // FIXME this really should be an error reporting check.
56 assert((Value & 0x3) == 0);
57
58 // 7.17 Compare and Branch
59 // Inst{20-19} = d10hi;
60 // Inst{12-5} = d10lo;
61 unsigned d10hi = (Value >> 10) & 0x3;
62 unsigned d10lo = (Value >> 2) & 0xff;
63 return (d10hi << 19) | (d10lo << 5);
64 }
65
66 case ELF::R_SPARC_HIX22:
67 return (~Value >> 10) & 0x3fffff;
68
69 // In PIC mode the parser may map %hi to PC22/GOT22. An operand that folds to
70 // an absolute value emits no relocation and needs the %hi encoding.
71 case ELF::R_SPARC_PC22:
72 case ELF::R_SPARC_HI22:
73 case ELF::R_SPARC_GOT22:
74 case ELF::R_SPARC_LM22:
75 return (Value >> 10) & 0x3fffff;
76
77 case ELF::R_SPARC_22:
78 return Value & 0x3fffff;
79
81 return Value & 0x1fff;
82
83 case ELF::R_SPARC_11:
84 return Value & 0x7ff;
85
86 case ELF::R_SPARC_10:
87 return Value & 0x3ff;
88
89 case ELF::R_SPARC_7:
90 return Value & 0x7f;
91
92 case ELF::R_SPARC_5:
93 return Value & 0x1f;
94
95 case ELF::R_SPARC_LOX10:
96 return (Value & 0x3ff) | 0x1c00;
97
98 case ELF::R_SPARC_PC10:
99 case ELF::R_SPARC_LO10:
100 case ELF::R_SPARC_GOT10:
101 return Value & 0x3ff;
102
103 case ELF::R_SPARC_H34:
104 return (Value >> 12) & 0x3fffff;
105 case ELF::R_SPARC_H44:
106 return (Value >> 22) & 0x3fffff;
107 case ELF::R_SPARC_M44:
108 return (Value >> 12) & 0x3ff;
109 case ELF::R_SPARC_L44:
110 return Value & 0xfff;
111
112 case ELF::R_SPARC_HH22:
113 return (Value >> 42) & 0x3fffff;
114 case ELF::R_SPARC_HM10:
115 return (Value >> 32) & 0x3ff;
116 }
117}
118
119/// getFixupKindNumBytes - The number of bytes the fixup may change.
120static unsigned getFixupKindNumBytes(unsigned Kind) {
121 switch (Kind) {
122 default:
123 return 4;
124 case FK_Data_1:
125 return 1;
126 case FK_Data_2:
127 return 2;
128 case FK_Data_8:
129 return 8;
130 }
131}
132
133namespace {
134class SparcAsmBackend : public MCAsmBackend {
135protected:
136 bool Is64Bit;
137 bool IsV8Plus;
138
139public:
140 SparcAsmBackend(const MCSubtargetInfo &STI)
141 : MCAsmBackend(STI.getTargetTriple().isLittleEndian()
142 ? llvm::endianness::little
143 : llvm::endianness::big),
144 Is64Bit(STI.getTargetTriple().isArch64Bit()),
145 IsV8Plus(STI.hasFeature(Sparc::FeatureV8Plus)) {}
146
147 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
148 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override;
149 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
150 uint8_t *Data, uint64_t Value, bool IsResolved) override;
151
152 bool writeNopData(raw_ostream &OS, uint64_t Count,
153 const MCSubtargetInfo *STI) const override {
154
155 // If the count is not 4-byte aligned, we must be writing data into the
156 // text section (otherwise we have unaligned instructions, and thus have
157 // far bigger problems), so just write zeros instead.
158 OS.write_zeros(Count % 4);
159
160 uint64_t NumNops = Count / 4;
161 for (uint64_t i = 0; i != NumNops; ++i)
162 support::endian::write<uint32_t>(OS, 0x01000000, Endian);
163
164 return true;
165 }
166};
167
168class ELFSparcAsmBackend : public SparcAsmBackend {
169 Triple::OSType OSType;
170
171public:
172 ELFSparcAsmBackend(const MCSubtargetInfo &STI, Triple::OSType OSType)
173 : SparcAsmBackend(STI), OSType(OSType) {}
174
175 std::unique_ptr<MCObjectTargetWriter>
176 createObjectTargetWriter() const override {
177 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
178 return createSparcELFObjectWriter(Is64Bit, IsV8Plus, OSABI);
179 }
180};
181} // end anonymous namespace
182
183std::optional<MCFixupKind> SparcAsmBackend::getFixupKind(StringRef Name) const {
184 unsigned Type;
185 Type = llvm::StringSwitch<unsigned>(Name)
186#define ELF_RELOC(X, Y) .Case(#X, Y)
187#include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
188#undef ELF_RELOC
189 .Case("BFD_RELOC_NONE", ELF::R_SPARC_NONE)
190 .Case("BFD_RELOC_8", ELF::R_SPARC_8)
191 .Case("BFD_RELOC_16", ELF::R_SPARC_16)
192 .Case("BFD_RELOC_32", ELF::R_SPARC_32)
193 .Case("BFD_RELOC_64", ELF::R_SPARC_64)
194 .Default(-1u);
195 if (Type == -1u)
196 return std::nullopt;
197 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
198}
199
200MCFixupKindInfo SparcAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
201 // clang-format off
202 const static MCFixupKindInfo InfosBE[Sparc::NumTargetFixupKinds] = {
203 // name offset bits flags
204 {"fixup_sparc_call30", 2, 30, 0},
205 {"fixup_sparc_13", 19, 13, 0},
206 };
207
208 const static MCFixupKindInfo InfosLE[Sparc::NumTargetFixupKinds] = {
209 // name offset bits flags
210 {"fixup_sparc_call30", 0, 30, 0},
211 {"fixup_sparc_13", 0, 13, 0},
212 };
213 // clang-format on
214
215 if (!mc::isRelocation(Kind)) {
216 if (Kind < FirstTargetFixupKind)
219 "Invalid kind!");
220 if (Endian == llvm::endianness::little)
221 return InfosLE[Kind - FirstTargetFixupKind];
222
223 return InfosBE[Kind - FirstTargetFixupKind];
224 }
225
226 MCFixupKindInfo Info{};
227 switch (uint16_t(Kind)) {
228 case ELF::R_SPARC_PC10:
229 Info = {"", 22, 10, 0};
230 break;
231 case ELF::R_SPARC_7:
232 Info = {"", 25, 7, 0};
233 break;
234 case ELF::R_SPARC_10:
235 Info = {"", 22, 10, 0};
236 break;
237 case ELF::R_SPARC_11:
238 Info = {"", 21, 11, 0};
239 break;
240 case ELF::R_SPARC_22:
241 Info = {"", 10, 22, 0};
242 break;
243 case ELF::R_SPARC_PC22:
244 Info = {"", 10, 22, 0};
245 break;
246 case ELF::R_SPARC_WDISP10:
247 Info = {"", 0, 32, 0};
248 break;
249 case ELF::R_SPARC_WDISP16:
250 Info = {"", 0, 32, 0};
251 break;
252 case ELF::R_SPARC_WDISP19:
253 Info = {"", 13, 19, 0};
254 break;
255 case ELF::R_SPARC_WDISP22:
256 Info = {"", 10, 22, 0};
257 break;
258
259 case ELF::R_SPARC_HI22:
260 Info = {"", 10, 22, 0};
261 break;
262 case ELF::R_SPARC_LO10:
263 Info = {"", 22, 10, 0};
264 break;
265 case ELF::R_SPARC_HH22:
266 Info = {"", 10, 22, 0};
267 break;
268 case ELF::R_SPARC_HM10:
269 Info = {"", 22, 10, 0};
270 break;
271 case ELF::R_SPARC_LM22:
272 Info = {"", 10, 22, 0};
273 break;
274 case ELF::R_SPARC_HIX22:
275 Info = {"", 10, 22, 0};
276 break;
277 case ELF::R_SPARC_LOX10:
278 Info = {"", 19, 13, 0};
279 break;
280 }
281 if (Endian == llvm::endianness::little)
282 Info.TargetOffset = 32 - Info.TargetOffset - Info.TargetSize;
283 return Info;
284}
285
286void SparcAsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
287 const MCValue &Target, uint8_t *Data,
288 uint64_t Value, bool IsResolved) {
289 maybeAddReloc(F, Fixup, Target, Value, IsResolved);
290 if (!IsResolved)
291 return;
292
293 // A data relocation specifier asks for a relocation. Like GNU as, reject
294 // one whose operand folds to a constant instead of writing the constant.
295 switch (Target.getSpecifier()) {
296 case ELF::R_SPARC_DISP8:
297 case ELF::R_SPARC_DISP16:
298 case ELF::R_SPARC_DISP32:
299 case ELF::R_SPARC_DISP64:
300 case ELF::R_SPARC_PLT32:
301 case ELF::R_SPARC_PLT64:
302 case ELF::R_SPARC_TLS_DTPOFF32:
303 case ELF::R_SPARC_TLS_DTPOFF64:
304 getContext().reportError(
305 Fixup.getLoc(), "%" + Sparc::getSpecifierName(Target.getSpecifier()) +
306 " cannot be applied to a constant");
307 return;
308 default:
309 break;
310 }
311
312 Value = adjustFixupValue(Fixup.getKind(), Value);
313
314 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
315 // For each byte of the fragment that the fixup touches, mask in the
316 // bits from the fixup value.
317 for (unsigned i = 0; i != NumBytes; ++i) {
318 unsigned Idx = Endian == llvm::endianness::little ? i : (NumBytes - 1) - i;
319 Data[Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
320 }
321}
322
324 const MCSubtargetInfo &STI,
325 const MCRegisterInfo &MRI,
326 const MCTargetOptions &Options) {
327 return new ELFSparcAsmBackend(STI, STI.getTargetTriple().getOS());
328}
static unsigned getFixupKindNumBytes(unsigned Kind)
The number of bytes the fixup may change.
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
static LVOptions Options
Definition LVOptions.cpp:25
static bool hasFeature(StringRef Feature, const FeatureBitset &FeatureBits, ArrayRef< SubtargetFeatureKV > ProcFeatures)
#define F(x, y, z)
Definition MD5.cpp:54
#define T
MachineRegisterInfo & MRI
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
static unsigned adjustFixupValue(unsigned Kind, uint64_t Value)
static unsigned getFixupKindNumBytes(unsigned Kind)
getFixupKindNumBytes - The number of bytes the fixup may change.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
Generic interface to target specific assembler backends.
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Target - Wrapper for Target specific information.
OSType getOS() const
Get the parsed operating system type of this triple.
Definition Triple.h:527
LLVM Value Representation.
Definition Value.h:75
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
@ fixup_sparc_13
fixup_sparc_13 - 13-bit fixup
StringRef getSpecifierName(uint16_t S)
VE::Fixups getFixupKind(uint8_t S)
bool isRelocation(MCFixupKind FixupKind)
Definition MCFixup.h:134
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition Endian.h:82
This is an optimization pass for GlobalISel generic memory operations.
constexpr MCFixupKind FirstTargetFixupKind
Definition MCFixup.h:49
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
std::unique_ptr< MCObjectTargetWriter > createSparcELFObjectWriter(bool Is64Bit, bool IsV8Plus, uint8_t OSABI)
@ FK_Data_8
A eight-byte fixup.
Definition MCFixup.h:41
@ FK_Data_1
A one-byte fixup.
Definition MCFixup.h:38
@ FK_Data_4
A four-byte fixup.
Definition MCFixup.h:40
@ FK_Data_2
A two-byte fixup.
Definition MCFixup.h:39
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
MCAsmBackend * createSparcAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
endianness
Definition bit.h:71
constexpr MCFixupKind FirstLiteralRelocationKind
Definition MCFixup.h:32