1 //===-- VEMCExpr.cpp - VE specific MC expression classes ------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains the implementation of the assembly expression modifiers 10 // accepted by the VE architecture (e.g. "%hi", "%lo", ...). 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "VEMCExpr.h" 15 #include "llvm/BinaryFormat/ELF.h" 16 #include "llvm/MC/MCAssembler.h" 17 #include "llvm/MC/MCContext.h" 18 #include "llvm/MC/MCObjectStreamer.h" 19 #include "llvm/MC/MCSymbolELF.h" 20 #include "llvm/MC/MCValue.h" 21 22 using namespace llvm; 23 24 #define DEBUG_TYPE "vemcexpr" 25 26 const VEMCExpr *VEMCExpr::create(VariantKind Kind, const MCExpr *Expr, 27 MCContext &Ctx) { 28 return new (Ctx) VEMCExpr(Kind, Expr); 29 } 30 31 void VEMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const { 32 33 bool closeParen = printVariantKind(OS, Kind); 34 35 const MCExpr *Expr = getSubExpr(); 36 Expr->print(OS, MAI); 37 38 if (closeParen) 39 OS << ')'; 40 printVariantKindSuffix(OS, Kind); 41 } 42 43 bool VEMCExpr::printVariantKind(raw_ostream &OS, VariantKind Kind) { 44 switch (Kind) { 45 case VK_VE_None: 46 case VK_VE_REFLONG: 47 return false; 48 49 case VK_VE_HI32: 50 case VK_VE_LO32: 51 case VK_VE_PC_HI32: 52 case VK_VE_PC_LO32: 53 case VK_VE_GOT_HI32: 54 case VK_VE_GOT_LO32: 55 case VK_VE_GOTOFF_HI32: 56 case VK_VE_GOTOFF_LO32: 57 case VK_VE_PLT_HI32: 58 case VK_VE_PLT_LO32: 59 case VK_VE_TLS_GD_HI32: 60 case VK_VE_TLS_GD_LO32: 61 case VK_VE_TPOFF_HI32: 62 case VK_VE_TPOFF_LO32: 63 // Use suffix for these variant kinds 64 return false; 65 } 66 return true; 67 } 68 69 void VEMCExpr::printVariantKindSuffix(raw_ostream &OS, VariantKind Kind) { 70 switch (Kind) { 71 case VK_VE_None: 72 case VK_VE_REFLONG: 73 break; 74 case VK_VE_HI32: 75 OS << "@hi"; 76 break; 77 case VK_VE_LO32: 78 OS << "@lo"; 79 break; 80 case VK_VE_PC_HI32: 81 OS << "@pc_hi"; 82 break; 83 case VK_VE_PC_LO32: 84 OS << "@pc_lo"; 85 break; 86 case VK_VE_GOT_HI32: 87 OS << "@got_hi"; 88 break; 89 case VK_VE_GOT_LO32: 90 OS << "@got_lo"; 91 break; 92 case VK_VE_GOTOFF_HI32: 93 OS << "@gotoff_hi"; 94 break; 95 case VK_VE_GOTOFF_LO32: 96 OS << "@gotoff_lo"; 97 break; 98 case VK_VE_PLT_HI32: 99 OS << "@plt_hi"; 100 break; 101 case VK_VE_PLT_LO32: 102 OS << "@plt_lo"; 103 break; 104 case VK_VE_TLS_GD_HI32: 105 OS << "@tls_gd_hi"; 106 break; 107 case VK_VE_TLS_GD_LO32: 108 OS << "@tls_gd_lo"; 109 break; 110 case VK_VE_TPOFF_HI32: 111 OS << "@tpoff_hi"; 112 break; 113 case VK_VE_TPOFF_LO32: 114 OS << "@tpoff_lo"; 115 break; 116 } 117 } 118 119 VEMCExpr::VariantKind VEMCExpr::parseVariantKind(StringRef name) { 120 return StringSwitch<VEMCExpr::VariantKind>(name) 121 .Case("hi", VK_VE_HI32) 122 .Case("lo", VK_VE_LO32) 123 .Case("pc_hi", VK_VE_PC_HI32) 124 .Case("pc_lo", VK_VE_PC_LO32) 125 .Case("got_hi", VK_VE_GOT_HI32) 126 .Case("got_lo", VK_VE_GOT_LO32) 127 .Case("gotoff_hi", VK_VE_GOTOFF_HI32) 128 .Case("gotoff_lo", VK_VE_GOTOFF_LO32) 129 .Case("plt_hi", VK_VE_PLT_HI32) 130 .Case("plt_lo", VK_VE_PLT_LO32) 131 .Case("tls_gd_hi", VK_VE_TLS_GD_HI32) 132 .Case("tls_gd_lo", VK_VE_TLS_GD_LO32) 133 .Case("tpoff_hi", VK_VE_TPOFF_HI32) 134 .Case("tpoff_lo", VK_VE_TPOFF_LO32) 135 .Default(VK_VE_None); 136 } 137 138 VE::Fixups VEMCExpr::getFixupKind(VEMCExpr::VariantKind Kind) { 139 switch (Kind) { 140 default: 141 llvm_unreachable("Unhandled VEMCExpr::VariantKind"); 142 case VK_VE_REFLONG: 143 return VE::fixup_ve_reflong; 144 case VK_VE_HI32: 145 return VE::fixup_ve_hi32; 146 case VK_VE_LO32: 147 return VE::fixup_ve_lo32; 148 case VK_VE_PC_HI32: 149 return VE::fixup_ve_pc_hi32; 150 case VK_VE_PC_LO32: 151 return VE::fixup_ve_pc_lo32; 152 case VK_VE_GOT_HI32: 153 return VE::fixup_ve_got_hi32; 154 case VK_VE_GOT_LO32: 155 return VE::fixup_ve_got_lo32; 156 case VK_VE_GOTOFF_HI32: 157 return VE::fixup_ve_gotoff_hi32; 158 case VK_VE_GOTOFF_LO32: 159 return VE::fixup_ve_gotoff_lo32; 160 case VK_VE_PLT_HI32: 161 return VE::fixup_ve_plt_hi32; 162 case VK_VE_PLT_LO32: 163 return VE::fixup_ve_plt_lo32; 164 case VK_VE_TLS_GD_HI32: 165 return VE::fixup_ve_tls_gd_hi32; 166 case VK_VE_TLS_GD_LO32: 167 return VE::fixup_ve_tls_gd_lo32; 168 case VK_VE_TPOFF_HI32: 169 return VE::fixup_ve_tpoff_hi32; 170 case VK_VE_TPOFF_LO32: 171 return VE::fixup_ve_tpoff_lo32; 172 } 173 } 174 175 bool VEMCExpr::evaluateAsRelocatableImpl(MCValue &Res, 176 const MCAsmLayout *Layout, 177 const MCFixup *Fixup) const { 178 if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup)) 179 return false; 180 181 Res = 182 MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind()); 183 184 return true; 185 } 186 187 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) { 188 switch (Expr->getKind()) { 189 case MCExpr::Target: 190 llvm_unreachable("Can't handle nested target expr!"); 191 break; 192 193 case MCExpr::Constant: 194 break; 195 196 case MCExpr::Binary: { 197 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr); 198 fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm); 199 fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm); 200 break; 201 } 202 203 case MCExpr::SymbolRef: { 204 // We're known to be under a TLS fixup, so any symbol should be 205 // modified. There should be only one. 206 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr); 207 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS); 208 break; 209 } 210 211 case MCExpr::Unary: 212 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm); 213 break; 214 } 215 } 216 217 void VEMCExpr::visitUsedExpr(MCStreamer &Streamer) const { 218 Streamer.visitUsedExpr(*getSubExpr()); 219 } 220 221 void VEMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const { 222 switch (getKind()) { 223 default: 224 return; 225 case VK_VE_TLS_GD_HI32: 226 case VK_VE_TLS_GD_LO32: 227 case VK_VE_TPOFF_HI32: 228 case VK_VE_TPOFF_LO32: 229 break; 230 } 231 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm); 232 } 233