blob: 631c38c2734f363a731f8f4ff5519cbe1cecf9f5 [file] [log] [blame]
Eugene Zelenko52889212017-08-01 21:20:10 +00001//===- HexagonMCCodeEmitter.cpp - Hexagon Target Descriptions -------------===//
Sid Manning7da3f9a2014-10-03 13:18:11 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth6bda14b2017-06-06 11:49:48 +000010#include "MCTargetDesc/HexagonMCCodeEmitter.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000011#include "Hexagon.h"
12#include "MCTargetDesc/HexagonBaseInfo.h"
Colin LeMahieub6625652015-05-01 21:14:21 +000013#include "MCTargetDesc/HexagonFixupKinds.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000014#include "MCTargetDesc/HexagonMCExpr.h"
Colin LeMahieuaf304e52015-02-19 19:00:00 +000015#include "MCTargetDesc/HexagonMCInstrInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000016#include "MCTargetDesc/HexagonMCTargetDesc.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000017#include "llvm/ADT/Statistic.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000018#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCExpr.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000020#include "llvm/MC/MCFixup.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000021#include "llvm/MC/MCInst.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000022#include "llvm/MC/MCInstrDesc.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000023#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCRegisterInfo.h"
25#include "llvm/MC/MCSubtargetInfo.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000026#include "llvm/Support/Casting.h"
27#include "llvm/Support/Compiler.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000028#include "llvm/Support/Debug.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000029#include "llvm/Support/Endian.h"
Benjamin Kramer50e2a292015-06-04 15:03:02 +000030#include "llvm/Support/EndianStream.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000031#include "llvm/Support/ErrorHandling.h"
Sid Manning7da3f9a2014-10-03 13:18:11 +000032#include "llvm/Support/raw_ostream.h"
Eugene Zelenko52889212017-08-01 21:20:10 +000033#include <cassert>
34#include <cstddef>
35#include <cstdint>
36#include <string>
Sid Manning7da3f9a2014-10-03 13:18:11 +000037
38#define DEBUG_TYPE "mccodeemitter"
39
40using namespace llvm;
41using namespace Hexagon;
42
43STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
44
Sid Manning7da3f9a2014-10-03 13:18:11 +000045HexagonMCCodeEmitter::HexagonMCCodeEmitter(MCInstrInfo const &aMII,
Sid Manning7da3f9a2014-10-03 13:18:11 +000046 MCContext &aMCT)
Colin LeMahieub6625652015-05-01 21:14:21 +000047 : MCT(aMCT), MCII(aMII), Addend(new unsigned(0)),
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000048 Extended(new bool(false)), CurrentBundle(new MCInst const *),
49 CurrentIndex(new size_t(0)) {}
Colin LeMahieu68d967d2015-05-29 14:44:13 +000050
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000051uint32_t HexagonMCCodeEmitter::parseBits(size_t Last,
Colin LeMahieu68d967d2015-05-29 14:44:13 +000052 MCInst const &MCB,
53 MCInst const &MCI) const {
Colin LeMahieube8c4532015-06-05 16:00:11 +000054 bool Duplex = HexagonMCInstrInfo::isDuplex(MCII, MCI);
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000055 if (*CurrentIndex == 0) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000056 if (HexagonMCInstrInfo::isInnerLoop(MCB)) {
Colin LeMahieube8c4532015-06-05 16:00:11 +000057 assert(!Duplex);
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000058 assert(*CurrentIndex != Last);
Colin LeMahieu68d967d2015-05-29 14:44:13 +000059 return HexagonII::INST_PARSE_LOOP_END;
60 }
61 }
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000062 if (*CurrentIndex == 1) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000063 if (HexagonMCInstrInfo::isOuterLoop(MCB)) {
Colin LeMahieube8c4532015-06-05 16:00:11 +000064 assert(!Duplex);
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000065 assert(*CurrentIndex != Last);
Colin LeMahieu68d967d2015-05-29 14:44:13 +000066 return HexagonII::INST_PARSE_LOOP_END;
67 }
68 }
Colin LeMahieube8c4532015-06-05 16:00:11 +000069 if (Duplex) {
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000070 assert(*CurrentIndex == Last);
Colin LeMahieube8c4532015-06-05 16:00:11 +000071 return HexagonII::INST_PARSE_DUPLEX;
72 }
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000073 if(*CurrentIndex == Last)
Colin LeMahieu68d967d2015-05-29 14:44:13 +000074 return HexagonII::INST_PARSE_PACKET_END;
75 return HexagonII::INST_PARSE_NOT_END;
76}
Sid Manning7da3f9a2014-10-03 13:18:11 +000077
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000078/// EncodeInstruction - Emit the bundle
79void HexagonMCCodeEmitter::encodeInstruction(const MCInst &MI, raw_ostream &OS,
Sid Manning7da3f9a2014-10-03 13:18:11 +000080 SmallVectorImpl<MCFixup> &Fixups,
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000081 const MCSubtargetInfo &STI) const {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000082 MCInst &HMB = const_cast<MCInst &>(MI);
83
84 assert(HexagonMCInstrInfo::isBundle(HMB));
85 DEBUG(dbgs() << "Encoding bundle\n";);
86 *Addend = 0;
87 *Extended = false;
88 *CurrentBundle = &MI;
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000089 *CurrentIndex = 0;
Colin LeMahieu68d967d2015-05-29 14:44:13 +000090 size_t Last = HexagonMCInstrInfo::bundleSize(HMB) - 1;
91 for (auto &I : HexagonMCInstrInfo::bundleInstructions(HMB)) {
92 MCInst &HMI = const_cast<MCInst &>(*I.getInst());
Daniel Sanders72db2a32016-11-19 13:05:44 +000093 verifyInstructionPredicates(HMI,
94 computeAvailableFeatures(STI.getFeatureBits()));
95
Colin LeMahieu68d967d2015-05-29 14:44:13 +000096 EncodeSingleInstruction(HMI, OS, Fixups, STI,
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +000097 parseBits(Last, HMB, HMI));
Colin LeMahieu68d967d2015-05-29 14:44:13 +000098 *Extended = HexagonMCInstrInfo::isImmext(HMI);
99 *Addend += HEXAGON_INSTR_SIZE;
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000100 ++*CurrentIndex;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000101 }
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000102}
103
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000104static bool RegisterMatches(unsigned Consumer, unsigned Producer,
105 unsigned Producer2) {
106 if (Consumer == Producer)
107 return true;
108 if (Consumer == Producer2)
109 return true;
110 // Calculate if we're a single vector consumer referencing a double producer
111 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
112 if (Consumer >= Hexagon::V0 && Consumer <= Hexagon::V31)
113 return ((Consumer - Hexagon::V0) >> 1) == (Producer - Hexagon::W0);
114 return false;
115}
116
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000117/// EncodeSingleInstruction - Emit a single
118void HexagonMCCodeEmitter::EncodeSingleInstruction(
119 const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000120 const MCSubtargetInfo &STI, uint32_t Parse) const {
121 assert(!HexagonMCInstrInfo::isBundle(MI));
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000122 uint64_t Binary;
123
124 // Pseudo instructions don't get encoded and shouldn't be here
125 // in the first place!
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000126 assert(!HexagonMCInstrInfo::getDesc(MCII, MI).isPseudo() &&
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000127 "pseudo-instruction found");
128 DEBUG(dbgs() << "Encoding insn"
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000129 " `" << HexagonMCInstrInfo::getName(MCII, MI) << "'"
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000130 "\n");
131
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000132 Binary = getBinaryCodeForInstr(MI, Fixups, STI);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000133 // Check for unimplemented instructions. Immediate extenders
134 // are encoded as zero, so they need to be accounted for.
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000135 if (!Binary &&
136 MI.getOpcode() != DuplexIClass0 &&
137 MI.getOpcode() != A4_ext) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000138 DEBUG(dbgs() << "Unimplemented inst: "
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000139 " `" << HexagonMCInstrInfo::getName(MCII, MI) << "'"
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000140 "\n");
141 llvm_unreachable("Unimplemented Instruction");
142 }
143 Binary |= Parse;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000144
145 // if we need to emit a duplexed instruction
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000146 if (MI.getOpcode() >= Hexagon::DuplexIClass0 &&
147 MI.getOpcode() <= Hexagon::DuplexIClassF) {
Colin LeMahieube8c4532015-06-05 16:00:11 +0000148 assert(Parse == HexagonII::INST_PARSE_DUPLEX &&
149 "Emitting duplex without duplex parse bits");
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000150 unsigned dupIClass = MI.getOpcode() - Hexagon::DuplexIClass0;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000151 // 29 is the bit position.
152 // 0b1110 =0xE bits are masked off and down shifted by 1 bit.
153 // Last bit is moved to bit position 13
154 Binary = ((dupIClass & 0xE) << (29 - 1)) | ((dupIClass & 0x1) << 13);
155
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000156 const MCInst *subInst0 = MI.getOperand(0).getInst();
157 const MCInst *subInst1 = MI.getOperand(1).getInst();
Colin LeMahieube8c4532015-06-05 16:00:11 +0000158
159 // get subinstruction slot 0
160 unsigned subInstSlot0Bits = getBinaryCodeForInstr(*subInst0, Fixups, STI);
161 // get subinstruction slot 1
162 unsigned subInstSlot1Bits = getBinaryCodeForInstr(*subInst1, Fixups, STI);
163
164 Binary |= subInstSlot0Bits | (subInstSlot1Bits << 16);
165 }
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000166 support::endian::Writer<support::little>(OS).write<uint32_t>(Binary);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000167 ++MCNumEmitted;
168}
169
Florian Hahn239e4b92017-07-26 16:07:51 +0000170LLVM_ATTRIBUTE_NORETURN
Eugene Zelenko52889212017-08-01 21:20:10 +0000171static void raise_relocation_error(unsigned bits, unsigned kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000172 std::string Text;
173 {
Eugene Zelenko52889212017-08-01 21:20:10 +0000174 raw_string_ostream Stream(Text);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000175 Stream << "Unrecognized relocation combination bits: " << bits
176 << " kind: " << kind;
177 }
178 report_fatal_error(Text);
179}
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000180
181/// getFixupNoBits - Some insns are not extended and thus have no
182/// bits. These cases require a more brute force method for determining
183/// the correct relocation.
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000184Hexagon::Fixups HexagonMCCodeEmitter::getFixupNoBits(
185 MCInstrInfo const &MCII, const MCInst &MI, const MCOperand &MO,
186 const MCSymbolRefExpr::VariantKind kind) const {
Colin LeMahieub6625652015-05-01 21:14:21 +0000187 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
Eugene Zelenko52889212017-08-01 21:20:10 +0000188 unsigned insnType = HexagonMCInstrInfo::getType(MCII, MI);
Colin LeMahieub6625652015-05-01 21:14:21 +0000189
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000190 if (insnType == HexagonII::TypeEXTENDER) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000191 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000192 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000193 return Hexagon::fixup_Hexagon_GOTREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000194 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000195 return Hexagon::fixup_Hexagon_GOT_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000196 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000197 return Hexagon::fixup_Hexagon_TPREL_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000198 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000199 return Hexagon::fixup_Hexagon_DTPREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000200 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000201 return Hexagon::fixup_Hexagon_GD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000202 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000203 return Hexagon::fixup_Hexagon_LD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000204 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000205 return Hexagon::fixup_Hexagon_IE_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000206 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000207 return Hexagon::fixup_Hexagon_IE_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000208 case MCSymbolRefExpr::VK_Hexagon_PCREL:
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000209 return Hexagon::fixup_Hexagon_B32_PCREL_X;
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000210 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
211 return Hexagon::fixup_Hexagon_GD_PLT_B32_PCREL_X;
212 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
213 return Hexagon::fixup_Hexagon_LD_PLT_B32_PCREL_X;
214
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000215 case MCSymbolRefExpr::VK_None: {
216 auto Insts = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000217 for (auto I = Insts.begin(), N = Insts.end(); I != N; ++I) {
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000218 if (I->getInst() == &MI) {
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000219 const MCInst &NextI = *(I+1)->getInst();
220 const MCInstrDesc &D = HexagonMCInstrInfo::getDesc(MCII, NextI);
221 if (D.isBranch() || D.isCall() ||
222 HexagonMCInstrInfo::getType(MCII, NextI) == HexagonII::TypeCR)
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000223 return Hexagon::fixup_Hexagon_B32_PCREL_X;
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000224 return Hexagon::fixup_Hexagon_32_6_X;
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000225 }
Krzysztof Parzyszeke17b0bf2017-02-02 20:21:56 +0000226 }
Krzysztof Parzyszek357b0482017-02-02 19:58:22 +0000227 raise_relocation_error(0, kind);
228 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000229 default:
230 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000231 }
232 } else if (MCID.isBranch())
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000233 return Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000234
235 switch (MCID.getOpcode()) {
236 case Hexagon::HI:
237 case Hexagon::A2_tfrih:
238 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000239 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000240 return Hexagon::fixup_Hexagon_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000241 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000242 return Hexagon::fixup_Hexagon_GOTREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000243 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000244 return Hexagon::fixup_Hexagon_GD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000245 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000246 return Hexagon::fixup_Hexagon_LD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000247 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000248 return Hexagon::fixup_Hexagon_IE_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000249 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000250 return Hexagon::fixup_Hexagon_IE_GOT_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000251 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000252 return Hexagon::fixup_Hexagon_TPREL_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000253 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000254 return Hexagon::fixup_Hexagon_DTPREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000255 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000256 return Hexagon::fixup_Hexagon_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000257 default:
258 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000259 }
260
261 case Hexagon::LO:
262 case Hexagon::A2_tfril:
263 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000264 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000265 return Hexagon::fixup_Hexagon_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000266 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000267 return Hexagon::fixup_Hexagon_GOTREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000268 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000269 return Hexagon::fixup_Hexagon_GD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000270 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000271 return Hexagon::fixup_Hexagon_LD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000272 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000273 return Hexagon::fixup_Hexagon_IE_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000274 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000275 return Hexagon::fixup_Hexagon_IE_GOT_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000276 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000277 return Hexagon::fixup_Hexagon_TPREL_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000278 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000279 return Hexagon::fixup_Hexagon_DTPREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000280 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000281 return Hexagon::fixup_Hexagon_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000282 default:
283 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000284 }
285
286 // The only relocs left should be GP relative:
287 default:
288 if (MCID.mayStore() || MCID.mayLoad()) {
Chad Rosierc00ab4f2016-02-18 17:49:57 +0000289 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
290 ++ImpUses) {
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000291 if (*ImpUses != Hexagon::GP)
292 continue;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +0000293 switch (HexagonMCInstrInfo::getMemAccessSize(MCII, MI)) {
294 case 1:
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000295 return fixup_Hexagon_GPREL16_0;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +0000296 case 2:
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000297 return fixup_Hexagon_GPREL16_1;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +0000298 case 4:
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000299 return fixup_Hexagon_GPREL16_2;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +0000300 case 8:
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000301 return fixup_Hexagon_GPREL16_3;
302 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000303 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000304 }
305 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000306 }
307 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000308 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000309 llvm_unreachable("Relocation exit not taken");
310}
Colin LeMahieub6625652015-05-01 21:14:21 +0000311
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000312namespace llvm {
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000313
Eugene Zelenko52889212017-08-01 21:20:10 +0000314extern const MCInstrDesc HexagonInsts[];
315
316} // end namespace llvm
317
318static bool isPCRel (unsigned Kind) {
319 switch(Kind){
320 case fixup_Hexagon_B22_PCREL:
321 case fixup_Hexagon_B15_PCREL:
322 case fixup_Hexagon_B7_PCREL:
323 case fixup_Hexagon_B13_PCREL:
324 case fixup_Hexagon_B9_PCREL:
325 case fixup_Hexagon_B32_PCREL_X:
326 case fixup_Hexagon_B22_PCREL_X:
327 case fixup_Hexagon_B15_PCREL_X:
328 case fixup_Hexagon_B13_PCREL_X:
329 case fixup_Hexagon_B9_PCREL_X:
330 case fixup_Hexagon_B7_PCREL_X:
331 case fixup_Hexagon_32_PCREL:
332 case fixup_Hexagon_PLT_B22_PCREL:
333 case fixup_Hexagon_GD_PLT_B22_PCREL:
334 case fixup_Hexagon_LD_PLT_B22_PCREL:
335 case fixup_Hexagon_GD_PLT_B22_PCREL_X:
336 case fixup_Hexagon_LD_PLT_B22_PCREL_X:
337 case fixup_Hexagon_6_PCREL_X:
338 return true;
339 default:
340 return false;
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000341 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000342}
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000343
Colin LeMahieub6625652015-05-01 21:14:21 +0000344unsigned HexagonMCCodeEmitter::getExprOpValue(const MCInst &MI,
345 const MCOperand &MO,
346 const MCExpr *ME,
347 SmallVectorImpl<MCFixup> &Fixups,
348 const MCSubtargetInfo &STI) const
Colin LeMahieub6625652015-05-01 21:14:21 +0000349{
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000350 if (isa<HexagonMCExpr>(ME))
351 ME = &HexagonMCInstrInfo::getExpr(*ME);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000352 int64_t Value;
353 if (ME->evaluateAsAbsolute(Value))
354 return Value;
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000355 assert(ME->getKind() == MCExpr::SymbolRef ||
356 ME->getKind() == MCExpr::Binary);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000357 if (ME->getKind() == MCExpr::Binary) {
358 MCBinaryExpr const *Binary = cast<MCBinaryExpr>(ME);
359 getExprOpValue(MI, MO, Binary->getLHS(), Fixups, STI);
360 getExprOpValue(MI, MO, Binary->getRHS(), Fixups, STI);
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000361 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000362 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000363 Hexagon::Fixups FixupKind =
364 Hexagon::Fixups(Hexagon::fixup_Hexagon_TPREL_LO16);
365 const MCSymbolRefExpr *MCSRE = static_cast<const MCSymbolRefExpr *>(ME);
366 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
Colin LeMahieub6625652015-05-01 21:14:21 +0000367 unsigned bits = HexagonMCInstrInfo::getExtentBits(MCII, MI) -
368 HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
369 const MCSymbolRefExpr::VariantKind kind = MCSRE->getKind();
370
371 DEBUG(dbgs() << "----------------------------------------\n");
372 DEBUG(dbgs() << "Opcode Name: " << HexagonMCInstrInfo::getName(MCII, MI)
373 << "\n");
Colin LeMahieu6efd2732015-05-01 21:30:22 +0000374 DEBUG(dbgs() << "Opcode: " << MCID.getOpcode() << "\n");
Colin LeMahieub6625652015-05-01 21:14:21 +0000375 DEBUG(dbgs() << "Relocation bits: " << bits << "\n");
376 DEBUG(dbgs() << "Addend: " << *Addend << "\n");
377 DEBUG(dbgs() << "----------------------------------------\n");
378
379 switch (bits) {
380 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000381 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000382 case 32:
383 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000384 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000385 FixupKind = *Extended ? Hexagon::fixup_Hexagon_DTPREL_32_6_X
386 : Hexagon::fixup_Hexagon_DTPREL_32;
387 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000388 case MCSymbolRefExpr::VK_GOT:
389 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOT_32_6_X
390 : Hexagon::fixup_Hexagon_GOT_32;
391 break;
392 case MCSymbolRefExpr::VK_GOTREL:
393 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOTREL_32_6_X
394 : Hexagon::fixup_Hexagon_GOTREL_32;
395 break;
396 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
397 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_GOT_32_6_X
398 : Hexagon::fixup_Hexagon_GD_GOT_32;
399 break;
400 case MCSymbolRefExpr::VK_Hexagon_IE:
401 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_32_6_X
402 : Hexagon::fixup_Hexagon_IE_32;
403 break;
404 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
405 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_GOT_32_6_X
406 : Hexagon::fixup_Hexagon_IE_GOT_32;
407 break;
408 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
409 FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_GOT_32_6_X
410 : Hexagon::fixup_Hexagon_LD_GOT_32;
411 break;
412 case MCSymbolRefExpr::VK_Hexagon_PCREL:
413 FixupKind = Hexagon::fixup_Hexagon_32_PCREL;
414 break;
415 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000416 FixupKind =
417 *Extended ? Hexagon::fixup_Hexagon_32_6_X : Hexagon::fixup_Hexagon_32;
418 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000419 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000420 FixupKind = *Extended ? Hexagon::fixup_Hexagon_TPREL_32_6_X
421 : Hexagon::fixup_Hexagon_TPREL_32;
422 break;
423 default:
424 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000425 }
426 break;
427
428 case 22:
429 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000430 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000431 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL_X
432 : Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000433 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000434 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000435 FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL_X
436 : Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000437 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000438 case MCSymbolRefExpr::VK_None:
439 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B22_PCREL_X
440 : Hexagon::fixup_Hexagon_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000441 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000442 case MCSymbolRefExpr::VK_PLT:
443 FixupKind = Hexagon::fixup_Hexagon_PLT_B22_PCREL;
444 break;
445 default:
446 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000447 }
448 break;
449
450 case 16:
451 if (*Extended) {
452 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000453 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000454 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
455 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000456 case MCSymbolRefExpr::VK_GOT:
457 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
458 break;
459 case MCSymbolRefExpr::VK_GOTREL:
460 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
461 break;
462 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
463 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16_X;
464 break;
465 case MCSymbolRefExpr::VK_Hexagon_IE:
466 FixupKind = Hexagon::fixup_Hexagon_IE_16_X;
467 break;
468 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
469 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16_X;
470 break;
471 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
472 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16_X;
473 break;
474 case MCSymbolRefExpr::VK_None:
475 FixupKind = Hexagon::fixup_Hexagon_16_X;
476 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000477 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000478 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
479 break;
480 default:
481 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000482 }
483 } else
484 switch (kind) {
Eugene Zelenko52889212017-08-01 21:20:10 +0000485 case MCSymbolRefExpr::VK_None:
Krzysztof Parzyszek57a8bb432017-05-02 18:19:11 +0000486 if (HexagonMCInstrInfo::s27_2_reloc(*MO.getExpr()))
487 FixupKind = Hexagon::fixup_Hexagon_27_REG;
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000488 else
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000489 if (MCID.mayStore() || MCID.mayLoad()) {
490 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000491 ++ImpUses) {
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000492 if (*ImpUses != Hexagon::GP)
493 continue;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +0000494 switch (HexagonMCInstrInfo::getMemAccessSize(MCII, MI)) {
495 case 1:
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000496 FixupKind = fixup_Hexagon_GPREL16_0;
497 break;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +0000498 case 2:
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000499 FixupKind = fixup_Hexagon_GPREL16_1;
500 break;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +0000501 case 4:
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000502 FixupKind = fixup_Hexagon_GPREL16_2;
503 break;
Krzysztof Parzyszek473d02d2017-09-14 12:06:40 +0000504 case 8:
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000505 FixupKind = fixup_Hexagon_GPREL16_3;
506 break;
507 default:
508 raise_relocation_error(bits, kind);
509 }
510 }
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000511 } else
Krzysztof Parzyszekd0d42f02017-02-02 20:35:12 +0000512 raise_relocation_error(bits, kind);
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000513 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000514 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000515 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16;
Colin LeMahieub6625652015-05-01 21:14:21 +0000516 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000517 case MCSymbolRefExpr::VK_GOTREL:
518 if (MCID.getOpcode() == Hexagon::HI)
Colin LeMahieub6625652015-05-01 21:14:21 +0000519 FixupKind = Hexagon::fixup_Hexagon_GOTREL_HI16;
520 else
521 FixupKind = Hexagon::fixup_Hexagon_GOTREL_LO16;
522 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000523 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000524 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16;
525 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000526 case MCSymbolRefExpr::VK_Hexagon_GPREL:
527 FixupKind = Hexagon::fixup_Hexagon_GPREL16_0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000528 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000529 case MCSymbolRefExpr::VK_Hexagon_HI16:
530 FixupKind = Hexagon::fixup_Hexagon_HI16;
531 break;
532 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000533 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16;
534 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000535 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
536 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16;
537 break;
538 case MCSymbolRefExpr::VK_Hexagon_LO16:
539 FixupKind = Hexagon::fixup_Hexagon_LO16;
540 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000541 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000542 FixupKind = Hexagon::fixup_Hexagon_TPREL_16;
543 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000544 default:
545 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000546 }
547 break;
548
549 case 15:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000550 switch (kind) {
551 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000552 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B15_PCREL_X
553 : Hexagon::fixup_Hexagon_B15_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000554 break;
555 default:
556 raise_relocation_error(bits, kind);
557 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000558 break;
559
560 case 13:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000561 switch (kind) {
562 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000563 FixupKind = Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000564 break;
565 default:
566 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000567 }
568 break;
569
570 case 12:
571 if (*Extended)
572 switch (kind) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000573 // There isn't a GOT_12_X, both 11_X and 16_X resolve to 6/26
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000574 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000575 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
576 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000577 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000578 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
579 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000580 case MCSymbolRefExpr::VK_None:
581 FixupKind = Hexagon::fixup_Hexagon_12_X;
582 break;
583 default:
584 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000585 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000586 else
587 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000588 break;
589
590 case 11:
591 if (*Extended)
592 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000593 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000594 FixupKind = Hexagon::fixup_Hexagon_DTPREL_11_X;
595 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000596 case MCSymbolRefExpr::VK_GOT:
597 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
598 break;
599 case MCSymbolRefExpr::VK_GOTREL:
600 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
601 break;
602 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
603 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_11_X;
604 break;
605 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
606 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_11_X;
607 break;
608 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
609 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_11_X;
610 break;
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000611 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
612 FixupKind = Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL_X;
613 break;
614 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
615 FixupKind = Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL_X;
616 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000617 case MCSymbolRefExpr::VK_None:
618 FixupKind = Hexagon::fixup_Hexagon_11_X;
619 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000620 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000621 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
622 break;
623 default:
624 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000625 }
626 else {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000627 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000628 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000629 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
630 break;
631 default:
632 raise_relocation_error(bits, kind);
633 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000634 }
635 break;
636
637 case 10:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000638 if (*Extended) {
639 switch (kind) {
640 case MCSymbolRefExpr::VK_None:
641 FixupKind = Hexagon::fixup_Hexagon_10_X;
642 break;
643 default:
644 raise_relocation_error(bits, kind);
645 }
646 } else
647 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000648 break;
649
650 case 9:
651 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000652 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000653 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B9_PCREL_X
654 : Hexagon::fixup_Hexagon_B9_PCREL;
655 else if (*Extended)
656 FixupKind = Hexagon::fixup_Hexagon_9_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000657 else
658 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000659 break;
660
661 case 8:
662 if (*Extended)
663 FixupKind = Hexagon::fixup_Hexagon_8_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 7:
669 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000670 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000671 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B7_PCREL_X
672 : Hexagon::fixup_Hexagon_B7_PCREL;
673 else if (*Extended)
674 FixupKind = Hexagon::fixup_Hexagon_7_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000675 else
676 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000677 break;
678
679 case 6:
680 if (*Extended) {
681 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000682 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000683 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
Colin LeMahieub6625652015-05-01 21:14:21 +0000684 break;
685 // This is part of an extender, GOT_11 is a
686 // Word32_U6 unsigned/truncated reloc.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000687 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000688 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
689 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000690 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000691 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
692 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000693 case MCSymbolRefExpr::VK_Hexagon_PCREL:
694 FixupKind = Hexagon::fixup_Hexagon_6_PCREL_X;
695 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000696 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000697 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
698 break;
699 case MCSymbolRefExpr::VK_None:
700 FixupKind = Hexagon::fixup_Hexagon_6_X;
701 break;
702 default:
703 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000704 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000705 } else
706 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000707 break;
708
709 case 0:
710 FixupKind = getFixupNoBits(MCII, MI, MO, kind);
711 break;
712 }
713
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000714 MCExpr const *FixupExpression =
715 (*Addend > 0 && isPCRel(FixupKind))
716 ? MCBinaryExpr::createAdd(MO.getExpr(),
717 MCConstantExpr::create(*Addend, MCT), MCT)
718 : MO.getExpr();
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000719
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000720 MCFixup fixup = MCFixup::create(*Addend, FixupExpression,
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000721 MCFixupKind(FixupKind), MI.getLoc());
Colin LeMahieub6625652015-05-01 21:14:21 +0000722 Fixups.push_back(fixup);
723 // All of the information is in the fixup.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000724 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000725}
726
Sid Manning7da3f9a2014-10-03 13:18:11 +0000727unsigned
728HexagonMCCodeEmitter::getMachineOpValue(MCInst const &MI, MCOperand const &MO,
729 SmallVectorImpl<MCFixup> &Fixups,
730 MCSubtargetInfo const &STI) const {
Simon Pilgrimee6ef4d2017-02-19 00:03:46 +0000731#ifndef NDEBUG
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000732 size_t OperandNumber = ~0U;
733 for (unsigned i = 0, n = MI.getNumOperands(); i < n; ++i)
734 if (&MI.getOperand(i) == &MO) {
735 OperandNumber = i;
736 break;
737 }
738 assert((OperandNumber != ~0U) && "Operand not found");
Simon Pilgrimee6ef4d2017-02-19 00:03:46 +0000739#endif
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000740
741 if (HexagonMCInstrInfo::isNewValue(MCII, MI) &&
742 &MO == &MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI))) {
743 // Calculate the new value distance to the associated producer
744 MCOperand const &MCO =
745 MI.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, MI));
746 unsigned SOffset = 0;
747 unsigned VOffset = 0;
748 unsigned Register = MCO.getReg();
749 unsigned Register1;
750 unsigned Register2;
751 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
752 auto i = Instructions.begin() + *CurrentIndex - 1;
753 for (;; --i) {
754 assert(i != Instructions.begin() - 1 && "Couldn't find producer");
755 MCInst const &Inst = *i->getInst();
756 if (HexagonMCInstrInfo::isImmext(Inst))
757 continue;
758 ++SOffset;
759 if (HexagonMCInstrInfo::isVector(MCII, Inst))
760 // Vector instructions don't count scalars
761 ++VOffset;
762 Register1 =
763 HexagonMCInstrInfo::hasNewValue(MCII, Inst)
764 ? HexagonMCInstrInfo::getNewValueOperand(MCII, Inst).getReg()
765 : static_cast<unsigned>(Hexagon::NoRegister);
766 Register2 =
767 HexagonMCInstrInfo::hasNewValue2(MCII, Inst)
768 ? HexagonMCInstrInfo::getNewValueOperand2(MCII, Inst).getReg()
769 : static_cast<unsigned>(Hexagon::NoRegister);
770 if (!RegisterMatches(Register, Register1, Register2))
771 // This isn't the register we're looking for
772 continue;
773 if (!HexagonMCInstrInfo::isPredicated(MCII, Inst))
774 // Producer is unpredicated
775 break;
776 assert(HexagonMCInstrInfo::isPredicated(MCII, MI) &&
777 "Unpredicated consumer depending on predicated producer");
778 if (HexagonMCInstrInfo::isPredicatedTrue(MCII, Inst) ==
779 HexagonMCInstrInfo::isPredicatedTrue(MCII, MI))
780 // Producer predicate sense matched ours
781 break;
782 }
783 // Hexagon PRM 10.11 Construct Nt from distance
784 unsigned Offset =
785 HexagonMCInstrInfo::isVector(MCII, MI) ? VOffset : SOffset;
786 Offset <<= 1;
787 Offset |=
788 HexagonMCInstrInfo::SubregisterBit(Register, Register1, Register2);
789 return Offset;
790 }
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000791 assert(!MO.isImm());
792 if (MO.isReg()) {
793 unsigned Reg = MO.getReg();
Krzysztof Parzyszekbc4dc9b2017-02-02 15:32:26 +0000794 if (HexagonMCInstrInfo::isSubInstruction(MI) ||
Eugene Zelenko52889212017-08-01 21:20:10 +0000795 HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCJ)
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000796 return HexagonMCInstrInfo::getDuplexRegisterNumbering(Reg);
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000797 return MCT.getRegisterInfo()->getEncodingValue(Reg);
798 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000799
Colin LeMahieub6625652015-05-01 21:14:21 +0000800 return getExprOpValue(MI, MO, MO.getExpr(), Fixups, STI);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000801}
802
Sid Manning7da3f9a2014-10-03 13:18:11 +0000803MCCodeEmitter *llvm::createHexagonMCCodeEmitter(MCInstrInfo const &MII,
804 MCRegisterInfo const &MRI,
Sid Manning7da3f9a2014-10-03 13:18:11 +0000805 MCContext &MCT) {
Eric Christopher0169e422015-03-10 22:03:14 +0000806 return new HexagonMCCodeEmitter(MII, MCT);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000807}
808
Daniel Sanders72db2a32016-11-19 13:05:44 +0000809#define ENABLE_INSTR_PREDICATE_VERIFIER
Sid Manning7da3f9a2014-10-03 13:18:11 +0000810#include "HexagonGenMCCodeEmitter.inc"