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/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCObjectStreamer.h"
15 #include "llvm/MC/MCSymbolELF.h"
16 #include "llvm/Support/ELF.h"
20 #define DEBUG_TYPE "mipsmcexpr"
22 const MipsMCExpr *MipsMCExpr::create(MipsMCExpr::MipsExprKind Kind,
23 const MCExpr *Expr, MCContext &Ctx) {
24 return new (Ctx) MipsMCExpr(Kind, Expr);
27 const MipsMCExpr *MipsMCExpr::createGpOff(MipsMCExpr::MipsExprKind Kind,
28 const MCExpr *Expr, MCContext &Ctx) {
29 return create(Kind, create(MEK_NEG, create(MEK_GPREL, Expr, Ctx), Ctx), Ctx);
32 void MipsMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
38 llvm_unreachable("MEK_None and MEK_Special are invalid");
115 if (Expr->evaluateAsAbsolute(AbsVal))
118 Expr->print(OS, MAI, true);
123 MipsMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
124 const MCAsmLayout *Layout,
125 const MCFixup *Fixup) const {
126 // Look for the %hi(%neg(%gp_rel(X))) and %lo(%neg(%gp_rel(X))) special cases.
128 const MCExpr *SubExpr =
129 cast<MipsMCExpr>(cast<MipsMCExpr>(getSubExpr())->getSubExpr())
131 if (!SubExpr->evaluateAsRelocatable(Res, Layout, Fixup))
134 Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(),
139 if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
142 if (Res.getRefKind() != MCSymbolRefExpr::VK_None)
145 // evaluateAsAbsolute() and evaluateAsValue() require that we evaluate the
146 // %hi/%lo/etc. here. Fixup is a null pointer when either of these is the
148 if (Res.isAbsolute() && Fixup == nullptr) {
149 int64_t AbsVal = Res.getConstant();
153 llvm_unreachable("MEK_None and MEK_Special are invalid");
174 AbsVal = SignExtend64<16>(AbsVal);
178 AbsVal = SignExtend64<16>((AbsVal + 0x8000) >> 16);
181 AbsVal = SignExtend64<16>((AbsVal + 0x80008000LL) >> 32);
184 AbsVal = SignExtend64<16>((AbsVal + 0x800080008000LL) >> 48);
190 Res = MCValue::get(AbsVal);
194 // We want to defer it for relocatable expressions since the constant is
195 // applied to the whole symbol value.
197 // The value of getKind() that is given to MCValue is only intended to aid
198 // debugging when inspecting MCValue objects. It shouldn't be relied upon
199 // for decision making.
200 Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
205 void MipsMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
206 Streamer.visitUsedExpr(*getSubExpr());
209 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
210 switch (Expr->getKind()) {
212 fixELFSymbolsInTLSFixupsImpl(cast<MipsMCExpr>(Expr)->getSubExpr(), Asm);
214 case MCExpr::Constant:
216 case MCExpr::Binary: {
217 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
218 fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
219 fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
222 case MCExpr::SymbolRef: {
223 // We're known to be under a TLS fixup, so any symbol should be
224 // modified. There should be only one.
225 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
226 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
230 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
235 void MipsMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
239 llvm_unreachable("MEK_None and MEK_Special are invalid");
261 // If we do have nested target-specific expressions, they will be in
262 // a consecutive chain.
263 if (const MipsMCExpr *E = dyn_cast<const MipsMCExpr>(getSubExpr()))
264 E->fixELFSymbolsInTLSFixups(Asm);
270 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
275 bool MipsMCExpr::isGpOff(MipsExprKind &Kind) const {
276 if (getKind() == MEK_HI || getKind() == MEK_LO) {
277 if (const MipsMCExpr *S1 = dyn_cast<const MipsMCExpr>(getSubExpr())) {
278 if (const MipsMCExpr *S2 = dyn_cast<const MipsMCExpr>(S1->getSubExpr())) {
279 if (S1->getKind() == MEK_NEG && S2->getKind() == MEK_GPREL) {