1 //=== HexagonMCELFStreamer.cpp - Hexagon subclass of MCELFStreamer -------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a stub that parses a MCInst bundle and passes the
10 // instructions on to the real streamer.
12 //===----------------------------------------------------------------------===//
14 #include "MCTargetDesc/HexagonMCELFStreamer.h"
15 #include "HexagonTargetStreamer.h"
16 #include "MCTargetDesc/HexagonMCChecker.h"
17 #include "MCTargetDesc/HexagonMCInstrInfo.h"
18 #include "MCTargetDesc/HexagonMCShuffler.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCCodeEmitter.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCObjectStreamer.h"
28 #include "llvm/MC/MCObjectWriter.h"
29 #include "llvm/MC/MCSection.h"
30 #include "llvm/MC/MCSectionELF.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSubtargetInfo.h"
33 #include "llvm/MC/MCSymbol.h"
34 #include "llvm/MC/MCSymbolELF.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/CommandLine.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Support/HexagonAttributes.h"
39 #include "llvm/Support/MathExtras.h"
43 #define DEBUG_TYPE "hexagonmcelfstreamer"
47 static cl::opt
<unsigned> GPSize
48 ("gpsize", cl::NotHidden
,
49 cl::desc("Global Pointer Addressing Size. The default size is 8."),
53 HexagonMCELFStreamer::HexagonMCELFStreamer(
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
)),
57 MCII(createHexagonMCInstrInfo()) {}
59 HexagonMCELFStreamer::HexagonMCELFStreamer(
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
)),
64 MCII(createHexagonMCInstrInfo()) {}
66 void HexagonMCELFStreamer::emitInstruction(const MCInst
&MCB
,
67 const MCSubtargetInfo
&STI
) {
68 assert(MCB
.getOpcode() == Hexagon::BUNDLE
);
69 assert(HexagonMCInstrInfo::bundleSize(MCB
) <= HEXAGON_PACKET_SIZE
);
70 assert(HexagonMCInstrInfo::bundleSize(MCB
) > 0);
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());
79 MCObjectStreamer::emitInstruction(MCB
, STI
);
82 void HexagonMCELFStreamer::EmitSymbol(const MCInst
&Inst
) {
84 for (unsigned i
= Inst
.getNumOperands(); i
--;)
85 if (Inst
.getOperand(i
).isExpr())
86 visitUsedExpr(*Inst
.getOperand(i
).getExpr());
89 // EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the
90 // functions found in MCELFStreamer.cpp taking AccessSize as an additional
92 void HexagonMCELFStreamer::HexagonMCEmitCommonSymbol(MCSymbol
*Symbol
,
95 unsigned AccessSize
) {
96 getAssembler().registerSymbol(*Symbol
);
97 StringRef sbss
[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"};
99 auto ELFSymbol
= cast
<MCSymbolELF
>(Symbol
);
100 if (!ELFSymbol
->isBindingSet())
101 ELFSymbol
->setBinding(ELF::STB_GLOBAL
);
103 ELFSymbol
->setType(ELF::STT_OBJECT
);
105 if (ELFSymbol
->getBinding() == ELF::STB_LOCAL
) {
106 StringRef SectionName
=
107 ((AccessSize
== 0) || (Size
== 0) || (Size
> GPSize
))
109 : sbss
[(Log2_64(AccessSize
))];
110 MCSection
&Section
= *getAssembler().getContext().getELFSection(
111 SectionName
, ELF::SHT_NOBITS
, ELF::SHF_WRITE
| ELF::SHF_ALLOC
);
112 MCSectionSubPair P
= getCurrentSection();
113 switchSection(&Section
);
115 if (ELFSymbol
->isUndefined()) {
116 emitValueToAlignment(ByteAlignment
, 0, 1, 0);
121 // Update the maximum alignment of the section if necessary.
122 Section
.ensureMinAlignment(ByteAlignment
);
124 switchSection(P
.first
, P
.second
);
126 if (ELFSymbol
->declareCommon(Size
, ByteAlignment
))
127 report_fatal_error("Symbol: " + Symbol
->getName() +
128 " redeclared as different type");
129 if ((AccessSize
) && (Size
<= GPSize
)) {
130 uint64_t SectionIndex
=
131 (AccessSize
<= GPSize
)
132 ? ELF::SHN_HEXAGON_SCOMMON
+ llvm::bit_width(AccessSize
)
133 : (unsigned)ELF::SHN_HEXAGON_SCOMMON
;
134 ELFSymbol
->setIndex(SectionIndex
);
138 ELFSymbol
->setSize(MCConstantExpr::create(Size
, getContext()));
141 void HexagonMCELFStreamer::HexagonMCEmitLocalCommonSymbol(MCSymbol
*Symbol
,
144 unsigned AccessSize
) {
145 getAssembler().registerSymbol(*Symbol
);
146 auto ELFSymbol
= cast
<MCSymbolELF
>(Symbol
);
147 ELFSymbol
->setBinding(ELF::STB_LOCAL
);
148 ELFSymbol
->setExternal(false);
149 HexagonMCEmitCommonSymbol(Symbol
, Size
, ByteAlignment
, AccessSize
);
152 static unsigned featureToArchVersion(unsigned Feature
) {
154 case Hexagon::ArchV5
:
156 case Hexagon::ArchV55
:
158 case Hexagon::ArchV60
:
159 case Hexagon::ExtensionHVXV60
:
161 case Hexagon::ArchV62
:
162 case Hexagon::ExtensionHVXV62
:
164 case Hexagon::ArchV65
:
165 case Hexagon::ExtensionHVXV65
:
167 case Hexagon::ArchV66
:
168 case Hexagon::ExtensionHVXV66
:
170 case Hexagon::ArchV67
:
171 case Hexagon::ExtensionHVXV67
:
173 case Hexagon::ArchV68
:
174 case Hexagon::ExtensionHVXV68
:
176 case Hexagon::ArchV69
:
177 case Hexagon::ExtensionHVXV69
:
179 case Hexagon::ArchV71
:
180 case Hexagon::ExtensionHVXV71
:
182 case Hexagon::ArchV73
:
183 case Hexagon::ExtensionHVXV73
:
185 case Hexagon::ArchV75
:
186 case Hexagon::ExtensionHVXV75
:
188 case Hexagon::ArchV79
:
189 case Hexagon::ExtensionHVXV79
:
192 llvm_unreachable("Expected valid arch feature");
196 void HexagonTargetStreamer::emitTargetAttributes(const MCSubtargetInfo
&STI
) {
197 auto Features
= STI
.getFeatureBits();
198 unsigned Arch
= featureToArchVersion(Hexagon_MC::getArchVersion(Features
));
199 std::optional
<unsigned> HVXArch
= Hexagon_MC::getHVXVersion(Features
);
200 emitAttribute(HexagonAttrs::ARCH
, Arch
);
202 emitAttribute(HexagonAttrs::HVXARCH
, featureToArchVersion(*HVXArch
));
203 if (Features
.test(Hexagon::ExtensionHVXIEEEFP
))
204 emitAttribute(HexagonAttrs::HVXIEEEFP
, 1);
205 if (Features
.test(Hexagon::ExtensionHVXQFloat
))
206 emitAttribute(HexagonAttrs::HVXQFLOAT
, 1);
207 if (Features
.test(Hexagon::ExtensionZReg
))
208 emitAttribute(HexagonAttrs::ZREG
, 1);
209 if (Features
.test(Hexagon::ExtensionAudio
))
210 emitAttribute(HexagonAttrs::AUDIO
, 1);
211 if (Features
.test(Hexagon::FeatureCabac
))
212 emitAttribute(HexagonAttrs::CABAC
, 1);
216 MCStreamer
*createHexagonELFStreamer(Triple
const &TT
, MCContext
&Context
,
217 std::unique_ptr
<MCAsmBackend
> MAB
,
218 std::unique_ptr
<MCObjectWriter
> OW
,
219 std::unique_ptr
<MCCodeEmitter
> CE
) {
220 return new HexagonMCELFStreamer(Context
, std::move(MAB
), std::move(OW
),
224 } // end namespace llvm