LLVM 22.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"
39#include <cassert>
40#include <cstdint>
41
42#define DEBUG_TYPE "hexagonmcelfstreamer"
43
44using namespace llvm;
45
47 ("gpsize", cl::NotHidden,
48 cl::desc("Global Pointer Addressing Size. The default size is 8."),
50 cl::init(8));
51
53 MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
54 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter)
55 : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
57
59 MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
60 std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
61 MCAssembler *Assembler)
62 : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
64
66 const MCSubtargetInfo &STI) {
67 assert(MCB.getOpcode() == Hexagon::BUNDLE);
70
71 // At this point, MCB is a bundle
72 // Iterate through the bundle and assign addends for the instructions
73 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
74 MCInst *MCI = const_cast<MCInst *>(I.getInst());
75 EmitSymbol(*MCI);
76 }
77
79}
80
82 // Scan for values.
83 for (unsigned i = Inst.getNumOperands(); i--;)
84 if (Inst.getOperand(i).isExpr())
86}
87
88// EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the
89// functions found in MCELFStreamer.cpp taking AccessSize as an additional
90// parameter.
93 Align ByteAlignment,
94 unsigned AccessSize) {
96 StringRef sbss[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"};
97
98 auto ELFSymbol = static_cast<MCSymbolELF *>(Symbol);
99 if (!ELFSymbol->isBindingSet())
100 ELFSymbol->setBinding(ELF::STB_GLOBAL);
101
102 ELFSymbol->setType(ELF::STT_OBJECT);
103
104 if (ELFSymbol->getBinding() == ELF::STB_LOCAL) {
106 ((AccessSize == 0) || (Size == 0) || (Size > GPSize))
107 ? ".bss"
108 : sbss[(Log2_64(AccessSize))];
112 switchSection(&Section);
113
114 if (ELFSymbol->isUndefined()) {
115 emitValueToAlignment(ByteAlignment, 0, 1, 0);
116 emitLabel(Symbol);
118 }
119
120 // Update the maximum alignment of the section if necessary.
121 Section.ensureMinAlignment(ByteAlignment);
122
123 switchSection(P.first, P.second);
124 } else {
125 if (ELFSymbol->declareCommon(Size, ByteAlignment))
126 report_fatal_error("Symbol: " + Symbol->getName() +
127 " redeclared as different type");
128 if ((AccessSize) && (Size <= GPSize)) {
129 uint64_t SectionIndex =
130 (AccessSize <= GPSize)
133 ELFSymbol->setIndex(SectionIndex);
134 }
135 }
136
137 ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
138}
139
142 Align ByteAlignment,
143 unsigned AccessSize) {
144 getAssembler().registerSymbol(*Symbol);
145 auto ELFSymbol = static_cast<const MCSymbolELF *>(Symbol);
146 ELFSymbol->setBinding(ELF::STB_LOCAL);
147 HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize);
148}
149
150static unsigned featureToArchVersion(unsigned Feature) {
151 switch (Feature) {
152 case Hexagon::ArchV5:
153 return 5;
154 case Hexagon::ArchV55:
155 return 55;
156 case Hexagon::ArchV60:
157 case Hexagon::ExtensionHVXV60:
158 return 60;
159 case Hexagon::ArchV62:
160 case Hexagon::ExtensionHVXV62:
161 return 62;
162 case Hexagon::ArchV65:
163 case Hexagon::ExtensionHVXV65:
164 return 65;
165 case Hexagon::ArchV66:
166 case Hexagon::ExtensionHVXV66:
167 return 66;
168 case Hexagon::ArchV67:
169 case Hexagon::ExtensionHVXV67:
170 return 67;
171 case Hexagon::ArchV68:
172 case Hexagon::ExtensionHVXV68:
173 return 68;
174 case Hexagon::ArchV69:
175 case Hexagon::ExtensionHVXV69:
176 return 69;
177 case Hexagon::ArchV71:
178 case Hexagon::ExtensionHVXV71:
179 return 71;
180 case Hexagon::ArchV73:
181 case Hexagon::ExtensionHVXV73:
182 return 73;
183 case Hexagon::ArchV75:
184 case Hexagon::ExtensionHVXV75:
185 return 75;
186 case Hexagon::ArchV79:
187 case Hexagon::ExtensionHVXV79:
188 return 79;
189 }
190 llvm_unreachable("Expected valid arch feature");
191 return 0;
192}
193
195 auto Features = STI.getFeatureBits();
196 unsigned Arch = featureToArchVersion(Hexagon_MC::getArchVersion(Features));
197 std::optional<unsigned> HVXArch = Hexagon_MC::getHVXVersion(Features);
199 if (HVXArch)
201 if (Features.test(Hexagon::ExtensionHVXIEEEFP))
203 if (Features.test(Hexagon::ExtensionHVXQFloat))
205 if (Features.test(Hexagon::ExtensionZReg))
207 if (Features.test(Hexagon::ExtensionAudio))
209 if (Features.test(Hexagon::FeatureCabac))
211}
212
213namespace llvm {
215 std::unique_ptr<MCAsmBackend> MAB,
216 std::unique_ptr<MCObjectWriter> OW,
217 std::unique_ptr<MCCodeEmitter> CE) {
218 return new HexagonMCELFStreamer(Context, std::move(MAB), std::move(OW),
219 std::move(CE));
220 }
221
222} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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
#define P(N)
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:169
LLVM_ABI bool registerSymbol(const MCSymbol &Symbol)
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:212
Context object for machine code objects.
Definition: MCContext.h:83
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:549
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:188
unsigned getNumOperands() const
Definition: MCInst.h:212
unsigned getOpcode() const
Definition: MCInst.h:202
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:210
void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
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:118
bool isExpr() const
Definition: MCInst.h:69
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:496
Streaming machine code generation interface.
Definition: MCStreamer.h:220
MCContext & getContext() const
Definition: MCStreamer.h:314
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
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:416
void emitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:204
void visitUsedExpr(const MCExpr &Expr)
Generic base class for all target subtargets.
const FeatureBitset & getFeatureBits() const
void setBinding(unsigned Binding) const
Definition: MCSymbolELF.cpp:43
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:47
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SHT_NOBITS
Definition: ELF.h:1147
@ SHN_HEXAGON_SCOMMON
Definition: ELF.h:689
@ STB_GLOBAL
Definition: ELF.h:1397
@ STB_LOCAL
Definition: ELF.h:1396
@ SHF_ALLOC
Definition: ELF.h:1240
@ SHF_WRITE
Definition: ELF.h:1237
@ STT_OBJECT
Definition: ELF.h:1409
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:444
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:270
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:342
std::pair< MCSection *, uint32_t > MCSectionSubPair
Definition: MCStreamer.h:66
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition: Error.cpp:167
MCInstrInfo * createHexagonMCInstrInfo()
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:1886
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:856
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39