blob: 1e708ba1bcd3accf2608471b8f32d90b0ca73fc6 [file] [log] [blame]
Colin LeMahieu7cd08922015-11-09 04:07:48 +00001//===-- HexagonMCExpr.cpp - Hexagon specific MC expression classes
2//----------===//
3//
Chandler Carruth2946cd72019-01-19 08:50:56 +00004// 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
Colin LeMahieu7cd08922015-11-09 04:07:48 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "HexagonMCExpr.h"
Benjamin Kramer27c769d2018-09-10 12:53:46 +000011#include "llvm/BinaryFormat/ELF.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000012#include "llvm/MC/MCContext.h"
Colin LeMahieu98c8e072016-02-15 18:42:07 +000013#include "llvm/MC/MCStreamer.h"
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000014#include "llvm/MC/MCSymbolELF.h"
Colin LeMahieu7cd08922015-11-09 04:07:48 +000015#include "llvm/MC/MCValue.h"
16#include "llvm/Support/raw_ostream.h"
17
18using namespace llvm;
19
20#define DEBUG_TYPE "hexagon-mcexpr"
21
Colin LeMahieuc7b21242016-02-15 18:47:55 +000022HexagonMCExpr *HexagonMCExpr::create(MCExpr const *Expr, MCContext &Ctx) {
Colin LeMahieu98c8e072016-02-15 18:42:07 +000023 return new (Ctx) HexagonMCExpr(Expr);
Colin LeMahieu7cd08922015-11-09 04:07:48 +000024}
25
Colin LeMahieu98c8e072016-02-15 18:42:07 +000026bool HexagonMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
27 MCAsmLayout const *Layout,
28 MCFixup const *Fixup) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +000029 return Expr->evaluateAsRelocatable(Res, Layout, Fixup);
30}
31
Colin LeMahieu98c8e072016-02-15 18:42:07 +000032void HexagonMCExpr::visitUsedExpr(MCStreamer &Streamer) const {
33 Streamer.visitUsedExpr(*Expr);
34}
Colin LeMahieu7cd08922015-11-09 04:07:48 +000035
Colin LeMahieu98c8e072016-02-15 18:42:07 +000036MCFragment *llvm::HexagonMCExpr::findAssociatedFragment() const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +000037 return Expr->findAssociatedFragment();
38}
39
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000040static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
41 switch (Expr->getKind()) {
42 case MCExpr::Target:
43 llvm_unreachable("Cannot handle nested target MCExpr");
44 break;
45 case MCExpr::Constant:
46 break;
47
48 case MCExpr::Binary: {
49 const MCBinaryExpr *be = cast<MCBinaryExpr>(Expr);
50 fixELFSymbolsInTLSFixupsImpl(be->getLHS(), Asm);
51 fixELFSymbolsInTLSFixupsImpl(be->getRHS(), Asm);
52 break;
53 }
54 case MCExpr::SymbolRef: {
55 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(Expr);
56 switch (symRef.getKind()) {
57 default:
58 return;
59 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
60 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
61 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
62 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
63 case MCSymbolRefExpr::VK_Hexagon_IE:
64 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
65 case MCSymbolRefExpr::VK_TPREL:
66 break;
67 }
68 cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
69 break;
70 }
71 case MCExpr::Unary:
72 fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
73 break;
74 }
75}
76
77void HexagonMCExpr::fixELFSymbolsInTLSFixups(MCAssembler &Asm) const {
78 auto expr = getExpr();
79 fixELFSymbolsInTLSFixupsImpl(expr, Asm);
80}
Colin LeMahieu7cd08922015-11-09 04:07:48 +000081
Colin LeMahieu98c8e072016-02-15 18:42:07 +000082MCExpr const *HexagonMCExpr::getExpr() const { return Expr; }
Colin LeMahieu7cd08922015-11-09 04:07:48 +000083
Colin LeMahieu98c8e072016-02-15 18:42:07 +000084void HexagonMCExpr::setMustExtend(bool Val) {
85 assert((!Val || !MustNotExtend) && "Extension contradiction");
86 MustExtend = Val;
87}
88
89bool HexagonMCExpr::mustExtend() const { return MustExtend; }
90void HexagonMCExpr::setMustNotExtend(bool Val) {
91 assert((!Val || !MustExtend) && "Extension contradiction");
92 MustNotExtend = Val;
93}
94bool HexagonMCExpr::mustNotExtend() const { return MustNotExtend; }
95
Krzysztof Parzyszek57a8bb432017-05-02 18:19:11 +000096bool HexagonMCExpr::s27_2_reloc() const { return S27_2_reloc; }
97void HexagonMCExpr::setS27_2_reloc(bool Val) {
98 S27_2_reloc = Val;
Colin LeMahieuecef1d92016-02-16 20:38:17 +000099}
100
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000101bool HexagonMCExpr::classof(MCExpr const *E) {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000102 return E->getKind() == MCExpr::Target;
103}
104
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000105HexagonMCExpr::HexagonMCExpr(MCExpr const *Expr)
Krzysztof Parzyszek57a8bb432017-05-02 18:19:11 +0000106 : Expr(Expr), MustNotExtend(false), MustExtend(false), S27_2_reloc(false),
Colin LeMahieu9e5a9c32016-02-29 20:42:25 +0000107 SignMismatch(false) {}
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000108
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000109void HexagonMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
Colin LeMahieu7cd08922015-11-09 04:07:48 +0000110 Expr->print(OS, MAI);
111}
Colin LeMahieub9f1eae2016-02-29 19:17:56 +0000112
113void HexagonMCExpr::setSignMismatch(bool Val) {
114 SignMismatch = Val;
115}
116
117bool HexagonMCExpr::signMismatch() const {
118 return SignMismatch;
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000119}