blob: 17d8bff6092d7baab1588090b4b00ae192fc169c [file] [log] [blame]
Charles Davisd652b132011-05-27 23:47:32 +00001//===-- CodeGen/AsmPrinter/Win64Exception.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 Win64 exception info into asm files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "DwarfException.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/Twine.h"
Charles Davisd652b132011-05-27 23:47:32 +000018#include "llvm/CodeGen/AsmPrinter.h"
Charles Davisd652b132011-05-27 23:47:32 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000022#include "llvm/IR/DataLayout.h"
Stephen Hines36b56882014-04-23 16:57:46 -070023#include "llvm/IR/Mangler.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/Module.h"
Charles Davisd652b132011-05-27 23:47:32 +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 Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Support/Dwarf.h"
32#include "llvm/Support/ErrorHandling.h"
33#include "llvm/Support/FormattedStream.h"
Charles Davisd652b132011-05-27 23:47:32 +000034#include "llvm/Target/TargetFrameLowering.h"
35#include "llvm/Target/TargetLoweringObjectFile.h"
Charles Davisd652b132011-05-27 23:47:32 +000036#include "llvm/Target/TargetOptions.h"
37#include "llvm/Target/TargetRegisterInfo.h"
Charles Davisd652b132011-05-27 23:47:32 +000038using namespace llvm;
39
40Win64Exception::Win64Exception(AsmPrinter *A)
41 : DwarfException(A),
42 shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false)
43 {}
44
45Win64Exception::~Win64Exception() {}
46
Stephen Hines36b56882014-04-23 16:57:46 -070047/// endModule - Emit all exception information that should come after the
Charles Davisd652b132011-05-27 23:47:32 +000048/// content.
Stephen Hines36b56882014-04-23 16:57:46 -070049void Win64Exception::endModule() {
Charles Davisd652b132011-05-27 23:47:32 +000050}
51
Stephen Hines36b56882014-04-23 16:57:46 -070052/// beginFunction - Gather pre-function exception information. Assumes it's
Charles Davisd652b132011-05-27 23:47:32 +000053/// being emitted immediately after the function entry point.
Stephen Hines36b56882014-04-23 16:57:46 -070054void Win64Exception::beginFunction(const MachineFunction *MF) {
Charles Davisf4633702011-05-28 04:21:04 +000055 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
56
57 // If any landing pads survive, we need an EH table.
58 bool hasLandingPads = !MMI->getLandingPads().empty();
59
60 shouldEmitMoves = Asm->needsSEHMoves();
61
62 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
63 unsigned PerEncoding = TLOF.getPersonalityEncoding();
64 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
65
66 shouldEmitPersonality = hasLandingPads &&
67 PerEncoding != dwarf::DW_EH_PE_omit && Per;
68
69 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
70 shouldEmitLSDA = shouldEmitPersonality &&
71 LSDAEncoding != dwarf::DW_EH_PE_omit;
72
73 if (!shouldEmitPersonality && !shouldEmitMoves)
74 return;
75
76 Asm->OutStreamer.EmitWin64EHStartProc(Asm->CurrentFnSym);
Charles Davis12e33492011-05-29 04:28:35 +000077
78 if (!shouldEmitPersonality)
79 return;
80
81 MCSymbol *GCCHandlerSym =
82 Asm->GetExternalSymbolSymbol("_GCC_specific_handler");
83 Asm->OutStreamer.EmitWin64EHHandler(GCCHandlerSym, true, true);
Charles Davis59ed4152011-05-30 00:13:34 +000084
85 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
86 Asm->getFunctionNumber()));
Charles Davisd652b132011-05-27 23:47:32 +000087}
88
Stephen Hines36b56882014-04-23 16:57:46 -070089/// endFunction - Gather and emit post-function exception information.
Charles Davisd652b132011-05-27 23:47:32 +000090///
Stephen Hines36b56882014-04-23 16:57:46 -070091void Win64Exception::endFunction(const MachineFunction *) {
Charles Davisf4633702011-05-28 04:21:04 +000092 if (!shouldEmitPersonality && !shouldEmitMoves)
93 return;
94
Charles Davis59ed4152011-05-30 00:13:34 +000095 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
96 Asm->getFunctionNumber()));
97
98 // Map all labels and get rid of any dead landing pads.
99 MMI->TidyLandingPads();
100
101 if (shouldEmitPersonality) {
102 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
103 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
Stephen Hines36b56882014-04-23 16:57:46 -0700104 const MCSymbol *Sym =
105 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
Charles Davis59ed4152011-05-30 00:13:34 +0000106
107 Asm->OutStreamer.PushSection();
108 Asm->OutStreamer.EmitWin64EHHandlerData();
109 Asm->OutStreamer.EmitValue(MCSymbolRefExpr::Create(Sym, Asm->OutContext),
110 4);
111 EmitExceptionTable();
112 Asm->OutStreamer.PopSection();
113 }
Charles Davisf4633702011-05-28 04:21:04 +0000114 Asm->OutStreamer.EmitWin64EHEndProc();
Charles Davisd652b132011-05-27 23:47:32 +0000115}