LLVM  16.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/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/MC/MCSymbolELF.h"
16 #include "llvm/Support/Casting.h"
17 
18 using namespace llvm;
19 
20 #define DEBUG_TYPE "csky-mc-expr"
21 
23  MCContext &Ctx) {
24  return new (Ctx) CSKYMCExpr(Kind, Expr);
25 }
26 
28  switch (Kind) {
29  default:
30  llvm_unreachable("Invalid ELF symbol kind");
31  case VK_CSKY_None:
32  case VK_CSKY_ADDR:
33  return "";
34  case VK_CSKY_ADDR_HI16:
35  return "@HI16";
36  case VK_CSKY_ADDR_LO16:
37  return "@LO16";
39  case VK_CSKY_GOT:
40  return "@GOT";
41  case VK_CSKY_GOTPC:
42  return "@GOTPC";
43  case VK_CSKY_GOTOFF:
44  return "@GOTOFF";
46  case VK_CSKY_PLT:
47  return "@PLT";
48  case VK_CSKY_TLSLE:
49  return "@TPOFF";
50  case VK_CSKY_TLSIE:
51  return "@GOTTPOFF";
52  case VK_CSKY_TLSGD:
53  return "@TLSGD32";
54  case VK_CSKY_TLSLDO:
55  return "@TLSLDO32";
56  case VK_CSKY_TLSLDM:
57  return "@TLSLDM32";
58  }
59 }
60 
61 void CSKYMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
62  Streamer.visitUsedExpr(*getSubExpr());
63 }
64 
65 void CSKYMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
66  Expr->print(OS, MAI);
67  OS << getVariantKindName(getKind());
68 }
69 
70 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
71  switch (Expr->getKind()) {
72  case MCExpr::Target:
73  llvm_unreachable("Can't handle nested target expression");
74  break;
75  case MCExpr::Constant:
76  break;
77 
78  case MCExpr::Binary: {
79  const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
82  break;
83  }
84 
85  case MCExpr::SymbolRef: {
86  // We're known to be under a TLS fixup, so any symbol should be
87  // modified. There should be only one.
88  const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
89  cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
90  break;
91  }
92 
93  case MCExpr::Unary:
94  fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
95  break;
96  }
97 }
98 
100  switch (getKind()) {
101  default:
102  return;
103  case VK_CSKY_TLSLE:
104  case VK_CSKY_TLSIE:
105  case VK_CSKY_TLSGD:
106  break;
107  }
108 
110 }
111 
113  const MCAsmLayout *Layout,
114  const MCFixup *Fixup) const {
115  if (!getSubExpr()->evaluateAsRelocatable(Res, Layout, Fixup))
116  return false;
117 
118  // Some custom fixup types are not valid with symbol difference expressions
119  if (Res.getSymA() && Res.getSymB()) {
120  switch (getKind()) {
121  default:
122  return true;
123  case VK_CSKY_GOT:
125  case VK_CSKY_GOTPC:
126  case VK_CSKY_GOTOFF:
127  case VK_CSKY_PLT:
129  case VK_CSKY_TLSIE:
130  case VK_CSKY_TLSLE:
131  case VK_CSKY_TLSGD:
132  case VK_CSKY_TLSLDO:
133  case VK_CSKY_TLSLDM:
134  return false;
135  }
136  }
137 
138  return true;
139 }
llvm::ELF::STT_TLS
@ STT_TLS
Definition: ELF.h:1239
llvm::CSKYMCExpr::VK_CSKY_GOTOFF
@ VK_CSKY_GOTOFF
Definition: CSKYMCExpr.h:28
llvm::MCStreamer::visitUsedExpr
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:1070
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::CSKYMCExpr::VK_CSKY_GOTPC
@ VK_CSKY_GOTPC
Definition: CSKYMCExpr.h:27
llvm::CSKYMCExpr::fixELFSymbolsInTLSFixups
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
Definition: CSKYMCExpr.cpp:99
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
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:27
llvm::CSKYMCExpr::create
static const CSKYMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: CSKYMCExpr.cpp:22
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:112
llvm::CSKYMCExpr::VK_CSKY_None
@ VK_CSKY_None
Definition: CSKYMCExpr.h:20
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:212
ELF.h
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:44
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::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:73
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:215
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:143
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:70
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
Casting.h
CSKYFixupKinds.h
CSKYMCExpr.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::CSKYMCExpr::printImpl
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: CSKYMCExpr.cpp:65
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:36
llvm::CSKYMCExpr::visitUsedExpr
void visitUsedExpr(MCStreamer &Streamer) const override
Definition: CSKYMCExpr.cpp:61
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:45
llvm::MCBinaryExpr::getLHS
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:628