[Darwin][Driver][clang] apple-none-macho orders the resource directory after internal...
[llvm-project.git] / llvm / lib / Target / SystemZ / MCTargetDesc / SystemZMCAsmBackend.cpp
blobed174f7ac01fa3a2371b75b5cf57e3d7554ded89
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 //===----------------------------------------------------------------------===//
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/MCAssembler.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCELFObjectWriter.h"
16 #include "llvm/MC/MCFixupKindInfo.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCSubtargetInfo.h"
21 using namespace llvm;
23 // Value is a fully-resolved relocation value: Symbol + Addend [- Pivot].
24 // Return the bits that should be installed in a relocation field for
25 // fixup kind Kind.
26 static uint64_t extractBitsForFixup(MCFixupKind Kind, uint64_t Value,
27 const MCFixup &Fixup, MCContext &Ctx) {
28 if (Kind < FirstTargetFixupKind)
29 return Value;
31 auto checkFixupInRange = [&](int64_t Min, int64_t Max) -> bool {
32 int64_t SVal = int64_t(Value);
33 if (SVal < Min || SVal > Max) {
34 Ctx.reportError(Fixup.getLoc(), "operand out of range (" + Twine(SVal) +
35 " not between " + Twine(Min) +
36 " and " + Twine(Max) + ")");
37 return false;
39 return true;
42 auto handlePCRelFixupValue = [&](unsigned W) -> uint64_t {
43 if (Value % 2 != 0)
44 Ctx.reportError(Fixup.getLoc(), "Non-even PC relative offset.");
45 if (!checkFixupInRange(minIntN(W) * 2, maxIntN(W) * 2))
46 return 0;
47 return (int64_t)Value / 2;
50 auto handleImmValue = [&](bool IsSigned, unsigned W) -> uint64_t {
51 if (!(IsSigned ? checkFixupInRange(minIntN(W), maxIntN(W))
52 : checkFixupInRange(0, maxUIntN(W))))
53 return 0;
54 return Value;
57 switch (unsigned(Kind)) {
58 case SystemZ::FK_390_PC12DBL:
59 return handlePCRelFixupValue(12);
60 case SystemZ::FK_390_PC16DBL:
61 return handlePCRelFixupValue(16);
62 case SystemZ::FK_390_PC24DBL:
63 return handlePCRelFixupValue(24);
64 case SystemZ::FK_390_PC32DBL:
65 return handlePCRelFixupValue(32);
67 case SystemZ::FK_390_TLS_CALL:
68 return 0;
70 case SystemZ::FK_390_S8Imm:
71 return handleImmValue(true, 8);
72 case SystemZ::FK_390_S16Imm:
73 return handleImmValue(true, 16);
74 case SystemZ::FK_390_S20Imm: {
75 Value = handleImmValue(true, 20);
76 // S20Imm is used only for signed 20-bit displacements.
77 // The high byte of a 20 bit displacement value comes first.
78 uint64_t DLo = Value & 0xfff;
79 uint64_t DHi = (Value >> 12) & 0xff;
80 return (DLo << 8) | DHi;
82 case SystemZ::FK_390_S32Imm:
83 return handleImmValue(true, 32);
84 case SystemZ::FK_390_U1Imm:
85 return handleImmValue(false, 1);
86 case SystemZ::FK_390_U2Imm:
87 return handleImmValue(false, 2);
88 case SystemZ::FK_390_U3Imm:
89 return handleImmValue(false, 3);
90 case SystemZ::FK_390_U4Imm:
91 return handleImmValue(false, 4);
92 case SystemZ::FK_390_U8Imm:
93 return handleImmValue(false, 8);
94 case SystemZ::FK_390_U12Imm:
95 return handleImmValue(false, 12);
96 case SystemZ::FK_390_U16Imm:
97 return handleImmValue(false, 16);
98 case SystemZ::FK_390_U32Imm:
99 return handleImmValue(false, 32);
100 case SystemZ::FK_390_U48Imm:
101 return handleImmValue(false, 48);
104 llvm_unreachable("Unknown fixup kind!");
107 namespace {
108 class SystemZMCAsmBackend : public MCAsmBackend {
109 public:
110 SystemZMCAsmBackend() : MCAsmBackend(llvm::endianness::big) {}
112 // Override MCAsmBackend
113 unsigned getNumFixupKinds() const override {
114 return SystemZ::NumTargetFixupKinds;
116 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
117 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
118 bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
119 const MCValue &Target,
120 const MCSubtargetInfo *STI) override;
121 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
122 const MCValue &Target, MutableArrayRef<char> Data,
123 uint64_t Value, bool IsResolved,
124 const MCSubtargetInfo *STI) const override;
125 bool writeNopData(raw_ostream &OS, uint64_t Count,
126 const MCSubtargetInfo *STI) const override;
128 } // end anonymous namespace
130 std::optional<MCFixupKind>
131 SystemZMCAsmBackend::getFixupKind(StringRef Name) const {
132 unsigned Type = llvm::StringSwitch<unsigned>(Name)
133 #define ELF_RELOC(X, Y) .Case(#X, Y)
134 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
135 #undef ELF_RELOC
136 .Case("BFD_RELOC_NONE", ELF::R_390_NONE)
137 .Case("BFD_RELOC_8", ELF::R_390_8)
138 .Case("BFD_RELOC_16", ELF::R_390_16)
139 .Case("BFD_RELOC_32", ELF::R_390_32)
140 .Case("BFD_RELOC_64", ELF::R_390_64)
141 .Default(-1u);
142 if (Type != -1u)
143 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
144 return std::nullopt;
147 const MCFixupKindInfo &
148 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
149 // Fixup kinds from .reloc directive are like R_390_NONE. They
150 // do not require any extra processing.
151 if (Kind >= FirstLiteralRelocationKind)
152 return MCAsmBackend::getFixupKindInfo(FK_NONE);
154 if (Kind < FirstTargetFixupKind)
155 return MCAsmBackend::getFixupKindInfo(Kind);
157 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
158 "Invalid kind!");
159 return SystemZ::MCFixupKindInfos[Kind - FirstTargetFixupKind];
162 bool SystemZMCAsmBackend::shouldForceRelocation(const MCAssembler &,
163 const MCFixup &Fixup,
164 const MCValue &,
165 const MCSubtargetInfo *STI) {
166 return Fixup.getKind() >= FirstLiteralRelocationKind;
169 void SystemZMCAsmBackend::applyFixup(const MCAssembler &Asm,
170 const MCFixup &Fixup,
171 const MCValue &Target,
172 MutableArrayRef<char> Data, uint64_t Value,
173 bool IsResolved,
174 const MCSubtargetInfo *STI) const {
175 MCFixupKind Kind = Fixup.getKind();
176 if (Kind >= FirstLiteralRelocationKind)
177 return;
178 unsigned Offset = Fixup.getOffset();
179 unsigned BitSize = getFixupKindInfo(Kind).TargetSize;
180 unsigned Size = (BitSize + 7) / 8;
182 assert(Offset + Size <= Data.size() && "Invalid fixup offset!");
184 // Big-endian insertion of Size bytes.
185 Value = extractBitsForFixup(Kind, Value, Fixup, Asm.getContext());
186 if (BitSize < 64)
187 Value &= ((uint64_t)1 << BitSize) - 1;
188 unsigned ShiftValue = (Size * 8) - 8;
189 for (unsigned I = 0; I != Size; ++I) {
190 Data[Offset + I] |= uint8_t(Value >> ShiftValue);
191 ShiftValue -= 8;
195 bool SystemZMCAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
196 const MCSubtargetInfo *STI) const {
197 for (uint64_t I = 0; I != Count; ++I)
198 OS << '\x7';
199 return true;
202 namespace {
203 class ELFSystemZAsmBackend : public SystemZMCAsmBackend {
204 uint8_t OSABI;
206 public:
207 ELFSystemZAsmBackend(uint8_t OsABI) : SystemZMCAsmBackend(), OSABI(OsABI){};
209 std::unique_ptr<MCObjectTargetWriter>
210 createObjectTargetWriter() const override {
211 return createSystemZELFObjectWriter(OSABI);
215 class GOFFSystemZAsmBackend : public SystemZMCAsmBackend {
216 public:
217 GOFFSystemZAsmBackend() : SystemZMCAsmBackend(){};
219 std::unique_ptr<MCObjectTargetWriter>
220 createObjectTargetWriter() const override {
221 return createSystemZGOFFObjectWriter();
224 } // namespace
226 MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T,
227 const MCSubtargetInfo &STI,
228 const MCRegisterInfo &MRI,
229 const MCTargetOptions &Options) {
230 if (STI.getTargetTriple().isOSzOS()) {
231 return new GOFFSystemZAsmBackend();
234 uint8_t OSABI =
235 MCELFObjectTargetWriter::getOSABI(STI.getTargetTriple().getOS());
236 return new ELFSystemZAsmBackend(OSABI);