1 //===-- RISCVMCExpr.cpp - RISC-V specific MC expression classes -----------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains the implementation of the assembly expression modifiers
10 // accepted by the RISC-V architecture (e.g. ":lo12:", ":gottprel_g1:", ...).
12 //===----------------------------------------------------------------------===//
14 #include "RISCVMCExpr.h"
15 #include "MCTargetDesc/RISCVAsmBackend.h"
16 #include "RISCVFixupKinds.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbolELF.h"
23 #include "llvm/MC/MCValue.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/ErrorHandling.h"
29 #define DEBUG_TYPE "riscvmcexpr"
31 const RISCVMCExpr
*RISCVMCExpr::create(const MCExpr
*Expr
, VariantKind Kind
,
33 return new (Ctx
) RISCVMCExpr(Expr
, Kind
);
36 void RISCVMCExpr::printImpl(raw_ostream
&OS
, const MCAsmInfo
*MAI
) const {
37 VariantKind Kind
= getKind();
38 bool HasVariant
= ((Kind
!= VK_RISCV_None
) && (Kind
!= VK_RISCV_CALL
) &&
39 (Kind
!= VK_RISCV_CALL_PLT
));
42 OS
<< '%' << getVariantKindName(getKind()) << '(';
44 if (Kind
== VK_RISCV_CALL_PLT
)
50 const MCFixup
*RISCVMCExpr::getPCRelHiFixup(const MCFragment
**DFOut
) const {
52 if (!getSubExpr()->evaluateAsRelocatable(AUIPCLoc
, nullptr, nullptr))
55 const MCSymbolRefExpr
*AUIPCSRE
= AUIPCLoc
.getSymA();
59 const MCSymbol
*AUIPCSymbol
= &AUIPCSRE
->getSymbol();
60 const auto *DF
= dyn_cast_or_null
<MCDataFragment
>(AUIPCSymbol
->getFragment());
65 uint64_t Offset
= AUIPCSymbol
->getOffset();
66 if (DF
->getContents().size() == Offset
) {
67 DF
= dyn_cast_or_null
<MCDataFragment
>(DF
->getNextNode());
73 for (const MCFixup
&F
: DF
->getFixups()) {
74 if (F
.getOffset() != Offset
)
77 switch ((unsigned)F
.getKind()) {
80 case RISCV::fixup_riscv_got_hi20
:
81 case RISCV::fixup_riscv_tls_got_hi20
:
82 case RISCV::fixup_riscv_tls_gd_hi20
:
83 case RISCV::fixup_riscv_pcrel_hi20
:
93 bool RISCVMCExpr::evaluateAsRelocatableImpl(MCValue
&Res
,
94 const MCAsmLayout
*Layout
,
95 const MCFixup
*Fixup
) const {
96 // Explicitly drop the layout and assembler to prevent any symbolic folding in
97 // the expression handling. This is required to preserve symbolic difference
98 // expressions to emit the paired relocations.
99 if (!getSubExpr()->evaluateAsRelocatable(Res
, nullptr, nullptr))
103 MCValue::get(Res
.getSymA(), Res
.getSymB(), Res
.getConstant(), getKind());
104 // Custom fixup types are not valid with symbol difference expressions.
105 return Res
.getSymB() ? getKind() == VK_RISCV_None
: true;
108 void RISCVMCExpr::visitUsedExpr(MCStreamer
&Streamer
) const {
109 Streamer
.visitUsedExpr(*getSubExpr());
112 RISCVMCExpr::VariantKind
RISCVMCExpr::getVariantKindForName(StringRef name
) {
113 return StringSwitch
<RISCVMCExpr::VariantKind
>(name
)
114 .Case("lo", VK_RISCV_LO
)
115 .Case("hi", VK_RISCV_HI
)
116 .Case("pcrel_lo", VK_RISCV_PCREL_LO
)
117 .Case("pcrel_hi", VK_RISCV_PCREL_HI
)
118 .Case("got_pcrel_hi", VK_RISCV_GOT_HI
)
119 .Case("tprel_lo", VK_RISCV_TPREL_LO
)
120 .Case("tprel_hi", VK_RISCV_TPREL_HI
)
121 .Case("tprel_add", VK_RISCV_TPREL_ADD
)
122 .Case("tls_ie_pcrel_hi", VK_RISCV_TLS_GOT_HI
)
123 .Case("tls_gd_pcrel_hi", VK_RISCV_TLS_GD_HI
)
124 .Default(VK_RISCV_Invalid
);
127 StringRef
RISCVMCExpr::getVariantKindName(VariantKind Kind
) {
129 case VK_RISCV_Invalid
:
131 llvm_unreachable("Invalid ELF symbol kind");
136 case VK_RISCV_PCREL_LO
:
138 case VK_RISCV_PCREL_HI
:
140 case VK_RISCV_GOT_HI
:
141 return "got_pcrel_hi";
142 case VK_RISCV_TPREL_LO
:
144 case VK_RISCV_TPREL_HI
:
146 case VK_RISCV_TPREL_ADD
:
148 case VK_RISCV_TLS_GOT_HI
:
149 return "tls_ie_pcrel_hi";
150 case VK_RISCV_TLS_GD_HI
:
151 return "tls_gd_pcrel_hi";
154 case VK_RISCV_CALL_PLT
:
156 case VK_RISCV_32_PCREL
:
159 llvm_unreachable("Invalid ELF symbol kind");
162 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr
*Expr
, MCAssembler
&Asm
) {
163 switch (Expr
->getKind()) {
165 llvm_unreachable("Can't handle nested target expression");
167 case MCExpr::Constant
:
170 case MCExpr::Binary
: {
171 const MCBinaryExpr
*BE
= cast
<MCBinaryExpr
>(Expr
);
172 fixELFSymbolsInTLSFixupsImpl(BE
->getLHS(), Asm
);
173 fixELFSymbolsInTLSFixupsImpl(BE
->getRHS(), Asm
);
177 case MCExpr::SymbolRef
: {
178 // We're known to be under a TLS fixup, so any symbol should be
179 // modified. There should be only one.
180 const MCSymbolRefExpr
&SymRef
= *cast
<MCSymbolRefExpr
>(Expr
);
181 cast
<MCSymbolELF
>(SymRef
.getSymbol()).setType(ELF::STT_TLS
);
186 fixELFSymbolsInTLSFixupsImpl(cast
<MCUnaryExpr
>(Expr
)->getSubExpr(), Asm
);
191 void RISCVMCExpr::fixELFSymbolsInTLSFixups(MCAssembler
&Asm
) const {
195 case VK_RISCV_TPREL_HI
:
196 case VK_RISCV_TLS_GOT_HI
:
197 case VK_RISCV_TLS_GD_HI
:
201 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm
);
204 bool RISCVMCExpr::evaluateAsConstant(int64_t &Res
) const {
207 if (Kind
== VK_RISCV_PCREL_HI
|| Kind
== VK_RISCV_PCREL_LO
||
208 Kind
== VK_RISCV_GOT_HI
|| Kind
== VK_RISCV_TPREL_HI
||
209 Kind
== VK_RISCV_TPREL_LO
|| Kind
== VK_RISCV_TPREL_ADD
||
210 Kind
== VK_RISCV_TLS_GOT_HI
|| Kind
== VK_RISCV_TLS_GD_HI
||
211 Kind
== VK_RISCV_CALL
|| Kind
== VK_RISCV_CALL_PLT
)
214 if (!getSubExpr()->evaluateAsRelocatable(Value
, nullptr, nullptr))
217 if (!Value
.isAbsolute())
220 Res
= evaluateAsInt64(Value
.getConstant());
224 int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value
) const {
227 llvm_unreachable("Invalid kind");
229 return SignExtend64
<12>(Value
);
231 // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
232 return ((Value
+ 0x800) >> 12) & 0xfffff;