xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCELFStreamer.cpp (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
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                                                      Align 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     Section.ensureMinAlignment(ByteAlignment);
121 
122     switchSection(P.first, P.second);
123   } else {
124     if (ELFSymbol->declareCommon(Size, ByteAlignment))
125       report_fatal_error("Symbol: " + Symbol->getName() +
126                          " redeclared as different type");
127     if ((AccessSize) && (Size <= GPSize)) {
128       uint64_t SectionIndex =
129           (AccessSize <= GPSize)
130               ? ELF::SHN_HEXAGON_SCOMMON + llvm::bit_width(AccessSize)
131               : (unsigned)ELF::SHN_HEXAGON_SCOMMON;
132       ELFSymbol->setIndex(SectionIndex);
133     }
134   }
135 
136   ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
137 }
138 
139 void HexagonMCELFStreamer::HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol,
140                                                           uint64_t Size,
141                                                           Align ByteAlignment,
142                                                           unsigned AccessSize) {
143   getAssembler().registerSymbol(*Symbol);
144   auto ELFSymbol = cast<MCSymbolELF>(Symbol);
145   ELFSymbol->setBinding(ELF::STB_LOCAL);
146   ELFSymbol->setExternal(false);
147   HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize);
148 }
149 
150 namespace llvm {
151 MCStreamer *createHexagonELFStreamer(Triple const &TT, MCContext &Context,
152                                      std::unique_ptr<MCAsmBackend> MAB,
153                                      std::unique_ptr<MCObjectWriter> OW,
154                                      std::unique_ptr<MCCodeEmitter> CE) {
155   return new HexagonMCELFStreamer(Context, std::move(MAB), std::move(OW),
156                                   std::move(CE));
157   }
158 
159 } // end namespace llvm
160