| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | #ifndef LLVM_CLANG_LIB_CODEGEN_EHSCOPESTACK_H |
| 16 | #define LLVM_CLANG_LIB_CODEGEN_EHSCOPESTACK_H |
| 17 | |
| 18 | #include "clang/Basic/LLVM.h" |
| 19 | #include "llvm/ADT/STLExtras.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
| 21 | #include "llvm/IR/BasicBlock.h" |
| 22 | #include "llvm/IR/Instructions.h" |
| 23 | #include "llvm/IR/Value.h" |
| 24 | |
| 25 | namespace clang { |
| 26 | namespace CodeGen { |
| 27 | |
| 28 | class CodeGenFunction; |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | struct BranchFixup { |
| 37 | |
| 38 | |
| 39 | |
| 40 | llvm::BasicBlock *OptimisticBranchBlock; |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | llvm::BasicBlock *Destination; |
| 47 | |
| 48 | |
| 49 | unsigned DestinationIndex; |
| 50 | |
| 51 | |
| 52 | llvm::BranchInst *InitialBranch; |
| 53 | }; |
| 54 | |
| 55 | template <class T> struct InvariantValue { |
| 56 | typedef T type; |
| 57 | typedef T saved_type; |
| 58 | static bool needsSaving(type value) { return false; } |
| 59 | static saved_type save(CodeGenFunction &CGF, type value) { return value; } |
| 60 | static type restore(CodeGenFunction &CGF, saved_type value) { return value; } |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | |
| 65 | template <class T> struct DominatingValue : InvariantValue<T> {}; |
| 66 | |
| 67 | template <class T, bool mightBeInstruction = |
| 68 | std::is_base_of<llvm::Value, T>::value && |
| 69 | !std::is_base_of<llvm::Constant, T>::value && |
| 70 | !std::is_base_of<llvm::BasicBlock, T>::value> |
| 71 | struct DominatingPointer; |
| 72 | template <class T> struct DominatingPointer<T,false> : InvariantValue<T*> {}; |
| 73 | |
| 74 | |
| 75 | template <class T> struct DominatingValue<T*> : DominatingPointer<T> {}; |
| 76 | |
| 77 | enum CleanupKind : unsigned { |
| 78 | |
| 79 | |
| 80 | EHCleanup = 0x1, |
| 81 | |
| 82 | |
| 83 | |
| 84 | NormalCleanup = 0x2, |
| 85 | |
| 86 | NormalAndEHCleanup = EHCleanup | NormalCleanup, |
| 87 | |
| 88 | InactiveCleanup = 0x4, |
| 89 | InactiveEHCleanup = EHCleanup | InactiveCleanup, |
| 90 | InactiveNormalCleanup = NormalCleanup | InactiveCleanup, |
| 91 | InactiveNormalAndEHCleanup = NormalAndEHCleanup | InactiveCleanup, |
| 92 | |
| 93 | LifetimeMarker = 0x8, |
| 94 | NormalEHLifetimeMarker = LifetimeMarker | NormalAndEHCleanup, |
| 95 | }; |
| 96 | |
| 97 | |
| 98 | |
| 99 | class EHScopeStack { |
| 100 | public: |
| 101 | |
| 102 | enum { ScopeStackAlignment = 8 }; |
| 103 | |
| 104 | |
| 105 | |
| 106 | class stable_iterator { |
| 107 | friend class EHScopeStack; |
| 108 | |
| 109 | |
| 110 | ptrdiff_t Size; |
| 111 | |
| 112 | stable_iterator(ptrdiff_t Size) : Size(Size) {} |
| 113 | |
| 114 | public: |
| 115 | static stable_iterator invalid() { return stable_iterator(-1); } |
| 116 | stable_iterator() : Size(-1) {} |
| 117 | |
| 118 | bool isValid() const { return Size >= 0; } |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | bool encloses(stable_iterator I) const { return Size <= I.Size; } |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | bool strictlyEncloses(stable_iterator I) const { return Size < I.Size; } |
| 130 | |
| 131 | friend bool operator==(stable_iterator A, stable_iterator B) { |
| 132 | return A.Size == B.Size; |
| 133 | } |
| 134 | friend bool operator!=(stable_iterator A, stable_iterator B) { |
| 135 | return A.Size != B.Size; |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | class Cleanup { |
| 147 | |
| 148 | virtual void anchor(); |
| 149 | |
| 150 | protected: |
| 151 | ~Cleanup() = default; |
| 152 | |
| 153 | public: |
| 154 | Cleanup(const Cleanup &) = default; |
| 155 | Cleanup(Cleanup &&) {} |
| 156 | Cleanup() = default; |
| 157 | |
| 158 | |
| 159 | class Flags { |
| 160 | enum { |
| 161 | F_IsForEH = 0x1, |
| 162 | F_IsNormalCleanupKind = 0x2, |
| 163 | F_IsEHCleanupKind = 0x4 |
| 164 | }; |
| 165 | unsigned flags; |
| 166 | |
| 167 | public: |
| 168 | Flags() : flags(0) {} |
| 169 | |
| 170 | |
| 171 | bool isForEHCleanup() const { return flags & F_IsForEH; } |
| 172 | bool isForNormalCleanup() const { return !isForEHCleanup(); } |
| 173 | void setIsForEHCleanup() { flags |= F_IsForEH; } |
| 174 | |
| 175 | bool isNormalCleanupKind() const { return flags & F_IsNormalCleanupKind; } |
| 176 | void setIsNormalCleanupKind() { flags |= F_IsNormalCleanupKind; } |
| 177 | |
| 178 | |
| 179 | |
| 180 | bool isEHCleanupKind() const { return flags & F_IsEHCleanupKind; } |
| 181 | void setIsEHCleanupKind() { flags |= F_IsEHCleanupKind; } |
| 182 | }; |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | virtual void Emit(CodeGenFunction &CGF, Flags flags) = 0; |
| 192 | }; |
| 193 | |
| 194 | |
| 195 | |
| 196 | template <class T, class... As> |
| 197 | class ConditionalCleanup final : public Cleanup { |
| 198 | typedef std::tuple<typename DominatingValue<As>::saved_type...> SavedTuple; |
| 199 | SavedTuple Saved; |
| 200 | |
| 201 | template <std::size_t... Is> |
| 202 | T restore(CodeGenFunction &CGF, llvm::index_sequence<Is...>) { |
| 203 | |
| 204 | |
| 205 | return T{DominatingValue<As>::restore(CGF, std::get<Is>(Saved))...}; |
| 206 | } |
| 207 | |
| 208 | void Emit(CodeGenFunction &CGF, Flags flags) override { |
| 209 | restore(CGF, llvm::index_sequence_for<As...>()).Emit(CGF, flags); |
| 210 | } |
| 211 | |
| 212 | public: |
| 213 | ConditionalCleanup(typename DominatingValue<As>::saved_type... A) |
| 214 | : Saved(A...) {} |
| 215 | |
| 216 | ConditionalCleanup(SavedTuple Tuple) : Saved(std::move(Tuple)) {} |
| 217 | }; |
| 218 | |
| 219 | private: |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | char *StartOfBuffer; |
| 228 | |
| 229 | |
| 230 | char *EndOfBuffer; |
| 231 | |
| 232 | |
| 233 | char *StartOfData; |
| 234 | |
| 235 | |
| 236 | stable_iterator InnermostNormalCleanup; |
| 237 | |
| 238 | |
| 239 | stable_iterator InnermostEHScope; |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | SmallVector<BranchFixup, 8> BranchFixups; |
| 259 | |
| 260 | char *allocate(size_t Size); |
| 261 | void deallocate(size_t Size); |
| 262 | |
| 263 | void *pushCleanup(CleanupKind K, size_t DataSize); |
| 264 | |
| 265 | public: |
| 266 | EHScopeStack() : StartOfBuffer(nullptr), EndOfBuffer(nullptr), |
| 267 | StartOfData(nullptr), InnermostNormalCleanup(stable_end()), |
| 268 | InnermostEHScope(stable_end()) {} |
| 269 | ~EHScopeStack() { delete[] StartOfBuffer; } |
| 270 | |
| 271 | |
| 272 | template <class T, class... As> void pushCleanup(CleanupKind Kind, As... A) { |
| 273 | static_assert(alignof(T) <= ScopeStackAlignment, |
| 274 | "Cleanup's alignment is too large."); |
| 275 | void *Buffer = pushCleanup(Kind, sizeof(T)); |
| 276 | Cleanup *Obj = new (Buffer) T(A...); |
| 277 | (void) Obj; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | template <class T, class... As> |
| 282 | void pushCleanupTuple(CleanupKind Kind, std::tuple<As...> A) { |
| 283 | static_assert(alignof(T) <= ScopeStackAlignment, |
| 284 | "Cleanup's alignment is too large."); |
| 285 | void *Buffer = pushCleanup(Kind, sizeof(T)); |
| 286 | Cleanup *Obj = new (Buffer) T(std::move(A)); |
| 287 | (void) Obj; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | template <class T, class... As> |
| 304 | T *(CleanupKind Kind, size_t N, As... A) { |
| 305 | static_assert(alignof(T) <= ScopeStackAlignment, |
| 306 | "Cleanup's alignment is too large."); |
| 307 | void *Buffer = pushCleanup(Kind, sizeof(T) + T::getExtraSize(N)); |
| 308 | return new (Buffer) T(N, A...); |
| 309 | } |
| 310 | |
| 311 | void pushCopyOfCleanup(CleanupKind Kind, const void *Cleanup, size_t Size) { |
| 312 | void *Buffer = pushCleanup(Kind, Size); |
| 313 | std::memcpy(Buffer, Cleanup, Size); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | void popCleanup(); |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | class EHCatchScope *pushCatch(unsigned NumHandlers); |
| 323 | |
| 324 | |
| 325 | void popCatch(); |
| 326 | |
| 327 | |
| 328 | class EHFilterScope *pushFilter(unsigned NumFilters); |
| 329 | |
| 330 | |
| 331 | void popFilter(); |
| 332 | |
| 333 | |
| 334 | void pushTerminate(); |
| 335 | |
| 336 | |
| 337 | void popTerminate(); |
| 338 | |
| 339 | |
| 340 | |
| 341 | bool containsOnlyLifetimeMarkers(stable_iterator Old) const; |
| 342 | |
| 343 | |
| 344 | bool empty() const { return StartOfData == EndOfBuffer; } |
| 345 | |
| 346 | bool requiresLandingPad() const; |
| 347 | |
| 348 | |
| 349 | bool hasNormalCleanups() const { |
| 350 | return InnermostNormalCleanup != stable_end(); |
| 351 | } |
| 352 | |
| 353 | |
| 354 | |
| 355 | stable_iterator getInnermostNormalCleanup() const { |
| 356 | return InnermostNormalCleanup; |
| 357 | } |
| 358 | stable_iterator getInnermostActiveNormalCleanup() const; |
| 359 | |
| 360 | stable_iterator getInnermostEHScope() const { |
| 361 | return InnermostEHScope; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | class iterator; |
| 368 | |
| 369 | |
| 370 | iterator begin() const; |
| 371 | |
| 372 | |
| 373 | iterator end() const; |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | stable_iterator stable_begin() const { |
| 379 | return stable_iterator(EndOfBuffer - StartOfData); |
| 380 | } |
| 381 | |
| 382 | |
| 383 | static stable_iterator stable_end() { |
| 384 | return stable_iterator(0); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | stable_iterator stabilize(iterator it) const; |
| 389 | |
| 390 | |
| 391 | |
| 392 | iterator find(stable_iterator save) const; |
| 393 | |
| 394 | |
| 395 | BranchFixup &addBranchFixup() { |
| 396 | (0) . __assert_fail ("hasNormalCleanups() && \"adding fixup in scope without cleanups\"", "/home/seafit/code_projects/clang_source/clang/lib/CodeGen/EHScopeStack.h", 396, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(hasNormalCleanups() && "adding fixup in scope without cleanups"); |
| 397 | BranchFixups.push_back(BranchFixup()); |
| 398 | return BranchFixups.back(); |
| 399 | } |
| 400 | |
| 401 | unsigned getNumBranchFixups() const { return BranchFixups.size(); } |
| 402 | BranchFixup &getBranchFixup(unsigned I) { |
| 403 | assert(I < getNumBranchFixups()); |
| 404 | return BranchFixups[I]; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | void popNullFixups(); |
| 411 | |
| 412 | |
| 413 | |
| 414 | void clearFixups() { BranchFixups.clear(); } |
| 415 | }; |
| 416 | |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | #endif |
| 421 | |