LLVM 19.0.0git
XtensaAsmBackend.cpp
Go to the documentation of this file.
1//===-- XtensaMCAsmBackend.cpp - Xtensa assembler backend -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
6// See https://llvm.org/LICENSE.txt for license information.
7// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8//
9//===----------------------------------------------------------------------===//
10
14#include "llvm/MC/MCAssembler.h"
15#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCInst.h"
22
23using namespace llvm;
24
25namespace llvm {
28 uint8_t OSABI;
29 bool IsLittleEndian;
30
31public:
32 XtensaMCAsmBackend(uint8_t osABI, bool isLE)
33 : MCAsmBackend(llvm::endianness::little), OSABI(osABI),
34 IsLittleEndian(isLE) {}
35
36 unsigned getNumFixupKinds() const override {
38 }
39 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
40 void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
42 uint64_t Value, bool IsResolved,
43 const MCSubtargetInfo *STI) const override;
44 bool mayNeedRelaxation(const MCInst &Inst,
45 const MCSubtargetInfo &STI) const override;
46 void relaxInstruction(MCInst &Inst,
47 const MCSubtargetInfo &STI) const override;
49 const MCSubtargetInfo *STI) const override;
50
51 std::unique_ptr<MCObjectTargetWriter> createObjectTargetWriter() const override {
52 return createXtensaObjectWriter(OSABI, IsLittleEndian);
53 }
54};
55} // namespace llvm
56
57const MCFixupKindInfo &
59 const static MCFixupKindInfo Infos[Xtensa::NumTargetFixupKinds] = {
60 // name offset bits flags
61 {"fixup_xtensa_branch_6", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
62 {"fixup_xtensa_branch_8", 16, 8, MCFixupKindInfo::FKF_IsPCRel},
63 {"fixup_xtensa_branch_12", 12, 12, MCFixupKindInfo::FKF_IsPCRel},
64 {"fixup_xtensa_jump_18", 6, 18, MCFixupKindInfo::FKF_IsPCRel},
65 {"fixup_xtensa_call_18", 6, 18,
68 {"fixup_xtensa_l32r_16", 8, 16,
71
72 if (Kind < FirstTargetFixupKind)
74 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
75 "Invalid kind!");
76 return Infos[Kind - FirstTargetFixupKind];
77}
78
80 MCContext &Ctx) {
81 unsigned Kind = Fixup.getKind();
82 switch (Kind) {
83 default:
84 llvm_unreachable("Unknown fixup kind!");
85 case FK_Data_1:
86 case FK_Data_2:
87 case FK_Data_4:
88 case FK_Data_8:
89 return Value;
91 Value -= 4;
92 if (!isInt<6>(Value))
93 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
94 unsigned Hi2 = (Value >> 4) & 0x3;
95 unsigned Lo4 = Value & 0xf;
96 return (Hi2 << 4) | (Lo4 << 12);
97 }
99 Value -= 4;
100 if (!isInt<8>(Value))
101 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
102 return (Value & 0xff);
104 Value -= 4;
105 if (!isInt<12>(Value))
106 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
107 return (Value & 0xfff);
109 Value -= 4;
110 if (!isInt<18>(Value))
111 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
112 return (Value & 0x3ffff);
114 Value -= 4;
115 if (!isInt<20>(Value))
116 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
117 if (Value & 0x3)
118 Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
119 return (Value & 0xffffc) >> 2;
121 unsigned Offset = Fixup.getOffset();
122 if (Offset & 0x3)
123 Value -= 4;
124 if (!isInt<18>(Value) && (Value & 0x20000))
125 Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
126 if (Value & 0x3)
127 Ctx.reportError(Fixup.getLoc(), "fixup value must be 4-byte aligned");
128 return (Value & 0x3fffc) >> 2;
129 }
130}
131
132static unsigned getSize(unsigned Kind) {
133 switch (Kind) {
134 default:
135 return 3;
137 return 4;
139 return 2;
140 }
141}
142
144 const MCFixup &Fixup, const MCValue &Target,
146 bool IsResolved,
147 const MCSubtargetInfo *STI) const {
148 MCContext &Ctx = Asm.getContext();
150
152
153 // Shift the value into position.
154 Value <<= Info.TargetOffset;
155
156 if (!Value)
157 return; // Doesn't change encoding.
158
159 unsigned Offset = Fixup.getOffset();
160 unsigned FullSize = getSize(Fixup.getKind());
161
162 for (unsigned i = 0; i != FullSize; ++i) {
163 Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
164 }
165}
166
168 const MCSubtargetInfo &STI) const {
169 return false;
170}
171
173 const MCSubtargetInfo &STI) const {}
174
176 const MCSubtargetInfo *STI) const {
177 uint64_t NumNops24b = Count / 3;
178
179 for (uint64_t i = 0; i != NumNops24b; ++i) {
180 // Currently just little-endian machine supported,
181 // but probably big-endian will be also implemented in future
182 if (IsLittleEndian) {
183 OS.write("\xf0", 1);
184 OS.write("\x20", 1);
185 OS.write("\0x00", 1);
186 } else {
187 report_fatal_error("Big-endian mode currently is not supported!");
188 }
189 Count -= 3;
190 }
191
192 // TODO maybe function should return error if (Count > 0)
193 switch (Count) {
194 default:
195 break;
196 case 1:
197 OS.write("\0", 1);
198 break;
199 case 2:
200 // NOP.N instruction
201 OS.write("\x3d", 1);
202 OS.write("\xf0", 1);
203 break;
204 }
205
206 return true;
207}
208
210 const MCSubtargetInfo &STI,
211 const MCRegisterInfo &MRI,
212 const MCTargetOptions &Options) {
213 uint8_t OSABI =
215 return new llvm::XtensaMCAsmBackend(OSABI, true);
216}
unsigned const MachineRegisterInfo * MRI
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
static LVOptions Options
Definition: LVOptions.cpp:25
PowerPC TLS Dynamic Call Fixup
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
static unsigned getSize(unsigned Kind)
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:42
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
Context object for machine code objects.
Definition: MCContext.h:83
void reportError(SMLoc L, const Twine &Msg)
Definition: MCContext.cpp:1069
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:71
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
Base class for classes that define behaviour that is specific to both the target and the object forma...
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
Generic base class for all target subtargets.
const Triple & getTargetTriple() const
This represents an "assembler immediate".
Definition: MCValue.h:36
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
Target - Wrapper for Target specific information.
OSType getOS() const
Get the parsed operating system type of this triple.
Definition: Triple.h:382
LLVM Value Representation.
Definition: Value.h:74
const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const override
Get information on a fixup kind.
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const override
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
XtensaMCAsmBackend(uint8_t osABI, bool isLE)
std::unique_ptr< MCObjectTargetWriter > createObjectTargetWriter() const override
bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const override
Write an (optimal) nop sequence of Count bytes to the given output.
unsigned getNumFixupKinds() const override
Get the number of target specific fixup kinds.
bool mayNeedRelaxation(const MCInst &Inst, const MCSubtargetInfo &STI) const override
Check whether the given instruction may need relaxation.
void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const override
Relax the instruction in the given fragment to the next wider instruction.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
raw_ostream & write(unsigned char C)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
MCFixupKind
Extensible enumeration to represent the type of a fixup.
Definition: MCFixup.h:21
@ FirstTargetFixupKind
Definition: MCFixup.h:45
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:26
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:23
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:25
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:24
std::unique_ptr< MCObjectTargetWriter > createXtensaObjectWriter(uint8_t OSABI, bool IsLittleEndian)
endianness
Definition: bit.h:70
MCAsmBackend * createXtensaMCAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
Target independent information on a fixup kind.
@ FKF_IsAlignedDownTo32Bits
Should this fixup kind force a 4-byte aligned effective PC value?
@ FKF_IsPCRel
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...