From b7c3a4b195597848e7c2559937914ae1087f3131 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Sat, 13 Feb 2010 09:28:03 +0000 Subject: [PATCH] MCAssembler: Add assorted dump() methods. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@96089 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/MC/MCAssembler.h | 80 +++++++++++-------- lib/MC/MCAssembler.cpp | 144 ++++++++++++++++++++++++++++++++++ 2 files changed, 193 insertions(+), 31 deletions(-) diff --git a/include/llvm/MC/MCAssembler.h b/include/llvm/MC/MCAssembler.h index 562a5210ad8..004ecceb8dd 100644 --- a/include/llvm/MC/MCAssembler.h +++ b/include/llvm/MC/MCAssembler.h @@ -46,7 +46,7 @@ struct MCAsmFixup { uint64_t FixedValue; public: - MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, unsigned _Size) + MCAsmFixup(uint64_t _Offset, const MCExpr &_Value, unsigned _Size) : Offset(_Offset), Value(&_Value), Size(_Size), FixedValue(0) {} }; @@ -151,7 +151,7 @@ public: uint64_t getAddress() const; - uint64_t getFileSize() const { + uint64_t getFileSize() const { assert(FileSize != ~UINT64_C(0) && "File size not set!"); return FileSize; } @@ -169,6 +169,8 @@ public: /// @} static bool classof(const MCFragment *O) { return true; } + + virtual void dump(); }; class MCDataFragment : public MCFragment { @@ -189,10 +191,12 @@ public: /// @} - static bool classof(const MCFragment *F) { - return F->getKind() == MCFragment::FT_Data; + static bool classof(const MCFragment *F) { + return F->getKind() == MCFragment::FT_Data; } static bool classof(const MCDataFragment *) { return true; } + + virtual void dump(); }; class MCAlignFragment : public MCFragment { @@ -224,7 +228,7 @@ public: } unsigned getAlignment() const { return Alignment; } - + int64_t getValue() const { return Value; } unsigned getValueSize() const { return ValueSize; } @@ -233,10 +237,12 @@ public: /// @} - static bool classof(const MCFragment *F) { - return F->getKind() == MCFragment::FT_Align; + static bool classof(const MCFragment *F) { + return F->getKind() == MCFragment::FT_Align; } static bool classof(const MCAlignFragment *) { return true; } + + virtual void dump(); }; class MCFillFragment : public MCFragment { @@ -251,7 +257,7 @@ class MCFillFragment : public MCFragment { public: MCFillFragment(const MCExpr &_Value, unsigned _ValueSize, uint64_t _Count, - MCSectionData *SD = 0) + MCSectionData *SD = 0) : MCFragment(FT_Fill, SD), Value(&_Value), ValueSize(_ValueSize), Count(_Count) {} @@ -263,24 +269,26 @@ public: } const MCExpr &getValue() const { return *Value; } - + unsigned getValueSize() const { return ValueSize; } uint64_t getCount() const { return Count; } /// @} - static bool classof(const MCFragment *F) { - return F->getKind() == MCFragment::FT_Fill; + static bool classof(const MCFragment *F) { + return F->getKind() == MCFragment::FT_Fill; } static bool classof(const MCFillFragment *) { return true; } + + virtual void dump(); }; class MCOrgFragment : public MCFragment { /// Offset - The offset this fragment should start at. const MCExpr *Offset; - /// Value - Value to use for filling bytes. + /// Value - Value to use for filling bytes. int8_t Value; public: @@ -297,15 +305,17 @@ public: } const MCExpr &getOffset() const { return *Offset; } - + uint8_t getValue() const { return Value; } /// @} - static bool classof(const MCFragment *F) { - return F->getKind() == MCFragment::FT_Org; + static bool classof(const MCFragment *F) { + return F->getKind() == MCFragment::FT_Org; } static bool classof(const MCOrgFragment *) { return true; } + + virtual void dump(); }; /// MCZeroFillFragment - Represent data which has a fixed size and alignment, @@ -331,15 +341,17 @@ public: } uint64_t getSize() const { return Size; } - + unsigned getAlignment() const { return Alignment; } /// @} - static bool classof(const MCFragment *F) { - return F->getKind() == MCFragment::FT_ZeroFill; + static bool classof(const MCFragment *F) { + return F->getKind() == MCFragment::FT_ZeroFill; } static bool classof(const MCZeroFillFragment *) { return true; } + + virtual void dump(); }; // FIXME: Should this be a separate class, or just merged into MCSection? Since @@ -387,7 +399,7 @@ private: /// @} -public: +public: // Only for use as sentinel. MCSectionData(); MCSectionData(const MCSection &Section, MCAssembler *A = 0); @@ -425,28 +437,30 @@ public: // // FIXME: This could all be kept private to the assembler implementation. - uint64_t getAddress() const { + uint64_t getAddress() const { assert(Address != ~UINT64_C(0) && "Address not set!"); return Address; } void setAddress(uint64_t Value) { Address = Value; } - uint64_t getSize() const { + uint64_t getSize() const { assert(Size != ~UINT64_C(0) && "File size not set!"); return Size; } void setSize(uint64_t Value) { Size = Value; } - uint64_t getFileSize() const { + uint64_t getFileSize() const { assert(FileSize != ~UINT64_C(0) && "File size not set!"); return FileSize; } - void setFileSize(uint64_t Value) { FileSize = Value; } + void setFileSize(uint64_t Value) { FileSize = Value; } bool hasInstructions() const { return HasInstructions; } void setHasInstructions(bool Value) { HasInstructions = Value; } /// @} + + void dump(); }; // FIXME: Same concerns as with SectionData. @@ -460,7 +474,7 @@ public: /// Offset - The offset to apply to the fragment address to form this symbol's /// value. uint64_t Offset; - + /// IsExternal - True if this symbol is visible outside this translation /// unit. unsigned IsExternal : 1; @@ -506,10 +520,10 @@ public: /// @} /// @name Symbol Attributes /// @{ - + bool isExternal() const { return IsExternal; } void setExternal(bool Value) { IsExternal = Value; } - + bool isPrivateExtern() const { return IsPrivateExtern; } void setPrivateExtern(bool Value) { IsPrivateExtern = Value; } @@ -542,14 +556,16 @@ public: /// setFlags - Set the (implementation defined) symbol flags. void setFlags(uint32_t Value) { Flags = Value; } - + /// getIndex - Get the (implementation defined) index. uint64_t getIndex() const { return Index; } /// setIndex - Set the (implementation defined) index. void setIndex(uint64_t Value) { Index = Value; } - - /// @} + + /// @} + + void dump(); }; // FIXME: This really doesn't belong here. See comments below. @@ -578,7 +594,7 @@ private: MCContext &Context; raw_ostream &OS; - + iplist Sections; iplist Symbols; @@ -622,7 +638,7 @@ public: /// @{ const SectionDataListType &getSectionList() const { return Sections; } - SectionDataListType &getSectionList() { return Sections; } + SectionDataListType &getSectionList() { return Sections; } iterator begin() { return Sections.begin(); } const_iterator begin() const { return Sections.begin(); } @@ -669,6 +685,8 @@ public: size_t indirect_symbol_size() const { return IndirectSymbols.size(); } /// @} + + void dump(); }; } // end namespace llvm diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index 9afc169fcf4..a5df4ba42be 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -16,11 +16,13 @@ #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Statistic.h" +#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MachO.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Debug.h" #include using namespace llvm; @@ -1148,6 +1150,10 @@ static void WriteFileData(raw_ostream &OS, const MCSectionData &SD, } void MCAssembler::Finish() { + DEBUG_WITH_TYPE("mc-dump", { + llvm::errs() << "assembler backend - pre-layout\n--\n"; + dump(); }); + // Layout the concrete sections and fragments. uint64_t Address = 0; MCSectionData *Prev = 0; @@ -1186,9 +1192,147 @@ void MCAssembler::Finish() { Address += SD.getSize(); } + DEBUG_WITH_TYPE("mc-dump", { + llvm::errs() << "assembler backend - post-layout\n--\n"; + dump(); }); + // Write the object file. MachObjectWriter MOW(OS); MOW.WriteObject(*this); OS.flush(); } + + +// Debugging methods + +namespace llvm { + +raw_ostream &operator<<(raw_ostream &OS, const MCAsmFixup &AF) { + OS << ""; + return OS; +} + +} + +void MCFragment::dump() { + raw_ostream &OS = llvm::errs(); + + OS << ""; +} + +void MCAlignFragment::dump() { + raw_ostream &OS = llvm::errs(); + + OS << "MCFragment::dump(); + OS << "\n "; + OS << " Alignment:" << getAlignment() + << " Value:" << getValue() << " ValueSize:" << getValueSize() + << " MaxBytesToEmit:" << getMaxBytesToEmit() << ">"; +} + +void MCDataFragment::dump() { + raw_ostream &OS = llvm::errs(); + + OS << "MCFragment::dump(); + OS << "\n "; + OS << " Contents:["; + for (unsigned i = 0, e = getContents().size(); i != e; ++i) { + if (i) OS << ","; + OS << hexdigit((Contents[i] >> 4) & 0xF) << hexdigit(Contents[i] & 0xF); + } + OS << "]>"; +} + +void MCFillFragment::dump() { + raw_ostream &OS = llvm::errs(); + + OS << "MCFragment::dump(); + OS << "\n "; + OS << " Value:" << getValue() << " ValueSize:" << getValueSize() + << " Count:" << getCount() << ">"; +} + +void MCOrgFragment::dump() { + raw_ostream &OS = llvm::errs(); + + OS << "MCFragment::dump(); + OS << "\n "; + OS << " Offset:" << getOffset() << " Value:" << getValue() << ">"; +} + +void MCZeroFillFragment::dump() { + raw_ostream &OS = llvm::errs(); + + OS << "MCFragment::dump(); + OS << "\n "; + OS << " Size:" << getSize() << " Alignment:" << getAlignment() << ">"; +} + +void MCSectionData::dump() { + raw_ostream &OS = llvm::errs(); + + OS << "dump(); + } + OS << "]>"; +} + +void MCSymbolData::dump() { + raw_ostream &OS = llvm::errs(); + + OS << ""; +} + +void MCAssembler::dump() { + raw_ostream &OS = llvm::errs(); + + OS << "dump(); + } + OS << "],\n"; + OS << " Symbols:["; + + for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { + if (it != symbol_begin()) OS << ",\n "; + it->dump(); + } + OS << "]>\n"; +} -- 2.34.1