LLVM 17.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
25using namespace llvm;
26
27std::optional<MCFixupKind>
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
44const 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.
72
73 if (Kind < FirstTargetFixupKind)
75
76 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
77 "Invalid kind!");
78 return Infos[Kind - FirstTargetFixupKind];
79}
80
81static 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 // We mostly follow binutils' convention here: align to 4-byte boundary with a
183 // 0-fill padding.
184 OS.write_zeros(Count % 4);
185
186 // The remainder is now padded with 4-byte nops.
187 // nop: andi r0, r0, 0
188 for (; Count >= 4; Count -= 4)
189 OS.write("\0\0\x40\x03", 4);
190
191 return true;
192}
193
194std::unique_ptr<MCObjectTargetWriter>
196 return createLoongArchELFObjectWriter(OSABI, Is64Bit);
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:41
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:1049
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.
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
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:675
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:247
std::unique_ptr< MCObjectTargetWriter > createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit)
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:240
#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...