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 // MEK_DTPREL is used for marking TLS DIEExpr only
48 // and contains a regular sub-expression.
49 getSubExpr()->print(OS, MAI, true);
126 if (Expr->evaluateAsAbsolute(AbsVal))
129 Expr->print(OS, MAI, true);
134 MipsMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
135 const MCAsmLayout *Layout,
136 const MCFixup *Fixup) const {
137 // Look for the %hi(%neg(%gp_rel(X))) and %lo(%neg(%gp_rel(X))) special cases.
139 const MCExpr *SubExpr =
140 cast<MipsMCExpr>(cast<MipsMCExpr>(getSubExpr())->getSubExpr())
142 if (!SubExpr->evaluateAsRelocatable(Res, Layout, Fixup))
145 Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(),
150 if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
153 if (Res.getRefKind() != MCSymbolRefExpr::VK_None)
156 // evaluateAsAbsolute() and evaluateAsValue() require that we evaluate the
157 // %hi/%lo/etc. here. Fixup is a null pointer when either of these is the
159 if (Res.isAbsolute() && Fixup == nullptr) {
160 int64_t AbsVal = Res.getConstant();
164 llvm_unreachable("MEK_None and MEK_Special are invalid");
166 // MEK_DTPREL is used for marking TLS DIEExpr only
167 // and contains a regular sub-expression.
168 return getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup);
189 AbsVal = SignExtend64<16>(AbsVal);
193 AbsVal = SignExtend64<16>((AbsVal + 0x8000) >> 16);
196 AbsVal = SignExtend64<16>((AbsVal + 0x80008000LL) >> 32);
199 AbsVal = SignExtend64<16>((AbsVal + 0x800080008000LL) >> 48);
205 Res = MCValue::get(AbsVal);
209 // We want to defer it for relocatable expressions since the constant is
210 // applied to the whole symbol value.
212 // The value of getKind() that is given to MCValue is only intended to aid
213 // debugging when inspecting MCValue objects. It shouldn't be relied upon
214 // for decision making.
215 Res = MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
220 void MipsMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
221 Streamer.visitUsedExpr(*getSubExpr());
224 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
225 switch (Expr->getKind()) {
227 fixELFSymbolsInTLSFixupsImpl(cast<MipsMCExpr>(Expr)->getSubExpr(), Asm);
229 case MCExpr::Constant:
231 case MCExpr::Binary: {
232 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
233 fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
234 fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
237 case MCExpr::SymbolRef: {
238 // We're known to be under a TLS fixup, so any symbol should be
239 // modified. There should be only one.
240 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
241 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
245 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
250 void MipsMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
254 llvm_unreachable("MEK_None and MEK_Special are invalid");
273 // If we do have nested target-specific expressions, they will be in
274 // a consecutive chain.
275 if (const MipsMCExpr *E = dyn_cast<const MipsMCExpr>(getSubExpr()))
276 E->fixELFSymbolsInTLSFixups(Asm);
286 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
291 bool MipsMCExpr::isGpOff(MipsExprKind &Kind) const {
292 if (getKind() == MEK_HI || getKind() == MEK_LO) {
293 if (const MipsMCExpr *S1 = dyn_cast<const MipsMCExpr>(getSubExpr())) {
294 if (const MipsMCExpr *S2 = dyn_cast<const MipsMCExpr>(S1->getSubExpr())) {
295 if (S1->getKind() == MEK_NEG && S2->getKind() == MEK_GPREL) {