blob: f6338e5a4a2fb99e379866090b017e3db65595e3 [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"
22#include "llvm/DataLayout.h"
Charles Davisd652b132011-05-27 23:47:32 +000023#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCExpr.h"
26#include "llvm/MC/MCSection.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/MC/MCSymbol.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Module.h"
30#include "llvm/Support/Dwarf.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/FormattedStream.h"
Charles Davisd652b132011-05-27 23:47:32 +000033#include "llvm/Target/Mangler.h"
Charles Davisd652b132011-05-27 23:47:32 +000034#include "llvm/Target/TargetFrameLowering.h"
35#include "llvm/Target/TargetLoweringObjectFile.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
38#include "llvm/Target/TargetRegisterInfo.h"
Charles Davisd652b132011-05-27 23:47:32 +000039using namespace llvm;
40
41Win64Exception::Win64Exception(AsmPrinter *A)
42 : DwarfException(A),
43 shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false)
44 {}
45
46Win64Exception::~Win64Exception() {}
47
48/// EndModule - Emit all exception information that should come after the
49/// content.
50void Win64Exception::EndModule() {
51}
52
53/// BeginFunction - Gather pre-function exception information. Assumes it's
54/// being emitted immediately after the function entry point.
55void Win64Exception::BeginFunction(const MachineFunction *MF) {
Charles Davisf4633702011-05-28 04:21:04 +000056 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
57
58 // If any landing pads survive, we need an EH table.
59 bool hasLandingPads = !MMI->getLandingPads().empty();
60
61 shouldEmitMoves = Asm->needsSEHMoves();
62
63 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
64 unsigned PerEncoding = TLOF.getPersonalityEncoding();
65 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
66
67 shouldEmitPersonality = hasLandingPads &&
68 PerEncoding != dwarf::DW_EH_PE_omit && Per;
69
70 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
71 shouldEmitLSDA = shouldEmitPersonality &&
72 LSDAEncoding != dwarf::DW_EH_PE_omit;
73
74 if (!shouldEmitPersonality && !shouldEmitMoves)
75 return;
76
77 Asm->OutStreamer.EmitWin64EHStartProc(Asm->CurrentFnSym);
Charles Davis12e33492011-05-29 04:28:35 +000078
79 if (!shouldEmitPersonality)
80 return;
81
82 MCSymbol *GCCHandlerSym =
83 Asm->GetExternalSymbolSymbol("_GCC_specific_handler");
84 Asm->OutStreamer.EmitWin64EHHandler(GCCHandlerSym, true, true);
Charles Davis59ed4152011-05-30 00:13:34 +000085
86 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
87 Asm->getFunctionNumber()));
Charles Davisd652b132011-05-27 23:47:32 +000088}
89
90/// EndFunction - Gather and emit post-function exception information.
91///
92void Win64Exception::EndFunction() {
Charles Davisf4633702011-05-28 04:21:04 +000093 if (!shouldEmitPersonality && !shouldEmitMoves)
94 return;
95
Charles Davis59ed4152011-05-30 00:13:34 +000096 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
97 Asm->getFunctionNumber()));
98
99 // Map all labels and get rid of any dead landing pads.
100 MMI->TidyLandingPads();
101
102 if (shouldEmitPersonality) {
103 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
104 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
105 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, Asm->Mang, MMI);
106
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}