1 //===-- RISCVMCExpr.cpp - RISCV specific MC expression classes ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the implementation of the assembly expression modifiers
11 // accepted by the RISCV architecture (e.g. ":lo12:", ":gottprel_g1:", ...).
13 //===----------------------------------------------------------------------===//
16 #include "RISCVMCExpr.h"
17 #include "RISCVFixupKinds.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/MC/MCSymbolELF.h"
22 #include "llvm/MC/MCValue.h"
23 #include "llvm/Support/ErrorHandling.h"
27 #define DEBUG_TYPE "riscvmcexpr"
29 const RISCVMCExpr *RISCVMCExpr::create(const MCExpr *Expr, VariantKind Kind,
31 return new (Ctx) RISCVMCExpr(Expr, Kind);
34 void RISCVMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
36 ((getKind() != VK_RISCV_None) && (getKind() != VK_RISCV_CALL));
38 OS << '%' << getVariantKindName(getKind()) << '(';
44 const MCFixup *RISCVMCExpr::getPCRelHiFixup() const {
46 if (!getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr, nullptr))
49 const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
54 dyn_cast_or_null<MCDataFragment>(AUIPCSRE->findAssociatedFragment());
58 const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
59 for (const MCFixup &F : DF->getFixups()) {
60 if (F.getOffset() != AUIPCSymbol->getOffset())
63 switch ((unsigned)F.getKind()) {
66 case RISCV::fixup_riscv_pcrel_hi20:
74 bool RISCVMCExpr::evaluatePCRelLo(MCValue &Res, const MCAsmLayout *Layout,
75 const MCFixup *Fixup) const {
76 // VK_RISCV_PCREL_LO has to be handled specially. The MCExpr inside is
77 // actually the location of a auipc instruction with a VK_RISCV_PCREL_HI fixup
78 // pointing to the real target. We need to generate an MCValue in the form of
79 // (<real target> + <offset from this fixup to the auipc fixup>). The Fixup
80 // is pcrel relative to the VK_RISCV_PCREL_LO fixup, so we need to add the
81 // offset to the VK_RISCV_PCREL_HI Fixup from VK_RISCV_PCREL_LO to correct.
83 if (!getSubExpr()->evaluateAsValue(AUIPCLoc, *Layout))
86 const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
87 // Don't try to evaluate %pcrel_hi/%pcrel_lo pairs that cross fragment
90 findAssociatedFragment() != AUIPCSRE->findAssociatedFragment())
93 const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
97 const MCFixup *TargetFixup = getPCRelHiFixup();
101 if ((unsigned)TargetFixup->getKind() != RISCV::fixup_riscv_pcrel_hi20)
105 if (!TargetFixup->getValue()->evaluateAsValue(Target, *Layout))
108 if (!Target.getSymA() || !Target.getSymA()->getSymbol().isInSection())
111 if (&Target.getSymA()->getSymbol().getSection() !=
112 findAssociatedFragment()->getParent())
115 uint64_t AUIPCOffset = AUIPCSymbol->getOffset();
117 Res = MCValue::get(Target.getSymA(), nullptr,
118 Target.getConstant() + (Fixup->getOffset() - AUIPCOffset));
122 bool RISCVMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
123 const MCAsmLayout *Layout,
124 const MCFixup *Fixup) const {
125 if (Kind == VK_RISCV_PCREL_LO && evaluatePCRelLo(Res, Layout, Fixup))
128 if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
131 // Some custom fixup types are not valid with symbol difference expressions
132 if (Res.getSymA() && Res.getSymB()) {
138 case VK_RISCV_PCREL_LO:
139 case VK_RISCV_PCREL_HI:
147 void RISCVMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
148 Streamer.visitUsedExpr(*getSubExpr());
151 RISCVMCExpr::VariantKind RISCVMCExpr::getVariantKindForName(StringRef name) {
152 return StringSwitch<RISCVMCExpr::VariantKind>(name)
153 .Case("lo", VK_RISCV_LO)
154 .Case("hi", VK_RISCV_HI)
155 .Case("pcrel_lo", VK_RISCV_PCREL_LO)
156 .Case("pcrel_hi", VK_RISCV_PCREL_HI)
157 .Default(VK_RISCV_Invalid);
160 StringRef RISCVMCExpr::getVariantKindName(VariantKind Kind) {
163 llvm_unreachable("Invalid ELF symbol kind");
168 case VK_RISCV_PCREL_LO:
170 case VK_RISCV_PCREL_HI:
175 bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
178 if (Kind == VK_RISCV_PCREL_HI || Kind == VK_RISCV_PCREL_LO ||
179 Kind == VK_RISCV_CALL)
182 if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr))
185 if (!Value.isAbsolute())
188 Res = evaluateAsInt64(Value.getConstant());
192 int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value) const {
195 llvm_unreachable("Invalid kind");
197 return SignExtend64<12>(Value);
199 // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
200 return ((Value + 0x800) >> 12) & 0xfffff;