1 //===-- MSP430AsmBackend.cpp - MSP430 Assembler 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 #include "MCTargetDesc/MSP430FixupKinds.h"
10 #include "MCTargetDesc/MSP430MCTargetDesc.h"
11 #include "llvm/ADT/APInt.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCELFObjectWriter.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCFixupKindInfo.h"
18 #include "llvm/MC/MCObjectWriter.h"
19 #include "llvm/MC/MCSubtargetInfo.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/MC/MCTargetOptions.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
28 class MSP430AsmBackend
: public MCAsmBackend
{
31 uint64_t adjustFixupValue(const MCFixup
&Fixup
, uint64_t Value
,
32 MCContext
&Ctx
) const;
35 MSP430AsmBackend(const MCSubtargetInfo
&STI
, uint8_t OSABI
)
36 : MCAsmBackend(llvm::endianness::little
), OSABI(OSABI
) {}
37 ~MSP430AsmBackend() override
= default;
39 void applyFixup(const MCAssembler
&Asm
, const MCFixup
&Fixup
,
40 const MCValue
&Target
, MutableArrayRef
<char> Data
,
41 uint64_t Value
, bool IsResolved
,
42 const MCSubtargetInfo
*STI
) const override
;
44 std::unique_ptr
<MCObjectTargetWriter
>
45 createObjectTargetWriter() const override
{
46 return createMSP430ELFObjectWriter(OSABI
);
49 bool fixupNeedsRelaxationAdvanced(const MCAssembler
&Asm
,
50 const MCFixup
&Fixup
, bool Resolved
,
52 const MCRelaxableFragment
*DF
,
53 const bool WasForced
) const override
{
57 unsigned getNumFixupKinds() const override
{
58 return MSP430::NumTargetFixupKinds
;
61 const MCFixupKindInfo
&getFixupKindInfo(MCFixupKind Kind
) const override
{
62 const static MCFixupKindInfo Infos
[MSP430::NumTargetFixupKinds
] = {
63 // This table must be in the same order of enum in MSP430FixupKinds.h.
65 // name offset bits flags
66 {"fixup_32", 0, 32, 0},
67 {"fixup_10_pcrel", 0, 10, MCFixupKindInfo::FKF_IsPCRel
},
68 {"fixup_16", 0, 16, 0},
69 {"fixup_16_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel
},
70 {"fixup_16_byte", 0, 16, 0},
71 {"fixup_16_pcrel_byte", 0, 16, MCFixupKindInfo::FKF_IsPCRel
},
72 {"fixup_2x_pcrel", 0, 10, MCFixupKindInfo::FKF_IsPCRel
},
73 {"fixup_rl_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel
},
75 {"fixup_sym_diff", 0, 32, 0},
77 static_assert((std::size(Infos
)) == MSP430::NumTargetFixupKinds
,
78 "Not all fixup kinds added to Infos array");
80 if (Kind
< FirstTargetFixupKind
)
81 return MCAsmBackend::getFixupKindInfo(Kind
);
83 return Infos
[Kind
- FirstTargetFixupKind
];
86 bool writeNopData(raw_ostream
&OS
, uint64_t Count
,
87 const MCSubtargetInfo
*STI
) const override
;
90 uint64_t MSP430AsmBackend::adjustFixupValue(const MCFixup
&Fixup
,
92 MCContext
&Ctx
) const {
93 unsigned Kind
= Fixup
.getKind();
95 case MSP430::fixup_10_pcrel
: {
97 Ctx
.reportError(Fixup
.getLoc(), "fixup value must be 2-byte aligned");
100 int16_t Offset
= Value
;
101 // Jumps are in words
103 // PC points to the next instruction so decrement by one
106 if (Offset
< -512 || Offset
> 511)
107 Ctx
.reportError(Fixup
.getLoc(), "fixup value out of range");
119 void MSP430AsmBackend::applyFixup(const MCAssembler
&Asm
, const MCFixup
&Fixup
,
120 const MCValue
&Target
,
121 MutableArrayRef
<char> Data
,
122 uint64_t Value
, bool IsResolved
,
123 const MCSubtargetInfo
*STI
) const {
124 Value
= adjustFixupValue(Fixup
, Value
, Asm
.getContext());
125 MCFixupKindInfo Info
= getFixupKindInfo(Fixup
.getKind());
127 return; // Doesn't change encoding.
129 // Shift the value into position.
130 Value
<<= Info
.TargetOffset
;
132 unsigned Offset
= Fixup
.getOffset();
133 unsigned NumBytes
= alignTo(Info
.TargetSize
+ Info
.TargetOffset
, 8) / 8;
135 assert(Offset
+ NumBytes
<= Data
.size() && "Invalid fixup offset!");
137 // For each byte of the fragment that the fixup touches, mask in the
138 // bits from the fixup value.
139 for (unsigned i
= 0; i
!= NumBytes
; ++i
) {
140 Data
[Offset
+ i
] |= uint8_t((Value
>> (i
* 8)) & 0xff);
144 bool MSP430AsmBackend::writeNopData(raw_ostream
&OS
, uint64_t Count
,
145 const MCSubtargetInfo
*STI
) const {
146 if ((Count
% 2) != 0)
149 // The canonical nop on MSP430 is mov #0, r3
150 uint64_t NopCount
= Count
/ 2;
152 OS
.write("\x03\x43", 2);
157 } // end anonymous namespace
159 MCAsmBackend
*llvm::createMSP430MCAsmBackend(const Target
&T
,
160 const MCSubtargetInfo
&STI
,
161 const MCRegisterInfo
&MRI
,
162 const MCTargetOptions
&Options
) {
163 return new MSP430AsmBackend(STI
, ELF::ELFOSABI_STANDALONE
);