xref: /freebsd/contrib/llvm-project/llvm/lib/Target/VE/MCTargetDesc/VEMCExpr.cpp (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
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("hi", VK_VE_HI32)
123       .Case("lo", VK_VE_LO32)
124       .Case("pc_hi", VK_VE_PC_HI32)
125       .Case("pc_lo", VK_VE_PC_LO32)
126       .Case("got_hi", VK_VE_GOT_HI32)
127       .Case("got_lo", VK_VE_GOT_LO32)
128       .Case("gotoff_hi", VK_VE_GOTOFF_HI32)
129       .Case("gotoff_lo", VK_VE_GOTOFF_LO32)
130       .Case("plt_hi", VK_VE_PLT_HI32)
131       .Case("plt_lo", VK_VE_PLT_LO32)
132       .Case("tls_gd_hi", VK_VE_TLS_GD_HI32)
133       .Case("tls_gd_lo", VK_VE_TLS_GD_LO32)
134       .Case("tpoff_hi", VK_VE_TPOFF_HI32)
135       .Case("tpoff_lo", VK_VE_TPOFF_LO32)
136       .Default(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,
177                                          const MCAsmLayout *Layout,
178                                          const MCFixup *Fixup) const {
179   if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
180     return false;
181 
182   Res =
183       MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
184 
185   return true;
186 }
187 
188 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
189   switch (Expr->getKind()) {
190   case MCExpr::Target:
191     llvm_unreachable("Can't handle nested target expr!");
192     break;
193 
194   case MCExpr::Constant:
195     break;
196 
197   case MCExpr::Binary: {
198     const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
199     fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
200     fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
201     break;
202   }
203 
204   case MCExpr::SymbolRef: {
205     // We're known to be under a TLS fixup, so any symbol should be
206     // modified. There should be only one.
207     const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
208     cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
209     break;
210   }
211 
212   case MCExpr::Unary:
213     fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
214     break;
215   }
216 }
217 
218 void VEMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
219   Streamer.visitUsedExpr(*getSubExpr());
220 }
221 
222 void VEMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
223   switch (getKind()) {
224   default:
225     return;
226   case VK_VE_TLS_GD_HI32:
227   case VK_VE_TLS_GD_LO32:
228   case VK_VE_TPOFF_HI32:
229   case VK_VE_TPOFF_LO32:
230     break;
231   }
232   fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
233 }
234