blob: 337af294eb861cb0e710355c9aa0844a2f418830 [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"
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000015#include "MCTargetDesc/HexagonMCTargetDesc.h"
Colin LeMahieu86f218e2015-05-30 18:55:47 +000016#include "MCTargetDesc/HexagonMCInstrInfo.h"
Colin LeMahieua3782da2016-04-27 21:37:44 +000017#include "MCTargetDesc/HexagonMCShuffler.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 }
Colin LeMahieu2c769202014-11-06 17:05:51 +000061public:
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000062 HexagonAsmBackend(const Target &T, const Triple &TT, uint8_t OSABI,
63 StringRef CPU) :
64 OSABI(OSABI), CPU(CPU), MCII(T.createMCInstrInfo()),
65 RelaxTarget(new MCInst *), Extender(nullptr) {}
Colin LeMahieu2c769202014-11-06 17:05:51 +000066
Colin LeMahieua6750772015-06-03 17:34:16 +000067 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
68 return createHexagonELFObjectWriter(OS, OSABI, CPU);
69 }
70
Colin LeMahieu65548942015-11-13 21:45:50 +000071 void setExtender(MCContext &Context) const {
72 if (Extender == nullptr)
73 const_cast<HexagonAsmBackend *>(this)->Extender = new (Context) MCInst;
74 }
75
76 MCInst *takeExtender() const {
77 assert(Extender != nullptr);
78 MCInst * Result = Extender;
79 const_cast<HexagonAsmBackend *>(this)->Extender = nullptr;
80 return Result;
81 }
82
Colin LeMahieua6750772015-06-03 17:34:16 +000083 unsigned getNumFixupKinds() const override {
84 return Hexagon::NumTargetFixupKinds;
85 }
86
87 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
88 const static MCFixupKindInfo Infos[Hexagon::NumTargetFixupKinds] = {
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +000089 // This table *must* be in same the order of fixup_* kinds in
90 // HexagonFixupKinds.h.
91 //
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +000092 // namei offset bits flags
93 { "fixup_Hexagon_B22_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
94 { "fixup_Hexagon_B15_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
95 { "fixup_Hexagon_B7_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
96 { "fixup_Hexagon_LO16", 0, 32, 0 },
97 { "fixup_Hexagon_HI16", 0, 32, 0 },
98 { "fixup_Hexagon_32", 0, 32, 0 },
99 { "fixup_Hexagon_16", 0, 32, 0 },
100 { "fixup_Hexagon_8", 0, 32, 0 },
101 { "fixup_Hexagon_GPREL16_0", 0, 32, 0 },
102 { "fixup_Hexagon_GPREL16_1", 0, 32, 0 },
103 { "fixup_Hexagon_GPREL16_2", 0, 32, 0 },
104 { "fixup_Hexagon_GPREL16_3", 0, 32, 0 },
105 { "fixup_Hexagon_HL16", 0, 32, 0 },
106 { "fixup_Hexagon_B13_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
107 { "fixup_Hexagon_B9_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
108 { "fixup_Hexagon_B32_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
109 { "fixup_Hexagon_32_6_X", 0, 32, 0 },
110 { "fixup_Hexagon_B22_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
111 { "fixup_Hexagon_B15_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
112 { "fixup_Hexagon_B13_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
113 { "fixup_Hexagon_B9_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
114 { "fixup_Hexagon_B7_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
115 { "fixup_Hexagon_16_X", 0, 32, 0 },
116 { "fixup_Hexagon_12_X", 0, 32, 0 },
117 { "fixup_Hexagon_11_X", 0, 32, 0 },
118 { "fixup_Hexagon_10_X", 0, 32, 0 },
119 { "fixup_Hexagon_9_X", 0, 32, 0 },
120 { "fixup_Hexagon_8_X", 0, 32, 0 },
121 { "fixup_Hexagon_7_X", 0, 32, 0 },
122 { "fixup_Hexagon_6_X", 0, 32, 0 },
123 { "fixup_Hexagon_32_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
124 { "fixup_Hexagon_COPY", 0, 32, 0 },
125 { "fixup_Hexagon_GLOB_DAT", 0, 32, 0 },
126 { "fixup_Hexagon_JMP_SLOT", 0, 32, 0 },
127 { "fixup_Hexagon_RELATIVE", 0, 32, 0 },
128 { "fixup_Hexagon_PLT_B22_PCREL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
129 { "fixup_Hexagon_GOTREL_LO16", 0, 32, 0 },
130 { "fixup_Hexagon_GOTREL_HI16", 0, 32, 0 },
131 { "fixup_Hexagon_GOTREL_32", 0, 32, 0 },
132 { "fixup_Hexagon_GOT_LO16", 0, 32, 0 },
133 { "fixup_Hexagon_GOT_HI16", 0, 32, 0 },
134 { "fixup_Hexagon_GOT_32", 0, 32, 0 },
135 { "fixup_Hexagon_GOT_16", 0, 32, 0 },
136 { "fixup_Hexagon_DTPMOD_32", 0, 32, 0 },
137 { "fixup_Hexagon_DTPREL_LO16", 0, 32, 0 },
138 { "fixup_Hexagon_DTPREL_HI16", 0, 32, 0 },
139 { "fixup_Hexagon_DTPREL_32", 0, 32, 0 },
140 { "fixup_Hexagon_DTPREL_16", 0, 32, 0 },
141 { "fixup_Hexagon_GD_PLT_B22_PCREL",0, 32, MCFixupKindInfo::FKF_IsPCRel },
142 { "fixup_Hexagon_LD_PLT_B22_PCREL",0, 32, MCFixupKindInfo::FKF_IsPCRel },
143 { "fixup_Hexagon_GD_GOT_LO16", 0, 32, 0 },
144 { "fixup_Hexagon_GD_GOT_HI16", 0, 32, 0 },
145 { "fixup_Hexagon_GD_GOT_32", 0, 32, 0 },
146 { "fixup_Hexagon_GD_GOT_16", 0, 32, 0 },
147 { "fixup_Hexagon_LD_GOT_LO16", 0, 32, 0 },
148 { "fixup_Hexagon_LD_GOT_HI16", 0, 32, 0 },
149 { "fixup_Hexagon_LD_GOT_32", 0, 32, 0 },
150 { "fixup_Hexagon_LD_GOT_16", 0, 32, 0 },
151 { "fixup_Hexagon_IE_LO16", 0, 32, 0 },
152 { "fixup_Hexagon_IE_HI16", 0, 32, 0 },
153 { "fixup_Hexagon_IE_32", 0, 32, 0 },
154 { "fixup_Hexagon_IE_16", 0, 32, 0 },
155 { "fixup_Hexagon_IE_GOT_LO16", 0, 32, 0 },
156 { "fixup_Hexagon_IE_GOT_HI16", 0, 32, 0 },
157 { "fixup_Hexagon_IE_GOT_32", 0, 32, 0 },
158 { "fixup_Hexagon_IE_GOT_16", 0, 32, 0 },
159 { "fixup_Hexagon_TPREL_LO16", 0, 32, 0 },
160 { "fixup_Hexagon_TPREL_HI16", 0, 32, 0 },
161 { "fixup_Hexagon_TPREL_32", 0, 32, 0 },
162 { "fixup_Hexagon_TPREL_16", 0, 32, 0 },
163 { "fixup_Hexagon_6_PCREL_X", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
164 { "fixup_Hexagon_GOTREL_32_6_X", 0, 32, 0 },
165 { "fixup_Hexagon_GOTREL_16_X", 0, 32, 0 },
166 { "fixup_Hexagon_GOTREL_11_X", 0, 32, 0 },
167 { "fixup_Hexagon_GOT_32_6_X", 0, 32, 0 },
168 { "fixup_Hexagon_GOT_16_X", 0, 32, 0 },
169 { "fixup_Hexagon_GOT_11_X", 0, 32, 0 },
170 { "fixup_Hexagon_DTPREL_32_6_X", 0, 32, 0 },
171 { "fixup_Hexagon_DTPREL_16_X", 0, 32, 0 },
172 { "fixup_Hexagon_DTPREL_11_X", 0, 32, 0 },
173 { "fixup_Hexagon_GD_GOT_32_6_X", 0, 32, 0 },
174 { "fixup_Hexagon_GD_GOT_16_X", 0, 32, 0 },
175 { "fixup_Hexagon_GD_GOT_11_X", 0, 32, 0 },
176 { "fixup_Hexagon_LD_GOT_32_6_X", 0, 32, 0 },
177 { "fixup_Hexagon_LD_GOT_16_X", 0, 32, 0 },
178 { "fixup_Hexagon_LD_GOT_11_X", 0, 32, 0 },
179 { "fixup_Hexagon_IE_32_6_X", 0, 32, 0 },
180 { "fixup_Hexagon_IE_16_X", 0, 32, 0 },
181 { "fixup_Hexagon_IE_GOT_32_6_X", 0, 32, 0 },
182 { "fixup_Hexagon_IE_GOT_16_X", 0, 32, 0 },
183 { "fixup_Hexagon_IE_GOT_11_X", 0, 32, 0 },
184 { "fixup_Hexagon_TPREL_32_6_X", 0, 32, 0 },
185 { "fixup_Hexagon_TPREL_16_X", 0, 32, 0 },
186 { "fixup_Hexagon_TPREL_11_X", 0, 32, 0 }
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000187 };
Colin LeMahieua6750772015-06-03 17:34:16 +0000188
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000189 if (Kind < FirstTargetFixupKind)
Colin LeMahieua6750772015-06-03 17:34:16 +0000190 return MCAsmBackend::getFixupKindInfo(Kind);
Colin LeMahieua6750772015-06-03 17:34:16 +0000191
192 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
193 "Invalid kind!");
194 return Infos[Kind - FirstTargetFixupKind];
195 }
Colin LeMahieu2c769202014-11-06 17:05:51 +0000196
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000197 /// processFixupValue - Target hook to adjust the literal value of a fixup
198 /// if necessary. IsResolved signals whether the caller believes a relocation
199 /// is needed; the target can modify the value. The default does nothing.
200 void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
201 const MCFixup &Fixup, const MCFragment *DF,
202 const MCValue &Target, uint64_t &Value,
203 bool &IsResolved) 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:
214 case fixup_Hexagon_GPREL16_0:
215 case fixup_Hexagon_GPREL16_1:
216 case fixup_Hexagon_GPREL16_2:
217 case fixup_Hexagon_GPREL16_3:
218 case fixup_Hexagon_HL16:
219 case fixup_Hexagon_32_6_X:
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:
229 case fixup_Hexagon_GLOB_DAT:
230 case fixup_Hexagon_JMP_SLOT:
231 case fixup_Hexagon_RELATIVE:
232 case fixup_Hexagon_PLT_B22_PCREL:
233 case fixup_Hexagon_GOTREL_LO16:
234 case fixup_Hexagon_GOTREL_HI16:
235 case fixup_Hexagon_GOTREL_32:
236 case fixup_Hexagon_GOT_LO16:
237 case fixup_Hexagon_GOT_HI16:
238 case fixup_Hexagon_GOT_32:
239 case fixup_Hexagon_GOT_16:
240 case fixup_Hexagon_DTPMOD_32:
241 case fixup_Hexagon_DTPREL_LO16:
242 case fixup_Hexagon_DTPREL_HI16:
243 case fixup_Hexagon_DTPREL_32:
244 case fixup_Hexagon_DTPREL_16:
245 case fixup_Hexagon_GD_PLT_B22_PCREL:
246 case fixup_Hexagon_LD_PLT_B22_PCREL:
247 case fixup_Hexagon_GD_GOT_LO16:
248 case fixup_Hexagon_GD_GOT_HI16:
249 case fixup_Hexagon_GD_GOT_32:
250 case fixup_Hexagon_GD_GOT_16:
251 case fixup_Hexagon_LD_GOT_LO16:
252 case fixup_Hexagon_LD_GOT_HI16:
253 case fixup_Hexagon_LD_GOT_32:
254 case fixup_Hexagon_LD_GOT_16:
255 case fixup_Hexagon_IE_LO16:
256 case fixup_Hexagon_IE_HI16:
257 case fixup_Hexagon_IE_32:
258 case fixup_Hexagon_IE_16:
259 case fixup_Hexagon_IE_GOT_LO16:
260 case fixup_Hexagon_IE_GOT_HI16:
261 case fixup_Hexagon_IE_GOT_32:
262 case fixup_Hexagon_IE_GOT_16:
263 case fixup_Hexagon_TPREL_LO16:
264 case fixup_Hexagon_TPREL_HI16:
265 case fixup_Hexagon_TPREL_32:
266 case fixup_Hexagon_TPREL_16:
267 case fixup_Hexagon_GOTREL_32_6_X:
268 case fixup_Hexagon_GOTREL_16_X:
269 case fixup_Hexagon_GOTREL_11_X:
270 case fixup_Hexagon_GOT_32_6_X:
271 case fixup_Hexagon_GOT_16_X:
272 case fixup_Hexagon_GOT_11_X:
273 case fixup_Hexagon_DTPREL_32_6_X:
274 case fixup_Hexagon_DTPREL_16_X:
275 case fixup_Hexagon_DTPREL_11_X:
276 case fixup_Hexagon_GD_GOT_32_6_X:
277 case fixup_Hexagon_GD_GOT_16_X:
278 case fixup_Hexagon_GD_GOT_11_X:
279 case fixup_Hexagon_LD_GOT_32_6_X:
280 case fixup_Hexagon_LD_GOT_16_X:
281 case fixup_Hexagon_LD_GOT_11_X:
282 case fixup_Hexagon_IE_32_6_X:
283 case fixup_Hexagon_IE_16_X:
284 case fixup_Hexagon_IE_GOT_32_6_X:
285 case fixup_Hexagon_IE_GOT_16_X:
286 case fixup_Hexagon_IE_GOT_11_X:
287 case fixup_Hexagon_TPREL_32_6_X:
288 case fixup_Hexagon_TPREL_16_X:
289 case fixup_Hexagon_TPREL_11_X:
290 case fixup_Hexagon_32_PCREL:
291 case fixup_Hexagon_6_PCREL_X:
292 case fixup_Hexagon_23_REG:
293 // These relocations should always have a relocation recorded
294 IsResolved = false;
295 return;
296
297 case fixup_Hexagon_B22_PCREL:
298 //IsResolved = false;
299 break;
300
301 case fixup_Hexagon_B13_PCREL:
302 case fixup_Hexagon_B13_PCREL_X:
303 case fixup_Hexagon_B32_PCREL_X:
304 case fixup_Hexagon_B22_PCREL_X:
305 case fixup_Hexagon_B15_PCREL:
306 case fixup_Hexagon_B15_PCREL_X:
307 case fixup_Hexagon_B9_PCREL:
308 case fixup_Hexagon_B9_PCREL_X:
309 case fixup_Hexagon_B7_PCREL:
310 case fixup_Hexagon_B7_PCREL_X:
311 if (DisableFixup)
312 IsResolved = false;
313 break;
314
315 case FK_Data_1:
316 case FK_Data_2:
317 case FK_Data_4:
318 case FK_PCRel_4:
319 case fixup_Hexagon_32:
320 // Leave these relocations alone as they are used for EH.
321 return;
322 }
323 }
324
325 /// getFixupKindNumBytes - The number of bytes the fixup may change.
326 static unsigned getFixupKindNumBytes(unsigned Kind) {
327 switch (Kind) {
328 default:
329 return 0;
330
331 case FK_Data_1:
332 return 1;
333 case FK_Data_2:
334 return 2;
335 case FK_Data_4: // this later gets mapped to R_HEX_32
336 case FK_PCRel_4: // this later gets mapped to R_HEX_32_PCREL
337 case fixup_Hexagon_32:
338 case fixup_Hexagon_B32_PCREL_X:
339 case fixup_Hexagon_B22_PCREL:
340 case fixup_Hexagon_B22_PCREL_X:
341 case fixup_Hexagon_B15_PCREL:
342 case fixup_Hexagon_B15_PCREL_X:
343 case fixup_Hexagon_B13_PCREL:
344 case fixup_Hexagon_B13_PCREL_X:
345 case fixup_Hexagon_B9_PCREL:
346 case fixup_Hexagon_B9_PCREL_X:
347 case fixup_Hexagon_B7_PCREL:
348 case fixup_Hexagon_B7_PCREL_X:
349 return 4;
350 }
351 }
352
353 // Make up for left shift when encoding the operand.
354 static uint64_t adjustFixupValue(MCFixupKind Kind, uint64_t Value) {
355 switch((unsigned)Kind) {
356 default:
357 break;
358
359 case fixup_Hexagon_B7_PCREL:
360 case fixup_Hexagon_B9_PCREL:
361 case fixup_Hexagon_B13_PCREL:
362 case fixup_Hexagon_B15_PCREL:
363 case fixup_Hexagon_B22_PCREL:
364 Value >>= 2;
365 break;
366
367 case fixup_Hexagon_B7_PCREL_X:
368 case fixup_Hexagon_B9_PCREL_X:
369 case fixup_Hexagon_B13_PCREL_X:
370 case fixup_Hexagon_B15_PCREL_X:
371 case fixup_Hexagon_B22_PCREL_X:
372 Value &= 0x3f;
373 break;
374
375 case fixup_Hexagon_B32_PCREL_X:
376 Value >>= 6;
377 break;
378 }
379 return (Value);
380 }
381
382 void HandleFixupError(const int bits, const int align_bits,
383 const int64_t FixupValue, const char *fixupStr) const {
384 // Error: value 1124 out of range: -1024-1023 when resolving
385 // symbol in file xprtsock.S
386 const APInt IntMin = APInt::getSignedMinValue(bits+align_bits);
387 const APInt IntMax = APInt::getSignedMaxValue(bits+align_bits);
388 std::stringstream errStr;
389 errStr << "\nError: value " <<
390 FixupValue <<
391 " out of range: " <<
392 IntMin.getSExtValue() <<
393 "-" <<
394 IntMax.getSExtValue() <<
395 " when resolving " <<
396 fixupStr <<
397 " fixup\n";
398 llvm_unreachable(errStr.str().c_str());
399 }
400
401 /// ApplyFixup - Apply the \arg Value for given \arg Fixup into the provided
402 /// data fragment, at the offset specified by the fixup and following the
403 /// fixup kind as appropriate.
404 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Alex Bradbury866113c2017-04-05 10:16:14 +0000405 uint64_t FixupValue, bool IsPCRel,
406 MCContext &Ctx) const override {
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000407
408 // When FixupValue is 0 the relocation is external and there
409 // is nothing for us to do.
410 if (!FixupValue) return;
411
412 MCFixupKind Kind = Fixup.getKind();
413 uint64_t Value;
414 uint32_t InstMask;
415 uint32_t Reloc;
416
417 // LLVM gives us an encoded value, we have to convert it back
418 // to a real offset before we can use it.
419 uint32_t Offset = Fixup.getOffset();
420 unsigned NumBytes = getFixupKindNumBytes(Kind);
421 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
David Majnemere61e4bf2016-06-21 05:10:24 +0000422 char *InstAddr = Data + Offset;
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000423
424 Value = adjustFixupValue(Kind, FixupValue);
425 if(!Value)
426 return;
David Majnemere61e4bf2016-06-21 05:10:24 +0000427 int sValue = (int)Value;
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000428
429 switch((unsigned)Kind) {
430 default:
431 return;
432
433 case fixup_Hexagon_B7_PCREL:
434 if (!(isIntN(7, sValue)))
435 HandleFixupError(7, 2, (int64_t)FixupValue, "B7_PCREL");
436 case fixup_Hexagon_B7_PCREL_X:
437 InstMask = 0x00001f18; // Word32_B7
438 Reloc = (((Value >> 2) & 0x1f) << 8) | // Value 6-2 = Target 12-8
439 ((Value & 0x3) << 3); // Value 1-0 = Target 4-3
440 break;
441
442 case fixup_Hexagon_B9_PCREL:
443 if (!(isIntN(9, sValue)))
444 HandleFixupError(9, 2, (int64_t)FixupValue, "B9_PCREL");
445 case fixup_Hexagon_B9_PCREL_X:
446 InstMask = 0x003000fe; // Word32_B9
447 Reloc = (((Value >> 7) & 0x3) << 20) | // Value 8-7 = Target 21-20
448 ((Value & 0x7f) << 1); // Value 6-0 = Target 7-1
449 break;
450
451 // Since the existing branches that use this relocation cannot be
452 // extended, they should only be fixed up if the target is within range.
453 case fixup_Hexagon_B13_PCREL:
454 if (!(isIntN(13, sValue)))
455 HandleFixupError(13, 2, (int64_t)FixupValue, "B13_PCREL");
456 case fixup_Hexagon_B13_PCREL_X:
457 InstMask = 0x00202ffe; // Word32_B13
458 Reloc = (((Value >> 12) & 0x1) << 21) | // Value 12 = Target 21
459 (((Value >> 11) & 0x1) << 13) | // Value 11 = Target 13
460 ((Value & 0x7ff) << 1); // Value 10-0 = Target 11-1
461 break;
462
463 case fixup_Hexagon_B15_PCREL:
464 if (!(isIntN(15, sValue)))
465 HandleFixupError(15, 2, (int64_t)FixupValue, "B15_PCREL");
466 case fixup_Hexagon_B15_PCREL_X:
467 InstMask = 0x00df20fe; // Word32_B15
468 Reloc = (((Value >> 13) & 0x3) << 22) | // Value 14-13 = Target 23-22
469 (((Value >> 8) & 0x1f) << 16) | // Value 12-8 = Target 20-16
470 (((Value >> 7) & 0x1) << 13) | // Value 7 = Target 13
471 ((Value & 0x7f) << 1); // Value 6-0 = Target 7-1
472 break;
473
474 case fixup_Hexagon_B22_PCREL:
475 if (!(isIntN(22, sValue)))
476 HandleFixupError(22, 2, (int64_t)FixupValue, "B22_PCREL");
477 case fixup_Hexagon_B22_PCREL_X:
478 InstMask = 0x01ff3ffe; // Word32_B22
479 Reloc = (((Value >> 13) & 0x1ff) << 16) | // Value 21-13 = Target 24-16
480 ((Value & 0x1fff) << 1); // Value 12-0 = Target 13-1
481 break;
482
483 case fixup_Hexagon_B32_PCREL_X:
484 InstMask = 0x0fff3fff; // Word32_X26
485 Reloc = (((Value >> 14) & 0xfff) << 16) | // Value 25-14 = Target 27-16
486 (Value & 0x3fff); // Value 13-0 = Target 13-0
487 break;
488
489 case FK_Data_1:
490 case FK_Data_2:
491 case FK_Data_4:
492 case fixup_Hexagon_32:
493 InstMask = 0xffffffff; // Word32
494 Reloc = Value;
495 break;
496 }
497
498 DEBUG(dbgs() << "Name=" << getFixupKindInfo(Kind).Name << "(" <<
499 (unsigned)Kind << ")\n");
500 DEBUG(uint32_t OldData = 0;
501 for (unsigned i = 0; i < NumBytes; i++)
502 OldData |= (InstAddr[i] << (i * 8)) & (0xff << (i * 8));
503 dbgs() << "\tBValue=0x"; dbgs().write_hex(Value) <<
504 ": AValue=0x"; dbgs().write_hex(FixupValue) <<
505 ": Offset=" << Offset <<
506 ": Size=" << DataSize <<
507 ": OInst=0x"; dbgs().write_hex(OldData) <<
508 ": Reloc=0x"; dbgs().write_hex(Reloc););
509
510 // For each byte of the fragment that the fixup touches, mask in the
511 // bits from the fixup value. The Value has been "split up" into the
512 // appropriate bitfields above.
513 for (unsigned i = 0; i < NumBytes; i++){
514 InstAddr[i] &= uint8_t(~InstMask >> (i * 8)) & 0xff; // Clear reloc bits
515 InstAddr[i] |= uint8_t(Reloc >> (i * 8)) & 0xff; // Apply new reloc
516 }
517
518 DEBUG(uint32_t NewData = 0;
519 for (unsigned i = 0; i < NumBytes; i++)
520 NewData |= (InstAddr[i] << (i * 8)) & (0xff << (i * 8));
521 dbgs() << ": NInst=0x"; dbgs().write_hex(NewData) << "\n";);
Colin LeMahieu2c769202014-11-06 17:05:51 +0000522 }
523
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000524 bool isInstRelaxable(MCInst const &HMI) const {
525 const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(*MCII, HMI);
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000526 bool Relaxable = false;
527 // Branches and loop-setup insns are handled as necessary by relaxation.
528 if (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeJ ||
Krzysztof Parzyszekf65b8f12017-02-02 15:03:30 +0000529 (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeCJ &&
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000530 MCID.isBranch()) ||
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000531 (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeNCJ &&
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000532 MCID.isBranch()) ||
533 (llvm::HexagonMCInstrInfo::getType(*MCII, HMI) == HexagonII::TypeCR &&
534 HMI.getOpcode() != Hexagon::C4_addipc))
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000535 if (HexagonMCInstrInfo::isExtendable(*MCII, HMI)) {
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000536 Relaxable = true;
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000537 MCOperand const &Operand =
538 HMI.getOperand(HexagonMCInstrInfo::getExtendableOp(*MCII, HMI));
539 if (HexagonMCInstrInfo::mustNotExtend(*Operand.getExpr()))
540 Relaxable = false;
541 }
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000542
543 return Relaxable;
544 }
545
546 /// MayNeedRelaxation - Check whether the given instruction may need
547 /// relaxation.
548 ///
549 /// \param Inst - The instruction to test.
Colin LeMahieub510fb32015-05-30 20:03:07 +0000550 bool mayNeedRelaxation(MCInst const &Inst) const override {
Colin LeMahieua3782da2016-04-27 21:37:44 +0000551 return true;
Colin LeMahieu2c769202014-11-06 17:05:51 +0000552 }
553
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000554 /// fixupNeedsRelaxation - Target specific predicate for whether a given
555 /// fixup requires the associated instruction to be relaxed.
556 bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
557 uint64_t Value,
558 const MCRelaxableFragment *DF,
Colin LeMahieub510fb32015-05-30 20:03:07 +0000559 const MCAsmLayout &Layout) const override {
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000560 MCInst const &MCB = DF->getInst();
561 assert(HexagonMCInstrInfo::isBundle(MCB));
562
563 *RelaxTarget = nullptr;
564 MCInst &MCI = const_cast<MCInst &>(HexagonMCInstrInfo::instruction(
565 MCB, Fixup.getOffset() / HEXAGON_INSTR_SIZE));
Krzysztof Parzyszekb14f4fd2016-03-21 20:27:17 +0000566 bool Relaxable = isInstRelaxable(MCI);
567 if (Relaxable == false)
568 return false;
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000569 // If we cannot resolve the fixup value, it requires relaxation.
570 if (!Resolved) {
571 switch ((unsigned)Fixup.getKind()) {
572 case fixup_Hexagon_B22_PCREL:
Justin Bognerb03fd122016-08-17 05:10:15 +0000573 // GetFixupCount assumes B22 won't relax
574 LLVM_FALLTHROUGH;
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000575 default:
576 return false;
577 break;
578 case fixup_Hexagon_B13_PCREL:
579 case fixup_Hexagon_B15_PCREL:
580 case fixup_Hexagon_B9_PCREL:
581 case fixup_Hexagon_B7_PCREL: {
582 if (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_SIZE) {
583 ++relaxedCnt;
584 *RelaxTarget = &MCI;
Colin LeMahieu65548942015-11-13 21:45:50 +0000585 setExtender(Layout.getAssembler().getContext());
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000586 return true;
587 } else {
588 return false;
589 }
590 break;
591 }
592 }
593 }
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000594
595 MCFixupKind Kind = Fixup.getKind();
596 int64_t sValue = Value;
597 int64_t maxValue;
598
599 switch ((unsigned)Kind) {
600 case fixup_Hexagon_B7_PCREL:
601 maxValue = 1 << 8;
602 break;
603 case fixup_Hexagon_B9_PCREL:
604 maxValue = 1 << 10;
605 break;
606 case fixup_Hexagon_B15_PCREL:
607 maxValue = 1 << 16;
608 break;
609 case fixup_Hexagon_B22_PCREL:
610 maxValue = 1 << 23;
611 break;
612 default:
613 maxValue = INT64_MAX;
614 break;
615 }
616
617 bool isFarAway = -maxValue > sValue || sValue > maxValue - 1;
618
619 if (isFarAway) {
620 if (HexagonMCInstrInfo::bundleSize(MCB) < HEXAGON_PACKET_SIZE) {
621 ++relaxedCnt;
622 *RelaxTarget = &MCI;
Colin LeMahieu65548942015-11-13 21:45:50 +0000623 setExtender(Layout.getAssembler().getContext());
Colin LeMahieu86f218e2015-05-30 18:55:47 +0000624 return true;
625 }
626 }
627
628 return false;
629 }
630
631 /// Simple predicate for targets where !Resolved implies requiring relaxation
632 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
633 const MCRelaxableFragment *DF,
634 const MCAsmLayout &Layout) const override {
635 llvm_unreachable("Handled by fixupNeedsRelaxationAdvanced");
Colin LeMahieu2c769202014-11-06 17:05:51 +0000636 }
637
Nirav Dave86030622016-07-11 14:23:53 +0000638 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
639 MCInst &Res) const override {
Colin LeMahieu8bb168b2015-11-13 01:12:25 +0000640 assert(HexagonMCInstrInfo::isBundle(Inst) &&
641 "Hexagon relaxInstruction only works on bundles");
642
Colin LeMahieuf0af6e52015-11-13 17:42:46 +0000643 Res = HexagonMCInstrInfo::createBundle();
Colin LeMahieu8bb168b2015-11-13 01:12:25 +0000644 // Copy the results into the bundle.
645 bool Update = false;
646 for (auto &I : HexagonMCInstrInfo::bundleInstructions(Inst)) {
647 MCInst &CrntHMI = const_cast<MCInst &>(*I.getInst());
648
649 // if immediate extender needed, add it in
650 if (*RelaxTarget == &CrntHMI) {
651 Update = true;
652 assert((HexagonMCInstrInfo::bundleSize(Res) < HEXAGON_PACKET_SIZE) &&
653 "No room to insert extender for relaxation");
654
Colin LeMahieu65548942015-11-13 21:45:50 +0000655 MCInst *HMIx = takeExtender();
656 *HMIx = HexagonMCInstrInfo::deriveExtender(
Colin LeMahieu8bb168b2015-11-13 01:12:25 +0000657 *MCII, CrntHMI,
Colin LeMahieu65548942015-11-13 21:45:50 +0000658 HexagonMCInstrInfo::getExtendableOperand(*MCII, CrntHMI));
Colin LeMahieu8bb168b2015-11-13 01:12:25 +0000659 Res.addOperand(MCOperand::createInst(HMIx));
660 *RelaxTarget = nullptr;
661 }
662 // now copy over the original instruction(the one we may have extended)
663 Res.addOperand(MCOperand::createInst(I.getInst()));
664 }
665 (void)Update;
666 assert(Update && "Didn't find relaxation target");
Colin LeMahieu2c769202014-11-06 17:05:51 +0000667 }
668
Colin LeMahieu1e9d1d72015-06-10 16:52:32 +0000669 bool writeNopData(uint64_t Count,
670 MCObjectWriter * OW) const override {
671 static const uint32_t Nopcode = 0x7f000000, // Hard-coded NOP.
672 ParseIn = 0x00004000, // In packet parse-bits.
673 ParseEnd = 0x0000c000; // End of packet parse-bits.
674
675 while(Count % HEXAGON_INSTR_SIZE) {
676 DEBUG(dbgs() << "Alignment not a multiple of the instruction size:" <<
677 Count % HEXAGON_INSTR_SIZE << "/" << HEXAGON_INSTR_SIZE << "\n");
678 --Count;
679 OW->write8(0);
680 }
681
682 while(Count) {
683 Count -= HEXAGON_INSTR_SIZE;
684 // Close the packet whenever a multiple of the maximum packet size remains
685 uint32_t ParseBits = (Count % (HEXAGON_PACKET_SIZE * HEXAGON_INSTR_SIZE))?
686 ParseIn: ParseEnd;
687 OW->write32(Nopcode | ParseBits);
688 }
Colin LeMahieu2c769202014-11-06 17:05:51 +0000689 return true;
690 }
Colin LeMahieua3782da2016-04-27 21:37:44 +0000691
692 void finishLayout(MCAssembler const &Asm,
693 MCAsmLayout &Layout) const override {
694 for (auto I : Layout.getSectionOrder()) {
695 auto &Fragments = I->getFragmentList();
696 for (auto &J : Fragments) {
697 switch (J.getKind()) {
698 default:
699 break;
700 case MCFragment::FT_Align: {
701 auto Size = Asm.computeFragmentSize(Layout, J);
702 for (auto K = J.getIterator();
703 K != Fragments.begin() && Size >= HEXAGON_PACKET_SIZE;) {
704 --K;
705 switch (K->getKind()) {
706 default:
707 break;
708 case MCFragment::FT_Align: {
709 // Don't pad before other alignments
710 Size = 0;
711 break;
712 }
713 case MCFragment::FT_Relaxable: {
714 auto &RF = cast<MCRelaxableFragment>(*K);
715 auto &Inst = const_cast<MCInst &>(RF.getInst());
716 while (Size > 0 && HexagonMCInstrInfo::bundleSize(Inst) < 4) {
717 MCInst *Nop = new (Asm.getContext()) MCInst;
718 Nop->setOpcode(Hexagon::A2_nop);
719 Inst.addOperand(MCOperand::createInst(Nop));
720 Size -= 4;
721 if (!HexagonMCChecker(
722 *MCII, RF.getSubtargetInfo(), Inst, Inst,
723 *Asm.getContext().getRegisterInfo()).check()) {
724 Inst.erase(Inst.end() - 1);
725 Size = 0;
726 }
727 }
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000728 bool Error = HexagonMCShuffle(true, *MCII, RF.getSubtargetInfo(),
729 Inst);
Colin LeMahieua3782da2016-04-27 21:37:44 +0000730 //assert(!Error);
731 (void)Error;
732 ReplaceInstruction(Asm.getEmitter(), RF, Inst);
733 Layout.invalidateFragmentsFrom(&RF);
734 Size = 0; // Only look back one instruction
735 break;
736 }
737 }
738 }
739 }
740 }
741 }
742 }
743 }
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000744}; // class HexagonAsmBackend
Colin LeMahieu2c769202014-11-06 17:05:51 +0000745
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000746} // namespace
747
748// MCAsmBackend
749MCAsmBackend *llvm::createHexagonAsmBackend(Target const &T,
Colin LeMahieu2c769202014-11-06 17:05:51 +0000750 MCRegisterInfo const & /*MRI*/,
Joel Jones373d7d32016-07-25 17:18:28 +0000751 const Triple &TT, StringRef CPU,
752 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000753 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
Krzysztof Parzyszek8cdfe8e2017-02-06 19:35:46 +0000754
755 StringRef CPUString = Hexagon_MC::selectHexagonCPU(TT, CPU);
756 return new HexagonAsmBackend(T, TT, OSABI, CPUString);
Colin LeMahieu2c769202014-11-06 17:05:51 +0000757}