blob: c93f2b7b120861fbed6f96968bf20daff349c4fe [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);
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 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);
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 Carlssond3379292009-10-30 02:27:02 +000046static llvm::Constant *getThrowFn(CodeGenFunction &CGF) {
Mike Stump99533832009-12-02 07:41:41 +000047 // void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
48 // 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);
52
53 const llvm::FunctionType *FTy =
Mike Stumpb4eea692009-11-20 00:56:31 +000054 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()),
55 Args, false);
Anders Carlssond3379292009-10-30 02:27:02 +000056
57 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
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 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);
74
75 const llvm::FunctionType *FTy =
Mike Stump0f590be2009-12-01 03:41:18 +000076 llvm::FunctionType::get(Int8PtrTy, Args, false);
Mike Stump2bf701e2009-11-20 23:44:51 +000077
78 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
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 Stump0f590be2009-12-01 03:41:18 +000090// FIXME: Eventually this will all go into the backend. Set from the target for
91// now.
92static int using_sjlj_exceptions = 0;
93
94static llvm::Constant *getUnwindResumeOrRethrowFn(CodeGenFunction &CGF) {
95 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
96 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
97
98 const llvm::FunctionType *FTy =
99 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), Args,
100 false);
101
102 if (using_sjlj_exceptions)
103 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_SjLj_Resume");
104 return CGF.CGM.CreateRuntimeFunction(FTy, "_Unwind_Resume_or_Rethrow");
105}
106
Mike Stump99533832009-12-02 07:41:41 +0000107static llvm::Constant *getTerminateFn(CodeGenFunction &CGF) {
108 // void __terminate();
109
110 const llvm::FunctionType *FTy =
111 llvm::FunctionType::get(llvm::Type::getVoidTy(CGF.getLLVMContext()), false);
112
113 return CGF.CGM.CreateRuntimeFunction(FTy, "_ZSt9terminatev");
114}
115
Mike Stump0f590be2009-12-01 03:41:18 +0000116// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
117// N is casted to the right type.
118static void CopyObject(CodeGenFunction &CGF, const Expr *E, llvm::Value *N) {
119 QualType ObjectType = E->getType();
120
121 // Store the throw exception in the exception object.
122 if (!CGF.hasAggregateLLVMType(ObjectType)) {
123 llvm::Value *Value = CGF.EmitScalarExpr(E);
124 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
125
126 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
127 } else {
128 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
129 const CXXRecordDecl *RD;
130 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
131 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
132 if (RD->hasTrivialCopyConstructor()) {
133 CGF.EmitAggExpr(E, This, false);
134 } else if (CXXConstructorDecl *CopyCtor
135 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump99533832009-12-02 07:41:41 +0000136 // All temporaries end before we call __cxa_throw
137 CodeGenFunction::CleanupScope TryScope(CGF);
138 {
139 // These actions are only on the exceptional edge.
140 CodeGenFunction::DelayedCleanupBlock Scope(CGF, true);
141
142 llvm::Constant *FreeExceptionFn = getFreeExceptionFn(CGF);
143 const llvm::Type *Int8PtrTy
144 = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
145 llvm::Value *ExceptionPtr = CGF.Builder.CreateBitCast(N, Int8PtrTy);
146 CGF.Builder.CreateCall(FreeExceptionFn, ExceptionPtr);
147 }
148
Mike Stump0f590be2009-12-01 03:41:18 +0000149 llvm::Value *Src = CGF.EmitLValue(E).getAddress();
150
151 // Stolen from EmitClassAggrMemberwiseCopy
152 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
153 Ctor_Complete);
154 CallArgList CallArgs;
155 CallArgs.push_back(std::make_pair(RValue::get(This),
156 CopyCtor->getThisType(CGF.getContext())));
157
158 // Push the Src ptr.
159 CallArgs.push_back(std::make_pair(RValue::get(Src),
160 CopyCtor->getParamDecl(0)->getType()));
161 QualType ResultType =
162 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
163 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
164 Callee, CallArgs, CopyCtor);
Mike Stump0f590be2009-12-01 03:41:18 +0000165 } else
Mike Stump99533832009-12-02 07:41:41 +0000166 llvm::llvm_unreachable("uncopyable object");
Mike Stump0f590be2009-12-01 03:41:18 +0000167 }
168}
169
170// CopyObject - Utility to copy an object. Calls copy constructor as necessary.
171// N is casted to the right type.
172static void CopyObject(CodeGenFunction &CGF, QualType ObjectType,
173 llvm::Value *E, llvm::Value *N) {
174 // Store the throw exception in the exception object.
175 if (!CGF.hasAggregateLLVMType(ObjectType)) {
176 llvm::Value *Value = E;
177 const llvm::Type *ValuePtrTy = Value->getType()->getPointerTo(0);
178
179 CGF.Builder.CreateStore(Value, CGF.Builder.CreateBitCast(N, ValuePtrTy));
180 } else {
181 const llvm::Type *Ty = CGF.ConvertType(ObjectType)->getPointerTo(0);
182 const CXXRecordDecl *RD;
183 RD = cast<CXXRecordDecl>(ObjectType->getAs<RecordType>()->getDecl());
184 llvm::Value *This = CGF.Builder.CreateBitCast(N, Ty);
185 if (RD->hasTrivialCopyConstructor()) {
186 CGF.EmitAggregateCopy(This, E, ObjectType);
187 } else if (CXXConstructorDecl *CopyCtor
188 = RD->getCopyConstructor(CGF.getContext(), 0)) {
Mike Stump99533832009-12-02 07:41:41 +0000189 // FIXME: region management, call terminate
Mike Stump0f590be2009-12-01 03:41:18 +0000190 llvm::Value *Src = E;
191
192 // Stolen from EmitClassAggrMemberwiseCopy
193 llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor,
194 Ctor_Complete);
195 CallArgList CallArgs;
196 CallArgs.push_back(std::make_pair(RValue::get(This),
197 CopyCtor->getThisType(CGF.getContext())));
198
199 // Push the Src ptr.
200 CallArgs.push_back(std::make_pair(RValue::get(Src),
201 CopyCtor->getParamDecl(0)->getType()));
202 QualType ResultType =
203 CopyCtor->getType()->getAs<FunctionType>()->getResultType();
204 CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(ResultType, CallArgs),
205 Callee, CallArgs, CopyCtor);
206 // FIXME: region management
207 } else
208 llvm::llvm_unreachable("uncopyable object");
209 }
210}
211
Anders Carlsson756b5c42009-10-30 01:42:31 +0000212void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E) {
Anders Carlssond3379292009-10-30 02:27:02 +0000213 if (!E->getSubExpr()) {
Mike Stumpb4eea692009-11-20 00:56:31 +0000214 Builder.CreateCall(getReThrowFn(*this))->setDoesNotReturn();
215 Builder.CreateUnreachable();
216
217 // Clear the insertion point to indicate we are in unreachable code.
218 Builder.ClearInsertionPoint();
Anders Carlssond3379292009-10-30 02:27:02 +0000219 return;
220 }
221
222 QualType ThrowType = E->getSubExpr()->getType();
Anders Carlssond3379292009-10-30 02:27:02 +0000223 // FIXME: Handle cleanup.
224 if (!CleanupEntries.empty()){
Mike Stump2bf701e2009-11-20 23:44:51 +0000225 ErrorUnsupported(E, "throw expression with cleanup entries");
Anders Carlssond3379292009-10-30 02:27:02 +0000226 return;
227 }
228
229 // Now allocate the exception object.
230 const llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
231 uint64_t TypeSize = getContext().getTypeSize(ThrowType) / 8;
232
233 llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(*this);
234 llvm::Value *ExceptionPtr =
235 Builder.CreateCall(AllocExceptionFn,
236 llvm::ConstantInt::get(SizeTy, TypeSize),
237 "exception");
238
Mike Stump0f590be2009-12-01 03:41:18 +0000239 CopyObject(*this, E->getSubExpr(), ExceptionPtr);
Anders Carlssond3379292009-10-30 02:27:02 +0000240
241 // Now throw the exception.
242 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
Mike Stump23886d02009-11-20 00:43:57 +0000243 llvm::Constant *TypeInfo = CGM.GenerateRtti(ThrowType);
Anders Carlssond3379292009-10-30 02:27:02 +0000244 llvm::Constant *Dtor = llvm::Constant::getNullValue(Int8PtrTy);
245
246 llvm::CallInst *ThrowCall =
247 Builder.CreateCall3(getThrowFn(*this), ExceptionPtr, TypeInfo, Dtor);
248 ThrowCall->setDoesNotReturn();
249 Builder.CreateUnreachable();
250
251 // Clear the insertion point to indicate we are in unreachable code.
252 Builder.ClearInsertionPoint();
Anders Carlsson756b5c42009-10-30 01:42:31 +0000253}
Mike Stump2bf701e2009-11-20 23:44:51 +0000254
255void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
Mike Stump0f590be2009-12-01 03:41:18 +0000256#if 1
Mike Stump2bf701e2009-11-20 23:44:51 +0000257 EmitStmt(S.getTryBlock());
Mike Stump0f590be2009-12-01 03:41:18 +0000258 if (0) {
259 getBeginCatchFn(*this);
260 getEndCatchFn(*this);
261 getUnwindResumeOrRethrowFn(*this);
262 CopyObject(*this, QualType(), 0, 0);
263 }
264#else
265 // FIXME: The below is still just a sketch of the code we need.
Mike Stump2bf701e2009-11-20 23:44:51 +0000266 // Pointer to the personality function
267 llvm::Constant *Personality =
268 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty
269 (VMContext),
270 true),
271 "__gxx_personality_v0");
272 Personality = llvm::ConstantExpr::getBitCast(Personality, PtrToInt8Ty);
273
Mike Stump2bf701e2009-11-20 23:44:51 +0000274 llvm::BasicBlock *PrevLandingPad = getInvokeDest();
275 llvm::BasicBlock *TryHandler = createBasicBlock("try.handler");
Mike Stump2bf701e2009-11-20 23:44:51 +0000276 llvm::BasicBlock *FinallyBlock = createBasicBlock("finally");
Mike Stump0f590be2009-12-01 03:41:18 +0000277 llvm::BasicBlock *FinallyRethrow = createBasicBlock("finally.throw");
Mike Stump2bf701e2009-11-20 23:44:51 +0000278 llvm::BasicBlock *FinallyEnd = createBasicBlock("finally.end");
279
280 // Push an EH context entry, used for handling rethrows.
281 PushCleanupBlock(FinallyBlock);
282
283 // Emit the statements in the try {} block
284 setInvokeDest(TryHandler);
285
286 EmitStmt(S.getTryBlock());
287
288 // Jump to end if there is no exception
289 EmitBranchThroughCleanup(FinallyEnd);
290
291 // Emit the handlers
292 EmitBlock(TryHandler);
293
294 const llvm::IntegerType *Int8Ty;
295 const llvm::PointerType *PtrToInt8Ty;
296 Int8Ty = llvm::Type::getInt8Ty(VMContext);
297 // C string type. Used in lots of places.
298 PtrToInt8Ty = llvm::PointerType::getUnqual(Int8Ty);
Mike Stump0f590be2009-12-01 03:41:18 +0000299 llvm::Constant *Null = llvm::ConstantPointerNull::get(PtrToInt8Ty);
300 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
Mike Stump2bf701e2009-11-20 23:44:51 +0000301 llvm::Value *llvm_eh_exception =
302 CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
303 llvm::Value *llvm_eh_selector =
304 CGM.getIntrinsic(llvm::Intrinsic::eh_selector);
Mike Stump0f590be2009-12-01 03:41:18 +0000305 llvm::Value *llvm_eh_typeid_for =
306 CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
Mike Stump2bf701e2009-11-20 23:44:51 +0000307 // Exception object
308 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000309 llvm::Value *RethrowPtr = CreateTempAlloca(Exc->getType(), "_rethrow");
Mike Stump2bf701e2009-11-20 23:44:51 +0000310
Mike Stump0f590be2009-12-01 03:41:18 +0000311 SelectorArgs.push_back(Exc);
312 SelectorArgs.push_back(Personality);
Mike Stump2bf701e2009-11-20 23:44:51 +0000313
Mike Stump0f590be2009-12-01 03:41:18 +0000314 bool HasCatchAll = false;
Mike Stump2bf701e2009-11-20 23:44:51 +0000315 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
316 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000317 VarDecl *CatchParam = C->getExceptionDecl();
318 if (CatchParam) {
319 llvm::Value *EHType = CGM.GenerateRtti(C->getCaughtType().getNonReferenceType());
320 SelectorArgs.push_back(EHType);
Mike Stump2bf701e2009-11-20 23:44:51 +0000321 } else {
322 // null indicates catch all
Mike Stump0f590be2009-12-01 03:41:18 +0000323 SelectorArgs.push_back(Null);
324 HasCatchAll = true;
Mike Stump2bf701e2009-11-20 23:44:51 +0000325 }
326 }
327
Mike Stump0f590be2009-12-01 03:41:18 +0000328 // We use a cleanup unless there was already a catch all.
329 if (!HasCatchAll) {
330 SelectorArgs.push_back(Null);
331 }
Mike Stump2bf701e2009-11-20 23:44:51 +0000332
Mike Stump0f590be2009-12-01 03:41:18 +0000333 // Find which handler was matched.
334 llvm::Value *Selector
335 = Builder.CreateCall(llvm_eh_selector, SelectorArgs.begin(),
336 SelectorArgs.end(), "selector");
Mike Stump99533832009-12-02 07:41:41 +0000337 llvm::BasicBlock *TerminateHandler = 0;
Mike Stump2bf701e2009-11-20 23:44:51 +0000338 for (unsigned i = 0; i<S.getNumHandlers(); ++i) {
339 const CXXCatchStmt *C = S.getHandler(i);
Mike Stump0f590be2009-12-01 03:41:18 +0000340 VarDecl *CatchParam = C->getExceptionDecl();
341 Stmt *CatchBody = C->getHandlerBlock();
342
343 llvm::BasicBlock *Next = 0;
344
345 if (SelectorArgs[i+2] != Null) {
346 llvm::BasicBlock *Match = createBasicBlock("match");
347 Next = createBasicBlock("catch.next");
348 const llvm::Type *Int8PtrTy = llvm::Type::getInt8PtrTy(getLLVMContext());
349 llvm::Value *Id
350 = Builder.CreateCall(llvm_eh_typeid_for,
351 Builder.CreateBitCast(SelectorArgs[i+2],
352 Int8PtrTy));
353 Builder.CreateCondBr(Builder.CreateICmpEQ(Selector, Id),
354 Match, Next);
355 EmitBlock(Match);
356 }
357
358 llvm::BasicBlock *MatchEnd = createBasicBlock("match.end");
359 llvm::BasicBlock *MatchHandler = createBasicBlock("match.handler");
360
361 PushCleanupBlock(MatchEnd);
362 setInvokeDest(MatchHandler);
363
364 llvm::Value *ExcObject = Builder.CreateCall(getBeginCatchFn(*this), Exc);
365
366 // Bind the catch parameter if it exists.
367 if (CatchParam) {
368 QualType CatchType = CatchParam->getType().getNonReferenceType();
369 if (!CatchType.getTypePtr()->isPointerType())
370 CatchType = getContext().getPointerType(CatchType);
371 ExcObject =
372 Builder.CreateBitCast(ExcObject, ConvertType(CatchType));
373 // CatchParam is a ParmVarDecl because of the grammar
374 // construction used to handle this, but for codegen purposes
375 // we treat this as a local decl.
376 EmitLocalBlockVarDecl(*CatchParam);
377#if 0
378 // FIXME: objects with ctors, references
379 Builder.CreateStore(ExcObject, GetAddrOfLocalVar(CatchParam));
380#else
381 CopyObject(*this, CatchParam->getType().getNonReferenceType(),
382 ExcObject, GetAddrOfLocalVar(CatchParam));
383#endif
384 }
385
386 EmitStmt(CatchBody);
387 EmitBranchThroughCleanup(FinallyEnd);
388
389 EmitBlock(MatchHandler);
390
391 llvm::Value *Exc = Builder.CreateCall(llvm_eh_exception, "exc");
392 // We are required to emit this call to satisfy LLVM, even
393 // though we don't use the result.
394 llvm::SmallVector<llvm::Value*, 8> Args;
395 Args.push_back(Exc);
396 Args.push_back(Personality);
397 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
398 0));
399 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
400 Builder.CreateStore(Exc, RethrowPtr);
401 EmitBranchThroughCleanup(FinallyRethrow);
402
403 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
404
405 EmitBlock(MatchEnd);
406
Mike Stump99533832009-12-02 07:41:41 +0000407 // Set up terminate handler
408 bool GenerateTerminate = false;
409 if (!TerminateHandler) {
410 TerminateHandler = createBasicBlock("terminate.handler");
411 GenerateTerminate = true;
412 }
413 llvm::BasicBlock *Cont = createBasicBlock("invoke.cont");
Mike Stump0f590be2009-12-01 03:41:18 +0000414 Builder.CreateInvoke(getEndCatchFn(*this),
Mike Stump99533832009-12-02 07:41:41 +0000415 Cont, TerminateHandler,
Mike Stump0f590be2009-12-01 03:41:18 +0000416 Args.begin(), Args.begin());
417
418 EmitBlock(Cont);
419 if (Info.SwitchBlock)
420 EmitBlock(Info.SwitchBlock);
421 if (Info.EndBlock)
422 EmitBlock(Info.EndBlock);
423
Mike Stump0f590be2009-12-01 03:41:18 +0000424 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
Mike Stump0f590be2009-12-01 03:41:18 +0000425 Builder.CreateStore(Exc, RethrowPtr);
426 EmitBranchThroughCleanup(FinallyRethrow);
427
Mike Stump99533832009-12-02 07:41:41 +0000428 if (GenerateTerminate) {
429 GenerateTerminate = false;
430 EmitBlock(TerminateHandler);
431 Exc = Builder.CreateCall(llvm_eh_exception, "exc");
432 // We are required to emit this call to satisfy LLVM, even
433 // though we don't use the result.
434 Args.clear();
435 Args.push_back(Exc);
436 Args.push_back(Personality);
437 Args.push_back(llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext),
438 0));
439 Builder.CreateCall(llvm_eh_selector, Args.begin(), Args.end());
440 llvm::CallInst *TerminateCall =
441 Builder.CreateCall(getTerminateFn(*this));
442 TerminateCall->setDoesNotReturn();
Mike Stump666571a2009-12-02 08:18:09 +0000443 TerminateCall->setDoesNotThrow();
Mike Stump99533832009-12-02 07:41:41 +0000444 Builder.CreateUnreachable();
445
446 // Clear the insertion point to indicate we are in unreachable code.
447 Builder.ClearInsertionPoint();
448 }
449
Mike Stump0f590be2009-12-01 03:41:18 +0000450 if (Next)
451 EmitBlock(Next);
452 }
453 if (!HasCatchAll)
454 EmitBranchThroughCleanup(FinallyRethrow);
Mike Stump2bf701e2009-11-20 23:44:51 +0000455
456 CodeGenFunction::CleanupBlockInfo Info = PopCleanupBlock();
457
458 setInvokeDest(PrevLandingPad);
459
460 EmitBlock(FinallyBlock);
461
Mike Stump0f590be2009-12-01 03:41:18 +0000462 if (Info.SwitchBlock)
463 EmitBlock(Info.SwitchBlock);
464 if (Info.EndBlock)
465 EmitBlock(Info.EndBlock);
466
Mike Stump2bf701e2009-11-20 23:44:51 +0000467 // Branch around the rethrow code.
468 EmitBranch(FinallyEnd);
469
470 EmitBlock(FinallyRethrow);
Mike Stump0f590be2009-12-01 03:41:18 +0000471 Builder.CreateCall(getUnwindResumeOrRethrowFn(*this),
472 Builder.CreateLoad(RethrowPtr));
Mike Stump2bf701e2009-11-20 23:44:51 +0000473 Builder.CreateUnreachable();
Mike Stump2bf701e2009-11-20 23:44:51 +0000474
475 EmitBlock(FinallyEnd);
476#endif
477}