blob: a3bb5768860ddec4402f0bf3d7dd9b9548946f08 [file] [log] [blame]
Anton Korobeynikovb46ef572011-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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/Twine.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000018#include "llvm/CodeGen/AsmPrinter.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Module.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000024#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSymbol.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/MC/MachineLocation.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/Dwarf.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/FormattedStream.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000034#include "llvm/Target/Mangler.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000035#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"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000040using namespace llvm;
41
42DwarfCFIException::DwarfCFIException(AsmPrinter *A)
43 : DwarfException(A),
Rafael Espindolaa6780982011-05-10 21:54:59 +000044 shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false),
45 moveTypeModule(AsmPrinter::CFI_M_None) {}
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000046
47DwarfCFIException::~DwarfCFIException() {}
48
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000049/// endModule - Emit all exception information that should come after the
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000050/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000051void DwarfCFIException::endModule() {
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000052 if (moveTypeModule == AsmPrinter::CFI_M_Debug)
53 Asm->OutStreamer.EmitCFISections(false, true);
54
Anton Korobeynikov9be547c2011-01-14 21:58:08 +000055 if (!Asm->MAI->isExceptionHandlingDwarf())
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000056 return;
57
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000058 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Rafael Espindola1fc5bf92011-04-29 15:09:53 +000059
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000060 unsigned PerEncoding = TLOF.getPersonalityEncoding();
61
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000062 if ((PerEncoding & 0x70) != dwarf::DW_EH_PE_pcrel)
63 return;
64
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000065 // Emit references to all used personality functions
Rafael Espindolae4066702011-05-02 15:49:52 +000066 bool AtLeastOne = false;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000067 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
68 for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
Rafael Espindola697edc82011-04-29 14:48:51 +000069 if (!Personalities[i])
70 continue;
Rafael Espindola79858aa2013-10-29 17:07:16 +000071 MCSymbol *Sym = Asm->getSymbol(Personalities[i]);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000072 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
Rafael Espindolae4066702011-05-02 15:49:52 +000073 AtLeastOne = true;
74 }
75
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.
Evan Cheng76792992011-07-20 05:58:47 +000080 Asm->OutStreamer.SwitchSection(
81 const_cast<TargetLoweringObjectFile&>(TLOF).getEHFrameSection());
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000082 }
83}
84
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000085/// beginFunction - Gather pre-function exception information. Assumes it's
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000086/// being emitted immediately after the function entry point.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000087void DwarfCFIException::beginFunction(const MachineFunction *MF) {
Rafael Espindola697edc82011-04-29 14:48:51 +000088 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000089
90 // If any landing pads survive, we need an EH table.
Rafael Espindola697edc82011-04-29 14:48:51 +000091 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000092
93 // See if we need frame move info.
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000094 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
95 if (MoveType == AsmPrinter::CFI_M_EH ||
96 (MoveType == AsmPrinter::CFI_M_Debug &&
97 moveTypeModule == AsmPrinter::CFI_M_None))
98 moveTypeModule = MoveType;
99
100 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000101
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000102 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000103 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000104 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
Rafael Espindola697edc82011-04-29 14:48:51 +0000105
106 shouldEmitPersonality = hasLandingPads &&
107 PerEncoding != dwarf::DW_EH_PE_omit && Per;
108
109 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
110 shouldEmitLSDA = shouldEmitPersonality &&
111 LSDAEncoding != dwarf::DW_EH_PE_omit;
112
113 if (!shouldEmitPersonality && !shouldEmitMoves)
114 return;
115
116 Asm->OutStreamer.EmitCFIStartProc();
117
118 // Indicate personality routine, if any.
119 if (!shouldEmitPersonality)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000120 return;
121
Rafael Espindolace83fc32011-04-27 23:17:57 +0000122 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, Asm->Mang, MMI);
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000123 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola697edc82011-04-29 14:48:51 +0000124
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000125 Asm->OutStreamer.EmitDebugLabel
126 (Asm->GetTempSymbol("eh_func_begin",
127 Asm->getFunctionNumber()));
Rafael Espindola697edc82011-04-29 14:48:51 +0000128
129 // Provide LSDA information.
130 if (!shouldEmitLSDA)
131 return;
132
133 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
134 Asm->getFunctionNumber()),
135 LSDAEncoding);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000136}
137
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000138/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000139///
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000140void DwarfCFIException::endFunction() {
Rafael Espindola697edc82011-04-29 14:48:51 +0000141 if (!shouldEmitPersonality && !shouldEmitMoves)
142 return;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000143
Rafael Espindolaa0761992011-04-24 19:55:34 +0000144 Asm->OutStreamer.EmitCFIEndProc();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000145
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000146 if (!shouldEmitPersonality)
147 return;
148
Rafael Espindolaa6931282012-01-13 23:28:50 +0000149 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
150 Asm->getFunctionNumber()));
151
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000152 // Map all labels and get rid of any dead landing pads.
153 MMI->TidyLandingPads();
154
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000155 EmitExceptionTable();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000156}