LLVM 19.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 "LoongArchFixupKinds.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCSymbolELF.h"
20#include "llvm/MC/MCValue.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "loongarch-mcexpr"
27
29 VariantKind Kind, MCContext &Ctx,
30 bool Hint) {
31 return new (Ctx) LoongArchMCExpr(Expr, Kind, Hint);
32}
33
35 VariantKind Kind = getKind();
36 bool HasVariant =
37 ((Kind != VK_LoongArch_None) && (Kind != VK_LoongArch_CALL));
38
39 if (HasVariant)
40 OS << '%' << getVariantKindName(getKind()) << '(';
41 Expr->print(OS, MAI);
42 if (HasVariant)
43 OS << ')';
44}
45
47 const MCAssembler *Asm,
48 const MCFixup *Fixup) const {
49 // Explicitly drop the layout and assembler to prevent any symbolic folding in
50 // the expression handling. This is required to preserve symbolic difference
51 // expressions to emit the paired relocations.
52 if (!getSubExpr()->evaluateAsRelocatable(Res, nullptr, nullptr))
53 return false;
54
55 Res =
56 MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
57 // Custom fixup types are not valid with symbol difference expressions.
58 return Res.getSymB() ? getKind() == VK_LoongArch_None : true;
59}
60
62 Streamer.visitUsedExpr(*getSubExpr());
63}
64
66 switch (Kind) {
67 default:
68 llvm_unreachable("Invalid ELF symbol kind");
70 return "plt";
72 return "b16";
74 return "b21";
76 return "b26";
78 return "abs_hi20";
80 return "abs_lo12";
82 return "abs64_lo20";
84 return "abs64_hi12";
86 return "pc_hi20";
88 return "pc_lo12";
90 return "pc64_lo20";
92 return "pc64_hi12";
94 return "got_pc_hi20";
96 return "got_pc_lo12";
98 return "got64_pc_lo20";
100 return "got64_pc_hi12";
102 return "got_hi20";
104 return "got_lo12";
106 return "got64_lo20";
108 return "got64_hi12";
110 return "le_hi20";
112 return "le_lo12";
114 return "le64_lo20";
116 return "le64_hi12";
118 return "ie_pc_hi20";
120 return "ie_pc_lo12";
122 return "ie64_pc_lo20";
124 return "ie64_pc_hi12";
126 return "ie_hi20";
128 return "ie_lo12";
130 return "ie64_lo20";
132 return "ie64_hi12";
134 return "ld_pc_hi20";
136 return "ld_hi20";
138 return "gd_pc_hi20";
140 return "gd_hi20";
142 return "call36";
144 return "desc_pc_hi20";
146 return "desc_pc_lo12";
148 return "desc64_pc_lo20";
150 return "desc64_pc_hi12";
152 return "desc_hi20";
154 return "desc_lo12";
156 return "desc64_lo20";
158 return "desc64_hi12";
160 return "desc_ld";
162 return "desc_call";
163 }
164}
165
170 .Case("b16", VK_LoongArch_B16)
171 .Case("b21", VK_LoongArch_B21)
172 .Case("b26", VK_LoongArch_B26)
173 .Case("abs_hi20", VK_LoongArch_ABS_HI20)
174 .Case("abs_lo12", VK_LoongArch_ABS_LO12)
175 .Case("abs64_lo20", VK_LoongArch_ABS64_LO20)
176 .Case("abs64_hi12", VK_LoongArch_ABS64_HI12)
177 .Case("pc_hi20", VK_LoongArch_PCALA_HI20)
178 .Case("pc_lo12", VK_LoongArch_PCALA_LO12)
179 .Case("pc64_lo20", VK_LoongArch_PCALA64_LO20)
180 .Case("pc64_hi12", VK_LoongArch_PCALA64_HI12)
181 .Case("got_pc_hi20", VK_LoongArch_GOT_PC_HI20)
182 .Case("got_pc_lo12", VK_LoongArch_GOT_PC_LO12)
183 .Case("got64_pc_lo20", VK_LoongArch_GOT64_PC_LO20)
184 .Case("got64_pc_hi12", VK_LoongArch_GOT64_PC_HI12)
185 .Case("got_hi20", VK_LoongArch_GOT_HI20)
186 .Case("got_lo12", VK_LoongArch_GOT_LO12)
187 .Case("got64_lo20", VK_LoongArch_GOT64_LO20)
188 .Case("got64_hi12", VK_LoongArch_GOT64_HI12)
189 .Case("le_hi20", VK_LoongArch_TLS_LE_HI20)
190 .Case("le_lo12", VK_LoongArch_TLS_LE_LO12)
191 .Case("le64_lo20", VK_LoongArch_TLS_LE64_LO20)
192 .Case("le64_hi12", VK_LoongArch_TLS_LE64_HI12)
193 .Case("ie_pc_hi20", VK_LoongArch_TLS_IE_PC_HI20)
194 .Case("ie_pc_lo12", VK_LoongArch_TLS_IE_PC_LO12)
195 .Case("ie64_pc_lo20", VK_LoongArch_TLS_IE64_PC_LO20)
196 .Case("ie64_pc_hi12", VK_LoongArch_TLS_IE64_PC_HI12)
197 .Case("ie_hi20", VK_LoongArch_TLS_IE_HI20)
198 .Case("ie_lo12", VK_LoongArch_TLS_IE_LO12)
199 .Case("ie64_lo20", VK_LoongArch_TLS_IE64_LO20)
200 .Case("ie64_hi12", VK_LoongArch_TLS_IE64_HI12)
201 .Case("ld_pc_hi20", VK_LoongArch_TLS_LD_PC_HI20)
202 .Case("ld_hi20", VK_LoongArch_TLS_LD_HI20)
203 .Case("gd_pc_hi20", VK_LoongArch_TLS_GD_PC_HI20)
204 .Case("gd_hi20", VK_LoongArch_TLS_GD_HI20)
205 .Case("call36", VK_LoongArch_CALL36)
206 .Case("desc_pc_hi20", VK_LoongArch_TLS_DESC_PC_HI20)
207 .Case("desc_pc_lo12", VK_LoongArch_TLS_DESC_PC_LO12)
208 .Case("desc64_pc_lo20", VK_LoongArch_TLS_DESC64_PC_LO20)
209 .Case("desc64_pc_hi12", VK_LoongArch_TLS_DESC64_PC_HI12)
210 .Case("desc_hi20", VK_LoongArch_TLS_DESC_HI20)
211 .Case("desc_lo12", VK_LoongArch_TLS_DESC_LO12)
212 .Case("desc64_lo20", VK_LoongArch_TLS_DESC64_LO20)
213 .Case("desc64_hi12", VK_LoongArch_TLS_DESC64_HI12)
214 .Case("desc_ld", VK_LoongArch_TLS_DESC_LD)
215 .Case("desc_call", VK_LoongArch_TLS_DESC_CALL)
217}
218
219static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
220 switch (Expr->getKind()) {
221 case MCExpr::Target:
222 llvm_unreachable("Can't handle nested target expression");
223 break;
224 case MCExpr::Constant:
225 break;
226 case MCExpr::Unary:
227 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
228 break;
229 case MCExpr::Binary: {
230 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
233 break;
234 }
235 case MCExpr::SymbolRef: {
236 // We're known to be under a TLS fixup, so any symbol should be
237 // modified. There should be only one.
238 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
239 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
240 break;
241 }
242 }
243}
244
246 switch (getKind()) {
247 default:
248 return;
258 break;
259 }
261}
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
PowerPC TLS Dynamic Call Fixup
static const char * name
Definition: SMEABIPass.cpp:50
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:488
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:635
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:638
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:789
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:188
const MCSymbol & getSymbol() const
Definition: MCExpr.h:406
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: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
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:1336
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18