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/ADT/SmallString.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/CodeGen/AsmPrinter.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Module.h"
24#include "llvm/MC/MCAsmInfo.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCExpr.h"
27#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSymbol.h"
30#include "llvm/Support/Dwarf.h"
31#include "llvm/Support/ErrorHandling.h"
32#include "llvm/Support/FormattedStream.h"
33#include "llvm/Target/Mangler.h"
34#include "llvm/Target/TargetFrameLowering.h"
35#include "llvm/Target/TargetLoweringObjectFile.h"
36#include "llvm/Target/TargetOptions.h"
37#include "llvm/Target/TargetRegisterInfo.h"
38using namespace llvm;
39
40Win64Exception::Win64Exception(AsmPrinter *A)
41  : DwarfException(A),
42    shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false)
43    {}
44
45Win64Exception::~Win64Exception() {}
46
47/// EndModule - Emit all exception information that should come after the
48/// content.
49void Win64Exception::EndModule() {
50}
51
52/// BeginFunction - Gather pre-function exception information. Assumes it's
53/// being emitted immediately after the function entry point.
54void Win64Exception::BeginFunction(const MachineFunction *MF) {
55  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);
77
78  if (!shouldEmitPersonality)
79    return;
80
81  MCSymbol *GCCHandlerSym =
82    Asm->GetExternalSymbolSymbol("_GCC_specific_handler");
83  Asm->OutStreamer.EmitWin64EHHandler(GCCHandlerSym, true, true);
84
85  Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
86                                                Asm->getFunctionNumber()));
87}
88
89/// EndFunction - Gather and emit post-function exception information.
90///
91void Win64Exception::EndFunction() {
92  if (!shouldEmitPersonality && !shouldEmitMoves)
93    return;
94
95  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()];
104    const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, Asm->Mang, MMI);
105
106    Asm->OutStreamer.PushSection();
107    Asm->OutStreamer.EmitWin64EHHandlerData();
108    Asm->OutStreamer.EmitValue(MCSymbolRefExpr::Create(Sym, Asm->OutContext),
109                               4);
110    EmitExceptionTable();
111    Asm->OutStreamer.PopSection();
112  }
113  Asm->OutStreamer.EmitWin64EHEndProc();
114}
115