blob: 73f66ccd3f5c16cb460466bde2aed45252e5e95a [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);
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 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);
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 Carlssond3379292009-10-30 02:27:02 +000046static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000047 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
48 // 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);
52
53 const llvm::FunctionType *FTy =
Mike Stumpb4eea692009-11-20 00:56:31 +000054 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
55 Args, false);
Anders Carlssond3379292009-10-30 02:27:02 +000056
57 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
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 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);
74
75 const llvm::FunctionType *FTy =
Mike Stump0f590be2009-12-01 03:41:18 +000076 llvm::FunctionType::get(Int8PtrTy, Args, false);
Mike Stump2bf701e2009-11-20 23:44:51 +000077
78 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
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 Stump0f590be2009-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 Stump99533832009-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 Stump0f590be2009-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 Stump99533832009-12-02 07:41:41 +0000136 // All temporaries end before we call __cxa_throw
Mike Stumpce033902009-12-04 03:55:53 +0000137 // FIXME: Doesn't work well with eh31.C and PopCXXTemporary
138 // CodeGenFunction::CleanupScope TryScope(CGF);
Mike Stump99533832009-12-02 07:41:41 +0000139 {
140 // These actions are only on the exceptional edge.
Mike Stumpa0867832009-12-04 19:03:47 +0000141 if (0) {
Mike Stumpce033902009-12-04 03:55:53 +0000142 // FIXME: Doesn't work well with eh31.C and PopCXXTemporary
Mike Stump99533832009-12-02 07:41:41 +0000143 CodeGenFunction::DelayedCleanupBlock Scope(CGF, true);
144
145 llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
146 const llvm::Type *Int8PtrTy
147 = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
148 llvm::Value *ExceptionPtr = CGF.Builder.CreateBitCast(N, Int8PtrTy);
149 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
Mike Stumpa0867832009-12-04 19:03:47 +0000150 }
Mike Stump99533832009-12-02 07:41:41 +0000151 }
152
Mike Stump0f590be2009-12-01 03:41:18 +0000153 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
154
155 // Stolen from EmitClassAggrMemberwiseCopy
156 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
157 Ctor_Complete);
158 CallArgList CallArgs;
159 CallArgs.push_back(std::make_pair(RValue::get(This),
160 CopyCtor->getThisType(CGF.getContext())));
161
162 // Push the Src ptr.
163 CallArgs.push_back(std::make_pair(RValue::get(Src),
164 CopyCtor->getParamDecl(0)->getType()));
165 QualType ResultType =
166 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
167 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
168 Callee, CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000169 } else
Mike Stump99533832009-12-02 07:41:41 +0000170 llvm::llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000171 }
172}
173
174// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
175// N is casted to the right type.
176static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000177 bool WasPointer, llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000178 // Store the throw exception in the exception object.
179 if (!CGF.hasAggregateLLVMType(ObjectType)) {
180 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000181 if (!WasPointer)
182 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000183 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump0f590be2009-12-01 03:41:18 +0000184 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
185 } else {
186 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
187 const CXXRecordDecl *RD;
188 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
189 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
190 if (RD->hasTrivialCopyConstructor()) {
191 CGF.EmitAggregateCopy(This, E, ObjectType);
192 } else if (CXXConstructorDecl *CopyCtor
193 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000194 llvm::Value *Src = E;
195
196 // Stolen from EmitClassAggrMemberwiseCopy
197 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
198 Ctor_Complete);
199 CallArgList CallArgs;
200 CallArgs.push_back(std::make_pair(RValue::get(This),
201 CopyCtor->getThisType(CGF.getContext())));
202
203 // Push the Src ptr.
204 CallArgs.push_back(std::make_pair(RValue::get(Src),
205 CopyCtor->getParamDecl(0)->getType()));
206 QualType ResultType =
207 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
208 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
209 Callee, CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000210 } else
211 llvm::llvm_unreachable("uncopyable object");
212 }
213}
214
Anders Carlsson756b5c42009-10-30 01:42:31 +0000215void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000216 if (!E->getSubExpr()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000217 if (getInvokeDest()) {
218 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
219 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
220 ->setDoesNotReturn();
221 EmitBlock(Cont);
222 } else
223 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
Mike Stumpb4eea692009-11-20 00:56:31 +0000224 Builder.CreateUnreachable();
225
226 // Clear the insertion point to indicate we are in unreachable code.
227 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000228 return;
229 }
230
231 QualType ThrowType = E->getSubExpr()->getType();
Anders Carlssond3379292009-10-30 02:27:02 +0000232
233 // Now allocate the exception object.
234 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
235 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
236
237 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
238 llvm::Value *ExceptionPtr =
239 Builder.CreateCall(AllocExceptionFn,
240 llvm::ConstantInt::get(SizeTy, TypeSize),
241 "exception");
242
Mike Stump0f590be2009-12-01 03:41:18 +0000243 CopyObject(*this, E->getSubExpr(), ExceptionPtr);
Anders Carlssond3379292009-10-30 02:27:02 +0000244
245 // Now throw the exception.
246 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stumpde050572009-12-02 18:57:08 +0000247 llvm::Constant *TypeInfo = CGM.GenerateRTTI(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000248 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
249
Mike Stump0a3816e2009-12-04 01:51:45 +0000250 if (getInvokeDest()) {
251 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
252 llvm::InvokeInst *ThrowCall =
253 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
254 ExceptionPtr, TypeInfo, Dtor);
255 ThrowCall->setDoesNotReturn();
256 EmitBlock(Cont);
257 } else {
258 llvm::CallInst *ThrowCall =
259 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
260 ThrowCall->setDoesNotReturn();
261 }
Anders Carlssond3379292009-10-30 02:27:02 +0000262 Builder.CreateUnreachable();
263
264 // Clear the insertion point to indicate we are in unreachable code.
265 Builder.ClearInsertionPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000266}
Mike Stump2bf701e2009-11-20 23:44:51 +0000267
268void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
Mike Stumpfffefeb2009-12-04 03:57:07 +0000269 if (0) {
Mike Stumpc88b6732009-12-02 18:20:18 +0000270 EmitStmt(S.getTryBlock());
271 return;
Mike Stump0f590be2009-12-01 03:41:18 +0000272 }
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000273
Mike Stump0f590be2009-12-01 03:41:18 +0000274 // FIXME: The below is still just a sketch of the code we need.
Mike Stump2bf701e2009-11-20 23:44:51 +0000275 // Pointer to the personality function
276 llvm::Constant *Personality =
277 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
278 (VMContext),
279 true),
280 "__gxx_personality_v0");
281 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump639787c2009-12-02 19:53:57 +0000282 llvm::Value *llvm_eh_exception =
283 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
284 llvm::Value *llvm_eh_selector =
285 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000286
Mike Stump2bf701e2009-11-20 23:44:51 +0000287 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
288 llvm::BasicBlock *TryHandler = createBasicBlock("try.handler");
Mike Stump2bf701e2009-11-20 23:44:51 +0000289 llvm::BasicBlock *FinallyBlock = createBasicBlock("finally");
Mike Stump0f590be2009-12-01 03:41:18 +0000290 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000291 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
292
293 // Push an EH context entry, used for handling rethrows.
294 PushCleanupBlock(FinallyBlock);
295
296 // Emit the statements in the try {} block
297 setInvokeDest(TryHandler);
298
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000299 // FIXME: We should not have to do this here. The AST should have the member
300 // initializers under the CXXTryStmt's TryBlock.
301 if (OuterTryBlock == &S) {
302 GlobalDecl GD = CurGD;
303 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
304
305 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
306 size_t OldCleanupStackSize = CleanupEntries.size();
307 EmitCtorPrologue(CD, CurGD.getCtorType());
308 EmitStmt(S.getTryBlock());
309
310 // If any of the member initializers are temporaries bound to references
311 // make sure to emit their destructors.
312 EmitCleanupBlocks(OldCleanupStackSize);
313 } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
314 llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue");
315 PushCleanupBlock(DtorEpilogue);
316
317 EmitStmt(S.getTryBlock());
318
319 CleanupBlockInfo Info = PopCleanupBlock();
320
321 assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!");
322 EmitBlock(DtorEpilogue);
323 EmitDtorEpilogue(DD, GD.getDtorType());
324
325 if (Info.SwitchBlock)
326 EmitBlock(Info.SwitchBlock);
327 if (Info.EndBlock)
328 EmitBlock(Info.EndBlock);
329 } else
330 EmitStmt(S.getTryBlock());
331 } else
332 EmitStmt(S.getTryBlock());
Mike Stump2bf701e2009-11-20 23:44:51 +0000333
334 // Jump to end if there is no exception
335 EmitBranchThroughCleanup(FinallyEnd);
336
Mike Stump639787c2009-12-02 19:53:57 +0000337 // Set up terminate handler
338 llvm::BasicBlock *TerminateHandler = createBasicBlock("terminate.handler");
339 EmitBlock(TerminateHandler);
340 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
341 // We are required to emit this call to satisfy LLVM, even
342 // though we don't use the result.
343 llvm::SmallVector<llvm::Value*, 8> Args;
344 Args.clear();
345 Args.push_back(Exc);
346 Args.push_back(Personality);
347 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
348 0));
349 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
350 llvm::CallInst *TerminateCall =
351 Builder.CreateCall(getTerminateFn(*this));
352 TerminateCall->setDoesNotReturn();
353 TerminateCall->setDoesNotThrow();
354 Builder.CreateUnreachable();
355
356 // Clear the insertion point to indicate we are in unreachable code.
357 Builder.ClearInsertionPoint();
358
Mike Stump2bf701e2009-11-20 23:44:51 +0000359 // Emit the handlers
360 EmitBlock(TryHandler);
361
362 const llvm::IntegerType *Int8Ty;
363 const llvm::PointerType *PtrToInt8Ty;
364 Int8Ty = llvm::Type::getInt8Ty(VMContext);
365 // C string type. Used in lots of places.
366 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000367 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
368 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000369 llvm::Value *llvm_eh_typeid_for =
370 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000371 // Exception object
Mike Stump639787c2009-12-02 19:53:57 +0000372 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000373 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000374
Mike Stump639787c2009-12-02 19:53:57 +0000375 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000376 SelectorArgs.push_back(Exc);
377 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000378
Mike Stump0f590be2009-12-01 03:41:18 +0000379 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000380 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
381 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000382 VarDecl *CatchParam = C->getExceptionDecl();
383 if (CatchParam) {
Mike Stumpde050572009-12-02 18:57:08 +0000384 llvm::Value *EHType
385 = CGM.GenerateRTTI(C->getCaughtType().getNonReferenceType());
Mike Stump0f590be2009-12-01 03:41:18 +0000386 SelectorArgs.push_back(EHType);
Mike Stump2bf701e2009-11-20 23:44:51 +0000387 } else {
388 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000389 SelectorArgs.push_back(Null);
390 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000391 }
392 }
393
Mike Stump0f590be2009-12-01 03:41:18 +0000394 // We use a cleanup unless there was already a catch all.
395 if (!HasCatchAll) {
396 SelectorArgs.push_back(Null);
397 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000398
Mike Stump0f590be2009-12-01 03:41:18 +0000399 // Find which handler was matched.
400 llvm::Value *Selector
401 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
402 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000403 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
404 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000405 VarDecl *CatchParam = C->getExceptionDecl();
406 Stmt *CatchBody = C->getHandlerBlock();
407
408 llvm::BasicBlock *Next = 0;
409
410 if (SelectorArgs[i+2] != Null) {
411 llvm::BasicBlock *Match = createBasicBlock("match");
412 Next = createBasicBlock("catch.next");
413 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
414 llvm::Value *Id
415 = Builder.CreateCall(llvm_eh_typeid_for,
416 Builder.CreateBitCast(SelectorArgs[i+2],
417 Int8PtrTy));
418 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
419 Match, Next);
420 EmitBlock(Match);
421 }
422
423 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
424 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
425
426 PushCleanupBlock(MatchEnd);
427 setInvokeDest(MatchHandler);
428
429 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
430
Mike Stumpf7f74672009-12-02 23:37:16 +0000431 {
432 CleanupScope CatchScope(*this);
433 // Bind the catch parameter if it exists.
434 if (CatchParam) {
435 QualType CatchType = CatchParam->getType().getNonReferenceType();
436 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000437 bool WasPointer = true;
438 if (!CatchType.getTypePtr()->isPointerType()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000439 if (!isa<ReferenceType>(CatchParam->getType()))
440 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000441 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000442 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000443 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000444 EmitLocalBlockVarDecl(*CatchParam);
Mike Stump0f590be2009-12-01 03:41:18 +0000445#if 0
Mike Stumpf7f74672009-12-02 23:37:16 +0000446 // FIXME: objects with ctors, references
447 Builder.CreateStore(ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stump0f590be2009-12-01 03:41:18 +0000448#else
Mike Stumpf668bd02009-12-03 03:40:14 +0000449 // FIXME: we need to do this sooner so that the EH region for the
450 // cleanup doesn't start until after the ctor completes, use a decl
451 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000452 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000453 WasPointer, ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stump0f590be2009-12-01 03:41:18 +0000454#endif
Mike Stumpf7f74672009-12-02 23:37:16 +0000455 setInvokeDest(MatchHandler);
456 }
457
458 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000459 }
460
Mike Stump0f590be2009-12-01 03:41:18 +0000461 EmitBranchThroughCleanup(FinallyEnd);
462
463 EmitBlock(MatchHandler);
464
465 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
466 // We are required to emit this call to satisfy LLVM, even
467 // though we don't use the result.
Mike Stump639787c2009-12-02 19:53:57 +0000468 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000469 Args.push_back(Exc);
470 Args.push_back(Personality);
471 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
472 0));
473 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
474 Builder.CreateStore(Exc, RethrowPtr);
475 EmitBranchThroughCleanup(FinallyRethrow);
476
477 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
478
479 EmitBlock(MatchEnd);
480
Mike Stump99533832009-12-02 07:41:41 +0000481 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000482 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000483 Cont, TerminateHandler,
Mike Stump0f590be2009-12-01 03:41:18 +0000484 Args.begin(), Args.begin());
Mike Stump0f590be2009-12-01 03:41:18 +0000485 EmitBlock(Cont);
486 if (Info.SwitchBlock)
487 EmitBlock(Info.SwitchBlock);
488 if (Info.EndBlock)
489 EmitBlock(Info.EndBlock);
490
Mike Stump0f590be2009-12-01 03:41:18 +0000491 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000492 Builder.CreateStore(Exc, RethrowPtr);
493 EmitBranchThroughCleanup(FinallyRethrow);
494
495 if (Next)
496 EmitBlock(Next);
497 }
Mike Stumpa0867832009-12-04 19:03:47 +0000498 if (!HasCatchAll) {
499 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000500 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000501 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000502
503 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
504
505 setInvokeDest(PrevLandingPad);
506
507 EmitBlock(FinallyBlock);
508
Mike Stump0f590be2009-12-01 03:41:18 +0000509 if (Info.SwitchBlock)
510 EmitBlock(Info.SwitchBlock);
511 if (Info.EndBlock)
512 EmitBlock(Info.EndBlock);
513
Mike Stump2bf701e2009-11-20 23:44:51 +0000514 // Branch around the rethrow code.
515 EmitBranch(FinallyEnd);
516
517 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000518 // FIXME: Eventually we can chain the handlers together and just do a call
519 // here.
520 if (getInvokeDest()) {
521 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
522 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(*this), Cont,
523 getInvokeDest(),
524 Builder.CreateLoad(RethrowPtr));
525 EmitBlock(Cont);
526 } else
527 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
528 Builder.CreateLoad(RethrowPtr));
529
Mike Stump2bf701e2009-11-20 23:44:51 +0000530 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000531
532 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000533}