refactor code so that LLVMTargetMachine creates the asmstreamer and

mccontext instead of having AsmPrinter do it.  This allows other 
types of MCStreamer's to be passed in.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@95155 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2010-02-02 23:37:42 +00:00
parent 9f34dd305b
commit 56591ab218
19 changed files with 97 additions and 56 deletions

View File

@ -149,7 +149,8 @@ namespace llvm {
protected: protected:
explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM, explicit AsmPrinter(formatted_raw_ostream &o, TargetMachine &TM,
const MCAsmInfo *T, bool V); MCContext &Ctx, MCStreamer &Streamer,
const MCAsmInfo *T);
public: public:
virtual ~AsmPrinter(); virtual ~AsmPrinter();

View File

@ -60,6 +60,10 @@ namespace llvm {
/// @name Assembly File Formatting. /// @name Assembly File Formatting.
/// @{ /// @{
/// isVerboseAsm - Return true if this streamer supports verbose assembly at
/// all.
virtual bool isVerboseAsm() const { return false; }
/// AddComment - Add a comment that can be emitted to the generated .s /// AddComment - Add a comment that can be emitted to the generated .s
/// file if applicable as a QoI issue to make the output of the compiler /// file if applicable as a QoI issue to make the output of the compiler

View File

@ -25,12 +25,14 @@
namespace llvm { namespace llvm {
class AsmPrinter; class AsmPrinter;
class MCAsmParser;
class MCCodeEmitter;
class Module; class Module;
class MCAsmInfo; class MCAsmInfo;
class MCAsmParser;
class MCCodeEmitter;
class MCContext;
class MCDisassembler; class MCDisassembler;
class MCInstPrinter; class MCInstPrinter;
class MCStreamer;
class TargetAsmLexer; class TargetAsmLexer;
class TargetAsmParser; class TargetAsmParser;
class TargetMachine; class TargetMachine;
@ -58,8 +60,9 @@ namespace llvm {
const std::string &Features); const std::string &Features);
typedef AsmPrinter *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS, typedef AsmPrinter *(*AsmPrinterCtorTy)(formatted_raw_ostream &OS,
TargetMachine &TM, TargetMachine &TM,
const MCAsmInfo *MAI, MCContext &Ctx,
bool VerboseAsm); MCStreamer &Streamer,
const MCAsmInfo *MAI);
typedef TargetAsmLexer *(*AsmLexerCtorTy)(const Target &T, typedef TargetAsmLexer *(*AsmLexerCtorTy)(const Target &T,
const MCAsmInfo &MAI); const MCAsmInfo &MAI);
typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,MCAsmParser &P); typedef TargetAsmParser *(*AsmParserCtorTy)(const Target &T,MCAsmParser &P);
@ -189,12 +192,14 @@ namespace llvm {
return TargetMachineCtorFn(*this, Triple, Features); return TargetMachineCtorFn(*this, Triple, Features);
} }
/// createAsmPrinter - Create a target specific assembly printer pass. /// createAsmPrinter - Create a target specific assembly printer pass. This
/// takes ownership of the MCContext and MCStreamer objects but not the MAI.
AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM, AsmPrinter *createAsmPrinter(formatted_raw_ostream &OS, TargetMachine &TM,
const MCAsmInfo *MAI, bool Verbose) const { MCContext &Ctx, MCStreamer &Streamer,
const MCAsmInfo *MAI) const {
if (!AsmPrinterCtorFn) if (!AsmPrinterCtorFn)
return 0; return 0;
return AsmPrinterCtorFn(OS, TM, MAI, Verbose); return AsmPrinterCtorFn(OS, TM, Ctx, Streamer, MAI);
} }
/// createAsmLexer - Create a target specific assembly lexer. /// createAsmLexer - Create a target specific assembly lexer.
@ -547,8 +552,9 @@ namespace llvm {
private: private:
static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM, static AsmPrinter *Allocator(formatted_raw_ostream &OS, TargetMachine &TM,
const MCAsmInfo *MAI, bool Verbose) { MCContext &Ctx, MCStreamer &Streamer,
return new AsmPrinterImpl(OS, TM, MAI, Verbose); const MCAsmInfo *MAI) {
return new AsmPrinterImpl(OS, TM, Ctx, Streamer, MAI);
} }
}; };

View File

@ -55,19 +55,14 @@ STATISTIC(EmittedInsts, "Number of machine instrs printed");
char AsmPrinter::ID = 0; char AsmPrinter::ID = 0;
AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm, AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
const MCAsmInfo *T, bool VerboseAsm) MCContext &Ctx, MCStreamer &Streamer,
const MCAsmInfo *T)
: MachineFunctionPass(&ID), O(o), : MachineFunctionPass(&ID), O(o),
TM(tm), MAI(T), TRI(tm.getRegisterInfo()), TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
OutContext(Ctx), OutStreamer(Streamer),
OutContext(*new MCContext()),
// FIXME: Pass instprinter to streamer.
OutStreamer(*createAsmStreamer(OutContext, O, *T,
TM.getTargetData()->isLittleEndian(),
VerboseAsm, 0)),
LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) { LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) {
DW = 0; MMI = 0; DW = 0; MMI = 0;
this->VerboseAsm = VerboseAsm; VerboseAsm = Streamer.isVerboseAsm();
} }
AsmPrinter::~AsmPrinter() { AsmPrinter::~AsmPrinter() {

View File

@ -21,6 +21,9 @@
#include "llvm/CodeGen/MachineFunctionAnalysis.h" #include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/Target/TargetOptions.h" #include "llvm/Target/TargetOptions.h"
#include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Target/TargetRegistry.h" #include "llvm/Target/TargetRegistry.h"
#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar.h"
#include "llvm/Support/CommandLine.h" #include "llvm/Support/CommandLine.h"
@ -121,10 +124,24 @@ LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
case CGFT_ObjectFile: case CGFT_ObjectFile:
return CGFT_ErrorOccurred; return CGFT_ErrorOccurred;
case CGFT_AssemblyFile: { case CGFT_AssemblyFile: {
MCContext *Context = new MCContext();
MCStreamer *AsmStreamer =
createAsmStreamer(*Context, Out, *getMCAsmInfo(),
getTargetData()->isLittleEndian(),
getVerboseAsm(),
/*instprinter*/0,
/*codeemitter*/0);
// Create the AsmPrinter, which takes ownership of Context and AsmStreamer
// if successful.
FunctionPass *Printer = FunctionPass *Printer =
getTarget().createAsmPrinter(Out, *this, getMCAsmInfo(), getTarget().createAsmPrinter(Out, *this, *Context, *AsmStreamer,
getVerboseAsm()); getMCAsmInfo());
if (Printer == 0) return CGFT_ErrorOccurred; if (Printer == 0) {
delete AsmStreamer;
delete Context;
return CGFT_ErrorOccurred;
}
PM.add(Printer); PM.add(Printer);
break; break;
} }

View File

@ -47,7 +47,6 @@ public:
bool isLittleEndian() const { return IsLittleEndian; } bool isLittleEndian() const { return IsLittleEndian; }
inline void EmitEOL() { inline void EmitEOL() {
// If we don't have any comments, just emit a \n. // If we don't have any comments, just emit a \n.
if (!IsVerboseAsm) { if (!IsVerboseAsm) {
@ -57,6 +56,10 @@ public:
EmitCommentsAndEOL(); EmitCommentsAndEOL();
} }
void EmitCommentsAndEOL(); void EmitCommentsAndEOL();
/// isVerboseAsm - Return true if this streamer supports verbose assembly at
/// all.
virtual bool isVerboseAsm() const { return IsVerboseAsm; }
/// AddComment - Add a comment that can be emitted to the generated .s /// AddComment - Add a comment that can be emitted to the generated .s
/// file if applicable as a QoI issue to make the output of the compiler /// file if applicable as a QoI issue to make the output of the compiler

View File

@ -73,8 +73,9 @@ namespace {
public: public:
explicit ARMAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, explicit ARMAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(O, TM, T, V), AFI(NULL), MCP(NULL) { const MCAsmInfo *T)
: AsmPrinter(O, TM, Ctx, Streamer, T), AFI(NULL), MCP(NULL) {
Subtarget = &TM.getSubtarget<ARMSubtarget>(); Subtarget = &TM.getSubtarget<ARMSubtarget>();
} }

View File

@ -37,8 +37,9 @@ namespace {
/// ///
explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm, explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
const MCAsmInfo *T, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(o, tm, T, V) {} const MCAsmInfo *T)
: AsmPrinter(o, tm, Ctx, Streamer, T) {}
virtual const char *getPassName() const { virtual const char *getPassName() const {
return "Alpha Assembly Printer"; return "Alpha Assembly Printer";

View File

@ -39,8 +39,9 @@ namespace {
class BlackfinAsmPrinter : public AsmPrinter { class BlackfinAsmPrinter : public AsmPrinter {
public: public:
BlackfinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, BlackfinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *MAI, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(O, TM, MAI, V) {} const MCAsmInfo *MAI)
: AsmPrinter(O, TM, Ctx, Streamer, MAI) {}
virtual const char *getPassName() const { virtual const char *getPassName() const {
return "Blackfin Assembly Printer"; return "Blackfin Assembly Printer";

View File

@ -38,8 +38,9 @@ namespace {
class SPUAsmPrinter : public AsmPrinter { class SPUAsmPrinter : public AsmPrinter {
public: public:
explicit SPUAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, explicit SPUAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V) : MCContext &Ctx, MCStreamer &Streamer,
AsmPrinter(O, TM, T, V) {} const MCAsmInfo *T) :
AsmPrinter(O, TM, Ctx, Streamer, T) {}
virtual const char *getPassName() const { virtual const char *getPassName() const {
return "STI CBEA SPU Assembly Printer"; return "STI CBEA SPU Assembly Printer";

View File

@ -42,8 +42,9 @@ namespace {
class MSP430AsmPrinter : public AsmPrinter { class MSP430AsmPrinter : public AsmPrinter {
public: public:
MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *MAI, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(O, TM, MAI, V) {} const MCAsmInfo *MAI)
: AsmPrinter(O, TM, Ctx, Streamer, MAI) {}
virtual const char *getPassName() const { virtual const char *getPassName() const {
return "MSP430 Assembly Printer"; return "MSP430 Assembly Printer";

View File

@ -50,8 +50,9 @@ namespace {
const MipsSubtarget *Subtarget; const MipsSubtarget *Subtarget;
public: public:
explicit MipsAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, explicit MipsAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(O, TM, T, V) { const MCAsmInfo *T)
: AsmPrinter(O, TM, Ctx, Streamer, T) {
Subtarget = &TM.getSubtarget<MipsSubtarget>(); Subtarget = &TM.getSubtarget<MipsSubtarget>();
} }

View File

@ -35,8 +35,9 @@ using namespace llvm;
#include "PIC16GenAsmWriter.inc" #include "PIC16GenAsmWriter.inc"
PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, PIC16AsmPrinter::PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(O, TM, T, V), DbgInfo(O, T) { const MCAsmInfo *T)
: AsmPrinter(O, TM, Ctx, Streamer, T), DbgInfo(O, T) {
PTLI = static_cast<PIC16TargetLowering*>(TM.getTargetLowering()); PTLI = static_cast<PIC16TargetLowering*>(TM.getTargetLowering());
PMAI = static_cast<const PIC16MCAsmInfo*>(T); PMAI = static_cast<const PIC16MCAsmInfo*>(T);
PTOF = (PIC16TargetObjectFile *)&PTLI->getObjFileLowering(); PTOF = (PIC16TargetObjectFile *)&PTLI->getObjFileLowering();

View File

@ -31,7 +31,8 @@ namespace llvm {
class VISIBILITY_HIDDEN PIC16AsmPrinter : public AsmPrinter { class VISIBILITY_HIDDEN PIC16AsmPrinter : public AsmPrinter {
public: public:
explicit PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, explicit PIC16AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V); MCContext &Ctx, MCStreamer &Streamer,
const MCAsmInfo *T);
private: private:
virtual const char *getPassName() const { virtual const char *getPassName() const {
return "PIC16 Assembly Printer"; return "PIC16 Assembly Printer";

View File

@ -60,8 +60,9 @@ namespace {
uint64_t LabelID; uint64_t LabelID;
public: public:
explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, explicit PPCAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(O, TM, T, V), const MCAsmInfo *T)
: AsmPrinter(O, TM, Ctx, Streamer, T),
Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {} Subtarget(TM.getSubtarget<PPCSubtarget>()), LabelID(0) {}
virtual const char *getPassName() const { virtual const char *getPassName() const {
@ -322,8 +323,9 @@ namespace {
class PPCLinuxAsmPrinter : public PPCAsmPrinter { class PPCLinuxAsmPrinter : public PPCAsmPrinter {
public: public:
explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, explicit PPCLinuxAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V) MCContext &Ctx, MCStreamer &Streamer,
: PPCAsmPrinter(O, TM, T, V){} const MCAsmInfo *T)
: PPCAsmPrinter(O, TM, Ctx, Streamer, T) {}
virtual const char *getPassName() const { virtual const char *getPassName() const {
return "Linux PPC Assembly Printer"; return "Linux PPC Assembly Printer";
@ -347,8 +349,9 @@ namespace {
formatted_raw_ostream &OS; formatted_raw_ostream &OS;
public: public:
explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, explicit PPCDarwinAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V) MCContext &Ctx, MCStreamer &Streamer,
: PPCAsmPrinter(O, TM, T, V), OS(O) {} const MCAsmInfo *T)
: PPCAsmPrinter(O, TM, Ctx, Streamer, T), OS(O) {}
virtual const char *getPassName() const { virtual const char *getPassName() const {
return "Darwin PPC Assembly Printer"; return "Darwin PPC Assembly Printer";
@ -826,13 +829,13 @@ bool PPCDarwinAsmPrinter::doFinalization(Module &M) {
/// ///
static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o, static AsmPrinter *createPPCAsmPrinterPass(formatted_raw_ostream &o,
TargetMachine &tm, TargetMachine &tm,
const MCAsmInfo *tai, MCContext &Ctx, MCStreamer &Streamer,
bool verbose) { const MCAsmInfo *tai) {
const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>(); const PPCSubtarget *Subtarget = &tm.getSubtarget<PPCSubtarget>();
if (Subtarget->isDarwin()) if (Subtarget->isDarwin())
return new PPCDarwinAsmPrinter(o, tm, tai, verbose); return new PPCDarwinAsmPrinter(o, tm, Ctx, Streamer, tai);
return new PPCLinuxAsmPrinter(o, tm, tai, verbose); return new PPCLinuxAsmPrinter(o, tm, Ctx, Streamer, tai);
} }
// Force static initialization. // Force static initialization.

View File

@ -29,8 +29,9 @@ namespace {
class SparcAsmPrinter : public AsmPrinter { class SparcAsmPrinter : public AsmPrinter {
public: public:
explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, explicit SparcAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(O, TM, T, V) {} const MCAsmInfo *T)
: AsmPrinter(O, TM, Ctx, Streamer, T) {}
virtual const char *getPassName() const { virtual const char *getPassName() const {
return "Sparc Assembly Printer"; return "Sparc Assembly Printer";

View File

@ -40,8 +40,9 @@ namespace {
class SystemZAsmPrinter : public AsmPrinter { class SystemZAsmPrinter : public AsmPrinter {
public: public:
SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *MAI, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(O, TM, MAI, V) {} const MCAsmInfo *MAI)
: AsmPrinter(O, TM, Ctx, Streamer, MAI) {}
virtual const char *getPassName() const { virtual const char *getPassName() const {
return "SystemZ Assembly Printer"; return "SystemZ Assembly Printer";

View File

@ -36,8 +36,9 @@ class VISIBILITY_HIDDEN X86AsmPrinter : public AsmPrinter {
const X86Subtarget *Subtarget; const X86Subtarget *Subtarget;
public: public:
explicit X86AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, explicit X86AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(O, TM, T, V) { const MCAsmInfo *T)
: AsmPrinter(O, TM, Ctx, Streamer, T) {
Subtarget = &TM.getSubtarget<X86Subtarget>(); Subtarget = &TM.getSubtarget<X86Subtarget>();
} }

View File

@ -52,8 +52,9 @@ namespace {
const XCoreSubtarget &Subtarget; const XCoreSubtarget &Subtarget;
public: public:
explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM, explicit XCoreAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
const MCAsmInfo *T, bool V) MCContext &Ctx, MCStreamer &Streamer,
: AsmPrinter(O, TM, T, V), const MCAsmInfo *T)
: AsmPrinter(O, TM, Ctx, Streamer, T),
Subtarget(TM.getSubtarget<XCoreSubtarget>()) {} Subtarget(TM.getSubtarget<XCoreSubtarget>()) {}
virtual const char *getPassName() const { virtual const char *getPassName() const {