LLVM  14.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 
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCObjectWriter.h"
20 
21 using 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) {
29  return Value;
30 
31  switch (unsigned(Kind)) {
36  return (int64_t)Value / 2;
37 
38  case SystemZ::FK_390_12:
39  if (!isUInt<12>(Value)) {
40  Ctx.reportError(Fixup.getLoc(), "displacement exceeds uint12");
41  return 0;
42  }
43  return Value;
44 
45  case SystemZ::FK_390_20: {
46  if (!isInt<20>(Value)) {
47  Ctx.reportError(Fixup.getLoc(), "displacement exceeds int20");
48  return 0;
49  }
50  // The high byte of a 20 bit displacement value comes first.
51  uint64_t DLo = Value & 0xfff;
52  uint64_t DHi = (Value >> 12) & 0xff;
53  return (DLo << 8) | DHi;
54  }
55 
57  return 0;
58  }
59 
60  llvm_unreachable("Unknown fixup kind!");
61 }
62 
63 namespace {
64 class SystemZMCAsmBackend : public MCAsmBackend {
65  uint8_t OSABI;
66 public:
67  SystemZMCAsmBackend(uint8_t osABI)
68  : MCAsmBackend(support::big), OSABI(osABI) {}
69 
70  // Override MCAsmBackend
71  unsigned getNumFixupKinds() const override {
73  }
74  Optional<MCFixupKind> getFixupKind(StringRef Name) const override;
75  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
76  bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
77  const MCValue &Target) override;
78  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
80  uint64_t Value, bool IsResolved,
81  const MCSubtargetInfo *STI) const override;
82  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
83  const MCRelaxableFragment *Fragment,
84  const MCAsmLayout &Layout) const override {
85  return false;
86  }
87  bool writeNopData(raw_ostream &OS, uint64_t Count,
88  const MCSubtargetInfo *STI) const override;
89  std::unique_ptr<MCObjectTargetWriter>
90  createObjectTargetWriter() const override {
91  return createSystemZObjectWriter(OSABI);
92  }
93 };
94 } // end anonymous namespace
95 
96 Optional<MCFixupKind> SystemZMCAsmBackend::getFixupKind(StringRef Name) const {
98 #define ELF_RELOC(X, Y) .Case(#X, Y)
99 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
100 #undef ELF_RELOC
101  .Case("BFD_RELOC_NONE", ELF::R_390_NONE)
102  .Case("BFD_RELOC_8", ELF::R_390_8)
103  .Case("BFD_RELOC_16", ELF::R_390_16)
104  .Case("BFD_RELOC_32", ELF::R_390_32)
105  .Case("BFD_RELOC_64", ELF::R_390_64)
106  .Default(-1u);
107  if (Type != -1u)
108  return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
109  return None;
110 }
111 
112 const MCFixupKindInfo &
113 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
114  const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = {
115  { "FK_390_PC12DBL", 4, 12, MCFixupKindInfo::FKF_IsPCRel },
116  { "FK_390_PC16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
117  { "FK_390_PC24DBL", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
118  { "FK_390_PC32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
119  { "FK_390_TLS_CALL", 0, 0, 0 },
120  { "FK_390_12", 4, 12, 0 },
121  { "FK_390_20", 4, 20, 0 }
122  };
123 
124  // Fixup kinds from .reloc directive are like R_390_NONE. They
125  // do not require any extra processing.
128 
131 
132  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
133  "Invalid kind!");
134  return Infos[Kind - FirstTargetFixupKind];
135 }
136 
137 bool SystemZMCAsmBackend::shouldForceRelocation(const MCAssembler &,
138  const MCFixup &Fixup,
139  const MCValue &) {
140  return Fixup.getKind() >= FirstLiteralRelocationKind;
141 }
142 
144  const MCFixup &Fixup,
145  const MCValue &Target,
147  bool IsResolved,
148  const MCSubtargetInfo *STI) const {
149  MCFixupKind Kind = Fixup.getKind();
151  return;
152  unsigned Offset = Fixup.getOffset();
153  unsigned BitSize = getFixupKindInfo(Kind).TargetSize;
154  unsigned Size = (BitSize + 7) / 8;
155 
156  assert(Offset + Size <= Data.size() && "Invalid fixup offset!");
157 
158  // Big-endian insertion of Size bytes.
159  Value = extractBitsForFixup(Kind, Value, Fixup, Asm.getContext());
160  if (BitSize < 64)
161  Value &= ((uint64_t)1 << BitSize) - 1;
162  unsigned ShiftValue = (Size * 8) - 8;
163  for (unsigned I = 0; I != Size; ++I) {
164  Data[Offset + I] |= uint8_t(Value >> ShiftValue);
165  ShiftValue -= 8;
166  }
167 }
168 
169 bool SystemZMCAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
170  const MCSubtargetInfo *STI) const {
171  for (uint64_t I = 0; I != Count; ++I)
172  OS << '\x7';
173  return true;
174 }
175 
177  const MCSubtargetInfo &STI,
178  const MCRegisterInfo &MRI,
179  const MCTargetOptions &Options) {
180  uint8_t OSABI =
182  return new SystemZMCAsmBackend(OSABI);
183 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::MCRelaxableFragment
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:271
llvm::MCAsmBackend::getFixupKindInfo
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
Definition: MCAsmBackend.cpp:74
llvm::SystemZ::FK_390_20
@ FK_390_20
Definition: SystemZMCFixups.h:24
T
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
llvm::SystemZ::FK_390_PC16DBL
@ FK_390_PC16DBL
Definition: SystemZMCFixups.h:19
llvm::FirstTargetFixupKind
@ FirstTargetFixupKind
Definition: MCFixup.h:45
MCAssembler.h
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::Optional
Definition: APInt.h:33
MCFixupKindInfo.h
SystemZMCTargetDesc.h
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::SystemZ::FK_390_PC12DBL
@ FK_390_PC12DBL
Definition: SystemZMCFixups.h:18
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::FirstLiteralRelocationKind
@ FirstLiteralRelocationKind
The range [FirstLiteralRelocationKind, MaxTargetFixupKind) is used for relocations coming from ....
Definition: MCFixup.h:50
llvm::MCAsmBackend
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
MCAsmBackend.h
llvm::MutableArrayRef< char >
llvm::SystemZ::FK_390_PC24DBL
@ FK_390_PC24DBL
Definition: SystemZMCFixups.h:20
llvm::MCSubtargetInfo::getTargetTriple
const Triple & getTargetTriple() const
Definition: MCSubtargetInfo.h:107
MCContext.h
llvm::SystemZ::FK_390_TLS_CALL
@ FK_390_TLS_CALL
Definition: SystemZMCFixups.h:22
MCInst.h
MCSubtargetInfo.h
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::SystemZ::FK_390_12
@ FK_390_12
Definition: SystemZMCFixups.h:23
llvm::None
const NoneType None
Definition: None.h:23
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::MCAssembler
Definition: MCAssembler.h:60
uint64_t
llvm::Triple::getOS
OSType getOS() const
Get the parsed operating system type of this triple.
Definition: Triple.h:321
MCELFObjectWriter.h
llvm::MCFixupKindInfo::FKF_IsPCRel
@ FKF_IsPCRel
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
Definition: MCFixupKindInfo.h:19
I
#define I(x, y, z)
Definition: MD5.cpp:59
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCFixupKindInfo
Target independent information on a fixup kind.
Definition: MCFixupKindInfo.h:15
llvm::MCContext::reportError
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:963
llvm::MCTargetOptions
Definition: MCTargetOptions.h:36
llvm::createSystemZObjectWriter
std::unique_ptr< MCObjectTargetWriter > createSystemZObjectWriter(uint8_t OSABI)
Definition: SystemZMCObjectWriter.cpp:168
llvm::SystemZ::FK_390_PC32DBL
@ FK_390_PC32DBL
Definition: SystemZMCFixups.h:21
llvm::FK_NONE
@ FK_NONE
A no-op fixup.
Definition: MCFixup.h:22
llvm::MCELFObjectTargetWriter::getOSABI
uint8_t getOSABI() const
Definition: MCELFObjectWriter.h:101
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:233
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::MCRegisterInfo
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Definition: MCRegisterInfo.h:135
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
extractBitsForFixup
static uint64_t extractBitsForFixup(MCFixupKind Kind, uint64_t Value, const MCFixup &Fixup, MCContext &Ctx)
Definition: SystemZMCAsmBackend.cpp:26
MCObjectWriter.h
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
SystemZMCFixups.h
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
StringSwitch.h
llvm::SystemZ::NumTargetFixupKinds
@ NumTargetFixupKinds
Definition: SystemZMCFixups.h:28
llvm::MCFixupKind
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
llvm::createSystemZMCAsmBackend
MCAsmBackend * createSystemZMCAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
Definition: SystemZMCAsmBackend.cpp:176
support
Reimplement select in terms of SEL *We would really like to support but we need to prove that the add doesn t need to overflow between the two bit chunks *Implement pre post increment support(e.g. PR935) *Implement smarter const ant generation for binops with large immediates. A few ARMv6T2 ops should be pattern matched
Definition: README.txt:10
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:37
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::support::big
@ big
Definition: Endian.h:27