LLVM  5.0.0
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/MCAssembler.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCSection.h"
27 #include "llvm/MC/MCSectionELF.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSymbol.h"
30 #include "llvm/MC/MCSymbolELF.h"
31 #include "llvm/Support/Casting.h"
35 #include <cassert>
36 #include <cstdint>
37 
38 using namespace llvm;
39 
41  ("gpsize", cl::NotHidden,
42  cl::desc("Global Pointer Addressing Size. The default size is 8."),
43  cl::Prefix,
44  cl::init(8));
45 
47  const MCSubtargetInfo &STI, bool) {
48  assert(MCB.getOpcode() == Hexagon::BUNDLE);
51  bool Extended = false;
52  for (auto &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
53  MCInst *MCI = const_cast<MCInst *>(I.getInst());
54  if (Extended) {
55  if (HexagonMCInstrInfo::isDuplex(*MCII, *MCI)) {
56  MCInst *SubInst = const_cast<MCInst *>(MCI->getOperand(1).getInst());
58  } else {
60  }
61  Extended = false;
62  } else {
63  Extended = HexagonMCInstrInfo::isImmext(*MCI);
64  }
65  }
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(false)) {
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  MCAsmBackend &MAB,
154  raw_pwrite_stream &OS, MCCodeEmitter *CE) {
155  return new HexagonMCELFStreamer(Context, MAB, OS, CE);
156  }
157 
158 } // end namespace llvm
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:39
bool isDuplex(MCInstrInfo const &MCII, MCInst const &MCI)
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:103
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:257
bool isImmext(MCInst const &MCI)
MCStreamer * createHexagonELFStreamer(Triple const &TT, MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS, MCCodeEmitter *CE)
#define HEXAGON_PACKET_SIZE
Definition: Hexagon.h:33
MCSectionSubPair getCurrentSection() const
Return the current section that the streamer is emitting code to.
Definition: MCStreamer.h:303
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)
MCContext & getContext() const
Definition: MCStreamer.h:226
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:59
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:159
const MCExpr * getExpr() const
Definition: MCInst.h:96
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
void EmitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:181
Streaming machine code generation interface.
Definition: MCStreamer.h:167
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:854
MCAssembler & getAssembler()
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:22
bool isExpr() const
Definition: MCInst.h:61
void clampExtended(MCInstrInfo const &MCII, MCContext &Context, MCInst &MCI)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
void HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:51
void EmitSymbol(const MCInst &Inst)
unsigned getOpcode() const
Definition: MCInst.h:168
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:754
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
unsigned getNumOperands() const
Definition: MCInst.h:175
#define I(x, y, z)
Definition: MD5.cpp:58
MCSubtargetInfo - Generic base class for all target subtargets.
const MCInst * getInst() const
Definition: MCInst.h:106
size_t bundleSize(MCInst const &MCI)
void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:333
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:379
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:35
const char SectionName[]
Definition: AMDGPUPTNote.h:24
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
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:537
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:159
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:173
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.