implement CodeGen/X86/inline-asm-x-scalar.ll:test3
[oota-llvm.git] / lib / CodeGen / DwarfWriter.cpp
index 5564d2b8e3647fe7a278adcd506a8c4a6089059e..1ca9e218375fddc30018d46cbc648fec037548f0 100644 (file)
@@ -22,6 +22,7 @@
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/CodeGen/MachineFrameInfo.h"
 #include "llvm/CodeGen/MachineLocation.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/Dwarf.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/DataTypes.h"
@@ -55,7 +56,7 @@ class DIEValue;
 
 //===----------------------------------------------------------------------===//
 /// DWLabel - Labels are used to track locations in the assembler file.
-/// Labels appear in the form <prefix>debug_<Tag><Number>, where the tag is a
+/// Labels appear in the form <prefix><Tag><Number>, where the tag is a
 /// category of label (Ex. location) and number is a value unique in that
 /// category.
 class DWLabel {
@@ -80,7 +81,7 @@ public:
     if (O) print(*O);
   }
   void print(std::ostream &O) const {
-    O << ".D" << Tag;
+    O << "." << Tag;
     if (Number) O << Number;
   }
 #endif
@@ -236,7 +237,7 @@ public:
   unsigned getOffset()                       const { return Offset; }
   unsigned getSize()                         const { return Size; }
   const std::vector<DIE *> &getChildren()    const { return Children; }
-  const std::vector<DIEValue *> &getValues() const { return Values; }
+  std::vector<DIEValue *> &getValues()       { return Values; }
   void setTag(unsigned Tag)                  { Abbrev.setTag(Tag); }
   void setOffset(unsigned O)                 { Offset = O; }
   void setSize(unsigned S)                   { Size = S; }
@@ -314,7 +315,7 @@ public:
   
   /// EmitValue - Emit value via the Dwarf writer.
   ///
-  virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const = 0;
+  virtual void EmitValue(DwarfDebug &DD, unsigned Form) = 0;
   
   /// SizeOf - Return the size of a value in bytes.
   ///
@@ -364,7 +365,7 @@ public:
     
   /// EmitValue - Emit integer of appropriate size.
   ///
-  virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
+  virtual void EmitValue(DwarfDebug &DD, unsigned Form);
   
   /// SizeOf - Determine size of integer value in bytes.
   ///
@@ -401,7 +402,7 @@ public:
   
   /// EmitValue - Emit string value.
   ///
-  virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
+  virtual void EmitValue(DwarfDebug &DD, unsigned Form);
   
   /// SizeOf - Determine size of string value in bytes.
   ///
@@ -440,7 +441,7 @@ public:
   
   /// EmitValue - Emit label value.
   ///
-  virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
+  virtual void EmitValue(DwarfDebug &DD, unsigned Form);
   
   /// SizeOf - Determine size of label value in bytes.
   ///
@@ -478,7 +479,7 @@ public:
   
   /// EmitValue - Emit label value.
   ///
-  virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
+  virtual void EmitValue(DwarfDebug &DD, unsigned Form);
   
   /// SizeOf - Determine size of label value in bytes.
   ///
@@ -516,7 +517,7 @@ public:
   
   /// EmitValue - Emit delta value.
   ///
-  virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
+  virtual void EmitValue(DwarfDebug &DD, unsigned Form);
   
   /// SizeOf - Determine size of delta value in bytes.
   ///
@@ -558,7 +559,7 @@ public:
   
   /// EmitValue - Emit debug information entry offset.
   ///
-  virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
+  virtual void EmitValue(DwarfDebug &DD, unsigned Form);
   
   /// SizeOf - Determine size of debug information entry in bytes.
   ///
@@ -623,7 +624,7 @@ public:
 
   /// EmitValue - Emit block data.
   ///
-  virtual void EmitValue(const DwarfDebug &DD, unsigned Form) const;
+  virtual void EmitValue(DwarfDebug &DD, unsigned Form);
   
   /// SizeOf - Determine size of block data in bytes.
   ///
@@ -791,18 +792,11 @@ protected:
   ///
   MachineModuleInfo *MMI;
   
-  /// didInitial - Flag to indicate if initial emission has been done.
-  ///
-  bool didInitial;
-  
-  /// shouldEmit - Flag to indicate if debug information should be emitted.
-  ///
-  bool shouldEmit;
-  
   /// SubprogramCount - The running count of functions being compiled.
   ///
   unsigned SubprogramCount;
-  
+
+  unsigned SetCounter;
   Dwarf(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
   : O(OS)
   , Asm(A)
@@ -812,9 +806,8 @@ protected:
   , M(NULL)
   , MF(NULL)
   , MMI(NULL)
-  , didInitial(false)
-  , shouldEmit(false)
   , SubprogramCount(0)
+  , SetCounter(1)
   {
   }
 
@@ -827,20 +820,17 @@ public:
   MachineModuleInfo *getMMI() const { return MMI; }
   const TargetAsmInfo *getTargetAsmInfo() const { return TAI; }
 
-  /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
-  ///
-  bool ShouldEmitDwarf() const { return shouldEmit; }
-
-
   /// PrintLabelName - Print label name in form used by Dwarf writer.
   ///
   void PrintLabelName(DWLabel Label) const {
     PrintLabelName(Label.Tag, Label.Number);
   }
-  void PrintLabelName(const char *Tag, unsigned Number) const {
-    O << TAI->getPrivateGlobalPrefix()
-      << ((Tag && *Tag) ? "debug_" : "label_")
-      << Tag;
+  void PrintLabelName(const char *Tag, unsigned Number,
+                      bool isInSection = false) const {
+    if (isInSection && TAI->getDwarfSectionOffsetDirective())
+      O << TAI->getDwarfSectionOffsetDirective() << Tag;
+    else
+      O << TAI->getPrivateGlobalPrefix() << Tag;
     if (Number) O << Number;
   }
   
@@ -885,17 +875,15 @@ public:
   /// assemblers do not behave with absolute expressions with data directives,
   /// so there is an option (needsSet) to use an intermediary set expression.
   void EmitDifference(DWLabel LabelHi, DWLabel LabelLo,
-                      bool IsSmall = false) const {
+                      bool IsSmall = false) {
     EmitDifference(LabelHi.Tag, LabelHi.Number,
                    LabelLo.Tag, LabelLo.Number,
                    IsSmall);
   }
   void EmitDifference(const char *TagHi, unsigned NumberHi,
                       const char *TagLo, unsigned NumberLo,
-                      bool IsSmall = false) const {
+                      bool IsSmall = false) {
     if (TAI->needsSet()) {
-      static unsigned SetCounter = 1;
-      
       O << "\t.set\t";
       PrintLabelName("set", SetCounter);
       O << ",";
@@ -923,7 +911,42 @@ public:
       PrintLabelName(TagLo, NumberLo);
     }
   }
-                      
+
+  void EmitSectionOffset(const char* Label, const char* Section,
+                         unsigned LabelNumber, unsigned SectionNumber,
+                         bool IsSmall = false) {
+    if (TAI->needsSet()) {
+      O << "\t.set\t";
+      PrintLabelName("set", SetCounter);
+      O << ",";
+      PrintLabelName(Label, LabelNumber, true);
+      if (!TAI->isAbsoluteSectionOffsets()) {
+        O << "-";
+        PrintLabelName(Section, SectionNumber);
+      }      
+      O << "\n";
+      
+      if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
+        O << TAI->getData32bitsDirective();
+      else
+        O << TAI->getData64bitsDirective();
+        
+      PrintLabelName("set", SetCounter);
+      ++SetCounter;
+    } else {
+      if (IsSmall || TAI->getAddressSize() == sizeof(int32_t))
+        O << TAI->getData32bitsDirective();
+      else
+        O << TAI->getData64bitsDirective();
+        
+      PrintLabelName(Label, LabelNumber, true);
+      if (!TAI->isAbsoluteSectionOffsets()) {
+        O << "-";
+        PrintLabelName(Section, SectionNumber);
+      }
+    }    
+  }
+  
   /// EmitFrameMoves - Emit frame instructions to describe the layout of the
   /// frame.
   void EmitFrameMoves(const char *BaseLabel, unsigned BaseLabelID,
@@ -932,7 +955,7 @@ public:
         Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
           TargetFrameInfo::StackGrowsUp ?
             TAI->getAddressSize() : -TAI->getAddressSize();
-    bool IsLocal = BaseLabel && strcmp(BaseLabel, "") == 0;
+    bool IsLocal = BaseLabel && strcmp(BaseLabel, "label") == 0;
 
     for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
       MachineMove &Move = Moves[i];
@@ -952,11 +975,11 @@ public:
       if (BaseLabel && LabelID && (BaseLabelID != LabelID || !IsLocal)) {
         Asm->EmitInt8(DW_CFA_advance_loc4);
         Asm->EOL("DW_CFA_advance_loc4");
-        EmitDifference("", LabelID, BaseLabel, BaseLabelID, true);
-        Asm->EOL("");
+        EmitDifference("label", LabelID, BaseLabel, BaseLabelID, true);
+        Asm->EOL();
         
         BaseLabelID = LabelID;
-        BaseLabel = "";
+        BaseLabel = "label";
         IsLocal = true;
       }
       
@@ -1066,8 +1089,19 @@ private:
   ///
   std::vector<std::vector<SourceLineInfo> > SectionSourceLines;
 
+  /// didInitial - Flag to indicate if initial emission has been done.
+  ///
+  bool didInitial;
+  
+  /// shouldEmit - Flag to indicate if debug information should be emitted.
+  ///
+  bool shouldEmit;
 
 public:
+  
+  /// ShouldEmitDwarf - Returns true if Dwarf declarations should be made.
+  ///
+  bool ShouldEmitDwarf() const { return shouldEmit; }
 
   /// AssignAbbrevNumber - Define a unique number for the abbreviation.
   ///  
@@ -1654,8 +1688,11 @@ private:
   CompileUnit *NewCompileUnit(CompileUnitDesc *UnitDesc, unsigned ID) {
     // Construct debug information entry.
     DIE *Die = new DIE(DW_TAG_compile_unit);
-    AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
-                                                  DWLabel("section_line", 0));
+    if (TAI->isAbsoluteSectionOffsets())
+      AddLabel(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0));
+    else
+      AddDelta(Die, DW_AT_stmt_list, DW_FORM_data4, DWLabel("section_line", 0),
+               DWLabel("section_line", 0));      
     AddString(Die, DW_AT_producer,  DW_FORM_string, UnitDesc->getProducer());
     AddUInt  (Die, DW_AT_language,  DW_FORM_data1,  UnitDesc->getLanguage());
     AddString(Die, DW_AT_name,      DW_FORM_string, UnitDesc->getFileName());
@@ -1850,14 +1887,14 @@ private:
         // Add the scope bounds.
         if (StartID) {
           AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
-                             DWLabel("", StartID));
+                             DWLabel("label", StartID));
         } else {
           AddLabel(ScopeDie, DW_AT_low_pc, DW_FORM_addr,
                              DWLabel("func_begin", SubprogramCount));
         }
         if (EndID) {
           AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
-                             DWLabel("", EndID));
+                             DWLabel("label", EndID));
         } else {
           AddLabel(ScopeDie, DW_AT_high_pc, DW_FORM_addr,
                              DWLabel("func_end", SubprogramCount));
@@ -1939,12 +1976,12 @@ private:
 
   /// EmitDIE - Recusively Emits a debug information entry.
   ///
-  void EmitDIE(DIE *Die) const {
+  void EmitDIE(DIE *Die) {
     // Get the abbreviation for this DIE.
     unsigned AbbrevNumber = Die->getAbbrevNumber();
     const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
     
-    Asm->EOL("");
+    Asm->EOL();
 
     // Emit the code (index) for the abbreviation.
     Asm->EmitULEB128Bytes(AbbrevNumber);
@@ -1954,7 +1991,7 @@ private:
              ":0x" + utohexstr(Die->getSize()) + " " +
              TagString(Abbrev->getTag())));
     
-    const std::vector<DIEValue *> &Values = Die->getValues();
+    std::vector<DIEValue *> &Values = Die->getValues();
     const std::vector<DIEAbbrevData> &AbbrevData = Abbrev->getData();
     
     // Emit the DIE attribute values.
@@ -2053,7 +2090,7 @@ private:
 
   /// EmitDebugInfo - Emit the debug info section.
   ///
-  void EmitDebugInfo() const {
+  void EmitDebugInfo() {
     // Start debug info section.
     Asm->SwitchToDataSection(TAI->getDwarfInfoSection());
     
@@ -2070,7 +2107,7 @@ private:
                            
     Asm->EmitInt32(ContentSize);  Asm->EOL("Length of Compilation Unit Info");
     Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF version number");
-    EmitDifference("abbrev_begin", 0, "section_abbrev", 0, true);
+    EmitSectionOffset("abbrev_begin", "section_abbrev", 0, 0, true);
     Asm->EOL("Offset Into Abbrev. Section");
     Asm->EmitInt8(TAI->getAddressSize()); Asm->EOL("Address Size (in bytes)");
   
@@ -2082,7 +2119,7 @@ private:
     Asm->EmitInt8(0); Asm->EOL("Extra Pad For GDB");
     EmitLabel("info_end", Unit->getID());
     
-    Asm->EOL("");
+    Asm->EOL();
   }
 
   /// EmitAbbreviations - Emit the abbreviation section.
@@ -2107,18 +2144,21 @@ private:
         // Emit the abbreviations data.
         Abbrev->Emit(*this);
     
-        Asm->EOL("");
+        Asm->EOL();
       }
       
+      // Mark end of abbreviations.
+      Asm->EmitULEB128Bytes(0); Asm->EOL("EOM(3)");
+
       EmitLabel("abbrev_end", 0);
     
-      Asm->EOL("");
+      Asm->EOL();
     }
   }
 
   /// EmitDebugLines - Emit source line information.
   ///
-  void EmitDebugLines() const {
+  void EmitDebugLines() {
     // Minimum line delta, thus ranging from -10..(255-10).
     const int MinLineDelta = -(DW_LNS_fixed_advance_pc + 1);
     // Maximum line delta, thus ranging from -10..(255-10).
@@ -2217,7 +2257,7 @@ private:
         Asm->EmitInt8(0); Asm->EOL("Extended Op");
         Asm->EmitInt8(TAI->getAddressSize() + 1); Asm->EOL("Op size");
         Asm->EmitInt8(DW_LNE_set_address); Asm->EOL("DW_LNE_set_address");
-        EmitReference("",  LabelID); Asm->EOL("Location label");
+        EmitReference("label",  LabelID); Asm->EOL("Location label");
         
         // If change of source, then switch to the new source.
         if (Source != LineInfo.getSourceID()) {
@@ -2259,13 +2299,13 @@ private:
 
       // Mark end of matrix.
       Asm->EmitInt8(0); Asm->EOL("DW_LNE_end_sequence");
-      Asm->EmitULEB128Bytes(1); Asm->EOL("");
-      Asm->EmitInt8(1); Asm->EOL("");
+      Asm->EmitULEB128Bytes(1); Asm->EOL();
+      Asm->EmitInt8(1); Asm->EOL();
     }
     
     EmitLabel("line_end", 0);
     
-    Asm->EOL("");
+    Asm->EOL();
   }
     
   /// EmitInitialDebugFrame - Emit common frame info into a debug frame section.
@@ -2308,7 +2348,7 @@ private:
     Asm->EmitAlignment(2);
     EmitLabel("frame_common_end", 0);
     
-    Asm->EOL("");
+    Asm->EOL();
   }
 
   /// EmitFunctionDebugFrame - Emit per function frame info into a debug frame
@@ -2325,8 +2365,8 @@ private:
     Asm->EOL("Length of Frame Information Entry");
     
     EmitLabel("frame_begin", SubprogramCount);
-    
-    EmitDifference("frame_common_begin", 0, "section_frame", 0, true);
+
+    EmitSectionOffset("frame_common_begin", "section_frame", 0, 0, true);
     Asm->EOL("FDE CIE offset");
 
     EmitReference("func_begin", SubprogramCount);
@@ -2342,7 +2382,7 @@ private:
     Asm->EmitAlignment(2);
     EmitLabel("frame_end", SubprogramCount);
 
-    Asm->EOL("");
+    Asm->EOL();
   }
 
   /// EmitDebugPubNames - Emit visible names into a debug pubnames section.
@@ -2360,8 +2400,8 @@ private:
     EmitLabel("pubnames_begin", Unit->getID());
     
     Asm->EmitInt16(DWARF_VERSION); Asm->EOL("DWARF Version");
-    
-    EmitDifference("info_begin", Unit->getID(), "section_info", 0, true);
+
+    EmitSectionOffset("info_begin", "section_info", Unit->getID(), 0, true);
     Asm->EOL("Offset of Compilation Unit Info");
 
     EmitDifference("info_end", Unit->getID(), "info_begin", Unit->getID(),true);
@@ -2382,7 +2422,7 @@ private:
     Asm->EmitInt32(0); Asm->EOL("End Mark");
     EmitLabel("pubnames_end", Unit->getID());
   
-    Asm->EOL("");
+    Asm->EOL();
   }
 
   /// EmitDebugStr - Emit visible names into a debug str section.
@@ -2400,10 +2440,10 @@ private:
         EmitLabel("string", StringID);
         // Emit the string itself.
         const std::string &String = StringPool[StringID];
-        Asm->EmitString(String); Asm->EOL("");
+        Asm->EmitString(String); Asm->EOL();
       }
     
-      Asm->EOL("");
+      Asm->EOL();
     }
   }
 
@@ -2413,7 +2453,7 @@ private:
     // Start the dwarf loc section.
     Asm->SwitchToDataSection(TAI->getDwarfLocSection());
     
-    Asm->EOL("");
+    Asm->EOL();
   }
 
   /// EmitDebugARanges - Emit visible names into a debug aranges section.
@@ -2448,7 +2488,7 @@ private:
     Asm->EmitInt32(0); Asm->EOL("EOM (1)");
     Asm->EmitInt32(0); Asm->EOL("EOM (2)");
     
-    Asm->EOL("");
+    Asm->EOL();
   #endif
   }
 
@@ -2458,7 +2498,7 @@ private:
     // Start the dwarf ranges section.
     Asm->SwitchToDataSection(TAI->getDwarfRangesSection());
     
-    Asm->EOL("");
+    Asm->EOL();
   }
 
   /// EmitDebugMacInfo - Emit visible names into a debug macinfo section.
@@ -2467,7 +2507,7 @@ private:
     // Start the dwarf macinfo section.
     Asm->SwitchToDataSection(TAI->getDwarfMacInfoSection());
     
-    Asm->EOL("");
+    Asm->EOL();
   }
 
   /// ConstructCompileUnitDIEs - Create a compile unit DIE for each source and
@@ -2521,6 +2561,8 @@ public:
   , DescToUnitMap()
   , SectionMap()
   , SectionSourceLines()
+  , didInitial(false)
+  , shouldEmit(false)
   {
   }
   virtual ~DwarfDebug() {
@@ -2661,41 +2703,94 @@ class DwarfException : public Dwarf  {
 
 private:
 
-  /// EmitInitial - Emit initial exception information.
+  /// didInitial - Flag to indicate if initial emission has been done.
   ///
-  void EmitInitial() {
+  bool didInitial;
+  
+  /// shouldEmit - Flag to indicate if debug information should be emitted.
+  ///
+  bool shouldEmit;
+  
+  /// FuncCPPPersonality - C++ persoanlity function.
+  ///
+  Function *FuncCPPPersonality;
+
+  /// EmitCommonEHFrame - Emit the common eh unwind frame.
+  ///
+  void EmitCommonEHFrame() {
+    // Only do it once.
+    if (didInitial) return;
+    didInitial = true;
+    
+    // If there is a personality present then we need to indicate that
+    // in the common eh frame.
+    Function *Personality = FuncCPPPersonality;
+
+    // Size and sign of stack growth.
     int stackGrowth =
         Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
           TargetFrameInfo::StackGrowsUp ?
         TAI->getAddressSize() : -TAI->getAddressSize();
 
+    // Begin eh frame section.
     Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
     O << "EH_frame:\n";
     EmitLabel("section_eh_frame", 0);
 
+    // Define base labels.
     EmitLabel("eh_frame_common", 0);
+    
+    // Define the eh frame length.
     EmitDifference("eh_frame_common_end", 0,
                    "eh_frame_common_begin", 0, true);
     Asm->EOL("Length of Common Information Entry");
 
+    // EH frame header.
     EmitLabel("eh_frame_common_begin", 0);
     Asm->EmitInt32((int)0);
     Asm->EOL("CIE Identifier Tag");
     Asm->EmitInt8(DW_CIE_VERSION);
     Asm->EOL("CIE Version");
-    Asm->EmitString("zR");
+    
+    // The personality presence indicates that language specific information
+    // will show up in the eh frame.
+    Asm->EmitString(Personality ? "zPLR" : "zR");
     Asm->EOL("CIE Augmentation");
+    
+    // Round out reader.
     Asm->EmitULEB128Bytes(1);
     Asm->EOL("CIE Code Alignment Factor");
     Asm->EmitSLEB128Bytes(stackGrowth);
     Asm->EOL("CIE Data Alignment Factor");   
     Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister()));
     Asm->EOL("CIE RA Column");
-    Asm->EmitULEB128Bytes(1);
-    Asm->EOL("Augmentation Size");
-    Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
-    Asm->EOL("FDE Encoding (pcrel)");
     
+    // If there is a personality, we need to indicate the functions location.
+    if (Personality) {
+      Asm->EmitULEB128Bytes(7);
+      Asm->EOL("Augmentation Size");
+      Asm->EmitInt8(DW_EH_PE_indirect |
+                            DW_EH_PE_pcrel |
+                            DW_EH_PE_sdata4);
+      Asm->EOL("Personality (indirect pcrel sdata4)");
+      
+      O << TAI->getData32bitsDirective();
+      Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
+      O << "-" << TAI->getPCSymbol();
+      Asm->EOL("Personality");
+      
+      Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
+      Asm->EOL("LSDA Encoding (pcrel)");
+      Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
+      Asm->EOL("FDE Encoding (pcrel)");
+   } else {
+      Asm->EmitULEB128Bytes(1);
+      Asm->EOL("Augmentation Size");
+      Asm->EmitULEB128Bytes(DW_EH_PE_pcrel);
+      Asm->EOL("FDE Encoding (pcrel)");
+    }
+
+    // Indicate locations of general callee saved registers in frame.
     std::vector<MachineMove> Moves;
     RI->getInitialFrameState(Moves);
     EmitFrameMoves(NULL, 0, Moves);
@@ -2703,39 +2798,280 @@ private:
     Asm->EmitAlignment(2);
     EmitLabel("eh_frame_common_end", 0);
     
-    Asm->EOL("");
-    
+    Asm->EOL();
   }
   
-  
   /// EmitEHFrame - Emit initial exception information.
   ///
   void EmitEHFrame() {
-    EmitDifference("eh_frame_end", SubprogramCount,
-                   "eh_frame_begin", SubprogramCount, true);
-    Asm->EOL("Length of Frame Information Entry");
-    
-    EmitLabel("eh_frame_begin", SubprogramCount);
+    // If there is a personality present then we need to indicate that
+    // in the common eh frame.
+    Function *Personality = FuncCPPPersonality;
+//    Function *Personality = MMI->getPersonality();
+    MachineFrameInfo *MFI = MF->getFrameInfo();
+
+    Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
+
+    // Externally visible entry into the functions eh frame info.
+    if (const char *GlobalDirective = TAI->getGlobalDirective())
+      O << GlobalDirective << getAsm()->CurrentFnName << ".eh\n";
     
-    EmitDifference("eh_frame_begin", SubprogramCount,
-                   "section_eh_frame", 0, true);
-    Asm->EOL("FDE CIE offset");
+    // If there are no calls then you can't unwind.
+    if (!MFI->hasCalls()) { 
+      O << getAsm()->CurrentFnName << ".eh = 0\n";
+    } else {
+      O << getAsm()->CurrentFnName << ".eh:\n";
+      
+      // EH frame header.
+      EmitDifference("eh_frame_end", SubprogramCount,
+                     "eh_frame_begin", SubprogramCount, true);
+      Asm->EOL("Length of Frame Information Entry");
+      
+      EmitLabel("eh_frame_begin", SubprogramCount);
 
-    EmitReference("eh_func_begin", SubprogramCount, true);
-    Asm->EOL("FDE initial location");
-    EmitDifference("eh_func_end", SubprogramCount,
-                   "eh_func_begin", SubprogramCount);
-    Asm->EOL("FDE address range");
+      EmitSectionOffset("eh_frame_begin", "section_eh_frame",
+                        SubprogramCount, 0, true);
+      Asm->EOL("FDE CIE offset");
+
+      EmitReference("eh_func_begin", SubprogramCount, true);
+      Asm->EOL("FDE initial location");
+      EmitDifference("eh_func_end", SubprogramCount,
+                     "eh_func_begin", SubprogramCount);
+      Asm->EOL("FDE address range");
+      
+      // If there is a personality and landing pads then point to the language
+      // specific data area in the exception table.
+      if (Personality) {
+        Asm->EmitULEB128Bytes(4);
+        Asm->EOL("Augmentation size");
+        
+        if (!MMI->getLandingPads().empty()) {
+          EmitReference("exception", SubprogramCount, true);
+        } else if(TAI->getAddressSize() == 8) {
+          Asm->EmitInt64((int)0);
+        } else {
+          Asm->EmitInt32((int)0);
+        }
+        Asm->EOL("Language Specific Data Area");
+      } else {
+        Asm->EmitULEB128Bytes(0);
+        Asm->EOL("Augmentation size");
+      }
+      
+      // Indicate locations of function specific  callee saved registers in
+      // frame.
+      std::vector<MachineMove> &Moves = MMI->getFrameMoves();
+      EmitFrameMoves("eh_func_begin", SubprogramCount, Moves);
+      
+      Asm->EmitAlignment(2);
+      EmitLabel("eh_frame_end", SubprogramCount);
+    }
     
-    Asm->EmitULEB128Bytes(0);
-    Asm->EOL("Augmentation size");
+    if (const char *UsedDirective = TAI->getUsedDirective())
+      O << UsedDirective << getAsm()->CurrentFnName << ".eh\n\n";
+  }
+  
+  /// EmitExceptionTable - Emit landpads and actions.
+  ///
+  /// The general organization of the table is complex, but the basic concepts
+  /// are easy.  First there is a header which describes the location and
+  /// organization of the three components that follow.
+  ///  1. The landing pad site information describes the range of code covered
+  ///     by the try.  In our case it's an accumulation of the ranges covered
+  ///     by the invokes in the try.  There is also a reference to the landing
+  ///     pad that handles the exception once processed.  Finally an index into
+  ///     the actions table.
+  ///  2. The action table, in our case, is composed of pairs of type ids
+  ///     and next action offset.  Starting with the action index from the
+  ///     landing pad site, each type Id is checked for a match to the current
+  ///     exception.  If it matches then the exception and type id are passed
+  ///     on to the landing pad.  Otherwise the next action is looked up.  This
+  ///     chain is terminated with a next action of zero.  If no type id is
+  ///     found the the frame is unwound and handling continues.
+  ///  3. Type id table contains references to all the C++ typeinfo for all
+  ///     catches in the function.  This tables is reversed indexed base 1.
+  void EmitExceptionTable() {
+    // Map all labels and get rid of any dead landing pads.
+    MMI->TidyLandingPads();
+    
+    const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
+    const std::vector<LandingPadInfo> &LandingPads = MMI->getLandingPads();
+    if (LandingPads.empty()) return;
+    
+    // FIXME - Should fold actions for multiple landing pads.
+    
+    // Gather first action index for each landing pad site.
+    SmallVector<unsigned, 8> Actions;
+    
+    // FIXME - Assume there is only one filter typeinfo list per function
+    // time being.  I.E., Each call to eh_filter will have the same list.
+    // This can change if a function is inlined. 
+    const LandingPadInfo *Filter = 0;
+
+    // Compute sizes for exception table.
+    unsigned SizeHeader = sizeof(int8_t) + // LPStart format
+                          sizeof(int8_t) + // TType format
+                          sizeof(int8_t) + // TType base offset (NEED ULEB128)
+                          sizeof(int8_t) + // Call site format
+                          sizeof(int8_t);  // Call-site table length
+    unsigned SizeSites = 0;
+    unsigned SizeActions = 0;
+
+    // Look at each landing pad site to compute size.  We need the size of each
+    // landing pad site info and the size of the landing pad's actions.
+    for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
+      const LandingPadInfo &LandingPad = LandingPads[i];
+      bool IsFilter = LandingPad.IsFilter;
+      unsigned SizeSiteActions = 0;
+      const std::vector<unsigned> &TypeIds = LandingPad.TypeIds;
+      unsigned SizeAction = 0;
+      signed FirstAction;
+      
+      if (IsFilter) {
+        // FIXME - Assume there is only one filter typeinfo list per function
+        // time being.  I.E., Each call to eh_filter will have the same list.
+        // This can change if a function is inlined. 
+        Filter = &LandingPad;
+        SizeAction =  Asm->SizeSLEB128(-1) + Asm->SizeSLEB128(0);
+        SizeSiteActions += SizeAction;
+        // Record the first action of the landing pad site.
+        FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
+      } else if (TypeIds.empty()) {
+        FirstAction = 0;
+      } else {
+        // Gather the action sizes
+        for (unsigned j = 0, M = TypeIds.size(); j != M; ++j) {
+          unsigned TypeID = TypeIds[i];
+          unsigned SizeTypeID = Asm->SizeSLEB128(TypeID);
+          signed Action = j ? -(SizeAction + SizeTypeID) : 0;
+          SizeAction = SizeTypeID + Asm->SizeSLEB128(Action);
+          SizeSiteActions += SizeAction;
+        }
+        
+        // Record the first action of the landing pad site.
+        FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
+      }
+      
+      Actions.push_back(FirstAction);
+      
+      // Compute this sites contribution to size.
+      SizeActions += SizeSiteActions;
+      SizeSites += sizeof(int32_t) + // Site start.
+                   sizeof(int32_t) + // Site length.
+                   sizeof(int32_t) + // Landing pad.
+                   Asm->SizeSLEB128(FirstAction); // Action.
+    }
     
-    std::vector<MachineMove> &Moves = MMI->getFrameMoves();
+    // Final tallies.
+    unsigned SizeTypes = TypeInfos.size() * TAI->getAddressSize();
+    unsigned SizePreType = SizeHeader + SizeSites + SizeActions;
+    unsigned SizeAlign =  (4 - SizePreType) & 3;
+    unsigned TypeOffset = SizePreType +
+                          SizeTypes +
+                          SizeAlign - 
+                          sizeof(int8_t) - // LPStart format
+                          sizeof(int8_t) - // TType format
+                          sizeof(int8_t);  // TType base offset (NEED ULEB128)
+    
+    // Begin the exception table.
+    Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
+    O << "GCC_except_table" << SubprogramCount << ":\n";
+    EmitLabel("exception", SubprogramCount);
+    
+    // Emit the header.
+    Asm->EmitInt8(DW_EH_PE_omit);
+    Asm->EOL("LPStart format (DW_EH_PE_omit)");
+    Asm->EmitInt8(DW_EH_PE_absptr);
+    Asm->EOL("TType format (DW_EH_PE_absptr)");
+    Asm->EmitULEB128Bytes(TypeOffset);
+    Asm->EOL("TType base offset");
+    Asm->EmitInt8(DW_EH_PE_udata4);
+    Asm->EOL("Call site format (DW_EH_PE_udata4)");
+    Asm->EmitULEB128Bytes(SizeSites);
+    Asm->EOL("Call-site table length");
+    
+    // Emit the landng pad site information.
+    for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
+      const LandingPadInfo &LandingPad = LandingPads[i];
+      EmitSectionOffset("label", "eh_func_begin",
+                        LandingPad.BeginLabel, SubprogramCount);
+      Asm->EOL("Region start");
+      
+      EmitDifference("label", LandingPad.EndLabel,
+                     "label", LandingPad.BeginLabel);
+      Asm->EOL("Region length");
+      
+      if (LandingPad.TypeIds.empty()) {
+        if (TAI->getAddressSize() == sizeof(int32_t))
+          Asm->EmitInt32(0);
+        else
+          Asm->EmitInt64(0);
+      } else {
+        EmitSectionOffset("label", "eh_func_begin",
+                          LandingPad.LandingPadLabel, SubprogramCount);
+      }
+      Asm->EOL("Landing pad");
+
+      Asm->EmitULEB128Bytes(Actions[i]);
+      Asm->EOL("Action");
+    }
     
-    EmitFrameMoves("eh_func_begin", SubprogramCount, Moves);
+    // Emit the actions.
+    for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
+      const LandingPadInfo &LandingPad = LandingPads[i];
+      const std::vector<unsigned> &TypeIds = LandingPad.TypeIds;
+      unsigned SizeAction = 0;
+      
+      if (LandingPad.IsFilter) {
+        Asm->EmitSLEB128Bytes(-1);
+        Asm->EOL("TypeInfo index");
+        Asm->EmitSLEB128Bytes(0);
+        Asm->EOL("Next action");
+      } else {
+        for (unsigned j = 0, M = TypeIds.size(); j < M; ++j) {
+          unsigned TypeID = TypeIds[j];
+          unsigned SizeTypeID = Asm->SizeSLEB128(TypeID);
+          Asm->EmitSLEB128Bytes(TypeID);
+          Asm->EOL("TypeInfo index");
+          signed Action = j ? -(SizeAction + SizeTypeID) : 0;
+          SizeAction = SizeTypeID + Asm->SizeSLEB128(Action);
+          Asm->EmitSLEB128Bytes(Action);
+          Asm->EOL("Next action");
+        }
+      }
+    }
+
+    // Emit the type ids.
+    Asm->EmitAlignment(2);
+    for (unsigned M = TypeInfos.size(); M; --M) {
+      GlobalVariable *GV = TypeInfos[M - 1];
+      
+      if (TAI->getAddressSize() == sizeof(int32_t))
+        O << TAI->getData32bitsDirective();
+      else
+        O << TAI->getData64bitsDirective();
+
+      if (GV)
+        O << Asm->getGlobalLinkName(GV);
+      else
+        O << "0";
+      
+      Asm->EOL("TypeInfo");
+    }
+
+    // Emit the filter typeinfo.
+    if (Filter) {
+      const std::vector<unsigned> &TypeIds = Filter->TypeIds;
+      for (unsigned j = 0, M = TypeIds.size(); j < M; ++j) {
+        unsigned TypeID = TypeIds[j];
+        Asm->EmitSLEB128Bytes(TypeID);
+        Asm->EOL("TypeInfo index");
+      }
+      Asm->EmitSLEB128Bytes(0);
+      Asm->EOL("End of filter typeinfo");
+    }
     
     Asm->EmitAlignment(2);
-    EmitLabel("eh_frame_end", SubprogramCount);
   }
 
 public:
@@ -2744,6 +3080,9 @@ public:
   //
   DwarfException(std::ostream &OS, AsmPrinter *A, const TargetAsmInfo *T)
   : Dwarf(OS, A, T)
+  , didInitial(false)
+  , shouldEmit(false)
+  , FuncCPPPersonality(NULL)
   {}
   
   virtual ~DwarfException() {}
@@ -2751,30 +3090,19 @@ public:
   /// SetModuleInfo - Set machine module information when it's known that pass
   /// manager has created it.  Set by the target AsmPrinter.
   void SetModuleInfo(MachineModuleInfo *mmi) {
-#if 1  // Not ready for prime time.
-    return;
-#endif
-    // Make sure initial declarations are made.
-    if (!MMI && ExceptionHandling && TAI->getSupportsExceptionHandling()) {
-      MMI = mmi;
-      shouldEmit = true;
-      
-      EmitInitial();
-    }
+    MMI = mmi;
   }
 
   /// BeginModule - Emit all exception information that should come prior to the
   /// content.
   void BeginModule(Module *M) {
     this->M = M;
-    
-    if (!ShouldEmitDwarf()) return;
+    FuncCPPPersonality = M->getFunction("__gxx_personality_v0");
   }
 
   /// EndModule - Emit all exception information that should come after the
   /// content.
   void EndModule() {
-    if (!ShouldEmitDwarf()) return;
   }
 
   /// BeginFunction - Gather pre-function exception information.  Assumes being 
@@ -2782,33 +3110,24 @@ public:
   void BeginFunction(MachineFunction *MF) {
     this->MF = MF;
     
-    if (!ShouldEmitDwarf()) return;
-    
-    // Assumes in correct section after the entry point.
-    EmitLabel("eh_func_begin", ++SubprogramCount);
+    if (MMI &&
+        ExceptionHandling &&
+        TAI->getSupportsExceptionHandling()) {
+      shouldEmit = true;
+      // Assumes in correct section after the entry point.
+      EmitLabel("eh_func_begin", ++SubprogramCount);
+    }
   }
 
   /// EndFunction - Gather and emit post-function exception information.
   ///
   void EndFunction() {
-    if (!ShouldEmitDwarf()) return;
+    if (!shouldEmit) return;
 
     EmitLabel("eh_func_end", SubprogramCount);
-
-    Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
-
-    if (const char *GlobalDirective = TAI->getGlobalDirective())
-      O << GlobalDirective << getAsm()->CurrentFnName << ".eh\n";
-     
-    if (0) {
-      O << getAsm()->CurrentFnName << ".eh = 0\n";
-    } else {
-      O << getAsm()->CurrentFnName << ".eh:\n";
-      EmitEHFrame();
-    }
-    
-    if (const char *UsedDirective = TAI->getUsedDirective())
-      O << UsedDirective << getAsm()->CurrentFnName << ".eh\n\n";
+    EmitExceptionTable();
+    EmitCommonEHFrame();
+    EmitEHFrame();
   }
 };
 
@@ -2878,7 +3197,7 @@ void DIEValue::dump() {
 
 /// EmitValue - Emit integer of appropriate size.
 ///
-void DIEInteger::EmitValue(const DwarfDebug &DD, unsigned Form) const {
+void DIEInteger::EmitValue(DwarfDebug &DD, unsigned Form) {
   switch (Form) {
   case DW_FORM_flag:  // Fall thru
   case DW_FORM_ref1:  // Fall thru
@@ -2919,7 +3238,7 @@ unsigned DIEInteger::SizeOf(const DwarfDebug &DD, unsigned Form) const {
 
 /// EmitValue - Emit string value.
 ///
-void DIEString::EmitValue(const DwarfDebug &DD, unsigned Form) const {
+void DIEString::EmitValue(DwarfDebug &DD, unsigned Form) {
   DD.getAsm()->EmitString(String);
 }
 
@@ -2927,7 +3246,7 @@ void DIEString::EmitValue(const DwarfDebug &DD, unsigned Form) const {
 
 /// EmitValue - Emit label value.
 ///
-void DIEDwarfLabel::EmitValue(const DwarfDebug &DD, unsigned Form) const {
+void DIEDwarfLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
   DD.EmitReference(Label);
 }
 
@@ -2941,7 +3260,7 @@ unsigned DIEDwarfLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
 
 /// EmitValue - Emit label value.
 ///
-void DIEObjectLabel::EmitValue(const DwarfDebug &DD, unsigned Form) const {
+void DIEObjectLabel::EmitValue(DwarfDebug &DD, unsigned Form) {
   DD.EmitReference(Label);
 }
 
@@ -2955,7 +3274,7 @@ unsigned DIEObjectLabel::SizeOf(const DwarfDebug &DD, unsigned Form) const {
 
 /// EmitValue - Emit delta value.
 ///
-void DIEDelta::EmitValue(const DwarfDebug &DD, unsigned Form) const {
+void DIEDelta::EmitValue(DwarfDebug &DD, unsigned Form) {
   bool IsSmall = Form == DW_FORM_data4;
   DD.EmitDifference(LabelHi, LabelLo, IsSmall);
 }
@@ -2971,7 +3290,7 @@ unsigned DIEDelta::SizeOf(const DwarfDebug &DD, unsigned Form) const {
 
 /// EmitValue - Emit debug information entry offset.
 ///
-void DIEntry::EmitValue(const DwarfDebug &DD, unsigned Form) const {
+void DIEntry::EmitValue(DwarfDebug &DD, unsigned Form) {
   DD.getAsm()->EmitInt32(Entry->getOffset());
 }
     
@@ -2992,7 +3311,7 @@ unsigned DIEBlock::ComputeSize(DwarfDebug &DD) {
 
 /// EmitValue - Emit block data.
 ///
-void DIEBlock::EmitValue(const DwarfDebug &DD, unsigned Form) const {
+void DIEBlock::EmitValue(DwarfDebug &DD, unsigned Form) {
   switch (Form) {
   case DW_FORM_block1: DD.getAsm()->EmitInt8(Size);         break;
   case DW_FORM_block2: DD.getAsm()->EmitInt16(Size);        break;
@@ -3004,7 +3323,7 @@ void DIEBlock::EmitValue(const DwarfDebug &DD, unsigned Form) const {
   const std::vector<DIEAbbrevData> &AbbrevData = Abbrev.getData();
 
   for (unsigned i = 0, N = Values.size(); i < N; ++i) {
-    DD.getAsm()->EOL("");
+    DD.getAsm()->EOL();
     Values[i]->EmitValue(DD, AbbrevData[i].getForm());
   }
 }
@@ -3154,7 +3473,7 @@ void DwarfWriter::EndFunction() {
   DD->EndFunction();
   DE->EndFunction();
   
-  if (MachineModuleInfo *MMI = DD->getMMI()) {
+  if (MachineModuleInfo *MMI = DD->getMMI() ? DD->getMMI() : DE->getMMI()) {
     // Clear function debug information.
     MMI->EndFunction();
   }