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 | #include "llvm/Support/Casting.h" |
22 | |
23 | using namespace llvm; |
24 | |
25 | #define DEBUG_TYPE "vemcexpr" |
26 | |
27 | const VEMCExpr *VEMCExpr::create(VariantKind Kind, const MCExpr *Expr, |
28 | MCContext &Ctx) { |
29 | return new (Ctx) VEMCExpr(Kind, Expr); |
30 | } |
31 | |
32 | void VEMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const { |
33 | |
34 | bool closeParen = printVariantKind(OS, Kind); |
35 | |
36 | const MCExpr *Expr = getSubExpr(); |
37 | Expr->print(OS, MAI); |
38 | |
39 | if (closeParen) |
40 | OS << ')'; |
41 | printVariantKindSuffix(OS, Kind); |
42 | } |
43 | |
44 | bool VEMCExpr::printVariantKind(raw_ostream &OS, VariantKind Kind) { |
45 | switch (Kind) { |
46 | case VK_VE_None: |
47 | case VK_VE_REFLONG: |
48 | return false; |
49 | |
50 | case VK_VE_HI32: |
51 | case VK_VE_LO32: |
52 | case VK_VE_PC_HI32: |
53 | case VK_VE_PC_LO32: |
54 | case VK_VE_GOT_HI32: |
55 | case VK_VE_GOT_LO32: |
56 | case VK_VE_GOTOFF_HI32: |
57 | case VK_VE_GOTOFF_LO32: |
58 | case VK_VE_PLT_HI32: |
59 | case VK_VE_PLT_LO32: |
60 | case VK_VE_TLS_GD_HI32: |
61 | case VK_VE_TLS_GD_LO32: |
62 | case VK_VE_TPOFF_HI32: |
63 | case VK_VE_TPOFF_LO32: |
64 | // Use suffix for these variant kinds |
65 | return false; |
66 | } |
67 | return true; |
68 | } |
69 | |
70 | void VEMCExpr::printVariantKindSuffix(raw_ostream &OS, VariantKind Kind) { |
71 | switch (Kind) { |
72 | case VK_VE_None: |
73 | case VK_VE_REFLONG: |
74 | break; |
75 | case VK_VE_HI32: |
76 | OS << "@hi" ; |
77 | break; |
78 | case VK_VE_LO32: |
79 | OS << "@lo" ; |
80 | break; |
81 | case VK_VE_PC_HI32: |
82 | OS << "@pc_hi" ; |
83 | break; |
84 | case VK_VE_PC_LO32: |
85 | OS << "@pc_lo" ; |
86 | break; |
87 | case VK_VE_GOT_HI32: |
88 | OS << "@got_hi" ; |
89 | break; |
90 | case VK_VE_GOT_LO32: |
91 | OS << "@got_lo" ; |
92 | break; |
93 | case VK_VE_GOTOFF_HI32: |
94 | OS << "@gotoff_hi" ; |
95 | break; |
96 | case VK_VE_GOTOFF_LO32: |
97 | OS << "@gotoff_lo" ; |
98 | break; |
99 | case VK_VE_PLT_HI32: |
100 | OS << "@plt_hi" ; |
101 | break; |
102 | case VK_VE_PLT_LO32: |
103 | OS << "@plt_lo" ; |
104 | break; |
105 | case VK_VE_TLS_GD_HI32: |
106 | OS << "@tls_gd_hi" ; |
107 | break; |
108 | case VK_VE_TLS_GD_LO32: |
109 | OS << "@tls_gd_lo" ; |
110 | break; |
111 | case VK_VE_TPOFF_HI32: |
112 | OS << "@tpoff_hi" ; |
113 | break; |
114 | case VK_VE_TPOFF_LO32: |
115 | OS << "@tpoff_lo" ; |
116 | break; |
117 | } |
118 | } |
119 | |
120 | VEMCExpr::VariantKind VEMCExpr::parseVariantKind(StringRef name) { |
121 | return StringSwitch<VEMCExpr::VariantKind>(name) |
122 | .Case(S: "hi" , Value: VK_VE_HI32) |
123 | .Case(S: "lo" , Value: VK_VE_LO32) |
124 | .Case(S: "pc_hi" , Value: VK_VE_PC_HI32) |
125 | .Case(S: "pc_lo" , Value: VK_VE_PC_LO32) |
126 | .Case(S: "got_hi" , Value: VK_VE_GOT_HI32) |
127 | .Case(S: "got_lo" , Value: VK_VE_GOT_LO32) |
128 | .Case(S: "gotoff_hi" , Value: VK_VE_GOTOFF_HI32) |
129 | .Case(S: "gotoff_lo" , Value: VK_VE_GOTOFF_LO32) |
130 | .Case(S: "plt_hi" , Value: VK_VE_PLT_HI32) |
131 | .Case(S: "plt_lo" , Value: VK_VE_PLT_LO32) |
132 | .Case(S: "tls_gd_hi" , Value: VK_VE_TLS_GD_HI32) |
133 | .Case(S: "tls_gd_lo" , Value: VK_VE_TLS_GD_LO32) |
134 | .Case(S: "tpoff_hi" , Value: VK_VE_TPOFF_HI32) |
135 | .Case(S: "tpoff_lo" , Value: VK_VE_TPOFF_LO32) |
136 | .Default(Value: VK_VE_None); |
137 | } |
138 | |
139 | VE::Fixups VEMCExpr::getFixupKind(VEMCExpr::VariantKind Kind) { |
140 | switch (Kind) { |
141 | default: |
142 | llvm_unreachable("Unhandled VEMCExpr::VariantKind" ); |
143 | case VK_VE_REFLONG: |
144 | return VE::fixup_ve_reflong; |
145 | case VK_VE_HI32: |
146 | return VE::fixup_ve_hi32; |
147 | case VK_VE_LO32: |
148 | return VE::fixup_ve_lo32; |
149 | case VK_VE_PC_HI32: |
150 | return VE::fixup_ve_pc_hi32; |
151 | case VK_VE_PC_LO32: |
152 | return VE::fixup_ve_pc_lo32; |
153 | case VK_VE_GOT_HI32: |
154 | return VE::fixup_ve_got_hi32; |
155 | case VK_VE_GOT_LO32: |
156 | return VE::fixup_ve_got_lo32; |
157 | case VK_VE_GOTOFF_HI32: |
158 | return VE::fixup_ve_gotoff_hi32; |
159 | case VK_VE_GOTOFF_LO32: |
160 | return VE::fixup_ve_gotoff_lo32; |
161 | case VK_VE_PLT_HI32: |
162 | return VE::fixup_ve_plt_hi32; |
163 | case VK_VE_PLT_LO32: |
164 | return VE::fixup_ve_plt_lo32; |
165 | case VK_VE_TLS_GD_HI32: |
166 | return VE::fixup_ve_tls_gd_hi32; |
167 | case VK_VE_TLS_GD_LO32: |
168 | return VE::fixup_ve_tls_gd_lo32; |
169 | case VK_VE_TPOFF_HI32: |
170 | return VE::fixup_ve_tpoff_hi32; |
171 | case VK_VE_TPOFF_LO32: |
172 | return VE::fixup_ve_tpoff_lo32; |
173 | } |
174 | } |
175 | |
176 | bool VEMCExpr::evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, |
177 | const MCFixup *Fixup) const { |
178 | if (!getSubExpr()->evaluateAsRelocatable(Res, Asm, Fixup)) |
179 | return false; |
180 | |
181 | Res = |
182 | MCValue::get(SymA: Res.getSymA(), SymB: Res.getSymB(), Val: Res.getConstant(), RefKind: 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>(Val: Expr); |
198 | fixELFSymbolsInTLSFixupsImpl(Expr: BE->getLHS(), Asm); |
199 | fixELFSymbolsInTLSFixupsImpl(Expr: 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>(Val: Expr); |
207 | cast<MCSymbolELF>(Val: SymRef.getSymbol()).setType(ELF::STT_TLS); |
208 | break; |
209 | } |
210 | |
211 | case MCExpr::Unary: |
212 | fixELFSymbolsInTLSFixupsImpl(Expr: cast<MCUnaryExpr>(Val: Expr)->getSubExpr(), Asm); |
213 | break; |
214 | } |
215 | } |
216 | |
217 | void VEMCExpr::visitUsedExpr(MCStreamer &Streamer) const { |
218 | Streamer.visitUsedExpr(Expr: *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(Expr: getSubExpr(), Asm); |
232 | } |
233 | |