LLVM  8.0.1
HexagonMCELFStreamer.cpp
Go to the documentation of this file.
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 
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"
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"
38 #include <cassert>
39 #include <cstdint>
40 
41 using namespace llvm;
42 
44  ("gpsize", cl::NotHidden,
45  cl::desc("Global Pointer Addressing Size. The default size is 8."),
46  cl::Prefix,
47  cl::init(8));
48 
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 
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 
63  const MCSubtargetInfo &STI, bool) {
64  assert(MCB.getOpcode() == Hexagon::BUNDLE);
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 
76 }
77 
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.
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) {
105  ((AccessSize == 0) || (Size == 0) || (Size > GPSize))
106  ? ".bss"
107  : sbss[(Log2_64(AccessSize))];
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 
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 {
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
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
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:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, bool) override
Emit the given Instruction into the current section.
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
MCContext & getContext() const
Definition: MCAssembler.h:285
MCContext & getContext() const
Definition: MCStreamer.h:251
Definition: BitVector.h:938
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:63
MCInstrInfo * createHexagonMCInstrInfo()
const MCExpr * getExpr() const
Definition: MCInst.h:96
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:423
void EmitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:201
Streaming machine code generation interface.
Definition: MCStreamer.h:189
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:61
unsigned getNumOperands() const
Definition: MCInst.h:184
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
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:341
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:57
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:182
void EmitSymbol(const MCInst &Inst)
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:930
#define I(x, y, z)
Definition: MD5.cpp:58
Generic base class for all target subtargets.
uint32_t Size
Definition: Profile.cpp:47
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:203
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:389
#define HEXAGON_PACKET_SIZE
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
unsigned getOpcode() const
Definition: MCInst.h:174
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:545
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:164
void HexagonMCEmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)
void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, bool=false) override
Emit the given Instruction into the current section.