LLVM 18.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"
21
22#define DEBUG_TYPE "loongarch-asmbackend"
23
24using namespace llvm;
25
26std::optional<MCFixupKind>
30#define ELF_RELOC(X, Y) .Case(#X, Y)
31#include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
32#undef ELF_RELOC
33 .Case("BFD_RELOC_NONE", ELF::R_LARCH_NONE)
34 .Case("BFD_RELOC_32", ELF::R_LARCH_32)
35 .Case("BFD_RELOC_64", ELF::R_LARCH_64)
36 .Default(-1u);
37 if (Type != -1u)
38 return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type);
39 }
40 return std::nullopt;
41}
42
43const MCFixupKindInfo &
45 const static MCFixupKindInfo Infos[] = {
46 // This table *must* be in the order that the fixup_* kinds are defined in
47 // LoongArchFixupKinds.h.
48 //
49 // {name, offset, bits, flags}
50 {"fixup_loongarch_b16", 10, 16, MCFixupKindInfo::FKF_IsPCRel},
51 {"fixup_loongarch_b21", 0, 26, MCFixupKindInfo::FKF_IsPCRel},
52 {"fixup_loongarch_b26", 0, 26, MCFixupKindInfo::FKF_IsPCRel},
53 {"fixup_loongarch_abs_hi20", 5, 20, 0},
54 {"fixup_loongarch_abs_lo12", 10, 12, 0},
55 {"fixup_loongarch_abs64_lo20", 5, 20, 0},
56 {"fixup_loongarch_abs64_hi12", 10, 12, 0},
57 {"fixup_loongarch_tls_le_hi20", 5, 20, 0},
58 {"fixup_loongarch_tls_le_lo12", 10, 12, 0},
59 {"fixup_loongarch_tls_le64_lo20", 5, 20, 0},
60 {"fixup_loongarch_tls_le64_hi12", 10, 12, 0},
61 // TODO: Add more fixup kinds.
62 };
63
64 static_assert((std::size(Infos)) == LoongArch::NumTargetFixupKinds,
65 "Not all fixup kinds added to Infos array");
66
67 // Fixup kinds from .reloc directive are like R_LARCH_NONE. They
68 // do not require any extra processing.
71
72 if (Kind < FirstTargetFixupKind)
74
75 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
76 "Invalid kind!");
77 return Infos[Kind - FirstTargetFixupKind];
78}
79
80static void reportOutOfRangeError(MCContext &Ctx, SMLoc Loc, unsigned N) {
81 Ctx.reportError(Loc, "fixup value out of range [" + Twine(llvm::minIntN(N)) +
82 ", " + Twine(llvm::maxIntN(N)) + "]");
83}
84
86 MCContext &Ctx) {
87 switch (Fixup.getTargetKind()) {
88 default:
89 llvm_unreachable("Unknown fixup kind");
90 case FK_Data_1:
91 case FK_Data_2:
92 case FK_Data_4:
93 case FK_Data_8:
94 return Value;
96 if (!isInt<18>(Value))
97 reportOutOfRangeError(Ctx, Fixup.getLoc(), 18);
98 if (Value % 4)
99 Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
100 return (Value >> 2) & 0xffff;
101 }
103 if (!isInt<23>(Value))
104 reportOutOfRangeError(Ctx, Fixup.getLoc(), 23);
105 if (Value % 4)
106 Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
107 return ((Value & 0x3fffc) << 8) | ((Value >> 18) & 0x1f);
108 }
110 if (!isInt<28>(Value))
111 reportOutOfRangeError(Ctx, Fixup.getLoc(), 28);
112 if (Value % 4)
113 Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
114 return ((Value & 0x3fffc) << 8) | ((Value >> 18) & 0x3ff);
115 }
118 return (Value >> 12) & 0xfffff;
121 return Value & 0xfff;
124 return (Value >> 32) & 0xfffff;
127 return (Value >> 52) & 0xfff;
128 }
129}
130
132 const MCFixup &Fixup,
133 const MCValue &Target,
135 bool IsResolved,
136 const MCSubtargetInfo *STI) const {
137 if (!Value)
138 return; // Doesn't change encoding.
139
140 MCFixupKind Kind = Fixup.getKind();
141 if (Kind >= FirstLiteralRelocationKind)
142 return;
144 MCContext &Ctx = Asm.getContext();
145
146 // Apply any target-specific value adjustments.
148
149 // Shift the value into position.
150 Value <<= Info.TargetOffset;
151
152 unsigned Offset = Fixup.getOffset();
153 unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
154
155 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
156 // For each byte of the fragment that the fixup touches, mask in the
157 // bits from the fixup value.
158 for (unsigned I = 0; I != NumBytes; ++I) {
159 Data[Offset + I] |= uint8_t((Value >> (I * 8)) & 0xff);
160 }
161}
162
164 const MCFixup &Fixup,
165 const MCValue &Target) {
166 if (Fixup.getKind() >= FirstLiteralRelocationKind)
167 return true;
168 switch (Fixup.getTargetKind()) {
169 default:
170 return STI.hasFeature(LoongArch::FeatureRelax);
171 case FK_Data_1:
172 case FK_Data_2:
173 case FK_Data_4:
174 case FK_Data_8:
175 return !Target.isAbsolute();
176 }
177}
178
180 const MCSubtargetInfo *STI) const {
181 // We mostly follow binutils' convention here: align to 4-byte boundary with a
182 // 0-fill padding.
183 OS.write_zeros(Count % 4);
184
185 // The remainder is now padded with 4-byte nops.
186 // nop: andi r0, r0, 0
187 for (; Count >= 4; Count -= 4)
188 OS.write("\0\0\x40\x03", 4);
189
190 return true;
191}
192
193std::unique_ptr<MCObjectTargetWriter>
196 OSABI, Is64Bit, STI.hasFeature(LoongArch::FeatureRelax));
197}
198
200 const MCSubtargetInfo &STI,
201 const MCRegisterInfo &MRI,
202 const MCTargetOptions &Options) {
203 const Triple &TT = STI.getTargetTriple();
204 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
205 return new LoongArchAsmBackend(STI, OSABI, TT.isArch64Bit(), Options);
206}
unsigned const MachineRegisterInfo * MRI
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
std::string Name
static LVOptions Options
Definition: LVOptions.cpp:25
static void reportOutOfRangeError(MCContext &Ctx, SMLoc Loc, unsigned N)
#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
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...
std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const override
std::optional< MCFixupKind > getFixupKind(StringRef Name) const override
Map a relocation name used in .reloc to a fixup kind.
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.
const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const override
Get information on a fixup kind.
unsigned getNumFixupKinds() const override
Get the number of target specific fixup kinds.
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.
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:43
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
Context object for machine code objects.
Definition: MCContext.h:76
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1058
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...
Generic base class for all target subtargets.
bool hasFeature(unsigned Feature) const
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:307
Represents a location in source code.
Definition: SMLoc.h:23
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.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
Definition: Triple.h:678
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
raw_ostream & write_zeros(unsigned NumZeros)
write_zeros - Insert 'NumZeros' nulls.
raw_ostream & write(unsigned char C)
#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:440
int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition: MathExtras.h:219
std::unique_ptr< MCObjectTargetWriter > createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit, bool Relax)
MCAsmBackend * createLoongArchAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
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_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ FK_NONE
A no-op fixup.
Definition: MCFixup.h:22
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:24
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
Definition: MathExtras.h:212
#define N
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...