blob: 691c6fc620a6c84a70bbc0ae8031a785fecf6d74 [file] [log] [blame]
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +00001//===----- 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
32using namespace llvm;
33
34JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : Jit(theJit) {}
35
36
37unsigned 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
64void 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.
146static 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.
162static 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
174struct 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.
183struct 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.
190struct 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
197typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType;
198
199/// CallSiteEntry - Structure describing an entry in the call-site table.
200struct 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
207unsigned 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
520unsigned 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
573unsigned 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}
Nicolas Geoffraydc17ab22008-04-18 20:59:31 +0000637
638unsigned JITDwarfEmitter::GetDwarfTableSize(MachineFunction& F,
639 MachineCodeEmitter& mce,
640 unsigned char* StartFunction,
641 unsigned char* EndFunction) {
642 const TargetMachine& TM = F.getTarget();
643 TD = TM.getTargetData();
644 needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding();
645 stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection();
646 RI = TM.getRegisterInfo();
647 MCE = &mce;
648 unsigned FinalSize = 0;
649
650 FinalSize += GetExceptionTableSize(&F);
651
652 const std::vector<Function *> Personalities = MMI->getPersonalities();
653 FinalSize += GetCommonEHFrameSize(Personalities[MMI->getPersonalityIndex()]);
654
655 FinalSize += GetEHFrameSize(Personalities[MMI->getPersonalityIndex()], StartFunction);
656
657 return FinalSize;
658}
659
660/// AddAlignment - Add the specified alignment.
661static void AddAlignment(unsigned& FinalSize, unsigned Alignment) {
662 if (Alignment == 0) Alignment = 1;
663 FinalSize = (FinalSize + Alignment - 1) & ~(Alignment - 1);
664}
665
666
667/// SizeULEB128Bytes - Gives the size of the ULEB128.
668static unsigned SizeULEB128Bytes(unsigned Value) {
669 unsigned FinalSize = 0;
670 do {
671 Value >>= 7;
672 ++FinalSize;
673 } while (Value);
674 return FinalSize;
675}
676
677/// SizeSLEB128Bytes - Gives the size of the SLEB128.
678static unsigned SizeSLEB128Bytes(int Value) {
679 int Sign = Value >> (8 * sizeof(Value) - 1);
680 bool IsMore;
681 unsigned FinalSize = 0;
682
683 do {
684 unsigned char Byte = Value & 0x7f;
685 Value >>= 7;
686 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
687 ++FinalSize;
688 } while (IsMore);
689
690 return FinalSize;
691}
692
693/// SizeString - Gives the size of the string.
694static unsigned SizeString(const std::string &String) {
695 return String.size() + 1;
696}
697
698unsigned JITDwarfEmitter::GetEHFrameSize(const Function* Personality,
699 unsigned char* StartFunction) {
700 unsigned PointerSize = TD->getPointerSize();
701 unsigned FinalSize = 0;
702 // EH frame header.
703 FinalSize += PointerSize;
704 // FDE CIE Offset
705 FinalSize += 3 * PointerSize;
706 // If there is a personality and landing pads then point to the language
707 // specific data area in the exception table.
708 if (MMI->getPersonalityIndex()) {
709 FinalSize += SizeULEB128Bytes(4);
710 FinalSize += PointerSize;
711 } else {
712 FinalSize += SizeULEB128Bytes(0);
713 }
714
715 // Indicate locations of function specific callee saved registers in
716 // frame.
717 FinalSize += GetFrameMovesSize((intptr_t)StartFunction, MMI->getFrameMoves());
718
719 AddAlignment(FinalSize, 4);
720
721 // Double zeroes for the unwind runtime
722 FinalSize += 2 * PointerSize;
723
724 return FinalSize;
725}
726
727unsigned JITDwarfEmitter::GetCommonEHFrameSize(const Function* Personality) {
728
729 unsigned PointerSize = TD->getPointerSize();
730 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
731 PointerSize : -PointerSize;
732 unsigned FinalSize = 0;
733 // EH Common Frame header
734 FinalSize += PointerSize;
735 FinalSize += 4;
736 FinalSize += 1;
737 FinalSize += SizeString(Personality ? "zPLR" : "zR");
738 FinalSize += SizeULEB128Bytes(1);
739 FinalSize += SizeSLEB128Bytes(stackGrowth);
740 FinalSize += 1;
741
742 if (Personality) {
743 FinalSize += SizeULEB128Bytes(7);
744
745 // Encoding
746 FinalSize+= 1;
747 //Personality
748 FinalSize += PointerSize;
749
750 FinalSize += SizeULEB128Bytes(dwarf::DW_EH_PE_pcrel);
751 FinalSize += SizeULEB128Bytes(dwarf::DW_EH_PE_pcrel);
752
753 } else {
754 FinalSize += SizeULEB128Bytes(1);
755 FinalSize += SizeULEB128Bytes(dwarf::DW_EH_PE_pcrel);
756 }
757
758 std::vector<MachineMove> Moves;
759 RI->getInitialFrameState(Moves);
760 FinalSize += GetFrameMovesSize(0, Moves);
761 AddAlignment(FinalSize, 4);
762 return FinalSize;
763}
764
765unsigned
766JITDwarfEmitter::GetFrameMovesSize(intptr_t BaseLabelPtr,
767 const std::vector<MachineMove> &Moves) {
768 unsigned PointerSize = TD->getPointerSize();
769 int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ?
770 PointerSize : -PointerSize;
771 bool IsLocal = BaseLabelPtr;
772 unsigned FinalSize = 0;
773
774 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
775 const MachineMove &Move = Moves[i];
776 unsigned LabelID = Move.getLabelID();
777
778 if (LabelID) {
779 LabelID = MMI->MappedLabel(LabelID);
780
781 // Throw out move if the label is invalid.
782 if (!LabelID) continue;
783 }
784
785 intptr_t LabelPtr = 0;
786 if (LabelID) LabelPtr = MCE->getLabelAddress(LabelID);
787
788 const MachineLocation &Dst = Move.getDestination();
789 const MachineLocation &Src = Move.getSource();
790
791 // Advance row if new location.
792 if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) {
793 FinalSize++;
794 FinalSize += PointerSize;
795 BaseLabelPtr = LabelPtr;
796 IsLocal = true;
797 }
798
799 // If advancing cfa.
800 if (Dst.isRegister() && Dst.getRegister() == MachineLocation::VirtualFP) {
801 if (!Src.isRegister()) {
802 if (Src.getRegister() == MachineLocation::VirtualFP) {
803 ++FinalSize;
804 } else {
805 ++FinalSize;
806 FinalSize +=
807 SizeULEB128Bytes(RI->getDwarfRegNum(Src.getRegister(), true));
808 }
809
810 int Offset = -Src.getOffset();
811
812 FinalSize += SizeULEB128Bytes(Offset);
813 } else {
814 assert(0 && "Machine move no supported yet.");
815 }
816 } else if (Src.isRegister() &&
817 Src.getRegister() == MachineLocation::VirtualFP) {
818 if (Dst.isRegister()) {
819 ++FinalSize;
820 FinalSize +=
821 SizeULEB128Bytes(RI->getDwarfRegNum(Dst.getRegister(), true));
822 } else {
823 assert(0 && "Machine move no supported yet.");
824 }
825 } else {
826 unsigned Reg = RI->getDwarfRegNum(Src.getRegister(), true);
827 int Offset = Dst.getOffset() / stackGrowth;
828
829 if (Offset < 0) {
830 ++FinalSize;
831 FinalSize += SizeULEB128Bytes(Reg);
832 FinalSize += SizeSLEB128Bytes(Offset);
833 } else if (Reg < 64) {
834 ++FinalSize;
835 FinalSize += SizeULEB128Bytes(Offset);
836 } else {
837 ++FinalSize;
838 FinalSize += SizeULEB128Bytes(Reg);
839 FinalSize += SizeULEB128Bytes(Offset);
840 }
841 }
842 }
843 return FinalSize;
844}
845
846unsigned JITDwarfEmitter::GetExceptionTableSize(MachineFunction* MF) {
847 unsigned FinalSize = 0;
848
849 // Map all labels and get rid of any dead landing pads.
850 MMI->TidyLandingPads();
851
852 const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
853 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
854 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
855 if (PadInfos.empty()) return 0;
856
857 // Sort the landing pads in order of their type ids. This is used to fold
858 // duplicate actions.
859 SmallVector<const LandingPadInfo *, 64> LandingPads;
860 LandingPads.reserve(PadInfos.size());
861 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
862 LandingPads.push_back(&PadInfos[i]);
863 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
864
865 // Negative type ids index into FilterIds, positive type ids index into
866 // TypeInfos. The value written for a positive type id is just the type
867 // id itself. For a negative type id, however, the value written is the
868 // (negative) byte offset of the corresponding FilterIds entry. The byte
869 // offset is usually equal to the type id, because the FilterIds entries
870 // are written using a variable width encoding which outputs one byte per
871 // entry as long as the value written is not too large, but can differ.
872 // This kind of complication does not occur for positive type ids because
873 // type infos are output using a fixed width encoding.
874 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
875 SmallVector<int, 16> FilterOffsets;
876 FilterOffsets.reserve(FilterIds.size());
877 int Offset = -1;
878 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
879 E = FilterIds.end(); I != E; ++I) {
880 FilterOffsets.push_back(Offset);
881 Offset -= AsmPrinter::SizeULEB128(*I);
882 }
883
884 // Compute the actions table and gather the first action index for each
885 // landing pad site.
886 SmallVector<ActionEntry, 32> Actions;
887 SmallVector<unsigned, 64> FirstActions;
888 FirstActions.reserve(LandingPads.size());
889
890 int FirstAction = 0;
891 unsigned SizeActions = 0;
892 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
893 const LandingPadInfo *LP = LandingPads[i];
894 const std::vector<int> &TypeIds = LP->TypeIds;
895 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
896 unsigned SizeSiteActions = 0;
897
898 if (NumShared < TypeIds.size()) {
899 unsigned SizeAction = 0;
900 ActionEntry *PrevAction = 0;
901
902 if (NumShared) {
903 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
904 assert(Actions.size());
905 PrevAction = &Actions.back();
906 SizeAction = AsmPrinter::SizeSLEB128(PrevAction->NextAction) +
907 AsmPrinter::SizeSLEB128(PrevAction->ValueForTypeID);
908 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
909 SizeAction -= AsmPrinter::SizeSLEB128(PrevAction->ValueForTypeID);
910 SizeAction += -PrevAction->NextAction;
911 PrevAction = PrevAction->Previous;
912 }
913 }
914
915 // Compute the actions.
916 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
917 int TypeID = TypeIds[I];
918 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
919 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
920 unsigned SizeTypeID = AsmPrinter::SizeSLEB128(ValueForTypeID);
921
922 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
923 SizeAction = SizeTypeID + AsmPrinter::SizeSLEB128(NextAction);
924 SizeSiteActions += SizeAction;
925
926 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
927 Actions.push_back(Action);
928
929 PrevAction = &Actions.back();
930 }
931
932 // Record the first action of the landing pad site.
933 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
934 } // else identical - re-use previous FirstAction
935
936 FirstActions.push_back(FirstAction);
937
938 // Compute this sites contribution to size.
939 SizeActions += SizeSiteActions;
940 }
941
942 // Compute the call-site table. Entries must be ordered by address.
943 SmallVector<CallSiteEntry, 64> CallSites;
944
945 RangeMapType PadMap;
946 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
947 const LandingPadInfo *LandingPad = LandingPads[i];
948 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
949 unsigned BeginLabel = LandingPad->BeginLabels[j];
950 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
951 PadRange P = { i, j };
952 PadMap[BeginLabel] = P;
953 }
954 }
955
956 bool MayThrow = false;
957 unsigned LastLabel = 0;
958 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
959 I != E; ++I) {
960 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
961 MI != E; ++MI) {
962 if (MI->getOpcode() != TargetInstrInfo::LABEL) {
963 MayThrow |= MI->getDesc().isCall();
964 continue;
965 }
966
967 unsigned BeginLabel = MI->getOperand(0).getImm();
968 assert(BeginLabel && "Invalid label!");
969
970 if (BeginLabel == LastLabel)
971 MayThrow = false;
972
973 RangeMapType::iterator L = PadMap.find(BeginLabel);
974
975 if (L == PadMap.end())
976 continue;
977
978 PadRange P = L->second;
979 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
980
981 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
982 "Inconsistent landing pad map!");
983
984 // If some instruction between the previous try-range and this one may
985 // throw, create a call-site entry with no landing pad for the region
986 // between the try-ranges.
987 if (MayThrow) {
988 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
989 CallSites.push_back(Site);
990 }
991
992 LastLabel = LandingPad->EndLabels[P.RangeIndex];
993 CallSiteEntry Site = {BeginLabel, LastLabel,
994 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
995
996 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
997 "Invalid landing pad!");
998
999 // Try to merge with the previous call-site.
1000 if (CallSites.size()) {
1001 CallSiteEntry &Prev = CallSites[CallSites.size()-1];
1002 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
1003 // Extend the range of the previous entry.
1004 Prev.EndLabel = Site.EndLabel;
1005 continue;
1006 }
1007 }
1008
1009 // Otherwise, create a new call-site.
1010 CallSites.push_back(Site);
1011 }
1012 }
1013 // If some instruction between the previous try-range and the end of the
1014 // function may throw, create a call-site entry with no landing pad for the
1015 // region following the try-range.
1016 if (MayThrow) {
1017 CallSiteEntry Site = {LastLabel, 0, 0, 0};
1018 CallSites.push_back(Site);
1019 }
1020
1021 // Final tallies.
1022 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
1023 sizeof(int32_t) + // Site length.
1024 sizeof(int32_t)); // Landing pad.
1025 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
1026 SizeSites += AsmPrinter::SizeULEB128(CallSites[i].Action);
1027
1028 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize();
1029
1030 unsigned TypeOffset = sizeof(int8_t) + // Call site format
1031 // Call-site table length
1032 AsmPrinter::SizeULEB128(SizeSites) +
1033 SizeSites + SizeActions + SizeTypes;
1034
1035 unsigned TotalSize = sizeof(int8_t) + // LPStart format
1036 sizeof(int8_t) + // TType format
1037 AsmPrinter::SizeULEB128(TypeOffset) + // TType base offset
1038 TypeOffset;
1039
1040 unsigned SizeAlign = (4 - TotalSize) & 3;
1041
1042 // Begin the exception table.
1043 AddAlignment(FinalSize, 4);
1044 for (unsigned i = 0; i != SizeAlign; ++i) {
1045 ++FinalSize;
1046 }
1047
1048 unsigned PointerSize = TD->getPointerSize();
1049
1050 // Emit the header.
1051 ++FinalSize;
1052 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
1053 ++FinalSize;
1054 // Asm->EOL("TType format (DW_EH_PE_absptr)");
1055 ++FinalSize;
1056 // Asm->EOL("TType base offset");
1057 ++FinalSize;
1058 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
1059 ++FinalSize;
1060 // Asm->EOL("Call-site table length");
1061
1062 // Emit the landing pad site information.
1063 for (unsigned i = 0; i < CallSites.size(); ++i) {
1064 CallSiteEntry &S = CallSites[i];
1065
1066 // Asm->EOL("Region start");
1067 FinalSize += PointerSize;
1068
1069 //Asm->EOL("Region length");
1070 FinalSize += PointerSize;
1071
1072 // Asm->EOL("Landing pad");
1073 FinalSize += PointerSize;
1074
1075 FinalSize += SizeULEB128Bytes(S.Action);
1076 // Asm->EOL("Action");
1077 }
1078
1079 // Emit the actions.
1080 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
1081 ActionEntry &Action = Actions[I];
1082
1083 //Asm->EOL("TypeInfo index");
1084 FinalSize += SizeSLEB128Bytes(Action.ValueForTypeID);
1085 //Asm->EOL("Next action");
1086 FinalSize += SizeSLEB128Bytes(Action.NextAction);
1087 }
1088
1089 // Emit the type ids.
1090 for (unsigned M = TypeInfos.size(); M; --M) {
1091 // Asm->EOL("TypeInfo");
1092 FinalSize += PointerSize;
1093 }
1094
1095 // Emit the filter typeids.
1096 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
1097 unsigned TypeID = FilterIds[j];
1098 FinalSize += SizeULEB128Bytes(TypeID);
1099 //Asm->EOL("Filter TypeInfo index");
1100 }
1101
1102 AddAlignment(FinalSize, 4);
1103
1104 return FinalSize;
1105}