blob: dbd52c4981b3f10f0f79bfa6b56c8847d8d114a4 [file] [log] [blame]
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +00001//===-- 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/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"
Rafael Espindolaf0adba92011-04-15 15:11:06 +000035#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000036#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
42DwarfCFIException::DwarfCFIException(AsmPrinter *A)
43 : DwarfException(A),
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000044 shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false)
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000045 {}
46
47DwarfCFIException::~DwarfCFIException() {}
48
49/// EndModule - Emit all exception information that should come after the
50/// content.
51void DwarfCFIException::EndModule() {
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +000052 if (!Asm->MAI->isExceptionHandlingDwarf())
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000053 return;
54
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000055 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Rafael Espindolaac3ba1b2011-04-29 15:09:53 +000056
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000057 unsigned PerEncoding = TLOF.getPersonalityEncoding();
58
Rafael Espindolaf0adba92011-04-15 15:11:06 +000059 if ((PerEncoding & 0x70) != dwarf::DW_EH_PE_pcrel)
60 return;
61
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000062 // Emit references to all used personality functions
Rafael Espindolae17e6c02011-05-02 15:49:52 +000063 bool AtLeastOne = false;
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000064 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
65 for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000066 if (!Personalities[i])
67 continue;
68 MCSymbol *Sym = Asm->Mang->getSymbol(Personalities[i]);
Rafael Espindola30deafc2011-04-16 03:51:21 +000069 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
Rafael Espindolae17e6c02011-05-02 15:49:52 +000070 AtLeastOne = true;
71 }
72
73 if (AtLeastOne && !TLOF.isFunctionEHFrameSymbolPrivate()) {
74 // This is a temporary hack to keep sections in the same order they
75 // were before. This lets us produce bit identical outputs while
76 // transitioning to CFI.
77 Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection());
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000078 }
79}
80
81/// BeginFunction - Gather pre-function exception information. Assumes it's
82/// being emitted immediately after the function entry point.
83void DwarfCFIException::BeginFunction(const MachineFunction *MF) {
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000084 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000085
86 // If any landing pads survive, we need an EH table.
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000087 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000088
89 // See if we need frame move info.
Rafael Espindolaa4329972011-04-29 14:14:06 +000090 shouldEmitMoves = Asm->needsCFIMoves();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000091
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000092 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000093 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Rafael Espindolaf0adba92011-04-15 15:11:06 +000094 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000095
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();
107
108 // Indicate personality routine, if any.
109 if (!shouldEmitPersonality)
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000110 return;
111
Rafael Espindola60246a92011-04-27 23:17:57 +0000112 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, Asm->Mang, MMI);
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000113 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola7b11a4c2011-04-29 14:48:51 +0000114
115 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
116 Asm->getFunctionNumber()));
117
118 // Provide LSDA information.
119 if (!shouldEmitLSDA)
120 return;
121
122 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
123 Asm->getFunctionNumber()),
124 LSDAEncoding);
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000125}
126
127/// EndFunction - Gather and emit post-function exception information.
128///
129void DwarfCFIException::EndFunction() {
Rafael Espindola7b11a4c2011-04-29 14:48:51 +0000130 if (!shouldEmitPersonality && !shouldEmitMoves)
131 return;
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000132
Rafael Espindolae54e7852011-04-24 19:55:34 +0000133 Asm->OutStreamer.EmitCFIEndProc();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000134
135 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
136 Asm->getFunctionNumber()));
137
138 // Map all labels and get rid of any dead landing pads.
139 MMI->TidyLandingPads();
140
Rafael Espindola7b11a4c2011-04-29 14:48:51 +0000141 if (shouldEmitPersonality)
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000142 EmitExceptionTable();
143}