LLVM  14.0.0git
AArch64MCExpr.cpp
Go to the documentation of this file.
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:", ...).
11 //
12 //===----------------------------------------------------------------------===//
13 
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"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "aarch64symbolrefexpr"
25 
27  MCContext &Ctx) {
28  return new (Ctx) AArch64MCExpr(Expr, Kind);
29 }
30 
32  switch (static_cast<uint32_t>(getKind())) {
33  case VK_CALL: return "";
34  case VK_LO12: return ":lo12:";
35  case VK_ABS_G3: return ":abs_g3:";
36  case VK_ABS_G2: return ":abs_g2:";
37  case VK_ABS_G2_S: return ":abs_g2_s:";
38  case VK_ABS_G2_NC: return ":abs_g2_nc:";
39  case VK_ABS_G1: return ":abs_g1:";
40  case VK_ABS_G1_S: return ":abs_g1_s:";
41  case VK_ABS_G1_NC: return ":abs_g1_nc:";
42  case VK_ABS_G0: return ":abs_g0:";
43  case VK_ABS_G0_S: return ":abs_g0_s:";
44  case VK_ABS_G0_NC: return ":abs_g0_nc:";
45  case VK_PREL_G3: return ":prel_g3:";
46  case VK_PREL_G2: return ":prel_g2:";
47  case VK_PREL_G2_NC: return ":prel_g2_nc:";
48  case VK_PREL_G1: return ":prel_g1:";
49  case VK_PREL_G1_NC: return ":prel_g1_nc:";
50  case VK_PREL_G0: return ":prel_g0:";
51  case VK_PREL_G0_NC: return ":prel_g0_nc:";
52  case VK_DTPREL_G2: return ":dtprel_g2:";
53  case VK_DTPREL_G1: return ":dtprel_g1:";
54  case VK_DTPREL_G1_NC: return ":dtprel_g1_nc:";
55  case VK_DTPREL_G0: return ":dtprel_g0:";
56  case VK_DTPREL_G0_NC: return ":dtprel_g0_nc:";
57  case VK_DTPREL_HI12: return ":dtprel_hi12:";
58  case VK_DTPREL_LO12: return ":dtprel_lo12:";
59  case VK_DTPREL_LO12_NC: return ":dtprel_lo12_nc:";
60  case VK_TPREL_G2: return ":tprel_g2:";
61  case VK_TPREL_G1: return ":tprel_g1:";
62  case VK_TPREL_G1_NC: return ":tprel_g1_nc:";
63  case VK_TPREL_G0: return ":tprel_g0:";
64  case VK_TPREL_G0_NC: return ":tprel_g0_nc:";
65  case VK_TPREL_HI12: return ":tprel_hi12:";
66  case VK_TPREL_LO12: return ":tprel_lo12:";
67  case VK_TPREL_LO12_NC: return ":tprel_lo12_nc:";
68  case VK_TLSDESC_LO12: return ":tlsdesc_lo12:";
69  case VK_ABS_PAGE: return "";
70  case VK_ABS_PAGE_NC: return ":pg_hi21_nc:";
71  case VK_GOT: return ":got:";
72  case VK_GOT_PAGE: return ":got:";
73  case VK_GOT_PAGE_LO15: return ":gotpage_lo15:";
74  case VK_GOT_LO12: return ":got_lo12:";
75  case VK_GOTTPREL: return ":gottprel:";
76  case VK_GOTTPREL_PAGE: return ":gottprel:";
77  case VK_GOTTPREL_LO12_NC: return ":gottprel_lo12:";
78  case VK_GOTTPREL_G1: return ":gottprel_g1:";
79  case VK_GOTTPREL_G0_NC: return ":gottprel_g0_nc:";
80  case VK_TLSDESC: return "";
81  case VK_TLSDESC_PAGE: return ":tlsdesc:";
82  case VK_SECREL_LO12: return ":secrel_lo12:";
83  case VK_SECREL_HI12: return ":secrel_hi12:";
84  default:
85  llvm_unreachable("Invalid ELF symbol kind");
86  }
87 }
88 
89 void AArch64MCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
90  OS << getVariantKindName();
91  Expr->print(OS, MAI);
92 }
93 
95  Streamer.visitUsedExpr(*getSubExpr());
96 }
97 
99  llvm_unreachable("FIXME: what goes here?");
100 }
101 
103  const MCAsmLayout *Layout,
104  const MCFixup *Fixup) const {
105  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
106  return false;
107 
108  Res =
109  MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
110 
111  return true;
112 }
113 
115  switch (Expr->getKind()) {
116  case MCExpr::Target:
117  llvm_unreachable("Can't handle nested target expression");
118  break;
119  case MCExpr::Constant:
120  break;
121 
122  case MCExpr::Binary: {
123  const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
126  break;
127  }
128 
129  case MCExpr::SymbolRef: {
130  // We're known to be under a TLS fixup, so any symbol should be
131  // modified. There should be only one.
132  const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
133  cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
134  break;
135  }
136 
137  case MCExpr::Unary:
138  fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
139  break;
140  }
141 }
142 
144  switch (getSymbolLoc(Kind)) {
145  default:
146  return;
147  case VK_DTPREL:
148  case VK_GOTTPREL:
149  case VK_TPREL:
150  case VK_TLSDESC:
151  break;
152  }
153 
155 }
llvm::AArch64MCExpr::create
static const AArch64MCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: AArch64MCExpr.cpp:26
llvm::MCStreamer::visitUsedExpr
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:1061
llvm::AArch64MCExpr::VK_PREL_G2_NC
@ VK_PREL_G2_NC
Definition: AArch64MCExpr.h:79
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::AArch64MCExpr::VK_ABS_G0_S
@ VK_ABS_G0_S
Definition: AArch64MCExpr.h:74
llvm::AArch64MCExpr::VK_ABS_PAGE_NC
@ VK_ABS_PAGE_NC
Definition: AArch64MCExpr.h:65
llvm::AArch64MCExpr::VK_GOTTPREL_PAGE
@ VK_GOTTPREL_PAGE
Definition: AArch64MCExpr.h:95
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::AArch64MCExpr::getKind
VariantKind getKind() const
Get the kind of this expression.
Definition: AArch64MCExpr.h:134
llvm::AArch64MCExpr::findAssociatedFragment
MCFragment * findAssociatedFragment() const override
Definition: AArch64MCExpr.cpp:98
llvm::AArch64MCExpr::VK_LO12
@ VK_LO12
Definition: AArch64MCExpr.h:76
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
llvm::AArch64MCExpr::VK_TLSDESC_LO12
@ VK_TLSDESC_LO12
Definition: AArch64MCExpr.h:107
AArch64MCExpr.h
ErrorHandling.h
fixELFSymbolsInTLSFixupsImpl
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
Definition: AArch64MCExpr.cpp:114
llvm::AArch64MCExpr::VK_ABS_G1_NC
@ VK_ABS_G1_NC
Definition: AArch64MCExpr.h:72
llvm::AArch64MCExpr::VK_DTPREL_G1_NC
@ VK_DTPREL_G1_NC
Definition: AArch64MCExpr.h:89
llvm::AArch64MCExpr::VK_ABS_G1
@ VK_ABS_G1
Definition: AArch64MCExpr.h:70
llvm::AArch64MCExpr::VK_TPREL_LO12
@ VK_TPREL_LO12
Definition: AArch64MCExpr.h:105
llvm::AArch64MCExpr::VK_TPREL
@ VK_TPREL
Definition: AArch64MCExpr.h:34
llvm::MCBinaryExpr
Binary assembler expressions.
Definition: MCExpr.h:480
llvm::AArch64MCExpr::evaluateAsRelocatableImpl
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
Definition: AArch64MCExpr.cpp:102
llvm::AArch64MCExpr::VK_DTPREL
@ VK_DTPREL
Definition: AArch64MCExpr.h:32
llvm::ELF::STT_TLS
@ STT_TLS
Definition: ELF.h:1162
llvm::MCExpr::evaluateAsRelocatable
bool evaluateAsRelocatable(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:747
llvm::AArch64MCExpr::VK_GOT_PAGE
@ VK_GOT_PAGE
Definition: AArch64MCExpr.h:85
llvm::MCFragment
Definition: MCFragment.h:31
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:199
llvm::AArch64MCExpr::getSubExpr
const MCExpr * getSubExpr() const
Get the expression this modifier applies to.
Definition: AArch64MCExpr.h:137
ELF.h
llvm::AArch64MCExpr::VK_TPREL_LO12_NC
@ VK_TPREL_LO12_NC
Definition: AArch64MCExpr.h:106
llvm::MCExpr::Target
@ Target
Target specific expression.
Definition: MCExpr.h:42
MCSymbolELF.h
MCContext.h
llvm::MCBinaryExpr::getRHS
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:630
llvm::AArch64MCExpr::VK_PREL_G0_NC
@ VK_PREL_G0_NC
Definition: AArch64MCExpr.h:83
llvm::AArch64MCExpr::VK_TPREL_G0_NC
@ VK_TPREL_G0_NC
Definition: AArch64MCExpr.h:103
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCSymbolRefExpr::getSymbol
const MCSymbol & getSymbol() const
Definition: MCExpr.h:398
llvm::MCValue::getConstant
int64_t getConstant() const
Definition: MCValue.h:44
llvm::AArch64MCExpr::VK_PREL_G1_NC
@ VK_PREL_G1_NC
Definition: AArch64MCExpr.h:81
llvm::MCValue::getSymA
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:45
llvm::MCExpr::getKind
ExprKind getKind() const
Definition: MCExpr.h:81
llvm::AArch64MCExpr::VK_GOTTPREL_G0_NC
@ VK_GOTTPREL_G0_NC
Definition: AArch64MCExpr.h:98
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::AArch64MCExpr::VK_GOTTPREL_LO12_NC
@ VK_GOTTPREL_LO12_NC
Definition: AArch64MCExpr.h:96
llvm::MCExpr::Binary
@ Binary
Binary expressions.
Definition: MCExpr.h:38
llvm::AArch64MCExpr::VK_ABS_PAGE
@ VK_ABS_PAGE
Definition: AArch64MCExpr.h:64
llvm::AArch64MCExpr::VK_GOT_LO12
@ VK_GOT_LO12
Definition: AArch64MCExpr.h:84
llvm::AArch64MCExpr::VK_ABS_G2_NC
@ VK_ABS_G2_NC
Definition: AArch64MCExpr.h:69
llvm::MCAssembler
Definition: MCAssembler.h:60
llvm::AArch64MCExpr::VK_TPREL_G1
@ VK_TPREL_G1
Definition: AArch64MCExpr.h:100
llvm::AArch64MCExpr::VK_PREL_G1
@ VK_PREL_G1
Definition: AArch64MCExpr.h:80
llvm::MCValue::get
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:60
llvm::AArch64MCExpr::VK_ABS_G0_NC
@ VK_ABS_G0_NC
Definition: AArch64MCExpr.h:75
llvm::AArch64MCExpr::VK_DTPREL_HI12
@ VK_DTPREL_HI12
Definition: AArch64MCExpr.h:92
llvm::AArch64MCExpr::VK_SECREL_LO12
@ VK_SECREL_LO12
Definition: AArch64MCExpr.h:109
llvm::AArch64MCExpr::fixELFSymbolsInTLSFixups
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
Definition: AArch64MCExpr.cpp:143
llvm::AArch64MCExpr::VK_PREL_G2
@ VK_PREL_G2
Definition: AArch64MCExpr.h:78
llvm::AArch64MCExpr::VK_DTPREL_G2
@ VK_DTPREL_G2
Definition: AArch64MCExpr.h:87
llvm::AArch64MCExpr::VK_DTPREL_G0
@ VK_DTPREL_G0
Definition: AArch64MCExpr.h:90
llvm::AArch64MCExpr::VK_PREL_G0
@ VK_PREL_G0
Definition: AArch64MCExpr.h:82
llvm::AArch64MCExpr::VK_GOTTPREL_G1
@ VK_GOTTPREL_G1
Definition: AArch64MCExpr.h:97
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::AArch64MCExpr::VK_ABS_G0
@ VK_ABS_G0
Definition: AArch64MCExpr.h:73
llvm::AArch64MCExpr::VK_DTPREL_G1
@ VK_DTPREL_G1
Definition: AArch64MCExpr.h:88
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:235
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
uint32_t
llvm::AArch64MCExpr::VK_TPREL_G1_NC
@ VK_TPREL_G1_NC
Definition: AArch64MCExpr.h:101
llvm::AArch64MCExpr::VK_ABS_G2_S
@ VK_ABS_G2_S
Definition: AArch64MCExpr.h:68
llvm::AArch64MCExpr::VK_DTPREL_LO12_NC
@ VK_DTPREL_LO12_NC
Definition: AArch64MCExpr.h:94
llvm::AArch64MCExpr::VK_GOT_PAGE_LO15
@ VK_GOT_PAGE_LO15
Definition: AArch64MCExpr.h:86
llvm::AArch64MCExpr::visitUsedExpr
void visitUsedExpr(MCStreamer &Streamer) const override
Definition: AArch64MCExpr.cpp:94
llvm::AArch64MCExpr::VK_ABS_G3
@ VK_ABS_G3
Definition: AArch64MCExpr.h:66
llvm::AArch64MCExpr::VK_TPREL_G2
@ VK_TPREL_G2
Definition: AArch64MCExpr.h:99
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::AArch64MCExpr
Definition: AArch64MCExpr.h:22
llvm::AArch64MCExpr::VK_ABS_G2
@ VK_ABS_G2
Definition: AArch64MCExpr.h:67
llvm::AArch64MCExpr::printImpl
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: AArch64MCExpr.cpp:89
llvm::AArch64MCExpr::VK_PREL_G3
@ VK_PREL_G3
Definition: AArch64MCExpr.h:77
llvm::AArch64MCExpr::getVariantKindName
StringRef getVariantKindName() const
Convert the variant kind into an ELF-appropriate modifier (e.g.
Definition: AArch64MCExpr.cpp:31
llvm::AArch64MCExpr::VK_ABS_G1_S
@ VK_ABS_G1_S
Definition: AArch64MCExpr.h:71
MCValue.h
llvm::MCExpr::print
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
llvm::MCExpr::SymbolRef
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:40
llvm::AArch64MCExpr::VariantKind
VariantKind
Definition: AArch64MCExpr.h:24
llvm::MCExpr::Unary
@ Unary
Unary expressions.
Definition: MCExpr.h:41
llvm::AArch64MCExpr::VK_DTPREL_LO12
@ VK_DTPREL_LO12
Definition: AArch64MCExpr.h:93
MCStreamer.h
llvm::AArch64MCExpr::VK_TLSDESC
@ VK_TLSDESC
Definition: AArch64MCExpr.h:35
llvm::MCExpr::Constant
@ Constant
Constant expressions.
Definition: MCExpr.h:39
llvm::AArch64MCExpr::VK_TPREL_HI12
@ VK_TPREL_HI12
Definition: AArch64MCExpr.h:104
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::AArch64MCExpr::VK_TPREL_G0
@ VK_TPREL_G0
Definition: AArch64MCExpr.h:102
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:37
llvm::AArch64MCExpr::VK_TLSDESC_PAGE
@ VK_TLSDESC_PAGE
Definition: AArch64MCExpr.h:108
llvm::AArch64MCExpr::getSymbolLoc
static VariantKind getSymbolLoc(VariantKind Kind)
Definition: AArch64MCExpr.h:143
llvm::AArch64MCExpr::VK_GOTTPREL
@ VK_GOTTPREL
Definition: AArch64MCExpr.h:33
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::AArch64MCExpr::VK_DTPREL_G0_NC
@ VK_DTPREL_G0_NC
Definition: AArch64MCExpr.h:91
llvm::AArch64MCExpr::VK_SECREL_HI12
@ VK_SECREL_HI12
Definition: AArch64MCExpr.h:110
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
llvm::MCValue::getSymB
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:46
llvm::MCBinaryExpr::getLHS
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:627
llvm::AArch64MCExpr::VK_CALL
@ VK_CALL
Definition: AArch64MCExpr.h:63
llvm::AArch64MCExpr::VK_GOT
@ VK_GOT
Definition: AArch64MCExpr.h:31