blob: 60a1016cc040b2bb4b11d3b618a73225146bcf99 [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
Mike Stump0f590be2009-12-01 03:41:18 +0000103// FIXME: Eventually this will all go into the backend. Set from the target for
104// now.
105static int using_sjlj_exceptions = 0;
106
107static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) {
108 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
109 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000110
111 const llvm::FunctionType *FTy =
Mike Stump0f590be2009-12-01 03:41:18 +0000112 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args,
113 false);
Mike Stump8755ec32009-12-10 00:06:18 +0000114
Mike Stump0f590be2009-12-01 03:41:18 +0000115 if (using_sjlj_exceptions)
116 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
117 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
118}
119
Mike Stump99533832009-12-02 07:41:41 +0000120static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
121 // void __terminate();
122
Mike Stump8755ec32009-12-10 00:06:18 +0000123 const llvm::FunctionType *FTy =
Mike Stump99533832009-12-02 07:41:41 +0000124 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +0000125
Mike Stump99533832009-12-02 07:41:41 +0000126 return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev");
127}
128
Mike Stump0f590be2009-12-01 03:41:18 +0000129// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
Anders Carlsson8370c582009-12-11 00:32:37 +0000130// DestPtr is casted to the right type.
131static void CopyObject(CodeGenFunction &CGF, const Expr *E,
132 llvm::Value *DestPtr, llvm::Value *ExceptionPtrPtr) {
Mike Stump0f590be2009-12-01 03:41:18 +0000133 QualType ObjectType = E->getType();
134
135 // Store the throw exception in the exception object.
136 if (!CGF.hasAggregateLLVMType(ObjectType)) {
137 llvm::Value *Value = CGF.EmitScalarExpr(E);
Anders Carlsson8370c582009-12-11 00:32:37 +0000138 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo();
Mike Stump8755ec32009-12-10 00:06:18 +0000139
Anders Carlsson8370c582009-12-11 00:32:37 +0000140 CGF.Builder.CreateStore(Value,
141 CGF.Builder.CreateBitCast(DestPtr, ValuePtrTy));
Mike Stump0f590be2009-12-01 03:41:18 +0000142 } else {
Anders Carlsson8370c582009-12-11 00:32:37 +0000143 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo();
144 const CXXRecordDecl *RD =
145 cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
146
147 llvm::Value *This = CGF.Builder.CreateBitCast(DestPtr, Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000148 if (RD->hasTrivialCopyConstructor()) {
149 CGF.EmitAggExpr(E, This, false);
150 } else if (CXXConstructorDecl *CopyCtor
151 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stumpf2945c02009-12-17 06:08:47 +0000152 llvm::Value *CondPtr = 0;
Mike Stump5030a982009-12-10 01:52:30 +0000153 if (CGF.Exceptions) {
154 CodeGenFunction::EHCleanupBlock Cleanup(CGF);
Mike Stump99533832009-12-02 07:41:41 +0000155 llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
Anders Carlsson8370c582009-12-11 00:32:37 +0000156
Mike Stumpf2945c02009-12-17 06:08:47 +0000157 llvm::BasicBlock *CondBlock = CGF.createBasicBlock("cond.free");
158 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
159 CondPtr = CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()),
160 "doEHfree");
161
162 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CondPtr),
163 CondBlock, Cont);
164 CGF.EmitBlock(CondBlock);
165
Anders Carlsson8370c582009-12-11 00:32:37 +0000166 // Load the exception pointer.
167 llvm::Value *ExceptionPtr = CGF.Builder.CreateLoad(ExceptionPtrPtr);
Mike Stump99533832009-12-02 07:41:41 +0000168 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
Mike Stumpf2945c02009-12-17 06:08:47 +0000169
170 CGF.EmitBlock(Cont);
Mike Stump99533832009-12-02 07:41:41 +0000171 }
Mike Stump0924a8a2009-12-11 00:02:10 +0000172
Mike Stumpf2945c02009-12-17 06:08:47 +0000173 if (CondPtr)
174 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()),
175 CondPtr);
176
Mike Stump0f590be2009-12-01 03:41:18 +0000177 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
Mike Stumpf2945c02009-12-17 06:08:47 +0000178
Mike Stumpf2945c02009-12-17 06:08:47 +0000179 if (CondPtr)
180 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()),
181 CondPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000182
Mike Stump76958092009-12-09 23:31:35 +0000183 llvm::BasicBlock *TerminateHandler = CGF.getTerminateHandler();
Mike Stump63df2ae2009-12-24 06:52:05 +0000184 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Mike Stump76958092009-12-09 23:31:35 +0000185 CGF.setInvokeDest(TerminateHandler);
186
Mike Stump0f590be2009-12-01 03:41:18 +0000187 // Stolen from EmitClassAggrMemberwiseCopy
188 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
189 Ctor_Complete);
190 CallArgList CallArgs;
191 CallArgs.push_back(std::make_pair(RValue::get(This),
192 CopyCtor->getThisType(CGF.getContext())));
193
194 // Push the Src ptr.
195 CallArgs.push_back(std::make_pair(RValue::get(Src),
196 CopyCtor->getParamDecl(0)->getType()));
John McCall04a67a62010-02-05 21:31:56 +0000197 const FunctionProtoType *FPT
198 = CopyCtor->getType()->getAs<FunctionProtoType>();
199 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000200 Callee, ReturnValueSlot(), CallArgs, CopyCtor);
Mike Stump76958092009-12-09 23:31:35 +0000201 CGF.setInvokeDest(PrevLandingPad);
Mike Stump0f590be2009-12-01 03:41:18 +0000202 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000203 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000204 }
205}
206
207// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
208// N is casted to the right type.
209static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stump13b2f922010-01-01 03:20:32 +0000210 bool WasPointer, bool WasPointerReference,
211 llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000212 // Store the throw exception in the exception object.
Mike Stumpb2debeb2009-12-08 01:29:31 +0000213 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000214 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000215 if (!WasPointer)
216 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000217 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump13b2f922010-01-01 03:20:32 +0000218 if (WasPointerReference) {
Mike Stumpb606c382010-01-01 02:51:52 +0000219 llvm::Value *Tmp = CGF.CreateTempAlloca(Value->getType(), "catch.param");
220 CGF.Builder.CreateStore(Value, Tmp);
221 Value = Tmp;
Mike Stump13b2f922010-01-01 03:20:32 +0000222 ValuePtrTy = Value->getType()->getPointerTo(0);
223 }
224 N = CGF.Builder.CreateBitCast(N, ValuePtrTy);
Mike Stumpb606c382010-01-01 02:51:52 +0000225 CGF.Builder.CreateStore(Value, N);
Mike Stump0f590be2009-12-01 03:41:18 +0000226 } else {
227 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
228 const CXXRecordDecl *RD;
229 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
230 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
231 if (RD->hasTrivialCopyConstructor()) {
232 CGF.EmitAggregateCopy(This, E, ObjectType);
233 } else if (CXXConstructorDecl *CopyCtor
234 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000235 llvm::Value *Src = E;
236
237 // Stolen from EmitClassAggrMemberwiseCopy
238 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
239 Ctor_Complete);
240 CallArgList CallArgs;
241 CallArgs.push_back(std::make_pair(RValue::get(This),
242 CopyCtor->getThisType(CGF.getContext())));
243
244 // Push the Src ptr.
245 CallArgs.push_back(std::make_pair(RValue::get(Src),
246 CopyCtor->getParamDecl(0)->getType()));
John McCall04a67a62010-02-05 21:31:56 +0000247
248 const FunctionProtoType *FPT
249 = CopyCtor->getType()->getAs<FunctionProtoType>();
250 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000251 Callee, ReturnValueSlot(), CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000252 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000253 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000254 }
255}
256
Anders Carlsson756b5c42009-10-30 01:42:31 +0000257void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000258 if (!E->getSubExpr()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000259 if (getInvokeDest()) {
260 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
261 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
262 ->setDoesNotReturn();
263 EmitBlock(Cont);
264 } else
265 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
Mike Stumpb4eea692009-11-20 00:56:31 +0000266 Builder.CreateUnreachable();
267
268 // Clear the insertion point to indicate we are in unreachable code.
269 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000270 return;
271 }
Mike Stump8755ec32009-12-10 00:06:18 +0000272
Anders Carlssond3379292009-10-30 02:27:02 +0000273 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000274
Anders Carlssond3379292009-10-30 02:27:02 +0000275 // Now allocate the exception object.
276 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
277 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
Mike Stump8755ec32009-12-10 00:06:18 +0000278
Anders Carlssond3379292009-10-30 02:27:02 +0000279 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
Mike Stump8755ec32009-12-10 00:06:18 +0000280 llvm::Value *ExceptionPtr =
281 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000282 llvm::ConstantInt::get(SizeTy, TypeSize),
283 "exception");
Anders Carlsson8370c582009-12-11 00:32:37 +0000284
285 llvm::Value *ExceptionPtrPtr =
286 CreateTempAlloca(ExceptionPtr->getType(), "exception.ptr");
287 Builder.CreateStore(ExceptionPtr, ExceptionPtrPtr);
Anders Carlssond3379292009-10-30 02:27:02 +0000288
Anders Carlsson8370c582009-12-11 00:32:37 +0000289
290 CopyObject(*this, E->getSubExpr(), ExceptionPtr, ExceptionPtrPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000291
Anders Carlssond3379292009-10-30 02:27:02 +0000292 // Now throw the exception.
293 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000294 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000295 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000296
Mike Stump0a3816e2009-12-04 01:51:45 +0000297 if (getInvokeDest()) {
298 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump8755ec32009-12-10 00:06:18 +0000299 llvm::InvokeInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000300 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
301 ExceptionPtr, TypeInfo, Dtor);
302 ThrowCall->setDoesNotReturn();
303 EmitBlock(Cont);
304 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000305 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000306 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
307 ThrowCall->setDoesNotReturn();
308 }
Anders Carlssond3379292009-10-30 02:27:02 +0000309 Builder.CreateUnreachable();
Mike Stump8755ec32009-12-10 00:06:18 +0000310
Anders Carlssond3379292009-10-30 02:27:02 +0000311 // Clear the insertion point to indicate we are in unreachable code.
312 Builder.ClearInsertionPoint();
Mike Stumpc2ab4862009-12-07 20:12:14 +0000313
314 // FIXME: For now, emit a dummy basic block because expr emitters in generally
315 // are not ready to handle emitting expressions at unreachable points.
316 EnsureInsertPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000317}
Mike Stump2bf701e2009-11-20 23:44:51 +0000318
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000319void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000320 if (!Exceptions)
321 return;
322
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000323 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
324 if (FD == 0)
325 return;
326 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
327 if (Proto == 0)
328 return;
329
330 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
331
332 if (!Proto->hasExceptionSpec())
333 return;
334
335 llvm::Constant *Personality =
336 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
337 (VMContext),
338 true),
339 "__gxx_personality_v0");
340 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
341 llvm::Value *llvm_eh_exception =
342 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
343 llvm::Value *llvm_eh_selector =
344 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
345 const llvm::IntegerType *Int8Ty;
346 const llvm::PointerType *PtrToInt8Ty;
347 Int8Ty = llvm::Type::getInt8Ty(VMContext);
348 // C string type. Used in lots of places.
349 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
350 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
351 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
352
353 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
354 llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
355 llvm::BasicBlock *Match = createBasicBlock("match");
356 llvm::BasicBlock *Unwind = 0;
357
358 assert(PrevLandingPad == 0 && "EHSpec has invoke context");
Chandler Carruth60cfcec2009-12-13 01:37:04 +0000359 (void)PrevLandingPad;
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000360
361 llvm::BasicBlock *Cont = createBasicBlock("cont");
362
363 EmitBranchThroughCleanup(Cont);
364
365 // Emit the statements in the try {} block
366 setInvokeDest(EHSpecHandler);
367
368 EmitBlock(EHSpecHandler);
369 // Exception object
370 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
371 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
372
373 SelectorArgs.push_back(Exc);
374 SelectorArgs.push_back(Personality);
375 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
376 Proto->getNumExceptions()+1));
377
378 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
379 QualType Ty = Proto->getExceptionType(i);
Douglas Gregor154fe982009-12-23 22:04:40 +0000380 QualType ExceptType
381 = Ty.getNonReferenceType().getUnqualifiedType();
382 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000383 SelectorArgs.push_back(EHType);
384 }
385 if (Proto->getNumExceptions())
386 SelectorArgs.push_back(Null);
387
388 // Find which handler was matched.
389 llvm::Value *Selector
390 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
391 SelectorArgs.end(), "selector");
392 if (Proto->getNumExceptions()) {
393 Unwind = createBasicBlock("Unwind");
394
395 Builder.CreateStore(Exc, RethrowPtr);
396 Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
397 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
398 0)),
399 Match, Unwind);
400
401 EmitBlock(Match);
402 }
403 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
404 Builder.CreateUnreachable();
405
406 if (Proto->getNumExceptions()) {
407 EmitBlock(Unwind);
408 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
409 Builder.CreateLoad(RethrowPtr));
410 Builder.CreateUnreachable();
411 }
412
413 EmitBlock(Cont);
414}
415
416void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000417 if (!Exceptions)
418 return;
419
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000420 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
421 if (FD == 0)
422 return;
423 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
424 if (Proto == 0)
425 return;
426
427 if (!Proto->hasExceptionSpec())
428 return;
429
430 setInvokeDest(0);
431}
432
Mike Stump2bf701e2009-11-20 23:44:51 +0000433void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
Mike Stump2bf701e2009-11-20 23:44:51 +0000434 // Pointer to the personality function
435 llvm::Constant *Personality =
436 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
437 (VMContext),
438 true),
439 "__gxx_personality_v0");
440 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump639787c2009-12-02 19:53:57 +0000441 llvm::Value *llvm_eh_exception =
442 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
443 llvm::Value *llvm_eh_selector =
444 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000445
Mike Stump2bf701e2009-11-20 23:44:51 +0000446 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
447 llvm::BasicBlock *TryHandler = createBasicBlock("try.handler");
Mike Stump2bf701e2009-11-20 23:44:51 +0000448 llvm::BasicBlock *FinallyBlock = createBasicBlock("finally");
Mike Stump0f590be2009-12-01 03:41:18 +0000449 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000450 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
451
452 // Push an EH context entry, used for handling rethrows.
453 PushCleanupBlock(FinallyBlock);
454
455 // Emit the statements in the try {} block
456 setInvokeDest(TryHandler);
457
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000458 // FIXME: We should not have to do this here. The AST should have the member
459 // initializers under the CXXTryStmt's TryBlock.
460 if (OuterTryBlock == &S) {
461 GlobalDecl GD = CurGD;
462 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
463
464 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
465 size_t OldCleanupStackSize = CleanupEntries.size();
466 EmitCtorPrologue(CD, CurGD.getCtorType());
467 EmitStmt(S.getTryBlock());
468
469 // If any of the member initializers are temporaries bound to references
470 // make sure to emit their destructors.
471 EmitCleanupBlocks(OldCleanupStackSize);
472 } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
473 llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue");
474 PushCleanupBlock(DtorEpilogue);
475
Anders Carlsson1851a122010-02-07 19:45:40 +0000476 InitializeVtablePtrs(DD->getParent());
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000477 EmitStmt(S.getTryBlock());
Mike Stump8755ec32009-12-10 00:06:18 +0000478
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000479 CleanupBlockInfo Info = PopCleanupBlock();
480
481 assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!");
482 EmitBlock(DtorEpilogue);
483 EmitDtorEpilogue(DD, GD.getDtorType());
Mike Stump8755ec32009-12-10 00:06:18 +0000484
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000485 if (Info.SwitchBlock)
486 EmitBlock(Info.SwitchBlock);
487 if (Info.EndBlock)
488 EmitBlock(Info.EndBlock);
Mike Stump8755ec32009-12-10 00:06:18 +0000489 } else
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000490 EmitStmt(S.getTryBlock());
491 } else
492 EmitStmt(S.getTryBlock());
Mike Stump2bf701e2009-11-20 23:44:51 +0000493
494 // Jump to end if there is no exception
495 EmitBranchThroughCleanup(FinallyEnd);
496
Mike Stump9b39c512009-12-09 22:59:31 +0000497 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump639787c2009-12-02 19:53:57 +0000498
Mike Stump2bf701e2009-11-20 23:44:51 +0000499 // Emit the handlers
500 EmitBlock(TryHandler);
Mike Stump8755ec32009-12-10 00:06:18 +0000501
Mike Stump2bf701e2009-11-20 23:44:51 +0000502 const llvm::IntegerType *Int8Ty;
503 const llvm::PointerType *PtrToInt8Ty;
504 Int8Ty = llvm::Type::getInt8Ty(VMContext);
505 // C string type. Used in lots of places.
506 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000507 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
508 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000509 llvm::Value *llvm_eh_typeid_for =
510 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000511 // Exception object
Mike Stump9b39c512009-12-09 22:59:31 +0000512 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000513 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000514
Mike Stump9b39c512009-12-09 22:59:31 +0000515 llvm::SmallVector<llvm::Value*, 8> Args;
Mike Stump639787c2009-12-02 19:53:57 +0000516 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000517 SelectorArgs.push_back(Exc);
518 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000519
Mike Stump0f590be2009-12-01 03:41:18 +0000520 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000521 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
522 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000523 VarDecl *CatchParam = C->getExceptionDecl();
524 if (CatchParam) {
Douglas Gregor154fe982009-12-23 22:04:40 +0000525 // C++ [except.handle]p3 indicates that top-level cv-qualifiers
526 // are ignored.
527 QualType CaughtType = C->getCaughtType().getNonReferenceType();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000528 llvm::Value *EHTypeInfo
Douglas Gregor154fe982009-12-23 22:04:40 +0000529 = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType());
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000530 SelectorArgs.push_back(EHTypeInfo);
Mike Stump2bf701e2009-11-20 23:44:51 +0000531 } else {
532 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000533 SelectorArgs.push_back(Null);
534 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000535 }
536 }
537
Mike Stump0f590be2009-12-01 03:41:18 +0000538 // We use a cleanup unless there was already a catch all.
539 if (!HasCatchAll) {
540 SelectorArgs.push_back(Null);
541 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000542
Mike Stump0f590be2009-12-01 03:41:18 +0000543 // Find which handler was matched.
544 llvm::Value *Selector
545 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
546 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000547 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
548 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000549 VarDecl *CatchParam = C->getExceptionDecl();
550 Stmt *CatchBody = C->getHandlerBlock();
551
552 llvm::BasicBlock *Next = 0;
553
554 if (SelectorArgs[i+2] != Null) {
555 llvm::BasicBlock *Match = createBasicBlock("match");
556 Next = createBasicBlock("catch.next");
557 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
558 llvm::Value *Id
559 = Builder.CreateCall(llvm_eh_typeid_for,
560 Builder.CreateBitCast(SelectorArgs[i+2],
561 Int8PtrTy));
562 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
563 Match, Next);
564 EmitBlock(Match);
565 }
566
567 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
568 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
569
570 PushCleanupBlock(MatchEnd);
571 setInvokeDest(MatchHandler);
572
573 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
574
Mike Stumpf7f74672009-12-02 23:37:16 +0000575 {
576 CleanupScope CatchScope(*this);
577 // Bind the catch parameter if it exists.
578 if (CatchParam) {
579 QualType CatchType = CatchParam->getType().getNonReferenceType();
580 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000581 bool WasPointer = true;
Mike Stump13b2f922010-01-01 03:20:32 +0000582 bool WasPointerReference = false;
Mike Stumpb606c382010-01-01 02:51:52 +0000583 CatchType = CGM.getContext().getCanonicalType(CatchType);
Mike Stump13b2f922010-01-01 03:20:32 +0000584 if (CatchType.getTypePtr()->isPointerType()) {
585 if (isa<ReferenceType>(CatchParam->getType()))
586 WasPointerReference = true;
587 } else {
Mike Stump0a3816e2009-12-04 01:51:45 +0000588 if (!isa<ReferenceType>(CatchParam->getType()))
589 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000590 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000591 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000592 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000593 EmitLocalBlockVarDecl(*CatchParam);
Mike Stumpf668bd02009-12-03 03:40:14 +0000594 // FIXME: we need to do this sooner so that the EH region for the
595 // cleanup doesn't start until after the ctor completes, use a decl
596 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000597 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stump13b2f922010-01-01 03:20:32 +0000598 WasPointer, WasPointerReference, ExcObject,
Mike Stumpb606c382010-01-01 02:51:52 +0000599 GetAddrOfLocalVar(CatchParam));
Mike Stumpf7f74672009-12-02 23:37:16 +0000600 setInvokeDest(MatchHandler);
601 }
602
603 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000604 }
605
Mike Stump0f590be2009-12-01 03:41:18 +0000606 EmitBranchThroughCleanup(FinallyEnd);
607
608 EmitBlock(MatchHandler);
609
610 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
611 // We are required to emit this call to satisfy LLVM, even
612 // though we don't use the result.
Mike Stump639787c2009-12-02 19:53:57 +0000613 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000614 Args.push_back(Exc);
615 Args.push_back(Personality);
616 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
617 0));
618 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
619 Builder.CreateStore(Exc, RethrowPtr);
620 EmitBranchThroughCleanup(FinallyRethrow);
621
622 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
623
624 EmitBlock(MatchEnd);
625
Mike Stump99533832009-12-02 07:41:41 +0000626 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000627 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000628 Cont, TerminateHandler,
Mike Stump0f590be2009-12-01 03:41:18 +0000629 Args.begin(), Args.begin());
Mike Stump0f590be2009-12-01 03:41:18 +0000630 EmitBlock(Cont);
631 if (Info.SwitchBlock)
632 EmitBlock(Info.SwitchBlock);
633 if (Info.EndBlock)
634 EmitBlock(Info.EndBlock);
635
Mike Stump0f590be2009-12-01 03:41:18 +0000636 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000637 Builder.CreateStore(Exc, RethrowPtr);
638 EmitBranchThroughCleanup(FinallyRethrow);
639
640 if (Next)
641 EmitBlock(Next);
642 }
Mike Stumpa0867832009-12-04 19:03:47 +0000643 if (!HasCatchAll) {
644 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000645 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000646 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000647
648 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
649
650 setInvokeDest(PrevLandingPad);
651
652 EmitBlock(FinallyBlock);
653
Mike Stump0f590be2009-12-01 03:41:18 +0000654 if (Info.SwitchBlock)
655 EmitBlock(Info.SwitchBlock);
656 if (Info.EndBlock)
657 EmitBlock(Info.EndBlock);
658
Mike Stump2bf701e2009-11-20 23:44:51 +0000659 // Branch around the rethrow code.
660 EmitBranch(FinallyEnd);
661
662 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000663 // FIXME: Eventually we can chain the handlers together and just do a call
664 // here.
665 if (getInvokeDest()) {
666 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
667 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(*this), Cont,
668 getInvokeDest(),
669 Builder.CreateLoad(RethrowPtr));
670 EmitBlock(Cont);
671 } else
672 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
673 Builder.CreateLoad(RethrowPtr));
674
Mike Stump2bf701e2009-11-20 23:44:51 +0000675 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000676
677 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000678}
Mike Stumpd88ea562009-12-09 03:35:49 +0000679
680CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
681 llvm::BasicBlock *Cont1 = CGF.createBasicBlock("cont");
682 CGF.EmitBranch(Cont1);
683 CGF.setInvokeDest(PreviousInvokeDest);
684
685
686 CGF.EmitBlock(CleanupHandler);
687
688 llvm::Constant *Personality =
689 CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
690 (CGF.VMContext),
691 true),
692 "__gxx_personality_v0");
693 Personality = llvm::ConstantExpr::getBitCast(Personality, CGF.PtrToInt8Ty);
694 llvm::Value *llvm_eh_exception =
695 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
696 llvm::Value *llvm_eh_selector =
697 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
698
699 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
700 const llvm::IntegerType *Int8Ty;
701 const llvm::PointerType *PtrToInt8Ty;
702 Int8Ty = llvm::Type::getInt8Ty(CGF.VMContext);
703 // C string type. Used in lots of places.
704 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
705 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
706 llvm::SmallVector<llvm::Value*, 8> Args;
707 Args.clear();
708 Args.push_back(Exc);
709 Args.push_back(Personality);
710 Args.push_back(Null);
711 CGF.Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
712
713 CGF.EmitBlock(CleanupEntryBB);
714
715 CGF.EmitBlock(Cont1);
716
717 if (CGF.getInvokeDest()) {
718 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
719 CGF.Builder.CreateInvoke(getUnwindResumeOrRethrowFn(CGF), Cont,
720 CGF.getInvokeDest(), Exc);
721 CGF.EmitBlock(Cont);
722 } else
723 CGF.Builder.CreateCall(getUnwindResumeOrRethrowFn(CGF), Exc);
724
725 CGF.Builder.CreateUnreachable();
726
727 CGF.EmitBlock(Cont);
728 if (CGF.Exceptions)
729 CGF.setInvokeDest(CleanupHandler);
730}
Mike Stump9b39c512009-12-09 22:59:31 +0000731
732llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +0000733 if (TerminateHandler)
734 return TerminateHandler;
735
Mike Stump76958092009-12-09 23:31:35 +0000736 llvm::BasicBlock *Cont = 0;
737
738 if (HaveInsertPoint()) {
739 Cont = createBasicBlock("cont");
740 EmitBranch(Cont);
741 }
742
Mike Stump9b39c512009-12-09 22:59:31 +0000743 llvm::Constant *Personality =
744 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
745 (VMContext),
746 true),
747 "__gxx_personality_v0");
748 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
749 llvm::Value *llvm_eh_exception =
750 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
751 llvm::Value *llvm_eh_selector =
752 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
753
754 // Set up terminate handler
Mike Stump182f3832009-12-10 00:02:42 +0000755 TerminateHandler = createBasicBlock("terminate.handler");
Mike Stump9b39c512009-12-09 22:59:31 +0000756 EmitBlock(TerminateHandler);
757 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
758 // We are required to emit this call to satisfy LLVM, even
759 // though we don't use the result.
760 llvm::SmallVector<llvm::Value*, 8> Args;
761 Args.push_back(Exc);
762 Args.push_back(Personality);
763 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Mike Stump76958092009-12-09 23:31:35 +0000764 1));
Mike Stump9b39c512009-12-09 22:59:31 +0000765 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
Mike Stump8755ec32009-12-10 00:06:18 +0000766 llvm::CallInst *TerminateCall =
Mike Stump9b39c512009-12-09 22:59:31 +0000767 Builder.CreateCall(getTerminateFn(*this));
768 TerminateCall->setDoesNotReturn();
769 TerminateCall->setDoesNotThrow();
770 Builder.CreateUnreachable();
771
772 // Clear the insertion point to indicate we are in unreachable code.
773 Builder.ClearInsertionPoint();
774
Mike Stump76958092009-12-09 23:31:35 +0000775 if (Cont)
776 EmitBlock(Cont);
777
Mike Stump9b39c512009-12-09 22:59:31 +0000778 return TerminateHandler;
779}