DwarfException.cpp revision 46b754c76bd6a720dd1dd3be248cb93a4e25d3e5
1eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
2eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//
3eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//                     The LLVM Compiler Infrastructure
4eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//
5eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// This file is distributed under the University of Illinois Open Source
6eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// License. See LICENSE.TXT for details.
7eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//
8eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//===----------------------------------------------------------------------===//
9eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//
10eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling// This file contains support for writing dwarf exception info into asm files.
11eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//
12eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling//===----------------------------------------------------------------------===//
13eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
14eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "DwarfException.h"
15eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Module.h"
16eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/CodeGen/MachineModuleInfo.h"
17eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/CodeGen/MachineFrameInfo.h"
18eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/CodeGen/MachineLocation.h"
19eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Support/Dwarf.h"
20eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Support/Timer.h"
21eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Support/raw_ostream.h"
22eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetAsmInfo.h"
23eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetRegisterInfo.h"
24eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetData.h"
25eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetFrameInfo.h"
26eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/Target/TargetOptions.h"
27eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling#include "llvm/ADT/StringExtras.h"
28eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingusing namespace llvm;
29eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
30eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingstatic TimerGroup &getDwarfTimerGroup() {
31eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  static TimerGroup DwarfTimerGroup("Dwarf Exception");
32eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return DwarfTimerGroup;
33eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
34eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
35bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::DwarfException(raw_ostream &OS, AsmPrinter *A,
36bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling                               const TargetAsmInfo *T)
37bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  : Dwarf(OS, A, T, "eh"), shouldEmitTable(false), shouldEmitMoves(false),
38bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    shouldEmitTableModule(false), shouldEmitMovesModule(false),
39bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer(0) {
40bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  if (TimePassesIsEnabled)
41bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling    ExceptionTimer = new Timer("Dwarf Exception Writer",
42bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling                               getDwarfTimerGroup());
43bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
44bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
45bc0d23afae781c45f92865c1c27bed0ce51fe545Bill WendlingDwarfException::~DwarfException() {
46bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling  delete ExceptionTimer;
47bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling}
48bc0d23afae781c45f92865c1c27bed0ce51fe545Bill Wendling
49eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EmitCommonEHFrame(const Function *Personality,
50eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                       unsigned Index) {
51eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Size and sign of stack growth.
52eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int stackGrowth =
53eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->TM.getFrameInfo()->getStackGrowthDirection() ==
54eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TargetFrameInfo::StackGrowsUp ?
55eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TD->getPointerSize() : -TD->getPointerSize();
56eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
57eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin eh frame section.
58eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
59eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
60e2cf37b88c089a71727b3ecd466856f0cd638813Chris Lattner  if (TAI->is_EHSymbolPrivate())
61e2cf37b88c089a71727b3ecd466856f0cd638813Chris Lattner    O << TAI->getPrivateGlobalPrefix();
62eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
63eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "EH_frame" << Index << ":\n";
64eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("section_eh_frame", Index);
65eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
66eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define base labels.
67eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common", Index);
68eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
69eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Define the eh frame length.
70eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitDifference("eh_frame_common_end", Index,
71eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                 "eh_frame_common_begin", Index, true);
72eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("Length of Common Information Entry");
73eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
74eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH frame header.
75eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_begin", Index);
76eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt32((int)0);
77eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Identifier Tag");
78eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(dwarf::DW_CIE_VERSION);
79eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Version");
80eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
81eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The personality presence indicates that language specific information will
82eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // show up in the eh frame.
83eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitString(Personality ? "zPLR" : "zR");
84eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Augmentation");
85eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
86eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Round out reader.
87eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitULEB128Bytes(1);
88eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Code Alignment Factor");
89eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitSLEB128Bytes(stackGrowth);
90eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Data Alignment Factor");
91eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(RI->getDwarfRegNum(RI->getRARegister(), true));
92eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("CIE Return Address Column");
93eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
94eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If there is a personality, we need to indicate the functions location.
95eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (Personality) {
96eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(7);
97eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Augmentation Size");
98eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
99eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (TAI->getNeedsIndirectEncoding()) {
100eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 |
101eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                    dwarf::DW_EH_PE_indirect);
102eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Personality (pcrel sdata4 indirect)");
103eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
104eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
105eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Personality (pcrel sdata4)");
106eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
107eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
108eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective(true);
109eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << TAI->getPersonalityPrefix();
110eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitExternalGlobal((const GlobalVariable *)(Personality));
111eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << TAI->getPersonalitySuffix();
112eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (strcmp(TAI->getPersonalitySuffix(), "+4@GOTPCREL"))
113eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << "-" << TAI->getPCSymbol();
114eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Personality");
115eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
116eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
117eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("LSDA Encoding (pcrel sdata4)");
118eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
119eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
120eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE Encoding (pcrel sdata4)");
121eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  } else {
122eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(1);
123eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Augmentation Size");
124eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
125eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
126eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE Encoding (pcrel sdata4)");
127eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
128eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
129eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Indicate locations of general callee saved registers in frame.
130eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  std::vector<MachineMove> Moves;
131eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  RI->getInitialFrameState(Moves);
132eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitFrameMoves(NULL, 0, Moves, true);
133eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
134eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // On Darwin the linker honors the alignment of eh_frame, which means it must
135eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // be 8-byte on 64-bit targets to match what gcc does.  Otherwise you get
136eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // holes which confuse readers of eh_frame.
137eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
138eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                     0, 0, false);
139eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("eh_frame_common_end", Index);
140eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
141eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL();
142eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
143eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
144eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EmitEHFrame - Emit function exception frame information.
145eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling///
146eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
147eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  assert(!EHFrameInfo.function->hasAvailableExternallyLinkage() &&
148eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         "Should not emit 'available externally' functions at all");
149eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
1503e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  const Function *TheFunc = EHFrameInfo.function;
1513e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner
152eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
153eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
154eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Externally visible entry into the functions eh frame info. If the
155eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // corresponding function is static, this should not be externally visible.
1563e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!TheFunc->hasLocalLinkage())
157eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
158eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
159eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
160eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If corresponding function is weak definition, this should be too.
1613e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (TheFunc->isWeakForLinker() && TAI->getWeakDefDirective())
162eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
163eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
164eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If there are no calls then you can't unwind.  This may mean we can omit the
165eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // EH Frame, but some environments do not handle weak absolute symbols. If
166eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // UnwindTablesMandatory is set we cannot do this optimization; the unwind
167eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // info is to be available for non-EH uses.
1683e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner  if (!EHFrameInfo.hasCalls && !UnwindTablesMandatory &&
1693e0f60be46cb5363d4375f26a5e27d1dcd9867fbChris Lattner      (!TheFunc->isWeakForLinker() ||
170eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       !TAI->getWeakDefDirective() ||
171eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       TAI->getSupportsWeakOmittedEHFrame())) {
172eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << " = 0\n";
173eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // This name has no connection to the function, so it might get
174eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripped when the function is not, erroneously.  Prohibit
175eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // dead-stripping unconditionally.
176eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (const char *UsedDirective = TAI->getUsedDirective())
177eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << UsedDirective << EHFrameInfo.FnName << "\n\n";
178eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  } else {
179eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    O << EHFrameInfo.FnName << ":\n";
180eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
181eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // EH frame header.
182eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_frame_end", EHFrameInfo.Number,
183eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   "eh_frame_begin", EHFrameInfo.Number, true);
184eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Length of Frame Information Entry");
185eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
186eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_begin", EHFrameInfo.Number);
187eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
188a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner    EmitSectionOffset("eh_frame_begin", "eh_frame_common",
189a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      EHFrameInfo.Number, EHFrameInfo.PersonalityIndex,
190a4ff5e48fc6a4718d3a2af8654cfc06a17310aedChris Lattner                      true, true, false);
191eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
192eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE CIE offset");
193eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
194eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitReference("eh_func_begin", EHFrameInfo.Number, true, true);
195eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE initial location");
196eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitDifference("eh_func_end", EHFrameInfo.Number,
197eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   "eh_func_begin", EHFrameInfo.Number, true);
198eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("FDE address range");
199eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
200eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If there is a personality and landing pads then point to the language
201eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // specific data area in the exception table.
202eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (EHFrameInfo.PersonalityIndex) {
203eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitULEB128Bytes(4);
204eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
205eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
206eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (EHFrameInfo.hasLandingPads)
207eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        EmitReference("exception", EHFrameInfo.Number, true, true);
208eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      else
209eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        Asm->EmitInt32((int)0);
210eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Language Specific Data Area");
211eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
212eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitULEB128Bytes(0);
213eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EOL("Augmentation size");
214eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
215eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
216eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Indicate locations of function specific callee saved registers in frame.
217eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitFrameMoves("eh_func_begin", EHFrameInfo.Number, EHFrameInfo.Moves,
218eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                   true);
219eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
220eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // On Darwin the linker honors the alignment of eh_frame, which means it
221eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // must be 8-byte on 64-bit targets to match what gcc does.  Otherwise you
222eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // get holes which confuse readers of eh_frame.
223eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitAlignment(TD->getPointerSize() == sizeof(int32_t) ? 2 : 3,
224eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       0, 0, false);
225eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_frame_end", EHFrameInfo.Number);
226eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
227eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If the function is marked used, this table should be also.  We cannot
228eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // make the mark unconditional in this case, since retaining the table also
229eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // retains the function in this case, and there is code around that depends
230eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // on unused functions (calling undefined externals) being dead-stripped to
231eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // link correctly.  Yes, there really is.
232401e10c4fbfcdcfade5065093e2ca97f69a1d144Chris Lattner    if (MMI->isUsedFunction(EHFrameInfo.function))
233eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (const char *UsedDirective = TAI->getUsedDirective())
234eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        O << UsedDirective << EHFrameInfo.FnName << "\n\n";
235eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
236eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
237eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
238eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// SharedTypeIds - How many leading type ids two landing pads have in common.
239eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingunsigned DwarfException::SharedTypeIds(const LandingPadInfo *L,
240eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                       const LandingPadInfo *R) {
241eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
242eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
243eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
244eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned Count = 0;
245eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
246eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (; Count != MinSize; ++Count)
247eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[Count] != RIds[Count])
248eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return Count;
249eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
250eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return Count;
251eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
252eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
253eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// PadLT - Order landing pads lexicographically by type id.
254eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingbool DwarfException::PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
255eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
256eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LSize = LIds.size(), RSize = RIds.size();
257eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned MinSize = LSize < RSize ? LSize : RSize;
258eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
259eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != MinSize; ++i)
260eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (LIds[i] != RIds[i])
261eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      return LIds[i] < RIds[i];
262eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
263eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  return LSize < RSize;
264eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
265eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
266d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// ComputeActionsTable - Compute the actions table and gather the first action
267d4609622ade8d0ebaf57fd70700c2fedf0b3d165Bill Wendling/// index for each landing pad site.
268ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingunsigned DwarfException::
269ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads,
270ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<ActionEntry> &Actions,
271ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                    SmallVectorImpl<unsigned> &FirstActions) {
2725e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // Negative type IDs index into FilterIds. Positive type IDs index into
2735e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // TypeInfos.  The value written for a positive type ID is just the type ID
2745e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // itself.  For a negative type ID, however, the value written is the
275eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // (negative) byte offset of the corresponding FilterIds entry.  The byte
2765e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // offset is usually equal to the type ID (because the FilterIds entries are
2775e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // written using a variable width encoding, which outputs one byte per entry
2785e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // as long as the value written is not too large) but can differ.  This kind
2795e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  // of complication does not occur for positive type IDs because type infos are
280eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // output using a fixed width encoding.  FilterOffsets[i] holds the byte
281eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // offset corresponding to FilterIds[i].
282409914b773ffd53d53d214394d5636a76f673186Bill Wendling
283409914b773ffd53d53d214394d5636a76f673186Bill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
284eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  SmallVector<int, 16> FilterOffsets;
285eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FilterOffsets.reserve(FilterIds.size());
286eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int Offset = -1;
287409914b773ffd53d53d214394d5636a76f673186Bill Wendling
288409914b773ffd53d53d214394d5636a76f673186Bill Wendling  for (std::vector<unsigned>::const_iterator
289409914b773ffd53d53d214394d5636a76f673186Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) {
290eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FilterOffsets.push_back(Offset);
291eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Offset -= TargetAsmInfo::getULEB128Size(*I);
292eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
293eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
294eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  FirstActions.reserve(LandingPads.size());
295eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
296eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  int FirstAction = 0;
297eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeActions = 0;
2985e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  const LandingPadInfo *PrevLPI = 0;
299409914b773ffd53d53d214394d5636a76f673186Bill Wendling
3005cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
3015e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling         I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) {
3025e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const LandingPadInfo *LPI = *I;
3035e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const std::vector<int> &TypeIds = LPI->TypeIds;
3045e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    const unsigned NumShared = PrevLPI ? SharedTypeIds(LPI, PrevLPI) : 0;
305eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    unsigned SizeSiteActions = 0;
306eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
307eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (NumShared < TypeIds.size()) {
308eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned SizeAction = 0;
309eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      ActionEntry *PrevAction = 0;
310eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
311eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (NumShared) {
3125e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        const unsigned SizePrevIds = PrevLPI->TypeIds.size();
313eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        assert(Actions.size());
314eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
315eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) +
316eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
317eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
318eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        for (unsigned j = NumShared; j != SizePrevIds; ++j) {
319eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction -=
320eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
321eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          SizeAction += -PrevAction->NextAction;
322eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          PrevAction = PrevAction->Previous;
323eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
324eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
325eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
326eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Compute the actions.
3275e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling      for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) {
3285e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        int TypeID = TypeIds[J];
3295e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling        assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
330eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
331eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID);
332eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
333eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
334eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction);
335eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SizeSiteActions += SizeAction;
336eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
337eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
338eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        Actions.push_back(Action);
339eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PrevAction = &Actions.back();
340eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
341eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
342eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Record the first action of the landing pad site.
343eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
344eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } // else identical - re-use previous FirstAction
345eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
346eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    FirstActions.push_back(FirstAction);
347eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
348eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Compute this sites contribution to size.
349eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeActions += SizeSiteActions;
3505e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
3515e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling    PrevLPI = LPI;
352eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
353eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
3545e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling  return SizeActions;
3555e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling}
3565e953ddfa3abdd684c8a43f9c9c144f584ec7d6fBill Wendling
357ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// ComputeCallSiteTable - Compute the call-site table.  The entry for an invoke
358ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// has a try-range containing the call, a non-zero landing pad and an
359ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// appropriate action.  The entry for an ordinary call has a try-range
360ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// containing the call and zero for the landing pad and the action.  Calls
361ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// marked 'nounwind' have no entry and must not be contained in the try-range
362ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// of any entry - they form gaps in the table.  Entries must be ordered by
363ade025c65c12503aba161a4fa399fd97414abaffBill Wendling/// try-range address.
364ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::
365ade025c65c12503aba161a4fa399fd97414abaffBill WendlingComputeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
366ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const RangeMapType &PadMap,
367ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
368ade025c65c12503aba161a4fa399fd97414abaffBill Wendling                     const SmallVectorImpl<unsigned> &FirstActions) {
369eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // The end label of the previous invoke or nounwind try-range.
370eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned LastLabel = 0;
371eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
372eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Whether there is a potentially throwing instruction (currently this means
373eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // an ordinary call) between the end of the previous try-range and now.
374eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  bool SawPotentiallyThrowing = false;
375eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
3765cff487665097d971067cbede1598e249f673182Bill Wendling  // Whether the last CallSite entry was for an invoke.
377eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  bool PreviousIsInvoke = false;
378eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
379eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Visit all instructions in order of address.
380eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
381eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling       I != E; ++I) {
382eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
383eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling         MI != E; ++MI) {
384eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (!MI->isLabel()) {
385eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SawPotentiallyThrowing |= MI->getDesc().isCall();
386eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        continue;
387eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
388eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
389eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      unsigned BeginLabel = MI->getOperand(0).getImm();
390eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel && "Invalid label!");
391eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
392eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // End of the previous try-range?
393eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (BeginLabel == LastLabel)
394eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        SawPotentiallyThrowing = false;
395eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
396eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Beginning of a new try-range?
397eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      RangeMapType::iterator L = PadMap.find(BeginLabel);
398eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (L == PadMap.end())
399eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Nope, it was just some random label.
400eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        continue;
401eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
402eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      PadRange P = L->second;
403eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
404eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
405eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling             "Inconsistent landing pad map!");
406eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
407eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // If some instruction between the previous try-range and this one may
408eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // throw, create a call-site entry with no landing pad for the region
409eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // between the try-ranges.
410eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (SawPotentiallyThrowing) {
411eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
412eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSites.push_back(Site);
413eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
414eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
415eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
416eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      LastLabel = LandingPad->EndLabels[P.RangeIndex];
417eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      assert(BeginLabel && LastLabel && "Invalid landing pad!");
418eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
419eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      if (LandingPad->LandingPadLabel) {
420eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // This try-range is for an invoke.
421eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSiteEntry Site = {BeginLabel, LastLabel,
422eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                              LandingPad->LandingPadLabel,
423eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                              FirstActions[P.PadIndex]};
424eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
425eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Try to merge with the previous call-site.
426eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        if (PreviousIsInvoke) {
427eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          CallSiteEntry &Prev = CallSites.back();
428eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
429eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            // Extend the range of the previous entry.
430eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            Prev.EndLabel = Site.EndLabel;
431eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling            continue;
432eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling          }
433eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        }
434eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
435eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Otherwise, create a new call-site.
436eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        CallSites.push_back(Site);
437eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = true;
438eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      } else {
439eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        // Create a gap.
440eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling        PreviousIsInvoke = false;
441eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      }
442eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
443eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
444eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
445eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // If some instruction between the previous try-range and the end of the
446eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // function may throw, create a call-site entry with no landing pad for the
447eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // region following the try-range.
448eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (SawPotentiallyThrowing) {
449eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    CallSiteEntry Site = {LastLabel, 0, 0, 0};
450eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    CallSites.push_back(Site);
451eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
452ade025c65c12503aba161a4fa399fd97414abaffBill Wendling}
453ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
4540dafca90761097230f02e655fdd541f59b888315Bill Wendling/// EmitExceptionTable - Emit landing pads and actions.
4550dafca90761097230f02e655fdd541f59b888315Bill Wendling///
4560dafca90761097230f02e655fdd541f59b888315Bill Wendling/// The general organization of the table is complex, but the basic concepts are
4570dafca90761097230f02e655fdd541f59b888315Bill Wendling/// easy.  First there is a header which describes the location and organization
4580dafca90761097230f02e655fdd541f59b888315Bill Wendling/// of the three components that follow.
4590dafca90761097230f02e655fdd541f59b888315Bill Wendling///
4600dafca90761097230f02e655fdd541f59b888315Bill Wendling///  1. The landing pad site information describes the range of code covered by
4610dafca90761097230f02e655fdd541f59b888315Bill Wendling///     the try.  In our case it's an accumulation of the ranges covered by the
4620dafca90761097230f02e655fdd541f59b888315Bill Wendling///     invokes in the try.  There is also a reference to the landing pad that
4630dafca90761097230f02e655fdd541f59b888315Bill Wendling///     handles the exception once processed.  Finally an index into the actions
4640dafca90761097230f02e655fdd541f59b888315Bill Wendling///     table.
4650dafca90761097230f02e655fdd541f59b888315Bill Wendling///  2. The action table, in our case, is composed of pairs of type ids and next
4660dafca90761097230f02e655fdd541f59b888315Bill Wendling///     action offset.  Starting with the action index from the landing pad
4670dafca90761097230f02e655fdd541f59b888315Bill Wendling///     site, each type Id is checked for a match to the current exception.  If
4680dafca90761097230f02e655fdd541f59b888315Bill Wendling///     it matches then the exception and type id are passed on to the landing
4690dafca90761097230f02e655fdd541f59b888315Bill Wendling///     pad.  Otherwise the next action is looked up.  This chain is terminated
4700dafca90761097230f02e655fdd541f59b888315Bill Wendling///     with a next action of zero.  If no type id is found the the frame is
4710dafca90761097230f02e655fdd541f59b888315Bill Wendling///     unwound and handling continues.
4720dafca90761097230f02e655fdd541f59b888315Bill Wendling///  3. Type id table contains references to all the C++ typeinfo for all
4730dafca90761097230f02e655fdd541f59b888315Bill Wendling///     catches in the function.  This tables is reversed indexed base 1.
474ade025c65c12503aba161a4fa399fd97414abaffBill Wendlingvoid DwarfException::EmitExceptionTable() {
475ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
476ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
477ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
478ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  if (PadInfos.empty()) return;
479ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
480ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Sort the landing pads in order of their type ids.  This is used to fold
481ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // duplicate actions.
482ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<const LandingPadInfo *, 64> LandingPads;
483ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  LandingPads.reserve(PadInfos.size());
484ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
485ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
486ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    LandingPads.push_back(&PadInfos[i]);
487ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
488ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
489ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
490ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the actions table and gather the first action index for each
491ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // landing pad site.
492ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<ActionEntry, 32> Actions;
493ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<unsigned, 64> FirstActions;
494ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  unsigned SizeActions = ComputeActionsTable(LandingPads, Actions, FirstActions);
495ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
496ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Invokes and nounwind calls have entries in PadMap (due to being bracketed
497ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // by try-range labels when lowered).  Ordinary calls do not, so appropriate
498ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // try-ranges for them need be deduced.
499ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  RangeMapType PadMap;
500ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
501ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    const LandingPadInfo *LandingPad = LandingPads[i];
502ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
503ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      unsigned BeginLabel = LandingPad->BeginLabels[j];
504ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
505ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadRange P = { i, j };
506ade025c65c12503aba161a4fa399fd97414abaffBill Wendling      PadMap[BeginLabel] = P;
507ade025c65c12503aba161a4fa399fd97414abaffBill Wendling    }
508ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  }
509ade025c65c12503aba161a4fa399fd97414abaffBill Wendling
510ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  // Compute the call-site table.
511ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  SmallVector<CallSiteEntry, 64> CallSites;
512ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  ComputeCallSiteTable(CallSites, PadMap, LandingPads, FirstActions);
513eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
514eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Final tallies.
515eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
516eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Call sites.
517eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned SiteStartSize  = sizeof(int32_t); // DW_EH_PE_udata4
518eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned SiteLengthSize = sizeof(int32_t); // DW_EH_PE_udata4
519eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned LandingPadSize = sizeof(int32_t); // DW_EH_PE_udata4
520eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeSites = CallSites.size() * (SiteStartSize +
521eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                           SiteLengthSize +
522eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                                           LandingPadSize);
523eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
524eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action);
525eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
526eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Type infos.
527eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  const unsigned TypeInfoSize = TD->getPointerSize(); // DW_EH_PE_absptr
528eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeTypes = TypeInfos.size() * TypeInfoSize;
529eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
530eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned TypeOffset = sizeof(int8_t) + // Call site format
531eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    TargetAsmInfo::getULEB128Size(SizeSites) + // Call-site table length
532eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    SizeSites + SizeActions + SizeTypes;
533eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
534eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned TotalSize = sizeof(int8_t) + // LPStart format
535eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       sizeof(int8_t) + // TType format
536eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling           TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset
537eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                       TypeOffset;
538eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
539eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  unsigned SizeAlign = (4 - TotalSize) & 3;
540eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
541eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Begin the exception table.
54246b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner  //MCSection *LSDASection = TAI->getLSDASection();
54346b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner  //Asm->SwitchToSection(LSDASection);
54446b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner
545eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->SwitchToDataSection(TAI->getDwarfExceptionSection());
546eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
547eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  O << "GCC_except_table" << SubprogramCount << ":\n";
548eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
549eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  for (unsigned i = 0; i != SizeAlign; ++i) {
550eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitInt8(0);
551eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Padding");
552c5800a809fd5e411de78ac1a4be633cd3e1d7840Bill Wendling  }
553eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
554eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  EmitLabel("exception", SubprogramCount);
555eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
556eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the header.
557eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(dwarf::DW_EH_PE_omit);
558eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("LPStart format (DW_EH_PE_omit)");
559b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
560ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#if 0
5610dafca90761097230f02e655fdd541f59b888315Bill Wendling  // FIXME: This should default to what the system wants, not just "absptr".
56281c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  if (TypeInfos.empty() && FilterIds.empty()) {
56381c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner    Asm->EmitInt8(dwarf::DW_EH_PE_omit);
56481c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner    Asm->EOL("TType format (DW_EH_PE_omit)");
56581c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner  } else {
56646b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // FIXME: Instead of using "PreferredEHDataFormat", we should use a simple
56746b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // approach to determine what needs to happen.  Basically, if the target
56846b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // wants the LSDA to be emitted into a read-only segment (like .text) then
56946b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // (unless in static mode) it can't output direct pointers to the typeinfo
57046b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // objects, which may be in an arbitrary locations.  Instead, it has to use
57146b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // and indirect stub pointer to get to the typeinfo.
57246b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //
57346b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // If the target wants to dump the LSDA's into a segment writable by the
57446b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // dynamic linker, then it can just use a normal pointer, and the dynamic
57546b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // linker will fix it up.
57646b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner
57746b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // TODO: Replace the getDwarfExceptionSection() callback on TAI with a new
57846b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // getLSDASection() method on TLOF.  Merge and sanitize the implementations,
57946b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // and figure out what the ".gcc_except_table" directive expands to on elf
58046b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    // systems.
58146b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner
58246b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //
58346b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //if (LSDASection->isWritable()) {
58446b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //Asm->EmitInt8(DW_EH_PE_absptr);
58546b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //} else {
58646b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //Asm->EmitInt8(DW_EH_PE_pcrel | DW_EH_PE_indirect | DW_EH_PE_sdata4);
58746b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner    //}
58846b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner
5897a01e959156ba4e0a15b45a1f153f67d1646c0a5Chris Lattner    Asm->EmitInt8(TAI->PreferredEHDataFormat());
59081c9a069377e9474d010f04eebe8325fd11429e3Chris Lattner
59146b754c76bd6a720dd1dd3be248cb93a4e25d3e5Chris Lattner
592b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    // FIXME: The comment here should correspond with what PreferredEHDataFormat
593b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    // returned.
594b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EOL("TType format (DW_EH_PE_xxxxx)");
595b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EmitULEB128Bytes(TypeOffset);
596b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling    Asm->EOL("TType base offset");
597b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling  }
598ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#else
599ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  Asm->EmitInt8(dwarf::DW_EH_PE_absptr);
600ade025c65c12503aba161a4fa399fd97414abaffBill Wendling  Asm->EOL("TType format (DW_EH_PE_absptr)");
601409914b773ffd53d53d214394d5636a76f673186Bill Wendling  Asm->EmitULEB128Bytes(TypeOffset);
602409914b773ffd53d53d214394d5636a76f673186Bill Wendling  Asm->EOL("TType base offset");
603ade025c65c12503aba161a4fa399fd97414abaffBill Wendling#endif
604b0d9c3e7fdc952ae7cbe169b01ccaf1b80329403Bill Wendling
605eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitInt8(dwarf::DW_EH_PE_udata4);
606eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("Call site format (DW_EH_PE_udata4)");
607eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitULEB128Bytes(SizeSites);
608eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EOL("Call-site table length");
609eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
610eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the landing pad site information.
6115cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<CallSiteEntry>::const_iterator
6125cff487665097d971067cbede1598e249f673182Bill Wendling         I = CallSites.begin(), E = CallSites.end(); I != E; ++I) {
6135cff487665097d971067cbede1598e249f673182Bill Wendling    const CallSiteEntry &S = *I;
614eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    const char *BeginTag;
615eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    unsigned BeginNumber;
616eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
617eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (!S.BeginLabel) {
618eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      BeginTag = "eh_func_begin";
619eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      BeginNumber = SubprogramCount;
620eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
621eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      BeginTag = "label";
622eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      BeginNumber = S.BeginLabel;
623eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
624eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
625eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitSectionOffset(BeginTag, "eh_func_begin", BeginNumber, SubprogramCount,
626eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                      true, true);
627eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Region start");
628eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
629eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (!S.EndLabel)
630eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitDifference("eh_func_end", SubprogramCount, BeginTag, BeginNumber,
631eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                     true);
632eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    else
633eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitDifference("label", S.EndLabel, BeginTag, BeginNumber, true);
634eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
635eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Region length");
636eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
637eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (!S.PadLabel)
638eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      Asm->EmitInt32(0);
639eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    else
640eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitSectionOffset("label", "eh_func_begin", S.PadLabel, SubprogramCount,
641eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                        true, true);
642eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
643eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Landing pad");
644eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
645eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(S.Action);
646eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Action");
647eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
648eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
649eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the actions.
6505cff487665097d971067cbede1598e249f673182Bill Wendling  for (SmallVectorImpl<ActionEntry>::const_iterator
6515cff487665097d971067cbede1598e249f673182Bill Wendling         I = Actions.begin(), E = Actions.end(); I != E; ++I) {
6525cff487665097d971067cbede1598e249f673182Bill Wendling    const ActionEntry &Action = *I;
653eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.ValueForTypeID);
654eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo index");
655eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitSLEB128Bytes(Action.NextAction);
656eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Next action");
657eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
658eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
659eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the type ids.
6605cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<GlobalVariable *>::const_reverse_iterator
6615cff487665097d971067cbede1598e249f673182Bill Wendling         I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) {
6625cff487665097d971067cbede1598e249f673182Bill Wendling    GlobalVariable *GV = *I;
663eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    PrintRelDirective();
664eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
665eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (GV) {
666eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      std::string GLN;
667eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << Asm->getGlobalLinkName(GV, GLN);
668eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    } else {
669eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      O << "0";
670eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    }
671eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
672eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("TypeInfo");
673eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
674eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
675eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  // Emit the filter typeids.
6765cff487665097d971067cbede1598e249f673182Bill Wendling  for (std::vector<unsigned>::const_iterator
6775cff487665097d971067cbede1598e249f673182Bill Wendling         I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) {
6785cff487665097d971067cbede1598e249f673182Bill Wendling    unsigned TypeID = *I;
679eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EmitULEB128Bytes(TypeID);
680eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    Asm->EOL("Filter TypeInfo index");
681eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
682eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
683eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  Asm->EmitAlignment(2, 0, 0, false);
684eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
685eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
686eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndModule - Emit all exception information that should come after the
687eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// content.
688eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndModule() {
689eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
690eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
691eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
692eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (shouldEmitMovesModule || shouldEmitTableModule) {
693eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    const std::vector<Function *> Personalities = MMI->getPersonalities();
694eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (unsigned i = 0; i < Personalities.size(); ++i)
695eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitCommonEHFrame(Personalities[i], i);
696eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
697eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    for (std::vector<FunctionEHFrameInfo>::iterator I = EHFrames.begin(),
698eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling           E = EHFrames.end(); I != E; ++I)
699eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitEHFrame(*I);
700eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
701eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
702eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
703eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
704eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
705eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
706eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// BeginFunction - Gather pre-function exception information.  Assumes being
707eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// emitted immediately after the function entry point.
708eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::BeginFunction(MachineFunction *MF) {
709eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
710eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
711eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
712eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  this->MF = MF;
713eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTable = shouldEmitMoves = false;
714eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
715eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (MMI && TAI->doesSupportExceptionHandling()) {
716eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Map all labels and get rid of any dead landing pads.
717eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    MMI->TidyLandingPads();
718eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
719eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // If any landing pads survive, we need an EH table.
720eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (MMI->getLandingPads().size())
721eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitTable = true;
722eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
723eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // See if we need frame move info.
724eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (!MF->getFunction()->doesNotThrow() || UnwindTablesMandatory)
725eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      shouldEmitMoves = true;
726eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
727eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    if (shouldEmitMoves || shouldEmitTable)
728eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      // Assumes in correct section after the entry point.
729eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling      EmitLabel("eh_func_begin", ++SubprogramCount);
730eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
731eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
732eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitTableModule |= shouldEmitTable;
733eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  shouldEmitMovesModule |= shouldEmitMoves;
734eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
735eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
736eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
737eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
738eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
739eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling/// EndFunction - Gather and emit post-function exception information.
740eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling///
741eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendlingvoid DwarfException::EndFunction() {
742eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
743eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->startTimer();
744eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
745eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (shouldEmitMoves || shouldEmitTable) {
746eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitLabel("eh_func_end", SubprogramCount);
747eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EmitExceptionTable();
748eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
749eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    // Save EH frame information
750eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    EHFrames.push_back(
751e2cf37b88c089a71727b3ecd466856f0cd638813Chris Lattner        FunctionEHFrameInfo(getAsm()->getCurrentFunctionEHName(MF),
752eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            SubprogramCount,
753eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MMI->getPersonalityIndex(),
754eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MF->getFrameInfo()->hasCalls(),
755eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            !MMI->getLandingPads().empty(),
756eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MMI->getFrameMoves(),
757eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling                            MF->getFunction()));
758eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  }
759eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling
760eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling  if (TimePassesIsEnabled)
761eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling    ExceptionTimer->stopTimer();
762eb9072195ca7d777a71194dcaaaa369d82f24f53Bill Wendling}
763