opnsense-src/lib/CodeGen/CGBlocks.cpp

1192 lines
43 KiB
C++
Raw Normal View History

2009-06-02 13:58:47 -04:00
//===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This contains code to emit blocks.
//
//===----------------------------------------------------------------------===//
2009-10-14 14:03:49 -04:00
#include "CGDebugInfo.h"
2009-06-02 13:58:47 -04:00
#include "CodeGenFunction.h"
2010-02-16 04:31:36 -05:00
#include "CGObjCRuntime.h"
2009-06-02 13:58:47 -04:00
#include "CodeGenModule.h"
#include "clang/AST/DeclObjC.h"
#include "llvm/Module.h"
#include "llvm/Target/TargetData.h"
#include <algorithm>
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
using namespace clang;
using namespace CodeGen;
llvm::Constant *CodeGenFunction::
2010-03-03 12:28:16 -05:00
BuildDescriptorBlockDecl(const BlockExpr *BE, bool BlockHasCopyDispose, CharUnits Size,
2009-06-02 13:58:47 -04:00
const llvm::StructType* Ty,
std::vector<HelperInfo> *NoteForHelper) {
const llvm::Type *UnsignedLongTy
= CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
llvm::Constant *C;
std::vector<llvm::Constant*> Elts;
// reserved
C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Elts.push_back(C);
// Size
// FIXME: What is the right way to say this doesn't fit? We should give
// a user diagnostic in that case. Better fix would be to change the
// API to size_t.
2010-01-15 10:39:40 -05:00
C = llvm::ConstantInt::get(UnsignedLongTy, Size.getQuantity());
2009-06-02 13:58:47 -04:00
Elts.push_back(C);
2010-03-03 12:28:16 -05:00
// optional copy/dispose helpers
2009-06-02 13:58:47 -04:00
if (BlockHasCopyDispose) {
// copy_func_helper_decl
Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
// destroy_func_decl
Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
}
2010-03-03 12:28:16 -05:00
// Signature. non-optional ObjC-style method descriptor @encode sequence
std::string BlockTypeEncoding;
CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
Elts.push_back(llvm::ConstantExpr::getBitCast(
CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty));
// Layout.
C = llvm::ConstantInt::get(UnsignedLongTy, 0);
Elts.push_back(C);
2009-10-14 14:03:49 -04:00
C = llvm::ConstantStruct::get(VMContext, Elts, false);
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
2009-06-02 13:58:47 -04:00
llvm::GlobalValue::InternalLinkage,
2009-10-14 14:03:49 -04:00
C, "__block_descriptor_tmp");
2009-06-02 13:58:47 -04:00
return C;
}
llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
if (NSConcreteGlobalBlock == 0)
2009-10-14 14:03:49 -04:00
NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
2009-06-02 13:58:47 -04:00
"_NSConcreteGlobalBlock");
return NSConcreteGlobalBlock;
}
llvm::Constant *BlockModule::getNSConcreteStackBlock() {
if (NSConcreteStackBlock == 0)
2009-10-14 14:03:49 -04:00
NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
2009-06-02 13:58:47 -04:00
"_NSConcreteStackBlock");
return NSConcreteStackBlock;
}
2009-10-23 10:22:18 -04:00
static void CollectBlockDeclRefInfo(
const Stmt *S, CodeGenFunction::BlockInfo &Info,
llvm::SmallSet<const DeclContext *, 16> &InnerContexts) {
2009-06-02 13:58:47 -04:00
for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
I != E; ++I)
if (*I)
2009-10-23 10:22:18 -04:00
CollectBlockDeclRefInfo(*I, Info, InnerContexts);
2009-06-02 13:58:47 -04:00
2009-10-23 10:22:18 -04:00
// We want to ensure we walk down into block literals so we can find
// all nested BlockDeclRefExprs.
if (const BlockExpr *BE = dyn_cast<BlockExpr>(S)) {
InnerContexts.insert(cast<DeclContext>(BE->getBlockDecl()));
CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
}
if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) {
2009-06-02 13:58:47 -04:00
// FIXME: Handle enums.
2009-10-23 10:22:18 -04:00
if (isa<FunctionDecl>(BDRE->getDecl()))
2009-06-02 13:58:47 -04:00
return;
2009-10-23 10:22:18 -04:00
// Only Decls that escape are added.
if (!InnerContexts.count(BDRE->getDecl()->getDeclContext()))
Info.DeclRefs.push_back(BDRE);
2009-06-02 13:58:47 -04:00
}
}
/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
/// declared as a global variable instead of on the stack.
static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
2009-10-23 10:22:18 -04:00
return Info.DeclRefs.empty();
}
/// AllocateAllBlockDeclRefs - Preallocate all nested BlockDeclRefExprs to
/// ensure we can generate the debug information for the parameter for the block
/// invoke function.
static void AllocateAllBlockDeclRefs(const CodeGenFunction::BlockInfo &Info,
CodeGenFunction *CGF) {
// FIXME: Also always forward the this pointer in C++ as well.
for (size_t i = 0; i < Info.DeclRefs.size(); ++i)
CGF->AllocateBlockDecl(Info.DeclRefs[i]);
2009-06-02 13:58:47 -04:00
}
// FIXME: Push most into CGM, passing down a few bits, like current function
// name.
llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
std::string Name = CurFn->getName();
CodeGenFunction::BlockInfo Info(0, Name.c_str());
2009-10-23 10:22:18 -04:00
llvm::SmallSet<const DeclContext *, 16> InnerContexts;
InnerContexts.insert(BE->getBlockDecl());
CollectBlockDeclRefInfo(BE->getBody(), Info, InnerContexts);
2009-06-02 13:58:47 -04:00
// Check if the block can be global.
// FIXME: This test doesn't work for nested blocks yet. Longer term, I'd like
// to just have one code path. We should move this function into CGM and pass
// CGF, then we can just check to see if CGF is 0.
if (0 && CanBlockBeGlobal(Info))
return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
2009-11-18 09:59:57 -05:00
size_t BlockFields = 5;
std::vector<llvm::Constant*> Elts(BlockFields);
2009-06-02 13:58:47 -04:00
llvm::Constant *C;
llvm::Value *V;
{
// C = BuildBlockStructInitlist();
2010-03-06 04:23:02 -05:00
unsigned int flags = BLOCK_HAS_SIGNATURE;
2009-11-18 09:59:57 -05:00
2009-06-02 13:58:47 -04:00
// We run this first so that we set BlockHasCopyDispose from the entire
// block literal.
// __invoke
2010-01-15 10:39:40 -05:00
CharUnits subBlockSize;
2010-02-16 04:31:36 -05:00
CharUnits subBlockAlign;
2009-06-02 13:58:47 -04:00
llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
bool subBlockHasCopyDispose = false;
llvm::Function *Fn
2009-10-14 14:03:49 -04:00
= CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl,
LocalDeclMap,
2009-06-02 13:58:47 -04:00
subBlockSize,
subBlockAlign,
subBlockDeclRefDecls,
subBlockHasCopyDispose);
BlockHasCopyDispose |= subBlockHasCopyDispose;
Elts[3] = Fn;
// FIXME: Don't use BlockHasCopyDispose, it is set more often then
// necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
if (subBlockHasCopyDispose)
flags |= BLOCK_HAS_COPY_DISPOSE;
// __isa
C = CGM.getNSConcreteStackBlock();
C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
Elts[0] = C;
// __flags
2010-03-06 04:23:02 -05:00
{
QualType BPT = BE->getType();
const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
QualType ResultType = ftype->getResultType();
CallArgList Args;
CodeGenTypes &Types = CGM.getTypes();
const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, Args,
CC_Default, false);
if (CGM.ReturnTypeUsesSret(FnInfo))
flags |= BLOCK_USE_STRET;
}
2009-06-02 13:58:47 -04:00
const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
CGM.getTypes().ConvertType(CGM.getContext().IntTy));
C = llvm::ConstantInt::get(IntTy, flags);
Elts[1] = C;
// __reserved
C = llvm::ConstantInt::get(IntTy, 0);
Elts[2] = C;
if (subBlockDeclRefDecls.size() == 0) {
// __descriptor
2010-03-03 12:28:16 -05:00
Elts[4] = BuildDescriptorBlockDecl(BE, subBlockHasCopyDispose, subBlockSize,
2009-10-23 10:22:18 -04:00
0, 0);
2009-06-02 13:58:47 -04:00
// Optimize to being a global block.
Elts[0] = CGM.getNSConcreteGlobalBlock();
2010-03-06 04:23:02 -05:00
2009-06-02 13:58:47 -04:00
Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
2009-10-14 14:03:49 -04:00
C = llvm::ConstantStruct::get(VMContext, Elts, false);
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
2010-01-23 06:10:26 -05:00
llvm::GlobalValue::InternalLinkage, C,
"__block_holder_tmp_" +
llvm::Twine(CGM.getGlobalUniqueCount()));
2009-06-02 13:58:47 -04:00
QualType BPT = BE->getType();
C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
return C;
}
2009-11-18 09:59:57 -05:00
std::vector<const llvm::Type *> Types(BlockFields+subBlockDeclRefDecls.size());
2009-06-02 13:58:47 -04:00
for (int i=0; i<4; ++i)
Types[i] = Elts[i]->getType();
Types[4] = PtrToInt8Ty;
for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
const Expr *E = subBlockDeclRefDecls[i];
const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
QualType Ty = E->getType();
if (BDRE && BDRE->isByRef()) {
2009-11-18 09:59:57 -05:00
Types[i+BlockFields] = llvm::PointerType::get(BuildByRefType(BDRE->getDecl()), 0);
2009-06-02 13:58:47 -04:00
} else
2009-11-18 09:59:57 -05:00
Types[i+BlockFields] = ConvertType(Ty);
2009-06-02 13:58:47 -04:00
}
2009-10-14 14:03:49 -04:00
llvm::StructType *Ty = llvm::StructType::get(VMContext, Types, true);
2009-06-02 13:58:47 -04:00
llvm::AllocaInst *A = CreateTempAlloca(Ty);
2010-02-16 04:31:36 -05:00
A->setAlignment(subBlockAlign.getQuantity());
2009-06-02 13:58:47 -04:00
V = A;
std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
int helpersize = 0;
for (unsigned i=0; i<4; ++i)
Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
{
// FIXME: Push const down.
Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
DeclRefExpr *DR;
ValueDecl *VD;
DR = dyn_cast<DeclRefExpr>(E);
// Skip padding.
if (DR) continue;
BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
VD = BDRE->getDecl();
2009-11-18 09:59:57 -05:00
llvm::Value* Addr = Builder.CreateStructGEP(V, i+BlockFields, "tmp");
2009-06-02 13:58:47 -04:00
NoteForHelper[helpersize].index = i+5;
2009-10-14 14:03:49 -04:00
NoteForHelper[helpersize].RequiresCopying
= BlockRequiresCopying(VD->getType());
2009-06-02 13:58:47 -04:00
NoteForHelper[helpersize].flag
2009-10-14 14:03:49 -04:00
= (VD->getType()->isBlockPointerType()
? BLOCK_FIELD_IS_BLOCK
: BLOCK_FIELD_IS_OBJECT);
2009-06-02 13:58:47 -04:00
if (LocalDeclMap[VD]) {
if (BDRE->isByRef()) {
NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
// FIXME: Someone double check this.
(VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
llvm::Value *Loc = LocalDeclMap[VD];
Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
2009-12-01 06:08:04 -05:00
Loc = Builder.CreateLoad(Loc);
2009-06-02 13:58:47 -04:00
Builder.CreateStore(Loc, Addr);
++helpersize;
continue;
} else
2009-12-15 13:49:47 -05:00
E = new (getContext()) DeclRefExpr (VD,
2009-12-01 06:08:04 -05:00
VD->getType(),
SourceLocation());
2009-06-02 13:58:47 -04:00
}
if (BDRE->isByRef()) {
NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
// FIXME: Someone double check this.
(VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
E = new (getContext())
UnaryOperator(E, UnaryOperator::AddrOf,
getContext().getPointerType(E->getType()),
SourceLocation());
}
++helpersize;
RValue r = EmitAnyExpr(E, Addr, false);
if (r.isScalar()) {
llvm::Value *Loc = r.getScalarVal();
2009-11-18 09:59:57 -05:00
const llvm::Type *Ty = Types[i+BlockFields];
2009-06-02 13:58:47 -04:00
if (BDRE->isByRef()) {
// E is now the address of the value field, instead, we want the
// address of the actual ByRef struct. We optimize this slightly
// compared to gcc by not grabbing the forwarding slot as this must
// be done during Block_copy for us, and we can postpone the work
// until then.
2010-01-15 10:39:40 -05:00
CharUnits offset = BlockDecls[BDRE->getDecl()];
2009-06-02 13:58:47 -04:00
llvm::Value *BlockLiteral = LoadBlockStruct();
Loc = Builder.CreateGEP(BlockLiteral,
2009-10-23 10:22:18 -04:00
llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
2010-01-15 10:39:40 -05:00
offset.getQuantity()),
2009-06-02 13:58:47 -04:00
"block.literal");
Ty = llvm::PointerType::get(Ty, 0);
Loc = Builder.CreateBitCast(Loc, Ty);
2009-12-01 06:08:04 -05:00
Loc = Builder.CreateLoad(Loc);
2009-06-02 13:58:47 -04:00
// Loc = Builder.CreateBitCast(Loc, Ty);
}
Builder.CreateStore(Loc, Addr);
} else if (r.isComplex())
// FIXME: implement
ErrorUnsupported(BE, "complex in block literal");
else if (r.isAggregate())
; // Already created into the destination
else
assert (0 && "bad block variable");
// FIXME: Ensure that the offset created by the backend for
// the struct matches the previously computed offset in BlockDecls.
}
NoteForHelper.resize(helpersize);
// __descriptor
2010-03-03 12:28:16 -05:00
llvm::Value *Descriptor = BuildDescriptorBlockDecl(BE,
subBlockHasCopyDispose,
2009-06-02 13:58:47 -04:00
subBlockSize, Ty,
&NoteForHelper);
Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
}
QualType BPT = BE->getType();
2010-02-16 04:31:36 -05:00
V = Builder.CreateBitCast(V, ConvertType(BPT));
// See if this is a __weak block variable and the must call objc_read_weak
// on it.
const FunctionType *ftype = BPT->getPointeeType()->getAs<FunctionType>();
QualType RES = ftype->getResultType();
if (RES.isObjCGCWeak()) {
// Must cast argument to id*
const llvm::Type *ObjectPtrTy =
ConvertType(CGM.getContext().getObjCIdType());
const llvm::Type *PtrObjectPtrTy =
llvm::PointerType::getUnqual(ObjectPtrTy);
V = Builder.CreateBitCast(V, PtrObjectPtrTy);
V = CGM.getObjCRuntime().EmitObjCWeakRead(*this, V);
}
return V;
2009-06-02 13:58:47 -04:00
}
const llvm::Type *BlockModule::getBlockDescriptorType() {
if (BlockDescriptorType)
return BlockDescriptorType;
const llvm::Type *UnsignedLongTy =
getTypes().ConvertType(getContext().UnsignedLongTy);
// struct __block_descriptor {
// unsigned long reserved;
// unsigned long block_size;
2010-03-03 12:28:16 -05:00
//
// // later, the following will be added
//
// struct {
// void (*copyHelper)();
// void (*copyHelper)();
// } helpers; // !!! optional
//
// const char *signature; // the block signature
// const char *layout; // reserved
2009-06-02 13:58:47 -04:00
// };
2009-10-14 14:03:49 -04:00
BlockDescriptorType = llvm::StructType::get(UnsignedLongTy->getContext(),
UnsignedLongTy,
2009-06-02 13:58:47 -04:00
UnsignedLongTy,
NULL);
getModule().addTypeName("struct.__block_descriptor",
BlockDescriptorType);
return BlockDescriptorType;
}
const llvm::Type *BlockModule::getGenericBlockLiteralType() {
if (GenericBlockLiteralType)
return GenericBlockLiteralType;
const llvm::Type *BlockDescPtrTy =
llvm::PointerType::getUnqual(getBlockDescriptorType());
const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
getTypes().ConvertType(getContext().IntTy));
// struct __block_literal_generic {
// void *__isa;
// int __flags;
// int __reserved;
// void (*__invoke)(void *);
// struct __block_descriptor *__descriptor;
// };
2010-03-03 12:28:16 -05:00
GenericBlockLiteralType = llvm::StructType::get(IntTy->getContext(),
2009-10-14 14:03:49 -04:00
PtrToInt8Ty,
2009-06-02 13:58:47 -04:00
IntTy,
IntTy,
PtrToInt8Ty,
BlockDescPtrTy,
NULL);
getModule().addTypeName("struct.__block_literal_generic",
GenericBlockLiteralType);
return GenericBlockLiteralType;
}
2010-01-01 05:34:51 -05:00
RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E,
ReturnValueSlot ReturnValue) {
2009-06-02 13:58:47 -04:00
const BlockPointerType *BPT =
2009-10-14 14:03:49 -04:00
E->getCallee()->getType()->getAs<BlockPointerType>();
2009-06-02 13:58:47 -04:00
llvm::Value *Callee = EmitScalarExpr(E->getCallee());
// Get a pointer to the generic block literal.
const llvm::Type *BlockLiteralTy =
llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
// Bitcast the callee to a block literal.
llvm::Value *BlockLiteral =
Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
// Get the function pointer from the literal.
llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
BlockLiteral =
Builder.CreateBitCast(BlockLiteral,
2009-10-14 14:03:49 -04:00
llvm::Type::getInt8PtrTy(VMContext),
2009-06-02 13:58:47 -04:00
"tmp");
// Add the block literal.
QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
CallArgList Args;
Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
QualType FnType = BPT->getPointeeType();
// And the rest of the arguments.
2009-10-14 14:03:49 -04:00
EmitCallArgs(Args, FnType->getAs<FunctionProtoType>(),
2009-06-02 13:58:47 -04:00
E->arg_begin(), E->arg_end());
// Load the function.
2009-12-01 06:08:04 -05:00
llvm::Value *Func = Builder.CreateLoad(FuncPtr, "tmp");
2009-06-02 13:58:47 -04:00
2010-02-16 04:31:36 -05:00
const FunctionType *FuncTy = FnType->getAs<FunctionType>();
QualType ResultType = FuncTy->getResultType();
2009-06-02 13:58:47 -04:00
2009-10-14 14:03:49 -04:00
const CGFunctionInfo &FnInfo =
2010-02-16 04:31:36 -05:00
CGM.getTypes().getFunctionInfo(ResultType, Args, FuncTy->getCallConv(),
FuncTy->getNoReturnAttr());
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Cast the function pointer to the right type.
2009-10-14 14:03:49 -04:00
const llvm::Type *BlockFTy =
2009-06-02 13:58:47 -04:00
CGM.getTypes().GetFunctionType(FnInfo, false);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
Func = Builder.CreateBitCast(Func, BlockFTyPtr);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// And call the block.
2010-01-01 05:34:51 -05:00
return EmitCall(FnInfo, Func, ReturnValue, Args);
2009-06-02 13:58:47 -04:00
}
2010-01-15 10:39:40 -05:00
CharUnits CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
2009-10-14 14:03:49 -04:00
const ValueDecl *VD = E->getDecl();
2010-01-15 10:39:40 -05:00
CharUnits &offset = BlockDecls[VD];
2009-06-02 13:58:47 -04:00
// See if we have already allocated an offset for this variable.
2010-01-15 10:39:40 -05:00
if (offset.isPositive())
2009-10-23 10:22:18 -04:00
return offset;
// Don't run the expensive check, unless we have to.
if (!BlockHasCopyDispose)
if (E->isByRef()
|| BlockRequiresCopying(E->getType()))
2009-06-02 13:58:47 -04:00
BlockHasCopyDispose = true;
2009-10-23 10:22:18 -04:00
// if not, allocate one now.
offset = getBlockOffset(E);
return offset;
}
llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
const ValueDecl *VD = E->getDecl();
2010-01-15 10:39:40 -05:00
CharUnits offset = AllocateBlockDecl(E);
2009-10-23 10:22:18 -04:00
2009-06-02 13:58:47 -04:00
llvm::Value *BlockLiteral = LoadBlockStruct();
llvm::Value *V = Builder.CreateGEP(BlockLiteral,
2009-10-23 10:22:18 -04:00
llvm::ConstantInt::get(llvm::Type::getInt64Ty(VMContext),
2010-01-15 10:39:40 -05:00
offset.getQuantity()),
2009-06-02 13:58:47 -04:00
"block.literal");
if (E->isByRef()) {
const llvm::Type *PtrStructTy
2009-10-14 14:03:49 -04:00
= llvm::PointerType::get(BuildByRefType(VD), 0);
2009-06-02 13:58:47 -04:00
// The block literal will need a copy/destroy helper.
BlockHasCopyDispose = true;
2009-10-14 14:03:49 -04:00
const llvm::Type *Ty = PtrStructTy;
2009-06-02 13:58:47 -04:00
Ty = llvm::PointerType::get(Ty, 0);
V = Builder.CreateBitCast(V, Ty);
2009-12-01 06:08:04 -05:00
V = Builder.CreateLoad(V);
2009-06-02 13:58:47 -04:00
V = Builder.CreateStructGEP(V, 1, "forwarding");
2009-12-01 06:08:04 -05:00
V = Builder.CreateLoad(V);
2009-06-02 13:58:47 -04:00
V = Builder.CreateBitCast(V, PtrStructTy);
2009-10-14 14:03:49 -04:00
V = Builder.CreateStructGEP(V, getByRefValueLLVMField(VD),
VD->getNameAsString());
2009-06-02 13:58:47 -04:00
} else {
2009-10-14 14:03:49 -04:00
const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
2009-06-02 13:58:47 -04:00
Ty = llvm::PointerType::get(Ty, 0);
V = Builder.CreateBitCast(V, Ty);
}
return V;
}
void CodeGenFunction::BlockForwardSelf() {
const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
ImplicitParamDecl *SelfDecl = OMD->getSelfDecl();
llvm::Value *&DMEntry = LocalDeclMap[SelfDecl];
if (DMEntry)
return;
// FIXME - Eliminate BlockDeclRefExprs, clients don't need/want to care
BlockDeclRefExpr *BDRE = new (getContext())
BlockDeclRefExpr(SelfDecl,
SelfDecl->getType(), SourceLocation(), false);
DMEntry = GetAddrOfBlockDecl(BDRE);
}
llvm::Constant *
BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
// Generate the block descriptor.
const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
getTypes().ConvertType(getContext().IntTy));
2010-03-03 12:28:16 -05:00
llvm::Constant *DescriptorFields[4];
2009-06-02 13:58:47 -04:00
// Reserved
DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
// Block literal size. For global blocks we just use the size of the generic
// block literal struct.
2010-02-16 04:31:36 -05:00
CharUnits BlockLiteralSize =
CGM.GetTargetTypeStoreSize(getGenericBlockLiteralType());
2009-10-14 14:03:49 -04:00
DescriptorFields[1] =
2010-01-15 10:39:40 -05:00
llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize.getQuantity());
2010-03-03 12:28:16 -05:00
// signature. non-optional ObjC-style method descriptor @encode sequence
std::string BlockTypeEncoding;
CGM.getContext().getObjCEncodingForBlock(BE, BlockTypeEncoding);
2009-06-02 13:58:47 -04:00
2010-03-03 12:28:16 -05:00
DescriptorFields[2] = llvm::ConstantExpr::getBitCast(
CGM.GetAddrOfConstantCString(BlockTypeEncoding), PtrToInt8Ty);
// layout
DescriptorFields[3] =
llvm::ConstantInt::get(UnsignedLongTy,0);
// build the structure from the 4 elements
2009-06-02 13:58:47 -04:00
llvm::Constant *DescriptorStruct =
2010-03-03 12:28:16 -05:00
llvm::ConstantStruct::get(VMContext, &DescriptorFields[0], 4, false);
2009-06-02 13:58:47 -04:00
llvm::GlobalVariable *Descriptor =
2009-10-14 14:03:49 -04:00
new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
2009-06-02 13:58:47 -04:00
llvm::GlobalVariable::InternalLinkage,
2009-10-14 14:03:49 -04:00
DescriptorStruct, "__block_descriptor_global");
2009-06-02 13:58:47 -04:00
2009-11-18 09:59:57 -05:00
int FieldCount = 5;
2009-06-02 13:58:47 -04:00
// Generate the constants for the block literal.
2009-11-18 09:59:57 -05:00
std::vector<llvm::Constant*> LiteralFields(FieldCount);
2009-06-02 13:58:47 -04:00
CodeGenFunction::BlockInfo Info(0, n);
2010-01-15 10:39:40 -05:00
CharUnits subBlockSize;
2010-02-16 04:31:36 -05:00
CharUnits subBlockAlign;
2009-06-02 13:58:47 -04:00
llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
bool subBlockHasCopyDispose = false;
llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
llvm::Function *Fn
= CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
subBlockSize,
subBlockAlign,
subBlockDeclRefDecls,
subBlockHasCopyDispose);
assert(subBlockSize == BlockLiteralSize
&& "no imports allowed for global block");
// isa
LiteralFields[0] = getNSConcreteGlobalBlock();
// Flags
2010-03-03 12:28:16 -05:00
LiteralFields[1] =
2010-03-06 04:23:02 -05:00
llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_SIGNATURE);
2009-06-02 13:58:47 -04:00
// Reserved
LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
// Function
LiteralFields[3] = Fn;
// Descriptor
LiteralFields[4] = Descriptor;
2009-11-18 09:59:57 -05:00
2009-06-02 13:58:47 -04:00
llvm::Constant *BlockLiteralStruct =
2009-11-18 09:59:57 -05:00
llvm::ConstantStruct::get(VMContext, LiteralFields, false);
2009-06-02 13:58:47 -04:00
llvm::GlobalVariable *BlockLiteral =
2009-10-14 14:03:49 -04:00
new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
2009-06-02 13:58:47 -04:00
llvm::GlobalVariable::InternalLinkage,
2009-10-14 14:03:49 -04:00
BlockLiteralStruct, "__block_literal_global");
2009-06-02 13:58:47 -04:00
return BlockLiteral;
}
llvm::Value *CodeGenFunction::LoadBlockStruct() {
2009-10-23 10:22:18 -04:00
llvm::Value *V = Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()],
"self");
// For now, we codegen based upon byte offsets.
return Builder.CreateBitCast(V, PtrToInt8Ty);
2009-06-02 13:58:47 -04:00
}
llvm::Function *
CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
const BlockInfo& Info,
const Decl *OuterFuncDecl,
llvm::DenseMap<const Decl*, llvm::Value*> ldm,
2010-01-15 10:39:40 -05:00
CharUnits &Size,
2010-02-16 04:31:36 -05:00
CharUnits &Align,
2009-06-02 13:58:47 -04:00
llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
bool &subBlockHasCopyDispose) {
// Check if we should generate debug info for this block.
if (CGM.getDebugInfo())
DebugInfo = CGM.getDebugInfo();
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
// Arrange for local static and local extern declarations to appear
// to be local to this function as well, as they are directly referenced
// in a block.
for (llvm::DenseMap<const Decl *, llvm::Value*>::iterator i = ldm.begin();
i != ldm.end();
++i) {
const VarDecl *VD = dyn_cast<VarDecl>(i->first);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
LocalDeclMap[VD] = i->second;
}
2010-02-16 04:31:36 -05:00
BlockOffset =
CGM.GetTargetTypeStoreSize(CGM.getGenericBlockLiteralType());
BlockAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
2009-06-02 13:58:47 -04:00
const FunctionType *BlockFunctionType = BExpr->getFunctionType();
QualType ResultType;
2010-02-16 04:31:36 -05:00
CallingConv CC = BlockFunctionType->getCallConv();
bool NoReturn = BlockFunctionType->getNoReturnAttr();
2009-06-02 13:58:47 -04:00
bool IsVariadic;
2009-10-14 14:03:49 -04:00
if (const FunctionProtoType *FTy =
2009-06-02 13:58:47 -04:00
dyn_cast<FunctionProtoType>(BlockFunctionType)) {
ResultType = FTy->getResultType();
IsVariadic = FTy->isVariadic();
2009-10-14 14:03:49 -04:00
} else {
2009-06-02 13:58:47 -04:00
// K&R style block.
ResultType = BlockFunctionType->getResultType();
IsVariadic = false;
}
FunctionArgList Args;
2009-10-23 10:22:18 -04:00
CurFuncDecl = OuterFuncDecl;
2009-06-02 13:58:47 -04:00
const BlockDecl *BD = BExpr->getBlockDecl();
2009-10-23 10:22:18 -04:00
IdentifierInfo *II = &CGM.getContext().Idents.get(".block_descriptor");
// Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
AllocateAllBlockDeclRefs(Info, this);
QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
BlockDeclRefDecls);
2009-06-02 13:58:47 -04:00
// FIXME: This leaks
ImplicitParamDecl *SelfDecl =
ImplicitParamDecl::Create(getContext(), 0,
2009-10-23 10:22:18 -04:00
SourceLocation(), II,
ParmTy);
2009-06-02 13:58:47 -04:00
Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
BlockStructDecl = SelfDecl;
for (BlockDecl::param_const_iterator i = BD->param_begin(),
e = BD->param_end(); i != e; ++i)
Args.push_back(std::make_pair(*i, (*i)->getType()));
const CGFunctionInfo &FI =
2010-02-16 04:31:36 -05:00
CGM.getTypes().getFunctionInfo(ResultType, Args, CC, NoReturn);
2009-06-02 13:58:47 -04:00
CodeGenTypes &Types = CGM.getTypes();
const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
llvm::Function *Fn =
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2010-01-23 06:10:26 -05:00
llvm::Twine("__") + Info.Name + "_block_invoke_",
2009-06-02 13:58:47 -04:00
&CGM.getModule());
CGM.SetInternalFunctionAttributes(BD, Fn, FI);
StartFunction(BD, ResultType, Fn, Args,
BExpr->getBody()->getLocEnd());
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
CurFuncDecl = OuterFuncDecl;
CurCodeDecl = BD;
2009-10-14 14:03:49 -04:00
// Save a spot to insert the debug information for all the BlockDeclRefDecls.
llvm::BasicBlock *entry = Builder.GetInsertBlock();
llvm::BasicBlock::iterator entry_ptr = Builder.GetInsertPoint();
--entry_ptr;
2009-06-02 13:58:47 -04:00
EmitStmt(BExpr->getBody());
2009-10-14 14:03:49 -04:00
// Remember where we were...
llvm::BasicBlock *resume = Builder.GetInsertBlock();
// Go back to the entry.
++entry_ptr;
Builder.SetInsertPoint(entry, entry_ptr);
if (CGDebugInfo *DI = getDebugInfo()) {
// Emit debug information for all the BlockDeclRefDecls.
2010-02-16 04:31:36 -05:00
for (unsigned i = 0, e = BlockDeclRefDecls.size(); i != e; ++i) {
if (const BlockDeclRefExpr *BDRE =
dyn_cast<BlockDeclRefExpr>(BlockDeclRefDecls[i])) {
2009-10-14 14:03:49 -04:00
const ValueDecl *D = BDRE->getDecl();
DI->setLocation(D->getLocation());
DI->EmitDeclareOfBlockDeclRefVariable(BDRE,
LocalDeclMap[getBlockStructDecl()],
Builder, this);
}
}
}
// And resume where we left off.
if (resume == 0)
Builder.ClearInsertionPoint();
else
Builder.SetInsertPoint(resume);
2009-06-02 13:58:47 -04:00
FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
// The runtime needs a minimum alignment of a void *.
2010-02-16 04:31:36 -05:00
CharUnits MinAlign = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
2010-01-15 10:39:40 -05:00
BlockOffset = CharUnits::fromQuantity(
2010-02-16 04:31:36 -05:00
llvm::RoundUpToAlignment(BlockOffset.getQuantity(),
MinAlign.getQuantity()));
2009-06-02 13:58:47 -04:00
Size = BlockOffset;
Align = BlockAlign;
subBlockDeclRefDecls = BlockDeclRefDecls;
subBlockHasCopyDispose |= BlockHasCopyDispose;
return Fn;
}
2010-01-15 10:39:40 -05:00
CharUnits BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
2009-06-02 13:58:47 -04:00
const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
2010-01-15 10:39:40 -05:00
CharUnits Size = getContext().getTypeSizeInChars(D->getType());
2010-02-16 04:31:36 -05:00
CharUnits Align = getContext().getDeclAlign(D);
2009-06-02 13:58:47 -04:00
if (BDRE->isByRef()) {
2010-01-15 10:39:40 -05:00
Size = getContext().getTypeSizeInChars(getContext().VoidPtrTy);
2010-02-16 04:31:36 -05:00
Align = getContext().getTypeAlignInChars(getContext().VoidPtrTy);
2009-06-02 13:58:47 -04:00
}
2010-02-16 04:31:36 -05:00
assert ((Align.isPositive()) && "alignment must be 1 byte or more");
2009-06-02 13:58:47 -04:00
2010-01-15 10:39:40 -05:00
CharUnits OldOffset = BlockOffset;
2009-06-02 13:58:47 -04:00
// Ensure proper alignment, even if it means we have to have a gap
2010-01-15 10:39:40 -05:00
BlockOffset = CharUnits::fromQuantity(
2010-02-16 04:31:36 -05:00
llvm::RoundUpToAlignment(BlockOffset.getQuantity(), Align.getQuantity()));
2009-06-02 13:58:47 -04:00
BlockAlign = std::max(Align, BlockAlign);
2010-01-15 10:39:40 -05:00
CharUnits Pad = BlockOffset - OldOffset;
if (Pad.isPositive()) {
llvm::ArrayType::get(llvm::Type::getInt8Ty(VMContext), Pad.getQuantity());
2009-06-02 13:58:47 -04:00
QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
2010-01-15 10:39:40 -05:00
llvm::APInt(32,
Pad.getQuantity()),
2009-06-02 13:58:47 -04:00
ArrayType::Normal, 0);
ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
2009-10-14 14:03:49 -04:00
0, QualType(PadTy), 0, VarDecl::None);
2009-06-02 13:58:47 -04:00
Expr *E;
E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
2009-12-01 06:08:04 -05:00
SourceLocation());
2009-06-02 13:58:47 -04:00
BlockDeclRefDecls.push_back(E);
}
BlockDeclRefDecls.push_back(BDRE);
BlockOffset += Size;
return BlockOffset-Size;
}
llvm::Constant *BlockFunction::
GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
std::vector<HelperInfo> *NoteForHelperp) {
QualType R = getContext().VoidTy;
FunctionArgList Args;
// FIXME: This leaks
ImplicitParamDecl *Dst =
2009-10-23 10:22:18 -04:00
ImplicitParamDecl::Create(getContext(), 0,
SourceLocation(), 0,
2009-06-02 13:58:47 -04:00
getContext().getPointerType(getContext().VoidTy));
Args.push_back(std::make_pair(Dst, Dst->getType()));
ImplicitParamDecl *Src =
2009-10-23 10:22:18 -04:00
ImplicitParamDecl::Create(getContext(), 0,
SourceLocation(), 0,
2009-06-02 13:58:47 -04:00
getContext().getPointerType(getContext().VoidTy));
Args.push_back(std::make_pair(Src, Src->getType()));
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
const CGFunctionInfo &FI =
2010-02-16 04:31:36 -05:00
CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
2009-06-02 13:58:47 -04:00
2009-06-06 04:21:31 -04:00
// FIXME: We'd like to put these into a mergable by content, with
// internal linkage.
2009-06-02 13:58:47 -04:00
CodeGenTypes &Types = CGM.getTypes();
const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
llvm::Function *Fn =
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2010-01-23 06:10:26 -05:00
"__copy_helper_block_", &CGM.getModule());
2009-06-02 13:58:47 -04:00
IdentifierInfo *II
= &CGM.getContext().Idents.get("__copy_helper_block_");
FunctionDecl *FD = FunctionDecl::Create(getContext(),
getContext().getTranslationUnitDecl(),
2009-10-14 14:03:49 -04:00
SourceLocation(), II, R, 0,
2009-06-02 13:58:47 -04:00
FunctionDecl::Static, false,
true);
CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
llvm::Type *PtrPtrT;
if (NoteForHelperp) {
std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
SrcObj = Builder.CreateLoad(SrcObj);
llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
llvm::Type *PtrPtrT;
PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
DstObj = Builder.CreateLoad(DstObj);
for (unsigned i=0; i < NoteForHelper.size(); ++i) {
int flag = NoteForHelper[i].flag;
int index = NoteForHelper[i].index;
if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
|| NoteForHelper[i].RequiresCopying) {
llvm::Value *Srcv = SrcObj;
Srcv = Builder.CreateStructGEP(Srcv, index);
Srcv = Builder.CreateBitCast(Srcv,
llvm::PointerType::get(PtrToInt8Ty, 0));
Srcv = Builder.CreateLoad(Srcv);
llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
2009-10-14 14:03:49 -04:00
llvm::Value *N = llvm::ConstantInt::get(
llvm::Type::getInt32Ty(T->getContext()), flag);
2009-06-02 13:58:47 -04:00
llvm::Value *F = getBlockObjectAssign();
Builder.CreateCall3(F, Dstv, Srcv, N);
}
}
}
CGF.FinishFunction();
return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
}
llvm::Constant *BlockFunction::
GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
const llvm::StructType* T,
std::vector<HelperInfo> *NoteForHelperp) {
QualType R = getContext().VoidTy;
FunctionArgList Args;
// FIXME: This leaks
ImplicitParamDecl *Src =
2009-10-23 10:22:18 -04:00
ImplicitParamDecl::Create(getContext(), 0,
SourceLocation(), 0,
2009-06-02 13:58:47 -04:00
getContext().getPointerType(getContext().VoidTy));
Args.push_back(std::make_pair(Src, Src->getType()));
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
const CGFunctionInfo &FI =
2010-02-16 04:31:36 -05:00
CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
2009-06-02 13:58:47 -04:00
2009-06-06 04:21:31 -04:00
// FIXME: We'd like to put these into a mergable by content, with
// internal linkage.
2009-06-02 13:58:47 -04:00
CodeGenTypes &Types = CGM.getTypes();
const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
llvm::Function *Fn =
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2010-01-23 06:10:26 -05:00
"__destroy_helper_block_", &CGM.getModule());
2009-06-02 13:58:47 -04:00
IdentifierInfo *II
= &CGM.getContext().Idents.get("__destroy_helper_block_");
FunctionDecl *FD = FunctionDecl::Create(getContext(),
getContext().getTranslationUnitDecl(),
2009-10-14 14:03:49 -04:00
SourceLocation(), II, R, 0,
2009-06-02 13:58:47 -04:00
FunctionDecl::Static, false,
true);
CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
if (NoteForHelperp) {
std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
llvm::Type *PtrPtrT;
PtrPtrT = llvm::PointerType::get(llvm::PointerType::get(T, 0), 0);
SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
SrcObj = Builder.CreateLoad(SrcObj);
for (unsigned i=0; i < NoteForHelper.size(); ++i) {
int flag = NoteForHelper[i].flag;
int index = NoteForHelper[i].index;
if ((NoteForHelper[i].flag & BLOCK_FIELD_IS_BYREF)
|| NoteForHelper[i].RequiresCopying) {
llvm::Value *Srcv = SrcObj;
Srcv = Builder.CreateStructGEP(Srcv, index);
Srcv = Builder.CreateBitCast(Srcv,
llvm::PointerType::get(PtrToInt8Ty, 0));
Srcv = Builder.CreateLoad(Srcv);
BuildBlockRelease(Srcv, flag);
}
}
}
CGF.FinishFunction();
return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
}
llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
std::vector<HelperInfo> *NoteForHelper) {
return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
T, NoteForHelper);
}
llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
std::vector<HelperInfo> *NoteForHelperp) {
return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
T, NoteForHelperp);
}
llvm::Constant *BlockFunction::
GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
QualType R = getContext().VoidTy;
FunctionArgList Args;
// FIXME: This leaks
ImplicitParamDecl *Dst =
2009-10-23 10:22:18 -04:00
ImplicitParamDecl::Create(getContext(), 0,
SourceLocation(), 0,
2009-06-02 13:58:47 -04:00
getContext().getPointerType(getContext().VoidTy));
Args.push_back(std::make_pair(Dst, Dst->getType()));
// FIXME: This leaks
ImplicitParamDecl *Src =
2009-10-23 10:22:18 -04:00
ImplicitParamDecl::Create(getContext(), 0,
SourceLocation(), 0,
2009-06-02 13:58:47 -04:00
getContext().getPointerType(getContext().VoidTy));
Args.push_back(std::make_pair(Src, Src->getType()));
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
const CGFunctionInfo &FI =
2010-02-16 04:31:36 -05:00
CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
2009-06-02 13:58:47 -04:00
CodeGenTypes &Types = CGM.getTypes();
const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
2009-06-06 04:21:31 -04:00
// FIXME: We'd like to put these into a mergable by content, with
// internal linkage.
2009-06-02 13:58:47 -04:00
llvm::Function *Fn =
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2010-01-23 06:10:26 -05:00
"__Block_byref_id_object_copy_", &CGM.getModule());
2009-06-02 13:58:47 -04:00
IdentifierInfo *II
= &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
FunctionDecl *FD = FunctionDecl::Create(getContext(),
getContext().getTranslationUnitDecl(),
2009-10-14 14:03:49 -04:00
SourceLocation(), II, R, 0,
2009-06-02 13:58:47 -04:00
FunctionDecl::Static, false,
true);
CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
// dst->x
llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
V = Builder.CreateLoad(V);
V = Builder.CreateStructGEP(V, 6, "x");
llvm::Value *DstObj = Builder.CreateBitCast(V, PtrToInt8Ty);
// src->x
V = CGF.GetAddrOfLocalVar(Src);
V = Builder.CreateLoad(V);
V = Builder.CreateBitCast(V, T);
V = Builder.CreateStructGEP(V, 6, "x");
V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
llvm::Value *SrcObj = Builder.CreateLoad(V);
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
flag |= BLOCK_BYREF_CALLER;
2009-10-14 14:03:49 -04:00
llvm::Value *N = llvm::ConstantInt::get(
llvm::Type::getInt32Ty(T->getContext()), flag);
2009-06-02 13:58:47 -04:00
llvm::Value *F = getBlockObjectAssign();
Builder.CreateCall3(F, DstObj, SrcObj, N);
CGF.FinishFunction();
return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
}
llvm::Constant *
BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
int flag) {
QualType R = getContext().VoidTy;
FunctionArgList Args;
// FIXME: This leaks
ImplicitParamDecl *Src =
2009-10-23 10:22:18 -04:00
ImplicitParamDecl::Create(getContext(), 0,
SourceLocation(), 0,
2009-06-02 13:58:47 -04:00
getContext().getPointerType(getContext().VoidTy));
Args.push_back(std::make_pair(Src, Src->getType()));
2009-10-14 14:03:49 -04:00
2009-06-02 13:58:47 -04:00
const CGFunctionInfo &FI =
2010-02-16 04:31:36 -05:00
CGM.getTypes().getFunctionInfo(R, Args, CC_Default, false);
2009-06-02 13:58:47 -04:00
CodeGenTypes &Types = CGM.getTypes();
const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
2009-06-06 04:21:31 -04:00
// FIXME: We'd like to put these into a mergable by content, with
// internal linkage.
2009-06-02 13:58:47 -04:00
llvm::Function *Fn =
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
2010-01-23 06:10:26 -05:00
"__Block_byref_id_object_dispose_",
2009-06-02 13:58:47 -04:00
&CGM.getModule());
IdentifierInfo *II
= &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
FunctionDecl *FD = FunctionDecl::Create(getContext(),
getContext().getTranslationUnitDecl(),
2009-10-14 14:03:49 -04:00
SourceLocation(), II, R, 0,
2009-06-02 13:58:47 -04:00
FunctionDecl::Static, false,
true);
CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
V = Builder.CreateBitCast(V, llvm::PointerType::get(T, 0));
V = Builder.CreateLoad(V);
V = Builder.CreateStructGEP(V, 6, "x");
V = Builder.CreateBitCast(V, llvm::PointerType::get(PtrToInt8Ty, 0));
V = Builder.CreateLoad(V);
flag |= BLOCK_BYREF_CALLER;
BuildBlockRelease(V, flag);
CGF.FinishFunction();
return llvm::ConstantExpr::getBitCast(Fn, PtrToInt8Ty);
}
llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
2009-12-15 13:49:47 -05:00
int Flag, unsigned Align) {
// All alignments below that of pointer alignment collapse down to just
2009-06-06 04:21:31 -04:00
// pointer alignment, as we always have at least that much alignment to begin
// with.
Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
2009-12-15 13:49:47 -05:00
2009-06-06 04:21:31 -04:00
// As an optimization, we only generate a single function of each kind we
// might need. We need a different one for each alignment and for each
// setting of flags. We mix Align and flag to get the kind.
2009-12-15 13:49:47 -05:00
uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
llvm::Constant *&Entry = CGM.AssignCache[Kind];
2009-06-06 04:21:31 -04:00
if (Entry)
return Entry;
2009-12-15 13:49:47 -05:00
return Entry = CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, Flag);
2009-06-02 13:58:47 -04:00
}
llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
2009-12-15 13:49:47 -05:00
int Flag,
2009-06-06 04:21:31 -04:00
unsigned Align) {
// All alignments below that of pointer alignment collpase down to just
// pointer alignment, as we always have at least that much alignment to begin
// with.
Align /= unsigned(CGF.Target.getPointerAlign(0)/8);
2009-12-15 13:49:47 -05:00
2009-06-06 04:21:31 -04:00
// As an optimization, we only generate a single function of each kind we
// might need. We need a different one for each alignment and for each
// setting of flags. We mix Align and flag to get the kind.
2009-12-15 13:49:47 -05:00
uint64_t Kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + Flag;
llvm::Constant *&Entry = CGM.DestroyCache[Kind];
2009-06-06 04:21:31 -04:00
if (Entry)
return Entry;
2009-12-15 13:49:47 -05:00
return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, Flag);
2009-06-02 13:58:47 -04:00
}
llvm::Value *BlockFunction::getBlockObjectDispose() {
if (CGM.BlockObjectDispose == 0) {
const llvm::FunctionType *FTy;
std::vector<const llvm::Type*> ArgTys;
2009-10-14 14:03:49 -04:00
const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
2009-06-02 13:58:47 -04:00
ArgTys.push_back(PtrToInt8Ty);
2009-10-14 14:03:49 -04:00
ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
2009-06-02 13:58:47 -04:00
FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
CGM.BlockObjectDispose
= CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
}
return CGM.BlockObjectDispose;
}
llvm::Value *BlockFunction::getBlockObjectAssign() {
if (CGM.BlockObjectAssign == 0) {
const llvm::FunctionType *FTy;
std::vector<const llvm::Type*> ArgTys;
2009-10-14 14:03:49 -04:00
const llvm::Type *ResultType = llvm::Type::getVoidTy(VMContext);
2009-06-02 13:58:47 -04:00
ArgTys.push_back(PtrToInt8Ty);
ArgTys.push_back(PtrToInt8Ty);
2009-10-14 14:03:49 -04:00
ArgTys.push_back(llvm::Type::getInt32Ty(VMContext));
2009-06-02 13:58:47 -04:00
FTy = llvm::FunctionType::get(ResultType, ArgTys, false);
CGM.BlockObjectAssign
= CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
}
return CGM.BlockObjectAssign;
}
void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
llvm::Value *F = getBlockObjectDispose();
llvm::Value *N;
V = Builder.CreateBitCast(V, PtrToInt8Ty);
2009-10-14 14:03:49 -04:00
N = llvm::ConstantInt::get(llvm::Type::getInt32Ty(V->getContext()), flag);
2009-06-02 13:58:47 -04:00
Builder.CreateCall2(F, V, N);
}
ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
CGBuilderTy &B)
2009-10-14 14:03:49 -04:00
: CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
PtrToInt8Ty = llvm::PointerType::getUnqual(
llvm::Type::getInt8Ty(VMContext));
2009-06-02 13:58:47 -04:00
BlockHasCopyDispose = false;
}