Support standard DWARF TLS opcode; Darwin and PS4 use it.
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfCFIException.cpp
1 //===-- CodeGen/AsmPrinter/DwarfException.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 DWARF 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/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/MC/MachineLocation.h"
32 #include "llvm/Support/Dwarf.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/FormattedStream.h"
35 #include "llvm/Target/TargetFrameLowering.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/Target/TargetRegisterInfo.h"
40 using namespace llvm;
41
42 DwarfCFIException::DwarfCFIException(AsmPrinter *A)
43   : EHStreamer(A), shouldEmitPersonality(false), shouldEmitLSDA(false),
44     shouldEmitMoves(false), moveTypeModule(AsmPrinter::CFI_M_None) {}
45
46 DwarfCFIException::~DwarfCFIException() {}
47
48 /// endModule - Emit all exception information that should come after the
49 /// content.
50 void DwarfCFIException::endModule() {
51   if (moveTypeModule == AsmPrinter::CFI_M_Debug)
52     Asm->OutStreamer.EmitCFISections(false, true);
53
54   // SjLj uses this pass and it doesn't need this info.
55   if (!Asm->MAI->usesCFIForEH())
56     return;
57
58   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
59
60   unsigned PerEncoding = TLOF.getPersonalityEncoding();
61
62   if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
63     return;
64
65   // Emit references to all used personality functions
66   const std::vector<const Function*> &Personalities = MMI->getPersonalities();
67   for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
68     if (!Personalities[i])
69       continue;
70     MCSymbol *Sym = Asm->getSymbol(Personalities[i]);
71     TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
72   }
73 }
74
75 /// beginFunction - Gather pre-function exception information. Assumes it's
76 /// being emitted immediately after the function entry point.
77 void DwarfCFIException::beginFunction(const MachineFunction *MF) {
78   shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
79
80   // If any landing pads survive, we need an EH table.
81   bool hasLandingPads = !MMI->getLandingPads().empty();
82
83   // See if we need frame move info.
84   AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
85   if (MoveType == AsmPrinter::CFI_M_EH ||
86       (MoveType == AsmPrinter::CFI_M_Debug &&
87        moveTypeModule == AsmPrinter::CFI_M_None))
88     moveTypeModule = MoveType;
89
90   shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
91
92   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
93   unsigned PerEncoding = TLOF.getPersonalityEncoding();
94   const Function *Per = MMI->getPersonality();
95
96   shouldEmitPersonality = hasLandingPads &&
97     PerEncoding != dwarf::DW_EH_PE_omit && Per;
98
99   unsigned LSDAEncoding = TLOF.getLSDAEncoding();
100   shouldEmitLSDA = shouldEmitPersonality &&
101     LSDAEncoding != dwarf::DW_EH_PE_omit;
102
103   if (!shouldEmitPersonality && !shouldEmitMoves)
104     return;
105
106   Asm->OutStreamer.EmitCFIStartProc(/*IsSimple=*/false);
107
108   // Indicate personality routine, if any.
109   if (!shouldEmitPersonality)
110     return;
111
112   const MCSymbol *Sym =
113       TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
114   Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
115
116   // Provide LSDA information.
117   if (!shouldEmitLSDA)
118     return;
119
120   Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
121                                                   Asm->getFunctionNumber()),
122                                LSDAEncoding);
123 }
124
125 /// endFunction - Gather and emit post-function exception information.
126 ///
127 void DwarfCFIException::endFunction(const MachineFunction *) {
128   if (!shouldEmitPersonality && !shouldEmitMoves)
129     return;
130
131   Asm->OutStreamer.EmitCFIEndProc();
132
133   if (!shouldEmitPersonality)
134     return;
135
136   // Map all labels and get rid of any dead landing pads.
137   MMI->TidyLandingPads();
138
139   emitExceptionTable();
140 }