blob: 30499157f14a5fb44dfa3a8aee8a3f855c9957fb [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();
Mike Stumpf2945c02009-12-17 06:08:47 +0000153 llvm::Value *CondPtr = 0;
Mike Stump5030a982009-12-10 01:52:30 +0000154 if (CGF.Exceptions) {
155 CodeGenFunction::EHCleanupBlock Cleanup(CGF);
Mike Stump99533832009-12-02 07:41:41 +0000156 llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
Anders Carlsson8370c582009-12-11 00:32:37 +0000157
Mike Stumpf2945c02009-12-17 06:08:47 +0000158 llvm::BasicBlock *CondBlock = CGF.createBasicBlock("cond.free");
159 llvm::BasicBlock *Cont = CGF.createBasicBlock("cont");
160 CondPtr = CGF.CreateTempAlloca(llvm::Type::getInt1Ty(CGF.getLLVMContext()),
161 "doEHfree");
162
163 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CondPtr),
164 CondBlock, Cont);
165 CGF.EmitBlock(CondBlock);
166
Anders Carlsson8370c582009-12-11 00:32:37 +0000167 // Load the exception pointer.
168 llvm::Value *ExceptionPtr = CGF.Builder.CreateLoad(ExceptionPtrPtr);
Mike Stump99533832009-12-02 07:41:41 +0000169 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
Mike Stumpf2945c02009-12-17 06:08:47 +0000170
171 CGF.EmitBlock(Cont);
Mike Stump99533832009-12-02 07:41:41 +0000172 }
Mike Stump0924a8a2009-12-11 00:02:10 +0000173
Mike Stumpf2945c02009-12-17 06:08:47 +0000174 if (CondPtr)
175 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(CGF.getLLVMContext()),
176 CondPtr);
177
Mike Stump0f590be2009-12-01 03:41:18 +0000178 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
Mike Stumpf2945c02009-12-17 06:08:47 +0000179
180 //CGF.setInvokeDest(PrevLandingPad);
181 if (CondPtr)
182 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(CGF.getLLVMContext()),
183 CondPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000184
Mike Stump76958092009-12-09 23:31:35 +0000185 llvm::BasicBlock *TerminateHandler = CGF.getTerminateHandler();
Mike Stump5030a982009-12-10 01:52:30 +0000186 PrevLandingPad = CGF.getInvokeDest();
Mike Stump76958092009-12-09 23:31:35 +0000187 CGF.setInvokeDest(TerminateHandler);
188
Mike Stump0f590be2009-12-01 03:41:18 +0000189 // Stolen from EmitClassAggrMemberwiseCopy
190 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
191 Ctor_Complete);
192 CallArgList CallArgs;
193 CallArgs.push_back(std::make_pair(RValue::get(This),
194 CopyCtor->getThisType(CGF.getContext())));
195
196 // Push the Src ptr.
197 CallArgs.push_back(std::make_pair(RValue::get(Src),
198 CopyCtor->getParamDecl(0)->getType()));
199 QualType ResultType =
200 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
201 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
202 Callee, CallArgs, CopyCtor);
Mike Stump76958092009-12-09 23:31:35 +0000203 CGF.setInvokeDest(PrevLandingPad);
Mike Stump0f590be2009-12-01 03:41:18 +0000204 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000205 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000206 }
207}
208
209// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
210// N is casted to the right type.
211static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000212 bool WasPointer, llvm::Value *E, llvm::Value *N) {
Mike Stump0f590be2009-12-01 03:41:18 +0000213 // Store the throw exception in the exception object.
Mike Stumpb2debeb2009-12-08 01:29:31 +0000214 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000215 llvm::Value *Value = E;
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000216 if (!WasPointer)
217 Value = CGF.Builder.CreateLoad(Value);
Mike Stump0f590be2009-12-01 03:41:18 +0000218 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump0f590be2009-12-01 03:41:18 +0000219 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
220 } else {
221 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
222 const CXXRecordDecl *RD;
223 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
224 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
225 if (RD->hasTrivialCopyConstructor()) {
226 CGF.EmitAggregateCopy(This, E, ObjectType);
227 } else if (CXXConstructorDecl *CopyCtor
228 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump0f590be2009-12-01 03:41:18 +0000229 llvm::Value *Src = E;
230
231 // Stolen from EmitClassAggrMemberwiseCopy
232 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
233 Ctor_Complete);
234 CallArgList CallArgs;
235 CallArgs.push_back(std::make_pair(RValue::get(This),
236 CopyCtor->getThisType(CGF.getContext())));
237
238 // Push the Src ptr.
239 CallArgs.push_back(std::make_pair(RValue::get(Src),
240 CopyCtor->getParamDecl(0)->getType()));
241 QualType ResultType =
242 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
243 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
244 Callee, CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000245 } else
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +0000246 llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000247 }
248}
249
Anders Carlsson756b5c42009-10-30 01:42:31 +0000250void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000251 if (!E->getSubExpr()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000252 if (getInvokeDest()) {
253 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
254 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
255 ->setDoesNotReturn();
256 EmitBlock(Cont);
257 } else
258 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
Mike Stumpb4eea692009-11-20 00:56:31 +0000259 Builder.CreateUnreachable();
260
261 // Clear the insertion point to indicate we are in unreachable code.
262 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000263 return;
264 }
Mike Stump8755ec32009-12-10 00:06:18 +0000265
Anders Carlssond3379292009-10-30 02:27:02 +0000266 QualType ThrowType = E->getSubExpr()->getType();
Mike Stump8755ec32009-12-10 00:06:18 +0000267
Anders Carlssond3379292009-10-30 02:27:02 +0000268 // Now allocate the exception object.
269 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
270 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
Mike Stump8755ec32009-12-10 00:06:18 +0000271
Anders Carlssond3379292009-10-30 02:27:02 +0000272 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
Mike Stump8755ec32009-12-10 00:06:18 +0000273 llvm::Value *ExceptionPtr =
274 Builder.CreateCall(AllocExceptionFn,
Anders Carlssond3379292009-10-30 02:27:02 +0000275 llvm::ConstantInt::get(SizeTy, TypeSize),
276 "exception");
Anders Carlsson8370c582009-12-11 00:32:37 +0000277
278 llvm::Value *ExceptionPtrPtr =
279 CreateTempAlloca(ExceptionPtr->getType(), "exception.ptr");
280 Builder.CreateStore(ExceptionPtr, ExceptionPtrPtr);
Anders Carlssond3379292009-10-30 02:27:02 +0000281
Anders Carlsson8370c582009-12-11 00:32:37 +0000282
283 CopyObject(*this, E->getSubExpr(), ExceptionPtr, ExceptionPtrPtr);
Mike Stump8755ec32009-12-10 00:06:18 +0000284
Anders Carlssond3379292009-10-30 02:27:02 +0000285 // Now throw the exception.
286 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000287 llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000288 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
Mike Stump8755ec32009-12-10 00:06:18 +0000289
Mike Stump0a3816e2009-12-04 01:51:45 +0000290 if (getInvokeDest()) {
291 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump8755ec32009-12-10 00:06:18 +0000292 llvm::InvokeInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000293 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
294 ExceptionPtr, TypeInfo, Dtor);
295 ThrowCall->setDoesNotReturn();
296 EmitBlock(Cont);
297 } else {
Mike Stump8755ec32009-12-10 00:06:18 +0000298 llvm::CallInst *ThrowCall =
Mike Stump0a3816e2009-12-04 01:51:45 +0000299 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
300 ThrowCall->setDoesNotReturn();
301 }
Anders Carlssond3379292009-10-30 02:27:02 +0000302 Builder.CreateUnreachable();
Mike Stump8755ec32009-12-10 00:06:18 +0000303
Anders Carlssond3379292009-10-30 02:27:02 +0000304 // Clear the insertion point to indicate we are in unreachable code.
305 Builder.ClearInsertionPoint();
Mike Stumpc2ab4862009-12-07 20:12:14 +0000306
307 // FIXME: For now, emit a dummy basic block because expr emitters in generally
308 // are not ready to handle emitting expressions at unreachable points.
309 EnsureInsertPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000310}
Mike Stump2bf701e2009-11-20 23:44:51 +0000311
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000312void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
313 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
314 if (FD == 0)
315 return;
316 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
317 if (Proto == 0)
318 return;
319
320 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
321
322 if (!Proto->hasExceptionSpec())
323 return;
324
325 llvm::Constant *Personality =
326 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
327 (VMContext),
328 true),
329 "__gxx_personality_v0");
330 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
331 llvm::Value *llvm_eh_exception =
332 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
333 llvm::Value *llvm_eh_selector =
334 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
335 const llvm::IntegerType *Int8Ty;
336 const llvm::PointerType *PtrToInt8Ty;
337 Int8Ty = llvm::Type::getInt8Ty(VMContext);
338 // C string type. Used in lots of places.
339 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
340 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
341 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
342
343 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
344 llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
345 llvm::BasicBlock *Match = createBasicBlock("match");
346 llvm::BasicBlock *Unwind = 0;
347
348 assert(PrevLandingPad == 0 && "EHSpec has invoke context");
Chandler Carruth60cfcec2009-12-13 01:37:04 +0000349 (void)PrevLandingPad;
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000350
351 llvm::BasicBlock *Cont = createBasicBlock("cont");
352
353 EmitBranchThroughCleanup(Cont);
354
355 // Emit the statements in the try {} block
356 setInvokeDest(EHSpecHandler);
357
358 EmitBlock(EHSpecHandler);
359 // Exception object
360 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
361 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
362
363 SelectorArgs.push_back(Exc);
364 SelectorArgs.push_back(Personality);
365 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
366 Proto->getNumExceptions()+1));
367
368 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
369 QualType Ty = Proto->getExceptionType(i);
Douglas Gregor154fe982009-12-23 22:04:40 +0000370 QualType ExceptType
371 = Ty.getNonReferenceType().getUnqualifiedType();
372 llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType);
Mike Stumpcce3d4f2009-12-07 23:38:24 +0000373 SelectorArgs.push_back(EHType);
374 }
375 if (Proto->getNumExceptions())
376 SelectorArgs.push_back(Null);
377
378 // Find which handler was matched.
379 llvm::Value *Selector
380 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
381 SelectorArgs.end(), "selector");
382 if (Proto->getNumExceptions()) {
383 Unwind = createBasicBlock("Unwind");
384
385 Builder.CreateStore(Exc, RethrowPtr);
386 Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
387 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
388 0)),
389 Match, Unwind);
390
391 EmitBlock(Match);
392 }
393 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
394 Builder.CreateUnreachable();
395
396 if (Proto->getNumExceptions()) {
397 EmitBlock(Unwind);
398 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
399 Builder.CreateLoad(RethrowPtr));
400 Builder.CreateUnreachable();
401 }
402
403 EmitBlock(Cont);
404}
405
406void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
407 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
408 if (FD == 0)
409 return;
410 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
411 if (Proto == 0)
412 return;
413
414 if (!Proto->hasExceptionSpec())
415 return;
416
417 setInvokeDest(0);
418}
419
Mike Stump2bf701e2009-11-20 23:44:51 +0000420void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
Mike Stump2bf701e2009-11-20 23:44:51 +0000421 // Pointer to the personality function
422 llvm::Constant *Personality =
423 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
424 (VMContext),
425 true),
426 "__gxx_personality_v0");
427 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump639787c2009-12-02 19:53:57 +0000428 llvm::Value *llvm_eh_exception =
429 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
430 llvm::Value *llvm_eh_selector =
431 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump2bf701e2009-11-20 23:44:51 +0000432
Mike Stump2bf701e2009-11-20 23:44:51 +0000433 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
434 llvm::BasicBlock *TryHandler = createBasicBlock("try.handler");
Mike Stump2bf701e2009-11-20 23:44:51 +0000435 llvm::BasicBlock *FinallyBlock = createBasicBlock("finally");
Mike Stump0f590be2009-12-01 03:41:18 +0000436 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000437 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
438
439 // Push an EH context entry, used for handling rethrows.
440 PushCleanupBlock(FinallyBlock);
441
442 // Emit the statements in the try {} block
443 setInvokeDest(TryHandler);
444
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000445 // FIXME: We should not have to do this here. The AST should have the member
446 // initializers under the CXXTryStmt's TryBlock.
447 if (OuterTryBlock == &S) {
448 GlobalDecl GD = CurGD;
449 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
450
451 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
452 size_t OldCleanupStackSize = CleanupEntries.size();
453 EmitCtorPrologue(CD, CurGD.getCtorType());
454 EmitStmt(S.getTryBlock());
455
456 // If any of the member initializers are temporaries bound to references
457 // make sure to emit their destructors.
458 EmitCleanupBlocks(OldCleanupStackSize);
459 } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
460 llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue");
461 PushCleanupBlock(DtorEpilogue);
462
463 EmitStmt(S.getTryBlock());
Mike Stump8755ec32009-12-10 00:06:18 +0000464
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000465 CleanupBlockInfo Info = PopCleanupBlock();
466
467 assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!");
468 EmitBlock(DtorEpilogue);
469 EmitDtorEpilogue(DD, GD.getDtorType());
Mike Stump8755ec32009-12-10 00:06:18 +0000470
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000471 if (Info.SwitchBlock)
472 EmitBlock(Info.SwitchBlock);
473 if (Info.EndBlock)
474 EmitBlock(Info.EndBlock);
Mike Stump8755ec32009-12-10 00:06:18 +0000475 } else
Mike Stump6a1e0eb2009-12-04 23:26:17 +0000476 EmitStmt(S.getTryBlock());
477 } else
478 EmitStmt(S.getTryBlock());
Mike Stump2bf701e2009-11-20 23:44:51 +0000479
480 // Jump to end if there is no exception
481 EmitBranchThroughCleanup(FinallyEnd);
482
Mike Stump9b39c512009-12-09 22:59:31 +0000483 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump639787c2009-12-02 19:53:57 +0000484
Mike Stump2bf701e2009-11-20 23:44:51 +0000485 // Emit the handlers
486 EmitBlock(TryHandler);
Mike Stump8755ec32009-12-10 00:06:18 +0000487
Mike Stump2bf701e2009-11-20 23:44:51 +0000488 const llvm::IntegerType *Int8Ty;
489 const llvm::PointerType *PtrToInt8Ty;
490 Int8Ty = llvm::Type::getInt8Ty(VMContext);
491 // C string type. Used in lots of places.
492 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000493 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
494 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump0f590be2009-12-01 03:41:18 +0000495 llvm::Value *llvm_eh_typeid_for =
496 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000497 // Exception object
Mike Stump9b39c512009-12-09 22:59:31 +0000498 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000499 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000500
Mike Stump9b39c512009-12-09 22:59:31 +0000501 llvm::SmallVector<llvm::Value*, 8> Args;
Mike Stump639787c2009-12-02 19:53:57 +0000502 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000503 SelectorArgs.push_back(Exc);
504 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000505
Mike Stump0f590be2009-12-01 03:41:18 +0000506 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000507 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
508 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000509 VarDecl *CatchParam = C->getExceptionDecl();
510 if (CatchParam) {
Douglas Gregor154fe982009-12-23 22:04:40 +0000511 // C++ [except.handle]p3 indicates that top-level cv-qualifiers
512 // are ignored.
513 QualType CaughtType = C->getCaughtType().getNonReferenceType();
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000514 llvm::Value *EHTypeInfo
Douglas Gregor154fe982009-12-23 22:04:40 +0000515 = CGM.GetAddrOfRTTIDescriptor(CaughtType.getUnqualifiedType());
Anders Carlsson1d7088d2009-12-17 07:09:17 +0000516 SelectorArgs.push_back(EHTypeInfo);
Mike Stump2bf701e2009-11-20 23:44:51 +0000517 } else {
518 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000519 SelectorArgs.push_back(Null);
520 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000521 }
522 }
523
Mike Stump0f590be2009-12-01 03:41:18 +0000524 // We use a cleanup unless there was already a catch all.
525 if (!HasCatchAll) {
526 SelectorArgs.push_back(Null);
527 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000528
Mike Stump0f590be2009-12-01 03:41:18 +0000529 // Find which handler was matched.
530 llvm::Value *Selector
531 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
532 SelectorArgs.end(), "selector");
Mike Stump2bf701e2009-11-20 23:44:51 +0000533 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
534 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000535 VarDecl *CatchParam = C->getExceptionDecl();
536 Stmt *CatchBody = C->getHandlerBlock();
537
538 llvm::BasicBlock *Next = 0;
539
540 if (SelectorArgs[i+2] != Null) {
541 llvm::BasicBlock *Match = createBasicBlock("match");
542 Next = createBasicBlock("catch.next");
543 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
544 llvm::Value *Id
545 = Builder.CreateCall(llvm_eh_typeid_for,
546 Builder.CreateBitCast(SelectorArgs[i+2],
547 Int8PtrTy));
548 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
549 Match, Next);
550 EmitBlock(Match);
551 }
552
553 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
554 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
555
556 PushCleanupBlock(MatchEnd);
557 setInvokeDest(MatchHandler);
558
559 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
560
Mike Stumpf7f74672009-12-02 23:37:16 +0000561 {
562 CleanupScope CatchScope(*this);
563 // Bind the catch parameter if it exists.
564 if (CatchParam) {
565 QualType CatchType = CatchParam->getType().getNonReferenceType();
566 setInvokeDest(TerminateHandler);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000567 bool WasPointer = true;
568 if (!CatchType.getTypePtr()->isPointerType()) {
Mike Stump0a3816e2009-12-04 01:51:45 +0000569 if (!isa<ReferenceType>(CatchParam->getType()))
570 WasPointer = false;
Mike Stumpf7f74672009-12-02 23:37:16 +0000571 CatchType = getContext().getPointerType(CatchType);
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000572 }
Mike Stumpf7f74672009-12-02 23:37:16 +0000573 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stumpf7f74672009-12-02 23:37:16 +0000574 EmitLocalBlockVarDecl(*CatchParam);
Mike Stumpf668bd02009-12-03 03:40:14 +0000575 // FIXME: we need to do this sooner so that the EH region for the
576 // cleanup doesn't start until after the ctor completes, use a decl
577 // init?
Mike Stumpf7f74672009-12-02 23:37:16 +0000578 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stumpd9cb7e92009-12-03 22:38:15 +0000579 WasPointer, ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stumpf7f74672009-12-02 23:37:16 +0000580 setInvokeDest(MatchHandler);
581 }
582
583 EmitStmt(CatchBody);
Mike Stump0f590be2009-12-01 03:41:18 +0000584 }
585
Mike Stump0f590be2009-12-01 03:41:18 +0000586 EmitBranchThroughCleanup(FinallyEnd);
587
588 EmitBlock(MatchHandler);
589
590 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
591 // We are required to emit this call to satisfy LLVM, even
592 // though we don't use the result.
Mike Stump639787c2009-12-02 19:53:57 +0000593 Args.clear();
Mike Stump0f590be2009-12-01 03:41:18 +0000594 Args.push_back(Exc);
595 Args.push_back(Personality);
596 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
597 0));
598 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
599 Builder.CreateStore(Exc, RethrowPtr);
600 EmitBranchThroughCleanup(FinallyRethrow);
601
602 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
603
604 EmitBlock(MatchEnd);
605
Mike Stump99533832009-12-02 07:41:41 +0000606 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000607 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000608 Cont, TerminateHandler,
Mike Stump0f590be2009-12-01 03:41:18 +0000609 Args.begin(), Args.begin());
Mike Stump0f590be2009-12-01 03:41:18 +0000610 EmitBlock(Cont);
611 if (Info.SwitchBlock)
612 EmitBlock(Info.SwitchBlock);
613 if (Info.EndBlock)
614 EmitBlock(Info.EndBlock);
615
Mike Stump0f590be2009-12-01 03:41:18 +0000616 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000617 Builder.CreateStore(Exc, RethrowPtr);
618 EmitBranchThroughCleanup(FinallyRethrow);
619
620 if (Next)
621 EmitBlock(Next);
622 }
Mike Stumpa0867832009-12-04 19:03:47 +0000623 if (!HasCatchAll) {
624 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump0f590be2009-12-01 03:41:18 +0000625 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stumpa0867832009-12-04 19:03:47 +0000626 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000627
628 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
629
630 setInvokeDest(PrevLandingPad);
631
632 EmitBlock(FinallyBlock);
633
Mike Stump0f590be2009-12-01 03:41:18 +0000634 if (Info.SwitchBlock)
635 EmitBlock(Info.SwitchBlock);
636 if (Info.EndBlock)
637 EmitBlock(Info.EndBlock);
638
Mike Stump2bf701e2009-11-20 23:44:51 +0000639 // Branch around the rethrow code.
640 EmitBranch(FinallyEnd);
641
642 EmitBlock(FinallyRethrow);
Mike Stumpb2c9c0b2009-12-04 19:21:57 +0000643 // FIXME: Eventually we can chain the handlers together and just do a call
644 // here.
645 if (getInvokeDest()) {
646 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
647 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(*this), Cont,
648 getInvokeDest(),
649 Builder.CreateLoad(RethrowPtr));
650 EmitBlock(Cont);
651 } else
652 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
653 Builder.CreateLoad(RethrowPtr));
654
Mike Stump2bf701e2009-11-20 23:44:51 +0000655 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000656
657 EmitBlock(FinallyEnd);
Mike Stump2bf701e2009-11-20 23:44:51 +0000658}
Mike Stumpd88ea562009-12-09 03:35:49 +0000659
660CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
661 llvm::BasicBlock *Cont1 = CGF.createBasicBlock("cont");
662 CGF.EmitBranch(Cont1);
663 CGF.setInvokeDest(PreviousInvokeDest);
664
665
666 CGF.EmitBlock(CleanupHandler);
667
668 llvm::Constant *Personality =
669 CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
670 (CGF.VMContext),
671 true),
672 "__gxx_personality_v0");
673 Personality = llvm::ConstantExpr::getBitCast(Personality, CGF.PtrToInt8Ty);
674 llvm::Value *llvm_eh_exception =
675 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
676 llvm::Value *llvm_eh_selector =
677 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
678
679 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
680 const llvm::IntegerType *Int8Ty;
681 const llvm::PointerType *PtrToInt8Ty;
682 Int8Ty = llvm::Type::getInt8Ty(CGF.VMContext);
683 // C string type. Used in lots of places.
684 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
685 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
686 llvm::SmallVector<llvm::Value*, 8> Args;
687 Args.clear();
688 Args.push_back(Exc);
689 Args.push_back(Personality);
690 Args.push_back(Null);
691 CGF.Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
692
693 CGF.EmitBlock(CleanupEntryBB);
694
695 CGF.EmitBlock(Cont1);
696
697 if (CGF.getInvokeDest()) {
698 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
699 CGF.Builder.CreateInvoke(getUnwindResumeOrRethrowFn(CGF), Cont,
700 CGF.getInvokeDest(), Exc);
701 CGF.EmitBlock(Cont);
702 } else
703 CGF.Builder.CreateCall(getUnwindResumeOrRethrowFn(CGF), Exc);
704
705 CGF.Builder.CreateUnreachable();
706
707 CGF.EmitBlock(Cont);
708 if (CGF.Exceptions)
709 CGF.setInvokeDest(CleanupHandler);
710}
Mike Stump9b39c512009-12-09 22:59:31 +0000711
712llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump182f3832009-12-10 00:02:42 +0000713 if (TerminateHandler)
714 return TerminateHandler;
715
Mike Stump76958092009-12-09 23:31:35 +0000716 llvm::BasicBlock *Cont = 0;
717
718 if (HaveInsertPoint()) {
719 Cont = createBasicBlock("cont");
720 EmitBranch(Cont);
721 }
722
Mike Stump9b39c512009-12-09 22:59:31 +0000723 llvm::Constant *Personality =
724 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
725 (VMContext),
726 true),
727 "__gxx_personality_v0");
728 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
729 llvm::Value *llvm_eh_exception =
730 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
731 llvm::Value *llvm_eh_selector =
732 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
733
734 // Set up terminate handler
Mike Stump182f3832009-12-10 00:02:42 +0000735 TerminateHandler = createBasicBlock("terminate.handler");
Mike Stump9b39c512009-12-09 22:59:31 +0000736 EmitBlock(TerminateHandler);
737 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
738 // We are required to emit this call to satisfy LLVM, even
739 // though we don't use the result.
740 llvm::SmallVector<llvm::Value*, 8> Args;
741 Args.push_back(Exc);
742 Args.push_back(Personality);
743 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
Mike Stump76958092009-12-09 23:31:35 +0000744 1));
Mike Stump9b39c512009-12-09 22:59:31 +0000745 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
Mike Stump8755ec32009-12-10 00:06:18 +0000746 llvm::CallInst *TerminateCall =
Mike Stump9b39c512009-12-09 22:59:31 +0000747 Builder.CreateCall(getTerminateFn(*this));
748 TerminateCall->setDoesNotReturn();
749 TerminateCall->setDoesNotThrow();
750 Builder.CreateUnreachable();
751
752 // Clear the insertion point to indicate we are in unreachable code.
753 Builder.ClearInsertionPoint();
754
Mike Stump76958092009-12-09 23:31:35 +0000755 if (Cont)
756 EmitBlock(Cont);
757
Mike Stump9b39c512009-12-09 22:59:31 +0000758 return TerminateHandler;
759}