1 //===-- CSKYMCExpr.cpp - CSKY specific MC expression classes -*- C++ -*----===//
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
7 //===----------------------------------------------------------------------===//
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"
18 #define DEBUG_TYPE "csky-mc-expr"
20 const CSKYMCExpr
*CSKYMCExpr::create(const MCExpr
*Expr
, VariantKind Kind
,
22 return new (Ctx
) CSKYMCExpr(Kind
, Expr
);
25 StringRef
CSKYMCExpr::getVariantKindName(VariantKind Kind
) {
28 llvm_unreachable("Invalid ELF symbol kind");
48 void CSKYMCExpr::visitUsedExpr(MCStreamer
&Streamer
) const {
49 Streamer
.visitUsedExpr(*getSubExpr());
52 void CSKYMCExpr::printImpl(raw_ostream
&OS
, const MCAsmInfo
*MAI
) const {
54 OS
<< getVariantKindName(getKind());
57 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr
*Expr
, MCAssembler
&Asm
) {
58 switch (Expr
->getKind()) {
60 llvm_unreachable("Can't handle nested target expression");
62 case MCExpr::Constant
:
65 case MCExpr::Binary
: {
66 const MCBinaryExpr
*BE
= cast
<MCBinaryExpr
>(Expr
);
67 fixELFSymbolsInTLSFixupsImpl(BE
->getLHS(), Asm
);
68 fixELFSymbolsInTLSFixupsImpl(BE
->getRHS(), Asm
);
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
);
81 fixELFSymbolsInTLSFixupsImpl(cast
<MCUnaryExpr
>(Expr
)->getSubExpr(), Asm
);
86 void CSKYMCExpr::fixELFSymbolsInTLSFixups(MCAssembler
&Asm
) const {
95 fixELFSymbolsInTLSFixupsImpl(getSubExpr(), Asm
);
98 bool CSKYMCExpr::evaluateAsRelocatableImpl(MCValue
&Res
,
99 const MCAsmLayout
*Layout
,
100 const MCFixup
*Fixup
) const {
101 if (!getSubExpr()->evaluateAsRelocatable(Res
, Layout
, Fixup
))
104 // Some custom fixup types are not valid with symbol difference expressions
105 if (Res
.getSymA() && Res
.getSymB()) {