2010-11-17 16:06:47 +00:00
|
|
|
//===- lib/MC/MCPureStreamer.cpp - MC "Pure" Object Output ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/MC/MCAssembler.h"
|
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
2013-04-14 21:18:36 +00:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2010-11-17 16:06:47 +00:00
|
|
|
#include "llvm/MC/MCObjectStreamer.h"
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class MCPureStreamer : public MCObjectStreamer {
|
|
|
|
private:
|
|
|
|
virtual void EmitInstToFragment(const MCInst &Inst);
|
|
|
|
virtual void EmitInstToData(const MCInst &Inst);
|
|
|
|
|
|
|
|
public:
|
2013-01-31 23:29:57 +00:00
|
|
|
MCPureStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
|
|
|
|
MCCodeEmitter *Emitter)
|
|
|
|
: MCObjectStreamer(SK_PureStreamer, Context, TAB, OS, Emitter) {}
|
2010-11-17 16:06:47 +00:00
|
|
|
|
|
|
|
/// @name MCStreamer Interface
|
|
|
|
/// @{
|
|
|
|
|
|
|
|
virtual void InitSections();
|
2013-01-14 19:04:57 +00:00
|
|
|
virtual void InitToTextSection();
|
2010-11-17 16:06:47 +00:00
|
|
|
virtual void EmitLabel(MCSymbol *Symbol);
|
2012-12-17 23:41:45 +00:00
|
|
|
virtual void EmitDebugLabel(MCSymbol *Symbol);
|
2010-11-17 16:06:47 +00:00
|
|
|
virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
|
2012-06-22 20:14:46 +00:00
|
|
|
uint64_t Size = 0, unsigned ByteAlignment = 0);
|
2013-07-02 15:49:13 +00:00
|
|
|
virtual void EmitBytes(StringRef Data);
|
2010-11-17 16:06:47 +00:00
|
|
|
virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
|
|
|
|
unsigned ValueSize = 1,
|
|
|
|
unsigned MaxBytesToEmit = 0);
|
|
|
|
virtual void EmitCodeAlignment(unsigned ByteAlignment,
|
|
|
|
unsigned MaxBytesToEmit = 0);
|
2012-01-27 00:37:08 +00:00
|
|
|
virtual bool EmitValueToOffset(const MCExpr *Offset,
|
2010-11-17 16:06:47 +00:00
|
|
|
unsigned char Value = 0);
|
2012-01-07 03:13:18 +00:00
|
|
|
virtual void FinishImpl();
|
2010-11-17 16:06:47 +00:00
|
|
|
|
|
|
|
|
2013-08-09 01:52:03 +00:00
|
|
|
virtual bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
|
2010-11-17 16:06:47 +00:00
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
2013-08-09 01:52:03 +00:00
|
|
|
return false;
|
2010-11-17 16:06:47 +00:00
|
|
|
}
|
|
|
|
virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
|
|
|
|
uint64_t Size, unsigned ByteAlignment = 0) {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|
|
|
unsigned ByteAlignment) {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
virtual void EmitThumbFunc(MCSymbol *Func) {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
virtual void EmitCOFFSymbolType(int Type) {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
virtual void EndCOFFSymbolDef() {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
2011-09-01 23:04:27 +00:00
|
|
|
virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|
|
|
unsigned ByteAlignment) {
|
2010-11-17 16:06:47 +00:00
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
virtual void EmitFileDirective(StringRef Filename) {
|
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
2011-10-17 23:05:28 +00:00
|
|
|
virtual bool EmitDwarfFileDirective(unsigned FileNo, StringRef Directory,
|
2013-03-07 01:42:00 +00:00
|
|
|
StringRef Filename, unsigned CUID = 0) {
|
2010-11-17 16:06:47 +00:00
|
|
|
report_fatal_error("unsupported directive in pure streamer");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @}
|
2013-01-31 23:29:57 +00:00
|
|
|
|
|
|
|
static bool classof(const MCStreamer *S) {
|
|
|
|
return S->getKind() == SK_PureStreamer;
|
|
|
|
}
|
2010-11-17 16:06:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace.
|
|
|
|
|
|
|
|
void MCPureStreamer::InitSections() {
|
2013-01-14 19:04:57 +00:00
|
|
|
InitToTextSection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCPureStreamer::InitToTextSection() {
|
2013-04-14 21:18:36 +00:00
|
|
|
SwitchSection(getContext().getObjectFileInfo()->getTextSection());
|
2010-11-17 16:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCPureStreamer::EmitLabel(MCSymbol *Symbol) {
|
|
|
|
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
|
|
|
|
assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
|
2013-04-17 21:18:16 +00:00
|
|
|
assert(getCurrentSection().first && "Cannot emit before setting section!");
|
2010-11-17 16:06:47 +00:00
|
|
|
|
2013-09-19 23:21:01 +00:00
|
|
|
AssignSection(Symbol, getCurrentSection().first);
|
2010-11-17 16:06:47 +00:00
|
|
|
|
|
|
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
|
|
|
|
|
|
|
|
// We have to create a new fragment if this is an atom defining symbol,
|
|
|
|
// fragments cannot span atoms.
|
|
|
|
if (getAssembler().isSymbolLinkerVisible(SD.getSymbol()))
|
2013-04-17 21:18:16 +00:00
|
|
|
insert(new MCDataFragment());
|
2010-11-17 16:06:47 +00:00
|
|
|
|
|
|
|
// FIXME: This is wasteful, we don't necessarily need to create a data
|
|
|
|
// fragment. Instead, we should mark the symbol as pointing into the data
|
|
|
|
// fragment if it exists, otherwise we should just queue the label and set its
|
|
|
|
// fragment pointer when we emit the next fragment.
|
|
|
|
MCDataFragment *F = getOrCreateDataFragment();
|
|
|
|
assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
|
|
|
|
SD.setFragment(F);
|
|
|
|
SD.setOffset(F->getContents().size());
|
|
|
|
}
|
|
|
|
|
2012-12-16 04:00:45 +00:00
|
|
|
|
2012-12-17 23:41:45 +00:00
|
|
|
void MCPureStreamer::EmitDebugLabel(MCSymbol *Symbol) {
|
|
|
|
EmitLabel(Symbol);
|
|
|
|
}
|
|
|
|
|
2010-11-17 16:06:47 +00:00
|
|
|
void MCPureStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
|
2012-06-22 20:14:46 +00:00
|
|
|
uint64_t Size, unsigned ByteAlignment) {
|
2010-11-17 16:06:47 +00:00
|
|
|
report_fatal_error("not yet implemented in pure streamer");
|
|
|
|
}
|
|
|
|
|
2013-07-02 15:49:13 +00:00
|
|
|
void MCPureStreamer::EmitBytes(StringRef Data) {
|
2010-11-17 16:06:47 +00:00
|
|
|
// TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
|
|
|
|
// MCObjectStreamer.
|
|
|
|
getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCPureStreamer::EmitValueToAlignment(unsigned ByteAlignment,
|
|
|
|
int64_t Value, unsigned ValueSize,
|
|
|
|
unsigned MaxBytesToEmit) {
|
|
|
|
// TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
|
|
|
|
// MCObjectStreamer.
|
|
|
|
if (MaxBytesToEmit == 0)
|
|
|
|
MaxBytesToEmit = ByteAlignment;
|
2013-04-17 21:18:16 +00:00
|
|
|
insert(new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit));
|
2010-11-17 16:06:47 +00:00
|
|
|
|
|
|
|
// Update the maximum alignment on the current section if necessary.
|
|
|
|
if (ByteAlignment > getCurrentSectionData()->getAlignment())
|
|
|
|
getCurrentSectionData()->setAlignment(ByteAlignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCPureStreamer::EmitCodeAlignment(unsigned ByteAlignment,
|
|
|
|
unsigned MaxBytesToEmit) {
|
|
|
|
// TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
|
|
|
|
// MCObjectStreamer.
|
|
|
|
if (MaxBytesToEmit == 0)
|
|
|
|
MaxBytesToEmit = ByteAlignment;
|
2013-04-17 21:18:16 +00:00
|
|
|
MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit);
|
|
|
|
insert(F);
|
2010-11-17 16:06:47 +00:00
|
|
|
F->setEmitNops(true);
|
|
|
|
|
|
|
|
// Update the maximum alignment on the current section if necessary.
|
|
|
|
if (ByteAlignment > getCurrentSectionData()->getAlignment())
|
|
|
|
getCurrentSectionData()->setAlignment(ByteAlignment);
|
|
|
|
}
|
|
|
|
|
2012-01-27 00:37:08 +00:00
|
|
|
bool MCPureStreamer::EmitValueToOffset(const MCExpr *Offset,
|
2010-11-17 16:06:47 +00:00
|
|
|
unsigned char Value) {
|
2013-04-17 21:18:16 +00:00
|
|
|
insert(new MCOrgFragment(*Offset, Value));
|
2012-01-27 00:37:08 +00:00
|
|
|
return false;
|
2010-11-17 16:06:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCPureStreamer::EmitInstToFragment(const MCInst &Inst) {
|
2013-04-17 21:18:16 +00:00
|
|
|
MCRelaxableFragment *IF = new MCRelaxableFragment(Inst);
|
|
|
|
insert(IF);
|
2010-11-17 16:06:47 +00:00
|
|
|
|
|
|
|
// Add the fixups and data.
|
|
|
|
//
|
|
|
|
// FIXME: Revisit this design decision when relaxation is done, we may be
|
|
|
|
// able to get away with not storing any extra data in the MCInst.
|
|
|
|
SmallVector<MCFixup, 4> Fixups;
|
|
|
|
SmallString<256> Code;
|
|
|
|
raw_svector_ostream VecOS(Code);
|
|
|
|
getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
|
|
|
|
VecOS.flush();
|
|
|
|
|
2012-12-07 19:13:57 +00:00
|
|
|
IF->getContents() = Code;
|
2010-11-17 16:06:47 +00:00
|
|
|
IF->getFixups() = Fixups;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MCPureStreamer::EmitInstToData(const MCInst &Inst) {
|
|
|
|
MCDataFragment *DF = getOrCreateDataFragment();
|
|
|
|
|
|
|
|
SmallVector<MCFixup, 4> Fixups;
|
|
|
|
SmallString<256> Code;
|
|
|
|
raw_svector_ostream VecOS(Code);
|
|
|
|
getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
|
|
|
|
VecOS.flush();
|
|
|
|
|
|
|
|
// Add the fixups and data.
|
|
|
|
for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
|
|
|
|
Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
|
2012-12-07 19:13:57 +00:00
|
|
|
DF->getFixups().push_back(Fixups[i]);
|
2010-11-17 16:06:47 +00:00
|
|
|
}
|
|
|
|
DF->getContents().append(Code.begin(), Code.end());
|
|
|
|
}
|
|
|
|
|
2012-01-07 03:13:18 +00:00
|
|
|
void MCPureStreamer::FinishImpl() {
|
2010-11-17 16:06:47 +00:00
|
|
|
// FIXME: Handle DWARF tables?
|
|
|
|
|
2012-01-07 03:13:18 +00:00
|
|
|
this->MCObjectStreamer::FinishImpl();
|
2010-11-17 16:06:47 +00:00
|
|
|
}
|
|
|
|
|
2011-07-25 23:24:55 +00:00
|
|
|
MCStreamer *llvm::createPureStreamer(MCContext &Context, MCAsmBackend &MAB,
|
2010-11-17 16:06:47 +00:00
|
|
|
raw_ostream &OS, MCCodeEmitter *CE) {
|
2011-07-25 23:24:55 +00:00
|
|
|
return new MCPureStreamer(Context, MAB, OS, CE);
|
2010-11-17 16:06:47 +00:00
|
|
|
}
|