[FastISel] Support unreachable with NoTrapAfterNoReturn (#118296)
[llvm-project.git] / llvm / lib / Target / AArch64 / MCTargetDesc / AArch64MCExpr.cpp
blob3430b9002894f005882f57d5e67386caffc2309a
1 //===-- AArch64MCExpr.cpp - AArch64 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 AArch64 architecture (e.g. ":lo12:", ":gottprel_g1:", ...).
12 //===----------------------------------------------------------------------===//
14 #include "AArch64MCExpr.h"
15 #include "llvm/BinaryFormat/ELF.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/MC/MCSymbolELF.h"
19 #include "llvm/MC/MCValue.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/ErrorHandling.h"
23 using namespace llvm;
25 #define DEBUG_TYPE "aarch64symbolrefexpr"
27 const AArch64MCExpr *AArch64MCExpr::create(const MCExpr *Expr, VariantKind Kind,
28 MCContext &Ctx) {
29 return new (Ctx) AArch64MCExpr(Expr, Kind);
32 StringRef AArch64MCExpr::getVariantKindName() const {
33 // clang-format off
34 switch (static_cast<uint32_t>(getKind())) {
35 case VK_CALL: return "";
36 case VK_LO12: return ":lo12:";
37 case VK_ABS_G3: return ":abs_g3:";
38 case VK_ABS_G2: return ":abs_g2:";
39 case VK_ABS_G2_S: return ":abs_g2_s:";
40 case VK_ABS_G2_NC: return ":abs_g2_nc:";
41 case VK_ABS_G1: return ":abs_g1:";
42 case VK_ABS_G1_S: return ":abs_g1_s:";
43 case VK_ABS_G1_NC: return ":abs_g1_nc:";
44 case VK_ABS_G0: return ":abs_g0:";
45 case VK_ABS_G0_S: return ":abs_g0_s:";
46 case VK_ABS_G0_NC: return ":abs_g0_nc:";
47 case VK_PREL_G3: return ":prel_g3:";
48 case VK_PREL_G2: return ":prel_g2:";
49 case VK_PREL_G2_NC: return ":prel_g2_nc:";
50 case VK_PREL_G1: return ":prel_g1:";
51 case VK_PREL_G1_NC: return ":prel_g1_nc:";
52 case VK_PREL_G0: return ":prel_g0:";
53 case VK_PREL_G0_NC: return ":prel_g0_nc:";
54 case VK_DTPREL_G2: return ":dtprel_g2:";
55 case VK_DTPREL_G1: return ":dtprel_g1:";
56 case VK_DTPREL_G1_NC: return ":dtprel_g1_nc:";
57 case VK_DTPREL_G0: return ":dtprel_g0:";
58 case VK_DTPREL_G0_NC: return ":dtprel_g0_nc:";
59 case VK_DTPREL_HI12: return ":dtprel_hi12:";
60 case VK_DTPREL_LO12: return ":dtprel_lo12:";
61 case VK_DTPREL_LO12_NC: return ":dtprel_lo12_nc:";
62 case VK_TPREL_G2: return ":tprel_g2:";
63 case VK_TPREL_G1: return ":tprel_g1:";
64 case VK_TPREL_G1_NC: return ":tprel_g1_nc:";
65 case VK_TPREL_G0: return ":tprel_g0:";
66 case VK_TPREL_G0_NC: return ":tprel_g0_nc:";
67 case VK_TPREL_HI12: return ":tprel_hi12:";
68 case VK_TPREL_LO12: return ":tprel_lo12:";
69 case VK_TPREL_LO12_NC: return ":tprel_lo12_nc:";
70 case VK_TLSDESC_LO12: return ":tlsdesc_lo12:";
71 case VK_ABS_PAGE: return "";
72 case VK_ABS_PAGE_NC: return ":pg_hi21_nc:";
73 case VK_GOT: return ":got:";
74 case VK_GOT_PAGE: return ":got:";
75 case VK_GOT_PAGE_LO15: return ":gotpage_lo15:";
76 case VK_GOT_LO12: return ":got_lo12:";
77 case VK_GOTTPREL: return ":gottprel:";
78 case VK_GOTTPREL_PAGE: return ":gottprel:";
79 case VK_GOTTPREL_LO12_NC: return ":gottprel_lo12:";
80 case VK_GOTTPREL_G1: return ":gottprel_g1:";
81 case VK_GOTTPREL_G0_NC: return ":gottprel_g0_nc:";
82 case VK_TLSDESC: return "";
83 case VK_TLSDESC_PAGE: return ":tlsdesc:";
84 case VK_SECREL_LO12: return ":secrel_lo12:";
85 case VK_SECREL_HI12: return ":secrel_hi12:";
86 case VK_GOT_AUTH: return ":got_auth:";
87 case VK_GOT_AUTH_PAGE: return ":got_auth:";
88 case VK_GOT_AUTH_LO12: return ":got_auth_lo12:";
89 default:
90 llvm_unreachable("Invalid ELF symbol kind");
92 // clang-format on
95 void AArch64MCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
96 OS << getVariantKindName();
97 Expr->print(OS, MAI);
100 void AArch64MCExpr::visitUsedExpr(MCStreamer &Streamer) const {
101 Streamer.visitUsedExpr(*getSubExpr());
104 MCFragment *AArch64MCExpr::findAssociatedFragment() const {
105 llvm_unreachable("FIXME: what goes here?");
108 bool AArch64MCExpr::evaluateAsRelocatableImpl(MCValue &Res,
109 const MCAssembler *Asm,
110 const MCFixup *Fixup) const {
111 if (!getSubExpr()->evaluateAsRelocatable(Res, Asm, Fixup))
112 return false;
114 Res =
115 MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
117 return true;
120 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
121 switch (Expr->getKind()) {
122 case MCExpr::Target:
123 llvm_unreachable("Can't handle nested target expression");
124 break;
125 case MCExpr::Constant:
126 break;
128 case MCExpr::Binary: {
129 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
130 fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
131 fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
132 break;
135 case MCExpr::SymbolRef: {
136 // We're known to be under a TLS fixup, so any symbol should be
137 // modified. There should be only one.
138 const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
139 cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
140 break;
143 case MCExpr::Unary:
144 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
145 break;
149 void AArch64MCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
150 switch (getSymbolLoc(Kind)) {
151 default:
152 return;
153 case VK_DTPREL:
154 case VK_GOTTPREL:
155 case VK_TPREL:
156 case VK_TLSDESC:
157 break;
160 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm);
163 const AArch64AuthMCExpr *AArch64AuthMCExpr::create(const MCExpr *Expr,
164 uint16_t Discriminator,
165 AArch64PACKey::ID Key,
166 bool HasAddressDiversity,
167 MCContext &Ctx) {
168 return new (Ctx)
169 AArch64AuthMCExpr(Expr, Discriminator, Key, HasAddressDiversity);
172 void AArch64AuthMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
173 bool WrapSubExprInParens = !isa<MCSymbolRefExpr>(getSubExpr());
174 if (WrapSubExprInParens)
175 OS << '(';
176 getSubExpr()->print(OS, MAI);
177 if (WrapSubExprInParens)
178 OS << ')';
180 OS << "@AUTH(" << AArch64PACKeyIDToString(Key) << ',' << Discriminator;
181 if (hasAddressDiversity())
182 OS << ",addr";
183 OS << ')';
186 void AArch64AuthMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
187 Streamer.visitUsedExpr(*getSubExpr());
190 MCFragment *AArch64AuthMCExpr::findAssociatedFragment() const {
191 llvm_unreachable("FIXME: what goes here?");
194 bool AArch64AuthMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
195 const MCAssembler *Asm,
196 const MCFixup *Fixup) const {
197 if (!getSubExpr()->evaluateAsRelocatable(Res, Asm, Fixup))
198 return false;
200 if (Res.getSymB())
201 report_fatal_error("Auth relocation can't reference two symbols");
203 Res = MCValue::get(Res.getSymA(), nullptr, Res.getConstant(), getKind());
204 return true;