LLVM 20.0.0git
LoongArchMCExpr.cpp
Go to the documentation of this file.
1//===-- LoongArchMCExpr.cpp - LoongArch specific MC expression classes ----===//
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 contains the implementation of the assembly expression modifiers
10// accepted by the LoongArch architecture.
11//
12//===----------------------------------------------------------------------===//
13
14#include "LoongArchMCExpr.h"
15#include "LoongArchAsmBackend.h"
16#include "llvm/MC/MCContext.h"
17#include "llvm/MC/MCStreamer.h"
18#include "llvm/MC/MCSymbolELF.h"
19#include "llvm/MC/MCValue.h"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "loongarch-mcexpr"
26
28 VariantKind Kind, MCContext &Ctx,
29 bool Hint) {
30 return new (Ctx) LoongArchMCExpr(Expr, Kind, Hint);
31}
32
34 VariantKind Kind = getKind();
35 bool HasVariant =
36 ((Kind != VK_LoongArch_None) && (Kind != VK_LoongArch_CALL));
37
38 if (HasVariant)
39 OS << '%' << getVariantKindName(getKind()) << '(';
40 Expr->print(OS, MAI);
41 if (HasVariant)
42 OS << ')';
43}
44
46 const MCAssembler *Asm,
47 const MCFixup *Fixup) const {
48 // Explicitly drop the layout and assembler to prevent any symbolic folding in
49 // the expression handling. This is required to preserve symbolic difference
50 // expressions to emit the paired relocations.
51 if (!getSubExpr()->evaluateAsRelocatable(Res, nullptr, nullptr))
52 return false;
53
54 Res =
55 MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
56 // Custom fixup types are not valid with symbol difference expressions.
57 return Res.getSymB() ? getKind() == VK_LoongArch_None : true;
58}
59
61 Streamer.visitUsedExpr(*getSubExpr());
62}
63
65 switch (Kind) {
66 default:
67 llvm_unreachable("Invalid ELF symbol kind");
69 return "plt";
71 return "b16";
73 return "b21";
75 return "b26";
77 return "abs_hi20";
79 return "abs_lo12";
81 return "abs64_lo20";
83 return "abs64_hi12";
85 return "pc_hi20";
87 return "pc_lo12";
89 return "pc64_lo20";
91 return "pc64_hi12";
93 return "got_pc_hi20";
95 return "got_pc_lo12";
97 return "got64_pc_lo20";
99 return "got64_pc_hi12";
101 return "got_hi20";
103 return "got_lo12";
105 return "got64_lo20";
107 return "got64_hi12";
109 return "le_hi20";
111 return "le_lo12";
113 return "le64_lo20";
115 return "le64_hi12";
117 return "ie_pc_hi20";
119 return "ie_pc_lo12";
121 return "ie64_pc_lo20";
123 return "ie64_pc_hi12";
125 return "ie_hi20";
127 return "ie_lo12";
129 return "ie64_lo20";
131 return "ie64_hi12";
133 return "ld_pc_hi20";
135 return "ld_hi20";
137 return "gd_pc_hi20";
139 return "gd_hi20";
141 return "call36";
143 return "desc_pc_hi20";
145 return "desc_pc_lo12";
147 return "desc64_pc_lo20";
149 return "desc64_pc_hi12";
151 return "desc_hi20";
153 return "desc_lo12";
155 return "desc64_lo20";
157 return "desc64_hi12";
159 return "desc_ld";
161 return "desc_call";
163 return "le_hi20_r";
165 return "le_add_r";
167 return "le_lo12_r";
169 return "pcrel_20";
171 return "ld_pcrel_20";
173 return "gd_pcrel_20";
175 return "desc_pcrel_20";
176 }
177}
178
183 .Case("b16", VK_LoongArch_B16)
184 .Case("b21", VK_LoongArch_B21)
185 .Case("b26", VK_LoongArch_B26)
186 .Case("abs_hi20", VK_LoongArch_ABS_HI20)
187 .Case("abs_lo12", VK_LoongArch_ABS_LO12)
188 .Case("abs64_lo20", VK_LoongArch_ABS64_LO20)
189 .Case("abs64_hi12", VK_LoongArch_ABS64_HI12)
190 .Case("pc_hi20", VK_LoongArch_PCALA_HI20)
191 .Case("pc_lo12", VK_LoongArch_PCALA_LO12)
192 .Case("pc64_lo20", VK_LoongArch_PCALA64_LO20)
193 .Case("pc64_hi12", VK_LoongArch_PCALA64_HI12)
194 .Case("got_pc_hi20", VK_LoongArch_GOT_PC_HI20)
195 .Case("got_pc_lo12", VK_LoongArch_GOT_PC_LO12)
196 .Case("got64_pc_lo20", VK_LoongArch_GOT64_PC_LO20)
197 .Case("got64_pc_hi12", VK_LoongArch_GOT64_PC_HI12)
198 .Case("got_hi20", VK_LoongArch_GOT_HI20)
199 .Case("got_lo12", VK_LoongArch_GOT_LO12)
200 .Case("got64_lo20", VK_LoongArch_GOT64_LO20)
201 .Case("got64_hi12", VK_LoongArch_GOT64_HI12)
202 .Case("le_hi20", VK_LoongArch_TLS_LE_HI20)
203 .Case("le_lo12", VK_LoongArch_TLS_LE_LO12)
204 .Case("le64_lo20", VK_LoongArch_TLS_LE64_LO20)
205 .Case("le64_hi12", VK_LoongArch_TLS_LE64_HI12)
206 .Case("ie_pc_hi20", VK_LoongArch_TLS_IE_PC_HI20)
207 .Case("ie_pc_lo12", VK_LoongArch_TLS_IE_PC_LO12)
208 .Case("ie64_pc_lo20", VK_LoongArch_TLS_IE64_PC_LO20)
209 .Case("ie64_pc_hi12", VK_LoongArch_TLS_IE64_PC_HI12)
210 .Case("ie_hi20", VK_LoongArch_TLS_IE_HI20)
211 .Case("ie_lo12", VK_LoongArch_TLS_IE_LO12)
212 .Case("ie64_lo20", VK_LoongArch_TLS_IE64_LO20)
213 .Case("ie64_hi12", VK_LoongArch_TLS_IE64_HI12)
214 .Case("ld_pc_hi20", VK_LoongArch_TLS_LD_PC_HI20)
215 .Case("ld_hi20", VK_LoongArch_TLS_LD_HI20)
216 .Case("gd_pc_hi20", VK_LoongArch_TLS_GD_PC_HI20)
217 .Case("gd_hi20", VK_LoongArch_TLS_GD_HI20)
218 .Case("call36", VK_LoongArch_CALL36)
219 .Case("desc_pc_hi20", VK_LoongArch_TLS_DESC_PC_HI20)
220 .Case("desc_pc_lo12", VK_LoongArch_TLS_DESC_PC_LO12)
221 .Case("desc64_pc_lo20", VK_LoongArch_TLS_DESC64_PC_LO20)
222 .Case("desc64_pc_hi12", VK_LoongArch_TLS_DESC64_PC_HI12)
223 .Case("desc_hi20", VK_LoongArch_TLS_DESC_HI20)
224 .Case("desc_lo12", VK_LoongArch_TLS_DESC_LO12)
225 .Case("desc64_lo20", VK_LoongArch_TLS_DESC64_LO20)
226 .Case("desc64_hi12", VK_LoongArch_TLS_DESC64_HI12)
227 .Case("desc_ld", VK_LoongArch_TLS_DESC_LD)
228 .Case("desc_call", VK_LoongArch_TLS_DESC_CALL)
229 .Case("le_hi20_r", VK_LoongArch_TLS_LE_HI20_R)
230 .Case("le_add_r", VK_LoongArch_TLS_LE_ADD_R)
231 .Case("le_lo12_r", VK_LoongArch_TLS_LE_LO12_R)
232 .Case("pcrel_20", VK_LoongArch_PCREL20_S2)
233 .Case("ld_pcrel_20", VK_LoongArch_TLS_LD_PCREL20_S2)
234 .Case("gd_pcrel_20", VK_LoongArch_TLS_GD_PCREL20_S2)
235 .Case("desc_pcrel_20", VK_LoongArch_TLS_DESC_PCREL20_S2)
237}
238
239static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
240 switch (Expr->getKind()) {
241 case MCExpr::Target:
242 llvm_unreachable("Can't handle nested target expression");
243 break;
244 case MCExpr::Constant:
245 break;
246 case MCExpr::Unary:
247 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
248 break;
249 case MCExpr::Binary: {
250 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
253 break;
254 }
255 case MCExpr::SymbolRef: {
256 // We're known to be under a TLS fixup, so any symbol should be
257 // modified. There should be only one.
258 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
259 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
260 break;
261 }
262 }
263}
264
266 switch (getKind()) {
267 default:
268 return;
281 break;
282 }
284}
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
PowerPC TLS Dynamic Call Fixup
static const char * name
Definition: SMEABIPass.cpp:46
raw_pwrite_stream & OS
VariantKind getKind() const
const MCExpr * getSubExpr() const
static VariantKind getVariantKindForName(StringRef name)
void visitUsedExpr(MCStreamer &Streamer) const override
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
static const LoongArchMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx, bool Hint=false)
static StringRef getVariantKindName(VariantKind Kind)
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) const override
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
Binary assembler expressions.
Definition: MCExpr.h:493
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:640
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:643
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
@ Unary
Unary expressions.
Definition: MCExpr.h:40
@ Constant
Constant expressions.
Definition: MCExpr.h:38
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:39
@ Target
Target specific expression.
Definition: MCExpr.h:41
@ Binary
Binary expressions.
Definition: MCExpr.h:37
bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:819
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:40
ExprKind getKind() const
Definition: MCExpr.h:78
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Streaming machine code generation interface.
Definition: MCStreamer.h:213
void visitUsedExpr(const MCExpr &Expr)
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
const MCSymbol & getSymbol() const
Definition: MCExpr.h:411
This represents an "assembler immediate".
Definition: MCValue.h:36
int64_t getConstant() const
Definition: MCValue.h:43
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:59
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:45
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
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
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.
@ STT_TLS
Definition: ELF.h:1359
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18