blob: 79cce51ee4d4a04455c9e48c55450f611c5e9b01 [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"
Rafael Espindola894843c2014-01-07 21:19:40 +000023#include "llvm/IR/Mangler.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Module.h"
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000025#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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/MC/MachineLocation.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/Dwarf.h"
33#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/FormattedStream.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)
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +000043 : EHStreamer(A), shouldEmitPersonality(false), shouldEmitLSDA(false),
44 shouldEmitMoves(false), moveTypeModule(AsmPrinter::CFI_M_None) {}
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000045
46DwarfCFIException::~DwarfCFIException() {}
47
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000048/// endModule - Emit all exception information that should come after the
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000049/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000050void DwarfCFIException::endModule() {
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000051 if (moveTypeModule == AsmPrinter::CFI_M_Debug)
52 Asm->OutStreamer.EmitCFISections(false, true);
53
Anton Korobeynikov9be547c2011-01-14 21:58:08 +000054 if (!Asm->MAI->isExceptionHandlingDwarf())
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000055 return;
56
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000057 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Rafael Espindola1fc5bf92011-04-29 15:09:53 +000058
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000059 unsigned PerEncoding = TLOF.getPersonalityEncoding();
60
Logan Chienc0029812014-05-30 16:48:56 +000061 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000062 return;
63
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000064 // Emit references to all used personality functions
65 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
66 for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
Rafael Espindola697edc82011-04-29 14:48:51 +000067 if (!Personalities[i])
68 continue;
Rafael Espindola79858aa2013-10-29 17:07:16 +000069 MCSymbol *Sym = Asm->getSymbol(Personalities[i]);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000070 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000071 }
72}
73
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000074/// beginFunction - Gather pre-function exception information. Assumes it's
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000075/// being emitted immediately after the function entry point.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000076void DwarfCFIException::beginFunction(const MachineFunction *MF) {
Rafael Espindola697edc82011-04-29 14:48:51 +000077 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000078
79 // If any landing pads survive, we need an EH table.
Rafael Espindola697edc82011-04-29 14:48:51 +000080 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000081
82 // See if we need frame move info.
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000083 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
84 if (MoveType == AsmPrinter::CFI_M_EH ||
85 (MoveType == AsmPrinter::CFI_M_Debug &&
86 moveTypeModule == AsmPrinter::CFI_M_None))
87 moveTypeModule = MoveType;
88
89 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000090
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000091 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000092 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000093 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
Rafael Espindola697edc82011-04-29 14:48:51 +000094
95 shouldEmitPersonality = hasLandingPads &&
96 PerEncoding != dwarf::DW_EH_PE_omit && Per;
97
98 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
99 shouldEmitLSDA = shouldEmitPersonality &&
100 LSDAEncoding != dwarf::DW_EH_PE_omit;
101
102 if (!shouldEmitPersonality && !shouldEmitMoves)
103 return;
104
David Majnemere035cf92014-01-27 17:20:25 +0000105 Asm->OutStreamer.EmitCFIStartProc(/*IsSimple=*/false);
Rafael Espindola697edc82011-04-29 14:48:51 +0000106
107 // Indicate personality routine, if any.
108 if (!shouldEmitPersonality)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000109 return;
110
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000111 const MCSymbol *Sym =
112 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000113 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola697edc82011-04-29 14:48:51 +0000114
Reed Kotleraee4d5d12012-12-16 04:00:45 +0000115 Asm->OutStreamer.EmitDebugLabel
116 (Asm->GetTempSymbol("eh_func_begin",
117 Asm->getFunctionNumber()));
Rafael Espindola697edc82011-04-29 14:48:51 +0000118
119 // Provide LSDA information.
120 if (!shouldEmitLSDA)
121 return;
122
123 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
124 Asm->getFunctionNumber()),
125 LSDAEncoding);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000126}
127
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000128/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000129///
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000130void DwarfCFIException::endFunction(const MachineFunction *) {
Rafael Espindola697edc82011-04-29 14:48:51 +0000131 if (!shouldEmitPersonality && !shouldEmitMoves)
132 return;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000133
Rafael Espindolaa0761992011-04-24 19:55:34 +0000134 Asm->OutStreamer.EmitCFIEndProc();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000135
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000136 if (!shouldEmitPersonality)
137 return;
138
Rafael Espindolaa6931282012-01-13 23:28:50 +0000139 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
140 Asm->getFunctionNumber()));
141
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000142 // Map all labels and get rid of any dead landing pads.
143 MMI->TidyLandingPads();
144
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000145 emitExceptionTable();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000146}