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