1afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray//===----- JITDwarfEmitter.cpp - Write dwarf tables into memory -----------===//
2afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray//
3afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray//                     The LLVM Compiler Infrastructure
4afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray//
5afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray// This file is distributed under the University of Illinois Open Source
6afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray// License. See LICENSE.TXT for details.
7afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray//
8afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray//===----------------------------------------------------------------------===//
9afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray//
10afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray// This file defines a JITDwarfEmitter object that is used by the JIT to
11afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray// write dwarf tables to memory.
12afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray//
13afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray//===----------------------------------------------------------------------===//
14afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
15afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "JIT.h"
16afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "JITDwarfEmitter.h"
17afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/Function.h"
18afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/ADT/DenseMap.h"
19a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes#include "llvm/CodeGen/JITCodeEmitter.h"
20afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/CodeGen/MachineFunction.h"
21afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/CodeGen/MachineModuleInfo.h"
22afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/ExecutionEngine/JITMemoryManager.h"
232d28617de2b0b731c08d1af9e830f31e14ac75b4Evan Cheng#include "llvm/MC/MachineLocation.h"
24af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner#include "llvm/MC/MCAsmInfo.h"
251611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner#include "llvm/MC/MCSymbol.h"
262d28617de2b0b731c08d1af9e830f31e14ac75b4Evan Cheng#include "llvm/Support/ErrorHandling.h"
27afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/Target/TargetData.h"
28afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/Target/TargetInstrInfo.h"
2916c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov#include "llvm/Target/TargetFrameLowering.h"
30afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/Target/TargetMachine.h"
31afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/Target/TargetRegisterInfo.h"
32afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffrayusing namespace llvm;
33afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
34003de66227d235a9ca7373d9cb2c0b1b6ae5b81aDaniel DunbarJITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : MMI(0), Jit(theJit) {}
35afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
36afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
378bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbachunsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F,
38a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes                                               JITCodeEmitter& jce,
39afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray                                               unsigned char* StartFunction,
402763217fbd2f1c54a7a25fd3ae9e997ea6ece0cbReid Kleckner                                               unsigned char* EndFunction,
412763217fbd2f1c54a7a25fd3ae9e997ea6ece0cbReid Kleckner                                               unsigned char* &EHFramePtr) {
42003de66227d235a9ca7373d9cb2c0b1b6ae5b81aDaniel Dunbar  assert(MMI && "MachineModuleInfo not registered!");
43003de66227d235a9ca7373d9cb2c0b1b6ae5b81aDaniel Dunbar
44afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  const TargetMachine& TM = F.getTarget();
45afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  TD = TM.getTargetData();
4616c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov  stackGrowthDirection = TM.getFrameLowering()->getStackGrowthDirection();
47afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  RI = TM.getRegisterInfo();
482d28617de2b0b731c08d1af9e830f31e14ac75b4Evan Cheng  MAI = TM.getMCAsmInfo();
49a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE = &jce;
508bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
51afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
52afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray                                                     EndFunction);
538bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
54afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned char* Result = 0;
55afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
5646510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  const std::vector<const Function *> Personalities = MMI->getPersonalities();
57afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]);
58afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
59afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr,
60afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray                       StartFunction, EndFunction, ExceptionTable);
619d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling
62afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  return Result;
63afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray}
64afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
65afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
668bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbachvoid
675913e6c5dbaff88ac00d5e679382abc72323831aNicolas GeoffrayJITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
685913e6c5dbaff88ac00d5e679382abc72323831aNicolas Geoffray                                const std::vector<MachineMove> &Moves) const {
69afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned PointerSize = TD->getPointerSize();
7016c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov  int stackGrowth = stackGrowthDirection == TargetFrameLowering::StackGrowsUp ?
71afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray          PointerSize : -PointerSize;
722e9919a5e5fe76f4b1e3290103c4bfd149ebba9cChris Lattner  MCSymbol *BaseLabel = 0;
73afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
74afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
75afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    const MachineMove &Move = Moves[i];
762e9919a5e5fe76f4b1e3290103c4bfd149ebba9cChris Lattner    MCSymbol *Label = Move.getLabel();
778bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
781611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner    // Throw out move if the label is invalid.
79091fc4b5560e0c87db74855aebecb3f872740c7dBill Wendling    if (Label && (*JCE->getLabelLocations())[Label] == 0)
801611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner      continue;
818bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
82afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    intptr_t LabelPtr = 0;
832e9919a5e5fe76f4b1e3290103c4bfd149ebba9cChris Lattner    if (Label) LabelPtr = JCE->getLabelAddress(Label);
84afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
85afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    const MachineLocation &Dst = Move.getDestination();
86afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    const MachineLocation &Src = Move.getSource();
878bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
88afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    // Advance row if new location.
892e9919a5e5fe76f4b1e3290103c4bfd149ebba9cChris Lattner    if (BaseLabelPtr && Label && BaseLabel != Label) {
90a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      JCE->emitByte(dwarf::DW_CFA_advance_loc4);
91a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      JCE->emitInt32(LabelPtr - BaseLabelPtr);
928bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
938bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach      BaseLabel = Label;
94afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      BaseLabelPtr = LabelPtr;
95afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    }
968bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
97afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    // If advancing cfa.
98489032a2b75f520cf73a53b79dc61792076eb95eDaniel Dunbar    if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
99489032a2b75f520cf73a53b79dc61792076eb95eDaniel Dunbar      if (!Src.isReg()) {
100489032a2b75f520cf73a53b79dc61792076eb95eDaniel Dunbar        if (Src.getReg() == MachineLocation::VirtualFP) {
101a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes          JCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
102afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        } else {
103a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes          JCE->emitByte(dwarf::DW_CFA_def_cfa);
104a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes          JCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true));
105afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        }
1068bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
1079d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling        JCE->emitULEB128Bytes(-Src.getOffset());
108afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      } else {
1099d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling        llvm_unreachable("Machine move not supported yet.");
110afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      }
111489032a2b75f520cf73a53b79dc61792076eb95eDaniel Dunbar    } else if (Src.isReg() &&
112489032a2b75f520cf73a53b79dc61792076eb95eDaniel Dunbar      Src.getReg() == MachineLocation::VirtualFP) {
113489032a2b75f520cf73a53b79dc61792076eb95eDaniel Dunbar      if (Dst.isReg()) {
114a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitByte(dwarf::DW_CFA_def_cfa_register);
115a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true));
116afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      } else {
1179d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling        llvm_unreachable("Machine move not supported yet.");
118afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      }
119afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    } else {
120489032a2b75f520cf73a53b79dc61792076eb95eDaniel Dunbar      unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
121afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      int Offset = Dst.getOffset() / stackGrowth;
1228bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
123afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      if (Offset < 0) {
124a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
125a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitULEB128Bytes(Reg);
126a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitSLEB128Bytes(Offset);
127afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      } else if (Reg < 64) {
128a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitByte(dwarf::DW_CFA_offset + Reg);
129a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitULEB128Bytes(Offset);
130afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      } else {
131a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitByte(dwarf::DW_CFA_offset_extended);
132a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitULEB128Bytes(Reg);
133a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitULEB128Bytes(Offset);
134afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      }
135afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    }
136afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
137afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray}
138afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
139afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray/// SharedTypeIds - How many leading type ids two landing pads have in common.
140afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffraystatic unsigned SharedTypeIds(const LandingPadInfo *L,
141afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray                              const LandingPadInfo *R) {
142afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
143afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned LSize = LIds.size(), RSize = RIds.size();
144afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned MinSize = LSize < RSize ? LSize : RSize;
145afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned Count = 0;
146afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
147afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (; Count != MinSize; ++Count)
148afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    if (LIds[Count] != RIds[Count])
149afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      return Count;
150afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
151afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  return Count;
152afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray}
153afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
154afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
155afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray/// PadLT - Order landing pads lexicographically by type id.
156afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffraystatic bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
157afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
158afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned LSize = LIds.size(), RSize = RIds.size();
159afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned MinSize = LSize < RSize ? LSize : RSize;
160afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
161afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (unsigned i = 0; i != MinSize; ++i)
162afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    if (LIds[i] != RIds[i])
163afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      return LIds[i] < RIds[i];
164afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
165afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  return LSize < RSize;
166afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray}
167afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
168844731a7f1909f55935e3514c9e713a62d67662eDan Gohmannamespace {
169844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
170afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray/// ActionEntry - Structure describing an entry in the actions table.
171afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffraystruct ActionEntry {
172afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  int ValueForTypeID; // The value to write - may not be equal to the type id.
173afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  int NextAction;
174afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  struct ActionEntry *Previous;
175afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray};
176afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
177afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray/// PadRange - Structure holding a try-range and the associated landing pad.
178afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffraystruct PadRange {
179afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // The index of the landing pad.
180afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned PadIndex;
181afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // The index of the begin and end labels in the landing pad's label lists.
182afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned RangeIndex;
183afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray};
184afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
1851611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattnertypedef DenseMap<MCSymbol*, PadRange> RangeMapType;
186afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
187afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray/// CallSiteEntry - Structure describing an entry in the call-site table.
188afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffraystruct CallSiteEntry {
1891611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner  MCSymbol *BeginLabel; // zero indicates the start of the function.
1901611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner  MCSymbol *EndLabel;   // zero indicates the end of the function.
1911611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner  MCSymbol *PadLabel;   // zero indicates that there is no landing pad.
192afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned Action;
193afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray};
194afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
195844731a7f1909f55935e3514c9e713a62d67662eDan Gohman}
196844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
197afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffrayunsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF,
198afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray                                         unsigned char* StartFunction,
1995913e6c5dbaff88ac00d5e679382abc72323831aNicolas Geoffray                                         unsigned char* EndFunction) const {
200003de66227d235a9ca7373d9cb2c0b1b6ae5b81aDaniel Dunbar  assert(MMI && "MachineModuleInfo not registered!");
201003de66227d235a9ca7373d9cb2c0b1b6ae5b81aDaniel Dunbar
202afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Map all labels and get rid of any dead landing pads.
20347639fc5be0b8f4873d076a9ed24b9a3c0682b15Bill Wendling  MMI->TidyLandingPads(JCE->getLabelLocations());
204afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
20546510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
206afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
207afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
208afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  if (PadInfos.empty()) return 0;
209afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
210afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Sort the landing pads in order of their type ids.  This is used to fold
211afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // duplicate actions.
212afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  SmallVector<const LandingPadInfo *, 64> LandingPads;
213afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  LandingPads.reserve(PadInfos.size());
214afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
215afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    LandingPads.push_back(&PadInfos[i]);
216afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
217afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
218afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Negative type ids index into FilterIds, positive type ids index into
219afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // TypeInfos.  The value written for a positive type id is just the type
220afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // id itself.  For a negative type id, however, the value written is the
221afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // (negative) byte offset of the corresponding FilterIds entry.  The byte
222afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // offset is usually equal to the type id, because the FilterIds entries
223afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // are written using a variable width encoding which outputs one byte per
224afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // entry as long as the value written is not too large, but can differ.
225afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // This kind of complication does not occur for positive type ids because
226afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // type infos are output using a fixed width encoding.
227afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
228afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  SmallVector<int, 16> FilterOffsets;
229afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  FilterOffsets.reserve(FilterIds.size());
230afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  int Offset = -1;
231afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
232afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    E = FilterIds.end(); I != E; ++I) {
233afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    FilterOffsets.push_back(Offset);
234af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner    Offset -= MCAsmInfo::getULEB128Size(*I);
235afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
236afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
237afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Compute the actions table and gather the first action index for each
238afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // landing pad site.
239afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  SmallVector<ActionEntry, 32> Actions;
240afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  SmallVector<unsigned, 64> FirstActions;
241afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  FirstActions.reserve(LandingPads.size());
242afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
243afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  int FirstAction = 0;
244afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned SizeActions = 0;
245afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
246afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    const LandingPadInfo *LP = LandingPads[i];
247afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    const std::vector<int> &TypeIds = LP->TypeIds;
248afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
249afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    unsigned SizeSiteActions = 0;
250afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
251afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    if (NumShared < TypeIds.size()) {
252afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      unsigned SizeAction = 0;
253afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      ActionEntry *PrevAction = 0;
254afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
255afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      if (NumShared) {
256afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
257afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        assert(Actions.size());
258afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        PrevAction = &Actions.back();
259af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
260af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner          MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
261afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        for (unsigned j = NumShared; j != SizePrevIds; ++j) {
262af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner          SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
263afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray          SizeAction += -PrevAction->NextAction;
264afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray          PrevAction = PrevAction->Previous;
265afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        }
266afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      }
267afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
268afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      // Compute the actions.
269afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
270afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        int TypeID = TypeIds[I];
271afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
272afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
273af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
274afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
275afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
276af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner        SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
277afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        SizeSiteActions += SizeAction;
278afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
279afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
280afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        Actions.push_back(Action);
281afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
282afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        PrevAction = &Actions.back();
283afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      }
284afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
285afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      // Record the first action of the landing pad site.
286afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
287afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    } // else identical - re-use previous FirstAction
288afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
289afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    FirstActions.push_back(FirstAction);
290afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
291afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    // Compute this sites contribution to size.
292afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    SizeActions += SizeSiteActions;
293afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
294afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
295afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Compute the call-site table.  Entries must be ordered by address.
296afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  SmallVector<CallSiteEntry, 64> CallSites;
297afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
298afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  RangeMapType PadMap;
299afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
300afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    const LandingPadInfo *LandingPad = LandingPads[i];
301afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
3021611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner      MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
303afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
304afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      PadRange P = { i, j };
305afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      PadMap[BeginLabel] = P;
306afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    }
307afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
308afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
309afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  bool MayThrow = false;
3101611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner  MCSymbol *LastLabel = 0;
311afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
312afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        I != E; ++I) {
313afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
314afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray          MI != E; ++MI) {
3154406604047423576e36657c7ede266ca42e79642Dan Gohman      if (!MI->isLabel()) {
3165a96b3dad2f634c9081c8b2b6c2575441dc5a2bdEvan Cheng        MayThrow |= MI->isCall();
317afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        continue;
318afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      }
319afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
3206b4205aa44094f96115be72dd23aaf47a0257d2fChris Lattner      MCSymbol *BeginLabel = MI->getOperand(0).getMCSymbol();
321afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      assert(BeginLabel && "Invalid label!");
322afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
323afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      if (BeginLabel == LastLabel)
324afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        MayThrow = false;
325afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
326afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      RangeMapType::iterator L = PadMap.find(BeginLabel);
327afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
328afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      if (L == PadMap.end())
329afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        continue;
330afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
331afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      PadRange P = L->second;
332afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
333afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
334afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
335afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray              "Inconsistent landing pad map!");
336afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
337afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      // If some instruction between the previous try-range and this one may
338afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      // throw, create a call-site entry with no landing pad for the region
339afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      // between the try-ranges.
340afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      if (MayThrow) {
341afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
342afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        CallSites.push_back(Site);
343afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      }
344afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
345afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      LastLabel = LandingPad->EndLabels[P.RangeIndex];
346afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      CallSiteEntry Site = {BeginLabel, LastLabel,
347afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
348afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
349afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
350afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray              "Invalid landing pad!");
351afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
352afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      // Try to merge with the previous call-site.
353afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      if (CallSites.size()) {
354719de53742167ca3f0e6b2efafb6eac18bd90452Dan Gohman        CallSiteEntry &Prev = CallSites.back();
355afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
356afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray          // Extend the range of the previous entry.
357afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray          Prev.EndLabel = Site.EndLabel;
358afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray          continue;
359afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray        }
360afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      }
361afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
362afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      // Otherwise, create a new call-site.
363afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      CallSites.push_back(Site);
364afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    }
365afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
366afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // If some instruction between the previous try-range and the end of the
367afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // function may throw, create a call-site entry with no landing pad for the
368afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // region following the try-range.
369afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  if (MayThrow) {
370afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    CallSiteEntry Site = {LastLabel, 0, 0, 0};
371afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    CallSites.push_back(Site);
372afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
373afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
374afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Final tallies.
375afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
376afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray                                            sizeof(int32_t) + // Site length.
377afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray                                            sizeof(int32_t)); // Landing pad.
378afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
379af76e592c7f9deff0e55c13dbb4a34f07f1c7f64Chris Lattner    SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
380afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
381afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
382afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
383afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned TypeOffset = sizeof(int8_t) + // Call site format
384afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray                        // Call-site table length
3858bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach                        MCAsmInfo::getULEB128Size(SizeSites) +
386afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray                        SizeSites + SizeActions + SizeTypes;
387afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
388afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Begin the exception table.
38901248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner  JCE->emitAlignmentWithFill(4, 0);
39001248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner  // Asm->EOL("Padding");
39101248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner
392a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue();
393afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
394afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Emit the header.
395a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitByte(dwarf::DW_EH_PE_omit);
396afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Asm->EOL("LPStart format (DW_EH_PE_omit)");
397a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitByte(dwarf::DW_EH_PE_absptr);
398afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Asm->EOL("TType format (DW_EH_PE_absptr)");
399a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitULEB128Bytes(TypeOffset);
400afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Asm->EOL("TType base offset");
401a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitByte(dwarf::DW_EH_PE_udata4);
402afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Asm->EOL("Call site format (DW_EH_PE_udata4)");
403a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitULEB128Bytes(SizeSites);
404afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Asm->EOL("Call-site table length");
405afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
406afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Emit the landing pad site information.
407afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (unsigned i = 0; i < CallSites.size(); ++i) {
408afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    CallSiteEntry &S = CallSites[i];
409afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    intptr_t BeginLabelPtr = 0;
410afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    intptr_t EndLabelPtr = 0;
411afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
412afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    if (!S.BeginLabel) {
413afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      BeginLabelPtr = (intptr_t)StartFunction;
414a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      JCE->emitInt32(0);
415afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    } else {
416a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel);
417a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
418afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    }
419afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
420afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    // Asm->EOL("Region start");
421afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
422abeca44d6387930b9ceadc51260f3d58bdc402eaBill Wendling    if (!S.EndLabel)
423afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      EndLabelPtr = (intptr_t)EndFunction;
424abeca44d6387930b9ceadc51260f3d58bdc402eaBill Wendling    else
425a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      EndLabelPtr = JCE->getLabelAddress(S.EndLabel);
426abeca44d6387930b9ceadc51260f3d58bdc402eaBill Wendling
427abeca44d6387930b9ceadc51260f3d58bdc402eaBill Wendling    JCE->emitInt32(EndLabelPtr - BeginLabelPtr);
428afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    //Asm->EOL("Region length");
429afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
430afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    if (!S.PadLabel) {
431a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      JCE->emitInt32(0);
432afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    } else {
433a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      unsigned PadLabelPtr = JCE->getLabelAddress(S.PadLabel);
434a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      JCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
435afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    }
436afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    // Asm->EOL("Landing pad");
437afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
438a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitULEB128Bytes(S.Action);
439afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    // Asm->EOL("Action");
440afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
441afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
442afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Emit the actions.
443afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
444afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    ActionEntry &Action = Actions[I];
445afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
446a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitSLEB128Bytes(Action.ValueForTypeID);
447afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    //Asm->EOL("TypeInfo index");
448a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitSLEB128Bytes(Action.NextAction);
449afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    //Asm->EOL("Next action");
450afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
451afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
452afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Emit the type ids.
453afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (unsigned M = TypeInfos.size(); M; --M) {
45446510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    const GlobalVariable *GV = TypeInfos[M - 1];
4558bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
456afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    if (GV) {
4579d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling      if (TD->getPointerSize() == sizeof(int32_t))
458a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
4599d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling      else
460a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
461afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    } else {
462afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      if (TD->getPointerSize() == sizeof(int32_t))
463a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitInt32(0);
464afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray      else
465a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes        JCE->emitInt64(0);
466afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    }
467afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    // Asm->EOL("TypeInfo");
468afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
469afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
470afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Emit the filter typeids.
471afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
472afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    unsigned TypeID = FilterIds[j];
473a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitULEB128Bytes(TypeID);
474afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    //Asm->EOL("Filter TypeInfo index");
475afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
47601248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner
47701248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner  JCE->emitAlignmentWithFill(4, 0);
478afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
479afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  return DwarfExceptionTable;
480afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray}
481afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
4825913e6c5dbaff88ac00d5e679382abc72323831aNicolas Geoffrayunsigned char*
4835913e6c5dbaff88ac00d5e679382abc72323831aNicolas GeoffrayJITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const {
484afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned PointerSize = TD->getPointerSize();
48516c29b5f285f375be53dabaa73e3e91107485fe4Anton Korobeynikov  int stackGrowth = stackGrowthDirection == TargetFrameLowering::StackGrowsUp ?
486afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray          PointerSize : -PointerSize;
4878bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
488a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  unsigned char* StartCommonPtr = (unsigned char*)JCE->getCurrentPCValue();
489afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // EH Common Frame header
490a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->allocateSpace(4, 0);
491a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  unsigned char* FrameCommonBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
492a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitInt32((int)0);
493a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitByte(dwarf::DW_CIE_VERSION);
494a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitString(Personality ? "zPLR" : "zR");
495a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitULEB128Bytes(1);
496a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitSLEB128Bytes(stackGrowth);
497a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
4989d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling
499afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  if (Personality) {
50042cc8f140a4332d949ec40726d6a011cf1e9f0d5Nicolas Geoffray    // Augmentation Size: 3 small ULEBs of one byte each, and the personality
50142cc8f140a4332d949ec40726d6a011cf1e9f0d5Nicolas Geoffray    // function which size is PointerSize.
5028bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach    JCE->emitULEB128Bytes(3 + PointerSize);
5038bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
50442cc8f140a4332d949ec40726d6a011cf1e9f0d5Nicolas Geoffray    // We set the encoding of the personality as direct encoding because we use
50542cc8f140a4332d949ec40726d6a011cf1e9f0d5Nicolas Geoffray    // the function pointer. The encoding is not relative because the current
50642cc8f140a4332d949ec40726d6a011cf1e9f0d5Nicolas Geoffray    // PC value may be bigger than the personality function pointer.
50742cc8f140a4332d949ec40726d6a011cf1e9f0d5Nicolas Geoffray    if (PointerSize == 4) {
5088bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach      JCE->emitByte(dwarf::DW_EH_PE_sdata4);
509a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      JCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality)));
51042cc8f140a4332d949ec40726d6a011cf1e9f0d5Nicolas Geoffray    } else {
511a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      JCE->emitByte(dwarf::DW_EH_PE_sdata8);
512a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes      JCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality)));
51342cc8f140a4332d949ec40726d6a011cf1e9f0d5Nicolas Geoffray    }
5149d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling
51589ee706b6b64aaef7227e2e232b4de9c473632a2Bill Wendling    // LSDA encoding: This must match the encoding used in EmitEHFrame ()
51689ee706b6b64aaef7227e2e232b4de9c473632a2Bill Wendling    if (PointerSize == 4)
51789ee706b6b64aaef7227e2e232b4de9c473632a2Bill Wendling      JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
51889ee706b6b64aaef7227e2e232b4de9c473632a2Bill Wendling    else
51989ee706b6b64aaef7227e2e232b4de9c473632a2Bill Wendling      JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8);
520a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
521afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  } else {
522a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitULEB128Bytes(1);
523a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
524afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
525afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
5262d28617de2b0b731c08d1af9e830f31e14ac75b4Evan Cheng  EmitFrameMoves(0, MAI->getInitialFrameState());
52701248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner
52801248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner  JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
52901248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner
53001248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner  JCE->emitInt32At((uintptr_t*)StartCommonPtr,
53101248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner                   (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
53201248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner                               FrameCommonBeginPtr));
533afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
534afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  return StartCommonPtr;
535afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray}
536afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
537afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
5385913e6c5dbaff88ac00d5e679382abc72323831aNicolas Geoffrayunsigned char*
5395913e6c5dbaff88ac00d5e679382abc72323831aNicolas GeoffrayJITDwarfEmitter::EmitEHFrame(const Function* Personality,
5405913e6c5dbaff88ac00d5e679382abc72323831aNicolas Geoffray                             unsigned char* StartCommonPtr,
5418bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach                             unsigned char* StartFunction,
5425913e6c5dbaff88ac00d5e679382abc72323831aNicolas Geoffray                             unsigned char* EndFunction,
5435913e6c5dbaff88ac00d5e679382abc72323831aNicolas Geoffray                             unsigned char* ExceptionTable) const {
544afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  unsigned PointerSize = TD->getPointerSize();
5458bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
546afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // EH frame header.
547a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue();
548a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->allocateSpace(4, 0);
549a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
550afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // FDE CIE Offset
551a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitInt32(FrameBeginPtr - StartCommonPtr);
552a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue());
553a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes  JCE->emitInt32(EndFunction - StartFunction);
554afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray
555afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // If there is a personality and landing pads then point to the language
556afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // specific data area in the exception table.
5579d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling  if (Personality) {
5589d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling    JCE->emitULEB128Bytes(PointerSize == 4 ? 4 : 8);
5598bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
5609d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling    if (PointerSize == 4) {
5619d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling      if (!MMI->getLandingPads().empty())
5629d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling        JCE->emitInt32(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
5639d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling      else
5649d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling        JCE->emitInt32((int)0);
565afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    } else {
5669d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling      if (!MMI->getLandingPads().empty())
5679d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling        JCE->emitInt64(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
5689d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling      else
5699d48b555e24842c9a180e22511a39bfe62dfd5bdBill Wendling        JCE->emitInt64((int)0);
570afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray    }
571afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  } else {
572a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitULEB128Bytes(0);
573afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
5748bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
575afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Indicate locations of function specific  callee saved registers in
576afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // frame.
577afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
57801248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner
57901248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner  JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
58001248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner
581afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Indicate the size of the table
58201248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner  JCE->emitInt32At((uintptr_t*)StartEHPtr,
58301248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner                   (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
58401248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner                               StartEHPtr));
58501248e671100fbd6eac6bc3646096dc75ec885d1Reid Kleckner
586afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  // Double zeroes for the unwind runtime
587afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  if (PointerSize == 8) {
588a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitInt64(0);
589a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitInt64(0);
590afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  } else {
591a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitInt32(0);
592a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes    JCE->emitInt32(0);
593afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  }
5948bf0ecdbf6d9a645638806441e1b65cb308cd3e7Jim Grosbach
595afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray  return StartEHPtr;
596afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray}
597