xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Sparc/MCTargetDesc/SparcMCExpr.cpp (revision 6ba2210ee039f2f12878c217bcf058e9c8b26b29)
1 //===-- SparcMCExpr.cpp - Sparc 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 Sparc architecture (e.g. "%hi", "%lo", ...).
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "SparcMCExpr.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 
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "sparcmcexpr"
24 
25 const SparcMCExpr*
26 SparcMCExpr::create(VariantKind Kind, const MCExpr *Expr,
27                       MCContext &Ctx) {
28     return new (Ctx) SparcMCExpr(Kind, Expr);
29 }
30 
31 void SparcMCExpr::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 }
41 
42 bool SparcMCExpr::printVariantKind(raw_ostream &OS, VariantKind Kind)
43 {
44   switch (Kind) {
45   case VK_Sparc_None:     return false;
46   case VK_Sparc_LO:       OS << "%lo(";  return true;
47   case VK_Sparc_HI:       OS << "%hi(";  return true;
48   case VK_Sparc_H44:      OS << "%h44("; return true;
49   case VK_Sparc_M44:      OS << "%m44("; return true;
50   case VK_Sparc_L44:      OS << "%l44("; return true;
51   case VK_Sparc_HH:       OS << "%hh(";  return true;
52   case VK_Sparc_HM:       OS << "%hm(";  return true;
53     // FIXME: use %pc22/%pc10, if system assembler supports them.
54   case VK_Sparc_PC22:     OS << "%hi("; return true;
55   case VK_Sparc_PC10:     OS << "%lo("; return true;
56     // FIXME: use %got22/%got10, if system assembler supports them.
57   case VK_Sparc_GOT22:    OS << "%hi("; return true;
58   case VK_Sparc_GOT10:    OS << "%lo("; return true;
59   case VK_Sparc_GOT13:    return false;
60   case VK_Sparc_13:       return false;
61   case VK_Sparc_WDISP30:  return false;
62   case VK_Sparc_WPLT30:   return false;
63   case VK_Sparc_R_DISP32: OS << "%r_disp32("; return true;
64   case VK_Sparc_TLS_GD_HI22:   OS << "%tgd_hi22(";   return true;
65   case VK_Sparc_TLS_GD_LO10:   OS << "%tgd_lo10(";   return true;
66   case VK_Sparc_TLS_GD_ADD:    OS << "%tgd_add(";    return true;
67   case VK_Sparc_TLS_GD_CALL:   OS << "%tgd_call(";   return true;
68   case VK_Sparc_TLS_LDM_HI22:  OS << "%tldm_hi22(";  return true;
69   case VK_Sparc_TLS_LDM_LO10:  OS << "%tldm_lo10(";  return true;
70   case VK_Sparc_TLS_LDM_ADD:   OS << "%tldm_add(";   return true;
71   case VK_Sparc_TLS_LDM_CALL:  OS << "%tldm_call(";  return true;
72   case VK_Sparc_TLS_LDO_HIX22: OS << "%tldo_hix22("; return true;
73   case VK_Sparc_TLS_LDO_LOX10: OS << "%tldo_lox10("; return true;
74   case VK_Sparc_TLS_LDO_ADD:   OS << "%tldo_add(";   return true;
75   case VK_Sparc_TLS_IE_HI22:   OS << "%tie_hi22(";   return true;
76   case VK_Sparc_TLS_IE_LO10:   OS << "%tie_lo10(";   return true;
77   case VK_Sparc_TLS_IE_LD:     OS << "%tie_ld(";     return true;
78   case VK_Sparc_TLS_IE_LDX:    OS << "%tie_ldx(";    return true;
79   case VK_Sparc_TLS_IE_ADD:    OS << "%tie_add(";    return true;
80   case VK_Sparc_TLS_LE_HIX22:  OS << "%tle_hix22(";  return true;
81   case VK_Sparc_TLS_LE_LOX10:  OS << "%tle_lox10(";  return true;
82   }
83   llvm_unreachable("Unhandled SparcMCExpr::VariantKind");
84 }
85 
86 SparcMCExpr::VariantKind SparcMCExpr::parseVariantKind(StringRef name)
87 {
88   return StringSwitch<SparcMCExpr::VariantKind>(name)
89     .Case("lo",  VK_Sparc_LO)
90     .Case("hi",  VK_Sparc_HI)
91     .Case("h44", VK_Sparc_H44)
92     .Case("m44", VK_Sparc_M44)
93     .Case("l44", VK_Sparc_L44)
94     .Case("hh",  VK_Sparc_HH)
95     .Case("hm",  VK_Sparc_HM)
96     .Case("pc22",  VK_Sparc_PC22)
97     .Case("pc10",  VK_Sparc_PC10)
98     .Case("got22", VK_Sparc_GOT22)
99     .Case("got10", VK_Sparc_GOT10)
100     .Case("got13", VK_Sparc_GOT13)
101     .Case("r_disp32",   VK_Sparc_R_DISP32)
102     .Case("tgd_hi22",   VK_Sparc_TLS_GD_HI22)
103     .Case("tgd_lo10",   VK_Sparc_TLS_GD_LO10)
104     .Case("tgd_add",    VK_Sparc_TLS_GD_ADD)
105     .Case("tgd_call",   VK_Sparc_TLS_GD_CALL)
106     .Case("tldm_hi22",  VK_Sparc_TLS_LDM_HI22)
107     .Case("tldm_lo10",  VK_Sparc_TLS_LDM_LO10)
108     .Case("tldm_add",   VK_Sparc_TLS_LDM_ADD)
109     .Case("tldm_call",  VK_Sparc_TLS_LDM_CALL)
110     .Case("tldo_hix22", VK_Sparc_TLS_LDO_HIX22)
111     .Case("tldo_lox10", VK_Sparc_TLS_LDO_LOX10)
112     .Case("tldo_add",   VK_Sparc_TLS_LDO_ADD)
113     .Case("tie_hi22",   VK_Sparc_TLS_IE_HI22)
114     .Case("tie_lo10",   VK_Sparc_TLS_IE_LO10)
115     .Case("tie_ld",     VK_Sparc_TLS_IE_LD)
116     .Case("tie_ldx",    VK_Sparc_TLS_IE_LDX)
117     .Case("tie_add",    VK_Sparc_TLS_IE_ADD)
118     .Case("tle_hix22",  VK_Sparc_TLS_LE_HIX22)
119     .Case("tle_lox10",  VK_Sparc_TLS_LE_LOX10)
120     .Default(VK_Sparc_None);
121 }
122 
123 Sparc::Fixups SparcMCExpr::getFixupKind(SparcMCExpr::VariantKind Kind) {
124   switch (Kind) {
125   default: llvm_unreachable("Unhandled SparcMCExpr::VariantKind");
126   case VK_Sparc_LO:       return Sparc::fixup_sparc_lo10;
127   case VK_Sparc_HI:       return Sparc::fixup_sparc_hi22;
128   case VK_Sparc_H44:      return Sparc::fixup_sparc_h44;
129   case VK_Sparc_M44:      return Sparc::fixup_sparc_m44;
130   case VK_Sparc_L44:      return Sparc::fixup_sparc_l44;
131   case VK_Sparc_HH:       return Sparc::fixup_sparc_hh;
132   case VK_Sparc_HM:       return Sparc::fixup_sparc_hm;
133   case VK_Sparc_PC22:     return Sparc::fixup_sparc_pc22;
134   case VK_Sparc_PC10:     return Sparc::fixup_sparc_pc10;
135   case VK_Sparc_GOT22:    return Sparc::fixup_sparc_got22;
136   case VK_Sparc_GOT10:    return Sparc::fixup_sparc_got10;
137   case VK_Sparc_GOT13:    return Sparc::fixup_sparc_got13;
138   case VK_Sparc_13:       return Sparc::fixup_sparc_13;
139   case VK_Sparc_WPLT30:   return Sparc::fixup_sparc_wplt30;
140   case VK_Sparc_WDISP30:  return Sparc::fixup_sparc_call30;
141   case VK_Sparc_TLS_GD_HI22:   return Sparc::fixup_sparc_tls_gd_hi22;
142   case VK_Sparc_TLS_GD_LO10:   return Sparc::fixup_sparc_tls_gd_lo10;
143   case VK_Sparc_TLS_GD_ADD:    return Sparc::fixup_sparc_tls_gd_add;
144   case VK_Sparc_TLS_GD_CALL:   return Sparc::fixup_sparc_tls_gd_call;
145   case VK_Sparc_TLS_LDM_HI22:  return Sparc::fixup_sparc_tls_ldm_hi22;
146   case VK_Sparc_TLS_LDM_LO10:  return Sparc::fixup_sparc_tls_ldm_lo10;
147   case VK_Sparc_TLS_LDM_ADD:   return Sparc::fixup_sparc_tls_ldm_add;
148   case VK_Sparc_TLS_LDM_CALL:  return Sparc::fixup_sparc_tls_ldm_call;
149   case VK_Sparc_TLS_LDO_HIX22: return Sparc::fixup_sparc_tls_ldo_hix22;
150   case VK_Sparc_TLS_LDO_LOX10: return Sparc::fixup_sparc_tls_ldo_lox10;
151   case VK_Sparc_TLS_LDO_ADD:   return Sparc::fixup_sparc_tls_ldo_add;
152   case VK_Sparc_TLS_IE_HI22:   return Sparc::fixup_sparc_tls_ie_hi22;
153   case VK_Sparc_TLS_IE_LO10:   return Sparc::fixup_sparc_tls_ie_lo10;
154   case VK_Sparc_TLS_IE_LD:     return Sparc::fixup_sparc_tls_ie_ld;
155   case VK_Sparc_TLS_IE_LDX:    return Sparc::fixup_sparc_tls_ie_ldx;
156   case VK_Sparc_TLS_IE_ADD:    return Sparc::fixup_sparc_tls_ie_add;
157   case VK_Sparc_TLS_LE_HIX22:  return Sparc::fixup_sparc_tls_le_hix22;
158   case VK_Sparc_TLS_LE_LOX10:  return Sparc::fixup_sparc_tls_le_lox10;
159   }
160 }
161 
162 bool
163 SparcMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
164                                        const MCAsmLayout *Layout,
165                                        const MCFixup *Fixup) const {
166   return getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup);
167 }
168 
169 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
170   switch (Expr->getKind()) {
171   case MCExpr::Target:
172     llvm_unreachable("Can't handle nested target expr!");
173     break;
174 
175   case MCExpr::Constant:
176     break;
177 
178   case MCExpr::Binary: {
179     const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
180     fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
181     fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
182     break;
183   }
184 
185   case MCExpr::SymbolRef: {
186     const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
187     cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
188     break;
189   }
190 
191   case MCExpr::Unary:
192     fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
193     break;
194   }
195 
196 }
197 
198 void SparcMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
199   switch(getKind()) {
200   default: return;
201   case VK_Sparc_TLS_GD_CALL:
202   case VK_Sparc_TLS_LDM_CALL: {
203     // The corresponding relocations reference __tls_get_addr, as they call it,
204     // but this is only implicit; we must explicitly add it to our symbol table
205     // to bind it for these uses.
206     MCSymbol *Symbol = Asm.getContext().getOrCreateSymbol("__tls_get_addr");
207     Asm.registerSymbol(*Symbol);
208     auto ELFSymbol = cast<MCSymbolELF>(Symbol);
209     if (!ELFSymbol->isBindingSet())
210       ELFSymbol->setBinding(ELF::STB_GLOBAL);
211     LLVM_FALLTHROUGH;
212   }
213   case VK_Sparc_TLS_GD_HI22:
214   case VK_Sparc_TLS_GD_LO10:
215   case VK_Sparc_TLS_GD_ADD:
216   case VK_Sparc_TLS_LDM_HI22:
217   case VK_Sparc_TLS_LDM_LO10:
218   case VK_Sparc_TLS_LDM_ADD:
219   case VK_Sparc_TLS_LDO_HIX22:
220   case VK_Sparc_TLS_LDO_LOX10:
221   case VK_Sparc_TLS_LDO_ADD:
222   case VK_Sparc_TLS_IE_HI22:
223   case VK_Sparc_TLS_IE_LO10:
224   case VK_Sparc_TLS_IE_LD:
225   case VK_Sparc_TLS_IE_LDX:
226   case VK_Sparc_TLS_IE_ADD:
227   case VK_Sparc_TLS_LE_HIX22:
228   case VK_Sparc_TLS_LE_LOX10: break;
229   }
230   fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
231 }
232 
233 void SparcMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
234   Streamer.visitUsedExpr(*getSubExpr());
235 }
236