DwarfException.cpp revision bff392384ddb032c732c38ec78b91d7a25dcf467
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"
19eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Support/Dwarf.h"
20eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Support/Timer.h"
21eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Support/raw_ostream.h"
22eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetAsmInfo.h"
23eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetData.h"
24eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetFrameInfo.h"
25d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner#include "llvm/Target/TargetLoweringObjectFile.h"
26eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetOptions.h"
27d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner#include "llvm/Target/TargetRegisterInfo.h"
28eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/ADT/StringExtras.h"
29eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingusing namespace llvm;
30eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
31eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingstatic TimerGroup &getDwarfTimerGroup() {
32eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  static TimerGroup DwarfTimerGroup("Dwarf Exception");
33eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return DwarfTimerGroup;
34eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
35eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
36bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
37bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling                               const TargetAsmInfo *T)
38bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
39bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    shouldEmitTableModule(false), shouldEmitMovesModule(false),
40bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer(0) {
41bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  if (TimePassesIsEnabled)
42bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer = new Timer("Dwarf Exception Writer",
43bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling                               getDwarfTimerGroup());
44bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
45bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
46bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::~DwarfException() {
47bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  delete ExceptionTimer;
48bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
49bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
50eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EmitCommonEHFrame(const Function *Personality,
51eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                       unsigned Index) {
52eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Size and sign of stack growth.
53eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int stackGrowth =
54eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
55eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TargetFrameInfo::StackGrowsUp ?
56eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TD->getPointerSize() : -TD->getPointerSize();
57eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
58eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin eh frame section.
5935039ac24163e99cfab161620a9fb41f944a63d5Chris Lattner  Asm->SwitchToSection(Asm->getObjFileLowering().getEHFrameSection());
60eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
61e2cf37b88c089a71727b3ecd466856f0cd638813Chris Lattner  if (TAI->is_EHSymbolPrivate())
62e2cf37b88c089a71727b3ecd466856f0cd638813Chris Lattner    O << TAI->getPrivateGlobalPrefix();
63eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
64eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "EH_frame" << Index << ":\n";
65eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("section_eh_frame", Index);
66eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
67eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define base labels.
68eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common", Index);
69eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
70eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define the eh frame length.
71eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitDifference("eh_frame_common_end", Index,
72eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                 "eh_frame_common_begin", Index, true);
73eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("Length of Common Information Entry");
74eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
75eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH frame header.
76eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_begin", Index);
77eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt32((int)0);
78eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Identifier Tag");
79eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(dwarf::DW_CIE_VERSION);
80eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Version");
81eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
82eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The personality presence indicates that language specific information will
83eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // show up in the eh frame.
84eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitString(Personality ? "zPLR" : "zR");
85eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Augmentation");
86eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
87eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Round out reader.
88eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitULEB128Bytes(1);
89eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Code Alignment Factor");
90eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitSLEB128Bytes(stackGrowth);
91eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Data Alignment Factor");
92eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
93eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Return Address Column");
94eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
95eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If there is a personality, we need to indicate the functions location.
96eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (Personality) {
97eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(7);
98eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Augmentation Size");
99eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
100eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (TAI->getNeedsIndirectEncoding()) {
101eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
102eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                    dwarf::DW_EH_PE_indirect);
103eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Personality (pcrel sdata4 indirect)");
104eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
105eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
106eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Personality (pcrel sdata4)");
107eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
108eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
109eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective(true);
110eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << TAI->getPersonalityPrefix();
111eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
112eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << TAI->getPersonalitySuffix();
113eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
114eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << "-" << TAI->getPCSymbol();
115eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Personality");
116eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
117eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
118eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("LSDA Encoding (pcrel sdata4)");
119eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
120eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
121eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE Encoding (pcrel sdata4)");
122eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  } else {
123eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(1);
124eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Augmentation Size");
125eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
126eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
127eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE Encoding (pcrel sdata4)");
128eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
129eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
130eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Indicate locations of general callee saved registers in frame.
131eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  std::vector<MachineMove> Moves;
132eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  RI->getInitialFrameState(Moves);
133eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitFrameMoves(NULL, 0, Moves, true);
134eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
135eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // On Darwin the linker honors the alignment of eh_frame, which means it must
136eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // be 8-byte on 64-bit targets to match what gcc does.  Otherwise you get
137eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // holes which confuse readers of eh_frame.
138eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
139eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                     0, 0, false);
140eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_end", Index);
141eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
142eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL();
143eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
144eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
145eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EmitEHFrame - Emit function exception frame information.
146eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling///
147eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
148eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
149eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         "Should not emit 'available externally' functions at all");
150eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
1513e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  const Function *TheFunc = EHFrameInfo.function;
1523e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner
15335039ac24163e99cfab161620a9fb41f944a63d5Chris Lattner  Asm->SwitchToSection(Asm->getObjFileLowering().getEHFrameSection());
154eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
155eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Externally visible entry into the functions eh frame info. If the
156eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // corresponding function is static, this should not be externally visible.
1573e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!TheFunc->hasLocalLinkage())
158eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
159eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
160eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
161eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If corresponding function is weak definition, this should be too.
1623e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (TheFunc->isWeakForLinker() && TAI->getWeakDefDirective())
163eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
164eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
165eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If there are no calls then you can't unwind.  This may mean we can omit the
166eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH Frame, but some environments do not handle weak absolute symbols. If
167eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // UnwindTablesMandatory is set we cannot do this optimization; the unwind
168eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // info is to be available for non-EH uses.
1693e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
1703e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner      (!TheFunc->isWeakForLinker() ||
171eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       !TAI->getWeakDefDirective() ||
172eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       TAI->getSupportsWeakOmittedEHFrame())) {
173eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << " = 0\n";
174eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // This name has no connection to the function, so it might get
175eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripped when the function is not, erroneously.  Prohibit
176eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripping unconditionally.
177eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (const char *UsedDirective = TAI->getUsedDirective())
178eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << UsedDirective << EHFrameInfo.FnName << "\n\n";
179eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  } else {
180eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << ":\n";
181eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
182eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // EH frame header.
183eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_frame_end", EHFrameInfo.Number,
184eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   "eh_frame_begin", EHFrameInfo.Number, true);
185eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Length of Frame Information Entry");
186eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
187eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_begin", EHFrameInfo.Number);
188eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
189a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner    EmitSectionOffset("eh_frame_begin", "eh_frame_common",
190a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
191a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      true, true, false);
192eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
193eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE CIE offset");
194eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
195eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
196eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE initial location");
197eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_func_end", EHFrameInfo.Number,
198eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   "eh_func_begin", EHFrameInfo.Number, true);
199eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE address range");
200eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
201eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If there is a personality and landing pads then point to the language
202eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // specific data area in the exception table.
203eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (EHFrameInfo.PersonalityIndex) {
204eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitULEB128Bytes(4);
205eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
206eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
207eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (EHFrameInfo.hasLandingPads)
208eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        EmitReference("exception", EHFrameInfo.Number, true, true);
209eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      else
210eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        Asm->EmitInt32((int)0);
211eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Language Specific Data Area");
212eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
213eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitULEB128Bytes(0);
214eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
215eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
216eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
217eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Indicate locations of function specific callee saved registers in frame.
218eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
219eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   true);
220eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
221eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // On Darwin the linker honors the alignment of eh_frame, which means it
222eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // must be 8-byte on 64-bit targets to match what gcc does.  Otherwise you
223eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // get holes which confuse readers of eh_frame.
224eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
225eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       0, 0, false);
226eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_end", EHFrameInfo.Number);
227eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
228eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If the function is marked used, this table should be also.  We cannot
229eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // make the mark unconditional in this case, since retaining the table also
230eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // retains the function in this case, and there is code around that depends
231eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // on unused functions (calling undefined externals) being dead-stripped to
232eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // link correctly.  Yes, there really is.
233401e10c4fbfcdcfade5065093e2ca97f69a1d144Chris Lattner    if (MMI->isUsedFunction(EHFrameInfo.function))
234eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (const char *UsedDirective = TAI->getUsedDirective())
235eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        O << UsedDirective << EHFrameInfo.FnName << "\n\n";
236eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
237eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
238eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
239eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// SharedTypeIds - How many leading type ids two landing pads have in common.
240eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingunsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
241eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                       const LandingPadInfo *R) {
242eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
243eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
244eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
245eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned Count = 0;
246eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
247eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (; Count != MinSize; ++Count)
248eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[Count] != RIds[Count])
249eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return Count;
250eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
251eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return Count;
252eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
253eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
254eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// PadLT - Order landing pads lexicographically by type id.
255eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingbool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
256eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
257eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
258eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
259eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
260eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != MinSize; ++i)
261eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[i] != RIds[i])
262eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return LIds[i] < RIds[i];
263eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
264eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return LSize < RSize;
265eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
266eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
267d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// ComputeActionsTable - Compute the actions table and gather the first action
268d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// index for each landing pad site.
269ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingunsigned DwarfException::
270ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
271ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<ActionEntry> &Actions,
272ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<unsigned> &FirstActions) {
2735e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // Negative type IDs index into FilterIds. Positive type IDs index into
2745e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // TypeInfos.  The value written for a positive type ID is just the type ID
2755e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // itself.  For a negative type ID, however, the value written is the
276eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // (negative) byte offset of the corresponding FilterIds entry.  The byte
2775e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // offset is usually equal to the type ID (because the FilterIds entries are
2785e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // written using a variable width encoding, which outputs one byte per entry
2795e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // as long as the value written is not too large) but can differ.  This kind
2805e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // of complication does not occur for positive type IDs because type infos are
281eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // output using a fixed width encoding.  FilterOffsets[i] holds the byte
282eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // offset corresponding to FilterIds[i].
283409914b773ffd53d53d214394d5636a76f673186Bill Wendling
284409914b773ffd53d53d214394d5636a76f673186Bill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
285eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  SmallVector<int, 16> FilterOffsets;
286eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FilterOffsets.reserve(FilterIds.size());
287eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int Offset = -1;
288409914b773ffd53d53d214394d5636a76f673186Bill Wendling
289409914b773ffd53d53d214394d5636a76f673186Bill Wendling  for (std::vector<unsigned>::const_iterator
290409914b773ffd53d53d214394d5636a76f673186Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
291eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FilterOffsets.push_back(Offset);
292eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Offset -= TargetAsmInfo::getULEB128Size(*I);
293eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
294eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
295eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FirstActions.reserve(LandingPads.size());
296eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
297eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int FirstAction = 0;
298eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeActions = 0;
2995e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  const LandingPadInfo *PrevLPI = 0;
300409914b773ffd53d53d214394d5636a76f673186Bill Wendling
3015cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
3025e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling         I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
3035e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const LandingPadInfo *LPI = *I;
3045e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const std::vector<int> &TypeIds = LPI->TypeIds;
3055e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
306eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    unsigned SizeSiteActions = 0;
307eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
308eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (NumShared < TypeIds.size()) {
309eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned SizeAction = 0;
310eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      ActionEntry *PrevAction = 0;
311eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
312eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (NumShared) {
3135e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        const unsigned SizePrevIds = PrevLPI->TypeIds.size();
314eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        assert(Actions.size());
315eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
316eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
317eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
318eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
319eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        for (unsigned j = NumShared; j != SizePrevIds; ++j) {
320eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction -=
321eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
322eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction += -PrevAction->NextAction;
323eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          PrevAction = PrevAction->Previous;
324eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
325eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
326eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
327eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Compute the actions.
3285e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling      for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
3295e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        int TypeID = TypeIds[J];
3305e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
331eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
332eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
333eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
334eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
335eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
336eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeSiteActions += SizeAction;
337eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
338eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
339eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        Actions.push_back(Action);
340eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
341eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
342eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
343eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Record the first action of the landing pad site.
344eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
345eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } // else identical - re-use previous FirstAction
346eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
347eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FirstActions.push_back(FirstAction);
348eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
349eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Compute this sites contribution to size.
350eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeActions += SizeSiteActions;
3515e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
3525e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    PrevLPI = LPI;
353eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
354eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
3555e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  return SizeActions;
3565e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling}
3575e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
358ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// ComputeCallSiteTable - Compute the call-site table.  The entry for an invoke
359ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// has a try-range containing the call, a non-zero landing pad and an
360ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// appropriate action.  The entry for an ordinary call has a try-range
361ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// containing the call and zero for the landing pad and the action.  Calls
362ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// marked 'nounwind' have no entry and must not be contained in the try-range
363ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// of any entry - they form gaps in the table.  Entries must be ordered by
364ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// try-range address.
365ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::
366ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
3671b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                     std::map<unsigned,CallSiteEntry*> &CallSiteIndexMap,
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);
4411b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        // For SjLj handling, map the call site entry to its index
4421b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
4431b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach          unsigned Index =
4441b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach            MF->getLandingPadCallSiteIndex(LandingPad->LandingPadBlock);
4451b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach          CallSiteIndexMap[Index] = &CallSites.back();
4461b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        }
447eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = true;
448eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      } else {
449eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Create a gap.
450eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
451eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
452eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
453eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
454eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
455eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If some instruction between the previous try-range and the end of the
456eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // function may throw, create a call-site entry with no landing pad for the
457eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // region following the try-range.
4581b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (SawPotentiallyThrowing &&
4591b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
460eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    CallSiteEntry Site = {LastLabel, 0, 0, 0};
461eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    CallSites.push_back(Site);
462eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
463ade025c65c12503aba161a4fa399fd97414abaffBill Wendling}
464ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
4650dafca90761097230f02e655fdd541f59b888315Bill Wendling/// EmitExceptionTable - Emit landing pads and actions.
4660dafca90761097230f02e655fdd541f59b888315Bill Wendling///
4670dafca90761097230f02e655fdd541f59b888315Bill Wendling/// The general organization of the table is complex, but the basic concepts are
4680dafca90761097230f02e655fdd541f59b888315Bill Wendling/// easy.  First there is a header which describes the location and organization
4690dafca90761097230f02e655fdd541f59b888315Bill Wendling/// of the three components that follow.
4700dafca90761097230f02e655fdd541f59b888315Bill Wendling///
4710dafca90761097230f02e655fdd541f59b888315Bill Wendling///  1. The landing pad site information describes the range of code covered by
4720dafca90761097230f02e655fdd541f59b888315Bill Wendling///     the try.  In our case it's an accumulation of the ranges covered by the
4730dafca90761097230f02e655fdd541f59b888315Bill Wendling///     invokes in the try.  There is also a reference to the landing pad that
4740dafca90761097230f02e655fdd541f59b888315Bill Wendling///     handles the exception once processed.  Finally an index into the actions
4750dafca90761097230f02e655fdd541f59b888315Bill Wendling///     table.
4760dafca90761097230f02e655fdd541f59b888315Bill Wendling///  2. The action table, in our case, is composed of pairs of type ids and next
4770dafca90761097230f02e655fdd541f59b888315Bill Wendling///     action offset.  Starting with the action index from the landing pad
4780dafca90761097230f02e655fdd541f59b888315Bill Wendling///     site, each type Id is checked for a match to the current exception.  If
4790dafca90761097230f02e655fdd541f59b888315Bill Wendling///     it matches then the exception and type id are passed on to the landing
4800dafca90761097230f02e655fdd541f59b888315Bill Wendling///     pad.  Otherwise the next action is looked up.  This chain is terminated
4810dafca90761097230f02e655fdd541f59b888315Bill Wendling///     with a next action of zero.  If no type id is found the the frame is
4820dafca90761097230f02e655fdd541f59b888315Bill Wendling///     unwound and handling continues.
4830dafca90761097230f02e655fdd541f59b888315Bill Wendling///  3. Type id table contains references to all the C++ typeinfo for all
4840dafca90761097230f02e655fdd541f59b888315Bill Wendling///     catches in the function.  This tables is reversed indexed base 1.
485ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::EmitExceptionTable() {
486ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
487ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
488ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
489ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  if (PadInfos.empty()) return;
490ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
491ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Sort the landing pads in order of their type ids.  This is used to fold
492ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // duplicate actions.
493ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<const LandingPadInfo *, 64> LandingPads;
494ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  LandingPads.reserve(PadInfos.size());
495ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
496ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
497ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    LandingPads.push_back(&PadInfos[i]);
498ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
499ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
500ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
501ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the actions table and gather the first action index for each
502ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // landing pad site.
503ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<ActionEntry, 32> Actions;
504ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<unsigned, 64> FirstActions;
505ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
506ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
507ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Invokes and nounwind calls have entries in PadMap (due to being bracketed
508ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // by try-range labels when lowered).  Ordinary calls do not, so appropriate
5091b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // try-ranges for them need be deduced when using Dwarf exception handling.
510ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  RangeMapType PadMap;
511ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
512ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    const LandingPadInfo *LandingPad = LandingPads[i];
513ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
514ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      unsigned BeginLabel = LandingPad->BeginLabels[j];
515ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
516ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadRange P = { i, j };
517ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadMap[BeginLabel] = P;
518ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    }
519ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  }
520ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
521ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the call-site table.
522ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<CallSiteEntry, 64> CallSites;
5231b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  std::map<unsigned,CallSiteEntry*> CallSiteIndexMap;
5241b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  ComputeCallSiteTable(CallSites, CallSiteIndexMap, PadMap,
5251b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                       LandingPads, FirstActions);
526eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
527eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Final tallies.
528eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
529eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Call sites.
530eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned SiteStartSize  = sizeof(int32_t); // DW_EH_PE_udata4
531eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
532eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
5331b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  unsigned SizeSites;
534bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
535bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach  bool HaveTTData = (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
536bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach    ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
537bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
538bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
5391b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
5401b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    SizeSites = (MF->getMaxCallSiteIndex() - CallSites.size()) *
5411b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      TargetAsmInfo::getULEB128Size(0) * 2;
5421b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else
5431b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    SizeSites = CallSites.size() *
5441b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      (SiteStartSize + SiteLengthSize + LandingPadSize);
5451b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
546eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
5471b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
5481b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      SizeSites += TargetAsmInfo::getULEB128Size(i);
5491b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      // FIXME: 'i' above should be the landing pad index
5501b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
551eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Type infos.
552eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
553eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
554eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
555eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned TypeOffset = sizeof(int8_t) + // Call site format
556eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
557eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeSites + SizeActions + SizeTypes;
558eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
559eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned TotalSize = sizeof(int8_t) + // LPStart format
560eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       sizeof(int8_t) + // TType format
561bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach       (HaveTTData ?
562bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach          TargetAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset
563eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       TypeOffset;
564eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
565eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeAlign = (4 - TotalSize) & 3;
566eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
567eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin the exception table.
568d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
569d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  Asm->SwitchToSection(LSDASection);
570eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
571eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "GCC_except_table" << SubprogramCount << ":\n";
572eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
573eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != SizeAlign; ++i) {
574eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(0);
575eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Padding");
576c5800a809fd5e411de78ac1a4be633cd3e1d7840Bill Wendling  }
577eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
578eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("exception", SubprogramCount);
5791b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
5801b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    std::string SjLjName = "_lsda_";
5811b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    SjLjName += MF->getFunction()->getName().str();
5821b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    EmitLabel(SjLjName.c_str(), 0);
5831b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
584eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
585eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the header.
586eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(dwarf::DW_EH_PE_omit);
587eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("LPStart format (DW_EH_PE_omit)");
588b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
589ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#if 0
59081c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  if (TypeInfos.empty() && FilterIds.empty()) {
591ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // If there are no typeinfos or filters, there is nothing to emit, optimize
592ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // by specifying the "omit" encoding.
59381c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner    Asm->EmitInt8(dwarf::DW_EH_PE_omit);
59481c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner    Asm->EOL("TType format (DW_EH_PE_omit)");
59581c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  } else {
596ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // Okay, we have actual filters or typeinfos to emit.  As such, we need to
597ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // pick a type encoding for them.  We're about to emit a list of pointers to
598ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // typeinfo objects at the end of the LSDA.  However, unless we're in static
599ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // mode, this reference will require a relocation by the dynamic linker.
60046b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //
601ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // Because of this, we have a couple of options:
602ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   1) If we are in -static mode, we can always use an absolute reference
603ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      from the LSDA, because the static linker will resolve it.
604ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   2) Otherwise, if the LSDA section is writable, we can output the direct
605ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      reference to the typeinfo and allow the dynamic linker to relocate
606ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      it.  Since it is in a writable section, the dynamic linker won't
607ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      have a problem.
608ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   3) Finally, if we're in PIC mode and the LDSA section isn't writable,
609ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      we need to use some form of indirection.  For example, on Darwin,
610ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      we can output a statically-relocatable reference to a dyld stub. The
611ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      offset to the stub is constant, but the contents are in a section
612ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      that is updated by the dynamic linker.  This is easy enough, but we
613ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      need to tell the personality function of the unwinder to indirect
614ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      through the dyld stub.
615ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //
616ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // FIXME: When this is actually implemented, we'll have to emit the stubs
617ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // somewhere.  This predicate should be moved to a shared location that is
618ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // in target-independent code.
619ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //
620ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    if (LSDASection->isWritable() ||
621ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner        Asm->TM.getRelocationModel() == Reloc::Static) {
622ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner      Asm->EmitInt8(DW_EH_PE_absptr);
623ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner      Asm->EOL("TType format (DW_EH_PE_absptr)");
624ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    } else {
625ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner      Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
626ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner      Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect"
627ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner               " | DW_EH_PE_sdata4)");
628ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    }
629b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EmitULEB128Bytes(TypeOffset);
630b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EOL("TType base offset");
631b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling  }
632ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#else
633bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach  // For SjLj exceptions, if there is no TypeInfo, then we just explicitly
6341b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // say that we're omitting that bit.
6351b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // FIXME: does this apply to Dwarf also? The above #if 0 implies yes?
636bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach  if (!HaveTTData) {
6371b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitInt8(dwarf::DW_EH_PE_omit);
6381b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("TType format (DW_EH_PE_omit)");
6391b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else {
6401b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
6411b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("TType format (DW_EH_PE_absptr)");
6421b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(TypeOffset);
6431b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("TType base offset");
6441b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
645ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#endif
646b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
6471b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // SjLj Exception handilng
6481b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (TAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
6491b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
6501b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("Call site format (DW_EH_PE_udata4)");
6511b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(SizeSites);
6521b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("Call-site table length");
6531b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
6541b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
6551b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    assert(MF->getCallSiteCount() == CallSites.size());
6561b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
6571b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // Emit the landing pad site information.
6581b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // SjLj handling assigned the call site indices in the front end, so
6591b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // we need to make sure the table here lines up with that. That's pretty
6601b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // horrible, and should be fixed ASAP to do that stuff in the back end
6611b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // instead.
6621b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    std::map<unsigned, CallSiteEntry*>::const_iterator I, E;
6631b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    I = CallSiteIndexMap.begin();
6641b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    E = CallSiteIndexMap.end();
6651b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    for (unsigned CurrIdx = 1; I != E; ++I) {
6661b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      // paranoia.
6671b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      assert(CurrIdx <= I->first);
6681b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      // Fill in any gaps in the table
6691b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      while (CurrIdx++ < I->first) {
6701b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        Asm->EmitULEB128Bytes(0);
6711b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        Asm->EOL("Filler landing pad");
6721b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        Asm->EmitULEB128Bytes(0);
6731b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        Asm->EOL("Filler action");
6741b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      }
6751b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const CallSiteEntry &S = *(I->second);
6761b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(I->first - 1);
6771b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Landing pad");
6781b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(S.Action);
6791b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Action");
6801b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    }
6811b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else {
6821b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // DWARF Exception handling
6831b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    assert(TAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
6841b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
6851b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
6861b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("Call site format (DW_EH_PE_udata4)");
6871b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(SizeSites);
6881b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EOL("Call-site table length");
689eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6901b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // Emit the landing pad site information.
6911b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    for (SmallVectorImpl<CallSiteEntry>::const_iterator
6925cff487665097d971067cbede1598e249f673182Bill Wendling         I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
6931b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const CallSiteEntry &S = *I;
6941b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const char *BeginTag;
6951b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      unsigned BeginNumber;
696eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6971b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.BeginLabel) {
6981b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginTag = "eh_func_begin";
6991b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginNumber = SubprogramCount;
7001b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      } else {
7011b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginTag = "label";
7021b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginNumber = S.BeginLabel;
7031b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      }
704eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7051b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
7061b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                        true, true);
7071b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Region start");
708eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7091b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.EndLabel)
7101b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
7111b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                       true);
7121b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      else
7131b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
714eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7151b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Region length");
716eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7171b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.PadLabel)
7181b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        Asm->EmitInt32(0);
7191b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      else
7201b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
7211b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                          true, true);
722eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7231b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Landing pad");
724eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7251b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(S.Action);
7261b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Action");
7271b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    }
728eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
729eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
730eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the actions.
7315cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<ActionEntry>::const_iterator
7325cff487665097d971067cbede1598e249f673182Bill Wendling         I = Actions.begin(), E = Actions.end(); I != E; ++I) {
7335cff487665097d971067cbede1598e249f673182Bill Wendling    const ActionEntry &Action = *I;
734eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
735eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo index");
736eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.NextAction);
737eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Next action");
738eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
739eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
740eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the type ids.
7415cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<GlobalVariable *>::const_reverse_iterator
7425cff487665097d971067cbede1598e249f673182Bill Wendling         I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
7435cff487665097d971067cbede1598e249f673182Bill Wendling    GlobalVariable *GV = *I;
744eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective();
745eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
746eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (GV) {
747eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      std::string GLN;
748eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << Asm->getGlobalLinkName(GV, GLN);
749eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
750eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << "0";
751eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
752eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
753eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo");
754eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
755eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
756eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the filter typeids.
7575cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<unsigned>::const_iterator
7585cff487665097d971067cbede1598e249f673182Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
7595cff487665097d971067cbede1598e249f673182Bill Wendling    unsigned TypeID = *I;
760eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(TypeID);
761eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Filter TypeInfo index");
762eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
763eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
764eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
765eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
766eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
767eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndModule - Emit all exception information that should come after the
768eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// content.
769eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndModule() {
7701b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (TAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
7711b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    return;
772eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
773eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
774eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
775eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (shouldEmitMovesModule || shouldEmitTableModule) {
776eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    const std::vector<Function *> Personalities = MMI->getPersonalities();
777eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (unsigned i = 0; i < Personalities.size(); ++i)
778eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitCommonEHFrame(Personalities[i], i);
779eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
780eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
781eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling           E = EHFrames.end(); I != E; ++I)
782eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitEHFrame(*I);
783eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
784eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
785eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
786eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
787eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
788eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
789eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// BeginFunction - Gather pre-function exception information.  Assumes being
790eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// emitted immediately after the function entry point.
791eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::BeginFunction(MachineFunction *MF) {
792eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
793eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
794eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
795eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  this->MF = MF;
796eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTable = shouldEmitMoves = false;
797eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
798eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (MMI && TAI->doesSupportExceptionHandling()) {
799eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Map all labels and get rid of any dead landing pads.
800eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    MMI->TidyLandingPads();
801eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
802eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If any landing pads survive, we need an EH table.
803eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (MMI->getLandingPads().size())
804eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitTable = true;
805eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
806eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // See if we need frame move info.
807eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
808eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitMoves = true;
809eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
810eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (shouldEmitMoves || shouldEmitTable)
811eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Assumes in correct section after the entry point.
812eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitLabel("eh_func_begin", ++SubprogramCount);
813eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
814eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
815eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTableModule |= shouldEmitTable;
816eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitMovesModule |= shouldEmitMoves;
817eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
818eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
819eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
820eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
821eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
822eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndFunction - Gather and emit post-function exception information.
823eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling///
824eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndFunction() {
825eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
826eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
827eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
828eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (shouldEmitMoves || shouldEmitTable) {
829eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_func_end", SubprogramCount);
830eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitExceptionTable();
831eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
832eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Save EH frame information
833eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EHFrames.push_back(
834e2cf37b88c089a71727b3ecd466856f0cd638813Chris Lattner        FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
835eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            SubprogramCount,
836eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MMI->getPersonalityIndex(),
837eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MF->getFrameInfo()->hasCalls(),
838eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            !MMI->getLandingPads().empty(),
839eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MMI->getFrameMoves(),
840eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MF->getFunction()));
841eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
842eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
843eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
844eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
845eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
846