blob: cfcac52d807c4c07cc10a0a883d9ad1ee50c5b8b [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
10#include "Hexagon.h"
11#include "MCTargetDesc/HexagonBaseInfo.h"
Colin LeMahieub6625652015-05-01 21:14:21 +000012#include "MCTargetDesc/HexagonFixupKinds.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000013#include "MCTargetDesc/HexagonMCCodeEmitter.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 {
162void raise_relocation_error(unsigned bits, unsigned kind) {
163 std::string Text;
164 {
165 llvm::raw_string_ostream Stream(Text);
166 Stream << "Unrecognized relocation combination bits: " << bits
167 << " kind: " << kind;
168 }
169 report_fatal_error(Text);
170}
171}
172
173/// getFixupNoBits - Some insns are not extended and thus have no
174/// bits. These cases require a more brute force method for determining
175/// the correct relocation.
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000176Hexagon::Fixups HexagonMCCodeEmitter::getFixupNoBits(
177 MCInstrInfo const &MCII, const MCInst &MI, const MCOperand &MO,
178 const MCSymbolRefExpr::VariantKind kind) const {
Colin LeMahieub6625652015-05-01 21:14:21 +0000179 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
180 unsigned insnType = llvm::HexagonMCInstrInfo::getType(MCII, MI);
181
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000182 if (insnType == HexagonII::TypeEXTENDER) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000183 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000184 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000185 return Hexagon::fixup_Hexagon_GOTREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000186 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000187 return Hexagon::fixup_Hexagon_GOT_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000188 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000189 return Hexagon::fixup_Hexagon_TPREL_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000190 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000191 return Hexagon::fixup_Hexagon_DTPREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000192 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000193 return Hexagon::fixup_Hexagon_GD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000194 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000195 return Hexagon::fixup_Hexagon_LD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000196 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000197 return Hexagon::fixup_Hexagon_IE_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000198 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000199 return Hexagon::fixup_Hexagon_IE_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000200 case MCSymbolRefExpr::VK_Hexagon_PCREL:
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000201 return Hexagon::fixup_Hexagon_B32_PCREL_X;
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000202 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
203 return Hexagon::fixup_Hexagon_GD_PLT_B32_PCREL_X;
204 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
205 return Hexagon::fixup_Hexagon_LD_PLT_B32_PCREL_X;
206
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000207 case MCSymbolRefExpr::VK_None: {
208 auto Insts = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000209 for (auto I = Insts.begin(), N = Insts.end(); I != N; ++I) {
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000210 if (I->getInst() == &MI) {
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000211 const MCInst &NextI = *(I+1)->getInst();
212 const MCInstrDesc &D = HexagonMCInstrInfo::getDesc(MCII, NextI);
213 if (D.isBranch() || D.isCall() ||
214 HexagonMCInstrInfo::getType(MCII, NextI) == HexagonII::TypeCR)
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000215 return Hexagon::fixup_Hexagon_B32_PCREL_X;
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000216 return Hexagon::fixup_Hexagon_32_6_X;
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000217 }
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000218 }
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000219 raise_relocation_error(0, kind);
220 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000221 default:
222 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000223 }
224 } else if (MCID.isBranch())
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000225 return Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000226
227 switch (MCID.getOpcode()) {
228 case Hexagon::HI:
229 case Hexagon::A2_tfrih:
230 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000231 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000232 return Hexagon::fixup_Hexagon_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000233 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000234 return Hexagon::fixup_Hexagon_GOTREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000235 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000236 return Hexagon::fixup_Hexagon_GD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000237 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000238 return Hexagon::fixup_Hexagon_LD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000239 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000240 return Hexagon::fixup_Hexagon_IE_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000241 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000242 return Hexagon::fixup_Hexagon_IE_GOT_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000243 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000244 return Hexagon::fixup_Hexagon_TPREL_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000245 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000246 return Hexagon::fixup_Hexagon_DTPREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000247 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000248 return Hexagon::fixup_Hexagon_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000249 default:
250 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000251 }
252
253 case Hexagon::LO:
254 case Hexagon::A2_tfril:
255 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000256 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000257 return Hexagon::fixup_Hexagon_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000258 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000259 return Hexagon::fixup_Hexagon_GOTREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000260 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000261 return Hexagon::fixup_Hexagon_GD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000262 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000263 return Hexagon::fixup_Hexagon_LD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000264 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000265 return Hexagon::fixup_Hexagon_IE_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000266 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000267 return Hexagon::fixup_Hexagon_IE_GOT_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000268 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000269 return Hexagon::fixup_Hexagon_TPREL_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000270 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000271 return Hexagon::fixup_Hexagon_DTPREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000272 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000273 return Hexagon::fixup_Hexagon_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000274 default:
275 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000276 }
277
278 // The only relocs left should be GP relative:
279 default:
280 if (MCID.mayStore() || MCID.mayLoad()) {
Chad Rosierc00ab4f2016-02-18 17:49:57 +0000281 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
282 ++ImpUses) {
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000283 if (*ImpUses != Hexagon::GP)
284 continue;
285 switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
286 case HexagonII::MemAccessSize::ByteAccess:
287 return fixup_Hexagon_GPREL16_0;
288 case HexagonII::MemAccessSize::HalfWordAccess:
289 return fixup_Hexagon_GPREL16_1;
290 case HexagonII::MemAccessSize::WordAccess:
291 return fixup_Hexagon_GPREL16_2;
292 case HexagonII::MemAccessSize::DoubleWordAccess:
293 return fixup_Hexagon_GPREL16_3;
294 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000295 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000296 }
297 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000298 }
299 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000300 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000301 llvm_unreachable("Relocation exit not taken");
302}
Colin LeMahieub6625652015-05-01 21:14:21 +0000303
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000304namespace llvm {
305extern const MCInstrDesc HexagonInsts[];
306}
307
308namespace {
309 bool isPCRel (unsigned Kind) {
310 switch(Kind){
311 case fixup_Hexagon_B22_PCREL:
312 case fixup_Hexagon_B15_PCREL:
313 case fixup_Hexagon_B7_PCREL:
314 case fixup_Hexagon_B13_PCREL:
315 case fixup_Hexagon_B9_PCREL:
316 case fixup_Hexagon_B32_PCREL_X:
317 case fixup_Hexagon_B22_PCREL_X:
318 case fixup_Hexagon_B15_PCREL_X:
319 case fixup_Hexagon_B13_PCREL_X:
320 case fixup_Hexagon_B9_PCREL_X:
321 case fixup_Hexagon_B7_PCREL_X:
322 case fixup_Hexagon_32_PCREL:
323 case fixup_Hexagon_PLT_B22_PCREL:
324 case fixup_Hexagon_GD_PLT_B22_PCREL:
325 case fixup_Hexagon_LD_PLT_B22_PCREL:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000326 case fixup_Hexagon_GD_PLT_B22_PCREL_X:
327 case fixup_Hexagon_LD_PLT_B22_PCREL_X:
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000328 case fixup_Hexagon_6_PCREL_X:
329 return true;
330 default:
331 return false;
332 }
333 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000334}
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000335
Colin LeMahieub6625652015-05-01 21:14:21 +0000336unsigned HexagonMCCodeEmitter::getExprOpValue(const MCInst &MI,
337 const MCOperand &MO,
338 const MCExpr *ME,
339 SmallVectorImpl<MCFixup> &Fixups,
340 const MCSubtargetInfo &STI) const
341
342{
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000343 if (isa<HexagonMCExpr>(ME))
344 ME = &HexagonMCInstrInfo::getExpr(*ME);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000345 int64_t Value;
346 if (ME->evaluateAsAbsolute(Value))
347 return Value;
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000348 assert(ME->getKind() == MCExpr::SymbolRef ||
349 ME->getKind() == MCExpr::Binary);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000350 if (ME->getKind() == MCExpr::Binary) {
351 MCBinaryExpr const *Binary = cast<MCBinaryExpr>(ME);
352 getExprOpValue(MI, MO, Binary->getLHS(), Fixups, STI);
353 getExprOpValue(MI, MO, Binary->getRHS(), Fixups, STI);
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000354 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000355 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000356 Hexagon::Fixups FixupKind =
357 Hexagon::Fixups(Hexagon::fixup_Hexagon_TPREL_LO16);
358 const MCSymbolRefExpr *MCSRE = static_cast<const MCSymbolRefExpr *>(ME);
359 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
Colin LeMahieub6625652015-05-01 21:14:21 +0000360 unsigned bits = HexagonMCInstrInfo::getExtentBits(MCII, MI) -
361 HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
362 const MCSymbolRefExpr::VariantKind kind = MCSRE->getKind();
363
364 DEBUG(dbgs() << "----------------------------------------\n");
365 DEBUG(dbgs() << "Opcode Name: " << HexagonMCInstrInfo::getName(MCII, MI)
366 << "\n");
Colin LeMahieu6efd2732015-05-01 21:30:22 +0000367 DEBUG(dbgs() << "Opcode: " << MCID.getOpcode() << "\n");
Colin LeMahieub6625652015-05-01 21:14:21 +0000368 DEBUG(dbgs() << "Relocation bits: " << bits << "\n");
369 DEBUG(dbgs() << "Addend: " << *Addend << "\n");
370 DEBUG(dbgs() << "----------------------------------------\n");
371
372 switch (bits) {
373 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000374 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000375 case 32:
376 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000377 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000378 FixupKind = *Extended ? Hexagon::fixup_Hexagon_DTPREL_32_6_X
379 : Hexagon::fixup_Hexagon_DTPREL_32;
380 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000381 case MCSymbolRefExpr::VK_GOT:
382 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOT_32_6_X
383 : Hexagon::fixup_Hexagon_GOT_32;
384 break;
385 case MCSymbolRefExpr::VK_GOTREL:
386 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOTREL_32_6_X
387 : Hexagon::fixup_Hexagon_GOTREL_32;
388 break;
389 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
390 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_GOT_32_6_X
391 : Hexagon::fixup_Hexagon_GD_GOT_32;
392 break;
393 case MCSymbolRefExpr::VK_Hexagon_IE:
394 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_32_6_X
395 : Hexagon::fixup_Hexagon_IE_32;
396 break;
397 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
398 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_GOT_32_6_X
399 : Hexagon::fixup_Hexagon_IE_GOT_32;
400 break;
401 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
402 FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_GOT_32_6_X
403 : Hexagon::fixup_Hexagon_LD_GOT_32;
404 break;
405 case MCSymbolRefExpr::VK_Hexagon_PCREL:
406 FixupKind = Hexagon::fixup_Hexagon_32_PCREL;
407 break;
408 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000409 FixupKind =
410 *Extended ? Hexagon::fixup_Hexagon_32_6_X : Hexagon::fixup_Hexagon_32;
411 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000412 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000413 FixupKind = *Extended ? Hexagon::fixup_Hexagon_TPREL_32_6_X
414 : Hexagon::fixup_Hexagon_TPREL_32;
415 break;
416 default:
417 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000418 }
419 break;
420
421 case 22:
422 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000423 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000424 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL_X
425 : Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000426 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000427 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000428 FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL_X
429 : Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000430 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000431 case MCSymbolRefExpr::VK_None:
432 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B22_PCREL_X
433 : Hexagon::fixup_Hexagon_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000434 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000435 case MCSymbolRefExpr::VK_PLT:
436 FixupKind = Hexagon::fixup_Hexagon_PLT_B22_PCREL;
437 break;
438 default:
439 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000440 }
441 break;
442
443 case 16:
444 if (*Extended) {
445 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000446 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000447 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
448 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000449 case MCSymbolRefExpr::VK_GOT:
450 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
451 break;
452 case MCSymbolRefExpr::VK_GOTREL:
453 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
454 break;
455 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
456 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16_X;
457 break;
458 case MCSymbolRefExpr::VK_Hexagon_IE:
459 FixupKind = Hexagon::fixup_Hexagon_IE_16_X;
460 break;
461 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
462 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16_X;
463 break;
464 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
465 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16_X;
466 break;
467 case MCSymbolRefExpr::VK_None:
468 FixupKind = Hexagon::fixup_Hexagon_16_X;
469 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000470 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000471 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
472 break;
473 default:
474 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000475 }
476 } else
477 switch (kind) {
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000478 case MCSymbolRefExpr::VK_None: {
479 if (HexagonMCInstrInfo::s23_2_reloc(*MO.getExpr()))
480 FixupKind = Hexagon::fixup_Hexagon_23_REG;
481 else
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000482 if (MCID.mayStore() || MCID.mayLoad()) {
483 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000484 ++ImpUses) {
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000485 if (*ImpUses != Hexagon::GP)
486 continue;
487 switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
488 case HexagonII::MemAccessSize::ByteAccess:
489 FixupKind = fixup_Hexagon_GPREL16_0;
490 break;
491 case HexagonII::MemAccessSize::HalfWordAccess:
492 FixupKind = fixup_Hexagon_GPREL16_1;
493 break;
494 case HexagonII::MemAccessSize::WordAccess:
495 FixupKind = fixup_Hexagon_GPREL16_2;
496 break;
497 case HexagonII::MemAccessSize::DoubleWordAccess:
498 FixupKind = fixup_Hexagon_GPREL16_3;
499 break;
500 default:
501 raise_relocation_error(bits, kind);
502 }
503 }
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000504 } else
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000505 raise_relocation_error(bits, kind);
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000506 break;
507 }
Colin LeMahieu0e051922016-02-10 18:32:01 +0000508 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000509 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16;
Colin LeMahieub6625652015-05-01 21:14:21 +0000510 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000511 case MCSymbolRefExpr::VK_GOTREL:
512 if (MCID.getOpcode() == Hexagon::HI)
Colin LeMahieub6625652015-05-01 21:14:21 +0000513 FixupKind = Hexagon::fixup_Hexagon_GOTREL_HI16;
514 else
515 FixupKind = Hexagon::fixup_Hexagon_GOTREL_LO16;
516 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000517 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000518 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16;
519 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000520 case MCSymbolRefExpr::VK_Hexagon_GPREL:
521 FixupKind = Hexagon::fixup_Hexagon_GPREL16_0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000522 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000523 case MCSymbolRefExpr::VK_Hexagon_HI16:
524 FixupKind = Hexagon::fixup_Hexagon_HI16;
525 break;
526 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000527 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16;
528 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000529 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
530 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16;
531 break;
532 case MCSymbolRefExpr::VK_Hexagon_LO16:
533 FixupKind = Hexagon::fixup_Hexagon_LO16;
534 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000535 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000536 FixupKind = Hexagon::fixup_Hexagon_TPREL_16;
537 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000538 default:
539 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000540 }
541 break;
542
543 case 15:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000544 switch (kind) {
545 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000546 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B15_PCREL_X
547 : Hexagon::fixup_Hexagon_B15_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000548 break;
549 default:
550 raise_relocation_error(bits, kind);
551 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000552 break;
553
554 case 13:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000555 switch (kind) {
556 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000557 FixupKind = Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000558 break;
559 default:
560 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000561 }
562 break;
563
564 case 12:
565 if (*Extended)
566 switch (kind) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000567 // There isn't a GOT_12_X, both 11_X and 16_X resolve to 6/26
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000568 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000569 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
570 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000571 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000572 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
573 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000574 case MCSymbolRefExpr::VK_None:
575 FixupKind = Hexagon::fixup_Hexagon_12_X;
576 break;
577 default:
578 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000579 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000580 else
581 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000582 break;
583
584 case 11:
585 if (*Extended)
586 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000587 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000588 FixupKind = Hexagon::fixup_Hexagon_DTPREL_11_X;
589 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000590 case MCSymbolRefExpr::VK_GOT:
591 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
592 break;
593 case MCSymbolRefExpr::VK_GOTREL:
594 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
595 break;
596 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
597 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_11_X;
598 break;
599 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
600 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_11_X;
601 break;
602 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
603 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_11_X;
604 break;
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000605 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
606 FixupKind = Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL_X;
607 break;
608 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
609 FixupKind = Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL_X;
610 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000611 case MCSymbolRefExpr::VK_None:
612 FixupKind = Hexagon::fixup_Hexagon_11_X;
613 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000614 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000615 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
616 break;
617 default:
618 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000619 }
620 else {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000621 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000622 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000623 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
624 break;
625 default:
626 raise_relocation_error(bits, kind);
627 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000628 }
629 break;
630
631 case 10:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000632 if (*Extended) {
633 switch (kind) {
634 case MCSymbolRefExpr::VK_None:
635 FixupKind = Hexagon::fixup_Hexagon_10_X;
636 break;
637 default:
638 raise_relocation_error(bits, kind);
639 }
640 } else
641 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000642 break;
643
644 case 9:
645 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000646 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000647 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B9_PCREL_X
648 : Hexagon::fixup_Hexagon_B9_PCREL;
649 else if (*Extended)
650 FixupKind = Hexagon::fixup_Hexagon_9_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000651 else
652 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000653 break;
654
655 case 8:
656 if (*Extended)
657 FixupKind = Hexagon::fixup_Hexagon_8_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000658 else
659 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000660 break;
661
662 case 7:
663 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000664 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000665 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B7_PCREL_X
666 : Hexagon::fixup_Hexagon_B7_PCREL;
667 else if (*Extended)
668 FixupKind = Hexagon::fixup_Hexagon_7_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000669 else
670 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000671 break;
672
673 case 6:
674 if (*Extended) {
675 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000676 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000677 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
Colin LeMahieub6625652015-05-01 21:14:21 +0000678 break;
679 // This is part of an extender, GOT_11 is a
680 // Word32_U6 unsigned/truncated reloc.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000681 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000682 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
683 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000684 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000685 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
686 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000687 case MCSymbolRefExpr::VK_Hexagon_PCREL:
688 FixupKind = Hexagon::fixup_Hexagon_6_PCREL_X;
689 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000690 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000691 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
692 break;
693 case MCSymbolRefExpr::VK_None:
694 FixupKind = Hexagon::fixup_Hexagon_6_X;
695 break;
696 default:
697 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000698 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000699 } else
700 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000701 break;
702
703 case 0:
704 FixupKind = getFixupNoBits(MCII, MI, MO, kind);
705 break;
706 }
707
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000708 MCExpr const *FixupExpression =
709 (*Addend > 0 && isPCRel(FixupKind))
710 ? MCBinaryExpr::createAdd(MO.getExpr(),
711 MCConstantExpr::create(*Addend, MCT), MCT)
712 : MO.getExpr();
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000713
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000714 MCFixup fixup = MCFixup::create(*Addend, FixupExpression,
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000715 MCFixupKind(FixupKind), MI.getLoc());
Colin LeMahieub6625652015-05-01 21:14:21 +0000716 Fixups.push_back(fixup);
717 // All of the information is in the fixup.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000718 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000719}
720
Sid Manning7da3f9a2014-10-03 13:18:11 +0000721unsigned
722HexagonMCCodeEmitter::getMachineOpValue(MCInst const &MI, MCOperand const &MO,
723 SmallVectorImpl<MCFixup> &Fixups,
724 MCSubtargetInfo const &STI) const {
Simon Pilgrimee6ef4d2017-02-19 00:03:46 +0000725#ifndef NDEBUG
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000726 size_t OperandNumber = ~0U;
727 for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i)
728 if (&MI.getOperand(i) == &MO) {
729 OperandNumber = i;
730 break;
731 }
732 assert((OperandNumber != ~0U) && "Operand not found");
Simon Pilgrimee6ef4d2017-02-19 00:03:46 +0000733#endif
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000734
735 if (HexagonMCInstrInfo::isNewValue(MCII, MI) &&
736 &MO == &MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI))) {
737 // Calculate the new value distance to the associated producer
738 MCOperand const &MCO =
739 MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI));
740 unsigned SOffset = 0;
741 unsigned VOffset = 0;
742 unsigned Register = MCO.getReg();
743 unsigned Register1;
744 unsigned Register2;
745 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
746 auto i = Instructions.begin() + *CurrentIndex - 1;
747 for (;; --i) {
748 assert(i != Instructions.begin() - 1 && "Couldn't find producer");
749 MCInst const &Inst = *i->getInst();
750 if (HexagonMCInstrInfo::isImmext(Inst))
751 continue;
752 ++SOffset;
753 if (HexagonMCInstrInfo::isVector(MCII, Inst))
754 // Vector instructions don't count scalars
755 ++VOffset;
756 Register1 =
757 HexagonMCInstrInfo::hasNewValue(MCII, Inst)
758 ? HexagonMCInstrInfo::getNewValueOperand(MCII, Inst).getReg()
759 : static_cast<unsigned>(Hexagon::NoRegister);
760 Register2 =
761 HexagonMCInstrInfo::hasNewValue2(MCII, Inst)
762 ? HexagonMCInstrInfo::getNewValueOperand2(MCII, Inst).getReg()
763 : static_cast<unsigned>(Hexagon::NoRegister);
764 if (!RegisterMatches(Register, Register1, Register2))
765 // This isn't the register we're looking for
766 continue;
767 if (!HexagonMCInstrInfo::isPredicated(MCII, Inst))
768 // Producer is unpredicated
769 break;
770 assert(HexagonMCInstrInfo::isPredicated(MCII, MI) &&
771 "Unpredicated consumer depending on predicated producer");
772 if (HexagonMCInstrInfo::isPredicatedTrue(MCII, Inst) ==
773 HexagonMCInstrInfo::isPredicatedTrue(MCII, MI))
774 // Producer predicate sense matched ours
775 break;
776 }
777 // Hexagon PRM 10.11 Construct Nt from distance
778 unsigned Offset =
779 HexagonMCInstrInfo::isVector(MCII, MI) ? VOffset : SOffset;
780 Offset <<= 1;
781 Offset |=
782 HexagonMCInstrInfo::SubregisterBit(Register, Register1, Register2);
783 return Offset;
784 }
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000785 assert(!MO.isImm());
786 if (MO.isReg()) {
787 unsigned Reg = MO.getReg();
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000788 if (HexagonMCInstrInfo::isSubInstruction(MI) ||
789 llvm::HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCJ)
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000790 return HexagonMCInstrInfo::getDuplexRegisterNumbering(Reg);
791 switch(MI.getOpcode()){
792 case Hexagon::A2_tfrrcr:
793 case Hexagon::A2_tfrcrr:
794 if(Reg == Hexagon::M0)
795 Reg = Hexagon::C6;
796 if(Reg == Hexagon::M1)
797 Reg = Hexagon::C7;
798 }
799 return MCT.getRegisterInfo()->getEncodingValue(Reg);
800 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000801
Colin LeMahieub6625652015-05-01 21:14:21 +0000802 return getExprOpValue(MI, MO, MO.getExpr(), Fixups, STI);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000803}
804
Sid Manning7da3f9a2014-10-03 13:18:11 +0000805MCCodeEmitter *llvm::createHexagonMCCodeEmitter(MCInstrInfo const &MII,
806 MCRegisterInfo const &MRI,
Sid Manning7da3f9a2014-10-03 13:18:11 +0000807 MCContext &MCT) {
Eric Christopher0169e422015-03-10 22:03:14 +0000808 return new HexagonMCCodeEmitter(MII, MCT);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000809}
810
Daniel Sanders72db2a32016-11-19 13:05:44 +0000811#define ENABLE_INSTR_PREDICATE_VERIFIER
Sid Manning7da3f9a2014-10-03 13:18:11 +0000812#include "HexagonGenMCCodeEmitter.inc"