]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/Hexagon/MCTargetDesc/HexagonMCELFStreamer.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / Hexagon / MCTargetDesc / HexagonMCELFStreamer.cpp
1 //=== HexagonMCELFStreamer.cpp - Hexagon subclass of MCELFStreamer -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a stub that parses a MCInst bundle and passes the
11 // instructions on to the real streamer.
12 //
13 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "hexagonmcelfstreamer"
15
16 #include "MCTargetDesc/HexagonMCELFStreamer.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/MCSymbol.h"
33 #include "llvm/MC/MCSymbolELF.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/MathExtras.h"
38 #include <cassert>
39 #include <cstdint>
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, bool) {
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 (ByteAlignment > Section.getAlignment())
121       Section.setAlignment(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