LLVM  9.0.0svn
HexagonMCELFStreamer.cpp
Go to the documentation of this file.
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 #define DEBUG_TYPE "hexagonmcelfstreamer"
14 
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/BinaryFormat/ELF.h"
20 #include "llvm/MC/MCAsmBackend.h"
21 #include "llvm/MC/MCAssembler.h"
22 #include "llvm/MC/MCCodeEmitter.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCObjectWriter.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCSectionELF.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/MC/MCSymbolELF.h"
33 #include "llvm/Support/Casting.h"
37 #include <cassert>
38 #include <cstdint>
39 
40 using namespace llvm;
41 
43  ("gpsize", cl::NotHidden,
44  cl::desc("Global Pointer Addressing Size. The default size is 8."),
45  cl::Prefix,
46  cl::init(8));
47 
49  MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
50  std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter)
51  : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
52  MCII(createHexagonMCInstrInfo()) {}
53 
55  MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
56  std::unique_ptr<MCObjectWriter> OW, std::unique_ptr<MCCodeEmitter> Emitter,
57  MCAssembler *Assembler)
58  : MCELFStreamer(Context, std::move(TAB), std::move(OW), std::move(Emitter)),
59  MCII(createHexagonMCInstrInfo()) {}
60 
62  const MCSubtargetInfo &STI) {
63  assert(MCB.getOpcode() == Hexagon::BUNDLE);
66 
67  // At this point, MCB is a bundle
68  // Iterate through the bundle and assign addends for the instructions
69  for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
70  MCInst *MCI = const_cast<MCInst *>(I.getInst());
71  EmitSymbol(*MCI);
72  }
73 
75 }
76 
78  // Scan for values.
79  for (unsigned i = Inst.getNumOperands(); i--;)
80  if (Inst.getOperand(i).isExpr())
81  visitUsedExpr(*Inst.getOperand(i).getExpr());
82 }
83 
84 // EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the
85 // functions found in MCELFStreamer.cpp taking AccessSize as an additional
86 // parameter.
88  uint64_t Size,
89  unsigned ByteAlignment,
90  unsigned AccessSize) {
91  getAssembler().registerSymbol(*Symbol);
92  StringRef sbss[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"};
93 
94  auto ELFSymbol = cast<MCSymbolELF>(Symbol);
95  if (!ELFSymbol->isBindingSet()) {
96  ELFSymbol->setBinding(ELF::STB_GLOBAL);
97  ELFSymbol->setExternal(true);
98  }
99 
100  ELFSymbol->setType(ELF::STT_OBJECT);
101 
102  if (ELFSymbol->getBinding() == ELF::STB_LOCAL) {
104  ((AccessSize == 0) || (Size == 0) || (Size > GPSize))
105  ? ".bss"
106  : sbss[(Log2_64(AccessSize))];
110  SwitchSection(&Section);
111 
112  if (ELFSymbol->isUndefined()) {
113  EmitValueToAlignment(ByteAlignment, 0, 1, 0);
114  EmitLabel(Symbol);
115  EmitZeros(Size);
116  }
117 
118  // Update the maximum alignment of the section if necessary.
119  if (ByteAlignment > Section.getAlignment())
120  Section.setAlignment(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 + (Log2_64(AccessSize) + 1)
131  : (unsigned)ELF::SHN_HEXAGON_SCOMMON;
132  ELFSymbol->setIndex(SectionIndex);
133  }
134  }
135 
136  ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
137 }
138 
140  uint64_t Size,
141  unsigned 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 
151 namespace llvm {
153  std::unique_ptr<MCAsmBackend> MAB,
154  std::unique_ptr<MCObjectWriter> OW,
155  std::unique_ptr<MCCodeEmitter> CE) {
156  return new HexagonMCELFStreamer(Context, std::move(MAB), std::move(OW),
157  std::move(CE));
158  }
159 
160 } // end namespace llvm
void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
LLVMContext & Context
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
MCContext & getContext() const
Definition: MCAssembler.h:284
MCContext & getContext() const
Definition: MCStreamer.h:250
Definition: BitVector.h:937
static cl::opt< unsigned > GPSize("gpsize", cl::NotHidden, cl::desc("Global Pointer Addressing Size. The default size is 8."), cl::Prefix, cl::init(8))
iterator_range< Hexagon::PacketIterator > bundleInstructions(MCInstrInfo const &MCII, MCInst const &MCI)
void EmitValueToAlignment(unsigned, int64_t, unsigned, unsigned) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
Context object for machine code objects.
Definition: MCContext.h:62
MCInstrInfo * createHexagonMCInstrInfo()
const MCExpr * getExpr() const
Definition: MCInst.h:95
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:427
void EmitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:205
Streaming machine code generation interface.
Definition: MCStreamer.h:188
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
MCAssembler & getAssembler()
bool isExpr() const
Definition: MCInst.h:60
unsigned getNumOperands() const
Definition: MCInst.h:181
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
void HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)
MCSectionSubPair getCurrentSection() const
Return the current section that the streamer is emitting code to.
Definition: MCStreamer.h:338
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:56
void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
HexagonMCELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:179
void EmitSymbol(const MCInst &Inst)
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:934
#define I(x, y, z)
Definition: MD5.cpp:58
Generic base class for all target subtargets.
uint32_t Size
Definition: Profile.cpp:46
size_t bundleSize(MCInst const &MCI)
void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:202
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCStreamer * createHexagonELFStreamer(Triple const &TT, MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > CE)
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:388
#define HEXAGON_PACKET_SIZE
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
unsigned getOpcode() const
Definition: MCInst.h:171
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:544
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:163
void HexagonMCEmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)