blob: 5e26e9bbea52037e3cf0785678ecd143e5a55552 [file] [log] [blame]
Anders Carlsson4b08db72009-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 Stump58ef18b2009-11-20 23:44:51 +000014#include "clang/AST/StmtCXX.h"
15
16#include "llvm/Intrinsics.h"
17
Anders Carlsson4b08db72009-10-30 01:42:31 +000018#include "CodeGenFunction.h"
19using namespace clang;
20using namespace CodeGen;
21
Anders Carlsson32e1b1c2009-10-30 02:27:02 +000022static llvm::Constant *getAllocateExceptionFn(CodeGenFunction &CGF) {
23 // void *__cxa_allocate_exception(size_t thrown_size);
24 const llvm::Type *SizeTy = CGF.ConvertType(CGF.getContext().getSizeType());
25 std::vector<const llvm::Type*> Args(1, SizeTy);
26
27 const llvm::FunctionType *FTy =
28 llvm::FunctionType::get(llvm::Type::getInt8PtrTy(CGF.getLLVMContext()),
29 Args, false);
30
31 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
32}
33
Mike Stump33270212009-12-02 07:41:41 +000034static llvm::Constant *getFreeExceptionFn(CodeGenFunction &CGF) {
35 // void __cxa_free_exception(void *thrown_exception);
36 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
37 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
38
39 const llvm::FunctionType *FTy =
40 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
41 Args, false);
42
43 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
44}
45
Anders Carlsson32e1b1c2009-10-30 02:27:02 +000046static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
Mike Stump33270212009-12-02 07:41:41 +000047 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
48 // void (*dest) (void *));
Anders Carlsson32e1b1c2009-10-30 02:27:02 +000049
50 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
51 std::vector<const llvm::Type*> Args(3, Int8PtrTy);
52
53 const llvm::FunctionType *FTy =
Mike Stumpd1782cc2009-11-20 00:56:31 +000054 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
55 Args, false);
Anders Carlsson32e1b1c2009-10-30 02:27:02 +000056
57 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
58}
59
Mike Stumpd1782cc2009-11-20 00:56:31 +000060static llvm::Constant *getReThrowFn(CodeGenFunction &CGF) {
Mike Stump33270212009-12-02 07:41:41 +000061 // void __cxa_rethrow();
Mike Stumpd1782cc2009-11-20 00:56:31 +000062
63 const llvm::FunctionType *FTy =
64 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
65
66 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_rethrow");
67}
68
Mike Stump58ef18b2009-11-20 23:44:51 +000069static llvm::Constant *getBeginCatchFn(CodeGenFunction &CGF) {
Mike Stump33270212009-12-02 07:41:41 +000070 // void* __cxa_begin_catch();
Mike Stump58ef18b2009-11-20 23:44:51 +000071
72 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
73 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
74
75 const llvm::FunctionType *FTy =
Mike Stump54066142009-12-01 03:41:18 +000076 llvm::FunctionType::get(Int8PtrTy, Args, false);
Mike Stump58ef18b2009-11-20 23:44:51 +000077
78 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
79}
80
81static llvm::Constant *getEndCatchFn(CodeGenFunction &CGF) {
Mike Stump33270212009-12-02 07:41:41 +000082 // void __cxa_end_catch();
Mike Stump58ef18b2009-11-20 23:44:51 +000083
84 const llvm::FunctionType *FTy =
85 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
86
87 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
88}
89
Mike Stump1d849212009-12-07 23:38:24 +000090static llvm::Constant *getUnexpectedFn(CodeGenFunction &CGF) {
91 // void __cxa_call_unexepcted(void *thrown_exception);
92
93 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
94 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
95
96 const llvm::FunctionType *FTy =
97 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
98 Args, false);
99
100 return CGF.CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
101}
102
Mike Stump54066142009-12-01 03:41:18 +0000103// FIXME: Eventually this will all go into the backend. Set from the target for
104// now.
105static int using_sjlj_exceptions = 0;
106
107static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) {
108 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
109 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
110
111 const llvm::FunctionType *FTy =
112 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args,
113 false);
114
115 if (using_sjlj_exceptions)
116 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
117 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
118}
119
Mike Stump33270212009-12-02 07:41:41 +0000120static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
121 // void __terminate();
122
123 const llvm::FunctionType *FTy =
124 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
125
126 return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev");
127}
128
Mike Stump54066142009-12-01 03:41:18 +0000129// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
130// N is casted to the right type.
131static void CopyObject(CodeGenFunction &CGF, const Expr *E, llvm::Value *N) {
132 QualType ObjectType = E->getType();
133
134 // Store the throw exception in the exception object.
135 if (!CGF.hasAggregateLLVMType(ObjectType)) {
136 llvm::Value *Value = CGF.EmitScalarExpr(E);
137 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
138
139 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
140 } else {
141 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
142 const CXXRecordDecl *RD;
143 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
144 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
145 if (RD->hasTrivialCopyConstructor()) {
146 CGF.EmitAggExpr(E, This, false);
147 } else if (CXXConstructorDecl *CopyCtor
148 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump33270212009-12-02 07:41:41 +0000149 // All temporaries end before we call __cxa_throw
Mike Stump2ab55762009-12-04 03:55:53 +0000150 // FIXME: Doesn't work well with eh31.C and PopCXXTemporary
151 // CodeGenFunction::CleanupScope TryScope(CGF);
Mike Stump33270212009-12-02 07:41:41 +0000152 {
153 // These actions are only on the exceptional edge.
Mike Stump5c820752009-12-04 19:03:47 +0000154 if (0) {
Mike Stump2ab55762009-12-04 03:55:53 +0000155 // FIXME: Doesn't work well with eh31.C and PopCXXTemporary
Mike Stump33270212009-12-02 07:41:41 +0000156 CodeGenFunction::DelayedCleanupBlock Scope(CGF, true);
157
158 llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
159 const llvm::Type *Int8PtrTy
160 = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
161 llvm::Value *ExceptionPtr = CGF.Builder.CreateBitCast(N, Int8PtrTy);
162 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
Mike Stump5c820752009-12-04 19:03:47 +0000163 }
Mike Stump33270212009-12-02 07:41:41 +0000164 }
165
Mike Stump54066142009-12-01 03:41:18 +0000166 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
167
Mike Stump25b20fc2009-12-09 23:31:35 +0000168 llvm::BasicBlock *TerminateHandler = CGF.getTerminateHandler();
169 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
170 CGF.setInvokeDest(TerminateHandler);
171
Mike Stump54066142009-12-01 03:41:18 +0000172 // Stolen from EmitClassAggrMemberwiseCopy
173 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
174 Ctor_Complete);
175 CallArgList CallArgs;
176 CallArgs.push_back(std::make_pair(RValue::get(This),
177 CopyCtor->getThisType(CGF.getContext())));
178
179 // Push the Src ptr.
180 CallArgs.push_back(std::make_pair(RValue::get(Src),
181 CopyCtor->getParamDecl(0)->getType()));
182 QualType ResultType =
183 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
184 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
185 Callee, CallArgs, CopyCtor);
Mike Stump25b20fc2009-12-09 23:31:35 +0000186 CGF.setInvokeDest(PrevLandingPad);
Mike Stump54066142009-12-01 03:41:18 +0000187 } else
Mike Stump33270212009-12-02 07:41:41 +0000188 llvm::llvm_unreachable("uncopyable object");
Mike Stump54066142009-12-01 03:41:18 +0000189 }
190}
191
192// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
193// N is casted to the right type.
194static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
Mike Stump7398ff02009-12-03 22:38:15 +0000195 bool WasPointer, llvm::Value *E, llvm::Value *N) {
Mike Stump54066142009-12-01 03:41:18 +0000196 // Store the throw exception in the exception object.
Mike Stump02c23d62009-12-08 01:29:31 +0000197 if (WasPointer || !CGF.hasAggregateLLVMType(ObjectType)) {
Mike Stump54066142009-12-01 03:41:18 +0000198 llvm::Value *Value = E;
Mike Stump7398ff02009-12-03 22:38:15 +0000199 if (!WasPointer)
200 Value = CGF.Builder.CreateLoad(Value);
Mike Stump54066142009-12-01 03:41:18 +0000201 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
Mike Stump54066142009-12-01 03:41:18 +0000202 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
203 } else {
204 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
205 const CXXRecordDecl *RD;
206 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
207 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
208 if (RD->hasTrivialCopyConstructor()) {
209 CGF.EmitAggregateCopy(This, E, ObjectType);
210 } else if (CXXConstructorDecl *CopyCtor
211 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump54066142009-12-01 03:41:18 +0000212 llvm::Value *Src = E;
213
214 // Stolen from EmitClassAggrMemberwiseCopy
215 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
216 Ctor_Complete);
217 CallArgList CallArgs;
218 CallArgs.push_back(std::make_pair(RValue::get(This),
219 CopyCtor->getThisType(CGF.getContext())));
220
221 // Push the Src ptr.
222 CallArgs.push_back(std::make_pair(RValue::get(Src),
223 CopyCtor->getParamDecl(0)->getType()));
224 QualType ResultType =
225 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
226 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
227 Callee, CallArgs, CopyCtor);
Mike Stump54066142009-12-01 03:41:18 +0000228 } else
229 llvm::llvm_unreachable("uncopyable object");
230 }
231}
232
Anders Carlsson4b08db72009-10-30 01:42:31 +0000233void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000234 if (!E->getSubExpr()) {
Mike Stump114ab9f2009-12-04 01:51:45 +0000235 if (getInvokeDest()) {
236 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
237 Builder.CreateInvoke(getReThrowFn(*this), Cont, getInvokeDest())
238 ->setDoesNotReturn();
239 EmitBlock(Cont);
240 } else
241 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
Mike Stumpd1782cc2009-11-20 00:56:31 +0000242 Builder.CreateUnreachable();
243
244 // Clear the insertion point to indicate we are in unreachable code.
245 Builder.ClearInsertionPoint();
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000246 return;
247 }
248
249 QualType ThrowType = E->getSubExpr()->getType();
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000250
251 // Now allocate the exception object.
252 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
253 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
254
255 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
256 llvm::Value *ExceptionPtr =
257 Builder.CreateCall(AllocExceptionFn,
258 llvm::ConstantInt::get(SizeTy, TypeSize),
259 "exception");
260
Mike Stump54066142009-12-01 03:41:18 +0000261 CopyObject(*this, E->getSubExpr(), ExceptionPtr);
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000262
263 // Now throw the exception.
264 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stumpc01c2b82009-12-02 18:57:08 +0000265 llvm::Constant *TypeInfo = CGM.GenerateRTTI(ThrowType);
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000266 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
267
Mike Stump114ab9f2009-12-04 01:51:45 +0000268 if (getInvokeDest()) {
269 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
270 llvm::InvokeInst *ThrowCall =
271 Builder.CreateInvoke3(getThrowFn(*this), Cont, getInvokeDest(),
272 ExceptionPtr, TypeInfo, Dtor);
273 ThrowCall->setDoesNotReturn();
274 EmitBlock(Cont);
275 } else {
276 llvm::CallInst *ThrowCall =
277 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
278 ThrowCall->setDoesNotReturn();
279 }
Anders Carlsson32e1b1c2009-10-30 02:27:02 +0000280 Builder.CreateUnreachable();
281
282 // Clear the insertion point to indicate we are in unreachable code.
283 Builder.ClearInsertionPoint();
Mike Stump62afe992009-12-07 20:12:14 +0000284
285 // FIXME: For now, emit a dummy basic block because expr emitters in generally
286 // are not ready to handle emitting expressions at unreachable points.
287 EnsureInsertPoint();
Anders Carlsson4b08db72009-10-30 01:42:31 +0000288}
Mike Stump58ef18b2009-11-20 23:44:51 +0000289
Mike Stump1d849212009-12-07 23:38:24 +0000290void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
291 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
292 if (FD == 0)
293 return;
294 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
295 if (Proto == 0)
296 return;
297
298 assert(!Proto->hasAnyExceptionSpec() && "function with parameter pack");
299
300 if (!Proto->hasExceptionSpec())
301 return;
302
303 llvm::Constant *Personality =
304 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
305 (VMContext),
306 true),
307 "__gxx_personality_v0");
308 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
309 llvm::Value *llvm_eh_exception =
310 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
311 llvm::Value *llvm_eh_selector =
312 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
313 const llvm::IntegerType *Int8Ty;
314 const llvm::PointerType *PtrToInt8Ty;
315 Int8Ty = llvm::Type::getInt8Ty(VMContext);
316 // C string type. Used in lots of places.
317 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
318 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
319 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
320
321 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
322 llvm::BasicBlock *EHSpecHandler = createBasicBlock("ehspec.handler");
323 llvm::BasicBlock *Match = createBasicBlock("match");
324 llvm::BasicBlock *Unwind = 0;
325
326 assert(PrevLandingPad == 0 && "EHSpec has invoke context");
327
328 llvm::BasicBlock *Cont = createBasicBlock("cont");
329
330 EmitBranchThroughCleanup(Cont);
331
332 // Emit the statements in the try {} block
333 setInvokeDest(EHSpecHandler);
334
335 EmitBlock(EHSpecHandler);
336 // Exception object
337 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
338 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
339
340 SelectorArgs.push_back(Exc);
341 SelectorArgs.push_back(Personality);
342 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
343 Proto->getNumExceptions()+1));
344
345 for (unsigned i = 0; i < Proto->getNumExceptions(); ++i) {
346 QualType Ty = Proto->getExceptionType(i);
347 llvm::Value *EHType
348 = CGM.GenerateRTTI(Ty.getNonReferenceType());
349 SelectorArgs.push_back(EHType);
350 }
351 if (Proto->getNumExceptions())
352 SelectorArgs.push_back(Null);
353
354 // Find which handler was matched.
355 llvm::Value *Selector
356 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
357 SelectorArgs.end(), "selector");
358 if (Proto->getNumExceptions()) {
359 Unwind = createBasicBlock("Unwind");
360
361 Builder.CreateStore(Exc, RethrowPtr);
362 Builder.CreateCondBr(Builder.CreateICmpSLT(Selector,
363 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
364 0)),
365 Match, Unwind);
366
367 EmitBlock(Match);
368 }
369 Builder.CreateCall(getUnexpectedFn(*this), Exc)->setDoesNotReturn();
370 Builder.CreateUnreachable();
371
372 if (Proto->getNumExceptions()) {
373 EmitBlock(Unwind);
374 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
375 Builder.CreateLoad(RethrowPtr));
376 Builder.CreateUnreachable();
377 }
378
379 EmitBlock(Cont);
380}
381
382void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
383 const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
384 if (FD == 0)
385 return;
386 const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
387 if (Proto == 0)
388 return;
389
390 if (!Proto->hasExceptionSpec())
391 return;
392
393 setInvokeDest(0);
394}
395
Mike Stump58ef18b2009-11-20 23:44:51 +0000396void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
Mike Stump8c1253b2009-12-04 03:57:07 +0000397 if (0) {
Mike Stumpa9a590c2009-12-02 18:20:18 +0000398 EmitStmt(S.getTryBlock());
399 return;
Mike Stump54066142009-12-01 03:41:18 +0000400 }
Mike Stumpbee78dd2009-12-04 23:26:17 +0000401
Mike Stump54066142009-12-01 03:41:18 +0000402 // FIXME: The below is still just a sketch of the code we need.
Mike Stump58ef18b2009-11-20 23:44:51 +0000403 // Pointer to the personality function
404 llvm::Constant *Personality =
405 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
406 (VMContext),
407 true),
408 "__gxx_personality_v0");
409 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
Mike Stump97329152009-12-02 19:53:57 +0000410 llvm::Value *llvm_eh_exception =
411 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
412 llvm::Value *llvm_eh_selector =
413 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump58ef18b2009-11-20 23:44:51 +0000414
Mike Stump58ef18b2009-11-20 23:44:51 +0000415 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
416 llvm::BasicBlock *TryHandler = createBasicBlock("try.handler");
Mike Stump58ef18b2009-11-20 23:44:51 +0000417 llvm::BasicBlock *FinallyBlock = createBasicBlock("finally");
Mike Stump54066142009-12-01 03:41:18 +0000418 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump58ef18b2009-11-20 23:44:51 +0000419 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
420
421 // Push an EH context entry, used for handling rethrows.
422 PushCleanupBlock(FinallyBlock);
423
424 // Emit the statements in the try {} block
425 setInvokeDest(TryHandler);
426
Mike Stumpbee78dd2009-12-04 23:26:17 +0000427 // FIXME: We should not have to do this here. The AST should have the member
428 // initializers under the CXXTryStmt's TryBlock.
429 if (OuterTryBlock == &S) {
430 GlobalDecl GD = CurGD;
431 const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
432
433 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(FD)) {
434 size_t OldCleanupStackSize = CleanupEntries.size();
435 EmitCtorPrologue(CD, CurGD.getCtorType());
436 EmitStmt(S.getTryBlock());
437
438 // If any of the member initializers are temporaries bound to references
439 // make sure to emit their destructors.
440 EmitCleanupBlocks(OldCleanupStackSize);
441 } else if (const CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(FD)) {
442 llvm::BasicBlock *DtorEpilogue = createBasicBlock("dtor.epilogue");
443 PushCleanupBlock(DtorEpilogue);
444
445 EmitStmt(S.getTryBlock());
446
447 CleanupBlockInfo Info = PopCleanupBlock();
448
449 assert(Info.CleanupBlock == DtorEpilogue && "Block mismatch!");
450 EmitBlock(DtorEpilogue);
451 EmitDtorEpilogue(DD, GD.getDtorType());
452
453 if (Info.SwitchBlock)
454 EmitBlock(Info.SwitchBlock);
455 if (Info.EndBlock)
456 EmitBlock(Info.EndBlock);
457 } else
458 EmitStmt(S.getTryBlock());
459 } else
460 EmitStmt(S.getTryBlock());
Mike Stump58ef18b2009-11-20 23:44:51 +0000461
462 // Jump to end if there is no exception
463 EmitBranchThroughCleanup(FinallyEnd);
464
Mike Stump2b488872009-12-09 22:59:31 +0000465 llvm::BasicBlock *TerminateHandler = getTerminateHandler();
Mike Stump97329152009-12-02 19:53:57 +0000466
Mike Stump58ef18b2009-11-20 23:44:51 +0000467 // Emit the handlers
468 EmitBlock(TryHandler);
469
470 const llvm::IntegerType *Int8Ty;
471 const llvm::PointerType *PtrToInt8Ty;
472 Int8Ty = llvm::Type::getInt8Ty(VMContext);
473 // C string type. Used in lots of places.
474 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump54066142009-12-01 03:41:18 +0000475 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
476 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump54066142009-12-01 03:41:18 +0000477 llvm::Value *llvm_eh_typeid_for =
478 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump58ef18b2009-11-20 23:44:51 +0000479 // Exception object
Mike Stump2b488872009-12-09 22:59:31 +0000480 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump54066142009-12-01 03:41:18 +0000481 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump58ef18b2009-11-20 23:44:51 +0000482
Mike Stump2b488872009-12-09 22:59:31 +0000483 llvm::SmallVector<llvm::Value*, 8> Args;
Mike Stump97329152009-12-02 19:53:57 +0000484 Args.clear();
Mike Stump54066142009-12-01 03:41:18 +0000485 SelectorArgs.push_back(Exc);
486 SelectorArgs.push_back(Personality);
Mike Stump58ef18b2009-11-20 23:44:51 +0000487
Mike Stump54066142009-12-01 03:41:18 +0000488 bool HasCatchAll = false;
Mike Stump58ef18b2009-11-20 23:44:51 +0000489 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
490 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump54066142009-12-01 03:41:18 +0000491 VarDecl *CatchParam = C->getExceptionDecl();
492 if (CatchParam) {
Mike Stumpc01c2b82009-12-02 18:57:08 +0000493 llvm::Value *EHType
494 = CGM.GenerateRTTI(C->getCaughtType().getNonReferenceType());
Mike Stump54066142009-12-01 03:41:18 +0000495 SelectorArgs.push_back(EHType);
Mike Stump58ef18b2009-11-20 23:44:51 +0000496 } else {
497 // null indicates catch all
Mike Stump54066142009-12-01 03:41:18 +0000498 SelectorArgs.push_back(Null);
499 HasCatchAll = true;
Mike Stump58ef18b2009-11-20 23:44:51 +0000500 }
501 }
502
Mike Stump54066142009-12-01 03:41:18 +0000503 // We use a cleanup unless there was already a catch all.
504 if (!HasCatchAll) {
505 SelectorArgs.push_back(Null);
506 }
Mike Stump58ef18b2009-11-20 23:44:51 +0000507
Mike Stump54066142009-12-01 03:41:18 +0000508 // Find which handler was matched.
509 llvm::Value *Selector
510 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
511 SelectorArgs.end(), "selector");
Mike Stump58ef18b2009-11-20 23:44:51 +0000512 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
513 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump54066142009-12-01 03:41:18 +0000514 VarDecl *CatchParam = C->getExceptionDecl();
515 Stmt *CatchBody = C->getHandlerBlock();
516
517 llvm::BasicBlock *Next = 0;
518
519 if (SelectorArgs[i+2] != Null) {
520 llvm::BasicBlock *Match = createBasicBlock("match");
521 Next = createBasicBlock("catch.next");
522 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
523 llvm::Value *Id
524 = Builder.CreateCall(llvm_eh_typeid_for,
525 Builder.CreateBitCast(SelectorArgs[i+2],
526 Int8PtrTy));
527 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
528 Match, Next);
529 EmitBlock(Match);
530 }
531
532 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
533 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
534
535 PushCleanupBlock(MatchEnd);
536 setInvokeDest(MatchHandler);
537
538 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
539
Mike Stump90990962009-12-02 23:37:16 +0000540 {
541 CleanupScope CatchScope(*this);
542 // Bind the catch parameter if it exists.
543 if (CatchParam) {
544 QualType CatchType = CatchParam->getType().getNonReferenceType();
545 setInvokeDest(TerminateHandler);
Mike Stump7398ff02009-12-03 22:38:15 +0000546 bool WasPointer = true;
547 if (!CatchType.getTypePtr()->isPointerType()) {
Mike Stump114ab9f2009-12-04 01:51:45 +0000548 if (!isa<ReferenceType>(CatchParam->getType()))
549 WasPointer = false;
Mike Stump90990962009-12-02 23:37:16 +0000550 CatchType = getContext().getPointerType(CatchType);
Mike Stump7398ff02009-12-03 22:38:15 +0000551 }
Mike Stump90990962009-12-02 23:37:16 +0000552 ExcObject = Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
Mike Stump90990962009-12-02 23:37:16 +0000553 EmitLocalBlockVarDecl(*CatchParam);
Mike Stump2842b4c2009-12-03 03:40:14 +0000554 // FIXME: we need to do this sooner so that the EH region for the
555 // cleanup doesn't start until after the ctor completes, use a decl
556 // init?
Mike Stump90990962009-12-02 23:37:16 +0000557 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
Mike Stump7398ff02009-12-03 22:38:15 +0000558 WasPointer, ExcObject, GetAddrOfLocalVar(CatchParam));
Mike Stump90990962009-12-02 23:37:16 +0000559 setInvokeDest(MatchHandler);
560 }
561
562 EmitStmt(CatchBody);
Mike Stump54066142009-12-01 03:41:18 +0000563 }
564
Mike Stump54066142009-12-01 03:41:18 +0000565 EmitBranchThroughCleanup(FinallyEnd);
566
567 EmitBlock(MatchHandler);
568
569 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
570 // We are required to emit this call to satisfy LLVM, even
571 // though we don't use the result.
Mike Stump97329152009-12-02 19:53:57 +0000572 Args.clear();
Mike Stump54066142009-12-01 03:41:18 +0000573 Args.push_back(Exc);
574 Args.push_back(Personality);
575 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
576 0));
577 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
578 Builder.CreateStore(Exc, RethrowPtr);
579 EmitBranchThroughCleanup(FinallyRethrow);
580
581 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
582
583 EmitBlock(MatchEnd);
584
Mike Stump33270212009-12-02 07:41:41 +0000585 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump54066142009-12-01 03:41:18 +0000586 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump33270212009-12-02 07:41:41 +0000587 Cont, TerminateHandler,
Mike Stump54066142009-12-01 03:41:18 +0000588 Args.begin(), Args.begin());
Mike Stump54066142009-12-01 03:41:18 +0000589 EmitBlock(Cont);
590 if (Info.SwitchBlock)
591 EmitBlock(Info.SwitchBlock);
592 if (Info.EndBlock)
593 EmitBlock(Info.EndBlock);
594
Mike Stump54066142009-12-01 03:41:18 +0000595 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump54066142009-12-01 03:41:18 +0000596 Builder.CreateStore(Exc, RethrowPtr);
597 EmitBranchThroughCleanup(FinallyRethrow);
598
599 if (Next)
600 EmitBlock(Next);
601 }
Mike Stump5c820752009-12-04 19:03:47 +0000602 if (!HasCatchAll) {
603 Builder.CreateStore(Exc, RethrowPtr);
Mike Stump54066142009-12-01 03:41:18 +0000604 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stump5c820752009-12-04 19:03:47 +0000605 }
Mike Stump58ef18b2009-11-20 23:44:51 +0000606
607 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
608
609 setInvokeDest(PrevLandingPad);
610
611 EmitBlock(FinallyBlock);
612
Mike Stump54066142009-12-01 03:41:18 +0000613 if (Info.SwitchBlock)
614 EmitBlock(Info.SwitchBlock);
615 if (Info.EndBlock)
616 EmitBlock(Info.EndBlock);
617
Mike Stump58ef18b2009-11-20 23:44:51 +0000618 // Branch around the rethrow code.
619 EmitBranch(FinallyEnd);
620
621 EmitBlock(FinallyRethrow);
Mike Stump875912a2009-12-04 19:21:57 +0000622 // FIXME: Eventually we can chain the handlers together and just do a call
623 // here.
624 if (getInvokeDest()) {
625 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
626 Builder.CreateInvoke(getUnwindResumeOrRethrowFn(*this), Cont,
627 getInvokeDest(),
628 Builder.CreateLoad(RethrowPtr));
629 EmitBlock(Cont);
630 } else
631 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
632 Builder.CreateLoad(RethrowPtr));
633
Mike Stump58ef18b2009-11-20 23:44:51 +0000634 Builder.CreateUnreachable();
Mike Stump58ef18b2009-11-20 23:44:51 +0000635
636 EmitBlock(FinallyEnd);
Mike Stump58ef18b2009-11-20 23:44:51 +0000637}
Mike Stumpaff69af2009-12-09 03:35:49 +0000638
639CodeGenFunction::EHCleanupBlock::~EHCleanupBlock() {
640 llvm::BasicBlock *Cont1 = CGF.createBasicBlock("cont");
641 CGF.EmitBranch(Cont1);
642 CGF.setInvokeDest(PreviousInvokeDest);
643
644
645 CGF.EmitBlock(CleanupHandler);
646
647 llvm::Constant *Personality =
648 CGF.CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
649 (CGF.VMContext),
650 true),
651 "__gxx_personality_v0");
652 Personality = llvm::ConstantExpr::getBitCast(Personality, CGF.PtrToInt8Ty);
653 llvm::Value *llvm_eh_exception =
654 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
655 llvm::Value *llvm_eh_selector =
656 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
657
658 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
659 const llvm::IntegerType *Int8Ty;
660 const llvm::PointerType *PtrToInt8Ty;
661 Int8Ty = llvm::Type::getInt8Ty(CGF.VMContext);
662 // C string type. Used in lots of places.
663 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
664 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
665 llvm::SmallVector<llvm::Value*, 8> Args;
666 Args.clear();
667 Args.push_back(Exc);
668 Args.push_back(Personality);
669 Args.push_back(Null);
670 CGF.Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
671
672 CGF.EmitBlock(CleanupEntryBB);
673
674 CGF.EmitBlock(Cont1);
675
676 if (CGF.getInvokeDest()) {
677 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
678 CGF.Builder.CreateInvoke(getUnwindResumeOrRethrowFn(CGF), Cont,
679 CGF.getInvokeDest(), Exc);
680 CGF.EmitBlock(Cont);
681 } else
682 CGF.Builder.CreateCall(getUnwindResumeOrRethrowFn(CGF), Exc);
683
684 CGF.Builder.CreateUnreachable();
685
686 CGF.EmitBlock(Cont);
687 if (CGF.Exceptions)
688 CGF.setInvokeDest(CleanupHandler);
689}
Mike Stump2b488872009-12-09 22:59:31 +0000690
691llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
Mike Stump25b20fc2009-12-09 23:31:35 +0000692 llvm::BasicBlock *Cont = 0;
693
694 if (HaveInsertPoint()) {
695 Cont = createBasicBlock("cont");
696 EmitBranch(Cont);
697 }
698
Mike Stump2b488872009-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
711 llvm::BasicBlock *TerminateHandler = createBasicBlock("terminate.handler");
712 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 Stump25b20fc2009-12-09 23:31:35 +0000720 1));
Mike Stump2b488872009-12-09 22:59:31 +0000721 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
722 llvm::CallInst *TerminateCall =
723 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 Stump25b20fc2009-12-09 23:31:35 +0000731 if (Cont)
732 EmitBlock(Cont);
733
Mike Stump2b488872009-12-09 22:59:31 +0000734 return TerminateHandler;
735}