Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/contrib/llvm-project/llvm/lib/Target/MSP430/MCTargetDesc/MSP430AsmBackend.cpp
35294 views
1
//===-- MSP430AsmBackend.cpp - MSP430 Assembler Backend -------------------===//
2
//
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
6
//
7
//===----------------------------------------------------------------------===//
8
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/MCDirectives.h"
16
#include "llvm/MC/MCELFObjectWriter.h"
17
#include "llvm/MC/MCExpr.h"
18
#include "llvm/MC/MCFixupKindInfo.h"
19
#include "llvm/MC/MCObjectWriter.h"
20
#include "llvm/MC/MCSubtargetInfo.h"
21
#include "llvm/MC/MCSymbol.h"
22
#include "llvm/MC/MCTargetOptions.h"
23
#include "llvm/Support/ErrorHandling.h"
24
#include "llvm/Support/raw_ostream.h"
25
26
using namespace llvm;
27
28
namespace {
29
class MSP430AsmBackend : public MCAsmBackend {
30
uint8_t OSABI;
31
32
uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
33
MCContext &Ctx) const;
34
35
public:
36
MSP430AsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI)
37
: MCAsmBackend(llvm::endianness::little), OSABI(OSABI) {}
38
~MSP430AsmBackend() override = default;
39
40
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
41
const MCValue &Target, MutableArrayRef<char> Data,
42
uint64_t Value, bool IsResolved,
43
const MCSubtargetInfo *STI) const override;
44
45
std::unique_ptr<MCObjectTargetWriter>
46
createObjectTargetWriter() const override {
47
return createMSP430ELFObjectWriter(OSABI);
48
}
49
50
bool fixupNeedsRelaxationAdvanced(const MCAssembler &Asm,
51
const MCFixup &Fixup, bool Resolved,
52
uint64_t Value,
53
const MCRelaxableFragment *DF,
54
const bool WasForced) const override {
55
return false;
56
}
57
58
unsigned getNumFixupKinds() const override {
59
return MSP430::NumTargetFixupKinds;
60
}
61
62
const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
63
const static MCFixupKindInfo Infos[MSP430::NumTargetFixupKinds] = {
64
// This table must be in the same order of enum in MSP430FixupKinds.h.
65
//
66
// name offset bits flags
67
{"fixup_32", 0, 32, 0},
68
{"fixup_10_pcrel", 0, 10, MCFixupKindInfo::FKF_IsPCRel},
69
{"fixup_16", 0, 16, 0},
70
{"fixup_16_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
71
{"fixup_16_byte", 0, 16, 0},
72
{"fixup_16_pcrel_byte", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
73
{"fixup_2x_pcrel", 0, 10, MCFixupKindInfo::FKF_IsPCRel},
74
{"fixup_rl_pcrel", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
75
{"fixup_8", 0, 8, 0},
76
{"fixup_sym_diff", 0, 32, 0},
77
};
78
static_assert((std::size(Infos)) == MSP430::NumTargetFixupKinds,
79
"Not all fixup kinds added to Infos array");
80
81
if (Kind < FirstTargetFixupKind)
82
return MCAsmBackend::getFixupKindInfo(Kind);
83
84
return Infos[Kind - FirstTargetFixupKind];
85
}
86
87
bool writeNopData(raw_ostream &OS, uint64_t Count,
88
const MCSubtargetInfo *STI) const override;
89
};
90
91
uint64_t MSP430AsmBackend::adjustFixupValue(const MCFixup &Fixup,
92
uint64_t Value,
93
MCContext &Ctx) const {
94
unsigned Kind = Fixup.getKind();
95
switch (Kind) {
96
case MSP430::fixup_10_pcrel: {
97
if (Value & 0x1)
98
Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
99
100
// Offset is signed
101
int16_t Offset = Value;
102
// Jumps are in words
103
Offset >>= 1;
104
// PC points to the next instruction so decrement by one
105
--Offset;
106
107
if (Offset < -512 || Offset > 511)
108
Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
109
110
// Mask 10 bits
111
Offset &= 0x3ff;
112
113
return Offset;
114
}
115
default:
116
return Value;
117
}
118
}
119
120
void MSP430AsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
121
const MCValue &Target,
122
MutableArrayRef<char> Data,
123
uint64_t Value, bool IsResolved,
124
const MCSubtargetInfo *STI) const {
125
Value = adjustFixupValue(Fixup, Value, Asm.getContext());
126
MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
127
if (!Value)
128
return; // Doesn't change encoding.
129
130
// Shift the value into position.
131
Value <<= Info.TargetOffset;
132
133
unsigned Offset = Fixup.getOffset();
134
unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
135
136
assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
137
138
// For each byte of the fragment that the fixup touches, mask in the
139
// bits from the fixup value.
140
for (unsigned i = 0; i != NumBytes; ++i) {
141
Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
142
}
143
}
144
145
bool MSP430AsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
146
const MCSubtargetInfo *STI) const {
147
if ((Count % 2) != 0)
148
return false;
149
150
// The canonical nop on MSP430 is mov #0, r3
151
uint64_t NopCount = Count / 2;
152
while (NopCount--)
153
OS.write("\x03\x43", 2);
154
155
return true;
156
}
157
158
} // end anonymous namespace
159
160
MCAsmBackend *llvm::createMSP430MCAsmBackend(const Target &T,
161
const MCSubtargetInfo &STI,
162
const MCRegisterInfo &MRI,
163
const MCTargetOptions &Options) {
164
return new MSP430AsmBackend(STI, ELF::ELFOSABI_STANDALONE);
165
}
166
167