blob: 18a408ed89e35c2022bc4939824bb0a7d6ab7567 [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
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000524 if (MCID.mayStore() || MCID.mayLoad()) {
525 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
526 ++ImpUses) {
527 if (*ImpUses != Hexagon::GP)
528 continue;
529 switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
530 case HexagonII::MemAccessSize::ByteAccess:
531 FixupKind = fixup_Hexagon_GPREL16_0;
532 break;
533 case HexagonII::MemAccessSize::HalfWordAccess:
534 FixupKind = fixup_Hexagon_GPREL16_1;
535 break;
536 case HexagonII::MemAccessSize::WordAccess:
537 FixupKind = fixup_Hexagon_GPREL16_2;
538 break;
539 case HexagonII::MemAccessSize::DoubleWordAccess:
540 FixupKind = fixup_Hexagon_GPREL16_3;
541 break;
542 default:
543 raise_relocation_error(bits, kind);
544 }
545 }
546 }
547 else
548 raise_relocation_error(bits, kind);
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000549 break;
550 }
Colin LeMahieu0e051922016-02-10 18:32:01 +0000551 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000552 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16;
Colin LeMahieub6625652015-05-01 21:14:21 +0000553 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000554 case MCSymbolRefExpr::VK_GOTREL:
555 if (MCID.getOpcode() == Hexagon::HI)
Colin LeMahieub6625652015-05-01 21:14:21 +0000556 FixupKind = Hexagon::fixup_Hexagon_GOTREL_HI16;
557 else
558 FixupKind = Hexagon::fixup_Hexagon_GOTREL_LO16;
559 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000560 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000561 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16;
562 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000563 case MCSymbolRefExpr::VK_Hexagon_GPREL:
564 FixupKind = Hexagon::fixup_Hexagon_GPREL16_0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000565 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000566 case MCSymbolRefExpr::VK_Hexagon_HI16:
567 FixupKind = Hexagon::fixup_Hexagon_HI16;
568 break;
569 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000570 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16;
571 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000572 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
573 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16;
574 break;
575 case MCSymbolRefExpr::VK_Hexagon_LO16:
576 FixupKind = Hexagon::fixup_Hexagon_LO16;
577 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000578 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000579 FixupKind = Hexagon::fixup_Hexagon_TPREL_16;
580 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000581 default:
582 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000583 }
584 break;
585
586 case 15:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000587 switch (kind) {
588 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000589 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B15_PCREL_X
590 : Hexagon::fixup_Hexagon_B15_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000591 break;
592 default:
593 raise_relocation_error(bits, kind);
594 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000595 break;
596
597 case 13:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000598 switch (kind) {
599 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000600 FixupKind = Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000601 break;
602 default:
603 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000604 }
605 break;
606
607 case 12:
608 if (*Extended)
609 switch (kind) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000610 // There isn't a GOT_12_X, both 11_X and 16_X resolve to 6/26
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000611 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000612 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
613 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000614 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000615 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
616 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000617 case MCSymbolRefExpr::VK_None:
618 FixupKind = Hexagon::fixup_Hexagon_12_X;
619 break;
620 default:
621 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000622 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000623 else
624 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000625 break;
626
627 case 11:
628 if (*Extended)
629 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000630 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000631 FixupKind = Hexagon::fixup_Hexagon_DTPREL_11_X;
632 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000633 case MCSymbolRefExpr::VK_GOT:
634 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
635 break;
636 case MCSymbolRefExpr::VK_GOTREL:
637 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
638 break;
639 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
640 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_11_X;
641 break;
642 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
643 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_11_X;
644 break;
645 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
646 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_11_X;
647 break;
648 case MCSymbolRefExpr::VK_None:
649 FixupKind = Hexagon::fixup_Hexagon_11_X;
650 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000651 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000652 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
653 break;
654 default:
655 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000656 }
657 else {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000658 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000659 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000660 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
661 break;
662 default:
663 raise_relocation_error(bits, kind);
664 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000665 }
666 break;
667
668 case 10:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000669 if (*Extended) {
670 switch (kind) {
671 case MCSymbolRefExpr::VK_None:
672 FixupKind = Hexagon::fixup_Hexagon_10_X;
673 break;
674 default:
675 raise_relocation_error(bits, kind);
676 }
677 } else
678 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000679 break;
680
681 case 9:
682 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000683 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000684 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B9_PCREL_X
685 : Hexagon::fixup_Hexagon_B9_PCREL;
686 else if (*Extended)
687 FixupKind = Hexagon::fixup_Hexagon_9_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000688 else
689 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000690 break;
691
692 case 8:
693 if (*Extended)
694 FixupKind = Hexagon::fixup_Hexagon_8_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000695 else
696 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000697 break;
698
699 case 7:
700 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000701 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000702 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B7_PCREL_X
703 : Hexagon::fixup_Hexagon_B7_PCREL;
704 else if (*Extended)
705 FixupKind = Hexagon::fixup_Hexagon_7_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000706 else
707 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000708 break;
709
710 case 6:
711 if (*Extended) {
712 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000713 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000714 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
Colin LeMahieub6625652015-05-01 21:14:21 +0000715 break;
716 // This is part of an extender, GOT_11 is a
717 // Word32_U6 unsigned/truncated reloc.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000718 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000719 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
720 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000721 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000722 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
723 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000724 case MCSymbolRefExpr::VK_Hexagon_PCREL:
725 FixupKind = Hexagon::fixup_Hexagon_6_PCREL_X;
726 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000727 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000728 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
729 break;
730 case MCSymbolRefExpr::VK_None:
731 FixupKind = Hexagon::fixup_Hexagon_6_X;
732 break;
733 default:
734 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000735 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000736 } else
737 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000738 break;
739
740 case 0:
741 FixupKind = getFixupNoBits(MCII, MI, MO, kind);
742 break;
743 }
744
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000745 MCExpr const *FixupExpression =
746 (*Addend > 0 && isPCRel(FixupKind))
747 ? MCBinaryExpr::createAdd(MO.getExpr(),
748 MCConstantExpr::create(*Addend, MCT), MCT)
749 : MO.getExpr();
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000750
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000751 MCFixup fixup = MCFixup::create(*Addend, FixupExpression,
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000752 MCFixupKind(FixupKind), MI.getLoc());
Colin LeMahieub6625652015-05-01 21:14:21 +0000753 Fixups.push_back(fixup);
754 // All of the information is in the fixup.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000755 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000756}
757
Sid Manning7da3f9a2014-10-03 13:18:11 +0000758unsigned
759HexagonMCCodeEmitter::getMachineOpValue(MCInst const &MI, MCOperand const &MO,
760 SmallVectorImpl<MCFixup> &Fixups,
761 MCSubtargetInfo const &STI) const {
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000762
763 if (HexagonMCInstrInfo::isNewValue(MCII, MI) &&
764 &MO == &MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI))) {
765 // Calculate the new value distance to the associated producer
766 MCOperand const &MCO =
767 MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI));
768 unsigned SOffset = 0;
769 unsigned VOffset = 0;
770 unsigned Register = MCO.getReg();
771 unsigned Register1;
772 unsigned Register2;
773 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
774 auto i = Instructions.begin() + *CurrentIndex - 1;
775 for (;; --i) {
776 assert(i != Instructions.begin() - 1 && "Couldn't find producer");
777 MCInst const &Inst = *i->getInst();
778 if (HexagonMCInstrInfo::isImmext(Inst))
779 continue;
780 ++SOffset;
781 if (HexagonMCInstrInfo::isVector(MCII, Inst))
782 // Vector instructions don't count scalars
783 ++VOffset;
784 Register1 =
785 HexagonMCInstrInfo::hasNewValue(MCII, Inst)
786 ? HexagonMCInstrInfo::getNewValueOperand(MCII, Inst).getReg()
787 : static_cast<unsigned>(Hexagon::NoRegister);
788 Register2 =
789 HexagonMCInstrInfo::hasNewValue2(MCII, Inst)
790 ? HexagonMCInstrInfo::getNewValueOperand2(MCII, Inst).getReg()
791 : static_cast<unsigned>(Hexagon::NoRegister);
792 if (!RegisterMatches(Register, Register1, Register2))
793 // This isn't the register we're looking for
794 continue;
795 if (!HexagonMCInstrInfo::isPredicated(MCII, Inst))
796 // Producer is unpredicated
797 break;
798 assert(HexagonMCInstrInfo::isPredicated(MCII, MI) &&
799 "Unpredicated consumer depending on predicated producer");
800 if (HexagonMCInstrInfo::isPredicatedTrue(MCII, Inst) ==
801 HexagonMCInstrInfo::isPredicatedTrue(MCII, MI))
802 // Producer predicate sense matched ours
803 break;
804 }
805 // Hexagon PRM 10.11 Construct Nt from distance
806 unsigned Offset =
807 HexagonMCInstrInfo::isVector(MCII, MI) ? VOffset : SOffset;
808 Offset <<= 1;
809 Offset |=
810 HexagonMCInstrInfo::SubregisterBit(Register, Register1, Register2);
811 return Offset;
812 }
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000813 assert(!MO.isImm());
814 if (MO.isReg()) {
815 unsigned Reg = MO.getReg();
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000816 if (HexagonMCInstrInfo::isSubInstruction(MI) ||
817 llvm::HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCJ)
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000818 return HexagonMCInstrInfo::getDuplexRegisterNumbering(Reg);
819 switch(MI.getOpcode()){
820 case Hexagon::A2_tfrrcr:
821 case Hexagon::A2_tfrcrr:
822 if(Reg == Hexagon::M0)
823 Reg = Hexagon::C6;
824 if(Reg == Hexagon::M1)
825 Reg = Hexagon::C7;
826 }
827 return MCT.getRegisterInfo()->getEncodingValue(Reg);
828 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000829
Colin LeMahieub6625652015-05-01 21:14:21 +0000830 return getExprOpValue(MI, MO, MO.getExpr(), Fixups, STI);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000831}
832
Sid Manning7da3f9a2014-10-03 13:18:11 +0000833MCCodeEmitter *llvm::createHexagonMCCodeEmitter(MCInstrInfo const &MII,
834 MCRegisterInfo const &MRI,
Sid Manning7da3f9a2014-10-03 13:18:11 +0000835 MCContext &MCT) {
Eric Christopher0169e422015-03-10 22:03:14 +0000836 return new HexagonMCCodeEmitter(MII, MCT);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000837}
838
Daniel Sanders72db2a32016-11-19 13:05:44 +0000839#define ENABLE_INSTR_PREDICATE_VERIFIER
Sid Manning7da3f9a2014-10-03 13:18:11 +0000840#include "HexagonGenMCCodeEmitter.inc"