blob: 270c3a19dcaa638c37839f6fcc2366b6ae7ffcf5 [file] [log] [blame]
Colin LeMahieu2c769202014-11-06 17:05:51 +00001//===-- 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
Colin LeMahieu86f218e2015-05-30 18:55:47 +000010#include "Hexagon.h"
11#include "HexagonFixupKinds.h"
Colin LeMahieu2c769202014-11-06 17:05:51 +000012#include "HexagonMCTargetDesc.h"
Colin LeMahieu86f218e2015-05-30 18:55:47 +000013#include "MCTargetDesc/HexagonBaseInfo.h"
14#include "MCTargetDesc/HexagonMCInstrInfo.h"
Colin LeMahieu2c769202014-11-06 17:05:51 +000015#include "llvm/MC/MCAsmBackend.h"
Colin LeMahieu86f218e2015-05-30 18:55:47 +000016#include "llvm/MC/MCAssembler.h"
Colin LeMahieu2c769202014-11-06 17:05:51 +000017#include "llvm/MC/MCELFObjectWriter.h"
18
19using namespace llvm;
Colin LeMahieu86f218e2015-05-30 18:55:47 +000020using namespace Hexagon;
Colin LeMahieu2c769202014-11-06 17:05:51 +000021
22namespace {
23
24class HexagonAsmBackend : public MCAsmBackend {
Colin LeMahieu86f218e2015-05-30 18:55:47 +000025 mutable uint64_t relaxedCnt;
26 std::unique_ptr <MCInstrInfo> MCII;
27 std::unique_ptr <MCInst *> RelaxTarget;
Colin LeMahieu2c769202014-11-06 17:05:51 +000028public:
Colin LeMahieu86f218e2015-05-30 18:55:47 +000029 HexagonAsmBackend(Target const & /*T*/) :
30 MCII (createHexagonMCInstrInfo()), RelaxTarget(new MCInst *){}
Colin LeMahieu2c769202014-11-06 17:05:51 +000031
32 unsigned getNumFixupKinds() const override { return 0; }
33
34 void applyFixup(MCFixup const & /*Fixup*/, char * /*Data*/,
35 unsigned /*DataSize*/, uint64_t /*Value*/,
36 bool /*IsPCRel*/) const override {
37 return;
38 }
39
Colin LeMahieu86f218e2015-05-30 18:55:47 +000040 bool isInstRelaxable(MCInst const &HMI) const {
41 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(*MCII, HMI);
42 assert(&MCID && "invalid instruction");
43
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 &&
48 MCID.isBranch()) ||
49 (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeCR &&
50 HMI.getOpcode() != Hexagon::C4_addipc))
51 if (HexagonMCInstrInfo::isExtendable(*MCII, HMI))
52 Relaxable = true;
53
54 return Relaxable;
55 }
56
57 /// MayNeedRelaxation - Check whether the given instruction may need
58 /// relaxation.
59 ///
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))
68 return true;
69 }
70 PreviousIsExtender = HexagonMCInstrInfo::isImmext(Inst);
71 }
Colin LeMahieu2c769202014-11-06 17:05:51 +000072 return false;
73 }
74
Colin LeMahieu86f218e2015-05-30 18:55:47 +000075 /// 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,
78 uint64_t Value,
79 const MCRelaxableFragment *DF,
80 const MCAsmLayout &Layout) const {
81 MCInst const &MCB = DF->getInst();
82 assert(HexagonMCInstrInfo::isBundle(MCB));
83
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.
88 if (!Resolved) {
89 switch ((unsigned)Fixup.getKind()) {
90 case fixup_Hexagon_B22_PCREL:
91 // GetFixupCount assumes B22 won't relax
92 // Fallthrough
93 default:
94 return false;
95 break;
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) {
101 ++relaxedCnt;
102 *RelaxTarget = &MCI;
103 return true;
104 } else {
105 return false;
106 }
107 break;
108 }
109 }
110 }
111 bool Relaxable = isInstRelaxable(MCI);
112 if (Relaxable == false)
113 return false;
114
115 MCFixupKind Kind = Fixup.getKind();
116 int64_t sValue = Value;
117 int64_t maxValue;
118
119 switch ((unsigned)Kind) {
120 case fixup_Hexagon_B7_PCREL:
121 maxValue = 1 << 8;
122 break;
123 case fixup_Hexagon_B9_PCREL:
124 maxValue = 1 << 10;
125 break;
126 case fixup_Hexagon_B15_PCREL:
127 maxValue = 1 << 16;
128 break;
129 case fixup_Hexagon_B22_PCREL:
130 maxValue = 1 << 23;
131 break;
132 default:
133 maxValue = INT64_MAX;
134 break;
135 }
136
137 bool isFarAway = -maxValue > sValue || sValue > maxValue - 1;
138
139 if (isFarAway) {
140 if (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_SIZE) {
141 ++relaxedCnt;
142 *RelaxTarget = &MCI;
143 return true;
144 }
145 }
146
147 return false;
148 }
149
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");
Colin LeMahieu2c769202014-11-06 17:05:51 +0000155 }
156
157 void relaxInstruction(MCInst const & /*Inst*/,
158 MCInst & /*Res*/) const override {
159 llvm_unreachable("relaxInstruction() unimplemented");
160 }
161
162 bool writeNopData(uint64_t /*Count*/,
163 MCObjectWriter * /*OW*/) const override {
164 return true;
165 }
166};
167} // end anonymous namespace
168
169namespace {
170class ELFHexagonAsmBackend : public HexagonAsmBackend {
171 uint8_t OSABI;
172
173public:
174 ELFHexagonAsmBackend(Target const &T, uint8_t OSABI)
175 : HexagonAsmBackend(T), OSABI(OSABI) {}
176
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000177 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
Colin LeMahieu2c769202014-11-06 17:05:51 +0000178 StringRef CPU("HexagonV4");
179 return createHexagonELFObjectWriter(OS, OSABI, CPU);
180 }
181};
182} // end anonymous namespace
183
184namespace llvm {
185MCAsmBackend *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);
190}
191}