diff --git a/CREDITS.TXT b/CREDITS.TXT index 3c1dcb0..ffcaaf1 100644 --- a/CREDITS.TXT +++ b/CREDITS.TXT @@ -443,3 +443,9 @@ D: Bunches of stuff N: Bob Wilson E: bob.wilson@acm.org D: Advanced SIMD (NEON) support in the ARM backend. + +N: David Fang +D: PowerPC-Darwin port + +N: Iain Sandoe +D: PowerPC-Darwin port diff --git a/autoconf/configure.ac b/autoconf/configure.ac index 7b4bae7..e61162b 100644 --- a/autoconf/configure.ac +++ b/autoconf/configure.ac @@ -1765,11 +1765,11 @@ AC_LINK_IFELSE( ], [ AC_MSG_RESULT([yes]) - AC_DEFINE([LLVM_HAS_ATOMICS], [1], [Has gcc/MSVC atomic intrinsics]) + AC_DEFINE([LLVM_HAS_ATOMICS], [1], [Has gcc/MSVC/Apple atomic intrinsics]) ], [ AC_MSG_RESULT([no]) - AC_DEFINE([LLVM_HAS_ATOMICS], [0], [Has gcc/MSVC atomic intrinsics]) + AC_DEFINE([LLVM_HAS_ATOMICS], [0], [Has gcc/MSVC/Apple atomic intrinsics]) AC_MSG_WARN([LLVM will be built thread-unsafe because atomic builtins are missing]) ]) AC_LANG_POP([C++]) diff --git a/cmake/config-ix.cmake b/cmake/config-ix.cmake index dc991a2..c7a4506 100755 --- a/cmake/config-ix.cmake +++ b/cmake/config-ix.cmake @@ -346,6 +346,12 @@ get_host_triple(LLVM_INFERRED_HOST_TRIPLE) set(LLVM_HOST_TRIPLE "${LLVM_INFERRED_HOST_TRIPLE}" CACHE STRING "Host on which LLVM binaries will run") +if( APPLE ) +get_host_osx_version(LLVM_INFERRED_OSX_VERSION) +set(LLVM_HOST_OSX_VERSION "${LLVM_INFERRED_OSX_VERSION}" CACHE STRING + "Host version of Mac OS X") +endif( APPLE ) + # Determine the native architecture. string(TOLOWER "${LLVM_TARGET_ARCH}" LLVM_NATIVE_ARCH) if( LLVM_NATIVE_ARCH STREQUAL "host" ) diff --git a/cmake/modules/CheckAtomic.cmake b/cmake/modules/CheckAtomic.cmake index 0d63a82..213b57f 100644 --- a/cmake/modules/CheckAtomic.cmake +++ b/cmake/modules/CheckAtomic.cmake @@ -6,6 +6,10 @@ CHECK_CXX_SOURCE_COMPILES(" #ifdef _MSC_VER #include #endif +#define NEED_DARWIN_ATOMICS (defined(__APPLE__) && defined(__GNUC__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 2))) +#if NEED_DARWIN_ATOMICS +#include +#endif int main() { #ifdef _MSC_VER volatile LONG val = 1; @@ -13,6 +17,12 @@ int main() { InterlockedCompareExchange(&val, 0, 1); InterlockedIncrement(&val); InterlockedDecrement(&val); +#elif NEED_DARWIN_ATOMICS + int32_t val = 1; + OSMemoryBarrier(); + OSAtomicCompareAndSwap32Barrier(1, 0, &val); + OSAtomicIncrement32(&val); + OSAtomicDecrement32(&val); #else volatile unsigned long val = 1; __sync_synchronize(); diff --git a/cmake/modules/GetHostTriple.cmake b/cmake/modules/GetHostTriple.cmake index 671a8ce..37caa10 100644 --- a/cmake/modules/GetHostTriple.cmake +++ b/cmake/modules/GetHostTriple.cmake @@ -28,3 +28,15 @@ function( get_host_triple var ) set( ${var} ${value} PARENT_SCOPE ) message(STATUS "Target triple: ${value}") endfunction( get_host_triple var ) + +# Mac OS X only: get the host version +function( get_host_osx_version var ) + if( APPLE ) + execute_process(COMMAND sw_vers -productVersion COMMAND cut -d. -f1-2 + RESULT_VARIABLE TT_RV + OUTPUT_VARIABLE value + OUTPUT_STRIP_TRAILING_WHITESPACE) + set( ${var} ${value} PARENT_SCOPE ) + message(STATUS "Host OS X version: ${value}") + endif ( APPLE ) +endfunction( get_host_osx_version var ) diff --git a/include/llvm/ADT/SmallBitVector.h b/include/llvm/ADT/SmallBitVector.h index 86949b2..305a054 100644 --- a/include/llvm/ADT/SmallBitVector.h +++ b/include/llvm/ADT/SmallBitVector.h @@ -34,7 +34,8 @@ class SmallBitVector { // pointer to memory containing size, allocation size, and the array of bits. uintptr_t X; - enum { +// gcc-4.0 workaround: make certain enums non-anonymous + enum _nums { // The number of bits in this class. NumBaseBits = sizeof(uintptr_t) * CHAR_BIT, diff --git a/include/llvm/Bitcode/BitstreamReader.h b/include/llvm/Bitcode/BitstreamReader.h index dc5e095..658a2dc 100644 --- a/include/llvm/Bitcode/BitstreamReader.h +++ b/include/llvm/Bitcode/BitstreamReader.h @@ -136,7 +136,9 @@ public: /// Record - This is a record with a specific AbbrevID. /// struct BitstreamEntry { - enum { +// anonymous enum named as workaround to g++-4.0.1 bug (fixed in 4.0.2): +// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20589 + enum KindEnum { Error, EndBlock, SubBlock, diff --git a/include/llvm/CodeGen/ScheduleDFS.h b/include/llvm/CodeGen/ScheduleDFS.h index 73ce99f..4f85f0d 100644 --- a/include/llvm/CodeGen/ScheduleDFS.h +++ b/include/llvm/CodeGen/ScheduleDFS.h @@ -68,7 +68,14 @@ struct ILPValue { class SchedDFSResult { friend class SchedDFSImpl; +// workaround gcc-4.0 accessibility bug +#if !__GNUC_PREREQ(4, 2) +public: +#endif static const unsigned InvalidSubtreeID = ~0u; +#if !__GNUC_PREREQ(4, 2) +private: +#endif /// \brief Per-SUnit data computed during DFS for various metrics. /// diff --git a/include/llvm/IR/InlineAsm.h b/include/llvm/IR/InlineAsm.h index 3398a83..5497822 100644 --- a/include/llvm/IR/InlineAsm.h +++ b/include/llvm/IR/InlineAsm.h @@ -197,7 +197,7 @@ public: // These are helper methods for dealing with flags in the INLINEASM SDNode // in the backend. - enum LLVM_ENUM_INT_TYPE(uint32_t) { + enum OpFields LLVM_ENUM_INT_TYPE(uint32_t) { // Fixed operands on an INLINEASM SDNode. Op_InputChain = 0, Op_AsmString = 1, diff --git a/include/llvm/MC/MCAssembler.h b/include/llvm/MC/MCAssembler.h index 8735a55..97e0a67 100644 --- a/include/llvm/MC/MCAssembler.h +++ b/include/llvm/MC/MCAssembler.h @@ -123,7 +123,7 @@ public: virtual void setBundlePadding(uint8_t N) { } - void dump(); + void dump() const; }; /// Interface implemented by fragments that contain encoded instructions and/or @@ -660,7 +660,7 @@ public: BundleGroupBeforeFirstInst = IsFirst; } - void dump(); + void dump() const; /// @} }; @@ -785,7 +785,7 @@ public: /// @} - void dump(); + void dump() const; }; // FIXME: This really doesn't belong here. See comments below. @@ -1175,7 +1175,7 @@ public: /// @} - void dump(); + void dump() const; }; } // end namespace llvm diff --git a/include/llvm/MC/MCMachObjectWriter.h b/include/llvm/MC/MCMachObjectWriter.h index 3ba6e65..f3f3b00 100644 --- a/include/llvm/MC/MCMachObjectWriter.h +++ b/include/llvm/MC/MCMachObjectWriter.h @@ -17,6 +17,16 @@ #include "llvm/MC/MCObjectWriter.h" #include "llvm/Support/DataTypes.h" #include "llvm/Support/MachO.h" + +/** + Mach-O needs indirect symbols grouped by section. + Goal: 1 + */ +#define ORDER_INDIRECT_SYMBOLS_BY_SECTION 1 +#if ORDER_INDIRECT_SYMBOLS_BY_SECTION +#include "llvm/ADT/SetVector.h" +#endif + #include namespace llvm { @@ -112,6 +122,22 @@ class MachObjectWriter : public MCObjectWriter { /// @} +#if ORDER_INDIRECT_SYMBOLS_BY_SECTION + /// @name Indirect Symbol Table Data + /// @{ + + typedef std::vector IndirectSymbol_list_type; + typedef DenseMap + IndirectSymbol_map_type; + // keep sections in order of appearance + typedef SetVector IndirectSymbolSection_set_type; + + IndirectSymbol_map_type IndirectSymbolMap; + IndirectSymbolSection_set_type IndirectSymbolSections; + + /// @} +#endif + public: MachObjectWriter(MCMachObjectTargetWriter *MOTW, raw_ostream &_OS, bool _IsLittleEndian) diff --git a/include/llvm/MC/SectionKind.h b/include/llvm/MC/SectionKind.h index 85a91c6..fbb359a 100644 --- a/include/llvm/MC/SectionKind.h +++ b/include/llvm/MC/SectionKind.h @@ -133,6 +133,10 @@ class SectionKind { } K : 8; public: +#if 1 +// for debugging purposes only + Kind getKindEnum(void) const { return K; } +#endif bool isMetadata() const { return K == Metadata; } bool isText() const { return K == Text; } diff --git a/include/llvm/Object/RelocVisitor.h b/include/llvm/Object/RelocVisitor.h index 97912fe..8538f41 100644 --- a/include/llvm/Object/RelocVisitor.h +++ b/include/llvm/Object/RelocVisitor.h @@ -128,7 +128,7 @@ public: return RelocToApply(); } - bool error() { return HasError; } + bool error() const { return HasError; } private: StringRef FileFormat; diff --git a/include/llvm/Support/AlignOf.h b/include/llvm/Support/AlignOf.h index bba3424..7faeb06 100644 --- a/include/llvm/Support/AlignOf.h +++ b/include/llvm/Support/AlignOf.h @@ -36,18 +36,20 @@ private: /// compile-time constant (e.g., for template instantiation). template struct AlignOf { - enum { Alignment = + enum AlignEnum { Alignment = static_cast(sizeof(AlignmentCalcImpl) - sizeof(T)) }; - enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 }; - enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 }; - enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 }; - enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 }; + enum AlignPredicates { + Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0, + Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0, + Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0, + Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0, - enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 }; - enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 }; - enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 }; - enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 }; + Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0, + Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0, + Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0, + Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 + }; }; /// alignOf - A templated function that returns the minimum alignment of diff --git a/include/llvm/Support/Atomic.h b/include/llvm/Support/Atomic.h index 9ec23e8..f1a1fd9 100644 --- a/include/llvm/Support/Atomic.h +++ b/include/llvm/Support/Atomic.h @@ -14,14 +14,21 @@ #ifndef LLVM_SUPPORT_ATOMIC_H #define LLVM_SUPPORT_ATOMIC_H +#include "llvm/Support/Compiler.h" // for __GNUC_PREREQ #include "llvm/Support/DataTypes.h" +// convenience macro, to force use of darwin atomic functions +// stage 1 with gcc-4.0 needs this, but maybe not stage 2? +#define USE_DARWIN_ATOMICS (defined(__APPLE__) && !__GNUC_PREREQ(4, 2)) + namespace llvm { namespace sys { void MemoryFence(); #ifdef _MSC_VER typedef long cas_flag; +#elif USE_DARWIN_ATOMICS + typedef int32_t cas_flag; #else typedef uint32_t cas_flag; #endif diff --git a/include/llvm/Support/Compiler.h b/include/llvm/Support/Compiler.h index 860f43e..d0e6bbb 100644 --- a/include/llvm/Support/Compiler.h +++ b/include/llvm/Support/Compiler.h @@ -179,7 +179,8 @@ #define LLVM_ATTRIBUTE_USED #endif -#if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4) +// gcc-4.0 spews tons of warning noise, so requiring 4.1 should silence them +#if __has_attribute(warn_unused_result) || __GNUC_PREREQ(4, 1) #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__)) #else #define LLVM_ATTRIBUTE_UNUSED_RESULT @@ -216,7 +217,9 @@ #define LLVM_READNONE #endif -#if __has_attribute(pure) || defined(__GNUC__) +// this attribute may be buggy for older gcc-4.0.1 (apple) +// removing this fixes bug #14244 +#if __has_attribute(pure) || __GNUC_PREREQ(4, 2) // aka 'PURE' but following LLVM Conventions. #define LLVM_READONLY __attribute__((__pure__)) #else diff --git a/include/llvm/Support/Format.h b/include/llvm/Support/Format.h index aaa54e1..75b249d 100644 --- a/include/llvm/Support/Format.h +++ b/include/llvm/Support/Format.h @@ -231,6 +231,43 @@ inline format_object5 format(const char *Fmt,const T1 &Val1, return format_object5(Fmt, Val1, Val2, Val3, Val4, Val5); } +// provide some default format strings +template +struct default_format_string; + +// definitions in Support/Format.cpp +#define SPECIALIZE_DEFAULT_FORMAT_STRING(T) \ +template <> \ +struct default_format_string { \ + static const char hex[]; /* e.g. "%d" */ \ + static const char dec[]; /* e.g. "%x" */ \ +}; + +SPECIALIZE_DEFAULT_FORMAT_STRING(int) +SPECIALIZE_DEFAULT_FORMAT_STRING(long) +SPECIALIZE_DEFAULT_FORMAT_STRING(long long) +SPECIALIZE_DEFAULT_FORMAT_STRING(unsigned int) +SPECIALIZE_DEFAULT_FORMAT_STRING(unsigned long) +SPECIALIZE_DEFAULT_FORMAT_STRING(unsigned long long) + +#undef SPECIALIZE_DEFAULT_FORMAT_STRING + +// format manipulators +template +inline +format_object1 +hex(const T& v1) { + return format(default_format_string::hex, v1); +} + +// decimal is the default, so this isn't really needed +template +inline +format_object1 +dec(const T& v1) { + return format(default_format_string::dec, v1); +} + } // end namespace llvm #endif diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h index ec7e06b..09cad11 100644 --- a/include/llvm/Support/raw_ostream.h +++ b/include/llvm/Support/raw_ostream.h @@ -192,6 +192,11 @@ public: raw_ostream &operator<<(double N); + /// basic manipulator, unary function + raw_ostream& operator<<(raw_ostream& (*pf)(raw_ostream&)) { + return (*pf)(*this); + } + /// write_hex - Output \p N in hexadecimal, without any prefix or padding. raw_ostream &write_hex(unsigned long long N); @@ -417,6 +422,14 @@ raw_ostream &errs(); /// output. raw_ostream &nulls(); +/// manipulator functions +inline +raw_ostream& endl(raw_ostream& o) { + o << '\n'; + o.flush(); + return o; +} + //===----------------------------------------------------------------------===// // Output Stream Adaptors //===----------------------------------------------------------------------===// diff --git a/include/llvm/Support/raw_ostream_iterator.h b/include/llvm/Support/raw_ostream_iterator.h new file mode 100644 index 0000000..72d6065 --- /dev/null +++ b/include/llvm/Support/raw_ostream_iterator.h @@ -0,0 +1,79 @@ +//===--- raw_ostream.h - Raw output stream ----------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the raw_ostream_iterator class. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_RAW_OSTREAM_ITERATOR_H +#define LLVM_SUPPORT_RAW_OSTREAM_ITERATOR_H + +#include "llvm/Support/raw_ostream.h" +#include + +namespace llvm { + +/// in the spirit of std::ostream_iterator +template +class ostream_iterator : + public std::iterator +{ +public: + //@{ + /// Public typedef + typedef T value_type; + typedef raw_ostream ostream_type; + //@} + +private: + ostream_type* _M_stream; + const char* _M_delim; + +public: + /// Construct from an ostream. + ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_delim(0) {} + + /** + * Construct from an ostream with optional delimiter. + * @param s Underlying ostream to write to. + * @param c CharT delimiter string to insert. + */ + ostream_iterator(ostream_type& __s, const char* __c) : + _M_stream(&__s), _M_delim(__c) { } + + /// Copy constructor. + ostream_iterator(const ostream_iterator& __obj) : + _M_stream(__obj._M_stream), _M_delim(__obj._M_delim) { } + + /// Writes @a value to underlying ostream using operator<<. If + /// constructed with delimiter string, writes delimiter to ostream. + ostream_iterator& + operator=(const T& __value) + { + *_M_stream << __value; + if (_M_delim) *_M_stream << _M_delim; + return *this; + } + + ostream_iterator& + operator*() + { return *this; } + + ostream_iterator& + operator++() + { return *this; } + + ostream_iterator& + operator++(int) + { return *this; } +}; // end class raw_ostream_iterator + +} // end llvm namespace + +#endif diff --git a/lib/AsmParser/LLParser.h b/lib/AsmParser/LLParser.h index ded776c..391a03f 100644 --- a/lib/AsmParser/LLParser.h +++ b/lib/AsmParser/LLParser.h @@ -43,7 +43,7 @@ namespace llvm { /// type can depend on later context. This may either be a numeric reference /// or a symbolic (%var) reference. This is just a discriminated union. struct ValID { - enum { + enum KindEnum { t_LocalID, t_GlobalID, // ID in UIntVal. t_LocalName, t_GlobalName, // Name in StrVal. t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal. diff --git a/lib/CodeGen/AsmPrinter/CMakeLists.txt b/lib/CodeGen/AsmPrinter/CMakeLists.txt index be484a6..2ad24e1 100644 --- a/lib/CodeGen/AsmPrinter/CMakeLists.txt +++ b/lib/CodeGen/AsmPrinter/CMakeLists.txt @@ -7,9 +7,9 @@ add_llvm_library(LLVMAsmPrinter DIEHash.cpp DwarfAccelTable.cpp DwarfCFIException.cpp - DwarfCompileUnit.cpp DwarfDebug.cpp DwarfException.cpp + DwarfUnit.cpp ErlangGCPrinter.cpp OcamlGCPrinter.cpp Win64Exception.cpp diff --git a/lib/CodeGen/AsmPrinter/DIEHash.cpp b/lib/CodeGen/AsmPrinter/DIEHash.cpp index 95eca90..a8ba298 100644 --- a/lib/CodeGen/AsmPrinter/DIEHash.cpp +++ b/lib/CodeGen/AsmPrinter/DIEHash.cpp @@ -16,7 +16,7 @@ #include "DIEHash.h" #include "DIE.h" -#include "DwarfCompileUnit.h" +//#include "DwarfCompileUnit.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/Debug.h" diff --git a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp b/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp deleted file mode 100644 index a6ff953..0000000 --- a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp +++ /dev/null @@ -1,1956 +0,0 @@ -//===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file contains support for constructing a dwarf compile unit. -// -//===----------------------------------------------------------------------===// - -#define DEBUG_TYPE "dwarfdebug" - -#include "DwarfCompileUnit.h" -#include "DwarfAccelTable.h" -#include "DwarfDebug.h" -#include "llvm/ADT/APFloat.h" -#include "llvm/DIBuilder.h" -#include "llvm/IR/Constants.h" -#include "llvm/IR/DataLayout.h" -#include "llvm/IR/GlobalVariable.h" -#include "llvm/IR/Instructions.h" -#include "llvm/MC/MCSection.h" -#include "llvm/MC/MCStreamer.h" -#include "llvm/Target/Mangler.h" -#include "llvm/Target/TargetFrameLowering.h" -#include "llvm/Target/TargetMachine.h" -#include "llvm/Target/TargetLoweringObjectFile.h" -#include "llvm/Target/TargetRegisterInfo.h" - -using namespace llvm; - -/// CompileUnit - Compile unit constructor. -CompileUnit::CompileUnit(unsigned UID, DIE *D, DICompileUnit Node, - AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU) - : UniqueID(UID), Node(Node), CUDie(D), Asm(A), DD(DW), DU(DWU), - IndexTyDie(0), DebugInfoOffset(0) { - DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1); - insertDIE(Node, D); -} - -/// ~CompileUnit - Destructor for compile unit. -CompileUnit::~CompileUnit() { - for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) - DIEBlocks[j]->~DIEBlock(); -} - -/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug -/// information entry. -DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) { - DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry); - return Value; -} - -/// getDefaultLowerBound - Return the default lower bound for an array. If the -/// DWARF version doesn't handle the language, return -1. -int64_t CompileUnit::getDefaultLowerBound() const { - switch (getLanguage()) { - default: - break; - - case dwarf::DW_LANG_C89: - case dwarf::DW_LANG_C99: - case dwarf::DW_LANG_C: - case dwarf::DW_LANG_C_plus_plus: - case dwarf::DW_LANG_ObjC: - case dwarf::DW_LANG_ObjC_plus_plus: - return 0; - - case dwarf::DW_LANG_Fortran77: - case dwarf::DW_LANG_Fortran90: - case dwarf::DW_LANG_Fortran95: - return 1; - - // The languages below have valid values only if the DWARF version >= 4. - case dwarf::DW_LANG_Java: - case dwarf::DW_LANG_Python: - case dwarf::DW_LANG_UPC: - case dwarf::DW_LANG_D: - if (dwarf::DWARF_VERSION >= 4) - return 0; - break; - - case dwarf::DW_LANG_Ada83: - case dwarf::DW_LANG_Ada95: - case dwarf::DW_LANG_Cobol74: - case dwarf::DW_LANG_Cobol85: - case dwarf::DW_LANG_Modula2: - case dwarf::DW_LANG_Pascal83: - case dwarf::DW_LANG_PLI: - if (dwarf::DWARF_VERSION >= 4) - return 1; - break; - } - - return -1; -} - -/// Check whether the DIE for this MDNode can be shared across CUs. -static bool isShareableAcrossCUs(DIDescriptor D) { - // When the MDNode can be part of the type system, the DIE can be - // shared across CUs. - return D.isType() || - (D.isSubprogram() && !DISubprogram(D).isDefinition()); -} - -/// getDIE - Returns the debug information entry map slot for the -/// specified debug variable. We delegate the request to DwarfDebug -/// when the DIE for this MDNode can be shared across CUs. The mappings -/// will be kept in DwarfDebug for shareable DIEs. -DIE *CompileUnit::getDIE(DIDescriptor D) const { - if (isShareableAcrossCUs(D)) - return DD->getDIE(D); - return MDNodeToDieMap.lookup(D); -} - -/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug -/// when the DIE for this MDNode can be shared across CUs. The mappings -/// will be kept in DwarfDebug for shareable DIEs. -void CompileUnit::insertDIE(DIDescriptor Desc, DIE *D) { - if (isShareableAcrossCUs(Desc)) { - DD->insertDIE(Desc, D); - return; - } - MDNodeToDieMap.insert(std::make_pair(Desc, D)); -} - -/// addFlag - Add a flag that is true. -void CompileUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) { - if (DD->getDwarfVersion() >= 4) - Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne); - else - Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne); -} - -/// addUInt - Add an unsigned integer attribute data and value. -/// -void CompileUnit::addUInt(DIE *Die, dwarf::Attribute Attribute, - Optional Form, uint64_t Integer) { - if (!Form) - Form = DIEInteger::BestForm(false, Integer); - DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator) - DIEInteger(Integer); - Die->addValue(Attribute, *Form, Value); -} - -void CompileUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) { - addUInt(Block, (dwarf::Attribute)0, Form, Integer); -} - -/// addSInt - Add an signed integer attribute data and value. -/// -void CompileUnit::addSInt(DIE *Die, dwarf::Attribute Attribute, - Optional Form, int64_t Integer) { - if (!Form) - Form = DIEInteger::BestForm(true, Integer); - DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); - Die->addValue(Attribute, *Form, Value); -} - -void CompileUnit::addSInt(DIEBlock *Die, Optional Form, - int64_t Integer) { - addSInt(Die, (dwarf::Attribute)0, Form, Integer); -} - -/// addString - Add a string attribute data and value. We always emit a -/// reference to the string pool instead of immediate strings so that DIEs have -/// more predictable sizes. In the case of split dwarf we emit an index -/// into another table which gets us the static offset into the string -/// table. -void CompileUnit::addString(DIE *Die, dwarf::Attribute Attribute, - StringRef String) { - DIEValue *Value; - dwarf::Form Form; - if (!DD->useSplitDwarf()) { - MCSymbol *Symb = DU->getStringPoolEntry(String); - if (Asm->needsRelocationsForDwarfStringPool()) - Value = new (DIEValueAllocator) DIELabel(Symb); - else { - MCSymbol *StringPool = DU->getStringPoolSym(); - Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); - } - Form = dwarf::DW_FORM_strp; - } else { - unsigned idx = DU->getStringPoolIndex(String); - Value = new (DIEValueAllocator) DIEInteger(idx); - Form = dwarf::DW_FORM_GNU_str_index; - } - DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String); - Die->addValue(Attribute, Form, Str); -} - -/// addLocalString - Add a string attribute data and value. This is guaranteed -/// to be in the local string pool instead of indirected. -void CompileUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute, - StringRef String) { - MCSymbol *Symb = DU->getStringPoolEntry(String); - DIEValue *Value; - if (Asm->needsRelocationsForDwarfStringPool()) - Value = new (DIEValueAllocator) DIELabel(Symb); - else { - MCSymbol *StringPool = DU->getStringPoolSym(); - Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); - } - Die->addValue(Attribute, dwarf::DW_FORM_strp, Value); -} - -/// addExpr - Add a Dwarf expression attribute data and value. -/// -void CompileUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) { - DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr); - Die->addValue((dwarf::Attribute)0, Form, Value); -} - -/// addLabel - Add a Dwarf label attribute data and value. -/// -void CompileUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, - dwarf::Form Form, const MCSymbol *Label) { - DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); - Die->addValue(Attribute, Form, Value); -} - -void CompileUnit::addLabel(DIEBlock *Die, dwarf::Form Form, - const MCSymbol *Label) { - addLabel(Die, (dwarf::Attribute)0, Form, Label); -} - -/// addLabelAddress - Add a dwarf label attribute data and value using -/// DW_FORM_addr or DW_FORM_GNU_addr_index. -/// -void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute, - MCSymbol *Label) { - if (Label) - DD->addArangeLabel(SymbolCU(this, Label)); - - if (!DD->useSplitDwarf()) { - if (Label != NULL) { - DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); - Die->addValue(Attribute, dwarf::DW_FORM_addr, Value); - } else { - DIEValue *Value = new (DIEValueAllocator) DIEInteger(0); - Die->addValue(Attribute, dwarf::DW_FORM_addr, Value); - } - } else { - unsigned idx = DU->getAddrPoolIndex(Label); - DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); - Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value); - } -} - -/// addOpAddress - Add a dwarf op address data and value using the -/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index. -/// -void CompileUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) { - DD->addArangeLabel(SymbolCU(this, Sym)); - if (!DD->useSplitDwarf()) { - addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); - addLabel(Die, dwarf::DW_FORM_udata, Sym); - } else { - addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index); - addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym)); - } -} - -/// addDelta - Add a label delta attribute data and value. -/// -void CompileUnit::addDelta(DIE *Die, dwarf::Attribute Attribute, - dwarf::Form Form, const MCSymbol *Hi, - const MCSymbol *Lo) { - DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); - Die->addValue(Attribute, Form, Value); -} - -/// addDIEEntry - Add a DIE attribute data and value. -/// -void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, - DIE *Entry) { - addDIEEntry(Die, Attribute, createDIEEntry(Entry)); -} - -void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, - DIEEntry *Entry) { - const DIE *DieCU = Die->getCompileUnitOrNull(); - const DIE *EntryCU = Entry->getEntry()->getCompileUnitOrNull(); - if (!DieCU) - // We assume that Die belongs to this CU, if it is not linked to any CU yet. - DieCU = getCUDie(); - if (!EntryCU) - EntryCU = getCUDie(); - Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4 - : dwarf::DW_FORM_ref_addr, - Entry); -} - -/// Create a DIE with the given Tag, add the DIE to its parent, and -/// call insertDIE if MD is not null. -DIE *CompileUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) { - DIE *Die = new DIE(Tag); - Parent.addChild(Die); - if (N) - insertDIE(N, Die); - return Die; -} - -/// addBlock - Add block data. -/// -void CompileUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, - DIEBlock *Block) { - Block->ComputeSize(Asm); - DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. - Die->addValue(Attribute, Block->BestForm(), Block); -} - -/// addSourceLine - Add location information to specified debug information -/// entry. -void CompileUnit::addSourceLine(DIE *Die, DIVariable V) { - // Verify variable. - if (!V.isVariable()) - return; - - unsigned Line = V.getLineNumber(); - if (Line == 0) - return; - unsigned FileID = - DD->getOrCreateSourceID(V.getContext().getFilename(), - V.getContext().getDirectory(), getUniqueID()); - assert(FileID && "Invalid file id"); - addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); - addUInt(Die, dwarf::DW_AT_decl_line, None, Line); -} - -/// addSourceLine - Add location information to specified debug information -/// entry. -void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) { - // Verify global variable. - if (!G.isGlobalVariable()) - return; - - unsigned Line = G.getLineNumber(); - if (Line == 0) - return; - unsigned FileID = - DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID()); - assert(FileID && "Invalid file id"); - addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); - addUInt(Die, dwarf::DW_AT_decl_line, None, Line); -} - -/// addSourceLine - Add location information to specified debug information -/// entry. -void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) { - // Verify subprogram. - if (!SP.isSubprogram()) - return; - - // If the line number is 0, don't add it. - unsigned Line = SP.getLineNumber(); - if (Line == 0) - return; - - unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(), - getUniqueID()); - assert(FileID && "Invalid file id"); - addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); - addUInt(Die, dwarf::DW_AT_decl_line, None, Line); -} - -/// addSourceLine - Add location information to specified debug information -/// entry. -void CompileUnit::addSourceLine(DIE *Die, DIType Ty) { - // Verify type. - if (!Ty.isType()) - return; - - unsigned Line = Ty.getLineNumber(); - if (Line == 0) - return; - unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(), - getUniqueID()); - assert(FileID && "Invalid file id"); - addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); - addUInt(Die, dwarf::DW_AT_decl_line, None, Line); -} - -/// addSourceLine - Add location information to specified debug information -/// entry. -void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) { - // Verify type. - if (!Ty.isObjCProperty()) - return; - - unsigned Line = Ty.getLineNumber(); - if (Line == 0) - return; - DIFile File = Ty.getFile(); - unsigned FileID = DD->getOrCreateSourceID(File.getFilename(), - File.getDirectory(), getUniqueID()); - assert(FileID && "Invalid file id"); - addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); - addUInt(Die, dwarf::DW_AT_decl_line, None, Line); -} - -/// addSourceLine - Add location information to specified debug information -/// entry. -void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) { - // Verify namespace. - if (!NS.Verify()) - return; - - unsigned Line = NS.getLineNumber(); - if (Line == 0) - return; - StringRef FN = NS.getFilename(); - - unsigned FileID = - DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID()); - assert(FileID && "Invalid file id"); - addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); - addUInt(Die, dwarf::DW_AT_decl_line, None, Line); -} - -/// addVariableAddress - Add DW_AT_location attribute for a -/// DbgVariable based on provided MachineLocation. -void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die, - MachineLocation Location) { - if (DV.variableHasComplexAddress()) - addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); - else if (DV.isBlockByrefVariable()) - addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); - else - addAddress(Die, dwarf::DW_AT_location, Location, - DV.getVariable().isIndirect()); -} - -/// addRegisterOp - Add register operand. -void CompileUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) { - const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); - unsigned DWReg = RI->getDwarfRegNum(Reg, false); - if (DWReg < 32) - addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg); - else { - addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); - addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); - } -} - -/// addRegisterOffset - Add register offset. -void CompileUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg, - int64_t Offset) { - const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); - unsigned DWReg = RI->getDwarfRegNum(Reg, false); - const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); - if (Reg == TRI->getFrameRegister(*Asm->MF)) - // If variable offset is based in frame register then use fbreg. - addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg); - else if (DWReg < 32) - addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg); - else { - addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); - addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); - } - addSInt(TheDie, dwarf::DW_FORM_sdata, Offset); -} - -/// addAddress - Add an address attribute to a die based on the location -/// provided. -void CompileUnit::addAddress(DIE *Die, dwarf::Attribute Attribute, - const MachineLocation &Location, bool Indirect) { - DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); - - if (Location.isReg() && !Indirect) - addRegisterOp(Block, Location.getReg()); - else { - addRegisterOffset(Block, Location.getReg(), Location.getOffset()); - if (Indirect && !Location.isReg()) { - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); - } - } - - // Now attach the location information to the DIE. - addBlock(Die, Attribute, Block); -} - -/// addComplexAddress - Start with the address based on the location provided, -/// and generate the DWARF information necessary to find the actual variable -/// given the extra address information encoded in the DIVariable, starting from -/// the starting location. Add the DWARF information to the die. -/// -void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die, - dwarf::Attribute Attribute, - const MachineLocation &Location) { - DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); - unsigned N = DV.getNumAddrElements(); - unsigned i = 0; - if (Location.isReg()) { - if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) { - // If first address element is OpPlus then emit - // DW_OP_breg + Offset instead of DW_OP_reg + Offset. - addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1)); - i = 2; - } else - addRegisterOp(Block, Location.getReg()); - } else - addRegisterOffset(Block, Location.getReg(), Location.getOffset()); - - for (; i < N; ++i) { - uint64_t Element = DV.getAddrElement(i); - if (Element == DIBuilder::OpPlus) { - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); - addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i)); - } else if (Element == DIBuilder::OpDeref) { - if (!Location.isReg()) - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); - } else - llvm_unreachable("unknown DIBuilder Opcode"); - } - - // Now attach the location information to the DIE. - addBlock(Die, Attribute, Block); -} - -/* Byref variables, in Blocks, are declared by the programmer as "SomeType - VarName;", but the compiler creates a __Block_byref_x_VarName struct, and - gives the variable VarName either the struct, or a pointer to the struct, as - its type. This is necessary for various behind-the-scenes things the - compiler needs to do with by-reference variables in Blocks. - - However, as far as the original *programmer* is concerned, the variable - should still have type 'SomeType', as originally declared. - - The function getBlockByrefType dives into the __Block_byref_x_VarName - struct to find the original type of the variable, which is then assigned to - the variable's Debug Information Entry as its real type. So far, so good. - However now the debugger will expect the variable VarName to have the type - SomeType. So we need the location attribute for the variable to be an - expression that explains to the debugger how to navigate through the - pointers and struct to find the actual variable of type SomeType. - - The following function does just that. We start by getting - the "normal" location for the variable. This will be the location - of either the struct __Block_byref_x_VarName or the pointer to the - struct __Block_byref_x_VarName. - - The struct will look something like: - - struct __Block_byref_x_VarName { - ... - struct __Block_byref_x_VarName *forwarding; - ... - SomeType VarName; - ... - }; - - If we are given the struct directly (as our starting point) we - need to tell the debugger to: - - 1). Add the offset of the forwarding field. - - 2). Follow that pointer to get the real __Block_byref_x_VarName - struct to use (the real one may have been copied onto the heap). - - 3). Add the offset for the field VarName, to find the actual variable. - - If we started with a pointer to the struct, then we need to - dereference that pointer first, before the other steps. - Translating this into DWARF ops, we will need to append the following - to the current location description for the variable: - - DW_OP_deref -- optional, if we start with a pointer - DW_OP_plus_uconst - DW_OP_deref - DW_OP_plus_uconst - - That is what this function does. */ - -/// addBlockByrefAddress - Start with the address based on the location -/// provided, and generate the DWARF information necessary to find the -/// actual Block variable (navigating the Block struct) based on the -/// starting location. Add the DWARF information to the die. For -/// more information, read large comment just above here. -/// -void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die, - dwarf::Attribute Attribute, - const MachineLocation &Location) { - DIType Ty = DV.getType(); - DIType TmpTy = Ty; - uint16_t Tag = Ty.getTag(); - bool isPointer = false; - - StringRef varName = DV.getName(); - - if (Tag == dwarf::DW_TAG_pointer_type) { - DIDerivedType DTy(Ty); - TmpTy = resolve(DTy.getTypeDerivedFrom()); - isPointer = true; - } - - DICompositeType blockStruct(TmpTy); - - // Find the __forwarding field and the variable field in the __Block_byref - // struct. - DIArray Fields = blockStruct.getTypeArray(); - DIDerivedType varField; - DIDerivedType forwardingField; - - for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { - DIDerivedType DT(Fields.getElement(i)); - StringRef fieldName = DT.getName(); - if (fieldName == "__forwarding") - forwardingField = DT; - else if (fieldName == varName) - varField = DT; - } - - // Get the offsets for the forwarding field and the variable field. - unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3; - unsigned varFieldOffset = varField.getOffsetInBits() >> 2; - - // Decode the original location, and use that as the start of the byref - // variable's location. - DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); - - if (Location.isReg()) - addRegisterOp(Block, Location.getReg()); - else - addRegisterOffset(Block, Location.getReg(), Location.getOffset()); - - // If we started with a pointer to the __Block_byref... struct, then - // the first thing we need to do is dereference the pointer (DW_OP_deref). - if (isPointer) - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); - - // Next add the offset for the '__forwarding' field: - // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in - // adding the offset if it's 0. - if (forwardingFieldOffset > 0) { - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); - addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset); - } - - // Now dereference the __forwarding field to get to the real __Block_byref - // struct: DW_OP_deref. - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); - - // Now that we've got the real __Block_byref... struct, add the offset - // for the variable's field to get to the location of the actual variable: - // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. - if (varFieldOffset > 0) { - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); - addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset); - } - - // Now attach the location information to the DIE. - addBlock(Die, Attribute, Block); -} - -/// isTypeSigned - Return true if the type is signed. -static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) { - if (Ty.isDerivedType()) - return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()), - SizeInBits); - if (Ty.isBasicType()) - if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed || - DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) { - *SizeInBits = Ty.getSizeInBits(); - return true; - } - return false; -} - -/// Return true if type encoding is unsigned. -static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) { - DIDerivedType DTy(Ty); - if (DTy.isDerivedType()) - return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom())); - - DIBasicType BTy(Ty); - if (BTy.isBasicType()) { - unsigned Encoding = BTy.getEncoding(); - if (Encoding == dwarf::DW_ATE_unsigned || - Encoding == dwarf::DW_ATE_unsigned_char || - Encoding == dwarf::DW_ATE_boolean) - return true; - } - return false; -} - -/// If this type is derived from a base type then return base type size. -static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) { - unsigned Tag = Ty.getTag(); - - if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && - Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && - Tag != dwarf::DW_TAG_restrict_type) - return Ty.getSizeInBits(); - - DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom()); - - // If this type is not derived from any type then take conservative approach. - if (!BaseType.isValid()) - return Ty.getSizeInBits(); - - // If this is a derived type, go ahead and get the base type, unless it's a - // reference then it's just the size of the field. Pointer types have no need - // of this since they're a different type of qualification on the type. - if (BaseType.getTag() == dwarf::DW_TAG_reference_type || - BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type) - return Ty.getSizeInBits(); - - if (BaseType.isDerivedType()) - return getBaseTypeSize(DD, DIDerivedType(BaseType)); - - return BaseType.getSizeInBits(); -} - -/// addConstantValue - Add constant value entry in variable DIE. -void CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO, - DIType Ty) { - // FIXME: This is a bit conservative/simple - it emits negative values at - // their maximum bit width which is a bit unfortunate (& doesn't prefer - // udata/sdata over dataN as suggested by the DWARF spec) - assert(MO.isImm() && "Invalid machine operand!"); - int SizeInBits = -1; - bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits); - dwarf::Form Form; - - // If we're a signed constant definitely use sdata. - if (SignedConstant) { - addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm()); - return; - } - - // Else use data for now unless it's larger than we can deal with. - switch (SizeInBits) { - case 8: - Form = dwarf::DW_FORM_data1; - break; - case 16: - Form = dwarf::DW_FORM_data2; - break; - case 32: - Form = dwarf::DW_FORM_data4; - break; - case 64: - Form = dwarf::DW_FORM_data8; - break; - default: - Form = dwarf::DW_FORM_udata; - addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm()); - return; - } - addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm()); -} - -/// addConstantFPValue - Add constant value entry in variable DIE. -void CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) { - assert(MO.isFPImm() && "Invalid machine operand!"); - DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); - APFloat FPImm = MO.getFPImm()->getValueAPF(); - - // Get the raw data form of the floating point. - const APInt FltVal = FPImm.bitcastToAPInt(); - const char *FltPtr = (const char *)FltVal.getRawData(); - - int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. - bool LittleEndian = Asm->getDataLayout().isLittleEndian(); - int Incr = (LittleEndian ? 1 : -1); - int Start = (LittleEndian ? 0 : NumBytes - 1); - int Stop = (LittleEndian ? NumBytes : -1); - - // Output the constant to DWARF one byte at a time. - for (; Start != Stop; Start += Incr) - addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]); - - addBlock(Die, dwarf::DW_AT_const_value, Block); -} - -/// addConstantFPValue - Add constant value entry in variable DIE. -void CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) { - // Pass this down to addConstantValue as an unsigned bag of bits. - addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true); -} - -/// addConstantValue - Add constant value entry in variable DIE. -void CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI, - bool Unsigned) { - addConstantValue(Die, CI->getValue(), Unsigned); -} - -// addConstantValue - Add constant value entry in variable DIE. -void CompileUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) { - unsigned CIBitWidth = Val.getBitWidth(); - if (CIBitWidth <= 64) { - // If we're a signed constant definitely use sdata. - if (!Unsigned) { - addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, - Val.getSExtValue()); - return; - } - - // Else use data for now unless it's larger than we can deal with. - dwarf::Form Form; - switch (CIBitWidth) { - case 8: - Form = dwarf::DW_FORM_data1; - break; - case 16: - Form = dwarf::DW_FORM_data2; - break; - case 32: - Form = dwarf::DW_FORM_data4; - break; - case 64: - Form = dwarf::DW_FORM_data8; - break; - default: - addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata, - Val.getZExtValue()); - return; - } - addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue()); - return; - } - - DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); - - // Get the raw data form of the large APInt. - const uint64_t *Ptr64 = Val.getRawData(); - - int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. - bool LittleEndian = Asm->getDataLayout().isLittleEndian(); - - // Output the constant to DWARF one byte at a time. - for (int i = 0; i < NumBytes; i++) { - uint8_t c; - if (LittleEndian) - c = Ptr64[i / 8] >> (8 * (i & 7)); - else - c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7)); - addUInt(Block, dwarf::DW_FORM_data1, c); - } - - addBlock(Die, dwarf::DW_AT_const_value, Block); -} - -/// addTemplateParams - Add template parameters into buffer. -void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) { - // Add template parameters. - for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) { - DIDescriptor Element = TParams.getElement(i); - if (Element.isTemplateTypeParameter()) - constructTemplateTypeParameterDIE(Buffer, - DITemplateTypeParameter(Element)); - else if (Element.isTemplateValueParameter()) - constructTemplateValueParameterDIE(Buffer, - DITemplateValueParameter(Element)); - } -} - -/// getOrCreateContextDIE - Get context owner's DIE. -DIE *CompileUnit::getOrCreateContextDIE(DIScope Context) { - if (!Context || Context.isFile()) - return getCUDie(); - if (Context.isType()) - return getOrCreateTypeDIE(DIType(Context)); - if (Context.isNameSpace()) - return getOrCreateNameSpace(DINameSpace(Context)); - if (Context.isSubprogram()) - return getOrCreateSubprogramDIE(DISubprogram(Context)); - return getDIE(Context); -} - -/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the -/// given DIType. -DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) { - if (!TyNode) - return NULL; - - DIType Ty(TyNode); - assert(Ty.isType()); - - // Construct the context before querying for the existence of the DIE in case - // such construction creates the DIE. - DIE *ContextDIE = getOrCreateContextDIE(resolve(Ty.getContext())); - assert(ContextDIE); - - DIE *TyDIE = getDIE(Ty); - if (TyDIE) - return TyDIE; - - // Create new type. - TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty); - - if (Ty.isBasicType()) - constructTypeDIE(*TyDIE, DIBasicType(Ty)); - else if (Ty.isCompositeType()) - constructTypeDIE(*TyDIE, DICompositeType(Ty)); - else { - assert(Ty.isDerivedType() && "Unknown kind of DIType"); - constructTypeDIE(*TyDIE, DIDerivedType(Ty)); - } - // If this is a named finished type then include it in the list of types - // for the accelerator tables. - if (!Ty.getName().empty() && !Ty.isForwardDecl()) { - bool IsImplementation = 0; - if (Ty.isCompositeType()) { - DICompositeType CT(Ty); - // A runtime language of 0 actually means C/C++ and that any - // non-negative value is some version of Objective-C/C++. - IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete(); - } - unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0; - addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags)); - } - - return TyDIE; -} - -/// addType - Add a new type attribute to the specified entity. -void CompileUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) { - assert(Ty && "Trying to add a type that doesn't exist?"); - - // Check for pre-existence. - DIEEntry *Entry = getDIEEntry(Ty); - // If it exists then use the existing value. - if (Entry) { - addDIEEntry(Entity, Attribute, Entry); - return; - } - - // Construct type. - DIE *Buffer = getOrCreateTypeDIE(Ty); - - // Set up proxy. - Entry = createDIEEntry(Buffer); - insertDIEEntry(Ty, Entry); - addDIEEntry(Entity, Attribute, Entry); - - // If this is a complete composite type then include it in the - // list of global types. - addGlobalType(Ty); -} - -// Accelerator table mutators - add each name along with its companion -// DIE to the proper table while ensuring that the name that we're going -// to reference is in the string table. We do this since the names we -// add may not only be identical to the names in the DIE. -void CompileUnit::addAccelName(StringRef Name, DIE *Die) { - DU->getStringPoolEntry(Name); - std::vector &DIEs = AccelNames[Name]; - DIEs.push_back(Die); -} - -void CompileUnit::addAccelObjC(StringRef Name, DIE *Die) { - DU->getStringPoolEntry(Name); - std::vector &DIEs = AccelObjC[Name]; - DIEs.push_back(Die); -} - -void CompileUnit::addAccelNamespace(StringRef Name, DIE *Die) { - DU->getStringPoolEntry(Name); - std::vector &DIEs = AccelNamespace[Name]; - DIEs.push_back(Die); -} - -void CompileUnit::addAccelType(StringRef Name, std::pair Die) { - DU->getStringPoolEntry(Name); - std::vector > &DIEs = AccelTypes[Name]; - DIEs.push_back(Die); -} - -/// addGlobalName - Add a new global name to the compile unit. -void CompileUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) { - std::string FullName = getParentContextString(Context) + Name.str(); - GlobalNames[FullName] = Die; -} - -/// addGlobalType - Add a new global type to the compile unit. -/// -void CompileUnit::addGlobalType(DIType Ty) { - DIScope Context = resolve(Ty.getContext()); - if (!Ty.getName().empty() && !Ty.isForwardDecl() && - (!Context || Context.isCompileUnit() || Context.isFile() || - Context.isNameSpace())) - if (DIEEntry *Entry = getDIEEntry(Ty)) { - std::string FullName = - getParentContextString(Context) + Ty.getName().str(); - GlobalTypes[FullName] = Entry->getEntry(); - } -} - -/// getParentContextString - Walks the metadata parent chain in a language -/// specific manner (using the compile unit language) and returns -/// it as a string. This is done at the metadata level because DIEs may -/// not currently have been added to the parent context and walking the -/// DIEs looking for names is more expensive than walking the metadata. -std::string CompileUnit::getParentContextString(DIScope Context) const { - if (!Context) - return ""; - - // FIXME: Decide whether to implement this for non-C++ languages. - if (getLanguage() != dwarf::DW_LANG_C_plus_plus) - return ""; - - std::string CS; - SmallVector Parents; - while (!Context.isCompileUnit()) { - Parents.push_back(Context); - if (Context.getContext()) - Context = resolve(Context.getContext()); - else - // Structure, etc types will have a NULL context if they're at the top - // level. - break; - } - - // Reverse iterate over our list to go from the outermost construct to the - // innermost. - for (SmallVectorImpl::reverse_iterator I = Parents.rbegin(), - E = Parents.rend(); - I != E; ++I) { - DIScope Ctx = *I; - StringRef Name = Ctx.getName(); - if (!Name.empty()) { - CS += Name; - CS += "::"; - } - } - return CS; -} - -/// addPubTypes - Add subprogram argument types for pubtypes section. -void CompileUnit::addPubTypes(DISubprogram SP) { - DICompositeType SPTy = SP.getType(); - uint16_t SPTag = SPTy.getTag(); - if (SPTag != dwarf::DW_TAG_subroutine_type) - return; - - DIArray Args = SPTy.getTypeArray(); - for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) { - DIType ATy(Args.getElement(i)); - if (!ATy.isType()) - continue; - addGlobalType(ATy); - } -} - -/// constructTypeDIE - Construct basic type die from DIBasicType. -void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) { - // Get core information. - StringRef Name = BTy.getName(); - // Add name if not anonymous or intermediate type. - if (!Name.empty()) - addString(&Buffer, dwarf::DW_AT_name, Name); - - // An unspecified type only has a name attribute. - if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) - return; - - addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, - BTy.getEncoding()); - - uint64_t Size = BTy.getSizeInBits() >> 3; - addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); -} - -/// constructTypeDIE - Construct derived type die from DIDerivedType. -void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) { - // Get core information. - StringRef Name = DTy.getName(); - uint64_t Size = DTy.getSizeInBits() >> 3; - uint16_t Tag = Buffer.getTag(); - - // Map to main type, void will not have a type. - DIType FromTy = resolve(DTy.getTypeDerivedFrom()); - if (FromTy) - addType(&Buffer, FromTy); - - // Add name if not anonymous or intermediate type. - if (!Name.empty()) - addString(&Buffer, dwarf::DW_AT_name, Name); - - // Add size if non-zero (derived types might be zero-sized.) - if (Size && Tag != dwarf::DW_TAG_pointer_type) - addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); - - if (Tag == dwarf::DW_TAG_ptr_to_member_type) - addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, - getOrCreateTypeDIE(resolve(DTy.getClassType()))); - // Add source line info if available and TyDesc is not a forward declaration. - if (!DTy.isForwardDecl()) - addSourceLine(&Buffer, DTy); -} - -/// Return true if the type is appropriately scoped to be contained inside -/// its own type unit. -static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) { - DIScope Parent = DD->resolve(Ty.getContext()); - while (Parent) { - // Don't generate a hash for anything scoped inside a function. - if (Parent.isSubprogram()) - return false; - Parent = DD->resolve(Parent.getContext()); - } - return true; -} - -/// Return true if the type should be split out into a type unit. -static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) { - uint16_t Tag = CTy.getTag(); - - switch (Tag) { - case dwarf::DW_TAG_structure_type: - case dwarf::DW_TAG_union_type: - case dwarf::DW_TAG_enumeration_type: - case dwarf::DW_TAG_class_type: - // If this is a class, structure, union, or enumeration type - // that is a definition (not a declaration), and not scoped - // inside a function then separate this out as a type unit. - return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD); - default: - return false; - } -} - -/// constructTypeDIE - Construct type DIE from DICompositeType. -void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) { - // Get core information. - StringRef Name = CTy.getName(); - - uint64_t Size = CTy.getSizeInBits() >> 3; - uint16_t Tag = Buffer.getTag(); - - switch (Tag) { - case dwarf::DW_TAG_array_type: - constructArrayTypeDIE(Buffer, CTy); - break; - case dwarf::DW_TAG_enumeration_type: - constructEnumTypeDIE(Buffer, CTy); - break; - case dwarf::DW_TAG_subroutine_type: { - // Add return type. A void return won't have a type. - DIArray Elements = CTy.getTypeArray(); - DIType RTy(Elements.getElement(0)); - if (RTy) - addType(&Buffer, RTy); - - bool isPrototyped = true; - // Add arguments. - for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) { - DIDescriptor Ty = Elements.getElement(i); - if (Ty.isUnspecifiedParameter()) { - createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer); - isPrototyped = false; - } else { - DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer); - addType(Arg, DIType(Ty)); - if (DIType(Ty).isArtificial()) - addFlag(Arg, dwarf::DW_AT_artificial); - } - } - // Add prototype flag if we're dealing with a C language and the - // function has been prototyped. - uint16_t Language = getLanguage(); - if (isPrototyped && - (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || - Language == dwarf::DW_LANG_ObjC)) - addFlag(&Buffer, dwarf::DW_AT_prototyped); - } break; - case dwarf::DW_TAG_structure_type: - case dwarf::DW_TAG_union_type: - case dwarf::DW_TAG_class_type: { - // Add elements to structure type. - DIArray Elements = CTy.getTypeArray(); - for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { - DIDescriptor Element = Elements.getElement(i); - DIE *ElemDie = NULL; - if (Element.isSubprogram()) { - DISubprogram SP(Element); - ElemDie = getOrCreateSubprogramDIE(SP); - if (SP.isProtected()) - addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, - dwarf::DW_ACCESS_protected); - else if (SP.isPrivate()) - addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, - dwarf::DW_ACCESS_private); - else - addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, - dwarf::DW_ACCESS_public); - if (SP.isExplicit()) - addFlag(ElemDie, dwarf::DW_AT_explicit); - } else if (Element.isDerivedType()) { - DIDerivedType DDTy(Element); - if (DDTy.getTag() == dwarf::DW_TAG_friend) { - ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer); - addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()), - dwarf::DW_AT_friend); - } else if (DDTy.isStaticMember()) { - getOrCreateStaticMemberDIE(DDTy); - } else { - constructMemberDIE(Buffer, DDTy); - } - } else if (Element.isObjCProperty()) { - DIObjCProperty Property(Element); - ElemDie = createAndAddDIE(Property.getTag(), Buffer); - StringRef PropertyName = Property.getObjCPropertyName(); - addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); - addType(ElemDie, Property.getType()); - addSourceLine(ElemDie, Property); - StringRef GetterName = Property.getObjCPropertyGetterName(); - if (!GetterName.empty()) - addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); - StringRef SetterName = Property.getObjCPropertySetterName(); - if (!SetterName.empty()) - addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); - unsigned PropertyAttributes = 0; - if (Property.isReadOnlyObjCProperty()) - PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly; - if (Property.isReadWriteObjCProperty()) - PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite; - if (Property.isAssignObjCProperty()) - PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign; - if (Property.isRetainObjCProperty()) - PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain; - if (Property.isCopyObjCProperty()) - PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy; - if (Property.isNonAtomicObjCProperty()) - PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic; - if (PropertyAttributes) - addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None, - PropertyAttributes); - - DIEEntry *Entry = getDIEEntry(Element); - if (!Entry) { - Entry = createDIEEntry(ElemDie); - insertDIEEntry(Element, Entry); - } - } else - continue; - } - - if (CTy.isAppleBlockExtension()) - addFlag(&Buffer, dwarf::DW_AT_APPLE_block); - - DICompositeType ContainingType(resolve(CTy.getContainingType())); - if (ContainingType) - addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, - getOrCreateTypeDIE(ContainingType)); - - if (CTy.isObjcClassComplete()) - addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type); - - // Add template parameters to a class, structure or union types. - // FIXME: The support isn't in the metadata for this yet. - if (Tag == dwarf::DW_TAG_class_type || - Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) - addTemplateParams(Buffer, CTy.getTemplateParams()); - - break; - } - default: - break; - } - - // Add name if not anonymous or intermediate type. - if (!Name.empty()) - addString(&Buffer, dwarf::DW_AT_name, Name); - - if (Tag == dwarf::DW_TAG_enumeration_type || - Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type || - Tag == dwarf::DW_TAG_union_type) { - // Add size if non-zero (derived types might be zero-sized.) - // TODO: Do we care about size for enum forward declarations? - if (Size) - addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); - else if (!CTy.isForwardDecl()) - // Add zero size if it is not a forward declaration. - addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0); - - // If we're a forward decl, say so. - if (CTy.isForwardDecl()) - addFlag(&Buffer, dwarf::DW_AT_declaration); - - // Add source line info if available. - if (!CTy.isForwardDecl()) - addSourceLine(&Buffer, CTy); - - // No harm in adding the runtime language to the declaration. - unsigned RLang = CTy.getRunTimeLang(); - if (RLang) - addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1, - RLang); - } - // If this is a type applicable to a type unit it then add it to the - // list of types we'll compute a hash for later. - if (shouldCreateTypeUnit(CTy, DD)) - DD->addTypeUnitType(&Buffer); -} - -/// constructTemplateTypeParameterDIE - Construct new DIE for the given -/// DITemplateTypeParameter. -void -CompileUnit::constructTemplateTypeParameterDIE(DIE &Buffer, - DITemplateTypeParameter TP) { - DIE *ParamDIE = - createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer); - // Add the type if it exists, it could be void and therefore no type. - if (TP.getType()) - addType(ParamDIE, resolve(TP.getType())); - if (!TP.getName().empty()) - addString(ParamDIE, dwarf::DW_AT_name, TP.getName()); -} - -/// constructTemplateValueParameterDIE - Construct new DIE for the given -/// DITemplateValueParameter. -void -CompileUnit::constructTemplateValueParameterDIE(DIE &Buffer, - DITemplateValueParameter VP) { - DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer); - - // Add the type if there is one, template template and template parameter - // packs will not have a type. - if (VP.getTag() == dwarf::DW_TAG_template_value_parameter) - addType(ParamDIE, resolve(VP.getType())); - if (!VP.getName().empty()) - addString(ParamDIE, dwarf::DW_AT_name, VP.getName()); - if (Value *Val = VP.getValue()) { - if (ConstantInt *CI = dyn_cast(Val)) - addConstantValue(ParamDIE, CI, - isUnsignedDIType(DD, resolve(VP.getType()))); - else if (GlobalValue *GV = dyn_cast(Val)) { - // For declaration non-type template parameters (such as global values and - // functions) - DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); - addOpAddress(Block, Asm->getSymbol(GV)); - // Emit DW_OP_stack_value to use the address as the immediate value of the - // parameter, rather than a pointer to it. - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); - addBlock(ParamDIE, dwarf::DW_AT_location, Block); - } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) { - assert(isa(Val)); - addString(ParamDIE, dwarf::DW_AT_GNU_template_name, - cast(Val)->getString()); - } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) { - assert(isa(Val)); - DIArray A(cast(Val)); - addTemplateParams(*ParamDIE, A); - } - } -} - -/// getOrCreateNameSpace - Create a DIE for DINameSpace. -DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) { - // Construct the context before querying for the existence of the DIE in case - // such construction creates the DIE. - DIE *ContextDIE = getOrCreateContextDIE(NS.getContext()); - - DIE *NDie = getDIE(NS); - if (NDie) - return NDie; - NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS); - - if (!NS.getName().empty()) { - addString(NDie, dwarf::DW_AT_name, NS.getName()); - addAccelNamespace(NS.getName(), NDie); - addGlobalName(NS.getName(), NDie, NS.getContext()); - } else - addAccelNamespace("(anonymous namespace)", NDie); - addSourceLine(NDie, NS); - return NDie; -} - -/// getOrCreateSubprogramDIE - Create new DIE using SP. -DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) { - // Construct the context before querying for the existence of the DIE in case - // such construction creates the DIE (as is the case for member function - // declarations). - DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext())); - - DIE *SPDie = getDIE(SP); - if (SPDie) - return SPDie; - - DISubprogram SPDecl = SP.getFunctionDeclaration(); - if (SPDecl.isSubprogram()) - // Add subprogram definitions to the CU die directly. - ContextDIE = CUDie.get(); - - // DW_TAG_inlined_subroutine may refer to this DIE. - SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP); - - DIE *DeclDie = NULL; - if (SPDecl.isSubprogram()) - DeclDie = getOrCreateSubprogramDIE(SPDecl); - - // Add function template parameters. - addTemplateParams(*SPDie, SP.getTemplateParams()); - - // If this DIE is going to refer declaration info using AT_specification - // then there is no need to add other attributes. - if (DeclDie) { - // Refer function declaration directly. - addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie); - - return SPDie; - } - - // Add the linkage name if we have one. - StringRef LinkageName = SP.getLinkageName(); - if (!LinkageName.empty()) - addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, - GlobalValue::getRealLinkageName(LinkageName)); - - // Constructors and operators for anonymous aggregates do not have names. - if (!SP.getName().empty()) - addString(SPDie, dwarf::DW_AT_name, SP.getName()); - - addSourceLine(SPDie, SP); - - // Add the prototype if we have a prototype and we have a C like - // language. - uint16_t Language = getLanguage(); - if (SP.isPrototyped() && - (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || - Language == dwarf::DW_LANG_ObjC)) - addFlag(SPDie, dwarf::DW_AT_prototyped); - - DICompositeType SPTy = SP.getType(); - assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type && - "the type of a subprogram should be a subroutine"); - - DIArray Args = SPTy.getTypeArray(); - // Add a return type. If this is a type like a C/C++ void type we don't add a - // return type. - if (Args.getElement(0)) - addType(SPDie, DIType(Args.getElement(0))); - - unsigned VK = SP.getVirtuality(); - if (VK) { - addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); - DIEBlock *Block = getDIEBlock(); - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); - addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex()); - addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block); - ContainingTypeMap.insert( - std::make_pair(SPDie, resolve(SP.getContainingType()))); - } - - if (!SP.isDefinition()) { - addFlag(SPDie, dwarf::DW_AT_declaration); - - // Add arguments. Do not add arguments for subprogram definition. They will - // be handled while processing variables. - for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { - DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie); - DIType ATy(Args.getElement(i)); - addType(Arg, ATy); - if (ATy.isArtificial()) - addFlag(Arg, dwarf::DW_AT_artificial); - } - } - - if (SP.isArtificial()) - addFlag(SPDie, dwarf::DW_AT_artificial); - - if (!SP.isLocalToUnit()) - addFlag(SPDie, dwarf::DW_AT_external); - - if (SP.isOptimized()) - addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); - - if (unsigned isa = Asm->getISAEncoding()) { - addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); - } - - return SPDie; -} - -// Return const expression if value is a GEP to access merged global -// constant. e.g. -// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) -static const ConstantExpr *getMergedGlobalExpr(const Value *V) { - const ConstantExpr *CE = dyn_cast_or_null(V); - if (!CE || CE->getNumOperands() != 3 || - CE->getOpcode() != Instruction::GetElementPtr) - return NULL; - - // First operand points to a global struct. - Value *Ptr = CE->getOperand(0); - if (!isa(Ptr) || - !isa(cast(Ptr->getType())->getElementType())) - return NULL; - - // Second operand is zero. - const ConstantInt *CI = dyn_cast_or_null(CE->getOperand(1)); - if (!CI || !CI->isZero()) - return NULL; - - // Third operand is offset. - if (!isa(CE->getOperand(2))) - return NULL; - - return CE; -} - -/// createGlobalVariableDIE - create global variable DIE. -void CompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) { - - // Check for pre-existence. - if (getDIE(GV)) - return; - - if (!GV.isGlobalVariable()) - return; - - DIScope GVContext = GV.getContext(); - DIType GTy = GV.getType(); - - // If this is a static data member definition, some attributes belong - // to the declaration DIE. - DIE *VariableDIE = NULL; - bool IsStaticMember = false; - DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration(); - if (SDMDecl.Verify()) { - assert(SDMDecl.isStaticMember() && "Expected static member decl"); - // We need the declaration DIE that is in the static member's class. - VariableDIE = getOrCreateStaticMemberDIE(SDMDecl); - IsStaticMember = true; - } - - // If this is not a static data member definition, create the variable - // DIE and add the initial set of attributes to it. - if (!VariableDIE) { - // Construct the context before querying for the existence of the DIE in - // case such construction creates the DIE. - DIE *ContextDIE = getOrCreateContextDIE(GVContext); - - // Add to map. - VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV); - - // Add name and type. - addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); - addType(VariableDIE, GTy); - - // Add scoping info. - if (!GV.isLocalToUnit()) - addFlag(VariableDIE, dwarf::DW_AT_external); - - // Add line number info. - addSourceLine(VariableDIE, GV); - } - - // Add location. - bool addToAccelTable = false; - DIE *VariableSpecDIE = NULL; - bool isGlobalVariable = GV.getGlobal() != NULL; - if (isGlobalVariable) { - addToAccelTable = true; - DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); - const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal()); - if (GV.getGlobal()->isThreadLocal()) { - // FIXME: Make this work with -gsplit-dwarf. - unsigned PointerSize = Asm->getDataLayout().getPointerSize(); - assert((PointerSize == 4 || PointerSize == 8) && - "Add support for other sizes if necessary"); - const MCExpr *Expr = - Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym); - // Based on GCC's support for TLS: - if (!DD->useSplitDwarf()) { - // 1) Start with a constNu of the appropriate pointer size - addUInt(Block, dwarf::DW_FORM_data1, - PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u); - // 2) containing the (relocated) offset of the TLS variable - // within the module's TLS block. - addExpr(Block, dwarf::DW_FORM_udata, Expr); - } else { - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); - addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr)); - } - // 3) followed by a custom OP to make the debugger do a TLS lookup. - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address); - } else - addOpAddress(Block, Sym); - // Do not create specification DIE if context is either compile unit - // or a subprogram. - if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() && - !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) { - // Create specification DIE. - VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *CUDie); - addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE); - addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block); - // A static member's declaration is already flagged as such. - if (!SDMDecl.Verify()) - addFlag(VariableDIE, dwarf::DW_AT_declaration); - } else { - addBlock(VariableDIE, dwarf::DW_AT_location, Block); - } - // Add the linkage name. - StringRef LinkageName = GV.getLinkageName(); - if (!LinkageName.empty()) - // From DWARF4: DIEs to which DW_AT_linkage_name may apply include: - // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and - // TAG_variable. - addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE - : VariableDIE, - dwarf::DW_AT_MIPS_linkage_name, - GlobalValue::getRealLinkageName(LinkageName)); - } else if (const ConstantInt *CI = - dyn_cast_or_null(GV.getConstant())) { - // AT_const_value was added when the static member was created. To avoid - // emitting AT_const_value multiple times, we only add AT_const_value when - // it is not a static member. - if (!IsStaticMember) - addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy)); - } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) { - addToAccelTable = true; - // GV is a merged global. - DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); - Value *Ptr = CE->getOperand(0); - addOpAddress(Block, Asm->getSymbol(cast(Ptr))); - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); - SmallVector Idx(CE->op_begin() + 1, CE->op_end()); - addUInt(Block, dwarf::DW_FORM_udata, - Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); - addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); - addBlock(VariableDIE, dwarf::DW_AT_location, Block); - } - - if (addToAccelTable) { - DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE; - addAccelName(GV.getName(), AddrDIE); - - // If the linkage name is different than the name, go ahead and output - // that as well into the name table. - if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) - addAccelName(GV.getLinkageName(), AddrDIE); - } - - if (!GV.isLocalToUnit()) - addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE, - GV.getContext()); -} - -/// constructSubrangeDIE - Construct subrange DIE from DISubrange. -void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, - DIE *IndexTy) { - DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer); - addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy); - - // The LowerBound value defines the lower bounds which is typically zero for - // C/C++. The Count value is the number of elements. Values are 64 bit. If - // Count == -1 then the array is unbounded and we do not emit - // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and - // Count == 0, then the array has zero elements in which case we do not emit - // an upper bound. - int64_t LowerBound = SR.getLo(); - int64_t DefaultLowerBound = getDefaultLowerBound(); - int64_t Count = SR.getCount(); - - if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound) - addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound); - - if (Count != -1 && Count != 0) - // FIXME: An unbounded array should reference the expression that defines - // the array. - addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None, - LowerBound + Count - 1); -} - -/// constructArrayTypeDIE - Construct array type DIE from DICompositeType. -void CompileUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) { - if (CTy.isVector()) - addFlag(&Buffer, dwarf::DW_AT_GNU_vector); - - // Emit the element type. - addType(&Buffer, resolve(CTy.getTypeDerivedFrom())); - - // Get an anonymous type for index type. - // FIXME: This type should be passed down from the front end - // as different languages may have different sizes for indexes. - DIE *IdxTy = getIndexTyDie(); - if (!IdxTy) { - // Construct an anonymous type for index type. - IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *CUDie.get()); - addString(IdxTy, dwarf::DW_AT_name, "int"); - addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t)); - addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, - dwarf::DW_ATE_signed); - setIndexTyDie(IdxTy); - } - - // Add subranges to array type. - DIArray Elements = CTy.getTypeArray(); - for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { - DIDescriptor Element = Elements.getElement(i); - if (Element.getTag() == dwarf::DW_TAG_subrange_type) - constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy); - } -} - -/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType. -void CompileUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) { - DIArray Elements = CTy.getTypeArray(); - - // Add enumerators to enumeration type. - for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { - DIEnumerator Enum(Elements.getElement(i)); - if (Enum.isEnumerator()) { - DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer); - StringRef Name = Enum.getName(); - addString(Enumerator, dwarf::DW_AT_name, Name); - int64_t Value = Enum.getEnumValue(); - addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value); - } - } - DIType DTy = resolve(CTy.getTypeDerivedFrom()); - if (DTy) { - addType(&Buffer, DTy); - addFlag(&Buffer, dwarf::DW_AT_enum_class); - } -} - -/// constructContainingTypeDIEs - Construct DIEs for types that contain -/// vtables. -void CompileUnit::constructContainingTypeDIEs() { - for (DenseMap::iterator CI = ContainingTypeMap.begin(), - CE = ContainingTypeMap.end(); - CI != CE; ++CI) { - DIE *SPDie = CI->first; - DIDescriptor D(CI->second); - if (!D) - continue; - DIE *NDie = getDIE(D); - if (!NDie) - continue; - addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie); - } -} - -/// constructVariableDIE - Construct a DIE for the given DbgVariable. -DIE *CompileUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) { - StringRef Name = DV.getName(); - - // Define variable debug information entry. - DIE *VariableDie = new DIE(DV.getTag()); - DbgVariable *AbsVar = DV.getAbstractVariable(); - DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL; - if (AbsDIE) - addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE); - else { - if (!Name.empty()) - addString(VariableDie, dwarf::DW_AT_name, Name); - addSourceLine(VariableDie, DV.getVariable()); - addType(VariableDie, DV.getType()); - } - - if (DV.isArtificial()) - addFlag(VariableDie, dwarf::DW_AT_artificial); - - if (isScopeAbstract) { - DV.setDIE(VariableDie); - return VariableDie; - } - - // Add variable address. - - unsigned Offset = DV.getDotDebugLocOffset(); - if (Offset != ~0U) { - addLabel(VariableDie, dwarf::DW_AT_location, - DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset - : dwarf::DW_FORM_data4, - Asm->GetTempSymbol("debug_loc", Offset)); - DV.setDIE(VariableDie); - return VariableDie; - } - - // Check if variable is described by a DBG_VALUE instruction. - if (const MachineInstr *DVInsn = DV.getMInsn()) { - assert(DVInsn->getNumOperands() == 3); - if (DVInsn->getOperand(0).isReg()) { - const MachineOperand RegOp = DVInsn->getOperand(0); - // If the second operand is an immediate, this is an indirect value. - if (DVInsn->getOperand(1).isImm()) { - MachineLocation Location(RegOp.getReg(), - DVInsn->getOperand(1).getImm()); - addVariableAddress(DV, VariableDie, Location); - } else if (RegOp.getReg()) - addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg())); - } else if (DVInsn->getOperand(0).isImm()) - addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType()); - else if (DVInsn->getOperand(0).isFPImm()) - addConstantFPValue(VariableDie, DVInsn->getOperand(0)); - else if (DVInsn->getOperand(0).isCImm()) - addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(), - isUnsignedDIType(DD, DV.getType())); - - DV.setDIE(VariableDie); - return VariableDie; - } else { - // .. else use frame index. - int FI = DV.getFrameIndex(); - if (FI != ~0) { - unsigned FrameReg = 0; - const TargetFrameLowering *TFI = Asm->TM.getFrameLowering(); - int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); - MachineLocation Location(FrameReg, Offset); - addVariableAddress(DV, VariableDie, Location); - } - } - - DV.setDIE(VariableDie); - return VariableDie; -} - -/// constructMemberDIE - Construct member DIE from DIDerivedType. -void CompileUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) { - DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer); - StringRef Name = DT.getName(); - if (!Name.empty()) - addString(MemberDie, dwarf::DW_AT_name, Name); - - addType(MemberDie, resolve(DT.getTypeDerivedFrom())); - - addSourceLine(MemberDie, DT); - - DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock(); - addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); - - if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) { - - // For C++, virtual base classes are not at fixed offset. Use following - // expression to extract appropriate offset from vtable. - // BaseAddr = ObAddr + *((*ObAddr) - Offset) - - DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock(); - addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); - addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); - addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); - addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits()); - addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); - addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); - addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); - - addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie); - } else { - uint64_t Size = DT.getSizeInBits(); - uint64_t FieldSize = getBaseTypeSize(DD, DT); - uint64_t OffsetInBytes; - - if (Size != FieldSize) { - // Handle bitfield. - addUInt(MemberDie, dwarf::DW_AT_byte_size, None, - getBaseTypeSize(DD, DT) >> 3); - addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits()); - - uint64_t Offset = DT.getOffsetInBits(); - uint64_t AlignMask = ~(DT.getAlignInBits() - 1); - uint64_t HiMark = (Offset + FieldSize) & AlignMask; - uint64_t FieldOffset = (HiMark - FieldSize); - Offset -= FieldOffset; - - // Maybe we need to work from the other end. - if (Asm->getDataLayout().isLittleEndian()) - Offset = FieldSize - (Offset + Size); - addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset); - - // Here WD_AT_data_member_location points to the anonymous - // field that includes this bit field. - OffsetInBytes = FieldOffset >> 3; - } else - // This is not a bitfield. - OffsetInBytes = DT.getOffsetInBits() >> 3; - addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes); - } - - if (DT.isProtected()) - addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, - dwarf::DW_ACCESS_protected); - else if (DT.isPrivate()) - addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, - dwarf::DW_ACCESS_private); - // Otherwise C++ member and base classes are considered public. - else - addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, - dwarf::DW_ACCESS_public); - if (DT.isVirtual()) - addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, - dwarf::DW_VIRTUALITY_virtual); - - // Objective-C properties. - if (MDNode *PNode = DT.getObjCProperty()) - if (DIEEntry *PropertyDie = getDIEEntry(PNode)) - MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4, - PropertyDie); - - if (DT.isArtificial()) - addFlag(MemberDie, dwarf::DW_AT_artificial); -} - -/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member. -DIE *CompileUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) { - if (!DT.Verify()) - return NULL; - - // Construct the context before querying for the existence of the DIE in case - // such construction creates the DIE. - DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext())); - assert(dwarf::isType(ContextDIE->getTag()) && - "Static member should belong to a type."); - - DIE *StaticMemberDIE = getDIE(DT); - if (StaticMemberDIE) - return StaticMemberDIE; - - StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT); - - DIType Ty = resolve(DT.getTypeDerivedFrom()); - - addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName()); - addType(StaticMemberDIE, Ty); - addSourceLine(StaticMemberDIE, DT); - addFlag(StaticMemberDIE, dwarf::DW_AT_external); - addFlag(StaticMemberDIE, dwarf::DW_AT_declaration); - - // FIXME: We could omit private if the parent is a class_type, and - // public if the parent is something else. - if (DT.isProtected()) - addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, - dwarf::DW_ACCESS_protected); - else if (DT.isPrivate()) - addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, - dwarf::DW_ACCESS_private); - else - addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, - dwarf::DW_ACCESS_public); - - if (const ConstantInt *CI = dyn_cast_or_null(DT.getConstant())) - addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty)); - if (const ConstantFP *CFP = dyn_cast_or_null(DT.getConstant())) - addConstantFPValue(StaticMemberDIE, CFP); - - return StaticMemberDIE; -} - -void CompileUnit::emitHeader(const MCSection *ASection, - const MCSymbol *ASectionSym) { - Asm->OutStreamer.AddComment("DWARF version number"); - Asm->EmitInt16(DD->getDwarfVersion()); - Asm->OutStreamer.AddComment("Offset Into Abbrev. Section"); - Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()), - ASectionSym); - Asm->OutStreamer.AddComment("Address Size (in bytes)"); - Asm->EmitInt8(Asm->getDataLayout().getPointerSize()); -} diff --git a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h b/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h deleted file mode 100644 index d782c88..0000000 --- a/lib/CodeGen/AsmPrinter/DwarfCompileUnit.h +++ /dev/null @@ -1,409 +0,0 @@ -//===-- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit ---*- C++ -*--===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file contains support for writing dwarf compile unit. -// -//===----------------------------------------------------------------------===// - -#ifndef CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H -#define CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H - -#include "DIE.h" -#include "DwarfDebug.h" -#include "llvm/ADT/DenseMap.h" -#include "llvm/ADT/Optional.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/ADT/StringMap.h" -#include "llvm/DebugInfo.h" -#include "llvm/MC/MCExpr.h" - -namespace llvm { - -class MachineLocation; -class MachineOperand; -class ConstantInt; -class ConstantFP; -class DbgVariable; - -//===----------------------------------------------------------------------===// -/// CompileUnit - This dwarf writer support class manages information associated -/// with a source file. -class CompileUnit { - /// UniqueID - a numeric ID unique among all CUs in the module - /// - unsigned UniqueID; - - /// Node - MDNode for the compile unit. - DICompileUnit Node; - - /// CUDie - Compile unit debug information entry. - /// - const OwningPtr CUDie; - - /// Asm - Target of Dwarf emission. - AsmPrinter *Asm; - - // Holders for some common dwarf information. - DwarfDebug *DD; - DwarfUnits *DU; - - /// IndexTyDie - An anonymous type for index type. Owned by CUDie. - DIE *IndexTyDie; - - /// MDNodeToDieMap - Tracks the mapping of unit level debug information - /// variables to debug information entries. - DenseMap MDNodeToDieMap; - - /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information - /// descriptors to debug information entries using a DIEEntry proxy. - DenseMap MDNodeToDIEEntryMap; - - /// GlobalNames - A map of globally visible named entities for this unit. - /// - StringMap GlobalNames; - - /// GlobalTypes - A map of globally visible types for this unit. - /// - StringMap GlobalTypes; - - /// AccelNames - A map of names for the name accelerator table. - /// - StringMap > AccelNames; - StringMap > AccelObjC; - StringMap > AccelNamespace; - StringMap > > AccelTypes; - - /// DIEBlocks - A list of all the DIEBlocks in use. - std::vector DIEBlocks; - - /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that - /// need DW_AT_containing_type attribute. This attribute points to a DIE that - /// corresponds to the MDNode mapped with the subprogram DIE. - DenseMap ContainingTypeMap; - - // DIEValueAllocator - All DIEValues are allocated through this allocator. - BumpPtrAllocator DIEValueAllocator; - - // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently. - DIEInteger *DIEIntegerOne; - -public: - CompileUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A, - DwarfDebug *DW, DwarfUnits *DWU); - ~CompileUnit(); - - // Accessors. - unsigned getUniqueID() const { return UniqueID; } - uint16_t getLanguage() const { return Node.getLanguage(); } - DICompileUnit getNode() const { return Node; } - DIE *getCUDie() const { return CUDie.get(); } - const StringMap &getGlobalNames() const { return GlobalNames; } - const StringMap &getGlobalTypes() const { return GlobalTypes; } - - const StringMap > &getAccelNames() const { - return AccelNames; - } - const StringMap > &getAccelObjC() const { - return AccelObjC; - } - const StringMap > &getAccelNamespace() const { - return AccelNamespace; - } - const StringMap > > & - getAccelTypes() const { - return AccelTypes; - } - - unsigned getDebugInfoOffset() const { return DebugInfoOffset; } - void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; } - - /// hasContent - Return true if this compile unit has something to write out. - /// - bool hasContent() const { return !CUDie->getChildren().empty(); } - - /// getParentContextString - Get a string containing the language specific - /// context for a global name. - std::string getParentContextString(DIScope Context) const; - - /// addGlobalName - Add a new global entity to the compile unit. - /// - void addGlobalName(StringRef Name, DIE *Die, DIScope Context); - - /// addGlobalType - Add a new global type to the compile unit. - /// - void addGlobalType(DIType Ty); - - /// addPubTypes - Add a set of types from the subprogram to the global types. - void addPubTypes(DISubprogram SP); - - /// addAccelName - Add a new name to the name accelerator table. - void addAccelName(StringRef Name, DIE *Die); - - /// addAccelObjC - Add a new name to the ObjC accelerator table. - void addAccelObjC(StringRef Name, DIE *Die); - - /// addAccelNamespace - Add a new name to the namespace accelerator table. - void addAccelNamespace(StringRef Name, DIE *Die); - - /// addAccelType - Add a new type to the type accelerator table. - void addAccelType(StringRef Name, std::pair Die); - - /// getDIE - Returns the debug information entry map slot for the - /// specified debug variable. We delegate the request to DwarfDebug - /// when the MDNode can be part of the type system, since DIEs for - /// the type system can be shared across CUs and the mappings are - /// kept in DwarfDebug. - DIE *getDIE(DIDescriptor D) const; - - DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); } - - /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug - /// when the MDNode can be part of the type system, since DIEs for - /// the type system can be shared across CUs and the mappings are - /// kept in DwarfDebug. - void insertDIE(DIDescriptor Desc, DIE *D); - - /// addDie - Adds or interns the DIE to the compile unit. - /// - void addDie(DIE *Buffer) { CUDie->addChild(Buffer); } - - /// addFlag - Add a flag that is true to the DIE. - void addFlag(DIE *Die, dwarf::Attribute Attribute); - - /// addUInt - Add an unsigned integer attribute data and value. - /// - void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional Form, - uint64_t Integer); - - void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer); - - /// addSInt - Add an signed integer attribute data and value. - /// - void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional Form, - int64_t Integer); - - void addSInt(DIEBlock *Die, Optional Form, int64_t Integer); - - /// addString - Add a string attribute data and value. - /// - void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str); - - /// addLocalString - Add a string attribute data and value. - /// - void addLocalString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str); - - /// addExpr - Add a Dwarf expression attribute data and value. - /// - void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr); - - /// addLabel - Add a Dwarf label attribute data and value. - /// - void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form, - const MCSymbol *Label); - - void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label); - - /// addLabelAddress - Add a dwarf label attribute data and value using - /// either DW_FORM_addr or DW_FORM_GNU_addr_index. - /// - void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label); - - /// addOpAddress - Add a dwarf op address data and value using the - /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index. - /// - void addOpAddress(DIEBlock *Die, const MCSymbol *Label); - - /// addDelta - Add a label delta attribute data and value. - /// - void addDelta(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form, const MCSymbol *Hi, - const MCSymbol *Lo); - - /// addDIEEntry - Add a DIE attribute data and value. - /// - void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry); - - /// addDIEEntry - Add a DIE attribute data and value. - /// - void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry); - - /// addBlock - Add block data. - /// - void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block); - - /// addSourceLine - Add location information to specified debug information - /// entry. - void addSourceLine(DIE *Die, DIVariable V); - void addSourceLine(DIE *Die, DIGlobalVariable G); - void addSourceLine(DIE *Die, DISubprogram SP); - void addSourceLine(DIE *Die, DIType Ty); - void addSourceLine(DIE *Die, DINameSpace NS); - void addSourceLine(DIE *Die, DIObjCProperty Ty); - - /// addAddress - Add an address attribute to a die based on the location - /// provided. - void addAddress(DIE *Die, dwarf::Attribute Attribute, const MachineLocation &Location, - bool Indirect = false); - - /// addConstantValue - Add constant value entry in variable DIE. - void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty); - void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned); - void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned); - - /// addConstantFPValue - Add constant value entry in variable DIE. - void addConstantFPValue(DIE *Die, const MachineOperand &MO); - void addConstantFPValue(DIE *Die, const ConstantFP *CFP); - - /// addTemplateParams - Add template parameters in buffer. - void addTemplateParams(DIE &Buffer, DIArray TParams); - - /// addRegisterOp - Add register operand. - void addRegisterOp(DIEBlock *TheDie, unsigned Reg); - - /// addRegisterOffset - Add register offset. - void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset); - - /// addComplexAddress - Start with the address based on the location provided, - /// and generate the DWARF information necessary to find the actual variable - /// (navigating the extra location information encoded in the type) based on - /// the starting location. Add the DWARF information to the die. - /// - void addComplexAddress(const DbgVariable &DV, DIE *Die, dwarf::Attribute Attribute, - const MachineLocation &Location); - - // FIXME: Should be reformulated in terms of addComplexAddress. - /// addBlockByrefAddress - Start with the address based on the location - /// provided, and generate the DWARF information necessary to find the - /// actual Block variable (navigating the Block struct) based on the - /// starting location. Add the DWARF information to the die. Obsolete, - /// please use addComplexAddress instead. - /// - void addBlockByrefAddress(const DbgVariable &DV, DIE *Die, dwarf::Attribute Attribute, - const MachineLocation &Location); - - /// addVariableAddress - Add DW_AT_location attribute for a - /// DbgVariable based on provided MachineLocation. - void addVariableAddress(const DbgVariable &DV, DIE *Die, - MachineLocation Location); - - /// addType - Add a new type attribute to the specified entity. This takes - /// and attribute parameter because DW_AT_friend attributes are also - /// type references. - void addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute = dwarf::DW_AT_type); - - /// getOrCreateNameSpace - Create a DIE for DINameSpace. - DIE *getOrCreateNameSpace(DINameSpace NS); - - /// getOrCreateSubprogramDIE - Create new DIE using SP. - DIE *getOrCreateSubprogramDIE(DISubprogram SP); - - /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the - /// given DIType. - DIE *getOrCreateTypeDIE(const MDNode *N); - - /// getOrCreateContextDIE - Get context owner's DIE. - DIE *getOrCreateContextDIE(DIScope Context); - - /// createGlobalVariableDIE - create global variable DIE. - void createGlobalVariableDIE(DIGlobalVariable GV); - - /// constructContainingTypeDIEs - Construct DIEs for types that contain - /// vtables. - void constructContainingTypeDIEs(); - - /// constructVariableDIE - Construct a DIE for the given DbgVariable. - DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract); - - /// Create a DIE with the given Tag, add the DIE to its parent, and - /// call insertDIE if MD is not null. - DIE *createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N = DIDescriptor()); - - /// Compute the size of a header for this unit, not including the initial - /// length field. - unsigned getHeaderSize() const { - return sizeof(int16_t) + // DWARF version number - sizeof(int32_t) + // Offset Into Abbrev. Section - sizeof(int8_t); // Pointer Size (in bytes) - } - - /// Emit the header for this unit, not including the initial length field. - void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym); - -private: - /// constructTypeDIE - Construct basic type die from DIBasicType. - void constructTypeDIE(DIE &Buffer, DIBasicType BTy); - - /// constructTypeDIE - Construct derived type die from DIDerivedType. - void constructTypeDIE(DIE &Buffer, DIDerivedType DTy); - - /// constructTypeDIE - Construct type DIE from DICompositeType. - void constructTypeDIE(DIE &Buffer, DICompositeType CTy); - - /// constructSubrangeDIE - Construct subrange DIE from DISubrange. - void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy); - - /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. - void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy); - - /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator. - void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy); - - /// constructMemberDIE - Construct member DIE from DIDerivedType. - void constructMemberDIE(DIE &Buffer, DIDerivedType DT); - - /// constructTemplateTypeParameterDIE - Construct new DIE for the given - /// DITemplateTypeParameter. - void constructTemplateTypeParameterDIE(DIE &Buffer, - DITemplateTypeParameter TP); - - /// constructTemplateValueParameterDIE - Construct new DIE for the given - /// DITemplateValueParameter. - void constructTemplateValueParameterDIE(DIE &Buffer, - DITemplateValueParameter TVP); - - /// getOrCreateStaticMemberDIE - Create new static data member DIE. - DIE *getOrCreateStaticMemberDIE(DIDerivedType DT); - - /// Offset of the CUDie from beginning of debug info section. - unsigned DebugInfoOffset; - - /// getLowerBoundDefault - Return the default lower bound for an array. If the - /// DWARF version doesn't handle the language, return -1. - int64_t getDefaultLowerBound() const; - - /// getDIEEntry - Returns the debug information entry for the specified - /// debug variable. - DIEEntry *getDIEEntry(const MDNode *N) const { - return MDNodeToDIEEntryMap.lookup(N); - } - - /// insertDIEEntry - Insert debug information entry into the map. - void insertDIEEntry(const MDNode *N, DIEEntry *E) { - MDNodeToDIEEntryMap.insert(std::make_pair(N, E)); - } - - // getIndexTyDie - Get an anonymous type for index type. - DIE *getIndexTyDie() { return IndexTyDie; } - - // setIndexTyDie - Set D as anonymous type for index which can be reused - // later. - void setIndexTyDie(DIE *D) { IndexTyDie = D; } - - /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug - /// information entry. - DIEEntry *createDIEEntry(DIE *Entry); - - /// resolve - Look in the DwarfDebug map for the MDNode that - /// corresponds to the reference. - template T resolve(DIRef Ref) const { - return DD->resolve(Ref); - } -}; - -} // end llvm namespace -#endif diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp index 24e2c05..5eac066 100644 --- a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -16,7 +16,7 @@ #include "DIE.h" #include "DIEHash.h" #include "DwarfAccelTable.h" -#include "DwarfCompileUnit.h" +#include "DwarfUnit.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/Statistic.h" #include "llvm/ADT/StringExtras.h" diff --git a/lib/CodeGen/AsmPrinter/DwarfUnit.cpp b/lib/CodeGen/AsmPrinter/DwarfUnit.cpp new file mode 100644 index 0000000..5366bb6 --- /dev/null +++ b/lib/CodeGen/AsmPrinter/DwarfUnit.cpp @@ -0,0 +1,1956 @@ +//===-- llvm/CodeGen/DwarfCompileUnit.cpp - Dwarf Compile Unit ------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains support for constructing a dwarf compile unit. +// +//===----------------------------------------------------------------------===// + +#define DEBUG_TYPE "dwarfdebug" + +#include "DwarfUnit.h" +#include "DwarfAccelTable.h" +#include "DwarfDebug.h" +#include "llvm/ADT/APFloat.h" +#include "llvm/DIBuilder.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/GlobalVariable.h" +#include "llvm/IR/Instructions.h" +#include "llvm/MC/MCSection.h" +#include "llvm/MC/MCStreamer.h" +#include "llvm/Target/Mangler.h" +#include "llvm/Target/TargetFrameLowering.h" +#include "llvm/Target/TargetMachine.h" +#include "llvm/Target/TargetLoweringObjectFile.h" +#include "llvm/Target/TargetRegisterInfo.h" + +using namespace llvm; + +/// CompileUnit - Compile unit constructor. +CompileUnit::CompileUnit(unsigned UID, DIE *D, DICompileUnit Node, + AsmPrinter *A, DwarfDebug *DW, DwarfUnits *DWU) + : UniqueID(UID), Node(Node), CUDie(D), Asm(A), DD(DW), DU(DWU), + IndexTyDie(0), DebugInfoOffset(0) { + DIEIntegerOne = new (DIEValueAllocator) DIEInteger(1); + insertDIE(Node, D); +} + +/// ~CompileUnit - Destructor for compile unit. +CompileUnit::~CompileUnit() { + for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j) + DIEBlocks[j]->~DIEBlock(); +} + +/// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug +/// information entry. +DIEEntry *CompileUnit::createDIEEntry(DIE *Entry) { + DIEEntry *Value = new (DIEValueAllocator) DIEEntry(Entry); + return Value; +} + +/// getDefaultLowerBound - Return the default lower bound for an array. If the +/// DWARF version doesn't handle the language, return -1. +int64_t CompileUnit::getDefaultLowerBound() const { + switch (getLanguage()) { + default: + break; + + case dwarf::DW_LANG_C89: + case dwarf::DW_LANG_C99: + case dwarf::DW_LANG_C: + case dwarf::DW_LANG_C_plus_plus: + case dwarf::DW_LANG_ObjC: + case dwarf::DW_LANG_ObjC_plus_plus: + return 0; + + case dwarf::DW_LANG_Fortran77: + case dwarf::DW_LANG_Fortran90: + case dwarf::DW_LANG_Fortran95: + return 1; + + // The languages below have valid values only if the DWARF version >= 4. + case dwarf::DW_LANG_Java: + case dwarf::DW_LANG_Python: + case dwarf::DW_LANG_UPC: + case dwarf::DW_LANG_D: + if (dwarf::DWARF_VERSION >= 4) + return 0; + break; + + case dwarf::DW_LANG_Ada83: + case dwarf::DW_LANG_Ada95: + case dwarf::DW_LANG_Cobol74: + case dwarf::DW_LANG_Cobol85: + case dwarf::DW_LANG_Modula2: + case dwarf::DW_LANG_Pascal83: + case dwarf::DW_LANG_PLI: + if (dwarf::DWARF_VERSION >= 4) + return 1; + break; + } + + return -1; +} + +/// Check whether the DIE for this MDNode can be shared across CUs. +static bool isShareableAcrossCUs(DIDescriptor D) { + // When the MDNode can be part of the type system, the DIE can be + // shared across CUs. + return D.isType() || + (D.isSubprogram() && !DISubprogram(D).isDefinition()); +} + +/// getDIE - Returns the debug information entry map slot for the +/// specified debug variable. We delegate the request to DwarfDebug +/// when the DIE for this MDNode can be shared across CUs. The mappings +/// will be kept in DwarfDebug for shareable DIEs. +DIE *CompileUnit::getDIE(DIDescriptor D) const { + if (isShareableAcrossCUs(D)) + return DD->getDIE(D); + return MDNodeToDieMap.lookup(D); +} + +/// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug +/// when the DIE for this MDNode can be shared across CUs. The mappings +/// will be kept in DwarfDebug for shareable DIEs. +void CompileUnit::insertDIE(DIDescriptor Desc, DIE *D) { + if (isShareableAcrossCUs(Desc)) { + DD->insertDIE(Desc, D); + return; + } + MDNodeToDieMap.insert(std::make_pair(Desc, D)); +} + +/// addFlag - Add a flag that is true. +void CompileUnit::addFlag(DIE *Die, dwarf::Attribute Attribute) { + if (DD->getDwarfVersion() >= 4) + Die->addValue(Attribute, dwarf::DW_FORM_flag_present, DIEIntegerOne); + else + Die->addValue(Attribute, dwarf::DW_FORM_flag, DIEIntegerOne); +} + +/// addUInt - Add an unsigned integer attribute data and value. +/// +void CompileUnit::addUInt(DIE *Die, dwarf::Attribute Attribute, + Optional Form, uint64_t Integer) { + if (!Form) + Form = DIEInteger::BestForm(false, Integer); + DIEValue *Value = Integer == 1 ? DIEIntegerOne : new (DIEValueAllocator) + DIEInteger(Integer); + Die->addValue(Attribute, *Form, Value); +} + +void CompileUnit::addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer) { + addUInt(Block, (dwarf::Attribute)0, Form, Integer); +} + +/// addSInt - Add an signed integer attribute data and value. +/// +void CompileUnit::addSInt(DIE *Die, dwarf::Attribute Attribute, + Optional Form, int64_t Integer) { + if (!Form) + Form = DIEInteger::BestForm(true, Integer); + DIEValue *Value = new (DIEValueAllocator) DIEInteger(Integer); + Die->addValue(Attribute, *Form, Value); +} + +void CompileUnit::addSInt(DIEBlock *Die, Optional Form, + int64_t Integer) { + addSInt(Die, (dwarf::Attribute)0, Form, Integer); +} + +/// addString - Add a string attribute data and value. We always emit a +/// reference to the string pool instead of immediate strings so that DIEs have +/// more predictable sizes. In the case of split dwarf we emit an index +/// into another table which gets us the static offset into the string +/// table. +void CompileUnit::addString(DIE *Die, dwarf::Attribute Attribute, + StringRef String) { + DIEValue *Value; + dwarf::Form Form; + if (!DD->useSplitDwarf()) { + MCSymbol *Symb = DU->getStringPoolEntry(String); + if (Asm->needsRelocationsForDwarfStringPool()) + Value = new (DIEValueAllocator) DIELabel(Symb); + else { + MCSymbol *StringPool = DU->getStringPoolSym(); + Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); + } + Form = dwarf::DW_FORM_strp; + } else { + unsigned idx = DU->getStringPoolIndex(String); + Value = new (DIEValueAllocator) DIEInteger(idx); + Form = dwarf::DW_FORM_GNU_str_index; + } + DIEValue *Str = new (DIEValueAllocator) DIEString(Value, String); + Die->addValue(Attribute, Form, Str); +} + +/// addLocalString - Add a string attribute data and value. This is guaranteed +/// to be in the local string pool instead of indirected. +void CompileUnit::addLocalString(DIE *Die, dwarf::Attribute Attribute, + StringRef String) { + MCSymbol *Symb = DU->getStringPoolEntry(String); + DIEValue *Value; + if (Asm->needsRelocationsForDwarfStringPool()) + Value = new (DIEValueAllocator) DIELabel(Symb); + else { + MCSymbol *StringPool = DU->getStringPoolSym(); + Value = new (DIEValueAllocator) DIEDelta(Symb, StringPool); + } + Die->addValue(Attribute, dwarf::DW_FORM_strp, Value); +} + +/// addExpr - Add a Dwarf expression attribute data and value. +/// +void CompileUnit::addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr) { + DIEValue *Value = new (DIEValueAllocator) DIEExpr(Expr); + Die->addValue((dwarf::Attribute)0, Form, Value); +} + +/// addLabel - Add a Dwarf label attribute data and value. +/// +void CompileUnit::addLabel(DIE *Die, dwarf::Attribute Attribute, + dwarf::Form Form, const MCSymbol *Label) { + DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); + Die->addValue(Attribute, Form, Value); +} + +void CompileUnit::addLabel(DIEBlock *Die, dwarf::Form Form, + const MCSymbol *Label) { + addLabel(Die, (dwarf::Attribute)0, Form, Label); +} + +/// addLabelAddress - Add a dwarf label attribute data and value using +/// DW_FORM_addr or DW_FORM_GNU_addr_index. +/// +void CompileUnit::addLabelAddress(DIE *Die, dwarf::Attribute Attribute, + MCSymbol *Label) { + if (Label) + DD->addArangeLabel(SymbolCU(this, Label)); + + if (!DD->useSplitDwarf()) { + if (Label != NULL) { + DIEValue *Value = new (DIEValueAllocator) DIELabel(Label); + Die->addValue(Attribute, dwarf::DW_FORM_addr, Value); + } else { + DIEValue *Value = new (DIEValueAllocator) DIEInteger(0); + Die->addValue(Attribute, dwarf::DW_FORM_addr, Value); + } + } else { + unsigned idx = DU->getAddrPoolIndex(Label); + DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx); + Die->addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value); + } +} + +/// addOpAddress - Add a dwarf op address data and value using the +/// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index. +/// +void CompileUnit::addOpAddress(DIEBlock *Die, const MCSymbol *Sym) { + DD->addArangeLabel(SymbolCU(this, Sym)); + if (!DD->useSplitDwarf()) { + addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr); + addLabel(Die, dwarf::DW_FORM_udata, Sym); + } else { + addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index); + addUInt(Die, dwarf::DW_FORM_GNU_addr_index, DU->getAddrPoolIndex(Sym)); + } +} + +/// addDelta - Add a label delta attribute data and value. +/// +void CompileUnit::addDelta(DIE *Die, dwarf::Attribute Attribute, + dwarf::Form Form, const MCSymbol *Hi, + const MCSymbol *Lo) { + DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo); + Die->addValue(Attribute, Form, Value); +} + +/// addDIEEntry - Add a DIE attribute data and value. +/// +void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, + DIE *Entry) { + addDIEEntry(Die, Attribute, createDIEEntry(Entry)); +} + +void CompileUnit::addDIEEntry(DIE *Die, dwarf::Attribute Attribute, + DIEEntry *Entry) { + const DIE *DieCU = Die->getCompileUnitOrNull(); + const DIE *EntryCU = Entry->getEntry()->getCompileUnitOrNull(); + if (!DieCU) + // We assume that Die belongs to this CU, if it is not linked to any CU yet. + DieCU = getCUDie(); + if (!EntryCU) + EntryCU = getCUDie(); + Die->addValue(Attribute, EntryCU == DieCU ? dwarf::DW_FORM_ref4 + : dwarf::DW_FORM_ref_addr, + Entry); +} + +/// Create a DIE with the given Tag, add the DIE to its parent, and +/// call insertDIE if MD is not null. +DIE *CompileUnit::createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N) { + DIE *Die = new DIE(Tag); + Parent.addChild(Die); + if (N) + insertDIE(N, Die); + return Die; +} + +/// addBlock - Add block data. +/// +void CompileUnit::addBlock(DIE *Die, dwarf::Attribute Attribute, + DIEBlock *Block) { + Block->ComputeSize(Asm); + DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on. + Die->addValue(Attribute, Block->BestForm(), Block); +} + +/// addSourceLine - Add location information to specified debug information +/// entry. +void CompileUnit::addSourceLine(DIE *Die, DIVariable V) { + // Verify variable. + if (!V.isVariable()) + return; + + unsigned Line = V.getLineNumber(); + if (Line == 0) + return; + unsigned FileID = + DD->getOrCreateSourceID(V.getContext().getFilename(), + V.getContext().getDirectory(), getUniqueID()); + assert(FileID && "Invalid file id"); + addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); + addUInt(Die, dwarf::DW_AT_decl_line, None, Line); +} + +/// addSourceLine - Add location information to specified debug information +/// entry. +void CompileUnit::addSourceLine(DIE *Die, DIGlobalVariable G) { + // Verify global variable. + if (!G.isGlobalVariable()) + return; + + unsigned Line = G.getLineNumber(); + if (Line == 0) + return; + unsigned FileID = + DD->getOrCreateSourceID(G.getFilename(), G.getDirectory(), getUniqueID()); + assert(FileID && "Invalid file id"); + addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); + addUInt(Die, dwarf::DW_AT_decl_line, None, Line); +} + +/// addSourceLine - Add location information to specified debug information +/// entry. +void CompileUnit::addSourceLine(DIE *Die, DISubprogram SP) { + // Verify subprogram. + if (!SP.isSubprogram()) + return; + + // If the line number is 0, don't add it. + unsigned Line = SP.getLineNumber(); + if (Line == 0) + return; + + unsigned FileID = DD->getOrCreateSourceID(SP.getFilename(), SP.getDirectory(), + getUniqueID()); + assert(FileID && "Invalid file id"); + addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); + addUInt(Die, dwarf::DW_AT_decl_line, None, Line); +} + +/// addSourceLine - Add location information to specified debug information +/// entry. +void CompileUnit::addSourceLine(DIE *Die, DIType Ty) { + // Verify type. + if (!Ty.isType()) + return; + + unsigned Line = Ty.getLineNumber(); + if (Line == 0) + return; + unsigned FileID = DD->getOrCreateSourceID(Ty.getFilename(), Ty.getDirectory(), + getUniqueID()); + assert(FileID && "Invalid file id"); + addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); + addUInt(Die, dwarf::DW_AT_decl_line, None, Line); +} + +/// addSourceLine - Add location information to specified debug information +/// entry. +void CompileUnit::addSourceLine(DIE *Die, DIObjCProperty Ty) { + // Verify type. + if (!Ty.isObjCProperty()) + return; + + unsigned Line = Ty.getLineNumber(); + if (Line == 0) + return; + DIFile File = Ty.getFile(); + unsigned FileID = DD->getOrCreateSourceID(File.getFilename(), + File.getDirectory(), getUniqueID()); + assert(FileID && "Invalid file id"); + addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); + addUInt(Die, dwarf::DW_AT_decl_line, None, Line); +} + +/// addSourceLine - Add location information to specified debug information +/// entry. +void CompileUnit::addSourceLine(DIE *Die, DINameSpace NS) { + // Verify namespace. + if (!NS.Verify()) + return; + + unsigned Line = NS.getLineNumber(); + if (Line == 0) + return; + StringRef FN = NS.getFilename(); + + unsigned FileID = + DD->getOrCreateSourceID(FN, NS.getDirectory(), getUniqueID()); + assert(FileID && "Invalid file id"); + addUInt(Die, dwarf::DW_AT_decl_file, None, FileID); + addUInt(Die, dwarf::DW_AT_decl_line, None, Line); +} + +/// addVariableAddress - Add DW_AT_location attribute for a +/// DbgVariable based on provided MachineLocation. +void CompileUnit::addVariableAddress(const DbgVariable &DV, DIE *Die, + MachineLocation Location) { + if (DV.variableHasComplexAddress()) + addComplexAddress(DV, Die, dwarf::DW_AT_location, Location); + else if (DV.isBlockByrefVariable()) + addBlockByrefAddress(DV, Die, dwarf::DW_AT_location, Location); + else + addAddress(Die, dwarf::DW_AT_location, Location, + DV.getVariable().isIndirect()); +} + +/// addRegisterOp - Add register operand. +void CompileUnit::addRegisterOp(DIEBlock *TheDie, unsigned Reg) { + const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); + unsigned DWReg = RI->getDwarfRegNum(Reg, false); + if (DWReg < 32) + addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_reg0 + DWReg); + else { + addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_regx); + addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); + } +} + +/// addRegisterOffset - Add register offset. +void CompileUnit::addRegisterOffset(DIEBlock *TheDie, unsigned Reg, + int64_t Offset) { + const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo(); + unsigned DWReg = RI->getDwarfRegNum(Reg, false); + const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo(); + if (Reg == TRI->getFrameRegister(*Asm->MF)) + // If variable offset is based in frame register then use fbreg. + addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_fbreg); + else if (DWReg < 32) + addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_breg0 + DWReg); + else { + addUInt(TheDie, dwarf::DW_FORM_data1, dwarf::DW_OP_bregx); + addUInt(TheDie, dwarf::DW_FORM_udata, DWReg); + } + addSInt(TheDie, dwarf::DW_FORM_sdata, Offset); +} + +/// addAddress - Add an address attribute to a die based on the location +/// provided. +void CompileUnit::addAddress(DIE *Die, dwarf::Attribute Attribute, + const MachineLocation &Location, bool Indirect) { + DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); + + if (Location.isReg() && !Indirect) + addRegisterOp(Block, Location.getReg()); + else { + addRegisterOffset(Block, Location.getReg(), Location.getOffset()); + if (Indirect && !Location.isReg()) { + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); + } + } + + // Now attach the location information to the DIE. + addBlock(Die, Attribute, Block); +} + +/// addComplexAddress - Start with the address based on the location provided, +/// and generate the DWARF information necessary to find the actual variable +/// given the extra address information encoded in the DIVariable, starting from +/// the starting location. Add the DWARF information to the die. +/// +void CompileUnit::addComplexAddress(const DbgVariable &DV, DIE *Die, + dwarf::Attribute Attribute, + const MachineLocation &Location) { + DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); + unsigned N = DV.getNumAddrElements(); + unsigned i = 0; + if (Location.isReg()) { + if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) { + // If first address element is OpPlus then emit + // DW_OP_breg + Offset instead of DW_OP_reg + Offset. + addRegisterOffset(Block, Location.getReg(), DV.getAddrElement(1)); + i = 2; + } else + addRegisterOp(Block, Location.getReg()); + } else + addRegisterOffset(Block, Location.getReg(), Location.getOffset()); + + for (; i < N; ++i) { + uint64_t Element = DV.getAddrElement(i); + if (Element == DIBuilder::OpPlus) { + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); + addUInt(Block, dwarf::DW_FORM_udata, DV.getAddrElement(++i)); + } else if (Element == DIBuilder::OpDeref) { + if (!Location.isReg()) + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); + } else + llvm_unreachable("unknown DIBuilder Opcode"); + } + + // Now attach the location information to the DIE. + addBlock(Die, Attribute, Block); +} + +/* Byref variables, in Blocks, are declared by the programmer as "SomeType + VarName;", but the compiler creates a __Block_byref_x_VarName struct, and + gives the variable VarName either the struct, or a pointer to the struct, as + its type. This is necessary for various behind-the-scenes things the + compiler needs to do with by-reference variables in Blocks. + + However, as far as the original *programmer* is concerned, the variable + should still have type 'SomeType', as originally declared. + + The function getBlockByrefType dives into the __Block_byref_x_VarName + struct to find the original type of the variable, which is then assigned to + the variable's Debug Information Entry as its real type. So far, so good. + However now the debugger will expect the variable VarName to have the type + SomeType. So we need the location attribute for the variable to be an + expression that explains to the debugger how to navigate through the + pointers and struct to find the actual variable of type SomeType. + + The following function does just that. We start by getting + the "normal" location for the variable. This will be the location + of either the struct __Block_byref_x_VarName or the pointer to the + struct __Block_byref_x_VarName. + + The struct will look something like: + + struct __Block_byref_x_VarName { + ... + struct __Block_byref_x_VarName *forwarding; + ... + SomeType VarName; + ... + }; + + If we are given the struct directly (as our starting point) we + need to tell the debugger to: + + 1). Add the offset of the forwarding field. + + 2). Follow that pointer to get the real __Block_byref_x_VarName + struct to use (the real one may have been copied onto the heap). + + 3). Add the offset for the field VarName, to find the actual variable. + + If we started with a pointer to the struct, then we need to + dereference that pointer first, before the other steps. + Translating this into DWARF ops, we will need to append the following + to the current location description for the variable: + + DW_OP_deref -- optional, if we start with a pointer + DW_OP_plus_uconst + DW_OP_deref + DW_OP_plus_uconst + + That is what this function does. */ + +/// addBlockByrefAddress - Start with the address based on the location +/// provided, and generate the DWARF information necessary to find the +/// actual Block variable (navigating the Block struct) based on the +/// starting location. Add the DWARF information to the die. For +/// more information, read large comment just above here. +/// +void CompileUnit::addBlockByrefAddress(const DbgVariable &DV, DIE *Die, + dwarf::Attribute Attribute, + const MachineLocation &Location) { + DIType Ty = DV.getType(); + DIType TmpTy = Ty; + uint16_t Tag = Ty.getTag(); + bool isPointer = false; + + StringRef varName = DV.getName(); + + if (Tag == dwarf::DW_TAG_pointer_type) { + DIDerivedType DTy(Ty); + TmpTy = resolve(DTy.getTypeDerivedFrom()); + isPointer = true; + } + + DICompositeType blockStruct(TmpTy); + + // Find the __forwarding field and the variable field in the __Block_byref + // struct. + DIArray Fields = blockStruct.getTypeArray(); + DIDerivedType varField; + DIDerivedType forwardingField; + + for (unsigned i = 0, N = Fields.getNumElements(); i < N; ++i) { + DIDerivedType DT(Fields.getElement(i)); + StringRef fieldName = DT.getName(); + if (fieldName == "__forwarding") + forwardingField = DT; + else if (fieldName == varName) + varField = DT; + } + + // Get the offsets for the forwarding field and the variable field. + unsigned forwardingFieldOffset = forwardingField.getOffsetInBits() >> 3; + unsigned varFieldOffset = varField.getOffsetInBits() >> 2; + + // Decode the original location, and use that as the start of the byref + // variable's location. + DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); + + if (Location.isReg()) + addRegisterOp(Block, Location.getReg()); + else + addRegisterOffset(Block, Location.getReg(), Location.getOffset()); + + // If we started with a pointer to the __Block_byref... struct, then + // the first thing we need to do is dereference the pointer (DW_OP_deref). + if (isPointer) + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); + + // Next add the offset for the '__forwarding' field: + // DW_OP_plus_uconst ForwardingFieldOffset. Note there's no point in + // adding the offset if it's 0. + if (forwardingFieldOffset > 0) { + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); + addUInt(Block, dwarf::DW_FORM_udata, forwardingFieldOffset); + } + + // Now dereference the __forwarding field to get to the real __Block_byref + // struct: DW_OP_deref. + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); + + // Now that we've got the real __Block_byref... struct, add the offset + // for the variable's field to get to the location of the actual variable: + // DW_OP_plus_uconst varFieldOffset. Again, don't add if it's 0. + if (varFieldOffset > 0) { + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); + addUInt(Block, dwarf::DW_FORM_udata, varFieldOffset); + } + + // Now attach the location information to the DIE. + addBlock(Die, Attribute, Block); +} + +/// isTypeSigned - Return true if the type is signed. +static bool isTypeSigned(DwarfDebug *DD, DIType Ty, int *SizeInBits) { + if (Ty.isDerivedType()) + return isTypeSigned(DD, DD->resolve(DIDerivedType(Ty).getTypeDerivedFrom()), + SizeInBits); + if (Ty.isBasicType()) + if (DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed || + DIBasicType(Ty).getEncoding() == dwarf::DW_ATE_signed_char) { + *SizeInBits = Ty.getSizeInBits(); + return true; + } + return false; +} + +/// Return true if type encoding is unsigned. +static bool isUnsignedDIType(DwarfDebug *DD, DIType Ty) { + DIDerivedType DTy(Ty); + if (DTy.isDerivedType()) + return isUnsignedDIType(DD, DD->resolve(DTy.getTypeDerivedFrom())); + + DIBasicType BTy(Ty); + if (BTy.isBasicType()) { + unsigned Encoding = BTy.getEncoding(); + if (Encoding == dwarf::DW_ATE_unsigned || + Encoding == dwarf::DW_ATE_unsigned_char || + Encoding == dwarf::DW_ATE_boolean) + return true; + } + return false; +} + +/// If this type is derived from a base type then return base type size. +static uint64_t getBaseTypeSize(DwarfDebug *DD, DIDerivedType Ty) { + unsigned Tag = Ty.getTag(); + + if (Tag != dwarf::DW_TAG_member && Tag != dwarf::DW_TAG_typedef && + Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type && + Tag != dwarf::DW_TAG_restrict_type) + return Ty.getSizeInBits(); + + DIType BaseType = DD->resolve(Ty.getTypeDerivedFrom()); + + // If this type is not derived from any type then take conservative approach. + if (!BaseType.isValid()) + return Ty.getSizeInBits(); + + // If this is a derived type, go ahead and get the base type, unless it's a + // reference then it's just the size of the field. Pointer types have no need + // of this since they're a different type of qualification on the type. + if (BaseType.getTag() == dwarf::DW_TAG_reference_type || + BaseType.getTag() == dwarf::DW_TAG_rvalue_reference_type) + return Ty.getSizeInBits(); + + if (BaseType.isDerivedType()) + return getBaseTypeSize(DD, DIDerivedType(BaseType)); + + return BaseType.getSizeInBits(); +} + +/// addConstantValue - Add constant value entry in variable DIE. +void CompileUnit::addConstantValue(DIE *Die, const MachineOperand &MO, + DIType Ty) { + // FIXME: This is a bit conservative/simple - it emits negative values at + // their maximum bit width which is a bit unfortunate (& doesn't prefer + // udata/sdata over dataN as suggested by the DWARF spec) + assert(MO.isImm() && "Invalid machine operand!"); + int SizeInBits = -1; + bool SignedConstant = isTypeSigned(DD, Ty, &SizeInBits); + dwarf::Form Form; + + // If we're a signed constant definitely use sdata. + if (SignedConstant) { + addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, MO.getImm()); + return; + } + + // Else use data for now unless it's larger than we can deal with. + switch (SizeInBits) { + case 8: + Form = dwarf::DW_FORM_data1; + break; + case 16: + Form = dwarf::DW_FORM_data2; + break; + case 32: + Form = dwarf::DW_FORM_data4; + break; + case 64: + Form = dwarf::DW_FORM_data8; + break; + default: + Form = dwarf::DW_FORM_udata; + addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm()); + return; + } + addUInt(Die, dwarf::DW_AT_const_value, Form, MO.getImm()); +} + +/// addConstantFPValue - Add constant value entry in variable DIE. +void CompileUnit::addConstantFPValue(DIE *Die, const MachineOperand &MO) { + assert(MO.isFPImm() && "Invalid machine operand!"); + DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); + APFloat FPImm = MO.getFPImm()->getValueAPF(); + + // Get the raw data form of the floating point. + const APInt FltVal = FPImm.bitcastToAPInt(); + const char *FltPtr = (const char *)FltVal.getRawData(); + + int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte. + bool LittleEndian = Asm->getDataLayout().isLittleEndian(); + int Incr = (LittleEndian ? 1 : -1); + int Start = (LittleEndian ? 0 : NumBytes - 1); + int Stop = (LittleEndian ? NumBytes : -1); + + // Output the constant to DWARF one byte at a time. + for (; Start != Stop; Start += Incr) + addUInt(Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]); + + addBlock(Die, dwarf::DW_AT_const_value, Block); +} + +/// addConstantFPValue - Add constant value entry in variable DIE. +void CompileUnit::addConstantFPValue(DIE *Die, const ConstantFP *CFP) { + // Pass this down to addConstantValue as an unsigned bag of bits. + addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true); +} + +/// addConstantValue - Add constant value entry in variable DIE. +void CompileUnit::addConstantValue(DIE *Die, const ConstantInt *CI, + bool Unsigned) { + addConstantValue(Die, CI->getValue(), Unsigned); +} + +// addConstantValue - Add constant value entry in variable DIE. +void CompileUnit::addConstantValue(DIE *Die, const APInt &Val, bool Unsigned) { + unsigned CIBitWidth = Val.getBitWidth(); + if (CIBitWidth <= 64) { + // If we're a signed constant definitely use sdata. + if (!Unsigned) { + addSInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, + Val.getSExtValue()); + return; + } + + // Else use data for now unless it's larger than we can deal with. + dwarf::Form Form; + switch (CIBitWidth) { + case 8: + Form = dwarf::DW_FORM_data1; + break; + case 16: + Form = dwarf::DW_FORM_data2; + break; + case 32: + Form = dwarf::DW_FORM_data4; + break; + case 64: + Form = dwarf::DW_FORM_data8; + break; + default: + addUInt(Die, dwarf::DW_AT_const_value, dwarf::DW_FORM_udata, + Val.getZExtValue()); + return; + } + addUInt(Die, dwarf::DW_AT_const_value, Form, Val.getZExtValue()); + return; + } + + DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); + + // Get the raw data form of the large APInt. + const uint64_t *Ptr64 = Val.getRawData(); + + int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte. + bool LittleEndian = Asm->getDataLayout().isLittleEndian(); + + // Output the constant to DWARF one byte at a time. + for (int i = 0; i < NumBytes; i++) { + uint8_t c; + if (LittleEndian) + c = Ptr64[i / 8] >> (8 * (i & 7)); + else + c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7)); + addUInt(Block, dwarf::DW_FORM_data1, c); + } + + addBlock(Die, dwarf::DW_AT_const_value, Block); +} + +/// addTemplateParams - Add template parameters into buffer. +void CompileUnit::addTemplateParams(DIE &Buffer, DIArray TParams) { + // Add template parameters. + for (unsigned i = 0, e = TParams.getNumElements(); i != e; ++i) { + DIDescriptor Element = TParams.getElement(i); + if (Element.isTemplateTypeParameter()) + constructTemplateTypeParameterDIE(Buffer, + DITemplateTypeParameter(Element)); + else if (Element.isTemplateValueParameter()) + constructTemplateValueParameterDIE(Buffer, + DITemplateValueParameter(Element)); + } +} + +/// getOrCreateContextDIE - Get context owner's DIE. +DIE *CompileUnit::getOrCreateContextDIE(DIScope Context) { + if (!Context || Context.isFile()) + return getCUDie(); + if (Context.isType()) + return getOrCreateTypeDIE(DIType(Context)); + if (Context.isNameSpace()) + return getOrCreateNameSpace(DINameSpace(Context)); + if (Context.isSubprogram()) + return getOrCreateSubprogramDIE(DISubprogram(Context)); + return getDIE(Context); +} + +/// getOrCreateTypeDIE - Find existing DIE or create new DIE for the +/// given DIType. +DIE *CompileUnit::getOrCreateTypeDIE(const MDNode *TyNode) { + if (!TyNode) + return NULL; + + DIType Ty(TyNode); + assert(Ty.isType()); + + // Construct the context before querying for the existence of the DIE in case + // such construction creates the DIE. + DIE *ContextDIE = getOrCreateContextDIE(resolve(Ty.getContext())); + assert(ContextDIE); + + DIE *TyDIE = getDIE(Ty); + if (TyDIE) + return TyDIE; + + // Create new type. + TyDIE = createAndAddDIE(Ty.getTag(), *ContextDIE, Ty); + + if (Ty.isBasicType()) + constructTypeDIE(*TyDIE, DIBasicType(Ty)); + else if (Ty.isCompositeType()) + constructTypeDIE(*TyDIE, DICompositeType(Ty)); + else { + assert(Ty.isDerivedType() && "Unknown kind of DIType"); + constructTypeDIE(*TyDIE, DIDerivedType(Ty)); + } + // If this is a named finished type then include it in the list of types + // for the accelerator tables. + if (!Ty.getName().empty() && !Ty.isForwardDecl()) { + bool IsImplementation = 0; + if (Ty.isCompositeType()) { + DICompositeType CT(Ty); + // A runtime language of 0 actually means C/C++ and that any + // non-negative value is some version of Objective-C/C++. + IsImplementation = (CT.getRunTimeLang() == 0) || CT.isObjcClassComplete(); + } + unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0; + addAccelType(Ty.getName(), std::make_pair(TyDIE, Flags)); + } + + return TyDIE; +} + +/// addType - Add a new type attribute to the specified entity. +void CompileUnit::addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute) { + assert(Ty && "Trying to add a type that doesn't exist?"); + + // Check for pre-existence. + DIEEntry *Entry = getDIEEntry(Ty); + // If it exists then use the existing value. + if (Entry) { + addDIEEntry(Entity, Attribute, Entry); + return; + } + + // Construct type. + DIE *Buffer = getOrCreateTypeDIE(Ty); + + // Set up proxy. + Entry = createDIEEntry(Buffer); + insertDIEEntry(Ty, Entry); + addDIEEntry(Entity, Attribute, Entry); + + // If this is a complete composite type then include it in the + // list of global types. + addGlobalType(Ty); +} + +// Accelerator table mutators - add each name along with its companion +// DIE to the proper table while ensuring that the name that we're going +// to reference is in the string table. We do this since the names we +// add may not only be identical to the names in the DIE. +void CompileUnit::addAccelName(StringRef Name, DIE *Die) { + DU->getStringPoolEntry(Name); + std::vector &DIEs = AccelNames[Name]; + DIEs.push_back(Die); +} + +void CompileUnit::addAccelObjC(StringRef Name, DIE *Die) { + DU->getStringPoolEntry(Name); + std::vector &DIEs = AccelObjC[Name]; + DIEs.push_back(Die); +} + +void CompileUnit::addAccelNamespace(StringRef Name, DIE *Die) { + DU->getStringPoolEntry(Name); + std::vector &DIEs = AccelNamespace[Name]; + DIEs.push_back(Die); +} + +void CompileUnit::addAccelType(StringRef Name, std::pair Die) { + DU->getStringPoolEntry(Name); + std::vector > &DIEs = AccelTypes[Name]; + DIEs.push_back(Die); +} + +/// addGlobalName - Add a new global name to the compile unit. +void CompileUnit::addGlobalName(StringRef Name, DIE *Die, DIScope Context) { + std::string FullName = getParentContextString(Context) + Name.str(); + GlobalNames[FullName] = Die; +} + +/// addGlobalType - Add a new global type to the compile unit. +/// +void CompileUnit::addGlobalType(DIType Ty) { + DIScope Context = resolve(Ty.getContext()); + if (!Ty.getName().empty() && !Ty.isForwardDecl() && + (!Context || Context.isCompileUnit() || Context.isFile() || + Context.isNameSpace())) + if (DIEEntry *Entry = getDIEEntry(Ty)) { + std::string FullName = + getParentContextString(Context) + Ty.getName().str(); + GlobalTypes[FullName] = Entry->getEntry(); + } +} + +/// getParentContextString - Walks the metadata parent chain in a language +/// specific manner (using the compile unit language) and returns +/// it as a string. This is done at the metadata level because DIEs may +/// not currently have been added to the parent context and walking the +/// DIEs looking for names is more expensive than walking the metadata. +std::string CompileUnit::getParentContextString(DIScope Context) const { + if (!Context) + return ""; + + // FIXME: Decide whether to implement this for non-C++ languages. + if (getLanguage() != dwarf::DW_LANG_C_plus_plus) + return ""; + + std::string CS; + SmallVector Parents; + while (!Context.isCompileUnit()) { + Parents.push_back(Context); + if (Context.getContext()) + Context = resolve(Context.getContext()); + else + // Structure, etc types will have a NULL context if they're at the top + // level. + break; + } + + // Reverse iterate over our list to go from the outermost construct to the + // innermost. + for (SmallVectorImpl::reverse_iterator I = Parents.rbegin(), + E = Parents.rend(); + I != E; ++I) { + DIScope Ctx = *I; + StringRef Name = Ctx.getName(); + if (!Name.empty()) { + CS += Name; + CS += "::"; + } + } + return CS; +} + +/// addPubTypes - Add subprogram argument types for pubtypes section. +void CompileUnit::addPubTypes(DISubprogram SP) { + DICompositeType SPTy = SP.getType(); + uint16_t SPTag = SPTy.getTag(); + if (SPTag != dwarf::DW_TAG_subroutine_type) + return; + + DIArray Args = SPTy.getTypeArray(); + for (unsigned i = 0, e = Args.getNumElements(); i != e; ++i) { + DIType ATy(Args.getElement(i)); + if (!ATy.isType()) + continue; + addGlobalType(ATy); + } +} + +/// constructTypeDIE - Construct basic type die from DIBasicType. +void CompileUnit::constructTypeDIE(DIE &Buffer, DIBasicType BTy) { + // Get core information. + StringRef Name = BTy.getName(); + // Add name if not anonymous or intermediate type. + if (!Name.empty()) + addString(&Buffer, dwarf::DW_AT_name, Name); + + // An unspecified type only has a name attribute. + if (BTy.getTag() == dwarf::DW_TAG_unspecified_type) + return; + + addUInt(&Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, + BTy.getEncoding()); + + uint64_t Size = BTy.getSizeInBits() >> 3; + addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); +} + +/// constructTypeDIE - Construct derived type die from DIDerivedType. +void CompileUnit::constructTypeDIE(DIE &Buffer, DIDerivedType DTy) { + // Get core information. + StringRef Name = DTy.getName(); + uint64_t Size = DTy.getSizeInBits() >> 3; + uint16_t Tag = Buffer.getTag(); + + // Map to main type, void will not have a type. + DIType FromTy = resolve(DTy.getTypeDerivedFrom()); + if (FromTy) + addType(&Buffer, FromTy); + + // Add name if not anonymous or intermediate type. + if (!Name.empty()) + addString(&Buffer, dwarf::DW_AT_name, Name); + + // Add size if non-zero (derived types might be zero-sized.) + if (Size && Tag != dwarf::DW_TAG_pointer_type) + addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); + + if (Tag == dwarf::DW_TAG_ptr_to_member_type) + addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, + getOrCreateTypeDIE(resolve(DTy.getClassType()))); + // Add source line info if available and TyDesc is not a forward declaration. + if (!DTy.isForwardDecl()) + addSourceLine(&Buffer, DTy); +} + +/// Return true if the type is appropriately scoped to be contained inside +/// its own type unit. +static bool isTypeUnitScoped(DIType Ty, const DwarfDebug *DD) { + DIScope Parent = DD->resolve(Ty.getContext()); + while (Parent) { + // Don't generate a hash for anything scoped inside a function. + if (Parent.isSubprogram()) + return false; + Parent = DD->resolve(Parent.getContext()); + } + return true; +} + +/// Return true if the type should be split out into a type unit. +static bool shouldCreateTypeUnit(DICompositeType CTy, const DwarfDebug *DD) { + uint16_t Tag = CTy.getTag(); + + switch (Tag) { + case dwarf::DW_TAG_structure_type: + case dwarf::DW_TAG_union_type: + case dwarf::DW_TAG_enumeration_type: + case dwarf::DW_TAG_class_type: + // If this is a class, structure, union, or enumeration type + // that is a definition (not a declaration), and not scoped + // inside a function then separate this out as a type unit. + return !CTy.isForwardDecl() && isTypeUnitScoped(CTy, DD); + default: + return false; + } +} + +/// constructTypeDIE - Construct type DIE from DICompositeType. +void CompileUnit::constructTypeDIE(DIE &Buffer, DICompositeType CTy) { + // Get core information. + StringRef Name = CTy.getName(); + + uint64_t Size = CTy.getSizeInBits() >> 3; + uint16_t Tag = Buffer.getTag(); + + switch (Tag) { + case dwarf::DW_TAG_array_type: + constructArrayTypeDIE(Buffer, CTy); + break; + case dwarf::DW_TAG_enumeration_type: + constructEnumTypeDIE(Buffer, CTy); + break; + case dwarf::DW_TAG_subroutine_type: { + // Add return type. A void return won't have a type. + DIArray Elements = CTy.getTypeArray(); + DIType RTy(Elements.getElement(0)); + if (RTy) + addType(&Buffer, RTy); + + bool isPrototyped = true; + // Add arguments. + for (unsigned i = 1, N = Elements.getNumElements(); i < N; ++i) { + DIDescriptor Ty = Elements.getElement(i); + if (Ty.isUnspecifiedParameter()) { + createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer); + isPrototyped = false; + } else { + DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer); + addType(Arg, DIType(Ty)); + if (DIType(Ty).isArtificial()) + addFlag(Arg, dwarf::DW_AT_artificial); + } + } + // Add prototype flag if we're dealing with a C language and the + // function has been prototyped. + uint16_t Language = getLanguage(); + if (isPrototyped && + (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || + Language == dwarf::DW_LANG_ObjC)) + addFlag(&Buffer, dwarf::DW_AT_prototyped); + } break; + case dwarf::DW_TAG_structure_type: + case dwarf::DW_TAG_union_type: + case dwarf::DW_TAG_class_type: { + // Add elements to structure type. + DIArray Elements = CTy.getTypeArray(); + for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { + DIDescriptor Element = Elements.getElement(i); + DIE *ElemDie = NULL; + if (Element.isSubprogram()) { + DISubprogram SP(Element); + ElemDie = getOrCreateSubprogramDIE(SP); + if (SP.isProtected()) + addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, + dwarf::DW_ACCESS_protected); + else if (SP.isPrivate()) + addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, + dwarf::DW_ACCESS_private); + else + addUInt(ElemDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, + dwarf::DW_ACCESS_public); + if (SP.isExplicit()) + addFlag(ElemDie, dwarf::DW_AT_explicit); + } else if (Element.isDerivedType()) { + DIDerivedType DDTy(Element); + if (DDTy.getTag() == dwarf::DW_TAG_friend) { + ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer); + addType(ElemDie, resolve(DDTy.getTypeDerivedFrom()), + dwarf::DW_AT_friend); + } else if (DDTy.isStaticMember()) { + getOrCreateStaticMemberDIE(DDTy); + } else { + constructMemberDIE(Buffer, DDTy); + } + } else if (Element.isObjCProperty()) { + DIObjCProperty Property(Element); + ElemDie = createAndAddDIE(Property.getTag(), Buffer); + StringRef PropertyName = Property.getObjCPropertyName(); + addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName); + addType(ElemDie, Property.getType()); + addSourceLine(ElemDie, Property); + StringRef GetterName = Property.getObjCPropertyGetterName(); + if (!GetterName.empty()) + addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName); + StringRef SetterName = Property.getObjCPropertySetterName(); + if (!SetterName.empty()) + addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName); + unsigned PropertyAttributes = 0; + if (Property.isReadOnlyObjCProperty()) + PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readonly; + if (Property.isReadWriteObjCProperty()) + PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_readwrite; + if (Property.isAssignObjCProperty()) + PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_assign; + if (Property.isRetainObjCProperty()) + PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_retain; + if (Property.isCopyObjCProperty()) + PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_copy; + if (Property.isNonAtomicObjCProperty()) + PropertyAttributes |= dwarf::DW_APPLE_PROPERTY_nonatomic; + if (PropertyAttributes) + addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None, + PropertyAttributes); + + DIEEntry *Entry = getDIEEntry(Element); + if (!Entry) { + Entry = createDIEEntry(ElemDie); + insertDIEEntry(Element, Entry); + } + } else + continue; + } + + if (CTy.isAppleBlockExtension()) + addFlag(&Buffer, dwarf::DW_AT_APPLE_block); + + DICompositeType ContainingType(resolve(CTy.getContainingType())); + if (ContainingType) + addDIEEntry(&Buffer, dwarf::DW_AT_containing_type, + getOrCreateTypeDIE(ContainingType)); + + if (CTy.isObjcClassComplete()) + addFlag(&Buffer, dwarf::DW_AT_APPLE_objc_complete_type); + + // Add template parameters to a class, structure or union types. + // FIXME: The support isn't in the metadata for this yet. + if (Tag == dwarf::DW_TAG_class_type || + Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) + addTemplateParams(Buffer, CTy.getTemplateParams()); + + break; + } + default: + break; + } + + // Add name if not anonymous or intermediate type. + if (!Name.empty()) + addString(&Buffer, dwarf::DW_AT_name, Name); + + if (Tag == dwarf::DW_TAG_enumeration_type || + Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type || + Tag == dwarf::DW_TAG_union_type) { + // Add size if non-zero (derived types might be zero-sized.) + // TODO: Do we care about size for enum forward declarations? + if (Size) + addUInt(&Buffer, dwarf::DW_AT_byte_size, None, Size); + else if (!CTy.isForwardDecl()) + // Add zero size if it is not a forward declaration. + addUInt(&Buffer, dwarf::DW_AT_byte_size, None, 0); + + // If we're a forward decl, say so. + if (CTy.isForwardDecl()) + addFlag(&Buffer, dwarf::DW_AT_declaration); + + // Add source line info if available. + if (!CTy.isForwardDecl()) + addSourceLine(&Buffer, CTy); + + // No harm in adding the runtime language to the declaration. + unsigned RLang = CTy.getRunTimeLang(); + if (RLang) + addUInt(&Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1, + RLang); + } + // If this is a type applicable to a type unit it then add it to the + // list of types we'll compute a hash for later. + if (shouldCreateTypeUnit(CTy, DD)) + DD->addTypeUnitType(&Buffer); +} + +/// constructTemplateTypeParameterDIE - Construct new DIE for the given +/// DITemplateTypeParameter. +void +CompileUnit::constructTemplateTypeParameterDIE(DIE &Buffer, + DITemplateTypeParameter TP) { + DIE *ParamDIE = + createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer); + // Add the type if it exists, it could be void and therefore no type. + if (TP.getType()) + addType(ParamDIE, resolve(TP.getType())); + if (!TP.getName().empty()) + addString(ParamDIE, dwarf::DW_AT_name, TP.getName()); +} + +/// constructTemplateValueParameterDIE - Construct new DIE for the given +/// DITemplateValueParameter. +void +CompileUnit::constructTemplateValueParameterDIE(DIE &Buffer, + DITemplateValueParameter VP) { + DIE *ParamDIE = createAndAddDIE(VP.getTag(), Buffer); + + // Add the type if there is one, template template and template parameter + // packs will not have a type. + if (VP.getTag() == dwarf::DW_TAG_template_value_parameter) + addType(ParamDIE, resolve(VP.getType())); + if (!VP.getName().empty()) + addString(ParamDIE, dwarf::DW_AT_name, VP.getName()); + if (Value *Val = VP.getValue()) { + if (ConstantInt *CI = dyn_cast(Val)) + addConstantValue(ParamDIE, CI, + isUnsignedDIType(DD, resolve(VP.getType()))); + else if (GlobalValue *GV = dyn_cast(Val)) { + // For declaration non-type template parameters (such as global values and + // functions) + DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); + addOpAddress(Block, Asm->getSymbol(GV)); + // Emit DW_OP_stack_value to use the address as the immediate value of the + // parameter, rather than a pointer to it. + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value); + addBlock(ParamDIE, dwarf::DW_AT_location, Block); + } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_template_param) { + assert(isa(Val)); + addString(ParamDIE, dwarf::DW_AT_GNU_template_name, + cast(Val)->getString()); + } else if (VP.getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) { + assert(isa(Val)); + DIArray A(cast(Val)); + addTemplateParams(*ParamDIE, A); + } + } +} + +/// getOrCreateNameSpace - Create a DIE for DINameSpace. +DIE *CompileUnit::getOrCreateNameSpace(DINameSpace NS) { + // Construct the context before querying for the existence of the DIE in case + // such construction creates the DIE. + DIE *ContextDIE = getOrCreateContextDIE(NS.getContext()); + + DIE *NDie = getDIE(NS); + if (NDie) + return NDie; + NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS); + + if (!NS.getName().empty()) { + addString(NDie, dwarf::DW_AT_name, NS.getName()); + addAccelNamespace(NS.getName(), NDie); + addGlobalName(NS.getName(), NDie, NS.getContext()); + } else + addAccelNamespace("(anonymous namespace)", NDie); + addSourceLine(NDie, NS); + return NDie; +} + +/// getOrCreateSubprogramDIE - Create new DIE using SP. +DIE *CompileUnit::getOrCreateSubprogramDIE(DISubprogram SP) { + // Construct the context before querying for the existence of the DIE in case + // such construction creates the DIE (as is the case for member function + // declarations). + DIE *ContextDIE = getOrCreateContextDIE(resolve(SP.getContext())); + + DIE *SPDie = getDIE(SP); + if (SPDie) + return SPDie; + + DISubprogram SPDecl = SP.getFunctionDeclaration(); + if (SPDecl.isSubprogram()) + // Add subprogram definitions to the CU die directly. + ContextDIE = CUDie.get(); + + // DW_TAG_inlined_subroutine may refer to this DIE. + SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP); + + DIE *DeclDie = NULL; + if (SPDecl.isSubprogram()) + DeclDie = getOrCreateSubprogramDIE(SPDecl); + + // Add function template parameters. + addTemplateParams(*SPDie, SP.getTemplateParams()); + + // If this DIE is going to refer declaration info using AT_specification + // then there is no need to add other attributes. + if (DeclDie) { + // Refer function declaration directly. + addDIEEntry(SPDie, dwarf::DW_AT_specification, DeclDie); + + return SPDie; + } + + // Add the linkage name if we have one. + StringRef LinkageName = SP.getLinkageName(); + if (!LinkageName.empty()) + addString(SPDie, dwarf::DW_AT_MIPS_linkage_name, + GlobalValue::getRealLinkageName(LinkageName)); + + // Constructors and operators for anonymous aggregates do not have names. + if (!SP.getName().empty()) + addString(SPDie, dwarf::DW_AT_name, SP.getName()); + + addSourceLine(SPDie, SP); + + // Add the prototype if we have a prototype and we have a C like + // language. + uint16_t Language = getLanguage(); + if (SP.isPrototyped() && + (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 || + Language == dwarf::DW_LANG_ObjC)) + addFlag(SPDie, dwarf::DW_AT_prototyped); + + DICompositeType SPTy = SP.getType(); + assert(SPTy.getTag() == dwarf::DW_TAG_subroutine_type && + "the type of a subprogram should be a subroutine"); + + DIArray Args = SPTy.getTypeArray(); + // Add a return type. If this is a type like a C/C++ void type we don't add a + // return type. + if (Args.getElement(0)) + addType(SPDie, DIType(Args.getElement(0))); + + unsigned VK = SP.getVirtuality(); + if (VK) { + addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK); + DIEBlock *Block = getDIEBlock(); + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); + addUInt(Block, dwarf::DW_FORM_udata, SP.getVirtualIndex()); + addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block); + ContainingTypeMap.insert( + std::make_pair(SPDie, resolve(SP.getContainingType()))); + } + + if (!SP.isDefinition()) { + addFlag(SPDie, dwarf::DW_AT_declaration); + + // Add arguments. Do not add arguments for subprogram definition. They will + // be handled while processing variables. + for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) { + DIE *Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie); + DIType ATy(Args.getElement(i)); + addType(Arg, ATy); + if (ATy.isArtificial()) + addFlag(Arg, dwarf::DW_AT_artificial); + } + } + + if (SP.isArtificial()) + addFlag(SPDie, dwarf::DW_AT_artificial); + + if (!SP.isLocalToUnit()) + addFlag(SPDie, dwarf::DW_AT_external); + + if (SP.isOptimized()) + addFlag(SPDie, dwarf::DW_AT_APPLE_optimized); + + if (unsigned isa = Asm->getISAEncoding()) { + addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa); + } + + return SPDie; +} + +// Return const expression if value is a GEP to access merged global +// constant. e.g. +// i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0) +static const ConstantExpr *getMergedGlobalExpr(const Value *V) { + const ConstantExpr *CE = dyn_cast_or_null(V); + if (!CE || CE->getNumOperands() != 3 || + CE->getOpcode() != Instruction::GetElementPtr) + return NULL; + + // First operand points to a global struct. + Value *Ptr = CE->getOperand(0); + if (!isa(Ptr) || + !isa(cast(Ptr->getType())->getElementType())) + return NULL; + + // Second operand is zero. + const ConstantInt *CI = dyn_cast_or_null(CE->getOperand(1)); + if (!CI || !CI->isZero()) + return NULL; + + // Third operand is offset. + if (!isa(CE->getOperand(2))) + return NULL; + + return CE; +} + +/// createGlobalVariableDIE - create global variable DIE. +void CompileUnit::createGlobalVariableDIE(DIGlobalVariable GV) { + + // Check for pre-existence. + if (getDIE(GV)) + return; + + if (!GV.isGlobalVariable()) + return; + + DIScope GVContext = GV.getContext(); + DIType GTy = GV.getType(); + + // If this is a static data member definition, some attributes belong + // to the declaration DIE. + DIE *VariableDIE = NULL; + bool IsStaticMember = false; + DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration(); + if (SDMDecl.Verify()) { + assert(SDMDecl.isStaticMember() && "Expected static member decl"); + // We need the declaration DIE that is in the static member's class. + VariableDIE = getOrCreateStaticMemberDIE(SDMDecl); + IsStaticMember = true; + } + + // If this is not a static data member definition, create the variable + // DIE and add the initial set of attributes to it. + if (!VariableDIE) { + // Construct the context before querying for the existence of the DIE in + // case such construction creates the DIE. + DIE *ContextDIE = getOrCreateContextDIE(GVContext); + + // Add to map. + VariableDIE = createAndAddDIE(GV.getTag(), *ContextDIE, GV); + + // Add name and type. + addString(VariableDIE, dwarf::DW_AT_name, GV.getDisplayName()); + addType(VariableDIE, GTy); + + // Add scoping info. + if (!GV.isLocalToUnit()) + addFlag(VariableDIE, dwarf::DW_AT_external); + + // Add line number info. + addSourceLine(VariableDIE, GV); + } + + // Add location. + bool addToAccelTable = false; + DIE *VariableSpecDIE = NULL; + bool isGlobalVariable = GV.getGlobal() != NULL; + if (isGlobalVariable) { + addToAccelTable = true; + DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); + const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal()); + if (GV.getGlobal()->isThreadLocal()) { + // FIXME: Make this work with -gsplit-dwarf. + unsigned PointerSize = Asm->getDataLayout().getPointerSize(); + assert((PointerSize == 4 || PointerSize == 8) && + "Add support for other sizes if necessary"); + const MCExpr *Expr = + Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym); + // Based on GCC's support for TLS: + if (!DD->useSplitDwarf()) { + // 1) Start with a constNu of the appropriate pointer size + addUInt(Block, dwarf::DW_FORM_data1, + PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u); + // 2) containing the (relocated) offset of the TLS variable + // within the module's TLS block. + addExpr(Block, dwarf::DW_FORM_udata, Expr); + } else { + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index); + addUInt(Block, dwarf::DW_FORM_udata, DU->getAddrPoolIndex(Expr)); + } + // 3) followed by a custom OP to make the debugger do a TLS lookup. + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address); + } else + addOpAddress(Block, Sym); + // Do not create specification DIE if context is either compile unit + // or a subprogram. + if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() && + !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) { + // Create specification DIE. + VariableSpecDIE = createAndAddDIE(dwarf::DW_TAG_variable, *CUDie); + addDIEEntry(VariableSpecDIE, dwarf::DW_AT_specification, VariableDIE); + addBlock(VariableSpecDIE, dwarf::DW_AT_location, Block); + // A static member's declaration is already flagged as such. + if (!SDMDecl.Verify()) + addFlag(VariableDIE, dwarf::DW_AT_declaration); + } else { + addBlock(VariableDIE, dwarf::DW_AT_location, Block); + } + // Add the linkage name. + StringRef LinkageName = GV.getLinkageName(); + if (!LinkageName.empty()) + // From DWARF4: DIEs to which DW_AT_linkage_name may apply include: + // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and + // TAG_variable. + addString(IsStaticMember && VariableSpecDIE ? VariableSpecDIE + : VariableDIE, + dwarf::DW_AT_MIPS_linkage_name, + GlobalValue::getRealLinkageName(LinkageName)); + } else if (const ConstantInt *CI = + dyn_cast_or_null(GV.getConstant())) { + // AT_const_value was added when the static member was created. To avoid + // emitting AT_const_value multiple times, we only add AT_const_value when + // it is not a static member. + if (!IsStaticMember) + addConstantValue(VariableDIE, CI, isUnsignedDIType(DD, GTy)); + } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV->getOperand(11))) { + addToAccelTable = true; + // GV is a merged global. + DIEBlock *Block = new (DIEValueAllocator) DIEBlock(); + Value *Ptr = CE->getOperand(0); + addOpAddress(Block, Asm->getSymbol(cast(Ptr))); + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); + SmallVector Idx(CE->op_begin() + 1, CE->op_end()); + addUInt(Block, dwarf::DW_FORM_udata, + Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx)); + addUInt(Block, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); + addBlock(VariableDIE, dwarf::DW_AT_location, Block); + } + + if (addToAccelTable) { + DIE *AddrDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE; + addAccelName(GV.getName(), AddrDIE); + + // If the linkage name is different than the name, go ahead and output + // that as well into the name table. + if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName()) + addAccelName(GV.getLinkageName(), AddrDIE); + } + + if (!GV.isLocalToUnit()) + addGlobalName(GV.getName(), VariableSpecDIE ? VariableSpecDIE : VariableDIE, + GV.getContext()); +} + +/// constructSubrangeDIE - Construct subrange DIE from DISubrange. +void CompileUnit::constructSubrangeDIE(DIE &Buffer, DISubrange SR, + DIE *IndexTy) { + DIE *DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer); + addDIEEntry(DW_Subrange, dwarf::DW_AT_type, IndexTy); + + // The LowerBound value defines the lower bounds which is typically zero for + // C/C++. The Count value is the number of elements. Values are 64 bit. If + // Count == -1 then the array is unbounded and we do not emit + // DW_AT_lower_bound and DW_AT_upper_bound attributes. If LowerBound == 0 and + // Count == 0, then the array has zero elements in which case we do not emit + // an upper bound. + int64_t LowerBound = SR.getLo(); + int64_t DefaultLowerBound = getDefaultLowerBound(); + int64_t Count = SR.getCount(); + + if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound) + addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound); + + if (Count != -1 && Count != 0) + // FIXME: An unbounded array should reference the expression that defines + // the array. + addUInt(DW_Subrange, dwarf::DW_AT_upper_bound, None, + LowerBound + Count - 1); +} + +/// constructArrayTypeDIE - Construct array type DIE from DICompositeType. +void CompileUnit::constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy) { + if (CTy.isVector()) + addFlag(&Buffer, dwarf::DW_AT_GNU_vector); + + // Emit the element type. + addType(&Buffer, resolve(CTy.getTypeDerivedFrom())); + + // Get an anonymous type for index type. + // FIXME: This type should be passed down from the front end + // as different languages may have different sizes for indexes. + DIE *IdxTy = getIndexTyDie(); + if (!IdxTy) { + // Construct an anonymous type for index type. + IdxTy = createAndAddDIE(dwarf::DW_TAG_base_type, *CUDie.get()); + addString(IdxTy, dwarf::DW_AT_name, "int"); + addUInt(IdxTy, dwarf::DW_AT_byte_size, None, sizeof(int32_t)); + addUInt(IdxTy, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1, + dwarf::DW_ATE_signed); + setIndexTyDie(IdxTy); + } + + // Add subranges to array type. + DIArray Elements = CTy.getTypeArray(); + for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { + DIDescriptor Element = Elements.getElement(i); + if (Element.getTag() == dwarf::DW_TAG_subrange_type) + constructSubrangeDIE(Buffer, DISubrange(Element), IdxTy); + } +} + +/// constructEnumTypeDIE - Construct an enum type DIE from DICompositeType. +void CompileUnit::constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy) { + DIArray Elements = CTy.getTypeArray(); + + // Add enumerators to enumeration type. + for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) { + DIEnumerator Enum(Elements.getElement(i)); + if (Enum.isEnumerator()) { + DIE *Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer); + StringRef Name = Enum.getName(); + addString(Enumerator, dwarf::DW_AT_name, Name); + int64_t Value = Enum.getEnumValue(); + addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata, Value); + } + } + DIType DTy = resolve(CTy.getTypeDerivedFrom()); + if (DTy) { + addType(&Buffer, DTy); + addFlag(&Buffer, dwarf::DW_AT_enum_class); + } +} + +/// constructContainingTypeDIEs - Construct DIEs for types that contain +/// vtables. +void CompileUnit::constructContainingTypeDIEs() { + for (DenseMap::iterator CI = ContainingTypeMap.begin(), + CE = ContainingTypeMap.end(); + CI != CE; ++CI) { + DIE *SPDie = CI->first; + DIDescriptor D(CI->second); + if (!D) + continue; + DIE *NDie = getDIE(D); + if (!NDie) + continue; + addDIEEntry(SPDie, dwarf::DW_AT_containing_type, NDie); + } +} + +/// constructVariableDIE - Construct a DIE for the given DbgVariable. +DIE *CompileUnit::constructVariableDIE(DbgVariable &DV, bool isScopeAbstract) { + StringRef Name = DV.getName(); + + // Define variable debug information entry. + DIE *VariableDie = new DIE(DV.getTag()); + DbgVariable *AbsVar = DV.getAbstractVariable(); + DIE *AbsDIE = AbsVar ? AbsVar->getDIE() : NULL; + if (AbsDIE) + addDIEEntry(VariableDie, dwarf::DW_AT_abstract_origin, AbsDIE); + else { + if (!Name.empty()) + addString(VariableDie, dwarf::DW_AT_name, Name); + addSourceLine(VariableDie, DV.getVariable()); + addType(VariableDie, DV.getType()); + } + + if (DV.isArtificial()) + addFlag(VariableDie, dwarf::DW_AT_artificial); + + if (isScopeAbstract) { + DV.setDIE(VariableDie); + return VariableDie; + } + + // Add variable address. + + unsigned Offset = DV.getDotDebugLocOffset(); + if (Offset != ~0U) { + addLabel(VariableDie, dwarf::DW_AT_location, + DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset + : dwarf::DW_FORM_data4, + Asm->GetTempSymbol("debug_loc", Offset)); + DV.setDIE(VariableDie); + return VariableDie; + } + + // Check if variable is described by a DBG_VALUE instruction. + if (const MachineInstr *DVInsn = DV.getMInsn()) { + assert(DVInsn->getNumOperands() == 3); + if (DVInsn->getOperand(0).isReg()) { + const MachineOperand RegOp = DVInsn->getOperand(0); + // If the second operand is an immediate, this is an indirect value. + if (DVInsn->getOperand(1).isImm()) { + MachineLocation Location(RegOp.getReg(), + DVInsn->getOperand(1).getImm()); + addVariableAddress(DV, VariableDie, Location); + } else if (RegOp.getReg()) + addVariableAddress(DV, VariableDie, MachineLocation(RegOp.getReg())); + } else if (DVInsn->getOperand(0).isImm()) + addConstantValue(VariableDie, DVInsn->getOperand(0), DV.getType()); + else if (DVInsn->getOperand(0).isFPImm()) + addConstantFPValue(VariableDie, DVInsn->getOperand(0)); + else if (DVInsn->getOperand(0).isCImm()) + addConstantValue(VariableDie, DVInsn->getOperand(0).getCImm(), + isUnsignedDIType(DD, DV.getType())); + + DV.setDIE(VariableDie); + return VariableDie; + } else { + // .. else use frame index. + int FI = DV.getFrameIndex(); + if (FI != ~0) { + unsigned FrameReg = 0; + const TargetFrameLowering *TFI = Asm->TM.getFrameLowering(); + int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg); + MachineLocation Location(FrameReg, Offset); + addVariableAddress(DV, VariableDie, Location); + } + } + + DV.setDIE(VariableDie); + return VariableDie; +} + +/// constructMemberDIE - Construct member DIE from DIDerivedType. +void CompileUnit::constructMemberDIE(DIE &Buffer, DIDerivedType DT) { + DIE *MemberDie = createAndAddDIE(DT.getTag(), Buffer); + StringRef Name = DT.getName(); + if (!Name.empty()) + addString(MemberDie, dwarf::DW_AT_name, Name); + + addType(MemberDie, resolve(DT.getTypeDerivedFrom())); + + addSourceLine(MemberDie, DT); + + DIEBlock *MemLocationDie = new (DIEValueAllocator) DIEBlock(); + addUInt(MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst); + + if (DT.getTag() == dwarf::DW_TAG_inheritance && DT.isVirtual()) { + + // For C++, virtual base classes are not at fixed offset. Use following + // expression to extract appropriate offset from vtable. + // BaseAddr = ObAddr + *((*ObAddr) - Offset) + + DIEBlock *VBaseLocationDie = new (DIEValueAllocator) DIEBlock(); + addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup); + addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); + addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu); + addUInt(VBaseLocationDie, dwarf::DW_FORM_udata, DT.getOffsetInBits()); + addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus); + addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref); + addUInt(VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus); + + addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie); + } else { + uint64_t Size = DT.getSizeInBits(); + uint64_t FieldSize = getBaseTypeSize(DD, DT); + uint64_t OffsetInBytes; + + if (Size != FieldSize) { + // Handle bitfield. + addUInt(MemberDie, dwarf::DW_AT_byte_size, None, + getBaseTypeSize(DD, DT) >> 3); + addUInt(MemberDie, dwarf::DW_AT_bit_size, None, DT.getSizeInBits()); + + uint64_t Offset = DT.getOffsetInBits(); + uint64_t AlignMask = ~(DT.getAlignInBits() - 1); + uint64_t HiMark = (Offset + FieldSize) & AlignMask; + uint64_t FieldOffset = (HiMark - FieldSize); + Offset -= FieldOffset; + + // Maybe we need to work from the other end. + if (Asm->getDataLayout().isLittleEndian()) + Offset = FieldSize - (Offset + Size); + addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset); + + // Here WD_AT_data_member_location points to the anonymous + // field that includes this bit field. + OffsetInBytes = FieldOffset >> 3; + } else + // This is not a bitfield. + OffsetInBytes = DT.getOffsetInBits() >> 3; + addUInt(MemberDie, dwarf::DW_AT_data_member_location, None, OffsetInBytes); + } + + if (DT.isProtected()) + addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, + dwarf::DW_ACCESS_protected); + else if (DT.isPrivate()) + addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, + dwarf::DW_ACCESS_private); + // Otherwise C++ member and base classes are considered public. + else + addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, + dwarf::DW_ACCESS_public); + if (DT.isVirtual()) + addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, + dwarf::DW_VIRTUALITY_virtual); + + // Objective-C properties. + if (MDNode *PNode = DT.getObjCProperty()) + if (DIEEntry *PropertyDie = getDIEEntry(PNode)) + MemberDie->addValue(dwarf::DW_AT_APPLE_property, dwarf::DW_FORM_ref4, + PropertyDie); + + if (DT.isArtificial()) + addFlag(MemberDie, dwarf::DW_AT_artificial); +} + +/// getOrCreateStaticMemberDIE - Create new DIE for C++ static member. +DIE *CompileUnit::getOrCreateStaticMemberDIE(DIDerivedType DT) { + if (!DT.Verify()) + return NULL; + + // Construct the context before querying for the existence of the DIE in case + // such construction creates the DIE. + DIE *ContextDIE = getOrCreateContextDIE(resolve(DT.getContext())); + assert(dwarf::isType(ContextDIE->getTag()) && + "Static member should belong to a type."); + + DIE *StaticMemberDIE = getDIE(DT); + if (StaticMemberDIE) + return StaticMemberDIE; + + StaticMemberDIE = createAndAddDIE(DT.getTag(), *ContextDIE, DT); + + DIType Ty = resolve(DT.getTypeDerivedFrom()); + + addString(StaticMemberDIE, dwarf::DW_AT_name, DT.getName()); + addType(StaticMemberDIE, Ty); + addSourceLine(StaticMemberDIE, DT); + addFlag(StaticMemberDIE, dwarf::DW_AT_external); + addFlag(StaticMemberDIE, dwarf::DW_AT_declaration); + + // FIXME: We could omit private if the parent is a class_type, and + // public if the parent is something else. + if (DT.isProtected()) + addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, + dwarf::DW_ACCESS_protected); + else if (DT.isPrivate()) + addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, + dwarf::DW_ACCESS_private); + else + addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1, + dwarf::DW_ACCESS_public); + + if (const ConstantInt *CI = dyn_cast_or_null(DT.getConstant())) + addConstantValue(StaticMemberDIE, CI, isUnsignedDIType(DD, Ty)); + if (const ConstantFP *CFP = dyn_cast_or_null(DT.getConstant())) + addConstantFPValue(StaticMemberDIE, CFP); + + return StaticMemberDIE; +} + +void CompileUnit::emitHeader(const MCSection *ASection, + const MCSymbol *ASectionSym) { + Asm->OutStreamer.AddComment("DWARF version number"); + Asm->EmitInt16(DD->getDwarfVersion()); + Asm->OutStreamer.AddComment("Offset Into Abbrev. Section"); + Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()), + ASectionSym); + Asm->OutStreamer.AddComment("Address Size (in bytes)"); + Asm->EmitInt8(Asm->getDataLayout().getPointerSize()); +} diff --git a/lib/CodeGen/AsmPrinter/DwarfUnit.h b/lib/CodeGen/AsmPrinter/DwarfUnit.h new file mode 100644 index 0000000..d782c88 --- /dev/null +++ b/lib/CodeGen/AsmPrinter/DwarfUnit.h @@ -0,0 +1,409 @@ +//===-- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit ---*- C++ -*--===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains support for writing dwarf compile unit. +// +//===----------------------------------------------------------------------===// + +#ifndef CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H +#define CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H + +#include "DIE.h" +#include "DwarfDebug.h" +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/OwningPtr.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/DebugInfo.h" +#include "llvm/MC/MCExpr.h" + +namespace llvm { + +class MachineLocation; +class MachineOperand; +class ConstantInt; +class ConstantFP; +class DbgVariable; + +//===----------------------------------------------------------------------===// +/// CompileUnit - This dwarf writer support class manages information associated +/// with a source file. +class CompileUnit { + /// UniqueID - a numeric ID unique among all CUs in the module + /// + unsigned UniqueID; + + /// Node - MDNode for the compile unit. + DICompileUnit Node; + + /// CUDie - Compile unit debug information entry. + /// + const OwningPtr CUDie; + + /// Asm - Target of Dwarf emission. + AsmPrinter *Asm; + + // Holders for some common dwarf information. + DwarfDebug *DD; + DwarfUnits *DU; + + /// IndexTyDie - An anonymous type for index type. Owned by CUDie. + DIE *IndexTyDie; + + /// MDNodeToDieMap - Tracks the mapping of unit level debug information + /// variables to debug information entries. + DenseMap MDNodeToDieMap; + + /// MDNodeToDIEEntryMap - Tracks the mapping of unit level debug information + /// descriptors to debug information entries using a DIEEntry proxy. + DenseMap MDNodeToDIEEntryMap; + + /// GlobalNames - A map of globally visible named entities for this unit. + /// + StringMap GlobalNames; + + /// GlobalTypes - A map of globally visible types for this unit. + /// + StringMap GlobalTypes; + + /// AccelNames - A map of names for the name accelerator table. + /// + StringMap > AccelNames; + StringMap > AccelObjC; + StringMap > AccelNamespace; + StringMap > > AccelTypes; + + /// DIEBlocks - A list of all the DIEBlocks in use. + std::vector DIEBlocks; + + /// ContainingTypeMap - This map is used to keep track of subprogram DIEs that + /// need DW_AT_containing_type attribute. This attribute points to a DIE that + /// corresponds to the MDNode mapped with the subprogram DIE. + DenseMap ContainingTypeMap; + + // DIEValueAllocator - All DIEValues are allocated through this allocator. + BumpPtrAllocator DIEValueAllocator; + + // DIEIntegerOne - A preallocated DIEValue because 1 is used frequently. + DIEInteger *DIEIntegerOne; + +public: + CompileUnit(unsigned UID, DIE *D, DICompileUnit CU, AsmPrinter *A, + DwarfDebug *DW, DwarfUnits *DWU); + ~CompileUnit(); + + // Accessors. + unsigned getUniqueID() const { return UniqueID; } + uint16_t getLanguage() const { return Node.getLanguage(); } + DICompileUnit getNode() const { return Node; } + DIE *getCUDie() const { return CUDie.get(); } + const StringMap &getGlobalNames() const { return GlobalNames; } + const StringMap &getGlobalTypes() const { return GlobalTypes; } + + const StringMap > &getAccelNames() const { + return AccelNames; + } + const StringMap > &getAccelObjC() const { + return AccelObjC; + } + const StringMap > &getAccelNamespace() const { + return AccelNamespace; + } + const StringMap > > & + getAccelTypes() const { + return AccelTypes; + } + + unsigned getDebugInfoOffset() const { return DebugInfoOffset; } + void setDebugInfoOffset(unsigned DbgInfoOff) { DebugInfoOffset = DbgInfoOff; } + + /// hasContent - Return true if this compile unit has something to write out. + /// + bool hasContent() const { return !CUDie->getChildren().empty(); } + + /// getParentContextString - Get a string containing the language specific + /// context for a global name. + std::string getParentContextString(DIScope Context) const; + + /// addGlobalName - Add a new global entity to the compile unit. + /// + void addGlobalName(StringRef Name, DIE *Die, DIScope Context); + + /// addGlobalType - Add a new global type to the compile unit. + /// + void addGlobalType(DIType Ty); + + /// addPubTypes - Add a set of types from the subprogram to the global types. + void addPubTypes(DISubprogram SP); + + /// addAccelName - Add a new name to the name accelerator table. + void addAccelName(StringRef Name, DIE *Die); + + /// addAccelObjC - Add a new name to the ObjC accelerator table. + void addAccelObjC(StringRef Name, DIE *Die); + + /// addAccelNamespace - Add a new name to the namespace accelerator table. + void addAccelNamespace(StringRef Name, DIE *Die); + + /// addAccelType - Add a new type to the type accelerator table. + void addAccelType(StringRef Name, std::pair Die); + + /// getDIE - Returns the debug information entry map slot for the + /// specified debug variable. We delegate the request to DwarfDebug + /// when the MDNode can be part of the type system, since DIEs for + /// the type system can be shared across CUs and the mappings are + /// kept in DwarfDebug. + DIE *getDIE(DIDescriptor D) const; + + DIEBlock *getDIEBlock() { return new (DIEValueAllocator) DIEBlock(); } + + /// insertDIE - Insert DIE into the map. We delegate the request to DwarfDebug + /// when the MDNode can be part of the type system, since DIEs for + /// the type system can be shared across CUs and the mappings are + /// kept in DwarfDebug. + void insertDIE(DIDescriptor Desc, DIE *D); + + /// addDie - Adds or interns the DIE to the compile unit. + /// + void addDie(DIE *Buffer) { CUDie->addChild(Buffer); } + + /// addFlag - Add a flag that is true to the DIE. + void addFlag(DIE *Die, dwarf::Attribute Attribute); + + /// addUInt - Add an unsigned integer attribute data and value. + /// + void addUInt(DIE *Die, dwarf::Attribute Attribute, Optional Form, + uint64_t Integer); + + void addUInt(DIEBlock *Block, dwarf::Form Form, uint64_t Integer); + + /// addSInt - Add an signed integer attribute data and value. + /// + void addSInt(DIE *Die, dwarf::Attribute Attribute, Optional Form, + int64_t Integer); + + void addSInt(DIEBlock *Die, Optional Form, int64_t Integer); + + /// addString - Add a string attribute data and value. + /// + void addString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str); + + /// addLocalString - Add a string attribute data and value. + /// + void addLocalString(DIE *Die, dwarf::Attribute Attribute, const StringRef Str); + + /// addExpr - Add a Dwarf expression attribute data and value. + /// + void addExpr(DIEBlock *Die, dwarf::Form Form, const MCExpr *Expr); + + /// addLabel - Add a Dwarf label attribute data and value. + /// + void addLabel(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form, + const MCSymbol *Label); + + void addLabel(DIEBlock *Die, dwarf::Form Form, const MCSymbol *Label); + + /// addLabelAddress - Add a dwarf label attribute data and value using + /// either DW_FORM_addr or DW_FORM_GNU_addr_index. + /// + void addLabelAddress(DIE *Die, dwarf::Attribute Attribute, MCSymbol *Label); + + /// addOpAddress - Add a dwarf op address data and value using the + /// form given and an op of either DW_FORM_addr or DW_FORM_GNU_addr_index. + /// + void addOpAddress(DIEBlock *Die, const MCSymbol *Label); + + /// addDelta - Add a label delta attribute data and value. + /// + void addDelta(DIE *Die, dwarf::Attribute Attribute, dwarf::Form Form, const MCSymbol *Hi, + const MCSymbol *Lo); + + /// addDIEEntry - Add a DIE attribute data and value. + /// + void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIE *Entry); + + /// addDIEEntry - Add a DIE attribute data and value. + /// + void addDIEEntry(DIE *Die, dwarf::Attribute Attribute, DIEEntry *Entry); + + /// addBlock - Add block data. + /// + void addBlock(DIE *Die, dwarf::Attribute Attribute, DIEBlock *Block); + + /// addSourceLine - Add location information to specified debug information + /// entry. + void addSourceLine(DIE *Die, DIVariable V); + void addSourceLine(DIE *Die, DIGlobalVariable G); + void addSourceLine(DIE *Die, DISubprogram SP); + void addSourceLine(DIE *Die, DIType Ty); + void addSourceLine(DIE *Die, DINameSpace NS); + void addSourceLine(DIE *Die, DIObjCProperty Ty); + + /// addAddress - Add an address attribute to a die based on the location + /// provided. + void addAddress(DIE *Die, dwarf::Attribute Attribute, const MachineLocation &Location, + bool Indirect = false); + + /// addConstantValue - Add constant value entry in variable DIE. + void addConstantValue(DIE *Die, const MachineOperand &MO, DIType Ty); + void addConstantValue(DIE *Die, const ConstantInt *CI, bool Unsigned); + void addConstantValue(DIE *Die, const APInt &Val, bool Unsigned); + + /// addConstantFPValue - Add constant value entry in variable DIE. + void addConstantFPValue(DIE *Die, const MachineOperand &MO); + void addConstantFPValue(DIE *Die, const ConstantFP *CFP); + + /// addTemplateParams - Add template parameters in buffer. + void addTemplateParams(DIE &Buffer, DIArray TParams); + + /// addRegisterOp - Add register operand. + void addRegisterOp(DIEBlock *TheDie, unsigned Reg); + + /// addRegisterOffset - Add register offset. + void addRegisterOffset(DIEBlock *TheDie, unsigned Reg, int64_t Offset); + + /// addComplexAddress - Start with the address based on the location provided, + /// and generate the DWARF information necessary to find the actual variable + /// (navigating the extra location information encoded in the type) based on + /// the starting location. Add the DWARF information to the die. + /// + void addComplexAddress(const DbgVariable &DV, DIE *Die, dwarf::Attribute Attribute, + const MachineLocation &Location); + + // FIXME: Should be reformulated in terms of addComplexAddress. + /// addBlockByrefAddress - Start with the address based on the location + /// provided, and generate the DWARF information necessary to find the + /// actual Block variable (navigating the Block struct) based on the + /// starting location. Add the DWARF information to the die. Obsolete, + /// please use addComplexAddress instead. + /// + void addBlockByrefAddress(const DbgVariable &DV, DIE *Die, dwarf::Attribute Attribute, + const MachineLocation &Location); + + /// addVariableAddress - Add DW_AT_location attribute for a + /// DbgVariable based on provided MachineLocation. + void addVariableAddress(const DbgVariable &DV, DIE *Die, + MachineLocation Location); + + /// addType - Add a new type attribute to the specified entity. This takes + /// and attribute parameter because DW_AT_friend attributes are also + /// type references. + void addType(DIE *Entity, DIType Ty, dwarf::Attribute Attribute = dwarf::DW_AT_type); + + /// getOrCreateNameSpace - Create a DIE for DINameSpace. + DIE *getOrCreateNameSpace(DINameSpace NS); + + /// getOrCreateSubprogramDIE - Create new DIE using SP. + DIE *getOrCreateSubprogramDIE(DISubprogram SP); + + /// getOrCreateTypeDIE - Find existing DIE or create new DIE for the + /// given DIType. + DIE *getOrCreateTypeDIE(const MDNode *N); + + /// getOrCreateContextDIE - Get context owner's DIE. + DIE *getOrCreateContextDIE(DIScope Context); + + /// createGlobalVariableDIE - create global variable DIE. + void createGlobalVariableDIE(DIGlobalVariable GV); + + /// constructContainingTypeDIEs - Construct DIEs for types that contain + /// vtables. + void constructContainingTypeDIEs(); + + /// constructVariableDIE - Construct a DIE for the given DbgVariable. + DIE *constructVariableDIE(DbgVariable &DV, bool isScopeAbstract); + + /// Create a DIE with the given Tag, add the DIE to its parent, and + /// call insertDIE if MD is not null. + DIE *createAndAddDIE(unsigned Tag, DIE &Parent, DIDescriptor N = DIDescriptor()); + + /// Compute the size of a header for this unit, not including the initial + /// length field. + unsigned getHeaderSize() const { + return sizeof(int16_t) + // DWARF version number + sizeof(int32_t) + // Offset Into Abbrev. Section + sizeof(int8_t); // Pointer Size (in bytes) + } + + /// Emit the header for this unit, not including the initial length field. + void emitHeader(const MCSection *ASection, const MCSymbol *ASectionSym); + +private: + /// constructTypeDIE - Construct basic type die from DIBasicType. + void constructTypeDIE(DIE &Buffer, DIBasicType BTy); + + /// constructTypeDIE - Construct derived type die from DIDerivedType. + void constructTypeDIE(DIE &Buffer, DIDerivedType DTy); + + /// constructTypeDIE - Construct type DIE from DICompositeType. + void constructTypeDIE(DIE &Buffer, DICompositeType CTy); + + /// constructSubrangeDIE - Construct subrange DIE from DISubrange. + void constructSubrangeDIE(DIE &Buffer, DISubrange SR, DIE *IndexTy); + + /// constructArrayTypeDIE - Construct array type DIE from DICompositeType. + void constructArrayTypeDIE(DIE &Buffer, DICompositeType CTy); + + /// constructEnumTypeDIE - Construct enum type DIE from DIEnumerator. + void constructEnumTypeDIE(DIE &Buffer, DICompositeType CTy); + + /// constructMemberDIE - Construct member DIE from DIDerivedType. + void constructMemberDIE(DIE &Buffer, DIDerivedType DT); + + /// constructTemplateTypeParameterDIE - Construct new DIE for the given + /// DITemplateTypeParameter. + void constructTemplateTypeParameterDIE(DIE &Buffer, + DITemplateTypeParameter TP); + + /// constructTemplateValueParameterDIE - Construct new DIE for the given + /// DITemplateValueParameter. + void constructTemplateValueParameterDIE(DIE &Buffer, + DITemplateValueParameter TVP); + + /// getOrCreateStaticMemberDIE - Create new static data member DIE. + DIE *getOrCreateStaticMemberDIE(DIDerivedType DT); + + /// Offset of the CUDie from beginning of debug info section. + unsigned DebugInfoOffset; + + /// getLowerBoundDefault - Return the default lower bound for an array. If the + /// DWARF version doesn't handle the language, return -1. + int64_t getDefaultLowerBound() const; + + /// getDIEEntry - Returns the debug information entry for the specified + /// debug variable. + DIEEntry *getDIEEntry(const MDNode *N) const { + return MDNodeToDIEEntryMap.lookup(N); + } + + /// insertDIEEntry - Insert debug information entry into the map. + void insertDIEEntry(const MDNode *N, DIEEntry *E) { + MDNodeToDIEEntryMap.insert(std::make_pair(N, E)); + } + + // getIndexTyDie - Get an anonymous type for index type. + DIE *getIndexTyDie() { return IndexTyDie; } + + // setIndexTyDie - Set D as anonymous type for index which can be reused + // later. + void setIndexTyDie(DIE *D) { IndexTyDie = D; } + + /// createDIEEntry - Creates a new DIEEntry to be a proxy for a debug + /// information entry. + DIEEntry *createDIEEntry(DIE *Entry); + + /// resolve - Look in the DwarfDebug map for the MDNode that + /// corresponds to the reference. + template T resolve(DIRef Ref) const { + return DD->resolve(Ref); + } +}; + +} // end llvm namespace +#endif diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index 68111f1..0965d13 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -1013,7 +1013,7 @@ raw_ostream &operator<<(raw_ostream &OS, const MCFixup &AF) { } #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) -void MCFragment::dump() { +void MCFragment::dump() const { raw_ostream &OS = llvm::errs(); OS << "<"; @@ -1124,20 +1124,20 @@ void MCFragment::dump() { OS << ">"; } -void MCSectionData::dump() { +void MCSectionData::dump() const { raw_ostream &OS = llvm::errs(); OS << "dump(); } OS << "]>"; } -void MCSymbolData::dump() { +void MCSymbolData::dump() const { raw_ostream &OS = llvm::errs(); OS << ""; } -void MCAssembler::dump() { +void MCAssembler::dump() const { raw_ostream &OS = llvm::errs(); OS << "dump(); } OS << "],\n"; OS << " Symbols:["; - for (symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { + for (const_symbol_iterator it = symbol_begin(), ie = symbol_end(); it != ie; ++it) { if (it != symbol_begin()) OS << ",\n "; it->dump(); } diff --git a/lib/MC/MCDisassembler/CMakeLists.txt b/lib/MC/MCDisassembler/CMakeLists.txt index 5195b9e..75f6493 100644 --- a/lib/MC/MCDisassembler/CMakeLists.txt +++ b/lib/MC/MCDisassembler/CMakeLists.txt @@ -1,3 +1,27 @@ add_llvm_library(LLVMMCDisassembler Disassembler.cpp ) + +target_link_libraries(LLVMMCDisassembler + LLVMMC + LLVMMCParser + LLVMSupport + LLVMTarget + ) + +foreach(t ${LLVM_TARGETS_TO_BUILD}) + set(td ${LLVM_MAIN_SRC_DIR}/lib/Target/${t}) + if(EXISTS ${td}/TargetInfo/CMakeLists.txt) + target_link_libraries(LLVMMCDisassembler "LLVM${t}Info") + endif() + if(EXISTS ${td}/MCTargetDesc/CMakeLists.txt) + target_link_libraries(LLVMMCDisassembler "LLVM${t}Desc") + endif() + if(EXISTS ${td}/AsmParser/CMakeLists.txt) + target_link_libraries(LLVMMCDisassembler "LLVM${t}AsmParser") + endif() + if(EXISTS ${td}/Disassembler/CMakeLists.txt) + target_link_libraries(LLVMMCDisassembler "LLVM${t}Disassembler") + endif() +endforeach(t) + diff --git a/lib/MC/MachObjectWriter.cpp b/lib/MC/MachObjectWriter.cpp index 8234aff..19531ed 100644 --- a/lib/MC/MachObjectWriter.cpp +++ b/lib/MC/MachObjectWriter.cpp @@ -446,6 +446,7 @@ void MachObjectWriter::BindIndirectSymbols(MCAssembler &Asm) { } } +#if !ORDER_INDIRECT_SYMBOLS_BY_SECTION // Bind non lazy symbol pointers first. unsigned IndirectIndex = 0; for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), @@ -484,6 +485,46 @@ void MachObjectWriter::BindIndirectSymbols(MCAssembler &Asm) { if (Created) Entry.setFlags(Entry.getFlags() | 0x0001); } +#else // ORDER_INDIRECT_SYMBOLS_BY_SECTION + // sort indirect symbols by section + for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), + ie = Asm.indirect_symbol_end(); it != ie; ++it) { + // track their sections by order of appearance + IndirectSymbolSections.insert(it->SectionData); + IndirectSymbolMap[it->SectionData].push_back(it->Symbol); + } + // process indirect symbols by section + unsigned offset = 0; // running total of indirect symbol index offset + IndirectSymbolSection_set_type::const_iterator + i(IndirectSymbolSections.begin()), e(IndirectSymbolSections.end()); + for ( ; i!=e; ++i) { + const IndirectSymbol_list_type& b(IndirectSymbolMap.find(*i)->second); + IndirectSymbol_list_type::const_iterator bi(b.begin()), be(b.end()); + const MCSectionMachO& Section(cast((*i)->getSection())); + switch (Section.getType()) { + default: break; + case MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS: { + for ( ; bi!=be; ++bi) + Asm.getOrCreateSymbolData(**bi); + break; + } + case MCSectionMachO::S_LAZY_SYMBOL_POINTERS: // fall-through + case MCSectionMachO::S_SYMBOL_STUBS: { + for ( ; bi!=be; ++bi) { + // Set the symbol type to undefined lazy, but only on construction. + // FIXME: Do not hardcode. + bool Created; + MCSymbolData &Entry(Asm.getOrCreateSymbolData(**bi, &Created)); + if (Created) + Entry.setFlags(Entry.getFlags() | 0x0001); + } // end for + break; + } + } // end switch(sectionType) + IndirectSymBase.insert(std::make_pair(*i, offset)); + offset += b.size(); + } +#endif // ORDER_INDIRECT_SYMBOLS_BY_SECTION } /// ComputeSymbolTable - Compute the symbol table data @@ -914,26 +955,44 @@ void MachObjectWriter::WriteObject(MCAssembler &Asm, // Write the symbol table data, if used. if (NumSymbols) { // Write the indirect symbol entries. +#if ORDER_INDIRECT_SYMBOLS_BY_SECTION + for (IndirectSymbolSection_set_type::const_iterator + si(IndirectSymbolSections.begin()), se(IndirectSymbolSections.end()); + si != se; ++si) { + const IndirectSymbol_list_type& l(IndirectSymbolMap.find(*si)->second); + for (IndirectSymbol_list_type::const_iterator + it(l.begin()), ie(l.end()); it != ie; ++it) +#else for (MCAssembler::const_indirect_symbol_iterator it = Asm.indirect_symbol_begin(), - ie = Asm.indirect_symbol_end(); it != ie; ++it) { + ie = Asm.indirect_symbol_end(); it != ie; ++it) +#endif + { // Indirect symbols in the non lazy symbol pointer section have some // special handling. const MCSectionMachO &Section = +#if ORDER_INDIRECT_SYMBOLS_BY_SECTION + static_cast((*si)->getSection()); + const MCSymbol* Sym = *it; +#else static_cast(it->SectionData->getSection()); + const MCSymbol* Sym = it->Symbol; +#endif if (Section.getType() == MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS) { // If this symbol is defined and internal, mark it as such. - if (it->Symbol->isDefined() && - !Asm.getSymbolData(*it->Symbol).isExternal()) { + if (Sym->isDefined() && + !Asm.getSymbolData(*Sym).isExternal()) { uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL; - if (it->Symbol->isAbsolute()) + if (Sym->isAbsolute()) Flags |= MachO::INDIRECT_SYMBOL_ABS; Write32(Flags); continue; } } - - Write32(Asm.getSymbolData(*it->Symbol).getIndex()); + Write32(Asm.getSymbolData(*Sym).getIndex()); +#if ORDER_INDIRECT_SYMBOLS_BY_SECTION + } +#endif } // FIXME: Check that offsets match computed ones. diff --git a/lib/Support/Atomic.cpp b/lib/Support/Atomic.cpp index 9559ad7..389a8e9 100644 --- a/lib/Support/Atomic.cpp +++ b/lib/Support/Atomic.cpp @@ -21,21 +21,68 @@ using namespace llvm; #undef MemoryFence #endif +// USE_DARWIN_ATOMICS conditionally defined in Atomics.h +#if USE_DARWIN_ATOMICS +#include +// __APPLE__ should take precedence over __GNUC__ +// sys::cas_flag is int32_t from Support/Atomic.h, so use '32' variants +// prototypes lack the 'volatile' qualifier, so we need to cast them away +template +static inline +T* vcast(volatile T* ptr) { return const_cast(ptr); } + +// note on weakly-ordered architectures (PPC): +/** +DESCRIPTION + These functions are thread and multiprocessor safe. For each function, + there is a version that does and another that does not incorporate a + memory barrier. Barriers strictly order memory access on a weakly- + ordered architecture such as PPC. All loads and stores executed in + sequential program order before the barrier will complete before any load + or store executed after the barrier. On a uniprocessor, the barrier + operation is typically a nop. On a multiprocessor, the barrier can be + quite expensive. + + Most code will want to use the barrier functions to insure that memory + shared between threads is properly synchronized. For example, if you + want to initialize a shared data structure and then atomically increment + a variable to indicate that the initialization is complete, then you MUST + use OSAtomicIncrement32Barrier() to ensure that the stores to your data + structure complete before the atomic add. Likewise, the consumer of that + data structure MUST use OSAtomicDecrement32Barrier(), in order to ensure + that their loads of the structure are not executed before the atomic + decrement. On the other hand, if you are simply incrementing a global + counter, then it is safe and potentially much faster to use OSAtomicIn- + crement32(). If you are unsure which version to use, prefer the barrier + variants as they are safer. + +RETURN VALUES + The arithmetic and logical operations return the new value, after the + operation has been performed. The compare-and-swap operations return + true if the comparison was equal, ie if the swap occured. The bit test + and set/clear operations return the original value of the bit. + + -- man 3 atomic (BSD Library Functions Manual) +**/ +#endif + #if defined(__GNUC__) || (defined(__IBMCPP__) && __IBMCPP__ >= 1210) +#if !USE_DARWIN_ATOMICS #define GNU_ATOMICS #endif +#endif void sys::MemoryFence() { #if LLVM_HAS_ATOMICS == 0 return; -#else -# if defined(GNU_ATOMICS) +#elif defined(GNU_ATOMICS) __sync_synchronize(); -# elif defined(_MSC_VER) +#elif USE_DARWIN_ATOMICS + OSMemoryBarrier(); +#elif defined(_MSC_VER) MemoryBarrier(); -# else +#else # error No memory fence implementation for your platform! -# endif #endif } @@ -49,6 +96,18 @@ sys::cas_flag sys::CompareAndSwap(volatile sys::cas_flag* ptr, return result; #elif defined(GNU_ATOMICS) return __sync_val_compare_and_swap(ptr, old_value, new_value); +/** +These builtins perform an atomic compare and swap. +That is, if the current value of *ptr is oldval, then write newval into *ptr. +The bool version returns true if the comparison is successful and newval +was written. The val version returns the contents of *ptr before the operation. + -- http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html +**/ +#elif USE_DARWIN_ATOMICS + const sys::cas_flag prev = *ptr; + // returns new value, but we don't want it + OSAtomicCompareAndSwap32Barrier(old_value, new_value, vcast(ptr)); + return prev; // return the previous value at *ptr #elif defined(_MSC_VER) return InterlockedCompareExchange(ptr, new_value, old_value); #else @@ -62,6 +121,8 @@ sys::cas_flag sys::AtomicIncrement(volatile sys::cas_flag* ptr) { return *ptr; #elif defined(GNU_ATOMICS) return __sync_add_and_fetch(ptr, 1); +#elif USE_DARWIN_ATOMICS + return OSAtomicIncrement32Barrier(vcast(ptr)); // return new value #elif defined(_MSC_VER) return InterlockedIncrement(ptr); #else @@ -75,6 +136,8 @@ sys::cas_flag sys::AtomicDecrement(volatile sys::cas_flag* ptr) { return *ptr; #elif defined(GNU_ATOMICS) return __sync_sub_and_fetch(ptr, 1); +#elif USE_DARWIN_ATOMICS + return OSAtomicDecrement32Barrier(vcast(ptr)); // return new value #elif defined(_MSC_VER) return InterlockedDecrement(ptr); #else @@ -88,6 +151,8 @@ sys::cas_flag sys::AtomicAdd(volatile sys::cas_flag* ptr, sys::cas_flag val) { return *ptr; #elif defined(GNU_ATOMICS) return __sync_add_and_fetch(ptr, val); +#elif USE_DARWIN_ATOMICS + return OSAtomicAdd32Barrier(val, vcast(ptr)); // return new value #elif defined(_MSC_VER) return InterlockedExchangeAdd(ptr, val) + val; #else diff --git a/lib/Support/CMakeLists.txt b/lib/Support/CMakeLists.txt index 3aecf3f..f175add 100644 --- a/lib/Support/CMakeLists.txt +++ b/lib/Support/CMakeLists.txt @@ -22,6 +22,7 @@ add_llvm_library(LLVMSupport FileUtilities.cpp FileOutputBuffer.cpp FoldingSet.cpp + Format.cpp FormattedStream.cpp GraphWriter.cpp Hashing.cpp diff --git a/lib/Support/CommandLine.cpp b/lib/Support/CommandLine.cpp index 44a88d8..79f8ec0 100644 --- a/lib/Support/CommandLine.cpp +++ b/lib/Support/CommandLine.cpp @@ -539,13 +539,14 @@ static size_t parseBackslash(StringRef Src, size_t I, SmallString<128> &Token) { return I - 1; } +enum StateEnum { INIT, UNQUOTED, QUOTED }; void cl::TokenizeWindowsCommandLine(StringRef Src, StringSaver &Saver, SmallVectorImpl &NewArgv) { SmallString<128> Token; // This is a small state machine to consume characters until it reaches the // end of the source string. - enum { INIT, UNQUOTED, QUOTED } State = INIT; + StateEnum State = INIT; for (size_t I = 0, E = Src.size(); I != E; ++I) { // INIT state indicates that the current input index is at the start of // the string or between tokens. diff --git a/lib/Support/Format.cpp b/lib/Support/Format.cpp new file mode 100644 index 0000000..ff378d4 --- /dev/null +++ b/lib/Support/Format.cpp @@ -0,0 +1,34 @@ +//===-- llvm/Support/FormattedStream.cpp - Formatted streams ----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file contains the implementation of default_format_string. +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/Format.h" + +namespace llvm { + +#define FORMAT_DEC(T, fmt) const char default_format_string::dec[] = fmt; +#define FORMAT_HEX(T, fmt) const char default_format_string::hex[] = fmt; + +FORMAT_DEC(int, "%d") +FORMAT_HEX(int, "0x%x") +FORMAT_DEC(long, "%ld") +FORMAT_HEX(long, "0x%lx") +FORMAT_DEC(long long, "%lld") +FORMAT_HEX(long long, "0x%llx") +FORMAT_DEC(unsigned int, "%u") +FORMAT_HEX(unsigned int, "0x%x") +FORMAT_DEC(unsigned long, "%lu") +FORMAT_HEX(unsigned long, "0x%lx") +FORMAT_DEC(unsigned long long, "%llu") +FORMAT_HEX(unsigned long long, "0x%llx") + +} // end namespace llvm diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index c869b30..6b53bc8 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -508,6 +508,8 @@ void system_temp_directory(bool erasedOnReboot, SmallVectorImpl &result) { result.clear(); #ifdef __APPLE__ +// macros expected in +#if defined(_CS_DARWIN_USER_TEMP_DIR) && defined(_CS_DARWIN_USER_CACHE_DIR) // On Darwin, use DARWIN_USER_TEMP_DIR or DARWIN_USER_CACHE_DIR. int ConfName = erasedOnReboot? _CS_DARWIN_USER_TEMP_DIR : _CS_DARWIN_USER_CACHE_DIR; @@ -527,6 +529,7 @@ void system_temp_directory(bool erasedOnReboot, SmallVectorImpl &result) { result.clear(); } #endif +#endif // Check whether the temporary directory is specified by an environment // variable. diff --git a/lib/Support/Unix/Memory.inc b/lib/Support/Unix/Memory.inc index 58fda42..c431068 100644 --- a/lib/Support/Unix/Memory.inc +++ b/lib/Support/Unix/Memory.inc @@ -55,7 +55,8 @@ int getPosixProtectionFlags(unsigned Flags) { llvm::sys::Memory::MF_EXEC: return PROT_READ | PROT_WRITE | PROT_EXEC; case llvm::sys::Memory::MF_EXEC: -#if defined(__FreeBSD__) +#if defined(__FreeBSD__) || defined(__APPLE__) + // patch from Roman Divacky, bug 14278 // On PowerPC, having an executable page that has no read permission // can have unintended consequences. The function InvalidateInstruction- // Cache uses instructions dcbf and icbi, both of which are treated by diff --git a/lib/Support/Unix/Signals.inc b/lib/Support/Unix/Signals.inc index b4c78d6..f387329 100644 --- a/lib/Support/Unix/Signals.inc +++ b/lib/Support/Unix/Signals.inc @@ -333,7 +333,8 @@ static void PrintStackTraceSignalHandler(void *) { void llvm::sys::PrintStackTraceOnErrorSignal() { AddSignalHandler(PrintStackTraceSignalHandler, 0); -#if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES) +// some of these constants are not defined for darwin8 +#if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES) && defined(MACH_EXCEPTION_CODES) && defined(EXC_MASK_CRASH) // Environment variable to disable any kind of crash dialog. if (getenv("LLVM_DISABLE_CRASH_REPORT")) { mach_port_t self = mach_task_self(); diff --git a/lib/Target/ARM/ARMFastISel.cpp b/lib/Target/ARM/ARMFastISel.cpp index a4004f3..819d0fe 100644 --- a/lib/Target/ARM/ARMFastISel.cpp +++ b/lib/Target/ARM/ARMFastISel.cpp @@ -54,7 +54,7 @@ namespace { // All possible address modes, plus some. typedef struct Address { - enum { + enum BaseTypeEnum { RegBase, FrameIndexBase } BaseType; diff --git a/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp b/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp index fe83fe1..b297cd8 100644 --- a/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp +++ b/lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp @@ -177,6 +177,7 @@ class PPCAsmParser : public MCTargetAsmParser { MCAsmParser &Parser; const MCInstrInfo &MII; bool IsPPC64; + bool IsDarwin; MCAsmParser &getParser() const { return Parser; } MCAsmLexer &getLexer() const { return Parser.getLexer(); } @@ -185,6 +186,7 @@ class PPCAsmParser : public MCTargetAsmParser { bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); } bool isPPC64() const { return IsPPC64; } + bool isDarwin() const { return IsDarwin; } bool MatchRegisterName(const AsmToken &Tok, unsigned &RegNo, int64_t &IntVal); @@ -195,12 +197,14 @@ class PPCAsmParser : public MCTargetAsmParser { PPCMCExpr::VariantKind &Variant); const MCExpr *FixupVariantKind(const MCExpr *E); bool ParseExpression(const MCExpr *&EVal); + bool ParseDarwinExpression(const MCExpr *&EVal); bool ParseOperand(SmallVectorImpl &Operands); bool ParseDirectiveWord(unsigned Size, SMLoc L); bool ParseDirectiveTC(unsigned Size, SMLoc L); bool ParseDirectiveMachine(SMLoc L); + bool ParseDarwinDirectiveMachine(SMLoc L); bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, SmallVectorImpl &Operands, @@ -227,6 +231,7 @@ public: Triple TheTriple(STI.getTargetTriple()); IsPPC64 = (TheTriple.getArch() == Triple::ppc64 || TheTriple.getArch() == Triple::ppc64le); + IsDarwin = TheTriple.isMacOSX(); // Initialize the set of available features. setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); } @@ -1081,10 +1086,16 @@ FixupVariantKind(const MCExpr *E) { llvm_unreachable("Invalid expression kind!"); } -/// Parse an expression. This differs from the default "parseExpression" -/// in that it handles complex \code @l/@ha \endcode modifiers. +/// ParseExpression. This differs from the default "parseExpression" in that +/// it handles modifiers. bool PPCAsmParser:: ParseExpression(const MCExpr *&EVal) { + + if (isDarwin()) + return ParseDarwinExpression(EVal); + + // (ELF Platforms) + // Handle \code @l/@ha \endcode if (getParser().parseExpression(EVal)) return true; @@ -1098,6 +1109,55 @@ ParseExpression(const MCExpr *&EVal) { return false; } +/// ParseDarwinExpression. (MachO Platforms) +/// This differs from the default "parseExpression" in that it handles detection +/// of the \code hi16(), ha16() and lo16() \endcode modifiers. At present, +/// parseExpression() doesn't recognise the modifiers when in the Darwin/MachO +/// syntax form so it is done here. TODO: Determine if there is merit in arranging +/// for this to be done at a higher level. +bool PPCAsmParser:: +ParseDarwinExpression(const MCExpr *&EVal) { + PPCMCExpr::VariantKind Variant = PPCMCExpr::VK_PPC_None; + switch (getLexer().getKind()) { + default: + break; + case AsmToken::Identifier: + // Compiler-generated Darwin identifiers begin with L,l,_ or "; thus + // something starting with any other char should be part of the + // asm syntax. If handwritten asm includes an identifier like lo16, + // then all bets are off - but no-one would do that, right? + StringRef poss = Parser.getTok().getString(); + if (poss.equals_lower("lo16")) { + Variant = PPCMCExpr::VK_PPC_LO; + } else if (poss.equals_lower("hi16")) { + Variant = PPCMCExpr::VK_PPC_HI; + } else if (poss.equals_lower("ha16")) { + Variant = PPCMCExpr::VK_PPC_HA; + } + if (Variant != PPCMCExpr::VK_PPC_None) { + Parser.Lex(); // Eat the xx16 + if (getLexer().isNot(AsmToken::LParen)) + return Error(Parser.getTok().getLoc(), "expected '('"); + Parser.Lex(); // Eat the '(' + } + break; + } + + if (getParser().parseExpression(EVal)) + return true; + + if (Variant != PPCMCExpr::VK_PPC_None) { + if (getLexer().isNot(AsmToken::RParen)) + return Error(Parser.getTok().getLoc(), "expected ')'"); + Parser.Lex(); // Eat the ')' + EVal = PPCMCExpr::Create(Variant, EVal, false, getParser().getContext()); + } + return false; +} + +/// ParseOperand +/// This handles registers in the form 'NN', '%rNN' for ELF platforms and +/// rNN for MachO. bool PPCAsmParser:: ParseOperand(SmallVectorImpl &Operands) { SMLoc S = Parser.getTok().getLoc(); @@ -1121,12 +1181,27 @@ ParseOperand(SmallVectorImpl &Operands) { } return Error(S, "invalid register name"); + case AsmToken::Identifier: + // Note that non-register-name identifiers from the compiler will begin + // with '_', 'L'/'l' or '"'. Of course, handwritten asm could include + // identifiers like r31foo - so we fall through in the event that parsing + // a register name fails. + if (isDarwin()) { + unsigned RegNo; + int64_t IntVal; + if (!MatchRegisterName(Parser.getTok(), RegNo, IntVal)) { + Parser.Lex(); // Eat the identifier token. + Op = PPCOperand::CreateImm(IntVal, S, E, isPPC64()); + Operands.push_back(Op); + return false; + } + } + // Fall-through to process non-register-name identifiers as expression. // All other expressions case AsmToken::LParen: case AsmToken::Plus: case AsmToken::Minus: case AsmToken::Integer: - case AsmToken::Identifier: case AsmToken::Dot: case AsmToken::Dollar: if (!ParseExpression(EVal)) @@ -1177,11 +1252,25 @@ ParseOperand(SmallVectorImpl &Operands) { break; case AsmToken::Integer: - if (getParser().parseAbsoluteExpression(IntVal) || + if (!isDarwin()) { + if (getParser().parseAbsoluteExpression(IntVal) || IntVal < 0 || IntVal > 31) return Error(S, "invalid register number"); + } else { + return Error(S, "unexpected integer value"); + } break; + case AsmToken::Identifier: + if (isDarwin()) { + unsigned RegNo; + if (!MatchRegisterName(Parser.getTok(), RegNo, IntVal)) { + Parser.Lex(); // Eat the identifier token. + break; + } + } + // Fall-through.. + default: return Error(S, "invalid memory operand"); } @@ -1261,14 +1350,19 @@ ParseInstruction(ParseInstructionInfo &Info, StringRef Name, SMLoc NameLoc, /// ParseDirective parses the PPC specific directives bool PPCAsmParser::ParseDirective(AsmToken DirectiveID) { StringRef IDVal = DirectiveID.getIdentifier(); - if (IDVal == ".word") - return ParseDirectiveWord(2, DirectiveID.getLoc()); - if (IDVal == ".llong") - return ParseDirectiveWord(8, DirectiveID.getLoc()); - if (IDVal == ".tc") - return ParseDirectiveTC(isPPC64()? 8 : 4, DirectiveID.getLoc()); - if (IDVal == ".machine") - return ParseDirectiveMachine(DirectiveID.getLoc()); + if (!isDarwin()) { + if (IDVal == ".word") + return ParseDirectiveWord(2, DirectiveID.getLoc()); + if (IDVal == ".llong") + return ParseDirectiveWord(8, DirectiveID.getLoc()); + if (IDVal == ".tc") + return ParseDirectiveTC(isPPC64()? 8 : 4, DirectiveID.getLoc()); + if (IDVal == ".machine") + return ParseDirectiveMachine(DirectiveID.getLoc()); + } else { + if (IDVal == ".machine") + return ParseDarwinDirectiveMachine(DirectiveID.getLoc()); + } return true; } @@ -1314,7 +1408,7 @@ bool PPCAsmParser::ParseDirectiveTC(unsigned Size, SMLoc L) { return ParseDirectiveWord(Size, L); } -/// ParseDirectiveMachine +/// ParseDirectiveMachine (ELF platforms) /// ::= .machine [ cpu | "push" | "pop" ] bool PPCAsmParser::ParseDirectiveMachine(SMLoc L) { if (getLexer().isNot(AsmToken::Identifier) && @@ -1338,6 +1432,33 @@ bool PPCAsmParser::ParseDirectiveMachine(SMLoc L) { return false; } +/// ParseDarwinDirectiveMachine (Mach-o platforms) +/// ::= .machine cpu-identifier +bool PPCAsmParser::ParseDarwinDirectiveMachine(SMLoc L) { + if (getLexer().isNot(AsmToken::Identifier) && + getLexer().isNot(AsmToken::String)) + return Error(L, "unexpected token in directive"); + + StringRef CPU = Parser.getTok().getIdentifier(); + Parser.Lex(); + + // FIXME: this is only the 'default' set of cpu variants. + // However we don't act on this information at present, this is simply + // allowing parsing to proceed with minimal sanity checking. + if (CPU != "ppc7400" && CPU != "ppc" && CPU != "ppc64") + return Error(L, "unrecognized cpu type"); + + if (isPPC64() && (CPU == "ppc7400" || CPU == "ppc")) + return Error(L, "wrong cpu type specified for 64bit"); + if (!isPPC64() && CPU == "ppc64") + return Error(L, "wrong cpu type specified for 32bit"); + + if (getLexer().isNot(AsmToken::EndOfStatement)) + return Error(L, "unexpected token in directive"); + + return false; +} + /// Force static initialization. extern "C" void LLVMInitializePowerPCAsmParser() { RegisterMCAsmParser A(ThePPC32Target); diff --git a/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp b/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp index 1d9c064..32c7b5d 100644 --- a/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp +++ b/lib/Target/PowerPC/MCTargetDesc/PPCMCAsmInfo.cpp @@ -34,6 +34,7 @@ PPCMCAsmInfoDarwin::PPCMCAsmInfoDarwin(bool is64Bit) { AssemblerDialect = 1; // New-Style mnemonics. SupportsDebugInformation= true; // Debug information. + // for darwin, call the other constructor with Triple argument, below } PPCMCAsmInfoDarwin::PPCMCAsmInfoDarwin(bool is64Bit, const Triple& T) { diff --git a/lib/Target/PowerPC/PPCAsmPrinter.cpp b/lib/Target/PowerPC/PPCAsmPrinter.cpp index 2d92a11..b79f851 100644 --- a/lib/Target/PowerPC/PPCAsmPrinter.cpp +++ b/lib/Target/PowerPC/PPCAsmPrinter.cpp @@ -889,6 +889,7 @@ static MCSymbol *GetLazyPtr(MCSymbol *Sym, MCContext &Ctx) { return Ctx.GetOrCreateSymbol(NoStub + "$lazy_ptr"); } +// 'L' needed to designate label as local static MCSymbol *GetAnonSym(MCSymbol *Sym, MCContext &Ctx) { // Add $tmp suffix to $stub, yielding $stub$tmp. return Ctx.GetOrCreateSymbol(Sym->getName() + "$tmp"); @@ -906,6 +907,7 @@ EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) { const MCSection *LSPSection = TLOFMacho.getLazySymbolPointerSection(); // Output stubs for dynamically-linked functions + // see http://developer.apple.com/library/mac/#documentation/developertools/reference/assembler/050-PowerPC_Addressing_Modes_and_Assembler_Instructions/ppc_instructions.html if (TM.getRelocationModel() == Reloc::PIC_) { const MCSection *StubSection = OutContext.getMachOSection("__TEXT", "__picsymbolstub1", @@ -932,10 +934,12 @@ EmitFunctionStubs(const MachineModuleInfoMachO::SymbolListTy &Stubs) { // mflr r0 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R0)); // bcl 20, 31, AnonSymbol + // unconditional, but doesn't push link register onto stack OutStreamer.EmitInstruction(MCInstBuilder(PPC::BCLalways).addExpr(Anon)); OutStreamer.EmitLabel(AnonSymbol); - // mflr r11 + // mflr r11 ; move LR to r11 OutStreamer.EmitInstruction(MCInstBuilder(PPC::MFLR).addReg(PPC::R11)); + // load address one half at a time // addis r11, r11, ha16(LazyPtr - AnonSymbol) const MCExpr *SubHa16 = PPCMCExpr::CreateHa(Sub, isDarwin, OutContext); OutStreamer.EmitInstruction(MCInstBuilder(PPC::ADDIS) diff --git a/lib/Target/PowerPC/PPCFastISel.cpp b/lib/Target/PowerPC/PPCFastISel.cpp index 4e3b0b8..8913326 100644 --- a/lib/Target/PowerPC/PPCFastISel.cpp +++ b/lib/Target/PowerPC/PPCFastISel.cpp @@ -61,7 +61,7 @@ using namespace llvm; namespace { typedef struct Address { - enum { + enum BaseTypeEnum { RegBase, FrameIndexBase } BaseType; diff --git a/lib/Target/PowerPC/PPCISelLowering.cpp b/lib/Target/PowerPC/PPCISelLowering.cpp index 25a7ca7..e447381 100644 --- a/lib/Target/PowerPC/PPCISelLowering.cpp +++ b/lib/Target/PowerPC/PPCISelLowering.cpp @@ -2611,6 +2611,7 @@ PPCTargetLowering::LowerFormalArguments_Darwin( SmallVector MemOps; unsigned nAltivecParamsAtEnd = 0; + // for Darwin, reported as: http://llvm.org/bugs/show_bug.cgi?id=15821 Function::const_arg_iterator FuncArg = MF.getFunction()->arg_begin(); unsigned CurArgIdx = 0; for (unsigned ArgNo = 0, e = Ins.size(); ArgNo != e; ++ArgNo) { diff --git a/lib/Target/PowerPC/PPCRegisterInfo.cpp b/lib/Target/PowerPC/PPCRegisterInfo.cpp index 19ccbfc..a08471f3 100644 --- a/lib/Target/PowerPC/PPCRegisterInfo.cpp +++ b/lib/Target/PowerPC/PPCRegisterInfo.cpp @@ -284,8 +284,8 @@ void PPCRegisterInfo::lowerDynamicAlloc(MachineBasicBlock::iterator II) const { unsigned Reg = MF.getRegInfo().createVirtualRegister(LP64 ? G8RC : GPRC); if (MaxAlign < TargetAlign && isInt<16>(FrameSize)) { - BuildMI(MBB, II, dl, TII.get(PPC::ADDI), Reg) - .addReg(PPC::R31) + BuildMI(MBB, II, dl, TII.get(LP64 ? PPC::ADDI8 : PPC::ADDI), Reg) + .addReg(getFrameRegister(MF)) .addImm(FrameSize); } else if (LP64) { BuildMI(MBB, II, dl, TII.get(PPC::LD), Reg) diff --git a/lib/Target/PowerPC/PPCSubtarget.cpp b/lib/Target/PowerPC/PPCSubtarget.cpp index 7231ab1..a26b583 100644 --- a/lib/Target/PowerPC/PPCSubtarget.cpp +++ b/lib/Target/PowerPC/PPCSubtarget.cpp @@ -156,7 +156,8 @@ bool PPCSubtarget::hasLazyResolverStub(const GlobalValue *GV, return false; // If symbol visibility is hidden, the extra load is not needed if // the symbol is definitely defined in the current translation unit. - bool isDecl = GV->isDeclaration() && !GV->isMaterializable(); + bool isDecl = (GV->isDeclaration() && !GV->isMaterializable()) || + GV->hasAvailableExternallyLinkage(); // fixes PR14579 if (GV->hasHiddenVisibility() && !isDecl && !GV->hasCommonLinkage()) return false; return GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() || diff --git a/lib/Target/X86/MCTargetDesc/X86BaseInfo.h b/lib/Target/X86/MCTargetDesc/X86BaseInfo.h index 1ef9814..9ddd104 100644 --- a/lib/Target/X86/MCTargetDesc/X86BaseInfo.h +++ b/lib/Target/X86/MCTargetDesc/X86BaseInfo.h @@ -29,7 +29,7 @@ namespace X86 { // a 5 operand sequence in the form: // [BaseReg, ScaleAmt, IndexReg, Disp, Segment] // These enums help decode this. - enum { + enum AddrOpEnum { AddrBaseReg = 0, AddrScaleAmt = 1, AddrIndexReg = 2, @@ -216,7 +216,7 @@ namespace X86II { MO_SECREL }; - enum { + enum InstrEncodingEnum { //===------------------------------------------------------------------===// // Instruction encodings. These are the standard/most common forms for X86 // instructions. diff --git a/lib/Target/X86/MCTargetDesc/X86MCAsmInfo.cpp b/lib/Target/X86/MCTargetDesc/X86MCAsmInfo.cpp index 8d2b595..37f79fa 100644 --- a/lib/Target/X86/MCTargetDesc/X86MCAsmInfo.cpp +++ b/lib/Target/X86/MCTargetDesc/X86MCAsmInfo.cpp @@ -66,6 +66,12 @@ X86MCAsmInfoDarwin::X86MCAsmInfoDarwin(const Triple &T) { // Exceptions handling ExceptionsType = ExceptionHandling::DwarfCFI; + // old assembler lacks some directives + // FIXME: this should really be a check on the assembler characteristics + // rather than OS version + if (T.isMacOSX() && T.isMacOSXVersionLT(10, 6)) + HasWeakDefCanBeHiddenDirective = false; + // FIXME: this should not depend on the target OS version, but on the ld64 // version in use. From at least >= ld64-97.17 (Xcode 3.2.6) the abs-ified // FDE relocs may be used. diff --git a/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.h b/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.h index 41ae435..442bcbd 100644 --- a/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.h +++ b/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.h @@ -35,7 +35,7 @@ extern Target TheX86_32Target, TheX86_64Target; /// DWARFFlavour - Flavour of dwarf regnumbers /// namespace DWARFFlavour { - enum { + enum DwarfFlavorEnum { X86_64 = 0, X86_32_DarwinEH = 1, X86_32_Generic = 2 }; } @@ -43,7 +43,7 @@ namespace DWARFFlavour { /// N86 namespace - Native X86 register numbers /// namespace N86 { - enum { + enum NativeRegisterEnum { EAX = 0, ECX = 1, EDX = 2, EBX = 3, ESP = 4, EBP = 5, ESI = 6, EDI = 7 }; } diff --git a/lib/Target/X86/X86ISelDAGToDAG.cpp b/lib/Target/X86/X86ISelDAGToDAG.cpp index 36d1690..073ecf8 100644 --- a/lib/Target/X86/X86ISelDAGToDAG.cpp +++ b/lib/Target/X86/X86ISelDAGToDAG.cpp @@ -47,7 +47,7 @@ namespace { /// SDValue's instead of register numbers for the leaves of the matched /// tree. struct X86ISelAddressMode { - enum { + enum BaseTypeEnum { RegBase, FrameIndexBase } BaseType; diff --git a/lib/Target/X86/X86InstrBuilder.h b/lib/Target/X86/X86InstrBuilder.h index aaef4a4..8e78622 100644 --- a/lib/Target/X86/X86InstrBuilder.h +++ b/lib/Target/X86/X86InstrBuilder.h @@ -35,7 +35,7 @@ namespace llvm { /// with BP or SP and Disp being offsetted accordingly. The displacement may /// also include the offset of a global value. struct X86AddressMode { - enum { + enum BaseTypeEnum { RegBase, FrameIndexBase } BaseType; diff --git a/lib/Target/X86/X86InstrInfo.cpp b/lib/Target/X86/X86InstrInfo.cpp index 2461773..400ae05 100644 --- a/lib/Target/X86/X86InstrInfo.cpp +++ b/lib/Target/X86/X86InstrInfo.cpp @@ -4911,6 +4911,13 @@ bool X86InstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2, return true; } +// moved enum to non-local + enum FuseKindEnum { + FuseTest, + FuseCmp, + FuseInc + }; + bool X86InstrInfo::shouldScheduleAdjacent(MachineInstr* First, MachineInstr *Second) const { // Check if this processor supports macro-fusion. Since this is a minor @@ -4919,11 +4926,7 @@ bool X86InstrInfo::shouldScheduleAdjacent(MachineInstr* First, if (!TM.getSubtarget().hasAVX()) return false; - enum { - FuseTest, - FuseCmp, - FuseInc - } FuseKind; + FuseKindEnum FuseKind; switch(Second->getOpcode()) { default: diff --git a/lib/Transforms/Hello/CMakeLists.txt b/lib/Transforms/Hello/CMakeLists.txt index 917b745..866b1f7 100644 --- a/lib/Transforms/Hello/CMakeLists.txt +++ b/lib/Transforms/Hello/CMakeLists.txt @@ -1,3 +1,5 @@ +# patch: this should be built as a bundle/module/plug-in +set(MODULE TRUE) add_llvm_loadable_module( LLVMHello Hello.cpp ) diff --git a/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/lib/Transforms/Instrumentation/MemorySanitizer.cpp index d547adc..233ffad 100644 --- a/lib/Transforms/Instrumentation/MemorySanitizer.cpp +++ b/lib/Transforms/Instrumentation/MemorySanitizer.cpp @@ -219,8 +219,15 @@ class MemorySanitizer : public FunctionPass { private: void initializeCallbacks(Module &M); +// workaround accessibility bug in g++-4.0 +#if !__GNUC_PREREQ(4, 2) + public: +#endif /// \brief Track origins (allocation points) of uninitialized values. bool TrackOrigins; +#if !__GNUC_PREREQ(4, 2) + private: +#endif DataLayout *TD; LLVMContext *C; diff --git a/lib/Transforms/Scalar/ScalarReplAggregates.cpp b/lib/Transforms/Scalar/ScalarReplAggregates.cpp index 57b290e..a82de84 100644 --- a/lib/Transforms/Scalar/ScalarReplAggregates.cpp +++ b/lib/Transforms/Scalar/ScalarReplAggregates.cpp @@ -267,7 +267,7 @@ class ConvertToScalarInfo { /// ScalarKind - Tracks the kind of alloca being considered for promotion, /// computed based on the uses of the alloca rather than the LLVM type system. - enum { + enum ScalarKindEnum { Unknown, // Accesses via GEPs that are consistent with element access of a vector diff --git a/test/CodeGen/PowerPC/hello-reloc.ll b/test/CodeGen/PowerPC/hello-reloc.ll new file mode 100644 index 0000000..0fda921 --- /dev/null +++ b/test/CodeGen/PowerPC/hello-reloc.ll @@ -0,0 +1,281 @@ +; This tests mach-O/PPC relocation entries. +; This test is paired with test/CodeGen/PowerPC/hello-reloc.s, +; which tests llvm-mc. + +; RUN: llc -filetype=asm -relocation-model=pic -mcpu=g4 -mtriple=powerpc-apple-darwin8 %s -o - | tee %t1 | FileCheck -check-prefix=DARWIN-G4-ASM %s +; RUN-WORKS-BUT-SKIPPING: llc -filetype=obj -relocation-model=pic -mcpu=g4 -mtriple=powerpc-apple-darwin8 %s -o - | tee %t2 | macho-dump | tee %t3 | FileCheck -check-prefix=DARWIN-G4-DUMP %s + +; FIXME: validating .s->.o requires darwin asm syntax support in PPCAsmParser +; RUN-XFAIL: llvm-mc -relocation-model=pic -mcpu=g4 -triple=powerpc-apple-darwin8 %t1 -o - | tee %t4 | macho-dump | tee %t5 | FileCheck -check-prefix=DARWIN-G4-DUMP %s +; RUN-XFAIL: diff -u %t2 %t4 || diff -u %t3 %t5 + +; ModuleID = 'hello-puts.c' +; compiled with clang (-fno-common -DPIC -femit-all-decls) from: +; extern int puts(const char*); +; int main(int argc, char* argv[]) { puts("Hello, world!"); return 0; } + +target datalayout = "E-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-v128:128:128-n32" +target triple = "powerpc-apple-macosx10.4.0" + +@.str = private unnamed_addr constant [14 x i8] c"Hello, world!\00", align 1 + +; Function Attrs: nounwind +define i32 @main(i32 %argc, i8** %argv) #0 { +entry: + %retval = alloca i32, align 4 + %argc.addr = alloca i32, align 4 + %argv.addr = alloca i8**, align 4 + store i32 0, i32* %retval + store i32 %argc, i32* %argc.addr, align 4 + store i8** %argv, i8*** %argv.addr, align 4 + %call = call i32 @puts(i8* getelementptr inbounds ([14 x i8]* @.str, i32 0, i32 0)) + ret i32 0 +} + +declare i32 @puts(i8*) #1 + +attributes #0 = { nounwind "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "ssp-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf"="true" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "ssp-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" } + +; DARWIN-G4-ASM: .machine ppc7400 +; DARWIN-G4-ASM: .section __TEXT,__textcoal_nt,coalesced,pure_instructions +; DARWIN-G4-ASM: .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32 +; DARWIN-G4-ASM: .section __TEXT,__text,regular,pure_instructions +; DARWIN-G4-ASM: .globl _main +; DARWIN-G4-ASM: .align 4 +; DARWIN-G4-ASM:_main: ; @main +; DARWIN-G4-ASM:; BB#0: ; %entry +; DARWIN-G4-ASM: mflr r0 +; DARWIN-G4-ASM: stw r31, -4(r1) +; DARWIN-G4-ASM: stw r0, 8(r1) +; DARWIN-G4-ASM: stwu r1, -80(r1) +; DARWIN-G4-ASM: bl L0$pb +; DARWIN-G4-ASM:L0$pb: +; DARWIN-G4-ASM: mr r31, r1 +; DARWIN-G4-ASM: li [[REGA:r[0-9]+]], 0 +; DARWIN-G4-ASM: mflr [[REGC:r[0-9]+]] +; DARWIN-G4-ASM: stw [[REGB:r[0-9]+]], 68(r31) +; DARWIN-G4-ASM: stw [[REGA]], 72(r31) +; DARWIN-G4-ASM: stw r4, 64(r31) +; DARWIN-G4-ASM: addis [[REGC]], [[REGC]], ha16(L_.str-L0$pb) +; DARWIN-G4-ASM: la [[REGB]], lo16(L_.str-L0$pb)([[REGC]]) +; DARWIN-G4-ASM: bl L_puts$stub +; DARWIN-G4-ASM: li [[REGB]], 0 +; DARWIN-G4-ASM: addi r1, r1, 80 +; DARWIN-G4-ASM: lwz r0, 8(r1) +; DARWIN-G4-ASM: lwz r31, -4(r1) +; DARWIN-G4-ASM: mtlr r0 +; DARWIN-G4-ASM: blr +; DARWIN-G4-ASM: .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32 +; DARWIN-G4-ASM: .align 4 +; DARWIN-G4-ASM:L_puts$stub: +; DARWIN-G4-ASM: .indirect_symbol _puts +; DARWIN-G4-ASM: mflr r0 +; DARWIN-G4-ASM: bcl 20, 31, L_puts$stub$tmp +; DARWIN-G4-ASM:L_puts$stub$tmp: +; DARWIN-G4-ASM: mflr [[REGD:r[0-9]+]] +; DARWIN-G4-ASM: addis [[REGD]], [[REGD]], ha16(L_puts$lazy_ptr-L_puts$stub$tmp) +; DARWIN-G4-ASM: mtlr r0 +; DARWIN-G4-ASM: lwzu [[REGE:r[0-9]+]], lo16(L_puts$lazy_ptr-L_puts$stub$tmp)([[REGD]]) +; DARWIN-G4-ASM: mtctr [[REGE]] +; DARWIN-G4-ASM: bctr +; DARWIN-G4-ASM: .section __DATA,__la_symbol_ptr,lazy_symbol_pointers +; DARWIN-G4-ASM:L_puts$lazy_ptr: +; DARWIN-G4-ASM: .indirect_symbol _puts +; DARWIN-G4-ASM: .long dyld_stub_binding_helper +; DARWIN-G4-ASM:.subsections_via_symbols +; DARWIN-G4-ASM: .section __TEXT,__cstring,cstring_literals +; DARWIN-G4-ASM:L_.str: ; @.str +; DARWIN-G4-ASM: .asciz "Hello, world!" + +; DARWIN-G4-DUMP: ('cputype', 18) +; DARWIN-G4-DUMP: ('cpusubtype', 0) +; DARWIN-G4-DUMP: ('filetype', 1) +; DARWIN-G4-DUMP: ('num_load_commands', 3) +; DARWIN-G4-DUMP: ('load_commands_size', 500) +; DARWIN-G4-DUMP: ('flag', 8192) +; DARWIN-G4-DUMP: ('load_commands', [ +; DARWIN-G4-DUMP: # Load Command 0 +; DARWIN-G4-DUMP: (('command', 1) +; DARWIN-G4-DUMP: ('size', 396) +; DARWIN-G4-DUMP: ('segment_name', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +; DARWIN-G4-DUMP: ('vm_addr', 0) +; DARWIN-G4-DUMP: ('vm_size', 130) +; DARWIN-G4-DUMP: ('file_offset', 528) +; DARWIN-G4-DUMP: ('file_size', 130) +; DARWIN-G4-DUMP: ('maxprot', 7) +; DARWIN-G4-DUMP: ('initprot', 7) +; DARWIN-G4-DUMP: ('num_sections', 5) +; DARWIN-G4-DUMP: ('flags', 0) +; DARWIN-G4-DUMP: ('sections', [ +; DARWIN-G4-DUMP: # Section 0 +; DARWIN-G4-DUMP: (('section_name', '__text\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +; DARWIN-G4-DUMP: ('segment_name', '__TEXT\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +; DARWIN-G4-DUMP: ('address', 0) +; DARWIN-G4-DUMP: ('size', 80) +; DARWIN-G4-DUMP: ('offset', 528) +; DARWIN-G4-DUMP: ('alignment', 4) +; DARWIN-G4-DUMP: ('reloc_offset', 660) +; DARWIN-G4-DUMP: ('num_reloc', 5) +; DARWIN-G4-DUMP: ('flags', 0x80000400) +; DARWIN-G4-DUMP: ('reserved1', 0) +; DARWIN-G4-DUMP: ('reserved2', 0) +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: ('_relocations', [ +; DARWIN-G4-DUMP: # Relocation 0 +; DARWIN-G4-DUMP: (('word-0', 0x34), +; DARWIN-G4-DUMP: ('word-1', 0x3c3)), +; DARWIN-G4-DUMP: # Relocation 1 +; DARWIN-G4-DUMP: (('word-0', 0xab000030), +; DARWIN-G4-DUMP: ('word-1', 0x74)), +; DARWIN-G4-DUMP: # Relocation 2 +; DARWIN-G4-DUMP: (('word-0', 0xa1000000), +; DARWIN-G4-DUMP: ('word-1', 0x14)), +; DARWIN-G4-DUMP: # Relocation 3 +; DARWIN-G4-DUMP: (('word-0', 0xac00002c), +; DARWIN-G4-DUMP: ('word-1', 0x74)), +; DARWIN-G4-DUMP: # Relocation 4 +; DARWIN-G4-DUMP: (('word-0', 0xa1000060), +; DARWIN-G4-DUMP: ('word-1', 0x14)), +; DARWIN-G4-DUMP: ]) +; DARWIN-G4-DUMP: # Section 1 +; DARWIN-G4-DUMP: (('section_name', '__textcoal_nt\x00\x00\x00') +; DARWIN-G4-DUMP: ('segment_name', '__TEXT\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +; DARWIN-G4-DUMP: ('address', 80) +; DARWIN-G4-DUMP: ('size', 0) +; DARWIN-G4-DUMP: ('offset', 608) +; DARWIN-G4-DUMP: ('alignment', 0) +; DARWIN-G4-DUMP: ('reloc_offset', 0) +; DARWIN-G4-DUMP: ('num_reloc', 0) +; DARWIN-G4-DUMP: ('flags', 0x8000000b) +; DARWIN-G4-DUMP: ('reserved1', 0) +; DARWIN-G4-DUMP: ('reserved2', 0) +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: ('_relocations', [ +; DARWIN-G4-DUMP: ]) +; DARWIN-G4-DUMP: # Section 2 +; DARWIN-G4-DUMP: (('section_name', '__picsymbolstub1') +; DARWIN-G4-DUMP: ('segment_name', '__TEXT\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +; DARWIN-G4-DUMP: ('address', 80) +; DARWIN-G4-DUMP: ('size', 32) +; DARWIN-G4-DUMP: ('offset', 608) +; DARWIN-G4-DUMP: ('alignment', 4) +; DARWIN-G4-DUMP: ('reloc_offset', 700) +; DARWIN-G4-DUMP: ('num_reloc', 4) +; DARWIN-G4-DUMP: ('flags', 0x80000408) +; DARWIN-G4-DUMP: ('reserved1', 0) +; DARWIN-G4-DUMP: ('reserved2', 32) +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: ('_relocations', [ +; DARWIN-G4-DUMP: # Relocation 0 +; DARWIN-G4-DUMP: (('word-0', 0xab000014), +; DARWIN-G4-DUMP: ('word-1', 0x70)), +; DARWIN-G4-DUMP: # Relocation 1 +; DARWIN-G4-DUMP: (('word-0', 0xa1000000), +; DARWIN-G4-DUMP: ('word-1', 0x58)), +; DARWIN-G4-DUMP: # Relocation 2 +; DARWIN-G4-DUMP: (('word-0', 0xac00000c), +; DARWIN-G4-DUMP: ('word-1', 0x70)), +; DARWIN-G4-DUMP: # Relocation 3 +; DARWIN-G4-DUMP: (('word-0', 0xa1000018), +; DARWIN-G4-DUMP: ('word-1', 0x58)), +; DARWIN-G4-DUMP: ]) +; DARWIN-G4-DUMP: # Section 3 +; DARWIN-G4-DUMP: (('section_name', '__la_symbol_ptr\x00') +; DARWIN-G4-DUMP: ('segment_name', '__DATA\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +; DARWIN-G4-DUMP: ('address', 112) +; DARWIN-G4-DUMP: ('size', 4) +; DARWIN-G4-DUMP: ('offset', 640) +; DARWIN-G4-DUMP: ('alignment', 0) +; DARWIN-G4-DUMP: ('reloc_offset', 732) +; DARWIN-G4-DUMP: ('num_reloc', 1) +; DARWIN-G4-DUMP: ('flags', 0x7) +; DARWIN-G4-DUMP: ('reserved1', 1) +; DARWIN-G4-DUMP: ('reserved2', 0) +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: ('_relocations', [ +; DARWIN-G4-DUMP: # Relocation 0 +; DARWIN-G4-DUMP: (('word-0', 0x0), +; DARWIN-G4-DUMP: ('word-1', 0x250)), +; DARWIN-G4-DUMP: ]) +; DARWIN-G4-DUMP: # Section 4 +; DARWIN-G4-DUMP: (('section_name', '__cstring\x00\x00\x00\x00\x00\x00\x00') +; DARWIN-G4-DUMP: ('segment_name', '__TEXT\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') +; DARWIN-G4-DUMP: ('address', 116) +; DARWIN-G4-DUMP: ('size', 14) +; DARWIN-G4-DUMP: ('offset', 644) +; DARWIN-G4-DUMP: ('alignment', 0) +; DARWIN-G4-DUMP: ('reloc_offset', 0) +; DARWIN-G4-DUMP: ('num_reloc', 0) +; DARWIN-G4-DUMP: ('flags', 0x2) +; DARWIN-G4-DUMP: ('reserved1', 0) +; DARWIN-G4-DUMP: ('reserved2', 0) +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: ('_relocations', [ +; DARWIN-G4-DUMP: ]) +; DARWIN-G4-DUMP: ]) +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: # Load Command 1 +; DARWIN-G4-DUMP: (('command', 2) +; DARWIN-G4-DUMP: ('size', 24) +; DARWIN-G4-DUMP: ('symoff', 748) +; DARWIN-G4-DUMP: ('nsyms', 3) +; DARWIN-G4-DUMP: ('stroff', 784) +; DARWIN-G4-DUMP: ('strsize', 40) +; DARWIN-G4-DUMP: ('_string_data', '\x00_main\x00dyld_stub_binding_helper\x00_puts\x00\x00\x00') +; DARWIN-G4-DUMP: ('_symbols', [ +; DARWIN-G4-DUMP: # Symbol 0 +; DARWIN-G4-DUMP: (('n_strx', 1) +; DARWIN-G4-DUMP: ('n_type', 0xf) +; DARWIN-G4-DUMP: ('n_sect', 1) +; DARWIN-G4-DUMP: ('n_desc', 0) +; DARWIN-G4-DUMP: ('n_value', 0) +; DARWIN-G4-DUMP: ('_string', '_main') +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: # Symbol 1 +; DARWIN-G4-DUMP: (('n_strx', 32) +; DARWIN-G4-DUMP: ('n_type', 0x1) +; DARWIN-G4-DUMP: ('n_sect', 0) +; DARWIN-G4-DUMP: ('n_desc', 1) +; DARWIN-G4-DUMP: ('n_value', 0) +; DARWIN-G4-DUMP: ('_string', '_puts') +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: # Symbol 2 +; DARWIN-G4-DUMP: (('n_strx', 7) +; DARWIN-G4-DUMP: ('n_type', 0x1) +; DARWIN-G4-DUMP: ('n_sect', 0) +; DARWIN-G4-DUMP: ('n_desc', 0) +; DARWIN-G4-DUMP: ('n_value', 0) +; DARWIN-G4-DUMP: ('_string', 'dyld_stub_binding_helper') +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: ]) +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: # Load Command 2 +; DARWIN-G4-DUMP: (('command', 11) +; DARWIN-G4-DUMP: ('size', 80) +; DARWIN-G4-DUMP: ('ilocalsym', 0) +; DARWIN-G4-DUMP: ('nlocalsym', 0) +; DARWIN-G4-DUMP: ('iextdefsym', 0) +; DARWIN-G4-DUMP: ('nextdefsym', 1) +; DARWIN-G4-DUMP: ('iundefsym', 1) +; DARWIN-G4-DUMP: ('nundefsym', 2) +; DARWIN-G4-DUMP: ('tocoff', 0) +; DARWIN-G4-DUMP: ('ntoc', 0) +; DARWIN-G4-DUMP: ('modtaboff', 0) +; DARWIN-G4-DUMP: ('nmodtab', 0) +; DARWIN-G4-DUMP: ('extrefsymoff', 0) +; DARWIN-G4-DUMP: ('nextrefsyms', 0) +; DARWIN-G4-DUMP: ('indirectsymoff', 740) +; DARWIN-G4-DUMP: ('nindirectsyms', 2) +; DARWIN-G4-DUMP: ('extreloff', 0) +; DARWIN-G4-DUMP: ('nextrel', 0) +; DARWIN-G4-DUMP: ('locreloff', 0) +; DARWIN-G4-DUMP: ('nlocrel', 0) +; DARWIN-G4-DUMP: ('_indirect_symbols', [ +; DARWIN-G4-DUMP: # Indirect Symbol 0 +; DARWIN-G4-DUMP: (('symbol_index', 0x1),), +; DARWIN-G4-DUMP: # Indirect Symbol 1 +; DARWIN-G4-DUMP: (('symbol_index', 0x1),), +; DARWIN-G4-DUMP: ]) +; DARWIN-G4-DUMP: ), +; DARWIN-G4-DUMP: ]) diff --git a/test/CodeGen/PowerPC/hello-reloc.s b/test/CodeGen/PowerPC/hello-reloc.s index 9bbfb38..1e3fb8f 100644 --- a/test/CodeGen/PowerPC/hello-reloc.s +++ b/test/CodeGen/PowerPC/hello-reloc.s @@ -1,14 +1,10 @@ ; This tests for the basic implementation of PPCMachObjectWriter.cpp, ; which is responsible for writing mach-o relocation entries for (PIC) ; PowerPC objects. -; NOTE: Darwin PPC asm syntax is not yet supported by PPCAsmParser, -; so this test case uses ELF PPC asm syntax to produce a mach-o object. -; Once PPCAsmParser supports darwin asm syntax, this test case should -; be updated accordingly. ; RUN: llvm-mc -filetype=obj -relocation-model=pic -mcpu=g4 -triple=powerpc-apple-darwin8 %s -o - | llvm-readobj -relocations | FileCheck -check-prefix=DARWIN-G4-DUMP %s -; .machine ppc7400 + .machine ppc7400 .section __TEXT,__textcoal_nt,coalesced,pure_instructions .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32 .section __TEXT,__text,regular,pure_instructions @@ -16,40 +12,40 @@ .align 4 _main: ; @main ; BB#0: ; %entry - mflr 0 - stw 31, -4(1) - stw 0, 8(1) - stwu 1, -80(1) + mflr r0 + stw r31, -4(r1) + stw r0, 8(r1) + stwu r1, -80(r1) bl L0$pb L0$pb: - mr 31, 1 - li 5, 0 + mr r31, r1 + li r5, 0 mflr 2 - stw 3, 68(31) - stw 5, 72(31) - stw 4, 64(31) - addis 2, 2, (L_.str-L0$pb)@ha - la 3, (L_.str-L0$pb)@l(2) + stw r3, 68(r31) + stw r5, 72(r31) + stw r4, 64(r31) + addis r2, r2, ha16(L_.str-L0$pb) + la r3, lo16(L_.str-L0$pb)(r2) bl L_puts$stub - li 3, 0 - addi 1, 1, 80 - lwz 0, 8(1) - lwz 31, -4(1) - mtlr 0 + li r3, 0 + addi r1, r1, 80 + lwz r0, 8(r1) + lwz r31, -4(r1) + mtlr r0 blr .section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32 .align 4 L_puts$stub: .indirect_symbol _puts - mflr 0 + mflr r0 bcl 20, 31, L_puts$stub$tmp L_puts$stub$tmp: - mflr 11 - addis 11, 11, (L_puts$lazy_ptr-L_puts$stub$tmp)@ha - mtlr 0 - lwzu 12, (L_puts$lazy_ptr-L_puts$stub$tmp)@l(11) - mtctr 12 + mflr r11 + addis r11, r11, ha16(L_puts$lazy_ptr-L_puts$stub$tmp) + mtlr r0 + lwzu r12, lo16(L_puts$lazy_ptr-L_puts$stub$tmp)(r11) + mtctr r12 bctr .section __DATA,__la_symbol_ptr,lazy_symbol_pointers L_puts$lazy_ptr: diff --git a/test/MC/MachO/indirect-symbols.s b/test/MC/MachO/indirect-symbols.s index 90fd231..deb9e7a 100644 --- a/test/MC/MachO/indirect-symbols.s +++ b/test/MC/MachO/indirect-symbols.s @@ -97,7 +97,7 @@ _e: // CHECK: ('nsyms', 6) // CHECK: ('stroff', 516) // CHECK: ('strsize', 20) -// CHECK: ('_string_data', '\x00_d\x00_a\x00_b\x00_c\x00_e\x00_f\x00\x00') +// CHECK: ('_string_data', '\x00_a\x00_d\x00_b\x00_c\x00_e\x00_f\x00\x00') // CHECK: ('_symbols', [ // CHECK: # Symbol 0 // CHECK: (('n_strx', 7) @@ -132,7 +132,7 @@ _e: // CHECK: ('_string', '_f') // CHECK: ), // CHECK: # Symbol 4 -// CHECK: (('n_strx', 4) +// CHECK: (('n_strx', 1) // CHECK: ('n_type', 0x1) // CHECK: ('n_sect', 0) // CHECK: ('n_desc', 1) @@ -140,7 +140,7 @@ _e: // CHECK: ('_string', '_a') // CHECK: ), // CHECK: # Symbol 5 -// CHECK: (('n_strx', 1) +// CHECK: (('n_strx', 4) // CHECK: ('n_type', 0x1) // CHECK: ('n_sect', 0) // CHECK: ('n_desc', 0) diff --git a/test/MC/MachO/symbol-indirect.s b/test/MC/MachO/symbol-indirect.s index 2412970..6f5c842 100644 --- a/test/MC/MachO/symbol-indirect.s +++ b/test/MC/MachO/symbol-indirect.s @@ -137,7 +137,7 @@ sym_nlp_G: // CHECK: ('nsyms', 10) // CHECK: ('stroff', 592) // CHECK: ('strsize', 104) -// CHECK: ('_string_data', '\x00sym_lsp_A\x00sym_lsp_G\x00sym_nlp_A\x00sym_nlp_G\x00sym_nlp_B\x00sym_nlp_E\x00sym_lsp_B\x00sym_lsp_E\x00sym_lsp_C\x00sym_nlp_C\x00\x00\x00\x00') +// CHECK: ('_string_data', '\x00sym_lsp_A\x00sym_lsp_G\x00sym_nlp_A\x00sym_nlp_G\x00sym_lsp_B\x00sym_lsp_E\x00sym_nlp_B\x00sym_nlp_E\x00sym_lsp_C\x00sym_nlp_C\x00\x00\x00\x00') // CHECK: ('_symbols', [ // CHECK: # Symbol 0 // CHECK: (('n_strx', 81) @@ -180,7 +180,7 @@ sym_nlp_G: // CHECK: ('_string', 'sym_lsp_A') // CHECK: ), // CHECK: # Symbol 5 -// CHECK: (('n_strx', 61) +// CHECK: (('n_strx', 41) // CHECK: ('n_type', 0x1) // CHECK: ('n_sect', 0) // CHECK: ('n_desc', 1) @@ -188,7 +188,7 @@ sym_nlp_G: // CHECK: ('_string', 'sym_lsp_B') // CHECK: ), // CHECK: # Symbol 6 -// CHECK: (('n_strx', 71) +// CHECK: (('n_strx', 51) // CHECK: ('n_type', 0x1) // CHECK: ('n_sect', 0) // CHECK: ('n_desc', 1) @@ -204,7 +204,7 @@ sym_nlp_G: // CHECK: ('_string', 'sym_nlp_A') // CHECK: ), // CHECK: # Symbol 8 -// CHECK: (('n_strx', 41) +// CHECK: (('n_strx', 61) // CHECK: ('n_type', 0x1) // CHECK: ('n_sect', 0) // CHECK: ('n_desc', 0) @@ -212,7 +212,7 @@ sym_nlp_G: // CHECK: ('_string', 'sym_nlp_B') // CHECK: ), // CHECK: # Symbol 9 -// CHECK: (('n_strx', 51) +// CHECK: (('n_strx', 71) // CHECK: ('n_type', 0x1) // CHECK: ('n_sect', 0) // CHECK: ('n_desc', 0) diff --git a/test/Transforms/GCOVProfiling/version.ll b/test/Transforms/GCOVProfiling/version.ll index 2f1bd70..c181058 100644 --- a/test/Transforms/GCOVProfiling/version.ll +++ b/test/Transforms/GCOVProfiling/version.ll @@ -1,11 +1,11 @@ ; RUN: echo '!9 = metadata !{metadata !"%T/version.ll", metadata !0}' > %t1 ; RUN: cat %s %t1 > %t2 ; RUN: opt -insert-gcov-profiling -disable-output < %t2 -; RUN: head -c12 %T/version.gcno | grep '^oncg\*204MVLL$' +; RUN: ghead -c12 %T/version.gcno | grep '^oncg\*204MVLL$' ; RUN: rm %T/version.gcno ; RUN: not opt -insert-gcov-profiling -default-gcov-version=asdfasdf -disable-output < %t2 ; RUN: opt -insert-gcov-profiling -default-gcov-version=407* -disable-output < %t2 -; RUN: head -c12 %T/version.gcno | grep '^oncg\*704MVLL$' +; RUN: ghead -c12 %T/version.gcno | grep '^oncg\*704MVLL$' ; RUN: rm %T/version.gcno define void @test() { diff --git a/tools/bugpoint-passes/CMakeLists.txt b/tools/bugpoint-passes/CMakeLists.txt index 05f190a..45b4a23 100644 --- a/tools/bugpoint-passes/CMakeLists.txt +++ b/tools/bugpoint-passes/CMakeLists.txt @@ -1,3 +1,6 @@ +# patch: this should be built as a bundle/module/plug-in +set(MODULE TRUE) + if( NOT LLVM_BUILD_TOOLS ) set(EXCLUDE_FROM_ALL ON) endif() diff --git a/utils/TableGen/AsmMatcherEmitter.cpp b/utils/TableGen/AsmMatcherEmitter.cpp index de24cde..adb129d 100644 --- a/utils/TableGen/AsmMatcherEmitter.cpp +++ b/utils/TableGen/AsmMatcherEmitter.cpp @@ -325,7 +325,7 @@ struct MatchableInfo { /// assembler operand expands to multiple MCOperands, this represents the /// single assembler operand, not the MCOperand. struct ResOperand { - enum { + enum ResOperandEnum { /// RenderAsmOperand - This represents an operand result that is /// generated by calling the render method on the assembly operand. The /// corresponding AsmOperand is specified by AsmOperandNum. diff --git a/utils/TableGen/CodeGenInstruction.h b/utils/TableGen/CodeGenInstruction.h index 6004f66..8bae408 100644 --- a/utils/TableGen/CodeGenInstruction.h +++ b/utils/TableGen/CodeGenInstruction.h @@ -30,7 +30,7 @@ namespace llvm { class CGIOperandList { public: class ConstraintInfo { - enum { None, EarlyClobber, Tied } Kind; + enum KindEnum { None, EarlyClobber, Tied } Kind; unsigned OtherTiedOperand; public: ConstraintInfo() : Kind(None) {} @@ -300,7 +300,7 @@ namespace llvm { int64_t Imm; public: - enum { + enum KindEnum { K_Record, K_Imm, K_Reg diff --git a/utils/TableGen/CodeGenIntrinsics.h b/utils/TableGen/CodeGenIntrinsics.h index ababfa4..750c2da 100644 --- a/utils/TableGen/CodeGenIntrinsics.h +++ b/utils/TableGen/CodeGenIntrinsics.h @@ -59,7 +59,7 @@ namespace llvm { IntrinsicSignature IS; // Memory mod/ref behavior of this intrinsic. - enum { + enum ModRefEnum { NoMem, ReadArgMem, ReadMem, ReadWriteArgMem, ReadWriteMem } ModRef; diff --git a/utils/TableGen/InstrInfoEmitter.cpp b/utils/TableGen/InstrInfoEmitter.cpp index d3d9cc1..c75bc31 100644 --- a/utils/TableGen/InstrInfoEmitter.cpp +++ b/utils/TableGen/InstrInfoEmitter.cpp @@ -255,7 +255,7 @@ void InstrInfoEmitter::emitOperandNameMappings(raw_ostream &OS, OS << "namespace llvm {"; OS << "namespace " << Namespace << " {\n"; OS << "namespace " << OpNameNS << " { \n"; - OS << "enum {\n"; + OS << "enum OperandEnum {\n"; for (StrUintMapIter i = Operands.begin(), e = Operands.end(); i != e; ++i) OS << " " << i->first << " = " << i->second << ",\n"; @@ -585,7 +585,7 @@ void InstrInfoEmitter::emitEnums(raw_ostream &OS) { Target.getInstructionsByEnumValue(); OS << "namespace " << Namespace << " {\n"; - OS << " enum {\n"; + OS << " enum InstructionEnum {\n"; for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) { OS << " " << NumberedInstructions[i]->TheDef->getName() << "\t= " << i << ",\n"; diff --git a/utils/TableGen/RegisterInfoEmitter.cpp b/utils/TableGen/RegisterInfoEmitter.cpp index cc08df9..28cbbe6 100644 --- a/utils/TableGen/RegisterInfoEmitter.cpp +++ b/utils/TableGen/RegisterInfoEmitter.cpp @@ -88,7 +88,7 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS, if (!Namespace.empty()) OS << "namespace " << Namespace << " {\n"; - OS << "enum {\n NoRegister,\n"; + OS << "enum RegisterEnum {\n NoRegister,\n"; for (unsigned i = 0, e = Registers.size(); i != e; ++i) OS << " " << Registers[i]->getName() << " = " << @@ -110,7 +110,7 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS, OS << "\n// Register classes\n"; if (!Namespace.empty()) OS << "namespace " << Namespace << " {\n"; - OS << "enum {\n"; + OS << "enum RegisterClassEnum {\n"; for (unsigned i = 0, e = RegisterClasses.size(); i != e; ++i) { if (i) OS << ",\n"; OS << " " << RegisterClasses[i]->getName() << "RegClassID"; @@ -128,7 +128,7 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS, OS << "\n// Register alternate name indices\n"; if (!Namespace.empty()) OS << "namespace " << Namespace << " {\n"; - OS << "enum {\n"; + OS << "enum AltNameEnum {\n"; for (unsigned i = 0, e = RegAltNameIndices.size(); i != e; ++i) OS << " " << RegAltNameIndices[i]->getName() << ",\t// " << i << "\n"; OS << " NUM_TARGET_REG_ALT_NAMES = " << RegAltNameIndices.size() << "\n"; @@ -144,7 +144,7 @@ void RegisterInfoEmitter::runEnums(raw_ostream &OS, SubRegIndices[0]->getNamespace(); if (!Namespace.empty()) OS << "namespace " << Namespace << " {\n"; - OS << "enum {\n NoSubRegister,\n"; + OS << "enum SubRegisterEnum {\n NoSubRegister,\n"; for (unsigned i = 0, e = SubRegIndices.size(); i != e; ++i) OS << " " << SubRegIndices[i]->getName() << ",\t// " << i+1 << "\n"; OS << " NUM_TARGET_SUBREGS\n};\n"; diff --git a/utils/TableGen/SubtargetEmitter.cpp b/utils/TableGen/SubtargetEmitter.cpp index b9f9d06..98d0137 100644 --- a/utils/TableGen/SubtargetEmitter.cpp +++ b/utils/TableGen/SubtargetEmitter.cpp @@ -141,7 +141,7 @@ void SubtargetEmitter::Enumeration(raw_ostream &OS, } } else { // Open enumeration - OS << "enum {\n"; + OS << "enum SubtargetEnum {\n"; // For each record for (unsigned i = 0; i < N;) { diff --git a/utils/Target/PowerPC/darwin-to-linux-ppc-asm.sed b/utils/Target/PowerPC/darwin-to-linux-ppc-asm.sed new file mode 100755 index 0000000..ac01cda --- /dev/null +++ b/utils/Target/PowerPC/darwin-to-linux-ppc-asm.sed @@ -0,0 +1,7 @@ +#!env sed -f +# syntactically translates darwin-ppc assembly to GNU/linux assembly +# GNU sed required +s|\|\1|g +s|\\(([^(]*)\)|\1@ha|g +s|\\(([^(]*)\)|\1@l|g +# s|\|.&|g diff --git a/utils/lit/lit/LitConfig.py b/utils/lit/lit/LitConfig.py index b0dde5d..ecfd9a8 100644 --- a/utils/lit/lit/LitConfig.py +++ b/utils/lit/lit/LitConfig.py @@ -34,7 +34,8 @@ class LitConfig: self.debug = debug self.isWindows = bool(isWindows) self.params = dict(params) - self.bashPath = None + # local hack only, don't commit this patch + self.bashPath = '@FINK_PREFIX@/bin/bash' # Configuration files to look for when discovering test suites. self.config_prefix = config_prefix or 'lit' diff --git a/utils/lit/lit/TestRunner.py b/utils/lit/lit/TestRunner.py index 9752417..2bb3f75 100644 --- a/utils/lit/lit/TestRunner.py +++ b/utils/lit/lit/TestRunner.py @@ -415,11 +415,16 @@ def parseIntegratedTestScript(test, normalize_slashes=False, return str(line_number - int(match.group(2))) ln = re.sub('%\(line *([\+-]) *(\d+)\)', replace_line_number, ln) + ln1 = ln.split(' ')[1]; # Collapse lines with trailing '\\'. if script and script[-1][-1] == '\\': script[-1] = script[-1][:-1] + ln - else: + # do not commit this patch + # exception: built-in shell commands and operators + elif ln[1] == '(' or ln[1] == '{' or ln[1] == '!' or ln[1] == '|' or ln1 == 'export' or ln1 == 'env' or ln1 == 'cd' or ln1 == 'pushd' or ln1 == 'set' or ln1 == 'test' or ln1 == 'umask': script.append(ln) + else: + script.append('gtimeout 1m ' +ln) elif command_type == 'XFAIL': test.xfails.extend([s.strip() for s in ln.split(',')]) elif command_type == 'REQUIRES':