blob: c0956520de738a6b6eacbc010bc8a0dd1c6c5032 [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;
202 case MCSymbolRefExpr::VK_None: {
203 auto Insts = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000204 for (auto I = Insts.begin(), N = Insts.end(); I != N; ++I) {
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000205 if (I->getInst() == &MI) {
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000206 const MCInst &NextI = *(I+1)->getInst();
207 const MCInstrDesc &D = HexagonMCInstrInfo::getDesc(MCII, NextI);
208 if (D.isBranch() || D.isCall() ||
209 HexagonMCInstrInfo::getType(MCII, NextI) == HexagonII::TypeCR)
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000210 return Hexagon::fixup_Hexagon_B32_PCREL_X;
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000211 return Hexagon::fixup_Hexagon_32_6_X;
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000212 }
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000213 }
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000214 raise_relocation_error(0, kind);
215 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000216 default:
217 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000218 }
219 } else if (MCID.isBranch())
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000220 return Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000221
222 switch (MCID.getOpcode()) {
223 case Hexagon::HI:
224 case Hexagon::A2_tfrih:
225 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000226 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000227 return Hexagon::fixup_Hexagon_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000228 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000229 return Hexagon::fixup_Hexagon_GOTREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000230 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000231 return Hexagon::fixup_Hexagon_GD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000232 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000233 return Hexagon::fixup_Hexagon_LD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000234 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000235 return Hexagon::fixup_Hexagon_IE_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000236 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000237 return Hexagon::fixup_Hexagon_IE_GOT_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000238 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000239 return Hexagon::fixup_Hexagon_TPREL_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000240 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000241 return Hexagon::fixup_Hexagon_DTPREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000242 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000243 return Hexagon::fixup_Hexagon_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000244 default:
245 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000246 }
247
248 case Hexagon::LO:
249 case Hexagon::A2_tfril:
250 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000251 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000252 return Hexagon::fixup_Hexagon_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000253 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000254 return Hexagon::fixup_Hexagon_GOTREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000255 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000256 return Hexagon::fixup_Hexagon_GD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000257 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000258 return Hexagon::fixup_Hexagon_LD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000259 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000260 return Hexagon::fixup_Hexagon_IE_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000261 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000262 return Hexagon::fixup_Hexagon_IE_GOT_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000263 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000264 return Hexagon::fixup_Hexagon_TPREL_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000265 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000266 return Hexagon::fixup_Hexagon_DTPREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000267 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000268 return Hexagon::fixup_Hexagon_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000269 default:
270 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000271 }
272
273 // The only relocs left should be GP relative:
274 default:
275 if (MCID.mayStore() || MCID.mayLoad()) {
Chad Rosierc00ab4f2016-02-18 17:49:57 +0000276 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
277 ++ImpUses) {
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000278 if (*ImpUses != Hexagon::GP)
279 continue;
280 switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
281 case HexagonII::MemAccessSize::ByteAccess:
282 return fixup_Hexagon_GPREL16_0;
283 case HexagonII::MemAccessSize::HalfWordAccess:
284 return fixup_Hexagon_GPREL16_1;
285 case HexagonII::MemAccessSize::WordAccess:
286 return fixup_Hexagon_GPREL16_2;
287 case HexagonII::MemAccessSize::DoubleWordAccess:
288 return fixup_Hexagon_GPREL16_3;
289 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000290 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000291 }
292 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000293 }
294 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000295 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000296 llvm_unreachable("Relocation exit not taken");
297}
Colin LeMahieub6625652015-05-01 21:14:21 +0000298
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000299namespace llvm {
300extern const MCInstrDesc HexagonInsts[];
301}
302
303namespace {
304 bool isPCRel (unsigned Kind) {
305 switch(Kind){
306 case fixup_Hexagon_B22_PCREL:
307 case fixup_Hexagon_B15_PCREL:
308 case fixup_Hexagon_B7_PCREL:
309 case fixup_Hexagon_B13_PCREL:
310 case fixup_Hexagon_B9_PCREL:
311 case fixup_Hexagon_B32_PCREL_X:
312 case fixup_Hexagon_B22_PCREL_X:
313 case fixup_Hexagon_B15_PCREL_X:
314 case fixup_Hexagon_B13_PCREL_X:
315 case fixup_Hexagon_B9_PCREL_X:
316 case fixup_Hexagon_B7_PCREL_X:
317 case fixup_Hexagon_32_PCREL:
318 case fixup_Hexagon_PLT_B22_PCREL:
319 case fixup_Hexagon_GD_PLT_B22_PCREL:
320 case fixup_Hexagon_LD_PLT_B22_PCREL:
321 case fixup_Hexagon_6_PCREL_X:
322 return true;
323 default:
324 return false;
325 }
326 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000327}
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000328
Colin LeMahieub6625652015-05-01 21:14:21 +0000329unsigned HexagonMCCodeEmitter::getExprOpValue(const MCInst &MI,
330 const MCOperand &MO,
331 const MCExpr *ME,
332 SmallVectorImpl<MCFixup> &Fixups,
333 const MCSubtargetInfo &STI) const
334
335{
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000336 if (isa<HexagonMCExpr>(ME))
337 ME = &HexagonMCInstrInfo::getExpr(*ME);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000338 int64_t Value;
339 if (ME->evaluateAsAbsolute(Value))
340 return Value;
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000341 assert(ME->getKind() == MCExpr::SymbolRef ||
342 ME->getKind() == MCExpr::Binary);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000343 if (ME->getKind() == MCExpr::Binary) {
344 MCBinaryExpr const *Binary = cast<MCBinaryExpr>(ME);
345 getExprOpValue(MI, MO, Binary->getLHS(), Fixups, STI);
346 getExprOpValue(MI, MO, Binary->getRHS(), Fixups, STI);
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000347 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000348 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000349 Hexagon::Fixups FixupKind =
350 Hexagon::Fixups(Hexagon::fixup_Hexagon_TPREL_LO16);
351 const MCSymbolRefExpr *MCSRE = static_cast<const MCSymbolRefExpr *>(ME);
352 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
Colin LeMahieub6625652015-05-01 21:14:21 +0000353 unsigned bits = HexagonMCInstrInfo::getExtentBits(MCII, MI) -
354 HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
355 const MCSymbolRefExpr::VariantKind kind = MCSRE->getKind();
356
357 DEBUG(dbgs() << "----------------------------------------\n");
358 DEBUG(dbgs() << "Opcode Name: " << HexagonMCInstrInfo::getName(MCII, MI)
359 << "\n");
Colin LeMahieu6efd2732015-05-01 21:30:22 +0000360 DEBUG(dbgs() << "Opcode: " << MCID.getOpcode() << "\n");
Colin LeMahieub6625652015-05-01 21:14:21 +0000361 DEBUG(dbgs() << "Relocation bits: " << bits << "\n");
362 DEBUG(dbgs() << "Addend: " << *Addend << "\n");
363 DEBUG(dbgs() << "----------------------------------------\n");
364
365 switch (bits) {
366 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000367 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000368 case 32:
369 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000370 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000371 FixupKind = *Extended ? Hexagon::fixup_Hexagon_DTPREL_32_6_X
372 : Hexagon::fixup_Hexagon_DTPREL_32;
373 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000374 case MCSymbolRefExpr::VK_GOT:
375 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOT_32_6_X
376 : Hexagon::fixup_Hexagon_GOT_32;
377 break;
378 case MCSymbolRefExpr::VK_GOTREL:
379 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOTREL_32_6_X
380 : Hexagon::fixup_Hexagon_GOTREL_32;
381 break;
382 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
383 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_GOT_32_6_X
384 : Hexagon::fixup_Hexagon_GD_GOT_32;
385 break;
386 case MCSymbolRefExpr::VK_Hexagon_IE:
387 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_32_6_X
388 : Hexagon::fixup_Hexagon_IE_32;
389 break;
390 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
391 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_GOT_32_6_X
392 : Hexagon::fixup_Hexagon_IE_GOT_32;
393 break;
394 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
395 FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_GOT_32_6_X
396 : Hexagon::fixup_Hexagon_LD_GOT_32;
397 break;
398 case MCSymbolRefExpr::VK_Hexagon_PCREL:
399 FixupKind = Hexagon::fixup_Hexagon_32_PCREL;
400 break;
401 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000402 FixupKind =
403 *Extended ? Hexagon::fixup_Hexagon_32_6_X : Hexagon::fixup_Hexagon_32;
404 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000405 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000406 FixupKind = *Extended ? Hexagon::fixup_Hexagon_TPREL_32_6_X
407 : Hexagon::fixup_Hexagon_TPREL_32;
408 break;
409 default:
410 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000411 }
412 break;
413
414 case 22:
415 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000416 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000417 FixupKind = Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL;
418 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000419 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000420 FixupKind = Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL;
421 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000422 case MCSymbolRefExpr::VK_None:
423 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B22_PCREL_X
424 : Hexagon::fixup_Hexagon_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000425 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000426 case MCSymbolRefExpr::VK_PLT:
427 FixupKind = Hexagon::fixup_Hexagon_PLT_B22_PCREL;
428 break;
429 default:
430 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000431 }
432 break;
433
434 case 16:
435 if (*Extended) {
436 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000437 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000438 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
439 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000440 case MCSymbolRefExpr::VK_GOT:
441 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
442 break;
443 case MCSymbolRefExpr::VK_GOTREL:
444 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
445 break;
446 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
447 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16_X;
448 break;
449 case MCSymbolRefExpr::VK_Hexagon_IE:
450 FixupKind = Hexagon::fixup_Hexagon_IE_16_X;
451 break;
452 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
453 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16_X;
454 break;
455 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
456 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16_X;
457 break;
458 case MCSymbolRefExpr::VK_None:
459 FixupKind = Hexagon::fixup_Hexagon_16_X;
460 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000461 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000462 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
463 break;
464 default:
465 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000466 }
467 } else
468 switch (kind) {
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000469 case MCSymbolRefExpr::VK_None: {
470 if (HexagonMCInstrInfo::s23_2_reloc(*MO.getExpr()))
471 FixupKind = Hexagon::fixup_Hexagon_23_REG;
472 else
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000473 if (MCID.mayStore() || MCID.mayLoad()) {
474 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000475 ++ImpUses) {
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000476 if (*ImpUses != Hexagon::GP)
477 continue;
478 switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
479 case HexagonII::MemAccessSize::ByteAccess:
480 FixupKind = fixup_Hexagon_GPREL16_0;
481 break;
482 case HexagonII::MemAccessSize::HalfWordAccess:
483 FixupKind = fixup_Hexagon_GPREL16_1;
484 break;
485 case HexagonII::MemAccessSize::WordAccess:
486 FixupKind = fixup_Hexagon_GPREL16_2;
487 break;
488 case HexagonII::MemAccessSize::DoubleWordAccess:
489 FixupKind = fixup_Hexagon_GPREL16_3;
490 break;
491 default:
492 raise_relocation_error(bits, kind);
493 }
494 }
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000495 } else
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000496 raise_relocation_error(bits, kind);
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000497 break;
498 }
Colin LeMahieu0e051922016-02-10 18:32:01 +0000499 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000500 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16;
Colin LeMahieub6625652015-05-01 21:14:21 +0000501 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000502 case MCSymbolRefExpr::VK_GOTREL:
503 if (MCID.getOpcode() == Hexagon::HI)
Colin LeMahieub6625652015-05-01 21:14:21 +0000504 FixupKind = Hexagon::fixup_Hexagon_GOTREL_HI16;
505 else
506 FixupKind = Hexagon::fixup_Hexagon_GOTREL_LO16;
507 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000508 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000509 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16;
510 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000511 case MCSymbolRefExpr::VK_Hexagon_GPREL:
512 FixupKind = Hexagon::fixup_Hexagon_GPREL16_0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000513 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000514 case MCSymbolRefExpr::VK_Hexagon_HI16:
515 FixupKind = Hexagon::fixup_Hexagon_HI16;
516 break;
517 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000518 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16;
519 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000520 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
521 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16;
522 break;
523 case MCSymbolRefExpr::VK_Hexagon_LO16:
524 FixupKind = Hexagon::fixup_Hexagon_LO16;
525 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000526 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000527 FixupKind = Hexagon::fixup_Hexagon_TPREL_16;
528 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000529 default:
530 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000531 }
532 break;
533
534 case 15:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000535 switch (kind) {
536 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000537 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B15_PCREL_X
538 : Hexagon::fixup_Hexagon_B15_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000539 break;
540 default:
541 raise_relocation_error(bits, kind);
542 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000543 break;
544
545 case 13:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000546 switch (kind) {
547 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000548 FixupKind = Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000549 break;
550 default:
551 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000552 }
553 break;
554
555 case 12:
556 if (*Extended)
557 switch (kind) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000558 // There isn't a GOT_12_X, both 11_X and 16_X resolve to 6/26
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000559 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000560 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
561 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000562 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000563 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
564 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000565 case MCSymbolRefExpr::VK_None:
566 FixupKind = Hexagon::fixup_Hexagon_12_X;
567 break;
568 default:
569 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000570 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000571 else
572 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000573 break;
574
575 case 11:
576 if (*Extended)
577 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000578 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000579 FixupKind = Hexagon::fixup_Hexagon_DTPREL_11_X;
580 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000581 case MCSymbolRefExpr::VK_GOT:
582 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
583 break;
584 case MCSymbolRefExpr::VK_GOTREL:
585 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
586 break;
587 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
588 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_11_X;
589 break;
590 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
591 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_11_X;
592 break;
593 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
594 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_11_X;
595 break;
596 case MCSymbolRefExpr::VK_None:
597 FixupKind = Hexagon::fixup_Hexagon_11_X;
598 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000599 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000600 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
601 break;
602 default:
603 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000604 }
605 else {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000606 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000607 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000608 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
609 break;
610 default:
611 raise_relocation_error(bits, kind);
612 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000613 }
614 break;
615
616 case 10:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000617 if (*Extended) {
618 switch (kind) {
619 case MCSymbolRefExpr::VK_None:
620 FixupKind = Hexagon::fixup_Hexagon_10_X;
621 break;
622 default:
623 raise_relocation_error(bits, kind);
624 }
625 } else
626 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000627 break;
628
629 case 9:
630 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000631 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000632 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B9_PCREL_X
633 : Hexagon::fixup_Hexagon_B9_PCREL;
634 else if (*Extended)
635 FixupKind = Hexagon::fixup_Hexagon_9_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000636 else
637 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000638 break;
639
640 case 8:
641 if (*Extended)
642 FixupKind = Hexagon::fixup_Hexagon_8_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000643 else
644 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000645 break;
646
647 case 7:
648 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000649 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000650 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B7_PCREL_X
651 : Hexagon::fixup_Hexagon_B7_PCREL;
652 else if (*Extended)
653 FixupKind = Hexagon::fixup_Hexagon_7_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000654 else
655 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000656 break;
657
658 case 6:
659 if (*Extended) {
660 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000661 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000662 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
Colin LeMahieub6625652015-05-01 21:14:21 +0000663 break;
664 // This is part of an extender, GOT_11 is a
665 // Word32_U6 unsigned/truncated reloc.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000666 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000667 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
668 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000669 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000670 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
671 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000672 case MCSymbolRefExpr::VK_Hexagon_PCREL:
673 FixupKind = Hexagon::fixup_Hexagon_6_PCREL_X;
674 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000675 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000676 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
677 break;
678 case MCSymbolRefExpr::VK_None:
679 FixupKind = Hexagon::fixup_Hexagon_6_X;
680 break;
681 default:
682 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000683 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000684 } else
685 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000686 break;
687
688 case 0:
689 FixupKind = getFixupNoBits(MCII, MI, MO, kind);
690 break;
691 }
692
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000693 MCExpr const *FixupExpression =
694 (*Addend > 0 && isPCRel(FixupKind))
695 ? MCBinaryExpr::createAdd(MO.getExpr(),
696 MCConstantExpr::create(*Addend, MCT), MCT)
697 : MO.getExpr();
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000698
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000699 MCFixup fixup = MCFixup::create(*Addend, FixupExpression,
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000700 MCFixupKind(FixupKind), MI.getLoc());
Colin LeMahieub6625652015-05-01 21:14:21 +0000701 Fixups.push_back(fixup);
702 // All of the information is in the fixup.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000703 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000704}
705
Sid Manning7da3f9a2014-10-03 13:18:11 +0000706unsigned
707HexagonMCCodeEmitter::getMachineOpValue(MCInst const &MI, MCOperand const &MO,
708 SmallVectorImpl<MCFixup> &Fixups,
709 MCSubtargetInfo const &STI) const {
Simon Pilgrimee6ef4d2017-02-19 00:03:46 +0000710#ifndef NDEBUG
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000711 size_t OperandNumber = ~0U;
712 for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i)
713 if (&MI.getOperand(i) == &MO) {
714 OperandNumber = i;
715 break;
716 }
717 assert((OperandNumber != ~0U) && "Operand not found");
Simon Pilgrimee6ef4d2017-02-19 00:03:46 +0000718#endif
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000719
720 if (HexagonMCInstrInfo::isNewValue(MCII, MI) &&
721 &MO == &MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI))) {
722 // Calculate the new value distance to the associated producer
723 MCOperand const &MCO =
724 MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI));
725 unsigned SOffset = 0;
726 unsigned VOffset = 0;
727 unsigned Register = MCO.getReg();
728 unsigned Register1;
729 unsigned Register2;
730 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
731 auto i = Instructions.begin() + *CurrentIndex - 1;
732 for (;; --i) {
733 assert(i != Instructions.begin() - 1 && "Couldn't find producer");
734 MCInst const &Inst = *i->getInst();
735 if (HexagonMCInstrInfo::isImmext(Inst))
736 continue;
737 ++SOffset;
738 if (HexagonMCInstrInfo::isVector(MCII, Inst))
739 // Vector instructions don't count scalars
740 ++VOffset;
741 Register1 =
742 HexagonMCInstrInfo::hasNewValue(MCII, Inst)
743 ? HexagonMCInstrInfo::getNewValueOperand(MCII, Inst).getReg()
744 : static_cast<unsigned>(Hexagon::NoRegister);
745 Register2 =
746 HexagonMCInstrInfo::hasNewValue2(MCII, Inst)
747 ? HexagonMCInstrInfo::getNewValueOperand2(MCII, Inst).getReg()
748 : static_cast<unsigned>(Hexagon::NoRegister);
749 if (!RegisterMatches(Register, Register1, Register2))
750 // This isn't the register we're looking for
751 continue;
752 if (!HexagonMCInstrInfo::isPredicated(MCII, Inst))
753 // Producer is unpredicated
754 break;
755 assert(HexagonMCInstrInfo::isPredicated(MCII, MI) &&
756 "Unpredicated consumer depending on predicated producer");
757 if (HexagonMCInstrInfo::isPredicatedTrue(MCII, Inst) ==
758 HexagonMCInstrInfo::isPredicatedTrue(MCII, MI))
759 // Producer predicate sense matched ours
760 break;
761 }
762 // Hexagon PRM 10.11 Construct Nt from distance
763 unsigned Offset =
764 HexagonMCInstrInfo::isVector(MCII, MI) ? VOffset : SOffset;
765 Offset <<= 1;
766 Offset |=
767 HexagonMCInstrInfo::SubregisterBit(Register, Register1, Register2);
768 return Offset;
769 }
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000770 assert(!MO.isImm());
771 if (MO.isReg()) {
772 unsigned Reg = MO.getReg();
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000773 if (HexagonMCInstrInfo::isSubInstruction(MI) ||
774 llvm::HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCJ)
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000775 return HexagonMCInstrInfo::getDuplexRegisterNumbering(Reg);
776 switch(MI.getOpcode()){
777 case Hexagon::A2_tfrrcr:
778 case Hexagon::A2_tfrcrr:
779 if(Reg == Hexagon::M0)
780 Reg = Hexagon::C6;
781 if(Reg == Hexagon::M1)
782 Reg = Hexagon::C7;
783 }
784 return MCT.getRegisterInfo()->getEncodingValue(Reg);
785 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000786
Colin LeMahieub6625652015-05-01 21:14:21 +0000787 return getExprOpValue(MI, MO, MO.getExpr(), Fixups, STI);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000788}
789
Sid Manning7da3f9a2014-10-03 13:18:11 +0000790MCCodeEmitter *llvm::createHexagonMCCodeEmitter(MCInstrInfo const &MII,
791 MCRegisterInfo const &MRI,
Sid Manning7da3f9a2014-10-03 13:18:11 +0000792 MCContext &MCT) {
Eric Christopher0169e422015-03-10 22:03:14 +0000793 return new HexagonMCCodeEmitter(MII, MCT);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000794}
795
Daniel Sanders72db2a32016-11-19 13:05:44 +0000796#define ENABLE_INSTR_PREDICATE_VERIFIER
Sid Manning7da3f9a2014-10-03 13:18:11 +0000797#include "HexagonGenMCCodeEmitter.inc"