1 //=== HexagonMCELFStreamer.cpp - Hexagon subclass of MCELFStreamer -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a stub that parses a MCInst bundle and passes the
11 // instructions on to the real streamer.
13 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "hexagonmcelfstreamer"
17 #include "HexagonMCELFStreamer.h"
18 #include "MCTargetDesc/HexagonBaseInfo.h"
19 #include "MCTargetDesc/HexagonMCShuffler.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/MC/MCAssembler.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCSectionELF.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCSymbolELF.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/raw_ostream.h"
33 static cl::opt<unsigned>
34 GPSize("gpsize", cl::NotHidden,
35 cl::desc("Global Pointer Addressing Size. The default size is 8."),
36 cl::Prefix, cl::init(8));
38 void HexagonMCELFStreamer::EmitInstruction(const MCInst &MCK,
39 const MCSubtargetInfo &STI) {
41 HMI.setOpcode(Hexagon::BUNDLE);
42 HMI.addOperand(MCOperand::createImm(0));
45 if (MCK.getOpcode() != Hexagon::BUNDLE) {
46 HMI.addOperand(MCOperand::createInst(&MCK));
49 MCB = const_cast<MCInst *>(&MCK);
51 // Examines packet and pad the packet, if needed, when an
52 // end-loop is in the bundle.
53 HexagonMCInstrInfo::padEndloop(*MCB);
54 HexagonMCShuffle(*MCII, STI, *MCB);
56 assert(HexagonMCInstrInfo::bundleSize(*MCB) <= HEXAGON_PACKET_SIZE);
57 bool Extended = false;
58 for (auto &I : HexagonMCInstrInfo::bundleInstructions(*MCB)) {
59 MCInst *MCI = const_cast<MCInst *>(I.getInst());
61 if (HexagonMCInstrInfo::isDuplex(*MCII, *MCI)) {
62 MCInst *SubInst = const_cast<MCInst *>(MCI->getOperand(1).getInst());
63 HexagonMCInstrInfo::clampExtended(*MCII, getContext(), *SubInst);
65 HexagonMCInstrInfo::clampExtended(*MCII, getContext(), *MCI);
69 Extended = HexagonMCInstrInfo::isImmext(*MCI);
73 // At this point, MCB is a bundle
74 // Iterate through the bundle and assign addends for the instructions
75 for (auto const &I : HexagonMCInstrInfo::bundleInstructions(*MCB)) {
76 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,
94 unsigned ByteAlignment,
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);
102 ELFSymbol->setExternal(true);
105 ELFSymbol->setType(ELF::STT_OBJECT);
107 if (ELFSymbol->getBinding() == ELF::STB_LOCAL) {
108 StringRef SectionName =
109 ((AccessSize == 0) || (Size == 0) || (Size > GPSize))
111 : sbss[(Log2_64(AccessSize))];
113 MCSection *CrntSection = getCurrentSection().first;
114 MCSection *Section = getAssembler().getContext().getELFSection(
115 SectionName, ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
116 SwitchSection(Section);
117 AssignFragment(Symbol, getCurrentFragment());
119 MCELFStreamer::EmitCommonSymbol(Symbol, Size, ByteAlignment);
120 SwitchSection(CrntSection);
122 if (ELFSymbol->declareCommon(Size, ByteAlignment))
123 report_fatal_error("Symbol: " + Symbol->getName() +
124 " redeclared as different type");
125 if ((AccessSize) && (Size <= GPSize)) {
126 uint64_t SectionIndex =
127 (AccessSize <= GPSize)
128 ? ELF::SHN_HEXAGON_SCOMMON + (Log2_64(AccessSize) + 1)
129 : (unsigned)ELF::SHN_HEXAGON_SCOMMON;
130 ELFSymbol->setIndex(SectionIndex);
134 ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
137 void HexagonMCELFStreamer::HexagonMCEmitLocalCommonSymbol(
138 MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment,
139 unsigned AccessSize) {
140 getAssembler().registerSymbol(*Symbol);
141 auto ELFSymbol = cast<MCSymbolELF>(Symbol);
142 ELFSymbol->setBinding(ELF::STB_LOCAL);
143 ELFSymbol->setExternal(false);
144 HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize);
148 MCStreamer *createHexagonELFStreamer(MCContext &Context, MCAsmBackend &MAB,
149 raw_pwrite_stream &OS, MCCodeEmitter *CE) {
150 return new HexagonMCELFStreamer(Context, MAB, OS, CE);