LLVM 17.0.0git
SystemZMCAsmBackend.cpp
Go to the documentation of this file.
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
13#include "llvm/MC/MCAssembler.h"
14#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCInst.h"
20
21using namespace llvm;
22
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.
27 const MCFixup &Fixup, MCContext &Ctx) {
28 if (Kind < FirstTargetFixupKind)
29 return Value;
30
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;
38 }
39 return true;
40 };
41
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;
48 };
49
50 switch (unsigned(Kind)) {
52 return handlePCRelFixupValue(12);
54 return handlePCRelFixupValue(16);
56 return handlePCRelFixupValue(24);
58 return handlePCRelFixupValue(32);
59
61 if (!checkFixupInRange(0, maxUIntN(12)))
62 return 0;
63 return Value;
64
65 case SystemZ::FK_390_20: {
66 if (!checkFixupInRange(minIntN(20), maxIntN(20)))
67 return 0;
68 // The high byte of a 20 bit displacement value comes first.
69 uint64_t DLo = Value & 0xfff;
70 uint64_t DHi = (Value >> 12) & 0xff;
71 return (DLo << 8) | DHi;
72 }
73
75 return 0;
76 }
77
78 llvm_unreachable("Unknown fixup kind!");
79}
80
81namespace {
82class SystemZMCAsmBackend : public MCAsmBackend {
83 uint8_t OSABI;
84public:
85 SystemZMCAsmBackend(uint8_t osABI)
86 : MCAsmBackend(support::big), OSABI(osABI) {}
87
88 // Override MCAsmBackend
89 unsigned getNumFixupKinds() const override {
91 }
92 std::optional<MCFixupKind> getFixupKind(StringRef Name) const override;
93 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
94 bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
95 const MCValue &Target) override;
96 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
98 uint64_t Value, bool IsResolved,
99 const MCSubtargetInfo *STI) const override;
101 const MCRelaxableFragment *Fragment,
102 const MCAsmLayout &Layout) const override {
103 return false;
104 }
105 bool writeNopData(raw_ostream &OS, uint64_t Count,
106 const MCSubtargetInfo *STI) const override;
107 std::unique_ptr<MCObjectTargetWriter>
108 createObjectTargetWriter() const override {
109 return createSystemZObjectWriter(OSABI);
110 }
111};
112} // end anonymous namespace
113
114std::optional<MCFixupKind>
115SystemZMCAsmBackend::getFixupKind(StringRef Name) const {
117#define ELF_RELOC(X, Y) .Case(#X, Y)
118#include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
119#undef ELF_RELOC
120 .Case("BFD_RELOC_NONE", ELF::R_390_NONE)
121 .Case("BFD_RELOC_8", ELF::R_390_8)
122 .Case("BFD_RELOC_16", ELF::R_390_16)
123 .Case("BFD_RELOC_32", ELF::R_390_32)
124 .Case("BFD_RELOC_64", ELF::R_390_64)
125 .Default(-1u);
126 if (Type != -1u)
127 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
128 return std::nullopt;
129}
130
131const MCFixupKindInfo &
132SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
133 const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = {
134 { "FK_390_PC12DBL", 4, 12, MCFixupKindInfo::FKF_IsPCRel },
135 { "FK_390_PC16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
136 { "FK_390_PC24DBL", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
137 { "FK_390_PC32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
138 { "FK_390_TLS_CALL", 0, 0, 0 },
139 { "FK_390_12", 4, 12, 0 },
140 { "FK_390_20", 4, 20, 0 }
141 };
142
143 // Fixup kinds from .reloc directive are like R_390_NONE. They
144 // do not require any extra processing.
145 if (Kind >= FirstLiteralRelocationKind)
147
148 if (Kind < FirstTargetFixupKind)
150
151 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
152 "Invalid kind!");
153 return Infos[Kind - FirstTargetFixupKind];
154}
155
156bool SystemZMCAsmBackend::shouldForceRelocation(const MCAssembler &,
157 const MCFixup &Fixup,
158 const MCValue &) {
159 return Fixup.getKind() >= FirstLiteralRelocationKind;
160}
161
162void SystemZMCAsmBackend::applyFixup(const MCAssembler &Asm,
163 const MCFixup &Fixup,
164 const MCValue &Target,
166 bool IsResolved,
167 const MCSubtargetInfo *STI) const {
168 MCFixupKind Kind = Fixup.getKind();
169 if (Kind >= FirstLiteralRelocationKind)
170 return;
171 unsigned Offset = Fixup.getOffset();
172 unsigned BitSize = getFixupKindInfo(Kind).TargetSize;
173 unsigned Size = (BitSize + 7) / 8;
174
175 assert(Offset + Size <= Data.size() && "Invalid fixup offset!");
176
177 // Big-endian insertion of Size bytes.
178 Value = extractBitsForFixup(Kind, Value, Fixup, Asm.getContext());
179 if (BitSize < 64)
180 Value &= ((uint64_t)1 << BitSize) - 1;
181 unsigned ShiftValue = (Size * 8) - 8;
182 for (unsigned I = 0; I != Size; ++I) {
183 Data[Offset + I] |= uint8_t(Value >> ShiftValue);
184 ShiftValue -= 8;
185 }
186}
187
188bool SystemZMCAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
189 const MCSubtargetInfo *STI) const {
190 for (uint64_t I = 0; I != Count; ++I)
191 OS << '\x7';
192 return true;
193}
194
196 const MCSubtargetInfo &STI,
197 const MCRegisterInfo &MRI,
198 const MCTargetOptions &Options) {
199 uint8_t OSABI =
201 return new SystemZMCAsmBackend(OSABI);
202}
unsigned const MachineRegisterInfo * MRI
std::string Name
uint64_t Size
static LVOptions Options
Definition: LVOptions.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
static uint64_t extractBitsForFixup(MCFixupKind Kind, uint64_t Value, const MCFixup &Fixup, MCContext &Ctx)
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:41
virtual bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const =0
Write an (optimal) nop sequence of Count bytes to the given output.
virtual bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const =0
Simple predicate for targets where !Resolved implies requiring relaxation.
virtual bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target)
Hook to check if a relocation is needed for some target specific reason.
Definition: MCAsmBackend.h:97
virtual std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const =0
virtual unsigned getNumFixupKinds() const =0
Get the number of target specific fixup kinds.
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
virtual std::optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
virtual void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const =0
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Context object for machine code objects.
Definition: MCContext.h:76
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1052
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:270
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
This represents an "assembler immediate".
Definition: MCValue.h:36
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:305
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
Target - Wrapper for Target specific information.
OSType getOS() const
Get the parsed operating system type of this triple.
Definition: Triple.h:365
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition: MathExtras.h:247
MCAsmBackend * createSystemZMCAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
std::unique_ptr< MCObjectTargetWriter > createSystemZObjectWriter(uint8_t OSABI)
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ FirstTargetFixupKind
Definition: MCFixup.h:45
@ FirstLiteralRelocationKind
The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for relocations coming from ....
Definition: MCFixup.h:50
@ FK_NONE
A no-op fixup.
Definition: MCFixup.h:22
int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
Definition: MathExtras.h:240
uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
Definition: MathExtras.h:229
Target independent information on a fixup kind.
@ FKF_IsPCRel
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...