xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Sparc/MCTargetDesc/SparcAsmBackend.cpp (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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 
9 #include "MCTargetDesc/SparcFixupKinds.h"
10 #include "MCTargetDesc/SparcMCTargetDesc.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCFixupKindInfo.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/MC/TargetRegistry.h"
20 #include "llvm/Support/EndianStream.h"
21 
22 using namespace llvm;
23 
24 static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
25   switch (Kind) {
26   default:
27     llvm_unreachable("Unknown fixup kind!");
28   case FK_Data_1:
29   case FK_Data_2:
30   case FK_Data_4:
31   case FK_Data_8:
32     return Value;
33 
34   case Sparc::fixup_sparc_wplt30:
35   case Sparc::fixup_sparc_call30:
36     return (Value >> 2) & 0x3fffffff;
37 
38   case Sparc::fixup_sparc_br22:
39     return (Value >> 2) & 0x3fffff;
40 
41   case Sparc::fixup_sparc_br19:
42     return (Value >> 2) & 0x7ffff;
43 
44   case Sparc::fixup_sparc_br16: {
45     // A.3 Branch on Integer Register with Prediction (BPr)
46     // Inst{21-20} = d16hi;
47     // Inst{13-0}  = d16lo;
48     unsigned d16hi = (Value >> 16) & 0x3;
49     unsigned d16lo = (Value >> 2) & 0x3fff;
50     return (d16hi << 20) | d16lo;
51   }
52 
53   case Sparc::fixup_sparc_hix22:
54     return (~Value >> 10) & 0x3fffff;
55 
56   case Sparc::fixup_sparc_pc22:
57   case Sparc::fixup_sparc_got22:
58   case Sparc::fixup_sparc_tls_gd_hi22:
59   case Sparc::fixup_sparc_tls_ldm_hi22:
60   case Sparc::fixup_sparc_tls_ie_hi22:
61   case Sparc::fixup_sparc_hi22:
62   case Sparc::fixup_sparc_lm:
63     return (Value >> 10) & 0x3fffff;
64 
65   case Sparc::fixup_sparc_got13:
66   case Sparc::fixup_sparc_13:
67     return Value & 0x1fff;
68 
69   case Sparc::fixup_sparc_lox10:
70     return (Value & 0x3ff) | 0x1c00;
71 
72   case Sparc::fixup_sparc_pc10:
73   case Sparc::fixup_sparc_got10:
74   case Sparc::fixup_sparc_tls_gd_lo10:
75   case Sparc::fixup_sparc_tls_ldm_lo10:
76   case Sparc::fixup_sparc_tls_ie_lo10:
77   case Sparc::fixup_sparc_lo10:
78     return Value & 0x3ff;
79 
80   case Sparc::fixup_sparc_h44:
81     return (Value >> 22) & 0x3fffff;
82 
83   case Sparc::fixup_sparc_m44:
84     return (Value >> 12) & 0x3ff;
85 
86   case Sparc::fixup_sparc_l44:
87     return Value & 0xfff;
88 
89   case Sparc::fixup_sparc_hh:
90     return (Value >> 42) & 0x3fffff;
91 
92   case Sparc::fixup_sparc_hm:
93     return (Value >> 32) & 0x3ff;
94 
95   case Sparc::fixup_sparc_tls_ldo_hix22:
96   case Sparc::fixup_sparc_tls_le_hix22:
97   case Sparc::fixup_sparc_tls_ldo_lox10:
98   case Sparc::fixup_sparc_tls_le_lox10:
99     assert(Value == 0 && "Sparc TLS relocs expect zero Value");
100     return 0;
101 
102   case Sparc::fixup_sparc_tls_gd_add:
103   case Sparc::fixup_sparc_tls_gd_call:
104   case Sparc::fixup_sparc_tls_ldm_add:
105   case Sparc::fixup_sparc_tls_ldm_call:
106   case Sparc::fixup_sparc_tls_ldo_add:
107   case Sparc::fixup_sparc_tls_ie_ld:
108   case Sparc::fixup_sparc_tls_ie_ldx:
109   case Sparc::fixup_sparc_tls_ie_add:
110   case Sparc::fixup_sparc_gotdata_lox10:
111   case Sparc::fixup_sparc_gotdata_hix22:
112   case Sparc::fixup_sparc_gotdata_op:
113     return 0;
114   }
115 }
116 
117 /// getFixupKindNumBytes - The number of bytes the fixup may change.
118 static unsigned getFixupKindNumBytes(unsigned Kind) {
119     switch (Kind) {
120   default:
121     return 4;
122   case FK_Data_1:
123     return 1;
124   case FK_Data_2:
125     return 2;
126   case FK_Data_8:
127     return 8;
128   }
129 }
130 
131 namespace {
132   class SparcAsmBackend : public MCAsmBackend {
133   protected:
134     const Target &TheTarget;
135     bool Is64Bit;
136 
137   public:
138     SparcAsmBackend(const Target &T)
139         : MCAsmBackend(StringRef(T.getName()) == "sparcel" ? support::little
140                                                            : support::big),
141           TheTarget(T), Is64Bit(StringRef(TheTarget.getName()) == "sparcv9") {}
142 
143     unsigned getNumFixupKinds() const override {
144       return Sparc::NumTargetFixupKinds;
145     }
146 
147     std::optional<MCFixupKind> getFixupKind(StringRef Name) const override {
148       unsigned Type;
149       Type = llvm::StringSwitch<unsigned>(Name)
150 #define ELF_RELOC(X, Y) .Case(#X, Y)
151 #include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
152 #undef ELF_RELOC
153                  .Case("BFD_RELOC_NONE", ELF::R_SPARC_NONE)
154                  .Case("BFD_RELOC_8", ELF::R_SPARC_8)
155                  .Case("BFD_RELOC_16", ELF::R_SPARC_16)
156                  .Case("BFD_RELOC_32", ELF::R_SPARC_32)
157                  .Case("BFD_RELOC_64", ELF::R_SPARC_64)
158                  .Default(-1u);
159       if (Type == -1u)
160         return std::nullopt;
161       return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
162     }
163 
164     const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
165       const static MCFixupKindInfo InfosBE[Sparc::NumTargetFixupKinds] = {
166         // name                    offset bits  flags
167         { "fixup_sparc_call30",     2,     30,  MCFixupKindInfo::FKF_IsPCRel },
168         { "fixup_sparc_br22",      10,     22,  MCFixupKindInfo::FKF_IsPCRel },
169         { "fixup_sparc_br19",      13,     19,  MCFixupKindInfo::FKF_IsPCRel },
170         { "fixup_sparc_br16",       0,     32,  MCFixupKindInfo::FKF_IsPCRel },
171         { "fixup_sparc_13",        19,     13,  0 },
172         { "fixup_sparc_hi22",      10,     22,  0 },
173         { "fixup_sparc_lo10",      22,     10,  0 },
174         { "fixup_sparc_h44",       10,     22,  0 },
175         { "fixup_sparc_m44",       22,     10,  0 },
176         { "fixup_sparc_l44",       20,     12,  0 },
177         { "fixup_sparc_hh",        10,     22,  0 },
178         { "fixup_sparc_hm",        22,     10,  0 },
179         { "fixup_sparc_lm",        10,     22,  0 },
180         { "fixup_sparc_pc22",      10,     22,  MCFixupKindInfo::FKF_IsPCRel },
181         { "fixup_sparc_pc10",      22,     10,  MCFixupKindInfo::FKF_IsPCRel },
182         { "fixup_sparc_got22",     10,     22,  0 },
183         { "fixup_sparc_got10",     22,     10,  0 },
184         { "fixup_sparc_got13",     19,     13,  0 },
185         { "fixup_sparc_wplt30",     2,     30,  MCFixupKindInfo::FKF_IsPCRel },
186         { "fixup_sparc_tls_gd_hi22",   10, 22,  0 },
187         { "fixup_sparc_tls_gd_lo10",   22, 10,  0 },
188         { "fixup_sparc_tls_gd_add",     0,  0,  0 },
189         { "fixup_sparc_tls_gd_call",    0,  0,  0 },
190         { "fixup_sparc_tls_ldm_hi22",  10, 22,  0 },
191         { "fixup_sparc_tls_ldm_lo10",  22, 10,  0 },
192         { "fixup_sparc_tls_ldm_add",    0,  0,  0 },
193         { "fixup_sparc_tls_ldm_call",   0,  0,  0 },
194         { "fixup_sparc_tls_ldo_hix22", 10, 22,  0 },
195         { "fixup_sparc_tls_ldo_lox10", 22, 10,  0 },
196         { "fixup_sparc_tls_ldo_add",    0,  0,  0 },
197         { "fixup_sparc_tls_ie_hi22",   10, 22,  0 },
198         { "fixup_sparc_tls_ie_lo10",   22, 10,  0 },
199         { "fixup_sparc_tls_ie_ld",      0,  0,  0 },
200         { "fixup_sparc_tls_ie_ldx",     0,  0,  0 },
201         { "fixup_sparc_tls_ie_add",     0,  0,  0 },
202         { "fixup_sparc_tls_le_hix22",   0,  0,  0 },
203         { "fixup_sparc_tls_le_lox10",   0,  0,  0 },
204         { "fixup_sparc_hix22",         10, 22,  0 },
205         { "fixup_sparc_lox10",         19, 13,  0 },
206         { "fixup_sparc_gotdata_hix22",  0,  0,  0 },
207         { "fixup_sparc_gotdata_lox10",  0,  0,  0 },
208         { "fixup_sparc_gotdata_op",     0,  0,  0 },
209       };
210 
211       const static MCFixupKindInfo InfosLE[Sparc::NumTargetFixupKinds] = {
212         // name                    offset bits  flags
213         { "fixup_sparc_call30",     0,     30,  MCFixupKindInfo::FKF_IsPCRel },
214         { "fixup_sparc_br22",       0,     22,  MCFixupKindInfo::FKF_IsPCRel },
215         { "fixup_sparc_br19",       0,     19,  MCFixupKindInfo::FKF_IsPCRel },
216         { "fixup_sparc_br16",      32,      0,  MCFixupKindInfo::FKF_IsPCRel },
217         { "fixup_sparc_13",         0,     13,  0 },
218         { "fixup_sparc_hi22",       0,     22,  0 },
219         { "fixup_sparc_lo10",       0,     10,  0 },
220         { "fixup_sparc_h44",        0,     22,  0 },
221         { "fixup_sparc_m44",        0,     10,  0 },
222         { "fixup_sparc_l44",        0,     12,  0 },
223         { "fixup_sparc_hh",         0,     22,  0 },
224         { "fixup_sparc_hm",         0,     10,  0 },
225         { "fixup_sparc_lm",         0,     22,  0 },
226         { "fixup_sparc_pc22",       0,     22,  MCFixupKindInfo::FKF_IsPCRel },
227         { "fixup_sparc_pc10",       0,     10,  MCFixupKindInfo::FKF_IsPCRel },
228         { "fixup_sparc_got22",      0,     22,  0 },
229         { "fixup_sparc_got10",      0,     10,  0 },
230         { "fixup_sparc_got13",      0,     13,  0 },
231         { "fixup_sparc_wplt30",      0,     30,  MCFixupKindInfo::FKF_IsPCRel },
232         { "fixup_sparc_tls_gd_hi22",    0, 22,  0 },
233         { "fixup_sparc_tls_gd_lo10",    0, 10,  0 },
234         { "fixup_sparc_tls_gd_add",     0,  0,  0 },
235         { "fixup_sparc_tls_gd_call",    0,  0,  0 },
236         { "fixup_sparc_tls_ldm_hi22",   0, 22,  0 },
237         { "fixup_sparc_tls_ldm_lo10",   0, 10,  0 },
238         { "fixup_sparc_tls_ldm_add",    0,  0,  0 },
239         { "fixup_sparc_tls_ldm_call",   0,  0,  0 },
240         { "fixup_sparc_tls_ldo_hix22",  0, 22,  0 },
241         { "fixup_sparc_tls_ldo_lox10",  0, 10,  0 },
242         { "fixup_sparc_tls_ldo_add",    0,  0,  0 },
243         { "fixup_sparc_tls_ie_hi22",    0, 22,  0 },
244         { "fixup_sparc_tls_ie_lo10",    0, 10,  0 },
245         { "fixup_sparc_tls_ie_ld",      0,  0,  0 },
246         { "fixup_sparc_tls_ie_ldx",     0,  0,  0 },
247         { "fixup_sparc_tls_ie_add",     0,  0,  0 },
248         { "fixup_sparc_tls_le_hix22",   0,  0,  0 },
249         { "fixup_sparc_tls_le_lox10",   0,  0,  0 },
250         { "fixup_sparc_hix22",          0, 22,  0 },
251         { "fixup_sparc_lox10",          0, 13,  0 },
252         { "fixup_sparc_gotdata_hix22",  0,  0,  0 },
253         { "fixup_sparc_gotdata_lox10",  0,  0,  0 },
254         { "fixup_sparc_gotdata_op",     0,  0,  0 },
255       };
256 
257       // Fixup kinds from .reloc directive are like R_SPARC_NONE. They do
258       // not require any extra processing.
259       if (Kind >= FirstLiteralRelocationKind)
260         return MCAsmBackend::getFixupKindInfo(FK_NONE);
261 
262       if (Kind < FirstTargetFixupKind)
263         return MCAsmBackend::getFixupKindInfo(Kind);
264 
265       assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
266              "Invalid kind!");
267       if (Endian == support::little)
268         return InfosLE[Kind - FirstTargetFixupKind];
269 
270       return InfosBE[Kind - FirstTargetFixupKind];
271     }
272 
273     bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
274                                const MCValue &Target) override {
275       if (Fixup.getKind() >= FirstLiteralRelocationKind)
276         return true;
277       switch ((Sparc::Fixups)Fixup.getKind()) {
278       default:
279         return false;
280       case Sparc::fixup_sparc_wplt30:
281         if (Target.getSymA()->getSymbol().isTemporary())
282           return false;
283         [[fallthrough]];
284       case Sparc::fixup_sparc_tls_gd_hi22:
285       case Sparc::fixup_sparc_tls_gd_lo10:
286       case Sparc::fixup_sparc_tls_gd_add:
287       case Sparc::fixup_sparc_tls_gd_call:
288       case Sparc::fixup_sparc_tls_ldm_hi22:
289       case Sparc::fixup_sparc_tls_ldm_lo10:
290       case Sparc::fixup_sparc_tls_ldm_add:
291       case Sparc::fixup_sparc_tls_ldm_call:
292       case Sparc::fixup_sparc_tls_ldo_hix22:
293       case Sparc::fixup_sparc_tls_ldo_lox10:
294       case Sparc::fixup_sparc_tls_ldo_add:
295       case Sparc::fixup_sparc_tls_ie_hi22:
296       case Sparc::fixup_sparc_tls_ie_lo10:
297       case Sparc::fixup_sparc_tls_ie_ld:
298       case Sparc::fixup_sparc_tls_ie_ldx:
299       case Sparc::fixup_sparc_tls_ie_add:
300       case Sparc::fixup_sparc_tls_le_hix22:
301       case Sparc::fixup_sparc_tls_le_lox10:
302         return true;
303       }
304     }
305 
306     /// fixupNeedsRelaxation - Target specific predicate for whether a given
307     /// fixup requires the associated instruction to be relaxed.
308     bool fixupNeedsRelaxation(const MCFixup &Fixup,
309                               uint64_t Value,
310                               const MCRelaxableFragment *DF,
311                               const MCAsmLayout &Layout) const override {
312       // FIXME.
313       llvm_unreachable("fixupNeedsRelaxation() unimplemented");
314       return false;
315     }
316     void relaxInstruction(MCInst &Inst,
317                           const MCSubtargetInfo &STI) const override {
318       // FIXME.
319       llvm_unreachable("relaxInstruction() unimplemented");
320     }
321 
322     bool writeNopData(raw_ostream &OS, uint64_t Count,
323                       const MCSubtargetInfo *STI) const override {
324       // Cannot emit NOP with size not multiple of 32 bits.
325       if (Count % 4 != 0)
326         return false;
327 
328       uint64_t NumNops = Count / 4;
329       for (uint64_t i = 0; i != NumNops; ++i)
330         support::endian::write<uint32_t>(OS, 0x01000000, Endian);
331 
332       return true;
333     }
334   };
335 
336   class ELFSparcAsmBackend : public SparcAsmBackend {
337     Triple::OSType OSType;
338   public:
339     ELFSparcAsmBackend(const Target &T, Triple::OSType OSType) :
340       SparcAsmBackend(T), OSType(OSType) { }
341 
342     void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
343                     const MCValue &Target, MutableArrayRef<char> Data,
344                     uint64_t Value, bool IsResolved,
345                     const MCSubtargetInfo *STI) const override {
346 
347       if (Fixup.getKind() >= FirstLiteralRelocationKind)
348         return;
349       Value = adjustFixupValue(Fixup.getKind(), Value);
350       if (!Value) return;           // Doesn't change encoding.
351 
352       unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
353       unsigned Offset = Fixup.getOffset();
354       // For each byte of the fragment that the fixup touches, mask in the bits
355       // from the fixup value. The Value has been "split up" into the
356       // appropriate bitfields above.
357       for (unsigned i = 0; i != NumBytes; ++i) {
358         unsigned Idx = Endian == support::little ? i : (NumBytes - 1) - i;
359         Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
360       }
361     }
362 
363     std::unique_ptr<MCObjectTargetWriter>
364     createObjectTargetWriter() const override {
365       uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(OSType);
366       return createSparcELFObjectWriter(Is64Bit, OSABI);
367     }
368   };
369 
370 } // end anonymous namespace
371 
372 MCAsmBackend *llvm::createSparcAsmBackend(const Target &T,
373                                           const MCSubtargetInfo &STI,
374                                           const MCRegisterInfo &MRI,
375                                           const MCTargetOptions &Options) {
376   return new ELFSparcAsmBackend(T, STI.getTargetTriple().getOS());
377 }
378