Thanks to visit codestin.com
Credit goes to llvm.org

LLVM 22.0.0git
MSP430AsmBackend.cpp
Go to the documentation of this file.
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
11#include "llvm/ADT/APInt.h"
13#include "llvm/MC/MCAssembler.h"
14#include "llvm/MC/MCContext.h"
16#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCSymbol.h"
23
24using namespace llvm;
25
26namespace {
27class MSP430AsmBackend : public MCAsmBackend {
28 uint8_t OSABI;
29
30 uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
31 MCContext &Ctx) const;
32
33public:
34 MSP430AsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI)
35 : MCAsmBackend(llvm::endianness::little), OSABI(OSABI) {}
36 ~MSP430AsmBackend() override = default;
37
38 void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
39 uint8_t *Data, uint64_t Value, bool IsResolved) override;
40
41 std::unique_ptr<MCObjectTargetWriter>
42 createObjectTargetWriter() const override {
43 return createMSP430ELFObjectWriter(OSABI);
44 }
45
46 MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const override {
47 // clang-format off
48 const static MCFixupKindInfo Infos[MSP430::NumTargetFixupKinds] = {
49 // This table must be in the same order of enum in MSP430FixupKinds.h.
50 //
51 // name offset bits flags
52 {"fixup_32", 0, 32, 0},
53 {"fixup_10_pcrel", 0, 10, 0},
54 {"fixup_16", 0, 16, 0},
55 {"fixup_16_pcrel", 0, 16, 0},
56 {"fixup_16_byte", 0, 16, 0},
57 {"fixup_16_pcrel_byte", 0, 16, 0},
58 {"fixup_2x_pcrel", 0, 10, 0},
59 {"fixup_rl_pcrel", 0, 16, 0},
60 {"fixup_8", 0, 8, 0},
61 {"fixup_sym_diff", 0, 32, 0},
62 };
63 // clang-format on
64 static_assert((std::size(Infos)) == MSP430::NumTargetFixupKinds,
65 "Not all fixup kinds added to Infos array");
66
67 if (Kind < FirstTargetFixupKind)
69
70 return Infos[Kind - FirstTargetFixupKind];
71 }
72
73 bool writeNopData(raw_ostream &OS, uint64_t Count,
74 const MCSubtargetInfo *STI) const override;
75};
76
77uint64_t MSP430AsmBackend::adjustFixupValue(const MCFixup &Fixup,
79 MCContext &Ctx) const {
80 unsigned Kind = Fixup.getKind();
81 switch (Kind) {
83 if (Value & 0x1)
84 Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
85
86 // Offset is signed
87 int16_t Offset = Value;
88 // Jumps are in words
89 Offset >>= 1;
90 // PC points to the next instruction so decrement by one
91 --Offset;
92
94 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
95
96 // Mask 10 bits
97 Offset &= 0x3ff;
98
99 return Offset;
100 }
101 default:
102 return Value;
103 }
104}
105
106void MSP430AsmBackend::applyFixup(const MCFragment &F, const MCFixup &Fixup,
107 const MCValue &Target, uint8_t *Data,
108 uint64_t Value, bool IsResolved) {
109 maybeAddReloc(F, Fixup, Target, Value, IsResolved);
111 MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
112 if (!Value)
113 return; // Doesn't change encoding.
114
115 // Shift the value into position.
116 Value <<= Info.TargetOffset;
117
118 unsigned NumBytes = alignTo(Info.TargetSize + Info.TargetOffset, 8) / 8;
119 assert(Fixup.getOffset() + NumBytes <= F.getSize() &&
120 "Invalid fixup offset!");
121
122 // For each byte of the fragment that the fixup touches, mask in the
123 // bits from the fixup value.
124 for (unsigned i = 0; i != NumBytes; ++i) {
125 Data[i] |= uint8_t((Value >> (i * 8)) & 0xff);
126 }
127}
128
129bool MSP430AsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
130 const MCSubtargetInfo *STI) const {
131 if ((Count % 2) != 0)
132 return false;
133
134 // The canonical nop on MSP430 is mov #0, r3
135 uint64_t NopCount = Count / 2;
136 while (NopCount--)
137 OS.write("\x03\x43", 2);
138
139 return true;
140}
141
142} // end anonymous namespace
143
145 const MCSubtargetInfo &STI,
146 const MCRegisterInfo &MRI,
147 const MCTargetOptions &Options) {
148 return new MSP430AsmBackend(STI, ELF::ELFOSABI_STANDALONE);
149}
unsigned const MachineRegisterInfo * MRI
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
Analysis containing CSE Info
Definition CSEInfo.cpp:27
static LVOptions Options
Definition LVOptions.cpp:25
#define F(x, y, z)
Definition MD5.cpp:55
#define T
PowerPC TLS Dynamic Call Fixup
Generic interface to target specific assembler backends.
virtual MCFixupKindInfo getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:61
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
raw_ostream & write(unsigned char C)
@ ELFOSABI_STANDALONE
Definition ELF.h:374
Context & getContext() const
Definition BasicBlock.h:99
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
uint16_t MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition MCFixup.h:22
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
@ FirstTargetFixupKind
Definition MCFixup.h:44
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:155
MCAsmBackend * createMSP430MCAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
std::unique_ptr< MCObjectTargetWriter > createMSP430ELFObjectWriter(uint8_t OSABI)
endianness
Definition bit.h:71