LLVM 20.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"
12#include "llvm/MC/MCAssembler.h"
13#include "llvm/MC/MCContext.h"
14#include "llvm/MC/MCStreamer.h"
15#include "llvm/MC/MCSymbolELF.h"
17
18using 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 "";
35 return "@HI16";
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
62 Streamer.visitUsedExpr(*getSubExpr());
63}
64
66 Expr->print(OS, MAI);
68}
69
70static 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;
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 MCFixup *Fixup) const {
114 if (!getSubExpr()->evaluateAsRelocatable(Res, Asm, Fixup))
115 return false;
116
117 // Some custom fixup types are not valid with symbol difference expressions
118 if (Res.getSymA() && Res.getSymB()) {
119 switch (getKind()) {
120 default:
121 return true;
122 case VK_CSKY_GOT:
124 case VK_CSKY_GOTPC:
125 case VK_CSKY_GOTOFF:
126 case VK_CSKY_PLT:
128 case VK_CSKY_TLSIE:
129 case VK_CSKY_TLSLE:
130 case VK_CSKY_TLSGD:
131 case VK_CSKY_TLSLDO:
132 case VK_CSKY_TLSLDM:
133 return false;
134 }
135 }
136
137 return true;
138}
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm)
PowerPC TLS Dynamic Call Fixup
raw_pwrite_stream & OS
void fixELFSymbolsInTLSFixups(MCAssembler &Asm) const override
Definition: CSKYMCExpr.cpp:99
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override
Definition: CSKYMCExpr.cpp:65
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) const override
Definition: CSKYMCExpr.cpp:112
const MCExpr * getSubExpr() const
Definition: CSKYMCExpr.h:54
static const CSKYMCExpr * create(const MCExpr *Expr, VariantKind Kind, MCContext &Ctx)
Definition: CSKYMCExpr.cpp:22
void visitUsedExpr(MCStreamer &Streamer) const override
Definition: CSKYMCExpr.cpp:61
static StringRef getVariantKindName(VariantKind Kind)
Definition: CSKYMCExpr.cpp:27
VariantKind getKind() const
Definition: CSKYMCExpr.h:51
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
Binary assembler expressions.
Definition: MCExpr.h:488
const MCExpr * getLHS() const
Get the left-hand side expression of the binary operator.
Definition: MCExpr.h:635
const MCExpr * getRHS() const
Get the right-hand side expression of the binary operator.
Definition: MCExpr.h:638
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
@ Unary
Unary expressions.
Definition: MCExpr.h:40
@ Constant
Constant expressions.
Definition: MCExpr.h:38
@ SymbolRef
References to labels and assigned expressions.
Definition: MCExpr.h:39
@ Target
Target specific expression.
Definition: MCExpr.h:41
@ Binary
Binary expressions.
Definition: MCExpr.h:37
bool evaluateAsRelocatable(MCValue &Res, const MCAssembler *Asm, const MCFixup *Fixup) const
Try to evaluate the expression to a relocatable value, i.e.
Definition: MCExpr.cpp:788
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:40
ExprKind getKind() const
Definition: MCExpr.h:78
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Streaming machine code generation interface.
Definition: MCStreamer.h:213
void visitUsedExpr(const MCExpr &Expr)
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:188
const MCSymbol & getSymbol() const
Definition: MCExpr.h:406
This represents an "assembler immediate".
Definition: MCValue.h:36
const MCSymbolRefExpr * getSymB() const
Definition: MCValue.h:45
const MCSymbolRefExpr * getSymA() const
Definition: MCValue.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ STT_TLS
Definition: ELF.h:1357
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18