blob: 142cb811b059fc2dceeae7309e8022cb6ec2f734 [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 +0000103static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) {
104 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
105 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000106
107 const llvm::FunctionType *FTy =
Mike Stump0f590be2009-12-01 03:41:18 +0000108 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args,
109 false);
Mike Stump8755ec32009-12-10 00:06:18 +0000110
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000111 if (CGF.CGM.getLangOptions().SjLjExceptions)
Mike Stump0f590be2009-12-01 03:41:18 +0000112 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
113 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
114}
115
Mike Stump99533832009-12-02 07:41:41 +0000116static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
117 // void __terminate();
118
Mike Stump8755ec32009-12-10 00:06:18 +0000119 const llvm::FunctionType *FTy =
Mike Stump99533832009-12-02 07:41:41 +0000120 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +0000121
Mike Stump99533832009-12-02 07:41:41 +0000122 return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev");
123}
124
Mike Stump0f590be2009-12-01 03:41:18 +0000125// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
Anders Carlsson8370c582009-12-11 00:32:37 +0000126// DestPtr is casted to the right type.
127static void CopyObject(CodeGenFunction &CGF, const Expr *E,
128 llvm::Value *DestPtr, llvm::Value *ExceptionPtrPtr) {
Mike Stump0f590be2009-12-01 03:41:18 +0000129 QualType ObjectType = E->getType();
130
131 // Store the throw exception in the exception object.
132 if (!CGF.hasAggregateLLVMType(ObjectType)) {
133 llvm::Value *Value = CGF.EmitScalarExpr(E);
Anders Carlsson8370c582009-12-11 00:32:37 +0000134 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo();
Mike Stump8755ec32009-12-10 00:06:18 +0000135
Anders Carlsson8370c582009-12-11 00:32:37 +0000136 CGF.Builder.CreateStore(Value,
137 CGF.Builder.CreateBitCast(DestPtr, ValuePtrTy));
Mike Stump0f590be2009-12-01 03:41:18 +0000138 } else {
Anders Carlsson8370c582009-12-11 00:32:37 +0000139 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo();
140 const CXXRecordDecl *RD =
141 cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
142
143 llvm::Value *This = CGF.Builder.CreateBitCast(DestPtr, Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000144 if (RD->hasTrivialCopyConstructor()) {
145 CGF.EmitAggExpr(E, This, false);
146 } else if (CXXConstructorDecl *CopyCtor
147 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stumpf2945c02009-12-17 06:08:47 +0000148 llvm::Value *CondPtr = 0;
Mike Stump5030a982009-12-10 01:52:30 +0000149 if (CGF.Exceptions) {
150 CodeGenFunction::EHCleanupBlock Cleanup(CGF);
Mike Stump99533832009-12-02 07:41:41 +0000151 llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
Anders Carlsson8370c582009-12-11 00:32:37 +0000152
Mike Stumpf2945c02009-12-17 06:08:47 +0000153 llvm::BasicBlock *CondBlock = CGF.createBasicBlock("cond.free");
154 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
155 CondPtr = CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()),
156 "doEHfree");
157
158 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CondPtr),
159 CondBlock, Cont);
160 CGF.EmitBlock(CondBlock);
161
Anders Carlsson8370c582009-12-11 00:32:37 +0000162 // Load the exception pointer.
163 llvm::Value *ExceptionPtr = CGF.Builder.CreateLoad(ExceptionPtrPtr);
Mike Stump99533832009-12-02 07:41:41 +0000164 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
Mike Stumpf2945c02009-12-17 06:08:47 +0000165
166 CGF.EmitBlock(Cont);
Mike Stump99533832009-12-02 07:41:41 +0000167 }
Mike Stump0924a8a2009-12-11 00:02:10 +0000168
Mike Stumpf2945c02009-12-17 06:08:47 +0000169 if (CondPtr)
170 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()),
171 CondPtr);
172
Mike Stump0f590be2009-12-01 03:41:18 +0000173 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
Mike Stumpf2945c02009-12-17 06:08:47 +0000174
Mike Stumpf2945c02009-12-17 06:08:47 +0000175 if (CondPtr)
176 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()),
177 CondPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000178
Mike Stump76958092009-12-09 23:31:35 +0000179 llvm::BasicBlock *TerminateHandler = CGF.getTerminateHandler();
Mike Stump63df2ae2009-12-24 06:52:05 +0000180 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Mike Stump76958092009-12-09 23:31:35 +0000181 CGF.setInvokeDest(TerminateHandler);
182
Mike Stump0f590be2009-12-01 03:41:18 +0000183 // Stolen from EmitClassAggrMemberwiseCopy
184 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
185 Ctor_Complete);
186 CallArgList CallArgs;
187 CallArgs.push_back(std::make_pair(RValue::get(This),
188 CopyCtor->getThisType(CGF.getContext())));
189
190 // Push the Src ptr.
191 CallArgs.push_back(std::make_pair(RValue::get(Src),
192 CopyCtor->getParamDecl(0)->getType()));
John McCall04a67a62010-02-05 21:31:56 +0000193 const FunctionProtoType *FPT
194 = CopyCtor->getType()->getAs<FunctionProtoType>();
195 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000196 Callee, ReturnValueSlot(), CallArgs, CopyCtor);
Mike Stump76958092009-12-09 23:31:35 +0000197 CGF.setInvokeDest(PrevLandingPad);
Mike Stump0f590be2009-12-01 03:41:18 +0000198 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000199 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000200 }
201}
202
203// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
204// N is casted to the right type.
205static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stump13b2f922010-01-01 03:20:32 +0000206 bool WasPointer, bool WasPointerReference,
207 llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000208 // Store the throw exception in the exception object.
Mike Stumpb2debeb2009-12-08 01:29:31 +0000209 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000210 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000211 if (!WasPointer)
212 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000213 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump13b2f922010-01-01 03:20:32 +0000214 if (WasPointerReference) {
Mike Stumpb606c382010-01-01 02:51:52 +0000215 llvm::Value *Tmp = CGF.CreateTempAlloca(Value->getType(), "catch.param");
216 CGF.Builder.CreateStore(Value, Tmp);
217 Value = Tmp;
Mike Stump13b2f922010-01-01 03:20:32 +0000218 ValuePtrTy = Value->getType()->getPointerTo(0);
219 }
220 N = CGF.Builder.CreateBitCast(N, ValuePtrTy);
Mike Stumpb606c382010-01-01 02:51:52 +0000221 CGF.Builder.CreateStore(Value, N);
Mike Stump0f590be2009-12-01 03:41:18 +0000222 } else {
223 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
224 const CXXRecordDecl *RD;
225 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
226 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
227 if (RD->hasTrivialCopyConstructor()) {
228 CGF.EmitAggregateCopy(This, E, ObjectType);
229 } else if (CXXConstructorDecl *CopyCtor
230 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000231 llvm::Value *Src = E;
232
233 // Stolen from EmitClassAggrMemberwiseCopy
234 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
235 Ctor_Complete);
236 CallArgList CallArgs;
237 CallArgs.push_back(std::make_pair(RValue::get(This),
238 CopyCtor->getThisType(CGF.getContext())));
239
240 // Push the Src ptr.
241 CallArgs.push_back(std::make_pair(RValue::get(Src),
242 CopyCtor->getParamDecl(0)->getType()));
John McCall04a67a62010-02-05 21:31:56 +0000243
244 const FunctionProtoType *FPT
245 = CopyCtor->getType()->getAs<FunctionProtoType>();
246 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
Anders Carlssonf3c47c92009-12-24 19:25:24 +0000247 Callee, ReturnValueSlot(), CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000248 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000249 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000250 }
251}
252
Anders Carlsson756b5c42009-10-30 01:42:31 +0000253void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000254 if (!E->getSubExpr()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000255 if (getInvokeDest()) {
256 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
257 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
258 ->setDoesNotReturn();
259 EmitBlock(Cont);
260 } else
261 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
Mike Stumpb4eea692009-11-20 00:56:31 +0000262 Builder.CreateUnreachable();
263
264 // Clear the insertion point to indicate we are in unreachable code.
265 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000266 return;
267 }
Mike Stump8755ec32009-12-10 00:06:18 +0000268
Anders Carlssond3379292009-10-30 02:27:02 +0000269 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000270
Anders Carlssond3379292009-10-30 02:27:02 +0000271 // Now allocate the exception object.
272 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
273 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
Mike Stump8755ec32009-12-10 00:06:18 +0000274
Anders Carlssond3379292009-10-30 02:27:02 +0000275 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
Mike Stump8755ec32009-12-10 00:06:18 +0000276 llvm::Value *ExceptionPtr =
277 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000278 llvm::ConstantInt::get(SizeTy, TypeSize),
279 "exception");
Anders Carlsson8370c582009-12-11 00:32:37 +0000280
281 llvm::Value *ExceptionPtrPtr =
282 CreateTempAlloca(ExceptionPtr->getType(), "exception.ptr");
283 Builder.CreateStore(ExceptionPtr, ExceptionPtrPtr);
Anders Carlssond3379292009-10-30 02:27:02 +0000284
Anders Carlsson8370c582009-12-11 00:32:37 +0000285
286 CopyObject(*this, E->getSubExpr(), ExceptionPtr, ExceptionPtrPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000287
Anders Carlssond3379292009-10-30 02:27:02 +0000288 // Now throw the exception.
289 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000290 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000291 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000292
Mike Stump0a3816e2009-12-04 01:51:45 +0000293 if (getInvokeDest()) {
294 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump8755ec32009-12-10 00:06:18 +0000295 llvm::InvokeInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000296 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
297 ExceptionPtr, TypeInfo, Dtor);
298 ThrowCall->setDoesNotReturn();
299 EmitBlock(Cont);
300 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000301 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000302 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
303 ThrowCall->setDoesNotReturn();
304 }
Anders Carlssond3379292009-10-30 02:27:02 +0000305 Builder.CreateUnreachable();
Mike Stump8755ec32009-12-10 00:06:18 +0000306
Anders Carlssond3379292009-10-30 02:27:02 +0000307 // Clear the insertion point to indicate we are in unreachable code.
308 Builder.ClearInsertionPoint();
Mike Stumpc2ab4862009-12-07 20:12:14 +0000309
310 // FIXME: For now, emit a dummy basic block because expr emitters in generally
311 // are not ready to handle emitting expressions at unreachable points.
312 EnsureInsertPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000313}
Mike Stump2bf701e2009-11-20 23:44:51 +0000314
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000315void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000316 if (!Exceptions)
317 return;
318
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000319 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
320 if (FD == 0)
321 return;
322 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
323 if (Proto == 0)
324 return;
325
326 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
327
328 if (!Proto->hasExceptionSpec())
329 return;
330
331 llvm::Constant *Personality =
332 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
333 (VMContext),
334 true),
335 "__gxx_personality_v0");
336 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
337 llvm::Value *llvm_eh_exception =
338 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
339 llvm::Value *llvm_eh_selector =
340 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
341 const llvm::IntegerType *Int8Ty;
342 const llvm::PointerType *PtrToInt8Ty;
343 Int8Ty = llvm::Type::getInt8Ty(VMContext);
344 // C string type. Used in lots of places.
345 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
346 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
347 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
348
349 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
350 llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
351 llvm::BasicBlock *Match = createBasicBlock("match");
352 llvm::BasicBlock *Unwind = 0;
353
354 assert(PrevLandingPad == 0 && "EHSpec has invoke context");
Chandler Carruth60cfcec2009-12-13 01:37:04 +0000355 (void)PrevLandingPad;
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000356
357 llvm::BasicBlock *Cont = createBasicBlock("cont");
358
359 EmitBranchThroughCleanup(Cont);
360
361 // Emit the statements in the try {} block
362 setInvokeDest(EHSpecHandler);
363
364 EmitBlock(EHSpecHandler);
365 // Exception object
366 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
367 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
368
369 SelectorArgs.push_back(Exc);
370 SelectorArgs.push_back(Personality);
371 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
372 Proto->getNumExceptions()+1));
373
374 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
375 QualType Ty = Proto->getExceptionType(i);
Douglas Gregor154fe982009-12-23 22:04:40 +0000376 QualType ExceptType
377 = Ty.getNonReferenceType().getUnqualifiedType();
378 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000379 SelectorArgs.push_back(EHType);
380 }
381 if (Proto->getNumExceptions())
382 SelectorArgs.push_back(Null);
383
384 // Find which handler was matched.
385 llvm::Value *Selector
386 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
387 SelectorArgs.end(), "selector");
388 if (Proto->getNumExceptions()) {
389 Unwind = createBasicBlock("Unwind");
390
391 Builder.CreateStore(Exc, RethrowPtr);
392 Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
393 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
394 0)),
395 Match, Unwind);
396
397 EmitBlock(Match);
398 }
399 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
400 Builder.CreateUnreachable();
401
402 if (Proto->getNumExceptions()) {
403 EmitBlock(Unwind);
404 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
405 Builder.CreateLoad(RethrowPtr));
406 Builder.CreateUnreachable();
407 }
408
409 EmitBlock(Cont);
410}
411
412void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
Anders Carlssona994ee42010-02-06 23:59:05 +0000413 if (!Exceptions)
414 return;
415
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000416 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
417 if (FD == 0)
418 return;
419 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
420 if (Proto == 0)
421 return;
422
423 if (!Proto->hasExceptionSpec())
424 return;
425
426 setInvokeDest(0);
427}
428
Mike Stump2bf701e2009-11-20 23:44:51 +0000429void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
John McCall9fc6a772010-02-19 09:25:03 +0000430 CXXTryStmtInfo Info = EnterCXXTryStmt(S);
431 EmitStmt(S.getTryBlock());
432 ExitCXXTryStmt(S, Info);
433}
434
435CodeGenFunction::CXXTryStmtInfo
436CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S) {
437 CXXTryStmtInfo Info;
438 Info.SavedLandingPad = getInvokeDest();
439 Info.HandlerBlock = createBasicBlock("try.handler");
440 Info.FinallyBlock = createBasicBlock("finally");
441
442 PushCleanupBlock(Info.FinallyBlock);
443 setInvokeDest(Info.HandlerBlock);
444
445 return Info;
446}
447
448void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S,
449 CXXTryStmtInfo TryInfo) {
Mike Stump2bf701e2009-11-20 23:44:51 +0000450 // Pointer to the personality function
451 llvm::Constant *Personality =
452 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
453 (VMContext),
454 true),
455 "__gxx_personality_v0");
456 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump639787c2009-12-02 19:53:57 +0000457 llvm::Value *llvm_eh_exception =
458 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
459 llvm::Value *llvm_eh_selector =
460 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000461
John McCall9fc6a772010-02-19 09:25:03 +0000462 llvm::BasicBlock *PrevLandingPad = TryInfo.SavedLandingPad;
463 llvm::BasicBlock *TryHandler = TryInfo.HandlerBlock;
464 llvm::BasicBlock *FinallyBlock = TryInfo.FinallyBlock;
Mike Stump0f590be2009-12-01 03:41:18 +0000465 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000466 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
467
Mike Stump2bf701e2009-11-20 23:44:51 +0000468 // Jump to end if there is no exception
469 EmitBranchThroughCleanup(FinallyEnd);
470
Mike Stump9b39c512009-12-09 22:59:31 +0000471 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump639787c2009-12-02 19:53:57 +0000472
Mike Stump2bf701e2009-11-20 23:44:51 +0000473 // Emit the handlers
474 EmitBlock(TryHandler);
Mike Stump8755ec32009-12-10 00:06:18 +0000475
Mike Stump2bf701e2009-11-20 23:44:51 +0000476 const llvm::IntegerType *Int8Ty;
477 const llvm::PointerType *PtrToInt8Ty;
478 Int8Ty = llvm::Type::getInt8Ty(VMContext);
479 // C string type. Used in lots of places.
480 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000481 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
482 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000483 llvm::Value *llvm_eh_typeid_for =
484 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000485 // Exception object
Mike Stump9b39c512009-12-09 22:59:31 +0000486 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000487 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000488
Mike Stump9b39c512009-12-09 22:59:31 +0000489 llvm::SmallVector<llvm::Value*, 8> Args;
Mike Stump639787c2009-12-02 19:53:57 +0000490 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000491 SelectorArgs.push_back(Exc);
492 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000493
Mike Stump0f590be2009-12-01 03:41:18 +0000494 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000495 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
496 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000497 VarDecl *CatchParam = C->getExceptionDecl();
498 if (CatchParam) {
Douglas Gregor154fe982009-12-23 22:04:40 +0000499 // C++ [except.handle]p3 indicates that top-level cv-qualifiers
500 // are ignored.
501 QualType CaughtType = C->getCaughtType().getNonReferenceType();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000502 llvm::Value *EHTypeInfo
Douglas Gregor154fe982009-12-23 22:04:40 +0000503 = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType());
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000504 SelectorArgs.push_back(EHTypeInfo);
Mike Stump2bf701e2009-11-20 23:44:51 +0000505 } else {
506 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000507 SelectorArgs.push_back(Null);
508 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000509 }
510 }
511
Mike Stump0f590be2009-12-01 03:41:18 +0000512 // We use a cleanup unless there was already a catch all.
513 if (!HasCatchAll) {
514 SelectorArgs.push_back(Null);
515 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000516
Mike Stump0f590be2009-12-01 03:41:18 +0000517 // Find which handler was matched.
518 llvm::Value *Selector
519 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
520 SelectorArgs.end(), "selector");
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 Stmt *CatchBody = C->getHandlerBlock();
525
526 llvm::BasicBlock *Next = 0;
527
528 if (SelectorArgs[i+2] != Null) {
529 llvm::BasicBlock *Match = createBasicBlock("match");
530 Next = createBasicBlock("catch.next");
531 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
532 llvm::Value *Id
533 = Builder.CreateCall(llvm_eh_typeid_for,
534 Builder.CreateBitCast(SelectorArgs[i+2],
535 Int8PtrTy));
536 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
537 Match, Next);
538 EmitBlock(Match);
539 }
540
541 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
542 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
543
544 PushCleanupBlock(MatchEnd);
545 setInvokeDest(MatchHandler);
546
547 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
548
Mike Stumpf7f74672009-12-02 23:37:16 +0000549 {
550 CleanupScope CatchScope(*this);
551 // Bind the catch parameter if it exists.
552 if (CatchParam) {
553 QualType CatchType = CatchParam->getType().getNonReferenceType();
554 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000555 bool WasPointer = true;
Mike Stump13b2f922010-01-01 03:20:32 +0000556 bool WasPointerReference = false;
Mike Stumpb606c382010-01-01 02:51:52 +0000557 CatchType = CGM.getContext().getCanonicalType(CatchType);
Mike Stump13b2f922010-01-01 03:20:32 +0000558 if (CatchType.getTypePtr()->isPointerType()) {
559 if (isa<ReferenceType>(CatchParam->getType()))
560 WasPointerReference = true;
561 } else {
Mike Stump0a3816e2009-12-04 01:51:45 +0000562 if (!isa<ReferenceType>(CatchParam->getType()))
563 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000564 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000565 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000566 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000567 EmitLocalBlockVarDecl(*CatchParam);
Mike Stumpf668bd02009-12-03 03:40:14 +0000568 // FIXME: we need to do this sooner so that the EH region for the
569 // cleanup doesn't start until after the ctor completes, use a decl
570 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000571 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stump13b2f922010-01-01 03:20:32 +0000572 WasPointer, WasPointerReference, ExcObject,
Mike Stumpb606c382010-01-01 02:51:52 +0000573 GetAddrOfLocalVar(CatchParam));
Mike Stumpf7f74672009-12-02 23:37:16 +0000574 setInvokeDest(MatchHandler);
575 }
576
577 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000578 }
579
Mike Stump0f590be2009-12-01 03:41:18 +0000580 EmitBranchThroughCleanup(FinallyEnd);
581
582 EmitBlock(MatchHandler);
583
584 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
585 // We are required to emit this call to satisfy LLVM, even
586 // though we don't use the result.
Mike Stump639787c2009-12-02 19:53:57 +0000587 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000588 Args.push_back(Exc);
589 Args.push_back(Personality);
590 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
591 0));
592 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
593 Builder.CreateStore(Exc, RethrowPtr);
594 EmitBranchThroughCleanup(FinallyRethrow);
595
596 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
597
598 EmitBlock(MatchEnd);
599
Mike Stump99533832009-12-02 07:41:41 +0000600 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000601 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000602 Cont, TerminateHandler,
Mike Stump0f590be2009-12-01 03:41:18 +0000603 Args.begin(), Args.begin());
Mike Stump0f590be2009-12-01 03:41:18 +0000604 EmitBlock(Cont);
605 if (Info.SwitchBlock)
606 EmitBlock(Info.SwitchBlock);
607 if (Info.EndBlock)
608 EmitBlock(Info.EndBlock);
609
Mike Stump0f590be2009-12-01 03:41:18 +0000610 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000611 Builder.CreateStore(Exc, RethrowPtr);
612 EmitBranchThroughCleanup(FinallyRethrow);
613
614 if (Next)
615 EmitBlock(Next);
616 }
Mike Stumpa0867832009-12-04 19:03:47 +0000617 if (!HasCatchAll) {
618 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000619 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000620 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000621
622 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
623
624 setInvokeDest(PrevLandingPad);
625
626 EmitBlock(FinallyBlock);
627
Mike Stump0f590be2009-12-01 03:41:18 +0000628 if (Info.SwitchBlock)
629 EmitBlock(Info.SwitchBlock);
630 if (Info.EndBlock)
631 EmitBlock(Info.EndBlock);
632
Mike Stump2bf701e2009-11-20 23:44:51 +0000633 // Branch around the rethrow code.
634 EmitBranch(FinallyEnd);
635
636 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000637 // FIXME: Eventually we can chain the handlers together and just do a call
638 // here.
639 if (getInvokeDest()) {
640 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
641 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(*this), Cont,
642 getInvokeDest(),
643 Builder.CreateLoad(RethrowPtr));
644 EmitBlock(Cont);
645 } else
646 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
647 Builder.CreateLoad(RethrowPtr));
648
Mike Stump2bf701e2009-11-20 23:44:51 +0000649 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000650
651 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000652}
Mike Stumpd88ea562009-12-09 03:35:49 +0000653
654CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
655 llvm::BasicBlock *Cont1 = CGF.createBasicBlock("cont");
656 CGF.EmitBranch(Cont1);
657 CGF.setInvokeDest(PreviousInvokeDest);
658
659
660 CGF.EmitBlock(CleanupHandler);
661
662 llvm::Constant *Personality =
663 CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
664 (CGF.VMContext),
665 true),
666 "__gxx_personality_v0");
667 Personality = llvm::ConstantExpr::getBitCast(Personality, CGF.PtrToInt8Ty);
668 llvm::Value *llvm_eh_exception =
669 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
670 llvm::Value *llvm_eh_selector =
671 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
672
673 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
674 const llvm::IntegerType *Int8Ty;
675 const llvm::PointerType *PtrToInt8Ty;
676 Int8Ty = llvm::Type::getInt8Ty(CGF.VMContext);
677 // C string type. Used in lots of places.
678 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
679 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
680 llvm::SmallVector<llvm::Value*, 8> Args;
681 Args.clear();
682 Args.push_back(Exc);
683 Args.push_back(Personality);
684 Args.push_back(Null);
685 CGF.Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
686
687 CGF.EmitBlock(CleanupEntryBB);
688
689 CGF.EmitBlock(Cont1);
690
691 if (CGF.getInvokeDest()) {
692 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
693 CGF.Builder.CreateInvoke(getUnwindResumeOrRethrowFn(CGF), Cont,
694 CGF.getInvokeDest(), Exc);
695 CGF.EmitBlock(Cont);
696 } else
697 CGF.Builder.CreateCall(getUnwindResumeOrRethrowFn(CGF), Exc);
698
699 CGF.Builder.CreateUnreachable();
700
701 CGF.EmitBlock(Cont);
702 if (CGF.Exceptions)
703 CGF.setInvokeDest(CleanupHandler);
704}
Mike Stump9b39c512009-12-09 22:59:31 +0000705
706llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +0000707 if (TerminateHandler)
708 return TerminateHandler;
709
Mike Stump76958092009-12-09 23:31:35 +0000710 llvm::BasicBlock *Cont = 0;
711
712 if (HaveInsertPoint()) {
713 Cont = createBasicBlock("cont");
714 EmitBranch(Cont);
715 }
716
Mike Stump9b39c512009-12-09 22:59:31 +0000717 llvm::Constant *Personality =
718 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
719 (VMContext),
720 true),
721 "__gxx_personality_v0");
722 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
723 llvm::Value *llvm_eh_exception =
724 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
725 llvm::Value *llvm_eh_selector =
726 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
727
728 // Set up terminate handler
Mike Stump182f3832009-12-10 00:02:42 +0000729 TerminateHandler = createBasicBlock("terminate.handler");
Mike Stump9b39c512009-12-09 22:59:31 +0000730 EmitBlock(TerminateHandler);
731 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
732 // We are required to emit this call to satisfy LLVM, even
733 // though we don't use the result.
734 llvm::SmallVector<llvm::Value*, 8> Args;
735 Args.push_back(Exc);
736 Args.push_back(Personality);
737 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Mike Stump76958092009-12-09 23:31:35 +0000738 1));
Mike Stump9b39c512009-12-09 22:59:31 +0000739 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
Mike Stump8755ec32009-12-10 00:06:18 +0000740 llvm::CallInst *TerminateCall =
Mike Stump9b39c512009-12-09 22:59:31 +0000741 Builder.CreateCall(getTerminateFn(*this));
742 TerminateCall->setDoesNotReturn();
743 TerminateCall->setDoesNotThrow();
744 Builder.CreateUnreachable();
745
746 // Clear the insertion point to indicate we are in unreachable code.
747 Builder.ClearInsertionPoint();
748
Mike Stump76958092009-12-09 23:31:35 +0000749 if (Cont)
750 EmitBlock(Cont);
751
Mike Stump9b39c512009-12-09 22:59:31 +0000752 return TerminateHandler;
753}