LLVM  9.0.0svn
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 
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;
82  return &F;
83  }
84  }
85 
86  return nullptr;
87 }
88 
89 bool RISCVMCExpr::evaluatePCRelLo(MCValue &Res, const MCAsmLayout *Layout,
90  const MCFixup *Fixup) const {
91  // VK_RISCV_PCREL_LO has to be handled specially. The MCExpr inside is
92  // actually the location of a auipc instruction with a VK_RISCV_PCREL_HI fixup
93  // pointing to the real target. We need to generate an MCValue in the form of
94  // (<real target> + <offset from this fixup to the auipc fixup>). The Fixup
95  // is pcrel relative to the VK_RISCV_PCREL_LO fixup, so we need to add the
96  // offset to the VK_RISCV_PCREL_HI Fixup from VK_RISCV_PCREL_LO to correct.
97 
98  // Don't try to evaluate if the fixup will be forced as a relocation (e.g.
99  // as linker relaxation is enabled). If we evaluated pcrel_lo in this case,
100  // the modified fixup will be converted into a relocation that no longer
101  // points to the pcrel_hi as the linker requires.
102  auto &RAB =
103  static_cast<RISCVAsmBackend &>(Layout->getAssembler().getBackend());
104  if (RAB.willForceRelocations())
105  return false;
106 
107  MCValue AUIPCLoc;
108  if (!getSubExpr()->evaluateAsValue(AUIPCLoc, *Layout))
109  return false;
110 
111  const MCSymbolRefExpr *AUIPCSRE = AUIPCLoc.getSymA();
112  // Don't try to evaluate %pcrel_hi/%pcrel_lo pairs that cross fragment
113  // boundries.
114  if (!AUIPCSRE ||
116  return false;
117 
118  const MCSymbol *AUIPCSymbol = &AUIPCSRE->getSymbol();
119  if (!AUIPCSymbol)
120  return false;
121 
122  const MCFixup *TargetFixup = getPCRelHiFixup();
123  if (!TargetFixup)
124  return false;
125 
126  if ((unsigned)TargetFixup->getKind() != RISCV::fixup_riscv_pcrel_hi20)
127  return false;
128 
129  MCValue Target;
130  if (!TargetFixup->getValue()->evaluateAsValue(Target, *Layout))
131  return false;
132 
133  if (!Target.getSymA() || !Target.getSymA()->getSymbol().isInSection())
134  return false;
135 
136  if (&Target.getSymA()->getSymbol().getSection() !=
138  return false;
139 
140  uint64_t AUIPCOffset = AUIPCSymbol->getOffset();
141 
142  Res = MCValue::get(Target.getSymA(), nullptr,
143  Target.getConstant() + (Fixup->getOffset() - AUIPCOffset));
144  return true;
145 }
146 
148  const MCAsmLayout *Layout,
149  const MCFixup *Fixup) const {
150  if (Kind == VK_RISCV_PCREL_LO && evaluatePCRelLo(Res, Layout, Fixup))
151  return true;
152 
153  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
154  return false;
155 
156  // Some custom fixup types are not valid with symbol difference expressions
157  if (Res.getSymA() && Res.getSymB()) {
158  switch (getKind()) {
159  default:
160  return true;
161  case VK_RISCV_LO:
162  case VK_RISCV_HI:
163  case VK_RISCV_PCREL_LO:
164  case VK_RISCV_PCREL_HI:
165  case VK_RISCV_GOT_HI:
166  case VK_RISCV_TPREL_LO:
167  case VK_RISCV_TPREL_HI:
168  case VK_RISCV_TPREL_ADD:
169  return false;
170  }
171  }
172 
173  return true;
174 }
175 
176 void RISCVMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
177  Streamer.visitUsedExpr(*getSubExpr());
178 }
179 
182  .Case("lo", VK_RISCV_LO)
183  .Case("hi", VK_RISCV_HI)
184  .Case("pcrel_lo", VK_RISCV_PCREL_LO)
185  .Case("pcrel_hi", VK_RISCV_PCREL_HI)
186  .Case("got_pcrel_hi", VK_RISCV_GOT_HI)
187  .Case("tprel_lo", VK_RISCV_TPREL_LO)
188  .Case("tprel_hi", VK_RISCV_TPREL_HI)
189  .Case("tprel_add", VK_RISCV_TPREL_ADD)
191 }
192 
194  switch (Kind) {
195  default:
196  llvm_unreachable("Invalid ELF symbol kind");
197  case VK_RISCV_LO:
198  return "lo";
199  case VK_RISCV_HI:
200  return "hi";
201  case VK_RISCV_PCREL_LO:
202  return "pcrel_lo";
203  case VK_RISCV_PCREL_HI:
204  return "pcrel_hi";
205  case VK_RISCV_GOT_HI:
206  return "got_pcrel_hi";
207  case VK_RISCV_TPREL_LO:
208  return "tprel_lo";
209  case VK_RISCV_TPREL_HI:
210  return "tprel_hi";
211  case VK_RISCV_TPREL_ADD:
212  return "tprel_add";
213  }
214 }
215 
217  switch (Expr->getKind()) {
218  case MCExpr::Target:
219  llvm_unreachable("Can't handle nested target expression");
220  break;
221  case MCExpr::Constant:
222  break;
223 
224  case MCExpr::Binary: {
225  const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
228  break;
229  }
230 
231  case MCExpr::SymbolRef: {
232  // We're known to be under a TLS fixup, so any symbol should be
233  // modified. There should be only one.
234  const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
235  cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
236  break;
237  }
238 
239  case MCExpr::Unary:
240  fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
241  break;
242  }
243 }
244 
246  switch (getKind()) {
247  default:
248  return;
249  case VK_RISCV_TPREL_HI:
250  break;
251  }
252 
254 }
255 
256 bool RISCVMCExpr::evaluateAsConstant(int64_t &Res) const {
257  MCValue Value;
258 
259  if (Kind == VK_RISCV_PCREL_HI || Kind == VK_RISCV_PCREL_LO ||
260  Kind == VK_RISCV_GOT_HI || Kind == VK_RISCV_TPREL_HI ||
261  Kind == VK_RISCV_TPREL_LO || Kind == VK_RISCV_TPREL_ADD ||
262  Kind == VK_RISCV_CALL || Kind == VK_RISCV_CALL_PLT)
263  return false;
264 
265  if (!getSubExpr()->evaluateAsRelocatable(Value, nullptr, nullptr))
266  return false;
267 
268  if (!Value.isAbsolute())
269  return false;
270 
271  Res = evaluateAsInt64(Value.getConstant());
272  return true;
273 }
274 
275 int64_t RISCVMCExpr::evaluateAsInt64(int64_t Value) const {
276  switch (Kind) {
277  default:
278  llvm_unreachable("Invalid kind");
279  case VK_RISCV_LO:
280  return SignExtend64<12>(Value);
281  case VK_RISCV_HI:
282  // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
283  return ((Value + 0x800) >> 12) & 0xfffff;
284  }
285 }
bool evaluateAsConstant(int64_t &Res) const
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:562
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:73
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
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:251
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
Context object for machine code objects.
Definition: MCContext.h:62
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:644
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:565
const MCFixup * getPCRelHiFixup() const
Get the corresponding PC-relative HI fixup that a VK_RISCV_PCREL_LO points to.
Definition: RISCVMCExpr.cpp:50
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:50
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:55
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
Streaming machine code generation interface.
Definition: MCStreamer.h:188
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:41
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:47
uint64_t getOffset() const
Definition: MCSymbol.h:320
uint32_t getOffset() const
Definition: MCFixup.h:124
Binary assembler expressions.
Definition: MCExpr.h:415
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
PowerPC TLS Dynamic Call Fixup
MCAsmBackend & getBackend() const
Definition: MCAssembler.h:292
const MCSymbol & getSymbol() const
Definition: MCExpr.h:334
ExprKind getKind() const
Definition: MCExpr.h:72
MCFragment * getFragment(bool SetUsed=true) const
Definition: MCSymbol.h:383
MCFragment * findAssociatedFragment() const
Find the "associated section" for this expression, which is currently defined as the absolute section...
Definition: MCExpr.cpp:864
MCFragment * findAssociatedFragment() const override
Definition: RISCVMCExpr.h:71
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:934
MCSection * getParent() const
Definition: MCFragment.h:98
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:266
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
static VariantKind getVariantKindForName(StringRef name)
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
bool evaluateAsValue(MCValue &Res, const MCAsmLayout &Layout) const
Try to evaluate the expression to the form (a - b + constant) where neither a nor b are variables...
Definition: MCExpr.cpp:652
LLVM Value Representation.
Definition: Value.h:72
static const char * name
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
const MCExpr * getValue() const
Definition: MCFixup.h:127
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
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
MCFixupKind getKind() const
Definition: MCFixup.h:122