blob: 7676ef8355a687430fbe9819b58f68d7eb8d81f2 [file] [log] [blame]
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +00001//===-- 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"
Rafael Espindolaf0adba92011-04-15 15:11:06 +000035#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000036#include "llvm/Support/FormattedStream.h"
37#include "llvm/ADT/SmallString.h"
38#include "llvm/ADT/StringExtras.h"
39#include "llvm/ADT/Twine.h"
40using namespace llvm;
41
42DwarfCFIException::DwarfCFIException(AsmPrinter *A)
43 : DwarfException(A),
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000044 shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false)
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000045 {}
46
47DwarfCFIException::~DwarfCFIException() {}
48
49/// EndModule - Emit all exception information that should come after the
50/// content.
51void DwarfCFIException::EndModule() {
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +000052 if (!Asm->MAI->isExceptionHandlingDwarf())
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000053 return;
54
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000055 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
56 unsigned PerEncoding = TLOF.getPersonalityEncoding();
57
Rafael Espindolaf0adba92011-04-15 15:11:06 +000058 if ((PerEncoding & 0x70) != dwarf::DW_EH_PE_pcrel)
59 return;
60
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000061 // Emit references to all used personality functions
62 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
63 for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000064 if (!Personalities[i])
65 continue;
66 MCSymbol *Sym = Asm->Mang->getSymbol(Personalities[i]);
Rafael Espindola30deafc2011-04-16 03:51:21 +000067 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000068 }
69}
70
71/// BeginFunction - Gather pre-function exception information. Assumes it's
72/// being emitted immediately after the function entry point.
73void DwarfCFIException::BeginFunction(const MachineFunction *MF) {
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000074 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000075
76 // If any landing pads survive, we need an EH table.
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000077 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000078
79 // See if we need frame move info.
Rafael Espindolaa4329972011-04-29 14:14:06 +000080 shouldEmitMoves = Asm->needsCFIMoves();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000081
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000082 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000083 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Rafael Espindolaf0adba92011-04-15 15:11:06 +000084 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000085
86 shouldEmitPersonality = hasLandingPads &&
87 PerEncoding != dwarf::DW_EH_PE_omit && Per;
88
89 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
90 shouldEmitLSDA = shouldEmitPersonality &&
91 LSDAEncoding != dwarf::DW_EH_PE_omit;
92
93 if (!shouldEmitPersonality && !shouldEmitMoves)
94 return;
95
96 Asm->OutStreamer.EmitCFIStartProc();
97
98 // Indicate personality routine, if any.
99 if (!shouldEmitPersonality)
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000100 return;
101
Rafael Espindola60246a92011-04-27 23:17:57 +0000102 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, Asm->Mang, MMI);
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000103 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola7b11a4c2011-04-29 14:48:51 +0000104
105 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
106 Asm->getFunctionNumber()));
107
108 // Provide LSDA information.
109 if (!shouldEmitLSDA)
110 return;
111
112 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
113 Asm->getFunctionNumber()),
114 LSDAEncoding);
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000115}
116
117/// EndFunction - Gather and emit post-function exception information.
118///
119void DwarfCFIException::EndFunction() {
Rafael Espindola7b11a4c2011-04-29 14:48:51 +0000120 if (!shouldEmitPersonality && !shouldEmitMoves)
121 return;
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000122
Rafael Espindolae54e7852011-04-24 19:55:34 +0000123 Asm->OutStreamer.EmitCFIEndProc();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000124
125 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
126 Asm->getFunctionNumber()));
127
128 // Map all labels and get rid of any dead landing pads.
129 MMI->TidyLandingPads();
130
Rafael Espindola7b11a4c2011-04-29 14:48:51 +0000131 if (shouldEmitPersonality)
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000132 EmitExceptionTable();
133}