blob: 0f0ad755835dc4ed9bfe7f79777294cfba198f28 [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
Stephen Hines37ed9c12014-12-01 14:51:49 -080014#include "Win64Exception.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)
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070041 : EHStreamer(A), shouldEmitPersonality(false), shouldEmitLSDA(false),
42 shouldEmitMoves(false) {}
Charles Davisd652b132011-05-27 23:47:32 +000043
44Win64Exception::~Win64Exception() {}
45
Stephen Hines36b56882014-04-23 16:57:46 -070046/// endModule - Emit all exception information that should come after the
Charles Davisd652b132011-05-27 23:47:32 +000047/// content.
Stephen Hines36b56882014-04-23 16:57:46 -070048void Win64Exception::endModule() {
Charles Davisd652b132011-05-27 23:47:32 +000049}
50
Stephen Hines36b56882014-04-23 16:57:46 -070051/// beginFunction - Gather pre-function exception information. Assumes it's
Charles Davisd652b132011-05-27 23:47:32 +000052/// being emitted immediately after the function entry point.
Stephen Hines36b56882014-04-23 16:57:46 -070053void Win64Exception::beginFunction(const MachineFunction *MF) {
Charles Davisf4633702011-05-28 04:21:04 +000054 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
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070075 Asm->OutStreamer.EmitWinCFIStartProc(Asm->CurrentFnSym);
Charles Davis12e33492011-05-29 04:28:35 +000076
77 if (!shouldEmitPersonality)
78 return;
79
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070080 const MCSymbol *PersHandlerSym =
81 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
82 Asm->OutStreamer.EmitWinEHHandler(PersHandlerSym, true, true);
Charles Davis59ed4152011-05-30 00:13:34 +000083
84 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
85 Asm->getFunctionNumber()));
Charles Davisd652b132011-05-27 23:47:32 +000086}
87
Stephen Hines36b56882014-04-23 16:57:46 -070088/// endFunction - Gather and emit post-function exception information.
Charles Davisd652b132011-05-27 23:47:32 +000089///
Stephen Hines36b56882014-04-23 16:57:46 -070090void Win64Exception::endFunction(const MachineFunction *) {
Charles Davisf4633702011-05-28 04:21:04 +000091 if (!shouldEmitPersonality && !shouldEmitMoves)
92 return;
93
Charles Davis59ed4152011-05-30 00:13:34 +000094 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) {
Charles Davis59ed4152011-05-30 00:13:34 +0000101 Asm->OutStreamer.PushSection();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700102 Asm->OutStreamer.EmitWinEHHandlerData();
103 emitExceptionTable();
Charles Davis59ed4152011-05-30 00:13:34 +0000104 Asm->OutStreamer.PopSection();
105 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700106 Asm->OutStreamer.EmitWinCFIEndProc();
Charles Davisd652b132011-05-27 23:47:32 +0000107}