LLVM  5.0.0
HexagonAsmBackend.cpp
Go to the documentation of this file.
1 //===-- HexagonAsmBackend.cpp - Hexagon Assembler Backend -----------------===//
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 "HexagonFixupKinds.h"
18 #include "llvm/MC/MCAsmBackend.h"
19 #include "llvm/MC/MCAsmLayout.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCInstrInfo.h"
25 #include "llvm/MC/MCObjectWriter.h"
26 #include "llvm/Support/Debug.h"
28 
29 #include <sstream>
30 
31 using namespace llvm;
32 using namespace Hexagon;
33 
34 #define DEBUG_TYPE "hexagon-asm-backend"
35 
37  ("mno-fixup", cl::desc("Disable fixing up resolved relocations for Hexagon"));
38 
39 namespace {
40 
41 class HexagonAsmBackend : public MCAsmBackend {
42  uint8_t OSABI;
43  StringRef CPU;
44  mutable uint64_t relaxedCnt;
45  std::unique_ptr <MCInstrInfo> MCII;
46  std::unique_ptr <MCInst *> RelaxTarget;
47  MCInst * Extender;
48 
49  void ReplaceInstruction(MCCodeEmitter &E, MCRelaxableFragment &RF,
50  MCInst &HMB) const {
52  SmallString<256> Code;
53  raw_svector_ostream VecOS(Code);
54  E.encodeInstruction(HMB, VecOS, Fixups, RF.getSubtargetInfo());
55 
56  // Update the fragment.
57  RF.setInst(HMB);
58  RF.getContents() = Code;
59  RF.getFixups() = Fixups;
60  }
61 
62 public:
63  HexagonAsmBackend(const Target &T, const Triple &TT, uint8_t OSABI,
64  StringRef CPU) :
65  OSABI(OSABI), CPU(CPU), MCII(T.createMCInstrInfo()),
66  RelaxTarget(new MCInst *), Extender(nullptr) {}
67 
68  MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
69  return createHexagonELFObjectWriter(OS, OSABI, CPU);
70  }
71 
72  void setExtender(MCContext &Context) const {
73  if (Extender == nullptr)
74  const_cast<HexagonAsmBackend *>(this)->Extender = new (Context) MCInst;
75  }
76 
77  MCInst *takeExtender() const {
78  assert(Extender != nullptr);
79  MCInst * Result = Extender;
80  const_cast<HexagonAsmBackend *>(this)->Extender = nullptr;
81  return Result;
82  }
83 
84  unsigned getNumFixupKinds() const override {
86  }
87 
88  const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
89  const static MCFixupKindInfo Infos[Hexagon::NumTargetFixupKinds] = {
90  // This table *must* be in same the order of fixup_* kinds in
91  // HexagonFixupKinds.h.
92  //
93  // namei offset bits flags
94  { "fixup_Hexagon_B22_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
95  { "fixup_Hexagon_B15_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
96  { "fixup_Hexagon_B7_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
97  { "fixup_Hexagon_LO16", 0, 32, 0 },
98  { "fixup_Hexagon_HI16", 0, 32, 0 },
99  { "fixup_Hexagon_32", 0, 32, 0 },
100  { "fixup_Hexagon_16", 0, 32, 0 },
101  { "fixup_Hexagon_8", 0, 32, 0 },
102  { "fixup_Hexagon_GPREL16_0", 0, 32, 0 },
103  { "fixup_Hexagon_GPREL16_1", 0, 32, 0 },
104  { "fixup_Hexagon_GPREL16_2", 0, 32, 0 },
105  { "fixup_Hexagon_GPREL16_3", 0, 32, 0 },
106  { "fixup_Hexagon_HL16", 0, 32, 0 },
107  { "fixup_Hexagon_B13_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
108  { "fixup_Hexagon_B9_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
109  { "fixup_Hexagon_B32_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
110  { "fixup_Hexagon_32_6_X", 0, 32, 0 },
111  { "fixup_Hexagon_B22_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
112  { "fixup_Hexagon_B15_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
113  { "fixup_Hexagon_B13_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
114  { "fixup_Hexagon_B9_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
115  { "fixup_Hexagon_B7_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
116  { "fixup_Hexagon_16_X", 0, 32, 0 },
117  { "fixup_Hexagon_12_X", 0, 32, 0 },
118  { "fixup_Hexagon_11_X", 0, 32, 0 },
119  { "fixup_Hexagon_10_X", 0, 32, 0 },
120  { "fixup_Hexagon_9_X", 0, 32, 0 },
121  { "fixup_Hexagon_8_X", 0, 32, 0 },
122  { "fixup_Hexagon_7_X", 0, 32, 0 },
123  { "fixup_Hexagon_6_X", 0, 32, 0 },
124  { "fixup_Hexagon_32_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
125  { "fixup_Hexagon_COPY", 0, 32, 0 },
126  { "fixup_Hexagon_GLOB_DAT", 0, 32, 0 },
127  { "fixup_Hexagon_JMP_SLOT", 0, 32, 0 },
128  { "fixup_Hexagon_RELATIVE", 0, 32, 0 },
129  { "fixup_Hexagon_PLT_B22_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
130  { "fixup_Hexagon_GOTREL_LO16", 0, 32, 0 },
131  { "fixup_Hexagon_GOTREL_HI16", 0, 32, 0 },
132  { "fixup_Hexagon_GOTREL_32", 0, 32, 0 },
133  { "fixup_Hexagon_GOT_LO16", 0, 32, 0 },
134  { "fixup_Hexagon_GOT_HI16", 0, 32, 0 },
135  { "fixup_Hexagon_GOT_32", 0, 32, 0 },
136  { "fixup_Hexagon_GOT_16", 0, 32, 0 },
137  { "fixup_Hexagon_DTPMOD_32", 0, 32, 0 },
138  { "fixup_Hexagon_DTPREL_LO16", 0, 32, 0 },
139  { "fixup_Hexagon_DTPREL_HI16", 0, 32, 0 },
140  { "fixup_Hexagon_DTPREL_32", 0, 32, 0 },
141  { "fixup_Hexagon_DTPREL_16", 0, 32, 0 },
142  { "fixup_Hexagon_GD_PLT_B22_PCREL",0, 32, MCFixupKindInfo::FKF_IsPCRel },
143  { "fixup_Hexagon_LD_PLT_B22_PCREL",0, 32, MCFixupKindInfo::FKF_IsPCRel },
144  { "fixup_Hexagon_GD_GOT_LO16", 0, 32, 0 },
145  { "fixup_Hexagon_GD_GOT_HI16", 0, 32, 0 },
146  { "fixup_Hexagon_GD_GOT_32", 0, 32, 0 },
147  { "fixup_Hexagon_GD_GOT_16", 0, 32, 0 },
148  { "fixup_Hexagon_LD_GOT_LO16", 0, 32, 0 },
149  { "fixup_Hexagon_LD_GOT_HI16", 0, 32, 0 },
150  { "fixup_Hexagon_LD_GOT_32", 0, 32, 0 },
151  { "fixup_Hexagon_LD_GOT_16", 0, 32, 0 },
152  { "fixup_Hexagon_IE_LO16", 0, 32, 0 },
153  { "fixup_Hexagon_IE_HI16", 0, 32, 0 },
154  { "fixup_Hexagon_IE_32", 0, 32, 0 },
155  { "fixup_Hexagon_IE_16", 0, 32, 0 },
156  { "fixup_Hexagon_IE_GOT_LO16", 0, 32, 0 },
157  { "fixup_Hexagon_IE_GOT_HI16", 0, 32, 0 },
158  { "fixup_Hexagon_IE_GOT_32", 0, 32, 0 },
159  { "fixup_Hexagon_IE_GOT_16", 0, 32, 0 },
160  { "fixup_Hexagon_TPREL_LO16", 0, 32, 0 },
161  { "fixup_Hexagon_TPREL_HI16", 0, 32, 0 },
162  { "fixup_Hexagon_TPREL_32", 0, 32, 0 },
163  { "fixup_Hexagon_TPREL_16", 0, 32, 0 },
164  { "fixup_Hexagon_6_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
165  { "fixup_Hexagon_GOTREL_32_6_X", 0, 32, 0 },
166  { "fixup_Hexagon_GOTREL_16_X", 0, 32, 0 },
167  { "fixup_Hexagon_GOTREL_11_X", 0, 32, 0 },
168  { "fixup_Hexagon_GOT_32_6_X", 0, 32, 0 },
169  { "fixup_Hexagon_GOT_16_X", 0, 32, 0 },
170  { "fixup_Hexagon_GOT_11_X", 0, 32, 0 },
171  { "fixup_Hexagon_DTPREL_32_6_X", 0, 32, 0 },
172  { "fixup_Hexagon_DTPREL_16_X", 0, 32, 0 },
173  { "fixup_Hexagon_DTPREL_11_X", 0, 32, 0 },
174  { "fixup_Hexagon_GD_GOT_32_6_X", 0, 32, 0 },
175  { "fixup_Hexagon_GD_GOT_16_X", 0, 32, 0 },
176  { "fixup_Hexagon_GD_GOT_11_X", 0, 32, 0 },
177  { "fixup_Hexagon_LD_GOT_32_6_X", 0, 32, 0 },
178  { "fixup_Hexagon_LD_GOT_16_X", 0, 32, 0 },
179  { "fixup_Hexagon_LD_GOT_11_X", 0, 32, 0 },
180  { "fixup_Hexagon_IE_32_6_X", 0, 32, 0 },
181  { "fixup_Hexagon_IE_16_X", 0, 32, 0 },
182  { "fixup_Hexagon_IE_GOT_32_6_X", 0, 32, 0 },
183  { "fixup_Hexagon_IE_GOT_16_X", 0, 32, 0 },
184  { "fixup_Hexagon_IE_GOT_11_X", 0, 32, 0 },
185  { "fixup_Hexagon_TPREL_32_6_X", 0, 32, 0 },
186  { "fixup_Hexagon_TPREL_16_X", 0, 32, 0 },
187  { "fixup_Hexagon_TPREL_11_X", 0, 32, 0 },
188  { "fixup_Hexagon_GD_PLT_B22_PCREL_X",0, 32, MCFixupKindInfo::FKF_IsPCRel },
189  { "fixup_Hexagon_GD_PLT_B32_PCREL_X",0, 32, MCFixupKindInfo::FKF_IsPCRel },
190  { "fixup_Hexagon_LD_PLT_B22_PCREL_X",0, 32, MCFixupKindInfo::FKF_IsPCRel },
191  { "fixup_Hexagon_LD_PLT_B32_PCREL_X",0, 32, MCFixupKindInfo::FKF_IsPCRel }
192  };
193 
194  if (Kind < FirstTargetFixupKind)
195  return MCAsmBackend::getFixupKindInfo(Kind);
196 
197  assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
198  "Invalid kind!");
199  return Infos[Kind - FirstTargetFixupKind];
200  }
201 
202  bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
203  const MCValue &Target) override {
204  MCFixupKind Kind = Fixup.getKind();
205 
206  switch((unsigned)Kind) {
207  default:
208  llvm_unreachable("Unknown Fixup Kind!");
209 
210  case fixup_Hexagon_LO16:
211  case fixup_Hexagon_HI16:
212  case fixup_Hexagon_16:
213  case fixup_Hexagon_8:
218  case fixup_Hexagon_HL16:
220  case fixup_Hexagon_16_X:
221  case fixup_Hexagon_12_X:
222  case fixup_Hexagon_11_X:
223  case fixup_Hexagon_10_X:
224  case fixup_Hexagon_9_X:
225  case fixup_Hexagon_8_X:
226  case fixup_Hexagon_7_X:
227  case fixup_Hexagon_6_X:
228  case fixup_Hexagon_COPY:
257  case fixup_Hexagon_IE_32:
258  case fixup_Hexagon_IE_16:
298  // These relocations should always have a relocation recorded
299  return true;
300 
302  //IsResolved = false;
303  break;
304 
315  if (DisableFixup)
316  return true;
317  break;
318 
319  case FK_Data_1:
320  case FK_Data_2:
321  case FK_Data_4:
322  case FK_PCRel_4:
323  case fixup_Hexagon_32:
324  // Leave these relocations alone as they are used for EH.
325  return false;
326  }
327  return false;
328  }
329 
330  /// getFixupKindNumBytes - The number of bytes the fixup may change.
331  static unsigned getFixupKindNumBytes(unsigned Kind) {
332  switch (Kind) {
333  default:
334  return 0;
335 
336  case FK_Data_1:
337  return 1;
338  case FK_Data_2:
339  return 2;
340  case FK_Data_4: // this later gets mapped to R_HEX_32
341  case FK_PCRel_4: // this later gets mapped to R_HEX_32_PCREL
342  case fixup_Hexagon_32:
356  return 4;
357  }
358  }
359 
360  // Make up for left shift when encoding the operand.
361  static uint64_t adjustFixupValue(MCFixupKind Kind, uint64_t Value) {
362  switch((unsigned)Kind) {
363  default:
364  break;
365 
371  Value >>= 2;
372  break;
373 
379  Value &= 0x3f;
380  break;
381 
385  Value >>= 6;
386  break;
387  }
388  return (Value);
389  }
390 
391  void HandleFixupError(const int bits, const int align_bits,
392  const int64_t FixupValue, const char *fixupStr) const {
393  // Error: value 1124 out of range: -1024-1023 when resolving
394  // symbol in file xprtsock.S
395  const APInt IntMin = APInt::getSignedMinValue(bits+align_bits);
396  const APInt IntMax = APInt::getSignedMaxValue(bits+align_bits);
397  std::stringstream errStr;
398  errStr << "\nError: value " <<
399  FixupValue <<
400  " out of range: " <<
401  IntMin.getSExtValue() <<
402  "-" <<
403  IntMax.getSExtValue() <<
404  " when resolving " <<
405  fixupStr <<
406  " fixup\n";
407  llvm_unreachable(errStr.str().c_str());
408  }
409 
410  /// ApplyFixup - Apply the \arg Value for given \arg Fixup into the provided
411  /// data fragment, at the offset specified by the fixup and following the
412  /// fixup kind as appropriate.
413  void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
414  const MCValue &Target, MutableArrayRef<char> Data,
415  uint64_t FixupValue, bool IsResolved) const override {
416 
417  // When FixupValue is 0 the relocation is external and there
418  // is nothing for us to do.
419  if (!FixupValue) return;
420 
421  MCFixupKind Kind = Fixup.getKind();
422  uint64_t Value;
423  uint32_t InstMask;
424  uint32_t Reloc;
425 
426  // LLVM gives us an encoded value, we have to convert it back
427  // to a real offset before we can use it.
428  uint32_t Offset = Fixup.getOffset();
429  unsigned NumBytes = getFixupKindNumBytes(Kind);
430  assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
431  char *InstAddr = Data.data() + Offset;
432 
433  Value = adjustFixupValue(Kind, FixupValue);
434  if(!Value)
435  return;
436  int sValue = (int)Value;
437 
438  switch((unsigned)Kind) {
439  default:
440  return;
441 
443  if (!(isIntN(7, sValue)))
444  HandleFixupError(7, 2, (int64_t)FixupValue, "B7_PCREL");
447  InstMask = 0x00001f18; // Word32_B7
448  Reloc = (((Value >> 2) & 0x1f) << 8) | // Value 6-2 = Target 12-8
449  ((Value & 0x3) << 3); // Value 1-0 = Target 4-3
450  break;
451 
453  if (!(isIntN(9, sValue)))
454  HandleFixupError(9, 2, (int64_t)FixupValue, "B9_PCREL");
457  InstMask = 0x003000fe; // Word32_B9
458  Reloc = (((Value >> 7) & 0x3) << 20) | // Value 8-7 = Target 21-20
459  ((Value & 0x7f) << 1); // Value 6-0 = Target 7-1
460  break;
461 
462  // Since the existing branches that use this relocation cannot be
463  // extended, they should only be fixed up if the target is within range.
465  if (!(isIntN(13, sValue)))
466  HandleFixupError(13, 2, (int64_t)FixupValue, "B13_PCREL");
469  InstMask = 0x00202ffe; // Word32_B13
470  Reloc = (((Value >> 12) & 0x1) << 21) | // Value 12 = Target 21
471  (((Value >> 11) & 0x1) << 13) | // Value 11 = Target 13
472  ((Value & 0x7ff) << 1); // Value 10-0 = Target 11-1
473  break;
474 
476  if (!(isIntN(15, sValue)))
477  HandleFixupError(15, 2, (int64_t)FixupValue, "B15_PCREL");
480  InstMask = 0x00df20fe; // Word32_B15
481  Reloc = (((Value >> 13) & 0x3) << 22) | // Value 14-13 = Target 23-22
482  (((Value >> 8) & 0x1f) << 16) | // Value 12-8 = Target 20-16
483  (((Value >> 7) & 0x1) << 13) | // Value 7 = Target 13
484  ((Value & 0x7f) << 1); // Value 6-0 = Target 7-1
485  break;
486 
488  if (!(isIntN(22, sValue)))
489  HandleFixupError(22, 2, (int64_t)FixupValue, "B22_PCREL");
492  InstMask = 0x01ff3ffe; // Word32_B22
493  Reloc = (((Value >> 13) & 0x1ff) << 16) | // Value 21-13 = Target 24-16
494  ((Value & 0x1fff) << 1); // Value 12-0 = Target 13-1
495  break;
496 
498  InstMask = 0x0fff3fff; // Word32_X26
499  Reloc = (((Value >> 14) & 0xfff) << 16) | // Value 25-14 = Target 27-16
500  (Value & 0x3fff); // Value 13-0 = Target 13-0
501  break;
502 
503  case FK_Data_1:
504  case FK_Data_2:
505  case FK_Data_4:
506  case fixup_Hexagon_32:
507  InstMask = 0xffffffff; // Word32
508  Reloc = Value;
509  break;
510  }
511 
512  DEBUG(dbgs() << "Name=" << getFixupKindInfo(Kind).Name << "(" <<
513  (unsigned)Kind << ")\n");
514  DEBUG(uint32_t OldData = 0;
515  for (unsigned i = 0; i < NumBytes; i++)
516  OldData |= (InstAddr[i] << (i * 8)) & (0xff << (i * 8));
517  dbgs() << "\tBValue=0x"; dbgs().write_hex(Value) <<
518  ": AValue=0x"; dbgs().write_hex(FixupValue) <<
519  ": Offset=" << Offset <<
520  ": Size=" << Data.size() <<
521  ": OInst=0x"; dbgs().write_hex(OldData) <<
522  ": Reloc=0x"; dbgs().write_hex(Reloc););
523 
524  // For each byte of the fragment that the fixup touches, mask in the
525  // bits from the fixup value. The Value has been "split up" into the
526  // appropriate bitfields above.
527  for (unsigned i = 0; i < NumBytes; i++){
528  InstAddr[i] &= uint8_t(~InstMask >> (i * 8)) & 0xff; // Clear reloc bits
529  InstAddr[i] |= uint8_t(Reloc >> (i * 8)) & 0xff; // Apply new reloc
530  }
531 
532  DEBUG(uint32_t NewData = 0;
533  for (unsigned i = 0; i < NumBytes; i++)
534  NewData |= (InstAddr[i] << (i * 8)) & (0xff << (i * 8));
535  dbgs() << ": NInst=0x"; dbgs().write_hex(NewData) << "\n";);
536  }
537 
538  bool isInstRelaxable(MCInst const &HMI) const {
539  const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(*MCII, HMI);
540  bool Relaxable = false;
541  // Branches and loop-setup insns are handled as necessary by relaxation.
544  MCID.isBranch()) ||
546  MCID.isBranch()) ||
548  HMI.getOpcode() != Hexagon::C4_addipc))
549  if (HexagonMCInstrInfo::isExtendable(*MCII, HMI)) {
550  Relaxable = true;
551  MCOperand const &Operand =
554  Relaxable = false;
555  }
556 
557  return Relaxable;
558  }
559 
560  /// MayNeedRelaxation - Check whether the given instruction may need
561  /// relaxation.
562  ///
563  /// \param Inst - The instruction to test.
564  bool mayNeedRelaxation(MCInst const &Inst) const override {
565  return true;
566  }
567 
568  /// fixupNeedsRelaxation - Target specific predicate for whether a given
569  /// fixup requires the associated instruction to be relaxed.
570  bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
571  uint64_t Value,
572  const MCRelaxableFragment *DF,
573  const MCAsmLayout &Layout) const override {
574  MCInst const &MCB = DF->getInst();
576 
577  *RelaxTarget = nullptr;
578  MCInst &MCI = const_cast<MCInst &>(HexagonMCInstrInfo::instruction(
579  MCB, Fixup.getOffset() / HEXAGON_INSTR_SIZE));
580  bool Relaxable = isInstRelaxable(MCI);
581  if (Relaxable == false)
582  return false;
583  // If we cannot resolve the fixup value, it requires relaxation.
584  if (!Resolved) {
585  switch ((unsigned)Fixup.getKind()) {
587  // GetFixupCount assumes B22 won't relax
589  default:
590  return false;
591  break;
595  case fixup_Hexagon_B7_PCREL: {
597  ++relaxedCnt;
598  *RelaxTarget = &MCI;
599  setExtender(Layout.getAssembler().getContext());
600  return true;
601  } else {
602  return false;
603  }
604  break;
605  }
606  }
607  }
608 
609  MCFixupKind Kind = Fixup.getKind();
610  int64_t sValue = Value;
611  int64_t maxValue;
612 
613  switch ((unsigned)Kind) {
615  maxValue = 1 << 8;
616  break;
618  maxValue = 1 << 10;
619  break;
621  maxValue = 1 << 16;
622  break;
624  maxValue = 1 << 23;
625  break;
626  default:
627  maxValue = INT64_MAX;
628  break;
629  }
630 
631  bool isFarAway = -maxValue > sValue || sValue > maxValue - 1;
632 
633  if (isFarAway) {
635  ++relaxedCnt;
636  *RelaxTarget = &MCI;
637  setExtender(Layout.getAssembler().getContext());
638  return true;
639  }
640  }
641 
642  return false;
643  }
644 
645  /// Simple predicate for targets where !Resolved implies requiring relaxation
646  bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
647  const MCRelaxableFragment *DF,
648  const MCAsmLayout &Layout) const override {
649  llvm_unreachable("Handled by fixupNeedsRelaxationAdvanced");
650  }
651 
652  void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
653  MCInst &Res) const override {
655  "Hexagon relaxInstruction only works on bundles");
656 
658  // Copy the results into the bundle.
659  bool Update = false;
660  for (auto &I : HexagonMCInstrInfo::bundleInstructions(Inst)) {
661  MCInst &CrntHMI = const_cast<MCInst &>(*I.getInst());
662 
663  // if immediate extender needed, add it in
664  if (*RelaxTarget == &CrntHMI) {
665  Update = true;
667  "No room to insert extender for relaxation");
668 
669  MCInst *HMIx = takeExtender();
671  *MCII, CrntHMI,
674  *RelaxTarget = nullptr;
675  }
676  // now copy over the original instruction(the one we may have extended)
677  Res.addOperand(MCOperand::createInst(I.getInst()));
678  }
679  (void)Update;
680  assert(Update && "Didn't find relaxation target");
681  }
682 
683  bool writeNopData(uint64_t Count,
684  MCObjectWriter * OW) const override {
685  static const uint32_t Nopcode = 0x7f000000, // Hard-coded NOP.
686  ParseIn = 0x00004000, // In packet parse-bits.
687  ParseEnd = 0x0000c000; // End of packet parse-bits.
688 
689  while(Count % HEXAGON_INSTR_SIZE) {
690  DEBUG(dbgs() << "Alignment not a multiple of the instruction size:" <<
691  Count % HEXAGON_INSTR_SIZE << "/" << HEXAGON_INSTR_SIZE << "\n");
692  --Count;
693  OW->write8(0);
694  }
695 
696  while(Count) {
697  Count -= HEXAGON_INSTR_SIZE;
698  // Close the packet whenever a multiple of the maximum packet size remains
699  uint32_t ParseBits = (Count % (HEXAGON_PACKET_SIZE * HEXAGON_INSTR_SIZE))?
700  ParseIn: ParseEnd;
701  OW->write32(Nopcode | ParseBits);
702  }
703  return true;
704  }
705 
706  void finishLayout(MCAssembler const &Asm,
707  MCAsmLayout &Layout) const override {
708  for (auto I : Layout.getSectionOrder()) {
709  auto &Fragments = I->getFragmentList();
710  for (auto &J : Fragments) {
711  switch (J.getKind()) {
712  default:
713  break;
714  case MCFragment::FT_Align: {
715  auto Size = Asm.computeFragmentSize(Layout, J);
716  for (auto K = J.getIterator();
717  K != Fragments.begin() && Size >= HEXAGON_PACKET_SIZE;) {
718  --K;
719  switch (K->getKind()) {
720  default:
721  break;
722  case MCFragment::FT_Align: {
723  // Don't pad before other alignments
724  Size = 0;
725  break;
726  }
728  MCContext &Context = Asm.getContext();
729  auto &RF = cast<MCRelaxableFragment>(*K);
730  auto &Inst = const_cast<MCInst &>(RF.getInst());
731  while (Size > 0 && HexagonMCInstrInfo::bundleSize(Inst) < 4) {
732  MCInst *Nop = new (Context) MCInst;
733  Nop->setOpcode(Hexagon::A2_nop);
735  Size -= 4;
736  if (!HexagonMCChecker(
737  Context, *MCII, RF.getSubtargetInfo(), Inst,
738  *Context.getRegisterInfo(), false)
739  .check()) {
740  Inst.erase(Inst.end() - 1);
741  Size = 0;
742  }
743  }
744  bool Error = HexagonMCShuffle(Context, true, *MCII,
745  RF.getSubtargetInfo(), Inst);
746  //assert(!Error);
747  (void)Error;
748  ReplaceInstruction(Asm.getEmitter(), RF, Inst);
749  Layout.invalidateFragmentsFrom(&RF);
750  Size = 0; // Only look back one instruction
751  break;
752  }
753  }
754  }
755  }
756  }
757  }
758  }
759  }
760 }; // class HexagonAsmBackend
761 
762 } // namespace
763 
764 // MCAsmBackend
766  MCRegisterInfo const & /*MRI*/,
767  const Triple &TT, StringRef CPU,
768  const MCTargetOptions &Options) {
769  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
770 
771  StringRef CPUString = Hexagon_MC::selectHexagonCPU(TT, CPU);
772  return new HexagonAsmBackend(T, TT, OSABI, CPUString);
773 }
static MCInstrInfo * createMCInstrInfo()
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
Definition: Triple.h:290
iterator end()
Definition: MCInst.h:187
LLVMContext & Context
This represents an "assembler immediate".
Definition: MCValue.h:40
Describe properties that are true of each instruction in the target description file.
Definition: MCInstrDesc.h:163
T * data() const
Definition: ArrayRef.h:329
StringRef selectHexagonCPU(const Triple &TT, StringRef CPU)
bool isBundle(MCInst const &MCI)
MCCodeEmitter & getEmitter() const
Definition: MCAssembler.h:261
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:490
demanded bits
const MCSubtargetInfo & getSubtargetInfo()
Definition: MCFragment.h:272
MCInst deriveExtender(MCInstrInfo const &MCII, MCInst const &Inst, MCOperand const &MO)
MCAsmBackend * createHexagonAsmBackend(const Target &T, const MCRegisterInfo &MRI, const Triple &TT, StringRef CPU, const MCTargetOptions &Options)
MCContext & getContext() const
Definition: MCAssembler.h:257
Defines the object file and target independent interfaces used by the assembler backend to write nati...
void write8(uint8_t Value)
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:528
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
const MCInst & getInst() const
Definition: MCFragment.h:269
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
MCInst const & instruction(MCInst const &MCB, size_t Index)
Encapsulates the layout of an assembly file at a particular point in time.
Definition: MCAsmLayout.h:29
#define HEXAGON_PACKET_SIZE
Definition: Hexagon.h:33
virtual void encodeInstruction(const MCInst &Inst, raw_ostream &OS, SmallVectorImpl< MCFixup > &Fixups, const MCSubtargetInfo &STI) const =0
EncodeInstruction - Encode the given Inst to bytes on the output stream OS.
bool isBranch() const
Returns true if this is a conditional, unconditional, or indirect branch.
Definition: MCInstrDesc.h:270
void erase(iterator I)
Definition: MCInst.h:183
static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value, MCContext &Ctx)
iterator_range< Hexagon::PacketIterator > bundleInstructions(MCInstrInfo const &MCII, MCInst const &MCI)
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:59
static cl::opt< bool > DisableFixup("mno-fixup", cl::desc("Disable fixing up resolved relocations for Hexagon"))
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
SmallVectorImpl< char > & getContents()
Definition: MCFragment.h:181
uint32_t getOffset() const
Definition: MCFixup.h:95
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
MCObjectWriter * createHexagonELFObjectWriter(raw_pwrite_stream &OS, uint8_t OSABI, StringRef CPU)
const char * Name
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
static unsigned getFixupKindNumBytes(unsigned Kind)
The number of bytes the fixup may change.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
Definition: MCFragment.h:255
const MCExpr * getExpr() const
Definition: MCInst.h:96
void write32(uint32_t Value)
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291
uint64_t computeFragmentSize(const MCAsmLayout &Layout, const MCFragment &F) const
Compute the effective fragment size assuming it is laid out at the given SectionAddress and FragmentO...
int64_t getSExtValue() const
Get sign extended value.
Definition: APInt.h:1554
void invalidateFragmentsFrom(MCFragment *F)
Invalidate the fragments starting with F because it has been resized.
Definition: MCFragment.cpp:51
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:23
SmallVectorImpl< MCFixup > & getFixups()
Definition: MCFragment.h:206
llvm::SmallVectorImpl< MCSection * > & getSectionOrder()
Definition: MCAsmLayout.h:66
Definition for classes that emit Hexagon machine code from MCInsts.
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition: MathExtras.h:390
MCFixupKind getKind() const
Definition: MCFixup.h:93
A one-byte fixup.
Definition: MCFixup.h:24
unsigned short getExtendableOp(MCInstrInfo const &MCII, MCInst const &MCI)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
void setOpcode(unsigned Op)
Definition: MCInst.h:167
#define E
Definition: LargeTest.cpp:27
bool isExtendable(MCInstrInfo const &MCII, MCInst const &MCI)
A four-byte pc relative fixup.
Definition: MCFixup.h:30
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
bool HexagonMCShuffle(MCContext &Context, bool Fatal, MCInstrInfo const &MCII, MCSubtargetInfo const &STI, MCInst &)
unsigned getOpcode() const
Definition: MCInst.h:168
Target - Wrapper for Target specific information.
Class for arbitrary precision integers.
Definition: APInt.h:69
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:144
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
const MCRegisterInfo * getRegisterInfo() const
Definition: MCContext.h:285
#define I(x, y, z)
Definition: MD5.cpp:58
MCSubtargetInfo - Generic base class for all target subtargets.
MCInstrDesc const & getDesc(MCInstrInfo const &MCII, MCInst const &MCI)
size_t bundleSize(MCInst const &MCI)
Target independent information on a fixup kind.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
const unsigned Kind
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:538
#define HEXAGON_INSTR_SIZE
Definition: Hexagon.h:30
LLVM Value Representation.
Definition: Value.h:73
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:35
static const unsigned Nop
Instruction opcodes emitted via means other than CodeGen.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
Definition: Compiler.h:235
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
MCAssembler & getAssembler() const
Get the assembler object this is a layout for.
Definition: MCAsmLayout.h:51
Check for a valid bundle.
#define DEBUG(X)
Definition: Debug.h:118
void addOperand(const MCOperand &Op)
Definition: MCInst.h:177
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
unsigned getType(MCInstrInfo const &MCII, MCInst const &MCI)
Return the Hexagon ISA class for the insn.
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
bool mustNotExtend(MCExpr const &Expr)
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
MCOperand const & getExtendableOperand(MCInstrInfo const &MCII, MCInst const &MCI)
A two-byte fixup.
Definition: MCFixup.h:25
void setInst(const MCInst &Value)
Definition: MCFragment.h:270
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:173