2009-02-12 08:39:25 +08: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "CodeGenFunction.h"
|
|
|
|
#include "CodeGenModule.h"
|
2009-03-21 05:53:12 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2009-02-12 08:39:25 +08:00
|
|
|
#include "llvm/Module.h"
|
2009-02-13 01:55:02 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-02-12 08:39:25 +08:00
|
|
|
#include <algorithm>
|
|
|
|
using namespace clang;
|
|
|
|
using namespace CodeGen;
|
|
|
|
|
2009-03-07 02:42:23 +08:00
|
|
|
llvm::Constant *CodeGenFunction::
|
2009-03-26 01:58:24 +08:00
|
|
|
BuildDescriptorBlockDecl(bool BlockHasCopyDispose, uint64_t Size,
|
|
|
|
const llvm::StructType* Ty,
|
2009-03-07 10:35:30 +08:00
|
|
|
std::vector<HelperInfo> *NoteForHelper) {
|
2009-02-14 00:55:51 +08:00
|
|
|
const llvm::Type *UnsignedLongTy
|
|
|
|
= CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
|
2009-02-14 00:19:19 +08:00
|
|
|
llvm::Constant *C;
|
|
|
|
std::vector<llvm::Constant*> Elts;
|
|
|
|
|
|
|
|
// reserved
|
2009-07-25 07:12:58 +08:00
|
|
|
C = llvm::ConstantInt::get(UnsignedLongTy, 0);
|
2009-02-14 00:19:19 +08:00
|
|
|
Elts.push_back(C);
|
|
|
|
|
|
|
|
// Size
|
2009-02-22 04:07:44 +08:00
|
|
|
// 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.
|
2009-07-25 07:12:58 +08:00
|
|
|
C = llvm::ConstantInt::get(UnsignedLongTy, Size);
|
2009-02-14 00:19:19 +08:00
|
|
|
Elts.push_back(C);
|
|
|
|
|
|
|
|
if (BlockHasCopyDispose) {
|
|
|
|
// copy_func_helper_decl
|
2009-04-11 02:52:28 +08:00
|
|
|
Elts.push_back(BuildCopyHelper(Ty, NoteForHelper));
|
2009-02-14 00:19:19 +08:00
|
|
|
|
|
|
|
// destroy_func_decl
|
2009-04-11 02:52:28 +08:00
|
|
|
Elts.push_back(BuildDestroyHelper(Ty, NoteForHelper));
|
2009-02-14 00:19:19 +08:00
|
|
|
}
|
|
|
|
|
2009-07-28 06:29:56 +08:00
|
|
|
C = llvm::ConstantStruct::get(Elts);
|
2009-02-14 00:19:19 +08:00
|
|
|
|
2009-07-09 03:05:04 +08:00
|
|
|
C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
|
2009-02-14 00:19:19 +08:00
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2009-07-09 03:05:04 +08:00
|
|
|
C, "__block_descriptor_tmp");
|
2009-02-14 00:19:19 +08:00
|
|
|
return C;
|
|
|
|
}
|
|
|
|
|
2009-03-05 02:17:45 +08:00
|
|
|
llvm::Constant *BlockModule::getNSConcreteGlobalBlock() {
|
2009-05-13 10:50:56 +08:00
|
|
|
if (NSConcreteGlobalBlock == 0)
|
|
|
|
NSConcreteGlobalBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
|
|
|
|
"_NSConcreteGlobalBlock");
|
2009-02-14 01:23:42 +08:00
|
|
|
return NSConcreteGlobalBlock;
|
|
|
|
}
|
|
|
|
|
2009-03-05 02:17:45 +08:00
|
|
|
llvm::Constant *BlockModule::getNSConcreteStackBlock() {
|
2009-05-13 10:50:56 +08:00
|
|
|
if (NSConcreteStackBlock == 0)
|
|
|
|
NSConcreteStackBlock = CGM.CreateRuntimeVariable(PtrToInt8Ty,
|
|
|
|
"_NSConcreteStackBlock");
|
2009-02-14 03:29:27 +08:00
|
|
|
return NSConcreteStackBlock;
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:32:30 +08:00
|
|
|
static void CollectBlockDeclRefInfo(const Stmt *S,
|
2009-03-01 09:09:12 +08:00
|
|
|
CodeGenFunction::BlockInfo &Info) {
|
|
|
|
for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
|
|
|
|
I != E; ++I)
|
2009-03-02 15:00:57 +08:00
|
|
|
if (*I)
|
|
|
|
CollectBlockDeclRefInfo(*I, Info);
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-03-01 09:09:12 +08:00
|
|
|
if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
|
|
|
|
// FIXME: Handle enums.
|
|
|
|
if (isa<FunctionDecl>(DE->getDecl()))
|
|
|
|
return;
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-03-01 09:09:12 +08:00
|
|
|
if (DE->isByRef())
|
|
|
|
Info.ByRefDeclRefs.push_back(DE);
|
|
|
|
else
|
|
|
|
Info.ByCopyDeclRefs.push_back(DE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-05 06:48:06 +08:00
|
|
|
/// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block can be
|
|
|
|
/// declared as a global variable instead of on the stack.
|
2009-05-13 10:50:56 +08:00
|
|
|
static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info) {
|
2009-03-01 09:09:12 +08:00
|
|
|
return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
|
|
|
|
}
|
|
|
|
|
2009-03-05 06:48:06 +08:00
|
|
|
// FIXME: Push most into CGM, passing down a few bits, like current function
|
|
|
|
// name.
|
2009-02-26 07:33:13 +08:00
|
|
|
llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
|
2009-02-14 00:19:19 +08:00
|
|
|
|
2009-03-01 09:09:12 +08:00
|
|
|
std::string Name = CurFn->getName();
|
|
|
|
CodeGenFunction::BlockInfo Info(0, Name.c_str());
|
|
|
|
CollectBlockDeclRefInfo(BE->getBody(), Info);
|
|
|
|
|
|
|
|
// Check if the block can be global.
|
2009-03-05 06:48:06 +08:00
|
|
|
// 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.
|
2009-03-04 11:23:46 +08:00
|
|
|
if (0 && CanBlockBeGlobal(Info))
|
2009-03-01 09:09:12 +08:00
|
|
|
return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
|
2009-03-05 16:32:30 +08:00
|
|
|
|
|
|
|
std::vector<llvm::Constant*> Elts(5);
|
2009-02-14 00:19:19 +08:00
|
|
|
llvm::Constant *C;
|
2009-02-26 07:33:13 +08:00
|
|
|
llvm::Value *V;
|
2009-02-14 00:19:19 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
// C = BuildBlockStructInitlist();
|
|
|
|
unsigned int flags = BLOCK_HAS_DESCRIPTOR;
|
|
|
|
|
2009-03-05 16:32:30 +08:00
|
|
|
// We run this first so that we set BlockHasCopyDispose from the entire
|
|
|
|
// block literal.
|
|
|
|
// __invoke
|
|
|
|
uint64_t subBlockSize, subBlockAlign;
|
|
|
|
llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
|
2009-03-26 01:58:24 +08:00
|
|
|
bool subBlockHasCopyDispose = false;
|
2009-03-05 16:32:30 +08:00
|
|
|
llvm::Function *Fn
|
2009-03-21 05:53:12 +08:00
|
|
|
= CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, CurFuncDecl, LocalDeclMap,
|
2009-03-14 07:34:28 +08:00
|
|
|
subBlockSize,
|
2009-03-05 16:32:30 +08:00
|
|
|
subBlockAlign,
|
|
|
|
subBlockDeclRefDecls,
|
2009-03-26 01:58:24 +08:00
|
|
|
subBlockHasCopyDispose);
|
|
|
|
BlockHasCopyDispose |= subBlockHasCopyDispose;
|
2009-03-05 16:32:30 +08:00
|
|
|
Elts[3] = Fn;
|
|
|
|
|
2009-05-16 15:57:57 +08:00
|
|
|
// FIXME: Don't use BlockHasCopyDispose, it is set more often then
|
|
|
|
// necessary, for example: { ^{ __block int i; ^{ i = 1; }(); }(); }
|
2009-03-26 01:58:24 +08:00
|
|
|
if (subBlockHasCopyDispose)
|
2009-02-14 00:19:19 +08:00
|
|
|
flags |= BLOCK_HAS_COPY_DISPOSE;
|
|
|
|
|
2009-02-14 04:17:16 +08:00
|
|
|
// __isa
|
2009-02-14 03:29:27 +08:00
|
|
|
C = CGM.getNSConcreteStackBlock();
|
2009-07-15 07:10:40 +08:00
|
|
|
C = VMContext.getConstantExprBitCast(C, PtrToInt8Ty);
|
2009-03-05 16:32:30 +08:00
|
|
|
Elts[0] = C;
|
2009-02-14 00:19:19 +08:00
|
|
|
|
|
|
|
// __flags
|
|
|
|
const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
|
|
|
|
CGM.getTypes().ConvertType(CGM.getContext().IntTy));
|
2009-07-25 07:12:58 +08:00
|
|
|
C = llvm::ConstantInt::get(IntTy, flags);
|
2009-03-05 16:32:30 +08:00
|
|
|
Elts[1] = C;
|
2009-02-14 00:19:19 +08:00
|
|
|
|
|
|
|
// __reserved
|
2009-07-25 07:12:58 +08:00
|
|
|
C = llvm::ConstantInt::get(IntTy, 0);
|
2009-03-05 16:32:30 +08:00
|
|
|
Elts[2] = C;
|
2009-02-14 00:19:19 +08:00
|
|
|
|
2009-02-26 07:33:13 +08:00
|
|
|
if (subBlockDeclRefDecls.size() == 0) {
|
2009-03-07 02:42:23 +08:00
|
|
|
// __descriptor
|
2009-03-26 01:58:24 +08:00
|
|
|
Elts[4] = BuildDescriptorBlockDecl(subBlockHasCopyDispose, subBlockSize, 0, 0);
|
2009-03-07 02:42:23 +08:00
|
|
|
|
2009-03-02 04:07:53 +08:00
|
|
|
// Optimize to being a global block.
|
|
|
|
Elts[0] = CGM.getNSConcreteGlobalBlock();
|
2009-07-25 07:12:58 +08:00
|
|
|
Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
|
2009-03-02 04:07:53 +08:00
|
|
|
|
2009-07-28 06:29:56 +08:00
|
|
|
C = llvm::ConstantStruct::get(Elts);
|
2009-02-26 07:33:13 +08:00
|
|
|
|
|
|
|
char Name[32];
|
|
|
|
sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
|
2009-07-09 03:05:04 +08:00
|
|
|
C = new llvm::GlobalVariable(CGM.getModule(), C->getType(), true,
|
2009-02-26 07:33:13 +08:00
|
|
|
llvm::GlobalValue::InternalLinkage,
|
2009-07-09 03:05:04 +08:00
|
|
|
C, Name);
|
2009-02-26 07:33:13 +08:00
|
|
|
QualType BPT = BE->getType();
|
2009-07-15 07:10:40 +08:00
|
|
|
C = VMContext.getConstantExprBitCast(C, ConvertType(BPT));
|
2009-02-26 07:33:13 +08:00
|
|
|
return C;
|
|
|
|
}
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-02-26 07:33:13 +08:00
|
|
|
std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
|
2009-03-07 10:35:30 +08:00
|
|
|
for (int i=0; i<4; ++i)
|
2009-02-26 07:33:13 +08:00
|
|
|
Types[i] = Elts[i]->getType();
|
2009-03-07 10:35:30 +08:00
|
|
|
Types[4] = PtrToInt8Ty;
|
2009-02-26 07:33:13 +08:00
|
|
|
|
2009-02-28 17:07:16 +08:00
|
|
|
for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
|
|
|
|
const Expr *E = subBlockDeclRefDecls[i];
|
|
|
|
const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
|
|
|
|
QualType Ty = E->getType();
|
2009-03-04 11:23:46 +08:00
|
|
|
if (BDRE && BDRE->isByRef()) {
|
|
|
|
uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl());
|
2009-07-15 07:10:40 +08:00
|
|
|
Types[i+5] = VMContext.getPointerType(BuildByRefType(Ty, Align), 0);
|
2009-03-04 11:23:46 +08:00
|
|
|
} else
|
|
|
|
Types[i+5] = ConvertType(Ty);
|
2009-02-28 17:07:16 +08:00
|
|
|
}
|
2009-02-26 07:33:13 +08:00
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
llvm::StructType *Ty = VMContext.getStructType(Types, true);
|
2009-02-26 07:33:13 +08:00
|
|
|
|
|
|
|
llvm::AllocaInst *A = CreateTempAlloca(Ty);
|
|
|
|
A->setAlignment(subBlockAlign);
|
|
|
|
V = A;
|
|
|
|
|
2009-03-07 10:35:30 +08:00
|
|
|
std::vector<HelperInfo> NoteForHelper(subBlockDeclRefDecls.size());
|
|
|
|
int helpersize = 0;
|
|
|
|
|
|
|
|
for (unsigned i=0; i<4; ++i)
|
2009-02-26 07:33:13 +08:00
|
|
|
Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-02-26 07:33:13 +08:00
|
|
|
for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
|
|
|
|
{
|
2009-02-28 17:07:16 +08:00
|
|
|
// 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-03-04 11:23:46 +08:00
|
|
|
llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
|
2009-03-07 10:35:30 +08:00
|
|
|
NoteForHelper[helpersize].index = i+5;
|
|
|
|
NoteForHelper[helpersize].RequiresCopying = BlockRequiresCopying(VD->getType());
|
|
|
|
NoteForHelper[helpersize].flag
|
|
|
|
= VD->getType()->isBlockPointerType() ? BLOCK_FIELD_IS_BLOCK : BLOCK_FIELD_IS_OBJECT;
|
|
|
|
|
2009-02-28 17:07:16 +08:00
|
|
|
if (LocalDeclMap[VD]) {
|
2009-03-04 11:23:46 +08:00
|
|
|
if (BDRE->isByRef()) {
|
2009-03-07 10:35:30 +08:00
|
|
|
NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
|
2009-03-07 14:04:31 +08:00
|
|
|
// FIXME: Someone double check this.
|
|
|
|
(VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
|
2009-03-04 11:23:46 +08:00
|
|
|
const llvm::Type *Ty = Types[i+5];
|
|
|
|
llvm::Value *Loc = LocalDeclMap[VD];
|
|
|
|
Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
|
|
|
|
Loc = Builder.CreateLoad(Loc, false);
|
|
|
|
Loc = Builder.CreateBitCast(Loc, Ty);
|
|
|
|
Builder.CreateStore(Loc, Addr);
|
2009-03-07 10:35:30 +08:00
|
|
|
++helpersize;
|
2009-03-04 11:23:46 +08:00
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
|
|
|
|
VD->getType(), SourceLocation(),
|
|
|
|
false, false);
|
2009-02-28 17:07:16 +08:00
|
|
|
}
|
2009-03-04 11:23:46 +08:00
|
|
|
if (BDRE->isByRef()) {
|
2009-03-22 05:00:35 +08:00
|
|
|
NoteForHelper[helpersize].flag = BLOCK_FIELD_IS_BYREF |
|
|
|
|
// FIXME: Someone double check this.
|
|
|
|
(VD->getType().isObjCGCWeak() ? BLOCK_FIELD_IS_WEAK : 0);
|
2009-02-28 17:07:16 +08:00
|
|
|
E = new (getContext())
|
|
|
|
UnaryOperator(E, UnaryOperator::AddrOf,
|
|
|
|
getContext().getPointerType(E->getType()),
|
|
|
|
SourceLocation());
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
2009-03-07 10:35:30 +08:00
|
|
|
++helpersize;
|
2009-02-26 07:33:13 +08:00
|
|
|
|
2009-02-28 17:07:16 +08:00
|
|
|
RValue r = EmitAnyExpr(E, Addr, false);
|
2009-03-04 11:23:46 +08:00
|
|
|
if (r.isScalar()) {
|
|
|
|
llvm::Value *Loc = r.getScalarVal();
|
|
|
|
const llvm::Type *Ty = Types[i+5];
|
|
|
|
if (BDRE->isByRef()) {
|
2009-03-05 06:48:06 +08:00
|
|
|
// 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.
|
|
|
|
uint64_t offset = BlockDecls[BDRE->getDecl()];
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-03-05 06:48:06 +08:00
|
|
|
llvm::Value *BlockLiteral = LoadBlockStruct();
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-03-05 06:48:06 +08:00
|
|
|
Loc = Builder.CreateGEP(BlockLiteral,
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(llvm::Type::Int64Ty,
|
2009-03-05 06:48:06 +08:00
|
|
|
offset),
|
|
|
|
"block.literal");
|
2009-07-15 07:10:40 +08:00
|
|
|
Ty = VMContext.getPointerType(Ty, 0);
|
2009-03-04 11:23:46 +08:00
|
|
|
Loc = Builder.CreateBitCast(Loc, Ty);
|
2009-03-05 06:48:06 +08:00
|
|
|
Loc = Builder.CreateLoad(Loc, false);
|
|
|
|
// Loc = Builder.CreateBitCast(Loc, Ty);
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
|
|
|
Builder.CreateStore(Loc, Addr);
|
|
|
|
} else if (r.isComplex())
|
2009-02-26 07:33:13 +08:00
|
|
|
// 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.
|
|
|
|
}
|
2009-03-07 10:35:30 +08:00
|
|
|
NoteForHelper.resize(helpersize);
|
2009-03-07 02:42:23 +08:00
|
|
|
|
|
|
|
// __descriptor
|
2009-03-26 01:58:24 +08:00
|
|
|
llvm::Value *Descriptor = BuildDescriptorBlockDecl(subBlockHasCopyDispose,
|
|
|
|
subBlockSize, Ty,
|
2009-03-07 10:35:30 +08:00
|
|
|
&NoteForHelper);
|
|
|
|
Descriptor = Builder.CreateBitCast(Descriptor, PtrToInt8Ty);
|
|
|
|
Builder.CreateStore(Descriptor, Builder.CreateStructGEP(V, 4, "block.tmp"));
|
2009-02-14 00:19:19 +08:00
|
|
|
}
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-02-19 09:01:04 +08:00
|
|
|
QualType BPT = BE->getType();
|
2009-02-26 07:33:13 +08:00
|
|
|
return Builder.CreateBitCast(V, ConvertType(BPT));
|
2009-02-14 00:19:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-05 02:17:45 +08:00
|
|
|
const llvm::Type *BlockModule::getBlockDescriptorType() {
|
2009-02-13 23:16:56 +08:00
|
|
|
if (BlockDescriptorType)
|
|
|
|
return BlockDescriptorType;
|
|
|
|
|
2009-02-13 23:32:32 +08:00
|
|
|
const llvm::Type *UnsignedLongTy =
|
2009-02-13 23:16:56 +08:00
|
|
|
getTypes().ConvertType(getContext().UnsignedLongTy);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 23:16:56 +08:00
|
|
|
// struct __block_descriptor {
|
|
|
|
// unsigned long reserved;
|
|
|
|
// unsigned long block_size;
|
|
|
|
// };
|
2009-07-15 07:10:40 +08:00
|
|
|
BlockDescriptorType = VMContext.getStructType(UnsignedLongTy,
|
2009-02-13 23:32:32 +08:00
|
|
|
UnsignedLongTy,
|
2009-02-13 23:16:56 +08:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
getModule().addTypeName("struct.__block_descriptor",
|
|
|
|
BlockDescriptorType);
|
|
|
|
|
|
|
|
return BlockDescriptorType;
|
2009-02-12 08:39:25 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 02:17:45 +08:00
|
|
|
const llvm::Type *BlockModule::getGenericBlockLiteralType() {
|
2009-02-13 23:25:34 +08:00
|
|
|
if (GenericBlockLiteralType)
|
|
|
|
return GenericBlockLiteralType;
|
|
|
|
|
2009-02-13 23:32:32 +08:00
|
|
|
const llvm::Type *BlockDescPtrTy =
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getPointerTypeUnqual(getBlockDescriptorType());
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-14 00:01:35 +08:00
|
|
|
const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
|
|
|
|
getTypes().ConvertType(getContext().IntTy));
|
|
|
|
|
2009-02-13 23:25:34 +08:00
|
|
|
// struct __block_literal_generic {
|
2009-02-19 09:01:04 +08:00
|
|
|
// void *__isa;
|
|
|
|
// int __flags;
|
|
|
|
// int __reserved;
|
|
|
|
// void (*__invoke)(void *);
|
|
|
|
// struct __block_descriptor *__descriptor;
|
2009-02-13 23:25:34 +08:00
|
|
|
// };
|
2009-07-15 07:10:40 +08:00
|
|
|
GenericBlockLiteralType = VMContext.getStructType(PtrToInt8Ty,
|
2009-02-14 00:01:35 +08:00
|
|
|
IntTy,
|
|
|
|
IntTy,
|
2009-03-05 09:23:13 +08:00
|
|
|
PtrToInt8Ty,
|
2009-02-13 23:25:34 +08:00
|
|
|
BlockDescPtrTy,
|
|
|
|
NULL);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 23:25:34 +08:00
|
|
|
getModule().addTypeName("struct.__block_literal_generic",
|
|
|
|
GenericBlockLiteralType);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 23:25:34 +08:00
|
|
|
return GenericBlockLiteralType;
|
2009-02-12 08:39:25 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 02:17:45 +08:00
|
|
|
const llvm::Type *BlockModule::getGenericExtendedBlockLiteralType() {
|
2009-02-19 09:01:04 +08:00
|
|
|
if (GenericExtendedBlockLiteralType)
|
|
|
|
return GenericExtendedBlockLiteralType;
|
|
|
|
|
|
|
|
const llvm::Type *BlockDescPtrTy =
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getPointerTypeUnqual(getBlockDescriptorType());
|
2009-02-19 09:01:04 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
// void *__copy_func_helper_decl;
|
|
|
|
// void *__destroy_func_decl;
|
|
|
|
// };
|
2009-07-15 07:10:40 +08:00
|
|
|
GenericExtendedBlockLiteralType = VMContext.getStructType(PtrToInt8Ty,
|
2009-02-19 09:01:04 +08:00
|
|
|
IntTy,
|
|
|
|
IntTy,
|
2009-03-05 09:23:13 +08:00
|
|
|
PtrToInt8Ty,
|
2009-02-19 09:01:04 +08:00
|
|
|
BlockDescPtrTy,
|
2009-03-05 09:23:13 +08:00
|
|
|
PtrToInt8Ty,
|
|
|
|
PtrToInt8Ty,
|
2009-02-19 09:01:04 +08:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
getModule().addTypeName("struct.__block_literal_extended_generic",
|
|
|
|
GenericExtendedBlockLiteralType);
|
|
|
|
|
|
|
|
return GenericExtendedBlockLiteralType;
|
|
|
|
}
|
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
|
2009-02-13 23:32:32 +08:00
|
|
|
const BlockPointerType *BPT =
|
2009-07-18 01:50:17 +08:00
|
|
|
E->getCallee()->getType()->getAsBlockPointerType();
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-12 08:39:25 +08:00
|
|
|
llvm::Value *Callee = EmitScalarExpr(E->getCallee());
|
|
|
|
|
|
|
|
// Get a pointer to the generic block literal.
|
|
|
|
const llvm::Type *BlockLiteralTy =
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getPointerTypeUnqual(CGM.getGenericBlockLiteralType());
|
2009-02-12 08:39:25 +08:00
|
|
|
|
|
|
|
// Bitcast the callee to a block literal.
|
2009-02-13 23:32:32 +08:00
|
|
|
llvm::Value *BlockLiteral =
|
2009-02-12 08:39:25 +08:00
|
|
|
Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
|
|
|
|
|
|
|
|
// Get the function pointer from the literal.
|
|
|
|
llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
|
|
|
|
|
2009-02-13 23:32:32 +08:00
|
|
|
BlockLiteral =
|
|
|
|
Builder.CreateBitCast(BlockLiteral,
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty),
|
2009-02-12 08:39:25 +08:00
|
|
|
"tmp");
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-12 08:39:25 +08:00
|
|
|
// Add the block literal.
|
|
|
|
QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
|
|
|
|
CallArgList Args;
|
|
|
|
Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-04-09 07:13:16 +08:00
|
|
|
QualType FnType = BPT->getPointeeType();
|
|
|
|
|
2009-02-12 08:39:25 +08:00
|
|
|
// And the rest of the arguments.
|
2009-04-09 07:13:16 +08:00
|
|
|
EmitCallArgs(Args, FnType->getAsFunctionProtoType(),
|
|
|
|
E->arg_begin(), E->arg_end());
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-04-08 06:10:22 +08:00
|
|
|
// Load the function.
|
|
|
|
llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
|
|
|
|
|
2009-04-08 10:55:55 +08:00
|
|
|
QualType ResultType = FnType->getAsFunctionType()->getResultType();
|
|
|
|
|
|
|
|
const CGFunctionInfo &FnInfo =
|
|
|
|
CGM.getTypes().getFunctionInfo(ResultType, Args);
|
2009-04-08 06:10:22 +08:00
|
|
|
|
|
|
|
// Cast the function pointer to the right type.
|
|
|
|
const llvm::Type *BlockFTy =
|
2009-04-08 10:55:55 +08:00
|
|
|
CGM.getTypes().GetFunctionType(FnInfo, false);
|
2009-04-08 06:10:22 +08:00
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
const llvm::Type *BlockFTyPtr = VMContext.getPointerTypeUnqual(BlockFTy);
|
2009-04-08 06:10:22 +08:00
|
|
|
Func = Builder.CreateBitCast(Func, BlockFTyPtr);
|
|
|
|
|
2009-02-12 08:39:25 +08:00
|
|
|
// And call the block.
|
2009-04-07 08:20:24 +08:00
|
|
|
return EmitCall(FnInfo, Func, Args);
|
2009-02-12 08:39:25 +08:00
|
|
|
}
|
2009-02-13 01:55:02 +08:00
|
|
|
|
2009-03-04 11:23:46 +08:00
|
|
|
llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
|
|
|
|
uint64_t &offset = BlockDecls[E->getDecl()];
|
|
|
|
|
|
|
|
const llvm::Type *Ty;
|
|
|
|
Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
|
|
|
|
|
|
|
|
// See if we have already allocated an offset for this variable.
|
|
|
|
if (offset == 0) {
|
2009-03-05 16:32:30 +08:00
|
|
|
// Don't run the expensive check, unless we have to.
|
|
|
|
if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
|
|
|
|
BlockHasCopyDispose = true;
|
2009-03-04 11:23:46 +08:00
|
|
|
// if not, allocate one now.
|
|
|
|
offset = getBlockOffset(E);
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Value *BlockLiteral = LoadBlockStruct();
|
|
|
|
llvm::Value *V = Builder.CreateGEP(BlockLiteral,
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(llvm::Type::Int64Ty,
|
2009-03-04 11:23:46 +08:00
|
|
|
offset),
|
2009-03-05 06:48:06 +08:00
|
|
|
"block.literal");
|
2009-03-04 11:23:46 +08:00
|
|
|
if (E->isByRef()) {
|
|
|
|
bool needsCopyDispose = BlockRequiresCopying(E->getType());
|
|
|
|
uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
|
|
|
|
const llvm::Type *PtrStructTy
|
2009-07-15 07:10:40 +08:00
|
|
|
= VMContext.getPointerType(BuildByRefType(E->getType(), Align), 0);
|
2009-03-22 05:00:35 +08:00
|
|
|
// The block literal will need a copy/destroy helper.
|
|
|
|
BlockHasCopyDispose = true;
|
2009-03-04 11:23:46 +08:00
|
|
|
Ty = PtrStructTy;
|
2009-07-15 07:10:40 +08:00
|
|
|
Ty = VMContext.getPointerType(Ty, 0);
|
2009-03-04 11:23:46 +08:00
|
|
|
V = Builder.CreateBitCast(V, Ty);
|
|
|
|
V = Builder.CreateLoad(V, false);
|
|
|
|
V = Builder.CreateStructGEP(V, 1, "forwarding");
|
|
|
|
V = Builder.CreateLoad(V, false);
|
|
|
|
V = Builder.CreateBitCast(V, PtrStructTy);
|
|
|
|
V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
|
|
|
|
} else {
|
2009-07-15 07:10:40 +08:00
|
|
|
Ty = VMContext.getPointerType(Ty, 0);
|
2009-03-04 11:23:46 +08:00
|
|
|
V = Builder.CreateBitCast(V, Ty);
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2009-03-21 05:53:12 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2009-02-15 06:16:35 +08:00
|
|
|
llvm::Constant *
|
2009-03-05 02:47:42 +08:00
|
|
|
BlockModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
|
2009-02-13 01:55:02 +08:00
|
|
|
// Generate the block descriptor.
|
|
|
|
const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
|
2009-02-14 00:01:35 +08:00
|
|
|
const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
|
|
|
|
getTypes().ConvertType(getContext().IntTy));
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
llvm::Constant *DescriptorFields[2];
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// Reserved
|
2009-07-13 12:10:07 +08:00
|
|
|
DescriptorFields[0] = getModule().getContext().getNullValue(UnsignedLongTy);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// Block literal size. For global blocks we just use the size of the generic
|
|
|
|
// block literal struct.
|
2009-02-13 23:32:32 +08:00
|
|
|
uint64_t BlockLiteralSize =
|
2009-02-13 23:25:34 +08:00
|
|
|
TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
|
2009-07-15 07:10:40 +08:00
|
|
|
DescriptorFields[1] =
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
|
|
|
llvm::Constant *DescriptorStruct =
|
2009-07-28 06:29:56 +08:00
|
|
|
llvm::ConstantStruct::get(&DescriptorFields[0], 2);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
llvm::GlobalVariable *Descriptor =
|
2009-07-09 03:05:04 +08:00
|
|
|
new llvm::GlobalVariable(getModule(), DescriptorStruct->getType(), true,
|
2009-02-13 23:32:32 +08:00
|
|
|
llvm::GlobalVariable::InternalLinkage,
|
2009-07-09 03:05:04 +08:00
|
|
|
DescriptorStruct, "__block_descriptor_global");
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// Generate the constants for the block literal.
|
|
|
|
llvm::Constant *LiteralFields[5];
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-15 06:16:35 +08:00
|
|
|
CodeGenFunction::BlockInfo Info(0, n);
|
2009-02-26 07:33:13 +08:00
|
|
|
uint64_t subBlockSize, subBlockAlign;
|
2009-02-28 17:07:16 +08:00
|
|
|
llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
|
2009-03-12 11:07:24 +08:00
|
|
|
bool subBlockHasCopyDispose = false;
|
2009-03-14 07:34:28 +08:00
|
|
|
llvm::DenseMap<const Decl*, llvm::Value*> LocalDeclMap;
|
2009-02-22 04:00:35 +08:00
|
|
|
llvm::Function *Fn
|
2009-03-21 05:53:12 +08:00
|
|
|
= CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, 0, LocalDeclMap,
|
2009-03-14 07:34:28 +08:00
|
|
|
subBlockSize,
|
2009-03-05 02:47:42 +08:00
|
|
|
subBlockAlign,
|
2009-03-05 16:32:30 +08:00
|
|
|
subBlockDeclRefDecls,
|
|
|
|
subBlockHasCopyDispose);
|
2009-02-22 04:00:35 +08:00
|
|
|
assert(subBlockSize == BlockLiteralSize
|
|
|
|
&& "no imports allowed for global block");
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// isa
|
2009-02-14 01:23:42 +08:00
|
|
|
LiteralFields[0] = getNSConcreteGlobalBlock();
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// Flags
|
2009-03-05 16:32:30 +08:00
|
|
|
LiteralFields[1] =
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// Reserved
|
2009-07-13 12:10:07 +08:00
|
|
|
LiteralFields[2] = getModule().getContext().getNullValue(IntTy);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// Function
|
|
|
|
LiteralFields[3] = Fn;
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
// Descriptor
|
|
|
|
LiteralFields[4] = Descriptor;
|
2009-02-13 23:32:32 +08:00
|
|
|
|
|
|
|
llvm::Constant *BlockLiteralStruct =
|
2009-07-28 06:29:56 +08:00
|
|
|
llvm::ConstantStruct::get(&LiteralFields[0], 5);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
|
|
|
llvm::GlobalVariable *BlockLiteral =
|
2009-07-09 03:05:04 +08:00
|
|
|
new llvm::GlobalVariable(getModule(), BlockLiteralStruct->getType(), true,
|
2009-02-13 23:32:32 +08:00
|
|
|
llvm::GlobalVariable::InternalLinkage,
|
2009-07-09 03:05:04 +08:00
|
|
|
BlockLiteralStruct, "__block_literal_global");
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
return BlockLiteral;
|
|
|
|
}
|
|
|
|
|
2009-02-22 04:00:35 +08:00
|
|
|
llvm::Value *CodeGenFunction::LoadBlockStruct() {
|
|
|
|
return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:32:30 +08:00
|
|
|
llvm::Function *
|
|
|
|
CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
|
|
|
|
const BlockInfo& Info,
|
2009-03-21 05:53:12 +08:00
|
|
|
const Decl *OuterFuncDecl,
|
2009-03-14 07:34:28 +08:00
|
|
|
llvm::DenseMap<const Decl*, llvm::Value*> ldm,
|
2009-03-05 16:32:30 +08:00
|
|
|
uint64_t &Size,
|
|
|
|
uint64_t &Align,
|
|
|
|
llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls,
|
|
|
|
bool &subBlockHasCopyDispose) {
|
2009-04-16 05:51:44 +08:00
|
|
|
|
|
|
|
// Check if we should generate debug info for this block.
|
|
|
|
if (CGM.getDebugInfo())
|
|
|
|
DebugInfo = CGM.getDebugInfo();
|
|
|
|
|
2009-03-14 07:34:28 +08: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-04-14 10:25:56 +08:00
|
|
|
if (VD->getStorageClass() == VarDecl::Static || VD->hasExternalStorage())
|
2009-03-14 07:34:28 +08:00
|
|
|
LocalDeclMap[VD] = i->second;
|
|
|
|
}
|
|
|
|
|
2009-03-28 11:24:54 +08:00
|
|
|
// FIXME: We need to rearrange the code for copy/dispose so we have this
|
|
|
|
// sooner, so we can calculate offsets correctly.
|
|
|
|
if (!BlockHasCopyDispose)
|
|
|
|
BlockOffset = CGM.getTargetData()
|
|
|
|
.getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
|
|
|
|
else
|
|
|
|
BlockOffset = CGM.getTargetData()
|
|
|
|
.getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
|
|
|
|
BlockAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
|
|
|
|
|
2009-04-12 01:55:15 +08:00
|
|
|
const FunctionType *BlockFunctionType = BExpr->getFunctionType();
|
|
|
|
QualType ResultType;
|
|
|
|
bool IsVariadic;
|
2009-04-12 02:54:57 +08:00
|
|
|
if (const FunctionProtoType *FTy =
|
|
|
|
dyn_cast<FunctionProtoType>(BlockFunctionType)) {
|
2009-04-12 01:55:15 +08:00
|
|
|
ResultType = FTy->getResultType();
|
|
|
|
IsVariadic = FTy->isVariadic();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// K&R style block.
|
|
|
|
ResultType = BlockFunctionType->getResultType();
|
|
|
|
IsVariadic = false;
|
|
|
|
}
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
FunctionArgList Args;
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-03-01 03:01:03 +08:00
|
|
|
const BlockDecl *BD = BExpr->getBlockDecl();
|
2009-02-13 01:55:02 +08:00
|
|
|
|
|
|
|
// FIXME: This leaks
|
2009-02-13 23:32:32 +08:00
|
|
|
ImplicitParamDecl *SelfDecl =
|
2009-02-13 01:55:02 +08:00
|
|
|
ImplicitParamDecl::Create(getContext(), 0,
|
|
|
|
SourceLocation(), 0,
|
|
|
|
getContext().getPointerType(getContext().VoidTy));
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-02-13 01:55:02 +08:00
|
|
|
Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
|
2009-02-22 04:00:35 +08:00
|
|
|
BlockStructDecl = SelfDecl;
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-03-14 00:56:44 +08:00
|
|
|
for (BlockDecl::param_const_iterator i = BD->param_begin(),
|
2009-02-13 01:55:02 +08:00
|
|
|
e = BD->param_end(); i != e; ++i)
|
2009-02-18 07:25:52 +08:00
|
|
|
Args.push_back(std::make_pair(*i, (*i)->getType()));
|
2009-02-13 23:32:32 +08:00
|
|
|
|
|
|
|
const CGFunctionInfo &FI =
|
2009-04-12 01:55:15 +08:00
|
|
|
CGM.getTypes().getFunctionInfo(ResultType, Args);
|
2009-02-13 01:55:02 +08:00
|
|
|
|
2009-02-15 06:16:35 +08:00
|
|
|
std::string Name = std::string("__") + Info.Name + "_block_invoke_";
|
2009-02-13 01:55:02 +08:00
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
2009-04-12 01:55:15 +08:00
|
|
|
const llvm::FunctionType *LTy = Types.GetFunctionType(FI, IsVariadic);
|
2009-02-13 23:32:32 +08:00
|
|
|
|
|
|
|
llvm::Function *Fn =
|
2009-02-13 01:55:02 +08:00
|
|
|
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
|
|
|
|
Name,
|
|
|
|
&CGM.getModule());
|
2009-02-13 23:32:32 +08:00
|
|
|
|
2009-04-17 08:48:04 +08:00
|
|
|
CGM.SetInternalFunctionAttributes(BD, Fn, FI);
|
|
|
|
|
2009-04-23 15:18:56 +08:00
|
|
|
StartFunction(BD, ResultType, Fn, Args,
|
2009-03-01 03:01:03 +08:00
|
|
|
BExpr->getBody()->getLocEnd());
|
2009-04-23 15:18:56 +08:00
|
|
|
CurFuncDecl = OuterFuncDecl;
|
2009-04-23 13:30:27 +08:00
|
|
|
CurCodeDecl = BD;
|
2009-03-01 03:01:03 +08:00
|
|
|
EmitStmt(BExpr->getBody());
|
|
|
|
FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
|
2009-02-13 01:55:02 +08:00
|
|
|
|
2009-02-26 07:33:13 +08:00
|
|
|
// The runtime needs a minimum alignment of a void *.
|
|
|
|
uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
|
|
|
|
BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
|
|
|
|
|
2009-02-22 04:00:35 +08:00
|
|
|
Size = BlockOffset;
|
2009-02-26 07:33:13 +08:00
|
|
|
Align = BlockAlign;
|
|
|
|
subBlockDeclRefDecls = BlockDeclRefDecls;
|
2009-03-05 16:32:30 +08:00
|
|
|
subBlockHasCopyDispose |= BlockHasCopyDispose;
|
2009-02-13 01:55:02 +08:00
|
|
|
return Fn;
|
|
|
|
}
|
2009-02-28 17:07:16 +08:00
|
|
|
|
2009-03-07 10:35:30 +08:00
|
|
|
uint64_t BlockFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
|
2009-02-28 17:07:16 +08:00
|
|
|
const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
|
|
|
|
|
|
|
|
uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
|
|
|
|
uint64_t Align = getContext().getDeclAlignInBytes(D);
|
|
|
|
|
|
|
|
if (BDRE->isByRef()) {
|
|
|
|
Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
|
|
|
|
Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert ((Align > 0) && "alignment must be 1 byte or more");
|
|
|
|
|
|
|
|
uint64_t OldOffset = BlockOffset;
|
|
|
|
|
|
|
|
// Ensure proper alignment, even if it means we have to have a gap
|
|
|
|
BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
|
|
|
|
BlockAlign = std::max(Align, BlockAlign);
|
2009-03-05 16:32:30 +08:00
|
|
|
|
2009-02-28 17:07:16 +08:00
|
|
|
uint64_t Pad = BlockOffset - OldOffset;
|
|
|
|
if (Pad) {
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getArrayType(llvm::Type::Int8Ty, Pad);
|
2009-02-28 17:07:16 +08:00
|
|
|
QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
|
|
|
|
llvm::APInt(32, Pad),
|
|
|
|
ArrayType::Normal, 0);
|
|
|
|
ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
|
|
|
|
0, QualType(PadTy), VarDecl::None,
|
|
|
|
SourceLocation());
|
|
|
|
Expr *E;
|
|
|
|
E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
|
|
|
|
SourceLocation(), false, false);
|
|
|
|
BlockDeclRefDecls.push_back(E);
|
|
|
|
}
|
|
|
|
BlockDeclRefDecls.push_back(BDRE);
|
|
|
|
|
|
|
|
BlockOffset += Size;
|
|
|
|
return BlockOffset-Size;
|
|
|
|
}
|
2009-03-04 11:23:46 +08:00
|
|
|
|
2009-03-07 10:35:30 +08:00
|
|
|
llvm::Constant *BlockFunction::
|
|
|
|
GenerateCopyHelperFunction(bool BlockHasCopyDispose, const llvm::StructType *T,
|
2009-04-11 02:52:28 +08:00
|
|
|
std::vector<HelperInfo> *NoteForHelperp) {
|
2009-03-06 09:33:24 +08:00
|
|
|
QualType R = getContext().VoidTy;
|
|
|
|
|
|
|
|
FunctionArgList Args;
|
|
|
|
// FIXME: This leaks
|
2009-03-07 10:35:30 +08:00
|
|
|
ImplicitParamDecl *Dst =
|
|
|
|
ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
|
|
|
|
getContext().getPointerType(getContext().VoidTy));
|
|
|
|
Args.push_back(std::make_pair(Dst, Dst->getType()));
|
2009-03-06 09:33:24 +08:00
|
|
|
ImplicitParamDecl *Src =
|
|
|
|
ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
|
|
|
|
getContext().getPointerType(getContext().VoidTy));
|
|
|
|
Args.push_back(std::make_pair(Src, Src->getType()));
|
|
|
|
|
|
|
|
const CGFunctionInfo &FI =
|
|
|
|
CGM.getTypes().getFunctionInfo(R, Args);
|
|
|
|
|
2009-06-06 07:26:36 +08:00
|
|
|
// FIXME: We'd like to put these into a mergable by content, with
|
|
|
|
// internal linkage.
|
2009-03-06 09:33:24 +08:00
|
|
|
std::string Name = std::string("__copy_helper_block_");
|
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
|
|
|
|
|
|
|
|
llvm::Function *Fn =
|
|
|
|
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
|
|
|
|
Name,
|
|
|
|
&CGM.getModule());
|
|
|
|
|
|
|
|
IdentifierInfo *II
|
|
|
|
= &CGM.getContext().Idents.get("__copy_helper_block_");
|
|
|
|
|
|
|
|
FunctionDecl *FD = FunctionDecl::Create(getContext(),
|
|
|
|
getContext().getTranslationUnitDecl(),
|
|
|
|
SourceLocation(), II, R,
|
|
|
|
FunctionDecl::Static, false,
|
|
|
|
true);
|
|
|
|
CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
|
2009-03-07 10:35:30 +08:00
|
|
|
|
|
|
|
llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
|
|
|
|
llvm::Type *PtrPtrT;
|
2009-04-11 02:52:28 +08:00
|
|
|
|
|
|
|
if (NoteForHelperp) {
|
|
|
|
std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0);
|
2009-04-11 02:52:28 +08:00
|
|
|
SrcObj = Builder.CreateBitCast(SrcObj, PtrPtrT);
|
|
|
|
SrcObj = Builder.CreateLoad(SrcObj);
|
|
|
|
|
|
|
|
llvm::Value *DstObj = CGF.GetAddrOfLocalVar(Dst);
|
2009-04-16 06:11:36 +08:00
|
|
|
llvm::Type *PtrPtrT;
|
2009-07-15 07:10:40 +08:00
|
|
|
PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0);
|
2009-04-16 06:11:36 +08:00
|
|
|
DstObj = Builder.CreateBitCast(DstObj, PtrPtrT);
|
|
|
|
DstObj = Builder.CreateLoad(DstObj);
|
2009-04-11 02:52:28 +08:00
|
|
|
|
|
|
|
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,
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getPointerType(PtrToInt8Ty, 0));
|
2009-04-11 02:52:28 +08:00
|
|
|
Srcv = Builder.CreateLoad(Srcv);
|
|
|
|
|
|
|
|
llvm::Value *Dstv = Builder.CreateStructGEP(DstObj, index);
|
|
|
|
Dstv = Builder.CreateBitCast(Dstv, PtrToInt8Ty);
|
|
|
|
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
|
2009-04-11 02:52:28 +08:00
|
|
|
llvm::Value *F = getBlockObjectAssign();
|
|
|
|
Builder.CreateCall3(F, Dstv, Srcv, N);
|
|
|
|
}
|
2009-03-07 10:35:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 09:33:24 +08:00
|
|
|
CGF.FinishFunction();
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty);
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
|
|
|
|
2009-03-07 02:42:23 +08:00
|
|
|
llvm::Constant *BlockFunction::
|
2009-03-07 10:35:30 +08:00
|
|
|
GenerateDestroyHelperFunction(bool BlockHasCopyDispose,
|
|
|
|
const llvm::StructType* T,
|
2009-04-11 02:52:28 +08:00
|
|
|
std::vector<HelperInfo> *NoteForHelperp) {
|
2009-03-06 09:33:24 +08:00
|
|
|
QualType R = getContext().VoidTy;
|
|
|
|
|
|
|
|
FunctionArgList Args;
|
|
|
|
// FIXME: This leaks
|
|
|
|
ImplicitParamDecl *Src =
|
|
|
|
ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
|
|
|
|
getContext().getPointerType(getContext().VoidTy));
|
|
|
|
|
|
|
|
Args.push_back(std::make_pair(Src, Src->getType()));
|
|
|
|
|
|
|
|
const CGFunctionInfo &FI =
|
|
|
|
CGM.getTypes().getFunctionInfo(R, Args);
|
|
|
|
|
2009-06-06 07:26:36 +08:00
|
|
|
// FIXME: We'd like to put these into a mergable by content, with
|
|
|
|
// internal linkage.
|
2009-03-06 09:33:24 +08:00
|
|
|
std::string Name = std::string("__destroy_helper_block_");
|
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
|
|
|
|
|
|
|
|
llvm::Function *Fn =
|
|
|
|
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
|
|
|
|
Name,
|
|
|
|
&CGM.getModule());
|
|
|
|
|
|
|
|
IdentifierInfo *II
|
|
|
|
= &CGM.getContext().Idents.get("__destroy_helper_block_");
|
|
|
|
|
|
|
|
FunctionDecl *FD = FunctionDecl::Create(getContext(),
|
|
|
|
getContext().getTranslationUnitDecl(),
|
|
|
|
SourceLocation(), II, R,
|
|
|
|
FunctionDecl::Static, false,
|
|
|
|
true);
|
|
|
|
CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
|
2009-03-07 10:53:18 +08:00
|
|
|
|
2009-04-11 02:52:28 +08:00
|
|
|
if (NoteForHelperp) {
|
|
|
|
std::vector<HelperInfo> &NoteForHelper = *NoteForHelperp;
|
|
|
|
|
|
|
|
llvm::Value *SrcObj = CGF.GetAddrOfLocalVar(Src);
|
|
|
|
llvm::Type *PtrPtrT;
|
2009-07-15 07:10:40 +08:00
|
|
|
PtrPtrT = VMContext.getPointerType(VMContext.getPointerType(T, 0), 0);
|
2009-04-11 02:52:28 +08:00
|
|
|
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,
|
2009-07-15 07:10:40 +08:00
|
|
|
VMContext.getPointerType(PtrToInt8Ty, 0));
|
2009-04-11 02:52:28 +08:00
|
|
|
Srcv = Builder.CreateLoad(Srcv);
|
|
|
|
|
|
|
|
BuildBlockRelease(Srcv, flag);
|
|
|
|
}
|
2009-03-07 10:53:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 09:33:24 +08:00
|
|
|
CGF.FinishFunction();
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty);
|
2009-03-06 09:33:24 +08:00
|
|
|
}
|
|
|
|
|
2009-03-07 10:35:30 +08:00
|
|
|
llvm::Constant *BlockFunction::BuildCopyHelper(const llvm::StructType *T,
|
2009-04-11 02:52:28 +08:00
|
|
|
std::vector<HelperInfo> *NoteForHelper) {
|
2009-03-07 10:35:30 +08:00
|
|
|
return CodeGenFunction(CGM).GenerateCopyHelperFunction(BlockHasCopyDispose,
|
|
|
|
T, NoteForHelper);
|
2009-03-06 09:33:24 +08:00
|
|
|
}
|
|
|
|
|
2009-03-07 10:35:30 +08:00
|
|
|
llvm::Constant *BlockFunction::BuildDestroyHelper(const llvm::StructType *T,
|
2009-04-11 02:52:28 +08:00
|
|
|
std::vector<HelperInfo> *NoteForHelperp) {
|
2009-03-07 10:35:30 +08:00
|
|
|
return CodeGenFunction(CGM).GenerateDestroyHelperFunction(BlockHasCopyDispose,
|
2009-04-11 02:52:28 +08:00
|
|
|
T, NoteForHelperp);
|
2009-03-04 11:23:46 +08:00
|
|
|
}
|
2009-03-05 09:23:13 +08:00
|
|
|
|
2009-03-06 14:12:24 +08:00
|
|
|
llvm::Constant *BlockFunction::
|
|
|
|
GeneratebyrefCopyHelperFunction(const llvm::Type *T, int flag) {
|
2009-03-06 10:29:21 +08:00
|
|
|
QualType R = getContext().VoidTy;
|
|
|
|
|
|
|
|
FunctionArgList Args;
|
|
|
|
// FIXME: This leaks
|
2009-03-06 14:12:24 +08:00
|
|
|
ImplicitParamDecl *Dst =
|
2009-03-06 10:29:21 +08:00
|
|
|
ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
|
|
|
|
getContext().getPointerType(getContext().VoidTy));
|
2009-03-06 14:12:24 +08:00
|
|
|
Args.push_back(std::make_pair(Dst, Dst->getType()));
|
2009-03-06 10:29:21 +08:00
|
|
|
|
2009-03-06 14:12:24 +08:00
|
|
|
// FIXME: This leaks
|
|
|
|
ImplicitParamDecl *Src =
|
|
|
|
ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
|
|
|
|
getContext().getPointerType(getContext().VoidTy));
|
2009-03-06 10:29:21 +08:00
|
|
|
Args.push_back(std::make_pair(Src, Src->getType()));
|
|
|
|
|
|
|
|
const CGFunctionInfo &FI =
|
|
|
|
CGM.getTypes().getFunctionInfo(R, Args);
|
|
|
|
|
|
|
|
std::string Name = std::string("__Block_byref_id_object_copy_");
|
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
|
|
|
|
|
2009-06-06 07:26:36 +08:00
|
|
|
// FIXME: We'd like to put these into a mergable by content, with
|
|
|
|
// internal linkage.
|
2009-03-06 10:29:21 +08:00
|
|
|
llvm::Function *Fn =
|
|
|
|
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
|
|
|
|
Name,
|
|
|
|
&CGM.getModule());
|
|
|
|
|
|
|
|
IdentifierInfo *II
|
|
|
|
= &CGM.getContext().Idents.get("__Block_byref_id_object_copy_");
|
|
|
|
|
|
|
|
FunctionDecl *FD = FunctionDecl::Create(getContext(),
|
|
|
|
getContext().getTranslationUnitDecl(),
|
|
|
|
SourceLocation(), II, R,
|
|
|
|
FunctionDecl::Static, false,
|
|
|
|
true);
|
|
|
|
CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
|
2009-03-06 14:12:24 +08:00
|
|
|
|
|
|
|
// dst->x
|
|
|
|
llvm::Value *V = CGF.GetAddrOfLocalVar(Dst);
|
2009-07-15 07:10:40 +08:00
|
|
|
V = Builder.CreateBitCast(V, VMContext.getPointerType(T, 0));
|
2009-04-16 06:11:36 +08:00
|
|
|
V = Builder.CreateLoad(V);
|
2009-03-06 14:12:24 +08:00
|
|
|
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");
|
2009-07-15 07:10:40 +08:00
|
|
|
V = Builder.CreateBitCast(V, VMContext.getPointerType(PtrToInt8Ty, 0));
|
2009-03-06 14:12:24 +08:00
|
|
|
llvm::Value *SrcObj = Builder.CreateLoad(V);
|
|
|
|
|
|
|
|
flag |= BLOCK_BYREF_CALLER;
|
|
|
|
|
2009-07-25 07:12:58 +08:00
|
|
|
llvm::Value *N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
|
2009-03-06 14:12:24 +08:00
|
|
|
llvm::Value *F = getBlockObjectAssign();
|
|
|
|
Builder.CreateCall3(F, DstObj, SrcObj, N);
|
|
|
|
|
2009-03-06 10:29:21 +08:00
|
|
|
CGF.FinishFunction();
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty);
|
2009-03-06 10:29:21 +08:00
|
|
|
}
|
|
|
|
|
2009-03-06 12:53:30 +08:00
|
|
|
llvm::Constant *
|
|
|
|
BlockFunction::GeneratebyrefDestroyHelperFunction(const llvm::Type *T,
|
|
|
|
int flag) {
|
2009-03-06 10:29:21 +08:00
|
|
|
QualType R = getContext().VoidTy;
|
|
|
|
|
|
|
|
FunctionArgList Args;
|
|
|
|
// FIXME: This leaks
|
|
|
|
ImplicitParamDecl *Src =
|
|
|
|
ImplicitParamDecl::Create(getContext(), 0, SourceLocation(), 0,
|
|
|
|
getContext().getPointerType(getContext().VoidTy));
|
|
|
|
|
|
|
|
Args.push_back(std::make_pair(Src, Src->getType()));
|
|
|
|
|
|
|
|
const CGFunctionInfo &FI =
|
|
|
|
CGM.getTypes().getFunctionInfo(R, Args);
|
|
|
|
|
|
|
|
std::string Name = std::string("__Block_byref_id_object_dispose_");
|
|
|
|
CodeGenTypes &Types = CGM.getTypes();
|
|
|
|
const llvm::FunctionType *LTy = Types.GetFunctionType(FI, false);
|
|
|
|
|
2009-06-06 07:26:36 +08:00
|
|
|
// FIXME: We'd like to put these into a mergable by content, with
|
|
|
|
// internal linkage.
|
2009-03-06 10:29:21 +08:00
|
|
|
llvm::Function *Fn =
|
|
|
|
llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
|
|
|
|
Name,
|
|
|
|
&CGM.getModule());
|
|
|
|
|
|
|
|
IdentifierInfo *II
|
|
|
|
= &CGM.getContext().Idents.get("__Block_byref_id_object_dispose_");
|
|
|
|
|
|
|
|
FunctionDecl *FD = FunctionDecl::Create(getContext(),
|
|
|
|
getContext().getTranslationUnitDecl(),
|
|
|
|
SourceLocation(), II, R,
|
|
|
|
FunctionDecl::Static, false,
|
|
|
|
true);
|
|
|
|
CGF.StartFunction(FD, R, Fn, Args, SourceLocation());
|
2009-03-06 12:53:30 +08:00
|
|
|
|
|
|
|
llvm::Value *V = CGF.GetAddrOfLocalVar(Src);
|
2009-07-15 07:10:40 +08:00
|
|
|
V = Builder.CreateBitCast(V, VMContext.getPointerType(T, 0));
|
2009-04-16 06:11:36 +08:00
|
|
|
V = Builder.CreateLoad(V);
|
2009-03-06 12:53:30 +08:00
|
|
|
V = Builder.CreateStructGEP(V, 6, "x");
|
2009-07-15 07:10:40 +08:00
|
|
|
V = Builder.CreateBitCast(V, VMContext.getPointerType(PtrToInt8Ty, 0));
|
2009-04-16 06:11:36 +08:00
|
|
|
V = Builder.CreateLoad(V);
|
2009-03-06 12:53:30 +08:00
|
|
|
|
|
|
|
flag |= BLOCK_BYREF_CALLER;
|
|
|
|
BuildBlockRelease(V, flag);
|
2009-03-06 10:29:21 +08:00
|
|
|
CGF.FinishFunction();
|
|
|
|
|
2009-07-15 07:10:40 +08:00
|
|
|
return VMContext.getConstantExprBitCast(Fn, PtrToInt8Ty);
|
2009-03-06 10:29:21 +08:00
|
|
|
}
|
|
|
|
|
2009-03-06 14:12:24 +08:00
|
|
|
llvm::Constant *BlockFunction::BuildbyrefCopyHelper(const llvm::Type *T,
|
2009-06-06 07:26:36 +08:00
|
|
|
int flag, 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);
|
|
|
|
// 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.
|
|
|
|
uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
|
|
|
|
llvm::Constant *& Entry = CGM.AssignCache[kind];
|
|
|
|
if (Entry)
|
|
|
|
return Entry;
|
|
|
|
return Entry=CodeGenFunction(CGM).GeneratebyrefCopyHelperFunction(T, flag);
|
2009-03-06 10:29:21 +08:00
|
|
|
}
|
|
|
|
|
2009-03-06 12:53:30 +08:00
|
|
|
llvm::Constant *BlockFunction::BuildbyrefDestroyHelper(const llvm::Type *T,
|
2009-06-06 07:26:36 +08:00
|
|
|
int flag,
|
|
|
|
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);
|
|
|
|
// 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.
|
|
|
|
uint64_t kind = (uint64_t)Align*BLOCK_BYREF_CURRENT_MAX + flag;
|
|
|
|
llvm::Constant *& Entry = CGM.DestroyCache[kind];
|
|
|
|
if (Entry)
|
|
|
|
return Entry;
|
|
|
|
return Entry=CodeGenFunction(CGM).GeneratebyrefDestroyHelperFunction(T, flag);
|
2009-03-06 10:29:21 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 09:23:13 +08:00
|
|
|
llvm::Value *BlockFunction::getBlockObjectDispose() {
|
|
|
|
if (CGM.BlockObjectDispose == 0) {
|
|
|
|
const llvm::FunctionType *FTy;
|
|
|
|
std::vector<const llvm::Type*> ArgTys;
|
|
|
|
const llvm::Type *ResultType = llvm::Type::VoidTy;
|
|
|
|
ArgTys.push_back(PtrToInt8Ty);
|
|
|
|
ArgTys.push_back(llvm::Type::Int32Ty);
|
2009-07-15 07:10:40 +08:00
|
|
|
FTy = VMContext.getFunctionType(ResultType, ArgTys, false);
|
2009-03-05 16:32:30 +08:00
|
|
|
CGM.BlockObjectDispose
|
2009-03-05 09:23:13 +08:00
|
|
|
= CGM.CreateRuntimeFunction(FTy, "_Block_object_dispose");
|
|
|
|
}
|
|
|
|
return CGM.BlockObjectDispose;
|
|
|
|
}
|
|
|
|
|
2009-03-06 14:12:24 +08:00
|
|
|
llvm::Value *BlockFunction::getBlockObjectAssign() {
|
|
|
|
if (CGM.BlockObjectAssign == 0) {
|
|
|
|
const llvm::FunctionType *FTy;
|
|
|
|
std::vector<const llvm::Type*> ArgTys;
|
|
|
|
const llvm::Type *ResultType = llvm::Type::VoidTy;
|
|
|
|
ArgTys.push_back(PtrToInt8Ty);
|
|
|
|
ArgTys.push_back(PtrToInt8Ty);
|
|
|
|
ArgTys.push_back(llvm::Type::Int32Ty);
|
2009-07-15 07:10:40 +08:00
|
|
|
FTy = VMContext.getFunctionType(ResultType, ArgTys, false);
|
2009-03-06 14:12:24 +08:00
|
|
|
CGM.BlockObjectAssign
|
|
|
|
= CGM.CreateRuntimeFunction(FTy, "_Block_object_assign");
|
|
|
|
}
|
|
|
|
return CGM.BlockObjectAssign;
|
|
|
|
}
|
|
|
|
|
2009-03-06 12:53:30 +08:00
|
|
|
void BlockFunction::BuildBlockRelease(llvm::Value *V, int flag) {
|
2009-03-05 09:23:13 +08:00
|
|
|
llvm::Value *F = getBlockObjectDispose();
|
2009-03-06 12:53:30 +08:00
|
|
|
llvm::Value *N;
|
2009-03-05 09:23:13 +08:00
|
|
|
V = Builder.CreateBitCast(V, PtrToInt8Ty);
|
2009-07-25 07:12:58 +08:00
|
|
|
N = llvm::ConstantInt::get(llvm::Type::Int32Ty, flag);
|
2009-03-05 09:23:13 +08:00
|
|
|
Builder.CreateCall2(F, V, N);
|
|
|
|
}
|
2009-03-05 16:32:30 +08:00
|
|
|
|
|
|
|
ASTContext &BlockFunction::getContext() const { return CGM.getContext(); }
|
2009-03-07 10:35:30 +08:00
|
|
|
|
|
|
|
BlockFunction::BlockFunction(CodeGenModule &cgm, CodeGenFunction &cgf,
|
|
|
|
CGBuilderTy &B)
|
2009-07-15 07:10:40 +08:00
|
|
|
: CGM(cgm), CGF(cgf), VMContext(cgm.getLLVMContext()), Builder(B) {
|
|
|
|
PtrToInt8Ty = VMContext.getPointerTypeUnqual(llvm::Type::Int8Ty);
|
2009-03-07 10:35:30 +08:00
|
|
|
|
|
|
|
BlockHasCopyDispose = false;
|
|
|
|
}
|