Strip trailing whitespace.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfException.cpp
index abd2e6fe80d30bb80fda99a3ea772f5cd689d0f7..d6000e66f0648d250294a85114e9ac3edd89beb8 100644 (file)
 #include "llvm/CodeGen/MachineFrameInfo.h"
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineLocation.h"
+#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCContext.h"
+#include "llvm/MC/MCExpr.h"
 #include "llvm/MC/MCSection.h"
 #include "llvm/MC/MCStreamer.h"
-#include "llvm/MC/MCAsmInfo.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetFrameInfo.h"
 #include "llvm/Target/TargetLoweringObjectFile.h"
@@ -72,23 +74,44 @@ unsigned DwarfException::SizeOfEncodedValue(unsigned Encoding) {
   return 0;
 }
 
+/// CreateLabelDiff - Emit a label and subtract it from the expression we
+/// already have.  This is equivalent to emitting "foo - .", but we have to emit
+/// the label for "." directly.
+const MCExpr *DwarfException::CreateLabelDiff(const MCExpr *ExprRef,
+                                              const char *LabelName,
+                                              unsigned Index) {
+  SmallString<64> Name;
+  raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
+                            << LabelName << Asm->getFunctionNumber()
+                            << "_" << Index;
+  MCSymbol *DotSym = Asm->OutContext.GetOrCreateSymbol(Name.str());
+  Asm->OutStreamer.EmitLabel(DotSym);
+
+  return MCBinaryExpr::CreateSub(ExprRef,
+                                 MCSymbolRefExpr::Create(DotSym,
+                                                         Asm->OutContext),
+                                 Asm->OutContext);
+}
+
 /// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
 /// is shared among many Frame Description Entries.  There is at least one CIE
 /// in every non-empty .debug_frame section.
-void DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
+void DwarfException::EmitCIE(const Function *PersonalityFn, unsigned Index) {
   // Size and sign of stack growth.
   int stackGrowth =
     Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
     TargetFrameInfo::StackGrowsUp ?
     TD->getPointerSize() : -TD->getPointerSize();
 
+  const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
+  
   // Begin eh frame section.
-  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
+  Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection());
 
   if (MAI->is_EHSymbolPrivate())
     O << MAI->getPrivateGlobalPrefix();
-
   O << "EH_frame" << Index << ":\n";
+  
   EmitLabel("section_eh_frame", Index);
 
   // Define base labels.
@@ -107,11 +130,23 @@ void DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
   Asm->EOL("CIE Version");
 
   // The personality presence indicates that language specific information will
-  // show up in the eh frame.
-
-  // FIXME: Don't hardcode these encodings.
+  // show up in the eh frame.  Find out how we are supposed to lower the
+  // personality function reference:
+  const MCExpr *PersonalityRef = 0;
+  bool IsPersonalityIndirect = false, IsPersonalityPCRel = false;
+  if (PersonalityFn) {
+    // FIXME: HANDLE STATIC CODEGEN MODEL HERE.
+    
+    // In non-static mode, ask the object file how to represent this reference.
+    PersonalityRef =
+      TLOF.getSymbolForDwarfGlobalReference(PersonalityFn, Asm->Mang,
+                                            Asm->MMI,
+                                            IsPersonalityIndirect,
+                                            IsPersonalityPCRel);
+  }
+  
   unsigned PerEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
-  if (Personality && MAI->getNeedsIndirectEncoding())
+  if (IsPersonalityIndirect)
     PerEncoding |= dwarf::DW_EH_PE_indirect;
   unsigned LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
   unsigned FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
@@ -120,7 +155,7 @@ void DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
   unsigned AugmentationSize = 0;
   char *APtr = Augmentation + 1;
 
-  if (Personality) {
+  if (PersonalityRef) {
     // There is a personality function.
     *APtr++ = 'P';
     AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding);
@@ -159,15 +194,13 @@ void DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
   Asm->EOL("Personality", PerEncoding);
 
   // If there is a personality, we need to indicate the function's location.
-  if (Personality) {
-    O << MAI->getData32bitsDirective();
+  if (PersonalityRef) {
+    if (!IsPersonalityPCRel)
+      PersonalityRef = CreateLabelDiff(PersonalityRef, "personalityref_addr",
+                                       Index);
     
-    O << MAI->getPersonalityPrefix();
-    Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
-    O << MAI->getPersonalitySuffix();
-    
-    if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL"))
-      O << "-" << MAI->getPCSymbol();
+    O << MAI->getData32bitsDirective();
+    PersonalityRef->print(O, MAI);
     Asm->EOL("Personality");
 
     Asm->EmitInt8(LSDAEncoding);
@@ -185,8 +218,7 @@ void DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
   // On Darwin the linker honors the alignment of eh_frame, which means it must
   // be 8-byte on 64-bit targets to match what gcc does.  Otherwise you get
   // holes which confuse readers of eh_frame.
-  Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
-                     0, 0, false);
+  Asm->EmitAlignment(TD->getPointerSize() == 4 ? 2 : 3, 0, 0, false);
   EmitLabel("eh_frame_common_end", Index);
 
   Asm->EOL();
@@ -866,14 +898,33 @@ void DwarfException::EmitExceptionTable() {
   // of the catch clauses as they appear in the source code, and must be kept in
   // the same order. As a result, changing the order of the catch clause would
   // change the semantics of the program.
+  const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
+  unsigned Index = 1;
+
   for (std::vector<GlobalVariable *>::const_reverse_iterator
-         I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
-    const GlobalVariable *GV = *I;
-    PrintRelDirective();
+         I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I, ++Index) {
+    const GlobalVariable *TI = *I;
+
+    if (TI) {
+      if (TTypeFormat == dwarf::DW_EH_PE_absptr) {
+        // Print out the unadorned name of the type info.
+        PrintRelDirective();
+        O << Asm->Mang->getMangledName(TI);
+      } else {
+        bool IsTypeInfoIndirect = false, IsTypeInfoPCRel = false;
+        const MCExpr *TypeInfoRef =
+          TLOF.getSymbolForDwarfGlobalReference(TI, Asm->Mang, Asm->MMI,
+                                                IsTypeInfoIndirect,
+                                                IsTypeInfoPCRel);
 
-    if (GV) {
-      O << Asm->Mang->getMangledName(GV);
+        if (!IsTypeInfoPCRel)
+          TypeInfoRef = CreateLabelDiff(TypeInfoRef, "typeinforef_addr", Index);
+
+        O << MAI->getData32bitsDirective();
+        TypeInfoRef->print(O, MAI);
+      }
     } else {
+      PrintRelDirective();
       O << "0x0";
     }
 
@@ -960,9 +1011,12 @@ void DwarfException::EndFunction() {
   EmitLabel("eh_func_end", SubprogramCount);
   EmitExceptionTable();
 
+  std::string FunctionEHName =
+    Asm->Mang->getMangledName(MF->getFunction(), ".eh",
+                              Asm->MAI->is_EHSymbolPrivate());
+  
   // Save EH frame information
-  EHFrames.push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
-                                         SubprogramCount,
+  EHFrames.push_back(FunctionEHFrameInfo(FunctionEHName, SubprogramCount,
                                          MMI->getPersonalityIndex(),
                                          MF->getFrameInfo()->hasCalls(),
                                          !MMI->getLandingPads().empty(),