blob: 83d91edb10609eea8c921bb1abf8e3b284293596 [file] [log] [blame]
Anders Carlsson756b5c42009-10-30 01:42:31 +00001//===--- 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
Mike Stump2bf701e2009-11-20 23:44:51 +000014#include "clang/AST/StmtCXX.h"
15
16#include "llvm/Intrinsics.h"
17
Anders Carlsson756b5c42009-10-30 01:42:31 +000018#include "CodeGenFunction.h"
19using namespace clang;
20using namespace CodeGen;
21
Anders Carlssond3379292009-10-30 02:27:02 +000022static 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);
Mike Stump8755ec32009-12-10 00:06:18 +000026
27 const llvm::FunctionType *FTy =
Anders Carlssond3379292009-10-30 02:27:02 +000028 llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()),
29 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000030
Anders Carlssond3379292009-10-30 02:27:02 +000031 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
32}
33
Mike Stump99533832009-12-02 07:41:41 +000034static 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);
Mike Stump8755ec32009-12-10 00:06:18 +000038
39 const llvm::FunctionType *FTy =
Mike Stump99533832009-12-02 07:41:41 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
41 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000042
Mike Stump99533832009-12-02 07:41:41 +000043 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
44}
45
Anders Carlssond3379292009-10-30 02:27:02 +000046static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
Mike Stump8755ec32009-12-10 00:06:18 +000047 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
Mike Stump99533832009-12-02 07:41:41 +000048 // void (*dest) (void *));
Anders Carlssond3379292009-10-30 02:27:02 +000049
50 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
51 std::vector<const llvm::Type*> Args(3, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +000052
53 const llvm::FunctionType *FTy =
Mike Stumpb4eea692009-11-20 00:56:31 +000054 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
55 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000056
Anders Carlssond3379292009-10-30 02:27:02 +000057 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
58}
59
Mike Stumpb4eea692009-11-20 00:56:31 +000060static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000061 // void __cxa_rethrow();
Mike Stumpb4eea692009-11-20 00:56:31 +000062
Mike Stump8755ec32009-12-10 00:06:18 +000063 const llvm::FunctionType *FTy =
Mike Stumpb4eea692009-11-20 00:56:31 +000064 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +000065
Mike Stumpb4eea692009-11-20 00:56:31 +000066 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
67}
68
Mike Stump2bf701e2009-11-20 23:44:51 +000069static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000070 // void* __cxa_begin_catch();
Mike Stump2bf701e2009-11-20 23:44:51 +000071
72 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
73 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +000074
75 const llvm::FunctionType *FTy =
Mike Stump0f590be2009-12-01 03:41:18 +000076 llvm::FunctionType::get(Int8PtrTy, Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000077
Mike Stump2bf701e2009-11-20 23:44:51 +000078 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
79}
80
81static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000082 // void __cxa_end_catch();
Mike Stump2bf701e2009-11-20 23:44:51 +000083
Mike Stump8755ec32009-12-10 00:06:18 +000084 const llvm::FunctionType *FTy =
Mike Stump2bf701e2009-11-20 23:44:51 +000085 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +000086
Mike Stump2bf701e2009-11-20 23:44:51 +000087 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
88}
89
Mike Stumpcce3d4f2009-12-07 23:38:24 +000090static 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);
Mike Stump8755ec32009-12-10 00:06:18 +000095
96 const llvm::FunctionType *FTy =
Mike Stumpcce3d4f2009-12-07 23:38:24 +000097 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
98 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000099
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000100 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
101}
102
Douglas Gregor86a3a032010-05-16 01:24:12 +0000103llvm::Constant *CodeGenFunction::getUnwindResumeOrRethrowFn() {
104 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stump0f590be2009-12-01 03:41:18 +0000105 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000106
107 const llvm::FunctionType *FTy =
Douglas Gregor86a3a032010-05-16 01:24:12 +0000108 llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), Args,
Mike Stump0f590be2009-12-01 03:41:18 +0000109 false);
Mike Stump8755ec32009-12-10 00:06:18 +0000110
Douglas Gregor86a3a032010-05-16 01:24:12 +0000111 if (CGM.getLangOptions().SjLjExceptions)
112 return CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
113 return CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
Mike Stump0f590be2009-12-01 03:41:18 +0000114}
115
Mike Stump99533832009-12-02 07:41:41 +0000116static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
117 // void __terminate();
118
Mike Stump8755ec32009-12-10 00:06:18 +0000119 const llvm::FunctionType *FTy =
Mike Stump99533832009-12-02 07:41:41 +0000120 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +0000121
David Chisnall79a9ad82010-05-17 13:49:20 +0000122 return CGF.CGM.CreateRuntimeFunction(FTy,
123 CGF.CGM.getLangOptions().CPlusPlus ? "_ZSt9terminatev" : "abort");
124}
125
126static llvm::Constant *getPersonalityFn(CodeGenModule &CGM) {
127 const char *PersonalityFnName = "__gcc_personality_v0";
128 LangOptions Opts = CGM.getLangOptions();
Daniel Dunbar8019c452010-05-28 19:43:36 +0000129 if (Opts.CPlusPlus) {
130 if (Opts.SjLjExceptions)
131 PersonalityFnName = "__gxx_personality_sj0";
132 else
David Chisnall79a9ad82010-05-17 13:49:20 +0000133 PersonalityFnName = "__gxx_personality_v0";
Daniel Dunbar8019c452010-05-28 19:43:36 +0000134 } else if (Opts.ObjC1) {
David Chisnall79a9ad82010-05-17 13:49:20 +0000135 if (Opts.NeXTRuntime) {
136 if (Opts.ObjCNonFragileABI)
137 PersonalityFnName = "__gcc_personality_v0";
Chandler Carruthdcf22ad2010-05-17 20:58:49 +0000138 } else
David Chisnall79a9ad82010-05-17 13:49:20 +0000139 PersonalityFnName = "__gnu_objc_personality_v0";
Chandler Carruthdcf22ad2010-05-17 20:58:49 +0000140 }
David Chisnall79a9ad82010-05-17 13:49:20 +0000141
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);
Mike Stump99533832009-12-02 07:41:41 +0000148}
149
John McCallac418162010-04-22 01:10:34 +0000150// 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.
154static 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()));
Mike Stump0f590be2009-12-01 03:41:18 +0000165
John McCallac418162010-04-22 01:10:34 +0000166 // 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");
Mike Stump8755ec32009-12-10 00:06:18 +0000171
John McCallac418162010-04-22 01:10:34 +0000172 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");
Mike Stumpf2945c02009-12-17 06:08:47 +0000177
John McCallac418162010-04-22 01:10:34 +0000178 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);
Mike Stump0f590be2009-12-01 03:41:18 +0000185 }
John McCallac418162010-04-22 01:10:34 +0000186 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);
Mike Stump0f590be2009-12-01 03:41:18 +0000215}
216
217// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
218// N is casted to the right type.
219static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stump13b2f922010-01-01 03:20:32 +0000220 bool WasPointer, bool WasPointerReference,
221 llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000222 // Store the throw exception in the exception object.
Mike Stumpb2debeb2009-12-08 01:29:31 +0000223 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000224 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000225 if (!WasPointer)
226 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000227 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump13b2f922010-01-01 03:20:32 +0000228 if (WasPointerReference) {
Mike Stumpb606c382010-01-01 02:51:52 +0000229 llvm::Value *Tmp = CGF.CreateTempAlloca(Value->getType(), "catch.param");
230 CGF.Builder.CreateStore(Value, Tmp);
231 Value = Tmp;
Mike Stump13b2f922010-01-01 03:20:32 +0000232 ValuePtrTy = Value->getType()->getPointerTo(0);
233 }
234 N = CGF.Builder.CreateBitCast(N, ValuePtrTy);
Mike Stumpb606c382010-01-01 02:51:52 +0000235 CGF.Builder.CreateStore(Value, N);
Mike Stump0f590be2009-12-01 03:41:18 +0000236 } 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)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000245 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()));
John McCall04a67a62010-02-05 21:31:56 +0000257
258 const FunctionProtoType *FPT
259 = CopyCtor->getType()->getAs<FunctionProtoType>();
260 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000261 Callee, ReturnValueSlot(), CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000262 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000263 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000264 }
265}
266
Anders Carlsson756b5c42009-10-30 01:42:31 +0000267void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000268 if (!E->getSubExpr()) {
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000269 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();
Anders Carlssond3379292009-10-30 02:27:02 +0000280 return;
281 }
Mike Stump8755ec32009-12-10 00:06:18 +0000282
Anders Carlssond3379292009-10-30 02:27:02 +0000283 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000284
Anders Carlssond3379292009-10-30 02:27:02 +0000285 // Now allocate the exception object.
286 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
John McCall3d3ec1c2010-04-21 10:05:39 +0000287 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
Mike Stump8755ec32009-12-10 00:06:18 +0000288
Anders Carlssond3379292009-10-30 02:27:02 +0000289 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
Mike Stump8755ec32009-12-10 00:06:18 +0000290 llvm::Value *ExceptionPtr =
291 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000292 llvm::ConstantInt::get(SizeTy, TypeSize),
293 "exception");
Anders Carlsson8370c582009-12-11 00:32:37 +0000294
John McCallac418162010-04-22 01:10:34 +0000295 EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000296
Anders Carlssond3379292009-10-30 02:27:02 +0000297 // Now throw the exception.
298 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
John McCall9dffe6f2010-04-30 01:15:21 +0000299 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, true);
John McCallac418162010-04-22 01:10:34 +0000300
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);
Mike Stump8755ec32009-12-10 00:06:18 +0000313
Mike Stump0a3816e2009-12-04 01:51:45 +0000314 if (getInvokeDest()) {
315 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump8755ec32009-12-10 00:06:18 +0000316 llvm::InvokeInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000317 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
318 ExceptionPtr, TypeInfo, Dtor);
319 ThrowCall->setDoesNotReturn();
320 EmitBlock(Cont);
321 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000322 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000323 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
324 ThrowCall->setDoesNotReturn();
325 }
Anders Carlssond3379292009-10-30 02:27:02 +0000326 Builder.CreateUnreachable();
Mike Stump8755ec32009-12-10 00:06:18 +0000327
Anders Carlssond3379292009-10-30 02:27:02 +0000328 // Clear the insertion point to indicate we are in unreachable code.
329 Builder.ClearInsertionPoint();
Mike Stumpc2ab4862009-12-07 20:12:14 +0000330
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();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000334}
Mike Stump2bf701e2009-11-20 23:44:51 +0000335
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000336void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000337 if (!Exceptions)
338 return;
339
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000340 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
David Chisnall79a9ad82010-05-17 13:49:20 +0000352 llvm::Constant *Personality = getPersonalityFn(CGM);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000353 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");
Chandler Carruth60cfcec2009-12-13 01:37:04 +0000371 (void)PrevLandingPad;
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000372
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);
Chris Lattner77b89b82010-06-27 07:15:29 +0000387 SelectorArgs.push_back(llvm::ConstantInt::get(Int32Ty,
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000388 Proto->getNumExceptions()+1));
389
390 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
391 QualType Ty = Proto->getExceptionType(i);
Douglas Gregor154fe982009-12-23 22:04:40 +0000392 QualType ExceptType
393 = Ty.getNonReferenceType().getUnqualifiedType();
John McCall9dffe6f2010-04-30 01:15:21 +0000394 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, true);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000395 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,
Chris Lattner77b89b82010-06-27 07:15:29 +0000409 llvm::ConstantInt::get(Int32Ty, 0)),
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000410 Match, Unwind);
411
412 EmitBlock(Match);
413 }
414 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
415 Builder.CreateUnreachable();
416
417 if (Proto->getNumExceptions()) {
418 EmitBlock(Unwind);
Douglas Gregor86a3a032010-05-16 01:24:12 +0000419 Builder.CreateCall(getUnwindResumeOrRethrowFn(),
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000420 Builder.CreateLoad(RethrowPtr));
421 Builder.CreateUnreachable();
422 }
423
424 EmitBlock(Cont);
425}
426
427void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000428 if (!Exceptions)
429 return;
430
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000431 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
432 if (FD == 0)
433 return;
434 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
435 if (Proto == 0)
436 return;
437
438 if (!Proto->hasExceptionSpec())
439 return;
440
441 setInvokeDest(0);
442}
443
Mike Stump2bf701e2009-11-20 23:44:51 +0000444void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
John McCall9fc6a772010-02-19 09:25:03 +0000445 CXXTryStmtInfo Info = EnterCXXTryStmt(S);
446 EmitStmt(S.getTryBlock());
447 ExitCXXTryStmt(S, Info);
448}
449
450CodeGenFunction::CXXTryStmtInfo
451CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S) {
452 CXXTryStmtInfo Info;
453 Info.SavedLandingPad = getInvokeDest();
454 Info.HandlerBlock = createBasicBlock("try.handler");
455 Info.FinallyBlock = createBasicBlock("finally");
456
457 PushCleanupBlock(Info.FinallyBlock);
458 setInvokeDest(Info.HandlerBlock);
459
460 return Info;
461}
462
463void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S,
464 CXXTryStmtInfo TryInfo) {
Mike Stump2bf701e2009-11-20 23:44:51 +0000465 // Pointer to the personality function
David Chisnall79a9ad82010-05-17 13:49:20 +0000466 llvm::Constant *Personality = getPersonalityFn(CGM);
Mike Stump639787c2009-12-02 19:53:57 +0000467 llvm::Value *llvm_eh_exception =
468 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
469 llvm::Value *llvm_eh_selector =
470 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000471
John McCall9fc6a772010-02-19 09:25:03 +0000472 llvm::BasicBlock *PrevLandingPad = TryInfo.SavedLandingPad;
473 llvm::BasicBlock *TryHandler = TryInfo.HandlerBlock;
474 llvm::BasicBlock *FinallyBlock = TryInfo.FinallyBlock;
Mike Stump0f590be2009-12-01 03:41:18 +0000475 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000476 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
477
Mike Stump2bf701e2009-11-20 23:44:51 +0000478 // Jump to end if there is no exception
479 EmitBranchThroughCleanup(FinallyEnd);
480
Mike Stump9b39c512009-12-09 22:59:31 +0000481 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump639787c2009-12-02 19:53:57 +0000482
Mike Stump2bf701e2009-11-20 23:44:51 +0000483 // Emit the handlers
484 EmitBlock(TryHandler);
Mike Stump8755ec32009-12-10 00:06:18 +0000485
Mike Stump2bf701e2009-11-20 23:44:51 +0000486 const llvm::IntegerType *Int8Ty;
487 const llvm::PointerType *PtrToInt8Ty;
488 Int8Ty = llvm::Type::getInt8Ty(VMContext);
489 // C string type. Used in lots of places.
490 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000491 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
492 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000493 llvm::Value *llvm_eh_typeid_for =
494 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000495 // Exception object
Mike Stump9b39c512009-12-09 22:59:31 +0000496 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000497 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000498
Mike Stump0f590be2009-12-01 03:41:18 +0000499 SelectorArgs.push_back(Exc);
500 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000501
Mike Stump0f590be2009-12-01 03:41:18 +0000502 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000503 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
504 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000505 VarDecl *CatchParam = C->getExceptionDecl();
506 if (CatchParam) {
Douglas Gregor154fe982009-12-23 22:04:40 +0000507 // C++ [except.handle]p3 indicates that top-level cv-qualifiers
508 // are ignored.
509 QualType CaughtType = C->getCaughtType().getNonReferenceType();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000510 llvm::Value *EHTypeInfo
John McCall9dffe6f2010-04-30 01:15:21 +0000511 = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType(), true);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000512 SelectorArgs.push_back(EHTypeInfo);
Mike Stump2bf701e2009-11-20 23:44:51 +0000513 } else {
514 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000515 SelectorArgs.push_back(Null);
516 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000517 }
518 }
519
Mike Stump0f590be2009-12-01 03:41:18 +0000520 // We use a cleanup unless there was already a catch all.
521 if (!HasCatchAll) {
522 SelectorArgs.push_back(Null);
523 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000524
Mike Stump0f590be2009-12-01 03:41:18 +0000525 // Find which handler was matched.
526 llvm::Value *Selector
527 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
528 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000529 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
530 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000531 VarDecl *CatchParam = C->getExceptionDecl();
532 Stmt *CatchBody = C->getHandlerBlock();
533
534 llvm::BasicBlock *Next = 0;
535
536 if (SelectorArgs[i+2] != Null) {
537 llvm::BasicBlock *Match = createBasicBlock("match");
538 Next = createBasicBlock("catch.next");
539 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
540 llvm::Value *Id
541 = Builder.CreateCall(llvm_eh_typeid_for,
542 Builder.CreateBitCast(SelectorArgs[i+2],
543 Int8PtrTy));
544 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
545 Match, Next);
546 EmitBlock(Match);
547 }
548
549 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
550 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
551
552 PushCleanupBlock(MatchEnd);
553 setInvokeDest(MatchHandler);
554
555 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
556
Mike Stumpf7f74672009-12-02 23:37:16 +0000557 {
558 CleanupScope CatchScope(*this);
559 // Bind the catch parameter if it exists.
560 if (CatchParam) {
561 QualType CatchType = CatchParam->getType().getNonReferenceType();
562 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000563 bool WasPointer = true;
Mike Stump13b2f922010-01-01 03:20:32 +0000564 bool WasPointerReference = false;
Mike Stumpb606c382010-01-01 02:51:52 +0000565 CatchType = CGM.getContext().getCanonicalType(CatchType);
Mike Stump13b2f922010-01-01 03:20:32 +0000566 if (CatchType.getTypePtr()->isPointerType()) {
567 if (isa<ReferenceType>(CatchParam->getType()))
568 WasPointerReference = true;
569 } else {
Mike Stump0a3816e2009-12-04 01:51:45 +0000570 if (!isa<ReferenceType>(CatchParam->getType()))
571 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000572 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000573 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000574 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000575 EmitLocalBlockVarDecl(*CatchParam);
Mike Stumpf668bd02009-12-03 03:40:14 +0000576 // FIXME: we need to do this sooner so that the EH region for the
577 // cleanup doesn't start until after the ctor completes, use a decl
578 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000579 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stump13b2f922010-01-01 03:20:32 +0000580 WasPointer, WasPointerReference, ExcObject,
Mike Stumpb606c382010-01-01 02:51:52 +0000581 GetAddrOfLocalVar(CatchParam));
Mike Stumpf7f74672009-12-02 23:37:16 +0000582 setInvokeDest(MatchHandler);
583 }
584
585 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000586 }
587
Mike Stump0f590be2009-12-01 03:41:18 +0000588 EmitBranchThroughCleanup(FinallyEnd);
589
590 EmitBlock(MatchHandler);
591
592 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
593 // We are required to emit this call to satisfy LLVM, even
594 // though we don't use the result.
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000595 llvm::Value *Args[] = {
Chris Lattner77b89b82010-06-27 07:15:29 +0000596 Exc, Personality, llvm::ConstantInt::getNullValue(Int32Ty)
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000597 };
598 Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stump0f590be2009-12-01 03:41:18 +0000599 Builder.CreateStore(Exc, RethrowPtr);
600 EmitBranchThroughCleanup(FinallyRethrow);
601
602 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
603
604 EmitBlock(MatchEnd);
605
Mike Stump99533832009-12-02 07:41:41 +0000606 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000607 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000608 Cont, TerminateHandler,
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000609 &Args[0], &Args[0]);
Mike Stump0f590be2009-12-01 03:41:18 +0000610 EmitBlock(Cont);
611 if (Info.SwitchBlock)
612 EmitBlock(Info.SwitchBlock);
613 if (Info.EndBlock)
614 EmitBlock(Info.EndBlock);
615
Mike Stump0f590be2009-12-01 03:41:18 +0000616 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000617 Builder.CreateStore(Exc, RethrowPtr);
618 EmitBranchThroughCleanup(FinallyRethrow);
619
620 if (Next)
621 EmitBlock(Next);
622 }
Mike Stumpa0867832009-12-04 19:03:47 +0000623 if (!HasCatchAll) {
624 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000625 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000626 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000627
628 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
629
630 setInvokeDest(PrevLandingPad);
631
632 EmitBlock(FinallyBlock);
633
Mike Stump0f590be2009-12-01 03:41:18 +0000634 if (Info.SwitchBlock)
635 EmitBlock(Info.SwitchBlock);
636 if (Info.EndBlock)
637 EmitBlock(Info.EndBlock);
638
Mike Stump2bf701e2009-11-20 23:44:51 +0000639 // Branch around the rethrow code.
640 EmitBranch(FinallyEnd);
641
642 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000643 // FIXME: Eventually we can chain the handlers together and just do a call
644 // here.
645 if (getInvokeDest()) {
646 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Douglas Gregor86a3a032010-05-16 01:24:12 +0000647 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont,
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000648 getInvokeDest(),
649 Builder.CreateLoad(RethrowPtr));
650 EmitBlock(Cont);
651 } else
Douglas Gregor86a3a032010-05-16 01:24:12 +0000652 Builder.CreateCall(getUnwindResumeOrRethrowFn(),
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000653 Builder.CreateLoad(RethrowPtr));
654
Mike Stump2bf701e2009-11-20 23:44:51 +0000655 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000656
657 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000658}
Mike Stumpd88ea562009-12-09 03:35:49 +0000659
660CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
Mike Stumpd88ea562009-12-09 03:35:49 +0000661 CGF.setInvokeDest(PreviousInvokeDest);
662
John McCall3d3ec1c2010-04-21 10:05:39 +0000663 llvm::BasicBlock *EndOfCleanup = CGF.Builder.GetInsertBlock();
Mike Stumpd88ea562009-12-09 03:35:49 +0000664
John McCall3d3ec1c2010-04-21 10:05:39 +0000665 // Jump to the beginning of the cleanup.
666 CGF.Builder.SetInsertPoint(CleanupHandler, CleanupHandler->begin());
667
668 // The libstdc++ personality function.
669 // TODO: generalize to work with other libraries.
David Chisnall79a9ad82010-05-17 13:49:20 +0000670 llvm::Constant *Personality = getPersonalityFn(CGF.CGM);
John McCall3d3ec1c2010-04-21 10:05:39 +0000671
672 // %exception = call i8* @llvm.eh.exception()
673 // Magic intrinsic which tells gives us a handle to the caught
674 // exception.
Mike Stumpd88ea562009-12-09 03:35:49 +0000675 llvm::Value *llvm_eh_exception =
676 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
John McCall3d3ec1c2010-04-21 10:05:39 +0000677 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
678
679 llvm::Constant *Null = llvm::ConstantPointerNull::get(CGF.PtrToInt8Ty);
680
681 // %ignored = call i32 @llvm.eh.selector(i8* %exception,
682 // i8* @__gxx_personality_v0,
683 // i8* null)
684 // Magic intrinsic which tells LLVM that this invoke landing pad is
685 // just a cleanup block.
686 llvm::Value *Args[] = { Exc, Personality, Null };
Mike Stumpd88ea562009-12-09 03:35:49 +0000687 llvm::Value *llvm_eh_selector =
688 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000689 CGF.Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stumpd88ea562009-12-09 03:35:49 +0000690
John McCall3d3ec1c2010-04-21 10:05:39 +0000691 // And then we fall through into the code that the user put there.
692 // Jump back to the end of the cleanup.
693 CGF.Builder.SetInsertPoint(EndOfCleanup);
Mike Stumpd88ea562009-12-09 03:35:49 +0000694
John McCall3d3ec1c2010-04-21 10:05:39 +0000695 // Rethrow the exception.
Mike Stumpd88ea562009-12-09 03:35:49 +0000696 if (CGF.getInvokeDest()) {
697 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Douglas Gregor86a3a032010-05-16 01:24:12 +0000698 CGF.Builder.CreateInvoke(CGF.getUnwindResumeOrRethrowFn(), Cont,
Mike Stumpd88ea562009-12-09 03:35:49 +0000699 CGF.getInvokeDest(), Exc);
700 CGF.EmitBlock(Cont);
701 } else
Douglas Gregor86a3a032010-05-16 01:24:12 +0000702 CGF.Builder.CreateCall(CGF.getUnwindResumeOrRethrowFn(), Exc);
Mike Stumpd88ea562009-12-09 03:35:49 +0000703 CGF.Builder.CreateUnreachable();
704
John McCall3d3ec1c2010-04-21 10:05:39 +0000705 // Resume inserting where we started, but put the new cleanup
706 // handler in place.
John McCall891f80e2010-04-30 00:06:43 +0000707 if (PreviousInsertionBlock)
708 CGF.Builder.SetInsertPoint(PreviousInsertionBlock);
709 else
710 CGF.Builder.ClearInsertionPoint();
711
Mike Stumpd88ea562009-12-09 03:35:49 +0000712 if (CGF.Exceptions)
713 CGF.setInvokeDest(CleanupHandler);
714}
Mike Stump9b39c512009-12-09 22:59:31 +0000715
716llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +0000717 if (TerminateHandler)
718 return TerminateHandler;
719
John McCall3d3ec1c2010-04-21 10:05:39 +0000720 // We don't want to change anything at the current location, so
721 // save it aside and clear the insert point.
722 llvm::BasicBlock *SavedInsertBlock = Builder.GetInsertBlock();
723 llvm::BasicBlock::iterator SavedInsertPoint = Builder.GetInsertPoint();
724 Builder.ClearInsertionPoint();
Mike Stump76958092009-12-09 23:31:35 +0000725
David Chisnall79a9ad82010-05-17 13:49:20 +0000726 llvm::Constant *Personality = getPersonalityFn(CGM);
Mike Stump9b39c512009-12-09 22:59:31 +0000727 llvm::Value *llvm_eh_exception =
728 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
729 llvm::Value *llvm_eh_selector =
730 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
731
732 // Set up terminate handler
Mike Stump182f3832009-12-10 00:02:42 +0000733 TerminateHandler = createBasicBlock("terminate.handler");
Mike Stump9b39c512009-12-09 22:59:31 +0000734 EmitBlock(TerminateHandler);
735 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
736 // We are required to emit this call to satisfy LLVM, even
737 // though we don't use the result.
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000738 llvm::Value *Args[] = {
Chris Lattner77b89b82010-06-27 07:15:29 +0000739 Exc, Personality, llvm::ConstantInt::get(Int32Ty, 1)
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000740 };
741 Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stump8755ec32009-12-10 00:06:18 +0000742 llvm::CallInst *TerminateCall =
Mike Stump9b39c512009-12-09 22:59:31 +0000743 Builder.CreateCall(getTerminateFn(*this));
744 TerminateCall->setDoesNotReturn();
745 TerminateCall->setDoesNotThrow();
746 Builder.CreateUnreachable();
747
John McCall3d3ec1c2010-04-21 10:05:39 +0000748 // Restore the saved insertion state.
749 Builder.SetInsertPoint(SavedInsertBlock, SavedInsertPoint);
Mike Stump76958092009-12-09 23:31:35 +0000750
Mike Stump9b39c512009-12-09 22:59:31 +0000751 return TerminateHandler;
752}