2015-06-17 05:06:16 +02:00
|
|
|
//=== HexagonMCELFStreamer.cpp - Hexagon subclass of MCELFStreamer -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a stub that parses a MCInst bundle and passes the
|
|
|
|
// instructions on to the real streamer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#define DEBUG_TYPE "hexagonmcelfstreamer"
|
|
|
|
|
2016-12-17 02:09:05 +01:00
|
|
|
#include "MCTargetDesc/HexagonMCELFStreamer.h"
|
|
|
|
#include "MCTargetDesc/HexagonMCInstrInfo.h"
|
2015-06-17 05:06:16 +02:00
|
|
|
#include "MCTargetDesc/HexagonMCShuffler.h"
|
2016-12-17 02:09:05 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2015-06-17 05:06:16 +02:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2016-12-17 02:09:05 +01:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCInst.h"
|
|
|
|
#include "llvm/MC/MCObjectStreamer.h"
|
|
|
|
#include "llvm/MC/MCSection.h"
|
2015-06-17 05:06:16 +02:00
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/MC/MCSymbolELF.h"
|
2016-12-17 02:09:05 +01:00
|
|
|
#include "llvm/Support/Casting.h"
|
2015-06-17 05:06:16 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2016-12-17 02:09:05 +01:00
|
|
|
#include "llvm/Support/ELF.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
2015-06-17 05:06:16 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2017-02-06 20:35:46 +01:00
|
|
|
static cl::opt<unsigned> GPSize
|
|
|
|
("gpsize", cl::NotHidden,
|
|
|
|
cl::desc("Global Pointer Addressing Size. The default size is 8."),
|
|
|
|
cl::Prefix,
|
|
|
|
cl::init(8));
|
2015-06-17 05:06:16 +02:00
|
|
|
|
2017-02-06 20:35:46 +01:00
|
|
|
void HexagonMCELFStreamer::EmitInstruction(const MCInst &MCB,
|
2017-04-14 09:44:23 +02:00
|
|
|
const MCSubtargetInfo &STI, bool) {
|
2017-02-06 20:35:46 +01:00
|
|
|
assert(MCB.getOpcode() == Hexagon::BUNDLE);
|
|
|
|
assert(HexagonMCInstrInfo::bundleSize(MCB) <= HEXAGON_PACKET_SIZE);
|
|
|
|
assert(HexagonMCInstrInfo::bundleSize(MCB) > 0);
|
2015-06-17 05:06:16 +02:00
|
|
|
bool Extended = false;
|
2017-02-06 20:35:46 +01:00
|
|
|
for (auto &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
|
2015-06-17 05:06:16 +02:00
|
|
|
MCInst *MCI = const_cast<MCInst *>(I.getInst());
|
|
|
|
if (Extended) {
|
|
|
|
if (HexagonMCInstrInfo::isDuplex(*MCII, *MCI)) {
|
|
|
|
MCInst *SubInst = const_cast<MCInst *>(MCI->getOperand(1).getInst());
|
2015-11-10 01:02:27 +01:00
|
|
|
HexagonMCInstrInfo::clampExtended(*MCII, getContext(), *SubInst);
|
2015-06-17 05:06:16 +02:00
|
|
|
} else {
|
2015-11-10 01:02:27 +01:00
|
|
|
HexagonMCInstrInfo::clampExtended(*MCII, getContext(), *MCI);
|
2015-06-17 05:06:16 +02:00
|
|
|
}
|
|
|
|
Extended = false;
|
|
|
|
} else {
|
|
|
|
Extended = HexagonMCInstrInfo::isImmext(*MCI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, MCB is a bundle
|
|
|
|
// Iterate through the bundle and assign addends for the instructions
|
2017-02-06 20:35:46 +01:00
|
|
|
for (auto const &I : HexagonMCInstrInfo::bundleInstructions(MCB)) {
|
2015-06-17 05:06:16 +02:00
|
|
|
MCInst *MCI = const_cast<MCInst *>(I.getInst());
|
|
|
|
EmitSymbol(*MCI);
|
|
|
|
}
|
2017-02-06 20:35:46 +01:00
|
|
|
|
|
|
|
MCObjectStreamer::EmitInstruction(MCB, STI);
|
2015-06-17 05:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void HexagonMCELFStreamer::EmitSymbol(const MCInst &Inst) {
|
|
|
|
// Scan for values.
|
|
|
|
for (unsigned i = Inst.getNumOperands(); i--;)
|
|
|
|
if (Inst.getOperand(i).isExpr())
|
|
|
|
visitUsedExpr(*Inst.getOperand(i).getExpr());
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmitCommonSymbol and EmitLocalCommonSymbol are extended versions of the
|
|
|
|
// functions found in MCELFStreamer.cpp taking AccessSize as an additional
|
|
|
|
// parameter.
|
|
|
|
void HexagonMCELFStreamer::HexagonMCEmitCommonSymbol(MCSymbol *Symbol,
|
|
|
|
uint64_t Size,
|
|
|
|
unsigned ByteAlignment,
|
|
|
|
unsigned AccessSize) {
|
|
|
|
getAssembler().registerSymbol(*Symbol);
|
|
|
|
StringRef sbss[4] = {".sbss.1", ".sbss.2", ".sbss.4", ".sbss.8"};
|
|
|
|
|
|
|
|
auto ELFSymbol = cast<MCSymbolELF>(Symbol);
|
|
|
|
if (!ELFSymbol->isBindingSet()) {
|
|
|
|
ELFSymbol->setBinding(ELF::STB_GLOBAL);
|
|
|
|
ELFSymbol->setExternal(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
ELFSymbol->setType(ELF::STT_OBJECT);
|
|
|
|
|
|
|
|
if (ELFSymbol->getBinding() == ELF::STB_LOCAL) {
|
|
|
|
StringRef SectionName =
|
|
|
|
((AccessSize == 0) || (Size == 0) || (Size > GPSize))
|
|
|
|
? ".bss"
|
|
|
|
: sbss[(Log2_64(AccessSize))];
|
2016-04-20 17:54:13 +02:00
|
|
|
MCSection &Section = *getAssembler().getContext().getELFSection(
|
2015-06-17 05:06:16 +02:00
|
|
|
SectionName, ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
|
2016-04-20 17:54:13 +02:00
|
|
|
MCSectionSubPair P = getCurrentSection();
|
|
|
|
SwitchSection(&Section);
|
|
|
|
|
2017-02-06 20:35:46 +01:00
|
|
|
if (ELFSymbol->isUndefined(false)) {
|
|
|
|
EmitValueToAlignment(ByteAlignment, 0, 1, 0);
|
|
|
|
EmitLabel(Symbol);
|
|
|
|
EmitZeros(Size);
|
|
|
|
}
|
2016-04-20 17:54:13 +02:00
|
|
|
|
|
|
|
// Update the maximum alignment of the section if necessary.
|
|
|
|
if (ByteAlignment > Section.getAlignment())
|
|
|
|
Section.setAlignment(ByteAlignment);
|
2015-06-17 05:06:16 +02:00
|
|
|
|
2016-04-20 17:54:13 +02:00
|
|
|
SwitchSection(P.first, P.second);
|
2015-06-17 05:06:16 +02:00
|
|
|
} else {
|
|
|
|
if (ELFSymbol->declareCommon(Size, ByteAlignment))
|
|
|
|
report_fatal_error("Symbol: " + Symbol->getName() +
|
|
|
|
" redeclared as different type");
|
|
|
|
if ((AccessSize) && (Size <= GPSize)) {
|
|
|
|
uint64_t SectionIndex =
|
|
|
|
(AccessSize <= GPSize)
|
|
|
|
? ELF::SHN_HEXAGON_SCOMMON + (Log2_64(AccessSize) + 1)
|
|
|
|
: (unsigned)ELF::SHN_HEXAGON_SCOMMON;
|
|
|
|
ELFSymbol->setIndex(SectionIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ELFSymbol->setSize(MCConstantExpr::create(Size, getContext()));
|
|
|
|
}
|
|
|
|
|
2017-02-06 20:35:46 +01:00
|
|
|
void HexagonMCELFStreamer::HexagonMCEmitLocalCommonSymbol(MCSymbol *Symbol,
|
|
|
|
uint64_t Size,
|
|
|
|
unsigned ByteAlignment,
|
|
|
|
unsigned AccessSize) {
|
2015-06-17 05:06:16 +02:00
|
|
|
getAssembler().registerSymbol(*Symbol);
|
|
|
|
auto ELFSymbol = cast<MCSymbolELF>(Symbol);
|
|
|
|
ELFSymbol->setBinding(ELF::STB_LOCAL);
|
|
|
|
ELFSymbol->setExternal(false);
|
|
|
|
HexagonMCEmitCommonSymbol(Symbol, Size, ByteAlignment, AccessSize);
|
|
|
|
}
|
|
|
|
|
2016-12-17 02:09:05 +01:00
|
|
|
|
2017-02-06 20:35:46 +01:00
|
|
|
namespace llvm {
|
|
|
|
MCStreamer *createHexagonELFStreamer(Triple const &TT, MCContext &Context,
|
|
|
|
MCAsmBackend &MAB,
|
|
|
|
raw_pwrite_stream &OS, MCCodeEmitter *CE) {
|
|
|
|
return new HexagonMCELFStreamer(Context, MAB, OS, CE);
|
|
|
|
}
|
2016-12-17 02:09:05 +01:00
|
|
|
|
|
|
|
} // end namespace llvm
|