blob: 32021eee3d17e881478da133019f332d9442ce21 [file] [log] [blame]
Sid Manning7da3f9a2014-10-03 13:18:11 +00001//===-- HexagonMCCodeEmitter.cpp - Hexagon Target Descriptions ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "MCTargetDesc/HexagonMCCodeEmitter.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000011#include "Hexagon.h"
12#include "MCTargetDesc/HexagonBaseInfo.h"
Colin LeMahieub6625652015-05-01 21:14:21 +000013#include "MCTargetDesc/HexagonFixupKinds.h"
Colin LeMahieuaf304e52015-02-19 19:00:00 +000014#include "MCTargetDesc/HexagonMCInstrInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000015#include "MCTargetDesc/HexagonMCTargetDesc.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000016#include "llvm/ADT/Statistic.h"
17#include "llvm/MC/MCCodeEmitter.h"
18#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
21#include "llvm/MC/MCInstrInfo.h"
22#include "llvm/MC/MCRegisterInfo.h"
23#include "llvm/MC/MCSubtargetInfo.h"
24#include "llvm/Support/Debug.h"
Benjamin Kramer50e2a292015-06-04 15:03:02 +000025#include "llvm/Support/EndianStream.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000026#include "llvm/Support/raw_ostream.h"
27
28#define DEBUG_TYPE "mccodeemitter"
29
30using namespace llvm;
31using namespace Hexagon;
32
33STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
34
Sid Manning7da3f9a2014-10-03 13:18:11 +000035HexagonMCCodeEmitter::HexagonMCCodeEmitter(MCInstrInfo const &aMII,
Sid Manning7da3f9a2014-10-03 13:18:11 +000036 MCContext &aMCT)
Colin LeMahieub6625652015-05-01 21:14:21 +000037 : MCT(aMCT), MCII(aMII), Addend(new unsigned(0)),
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000038 Extended(new bool(false)), CurrentBundle(new MCInst const *),
39 CurrentIndex(new size_t(0)) {}
Colin LeMahieu68d967d2015-05-29 14:44:13 +000040
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000041uint32_t HexagonMCCodeEmitter::parseBits(size_t Last,
Colin LeMahieu68d967d2015-05-29 14:44:13 +000042 MCInst const &MCB,
43 MCInst const &MCI) const {
Colin LeMahieube8c4532015-06-05 16:00:11 +000044 bool Duplex = HexagonMCInstrInfo::isDuplex(MCII, MCI);
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000045 if (*CurrentIndex == 0) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000046 if (HexagonMCInstrInfo::isInnerLoop(MCB)) {
Colin LeMahieube8c4532015-06-05 16:00:11 +000047 assert(!Duplex);
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000048 assert(*CurrentIndex != Last);
Colin LeMahieu68d967d2015-05-29 14:44:13 +000049 return HexagonII::INST_PARSE_LOOP_END;
50 }
51 }
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000052 if (*CurrentIndex == 1) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000053 if (HexagonMCInstrInfo::isOuterLoop(MCB)) {
Colin LeMahieube8c4532015-06-05 16:00:11 +000054 assert(!Duplex);
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000055 assert(*CurrentIndex != Last);
Colin LeMahieu68d967d2015-05-29 14:44:13 +000056 return HexagonII::INST_PARSE_LOOP_END;
57 }
58 }
Colin LeMahieube8c4532015-06-05 16:00:11 +000059 if (Duplex) {
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000060 assert(*CurrentIndex == Last);
Colin LeMahieube8c4532015-06-05 16:00:11 +000061 return HexagonII::INST_PARSE_DUPLEX;
62 }
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000063 if(*CurrentIndex == Last)
Colin LeMahieu68d967d2015-05-29 14:44:13 +000064 return HexagonII::INST_PARSE_PACKET_END;
65 return HexagonII::INST_PARSE_NOT_END;
66}
Sid Manning7da3f9a2014-10-03 13:18:11 +000067
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000068/// EncodeInstruction - Emit the bundle
69void HexagonMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
Sid Manning7da3f9a2014-10-03 13:18:11 +000070 SmallVectorImpl<MCFixup> &Fixups,
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000071 const MCSubtargetInfo &STI) const {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000072 MCInst &HMB = const_cast<MCInst &>(MI);
73
74 assert(HexagonMCInstrInfo::isBundle(HMB));
75 DEBUG(dbgs() << "Encoding bundle\n";);
76 *Addend = 0;
77 *Extended = false;
78 *CurrentBundle = &MI;
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000079 *CurrentIndex = 0;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000080 size_t Last = HexagonMCInstrInfo::bundleSize(HMB) - 1;
81 for (auto &I : HexagonMCInstrInfo::bundleInstructions(HMB)) {
82 MCInst &HMI = const_cast<MCInst &>(*I.getInst());
Daniel Sanders72db2a32016-11-19 13:05:44 +000083 verifyInstructionPredicates(HMI,
84 computeAvailableFeatures(STI.getFeatureBits()));
85
Colin LeMahieu68d967d2015-05-29 14:44:13 +000086 EncodeSingleInstruction(HMI, OS, Fixups, STI,
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000087 parseBits(Last, HMB, HMI));
Colin LeMahieu68d967d2015-05-29 14:44:13 +000088 *Extended = HexagonMCInstrInfo::isImmext(HMI);
89 *Addend += HEXAGON_INSTR_SIZE;
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000090 ++*CurrentIndex;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000091 }
92 return;
93}
94
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +000095static bool RegisterMatches(unsigned Consumer, unsigned Producer,
96 unsigned Producer2) {
97 if (Consumer == Producer)
98 return true;
99 if (Consumer == Producer2)
100 return true;
101 // Calculate if we're a single vector consumer referencing a double producer
102 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
103 if (Consumer >= Hexagon::V0 && Consumer <= Hexagon::V31)
104 return ((Consumer - Hexagon::V0) >> 1) == (Producer - Hexagon::W0);
105 return false;
106}
107
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000108/// EncodeSingleInstruction - Emit a single
109void HexagonMCCodeEmitter::EncodeSingleInstruction(
110 const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000111 const MCSubtargetInfo &STI, uint32_t Parse) const {
112 assert(!HexagonMCInstrInfo::isBundle(MI));
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000113 uint64_t Binary;
114
115 // Pseudo instructions don't get encoded and shouldn't be here
116 // in the first place!
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000117 assert(!HexagonMCInstrInfo::getDesc(MCII, MI).isPseudo() &&
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000118 "pseudo-instruction found");
119 DEBUG(dbgs() << "Encoding insn"
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000120 " `" << HexagonMCInstrInfo::getName(MCII, MI) << "'"
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000121 "\n");
122
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000123 Binary = getBinaryCodeForInstr(MI, Fixups, STI);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000124 // Check for unimplemented instructions. Immediate extenders
125 // are encoded as zero, so they need to be accounted for.
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000126 if (!Binary &&
127 MI.getOpcode() != DuplexIClass0 &&
128 MI.getOpcode() != A4_ext) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000129 DEBUG(dbgs() << "Unimplemented inst: "
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000130 " `" << HexagonMCInstrInfo::getName(MCII, MI) << "'"
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000131 "\n");
132 llvm_unreachable("Unimplemented Instruction");
133 }
134 Binary |= Parse;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000135
136 // if we need to emit a duplexed instruction
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000137 if (MI.getOpcode() >= Hexagon::DuplexIClass0 &&
138 MI.getOpcode() <= Hexagon::DuplexIClassF) {
Colin LeMahieube8c4532015-06-05 16:00:11 +0000139 assert(Parse == HexagonII::INST_PARSE_DUPLEX &&
140 "Emitting duplex without duplex parse bits");
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000141 unsigned dupIClass = MI.getOpcode() - Hexagon::DuplexIClass0;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000142 // 29 is the bit position.
143 // 0b1110 =0xE bits are masked off and down shifted by 1 bit.
144 // Last bit is moved to bit position 13
145 Binary = ((dupIClass & 0xE) << (29 - 1)) | ((dupIClass & 0x1) << 13);
146
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000147 const MCInst *subInst0 = MI.getOperand(0).getInst();
148 const MCInst *subInst1 = MI.getOperand(1).getInst();
Colin LeMahieube8c4532015-06-05 16:00:11 +0000149
150 // get subinstruction slot 0
151 unsigned subInstSlot0Bits = getBinaryCodeForInstr(*subInst0, Fixups, STI);
152 // get subinstruction slot 1
153 unsigned subInstSlot1Bits = getBinaryCodeForInstr(*subInst1, Fixups, STI);
154
155 Binary |= subInstSlot0Bits | (subInstSlot1Bits << 16);
156 }
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000157 support::endian::Writer<support::little>(OS).write<uint32_t>(Binary);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000158 ++MCNumEmitted;
159}
160
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000161namespace {
Florian Hahn239e4b92017-07-26 16:07:51 +0000162LLVM_ATTRIBUTE_NORETURN
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000163void raise_relocation_error(unsigned bits, unsigned kind) {
164 std::string Text;
165 {
166 llvm::raw_string_ostream Stream(Text);
167 Stream << "Unrecognized relocation combination bits: " << bits
168 << " kind: " << kind;
169 }
170 report_fatal_error(Text);
171}
172}
173
174/// getFixupNoBits - Some insns are not extended and thus have no
175/// bits. These cases require a more brute force method for determining
176/// the correct relocation.
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000177Hexagon::Fixups HexagonMCCodeEmitter::getFixupNoBits(
178 MCInstrInfo const &MCII, const MCInst &MI, const MCOperand &MO,
179 const MCSymbolRefExpr::VariantKind kind) const {
Colin LeMahieub6625652015-05-01 21:14:21 +0000180 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
181 unsigned insnType = llvm::HexagonMCInstrInfo::getType(MCII, MI);
182
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000183 if (insnType == HexagonII::TypeEXTENDER) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000184 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000185 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000186 return Hexagon::fixup_Hexagon_GOTREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000187 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000188 return Hexagon::fixup_Hexagon_GOT_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000189 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000190 return Hexagon::fixup_Hexagon_TPREL_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000191 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000192 return Hexagon::fixup_Hexagon_DTPREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000193 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000194 return Hexagon::fixup_Hexagon_GD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000195 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000196 return Hexagon::fixup_Hexagon_LD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000197 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000198 return Hexagon::fixup_Hexagon_IE_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000199 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000200 return Hexagon::fixup_Hexagon_IE_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000201 case MCSymbolRefExpr::VK_Hexagon_PCREL:
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000202 return Hexagon::fixup_Hexagon_B32_PCREL_X;
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000203 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
204 return Hexagon::fixup_Hexagon_GD_PLT_B32_PCREL_X;
205 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
206 return Hexagon::fixup_Hexagon_LD_PLT_B32_PCREL_X;
207
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000208 case MCSymbolRefExpr::VK_None: {
209 auto Insts = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000210 for (auto I = Insts.begin(), N = Insts.end(); I != N; ++I) {
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000211 if (I->getInst() == &MI) {
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000212 const MCInst &NextI = *(I+1)->getInst();
213 const MCInstrDesc &D = HexagonMCInstrInfo::getDesc(MCII, NextI);
214 if (D.isBranch() || D.isCall() ||
215 HexagonMCInstrInfo::getType(MCII, NextI) == HexagonII::TypeCR)
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000216 return Hexagon::fixup_Hexagon_B32_PCREL_X;
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000217 return Hexagon::fixup_Hexagon_32_6_X;
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000218 }
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000219 }
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000220 raise_relocation_error(0, kind);
221 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000222 default:
223 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000224 }
225 } else if (MCID.isBranch())
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000226 return Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000227
228 switch (MCID.getOpcode()) {
229 case Hexagon::HI:
230 case Hexagon::A2_tfrih:
231 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000232 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000233 return Hexagon::fixup_Hexagon_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000234 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000235 return Hexagon::fixup_Hexagon_GOTREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000236 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000237 return Hexagon::fixup_Hexagon_GD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000238 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000239 return Hexagon::fixup_Hexagon_LD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000240 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000241 return Hexagon::fixup_Hexagon_IE_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000242 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000243 return Hexagon::fixup_Hexagon_IE_GOT_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000244 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000245 return Hexagon::fixup_Hexagon_TPREL_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000246 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000247 return Hexagon::fixup_Hexagon_DTPREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000248 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000249 return Hexagon::fixup_Hexagon_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000250 default:
251 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000252 }
253
254 case Hexagon::LO:
255 case Hexagon::A2_tfril:
256 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000257 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000258 return Hexagon::fixup_Hexagon_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000259 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000260 return Hexagon::fixup_Hexagon_GOTREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000261 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000262 return Hexagon::fixup_Hexagon_GD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000263 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000264 return Hexagon::fixup_Hexagon_LD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000265 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000266 return Hexagon::fixup_Hexagon_IE_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000267 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000268 return Hexagon::fixup_Hexagon_IE_GOT_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000269 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000270 return Hexagon::fixup_Hexagon_TPREL_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000271 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000272 return Hexagon::fixup_Hexagon_DTPREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000273 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000274 return Hexagon::fixup_Hexagon_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000275 default:
276 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000277 }
278
279 // The only relocs left should be GP relative:
280 default:
281 if (MCID.mayStore() || MCID.mayLoad()) {
Chad Rosierc00ab4f2016-02-18 17:49:57 +0000282 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
283 ++ImpUses) {
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000284 if (*ImpUses != Hexagon::GP)
285 continue;
286 switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
287 case HexagonII::MemAccessSize::ByteAccess:
288 return fixup_Hexagon_GPREL16_0;
289 case HexagonII::MemAccessSize::HalfWordAccess:
290 return fixup_Hexagon_GPREL16_1;
291 case HexagonII::MemAccessSize::WordAccess:
292 return fixup_Hexagon_GPREL16_2;
293 case HexagonII::MemAccessSize::DoubleWordAccess:
294 return fixup_Hexagon_GPREL16_3;
295 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000296 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000297 }
298 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000299 }
300 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000301 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000302 llvm_unreachable("Relocation exit not taken");
303}
Colin LeMahieub6625652015-05-01 21:14:21 +0000304
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000305namespace llvm {
306extern const MCInstrDesc HexagonInsts[];
307}
308
309namespace {
310 bool isPCRel (unsigned Kind) {
311 switch(Kind){
312 case fixup_Hexagon_B22_PCREL:
313 case fixup_Hexagon_B15_PCREL:
314 case fixup_Hexagon_B7_PCREL:
315 case fixup_Hexagon_B13_PCREL:
316 case fixup_Hexagon_B9_PCREL:
317 case fixup_Hexagon_B32_PCREL_X:
318 case fixup_Hexagon_B22_PCREL_X:
319 case fixup_Hexagon_B15_PCREL_X:
320 case fixup_Hexagon_B13_PCREL_X:
321 case fixup_Hexagon_B9_PCREL_X:
322 case fixup_Hexagon_B7_PCREL_X:
323 case fixup_Hexagon_32_PCREL:
324 case fixup_Hexagon_PLT_B22_PCREL:
325 case fixup_Hexagon_GD_PLT_B22_PCREL:
326 case fixup_Hexagon_LD_PLT_B22_PCREL:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000327 case fixup_Hexagon_GD_PLT_B22_PCREL_X:
328 case fixup_Hexagon_LD_PLT_B22_PCREL_X:
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000329 case fixup_Hexagon_6_PCREL_X:
330 return true;
331 default:
332 return false;
333 }
334 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000335}
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000336
Colin LeMahieub6625652015-05-01 21:14:21 +0000337unsigned HexagonMCCodeEmitter::getExprOpValue(const MCInst &MI,
338 const MCOperand &MO,
339 const MCExpr *ME,
340 SmallVectorImpl<MCFixup> &Fixups,
341 const MCSubtargetInfo &STI) const
342
343{
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000344 if (isa<HexagonMCExpr>(ME))
345 ME = &HexagonMCInstrInfo::getExpr(*ME);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000346 int64_t Value;
347 if (ME->evaluateAsAbsolute(Value))
348 return Value;
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000349 assert(ME->getKind() == MCExpr::SymbolRef ||
350 ME->getKind() == MCExpr::Binary);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000351 if (ME->getKind() == MCExpr::Binary) {
352 MCBinaryExpr const *Binary = cast<MCBinaryExpr>(ME);
353 getExprOpValue(MI, MO, Binary->getLHS(), Fixups, STI);
354 getExprOpValue(MI, MO, Binary->getRHS(), Fixups, STI);
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000355 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000356 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000357 Hexagon::Fixups FixupKind =
358 Hexagon::Fixups(Hexagon::fixup_Hexagon_TPREL_LO16);
359 const MCSymbolRefExpr *MCSRE = static_cast<const MCSymbolRefExpr *>(ME);
360 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
Colin LeMahieub6625652015-05-01 21:14:21 +0000361 unsigned bits = HexagonMCInstrInfo::getExtentBits(MCII, MI) -
362 HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
363 const MCSymbolRefExpr::VariantKind kind = MCSRE->getKind();
364
365 DEBUG(dbgs() << "----------------------------------------\n");
366 DEBUG(dbgs() << "Opcode Name: " << HexagonMCInstrInfo::getName(MCII, MI)
367 << "\n");
Colin LeMahieu6efd2732015-05-01 21:30:22 +0000368 DEBUG(dbgs() << "Opcode: " << MCID.getOpcode() << "\n");
Colin LeMahieub6625652015-05-01 21:14:21 +0000369 DEBUG(dbgs() << "Relocation bits: " << bits << "\n");
370 DEBUG(dbgs() << "Addend: " << *Addend << "\n");
371 DEBUG(dbgs() << "----------------------------------------\n");
372
373 switch (bits) {
374 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000375 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000376 case 32:
377 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000378 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000379 FixupKind = *Extended ? Hexagon::fixup_Hexagon_DTPREL_32_6_X
380 : Hexagon::fixup_Hexagon_DTPREL_32;
381 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000382 case MCSymbolRefExpr::VK_GOT:
383 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOT_32_6_X
384 : Hexagon::fixup_Hexagon_GOT_32;
385 break;
386 case MCSymbolRefExpr::VK_GOTREL:
387 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOTREL_32_6_X
388 : Hexagon::fixup_Hexagon_GOTREL_32;
389 break;
390 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
391 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_GOT_32_6_X
392 : Hexagon::fixup_Hexagon_GD_GOT_32;
393 break;
394 case MCSymbolRefExpr::VK_Hexagon_IE:
395 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_32_6_X
396 : Hexagon::fixup_Hexagon_IE_32;
397 break;
398 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
399 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_GOT_32_6_X
400 : Hexagon::fixup_Hexagon_IE_GOT_32;
401 break;
402 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
403 FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_GOT_32_6_X
404 : Hexagon::fixup_Hexagon_LD_GOT_32;
405 break;
406 case MCSymbolRefExpr::VK_Hexagon_PCREL:
407 FixupKind = Hexagon::fixup_Hexagon_32_PCREL;
408 break;
409 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000410 FixupKind =
411 *Extended ? Hexagon::fixup_Hexagon_32_6_X : Hexagon::fixup_Hexagon_32;
412 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000413 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000414 FixupKind = *Extended ? Hexagon::fixup_Hexagon_TPREL_32_6_X
415 : Hexagon::fixup_Hexagon_TPREL_32;
416 break;
417 default:
418 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000419 }
420 break;
421
422 case 22:
423 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000424 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000425 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL_X
426 : Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000427 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000428 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000429 FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL_X
430 : Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000431 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000432 case MCSymbolRefExpr::VK_None:
433 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B22_PCREL_X
434 : Hexagon::fixup_Hexagon_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000435 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000436 case MCSymbolRefExpr::VK_PLT:
437 FixupKind = Hexagon::fixup_Hexagon_PLT_B22_PCREL;
438 break;
439 default:
440 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000441 }
442 break;
443
444 case 16:
445 if (*Extended) {
446 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000447 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000448 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
449 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000450 case MCSymbolRefExpr::VK_GOT:
451 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
452 break;
453 case MCSymbolRefExpr::VK_GOTREL:
454 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
455 break;
456 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
457 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16_X;
458 break;
459 case MCSymbolRefExpr::VK_Hexagon_IE:
460 FixupKind = Hexagon::fixup_Hexagon_IE_16_X;
461 break;
462 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
463 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16_X;
464 break;
465 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
466 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16_X;
467 break;
468 case MCSymbolRefExpr::VK_None:
469 FixupKind = Hexagon::fixup_Hexagon_16_X;
470 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000471 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000472 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
473 break;
474 default:
475 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000476 }
477 } else
478 switch (kind) {
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000479 case MCSymbolRefExpr::VK_None: {
Krzysztof Parzyszek57a8bb432017-05-02 18:19:11 +0000480 if (HexagonMCInstrInfo::s27_2_reloc(*MO.getExpr()))
481 FixupKind = Hexagon::fixup_Hexagon_27_REG;
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000482 else
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000483 if (MCID.mayStore() || MCID.mayLoad()) {
484 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000485 ++ImpUses) {
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000486 if (*ImpUses != Hexagon::GP)
487 continue;
488 switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
489 case HexagonII::MemAccessSize::ByteAccess:
490 FixupKind = fixup_Hexagon_GPREL16_0;
491 break;
492 case HexagonII::MemAccessSize::HalfWordAccess:
493 FixupKind = fixup_Hexagon_GPREL16_1;
494 break;
495 case HexagonII::MemAccessSize::WordAccess:
496 FixupKind = fixup_Hexagon_GPREL16_2;
497 break;
498 case HexagonII::MemAccessSize::DoubleWordAccess:
499 FixupKind = fixup_Hexagon_GPREL16_3;
500 break;
501 default:
502 raise_relocation_error(bits, kind);
503 }
504 }
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000505 } else
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000506 raise_relocation_error(bits, kind);
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000507 break;
508 }
Colin LeMahieu0e051922016-02-10 18:32:01 +0000509 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000510 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16;
Colin LeMahieub6625652015-05-01 21:14:21 +0000511 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000512 case MCSymbolRefExpr::VK_GOTREL:
513 if (MCID.getOpcode() == Hexagon::HI)
Colin LeMahieub6625652015-05-01 21:14:21 +0000514 FixupKind = Hexagon::fixup_Hexagon_GOTREL_HI16;
515 else
516 FixupKind = Hexagon::fixup_Hexagon_GOTREL_LO16;
517 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000518 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000519 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16;
520 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000521 case MCSymbolRefExpr::VK_Hexagon_GPREL:
522 FixupKind = Hexagon::fixup_Hexagon_GPREL16_0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000523 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000524 case MCSymbolRefExpr::VK_Hexagon_HI16:
525 FixupKind = Hexagon::fixup_Hexagon_HI16;
526 break;
527 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000528 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16;
529 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000530 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
531 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16;
532 break;
533 case MCSymbolRefExpr::VK_Hexagon_LO16:
534 FixupKind = Hexagon::fixup_Hexagon_LO16;
535 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000536 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000537 FixupKind = Hexagon::fixup_Hexagon_TPREL_16;
538 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000539 default:
540 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000541 }
542 break;
543
544 case 15:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000545 switch (kind) {
546 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000547 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B15_PCREL_X
548 : Hexagon::fixup_Hexagon_B15_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000549 break;
550 default:
551 raise_relocation_error(bits, kind);
552 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000553 break;
554
555 case 13:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000556 switch (kind) {
557 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000558 FixupKind = Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000559 break;
560 default:
561 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000562 }
563 break;
564
565 case 12:
566 if (*Extended)
567 switch (kind) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000568 // There isn't a GOT_12_X, both 11_X and 16_X resolve to 6/26
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000569 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000570 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
571 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000572 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000573 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
574 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000575 case MCSymbolRefExpr::VK_None:
576 FixupKind = Hexagon::fixup_Hexagon_12_X;
577 break;
578 default:
579 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000580 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000581 else
582 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000583 break;
584
585 case 11:
586 if (*Extended)
587 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000588 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000589 FixupKind = Hexagon::fixup_Hexagon_DTPREL_11_X;
590 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000591 case MCSymbolRefExpr::VK_GOT:
592 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
593 break;
594 case MCSymbolRefExpr::VK_GOTREL:
595 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
596 break;
597 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
598 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_11_X;
599 break;
600 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
601 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_11_X;
602 break;
603 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
604 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_11_X;
605 break;
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000606 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
607 FixupKind = Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL_X;
608 break;
609 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
610 FixupKind = Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL_X;
611 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000612 case MCSymbolRefExpr::VK_None:
613 FixupKind = Hexagon::fixup_Hexagon_11_X;
614 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000615 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000616 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
617 break;
618 default:
619 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000620 }
621 else {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000622 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000623 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000624 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
625 break;
626 default:
627 raise_relocation_error(bits, kind);
628 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000629 }
630 break;
631
632 case 10:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000633 if (*Extended) {
634 switch (kind) {
635 case MCSymbolRefExpr::VK_None:
636 FixupKind = Hexagon::fixup_Hexagon_10_X;
637 break;
638 default:
639 raise_relocation_error(bits, kind);
640 }
641 } else
642 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000643 break;
644
645 case 9:
646 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000647 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000648 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B9_PCREL_X
649 : Hexagon::fixup_Hexagon_B9_PCREL;
650 else if (*Extended)
651 FixupKind = Hexagon::fixup_Hexagon_9_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000652 else
653 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000654 break;
655
656 case 8:
657 if (*Extended)
658 FixupKind = Hexagon::fixup_Hexagon_8_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000659 else
660 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000661 break;
662
663 case 7:
664 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000665 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000666 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B7_PCREL_X
667 : Hexagon::fixup_Hexagon_B7_PCREL;
668 else if (*Extended)
669 FixupKind = Hexagon::fixup_Hexagon_7_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000670 else
671 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000672 break;
673
674 case 6:
675 if (*Extended) {
676 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000677 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000678 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
Colin LeMahieub6625652015-05-01 21:14:21 +0000679 break;
680 // This is part of an extender, GOT_11 is a
681 // Word32_U6 unsigned/truncated reloc.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000682 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000683 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
684 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000685 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000686 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
687 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000688 case MCSymbolRefExpr::VK_Hexagon_PCREL:
689 FixupKind = Hexagon::fixup_Hexagon_6_PCREL_X;
690 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000691 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000692 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
693 break;
694 case MCSymbolRefExpr::VK_None:
695 FixupKind = Hexagon::fixup_Hexagon_6_X;
696 break;
697 default:
698 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000699 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000700 } else
701 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000702 break;
703
704 case 0:
705 FixupKind = getFixupNoBits(MCII, MI, MO, kind);
706 break;
707 }
708
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000709 MCExpr const *FixupExpression =
710 (*Addend > 0 && isPCRel(FixupKind))
711 ? MCBinaryExpr::createAdd(MO.getExpr(),
712 MCConstantExpr::create(*Addend, MCT), MCT)
713 : MO.getExpr();
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000714
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000715 MCFixup fixup = MCFixup::create(*Addend, FixupExpression,
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000716 MCFixupKind(FixupKind), MI.getLoc());
Colin LeMahieub6625652015-05-01 21:14:21 +0000717 Fixups.push_back(fixup);
718 // All of the information is in the fixup.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000719 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000720}
721
Sid Manning7da3f9a2014-10-03 13:18:11 +0000722unsigned
723HexagonMCCodeEmitter::getMachineOpValue(MCInst const &MI, MCOperand const &MO,
724 SmallVectorImpl<MCFixup> &Fixups,
725 MCSubtargetInfo const &STI) const {
Simon Pilgrimee6ef4d2017-02-19 00:03:46 +0000726#ifndef NDEBUG
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000727 size_t OperandNumber = ~0U;
728 for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i)
729 if (&MI.getOperand(i) == &MO) {
730 OperandNumber = i;
731 break;
732 }
733 assert((OperandNumber != ~0U) && "Operand not found");
Simon Pilgrimee6ef4d2017-02-19 00:03:46 +0000734#endif
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000735
736 if (HexagonMCInstrInfo::isNewValue(MCII, MI) &&
737 &MO == &MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI))) {
738 // Calculate the new value distance to the associated producer
739 MCOperand const &MCO =
740 MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI));
741 unsigned SOffset = 0;
742 unsigned VOffset = 0;
743 unsigned Register = MCO.getReg();
744 unsigned Register1;
745 unsigned Register2;
746 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
747 auto i = Instructions.begin() + *CurrentIndex - 1;
748 for (;; --i) {
749 assert(i != Instructions.begin() - 1 && "Couldn't find producer");
750 MCInst const &Inst = *i->getInst();
751 if (HexagonMCInstrInfo::isImmext(Inst))
752 continue;
753 ++SOffset;
754 if (HexagonMCInstrInfo::isVector(MCII, Inst))
755 // Vector instructions don't count scalars
756 ++VOffset;
757 Register1 =
758 HexagonMCInstrInfo::hasNewValue(MCII, Inst)
759 ? HexagonMCInstrInfo::getNewValueOperand(MCII, Inst).getReg()
760 : static_cast<unsigned>(Hexagon::NoRegister);
761 Register2 =
762 HexagonMCInstrInfo::hasNewValue2(MCII, Inst)
763 ? HexagonMCInstrInfo::getNewValueOperand2(MCII, Inst).getReg()
764 : static_cast<unsigned>(Hexagon::NoRegister);
765 if (!RegisterMatches(Register, Register1, Register2))
766 // This isn't the register we're looking for
767 continue;
768 if (!HexagonMCInstrInfo::isPredicated(MCII, Inst))
769 // Producer is unpredicated
770 break;
771 assert(HexagonMCInstrInfo::isPredicated(MCII, MI) &&
772 "Unpredicated consumer depending on predicated producer");
773 if (HexagonMCInstrInfo::isPredicatedTrue(MCII, Inst) ==
774 HexagonMCInstrInfo::isPredicatedTrue(MCII, MI))
775 // Producer predicate sense matched ours
776 break;
777 }
778 // Hexagon PRM 10.11 Construct Nt from distance
779 unsigned Offset =
780 HexagonMCInstrInfo::isVector(MCII, MI) ? VOffset : SOffset;
781 Offset <<= 1;
782 Offset |=
783 HexagonMCInstrInfo::SubregisterBit(Register, Register1, Register2);
784 return Offset;
785 }
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000786 assert(!MO.isImm());
787 if (MO.isReg()) {
788 unsigned Reg = MO.getReg();
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000789 if (HexagonMCInstrInfo::isSubInstruction(MI) ||
790 llvm::HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCJ)
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000791 return HexagonMCInstrInfo::getDuplexRegisterNumbering(Reg);
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000792 return MCT.getRegisterInfo()->getEncodingValue(Reg);
793 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000794
Colin LeMahieub6625652015-05-01 21:14:21 +0000795 return getExprOpValue(MI, MO, MO.getExpr(), Fixups, STI);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000796}
797
Sid Manning7da3f9a2014-10-03 13:18:11 +0000798MCCodeEmitter *llvm::createHexagonMCCodeEmitter(MCInstrInfo const &MII,
799 MCRegisterInfo const &MRI,
Sid Manning7da3f9a2014-10-03 13:18:11 +0000800 MCContext &MCT) {
Eric Christopher0169e422015-03-10 22:03:14 +0000801 return new HexagonMCCodeEmitter(MII, MCT);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000802}
803
Daniel Sanders72db2a32016-11-19 13:05:44 +0000804#define ENABLE_INSTR_PREDICATE_VERIFIER
Sid Manning7da3f9a2014-10-03 13:18:11 +0000805#include "HexagonGenMCCodeEmitter.inc"