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 DwarfCFIException::DwarfCFIException(AsmPrinter *A)
43 : EHStreamer(A), shouldEmitPersonality(false), shouldEmitLSDA(false),
44 shouldEmitMoves(false), moveTypeModule(AsmPrinter::CFI_M_None) {}
46 DwarfCFIException::~DwarfCFIException() {}
48 /// endModule - Emit all exception information that should come after the
50 void DwarfCFIException::endModule() {
51 if (moveTypeModule == AsmPrinter::CFI_M_Debug)
52 Asm->OutStreamer.EmitCFISections(false, true);
54 // SjLj uses this pass and it doesn't need this info.
55 if (!Asm->MAI->usesCFIForEH())
58 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
60 unsigned PerEncoding = TLOF.getPersonalityEncoding();
62 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
65 // Emit references to all used personality functions
66 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
67 for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
68 if (!Personalities[i])
70 MCSymbol *Sym = Asm->getSymbol(Personalities[i]);
71 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
75 /// beginFunction - Gather pre-function exception information. Assumes it's
76 /// being emitted immediately after the function entry point.
77 void DwarfCFIException::beginFunction(const MachineFunction *MF) {
78 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
80 // If any landing pads survive, we need an EH table.
81 bool hasLandingPads = !MMI->getLandingPads().empty();
83 // See if we need frame move info.
84 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
85 if (MoveType == AsmPrinter::CFI_M_EH ||
86 (MoveType == AsmPrinter::CFI_M_Debug &&
87 moveTypeModule == AsmPrinter::CFI_M_None))
88 moveTypeModule = MoveType;
90 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
92 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
93 unsigned PerEncoding = TLOF.getPersonalityEncoding();
94 const Function *Per = MMI->getPersonality();
96 shouldEmitPersonality = hasLandingPads &&
97 PerEncoding != dwarf::DW_EH_PE_omit && Per;
99 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
100 shouldEmitLSDA = shouldEmitPersonality &&
101 LSDAEncoding != dwarf::DW_EH_PE_omit;
103 if (!shouldEmitPersonality && !shouldEmitMoves)
106 Asm->OutStreamer.EmitCFIStartProc(/*IsSimple=*/false);
108 // Indicate personality routine, if any.
109 if (!shouldEmitPersonality)
112 const MCSymbol *Sym =
113 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
114 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
117 Asm->GetTempSymbol("eh_func_begin", Asm->getFunctionNumber());
118 if (Asm->MAI->useAssignmentForEHBegin()) {
119 MCContext &Ctx = Asm->OutContext;
120 MCSymbol *CurPos = Ctx.CreateTempSymbol();
121 Asm->OutStreamer.EmitLabel(CurPos);
122 Asm->OutStreamer.EmitAssignment(EHBegin,
123 MCSymbolRefExpr::Create(CurPos, Ctx));
125 Asm->OutStreamer.EmitLabel(EHBegin);
128 // Provide LSDA information.
132 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
133 Asm->getFunctionNumber()),
137 /// endFunction - Gather and emit post-function exception information.
139 void DwarfCFIException::endFunction(const MachineFunction *) {
140 if (!shouldEmitPersonality && !shouldEmitMoves)
143 Asm->OutStreamer.EmitCFIEndProc();
145 if (!shouldEmitPersonality)
148 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
149 Asm->getFunctionNumber()));
151 // Map all labels and get rid of any dead landing pads.
152 MMI->TidyLandingPads();
154 emitExceptionTable();