blob: f45b24c17f7cb99e46b4a28270c74928a01e28cd [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
Reid Kleckner5cc15692015-01-23 18:49:01 +000054 // SjLj uses this pass and it doesn't need this info.
55 if (!Asm->MAI->usesCFIForEH())
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
Logan Chienc0029812014-05-30 16:48:56 +000062 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000063 return;
64
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000065 // 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) {
Rafael Espindola697edc82011-04-29 14:48:51 +000068 if (!Personalities[i])
69 continue;
Rafael Espindola79858aa2013-10-29 17:07:16 +000070 MCSymbol *Sym = Asm->getSymbol(Personalities[i]);
Rafael Espindolaa83b1772011-04-16 03:51:21 +000071 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000072 }
73}
74
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000075/// beginFunction - Gather pre-function exception information. Assumes it's
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000076/// being emitted immediately after the function entry point.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000077void DwarfCFIException::beginFunction(const MachineFunction *MF) {
Rafael Espindola697edc82011-04-29 14:48:51 +000078 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000079
80 // If any landing pads survive, we need an EH table.
Rafael Espindola697edc82011-04-29 14:48:51 +000081 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000082
83 // See if we need frame move info.
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000084 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;
89
90 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000091
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000092 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000093 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Reid Klecknere80a0a72015-01-14 22:47:54 +000094 const Function *Per = MMI->getPersonality();
Rafael Espindola697edc82011-04-29 14:48:51 +000095
96 shouldEmitPersonality = hasLandingPads &&
97 PerEncoding != dwarf::DW_EH_PE_omit && Per;
98
99 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
100 shouldEmitLSDA = shouldEmitPersonality &&
101 LSDAEncoding != dwarf::DW_EH_PE_omit;
102
103 if (!shouldEmitPersonality && !shouldEmitMoves)
104 return;
105
Oliver Stannardcf6bfb12014-11-03 12:19:03 +0000106 Asm->OutStreamer.EmitCFIStartProc(/*IsSimple=*/false);
Rafael Espindola697edc82011-04-29 14:48:51 +0000107
108 // Indicate personality routine, if any.
109 if (!shouldEmitPersonality)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000110 return;
111
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000112 const MCSymbol *Sym =
113 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000114 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola697edc82011-04-29 14:48:51 +0000115
Rafael Espindolaf4917042014-06-24 22:45:16 +0000116 MCSymbol *EHBegin =
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));
124 } else {
125 Asm->OutStreamer.EmitLabel(EHBegin);
126 }
Rafael Espindola697edc82011-04-29 14:48:51 +0000127
128 // Provide LSDA information.
129 if (!shouldEmitLSDA)
130 return;
131
132 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
133 Asm->getFunctionNumber()),
134 LSDAEncoding);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000135}
136
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000137/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000138///
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000139void DwarfCFIException::endFunction(const MachineFunction *) {
Rafael Espindola697edc82011-04-29 14:48:51 +0000140 if (!shouldEmitPersonality && !shouldEmitMoves)
141 return;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000142
Rafael Espindolaa0761992011-04-24 19:55:34 +0000143 Asm->OutStreamer.EmitCFIEndProc();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000144
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000145 if (!shouldEmitPersonality)
146 return;
147
Rafael Espindolaa6931282012-01-13 23:28:50 +0000148 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
149 Asm->getFunctionNumber()));
150
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000151 // Map all labels and get rid of any dead landing pads.
152 MMI->TidyLandingPads();
153
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000154 emitExceptionTable();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000155}