blob: 8ed4f4c43a7c616eece91adc9e3bc0f36fbb6440 [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"
Evan Cheng2d286172011-07-18 22:29:13 +000020#include "llvm/MC/MachineLocation.h"
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000021#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 Espindola354c82b2011-05-10 21:54:59 +000044 shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false),
45 moveTypeModule(AsmPrinter::CFI_M_None) {}
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000046
47DwarfCFIException::~DwarfCFIException() {}
48
49/// EndModule - Emit all exception information that should come after the
50/// content.
51void DwarfCFIException::EndModule() {
Rafael Espindolae29887b2011-05-10 18:39:09 +000052 if (moveTypeModule == AsmPrinter::CFI_M_Debug)
53 Asm->OutStreamer.EmitCFISections(false, true);
54
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +000055 if (!Asm->MAI->isExceptionHandlingDwarf())
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000056 return;
57
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000058 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Rafael Espindolaac3ba1b2011-04-29 15:09:53 +000059
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000060 unsigned PerEncoding = TLOF.getPersonalityEncoding();
61
Rafael Espindolaf0adba92011-04-15 15:11:06 +000062 if ((PerEncoding & 0x70) != dwarf::DW_EH_PE_pcrel)
63 return;
64
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000065 // Emit references to all used personality functions
Rafael Espindolae17e6c02011-05-02 15:49:52 +000066 bool AtLeastOne = false;
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000067 const std::vector<const Function*> &Personalities = MMI->getPersonalities();
68 for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000069 if (!Personalities[i])
70 continue;
71 MCSymbol *Sym = Asm->Mang->getSymbol(Personalities[i]);
Rafael Espindola30deafc2011-04-16 03:51:21 +000072 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym);
Rafael Espindolae17e6c02011-05-02 15:49:52 +000073 AtLeastOne = true;
74 }
75
76 if (AtLeastOne && !TLOF.isFunctionEHFrameSymbolPrivate()) {
77 // This is a temporary hack to keep sections in the same order they
78 // were before. This lets us produce bit identical outputs while
79 // transitioning to CFI.
Evan Chenge76a33b2011-07-20 05:58:47 +000080 Asm->OutStreamer.SwitchSection(
81 const_cast<TargetLoweringObjectFile&>(TLOF).getEHFrameSection());
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000082 }
83}
84
85/// BeginFunction - Gather pre-function exception information. Assumes it's
86/// being emitted immediately after the function entry point.
87void DwarfCFIException::BeginFunction(const MachineFunction *MF) {
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000088 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000089
90 // If any landing pads survive, we need an EH table.
Rafael Espindola7b11a4c2011-04-29 14:48:51 +000091 bool hasLandingPads = !MMI->getLandingPads().empty();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +000092
93 // See if we need frame move info.
Rafael Espindolae29887b2011-05-10 18:39:09 +000094 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
95 if (MoveType == AsmPrinter::CFI_M_EH ||
96 (MoveType == AsmPrinter::CFI_M_Debug &&
97 moveTypeModule == AsmPrinter::CFI_M_None))
98 moveTypeModule = MoveType;
99
100 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000101
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000102 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000103 unsigned PerEncoding = TLOF.getPersonalityEncoding();
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000104 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()];
Rafael Espindola7b11a4c2011-04-29 14:48:51 +0000105
106 shouldEmitPersonality = hasLandingPads &&
107 PerEncoding != dwarf::DW_EH_PE_omit && Per;
108
109 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
110 shouldEmitLSDA = shouldEmitPersonality &&
111 LSDAEncoding != dwarf::DW_EH_PE_omit;
112
113 if (!shouldEmitPersonality && !shouldEmitMoves)
114 return;
115
116 Asm->OutStreamer.EmitCFIStartProc();
117
118 // Indicate personality routine, if any.
119 if (!shouldEmitPersonality)
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000120 return;
121
Rafael Espindola60246a92011-04-27 23:17:57 +0000122 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, Asm->Mang, MMI);
Rafael Espindolaf0adba92011-04-15 15:11:06 +0000123 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
Rafael Espindola7b11a4c2011-04-29 14:48:51 +0000124
125 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin",
126 Asm->getFunctionNumber()));
127
128 // Provide LSDA information.
129 if (!shouldEmitLSDA)
130 return;
131
132 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception",
133 Asm->getFunctionNumber()),
134 LSDAEncoding);
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000135}
136
137/// EndFunction - Gather and emit post-function exception information.
138///
139void DwarfCFIException::EndFunction() {
Rafael Espindola7b11a4c2011-04-29 14:48:51 +0000140 if (!shouldEmitPersonality && !shouldEmitMoves)
141 return;
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000142
Rafael Espindolae54e7852011-04-24 19:55:34 +0000143 Asm->OutStreamer.EmitCFIEndProc();
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000144
145 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end",
146 Asm->getFunctionNumber()));
147
148 // Map all labels and get rid of any dead landing pads.
149 MMI->TidyLandingPads();
150
Rafael Espindola7b11a4c2011-04-29 14:48:51 +0000151 if (shouldEmitPersonality)
Anton Korobeynikov9a1ef4e2011-01-14 21:57:53 +0000152 EmitExceptionTable();
153}