John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame^] | 1 | //===-- CGException.h - Classes for exceptions IR generation ----*- C++ -*-===// |
| 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 | // These classes support the generation of LLVM IR for exceptions in |
| 11 | // C++ and Objective C. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef CLANG_CODEGEN_CGEXCEPTION_H |
| 16 | #define CLANG_CODEGEN_CGEXCEPTION_H |
| 17 | |
| 18 | /// EHScopeStack is defined in CodeGenFunction.h, but its |
| 19 | /// implementation is in this file and in CGException.cpp. |
| 20 | #include "CodeGenFunction.h" |
| 21 | |
| 22 | namespace llvm { |
| 23 | class Value; |
| 24 | class BasicBlock; |
| 25 | } |
| 26 | |
| 27 | namespace clang { |
| 28 | namespace CodeGen { |
| 29 | |
| 30 | /// A protected scope for zero-cost EH handling. |
| 31 | class EHScope { |
| 32 | llvm::BasicBlock *CachedLandingPad; |
| 33 | |
| 34 | unsigned K : 2; |
| 35 | |
| 36 | protected: |
| 37 | enum { BitsRemaining = 30 }; |
| 38 | |
| 39 | public: |
| 40 | enum Kind { Cleanup, Catch, Terminate, Filter }; |
| 41 | |
| 42 | EHScope(Kind K) : CachedLandingPad(0), K(K) {} |
| 43 | |
| 44 | Kind getKind() const { return static_cast<Kind>(K); } |
| 45 | |
| 46 | llvm::BasicBlock *getCachedLandingPad() const { |
| 47 | return CachedLandingPad; |
| 48 | } |
| 49 | |
| 50 | void setCachedLandingPad(llvm::BasicBlock *Block) { |
| 51 | CachedLandingPad = Block; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | /// A scope which attempts to handle some, possibly all, types of |
| 56 | /// exceptions. |
| 57 | /// |
| 58 | /// Objective C @finally blocks are represented using a cleanup scope |
| 59 | /// after the catch scope. |
| 60 | class EHCatchScope : public EHScope { |
| 61 | unsigned NumHandlers : BitsRemaining; |
| 62 | |
| 63 | // In effect, we have a flexible array member |
| 64 | // Handler Handlers[0]; |
| 65 | // But that's only standard in C99, not C++, so we have to do |
| 66 | // annoying pointer arithmetic instead. |
| 67 | |
| 68 | public: |
| 69 | struct Handler { |
| 70 | /// A type info value, or null (C++ null, not an LLVM null pointer) |
| 71 | /// for a catch-all. |
| 72 | llvm::Value *Type; |
| 73 | |
| 74 | /// The catch handler for this type. |
| 75 | llvm::BasicBlock *Block; |
| 76 | |
| 77 | static Handler make(llvm::Value *Type, llvm::BasicBlock *Block) { |
| 78 | Handler Temp; |
| 79 | Temp.Type = Type; |
| 80 | Temp.Block = Block; |
| 81 | return Temp; |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | private: |
| 86 | Handler *getHandlers() { |
| 87 | return reinterpret_cast<Handler*>(this+1); |
| 88 | } |
| 89 | |
| 90 | const Handler *getHandlers() const { |
| 91 | return reinterpret_cast<const Handler*>(this+1); |
| 92 | } |
| 93 | |
| 94 | public: |
| 95 | static size_t getSizeForNumHandlers(unsigned N) { |
| 96 | return sizeof(EHCatchScope) + N * sizeof(Handler); |
| 97 | } |
| 98 | |
| 99 | EHCatchScope(unsigned NumHandlers) |
| 100 | : EHScope(Catch), NumHandlers(NumHandlers) { |
| 101 | } |
| 102 | |
| 103 | unsigned getNumHandlers() const { |
| 104 | return NumHandlers; |
| 105 | } |
| 106 | |
| 107 | void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block) { |
| 108 | setHandler(I, /*catchall*/ 0, Block); |
| 109 | } |
| 110 | |
| 111 | void setHandler(unsigned I, llvm::Value *Type, llvm::BasicBlock *Block) { |
| 112 | assert(I < getNumHandlers()); |
| 113 | getHandlers()[I] = Handler::make(Type, Block); |
| 114 | } |
| 115 | |
| 116 | const Handler &getHandler(unsigned I) const { |
| 117 | assert(I < getNumHandlers()); |
| 118 | return getHandlers()[I]; |
| 119 | } |
| 120 | |
| 121 | typedef const Handler *iterator; |
| 122 | iterator begin() const { return getHandlers(); } |
| 123 | iterator end() const { return getHandlers() + getNumHandlers(); } |
| 124 | |
| 125 | static bool classof(const EHScope *Scope) { |
| 126 | return Scope->getKind() == Catch; |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | /// A scope which needs to execute some code if we try to unwind --- |
| 131 | /// either normally, via the EH mechanism, or both --- through it. |
| 132 | class EHCleanupScope : public EHScope { |
| 133 | /// The number of fixups required by enclosing scopes (not including |
| 134 | /// this one). If this is the top cleanup scope, all the fixups |
| 135 | /// from this index onwards belong to this scope. |
| 136 | unsigned FixupDepth : BitsRemaining; |
| 137 | |
| 138 | /// The nearest normal cleanup scope enclosing this one. |
| 139 | EHScopeStack::stable_iterator EnclosingNormal; |
| 140 | |
| 141 | /// The nearest EH cleanup scope enclosing this one. |
| 142 | EHScopeStack::stable_iterator EnclosingEH; |
| 143 | |
| 144 | llvm::BasicBlock *NormalEntry; |
| 145 | llvm::BasicBlock *NormalExit; |
| 146 | llvm::BasicBlock *EHEntry; |
| 147 | llvm::BasicBlock *EHExit; |
| 148 | |
| 149 | public: |
| 150 | static size_t getSize() { return sizeof(EHCleanupScope); } |
| 151 | |
| 152 | EHCleanupScope(unsigned FixupDepth, |
| 153 | EHScopeStack::stable_iterator EnclosingNormal, |
| 154 | EHScopeStack::stable_iterator EnclosingEH, |
| 155 | llvm::BasicBlock *NormalEntry, llvm::BasicBlock *NormalExit, |
| 156 | llvm::BasicBlock *EHEntry, llvm::BasicBlock *EHExit) |
| 157 | : EHScope(Cleanup), FixupDepth(FixupDepth), |
| 158 | EnclosingNormal(EnclosingNormal), EnclosingEH(EnclosingEH), |
| 159 | NormalEntry(NormalEntry), NormalExit(NormalExit), |
| 160 | EHEntry(EHEntry), EHExit(EHExit) { |
| 161 | assert((NormalEntry != 0) == (NormalExit != 0)); |
| 162 | assert((EHEntry != 0) == (EHExit != 0)); |
| 163 | } |
| 164 | |
| 165 | bool isNormalCleanup() const { return NormalEntry != 0; } |
| 166 | bool isEHCleanup() const { return EHEntry != 0; } |
| 167 | |
| 168 | llvm::BasicBlock *getNormalEntry() const { return NormalEntry; } |
| 169 | llvm::BasicBlock *getNormalExit() const { return NormalExit; } |
| 170 | llvm::BasicBlock *getEHEntry() const { return EHEntry; } |
| 171 | llvm::BasicBlock *getEHExit() const { return EHExit; } |
| 172 | unsigned getFixupDepth() const { return FixupDepth; } |
| 173 | EHScopeStack::stable_iterator getEnclosingNormalCleanup() const { |
| 174 | return EnclosingNormal; |
| 175 | } |
| 176 | EHScopeStack::stable_iterator getEnclosingEHCleanup() const { |
| 177 | return EnclosingEH; |
| 178 | } |
| 179 | |
| 180 | static bool classof(const EHScope *Scope) { |
| 181 | return Scope->getKind() == Cleanup; |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | /// An exceptions scope which filters exceptions thrown through it. |
| 186 | /// Only exceptions matching the filter types will be permitted to be |
| 187 | /// thrown. |
| 188 | /// |
| 189 | /// This is used to implement C++ exception specifications. |
| 190 | class EHFilterScope : public EHScope { |
| 191 | unsigned NumFilters : BitsRemaining; |
| 192 | |
| 193 | // Essentially ends in a flexible array member: |
| 194 | // llvm::Value *FilterTypes[0]; |
| 195 | |
| 196 | llvm::Value **getFilters() { |
| 197 | return reinterpret_cast<llvm::Value**>(this+1); |
| 198 | } |
| 199 | |
| 200 | llvm::Value * const *getFilters() const { |
| 201 | return reinterpret_cast<llvm::Value* const *>(this+1); |
| 202 | } |
| 203 | |
| 204 | public: |
| 205 | EHFilterScope(unsigned NumFilters) : |
| 206 | EHScope(Filter), NumFilters(NumFilters) {} |
| 207 | |
| 208 | static size_t getSizeForNumFilters(unsigned NumFilters) { |
| 209 | return sizeof(EHFilterScope) + NumFilters * sizeof(llvm::Value*); |
| 210 | } |
| 211 | |
| 212 | unsigned getNumFilters() const { return NumFilters; } |
| 213 | |
| 214 | void setFilter(unsigned I, llvm::Value *FilterValue) { |
| 215 | assert(I < getNumFilters()); |
| 216 | getFilters()[I] = FilterValue; |
| 217 | } |
| 218 | |
| 219 | llvm::Value *getFilter(unsigned I) const { |
| 220 | assert(I < getNumFilters()); |
| 221 | return getFilters()[I]; |
| 222 | } |
| 223 | |
| 224 | static bool classof(const EHScope *Scope) { |
| 225 | return Scope->getKind() == Filter; |
| 226 | } |
| 227 | }; |
| 228 | |
| 229 | /// An exceptions scope which calls std::terminate if any exception |
| 230 | /// reaches it. |
| 231 | class EHTerminateScope : public EHScope { |
| 232 | public: |
| 233 | EHTerminateScope() : EHScope(Terminate) {} |
| 234 | static size_t getSize() { return sizeof(EHTerminateScope); } |
| 235 | |
| 236 | static bool classof(const EHScope *Scope) { |
| 237 | return Scope->getKind() == Terminate; |
| 238 | } |
| 239 | }; |
| 240 | |
| 241 | /// A non-stable pointer into the scope stack. |
| 242 | class EHScopeStack::iterator { |
| 243 | char *Ptr; |
| 244 | |
| 245 | friend class EHScopeStack; |
| 246 | explicit iterator(char *Ptr) : Ptr(Ptr) {} |
| 247 | |
| 248 | public: |
| 249 | iterator() : Ptr(0) {} |
| 250 | |
| 251 | EHScope *get() const { |
| 252 | return reinterpret_cast<EHScope*>(Ptr); |
| 253 | } |
| 254 | |
| 255 | EHScope *operator->() const { return get(); } |
| 256 | EHScope &operator*() const { return *get(); } |
| 257 | |
| 258 | iterator &operator++() { |
| 259 | switch (get()->getKind()) { |
| 260 | case EHScope::Catch: |
| 261 | Ptr += EHCatchScope::getSizeForNumHandlers( |
| 262 | static_cast<const EHCatchScope*>(get())->getNumHandlers()); |
| 263 | break; |
| 264 | |
| 265 | case EHScope::Filter: |
| 266 | Ptr += EHFilterScope::getSizeForNumFilters( |
| 267 | static_cast<const EHFilterScope*>(get())->getNumFilters()); |
| 268 | break; |
| 269 | |
| 270 | case EHScope::Cleanup: |
| 271 | Ptr += EHCleanupScope::getSize(); |
| 272 | break; |
| 273 | |
| 274 | case EHScope::Terminate: |
| 275 | Ptr += EHTerminateScope::getSize(); |
| 276 | break; |
| 277 | } |
| 278 | |
| 279 | return *this; |
| 280 | } |
| 281 | |
| 282 | iterator next() { |
| 283 | iterator copy = *this; |
| 284 | ++copy; |
| 285 | return copy; |
| 286 | } |
| 287 | |
| 288 | iterator operator++(int) { |
| 289 | iterator copy = *this; |
| 290 | operator++(); |
| 291 | return copy; |
| 292 | } |
| 293 | |
| 294 | bool operator==(iterator other) const { return Ptr == other.Ptr; } |
| 295 | bool operator!=(iterator other) const { return Ptr != other.Ptr; } |
| 296 | }; |
| 297 | |
| 298 | inline EHScopeStack::iterator EHScopeStack::begin() const { |
| 299 | return iterator(StartOfData); |
| 300 | } |
| 301 | |
| 302 | inline EHScopeStack::iterator EHScopeStack::end() const { |
| 303 | return iterator(EndOfBuffer); |
| 304 | } |
| 305 | |
| 306 | inline void EHScopeStack::popCatch() { |
| 307 | assert(!empty() && "popping exception stack when not empty"); |
| 308 | |
| 309 | assert(isa<EHCatchScope>(*begin())); |
| 310 | StartOfData += EHCatchScope::getSizeForNumHandlers( |
| 311 | cast<EHCatchScope>(*begin()).getNumHandlers()); |
| 312 | |
| 313 | assert(CatchDepth > 0 && "mismatched catch/terminate push/pop"); |
| 314 | CatchDepth--; |
| 315 | } |
| 316 | |
| 317 | inline void EHScopeStack::popTerminate() { |
| 318 | assert(!empty() && "popping exception stack when not empty"); |
| 319 | |
| 320 | assert(isa<EHTerminateScope>(*begin())); |
| 321 | StartOfData += EHTerminateScope::getSize(); |
| 322 | |
| 323 | assert(CatchDepth > 0 && "mismatched catch/terminate push/pop"); |
| 324 | CatchDepth--; |
| 325 | } |
| 326 | |
| 327 | inline EHScopeStack::iterator EHScopeStack::find(stable_iterator sp) const { |
| 328 | assert(sp.isValid() && "finding invalid savepoint"); |
| 329 | assert(sp.Size <= stable_begin().Size && "finding savepoint after pop"); |
| 330 | return iterator(EndOfBuffer - sp.Size); |
| 331 | } |
| 332 | |
| 333 | inline EHScopeStack::stable_iterator |
| 334 | EHScopeStack::stabilize(iterator ir) const { |
| 335 | assert(StartOfData <= ir.Ptr && ir.Ptr <= EndOfBuffer); |
| 336 | return stable_iterator(EndOfBuffer - ir.Ptr); |
| 337 | } |
| 338 | |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | #endif |