LLVM  16.0.0git
LoongArchAsmBackend.cpp
Go to the documentation of this file.
1 //===-- LoongArchAsmBackend.cpp - LoongArch Assembler Backend -*- C++ -*---===//
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 // This file implements the LoongArchAsmBackend class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "LoongArchAsmBackend.h"
14 #include "LoongArchFixupKinds.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/Support/Endian.h"
22 
23 #define DEBUG_TYPE "loongarch-asmbackend"
24 
25 using namespace llvm;
26 
27 std::optional<MCFixupKind>
29  if (STI.getTargetTriple().isOSBinFormatELF()) {
31 #define ELF_RELOC(X, Y) .Case(#X, Y)
32 #include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
33 #undef ELF_RELOC
34  .Case("BFD_RELOC_NONE", ELF::R_LARCH_NONE)
35  .Case("BFD_RELOC_32", ELF::R_LARCH_32)
36  .Case("BFD_RELOC_64", ELF::R_LARCH_64)
37  .Default(-1u);
38  if (Type != -1u)
39  return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
40  }
41  return std::nullopt;
42 }
43 
44 const MCFixupKindInfo &
46  const static MCFixupKindInfo Infos[] = {
47  // This table *must* be in the order that the fixup_* kinds are defined in
48  // LoongArchFixupKinds.h.
49  //
50  // {name, offset, bits, flags}
51  {"fixup_loongarch_b16", 10, 16, MCFixupKindInfo::FKF_IsPCRel},
52  {"fixup_loongarch_b21", 0, 26, MCFixupKindInfo::FKF_IsPCRel},
53  {"fixup_loongarch_b26", 0, 26, MCFixupKindInfo::FKF_IsPCRel},
54  {"fixup_loongarch_abs_hi20", 5, 20, 0},
55  {"fixup_loongarch_abs_lo12", 10, 12, 0},
56  {"fixup_loongarch_abs64_lo20", 5, 20, 0},
57  {"fixup_loongarch_abs64_hi12", 10, 12, 0},
58  {"fixup_loongarch_tls_le_hi20", 5, 20, 0},
59  {"fixup_loongarch_tls_le_lo12", 10, 12, 0},
60  {"fixup_loongarch_tls_le64_lo20", 5, 20, 0},
61  {"fixup_loongarch_tls_le64_hi12", 10, 12, 0},
62  // TODO: Add more fixup kinds.
63  };
64 
65  static_assert((std::size(Infos)) == LoongArch::NumTargetFixupKinds,
66  "Not all fixup kinds added to Infos array");
67 
68  // Fixup kinds from .reloc directive are like R_LARCH_NONE. They
69  // do not require any extra processing.
70  if (Kind >= FirstLiteralRelocationKind)
72 
73  if (Kind < FirstTargetFixupKind)
74  return MCAsmBackend::getFixupKindInfo(Kind);
75 
76  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
77  "Invalid kind!");
78  return Infos[Kind - FirstTargetFixupKind];
79 }
80 
81 static void reportOutOfRangeError(MCContext &Ctx, SMLoc Loc, unsigned N) {
82  Ctx.reportError(Loc, "fixup value out of range [" + Twine(llvm::minIntN(N)) +
83  ", " + Twine(llvm::maxIntN(N)) + "]");
84 }
85 
87  MCContext &Ctx) {
88  switch (Fixup.getTargetKind()) {
89  default:
90  llvm_unreachable("Unknown fixup kind");
91  case FK_Data_1:
92  case FK_Data_2:
93  case FK_Data_4:
94  case FK_Data_8:
95  return Value;
97  if (!isInt<18>(Value))
98  reportOutOfRangeError(Ctx, Fixup.getLoc(), 18);
99  if (Value % 4)
100  Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
101  return (Value >> 2) & 0xffff;
102  }
104  if (!isInt<23>(Value))
105  reportOutOfRangeError(Ctx, Fixup.getLoc(), 23);
106  if (Value % 4)
107  Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
108  return ((Value & 0x3fffc) << 8) | ((Value >> 18) & 0x1f);
109  }
111  if (!isInt<28>(Value))
112  reportOutOfRangeError(Ctx, Fixup.getLoc(), 28);
113  if (Value % 4)
114  Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
115  return ((Value & 0x3fffc) << 8) | ((Value >> 18) & 0x3ff);
116  }
119  return (Value >> 12) & 0xfffff;
122  return Value & 0xfff;
125  return (Value >> 32) & 0xfffff;
128  return (Value >> 52) & 0xfff;
129  }
130 }
131 
133  const MCFixup &Fixup,
134  const MCValue &Target,
136  bool IsResolved,
137  const MCSubtargetInfo *STI) const {
138  if (!Value)
139  return; // Doesn't change encoding.
140 
141  MCFixupKind Kind = Fixup.getKind();
142  if (Kind >= FirstLiteralRelocationKind)
143  return;
145  MCContext &Ctx = Asm.getContext();
146 
147  // Apply any target-specific value adjustments.
149 
150  // Shift the value into position.
151  Value <<= Info.TargetOffset;
152 
153  unsigned Offset = Fixup.getOffset();
154  unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
155 
156  assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
157  // For each byte of the fragment that the fixup touches, mask in the
158  // bits from the fixup value.
159  for (unsigned I = 0; I != NumBytes; ++I) {
160  Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
161  }
162 }
163 
165  const MCFixup &Fixup,
166  const MCValue &Target) {
167  if (Fixup.getKind() >= FirstLiteralRelocationKind)
168  return true;
169  switch (Fixup.getTargetKind()) {
170  default:
171  return false;
172  case FK_Data_1:
173  case FK_Data_2:
174  case FK_Data_4:
175  case FK_Data_8:
176  return !Target.isAbsolute();
177  }
178 }
179 
181  const MCSubtargetInfo *STI) const {
182  // Check for byte count not multiple of instruction word size
183  if (Count % 4 != 0)
184  return false;
185 
186  // The nop on LoongArch is andi r0, r0, 0.
187  for (; Count >= 4; Count -= 4)
188  support::endian::write<uint32_t>(OS, 0x03400000, support::little);
189 
190  return true;
191 }
192 
193 std::unique_ptr<MCObjectTargetWriter>
195  return createLoongArchELFObjectWriter(OSABI, Is64Bit);
196 }
197 
199  const MCSubtargetInfo &STI,
200  const MCRegisterInfo &MRI,
201  const MCTargetOptions &Options) {
202  const Triple &TT = STI.getTargetTriple();
203  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
204  return new LoongArchAsmBackend(STI, OSABI, TT.isArch64Bit());
205 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:156
llvm::LoongArch::fixup_loongarch_abs64_lo20
@ fixup_loongarch_abs64_lo20
Definition: LoongArchFixupKinds.h:37
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::MCAsmBackend::getFixupKindInfo
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
Definition: MCAsmBackend.cpp:81
T
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:77
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:150
llvm::FirstTargetFixupKind
@ FirstTargetFixupKind
Definition: MCFixup.h:45
llvm::LoongArchAsmBackend::createObjectTargetWriter
std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const override
Definition: LoongArchAsmBackend.cpp:194
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
MCAssembler.h
llvm::LoongArch::fixup_loongarch_tls_le64_lo20
@ fixup_loongarch_tls_le64_lo20
Definition: LoongArchFixupKinds.h:45
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::maxIntN
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition: MathExtras.h:413
llvm::LoongArch::fixup_loongarch_tls_le_lo12
@ fixup_loongarch_tls_le_lo12
Definition: LoongArchFixupKinds.h:43
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::LoongArch::fixup_loongarch_abs64_hi12
@ fixup_loongarch_abs64_hi12
Definition: LoongArchFixupKinds.h:39
llvm::FK_Data_4
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
llvm::Triple::isOSBinFormatELF
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:673
llvm::MCAsmBackend
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:41
llvm::MutableArrayRef< char >
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::support::little
@ little
Definition: Endian.h:27
reportOutOfRangeError
static void reportOutOfRangeError(MCContext &Ctx, SMLoc Loc, unsigned N)
Definition: LoongArchAsmBackend.cpp:81
llvm::MCSubtargetInfo::getTargetTriple
const Triple & getTargetTriple() const
Definition: MCSubtargetInfo.h:108
llvm::LoongArch::fixup_loongarch_b16
@ fixup_loongarch_b16
Definition: LoongArchFixupKinds.h:27
MCContext.h
llvm::LoongArchAsmBackend::getFixupKindInfo
const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const override
Get information on a fixup kind.
Definition: LoongArchAsmBackend.cpp:45
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
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::LoongArchAsmBackend::shouldForceRelocation
bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target) override
Hook to check if a relocation is needed for some target specific reason.
Definition: LoongArchAsmBackend.cpp:164
llvm::createLoongArchAsmBackend
MCAsmBackend * createLoongArchAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
Definition: LoongArchAsmBackend.cpp:198
LoongArchAsmBackend.h
llvm::LoongArch::fixup_loongarch_abs_hi20
@ fixup_loongarch_abs_hi20
Definition: LoongArchFixupKinds.h:33
llvm::MCAssembler
Definition: MCAssembler.h:73
llvm::minIntN
int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
Definition: MathExtras.h:406
uint64_t
llvm::LoongArch::fixup_loongarch_b26
@ fixup_loongarch_b26
Definition: LoongArchFixupKinds.h:31
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:58
size
i< reg-> size
Definition: README.txt:166
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:1055
llvm::LoongArch::fixup_loongarch_tls_le64_hi12
@ fixup_loongarch_tls_le64_hi12
Definition: LoongArchFixupKinds.h:47
llvm::FK_Data_1
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
llvm::MCTargetOptions
Definition: MCTargetOptions.h:37
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:215
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
adjustFixupValue
static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value, MCContext &Ctx)
Definition: LoongArchAsmBackend.cpp:86
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
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
MCAsmLayout.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::createLoongArchELFObjectWriter
std::unique_ptr< MCObjectTargetWriter > createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit)
Definition: LoongArchELFObjectWriter.cpp:90
llvm::LoongArch::fixup_loongarch_b21
@ fixup_loongarch_b21
Definition: LoongArchFixupKinds.h:29
EndianStream.h
llvm::LoongArch::NumTargetFixupKinds
@ NumTargetFixupKinds
Definition: LoongArchFixupKinds.h:53
llvm::LoongArch::fixup_loongarch_abs_lo12
@ fixup_loongarch_abs_lo12
Definition: LoongArchFixupKinds.h:35
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::LoongArchAsmBackend::getNumFixupKinds
unsigned getNumFixupKinds() const override
Get the number of target specific fixup kinds.
Definition: LoongArchAsmBackend.h:50
MCValue.h
llvm::LoongArchAsmBackend::getFixupKind
std::optional< MCFixupKind > getFixupKind(StringRef Name) const override
Map a relocation name used in .reloc to a fixup kind.
Definition: LoongArchAsmBackend.cpp:28
llvm::LoongArchAsmBackend::writeNopData
bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const override
Write an (optimal) nop sequence of Count bytes to the given output.
Definition: LoongArchAsmBackend.cpp:180
llvm::MCFixupKind
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
llvm::LoongArchAsmBackend::applyFixup
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const override
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
Definition: LoongArchAsmBackend.cpp:132
llvm::FK_Data_8
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
N
#define N
LoongArchFixupKinds.h
llvm::StringSwitch::Default
R Default(T Value)
Definition: StringSwitch.h:182
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:36
llvm::FK_Data_2
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:24
Endian.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:76
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::LoongArchAsmBackend
Definition: LoongArchAsmBackend.h:25
llvm::LoongArch::fixup_loongarch_tls_le_hi20
@ fixup_loongarch_tls_le_hi20
Definition: LoongArchFixupKinds.h:41