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/ADT/SmallString.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Mangler.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MachineLocation.h"
32 #include "llvm/Support/Dwarf.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/FormattedStream.h"
35 #include "llvm/Target/TargetFrameLowering.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/Target/TargetRegisterInfo.h"
42 DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
43 : EHStreamer(A), shouldEmitCFI(false) {}
45 void DwarfCFIExceptionBase::markFunctionEnd() {
47 Asm->OutStreamer.EmitCFIEndProc();
49 if (MMI->getLandingPads().empty())
52 // Map all labels and get rid of any dead landing pads.
53 MMI->TidyLandingPads();
56 DwarfCFIException::DwarfCFIException(AsmPrinter *A)
57 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
58 shouldEmitLSDA(false), shouldEmitMoves(false),
59 moveTypeModule(AsmPrinter::CFI_M_None) {}
61 DwarfCFIException::~DwarfCFIException() {}
63 /// endModule - Emit all exception information that should come after the
65 void DwarfCFIException::endModule() {
66 if (moveTypeModule == AsmPrinter::CFI_M_Debug)
67 Asm->OutStreamer.EmitCFISections(false, true);
69 // SjLj uses this pass and it doesn't need this info.
70 if (!Asm->MAI->usesCFIForEH())
73 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
75 unsigned PerEncoding = TLOF.getPersonalityEncoding();
77 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
80 // Emit references to all used personality functions
81 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
82 for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
83 if (!Personalities[i])
85 MCSymbol *Sym = Asm->getSymbol(Personalities[i]);
86 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
90 /// beginFunction - Gather pre-function exception information. Assumes it's
91 /// being emitted immediately after the function entry point.
92 void DwarfCFIException::beginFunction(const MachineFunction *MF) {
93 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
95 // If any landing pads survive, we need an EH table.
96 bool hasLandingPads = !MMI->getLandingPads().empty();
98 // See if we need frame move info.
99 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
100 if (MoveType == AsmPrinter::CFI_M_EH ||
101 (MoveType == AsmPrinter::CFI_M_Debug &&
102 moveTypeModule == AsmPrinter::CFI_M_None))
103 moveTypeModule = MoveType;
105 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
107 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
108 unsigned PerEncoding = TLOF.getPersonalityEncoding();
109 const Function *Per = MMI->getPersonality();
111 shouldEmitPersonality = hasLandingPads &&
112 PerEncoding != dwarf::DW_EH_PE_omit && Per;
114 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
115 shouldEmitLSDA = shouldEmitPersonality &&
116 LSDAEncoding != dwarf::DW_EH_PE_omit;
118 shouldEmitCFI = shouldEmitPersonality || shouldEmitMoves;
122 Asm->OutStreamer.EmitCFIStartProc(/*IsSimple=*/false);
124 // Indicate personality routine, if any.
125 if (!shouldEmitPersonality)
128 const MCSymbol *Sym =
129 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
130 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
132 // Provide LSDA information.
136 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
137 Asm->getFunctionNumber()),
141 /// endFunction - Gather and emit post-function exception information.
143 void DwarfCFIException::endFunction(const MachineFunction *) {
144 if (!shouldEmitPersonality)
147 emitExceptionTable();