LLVM  4.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/MC/MCAssembler.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCExpr.h"
23 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCSectionELF.h"
27 #include "llvm/MC/MCStreamer.h"
28 #include "llvm/MC/MCSymbol.h"
29 #include "llvm/MC/MCSymbolELF.h"
30 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/ELF.h"
35 #include <cassert>
36 #include <cstdint>
37 
38 using namespace llvm;
39 
40 static cl::opt<unsigned>
41  GPSize("gpsize", cl::NotHidden,
42  cl::desc("Global Pointer Addressing Size. The default size is 8."),
43  cl::Prefix, cl::init(8));
44 
46  const MCSubtargetInfo &STI) {
48  MCInst *MCB;
49 
50  if (MCK.getOpcode() != Hexagon::BUNDLE) {
52  MCB = &HMI;
53  } else
54  MCB = const_cast<MCInst *>(&MCK);
55 
56  // Examines packet and pad the packet, if needed, when an
57  // end-loop is in the bundle.
59  HexagonMCShuffle(*MCII, STI, *MCB);
60 
62  bool Extended = false;
63  for (auto &I : HexagonMCInstrInfo::bundleInstructions(*MCB)) {
64  MCInst *MCI = const_cast<MCInst *>(I.getInst());
65  if (Extended) {
66  if (HexagonMCInstrInfo::isDuplex(*MCII, *MCI)) {
67  MCInst *SubInst = const_cast<MCInst *>(MCI->getOperand(1).getInst());
69  } else {
71  }
72  Extended = false;
73  } else {
74  Extended = HexagonMCInstrInfo::isImmext(*MCI);
75  }
76  }
77 
78  // At this point, MCB is a bundle
79  // Iterate through the bundle and assign addends for the instructions
80  for (auto const &I : HexagonMCInstrInfo::bundleInstructions(*MCB)) {
81  MCInst *MCI = const_cast<MCInst *>(I.getInst());
82  EmitSymbol(*MCI);
83  }
85 }
86 
88  // Scan for values.
89  for (unsigned i = Inst.getNumOperands(); i--;)
90  if (Inst.getOperand(i).isExpr())
92 }
93 
94 // EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the
95 // functions found in MCELFStreamer.cpp taking AccessSize as an additional
96 // parameter.
98  uint64_t Size,
99  unsigned ByteAlignment,
100  unsigned AccessSize) {
101  getAssembler().registerSymbol(*Symbol);
102  StringRef sbss[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"};
103 
104  auto ELFSymbol = cast<MCSymbolELF>(Symbol);
105  if (!ELFSymbol->isBindingSet()) {
106  ELFSymbol->setBinding(ELF::STB_GLOBAL);
107  ELFSymbol->setExternal(true);
108  }
109 
110  ELFSymbol->setType(ELF::STT_OBJECT);
111 
112  if (ELFSymbol->getBinding() == ELF::STB_LOCAL) {
114  ((AccessSize == 0) || (Size == 0) || (Size > GPSize))
115  ? ".bss"
116  : sbss[(Log2_64(AccessSize))];
120  SwitchSection(&Section);
121 
122  EmitValueToAlignment(ByteAlignment, 0, 1, 0);
123  EmitLabel(Symbol);
124  EmitZeros(Size);
125 
126  // Update the maximum alignment of the section if necessary.
127  if (ByteAlignment > Section.getAlignment())
128  Section.setAlignment(ByteAlignment);
129 
130  SwitchSection(P.first, P.second);
131  } else {
132  if (ELFSymbol->declareCommon(Size, ByteAlignment))
133  report_fatal_error("Symbol: " + Symbol->getName() +
134  " redeclared as different type");
135  if ((AccessSize) && (Size <= GPSize)) {
136  uint64_t SectionIndex =
137  (AccessSize <= GPSize)
138  ? ELF::SHN_HEXAGON_SCOMMON + (Log2_64(AccessSize) + 1)
139  : (unsigned)ELF::SHN_HEXAGON_SCOMMON;
140  ELFSymbol->setIndex(SectionIndex);
141  }
142  }
143 
144  ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
145 }
146 
148  MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment,
149  unsigned AccessSize) {
150  getAssembler().registerSymbol(*Symbol);
151  auto ELFSymbol = cast<MCSymbolELF>(Symbol);
152  ELFSymbol->setBinding(ELF::STB_LOCAL);
153  ELFSymbol->setExternal(false);
154  HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize);
155 }
156 
157 namespace llvm {
158 
160  raw_pwrite_stream &OS, MCCodeEmitter *CE) {
161  return new HexagonMCELFStreamer(Context, MAB, OS, CE);
162 }
163 
164 } // 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:40
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.
size_t i
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
MCContext & getContext() const
Definition: MCAssembler.h:258
bool isImmext(MCInst const &MCI)
#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:297
static cl::opt< unsigned > GPSize("gpsize", cl::NotHidden, cl::desc("Global Pointer Addressing Size. The default size is 8."), cl::Prefix, cl::init(8))
void EmitLabel(MCSymbol *Symbol) override
Emit a label for Symbol into the current section.
void padEndloop(MCContext &Context, MCInst &MCI)
MCContext & getContext() const
Definition: MCStreamer.h:221
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:51
std::pair< MCSection *, const MCExpr * > MCSectionSubPair
Definition: MCStreamer.h:44
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
const MCExpr * getExpr() const
Definition: MCInst.h:93
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:395
void EmitZeros(uint64_t NumBytes)
Emit NumBytes worth of zeros.
Definition: MCStreamer.cpp:173
Streaming machine code generation interface.
Definition: MCStreamer.h:161
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:829
MCAssembler & getAssembler()
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:23
bool isExpr() const
Definition: MCInst.h:59
iterator_range< MCInst::const_iterator > bundleInstructions(MCInst const &MCI)
void clampExtended(MCInstrInfo const &MCII, MCContext &Context, MCInst &MCI)
void HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)
void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
bool HexagonMCShuffle(MCInstrInfo const &MCII, MCSubtargetInfo const &STI, MCInst &)
void EmitSymbol(const MCInst &Inst)
unsigned getOpcode() const
Definition: MCInst.h:159
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:739
MCStreamer * createHexagonELFStreamer(MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS, MCCodeEmitter *CE)
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:135
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:199
unsigned getNumOperands() const
Definition: MCInst.h:166
#define I(x, y, z)
Definition: MD5.cpp:54
MCSubtargetInfo - Generic base class for all target subtargets.
const MCInst * getInst() const
Definition: MCInst.h:102
const char SectionName[]
Definition: AMDGPUPTNote.h:24
size_t bundleSize(MCInst const &MCI)
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:341
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:36
void addOperand(const MCOperand &Op)
Definition: MCInst.h:168
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
unsigned Log2_64(uint64_t Value)
Log2_64 - This function returns the floor log base 2 of the specified value, -1 if the value is zero...
Definition: MathExtras.h:519
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:149
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:164
void HexagonMCEmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)