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/MCObjectStreamer.h"
19 #include "llvm/MC/MCSymbolELF.h"
20 #include "llvm/Object/ELF.h"
21
22
23 using namespace llvm;
24
25 #define DEBUG_TYPE "sparcmcexpr"
26
27 const SparcMCExpr*
create(VariantKind Kind,const MCExpr * Expr,MCContext & Ctx)28 SparcMCExpr::create(VariantKind Kind, const MCExpr *Expr,
29 MCContext &Ctx) {
30 return new (Ctx) SparcMCExpr(Kind, Expr);
31 }
32
printImpl(raw_ostream & OS,const MCAsmInfo * MAI) const33 void SparcMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
34
35 bool closeParen = printVariantKind(OS, Kind);
36
37 const MCExpr *Expr = getSubExpr();
38 Expr->print(OS, MAI);
39
40 if (closeParen)
41 OS << ')';
42 }
43
printVariantKind(raw_ostream & OS,VariantKind Kind)44 bool SparcMCExpr::printVariantKind(raw_ostream &OS, VariantKind Kind)
45 {
46 bool closeParen = true;
47 switch (Kind) {
48 case VK_Sparc_None: closeParen = false; break;
49 case VK_Sparc_LO: OS << "%lo("; break;
50 case VK_Sparc_HI: OS << "%hi("; break;
51 case VK_Sparc_H44: OS << "%h44("; break;
52 case VK_Sparc_M44: OS << "%m44("; break;
53 case VK_Sparc_L44: OS << "%l44("; break;
54 case VK_Sparc_HH: OS << "%hh("; break;
55 case VK_Sparc_HM: OS << "%hm("; break;
56 // FIXME: use %pc22/%pc10, if system assembler supports them.
57 case VK_Sparc_PC22: OS << "%hi("; break;
58 case VK_Sparc_PC10: OS << "%lo("; break;
59 // FIXME: use %got22/%got10, if system assembler supports them.
60 case VK_Sparc_GOT22: OS << "%hi("; break;
61 case VK_Sparc_GOT10: OS << "%lo("; break;
62 case VK_Sparc_WPLT30: closeParen = false; break;
63 case VK_Sparc_R_DISP32: OS << "%r_disp32("; break;
64 case VK_Sparc_TLS_GD_HI22: OS << "%tgd_hi22("; break;
65 case VK_Sparc_TLS_GD_LO10: OS << "%tgd_lo10("; break;
66 case VK_Sparc_TLS_GD_ADD: OS << "%tgd_add("; break;
67 case VK_Sparc_TLS_GD_CALL: OS << "%tgd_call("; break;
68 case VK_Sparc_TLS_LDM_HI22: OS << "%tldm_hi22("; break;
69 case VK_Sparc_TLS_LDM_LO10: OS << "%tldm_lo10("; break;
70 case VK_Sparc_TLS_LDM_ADD: OS << "%tldm_add("; break;
71 case VK_Sparc_TLS_LDM_CALL: OS << "%tldm_call("; break;
72 case VK_Sparc_TLS_LDO_HIX22: OS << "%tldo_hix22("; break;
73 case VK_Sparc_TLS_LDO_LOX10: OS << "%tldo_lox10("; break;
74 case VK_Sparc_TLS_LDO_ADD: OS << "%tldo_add("; break;
75 case VK_Sparc_TLS_IE_HI22: OS << "%tie_hi22("; break;
76 case VK_Sparc_TLS_IE_LO10: OS << "%tie_lo10("; break;
77 case VK_Sparc_TLS_IE_LD: OS << "%tie_ld("; break;
78 case VK_Sparc_TLS_IE_LDX: OS << "%tie_ldx("; break;
79 case VK_Sparc_TLS_IE_ADD: OS << "%tie_add("; break;
80 case VK_Sparc_TLS_LE_HIX22: OS << "%tle_hix22("; break;
81 case VK_Sparc_TLS_LE_LOX10: OS << "%tle_lox10("; break;
82 }
83 return closeParen;
84 }
85
parseVariantKind(StringRef name)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("r_disp32", VK_Sparc_R_DISP32)
101 .Case("tgd_hi22", VK_Sparc_TLS_GD_HI22)
102 .Case("tgd_lo10", VK_Sparc_TLS_GD_LO10)
103 .Case("tgd_add", VK_Sparc_TLS_GD_ADD)
104 .Case("tgd_call", VK_Sparc_TLS_GD_CALL)
105 .Case("tldm_hi22", VK_Sparc_TLS_LDM_HI22)
106 .Case("tldm_lo10", VK_Sparc_TLS_LDM_LO10)
107 .Case("tldm_add", VK_Sparc_TLS_LDM_ADD)
108 .Case("tldm_call", VK_Sparc_TLS_LDM_CALL)
109 .Case("tldo_hix22", VK_Sparc_TLS_LDO_HIX22)
110 .Case("tldo_lox10", VK_Sparc_TLS_LDO_LOX10)
111 .Case("tldo_add", VK_Sparc_TLS_LDO_ADD)
112 .Case("tie_hi22", VK_Sparc_TLS_IE_HI22)
113 .Case("tie_lo10", VK_Sparc_TLS_IE_LO10)
114 .Case("tie_ld", VK_Sparc_TLS_IE_LD)
115 .Case("tie_ldx", VK_Sparc_TLS_IE_LDX)
116 .Case("tie_add", VK_Sparc_TLS_IE_ADD)
117 .Case("tle_hix22", VK_Sparc_TLS_LE_HIX22)
118 .Case("tle_lox10", VK_Sparc_TLS_LE_LOX10)
119 .Default(VK_Sparc_None);
120 }
121
getFixupKind(SparcMCExpr::VariantKind Kind)122 Sparc::Fixups SparcMCExpr::getFixupKind(SparcMCExpr::VariantKind Kind) {
123 switch (Kind) {
124 default: llvm_unreachable("Unhandled SparcMCExpr::VariantKind");
125 case VK_Sparc_LO: return Sparc::fixup_sparc_lo10;
126 case VK_Sparc_HI: return Sparc::fixup_sparc_hi22;
127 case VK_Sparc_H44: return Sparc::fixup_sparc_h44;
128 case VK_Sparc_M44: return Sparc::fixup_sparc_m44;
129 case VK_Sparc_L44: return Sparc::fixup_sparc_l44;
130 case VK_Sparc_HH: return Sparc::fixup_sparc_hh;
131 case VK_Sparc_HM: return Sparc::fixup_sparc_hm;
132 case VK_Sparc_PC22: return Sparc::fixup_sparc_pc22;
133 case VK_Sparc_PC10: return Sparc::fixup_sparc_pc10;
134 case VK_Sparc_GOT22: return Sparc::fixup_sparc_got22;
135 case VK_Sparc_GOT10: return Sparc::fixup_sparc_got10;
136 case VK_Sparc_WPLT30: return Sparc::fixup_sparc_wplt30;
137 case VK_Sparc_TLS_GD_HI22: return Sparc::fixup_sparc_tls_gd_hi22;
138 case VK_Sparc_TLS_GD_LO10: return Sparc::fixup_sparc_tls_gd_lo10;
139 case VK_Sparc_TLS_GD_ADD: return Sparc::fixup_sparc_tls_gd_add;
140 case VK_Sparc_TLS_GD_CALL: return Sparc::fixup_sparc_tls_gd_call;
141 case VK_Sparc_TLS_LDM_HI22: return Sparc::fixup_sparc_tls_ldm_hi22;
142 case VK_Sparc_TLS_LDM_LO10: return Sparc::fixup_sparc_tls_ldm_lo10;
143 case VK_Sparc_TLS_LDM_ADD: return Sparc::fixup_sparc_tls_ldm_add;
144 case VK_Sparc_TLS_LDM_CALL: return Sparc::fixup_sparc_tls_ldm_call;
145 case VK_Sparc_TLS_LDO_HIX22: return Sparc::fixup_sparc_tls_ldo_hix22;
146 case VK_Sparc_TLS_LDO_LOX10: return Sparc::fixup_sparc_tls_ldo_lox10;
147 case VK_Sparc_TLS_LDO_ADD: return Sparc::fixup_sparc_tls_ldo_add;
148 case VK_Sparc_TLS_IE_HI22: return Sparc::fixup_sparc_tls_ie_hi22;
149 case VK_Sparc_TLS_IE_LO10: return Sparc::fixup_sparc_tls_ie_lo10;
150 case VK_Sparc_TLS_IE_LD: return Sparc::fixup_sparc_tls_ie_ld;
151 case VK_Sparc_TLS_IE_LDX: return Sparc::fixup_sparc_tls_ie_ldx;
152 case VK_Sparc_TLS_IE_ADD: return Sparc::fixup_sparc_tls_ie_add;
153 case VK_Sparc_TLS_LE_HIX22: return Sparc::fixup_sparc_tls_le_hix22;
154 case VK_Sparc_TLS_LE_LOX10: return Sparc::fixup_sparc_tls_le_lox10;
155 }
156 }
157
158 bool
evaluateAsRelocatableImpl(MCValue & Res,const MCAsmLayout * Layout,const MCFixup * Fixup) const159 SparcMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
160 const MCAsmLayout *Layout,
161 const MCFixup *Fixup) const {
162 return getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup);
163 }
164
fixELFSymbolsInTLSFixupsImpl(const MCExpr * Expr,MCAssembler & Asm)165 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
166 switch (Expr->getKind()) {
167 case MCExpr::Target:
168 llvm_unreachable("Can't handle nested target expr!");
169 break;
170
171 case MCExpr::Constant:
172 break;
173
174 case MCExpr::Binary: {
175 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
176 fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
177 fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
178 break;
179 }
180
181 case MCExpr::SymbolRef: {
182 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
183 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
184 break;
185 }
186
187 case MCExpr::Unary:
188 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
189 break;
190 }
191
192 }
193
fixELFSymbolsInTLSFixups(MCAssembler & Asm) const194 void SparcMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
195 switch(getKind()) {
196 default: return;
197 case VK_Sparc_TLS_GD_HI22:
198 case VK_Sparc_TLS_GD_LO10:
199 case VK_Sparc_TLS_GD_ADD:
200 case VK_Sparc_TLS_GD_CALL:
201 case VK_Sparc_TLS_LDM_HI22:
202 case VK_Sparc_TLS_LDM_LO10:
203 case VK_Sparc_TLS_LDM_ADD:
204 case VK_Sparc_TLS_LDM_CALL:
205 case VK_Sparc_TLS_LDO_HIX22:
206 case VK_Sparc_TLS_LDO_LOX10:
207 case VK_Sparc_TLS_LDO_ADD:
208 case VK_Sparc_TLS_IE_HI22:
209 case VK_Sparc_TLS_IE_LO10:
210 case VK_Sparc_TLS_IE_LD:
211 case VK_Sparc_TLS_IE_LDX:
212 case VK_Sparc_TLS_IE_ADD:
213 case VK_Sparc_TLS_LE_HIX22:
214 case VK_Sparc_TLS_LE_LOX10: break;
215 }
216 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
217 }
218
visitUsedExpr(MCStreamer & Streamer) const219 void SparcMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
220 Streamer.visitUsedExpr(*getSubExpr());
221 }
222