DwarfException.cpp revision 6c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7f
1eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
2eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//
3eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//                     The LLVM Compiler Infrastructure
4eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//
5eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// This file is distributed under the University of Illinois Open Source
6eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// License. See LICENSE.TXT for details.
7eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//
8eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//===----------------------------------------------------------------------===//
9eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//
10eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// This file contains support for writing dwarf exception info into asm files.
11eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//
12eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//===----------------------------------------------------------------------===//
13eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
14eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "DwarfException.h"
15eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Module.h"
16eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/CodeGen/MachineModuleInfo.h"
17eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/CodeGen/MachineFrameInfo.h"
18eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/CodeGen/MachineLocation.h"
196c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/MC/MCStreamer.h"
20eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetAsmInfo.h"
21eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetData.h"
22eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetFrameInfo.h"
23d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner#include "llvm/Target/TargetLoweringObjectFile.h"
24eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetOptions.h"
25d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner#include "llvm/Target/TargetRegisterInfo.h"
266c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/Dwarf.h"
276c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/Timer.h"
286c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/raw_ostream.h"
29eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/ADT/StringExtras.h"
30eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingusing namespace llvm;
31eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
32eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingstatic TimerGroup &getDwarfTimerGroup() {
33eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  static TimerGroup DwarfTimerGroup("Dwarf Exception");
34eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return DwarfTimerGroup;
35eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
36eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
37bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
38bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling                               const TargetAsmInfo *T)
39bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
40bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    shouldEmitTableModule(false), shouldEmitMovesModule(false),
41bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer(0) {
42bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  if (TimePassesIsEnabled)
43bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer = new Timer("Dwarf Exception Writer",
44bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling                               getDwarfTimerGroup());
45bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
46bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
47bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::~DwarfException() {
48bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  delete ExceptionTimer;
49bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
50bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
51eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EmitCommonEHFrame(const Function *Personality,
52eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                       unsigned Index) {
53eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Size and sign of stack growth.
54eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int stackGrowth =
55eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
56eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TargetFrameInfo::StackGrowsUp ?
57eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TD->getPointerSize() : -TD->getPointerSize();
58eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
59eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin eh frame section.
606c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
61eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
62e2cf37b88c089a71727b3ecd466856f0cd638813Chris Lattner  if (TAI->is_EHSymbolPrivate())
63e2cf37b88c089a71727b3ecd466856f0cd638813Chris Lattner    O << TAI->getPrivateGlobalPrefix();
64eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
65eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "EH_frame" << Index << ":\n";
66eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("section_eh_frame", Index);
67eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
68eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define base labels.
69eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common", Index);
70eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
71eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define the eh frame length.
72eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitDifference("eh_frame_common_end", Index,
73eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                 "eh_frame_common_begin", Index, true);
74eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("Length of Common Information Entry");
75eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
76eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH frame header.
77eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_begin", Index);
78eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt32((int)0);
79eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Identifier Tag");
80eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(dwarf::DW_CIE_VERSION);
81eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Version");
82eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
83eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The personality presence indicates that language specific information will
84eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // show up in the eh frame.
85eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitString(Personality ? "zPLR" : "zR");
86eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Augmentation");
87eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
88eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Round out reader.
89eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitULEB128Bytes(1);
90eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Code Alignment Factor");
91eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitSLEB128Bytes(stackGrowth);
92eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Data Alignment Factor");
93eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
94eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Return Address Column");
95eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
96eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If there is a personality, we need to indicate the functions location.
97eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (Personality) {
98eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(7);
99eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Augmentation Size");
100eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
101eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (TAI->getNeedsIndirectEncoding()) {
102eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
103eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                    dwarf::DW_EH_PE_indirect);
104eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Personality (pcrel sdata4 indirect)");
105eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
106eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
107eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Personality (pcrel sdata4)");
108eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
109eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
110eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective(true);
111eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << TAI->getPersonalityPrefix();
112eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
113eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << TAI->getPersonalitySuffix();
114eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
115eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << "-" << TAI->getPCSymbol();
116eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Personality");
117eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
118eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
119eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("LSDA Encoding (pcrel sdata4)");
120eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
121eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
122eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE Encoding (pcrel sdata4)");
123eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  } else {
124eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(1);
125eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Augmentation Size");
126eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
127eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
128eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE Encoding (pcrel sdata4)");
129eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
130eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
131eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Indicate locations of general callee saved registers in frame.
132eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  std::vector<MachineMove> Moves;
133eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  RI->getInitialFrameState(Moves);
134eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitFrameMoves(NULL, 0, Moves, true);
135eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
136eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // On Darwin the linker honors the alignment of eh_frame, which means it must
137eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // be 8-byte on 64-bit targets to match what gcc does.  Otherwise you get
138eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // holes which confuse readers of eh_frame.
139eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
140eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                     0, 0, false);
141eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_end", Index);
142eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
143eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL();
144eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
145eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
146eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EmitEHFrame - Emit function exception frame information.
147eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling///
148eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
149eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
150eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         "Should not emit 'available externally' functions at all");
151eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
1523e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  const Function *TheFunc = EHFrameInfo.function;
1533e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner
1546c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
1556a66e585b8242b9764ec3343309697d24bb1e312Chris Lattner
156eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Externally visible entry into the functions eh frame info. If the
157eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // corresponding function is static, this should not be externally visible.
1583e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!TheFunc->hasLocalLinkage())
159eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
160eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
161eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
162eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If corresponding function is weak definition, this should be too.
1633e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (TheFunc->isWeakForLinker() && TAI->getWeakDefDirective())
164eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
165eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
166eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If there are no calls then you can't unwind.  This may mean we can omit the
167eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH Frame, but some environments do not handle weak absolute symbols. If
168eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // UnwindTablesMandatory is set we cannot do this optimization; the unwind
169eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // info is to be available for non-EH uses.
1703e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
1713e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner      (!TheFunc->isWeakForLinker() ||
172eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       !TAI->getWeakDefDirective() ||
173eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       TAI->getSupportsWeakOmittedEHFrame())) {
174eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << " = 0\n";
175eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // This name has no connection to the function, so it might get
176eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripped when the function is not, erroneously.  Prohibit
177eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripping unconditionally.
178eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (const char *UsedDirective = TAI->getUsedDirective())
179eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << UsedDirective << EHFrameInfo.FnName << "\n\n";
180eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  } else {
181eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << ":\n";
182eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
183eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // EH frame header.
184eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_frame_end", EHFrameInfo.Number,
185eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   "eh_frame_begin", EHFrameInfo.Number, true);
186eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Length of Frame Information Entry");
187eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
188eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_begin", EHFrameInfo.Number);
189eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
190a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner    EmitSectionOffset("eh_frame_begin", "eh_frame_common",
191a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
192a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      true, true, false);
193eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
194eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE CIE offset");
195eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
196eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
197eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE initial location");
198eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_func_end", EHFrameInfo.Number,
199eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   "eh_func_begin", EHFrameInfo.Number, true);
200eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE address range");
201eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
202eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If there is a personality and landing pads then point to the language
203eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // specific data area in the exception table.
204eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (EHFrameInfo.PersonalityIndex) {
205eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitULEB128Bytes(4);
206eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
207eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
208eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (EHFrameInfo.hasLandingPads)
209eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        EmitReference("exception", EHFrameInfo.Number, true, true);
210eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      else
211eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        Asm->EmitInt32((int)0);
212eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Language Specific Data Area");
213eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
214eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitULEB128Bytes(0);
215eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
216eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
217eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
218eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Indicate locations of function specific callee saved registers in frame.
219eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
220eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   true);
221eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
222eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // On Darwin the linker honors the alignment of eh_frame, which means it
223eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // must be 8-byte on 64-bit targets to match what gcc does.  Otherwise you
224eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // get holes which confuse readers of eh_frame.
225eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
226eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       0, 0, false);
227eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_end", EHFrameInfo.Number);
228eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
229eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If the function is marked used, this table should be also.  We cannot
230eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // make the mark unconditional in this case, since retaining the table also
231eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // retains the function in this case, and there is code around that depends
232eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // on unused functions (calling undefined externals) being dead-stripped to
233eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // link correctly.  Yes, there really is.
234401e10c4fbfcdcfade5065093e2ca97f69a1d144Chris Lattner    if (MMI->isUsedFunction(EHFrameInfo.function))
235eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (const char *UsedDirective = TAI->getUsedDirective())
236eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        O << UsedDirective << EHFrameInfo.FnName << "\n\n";
237eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
238eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
239eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
240eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// SharedTypeIds - How many leading type ids two landing pads have in common.
241eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingunsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
242eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                       const LandingPadInfo *R) {
243eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
244eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
245eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
246eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned Count = 0;
247eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
248eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (; Count != MinSize; ++Count)
249eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[Count] != RIds[Count])
250eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return Count;
251eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
252eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return Count;
253eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
254eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
255eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// PadLT - Order landing pads lexicographically by type id.
256eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingbool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
257eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
258eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
259eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
260eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
261eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != MinSize; ++i)
262eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[i] != RIds[i])
263eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return LIds[i] < RIds[i];
264eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
265eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return LSize < RSize;
266eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
267eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
268d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// ComputeActionsTable - Compute the actions table and gather the first action
269d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// index for each landing pad site.
270ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingunsigned DwarfException::
271ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
272ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<ActionEntry> &Actions,
273ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<unsigned> &FirstActions) {
2745e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // Negative type IDs index into FilterIds. Positive type IDs index into
2755e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // TypeInfos.  The value written for a positive type ID is just the type ID
2765e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // itself.  For a negative type ID, however, the value written is the
277eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // (negative) byte offset of the corresponding FilterIds entry.  The byte
2785e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // offset is usually equal to the type ID (because the FilterIds entries are
2795e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // written using a variable width encoding, which outputs one byte per entry
2805e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // as long as the value written is not too large) but can differ.  This kind
2815e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // of complication does not occur for positive type IDs because type infos are
282eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // output using a fixed width encoding.  FilterOffsets[i] holds the byte
283eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // offset corresponding to FilterIds[i].
284409914b773ffd53d53d214394d5636a76f673186Bill Wendling
285409914b773ffd53d53d214394d5636a76f673186Bill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
286eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  SmallVector<int, 16> FilterOffsets;
287eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FilterOffsets.reserve(FilterIds.size());
288eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int Offset = -1;
289409914b773ffd53d53d214394d5636a76f673186Bill Wendling
290409914b773ffd53d53d214394d5636a76f673186Bill Wendling  for (std::vector<unsigned>::const_iterator
291409914b773ffd53d53d214394d5636a76f673186Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
292eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FilterOffsets.push_back(Offset);
293eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Offset -= TargetAsmInfo::getULEB128Size(*I);
294eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
295eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
296eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FirstActions.reserve(LandingPads.size());
297eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
298eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int FirstAction = 0;
299eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeActions = 0;
3005e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  const LandingPadInfo *PrevLPI = 0;
301409914b773ffd53d53d214394d5636a76f673186Bill Wendling
3025cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
3035e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling         I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
3045e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const LandingPadInfo *LPI = *I;
3055e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const std::vector<int> &TypeIds = LPI->TypeIds;
3065e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
307eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    unsigned SizeSiteActions = 0;
308eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
309eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (NumShared < TypeIds.size()) {
310eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned SizeAction = 0;
311eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      ActionEntry *PrevAction = 0;
312eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
313eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (NumShared) {
3145e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        const unsigned SizePrevIds = PrevLPI->TypeIds.size();
315eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        assert(Actions.size());
316eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
317eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
318eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
319eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
320eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        for (unsigned j = NumShared; j != SizePrevIds; ++j) {
321eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction -=
322eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
323eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction += -PrevAction->NextAction;
324eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          PrevAction = PrevAction->Previous;
325eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
326eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
327eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
328eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Compute the actions.
3295e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling      for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
3305e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        int TypeID = TypeIds[J];
3315e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
332eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
333eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
334eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
335eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
336eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
337eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeSiteActions += SizeAction;
338eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
339eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
340eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        Actions.push_back(Action);
341eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
342eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
343eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
344eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Record the first action of the landing pad site.
345eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
346eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } // else identical - re-use previous FirstAction
347eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
348eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FirstActions.push_back(FirstAction);
349eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
350eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Compute this sites contribution to size.
351eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeActions += SizeSiteActions;
3525e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
3535e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    PrevLPI = LPI;
354eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
355eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
3565e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  return SizeActions;
3575e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling}
3585e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
359ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// ComputeCallSiteTable - Compute the call-site table.  The entry for an invoke
360ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// has a try-range containing the call, a non-zero landing pad and an
361ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// appropriate action.  The entry for an ordinary call has a try-range
362ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// containing the call and zero for the landing pad and the action.  Calls
363ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// marked 'nounwind' have no entry and must not be contained in the try-range
364ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// of any entry - they form gaps in the table.  Entries must be ordered by
365ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// try-range address.
366ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::
367ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
368ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const RangeMapType &PadMap,
369ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
370ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const SmallVectorImpl<unsigned> &FirstActions) {
371eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The end label of the previous invoke or nounwind try-range.
372eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LastLabel = 0;
373eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
374eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Whether there is a potentially throwing instruction (currently this means
375eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // an ordinary call) between the end of the previous try-range and now.
376eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  bool SawPotentiallyThrowing = false;
377eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
3785cff487665097d971067cbede1598e249f673182Bill Wendling  // Whether the last CallSite entry was for an invoke.
379eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  bool PreviousIsInvoke = false;
380eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
381eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Visit all instructions in order of address.
382eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
383eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       I != E; ++I) {
384eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
385eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         MI != E; ++MI) {
386eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (!MI->isLabel()) {
387eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SawPotentiallyThrowing |= MI->getDesc().isCall();
388eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        continue;
389eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
390eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
391eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned BeginLabel = MI->getOperand(0).getImm();
392eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel && "Invalid label!");
393eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
394eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // End of the previous try-range?
395eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (BeginLabel == LastLabel)
396eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SawPotentiallyThrowing = false;
397eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
398eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Beginning of a new try-range?
399eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      RangeMapType::iterator L = PadMap.find(BeginLabel);
400eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (L == PadMap.end())
401eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Nope, it was just some random label.
402eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        continue;
403eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
404eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      PadRange P = L->second;
405eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
406eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
407eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling             "Inconsistent landing pad map!");
408eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
4091b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      // For Dwarf exception handling (SjLj handling doesn't use this)
410eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // If some instruction between the previous try-range and this one may
411eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // throw, create a call-site entry with no landing pad for the region
412eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // between the try-ranges.
4131b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (SawPotentiallyThrowing &&
4141b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach          TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
415eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
416eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSites.push_back(Site);
417eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
418eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
419eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
420eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      LastLabel = LandingPad->EndLabels[P.RangeIndex];
421eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel && LastLabel && "Invalid landing pad!");
422eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
423eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (LandingPad->LandingPadLabel) {
424eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // This try-range is for an invoke.
425eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSiteEntry Site = {BeginLabel, LastLabel,
426eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                              LandingPad->LandingPadLabel,
427eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                              FirstActions[P.PadIndex]};
428eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
429eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Try to merge with the previous call-site.
430eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        if (PreviousIsInvoke) {
431eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          CallSiteEntry &Prev = CallSites.back();
432eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
433eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            // Extend the range of the previous entry.
434eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            Prev.EndLabel = Site.EndLabel;
435eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            continue;
436eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          }
437eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
438eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
439eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Otherwise, create a new call-site.
440eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSites.push_back(Site);
441eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = true;
442eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      } else {
443eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Create a gap.
444eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
445eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
446eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
447eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
448eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
449eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If some instruction between the previous try-range and the end of the
450eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // function may throw, create a call-site entry with no landing pad for the
451eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // region following the try-range.
4521b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (SawPotentiallyThrowing &&
4531b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
454eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    CallSiteEntry Site = {LastLabel, 0, 0, 0};
455eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    CallSites.push_back(Site);
456eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
457ade025c65c12503aba161a4fa399fd97414abaffBill Wendling}
458ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
4590dafca90761097230f02e655fdd541f59b888315Bill Wendling/// EmitExceptionTable - Emit landing pads and actions.
4600dafca90761097230f02e655fdd541f59b888315Bill Wendling///
4610dafca90761097230f02e655fdd541f59b888315Bill Wendling/// The general organization of the table is complex, but the basic concepts are
4620dafca90761097230f02e655fdd541f59b888315Bill Wendling/// easy.  First there is a header which describes the location and organization
4630dafca90761097230f02e655fdd541f59b888315Bill Wendling/// of the three components that follow.
4640dafca90761097230f02e655fdd541f59b888315Bill Wendling///
4650dafca90761097230f02e655fdd541f59b888315Bill Wendling///  1. The landing pad site information describes the range of code covered by
4660dafca90761097230f02e655fdd541f59b888315Bill Wendling///     the try.  In our case it's an accumulation of the ranges covered by the
4670dafca90761097230f02e655fdd541f59b888315Bill Wendling///     invokes in the try.  There is also a reference to the landing pad that
4680dafca90761097230f02e655fdd541f59b888315Bill Wendling///     handles the exception once processed.  Finally an index into the actions
4690dafca90761097230f02e655fdd541f59b888315Bill Wendling///     table.
4700dafca90761097230f02e655fdd541f59b888315Bill Wendling///  2. The action table, in our case, is composed of pairs of type ids and next
4710dafca90761097230f02e655fdd541f59b888315Bill Wendling///     action offset.  Starting with the action index from the landing pad
4720dafca90761097230f02e655fdd541f59b888315Bill Wendling///     site, each type Id is checked for a match to the current exception.  If
4730dafca90761097230f02e655fdd541f59b888315Bill Wendling///     it matches then the exception and type id are passed on to the landing
4740dafca90761097230f02e655fdd541f59b888315Bill Wendling///     pad.  Otherwise the next action is looked up.  This chain is terminated
4750dafca90761097230f02e655fdd541f59b888315Bill Wendling///     with a next action of zero.  If no type id is found the the frame is
4760dafca90761097230f02e655fdd541f59b888315Bill Wendling///     unwound and handling continues.
4770dafca90761097230f02e655fdd541f59b888315Bill Wendling///  3. Type id table contains references to all the C++ typeinfo for all
4780dafca90761097230f02e655fdd541f59b888315Bill Wendling///     catches in the function.  This tables is reversed indexed base 1.
479ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::EmitExceptionTable() {
480ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
481ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
482ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
483ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  if (PadInfos.empty()) return;
484ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
485ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Sort the landing pads in order of their type ids.  This is used to fold
486ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // duplicate actions.
487ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<const LandingPadInfo *, 64> LandingPads;
488ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  LandingPads.reserve(PadInfos.size());
489ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
490ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
491ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    LandingPads.push_back(&PadInfos[i]);
492ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
493ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
494ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
495ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the actions table and gather the first action index for each
496ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // landing pad site.
497ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<ActionEntry, 32> Actions;
498ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<unsigned, 64> FirstActions;
499ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
500ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
501ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Invokes and nounwind calls have entries in PadMap (due to being bracketed
502ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // by try-range labels when lowered).  Ordinary calls do not, so appropriate
5031b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // try-ranges for them need be deduced when using Dwarf exception handling.
504ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  RangeMapType PadMap;
505ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
506ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    const LandingPadInfo *LandingPad = LandingPads[i];
507ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
508ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      unsigned BeginLabel = LandingPad->BeginLabels[j];
509ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
510ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadRange P = { i, j };
511ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadMap[BeginLabel] = P;
512ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    }
513ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  }
514ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
515ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the call-site table.
516ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<CallSiteEntry, 64> CallSites;
5178b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach  ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
518eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
519eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Final tallies.
520eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
521eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Call sites.
522eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned SiteStartSize  = sizeof(int32_t); // DW_EH_PE_udata4
523eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
524eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
5251b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  unsigned SizeSites;
526bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
527bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach  bool HaveTTData = (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
528bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach    ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
529bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
530bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
5311b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
5328b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    SizeSites = 0;
5331b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else
5341b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    SizeSites = CallSites.size() *
5351b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      (SiteStartSize + SiteLengthSize + LandingPadSize);
5361b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
537eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
5381b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
5391b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      SizeSites += TargetAsmInfo::getULEB128Size(i);
5401b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
541eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Type infos.
542eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
543eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
544eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
545eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned TypeOffset = sizeof(int8_t) + // Call site format
546eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
547eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeSites + SizeActions + SizeTypes;
548eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
549eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned TotalSize = sizeof(int8_t) + // LPStart format
550eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       sizeof(int8_t) + // TType format
551bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach       (HaveTTData ?
552bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach          TargetAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset
553eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       TypeOffset;
554eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
555eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeAlign = (4 - TotalSize) & 3;
556eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
557eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin the exception table.
558d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
5596c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(LSDASection);
560eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
561eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "GCC_except_table" << SubprogramCount << ":\n";
562eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
563eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != SizeAlign; ++i) {
564eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(0);
565eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Padding");
566c5800a809fd5e411de78ac1a4be633cd3e1d7840Bill Wendling  }
567eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
568eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("exception", SubprogramCount);
5691b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
5701b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    std::string SjLjName = "_lsda_";
5711b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    SjLjName += MF->getFunction()->getName().str();
5721b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    EmitLabel(SjLjName.c_str(), 0);
5731b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
574eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
575eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the header.
576eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(dwarf::DW_EH_PE_omit);
577eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("LPStart format (DW_EH_PE_omit)");
578b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
579ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#if 0
58081c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  if (TypeInfos.empty() && FilterIds.empty()) {
581ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // If there are no typeinfos or filters, there is nothing to emit, optimize
582ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // by specifying the "omit" encoding.
58381c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner    Asm->EmitInt8(dwarf::DW_EH_PE_omit);
58481c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner    Asm->EOL("TType format (DW_EH_PE_omit)");
58581c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  } else {
586ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // Okay, we have actual filters or typeinfos to emit.  As such, we need to
587ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // pick a type encoding for them.  We're about to emit a list of pointers to
588ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // typeinfo objects at the end of the LSDA.  However, unless we're in static
589ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // mode, this reference will require a relocation by the dynamic linker.
59046b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //
591ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // Because of this, we have a couple of options:
592ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   1) If we are in -static mode, we can always use an absolute reference
593ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      from the LSDA, because the static linker will resolve it.
594ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   2) Otherwise, if the LSDA section is writable, we can output the direct
595ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      reference to the typeinfo and allow the dynamic linker to relocate
596ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      it.  Since it is in a writable section, the dynamic linker won't
597ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      have a problem.
598ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   3) Finally, if we're in PIC mode and the LDSA section isn't writable,
599ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      we need to use some form of indirection.  For example, on Darwin,
600ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      we can output a statically-relocatable reference to a dyld stub. The
601ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      offset to the stub is constant, but the contents are in a section
602ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      that is updated by the dynamic linker.  This is easy enough, but we
603ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      need to tell the personality function of the unwinder to indirect
604ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      through the dyld stub.
605ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //
606ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // FIXME: When this is actually implemented, we'll have to emit the stubs
607ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // somewhere.  This predicate should be moved to a shared location that is
608ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // in target-independent code.
609ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //
610ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    if (LSDASection->isWritable() ||
611ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner        Asm->TM.getRelocationModel() == Reloc::Static) {
612ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner      Asm->EmitInt8(DW_EH_PE_absptr);
613ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner      Asm->EOL("TType format (DW_EH_PE_absptr)");
614ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    } else {
615ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner      Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
616ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner      Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect"
617ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner               " | DW_EH_PE_sdata4)");
618ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    }
619b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EmitULEB128Bytes(TypeOffset);
620b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EOL("TType base offset");
621b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling  }
622ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#else
623bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach  // For SjLj exceptions, if there is no TypeInfo, then we just explicitly
6241b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // say that we're omitting that bit.
6251b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // FIXME: does this apply to Dwarf also? The above #if 0 implies yes?
626bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach  if (!HaveTTData) {
6271b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitInt8(dwarf::DW_EH_PE_omit);
6281b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("TType format (DW_EH_PE_omit)");
6291b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else {
6301b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
6311b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("TType format (DW_EH_PE_absptr)");
6321b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(TypeOffset);
6331b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("TType base offset");
6341b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
635ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#endif
636b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
6371b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // SjLj Exception handilng
6381b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
6391b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
6401b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("Call site format (DW_EH_PE_udata4)");
6411b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(SizeSites);
6421b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("Call-site table length");
6431b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
6441b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // Emit the landing pad site information.
6458b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    unsigned idx = 0;
6468b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    for (SmallVectorImpl<CallSiteEntry>::const_iterator
6478b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach         I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
6488b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach      const CallSiteEntry &S = *I;
6498b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach      Asm->EmitULEB128Bytes(idx);
6501b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Landing pad");
6511b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(S.Action);
6521b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Action");
6531b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    }
6541b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else {
6551b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // DWARF Exception handling
6561b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    assert(TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
6571b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
6581b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
6591b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("Call site format (DW_EH_PE_udata4)");
6601b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(SizeSites);
6611b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("Call-site table length");
662eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6631b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // Emit the landing pad site information.
6641b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    for (SmallVectorImpl<CallSiteEntry>::const_iterator
6655cff487665097d971067cbede1598e249f673182Bill Wendling         I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
6661b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const CallSiteEntry &S = *I;
6671b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const char *BeginTag;
6681b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      unsigned BeginNumber;
669eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6701b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.BeginLabel) {
6711b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginTag = "eh_func_begin";
6721b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginNumber = SubprogramCount;
6731b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      } else {
6741b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginTag = "label";
6751b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginNumber = S.BeginLabel;
6761b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      }
677eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6781b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
6791b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                        true, true);
6801b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Region start");
681eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6821b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.EndLabel)
6831b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
6841b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                       true);
6851b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      else
6861b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
687eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6881b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Region length");
689eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6901b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.PadLabel)
6911b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        Asm->EmitInt32(0);
6921b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      else
6931b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
6941b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                          true, true);
695eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6961b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Landing pad");
697eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6981b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(S.Action);
6991b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Action");
7001b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    }
701eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
702eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
703eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the actions.
7045cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<ActionEntry>::const_iterator
7055cff487665097d971067cbede1598e249f673182Bill Wendling         I = Actions.begin(), E = Actions.end(); I != E; ++I) {
7065cff487665097d971067cbede1598e249f673182Bill Wendling    const ActionEntry &Action = *I;
707eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
708eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo index");
709eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.NextAction);
710eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Next action");
711eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
712eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
713eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the type ids.
7145cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<GlobalVariable *>::const_reverse_iterator
7155cff487665097d971067cbede1598e249f673182Bill Wendling         I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
7165cff487665097d971067cbede1598e249f673182Bill Wendling    GlobalVariable *GV = *I;
717eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective();
718eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
719eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (GV) {
720eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      std::string GLN;
721eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << Asm->getGlobalLinkName(GV, GLN);
722eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
723eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << "0";
724eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
725eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
726eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo");
727eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
728eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
729eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the filter typeids.
7305cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<unsigned>::const_iterator
7315cff487665097d971067cbede1598e249f673182Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
7325cff487665097d971067cbede1598e249f673182Bill Wendling    unsigned TypeID = *I;
733eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(TypeID);
734eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Filter TypeInfo index");
735eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
736eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
737eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
738eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
739eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
740eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndModule - Emit all exception information that should come after the
741eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// content.
742eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndModule() {
7431b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (TAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
7441b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    return;
745eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
746eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
747eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
748eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (shouldEmitMovesModule || shouldEmitTableModule) {
749eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    const std::vector<Function *> Personalities = MMI->getPersonalities();
750eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (unsigned i = 0; i < Personalities.size(); ++i)
751eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitCommonEHFrame(Personalities[i], i);
752eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
753eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
754eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling           E = EHFrames.end(); I != E; ++I)
755eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitEHFrame(*I);
756eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
757eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
758eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
759eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
760eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
761eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
762eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// BeginFunction - Gather pre-function exception information.  Assumes being
763eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// emitted immediately after the function entry point.
764eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::BeginFunction(MachineFunction *MF) {
765eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
766eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
767eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
768eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  this->MF = MF;
769eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTable = shouldEmitMoves = false;
770eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
771eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (MMI && TAI->doesSupportExceptionHandling()) {
772eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Map all labels and get rid of any dead landing pads.
773eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    MMI->TidyLandingPads();
774eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
775eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If any landing pads survive, we need an EH table.
776eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (MMI->getLandingPads().size())
777eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitTable = true;
778eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
779eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // See if we need frame move info.
780eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
781eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitMoves = true;
782eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
783eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (shouldEmitMoves || shouldEmitTable)
784eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Assumes in correct section after the entry point.
785eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitLabel("eh_func_begin", ++SubprogramCount);
786eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
787eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
788eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTableModule |= shouldEmitTable;
789eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitMovesModule |= shouldEmitMoves;
790eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
791eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
792eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
793eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
794eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
795eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndFunction - Gather and emit post-function exception information.
796eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling///
797eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndFunction() {
798eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
799eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
800eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
801eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (shouldEmitMoves || shouldEmitTable) {
802eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_func_end", SubprogramCount);
803eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitExceptionTable();
804eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
805eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Save EH frame information
806eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EHFrames.push_back(
807e2cf37b88c089a71727b3ecd466856f0cd638813Chris Lattner        FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
808eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            SubprogramCount,
809eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MMI->getPersonalityIndex(),
810eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MF->getFrameInfo()->hasCalls(),
811eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            !MMI->getLandingPads().empty(),
812eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MMI->getFrameMoves(),
813eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MF->getFunction()));
814eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
815eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
816eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
817eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
818eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
819