DwarfException.cpp revision fe220285b5ef1e4480ddd7f7c8bb182b88a33b16
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"
2043e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling#include "llvm/MC/MCSection.h"
216c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/MC/MCStreamer.h"
22af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner#include "llvm/MC/MCAsmInfo.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"
286c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/Dwarf.h"
293fb2b1ede30193b59a651328a946174196b20610Jim Grosbach#include "llvm/Support/Mangler.h"
306c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/Timer.h"
316c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/raw_ostream.h"
32c40d9f9bae70c83947bf8fa5f9ee97adbf1bb0c0Jim Grosbach#include "llvm/ADT/SmallString.h"
33eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/ADT/StringExtras.h"
34eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingusing namespace llvm;
35eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
36eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingstatic TimerGroup &getDwarfTimerGroup() {
37eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  static TimerGroup DwarfTimerGroup("Dwarf Exception");
38eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return DwarfTimerGroup;
39eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
40eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
41bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
42af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner                               const MCAsmInfo *T)
43bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
44bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    shouldEmitTableModule(false), shouldEmitMovesModule(false),
45bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer(0) {
46dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  if (TimePassesIsEnabled)
47bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer = new Timer("Dwarf Exception Writer",
48bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling                               getDwarfTimerGroup());
49bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
50bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
51bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::~DwarfException() {
52bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  delete ExceptionTimer;
53bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
54bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
55bb3e29932d14cc012149a1ab452b88e6f6f5bb49Bill Wendling/// SizeOfEncodedValue - Return the size of the encoding in bytes.
5652783c63a13d384582997684384553e6545852d3Bill Wendlingunsigned DwarfException::SizeOfEncodedValue(unsigned Encoding) {
5752783c63a13d384582997684384553e6545852d3Bill Wendling  if (Encoding == dwarf::DW_EH_PE_omit)
5852783c63a13d384582997684384553e6545852d3Bill Wendling    return 0;
5952783c63a13d384582997684384553e6545852d3Bill Wendling
6052783c63a13d384582997684384553e6545852d3Bill Wendling  switch (Encoding & 0x07) {
6152783c63a13d384582997684384553e6545852d3Bill Wendling  case dwarf::DW_EH_PE_absptr:
6252783c63a13d384582997684384553e6545852d3Bill Wendling    return TD->getPointerSize();
6352783c63a13d384582997684384553e6545852d3Bill Wendling  case dwarf::DW_EH_PE_udata2:
6452783c63a13d384582997684384553e6545852d3Bill Wendling    return 2;
6552783c63a13d384582997684384553e6545852d3Bill Wendling  case dwarf::DW_EH_PE_udata4:
6652783c63a13d384582997684384553e6545852d3Bill Wendling    return 4;
6752783c63a13d384582997684384553e6545852d3Bill Wendling  case dwarf::DW_EH_PE_udata8:
6852783c63a13d384582997684384553e6545852d3Bill Wendling    return 8;
6952783c63a13d384582997684384553e6545852d3Bill Wendling  }
7052783c63a13d384582997684384553e6545852d3Bill Wendling
7152783c63a13d384582997684384553e6545852d3Bill Wendling  llvm_unreachable("Invalid encoded value.");
7252783c63a13d384582997684384553e6545852d3Bill Wendling  return 0;
7352783c63a13d384582997684384553e6545852d3Bill Wendling}
7452783c63a13d384582997684384553e6545852d3Bill Wendling
757ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
767ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// is shared among many Frame Description Entries.  There is at least one CIE
777ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// in every non-empty .debug_frame section.
787ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendlingvoid DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
79eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Size and sign of stack growth.
80eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int stackGrowth =
81eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
82eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TargetFrameInfo::StackGrowsUp ?
83eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TD->getPointerSize() : -TD->getPointerSize();
84eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
85eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin eh frame section.
866c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
87eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
8833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->is_EHSymbolPrivate())
8933adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getPrivateGlobalPrefix();
90eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
91eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "EH_frame" << Index << ":\n";
92eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("section_eh_frame", Index);
93eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
94eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define base labels.
95eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common", Index);
96eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
97eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define the eh frame length.
98eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitDifference("eh_frame_common_end", Index,
99eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                 "eh_frame_common_begin", Index, true);
100eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("Length of Common Information Entry");
101eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
102eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH frame header.
103eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_begin", Index);
104eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt32((int)0);
105eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Identifier Tag");
106eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(dwarf::DW_CIE_VERSION);
107eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Version");
108eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
109eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The personality presence indicates that language specific information will
110eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // show up in the eh frame.
11152783c63a13d384582997684384553e6545852d3Bill Wendling
11252783c63a13d384582997684384553e6545852d3Bill Wendling  // FIXME: Don't hardcode these encodings.
11352783c63a13d384582997684384553e6545852d3Bill Wendling  unsigned PerEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
11452783c63a13d384582997684384553e6545852d3Bill Wendling  if (Personality && MAI->getNeedsIndirectEncoding())
11552783c63a13d384582997684384553e6545852d3Bill Wendling    PerEncoding |= dwarf::DW_EH_PE_indirect;
11652783c63a13d384582997684384553e6545852d3Bill Wendling  unsigned LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
11752783c63a13d384582997684384553e6545852d3Bill Wendling  unsigned FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
11852783c63a13d384582997684384553e6545852d3Bill Wendling
11952783c63a13d384582997684384553e6545852d3Bill Wendling  char Augmentation[5] = { 0 };
12052783c63a13d384582997684384553e6545852d3Bill Wendling  unsigned AugmentationSize = 0;
12152783c63a13d384582997684384553e6545852d3Bill Wendling  char *APtr = Augmentation + 1;
12252783c63a13d384582997684384553e6545852d3Bill Wendling
12352783c63a13d384582997684384553e6545852d3Bill Wendling  if (Personality) {
12452783c63a13d384582997684384553e6545852d3Bill Wendling    // There is a personality function.
12552783c63a13d384582997684384553e6545852d3Bill Wendling    *APtr++ = 'P';
12652783c63a13d384582997684384553e6545852d3Bill Wendling    AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding);
12752783c63a13d384582997684384553e6545852d3Bill Wendling  }
12852783c63a13d384582997684384553e6545852d3Bill Wendling
12952783c63a13d384582997684384553e6545852d3Bill Wendling  if (UsesLSDA[Index]) {
13052783c63a13d384582997684384553e6545852d3Bill Wendling    // An LSDA pointer is in the FDE augmentation.
13152783c63a13d384582997684384553e6545852d3Bill Wendling    *APtr++ = 'L';
13252783c63a13d384582997684384553e6545852d3Bill Wendling    ++AugmentationSize;
13352783c63a13d384582997684384553e6545852d3Bill Wendling  }
13452783c63a13d384582997684384553e6545852d3Bill Wendling
13552783c63a13d384582997684384553e6545852d3Bill Wendling  if (FDEEncoding != dwarf::DW_EH_PE_absptr) {
13652783c63a13d384582997684384553e6545852d3Bill Wendling    // A non-default pointer encoding for the FDE.
13752783c63a13d384582997684384553e6545852d3Bill Wendling    *APtr++ = 'R';
13852783c63a13d384582997684384553e6545852d3Bill Wendling    ++AugmentationSize;
13952783c63a13d384582997684384553e6545852d3Bill Wendling  }
14052783c63a13d384582997684384553e6545852d3Bill Wendling
14152783c63a13d384582997684384553e6545852d3Bill Wendling  if (APtr != Augmentation + 1)
14252783c63a13d384582997684384553e6545852d3Bill Wendling    Augmentation[0] = 'z';
14352783c63a13d384582997684384553e6545852d3Bill Wendling
14452783c63a13d384582997684384553e6545852d3Bill Wendling  Asm->EmitString(Augmentation);
145eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Augmentation");
146eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
147eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Round out reader.
148eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitULEB128Bytes(1);
149eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Code Alignment Factor");
150eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitSLEB128Bytes(stackGrowth);
151eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Data Alignment Factor");
152eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
153eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Return Address Column");
154eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
15552783c63a13d384582997684384553e6545852d3Bill Wendling  Asm->EmitULEB128Bytes(AugmentationSize);
15652783c63a13d384582997684384553e6545852d3Bill Wendling  Asm->EOL("Augmentation Size");
15752783c63a13d384582997684384553e6545852d3Bill Wendling
15852783c63a13d384582997684384553e6545852d3Bill Wendling  Asm->EmitInt8(PerEncoding);
15952783c63a13d384582997684384553e6545852d3Bill Wendling  Asm->EOL("Personality", PerEncoding);
16052783c63a13d384582997684384553e6545852d3Bill Wendling
1614bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling  // If there is a personality, we need to indicate the function's location.
162eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (Personality) {
163eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective(true);
16433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getPersonalityPrefix();
165eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
16633adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getPersonalitySuffix();
16733adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL"))
16833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner      O << "-" << MAI->getPCSymbol();
169eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Personality");
170eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
17152783c63a13d384582997684384553e6545852d3Bill Wendling    Asm->EmitInt8(LSDAEncoding);
17252783c63a13d384582997684384553e6545852d3Bill Wendling    Asm->EOL("LSDA Encoding", LSDAEncoding);
173eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
17452783c63a13d384582997684384553e6545852d3Bill Wendling    Asm->EmitInt8(FDEEncoding);
17552783c63a13d384582997684384553e6545852d3Bill Wendling    Asm->EOL("FDE Encoding", FDEEncoding);
176eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
177eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
178eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Indicate locations of general callee saved registers in frame.
179eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  std::vector<MachineMove> Moves;
180eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  RI->getInitialFrameState(Moves);
181eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitFrameMoves(NULL, 0, Moves, true);
182eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
183eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // On Darwin the linker honors the alignment of eh_frame, which means it must
184eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // be 8-byte on 64-bit targets to match what gcc does.  Otherwise you get
185eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // holes which confuse readers of eh_frame.
186eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
187eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                     0, 0, false);
188eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_end", Index);
189eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
190eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL();
191eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
192eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
1937ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
1947ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendlingvoid DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
195dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
196eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         "Should not emit 'available externally' functions at all");
197eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
1983e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  const Function *TheFunc = EHFrameInfo.function;
199dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher
2006c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
201dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher
202eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Externally visible entry into the functions eh frame info. If the
203eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // corresponding function is static, this should not be externally visible.
2043e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!TheFunc->hasLocalLinkage())
20533adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
206eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
207eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
208eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If corresponding function is weak definition, this should be too.
20933adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
21033adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
211eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
212eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If there are no calls then you can't unwind.  This may mean we can omit the
213eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH Frame, but some environments do not handle weak absolute symbols. If
214eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // UnwindTablesMandatory is set we cannot do this optimization; the unwind
215eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // info is to be available for non-EH uses.
2163e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
2173e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner      (!TheFunc->isWeakForLinker() ||
21833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner       !MAI->getWeakDefDirective() ||
21933adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner       MAI->getSupportsWeakOmittedEHFrame())) {
220eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << " = 0\n";
221eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // This name has no connection to the function, so it might get
222eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripped when the function is not, erroneously.  Prohibit
223eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripping unconditionally.
22433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (const char *UsedDirective = MAI->getUsedDirective())
225eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << UsedDirective << EHFrameInfo.FnName << "\n\n";
226eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  } else {
227eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << ":\n";
228eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
229eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // EH frame header.
230eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_frame_end", EHFrameInfo.Number,
231eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   "eh_frame_begin", EHFrameInfo.Number, true);
232eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Length of Frame Information Entry");
233eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
234eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_begin", EHFrameInfo.Number);
235eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
236a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner    EmitSectionOffset("eh_frame_begin", "eh_frame_common",
237a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
238a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      true, true, false);
239eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
240eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE CIE offset");
241eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
242c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands    EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
243eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE initial location");
244eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_func_end", EHFrameInfo.Number,
245c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands                   "eh_func_begin", EHFrameInfo.Number, true);
246eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE address range");
247eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
248eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If there is a personality and landing pads then point to the language
249eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // specific data area in the exception table.
250d44fff7849034fc17d6a0e9f9926542865bb7881Eric Christopher    if (MMI->getPersonalities()[0] != NULL) {
251c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      bool is4Byte = TD->getPointerSize() == sizeof(int32_t);
252c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands
2536fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher      Asm->EmitULEB128Bytes(is4Byte ? 4 : 8);
254eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
255eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
256c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      if (EHFrameInfo.hasLandingPads)
2576fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher        EmitReference("exception", EHFrameInfo.Number, true, false);
258c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      else {
25952783c63a13d384582997684384553e6545852d3Bill Wendling        if (is4Byte)
26052783c63a13d384582997684384553e6545852d3Bill Wendling          Asm->EmitInt32((int)0);
26152783c63a13d384582997684384553e6545852d3Bill Wendling        else
26252783c63a13d384582997684384553e6545852d3Bill Wendling          Asm->EmitInt64((int)0);
2636fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher      }
264eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Language Specific Data Area");
265eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
266eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitULEB128Bytes(0);
267eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
268eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
269eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
270eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Indicate locations of function specific callee saved registers in frame.
271dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher    EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
272eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   true);
273eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
274eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // On Darwin the linker honors the alignment of eh_frame, which means it
275eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // must be 8-byte on 64-bit targets to match what gcc does.  Otherwise you
276eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // get holes which confuse readers of eh_frame.
277eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
278eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       0, 0, false);
279eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_end", EHFrameInfo.Number);
280eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
281eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If the function is marked used, this table should be also.  We cannot
282eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // make the mark unconditional in this case, since retaining the table also
283eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // retains the function in this case, and there is code around that depends
284eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // on unused functions (calling undefined externals) being dead-stripped to
285eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // link correctly.  Yes, there really is.
286401e10c4fbfcdcfade5065093e2ca97f69a1d144Chris Lattner    if (MMI->isUsedFunction(EHFrameInfo.function))
28733adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner      if (const char *UsedDirective = MAI->getUsedDirective())
288eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        O << UsedDirective << EHFrameInfo.FnName << "\n\n";
289eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
2904bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling
2914bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling  Asm->EOL();
292eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
293eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
294eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// SharedTypeIds - How many leading type ids two landing pads have in common.
295eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingunsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
296eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                       const LandingPadInfo *R) {
297eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
298eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
299eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
300eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned Count = 0;
301eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
302eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (; Count != MinSize; ++Count)
303eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[Count] != RIds[Count])
304eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return Count;
305eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
306eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return Count;
307eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
308eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
309eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// PadLT - Order landing pads lexicographically by type id.
310eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingbool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
311eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
312eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
313eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
314eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
315eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != MinSize; ++i)
316eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[i] != RIds[i])
317eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return LIds[i] < RIds[i];
318eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
319eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return LSize < RSize;
320eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
321eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
322d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// ComputeActionsTable - Compute the actions table and gather the first action
323d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// index for each landing pad site.
324ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingunsigned DwarfException::
325ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
326ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<ActionEntry> &Actions,
327ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<unsigned> &FirstActions) {
328a583c55864e83e470333b7be878280b10e175a6eBill Wendling
329a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // The action table follows the call-site table in the LSDA. The individual
330a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // records are of two types:
331a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
332a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Catch clause
333a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Exception specification
334a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
335a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // The two record kinds have the same format, with only small differences.
336a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // They are distinguished by the "switch value" field: Catch clauses
337a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // (TypeInfos) have strictly positive switch values, and exception
338a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // specifications (FilterIds) have strictly negative switch values. Value 0
339a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // indicates a catch-all clause.
340a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
3415e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // Negative type IDs index into FilterIds. Positive type IDs index into
3425e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // TypeInfos.  The value written for a positive type ID is just the type ID
3435e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // itself.  For a negative type ID, however, the value written is the
344eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // (negative) byte offset of the corresponding FilterIds entry.  The byte
3455e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // offset is usually equal to the type ID (because the FilterIds entries are
3465e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // written using a variable width encoding, which outputs one byte per entry
3475e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // as long as the value written is not too large) but can differ.  This kind
3485e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // of complication does not occur for positive type IDs because type infos are
349eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // output using a fixed width encoding.  FilterOffsets[i] holds the byte
350eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // offset corresponding to FilterIds[i].
351409914b773ffd53d53d214394d5636a76f673186Bill Wendling
352409914b773ffd53d53d214394d5636a76f673186Bill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
353eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  SmallVector<int, 16> FilterOffsets;
354eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FilterOffsets.reserve(FilterIds.size());
355eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int Offset = -1;
356409914b773ffd53d53d214394d5636a76f673186Bill Wendling
357409914b773ffd53d53d214394d5636a76f673186Bill Wendling  for (std::vector<unsigned>::const_iterator
358409914b773ffd53d53d214394d5636a76f673186Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
359eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FilterOffsets.push_back(Offset);
360af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner    Offset -= MCAsmInfo::getULEB128Size(*I);
361eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
362eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
363eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FirstActions.reserve(LandingPads.size());
364eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
365eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int FirstAction = 0;
366eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeActions = 0;
3675e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  const LandingPadInfo *PrevLPI = 0;
368409914b773ffd53d53d214394d5636a76f673186Bill Wendling
3695cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
3705e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling         I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
3715e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const LandingPadInfo *LPI = *I;
3725e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const std::vector<int> &TypeIds = LPI->TypeIds;
3735e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
374eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    unsigned SizeSiteActions = 0;
375eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
376eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (NumShared < TypeIds.size()) {
377eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned SizeAction = 0;
378eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      ActionEntry *PrevAction = 0;
379eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
380eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (NumShared) {
3815e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        const unsigned SizePrevIds = PrevLPI->TypeIds.size();
382eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        assert(Actions.size());
383eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
384af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
385af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner          MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
386eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
387eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        for (unsigned j = NumShared; j != SizePrevIds; ++j) {
388eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction -=
389af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner            MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
390eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction += -PrevAction->NextAction;
391eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          PrevAction = PrevAction->Previous;
392eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
393eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
394eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
395eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Compute the actions.
3965e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling      for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
3975e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        int TypeID = TypeIds[J];
3985e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
399eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
400af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
401eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
402eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
403af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
404eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeSiteActions += SizeAction;
405eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
406a583c55864e83e470333b7be878280b10e175a6eBill Wendling        ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
407eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        Actions.push_back(Action);
408eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
409eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
410eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
411eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Record the first action of the landing pad site.
412eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
413eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } // else identical - re-use previous FirstAction
414eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
415a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Information used when created the call-site table. The action record
416a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // field of the call site record is the offset of the first associated
417a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // action record, relative to the start of the actions table. This value is
418a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // biased by 1 (1 in dicating the start of the actions table), and 0
419a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // indicates that there are no actions.
420eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FirstActions.push_back(FirstAction);
421eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
422eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Compute this sites contribution to size.
423eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeActions += SizeSiteActions;
4245e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
4255e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    PrevLPI = LPI;
426eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
427eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
4285e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  return SizeActions;
4295e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling}
4305e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
431ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// ComputeCallSiteTable - Compute the call-site table.  The entry for an invoke
432a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// has a try-range containing the call, a non-zero landing pad, and an
433ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// appropriate action.  The entry for an ordinary call has a try-range
434ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// containing the call and zero for the landing pad and the action.  Calls
435ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// marked 'nounwind' have no entry and must not be contained in the try-range
436ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// of any entry - they form gaps in the table.  Entries must be ordered by
437ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// try-range address.
438ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::
439ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
440ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const RangeMapType &PadMap,
441ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
442ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const SmallVectorImpl<unsigned> &FirstActions) {
443eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The end label of the previous invoke or nounwind try-range.
444eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LastLabel = 0;
445eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
446eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Whether there is a potentially throwing instruction (currently this means
447eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // an ordinary call) between the end of the previous try-range and now.
448eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  bool SawPotentiallyThrowing = false;
449eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
4505cff487665097d971067cbede1598e249f673182Bill Wendling  // Whether the last CallSite entry was for an invoke.
451eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  bool PreviousIsInvoke = false;
452eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
453eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Visit all instructions in order of address.
454eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
455eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       I != E; ++I) {
456eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
457eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         MI != E; ++MI) {
458eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (!MI->isLabel()) {
459eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SawPotentiallyThrowing |= MI->getDesc().isCall();
460eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        continue;
461eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
462eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
463eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned BeginLabel = MI->getOperand(0).getImm();
464eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel && "Invalid label!");
465eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
466eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // End of the previous try-range?
467eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (BeginLabel == LastLabel)
468eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SawPotentiallyThrowing = false;
469eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
470eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Beginning of a new try-range?
471eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      RangeMapType::iterator L = PadMap.find(BeginLabel);
472eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (L == PadMap.end())
473eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Nope, it was just some random label.
474eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        continue;
475eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
476a583c55864e83e470333b7be878280b10e175a6eBill Wendling      const PadRange &P = L->second;
477eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
478eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
479eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling             "Inconsistent landing pad map!");
480eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
481a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // For Dwarf exception handling (SjLj handling doesn't use this). If some
482a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // instruction between the previous try-range and this one may throw,
483a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // create a call-site entry with no landing pad for the region between the
484a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // try-ranges.
4851b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (SawPotentiallyThrowing &&
48633adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner          MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
487a583c55864e83e470333b7be878280b10e175a6eBill Wendling        CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
488eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSites.push_back(Site);
489eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
490eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
491eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
492eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      LastLabel = LandingPad->EndLabels[P.RangeIndex];
493eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel && LastLabel && "Invalid landing pad!");
494eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
495eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (LandingPad->LandingPadLabel) {
496eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // This try-range is for an invoke.
497a583c55864e83e470333b7be878280b10e175a6eBill Wendling        CallSiteEntry Site = {
498a583c55864e83e470333b7be878280b10e175a6eBill Wendling          BeginLabel,
499a583c55864e83e470333b7be878280b10e175a6eBill Wendling          LastLabel,
500a583c55864e83e470333b7be878280b10e175a6eBill Wendling          LandingPad->LandingPadLabel,
501a583c55864e83e470333b7be878280b10e175a6eBill Wendling          FirstActions[P.PadIndex]
502a583c55864e83e470333b7be878280b10e175a6eBill Wendling        };
503eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
50433668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach        // Try to merge with the previous call-site. SJLJ doesn't do this
50533668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach        if (PreviousIsInvoke &&
50633668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach          MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
507eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          CallSiteEntry &Prev = CallSites.back();
508eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
509eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            // Extend the range of the previous entry.
510eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            Prev.EndLabel = Site.EndLabel;
511eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            continue;
512eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          }
513eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
514eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
515eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Otherwise, create a new call-site.
516eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSites.push_back(Site);
517eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = true;
518eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      } else {
519eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Create a gap.
520eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
521eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
522eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
523eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
524eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
525eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If some instruction between the previous try-range and the end of the
526eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // function may throw, create a call-site entry with no landing pad for the
527eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // region following the try-range.
5281b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (SawPotentiallyThrowing &&
52933adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner      MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
530a583c55864e83e470333b7be878280b10e175a6eBill Wendling    CallSiteEntry Site = { LastLabel, 0, 0, 0 };
531eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    CallSites.push_back(Site);
532eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
533ade025c65c12503aba161a4fa399fd97414abaffBill Wendling}
534ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
5350dafca90761097230f02e655fdd541f59b888315Bill Wendling/// EmitExceptionTable - Emit landing pads and actions.
5360dafca90761097230f02e655fdd541f59b888315Bill Wendling///
5370dafca90761097230f02e655fdd541f59b888315Bill Wendling/// The general organization of the table is complex, but the basic concepts are
5380dafca90761097230f02e655fdd541f59b888315Bill Wendling/// easy.  First there is a header which describes the location and organization
5390dafca90761097230f02e655fdd541f59b888315Bill Wendling/// of the three components that follow.
540dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher///
5410dafca90761097230f02e655fdd541f59b888315Bill Wendling///  1. The landing pad site information describes the range of code covered by
5420dafca90761097230f02e655fdd541f59b888315Bill Wendling///     the try.  In our case it's an accumulation of the ranges covered by the
5430dafca90761097230f02e655fdd541f59b888315Bill Wendling///     invokes in the try.  There is also a reference to the landing pad that
5440dafca90761097230f02e655fdd541f59b888315Bill Wendling///     handles the exception once processed.  Finally an index into the actions
5450dafca90761097230f02e655fdd541f59b888315Bill Wendling///     table.
546a583c55864e83e470333b7be878280b10e175a6eBill Wendling///  2. The action table, in our case, is composed of pairs of type IDs and next
5470dafca90761097230f02e655fdd541f59b888315Bill Wendling///     action offset.  Starting with the action index from the landing pad
548a583c55864e83e470333b7be878280b10e175a6eBill Wendling///     site, each type ID is checked for a match to the current exception.  If
5490dafca90761097230f02e655fdd541f59b888315Bill Wendling///     it matches then the exception and type id are passed on to the landing
5500dafca90761097230f02e655fdd541f59b888315Bill Wendling///     pad.  Otherwise the next action is looked up.  This chain is terminated
5510dafca90761097230f02e655fdd541f59b888315Bill Wendling///     with a next action of zero.  If no type id is found the the frame is
5520dafca90761097230f02e655fdd541f59b888315Bill Wendling///     unwound and handling continues.
553a583c55864e83e470333b7be878280b10e175a6eBill Wendling///  3. Type ID table contains references to all the C++ typeinfo for all
5540dafca90761097230f02e655fdd541f59b888315Bill Wendling///     catches in the function.  This tables is reversed indexed base 1.
555ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::EmitExceptionTable() {
556ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
557ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
558ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
559ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  if (PadInfos.empty()) return;
560ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
561ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Sort the landing pads in order of their type ids.  This is used to fold
562ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // duplicate actions.
563ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<const LandingPadInfo *, 64> LandingPads;
564ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  LandingPads.reserve(PadInfos.size());
565ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
566ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
567ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    LandingPads.push_back(&PadInfos[i]);
568ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
569ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
570ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
571ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the actions table and gather the first action index for each
572ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // landing pad site.
573ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<ActionEntry, 32> Actions;
574ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<unsigned, 64> FirstActions;
575ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
576ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
577ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Invokes and nounwind calls have entries in PadMap (due to being bracketed
578ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // by try-range labels when lowered).  Ordinary calls do not, so appropriate
5791b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // try-ranges for them need be deduced when using Dwarf exception handling.
580ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  RangeMapType PadMap;
581ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
582ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    const LandingPadInfo *LandingPad = LandingPads[i];
583ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
584ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      unsigned BeginLabel = LandingPad->BeginLabels[j];
585ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
586ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadRange P = { i, j };
587ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadMap[BeginLabel] = P;
588ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    }
589ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  }
590ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
591ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the call-site table.
592ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<CallSiteEntry, 64> CallSites;
5938b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach  ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
594eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
595eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Final tallies.
596eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
597eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Call sites.
59840121bccbee44d387ab5d6a5194c5fc27c542e12Bill Wendling  const unsigned SiteStartSize  = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
59940121bccbee44d387ab5d6a5194c5fc27c542e12Bill Wendling  const unsigned SiteLengthSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
60040121bccbee44d387ab5d6a5194c5fc27c542e12Bill Wendling  const unsigned LandingPadSize = SizeOfEncodedValue(dwarf::DW_EH_PE_udata4);
6011b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  unsigned SizeSites;
602bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
603d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling  bool IsSJLJ = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
604bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
605d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling  bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
606d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling
607d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling  if (IsSJLJ)
6088b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    SizeSites = 0;
609d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling  else
6101b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    SizeSites = CallSites.size() *
6111b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      (SiteStartSize + SiteLengthSize + LandingPadSize);
612d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling
6131b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
614af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner    SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
615d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling    if (IsSJLJ)
616af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner      SizeSites += MCAsmInfo::getULEB128Size(i);
6171b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
618d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling
619eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Type infos.
620d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
621fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  unsigned TTypeFormat;
622eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
62343e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling  if (!HaveTTData) {
624fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    TTypeFormat = dwarf::DW_EH_PE_omit;
62581c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  } else {
626ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // Okay, we have actual filters or typeinfos to emit.  As such, we need to
627ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // pick a type encoding for them.  We're about to emit a list of pointers to
628ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // typeinfo objects at the end of the LSDA.  However, unless we're in static
629ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // mode, this reference will require a relocation by the dynamic linker.
63046b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //
631ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // Because of this, we have a couple of options:
632ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   1) If we are in -static mode, we can always use an absolute reference
633ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      from the LSDA, because the static linker will resolve it.
634ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   2) Otherwise, if the LSDA section is writable, we can output the direct
635ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      reference to the typeinfo and allow the dynamic linker to relocate
636ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      it.  Since it is in a writable section, the dynamic linker won't
637ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      have a problem.
638ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   3) Finally, if we're in PIC mode and the LDSA section isn't writable,
639ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      we need to use some form of indirection.  For example, on Darwin,
640ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      we can output a statically-relocatable reference to a dyld stub. The
641ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      offset to the stub is constant, but the contents are in a section
642ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      that is updated by the dynamic linker.  This is easy enough, but we
643ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      need to tell the personality function of the unwinder to indirect
644ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      through the dyld stub.
645ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //
64643e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling    // FIXME: When (3) is actually implemented, we'll have to emit the stubs
647ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // somewhere.  This predicate should be moved to a shared location that is
648ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // in target-independent code.
649ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //
65043e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling    if (LSDASection->getKind().isWriteable() ||
65143e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling        Asm->TM.getRelocationModel() == Reloc::Static)
65243e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling      TTypeFormat = dwarf::DW_EH_PE_absptr;
65343e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling    else
65443e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling      TTypeFormat = dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |
65543e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling        dwarf::DW_EH_PE_sdata4;
656fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  }
657fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling
658fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  // Begin the exception table.
659fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  Asm->OutStreamer.SwitchSection(LSDASection);
660fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
66143e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling
662fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  O << "GCC_except_table" << SubprogramCount << ":\n";
663fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  EmitLabel("exception", SubprogramCount);
664fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling
665fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  if (IsSJLJ) {
666fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    SmallString<16> LSDAName;
667fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
668fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling      "_LSDA_" << Asm->getFunctionNumber();
669fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    O << LSDAName.str() << ":\n";
670fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  }
671fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling
672fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  // Emit the header.
673fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  Asm->EmitInt8(dwarf::DW_EH_PE_omit);
674fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  Asm->EOL("@LPStart format", dwarf::DW_EH_PE_omit);
675fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling
676fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  // For SjLj exceptions, if there is no TypeInfo, then we just explicitly
677fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  // say that we're omitting that bit.
678fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  Asm->EmitInt8(TTypeFormat);
679fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  Asm->EOL("@TType format", TTypeFormat);
680fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling
681fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling  if (HaveTTData) {
682fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    unsigned TypeFormatSize = SizeOfEncodedValue(TTypeFormat);
683fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    unsigned SizeTypes = TypeInfos.size() * TypeFormatSize;
684fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    unsigned BeforeOffset = 2;
685fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    unsigned TypeOffset = sizeof(int8_t) +   // Call site format
686fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling      MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
687fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling      SizeSites + SizeActions + SizeTypes;
688fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    unsigned Offset = TypeOffset;
689fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    unsigned LastOffset = 0;
690fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling
691fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    while (Offset != LastOffset) {
692fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling      LastOffset = Offset;
693fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling      unsigned Size = MCAsmInfo::getULEB128Size(Offset);
694fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling      unsigned Pad = BeforeOffset + Size + TypeOffset;
695fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling
696fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling      if (Pad % TypeFormatSize)
697fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling        Pad = TypeFormatSize - (Pad % TypeFormatSize);
698fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling      else
699fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling        Pad = 0;
700fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling
701fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling      Offset = TypeOffset + Pad;
702fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    }
70343e484fbcf6a40a70d9a5c7ea142f37416ef1e68Bill Wendling
704fe220285b5ef1e4480ddd7f7c8bb182b88a33b16Bill Wendling    Asm->EmitULEB128Bytes(Offset);
705a583c55864e83e470333b7be878280b10e175a6eBill Wendling    Asm->EOL("@TType base offset");
7061b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
707b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
7081b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // SjLj Exception handilng
709d1a5b37a62e829e56b6225e0934a01d9f8823387Bill Wendling  if (IsSJLJ) {
710639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
7110734d35044c304dd072f20e49840bcbea427db9cBill Wendling    Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
7121b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(SizeSites);
713a583c55864e83e470333b7be878280b10e175a6eBill Wendling    Asm->EOL("Call site table length");
7141b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
7151b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // Emit the landing pad site information.
7168b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    unsigned idx = 0;
7178b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    for (SmallVectorImpl<CallSiteEntry>::const_iterator
7188b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach         I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
7198b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach      const CallSiteEntry &S = *I;
720a583c55864e83e470333b7be878280b10e175a6eBill Wendling
721a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the landing pad, counted in 16-byte bundles relative to the
722a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // @LPStart address.
7238b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach      Asm->EmitULEB128Bytes(idx);
7241b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Landing pad");
725a583c55864e83e470333b7be878280b10e175a6eBill Wendling
726a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the first associated action record, relative to the start of
727a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table. This value is biased by 1 (1 indicates the start of
728a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table), and 0 indicates that there are no actions.
7291b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(S.Action);
7301b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Action");
7311b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    }
7321b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else {
7331b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // DWARF Exception handling
73433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
7351b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
736a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // The call-site table is a list of all call sites that may throw an
737a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // exception (including C++ 'throw' statements) in the procedure
738a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // fragment. It immediately follows the LSDA header. Each entry indicates,
739a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // for a given call, the first corresponding action record and corresponding
740a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // landing pad.
741a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
742a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // The table begins with the number of bytes, stored as an LEB128
743a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // compressed, unsigned integer. The records immediately follow the record
744a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // count. They are sorted in increasing call-site address. Each record
745a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // indicates:
746a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
747a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * The position of the call-site.
748a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * The position of the landing pad.
749a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * The first action record for that call site.
750a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
751a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // A missing entry in the call-site table indicates that a call is not
752a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // supposed to throw. Such calls include:
753a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
754a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * Calls to destructors within cleanup code. C++ semantics forbids these
755a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //     calls to throw.
756a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * Calls to intrinsic routines in the standard library which are known
757a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //     not to throw (sin, memcpy, et al).
758a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
759a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // If the runtime does not find the call-site entry for a given call, it
760a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // will call `terminate()'.
761a583c55864e83e470333b7be878280b10e175a6eBill Wendling
762a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Emit the landing pad call site table.
763639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
7640734d35044c304dd072f20e49840bcbea427db9cBill Wendling    Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
7651b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(SizeSites);
766a583c55864e83e470333b7be878280b10e175a6eBill Wendling    Asm->EOL("Call site table size");
767eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7681b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    for (SmallVectorImpl<CallSiteEntry>::const_iterator
7695cff487665097d971067cbede1598e249f673182Bill Wendling         I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
7701b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const CallSiteEntry &S = *I;
7711b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const char *BeginTag;
7721b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      unsigned BeginNumber;
773eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7741b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.BeginLabel) {
7751b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginTag = "eh_func_begin";
7761b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginNumber = SubprogramCount;
7771b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      } else {
7781b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginTag = "label";
7791b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginNumber = S.BeginLabel;
7801b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      }
781eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
782a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the call site relative to the previous call site, counted in
783a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // number of 16-byte bundles. The first call site is counted relative to
784a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the start of the procedure fragment.
7851b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
7861b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                        true, true);
7871b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Region start");
788eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7891b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.EndLabel)
7901b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
7911b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                       true);
7921b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      else
7931b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
794eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7951b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Region length");
796eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
797a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the landing pad, counted in 16-byte bundles relative to the
798a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // @LPStart address.
7991b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.PadLabel)
8001b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        Asm->EmitInt32(0);
8011b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      else
8021b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
8031b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                          true, true);
804eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
8051b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Landing pad");
806eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
807a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the first associated action record, relative to the start of
808a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table. This value is biased by 1 (1 indicates the start of
809a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table), and 0 indicates that there are no actions.
8101b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(S.Action);
8111b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Action");
8121b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    }
813eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
814eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
815a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // Emit the Action Table.
8165cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<ActionEntry>::const_iterator
8175cff487665097d971067cbede1598e249f673182Bill Wendling         I = Actions.begin(), E = Actions.end(); I != E; ++I) {
8185cff487665097d971067cbede1598e249f673182Bill Wendling    const ActionEntry &Action = *I;
819a583c55864e83e470333b7be878280b10e175a6eBill Wendling
820a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Type Filter
821a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
822a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   Used by the runtime to match the type of the thrown exception to the
823a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   type of the catch clauses or the types in the exception specification.
824a583c55864e83e470333b7be878280b10e175a6eBill Wendling
825eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
826eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo index");
827a583c55864e83e470333b7be878280b10e175a6eBill Wendling
828a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Action Record
829a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
830a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   Self-relative signed displacement in bytes of the next action record,
831a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   or 0 if there is no next action record.
832a583c55864e83e470333b7be878280b10e175a6eBill Wendling
833eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.NextAction);
834eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Next action");
835eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
836eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
837a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // Emit the Catch Clauses. The code for the catch clauses following the same
838a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // try is similar to a switch statement. The catch clause action record
839a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // informs the runtime about the type of a catch clause and about the
840a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // associated switch value.
841a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
842a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //  Action Record Fields:
843dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  //
844a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Filter Value
845a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     Positive value, starting at 1. Index in the types table of the
846a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     __typeinfo for the catch-clause type. 1 is the first word preceding
847a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     TTBase, 2 is the second word, and so on. Used by the runtime to check
848a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     if the thrown exception type matches the catch-clause type. Back-end
849a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     generated switch statements check against this value.
850a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
851a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Next
852a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     Signed offset, in bytes from the start of this field, to the next
853a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     chained action record, or zero if none.
854a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
855a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // The order of the action records determined by the next field is the order
856a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // of the catch clauses as they appear in the source code, and must be kept in
857a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // the same order. As a result, changing the order of the catch clause would
858a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // change the semantics of the program.
8595cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<GlobalVariable *>::const_reverse_iterator
8605cff487665097d971067cbede1598e249f673182Bill Wendling         I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
861a583c55864e83e470333b7be878280b10e175a6eBill Wendling    const GlobalVariable *GV = *I;
862eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective();
863eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
864eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (GV) {
865eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      std::string GLN;
866eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << Asm->getGlobalLinkName(GV, GLN);
867eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
868049e98d641f90e90e8312d76cbf4c68908fb9d1dBill Wendling      O << "0x0";
869eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
870eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
871eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo");
872eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
873eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
874a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // Emit the Type Table.
8755cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<unsigned>::const_iterator
8765cff487665097d971067cbede1598e249f673182Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
8775cff487665097d971067cbede1598e249f673182Bill Wendling    unsigned TypeID = *I;
878eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(TypeID);
879eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Filter TypeInfo index");
880eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
881eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
882eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
883eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
884eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
885eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndModule - Emit all exception information that should come after the
886eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// content.
887eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndModule() {
88833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
8891b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    return;
890b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling
89152783c63a13d384582997684384553e6545852d3Bill Wendling  if (!shouldEmitMovesModule && !shouldEmitTableModule)
89252783c63a13d384582997684384553e6545852d3Bill Wendling    return;
89352783c63a13d384582997684384553e6545852d3Bill Wendling
894eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
895eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
896eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
89752783c63a13d384582997684384553e6545852d3Bill Wendling  const std::vector<Function *> Personalities = MMI->getPersonalities();
898b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling
89952783c63a13d384582997684384553e6545852d3Bill Wendling  for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
90052783c63a13d384582997684384553e6545852d3Bill Wendling    EmitCIE(Personalities[i], i);
901eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
90252783c63a13d384582997684384553e6545852d3Bill Wendling  for (std::vector<FunctionEHFrameInfo>::iterator
90352783c63a13d384582997684384553e6545852d3Bill Wendling         I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
90452783c63a13d384582997684384553e6545852d3Bill Wendling    EmitFDE(*I);
905eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
906eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
907eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
908eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
909eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
910eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// BeginFunction - Gather pre-function exception information.  Assumes being
911eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// emitted immediately after the function entry point.
912eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::BeginFunction(MachineFunction *MF) {
913eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
914eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
915eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
916eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  this->MF = MF;
917eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTable = shouldEmitMoves = false;
918eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
91933adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MMI && MAI->doesSupportExceptionHandling()) {
920eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Map all labels and get rid of any dead landing pads.
921eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    MMI->TidyLandingPads();
922eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
923eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If any landing pads survive, we need an EH table.
924b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling    if (!MMI->getLandingPads().empty())
925eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitTable = true;
926eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
927eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // See if we need frame move info.
928eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
929eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitMoves = true;
930eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
931eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (shouldEmitMoves || shouldEmitTable)
932eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Assumes in correct section after the entry point.
933eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitLabel("eh_func_begin", ++SubprogramCount);
934eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
935eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
936eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTableModule |= shouldEmitTable;
937eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitMovesModule |= shouldEmitMoves;
938eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
939eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
940eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
941eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
942eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
943eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndFunction - Gather and emit post-function exception information.
944eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling///
945eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndFunction() {
9467b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling  if (!shouldEmitMoves && !shouldEmitTable) return;
9477b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling
948dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  if (TimePassesIsEnabled)
949eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
950eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
9517b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling  EmitLabel("eh_func_end", SubprogramCount);
9527b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling  EmitExceptionTable();
9537b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling
9547b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling  // Save EH frame information
9557b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling  EHFrames.push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
9567b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         SubprogramCount,
9577b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         MMI->getPersonalityIndex(),
9587b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         MF->getFrameInfo()->hasCalls(),
9597b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         !MMI->getLandingPads().empty(),
9607b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         MMI->getFrameMoves(),
9617b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         MF->getFunction()));
962eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
96352783c63a13d384582997684384553e6545852d3Bill Wendling  // Record if this personality index uses a landing pad.
96452783c63a13d384582997684384553e6545852d3Bill Wendling  UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty();
96552783c63a13d384582997684384553e6545852d3Bill Wendling
966dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  if (TimePassesIsEnabled)
967eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
968eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
969