LLVM  10.0.0
RISCVMCExpr.cpp
Go to the documentation of this file.
1 //===-- RISCVMCExpr.cpp - RISCV 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 RISCV architecture (e.g. ":lo12:", ":gottprel_g1:", ...).
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "RISCVMCExpr.h"
16 #include "RISCV.h"
17 #include "RISCVFixupKinds.h"
18 #include "llvm/BinaryFormat/ELF.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSymbolELF.h"
24 #include "llvm/MC/MCValue.h"
26 
27 using namespace llvm;
28 
29 #define DEBUG_TYPE "riscvmcexpr"
30 
32  MCContext &Ctx) {
33  return new (Ctx) RISCVMCExpr(Expr, Kind);
34 }
35 
36 void RISCVMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
37  VariantKind Kind = getKind();
38  bool HasVariant = ((Kind != VK_RISCV_None) && (Kind != VK_RISCV_CALL) &&
39  (Kind != VK_RISCV_CALL_PLT));
40 
41  if (HasVariant)
42  OS << '%' << getVariantKindName(getKind()) << '(';
43  Expr->print(OS, MAI);
44  if (Kind == VK_RISCV_CALL_PLT)
45  OS << "@plt";
46  if (HasVariant)
47  OS << ')';
48 }
49 
50 const MCFixup *RISCVMCExpr::getPCRelHiFixup(const MCFragment **DFOut) const {
51  MCValue AUIPCLoc;
52  if (!getSubExpr()->evaluateAsRelocatable(AUIPCLoc, nullptr, nullptr))
53  return nullptr;
54 
55  const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
56  if (!AUIPCSRE)
57  return nullptr;
58 
59  const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
60  const auto *DF = dyn_cast_or_null<MCDataFragment>(AUIPCSymbol->getFragment());
61 
62  if (!DF)
63  return nullptr;
64 
65  uint64_t Offset = AUIPCSymbol->getOffset();
66  if (DF->getContents().size() == Offset) {
67  DF = dyn_cast_or_null<MCDataFragment>(DF->getNextNode());
68  if (!DF)
69  return nullptr;
70  Offset = 0;
71  }
72 
73  for (const MCFixup &F : DF->getFixups()) {
74  if (F.getOffset() != Offset)
75  continue;
76 
77  switch ((unsigned)F.getKind()) {
78  default:
79  continue;
84  if (DFOut)
85  *DFOut = DF;
86  return &F;
87  }
88  }
89 
90  return nullptr;
91 }
92 
94  const MCAsmLayout *Layout,
95  const MCFixup *Fixup) const {
96  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
97  return false;
98 
99  // Some custom fixup types are not valid with symbol difference expressions
100  if (Res.getSymA() && Res.getSymB()) {
101  switch (getKind()) {
102  default:
103  return true;
104  case VK_RISCV_LO:
105  case VK_RISCV_HI:
106  case VK_RISCV_PCREL_LO:
107  case VK_RISCV_PCREL_HI:
108  case VK_RISCV_GOT_HI:
109  case VK_RISCV_TPREL_LO:
110  case VK_RISCV_TPREL_HI:
111  case VK_RISCV_TPREL_ADD:
112  case VK_RISCV_TLS_GOT_HI:
113  case VK_RISCV_TLS_GD_HI:
114  return false;
115  }
116  }
117 
118  return true;
119 }
120 
121 void RISCVMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
122  Streamer.visitUsedExpr(*getSubExpr());
123 }
124 
127  .Case("lo", VK_RISCV_LO)
128  .Case("hi", VK_RISCV_HI)
129  .Case("pcrel_lo", VK_RISCV_PCREL_LO)
130  .Case("pcrel_hi", VK_RISCV_PCREL_HI)
131  .Case("got_pcrel_hi", VK_RISCV_GOT_HI)
132  .Case("tprel_lo", VK_RISCV_TPREL_LO)
133  .Case("tprel_hi", VK_RISCV_TPREL_HI)
134  .Case("tprel_add", VK_RISCV_TPREL_ADD)
135  .Case("tls_ie_pcrel_hi", VK_RISCV_TLS_GOT_HI)
136  .Case("tls_gd_pcrel_hi", VK_RISCV_TLS_GD_HI)
138 }
139 
141  switch (Kind) {
142  default:
143  llvm_unreachable("Invalid ELF symbol kind");
144  case VK_RISCV_LO:
145  return "lo";
146  case VK_RISCV_HI:
147  return "hi";
148  case VK_RISCV_PCREL_LO:
149  return "pcrel_lo";
150  case VK_RISCV_PCREL_HI:
151  return "pcrel_hi";
152  case VK_RISCV_GOT_HI:
153  return "got_pcrel_hi";
154  case VK_RISCV_TPREL_LO:
155  return "tprel_lo";
156  case VK_RISCV_TPREL_HI:
157  return "tprel_hi";
158  case VK_RISCV_TPREL_ADD:
159  return "tprel_add";
160  case VK_RISCV_TLS_GOT_HI:
161  return "tls_ie_pcrel_hi";
162  case VK_RISCV_TLS_GD_HI:
163  return "tls_gd_pcrel_hi";
164  }
165 }
166 
168  switch (Expr->getKind()) {
169  case MCExpr::Target:
170  llvm_unreachable("Can't handle nested target expression");
171  break;
172  case MCExpr::Constant:
173  break;
174 
175  case MCExpr::Binary: {
176  const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
179  break;
180  }
181 
182  case MCExpr::SymbolRef: {
183  // We're known to be under a TLS fixup, so any symbol should be
184  // modified. There should be only one.
185  const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
186  cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
187  break;
188  }
189 
190  case MCExpr::Unary:
191  fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
192  break;
193  }
194 }
195 
197  switch (getKind()) {
198  default:
199  return;
200  case VK_RISCV_TPREL_HI:
201  case VK_RISCV_TLS_GOT_HI:
202  case VK_RISCV_TLS_GD_HI:
203  break;
204  }
205 
207 }
208 
209 bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
210  MCValue Value;
211 
212  if (Kind == VK_RISCV_PCREL_HI || Kind == VK_RISCV_PCREL_LO ||
213  Kind == VK_RISCV_GOT_HI || Kind == VK_RISCV_TPREL_HI ||
214  Kind == VK_RISCV_TPREL_LO || Kind == VK_RISCV_TPREL_ADD ||
215  Kind == VK_RISCV_TLS_GOT_HI || Kind == VK_RISCV_TLS_GD_HI ||
216  Kind == VK_RISCV_CALL || Kind == VK_RISCV_CALL_PLT)
217  return false;
218 
219  if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr))
220  return false;
221 
222  if (!Value.isAbsolute())
223  return false;
224 
225  Res = evaluateAsInt64(Value.getConstant());
226  return true;
227 }
228 
229 int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value) const {
230  switch (Kind) {
231  default:
232  llvm_unreachable("Invalid kind");
233  case VK_RISCV_LO:
234  return SignExtend64<12>(Value);
235  case VK_RISCV_HI:
236  // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
237  return ((Value + 0x800) >> 12) & 0xfffff;
238  }
239 }
bool evaluateAsConstant(int64_t &Res) const
static const char * name
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:39
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: RISCVMCExpr.cpp:36
VariantKind getKind() const
Definition: RISCVMCExpr.h:56
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:570
bool isAbsolute() const
Is this an absolute (as opposed to relocatable) value.
Definition: MCValue.h:52
F(f)
static StringRef getVariantKindName(VariantKind Kind)
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
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
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:169
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
const MCExpr * getSubExpr() const
Definition: RISCVMCExpr.h:58
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
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
Streaming machine code generation interface.
Definition: MCStreamer.h:197
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
uint64_t getOffset() const
Definition: MCSymbol.h:316
Binary assembler expressions.
Definition: MCExpr.h:423
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
const MCSymbol & getSymbol() const
Definition: MCExpr.h:342
ExprKind getKind() const
Definition: MCExpr.h:68
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:388
const MCFixup * getPCRelHiFixup(const MCFragment **DFOut) const
Get the corresponding PC-relative HI fixup that a VK_RISCV_PCREL_LO points to, and optionally the fra...
Definition: RISCVMCExpr.cpp:50
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:989
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
Definition: RISCVMCExpr.cpp:93
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
static VariantKind getVariantKindForName(StringRef name)
References to labels and assigned expressions.
Definition: MCExpr.h:40
LLVM Value Representation.
Definition: Value.h:74
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:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
Target specific expression.
Definition: MCExpr.h:42
void visitUsedExpr(MCStreamer &Streamer) const override
static const RISCVMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: RISCVMCExpr.cpp:31