blob: 2645a17b9bd081814980bd71f2b62a6a55d8841a [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)),
Colin LeMahieu68d967d2015-05-29 14:44:13 +000038 Extended(new bool(false)), CurrentBundle(new MCInst const *) {}
39
40uint32_t HexagonMCCodeEmitter::parseBits(size_t Instruction, size_t Last,
41 MCInst const &MCB,
42 MCInst const &MCI) const {
Colin LeMahieube8c4532015-06-05 16:00:11 +000043 bool Duplex = HexagonMCInstrInfo::isDuplex(MCII, MCI);
Colin LeMahieu68d967d2015-05-29 14:44:13 +000044 if (Instruction == 0) {
45 if (HexagonMCInstrInfo::isInnerLoop(MCB)) {
Colin LeMahieube8c4532015-06-05 16:00:11 +000046 assert(!Duplex);
Colin LeMahieu68d967d2015-05-29 14:44:13 +000047 assert(Instruction != Last);
48 return HexagonII::INST_PARSE_LOOP_END;
49 }
50 }
51 if (Instruction == 1) {
52 if (HexagonMCInstrInfo::isOuterLoop(MCB)) {
Colin LeMahieube8c4532015-06-05 16:00:11 +000053 assert(!Duplex);
Colin LeMahieu68d967d2015-05-29 14:44:13 +000054 assert(Instruction != Last);
55 return HexagonII::INST_PARSE_LOOP_END;
56 }
57 }
Colin LeMahieube8c4532015-06-05 16:00:11 +000058 if (Duplex) {
59 assert(Instruction == Last);
60 return HexagonII::INST_PARSE_DUPLEX;
61 }
Colin LeMahieu68d967d2015-05-29 14:44:13 +000062 if(Instruction == Last)
63 return HexagonII::INST_PARSE_PACKET_END;
64 return HexagonII::INST_PARSE_NOT_END;
65}
Sid Manning7da3f9a2014-10-03 13:18:11 +000066
Jim Grosbach91df21f2015-05-15 19:13:16 +000067void HexagonMCCodeEmitter::encodeInstruction(MCInst const &MI, raw_ostream &OS,
Sid Manning7da3f9a2014-10-03 13:18:11 +000068 SmallVectorImpl<MCFixup> &Fixups,
69 MCSubtargetInfo const &STI) const {
Colin LeMahieu68d967d2015-05-29 14:44:13 +000070 MCInst &HMB = const_cast<MCInst &>(MI);
71
72 assert(HexagonMCInstrInfo::isBundle(HMB));
73 DEBUG(dbgs() << "Encoding bundle\n";);
74 *Addend = 0;
75 *Extended = false;
76 *CurrentBundle = &MI;
77 size_t Instruction = 0;
78 size_t Last = HexagonMCInstrInfo::bundleSize(HMB) - 1;
79 for (auto &I : HexagonMCInstrInfo::bundleInstructions(HMB)) {
80 MCInst &HMI = const_cast<MCInst &>(*I.getInst());
Daniel Sanders72db2a32016-11-19 13:05:44 +000081 verifyInstructionPredicates(HMI,
82 computeAvailableFeatures(STI.getFeatureBits()));
83
Colin LeMahieu68d967d2015-05-29 14:44:13 +000084 EncodeSingleInstruction(HMI, OS, Fixups, STI,
85 parseBits(Instruction, Last, HMB, HMI),
86 Instruction);
87 *Extended = HexagonMCInstrInfo::isImmext(HMI);
88 *Addend += HEXAGON_INSTR_SIZE;
89 ++Instruction;
90 }
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,
110 const MCSubtargetInfo &STI, uint32_t Parse, size_t Index) const {
111 MCInst HMB = MI;
112 assert(!HexagonMCInstrInfo::isBundle(HMB));
113 uint64_t Binary;
114
Colin LeMahieu13cc3ab2015-11-10 00:51:56 +0000115 // Compound instructions are limited to using registers 0-7 and 16-23
116 // and here we make a map 16-23 to 8-15 so they can be correctly encoded.
117 static unsigned RegMap[8] = {Hexagon::R8, Hexagon::R9, Hexagon::R10,
118 Hexagon::R11, Hexagon::R12, Hexagon::R13,
119 Hexagon::R14, Hexagon::R15};
120
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000121 // Pseudo instructions don't get encoded and shouldn't be here
122 // in the first place!
123 assert(!HexagonMCInstrInfo::getDesc(MCII, HMB).isPseudo() &&
124 "pseudo-instruction found");
125 DEBUG(dbgs() << "Encoding insn"
126 " `" << HexagonMCInstrInfo::getName(MCII, HMB) << "'"
127 "\n");
128
Colin LeMahieu13cc3ab2015-11-10 00:51:56 +0000129 if (llvm::HexagonMCInstrInfo::getType(MCII, HMB) == HexagonII::TypeCOMPOUND) {
130 for (unsigned i = 0; i < HMB.getNumOperands(); ++i)
131 if (HMB.getOperand(i).isReg()) {
132 unsigned Reg =
133 MCT.getRegisterInfo()->getEncodingValue(HMB.getOperand(i).getReg());
134 if ((Reg <= 23) && (Reg >= 16))
135 HMB.getOperand(i).setReg(RegMap[Reg - 16]);
136 }
137 }
138
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000139 if (HexagonMCInstrInfo::isNewValue(MCII, HMB)) {
140 // Calculate the new value distance to the associated producer
141 MCOperand &MCO =
142 HMB.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, HMB));
143 unsigned SOffset = 0;
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000144 unsigned VOffset = 0;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000145 unsigned Register = MCO.getReg();
146 unsigned Register1;
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000147 unsigned Register2;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000148 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
149 auto i = Instructions.begin() + Index - 1;
150 for (;; --i) {
151 assert(i != Instructions.begin() - 1 && "Couldn't find producer");
152 MCInst const &Inst = *i->getInst();
153 if (HexagonMCInstrInfo::isImmext(Inst))
154 continue;
155 ++SOffset;
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000156 if (HexagonMCInstrInfo::isVector(MCII, Inst))
157 // Vector instructions don't count scalars
158 ++VOffset;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000159 Register1 =
160 HexagonMCInstrInfo::hasNewValue(MCII, Inst)
161 ? HexagonMCInstrInfo::getNewValueOperand(MCII, Inst).getReg()
162 : static_cast<unsigned>(Hexagon::NoRegister);
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000163 Register2 =
164 HexagonMCInstrInfo::hasNewValue2(MCII, Inst)
165 ? HexagonMCInstrInfo::getNewValueOperand2(MCII, Inst).getReg()
166 : static_cast<unsigned>(Hexagon::NoRegister);
167 if (!RegisterMatches(Register, Register1, Register2))
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000168 // This isn't the register we're looking for
169 continue;
170 if (!HexagonMCInstrInfo::isPredicated(MCII, Inst))
171 // Producer is unpredicated
172 break;
173 assert(HexagonMCInstrInfo::isPredicated(MCII, HMB) &&
174 "Unpredicated consumer depending on predicated producer");
175 if (HexagonMCInstrInfo::isPredicatedTrue(MCII, Inst) ==
176 HexagonMCInstrInfo::isPredicatedTrue(MCII, HMB))
177 // Producer predicate sense matched ours
178 break;
179 }
180 // Hexagon PRM 10.11 Construct Nt from distance
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000181 unsigned Offset =
182 HexagonMCInstrInfo::isVector(MCII, HMB) ? VOffset : SOffset;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000183 Offset <<= 1;
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000184 Offset |=
185 HexagonMCInstrInfo::SubregisterBit(Register, Register1, Register2);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000186 MCO.setReg(Offset + Hexagon::R0);
187 }
188
189 Binary = getBinaryCodeForInstr(HMB, Fixups, STI);
190 // Check for unimplemented instructions. Immediate extenders
191 // are encoded as zero, so they need to be accounted for.
192 if ((!Binary) &&
193 ((HMB.getOpcode() != DuplexIClass0) && (HMB.getOpcode() != A4_ext) &&
194 (HMB.getOpcode() != A4_ext_b) && (HMB.getOpcode() != A4_ext_c) &&
195 (HMB.getOpcode() != A4_ext_g))) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000196 DEBUG(dbgs() << "Unimplemented inst: "
197 " `" << HexagonMCInstrInfo::getName(MCII, HMB) << "'"
198 "\n");
199 llvm_unreachable("Unimplemented Instruction");
200 }
201 Binary |= Parse;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000202
203 // if we need to emit a duplexed instruction
204 if (HMB.getOpcode() >= Hexagon::DuplexIClass0 &&
205 HMB.getOpcode() <= Hexagon::DuplexIClassF) {
206 assert(Parse == HexagonII::INST_PARSE_DUPLEX &&
207 "Emitting duplex without duplex parse bits");
208 unsigned dupIClass;
209 switch (HMB.getOpcode()) {
210 case Hexagon::DuplexIClass0:
211 dupIClass = 0;
212 break;
213 case Hexagon::DuplexIClass1:
214 dupIClass = 1;
215 break;
216 case Hexagon::DuplexIClass2:
217 dupIClass = 2;
218 break;
219 case Hexagon::DuplexIClass3:
220 dupIClass = 3;
221 break;
222 case Hexagon::DuplexIClass4:
223 dupIClass = 4;
224 break;
225 case Hexagon::DuplexIClass5:
226 dupIClass = 5;
227 break;
228 case Hexagon::DuplexIClass6:
229 dupIClass = 6;
230 break;
231 case Hexagon::DuplexIClass7:
232 dupIClass = 7;
233 break;
234 case Hexagon::DuplexIClass8:
235 dupIClass = 8;
236 break;
237 case Hexagon::DuplexIClass9:
238 dupIClass = 9;
239 break;
240 case Hexagon::DuplexIClassA:
241 dupIClass = 10;
242 break;
243 case Hexagon::DuplexIClassB:
244 dupIClass = 11;
245 break;
246 case Hexagon::DuplexIClassC:
247 dupIClass = 12;
248 break;
249 case Hexagon::DuplexIClassD:
250 dupIClass = 13;
251 break;
252 case Hexagon::DuplexIClassE:
253 dupIClass = 14;
254 break;
255 case Hexagon::DuplexIClassF:
256 dupIClass = 15;
257 break;
258 default:
259 llvm_unreachable("Unimplemented DuplexIClass");
260 break;
261 }
262 // 29 is the bit position.
263 // 0b1110 =0xE bits are masked off and down shifted by 1 bit.
264 // Last bit is moved to bit position 13
265 Binary = ((dupIClass & 0xE) << (29 - 1)) | ((dupIClass & 0x1) << 13);
266
267 const MCInst *subInst0 = HMB.getOperand(0).getInst();
268 const MCInst *subInst1 = HMB.getOperand(1).getInst();
269
270 // get subinstruction slot 0
271 unsigned subInstSlot0Bits = getBinaryCodeForInstr(*subInst0, Fixups, STI);
272 // get subinstruction slot 1
273 unsigned subInstSlot1Bits = getBinaryCodeForInstr(*subInst1, Fixups, STI);
274
275 Binary |= subInstSlot0Bits | (subInstSlot1Bits << 16);
276 }
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000277 support::endian::Writer<support::little>(OS).write<uint32_t>(Binary);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000278 ++MCNumEmitted;
279}
280
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000281namespace {
282void raise_relocation_error(unsigned bits, unsigned kind) {
283 std::string Text;
284 {
285 llvm::raw_string_ostream Stream(Text);
286 Stream << "Unrecognized relocation combination bits: " << bits
287 << " kind: " << kind;
288 }
289 report_fatal_error(Text);
290}
291}
292
293/// getFixupNoBits - Some insns are not extended and thus have no
294/// bits. These cases require a more brute force method for determining
295/// the correct relocation.
296namespace {
297Hexagon::Fixups getFixupNoBits(MCInstrInfo const &MCII, const MCInst &MI,
Colin LeMahieub6625652015-05-01 21:14:21 +0000298 const MCOperand &MO,
299 const MCSymbolRefExpr::VariantKind kind) {
300 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
301 unsigned insnType = llvm::HexagonMCInstrInfo::getType(MCII, MI);
302
303 if (insnType == HexagonII::TypePREFIX) {
304 switch (kind) {
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_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000307 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000308 return Hexagon::fixup_Hexagon_GOT_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000309 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000310 return Hexagon::fixup_Hexagon_TPREL_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000311 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000312 return Hexagon::fixup_Hexagon_DTPREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000313 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000314 return Hexagon::fixup_Hexagon_GD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000315 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000316 return Hexagon::fixup_Hexagon_LD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000317 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000318 return Hexagon::fixup_Hexagon_IE_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000319 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000320 return Hexagon::fixup_Hexagon_IE_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000321 case MCSymbolRefExpr::VK_Hexagon_PCREL:
322 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000323 if (MCID.isBranch())
324 return Hexagon::fixup_Hexagon_B32_PCREL_X;
325 else
326 return Hexagon::fixup_Hexagon_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000327 default:
328 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000329 }
330 } else if (MCID.isBranch())
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000331 return Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000332
333 switch (MCID.getOpcode()) {
334 case Hexagon::HI:
335 case Hexagon::A2_tfrih:
336 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000337 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000338 return Hexagon::fixup_Hexagon_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000339 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000340 return Hexagon::fixup_Hexagon_GOTREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000341 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000342 return Hexagon::fixup_Hexagon_GD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000343 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000344 return Hexagon::fixup_Hexagon_LD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000345 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000346 return Hexagon::fixup_Hexagon_IE_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000347 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000348 return Hexagon::fixup_Hexagon_IE_GOT_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000349 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000350 return Hexagon::fixup_Hexagon_TPREL_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000351 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000352 return Hexagon::fixup_Hexagon_DTPREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000353 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000354 return Hexagon::fixup_Hexagon_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000355 default:
356 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000357 }
358
359 case Hexagon::LO:
360 case Hexagon::A2_tfril:
361 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000362 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000363 return Hexagon::fixup_Hexagon_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000364 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000365 return Hexagon::fixup_Hexagon_GOTREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000366 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000367 return Hexagon::fixup_Hexagon_GD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000368 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000369 return Hexagon::fixup_Hexagon_LD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000370 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000371 return Hexagon::fixup_Hexagon_IE_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000372 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000373 return Hexagon::fixup_Hexagon_IE_GOT_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000374 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000375 return Hexagon::fixup_Hexagon_TPREL_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000376 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000377 return Hexagon::fixup_Hexagon_DTPREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000378 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000379 return Hexagon::fixup_Hexagon_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000380 default:
381 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000382 }
383
384 // The only relocs left should be GP relative:
385 default:
386 if (MCID.mayStore() || MCID.mayLoad()) {
Chad Rosierc00ab4f2016-02-18 17:49:57 +0000387 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
388 ++ImpUses) {
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000389 if (*ImpUses != Hexagon::GP)
390 continue;
391 switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
392 case HexagonII::MemAccessSize::ByteAccess:
393 return fixup_Hexagon_GPREL16_0;
394 case HexagonII::MemAccessSize::HalfWordAccess:
395 return fixup_Hexagon_GPREL16_1;
396 case HexagonII::MemAccessSize::WordAccess:
397 return fixup_Hexagon_GPREL16_2;
398 case HexagonII::MemAccessSize::DoubleWordAccess:
399 return fixup_Hexagon_GPREL16_3;
400 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000401 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000402 }
403 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000404 }
405 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000406 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000407 llvm_unreachable("Relocation exit not taken");
408}
Colin LeMahieub6625652015-05-01 21:14:21 +0000409}
410
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000411namespace llvm {
412extern const MCInstrDesc HexagonInsts[];
413}
414
415namespace {
416 bool isPCRel (unsigned Kind) {
417 switch(Kind){
418 case fixup_Hexagon_B22_PCREL:
419 case fixup_Hexagon_B15_PCREL:
420 case fixup_Hexagon_B7_PCREL:
421 case fixup_Hexagon_B13_PCREL:
422 case fixup_Hexagon_B9_PCREL:
423 case fixup_Hexagon_B32_PCREL_X:
424 case fixup_Hexagon_B22_PCREL_X:
425 case fixup_Hexagon_B15_PCREL_X:
426 case fixup_Hexagon_B13_PCREL_X:
427 case fixup_Hexagon_B9_PCREL_X:
428 case fixup_Hexagon_B7_PCREL_X:
429 case fixup_Hexagon_32_PCREL:
430 case fixup_Hexagon_PLT_B22_PCREL:
431 case fixup_Hexagon_GD_PLT_B22_PCREL:
432 case fixup_Hexagon_LD_PLT_B22_PCREL:
433 case fixup_Hexagon_6_PCREL_X:
434 return true;
435 default:
436 return false;
437 }
438 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000439}
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000440
Colin LeMahieub6625652015-05-01 21:14:21 +0000441unsigned HexagonMCCodeEmitter::getExprOpValue(const MCInst &MI,
442 const MCOperand &MO,
443 const MCExpr *ME,
444 SmallVectorImpl<MCFixup> &Fixups,
445 const MCSubtargetInfo &STI) const
446
447{
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000448 if (isa<HexagonMCExpr>(ME))
449 ME = &HexagonMCInstrInfo::getExpr(*ME);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000450 int64_t Value;
451 if (ME->evaluateAsAbsolute(Value))
452 return Value;
453 assert(ME->getKind() == MCExpr::SymbolRef || ME->getKind() == MCExpr::Binary);
454 if (ME->getKind() == MCExpr::Binary) {
455 MCBinaryExpr const *Binary = cast<MCBinaryExpr>(ME);
456 getExprOpValue(MI, MO, Binary->getLHS(), Fixups, STI);
457 getExprOpValue(MI, MO, Binary->getRHS(), Fixups, STI);
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000458 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000459 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000460 Hexagon::Fixups FixupKind =
461 Hexagon::Fixups(Hexagon::fixup_Hexagon_TPREL_LO16);
462 const MCSymbolRefExpr *MCSRE = static_cast<const MCSymbolRefExpr *>(ME);
463 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
Colin LeMahieub6625652015-05-01 21:14:21 +0000464 unsigned bits = HexagonMCInstrInfo::getExtentBits(MCII, MI) -
465 HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
466 const MCSymbolRefExpr::VariantKind kind = MCSRE->getKind();
467
468 DEBUG(dbgs() << "----------------------------------------\n");
469 DEBUG(dbgs() << "Opcode Name: " << HexagonMCInstrInfo::getName(MCII, MI)
470 << "\n");
Colin LeMahieu6efd2732015-05-01 21:30:22 +0000471 DEBUG(dbgs() << "Opcode: " << MCID.getOpcode() << "\n");
Colin LeMahieub6625652015-05-01 21:14:21 +0000472 DEBUG(dbgs() << "Relocation bits: " << bits << "\n");
473 DEBUG(dbgs() << "Addend: " << *Addend << "\n");
474 DEBUG(dbgs() << "----------------------------------------\n");
475
476 switch (bits) {
477 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000478 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000479 case 32:
480 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000481 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000482 FixupKind = *Extended ? Hexagon::fixup_Hexagon_DTPREL_32_6_X
483 : Hexagon::fixup_Hexagon_DTPREL_32;
484 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000485 case MCSymbolRefExpr::VK_GOT:
486 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOT_32_6_X
487 : Hexagon::fixup_Hexagon_GOT_32;
488 break;
489 case MCSymbolRefExpr::VK_GOTREL:
490 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOTREL_32_6_X
491 : Hexagon::fixup_Hexagon_GOTREL_32;
492 break;
493 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
494 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_GOT_32_6_X
495 : Hexagon::fixup_Hexagon_GD_GOT_32;
496 break;
497 case MCSymbolRefExpr::VK_Hexagon_IE:
498 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_32_6_X
499 : Hexagon::fixup_Hexagon_IE_32;
500 break;
501 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
502 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_GOT_32_6_X
503 : Hexagon::fixup_Hexagon_IE_GOT_32;
504 break;
505 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
506 FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_GOT_32_6_X
507 : Hexagon::fixup_Hexagon_LD_GOT_32;
508 break;
509 case MCSymbolRefExpr::VK_Hexagon_PCREL:
510 FixupKind = Hexagon::fixup_Hexagon_32_PCREL;
511 break;
512 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000513 FixupKind =
514 *Extended ? Hexagon::fixup_Hexagon_32_6_X : Hexagon::fixup_Hexagon_32;
515 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000516 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000517 FixupKind = *Extended ? Hexagon::fixup_Hexagon_TPREL_32_6_X
518 : Hexagon::fixup_Hexagon_TPREL_32;
519 break;
520 default:
521 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000522 }
523 break;
524
525 case 22:
526 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000527 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000528 FixupKind = Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL;
529 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000530 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000531 FixupKind = Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL;
532 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000533 case MCSymbolRefExpr::VK_None:
534 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B22_PCREL_X
535 : Hexagon::fixup_Hexagon_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000536 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000537 case MCSymbolRefExpr::VK_PLT:
538 FixupKind = Hexagon::fixup_Hexagon_PLT_B22_PCREL;
539 break;
540 default:
541 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000542 }
543 break;
544
545 case 16:
546 if (*Extended) {
547 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000548 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000549 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
550 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000551 case MCSymbolRefExpr::VK_GOT:
552 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
553 break;
554 case MCSymbolRefExpr::VK_GOTREL:
555 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
556 break;
557 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
558 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16_X;
559 break;
560 case MCSymbolRefExpr::VK_Hexagon_IE:
561 FixupKind = Hexagon::fixup_Hexagon_IE_16_X;
562 break;
563 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
564 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16_X;
565 break;
566 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
567 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16_X;
568 break;
569 case MCSymbolRefExpr::VK_None:
570 FixupKind = Hexagon::fixup_Hexagon_16_X;
571 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000572 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000573 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
574 break;
575 default:
576 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000577 }
578 } else
579 switch (kind) {
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000580 case MCSymbolRefExpr::VK_None: {
581 if (HexagonMCInstrInfo::s23_2_reloc(*MO.getExpr()))
582 FixupKind = Hexagon::fixup_Hexagon_23_REG;
583 else
584 raise_relocation_error(bits, kind);
585 break;
586 }
Colin LeMahieu0e051922016-02-10 18:32:01 +0000587 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000588 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16;
Colin LeMahieub6625652015-05-01 21:14:21 +0000589 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000590 case MCSymbolRefExpr::VK_GOTREL:
591 if (MCID.getOpcode() == Hexagon::HI)
Colin LeMahieub6625652015-05-01 21:14:21 +0000592 FixupKind = Hexagon::fixup_Hexagon_GOTREL_HI16;
593 else
594 FixupKind = Hexagon::fixup_Hexagon_GOTREL_LO16;
595 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000596 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000597 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16;
598 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000599 case MCSymbolRefExpr::VK_Hexagon_GPREL:
600 FixupKind = Hexagon::fixup_Hexagon_GPREL16_0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000601 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000602 case MCSymbolRefExpr::VK_Hexagon_HI16:
603 FixupKind = Hexagon::fixup_Hexagon_HI16;
604 break;
605 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000606 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16;
607 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000608 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
609 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16;
610 break;
611 case MCSymbolRefExpr::VK_Hexagon_LO16:
612 FixupKind = Hexagon::fixup_Hexagon_LO16;
613 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000614 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000615 FixupKind = Hexagon::fixup_Hexagon_TPREL_16;
616 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000617 default:
618 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000619 }
620 break;
621
622 case 15:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000623 switch (kind) {
624 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000625 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B15_PCREL_X
626 : Hexagon::fixup_Hexagon_B15_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000627 break;
628 default:
629 raise_relocation_error(bits, kind);
630 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000631 break;
632
633 case 13:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000634 switch (kind) {
635 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000636 FixupKind = Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000637 break;
638 default:
639 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000640 }
641 break;
642
643 case 12:
644 if (*Extended)
645 switch (kind) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000646 // There isn't a GOT_12_X, both 11_X and 16_X resolve to 6/26
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000647 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000648 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
649 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000650 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000651 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
652 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000653 case MCSymbolRefExpr::VK_None:
654 FixupKind = Hexagon::fixup_Hexagon_12_X;
655 break;
656 default:
657 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000658 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000659 else
660 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000661 break;
662
663 case 11:
664 if (*Extended)
665 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000666 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000667 FixupKind = Hexagon::fixup_Hexagon_DTPREL_11_X;
668 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000669 case MCSymbolRefExpr::VK_GOT:
670 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
671 break;
672 case MCSymbolRefExpr::VK_GOTREL:
673 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
674 break;
675 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
676 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_11_X;
677 break;
678 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
679 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_11_X;
680 break;
681 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
682 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_11_X;
683 break;
684 case MCSymbolRefExpr::VK_None:
685 FixupKind = Hexagon::fixup_Hexagon_11_X;
686 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000687 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000688 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
689 break;
690 default:
691 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000692 }
693 else {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000694 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000695 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000696 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
697 break;
698 default:
699 raise_relocation_error(bits, kind);
700 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000701 }
702 break;
703
704 case 10:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000705 if (*Extended) {
706 switch (kind) {
707 case MCSymbolRefExpr::VK_None:
708 FixupKind = Hexagon::fixup_Hexagon_10_X;
709 break;
710 default:
711 raise_relocation_error(bits, kind);
712 }
713 } else
714 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000715 break;
716
717 case 9:
718 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000719 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000720 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B9_PCREL_X
721 : Hexagon::fixup_Hexagon_B9_PCREL;
722 else if (*Extended)
723 FixupKind = Hexagon::fixup_Hexagon_9_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000724 else
725 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000726 break;
727
728 case 8:
729 if (*Extended)
730 FixupKind = Hexagon::fixup_Hexagon_8_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000731 else
732 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000733 break;
734
735 case 7:
736 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000737 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000738 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B7_PCREL_X
739 : Hexagon::fixup_Hexagon_B7_PCREL;
740 else if (*Extended)
741 FixupKind = Hexagon::fixup_Hexagon_7_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000742 else
743 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000744 break;
745
746 case 6:
747 if (*Extended) {
748 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000749 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000750 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
Colin LeMahieub6625652015-05-01 21:14:21 +0000751 break;
752 // This is part of an extender, GOT_11 is a
753 // Word32_U6 unsigned/truncated reloc.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000754 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000755 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
756 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000757 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000758 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
759 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000760 case MCSymbolRefExpr::VK_Hexagon_PCREL:
761 FixupKind = Hexagon::fixup_Hexagon_6_PCREL_X;
762 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000763 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000764 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
765 break;
766 case MCSymbolRefExpr::VK_None:
767 FixupKind = Hexagon::fixup_Hexagon_6_X;
768 break;
769 default:
770 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000771 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000772 } else
773 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000774 break;
775
776 case 0:
777 FixupKind = getFixupNoBits(MCII, MI, MO, kind);
778 break;
779 }
780
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000781 MCExpr const *FixupExpression =
782 (*Addend > 0 && isPCRel(FixupKind))
783 ? MCBinaryExpr::createAdd(MO.getExpr(),
784 MCConstantExpr::create(*Addend, MCT), MCT)
785 : MO.getExpr();
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000786
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000787 MCFixup fixup = MCFixup::create(*Addend, FixupExpression,
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000788 MCFixupKind(FixupKind), MI.getLoc());
Colin LeMahieub6625652015-05-01 21:14:21 +0000789 Fixups.push_back(fixup);
790 // All of the information is in the fixup.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000791 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000792}
793
Sid Manning7da3f9a2014-10-03 13:18:11 +0000794unsigned
795HexagonMCCodeEmitter::getMachineOpValue(MCInst const &MI, MCOperand const &MO,
796 SmallVectorImpl<MCFixup> &Fixups,
797 MCSubtargetInfo const &STI) const {
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000798 assert(!MO.isImm());
799 if (MO.isReg()) {
800 unsigned Reg = MO.getReg();
801 if (HexagonMCInstrInfo::isSubInstruction(MI))
802 return HexagonMCInstrInfo::getDuplexRegisterNumbering(Reg);
803 switch(MI.getOpcode()){
804 case Hexagon::A2_tfrrcr:
805 case Hexagon::A2_tfrcrr:
806 if(Reg == Hexagon::M0)
807 Reg = Hexagon::C6;
808 if(Reg == Hexagon::M1)
809 Reg = Hexagon::C7;
810 }
811 return MCT.getRegisterInfo()->getEncodingValue(Reg);
812 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000813
Colin LeMahieub6625652015-05-01 21:14:21 +0000814 return getExprOpValue(MI, MO, MO.getExpr(), Fixups, STI);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000815}
816
Sid Manning7da3f9a2014-10-03 13:18:11 +0000817MCCodeEmitter *llvm::createHexagonMCCodeEmitter(MCInstrInfo const &MII,
818 MCRegisterInfo const &MRI,
Sid Manning7da3f9a2014-10-03 13:18:11 +0000819 MCContext &MCT) {
Eric Christopher0169e422015-03-10 22:03:14 +0000820 return new HexagonMCCodeEmitter(MII, MCT);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000821}
822
Daniel Sanders72db2a32016-11-19 13:05:44 +0000823#define ENABLE_INSTR_PREDICATE_VERIFIER
Sid Manning7da3f9a2014-10-03 13:18:11 +0000824#include "HexagonGenMCCodeEmitter.inc"