blob: 692153ed683a6bcbc52ac0a4667501b73b9ad196 [file] [log] [blame]
Anders Carlsson4b08db72009-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 Stump58ef18b2009-11-20 23:44:51 +000014#include "clang/AST/StmtCXX.h"
15
16#include "llvm/Intrinsics.h"
17
Anders Carlsson4b08db72009-10-30 01:42:31 +000018#include "CodeGenFunction.h"
19using namespace clang;
20using namespace CodeGen;
21
Anders Carlsson32e1b1c2009-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);
26
27 const llvm::FunctionType *FTy =
28 llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()),
29 Args, false);
30
31 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
32}
33
Mike Stump33270212009-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);
38
39 const llvm::FunctionType *FTy =
40 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
41 Args, false);
42
43 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
44}
45
Anders Carlsson32e1b1c2009-10-30 02:27:02 +000046static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
Mike Stump33270212009-12-02 07:41:41 +000047 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
48 // void (*dest) (void *));
Anders Carlsson32e1b1c2009-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);
52
53 const llvm::FunctionType *FTy =
Mike Stumpd1782cc2009-11-20 00:56:31 +000054 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
55 Args, false);
Anders Carlsson32e1b1c2009-10-30 02:27:02 +000056
57 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
58}
59
Mike Stumpd1782cc2009-11-20 00:56:31 +000060static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
Mike Stump33270212009-12-02 07:41:41 +000061 // void __cxa_rethrow();
Mike Stumpd1782cc2009-11-20 00:56:31 +000062
63 const llvm::FunctionType *FTy =
64 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
65
66 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
67}
68
Mike Stump58ef18b2009-11-20 23:44:51 +000069static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
Mike Stump33270212009-12-02 07:41:41 +000070 // void* __cxa_begin_catch();
Mike Stump58ef18b2009-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);
74
75 const llvm::FunctionType *FTy =
Mike Stump54066142009-12-01 03:41:18 +000076 llvm::FunctionType::get(Int8PtrTy, Args, false);
Mike Stump58ef18b2009-11-20 23:44:51 +000077
78 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
79}
80
81static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
Mike Stump33270212009-12-02 07:41:41 +000082 // void __cxa_end_catch();
Mike Stump58ef18b2009-11-20 23:44:51 +000083
84 const llvm::FunctionType *FTy =
85 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
86
87 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
88}
89
Mike Stump54066142009-12-01 03:41:18 +000090// FIXME: Eventually this will all go into the backend. Set from the target for
91// now.
92static int using_sjlj_exceptions = 0;
93
94static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) {
95 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
96 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
97
98 const llvm::FunctionType *FTy =
99 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args,
100 false);
101
102 if (using_sjlj_exceptions)
103 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
104 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
105}
106
Mike Stump33270212009-12-02 07:41:41 +0000107static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
108 // void __terminate();
109
110 const llvm::FunctionType *FTy =
111 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
112
113 return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev");
114}
115
Mike Stump54066142009-12-01 03:41:18 +0000116// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
117// N is casted to the right type.
118static void CopyObject(CodeGenFunction &CGF, const Expr *E, llvm::Value *N) {
119 QualType ObjectType = E->getType();
120
121 // Store the throw exception in the exception object.
122 if (!CGF.hasAggregateLLVMType(ObjectType)) {
123 llvm::Value *Value = CGF.EmitScalarExpr(E);
124 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
125
126 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
127 } else {
128 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
129 const CXXRecordDecl *RD;
130 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
131 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
132 if (RD->hasTrivialCopyConstructor()) {
133 CGF.EmitAggExpr(E, This, false);
134 } else if (CXXConstructorDecl *CopyCtor
135 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump33270212009-12-02 07:41:41 +0000136 // All temporaries end before we call __cxa_throw
137 CodeGenFunction::CleanupScope TryScope(CGF);
138 {
139 // These actions are only on the exceptional edge.
140 CodeGenFunction::DelayedCleanupBlock Scope(CGF, true);
141
142 llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
143 const llvm::Type *Int8PtrTy
144 = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
145 llvm::Value *ExceptionPtr = CGF.Builder.CreateBitCast(N, Int8PtrTy);
146 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
147 }
148
Mike Stump54066142009-12-01 03:41:18 +0000149 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
150
151 // Stolen from EmitClassAggrMemberwiseCopy
152 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
153 Ctor_Complete);
154 CallArgList CallArgs;
155 CallArgs.push_back(std::make_pair(RValue::get(This),
156 CopyCtor->getThisType(CGF.getContext())));
157
158 // Push the Src ptr.
159 CallArgs.push_back(std::make_pair(RValue::get(Src),
160 CopyCtor->getParamDecl(0)->getType()));
161 QualType ResultType =
162 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
163 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
164 Callee, CallArgs, CopyCtor);
Mike Stump54066142009-12-01 03:41:18 +0000165 } else
Mike Stump33270212009-12-02 07:41:41 +0000166 llvm::llvm_unreachable("uncopyable object");
Mike Stump54066142009-12-01 03:41:18 +0000167 }
168}
169
170// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
171// N is casted to the right type.
172static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stump7398ff02009-12-03 22:38:15 +0000173 bool WasPointer, llvm::Value *E, llvm::Value *N) {
Mike Stump54066142009-12-01 03:41:18 +0000174 // Store the throw exception in the exception object.
175 if (!CGF.hasAggregateLLVMType(ObjectType)) {
176 llvm::Value *Value = E;
Mike Stump7398ff02009-12-03 22:38:15 +0000177 if (!WasPointer)
178 Value = CGF.Builder.CreateLoad(Value);
Mike Stump54066142009-12-01 03:41:18 +0000179 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump54066142009-12-01 03:41:18 +0000180 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
181 } else {
182 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
183 const CXXRecordDecl *RD;
184 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
185 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
186 if (RD->hasTrivialCopyConstructor()) {
187 CGF.EmitAggregateCopy(This, E, ObjectType);
188 } else if (CXXConstructorDecl *CopyCtor
189 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump54066142009-12-01 03:41:18 +0000190 llvm::Value *Src = E;
191
192 // Stolen from EmitClassAggrMemberwiseCopy
193 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
194 Ctor_Complete);
195 CallArgList CallArgs;
196 CallArgs.push_back(std::make_pair(RValue::get(This),
197 CopyCtor->getThisType(CGF.getContext())));
198
199 // Push the Src ptr.
200 CallArgs.push_back(std::make_pair(RValue::get(Src),
201 CopyCtor->getParamDecl(0)->getType()));
202 QualType ResultType =
203 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
204 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
205 Callee, CallArgs, CopyCtor);
Mike Stump54066142009-12-01 03:41:18 +0000206 } else
207 llvm::llvm_unreachable("uncopyable object");
208 }
209}
210
Anders Carlsson4b08db72009-10-30 01:42:31 +0000211void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000212 if (!E->getSubExpr()) {
Mike Stumpd1782cc2009-11-20 00:56:31 +0000213 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
214 Builder.CreateUnreachable();
215
216 // Clear the insertion point to indicate we are in unreachable code.
217 Builder.ClearInsertionPoint();
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000218 return;
219 }
220
221 QualType ThrowType = E->getSubExpr()->getType();
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000222
223 // Now allocate the exception object.
224 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
225 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
226
227 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
228 llvm::Value *ExceptionPtr =
229 Builder.CreateCall(AllocExceptionFn,
230 llvm::ConstantInt::get(SizeTy, TypeSize),
231 "exception");
232
Mike Stump54066142009-12-01 03:41:18 +0000233 CopyObject(*this, E->getSubExpr(), ExceptionPtr);
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000234
235 // Now throw the exception.
236 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stumpc01c2b82009-12-02 18:57:08 +0000237 llvm::Constant *TypeInfo = CGM.GenerateRTTI(ThrowType);
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000238 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
239
240 llvm::CallInst *ThrowCall =
241 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
242 ThrowCall->setDoesNotReturn();
243 Builder.CreateUnreachable();
244
245 // Clear the insertion point to indicate we are in unreachable code.
246 Builder.ClearInsertionPoint();
Anders Carlsson4b08db72009-10-30 01:42:31 +0000247}
Mike Stump58ef18b2009-11-20 23:44:51 +0000248
249void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
Mike Stumpa9a590c2009-12-02 18:20:18 +0000250 if (1) {
251 EmitStmt(S.getTryBlock());
252 return;
Mike Stump54066142009-12-01 03:41:18 +0000253 }
Mike Stump54066142009-12-01 03:41:18 +0000254 // FIXME: The below is still just a sketch of the code we need.
Mike Stump58ef18b2009-11-20 23:44:51 +0000255 // Pointer to the personality function
256 llvm::Constant *Personality =
257 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
258 (VMContext),
259 true),
260 "__gxx_personality_v0");
261 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump97329152009-12-02 19:53:57 +0000262 llvm::Value *llvm_eh_exception =
263 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
264 llvm::Value *llvm_eh_selector =
265 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump58ef18b2009-11-20 23:44:51 +0000266
Mike Stump58ef18b2009-11-20 23:44:51 +0000267 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
268 llvm::BasicBlock *TryHandler = createBasicBlock("try.handler");
Mike Stump58ef18b2009-11-20 23:44:51 +0000269 llvm::BasicBlock *FinallyBlock = createBasicBlock("finally");
Mike Stump54066142009-12-01 03:41:18 +0000270 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump58ef18b2009-11-20 23:44:51 +0000271 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
272
273 // Push an EH context entry, used for handling rethrows.
274 PushCleanupBlock(FinallyBlock);
275
276 // Emit the statements in the try {} block
277 setInvokeDest(TryHandler);
278
279 EmitStmt(S.getTryBlock());
280
281 // Jump to end if there is no exception
282 EmitBranchThroughCleanup(FinallyEnd);
283
Mike Stump97329152009-12-02 19:53:57 +0000284 // Set up terminate handler
285 llvm::BasicBlock *TerminateHandler = createBasicBlock("terminate.handler");
286 EmitBlock(TerminateHandler);
287 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
288 // We are required to emit this call to satisfy LLVM, even
289 // though we don't use the result.
290 llvm::SmallVector<llvm::Value*, 8> Args;
291 Args.clear();
292 Args.push_back(Exc);
293 Args.push_back(Personality);
294 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
295 0));
296 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
297 llvm::CallInst *TerminateCall =
298 Builder.CreateCall(getTerminateFn(*this));
299 TerminateCall->setDoesNotReturn();
300 TerminateCall->setDoesNotThrow();
301 Builder.CreateUnreachable();
302
303 // Clear the insertion point to indicate we are in unreachable code.
304 Builder.ClearInsertionPoint();
305
Mike Stump58ef18b2009-11-20 23:44:51 +0000306 // Emit the handlers
307 EmitBlock(TryHandler);
308
309 const llvm::IntegerType *Int8Ty;
310 const llvm::PointerType *PtrToInt8Ty;
311 Int8Ty = llvm::Type::getInt8Ty(VMContext);
312 // C string type. Used in lots of places.
313 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump54066142009-12-01 03:41:18 +0000314 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
315 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump54066142009-12-01 03:41:18 +0000316 llvm::Value *llvm_eh_typeid_for =
317 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump58ef18b2009-11-20 23:44:51 +0000318 // Exception object
Mike Stump97329152009-12-02 19:53:57 +0000319 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump54066142009-12-01 03:41:18 +0000320 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump58ef18b2009-11-20 23:44:51 +0000321
Mike Stump97329152009-12-02 19:53:57 +0000322 Args.clear();
Mike Stump54066142009-12-01 03:41:18 +0000323 SelectorArgs.push_back(Exc);
324 SelectorArgs.push_back(Personality);
Mike Stump58ef18b2009-11-20 23:44:51 +0000325
Mike Stump54066142009-12-01 03:41:18 +0000326 bool HasCatchAll = false;
Mike Stump58ef18b2009-11-20 23:44:51 +0000327 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
328 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump54066142009-12-01 03:41:18 +0000329 VarDecl *CatchParam = C->getExceptionDecl();
330 if (CatchParam) {
Mike Stumpc01c2b82009-12-02 18:57:08 +0000331 llvm::Value *EHType
332 = CGM.GenerateRTTI(C->getCaughtType().getNonReferenceType());
Mike Stump54066142009-12-01 03:41:18 +0000333 SelectorArgs.push_back(EHType);
Mike Stump58ef18b2009-11-20 23:44:51 +0000334 } else {
335 // null indicates catch all
Mike Stump54066142009-12-01 03:41:18 +0000336 SelectorArgs.push_back(Null);
337 HasCatchAll = true;
Mike Stump58ef18b2009-11-20 23:44:51 +0000338 }
339 }
340
Mike Stump54066142009-12-01 03:41:18 +0000341 // We use a cleanup unless there was already a catch all.
342 if (!HasCatchAll) {
343 SelectorArgs.push_back(Null);
344 }
Mike Stump58ef18b2009-11-20 23:44:51 +0000345
Mike Stump54066142009-12-01 03:41:18 +0000346 // Find which handler was matched.
347 llvm::Value *Selector
348 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
349 SelectorArgs.end(), "selector");
Mike Stump58ef18b2009-11-20 23:44:51 +0000350 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
351 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump54066142009-12-01 03:41:18 +0000352 VarDecl *CatchParam = C->getExceptionDecl();
353 Stmt *CatchBody = C->getHandlerBlock();
354
355 llvm::BasicBlock *Next = 0;
356
357 if (SelectorArgs[i+2] != Null) {
358 llvm::BasicBlock *Match = createBasicBlock("match");
359 Next = createBasicBlock("catch.next");
360 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
361 llvm::Value *Id
362 = Builder.CreateCall(llvm_eh_typeid_for,
363 Builder.CreateBitCast(SelectorArgs[i+2],
364 Int8PtrTy));
365 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
366 Match, Next);
367 EmitBlock(Match);
368 }
369
370 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
371 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
372
373 PushCleanupBlock(MatchEnd);
374 setInvokeDest(MatchHandler);
375
376 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
377
Mike Stump90990962009-12-02 23:37:16 +0000378 {
379 CleanupScope CatchScope(*this);
380 // Bind the catch parameter if it exists.
381 if (CatchParam) {
382 QualType CatchType = CatchParam->getType().getNonReferenceType();
383 setInvokeDest(TerminateHandler);
Mike Stump7398ff02009-12-03 22:38:15 +0000384 bool WasPointer = true;
385 if (!CatchType.getTypePtr()->isPointerType()) {
386 WasPointer = false;
Mike Stump90990962009-12-02 23:37:16 +0000387 CatchType = getContext().getPointerType(CatchType);
Mike Stump7398ff02009-12-03 22:38:15 +0000388 }
Mike Stump90990962009-12-02 23:37:16 +0000389 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stump90990962009-12-02 23:37:16 +0000390 EmitLocalBlockVarDecl(*CatchParam);
Mike Stump54066142009-12-01 03:41:18 +0000391#if 0
Mike Stump90990962009-12-02 23:37:16 +0000392 // FIXME: objects with ctors, references
393 Builder.CreateStore(ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stump54066142009-12-01 03:41:18 +0000394#else
Mike Stump2842b4c2009-12-03 03:40:14 +0000395 // FIXME: we need to do this sooner so that the EH region for the
396 // cleanup doesn't start until after the ctor completes, use a decl
397 // init?
Mike Stump90990962009-12-02 23:37:16 +0000398 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stump7398ff02009-12-03 22:38:15 +0000399 WasPointer, ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stump54066142009-12-01 03:41:18 +0000400#endif
Mike Stump90990962009-12-02 23:37:16 +0000401 setInvokeDest(MatchHandler);
402 }
403
404 EmitStmt(CatchBody);
Mike Stump54066142009-12-01 03:41:18 +0000405 }
406
Mike Stump54066142009-12-01 03:41:18 +0000407 EmitBranchThroughCleanup(FinallyEnd);
408
409 EmitBlock(MatchHandler);
410
411 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
412 // We are required to emit this call to satisfy LLVM, even
413 // though we don't use the result.
Mike Stump97329152009-12-02 19:53:57 +0000414 Args.clear();
Mike Stump54066142009-12-01 03:41:18 +0000415 Args.push_back(Exc);
416 Args.push_back(Personality);
417 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
418 0));
419 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
420 Builder.CreateStore(Exc, RethrowPtr);
421 EmitBranchThroughCleanup(FinallyRethrow);
422
423 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
424
425 EmitBlock(MatchEnd);
426
Mike Stump33270212009-12-02 07:41:41 +0000427 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump54066142009-12-01 03:41:18 +0000428 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump33270212009-12-02 07:41:41 +0000429 Cont, TerminateHandler,
Mike Stump54066142009-12-01 03:41:18 +0000430 Args.begin(), Args.begin());
431
432 EmitBlock(Cont);
433 if (Info.SwitchBlock)
434 EmitBlock(Info.SwitchBlock);
435 if (Info.EndBlock)
436 EmitBlock(Info.EndBlock);
437
Mike Stump54066142009-12-01 03:41:18 +0000438 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump54066142009-12-01 03:41:18 +0000439 Builder.CreateStore(Exc, RethrowPtr);
440 EmitBranchThroughCleanup(FinallyRethrow);
441
442 if (Next)
443 EmitBlock(Next);
444 }
445 if (!HasCatchAll)
446 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stump58ef18b2009-11-20 23:44:51 +0000447
448 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
449
450 setInvokeDest(PrevLandingPad);
451
452 EmitBlock(FinallyBlock);
453
Mike Stump54066142009-12-01 03:41:18 +0000454 if (Info.SwitchBlock)
455 EmitBlock(Info.SwitchBlock);
456 if (Info.EndBlock)
457 EmitBlock(Info.EndBlock);
458
Mike Stump58ef18b2009-11-20 23:44:51 +0000459 // Branch around the rethrow code.
460 EmitBranch(FinallyEnd);
461
462 EmitBlock(FinallyRethrow);
Mike Stump54066142009-12-01 03:41:18 +0000463 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
464 Builder.CreateLoad(RethrowPtr));
Mike Stump58ef18b2009-11-20 23:44:51 +0000465 Builder.CreateUnreachable();
Mike Stump58ef18b2009-11-20 23:44:51 +0000466
467 EmitBlock(FinallyEnd);
Mike Stump58ef18b2009-11-20 23:44:51 +0000468}