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");
121 if (Expr->evaluateAsAbsolute(AbsVal))
124 Expr->print(OS, MAI, true);
129 MipsMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
130 const MCAsmLayout *Layout,
131 const MCFixup *Fixup) const {
132 // Look for the %hi(%neg(%gp_rel(X))) and %lo(%neg(%gp_rel(X))) special cases.
134 const MCExpr *SubExpr =
135 cast<MipsMCExpr>(cast<MipsMCExpr>(getSubExpr())->getSubExpr())
137 if (!SubExpr->evaluateAsRelocatable(Res, Layout, Fixup))
140 Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(),
145 if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
148 if (Res.getRefKind() != MCSymbolRefExpr::VK_None)
151 // evaluateAsAbsolute() and evaluateAsValue() require that we evaluate the
152 // %hi/%lo/etc. here. Fixup is a null pointer when either of these is the
154 if (Res.isAbsolute() && Fixup == nullptr) {
155 int64_t AbsVal = Res.getConstant();
159 llvm_unreachable("MEK_None and MEK_Special are invalid");
180 AbsVal = SignExtend64<16>(AbsVal);
184 AbsVal = SignExtend64<16>((AbsVal + 0x8000) >> 16);
187 AbsVal = SignExtend64<16>((AbsVal + 0x80008000LL) >> 32);
190 AbsVal = SignExtend64<16>((AbsVal + 0x800080008000LL) >> 48);
196 Res = MCValue::get(AbsVal);
200 // We want to defer it for relocatable expressions since the constant is
201 // applied to the whole symbol value.
203 // The value of getKind() that is given to MCValue is only intended to aid
204 // debugging when inspecting MCValue objects. It shouldn't be relied upon
205 // for decision making.
206 Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
211 void MipsMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
212 Streamer.visitUsedExpr(*getSubExpr());
215 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
216 switch (Expr->getKind()) {
218 fixELFSymbolsInTLSFixupsImpl(cast<MipsMCExpr>(Expr)->getSubExpr(), Asm);
220 case MCExpr::Constant:
222 case MCExpr::Binary: {
223 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
224 fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
225 fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
228 case MCExpr::SymbolRef: {
229 // We're known to be under a TLS fixup, so any symbol should be
230 // modified. There should be only one.
231 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
232 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
236 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
241 void MipsMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
245 llvm_unreachable("MEK_None and MEK_Special are invalid");
264 // If we do have nested target-specific expressions, they will be in
265 // a consecutive chain.
266 if (const MipsMCExpr *E = dyn_cast<const MipsMCExpr>(getSubExpr()))
267 E->fixELFSymbolsInTLSFixups(Asm);
276 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
281 bool MipsMCExpr::isGpOff(MipsExprKind &Kind) const {
282 if (getKind() == MEK_HI || getKind() == MEK_LO) {
283 if (const MipsMCExpr *S1 = dyn_cast<const MipsMCExpr>(getSubExpr())) {
284 if (const MipsMCExpr *S2 = dyn_cast<const MipsMCExpr>(S1->getSubExpr())) {
285 if (S1->getKind() == MEK_NEG && S2->getKind() == MEK_GPREL) {