xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZMCAsmBackend.cpp (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1 //===-- SystemZMCAsmBackend.cpp - SystemZ 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/SystemZMCFixups.h"
10 #include "MCTargetDesc/SystemZMCTargetDesc.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCFixupKindInfo.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
18 
19 using namespace llvm;
20 
21 // Value is a fully-resolved relocation value: Symbol + Addend [- Pivot].
22 // Return the bits that should be installed in a relocation field for
23 // fixup kind Kind.
24 static uint64_t extractBitsForFixup(MCFixupKind Kind, uint64_t Value) {
25   if (Kind < FirstTargetFixupKind)
26     return Value;
27 
28   switch (unsigned(Kind)) {
29   case SystemZ::FK_390_PC12DBL:
30   case SystemZ::FK_390_PC16DBL:
31   case SystemZ::FK_390_PC24DBL:
32   case SystemZ::FK_390_PC32DBL:
33     return (int64_t)Value / 2;
34 
35   case SystemZ::FK_390_TLS_CALL:
36     return 0;
37   }
38 
39   llvm_unreachable("Unknown fixup kind!");
40 }
41 
42 namespace {
43 class SystemZMCAsmBackend : public MCAsmBackend {
44   uint8_t OSABI;
45 public:
46   SystemZMCAsmBackend(uint8_t osABI)
47       : MCAsmBackend(support::big), OSABI(osABI) {}
48 
49   // Override MCAsmBackend
50   unsigned getNumFixupKinds() const override {
51     return SystemZ::NumTargetFixupKinds;
52   }
53   Optional<MCFixupKind> getFixupKind(StringRef Name) const override;
54   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
55   bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
56                              const MCValue &Target) override;
57   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
58                   const MCValue &Target, MutableArrayRef<char> Data,
59                   uint64_t Value, bool IsResolved,
60                   const MCSubtargetInfo *STI) const override;
61   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
62                             const MCRelaxableFragment *Fragment,
63                             const MCAsmLayout &Layout) const override {
64     return false;
65   }
66   bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
67   std::unique_ptr<MCObjectTargetWriter>
68   createObjectTargetWriter() const override {
69     return createSystemZObjectWriter(OSABI);
70   }
71 };
72 } // end anonymous namespace
73 
74 Optional<MCFixupKind> SystemZMCAsmBackend::getFixupKind(StringRef Name) const {
75   unsigned Type = llvm::StringSwitch<unsigned>(Name)
76 #define ELF_RELOC(X, Y) .Case(#X, Y)
77 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
78 #undef ELF_RELOC
79 			.Case("BFD_RELOC_NONE", ELF::R_390_NONE)
80 			.Case("BFD_RELOC_8", ELF::R_390_8)
81 			.Case("BFD_RELOC_16", ELF::R_390_16)
82 			.Case("BFD_RELOC_32", ELF::R_390_32)
83 			.Case("BFD_RELOC_64", ELF::R_390_64)
84 			.Default(-1u);
85   if (Type != -1u)
86     return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
87   return None;
88 }
89 
90 const MCFixupKindInfo &
91 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
92   const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = {
93     { "FK_390_PC12DBL",  4, 12, MCFixupKindInfo::FKF_IsPCRel },
94     { "FK_390_PC16DBL",  0, 16, MCFixupKindInfo::FKF_IsPCRel },
95     { "FK_390_PC24DBL",  0, 24, MCFixupKindInfo::FKF_IsPCRel },
96     { "FK_390_PC32DBL",  0, 32, MCFixupKindInfo::FKF_IsPCRel },
97     { "FK_390_TLS_CALL", 0, 0, 0 }
98   };
99 
100   // Fixup kinds from .reloc directive are like R_390_NONE. They
101   // do not require any extra processing.
102   if (Kind >= FirstLiteralRelocationKind)
103     return MCAsmBackend::getFixupKindInfo(FK_NONE);
104 
105   if (Kind < FirstTargetFixupKind)
106     return MCAsmBackend::getFixupKindInfo(Kind);
107 
108   assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
109          "Invalid kind!");
110   return Infos[Kind - FirstTargetFixupKind];
111 }
112 
113 bool SystemZMCAsmBackend::shouldForceRelocation(const MCAssembler &,
114 						const MCFixup &Fixup,
115 						const MCValue &) {
116   return Fixup.getKind() >= FirstLiteralRelocationKind;
117 }
118 
119 void SystemZMCAsmBackend::applyFixup(const MCAssembler &Asm,
120                                      const MCFixup &Fixup,
121                                      const MCValue &Target,
122                                      MutableArrayRef<char> Data, uint64_t Value,
123                                      bool IsResolved,
124                                      const MCSubtargetInfo *STI) const {
125   MCFixupKind Kind = Fixup.getKind();
126   if (Kind >= FirstLiteralRelocationKind)
127     return;
128   unsigned Offset = Fixup.getOffset();
129   unsigned BitSize = getFixupKindInfo(Kind).TargetSize;
130   unsigned Size = (BitSize + 7) / 8;
131 
132   assert(Offset + Size <= Data.size() && "Invalid fixup offset!");
133 
134   // Big-endian insertion of Size bytes.
135   Value = extractBitsForFixup(Kind, Value);
136   if (BitSize < 64)
137     Value &= ((uint64_t)1 << BitSize) - 1;
138   unsigned ShiftValue = (Size * 8) - 8;
139   for (unsigned I = 0; I != Size; ++I) {
140     Data[Offset + I] |= uint8_t(Value >> ShiftValue);
141     ShiftValue -= 8;
142   }
143 }
144 
145 bool SystemZMCAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
146   for (uint64_t I = 0; I != Count; ++I)
147     OS << '\x7';
148   return true;
149 }
150 
151 MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T,
152                                               const MCSubtargetInfo &STI,
153                                               const MCRegisterInfo &MRI,
154                                               const MCTargetOptions &Options) {
155   uint8_t OSABI =
156       MCELFObjectTargetWriter::getOSABI(STI.getTargetTriple().getOS());
157   return new SystemZMCAsmBackend(OSABI);
158 }
159