1 //===-- AVRAsmBackend.cpp - AVR Asm Backend ------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the AVRAsmBackend class.
11 //===----------------------------------------------------------------------===//
13 #include "MCTargetDesc/AVRAsmBackend.h"
14 #include "MCTargetDesc/AVRFixupKinds.h"
15 #include "MCTargetDesc/AVRMCTargetDesc.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAssembler.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCDirectives.h"
21 #include "llvm/MC/MCELFObjectWriter.h"
22 #include "llvm/MC/MCFixupKindInfo.h"
23 #include "llvm/MC/MCObjectWriter.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/MC/MCValue.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
30 // FIXME: we should be doing checks to make sure asm operands
31 // are not out of bounds.
37 void signed_width(unsigned Width
, uint64_t Value
, std::string Description
,
38 const MCFixup
&Fixup
, MCContext
*Ctx
= nullptr) {
39 if (!isIntN(Width
, Value
)) {
40 std::string Diagnostic
= "out of range " + Description
;
42 int64_t Min
= minIntN(Width
);
43 int64_t Max
= maxIntN(Width
);
45 Diagnostic
+= " (expected an integer in the range " + std::to_string(Min
) +
46 " to " + std::to_string(Max
) + ")";
49 Ctx
->reportFatalError(Fixup
.getLoc(), Diagnostic
);
51 llvm_unreachable(Diagnostic
.c_str());
56 void unsigned_width(unsigned Width
, uint64_t Value
, std::string Description
,
57 const MCFixup
&Fixup
, MCContext
*Ctx
= nullptr) {
58 if (!isUIntN(Width
, Value
)) {
59 std::string Diagnostic
= "out of range " + Description
;
61 int64_t Max
= maxUIntN(Width
);
63 Diagnostic
+= " (expected an integer in the range 0 to " +
64 std::to_string(Max
) + ")";
67 Ctx
->reportFatalError(Fixup
.getLoc(), Diagnostic
);
69 llvm_unreachable(Diagnostic
.c_str());
74 /// Adjusts the value of a branch target before fixup application.
75 void adjustBranch(unsigned Size
, const MCFixup
&Fixup
, uint64_t &Value
,
76 MCContext
*Ctx
= nullptr) {
77 // We have one extra bit of precision because the value is rightshifted by
79 unsigned_width(Size
+ 1, Value
, std::string("branch target"), Fixup
, Ctx
);
81 // Rightshifts the value by one.
82 AVR::fixups::adjustBranchTarget(Value
);
85 /// Adjusts the value of a relative branch target before fixup application.
86 void adjustRelativeBranch(unsigned Size
, const MCFixup
&Fixup
, uint64_t &Value
,
87 MCContext
*Ctx
= nullptr) {
88 // We have one extra bit of precision because the value is rightshifted by
90 signed_width(Size
+ 1, Value
, std::string("branch target"), Fixup
, Ctx
);
94 // Rightshifts the value by one.
95 AVR::fixups::adjustBranchTarget(Value
);
98 /// 22-bit absolute fixup.
101 /// 1001 kkkk 010k kkkk kkkk kkkk 111k kkkk
103 /// Offset of 0 (so the result is left shifted by 3 bits before application).
104 void fixup_call(unsigned Size
, const MCFixup
&Fixup
, uint64_t &Value
,
105 MCContext
*Ctx
= nullptr) {
106 adjustBranch(Size
, Fixup
, Value
, Ctx
);
108 auto top
= Value
& (0xf00000 << 6); // the top four bits
109 auto middle
= Value
& (0x1ffff << 5); // the middle 13 bits
110 auto bottom
= Value
& 0x1f; // end bottom 5 bits
112 Value
= (top
<< 6) | (middle
<< 3) | (bottom
<< 0);
115 /// 7-bit PC-relative fixup.
118 /// 0000 00kk kkkk k000
119 /// Offset of 0 (so the result is left shifted by 3 bits before application).
120 void fixup_7_pcrel(unsigned Size
, const MCFixup
&Fixup
, uint64_t &Value
,
121 MCContext
*Ctx
= nullptr) {
122 adjustRelativeBranch(Size
, Fixup
, Value
, Ctx
);
124 // Because the value may be negative, we must mask out the sign bits
128 /// 12-bit PC-relative fixup.
129 /// Yes, the fixup is 12 bits even though the name says otherwise.
132 /// 0000 kkkk kkkk kkkk
133 /// Offset of 0 (so the result isn't left-shifted before application).
134 void fixup_13_pcrel(unsigned Size
, const MCFixup
&Fixup
, uint64_t &Value
,
135 MCContext
*Ctx
= nullptr) {
136 adjustRelativeBranch(Size
, Fixup
, Value
, Ctx
);
138 // Because the value may be negative, we must mask out the sign bits
142 /// 6-bit fixup for the immediate operand of the ADIW family of
146 /// 0000 0000 kk00 kkkk
147 void fixup_6_adiw(const MCFixup
&Fixup
, uint64_t &Value
,
148 MCContext
*Ctx
= nullptr) {
149 unsigned_width(6, Value
, std::string("immediate"), Fixup
, Ctx
);
151 Value
= ((Value
& 0x30) << 2) | (Value
& 0x0f);
154 /// 5-bit port number fixup on the SBIC family of instructions.
157 /// 0000 0000 AAAA A000
158 void fixup_port5(const MCFixup
&Fixup
, uint64_t &Value
,
159 MCContext
*Ctx
= nullptr) {
160 unsigned_width(5, Value
, std::string("port number"), Fixup
, Ctx
);
167 /// 6-bit port number fixup on the `IN` family of instructions.
170 /// 1011 0AAd dddd AAAA
171 void fixup_port6(const MCFixup
&Fixup
, uint64_t &Value
,
172 MCContext
*Ctx
= nullptr) {
173 unsigned_width(6, Value
, std::string("port number"), Fixup
, Ctx
);
175 Value
= ((Value
& 0x30) << 5) | (Value
& 0x0f);
178 /// Adjusts a program memory address.
179 /// This is a simple right-shift.
180 void pm(uint64_t &Value
) {
184 /// Fixups relating to the LDI instruction.
187 /// Adjusts a value to fix up the immediate of an `LDI Rd, K` instruction.
190 /// 0000 KKKK 0000 KKKK
191 /// Offset of 0 (so the result isn't left-shifted before application).
192 void fixup(unsigned Size
, const MCFixup
&Fixup
, uint64_t &Value
,
193 MCContext
*Ctx
= nullptr) {
194 uint64_t upper
= Value
& 0xf0;
195 uint64_t lower
= Value
& 0x0f;
197 Value
= (upper
<< 4) | lower
;
200 void neg(uint64_t &Value
) { Value
*= -1; }
202 void lo8(unsigned Size
, const MCFixup
&Fixup
, uint64_t &Value
,
203 MCContext
*Ctx
= nullptr) {
205 ldi::fixup(Size
, Fixup
, Value
, Ctx
);
208 void hi8(unsigned Size
, const MCFixup
&Fixup
, uint64_t &Value
,
209 MCContext
*Ctx
= nullptr) {
210 Value
= (Value
& 0xff00) >> 8;
211 ldi::fixup(Size
, Fixup
, Value
, Ctx
);
214 void hh8(unsigned Size
, const MCFixup
&Fixup
, uint64_t &Value
,
215 MCContext
*Ctx
= nullptr) {
216 Value
= (Value
& 0xff0000) >> 16;
217 ldi::fixup(Size
, Fixup
, Value
, Ctx
);
220 void ms8(unsigned Size
, const MCFixup
&Fixup
, uint64_t &Value
,
221 MCContext
*Ctx
= nullptr) {
222 Value
= (Value
& 0xff000000) >> 24;
223 ldi::fixup(Size
, Fixup
, Value
, Ctx
);
226 } // end of ldi namespace
227 } // end of adjust namespace
231 // Prepare value for the target space for it
232 void AVRAsmBackend::adjustFixupValue(const MCFixup
&Fixup
,
233 const MCValue
&Target
,
235 MCContext
*Ctx
) const {
236 // The size of the fixup in bits.
237 uint64_t Size
= AVRAsmBackend::getFixupKindInfo(Fixup
.getKind()).TargetSize
;
239 unsigned Kind
= Fixup
.getKind();
241 // Parsed LLVM-generated temporary labels are already
242 // adjusted for instruction size, but normal labels aren't.
244 // To handle both cases, we simply un-adjust the temporary label
245 // case so it acts like all other labels.
246 if (const MCSymbolRefExpr
*A
= Target
.getSymA()) {
247 if (A
->getSymbol().isTemporary())
253 llvm_unreachable("unhandled fixup");
254 case AVR::fixup_7_pcrel
:
255 adjust::fixup_7_pcrel(Size
, Fixup
, Value
, Ctx
);
257 case AVR::fixup_13_pcrel
:
258 adjust::fixup_13_pcrel(Size
, Fixup
, Value
, Ctx
);
260 case AVR::fixup_call
:
261 adjust::fixup_call(Size
, Fixup
, Value
, Ctx
);
264 adjust::ldi::fixup(Size
, Fixup
, Value
, Ctx
);
266 case AVR::fixup_lo8_ldi
:
267 adjust::ldi::lo8(Size
, Fixup
, Value
, Ctx
);
269 case AVR::fixup_lo8_ldi_pm
:
270 case AVR::fixup_lo8_ldi_gs
:
272 adjust::ldi::lo8(Size
, Fixup
, Value
, Ctx
);
274 case AVR::fixup_hi8_ldi
:
275 adjust::ldi::hi8(Size
, Fixup
, Value
, Ctx
);
277 case AVR::fixup_hi8_ldi_pm
:
278 case AVR::fixup_hi8_ldi_gs
:
280 adjust::ldi::hi8(Size
, Fixup
, Value
, Ctx
);
282 case AVR::fixup_hh8_ldi
:
283 case AVR::fixup_hh8_ldi_pm
:
284 if (Kind
== AVR::fixup_hh8_ldi_pm
) adjust::pm(Value
);
286 adjust::ldi::hh8(Size
, Fixup
, Value
, Ctx
);
288 case AVR::fixup_ms8_ldi
:
289 adjust::ldi::ms8(Size
, Fixup
, Value
, Ctx
);
292 case AVR::fixup_lo8_ldi_neg
:
293 case AVR::fixup_lo8_ldi_pm_neg
:
294 if (Kind
== AVR::fixup_lo8_ldi_pm_neg
) adjust::pm(Value
);
296 adjust::ldi::neg(Value
);
297 adjust::ldi::lo8(Size
, Fixup
, Value
, Ctx
);
299 case AVR::fixup_hi8_ldi_neg
:
300 case AVR::fixup_hi8_ldi_pm_neg
:
301 if (Kind
== AVR::fixup_hi8_ldi_pm_neg
) adjust::pm(Value
);
303 adjust::ldi::neg(Value
);
304 adjust::ldi::hi8(Size
, Fixup
, Value
, Ctx
);
306 case AVR::fixup_hh8_ldi_neg
:
307 case AVR::fixup_hh8_ldi_pm_neg
:
308 if (Kind
== AVR::fixup_hh8_ldi_pm_neg
) adjust::pm(Value
);
310 adjust::ldi::neg(Value
);
311 adjust::ldi::hh8(Size
, Fixup
, Value
, Ctx
);
313 case AVR::fixup_ms8_ldi_neg
:
314 adjust::ldi::neg(Value
);
315 adjust::ldi::ms8(Size
, Fixup
, Value
, Ctx
);
318 adjust::unsigned_width(16, Value
, std::string("port number"), Fixup
, Ctx
);
322 case AVR::fixup_16_pm
:
323 Value
>>= 1; // Flash addresses are always shifted.
324 adjust::unsigned_width(16, Value
, std::string("port number"), Fixup
, Ctx
);
329 case AVR::fixup_6_adiw
:
330 adjust::fixup_6_adiw(Fixup
, Value
, Ctx
);
333 case AVR::fixup_port5
:
334 adjust::fixup_port5(Fixup
, Value
, Ctx
);
337 case AVR::fixup_port6
:
338 adjust::fixup_port6(Fixup
, Value
, Ctx
);
341 // Fixups which do not require adjustments.
349 llvm_unreachable("don't know how to adjust this fixup");
354 std::unique_ptr
<MCObjectTargetWriter
>
355 AVRAsmBackend::createObjectTargetWriter() const {
356 return createAVRELFObjectWriter(MCELFObjectTargetWriter::getOSABI(OSType
));
359 void AVRAsmBackend::applyFixup(const MCAssembler
&Asm
, const MCFixup
&Fixup
,
360 const MCValue
&Target
,
361 MutableArrayRef
<char> Data
, uint64_t Value
,
363 const MCSubtargetInfo
*STI
) const {
364 adjustFixupValue(Fixup
, Target
, Value
, &Asm
.getContext());
366 return; // Doesn't change encoding.
368 MCFixupKindInfo Info
= getFixupKindInfo(Fixup
.getKind());
370 // The number of bits in the fixup mask
371 auto NumBits
= Info
.TargetSize
+ Info
.TargetOffset
;
372 auto NumBytes
= (NumBits
/ 8) + ((NumBits
% 8) == 0 ? 0 : 1);
374 // Shift the value into position.
375 Value
<<= Info
.TargetOffset
;
377 unsigned Offset
= Fixup
.getOffset();
378 assert(Offset
+ NumBytes
<= Data
.size() && "Invalid fixup offset!");
380 // For each byte of the fragment that the fixup touches, mask in the
381 // bits from the fixup value.
382 for (unsigned i
= 0; i
< NumBytes
; ++i
) {
383 uint8_t mask
= (((Value
>> (i
* 8)) & 0xff));
384 Data
[Offset
+ i
] |= mask
;
388 MCFixupKindInfo
const &AVRAsmBackend::getFixupKindInfo(MCFixupKind Kind
) const {
389 // NOTE: Many AVR fixups work on sets of non-contignous bits. We work around
390 // this by saying that the fixup is the size of the entire instruction.
391 const static MCFixupKindInfo Infos
[AVR::NumTargetFixupKinds
] = {
392 // This table *must* be in same the order of fixup_* kinds in
395 // name offset bits flags
396 {"fixup_32", 0, 32, 0},
398 {"fixup_7_pcrel", 3, 7, MCFixupKindInfo::FKF_IsPCRel
},
399 {"fixup_13_pcrel", 0, 12, MCFixupKindInfo::FKF_IsPCRel
},
401 {"fixup_16", 0, 16, 0},
402 {"fixup_16_pm", 0, 16, 0},
404 {"fixup_ldi", 0, 8, 0},
406 {"fixup_lo8_ldi", 0, 8, 0},
407 {"fixup_hi8_ldi", 0, 8, 0},
408 {"fixup_hh8_ldi", 0, 8, 0},
409 {"fixup_ms8_ldi", 0, 8, 0},
411 {"fixup_lo8_ldi_neg", 0, 8, 0},
412 {"fixup_hi8_ldi_neg", 0, 8, 0},
413 {"fixup_hh8_ldi_neg", 0, 8, 0},
414 {"fixup_ms8_ldi_neg", 0, 8, 0},
416 {"fixup_lo8_ldi_pm", 0, 8, 0},
417 {"fixup_hi8_ldi_pm", 0, 8, 0},
418 {"fixup_hh8_ldi_pm", 0, 8, 0},
420 {"fixup_lo8_ldi_pm_neg", 0, 8, 0},
421 {"fixup_hi8_ldi_pm_neg", 0, 8, 0},
422 {"fixup_hh8_ldi_pm_neg", 0, 8, 0},
424 {"fixup_call", 0, 22, 0},
426 {"fixup_6", 0, 16, 0}, // non-contiguous
427 {"fixup_6_adiw", 0, 6, 0},
429 {"fixup_lo8_ldi_gs", 0, 8, 0},
430 {"fixup_hi8_ldi_gs", 0, 8, 0},
432 {"fixup_8", 0, 8, 0},
433 {"fixup_8_lo8", 0, 8, 0},
434 {"fixup_8_hi8", 0, 8, 0},
435 {"fixup_8_hlo8", 0, 8, 0},
437 {"fixup_diff8", 0, 8, 0},
438 {"fixup_diff16", 0, 16, 0},
439 {"fixup_diff32", 0, 32, 0},
441 {"fixup_lds_sts_16", 0, 16, 0},
443 {"fixup_port6", 0, 16, 0}, // non-contiguous
444 {"fixup_port5", 3, 5, 0},
447 if (Kind
< FirstTargetFixupKind
)
448 return MCAsmBackend::getFixupKindInfo(Kind
);
450 assert(unsigned(Kind
- FirstTargetFixupKind
) < getNumFixupKinds() &&
453 return Infos
[Kind
- FirstTargetFixupKind
];
456 bool AVRAsmBackend::writeNopData(raw_ostream
&OS
, uint64_t Count
) const {
457 // If the count is not 2-byte aligned, we must be writing data into the text
458 // section (otherwise we have unaligned instructions, and thus have far
459 // bigger problems), so just write zeros instead.
460 assert((Count
% 2) == 0 && "NOP instructions must be 2 bytes");
462 OS
.write_zeros(Count
);
466 bool AVRAsmBackend::shouldForceRelocation(const MCAssembler
&Asm
,
467 const MCFixup
&Fixup
,
468 const MCValue
&Target
) {
469 switch ((unsigned) Fixup
.getKind()) {
470 default: return false;
471 // Fixups which should always be recorded as relocations.
472 case AVR::fixup_7_pcrel
:
473 case AVR::fixup_13_pcrel
:
474 case AVR::fixup_call
:
479 MCAsmBackend
*createAVRAsmBackend(const Target
&T
, const MCSubtargetInfo
&STI
,
480 const MCRegisterInfo
&MRI
,
481 const llvm::MCTargetOptions
&TO
) {
482 return new AVRAsmBackend(STI
.getTargetTriple().getOS());
485 } // end of namespace llvm