DwarfException.cpp revision 049e98d641f90e90e8312d76cbf4c68908fb9d1d
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//
10639217cb6a3948800c71c39503f8def4873a535eBill 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"
18fc4da0cea8a0f6c9c1483b92c2dffda217c0ba24David Greene#include "llvm/CodeGen/MachineFunction.h"
19eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/CodeGen/MachineLocation.h"
206c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/MC/MCStreamer.h"
21af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner#include "llvm/MC/MCAsmInfo.h"
22eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetData.h"
23eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetFrameInfo.h"
24d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner#include "llvm/Target/TargetLoweringObjectFile.h"
25eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetOptions.h"
26d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner#include "llvm/Target/TargetRegisterInfo.h"
276c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/Dwarf.h"
286c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/Timer.h"
296c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/raw_ostream.h"
30eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/ADT/StringExtras.h"
31eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingusing namespace llvm;
32eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
33eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingstatic TimerGroup &getDwarfTimerGroup() {
34eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  static TimerGroup DwarfTimerGroup("Dwarf Exception");
35eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return DwarfTimerGroup;
36eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
37eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
38bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
39af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner                               const MCAsmInfo *T)
40bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
41bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    shouldEmitTableModule(false), shouldEmitMovesModule(false),
42bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer(0) {
43dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  if (TimePassesIsEnabled)
44bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer = new Timer("Dwarf Exception Writer",
45bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling                               getDwarfTimerGroup());
46bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
47bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
48bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::~DwarfException() {
49bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  delete ExceptionTimer;
50bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
51bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
527ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
537ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// is shared among many Frame Description Entries.  There is at least one CIE
547ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// in every non-empty .debug_frame section.
557ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendlingvoid DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
56eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Size and sign of stack growth.
57eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int stackGrowth =
58eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
59eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TargetFrameInfo::StackGrowsUp ?
60eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TD->getPointerSize() : -TD->getPointerSize();
61eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
62eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin eh frame section.
636c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
64eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
6533adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->is_EHSymbolPrivate())
6633adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getPrivateGlobalPrefix();
67eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
68eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "EH_frame" << Index << ":\n";
69eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("section_eh_frame", Index);
70eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
71eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define base labels.
72eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common", Index);
73eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
74eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define the eh frame length.
75eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitDifference("eh_frame_common_end", Index,
76eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                 "eh_frame_common_begin", Index, true);
77eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("Length of Common Information Entry");
78eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
79eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH frame header.
80eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_begin", Index);
81eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt32((int)0);
82eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Identifier Tag");
83eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(dwarf::DW_CIE_VERSION);
84eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Version");
85eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
86eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The personality presence indicates that language specific information will
87eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // show up in the eh frame.
88eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitString(Personality ? "zPLR" : "zR");
89eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Augmentation");
90eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
91eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Round out reader.
92eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitULEB128Bytes(1);
93eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Code Alignment Factor");
94eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitSLEB128Bytes(stackGrowth);
95eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Data Alignment Factor");
96eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
97eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Return Address Column");
98eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
994bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling  // If there is a personality, we need to indicate the function's location.
100eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (Personality) {
101eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(7);
102eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Augmentation Size");
103eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
10433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (MAI->getNeedsIndirectEncoding()) {
105c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
106c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands                    dwarf::DW_EH_PE_indirect);
107c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      Asm->EOL("Personality (pcrel sdata4 indirect)");
108eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
109c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
110c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      Asm->EOL("Personality (pcrel sdata4)");
111eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
112eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
113eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective(true);
11433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getPersonalityPrefix();
115eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
11633adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getPersonalitySuffix();
11733adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL"))
11833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner      O << "-" << MAI->getPCSymbol();
119eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Personality");
120eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
121c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
122c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands    Asm->EOL("LSDA Encoding (pcrel sdata4)");
123eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
124c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
125c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands    Asm->EOL("FDE Encoding (pcrel sdata4)");
126eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  } else {
127eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(1);
128eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Augmentation Size");
129eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
130c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
131c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands    Asm->EOL("FDE Encoding (pcrel sdata4)");
132eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
133eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
134eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Indicate locations of general callee saved registers in frame.
135eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  std::vector<MachineMove> Moves;
136eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  RI->getInitialFrameState(Moves);
137eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitFrameMoves(NULL, 0, Moves, true);
138eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
139eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // On Darwin the linker honors the alignment of eh_frame, which means it must
140eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // be 8-byte on 64-bit targets to match what gcc does.  Otherwise you get
141eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // holes which confuse readers of eh_frame.
142eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
143eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                     0, 0, false);
144eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_end", Index);
145eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
146eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL();
147eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
148eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
1497ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
1507ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendlingvoid DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
151dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
152eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         "Should not emit 'available externally' functions at all");
153eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
1543e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  const Function *TheFunc = EHFrameInfo.function;
155dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher
1566c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
157dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher
158eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Externally visible entry into the functions eh frame info. If the
159eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // corresponding function is static, this should not be externally visible.
1603e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!TheFunc->hasLocalLinkage())
16133adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
162eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
163eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
164eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If corresponding function is weak definition, this should be too.
16533adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
16633adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
167eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
168eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If there are no calls then you can't unwind.  This may mean we can omit the
169eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH Frame, but some environments do not handle weak absolute symbols. If
170eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // UnwindTablesMandatory is set we cannot do this optimization; the unwind
171eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // info is to be available for non-EH uses.
1723e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
1733e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner      (!TheFunc->isWeakForLinker() ||
17433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner       !MAI->getWeakDefDirective() ||
17533adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner       MAI->getSupportsWeakOmittedEHFrame())) {
176eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << " = 0\n";
177eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // This name has no connection to the function, so it might get
178eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripped when the function is not, erroneously.  Prohibit
179eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripping unconditionally.
18033adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (const char *UsedDirective = MAI->getUsedDirective())
181eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << UsedDirective << EHFrameInfo.FnName << "\n\n";
182eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  } else {
183eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << ":\n";
184eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
185eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // EH frame header.
186eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_frame_end", EHFrameInfo.Number,
187eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   "eh_frame_begin", EHFrameInfo.Number, true);
188eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Length of Frame Information Entry");
189eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
190eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_begin", EHFrameInfo.Number);
191eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
192a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner    EmitSectionOffset("eh_frame_begin", "eh_frame_common",
193a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
194a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      true, true, false);
195eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
196eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE CIE offset");
197eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
198c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands    EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
199eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE initial location");
200eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_func_end", EHFrameInfo.Number,
201c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands                   "eh_func_begin", EHFrameInfo.Number, true);
202eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE address range");
203eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
204eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If there is a personality and landing pads then point to the language
205eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // specific data area in the exception table.
206d44fff7849034fc17d6a0e9f9926542865bb7881Eric Christopher    if (MMI->getPersonalities()[0] != NULL) {
207c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      bool is4Byte = TD->getPointerSize() == sizeof(int32_t);
208c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands
2096fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher      Asm->EmitULEB128Bytes(is4Byte ? 4 : 8);
210eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
211eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
212c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      if (EHFrameInfo.hasLandingPads)
2136fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher        EmitReference("exception", EHFrameInfo.Number, true, false);
214c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      else {
2156fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher	if (is4Byte)
2166fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher	  Asm->EmitInt32((int)0);
2176fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher	else
2186fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher	  Asm->EmitInt64((int)0);
2196fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher      }
220eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Language Specific Data Area");
221eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
222eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitULEB128Bytes(0);
223eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
224eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
225eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
226eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Indicate locations of function specific callee saved registers in frame.
227dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher    EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
228eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   true);
229eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
230eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // On Darwin the linker honors the alignment of eh_frame, which means it
231eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // must be 8-byte on 64-bit targets to match what gcc does.  Otherwise you
232eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // get holes which confuse readers of eh_frame.
233eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
234eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       0, 0, false);
235eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_end", EHFrameInfo.Number);
236eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
237eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If the function is marked used, this table should be also.  We cannot
238eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // make the mark unconditional in this case, since retaining the table also
239eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // retains the function in this case, and there is code around that depends
240eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // on unused functions (calling undefined externals) being dead-stripped to
241eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // link correctly.  Yes, there really is.
242401e10c4fbfcdcfade5065093e2ca97f69a1d144Chris Lattner    if (MMI->isUsedFunction(EHFrameInfo.function))
24333adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner      if (const char *UsedDirective = MAI->getUsedDirective())
244eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        O << UsedDirective << EHFrameInfo.FnName << "\n\n";
245eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
2464bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling
2474bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling  Asm->EOL();
248eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
249eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
250eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// SharedTypeIds - How many leading type ids two landing pads have in common.
251eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingunsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
252eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                       const LandingPadInfo *R) {
253eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
254eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
255eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
256eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned Count = 0;
257eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
258eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (; Count != MinSize; ++Count)
259eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[Count] != RIds[Count])
260eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return Count;
261eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
262eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return Count;
263eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
264eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
265eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// PadLT - Order landing pads lexicographically by type id.
266eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingbool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
267eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
268eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
269eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
270eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
271eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != MinSize; ++i)
272eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[i] != RIds[i])
273eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return LIds[i] < RIds[i];
274eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
275eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return LSize < RSize;
276eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
277eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
278d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// ComputeActionsTable - Compute the actions table and gather the first action
279d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// index for each landing pad site.
280ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingunsigned DwarfException::
281ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
282ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<ActionEntry> &Actions,
283ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<unsigned> &FirstActions) {
284a583c55864e83e470333b7be878280b10e175a6eBill Wendling
285a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // The action table follows the call-site table in the LSDA. The individual
286a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // records are of two types:
287a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
288a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Catch clause
289a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Exception specification
290a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
291a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // The two record kinds have the same format, with only small differences.
292a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // They are distinguished by the "switch value" field: Catch clauses
293a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // (TypeInfos) have strictly positive switch values, and exception
294a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // specifications (FilterIds) have strictly negative switch values. Value 0
295a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // indicates a catch-all clause.
296a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
2975e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // Negative type IDs index into FilterIds. Positive type IDs index into
2985e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // TypeInfos.  The value written for a positive type ID is just the type ID
2995e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // itself.  For a negative type ID, however, the value written is the
300eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // (negative) byte offset of the corresponding FilterIds entry.  The byte
3015e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // offset is usually equal to the type ID (because the FilterIds entries are
3025e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // written using a variable width encoding, which outputs one byte per entry
3035e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // as long as the value written is not too large) but can differ.  This kind
3045e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // of complication does not occur for positive type IDs because type infos are
305eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // output using a fixed width encoding.  FilterOffsets[i] holds the byte
306eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // offset corresponding to FilterIds[i].
307409914b773ffd53d53d214394d5636a76f673186Bill Wendling
308409914b773ffd53d53d214394d5636a76f673186Bill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
309eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  SmallVector<int, 16> FilterOffsets;
310eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FilterOffsets.reserve(FilterIds.size());
311eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int Offset = -1;
312409914b773ffd53d53d214394d5636a76f673186Bill Wendling
313409914b773ffd53d53d214394d5636a76f673186Bill Wendling  for (std::vector<unsigned>::const_iterator
314409914b773ffd53d53d214394d5636a76f673186Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
315eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FilterOffsets.push_back(Offset);
316af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner    Offset -= MCAsmInfo::getULEB128Size(*I);
317eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
318eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
319eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FirstActions.reserve(LandingPads.size());
320eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
321eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int FirstAction = 0;
322eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeActions = 0;
3235e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  const LandingPadInfo *PrevLPI = 0;
324409914b773ffd53d53d214394d5636a76f673186Bill Wendling
3255cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
3265e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling         I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
3275e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const LandingPadInfo *LPI = *I;
3285e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const std::vector<int> &TypeIds = LPI->TypeIds;
3295e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
330eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    unsigned SizeSiteActions = 0;
331eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
332eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (NumShared < TypeIds.size()) {
333eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned SizeAction = 0;
334eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      ActionEntry *PrevAction = 0;
335eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
336eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (NumShared) {
3375e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        const unsigned SizePrevIds = PrevLPI->TypeIds.size();
338eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        assert(Actions.size());
339eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
340af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
341af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner          MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
342eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
343eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        for (unsigned j = NumShared; j != SizePrevIds; ++j) {
344eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction -=
345af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner            MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
346eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction += -PrevAction->NextAction;
347eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          PrevAction = PrevAction->Previous;
348eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
349eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
350eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
351eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Compute the actions.
3525e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling      for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
3535e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        int TypeID = TypeIds[J];
3545e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
355eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
356af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
357eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
358eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
359af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
360eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeSiteActions += SizeAction;
361eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
362a583c55864e83e470333b7be878280b10e175a6eBill Wendling        ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
363eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        Actions.push_back(Action);
364eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
365eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
366eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
367eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Record the first action of the landing pad site.
368eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
369eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } // else identical - re-use previous FirstAction
370eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
371a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Information used when created the call-site table. The action record
372a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // field of the call site record is the offset of the first associated
373a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // action record, relative to the start of the actions table. This value is
374a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // biased by 1 (1 in dicating the start of the actions table), and 0
375a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // indicates that there are no actions.
376eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FirstActions.push_back(FirstAction);
377eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
378eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Compute this sites contribution to size.
379eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeActions += SizeSiteActions;
3805e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
3815e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    PrevLPI = LPI;
382eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
383eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
3845e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  return SizeActions;
3855e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling}
3865e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
387ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// ComputeCallSiteTable - Compute the call-site table.  The entry for an invoke
388a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// has a try-range containing the call, a non-zero landing pad, and an
389ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// appropriate action.  The entry for an ordinary call has a try-range
390ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// containing the call and zero for the landing pad and the action.  Calls
391ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// marked 'nounwind' have no entry and must not be contained in the try-range
392ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// of any entry - they form gaps in the table.  Entries must be ordered by
393ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// try-range address.
394ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::
395ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
396ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const RangeMapType &PadMap,
397ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
398ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const SmallVectorImpl<unsigned> &FirstActions) {
399eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The end label of the previous invoke or nounwind try-range.
400eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LastLabel = 0;
401eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
402eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Whether there is a potentially throwing instruction (currently this means
403eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // an ordinary call) between the end of the previous try-range and now.
404eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  bool SawPotentiallyThrowing = false;
405eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
4065cff487665097d971067cbede1598e249f673182Bill Wendling  // Whether the last CallSite entry was for an invoke.
407eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  bool PreviousIsInvoke = false;
408eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
409eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Visit all instructions in order of address.
410eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
411eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       I != E; ++I) {
412eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
413eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         MI != E; ++MI) {
414eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (!MI->isLabel()) {
415eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SawPotentiallyThrowing |= MI->getDesc().isCall();
416eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        continue;
417eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
418eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
419eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned BeginLabel = MI->getOperand(0).getImm();
420eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel && "Invalid label!");
421eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
422eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // End of the previous try-range?
423eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (BeginLabel == LastLabel)
424eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SawPotentiallyThrowing = false;
425eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
426eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Beginning of a new try-range?
427eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      RangeMapType::iterator L = PadMap.find(BeginLabel);
428eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (L == PadMap.end())
429eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Nope, it was just some random label.
430eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        continue;
431eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
432a583c55864e83e470333b7be878280b10e175a6eBill Wendling      const PadRange &P = L->second;
433eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
434eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
435eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling             "Inconsistent landing pad map!");
436eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
437a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // For Dwarf exception handling (SjLj handling doesn't use this). If some
438a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // instruction between the previous try-range and this one may throw,
439a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // create a call-site entry with no landing pad for the region between the
440a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // try-ranges.
4411b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (SawPotentiallyThrowing &&
44233adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner          MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
443a583c55864e83e470333b7be878280b10e175a6eBill Wendling        CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
444eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSites.push_back(Site);
445eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
446eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
447eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
448eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      LastLabel = LandingPad->EndLabels[P.RangeIndex];
449eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel && LastLabel && "Invalid landing pad!");
450eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
451eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (LandingPad->LandingPadLabel) {
452eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // This try-range is for an invoke.
453a583c55864e83e470333b7be878280b10e175a6eBill Wendling        CallSiteEntry Site = {
454a583c55864e83e470333b7be878280b10e175a6eBill Wendling          BeginLabel,
455a583c55864e83e470333b7be878280b10e175a6eBill Wendling          LastLabel,
456a583c55864e83e470333b7be878280b10e175a6eBill Wendling          LandingPad->LandingPadLabel,
457a583c55864e83e470333b7be878280b10e175a6eBill Wendling          FirstActions[P.PadIndex]
458a583c55864e83e470333b7be878280b10e175a6eBill Wendling        };
459eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
4600bb61c56bcad7a738d50f705b5966be446ef06c1Jim Grosbach        // Try to merge with the previous call-site. SJLJ doesn't do this
4610bb61c56bcad7a738d50f705b5966be446ef06c1Jim Grosbach        if (PreviousIsInvoke &&
4620bb61c56bcad7a738d50f705b5966be446ef06c1Jim Grosbach          MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
463eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          CallSiteEntry &Prev = CallSites.back();
464eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
465eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            // Extend the range of the previous entry.
466eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            Prev.EndLabel = Site.EndLabel;
467eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            continue;
468eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          }
469eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
470eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
471eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Otherwise, create a new call-site.
472eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSites.push_back(Site);
473eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = true;
474eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      } else {
475eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Create a gap.
476eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
477eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
478eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
479eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
480eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
481eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If some instruction between the previous try-range and the end of the
482eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // function may throw, create a call-site entry with no landing pad for the
483eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // region following the try-range.
4841b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (SawPotentiallyThrowing &&
48533adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner      MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
486a583c55864e83e470333b7be878280b10e175a6eBill Wendling    CallSiteEntry Site = { LastLabel, 0, 0, 0 };
487eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    CallSites.push_back(Site);
488eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
489ade025c65c12503aba161a4fa399fd97414abaffBill Wendling}
490ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
4910dafca90761097230f02e655fdd541f59b888315Bill Wendling/// EmitExceptionTable - Emit landing pads and actions.
4920dafca90761097230f02e655fdd541f59b888315Bill Wendling///
4930dafca90761097230f02e655fdd541f59b888315Bill Wendling/// The general organization of the table is complex, but the basic concepts are
4940dafca90761097230f02e655fdd541f59b888315Bill Wendling/// easy.  First there is a header which describes the location and organization
4950dafca90761097230f02e655fdd541f59b888315Bill Wendling/// of the three components that follow.
496dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher///
4970dafca90761097230f02e655fdd541f59b888315Bill Wendling///  1. The landing pad site information describes the range of code covered by
4980dafca90761097230f02e655fdd541f59b888315Bill Wendling///     the try.  In our case it's an accumulation of the ranges covered by the
4990dafca90761097230f02e655fdd541f59b888315Bill Wendling///     invokes in the try.  There is also a reference to the landing pad that
5000dafca90761097230f02e655fdd541f59b888315Bill Wendling///     handles the exception once processed.  Finally an index into the actions
5010dafca90761097230f02e655fdd541f59b888315Bill Wendling///     table.
502a583c55864e83e470333b7be878280b10e175a6eBill Wendling///  2. The action table, in our case, is composed of pairs of type IDs and next
5030dafca90761097230f02e655fdd541f59b888315Bill Wendling///     action offset.  Starting with the action index from the landing pad
504a583c55864e83e470333b7be878280b10e175a6eBill Wendling///     site, each type ID is checked for a match to the current exception.  If
5050dafca90761097230f02e655fdd541f59b888315Bill Wendling///     it matches then the exception and type id are passed on to the landing
5060dafca90761097230f02e655fdd541f59b888315Bill Wendling///     pad.  Otherwise the next action is looked up.  This chain is terminated
5070dafca90761097230f02e655fdd541f59b888315Bill Wendling///     with a next action of zero.  If no type id is found the the frame is
5080dafca90761097230f02e655fdd541f59b888315Bill Wendling///     unwound and handling continues.
509a583c55864e83e470333b7be878280b10e175a6eBill Wendling///  3. Type ID table contains references to all the C++ typeinfo for all
5100dafca90761097230f02e655fdd541f59b888315Bill Wendling///     catches in the function.  This tables is reversed indexed base 1.
511ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::EmitExceptionTable() {
512ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
513ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
514ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
515ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  if (PadInfos.empty()) return;
516ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
517ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Sort the landing pads in order of their type ids.  This is used to fold
518ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // duplicate actions.
519ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<const LandingPadInfo *, 64> LandingPads;
520ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  LandingPads.reserve(PadInfos.size());
521ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
522ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
523ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    LandingPads.push_back(&PadInfos[i]);
524ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
525ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
526ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
527ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the actions table and gather the first action index for each
528ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // landing pad site.
529ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<ActionEntry, 32> Actions;
530ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<unsigned, 64> FirstActions;
531ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
532ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
533ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Invokes and nounwind calls have entries in PadMap (due to being bracketed
534ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // by try-range labels when lowered).  Ordinary calls do not, so appropriate
5351b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // try-ranges for them need be deduced when using Dwarf exception handling.
536ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  RangeMapType PadMap;
537ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
538ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    const LandingPadInfo *LandingPad = LandingPads[i];
539ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
540ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      unsigned BeginLabel = LandingPad->BeginLabels[j];
541ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
542ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadRange P = { i, j };
543ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadMap[BeginLabel] = P;
544ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    }
545ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  }
546ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
547ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the call-site table.
548ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<CallSiteEntry, 64> CallSites;
5498b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach  ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
550eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
551eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Final tallies.
552eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
553eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Call sites.
554eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned SiteStartSize  = sizeof(int32_t); // DW_EH_PE_udata4
555eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
556eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
5571b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  unsigned SizeSites;
558bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
55933adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  bool HaveTTData = (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
560bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach    ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
561bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
562bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
56333adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
5648b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    SizeSites = 0;
5651b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else
5661b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    SizeSites = CallSites.size() *
5671b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      (SiteStartSize + SiteLengthSize + LandingPadSize);
5681b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
569af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner    SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
57033adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
571af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner      SizeSites += MCAsmInfo::getULEB128Size(i);
5721b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
573eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Type infos.
574eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
575eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
576eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
577eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned TypeOffset = sizeof(int8_t) + // Call site format
578af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner    MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
579eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeSites + SizeActions + SizeTypes;
580eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
581eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned TotalSize = sizeof(int8_t) + // LPStart format
582eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       sizeof(int8_t) + // TType format
583bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach       (HaveTTData ?
584af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner          MCAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset
585eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       TypeOffset;
586eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
587eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeAlign = (4 - TotalSize) & 3;
588eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
589eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin the exception table.
590d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
5916c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(LSDASection);
592eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
593eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "GCC_except_table" << SubprogramCount << ":\n";
594eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
595eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != SizeAlign; ++i) {
596eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(0);
597eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Padding");
598c5800a809fd5e411de78ac1a4be633cd3e1d7840Bill Wendling  }
599eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
600eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("exception", SubprogramCount);
60133adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
6021b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    std::string SjLjName = "_lsda_";
6031b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    SjLjName += MF->getFunction()->getName().str();
6041b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    EmitLabel(SjLjName.c_str(), 0);
6051b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
606eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
607eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the header.
608639217cb6a3948800c71c39503f8def4873a535eBill Wendling  Asm->EmitInt8(dwarf::DW_EH_PE_omit);
609639217cb6a3948800c71c39503f8def4873a535eBill Wendling  Asm->EOL("@LPStart format (DW_EH_PE_omit)");
610b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
611ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#if 0
61281c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  if (TypeInfos.empty() && FilterIds.empty()) {
613ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // If there are no typeinfos or filters, there is nothing to emit, optimize
614ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // by specifying the "omit" encoding.
615639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_omit);
616639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EOL("@TType format (DW_EH_PE_omit)");
61781c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  } else {
618ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // Okay, we have actual filters or typeinfos to emit.  As such, we need to
619ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // pick a type encoding for them.  We're about to emit a list of pointers to
620ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // typeinfo objects at the end of the LSDA.  However, unless we're in static
621ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // mode, this reference will require a relocation by the dynamic linker.
62246b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //
623ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // Because of this, we have a couple of options:
624ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   1) If we are in -static mode, we can always use an absolute reference
625ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      from the LSDA, because the static linker will resolve it.
626ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   2) Otherwise, if the LSDA section is writable, we can output the direct
627ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      reference to the typeinfo and allow the dynamic linker to relocate
628ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      it.  Since it is in a writable section, the dynamic linker won't
629ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      have a problem.
630ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   3) Finally, if we're in PIC mode and the LDSA section isn't writable,
631ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      we need to use some form of indirection.  For example, on Darwin,
632ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      we can output a statically-relocatable reference to a dyld stub. The
633ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      offset to the stub is constant, but the contents are in a section
634ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      that is updated by the dynamic linker.  This is easy enough, but we
635ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      need to tell the personality function of the unwinder to indirect
636ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      through the dyld stub.
637ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //
638ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // FIXME: When this is actually implemented, we'll have to emit the stubs
639ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // somewhere.  This predicate should be moved to a shared location that is
640ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // in target-independent code.
641ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //
642ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    if (LSDASection->isWritable() ||
643ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner        Asm->TM.getRelocationModel() == Reloc::Static) {
644639217cb6a3948800c71c39503f8def4873a535eBill Wendling      Asm->EmitInt8(DW_EH_PE_absptr);
645639217cb6a3948800c71c39503f8def4873a535eBill Wendling      Asm->EOL("TType format (DW_EH_PE_absptr)");
646ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    } else {
647ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner      Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
648639217cb6a3948800c71c39503f8def4873a535eBill Wendling      Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect"
649639217cb6a3948800c71c39503f8def4873a535eBill Wendling               " | DW_EH_PE_sdata4)");
650ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    }
651b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EmitULEB128Bytes(TypeOffset);
652b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EOL("TType base offset");
653b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling  }
654ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#else
655bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach  // For SjLj exceptions, if there is no TypeInfo, then we just explicitly
6561b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // say that we're omitting that bit.
6571b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // FIXME: does this apply to Dwarf also? The above #if 0 implies yes?
658bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach  if (!HaveTTData) {
659639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_omit);
660639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EOL("@TType format (DW_EH_PE_omit)");
6611b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else {
662639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
663639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EOL("@TType format (DW_EH_PE_absptr)");
6641b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(TypeOffset);
665a583c55864e83e470333b7be878280b10e175a6eBill Wendling    Asm->EOL("@TType base offset");
6661b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
667ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#endif
668b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
6691b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // SjLj Exception handilng
67033adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
671639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
672639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EOL("Call site format (DW_EH_PE_udata4)");
6731b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(SizeSites);
674a583c55864e83e470333b7be878280b10e175a6eBill Wendling    Asm->EOL("Call site table length");
6751b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
6761b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // Emit the landing pad site information.
6778b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    unsigned idx = 0;
6788b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    for (SmallVectorImpl<CallSiteEntry>::const_iterator
6798b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach         I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
6808b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach      const CallSiteEntry &S = *I;
681a583c55864e83e470333b7be878280b10e175a6eBill Wendling
682a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the landing pad, counted in 16-byte bundles relative to the
683a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // @LPStart address.
6848b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach      Asm->EmitULEB128Bytes(idx);
6851b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Landing pad");
686a583c55864e83e470333b7be878280b10e175a6eBill Wendling
687a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the first associated action record, relative to the start of
688a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table. This value is biased by 1 (1 indicates the start of
689a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table), and 0 indicates that there are no actions.
6901b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(S.Action);
6911b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Action");
6921b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    }
6931b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else {
6941b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // DWARF Exception handling
69533adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
6961b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
697a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // The call-site table is a list of all call sites that may throw an
698a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // exception (including C++ 'throw' statements) in the procedure
699a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // fragment. It immediately follows the LSDA header. Each entry indicates,
700a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // for a given call, the first corresponding action record and corresponding
701a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // landing pad.
702a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
703a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // The table begins with the number of bytes, stored as an LEB128
704a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // compressed, unsigned integer. The records immediately follow the record
705a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // count. They are sorted in increasing call-site address. Each record
706a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // indicates:
707a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
708a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * The position of the call-site.
709a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * The position of the landing pad.
710a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * The first action record for that call site.
711a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
712a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // A missing entry in the call-site table indicates that a call is not
713a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // supposed to throw. Such calls include:
714a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
715a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * Calls to destructors within cleanup code. C++ semantics forbids these
716a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //     calls to throw.
717a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * Calls to intrinsic routines in the standard library which are known
718a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //     not to throw (sin, memcpy, et al).
719a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
720a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // If the runtime does not find the call-site entry for a given call, it
721a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // will call `terminate()'.
722a583c55864e83e470333b7be878280b10e175a6eBill Wendling
723a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Emit the landing pad call site table.
724639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
725639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EOL("Call site format (DW_EH_PE_udata4)");
7261b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(SizeSites);
727a583c55864e83e470333b7be878280b10e175a6eBill Wendling    Asm->EOL("Call site table size");
728eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7291b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    for (SmallVectorImpl<CallSiteEntry>::const_iterator
7305cff487665097d971067cbede1598e249f673182Bill Wendling         I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
7311b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const CallSiteEntry &S = *I;
7321b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const char *BeginTag;
7331b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      unsigned BeginNumber;
734eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7351b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.BeginLabel) {
7361b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginTag = "eh_func_begin";
7371b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginNumber = SubprogramCount;
7381b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      } else {
7391b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginTag = "label";
7401b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginNumber = S.BeginLabel;
7411b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      }
742eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
743a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the call site relative to the previous call site, counted in
744a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // number of 16-byte bundles. The first call site is counted relative to
745a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the start of the procedure fragment.
7461b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
7471b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                        true, true);
7481b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Region start");
749eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7501b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.EndLabel)
7511b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
7521b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                       true);
7531b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      else
7541b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
755eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7561b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Region length");
757eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
758a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the landing pad, counted in 16-byte bundles relative to the
759a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // @LPStart address.
7601b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.PadLabel)
7611b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        Asm->EmitInt32(0);
7621b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      else
7631b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
7641b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                          true, true);
765eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7661b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Landing pad");
767eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
768a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the first associated action record, relative to the start of
769a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table. This value is biased by 1 (1 indicates the start of
770a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table), and 0 indicates that there are no actions.
7711b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(S.Action);
7721b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Action");
7731b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    }
774eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
775eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
776a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // Emit the Action Table.
7775cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<ActionEntry>::const_iterator
7785cff487665097d971067cbede1598e249f673182Bill Wendling         I = Actions.begin(), E = Actions.end(); I != E; ++I) {
7795cff487665097d971067cbede1598e249f673182Bill Wendling    const ActionEntry &Action = *I;
780a583c55864e83e470333b7be878280b10e175a6eBill Wendling
781a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Type Filter
782a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
783a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   Used by the runtime to match the type of the thrown exception to the
784a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   type of the catch clauses or the types in the exception specification.
785a583c55864e83e470333b7be878280b10e175a6eBill Wendling
786eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
787eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo index");
788a583c55864e83e470333b7be878280b10e175a6eBill Wendling
789a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Action Record
790a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
791a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   Self-relative signed displacement in bytes of the next action record,
792a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   or 0 if there is no next action record.
793a583c55864e83e470333b7be878280b10e175a6eBill Wendling
794eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.NextAction);
795eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Next action");
796eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
797eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
798a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // Emit the Catch Clauses. The code for the catch clauses following the same
799a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // try is similar to a switch statement. The catch clause action record
800a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // informs the runtime about the type of a catch clause and about the
801a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // associated switch value.
802a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
803a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //  Action Record Fields:
804dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  //
805a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Filter Value
806a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     Positive value, starting at 1. Index in the types table of the
807a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     __typeinfo for the catch-clause type. 1 is the first word preceding
808a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     TTBase, 2 is the second word, and so on. Used by the runtime to check
809a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     if the thrown exception type matches the catch-clause type. Back-end
810a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     generated switch statements check against this value.
811a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
812a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Next
813a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     Signed offset, in bytes from the start of this field, to the next
814a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     chained action record, or zero if none.
815a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
816a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // The order of the action records determined by the next field is the order
817a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // of the catch clauses as they appear in the source code, and must be kept in
818a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // the same order. As a result, changing the order of the catch clause would
819a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // change the semantics of the program.
8205cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<GlobalVariable *>::const_reverse_iterator
8215cff487665097d971067cbede1598e249f673182Bill Wendling         I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
822a583c55864e83e470333b7be878280b10e175a6eBill Wendling    const GlobalVariable *GV = *I;
823eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective();
824eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
825eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (GV) {
826eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      std::string GLN;
827eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << Asm->getGlobalLinkName(GV, GLN);
828eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
829049e98d641f90e90e8312d76cbf4c68908fb9d1dBill Wendling      O << "0x0";
830eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
831eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
832eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo");
833eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
834eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
835a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // Emit the Type Table.
8365cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<unsigned>::const_iterator
8375cff487665097d971067cbede1598e249f673182Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
8385cff487665097d971067cbede1598e249f673182Bill Wendling    unsigned TypeID = *I;
839eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(TypeID);
840eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Filter TypeInfo index");
841eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
842eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
843eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
844eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
845eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
846eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndModule - Emit all exception information that should come after the
847eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// content.
848eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndModule() {
84933adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
8501b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    return;
851eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
852eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
853eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
854eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (shouldEmitMovesModule || shouldEmitTableModule) {
855eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    const std::vector<Function *> Personalities = MMI->getPersonalities();
856eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (unsigned i = 0; i < Personalities.size(); ++i)
8577ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling      EmitCIE(Personalities[i], i);
858eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
859eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
860eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling           E = EHFrames.end(); I != E; ++I)
8617ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling      EmitFDE(*I);
862eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
863eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
864eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
865eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
866eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
867eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
868eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// BeginFunction - Gather pre-function exception information.  Assumes being
869eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// emitted immediately after the function entry point.
870eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::BeginFunction(MachineFunction *MF) {
871eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
872eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
873eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
874eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  this->MF = MF;
875eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTable = shouldEmitMoves = false;
876eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
87733adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MMI && MAI->doesSupportExceptionHandling()) {
878eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Map all labels and get rid of any dead landing pads.
879eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    MMI->TidyLandingPads();
880eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
881eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If any landing pads survive, we need an EH table.
882eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (MMI->getLandingPads().size())
883eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitTable = true;
884eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
885eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // See if we need frame move info.
886eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
887eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitMoves = true;
888eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
889eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (shouldEmitMoves || shouldEmitTable)
890eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Assumes in correct section after the entry point.
891eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitLabel("eh_func_begin", ++SubprogramCount);
892eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
893eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
894eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTableModule |= shouldEmitTable;
895eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitMovesModule |= shouldEmitMoves;
896eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
897eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
898eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
899eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
900eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
901eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndFunction - Gather and emit post-function exception information.
902eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling///
903eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndFunction() {
904dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  if (TimePassesIsEnabled)
905eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
906eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
907eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (shouldEmitMoves || shouldEmitTable) {
908eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_func_end", SubprogramCount);
909eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitExceptionTable();
910eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
911eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Save EH frame information
912eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EHFrames.push_back(
913e2cf37b88c089a71727b3ecd466856f0cd638813Chris Lattner        FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
914eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            SubprogramCount,
915eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MMI->getPersonalityIndex(),
916eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MF->getFrameInfo()->hasCalls(),
917eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            !MMI->getLandingPads().empty(),
918eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MMI->getFrameMoves(),
919eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MF->getFunction()));
920eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
921eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
922dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  if (TimePassesIsEnabled)
923eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
924eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
925