LLVM  3.7.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 
16 #include "Hexagon.h"
17 #include "HexagonMCELFStreamer.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/MC/MCAssembler.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCSectionELF.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCSymbol.h"
26 #include "llvm/MC/MCSymbolELF.h"
28 #include "llvm/Support/Debug.h"
30 
31 using namespace llvm;
32 
33 static cl::opt<unsigned>
34  GPSize("gpsize", cl::NotHidden,
35  cl::desc("Global Pointer Addressing Size. The default size is 8."),
36  cl::Prefix, cl::init(8));
37 
39  const MCSubtargetInfo &STI) {
40  MCInst HMI;
43  MCInst *MCB;
44 
45  if (MCK.getOpcode() != Hexagon::BUNDLE) {
47  MCB = &HMI;
48  } else
49  MCB = const_cast<MCInst *>(&MCK);
50 
51  // Examines packet and pad the packet, if needed, when an
52  // end-loop is in the bundle.
54  HexagonMCShuffle(*MCII, STI, *MCB);
55 
57  bool Extended = false;
58  for (auto &I : HexagonMCInstrInfo::bundleInstructions(*MCB)) {
59  MCInst *MCI = const_cast<MCInst *>(I.getInst());
60  if (Extended) {
61  if (HexagonMCInstrInfo::isDuplex(*MCII, *MCI)) {
62  MCInst *SubInst = const_cast<MCInst *>(MCI->getOperand(1).getInst());
63  HexagonMCInstrInfo::clampExtended(*MCII, *SubInst);
64  } else {
66  }
67  Extended = false;
68  } else {
69  Extended = HexagonMCInstrInfo::isImmext(*MCI);
70  }
71  }
72 
73  // At this point, MCB is a bundle
74  // Iterate through the bundle and assign addends for the instructions
75  for (auto const &I : HexagonMCInstrInfo::bundleInstructions(*MCB)) {
76  MCInst *MCI = const_cast<MCInst *>(I.getInst());
77  EmitSymbol(*MCI);
78  }
80 }
81 
83  // Scan for values.
84  for (unsigned i = Inst.getNumOperands(); i--;)
85  if (Inst.getOperand(i).isExpr())
86  visitUsedExpr(*Inst.getOperand(i).getExpr());
87 }
88 
89 // EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the
90 // functions found in MCELFStreamer.cpp taking AccessSize as an additional
91 // parameter.
93  uint64_t Size,
94  unsigned ByteAlignment,
95  unsigned AccessSize) {
96  getAssembler().registerSymbol(*Symbol);
97  StringRef sbss[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"};
98 
99  auto ELFSymbol = cast<MCSymbolELF>(Symbol);
100  if (!ELFSymbol->isBindingSet()) {
101  ELFSymbol->setBinding(ELF::STB_GLOBAL);
102  ELFSymbol->setExternal(true);
103  }
104 
105  ELFSymbol->setType(ELF::STT_OBJECT);
106 
107  if (ELFSymbol->getBinding() == ELF::STB_LOCAL) {
108  StringRef SectionName =
109  ((AccessSize == 0) || (Size == 0) || (Size > GPSize))
110  ? ".bss"
111  : sbss[(Log2_64(AccessSize))];
112 
113  MCSection *CrntSection = getCurrentSection().first;
116  SwitchSection(Section);
117  AssignSection(Symbol, Section);
118 
119  MCELFStreamer::EmitCommonSymbol(Symbol, Size, ByteAlignment);
120  SwitchSection(CrntSection);
121  } else {
122  if (ELFSymbol->declareCommon(Size, ByteAlignment))
123  report_fatal_error("Symbol: " + Symbol->getName() +
124  " redeclared as different type");
125  if ((AccessSize) && (Size <= GPSize)) {
126  uint64_t SectionIndex =
127  (AccessSize <= GPSize)
128  ? ELF::SHN_HEXAGON_SCOMMON + (Log2_64(AccessSize) + 1)
129  : (unsigned)ELF::SHN_HEXAGON_SCOMMON;
130  ELFSymbol->setIndex(SectionIndex);
131  }
132  }
133 
134  ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
135 }
136 
138  MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment,
139  unsigned AccessSize) {
140  getAssembler().registerSymbol(*Symbol);
141  auto ELFSymbol = cast<MCSymbolELF>(Symbol);
142  ELFSymbol->setBinding(ELF::STB_LOCAL);
143  ELFSymbol->setExternal(false);
144  HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize);
145 }
146 
147 namespace llvm {
149  raw_pwrite_stream &OS, MCCodeEmitter *CE) {
150  return new HexagonMCELFStreamer(Context, MAB, OS, CE);
151 }
152 }
virtual void EmitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override
Emit the given Instruction into the current section.
void AssignSection(MCSymbol *Symbol, MCSection *Section)
Sets the symbol's section.
Definition: MCStreamer.cpp:192
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
bool isDuplex(MCInstrInfo const &MCII, MCInst const &MCI)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
MCSectionELF * getELFSection(StringRef Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:311
void clampExtended(MCInstrInfo const &MCII, MCInst &MCI)
void registerSymbol(const MCSymbol &Symbol, bool *Created=nullptr)
MCContext & getContext() const
Definition: MCAssembler.h:731
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
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:274
static cl::opt< unsigned > GPSize("gpsize", cl::NotHidden, cl::desc("Global Pointer Addressing Size. The default size is 8."), cl::Prefix, cl::init(8))
MCContext & getContext() const
Definition: MCStreamer.h:210
Context object for machine code objects.
Definition: MCContext.h:48
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
const MCExpr * getExpr() const
Definition: MCInst.h:93
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:325
Streaming machine code generation interface.
Definition: MCStreamer.h:157
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
Definition: MCStreamer.cpp:701
MCAssembler & getAssembler()
MCCodeEmitter - Generic instruction encoding interface.
Definition: MCCodeEmitter.h:23
void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment) override
Emit a common symbol.
bool isExpr() const
Definition: MCInst.h:59
iterator_range< MCInst::const_iterator > bundleInstructions(MCInst const &MCI)
void HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)
void setOpcode(unsigned Op)
Definition: MCInst.h:158
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)
BUNDLE - This instruction represents an instruction bundle.
Definition: TargetOpcodes.h:91
unsigned getOpcode() const
Definition: MCInst.h:159
void visitUsedExpr(const MCExpr &Expr)
Definition: MCStreamer.cpp:613
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:205
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
size_t bundleSize(MCInst const &MCI)
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:321
Generic interface to target specific assembler backends.
Definition: MCAsmBackend.h:34
void addOperand(const MCOperand &Op)
Definition: MCInst.h:168
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:117
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:474
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:150
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:164
void HexagonMCEmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, unsigned AccessSize)