1 //===-- SparcMCExpr.cpp - Sparc specific MC expression classes --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the implementation of the assembly expression modifiers
11 // accepted by the Sparc architecture (e.g. "%hi", "%lo", ...).
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "SparcMCExpr.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCELF.h"
19 #include "llvm/MC/MCObjectStreamer.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Object/ELF.h"
22 
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "sparcmcexpr"
27 
28 const SparcMCExpr*
Create(VariantKind Kind,const MCExpr * Expr,MCContext & Ctx)29 SparcMCExpr::Create(VariantKind Kind, const MCExpr *Expr,
30                       MCContext &Ctx) {
31     return new (Ctx) SparcMCExpr(Kind, Expr);
32 }
33 
34 
35 
PrintImpl(raw_ostream & OS) const36 void SparcMCExpr::PrintImpl(raw_ostream &OS) const
37 {
38 
39   bool closeParen = printVariantKind(OS, Kind);
40 
41   const MCExpr *Expr = getSubExpr();
42   Expr->print(OS);
43 
44   if (closeParen)
45     OS << ')';
46 }
47 
printVariantKind(raw_ostream & OS,VariantKind Kind)48 bool SparcMCExpr::printVariantKind(raw_ostream &OS, VariantKind Kind)
49 {
50   bool closeParen = true;
51   switch (Kind) {
52   case VK_Sparc_None:     closeParen = false; break;
53   case VK_Sparc_LO:       OS << "%lo(";  break;
54   case VK_Sparc_HI:       OS << "%hi(";  break;
55   case VK_Sparc_H44:      OS << "%h44("; break;
56   case VK_Sparc_M44:      OS << "%m44("; break;
57   case VK_Sparc_L44:      OS << "%l44("; break;
58   case VK_Sparc_HH:       OS << "%hh(";  break;
59   case VK_Sparc_HM:       OS << "%hm(";  break;
60     // FIXME: use %pc22/%pc10, if system assembler supports them.
61   case VK_Sparc_PC22:     OS << "%hi("; break;
62   case VK_Sparc_PC10:     OS << "%lo("; break;
63     // FIXME: use %got22/%got10, if system assembler supports them.
64   case VK_Sparc_GOT22:    OS << "%hi("; break;
65   case VK_Sparc_GOT10:    OS << "%lo("; break;
66   case VK_Sparc_WPLT30:   closeParen = false; break;
67   case VK_Sparc_R_DISP32: OS << "%r_disp32("; break;
68   case VK_Sparc_TLS_GD_HI22:   OS << "%tgd_hi22(";   break;
69   case VK_Sparc_TLS_GD_LO10:   OS << "%tgd_lo10(";   break;
70   case VK_Sparc_TLS_GD_ADD:    OS << "%tgd_add(";    break;
71   case VK_Sparc_TLS_GD_CALL:   OS << "%tgd_call(";   break;
72   case VK_Sparc_TLS_LDM_HI22:  OS << "%tldm_hi22(";  break;
73   case VK_Sparc_TLS_LDM_LO10:  OS << "%tldm_lo10(";  break;
74   case VK_Sparc_TLS_LDM_ADD:   OS << "%tldm_add(";   break;
75   case VK_Sparc_TLS_LDM_CALL:  OS << "%tldm_call(";  break;
76   case VK_Sparc_TLS_LDO_HIX22: OS << "%tldo_hix22("; break;
77   case VK_Sparc_TLS_LDO_LOX10: OS << "%tldo_lox10("; break;
78   case VK_Sparc_TLS_LDO_ADD:   OS << "%tldo_add(";   break;
79   case VK_Sparc_TLS_IE_HI22:   OS << "%tie_hi22(";   break;
80   case VK_Sparc_TLS_IE_LO10:   OS << "%tie_lo10(";   break;
81   case VK_Sparc_TLS_IE_LD:     OS << "%tie_ld(";     break;
82   case VK_Sparc_TLS_IE_LDX:    OS << "%tie_ldx(";    break;
83   case VK_Sparc_TLS_IE_ADD:    OS << "%tie_add(";    break;
84   case VK_Sparc_TLS_LE_HIX22:  OS << "%tle_hix22(";  break;
85   case VK_Sparc_TLS_LE_LOX10:  OS << "%tle_lox10(";  break;
86   }
87   return closeParen;
88 }
89 
parseVariantKind(StringRef name)90 SparcMCExpr::VariantKind SparcMCExpr::parseVariantKind(StringRef name)
91 {
92   return StringSwitch<SparcMCExpr::VariantKind>(name)
93     .Case("lo",  VK_Sparc_LO)
94     .Case("hi",  VK_Sparc_HI)
95     .Case("h44", VK_Sparc_H44)
96     .Case("m44", VK_Sparc_M44)
97     .Case("l44", VK_Sparc_L44)
98     .Case("hh",  VK_Sparc_HH)
99     .Case("hm",  VK_Sparc_HM)
100     .Case("pc22",  VK_Sparc_PC22)
101     .Case("pc10",  VK_Sparc_PC10)
102     .Case("got22", VK_Sparc_GOT22)
103     .Case("got10", VK_Sparc_GOT10)
104     .Case("r_disp32",   VK_Sparc_R_DISP32)
105     .Case("tgd_hi22",   VK_Sparc_TLS_GD_HI22)
106     .Case("tgd_lo10",   VK_Sparc_TLS_GD_LO10)
107     .Case("tgd_add",    VK_Sparc_TLS_GD_ADD)
108     .Case("tgd_call",   VK_Sparc_TLS_GD_CALL)
109     .Case("tldm_hi22",  VK_Sparc_TLS_LDM_HI22)
110     .Case("tldm_lo10",  VK_Sparc_TLS_LDM_LO10)
111     .Case("tldm_add",   VK_Sparc_TLS_LDM_ADD)
112     .Case("tldm_call",  VK_Sparc_TLS_LDM_CALL)
113     .Case("tldo_hix22", VK_Sparc_TLS_LDO_HIX22)
114     .Case("tldo_lox10", VK_Sparc_TLS_LDO_LOX10)
115     .Case("tldo_add",   VK_Sparc_TLS_LDO_ADD)
116     .Case("tie_hi22",   VK_Sparc_TLS_IE_HI22)
117     .Case("tie_lo10",   VK_Sparc_TLS_IE_LO10)
118     .Case("tie_ld",     VK_Sparc_TLS_IE_LD)
119     .Case("tie_ldx",    VK_Sparc_TLS_IE_LDX)
120     .Case("tie_add",    VK_Sparc_TLS_IE_ADD)
121     .Case("tle_hix22",  VK_Sparc_TLS_LE_HIX22)
122     .Case("tle_lox10",  VK_Sparc_TLS_LE_LOX10)
123     .Default(VK_Sparc_None);
124 }
125 
getFixupKind(SparcMCExpr::VariantKind Kind)126 Sparc::Fixups SparcMCExpr::getFixupKind(SparcMCExpr::VariantKind Kind) {
127   switch (Kind) {
128   default: llvm_unreachable("Unhandled SparcMCExpr::VariantKind");
129   case VK_Sparc_LO:       return Sparc::fixup_sparc_lo10;
130   case VK_Sparc_HI:       return Sparc::fixup_sparc_hi22;
131   case VK_Sparc_H44:      return Sparc::fixup_sparc_h44;
132   case VK_Sparc_M44:      return Sparc::fixup_sparc_m44;
133   case VK_Sparc_L44:      return Sparc::fixup_sparc_l44;
134   case VK_Sparc_HH:       return Sparc::fixup_sparc_hh;
135   case VK_Sparc_HM:       return Sparc::fixup_sparc_hm;
136   case VK_Sparc_PC22:     return Sparc::fixup_sparc_pc22;
137   case VK_Sparc_PC10:     return Sparc::fixup_sparc_pc10;
138   case VK_Sparc_GOT22:    return Sparc::fixup_sparc_got22;
139   case VK_Sparc_GOT10:    return Sparc::fixup_sparc_got10;
140   case VK_Sparc_WPLT30:   return Sparc::fixup_sparc_wplt30;
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
EvaluateAsRelocatableImpl(MCValue & Res,const MCAsmLayout * Layout,const MCFixup * Fixup) const163 SparcMCExpr::EvaluateAsRelocatableImpl(MCValue &Res,
164                                        const MCAsmLayout *Layout,
165                                        const MCFixup *Fixup) const {
166   return getSubExpr()->EvaluateAsRelocatable(Res, Layout, Fixup);
167 }
168 
fixELFSymbolsInTLSFixupsImpl(const MCExpr * Expr,MCAssembler & Asm)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     MCSymbolData &SD = Asm.getOrCreateSymbolData(SymRef.getSymbol());
188     MCELF::SetType(SD, ELF::STT_TLS);
189     break;
190   }
191 
192   case MCExpr::Unary:
193     fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
194     break;
195   }
196 
197 }
198 
fixELFSymbolsInTLSFixups(MCAssembler & Asm) const199 void SparcMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
200   switch(getKind()) {
201   default: return;
202   case VK_Sparc_TLS_GD_HI22:
203   case VK_Sparc_TLS_GD_LO10:
204   case VK_Sparc_TLS_GD_ADD:
205   case VK_Sparc_TLS_GD_CALL:
206   case VK_Sparc_TLS_LDM_HI22:
207   case VK_Sparc_TLS_LDM_LO10:
208   case VK_Sparc_TLS_LDM_ADD:
209   case VK_Sparc_TLS_LDM_CALL:
210   case VK_Sparc_TLS_LDO_HIX22:
211   case VK_Sparc_TLS_LDO_LOX10:
212   case VK_Sparc_TLS_LDO_ADD:
213   case VK_Sparc_TLS_IE_HI22:
214   case VK_Sparc_TLS_IE_LO10:
215   case VK_Sparc_TLS_IE_LD:
216   case VK_Sparc_TLS_IE_LDX:
217   case VK_Sparc_TLS_IE_ADD:
218   case VK_Sparc_TLS_LE_HIX22:
219   case VK_Sparc_TLS_LE_LOX10: break;
220   }
221   fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
222 }
223 
visitUsedExpr(MCStreamer & Streamer) const224 void SparcMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
225   Streamer.visitUsedExpr(*getSubExpr());
226 }
227