blob: 39b828d8a03aef2f65e63478af4bd314b4ce59ba [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());
81 EncodeSingleInstruction(HMI, OS, Fixups, STI,
82 parseBits(Instruction, Last, HMB, HMI),
83 Instruction);
84 *Extended = HexagonMCInstrInfo::isImmext(HMI);
85 *Addend += HEXAGON_INSTR_SIZE;
86 ++Instruction;
87 }
88 return;
89}
90
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +000091static bool RegisterMatches(unsigned Consumer, unsigned Producer,
92 unsigned Producer2) {
93 if (Consumer == Producer)
94 return true;
95 if (Consumer == Producer2)
96 return true;
97 // Calculate if we're a single vector consumer referencing a double producer
98 if (Producer >= Hexagon::W0 && Producer <= Hexagon::W15)
99 if (Consumer >= Hexagon::V0 && Consumer <= Hexagon::V31)
100 return ((Consumer - Hexagon::V0) >> 1) == (Producer - Hexagon::W0);
101 return false;
102}
103
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000104/// EncodeSingleInstruction - Emit a single
105void HexagonMCCodeEmitter::EncodeSingleInstruction(
106 const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
107 const MCSubtargetInfo &STI, uint32_t Parse, size_t Index) const {
108 MCInst HMB = MI;
109 assert(!HexagonMCInstrInfo::isBundle(HMB));
110 uint64_t Binary;
111
Colin LeMahieu13cc3ab2015-11-10 00:51:56 +0000112 // Compound instructions are limited to using registers 0-7 and 16-23
113 // and here we make a map 16-23 to 8-15 so they can be correctly encoded.
114 static unsigned RegMap[8] = {Hexagon::R8, Hexagon::R9, Hexagon::R10,
115 Hexagon::R11, Hexagon::R12, Hexagon::R13,
116 Hexagon::R14, Hexagon::R15};
117
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000118 // Pseudo instructions don't get encoded and shouldn't be here
119 // in the first place!
120 assert(!HexagonMCInstrInfo::getDesc(MCII, HMB).isPseudo() &&
121 "pseudo-instruction found");
122 DEBUG(dbgs() << "Encoding insn"
123 " `" << HexagonMCInstrInfo::getName(MCII, HMB) << "'"
124 "\n");
125
Colin LeMahieu13cc3ab2015-11-10 00:51:56 +0000126 if (llvm::HexagonMCInstrInfo::getType(MCII, HMB) == HexagonII::TypeCOMPOUND) {
127 for (unsigned i = 0; i < HMB.getNumOperands(); ++i)
128 if (HMB.getOperand(i).isReg()) {
129 unsigned Reg =
130 MCT.getRegisterInfo()->getEncodingValue(HMB.getOperand(i).getReg());
131 if ((Reg <= 23) && (Reg >= 16))
132 HMB.getOperand(i).setReg(RegMap[Reg - 16]);
133 }
134 }
135
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000136 if (HexagonMCInstrInfo::isNewValue(MCII, HMB)) {
137 // Calculate the new value distance to the associated producer
138 MCOperand &MCO =
139 HMB.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, HMB));
140 unsigned SOffset = 0;
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000141 unsigned VOffset = 0;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000142 unsigned Register = MCO.getReg();
143 unsigned Register1;
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000144 unsigned Register2;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000145 auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
146 auto i = Instructions.begin() + Index - 1;
147 for (;; --i) {
148 assert(i != Instructions.begin() - 1 && "Couldn't find producer");
149 MCInst const &Inst = *i->getInst();
150 if (HexagonMCInstrInfo::isImmext(Inst))
151 continue;
152 ++SOffset;
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000153 if (HexagonMCInstrInfo::isVector(MCII, Inst))
154 // Vector instructions don't count scalars
155 ++VOffset;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000156 Register1 =
157 HexagonMCInstrInfo::hasNewValue(MCII, Inst)
158 ? HexagonMCInstrInfo::getNewValueOperand(MCII, Inst).getReg()
159 : static_cast<unsigned>(Hexagon::NoRegister);
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000160 Register2 =
161 HexagonMCInstrInfo::hasNewValue2(MCII, Inst)
162 ? HexagonMCInstrInfo::getNewValueOperand2(MCII, Inst).getReg()
163 : static_cast<unsigned>(Hexagon::NoRegister);
164 if (!RegisterMatches(Register, Register1, Register2))
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000165 // This isn't the register we're looking for
166 continue;
167 if (!HexagonMCInstrInfo::isPredicated(MCII, Inst))
168 // Producer is unpredicated
169 break;
170 assert(HexagonMCInstrInfo::isPredicated(MCII, HMB) &&
171 "Unpredicated consumer depending on predicated producer");
172 if (HexagonMCInstrInfo::isPredicatedTrue(MCII, Inst) ==
173 HexagonMCInstrInfo::isPredicatedTrue(MCII, HMB))
174 // Producer predicate sense matched ours
175 break;
176 }
177 // Hexagon PRM 10.11 Construct Nt from distance
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000178 unsigned Offset =
179 HexagonMCInstrInfo::isVector(MCII, HMB) ? VOffset : SOffset;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000180 Offset <<= 1;
Krzysztof Parzyszeke737b862016-04-28 15:54:48 +0000181 Offset |=
182 HexagonMCInstrInfo::SubregisterBit(Register, Register1, Register2);
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000183 MCO.setReg(Offset + Hexagon::R0);
184 }
185
186 Binary = getBinaryCodeForInstr(HMB, Fixups, STI);
187 // Check for unimplemented instructions. Immediate extenders
188 // are encoded as zero, so they need to be accounted for.
189 if ((!Binary) &&
190 ((HMB.getOpcode() != DuplexIClass0) && (HMB.getOpcode() != A4_ext) &&
191 (HMB.getOpcode() != A4_ext_b) && (HMB.getOpcode() != A4_ext_c) &&
192 (HMB.getOpcode() != A4_ext_g))) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000193 DEBUG(dbgs() << "Unimplemented inst: "
194 " `" << HexagonMCInstrInfo::getName(MCII, HMB) << "'"
195 "\n");
196 llvm_unreachable("Unimplemented Instruction");
197 }
198 Binary |= Parse;
Colin LeMahieube8c4532015-06-05 16:00:11 +0000199
200 // if we need to emit a duplexed instruction
201 if (HMB.getOpcode() >= Hexagon::DuplexIClass0 &&
202 HMB.getOpcode() <= Hexagon::DuplexIClassF) {
203 assert(Parse == HexagonII::INST_PARSE_DUPLEX &&
204 "Emitting duplex without duplex parse bits");
205 unsigned dupIClass;
206 switch (HMB.getOpcode()) {
207 case Hexagon::DuplexIClass0:
208 dupIClass = 0;
209 break;
210 case Hexagon::DuplexIClass1:
211 dupIClass = 1;
212 break;
213 case Hexagon::DuplexIClass2:
214 dupIClass = 2;
215 break;
216 case Hexagon::DuplexIClass3:
217 dupIClass = 3;
218 break;
219 case Hexagon::DuplexIClass4:
220 dupIClass = 4;
221 break;
222 case Hexagon::DuplexIClass5:
223 dupIClass = 5;
224 break;
225 case Hexagon::DuplexIClass6:
226 dupIClass = 6;
227 break;
228 case Hexagon::DuplexIClass7:
229 dupIClass = 7;
230 break;
231 case Hexagon::DuplexIClass8:
232 dupIClass = 8;
233 break;
234 case Hexagon::DuplexIClass9:
235 dupIClass = 9;
236 break;
237 case Hexagon::DuplexIClassA:
238 dupIClass = 10;
239 break;
240 case Hexagon::DuplexIClassB:
241 dupIClass = 11;
242 break;
243 case Hexagon::DuplexIClassC:
244 dupIClass = 12;
245 break;
246 case Hexagon::DuplexIClassD:
247 dupIClass = 13;
248 break;
249 case Hexagon::DuplexIClassE:
250 dupIClass = 14;
251 break;
252 case Hexagon::DuplexIClassF:
253 dupIClass = 15;
254 break;
255 default:
256 llvm_unreachable("Unimplemented DuplexIClass");
257 break;
258 }
259 // 29 is the bit position.
260 // 0b1110 =0xE bits are masked off and down shifted by 1 bit.
261 // Last bit is moved to bit position 13
262 Binary = ((dupIClass & 0xE) << (29 - 1)) | ((dupIClass & 0x1) << 13);
263
264 const MCInst *subInst0 = HMB.getOperand(0).getInst();
265 const MCInst *subInst1 = HMB.getOperand(1).getInst();
266
267 // get subinstruction slot 0
268 unsigned subInstSlot0Bits = getBinaryCodeForInstr(*subInst0, Fixups, STI);
269 // get subinstruction slot 1
270 unsigned subInstSlot1Bits = getBinaryCodeForInstr(*subInst1, Fixups, STI);
271
272 Binary |= subInstSlot0Bits | (subInstSlot1Bits << 16);
273 }
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000274 support::endian::Writer<support::little>(OS).write<uint32_t>(Binary);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000275 ++MCNumEmitted;
276}
277
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000278namespace {
279void raise_relocation_error(unsigned bits, unsigned kind) {
280 std::string Text;
281 {
282 llvm::raw_string_ostream Stream(Text);
283 Stream << "Unrecognized relocation combination bits: " << bits
284 << " kind: " << kind;
285 }
286 report_fatal_error(Text);
287}
288}
289
290/// getFixupNoBits - Some insns are not extended and thus have no
291/// bits. These cases require a more brute force method for determining
292/// the correct relocation.
293namespace {
294Hexagon::Fixups getFixupNoBits(MCInstrInfo const &MCII, const MCInst &MI,
Colin LeMahieub6625652015-05-01 21:14:21 +0000295 const MCOperand &MO,
296 const MCSymbolRefExpr::VariantKind kind) {
297 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
298 unsigned insnType = llvm::HexagonMCInstrInfo::getType(MCII, MI);
299
300 if (insnType == HexagonII::TypePREFIX) {
301 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000302 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000303 return Hexagon::fixup_Hexagon_GOTREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000304 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000305 return Hexagon::fixup_Hexagon_GOT_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000306 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000307 return Hexagon::fixup_Hexagon_TPREL_32_6_X;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000308 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000309 return Hexagon::fixup_Hexagon_DTPREL_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000310 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000311 return Hexagon::fixup_Hexagon_GD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000312 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000313 return Hexagon::fixup_Hexagon_LD_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000314 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000315 return Hexagon::fixup_Hexagon_IE_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000316 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000317 return Hexagon::fixup_Hexagon_IE_GOT_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000318 case MCSymbolRefExpr::VK_Hexagon_PCREL:
319 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000320 if (MCID.isBranch())
321 return Hexagon::fixup_Hexagon_B32_PCREL_X;
322 else
323 return Hexagon::fixup_Hexagon_32_6_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000324 default:
325 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000326 }
327 } else if (MCID.isBranch())
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000328 return Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000329
330 switch (MCID.getOpcode()) {
331 case Hexagon::HI:
332 case Hexagon::A2_tfrih:
333 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000334 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000335 return Hexagon::fixup_Hexagon_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000336 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000337 return Hexagon::fixup_Hexagon_GOTREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000338 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000339 return Hexagon::fixup_Hexagon_GD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000340 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000341 return Hexagon::fixup_Hexagon_LD_GOT_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000342 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000343 return Hexagon::fixup_Hexagon_IE_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000344 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000345 return Hexagon::fixup_Hexagon_IE_GOT_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000346 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000347 return Hexagon::fixup_Hexagon_TPREL_HI16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000348 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000349 return Hexagon::fixup_Hexagon_DTPREL_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000350 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000351 return Hexagon::fixup_Hexagon_HI16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000352 default:
353 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000354 }
355
356 case Hexagon::LO:
357 case Hexagon::A2_tfril:
358 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000359 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000360 return Hexagon::fixup_Hexagon_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000361 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000362 return Hexagon::fixup_Hexagon_GOTREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000363 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000364 return Hexagon::fixup_Hexagon_GD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000365 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000366 return Hexagon::fixup_Hexagon_LD_GOT_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000367 case MCSymbolRefExpr::VK_Hexagon_IE:
Colin LeMahieub6625652015-05-01 21:14:21 +0000368 return Hexagon::fixup_Hexagon_IE_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000369 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000370 return Hexagon::fixup_Hexagon_IE_GOT_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000371 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000372 return Hexagon::fixup_Hexagon_TPREL_LO16;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000373 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000374 return Hexagon::fixup_Hexagon_DTPREL_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000375 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000376 return Hexagon::fixup_Hexagon_LO16;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000377 default:
378 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000379 }
380
381 // The only relocs left should be GP relative:
382 default:
383 if (MCID.mayStore() || MCID.mayLoad()) {
Chad Rosierc00ab4f2016-02-18 17:49:57 +0000384 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses;
385 ++ImpUses) {
Krzysztof Parzyszekbc17b682016-01-11 15:51:53 +0000386 if (*ImpUses != Hexagon::GP)
387 continue;
388 switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
389 case HexagonII::MemAccessSize::ByteAccess:
390 return fixup_Hexagon_GPREL16_0;
391 case HexagonII::MemAccessSize::HalfWordAccess:
392 return fixup_Hexagon_GPREL16_1;
393 case HexagonII::MemAccessSize::WordAccess:
394 return fixup_Hexagon_GPREL16_2;
395 case HexagonII::MemAccessSize::DoubleWordAccess:
396 return fixup_Hexagon_GPREL16_3;
397 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000398 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000399 }
400 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000401 }
402 raise_relocation_error(0, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000403 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000404 llvm_unreachable("Relocation exit not taken");
405}
Colin LeMahieub6625652015-05-01 21:14:21 +0000406}
407
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000408namespace llvm {
409extern const MCInstrDesc HexagonInsts[];
410}
411
412namespace {
413 bool isPCRel (unsigned Kind) {
414 switch(Kind){
415 case fixup_Hexagon_B22_PCREL:
416 case fixup_Hexagon_B15_PCREL:
417 case fixup_Hexagon_B7_PCREL:
418 case fixup_Hexagon_B13_PCREL:
419 case fixup_Hexagon_B9_PCREL:
420 case fixup_Hexagon_B32_PCREL_X:
421 case fixup_Hexagon_B22_PCREL_X:
422 case fixup_Hexagon_B15_PCREL_X:
423 case fixup_Hexagon_B13_PCREL_X:
424 case fixup_Hexagon_B9_PCREL_X:
425 case fixup_Hexagon_B7_PCREL_X:
426 case fixup_Hexagon_32_PCREL:
427 case fixup_Hexagon_PLT_B22_PCREL:
428 case fixup_Hexagon_GD_PLT_B22_PCREL:
429 case fixup_Hexagon_LD_PLT_B22_PCREL:
430 case fixup_Hexagon_6_PCREL_X:
431 return true;
432 default:
433 return false;
434 }
435 }
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000436}
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000437
Colin LeMahieub6625652015-05-01 21:14:21 +0000438unsigned HexagonMCCodeEmitter::getExprOpValue(const MCInst &MI,
439 const MCOperand &MO,
440 const MCExpr *ME,
441 SmallVectorImpl<MCFixup> &Fixups,
442 const MCSubtargetInfo &STI) const
443
444{
Colin LeMahieu98c8e072016-02-15 18:42:07 +0000445 if (isa<HexagonMCExpr>(ME))
446 ME = &HexagonMCInstrInfo::getExpr(*ME);
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000447 int64_t Value;
448 if (ME->evaluateAsAbsolute(Value))
449 return Value;
450 assert(ME->getKind() == MCExpr::SymbolRef || ME->getKind() == MCExpr::Binary);
451 if (ME->getKind() == MCExpr::Binary) {
452 MCBinaryExpr const *Binary = cast<MCBinaryExpr>(ME);
453 getExprOpValue(MI, MO, Binary->getLHS(), Fixups, STI);
454 getExprOpValue(MI, MO, Binary->getRHS(), Fixups, STI);
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000455 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000456 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000457 Hexagon::Fixups FixupKind =
458 Hexagon::Fixups(Hexagon::fixup_Hexagon_TPREL_LO16);
459 const MCSymbolRefExpr *MCSRE = static_cast<const MCSymbolRefExpr *>(ME);
460 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
Colin LeMahieub6625652015-05-01 21:14:21 +0000461 unsigned bits = HexagonMCInstrInfo::getExtentBits(MCII, MI) -
462 HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
463 const MCSymbolRefExpr::VariantKind kind = MCSRE->getKind();
464
465 DEBUG(dbgs() << "----------------------------------------\n");
466 DEBUG(dbgs() << "Opcode Name: " << HexagonMCInstrInfo::getName(MCII, MI)
467 << "\n");
Colin LeMahieu6efd2732015-05-01 21:30:22 +0000468 DEBUG(dbgs() << "Opcode: " << MCID.getOpcode() << "\n");
Colin LeMahieub6625652015-05-01 21:14:21 +0000469 DEBUG(dbgs() << "Relocation bits: " << bits << "\n");
470 DEBUG(dbgs() << "Addend: " << *Addend << "\n");
471 DEBUG(dbgs() << "----------------------------------------\n");
472
473 switch (bits) {
474 default:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000475 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000476 case 32:
477 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000478 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000479 FixupKind = *Extended ? Hexagon::fixup_Hexagon_DTPREL_32_6_X
480 : Hexagon::fixup_Hexagon_DTPREL_32;
481 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000482 case MCSymbolRefExpr::VK_GOT:
483 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOT_32_6_X
484 : Hexagon::fixup_Hexagon_GOT_32;
485 break;
486 case MCSymbolRefExpr::VK_GOTREL:
487 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOTREL_32_6_X
488 : Hexagon::fixup_Hexagon_GOTREL_32;
489 break;
490 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
491 FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_GOT_32_6_X
492 : Hexagon::fixup_Hexagon_GD_GOT_32;
493 break;
494 case MCSymbolRefExpr::VK_Hexagon_IE:
495 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_32_6_X
496 : Hexagon::fixup_Hexagon_IE_32;
497 break;
498 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
499 FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_GOT_32_6_X
500 : Hexagon::fixup_Hexagon_IE_GOT_32;
501 break;
502 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
503 FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_GOT_32_6_X
504 : Hexagon::fixup_Hexagon_LD_GOT_32;
505 break;
506 case MCSymbolRefExpr::VK_Hexagon_PCREL:
507 FixupKind = Hexagon::fixup_Hexagon_32_PCREL;
508 break;
509 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000510 FixupKind =
511 *Extended ? Hexagon::fixup_Hexagon_32_6_X : Hexagon::fixup_Hexagon_32;
512 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000513 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000514 FixupKind = *Extended ? Hexagon::fixup_Hexagon_TPREL_32_6_X
515 : Hexagon::fixup_Hexagon_TPREL_32;
516 break;
517 default:
518 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000519 }
520 break;
521
522 case 22:
523 switch (kind) {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000524 case MCSymbolRefExpr::VK_Hexagon_GD_PLT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000525 FixupKind = Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL;
526 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000527 case MCSymbolRefExpr::VK_Hexagon_LD_PLT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000528 FixupKind = Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL;
529 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000530 case MCSymbolRefExpr::VK_None:
531 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B22_PCREL_X
532 : Hexagon::fixup_Hexagon_B22_PCREL;
Colin LeMahieub6625652015-05-01 21:14:21 +0000533 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000534 case MCSymbolRefExpr::VK_PLT:
535 FixupKind = Hexagon::fixup_Hexagon_PLT_B22_PCREL;
536 break;
537 default:
538 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000539 }
540 break;
541
542 case 16:
543 if (*Extended) {
544 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000545 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000546 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
547 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000548 case MCSymbolRefExpr::VK_GOT:
549 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
550 break;
551 case MCSymbolRefExpr::VK_GOTREL:
552 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
553 break;
554 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
555 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16_X;
556 break;
557 case MCSymbolRefExpr::VK_Hexagon_IE:
558 FixupKind = Hexagon::fixup_Hexagon_IE_16_X;
559 break;
560 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
561 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16_X;
562 break;
563 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
564 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16_X;
565 break;
566 case MCSymbolRefExpr::VK_None:
567 FixupKind = Hexagon::fixup_Hexagon_16_X;
568 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000569 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000570 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
571 break;
572 default:
573 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000574 }
575 } else
576 switch (kind) {
Colin LeMahieuecef1d92016-02-16 20:38:17 +0000577 case MCSymbolRefExpr::VK_None: {
578 if (HexagonMCInstrInfo::s23_2_reloc(*MO.getExpr()))
579 FixupKind = Hexagon::fixup_Hexagon_23_REG;
580 else
581 raise_relocation_error(bits, kind);
582 break;
583 }
Colin LeMahieu0e051922016-02-10 18:32:01 +0000584 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000585 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16;
Colin LeMahieub6625652015-05-01 21:14:21 +0000586 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000587 case MCSymbolRefExpr::VK_GOTREL:
588 if (MCID.getOpcode() == Hexagon::HI)
Colin LeMahieub6625652015-05-01 21:14:21 +0000589 FixupKind = Hexagon::fixup_Hexagon_GOTREL_HI16;
590 else
591 FixupKind = Hexagon::fixup_Hexagon_GOTREL_LO16;
592 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000593 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000594 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16;
595 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000596 case MCSymbolRefExpr::VK_Hexagon_GPREL:
597 FixupKind = Hexagon::fixup_Hexagon_GPREL16_0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000598 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000599 case MCSymbolRefExpr::VK_Hexagon_HI16:
600 FixupKind = Hexagon::fixup_Hexagon_HI16;
601 break;
602 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000603 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16;
604 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000605 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
606 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16;
607 break;
608 case MCSymbolRefExpr::VK_Hexagon_LO16:
609 FixupKind = Hexagon::fixup_Hexagon_LO16;
610 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000611 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000612 FixupKind = Hexagon::fixup_Hexagon_TPREL_16;
613 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000614 default:
615 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000616 }
617 break;
618
619 case 15:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000620 switch (kind) {
621 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000622 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B15_PCREL_X
623 : Hexagon::fixup_Hexagon_B15_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000624 break;
625 default:
626 raise_relocation_error(bits, kind);
627 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000628 break;
629
630 case 13:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000631 switch (kind) {
632 case MCSymbolRefExpr::VK_None:
Colin LeMahieub6625652015-05-01 21:14:21 +0000633 FixupKind = Hexagon::fixup_Hexagon_B13_PCREL;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000634 break;
635 default:
636 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000637 }
638 break;
639
640 case 12:
641 if (*Extended)
642 switch (kind) {
Colin LeMahieub6625652015-05-01 21:14:21 +0000643 // There isn't a GOT_12_X, both 11_X and 16_X resolve to 6/26
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000644 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000645 FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
646 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000647 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000648 FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
649 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000650 case MCSymbolRefExpr::VK_None:
651 FixupKind = Hexagon::fixup_Hexagon_12_X;
652 break;
653 default:
654 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000655 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000656 else
657 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000658 break;
659
660 case 11:
661 if (*Extended)
662 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000663 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000664 FixupKind = Hexagon::fixup_Hexagon_DTPREL_11_X;
665 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000666 case MCSymbolRefExpr::VK_GOT:
667 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
668 break;
669 case MCSymbolRefExpr::VK_GOTREL:
670 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
671 break;
672 case MCSymbolRefExpr::VK_Hexagon_GD_GOT:
673 FixupKind = Hexagon::fixup_Hexagon_GD_GOT_11_X;
674 break;
675 case MCSymbolRefExpr::VK_Hexagon_IE_GOT:
676 FixupKind = Hexagon::fixup_Hexagon_IE_GOT_11_X;
677 break;
678 case MCSymbolRefExpr::VK_Hexagon_LD_GOT:
679 FixupKind = Hexagon::fixup_Hexagon_LD_GOT_11_X;
680 break;
681 case MCSymbolRefExpr::VK_None:
682 FixupKind = Hexagon::fixup_Hexagon_11_X;
683 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000684 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000685 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
686 break;
687 default:
688 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000689 }
690 else {
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000691 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000692 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000693 FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
694 break;
695 default:
696 raise_relocation_error(bits, kind);
697 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000698 }
699 break;
700
701 case 10:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000702 if (*Extended) {
703 switch (kind) {
704 case MCSymbolRefExpr::VK_None:
705 FixupKind = Hexagon::fixup_Hexagon_10_X;
706 break;
707 default:
708 raise_relocation_error(bits, kind);
709 }
710 } else
711 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000712 break;
713
714 case 9:
715 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000716 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000717 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B9_PCREL_X
718 : Hexagon::fixup_Hexagon_B9_PCREL;
719 else if (*Extended)
720 FixupKind = Hexagon::fixup_Hexagon_9_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000721 else
722 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000723 break;
724
725 case 8:
726 if (*Extended)
727 FixupKind = Hexagon::fixup_Hexagon_8_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000728 else
729 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000730 break;
731
732 case 7:
733 if (MCID.isBranch() ||
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000734 (HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
Colin LeMahieub6625652015-05-01 21:14:21 +0000735 FixupKind = *Extended ? Hexagon::fixup_Hexagon_B7_PCREL_X
736 : Hexagon::fixup_Hexagon_B7_PCREL;
737 else if (*Extended)
738 FixupKind = Hexagon::fixup_Hexagon_7_X;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000739 else
740 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000741 break;
742
743 case 6:
744 if (*Extended) {
745 switch (kind) {
Colin LeMahieu0e051922016-02-10 18:32:01 +0000746 case MCSymbolRefExpr::VK_DTPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000747 FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
Colin LeMahieub6625652015-05-01 21:14:21 +0000748 break;
749 // This is part of an extender, GOT_11 is a
750 // Word32_U6 unsigned/truncated reloc.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000751 case MCSymbolRefExpr::VK_GOT:
Colin LeMahieub6625652015-05-01 21:14:21 +0000752 FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
753 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000754 case MCSymbolRefExpr::VK_GOTREL:
Colin LeMahieub6625652015-05-01 21:14:21 +0000755 FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
756 break;
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000757 case MCSymbolRefExpr::VK_Hexagon_PCREL:
758 FixupKind = Hexagon::fixup_Hexagon_6_PCREL_X;
759 break;
Colin LeMahieu0e051922016-02-10 18:32:01 +0000760 case MCSymbolRefExpr::VK_TPREL:
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000761 FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
762 break;
763 case MCSymbolRefExpr::VK_None:
764 FixupKind = Hexagon::fixup_Hexagon_6_X;
765 break;
766 default:
767 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000768 }
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000769 } else
770 raise_relocation_error(bits, kind);
Colin LeMahieub6625652015-05-01 21:14:21 +0000771 break;
772
773 case 0:
774 FixupKind = getFixupNoBits(MCII, MI, MO, kind);
775 break;
776 }
777
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000778 MCExpr const *FixupExpression =
779 (*Addend > 0 && isPCRel(FixupKind))
780 ? MCBinaryExpr::createAdd(MO.getExpr(),
781 MCConstantExpr::create(*Addend, MCT), MCT)
782 : MO.getExpr();
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000783
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000784 MCFixup fixup = MCFixup::create(*Addend, FixupExpression,
Colin LeMahieua071a8e2015-06-15 21:52:13 +0000785 MCFixupKind(FixupKind), MI.getLoc());
Colin LeMahieub6625652015-05-01 21:14:21 +0000786 Fixups.push_back(fixup);
787 // All of the information is in the fixup.
Colin LeMahieu1c79d9b2016-02-09 19:18:02 +0000788 return 0;
Colin LeMahieub6625652015-05-01 21:14:21 +0000789}
790
Sid Manning7da3f9a2014-10-03 13:18:11 +0000791unsigned
792HexagonMCCodeEmitter::getMachineOpValue(MCInst const &MI, MCOperand const &MO,
793 SmallVectorImpl<MCFixup> &Fixups,
794 MCSubtargetInfo const &STI) const {
Krzysztof Parzyszekc6f1e1a2016-03-21 20:13:33 +0000795 assert(!MO.isImm());
796 if (MO.isReg()) {
797 unsigned Reg = MO.getReg();
798 if (HexagonMCInstrInfo::isSubInstruction(MI))
799 return HexagonMCInstrInfo::getDuplexRegisterNumbering(Reg);
800 switch(MI.getOpcode()){
801 case Hexagon::A2_tfrrcr:
802 case Hexagon::A2_tfrcrr:
803 if(Reg == Hexagon::M0)
804 Reg = Hexagon::C6;
805 if(Reg == Hexagon::M1)
806 Reg = Hexagon::C7;
807 }
808 return MCT.getRegisterInfo()->getEncodingValue(Reg);
809 }
Colin LeMahieub6625652015-05-01 21:14:21 +0000810
Colin LeMahieub6625652015-05-01 21:14:21 +0000811 return getExprOpValue(MI, MO, MO.getExpr(), Fixups, STI);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000812}
813
Sid Manning7da3f9a2014-10-03 13:18:11 +0000814MCCodeEmitter *llvm::createHexagonMCCodeEmitter(MCInstrInfo const &MII,
815 MCRegisterInfo const &MRI,
Sid Manning7da3f9a2014-10-03 13:18:11 +0000816 MCContext &MCT) {
Eric Christopher0169e422015-03-10 22:03:14 +0000817 return new HexagonMCCodeEmitter(MII, MCT);
Sid Manning7da3f9a2014-10-03 13:18:11 +0000818}
819
820#include "HexagonGenMCCodeEmitter.inc"