1 //===-- HexagonAsmBackend.cpp - Hexagon Assembler Backend -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "HexagonFixupKinds.h"
12 #include "HexagonMCTargetDesc.h"
13 #include "MCTargetDesc/HexagonBaseInfo.h"
14 #include "MCTargetDesc/HexagonMCInstrInfo.h"
15 #include "llvm/MC/MCAsmBackend.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCELFObjectWriter.h"
20 using namespace Hexagon;
24 class HexagonAsmBackend : public MCAsmBackend {
25 mutable uint64_t relaxedCnt;
26 std::unique_ptr <MCInstrInfo> MCII;
27 std::unique_ptr <MCInst *> RelaxTarget;
29 HexagonAsmBackend(Target const & /*T*/) :
30 MCII (createHexagonMCInstrInfo()), RelaxTarget(new MCInst *){}
32 unsigned getNumFixupKinds() const override { return 0; }
34 void applyFixup(MCFixup const & /*Fixup*/, char * /*Data*/,
35 unsigned /*DataSize*/, uint64_t /*Value*/,
36 bool /*IsPCRel*/) const override {
40 bool isInstRelaxable(MCInst const &HMI) const {
41 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(*MCII, HMI);
42 assert(&MCID && "invalid instruction");
44 bool Relaxable = false;
45 // Branches and loop-setup insns are handled as necessary by relaxation.
46 if (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeJ ||
47 (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeNV &&
49 (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeCR &&
50 HMI.getOpcode() != Hexagon::C4_addipc))
51 if (HexagonMCInstrInfo::isExtendable(*MCII, HMI))
57 /// MayNeedRelaxation - Check whether the given instruction may need
60 /// \param Inst - The instruction to test.
61 bool mayNeedRelaxation(MCInst const &Inst) const {
62 assert(HexagonMCInstrInfo::isBundle(Inst));
63 bool PreviousIsExtender = false;
64 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(Inst)) {
65 auto const &Inst = *I.getInst();
66 if (!PreviousIsExtender) {
67 if (isInstRelaxable(Inst))
70 PreviousIsExtender = HexagonMCInstrInfo::isImmext(Inst);
75 /// fixupNeedsRelaxation - Target specific predicate for whether a given
76 /// fixup requires the associated instruction to be relaxed.
77 bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
79 const MCRelaxableFragment *DF,
80 const MCAsmLayout &Layout) const {
81 MCInst const &MCB = DF->getInst();
82 assert(HexagonMCInstrInfo::isBundle(MCB));
84 *RelaxTarget = nullptr;
85 MCInst &MCI = const_cast<MCInst &>(HexagonMCInstrInfo::instruction(
86 MCB, Fixup.getOffset() / HEXAGON_INSTR_SIZE));
87 // If we cannot resolve the fixup value, it requires relaxation.
89 switch ((unsigned)Fixup.getKind()) {
90 case fixup_Hexagon_B22_PCREL:
91 // GetFixupCount assumes B22 won't relax
96 case fixup_Hexagon_B13_PCREL:
97 case fixup_Hexagon_B15_PCREL:
98 case fixup_Hexagon_B9_PCREL:
99 case fixup_Hexagon_B7_PCREL: {
100 if (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_SIZE) {
111 bool Relaxable = isInstRelaxable(MCI);
112 if (Relaxable == false)
115 MCFixupKind Kind = Fixup.getKind();
116 int64_t sValue = Value;
119 switch ((unsigned)Kind) {
120 case fixup_Hexagon_B7_PCREL:
123 case fixup_Hexagon_B9_PCREL:
126 case fixup_Hexagon_B15_PCREL:
129 case fixup_Hexagon_B22_PCREL:
133 maxValue = INT64_MAX;
137 bool isFarAway = -maxValue > sValue || sValue > maxValue - 1;
140 if (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_SIZE) {
150 /// Simple predicate for targets where !Resolved implies requiring relaxation
151 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
152 const MCRelaxableFragment *DF,
153 const MCAsmLayout &Layout) const override {
154 llvm_unreachable("Handled by fixupNeedsRelaxationAdvanced");
157 void relaxInstruction(MCInst const & /*Inst*/,
158 MCInst & /*Res*/) const override {
159 llvm_unreachable("relaxInstruction() unimplemented");
162 bool writeNopData(uint64_t /*Count*/,
163 MCObjectWriter * /*OW*/) const override {
167 } // end anonymous namespace
170 class ELFHexagonAsmBackend : public HexagonAsmBackend {
174 ELFHexagonAsmBackend(Target const &T, uint8_t OSABI)
175 : HexagonAsmBackend(T), OSABI(OSABI) {}
177 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
178 StringRef CPU("HexagonV4");
179 return createHexagonELFObjectWriter(OS, OSABI, CPU);
182 } // end anonymous namespace
185 MCAsmBackend *createHexagonAsmBackend(Target const &T,
186 MCRegisterInfo const & /*MRI*/,
187 StringRef TT, StringRef /*CPU*/) {
188 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
189 return new ELFHexagonAsmBackend(T, OSABI);