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