blob: cfb5165e590f5daafa6221bc766cdfd7f68f6de3 [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
Jim Grosbach91df21f2015-05-15 19:13:16 +000068void HexagonMCCodeEmitter::encodeInstruction(MCInst const &MI, raw_ostream &OS,
Sid Manning7da3f9a2014-10-03 13:18:11 +000069 SmallVectorImpl<MCFixup> &Fixups,
70 MCSubtargetInfo const &STI) const {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000071 MCInst &HMB = const_cast<MCInst &>(MI);
72
73 assert(HexagonMCInstrInfo::isBundle(HMB));
74 DEBUG(dbgs() << "Encoding bundle\n";);
75 *Addend = 0;
76 *Extended = false;
77 *CurrentBundle = &MI;
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000078 *CurrentIndex = 0;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000079 size_t Last = HexagonMCInstrInfo::bundleSize(HMB) - 1;
80 for (auto &I : HexagonMCInstrInfo::bundleInstructions(HMB)) {
81 MCInst &HMI = const_cast<MCInst &>(*I.getInst());
Daniel Sanders72db2a32016-11-19 13:05:44 +000082 verifyInstructionPredicates(HMI,
83 computeAvailableFeatures(STI.getFeatureBits()));
84
Colin LeMahieu68d967d2015-05-29 14:44:13 +000085 EncodeSingleInstruction(HMI, OS, Fixups, STI,
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000086 parseBits(Last, HMB, HMI));
Colin LeMahieu68d967d2015-05-29 14:44:13 +000087 *Extended = HexagonMCInstrInfo::isImmext(HMI);
88 *Addend += HEXAGON_INSTR_SIZE;
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000089 ++*CurrentIndex;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000090 }
91 return;
92}
93
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +000094static bool RegisterMatches(unsigned Consumer, unsigned Producer,
95 unsigned Producer2) {
96 if (Consumer == Producer)
97 return true;
98 if (Consumer == Producer2)
99 return true;
100 // Calculate if we're a single vector consumer referencing a double producer
101 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
102 if (Consumer >= Hexagon::V0 && Consumer <= Hexagon::V31)
103 return ((Consumer - Hexagon::V0) >> 1) == (Producer - Hexagon::W0);
104 return false;
105}
106
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000107/// EncodeSingleInstruction - Emit a single
108void HexagonMCCodeEmitter::EncodeSingleInstruction(
109 const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000110 const MCSubtargetInfo &STI, uint32_t Parse) const {
111 assert(!HexagonMCInstrInfo::isBundle(MI));
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000112 uint64_t Binary;
113
114 // Pseudo instructions don't get encoded and shouldn't be here
115 // in the first place!
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000116 assert(!HexagonMCInstrInfo::getDesc(MCII, MI).isPseudo() &&
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000117 "pseudo-instruction found");
118 DEBUG(dbgs() << "Encoding insn"
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000119 " `" << HexagonMCInstrInfo::getName(MCII, MI) << "'"
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000120 "\n");
121
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000122 Binary = getBinaryCodeForInstr(MI, Fixups, STI);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000123 // Check for unimplemented instructions. Immediate extenders
124 // are encoded as zero, so they need to be accounted for.
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000125 if (!Binary &&
126 MI.getOpcode() != DuplexIClass0 &&
127 MI.getOpcode() != A4_ext) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000128 DEBUG(dbgs() << "Unimplemented inst: "
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000129 " `" << HexagonMCInstrInfo::getName(MCII, MI) << "'"
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000130 "\n");
131 llvm_unreachable("Unimplemented Instruction");
132 }
133 Binary |= Parse;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000134
135 // if we need to emit a duplexed instruction
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000136 if (MI.getOpcode() >= Hexagon::DuplexIClass0 &&
137 MI.getOpcode() <= Hexagon::DuplexIClassF) {
Colin LeMahieube8c4532015-06-05 16:00:11 +0000138 assert(Parse == HexagonII::INST_PARSE_DUPLEX &&
139 "Emitting duplex without duplex parse bits");
140 unsigned dupIClass;
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000141 switch (MI.getOpcode()) {
Colin LeMahieube8c4532015-06-05 16:00:11 +0000142 case Hexagon::DuplexIClass0:
143 dupIClass = 0;
144 break;
145 case Hexagon::DuplexIClass1:
146 dupIClass = 1;
147 break;
148 case Hexagon::DuplexIClass2:
149 dupIClass = 2;
150 break;
151 case Hexagon::DuplexIClass3:
152 dupIClass = 3;
153 break;
154 case Hexagon::DuplexIClass4:
155 dupIClass = 4;
156 break;
157 case Hexagon::DuplexIClass5:
158 dupIClass = 5;
159 break;
160 case Hexagon::DuplexIClass6:
161 dupIClass = 6;
162 break;
163 case Hexagon::DuplexIClass7:
164 dupIClass = 7;
165 break;
166 case Hexagon::DuplexIClass8:
167 dupIClass = 8;
168 break;
169 case Hexagon::DuplexIClass9:
170 dupIClass = 9;
171 break;
172 case Hexagon::DuplexIClassA:
173 dupIClass = 10;
174 break;
175 case Hexagon::DuplexIClassB:
176 dupIClass = 11;
177 break;
178 case Hexagon::DuplexIClassC:
179 dupIClass = 12;
180 break;
181 case Hexagon::DuplexIClassD:
182 dupIClass = 13;
183 break;
184 case Hexagon::DuplexIClassE:
185 dupIClass = 14;
186 break;
187 case Hexagon::DuplexIClassF:
188 dupIClass = 15;
189 break;
190 default:
191 llvm_unreachable("Unimplemented DuplexIClass");
192 break;
193 }
194 // 29 is the bit position.
195 // 0b1110 =0xE bits are masked off and down shifted by 1 bit.
196 // Last bit is moved to bit position 13
197 Binary = ((dupIClass & 0xE) << (29 - 1)) | ((dupIClass & 0x1) << 13);
198
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000199 const MCInst *subInst0 = MI.getOperand(0).getInst();
200 const MCInst *subInst1 = MI.getOperand(1).getInst();
Colin LeMahieube8c4532015-06-05 16:00:11 +0000201
202 // get subinstruction slot 0
203 unsigned subInstSlot0Bits = getBinaryCodeForInstr(*subInst0, Fixups, STI);
204 // get subinstruction slot 1
205 unsigned subInstSlot1Bits = getBinaryCodeForInstr(*subInst1, Fixups, STI);
206
207 Binary |= subInstSlot0Bits | (subInstSlot1Bits << 16);
208 }
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000209 support::endian::Writer<support::little>(OS).write<uint32_t>(Binary);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000210 ++MCNumEmitted;
211}
212
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000213namespace {
214void raise_relocation_error(unsigned bits, unsigned kind) {
215 std::string Text;
216 {
217 llvm::raw_string_ostream Stream(Text);
218 Stream << "Unrecognized relocation combination bits: " << bits
219 << " kind: " << kind;
220 }
221 report_fatal_error(Text);
222}
223}
224
225/// getFixupNoBits - Some insns are not extended and thus have no
226/// bits. These cases require a more brute force method for determining
227/// the correct relocation.
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000228Hexagon::Fixups HexagonMCCodeEmitter::getFixupNoBits(
229 MCInstrInfo const &MCII, const MCInst &MI, const MCOperand &MO,
230 const MCSymbolRefExpr::VariantKind kind) const {
Colin LeMahieub6625652015-05-01 21:14:21 +0000231 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
232 unsigned insnType = llvm::HexagonMCInstrInfo::getType(MCII, MI);
233
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000234 if (insnType == HexagonII::TypeEXTENDER) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000235 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000236 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000237 return Hexagon::fixup_Hexagon_GOTREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000238 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000239 return Hexagon::fixup_Hexagon_GOT_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000240 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000241 return Hexagon::fixup_Hexagon_TPREL_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000242 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000243 return Hexagon::fixup_Hexagon_DTPREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000244 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000245 return Hexagon::fixup_Hexagon_GD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000246 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000247 return Hexagon::fixup_Hexagon_LD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000248 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000249 return Hexagon::fixup_Hexagon_IE_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000250 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000251 return Hexagon::fixup_Hexagon_IE_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000252 case MCSymbolRefExpr::VK_Hexagon_PCREL:
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000253 return Hexagon::fixup_Hexagon_B32_PCREL_X;
254 case MCSymbolRefExpr::VK_None: {
255 auto Insts = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000256 for (auto I = Insts.begin(), N = Insts.end(); I != N; ++I) {
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000257 if (I->getInst() == &MI) {
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000258 const MCInst &NextI = *(I+1)->getInst();
259 const MCInstrDesc &D = HexagonMCInstrInfo::getDesc(MCII, NextI);
260 if (D.isBranch() || D.isCall() ||
261 HexagonMCInstrInfo::getType(MCII, NextI) == HexagonII::TypeCR)
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000262 return Hexagon::fixup_Hexagon_B32_PCREL_X;
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000263 return Hexagon::fixup_Hexagon_32_6_X;
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000264 }
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000265 }
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000266 raise_relocation_error(0, kind);
267 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000268 default:
269 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000270 }
271 } else if (MCID.isBranch())
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000272 return Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000273
274 switch (MCID.getOpcode()) {
275 case Hexagon::HI:
276 case Hexagon::A2_tfrih:
277 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000278 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000279 return Hexagon::fixup_Hexagon_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000280 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000281 return Hexagon::fixup_Hexagon_GOTREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000282 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000283 return Hexagon::fixup_Hexagon_GD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000284 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000285 return Hexagon::fixup_Hexagon_LD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000286 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000287 return Hexagon::fixup_Hexagon_IE_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000288 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000289 return Hexagon::fixup_Hexagon_IE_GOT_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000290 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000291 return Hexagon::fixup_Hexagon_TPREL_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000292 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000293 return Hexagon::fixup_Hexagon_DTPREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000294 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000295 return Hexagon::fixup_Hexagon_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000296 default:
297 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000298 }
299
300 case Hexagon::LO:
301 case Hexagon::A2_tfril:
302 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000303 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000304 return Hexagon::fixup_Hexagon_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000305 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000306 return Hexagon::fixup_Hexagon_GOTREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000307 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000308 return Hexagon::fixup_Hexagon_GD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000309 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000310 return Hexagon::fixup_Hexagon_LD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000311 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000312 return Hexagon::fixup_Hexagon_IE_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000313 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000314 return Hexagon::fixup_Hexagon_IE_GOT_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000315 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000316 return Hexagon::fixup_Hexagon_TPREL_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000317 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000318 return Hexagon::fixup_Hexagon_DTPREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000319 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000320 return Hexagon::fixup_Hexagon_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000321 default:
322 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000323 }
324
325 // The only relocs left should be GP relative:
326 default:
327 if (MCID.mayStore() || MCID.mayLoad()) {
Chad Rosierc00ab4f2016-02-18 17:49:57 +0000328 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
329 ++ImpUses) {
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000330 if (*ImpUses != Hexagon::GP)
331 continue;
332 switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
333 case HexagonII::MemAccessSize::ByteAccess:
334 return fixup_Hexagon_GPREL16_0;
335 case HexagonII::MemAccessSize::HalfWordAccess:
336 return fixup_Hexagon_GPREL16_1;
337 case HexagonII::MemAccessSize::WordAccess:
338 return fixup_Hexagon_GPREL16_2;
339 case HexagonII::MemAccessSize::DoubleWordAccess:
340 return fixup_Hexagon_GPREL16_3;
341 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000342 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000343 }
344 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000345 }
346 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000347 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000348 llvm_unreachable("Relocation exit not taken");
349}
Colin LeMahieub6625652015-05-01 21:14:21 +0000350
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000351namespace llvm {
352extern const MCInstrDesc HexagonInsts[];
353}
354
355namespace {
356 bool isPCRel (unsigned Kind) {
357 switch(Kind){
358 case fixup_Hexagon_B22_PCREL:
359 case fixup_Hexagon_B15_PCREL:
360 case fixup_Hexagon_B7_PCREL:
361 case fixup_Hexagon_B13_PCREL:
362 case fixup_Hexagon_B9_PCREL:
363 case fixup_Hexagon_B32_PCREL_X:
364 case fixup_Hexagon_B22_PCREL_X:
365 case fixup_Hexagon_B15_PCREL_X:
366 case fixup_Hexagon_B13_PCREL_X:
367 case fixup_Hexagon_B9_PCREL_X:
368 case fixup_Hexagon_B7_PCREL_X:
369 case fixup_Hexagon_32_PCREL:
370 case fixup_Hexagon_PLT_B22_PCREL:
371 case fixup_Hexagon_GD_PLT_B22_PCREL:
372 case fixup_Hexagon_LD_PLT_B22_PCREL:
373 case fixup_Hexagon_6_PCREL_X:
374 return true;
375 default:
376 return false;
377 }
378 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000379}
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000380
Colin LeMahieub6625652015-05-01 21:14:21 +0000381unsigned HexagonMCCodeEmitter::getExprOpValue(const MCInst &MI,
382 const MCOperand &MO,
383 const MCExpr *ME,
384 SmallVectorImpl<MCFixup> &Fixups,
385 const MCSubtargetInfo &STI) const
386
387{
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000388 if (isa<HexagonMCExpr>(ME))
389 ME = &HexagonMCInstrInfo::getExpr(*ME);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000390 int64_t Value;
391 if (ME->evaluateAsAbsolute(Value))
392 return Value;
393 assert(ME->getKind() == MCExpr::SymbolRef || ME->getKind() == MCExpr::Binary);
394 if (ME->getKind() == MCExpr::Binary) {
395 MCBinaryExpr const *Binary = cast<MCBinaryExpr>(ME);
396 getExprOpValue(MI, MO, Binary->getLHS(), Fixups, STI);
397 getExprOpValue(MI, MO, Binary->getRHS(), Fixups, STI);
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000398 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000399 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000400 Hexagon::Fixups FixupKind =
401 Hexagon::Fixups(Hexagon::fixup_Hexagon_TPREL_LO16);
402 const MCSymbolRefExpr *MCSRE = static_cast<const MCSymbolRefExpr *>(ME);
403 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
Colin LeMahieub6625652015-05-01 21:14:21 +0000404 unsigned bits = HexagonMCInstrInfo::getExtentBits(MCII, MI) -
405 HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
406 const MCSymbolRefExpr::VariantKind kind = MCSRE->getKind();
407
408 DEBUG(dbgs() << "----------------------------------------\n");
409 DEBUG(dbgs() << "Opcode Name: " << HexagonMCInstrInfo::getName(MCII, MI)
410 << "\n");
Colin LeMahieu6efd2732015-05-01 21:30:22 +0000411 DEBUG(dbgs() << "Opcode: " << MCID.getOpcode() << "\n");
Colin LeMahieub6625652015-05-01 21:14:21 +0000412 DEBUG(dbgs() << "Relocation bits: " << bits << "\n");
413 DEBUG(dbgs() << "Addend: " << *Addend << "\n");
414 DEBUG(dbgs() << "----------------------------------------\n");
415
416 switch (bits) {
417 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000418 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000419 case 32:
420 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000421 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000422 FixupKind = *Extended ? Hexagon::fixup_Hexagon_DTPREL_32_6_X
423 : Hexagon::fixup_Hexagon_DTPREL_32;
424 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000425 case MCSymbolRefExpr::VK_GOT:
426 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOT_32_6_X
427 : Hexagon::fixup_Hexagon_GOT_32;
428 break;
429 case MCSymbolRefExpr::VK_GOTREL:
430 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOTREL_32_6_X
431 : Hexagon::fixup_Hexagon_GOTREL_32;
432 break;
433 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
434 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_GOT_32_6_X
435 : Hexagon::fixup_Hexagon_GD_GOT_32;
436 break;
437 case MCSymbolRefExpr::VK_Hexagon_IE:
438 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_32_6_X
439 : Hexagon::fixup_Hexagon_IE_32;
440 break;
441 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
442 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_GOT_32_6_X
443 : Hexagon::fixup_Hexagon_IE_GOT_32;
444 break;
445 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
446 FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_GOT_32_6_X
447 : Hexagon::fixup_Hexagon_LD_GOT_32;
448 break;
449 case MCSymbolRefExpr::VK_Hexagon_PCREL:
450 FixupKind = Hexagon::fixup_Hexagon_32_PCREL;
451 break;
452 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000453 FixupKind =
454 *Extended ? Hexagon::fixup_Hexagon_32_6_X : Hexagon::fixup_Hexagon_32;
455 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000456 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000457 FixupKind = *Extended ? Hexagon::fixup_Hexagon_TPREL_32_6_X
458 : Hexagon::fixup_Hexagon_TPREL_32;
459 break;
460 default:
461 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000462 }
463 break;
464
465 case 22:
466 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000467 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000468 FixupKind = Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL;
469 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000470 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000471 FixupKind = Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL;
472 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000473 case MCSymbolRefExpr::VK_None:
474 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B22_PCREL_X
475 : Hexagon::fixup_Hexagon_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000476 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000477 case MCSymbolRefExpr::VK_PLT:
478 FixupKind = Hexagon::fixup_Hexagon_PLT_B22_PCREL;
479 break;
480 default:
481 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000482 }
483 break;
484
485 case 16:
486 if (*Extended) {
487 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000488 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000489 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
490 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000491 case MCSymbolRefExpr::VK_GOT:
492 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
493 break;
494 case MCSymbolRefExpr::VK_GOTREL:
495 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
496 break;
497 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
498 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16_X;
499 break;
500 case MCSymbolRefExpr::VK_Hexagon_IE:
501 FixupKind = Hexagon::fixup_Hexagon_IE_16_X;
502 break;
503 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
504 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16_X;
505 break;
506 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
507 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16_X;
508 break;
509 case MCSymbolRefExpr::VK_None:
510 FixupKind = Hexagon::fixup_Hexagon_16_X;
511 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000512 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000513 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
514 break;
515 default:
516 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000517 }
518 } else
519 switch (kind) {
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000520 case MCSymbolRefExpr::VK_None: {
521 if (HexagonMCInstrInfo::s23_2_reloc(*MO.getExpr()))
522 FixupKind = Hexagon::fixup_Hexagon_23_REG;
523 else
524 raise_relocation_error(bits, kind);
525 break;
526 }
Colin LeMahieu0e051922016-02-10 18:32:01 +0000527 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000528 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16;
Colin LeMahieub6625652015-05-01 21:14:21 +0000529 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000530 case MCSymbolRefExpr::VK_GOTREL:
531 if (MCID.getOpcode() == Hexagon::HI)
Colin LeMahieub6625652015-05-01 21:14:21 +0000532 FixupKind = Hexagon::fixup_Hexagon_GOTREL_HI16;
533 else
534 FixupKind = Hexagon::fixup_Hexagon_GOTREL_LO16;
535 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000536 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000537 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16;
538 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000539 case MCSymbolRefExpr::VK_Hexagon_GPREL:
540 FixupKind = Hexagon::fixup_Hexagon_GPREL16_0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000541 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000542 case MCSymbolRefExpr::VK_Hexagon_HI16:
543 FixupKind = Hexagon::fixup_Hexagon_HI16;
544 break;
545 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000546 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16;
547 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000548 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
549 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16;
550 break;
551 case MCSymbolRefExpr::VK_Hexagon_LO16:
552 FixupKind = Hexagon::fixup_Hexagon_LO16;
553 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000554 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000555 FixupKind = Hexagon::fixup_Hexagon_TPREL_16;
556 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000557 default:
558 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000559 }
560 break;
561
562 case 15:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000563 switch (kind) {
564 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000565 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B15_PCREL_X
566 : Hexagon::fixup_Hexagon_B15_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000567 break;
568 default:
569 raise_relocation_error(bits, kind);
570 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000571 break;
572
573 case 13:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000574 switch (kind) {
575 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000576 FixupKind = Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000577 break;
578 default:
579 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000580 }
581 break;
582
583 case 12:
584 if (*Extended)
585 switch (kind) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000586 // There isn't a GOT_12_X, both 11_X and 16_X resolve to 6/26
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000587 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000588 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
589 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000590 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000591 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
592 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000593 case MCSymbolRefExpr::VK_None:
594 FixupKind = Hexagon::fixup_Hexagon_12_X;
595 break;
596 default:
597 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000598 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000599 else
600 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000601 break;
602
603 case 11:
604 if (*Extended)
605 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000606 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000607 FixupKind = Hexagon::fixup_Hexagon_DTPREL_11_X;
608 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000609 case MCSymbolRefExpr::VK_GOT:
610 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
611 break;
612 case MCSymbolRefExpr::VK_GOTREL:
613 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
614 break;
615 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
616 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_11_X;
617 break;
618 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
619 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_11_X;
620 break;
621 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
622 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_11_X;
623 break;
624 case MCSymbolRefExpr::VK_None:
625 FixupKind = Hexagon::fixup_Hexagon_11_X;
626 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000627 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000628 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
629 break;
630 default:
631 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000632 }
633 else {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000634 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000635 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000636 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
637 break;
638 default:
639 raise_relocation_error(bits, kind);
640 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000641 }
642 break;
643
644 case 10:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000645 if (*Extended) {
646 switch (kind) {
647 case MCSymbolRefExpr::VK_None:
648 FixupKind = Hexagon::fixup_Hexagon_10_X;
649 break;
650 default:
651 raise_relocation_error(bits, kind);
652 }
653 } else
654 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000655 break;
656
657 case 9:
658 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000659 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000660 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B9_PCREL_X
661 : Hexagon::fixup_Hexagon_B9_PCREL;
662 else if (*Extended)
663 FixupKind = Hexagon::fixup_Hexagon_9_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000664 else
665 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000666 break;
667
668 case 8:
669 if (*Extended)
670 FixupKind = Hexagon::fixup_Hexagon_8_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000671 else
672 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000673 break;
674
675 case 7:
676 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000677 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000678 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B7_PCREL_X
679 : Hexagon::fixup_Hexagon_B7_PCREL;
680 else if (*Extended)
681 FixupKind = Hexagon::fixup_Hexagon_7_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000682 else
683 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000684 break;
685
686 case 6:
687 if (*Extended) {
688 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000689 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000690 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
Colin LeMahieub6625652015-05-01 21:14:21 +0000691 break;
692 // This is part of an extender, GOT_11 is a
693 // Word32_U6 unsigned/truncated reloc.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000694 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000695 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
696 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000697 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000698 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
699 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000700 case MCSymbolRefExpr::VK_Hexagon_PCREL:
701 FixupKind = Hexagon::fixup_Hexagon_6_PCREL_X;
702 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000703 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000704 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
705 break;
706 case MCSymbolRefExpr::VK_None:
707 FixupKind = Hexagon::fixup_Hexagon_6_X;
708 break;
709 default:
710 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000711 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000712 } else
713 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000714 break;
715
716 case 0:
717 FixupKind = getFixupNoBits(MCII, MI, MO, kind);
718 break;
719 }
720
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000721 MCExpr const *FixupExpression =
722 (*Addend > 0 && isPCRel(FixupKind))
723 ? MCBinaryExpr::createAdd(MO.getExpr(),
724 MCConstantExpr::create(*Addend, MCT), MCT)
725 : MO.getExpr();
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000726
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000727 MCFixup fixup = MCFixup::create(*Addend, FixupExpression,
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000728 MCFixupKind(FixupKind), MI.getLoc());
Colin LeMahieub6625652015-05-01 21:14:21 +0000729 Fixups.push_back(fixup);
730 // All of the information is in the fixup.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000731 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000732}
733
Sid Manning7da3f9a2014-10-03 13:18:11 +0000734unsigned
735HexagonMCCodeEmitter::getMachineOpValue(MCInst const &MI, MCOperand const &MO,
736 SmallVectorImpl<MCFixup> &Fixups,
737 MCSubtargetInfo const &STI) const {
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000738
739 if (HexagonMCInstrInfo::isNewValue(MCII, MI) &&
740 &MO == &MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI))) {
741 // Calculate the new value distance to the associated producer
742 MCOperand const &MCO =
743 MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI));
744 unsigned SOffset = 0;
745 unsigned VOffset = 0;
746 unsigned Register = MCO.getReg();
747 unsigned Register1;
748 unsigned Register2;
749 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
750 auto i = Instructions.begin() + *CurrentIndex - 1;
751 for (;; --i) {
752 assert(i != Instructions.begin() - 1 && "Couldn't find producer");
753 MCInst const &Inst = *i->getInst();
754 if (HexagonMCInstrInfo::isImmext(Inst))
755 continue;
756 ++SOffset;
757 if (HexagonMCInstrInfo::isVector(MCII, Inst))
758 // Vector instructions don't count scalars
759 ++VOffset;
760 Register1 =
761 HexagonMCInstrInfo::hasNewValue(MCII, Inst)
762 ? HexagonMCInstrInfo::getNewValueOperand(MCII, Inst).getReg()
763 : static_cast<unsigned>(Hexagon::NoRegister);
764 Register2 =
765 HexagonMCInstrInfo::hasNewValue2(MCII, Inst)
766 ? HexagonMCInstrInfo::getNewValueOperand2(MCII, Inst).getReg()
767 : static_cast<unsigned>(Hexagon::NoRegister);
768 if (!RegisterMatches(Register, Register1, Register2))
769 // This isn't the register we're looking for
770 continue;
771 if (!HexagonMCInstrInfo::isPredicated(MCII, Inst))
772 // Producer is unpredicated
773 break;
774 assert(HexagonMCInstrInfo::isPredicated(MCII, MI) &&
775 "Unpredicated consumer depending on predicated producer");
776 if (HexagonMCInstrInfo::isPredicatedTrue(MCII, Inst) ==
777 HexagonMCInstrInfo::isPredicatedTrue(MCII, MI))
778 // Producer predicate sense matched ours
779 break;
780 }
781 // Hexagon PRM 10.11 Construct Nt from distance
782 unsigned Offset =
783 HexagonMCInstrInfo::isVector(MCII, MI) ? VOffset : SOffset;
784 Offset <<= 1;
785 Offset |=
786 HexagonMCInstrInfo::SubregisterBit(Register, Register1, Register2);
787 return Offset;
788 }
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000789 assert(!MO.isImm());
790 if (MO.isReg()) {
791 unsigned Reg = MO.getReg();
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000792 if (HexagonMCInstrInfo::isSubInstruction(MI) ||
793 llvm::HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCJ)
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000794 return HexagonMCInstrInfo::getDuplexRegisterNumbering(Reg);
795 switch(MI.getOpcode()){
796 case Hexagon::A2_tfrrcr:
797 case Hexagon::A2_tfrcrr:
798 if(Reg == Hexagon::M0)
799 Reg = Hexagon::C6;
800 if(Reg == Hexagon::M1)
801 Reg = Hexagon::C7;
802 }
803 return MCT.getRegisterInfo()->getEncodingValue(Reg);
804 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000805
Colin LeMahieub6625652015-05-01 21:14:21 +0000806 return getExprOpValue(MI, MO, MO.getExpr(), Fixups, STI);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000807}
808
Sid Manning7da3f9a2014-10-03 13:18:11 +0000809MCCodeEmitter *llvm::createHexagonMCCodeEmitter(MCInstrInfo const &MII,
810 MCRegisterInfo const &MRI,
Sid Manning7da3f9a2014-10-03 13:18:11 +0000811 MCContext &MCT) {
Eric Christopher0169e422015-03-10 22:03:14 +0000812 return new HexagonMCCodeEmitter(MII, MCT);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000813}
814
Daniel Sanders72db2a32016-11-19 13:05:44 +0000815#define ENABLE_INSTR_PREDICATE_VERIFIER
Sid Manning7da3f9a2014-10-03 13:18:11 +0000816#include "HexagonGenMCCodeEmitter.inc"