blob: bcd5b263654af2eb2336cd7f7f7c45cc08458287 [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"
Micah Villmow2c39b152012-10-15 16:24:29 +000017#include "llvm/DerivedTypes.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000018#include "llvm/Function.h"
Micah Villmow2c39b152012-10-15 16:24:29 +000019#include "llvm/GlobalVariable.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000020#include "llvm/ADT/DenseMap.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000021#include "llvm/CodeGen/JITCodeEmitter.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000022#include "llvm/CodeGen/MachineFunction.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
24#include "llvm/ExecutionEngine/JITMemoryManager.h"
Evan Cheng2d286172011-07-18 22:29:13 +000025#include "llvm/MC/MachineLocation.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000026#include "llvm/MC/MCAsmInfo.h"
Chris Lattner16112732010-03-14 01:41:15 +000027#include "llvm/MC/MCSymbol.h"
Evan Cheng2d286172011-07-18 22:29:13 +000028#include "llvm/Support/ErrorHandling.h"
Micah Villmow3574eca2012-10-08 16:38:25 +000029#include "llvm/DataLayout.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000030#include "llvm/Target/TargetInstrInfo.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000031#include "llvm/Target/TargetFrameLowering.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000032#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetRegisterInfo.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000034using namespace llvm;
35
Daniel Dunbar003de662009-09-21 05:58:35 +000036JITDwarfEmitter::JITDwarfEmitter(JIT& theJit) : MMI(0), Jit(theJit) {}
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000037
38
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +000039unsigned char* JITDwarfEmitter::EmitDwarfTable(MachineFunction& F,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000040 JITCodeEmitter& jce,
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000041 unsigned char* StartFunction,
Reid Kleckner27632172009-09-20 23:52:43 +000042 unsigned char* EndFunction,
43 unsigned char* &EHFramePtr) {
Daniel Dunbar003de662009-09-21 05:58:35 +000044 assert(MMI && "MachineModuleInfo not registered!");
45
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000046 const TargetMachine& TM = F.getTarget();
Micah Villmow3574eca2012-10-08 16:38:25 +000047 TD = TM.getDataLayout();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000048 stackGrowthDirection = TM.getFrameLowering()->getStackGrowthDirection();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000049 RI = TM.getRegisterInfo();
Evan Cheng2d286172011-07-18 22:29:13 +000050 MAI = TM.getMCAsmInfo();
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000051 JCE = &jce;
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +000052
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000053 unsigned char* ExceptionTable = EmitExceptionTable(&F, StartFunction,
54 EndFunction);
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +000055
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000056 unsigned char* Result = 0;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000057
Dan Gohman46510a72010-04-15 01:51:59 +000058 const std::vector<const Function *> Personalities = MMI->getPersonalities();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000059 EHFramePtr = EmitCommonEHFrame(Personalities[MMI->getPersonalityIndex()]);
60
61 Result = EmitEHFrame(Personalities[MMI->getPersonalityIndex()], EHFramePtr,
62 StartFunction, EndFunction, ExceptionTable);
Bill Wendling9d48b552009-09-09 00:11:02 +000063
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000064 return Result;
65}
66
67
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +000068void
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +000069JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr,
70 const std::vector<MachineMove> &Moves) const {
Micah Villmow2c39b152012-10-15 16:24:29 +000071 unsigned PointerSize = TD->getPointerSize(0);
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000072 int stackGrowth = stackGrowthDirection == TargetFrameLowering::StackGrowsUp ?
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000073 PointerSize : -PointerSize;
Chris Lattner2e9919a2010-03-14 08:12:40 +000074 MCSymbol *BaseLabel = 0;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000075
76 for (unsigned i = 0, N = Moves.size(); i < N; ++i) {
77 const MachineMove &Move = Moves[i];
Chris Lattner2e9919a2010-03-14 08:12:40 +000078 MCSymbol *Label = Move.getLabel();
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +000079
Chris Lattner16112732010-03-14 01:41:15 +000080 // Throw out move if the label is invalid.
Bill Wendling091fc4b2010-04-18 00:56:05 +000081 if (Label && (*JCE->getLabelLocations())[Label] == 0)
Chris Lattner16112732010-03-14 01:41:15 +000082 continue;
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +000083
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000084 intptr_t LabelPtr = 0;
Chris Lattner2e9919a2010-03-14 08:12:40 +000085 if (Label) LabelPtr = JCE->getLabelAddress(Label);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000086
87 const MachineLocation &Dst = Move.getDestination();
88 const MachineLocation &Src = Move.getSource();
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +000089
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000090 // Advance row if new location.
Chris Lattner2e9919a2010-03-14 08:12:40 +000091 if (BaseLabelPtr && Label && BaseLabel != Label) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000092 JCE->emitByte(dwarf::DW_CFA_advance_loc4);
93 JCE->emitInt32(LabelPtr - BaseLabelPtr);
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +000094
95 BaseLabel = Label;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000096 BaseLabelPtr = LabelPtr;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000097 }
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +000098
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000099 // If advancing cfa.
Daniel Dunbar489032a2008-10-03 17:11:57 +0000100 if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) {
101 if (!Src.isReg()) {
102 if (Src.getReg() == MachineLocation::VirtualFP) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000103 JCE->emitByte(dwarf::DW_CFA_def_cfa_offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000104 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000105 JCE->emitByte(dwarf::DW_CFA_def_cfa);
106 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000107 }
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000108
Bill Wendling9d48b552009-09-09 00:11:02 +0000109 JCE->emitULEB128Bytes(-Src.getOffset());
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000110 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000111 llvm_unreachable("Machine move not supported yet.");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000112 }
Daniel Dunbar489032a2008-10-03 17:11:57 +0000113 } else if (Src.isReg() &&
114 Src.getReg() == MachineLocation::VirtualFP) {
115 if (Dst.isReg()) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000116 JCE->emitByte(dwarf::DW_CFA_def_cfa_register);
117 JCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000118 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000119 llvm_unreachable("Machine move not supported yet.");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000120 }
121 } else {
Daniel Dunbar489032a2008-10-03 17:11:57 +0000122 unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000123 int Offset = Dst.getOffset() / stackGrowth;
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000124
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000125 if (Offset < 0) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000126 JCE->emitByte(dwarf::DW_CFA_offset_extended_sf);
127 JCE->emitULEB128Bytes(Reg);
128 JCE->emitSLEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000129 } else if (Reg < 64) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000130 JCE->emitByte(dwarf::DW_CFA_offset + Reg);
131 JCE->emitULEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000132 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000133 JCE->emitByte(dwarf::DW_CFA_offset_extended);
134 JCE->emitULEB128Bytes(Reg);
135 JCE->emitULEB128Bytes(Offset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000136 }
137 }
138 }
139}
140
141/// SharedTypeIds - How many leading type ids two landing pads have in common.
142static unsigned SharedTypeIds(const LandingPadInfo *L,
143 const LandingPadInfo *R) {
144 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
145 unsigned LSize = LIds.size(), RSize = RIds.size();
146 unsigned MinSize = LSize < RSize ? LSize : RSize;
147 unsigned Count = 0;
148
149 for (; Count != MinSize; ++Count)
150 if (LIds[Count] != RIds[Count])
151 return Count;
152
153 return Count;
154}
155
156
157/// PadLT - Order landing pads lexicographically by type id.
158static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) {
159 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
160 unsigned LSize = LIds.size(), RSize = RIds.size();
161 unsigned MinSize = LSize < RSize ? LSize : RSize;
162
163 for (unsigned i = 0; i != MinSize; ++i)
164 if (LIds[i] != RIds[i])
165 return LIds[i] < RIds[i];
166
167 return LSize < RSize;
168}
169
Dan Gohman844731a2008-05-13 00:00:25 +0000170namespace {
171
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000172/// ActionEntry - Structure describing an entry in the actions table.
173struct ActionEntry {
174 int ValueForTypeID; // The value to write - may not be equal to the type id.
175 int NextAction;
176 struct ActionEntry *Previous;
177};
178
179/// PadRange - Structure holding a try-range and the associated landing pad.
180struct PadRange {
181 // The index of the landing pad.
182 unsigned PadIndex;
183 // The index of the begin and end labels in the landing pad's label lists.
184 unsigned RangeIndex;
185};
186
Chris Lattner16112732010-03-14 01:41:15 +0000187typedef DenseMap<MCSymbol*, PadRange> RangeMapType;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000188
189/// CallSiteEntry - Structure describing an entry in the call-site table.
190struct CallSiteEntry {
Chris Lattner16112732010-03-14 01:41:15 +0000191 MCSymbol *BeginLabel; // zero indicates the start of the function.
192 MCSymbol *EndLabel; // zero indicates the end of the function.
193 MCSymbol *PadLabel; // zero indicates that there is no landing pad.
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000194 unsigned Action;
195};
196
Dan Gohman844731a2008-05-13 00:00:25 +0000197}
198
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000199unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF,
200 unsigned char* StartFunction,
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000201 unsigned char* EndFunction) const {
Daniel Dunbar003de662009-09-21 05:58:35 +0000202 assert(MMI && "MachineModuleInfo not registered!");
203
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000204 // Map all labels and get rid of any dead landing pads.
Bill Wendling47639fc2010-04-16 08:46:10 +0000205 MMI->TidyLandingPads(JCE->getLabelLocations());
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000206
Dan Gohman46510a72010-04-15 01:51:59 +0000207 const std::vector<const GlobalVariable *> &TypeInfos = MMI->getTypeInfos();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000208 const std::vector<unsigned> &FilterIds = MMI->getFilterIds();
209 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads();
210 if (PadInfos.empty()) return 0;
211
212 // Sort the landing pads in order of their type ids. This is used to fold
213 // duplicate actions.
214 SmallVector<const LandingPadInfo *, 64> LandingPads;
215 LandingPads.reserve(PadInfos.size());
216 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i)
217 LandingPads.push_back(&PadInfos[i]);
218 std::sort(LandingPads.begin(), LandingPads.end(), PadLT);
219
220 // Negative type ids index into FilterIds, positive type ids index into
221 // TypeInfos. The value written for a positive type id is just the type
222 // id itself. For a negative type id, however, the value written is the
223 // (negative) byte offset of the corresponding FilterIds entry. The byte
224 // offset is usually equal to the type id, because the FilterIds entries
225 // are written using a variable width encoding which outputs one byte per
226 // entry as long as the value written is not too large, but can differ.
227 // This kind of complication does not occur for positive type ids because
228 // type infos are output using a fixed width encoding.
229 // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i].
230 SmallVector<int, 16> FilterOffsets;
231 FilterOffsets.reserve(FilterIds.size());
232 int Offset = -1;
233 for(std::vector<unsigned>::const_iterator I = FilterIds.begin(),
234 E = FilterIds.end(); I != E; ++I) {
235 FilterOffsets.push_back(Offset);
Chris Lattneraf76e592009-08-22 20:48:53 +0000236 Offset -= MCAsmInfo::getULEB128Size(*I);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000237 }
238
239 // Compute the actions table and gather the first action index for each
240 // landing pad site.
241 SmallVector<ActionEntry, 32> Actions;
242 SmallVector<unsigned, 64> FirstActions;
243 FirstActions.reserve(LandingPads.size());
244
245 int FirstAction = 0;
246 unsigned SizeActions = 0;
247 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
248 const LandingPadInfo *LP = LandingPads[i];
249 const std::vector<int> &TypeIds = LP->TypeIds;
250 const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0;
251 unsigned SizeSiteActions = 0;
252
253 if (NumShared < TypeIds.size()) {
254 unsigned SizeAction = 0;
255 ActionEntry *PrevAction = 0;
256
257 if (NumShared) {
258 const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size();
259 assert(Actions.size());
260 PrevAction = &Actions.back();
Chris Lattneraf76e592009-08-22 20:48:53 +0000261 SizeAction = MCAsmInfo::getSLEB128Size(PrevAction->NextAction) +
262 MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000263 for (unsigned j = NumShared; j != SizePrevIds; ++j) {
Chris Lattneraf76e592009-08-22 20:48:53 +0000264 SizeAction -= MCAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000265 SizeAction += -PrevAction->NextAction;
266 PrevAction = PrevAction->Previous;
267 }
268 }
269
270 // Compute the actions.
271 for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) {
272 int TypeID = TypeIds[I];
273 assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
274 int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID;
Chris Lattneraf76e592009-08-22 20:48:53 +0000275 unsigned SizeTypeID = MCAsmInfo::getSLEB128Size(ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000276
277 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0;
Chris Lattneraf76e592009-08-22 20:48:53 +0000278 SizeAction = SizeTypeID + MCAsmInfo::getSLEB128Size(NextAction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000279 SizeSiteActions += SizeAction;
280
281 ActionEntry Action = {ValueForTypeID, NextAction, PrevAction};
282 Actions.push_back(Action);
283
284 PrevAction = &Actions.back();
285 }
286
287 // Record the first action of the landing pad site.
288 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1;
289 } // else identical - re-use previous FirstAction
290
291 FirstActions.push_back(FirstAction);
292
293 // Compute this sites contribution to size.
294 SizeActions += SizeSiteActions;
295 }
296
297 // Compute the call-site table. Entries must be ordered by address.
298 SmallVector<CallSiteEntry, 64> CallSites;
299
300 RangeMapType PadMap;
301 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) {
302 const LandingPadInfo *LandingPad = LandingPads[i];
303 for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) {
Chris Lattner16112732010-03-14 01:41:15 +0000304 MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000305 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
306 PadRange P = { i, j };
307 PadMap[BeginLabel] = P;
308 }
309 }
310
311 bool MayThrow = false;
Chris Lattner16112732010-03-14 01:41:15 +0000312 MCSymbol *LastLabel = 0;
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000313 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
314 I != E; ++I) {
315 for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end();
316 MI != E; ++MI) {
Dan Gohman44066042008-07-01 00:05:16 +0000317 if (!MI->isLabel()) {
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000318 MayThrow |= MI->isCall();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000319 continue;
320 }
321
Chris Lattner6b4205a2010-03-14 08:28:48 +0000322 MCSymbol *BeginLabel = MI->getOperand(0).getMCSymbol();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000323 assert(BeginLabel && "Invalid label!");
324
325 if (BeginLabel == LastLabel)
326 MayThrow = false;
327
328 RangeMapType::iterator L = PadMap.find(BeginLabel);
329
330 if (L == PadMap.end())
331 continue;
332
333 PadRange P = L->second;
334 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
335
336 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
337 "Inconsistent landing pad map!");
338
339 // If some instruction between the previous try-range and this one may
340 // throw, create a call-site entry with no landing pad for the region
341 // between the try-ranges.
342 if (MayThrow) {
343 CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0};
344 CallSites.push_back(Site);
345 }
346
347 LastLabel = LandingPad->EndLabels[P.RangeIndex];
348 CallSiteEntry Site = {BeginLabel, LastLabel,
349 LandingPad->LandingPadLabel, FirstActions[P.PadIndex]};
350
351 assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel &&
352 "Invalid landing pad!");
353
354 // Try to merge with the previous call-site.
355 if (CallSites.size()) {
Dan Gohman719de532008-06-21 22:00:54 +0000356 CallSiteEntry &Prev = CallSites.back();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000357 if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) {
358 // Extend the range of the previous entry.
359 Prev.EndLabel = Site.EndLabel;
360 continue;
361 }
362 }
363
364 // Otherwise, create a new call-site.
365 CallSites.push_back(Site);
366 }
367 }
368 // If some instruction between the previous try-range and the end of the
369 // function may throw, create a call-site entry with no landing pad for the
370 // region following the try-range.
371 if (MayThrow) {
372 CallSiteEntry Site = {LastLabel, 0, 0, 0};
373 CallSites.push_back(Site);
374 }
375
376 // Final tallies.
377 unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start.
378 sizeof(int32_t) + // Site length.
379 sizeof(int32_t)); // Landing pad.
380 for (unsigned i = 0, e = CallSites.size(); i < e; ++i)
Chris Lattneraf76e592009-08-22 20:48:53 +0000381 SizeSites += MCAsmInfo::getULEB128Size(CallSites[i].Action);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000382
Micah Villmow2c39b152012-10-15 16:24:29 +0000383 unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000384
385 unsigned TypeOffset = sizeof(int8_t) + // Call site format
386 // Call-site table length
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000387 MCAsmInfo::getULEB128Size(SizeSites) +
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000388 SizeSites + SizeActions + SizeTypes;
389
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000390 // Begin the exception table.
Reid Kleckner01248e62009-08-21 21:03:57 +0000391 JCE->emitAlignmentWithFill(4, 0);
392 // Asm->EOL("Padding");
393
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000394 unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000395
396 // Emit the header.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000397 JCE->emitByte(dwarf::DW_EH_PE_omit);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000398 // Asm->EOL("LPStart format (DW_EH_PE_omit)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000399 JCE->emitByte(dwarf::DW_EH_PE_absptr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000400 // Asm->EOL("TType format (DW_EH_PE_absptr)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000401 JCE->emitULEB128Bytes(TypeOffset);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000402 // Asm->EOL("TType base offset");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000403 JCE->emitByte(dwarf::DW_EH_PE_udata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000404 // Asm->EOL("Call site format (DW_EH_PE_udata4)");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000405 JCE->emitULEB128Bytes(SizeSites);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000406 // Asm->EOL("Call-site table length");
407
408 // Emit the landing pad site information.
409 for (unsigned i = 0; i < CallSites.size(); ++i) {
410 CallSiteEntry &S = CallSites[i];
411 intptr_t BeginLabelPtr = 0;
412 intptr_t EndLabelPtr = 0;
413
414 if (!S.BeginLabel) {
415 BeginLabelPtr = (intptr_t)StartFunction;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000416 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000417 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000418 BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel);
419 JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000420 }
421
422 // Asm->EOL("Region start");
423
Bill Wendlingabeca442009-12-28 01:53:00 +0000424 if (!S.EndLabel)
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000425 EndLabelPtr = (intptr_t)EndFunction;
Bill Wendlingabeca442009-12-28 01:53:00 +0000426 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000427 EndLabelPtr = JCE->getLabelAddress(S.EndLabel);
Bill Wendlingabeca442009-12-28 01:53:00 +0000428
429 JCE->emitInt32(EndLabelPtr - BeginLabelPtr);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000430 //Asm->EOL("Region length");
431
432 if (!S.PadLabel) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000433 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000434 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000435 unsigned PadLabelPtr = JCE->getLabelAddress(S.PadLabel);
436 JCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000437 }
438 // Asm->EOL("Landing pad");
439
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000440 JCE->emitULEB128Bytes(S.Action);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000441 // Asm->EOL("Action");
442 }
443
444 // Emit the actions.
445 for (unsigned I = 0, N = Actions.size(); I != N; ++I) {
446 ActionEntry &Action = Actions[I];
447
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000448 JCE->emitSLEB128Bytes(Action.ValueForTypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000449 //Asm->EOL("TypeInfo index");
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000450 JCE->emitSLEB128Bytes(Action.NextAction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000451 //Asm->EOL("Next action");
452 }
453
454 // Emit the type ids.
455 for (unsigned M = TypeInfos.size(); M; --M) {
Dan Gohman46510a72010-04-15 01:51:59 +0000456 const GlobalVariable *GV = TypeInfos[M - 1];
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000457
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000458 if (GV) {
Micah Villmow2c39b152012-10-15 16:24:29 +0000459 if (TD->getPointerSize(GV->getType()->getAddressSpace()) == sizeof(int32_t))
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000460 JCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV));
Bill Wendling9d48b552009-09-09 00:11:02 +0000461 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000462 JCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000463 } else {
Micah Villmow2c39b152012-10-15 16:24:29 +0000464 if (TD->getPointerSize(0) == sizeof(int32_t))
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000465 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000466 else
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000467 JCE->emitInt64(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000468 }
469 // Asm->EOL("TypeInfo");
470 }
471
472 // Emit the filter typeids.
473 for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) {
474 unsigned TypeID = FilterIds[j];
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000475 JCE->emitULEB128Bytes(TypeID);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000476 //Asm->EOL("Filter TypeInfo index");
477 }
Reid Kleckner01248e62009-08-21 21:03:57 +0000478
479 JCE->emitAlignmentWithFill(4, 0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000480
481 return DwarfExceptionTable;
482}
483
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000484unsigned char*
485JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const {
Micah Villmow2c39b152012-10-15 16:24:29 +0000486 unsigned PointerSize = TD->getPointerSize(0);
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000487 int stackGrowth = stackGrowthDirection == TargetFrameLowering::StackGrowsUp ?
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000488 PointerSize : -PointerSize;
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000489
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000490 unsigned char* StartCommonPtr = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000491 // EH Common Frame header
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000492 JCE->allocateSpace(4, 0);
493 unsigned char* FrameCommonBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
494 JCE->emitInt32((int)0);
495 JCE->emitByte(dwarf::DW_CIE_VERSION);
496 JCE->emitString(Personality ? "zPLR" : "zR");
497 JCE->emitULEB128Bytes(1);
498 JCE->emitSLEB128Bytes(stackGrowth);
499 JCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true));
Bill Wendling9d48b552009-09-09 00:11:02 +0000500
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000501 if (Personality) {
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000502 // Augmentation Size: 3 small ULEBs of one byte each, and the personality
503 // function which size is PointerSize.
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000504 JCE->emitULEB128Bytes(3 + PointerSize);
505
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000506 // We set the encoding of the personality as direct encoding because we use
507 // the function pointer. The encoding is not relative because the current
508 // PC value may be bigger than the personality function pointer.
509 if (PointerSize == 4) {
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000510 JCE->emitByte(dwarf::DW_EH_PE_sdata4);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000511 JCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality)));
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000512 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000513 JCE->emitByte(dwarf::DW_EH_PE_sdata8);
514 JCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality)));
Nicolas Geoffray42cc8f12009-02-15 20:49:23 +0000515 }
Bill Wendling9d48b552009-09-09 00:11:02 +0000516
Bill Wendling89ee7062010-02-16 00:58:02 +0000517 // LSDA encoding: This must match the encoding used in EmitEHFrame ()
518 if (PointerSize == 4)
519 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
520 else
521 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000522 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000523 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000524 JCE->emitULEB128Bytes(1);
525 JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000526 }
527
Evan Cheng2d286172011-07-18 22:29:13 +0000528 EmitFrameMoves(0, MAI->getInitialFrameState());
Reid Kleckner01248e62009-08-21 21:03:57 +0000529
530 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
531
532 JCE->emitInt32At((uintptr_t*)StartCommonPtr,
533 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
534 FrameCommonBeginPtr));
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000535
536 return StartCommonPtr;
537}
538
539
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000540unsigned char*
541JITDwarfEmitter::EmitEHFrame(const Function* Personality,
542 unsigned char* StartCommonPtr,
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000543 unsigned char* StartFunction,
Nicolas Geoffray5913e6c2008-04-20 17:44:19 +0000544 unsigned char* EndFunction,
545 unsigned char* ExceptionTable) const {
Micah Villmow2c39b152012-10-15 16:24:29 +0000546 unsigned PointerSize = TD->getPointerSize(0);
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000547
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000548 // EH frame header.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000549 unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue();
550 JCE->allocateSpace(4, 0);
551 unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue();
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000552 // FDE CIE Offset
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000553 JCE->emitInt32(FrameBeginPtr - StartCommonPtr);
554 JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue());
555 JCE->emitInt32(EndFunction - StartFunction);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000556
557 // If there is a personality and landing pads then point to the language
558 // specific data area in the exception table.
Bill Wendling9d48b552009-09-09 00:11:02 +0000559 if (Personality) {
560 JCE->emitULEB128Bytes(PointerSize == 4 ? 4 : 8);
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000561
Bill Wendling9d48b552009-09-09 00:11:02 +0000562 if (PointerSize == 4) {
563 if (!MMI->getLandingPads().empty())
564 JCE->emitInt32(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
565 else
566 JCE->emitInt32((int)0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000567 } else {
Bill Wendling9d48b552009-09-09 00:11:02 +0000568 if (!MMI->getLandingPads().empty())
569 JCE->emitInt64(ExceptionTable-(unsigned char*)JCE->getCurrentPCValue());
570 else
571 JCE->emitInt64((int)0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000572 }
573 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000574 JCE->emitULEB128Bytes(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000575 }
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000576
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000577 // Indicate locations of function specific callee saved registers in
578 // frame.
579 EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves());
Reid Kleckner01248e62009-08-21 21:03:57 +0000580
581 JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop);
582
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000583 // Indicate the size of the table
Reid Kleckner01248e62009-08-21 21:03:57 +0000584 JCE->emitInt32At((uintptr_t*)StartEHPtr,
585 (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() -
586 StartEHPtr));
587
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000588 // Double zeroes for the unwind runtime
589 if (PointerSize == 8) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000590 JCE->emitInt64(0);
591 JCE->emitInt64(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000592 } else {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000593 JCE->emitInt32(0);
594 JCE->emitInt32(0);
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000595 }
Jim Grosbach8bf0ecd2011-03-22 15:21:58 +0000596
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000597 return StartEHPtr;
598}