blob: 9f4d50ccb7c51e9e7f6ef195a9b6a795d2eb6f9b [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 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);
95
96 const llvm::FunctionType *FTy =
97 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
98 Args, false);
99
100 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);
110
111 const llvm::FunctionType *FTy =
112 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args,
113 false);
114
115 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
123 const llvm::FunctionType *FTy =
124 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
125
126 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.
130// N is casted to the right type.
131static void CopyObject(CodeGenFunction &CGF, const Expr *E, llvm::Value *N) {
132 QualType ObjectType = E->getType();
133
134 // Store the throw exception in the exception object.
135 if (!CGF.hasAggregateLLVMType(ObjectType)) {
136 llvm::Value *Value = CGF.EmitScalarExpr(E);
137 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
138
139 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
140 } else {
141 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
142 const CXXRecordDecl *RD;
143 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
144 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
145 if (RD->hasTrivialCopyConstructor()) {
146 CGF.EmitAggExpr(E, This, false);
147 } else if (CXXConstructorDecl *CopyCtor
148 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump99533832009-12-02 07:41:41 +0000149 // All temporaries end before we call __cxa_throw
Mike Stumpce033902009-12-04 03:55:53 +0000150 // FIXME: Doesn't work well with eh31.C and PopCXXTemporary
151 // CodeGenFunction::CleanupScope TryScope(CGF);
Mike Stump99533832009-12-02 07:41:41 +0000152 {
153 // These actions are only on the exceptional edge.
Mike Stumpa0867832009-12-04 19:03:47 +0000154 if (0) {
Mike Stumpce033902009-12-04 03:55:53 +0000155 // FIXME: Doesn't work well with eh31.C and PopCXXTemporary
Mike Stump99533832009-12-02 07:41:41 +0000156 CodeGenFunction::DelayedCleanupBlock Scope(CGF, true);
157
158 llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
159 const llvm::Type *Int8PtrTy
160 = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
161 llvm::Value *ExceptionPtr = CGF.Builder.CreateBitCast(N, Int8PtrTy);
162 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
Mike Stumpa0867832009-12-04 19:03:47 +0000163 }
Mike Stump99533832009-12-02 07:41:41 +0000164 }
165
Mike Stump0f590be2009-12-01 03:41:18 +0000166 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
167
Mike Stump76958092009-12-09 23:31:35 +0000168 llvm::BasicBlock *TerminateHandler = CGF.getTerminateHandler();
169 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
170 CGF.setInvokeDest(TerminateHandler);
171
Mike Stump0f590be2009-12-01 03:41:18 +0000172 // Stolen from EmitClassAggrMemberwiseCopy
173 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
174 Ctor_Complete);
175 CallArgList CallArgs;
176 CallArgs.push_back(std::make_pair(RValue::get(This),
177 CopyCtor->getThisType(CGF.getContext())));
178
179 // Push the Src ptr.
180 CallArgs.push_back(std::make_pair(RValue::get(Src),
181 CopyCtor->getParamDecl(0)->getType()));
182 QualType ResultType =
183 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
184 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
185 Callee, CallArgs, CopyCtor);
Mike Stump76958092009-12-09 23:31:35 +0000186 CGF.setInvokeDest(PrevLandingPad);
Mike Stump0f590be2009-12-01 03:41:18 +0000187 } else
Mike Stump99533832009-12-02 07:41:41 +0000188 llvm::llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000189 }
190}
191
192// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
193// N is casted to the right type.
194static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000195 bool WasPointer, llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000196 // Store the throw exception in the exception object.
Mike Stumpb2debeb2009-12-08 01:29:31 +0000197 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000198 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000199 if (!WasPointer)
200 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000201 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump0f590be2009-12-01 03:41:18 +0000202 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
203 } else {
204 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
205 const CXXRecordDecl *RD;
206 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
207 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
208 if (RD->hasTrivialCopyConstructor()) {
209 CGF.EmitAggregateCopy(This, E, ObjectType);
210 } else if (CXXConstructorDecl *CopyCtor
211 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000212 llvm::Value *Src = E;
213
214 // Stolen from EmitClassAggrMemberwiseCopy
215 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
216 Ctor_Complete);
217 CallArgList CallArgs;
218 CallArgs.push_back(std::make_pair(RValue::get(This),
219 CopyCtor->getThisType(CGF.getContext())));
220
221 // Push the Src ptr.
222 CallArgs.push_back(std::make_pair(RValue::get(Src),
223 CopyCtor->getParamDecl(0)->getType()));
224 QualType ResultType =
225 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
226 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
227 Callee, CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000228 } else
229 llvm::llvm_unreachable("uncopyable object");
230 }
231}
232
Anders Carlsson756b5c42009-10-30 01:42:31 +0000233void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000234 if (!E->getSubExpr()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000235 if (getInvokeDest()) {
236 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
237 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
238 ->setDoesNotReturn();
239 EmitBlock(Cont);
240 } else
241 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
Mike Stumpb4eea692009-11-20 00:56:31 +0000242 Builder.CreateUnreachable();
243
244 // Clear the insertion point to indicate we are in unreachable code.
245 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000246 return;
247 }
248
249 QualType ThrowType = E->getSubExpr()->getType();
Anders Carlssond3379292009-10-30 02:27:02 +0000250
251 // Now allocate the exception object.
252 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
253 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
254
255 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
256 llvm::Value *ExceptionPtr =
257 Builder.CreateCall(AllocExceptionFn,
258 llvm::ConstantInt::get(SizeTy, TypeSize),
259 "exception");
260
Mike Stump0f590be2009-12-01 03:41:18 +0000261 CopyObject(*this, E->getSubExpr(), ExceptionPtr);
Anders Carlssond3379292009-10-30 02:27:02 +0000262
263 // Now throw the exception.
264 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stumpde050572009-12-02 18:57:08 +0000265 llvm::Constant *TypeInfo = CGM.GenerateRTTI(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000266 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
267
Mike Stump0a3816e2009-12-04 01:51:45 +0000268 if (getInvokeDest()) {
269 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
270 llvm::InvokeInst *ThrowCall =
271 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
272 ExceptionPtr, TypeInfo, Dtor);
273 ThrowCall->setDoesNotReturn();
274 EmitBlock(Cont);
275 } else {
276 llvm::CallInst *ThrowCall =
277 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
278 ThrowCall->setDoesNotReturn();
279 }
Anders Carlssond3379292009-10-30 02:27:02 +0000280 Builder.CreateUnreachable();
281
282 // Clear the insertion point to indicate we are in unreachable code.
283 Builder.ClearInsertionPoint();
Mike Stumpc2ab4862009-12-07 20:12:14 +0000284
285 // FIXME: For now, emit a dummy basic block because expr emitters in generally
286 // are not ready to handle emitting expressions at unreachable points.
287 EnsureInsertPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000288}
Mike Stump2bf701e2009-11-20 23:44:51 +0000289
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000290void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
291 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
292 if (FD == 0)
293 return;
294 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
295 if (Proto == 0)
296 return;
297
298 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
299
300 if (!Proto->hasExceptionSpec())
301 return;
302
303 llvm::Constant *Personality =
304 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
305 (VMContext),
306 true),
307 "__gxx_personality_v0");
308 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
309 llvm::Value *llvm_eh_exception =
310 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
311 llvm::Value *llvm_eh_selector =
312 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
313 const llvm::IntegerType *Int8Ty;
314 const llvm::PointerType *PtrToInt8Ty;
315 Int8Ty = llvm::Type::getInt8Ty(VMContext);
316 // C string type. Used in lots of places.
317 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
318 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
319 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
320
321 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
322 llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
323 llvm::BasicBlock *Match = createBasicBlock("match");
324 llvm::BasicBlock *Unwind = 0;
325
326 assert(PrevLandingPad == 0 && "EHSpec has invoke context");
327
328 llvm::BasicBlock *Cont = createBasicBlock("cont");
329
330 EmitBranchThroughCleanup(Cont);
331
332 // Emit the statements in the try {} block
333 setInvokeDest(EHSpecHandler);
334
335 EmitBlock(EHSpecHandler);
336 // Exception object
337 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
338 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
339
340 SelectorArgs.push_back(Exc);
341 SelectorArgs.push_back(Personality);
342 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
343 Proto->getNumExceptions()+1));
344
345 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
346 QualType Ty = Proto->getExceptionType(i);
347 llvm::Value *EHType
348 = CGM.GenerateRTTI(Ty.getNonReferenceType());
349 SelectorArgs.push_back(EHType);
350 }
351 if (Proto->getNumExceptions())
352 SelectorArgs.push_back(Null);
353
354 // Find which handler was matched.
355 llvm::Value *Selector
356 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
357 SelectorArgs.end(), "selector");
358 if (Proto->getNumExceptions()) {
359 Unwind = createBasicBlock("Unwind");
360
361 Builder.CreateStore(Exc, RethrowPtr);
362 Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
363 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
364 0)),
365 Match, Unwind);
366
367 EmitBlock(Match);
368 }
369 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
370 Builder.CreateUnreachable();
371
372 if (Proto->getNumExceptions()) {
373 EmitBlock(Unwind);
374 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
375 Builder.CreateLoad(RethrowPtr));
376 Builder.CreateUnreachable();
377 }
378
379 EmitBlock(Cont);
380}
381
382void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
383 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
384 if (FD == 0)
385 return;
386 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
387 if (Proto == 0)
388 return;
389
390 if (!Proto->hasExceptionSpec())
391 return;
392
393 setInvokeDest(0);
394}
395
Mike Stump2bf701e2009-11-20 23:44:51 +0000396void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
Mike Stump2bf701e2009-11-20 23:44:51 +0000397 // Pointer to the personality function
398 llvm::Constant *Personality =
399 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
400 (VMContext),
401 true),
402 "__gxx_personality_v0");
403 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump639787c2009-12-02 19:53:57 +0000404 llvm::Value *llvm_eh_exception =
405 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
406 llvm::Value *llvm_eh_selector =
407 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000408
Mike Stump2bf701e2009-11-20 23:44:51 +0000409 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
410 llvm::BasicBlock *TryHandler = createBasicBlock("try.handler");
Mike Stump2bf701e2009-11-20 23:44:51 +0000411 llvm::BasicBlock *FinallyBlock = createBasicBlock("finally");
Mike Stump0f590be2009-12-01 03:41:18 +0000412 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000413 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
414
415 // Push an EH context entry, used for handling rethrows.
416 PushCleanupBlock(FinallyBlock);
417
418 // Emit the statements in the try {} block
419 setInvokeDest(TryHandler);
420
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000421 // FIXME: We should not have to do this here. The AST should have the member
422 // initializers under the CXXTryStmt's TryBlock.
423 if (OuterTryBlock == &S) {
424 GlobalDecl GD = CurGD;
425 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
426
427 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
428 size_t OldCleanupStackSize = CleanupEntries.size();
429 EmitCtorPrologue(CD, CurGD.getCtorType());
430 EmitStmt(S.getTryBlock());
431
432 // If any of the member initializers are temporaries bound to references
433 // make sure to emit their destructors.
434 EmitCleanupBlocks(OldCleanupStackSize);
435 } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
436 llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue");
437 PushCleanupBlock(DtorEpilogue);
438
439 EmitStmt(S.getTryBlock());
440
441 CleanupBlockInfo Info = PopCleanupBlock();
442
443 assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!");
444 EmitBlock(DtorEpilogue);
445 EmitDtorEpilogue(DD, GD.getDtorType());
446
447 if (Info.SwitchBlock)
448 EmitBlock(Info.SwitchBlock);
449 if (Info.EndBlock)
450 EmitBlock(Info.EndBlock);
451 } else
452 EmitStmt(S.getTryBlock());
453 } else
454 EmitStmt(S.getTryBlock());
Mike Stump2bf701e2009-11-20 23:44:51 +0000455
456 // Jump to end if there is no exception
457 EmitBranchThroughCleanup(FinallyEnd);
458
Mike Stump9b39c512009-12-09 22:59:31 +0000459 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump639787c2009-12-02 19:53:57 +0000460
Mike Stump2bf701e2009-11-20 23:44:51 +0000461 // Emit the handlers
462 EmitBlock(TryHandler);
463
464 const llvm::IntegerType *Int8Ty;
465 const llvm::PointerType *PtrToInt8Ty;
466 Int8Ty = llvm::Type::getInt8Ty(VMContext);
467 // C string type. Used in lots of places.
468 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000469 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
470 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000471 llvm::Value *llvm_eh_typeid_for =
472 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000473 // Exception object
Mike Stump9b39c512009-12-09 22:59:31 +0000474 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000475 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000476
Mike Stump9b39c512009-12-09 22:59:31 +0000477 llvm::SmallVector<llvm::Value*, 8> Args;
Mike Stump639787c2009-12-02 19:53:57 +0000478 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000479 SelectorArgs.push_back(Exc);
480 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000481
Mike Stump0f590be2009-12-01 03:41:18 +0000482 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000483 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
484 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000485 VarDecl *CatchParam = C->getExceptionDecl();
486 if (CatchParam) {
Mike Stumpde050572009-12-02 18:57:08 +0000487 llvm::Value *EHType
488 = CGM.GenerateRTTI(C->getCaughtType().getNonReferenceType());
Mike Stump0f590be2009-12-01 03:41:18 +0000489 SelectorArgs.push_back(EHType);
Mike Stump2bf701e2009-11-20 23:44:51 +0000490 } else {
491 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000492 SelectorArgs.push_back(Null);
493 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000494 }
495 }
496
Mike Stump0f590be2009-12-01 03:41:18 +0000497 // We use a cleanup unless there was already a catch all.
498 if (!HasCatchAll) {
499 SelectorArgs.push_back(Null);
500 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000501
Mike Stump0f590be2009-12-01 03:41:18 +0000502 // Find which handler was matched.
503 llvm::Value *Selector
504 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
505 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000506 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
507 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000508 VarDecl *CatchParam = C->getExceptionDecl();
509 Stmt *CatchBody = C->getHandlerBlock();
510
511 llvm::BasicBlock *Next = 0;
512
513 if (SelectorArgs[i+2] != Null) {
514 llvm::BasicBlock *Match = createBasicBlock("match");
515 Next = createBasicBlock("catch.next");
516 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
517 llvm::Value *Id
518 = Builder.CreateCall(llvm_eh_typeid_for,
519 Builder.CreateBitCast(SelectorArgs[i+2],
520 Int8PtrTy));
521 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
522 Match, Next);
523 EmitBlock(Match);
524 }
525
526 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
527 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
528
529 PushCleanupBlock(MatchEnd);
530 setInvokeDest(MatchHandler);
531
532 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
533
Mike Stumpf7f74672009-12-02 23:37:16 +0000534 {
535 CleanupScope CatchScope(*this);
536 // Bind the catch parameter if it exists.
537 if (CatchParam) {
538 QualType CatchType = CatchParam->getType().getNonReferenceType();
539 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000540 bool WasPointer = true;
541 if (!CatchType.getTypePtr()->isPointerType()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000542 if (!isa<ReferenceType>(CatchParam->getType()))
543 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000544 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000545 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000546 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000547 EmitLocalBlockVarDecl(*CatchParam);
Mike Stumpf668bd02009-12-03 03:40:14 +0000548 // FIXME: we need to do this sooner so that the EH region for the
549 // cleanup doesn't start until after the ctor completes, use a decl
550 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000551 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000552 WasPointer, ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stumpf7f74672009-12-02 23:37:16 +0000553 setInvokeDest(MatchHandler);
554 }
555
556 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000557 }
558
Mike Stump0f590be2009-12-01 03:41:18 +0000559 EmitBranchThroughCleanup(FinallyEnd);
560
561 EmitBlock(MatchHandler);
562
563 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
564 // We are required to emit this call to satisfy LLVM, even
565 // though we don't use the result.
Mike Stump639787c2009-12-02 19:53:57 +0000566 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000567 Args.push_back(Exc);
568 Args.push_back(Personality);
569 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
570 0));
571 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
572 Builder.CreateStore(Exc, RethrowPtr);
573 EmitBranchThroughCleanup(FinallyRethrow);
574
575 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
576
577 EmitBlock(MatchEnd);
578
Mike Stump99533832009-12-02 07:41:41 +0000579 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000580 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000581 Cont, TerminateHandler,
Mike Stump0f590be2009-12-01 03:41:18 +0000582 Args.begin(), Args.begin());
Mike Stump0f590be2009-12-01 03:41:18 +0000583 EmitBlock(Cont);
584 if (Info.SwitchBlock)
585 EmitBlock(Info.SwitchBlock);
586 if (Info.EndBlock)
587 EmitBlock(Info.EndBlock);
588
Mike Stump0f590be2009-12-01 03:41:18 +0000589 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000590 Builder.CreateStore(Exc, RethrowPtr);
591 EmitBranchThroughCleanup(FinallyRethrow);
592
593 if (Next)
594 EmitBlock(Next);
595 }
Mike Stumpa0867832009-12-04 19:03:47 +0000596 if (!HasCatchAll) {
597 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000598 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000599 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000600
601 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
602
603 setInvokeDest(PrevLandingPad);
604
605 EmitBlock(FinallyBlock);
606
Mike Stump0f590be2009-12-01 03:41:18 +0000607 if (Info.SwitchBlock)
608 EmitBlock(Info.SwitchBlock);
609 if (Info.EndBlock)
610 EmitBlock(Info.EndBlock);
611
Mike Stump2bf701e2009-11-20 23:44:51 +0000612 // Branch around the rethrow code.
613 EmitBranch(FinallyEnd);
614
615 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000616 // FIXME: Eventually we can chain the handlers together and just do a call
617 // here.
618 if (getInvokeDest()) {
619 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
620 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(*this), Cont,
621 getInvokeDest(),
622 Builder.CreateLoad(RethrowPtr));
623 EmitBlock(Cont);
624 } else
625 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
626 Builder.CreateLoad(RethrowPtr));
627
Mike Stump2bf701e2009-11-20 23:44:51 +0000628 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000629
630 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000631}
Mike Stumpd88ea562009-12-09 03:35:49 +0000632
633CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
634 llvm::BasicBlock *Cont1 = CGF.createBasicBlock("cont");
635 CGF.EmitBranch(Cont1);
636 CGF.setInvokeDest(PreviousInvokeDest);
637
638
639 CGF.EmitBlock(CleanupHandler);
640
641 llvm::Constant *Personality =
642 CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
643 (CGF.VMContext),
644 true),
645 "__gxx_personality_v0");
646 Personality = llvm::ConstantExpr::getBitCast(Personality, CGF.PtrToInt8Ty);
647 llvm::Value *llvm_eh_exception =
648 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
649 llvm::Value *llvm_eh_selector =
650 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
651
652 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
653 const llvm::IntegerType *Int8Ty;
654 const llvm::PointerType *PtrToInt8Ty;
655 Int8Ty = llvm::Type::getInt8Ty(CGF.VMContext);
656 // C string type. Used in lots of places.
657 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
658 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
659 llvm::SmallVector<llvm::Value*, 8> Args;
660 Args.clear();
661 Args.push_back(Exc);
662 Args.push_back(Personality);
663 Args.push_back(Null);
664 CGF.Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
665
666 CGF.EmitBlock(CleanupEntryBB);
667
668 CGF.EmitBlock(Cont1);
669
670 if (CGF.getInvokeDest()) {
671 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
672 CGF.Builder.CreateInvoke(getUnwindResumeOrRethrowFn(CGF), Cont,
673 CGF.getInvokeDest(), Exc);
674 CGF.EmitBlock(Cont);
675 } else
676 CGF.Builder.CreateCall(getUnwindResumeOrRethrowFn(CGF), Exc);
677
678 CGF.Builder.CreateUnreachable();
679
680 CGF.EmitBlock(Cont);
681 if (CGF.Exceptions)
682 CGF.setInvokeDest(CleanupHandler);
683}
Mike Stump9b39c512009-12-09 22:59:31 +0000684
685llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +0000686 if (TerminateHandler)
687 return TerminateHandler;
688
Mike Stump76958092009-12-09 23:31:35 +0000689 llvm::BasicBlock *Cont = 0;
690
691 if (HaveInsertPoint()) {
692 Cont = createBasicBlock("cont");
693 EmitBranch(Cont);
694 }
695
Mike Stump9b39c512009-12-09 22:59:31 +0000696 llvm::Constant *Personality =
697 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
698 (VMContext),
699 true),
700 "__gxx_personality_v0");
701 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
702 llvm::Value *llvm_eh_exception =
703 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
704 llvm::Value *llvm_eh_selector =
705 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
706
707 // Set up terminate handler
Mike Stump182f3832009-12-10 00:02:42 +0000708 TerminateHandler = createBasicBlock("terminate.handler");
Mike Stump9b39c512009-12-09 22:59:31 +0000709 EmitBlock(TerminateHandler);
710 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
711 // We are required to emit this call to satisfy LLVM, even
712 // though we don't use the result.
713 llvm::SmallVector<llvm::Value*, 8> Args;
714 Args.push_back(Exc);
715 Args.push_back(Personality);
716 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Mike Stump76958092009-12-09 23:31:35 +0000717 1));
Mike Stump9b39c512009-12-09 22:59:31 +0000718 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
719 llvm::CallInst *TerminateCall =
720 Builder.CreateCall(getTerminateFn(*this));
721 TerminateCall->setDoesNotReturn();
722 TerminateCall->setDoesNotThrow();
723 Builder.CreateUnreachable();
724
725 // Clear the insertion point to indicate we are in unreachable code.
726 Builder.ClearInsertionPoint();
727
Mike Stump76958092009-12-09 23:31:35 +0000728 if (Cont)
729 EmitBlock(Cont);
730
Mike Stump9b39c512009-12-09 22:59:31 +0000731 return TerminateHandler;
732}