1 //===--- CGException.cpp - Emit LLVM Code for C++ exceptions --------------===//
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 contains code dealing with C++ exception related code generation.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/AST/StmtCXX.h"
15 
16 #include "llvm/Intrinsics.h"
17 
18 #include "CodeGenFunction.h"
19 using namespace clang;
20 using namespace CodeGen;
21 
22 static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) {
23   // void *__cxa_allocate_exception(size_t thrown_size);
24   const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
25   std::vector<const llvm::Type*> Args(1, SizeTy);
26 
27   const llvm::FunctionType *FTy =
28   llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()),
29                           Args, false);
30 
31   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
32 }
33 
34 static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) {
35   // void __cxa_free_exception(void *thrown_exception);
36   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
37   std::vector<const llvm::Type*> Args(1, Int8PtrTy);
38 
39   const llvm::FunctionType *FTy =
40   llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
41                           Args, false);
42 
43   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
44 }
45 
46 static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
47   // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
48   //                  void (*dest) (void *));
49 
50   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
51   std::vector<const llvm::Type*> Args(3, Int8PtrTy);
52 
53   const llvm::FunctionType *FTy =
54     llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
55                             Args, false);
56 
57   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
58 }
59 
60 static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
61   // void __cxa_rethrow();
62 
63   const llvm::FunctionType *FTy =
64     llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
65 
66   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
67 }
68 
69 static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
70   // void* __cxa_begin_catch();
71 
72   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
73   std::vector<const llvm::Type*> Args(1, Int8PtrTy);
74 
75   const llvm::FunctionType *FTy =
76     llvm::FunctionType::get(Int8PtrTy, Args, false);
77 
78   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
79 }
80 
81 static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
82   // void __cxa_end_catch();
83 
84   const llvm::FunctionType *FTy =
85     llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
86 
87   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
88 }
89 
90 static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) {
91   // void __cxa_call_unexepcted(void *thrown_exception);
92 
93   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
94   std::vector<const llvm::Type*> Args(1, Int8PtrTy);
95 
96   const llvm::FunctionType *FTy =
97     llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
98                             Args, false);
99 
100   return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
101 }
102 
103 llvm::Constant *CodeGenFunction::getUnwindResumeOrRethrowFn() {
104   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
105   std::vector<const llvm::Type*> Args(1, Int8PtrTy);
106 
107   const llvm::FunctionType *FTy =
108     llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), Args,
109                             false);
110 
111   if (CGM.getLangOptions().SjLjExceptions)
112     return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
113   return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
114 }
115 
116 static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
117   // void __terminate();
118 
119   const llvm::FunctionType *FTy =
120     llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
121 
122   return CGF.CGM.CreateRuntimeFunction(FTy,
123       CGF.CGM.getLangOptions().CPlusPlus ? "_ZSt9terminatev" : "abort");
124 }
125 
126 static llvm::Constant *getPersonalityFn(CodeGenModule &CGM) {
127   const char *PersonalityFnName = "__gcc_personality_v0";
128   LangOptions Opts = CGM.getLangOptions();
129   if (Opts.CPlusPlus) {
130     if (Opts.SjLjExceptions)
131      PersonalityFnName = "__gxx_personality_sj0";
132     else
133      PersonalityFnName = "__gxx_personality_v0";
134   } else if (Opts.ObjC1) {
135     if (Opts.NeXTRuntime) {
136       if (Opts.ObjCNonFragileABI)
137         PersonalityFnName = "__gcc_personality_v0";
138     } else
139       PersonalityFnName = "__gnu_objc_personality_v0";
140   }
141 
142   llvm::Constant *Personality =
143   CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(
144                                                         CGM.getLLVMContext()),
145                                                     true),
146       PersonalityFnName);
147   return llvm::ConstantExpr::getBitCast(Personality, CGM.PtrToInt8Ty);
148 }
149 
150 // Emits an exception expression into the given location.  This
151 // differs from EmitAnyExprToMem only in that, if a final copy-ctor
152 // call is required, an exception within that copy ctor causes
153 // std::terminate to be invoked.
154 static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *E,
155                              llvm::Value *ExnLoc) {
156   // We want to release the allocated exception object if this
157   // expression throws.  We do this by pushing an EH-only cleanup
158   // block which, furthermore, deactivates itself after the expression
159   // is complete.
160   llvm::AllocaInst *ShouldFreeVar =
161     CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()),
162                          "should-free-exnobj.var");
163   CGF.InitTempAlloca(ShouldFreeVar,
164                      llvm::ConstantInt::getFalse(CGF.getLLVMContext()));
165 
166   // A variable holding the exception pointer.  This is necessary
167   // because the throw expression does not necessarily dominate the
168   // cleanup, for example if it appears in a conditional expression.
169   llvm::AllocaInst *ExnLocVar =
170     CGF.CreateTempAlloca(ExnLoc->getType(), "exnobj.var");
171 
172   llvm::BasicBlock *SavedInvokeDest = CGF.getInvokeDest();
173   {
174     CodeGenFunction::EHCleanupBlock Cleanup(CGF);
175     llvm::BasicBlock *FreeBB = CGF.createBasicBlock("free-exnobj");
176     llvm::BasicBlock *DoneBB = CGF.createBasicBlock("free-exnobj.done");
177 
178     llvm::Value *ShouldFree = CGF.Builder.CreateLoad(ShouldFreeVar,
179                                                      "should-free-exnobj");
180     CGF.Builder.CreateCondBr(ShouldFree, FreeBB, DoneBB);
181     CGF.EmitBlock(FreeBB);
182     llvm::Value *ExnLocLocal = CGF.Builder.CreateLoad(ExnLocVar, "exnobj");
183     CGF.Builder.CreateCall(getFreeExceptionFn(CGF), ExnLocLocal);
184     CGF.EmitBlock(DoneBB);
185   }
186   llvm::BasicBlock *Cleanup = CGF.getInvokeDest();
187 
188   CGF.Builder.CreateStore(ExnLoc, ExnLocVar);
189   CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()),
190                           ShouldFreeVar);
191 
192   // __cxa_allocate_exception returns a void*;  we need to cast this
193   // to the appropriate type for the object.
194   const llvm::Type *Ty = CGF.ConvertType(E->getType())->getPointerTo();
195   llvm::Value *TypedExnLoc = CGF.Builder.CreateBitCast(ExnLoc, Ty);
196 
197   // FIXME: this isn't quite right!  If there's a final unelided call
198   // to a copy constructor, then according to [except.terminate]p1 we
199   // must call std::terminate() if that constructor throws, because
200   // technically that copy occurs after the exception expression is
201   // evaluated but before the exception is caught.  But the best way
202   // to handle that is to teach EmitAggExpr to do the final copy
203   // differently if it can't be elided.
204   CGF.EmitAnyExprToMem(E, TypedExnLoc, /*Volatile*/ false);
205 
206   CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()),
207                           ShouldFreeVar);
208 
209   // Pop the cleanup block if it's still the top of the cleanup stack.
210   // Otherwise, temporaries have been created and our cleanup will get
211   // properly removed in time.
212   // TODO: this is not very resilient.
213   if (CGF.getInvokeDest() == Cleanup)
214     CGF.setInvokeDest(SavedInvokeDest);
215 }
216 
217 // CopyObject - Utility to copy an object.  Calls copy constructor as necessary.
218 // N is casted to the right type.
219 static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
220                        bool WasPointer, bool WasPointerReference,
221                        llvm::Value *E, llvm::Value *N) {
222   // Store the throw exception in the exception object.
223   if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
224     llvm::Value *Value = E;
225     if (!WasPointer)
226       Value = CGF.Builder.CreateLoad(Value);
227     const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
228     if (WasPointerReference) {
229       llvm::Value *Tmp = CGF.CreateTempAlloca(Value->getType(), "catch.param");
230       CGF.Builder.CreateStore(Value, Tmp);
231       Value = Tmp;
232       ValuePtrTy = Value->getType()->getPointerTo(0);
233     }
234     N = CGF.Builder.CreateBitCast(N, ValuePtrTy);
235     CGF.Builder.CreateStore(Value, N);
236   } else {
237     const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
238     const CXXRecordDecl *RD;
239     RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
240     llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
241     if (RD->hasTrivialCopyConstructor()) {
242       CGF.EmitAggregateCopy(This, E, ObjectType);
243     } else if (CXXConstructorDecl *CopyCtor
244                = RD->getCopyConstructor(CGF.getContext(), 0)) {
245       llvm::Value *Src = E;
246 
247       // Stolen from EmitClassAggrMemberwiseCopy
248       llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
249                                                             Ctor_Complete);
250       CallArgList CallArgs;
251       CallArgs.push_back(std::make_pair(RValue::get(This),
252                                       CopyCtor->getThisType(CGF.getContext())));
253 
254       // Push the Src ptr.
255       CallArgs.push_back(std::make_pair(RValue::get(Src),
256                                         CopyCtor->getParamDecl(0)->getType()));
257 
258       const FunctionProtoType *FPT
259         = CopyCtor->getType()->getAs<FunctionProtoType>();
260       CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
261                    Callee, ReturnValueSlot(), CallArgs, CopyCtor);
262     } else
263       llvm_unreachable("uncopyable object");
264   }
265 }
266 
267 void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
268   if (!E->getSubExpr()) {
269     if (getInvokeDest()) {
270       llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
271       Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
272         ->setDoesNotReturn();
273       EmitBlock(Cont);
274     } else
275       Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
276     Builder.CreateUnreachable();
277 
278     // Clear the insertion point to indicate we are in unreachable code.
279     Builder.ClearInsertionPoint();
280     return;
281   }
282 
283   QualType ThrowType = E->getSubExpr()->getType();
284 
285   // Now allocate the exception object.
286   const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
287   uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
288 
289   llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
290   llvm::Value *ExceptionPtr =
291     Builder.CreateCall(AllocExceptionFn,
292                        llvm::ConstantInt::get(SizeTy, TypeSize),
293                        "exception");
294 
295   EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
296 
297   // Now throw the exception.
298   const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
299   llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, true);
300 
301   // The address of the destructor.  If the exception type has a
302   // trivial destructor (or isn't a record), we just pass null.
303   llvm::Constant *Dtor = 0;
304   if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
305     CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
306     if (!Record->hasTrivialDestructor()) {
307       CXXDestructorDecl *DtorD = Record->getDestructor(getContext());
308       Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete);
309       Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
310     }
311   }
312   if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
313 
314   if (getInvokeDest()) {
315     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
316     llvm::InvokeInst *ThrowCall =
317       Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
318                             ExceptionPtr, TypeInfo, Dtor);
319     ThrowCall->setDoesNotReturn();
320     EmitBlock(Cont);
321   } else {
322     llvm::CallInst *ThrowCall =
323       Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
324     ThrowCall->setDoesNotReturn();
325   }
326   Builder.CreateUnreachable();
327 
328   // Clear the insertion point to indicate we are in unreachable code.
329   Builder.ClearInsertionPoint();
330 
331   // FIXME: For now, emit a dummy basic block because expr emitters in generally
332   // are not ready to handle emitting expressions at unreachable points.
333   EnsureInsertPoint();
334 }
335 
336 void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
337   if (!Exceptions)
338     return;
339 
340   const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
341   if (FD == 0)
342     return;
343   const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
344   if (Proto == 0)
345     return;
346 
347   assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
348 
349   if (!Proto->hasExceptionSpec())
350     return;
351 
352   llvm::Constant *Personality = getPersonalityFn(CGM);
353   llvm::Value *llvm_eh_exception =
354     CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
355   llvm::Value *llvm_eh_selector =
356     CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
357   const llvm::IntegerType *Int8Ty;
358   const llvm::PointerType *PtrToInt8Ty;
359   Int8Ty = llvm::Type::getInt8Ty(VMContext);
360   // C string type.  Used in lots of places.
361   PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
362   llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
363   llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
364 
365   llvm::BasicBlock *PrevLandingPad = getInvokeDest();
366   llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
367   llvm::BasicBlock *Match = createBasicBlock("match");
368   llvm::BasicBlock *Unwind = 0;
369 
370   assert(PrevLandingPad == 0 && "EHSpec has invoke context");
371   (void)PrevLandingPad;
372 
373   llvm::BasicBlock *Cont = createBasicBlock("cont");
374 
375   EmitBranchThroughCleanup(Cont);
376 
377   // Emit the statements in the try {} block
378   setInvokeDest(EHSpecHandler);
379 
380   EmitBlock(EHSpecHandler);
381   // Exception object
382   llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
383   llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
384 
385   SelectorArgs.push_back(Exc);
386   SelectorArgs.push_back(Personality);
387   SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
388                                                 Proto->getNumExceptions()+1));
389 
390   for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
391     QualType Ty = Proto->getExceptionType(i);
392     QualType ExceptType
393       = Ty.getNonReferenceType().getUnqualifiedType();
394     llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, true);
395     SelectorArgs.push_back(EHType);
396   }
397   if (Proto->getNumExceptions())
398     SelectorArgs.push_back(Null);
399 
400   // Find which handler was matched.
401   llvm::Value *Selector
402     = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
403                          SelectorArgs.end(), "selector");
404   if (Proto->getNumExceptions()) {
405     Unwind = createBasicBlock("Unwind");
406 
407     Builder.CreateStore(Exc, RethrowPtr);
408     Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
409                                                llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
410                                                                       0)),
411                          Match, Unwind);
412 
413     EmitBlock(Match);
414   }
415   Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
416   Builder.CreateUnreachable();
417 
418   if (Proto->getNumExceptions()) {
419     EmitBlock(Unwind);
420     Builder.CreateCall(getUnwindResumeOrRethrowFn(),
421                        Builder.CreateLoad(RethrowPtr));
422     Builder.CreateUnreachable();
423   }
424 
425   EmitBlock(Cont);
426 }
427 
428 void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
429   if (!Exceptions)
430     return;
431 
432   const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
433   if (FD == 0)
434     return;
435   const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
436   if (Proto == 0)
437     return;
438 
439   if (!Proto->hasExceptionSpec())
440     return;
441 
442   setInvokeDest(0);
443 }
444 
445 void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
446   CXXTryStmtInfo Info = EnterCXXTryStmt(S);
447   EmitStmt(S.getTryBlock());
448   ExitCXXTryStmt(S, Info);
449 }
450 
451 CodeGenFunction::CXXTryStmtInfo
452 CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S) {
453   CXXTryStmtInfo Info;
454   Info.SavedLandingPad = getInvokeDest();
455   Info.HandlerBlock = createBasicBlock("try.handler");
456   Info.FinallyBlock = createBasicBlock("finally");
457 
458   PushCleanupBlock(Info.FinallyBlock);
459   setInvokeDest(Info.HandlerBlock);
460 
461   return Info;
462 }
463 
464 void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S,
465                                      CXXTryStmtInfo TryInfo) {
466   // Pointer to the personality function
467   llvm::Constant *Personality = getPersonalityFn(CGM);
468   llvm::Value *llvm_eh_exception =
469     CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
470   llvm::Value *llvm_eh_selector =
471     CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
472 
473   llvm::BasicBlock *PrevLandingPad = TryInfo.SavedLandingPad;
474   llvm::BasicBlock *TryHandler = TryInfo.HandlerBlock;
475   llvm::BasicBlock *FinallyBlock = TryInfo.FinallyBlock;
476   llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
477   llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
478 
479   // Jump to end if there is no exception
480   EmitBranchThroughCleanup(FinallyEnd);
481 
482   llvm::BasicBlock *TerminateHandler = getTerminateHandler();
483 
484   // Emit the handlers
485   EmitBlock(TryHandler);
486 
487   const llvm::IntegerType *Int8Ty;
488   const llvm::PointerType *PtrToInt8Ty;
489   Int8Ty = llvm::Type::getInt8Ty(VMContext);
490   // C string type.  Used in lots of places.
491   PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
492   llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
493   llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
494   llvm::Value *llvm_eh_typeid_for =
495     CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
496   // Exception object
497   llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
498   llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
499 
500   SelectorArgs.push_back(Exc);
501   SelectorArgs.push_back(Personality);
502 
503   bool HasCatchAll = false;
504   for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
505     const CXXCatchStmt *C = S.getHandler(i);
506     VarDecl *CatchParam = C->getExceptionDecl();
507     if (CatchParam) {
508       // C++ [except.handle]p3 indicates that top-level cv-qualifiers
509       // are ignored.
510       QualType CaughtType = C->getCaughtType().getNonReferenceType();
511       llvm::Value *EHTypeInfo
512         = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType(), true);
513       SelectorArgs.push_back(EHTypeInfo);
514     } else {
515       // null indicates catch all
516       SelectorArgs.push_back(Null);
517       HasCatchAll = true;
518     }
519   }
520 
521   // We use a cleanup unless there was already a catch all.
522   if (!HasCatchAll) {
523     SelectorArgs.push_back(Null);
524   }
525 
526   // Find which handler was matched.
527   llvm::Value *Selector
528     = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
529                          SelectorArgs.end(), "selector");
530   for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
531     const CXXCatchStmt *C = S.getHandler(i);
532     VarDecl *CatchParam = C->getExceptionDecl();
533     Stmt *CatchBody = C->getHandlerBlock();
534 
535     llvm::BasicBlock *Next = 0;
536 
537     if (SelectorArgs[i+2] != Null) {
538       llvm::BasicBlock *Match = createBasicBlock("match");
539       Next = createBasicBlock("catch.next");
540       const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
541       llvm::Value *Id
542         = Builder.CreateCall(llvm_eh_typeid_for,
543                              Builder.CreateBitCast(SelectorArgs[i+2],
544                                                    Int8PtrTy));
545       Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
546                            Match, Next);
547       EmitBlock(Match);
548     }
549 
550     llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
551     llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
552 
553     PushCleanupBlock(MatchEnd);
554     setInvokeDest(MatchHandler);
555 
556     llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
557 
558     {
559       CleanupScope CatchScope(*this);
560       // Bind the catch parameter if it exists.
561       if (CatchParam) {
562         QualType CatchType = CatchParam->getType().getNonReferenceType();
563         setInvokeDest(TerminateHandler);
564         bool WasPointer = true;
565         bool WasPointerReference = false;
566         CatchType = CGM.getContext().getCanonicalType(CatchType);
567         if (CatchType.getTypePtr()->isPointerType()) {
568           if (isa<ReferenceType>(CatchParam->getType()))
569             WasPointerReference = true;
570         } else {
571           if (!isa<ReferenceType>(CatchParam->getType()))
572             WasPointer = false;
573           CatchType = getContext().getPointerType(CatchType);
574         }
575         ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
576         EmitLocalBlockVarDecl(*CatchParam);
577         // FIXME: we need to do this sooner so that the EH region for the
578         // cleanup doesn't start until after the ctor completes, use a decl
579         // init?
580         CopyObject(*this, CatchParam->getType().getNonReferenceType(),
581                    WasPointer, WasPointerReference, ExcObject,
582                    GetAddrOfLocalVar(CatchParam));
583         setInvokeDest(MatchHandler);
584       }
585 
586       EmitStmt(CatchBody);
587     }
588 
589     EmitBranchThroughCleanup(FinallyEnd);
590 
591     EmitBlock(MatchHandler);
592 
593     llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
594     // We are required to emit this call to satisfy LLVM, even
595     // though we don't use the result.
596     llvm::Value *Args[] = {
597       Exc, Personality,
598       llvm::ConstantInt::getNullValue(llvm::Type::getInt32Ty(VMContext))
599     };
600     Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
601     Builder.CreateStore(Exc, RethrowPtr);
602     EmitBranchThroughCleanup(FinallyRethrow);
603 
604     CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
605 
606     EmitBlock(MatchEnd);
607 
608     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
609     Builder.CreateInvoke(getEndCatchFn(*this),
610                          Cont, TerminateHandler,
611                          &Args[0], &Args[0]);
612     EmitBlock(Cont);
613     if (Info.SwitchBlock)
614       EmitBlock(Info.SwitchBlock);
615     if (Info.EndBlock)
616       EmitBlock(Info.EndBlock);
617 
618     Exc = Builder.CreateCall(llvm_eh_exception, "exc");
619     Builder.CreateStore(Exc, RethrowPtr);
620     EmitBranchThroughCleanup(FinallyRethrow);
621 
622     if (Next)
623       EmitBlock(Next);
624   }
625   if (!HasCatchAll) {
626     Builder.CreateStore(Exc, RethrowPtr);
627     EmitBranchThroughCleanup(FinallyRethrow);
628   }
629 
630   CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
631 
632   setInvokeDest(PrevLandingPad);
633 
634   EmitBlock(FinallyBlock);
635 
636   if (Info.SwitchBlock)
637     EmitBlock(Info.SwitchBlock);
638   if (Info.EndBlock)
639     EmitBlock(Info.EndBlock);
640 
641   // Branch around the rethrow code.
642   EmitBranch(FinallyEnd);
643 
644   EmitBlock(FinallyRethrow);
645   // FIXME: Eventually we can chain the handlers together and just do a call
646   // here.
647   if (getInvokeDest()) {
648     llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
649     Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont,
650                          getInvokeDest(),
651                          Builder.CreateLoad(RethrowPtr));
652     EmitBlock(Cont);
653   } else
654     Builder.CreateCall(getUnwindResumeOrRethrowFn(),
655                        Builder.CreateLoad(RethrowPtr));
656 
657   Builder.CreateUnreachable();
658 
659   EmitBlock(FinallyEnd);
660 }
661 
662 CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
663   CGF.setInvokeDest(PreviousInvokeDest);
664 
665   llvm::BasicBlock *EndOfCleanup = CGF.Builder.GetInsertBlock();
666 
667   // Jump to the beginning of the cleanup.
668   CGF.Builder.SetInsertPoint(CleanupHandler, CleanupHandler->begin());
669 
670   // The libstdc++ personality function.
671   // TODO: generalize to work with other libraries.
672   llvm::Constant *Personality = getPersonalityFn(CGF.CGM);
673 
674   // %exception = call i8* @llvm.eh.exception()
675   //   Magic intrinsic which tells gives us a handle to the caught
676   //   exception.
677   llvm::Value *llvm_eh_exception =
678     CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
679   llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
680 
681   llvm::Constant *Null = llvm::ConstantPointerNull::get(CGF.PtrToInt8Ty);
682 
683   // %ignored = call i32 @llvm.eh.selector(i8* %exception,
684   //                                       i8* @__gxx_personality_v0,
685   //                                       i8* null)
686   //   Magic intrinsic which tells LLVM that this invoke landing pad is
687   //   just a cleanup block.
688   llvm::Value *Args[] = { Exc, Personality, Null };
689   llvm::Value *llvm_eh_selector =
690     CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
691   CGF.Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
692 
693   // And then we fall through into the code that the user put there.
694   // Jump back to the end of the cleanup.
695   CGF.Builder.SetInsertPoint(EndOfCleanup);
696 
697   // Rethrow the exception.
698   if (CGF.getInvokeDest()) {
699     llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
700     CGF.Builder.CreateInvoke(CGF.getUnwindResumeOrRethrowFn(), Cont,
701                              CGF.getInvokeDest(), Exc);
702     CGF.EmitBlock(Cont);
703   } else
704     CGF.Builder.CreateCall(CGF.getUnwindResumeOrRethrowFn(), Exc);
705   CGF.Builder.CreateUnreachable();
706 
707   // Resume inserting where we started, but put the new cleanup
708   // handler in place.
709   if (PreviousInsertionBlock)
710     CGF.Builder.SetInsertPoint(PreviousInsertionBlock);
711   else
712     CGF.Builder.ClearInsertionPoint();
713 
714   if (CGF.Exceptions)
715     CGF.setInvokeDest(CleanupHandler);
716 }
717 
718 llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
719   if (TerminateHandler)
720     return TerminateHandler;
721 
722   // We don't want to change anything at the current location, so
723   // save it aside and clear the insert point.
724   llvm::BasicBlock *SavedInsertBlock = Builder.GetInsertBlock();
725   llvm::BasicBlock::iterator SavedInsertPoint = Builder.GetInsertPoint();
726   Builder.ClearInsertionPoint();
727 
728   llvm::Constant *Personality = getPersonalityFn(CGM);
729   llvm::Value *llvm_eh_exception =
730     CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
731   llvm::Value *llvm_eh_selector =
732     CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
733 
734   // Set up terminate handler
735   TerminateHandler = createBasicBlock("terminate.handler");
736   EmitBlock(TerminateHandler);
737   llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
738   // We are required to emit this call to satisfy LLVM, even
739   // though we don't use the result.
740   llvm::Value *Args[] = {
741     Exc, Personality,
742     llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)
743   };
744   Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
745   llvm::CallInst *TerminateCall =
746     Builder.CreateCall(getTerminateFn(*this));
747   TerminateCall->setDoesNotReturn();
748   TerminateCall->setDoesNotThrow();
749   Builder.CreateUnreachable();
750 
751   // Restore the saved insertion state.
752   Builder.SetInsertPoint(SavedInsertBlock, SavedInsertPoint);
753 
754   return TerminateHandler;
755 }
756