2014-02-04 18:41:57 +00:00
|
|
|
//===-- MipsMCExpr.cpp - Mips specific MC expression classes --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "mipsmcexpr"
|
|
|
|
#include "MipsMCExpr.h"
|
2014-03-04 10:07:28 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2014-02-04 18:41:57 +00:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-03 10:37:45 +00:00
|
|
|
bool MipsMCExpr::isSupportedBinaryExpr(MCSymbolRefExpr::VariantKind VK,
|
|
|
|
const MCBinaryExpr *BE) {
|
|
|
|
switch (VK) {
|
|
|
|
case MCSymbolRefExpr::VK_Mips_ABS_LO:
|
|
|
|
case MCSymbolRefExpr::VK_Mips_ABS_HI:
|
|
|
|
case MCSymbolRefExpr::VK_Mips_HIGHER:
|
|
|
|
case MCSymbolRefExpr::VK_Mips_HIGHEST:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We support expressions of the form "(sym1 binop1 sym2) binop2 const",
|
|
|
|
// where "binop2 const" is optional.
|
|
|
|
if (isa<MCBinaryExpr>(BE->getLHS())) {
|
|
|
|
if (!isa<MCConstantExpr>(BE->getRHS()))
|
|
|
|
return false;
|
|
|
|
BE = cast<MCBinaryExpr>(BE->getLHS());
|
|
|
|
}
|
|
|
|
return (isa<MCSymbolRefExpr>(BE->getLHS())
|
|
|
|
&& isa<MCSymbolRefExpr>(BE->getRHS()));
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:41:57 +00:00
|
|
|
const MipsMCExpr*
|
2014-04-03 10:37:45 +00:00
|
|
|
MipsMCExpr::Create(MCSymbolRefExpr::VariantKind VK, const MCExpr *Expr,
|
2014-02-04 18:41:57 +00:00
|
|
|
MCContext &Ctx) {
|
2014-04-03 10:37:45 +00:00
|
|
|
VariantKind Kind;
|
|
|
|
switch (VK) {
|
|
|
|
case MCSymbolRefExpr::VK_Mips_ABS_LO:
|
|
|
|
Kind = VK_Mips_LO;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_ABS_HI:
|
|
|
|
Kind = VK_Mips_HI;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_HIGHER:
|
|
|
|
Kind = VK_Mips_HIGHER;
|
|
|
|
break;
|
|
|
|
case MCSymbolRefExpr::VK_Mips_HIGHEST:
|
|
|
|
Kind = VK_Mips_HIGHEST;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Invalid kind!");
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:41:57 +00:00
|
|
|
return new (Ctx) MipsMCExpr(Kind, Expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MipsMCExpr::PrintImpl(raw_ostream &OS) const {
|
|
|
|
switch (Kind) {
|
|
|
|
default: llvm_unreachable("Invalid kind!");
|
2014-04-03 10:37:45 +00:00
|
|
|
case VK_Mips_LO: OS << "%lo"; break;
|
|
|
|
case VK_Mips_HI: OS << "%hi"; break;
|
|
|
|
case VK_Mips_HIGHER: OS << "%higher"; break;
|
|
|
|
case VK_Mips_HIGHEST: OS << "%highest"; break;
|
2014-02-04 18:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OS << '(';
|
|
|
|
Expr->print(OS);
|
|
|
|
OS << ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MipsMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
|
|
|
|
const MCAsmLayout *Layout) const {
|
2014-03-12 16:55:59 +00:00
|
|
|
return getSubExpr()->EvaluateAsRelocatable(Res, Layout);
|
2014-02-04 18:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: This basically copies MCObjectStreamer::AddValueSymbols. Perhaps
|
|
|
|
// that method should be made public?
|
|
|
|
static void AddValueSymbolsImpl(const MCExpr *Value, MCAssembler *Asm) {
|
|
|
|
switch (Value->getKind()) {
|
|
|
|
case MCExpr::Target:
|
|
|
|
llvm_unreachable("Can't handle nested target expr!");
|
|
|
|
|
|
|
|
case MCExpr::Constant:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MCExpr::Binary: {
|
|
|
|
const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
|
|
|
|
AddValueSymbolsImpl(BE->getLHS(), Asm);
|
|
|
|
AddValueSymbolsImpl(BE->getRHS(), Asm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case MCExpr::SymbolRef:
|
|
|
|
Asm->getOrCreateSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MCExpr::Unary:
|
|
|
|
AddValueSymbolsImpl(cast<MCUnaryExpr>(Value)->getSubExpr(), Asm);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MipsMCExpr::AddValueSymbols(MCAssembler *Asm) const {
|
|
|
|
AddValueSymbolsImpl(getSubExpr(), Asm);
|
|
|
|
}
|