CodeGen: split out the Win64Exception emitter
[oota-llvm.git] / lib / CodeGen / AsmPrinter / Win64Exception.cpp
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 "Win64Exception.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/Mangler.h"
24 #include "llvm/IR/Module.h"
25 #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"
31 #include "llvm/Support/Dwarf.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/FormattedStream.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"
38 using namespace llvm;
39
40 Win64Exception::Win64Exception(AsmPrinter *A)
41   : EHStreamer(A), shouldEmitPersonality(false), shouldEmitLSDA(false),
42     shouldEmitMoves(false) {}
43
44 Win64Exception::~Win64Exception() {}
45
46 /// endModule - Emit all exception information that should come after the
47 /// content.
48 void Win64Exception::endModule() {
49 }
50
51 /// beginFunction - Gather pre-function exception information. Assumes it's
52 /// being emitted immediately after the function entry point.
53 void Win64Exception::beginFunction(const MachineFunction *MF) {
54   shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
55
56   // If any landing pads survive, we need an EH table.
57   bool hasLandingPads = !MMI->getLandingPads().empty();
58
59   shouldEmitMoves = Asm->needsSEHMoves();
60
61   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
62   unsigned PerEncoding = TLOF.getPersonalityEncoding();
63   const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
64
65   shouldEmitPersonality = hasLandingPads &&
66     PerEncoding != dwarf::DW_EH_PE_omit && Per;
67
68   unsigned LSDAEncoding = TLOF.getLSDAEncoding();
69   shouldEmitLSDA = shouldEmitPersonality &&
70     LSDAEncoding != dwarf::DW_EH_PE_omit;
71
72   if (!shouldEmitPersonality && !shouldEmitMoves)
73     return;
74
75   Asm->OutStreamer.EmitWinCFIStartProc(Asm->CurrentFnSym);
76
77   if (!shouldEmitPersonality)
78     return;
79
80   const MCSymbol *PersHandlerSym =
81       TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
82   Asm->OutStreamer.EmitWinEHHandler(PersHandlerSym, true, true);
83
84   Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
85                                                 Asm->getFunctionNumber()));
86 }
87
88 /// endFunction - Gather and emit post-function exception information.
89 ///
90 void Win64Exception::endFunction(const MachineFunction *) {
91   if (!shouldEmitPersonality && !shouldEmitMoves)
92     return;
93
94   Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
95                                                 Asm->getFunctionNumber()));
96
97   // Map all labels and get rid of any dead landing pads.
98   MMI->TidyLandingPads();
99
100   if (shouldEmitPersonality) {
101     Asm->OutStreamer.PushSection();
102     Asm->OutStreamer.EmitWinEHHandlerData();
103     emitExceptionTable();
104     Asm->OutStreamer.PopSection();
105   }
106   Asm->OutStreamer.EmitWinCFIEndProc();
107 }