LLVM API Documentation

SparcMCExpr.cpp
Go to the documentation of this file.
00001 //===-- SparcMCExpr.cpp - Sparc 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 // This file contains the implementation of the assembly expression modifiers
00011 // accepted by the Sparc architecture (e.g. "%hi", "%lo", ...).
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "SparcMCExpr.h"
00016 #include "llvm/MC/MCAssembler.h"
00017 #include "llvm/MC/MCContext.h"
00018 #include "llvm/MC/MCELF.h"
00019 #include "llvm/MC/MCObjectStreamer.h"
00020 #include "llvm/MC/MCSymbol.h"
00021 #include "llvm/Object/ELF.h"
00022 
00023 
00024 using namespace llvm;
00025 
00026 #define DEBUG_TYPE "sparcmcexpr"
00027 
00028 const SparcMCExpr*
00029 SparcMCExpr::Create(VariantKind Kind, const MCExpr *Expr,
00030                       MCContext &Ctx) {
00031     return new (Ctx) SparcMCExpr(Kind, Expr);
00032 }
00033 
00034 
00035 
00036 void SparcMCExpr::PrintImpl(raw_ostream &OS) const
00037 {
00038 
00039   bool closeParen = printVariantKind(OS, Kind);
00040 
00041   const MCExpr *Expr = getSubExpr();
00042   Expr->print(OS);
00043 
00044   if (closeParen)
00045     OS << ')';
00046 }
00047 
00048 bool SparcMCExpr::printVariantKind(raw_ostream &OS, VariantKind Kind)
00049 {
00050   bool closeParen = true;
00051   switch (Kind) {
00052   case VK_Sparc_None:     closeParen = false; break;
00053   case VK_Sparc_LO:       OS << "%lo(";  break;
00054   case VK_Sparc_HI:       OS << "%hi(";  break;
00055   case VK_Sparc_H44:      OS << "%h44("; break;
00056   case VK_Sparc_M44:      OS << "%m44("; break;
00057   case VK_Sparc_L44:      OS << "%l44("; break;
00058   case VK_Sparc_HH:       OS << "%hh(";  break;
00059   case VK_Sparc_HM:       OS << "%hm(";  break;
00060     // FIXME: use %pc22/%pc10, if system assembler supports them.
00061   case VK_Sparc_PC22:     OS << "%hi("; break;
00062   case VK_Sparc_PC10:     OS << "%lo("; break;
00063     // FIXME: use %got22/%got10, if system assembler supports them.
00064   case VK_Sparc_GOT22:    OS << "%hi("; break;
00065   case VK_Sparc_GOT10:    OS << "%lo("; break;
00066   case VK_Sparc_WPLT30:   closeParen = false; break;
00067   case VK_Sparc_R_DISP32: OS << "%r_disp32("; break;
00068   case VK_Sparc_TLS_GD_HI22:   OS << "%tgd_hi22(";   break;
00069   case VK_Sparc_TLS_GD_LO10:   OS << "%tgd_lo10(";   break;
00070   case VK_Sparc_TLS_GD_ADD:    OS << "%tgd_add(";    break;
00071   case VK_Sparc_TLS_GD_CALL:   OS << "%tgd_call(";   break;
00072   case VK_Sparc_TLS_LDM_HI22:  OS << "%tldm_hi22(";  break;
00073   case VK_Sparc_TLS_LDM_LO10:  OS << "%tldm_lo10(";  break;
00074   case VK_Sparc_TLS_LDM_ADD:   OS << "%tldm_add(";   break;
00075   case VK_Sparc_TLS_LDM_CALL:  OS << "%tldm_call(";  break;
00076   case VK_Sparc_TLS_LDO_HIX22: OS << "%tldo_hix22("; break;
00077   case VK_Sparc_TLS_LDO_LOX10: OS << "%tldo_lox10("; break;
00078   case VK_Sparc_TLS_LDO_ADD:   OS << "%tldo_add(";   break;
00079   case VK_Sparc_TLS_IE_HI22:   OS << "%tie_hi22(";   break;
00080   case VK_Sparc_TLS_IE_LO10:   OS << "%tie_lo10(";   break;
00081   case VK_Sparc_TLS_IE_LD:     OS << "%tie_ld(";     break;
00082   case VK_Sparc_TLS_IE_LDX:    OS << "%tie_ldx(";    break;
00083   case VK_Sparc_TLS_IE_ADD:    OS << "%tie_add(";    break;
00084   case VK_Sparc_TLS_LE_HIX22:  OS << "%tle_hix22(";  break;
00085   case VK_Sparc_TLS_LE_LOX10:  OS << "%tle_lox10(";  break;
00086   }
00087   return closeParen;
00088 }
00089 
00090 SparcMCExpr::VariantKind SparcMCExpr::parseVariantKind(StringRef name)
00091 {
00092   return StringSwitch<SparcMCExpr::VariantKind>(name)
00093     .Case("lo",  VK_Sparc_LO)
00094     .Case("hi",  VK_Sparc_HI)
00095     .Case("h44", VK_Sparc_H44)
00096     .Case("m44", VK_Sparc_M44)
00097     .Case("l44", VK_Sparc_L44)
00098     .Case("hh",  VK_Sparc_HH)
00099     .Case("hm",  VK_Sparc_HM)
00100     .Case("pc22",  VK_Sparc_PC22)
00101     .Case("pc10",  VK_Sparc_PC10)
00102     .Case("got22", VK_Sparc_GOT22)
00103     .Case("got10", VK_Sparc_GOT10)
00104     .Case("r_disp32",   VK_Sparc_R_DISP32)
00105     .Case("tgd_hi22",   VK_Sparc_TLS_GD_HI22)
00106     .Case("tgd_lo10",   VK_Sparc_TLS_GD_LO10)
00107     .Case("tgd_add",    VK_Sparc_TLS_GD_ADD)
00108     .Case("tgd_call",   VK_Sparc_TLS_GD_CALL)
00109     .Case("tldm_hi22",  VK_Sparc_TLS_LDM_HI22)
00110     .Case("tldm_lo10",  VK_Sparc_TLS_LDM_LO10)
00111     .Case("tldm_add",   VK_Sparc_TLS_LDM_ADD)
00112     .Case("tldm_call",  VK_Sparc_TLS_LDM_CALL)
00113     .Case("tldo_hix22", VK_Sparc_TLS_LDO_HIX22)
00114     .Case("tldo_lox10", VK_Sparc_TLS_LDO_LOX10)
00115     .Case("tldo_add",   VK_Sparc_TLS_LDO_ADD)
00116     .Case("tie_hi22",   VK_Sparc_TLS_IE_HI22)
00117     .Case("tie_lo10",   VK_Sparc_TLS_IE_LO10)
00118     .Case("tie_ld",     VK_Sparc_TLS_IE_LD)
00119     .Case("tie_ldx",    VK_Sparc_TLS_IE_LDX)
00120     .Case("tie_add",    VK_Sparc_TLS_IE_ADD)
00121     .Case("tle_hix22",  VK_Sparc_TLS_LE_HIX22)
00122     .Case("tle_lox10",  VK_Sparc_TLS_LE_LOX10)
00123     .Default(VK_Sparc_None);
00124 }
00125 
00126 Sparc::Fixups SparcMCExpr::getFixupKind(SparcMCExpr::VariantKind Kind) {
00127   switch (Kind) {
00128   default: llvm_unreachable("Unhandled SparcMCExpr::VariantKind");
00129   case VK_Sparc_LO:       return Sparc::fixup_sparc_lo10;
00130   case VK_Sparc_HI:       return Sparc::fixup_sparc_hi22;
00131   case VK_Sparc_H44:      return Sparc::fixup_sparc_h44;
00132   case VK_Sparc_M44:      return Sparc::fixup_sparc_m44;
00133   case VK_Sparc_L44:      return Sparc::fixup_sparc_l44;
00134   case VK_Sparc_HH:       return Sparc::fixup_sparc_hh;
00135   case VK_Sparc_HM:       return Sparc::fixup_sparc_hm;
00136   case VK_Sparc_PC22:     return Sparc::fixup_sparc_pc22;
00137   case VK_Sparc_PC10:     return Sparc::fixup_sparc_pc10;
00138   case VK_Sparc_GOT22:    return Sparc::fixup_sparc_got22;
00139   case VK_Sparc_GOT10:    return Sparc::fixup_sparc_got10;
00140   case VK_Sparc_WPLT30:   return Sparc::fixup_sparc_wplt30;
00141   case VK_Sparc_TLS_GD_HI22:   return Sparc::fixup_sparc_tls_gd_hi22;
00142   case VK_Sparc_TLS_GD_LO10:   return Sparc::fixup_sparc_tls_gd_lo10;
00143   case VK_Sparc_TLS_GD_ADD:    return Sparc::fixup_sparc_tls_gd_add;
00144   case VK_Sparc_TLS_GD_CALL:   return Sparc::fixup_sparc_tls_gd_call;
00145   case VK_Sparc_TLS_LDM_HI22:  return Sparc::fixup_sparc_tls_ldm_hi22;
00146   case VK_Sparc_TLS_LDM_LO10:  return Sparc::fixup_sparc_tls_ldm_lo10;
00147   case VK_Sparc_TLS_LDM_ADD:   return Sparc::fixup_sparc_tls_ldm_add;
00148   case VK_Sparc_TLS_LDM_CALL:  return Sparc::fixup_sparc_tls_ldm_call;
00149   case VK_Sparc_TLS_LDO_HIX22: return Sparc::fixup_sparc_tls_ldo_hix22;
00150   case VK_Sparc_TLS_LDO_LOX10: return Sparc::fixup_sparc_tls_ldo_lox10;
00151   case VK_Sparc_TLS_LDO_ADD:   return Sparc::fixup_sparc_tls_ldo_add;
00152   case VK_Sparc_TLS_IE_HI22:   return Sparc::fixup_sparc_tls_ie_hi22;
00153   case VK_Sparc_TLS_IE_LO10:   return Sparc::fixup_sparc_tls_ie_lo10;
00154   case VK_Sparc_TLS_IE_LD:     return Sparc::fixup_sparc_tls_ie_ld;
00155   case VK_Sparc_TLS_IE_LDX:    return Sparc::fixup_sparc_tls_ie_ldx;
00156   case VK_Sparc_TLS_IE_ADD:    return Sparc::fixup_sparc_tls_ie_add;
00157   case VK_Sparc_TLS_LE_HIX22:  return Sparc::fixup_sparc_tls_le_hix22;
00158   case VK_Sparc_TLS_LE_LOX10:  return Sparc::fixup_sparc_tls_le_lox10;
00159   }
00160 }
00161 
00162 bool
00163 SparcMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
00164                                        const MCAsmLayout *Layout,
00165                                        const MCFixup *Fixup) const {
00166   return getSubExpr()->EvaluateAsRelocatable(Res, Layout, Fixup);
00167 }
00168 
00169 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
00170   switch (Expr->getKind()) {
00171   case MCExpr::Target:
00172     llvm_unreachable("Can't handle nested target expr!");
00173     break;
00174 
00175   case MCExpr::Constant:
00176     break;
00177 
00178   case MCExpr::Binary: {
00179     const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
00180     fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
00181     fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
00182     break;
00183   }
00184 
00185   case MCExpr::SymbolRef: {
00186     const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
00187     MCSymbolData &SD = Asm.getOrCreateSymbolData(SymRef.getSymbol());
00188     MCELF::SetType(SD, ELF::STT_TLS);
00189     break;
00190   }
00191 
00192   case MCExpr::Unary:
00193     fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
00194     break;
00195   }
00196 
00197 }
00198 
00199 void SparcMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
00200   switch(getKind()) {
00201   default: return;
00202   case VK_Sparc_TLS_GD_HI22:
00203   case VK_Sparc_TLS_GD_LO10:
00204   case VK_Sparc_TLS_GD_ADD:
00205   case VK_Sparc_TLS_GD_CALL:
00206   case VK_Sparc_TLS_LDM_HI22:
00207   case VK_Sparc_TLS_LDM_LO10:
00208   case VK_Sparc_TLS_LDM_ADD:
00209   case VK_Sparc_TLS_LDM_CALL:
00210   case VK_Sparc_TLS_LDO_HIX22:
00211   case VK_Sparc_TLS_LDO_LOX10:
00212   case VK_Sparc_TLS_LDO_ADD:
00213   case VK_Sparc_TLS_IE_HI22:
00214   case VK_Sparc_TLS_IE_LO10:
00215   case VK_Sparc_TLS_IE_LD:
00216   case VK_Sparc_TLS_IE_LDX:
00217   case VK_Sparc_TLS_IE_ADD:
00218   case VK_Sparc_TLS_LE_HIX22:
00219   case VK_Sparc_TLS_LE_LOX10: break;
00220   }
00221   fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
00222 }
00223 
00224 void SparcMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
00225   Streamer.visitUsedExpr(*getSubExpr());
00226 }