10b57cec5SDimitry Andric //=== HexagonMCELFStreamer.cpp - Hexagon subclass of MCELFStreamer -------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is a stub that parses a MCInst bundle and passes the
100b57cec5SDimitry Andric // instructions on to the real streamer.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "MCTargetDesc/HexagonMCELFStreamer.h"
15*0fca6ea1SDimitry Andric #include "HexagonTargetStreamer.h"
16*0fca6ea1SDimitry Andric #include "MCTargetDesc/HexagonMCChecker.h"
170b57cec5SDimitry Andric #include "MCTargetDesc/HexagonMCInstrInfo.h"
180b57cec5SDimitry Andric #include "MCTargetDesc/HexagonMCShuffler.h"
190b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
200b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
210b57cec5SDimitry Andric #include "llvm/MC/MCAsmBackend.h"
220b57cec5SDimitry Andric #include "llvm/MC/MCAssembler.h"
230b57cec5SDimitry Andric #include "llvm/MC/MCCodeEmitter.h"
240b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
250b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
260b57cec5SDimitry Andric #include "llvm/MC/MCInst.h"
270b57cec5SDimitry Andric #include "llvm/MC/MCObjectStreamer.h"
280b57cec5SDimitry Andric #include "llvm/MC/MCObjectWriter.h"
290b57cec5SDimitry Andric #include "llvm/MC/MCSection.h"
300b57cec5SDimitry Andric #include "llvm/MC/MCSectionELF.h"
310b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
32*0fca6ea1SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
330b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h"
340b57cec5SDimitry Andric #include "llvm/MC/MCSymbolELF.h"
350b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
360b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
370b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
38*0fca6ea1SDimitry Andric #include "llvm/Support/HexagonAttributes.h"
390b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
400b57cec5SDimitry Andric #include <cassert>
410b57cec5SDimitry Andric #include <cstdint>
420b57cec5SDimitry Andric
43fe6060f1SDimitry Andric #define DEBUG_TYPE "hexagonmcelfstreamer"
44fe6060f1SDimitry Andric
450b57cec5SDimitry Andric using namespace llvm;
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric static cl::opt<unsigned> GPSize
480b57cec5SDimitry Andric ("gpsize", cl::NotHidden,
490b57cec5SDimitry Andric cl::desc("Global Pointer Addressing Size. The default size is 8."),
500b57cec5SDimitry Andric cl::Prefix,
510b57cec5SDimitry Andric cl::init(8));
520b57cec5SDimitry Andric
HexagonMCELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> TAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter)530b57cec5SDimitry Andric HexagonMCELFStreamer::HexagonMCELFStreamer(
540b57cec5SDimitry Andric MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
550b57cec5SDimitry Andric std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter)
560b57cec5SDimitry Andric : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
570b57cec5SDimitry Andric MCII(createHexagonMCInstrInfo()) {}
580b57cec5SDimitry Andric
HexagonMCELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> TAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter,MCAssembler * Assembler)590b57cec5SDimitry Andric HexagonMCELFStreamer::HexagonMCELFStreamer(
600b57cec5SDimitry Andric MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
610b57cec5SDimitry Andric std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
620b57cec5SDimitry Andric MCAssembler *Assembler)
630b57cec5SDimitry Andric : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
640b57cec5SDimitry Andric MCII(createHexagonMCInstrInfo()) {}
650b57cec5SDimitry Andric
emitInstruction(const MCInst & MCB,const MCSubtargetInfo & STI)665ffd83dbSDimitry Andric void HexagonMCELFStreamer::emitInstruction(const MCInst &MCB,
670b57cec5SDimitry Andric const MCSubtargetInfo &STI) {
680b57cec5SDimitry Andric assert(MCB.getOpcode() == Hexagon::BUNDLE);
690b57cec5SDimitry Andric assert(HexagonMCInstrInfo::bundleSize(MCB) <= HEXAGON_PACKET_SIZE);
700b57cec5SDimitry Andric assert(HexagonMCInstrInfo::bundleSize(MCB) > 0);
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric // At this point, MCB is a bundle
730b57cec5SDimitry Andric // Iterate through the bundle and assign addends for the instructions
740b57cec5SDimitry Andric for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
750b57cec5SDimitry Andric MCInst *MCI = const_cast<MCInst *>(I.getInst());
760b57cec5SDimitry Andric EmitSymbol(*MCI);
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric
795ffd83dbSDimitry Andric MCObjectStreamer::emitInstruction(MCB, STI);
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric
EmitSymbol(const MCInst & Inst)820b57cec5SDimitry Andric void HexagonMCELFStreamer::EmitSymbol(const MCInst &Inst) {
830b57cec5SDimitry Andric // Scan for values.
840b57cec5SDimitry Andric for (unsigned i = Inst.getNumOperands(); i--;)
850b57cec5SDimitry Andric if (Inst.getOperand(i).isExpr())
860b57cec5SDimitry Andric visitUsedExpr(*Inst.getOperand(i).getExpr());
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric // EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the
900b57cec5SDimitry Andric // functions found in MCELFStreamer.cpp taking AccessSize as an additional
910b57cec5SDimitry Andric // parameter.
HexagonMCEmitCommonSymbol(MCSymbol * Symbol,uint64_t Size,Align ByteAlignment,unsigned AccessSize)920b57cec5SDimitry Andric void HexagonMCELFStreamer::HexagonMCEmitCommonSymbol(MCSymbol *Symbol,
930b57cec5SDimitry Andric uint64_t Size,
94bdd1243dSDimitry Andric Align ByteAlignment,
950b57cec5SDimitry Andric unsigned AccessSize) {
960b57cec5SDimitry Andric getAssembler().registerSymbol(*Symbol);
970b57cec5SDimitry Andric StringRef sbss[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"};
980b57cec5SDimitry Andric
990b57cec5SDimitry Andric auto ELFSymbol = cast<MCSymbolELF>(Symbol);
100*0fca6ea1SDimitry Andric if (!ELFSymbol->isBindingSet())
1010b57cec5SDimitry Andric ELFSymbol->setBinding(ELF::STB_GLOBAL);
1020b57cec5SDimitry Andric
1030b57cec5SDimitry Andric ELFSymbol->setType(ELF::STT_OBJECT);
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric if (ELFSymbol->getBinding() == ELF::STB_LOCAL) {
1060b57cec5SDimitry Andric StringRef SectionName =
1070b57cec5SDimitry Andric ((AccessSize == 0) || (Size == 0) || (Size > GPSize))
1080b57cec5SDimitry Andric ? ".bss"
1090b57cec5SDimitry Andric : sbss[(Log2_64(AccessSize))];
1100b57cec5SDimitry Andric MCSection &Section = *getAssembler().getContext().getELFSection(
1110b57cec5SDimitry Andric SectionName, ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
1120b57cec5SDimitry Andric MCSectionSubPair P = getCurrentSection();
11381ad6265SDimitry Andric switchSection(&Section);
1140b57cec5SDimitry Andric
1150b57cec5SDimitry Andric if (ELFSymbol->isUndefined()) {
1165ffd83dbSDimitry Andric emitValueToAlignment(ByteAlignment, 0, 1, 0);
1175ffd83dbSDimitry Andric emitLabel(Symbol);
1185ffd83dbSDimitry Andric emitZeros(Size);
1190b57cec5SDimitry Andric }
1200b57cec5SDimitry Andric
1210b57cec5SDimitry Andric // Update the maximum alignment of the section if necessary.
122bdd1243dSDimitry Andric Section.ensureMinAlignment(ByteAlignment);
1230b57cec5SDimitry Andric
12481ad6265SDimitry Andric switchSection(P.first, P.second);
1250b57cec5SDimitry Andric } else {
1260b57cec5SDimitry Andric if (ELFSymbol->declareCommon(Size, ByteAlignment))
1270b57cec5SDimitry Andric report_fatal_error("Symbol: " + Symbol->getName() +
1280b57cec5SDimitry Andric " redeclared as different type");
1290b57cec5SDimitry Andric if ((AccessSize) && (Size <= GPSize)) {
1300b57cec5SDimitry Andric uint64_t SectionIndex =
1310b57cec5SDimitry Andric (AccessSize <= GPSize)
132bdd1243dSDimitry Andric ? ELF::SHN_HEXAGON_SCOMMON + llvm::bit_width(AccessSize)
1330b57cec5SDimitry Andric : (unsigned)ELF::SHN_HEXAGON_SCOMMON;
1340b57cec5SDimitry Andric ELFSymbol->setIndex(SectionIndex);
1350b57cec5SDimitry Andric }
1360b57cec5SDimitry Andric }
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
1390b57cec5SDimitry Andric }
1400b57cec5SDimitry Andric
HexagonMCEmitLocalCommonSymbol(MCSymbol * Symbol,uint64_t Size,Align ByteAlignment,unsigned AccessSize)1410b57cec5SDimitry Andric void HexagonMCELFStreamer::HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol,
1420b57cec5SDimitry Andric uint64_t Size,
143bdd1243dSDimitry Andric Align ByteAlignment,
1440b57cec5SDimitry Andric unsigned AccessSize) {
1450b57cec5SDimitry Andric getAssembler().registerSymbol(*Symbol);
1460b57cec5SDimitry Andric auto ELFSymbol = cast<MCSymbolELF>(Symbol);
1470b57cec5SDimitry Andric ELFSymbol->setBinding(ELF::STB_LOCAL);
1480b57cec5SDimitry Andric ELFSymbol->setExternal(false);
1490b57cec5SDimitry Andric HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize);
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric
featureToArchVersion(unsigned Feature)152*0fca6ea1SDimitry Andric static unsigned featureToArchVersion(unsigned Feature) {
153*0fca6ea1SDimitry Andric switch (Feature) {
154*0fca6ea1SDimitry Andric case Hexagon::ArchV5:
155*0fca6ea1SDimitry Andric return 5;
156*0fca6ea1SDimitry Andric case Hexagon::ArchV55:
157*0fca6ea1SDimitry Andric return 55;
158*0fca6ea1SDimitry Andric case Hexagon::ArchV60:
159*0fca6ea1SDimitry Andric case Hexagon::ExtensionHVXV60:
160*0fca6ea1SDimitry Andric return 60;
161*0fca6ea1SDimitry Andric case Hexagon::ArchV62:
162*0fca6ea1SDimitry Andric case Hexagon::ExtensionHVXV62:
163*0fca6ea1SDimitry Andric return 62;
164*0fca6ea1SDimitry Andric case Hexagon::ArchV65:
165*0fca6ea1SDimitry Andric case Hexagon::ExtensionHVXV65:
166*0fca6ea1SDimitry Andric return 65;
167*0fca6ea1SDimitry Andric case Hexagon::ArchV66:
168*0fca6ea1SDimitry Andric case Hexagon::ExtensionHVXV66:
169*0fca6ea1SDimitry Andric return 66;
170*0fca6ea1SDimitry Andric case Hexagon::ArchV67:
171*0fca6ea1SDimitry Andric case Hexagon::ExtensionHVXV67:
172*0fca6ea1SDimitry Andric return 67;
173*0fca6ea1SDimitry Andric case Hexagon::ArchV68:
174*0fca6ea1SDimitry Andric case Hexagon::ExtensionHVXV68:
175*0fca6ea1SDimitry Andric return 68;
176*0fca6ea1SDimitry Andric case Hexagon::ArchV69:
177*0fca6ea1SDimitry Andric case Hexagon::ExtensionHVXV69:
178*0fca6ea1SDimitry Andric return 69;
179*0fca6ea1SDimitry Andric case Hexagon::ArchV71:
180*0fca6ea1SDimitry Andric case Hexagon::ExtensionHVXV71:
181*0fca6ea1SDimitry Andric return 71;
182*0fca6ea1SDimitry Andric case Hexagon::ArchV73:
183*0fca6ea1SDimitry Andric case Hexagon::ExtensionHVXV73:
184*0fca6ea1SDimitry Andric return 73;
185*0fca6ea1SDimitry Andric }
186*0fca6ea1SDimitry Andric llvm_unreachable("Expected valid arch feature");
187*0fca6ea1SDimitry Andric return 0;
188*0fca6ea1SDimitry Andric }
189*0fca6ea1SDimitry Andric
emitTargetAttributes(const MCSubtargetInfo & STI)190*0fca6ea1SDimitry Andric void HexagonTargetStreamer::emitTargetAttributes(const MCSubtargetInfo &STI) {
191*0fca6ea1SDimitry Andric auto Features = STI.getFeatureBits();
192*0fca6ea1SDimitry Andric unsigned Arch = featureToArchVersion(Hexagon_MC::getArchVersion(Features));
193*0fca6ea1SDimitry Andric std::optional<unsigned> HVXArch = Hexagon_MC::getHVXVersion(Features);
194*0fca6ea1SDimitry Andric emitAttribute(HexagonAttrs::ARCH, Arch);
195*0fca6ea1SDimitry Andric if (HVXArch)
196*0fca6ea1SDimitry Andric emitAttribute(HexagonAttrs::HVXARCH, featureToArchVersion(*HVXArch));
197*0fca6ea1SDimitry Andric if (Features.test(Hexagon::ExtensionHVXIEEEFP))
198*0fca6ea1SDimitry Andric emitAttribute(HexagonAttrs::HVXIEEEFP, 1);
199*0fca6ea1SDimitry Andric if (Features.test(Hexagon::ExtensionHVXQFloat))
200*0fca6ea1SDimitry Andric emitAttribute(HexagonAttrs::HVXQFLOAT, 1);
201*0fca6ea1SDimitry Andric if (Features.test(Hexagon::ExtensionZReg))
202*0fca6ea1SDimitry Andric emitAttribute(HexagonAttrs::ZREG, 1);
203*0fca6ea1SDimitry Andric if (Features.test(Hexagon::ExtensionAudio))
204*0fca6ea1SDimitry Andric emitAttribute(HexagonAttrs::AUDIO, 1);
205*0fca6ea1SDimitry Andric if (Features.test(Hexagon::FeatureCabac))
206*0fca6ea1SDimitry Andric emitAttribute(HexagonAttrs::CABAC, 1);
207*0fca6ea1SDimitry Andric }
208*0fca6ea1SDimitry Andric
2090b57cec5SDimitry Andric namespace llvm {
createHexagonELFStreamer(Triple const & TT,MCContext & Context,std::unique_ptr<MCAsmBackend> MAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> CE)2100b57cec5SDimitry Andric MCStreamer *createHexagonELFStreamer(Triple const &TT, MCContext &Context,
2110b57cec5SDimitry Andric std::unique_ptr<MCAsmBackend> MAB,
2120b57cec5SDimitry Andric std::unique_ptr<MCObjectWriter> OW,
2130b57cec5SDimitry Andric std::unique_ptr<MCCodeEmitter> CE) {
2140b57cec5SDimitry Andric return new HexagonMCELFStreamer(Context, std::move(MAB), std::move(OW),
2150b57cec5SDimitry Andric std::move(CE));
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric } // end namespace llvm
219