1 //===-- MipsMCExpr.cpp - Mips 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 #include "MipsMCExpr.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCStreamer.h"
16 #include "llvm/MC/MCSymbolELF.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Support/Casting.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/MathExtras.h"
21 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "mipsmcexpr"
28 const MipsMCExpr *MipsMCExpr::create(MipsMCExpr::MipsExprKind Kind,
29 const MCExpr *Expr, MCContext &Ctx) {
30 return new (Ctx) MipsMCExpr(Kind, Expr);
33 const MipsMCExpr *MipsMCExpr::createGpOff(MipsMCExpr::MipsExprKind Kind,
34 const MCExpr *Expr, MCContext &Ctx) {
35 return create(Kind, create(MEK_NEG, create(MEK_GPREL, Expr, Ctx), Ctx), Ctx);
38 void MipsMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
44 llvm_unreachable("MEK_None and MEK_Special are invalid");
47 llvm_unreachable("MEK_DTPREL is used for TLS DIEExpr only");
124 if (Expr->evaluateAsAbsolute(AbsVal))
127 Expr->print(OS, MAI, true);
132 MipsMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
133 const MCAsmLayout *Layout,
134 const MCFixup *Fixup) const {
135 // Look for the %hi(%neg(%gp_rel(X))) and %lo(%neg(%gp_rel(X))) special cases.
137 const MCExpr *SubExpr =
138 cast<MipsMCExpr>(cast<MipsMCExpr>(getSubExpr())->getSubExpr())
140 if (!SubExpr->evaluateAsRelocatable(Res, Layout, Fixup))
143 Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(),
148 if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
151 if (Res.getRefKind() != MCSymbolRefExpr::VK_None)
154 // evaluateAsAbsolute() and evaluateAsValue() require that we evaluate the
155 // %hi/%lo/etc. here. Fixup is a null pointer when either of these is the
157 if (Res.isAbsolute() && Fixup == nullptr) {
158 int64_t AbsVal = Res.getConstant();
162 llvm_unreachable("MEK_None and MEK_Special are invalid");
164 llvm_unreachable("MEK_DTPREL is used for TLS DIEExpr only");
185 AbsVal = SignExtend64<16>(AbsVal);
189 AbsVal = SignExtend64<16>((AbsVal + 0x8000) >> 16);
192 AbsVal = SignExtend64<16>((AbsVal + 0x80008000LL) >> 32);
195 AbsVal = SignExtend64<16>((AbsVal + 0x800080008000LL) >> 48);
201 Res = MCValue::get(AbsVal);
205 // We want to defer it for relocatable expressions since the constant is
206 // applied to the whole symbol value.
208 // The value of getKind() that is given to MCValue is only intended to aid
209 // debugging when inspecting MCValue objects. It shouldn't be relied upon
210 // for decision making.
211 Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
216 void MipsMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
217 Streamer.visitUsedExpr(*getSubExpr());
220 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
221 switch (Expr->getKind()) {
223 fixELFSymbolsInTLSFixupsImpl(cast<MipsMCExpr>(Expr)->getSubExpr(), Asm);
225 case MCExpr::Constant:
227 case MCExpr::Binary: {
228 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
229 fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
230 fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
233 case MCExpr::SymbolRef: {
234 // We're known to be under a TLS fixup, so any symbol should be
235 // modified. There should be only one.
236 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
237 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
241 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
246 void MipsMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
250 llvm_unreachable("MEK_None and MEK_Special are invalid");
253 llvm_unreachable("MEK_DTPREL is used for TLS DIEExpr only");
272 // If we do have nested target-specific expressions, they will be in
273 // a consecutive chain.
274 if (const MipsMCExpr *E = dyn_cast<const MipsMCExpr>(getSubExpr()))
275 E->fixELFSymbolsInTLSFixups(Asm);
284 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
289 bool MipsMCExpr::isGpOff(MipsExprKind &Kind) const {
290 if (getKind() == MEK_HI || getKind() == MEK_LO) {
291 if (const MipsMCExpr *S1 = dyn_cast<const MipsMCExpr>(getSubExpr())) {
292 if (const MipsMCExpr *S2 = dyn_cast<const MipsMCExpr>(S1->getSubExpr())) {
293 if (S1->getKind() == MEK_NEG && S2->getKind() == MEK_GPREL) {