blob: ddc1c7743344ae0d488140fef6a76a09b8464960 [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();
129 if (Opts.CPlusPlus)
130 PersonalityFnName = "__gxx_personality_v0";
Chandler Carruthdcf22ad2010-05-17 20:58:49 +0000131 else if (Opts.ObjC1) {
David Chisnall79a9ad82010-05-17 13:49:20 +0000132 if (Opts.NeXTRuntime) {
133 if (Opts.ObjCNonFragileABI)
134 PersonalityFnName = "__gcc_personality_v0";
Chandler Carruthdcf22ad2010-05-17 20:58:49 +0000135 } else
David Chisnall79a9ad82010-05-17 13:49:20 +0000136 PersonalityFnName = "__gnu_objc_personality_v0";
Chandler Carruthdcf22ad2010-05-17 20:58:49 +0000137 }
David Chisnall79a9ad82010-05-17 13:49:20 +0000138
139 llvm::Constant *Personality =
140 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(
141 CGM.getLLVMContext()),
142 true),
143 PersonalityFnName);
144 return llvm::ConstantExpr::getBitCast(Personality, CGM.PtrToInt8Ty);
Mike Stump99533832009-12-02 07:41:41 +0000145}
146
John McCallac418162010-04-22 01:10:34 +0000147// Emits an exception expression into the given location. This
148// differs from EmitAnyExprToMem only in that, if a final copy-ctor
149// call is required, an exception within that copy ctor causes
150// std::terminate to be invoked.
151static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *E,
152 llvm::Value *ExnLoc) {
153 // We want to release the allocated exception object if this
154 // expression throws. We do this by pushing an EH-only cleanup
155 // block which, furthermore, deactivates itself after the expression
156 // is complete.
157 llvm::AllocaInst *ShouldFreeVar =
158 CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()),
159 "should-free-exnobj.var");
160 CGF.InitTempAlloca(ShouldFreeVar,
161 llvm::ConstantInt::getFalse(CGF.getLLVMContext()));
Mike Stump0f590be2009-12-01 03:41:18 +0000162
John McCallac418162010-04-22 01:10:34 +0000163 // A variable holding the exception pointer. This is necessary
164 // because the throw expression does not necessarily dominate the
165 // cleanup, for example if it appears in a conditional expression.
166 llvm::AllocaInst *ExnLocVar =
167 CGF.CreateTempAlloca(ExnLoc->getType(), "exnobj.var");
Mike Stump8755ec32009-12-10 00:06:18 +0000168
John McCallac418162010-04-22 01:10:34 +0000169 llvm::BasicBlock *SavedInvokeDest = CGF.getInvokeDest();
170 {
171 CodeGenFunction::EHCleanupBlock Cleanup(CGF);
172 llvm::BasicBlock *FreeBB = CGF.createBasicBlock("free-exnobj");
173 llvm::BasicBlock *DoneBB = CGF.createBasicBlock("free-exnobj.done");
Mike Stumpf2945c02009-12-17 06:08:47 +0000174
John McCallac418162010-04-22 01:10:34 +0000175 llvm::Value *ShouldFree = CGF.Builder.CreateLoad(ShouldFreeVar,
176 "should-free-exnobj");
177 CGF.Builder.CreateCondBr(ShouldFree, FreeBB, DoneBB);
178 CGF.EmitBlock(FreeBB);
179 llvm::Value *ExnLocLocal = CGF.Builder.CreateLoad(ExnLocVar, "exnobj");
180 CGF.Builder.CreateCall(getFreeExceptionFn(CGF), ExnLocLocal);
181 CGF.EmitBlock(DoneBB);
Mike Stump0f590be2009-12-01 03:41:18 +0000182 }
John McCallac418162010-04-22 01:10:34 +0000183 llvm::BasicBlock *Cleanup = CGF.getInvokeDest();
184
185 CGF.Builder.CreateStore(ExnLoc, ExnLocVar);
186 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()),
187 ShouldFreeVar);
188
189 // __cxa_allocate_exception returns a void*; we need to cast this
190 // to the appropriate type for the object.
191 const llvm::Type *Ty = CGF.ConvertType(E->getType())->getPointerTo();
192 llvm::Value *TypedExnLoc = CGF.Builder.CreateBitCast(ExnLoc, Ty);
193
194 // FIXME: this isn't quite right! If there's a final unelided call
195 // to a copy constructor, then according to [except.terminate]p1 we
196 // must call std::terminate() if that constructor throws, because
197 // technically that copy occurs after the exception expression is
198 // evaluated but before the exception is caught. But the best way
199 // to handle that is to teach EmitAggExpr to do the final copy
200 // differently if it can't be elided.
201 CGF.EmitAnyExprToMem(E, TypedExnLoc, /*Volatile*/ false);
202
203 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()),
204 ShouldFreeVar);
205
206 // Pop the cleanup block if it's still the top of the cleanup stack.
207 // Otherwise, temporaries have been created and our cleanup will get
208 // properly removed in time.
209 // TODO: this is not very resilient.
210 if (CGF.getInvokeDest() == Cleanup)
211 CGF.setInvokeDest(SavedInvokeDest);
Mike Stump0f590be2009-12-01 03:41:18 +0000212}
213
214// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
215// N is casted to the right type.
216static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stump13b2f922010-01-01 03:20:32 +0000217 bool WasPointer, bool WasPointerReference,
218 llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000219 // Store the throw exception in the exception object.
Mike Stumpb2debeb2009-12-08 01:29:31 +0000220 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000221 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000222 if (!WasPointer)
223 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000224 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump13b2f922010-01-01 03:20:32 +0000225 if (WasPointerReference) {
Mike Stumpb606c382010-01-01 02:51:52 +0000226 llvm::Value *Tmp = CGF.CreateTempAlloca(Value->getType(), "catch.param");
227 CGF.Builder.CreateStore(Value, Tmp);
228 Value = Tmp;
Mike Stump13b2f922010-01-01 03:20:32 +0000229 ValuePtrTy = Value->getType()->getPointerTo(0);
230 }
231 N = CGF.Builder.CreateBitCast(N, ValuePtrTy);
Mike Stumpb606c382010-01-01 02:51:52 +0000232 CGF.Builder.CreateStore(Value, N);
Mike Stump0f590be2009-12-01 03:41:18 +0000233 } else {
234 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
235 const CXXRecordDecl *RD;
236 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
237 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
238 if (RD->hasTrivialCopyConstructor()) {
239 CGF.EmitAggregateCopy(This, E, ObjectType);
240 } else if (CXXConstructorDecl *CopyCtor
241 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000242 llvm::Value *Src = E;
243
244 // Stolen from EmitClassAggrMemberwiseCopy
245 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
246 Ctor_Complete);
247 CallArgList CallArgs;
248 CallArgs.push_back(std::make_pair(RValue::get(This),
249 CopyCtor->getThisType(CGF.getContext())));
250
251 // Push the Src ptr.
252 CallArgs.push_back(std::make_pair(RValue::get(Src),
253 CopyCtor->getParamDecl(0)->getType()));
John McCall04a67a62010-02-05 21:31:56 +0000254
255 const FunctionProtoType *FPT
256 = CopyCtor->getType()->getAs<FunctionProtoType>();
257 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000258 Callee, ReturnValueSlot(), CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000259 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000260 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000261 }
262}
263
Anders Carlsson756b5c42009-10-30 01:42:31 +0000264void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000265 if (!E->getSubExpr()) {
Douglas Gregor1eb2e592010-05-16 00:44:00 +0000266 if (getInvokeDest()) {
267 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
268 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
269 ->setDoesNotReturn();
270 EmitBlock(Cont);
271 } else
272 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
273 Builder.CreateUnreachable();
274
275 // Clear the insertion point to indicate we are in unreachable code.
276 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000277 return;
278 }
Mike Stump8755ec32009-12-10 00:06:18 +0000279
Anders Carlssond3379292009-10-30 02:27:02 +0000280 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000281
Anders Carlssond3379292009-10-30 02:27:02 +0000282 // Now allocate the exception object.
283 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
John McCall3d3ec1c2010-04-21 10:05:39 +0000284 uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
Mike Stump8755ec32009-12-10 00:06:18 +0000285
Anders Carlssond3379292009-10-30 02:27:02 +0000286 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
Mike Stump8755ec32009-12-10 00:06:18 +0000287 llvm::Value *ExceptionPtr =
288 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000289 llvm::ConstantInt::get(SizeTy, TypeSize),
290 "exception");
Anders Carlsson8370c582009-12-11 00:32:37 +0000291
John McCallac418162010-04-22 01:10:34 +0000292 EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000293
Anders Carlssond3379292009-10-30 02:27:02 +0000294 // Now throw the exception.
295 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
John McCall9dffe6f2010-04-30 01:15:21 +0000296 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType, true);
John McCallac418162010-04-22 01:10:34 +0000297
298 // The address of the destructor. If the exception type has a
299 // trivial destructor (or isn't a record), we just pass null.
300 llvm::Constant *Dtor = 0;
301 if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
302 CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
303 if (!Record->hasTrivialDestructor()) {
304 CXXDestructorDecl *DtorD = Record->getDestructor(getContext());
305 Dtor = CGM.GetAddrOfCXXDestructor(DtorD, Dtor_Complete);
306 Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
307 }
308 }
309 if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000310
Mike Stump0a3816e2009-12-04 01:51:45 +0000311 if (getInvokeDest()) {
312 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump8755ec32009-12-10 00:06:18 +0000313 llvm::InvokeInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000314 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
315 ExceptionPtr, TypeInfo, Dtor);
316 ThrowCall->setDoesNotReturn();
317 EmitBlock(Cont);
318 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000319 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000320 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
321 ThrowCall->setDoesNotReturn();
322 }
Anders Carlssond3379292009-10-30 02:27:02 +0000323 Builder.CreateUnreachable();
Mike Stump8755ec32009-12-10 00:06:18 +0000324
Anders Carlssond3379292009-10-30 02:27:02 +0000325 // Clear the insertion point to indicate we are in unreachable code.
326 Builder.ClearInsertionPoint();
Mike Stumpc2ab4862009-12-07 20:12:14 +0000327
328 // FIXME: For now, emit a dummy basic block because expr emitters in generally
329 // are not ready to handle emitting expressions at unreachable points.
330 EnsureInsertPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000331}
Mike Stump2bf701e2009-11-20 23:44:51 +0000332
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000333void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000334 if (!Exceptions)
335 return;
336
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000337 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
338 if (FD == 0)
339 return;
340 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
341 if (Proto == 0)
342 return;
343
344 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
345
346 if (!Proto->hasExceptionSpec())
347 return;
348
David Chisnall79a9ad82010-05-17 13:49:20 +0000349 llvm::Constant *Personality = getPersonalityFn(CGM);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000350 llvm::Value *llvm_eh_exception =
351 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
352 llvm::Value *llvm_eh_selector =
353 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
354 const llvm::IntegerType *Int8Ty;
355 const llvm::PointerType *PtrToInt8Ty;
356 Int8Ty = llvm::Type::getInt8Ty(VMContext);
357 // C string type. Used in lots of places.
358 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
359 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
360 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
361
362 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
363 llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
364 llvm::BasicBlock *Match = createBasicBlock("match");
365 llvm::BasicBlock *Unwind = 0;
366
367 assert(PrevLandingPad == 0 && "EHSpec has invoke context");
Chandler Carruth60cfcec2009-12-13 01:37:04 +0000368 (void)PrevLandingPad;
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000369
370 llvm::BasicBlock *Cont = createBasicBlock("cont");
371
372 EmitBranchThroughCleanup(Cont);
373
374 // Emit the statements in the try {} block
375 setInvokeDest(EHSpecHandler);
376
377 EmitBlock(EHSpecHandler);
378 // Exception object
379 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
380 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
381
382 SelectorArgs.push_back(Exc);
383 SelectorArgs.push_back(Personality);
384 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
385 Proto->getNumExceptions()+1));
386
387 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
388 QualType Ty = Proto->getExceptionType(i);
Douglas Gregor154fe982009-12-23 22:04:40 +0000389 QualType ExceptType
390 = Ty.getNonReferenceType().getUnqualifiedType();
John McCall9dffe6f2010-04-30 01:15:21 +0000391 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType, true);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000392 SelectorArgs.push_back(EHType);
393 }
394 if (Proto->getNumExceptions())
395 SelectorArgs.push_back(Null);
396
397 // Find which handler was matched.
398 llvm::Value *Selector
399 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
400 SelectorArgs.end(), "selector");
401 if (Proto->getNumExceptions()) {
402 Unwind = createBasicBlock("Unwind");
403
404 Builder.CreateStore(Exc, RethrowPtr);
405 Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
406 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
407 0)),
408 Match, Unwind);
409
410 EmitBlock(Match);
411 }
412 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
413 Builder.CreateUnreachable();
414
415 if (Proto->getNumExceptions()) {
416 EmitBlock(Unwind);
Douglas Gregor86a3a032010-05-16 01:24:12 +0000417 Builder.CreateCall(getUnwindResumeOrRethrowFn(),
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000418 Builder.CreateLoad(RethrowPtr));
419 Builder.CreateUnreachable();
420 }
421
422 EmitBlock(Cont);
423}
424
425void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000426 if (!Exceptions)
427 return;
428
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000429 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
430 if (FD == 0)
431 return;
432 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
433 if (Proto == 0)
434 return;
435
436 if (!Proto->hasExceptionSpec())
437 return;
438
439 setInvokeDest(0);
440}
441
Mike Stump2bf701e2009-11-20 23:44:51 +0000442void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
John McCall9fc6a772010-02-19 09:25:03 +0000443 CXXTryStmtInfo Info = EnterCXXTryStmt(S);
444 EmitStmt(S.getTryBlock());
445 ExitCXXTryStmt(S, Info);
446}
447
448CodeGenFunction::CXXTryStmtInfo
449CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S) {
450 CXXTryStmtInfo Info;
451 Info.SavedLandingPad = getInvokeDest();
452 Info.HandlerBlock = createBasicBlock("try.handler");
453 Info.FinallyBlock = createBasicBlock("finally");
454
455 PushCleanupBlock(Info.FinallyBlock);
456 setInvokeDest(Info.HandlerBlock);
457
458 return Info;
459}
460
461void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S,
462 CXXTryStmtInfo TryInfo) {
Mike Stump2bf701e2009-11-20 23:44:51 +0000463 // Pointer to the personality function
David Chisnall79a9ad82010-05-17 13:49:20 +0000464 llvm::Constant *Personality = getPersonalityFn(CGM);
Mike Stump639787c2009-12-02 19:53:57 +0000465 llvm::Value *llvm_eh_exception =
466 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
467 llvm::Value *llvm_eh_selector =
468 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000469
John McCall9fc6a772010-02-19 09:25:03 +0000470 llvm::BasicBlock *PrevLandingPad = TryInfo.SavedLandingPad;
471 llvm::BasicBlock *TryHandler = TryInfo.HandlerBlock;
472 llvm::BasicBlock *FinallyBlock = TryInfo.FinallyBlock;
Mike Stump0f590be2009-12-01 03:41:18 +0000473 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000474 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
475
Mike Stump2bf701e2009-11-20 23:44:51 +0000476 // Jump to end if there is no exception
477 EmitBranchThroughCleanup(FinallyEnd);
478
Mike Stump9b39c512009-12-09 22:59:31 +0000479 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump639787c2009-12-02 19:53:57 +0000480
Mike Stump2bf701e2009-11-20 23:44:51 +0000481 // Emit the handlers
482 EmitBlock(TryHandler);
Mike Stump8755ec32009-12-10 00:06:18 +0000483
Mike Stump2bf701e2009-11-20 23:44:51 +0000484 const llvm::IntegerType *Int8Ty;
485 const llvm::PointerType *PtrToInt8Ty;
486 Int8Ty = llvm::Type::getInt8Ty(VMContext);
487 // C string type. Used in lots of places.
488 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000489 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
490 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000491 llvm::Value *llvm_eh_typeid_for =
492 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000493 // Exception object
Mike Stump9b39c512009-12-09 22:59:31 +0000494 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000495 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000496
Mike Stump0f590be2009-12-01 03:41:18 +0000497 SelectorArgs.push_back(Exc);
498 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000499
Mike Stump0f590be2009-12-01 03:41:18 +0000500 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000501 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
502 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000503 VarDecl *CatchParam = C->getExceptionDecl();
504 if (CatchParam) {
Douglas Gregor154fe982009-12-23 22:04:40 +0000505 // C++ [except.handle]p3 indicates that top-level cv-qualifiers
506 // are ignored.
507 QualType CaughtType = C->getCaughtType().getNonReferenceType();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000508 llvm::Value *EHTypeInfo
John McCall9dffe6f2010-04-30 01:15:21 +0000509 = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType(), true);
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000510 SelectorArgs.push_back(EHTypeInfo);
Mike Stump2bf701e2009-11-20 23:44:51 +0000511 } else {
512 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000513 SelectorArgs.push_back(Null);
514 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000515 }
516 }
517
Mike Stump0f590be2009-12-01 03:41:18 +0000518 // We use a cleanup unless there was already a catch all.
519 if (!HasCatchAll) {
520 SelectorArgs.push_back(Null);
521 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000522
Mike Stump0f590be2009-12-01 03:41:18 +0000523 // Find which handler was matched.
524 llvm::Value *Selector
525 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
526 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000527 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
528 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000529 VarDecl *CatchParam = C->getExceptionDecl();
530 Stmt *CatchBody = C->getHandlerBlock();
531
532 llvm::BasicBlock *Next = 0;
533
534 if (SelectorArgs[i+2] != Null) {
535 llvm::BasicBlock *Match = createBasicBlock("match");
536 Next = createBasicBlock("catch.next");
537 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
538 llvm::Value *Id
539 = Builder.CreateCall(llvm_eh_typeid_for,
540 Builder.CreateBitCast(SelectorArgs[i+2],
541 Int8PtrTy));
542 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
543 Match, Next);
544 EmitBlock(Match);
545 }
546
547 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
548 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
549
550 PushCleanupBlock(MatchEnd);
551 setInvokeDest(MatchHandler);
552
553 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
554
Mike Stumpf7f74672009-12-02 23:37:16 +0000555 {
556 CleanupScope CatchScope(*this);
557 // Bind the catch parameter if it exists.
558 if (CatchParam) {
559 QualType CatchType = CatchParam->getType().getNonReferenceType();
560 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000561 bool WasPointer = true;
Mike Stump13b2f922010-01-01 03:20:32 +0000562 bool WasPointerReference = false;
Mike Stumpb606c382010-01-01 02:51:52 +0000563 CatchType = CGM.getContext().getCanonicalType(CatchType);
Mike Stump13b2f922010-01-01 03:20:32 +0000564 if (CatchType.getTypePtr()->isPointerType()) {
565 if (isa<ReferenceType>(CatchParam->getType()))
566 WasPointerReference = true;
567 } else {
Mike Stump0a3816e2009-12-04 01:51:45 +0000568 if (!isa<ReferenceType>(CatchParam->getType()))
569 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000570 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000571 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000572 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000573 EmitLocalBlockVarDecl(*CatchParam);
Mike Stumpf668bd02009-12-03 03:40:14 +0000574 // FIXME: we need to do this sooner so that the EH region for the
575 // cleanup doesn't start until after the ctor completes, use a decl
576 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000577 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stump13b2f922010-01-01 03:20:32 +0000578 WasPointer, WasPointerReference, ExcObject,
Mike Stumpb606c382010-01-01 02:51:52 +0000579 GetAddrOfLocalVar(CatchParam));
Mike Stumpf7f74672009-12-02 23:37:16 +0000580 setInvokeDest(MatchHandler);
581 }
582
583 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000584 }
585
Mike Stump0f590be2009-12-01 03:41:18 +0000586 EmitBranchThroughCleanup(FinallyEnd);
587
588 EmitBlock(MatchHandler);
589
590 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
591 // We are required to emit this call to satisfy LLVM, even
592 // though we don't use the result.
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000593 llvm::Value *Args[] = {
594 Exc, Personality,
595 llvm::ConstantInt::getNullValue(llvm::Type::getInt32Ty(VMContext))
596 };
597 Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stump0f590be2009-12-01 03:41:18 +0000598 Builder.CreateStore(Exc, RethrowPtr);
599 EmitBranchThroughCleanup(FinallyRethrow);
600
601 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
602
603 EmitBlock(MatchEnd);
604
Mike Stump99533832009-12-02 07:41:41 +0000605 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000606 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000607 Cont, TerminateHandler,
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000608 &Args[0], &Args[0]);
Mike Stump0f590be2009-12-01 03:41:18 +0000609 EmitBlock(Cont);
610 if (Info.SwitchBlock)
611 EmitBlock(Info.SwitchBlock);
612 if (Info.EndBlock)
613 EmitBlock(Info.EndBlock);
614
Mike Stump0f590be2009-12-01 03:41:18 +0000615 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000616 Builder.CreateStore(Exc, RethrowPtr);
617 EmitBranchThroughCleanup(FinallyRethrow);
618
619 if (Next)
620 EmitBlock(Next);
621 }
Mike Stumpa0867832009-12-04 19:03:47 +0000622 if (!HasCatchAll) {
623 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000624 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000625 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000626
627 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
628
629 setInvokeDest(PrevLandingPad);
630
631 EmitBlock(FinallyBlock);
632
Mike Stump0f590be2009-12-01 03:41:18 +0000633 if (Info.SwitchBlock)
634 EmitBlock(Info.SwitchBlock);
635 if (Info.EndBlock)
636 EmitBlock(Info.EndBlock);
637
Mike Stump2bf701e2009-11-20 23:44:51 +0000638 // Branch around the rethrow code.
639 EmitBranch(FinallyEnd);
640
641 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000642 // FIXME: Eventually we can chain the handlers together and just do a call
643 // here.
644 if (getInvokeDest()) {
645 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Douglas Gregor86a3a032010-05-16 01:24:12 +0000646 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(), Cont,
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000647 getInvokeDest(),
648 Builder.CreateLoad(RethrowPtr));
649 EmitBlock(Cont);
650 } else
Douglas Gregor86a3a032010-05-16 01:24:12 +0000651 Builder.CreateCall(getUnwindResumeOrRethrowFn(),
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000652 Builder.CreateLoad(RethrowPtr));
653
Mike Stump2bf701e2009-11-20 23:44:51 +0000654 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000655
656 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000657}
Mike Stumpd88ea562009-12-09 03:35:49 +0000658
659CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
Mike Stumpd88ea562009-12-09 03:35:49 +0000660 CGF.setInvokeDest(PreviousInvokeDest);
661
John McCall3d3ec1c2010-04-21 10:05:39 +0000662 llvm::BasicBlock *EndOfCleanup = CGF.Builder.GetInsertBlock();
Mike Stumpd88ea562009-12-09 03:35:49 +0000663
John McCall3d3ec1c2010-04-21 10:05:39 +0000664 // Jump to the beginning of the cleanup.
665 CGF.Builder.SetInsertPoint(CleanupHandler, CleanupHandler->begin());
666
667 // The libstdc++ personality function.
668 // TODO: generalize to work with other libraries.
David Chisnall79a9ad82010-05-17 13:49:20 +0000669 llvm::Constant *Personality = getPersonalityFn(CGF.CGM);
John McCall3d3ec1c2010-04-21 10:05:39 +0000670
671 // %exception = call i8* @llvm.eh.exception()
672 // Magic intrinsic which tells gives us a handle to the caught
673 // exception.
Mike Stumpd88ea562009-12-09 03:35:49 +0000674 llvm::Value *llvm_eh_exception =
675 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
John McCall3d3ec1c2010-04-21 10:05:39 +0000676 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
677
678 llvm::Constant *Null = llvm::ConstantPointerNull::get(CGF.PtrToInt8Ty);
679
680 // %ignored = call i32 @llvm.eh.selector(i8* %exception,
681 // i8* @__gxx_personality_v0,
682 // i8* null)
683 // Magic intrinsic which tells LLVM that this invoke landing pad is
684 // just a cleanup block.
685 llvm::Value *Args[] = { Exc, Personality, Null };
Mike Stumpd88ea562009-12-09 03:35:49 +0000686 llvm::Value *llvm_eh_selector =
687 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000688 CGF.Builder.CreateCall(llvm_eh_selector, &Args[0], llvm::array_endof(Args));
Mike Stumpd88ea562009-12-09 03:35:49 +0000689
John McCall3d3ec1c2010-04-21 10:05:39 +0000690 // And then we fall through into the code that the user put there.
691 // Jump back to the end of the cleanup.
692 CGF.Builder.SetInsertPoint(EndOfCleanup);
Mike Stumpd88ea562009-12-09 03:35:49 +0000693
John McCall3d3ec1c2010-04-21 10:05:39 +0000694 // Rethrow the exception.
Mike Stumpd88ea562009-12-09 03:35:49 +0000695 if (CGF.getInvokeDest()) {
696 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Douglas Gregor86a3a032010-05-16 01:24:12 +0000697 CGF.Builder.CreateInvoke(CGF.getUnwindResumeOrRethrowFn(), Cont,
Mike Stumpd88ea562009-12-09 03:35:49 +0000698 CGF.getInvokeDest(), Exc);
699 CGF.EmitBlock(Cont);
700 } else
Douglas Gregor86a3a032010-05-16 01:24:12 +0000701 CGF.Builder.CreateCall(CGF.getUnwindResumeOrRethrowFn(), Exc);
Mike Stumpd88ea562009-12-09 03:35:49 +0000702 CGF.Builder.CreateUnreachable();
703
John McCall3d3ec1c2010-04-21 10:05:39 +0000704 // Resume inserting where we started, but put the new cleanup
705 // handler in place.
John McCall891f80e2010-04-30 00:06:43 +0000706 if (PreviousInsertionBlock)
707 CGF.Builder.SetInsertPoint(PreviousInsertionBlock);
708 else
709 CGF.Builder.ClearInsertionPoint();
710
Mike Stumpd88ea562009-12-09 03:35:49 +0000711 if (CGF.Exceptions)
712 CGF.setInvokeDest(CleanupHandler);
713}
Mike Stump9b39c512009-12-09 22:59:31 +0000714
715llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +0000716 if (TerminateHandler)
717 return TerminateHandler;
718
John McCall3d3ec1c2010-04-21 10:05:39 +0000719 // We don't want to change anything at the current location, so
720 // save it aside and clear the insert point.
721 llvm::BasicBlock *SavedInsertBlock = Builder.GetInsertBlock();
722 llvm::BasicBlock::iterator SavedInsertPoint = Builder.GetInsertPoint();
723 Builder.ClearInsertionPoint();
Mike Stump76958092009-12-09 23:31:35 +0000724
David Chisnall79a9ad82010-05-17 13:49:20 +0000725 llvm::Constant *Personality = getPersonalityFn(CGM);
Mike Stump9b39c512009-12-09 22:59:31 +0000726 llvm::Value *llvm_eh_exception =
727 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
728 llvm::Value *llvm_eh_selector =
729 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
730
731 // Set up terminate handler
Mike Stump182f3832009-12-10 00:02:42 +0000732 TerminateHandler = createBasicBlock("terminate.handler");
Mike Stump9b39c512009-12-09 22:59:31 +0000733 EmitBlock(TerminateHandler);
734 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
735 // We are required to emit this call to satisfy LLVM, even
736 // though we don't use the result.
Benjamin Kramer7c647a12010-03-18 16:59:57 +0000737 llvm::Value *Args[] = {
738 Exc, Personality,
739 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 1)
740 };
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}