fix an ugly wart in the MCInstPrinter api where the

raw_ostream to print an instruction to had to be specified
at MCInstPrinter construction time instead of being able
to pick at each call to printInstruction.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100307 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2010-04-04 05:04:31 +00:00
parent 35c33bd772
commit d374087be5
18 changed files with 42 additions and 58 deletions

View File

@ -20,26 +20,23 @@ class StringRef;
/// that converts an MCInst to valid target assembly syntax. /// that converts an MCInst to valid target assembly syntax.
class MCInstPrinter { class MCInstPrinter {
protected: protected:
/// O - The main stream to emit instruction text to.
raw_ostream &O;
/// CommentStream - a stream that comments can be emitted to if desired. /// CommentStream - a stream that comments can be emitted to if desired.
/// Each comment must end with a newline. This will be null if verbose /// Each comment must end with a newline. This will be null if verbose
/// assembly emission is disable. /// assembly emission is disable.
raw_ostream *CommentStream; raw_ostream *CommentStream;
const MCAsmInfo &MAI; const MCAsmInfo &MAI;
public: public:
MCInstPrinter(raw_ostream &o, const MCAsmInfo &mai) MCInstPrinter(const MCAsmInfo &mai)
: O(o), CommentStream(0), MAI(mai) {} : CommentStream(0), MAI(mai) {}
virtual ~MCInstPrinter(); virtual ~MCInstPrinter();
/// setCommentStream - Specify a stream to emit comments to. /// setCommentStream - Specify a stream to emit comments to.
void setCommentStream(raw_ostream &OS) { CommentStream = &OS; } void setCommentStream(raw_ostream &OS) { CommentStream = &OS; }
/// printInst - Print the specified MCInst to the current raw_ostream. /// printInst - Print the specified MCInst to the specified raw_ostream.
/// ///
virtual void printInst(const MCInst *MI) = 0; virtual void printInst(const MCInst *MI, raw_ostream &OS) = 0;
/// getOpcodeName - Return the name of the specified opcode enum (e.g. /// getOpcodeName - Return the name of the specified opcode enum (e.g.
/// "MOV32ri") or empty if we can't resolve it. /// "MOV32ri") or empty if we can't resolve it.

View File

@ -71,8 +71,7 @@ namespace llvm {
typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T); typedef MCDisassembler *(*MCDisassemblerCtorTy)(const Target &T);
typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T, typedef MCInstPrinter *(*MCInstPrinterCtorTy)(const Target &T,
unsigned SyntaxVariant, unsigned SyntaxVariant,
const MCAsmInfo &MAI, const MCAsmInfo &MAI);
raw_ostream &O);
typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T, typedef MCCodeEmitter *(*CodeEmitterCtorTy)(const Target &T,
TargetMachine &TM, TargetMachine &TM,
MCContext &Ctx); MCContext &Ctx);
@ -248,11 +247,10 @@ namespace llvm {
} }
MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant, MCInstPrinter *createMCInstPrinter(unsigned SyntaxVariant,
const MCAsmInfo &MAI, const MCAsmInfo &MAI) const {
raw_ostream &O) const {
if (!MCInstPrinterCtorFn) if (!MCInstPrinterCtorFn)
return 0; return 0;
return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI, O); return MCInstPrinterCtorFn(*this, SyntaxVariant, MAI);
} }

View File

@ -128,7 +128,7 @@ bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
default: return true; default: return true;
case CGFT_AssemblyFile: { case CGFT_AssemblyFile: {
MCInstPrinter *InstPrinter = MCInstPrinter *InstPrinter =
getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, Out); getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI);
AsmStreamer.reset(createAsmStreamer(*Context, Out, AsmStreamer.reset(createAsmStreamer(*Context, Out,
getTargetData()->isLittleEndian(), getTargetData()->isLittleEndian(),
getVerboseAsm(), InstPrinter, getVerboseAsm(), InstPrinter,

View File

@ -635,7 +635,7 @@ void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
// If we have an AsmPrinter, use that to print, otherwise print the MCInst. // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
if (InstPrinter) if (InstPrinter)
InstPrinter->printInst(&Inst); InstPrinter->printInst(&Inst, OS);
else else
Inst.print(OS, &MAI); Inst.print(OS, &MAI);
EmitEOL(); EmitEOL();

View File

@ -1385,10 +1385,9 @@ void ARMAsmPrinter::printInstructionThroughMCStreamer(const MachineInstr *MI) {
static MCInstPrinter *createARMMCInstPrinter(const Target &T, static MCInstPrinter *createARMMCInstPrinter(const Target &T,
unsigned SyntaxVariant, unsigned SyntaxVariant,
const MCAsmInfo &MAI, const MCAsmInfo &MAI) {
raw_ostream &O) {
if (SyntaxVariant == 0) if (SyntaxVariant == 0)
return new ARMInstPrinter(O, MAI, false); return new ARMInstPrinter(MAI, false);
return 0; return 0;
} }

View File

@ -98,7 +98,7 @@ static unsigned NextReg(unsigned Reg) {
} }
} }
void ARMInstPrinter::printInst(const MCInst *MI) { void ARMInstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
// Check for MOVs and print canonical forms, instead. // Check for MOVs and print canonical forms, instead.
if (MI->getOpcode() == ARM::MOVs) { if (MI->getOpcode() == ARM::MOVs) {
const MCOperand &Dst = MI->getOperand(0); const MCOperand &Dst = MI->getOperand(0);

View File

@ -22,10 +22,10 @@ namespace llvm {
class ARMInstPrinter : public MCInstPrinter { class ARMInstPrinter : public MCInstPrinter {
bool VerboseAsm; bool VerboseAsm;
public: public:
ARMInstPrinter(raw_ostream &O, const MCAsmInfo &MAI, bool verboseAsm) ARMInstPrinter(const MCAsmInfo &MAI, bool verboseAsm)
: MCInstPrinter(O, MAI), VerboseAsm(verboseAsm) {} : MCInstPrinter(MAI), VerboseAsm(verboseAsm) {}
virtual void printInst(const MCInst *MI); virtual void printInst(const MCInst *MI, raw_ostream &O);
// Autogenerated by tblgen. // Autogenerated by tblgen.
void printInstruction(const MCInst *MI, raw_ostream &O); void printInstruction(const MCInst *MI, raw_ostream &O);

View File

@ -51,7 +51,7 @@ namespace {
} }
void printMCInst(const MCInst *MI) { void printMCInst(const MCInst *MI) {
MSP430InstPrinter(O, *MAI).printInstruction(MI, O); MSP430InstPrinter(*MAI).printInstruction(MI, O);
} }
void printOperand(const MachineInstr *MI, int OpNum, void printOperand(const MachineInstr *MI, int OpNum,
const char* Modifier = 0); const char* Modifier = 0);
@ -191,10 +191,9 @@ void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
static MCInstPrinter *createMSP430MCInstPrinter(const Target &T, static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
unsigned SyntaxVariant, unsigned SyntaxVariant,
const MCAsmInfo &MAI, const MCAsmInfo &MAI) {
raw_ostream &O) {
if (SyntaxVariant == 0) if (SyntaxVariant == 0)
return new MSP430InstPrinter(O, MAI); return new MSP430InstPrinter(MAI);
return 0; return 0;
} }

View File

@ -28,7 +28,7 @@ using namespace llvm;
#include "MSP430GenAsmWriter.inc" #include "MSP430GenAsmWriter.inc"
#undef MachineInstr #undef MachineInstr
void MSP430InstPrinter::printInst(const MCInst *MI) { void MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
printInstruction(MI, O); printInstruction(MI, O);
} }

View File

@ -16,18 +16,15 @@
#include "llvm/MC/MCInstPrinter.h" #include "llvm/MC/MCInstPrinter.h"
namespace llvm namespace llvm {
{
class MCOperand; class MCOperand;
class MSP430InstPrinter : public MCInstPrinter { class MSP430InstPrinter : public MCInstPrinter {
public: public:
MSP430InstPrinter(raw_ostream &O, const MCAsmInfo &MAI) : MSP430InstPrinter(const MCAsmInfo &MAI) : MCInstPrinter(MAI) {
MCInstPrinter(O, MAI) {
} }
virtual void printInst(const MCInst *MI); virtual void printInst(const MCInst *MI, raw_ostream &O);
// Autogenerated by tblgen. // Autogenerated by tblgen.
void printInstruction(const MCInst *MI, raw_ostream &O); void printInstruction(const MCInst *MI, raw_ostream &O);

View File

@ -29,8 +29,8 @@ using namespace llvm;
#include "X86GenAsmWriter.inc" #include "X86GenAsmWriter.inc"
#undef MachineInstr #undef MachineInstr
void X86ATTInstPrinter::printInst(const MCInst *MI) { void X86ATTInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
printInstruction(MI, O); printInstruction(MI, OS);
} }
StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const { StringRef X86ATTInstPrinter::getOpcodeName(unsigned Opcode) const {
return getInstructionName(Opcode); return getInstructionName(Opcode);

View File

@ -21,11 +21,10 @@ namespace llvm {
class X86ATTInstPrinter : public MCInstPrinter { class X86ATTInstPrinter : public MCInstPrinter {
public: public:
X86ATTInstPrinter(raw_ostream &O, const MCAsmInfo &MAI) X86ATTInstPrinter(const MCAsmInfo &MAI) : MCInstPrinter(MAI) {}
: MCInstPrinter(O, MAI) {}
virtual void printInst(const MCInst *MI); virtual void printInst(const MCInst *MI, raw_ostream &OS);
virtual StringRef getOpcodeName(unsigned Opcode) const; virtual StringRef getOpcodeName(unsigned Opcode) const;
// Autogenerated by tblgen. // Autogenerated by tblgen.

View File

@ -636,12 +636,11 @@ void X86AsmPrinter::EmitEndOfAsmFile(Module &M) {
static MCInstPrinter *createX86MCInstPrinter(const Target &T, static MCInstPrinter *createX86MCInstPrinter(const Target &T,
unsigned SyntaxVariant, unsigned SyntaxVariant,
const MCAsmInfo &MAI, const MCAsmInfo &MAI) {
raw_ostream &O) {
if (SyntaxVariant == 0) if (SyntaxVariant == 0)
return new X86ATTInstPrinter(O, MAI); return new X86ATTInstPrinter(MAI);
if (SyntaxVariant == 1) if (SyntaxVariant == 1)
return new X86IntelInstPrinter(O, MAI); return new X86IntelInstPrinter(MAI);
return 0; return 0;
} }

View File

@ -28,8 +28,8 @@ using namespace llvm;
#include "X86GenAsmWriter1.inc" #include "X86GenAsmWriter1.inc"
#undef MachineInstr #undef MachineInstr
void X86IntelInstPrinter::printInst(const MCInst *MI) { void X86IntelInstPrinter::printInst(const MCInst *MI, raw_ostream &OS) {
printInstruction(MI, O); printInstruction(MI, OS);
} }
StringRef X86IntelInstPrinter::getOpcodeName(unsigned Opcode) const { StringRef X86IntelInstPrinter::getOpcodeName(unsigned Opcode) const {
return getInstructionName(Opcode); return getInstructionName(Opcode);

View File

@ -22,10 +22,10 @@ namespace llvm {
class X86IntelInstPrinter : public MCInstPrinter { class X86IntelInstPrinter : public MCInstPrinter {
public: public:
X86IntelInstPrinter(raw_ostream &O, const MCAsmInfo &MAI) X86IntelInstPrinter(const MCAsmInfo &MAI)
: MCInstPrinter(O, MAI) {} : MCInstPrinter(MAI) {}
virtual void printInst(const MCInst *MI); virtual void printInst(const MCInst *MI, raw_ostream &OS);
virtual StringRef getOpcodeName(unsigned Opcode) const; virtual StringRef getOpcodeName(unsigned Opcode) const;
// Autogenerated by tblgen. // Autogenerated by tblgen.

View File

@ -195,10 +195,7 @@ EDDisassembler::EDDisassembler(CPUKey &key) :
InstString.reset(new std::string); InstString.reset(new std::string);
InstStream.reset(new raw_string_ostream(*InstString)); InstStream.reset(new raw_string_ostream(*InstString));
InstPrinter.reset(Tgt->createMCInstPrinter(syntaxVariant, *AsmInfo));
InstPrinter.reset(Tgt->createMCInstPrinter(syntaxVariant,
*AsmInfo,
*InstStream));
if (!InstPrinter) if (!InstPrinter)
return; return;
@ -314,11 +311,10 @@ bool EDDisassembler::registerIsProgramCounter(unsigned registerID) {
return (programCounters.find(registerID) != programCounters.end()); return (programCounters.find(registerID) != programCounters.end());
} }
int EDDisassembler::printInst(std::string& str, int EDDisassembler::printInst(std::string &str, MCInst &inst) {
MCInst& inst) {
PrinterMutex.acquire(); PrinterMutex.acquire();
InstPrinter->printInst(&inst); InstPrinter->printInst(&inst, *InstStream);
InstStream->flush(); InstStream->flush();
str = *InstString; str = *InstString;
InstString->clear(); InstString->clear();

View File

@ -48,8 +48,8 @@ public:
} }
static bool PrintInsts(const MCDisassembler &DisAsm, static bool PrintInsts(const MCDisassembler &DisAsm,
MCInstPrinter &Printer, const ByteArrayTy &Bytes, MCInstPrinter &Printer, const ByteArrayTy &Bytes,
SourceMgr &SM) { SourceMgr &SM) {
// Wrap the vector in a MemoryObject. // Wrap the vector in a MemoryObject.
VectorMemoryObject memoryObject(Bytes); VectorMemoryObject memoryObject(Bytes);
@ -62,7 +62,7 @@ static bool PrintInsts(const MCDisassembler &DisAsm,
if (DisAsm.getInstruction(Inst, Size, memoryObject, Index, if (DisAsm.getInstruction(Inst, Size, memoryObject, Index,
/*REMOVE*/ nulls())) { /*REMOVE*/ nulls())) {
Printer.printInst(&Inst); Printer.printInst(&Inst, outs());
outs() << "\n"; outs() << "\n";
} }
else { else {
@ -92,7 +92,7 @@ int Disassembler::disassemble(const Target &T, const std::string &Triple,
return -1; return -1;
} }
OwningPtr<MCInstPrinter> IP(T.createMCInstPrinter(0, *AsmInfo, outs())); OwningPtr<MCInstPrinter> IP(T.createMCInstPrinter(0, *AsmInfo));
if (!IP) { if (!IP) {
errs() << "error: no instruction printer for target " << Triple << '\n'; errs() << "error: no instruction printer for target " << Triple << '\n';
return -1; return -1;

View File

@ -290,7 +290,7 @@ static int AssembleInput(const char *ProgName) {
if (FileType == OFT_AssemblyFile) { if (FileType == OFT_AssemblyFile) {
MCInstPrinter *IP = MCInstPrinter *IP =
TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI, *Out); TheTarget->createMCInstPrinter(OutputAsmVariant, *MAI);
if (ShowEncoding) if (ShowEncoding)
CE.reset(TheTarget->createCodeEmitter(*TM, Ctx)); CE.reset(TheTarget->createCodeEmitter(*TM, Ctx));
Str.reset(createAsmStreamer(Ctx, *Out,TM->getTargetData()->isLittleEndian(), Str.reset(createAsmStreamer(Ctx, *Out,TM->getTargetData()->isLittleEndian(),