LLVM  15.0.0git
VEMCExpr.cpp
Go to the documentation of this file.
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"
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 
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 
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 
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 
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)
137 }
138 
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:
159  case VK_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:
167  case VK_VE_TLS_GD_LO32:
169  case VK_VE_TPOFF_HI32:
171  case VK_VE_TPOFF_LO32:
173  }
174 }
175 
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 
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);
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 
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  }
233 }
llvm::VE::fixup_ve_reflong
@ fixup_ve_reflong
fixup_ve_reflong - 32-bit fixup corresponding to foo
Definition: VEFixupKinds.h:18
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
llvm::VE::fixup_ve_plt_hi32
@ fixup_ve_plt_hi32
fixup_ve_plt_hi32/lo32
Definition: VEFixupKinds.h:48
llvm::VEMCExpr::getSubExpr
const MCExpr * getSubExpr() const
getSubExpr - Get the child of this expression.
Definition: VEMCExpr.h:65
llvm::MCStreamer::visitUsedExpr
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:1065
llvm::VE::fixup_ve_pc_lo32
@ fixup_ve_pc_lo32
fixup_ve_pc_lo32 - 32-bit fixup corresponding to foo@pc_lo
Definition: VEFixupKinds.h:33
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::VEMCExpr::VK_VE_PLT_HI32
@ VK_VE_PLT_HI32
Definition: VEMCExpr.h:36
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:74
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:183
llvm::VEMCExpr::VariantKind
VariantKind
Definition: VEMCExpr.h:25
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::VEMCExpr::VK_VE_REFLONG
@ VK_VE_REFLONG
Definition: VEMCExpr.h:27
llvm::VEMCExpr::VK_VE_None
@ VK_VE_None
Definition: VEMCExpr.h:26
llvm::VE::fixup_ve_pc_hi32
@ fixup_ve_pc_hi32
fixup_ve_pc_hi32 - 32-bit fixup corresponding to foo@pc_hi
Definition: VEFixupKinds.h:30
MCAssembler.h
llvm::ELF::STT_TLS
@ STT_TLS
Definition: ELF.h:1197
llvm::VEMCExpr::evaluateAsRelocatableImpl
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
Definition: VEMCExpr.cpp:176
llvm::VE::fixup_ve_tls_gd_lo32
@ fixup_ve_tls_gd_lo32
Definition: VEFixupKinds.h:53
llvm::VEMCExpr::VK_VE_PC_HI32
@ VK_VE_PC_HI32
Definition: VEMCExpr.h:30
llvm::MCBinaryExpr
Binary assembler expressions.
Definition: MCExpr.h:481
llvm::VE::Fixups
Fixups
Definition: VEFixupKinds.h:16
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:749
llvm::VEMCExpr::printImpl
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: VEMCExpr.cpp:32
MCObjectStreamer.h
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:201
llvm::VEMCExpr::VK_VE_GOT_LO32
@ VK_VE_GOT_LO32
Definition: VEMCExpr.h:33
llvm::VEMCExpr::VK_VE_PLT_LO32
@ VK_VE_PLT_LO32
Definition: VEMCExpr.h:37
ELF.h
llvm::MCExpr::Target
@ Target
Target specific expression.
Definition: MCExpr.h:42
MCSymbolELF.h
MCContext.h
llvm::VEMCExpr::visitUsedExpr
void visitUsedExpr(MCStreamer &Streamer) const override
Definition: VEMCExpr.cpp:218
llvm::MCBinaryExpr::getRHS
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:631
llvm::VEMCExpr
Definition: VEMCExpr.h:23
llvm::VE::fixup_ve_lo32
@ fixup_ve_lo32
fixup_ve_lo32 - 32-bit fixup corresponding to foo@lo
Definition: VEFixupKinds.h:27
llvm::VEMCExpr::create
static const VEMCExpr * create(VariantKind Kind, const MCExpr *Expr, MCContext &Ctx)
Definition: VEMCExpr.cpp:27
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::MCSymbolRefExpr::getSymbol
const MCSymbol & getSymbol() const
Definition: MCExpr.h:399
llvm::MCValue::getConstant
int64_t getConstant() const
Definition: MCValue.h:43
llvm::MCValue::getSymA
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:44
llvm::VEMCExpr::VK_VE_LO32
@ VK_VE_LO32
Definition: VEMCExpr.h:29
llvm::VEMCExpr::getKind
VariantKind getKind() const
getOpcode - Get the kind of this expression.
Definition: VEMCExpr.h:62
llvm::MCExpr::getKind
ExprKind getKind() const
Definition: MCExpr.h:81
llvm::VEMCExpr::VK_VE_TLS_GD_LO32
@ VK_VE_TLS_GD_LO32
Definition: VEMCExpr.h:39
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::MCExpr::Binary
@ Binary
Binary expressions.
Definition: MCExpr.h:38
llvm::VEMCExpr::VK_VE_GOT_HI32
@ VK_VE_GOT_HI32
Definition: VEMCExpr.h:32
llvm::VEMCExpr::getFixupKind
VE::Fixups getFixupKind() const
getFixupKind - Get the fixup kind of this expression.
Definition: VEMCExpr.h:68
llvm::VEMCExpr::VK_VE_PC_LO32
@ VK_VE_PC_LO32
Definition: VEMCExpr.h:31
llvm::MCAssembler
Definition: MCAssembler.h:73
llvm::VEMCExpr::VK_VE_GOTOFF_LO32
@ VK_VE_GOTOFF_LO32
Definition: VEMCExpr.h:35
llvm::VE::fixup_ve_tls_gd_hi32
@ fixup_ve_tls_gd_hi32
fixups for Thread Local Storage
Definition: VEFixupKinds.h:52
llvm::VEMCExpr::printVariantKindSuffix
static void printVariantKindSuffix(raw_ostream &OS, VariantKind Kind)
Definition: VEMCExpr.cpp:70
llvm::MCValue::get
static MCValue get(const MCSymbolRefExpr *SymA, const MCSymbolRefExpr *SymB=nullptr, int64_t Val=0, uint32_t RefKind=0)
Definition: MCValue.h:59
llvm::VE::fixup_ve_gotoff_lo32
@ fixup_ve_gotoff_lo32
fixup_ve_gotoff_lo32 - 32-bit fixup corresponding to foo@gotoff_lo
Definition: VEFixupKinds.h:45
llvm::VEMCExpr::VK_VE_TPOFF_HI32
@ VK_VE_TPOFF_HI32
Definition: VEMCExpr.h:40
llvm::VE::fixup_ve_gotoff_hi32
@ fixup_ve_gotoff_hi32
fixup_ve_gotoff_hi32 - 32-bit fixup corresponding to foo@gotoff_hi
Definition: VEFixupKinds.h:42
llvm::VE::fixup_ve_hi32
@ fixup_ve_hi32
fixup_ve_hi32 - 32-bit fixup corresponding to foo@hi
Definition: VEFixupKinds.h:24
llvm::VE::fixup_ve_got_lo32
@ fixup_ve_got_lo32
fixup_ve_got_lo32 - 32-bit fixup corresponding to foo@got_lo
Definition: VEFixupKinds.h:39
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::VE::fixup_ve_got_hi32
@ fixup_ve_got_hi32
fixup_ve_got_hi32 - 32-bit fixup corresponding to foo@got_hi
Definition: VEFixupKinds.h:36
Fixup
PowerPC TLS Dynamic Call Fixup
Definition: PPCTLSDynamicCall.cpp:233
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:143
name
static const char * name
Definition: SVEIntrinsicOpts.cpp:74
VEMCExpr.h
llvm::VE::fixup_ve_tpoff_lo32
@ fixup_ve_tpoff_lo32
Definition: VEFixupKinds.h:55
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::VEMCExpr::VK_VE_TLS_GD_HI32
@ VK_VE_TLS_GD_HI32
Definition: VEMCExpr.h:38
Casting.h
llvm::VEMCExpr::fixELFSymbolsInTLSFixups
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
Definition: VEMCExpr.cpp:222
llvm::VEMCExpr::VK_VE_TPOFF_LO32
@ VK_VE_TPOFF_LO32
Definition: VEMCExpr.h:41
llvm::VE::fixup_ve_plt_lo32
@ fixup_ve_plt_lo32
Definition: VEFixupKinds.h:49
MCValue.h
llvm::MCExpr::print
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:41
llvm::MCExpr::SymbolRef
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:40
llvm::MCExpr::Unary
@ Unary
Unary expressions.
Definition: MCExpr.h:41
llvm::MCExpr::Constant
@ Constant
Constant expressions.
Definition: MCExpr.h:39
llvm::VEMCExpr::parseVariantKind
static VariantKind parseVariantKind(StringRef name)
Definition: VEMCExpr.cpp:120
llvm::VEMCExpr::printVariantKind
static bool printVariantKind(raw_ostream &OS, VariantKind Kind)
Definition: VEMCExpr.cpp:44
fixELFSymbolsInTLSFixupsImpl
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
Definition: VEMCExpr.cpp:188
llvm::VE::fixup_ve_tpoff_hi32
@ fixup_ve_tpoff_hi32
Definition: VEFixupKinds.h:54
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:36
llvm::VEMCExpr::VK_VE_HI32
@ VK_VE_HI32
Definition: VEMCExpr.h:28
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
llvm::VEMCExpr::VK_VE_GOTOFF_HI32
@ VK_VE_GOTOFF_HI32
Definition: VEMCExpr.h:34
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:45
llvm::MCBinaryExpr::getLHS
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:628