blob: b15b2e9b3b0f988840c4d2400a33d8edbac9d169 [file] [log] [blame]
Anders Carlsson756b5c42009-10-30 01:42:31 +00001//===--- CGException.cpp - Emit LLVM Code for C++ exceptions --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This contains code dealing with C++ exception related code generation.
11//
12//===----------------------------------------------------------------------===//
13
Mike Stump2bf701e2009-11-20 23:44:51 +000014#include "clang/AST/StmtCXX.h"
15
16#include "llvm/Intrinsics.h"
17
Anders Carlsson756b5c42009-10-30 01:42:31 +000018#include "CodeGenFunction.h"
19using namespace clang;
20using namespace CodeGen;
21
Anders Carlssond3379292009-10-30 02:27:02 +000022static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) {
23 // void *__cxa_allocate_exception(size_t thrown_size);
24 const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
25 std::vector<const llvm::Type*> Args(1, SizeTy);
Mike Stump8755ec32009-12-10 00:06:18 +000026
27 const llvm::FunctionType *FTy =
Anders Carlssond3379292009-10-30 02:27:02 +000028 llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()),
29 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000030
Anders Carlssond3379292009-10-30 02:27:02 +000031 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
32}
33
Mike Stump99533832009-12-02 07:41:41 +000034static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) {
35 // void __cxa_free_exception(void *thrown_exception);
36 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
37 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +000038
39 const llvm::FunctionType *FTy =
Mike Stump99533832009-12-02 07:41:41 +000040 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
41 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000042
Mike Stump99533832009-12-02 07:41:41 +000043 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
44}
45
Anders Carlssond3379292009-10-30 02:27:02 +000046static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
Mike Stump8755ec32009-12-10 00:06:18 +000047 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
Mike Stump99533832009-12-02 07:41:41 +000048 // void (*dest) (void *));
Anders Carlssond3379292009-10-30 02:27:02 +000049
50 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
51 std::vector<const llvm::Type*> Args(3, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +000052
53 const llvm::FunctionType *FTy =
Mike Stumpb4eea692009-11-20 00:56:31 +000054 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
55 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000056
Anders Carlssond3379292009-10-30 02:27:02 +000057 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
58}
59
Mike Stumpb4eea692009-11-20 00:56:31 +000060static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000061 // void __cxa_rethrow();
Mike Stumpb4eea692009-11-20 00:56:31 +000062
Mike Stump8755ec32009-12-10 00:06:18 +000063 const llvm::FunctionType *FTy =
Mike Stumpb4eea692009-11-20 00:56:31 +000064 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +000065
Mike Stumpb4eea692009-11-20 00:56:31 +000066 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
67}
68
Mike Stump2bf701e2009-11-20 23:44:51 +000069static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000070 // void* __cxa_begin_catch();
Mike Stump2bf701e2009-11-20 23:44:51 +000071
72 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
73 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +000074
75 const llvm::FunctionType *FTy =
Mike Stump0f590be2009-12-01 03:41:18 +000076 llvm::FunctionType::get(Int8PtrTy, Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000077
Mike Stump2bf701e2009-11-20 23:44:51 +000078 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
79}
80
81static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000082 // void __cxa_end_catch();
Mike Stump2bf701e2009-11-20 23:44:51 +000083
Mike Stump8755ec32009-12-10 00:06:18 +000084 const llvm::FunctionType *FTy =
Mike Stump2bf701e2009-11-20 23:44:51 +000085 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +000086
Mike Stump2bf701e2009-11-20 23:44:51 +000087 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
88}
89
Mike Stumpcce3d4f2009-12-07 23:38:24 +000090static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) {
91 // void __cxa_call_unexepcted(void *thrown_exception);
92
93 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
94 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +000095
96 const llvm::FunctionType *FTy =
Mike Stumpcce3d4f2009-12-07 23:38:24 +000097 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
98 Args, false);
Mike Stump8755ec32009-12-10 00:06:18 +000099
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000100 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
101}
102
Mike Stump0f590be2009-12-01 03:41:18 +0000103// FIXME: Eventually this will all go into the backend. Set from the target for
104// now.
105static int using_sjlj_exceptions = 0;
106
107static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) {
108 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
109 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000110
111 const llvm::FunctionType *FTy =
Mike Stump0f590be2009-12-01 03:41:18 +0000112 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args,
113 false);
Mike Stump8755ec32009-12-10 00:06:18 +0000114
Mike Stump0f590be2009-12-01 03:41:18 +0000115 if (using_sjlj_exceptions)
116 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
117 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
118}
119
Mike Stump99533832009-12-02 07:41:41 +0000120static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
121 // void __terminate();
122
Mike Stump8755ec32009-12-10 00:06:18 +0000123 const llvm::FunctionType *FTy =
Mike Stump99533832009-12-02 07:41:41 +0000124 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
Mike Stump8755ec32009-12-10 00:06:18 +0000125
Mike Stump99533832009-12-02 07:41:41 +0000126 return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev");
127}
128
Mike Stump0f590be2009-12-01 03:41:18 +0000129// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
Anders Carlsson8370c582009-12-11 00:32:37 +0000130// DestPtr is casted to the right type.
131static void CopyObject(CodeGenFunction &CGF, const Expr *E,
132 llvm::Value *DestPtr, llvm::Value *ExceptionPtrPtr) {
Mike Stump0f590be2009-12-01 03:41:18 +0000133 QualType ObjectType = E->getType();
134
135 // Store the throw exception in the exception object.
136 if (!CGF.hasAggregateLLVMType(ObjectType)) {
137 llvm::Value *Value = CGF.EmitScalarExpr(E);
Anders Carlsson8370c582009-12-11 00:32:37 +0000138 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo();
Mike Stump8755ec32009-12-10 00:06:18 +0000139
Anders Carlsson8370c582009-12-11 00:32:37 +0000140 CGF.Builder.CreateStore(Value,
141 CGF.Builder.CreateBitCast(DestPtr, ValuePtrTy));
Mike Stump0f590be2009-12-01 03:41:18 +0000142 } else {
Anders Carlsson8370c582009-12-11 00:32:37 +0000143 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo();
144 const CXXRecordDecl *RD =
145 cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
146
147 llvm::Value *This = CGF.Builder.CreateBitCast(DestPtr, Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000148 if (RD->hasTrivialCopyConstructor()) {
149 CGF.EmitAggExpr(E, This, false);
150 } else if (CXXConstructorDecl *CopyCtor
151 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump5030a982009-12-10 01:52:30 +0000152 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
153 if (CGF.Exceptions) {
154 CodeGenFunction::EHCleanupBlock Cleanup(CGF);
Mike Stump99533832009-12-02 07:41:41 +0000155 llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
Anders Carlsson8370c582009-12-11 00:32:37 +0000156
157 // Load the exception pointer.
158 llvm::Value *ExceptionPtr = CGF.Builder.CreateLoad(ExceptionPtrPtr);
Mike Stump99533832009-12-02 07:41:41 +0000159 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
160 }
Mike Stump0924a8a2009-12-11 00:02:10 +0000161
Mike Stump0f590be2009-12-01 03:41:18 +0000162 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
Mike Stump5030a982009-12-10 01:52:30 +0000163 CGF.setInvokeDest(PrevLandingPad);
Mike Stump0f590be2009-12-01 03:41:18 +0000164
Mike Stump76958092009-12-09 23:31:35 +0000165 llvm::BasicBlock *TerminateHandler = CGF.getTerminateHandler();
Mike Stump5030a982009-12-10 01:52:30 +0000166 PrevLandingPad = CGF.getInvokeDest();
Mike Stump76958092009-12-09 23:31:35 +0000167 CGF.setInvokeDest(TerminateHandler);
168
Mike Stump0f590be2009-12-01 03:41:18 +0000169 // Stolen from EmitClassAggrMemberwiseCopy
170 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
171 Ctor_Complete);
172 CallArgList CallArgs;
173 CallArgs.push_back(std::make_pair(RValue::get(This),
174 CopyCtor->getThisType(CGF.getContext())));
175
176 // Push the Src ptr.
177 CallArgs.push_back(std::make_pair(RValue::get(Src),
178 CopyCtor->getParamDecl(0)->getType()));
179 QualType ResultType =
180 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
181 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
182 Callee, CallArgs, CopyCtor);
Mike Stump76958092009-12-09 23:31:35 +0000183 CGF.setInvokeDest(PrevLandingPad);
Mike Stump0f590be2009-12-01 03:41:18 +0000184 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000185 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000186 }
187}
188
189// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
190// N is casted to the right type.
191static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000192 bool WasPointer, llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000193 // Store the throw exception in the exception object.
Mike Stumpb2debeb2009-12-08 01:29:31 +0000194 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000195 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000196 if (!WasPointer)
197 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000198 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump0f590be2009-12-01 03:41:18 +0000199 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
200 } else {
201 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
202 const CXXRecordDecl *RD;
203 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
204 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
205 if (RD->hasTrivialCopyConstructor()) {
206 CGF.EmitAggregateCopy(This, E, ObjectType);
207 } else if (CXXConstructorDecl *CopyCtor
208 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000209 llvm::Value *Src = E;
210
211 // Stolen from EmitClassAggrMemberwiseCopy
212 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
213 Ctor_Complete);
214 CallArgList CallArgs;
215 CallArgs.push_back(std::make_pair(RValue::get(This),
216 CopyCtor->getThisType(CGF.getContext())));
217
218 // Push the Src ptr.
219 CallArgs.push_back(std::make_pair(RValue::get(Src),
220 CopyCtor->getParamDecl(0)->getType()));
221 QualType ResultType =
222 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
223 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
224 Callee, CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000225 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000226 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000227 }
228}
229
Anders Carlsson756b5c42009-10-30 01:42:31 +0000230void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000231 if (!E->getSubExpr()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000232 if (getInvokeDest()) {
233 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
234 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
235 ->setDoesNotReturn();
236 EmitBlock(Cont);
237 } else
238 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
Mike Stumpb4eea692009-11-20 00:56:31 +0000239 Builder.CreateUnreachable();
240
241 // Clear the insertion point to indicate we are in unreachable code.
242 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000243 return;
244 }
Mike Stump8755ec32009-12-10 00:06:18 +0000245
Anders Carlssond3379292009-10-30 02:27:02 +0000246 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000247
Anders Carlssond3379292009-10-30 02:27:02 +0000248 // Now allocate the exception object.
249 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
250 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
Mike Stump8755ec32009-12-10 00:06:18 +0000251
Anders Carlssond3379292009-10-30 02:27:02 +0000252 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
Mike Stump8755ec32009-12-10 00:06:18 +0000253 llvm::Value *ExceptionPtr =
254 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000255 llvm::ConstantInt::get(SizeTy, TypeSize),
256 "exception");
Anders Carlsson8370c582009-12-11 00:32:37 +0000257
258 llvm::Value *ExceptionPtrPtr =
259 CreateTempAlloca(ExceptionPtr->getType(), "exception.ptr");
260 Builder.CreateStore(ExceptionPtr, ExceptionPtrPtr);
Anders Carlssond3379292009-10-30 02:27:02 +0000261
Anders Carlsson8370c582009-12-11 00:32:37 +0000262
263 CopyObject(*this, E->getSubExpr(), ExceptionPtr, ExceptionPtrPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000264
Anders Carlssond3379292009-10-30 02:27:02 +0000265 // Now throw the exception.
266 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stumpde050572009-12-02 18:57:08 +0000267 llvm::Constant *TypeInfo = CGM.GenerateRTTI(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000268 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000269
Mike Stump0a3816e2009-12-04 01:51:45 +0000270 if (getInvokeDest()) {
271 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump8755ec32009-12-10 00:06:18 +0000272 llvm::InvokeInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000273 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
274 ExceptionPtr, TypeInfo, Dtor);
275 ThrowCall->setDoesNotReturn();
276 EmitBlock(Cont);
277 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000278 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000279 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
280 ThrowCall->setDoesNotReturn();
281 }
Anders Carlssond3379292009-10-30 02:27:02 +0000282 Builder.CreateUnreachable();
Mike Stump8755ec32009-12-10 00:06:18 +0000283
Anders Carlssond3379292009-10-30 02:27:02 +0000284 // Clear the insertion point to indicate we are in unreachable code.
285 Builder.ClearInsertionPoint();
Mike Stumpc2ab4862009-12-07 20:12:14 +0000286
287 // FIXME: For now, emit a dummy basic block because expr emitters in generally
288 // are not ready to handle emitting expressions at unreachable points.
289 EnsureInsertPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000290}
Mike Stump2bf701e2009-11-20 23:44:51 +0000291
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000292void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
293 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
294 if (FD == 0)
295 return;
296 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
297 if (Proto == 0)
298 return;
299
300 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
301
302 if (!Proto->hasExceptionSpec())
303 return;
304
305 llvm::Constant *Personality =
306 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
307 (VMContext),
308 true),
309 "__gxx_personality_v0");
310 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
311 llvm::Value *llvm_eh_exception =
312 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
313 llvm::Value *llvm_eh_selector =
314 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
315 const llvm::IntegerType *Int8Ty;
316 const llvm::PointerType *PtrToInt8Ty;
317 Int8Ty = llvm::Type::getInt8Ty(VMContext);
318 // C string type. Used in lots of places.
319 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
320 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
321 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
322
323 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
324 llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
325 llvm::BasicBlock *Match = createBasicBlock("match");
326 llvm::BasicBlock *Unwind = 0;
327
328 assert(PrevLandingPad == 0 && "EHSpec has invoke context");
Chandler Carruth60cfcec2009-12-13 01:37:04 +0000329 (void)PrevLandingPad;
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000330
331 llvm::BasicBlock *Cont = createBasicBlock("cont");
332
333 EmitBranchThroughCleanup(Cont);
334
335 // Emit the statements in the try {} block
336 setInvokeDest(EHSpecHandler);
337
338 EmitBlock(EHSpecHandler);
339 // Exception object
340 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
341 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
342
343 SelectorArgs.push_back(Exc);
344 SelectorArgs.push_back(Personality);
345 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
346 Proto->getNumExceptions()+1));
347
348 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
349 QualType Ty = Proto->getExceptionType(i);
350 llvm::Value *EHType
351 = CGM.GenerateRTTI(Ty.getNonReferenceType());
352 SelectorArgs.push_back(EHType);
353 }
354 if (Proto->getNumExceptions())
355 SelectorArgs.push_back(Null);
356
357 // Find which handler was matched.
358 llvm::Value *Selector
359 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
360 SelectorArgs.end(), "selector");
361 if (Proto->getNumExceptions()) {
362 Unwind = createBasicBlock("Unwind");
363
364 Builder.CreateStore(Exc, RethrowPtr);
365 Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
366 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
367 0)),
368 Match, Unwind);
369
370 EmitBlock(Match);
371 }
372 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
373 Builder.CreateUnreachable();
374
375 if (Proto->getNumExceptions()) {
376 EmitBlock(Unwind);
377 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
378 Builder.CreateLoad(RethrowPtr));
379 Builder.CreateUnreachable();
380 }
381
382 EmitBlock(Cont);
383}
384
385void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
386 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
387 if (FD == 0)
388 return;
389 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
390 if (Proto == 0)
391 return;
392
393 if (!Proto->hasExceptionSpec())
394 return;
395
396 setInvokeDest(0);
397}
398
Mike Stump2bf701e2009-11-20 23:44:51 +0000399void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
Mike Stump2bf701e2009-11-20 23:44:51 +0000400 // Pointer to the personality function
401 llvm::Constant *Personality =
402 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
403 (VMContext),
404 true),
405 "__gxx_personality_v0");
406 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump639787c2009-12-02 19:53:57 +0000407 llvm::Value *llvm_eh_exception =
408 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
409 llvm::Value *llvm_eh_selector =
410 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000411
Mike Stump2bf701e2009-11-20 23:44:51 +0000412 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
413 llvm::BasicBlock *TryHandler = createBasicBlock("try.handler");
Mike Stump2bf701e2009-11-20 23:44:51 +0000414 llvm::BasicBlock *FinallyBlock = createBasicBlock("finally");
Mike Stump0f590be2009-12-01 03:41:18 +0000415 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000416 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
417
418 // Push an EH context entry, used for handling rethrows.
419 PushCleanupBlock(FinallyBlock);
420
421 // Emit the statements in the try {} block
422 setInvokeDest(TryHandler);
423
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000424 // FIXME: We should not have to do this here. The AST should have the member
425 // initializers under the CXXTryStmt's TryBlock.
426 if (OuterTryBlock == &S) {
427 GlobalDecl GD = CurGD;
428 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
429
430 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
431 size_t OldCleanupStackSize = CleanupEntries.size();
432 EmitCtorPrologue(CD, CurGD.getCtorType());
433 EmitStmt(S.getTryBlock());
434
435 // If any of the member initializers are temporaries bound to references
436 // make sure to emit their destructors.
437 EmitCleanupBlocks(OldCleanupStackSize);
438 } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
439 llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue");
440 PushCleanupBlock(DtorEpilogue);
441
442 EmitStmt(S.getTryBlock());
Mike Stump8755ec32009-12-10 00:06:18 +0000443
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000444 CleanupBlockInfo Info = PopCleanupBlock();
445
446 assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!");
447 EmitBlock(DtorEpilogue);
448 EmitDtorEpilogue(DD, GD.getDtorType());
Mike Stump8755ec32009-12-10 00:06:18 +0000449
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000450 if (Info.SwitchBlock)
451 EmitBlock(Info.SwitchBlock);
452 if (Info.EndBlock)
453 EmitBlock(Info.EndBlock);
Mike Stump8755ec32009-12-10 00:06:18 +0000454 } else
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000455 EmitStmt(S.getTryBlock());
456 } else
457 EmitStmt(S.getTryBlock());
Mike Stump2bf701e2009-11-20 23:44:51 +0000458
459 // Jump to end if there is no exception
460 EmitBranchThroughCleanup(FinallyEnd);
461
Mike Stump9b39c512009-12-09 22:59:31 +0000462 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump639787c2009-12-02 19:53:57 +0000463
Mike Stump2bf701e2009-11-20 23:44:51 +0000464 // Emit the handlers
465 EmitBlock(TryHandler);
Mike Stump8755ec32009-12-10 00:06:18 +0000466
Mike Stump2bf701e2009-11-20 23:44:51 +0000467 const llvm::IntegerType *Int8Ty;
468 const llvm::PointerType *PtrToInt8Ty;
469 Int8Ty = llvm::Type::getInt8Ty(VMContext);
470 // C string type. Used in lots of places.
471 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000472 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
473 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000474 llvm::Value *llvm_eh_typeid_for =
475 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000476 // Exception object
Mike Stump9b39c512009-12-09 22:59:31 +0000477 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000478 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000479
Mike Stump9b39c512009-12-09 22:59:31 +0000480 llvm::SmallVector<llvm::Value*, 8> Args;
Mike Stump639787c2009-12-02 19:53:57 +0000481 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000482 SelectorArgs.push_back(Exc);
483 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000484
Mike Stump0f590be2009-12-01 03:41:18 +0000485 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000486 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
487 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000488 VarDecl *CatchParam = C->getExceptionDecl();
489 if (CatchParam) {
Mike Stumpde050572009-12-02 18:57:08 +0000490 llvm::Value *EHType
491 = CGM.GenerateRTTI(C->getCaughtType().getNonReferenceType());
Mike Stump0f590be2009-12-01 03:41:18 +0000492 SelectorArgs.push_back(EHType);
Mike Stump2bf701e2009-11-20 23:44:51 +0000493 } else {
494 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000495 SelectorArgs.push_back(Null);
496 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000497 }
498 }
499
Mike Stump0f590be2009-12-01 03:41:18 +0000500 // We use a cleanup unless there was already a catch all.
501 if (!HasCatchAll) {
502 SelectorArgs.push_back(Null);
503 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000504
Mike Stump0f590be2009-12-01 03:41:18 +0000505 // Find which handler was matched.
506 llvm::Value *Selector
507 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
508 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000509 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
510 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000511 VarDecl *CatchParam = C->getExceptionDecl();
512 Stmt *CatchBody = C->getHandlerBlock();
513
514 llvm::BasicBlock *Next = 0;
515
516 if (SelectorArgs[i+2] != Null) {
517 llvm::BasicBlock *Match = createBasicBlock("match");
518 Next = createBasicBlock("catch.next");
519 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
520 llvm::Value *Id
521 = Builder.CreateCall(llvm_eh_typeid_for,
522 Builder.CreateBitCast(SelectorArgs[i+2],
523 Int8PtrTy));
524 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
525 Match, Next);
526 EmitBlock(Match);
527 }
528
529 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
530 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
531
532 PushCleanupBlock(MatchEnd);
533 setInvokeDest(MatchHandler);
534
535 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
536
Mike Stumpf7f74672009-12-02 23:37:16 +0000537 {
538 CleanupScope CatchScope(*this);
539 // Bind the catch parameter if it exists.
540 if (CatchParam) {
541 QualType CatchType = CatchParam->getType().getNonReferenceType();
542 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000543 bool WasPointer = true;
544 if (!CatchType.getTypePtr()->isPointerType()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000545 if (!isa<ReferenceType>(CatchParam->getType()))
546 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000547 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000548 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000549 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000550 EmitLocalBlockVarDecl(*CatchParam);
Mike Stumpf668bd02009-12-03 03:40:14 +0000551 // FIXME: we need to do this sooner so that the EH region for the
552 // cleanup doesn't start until after the ctor completes, use a decl
553 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000554 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000555 WasPointer, ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stumpf7f74672009-12-02 23:37:16 +0000556 setInvokeDest(MatchHandler);
557 }
558
559 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000560 }
561
Mike Stump0f590be2009-12-01 03:41:18 +0000562 EmitBranchThroughCleanup(FinallyEnd);
563
564 EmitBlock(MatchHandler);
565
566 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
567 // We are required to emit this call to satisfy LLVM, even
568 // though we don't use the result.
Mike Stump639787c2009-12-02 19:53:57 +0000569 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000570 Args.push_back(Exc);
571 Args.push_back(Personality);
572 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
573 0));
574 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
575 Builder.CreateStore(Exc, RethrowPtr);
576 EmitBranchThroughCleanup(FinallyRethrow);
577
578 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
579
580 EmitBlock(MatchEnd);
581
Mike Stump99533832009-12-02 07:41:41 +0000582 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000583 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000584 Cont, TerminateHandler,
Mike Stump0f590be2009-12-01 03:41:18 +0000585 Args.begin(), Args.begin());
Mike Stump0f590be2009-12-01 03:41:18 +0000586 EmitBlock(Cont);
587 if (Info.SwitchBlock)
588 EmitBlock(Info.SwitchBlock);
589 if (Info.EndBlock)
590 EmitBlock(Info.EndBlock);
591
Mike Stump0f590be2009-12-01 03:41:18 +0000592 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000593 Builder.CreateStore(Exc, RethrowPtr);
594 EmitBranchThroughCleanup(FinallyRethrow);
595
596 if (Next)
597 EmitBlock(Next);
598 }
Mike Stumpa0867832009-12-04 19:03:47 +0000599 if (!HasCatchAll) {
600 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000601 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000602 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000603
604 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
605
606 setInvokeDest(PrevLandingPad);
607
608 EmitBlock(FinallyBlock);
609
Mike Stump0f590be2009-12-01 03:41:18 +0000610 if (Info.SwitchBlock)
611 EmitBlock(Info.SwitchBlock);
612 if (Info.EndBlock)
613 EmitBlock(Info.EndBlock);
614
Mike Stump2bf701e2009-11-20 23:44:51 +0000615 // Branch around the rethrow code.
616 EmitBranch(FinallyEnd);
617
618 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000619 // FIXME: Eventually we can chain the handlers together and just do a call
620 // here.
621 if (getInvokeDest()) {
622 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
623 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(*this), Cont,
624 getInvokeDest(),
625 Builder.CreateLoad(RethrowPtr));
626 EmitBlock(Cont);
627 } else
628 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
629 Builder.CreateLoad(RethrowPtr));
630
Mike Stump2bf701e2009-11-20 23:44:51 +0000631 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000632
633 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000634}
Mike Stumpd88ea562009-12-09 03:35:49 +0000635
636CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
637 llvm::BasicBlock *Cont1 = CGF.createBasicBlock("cont");
638 CGF.EmitBranch(Cont1);
639 CGF.setInvokeDest(PreviousInvokeDest);
640
641
642 CGF.EmitBlock(CleanupHandler);
643
644 llvm::Constant *Personality =
645 CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
646 (CGF.VMContext),
647 true),
648 "__gxx_personality_v0");
649 Personality = llvm::ConstantExpr::getBitCast(Personality, CGF.PtrToInt8Ty);
650 llvm::Value *llvm_eh_exception =
651 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
652 llvm::Value *llvm_eh_selector =
653 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
654
655 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
656 const llvm::IntegerType *Int8Ty;
657 const llvm::PointerType *PtrToInt8Ty;
658 Int8Ty = llvm::Type::getInt8Ty(CGF.VMContext);
659 // C string type. Used in lots of places.
660 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
661 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
662 llvm::SmallVector<llvm::Value*, 8> Args;
663 Args.clear();
664 Args.push_back(Exc);
665 Args.push_back(Personality);
666 Args.push_back(Null);
667 CGF.Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
668
669 CGF.EmitBlock(CleanupEntryBB);
670
671 CGF.EmitBlock(Cont1);
672
673 if (CGF.getInvokeDest()) {
674 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
675 CGF.Builder.CreateInvoke(getUnwindResumeOrRethrowFn(CGF), Cont,
676 CGF.getInvokeDest(), Exc);
677 CGF.EmitBlock(Cont);
678 } else
679 CGF.Builder.CreateCall(getUnwindResumeOrRethrowFn(CGF), Exc);
680
681 CGF.Builder.CreateUnreachable();
682
683 CGF.EmitBlock(Cont);
684 if (CGF.Exceptions)
685 CGF.setInvokeDest(CleanupHandler);
686}
Mike Stump9b39c512009-12-09 22:59:31 +0000687
688llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +0000689 if (TerminateHandler)
690 return TerminateHandler;
691
Mike Stump76958092009-12-09 23:31:35 +0000692 llvm::BasicBlock *Cont = 0;
693
694 if (HaveInsertPoint()) {
695 Cont = createBasicBlock("cont");
696 EmitBranch(Cont);
697 }
698
Mike Stump9b39c512009-12-09 22:59:31 +0000699 llvm::Constant *Personality =
700 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
701 (VMContext),
702 true),
703 "__gxx_personality_v0");
704 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
705 llvm::Value *llvm_eh_exception =
706 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
707 llvm::Value *llvm_eh_selector =
708 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
709
710 // Set up terminate handler
Mike Stump182f3832009-12-10 00:02:42 +0000711 TerminateHandler = createBasicBlock("terminate.handler");
Mike Stump9b39c512009-12-09 22:59:31 +0000712 EmitBlock(TerminateHandler);
713 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
714 // We are required to emit this call to satisfy LLVM, even
715 // though we don't use the result.
716 llvm::SmallVector<llvm::Value*, 8> Args;
717 Args.push_back(Exc);
718 Args.push_back(Personality);
719 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Mike Stump76958092009-12-09 23:31:35 +0000720 1));
Mike Stump9b39c512009-12-09 22:59:31 +0000721 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
Mike Stump8755ec32009-12-10 00:06:18 +0000722 llvm::CallInst *TerminateCall =
Mike Stump9b39c512009-12-09 22:59:31 +0000723 Builder.CreateCall(getTerminateFn(*this));
724 TerminateCall->setDoesNotReturn();
725 TerminateCall->setDoesNotThrow();
726 Builder.CreateUnreachable();
727
728 // Clear the insertion point to indicate we are in unreachable code.
729 Builder.ClearInsertionPoint();
730
Mike Stump76958092009-12-09 23:31:35 +0000731 if (Cont)
732 EmitBlock(Cont);
733
Mike Stump9b39c512009-12-09 22:59:31 +0000734 return TerminateHandler;
735}