mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-08-20 10:30:17 +00:00
MC: Factor out an MCObjectStreamer class, which will be shared by the concrete
object file format writers. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@106140 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
43
include/llvm/MC/MCObjectStreamer.h
Normal file
43
include/llvm/MC/MCObjectStreamer.h
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
//===- MCObjectStreamer.h - MCStreamer Object File Interface ----*- C++ -*-===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#ifndef LLVM_MC_MCOBJECTSTREAMER_H
|
||||||
|
#define LLVM_MC_MCOBJECTSTREAMER_H
|
||||||
|
|
||||||
|
#include "llvm/MC/MCStreamer.h"
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
class MCAssembler;
|
||||||
|
class MCCodeEmitter;
|
||||||
|
class MCSectionData;
|
||||||
|
class TargetAsmBackend;
|
||||||
|
class raw_ostream;
|
||||||
|
|
||||||
|
/// \brief Streaming object file generation interface.
|
||||||
|
///
|
||||||
|
/// This class provides an implementation of the MCStreamer interface which is
|
||||||
|
/// suitable for use with the assembler backend. Specific object file formats
|
||||||
|
/// are expected to subclass this interface to implement directives specific
|
||||||
|
/// to that file format or custom semantics expected by the object writer
|
||||||
|
/// implementation.
|
||||||
|
class MCObjectStreamer : public MCStreamer {
|
||||||
|
MCAssembler *Assembler;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
|
||||||
|
raw_ostream &_OS, MCCodeEmitter *_Emitter);
|
||||||
|
~MCObjectStreamer();
|
||||||
|
|
||||||
|
public:
|
||||||
|
MCAssembler &getAssembler() { return *Assembler; }
|
||||||
|
};
|
||||||
|
|
||||||
|
} // end namespace llvm
|
||||||
|
|
||||||
|
#endif
|
@@ -14,6 +14,7 @@ add_llvm_library(LLVMMC
|
|||||||
MCLoggingStreamer.cpp
|
MCLoggingStreamer.cpp
|
||||||
MCMachOStreamer.cpp
|
MCMachOStreamer.cpp
|
||||||
MCNullStreamer.cpp
|
MCNullStreamer.cpp
|
||||||
|
MCObjectStreamer.cpp
|
||||||
MCObjectWriter.cpp
|
MCObjectWriter.cpp
|
||||||
MCSection.cpp
|
MCSection.cpp
|
||||||
MCSectionCOFF.cpp
|
MCSectionCOFF.cpp
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
#include "llvm/MC/MCCodeEmitter.h"
|
#include "llvm/MC/MCCodeEmitter.h"
|
||||||
#include "llvm/MC/MCExpr.h"
|
#include "llvm/MC/MCExpr.h"
|
||||||
#include "llvm/MC/MCInst.h"
|
#include "llvm/MC/MCInst.h"
|
||||||
|
#include "llvm/MC/MCObjectStreamer.h"
|
||||||
#include "llvm/MC/MCSection.h"
|
#include "llvm/MC/MCSection.h"
|
||||||
#include "llvm/MC/MCSymbol.h"
|
#include "llvm/MC/MCSymbol.h"
|
||||||
#include "llvm/MC/MCMachOSymbolFlags.h"
|
#include "llvm/MC/MCMachOSymbolFlags.h"
|
||||||
@@ -25,10 +26,9 @@ using namespace llvm;
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
class MCMachOStreamer : public MCStreamer {
|
class MCMachOStreamer : public MCObjectStreamer {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
MCAssembler Assembler;
|
|
||||||
MCSectionData *CurSectionData;
|
MCSectionData *CurSectionData;
|
||||||
|
|
||||||
/// Track the current atom for each section.
|
/// Track the current atom for each section.
|
||||||
@@ -65,12 +65,8 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
|
MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
|
||||||
raw_ostream &_OS, MCCodeEmitter *_Emitter)
|
raw_ostream &OS, MCCodeEmitter *Emitter)
|
||||||
: MCStreamer(Context), Assembler(Context, TAB, *_Emitter, _OS),
|
: MCObjectStreamer(Context, TAB, OS, Emitter), CurSectionData(0) {}
|
||||||
CurSectionData(0) {}
|
|
||||||
~MCMachOStreamer() {}
|
|
||||||
|
|
||||||
MCAssembler &getAssembler() { return Assembler; }
|
|
||||||
|
|
||||||
const MCExpr *AddValueSymbols(const MCExpr *Value) {
|
const MCExpr *AddValueSymbols(const MCExpr *Value) {
|
||||||
switch (Value->getKind()) {
|
switch (Value->getKind()) {
|
||||||
@@ -86,7 +82,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
case MCExpr::SymbolRef:
|
case MCExpr::SymbolRef:
|
||||||
Assembler.getOrCreateSymbolData(
|
getAssembler().getOrCreateSymbolData(
|
||||||
cast<MCSymbolRefExpr>(Value)->getSymbol());
|
cast<MCSymbolRefExpr>(Value)->getSymbol());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -166,7 +162,7 @@ void MCMachOStreamer::SwitchSection(const MCSection *Section) {
|
|||||||
if (Section == CurSection) return;
|
if (Section == CurSection) return;
|
||||||
|
|
||||||
CurSection = Section;
|
CurSection = Section;
|
||||||
CurSectionData = &Assembler.getOrCreateSectionData(*Section);
|
CurSectionData = &getAssembler().getOrCreateSectionData(*Section);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
|
void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
|
||||||
@@ -174,11 +170,11 @@ void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
|
|||||||
assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
|
assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
|
||||||
assert(CurSection && "Cannot emit before setting section!");
|
assert(CurSection && "Cannot emit before setting section!");
|
||||||
|
|
||||||
MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
|
||||||
|
|
||||||
// Update the current atom map, if necessary.
|
// Update the current atom map, if necessary.
|
||||||
bool MustCreateFragment = false;
|
bool MustCreateFragment = false;
|
||||||
if (Assembler.isSymbolLinkerVisible(&SD)) {
|
if (getAssembler().isSymbolLinkerVisible(&SD)) {
|
||||||
CurrentAtomMap[CurSectionData] = &SD;
|
CurrentAtomMap[CurSectionData] = &SD;
|
||||||
|
|
||||||
// We have to create a new fragment, fragments cannot span atoms.
|
// We have to create a new fragment, fragments cannot span atoms.
|
||||||
@@ -210,7 +206,7 @@ void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
|
|||||||
void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
|
void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
|
||||||
switch (Flag) {
|
switch (Flag) {
|
||||||
case MCAF_SubsectionsViaSymbols:
|
case MCAF_SubsectionsViaSymbols:
|
||||||
Assembler.setSubsectionsViaSymbols(true);
|
getAssembler().setSubsectionsViaSymbols(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,7 +215,7 @@ void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
|
|||||||
|
|
||||||
void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
|
void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
|
||||||
// FIXME: Lift context changes into super class.
|
// FIXME: Lift context changes into super class.
|
||||||
Assembler.getOrCreateSymbolData(*Symbol);
|
getAssembler().getOrCreateSymbolData(*Symbol);
|
||||||
Symbol->setVariableValue(AddValueSymbols(Value));
|
Symbol->setVariableValue(AddValueSymbols(Value));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,14 +229,14 @@ void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
|
|||||||
IndirectSymbolData ISD;
|
IndirectSymbolData ISD;
|
||||||
ISD.Symbol = Symbol;
|
ISD.Symbol = Symbol;
|
||||||
ISD.SectionData = CurSectionData;
|
ISD.SectionData = CurSectionData;
|
||||||
Assembler.getIndirectSymbols().push_back(ISD);
|
getAssembler().getIndirectSymbols().push_back(ISD);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adding a symbol attribute always introduces the symbol, note that an
|
// Adding a symbol attribute always introduces the symbol, note that an
|
||||||
// important side effect of calling getOrCreateSymbolData here is to register
|
// important side effect of calling getOrCreateSymbolData here is to register
|
||||||
// the symbol with the assembler.
|
// the symbol with the assembler.
|
||||||
MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
|
||||||
|
|
||||||
// The implementation of symbol attributes is designed to match 'as', but it
|
// The implementation of symbol attributes is designed to match 'as', but it
|
||||||
// leaves much to desired. It doesn't really make sense to arbitrarily add and
|
// leaves much to desired. It doesn't really make sense to arbitrarily add and
|
||||||
@@ -313,7 +309,8 @@ void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
|
|||||||
// Encode the 'desc' value into the lowest implementation defined bits.
|
// Encode the 'desc' value into the lowest implementation defined bits.
|
||||||
assert(DescValue == (DescValue & SF_DescFlagsMask) &&
|
assert(DescValue == (DescValue & SF_DescFlagsMask) &&
|
||||||
"Invalid .desc value!");
|
"Invalid .desc value!");
|
||||||
Assembler.getOrCreateSymbolData(*Symbol).setFlags(DescValue&SF_DescFlagsMask);
|
getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
|
||||||
|
DescValue & SF_DescFlagsMask);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
||||||
@@ -321,14 +318,14 @@ void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
|
|||||||
// FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
|
// FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
|
||||||
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
|
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
|
||||||
|
|
||||||
MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
|
||||||
SD.setExternal(true);
|
SD.setExternal(true);
|
||||||
SD.setCommon(Size, ByteAlignment);
|
SD.setCommon(Size, ByteAlignment);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
|
void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
|
||||||
unsigned Size, unsigned ByteAlignment) {
|
unsigned Size, unsigned ByteAlignment) {
|
||||||
MCSectionData &SectData = Assembler.getOrCreateSectionData(*Section);
|
MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
|
||||||
|
|
||||||
// The symbol may not be present, which only creates the section.
|
// The symbol may not be present, which only creates the section.
|
||||||
if (!Symbol)
|
if (!Symbol)
|
||||||
@@ -338,7 +335,7 @@ void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
|
|||||||
|
|
||||||
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
|
assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
|
||||||
|
|
||||||
MCSymbolData &SD = Assembler.getOrCreateSymbolData(*Symbol);
|
MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
|
||||||
|
|
||||||
// Emit an align fragment if necessary.
|
// Emit an align fragment if necessary.
|
||||||
if (ByteAlignment != 1)
|
if (ByteAlignment != 1)
|
||||||
@@ -346,7 +343,7 @@ void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
|
|||||||
|
|
||||||
MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
|
MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
|
||||||
SD.setFragment(F);
|
SD.setFragment(F);
|
||||||
if (Assembler.isSymbolLinkerVisible(&SD))
|
if (getAssembler().isSymbolLinkerVisible(&SD))
|
||||||
F->setAtom(&SD);
|
F->setAtom(&SD);
|
||||||
|
|
||||||
Symbol->setSection(*Section);
|
Symbol->setSection(*Section);
|
||||||
@@ -431,7 +428,7 @@ void MCMachOStreamer::EmitInstToFragment(const MCInst &Inst) {
|
|||||||
SmallVector<MCFixup, 4> Fixups;
|
SmallVector<MCFixup, 4> Fixups;
|
||||||
SmallString<256> Code;
|
SmallString<256> Code;
|
||||||
raw_svector_ostream VecOS(Code);
|
raw_svector_ostream VecOS(Code);
|
||||||
Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
|
getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
|
||||||
VecOS.flush();
|
VecOS.flush();
|
||||||
|
|
||||||
IF->getCode() = Code;
|
IF->getCode() = Code;
|
||||||
@@ -444,7 +441,7 @@ void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
|
|||||||
SmallVector<MCFixup, 4> Fixups;
|
SmallVector<MCFixup, 4> Fixups;
|
||||||
SmallString<256> Code;
|
SmallString<256> Code;
|
||||||
raw_svector_ostream VecOS(Code);
|
raw_svector_ostream VecOS(Code);
|
||||||
Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
|
getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
|
||||||
VecOS.flush();
|
VecOS.flush();
|
||||||
|
|
||||||
// Add the fixups and data.
|
// Add the fixups and data.
|
||||||
@@ -464,18 +461,18 @@ void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
|
|||||||
CurSectionData->setHasInstructions(true);
|
CurSectionData->setHasInstructions(true);
|
||||||
|
|
||||||
// If this instruction doesn't need relaxation, just emit it as data.
|
// If this instruction doesn't need relaxation, just emit it as data.
|
||||||
if (!Assembler.getBackend().MayNeedRelaxation(Inst)) {
|
if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
|
||||||
EmitInstToData(Inst);
|
EmitInstToData(Inst);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, if we are relaxing everything, relax the instruction as much as
|
// Otherwise, if we are relaxing everything, relax the instruction as much as
|
||||||
// possible and emit it as data.
|
// possible and emit it as data.
|
||||||
if (Assembler.getRelaxAll()) {
|
if (getAssembler().getRelaxAll()) {
|
||||||
MCInst Relaxed;
|
MCInst Relaxed;
|
||||||
Assembler.getBackend().RelaxInstruction(Inst, Relaxed);
|
getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
|
||||||
while (Assembler.getBackend().MayNeedRelaxation(Relaxed))
|
while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
|
||||||
Assembler.getBackend().RelaxInstruction(Relaxed, Relaxed);
|
getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
|
||||||
EmitInstToData(Relaxed);
|
EmitInstToData(Relaxed);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -485,7 +482,7 @@ void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void MCMachOStreamer::Finish() {
|
void MCMachOStreamer::Finish() {
|
||||||
Assembler.Finish();
|
getAssembler().Finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
|
MCStreamer *llvm::createMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
|
||||||
|
24
lib/MC/MCObjectStreamer.cpp
Normal file
24
lib/MC/MCObjectStreamer.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
//===- lib/MC/MCObjectStreamer.cpp - Object File MCStreamer Interface -----===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#include "llvm/MC/MCObjectStreamer.h"
|
||||||
|
|
||||||
|
#include "llvm/MC/MCAssembler.h"
|
||||||
|
using namespace llvm;
|
||||||
|
|
||||||
|
MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
|
||||||
|
raw_ostream &_OS, MCCodeEmitter *_Emitter)
|
||||||
|
: MCStreamer(Context),
|
||||||
|
Assembler(new MCAssembler(Context, TAB, *_Emitter, _OS))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
MCObjectStreamer::~MCObjectStreamer() {
|
||||||
|
delete Assembler;
|
||||||
|
}
|
Reference in New Issue
Block a user