From de093ef8d6c1efae9da16a4f841d392fba389e07 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Thu, 31 Jan 2013 23:29:57 +0000 Subject: [PATCH] Give the MCStreamer class hierarchy LLVM RTTI facilities for use with isa<> and dyn_cast<>. In several places, code is already hacking around the absence of this, and there seem to be several interfaces that might be lifted and/or devirtualized using this. This change was based on a discussion with Jim Grosbach about how best to handle testing for specific MCStreamer subclasses. He said that this was the correct end state, and everything else was too hacky so I decided to just make it so. No functionality should be changed here, this is just threading the kind through all the constructors and setting up the classof overloads. llvm-svn: 174113 --- llvm/include/llvm/MC/MCELFStreamer.h | 23 ++++++++++++------ llvm/include/llvm/MC/MCObjectStreamer.h | 8 +++++-- llvm/include/llvm/MC/MCStreamer.h | 20 +++++++++++++++- llvm/lib/MC/MCAsmStreamer.cpp | 6 ++++- llvm/lib/MC/MCMachOStreamer.cpp | 10 +++++--- llvm/lib/MC/MCNullStreamer.cpp | 7 +++++- llvm/lib/MC/MCObjectStreamer.cpp | 27 ++++++++++------------ llvm/lib/MC/MCPureStreamer.cpp | 10 +++++--- llvm/lib/MC/MCStreamer.cpp | 7 +++--- llvm/lib/MC/WinCOFFStreamer.cpp | 15 ++++++------ .../lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp | 15 +++++++----- llvm/tools/lto/LTOModule.cpp | 7 +++++- 12 files changed, 104 insertions(+), 51 deletions(-) diff --git a/llvm/include/llvm/MC/MCELFStreamer.h b/llvm/include/llvm/MC/MCELFStreamer.h index 60a360f..0413613 100644 --- a/llvm/include/llvm/MC/MCELFStreamer.h +++ b/llvm/include/llvm/MC/MCELFStreamer.h @@ -28,15 +28,20 @@ class MCSymbolData; class raw_ostream; class MCELFStreamer : public MCObjectStreamer { -public: - MCELFStreamer(MCContext &Context, MCAsmBackend &TAB, +protected: + MCELFStreamer(StreamerKind Kind, MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter) - : MCObjectStreamer(Context, TAB, OS, Emitter) {} + : MCObjectStreamer(Kind, Context, TAB, OS, Emitter) {} + +public: + MCELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS, + MCCodeEmitter *Emitter) + : MCObjectStreamer(SK_ELFStreamer, Context, TAB, OS, Emitter) {} - MCELFStreamer(MCContext &Context, MCAsmBackend &TAB, - raw_ostream &OS, MCCodeEmitter *Emitter, - MCAssembler *Assembler) - : MCObjectStreamer(Context, TAB, OS, Emitter, Assembler) {} + MCELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS, + MCCodeEmitter *Emitter, MCAssembler *Assembler) + : MCObjectStreamer(SK_ELFStreamer, Context, TAB, OS, Emitter, + Assembler) {} virtual ~MCELFStreamer(); @@ -81,6 +86,10 @@ public: virtual void FinishImpl(); /// @} + static bool classof(const MCStreamer *S) { + return S->getKind() == SK_ELFStreamer && S->getKind() == SK_ARMELFStreamer; + } + private: virtual void EmitInstToFragment(const MCInst &Inst); virtual void EmitInstToData(const MCInst &Inst); diff --git a/llvm/include/llvm/MC/MCObjectStreamer.h b/llvm/include/llvm/MC/MCObjectStreamer.h index d73fe3a..f06c49f 100644 --- a/llvm/include/llvm/MC/MCObjectStreamer.h +++ b/llvm/include/llvm/MC/MCObjectStreamer.h @@ -38,9 +38,9 @@ class MCObjectStreamer : public MCStreamer { virtual void EmitCFIEndProcImpl(MCDwarfFrameInfo &Frame); protected: - MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, + MCObjectStreamer(StreamerKind Kind, MCContext &Context, MCAsmBackend &TAB, raw_ostream &_OS, MCCodeEmitter *_Emitter); - MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, + MCObjectStreamer(StreamerKind Kind, MCContext &Context, MCAsmBackend &TAB, raw_ostream &_OS, MCCodeEmitter *_Emitter, MCAssembler *_Assembler); ~MCObjectStreamer(); @@ -107,6 +107,10 @@ public: virtual void FinishImpl(); /// @} + + static bool classof(const MCStreamer *S) { + return S->getKind() >= SK_ELFStreamer && S->getKind() <= SK_WinCOFFStreamer; + } }; } // end namespace llvm diff --git a/llvm/include/llvm/MC/MCStreamer.h b/llvm/include/llvm/MC/MCStreamer.h index c6246e1..3631325 100644 --- a/llvm/include/llvm/MC/MCStreamer.h +++ b/llvm/include/llvm/MC/MCStreamer.h @@ -46,6 +46,22 @@ namespace llvm { /// a .s file, and implementations that write out .o files of various formats. /// class MCStreamer { + public: + enum StreamerKind { + SK_AsmStreamer, + SK_NullStreamer, + SK_RecordStreamer, + + // MCObjectStreamer subclasses. + SK_ELFStreamer, + SK_ARMELFStreamer, + SK_MachOStreamer, + SK_PureStreamer, + SK_WinCOFFStreamer + }; + + private: + const StreamerKind Kind; MCContext &Context; MCStreamer(const MCStreamer&) LLVM_DELETED_FUNCTION; @@ -74,7 +90,7 @@ namespace llvm { bool AutoInitSections; protected: - MCStreamer(MCContext &Ctx); + MCStreamer(StreamerKind Kind, MCContext &Ctx); const MCExpr *BuildSymbolDiff(MCContext &Context, const MCSymbol *A, const MCSymbol *B); @@ -93,6 +109,8 @@ namespace llvm { public: virtual ~MCStreamer(); + StreamerKind getKind() const { return Kind; } + /// State management /// virtual void reset(); diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp index 7191947..71be1a9 100644 --- a/llvm/lib/MC/MCAsmStreamer.cpp +++ b/llvm/lib/MC/MCAsmStreamer.cpp @@ -71,7 +71,7 @@ public: MCInstPrinter *printer, MCCodeEmitter *emitter, MCAsmBackend *asmbackend, bool showInst) - : MCStreamer(Context), OS(os), MAI(Context.getAsmInfo()), + : MCStreamer(SK_AsmStreamer, Context), OS(os), MAI(Context.getAsmInfo()), InstPrinter(printer), Emitter(emitter), AsmBackend(asmbackend), CommentStream(CommentToEmit), IsVerboseAsm(isVerboseAsm), ShowInst(showInst), UseLoc(useLoc), UseCFI(useCFI), @@ -277,6 +277,10 @@ public: virtual void FinishImpl(); /// @} + + static bool classof(const MCStreamer *S) { + return S->getKind() == SK_NullStreamer; + } }; } // end anonymous namespace. diff --git a/llvm/lib/MC/MCMachOStreamer.cpp b/llvm/lib/MC/MCMachOStreamer.cpp index 2c0c97a..7d08d0e 100644 --- a/llvm/lib/MC/MCMachOStreamer.cpp +++ b/llvm/lib/MC/MCMachOStreamer.cpp @@ -34,9 +34,9 @@ private: void EmitDataRegion(DataRegionData::KindTy Kind); void EmitDataRegionEnd(); public: - MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, - raw_ostream &OS, MCCodeEmitter *Emitter) - : MCObjectStreamer(Context, MAB, OS, Emitter) {} + MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS, + MCCodeEmitter *Emitter) + : MCObjectStreamer(SK_MachOStreamer, Context, MAB, OS, Emitter) {} /// @name MCStreamer Interface /// @{ @@ -87,6 +87,10 @@ public: virtual void FinishImpl(); /// @} + + static bool classof(const MCStreamer *S) { + return S->getKind() == SK_MachOStreamer; + } }; } // end anonymous namespace. diff --git a/llvm/lib/MC/MCNullStreamer.cpp b/llvm/lib/MC/MCNullStreamer.cpp index 3eee5ca..89f74c1 100644 --- a/llvm/lib/MC/MCNullStreamer.cpp +++ b/llvm/lib/MC/MCNullStreamer.cpp @@ -19,7 +19,7 @@ namespace { class MCNullStreamer : public MCStreamer { public: - MCNullStreamer(MCContext &Context) : MCStreamer(Context) {} + MCNullStreamer(MCContext &Context) : MCStreamer(SK_NullStreamer, Context) {} /// @name MCStreamer Interface /// @{ @@ -109,6 +109,11 @@ namespace { } /// @} + + static bool classof(const MCStreamer *S) { + return S->getKind() == SK_NullStreamer; + } + }; } diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp index 6f2dce6..5aa4b40 100644 --- a/llvm/lib/MC/MCObjectStreamer.cpp +++ b/llvm/lib/MC/MCObjectStreamer.cpp @@ -20,22 +20,19 @@ #include "llvm/Support/ErrorHandling.h" using namespace llvm; -MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, - raw_ostream &OS, MCCodeEmitter *Emitter_) - : MCStreamer(Context), - Assembler(new MCAssembler(Context, TAB, - *Emitter_, *TAB.createObjectWriter(OS), - OS)), - CurSectionData(0) -{ -} - -MCObjectStreamer::MCObjectStreamer(MCContext &Context, MCAsmBackend &TAB, - raw_ostream &OS, MCCodeEmitter *Emitter_, +MCObjectStreamer::MCObjectStreamer(StreamerKind Kind, MCContext &Context, + MCAsmBackend &TAB, raw_ostream &OS, + MCCodeEmitter *Emitter_) + : MCStreamer(Kind, Context), + Assembler(new MCAssembler(Context, TAB, *Emitter_, + *TAB.createObjectWriter(OS), OS)), + CurSectionData(0) {} + +MCObjectStreamer::MCObjectStreamer(StreamerKind Kind, MCContext &Context, + MCAsmBackend &TAB, raw_ostream &OS, + MCCodeEmitter *Emitter_, MCAssembler *_Assembler) - : MCStreamer(Context), Assembler(_Assembler), CurSectionData(0) -{ -} + : MCStreamer(Kind, Context), Assembler(_Assembler), CurSectionData(0) {} MCObjectStreamer::~MCObjectStreamer() { delete &Assembler->getBackend(); diff --git a/llvm/lib/MC/MCPureStreamer.cpp b/llvm/lib/MC/MCPureStreamer.cpp index 6ce7ae8..573308a 100644 --- a/llvm/lib/MC/MCPureStreamer.cpp +++ b/llvm/lib/MC/MCPureStreamer.cpp @@ -28,9 +28,9 @@ private: virtual void EmitInstToData(const MCInst &Inst); public: - MCPureStreamer(MCContext &Context, MCAsmBackend &TAB, - raw_ostream &OS, MCCodeEmitter *Emitter) - : MCObjectStreamer(Context, TAB, OS, Emitter) {} + MCPureStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS, + MCCodeEmitter *Emitter) + : MCObjectStreamer(SK_PureStreamer, Context, TAB, OS, Emitter) {} /// @name MCStreamer Interface /// @{ @@ -100,6 +100,10 @@ public: } /// @} + + static bool classof(const MCStreamer *S) { + return S->getKind() == SK_PureStreamer; + } }; } // end anonymous namespace. diff --git a/llvm/lib/MC/MCStreamer.cpp b/llvm/lib/MC/MCStreamer.cpp index 00ebde3..e92569b 100644 --- a/llvm/lib/MC/MCStreamer.cpp +++ b/llvm/lib/MC/MCStreamer.cpp @@ -21,10 +21,9 @@ #include using namespace llvm; -MCStreamer::MCStreamer(MCContext &Ctx) - : Context(Ctx), EmitEHFrame(true), EmitDebugFrame(false), - CurrentW64UnwindInfo(0), LastSymbol(0), - AutoInitSections(false) { +MCStreamer::MCStreamer(StreamerKind Kind, MCContext &Ctx) + : Kind(Kind), Context(Ctx), EmitEHFrame(true), EmitDebugFrame(false), + CurrentW64UnwindInfo(0), LastSymbol(0), AutoInitSections(false) { const MCSection *section = NULL; SectionStack.push_back(std::make_pair(section, section)); } diff --git a/llvm/lib/MC/WinCOFFStreamer.cpp b/llvm/lib/MC/WinCOFFStreamer.cpp index b529489..75f343c 100644 --- a/llvm/lib/MC/WinCOFFStreamer.cpp +++ b/llvm/lib/MC/WinCOFFStreamer.cpp @@ -75,6 +75,10 @@ public: virtual void EmitWin64EHHandlerData(); virtual void FinishImpl(); + static bool classof(const MCStreamer *S) { + return S->getKind() == SK_WinCOFFStreamer; + } + private: virtual void EmitInstToData(const MCInst &Inst) { MCDataFragment *DF = getOrCreateDataFragment(); @@ -128,13 +132,10 @@ private: }; } // end anonymous namespace. -WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, - MCAsmBackend &MAB, - MCCodeEmitter &CE, - raw_ostream &OS) - : MCObjectStreamer(Context, MAB, OS, &CE) - , CurSymbol(NULL) { -} +WinCOFFStreamer::WinCOFFStreamer(MCContext &Context, MCAsmBackend &MAB, + MCCodeEmitter &CE, raw_ostream &OS) + : MCObjectStreamer(SK_WinCOFFStreamer, Context, MAB, OS, &CE), + CurSymbol(NULL) {} void WinCOFFStreamer::AddCommonSymbol(MCSymbol *Symbol, uint64_t Size, unsigned ByteAlignment, bool External) { diff --git a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp index 526f571..418971d 100644 --- a/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp +++ b/llvm/lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp @@ -54,12 +54,11 @@ namespace { /// by MachO. Beware! class ARMELFStreamer : public MCELFStreamer { public: - ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, - raw_ostream &OS, MCCodeEmitter *Emitter, bool IsThumb) - : MCELFStreamer(Context, TAB, OS, Emitter), - IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None), - ExTab(0), FnStart(0), Personality(0), CantUnwind(false) { - } + ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS, + MCCodeEmitter *Emitter, bool IsThumb) + : MCELFStreamer(SK_ARMELFStreamer, Context, TAB, OS, Emitter), + IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None), ExTab(0), + FnStart(0), Personality(0), CantUnwind(false) {} ~ARMELFStreamer() {} @@ -134,6 +133,10 @@ public: } } + static bool classof(const MCStreamer *S) { + return S->getKind() == SK_ARMELFStreamer; + } + private: enum ElfMappingSymbol { EMS_None, diff --git a/llvm/tools/lto/LTOModule.cpp b/llvm/tools/lto/LTOModule.cpp index d90a403..e2bf858 100644 --- a/llvm/tools/lto/LTOModule.cpp +++ b/llvm/tools/lto/LTOModule.cpp @@ -733,7 +733,8 @@ namespace { return Symbols.end(); } - RecordStreamer(MCContext &Context) : MCStreamer(Context) {} + RecordStreamer(MCContext &Context) + : MCStreamer(SK_RecordStreamer, Context) {} virtual void EmitInstruction(const MCInst &Inst) { // Scan for values. @@ -804,6 +805,10 @@ namespace { const MCSymbol *Label, unsigned PointerSize) {} virtual void FinishImpl() {} + + static bool classof(const MCStreamer *S) { + return S->getKind == SK_RecordStreamer; + } }; } // end anonymous namespace -- 2.7.4