Add 129518 back with a fix for when we are producing eh just because of debug info.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfCFIException.cpp
1 //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing DWARF exception info into asm files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "DwarfException.h"
15 #include "llvm/Module.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineModuleInfo.h"
18 #include "llvm/CodeGen/MachineFrameInfo.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineLocation.h"
21 #include "llvm/MC/MCAsmInfo.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCSection.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Target/Mangler.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetFrameLowering.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Target/TargetOptions.h"
33 #include "llvm/Target/TargetRegisterInfo.h"
34 #include "llvm/Support/Dwarf.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/ADT/SmallString.h"
38 #include "llvm/ADT/StringExtras.h"
39 #include "llvm/ADT/Twine.h"
40 using namespace llvm;
41
42 DwarfCFIException::DwarfCFIException(AsmPrinter *A)
43   : DwarfException(A),
44     shouldEmitTable(false), shouldEmitMoves(false), shouldEmitTableModule(false)
45     {}
46
47 DwarfCFIException::~DwarfCFIException() {}
48
49 /// EndModule - Emit all exception information that should come after the
50 /// content.
51 void DwarfCFIException::EndModule() {
52   if (!Asm->MAI->isExceptionHandlingDwarf())
53     return;
54
55   if (!shouldEmitTableModule)
56     return;
57
58   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
59   unsigned PerEncoding = TLOF.getPersonalityEncoding();
60
61   // Begin eh frame section.
62   Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection());
63
64   if ((PerEncoding & 0x70) != dwarf::DW_EH_PE_pcrel)
65     return;
66
67   // Emit references to all used personality functions
68   const std::vector<const Function*> &Personalities = MMI->getPersonalities();
69   for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
70     Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("personality", i));
71     const MCSymbol *Sym = Asm->Mang->getSymbol(Personalities[i]);
72     unsigned Size = Asm->TM.getTargetData()->getPointerSize();
73     Asm->OutStreamer.EmitSymbolValue(Sym, Size);
74   }
75 }
76
77 /// BeginFunction - Gather pre-function exception information. Assumes it's
78 /// being emitted immediately after the function entry point.
79 void DwarfCFIException::BeginFunction(const MachineFunction *MF) {
80   shouldEmitTable = shouldEmitMoves = false;
81
82   // If any landing pads survive, we need an EH table.
83   shouldEmitTable = !MMI->getLandingPads().empty();
84
85   // See if we need frame move info.
86   shouldEmitMoves = MMI->hasDebugInfo() ||
87     !Asm->MF->getFunction()->doesNotThrow() || UnwindTablesMandatory;
88
89   if (shouldEmitMoves || shouldEmitTable)
90     // Assumes in correct section after the entry point.
91     Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
92                                                   Asm->getFunctionNumber()));
93
94   shouldEmitTableModule |= shouldEmitTable;
95
96   if (shouldEmitMoves || shouldEmitTable)
97     Asm->OutStreamer.EmitCFIStartProc();
98
99   if (!shouldEmitTable)
100     return;
101
102   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
103
104   // Provide LSDA information.
105   unsigned LSDAEncoding = TLOF.getLSDAEncoding();
106   if (LSDAEncoding != dwarf::DW_EH_PE_omit)
107     Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
108                                                     Asm->getFunctionNumber()),
109                                  LSDAEncoding);
110
111   // Indicate personality routine, if any.
112   unsigned PerEncoding = TLOF.getPersonalityEncoding();
113   const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
114   if (PerEncoding == dwarf::DW_EH_PE_omit || !Per)
115     return;
116
117   const MCSymbol *Sym;
118   switch (PerEncoding & 0x70) {
119   default:
120     report_fatal_error("We do not support this DWARF encoding yet!");
121   case dwarf::DW_EH_PE_absptr: {
122     Sym = Asm->Mang->getSymbol(Per);
123     break;
124   }
125   case dwarf::DW_EH_PE_pcrel: {
126     Sym = Asm->GetTempSymbol("personality",
127                              MMI->getPersonalityIndex());
128     break;
129   }
130   }
131   Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
132 }
133
134 /// EndFunction - Gather and emit post-function exception information.
135 ///
136 void DwarfCFIException::EndFunction() {
137   if (!shouldEmitMoves && !shouldEmitTable) return;
138
139   if (shouldEmitMoves)
140     Asm->OutStreamer.EmitCFIEndProc();
141
142   Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
143                                                 Asm->getFunctionNumber()));
144
145   // Map all labels and get rid of any dead landing pads.
146   MMI->TidyLandingPads();
147
148   if (shouldEmitTable)
149     EmitExceptionTable();
150 }