LLVM  16.0.0git
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 
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"
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"
36 #include <cassert>
37 #include <cstdint>
38 
39 #define DEBUG_TYPE "hexagonmcelfstreamer"
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) {
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())
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()) {
115  emitLabel(Symbol);
116  emitZeros(Size);
117  }
118 
119  // Update the maximum alignment of the section if necessary.
120  Section.ensureMinAlignment(Align(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
i
i
Definition: README.txt:29
llvm::MCStreamer::visitUsedExpr
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:1070
MathExtras.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::ELF::STB_GLOBAL
@ STB_GLOBAL
Definition: ELF.h:1218
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::MCELFStreamer::emitLabel
void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
Definition: MCELFStreamer.cpp:101
llvm::MCAssembler::registerSymbol
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
Definition: MCAssembler.cpp:467
llvm::cl::Prefix
@ Prefix
Definition: CommandLine.h:161
llvm::HexagonMCELFStreamer::emitInstruction
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
Definition: HexagonMCELFStreamer.cpp:62
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
StringRef.h
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
MCCodeEmitter.h
llvm::MCConstantExpr::create
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition: MCExpr.cpp:194
MCSectionELF.h
llvm::ELF::SHN_HEXAGON_SCOMMON
@ SHN_HEXAGON_SCOMMON
Definition: ELF.h:651
ErrorHandling.h
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
MCAssembler.h
llvm::ELF::SHT_NOBITS
@ SHT_NOBITS
Definition: ELF.h:987
GPSize
static cl::opt< unsigned > GPSize("gpsize", cl::NotHidden, cl::desc("Global Pointer Addressing Size. The default size is 8."), cl::Prefix, cl::init(8))
llvm::MCStreamer::getCurrentSection
MCSectionSubPair getCurrentSection() const
Return the current section that the streamer is emitting code to.
Definition: MCStreamer.h:384
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::MCInst::getNumOperands
unsigned getNumOperands() const
Definition: MCInst.h:208
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
MCObjectStreamer.h
CommandLine.h
llvm::MCStreamer
Streaming machine code generation interface.
Definition: MCStreamer.h:212
HexagonMCShuffler.h
Emitter
dxil DXContainer Global Emitter
Definition: DXContainerGlobals.cpp:67
llvm::Log2_64
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:553
llvm::createHexagonELFStreamer
MCStreamer * createHexagonELFStreamer(Triple const &TT, MCContext &Context, std::unique_ptr< MCAsmBackend > MAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > CE)
Definition: HexagonMCELFStreamer.cpp:152
ELF.h
llvm::MCELFStreamer
Definition: MCELFStreamer.h:31
MCAsmBackend.h
HexagonMCInstrInfo.h
llvm::MCAssembler::getContext
MCContext & getContext() const
Definition: MCAssembler.h:321
MCSymbolELF.h
MCContext.h
llvm::MCObjectStreamer::emitInstruction
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
Definition: MCObjectStreamer.cpp:409
llvm::MCStreamer::switchSection
virtual void switchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:1234
llvm::cl::NotHidden
@ NotHidden
Definition: CommandLine.h:139
MCSymbol.h
llvm::MCContext::getELFSection
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:564
MCInst.h
llvm::HexagonMCInstrInfo::bundleInstructions
iterator_range< Hexagon::PacketIterator > bundleInstructions(MCInstrInfo const &MCII, MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:103
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:145
Align
uint64_t Align
Definition: ELFObjHandler.cpp:82
llvm::HexagonMCInstrInfo::bundleSize
size_t bundleSize(MCInst const &MCI)
Definition: HexagonMCInstrInfo.cpp:116
llvm::MCELFStreamer::emitValueToAlignment
void emitValueToAlignment(Align, int64_t, unsigned, unsigned) override
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
Definition: MCELFStreamer.cpp:367
llvm::cl::opt
Definition: CommandLine.h:1412
llvm::MCAssembler
Definition: MCAssembler.h:73
HexagonMCELFStreamer.h
llvm::createHexagonMCInstrInfo
MCInstrInfo * createHexagonMCInstrInfo()
Definition: HexagonMCTargetDesc.cpp:304
uint64_t
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:447
llvm::ELF::SHF_ALLOC
@ SHF_ALLOC
Definition: ELF.h:1064
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1836
MCSection.h
llvm::MCObjectStreamer::getAssembler
MCAssembler & getAssembler()
Definition: MCObjectStreamer.h:128
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::MCSection
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Definition: MCSection.h:39
llvm::HexagonMCELFStreamer::HexagonMCELFStreamer
HexagonMCELFStreamer(MCContext &Context, std::unique_ptr< MCAsmBackend > TAB, std::unique_ptr< MCObjectWriter > OW, std::unique_ptr< MCCodeEmitter > Emitter)
Definition: HexagonMCELFStreamer.cpp:49
llvm::HexagonMCELFStreamer
Definition: HexagonMCELFStreamer.h:20
MCObjectWriter.h
std
Definition: BitVector.h:851
llvm::SectionName
Definition: DWARFSection.h:21
Casting.h
llvm::MCOperand::getExpr
const MCExpr * getExpr() const
Definition: MCInst.h:114
llvm::ELF::SHF_WRITE
@ SHF_WRITE
Definition: ELF.h:1061
llvm::MCInst::getOpcode
unsigned getOpcode() const
Definition: MCInst.h:198
llvm::MCOperand::isExpr
bool isExpr() const
Definition: MCInst.h:65
llvm::LCOMM::ByteAlignment
@ ByteAlignment
Definition: MCAsmInfo.h:50
llvm::HexagonMCELFStreamer::EmitSymbol
void EmitSymbol(const MCInst &Inst)
Definition: HexagonMCELFStreamer.cpp:78
llvm::MCStreamer::getContext
MCContext & getContext() const
Definition: MCStreamer.h:293
MCStreamer.h
llvm::MCInst::getOperand
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:206
llvm::MCSectionSubPair
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:66
llvm::ELF::STB_LOCAL
@ STB_LOCAL
Definition: ELF.h:1217
llvm::HexagonMCELFStreamer::HexagonMCEmitLocalCommonSymbol
void HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)
Definition: HexagonMCELFStreamer.cpp:139
llvm::cl::desc
Definition: CommandLine.h:413
HEXAGON_PACKET_SIZE
#define HEXAGON_PACKET_SIZE
Definition: HexagonMCTargetDesc.h:36
MCExpr.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:76
llvm::ELF::STT_OBJECT
@ STT_OBJECT
Definition: ELF.h:1230
llvm::HexagonMCELFStreamer::HexagonMCEmitCommonSymbol
void HexagonMCEmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)
Definition: HexagonMCELFStreamer.cpp:88
llvm::MCStreamer::emitZeros
void emitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:227