aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/CGException.h
blob: 80739cd8d73ef55ff03af8070c6eb94e758760e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//===-- CGException.h - Classes for exceptions IR generation ----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// These classes support the generation of LLVM IR for exceptions in
// C++ and Objective C.
//
//===----------------------------------------------------------------------===//

#ifndef CLANG_CODEGEN_CGEXCEPTION_H
#define CLANG_CODEGEN_CGEXCEPTION_H

/// EHScopeStack is defined in CodeGenFunction.h, but its
/// implementation is in this file and in CGException.cpp.
#include "CodeGenFunction.h"

namespace llvm {
  class Value;
  class BasicBlock;
}

namespace clang {
namespace CodeGen {

/// A protected scope for zero-cost EH handling.
class EHScope {
  llvm::BasicBlock *CachedLandingPad;

  unsigned K : 3;

protected:
  enum { BitsRemaining = 29 };

public:
  enum Kind { Cleanup, LazyCleanup, Catch, Terminate, Filter };

  EHScope(Kind K) : CachedLandingPad(0), K(K) {}

  Kind getKind() const { return static_cast<Kind>(K); }

  llvm::BasicBlock *getCachedLandingPad() const {
    return CachedLandingPad;
  }

  void setCachedLandingPad(llvm::BasicBlock *Block) {
    CachedLandingPad = Block;
  }
};

/// A scope which attempts to handle some, possibly all, types of
/// exceptions.
///
/// Objective C @finally blocks are represented using a cleanup scope
/// after the catch scope.
class EHCatchScope : public EHScope {
  unsigned NumHandlers : BitsRemaining;

  // In effect, we have a flexible array member
  //   Handler Handlers[0];
  // But that's only standard in C99, not C++, so we have to do
  // annoying pointer arithmetic instead.

public:
  struct Handler {
    /// A type info value, or null (C++ null, not an LLVM null pointer)
    /// for a catch-all.
    llvm::Value *Type;

    /// The catch handler for this type.
    llvm::BasicBlock *Block;

    static Handler make(llvm::Value *Type, llvm::BasicBlock *Block) {
      Handler Temp;
      Temp.Type = Type;
      Temp.Block = Block;
      return Temp;
    }
  };

private:
  Handler *getHandlers() {
    return reinterpret_cast<Handler*>(this+1);
  }

  const Handler *getHandlers() const {
    return reinterpret_cast<const Handler*>(this+1);
  }

public:
  static size_t getSizeForNumHandlers(unsigned N) {
    return sizeof(EHCatchScope) + N * sizeof(Handler);
  }

  EHCatchScope(unsigned NumHandlers)
    : EHScope(Catch), NumHandlers(NumHandlers) {
  }

  unsigned getNumHandlers() const {
    return NumHandlers;
  }

  void setCatchAllHandler(unsigned I, llvm::BasicBlock *Block) {
    setHandler(I, /*catchall*/ 0, Block);
  }

  void setHandler(unsigned I, llvm::Value *Type, llvm::BasicBlock *Block) {
    assert(I < getNumHandlers());
    getHandlers()[I] = Handler::make(Type, Block);
  }

  const Handler &getHandler(unsigned I) const {
    assert(I < getNumHandlers());
    return getHandlers()[I];
  }

  typedef const Handler *iterator;
  iterator begin() const { return getHandlers(); }
  iterator end() const { return getHandlers() + getNumHandlers(); }

  static bool classof(const EHScope *Scope) {
    return Scope->getKind() == Catch;
  }
};

/// A cleanup scope which generates the cleanup blocks lazily.
class EHLazyCleanupScope : public EHScope {
  /// Whether this cleanup needs to be run along normal edges.
  bool IsNormalCleanup : 1;

  /// Whether this cleanup needs to be run along exception edges.
  bool IsEHCleanup : 1;

  /// The amount of extra storage needed by the LazyCleanup.
  /// Always a multiple of the scope-stack alignment.
  unsigned CleanupSize : 12;

  /// The number of fixups required by enclosing scopes (not including
  /// this one).  If this is the top cleanup scope, all the fixups
  /// from this index onwards belong to this scope.
  unsigned FixupDepth : BitsRemaining - 14;

  /// The nearest normal cleanup scope enclosing this one.
  EHScopeStack::stable_iterator EnclosingNormal;

  /// The nearest EH cleanup scope enclosing this one.
  EHScopeStack::stable_iterator EnclosingEH;

  /// The dual entry/exit block along the normal edge.  This is lazily
  /// created if needed before the cleanup is popped.
  llvm::BasicBlock *NormalBlock;

  /// The dual entry/exit block along the EH edge.  This is lazily
  /// created if needed before the cleanup is popped.
  llvm::BasicBlock *EHBlock;

public:
  /// Gets the size required for a lazy cleanup scope with the given
  /// cleanup-data requirements.
  static size_t getSizeForCleanupSize(size_t Size) {
    return sizeof(EHLazyCleanupScope) + Size;
  }

  size_t getAllocatedSize() const {
    return sizeof(EHLazyCleanupScope) + CleanupSize;
  }

  EHLazyCleanupScope(bool IsNormal, bool IsEH, unsigned CleanupSize,
                     unsigned FixupDepth,
                     EHScopeStack::stable_iterator EnclosingNormal,
                     EHScopeStack::stable_iterator EnclosingEH)
    : EHScope(EHScope::LazyCleanup),
      IsNormalCleanup(IsNormal), IsEHCleanup(IsEH),
      CleanupSize(CleanupSize), FixupDepth(FixupDepth),
      EnclosingNormal(EnclosingNormal), EnclosingEH(EnclosingEH),
      NormalBlock(0), EHBlock(0)
  {}

  bool isNormalCleanup() const { return IsNormalCleanup; }
  llvm::BasicBlock *getNormalBlock() const { return NormalBlock; }
  void setNormalBlock(llvm::BasicBlock *BB) { NormalBlock = BB; }

  bool isEHCleanup() const { return IsEHCleanup; }
  llvm::BasicBlock *getEHBlock() const { return EHBlock; }
  void setEHBlock(llvm::BasicBlock *BB) { EHBlock = BB; }

  unsigned getFixupDepth() const { return FixupDepth; }
  EHScopeStack::stable_iterator getEnclosingNormalCleanup() const {
    return EnclosingNormal;
  }
  EHScopeStack::stable_iterator getEnclosingEHCleanup() const {
    return EnclosingEH;
  }

  size_t getCleanupSize() const { return CleanupSize; }
  void *getCleanupBuffer() { return this + 1; }

  EHScopeStack::LazyCleanup *getCleanup() {
    return reinterpret_cast<EHScopeStack::LazyCleanup*>(getCleanupBuffer());
  }

  static bool classof(const EHScope *Scope) {
    return (Scope->getKind() == LazyCleanup);
  }
};

/// A scope which needs to execute some code if we try to unwind ---
/// either normally, via the EH mechanism, or both --- through it.
class EHCleanupScope : public EHScope {
  /// The number of fixups required by enclosing scopes (not including
  /// this one).  If this is the top cleanup scope, all the fixups
  /// from this index onwards belong to this scope.
  unsigned FixupDepth : BitsRemaining;

  /// The nearest normal cleanup scope enclosing this one.
  EHScopeStack::stable_iterator EnclosingNormal;

  /// The nearest EH cleanup scope enclosing this one.
  EHScopeStack::stable_iterator EnclosingEH;

  llvm::BasicBlock *NormalEntry;
  llvm::BasicBlock *NormalExit;
  llvm::BasicBlock *EHEntry;
  llvm::BasicBlock *EHExit;

public:
  static size_t getSize() { return sizeof(EHCleanupScope); }

  EHCleanupScope(unsigned FixupDepth,
                 EHScopeStack::stable_iterator EnclosingNormal,
                 EHScopeStack::stable_iterator EnclosingEH,
                 llvm::BasicBlock *NormalEntry, llvm::BasicBlock *NormalExit,
                 llvm::BasicBlock *EHEntry, llvm::BasicBlock *EHExit)
    : EHScope(Cleanup), FixupDepth(FixupDepth),
      EnclosingNormal(EnclosingNormal), EnclosingEH(EnclosingEH),
      NormalEntry(NormalEntry), NormalExit(NormalExit),
      EHEntry(EHEntry), EHExit(EHExit) {
    assert((NormalEntry != 0) == (NormalExit != 0));
    assert((EHEntry != 0) == (EHExit != 0));
  }

  bool isNormalCleanup() const { return NormalEntry != 0; }
  bool isEHCleanup() const { return EHEntry != 0; }

  llvm::BasicBlock *getNormalEntry() const { return NormalEntry; }
  llvm::BasicBlock *getNormalExit() const { return NormalExit; }
  llvm::BasicBlock *getEHEntry() const { return EHEntry; }
  llvm::BasicBlock *getEHExit() const { return EHExit; }
  unsigned getFixupDepth() const { return FixupDepth; }
  EHScopeStack::stable_iterator getEnclosingNormalCleanup() const {
    return EnclosingNormal;
  }
  EHScopeStack::stable_iterator getEnclosingEHCleanup() const {
    return EnclosingEH;
  }

  static bool classof(const EHScope *Scope) {
    return Scope->getKind() == Cleanup;
  }
};

/// An exceptions scope which filters exceptions thrown through it.
/// Only exceptions matching the filter types will be permitted to be
/// thrown.
///
/// This is used to implement C++ exception specifications.
class EHFilterScope : public EHScope {
  unsigned NumFilters : BitsRemaining;

  // Essentially ends in a flexible array member:
  // llvm::Value *FilterTypes[0];

  llvm::Value **getFilters() {
    return reinterpret_cast<llvm::Value**>(this+1);
  }

  llvm::Value * const *getFilters() const {
    return reinterpret_cast<llvm::Value* const *>(this+1);
  }

public:
  EHFilterScope(unsigned NumFilters) :
    EHScope(Filter), NumFilters(NumFilters) {}

  static size_t getSizeForNumFilters(unsigned NumFilters) {
    return sizeof(EHFilterScope) + NumFilters * sizeof(llvm::Value*);
  }

  unsigned getNumFilters() const { return NumFilters; }

  void setFilter(unsigned I, llvm::Value *FilterValue) {
    assert(I < getNumFilters());
    getFilters()[I] = FilterValue;
  }

  llvm::Value *getFilter(unsigned I) const {
    assert(I < getNumFilters());
    return getFilters()[I];
  }

  static bool classof(const EHScope *Scope) {
    return Scope->getKind() == Filter;
  }
};

/// An exceptions scope which calls std::terminate if any exception
/// reaches it.
class EHTerminateScope : public EHScope {
public:
  EHTerminateScope() : EHScope(Terminate) {}
  static size_t getSize() { return sizeof(EHTerminateScope); }

  static bool classof(const EHScope *Scope) {
    return Scope->getKind() == Terminate;
  }
};

/// A non-stable pointer into the scope stack.
class EHScopeStack::iterator {
  char *Ptr;

  friend class EHScopeStack;
  explicit iterator(char *Ptr) : Ptr(Ptr) {}

public:
  iterator() : Ptr(0) {}

  EHScope *get() const { 
    return reinterpret_cast<EHScope*>(Ptr);
  }

  EHScope *operator->() const { return get(); }
  EHScope &operator*() const { return *get(); }

  iterator &operator++() {
    switch (get()->getKind()) {
    case EHScope::Catch:
      Ptr += EHCatchScope::getSizeForNumHandlers(
          static_cast<const EHCatchScope*>(get())->getNumHandlers());
      break;

    case EHScope::Filter:
      Ptr += EHFilterScope::getSizeForNumFilters(
          static_cast<const EHFilterScope*>(get())->getNumFilters());
      break;

    case EHScope::LazyCleanup:
      Ptr += static_cast<const EHLazyCleanupScope*>(get())
        ->getAllocatedSize();
      break;

    case EHScope::Cleanup:
      Ptr += EHCleanupScope::getSize();
      break;

    case EHScope::Terminate:
      Ptr += EHTerminateScope::getSize();
      break;
    }

    return *this;
  }

  iterator next() {
    iterator copy = *this;
    ++copy;
    return copy;
  }

  iterator operator++(int) {
    iterator copy = *this;
    operator++();
    return copy;
  }

  bool operator==(iterator other) const { return Ptr == other.Ptr; }
  bool operator!=(iterator other) const { return Ptr != other.Ptr; }
};

inline EHScopeStack::iterator EHScopeStack::begin() const {
  return iterator(StartOfData);
}

inline EHScopeStack::iterator EHScopeStack::end() const {
  return iterator(EndOfBuffer);
}

inline void EHScopeStack::popCatch() {
  assert(!empty() && "popping exception stack when not empty");

  assert(isa<EHCatchScope>(*begin()));
  StartOfData += EHCatchScope::getSizeForNumHandlers(
                          cast<EHCatchScope>(*begin()).getNumHandlers());

  assert(CatchDepth > 0 && "mismatched catch/terminate push/pop");
  CatchDepth--;
}

inline void EHScopeStack::popTerminate() {
  assert(!empty() && "popping exception stack when not empty");

  assert(isa<EHTerminateScope>(*begin()));
  StartOfData += EHTerminateScope::getSize();

  assert(CatchDepth > 0 && "mismatched catch/terminate push/pop");
  CatchDepth--;
}

inline EHScopeStack::iterator EHScopeStack::find(stable_iterator sp) const {
  assert(sp.isValid() && "finding invalid savepoint");
  assert(sp.Size <= stable_begin().Size && "finding savepoint after pop");
  return iterator(EndOfBuffer - sp.Size);
}

inline EHScopeStack::stable_iterator
EHScopeStack::stabilize(iterator ir) const {
  assert(StartOfData <= ir.Ptr && ir.Ptr <= EndOfBuffer);
  return stable_iterator(EndOfBuffer - ir.Ptr);
}

}
}

#endif