1 //===-- HexagonMCExpr.cpp - Hexagon specific MC expression classes
2 //----------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "HexagonMCExpr.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCStreamer.h"
14 #include "llvm/MC/MCSymbolELF.h"
15 #include "llvm/MC/MCValue.h"
16 #include "llvm/Support/Casting.h"
17 #include "llvm/Support/raw_ostream.h"
18 
19 using namespace llvm;
20 
21 #define DEBUG_TYPE "hexagon-mcexpr"
22 
23 HexagonMCExpr *HexagonMCExpr::create(MCExpr const *Expr, MCContext &Ctx) {
24   return new (Ctx) HexagonMCExpr(Expr);
25 }
26 
27 bool HexagonMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
28                                               MCAsmLayout const *Layout,
29                                               MCFixup const *Fixup) const {
30   return Expr->evaluateAsRelocatable(Res, Layout, Fixup);
31 }
32 
33 void HexagonMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
34   Streamer.visitUsedExpr(*Expr);
35 }
36 
37 MCFragment *llvm::HexagonMCExpr::findAssociatedFragment() const {
38   return Expr->findAssociatedFragment();
39 }
40 
41 static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
42   switch (Expr->getKind()) {
43   case MCExpr::Target:
44     llvm_unreachable("Cannot handle nested target MCExpr");
45     break;
46   case MCExpr::Constant:
47     break;
48 
49   case MCExpr::Binary: {
50     const MCBinaryExpr *be = cast<MCBinaryExpr>(Expr);
51     fixELFSymbolsInTLSFixupsImpl(be->getLHS(), Asm);
52     fixELFSymbolsInTLSFixupsImpl(be->getRHS(), Asm);
53     break;
54   }
55   case MCExpr::SymbolRef: {
56     const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(Expr);
57     switch (symRef.getKind()) {
58     default:
59       return;
60     case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
61     case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
62     case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
63     case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
64     case MCSymbolRefExpr::VK_Hexagon_IE:
65     case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
66     case MCSymbolRefExpr::VK_TPREL:
67       break;
68     }
69     cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
70     break;
71   }
72   case MCExpr::Unary:
73     fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
74     break;
75   }
76 }
77 
78 void HexagonMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
79   auto expr = getExpr();
80   fixELFSymbolsInTLSFixupsImpl(expr, Asm);
81 }
82 
83 MCExpr const *HexagonMCExpr::getExpr() const { return Expr; }
84 
85 void HexagonMCExpr::setMustExtend(bool Val) {
86   assert((!Val || !MustNotExtend) && "Extension contradiction");
87   MustExtend = Val;
88 }
89 
90 bool HexagonMCExpr::mustExtend() const { return MustExtend; }
91 void HexagonMCExpr::setMustNotExtend(bool Val) {
92   assert((!Val || !MustExtend) && "Extension contradiction");
93   MustNotExtend = Val;
94 }
95 bool HexagonMCExpr::mustNotExtend() const { return MustNotExtend; }
96 
97 bool HexagonMCExpr::s27_2_reloc() const { return S27_2_reloc; }
98 void HexagonMCExpr::setS27_2_reloc(bool Val) {
99   S27_2_reloc = Val;
100 }
101 
102 bool HexagonMCExpr::classof(MCExpr const *E) {
103   return E->getKind() == MCExpr::Target;
104 }
105 
106 HexagonMCExpr::HexagonMCExpr(MCExpr const *Expr)
107     : Expr(Expr), MustNotExtend(false), MustExtend(false), S27_2_reloc(false),
108       SignMismatch(false) {}
109 
110 void HexagonMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
111   Expr->print(OS, MAI);
112 }
113 
114 void HexagonMCExpr::setSignMismatch(bool Val) {
115   SignMismatch = Val;
116 }
117 
118 bool HexagonMCExpr::signMismatch() const {
119   return SignMismatch;
120 }
121