blob: 63e8679408e4d228c903bdeb352df6e4b226ffb0 [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 Stumpf2945c02009-12-17 06:08:47 +0000152 llvm::Value *CondPtr = 0;
Mike Stump5030a982009-12-10 01:52:30 +0000153 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
Mike Stumpf2945c02009-12-17 06:08:47 +0000157 llvm::BasicBlock *CondBlock = CGF.createBasicBlock("cond.free");
158 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
159 CondPtr = CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()),
160 "doEHfree");
161
162 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CondPtr),
163 CondBlock, Cont);
164 CGF.EmitBlock(CondBlock);
165
Anders Carlsson8370c582009-12-11 00:32:37 +0000166 // Load the exception pointer.
167 llvm::Value *ExceptionPtr = CGF.Builder.CreateLoad(ExceptionPtrPtr);
Mike Stump99533832009-12-02 07:41:41 +0000168 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
Mike Stumpf2945c02009-12-17 06:08:47 +0000169
170 CGF.EmitBlock(Cont);
Mike Stump99533832009-12-02 07:41:41 +0000171 }
Mike Stump0924a8a2009-12-11 00:02:10 +0000172
Mike Stumpf2945c02009-12-17 06:08:47 +0000173 if (CondPtr)
174 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()),
175 CondPtr);
176
Mike Stump0f590be2009-12-01 03:41:18 +0000177 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
Mike Stumpf2945c02009-12-17 06:08:47 +0000178
Mike Stumpf2945c02009-12-17 06:08:47 +0000179 if (CondPtr)
180 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()),
181 CondPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000182
Mike Stump76958092009-12-09 23:31:35 +0000183 llvm::BasicBlock *TerminateHandler = CGF.getTerminateHandler();
Mike Stump63df2ae2009-12-24 06:52:05 +0000184 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Mike Stump76958092009-12-09 23:31:35 +0000185 CGF.setInvokeDest(TerminateHandler);
186
Mike Stump0f590be2009-12-01 03:41:18 +0000187 // Stolen from EmitClassAggrMemberwiseCopy
188 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
189 Ctor_Complete);
190 CallArgList CallArgs;
191 CallArgs.push_back(std::make_pair(RValue::get(This),
192 CopyCtor->getThisType(CGF.getContext())));
193
194 // Push the Src ptr.
195 CallArgs.push_back(std::make_pair(RValue::get(Src),
196 CopyCtor->getParamDecl(0)->getType()));
197 QualType ResultType =
198 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
199 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
200 Callee, CallArgs, CopyCtor);
Mike Stump76958092009-12-09 23:31:35 +0000201 CGF.setInvokeDest(PrevLandingPad);
Mike Stump0f590be2009-12-01 03:41:18 +0000202 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000203 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000204 }
205}
206
207// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
208// N is casted to the right type.
209static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000210 bool WasPointer, llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000211 // Store the throw exception in the exception object.
Mike Stumpb2debeb2009-12-08 01:29:31 +0000212 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000213 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000214 if (!WasPointer)
215 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000216 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump0f590be2009-12-01 03:41:18 +0000217 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
218 } else {
219 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
220 const CXXRecordDecl *RD;
221 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
222 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
223 if (RD->hasTrivialCopyConstructor()) {
224 CGF.EmitAggregateCopy(This, E, ObjectType);
225 } else if (CXXConstructorDecl *CopyCtor
226 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000227 llvm::Value *Src = E;
228
229 // Stolen from EmitClassAggrMemberwiseCopy
230 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
231 Ctor_Complete);
232 CallArgList CallArgs;
233 CallArgs.push_back(std::make_pair(RValue::get(This),
234 CopyCtor->getThisType(CGF.getContext())));
235
236 // Push the Src ptr.
237 CallArgs.push_back(std::make_pair(RValue::get(Src),
238 CopyCtor->getParamDecl(0)->getType()));
239 QualType ResultType =
240 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
241 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
242 Callee, CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000243 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000244 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000245 }
246}
247
Anders Carlsson756b5c42009-10-30 01:42:31 +0000248void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000249 if (!E->getSubExpr()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000250 if (getInvokeDest()) {
251 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
252 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
253 ->setDoesNotReturn();
254 EmitBlock(Cont);
255 } else
256 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
Mike Stumpb4eea692009-11-20 00:56:31 +0000257 Builder.CreateUnreachable();
258
259 // Clear the insertion point to indicate we are in unreachable code.
260 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000261 return;
262 }
Mike Stump8755ec32009-12-10 00:06:18 +0000263
Anders Carlssond3379292009-10-30 02:27:02 +0000264 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000265
Anders Carlssond3379292009-10-30 02:27:02 +0000266 // Now allocate the exception object.
267 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
268 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
Mike Stump8755ec32009-12-10 00:06:18 +0000269
Anders Carlssond3379292009-10-30 02:27:02 +0000270 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
Mike Stump8755ec32009-12-10 00:06:18 +0000271 llvm::Value *ExceptionPtr =
272 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000273 llvm::ConstantInt::get(SizeTy, TypeSize),
274 "exception");
Anders Carlsson8370c582009-12-11 00:32:37 +0000275
276 llvm::Value *ExceptionPtrPtr =
277 CreateTempAlloca(ExceptionPtr->getType(), "exception.ptr");
278 Builder.CreateStore(ExceptionPtr, ExceptionPtrPtr);
Anders Carlssond3379292009-10-30 02:27:02 +0000279
Anders Carlsson8370c582009-12-11 00:32:37 +0000280
281 CopyObject(*this, E->getSubExpr(), ExceptionPtr, ExceptionPtrPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000282
Anders Carlssond3379292009-10-30 02:27:02 +0000283 // Now throw the exception.
284 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000285 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000286 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000287
Mike Stump0a3816e2009-12-04 01:51:45 +0000288 if (getInvokeDest()) {
289 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump8755ec32009-12-10 00:06:18 +0000290 llvm::InvokeInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000291 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
292 ExceptionPtr, TypeInfo, Dtor);
293 ThrowCall->setDoesNotReturn();
294 EmitBlock(Cont);
295 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000296 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000297 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
298 ThrowCall->setDoesNotReturn();
299 }
Anders Carlssond3379292009-10-30 02:27:02 +0000300 Builder.CreateUnreachable();
Mike Stump8755ec32009-12-10 00:06:18 +0000301
Anders Carlssond3379292009-10-30 02:27:02 +0000302 // Clear the insertion point to indicate we are in unreachable code.
303 Builder.ClearInsertionPoint();
Mike Stumpc2ab4862009-12-07 20:12:14 +0000304
305 // FIXME: For now, emit a dummy basic block because expr emitters in generally
306 // are not ready to handle emitting expressions at unreachable points.
307 EnsureInsertPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000308}
Mike Stump2bf701e2009-11-20 23:44:51 +0000309
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000310void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
311 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
312 if (FD == 0)
313 return;
314 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
315 if (Proto == 0)
316 return;
317
318 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
319
320 if (!Proto->hasExceptionSpec())
321 return;
322
323 llvm::Constant *Personality =
324 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
325 (VMContext),
326 true),
327 "__gxx_personality_v0");
328 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
329 llvm::Value *llvm_eh_exception =
330 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
331 llvm::Value *llvm_eh_selector =
332 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
333 const llvm::IntegerType *Int8Ty;
334 const llvm::PointerType *PtrToInt8Ty;
335 Int8Ty = llvm::Type::getInt8Ty(VMContext);
336 // C string type. Used in lots of places.
337 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
338 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
339 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
340
341 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
342 llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
343 llvm::BasicBlock *Match = createBasicBlock("match");
344 llvm::BasicBlock *Unwind = 0;
345
346 assert(PrevLandingPad == 0 && "EHSpec has invoke context");
Chandler Carruth60cfcec2009-12-13 01:37:04 +0000347 (void)PrevLandingPad;
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000348
349 llvm::BasicBlock *Cont = createBasicBlock("cont");
350
351 EmitBranchThroughCleanup(Cont);
352
353 // Emit the statements in the try {} block
354 setInvokeDest(EHSpecHandler);
355
356 EmitBlock(EHSpecHandler);
357 // Exception object
358 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
359 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
360
361 SelectorArgs.push_back(Exc);
362 SelectorArgs.push_back(Personality);
363 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
364 Proto->getNumExceptions()+1));
365
366 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
367 QualType Ty = Proto->getExceptionType(i);
Douglas Gregor154fe982009-12-23 22:04:40 +0000368 QualType ExceptType
369 = Ty.getNonReferenceType().getUnqualifiedType();
370 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000371 SelectorArgs.push_back(EHType);
372 }
373 if (Proto->getNumExceptions())
374 SelectorArgs.push_back(Null);
375
376 // Find which handler was matched.
377 llvm::Value *Selector
378 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
379 SelectorArgs.end(), "selector");
380 if (Proto->getNumExceptions()) {
381 Unwind = createBasicBlock("Unwind");
382
383 Builder.CreateStore(Exc, RethrowPtr);
384 Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
385 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
386 0)),
387 Match, Unwind);
388
389 EmitBlock(Match);
390 }
391 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
392 Builder.CreateUnreachable();
393
394 if (Proto->getNumExceptions()) {
395 EmitBlock(Unwind);
396 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
397 Builder.CreateLoad(RethrowPtr));
398 Builder.CreateUnreachable();
399 }
400
401 EmitBlock(Cont);
402}
403
404void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
405 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
406 if (FD == 0)
407 return;
408 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
409 if (Proto == 0)
410 return;
411
412 if (!Proto->hasExceptionSpec())
413 return;
414
415 setInvokeDest(0);
416}
417
Mike Stump2bf701e2009-11-20 23:44:51 +0000418void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
Mike Stump2bf701e2009-11-20 23:44:51 +0000419 // Pointer to the personality function
420 llvm::Constant *Personality =
421 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
422 (VMContext),
423 true),
424 "__gxx_personality_v0");
425 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump639787c2009-12-02 19:53:57 +0000426 llvm::Value *llvm_eh_exception =
427 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
428 llvm::Value *llvm_eh_selector =
429 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000430
Mike Stump2bf701e2009-11-20 23:44:51 +0000431 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
432 llvm::BasicBlock *TryHandler = createBasicBlock("try.handler");
Mike Stump2bf701e2009-11-20 23:44:51 +0000433 llvm::BasicBlock *FinallyBlock = createBasicBlock("finally");
Mike Stump0f590be2009-12-01 03:41:18 +0000434 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000435 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
436
437 // Push an EH context entry, used for handling rethrows.
438 PushCleanupBlock(FinallyBlock);
439
440 // Emit the statements in the try {} block
441 setInvokeDest(TryHandler);
442
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000443 // FIXME: We should not have to do this here. The AST should have the member
444 // initializers under the CXXTryStmt's TryBlock.
445 if (OuterTryBlock == &S) {
446 GlobalDecl GD = CurGD;
447 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
448
449 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
450 size_t OldCleanupStackSize = CleanupEntries.size();
451 EmitCtorPrologue(CD, CurGD.getCtorType());
452 EmitStmt(S.getTryBlock());
453
454 // If any of the member initializers are temporaries bound to references
455 // make sure to emit their destructors.
456 EmitCleanupBlocks(OldCleanupStackSize);
457 } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
458 llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue");
459 PushCleanupBlock(DtorEpilogue);
460
461 EmitStmt(S.getTryBlock());
Mike Stump8755ec32009-12-10 00:06:18 +0000462
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000463 CleanupBlockInfo Info = PopCleanupBlock();
464
465 assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!");
466 EmitBlock(DtorEpilogue);
467 EmitDtorEpilogue(DD, GD.getDtorType());
Mike Stump8755ec32009-12-10 00:06:18 +0000468
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000469 if (Info.SwitchBlock)
470 EmitBlock(Info.SwitchBlock);
471 if (Info.EndBlock)
472 EmitBlock(Info.EndBlock);
Mike Stump8755ec32009-12-10 00:06:18 +0000473 } else
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000474 EmitStmt(S.getTryBlock());
475 } else
476 EmitStmt(S.getTryBlock());
Mike Stump2bf701e2009-11-20 23:44:51 +0000477
478 // Jump to end if there is no exception
479 EmitBranchThroughCleanup(FinallyEnd);
480
Mike Stump9b39c512009-12-09 22:59:31 +0000481 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump639787c2009-12-02 19:53:57 +0000482
Mike Stump2bf701e2009-11-20 23:44:51 +0000483 // Emit the handlers
484 EmitBlock(TryHandler);
Mike Stump8755ec32009-12-10 00:06:18 +0000485
Mike Stump2bf701e2009-11-20 23:44:51 +0000486 const llvm::IntegerType *Int8Ty;
487 const llvm::PointerType *PtrToInt8Ty;
488 Int8Ty = llvm::Type::getInt8Ty(VMContext);
489 // C string type. Used in lots of places.
490 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000491 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
492 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000493 llvm::Value *llvm_eh_typeid_for =
494 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000495 // Exception object
Mike Stump9b39c512009-12-09 22:59:31 +0000496 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000497 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000498
Mike Stump9b39c512009-12-09 22:59:31 +0000499 llvm::SmallVector<llvm::Value*, 8> Args;
Mike Stump639787c2009-12-02 19:53:57 +0000500 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000501 SelectorArgs.push_back(Exc);
502 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000503
Mike Stump0f590be2009-12-01 03:41:18 +0000504 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000505 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
506 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000507 VarDecl *CatchParam = C->getExceptionDecl();
508 if (CatchParam) {
Douglas Gregor154fe982009-12-23 22:04:40 +0000509 // C++ [except.handle]p3 indicates that top-level cv-qualifiers
510 // are ignored.
511 QualType CaughtType = C->getCaughtType().getNonReferenceType();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000512 llvm::Value *EHTypeInfo
Douglas Gregor154fe982009-12-23 22:04:40 +0000513 = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType());
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000514 SelectorArgs.push_back(EHTypeInfo);
Mike Stump2bf701e2009-11-20 23:44:51 +0000515 } else {
516 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000517 SelectorArgs.push_back(Null);
518 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000519 }
520 }
521
Mike Stump0f590be2009-12-01 03:41:18 +0000522 // We use a cleanup unless there was already a catch all.
523 if (!HasCatchAll) {
524 SelectorArgs.push_back(Null);
525 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000526
Mike Stump0f590be2009-12-01 03:41:18 +0000527 // Find which handler was matched.
528 llvm::Value *Selector
529 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
530 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000531 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
532 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000533 VarDecl *CatchParam = C->getExceptionDecl();
534 Stmt *CatchBody = C->getHandlerBlock();
535
536 llvm::BasicBlock *Next = 0;
537
538 if (SelectorArgs[i+2] != Null) {
539 llvm::BasicBlock *Match = createBasicBlock("match");
540 Next = createBasicBlock("catch.next");
541 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
542 llvm::Value *Id
543 = Builder.CreateCall(llvm_eh_typeid_for,
544 Builder.CreateBitCast(SelectorArgs[i+2],
545 Int8PtrTy));
546 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
547 Match, Next);
548 EmitBlock(Match);
549 }
550
551 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
552 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
553
554 PushCleanupBlock(MatchEnd);
555 setInvokeDest(MatchHandler);
556
557 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
558
Mike Stumpf7f74672009-12-02 23:37:16 +0000559 {
560 CleanupScope CatchScope(*this);
561 // Bind the catch parameter if it exists.
562 if (CatchParam) {
563 QualType CatchType = CatchParam->getType().getNonReferenceType();
564 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000565 bool WasPointer = true;
566 if (!CatchType.getTypePtr()->isPointerType()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000567 if (!isa<ReferenceType>(CatchParam->getType()))
568 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000569 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000570 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000571 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000572 EmitLocalBlockVarDecl(*CatchParam);
Mike Stumpf668bd02009-12-03 03:40:14 +0000573 // FIXME: we need to do this sooner so that the EH region for the
574 // cleanup doesn't start until after the ctor completes, use a decl
575 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000576 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000577 WasPointer, ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stumpf7f74672009-12-02 23:37:16 +0000578 setInvokeDest(MatchHandler);
579 }
580
581 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000582 }
583
Mike Stump0f590be2009-12-01 03:41:18 +0000584 EmitBranchThroughCleanup(FinallyEnd);
585
586 EmitBlock(MatchHandler);
587
588 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
589 // We are required to emit this call to satisfy LLVM, even
590 // though we don't use the result.
Mike Stump639787c2009-12-02 19:53:57 +0000591 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000592 Args.push_back(Exc);
593 Args.push_back(Personality);
594 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
595 0));
596 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
597 Builder.CreateStore(Exc, RethrowPtr);
598 EmitBranchThroughCleanup(FinallyRethrow);
599
600 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
601
602 EmitBlock(MatchEnd);
603
Mike Stump99533832009-12-02 07:41:41 +0000604 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000605 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000606 Cont, TerminateHandler,
Mike Stump0f590be2009-12-01 03:41:18 +0000607 Args.begin(), Args.begin());
Mike Stump0f590be2009-12-01 03:41:18 +0000608 EmitBlock(Cont);
609 if (Info.SwitchBlock)
610 EmitBlock(Info.SwitchBlock);
611 if (Info.EndBlock)
612 EmitBlock(Info.EndBlock);
613
Mike Stump0f590be2009-12-01 03:41:18 +0000614 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000615 Builder.CreateStore(Exc, RethrowPtr);
616 EmitBranchThroughCleanup(FinallyRethrow);
617
618 if (Next)
619 EmitBlock(Next);
620 }
Mike Stumpa0867832009-12-04 19:03:47 +0000621 if (!HasCatchAll) {
622 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000623 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000624 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000625
626 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
627
628 setInvokeDest(PrevLandingPad);
629
630 EmitBlock(FinallyBlock);
631
Mike Stump0f590be2009-12-01 03:41:18 +0000632 if (Info.SwitchBlock)
633 EmitBlock(Info.SwitchBlock);
634 if (Info.EndBlock)
635 EmitBlock(Info.EndBlock);
636
Mike Stump2bf701e2009-11-20 23:44:51 +0000637 // Branch around the rethrow code.
638 EmitBranch(FinallyEnd);
639
640 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000641 // FIXME: Eventually we can chain the handlers together and just do a call
642 // here.
643 if (getInvokeDest()) {
644 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
645 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(*this), Cont,
646 getInvokeDest(),
647 Builder.CreateLoad(RethrowPtr));
648 EmitBlock(Cont);
649 } else
650 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
651 Builder.CreateLoad(RethrowPtr));
652
Mike Stump2bf701e2009-11-20 23:44:51 +0000653 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000654
655 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000656}
Mike Stumpd88ea562009-12-09 03:35:49 +0000657
658CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
659 llvm::BasicBlock *Cont1 = CGF.createBasicBlock("cont");
660 CGF.EmitBranch(Cont1);
661 CGF.setInvokeDest(PreviousInvokeDest);
662
663
664 CGF.EmitBlock(CleanupHandler);
665
666 llvm::Constant *Personality =
667 CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
668 (CGF.VMContext),
669 true),
670 "__gxx_personality_v0");
671 Personality = llvm::ConstantExpr::getBitCast(Personality, CGF.PtrToInt8Ty);
672 llvm::Value *llvm_eh_exception =
673 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
674 llvm::Value *llvm_eh_selector =
675 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
676
677 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
678 const llvm::IntegerType *Int8Ty;
679 const llvm::PointerType *PtrToInt8Ty;
680 Int8Ty = llvm::Type::getInt8Ty(CGF.VMContext);
681 // C string type. Used in lots of places.
682 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
683 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
684 llvm::SmallVector<llvm::Value*, 8> Args;
685 Args.clear();
686 Args.push_back(Exc);
687 Args.push_back(Personality);
688 Args.push_back(Null);
689 CGF.Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
690
691 CGF.EmitBlock(CleanupEntryBB);
692
693 CGF.EmitBlock(Cont1);
694
695 if (CGF.getInvokeDest()) {
696 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
697 CGF.Builder.CreateInvoke(getUnwindResumeOrRethrowFn(CGF), Cont,
698 CGF.getInvokeDest(), Exc);
699 CGF.EmitBlock(Cont);
700 } else
701 CGF.Builder.CreateCall(getUnwindResumeOrRethrowFn(CGF), Exc);
702
703 CGF.Builder.CreateUnreachable();
704
705 CGF.EmitBlock(Cont);
706 if (CGF.Exceptions)
707 CGF.setInvokeDest(CleanupHandler);
708}
Mike Stump9b39c512009-12-09 22:59:31 +0000709
710llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +0000711 if (TerminateHandler)
712 return TerminateHandler;
713
Mike Stump76958092009-12-09 23:31:35 +0000714 llvm::BasicBlock *Cont = 0;
715
716 if (HaveInsertPoint()) {
717 Cont = createBasicBlock("cont");
718 EmitBranch(Cont);
719 }
720
Mike Stump9b39c512009-12-09 22:59:31 +0000721 llvm::Constant *Personality =
722 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
723 (VMContext),
724 true),
725 "__gxx_personality_v0");
726 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
727 llvm::Value *llvm_eh_exception =
728 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
729 llvm::Value *llvm_eh_selector =
730 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
731
732 // Set up terminate handler
Mike Stump182f3832009-12-10 00:02:42 +0000733 TerminateHandler = createBasicBlock("terminate.handler");
Mike Stump9b39c512009-12-09 22:59:31 +0000734 EmitBlock(TerminateHandler);
735 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
736 // We are required to emit this call to satisfy LLVM, even
737 // though we don't use the result.
738 llvm::SmallVector<llvm::Value*, 8> Args;
739 Args.push_back(Exc);
740 Args.push_back(Personality);
741 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Mike Stump76958092009-12-09 23:31:35 +0000742 1));
Mike Stump9b39c512009-12-09 22:59:31 +0000743 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
Mike Stump8755ec32009-12-10 00:06:18 +0000744 llvm::CallInst *TerminateCall =
Mike Stump9b39c512009-12-09 22:59:31 +0000745 Builder.CreateCall(getTerminateFn(*this));
746 TerminateCall->setDoesNotReturn();
747 TerminateCall->setDoesNotThrow();
748 Builder.CreateUnreachable();
749
750 // Clear the insertion point to indicate we are in unreachable code.
751 Builder.ClearInsertionPoint();
752
Mike Stump76958092009-12-09 23:31:35 +0000753 if (Cont)
754 EmitBlock(Cont);
755
Mike Stump9b39c512009-12-09 22:59:31 +0000756 return TerminateHandler;
757}