1 //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing DWARF exception info into asm files.
12 //===----------------------------------------------------------------------===//
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"
42 DwarfCFIException::DwarfCFIException(AsmPrinter
*A
)
44 shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false),
45 moveTypeModule(AsmPrinter::CFI_M_None
) {}
47 DwarfCFIException::~DwarfCFIException() {}
49 /// EndModule - Emit all exception information that should come after the
51 void DwarfCFIException::EndModule() {
52 if (moveTypeModule
== AsmPrinter::CFI_M_Debug
)
53 Asm
->OutStreamer
.EmitCFISections(false, true);
55 if (!Asm
->MAI
->isExceptionHandlingDwarf())
58 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
60 unsigned PerEncoding
= TLOF
.getPersonalityEncoding();
62 if ((PerEncoding
& 0x70) != dwarf::DW_EH_PE_pcrel
)
65 // Emit references to all used personality functions
66 bool AtLeastOne
= false;
67 const std::vector
<const Function
*> &Personalities
= MMI
->getPersonalities();
68 for (size_t i
= 0, e
= Personalities
.size(); i
!= e
; ++i
) {
69 if (!Personalities
[i
])
71 MCSymbol
*Sym
= Asm
->Mang
->getSymbol(Personalities
[i
]);
72 TLOF
.emitPersonalityValue(Asm
->OutStreamer
, Asm
->TM
, Sym
);
76 if (AtLeastOne
&& !TLOF
.isFunctionEHFrameSymbolPrivate()) {
77 // This is a temporary hack to keep sections in the same order they
78 // were before. This lets us produce bit identical outputs while
79 // transitioning to CFI.
80 Asm
->OutStreamer
.SwitchSection(TLOF
.getEHFrameSection());
84 /// BeginFunction - Gather pre-function exception information. Assumes it's
85 /// being emitted immediately after the function entry point.
86 void DwarfCFIException::BeginFunction(const MachineFunction
*MF
) {
87 shouldEmitMoves
= shouldEmitPersonality
= shouldEmitLSDA
= false;
89 // If any landing pads survive, we need an EH table.
90 bool hasLandingPads
= !MMI
->getLandingPads().empty();
92 // See if we need frame move info.
93 AsmPrinter::CFIMoveType MoveType
= Asm
->needsCFIMoves();
94 if (MoveType
== AsmPrinter::CFI_M_EH
||
95 (MoveType
== AsmPrinter::CFI_M_Debug
&&
96 moveTypeModule
== AsmPrinter::CFI_M_None
))
97 moveTypeModule
= MoveType
;
99 shouldEmitMoves
= MoveType
!= AsmPrinter::CFI_M_None
;
101 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
102 unsigned PerEncoding
= TLOF
.getPersonalityEncoding();
103 const Function
*Per
= MMI
->getPersonalities()[MMI
->getPersonalityIndex()];
105 shouldEmitPersonality
= hasLandingPads
&&
106 PerEncoding
!= dwarf::DW_EH_PE_omit
&& Per
;
108 unsigned LSDAEncoding
= TLOF
.getLSDAEncoding();
109 shouldEmitLSDA
= shouldEmitPersonality
&&
110 LSDAEncoding
!= dwarf::DW_EH_PE_omit
;
112 if (!shouldEmitPersonality
&& !shouldEmitMoves
)
115 Asm
->OutStreamer
.EmitCFIStartProc();
117 // Indicate personality routine, if any.
118 if (!shouldEmitPersonality
)
121 const MCSymbol
*Sym
= TLOF
.getCFIPersonalitySymbol(Per
, Asm
->Mang
, MMI
);
122 Asm
->OutStreamer
.EmitCFIPersonality(Sym
, PerEncoding
);
124 Asm
->OutStreamer
.EmitLabel(Asm
->GetTempSymbol("eh_func_begin",
125 Asm
->getFunctionNumber()));
127 // Provide LSDA information.
131 Asm
->OutStreamer
.EmitCFILsda(Asm
->GetTempSymbol("exception",
132 Asm
->getFunctionNumber()),
136 /// EndFunction - Gather and emit post-function exception information.
138 void DwarfCFIException::EndFunction() {
139 if (!shouldEmitPersonality
&& !shouldEmitMoves
)
142 Asm
->OutStreamer
.EmitCFIEndProc();
144 Asm
->OutStreamer
.EmitLabel(Asm
->GetTempSymbol("eh_func_end",
145 Asm
->getFunctionNumber()));
147 // Map all labels and get rid of any dead landing pads.
148 MMI
->TidyLandingPads();
150 if (shouldEmitPersonality
)
151 EmitExceptionTable();