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_None:
30  case VK_CSKY_ADDR:
31  return "";
32  case VK_CSKY_ADDR_HI16:
33  return "@HI16";
34  case VK_CSKY_ADDR_LO16:
35  return "@LO16";
37  case VK_CSKY_GOT:
38  return "@GOT";
39  case VK_CSKY_GOTPC:
40  return "@GOTPC";
41  case VK_CSKY_GOTOFF:
42  return "@GOTOFF";
44  case VK_CSKY_PLT:
45  return "@PLT";
46  case VK_CSKY_TLSLE:
47  return "@TPOFF";
48  case VK_CSKY_TLSIE:
49  return "@GOTTPOFF";
50  case VK_CSKY_TLSGD:
51  return "@TLSGD32";
52  case VK_CSKY_TLSLDO:
53  return "@TLSLDO32";
54  case VK_CSKY_TLSLDM:
55  return "@TLSLDM32";
56  }
57 }
58 
59 void CSKYMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
60  Streamer.visitUsedExpr(*getSubExpr());
61 }
62 
63 void CSKYMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
64  Expr->print(OS, MAI);
65  OS << getVariantKindName(getKind());
66 }
67 
68 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
69  switch (Expr->getKind()) {
70  case MCExpr::Target:
71  llvm_unreachable("Can't handle nested target expression");
72  break;
73  case MCExpr::Constant:
74  break;
75 
76  case MCExpr::Binary: {
77  const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
80  break;
81  }
82 
83  case MCExpr::SymbolRef: {
84  // We're known to be under a TLS fixup, so any symbol should be
85  // modified. There should be only one.
86  const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
87  cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
88  break;
89  }
90 
91  case MCExpr::Unary:
92  fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
93  break;
94  }
95 }
96 
98  switch (getKind()) {
99  default:
100  return;
101  case VK_CSKY_TLSLE:
102  case VK_CSKY_TLSIE:
103  case VK_CSKY_TLSGD:
104  break;
105  }
106 
108 }
109 
111  const MCAsmLayout *Layout,
112  const MCFixup *Fixup) const {
113  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
114  return false;
115 
116  // Some custom fixup types are not valid with symbol difference expressions
117  if (Res.getSymA() && Res.getSymB()) {
118  switch (getKind()) {
119  default:
120  return true;
121  case VK_CSKY_GOT:
123  case VK_CSKY_GOTPC:
124  case VK_CSKY_GOTOFF:
125  case VK_CSKY_PLT:
127  case VK_CSKY_TLSIE:
128  case VK_CSKY_TLSLE:
129  case VK_CSKY_TLSGD:
130  case VK_CSKY_TLSLDO:
131  case VK_CSKY_TLSLDM:
132  return false;
133  }
134  }
135 
136  return true;
137 }
llvm::CSKYMCExpr::VK_CSKY_GOTOFF
@ VK_CSKY_GOTOFF
Definition: CSKYMCExpr.h:28
llvm::MCStreamer::visitUsedExpr
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:1061
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::CSKYMCExpr::VK_CSKY_GOTPC
@ VK_CSKY_GOTPC
Definition: CSKYMCExpr.h:27
llvm::CSKYMCExpr::fixELFSymbolsInTLSFixups
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
Definition: CSKYMCExpr.cpp:97
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
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:25
llvm::MCBinaryExpr
Binary assembler expressions.
Definition: MCExpr.h:481
llvm::CSKYMCExpr::evaluateAsRelocatableImpl
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAsmLayout *Layout, const MCFixup *Fixup) const override
Definition: CSKYMCExpr.cpp:110
llvm::CSKYMCExpr::VK_CSKY_None
@ VK_CSKY_None
Definition: CSKYMCExpr.h:20
llvm::ELF::STT_TLS
@ STT_TLS
Definition: ELF.h:1162
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::CSKYMCExpr::VK_CSKY_TLSLDO
@ VK_CSKY_TLSLDO
Definition: CSKYMCExpr.h:34
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:199
llvm::CSKYMCExpr::VK_CSKY_ADDR_HI16
@ VK_CSKY_ADDR_HI16
Definition: CSKYMCExpr.h:22
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:51
llvm::MCBinaryExpr::getRHS
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:631
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:399
llvm::MCValue::getSymA
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:45
llvm::CSKYMCExpr::VK_CSKY_PLT
@ VK_CSKY_PLT
Definition: CSKYMCExpr.h:29
llvm::MCExpr::getKind
ExprKind getKind() const
Definition: MCExpr.h:81
llvm::CSKYMCExpr::VK_CSKY_TLSLDM
@ VK_CSKY_TLSLDM
Definition: CSKYMCExpr.h:35
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::MCExpr::Binary
@ Binary
Binary expressions.
Definition: MCExpr.h:38
llvm::CSKYMCExpr::VK_CSKY_ADDR_LO16
@ VK_CSKY_ADDR_LO16
Definition: CSKYMCExpr.h:23
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:54
llvm::MCSymbolRefExpr
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:192
llvm::CSKYMCExpr::VK_CSKY_TLSIE
@ VK_CSKY_TLSIE
Definition: CSKYMCExpr.h:31
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:57
llvm::CSKYMCExpr::VK_CSKY_TLSLE
@ VK_CSKY_TLSLE
Definition: CSKYMCExpr.h:32
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::CSKYMCExpr::VK_CSKY_PLT_IMM18_BY4
@ VK_CSKY_PLT_IMM18_BY4
Definition: CSKYMCExpr.h:30
fixELFSymbolsInTLSFixupsImpl
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
Definition: CSKYMCExpr.cpp:68
llvm::CSKYMCExpr::VK_CSKY_GOT_IMM18_BY4
@ VK_CSKY_GOT_IMM18_BY4
Definition: CSKYMCExpr.h:26
llvm::CSKYMCExpr::VK_CSKY_TLSGD
@ VK_CSKY_TLSGD
Definition: CSKYMCExpr.h:33
llvm::MCAsmLayout
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:28
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:63
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:59
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:628