2009-10-30 09:42:31 +08:00
|
|
|
//===--- CGException.cpp - Emit LLVM Code for C++ exceptions --------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This contains code dealing with C++ exception related code generation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
2014-11-25 15:20:20 +08:00
|
|
|
#include "CGCXXABI.h"
|
2015-01-14 19:29:14 +08:00
|
|
|
#include "CGCleanup.h"
|
2012-02-08 20:41:24 +08:00
|
|
|
#include "CGObjCRuntime.h"
|
2010-07-21 06:17:55 +08:00
|
|
|
#include "TargetInfo.h"
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
#include "clang/AST/Mangle.h"
|
2012-02-08 20:41:24 +08:00
|
|
|
#include "clang/AST/StmtCXX.h"
|
2013-01-19 16:09:44 +08:00
|
|
|
#include "clang/AST/StmtObjC.h"
|
2014-03-04 19:02:08 +08:00
|
|
|
#include "llvm/IR/CallSite.h"
|
2013-01-02 19:45:17 +08:00
|
|
|
#include "llvm/IR/Intrinsics.h"
|
2015-02-12 05:40:48 +08:00
|
|
|
#include "llvm/Support/SaveAndRestore.h"
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2009-10-30 09:42:31 +08:00
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
static llvm::Constant *getAllocateExceptionFn(CodeGenModule &CGM) {
|
2009-10-30 10:27:02 +08:00
|
|
|
// void *__cxa_allocate_exception(size_t thrown_size);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::FunctionType::get(CGM.Int8PtrTy, CGM.SizeTy, /*IsVarArgs=*/false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, "__cxa_allocate_exception");
|
2009-10-30 10:27:02 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
static llvm::Constant *getFreeExceptionFn(CodeGenModule &CGM) {
|
2009-12-02 15:41:41 +08:00
|
|
|
// void __cxa_free_exception(void *thrown_exception);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, "__cxa_free_exception");
|
2009-12-02 15:41:41 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
static llvm::Constant *getThrowFn(CodeGenModule &CGM) {
|
2009-12-10 08:06:18 +08:00
|
|
|
// void __cxa_throw(void *thrown_exception, std::type_info *tinfo,
|
2009-12-02 15:41:41 +08:00
|
|
|
// void (*dest) (void *));
|
2009-10-30 10:27:02 +08:00
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::Type *Args[3] = { CGM.Int8PtrTy, CGM.Int8PtrTy, CGM.Int8PtrTy };
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, Args, /*IsVarArgs=*/false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, "__cxa_throw");
|
2009-10-30 10:27:02 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
static llvm::Constant *getGetExceptionPtrFn(CodeGenModule &CGM) {
|
2010-07-06 09:34:17 +08:00
|
|
|
// void *__cxa_get_exception_ptr(void*);
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::FunctionType::get(CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, "__cxa_get_exception_ptr");
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
static llvm::Constant *getBeginCatchFn(CodeGenModule &CGM) {
|
2010-07-06 09:34:17 +08:00
|
|
|
// void *__cxa_begin_catch(void*);
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::FunctionType::get(CGM.Int8PtrTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, "__cxa_begin_catch");
|
2009-11-21 07:44:51 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
static llvm::Constant *getEndCatchFn(CodeGenModule &CGM) {
|
2009-12-02 15:41:41 +08:00
|
|
|
// void __cxa_end_catch();
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, "__cxa_end_catch");
|
2009-11-21 07:44:51 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
static llvm::Constant *getUnexpectedFn(CodeGenModule &CGM) {
|
2013-06-21 07:03:35 +08:00
|
|
|
// void __cxa_call_unexpected(void *thrown_exception);
|
2009-12-08 07:38:24 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, "__cxa_call_unexpected");
|
2009-12-08 07:38:24 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
static llvm::Constant *getTerminateFn(CodeGenModule &CGM) {
|
2009-12-02 15:41:41 +08:00
|
|
|
// void __terminate();
|
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, /*IsVarArgs=*/false);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef name;
|
2011-07-06 09:22:26 +08:00
|
|
|
|
|
|
|
// In C++, use std::terminate().
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
if (CGM.getLangOpts().CPlusPlus &&
|
|
|
|
CGM.getTarget().getCXXABI().isItaniumFamily()) {
|
|
|
|
name = "_ZSt9terminatev";
|
|
|
|
} else if (CGM.getLangOpts().ObjC1 &&
|
2013-02-12 11:51:38 +08:00
|
|
|
CGM.getLangOpts().ObjCRuntime.hasTerminate())
|
2011-07-06 09:22:26 +08:00
|
|
|
name = "objc_terminate";
|
|
|
|
else
|
|
|
|
name = "abort";
|
2013-02-12 11:51:38 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, name);
|
2010-05-17 21:49:20 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
static llvm::Constant *getCatchallRethrowFn(CodeGenModule &CGM,
|
2011-07-23 18:55:15 +08:00
|
|
|
StringRef Name) {
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *FTy =
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
|
2010-07-17 08:43:08 +08:00
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
return CGM.CreateRuntimeFunction(FTy, Name);
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
2012-02-08 20:41:24 +08:00
|
|
|
namespace {
|
|
|
|
/// The exceptions personality for a function.
|
|
|
|
struct EHPersonality {
|
|
|
|
const char *PersonalityFn;
|
|
|
|
|
|
|
|
// If this is non-null, this personality requires a non-standard
|
|
|
|
// function for rethrowing an exception after a catchall cleanup.
|
|
|
|
// This function must have prototype void(void*).
|
|
|
|
const char *CatchallRethrowFn;
|
|
|
|
|
2015-02-06 02:56:03 +08:00
|
|
|
static const EHPersonality &get(CodeGenModule &CGM,
|
|
|
|
const FunctionDecl *FD);
|
|
|
|
static const EHPersonality &get(CodeGenFunction &CGF) {
|
|
|
|
return get(CGF.CGM, dyn_cast_or_null<FunctionDecl>(CGF.CurCodeDecl));
|
|
|
|
}
|
|
|
|
|
2012-02-08 20:41:24 +08:00
|
|
|
static const EHPersonality GNU_C;
|
|
|
|
static const EHPersonality GNU_C_SJLJ;
|
2014-09-16 01:19:16 +08:00
|
|
|
static const EHPersonality GNU_C_SEH;
|
2012-02-08 20:41:24 +08:00
|
|
|
static const EHPersonality GNU_ObjC;
|
2013-01-11 23:33:01 +08:00
|
|
|
static const EHPersonality GNUstep_ObjC;
|
2012-02-08 20:41:24 +08:00
|
|
|
static const EHPersonality GNU_ObjCXX;
|
|
|
|
static const EHPersonality NeXT_ObjC;
|
|
|
|
static const EHPersonality GNU_CPlusPlus;
|
|
|
|
static const EHPersonality GNU_CPlusPlus_SJLJ;
|
2014-09-16 01:19:16 +08:00
|
|
|
static const EHPersonality GNU_CPlusPlus_SEH;
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
static const EHPersonality MSVC_except_handler;
|
|
|
|
static const EHPersonality MSVC_C_specific_handler;
|
2015-02-06 02:56:03 +08:00
|
|
|
static const EHPersonality MSVC_CxxFrameHandler3;
|
2012-02-08 20:41:24 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
const EHPersonality EHPersonality::GNU_C = { "__gcc_personality_v0", nullptr };
|
2012-02-08 20:41:24 +08:00
|
|
|
const EHPersonality
|
2014-05-21 13:09:00 +08:00
|
|
|
EHPersonality::GNU_C_SJLJ = { "__gcc_personality_sj0", nullptr };
|
|
|
|
const EHPersonality
|
2014-09-16 01:19:16 +08:00
|
|
|
EHPersonality::GNU_C_SEH = { "__gcc_personality_seh0", nullptr };
|
|
|
|
const EHPersonality
|
2014-05-21 13:09:00 +08:00
|
|
|
EHPersonality::NeXT_ObjC = { "__objc_personality_v0", nullptr };
|
|
|
|
const EHPersonality
|
|
|
|
EHPersonality::GNU_CPlusPlus = { "__gxx_personality_v0", nullptr };
|
|
|
|
const EHPersonality
|
|
|
|
EHPersonality::GNU_CPlusPlus_SJLJ = { "__gxx_personality_sj0", nullptr };
|
2012-02-08 20:41:24 +08:00
|
|
|
const EHPersonality
|
2014-09-16 01:19:16 +08:00
|
|
|
EHPersonality::GNU_CPlusPlus_SEH = { "__gxx_personality_seh0", nullptr };
|
|
|
|
const EHPersonality
|
2012-02-08 20:41:24 +08:00
|
|
|
EHPersonality::GNU_ObjC = {"__gnu_objc_personality_v0", "objc_exception_throw"};
|
|
|
|
const EHPersonality
|
2014-05-21 13:09:00 +08:00
|
|
|
EHPersonality::GNU_ObjCXX = { "__gnustep_objcxx_personality_v0", nullptr };
|
2013-01-11 23:33:01 +08:00
|
|
|
const EHPersonality
|
2014-05-21 13:09:00 +08:00
|
|
|
EHPersonality::GNUstep_ObjC = { "__gnustep_objc_personality_v0", nullptr };
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
const EHPersonality
|
|
|
|
EHPersonality::MSVC_except_handler = { "_except_handler3", nullptr };
|
|
|
|
const EHPersonality
|
|
|
|
EHPersonality::MSVC_C_specific_handler = { "__C_specific_handler", nullptr };
|
2015-02-06 02:56:03 +08:00
|
|
|
const EHPersonality
|
|
|
|
EHPersonality::MSVC_CxxFrameHandler3 = { "__CxxFrameHandler3", nullptr };
|
2010-07-17 08:43:08 +08:00
|
|
|
|
2014-11-14 10:01:10 +08:00
|
|
|
/// On Win64, use libgcc's SEH personality function. We fall back to dwarf on
|
|
|
|
/// other platforms, unless the user asked for SjLj exceptions.
|
|
|
|
static bool useLibGCCSEHPersonality(const llvm::Triple &T) {
|
|
|
|
return T.isOSWindows() && T.getArch() == llvm::Triple::x86_64;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const EHPersonality &getCPersonality(const llvm::Triple &T,
|
|
|
|
const LangOptions &L) {
|
2010-11-07 10:35:25 +08:00
|
|
|
if (L.SjLjExceptions)
|
|
|
|
return EHPersonality::GNU_C_SJLJ;
|
2014-11-14 10:01:10 +08:00
|
|
|
else if (useLibGCCSEHPersonality(T))
|
2014-09-16 01:19:16 +08:00
|
|
|
return EHPersonality::GNU_C_SEH;
|
2010-07-17 08:43:08 +08:00
|
|
|
return EHPersonality::GNU_C;
|
|
|
|
}
|
|
|
|
|
2014-11-14 10:01:10 +08:00
|
|
|
static const EHPersonality &getObjCPersonality(const llvm::Triple &T,
|
|
|
|
const LangOptions &L) {
|
2012-06-20 14:18:46 +08:00
|
|
|
switch (L.ObjCRuntime.getKind()) {
|
|
|
|
case ObjCRuntime::FragileMacOSX:
|
2014-11-14 10:01:10 +08:00
|
|
|
return getCPersonality(T, L);
|
2012-06-20 14:18:46 +08:00
|
|
|
case ObjCRuntime::MacOSX:
|
|
|
|
case ObjCRuntime::iOS:
|
|
|
|
return EHPersonality::NeXT_ObjC;
|
2012-07-04 04:49:52 +08:00
|
|
|
case ObjCRuntime::GNUstep:
|
2013-01-11 23:33:01 +08:00
|
|
|
if (L.ObjCRuntime.getVersion() >= VersionTuple(1, 7))
|
|
|
|
return EHPersonality::GNUstep_ObjC;
|
|
|
|
// fallthrough
|
2012-07-04 04:49:52 +08:00
|
|
|
case ObjCRuntime::GCC:
|
2012-07-12 10:07:58 +08:00
|
|
|
case ObjCRuntime::ObjFW:
|
2010-07-17 08:43:08 +08:00
|
|
|
return EHPersonality::GNU_ObjC;
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
2012-06-20 14:18:46 +08:00
|
|
|
llvm_unreachable("bad runtime kind");
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
2014-11-14 10:01:10 +08:00
|
|
|
static const EHPersonality &getCXXPersonality(const llvm::Triple &T,
|
|
|
|
const LangOptions &L) {
|
2010-07-17 08:43:08 +08:00
|
|
|
if (L.SjLjExceptions)
|
|
|
|
return EHPersonality::GNU_CPlusPlus_SJLJ;
|
2014-11-14 10:01:10 +08:00
|
|
|
else if (useLibGCCSEHPersonality(T))
|
2014-09-16 01:19:16 +08:00
|
|
|
return EHPersonality::GNU_CPlusPlus_SEH;
|
2014-11-14 10:01:10 +08:00
|
|
|
return EHPersonality::GNU_CPlusPlus;
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Determines the personality function to use when both C++
|
|
|
|
/// and Objective-C exceptions are being caught.
|
2014-11-14 10:01:10 +08:00
|
|
|
static const EHPersonality &getObjCXXPersonality(const llvm::Triple &T,
|
|
|
|
const LangOptions &L) {
|
2012-06-20 14:18:46 +08:00
|
|
|
switch (L.ObjCRuntime.getKind()) {
|
2010-07-06 09:34:17 +08:00
|
|
|
// The ObjC personality defers to the C++ personality for non-ObjC
|
|
|
|
// handlers. Unlike the C++ case, we use the same personality
|
|
|
|
// function on targets using (backend-driven) SJLJ EH.
|
2012-06-20 14:18:46 +08:00
|
|
|
case ObjCRuntime::MacOSX:
|
|
|
|
case ObjCRuntime::iOS:
|
|
|
|
return EHPersonality::NeXT_ObjC;
|
|
|
|
|
|
|
|
// In the fragile ABI, just use C++ exception handling and hope
|
|
|
|
// they're not doing crazy exception mixing.
|
|
|
|
case ObjCRuntime::FragileMacOSX:
|
2014-11-14 10:01:10 +08:00
|
|
|
return getCXXPersonality(T, L);
|
2010-05-17 21:49:20 +08:00
|
|
|
|
2012-07-04 04:49:52 +08:00
|
|
|
// The GCC runtime's personality function inherently doesn't support
|
2010-07-17 08:43:08 +08:00
|
|
|
// mixed EH. Use the C++ personality just to avoid returning null.
|
2012-07-04 04:49:52 +08:00
|
|
|
case ObjCRuntime::GCC:
|
2012-07-12 10:07:58 +08:00
|
|
|
case ObjCRuntime::ObjFW: // XXX: this will change soon
|
2012-07-04 04:49:52 +08:00
|
|
|
return EHPersonality::GNU_ObjC;
|
|
|
|
case ObjCRuntime::GNUstep:
|
2012-06-20 14:18:46 +08:00
|
|
|
return EHPersonality::GNU_ObjCXX;
|
|
|
|
}
|
|
|
|
llvm_unreachable("bad runtime kind");
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
2015-02-06 02:56:03 +08:00
|
|
|
static const EHPersonality &getSEHPersonalityMSVC(const llvm::Triple &T) {
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
if (T.getArch() == llvm::Triple::x86)
|
|
|
|
return EHPersonality::MSVC_except_handler;
|
|
|
|
return EHPersonality::MSVC_C_specific_handler;
|
|
|
|
}
|
|
|
|
|
2015-02-06 02:56:03 +08:00
|
|
|
const EHPersonality &EHPersonality::get(CodeGenModule &CGM,
|
|
|
|
const FunctionDecl *FD) {
|
2014-11-14 10:01:10 +08:00
|
|
|
const llvm::Triple &T = CGM.getTarget().getTriple();
|
|
|
|
const LangOptions &L = CGM.getLangOpts();
|
2015-02-06 02:56:03 +08:00
|
|
|
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
// Try to pick a personality function that is compatible with MSVC if we're
|
|
|
|
// not compiling Obj-C. Obj-C users better have an Obj-C runtime that supports
|
|
|
|
// the GCC-style personality function.
|
|
|
|
if (T.isWindowsMSVCEnvironment() && !L.ObjC1) {
|
2015-02-06 02:56:03 +08:00
|
|
|
if (L.SjLjExceptions)
|
|
|
|
return EHPersonality::GNU_CPlusPlus_SJLJ;
|
|
|
|
else if (FD && FD->usesSEHTry())
|
|
|
|
return getSEHPersonalityMSVC(T);
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
else
|
2015-02-06 02:56:03 +08:00
|
|
|
return EHPersonality::MSVC_CxxFrameHandler3;
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
}
|
|
|
|
|
2010-07-17 08:43:08 +08:00
|
|
|
if (L.CPlusPlus && L.ObjC1)
|
2014-11-14 10:01:10 +08:00
|
|
|
return getObjCXXPersonality(T, L);
|
2010-07-17 08:43:08 +08:00
|
|
|
else if (L.CPlusPlus)
|
2014-11-14 10:01:10 +08:00
|
|
|
return getCXXPersonality(T, L);
|
2010-07-17 08:43:08 +08:00
|
|
|
else if (L.ObjC1)
|
2014-11-14 10:01:10 +08:00
|
|
|
return getObjCPersonality(T, L);
|
2010-07-06 09:34:17 +08:00
|
|
|
else
|
2014-11-14 10:01:10 +08:00
|
|
|
return getCPersonality(T, L);
|
2010-07-17 08:43:08 +08:00
|
|
|
}
|
|
|
|
|
2010-09-16 14:16:50 +08:00
|
|
|
static llvm::Constant *getPersonalityFn(CodeGenModule &CGM,
|
2010-07-17 08:43:08 +08:00
|
|
|
const EHPersonality &Personality) {
|
|
|
|
llvm::Constant *Fn =
|
2012-02-07 08:39:47 +08:00
|
|
|
CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty, true),
|
2012-02-08 20:41:24 +08:00
|
|
|
Personality.PersonalityFn);
|
2010-09-16 14:16:50 +08:00
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static llvm::Constant *getOpaquePersonalityFn(CodeGenModule &CGM,
|
|
|
|
const EHPersonality &Personality) {
|
|
|
|
llvm::Constant *Fn = getPersonalityFn(CGM, Personality);
|
2011-02-08 16:22:06 +08:00
|
|
|
return llvm::ConstantExpr::getBitCast(Fn, CGM.Int8PtrTy);
|
2010-09-16 14:16:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether a personality function could reasonably be swapped
|
|
|
|
/// for a C++ personality function.
|
|
|
|
static bool PersonalityHasOnlyCXXUses(llvm::Constant *Fn) {
|
2014-03-09 11:16:50 +08:00
|
|
|
for (llvm::User *U : Fn->users()) {
|
2010-09-16 14:16:50 +08:00
|
|
|
// Conditionally white-list bitcasts.
|
2014-03-09 11:16:50 +08:00
|
|
|
if (llvm::ConstantExpr *CE = dyn_cast<llvm::ConstantExpr>(U)) {
|
2010-09-16 14:16:50 +08:00
|
|
|
if (CE->getOpcode() != llvm::Instruction::BitCast) return false;
|
|
|
|
if (!PersonalityHasOnlyCXXUses(CE))
|
|
|
|
return false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-09-20 06:08:36 +08:00
|
|
|
// Otherwise, it has to be a landingpad instruction.
|
2014-03-09 11:16:50 +08:00
|
|
|
llvm::LandingPadInst *LPI = dyn_cast<llvm::LandingPadInst>(U);
|
2011-09-20 06:08:36 +08:00
|
|
|
if (!LPI) return false;
|
2010-09-16 14:16:50 +08:00
|
|
|
|
2011-09-20 06:08:36 +08:00
|
|
|
for (unsigned I = 0, E = LPI->getNumClauses(); I != E; ++I) {
|
2010-09-16 14:16:50 +08:00
|
|
|
// Look for something that would've been returned by the ObjC
|
|
|
|
// runtime's GetEHType() method.
|
2011-09-20 06:08:36 +08:00
|
|
|
llvm::Value *Val = LPI->getClause(I)->stripPointerCasts();
|
|
|
|
if (LPI->isCatch(I)) {
|
|
|
|
// Check if the catch value has the ObjC prefix.
|
2011-09-20 08:40:19 +08:00
|
|
|
if (llvm::GlobalVariable *GV = dyn_cast<llvm::GlobalVariable>(Val))
|
|
|
|
// ObjC EH selector entries are always global variables with
|
|
|
|
// names starting like this.
|
|
|
|
if (GV->getName().startswith("OBJC_EHTYPE"))
|
|
|
|
return false;
|
2011-09-20 06:08:36 +08:00
|
|
|
} else {
|
|
|
|
// Check if any of the filter values have the ObjC prefix.
|
|
|
|
llvm::Constant *CVal = cast<llvm::Constant>(Val);
|
|
|
|
for (llvm::User::op_iterator
|
|
|
|
II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) {
|
2011-09-20 08:40:19 +08:00
|
|
|
if (llvm::GlobalVariable *GV =
|
|
|
|
cast<llvm::GlobalVariable>((*II)->stripPointerCasts()))
|
|
|
|
// ObjC EH selector entries are always global variables with
|
|
|
|
// names starting like this.
|
|
|
|
if (GV->getName().startswith("OBJC_EHTYPE"))
|
|
|
|
return false;
|
2011-09-20 06:08:36 +08:00
|
|
|
}
|
|
|
|
}
|
2010-09-16 14:16:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to use the C++ personality function in ObjC++. Not doing this
|
|
|
|
/// can cause some incompatibilities with gcc, which is more
|
|
|
|
/// aggressive about only using the ObjC++ personality in a function
|
|
|
|
/// when it really needs it.
|
|
|
|
void CodeGenModule::SimplifyPersonality() {
|
|
|
|
// If we're not in ObjC++ -fexceptions, there's nothing to do.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!LangOpts.CPlusPlus || !LangOpts.ObjC1 || !LangOpts.Exceptions)
|
2010-09-16 14:16:50 +08:00
|
|
|
return;
|
|
|
|
|
2012-11-15 01:48:31 +08:00
|
|
|
// Both the problem this endeavors to fix and the way the logic
|
|
|
|
// above works is specific to the NeXT runtime.
|
|
|
|
if (!LangOpts.ObjCRuntime.isNeXTFamily())
|
|
|
|
return;
|
|
|
|
|
2015-02-06 02:56:03 +08:00
|
|
|
const EHPersonality &ObjCXX = EHPersonality::get(*this, /*FD=*/nullptr);
|
2014-11-14 10:01:10 +08:00
|
|
|
const EHPersonality &CXX =
|
|
|
|
getCXXPersonality(getTarget().getTriple(), LangOpts);
|
2012-02-08 20:41:24 +08:00
|
|
|
if (&ObjCXX == &CXX)
|
2010-09-16 14:16:50 +08:00
|
|
|
return;
|
|
|
|
|
2012-02-08 20:41:24 +08:00
|
|
|
assert(std::strcmp(ObjCXX.PersonalityFn, CXX.PersonalityFn) != 0 &&
|
|
|
|
"Different EHPersonalities using the same personality function.");
|
|
|
|
|
|
|
|
llvm::Function *Fn = getModule().getFunction(ObjCXX.PersonalityFn);
|
2010-09-16 14:16:50 +08:00
|
|
|
|
|
|
|
// Nothing to do if it's unused.
|
|
|
|
if (!Fn || Fn->use_empty()) return;
|
|
|
|
|
|
|
|
// Can't do the optimization if it has non-C++ uses.
|
|
|
|
if (!PersonalityHasOnlyCXXUses(Fn)) return;
|
|
|
|
|
|
|
|
// Create the C++ personality function and kill off the old
|
|
|
|
// function.
|
|
|
|
llvm::Constant *CXXFn = getPersonalityFn(*this, CXX);
|
|
|
|
|
|
|
|
// This can happen if the user is screwing with us.
|
|
|
|
if (Fn->getType() != CXXFn->getType()) return;
|
|
|
|
|
|
|
|
Fn->replaceAllUsesWith(CXXFn);
|
|
|
|
Fn->eraseFromParent();
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the value to inject into a selector to indicate the
|
|
|
|
/// presence of a catch-all.
|
|
|
|
static llvm::Constant *getCatchAllValue(CodeGenFunction &CGF) {
|
|
|
|
// Possibly we should use @llvm.eh.catch.all.value here.
|
2011-02-08 16:22:06 +08:00
|
|
|
return llvm::ConstantPointerNull::get(CGF.Int8PtrTy);
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
2010-07-14 05:17:51 +08:00
|
|
|
namespace {
|
|
|
|
/// A cleanup to free the exception object if its initialization
|
|
|
|
/// throws.
|
2011-07-12 08:15:30 +08:00
|
|
|
struct FreeException : EHScopeStack::Cleanup {
|
|
|
|
llvm::Value *exn;
|
|
|
|
FreeException(llvm::Value *exn) : exn(exn) {}
|
2014-03-12 14:41:41 +08:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) override {
|
2013-03-01 03:01:20 +08:00
|
|
|
CGF.EmitNounwindRuntimeCall(getFreeExceptionFn(CGF.CGM), exn);
|
2010-07-14 05:17:51 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-04-22 09:10:34 +08:00
|
|
|
// Emits an exception expression into the given location. This
|
|
|
|
// differs from EmitAnyExprToMem only in that, if a final copy-ctor
|
|
|
|
// call is required, an exception within that copy ctor causes
|
|
|
|
// std::terminate to be invoked.
|
2011-01-28 16:37:24 +08:00
|
|
|
static void EmitAnyExprToExn(CodeGenFunction &CGF, const Expr *e,
|
|
|
|
llvm::Value *addr) {
|
2010-07-06 09:34:17 +08:00
|
|
|
// Make sure the exception object is cleaned up if there's an
|
|
|
|
// exception during initialization.
|
2011-01-28 16:37:24 +08:00
|
|
|
CGF.pushFullExprCleanup<FreeException>(EHCleanup, addr);
|
|
|
|
EHScopeStack::stable_iterator cleanup = CGF.EHStack.stable_begin();
|
2010-04-22 09:10:34 +08:00
|
|
|
|
|
|
|
// __cxa_allocate_exception returns a void*; we need to cast this
|
|
|
|
// to the appropriate type for the object.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *ty = CGF.ConvertTypeForMem(e->getType())->getPointerTo();
|
2011-01-28 16:37:24 +08:00
|
|
|
llvm::Value *typedAddr = CGF.Builder.CreateBitCast(addr, ty);
|
2010-04-22 09:10:34 +08:00
|
|
|
|
|
|
|
// FIXME: this isn't quite right! If there's a final unelided call
|
|
|
|
// to a copy constructor, then according to [except.terminate]p1 we
|
|
|
|
// must call std::terminate() if that constructor throws, because
|
|
|
|
// technically that copy occurs after the exception expression is
|
|
|
|
// evaluated but before the exception is caught. But the best way
|
|
|
|
// to handle that is to teach EmitAggExpr to do the final copy
|
|
|
|
// differently if it can't be elided.
|
2012-03-30 01:37:10 +08:00
|
|
|
CGF.EmitAnyExprToMem(e, typedAddr, e->getType().getQualifiers(),
|
|
|
|
/*IsInit*/ true);
|
2010-04-22 09:10:34 +08:00
|
|
|
|
2011-01-28 16:37:24 +08:00
|
|
|
// Deactivate the cleanup block.
|
2011-11-10 18:43:54 +08:00
|
|
|
CGF.DeactivateCleanupBlock(cleanup, cast<llvm::Instruction>(typedAddr));
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::getExceptionSlot() {
|
2011-05-29 05:13:02 +08:00
|
|
|
if (!ExceptionSlot)
|
|
|
|
ExceptionSlot = CreateTempAlloca(Int8PtrTy, "exn.slot");
|
2010-07-06 09:34:17 +08:00
|
|
|
return ExceptionSlot;
|
2009-12-01 11:41:18 +08:00
|
|
|
}
|
|
|
|
|
2011-05-29 05:13:02 +08:00
|
|
|
llvm::Value *CodeGenFunction::getEHSelectorSlot() {
|
|
|
|
if (!EHSelectorSlot)
|
|
|
|
EHSelectorSlot = CreateTempAlloca(Int32Ty, "ehselector.slot");
|
|
|
|
return EHSelectorSlot;
|
|
|
|
}
|
|
|
|
|
2011-09-16 02:57:19 +08:00
|
|
|
llvm::Value *CodeGenFunction::getExceptionFromSlot() {
|
|
|
|
return Builder.CreateLoad(getExceptionSlot(), "exn");
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::getSelectorFromSlot() {
|
|
|
|
return Builder.CreateLoad(getEHSelectorSlot(), "sel");
|
|
|
|
}
|
|
|
|
|
2015-02-05 06:37:07 +08:00
|
|
|
llvm::Value *CodeGenFunction::getAbnormalTerminationSlot() {
|
|
|
|
if (!AbnormalTerminationSlot)
|
2015-02-12 06:33:32 +08:00
|
|
|
AbnormalTerminationSlot =
|
|
|
|
CreateTempAlloca(Int8Ty, "abnormal.termination.slot");
|
2015-02-05 06:37:07 +08:00
|
|
|
return AbnormalTerminationSlot;
|
|
|
|
}
|
|
|
|
|
2013-05-08 05:53:22 +08:00
|
|
|
void CodeGenFunction::EmitCXXThrowExpr(const CXXThrowExpr *E,
|
|
|
|
bool KeepInsertionPoint) {
|
2009-10-30 10:27:02 +08:00
|
|
|
if (!E->getSubExpr()) {
|
2014-11-25 15:20:20 +08:00
|
|
|
CGM.getCXXABI().emitRethrow(*this, /*isNoReturn*/true);
|
2010-05-16 08:44:00 +08:00
|
|
|
|
2011-01-12 11:41:02 +08:00
|
|
|
// throw is an expression, and the expression emitters expect us
|
|
|
|
// to leave ourselves at a valid insertion point.
|
2013-05-08 05:53:22 +08:00
|
|
|
if (KeepInsertionPoint)
|
|
|
|
EmitBlock(createBasicBlock("throw.cont"));
|
2011-01-12 11:41:02 +08:00
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2014-11-25 15:20:20 +08:00
|
|
|
if (CGM.getTarget().getTriple().isKnownWindowsMSVCEnvironment()) {
|
|
|
|
ErrorUnsupported(E, "throw expression");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
QualType ThrowType = E->getSubExpr()->getType();
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2013-01-11 03:02:56 +08:00
|
|
|
if (ThrowType->isObjCObjectPointerType()) {
|
|
|
|
const Stmt *ThrowStmt = E->getSubExpr();
|
|
|
|
const ObjCAtThrowStmt S(E->getExprLoc(),
|
|
|
|
const_cast<Stmt *>(ThrowStmt));
|
|
|
|
CGM.getObjCRuntime().EmitThrowStmt(*this, S, false);
|
|
|
|
// This will clear insertion point which was not cleared in
|
|
|
|
// call to EmitThrowStmt.
|
2013-05-08 05:53:22 +08:00
|
|
|
if (KeepInsertionPoint)
|
|
|
|
EmitBlock(createBasicBlock("throw.cont"));
|
2013-01-11 03:02:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
// Now allocate the exception object.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *SizeTy = ConvertType(getContext().getSizeType());
|
2010-04-21 18:05:39 +08:00
|
|
|
uint64_t TypeSize = getContext().getTypeSizeInChars(ThrowType).getQuantity();
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2013-02-12 11:51:38 +08:00
|
|
|
llvm::Constant *AllocExceptionFn = getAllocateExceptionFn(CGM);
|
2010-07-06 09:34:17 +08:00
|
|
|
llvm::CallInst *ExceptionPtr =
|
2013-03-01 03:01:20 +08:00
|
|
|
EmitNounwindRuntimeCall(AllocExceptionFn,
|
|
|
|
llvm::ConstantInt::get(SizeTy, TypeSize),
|
|
|
|
"exception");
|
2009-12-11 08:32:37 +08:00
|
|
|
|
2010-04-22 09:10:34 +08:00
|
|
|
EmitAnyExprToExn(*this, E->getSubExpr(), ExceptionPtr);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2009-10-30 10:27:02 +08:00
|
|
|
// Now throw the exception.
|
2011-01-24 09:59:49 +08:00
|
|
|
llvm::Constant *TypeInfo = CGM.GetAddrOfRTTIDescriptor(ThrowType,
|
|
|
|
/*ForEH=*/true);
|
2010-04-22 09:10:34 +08:00
|
|
|
|
|
|
|
// The address of the destructor. If the exception type has a
|
|
|
|
// trivial destructor (or isn't a record), we just pass null.
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Constant *Dtor = nullptr;
|
2010-04-22 09:10:34 +08:00
|
|
|
if (const RecordType *RecordTy = ThrowType->getAs<RecordType>()) {
|
|
|
|
CXXRecordDecl *Record = cast<CXXRecordDecl>(RecordTy->getDecl());
|
|
|
|
if (!Record->hasTrivialDestructor()) {
|
2010-07-01 22:13:13 +08:00
|
|
|
CXXDestructorDecl *DtorD = Record->getDestructor();
|
2014-09-11 23:42:06 +08:00
|
|
|
Dtor = CGM.getAddrOfCXXStructor(DtorD, StructorType::Complete);
|
2010-04-22 09:10:34 +08:00
|
|
|
Dtor = llvm::ConstantExpr::getBitCast(Dtor, Int8PtrTy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!Dtor) Dtor = llvm::Constant::getNullValue(Int8PtrTy);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2013-03-01 03:01:20 +08:00
|
|
|
llvm::Value *args[] = { ExceptionPtr, TypeInfo, Dtor };
|
|
|
|
EmitNoreturnRuntimeCallOrInvoke(getThrowFn(CGM), args);
|
2009-12-10 08:06:18 +08:00
|
|
|
|
2011-01-12 11:41:02 +08:00
|
|
|
// throw is an expression, and the expression emitters expect us
|
|
|
|
// to leave ourselves at a valid insertion point.
|
2013-05-08 05:53:22 +08:00
|
|
|
if (KeepInsertionPoint)
|
|
|
|
EmitBlock(createBasicBlock("throw.cont"));
|
2009-10-30 09:42:31 +08:00
|
|
|
}
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2009-12-08 07:38:24 +08:00
|
|
|
void CodeGenFunction::EmitStartEHSpec(const Decl *D) {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!CGM.getLangOpts().CXXExceptions)
|
2010-02-07 07:59:05 +08:00
|
|
|
return;
|
|
|
|
|
2009-12-08 07:38:24 +08:00
|
|
|
const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!FD) {
|
2014-05-06 18:08:46 +08:00
|
|
|
// Check if CapturedDecl is nothrow and create terminate scope for it.
|
|
|
|
if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) {
|
|
|
|
if (CD->isNothrow())
|
|
|
|
EHStack.pushTerminate();
|
|
|
|
}
|
2009-12-08 07:38:24 +08:00
|
|
|
return;
|
2014-05-06 18:08:46 +08:00
|
|
|
}
|
2009-12-08 07:38:24 +08:00
|
|
|
const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!Proto)
|
2009-12-08 07:38:24 +08:00
|
|
|
return;
|
|
|
|
|
2011-03-16 02:42:48 +08:00
|
|
|
ExceptionSpecificationType EST = Proto->getExceptionSpecType();
|
|
|
|
if (isNoexceptExceptionSpec(EST)) {
|
|
|
|
if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
|
|
|
|
// noexcept functions are simple terminate scopes.
|
|
|
|
EHStack.pushTerminate();
|
|
|
|
}
|
|
|
|
} else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
|
|
|
|
unsigned NumExceptions = Proto->getNumExceptions();
|
|
|
|
EHFilterScope *Filter = EHStack.pushFilter(NumExceptions);
|
|
|
|
|
|
|
|
for (unsigned I = 0; I != NumExceptions; ++I) {
|
|
|
|
QualType Ty = Proto->getExceptionType(I);
|
|
|
|
QualType ExceptType = Ty.getNonReferenceType().getUnqualifiedType();
|
|
|
|
llvm::Value *EHType = CGM.GetAddrOfRTTIDescriptor(ExceptType,
|
|
|
|
/*ForEH=*/true);
|
|
|
|
Filter->setFilter(I, EHType);
|
|
|
|
}
|
2009-12-08 07:38:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
/// Emit the dispatch block for a filter scope if necessary.
|
|
|
|
static void emitFilterDispatchBlock(CodeGenFunction &CGF,
|
|
|
|
EHFilterScope &filterScope) {
|
|
|
|
llvm::BasicBlock *dispatchBlock = filterScope.getCachedEHDispatchBlock();
|
|
|
|
if (!dispatchBlock) return;
|
|
|
|
if (dispatchBlock->use_empty()) {
|
|
|
|
delete dispatchBlock;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGF.EmitBlockAfterUses(dispatchBlock);
|
|
|
|
|
|
|
|
// If this isn't a catch-all filter, we need to check whether we got
|
|
|
|
// here because the filter triggered.
|
|
|
|
if (filterScope.getNumFilters()) {
|
|
|
|
// Load the selector value.
|
2011-09-16 02:57:19 +08:00
|
|
|
llvm::Value *selector = CGF.getSelectorFromSlot();
|
2011-08-11 10:22:43 +08:00
|
|
|
llvm::BasicBlock *unexpectedBB = CGF.createBasicBlock("ehspec.unexpected");
|
|
|
|
|
|
|
|
llvm::Value *zero = CGF.Builder.getInt32(0);
|
|
|
|
llvm::Value *failsFilter =
|
2015-02-12 06:33:32 +08:00
|
|
|
CGF.Builder.CreateICmpSLT(selector, zero, "ehspec.fails");
|
|
|
|
CGF.Builder.CreateCondBr(failsFilter, unexpectedBB,
|
|
|
|
CGF.getEHResumeBlock(false));
|
2011-08-11 10:22:43 +08:00
|
|
|
|
|
|
|
CGF.EmitBlock(unexpectedBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call __cxa_call_unexpected. This doesn't need to be an invoke
|
|
|
|
// because __cxa_call_unexpected magically filters exceptions
|
|
|
|
// according to the last landing pad the exception was thrown
|
|
|
|
// into. Seriously.
|
2011-09-16 02:57:19 +08:00
|
|
|
llvm::Value *exn = CGF.getExceptionFromSlot();
|
2013-03-01 03:01:20 +08:00
|
|
|
CGF.EmitRuntimeCall(getUnexpectedFn(CGF.CGM), exn)
|
2011-08-11 10:22:43 +08:00
|
|
|
->setDoesNotReturn();
|
|
|
|
CGF.Builder.CreateUnreachable();
|
|
|
|
}
|
|
|
|
|
2009-12-08 07:38:24 +08:00
|
|
|
void CodeGenFunction::EmitEndEHSpec(const Decl *D) {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (!CGM.getLangOpts().CXXExceptions)
|
2010-02-07 07:59:05 +08:00
|
|
|
return;
|
|
|
|
|
2009-12-08 07:38:24 +08:00
|
|
|
const FunctionDecl* FD = dyn_cast_or_null<FunctionDecl>(D);
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!FD) {
|
2014-05-06 18:08:46 +08:00
|
|
|
// Check if CapturedDecl is nothrow and pop terminate scope for it.
|
|
|
|
if (const CapturedDecl* CD = dyn_cast_or_null<CapturedDecl>(D)) {
|
|
|
|
if (CD->isNothrow())
|
|
|
|
EHStack.popTerminate();
|
|
|
|
}
|
2009-12-08 07:38:24 +08:00
|
|
|
return;
|
2014-05-06 18:08:46 +08:00
|
|
|
}
|
2009-12-08 07:38:24 +08:00
|
|
|
const FunctionProtoType *Proto = FD->getType()->getAs<FunctionProtoType>();
|
2014-05-21 13:09:00 +08:00
|
|
|
if (!Proto)
|
2009-12-08 07:38:24 +08:00
|
|
|
return;
|
|
|
|
|
2011-03-16 02:42:48 +08:00
|
|
|
ExceptionSpecificationType EST = Proto->getExceptionSpecType();
|
|
|
|
if (isNoexceptExceptionSpec(EST)) {
|
|
|
|
if (Proto->getNoexceptSpec(getContext()) == FunctionProtoType::NR_Nothrow) {
|
|
|
|
EHStack.popTerminate();
|
|
|
|
}
|
|
|
|
} else if (EST == EST_Dynamic || EST == EST_DynamicNone) {
|
2011-08-11 10:22:43 +08:00
|
|
|
EHFilterScope &filterScope = cast<EHFilterScope>(*EHStack.begin());
|
|
|
|
emitFilterDispatchBlock(*this, filterScope);
|
2011-03-16 02:42:48 +08:00
|
|
|
EHStack.popFilter();
|
|
|
|
}
|
2009-12-08 07:38:24 +08:00
|
|
|
}
|
|
|
|
|
2009-11-21 07:44:51 +08:00
|
|
|
void CodeGenFunction::EmitCXXTryStmt(const CXXTryStmt &S) {
|
2014-11-18 06:11:07 +08:00
|
|
|
if (CGM.getTarget().getTriple().isKnownWindowsMSVCEnvironment()) {
|
2014-05-06 05:12:12 +08:00
|
|
|
ErrorUnsupported(&S, "try statement");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-07 14:56:46 +08:00
|
|
|
EnterCXXTryStmt(S);
|
2010-02-19 17:25:03 +08:00
|
|
|
EmitStmt(S.getTryBlock());
|
2010-07-07 14:56:46 +08:00
|
|
|
ExitCXXTryStmt(S);
|
2010-02-19 17:25:03 +08:00
|
|
|
}
|
|
|
|
|
2010-07-07 14:56:46 +08:00
|
|
|
void CodeGenFunction::EnterCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
|
2010-07-06 09:34:17 +08:00
|
|
|
unsigned NumHandlers = S.getNumHandlers();
|
|
|
|
EHCatchScope *CatchScope = EHStack.pushCatch(NumHandlers);
|
|
|
|
|
|
|
|
for (unsigned I = 0; I != NumHandlers; ++I) {
|
|
|
|
const CXXCatchStmt *C = S.getHandler(I);
|
|
|
|
|
|
|
|
llvm::BasicBlock *Handler = createBasicBlock("catch");
|
|
|
|
if (C->getExceptionDecl()) {
|
|
|
|
// FIXME: Dropping the reference type on the type into makes it
|
|
|
|
// impossible to correctly implement catch-by-reference
|
|
|
|
// semantics for pointers. Unfortunately, this is what all
|
|
|
|
// existing compilers do, and it's not clear that the standard
|
|
|
|
// personality routine is capable of doing this right. See C++ DR 388:
|
|
|
|
// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#388
|
2014-10-12 14:58:22 +08:00
|
|
|
Qualifiers CaughtTypeQuals;
|
|
|
|
QualType CaughtType = CGM.getContext().getUnqualifiedArrayType(
|
|
|
|
C->getCaughtType().getNonReferenceType(), CaughtTypeQuals);
|
2010-07-24 08:37:23 +08:00
|
|
|
|
2014-06-05 02:51:46 +08:00
|
|
|
llvm::Constant *TypeInfo = nullptr;
|
2010-07-24 08:37:23 +08:00
|
|
|
if (CaughtType->isObjCObjectPointerType())
|
2011-06-24 03:00:08 +08:00
|
|
|
TypeInfo = CGM.getObjCRuntime().GetEHType(CaughtType);
|
2010-07-24 08:37:23 +08:00
|
|
|
else
|
2011-01-24 09:59:49 +08:00
|
|
|
TypeInfo = CGM.GetAddrOfRTTIDescriptor(CaughtType, /*ForEH=*/true);
|
2010-07-06 09:34:17 +08:00
|
|
|
CatchScope->setHandler(I, TypeInfo, Handler);
|
|
|
|
} else {
|
|
|
|
// No exception decl indicates '...', a catch-all.
|
|
|
|
CatchScope->setCatchAllHandler(I, Handler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
llvm::BasicBlock *
|
|
|
|
CodeGenFunction::getEHDispatchBlock(EHScopeStack::stable_iterator si) {
|
|
|
|
// The dispatch block for the end of the scope chain is a block that
|
|
|
|
// just resumes unwinding.
|
|
|
|
if (si == EHStack.stable_end())
|
2012-11-08 00:50:40 +08:00
|
|
|
return getEHResumeBlock(true);
|
2011-08-11 10:22:43 +08:00
|
|
|
|
|
|
|
// Otherwise, we should look at the actual scope.
|
|
|
|
EHScope &scope = *EHStack.find(si);
|
|
|
|
|
|
|
|
llvm::BasicBlock *dispatchBlock = scope.getCachedEHDispatchBlock();
|
|
|
|
if (!dispatchBlock) {
|
|
|
|
switch (scope.getKind()) {
|
|
|
|
case EHScope::Catch: {
|
|
|
|
// Apply a special case to a single catch-all.
|
|
|
|
EHCatchScope &catchScope = cast<EHCatchScope>(scope);
|
|
|
|
if (catchScope.getNumHandlers() == 1 &&
|
|
|
|
catchScope.getHandler(0).isCatchAll()) {
|
|
|
|
dispatchBlock = catchScope.getHandler(0).Block;
|
|
|
|
|
|
|
|
// Otherwise, make a dispatch block.
|
|
|
|
} else {
|
|
|
|
dispatchBlock = createBasicBlock("catch.dispatch");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EHScope::Cleanup:
|
|
|
|
dispatchBlock = createBasicBlock("ehcleanup");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EHScope::Filter:
|
|
|
|
dispatchBlock = createBasicBlock("filter.dispatch");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EHScope::Terminate:
|
|
|
|
dispatchBlock = getTerminateHandler();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
scope.setCachedEHDispatchBlock(dispatchBlock);
|
|
|
|
}
|
|
|
|
return dispatchBlock;
|
|
|
|
}
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
/// Check whether this is a non-EH scope, i.e. a scope which doesn't
|
|
|
|
/// affect exception handling. Currently, the only non-EH scopes are
|
|
|
|
/// normal-only cleanup scopes.
|
|
|
|
static bool isNonEHScope(const EHScope &S) {
|
2010-07-14 04:32:21 +08:00
|
|
|
switch (S.getKind()) {
|
2010-07-21 15:22:38 +08:00
|
|
|
case EHScope::Cleanup:
|
|
|
|
return !cast<EHCleanupScope>(S).isEHCleanup();
|
2010-07-14 04:32:21 +08:00
|
|
|
case EHScope::Filter:
|
|
|
|
case EHScope::Catch:
|
|
|
|
case EHScope::Terminate:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-21 05:50:17 +08:00
|
|
|
llvm_unreachable("Invalid EHScope Kind!");
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
2010-02-19 17:25:03 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
llvm::BasicBlock *CodeGenFunction::getInvokeDestImpl() {
|
|
|
|
assert(EHStack.requiresLandingPad());
|
|
|
|
assert(!EHStack.empty());
|
2010-02-19 17:25:03 +08:00
|
|
|
|
2015-02-06 02:56:03 +08:00
|
|
|
// If exceptions are disabled, there are usually no landingpads. However, when
|
|
|
|
// SEH is enabled, functions using SEH still get landingpads.
|
|
|
|
const LangOptions &LO = CGM.getLangOpts();
|
|
|
|
if (!LO.Exceptions) {
|
|
|
|
if (!LO.Borland && !LO.MicrosoftExt)
|
|
|
|
return nullptr;
|
2015-02-11 08:00:21 +08:00
|
|
|
if (!currentFunctionUsesSEHTry())
|
2015-02-06 02:56:03 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2010-07-14 04:32:21 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// Check the innermost scope for a cached landing pad. If this is
|
|
|
|
// a non-EH cleanup, we'll check enclosing scopes in EmitLandingPad.
|
|
|
|
llvm::BasicBlock *LP = EHStack.begin()->getCachedLandingPad();
|
|
|
|
if (LP) return LP;
|
|
|
|
|
|
|
|
// Build the landing pad for this scope.
|
|
|
|
LP = EmitLandingPad();
|
|
|
|
assert(LP);
|
|
|
|
|
|
|
|
// Cache the landing pad on the innermost scope. If this is a
|
|
|
|
// non-EH scope, cache the landing pad on the enclosing scope, too.
|
|
|
|
for (EHScopeStack::iterator ir = EHStack.begin(); true; ++ir) {
|
|
|
|
ir->setCachedLandingPad(LP);
|
|
|
|
if (!isNonEHScope(*ir)) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return LP;
|
2010-02-19 17:25:03 +08:00
|
|
|
}
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
llvm::BasicBlock *CodeGenFunction::EmitLandingPad() {
|
|
|
|
assert(EHStack.requiresLandingPad());
|
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
EHScope &innermostEHScope = *EHStack.find(EHStack.getInnermostEHScope());
|
|
|
|
switch (innermostEHScope.getKind()) {
|
|
|
|
case EHScope::Terminate:
|
|
|
|
return getTerminateLandingPad();
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
case EHScope::Catch:
|
|
|
|
case EHScope::Cleanup:
|
|
|
|
case EHScope::Filter:
|
|
|
|
if (llvm::BasicBlock *lpad = innermostEHScope.getCachedLandingPad())
|
|
|
|
return lpad;
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the current IR generation state.
|
2011-08-11 10:22:43 +08:00
|
|
|
CGBuilderTy::InsertPoint savedIP = Builder.saveAndClearIP();
|
2015-02-04 04:00:54 +08:00
|
|
|
auto DL = ApplyDebugLocation::CreateDefaultArtificial(*this, CurEHLocation);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2015-02-06 02:56:03 +08:00
|
|
|
const EHPersonality &personality = EHPersonality::get(*this);
|
2010-07-17 08:43:08 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// Create and configure the landing pad.
|
2011-08-11 10:22:43 +08:00
|
|
|
llvm::BasicBlock *lpad = createBasicBlock("lpad");
|
|
|
|
EmitBlock(lpad);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-09-20 04:31:14 +08:00
|
|
|
llvm::LandingPadInst *LPadInst =
|
2014-12-02 06:02:27 +08:00
|
|
|
Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, nullptr),
|
2011-09-20 04:31:14 +08:00
|
|
|
getOpaquePersonalityFn(CGM, personality), 0);
|
|
|
|
|
|
|
|
llvm::Value *LPadExn = Builder.CreateExtractValue(LPadInst, 0);
|
|
|
|
Builder.CreateStore(LPadExn, getExceptionSlot());
|
|
|
|
llvm::Value *LPadSel = Builder.CreateExtractValue(LPadInst, 1);
|
|
|
|
Builder.CreateStore(LPadSel, getEHSelectorSlot());
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// Save the exception pointer. It's safe to use a single exception
|
|
|
|
// pointer per function because EH cleanups can never have nested
|
|
|
|
// try/catches.
|
2011-09-20 04:31:14 +08:00
|
|
|
// Build the landingpad instruction.
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Accumulate all the handlers in scope.
|
2011-08-11 10:22:43 +08:00
|
|
|
bool hasCatchAll = false;
|
|
|
|
bool hasCleanup = false;
|
|
|
|
bool hasFilter = false;
|
|
|
|
SmallVector<llvm::Value*, 4> filterTypes;
|
|
|
|
llvm::SmallPtrSet<llvm::Value*, 4> catchTypes;
|
2010-07-06 09:34:17 +08:00
|
|
|
for (EHScopeStack::iterator I = EHStack.begin(), E = EHStack.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
|
|
|
|
switch (I->getKind()) {
|
2010-07-21 15:22:38 +08:00
|
|
|
case EHScope::Cleanup:
|
2011-08-11 10:22:43 +08:00
|
|
|
// If we have a cleanup, remember that.
|
|
|
|
hasCleanup = (hasCleanup || cast<EHCleanupScope>(*I).isEHCleanup());
|
2010-07-14 04:32:21 +08:00
|
|
|
continue;
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
case EHScope::Filter: {
|
|
|
|
assert(I.next() == EHStack.end() && "EH filter is not end of EH stack");
|
2011-08-11 10:22:43 +08:00
|
|
|
assert(!hasCatchAll && "EH filter reached after catch-all");
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-09-20 04:31:14 +08:00
|
|
|
// Filter scopes get added to the landingpad in weird ways.
|
2011-08-11 10:22:43 +08:00
|
|
|
EHFilterScope &filter = cast<EHFilterScope>(*I);
|
|
|
|
hasFilter = true;
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-09-23 04:32:54 +08:00
|
|
|
// Add all the filter values.
|
|
|
|
for (unsigned i = 0, e = filter.getNumFilters(); i != e; ++i)
|
|
|
|
filterTypes.push_back(filter.getFilter(i));
|
2010-07-06 09:34:17 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
case EHScope::Terminate:
|
|
|
|
// Terminate scopes are basically catch-alls.
|
2011-08-11 10:22:43 +08:00
|
|
|
assert(!hasCatchAll);
|
|
|
|
hasCatchAll = true;
|
2010-07-06 09:34:17 +08:00
|
|
|
goto done;
|
|
|
|
|
|
|
|
case EHScope::Catch:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
EHCatchScope &catchScope = cast<EHCatchScope>(*I);
|
|
|
|
for (unsigned hi = 0, he = catchScope.getNumHandlers(); hi != he; ++hi) {
|
|
|
|
EHCatchScope::Handler handler = catchScope.getHandler(hi);
|
|
|
|
|
|
|
|
// If this is a catch-all, register that and abort.
|
|
|
|
if (!handler.Type) {
|
|
|
|
assert(!hasCatchAll);
|
|
|
|
hasCatchAll = true;
|
|
|
|
goto done;
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether we already have a handler for this type.
|
2014-11-19 15:49:47 +08:00
|
|
|
if (catchTypes.insert(handler.Type).second)
|
2011-09-20 04:31:14 +08:00
|
|
|
// If not, add it directly to the landingpad.
|
|
|
|
LPadInst->addClause(handler.Type);
|
2009-11-21 07:44:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
done:
|
2011-09-20 04:31:14 +08:00
|
|
|
// If we have a catch-all, add null to the landingpad.
|
2011-08-11 10:22:43 +08:00
|
|
|
assert(!(hasCatchAll && hasFilter));
|
|
|
|
if (hasCatchAll) {
|
2011-09-20 04:31:14 +08:00
|
|
|
LPadInst->addClause(getCatchAllValue(*this));
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// If we have an EH filter, we need to add those handlers in the
|
2011-09-20 04:31:14 +08:00
|
|
|
// right place in the landingpad, which is to say, at the end.
|
2011-08-11 10:22:43 +08:00
|
|
|
} else if (hasFilter) {
|
2011-09-20 06:08:36 +08:00
|
|
|
// Create a filter expression: a constant array indicating which filter
|
|
|
|
// types there are. The personality routine only lands here if the filter
|
|
|
|
// doesn't match.
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<llvm::Constant*, 8> Filters;
|
2011-09-20 04:31:14 +08:00
|
|
|
llvm::ArrayType *AType =
|
|
|
|
llvm::ArrayType::get(!filterTypes.empty() ?
|
|
|
|
filterTypes[0]->getType() : Int8PtrTy,
|
|
|
|
filterTypes.size());
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = filterTypes.size(); i != e; ++i)
|
|
|
|
Filters.push_back(cast<llvm::Constant>(filterTypes[i]));
|
|
|
|
llvm::Constant *FilterArray = llvm::ConstantArray::get(AType, Filters);
|
|
|
|
LPadInst->addClause(FilterArray);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Also check whether we need a cleanup.
|
2011-09-20 04:31:14 +08:00
|
|
|
if (hasCleanup)
|
|
|
|
LPadInst->setCleanup(true);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Otherwise, signal that we at least have cleanups.
|
2014-07-01 19:47:10 +08:00
|
|
|
} else if (hasCleanup) {
|
|
|
|
LPadInst->setCleanup(true);
|
2009-12-01 11:41:18 +08:00
|
|
|
}
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2011-09-20 04:31:14 +08:00
|
|
|
assert((LPadInst->getNumClauses() > 0 || LPadInst->isCleanup()) &&
|
|
|
|
"landingpad instruction has no clauses!");
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Tell the backend how to generate the landing pad.
|
2011-08-11 10:22:43 +08:00
|
|
|
Builder.CreateBr(getEHDispatchBlock(EHStack.getInnermostEHScope()));
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Restore the old IR generation state.
|
2011-08-11 10:22:43 +08:00
|
|
|
Builder.restoreIP(savedIP);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
return lpad;
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
2009-12-01 11:41:18 +08:00
|
|
|
|
2010-07-14 06:12:14 +08:00
|
|
|
namespace {
|
|
|
|
/// A cleanup to call __cxa_end_catch. In many cases, the caught
|
|
|
|
/// exception type lets us state definitively that the thrown exception
|
|
|
|
/// type does not have a destructor. In particular:
|
|
|
|
/// - Catch-alls tell us nothing, so we have to conservatively
|
|
|
|
/// assume that the thrown exception might have a destructor.
|
|
|
|
/// - Catches by reference behave according to their base types.
|
|
|
|
/// - Catches of non-record types will only trigger for exceptions
|
|
|
|
/// of non-record types, which never have destructors.
|
|
|
|
/// - Catches of record types can trigger for arbitrary subclasses
|
|
|
|
/// of the caught type, so we have to assume the actual thrown
|
|
|
|
/// exception type might have a throwing destructor, even if the
|
|
|
|
/// caught type's destructor is trivial or nothrow.
|
2010-07-21 15:22:38 +08:00
|
|
|
struct CallEndCatch : EHScopeStack::Cleanup {
|
2010-07-14 06:12:14 +08:00
|
|
|
CallEndCatch(bool MightThrow) : MightThrow(MightThrow) {}
|
|
|
|
bool MightThrow;
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) override {
|
2010-07-14 06:12:14 +08:00
|
|
|
if (!MightThrow) {
|
2013-03-01 03:01:20 +08:00
|
|
|
CGF.EmitNounwindRuntimeCall(getEndCatchFn(CGF.CGM));
|
2010-07-14 06:12:14 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-01 03:01:20 +08:00
|
|
|
CGF.EmitRuntimeCallOrInvoke(getEndCatchFn(CGF.CGM));
|
2010-07-14 06:12:14 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
/// Emits a call to __cxa_begin_catch and enters a cleanup to call
|
|
|
|
/// __cxa_end_catch.
|
2010-07-14 06:12:14 +08:00
|
|
|
///
|
|
|
|
/// \param EndMightThrow - true if __cxa_end_catch might throw
|
|
|
|
static llvm::Value *CallBeginCatch(CodeGenFunction &CGF,
|
|
|
|
llvm::Value *Exn,
|
|
|
|
bool EndMightThrow) {
|
2013-03-01 03:01:20 +08:00
|
|
|
llvm::CallInst *call =
|
|
|
|
CGF.EmitNounwindRuntimeCall(getBeginCatchFn(CGF.CGM), Exn);
|
2009-12-01 11:41:18 +08:00
|
|
|
|
2010-07-21 15:22:38 +08:00
|
|
|
CGF.EHStack.pushCleanup<CallEndCatch>(NormalAndEHCleanup, EndMightThrow);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2013-03-01 03:01:20 +08:00
|
|
|
return call;
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A "special initializer" callback for initializing a catch
|
|
|
|
/// parameter during catch initialization.
|
|
|
|
static void InitCatchParam(CodeGenFunction &CGF,
|
|
|
|
const VarDecl &CatchParam,
|
2013-10-02 10:29:49 +08:00
|
|
|
llvm::Value *ParamAddr,
|
|
|
|
SourceLocation Loc) {
|
2010-07-06 09:34:17 +08:00
|
|
|
// Load the exception from where the landing pad saved it.
|
2011-09-16 02:57:19 +08:00
|
|
|
llvm::Value *Exn = CGF.getExceptionFromSlot();
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
CanQualType CatchType =
|
|
|
|
CGF.CGM.getContext().getCanonicalType(CatchParam.getType());
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *LLVMCatchTy = CGF.ConvertTypeForMem(CatchType);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// If we're catching by reference, we can just cast the object
|
|
|
|
// pointer to the appropriate pointer.
|
|
|
|
if (isa<ReferenceType>(CatchType)) {
|
2010-07-21 06:17:55 +08:00
|
|
|
QualType CaughtType = cast<ReferenceType>(CatchType)->getPointeeType();
|
|
|
|
bool EndCatchMightThrow = CaughtType->isRecordType();
|
2010-07-14 06:12:14 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// __cxa_begin_catch returns the adjusted object pointer.
|
2010-07-14 06:12:14 +08:00
|
|
|
llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, EndCatchMightThrow);
|
2010-07-21 06:17:55 +08:00
|
|
|
|
|
|
|
// We have no way to tell the personality function that we're
|
|
|
|
// catching by reference, so if we're catching a pointer,
|
|
|
|
// __cxa_begin_catch will actually return that pointer by value.
|
|
|
|
if (const PointerType *PT = dyn_cast<PointerType>(CaughtType)) {
|
|
|
|
QualType PointeeType = PT->getPointeeType();
|
|
|
|
|
|
|
|
// When catching by reference, generally we should just ignore
|
|
|
|
// this by-value pointer and use the exception object instead.
|
|
|
|
if (!PointeeType->isRecordType()) {
|
|
|
|
|
|
|
|
// Exn points to the struct _Unwind_Exception header, which
|
|
|
|
// we have to skip past in order to reach the exception data.
|
|
|
|
unsigned HeaderSize =
|
|
|
|
CGF.CGM.getTargetCodeGenInfo().getSizeOfUnwindException();
|
|
|
|
AdjustedExn = CGF.Builder.CreateConstGEP1_32(Exn, HeaderSize);
|
|
|
|
|
|
|
|
// However, if we're catching a pointer-to-record type that won't
|
|
|
|
// work, because the personality function might have adjusted
|
|
|
|
// the pointer. There's actually no way for us to fully satisfy
|
|
|
|
// the language/ABI contract here: we can't use Exn because it
|
|
|
|
// might have the wrong adjustment, but we can't use the by-value
|
|
|
|
// pointer because it's off by a level of abstraction.
|
|
|
|
//
|
|
|
|
// The current solution is to dump the adjusted pointer into an
|
|
|
|
// alloca, which breaks language semantics (because changing the
|
|
|
|
// pointer doesn't change the exception) but at least works.
|
|
|
|
// The better solution would be to filter out non-exact matches
|
|
|
|
// and rethrow them, but this is tricky because the rethrow
|
|
|
|
// really needs to be catchable by other sites at this landing
|
|
|
|
// pad. The best solution is to fix the personality function.
|
|
|
|
} else {
|
|
|
|
// Pull the pointer for the reference type off.
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *PtrTy =
|
2010-07-21 06:17:55 +08:00
|
|
|
cast<llvm::PointerType>(LLVMCatchTy)->getElementType();
|
|
|
|
|
|
|
|
// Create the temporary and write the adjusted pointer into it.
|
|
|
|
llvm::Value *ExnPtrTmp = CGF.CreateTempAlloca(PtrTy, "exn.byref.tmp");
|
|
|
|
llvm::Value *Casted = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
|
|
|
|
CGF.Builder.CreateStore(Casted, ExnPtrTmp);
|
|
|
|
|
|
|
|
// Bind the reference to the temporary.
|
|
|
|
AdjustedExn = ExnPtrTmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
llvm::Value *ExnCast =
|
|
|
|
CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.byref");
|
|
|
|
CGF.Builder.CreateStore(ExnCast, ParamAddr);
|
|
|
|
return;
|
2009-12-05 03:03:47 +08:00
|
|
|
}
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2013-03-08 05:37:08 +08:00
|
|
|
// Scalars and complexes.
|
|
|
|
TypeEvaluationKind TEK = CGF.getEvaluationKind(CatchType);
|
|
|
|
if (TEK != TEK_Aggregate) {
|
2010-07-14 06:12:14 +08:00
|
|
|
llvm::Value *AdjustedExn = CallBeginCatch(CGF, Exn, false);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// If the catch type is a pointer type, __cxa_begin_catch returns
|
|
|
|
// the pointer by value.
|
|
|
|
if (CatchType->hasPointerRepresentation()) {
|
|
|
|
llvm::Value *CastExn =
|
|
|
|
CGF.Builder.CreateBitCast(AdjustedExn, LLVMCatchTy, "exn.casted");
|
2012-01-18 04:16:56 +08:00
|
|
|
|
|
|
|
switch (CatchType.getQualifiers().getObjCLifetime()) {
|
|
|
|
case Qualifiers::OCL_Strong:
|
|
|
|
CastExn = CGF.EmitARCRetainNonBlock(CastExn);
|
|
|
|
// fallthrough
|
|
|
|
|
|
|
|
case Qualifiers::OCL_None:
|
|
|
|
case Qualifiers::OCL_ExplicitNone:
|
|
|
|
case Qualifiers::OCL_Autoreleasing:
|
|
|
|
CGF.Builder.CreateStore(CastExn, ParamAddr);
|
|
|
|
return;
|
|
|
|
|
|
|
|
case Qualifiers::OCL_Weak:
|
|
|
|
CGF.EmitARCInitWeak(ParamAddr, CastExn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm_unreachable("bad ownership qualifier!");
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// Otherwise, it returns a pointer into the exception object.
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
|
2010-07-06 09:34:17 +08:00
|
|
|
llvm::Value *Cast = CGF.Builder.CreateBitCast(AdjustedExn, PtrTy);
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2013-03-08 05:37:08 +08:00
|
|
|
LValue srcLV = CGF.MakeNaturalAlignAddrLValue(Cast, CatchType);
|
|
|
|
LValue destLV = CGF.MakeAddrLValue(ParamAddr, CatchType,
|
|
|
|
CGF.getContext().getDeclAlign(&CatchParam));
|
|
|
|
switch (TEK) {
|
|
|
|
case TEK_Complex:
|
2013-10-02 10:29:49 +08:00
|
|
|
CGF.EmitStoreOfComplex(CGF.EmitLoadOfComplex(srcLV, Loc), destLV,
|
2013-03-08 05:37:08 +08:00
|
|
|
/*init*/ true);
|
|
|
|
return;
|
|
|
|
case TEK_Scalar: {
|
2013-10-02 10:29:49 +08:00
|
|
|
llvm::Value *ExnLoad = CGF.EmitLoadOfScalar(srcLV, Loc);
|
2013-03-08 05:37:08 +08:00
|
|
|
CGF.EmitStoreOfScalar(ExnLoad, destLV, /*init*/ true);
|
|
|
|
return;
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
2013-03-08 05:37:08 +08:00
|
|
|
case TEK_Aggregate:
|
|
|
|
llvm_unreachable("evaluation kind filtered out!");
|
|
|
|
}
|
|
|
|
llvm_unreachable("bad evaluation kind");
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
2009-12-01 11:41:18 +08:00
|
|
|
|
2011-02-16 16:39:19 +08:00
|
|
|
assert(isa<RecordType>(CatchType) && "unexpected catch type!");
|
2009-12-05 03:21:57 +08:00
|
|
|
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::Type *PtrTy = LLVMCatchTy->getPointerTo(0); // addrspace 0 ok
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-02-16 16:39:19 +08:00
|
|
|
// Check for a copy expression. If we don't have a copy expression,
|
|
|
|
// that means a trivial copy is okay.
|
2011-02-16 16:02:54 +08:00
|
|
|
const Expr *copyExpr = CatchParam.getInit();
|
|
|
|
if (!copyExpr) {
|
2011-02-16 16:39:19 +08:00
|
|
|
llvm::Value *rawAdjustedExn = CallBeginCatch(CGF, Exn, true);
|
|
|
|
llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
|
2012-03-30 01:37:10 +08:00
|
|
|
CGF.EmitAggregateCopy(ParamAddr, adjustedExn, CatchType);
|
2010-07-06 09:34:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to call __cxa_get_exception_ptr to get the adjusted
|
|
|
|
// pointer before copying.
|
2011-02-16 16:02:54 +08:00
|
|
|
llvm::CallInst *rawAdjustedExn =
|
2013-03-01 03:01:20 +08:00
|
|
|
CGF.EmitNounwindRuntimeCall(getGetExceptionPtrFn(CGF.CGM), Exn);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-02-16 16:02:54 +08:00
|
|
|
// Cast that to the appropriate type.
|
|
|
|
llvm::Value *adjustedExn = CGF.Builder.CreateBitCast(rawAdjustedExn, PtrTy);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-02-16 16:02:54 +08:00
|
|
|
// The copy expression is defined in terms of an OpaqueValueExpr.
|
|
|
|
// Find it and map it to the adjusted expression.
|
|
|
|
CodeGenFunction::OpaqueValueMapping
|
2011-02-17 18:25:35 +08:00
|
|
|
opaque(CGF, OpaqueValueExpr::findInCopyConstruct(copyExpr),
|
|
|
|
CGF.MakeAddrLValue(adjustedExn, CatchParam.getType()));
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Call the copy ctor in a terminate scope.
|
|
|
|
CGF.EHStack.pushTerminate();
|
2011-02-16 16:02:54 +08:00
|
|
|
|
|
|
|
// Perform the copy construction.
|
2011-12-03 10:13:40 +08:00
|
|
|
CharUnits Alignment = CGF.getContext().getDeclAlign(&CatchParam);
|
2011-12-03 08:54:26 +08:00
|
|
|
CGF.EmitAggExpr(copyExpr,
|
|
|
|
AggValueSlot::forAddr(ParamAddr, Alignment, Qualifiers(),
|
|
|
|
AggValueSlot::IsNotDestructed,
|
|
|
|
AggValueSlot::DoesNotNeedGCBarriers,
|
2012-03-30 01:37:10 +08:00
|
|
|
AggValueSlot::IsNotAliased));
|
2011-02-16 16:02:54 +08:00
|
|
|
|
|
|
|
// Leave the terminate scope.
|
2010-07-06 09:34:17 +08:00
|
|
|
CGF.EHStack.popTerminate();
|
|
|
|
|
2011-02-16 16:02:54 +08:00
|
|
|
// Undo the opaque value mapping.
|
|
|
|
opaque.pop();
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// Finally we can call __cxa_begin_catch.
|
2010-07-14 06:12:14 +08:00
|
|
|
CallBeginCatch(CGF, Exn, true);
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Begins a catch statement by initializing the catch variable and
|
|
|
|
/// calling __cxa_begin_catch.
|
2011-02-16 16:02:54 +08:00
|
|
|
static void BeginCatch(CodeGenFunction &CGF, const CXXCatchStmt *S) {
|
2010-07-06 09:34:17 +08:00
|
|
|
// We have to be very careful with the ordering of cleanups here:
|
|
|
|
// C++ [except.throw]p4:
|
|
|
|
// The destruction [of the exception temporary] occurs
|
|
|
|
// immediately after the destruction of the object declared in
|
|
|
|
// the exception-declaration in the handler.
|
|
|
|
//
|
|
|
|
// So the precise ordering is:
|
|
|
|
// 1. Construct catch variable.
|
|
|
|
// 2. __cxa_begin_catch
|
|
|
|
// 3. Enter __cxa_end_catch cleanup
|
|
|
|
// 4. Enter dtor cleanup
|
|
|
|
//
|
2011-02-22 14:44:22 +08:00
|
|
|
// We do this by using a slightly abnormal initialization process.
|
|
|
|
// Delegation sequence:
|
2010-07-06 09:34:17 +08:00
|
|
|
// - ExitCXXTryStmt opens a RunCleanupsScope
|
2011-02-22 14:44:22 +08:00
|
|
|
// - EmitAutoVarAlloca creates the variable and debug info
|
2010-07-06 09:34:17 +08:00
|
|
|
// - InitCatchParam initializes the variable from the exception
|
2011-02-22 14:44:22 +08:00
|
|
|
// - CallBeginCatch calls __cxa_begin_catch
|
|
|
|
// - CallBeginCatch enters the __cxa_end_catch cleanup
|
|
|
|
// - EmitAutoVarCleanups enters the variable destructor cleanup
|
2010-07-06 09:34:17 +08:00
|
|
|
// - EmitCXXTryStmt emits the code for the catch body
|
|
|
|
// - EmitCXXTryStmt close the RunCleanupsScope
|
|
|
|
|
|
|
|
VarDecl *CatchParam = S->getExceptionDecl();
|
|
|
|
if (!CatchParam) {
|
2011-09-16 02:57:19 +08:00
|
|
|
llvm::Value *Exn = CGF.getExceptionFromSlot();
|
2010-07-14 06:12:14 +08:00
|
|
|
CallBeginCatch(CGF, Exn, true);
|
2010-07-06 09:34:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the local.
|
2011-02-22 14:44:22 +08:00
|
|
|
CodeGenFunction::AutoVarEmission var = CGF.EmitAutoVarAlloca(*CatchParam);
|
2013-10-02 10:29:49 +08:00
|
|
|
InitCatchParam(CGF, *CatchParam, var.getObjectAddress(CGF), S->getLocStart());
|
2011-02-22 14:44:22 +08:00
|
|
|
CGF.EmitAutoVarCleanups(var);
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
/// Emit the structure of the dispatch block for the given catch scope.
|
|
|
|
/// It is an invariant that the dispatch block already exists.
|
|
|
|
static void emitCatchDispatchBlock(CodeGenFunction &CGF,
|
|
|
|
EHCatchScope &catchScope) {
|
|
|
|
llvm::BasicBlock *dispatchBlock = catchScope.getCachedEHDispatchBlock();
|
|
|
|
assert(dispatchBlock);
|
|
|
|
|
|
|
|
// If there's only a single catch-all, getEHDispatchBlock returned
|
|
|
|
// that catch-all as the dispatch block.
|
|
|
|
if (catchScope.getNumHandlers() == 1 &&
|
|
|
|
catchScope.getHandler(0).isCatchAll()) {
|
|
|
|
assert(dispatchBlock == catchScope.getHandler(0).Block);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveIP();
|
|
|
|
CGF.EmitBlockAfterUses(dispatchBlock);
|
|
|
|
|
|
|
|
// Select the right handler.
|
|
|
|
llvm::Value *llvm_eh_typeid_for =
|
|
|
|
CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for);
|
|
|
|
|
|
|
|
// Load the selector value.
|
2011-09-16 02:57:19 +08:00
|
|
|
llvm::Value *selector = CGF.getSelectorFromSlot();
|
2011-08-11 10:22:43 +08:00
|
|
|
|
|
|
|
// Test against each of the exception types we claim to catch.
|
|
|
|
for (unsigned i = 0, e = catchScope.getNumHandlers(); ; ++i) {
|
|
|
|
assert(i < e && "ran off end of handlers!");
|
|
|
|
const EHCatchScope::Handler &handler = catchScope.getHandler(i);
|
|
|
|
|
|
|
|
llvm::Value *typeValue = handler.Type;
|
|
|
|
assert(typeValue && "fell into catch-all case!");
|
|
|
|
typeValue = CGF.Builder.CreateBitCast(typeValue, CGF.Int8PtrTy);
|
|
|
|
|
|
|
|
// Figure out the next block.
|
|
|
|
bool nextIsEnd;
|
|
|
|
llvm::BasicBlock *nextBlock;
|
|
|
|
|
|
|
|
// If this is the last handler, we're at the end, and the next
|
|
|
|
// block is the block for the enclosing EH scope.
|
|
|
|
if (i + 1 == e) {
|
|
|
|
nextBlock = CGF.getEHDispatchBlock(catchScope.getEnclosingEHScope());
|
|
|
|
nextIsEnd = true;
|
|
|
|
|
|
|
|
// If the next handler is a catch-all, we're at the end, and the
|
|
|
|
// next block is that handler.
|
|
|
|
} else if (catchScope.getHandler(i+1).isCatchAll()) {
|
|
|
|
nextBlock = catchScope.getHandler(i+1).Block;
|
|
|
|
nextIsEnd = true;
|
|
|
|
|
|
|
|
// Otherwise, we're not at the end and we need a new block.
|
|
|
|
} else {
|
|
|
|
nextBlock = CGF.createBasicBlock("catch.fallthrough");
|
|
|
|
nextIsEnd = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Figure out the catch type's index in the LSDA's type table.
|
|
|
|
llvm::CallInst *typeIndex =
|
|
|
|
CGF.Builder.CreateCall(llvm_eh_typeid_for, typeValue);
|
|
|
|
typeIndex->setDoesNotThrow();
|
|
|
|
|
|
|
|
llvm::Value *matchesTypeIndex =
|
|
|
|
CGF.Builder.CreateICmpEQ(selector, typeIndex, "matches");
|
|
|
|
CGF.Builder.CreateCondBr(matchesTypeIndex, handler.Block, nextBlock);
|
|
|
|
|
|
|
|
// If the next handler is a catch-all, we're completely done.
|
|
|
|
if (nextIsEnd) {
|
|
|
|
CGF.Builder.restoreIP(savedIP);
|
|
|
|
return;
|
|
|
|
}
|
2012-02-19 19:57:29 +08:00
|
|
|
// Otherwise we need to emit and continue at that block.
|
|
|
|
CGF.EmitBlock(nextBlock);
|
2011-08-11 10:22:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::popCatchScope() {
|
|
|
|
EHCatchScope &catchScope = cast<EHCatchScope>(*EHStack.begin());
|
|
|
|
if (catchScope.hasEHBranches())
|
|
|
|
emitCatchDispatchBlock(*this, catchScope);
|
|
|
|
EHStack.popCatch();
|
|
|
|
}
|
|
|
|
|
2010-07-07 14:56:46 +08:00
|
|
|
void CodeGenFunction::ExitCXXTryStmt(const CXXTryStmt &S, bool IsFnTryBlock) {
|
2010-07-06 09:34:17 +08:00
|
|
|
unsigned NumHandlers = S.getNumHandlers();
|
|
|
|
EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
|
|
|
|
assert(CatchScope.getNumHandlers() == NumHandlers);
|
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
// If the catch was not required, bail out now.
|
|
|
|
if (!CatchScope.hasEHBranches()) {
|
2014-01-09 17:22:32 +08:00
|
|
|
CatchScope.clearHandlerBlocks();
|
2011-08-11 10:22:43 +08:00
|
|
|
EHStack.popCatch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the structure of the EH dispatch for this catch.
|
|
|
|
emitCatchDispatchBlock(*this, CatchScope);
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// Copy the handler blocks off before we pop the EH stack. Emitting
|
|
|
|
// the handlers might scribble on this memory.
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<EHCatchScope::Handler, 8> Handlers(NumHandlers);
|
2010-07-06 09:34:17 +08:00
|
|
|
memcpy(Handlers.data(), CatchScope.begin(),
|
|
|
|
NumHandlers * sizeof(EHCatchScope::Handler));
|
2011-08-11 10:22:43 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
EHStack.popCatch();
|
|
|
|
|
|
|
|
// The fall-through block.
|
|
|
|
llvm::BasicBlock *ContBB = createBasicBlock("try.cont");
|
|
|
|
|
|
|
|
// We just emitted the body of the try; jump to the continue block.
|
|
|
|
if (HaveInsertPoint())
|
|
|
|
Builder.CreateBr(ContBB);
|
|
|
|
|
2012-06-15 13:27:05 +08:00
|
|
|
// Determine if we need an implicit rethrow for all these catch handlers;
|
|
|
|
// see the comment below.
|
|
|
|
bool doImplicitRethrow = false;
|
2010-07-07 14:56:46 +08:00
|
|
|
if (IsFnTryBlock)
|
2012-06-15 13:27:05 +08:00
|
|
|
doImplicitRethrow = isa<CXXDestructorDecl>(CurCodeDecl) ||
|
|
|
|
isa<CXXConstructorDecl>(CurCodeDecl);
|
2010-07-07 14:56:46 +08:00
|
|
|
|
2011-08-11 10:22:43 +08:00
|
|
|
// Perversely, we emit the handlers backwards precisely because we
|
|
|
|
// want them to appear in source order. In all of these cases, the
|
|
|
|
// catch block will have exactly one predecessor, which will be a
|
|
|
|
// particular block in the catch dispatch. However, in the case of
|
|
|
|
// a catch-all, one of the dispatch blocks will branch to two
|
|
|
|
// different handlers, and EmitBlockAfterUses will cause the second
|
|
|
|
// handler to be moved before the first.
|
|
|
|
for (unsigned I = NumHandlers; I != 0; --I) {
|
|
|
|
llvm::BasicBlock *CatchBlock = Handlers[I-1].Block;
|
|
|
|
EmitBlockAfterUses(CatchBlock);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Catch the exception if this isn't a catch-all.
|
2011-08-11 10:22:43 +08:00
|
|
|
const CXXCatchStmt *C = S.getHandler(I-1);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Enter a cleanup scope, including the catch variable and the
|
|
|
|
// end-catch.
|
|
|
|
RunCleanupsScope CatchScope(*this);
|
|
|
|
|
|
|
|
// Initialize the catch variable and set up the cleanups.
|
|
|
|
BeginCatch(*this, C);
|
|
|
|
|
2014-01-07 08:20:28 +08:00
|
|
|
// Emit the PGO counter increment.
|
2014-01-07 06:27:43 +08:00
|
|
|
RegionCounter CatchCnt = getPGORegionCounter(C);
|
|
|
|
CatchCnt.beginRegion(Builder);
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// Perform the body of the catch.
|
|
|
|
EmitStmt(C->getHandlerBlock());
|
|
|
|
|
2012-06-15 13:27:05 +08:00
|
|
|
// [except.handle]p11:
|
|
|
|
// The currently handled exception is rethrown if control
|
|
|
|
// reaches the end of a handler of the function-try-block of a
|
|
|
|
// constructor or destructor.
|
|
|
|
|
|
|
|
// It is important that we only do this on fallthrough and not on
|
|
|
|
// return. Note that it's illegal to put a return in a
|
|
|
|
// constructor function-try-block's catch handler (p14), so this
|
|
|
|
// really only applies to destructors.
|
|
|
|
if (doImplicitRethrow && HaveInsertPoint()) {
|
2014-11-25 15:20:20 +08:00
|
|
|
CGM.getCXXABI().emitRethrow(*this, /*isNoReturn*/false);
|
2012-06-15 13:27:05 +08:00
|
|
|
Builder.CreateUnreachable();
|
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
}
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// Fall out through the catch cleanups.
|
|
|
|
CatchScope.ForceCleanup();
|
|
|
|
|
|
|
|
// Branch out of the try.
|
|
|
|
if (HaveInsertPoint())
|
|
|
|
Builder.CreateBr(ContBB);
|
|
|
|
}
|
2009-11-21 07:44:51 +08:00
|
|
|
|
2014-01-07 06:27:43 +08:00
|
|
|
RegionCounter ContCnt = getPGORegionCounter(&S);
|
2010-07-06 09:34:17 +08:00
|
|
|
EmitBlock(ContBB);
|
2014-01-07 06:27:43 +08:00
|
|
|
ContCnt.beginRegion(Builder);
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
2010-07-21 08:52:03 +08:00
|
|
|
namespace {
|
2010-07-21 15:22:38 +08:00
|
|
|
struct CallEndCatchForFinally : EHScopeStack::Cleanup {
|
2010-07-21 08:52:03 +08:00
|
|
|
llvm::Value *ForEHVar;
|
|
|
|
llvm::Value *EndCatchFn;
|
|
|
|
CallEndCatchForFinally(llvm::Value *ForEHVar, llvm::Value *EndCatchFn)
|
|
|
|
: ForEHVar(ForEHVar), EndCatchFn(EndCatchFn) {}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) override {
|
2010-07-21 08:52:03 +08:00
|
|
|
llvm::BasicBlock *EndCatchBB = CGF.createBasicBlock("finally.endcatch");
|
|
|
|
llvm::BasicBlock *CleanupContBB =
|
|
|
|
CGF.createBasicBlock("finally.cleanup.cont");
|
|
|
|
|
|
|
|
llvm::Value *ShouldEndCatch =
|
|
|
|
CGF.Builder.CreateLoad(ForEHVar, "finally.endcatch");
|
|
|
|
CGF.Builder.CreateCondBr(ShouldEndCatch, EndCatchBB, CleanupContBB);
|
|
|
|
CGF.EmitBlock(EndCatchBB);
|
2013-03-01 03:01:20 +08:00
|
|
|
CGF.EmitRuntimeCallOrInvoke(EndCatchFn); // catch-all, so might throw
|
2010-07-21 08:52:03 +08:00
|
|
|
CGF.EmitBlock(CleanupContBB);
|
|
|
|
}
|
|
|
|
};
|
2010-07-21 13:47:49 +08:00
|
|
|
|
2010-07-21 15:22:38 +08:00
|
|
|
struct PerformFinally : EHScopeStack::Cleanup {
|
2010-07-21 13:47:49 +08:00
|
|
|
const Stmt *Body;
|
|
|
|
llvm::Value *ForEHVar;
|
|
|
|
llvm::Value *EndCatchFn;
|
|
|
|
llvm::Value *RethrowFn;
|
|
|
|
llvm::Value *SavedExnVar;
|
|
|
|
|
|
|
|
PerformFinally(const Stmt *Body, llvm::Value *ForEHVar,
|
|
|
|
llvm::Value *EndCatchFn,
|
|
|
|
llvm::Value *RethrowFn, llvm::Value *SavedExnVar)
|
|
|
|
: Body(Body), ForEHVar(ForEHVar), EndCatchFn(EndCatchFn),
|
|
|
|
RethrowFn(RethrowFn), SavedExnVar(SavedExnVar) {}
|
|
|
|
|
2014-03-12 14:41:41 +08:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags flags) override {
|
2010-07-21 13:47:49 +08:00
|
|
|
// Enter a cleanup to call the end-catch function if one was provided.
|
|
|
|
if (EndCatchFn)
|
2010-07-21 15:22:38 +08:00
|
|
|
CGF.EHStack.pushCleanup<CallEndCatchForFinally>(NormalAndEHCleanup,
|
|
|
|
ForEHVar, EndCatchFn);
|
2010-07-21 13:47:49 +08:00
|
|
|
|
2010-08-11 08:16:14 +08:00
|
|
|
// Save the current cleanup destination in case there are
|
|
|
|
// cleanups in the finally block.
|
|
|
|
llvm::Value *SavedCleanupDest =
|
|
|
|
CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot(),
|
|
|
|
"cleanup.dest.saved");
|
|
|
|
|
2010-07-21 13:47:49 +08:00
|
|
|
// Emit the finally block.
|
|
|
|
CGF.EmitStmt(Body);
|
|
|
|
|
|
|
|
// If the end of the finally is reachable, check whether this was
|
|
|
|
// for EH. If so, rethrow.
|
|
|
|
if (CGF.HaveInsertPoint()) {
|
|
|
|
llvm::BasicBlock *RethrowBB = CGF.createBasicBlock("finally.rethrow");
|
|
|
|
llvm::BasicBlock *ContBB = CGF.createBasicBlock("finally.cont");
|
|
|
|
|
|
|
|
llvm::Value *ShouldRethrow =
|
|
|
|
CGF.Builder.CreateLoad(ForEHVar, "finally.shouldthrow");
|
|
|
|
CGF.Builder.CreateCondBr(ShouldRethrow, RethrowBB, ContBB);
|
|
|
|
|
|
|
|
CGF.EmitBlock(RethrowBB);
|
|
|
|
if (SavedExnVar) {
|
2013-03-01 03:01:20 +08:00
|
|
|
CGF.EmitRuntimeCallOrInvoke(RethrowFn,
|
|
|
|
CGF.Builder.CreateLoad(SavedExnVar));
|
2010-07-21 13:47:49 +08:00
|
|
|
} else {
|
2013-03-01 03:01:20 +08:00
|
|
|
CGF.EmitRuntimeCallOrInvoke(RethrowFn);
|
2010-07-21 13:47:49 +08:00
|
|
|
}
|
|
|
|
CGF.Builder.CreateUnreachable();
|
|
|
|
|
|
|
|
CGF.EmitBlock(ContBB);
|
2010-08-11 08:16:14 +08:00
|
|
|
|
|
|
|
// Restore the cleanup destination.
|
|
|
|
CGF.Builder.CreateStore(SavedCleanupDest,
|
|
|
|
CGF.getNormalCleanupDestSlot());
|
2010-07-21 13:47:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Leave the end-catch cleanup. As an optimization, pretend that
|
|
|
|
// the fallthrough path was inaccessible; we've dynamically proven
|
|
|
|
// that we're not in the EH case along that path.
|
|
|
|
if (EndCatchFn) {
|
|
|
|
CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
|
|
|
|
CGF.PopCleanupBlock();
|
|
|
|
CGF.Builder.restoreIP(SavedIP);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure we actually have an insertion point or the
|
|
|
|
// cleanup gods will hate us.
|
|
|
|
CGF.EnsureInsertPoint();
|
|
|
|
}
|
|
|
|
};
|
2010-07-21 08:52:03 +08:00
|
|
|
}
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
/// Enters a finally block for an implementation using zero-cost
|
|
|
|
/// exceptions. This is mostly general, but hard-codes some
|
|
|
|
/// language/ABI-specific behavior in the catch-all sections.
|
2011-06-22 10:32:12 +08:00
|
|
|
void CodeGenFunction::FinallyInfo::enter(CodeGenFunction &CGF,
|
|
|
|
const Stmt *body,
|
|
|
|
llvm::Constant *beginCatchFn,
|
|
|
|
llvm::Constant *endCatchFn,
|
|
|
|
llvm::Constant *rethrowFn) {
|
2014-05-21 13:09:00 +08:00
|
|
|
assert((beginCatchFn != nullptr) == (endCatchFn != nullptr) &&
|
2010-07-06 09:34:17 +08:00
|
|
|
"begin/end catch functions not paired");
|
2011-06-22 10:32:12 +08:00
|
|
|
assert(rethrowFn && "rethrow function is required");
|
|
|
|
|
|
|
|
BeginCatchFn = beginCatchFn;
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// The rethrow function has one of the following two types:
|
|
|
|
// void (*)()
|
|
|
|
// void (*)(void*)
|
|
|
|
// In the latter case we need to pass it the exception object.
|
|
|
|
// But we can't use the exception slot because the @finally might
|
|
|
|
// have a landing pad (which would overwrite the exception slot).
|
2011-07-18 12:24:23 +08:00
|
|
|
llvm::FunctionType *rethrowFnTy =
|
2010-07-06 09:34:17 +08:00
|
|
|
cast<llvm::FunctionType>(
|
2011-06-22 10:32:12 +08:00
|
|
|
cast<llvm::PointerType>(rethrowFn->getType())->getElementType());
|
2014-05-21 13:09:00 +08:00
|
|
|
SavedExnVar = nullptr;
|
2011-06-22 10:32:12 +08:00
|
|
|
if (rethrowFnTy->getNumParams())
|
|
|
|
SavedExnVar = CGF.CreateTempAlloca(CGF.Int8PtrTy, "finally.exn");
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// A finally block is a statement which must be executed on any edge
|
|
|
|
// out of a given scope. Unlike a cleanup, the finally block may
|
|
|
|
// contain arbitrary control flow leading out of itself. In
|
|
|
|
// addition, finally blocks should always be executed, even if there
|
|
|
|
// are no catch handlers higher on the stack. Therefore, we
|
|
|
|
// surround the protected scope with a combination of a normal
|
|
|
|
// cleanup (to catch attempts to break out of the block via normal
|
|
|
|
// control flow) and an EH catch-all (semantically "outside" any try
|
|
|
|
// statement to which the finally block might have been attached).
|
|
|
|
// The finally block itself is generated in the context of a cleanup
|
|
|
|
// which conditionally leaves the catch-all.
|
|
|
|
|
|
|
|
// Jump destination for performing the finally block on an exception
|
|
|
|
// edge. We'll never actually reach this block, so unreachable is
|
|
|
|
// fine.
|
2011-06-22 10:32:12 +08:00
|
|
|
RethrowDest = CGF.getJumpDestInCurrentScope(CGF.getUnreachableBlock());
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Whether the finally block is being executed for EH purposes.
|
2011-06-22 10:32:12 +08:00
|
|
|
ForEHVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(), "finally.for-eh");
|
|
|
|
CGF.Builder.CreateStore(CGF.Builder.getFalse(), ForEHVar);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Enter a normal cleanup which will perform the @finally block.
|
2011-06-22 10:32:12 +08:00
|
|
|
CGF.EHStack.pushCleanup<PerformFinally>(NormalCleanup, body,
|
|
|
|
ForEHVar, endCatchFn,
|
|
|
|
rethrowFn, SavedExnVar);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
|
|
|
// Enter a catch-all scope.
|
2011-06-22 10:32:12 +08:00
|
|
|
llvm::BasicBlock *catchBB = CGF.createBasicBlock("finally.catchall");
|
|
|
|
EHCatchScope *catchScope = CGF.EHStack.pushCatch(1);
|
|
|
|
catchScope->setCatchAllHandler(0, catchBB);
|
|
|
|
}
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-06-22 10:32:12 +08:00
|
|
|
void CodeGenFunction::FinallyInfo::exit(CodeGenFunction &CGF) {
|
|
|
|
// Leave the finally catch-all.
|
|
|
|
EHCatchScope &catchScope = cast<EHCatchScope>(*CGF.EHStack.begin());
|
|
|
|
llvm::BasicBlock *catchBB = catchScope.getHandler(0).Block;
|
2011-08-11 10:22:43 +08:00
|
|
|
|
|
|
|
CGF.popCatchScope();
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-06-22 10:32:12 +08:00
|
|
|
// If there are any references to the catch-all block, emit it.
|
|
|
|
if (catchBB->use_empty()) {
|
|
|
|
delete catchBB;
|
|
|
|
} else {
|
|
|
|
CGBuilderTy::InsertPoint savedIP = CGF.Builder.saveAndClearIP();
|
|
|
|
CGF.EmitBlock(catchBB);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2014-05-21 13:09:00 +08:00
|
|
|
llvm::Value *exn = nullptr;
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-06-22 10:32:12 +08:00
|
|
|
// If there's a begin-catch function, call it.
|
|
|
|
if (BeginCatchFn) {
|
2011-09-16 02:57:19 +08:00
|
|
|
exn = CGF.getExceptionFromSlot();
|
2013-03-01 03:01:20 +08:00
|
|
|
CGF.EmitNounwindRuntimeCall(BeginCatchFn, exn);
|
2011-06-22 10:32:12 +08:00
|
|
|
}
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-06-22 10:32:12 +08:00
|
|
|
// If we need to remember the exception pointer to rethrow later, do so.
|
|
|
|
if (SavedExnVar) {
|
2011-09-16 02:57:19 +08:00
|
|
|
if (!exn) exn = CGF.getExceptionFromSlot();
|
2011-06-22 10:32:12 +08:00
|
|
|
CGF.Builder.CreateStore(exn, SavedExnVar);
|
|
|
|
}
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-06-22 10:32:12 +08:00
|
|
|
// Tell the cleanups in the finally block that we're do this for EH.
|
|
|
|
CGF.Builder.CreateStore(CGF.Builder.getTrue(), ForEHVar);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-06-22 10:32:12 +08:00
|
|
|
// Thread a jump through the finally cleanup.
|
|
|
|
CGF.EmitBranchThroughCleanup(RethrowDest);
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-06-22 10:32:12 +08:00
|
|
|
CGF.Builder.restoreIP(savedIP);
|
|
|
|
}
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2011-06-22 10:32:12 +08:00
|
|
|
// Finally, leave the @finally cleanup.
|
|
|
|
CGF.PopCleanupBlock();
|
2010-07-06 09:34:17 +08:00
|
|
|
}
|
|
|
|
|
2013-02-12 11:51:46 +08:00
|
|
|
/// In a terminate landing pad, should we use __clang__call_terminate
|
|
|
|
/// or just a naked call to std::terminate?
|
|
|
|
///
|
|
|
|
/// __clang_call_terminate calls __cxa_begin_catch, which then allows
|
|
|
|
/// std::terminate to usefully report something about the
|
|
|
|
/// violating exception.
|
|
|
|
static bool useClangCallTerminate(CodeGenModule &CGM) {
|
|
|
|
// Only do this for Itanium-family ABIs in C++ mode.
|
|
|
|
return (CGM.getLangOpts().CPlusPlus &&
|
|
|
|
CGM.getTarget().getCXXABI().isItaniumFamily());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get or define the following function:
|
|
|
|
/// void @__clang_call_terminate(i8* %exn) nounwind noreturn
|
|
|
|
/// This code is used only in C++.
|
|
|
|
static llvm::Constant *getClangCallTerminateFn(CodeGenModule &CGM) {
|
|
|
|
llvm::FunctionType *fnTy =
|
|
|
|
llvm::FunctionType::get(CGM.VoidTy, CGM.Int8PtrTy, /*IsVarArgs=*/false);
|
|
|
|
llvm::Constant *fnRef =
|
|
|
|
CGM.CreateRuntimeFunction(fnTy, "__clang_call_terminate");
|
|
|
|
|
|
|
|
llvm::Function *fn = dyn_cast<llvm::Function>(fnRef);
|
|
|
|
if (fn && fn->empty()) {
|
|
|
|
fn->setDoesNotThrow();
|
|
|
|
fn->setDoesNotReturn();
|
|
|
|
|
|
|
|
// What we really want is to massively penalize inlining without
|
|
|
|
// forbidding it completely. The difference between that and
|
|
|
|
// 'noinline' is negligible.
|
|
|
|
fn->addFnAttr(llvm::Attribute::NoInline);
|
|
|
|
|
|
|
|
// Allow this function to be shared across translation units, but
|
|
|
|
// we don't want it to turn into an exported symbol.
|
|
|
|
fn->setLinkage(llvm::Function::LinkOnceODRLinkage);
|
|
|
|
fn->setVisibility(llvm::Function::HiddenVisibility);
|
2015-02-12 02:50:13 +08:00
|
|
|
if (CGM.supportsCOMDAT())
|
|
|
|
fn->setComdat(CGM.getModule().getOrInsertComdat(fn->getName()));
|
2013-02-12 11:51:46 +08:00
|
|
|
|
|
|
|
// Set up the function.
|
|
|
|
llvm::BasicBlock *entry =
|
|
|
|
llvm::BasicBlock::Create(CGM.getLLVMContext(), "", fn);
|
|
|
|
CGBuilderTy builder(entry);
|
|
|
|
|
|
|
|
// Pull the exception pointer out of the parameter list.
|
|
|
|
llvm::Value *exn = &*fn->arg_begin();
|
|
|
|
|
|
|
|
// Call __cxa_begin_catch(exn).
|
2013-03-01 03:01:20 +08:00
|
|
|
llvm::CallInst *catchCall = builder.CreateCall(getBeginCatchFn(CGM), exn);
|
|
|
|
catchCall->setDoesNotThrow();
|
|
|
|
catchCall->setCallingConv(CGM.getRuntimeCC());
|
2013-02-12 11:51:46 +08:00
|
|
|
|
|
|
|
// Call std::terminate().
|
|
|
|
llvm::CallInst *termCall = builder.CreateCall(getTerminateFn(CGM));
|
|
|
|
termCall->setDoesNotThrow();
|
|
|
|
termCall->setDoesNotReturn();
|
2013-03-01 03:01:20 +08:00
|
|
|
termCall->setCallingConv(CGM.getRuntimeCC());
|
2013-02-12 11:51:46 +08:00
|
|
|
|
|
|
|
// std::terminate cannot return.
|
|
|
|
builder.CreateUnreachable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return fnRef;
|
|
|
|
}
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
llvm::BasicBlock *CodeGenFunction::getTerminateLandingPad() {
|
|
|
|
if (TerminateLandingPad)
|
|
|
|
return TerminateLandingPad;
|
|
|
|
|
|
|
|
CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
|
|
|
|
|
|
|
|
// This will get inserted at the end of the function.
|
|
|
|
TerminateLandingPad = createBasicBlock("terminate.lpad");
|
|
|
|
Builder.SetInsertPoint(TerminateLandingPad);
|
|
|
|
|
|
|
|
// Tell the backend that this is a landing pad.
|
2015-02-06 02:56:03 +08:00
|
|
|
const EHPersonality &Personality = EHPersonality::get(*this);
|
2011-09-20 04:31:14 +08:00
|
|
|
llvm::LandingPadInst *LPadInst =
|
2014-12-02 06:02:27 +08:00
|
|
|
Builder.CreateLandingPad(llvm::StructType::get(Int8PtrTy, Int32Ty, nullptr),
|
2011-09-20 04:31:14 +08:00
|
|
|
getOpaquePersonalityFn(CGM, Personality), 0);
|
|
|
|
LPadInst->addClause(getCatchAllValue(*this));
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2013-02-12 11:51:46 +08:00
|
|
|
llvm::CallInst *terminateCall;
|
|
|
|
if (useClangCallTerminate(CGM)) {
|
|
|
|
// Extract out the exception pointer.
|
|
|
|
llvm::Value *exn = Builder.CreateExtractValue(LPadInst, 0);
|
2013-03-01 03:01:20 +08:00
|
|
|
terminateCall = EmitNounwindRuntimeCall(getClangCallTerminateFn(CGM), exn);
|
2013-02-12 11:51:46 +08:00
|
|
|
} else {
|
2013-03-01 03:01:20 +08:00
|
|
|
terminateCall = EmitNounwindRuntimeCall(getTerminateFn(CGM));
|
2013-02-12 11:51:46 +08:00
|
|
|
}
|
|
|
|
terminateCall->setDoesNotReturn();
|
2011-02-08 16:22:06 +08:00
|
|
|
Builder.CreateUnreachable();
|
2009-12-09 11:35:49 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// Restore the saved insertion state.
|
|
|
|
Builder.restoreIP(SavedIP);
|
2010-04-30 08:06:43 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
return TerminateLandingPad;
|
2009-12-09 11:35:49 +08:00
|
|
|
}
|
2009-12-10 06:59:31 +08:00
|
|
|
|
|
|
|
llvm::BasicBlock *CodeGenFunction::getTerminateHandler() {
|
2009-12-10 08:02:42 +08:00
|
|
|
if (TerminateHandler)
|
|
|
|
return TerminateHandler;
|
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
CGBuilderTy::InsertPoint SavedIP = Builder.saveAndClearIP();
|
2009-12-10 06:59:31 +08:00
|
|
|
|
2010-07-06 09:34:17 +08:00
|
|
|
// Set up the terminate handler. This block is inserted at the very
|
|
|
|
// end of the function by FinishFunction.
|
2009-12-10 08:02:42 +08:00
|
|
|
TerminateHandler = createBasicBlock("terminate.handler");
|
2010-07-06 09:34:17 +08:00
|
|
|
Builder.SetInsertPoint(TerminateHandler);
|
2013-06-21 05:37:43 +08:00
|
|
|
llvm::CallInst *terminateCall;
|
|
|
|
if (useClangCallTerminate(CGM)) {
|
|
|
|
// Load the exception pointer.
|
|
|
|
llvm::Value *exn = getExceptionFromSlot();
|
|
|
|
terminateCall = EmitNounwindRuntimeCall(getClangCallTerminateFn(CGM), exn);
|
|
|
|
} else {
|
|
|
|
terminateCall = EmitNounwindRuntimeCall(getTerminateFn(CGM));
|
|
|
|
}
|
|
|
|
terminateCall->setDoesNotReturn();
|
2009-12-10 06:59:31 +08:00
|
|
|
Builder.CreateUnreachable();
|
|
|
|
|
2010-04-21 18:05:39 +08:00
|
|
|
// Restore the saved insertion state.
|
2010-07-06 09:34:17 +08:00
|
|
|
Builder.restoreIP(SavedIP);
|
2009-12-10 07:31:35 +08:00
|
|
|
|
2009-12-10 06:59:31 +08:00
|
|
|
return TerminateHandler;
|
|
|
|
}
|
2010-07-06 09:34:17 +08:00
|
|
|
|
2012-11-08 00:50:40 +08:00
|
|
|
llvm::BasicBlock *CodeGenFunction::getEHResumeBlock(bool isCleanup) {
|
2011-08-11 10:22:43 +08:00
|
|
|
if (EHResumeBlock) return EHResumeBlock;
|
2010-07-24 05:56:41 +08:00
|
|
|
|
|
|
|
CGBuilderTy::InsertPoint SavedIP = Builder.saveIP();
|
|
|
|
|
|
|
|
// We emit a jump to a notional label at the outermost unwind state.
|
2011-08-11 10:22:43 +08:00
|
|
|
EHResumeBlock = createBasicBlock("eh.resume");
|
|
|
|
Builder.SetInsertPoint(EHResumeBlock);
|
2010-07-24 05:56:41 +08:00
|
|
|
|
2015-02-06 02:56:03 +08:00
|
|
|
const EHPersonality &Personality = EHPersonality::get(*this);
|
2010-07-24 05:56:41 +08:00
|
|
|
|
|
|
|
// This can always be a call because we necessarily didn't find
|
|
|
|
// anything on the EH stack which needs our help.
|
2012-02-08 20:41:24 +08:00
|
|
|
const char *RethrowName = Personality.CatchallRethrowFn;
|
2014-05-21 13:09:00 +08:00
|
|
|
if (RethrowName != nullptr && !isCleanup) {
|
2013-03-01 03:01:20 +08:00
|
|
|
EmitRuntimeCall(getCatchallRethrowFn(CGM, RethrowName),
|
2014-07-01 19:47:10 +08:00
|
|
|
getExceptionFromSlot())
|
2011-05-29 05:13:02 +08:00
|
|
|
->setDoesNotReturn();
|
2014-07-01 19:47:10 +08:00
|
|
|
Builder.CreateUnreachable();
|
|
|
|
Builder.restoreIP(SavedIP);
|
|
|
|
return EHResumeBlock;
|
2011-05-29 05:13:02 +08:00
|
|
|
}
|
2010-07-24 05:56:41 +08:00
|
|
|
|
2014-07-01 19:47:10 +08:00
|
|
|
// Recreate the landingpad's return value for the 'resume' instruction.
|
|
|
|
llvm::Value *Exn = getExceptionFromSlot();
|
|
|
|
llvm::Value *Sel = getSelectorFromSlot();
|
2010-07-24 05:56:41 +08:00
|
|
|
|
2014-07-01 19:47:10 +08:00
|
|
|
llvm::Type *LPadType = llvm::StructType::get(Exn->getType(),
|
2014-12-02 06:02:27 +08:00
|
|
|
Sel->getType(), nullptr);
|
2014-07-01 19:47:10 +08:00
|
|
|
llvm::Value *LPadVal = llvm::UndefValue::get(LPadType);
|
|
|
|
LPadVal = Builder.CreateInsertValue(LPadVal, Exn, 0, "lpad.val");
|
|
|
|
LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
|
2010-07-24 05:56:41 +08:00
|
|
|
|
2014-07-01 19:47:10 +08:00
|
|
|
Builder.CreateResume(LPadVal);
|
|
|
|
Builder.restoreIP(SavedIP);
|
2011-08-11 10:22:43 +08:00
|
|
|
return EHResumeBlock;
|
2010-07-24 05:56:41 +08:00
|
|
|
}
|
2013-09-17 05:46:30 +08:00
|
|
|
|
|
|
|
void CodeGenFunction::EmitSEHTryStmt(const SEHTryStmt &S) {
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
// FIXME: Implement SEH on other architectures.
|
|
|
|
const llvm::Triple &T = CGM.getTarget().getTriple();
|
|
|
|
if (T.getArch() != llvm::Triple::x86_64 ||
|
|
|
|
!T.isKnownWindowsMSVCEnvironment()) {
|
|
|
|
ErrorUnsupported(&S, "__try statement");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-05 06:37:07 +08:00
|
|
|
SEHFinallyInfo FI;
|
|
|
|
EnterSEHTryStmt(S, FI);
|
2015-02-12 05:40:48 +08:00
|
|
|
{
|
|
|
|
// Disable inlining inside SEH __try scopes.
|
|
|
|
SaveAndRestore<bool> Saver(IsSEHTryScope, true);
|
|
|
|
EmitStmt(S.getTryBlock());
|
|
|
|
}
|
2015-02-05 06:37:07 +08:00
|
|
|
ExitSEHTryStmt(S, FI);
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
struct PerformSEHFinally : EHScopeStack::Cleanup {
|
2015-02-05 06:37:07 +08:00
|
|
|
CodeGenFunction::SEHFinallyInfo *FI;
|
|
|
|
PerformSEHFinally(CodeGenFunction::SEHFinallyInfo *FI) : FI(FI) {}
|
|
|
|
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
void Emit(CodeGenFunction &CGF, Flags F) override {
|
2015-02-05 06:37:07 +08:00
|
|
|
// Cleanups are emitted at most twice: once for normal control flow and once
|
|
|
|
// for exception control flow. Branch into the finally block, and remember
|
|
|
|
// the continuation block so we can branch out later.
|
|
|
|
if (!FI->FinallyBB) {
|
|
|
|
FI->FinallyBB = CGF.createBasicBlock("__finally");
|
|
|
|
FI->FinallyBB->insertInto(CGF.CurFn);
|
|
|
|
FI->FinallyBB->moveAfter(CGF.Builder.GetInsertBlock());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the termination status and branch in.
|
|
|
|
CGF.Builder.CreateStore(
|
|
|
|
llvm::ConstantInt::get(CGF.Int8Ty, F.isForEHCleanup()),
|
|
|
|
CGF.getAbnormalTerminationSlot());
|
|
|
|
CGF.Builder.CreateBr(FI->FinallyBB);
|
|
|
|
|
|
|
|
// Create a continuation block for normal or exceptional control.
|
|
|
|
if (F.isForEHCleanup()) {
|
|
|
|
assert(!FI->ResumeBB && "double emission for EH");
|
|
|
|
FI->ResumeBB = CGF.createBasicBlock("__finally.resume");
|
|
|
|
CGF.EmitBlock(FI->ResumeBB);
|
|
|
|
} else {
|
|
|
|
assert(F.isForNormalCleanup() && !FI->ContBB && "double normal emission");
|
|
|
|
FI->ContBB = CGF.createBasicBlock("__finally.cont");
|
|
|
|
CGF.EmitBlock(FI->ContBB);
|
|
|
|
// Try to keep source order.
|
|
|
|
FI->ContBB->moveAfter(FI->FinallyBB);
|
|
|
|
}
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a stub filter function that will ultimately hold the code of the
|
|
|
|
/// filter expression. The EH preparation passes in LLVM will outline the code
|
|
|
|
/// from the main function body into this stub.
|
|
|
|
llvm::Function *
|
|
|
|
CodeGenFunction::GenerateSEHFilterFunction(CodeGenFunction &ParentCGF,
|
|
|
|
const SEHExceptStmt &Except) {
|
|
|
|
const Decl *ParentCodeDecl = ParentCGF.CurCodeDecl;
|
|
|
|
llvm::Function *ParentFn = ParentCGF.CurFn;
|
|
|
|
|
|
|
|
Expr *FilterExpr = Except.getFilterExpr();
|
|
|
|
|
|
|
|
// Get the mangled function name.
|
|
|
|
SmallString<128> Name;
|
|
|
|
{
|
|
|
|
llvm::raw_svector_ostream OS(Name);
|
|
|
|
const NamedDecl *Parent = dyn_cast_or_null<NamedDecl>(ParentCodeDecl);
|
|
|
|
assert(Parent && "FIXME: handle unnamed decls (lambdas, blocks) with SEH");
|
|
|
|
CGM.getCXXABI().getMangleContext().mangleSEHFilterExpression(Parent, OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arrange a function with the declaration:
|
|
|
|
// int filt(EXCEPTION_POINTERS *exception_pointers, void *frame_pointer)
|
|
|
|
QualType RetTy = getContext().IntTy;
|
|
|
|
FunctionArgList Args;
|
|
|
|
SEHPointersDecl = ImplicitParamDecl::Create(
|
|
|
|
getContext(), nullptr, FilterExpr->getLocStart(),
|
|
|
|
&getContext().Idents.get("exception_pointers"), getContext().VoidPtrTy);
|
|
|
|
Args.push_back(SEHPointersDecl);
|
|
|
|
Args.push_back(ImplicitParamDecl::Create(
|
|
|
|
getContext(), nullptr, FilterExpr->getLocStart(),
|
|
|
|
&getContext().Idents.get("frame_pointer"), getContext().VoidPtrTy));
|
|
|
|
const CGFunctionInfo &FnInfo = CGM.getTypes().arrangeFreeFunctionDeclaration(
|
|
|
|
RetTy, Args, FunctionType::ExtInfo(), /*isVariadic=*/false);
|
|
|
|
llvm::FunctionType *FnTy = CGM.getTypes().GetFunctionType(FnInfo);
|
|
|
|
llvm::Function *Fn = llvm::Function::Create(FnTy, ParentFn->getLinkage(),
|
|
|
|
Name.str(), &CGM.getModule());
|
|
|
|
// The filter is either in the same comdat as the function, or it's internal.
|
|
|
|
if (llvm::Comdat *C = ParentFn->getComdat()) {
|
|
|
|
Fn->setComdat(C);
|
|
|
|
} else if (ParentFn->hasWeakLinkage() || ParentFn->hasLinkOnceLinkage()) {
|
|
|
|
// FIXME: Unreachable with Rafael's changes?
|
|
|
|
llvm::Comdat *C = CGM.getModule().getOrInsertComdat(ParentFn->getName());
|
|
|
|
ParentFn->setComdat(C);
|
|
|
|
Fn->setComdat(C);
|
|
|
|
} else {
|
|
|
|
Fn->setLinkage(llvm::GlobalValue::InternalLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
StartFunction(GlobalDecl(), RetTy, Fn, FnInfo, Args,
|
|
|
|
FilterExpr->getLocStart(), FilterExpr->getLocStart());
|
|
|
|
|
|
|
|
EmitSEHExceptionCodeSave();
|
|
|
|
|
|
|
|
// Insert dummy allocas for every local variable in scope. We'll initialize
|
|
|
|
// them and prune the unused ones after we find out which ones were
|
|
|
|
// referenced.
|
|
|
|
for (const auto &DeclPtrs : ParentCGF.LocalDeclMap) {
|
|
|
|
const Decl *VD = DeclPtrs.first;
|
|
|
|
llvm::Value *Ptr = DeclPtrs.second;
|
|
|
|
auto *ValTy = cast<llvm::PointerType>(Ptr->getType())->getElementType();
|
|
|
|
LocalDeclMap[VD] = CreateTempAlloca(ValTy, Ptr->getName() + ".filt");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the original filter expression, convert to i32, and return.
|
|
|
|
llvm::Value *R = EmitScalarExpr(FilterExpr);
|
|
|
|
R = Builder.CreateIntCast(R, CGM.IntTy,
|
|
|
|
FilterExpr->getType()->isSignedIntegerType());
|
|
|
|
Builder.CreateStore(R, ReturnValue);
|
|
|
|
|
|
|
|
FinishFunction(FilterExpr->getLocEnd());
|
|
|
|
|
|
|
|
for (const auto &DeclPtrs : ParentCGF.LocalDeclMap) {
|
|
|
|
const Decl *VD = DeclPtrs.first;
|
|
|
|
auto *Alloca = cast<llvm::AllocaInst>(LocalDeclMap[VD]);
|
|
|
|
if (Alloca->hasNUses(0)) {
|
|
|
|
Alloca->eraseFromParent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ErrorUnsupported(FilterExpr,
|
|
|
|
"SEH filter expression local variable capture");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EmitSEHExceptionCodeSave() {
|
|
|
|
// Save the exception code in the exception slot to unify exception access in
|
|
|
|
// the filter function and the landing pad.
|
|
|
|
// struct EXCEPTION_POINTERS {
|
|
|
|
// EXCEPTION_RECORD *ExceptionRecord;
|
|
|
|
// CONTEXT *ContextRecord;
|
|
|
|
// };
|
|
|
|
// void *exn.slot =
|
|
|
|
// (void *)(uintptr_t)exception_pointers->ExceptionRecord->ExceptionCode;
|
|
|
|
llvm::Value *Ptrs = Builder.CreateLoad(GetAddrOfLocalVar(SEHPointersDecl));
|
|
|
|
llvm::Type *RecordTy = CGM.Int32Ty->getPointerTo();
|
|
|
|
llvm::Type *PtrsTy = llvm::StructType::get(RecordTy, CGM.VoidPtrTy, nullptr);
|
|
|
|
Ptrs = Builder.CreateBitCast(Ptrs, PtrsTy->getPointerTo());
|
|
|
|
llvm::Value *Rec = Builder.CreateStructGEP(Ptrs, 0);
|
|
|
|
Rec = Builder.CreateLoad(Rec);
|
|
|
|
llvm::Value *Code = Builder.CreateLoad(Rec);
|
|
|
|
Code = Builder.CreateZExt(Code, CGM.IntPtrTy);
|
|
|
|
// FIXME: Change landing pads to produce {i32, i32} and make the exception
|
|
|
|
// slot an i32.
|
|
|
|
Code = Builder.CreateIntToPtr(Code, CGM.VoidPtrTy);
|
|
|
|
Builder.CreateStore(Code, getExceptionSlot());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::EmitSEHExceptionInfo() {
|
|
|
|
// Sema should diagnose calling this builtin outside of a filter context, but
|
|
|
|
// don't crash if we screw up.
|
|
|
|
if (!SEHPointersDecl)
|
|
|
|
return llvm::UndefValue::get(Int8PtrTy);
|
|
|
|
return Builder.CreateLoad(GetAddrOfLocalVar(SEHPointersDecl));
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *CodeGenFunction::EmitSEHExceptionCode() {
|
|
|
|
// If we're in a landing pad or filter function, the exception slot contains
|
|
|
|
// the code.
|
|
|
|
assert(ExceptionSlot);
|
|
|
|
llvm::Value *Code =
|
|
|
|
Builder.CreatePtrToInt(getExceptionFromSlot(), CGM.IntPtrTy);
|
|
|
|
return Builder.CreateTrunc(Code, CGM.Int32Ty);
|
|
|
|
}
|
|
|
|
|
2015-02-05 06:37:07 +08:00
|
|
|
llvm::Value *CodeGenFunction::EmitSEHAbnormalTermination() {
|
|
|
|
// Load from the abnormal termination slot. It will be uninitialized outside
|
|
|
|
// of __finally blocks, which we should warn or error on.
|
|
|
|
llvm::Value *IsEH = Builder.CreateLoad(getAbnormalTerminationSlot());
|
|
|
|
return Builder.CreateZExt(IsEH, Int32Ty);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CodeGenFunction::EnterSEHTryStmt(const SEHTryStmt &S, SEHFinallyInfo &FI) {
|
2015-02-05 09:20:26 +08:00
|
|
|
if (S.getFinallyHandler()) {
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
// Push a cleanup for __finally blocks.
|
2015-02-05 06:37:07 +08:00
|
|
|
EHStack.pushCleanup<PerformSEHFinally>(NormalAndEHCleanup, &FI);
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we must have an __except block.
|
|
|
|
SEHExceptStmt *Except = S.getExceptHandler();
|
|
|
|
assert(Except);
|
|
|
|
EHCatchScope *CatchScope = EHStack.pushCatch(1);
|
2015-01-22 10:25:56 +08:00
|
|
|
|
|
|
|
// If the filter is known to evaluate to 1, then we can use the clause "catch
|
|
|
|
// i8* null".
|
|
|
|
llvm::Constant *C =
|
|
|
|
CGM.EmitConstantExpr(Except->getFilterExpr(), getContext().IntTy, this);
|
|
|
|
if (C && C->isOneValue()) {
|
|
|
|
CatchScope->setCatchAllHandler(0, createBasicBlock("__except"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In general, we have to emit an outlined filter function. Use the function
|
|
|
|
// in place of the RTTI typeinfo global that C++ EH uses.
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
CodeGenFunction FilterCGF(CGM, /*suppressNewContext=*/true);
|
|
|
|
llvm::Function *FilterFunc =
|
|
|
|
FilterCGF.GenerateSEHFilterFunction(*this, *Except);
|
|
|
|
llvm::Constant *OpaqueFunc =
|
|
|
|
llvm::ConstantExpr::getBitCast(FilterFunc, Int8PtrTy);
|
|
|
|
CatchScope->setHandler(0, OpaqueFunc, createBasicBlock("__except"));
|
|
|
|
}
|
|
|
|
|
2015-02-05 06:37:07 +08:00
|
|
|
void CodeGenFunction::ExitSEHTryStmt(const SEHTryStmt &S, SEHFinallyInfo &FI) {
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
// Just pop the cleanup if it's a __finally block.
|
2015-02-05 06:37:07 +08:00
|
|
|
if (const SEHFinallyStmt *Finally = S.getFinallyHandler()) {
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
PopCleanupBlock();
|
2015-02-05 08:58:46 +08:00
|
|
|
assert(FI.ContBB && "did not emit normal cleanup");
|
2015-02-05 06:37:07 +08:00
|
|
|
|
|
|
|
// Emit the code into FinallyBB.
|
|
|
|
Builder.SetInsertPoint(FI.FinallyBB);
|
|
|
|
EmitStmt(Finally->getBlock());
|
|
|
|
|
2015-02-05 08:58:46 +08:00
|
|
|
// If the finally block doesn't fall through, we don't need these blocks.
|
|
|
|
if (!HaveInsertPoint()) {
|
|
|
|
FI.ContBB->eraseFromParent();
|
|
|
|
if (FI.ResumeBB)
|
|
|
|
FI.ResumeBB->eraseFromParent();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-05 06:37:07 +08:00
|
|
|
if (FI.ResumeBB) {
|
|
|
|
llvm::Value *IsEH = Builder.CreateLoad(getAbnormalTerminationSlot(),
|
|
|
|
"abnormal.termination");
|
|
|
|
IsEH = Builder.CreateICmpEQ(IsEH, llvm::ConstantInt::get(Int8Ty, 0));
|
|
|
|
Builder.CreateCondBr(IsEH, FI.ContBB, FI.ResumeBB);
|
|
|
|
} else {
|
|
|
|
// There was nothing exceptional in the try body, so we only have normal
|
|
|
|
// control flow.
|
|
|
|
Builder.CreateBr(FI.ContBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
Builder.SetInsertPoint(FI.ContBB);
|
|
|
|
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we must have an __except block.
|
2015-02-05 06:37:07 +08:00
|
|
|
const SEHExceptStmt *Except = S.getExceptHandler();
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
assert(Except && "__try must have __finally xor __except");
|
|
|
|
EHCatchScope &CatchScope = cast<EHCatchScope>(*EHStack.begin());
|
|
|
|
|
|
|
|
// Don't emit the __except block if the __try block lacked invokes.
|
|
|
|
// TODO: Model unwind edges from instructions, either with iload / istore or
|
|
|
|
// a try body function.
|
|
|
|
if (!CatchScope.hasEHBranches()) {
|
|
|
|
CatchScope.clearHandlerBlocks();
|
|
|
|
EHStack.popCatch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The fall-through block.
|
|
|
|
llvm::BasicBlock *ContBB = createBasicBlock("__try.cont");
|
|
|
|
|
|
|
|
// We just emitted the body of the __try; jump to the continue block.
|
|
|
|
if (HaveInsertPoint())
|
|
|
|
Builder.CreateBr(ContBB);
|
|
|
|
|
|
|
|
// Check if our filter function returned true.
|
|
|
|
emitCatchDispatchBlock(*this, CatchScope);
|
|
|
|
|
|
|
|
// Grab the block before we pop the handler.
|
|
|
|
llvm::BasicBlock *ExceptBB = CatchScope.getHandler(0).Block;
|
|
|
|
EHStack.popCatch();
|
|
|
|
|
|
|
|
EmitBlockAfterUses(ExceptBB);
|
|
|
|
|
|
|
|
// Emit the __except body.
|
|
|
|
EmitStmt(Except->getBlock());
|
|
|
|
|
2015-01-31 06:16:45 +08:00
|
|
|
if (HaveInsertPoint())
|
|
|
|
Builder.CreateBr(ContBB);
|
Initial support for Win64 SEH IR emission
The lowering looks a lot like normal EH lowering, with the exception
that the exceptions are caught by executing filter expression code
instead of matching typeinfo globals. The filter expressions are
outlined into functions which are used in landingpad clauses where
typeinfo would normally go.
Major aspects that still need work:
- Non-call exceptions in __try bodies won't work yet. The plan is to
outline the __try block in the frontend to keep things simple.
- Filter expressions cannot use local variables until capturing is
implemented.
- __finally blocks will not run after exceptions. Fixing this requires
work in the LLVM SEH preparation pass.
The IR lowering looks like this:
// C code:
bool safe_div(int n, int d, int *r) {
__try {
*r = normal_div(n, d);
} __except(_exception_code() == EXCEPTION_INT_DIVIDE_BY_ZERO) {
return false;
}
return true;
}
; LLVM IR:
define i32 @filter(i8* %e, i8* %fp) {
%ehptrs = bitcast i8* %e to i32**
%ehrec = load i32** %ehptrs
%code = load i32* %ehrec
%matches = icmp eq i32 %code, i32 u0xC0000094
%matches.i32 = zext i1 %matches to i32
ret i32 %matches.i32
}
define i1 zeroext @safe_div(i32 %n, i32 %d, i32* %r) {
%rr = invoke i32 @normal_div(i32 %n, i32 %d)
to label %normal unwind to label %lpad
normal:
store i32 %rr, i32* %r
ret i1 1
lpad:
%ehvals = landingpad {i8*, i32} personality i32 (...)* @__C_specific_handler
catch i8* bitcast (i32 (i8*, i8*)* @filter to i8*)
%ehptr = extractvalue {i8*, i32} %ehvals, i32 0
%sel = extractvalue {i8*, i32} %ehvals, i32 1
%filter_sel = call i32 @llvm.eh.seh.typeid.for(i8* bitcast (i32 (i8*, i8*)* @filter to i8*))
%matches = icmp eq i32 %sel, %filter_sel
br i1 %matches, label %eh.except, label %eh.resume
eh.except:
ret i1 false
eh.resume:
resume
}
Reviewers: rjmccall, rsmith, majnemer
Differential Revision: http://reviews.llvm.org/D5607
llvm-svn: 226760
2015-01-22 09:36:17 +08:00
|
|
|
|
|
|
|
EmitBlock(ContBB);
|
2013-09-17 05:46:30 +08:00
|
|
|
}
|
2014-07-07 08:12:30 +08:00
|
|
|
|
|
|
|
void CodeGenFunction::EmitSEHLeaveStmt(const SEHLeaveStmt &S) {
|
|
|
|
CGM.ErrorUnsupported(&S, "SEH __leave");
|
|
|
|
}
|