Enable exception handling int JIT
[oota-llvm.git] / lib / ExecutionEngine / JIT / JITDwarfEmitter.cpp
1 //===----- JITDwarfEmitter.cpp - Write dwarf tables into memory -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a JITDwarfEmitter object that is used by the JIT to
11 // write dwarf tables to memory.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "JIT.h"
16 #include "JITDwarfEmitter.h"
17 #include "llvm/Function.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/MachineCodeEmitter.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineLocation.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/ExecutionEngine/JITMemoryManager.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetFrameInfo.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetRegisterInfo.h"
31
32 using namespace llvm;
33
34 JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : Jit(theJit) {}
35
36
37 unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F, 
38                                                MachineCodeEmitter& mce,
39                                                unsigned char* StartFunction,
40                                                unsigned char* EndFunction) {
41   const TargetMachine& TM = F.getTarget();
42   TD = TM.getTargetData();
43   needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding();
44   stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
45   RI = TM.getRegisterInfo();
46   MCE = &mce;
47   
48   unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
49                                                      EndFunction);
50       
51   unsigned char* Result = 0;
52   unsigned char* EHFramePtr = 0;
53
54   const std::vector<Function *> Personalities = MMI->getPersonalities();
55   EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]);
56
57   Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr,
58                        StartFunction, EndFunction, ExceptionTable);
59   
60   return Result;
61 }
62
63
64 void JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
65                                      const std::vector<MachineMove> &Moves) {
66   unsigned PointerSize = TD->getPointerSize();
67   int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
68           PointerSize : -PointerSize;
69   bool IsLocal = BaseLabelPtr;
70
71   for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
72     const MachineMove &Move = Moves[i];
73     unsigned LabelID = Move.getLabelID();
74     
75     if (LabelID) {
76       LabelID = MMI->MappedLabel(LabelID);
77     
78       // Throw out move if the label is invalid.
79       if (!LabelID) continue;
80     }
81     
82     intptr_t LabelPtr = 0;
83     if (LabelID) LabelPtr = MCE->getLabelAddress(LabelID);
84
85     const MachineLocation &Dst = Move.getDestination();
86     const MachineLocation &Src = Move.getSource();
87     
88     // Advance row if new location.
89     if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) {
90       MCE->emitByte(dwarf::DW_CFA_advance_loc4);
91       if (PointerSize == 8) {
92         MCE->emitInt64(LabelPtr - BaseLabelPtr);
93       } else {
94         MCE->emitInt32(LabelPtr - BaseLabelPtr);
95       }
96       
97       BaseLabelPtr = LabelPtr;
98       IsLocal = true;
99     }
100     
101     // If advancing cfa.
102     if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
103       if (!Src.isRegister()) {
104         if (Src.getRegister() == MachineLocation::VirtualFP) {
105           MCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
106         } else {
107           MCE->emitByte(dwarf::DW_CFA_def_cfa);
108           MCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getRegister(), true));
109         }
110         
111         int Offset = -Src.getOffset();
112         
113         MCE->emitULEB128Bytes(Offset);
114       } else {
115         assert(0 && "Machine move no supported yet.");
116       }
117     } else if (Src.isRegister() &&
118       Src.getRegister() == MachineLocation::VirtualFP) {
119       if (Dst.isRegister()) {
120         MCE->emitByte(dwarf::DW_CFA_def_cfa_register);
121         MCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister(), true));
122       } else {
123         assert(0 && "Machine move no supported yet.");
124       }
125     } else {
126       unsigned Reg = RI->getDwarfRegNum(Src.getRegister(), true);
127       int Offset = Dst.getOffset() / stackGrowth;
128       
129       if (Offset < 0) {
130         MCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
131         MCE->emitULEB128Bytes(Reg);
132         MCE->emitSLEB128Bytes(Offset);
133       } else if (Reg < 64) {
134         MCE->emitByte(dwarf::DW_CFA_offset + Reg);
135         MCE->emitULEB128Bytes(Offset);
136       } else {
137         MCE->emitByte(dwarf::DW_CFA_offset_extended);
138         MCE->emitULEB128Bytes(Reg);
139         MCE->emitULEB128Bytes(Offset);
140       }
141     }
142   }
143 }
144
145 /// SharedTypeIds - How many leading type ids two landing pads have in common.
146 static unsigned SharedTypeIds(const LandingPadInfo *L,
147                               const LandingPadInfo *R) {
148   const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
149   unsigned LSize = LIds.size(), RSize = RIds.size();
150   unsigned MinSize = LSize < RSize ? LSize : RSize;
151   unsigned Count = 0;
152
153   for (; Count != MinSize; ++Count)
154     if (LIds[Count] != RIds[Count])
155       return Count;
156
157   return Count;
158 }
159
160
161 /// PadLT - Order landing pads lexicographically by type id.
162 static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
163   const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
164   unsigned LSize = LIds.size(), RSize = RIds.size();
165   unsigned MinSize = LSize < RSize ? LSize : RSize;
166
167   for (unsigned i = 0; i != MinSize; ++i)
168     if (LIds[i] != RIds[i])
169       return LIds[i] < RIds[i];
170
171   return LSize < RSize;
172 }
173
174 struct KeyInfo {
175   static inline unsigned getEmptyKey() { return -1U; }
176   static inline unsigned getTombstoneKey() { return -2U; }
177   static unsigned getHashValue(const unsigned &Key) { return Key; }
178   static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; }
179   static bool isPod() { return true; }
180 };
181
182 /// ActionEntry - Structure describing an entry in the actions table.
183 struct ActionEntry {
184   int ValueForTypeID; // The value to write - may not be equal to the type id.
185   int NextAction;
186   struct ActionEntry *Previous;
187 };
188
189 /// PadRange - Structure holding a try-range and the associated landing pad.
190 struct PadRange {
191   // The index of the landing pad.
192   unsigned PadIndex;
193   // The index of the begin and end labels in the landing pad's label lists.
194   unsigned RangeIndex;
195 };
196
197 typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
198
199 /// CallSiteEntry - Structure describing an entry in the call-site table.
200 struct CallSiteEntry {
201   unsigned BeginLabel; // zero indicates the start of the function.
202   unsigned EndLabel;   // zero indicates the end of the function.
203   unsigned PadLabel;   // zero indicates that there is no landing pad.
204   unsigned Action;
205 };
206
207 unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF,
208                                          unsigned char* StartFunction,
209                                          unsigned char* EndFunction) {
210   // Map all labels and get rid of any dead landing pads.
211   MMI->TidyLandingPads();
212
213   const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
214   const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
215   const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
216   if (PadInfos.empty()) return 0;
217
218   // Sort the landing pads in order of their type ids.  This is used to fold
219   // duplicate actions.
220   SmallVector<const LandingPadInfo *, 64> LandingPads;
221   LandingPads.reserve(PadInfos.size());
222   for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
223     LandingPads.push_back(&PadInfos[i]);
224   std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
225
226   // Negative type ids index into FilterIds, positive type ids index into
227   // TypeInfos.  The value written for a positive type id is just the type
228   // id itself.  For a negative type id, however, the value written is the
229   // (negative) byte offset of the corresponding FilterIds entry.  The byte
230   // offset is usually equal to the type id, because the FilterIds entries
231   // are written using a variable width encoding which outputs one byte per
232   // entry as long as the value written is not too large, but can differ.
233   // This kind of complication does not occur for positive type ids because
234   // type infos are output using a fixed width encoding.
235   // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
236   SmallVector<int, 16> FilterOffsets;
237   FilterOffsets.reserve(FilterIds.size());
238   int Offset = -1;
239   for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
240     E = FilterIds.end(); I != E; ++I) {
241     FilterOffsets.push_back(Offset);
242     Offset -= AsmPrinter::SizeULEB128(*I);
243   }
244
245   // Compute the actions table and gather the first action index for each
246   // landing pad site.
247   SmallVector<ActionEntry, 32> Actions;
248   SmallVector<unsigned, 64> FirstActions;
249   FirstActions.reserve(LandingPads.size());
250
251   int FirstAction = 0;
252   unsigned SizeActions = 0;
253   for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
254     const LandingPadInfo *LP = LandingPads[i];
255     const std::vector<int> &TypeIds = LP->TypeIds;
256     const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
257     unsigned SizeSiteActions = 0;
258
259     if (NumShared < TypeIds.size()) {
260       unsigned SizeAction = 0;
261       ActionEntry *PrevAction = 0;
262
263       if (NumShared) {
264         const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
265         assert(Actions.size());
266         PrevAction = &Actions.back();
267         SizeAction = AsmPrinter::SizeSLEB128(PrevAction->NextAction) +
268           AsmPrinter::SizeSLEB128(PrevAction->ValueForTypeID);
269         for (unsigned j = NumShared; j != SizePrevIds; ++j) {
270           SizeAction -= AsmPrinter::SizeSLEB128(PrevAction->ValueForTypeID);
271           SizeAction += -PrevAction->NextAction;
272           PrevAction = PrevAction->Previous;
273         }
274       }
275
276       // Compute the actions.
277       for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
278         int TypeID = TypeIds[I];
279         assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
280         int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
281         unsigned SizeTypeID = AsmPrinter::SizeSLEB128(ValueForTypeID);
282
283         int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
284         SizeAction = SizeTypeID + AsmPrinter::SizeSLEB128(NextAction);
285         SizeSiteActions += SizeAction;
286
287         ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
288         Actions.push_back(Action);
289
290         PrevAction = &Actions.back();
291       }
292
293       // Record the first action of the landing pad site.
294       FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
295     } // else identical - re-use previous FirstAction
296
297     FirstActions.push_back(FirstAction);
298
299     // Compute this sites contribution to size.
300     SizeActions += SizeSiteActions;
301   }
302
303   // Compute the call-site table.  Entries must be ordered by address.
304   SmallVector<CallSiteEntry, 64> CallSites;
305
306   RangeMapType PadMap;
307   for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
308     const LandingPadInfo *LandingPad = LandingPads[i];
309     for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
310       unsigned BeginLabel = LandingPad->BeginLabels[j];
311       assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
312       PadRange P = { i, j };
313       PadMap[BeginLabel] = P;
314     }
315   }
316
317   bool MayThrow = false;
318   unsigned LastLabel = 0;
319   for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
320         I != E; ++I) {
321     for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
322           MI != E; ++MI) {
323       if (MI->getOpcode() != TargetInstrInfo::LABEL) {
324         MayThrow |= MI->getDesc().isCall();
325         continue;
326       }
327
328       unsigned BeginLabel = MI->getOperand(0).getImm();
329       assert(BeginLabel && "Invalid label!");
330
331       if (BeginLabel == LastLabel)
332         MayThrow = false;
333
334       RangeMapType::iterator L = PadMap.find(BeginLabel);
335
336       if (L == PadMap.end())
337         continue;
338
339       PadRange P = L->second;
340       const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
341
342       assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
343               "Inconsistent landing pad map!");
344
345       // If some instruction between the previous try-range and this one may
346       // throw, create a call-site entry with no landing pad for the region
347       // between the try-ranges.
348       if (MayThrow) {
349         CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
350         CallSites.push_back(Site);
351       }
352
353       LastLabel = LandingPad->EndLabels[P.RangeIndex];
354       CallSiteEntry Site = {BeginLabel, LastLabel,
355         LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
356
357       assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
358               "Invalid landing pad!");
359
360       // Try to merge with the previous call-site.
361       if (CallSites.size()) {
362         CallSiteEntry &Prev = CallSites[CallSites.size()-1];
363         if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
364           // Extend the range of the previous entry.
365           Prev.EndLabel = Site.EndLabel;
366           continue;
367         }
368       }
369
370       // Otherwise, create a new call-site.
371       CallSites.push_back(Site);
372     }
373   }
374   // If some instruction between the previous try-range and the end of the
375   // function may throw, create a call-site entry with no landing pad for the
376   // region following the try-range.
377   if (MayThrow) {
378     CallSiteEntry Site = {LastLabel, 0, 0, 0};
379     CallSites.push_back(Site);
380   }
381
382   // Final tallies.
383   unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
384                                             sizeof(int32_t) + // Site length.
385                                             sizeof(int32_t)); // Landing pad.
386   for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
387     SizeSites += AsmPrinter::SizeULEB128(CallSites[i].Action);
388
389   unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
390
391   unsigned TypeOffset = sizeof(int8_t) + // Call site format
392                         // Call-site table length
393                         AsmPrinter::SizeULEB128(SizeSites) + 
394                         SizeSites + SizeActions + SizeTypes;
395
396   unsigned TotalSize = sizeof(int8_t) + // LPStart format
397                        sizeof(int8_t) + // TType format
398                        AsmPrinter::SizeULEB128(TypeOffset) + // TType base offset
399                        TypeOffset;
400
401   unsigned SizeAlign = (4 - TotalSize) & 3;
402
403   // Begin the exception table.
404   MCE->emitAlignment(4);
405   for (unsigned i = 0; i != SizeAlign; ++i) {
406     MCE->emitByte(0);
407     // Asm->EOL("Padding");
408   }
409   
410   unsigned char* DwarfExceptionTable = (unsigned char*)MCE->getCurrentPCValue();
411
412   // Emit the header.
413   MCE->emitByte(dwarf::DW_EH_PE_omit);
414   // Asm->EOL("LPStart format (DW_EH_PE_omit)");
415   MCE->emitByte(dwarf::DW_EH_PE_absptr);
416   // Asm->EOL("TType format (DW_EH_PE_absptr)");
417   MCE->emitULEB128Bytes(TypeOffset);
418   // Asm->EOL("TType base offset");
419   MCE->emitByte(dwarf::DW_EH_PE_udata4);
420   // Asm->EOL("Call site format (DW_EH_PE_udata4)");
421   MCE->emitULEB128Bytes(SizeSites);
422   // Asm->EOL("Call-site table length");
423
424   // Emit the landing pad site information.
425   for (unsigned i = 0; i < CallSites.size(); ++i) {
426     CallSiteEntry &S = CallSites[i];
427     intptr_t BeginLabelPtr = 0;
428     intptr_t EndLabelPtr = 0;
429
430     if (!S.BeginLabel) {
431       BeginLabelPtr = (intptr_t)StartFunction;
432       if (TD->getPointerSize() == sizeof(int32_t))
433         MCE->emitInt32(0);
434       else
435         MCE->emitInt64(0);
436     } else {
437       BeginLabelPtr = MCE->getLabelAddress(S.BeginLabel);
438       if (TD->getPointerSize() == sizeof(int32_t))
439         MCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
440       else
441         MCE->emitInt64(BeginLabelPtr - (intptr_t)StartFunction);
442     }
443
444     // Asm->EOL("Region start");
445
446     if (!S.EndLabel) {
447       EndLabelPtr = (intptr_t)EndFunction;
448       if (TD->getPointerSize() == sizeof(int32_t))
449         MCE->emitInt32((intptr_t)EndFunction - BeginLabelPtr);
450       else
451         MCE->emitInt64((intptr_t)EndFunction - BeginLabelPtr);
452     } else {
453       EndLabelPtr = MCE->getLabelAddress(S.EndLabel);
454       if (TD->getPointerSize() == sizeof(int32_t))
455         MCE->emitInt32(EndLabelPtr - BeginLabelPtr);
456       else
457         MCE->emitInt64(EndLabelPtr - BeginLabelPtr);
458     }
459     //Asm->EOL("Region length");
460
461     if (!S.PadLabel) {
462       if (TD->getPointerSize() == sizeof(int32_t))
463         MCE->emitInt32(0);
464       else
465         MCE->emitInt64(0);
466     } else {
467       unsigned PadLabelPtr = MCE->getLabelAddress(S.PadLabel);
468       if (TD->getPointerSize() == sizeof(int32_t))
469         MCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
470       else
471         MCE->emitInt64(PadLabelPtr - (intptr_t)StartFunction);
472     }
473     // Asm->EOL("Landing pad");
474
475     MCE->emitULEB128Bytes(S.Action);
476     // Asm->EOL("Action");
477   }
478
479   // Emit the actions.
480   for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
481     ActionEntry &Action = Actions[I];
482
483     MCE->emitSLEB128Bytes(Action.ValueForTypeID);
484     //Asm->EOL("TypeInfo index");
485     MCE->emitSLEB128Bytes(Action.NextAction);
486     //Asm->EOL("Next action");
487   }
488
489   // Emit the type ids.
490   for (unsigned M = TypeInfos.size(); M; --M) {
491     GlobalVariable *GV = TypeInfos[M - 1];
492     
493     if (GV) {
494       if (TD->getPointerSize() == sizeof(int32_t)) {
495         MCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
496       } else {
497         MCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
498       }
499     } else {
500       if (TD->getPointerSize() == sizeof(int32_t))
501         MCE->emitInt32(0);
502       else
503         MCE->emitInt64(0);
504     }
505     // Asm->EOL("TypeInfo");
506   }
507
508   // Emit the filter typeids.
509   for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
510     unsigned TypeID = FilterIds[j];
511     MCE->emitULEB128Bytes(TypeID);
512     //Asm->EOL("Filter TypeInfo index");
513   }
514   
515   MCE->emitAlignment(4);
516
517   return DwarfExceptionTable;
518 }
519
520 unsigned char* JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) {
521   unsigned PointerSize = TD->getPointerSize();
522   int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
523           PointerSize : -PointerSize;
524   
525   unsigned char* StartCommonPtr = (unsigned char*)MCE->getCurrentPCValue();
526   // EH Common Frame header
527   MCE->allocateSpace(PointerSize, 0);
528   unsigned char* FrameCommonBeginPtr = (unsigned char*)MCE->getCurrentPCValue();
529   MCE->emitInt32((int)0);
530   MCE->emitByte(dwarf::DW_CIE_VERSION);
531   MCE->emitString(Personality ? "zPLR" : "zR");
532   MCE->emitULEB128Bytes(1);
533   MCE->emitSLEB128Bytes(stackGrowth);
534   MCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
535   
536   if (Personality) {
537     MCE->emitULEB128Bytes(7);
538     
539     if (needsIndirectEncoding)
540       MCE->emitByte(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4 | 
541                dwarf::DW_EH_PE_indirect);
542     else
543       MCE->emitByte(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
544
545     if (PointerSize == 8)
546       MCE->emitInt64((intptr_t)Jit.getPointerToGlobal(Personality) - 
547                 MCE->getCurrentPCValue());
548     else
549       MCE->emitInt32((intptr_t)Jit.getPointerToGlobal(Personality) - 
550                 MCE->getCurrentPCValue());
551     
552     MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel);
553     MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel);
554       
555   } else {
556     MCE->emitULEB128Bytes(1);
557     MCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel);
558   }
559
560   std::vector<MachineMove> Moves;
561   RI->getInitialFrameState(Moves);
562   EmitFrameMoves(0, Moves);
563   MCE->emitAlignment(4);
564   
565   MCE->emitAt((uintptr_t*)StartCommonPtr, 
566               (uintptr_t)((unsigned char*)MCE->getCurrentPCValue() - 
567                           FrameCommonBeginPtr));
568
569   return StartCommonPtr;
570 }
571
572
573 unsigned char* JITDwarfEmitter::EmitEHFrame(const Function* Personality,
574                                             unsigned char* StartCommonPtr,
575                                             unsigned char* StartFunction, 
576                                             unsigned char* EndFunction,
577                                             unsigned char* ExceptionTable) {
578   unsigned PointerSize = TD->getPointerSize();
579   
580   // EH frame header.
581   unsigned char* StartEHPtr = (unsigned char*)MCE->getCurrentPCValue();
582   MCE->allocateSpace(PointerSize, 0);
583   unsigned char* FrameBeginPtr = (unsigned char*)MCE->getCurrentPCValue();
584   // FDE CIE Offset
585   if (PointerSize == 8) {
586     MCE->emitInt64(FrameBeginPtr - StartCommonPtr);
587     MCE->emitInt64(StartFunction - (unsigned char*)MCE->getCurrentPCValue());
588     MCE->emitInt64(EndFunction - StartFunction);
589   } else {
590     MCE->emitInt32(FrameBeginPtr - StartCommonPtr);
591     MCE->emitInt32(StartFunction - (unsigned char*)MCE->getCurrentPCValue());
592     MCE->emitInt32(EndFunction - StartFunction);
593   }
594
595   // If there is a personality and landing pads then point to the language
596   // specific data area in the exception table.
597   if (MMI->getPersonalityIndex()) {
598     MCE->emitULEB128Bytes(4);
599         
600     if (!MMI->getLandingPads().empty()) {
601       if (PointerSize == 8)
602         MCE->emitInt64(ExceptionTable - (unsigned char*)MCE->getCurrentPCValue());
603       else
604         MCE->emitInt32(ExceptionTable - (unsigned char*)MCE->getCurrentPCValue());
605     } else if (PointerSize == 8) {
606       MCE->emitInt64((int)0);
607     } else {
608       MCE->emitInt32((int)0);
609     }
610   } else {
611     MCE->emitULEB128Bytes(0);
612   }
613       
614   // Indicate locations of function specific  callee saved registers in
615   // frame.
616   EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
617       
618   MCE->emitAlignment(4);
619   
620   // Indicate the size of the table
621   MCE->emitAt((uintptr_t*)StartEHPtr, 
622               (uintptr_t)((unsigned char*)MCE->getCurrentPCValue() - 
623                           StartEHPtr));
624   
625   // Double zeroes for the unwind runtime
626   if (PointerSize == 8) {
627     MCE->emitInt64(0);
628     MCE->emitInt64(0);
629   } else {
630     MCE->emitInt32(0);
631     MCE->emitInt32(0);
632   }
633
634   
635   return StartEHPtr;
636 }