LLVM  10.0.0svn
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_LO12: return ":got_lo12:";
74  case VK_GOTTPREL: return ":gottprel:";
75  case VK_GOTTPREL_PAGE: return ":gottprel:";
76  case VK_GOTTPREL_LO12_NC: return ":gottprel_lo12:";
77  case VK_GOTTPREL_G1: return ":gottprel_g1:";
78  case VK_GOTTPREL_G0_NC: return ":gottprel_g0_nc:";
79  case VK_TLSDESC: return "";
80  case VK_TLSDESC_PAGE: return ":tlsdesc:";
81  case VK_SECREL_LO12: return ":secrel_lo12:";
82  case VK_SECREL_HI12: return ":secrel_hi12:";
83  default:
84  llvm_unreachable("Invalid ELF symbol kind");
85  }
86 }
87 
88 void AArch64MCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
89  OS << getVariantKindName();
90  Expr->print(OS, MAI);
91 }
92 
94  Streamer.visitUsedExpr(*getSubExpr());
95 }
96 
98  llvm_unreachable("FIXME: what goes here?");
99 }
100 
102  const MCAsmLayout *Layout,
103  const MCFixup *Fixup) const {
104  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
105  return false;
106 
107  Res =
108  MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), getKind());
109 
110  return true;
111 }
112 
114  switch (Expr->getKind()) {
115  case MCExpr::Target:
116  llvm_unreachable("Can't handle nested target expression");
117  break;
118  case MCExpr::Constant:
119  break;
120 
121  case MCExpr::Binary: {
122  const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
125  break;
126  }
127 
128  case MCExpr::SymbolRef: {
129  // We're known to be under a TLS fixup, so any symbol should be
130  // modified. There should be only one.
131  const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
132  cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
133  break;
134  }
135 
136  case MCExpr::Unary:
137  fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
138  break;
139  }
140 }
141 
143  switch (getSymbolLoc(Kind)) {
144  default:
145  return;
146  case VK_DTPREL:
147  case VK_GOTTPREL:
148  case VK_TPREL:
149  case VK_TLSDESC:
150  break;
151  }
152 
154 }
VariantKind getKind() const
Get the kind of this expression.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:39
static const AArch64MCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:570
StringRef getVariantKindName() const
Convert the variant kind into an ELF-appropriate modifier (e.g.
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:77
int64_t getConstant() const
Definition: MCValue.h:46
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:48
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
Context object for machine code objects.
Definition: MCContext.h:65
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:669
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:573
Unary expressions.
Definition: MCExpr.h:41
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
Streaming machine code generation interface.
Definition: MCStreamer.h:196
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
const MCExpr * getSubExpr() const
Get the expression this modifier applies to.
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
void visitUsedExpr(MCStreamer &Streamer) const override
Binary assembler expressions.
Definition: MCExpr.h:423
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const MCSymbol & getSymbol() const
Definition: MCExpr.h:342
ExprKind getKind() const
Definition: MCExpr.h:68
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:988
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:62
References to labels and assigned expressions.
Definition: MCExpr.h:40
static VariantKind getSymbolLoc(VariantKind Kind)
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Constant expressions.
Definition: MCExpr.h:39
Binary expressions.
Definition: MCExpr.h:38
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Target specific expression.
Definition: MCExpr.h:42
MCFragment * findAssociatedFragment() const override