DwarfException.cpp revision bb3e29932d14cc012149a1ab452b88e6f6f5bb49
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"
283fb2b1ede30193b59a651328a946174196b20610Jim Grosbach#include "llvm/Support/Mangler.h"
296c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/Timer.h"
306c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner#include "llvm/Support/raw_ostream.h"
31c40d9f9bae70c83947bf8fa5f9ee97adbf1bb0c0Jim Grosbach#include "llvm/ADT/SmallString.h"
32eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/ADT/StringExtras.h"
33eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingusing namespace llvm;
34eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
35eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingstatic TimerGroup &getDwarfTimerGroup() {
36eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  static TimerGroup DwarfTimerGroup("Dwarf Exception");
37eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return DwarfTimerGroup;
38eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
39eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
40bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
41af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner                               const MCAsmInfo *T)
42bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
43bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    shouldEmitTableModule(false), shouldEmitMovesModule(false),
44bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer(0) {
45dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  if (TimePassesIsEnabled)
46bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer = new Timer("Dwarf Exception Writer",
47bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling                               getDwarfTimerGroup());
48bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
49bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
50bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::~DwarfException() {
51bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  delete ExceptionTimer;
52bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
53bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
54bb3e29932d14cc012149a1ab452b88e6f6f5bb49Bill Wendling/// SizeOfEncodedValue - Return the size of the encoding in bytes.
5552783c63a13d384582997684384553e6545852d3Bill Wendlingunsigned DwarfException::SizeOfEncodedValue(unsigned Encoding) {
5652783c63a13d384582997684384553e6545852d3Bill Wendling  if (Encoding == dwarf::DW_EH_PE_omit)
5752783c63a13d384582997684384553e6545852d3Bill Wendling    return 0;
5852783c63a13d384582997684384553e6545852d3Bill Wendling
5952783c63a13d384582997684384553e6545852d3Bill Wendling  switch (Encoding & 0x07) {
6052783c63a13d384582997684384553e6545852d3Bill Wendling  case dwarf::DW_EH_PE_absptr:
6152783c63a13d384582997684384553e6545852d3Bill Wendling    return TD->getPointerSize();
6252783c63a13d384582997684384553e6545852d3Bill Wendling  case dwarf::DW_EH_PE_udata2:
6352783c63a13d384582997684384553e6545852d3Bill Wendling    return 2;
6452783c63a13d384582997684384553e6545852d3Bill Wendling  case dwarf::DW_EH_PE_udata4:
6552783c63a13d384582997684384553e6545852d3Bill Wendling    return 4;
6652783c63a13d384582997684384553e6545852d3Bill Wendling  case dwarf::DW_EH_PE_udata8:
6752783c63a13d384582997684384553e6545852d3Bill Wendling    return 8;
6852783c63a13d384582997684384553e6545852d3Bill Wendling  }
6952783c63a13d384582997684384553e6545852d3Bill Wendling
7052783c63a13d384582997684384553e6545852d3Bill Wendling  llvm_unreachable("Invalid encoded value.");
7152783c63a13d384582997684384553e6545852d3Bill Wendling  return 0;
7252783c63a13d384582997684384553e6545852d3Bill Wendling}
7352783c63a13d384582997684384553e6545852d3Bill Wendling
747ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// EmitCIE - Emit a Common Information Entry (CIE). This holds information that
757ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// is shared among many Frame Description Entries.  There is at least one CIE
767ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// in every non-empty .debug_frame section.
777ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendlingvoid DwarfException::EmitCIE(const Function *Personality, unsigned Index) {
78eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Size and sign of stack growth.
79eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int stackGrowth =
80eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
81eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TargetFrameInfo::StackGrowsUp ?
82eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TD->getPointerSize() : -TD->getPointerSize();
83eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
84eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin eh frame section.
856c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
86eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
8733adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->is_EHSymbolPrivate())
8833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getPrivateGlobalPrefix();
89eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
90eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "EH_frame" << Index << ":\n";
91eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("section_eh_frame", Index);
92eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
93eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define base labels.
94eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common", Index);
95eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
96eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define the eh frame length.
97eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitDifference("eh_frame_common_end", Index,
98eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                 "eh_frame_common_begin", Index, true);
99eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("Length of Common Information Entry");
100eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
101eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH frame header.
102eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_begin", Index);
103eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt32((int)0);
104eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Identifier Tag");
105eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(dwarf::DW_CIE_VERSION);
106eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Version");
107eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
108eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The personality presence indicates that language specific information will
109eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // show up in the eh frame.
11052783c63a13d384582997684384553e6545852d3Bill Wendling
11152783c63a13d384582997684384553e6545852d3Bill Wendling  // FIXME: Don't hardcode these encodings.
11252783c63a13d384582997684384553e6545852d3Bill Wendling  unsigned PerEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
11352783c63a13d384582997684384553e6545852d3Bill Wendling  if (Personality && MAI->getNeedsIndirectEncoding())
11452783c63a13d384582997684384553e6545852d3Bill Wendling    PerEncoding |= dwarf::DW_EH_PE_indirect;
11552783c63a13d384582997684384553e6545852d3Bill Wendling  unsigned LSDAEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
11652783c63a13d384582997684384553e6545852d3Bill Wendling  unsigned FDEEncoding = dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4;
11752783c63a13d384582997684384553e6545852d3Bill Wendling
11852783c63a13d384582997684384553e6545852d3Bill Wendling  char Augmentation[5] = { 0 };
11952783c63a13d384582997684384553e6545852d3Bill Wendling  unsigned AugmentationSize = 0;
12052783c63a13d384582997684384553e6545852d3Bill Wendling  char *APtr = Augmentation + 1;
12152783c63a13d384582997684384553e6545852d3Bill Wendling
12252783c63a13d384582997684384553e6545852d3Bill Wendling  if (Personality) {
12352783c63a13d384582997684384553e6545852d3Bill Wendling    // There is a personality function.
12452783c63a13d384582997684384553e6545852d3Bill Wendling    *APtr++ = 'P';
12552783c63a13d384582997684384553e6545852d3Bill Wendling    AugmentationSize += 1 + SizeOfEncodedValue(PerEncoding);
12652783c63a13d384582997684384553e6545852d3Bill Wendling  }
12752783c63a13d384582997684384553e6545852d3Bill Wendling
12852783c63a13d384582997684384553e6545852d3Bill Wendling  if (UsesLSDA[Index]) {
12952783c63a13d384582997684384553e6545852d3Bill Wendling    // An LSDA pointer is in the FDE augmentation.
13052783c63a13d384582997684384553e6545852d3Bill Wendling    *APtr++ = 'L';
13152783c63a13d384582997684384553e6545852d3Bill Wendling    ++AugmentationSize;
13252783c63a13d384582997684384553e6545852d3Bill Wendling  }
13352783c63a13d384582997684384553e6545852d3Bill Wendling
13452783c63a13d384582997684384553e6545852d3Bill Wendling  if (FDEEncoding != dwarf::DW_EH_PE_absptr) {
13552783c63a13d384582997684384553e6545852d3Bill Wendling    // A non-default pointer encoding for the FDE.
13652783c63a13d384582997684384553e6545852d3Bill Wendling    *APtr++ = 'R';
13752783c63a13d384582997684384553e6545852d3Bill Wendling    ++AugmentationSize;
13852783c63a13d384582997684384553e6545852d3Bill Wendling  }
13952783c63a13d384582997684384553e6545852d3Bill Wendling
14052783c63a13d384582997684384553e6545852d3Bill Wendling  if (APtr != Augmentation + 1)
14152783c63a13d384582997684384553e6545852d3Bill Wendling    Augmentation[0] = 'z';
14252783c63a13d384582997684384553e6545852d3Bill Wendling
14352783c63a13d384582997684384553e6545852d3Bill Wendling  Asm->EmitString(Augmentation);
144eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Augmentation");
145eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
146eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Round out reader.
147eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitULEB128Bytes(1);
148eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Code Alignment Factor");
149eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitSLEB128Bytes(stackGrowth);
150eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Data Alignment Factor");
151eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
152eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Return Address Column");
153eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
15452783c63a13d384582997684384553e6545852d3Bill Wendling  Asm->EmitULEB128Bytes(AugmentationSize);
15552783c63a13d384582997684384553e6545852d3Bill Wendling  Asm->EOL("Augmentation Size");
15652783c63a13d384582997684384553e6545852d3Bill Wendling
15752783c63a13d384582997684384553e6545852d3Bill Wendling  Asm->EmitInt8(PerEncoding);
15852783c63a13d384582997684384553e6545852d3Bill Wendling  Asm->EOL("Personality", PerEncoding);
15952783c63a13d384582997684384553e6545852d3Bill Wendling
1604bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling  // If there is a personality, we need to indicate the function's location.
161eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (Personality) {
162eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective(true);
16333adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getPersonalityPrefix();
164eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
16533adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getPersonalitySuffix();
16633adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (strcmp(MAI->getPersonalitySuffix(), "+4@GOTPCREL"))
16733adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner      O << "-" << MAI->getPCSymbol();
168eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Personality");
169eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
17052783c63a13d384582997684384553e6545852d3Bill Wendling    Asm->EmitInt8(LSDAEncoding);
17152783c63a13d384582997684384553e6545852d3Bill Wendling    Asm->EOL("LSDA Encoding", LSDAEncoding);
172eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
17352783c63a13d384582997684384553e6545852d3Bill Wendling    Asm->EmitInt8(FDEEncoding);
17452783c63a13d384582997684384553e6545852d3Bill Wendling    Asm->EOL("FDE Encoding", FDEEncoding);
175eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
176eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
177eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Indicate locations of general callee saved registers in frame.
178eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  std::vector<MachineMove> Moves;
179eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  RI->getInitialFrameState(Moves);
180eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitFrameMoves(NULL, 0, Moves, true);
181eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
182eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // On Darwin the linker honors the alignment of eh_frame, which means it must
183eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // be 8-byte on 64-bit targets to match what gcc does.  Otherwise you get
184eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // holes which confuse readers of eh_frame.
185eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
186eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                     0, 0, false);
187eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_end", Index);
188eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
189eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL();
190eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
191eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
1927ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendling/// EmitFDE - Emit the Frame Description Entry (FDE) for the function.
1937ccda0f2aadb601ce48f05c12cc153f3fcc554e6Bill Wendlingvoid DwarfException::EmitFDE(const FunctionEHFrameInfo &EHFrameInfo) {
194dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
195eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         "Should not emit 'available externally' functions at all");
196eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
1973e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  const Function *TheFunc = EHFrameInfo.function;
198dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher
1996c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering().getEHFrameSection());
200dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher
201eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Externally visible entry into the functions eh frame info. If the
202eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // corresponding function is static, this should not be externally visible.
2033e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!TheFunc->hasLocalLinkage())
20433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (const char *GlobalEHDirective = MAI->getGlobalEHDirective())
205eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
206eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
207eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If corresponding function is weak definition, this should be too.
20833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (TheFunc->isWeakForLinker() && MAI->getWeakDefDirective())
20933adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    O << MAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
210eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
211eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If there are no calls then you can't unwind.  This may mean we can omit the
212eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH Frame, but some environments do not handle weak absolute symbols. If
213eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // UnwindTablesMandatory is set we cannot do this optimization; the unwind
214eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // info is to be available for non-EH uses.
2153e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
2163e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner      (!TheFunc->isWeakForLinker() ||
21733adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner       !MAI->getWeakDefDirective() ||
21833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner       MAI->getSupportsWeakOmittedEHFrame())) {
219eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << " = 0\n";
220eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // This name has no connection to the function, so it might get
221eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripped when the function is not, erroneously.  Prohibit
222eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripping unconditionally.
22333adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (const char *UsedDirective = MAI->getUsedDirective())
224eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << UsedDirective << EHFrameInfo.FnName << "\n\n";
225eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  } else {
226eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << ":\n";
227eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
228eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // EH frame header.
229eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_frame_end", EHFrameInfo.Number,
230eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   "eh_frame_begin", EHFrameInfo.Number, true);
231eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Length of Frame Information Entry");
232eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
233eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_begin", EHFrameInfo.Number);
234eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
235a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner    EmitSectionOffset("eh_frame_begin", "eh_frame_common",
236a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
237a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      true, true, false);
238eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
239eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE CIE offset");
240eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
241c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands    EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
242eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE initial location");
243eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_func_end", EHFrameInfo.Number,
244c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands                   "eh_func_begin", EHFrameInfo.Number, true);
245eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE address range");
246eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
247eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If there is a personality and landing pads then point to the language
248eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // specific data area in the exception table.
249d44fff7849034fc17d6a0e9f9926542865bb7881Eric Christopher    if (MMI->getPersonalities()[0] != NULL) {
250c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      bool is4Byte = TD->getPointerSize() == sizeof(int32_t);
251c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands
2526fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher      Asm->EmitULEB128Bytes(is4Byte ? 4 : 8);
253eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
254eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
255c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      if (EHFrameInfo.hasLandingPads)
2566fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher        EmitReference("exception", EHFrameInfo.Number, true, false);
257c69d74a5d41a6c5e92f9d947f2fa181f48626ca5Duncan Sands      else {
25852783c63a13d384582997684384553e6545852d3Bill Wendling        if (is4Byte)
25952783c63a13d384582997684384553e6545852d3Bill Wendling          Asm->EmitInt32((int)0);
26052783c63a13d384582997684384553e6545852d3Bill Wendling        else
26152783c63a13d384582997684384553e6545852d3Bill Wendling          Asm->EmitInt64((int)0);
2626fefcebc4f92b944664b7b26042bc21e57aefe43Eric Christopher      }
263eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Language Specific Data Area");
264eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
265eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitULEB128Bytes(0);
266eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
267eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
268eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
269eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Indicate locations of function specific callee saved registers in frame.
270dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher    EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
271eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   true);
272eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
273eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // On Darwin the linker honors the alignment of eh_frame, which means it
274eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // must be 8-byte on 64-bit targets to match what gcc does.  Otherwise you
275eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // get holes which confuse readers of eh_frame.
276eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
277eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       0, 0, false);
278eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_end", EHFrameInfo.Number);
279eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
280eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If the function is marked used, this table should be also.  We cannot
281eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // make the mark unconditional in this case, since retaining the table also
282eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // retains the function in this case, and there is code around that depends
283eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // on unused functions (calling undefined externals) being dead-stripped to
284eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // link correctly.  Yes, there really is.
285401e10c4fbfcdcfade5065093e2ca97f69a1d144Chris Lattner    if (MMI->isUsedFunction(EHFrameInfo.function))
28633adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner      if (const char *UsedDirective = MAI->getUsedDirective())
287eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        O << UsedDirective << EHFrameInfo.FnName << "\n\n";
288eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
2894bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling
2904bda11fbdafc1d4fa97b7539bdf5a0f62ecfc280Bill Wendling  Asm->EOL();
291eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
292eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
293eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// SharedTypeIds - How many leading type ids two landing pads have in common.
294eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingunsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
295eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                       const LandingPadInfo *R) {
296eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
297eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
298eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
299eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned Count = 0;
300eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
301eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (; Count != MinSize; ++Count)
302eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[Count] != RIds[Count])
303eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return Count;
304eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
305eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return Count;
306eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
307eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
308eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// PadLT - Order landing pads lexicographically by type id.
309eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingbool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
310eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
311eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
312eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
313eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
314eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != MinSize; ++i)
315eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[i] != RIds[i])
316eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return LIds[i] < RIds[i];
317eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
318eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return LSize < RSize;
319eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
320eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
321d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// ComputeActionsTable - Compute the actions table and gather the first action
322d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// index for each landing pad site.
323ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingunsigned DwarfException::
324ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
325ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<ActionEntry> &Actions,
326ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<unsigned> &FirstActions) {
327a583c55864e83e470333b7be878280b10e175a6eBill Wendling
328a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // The action table follows the call-site table in the LSDA. The individual
329a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // records are of two types:
330a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
331a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Catch clause
332a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Exception specification
333a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
334a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // The two record kinds have the same format, with only small differences.
335a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // They are distinguished by the "switch value" field: Catch clauses
336a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // (TypeInfos) have strictly positive switch values, and exception
337a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // specifications (FilterIds) have strictly negative switch values. Value 0
338a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // indicates a catch-all clause.
339a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
3405e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // Negative type IDs index into FilterIds. Positive type IDs index into
3415e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // TypeInfos.  The value written for a positive type ID is just the type ID
3425e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // itself.  For a negative type ID, however, the value written is the
343eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // (negative) byte offset of the corresponding FilterIds entry.  The byte
3445e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // offset is usually equal to the type ID (because the FilterIds entries are
3455e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // written using a variable width encoding, which outputs one byte per entry
3465e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // as long as the value written is not too large) but can differ.  This kind
3475e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // of complication does not occur for positive type IDs because type infos are
348eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // output using a fixed width encoding.  FilterOffsets[i] holds the byte
349eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // offset corresponding to FilterIds[i].
350409914b773ffd53d53d214394d5636a76f673186Bill Wendling
351409914b773ffd53d53d214394d5636a76f673186Bill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
352eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  SmallVector<int, 16> FilterOffsets;
353eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FilterOffsets.reserve(FilterIds.size());
354eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int Offset = -1;
355409914b773ffd53d53d214394d5636a76f673186Bill Wendling
356409914b773ffd53d53d214394d5636a76f673186Bill Wendling  for (std::vector<unsigned>::const_iterator
357409914b773ffd53d53d214394d5636a76f673186Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
358eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FilterOffsets.push_back(Offset);
359af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner    Offset -= MCAsmInfo::getULEB128Size(*I);
360eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
361eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
362eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FirstActions.reserve(LandingPads.size());
363eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
364eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int FirstAction = 0;
365eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeActions = 0;
3665e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  const LandingPadInfo *PrevLPI = 0;
367409914b773ffd53d53d214394d5636a76f673186Bill Wendling
3685cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
3695e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling         I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
3705e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const LandingPadInfo *LPI = *I;
3715e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const std::vector<int> &TypeIds = LPI->TypeIds;
3725e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
373eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    unsigned SizeSiteActions = 0;
374eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
375eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (NumShared < TypeIds.size()) {
376eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned SizeAction = 0;
377eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      ActionEntry *PrevAction = 0;
378eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
379eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (NumShared) {
3805e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        const unsigned SizePrevIds = PrevLPI->TypeIds.size();
381eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        assert(Actions.size());
382eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
383af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
384af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner          MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
385eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
386eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        for (unsigned j = NumShared; j != SizePrevIds; ++j) {
387eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction -=
388af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner            MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
389eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction += -PrevAction->NextAction;
390eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          PrevAction = PrevAction->Previous;
391eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
392eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
393eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
394eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Compute the actions.
3955e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling      for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
3965e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        int TypeID = TypeIds[J];
3975e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
398eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
399af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
400eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
401eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
402af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
403eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeSiteActions += SizeAction;
404eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
405a583c55864e83e470333b7be878280b10e175a6eBill Wendling        ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
406eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        Actions.push_back(Action);
407eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
408eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
409eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
410eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Record the first action of the landing pad site.
411eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
412eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } // else identical - re-use previous FirstAction
413eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
414a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Information used when created the call-site table. The action record
415a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // field of the call site record is the offset of the first associated
416a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // action record, relative to the start of the actions table. This value is
417a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // biased by 1 (1 in dicating the start of the actions table), and 0
418a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // indicates that there are no actions.
419eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FirstActions.push_back(FirstAction);
420eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
421eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Compute this sites contribution to size.
422eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeActions += SizeSiteActions;
4235e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
4245e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    PrevLPI = LPI;
425eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
426eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
4275e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  return SizeActions;
4285e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling}
4295e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
430ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// ComputeCallSiteTable - Compute the call-site table.  The entry for an invoke
431a583c55864e83e470333b7be878280b10e175a6eBill Wendling/// has a try-range containing the call, a non-zero landing pad, and an
432ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// appropriate action.  The entry for an ordinary call has a try-range
433ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// containing the call and zero for the landing pad and the action.  Calls
434ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// marked 'nounwind' have no entry and must not be contained in the try-range
435ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// of any entry - they form gaps in the table.  Entries must be ordered by
436ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// try-range address.
437ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::
438ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
439ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const RangeMapType &PadMap,
440ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
441ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const SmallVectorImpl<unsigned> &FirstActions) {
442eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The end label of the previous invoke or nounwind try-range.
443eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LastLabel = 0;
444eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
445eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Whether there is a potentially throwing instruction (currently this means
446eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // an ordinary call) between the end of the previous try-range and now.
447eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  bool SawPotentiallyThrowing = false;
448eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
4495cff487665097d971067cbede1598e249f673182Bill Wendling  // Whether the last CallSite entry was for an invoke.
450eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  bool PreviousIsInvoke = false;
451eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
452eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Visit all instructions in order of address.
453eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
454eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       I != E; ++I) {
455eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
456eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         MI != E; ++MI) {
457eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (!MI->isLabel()) {
458eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SawPotentiallyThrowing |= MI->getDesc().isCall();
459eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        continue;
460eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
461eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
462eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned BeginLabel = MI->getOperand(0).getImm();
463eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel && "Invalid label!");
464eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
465eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // End of the previous try-range?
466eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (BeginLabel == LastLabel)
467eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SawPotentiallyThrowing = false;
468eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
469eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Beginning of a new try-range?
470eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      RangeMapType::iterator L = PadMap.find(BeginLabel);
471eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (L == PadMap.end())
472eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Nope, it was just some random label.
473eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        continue;
474eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
475a583c55864e83e470333b7be878280b10e175a6eBill Wendling      const PadRange &P = L->second;
476eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
477eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
478eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling             "Inconsistent landing pad map!");
479eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
480a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // For Dwarf exception handling (SjLj handling doesn't use this). If some
481a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // instruction between the previous try-range and this one may throw,
482a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // create a call-site entry with no landing pad for the region between the
483a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // try-ranges.
4841b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (SawPotentiallyThrowing &&
48533adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner          MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
486a583c55864e83e470333b7be878280b10e175a6eBill Wendling        CallSiteEntry Site = { LastLabel, BeginLabel, 0, 0 };
487eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSites.push_back(Site);
488eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
489eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
490eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
491eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      LastLabel = LandingPad->EndLabels[P.RangeIndex];
492eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel && LastLabel && "Invalid landing pad!");
493eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
494eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (LandingPad->LandingPadLabel) {
495eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // This try-range is for an invoke.
496a583c55864e83e470333b7be878280b10e175a6eBill Wendling        CallSiteEntry Site = {
497a583c55864e83e470333b7be878280b10e175a6eBill Wendling          BeginLabel,
498a583c55864e83e470333b7be878280b10e175a6eBill Wendling          LastLabel,
499a583c55864e83e470333b7be878280b10e175a6eBill Wendling          LandingPad->LandingPadLabel,
500a583c55864e83e470333b7be878280b10e175a6eBill Wendling          FirstActions[P.PadIndex]
501a583c55864e83e470333b7be878280b10e175a6eBill Wendling        };
502eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
50333668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach        // Try to merge with the previous call-site. SJLJ doesn't do this
50433668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach        if (PreviousIsInvoke &&
50533668c0f1a7e374d4c3c53df34efbdf570e0987bJim Grosbach          MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
506eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          CallSiteEntry &Prev = CallSites.back();
507eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
508eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            // Extend the range of the previous entry.
509eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            Prev.EndLabel = Site.EndLabel;
510eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            continue;
511eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          }
512eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
513eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
514eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Otherwise, create a new call-site.
515eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSites.push_back(Site);
516eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = true;
517eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      } else {
518eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Create a gap.
519eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
520eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
521eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
522eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
523eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
524eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If some instruction between the previous try-range and the end of the
525eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // function may throw, create a call-site entry with no landing pad for the
526eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // region following the try-range.
5271b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  if (SawPotentiallyThrowing &&
52833adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner      MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf) {
529a583c55864e83e470333b7be878280b10e175a6eBill Wendling    CallSiteEntry Site = { LastLabel, 0, 0, 0 };
530eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    CallSites.push_back(Site);
531eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
532ade025c65c12503aba161a4fa399fd97414abaffBill Wendling}
533ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
5340dafca90761097230f02e655fdd541f59b888315Bill Wendling/// EmitExceptionTable - Emit landing pads and actions.
5350dafca90761097230f02e655fdd541f59b888315Bill Wendling///
5360dafca90761097230f02e655fdd541f59b888315Bill Wendling/// The general organization of the table is complex, but the basic concepts are
5370dafca90761097230f02e655fdd541f59b888315Bill Wendling/// easy.  First there is a header which describes the location and organization
5380dafca90761097230f02e655fdd541f59b888315Bill Wendling/// of the three components that follow.
539dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher///
5400dafca90761097230f02e655fdd541f59b888315Bill Wendling///  1. The landing pad site information describes the range of code covered by
5410dafca90761097230f02e655fdd541f59b888315Bill Wendling///     the try.  In our case it's an accumulation of the ranges covered by the
5420dafca90761097230f02e655fdd541f59b888315Bill Wendling///     invokes in the try.  There is also a reference to the landing pad that
5430dafca90761097230f02e655fdd541f59b888315Bill Wendling///     handles the exception once processed.  Finally an index into the actions
5440dafca90761097230f02e655fdd541f59b888315Bill Wendling///     table.
545a583c55864e83e470333b7be878280b10e175a6eBill Wendling///  2. The action table, in our case, is composed of pairs of type IDs and next
5460dafca90761097230f02e655fdd541f59b888315Bill Wendling///     action offset.  Starting with the action index from the landing pad
547a583c55864e83e470333b7be878280b10e175a6eBill Wendling///     site, each type ID is checked for a match to the current exception.  If
5480dafca90761097230f02e655fdd541f59b888315Bill Wendling///     it matches then the exception and type id are passed on to the landing
5490dafca90761097230f02e655fdd541f59b888315Bill Wendling///     pad.  Otherwise the next action is looked up.  This chain is terminated
5500dafca90761097230f02e655fdd541f59b888315Bill Wendling///     with a next action of zero.  If no type id is found the the frame is
5510dafca90761097230f02e655fdd541f59b888315Bill Wendling///     unwound and handling continues.
552a583c55864e83e470333b7be878280b10e175a6eBill Wendling///  3. Type ID table contains references to all the C++ typeinfo for all
5530dafca90761097230f02e655fdd541f59b888315Bill Wendling///     catches in the function.  This tables is reversed indexed base 1.
554ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::EmitExceptionTable() {
555ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
556ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
557ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
558ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  if (PadInfos.empty()) return;
559ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
560ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Sort the landing pads in order of their type ids.  This is used to fold
561ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // duplicate actions.
562ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<const LandingPadInfo *, 64> LandingPads;
563ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  LandingPads.reserve(PadInfos.size());
564ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
565ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
566ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    LandingPads.push_back(&PadInfos[i]);
567ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
568ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
569ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
570ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the actions table and gather the first action index for each
571ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // landing pad site.
572ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<ActionEntry, 32> Actions;
573ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<unsigned, 64> FirstActions;
574ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
575ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
576ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Invokes and nounwind calls have entries in PadMap (due to being bracketed
577ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // by try-range labels when lowered).  Ordinary calls do not, so appropriate
5781b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // try-ranges for them need be deduced when using Dwarf exception handling.
579ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  RangeMapType PadMap;
580ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
581ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    const LandingPadInfo *LandingPad = LandingPads[i];
582ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
583ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      unsigned BeginLabel = LandingPad->BeginLabels[j];
584ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
585ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadRange P = { i, j };
586ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadMap[BeginLabel] = P;
587ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    }
588ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  }
589ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
590ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the call-site table.
591ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<CallSiteEntry, 64> CallSites;
5928b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach  ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
593eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
594eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Final tallies.
595eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
596eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Call sites.
597eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned SiteStartSize  = sizeof(int32_t); // DW_EH_PE_udata4
598eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
599eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
6001b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  unsigned SizeSites;
601bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
60233adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  bool HaveTTData = (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
603bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach    ? (!TypeInfos.empty() || !FilterIds.empty()) : true;
604bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
605bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach
60633adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
6078b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    SizeSites = 0;
6081b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else
6091b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    SizeSites = CallSites.size() *
6101b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      (SiteStartSize + SiteLengthSize + LandingPadSize);
6111b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  for (unsigned i = 0, e = CallSites.size(); i < e; ++i) {
612af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner    SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
61333adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj)
614af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner      SizeSites += MCAsmInfo::getULEB128Size(i);
6151b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
616eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Type infos.
617eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
618eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
619eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
620b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling  unsigned TypeOffset = sizeof(int8_t) +   // Call site format
621af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner    MCAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
622eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeSites + SizeActions + SizeTypes;
623eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
624eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned TotalSize = sizeof(int8_t) + // LPStart format
625eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       sizeof(int8_t) + // TType format
626bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach       (HaveTTData ?
627af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner          MCAsmInfo::getULEB128Size(TypeOffset) : 0) + // TType base offset
628eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       TypeOffset;
629eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
630eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeAlign = (4 - TotalSize) & 3;
631eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
632eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin the exception table.
633d5bbb07ec806e6fa1e804afd7073987fdacc83e4Chris Lattner  const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
6346c2f9e14fdf14d8c1c687c6bd9918183fa7f8a7fChris Lattner  Asm->OutStreamer.SwitchSection(LSDASection);
635eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
636eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "GCC_except_table" << SubprogramCount << ":\n";
637eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
638eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != SizeAlign; ++i) {
639eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(0);
640eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Padding");
641c5800a809fd5e411de78ac1a4be633cd3e1d7840Bill Wendling  }
642eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
643eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("exception", SubprogramCount);
64433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
645ee793a6c197b2cccfee96c6da1bbe6a2048830ccJim Grosbach    SmallString<16> LSDAName;
646c40d9f9bae70c83947bf8fa5f9ee97adbf1bb0c0Jim Grosbach    raw_svector_ostream(LSDAName) << MAI->getPrivateGlobalPrefix() <<
647c40d9f9bae70c83947bf8fa5f9ee97adbf1bb0c0Jim Grosbach      "_LSDA_" << Asm->getFunctionNumber();
648c40d9f9bae70c83947bf8fa5f9ee97adbf1bb0c0Jim Grosbach    O << LSDAName.str() << ":\n";
6491b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
650eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
651eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the header.
652639217cb6a3948800c71c39503f8def4873a535eBill Wendling  Asm->EmitInt8(dwarf::DW_EH_PE_omit);
6530734d35044c304dd072f20e49840bcbea427db9cBill Wendling  Asm->EOL("@LPStart format", dwarf::DW_EH_PE_omit);
654b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
655ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#if 0
65681c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  if (TypeInfos.empty() && FilterIds.empty()) {
657ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // If there are no typeinfos or filters, there is nothing to emit, optimize
658ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // by specifying the "omit" encoding.
659639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_omit);
6600734d35044c304dd072f20e49840bcbea427db9cBill Wendling    Asm->EOL("@TType format", dwarf::DW_EH_PE_omit);
66181c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  } else {
662ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // Okay, we have actual filters or typeinfos to emit.  As such, we need to
663ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // pick a type encoding for them.  We're about to emit a list of pointers to
664ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // typeinfo objects at the end of the LSDA.  However, unless we're in static
665ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // mode, this reference will require a relocation by the dynamic linker.
66646b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //
667ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // Because of this, we have a couple of options:
668ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   1) If we are in -static mode, we can always use an absolute reference
669ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      from the LSDA, because the static linker will resolve it.
670ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   2) Otherwise, if the LSDA section is writable, we can output the direct
671ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      reference to the typeinfo and allow the dynamic linker to relocate
672ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      it.  Since it is in a writable section, the dynamic linker won't
673ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      have a problem.
674ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //   3) Finally, if we're in PIC mode and the LDSA section isn't writable,
675ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      we need to use some form of indirection.  For example, on Darwin,
676ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      we can output a statically-relocatable reference to a dyld stub. The
677ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      offset to the stub is constant, but the contents are in a section
678ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      that is updated by the dynamic linker.  This is easy enough, but we
679ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      need to tell the personality function of the unwinder to indirect
680ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //      through the dyld stub.
681ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //
682ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // FIXME: When this is actually implemented, we'll have to emit the stubs
683ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // somewhere.  This predicate should be moved to a shared location that is
684ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    // in target-independent code.
685ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    //
686ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    if (LSDASection->isWritable() ||
687ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner        Asm->TM.getRelocationModel() == Reloc::Static) {
688639217cb6a3948800c71c39503f8def4873a535eBill Wendling      Asm->EmitInt8(DW_EH_PE_absptr);
689639217cb6a3948800c71c39503f8def4873a535eBill Wendling      Asm->EOL("TType format (DW_EH_PE_absptr)");
690ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    } else {
691ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner      Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
692639217cb6a3948800c71c39503f8def4873a535eBill Wendling      Asm->EOL("TType format (DW_EH_PE_pcrel | DW_EH_PE_indirect"
693639217cb6a3948800c71c39503f8def4873a535eBill Wendling               " | DW_EH_PE_sdata4)");
694ad88bc4076d088baad8a5ee9561e46ac08a1c88aChris Lattner    }
695b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EmitULEB128Bytes(TypeOffset);
696b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EOL("TType base offset");
697b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling  }
698ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#else
699bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach  // For SjLj exceptions, if there is no TypeInfo, then we just explicitly
7001b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // say that we're omitting that bit.
7011b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // FIXME: does this apply to Dwarf also? The above #if 0 implies yes?
702bff392384ddb032c732c38ec78b91d7a25dcf467Jim Grosbach  if (!HaveTTData) {
703639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_omit);
7040734d35044c304dd072f20e49840bcbea427db9cBill Wendling    Asm->EOL("@TType format", dwarf::DW_EH_PE_omit);
7051b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else {
706639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
7070734d35044c304dd072f20e49840bcbea427db9cBill Wendling    Asm->EOL("@TType format", dwarf::DW_EH_PE_absptr);
7081b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(TypeOffset);
709a583c55864e83e470333b7be878280b10e175a6eBill Wendling    Asm->EOL("@TType base offset");
7101b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  }
711ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#endif
712b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
7131b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  // SjLj Exception handilng
71433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->getExceptionHandlingType() == ExceptionHandling::SjLj) {
715639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
7160734d35044c304dd072f20e49840bcbea427db9cBill Wendling    Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
7171b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(SizeSites);
718a583c55864e83e470333b7be878280b10e175a6eBill Wendling    Asm->EOL("Call site table length");
7191b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
7201b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // Emit the landing pad site information.
7218b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    unsigned idx = 0;
7228b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach    for (SmallVectorImpl<CallSiteEntry>::const_iterator
7238b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach         I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) {
7248b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach      const CallSiteEntry &S = *I;
725a583c55864e83e470333b7be878280b10e175a6eBill Wendling
726a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the landing pad, counted in 16-byte bundles relative to the
727a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // @LPStart address.
7288b818d7e98309125c6058c4ea72a7dc73b031db2Jim Grosbach      Asm->EmitULEB128Bytes(idx);
7291b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Landing pad");
730a583c55864e83e470333b7be878280b10e175a6eBill Wendling
731a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the first associated action record, relative to the start of
732a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table. This value is biased by 1 (1 indicates the start of
733a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table), and 0 indicates that there are no actions.
7341b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(S.Action);
7351b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Action");
7361b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    }
7371b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach  } else {
7381b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    // DWARF Exception handling
73933adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner    assert(MAI->getExceptionHandlingType() == ExceptionHandling::Dwarf);
7401b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach
741a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // The call-site table is a list of all call sites that may throw an
742a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // exception (including C++ 'throw' statements) in the procedure
743a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // fragment. It immediately follows the LSDA header. Each entry indicates,
744a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // for a given call, the first corresponding action record and corresponding
745a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // landing pad.
746a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
747a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // The table begins with the number of bytes, stored as an LEB128
748a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // compressed, unsigned integer. The records immediately follow the record
749a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // count. They are sorted in increasing call-site address. Each record
750a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // indicates:
751a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
752a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * The position of the call-site.
753a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * The position of the landing pad.
754a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * The first action record for that call site.
755a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
756a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // A missing entry in the call-site table indicates that a call is not
757a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // supposed to throw. Such calls include:
758a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
759a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * Calls to destructors within cleanup code. C++ semantics forbids these
760a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //     calls to throw.
761a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   * Calls to intrinsic routines in the standard library which are known
762a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //     not to throw (sin, memcpy, et al).
763a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
764a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // If the runtime does not find the call-site entry for a given call, it
765a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // will call `terminate()'.
766a583c55864e83e470333b7be878280b10e175a6eBill Wendling
767a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Emit the landing pad call site table.
768639217cb6a3948800c71c39503f8def4873a535eBill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
7690734d35044c304dd072f20e49840bcbea427db9cBill Wendling    Asm->EOL("Call site format", dwarf::DW_EH_PE_udata4);
7701b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    Asm->EmitULEB128Bytes(SizeSites);
771a583c55864e83e470333b7be878280b10e175a6eBill Wendling    Asm->EOL("Call site table size");
772eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7731b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    for (SmallVectorImpl<CallSiteEntry>::const_iterator
7745cff487665097d971067cbede1598e249f673182Bill Wendling         I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
7751b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const CallSiteEntry &S = *I;
7761b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      const char *BeginTag;
7771b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      unsigned BeginNumber;
778eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7791b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.BeginLabel) {
7801b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginTag = "eh_func_begin";
7811b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginNumber = SubprogramCount;
7821b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      } else {
7831b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginTag = "label";
7841b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        BeginNumber = S.BeginLabel;
7851b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      }
786eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
787a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the call site relative to the previous call site, counted in
788a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // number of 16-byte bundles. The first call site is counted relative to
789a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the start of the procedure fragment.
7901b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
7911b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                        true, true);
7921b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Region start");
793eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
7941b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.EndLabel)
7951b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
7961b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                       true);
7971b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      else
7981b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
799eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
8001b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Region length");
801eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
802a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the landing pad, counted in 16-byte bundles relative to the
803a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // @LPStart address.
8041b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      if (!S.PadLabel)
8051b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        Asm->EmitInt32(0);
8061b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      else
8071b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach        EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
8081b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach                          true, true);
809eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
8101b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Landing pad");
811eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
812a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // Offset of the first associated action record, relative to the start of
813a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table. This value is biased by 1 (1 indicates the start of
814a583c55864e83e470333b7be878280b10e175a6eBill Wendling      // the action table), and 0 indicates that there are no actions.
8151b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EmitULEB128Bytes(S.Action);
8161b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach      Asm->EOL("Action");
8171b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    }
818eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
819eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
820a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // Emit the Action Table.
8215cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<ActionEntry>::const_iterator
8225cff487665097d971067cbede1598e249f673182Bill Wendling         I = Actions.begin(), E = Actions.end(); I != E; ++I) {
8235cff487665097d971067cbede1598e249f673182Bill Wendling    const ActionEntry &Action = *I;
824a583c55864e83e470333b7be878280b10e175a6eBill Wendling
825a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Type Filter
826a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
827a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   Used by the runtime to match the type of the thrown exception to the
828a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   type of the catch clauses or the types in the exception specification.
829a583c55864e83e470333b7be878280b10e175a6eBill Wendling
830eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
831eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo index");
832a583c55864e83e470333b7be878280b10e175a6eBill Wendling
833a583c55864e83e470333b7be878280b10e175a6eBill Wendling    // Action Record
834a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //
835a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   Self-relative signed displacement in bytes of the next action record,
836a583c55864e83e470333b7be878280b10e175a6eBill Wendling    //   or 0 if there is no next action record.
837a583c55864e83e470333b7be878280b10e175a6eBill Wendling
838eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.NextAction);
839eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Next action");
840eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
841eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
842a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // Emit the Catch Clauses. The code for the catch clauses following the same
843a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // try is similar to a switch statement. The catch clause action record
844a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // informs the runtime about the type of a catch clause and about the
845a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // associated switch value.
846a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
847a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //  Action Record Fields:
848dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  //
849a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Filter Value
850a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     Positive value, starting at 1. Index in the types table of the
851a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     __typeinfo for the catch-clause type. 1 is the first word preceding
852a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     TTBase, 2 is the second word, and so on. Used by the runtime to check
853a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     if the thrown exception type matches the catch-clause type. Back-end
854a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     generated switch statements check against this value.
855a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
856a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //   * Next
857a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     Signed offset, in bytes from the start of this field, to the next
858a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //     chained action record, or zero if none.
859a583c55864e83e470333b7be878280b10e175a6eBill Wendling  //
860a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // The order of the action records determined by the next field is the order
861a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // of the catch clauses as they appear in the source code, and must be kept in
862a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // the same order. As a result, changing the order of the catch clause would
863a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // change the semantics of the program.
8645cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<GlobalVariable *>::const_reverse_iterator
8655cff487665097d971067cbede1598e249f673182Bill Wendling         I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
866a583c55864e83e470333b7be878280b10e175a6eBill Wendling    const GlobalVariable *GV = *I;
867eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective();
868eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
869eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (GV) {
870eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      std::string GLN;
871eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << Asm->getGlobalLinkName(GV, GLN);
872eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
873049e98d641f90e90e8312d76cbf4c68908fb9d1dBill Wendling      O << "0x0";
874eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
875eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
876eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo");
877eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
878eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
879a583c55864e83e470333b7be878280b10e175a6eBill Wendling  // Emit the Type Table.
8805cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<unsigned>::const_iterator
8815cff487665097d971067cbede1598e249f673182Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
8825cff487665097d971067cbede1598e249f673182Bill Wendling    unsigned TypeID = *I;
883eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(TypeID);
884eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Filter TypeInfo index");
885eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
886eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
887eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
888eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
889eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
890eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndModule - Emit all exception information that should come after the
891eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// content.
892eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndModule() {
89333adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MAI->getExceptionHandlingType() != ExceptionHandling::Dwarf)
8941b747ad8a0694b86e8d98a8b9a05ddfe74ec0cd3Jim Grosbach    return;
895b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling
89652783c63a13d384582997684384553e6545852d3Bill Wendling  if (!shouldEmitMovesModule && !shouldEmitTableModule)
89752783c63a13d384582997684384553e6545852d3Bill Wendling    return;
89852783c63a13d384582997684384553e6545852d3Bill Wendling
899eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
900eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
901eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
90252783c63a13d384582997684384553e6545852d3Bill Wendling  const std::vector<Function *> Personalities = MMI->getPersonalities();
903b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling
90452783c63a13d384582997684384553e6545852d3Bill Wendling  for (unsigned i = 0, e = Personalities.size(); i < e; ++i)
90552783c63a13d384582997684384553e6545852d3Bill Wendling    EmitCIE(Personalities[i], i);
906eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
90752783c63a13d384582997684384553e6545852d3Bill Wendling  for (std::vector<FunctionEHFrameInfo>::iterator
90852783c63a13d384582997684384553e6545852d3Bill Wendling         I = EHFrames.begin(), E = EHFrames.end(); I != E; ++I)
90952783c63a13d384582997684384553e6545852d3Bill Wendling    EmitFDE(*I);
910eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
911eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
912eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
913eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
914eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
915eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// BeginFunction - Gather pre-function exception information.  Assumes being
916eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// emitted immediately after the function entry point.
917eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::BeginFunction(MachineFunction *MF) {
918eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
919eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
920eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
921eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  this->MF = MF;
922eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTable = shouldEmitMoves = false;
923eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
92433adcfb4d217f5f23d9bde8ba02b8e48f9605fc5Chris Lattner  if (MMI && MAI->doesSupportExceptionHandling()) {
925eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Map all labels and get rid of any dead landing pads.
926eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    MMI->TidyLandingPads();
927eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
928eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If any landing pads survive, we need an EH table.
929b4049fe8cbb38416522f7b214f028ef0b7693d21Bill Wendling    if (!MMI->getLandingPads().empty())
930eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitTable = true;
931eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
932eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // See if we need frame move info.
933eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
934eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitMoves = true;
935eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
936eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (shouldEmitMoves || shouldEmitTable)
937eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Assumes in correct section after the entry point.
938eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitLabel("eh_func_begin", ++SubprogramCount);
939eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
940eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
941eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTableModule |= shouldEmitTable;
942eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitMovesModule |= shouldEmitMoves;
943eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
944eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
945eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
946eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
947eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
948eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndFunction - Gather and emit post-function exception information.
949eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling///
950eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndFunction() {
9517b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling  if (!shouldEmitMoves && !shouldEmitTable) return;
9527b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling
953dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  if (TimePassesIsEnabled)
954eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
955eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
9567b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling  EmitLabel("eh_func_end", SubprogramCount);
9577b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling  EmitExceptionTable();
9587b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling
9597b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling  // Save EH frame information
9607b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling  EHFrames.push_back(FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
9617b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         SubprogramCount,
9627b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         MMI->getPersonalityIndex(),
9637b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         MF->getFrameInfo()->hasCalls(),
9647b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         !MMI->getLandingPads().empty(),
9657b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         MMI->getFrameMoves(),
9667b09a6c62215e5ed30443b0d2b2a5ae18b89b94fBill Wendling                                         MF->getFunction()));
967eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
96852783c63a13d384582997684384553e6545852d3Bill Wendling  // Record if this personality index uses a landing pad.
96952783c63a13d384582997684384553e6545852d3Bill Wendling  UsesLSDA[MMI->getPersonalityIndex()] |= !MMI->getLandingPads().empty();
97052783c63a13d384582997684384553e6545852d3Bill Wendling
971dbfcdb976367ad5a9d3541fef90cd9f8dde7e2b4Eric Christopher  if (TimePassesIsEnabled)
972eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
973eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
974