LLVM API Documentation

MipsMCExpr.cpp
Go to the documentation of this file.
00001 //===-- MipsMCExpr.cpp - Mips specific MC expression classes --------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "MipsMCExpr.h"
00011 #include "llvm/MC/MCAsmInfo.h"
00012 #include "llvm/MC/MCAssembler.h"
00013 #include "llvm/MC/MCContext.h"
00014 #include "llvm/MC/MCObjectStreamer.h"
00015 
00016 using namespace llvm;
00017 
00018 #define DEBUG_TYPE "mipsmcexpr"
00019 
00020 bool MipsMCExpr::isSupportedBinaryExpr(MCSymbolRefExpr::VariantKind VK,
00021                                        const MCBinaryExpr *BE) {
00022   switch (VK) {
00023   case MCSymbolRefExpr::VK_Mips_ABS_LO:
00024   case MCSymbolRefExpr::VK_Mips_ABS_HI:
00025   case MCSymbolRefExpr::VK_Mips_HIGHER:
00026   case MCSymbolRefExpr::VK_Mips_HIGHEST:
00027     break;
00028   default:
00029     return false;
00030   }
00031 
00032   // We support expressions of the form "(sym1 binop1 sym2) binop2 const",
00033   // where "binop2 const" is optional.
00034   if (isa<MCBinaryExpr>(BE->getLHS())) {
00035     if (!isa<MCConstantExpr>(BE->getRHS()))
00036       return false;
00037     BE = cast<MCBinaryExpr>(BE->getLHS());
00038   }
00039   return (isa<MCSymbolRefExpr>(BE->getLHS())
00040           && isa<MCSymbolRefExpr>(BE->getRHS()));
00041 }
00042 
00043 const MipsMCExpr*
00044 MipsMCExpr::Create(MCSymbolRefExpr::VariantKind VK, const MCExpr *Expr,
00045                    MCContext &Ctx) {
00046   VariantKind Kind;
00047   switch (VK) {
00048   case MCSymbolRefExpr::VK_Mips_ABS_LO:
00049     Kind = VK_Mips_LO;
00050     break;
00051   case MCSymbolRefExpr::VK_Mips_ABS_HI:
00052     Kind = VK_Mips_HI;
00053     break;
00054   case MCSymbolRefExpr::VK_Mips_HIGHER:
00055     Kind = VK_Mips_HIGHER;
00056     break;
00057   case MCSymbolRefExpr::VK_Mips_HIGHEST:
00058     Kind = VK_Mips_HIGHEST;
00059     break;
00060   default:
00061     llvm_unreachable("Invalid kind!");
00062   }
00063 
00064   return new (Ctx) MipsMCExpr(Kind, Expr);
00065 }
00066 
00067 void MipsMCExpr::PrintImpl(raw_ostream &OS) const {
00068   switch (Kind) {
00069   default: llvm_unreachable("Invalid kind!");
00070   case VK_Mips_LO: OS << "%lo"; break;
00071   case VK_Mips_HI: OS << "%hi"; break;
00072   case VK_Mips_HIGHER: OS << "%higher"; break;
00073   case VK_Mips_HIGHEST: OS << "%highest"; break;
00074   }
00075 
00076   OS << '(';
00077   Expr->print(OS);
00078   OS << ')';
00079 }
00080 
00081 bool
00082 MipsMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
00083                                       const MCAsmLayout *Layout,
00084                                       const MCFixup *Fixup) const {
00085   return getSubExpr()->EvaluateAsRelocatable(Res, Layout, Fixup);
00086 }
00087 
00088 void MipsMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
00089   Streamer.visitUsedExpr(*getSubExpr());
00090 }