LLVM  14.0.0git
CSKYMCExpr.cpp
Go to the documentation of this file.
1 //===-- CSKYMCExpr.cpp - CSKY specific MC expression classes -*- C++ -*----===//
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 #include "CSKYMCExpr.h"
10 #include "CSKYFixupKinds.h"
11 #include "llvm/MC/MCAssembler.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCStreamer.h"
14 #include "llvm/MC/MCSymbolELF.h"
15 
16 using namespace llvm;
17 
18 #define DEBUG_TYPE "csky-mc-expr"
19 
21  MCContext &Ctx) {
22  return new (Ctx) CSKYMCExpr(Kind, Expr);
23 }
24 
26  switch (Kind) {
27  default:
28  llvm_unreachable("Invalid ELF symbol kind");
29  case VK_CSKY_ADDR:
30  return "";
31  case VK_CSKY_PCREL:
32  return "";
33  case VK_CSKY_GOT:
34  return "@GOT";
35  case VK_CSKY_GOTPC:
36  return "@GOTPC";
37  case VK_CSKY_GOTOFF:
38  return "@GOTOFF";
39  case VK_CSKY_PLT:
40  return "@PLT";
41  case VK_CSKY_TPOFF:
42  return "@TPOFF";
43  case VK_CSKY_TLSGD:
44  return "@TLSGD";
45  }
46 }
47 
48 void CSKYMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
49  Streamer.visitUsedExpr(*getSubExpr());
50 }
51 
52 void CSKYMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
53  Expr->print(OS, MAI);
54  OS << getVariantKindName(getKind());
55 }
56 
57 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
58  switch (Expr->getKind()) {
59  case MCExpr::Target:
60  llvm_unreachable("Can't handle nested target expression");
61  break;
62  case MCExpr::Constant:
63  break;
64 
65  case MCExpr::Binary: {
66  const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
69  break;
70  }
71 
72  case MCExpr::SymbolRef: {
73  // We're known to be under a TLS fixup, so any symbol should be
74  // modified. There should be only one.
75  const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
76  cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
77  break;
78  }
79 
80  case MCExpr::Unary:
81  fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
82  break;
83  }
84 }
85 
87  switch (getKind()) {
88  default:
89  return;
90  case VK_CSKY_TPOFF:
91  case VK_CSKY_TLSGD:
92  break;
93  }
94 
96 }
97 
99  const MCAsmLayout *Layout,
100  const MCFixup *Fixup) const {
101  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
102  return false;
103 
104  // Some custom fixup types are not valid with symbol difference expressions
105  if (Res.getSymA() && Res.getSymB()) {
106  switch (getKind()) {
107  default:
108  return true;
109 
110  case VK_CSKY_ADDR:
111  case VK_CSKY_PCREL:
112  case VK_CSKY_GOT:
113  case VK_CSKY_GOTPC:
114  case VK_CSKY_GOTOFF:
115  case VK_CSKY_TPOFF:
116  case VK_CSKY_TLSGD:
117  return false;
118  }
119  }
120 
121  return true;
122 }
llvm::CSKYMCExpr::VK_CSKY_GOTOFF
@ VK_CSKY_GOTOFF
Definition: CSKYMCExpr.h:25
llvm::MCStreamer::visitUsedExpr
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:1059
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::CSKYMCExpr::VK_CSKY_GOTPC
@ VK_CSKY_GOTPC
Definition: CSKYMCExpr.h:24
llvm::CSKYMCExpr::fixELFSymbolsInTLSFixups
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
Definition: CSKYMCExpr.cpp:86
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
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::ELF::STT_TLS
@ STT_TLS
Definition: ELF.h:1156
MCAssembler.h
llvm::CSKYMCExpr::getVariantKindName
static StringRef getVariantKindName(VariantKind Kind)
Definition: CSKYMCExpr.cpp:25
llvm::CSKYMCExpr::create
static const CSKYMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: CSKYMCExpr.cpp:20
llvm::CSKYMCExpr::VK_CSKY_GOT
@ VK_CSKY_GOT
Definition: CSKYMCExpr.h:23
llvm::MCBinaryExpr
Binary assembler expressions.
Definition: MCExpr.h:479
llvm::CSKYMCExpr::evaluateAsRelocatableImpl
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
Definition: CSKYMCExpr.cpp:98
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:745
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:197
llvm::MCExpr::Target
@ Target
Target specific expression.
Definition: MCExpr.h:42
llvm::CSKYMCExpr::VK_CSKY_ADDR
@ VK_CSKY_ADDR
Definition: CSKYMCExpr.h:21
MCSymbolELF.h
MCContext.h
llvm::CSKYMCExpr::getKind
VariantKind getKind() const
Definition: CSKYMCExpr.h:44
llvm::MCBinaryExpr::getRHS
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:629
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:397
llvm::MCValue::getSymA
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:45
llvm::CSKYMCExpr::VK_CSKY_PLT
@ VK_CSKY_PLT
Definition: CSKYMCExpr.h:26
llvm::MCExpr::getKind
ExprKind getKind() const
Definition: MCExpr.h:81
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::MCExpr::Binary
@ Binary
Binary expressions.
Definition: MCExpr.h:38
llvm::MCAssembler
Definition: MCAssembler.h:60
llvm::CSKYMCExpr
Definition: CSKYMCExpr.h:17
llvm::CSKYMCExpr::VariantKind
VariantKind
Definition: CSKYMCExpr.h:19
llvm::CSKYMCExpr::getSubExpr
const MCExpr * getSubExpr() const
Definition: CSKYMCExpr.h:47
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::CSKYMCExpr::VK_CSKY_TPOFF
@ VK_CSKY_TPOFF
Definition: CSKYMCExpr.h:27
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:136
fixELFSymbolsInTLSFixupsImpl
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
Definition: CSKYMCExpr.cpp:57
llvm::CSKYMCExpr::VK_CSKY_TLSGD
@ VK_CSKY_TLSGD
Definition: CSKYMCExpr.h:28
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
llvm::CSKYMCExpr::VK_CSKY_PCREL
@ VK_CSKY_PCREL
Definition: CSKYMCExpr.h:22
CSKYFixupKinds.h
CSKYMCExpr.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::MCExpr::Unary
@ Unary
Unary expressions.
Definition: MCExpr.h:41
llvm::CSKYMCExpr::printImpl
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: CSKYMCExpr.cpp:52
MCStreamer.h
llvm::MCExpr::Constant
@ Constant
Constant expressions.
Definition: MCExpr.h:39
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:37
llvm::CSKYMCExpr::visitUsedExpr
void visitUsedExpr(MCStreamer &Streamer) const override
Definition: CSKYMCExpr.cpp:48
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
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:626