blob: 2c212c7ecee1e7efffff709d9d1bb1c750fac088 [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
Rafael Espindolaa6001792015-03-09 18:29:12 +000042DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
43 : EHStreamer(A), shouldEmitCFI(false) {}
44
45void DwarfCFIExceptionBase::markFunctionEnd() {
46 if (shouldEmitCFI)
Lang Hames9ff69c82015-04-24 19:11:51 +000047 Asm->OutStreamer->EmitCFIEndProc();
Rafael Espindolaa6001792015-03-09 18:29:12 +000048
49 if (MMI->getLandingPads().empty())
50 return;
51
52 // Map all labels and get rid of any dead landing pads.
53 MMI->TidyLandingPads();
54}
55
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000056DwarfCFIException::DwarfCFIException(AsmPrinter *A)
Rafael Espindolaa6001792015-03-09 18:29:12 +000057 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
58 shouldEmitLSDA(false), shouldEmitMoves(false),
59 moveTypeModule(AsmPrinter::CFI_M_None) {}
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000060
61DwarfCFIException::~DwarfCFIException() {}
62
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000063/// endModule - Emit all exception information that should come after the
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000064/// content.
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000065void DwarfCFIException::endModule() {
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000066 if (moveTypeModule == AsmPrinter::CFI_M_Debug)
Lang Hames9ff69c82015-04-24 19:11:51 +000067 Asm->OutStreamer->EmitCFISections(false, true);
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000068
Reid Kleckner5cc15692015-01-23 18:49:01 +000069 // SjLj uses this pass and it doesn't need this info.
70 if (!Asm->MAI->usesCFIForEH())
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000071 return;
72
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000073 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Rafael Espindola1fc5bf92011-04-29 15:09:53 +000074
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000075 unsigned PerEncoding = TLOF.getPersonalityEncoding();
76
Logan Chienc0029812014-05-30 16:48:56 +000077 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +000078 return;
79
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000080 // Emit references to all used personality functions
81 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
82 for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
Rafael Espindola697edc82011-04-29 14:48:51 +000083 if (!Personalities[i])
84 continue;
Rafael Espindola79858aa2013-10-29 17:07:16 +000085 MCSymbol *Sym = Asm->getSymbol(Personalities[i]);
Lang Hames9ff69c82015-04-24 19:11:51 +000086 TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->TM, Sym);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000087 }
88}
89
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +000090void DwarfCFIException::beginFunction(const MachineFunction *MF) {
Rafael Espindola697edc82011-04-29 14:48:51 +000091 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Keno Fischeraff703a2015-07-14 19:22:51 +000092 const Function *F = MF->getFunction();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000093
94 // If any landing pads survive, we need an EH table.
Rafael Espindola697edc82011-04-29 14:48:51 +000095 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +000096
97 // See if we need frame move info.
Rafael Espindolafdc3e6f2011-05-10 18:39:09 +000098 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
99 if (MoveType == AsmPrinter::CFI_M_EH ||
100 (MoveType == AsmPrinter::CFI_M_Debug &&
101 moveTypeModule == AsmPrinter::CFI_M_None))
102 moveTypeModule = MoveType;
103
104 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000105
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000106 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000107 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Keno Fischeraff703a2015-07-14 19:22:51 +0000108 const Function *Per = nullptr;
109 if (F->hasPersonalityFn())
110 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
111 assert(!MMI->getPersonality() || Per == MMI->getPersonality());
Rafael Espindola697edc82011-04-29 14:48:51 +0000112
Keno Fischeraff703a2015-07-14 19:22:51 +0000113 // Emit a personality function even when there are no landing pads
114 bool forceEmitPersonality =
115 // ...if a personality function is explicitly specified
116 F->hasPersonalityFn() &&
117 // ... and it's not known to be a noop in the absence of invokes
118 !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
119 // ... and we're not explicitly asked not to emit it
120 F->needsUnwindTableEntry();
121
122 shouldEmitPersonality =
123 (forceEmitPersonality ||
124 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
125 Per;
Rafael Espindola697edc82011-04-29 14:48:51 +0000126
127 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
128 shouldEmitLSDA = shouldEmitPersonality &&
129 LSDAEncoding != dwarf::DW_EH_PE_omit;
130
Rafael Espindolaa6001792015-03-09 18:29:12 +0000131 shouldEmitCFI = shouldEmitPersonality || shouldEmitMoves;
132 if (!shouldEmitCFI)
Rafael Espindola697edc82011-04-29 14:48:51 +0000133 return;
134
Lang Hames9ff69c82015-04-24 19:11:51 +0000135 Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
Rafael Espindola697edc82011-04-29 14:48:51 +0000136
137 // Indicate personality routine, if any.
138 if (!shouldEmitPersonality)
Rafael Espindolaa01cdb02011-04-15 15:11:06 +0000139 return;
140
Keno Fischeraff703a2015-07-14 19:22:51 +0000141 // If we are forced to emit this personality, make sure to record
142 // it because it might not appear in any landingpad
143 if (forceEmitPersonality)
144 MMI->addPersonality(Per);
145
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000146 const MCSymbol *Sym =
147 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
Lang Hames9ff69c82015-04-24 19:11:51 +0000148 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola697edc82011-04-29 14:48:51 +0000149
Rafael Espindola697edc82011-04-29 14:48:51 +0000150 // Provide LSDA information.
151 if (!shouldEmitLSDA)
152 return;
153
Lang Hames9ff69c82015-04-24 19:11:51 +0000154 Asm->OutStreamer->EmitCFILsda(Asm->getCurExceptionSym(), LSDAEncoding);
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000155}
156
Timur Iskhodzhanov119f3072013-11-26 13:34:55 +0000157/// endFunction - Gather and emit post-function exception information.
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000158///
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000159void DwarfCFIException::endFunction(const MachineFunction *) {
Rafael Espindolacbda0e22012-01-17 04:19:20 +0000160 if (!shouldEmitPersonality)
161 return;
162
Saleem Abdulrasool8076cab2014-06-11 01:19:03 +0000163 emitExceptionTable();
Anton Korobeynikovb46ef572011-01-14 21:57:53 +0000164}