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 
14 #include "MCTargetDesc/HexagonMCELFStreamer.h"
15 #include "MCTargetDesc/HexagonMCInstrInfo.h"
16 #include "MCTargetDesc/HexagonMCShuffler.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/BinaryFormat/ELF.h"
19 #include "llvm/MC/MCAsmBackend.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCObjectStreamer.h"
26 #include "llvm/MC/MCObjectWriter.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCSectionELF.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCSymbolELF.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/MathExtras.h"
36 #include <cassert>
37 #include <cstdint>
38 
39 #define DEBUG_TYPE "hexagonmcelfstreamer"
40 
41 using namespace llvm;
42 
43 static cl::opt<unsigned> GPSize
44   ("gpsize", cl::NotHidden,
45    cl::desc("Global Pointer Addressing Size.  The default size is 8."),
46    cl::Prefix,
47    cl::init(8));
48 
49 HexagonMCELFStreamer::HexagonMCELFStreamer(
50     MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
51     std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter)
52     : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
53       MCII(createHexagonMCInstrInfo()) {}
54 
55 HexagonMCELFStreamer::HexagonMCELFStreamer(
56     MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
57     std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
58     MCAssembler *Assembler)
59     : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
60       MCII(createHexagonMCInstrInfo()) {}
61 
62 void HexagonMCELFStreamer::emitInstruction(const MCInst &MCB,
63                                            const MCSubtargetInfo &STI) {
64   assert(MCB.getOpcode() == Hexagon::BUNDLE);
65   assert(HexagonMCInstrInfo::bundleSize(MCB) <= HEXAGON_PACKET_SIZE);
66   assert(HexagonMCInstrInfo::bundleSize(MCB) > 0);
67 
68   // At this point, MCB is a bundle
69   // Iterate through the bundle and assign addends for the instructions
70   for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
71     MCInst *MCI = const_cast<MCInst *>(I.getInst());
72     EmitSymbol(*MCI);
73   }
74 
75   MCObjectStreamer::emitInstruction(MCB, STI);
76 }
77 
78 void HexagonMCELFStreamer::EmitSymbol(const MCInst &Inst) {
79   // Scan for values.
80   for (unsigned i = Inst.getNumOperands(); i--;)
81     if (Inst.getOperand(i).isExpr())
82       visitUsedExpr(*Inst.getOperand(i).getExpr());
83 }
84 
85 // EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the
86 // functions found in MCELFStreamer.cpp taking AccessSize as an additional
87 // parameter.
88 void HexagonMCELFStreamer::HexagonMCEmitCommonSymbol(MCSymbol *Symbol,
89                                                      uint64_t Size,
90                                                      unsigned ByteAlignment,
91                                                      unsigned AccessSize) {
92   getAssembler().registerSymbol(*Symbol);
93   StringRef sbss[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"};
94 
95   auto ELFSymbol = cast<MCSymbolELF>(Symbol);
96   if (!ELFSymbol->isBindingSet()) {
97     ELFSymbol->setBinding(ELF::STB_GLOBAL);
98     ELFSymbol->setExternal(true);
99   }
100 
101   ELFSymbol->setType(ELF::STT_OBJECT);
102 
103   if (ELFSymbol->getBinding() == ELF::STB_LOCAL) {
104     StringRef SectionName =
105         ((AccessSize == 0) || (Size == 0) || (Size > GPSize))
106             ? ".bss"
107             : sbss[(Log2_64(AccessSize))];
108     MCSection &Section = *getAssembler().getContext().getELFSection(
109         SectionName, ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
110     MCSectionSubPair P = getCurrentSection();
111     switchSection(&Section);
112 
113     if (ELFSymbol->isUndefined()) {
114       emitValueToAlignment(ByteAlignment, 0, 1, 0);
115       emitLabel(Symbol);
116       emitZeros(Size);
117     }
118 
119     // Update the maximum alignment of the section if necessary.
120     if (Align(ByteAlignment) > Section.getAlignment())
121       Section.setAlignment(Align(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)
131               ? ELF::SHN_HEXAGON_SCOMMON + (Log2_64(AccessSize) + 1)
132               : (unsigned)ELF::SHN_HEXAGON_SCOMMON;
133       ELFSymbol->setIndex(SectionIndex);
134     }
135   }
136 
137   ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
138 }
139 
140 void HexagonMCELFStreamer::HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol,
141                                                          uint64_t Size,
142                                                          unsigned ByteAlignment,
143                                                          unsigned AccessSize) {
144   getAssembler().registerSymbol(*Symbol);
145   auto ELFSymbol = cast<MCSymbolELF>(Symbol);
146   ELFSymbol->setBinding(ELF::STB_LOCAL);
147   ELFSymbol->setExternal(false);
148   HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize);
149 }
150 
151 
152 namespace llvm {
153 MCStreamer *createHexagonELFStreamer(Triple const &TT, MCContext &Context,
154                                      std::unique_ptr<MCAsmBackend> MAB,
155                                      std::unique_ptr<MCObjectWriter> OW,
156                                      std::unique_ptr<MCCodeEmitter> CE) {
157   return new HexagonMCELFStreamer(Context, std::move(MAB), std::move(OW),
158                                   std::move(CE));
159   }
160 
161 } // end namespace llvm
162