Win64Exception.cpp revision 59ed4151d8d0567b7771772cfa2b65e7c25ffb68
1//===-- 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"
15#include "llvm/Module.h"
16#include "llvm/CodeGen/AsmPrinter.h"
17#include "llvm/CodeGen/MachineModuleInfo.h"
18#include "llvm/CodeGen/MachineFrameInfo.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/MachineLocation.h"
21#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/MC/MCContext.h"
23#include "llvm/MC/MCExpr.h"
24#include "llvm/MC/MCSection.h"
25#include "llvm/MC/MCStreamer.h"
26#include "llvm/MC/MCSymbol.h"
27#include "llvm/Target/Mangler.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetFrameLowering.h"
30#include "llvm/Target/TargetLoweringObjectFile.h"
31#include "llvm/Target/TargetMachine.h"
32#include "llvm/Target/TargetOptions.h"
33#include "llvm/Target/TargetRegisterInfo.h"
34#include "llvm/Support/Dwarf.h"
35#include "llvm/Support/ErrorHandling.h"
36#include "llvm/Support/FormattedStream.h"
37#include "llvm/ADT/SmallString.h"
38#include "llvm/ADT/StringExtras.h"
39#include "llvm/ADT/Twine.h"
40using namespace llvm;
41
42Win64Exception::Win64Exception(AsmPrinter *A)
43  : DwarfException(A),
44    shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false)
45    {}
46
47Win64Exception::~Win64Exception() {}
48
49/// EndModule - Emit all exception information that should come after the
50/// content.
51void Win64Exception::EndModule() {
52}
53
54/// BeginFunction - Gather pre-function exception information. Assumes it's
55/// being emitted immediately after the function entry point.
56void Win64Exception::BeginFunction(const MachineFunction *MF) {
57  shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
58
59  // If any landing pads survive, we need an EH table.
60  bool hasLandingPads = !MMI->getLandingPads().empty();
61
62  shouldEmitMoves = Asm->needsSEHMoves();
63
64  const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
65  unsigned PerEncoding = TLOF.getPersonalityEncoding();
66  const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
67
68  shouldEmitPersonality = hasLandingPads &&
69    PerEncoding != dwarf::DW_EH_PE_omit && Per;
70
71  unsigned LSDAEncoding = TLOF.getLSDAEncoding();
72  shouldEmitLSDA = shouldEmitPersonality &&
73    LSDAEncoding != dwarf::DW_EH_PE_omit;
74
75  if (!shouldEmitPersonality && !shouldEmitMoves)
76    return;
77
78  Asm->OutStreamer.EmitWin64EHStartProc(Asm->CurrentFnSym);
79
80  if (!shouldEmitPersonality)
81    return;
82
83  MCSymbol *GCCHandlerSym =
84    Asm->GetExternalSymbolSymbol("_GCC_specific_handler");
85  Asm->OutStreamer.EmitWin64EHHandler(GCCHandlerSym, true, true);
86
87  Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
88                                                Asm->getFunctionNumber()));
89}
90
91/// EndFunction - Gather and emit post-function exception information.
92///
93void Win64Exception::EndFunction() {
94  if (!shouldEmitPersonality && !shouldEmitMoves)
95    return;
96
97  Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
98                                                Asm->getFunctionNumber()));
99
100  // Map all labels and get rid of any dead landing pads.
101  MMI->TidyLandingPads();
102
103  if (shouldEmitPersonality) {
104    const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
105    const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
106    const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, Asm->Mang, MMI);
107
108    Asm->OutStreamer.PushSection();
109    Asm->OutStreamer.EmitWin64EHHandlerData();
110    Asm->OutStreamer.EmitValue(MCSymbolRefExpr::Create(Sym, Asm->OutContext),
111                               4);
112    EmitExceptionTable();
113    Asm->OutStreamer.PopSection();
114  }
115  Asm->OutStreamer.EmitWin64EHEndProc();
116}
117