Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 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" |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/JITCodeEmitter.h" |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunction.h" |
| 21 | #include "llvm/CodeGen/MachineLocation.h" |
| 22 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 23 | #include "llvm/ExecutionEngine/JITMemoryManager.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 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, |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 38 | JITCodeEmitter& jce, |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 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(); |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 46 | JCE = &jce; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 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 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 64 | void |
| 65 | JITDwarfEmitter::EmitFrameMoves(intptr_t BaseLabelPtr, |
| 66 | const std::vector<MachineMove> &Moves) const { |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 67 | unsigned PointerSize = TD->getPointerSize(); |
| 68 | int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ? |
| 69 | PointerSize : -PointerSize; |
Nicolas Geoffray | 2d450eb | 2008-08-19 14:48:14 +0000 | [diff] [blame] | 70 | bool IsLocal = false; |
| 71 | unsigned BaseLabelID = 0; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 72 | |
| 73 | for (unsigned i = 0, N = Moves.size(); i < N; ++i) { |
| 74 | const MachineMove &Move = Moves[i]; |
| 75 | unsigned LabelID = Move.getLabelID(); |
| 76 | |
| 77 | if (LabelID) { |
| 78 | LabelID = MMI->MappedLabel(LabelID); |
| 79 | |
| 80 | // Throw out move if the label is invalid. |
| 81 | if (!LabelID) continue; |
| 82 | } |
| 83 | |
| 84 | intptr_t LabelPtr = 0; |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 85 | if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 86 | |
| 87 | const MachineLocation &Dst = Move.getDestination(); |
| 88 | const MachineLocation &Src = Move.getSource(); |
| 89 | |
| 90 | // Advance row if new location. |
Nicolas Geoffray | 2d450eb | 2008-08-19 14:48:14 +0000 | [diff] [blame] | 91 | if (BaseLabelPtr && LabelID && (BaseLabelID != LabelID || !IsLocal)) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 92 | JCE->emitByte(dwarf::DW_CFA_advance_loc4); |
| 93 | JCE->emitInt32(LabelPtr - BaseLabelPtr); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 94 | |
Nicolas Geoffray | 2d450eb | 2008-08-19 14:48:14 +0000 | [diff] [blame] | 95 | BaseLabelID = LabelID; |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 96 | BaseLabelPtr = LabelPtr; |
| 97 | IsLocal = true; |
| 98 | } |
| 99 | |
| 100 | // If advancing cfa. |
Daniel Dunbar | 489032a | 2008-10-03 17:11:57 +0000 | [diff] [blame] | 101 | if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { |
| 102 | if (!Src.isReg()) { |
| 103 | if (Src.getReg() == MachineLocation::VirtualFP) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 104 | JCE->emitByte(dwarf::DW_CFA_def_cfa_offset); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 105 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 106 | JCE->emitByte(dwarf::DW_CFA_def_cfa); |
| 107 | JCE->emitULEB128Bytes(RI->getDwarfRegNum(Src.getReg(), true)); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int Offset = -Src.getOffset(); |
| 111 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 112 | JCE->emitULEB128Bytes(Offset); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 113 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 114 | llvm_unreachable("Machine move no supported yet."); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 115 | } |
Daniel Dunbar | 489032a | 2008-10-03 17:11:57 +0000 | [diff] [blame] | 116 | } else if (Src.isReg() && |
| 117 | Src.getReg() == MachineLocation::VirtualFP) { |
| 118 | if (Dst.isReg()) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 119 | JCE->emitByte(dwarf::DW_CFA_def_cfa_register); |
| 120 | JCE->emitULEB128Bytes(RI->getDwarfRegNum(Dst.getReg(), true)); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 121 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 122 | llvm_unreachable("Machine move no supported yet."); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 123 | } |
| 124 | } else { |
Daniel Dunbar | 489032a | 2008-10-03 17:11:57 +0000 | [diff] [blame] | 125 | unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 126 | int Offset = Dst.getOffset() / stackGrowth; |
| 127 | |
| 128 | if (Offset < 0) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 129 | JCE->emitByte(dwarf::DW_CFA_offset_extended_sf); |
| 130 | JCE->emitULEB128Bytes(Reg); |
| 131 | JCE->emitSLEB128Bytes(Offset); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 132 | } else if (Reg < 64) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 133 | JCE->emitByte(dwarf::DW_CFA_offset + Reg); |
| 134 | JCE->emitULEB128Bytes(Offset); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 135 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 136 | JCE->emitByte(dwarf::DW_CFA_offset_extended); |
| 137 | JCE->emitULEB128Bytes(Reg); |
| 138 | JCE->emitULEB128Bytes(Offset); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /// SharedTypeIds - How many leading type ids two landing pads have in common. |
| 145 | static unsigned SharedTypeIds(const LandingPadInfo *L, |
| 146 | const LandingPadInfo *R) { |
| 147 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; |
| 148 | unsigned LSize = LIds.size(), RSize = RIds.size(); |
| 149 | unsigned MinSize = LSize < RSize ? LSize : RSize; |
| 150 | unsigned Count = 0; |
| 151 | |
| 152 | for (; Count != MinSize; ++Count) |
| 153 | if (LIds[Count] != RIds[Count]) |
| 154 | return Count; |
| 155 | |
| 156 | return Count; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /// PadLT - Order landing pads lexicographically by type id. |
| 161 | static bool PadLT(const LandingPadInfo *L, const LandingPadInfo *R) { |
| 162 | const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; |
| 163 | unsigned LSize = LIds.size(), RSize = RIds.size(); |
| 164 | unsigned MinSize = LSize < RSize ? LSize : RSize; |
| 165 | |
| 166 | for (unsigned i = 0; i != MinSize; ++i) |
| 167 | if (LIds[i] != RIds[i]) |
| 168 | return LIds[i] < RIds[i]; |
| 169 | |
| 170 | return LSize < RSize; |
| 171 | } |
| 172 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 173 | namespace { |
| 174 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 175 | struct KeyInfo { |
| 176 | static inline unsigned getEmptyKey() { return -1U; } |
| 177 | static inline unsigned getTombstoneKey() { return -2U; } |
| 178 | static unsigned getHashValue(const unsigned &Key) { return Key; } |
| 179 | static bool isEqual(unsigned LHS, unsigned RHS) { return LHS == RHS; } |
| 180 | static bool isPod() { return true; } |
| 181 | }; |
| 182 | |
| 183 | /// ActionEntry - Structure describing an entry in the actions table. |
| 184 | struct ActionEntry { |
| 185 | int ValueForTypeID; // The value to write - may not be equal to the type id. |
| 186 | int NextAction; |
| 187 | struct ActionEntry *Previous; |
| 188 | }; |
| 189 | |
| 190 | /// PadRange - Structure holding a try-range and the associated landing pad. |
| 191 | struct PadRange { |
| 192 | // The index of the landing pad. |
| 193 | unsigned PadIndex; |
| 194 | // The index of the begin and end labels in the landing pad's label lists. |
| 195 | unsigned RangeIndex; |
| 196 | }; |
| 197 | |
| 198 | typedef DenseMap<unsigned, PadRange, KeyInfo> RangeMapType; |
| 199 | |
| 200 | /// CallSiteEntry - Structure describing an entry in the call-site table. |
| 201 | struct CallSiteEntry { |
| 202 | unsigned BeginLabel; // zero indicates the start of the function. |
| 203 | unsigned EndLabel; // zero indicates the end of the function. |
| 204 | unsigned PadLabel; // zero indicates that there is no landing pad. |
| 205 | unsigned Action; |
| 206 | }; |
| 207 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 210 | unsigned char* JITDwarfEmitter::EmitExceptionTable(MachineFunction* MF, |
| 211 | unsigned char* StartFunction, |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 212 | unsigned char* EndFunction) const { |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 213 | // Map all labels and get rid of any dead landing pads. |
| 214 | MMI->TidyLandingPads(); |
| 215 | |
| 216 | const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); |
| 217 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
| 218 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); |
| 219 | if (PadInfos.empty()) return 0; |
| 220 | |
| 221 | // Sort the landing pads in order of their type ids. This is used to fold |
| 222 | // duplicate actions. |
| 223 | SmallVector<const LandingPadInfo *, 64> LandingPads; |
| 224 | LandingPads.reserve(PadInfos.size()); |
| 225 | for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) |
| 226 | LandingPads.push_back(&PadInfos[i]); |
| 227 | std::sort(LandingPads.begin(), LandingPads.end(), PadLT); |
| 228 | |
| 229 | // Negative type ids index into FilterIds, positive type ids index into |
| 230 | // TypeInfos. The value written for a positive type id is just the type |
| 231 | // id itself. For a negative type id, however, the value written is the |
| 232 | // (negative) byte offset of the corresponding FilterIds entry. The byte |
| 233 | // offset is usually equal to the type id, because the FilterIds entries |
| 234 | // are written using a variable width encoding which outputs one byte per |
| 235 | // entry as long as the value written is not too large, but can differ. |
| 236 | // This kind of complication does not occur for positive type ids because |
| 237 | // type infos are output using a fixed width encoding. |
| 238 | // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i]. |
| 239 | SmallVector<int, 16> FilterOffsets; |
| 240 | FilterOffsets.reserve(FilterIds.size()); |
| 241 | int Offset = -1; |
| 242 | for(std::vector<unsigned>::const_iterator I = FilterIds.begin(), |
| 243 | E = FilterIds.end(); I != E; ++I) { |
| 244 | FilterOffsets.push_back(Offset); |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 245 | Offset -= TargetAsmInfo::getULEB128Size(*I); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | // Compute the actions table and gather the first action index for each |
| 249 | // landing pad site. |
| 250 | SmallVector<ActionEntry, 32> Actions; |
| 251 | SmallVector<unsigned, 64> FirstActions; |
| 252 | FirstActions.reserve(LandingPads.size()); |
| 253 | |
| 254 | int FirstAction = 0; |
| 255 | unsigned SizeActions = 0; |
| 256 | for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { |
| 257 | const LandingPadInfo *LP = LandingPads[i]; |
| 258 | const std::vector<int> &TypeIds = LP->TypeIds; |
| 259 | const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0; |
| 260 | unsigned SizeSiteActions = 0; |
| 261 | |
| 262 | if (NumShared < TypeIds.size()) { |
| 263 | unsigned SizeAction = 0; |
| 264 | ActionEntry *PrevAction = 0; |
| 265 | |
| 266 | if (NumShared) { |
| 267 | const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size(); |
| 268 | assert(Actions.size()); |
| 269 | PrevAction = &Actions.back(); |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 270 | SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) + |
| 271 | TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 272 | for (unsigned j = NumShared; j != SizePrevIds; ++j) { |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 273 | SizeAction -= TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 274 | SizeAction += -PrevAction->NextAction; |
| 275 | PrevAction = PrevAction->Previous; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // Compute the actions. |
| 280 | for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) { |
| 281 | int TypeID = TypeIds[I]; |
| 282 | assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); |
| 283 | int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 284 | unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 285 | |
| 286 | int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 287 | SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 288 | SizeSiteActions += SizeAction; |
| 289 | |
| 290 | ActionEntry Action = {ValueForTypeID, NextAction, PrevAction}; |
| 291 | Actions.push_back(Action); |
| 292 | |
| 293 | PrevAction = &Actions.back(); |
| 294 | } |
| 295 | |
| 296 | // Record the first action of the landing pad site. |
| 297 | FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; |
| 298 | } // else identical - re-use previous FirstAction |
| 299 | |
| 300 | FirstActions.push_back(FirstAction); |
| 301 | |
| 302 | // Compute this sites contribution to size. |
| 303 | SizeActions += SizeSiteActions; |
| 304 | } |
| 305 | |
| 306 | // Compute the call-site table. Entries must be ordered by address. |
| 307 | SmallVector<CallSiteEntry, 64> CallSites; |
| 308 | |
| 309 | RangeMapType PadMap; |
| 310 | for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { |
| 311 | const LandingPadInfo *LandingPad = LandingPads[i]; |
| 312 | for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) { |
| 313 | unsigned BeginLabel = LandingPad->BeginLabels[j]; |
| 314 | assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); |
| 315 | PadRange P = { i, j }; |
| 316 | PadMap[BeginLabel] = P; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | bool MayThrow = false; |
| 321 | unsigned LastLabel = 0; |
| 322 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); |
| 323 | I != E; ++I) { |
| 324 | for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); |
| 325 | MI != E; ++MI) { |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 326 | if (!MI->isLabel()) { |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 327 | MayThrow |= MI->getDesc().isCall(); |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | unsigned BeginLabel = MI->getOperand(0).getImm(); |
| 332 | assert(BeginLabel && "Invalid label!"); |
| 333 | |
| 334 | if (BeginLabel == LastLabel) |
| 335 | MayThrow = false; |
| 336 | |
| 337 | RangeMapType::iterator L = PadMap.find(BeginLabel); |
| 338 | |
| 339 | if (L == PadMap.end()) |
| 340 | continue; |
| 341 | |
| 342 | PadRange P = L->second; |
| 343 | const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; |
| 344 | |
| 345 | assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && |
| 346 | "Inconsistent landing pad map!"); |
| 347 | |
| 348 | // If some instruction between the previous try-range and this one may |
| 349 | // throw, create a call-site entry with no landing pad for the region |
| 350 | // between the try-ranges. |
| 351 | if (MayThrow) { |
| 352 | CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0}; |
| 353 | CallSites.push_back(Site); |
| 354 | } |
| 355 | |
| 356 | LastLabel = LandingPad->EndLabels[P.RangeIndex]; |
| 357 | CallSiteEntry Site = {BeginLabel, LastLabel, |
| 358 | LandingPad->LandingPadLabel, FirstActions[P.PadIndex]}; |
| 359 | |
| 360 | assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel && |
| 361 | "Invalid landing pad!"); |
| 362 | |
| 363 | // Try to merge with the previous call-site. |
| 364 | if (CallSites.size()) { |
Dan Gohman | 719de53 | 2008-06-21 22:00:54 +0000 | [diff] [blame] | 365 | CallSiteEntry &Prev = CallSites.back(); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 366 | if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { |
| 367 | // Extend the range of the previous entry. |
| 368 | Prev.EndLabel = Site.EndLabel; |
| 369 | continue; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | // Otherwise, create a new call-site. |
| 374 | CallSites.push_back(Site); |
| 375 | } |
| 376 | } |
| 377 | // If some instruction between the previous try-range and the end of the |
| 378 | // function may throw, create a call-site entry with no landing pad for the |
| 379 | // region following the try-range. |
| 380 | if (MayThrow) { |
| 381 | CallSiteEntry Site = {LastLabel, 0, 0, 0}; |
| 382 | CallSites.push_back(Site); |
| 383 | } |
| 384 | |
| 385 | // Final tallies. |
| 386 | unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start. |
| 387 | sizeof(int32_t) + // Site length. |
| 388 | sizeof(int32_t)); // Landing pad. |
| 389 | for (unsigned i = 0, e = CallSites.size(); i < e; ++i) |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 390 | SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 391 | |
| 392 | unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize(); |
| 393 | |
| 394 | unsigned TypeOffset = sizeof(int8_t) + // Call site format |
| 395 | // Call-site table length |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 396 | TargetAsmInfo::getULEB128Size(SizeSites) + |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 397 | SizeSites + SizeActions + SizeTypes; |
| 398 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 399 | // Begin the exception table. |
Reid Kleckner | 01248e6 | 2009-08-21 21:03:57 +0000 | [diff] [blame] | 400 | JCE->emitAlignmentWithFill(4, 0); |
| 401 | // Asm->EOL("Padding"); |
| 402 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 403 | unsigned char* DwarfExceptionTable = (unsigned char*)JCE->getCurrentPCValue(); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 404 | |
| 405 | // Emit the header. |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 406 | JCE->emitByte(dwarf::DW_EH_PE_omit); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 407 | // Asm->EOL("LPStart format (DW_EH_PE_omit)"); |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 408 | JCE->emitByte(dwarf::DW_EH_PE_absptr); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 409 | // Asm->EOL("TType format (DW_EH_PE_absptr)"); |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 410 | JCE->emitULEB128Bytes(TypeOffset); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 411 | // Asm->EOL("TType base offset"); |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 412 | JCE->emitByte(dwarf::DW_EH_PE_udata4); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 413 | // Asm->EOL("Call site format (DW_EH_PE_udata4)"); |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 414 | JCE->emitULEB128Bytes(SizeSites); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 415 | // Asm->EOL("Call-site table length"); |
| 416 | |
| 417 | // Emit the landing pad site information. |
| 418 | for (unsigned i = 0; i < CallSites.size(); ++i) { |
| 419 | CallSiteEntry &S = CallSites[i]; |
| 420 | intptr_t BeginLabelPtr = 0; |
| 421 | intptr_t EndLabelPtr = 0; |
| 422 | |
| 423 | if (!S.BeginLabel) { |
| 424 | BeginLabelPtr = (intptr_t)StartFunction; |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 425 | JCE->emitInt32(0); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 426 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 427 | BeginLabelPtr = JCE->getLabelAddress(S.BeginLabel); |
| 428 | JCE->emitInt32(BeginLabelPtr - (intptr_t)StartFunction); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | // Asm->EOL("Region start"); |
| 432 | |
| 433 | if (!S.EndLabel) { |
| 434 | EndLabelPtr = (intptr_t)EndFunction; |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 435 | JCE->emitInt32((intptr_t)EndFunction - BeginLabelPtr); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 436 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 437 | EndLabelPtr = JCE->getLabelAddress(S.EndLabel); |
| 438 | JCE->emitInt32(EndLabelPtr - BeginLabelPtr); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 439 | } |
| 440 | //Asm->EOL("Region length"); |
| 441 | |
| 442 | if (!S.PadLabel) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 443 | JCE->emitInt32(0); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 444 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 445 | unsigned PadLabelPtr = JCE->getLabelAddress(S.PadLabel); |
| 446 | JCE->emitInt32(PadLabelPtr - (intptr_t)StartFunction); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 447 | } |
| 448 | // Asm->EOL("Landing pad"); |
| 449 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 450 | JCE->emitULEB128Bytes(S.Action); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 451 | // Asm->EOL("Action"); |
| 452 | } |
| 453 | |
| 454 | // Emit the actions. |
| 455 | for (unsigned I = 0, N = Actions.size(); I != N; ++I) { |
| 456 | ActionEntry &Action = Actions[I]; |
| 457 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 458 | JCE->emitSLEB128Bytes(Action.ValueForTypeID); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 459 | //Asm->EOL("TypeInfo index"); |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 460 | JCE->emitSLEB128Bytes(Action.NextAction); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 461 | //Asm->EOL("Next action"); |
| 462 | } |
| 463 | |
| 464 | // Emit the type ids. |
| 465 | for (unsigned M = TypeInfos.size(); M; --M) { |
| 466 | GlobalVariable *GV = TypeInfos[M - 1]; |
| 467 | |
| 468 | if (GV) { |
| 469 | if (TD->getPointerSize() == sizeof(int32_t)) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 470 | JCE->emitInt32((intptr_t)Jit.getOrEmitGlobalVariable(GV)); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 471 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 472 | JCE->emitInt64((intptr_t)Jit.getOrEmitGlobalVariable(GV)); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 473 | } |
| 474 | } else { |
| 475 | if (TD->getPointerSize() == sizeof(int32_t)) |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 476 | JCE->emitInt32(0); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 477 | else |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 478 | JCE->emitInt64(0); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 479 | } |
| 480 | // Asm->EOL("TypeInfo"); |
| 481 | } |
| 482 | |
| 483 | // Emit the filter typeids. |
| 484 | for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) { |
| 485 | unsigned TypeID = FilterIds[j]; |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 486 | JCE->emitULEB128Bytes(TypeID); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 487 | //Asm->EOL("Filter TypeInfo index"); |
| 488 | } |
Reid Kleckner | 01248e6 | 2009-08-21 21:03:57 +0000 | [diff] [blame] | 489 | |
| 490 | JCE->emitAlignmentWithFill(4, 0); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 491 | |
| 492 | return DwarfExceptionTable; |
| 493 | } |
| 494 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 495 | unsigned char* |
| 496 | JITDwarfEmitter::EmitCommonEHFrame(const Function* Personality) const { |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 497 | unsigned PointerSize = TD->getPointerSize(); |
| 498 | int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ? |
| 499 | PointerSize : -PointerSize; |
| 500 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 501 | unsigned char* StartCommonPtr = (unsigned char*)JCE->getCurrentPCValue(); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 502 | // EH Common Frame header |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 503 | JCE->allocateSpace(4, 0); |
| 504 | unsigned char* FrameCommonBeginPtr = (unsigned char*)JCE->getCurrentPCValue(); |
| 505 | JCE->emitInt32((int)0); |
| 506 | JCE->emitByte(dwarf::DW_CIE_VERSION); |
| 507 | JCE->emitString(Personality ? "zPLR" : "zR"); |
| 508 | JCE->emitULEB128Bytes(1); |
| 509 | JCE->emitSLEB128Bytes(stackGrowth); |
| 510 | JCE->emitByte(RI->getDwarfRegNum(RI->getRARegister(), true)); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 511 | |
| 512 | if (Personality) { |
Nicolas Geoffray | 42cc8f1 | 2009-02-15 20:49:23 +0000 | [diff] [blame] | 513 | // Augmentation Size: 3 small ULEBs of one byte each, and the personality |
| 514 | // function which size is PointerSize. |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 515 | JCE->emitULEB128Bytes(3 + PointerSize); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 516 | |
Nicolas Geoffray | 42cc8f1 | 2009-02-15 20:49:23 +0000 | [diff] [blame] | 517 | // We set the encoding of the personality as direct encoding because we use |
| 518 | // the function pointer. The encoding is not relative because the current |
| 519 | // PC value may be bigger than the personality function pointer. |
| 520 | if (PointerSize == 4) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 521 | JCE->emitByte(dwarf::DW_EH_PE_sdata4); |
| 522 | JCE->emitInt32(((intptr_t)Jit.getPointerToGlobal(Personality))); |
Nicolas Geoffray | 42cc8f1 | 2009-02-15 20:49:23 +0000 | [diff] [blame] | 523 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 524 | JCE->emitByte(dwarf::DW_EH_PE_sdata8); |
| 525 | JCE->emitInt64(((intptr_t)Jit.getPointerToGlobal(Personality))); |
Nicolas Geoffray | 42cc8f1 | 2009-02-15 20:49:23 +0000 | [diff] [blame] | 526 | } |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 527 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 528 | JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
| 529 | JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 530 | |
| 531 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 532 | JCE->emitULEB128Bytes(1); |
| 533 | JCE->emitULEB128Bytes(dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | std::vector<MachineMove> Moves; |
| 537 | RI->getInitialFrameState(Moves); |
| 538 | EmitFrameMoves(0, Moves); |
Reid Kleckner | 01248e6 | 2009-08-21 21:03:57 +0000 | [diff] [blame] | 539 | |
| 540 | JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop); |
| 541 | |
| 542 | JCE->emitInt32At((uintptr_t*)StartCommonPtr, |
| 543 | (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() - |
| 544 | FrameCommonBeginPtr)); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 545 | |
| 546 | return StartCommonPtr; |
| 547 | } |
| 548 | |
| 549 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 550 | unsigned char* |
| 551 | JITDwarfEmitter::EmitEHFrame(const Function* Personality, |
| 552 | unsigned char* StartCommonPtr, |
| 553 | unsigned char* StartFunction, |
| 554 | unsigned char* EndFunction, |
| 555 | unsigned char* ExceptionTable) const { |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 556 | unsigned PointerSize = TD->getPointerSize(); |
| 557 | |
| 558 | // EH frame header. |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 559 | unsigned char* StartEHPtr = (unsigned char*)JCE->getCurrentPCValue(); |
| 560 | JCE->allocateSpace(4, 0); |
| 561 | unsigned char* FrameBeginPtr = (unsigned char*)JCE->getCurrentPCValue(); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 562 | // FDE CIE Offset |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 563 | JCE->emitInt32(FrameBeginPtr - StartCommonPtr); |
| 564 | JCE->emitInt32(StartFunction - (unsigned char*)JCE->getCurrentPCValue()); |
| 565 | JCE->emitInt32(EndFunction - StartFunction); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 566 | |
| 567 | // If there is a personality and landing pads then point to the language |
| 568 | // specific data area in the exception table. |
| 569 | if (MMI->getPersonalityIndex()) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 570 | JCE->emitULEB128Bytes(4); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 571 | |
| 572 | if (!MMI->getLandingPads().empty()) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 573 | JCE->emitInt32(ExceptionTable - (unsigned char*)JCE->getCurrentPCValue()); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 574 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 575 | JCE->emitInt32((int)0); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 576 | } |
| 577 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 578 | JCE->emitULEB128Bytes(0); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | // Indicate locations of function specific callee saved registers in |
| 582 | // frame. |
| 583 | EmitFrameMoves((intptr_t)StartFunction, MMI->getFrameMoves()); |
Reid Kleckner | 01248e6 | 2009-08-21 21:03:57 +0000 | [diff] [blame] | 584 | |
| 585 | JCE->emitAlignmentWithFill(PointerSize, dwarf::DW_CFA_nop); |
| 586 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 587 | // Indicate the size of the table |
Reid Kleckner | 01248e6 | 2009-08-21 21:03:57 +0000 | [diff] [blame] | 588 | JCE->emitInt32At((uintptr_t*)StartEHPtr, |
| 589 | (uintptr_t)((unsigned char*)JCE->getCurrentPCValue() - |
| 590 | StartEHPtr)); |
| 591 | |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 592 | // Double zeroes for the unwind runtime |
| 593 | if (PointerSize == 8) { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 594 | JCE->emitInt64(0); |
| 595 | JCE->emitInt64(0); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 596 | } else { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 597 | JCE->emitInt32(0); |
| 598 | JCE->emitInt32(0); |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 599 | } |
Nicolas Geoffray | afe6c2b | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 600 | |
| 601 | return StartEHPtr; |
| 602 | } |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 603 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 604 | unsigned JITDwarfEmitter::GetDwarfTableSizeInBytes(MachineFunction& F, |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 605 | JITCodeEmitter& jce, |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 606 | unsigned char* StartFunction, |
| 607 | unsigned char* EndFunction) { |
| 608 | const TargetMachine& TM = F.getTarget(); |
| 609 | TD = TM.getTargetData(); |
| 610 | needsIndirectEncoding = TM.getTargetAsmInfo()->getNeedsIndirectEncoding(); |
| 611 | stackGrowthDirection = TM.getFrameInfo()->getStackGrowthDirection(); |
| 612 | RI = TM.getRegisterInfo(); |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 613 | JCE = &jce; |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 614 | unsigned FinalSize = 0; |
| 615 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 616 | FinalSize += GetExceptionTableSizeInBytes(&F); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 617 | |
| 618 | const std::vector<Function *> Personalities = MMI->getPersonalities(); |
Nicolas Geoffray | 67c8c4c | 2008-11-18 10:44:46 +0000 | [diff] [blame] | 619 | FinalSize += |
| 620 | GetCommonEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()]); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 621 | |
Nicolas Geoffray | 67c8c4c | 2008-11-18 10:44:46 +0000 | [diff] [blame] | 622 | FinalSize += GetEHFrameSizeInBytes(Personalities[MMI->getPersonalityIndex()], |
| 623 | StartFunction); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 624 | |
| 625 | return FinalSize; |
| 626 | } |
| 627 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 628 | /// RoundUpToAlign - Add the specified alignment to FinalSize and returns |
| 629 | /// the new value. |
| 630 | static unsigned RoundUpToAlign(unsigned FinalSize, unsigned Alignment) { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 631 | if (Alignment == 0) Alignment = 1; |
Nicolas Geoffray | 580631a | 2008-04-20 23:39:44 +0000 | [diff] [blame] | 632 | // Since we do not know where the buffer will be allocated, be pessimistic. |
| 633 | return FinalSize + Alignment; |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 634 | } |
| 635 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 636 | unsigned |
| 637 | JITDwarfEmitter::GetEHFrameSizeInBytes(const Function* Personality, |
| 638 | unsigned char* StartFunction) const { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 639 | unsigned PointerSize = TD->getPointerSize(); |
| 640 | unsigned FinalSize = 0; |
| 641 | // EH frame header. |
| 642 | FinalSize += PointerSize; |
| 643 | // FDE CIE Offset |
| 644 | FinalSize += 3 * PointerSize; |
| 645 | // If there is a personality and landing pads then point to the language |
| 646 | // specific data area in the exception table. |
| 647 | if (MMI->getPersonalityIndex()) { |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 648 | FinalSize += TargetAsmInfo::getULEB128Size(4); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 649 | FinalSize += PointerSize; |
| 650 | } else { |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 651 | FinalSize += TargetAsmInfo::getULEB128Size(0); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | // Indicate locations of function specific callee saved registers in |
| 655 | // frame. |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 656 | FinalSize += GetFrameMovesSizeInBytes((intptr_t)StartFunction, |
| 657 | MMI->getFrameMoves()); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 658 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 659 | FinalSize = RoundUpToAlign(FinalSize, 4); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 660 | |
| 661 | // Double zeroes for the unwind runtime |
| 662 | FinalSize += 2 * PointerSize; |
| 663 | |
| 664 | return FinalSize; |
| 665 | } |
| 666 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 667 | unsigned JITDwarfEmitter::GetCommonEHFrameSizeInBytes(const Function* Personality) |
| 668 | const { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 669 | |
| 670 | unsigned PointerSize = TD->getPointerSize(); |
| 671 | int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ? |
| 672 | PointerSize : -PointerSize; |
| 673 | unsigned FinalSize = 0; |
| 674 | // EH Common Frame header |
| 675 | FinalSize += PointerSize; |
| 676 | FinalSize += 4; |
| 677 | FinalSize += 1; |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 678 | FinalSize += Personality ? 5 : 3; // "zPLR" or "zR" |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 679 | FinalSize += TargetAsmInfo::getULEB128Size(1); |
| 680 | FinalSize += TargetAsmInfo::getSLEB128Size(stackGrowth); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 681 | FinalSize += 1; |
| 682 | |
| 683 | if (Personality) { |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 684 | FinalSize += TargetAsmInfo::getULEB128Size(7); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 685 | |
| 686 | // Encoding |
| 687 | FinalSize+= 1; |
| 688 | //Personality |
| 689 | FinalSize += PointerSize; |
| 690 | |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 691 | FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel); |
| 692 | FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 693 | |
| 694 | } else { |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 695 | FinalSize += TargetAsmInfo::getULEB128Size(1); |
| 696 | FinalSize += TargetAsmInfo::getULEB128Size(dwarf::DW_EH_PE_pcrel); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | std::vector<MachineMove> Moves; |
| 700 | RI->getInitialFrameState(Moves); |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 701 | FinalSize += GetFrameMovesSizeInBytes(0, Moves); |
| 702 | FinalSize = RoundUpToAlign(FinalSize, 4); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 703 | return FinalSize; |
| 704 | } |
| 705 | |
| 706 | unsigned |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 707 | JITDwarfEmitter::GetFrameMovesSizeInBytes(intptr_t BaseLabelPtr, |
| 708 | const std::vector<MachineMove> &Moves) const { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 709 | unsigned PointerSize = TD->getPointerSize(); |
| 710 | int stackGrowth = stackGrowthDirection == TargetFrameInfo::StackGrowsUp ? |
| 711 | PointerSize : -PointerSize; |
| 712 | bool IsLocal = BaseLabelPtr; |
| 713 | unsigned FinalSize = 0; |
| 714 | |
| 715 | for (unsigned i = 0, N = Moves.size(); i < N; ++i) { |
| 716 | const MachineMove &Move = Moves[i]; |
| 717 | unsigned LabelID = Move.getLabelID(); |
| 718 | |
| 719 | if (LabelID) { |
| 720 | LabelID = MMI->MappedLabel(LabelID); |
| 721 | |
| 722 | // Throw out move if the label is invalid. |
| 723 | if (!LabelID) continue; |
| 724 | } |
| 725 | |
| 726 | intptr_t LabelPtr = 0; |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 727 | if (LabelID) LabelPtr = JCE->getLabelAddress(LabelID); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 728 | |
| 729 | const MachineLocation &Dst = Move.getDestination(); |
| 730 | const MachineLocation &Src = Move.getSource(); |
| 731 | |
| 732 | // Advance row if new location. |
| 733 | if (BaseLabelPtr && LabelID && (BaseLabelPtr != LabelPtr || !IsLocal)) { |
| 734 | FinalSize++; |
| 735 | FinalSize += PointerSize; |
| 736 | BaseLabelPtr = LabelPtr; |
| 737 | IsLocal = true; |
| 738 | } |
| 739 | |
| 740 | // If advancing cfa. |
Daniel Dunbar | 489032a | 2008-10-03 17:11:57 +0000 | [diff] [blame] | 741 | if (Dst.isReg() && Dst.getReg() == MachineLocation::VirtualFP) { |
| 742 | if (!Src.isReg()) { |
| 743 | if (Src.getReg() == MachineLocation::VirtualFP) { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 744 | ++FinalSize; |
| 745 | } else { |
| 746 | ++FinalSize; |
Daniel Dunbar | 489032a | 2008-10-03 17:11:57 +0000 | [diff] [blame] | 747 | unsigned RegNum = RI->getDwarfRegNum(Src.getReg(), true); |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 748 | FinalSize += TargetAsmInfo::getULEB128Size(RegNum); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | int Offset = -Src.getOffset(); |
| 752 | |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 753 | FinalSize += TargetAsmInfo::getULEB128Size(Offset); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 754 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 755 | llvm_unreachable("Machine move no supported yet."); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 756 | } |
Daniel Dunbar | 489032a | 2008-10-03 17:11:57 +0000 | [diff] [blame] | 757 | } else if (Src.isReg() && |
| 758 | Src.getReg() == MachineLocation::VirtualFP) { |
| 759 | if (Dst.isReg()) { |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 760 | ++FinalSize; |
Daniel Dunbar | 489032a | 2008-10-03 17:11:57 +0000 | [diff] [blame] | 761 | unsigned RegNum = RI->getDwarfRegNum(Dst.getReg(), true); |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 762 | FinalSize += TargetAsmInfo::getULEB128Size(RegNum); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 763 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 764 | llvm_unreachable("Machine move no supported yet."); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 765 | } |
| 766 | } else { |
Daniel Dunbar | 489032a | 2008-10-03 17:11:57 +0000 | [diff] [blame] | 767 | unsigned Reg = RI->getDwarfRegNum(Src.getReg(), true); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 768 | int Offset = Dst.getOffset() / stackGrowth; |
| 769 | |
| 770 | if (Offset < 0) { |
| 771 | ++FinalSize; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 772 | FinalSize += TargetAsmInfo::getULEB128Size(Reg); |
| 773 | FinalSize += TargetAsmInfo::getSLEB128Size(Offset); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 774 | } else if (Reg < 64) { |
| 775 | ++FinalSize; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 776 | FinalSize += TargetAsmInfo::getULEB128Size(Offset); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 777 | } else { |
| 778 | ++FinalSize; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 779 | FinalSize += TargetAsmInfo::getULEB128Size(Reg); |
| 780 | FinalSize += TargetAsmInfo::getULEB128Size(Offset); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } |
| 784 | return FinalSize; |
| 785 | } |
| 786 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 787 | unsigned |
| 788 | JITDwarfEmitter::GetExceptionTableSizeInBytes(MachineFunction* MF) const { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 789 | unsigned FinalSize = 0; |
| 790 | |
| 791 | // Map all labels and get rid of any dead landing pads. |
| 792 | MMI->TidyLandingPads(); |
| 793 | |
| 794 | const std::vector<GlobalVariable *> &TypeInfos = MMI->getTypeInfos(); |
| 795 | const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); |
| 796 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); |
| 797 | if (PadInfos.empty()) return 0; |
| 798 | |
| 799 | // Sort the landing pads in order of their type ids. This is used to fold |
| 800 | // duplicate actions. |
| 801 | SmallVector<const LandingPadInfo *, 64> LandingPads; |
| 802 | LandingPads.reserve(PadInfos.size()); |
| 803 | for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) |
| 804 | LandingPads.push_back(&PadInfos[i]); |
| 805 | std::sort(LandingPads.begin(), LandingPads.end(), PadLT); |
| 806 | |
| 807 | // Negative type ids index into FilterIds, positive type ids index into |
| 808 | // TypeInfos. The value written for a positive type id is just the type |
| 809 | // id itself. For a negative type id, however, the value written is the |
| 810 | // (negative) byte offset of the corresponding FilterIds entry. The byte |
| 811 | // offset is usually equal to the type id, because the FilterIds entries |
| 812 | // are written using a variable width encoding which outputs one byte per |
| 813 | // entry as long as the value written is not too large, but can differ. |
| 814 | // This kind of complication does not occur for positive type ids because |
| 815 | // type infos are output using a fixed width encoding. |
| 816 | // FilterOffsets[i] holds the byte offset corresponding to FilterIds[i]. |
| 817 | SmallVector<int, 16> FilterOffsets; |
| 818 | FilterOffsets.reserve(FilterIds.size()); |
| 819 | int Offset = -1; |
| 820 | for(std::vector<unsigned>::const_iterator I = FilterIds.begin(), |
| 821 | E = FilterIds.end(); I != E; ++I) { |
| 822 | FilterOffsets.push_back(Offset); |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 823 | Offset -= TargetAsmInfo::getULEB128Size(*I); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | // Compute the actions table and gather the first action index for each |
| 827 | // landing pad site. |
| 828 | SmallVector<ActionEntry, 32> Actions; |
| 829 | SmallVector<unsigned, 64> FirstActions; |
| 830 | FirstActions.reserve(LandingPads.size()); |
| 831 | |
| 832 | int FirstAction = 0; |
| 833 | unsigned SizeActions = 0; |
| 834 | for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { |
| 835 | const LandingPadInfo *LP = LandingPads[i]; |
| 836 | const std::vector<int> &TypeIds = LP->TypeIds; |
| 837 | const unsigned NumShared = i ? SharedTypeIds(LP, LandingPads[i-1]) : 0; |
| 838 | unsigned SizeSiteActions = 0; |
| 839 | |
| 840 | if (NumShared < TypeIds.size()) { |
| 841 | unsigned SizeAction = 0; |
| 842 | ActionEntry *PrevAction = 0; |
| 843 | |
| 844 | if (NumShared) { |
| 845 | const unsigned SizePrevIds = LandingPads[i-1]->TypeIds.size(); |
| 846 | assert(Actions.size()); |
| 847 | PrevAction = &Actions.back(); |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 848 | SizeAction = TargetAsmInfo::getSLEB128Size(PrevAction->NextAction) + |
| 849 | TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 850 | for (unsigned j = NumShared; j != SizePrevIds; ++j) { |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 851 | SizeAction -= TargetAsmInfo::getSLEB128Size(PrevAction->ValueForTypeID); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 852 | SizeAction += -PrevAction->NextAction; |
| 853 | PrevAction = PrevAction->Previous; |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | // Compute the actions. |
| 858 | for (unsigned I = NumShared, M = TypeIds.size(); I != M; ++I) { |
| 859 | int TypeID = TypeIds[I]; |
| 860 | assert(-1-TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); |
| 861 | int ValueForTypeID = TypeID < 0 ? FilterOffsets[-1 - TypeID] : TypeID; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 862 | unsigned SizeTypeID = TargetAsmInfo::getSLEB128Size(ValueForTypeID); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 863 | |
| 864 | int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 865 | SizeAction = SizeTypeID + TargetAsmInfo::getSLEB128Size(NextAction); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 866 | SizeSiteActions += SizeAction; |
| 867 | |
| 868 | ActionEntry Action = {ValueForTypeID, NextAction, PrevAction}; |
| 869 | Actions.push_back(Action); |
| 870 | |
| 871 | PrevAction = &Actions.back(); |
| 872 | } |
| 873 | |
| 874 | // Record the first action of the landing pad site. |
| 875 | FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; |
| 876 | } // else identical - re-use previous FirstAction |
| 877 | |
| 878 | FirstActions.push_back(FirstAction); |
| 879 | |
| 880 | // Compute this sites contribution to size. |
| 881 | SizeActions += SizeSiteActions; |
| 882 | } |
| 883 | |
| 884 | // Compute the call-site table. Entries must be ordered by address. |
| 885 | SmallVector<CallSiteEntry, 64> CallSites; |
| 886 | |
| 887 | RangeMapType PadMap; |
| 888 | for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { |
| 889 | const LandingPadInfo *LandingPad = LandingPads[i]; |
| 890 | for (unsigned j=0, E = LandingPad->BeginLabels.size(); j != E; ++j) { |
| 891 | unsigned BeginLabel = LandingPad->BeginLabels[j]; |
| 892 | assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); |
| 893 | PadRange P = { i, j }; |
| 894 | PadMap[BeginLabel] = P; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | bool MayThrow = false; |
| 899 | unsigned LastLabel = 0; |
| 900 | for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); |
| 901 | I != E; ++I) { |
| 902 | for (MachineBasicBlock::const_iterator MI = I->begin(), E = I->end(); |
| 903 | MI != E; ++MI) { |
Dan Gohman | 4406604 | 2008-07-01 00:05:16 +0000 | [diff] [blame] | 904 | if (!MI->isLabel()) { |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 905 | MayThrow |= MI->getDesc().isCall(); |
| 906 | continue; |
| 907 | } |
| 908 | |
| 909 | unsigned BeginLabel = MI->getOperand(0).getImm(); |
| 910 | assert(BeginLabel && "Invalid label!"); |
| 911 | |
| 912 | if (BeginLabel == LastLabel) |
| 913 | MayThrow = false; |
| 914 | |
| 915 | RangeMapType::iterator L = PadMap.find(BeginLabel); |
| 916 | |
| 917 | if (L == PadMap.end()) |
| 918 | continue; |
| 919 | |
| 920 | PadRange P = L->second; |
| 921 | const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; |
| 922 | |
| 923 | assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && |
| 924 | "Inconsistent landing pad map!"); |
| 925 | |
| 926 | // If some instruction between the previous try-range and this one may |
| 927 | // throw, create a call-site entry with no landing pad for the region |
| 928 | // between the try-ranges. |
| 929 | if (MayThrow) { |
| 930 | CallSiteEntry Site = {LastLabel, BeginLabel, 0, 0}; |
| 931 | CallSites.push_back(Site); |
| 932 | } |
| 933 | |
| 934 | LastLabel = LandingPad->EndLabels[P.RangeIndex]; |
| 935 | CallSiteEntry Site = {BeginLabel, LastLabel, |
| 936 | LandingPad->LandingPadLabel, FirstActions[P.PadIndex]}; |
| 937 | |
| 938 | assert(Site.BeginLabel && Site.EndLabel && Site.PadLabel && |
| 939 | "Invalid landing pad!"); |
| 940 | |
| 941 | // Try to merge with the previous call-site. |
| 942 | if (CallSites.size()) { |
Dan Gohman | 719de53 | 2008-06-21 22:00:54 +0000 | [diff] [blame] | 943 | CallSiteEntry &Prev = CallSites.back(); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 944 | if (Site.PadLabel == Prev.PadLabel && Site.Action == Prev.Action) { |
| 945 | // Extend the range of the previous entry. |
| 946 | Prev.EndLabel = Site.EndLabel; |
| 947 | continue; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | // Otherwise, create a new call-site. |
| 952 | CallSites.push_back(Site); |
| 953 | } |
| 954 | } |
| 955 | // If some instruction between the previous try-range and the end of the |
| 956 | // function may throw, create a call-site entry with no landing pad for the |
| 957 | // region following the try-range. |
| 958 | if (MayThrow) { |
| 959 | CallSiteEntry Site = {LastLabel, 0, 0, 0}; |
| 960 | CallSites.push_back(Site); |
| 961 | } |
| 962 | |
| 963 | // Final tallies. |
| 964 | unsigned SizeSites = CallSites.size() * (sizeof(int32_t) + // Site start. |
| 965 | sizeof(int32_t) + // Site length. |
| 966 | sizeof(int32_t)); // Landing pad. |
| 967 | for (unsigned i = 0, e = CallSites.size(); i < e; ++i) |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 968 | SizeSites += TargetAsmInfo::getULEB128Size(CallSites[i].Action); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 969 | |
| 970 | unsigned SizeTypes = TypeInfos.size() * TD->getPointerSize(); |
| 971 | |
| 972 | unsigned TypeOffset = sizeof(int8_t) + // Call site format |
| 973 | // Call-site table length |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 974 | TargetAsmInfo::getULEB128Size(SizeSites) + |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 975 | SizeSites + SizeActions + SizeTypes; |
| 976 | |
| 977 | unsigned TotalSize = sizeof(int8_t) + // LPStart format |
| 978 | sizeof(int8_t) + // TType format |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 979 | TargetAsmInfo::getULEB128Size(TypeOffset) + // TType base offset |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 980 | TypeOffset; |
| 981 | |
| 982 | unsigned SizeAlign = (4 - TotalSize) & 3; |
| 983 | |
| 984 | // Begin the exception table. |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 985 | FinalSize = RoundUpToAlign(FinalSize, 4); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 986 | for (unsigned i = 0; i != SizeAlign; ++i) { |
| 987 | ++FinalSize; |
| 988 | } |
| 989 | |
| 990 | unsigned PointerSize = TD->getPointerSize(); |
| 991 | |
| 992 | // Emit the header. |
| 993 | ++FinalSize; |
| 994 | // Asm->EOL("LPStart format (DW_EH_PE_omit)"); |
| 995 | ++FinalSize; |
| 996 | // Asm->EOL("TType format (DW_EH_PE_absptr)"); |
| 997 | ++FinalSize; |
| 998 | // Asm->EOL("TType base offset"); |
| 999 | ++FinalSize; |
| 1000 | // Asm->EOL("Call site format (DW_EH_PE_udata4)"); |
| 1001 | ++FinalSize; |
| 1002 | // Asm->EOL("Call-site table length"); |
| 1003 | |
| 1004 | // Emit the landing pad site information. |
| 1005 | for (unsigned i = 0; i < CallSites.size(); ++i) { |
| 1006 | CallSiteEntry &S = CallSites[i]; |
| 1007 | |
| 1008 | // Asm->EOL("Region start"); |
| 1009 | FinalSize += PointerSize; |
| 1010 | |
| 1011 | //Asm->EOL("Region length"); |
| 1012 | FinalSize += PointerSize; |
| 1013 | |
| 1014 | // Asm->EOL("Landing pad"); |
| 1015 | FinalSize += PointerSize; |
| 1016 | |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 1017 | FinalSize += TargetAsmInfo::getULEB128Size(S.Action); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 1018 | // Asm->EOL("Action"); |
| 1019 | } |
| 1020 | |
| 1021 | // Emit the actions. |
| 1022 | for (unsigned I = 0, N = Actions.size(); I != N; ++I) { |
| 1023 | ActionEntry &Action = Actions[I]; |
| 1024 | |
| 1025 | //Asm->EOL("TypeInfo index"); |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 1026 | FinalSize += TargetAsmInfo::getSLEB128Size(Action.ValueForTypeID); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 1027 | //Asm->EOL("Next action"); |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 1028 | FinalSize += TargetAsmInfo::getSLEB128Size(Action.NextAction); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | // Emit the type ids. |
| 1032 | for (unsigned M = TypeInfos.size(); M; --M) { |
| 1033 | // Asm->EOL("TypeInfo"); |
| 1034 | FinalSize += PointerSize; |
| 1035 | } |
| 1036 | |
| 1037 | // Emit the filter typeids. |
| 1038 | for (unsigned j = 0, M = FilterIds.size(); j < M; ++j) { |
| 1039 | unsigned TypeID = FilterIds[j]; |
Anton Korobeynikov | ffe31d7 | 2008-08-16 12:57:46 +0000 | [diff] [blame] | 1040 | FinalSize += TargetAsmInfo::getULEB128Size(TypeID); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 1041 | //Asm->EOL("Filter TypeInfo index"); |
| 1042 | } |
| 1043 | |
Nicolas Geoffray | 5913e6c | 2008-04-20 17:44:19 +0000 | [diff] [blame] | 1044 | FinalSize = RoundUpToAlign(FinalSize, 4); |
Nicolas Geoffray | dc17ab2 | 2008-04-18 20:59:31 +0000 | [diff] [blame] | 1045 | |
| 1046 | return FinalSize; |
| 1047 | } |