LLVM 19.0.0git
HexagonMCELFStreamer.cpp
Go to the documentation of this file.
1//=== HexagonMCELFStreamer.cpp - Hexagon subclass of MCELFStreamer -------===//
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// This file is a stub that parses a MCInst bundle and passes the
10// instructions on to the real streamer.
11//
12//===----------------------------------------------------------------------===//
13
19#include "llvm/ADT/StringRef.h"
22#include "llvm/MC/MCAssembler.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCExpr.h"
26#include "llvm/MC/MCInst.h"
29#include "llvm/MC/MCSection.h"
31#include "llvm/MC/MCStreamer.h"
33#include "llvm/MC/MCSymbol.h"
34#include "llvm/MC/MCSymbolELF.h"
40#include <cassert>
41#include <cstdint>
42
43#define DEBUG_TYPE "hexagonmcelfstreamer"
44
45using namespace llvm;
46
48 ("gpsize", cl::NotHidden,
49 cl::desc("Global Pointer Addressing Size. The default size is 8."),
51 cl::init(8));
52
54 MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
55 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter)
56 : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
58
60 MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
61 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
62 MCAssembler *Assembler)
63 : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
65
67 const MCSubtargetInfo &STI) {
68 assert(MCB.getOpcode() == Hexagon::BUNDLE);
71
72 // At this point, MCB is a bundle
73 // Iterate through the bundle and assign addends for the instructions
74 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
75 MCInst *MCI = const_cast<MCInst *>(I.getInst());
76 EmitSymbol(*MCI);
77 }
78
80}
81
83 // Scan for values.
84 for (unsigned i = Inst.getNumOperands(); i--;)
85 if (Inst.getOperand(i).isExpr())
87}
88
89// EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the
90// functions found in MCELFStreamer.cpp taking AccessSize as an additional
91// parameter.
94 Align ByteAlignment,
95 unsigned AccessSize) {
97 StringRef sbss[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"};
98
99 auto ELFSymbol = cast<MCSymbolELF>(Symbol);
100 if (!ELFSymbol->isBindingSet()) {
101 ELFSymbol->setBinding(ELF::STB_GLOBAL);
102 ELFSymbol->setExternal(true);
103 }
104
105 ELFSymbol->setType(ELF::STT_OBJECT);
106
107 if (ELFSymbol->getBinding() == ELF::STB_LOCAL) {
109 ((AccessSize == 0) || (Size == 0) || (Size > GPSize))
110 ? ".bss"
111 : sbss[(Log2_64(AccessSize))];
115 switchSection(&Section);
116
117 if (ELFSymbol->isUndefined()) {
118 emitValueToAlignment(ByteAlignment, 0, 1, 0);
119 emitLabel(Symbol);
121 }
122
123 // Update the maximum alignment of the section if necessary.
124 Section.ensureMinAlignment(ByteAlignment);
125
126 switchSection(P.first, P.second);
127 } else {
128 if (ELFSymbol->declareCommon(Size, ByteAlignment))
129 report_fatal_error("Symbol: " + Symbol->getName() +
130 " redeclared as different type");
131 if ((AccessSize) && (Size <= GPSize)) {
132 uint64_t SectionIndex =
133 (AccessSize <= GPSize)
136 ELFSymbol->setIndex(SectionIndex);
137 }
138 }
139
140 ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
141}
142
145 Align ByteAlignment,
146 unsigned AccessSize) {
147 getAssembler().registerSymbol(*Symbol);
148 auto ELFSymbol = cast<MCSymbolELF>(Symbol);
149 ELFSymbol->setBinding(ELF::STB_LOCAL);
150 ELFSymbol->setExternal(false);
151 HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize);
152}
153
154static unsigned featureToArchVersion(unsigned Feature) {
155 switch (Feature) {
156 case Hexagon::ArchV5:
157 return 5;
158 case Hexagon::ArchV55:
159 return 55;
160 case Hexagon::ArchV60:
161 case Hexagon::ExtensionHVXV60:
162 return 60;
163 case Hexagon::ArchV62:
164 case Hexagon::ExtensionHVXV62:
165 return 62;
166 case Hexagon::ArchV65:
167 case Hexagon::ExtensionHVXV65:
168 return 65;
169 case Hexagon::ArchV66:
170 case Hexagon::ExtensionHVXV66:
171 return 66;
172 case Hexagon::ArchV67:
173 case Hexagon::ExtensionHVXV67:
174 return 67;
175 case Hexagon::ArchV68:
176 case Hexagon::ExtensionHVXV68:
177 return 68;
178 case Hexagon::ArchV69:
179 case Hexagon::ExtensionHVXV69:
180 return 69;
181 case Hexagon::ArchV71:
182 case Hexagon::ExtensionHVXV71:
183 return 71;
184 case Hexagon::ArchV73:
185 case Hexagon::ExtensionHVXV73:
186 return 73;
187 }
188 llvm_unreachable("Expected valid arch feature");
189 return 0;
190}
191
193 auto Features = STI.getFeatureBits();
194 unsigned Arch = featureToArchVersion(Hexagon_MC::getArchVersion(Features));
195 std::optional<unsigned> HVXArch = Hexagon_MC::getHVXVersion(Features);
197 if (HVXArch)
199 if (Features.test(Hexagon::ExtensionHVXIEEEFP))
201 if (Features.test(Hexagon::ExtensionHVXQFloat))
203 if (Features.test(Hexagon::ExtensionZReg))
205 if (Features.test(Hexagon::ExtensionAudio))
207 if (Features.test(Hexagon::FeatureCabac))
209}
210
211namespace llvm {
213 std::unique_ptr<MCAsmBackend> MAB,
214 std::unique_ptr<MCObjectWriter> OW,
215 std::unique_ptr<MCCodeEmitter> CE) {
216 return new HexagonMCELFStreamer(Context, std::move(MAB), std::move(OW),
217 std::move(CE));
218 }
219
220} // end namespace llvm
dxil DXContainer Global Emitter
uint64_t Size
static cl::opt< unsigned > GPSize("gpsize", cl::NotHidden, cl::desc("Global Pointer Addressing Size. The default size is 8."), cl::Prefix, cl::init(8))
static unsigned featureToArchVersion(unsigned Feature)
#define HEXAGON_PACKET_SIZE
#define I(x, y, z)
Definition: MD5.cpp:58
LLVMContext & Context
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment, unsigned AccessSize)
void HexagonMCEmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment, unsigned AccessSize)
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void EmitSymbol(const MCInst &Inst)
HexagonMCELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
void emitTargetAttributes(const MCSubtargetInfo &STI)
virtual void emitAttribute(unsigned Attribute, unsigned Value)
MCContext & getContext() const
Definition: MCAssembler.h:326
bool registerSymbol(const MCSymbol &Symbol)
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
Context object for machine code objects.
Definition: MCContext.h:81
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:578
void emitValueToAlignment(Align, int64_t, unsigned, unsigned) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
unsigned getNumOperands() const
Definition: MCInst.h:208
unsigned getOpcode() const
Definition: MCInst.h:198
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:206
MCAssembler & getAssembler()
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
const MCExpr * getExpr() const
Definition: MCInst.h:114
bool isExpr() const
Definition: MCInst.h:65
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
Streaming machine code generation interface.
Definition: MCStreamer.h:212
MCContext & getContext() const
Definition: MCStreamer.h:297
virtual void switchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
MCSectionSubPair getCurrentSection() const
Return the current section that the streamer is emitting code to.
Definition: MCStreamer.h:388
void emitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:230
void visitUsedExpr(const MCExpr &Expr)
Generic base class for all target subtargets.
const FeatureBitset & getFeatureBits() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SHF_ALLOC
Definition: ELF.h:1157
@ SHF_WRITE
Definition: ELF.h:1154
@ SHT_NOBITS
Definition: ELF.h:1070
@ SHN_HEXAGON_SCOMMON
Definition: ELF.h:654
@ STB_GLOBAL
Definition: ELF.h:1311
@ STB_LOCAL
Definition: ELF.h:1310
@ STT_OBJECT
Definition: ELF.h:1323
size_t bundleSize(MCInst const &MCI)
iterator_range< Hexagon::PacketIterator > bundleInstructions(MCInstrInfo const &MCII, MCInst const &MCI)
unsigned getArchVersion(const FeatureBitset &Features)
std::optional< unsigned > getHVXVersion(const FeatureBitset &Features)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition: bit.h:317
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:319
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
MCInstrInfo * createHexagonMCInstrInfo()
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:66
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1849
MCStreamer * createHexagonELFStreamer(Triple const &TT, MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > CE)
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39