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