[Hexagon] PC-relative offsets are relative to packet start rather than the offset...
[oota-llvm.git] / lib / Target / Hexagon / MCTargetDesc / HexagonMCCodeEmitter.cpp
1 //===-- 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"
12 #include "MCTargetDesc/HexagonFixupKinds.h"
13 #include "MCTargetDesc/HexagonMCCodeEmitter.h"
14 #include "MCTargetDesc/HexagonMCInstrInfo.h"
15 #include "MCTargetDesc/HexagonMCTargetDesc.h"
16 #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"
25 #include "llvm/Support/EndianStream.h"
26 #include "llvm/Support/raw_ostream.h"
27
28 #define DEBUG_TYPE "mccodeemitter"
29
30 using namespace llvm;
31 using namespace Hexagon;
32
33 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
34
35 HexagonMCCodeEmitter::HexagonMCCodeEmitter(MCInstrInfo const &aMII,
36                                            MCContext &aMCT)
37     : MCT(aMCT), MCII(aMII), Addend(new unsigned(0)),
38       Extended(new bool(false)), CurrentBundle(new MCInst const *) {}
39
40 uint32_t HexagonMCCodeEmitter::parseBits(size_t Instruction, size_t Last,
41                                          MCInst const &MCB,
42                                          MCInst const &MCI) const {
43   bool Duplex = HexagonMCInstrInfo::isDuplex(MCII, MCI);
44   if (Instruction == 0) {
45     if (HexagonMCInstrInfo::isInnerLoop(MCB)) {
46       assert(!Duplex);
47       assert(Instruction != Last);
48       return HexagonII::INST_PARSE_LOOP_END;
49     }
50   }
51   if (Instruction == 1) {
52     if (HexagonMCInstrInfo::isOuterLoop(MCB)) {
53       assert(!Duplex);
54       assert(Instruction != Last);
55       return HexagonII::INST_PARSE_LOOP_END;
56     }
57   }
58   if (Duplex) {
59     assert(Instruction == Last);
60     return HexagonII::INST_PARSE_DUPLEX;
61   }
62   if(Instruction == Last)
63     return HexagonII::INST_PARSE_PACKET_END;
64   return HexagonII::INST_PARSE_NOT_END;
65 }
66
67 void HexagonMCCodeEmitter::encodeInstruction(MCInst const &MI, raw_ostream &OS,
68                                              SmallVectorImpl<MCFixup> &Fixups,
69                                              MCSubtargetInfo const &STI) const {
70   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
91 /// EncodeSingleInstruction - Emit a single
92 void HexagonMCCodeEmitter::EncodeSingleInstruction(
93     const MCInst &MI, raw_ostream &OS, SmallVectorImpl<MCFixup> &Fixups,
94     const MCSubtargetInfo &STI, uint32_t Parse, size_t Index) const {
95   MCInst HMB = MI;
96   assert(!HexagonMCInstrInfo::isBundle(HMB));
97   uint64_t Binary;
98
99   // Pseudo instructions don't get encoded and shouldn't be here
100   // in the first place!
101   assert(!HexagonMCInstrInfo::getDesc(MCII, HMB).isPseudo() &&
102          "pseudo-instruction found");
103   DEBUG(dbgs() << "Encoding insn"
104                   " `" << HexagonMCInstrInfo::getName(MCII, HMB) << "'"
105                                                                     "\n");
106
107   if (HexagonMCInstrInfo::isNewValue(MCII, HMB)) {
108     // Calculate the new value distance to the associated producer
109     MCOperand &MCO =
110         HMB.getOperand(HexagonMCInstrInfo::getNewValueOp(MCII, HMB));
111     unsigned SOffset = 0;
112     unsigned Register = MCO.getReg();
113     unsigned Register1;
114     auto Instructions = HexagonMCInstrInfo::bundleInstructions(**CurrentBundle);
115     auto i = Instructions.begin() + Index - 1;
116     for (;; --i) {
117       assert(i != Instructions.begin() - 1 && "Couldn't find producer");
118       MCInst const &Inst = *i->getInst();
119       if (HexagonMCInstrInfo::isImmext(Inst))
120         continue;
121       ++SOffset;
122       Register1 =
123           HexagonMCInstrInfo::hasNewValue(MCII, Inst)
124               ? HexagonMCInstrInfo::getNewValueOperand(MCII, Inst).getReg()
125               : static_cast<unsigned>(Hexagon::NoRegister);
126       if (Register != Register1)
127         // This isn't the register we're looking for
128         continue;
129       if (!HexagonMCInstrInfo::isPredicated(MCII, Inst))
130         // Producer is unpredicated
131         break;
132       assert(HexagonMCInstrInfo::isPredicated(MCII, HMB) &&
133              "Unpredicated consumer depending on predicated producer");
134       if (HexagonMCInstrInfo::isPredicatedTrue(MCII, Inst) ==
135           HexagonMCInstrInfo::isPredicatedTrue(MCII, HMB))
136         // Producer predicate sense matched ours
137         break;
138     }
139     // Hexagon PRM 10.11 Construct Nt from distance
140     unsigned Offset = SOffset;
141     Offset <<= 1;
142     MCO.setReg(Offset + Hexagon::R0);
143   }
144
145   Binary = getBinaryCodeForInstr(HMB, Fixups, STI);
146   // Check for unimplemented instructions. Immediate extenders
147   // are encoded as zero, so they need to be accounted for.
148   if ((!Binary) &&
149       ((HMB.getOpcode() != DuplexIClass0) && (HMB.getOpcode() != A4_ext) &&
150        (HMB.getOpcode() != A4_ext_b) && (HMB.getOpcode() != A4_ext_c) &&
151        (HMB.getOpcode() != A4_ext_g))) {
152     // Use a A2_nop for unimplemented instructions.
153     DEBUG(dbgs() << "Unimplemented inst: "
154                     " `" << HexagonMCInstrInfo::getName(MCII, HMB) << "'"
155                                                                       "\n");
156     llvm_unreachable("Unimplemented Instruction");
157   }
158   Binary |= Parse;
159
160   // if we need to emit a duplexed instruction
161   if (HMB.getOpcode() >= Hexagon::DuplexIClass0 &&
162       HMB.getOpcode() <= Hexagon::DuplexIClassF) {
163     assert(Parse == HexagonII::INST_PARSE_DUPLEX &&
164            "Emitting duplex without duplex parse bits");
165     unsigned dupIClass;
166     switch (HMB.getOpcode()) {
167     case Hexagon::DuplexIClass0:
168       dupIClass = 0;
169       break;
170     case Hexagon::DuplexIClass1:
171       dupIClass = 1;
172       break;
173     case Hexagon::DuplexIClass2:
174       dupIClass = 2;
175       break;
176     case Hexagon::DuplexIClass3:
177       dupIClass = 3;
178       break;
179     case Hexagon::DuplexIClass4:
180       dupIClass = 4;
181       break;
182     case Hexagon::DuplexIClass5:
183       dupIClass = 5;
184       break;
185     case Hexagon::DuplexIClass6:
186       dupIClass = 6;
187       break;
188     case Hexagon::DuplexIClass7:
189       dupIClass = 7;
190       break;
191     case Hexagon::DuplexIClass8:
192       dupIClass = 8;
193       break;
194     case Hexagon::DuplexIClass9:
195       dupIClass = 9;
196       break;
197     case Hexagon::DuplexIClassA:
198       dupIClass = 10;
199       break;
200     case Hexagon::DuplexIClassB:
201       dupIClass = 11;
202       break;
203     case Hexagon::DuplexIClassC:
204       dupIClass = 12;
205       break;
206     case Hexagon::DuplexIClassD:
207       dupIClass = 13;
208       break;
209     case Hexagon::DuplexIClassE:
210       dupIClass = 14;
211       break;
212     case Hexagon::DuplexIClassF:
213       dupIClass = 15;
214       break;
215     default:
216       llvm_unreachable("Unimplemented DuplexIClass");
217       break;
218     }
219     // 29 is the bit position.
220     // 0b1110 =0xE bits are masked off and down shifted by 1 bit.
221     // Last bit is moved to bit position 13
222     Binary = ((dupIClass & 0xE) << (29 - 1)) | ((dupIClass & 0x1) << 13);
223
224     const MCInst *subInst0 = HMB.getOperand(0).getInst();
225     const MCInst *subInst1 = HMB.getOperand(1).getInst();
226
227     // get subinstruction slot 0
228     unsigned subInstSlot0Bits = getBinaryCodeForInstr(*subInst0, Fixups, STI);
229     // get subinstruction slot 1
230     unsigned subInstSlot1Bits = getBinaryCodeForInstr(*subInst1, Fixups, STI);
231
232     Binary |= subInstSlot0Bits | (subInstSlot1Bits << 16);
233   }
234   support::endian::Writer<support::little>(OS).write<uint32_t>(Binary);
235   ++MCNumEmitted;
236 }
237
238 static Hexagon::Fixups getFixupNoBits(MCInstrInfo const &MCII, const MCInst &MI,
239                                       const MCOperand &MO,
240                                       const MCSymbolRefExpr::VariantKind kind) {
241   const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
242   unsigned insnType = llvm::HexagonMCInstrInfo::getType(MCII, MI);
243
244   if (insnType == HexagonII::TypePREFIX) {
245     switch (kind) {
246     case llvm::MCSymbolRefExpr::VK_GOTOFF:
247       return Hexagon::fixup_Hexagon_GOTREL_32_6_X;
248     case llvm::MCSymbolRefExpr::VK_GOT:
249       return Hexagon::fixup_Hexagon_GOT_32_6_X;
250     case llvm::MCSymbolRefExpr::VK_TPREL:
251       return Hexagon::fixup_Hexagon_TPREL_32_6_X;
252     case llvm::MCSymbolRefExpr::VK_DTPREL:
253       return Hexagon::fixup_Hexagon_DTPREL_32_6_X;
254     case llvm::MCSymbolRefExpr::VK_Hexagon_GD_GOT:
255       return Hexagon::fixup_Hexagon_GD_GOT_32_6_X;
256     case llvm::MCSymbolRefExpr::VK_Hexagon_LD_GOT:
257       return Hexagon::fixup_Hexagon_LD_GOT_32_6_X;
258     case llvm::MCSymbolRefExpr::VK_Hexagon_IE:
259       return Hexagon::fixup_Hexagon_IE_32_6_X;
260     case llvm::MCSymbolRefExpr::VK_Hexagon_IE_GOT:
261       return Hexagon::fixup_Hexagon_IE_GOT_32_6_X;
262     default:
263       if (MCID.isBranch())
264         return Hexagon::fixup_Hexagon_B32_PCREL_X;
265       else
266         return Hexagon::fixup_Hexagon_32_6_X;
267     }
268   } else if (MCID.isBranch())
269     return (Hexagon::fixup_Hexagon_B13_PCREL);
270
271   switch (MCID.getOpcode()) {
272   case Hexagon::HI:
273   case Hexagon::A2_tfrih:
274     switch (kind) {
275     case llvm::MCSymbolRefExpr::VK_GOT:
276       return Hexagon::fixup_Hexagon_GOT_HI16;
277     case llvm::MCSymbolRefExpr::VK_GOTOFF:
278       return Hexagon::fixup_Hexagon_GOTREL_HI16;
279     case llvm::MCSymbolRefExpr::VK_Hexagon_GD_GOT:
280       return Hexagon::fixup_Hexagon_GD_GOT_HI16;
281     case llvm::MCSymbolRefExpr::VK_Hexagon_LD_GOT:
282       return Hexagon::fixup_Hexagon_LD_GOT_HI16;
283     case llvm::MCSymbolRefExpr::VK_Hexagon_IE:
284       return Hexagon::fixup_Hexagon_IE_HI16;
285     case llvm::MCSymbolRefExpr::VK_Hexagon_IE_GOT:
286       return Hexagon::fixup_Hexagon_IE_GOT_HI16;
287     case llvm::MCSymbolRefExpr::VK_TPREL:
288       return Hexagon::fixup_Hexagon_TPREL_HI16;
289     case llvm::MCSymbolRefExpr::VK_DTPREL:
290       return Hexagon::fixup_Hexagon_DTPREL_HI16;
291     default:
292       return Hexagon::fixup_Hexagon_HI16;
293     }
294
295   case Hexagon::LO:
296   case Hexagon::A2_tfril:
297     switch (kind) {
298     case llvm::MCSymbolRefExpr::VK_GOT:
299       return Hexagon::fixup_Hexagon_GOT_LO16;
300     case llvm::MCSymbolRefExpr::VK_GOTOFF:
301       return Hexagon::fixup_Hexagon_GOTREL_LO16;
302     case llvm::MCSymbolRefExpr::VK_Hexagon_GD_GOT:
303       return Hexagon::fixup_Hexagon_GD_GOT_LO16;
304     case llvm::MCSymbolRefExpr::VK_Hexagon_LD_GOT:
305       return Hexagon::fixup_Hexagon_LD_GOT_LO16;
306     case llvm::MCSymbolRefExpr::VK_Hexagon_IE:
307       return Hexagon::fixup_Hexagon_IE_LO16;
308     case llvm::MCSymbolRefExpr::VK_Hexagon_IE_GOT:
309       return Hexagon::fixup_Hexagon_IE_GOT_LO16;
310     case llvm::MCSymbolRefExpr::VK_TPREL:
311       return Hexagon::fixup_Hexagon_TPREL_LO16;
312     case llvm::MCSymbolRefExpr::VK_DTPREL:
313       return Hexagon::fixup_Hexagon_DTPREL_LO16;
314     default:
315       return Hexagon::fixup_Hexagon_LO16;
316     }
317
318   // The only relocs left should be GP relative:
319   default:
320     if (MCID.mayStore() || MCID.mayLoad()) {
321       for (const uint16_t *ImpUses = MCID.getImplicitUses(); *ImpUses;
322            ++ImpUses) {
323         if (*ImpUses == Hexagon::GP) {
324           switch (HexagonMCInstrInfo::getAccessSize(MCII, MI)) {
325           case HexagonII::MemAccessSize::ByteAccess:
326             return fixup_Hexagon_GPREL16_0;
327           case HexagonII::MemAccessSize::HalfWordAccess:
328             return fixup_Hexagon_GPREL16_1;
329           case HexagonII::MemAccessSize::WordAccess:
330             return fixup_Hexagon_GPREL16_2;
331           case HexagonII::MemAccessSize::DoubleWordAccess:
332             return fixup_Hexagon_GPREL16_3;
333           default:
334             llvm_unreachable("unhandled fixup");
335           }
336         }
337       }
338     } else
339       llvm_unreachable("unhandled fixup");
340   }
341
342   return LastTargetFixupKind;
343 }
344
345 namespace llvm {
346 extern const MCInstrDesc HexagonInsts[];
347 }
348
349 namespace {
350   bool isPCRel (unsigned Kind) {
351     switch(Kind){
352     case fixup_Hexagon_B22_PCREL:
353     case fixup_Hexagon_B15_PCREL:
354     case fixup_Hexagon_B7_PCREL:
355     case fixup_Hexagon_B13_PCREL:
356     case fixup_Hexagon_B9_PCREL:
357     case fixup_Hexagon_B32_PCREL_X:
358     case fixup_Hexagon_B22_PCREL_X:
359     case fixup_Hexagon_B15_PCREL_X:
360     case fixup_Hexagon_B13_PCREL_X:
361     case fixup_Hexagon_B9_PCREL_X:
362     case fixup_Hexagon_B7_PCREL_X:
363     case fixup_Hexagon_32_PCREL:
364     case fixup_Hexagon_PLT_B22_PCREL:
365     case fixup_Hexagon_GD_PLT_B22_PCREL:
366     case fixup_Hexagon_LD_PLT_B22_PCREL:
367     case fixup_Hexagon_6_PCREL_X:
368       return true;
369     default:
370       return false;
371     }
372   }
373 }
374
375 unsigned HexagonMCCodeEmitter::getExprOpValue(const MCInst &MI,
376                                               const MCOperand &MO,
377                                               const MCExpr *ME,
378                                               SmallVectorImpl<MCFixup> &Fixups,
379                                               const MCSubtargetInfo &STI) const
380
381 {
382   int64_t Res;
383
384   if (ME->evaluateAsAbsolute(Res))
385     return Res;
386
387   MCExpr::ExprKind MK = ME->getKind();
388   if (MK == MCExpr::Constant) {
389     return cast<MCConstantExpr>(ME)->getValue();
390   }
391   if (MK == MCExpr::Binary) {
392     unsigned Res;
393     Res = getExprOpValue(MI, MO, cast<MCBinaryExpr>(ME)->getLHS(), Fixups, STI);
394     Res +=
395         getExprOpValue(MI, MO, cast<MCBinaryExpr>(ME)->getRHS(), Fixups, STI);
396     return 0;
397   }
398
399   assert(MK == MCExpr::SymbolRef);
400
401   Hexagon::Fixups FixupKind =
402       Hexagon::Fixups(Hexagon::fixup_Hexagon_TPREL_LO16);
403   const MCSymbolRefExpr *MCSRE = static_cast<const MCSymbolRefExpr *>(ME);
404   const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(MCII, MI);
405   unsigned bits = HexagonMCInstrInfo::getExtentBits(MCII, MI) -
406                   HexagonMCInstrInfo::getExtentAlignment(MCII, MI);
407   const MCSymbolRefExpr::VariantKind kind = MCSRE->getKind();
408
409   DEBUG(dbgs() << "----------------------------------------\n");
410   DEBUG(dbgs() << "Opcode Name: " << HexagonMCInstrInfo::getName(MCII, MI)
411                << "\n");
412   DEBUG(dbgs() << "Opcode: " << MCID.getOpcode() << "\n");
413   DEBUG(dbgs() << "Relocation bits: " << bits << "\n");
414   DEBUG(dbgs() << "Addend: " << *Addend << "\n");
415   DEBUG(dbgs() << "----------------------------------------\n");
416
417   switch (bits) {
418   default:
419     DEBUG(dbgs() << "unrecognized bit count of " << bits << '\n');
420     break;
421
422   case 32:
423     switch (kind) {
424     case llvm::MCSymbolRefExpr::VK_Hexagon_PCREL:
425       FixupKind = Hexagon::fixup_Hexagon_32_PCREL;
426       break;
427     case llvm::MCSymbolRefExpr::VK_GOT:
428       FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOT_32_6_X
429                             : Hexagon::fixup_Hexagon_GOT_32;
430       break;
431     case llvm::MCSymbolRefExpr::VK_GOTOFF:
432       FixupKind = *Extended ? Hexagon::fixup_Hexagon_GOTREL_32_6_X
433                             : Hexagon::fixup_Hexagon_GOTREL_32;
434       break;
435     case llvm::MCSymbolRefExpr::VK_Hexagon_GD_GOT:
436       FixupKind = *Extended ? Hexagon::fixup_Hexagon_GD_GOT_32_6_X
437                             : Hexagon::fixup_Hexagon_GD_GOT_32;
438       break;
439     case llvm::MCSymbolRefExpr::VK_Hexagon_LD_GOT:
440       FixupKind = *Extended ? Hexagon::fixup_Hexagon_LD_GOT_32_6_X
441                             : Hexagon::fixup_Hexagon_LD_GOT_32;
442       break;
443     case llvm::MCSymbolRefExpr::VK_Hexagon_IE:
444       FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_32_6_X
445                             : Hexagon::fixup_Hexagon_IE_32;
446       break;
447     case llvm::MCSymbolRefExpr::VK_Hexagon_IE_GOT:
448       FixupKind = *Extended ? Hexagon::fixup_Hexagon_IE_GOT_32_6_X
449                             : Hexagon::fixup_Hexagon_IE_GOT_32;
450       break;
451     case llvm::MCSymbolRefExpr::VK_TPREL:
452       FixupKind = *Extended ? Hexagon::fixup_Hexagon_TPREL_32_6_X
453                             : Hexagon::fixup_Hexagon_TPREL_32;
454       break;
455     case llvm::MCSymbolRefExpr::VK_DTPREL:
456       FixupKind = *Extended ? Hexagon::fixup_Hexagon_DTPREL_32_6_X
457                             : Hexagon::fixup_Hexagon_DTPREL_32;
458       break;
459     default:
460       FixupKind =
461           *Extended ? Hexagon::fixup_Hexagon_32_6_X : Hexagon::fixup_Hexagon_32;
462       break;
463     }
464     break;
465
466   case 22:
467     switch (kind) {
468     case llvm::MCSymbolRefExpr::VK_Hexagon_GD_PLT:
469       FixupKind = Hexagon::fixup_Hexagon_GD_PLT_B22_PCREL;
470       break;
471     case llvm::MCSymbolRefExpr::VK_Hexagon_LD_PLT:
472       FixupKind = Hexagon::fixup_Hexagon_LD_PLT_B22_PCREL;
473       break;
474     default:
475       if (MCID.isBranch() || MCID.isCall()) {
476         FixupKind = *Extended ? Hexagon::fixup_Hexagon_B22_PCREL_X
477                               : Hexagon::fixup_Hexagon_B22_PCREL;
478       } else {
479         errs() << "unrecognized relocation, bits: " << bits << "\n";
480         errs() << "name = " << HexagonMCInstrInfo::getName(MCII, MI) << "\n";
481       }
482       break;
483     }
484     break;
485
486   case 16:
487     if (*Extended) {
488       switch (kind) {
489       default:
490         FixupKind = Hexagon::fixup_Hexagon_16_X;
491         break;
492       case llvm::MCSymbolRefExpr::VK_GOT:
493         FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
494         break;
495       case llvm::MCSymbolRefExpr::VK_GOTOFF:
496         FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
497         break;
498       case llvm::MCSymbolRefExpr::VK_Hexagon_GD_GOT:
499         FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16_X;
500         break;
501       case llvm::MCSymbolRefExpr::VK_Hexagon_LD_GOT:
502         FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16_X;
503         break;
504       case llvm::MCSymbolRefExpr::VK_Hexagon_IE:
505         FixupKind = Hexagon::fixup_Hexagon_IE_16_X;
506         break;
507       case llvm::MCSymbolRefExpr::VK_Hexagon_IE_GOT:
508         FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16_X;
509         break;
510       case llvm::MCSymbolRefExpr::VK_TPREL:
511         FixupKind = Hexagon::fixup_Hexagon_TPREL_16_X;
512         break;
513       case llvm::MCSymbolRefExpr::VK_DTPREL:
514         FixupKind = Hexagon::fixup_Hexagon_DTPREL_16_X;
515         break;
516       }
517     } else
518       switch (kind) {
519       default:
520         errs() << "unrecognized relocation, bits " << bits << "\n";
521         errs() << "name = " << HexagonMCInstrInfo::getName(MCII, MI) << "\n";
522         break;
523       case llvm::MCSymbolRefExpr::VK_GOTOFF:
524         if ((MCID.getOpcode() == Hexagon::HI) ||
525             (MCID.getOpcode() == Hexagon::LO_H))
526           FixupKind = Hexagon::fixup_Hexagon_GOTREL_HI16;
527         else
528           FixupKind = Hexagon::fixup_Hexagon_GOTREL_LO16;
529         break;
530       case llvm::MCSymbolRefExpr::VK_Hexagon_GPREL:
531         FixupKind = Hexagon::fixup_Hexagon_GPREL16_0;
532         break;
533       case llvm::MCSymbolRefExpr::VK_Hexagon_LO16:
534         FixupKind = Hexagon::fixup_Hexagon_LO16;
535         break;
536       case llvm::MCSymbolRefExpr::VK_Hexagon_HI16:
537         FixupKind = Hexagon::fixup_Hexagon_HI16;
538         break;
539       case llvm::MCSymbolRefExpr::VK_Hexagon_GD_GOT:
540         FixupKind = Hexagon::fixup_Hexagon_GD_GOT_16;
541         break;
542       case llvm::MCSymbolRefExpr::VK_Hexagon_LD_GOT:
543         FixupKind = Hexagon::fixup_Hexagon_LD_GOT_16;
544         break;
545       case llvm::MCSymbolRefExpr::VK_Hexagon_IE_GOT:
546         FixupKind = Hexagon::fixup_Hexagon_IE_GOT_16;
547         break;
548       case llvm::MCSymbolRefExpr::VK_TPREL:
549         FixupKind = Hexagon::fixup_Hexagon_TPREL_16;
550         break;
551       case llvm::MCSymbolRefExpr::VK_DTPREL:
552         FixupKind = Hexagon::fixup_Hexagon_DTPREL_16;
553         break;
554       }
555     break;
556
557   case 15:
558     if (MCID.isBranch() || MCID.isCall())
559       FixupKind = *Extended ? Hexagon::fixup_Hexagon_B15_PCREL_X
560                             : Hexagon::fixup_Hexagon_B15_PCREL;
561     break;
562
563   case 13:
564     if (MCID.isBranch())
565       FixupKind = Hexagon::fixup_Hexagon_B13_PCREL;
566     else {
567       errs() << "unrecognized relocation, bits " << bits << "\n";
568       errs() << "name = " << HexagonMCInstrInfo::getName(MCII, MI) << "\n";
569     }
570     break;
571
572   case 12:
573     if (*Extended)
574       switch (kind) {
575       default:
576         FixupKind = Hexagon::fixup_Hexagon_12_X;
577         break;
578       // There isn't a GOT_12_X, both 11_X and 16_X resolve to 6/26
579       case llvm::MCSymbolRefExpr::VK_GOT:
580         FixupKind = Hexagon::fixup_Hexagon_GOT_16_X;
581         break;
582       case llvm::MCSymbolRefExpr::VK_GOTOFF:
583         FixupKind = Hexagon::fixup_Hexagon_GOTREL_16_X;
584         break;
585       }
586     else {
587       errs() << "unrecognized relocation, bits " << bits << "\n";
588       errs() << "name = " << HexagonMCInstrInfo::getName(MCII, MI) << "\n";
589     }
590     break;
591
592   case 11:
593     if (*Extended)
594       switch (kind) {
595       default:
596         FixupKind = Hexagon::fixup_Hexagon_11_X;
597         break;
598       case llvm::MCSymbolRefExpr::VK_GOT:
599         FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
600         break;
601       case llvm::MCSymbolRefExpr::VK_GOTOFF:
602         FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
603         break;
604       case llvm::MCSymbolRefExpr::VK_Hexagon_GD_GOT:
605         FixupKind = Hexagon::fixup_Hexagon_GD_GOT_11_X;
606         break;
607       case llvm::MCSymbolRefExpr::VK_Hexagon_LD_GOT:
608         FixupKind = Hexagon::fixup_Hexagon_LD_GOT_11_X;
609         break;
610       case llvm::MCSymbolRefExpr::VK_Hexagon_IE_GOT:
611         FixupKind = Hexagon::fixup_Hexagon_IE_GOT_11_X;
612         break;
613       case llvm::MCSymbolRefExpr::VK_TPREL:
614         FixupKind = Hexagon::fixup_Hexagon_TPREL_11_X;
615         break;
616       case llvm::MCSymbolRefExpr::VK_DTPREL:
617         FixupKind = Hexagon::fixup_Hexagon_DTPREL_11_X;
618         break;
619       }
620     else {
621       errs() << "unrecognized relocation, bits " << bits << "\n";
622       errs() << "name = " << HexagonMCInstrInfo::getName(MCII, MI) << "\n";
623     }
624     break;
625
626   case 10:
627     if (*Extended)
628       FixupKind = Hexagon::fixup_Hexagon_10_X;
629     break;
630
631   case 9:
632     if (MCID.isBranch() ||
633         (llvm::HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
634       FixupKind = *Extended ? Hexagon::fixup_Hexagon_B9_PCREL_X
635                             : Hexagon::fixup_Hexagon_B9_PCREL;
636     else if (*Extended)
637       FixupKind = Hexagon::fixup_Hexagon_9_X;
638     else {
639       errs() << "unrecognized relocation, bits " << bits << "\n";
640       errs() << "name = " << HexagonMCInstrInfo::getName(MCII, MI) << "\n";
641     }
642     break;
643
644   case 8:
645     if (*Extended)
646       FixupKind = Hexagon::fixup_Hexagon_8_X;
647     else {
648       errs() << "unrecognized relocation, bits " << bits << "\n";
649       errs() << "name = " << HexagonMCInstrInfo::getName(MCII, MI) << "\n";
650     }
651     break;
652
653   case 7:
654     if (MCID.isBranch() ||
655         (llvm::HexagonMCInstrInfo::getType(MCII, MI) == HexagonII::TypeCR))
656       FixupKind = *Extended ? Hexagon::fixup_Hexagon_B7_PCREL_X
657                             : Hexagon::fixup_Hexagon_B7_PCREL;
658     else if (*Extended)
659       FixupKind = Hexagon::fixup_Hexagon_7_X;
660     else {
661       errs() << "unrecognized relocation, bits " << bits << "\n";
662       errs() << "name = " << HexagonMCInstrInfo::getName(MCII, MI) << "\n";
663     }
664     break;
665
666   case 6:
667     if (*Extended) {
668       switch (kind) {
669       default:
670         FixupKind = Hexagon::fixup_Hexagon_6_X;
671         break;
672       case llvm::MCSymbolRefExpr::VK_Hexagon_PCREL:
673         FixupKind = Hexagon::fixup_Hexagon_6_PCREL_X;
674         break;
675       // This is part of an extender, GOT_11 is a
676       // Word32_U6 unsigned/truncated reloc.
677       case llvm::MCSymbolRefExpr::VK_GOT:
678         FixupKind = Hexagon::fixup_Hexagon_GOT_11_X;
679         break;
680       case llvm::MCSymbolRefExpr::VK_GOTOFF:
681         FixupKind = Hexagon::fixup_Hexagon_GOTREL_11_X;
682         break;
683       }
684     } else {
685       errs() << "unrecognized relocation, bits " << bits << "\n";
686       errs() << "name = " << HexagonMCInstrInfo::getName(MCII, MI) << "\n";
687     }
688     break;
689
690   case 0:
691     FixupKind = getFixupNoBits(MCII, MI, MO, kind);
692     break;
693   }
694
695   MCExpr const *FixupExpression = (*Addend > 0 && isPCRel(FixupKind)) ?
696     MCBinaryExpr::createAdd(MO.getExpr(),
697                             MCConstantExpr::create(*Addend, MCT), MCT) :
698     MO.getExpr();
699
700   MCFixup fixup = MCFixup::create(*Addend, FixupExpression, 
701                                   MCFixupKind(FixupKind), MI.getLoc());
702   Fixups.push_back(fixup);
703   // All of the information is in the fixup.
704   return (0);
705 }
706
707 unsigned
708 HexagonMCCodeEmitter::getMachineOpValue(MCInst const &MI, MCOperand const &MO,
709                                         SmallVectorImpl<MCFixup> &Fixups,
710                                         MCSubtargetInfo const &STI) const {
711   if (MO.isReg())
712     return MCT.getRegisterInfo()->getEncodingValue(MO.getReg());
713   if (MO.isImm())
714     return static_cast<unsigned>(MO.getImm());
715
716   // MO must be an ME.
717   assert(MO.isExpr());
718   return getExprOpValue(MI, MO, MO.getExpr(), Fixups, STI);
719 }
720
721 MCCodeEmitter *llvm::createHexagonMCCodeEmitter(MCInstrInfo const &MII,
722                                                 MCRegisterInfo const &MRI,
723                                                 MCContext &MCT) {
724   return new HexagonMCCodeEmitter(MII, MCT);
725 }
726
727 #include "HexagonGenMCCodeEmitter.inc"