blob: 093ce80bc2e3f4810911b7854b1a926ae9fb61e4 [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 LeMahieu86f218e2015-05-30 18:55:47 +000012#include "MCTargetDesc/HexagonBaseInfo.h"
Colin LeMahieua3782da2016-04-27 21:37:44 +000013#include "MCTargetDesc/HexagonMCChecker.h"
14#include "MCTargetDesc/HexagonMCCodeEmitter.h"
Colin LeMahieu86f218e2015-05-30 18:55:47 +000015#include "MCTargetDesc/HexagonMCInstrInfo.h"
Colin LeMahieua3782da2016-04-27 21:37:44 +000016#include "MCTargetDesc/HexagonMCShuffler.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000017#include "MCTargetDesc/HexagonMCTargetDesc.h"
Colin LeMahieu2c769202014-11-06 17:05:51 +000018#include "llvm/MC/MCAsmBackend.h"
Colin LeMahieue6241792015-11-30 17:32:34 +000019#include "llvm/MC/MCAsmLayout.h"
Colin LeMahieu86f218e2015-05-30 18:55:47 +000020#include "llvm/MC/MCAssembler.h"
Colin LeMahieu65548942015-11-13 21:45:50 +000021#include "llvm/MC/MCContext.h"
Colin LeMahieu2c769202014-11-06 17:05:51 +000022#include "llvm/MC/MCELFObjectWriter.h"
Colin LeMahieua6750772015-06-03 17:34:16 +000023#include "llvm/MC/MCFixupKindInfo.h"
Colin LeMahieube8c4532015-06-05 16:00:11 +000024#include "llvm/MC/MCInstrInfo.h"
Reid Kleckner858239d2016-06-22 23:23:08 +000025#include "llvm/MC/MCObjectWriter.h"
Colin LeMahieu1e9d1d72015-06-10 16:52:32 +000026#include "llvm/Support/Debug.h"
Colin LeMahieua6750772015-06-03 17:34:16 +000027#include "llvm/Support/TargetRegistry.h"
Colin LeMahieu2c769202014-11-06 17:05:51 +000028
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +000029#include <sstream>
30
Colin LeMahieu2c769202014-11-06 17:05:51 +000031using namespace llvm;
Colin LeMahieu86f218e2015-05-30 18:55:47 +000032using namespace Hexagon;
Colin LeMahieu2c769202014-11-06 17:05:51 +000033
Colin LeMahieu1e9d1d72015-06-10 16:52:32 +000034#define DEBUG_TYPE "hexagon-asm-backend"
35
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +000036static cl::opt<bool> DisableFixup
37 ("mno-fixup", cl::desc("Disable fixing up resolved relocations for Hexagon"));
38
Colin LeMahieu2c769202014-11-06 17:05:51 +000039namespace {
40
41class HexagonAsmBackend : public MCAsmBackend {
Colin LeMahieua6750772015-06-03 17:34:16 +000042 uint8_t OSABI;
43 StringRef CPU;
Colin LeMahieu86f218e2015-05-30 18:55:47 +000044 mutable uint64_t relaxedCnt;
45 std::unique_ptr <MCInstrInfo> MCII;
46 std::unique_ptr <MCInst *> RelaxTarget;
Colin LeMahieu65548942015-11-13 21:45:50 +000047 MCInst * Extender;
Colin LeMahieua3782da2016-04-27 21:37:44 +000048
49 void ReplaceInstruction(MCCodeEmitter &E, MCRelaxableFragment &RF,
50 MCInst &HMB) const {
51 SmallVector<MCFixup, 4> Fixups;
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 }
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +000061
Colin LeMahieu2c769202014-11-06 17:05:51 +000062public:
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000063 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) {}
Colin LeMahieu2c769202014-11-06 17:05:51 +000067
Colin LeMahieua6750772015-06-03 17:34:16 +000068 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
69 return createHexagonELFObjectWriter(OS, OSABI, CPU);
70 }
71
Colin LeMahieu65548942015-11-13 21:45:50 +000072 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
Colin LeMahieua6750772015-06-03 17:34:16 +000084 unsigned getNumFixupKinds() const override {
85 return Hexagon::NumTargetFixupKinds;
86 }
87
88 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
89 const static MCFixupKindInfo Infos[Hexagon::NumTargetFixupKinds] = {
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +000090 // This table *must* be in same the order of fixup_* kinds in
91 // HexagonFixupKinds.h.
92 //
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000093 // 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 },
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000187 { "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 }
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000192 };
Colin LeMahieua6750772015-06-03 17:34:16 +0000193
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000194 if (Kind < FirstTargetFixupKind)
Colin LeMahieua6750772015-06-03 17:34:16 +0000195 return MCAsmBackend::getFixupKindInfo(Kind);
Colin LeMahieua6750772015-06-03 17:34:16 +0000196
197 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
198 "Invalid kind!");
199 return Infos[Kind - FirstTargetFixupKind];
200 }
Colin LeMahieu2c769202014-11-06 17:05:51 +0000201
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000202 /// processFixupValue - Target hook to adjust the literal value of a fixup
203 /// if necessary. IsResolved signals whether the caller believes a relocation
204 /// is needed; the target can modify the value. The default does nothing.
Rafael Espindola64188562017-06-24 05:22:28 +0000205 void processFixupValue(const MCAssembler &Asm, const MCFixup &Fixup,
206 const MCValue &Target, bool &IsResolved) override {
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000207 MCFixupKind Kind = Fixup.getKind();
208
209 switch((unsigned)Kind) {
210 default:
211 llvm_unreachable("Unknown Fixup Kind!");
212
213 case fixup_Hexagon_LO16:
214 case fixup_Hexagon_HI16:
215 case fixup_Hexagon_16:
216 case fixup_Hexagon_8:
217 case fixup_Hexagon_GPREL16_0:
218 case fixup_Hexagon_GPREL16_1:
219 case fixup_Hexagon_GPREL16_2:
220 case fixup_Hexagon_GPREL16_3:
221 case fixup_Hexagon_HL16:
222 case fixup_Hexagon_32_6_X:
223 case fixup_Hexagon_16_X:
224 case fixup_Hexagon_12_X:
225 case fixup_Hexagon_11_X:
226 case fixup_Hexagon_10_X:
227 case fixup_Hexagon_9_X:
228 case fixup_Hexagon_8_X:
229 case fixup_Hexagon_7_X:
230 case fixup_Hexagon_6_X:
231 case fixup_Hexagon_COPY:
232 case fixup_Hexagon_GLOB_DAT:
233 case fixup_Hexagon_JMP_SLOT:
234 case fixup_Hexagon_RELATIVE:
235 case fixup_Hexagon_PLT_B22_PCREL:
236 case fixup_Hexagon_GOTREL_LO16:
237 case fixup_Hexagon_GOTREL_HI16:
238 case fixup_Hexagon_GOTREL_32:
239 case fixup_Hexagon_GOT_LO16:
240 case fixup_Hexagon_GOT_HI16:
241 case fixup_Hexagon_GOT_32:
242 case fixup_Hexagon_GOT_16:
243 case fixup_Hexagon_DTPMOD_32:
244 case fixup_Hexagon_DTPREL_LO16:
245 case fixup_Hexagon_DTPREL_HI16:
246 case fixup_Hexagon_DTPREL_32:
247 case fixup_Hexagon_DTPREL_16:
248 case fixup_Hexagon_GD_PLT_B22_PCREL:
249 case fixup_Hexagon_LD_PLT_B22_PCREL:
250 case fixup_Hexagon_GD_GOT_LO16:
251 case fixup_Hexagon_GD_GOT_HI16:
252 case fixup_Hexagon_GD_GOT_32:
253 case fixup_Hexagon_GD_GOT_16:
254 case fixup_Hexagon_LD_GOT_LO16:
255 case fixup_Hexagon_LD_GOT_HI16:
256 case fixup_Hexagon_LD_GOT_32:
257 case fixup_Hexagon_LD_GOT_16:
258 case fixup_Hexagon_IE_LO16:
259 case fixup_Hexagon_IE_HI16:
260 case fixup_Hexagon_IE_32:
261 case fixup_Hexagon_IE_16:
262 case fixup_Hexagon_IE_GOT_LO16:
263 case fixup_Hexagon_IE_GOT_HI16:
264 case fixup_Hexagon_IE_GOT_32:
265 case fixup_Hexagon_IE_GOT_16:
266 case fixup_Hexagon_TPREL_LO16:
267 case fixup_Hexagon_TPREL_HI16:
268 case fixup_Hexagon_TPREL_32:
269 case fixup_Hexagon_TPREL_16:
270 case fixup_Hexagon_GOTREL_32_6_X:
271 case fixup_Hexagon_GOTREL_16_X:
272 case fixup_Hexagon_GOTREL_11_X:
273 case fixup_Hexagon_GOT_32_6_X:
274 case fixup_Hexagon_GOT_16_X:
275 case fixup_Hexagon_GOT_11_X:
276 case fixup_Hexagon_DTPREL_32_6_X:
277 case fixup_Hexagon_DTPREL_16_X:
278 case fixup_Hexagon_DTPREL_11_X:
279 case fixup_Hexagon_GD_GOT_32_6_X:
280 case fixup_Hexagon_GD_GOT_16_X:
281 case fixup_Hexagon_GD_GOT_11_X:
282 case fixup_Hexagon_LD_GOT_32_6_X:
283 case fixup_Hexagon_LD_GOT_16_X:
284 case fixup_Hexagon_LD_GOT_11_X:
285 case fixup_Hexagon_IE_32_6_X:
286 case fixup_Hexagon_IE_16_X:
287 case fixup_Hexagon_IE_GOT_32_6_X:
288 case fixup_Hexagon_IE_GOT_16_X:
289 case fixup_Hexagon_IE_GOT_11_X:
290 case fixup_Hexagon_TPREL_32_6_X:
291 case fixup_Hexagon_TPREL_16_X:
292 case fixup_Hexagon_TPREL_11_X:
293 case fixup_Hexagon_32_PCREL:
294 case fixup_Hexagon_6_PCREL_X:
295 case fixup_Hexagon_23_REG:
Krzysztof Parzyszek57a8bb432017-05-02 18:19:11 +0000296 case fixup_Hexagon_27_REG:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000297 case fixup_Hexagon_GD_PLT_B22_PCREL_X:
298 case fixup_Hexagon_GD_PLT_B32_PCREL_X:
299 case fixup_Hexagon_LD_PLT_B22_PCREL_X:
300 case fixup_Hexagon_LD_PLT_B32_PCREL_X:
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000301 // These relocations should always have a relocation recorded
302 IsResolved = false;
303 return;
304
305 case fixup_Hexagon_B22_PCREL:
306 //IsResolved = false;
307 break;
308
309 case fixup_Hexagon_B13_PCREL:
310 case fixup_Hexagon_B13_PCREL_X:
311 case fixup_Hexagon_B32_PCREL_X:
312 case fixup_Hexagon_B22_PCREL_X:
313 case fixup_Hexagon_B15_PCREL:
314 case fixup_Hexagon_B15_PCREL_X:
315 case fixup_Hexagon_B9_PCREL:
316 case fixup_Hexagon_B9_PCREL_X:
317 case fixup_Hexagon_B7_PCREL:
318 case fixup_Hexagon_B7_PCREL_X:
319 if (DisableFixup)
320 IsResolved = false;
321 break;
322
323 case FK_Data_1:
324 case FK_Data_2:
325 case FK_Data_4:
326 case FK_PCRel_4:
327 case fixup_Hexagon_32:
328 // Leave these relocations alone as they are used for EH.
329 return;
330 }
331 }
332
333 /// getFixupKindNumBytes - The number of bytes the fixup may change.
334 static unsigned getFixupKindNumBytes(unsigned Kind) {
335 switch (Kind) {
336 default:
337 return 0;
338
339 case FK_Data_1:
340 return 1;
341 case FK_Data_2:
342 return 2;
343 case FK_Data_4: // this later gets mapped to R_HEX_32
344 case FK_PCRel_4: // this later gets mapped to R_HEX_32_PCREL
345 case fixup_Hexagon_32:
346 case fixup_Hexagon_B32_PCREL_X:
347 case fixup_Hexagon_B22_PCREL:
348 case fixup_Hexagon_B22_PCREL_X:
349 case fixup_Hexagon_B15_PCREL:
350 case fixup_Hexagon_B15_PCREL_X:
351 case fixup_Hexagon_B13_PCREL:
352 case fixup_Hexagon_B13_PCREL_X:
353 case fixup_Hexagon_B9_PCREL:
354 case fixup_Hexagon_B9_PCREL_X:
355 case fixup_Hexagon_B7_PCREL:
356 case fixup_Hexagon_B7_PCREL_X:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000357 case fixup_Hexagon_GD_PLT_B32_PCREL_X:
358 case fixup_Hexagon_LD_PLT_B32_PCREL_X:
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000359 return 4;
360 }
361 }
362
363 // Make up for left shift when encoding the operand.
364 static uint64_t adjustFixupValue(MCFixupKind Kind, uint64_t Value) {
365 switch((unsigned)Kind) {
366 default:
367 break;
368
369 case fixup_Hexagon_B7_PCREL:
370 case fixup_Hexagon_B9_PCREL:
371 case fixup_Hexagon_B13_PCREL:
372 case fixup_Hexagon_B15_PCREL:
373 case fixup_Hexagon_B22_PCREL:
374 Value >>= 2;
375 break;
376
377 case fixup_Hexagon_B7_PCREL_X:
378 case fixup_Hexagon_B9_PCREL_X:
379 case fixup_Hexagon_B13_PCREL_X:
380 case fixup_Hexagon_B15_PCREL_X:
381 case fixup_Hexagon_B22_PCREL_X:
382 Value &= 0x3f;
383 break;
384
385 case fixup_Hexagon_B32_PCREL_X:
Krzysztof Parzyszeka7503832017-05-02 18:15:33 +0000386 case fixup_Hexagon_GD_PLT_B32_PCREL_X:
387 case fixup_Hexagon_LD_PLT_B32_PCREL_X:
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000388 Value >>= 6;
389 break;
390 }
391 return (Value);
392 }
393
394 void HandleFixupError(const int bits, const int align_bits,
395 const int64_t FixupValue, const char *fixupStr) const {
396 // Error: value 1124 out of range: -1024-1023 when resolving
397 // symbol in file xprtsock.S
398 const APInt IntMin = APInt::getSignedMinValue(bits+align_bits);
399 const APInt IntMax = APInt::getSignedMaxValue(bits+align_bits);
400 std::stringstream errStr;
401 errStr << "\nError: value " <<
402 FixupValue <<
403 " out of range: " <<
404 IntMin.getSExtValue() <<
405 "-" <<
406 IntMax.getSExtValue() <<
407 " when resolving " <<
408 fixupStr <<
409 " fixup\n";
410 llvm_unreachable(errStr.str().c_str());
411 }
412
413 /// ApplyFixup - Apply the \arg Value for given \arg Fixup into the provided
414 /// data fragment, at the offset specified by the fixup and following the
415 /// fixup kind as appropriate.
Rafael Espindola801b42d2017-06-23 22:52:36 +0000416 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
417 const MCValue &Target, MutableArrayRef<char> Data,
Rafael Espindolaf3512922017-06-24 00:26:57 +0000418 uint64_t FixupValue, bool IsPCRel) const override {
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000419
420 // When FixupValue is 0 the relocation is external and there
421 // is nothing for us to do.
422 if (!FixupValue) return;
423
424 MCFixupKind Kind = Fixup.getKind();
425 uint64_t Value;
426 uint32_t InstMask;
427 uint32_t Reloc;
428
429 // LLVM gives us an encoded value, we have to convert it back
430 // to a real offset before we can use it.
431 uint32_t Offset = Fixup.getOffset();
432 unsigned NumBytes = getFixupKindNumBytes(Kind);
Rafael Espindola88d9e372017-06-21 23:06:53 +0000433 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
434 char *InstAddr = Data.data() + Offset;
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000435
436 Value = adjustFixupValue(Kind, FixupValue);
437 if(!Value)
438 return;
David Majnemere61e4bf2016-06-21 05:10:24 +0000439 int sValue = (int)Value;
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000440
441 switch((unsigned)Kind) {
442 default:
443 return;
444
445 case fixup_Hexagon_B7_PCREL:
446 if (!(isIntN(7, sValue)))
447 HandleFixupError(7, 2, (int64_t)FixupValue, "B7_PCREL");
448 case fixup_Hexagon_B7_PCREL_X:
449 InstMask = 0x00001f18; // Word32_B7
450 Reloc = (((Value >> 2) & 0x1f) << 8) | // Value 6-2 = Target 12-8
451 ((Value & 0x3) << 3); // Value 1-0 = Target 4-3
452 break;
453
454 case fixup_Hexagon_B9_PCREL:
455 if (!(isIntN(9, sValue)))
456 HandleFixupError(9, 2, (int64_t)FixupValue, "B9_PCREL");
457 case fixup_Hexagon_B9_PCREL_X:
458 InstMask = 0x003000fe; // Word32_B9
459 Reloc = (((Value >> 7) & 0x3) << 20) | // Value 8-7 = Target 21-20
460 ((Value & 0x7f) << 1); // Value 6-0 = Target 7-1
461 break;
462
463 // Since the existing branches that use this relocation cannot be
464 // extended, they should only be fixed up if the target is within range.
465 case fixup_Hexagon_B13_PCREL:
466 if (!(isIntN(13, sValue)))
467 HandleFixupError(13, 2, (int64_t)FixupValue, "B13_PCREL");
468 case fixup_Hexagon_B13_PCREL_X:
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
475 case fixup_Hexagon_B15_PCREL:
476 if (!(isIntN(15, sValue)))
477 HandleFixupError(15, 2, (int64_t)FixupValue, "B15_PCREL");
478 case fixup_Hexagon_B15_PCREL_X:
479 InstMask = 0x00df20fe; // Word32_B15
480 Reloc = (((Value >> 13) & 0x3) << 22) | // Value 14-13 = Target 23-22
481 (((Value >> 8) & 0x1f) << 16) | // Value 12-8 = Target 20-16
482 (((Value >> 7) & 0x1) << 13) | // Value 7 = Target 13
483 ((Value & 0x7f) << 1); // Value 6-0 = Target 7-1
484 break;
485
486 case fixup_Hexagon_B22_PCREL:
487 if (!(isIntN(22, sValue)))
488 HandleFixupError(22, 2, (int64_t)FixupValue, "B22_PCREL");
489 case fixup_Hexagon_B22_PCREL_X:
490 InstMask = 0x01ff3ffe; // Word32_B22
491 Reloc = (((Value >> 13) & 0x1ff) << 16) | // Value 21-13 = Target 24-16
492 ((Value & 0x1fff) << 1); // Value 12-0 = Target 13-1
493 break;
494
495 case fixup_Hexagon_B32_PCREL_X:
496 InstMask = 0x0fff3fff; // Word32_X26
497 Reloc = (((Value >> 14) & 0xfff) << 16) | // Value 25-14 = Target 27-16
498 (Value & 0x3fff); // Value 13-0 = Target 13-0
499 break;
500
501 case FK_Data_1:
502 case FK_Data_2:
503 case FK_Data_4:
504 case fixup_Hexagon_32:
505 InstMask = 0xffffffff; // Word32
506 Reloc = Value;
507 break;
508 }
509
510 DEBUG(dbgs() << "Name=" << getFixupKindInfo(Kind).Name << "(" <<
511 (unsigned)Kind << ")\n");
512 DEBUG(uint32_t OldData = 0;
513 for (unsigned i = 0; i < NumBytes; i++)
514 OldData |= (InstAddr[i] << (i * 8)) & (0xff << (i * 8));
515 dbgs() << "\tBValue=0x"; dbgs().write_hex(Value) <<
516 ": AValue=0x"; dbgs().write_hex(FixupValue) <<
517 ": Offset=" << Offset <<
Rafael Espindola88d9e372017-06-21 23:06:53 +0000518 ": Size=" << Data.size() <<
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000519 ": OInst=0x"; dbgs().write_hex(OldData) <<
520 ": Reloc=0x"; dbgs().write_hex(Reloc););
521
522 // For each byte of the fragment that the fixup touches, mask in the
523 // bits from the fixup value. The Value has been "split up" into the
524 // appropriate bitfields above.
525 for (unsigned i = 0; i < NumBytes; i++){
526 InstAddr[i] &= uint8_t(~InstMask >> (i * 8)) & 0xff; // Clear reloc bits
527 InstAddr[i] |= uint8_t(Reloc >> (i * 8)) & 0xff; // Apply new reloc
528 }
529
530 DEBUG(uint32_t NewData = 0;
531 for (unsigned i = 0; i < NumBytes; i++)
532 NewData |= (InstAddr[i] << (i * 8)) & (0xff << (i * 8));
533 dbgs() << ": NInst=0x"; dbgs().write_hex(NewData) << "\n";);
Colin LeMahieu2c769202014-11-06 17:05:51 +0000534 }
535
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000536 bool isInstRelaxable(MCInst const &HMI) const {
537 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(*MCII, HMI);
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000538 bool Relaxable = false;
539 // Branches and loop-setup insns are handled as necessary by relaxation.
540 if (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeJ ||
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +0000541 (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeCJ &&
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000542 MCID.isBranch()) ||
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000543 (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeNCJ &&
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000544 MCID.isBranch()) ||
545 (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeCR &&
546 HMI.getOpcode() != Hexagon::C4_addipc))
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000547 if (HexagonMCInstrInfo::isExtendable(*MCII, HMI)) {
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000548 Relaxable = true;
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000549 MCOperand const &Operand =
550 HMI.getOperand(HexagonMCInstrInfo::getExtendableOp(*MCII, HMI));
551 if (HexagonMCInstrInfo::mustNotExtend(*Operand.getExpr()))
552 Relaxable = false;
553 }
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000554
555 return Relaxable;
556 }
557
558 /// MayNeedRelaxation - Check whether the given instruction may need
559 /// relaxation.
560 ///
561 /// \param Inst - The instruction to test.
Colin LeMahieub510fb32015-05-30 20:03:07 +0000562 bool mayNeedRelaxation(MCInst const &Inst) const override {
Colin LeMahieua3782da2016-04-27 21:37:44 +0000563 return true;
Colin LeMahieu2c769202014-11-06 17:05:51 +0000564 }
565
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000566 /// fixupNeedsRelaxation - Target specific predicate for whether a given
567 /// fixup requires the associated instruction to be relaxed.
568 bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
569 uint64_t Value,
570 const MCRelaxableFragment *DF,
Colin LeMahieub510fb32015-05-30 20:03:07 +0000571 const MCAsmLayout &Layout) const override {
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000572 MCInst const &MCB = DF->getInst();
573 assert(HexagonMCInstrInfo::isBundle(MCB));
574
575 *RelaxTarget = nullptr;
576 MCInst &MCI = const_cast<MCInst &>(HexagonMCInstrInfo::instruction(
577 MCB, Fixup.getOffset() / HEXAGON_INSTR_SIZE));
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000578 bool Relaxable = isInstRelaxable(MCI);
579 if (Relaxable == false)
580 return false;
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000581 // If we cannot resolve the fixup value, it requires relaxation.
582 if (!Resolved) {
583 switch ((unsigned)Fixup.getKind()) {
584 case fixup_Hexagon_B22_PCREL:
Justin Bognerb03fd122016-08-17 05:10:15 +0000585 // GetFixupCount assumes B22 won't relax
586 LLVM_FALLTHROUGH;
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000587 default:
588 return false;
589 break;
590 case fixup_Hexagon_B13_PCREL:
591 case fixup_Hexagon_B15_PCREL:
592 case fixup_Hexagon_B9_PCREL:
593 case fixup_Hexagon_B7_PCREL: {
594 if (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_SIZE) {
595 ++relaxedCnt;
596 *RelaxTarget = &MCI;
Colin LeMahieu65548942015-11-13 21:45:50 +0000597 setExtender(Layout.getAssembler().getContext());
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000598 return true;
599 } else {
600 return false;
601 }
602 break;
603 }
604 }
605 }
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000606
607 MCFixupKind Kind = Fixup.getKind();
608 int64_t sValue = Value;
609 int64_t maxValue;
610
611 switch ((unsigned)Kind) {
612 case fixup_Hexagon_B7_PCREL:
613 maxValue = 1 << 8;
614 break;
615 case fixup_Hexagon_B9_PCREL:
616 maxValue = 1 << 10;
617 break;
618 case fixup_Hexagon_B15_PCREL:
619 maxValue = 1 << 16;
620 break;
621 case fixup_Hexagon_B22_PCREL:
622 maxValue = 1 << 23;
623 break;
624 default:
625 maxValue = INT64_MAX;
626 break;
627 }
628
629 bool isFarAway = -maxValue > sValue || sValue > maxValue - 1;
630
631 if (isFarAway) {
632 if (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_SIZE) {
633 ++relaxedCnt;
634 *RelaxTarget = &MCI;
Colin LeMahieu65548942015-11-13 21:45:50 +0000635 setExtender(Layout.getAssembler().getContext());
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000636 return true;
637 }
638 }
639
640 return false;
641 }
642
643 /// Simple predicate for targets where !Resolved implies requiring relaxation
644 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
645 const MCRelaxableFragment *DF,
646 const MCAsmLayout &Layout) const override {
647 llvm_unreachable("Handled by fixupNeedsRelaxationAdvanced");
Colin LeMahieu2c769202014-11-06 17:05:51 +0000648 }
649
Nirav Dave86030622016-07-11 14:23:53 +0000650 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
651 MCInst &Res) const override {
Colin LeMahieu8bb168b2015-11-13 01:12:25 +0000652 assert(HexagonMCInstrInfo::isBundle(Inst) &&
653 "Hexagon relaxInstruction only works on bundles");
654
Colin LeMahieuf0af6e52015-11-13 17:42:46 +0000655 Res = HexagonMCInstrInfo::createBundle();
Colin LeMahieu8bb168b2015-11-13 01:12:25 +0000656 // Copy the results into the bundle.
657 bool Update = false;
658 for (auto &I : HexagonMCInstrInfo::bundleInstructions(Inst)) {
659 MCInst &CrntHMI = const_cast<MCInst &>(*I.getInst());
660
661 // if immediate extender needed, add it in
662 if (*RelaxTarget == &CrntHMI) {
663 Update = true;
664 assert((HexagonMCInstrInfo::bundleSize(Res) < HEXAGON_PACKET_SIZE) &&
665 "No room to insert extender for relaxation");
666
Colin LeMahieu65548942015-11-13 21:45:50 +0000667 MCInst *HMIx = takeExtender();
668 *HMIx = HexagonMCInstrInfo::deriveExtender(
Colin LeMahieu8bb168b2015-11-13 01:12:25 +0000669 *MCII, CrntHMI,
Colin LeMahieu65548942015-11-13 21:45:50 +0000670 HexagonMCInstrInfo::getExtendableOperand(*MCII, CrntHMI));
Colin LeMahieu8bb168b2015-11-13 01:12:25 +0000671 Res.addOperand(MCOperand::createInst(HMIx));
672 *RelaxTarget = nullptr;
673 }
674 // now copy over the original instruction(the one we may have extended)
675 Res.addOperand(MCOperand::createInst(I.getInst()));
676 }
677 (void)Update;
678 assert(Update && "Didn't find relaxation target");
Colin LeMahieu2c769202014-11-06 17:05:51 +0000679 }
680
Colin LeMahieu1e9d1d72015-06-10 16:52:32 +0000681 bool writeNopData(uint64_t Count,
682 MCObjectWriter * OW) const override {
683 static const uint32_t Nopcode = 0x7f000000, // Hard-coded NOP.
684 ParseIn = 0x00004000, // In packet parse-bits.
685 ParseEnd = 0x0000c000; // End of packet parse-bits.
686
687 while(Count % HEXAGON_INSTR_SIZE) {
688 DEBUG(dbgs() << "Alignment not a multiple of the instruction size:" <<
689 Count % HEXAGON_INSTR_SIZE << "/" << HEXAGON_INSTR_SIZE << "\n");
690 --Count;
691 OW->write8(0);
692 }
693
694 while(Count) {
695 Count -= HEXAGON_INSTR_SIZE;
696 // Close the packet whenever a multiple of the maximum packet size remains
697 uint32_t ParseBits = (Count % (HEXAGON_PACKET_SIZE * HEXAGON_INSTR_SIZE))?
698 ParseIn: ParseEnd;
699 OW->write32(Nopcode | ParseBits);
700 }
Colin LeMahieu2c769202014-11-06 17:05:51 +0000701 return true;
702 }
Colin LeMahieua3782da2016-04-27 21:37:44 +0000703
704 void finishLayout(MCAssembler const &Asm,
705 MCAsmLayout &Layout) const override {
706 for (auto I : Layout.getSectionOrder()) {
707 auto &Fragments = I->getFragmentList();
708 for (auto &J : Fragments) {
709 switch (J.getKind()) {
710 default:
711 break;
712 case MCFragment::FT_Align: {
713 auto Size = Asm.computeFragmentSize(Layout, J);
714 for (auto K = J.getIterator();
715 K != Fragments.begin() && Size >= HEXAGON_PACKET_SIZE;) {
716 --K;
717 switch (K->getKind()) {
718 default:
719 break;
720 case MCFragment::FT_Align: {
721 // Don't pad before other alignments
722 Size = 0;
723 break;
724 }
725 case MCFragment::FT_Relaxable: {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000726 MCContext &Context = Asm.getContext();
Colin LeMahieua3782da2016-04-27 21:37:44 +0000727 auto &RF = cast<MCRelaxableFragment>(*K);
728 auto &Inst = const_cast<MCInst &>(RF.getInst());
729 while (Size > 0 && HexagonMCInstrInfo::bundleSize(Inst) < 4) {
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000730 MCInst *Nop = new (Context) MCInst;
Colin LeMahieua3782da2016-04-27 21:37:44 +0000731 Nop->setOpcode(Hexagon::A2_nop);
732 Inst.addOperand(MCOperand::createInst(Nop));
733 Size -= 4;
734 if (!HexagonMCChecker(
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000735 Context, *MCII, RF.getSubtargetInfo(), Inst,
736 *Context.getRegisterInfo(), false)
737 .check()) {
Colin LeMahieua3782da2016-04-27 21:37:44 +0000738 Inst.erase(Inst.end() - 1);
739 Size = 0;
740 }
741 }
Krzysztof Parzyszeke12d1e72017-05-01 19:41:43 +0000742 bool Error = HexagonMCShuffle(Context, true, *MCII,
743 RF.getSubtargetInfo(), Inst);
Colin LeMahieua3782da2016-04-27 21:37:44 +0000744 //assert(!Error);
745 (void)Error;
746 ReplaceInstruction(Asm.getEmitter(), RF, Inst);
747 Layout.invalidateFragmentsFrom(&RF);
748 Size = 0; // Only look back one instruction
749 break;
750 }
751 }
752 }
753 }
754 }
755 }
756 }
757 }
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000758}; // class HexagonAsmBackend
Colin LeMahieu2c769202014-11-06 17:05:51 +0000759
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000760} // namespace
761
762// MCAsmBackend
763MCAsmBackend *llvm::createHexagonAsmBackend(Target const &T,
Colin LeMahieu2c769202014-11-06 17:05:51 +0000764 MCRegisterInfo const & /*MRI*/,
Joel Jones373d7d32016-07-25 17:18:28 +0000765 const Triple &TT, StringRef CPU,
766 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000767 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000768
769 StringRef CPUString = Hexagon_MC::selectHexagonCPU(TT, CPU);
770 return new HexagonAsmBackend(T, TT, OSABI, CPUString);
Colin LeMahieu2c769202014-11-06 17:05:51 +0000771}