Skip to content
CGBlocks.cpp 14.5 KiB
Newer Older
//===--- 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"
#include "llvm/Module.h"

#include <algorithm>

using namespace clang;
using namespace CodeGen;

  BLOCK_NEEDS_FREE =        (1 << 24),
  BLOCK_HAS_COPY_DISPOSE =  (1 << 25),
  BLOCK_HAS_CXX_OBJ =       (1 << 26),
  BLOCK_IS_GC =             (1 << 27),
  BLOCK_IS_GLOBAL =         (1 << 28),
  BLOCK_HAS_DESCRIPTOR =    (1 << 29)
llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl() {
  // FIXME: Push up.
  bool BlockHasCopyDispose = false;

  const llvm::PointerType *PtrToInt8Ty
    = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump's avatar
Mike Stump committed
  const llvm::Type *UnsignedLongTy
    = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
  llvm::Constant *C;
  std::vector<llvm::Constant*> Elts;

  // reserved
Mike Stump's avatar
Mike Stump committed
  C = llvm::ConstantInt::get(UnsignedLongTy, 0);
  Elts.push_back(C);

  // Size
  int sz;
  if (!BlockHasCopyDispose)
    sz = CGM.getTargetData()
      .getTypeStoreSizeInBits(CGM.getGenericBlockLiteralType()) / 8;
  else
    sz = CGM.getTargetData()
      .getTypeStoreSizeInBits(CGM.getGenericExtendedBlockLiteralType()) / 8;
Mike Stump's avatar
Mike Stump committed
  C = llvm::ConstantInt::get(UnsignedLongTy, sz);
  Elts.push_back(C);

  if (BlockHasCopyDispose) {
    // copy_func_helper_decl
Mike Stump's avatar
Mike Stump committed
    C = llvm::ConstantInt::get(UnsignedLongTy, 0);
    C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
    Elts.push_back(C);

    // destroy_func_decl
Mike Stump's avatar
Mike Stump committed
    C = llvm::ConstantInt::get(UnsignedLongTy, 0);
    C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
    Elts.push_back(C);
  }

  C = llvm::ConstantStruct::get(Elts);

  C = new llvm::GlobalVariable(C->getType(), true,
                               llvm::GlobalValue::InternalLinkage,
                               C, "__block_descriptor_tmp", &CGM.getModule());
llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
  if (NSConcreteGlobalBlock)
    return NSConcreteGlobalBlock;

  const llvm::PointerType *PtrToInt8Ty
    = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump's avatar
Mike Stump committed
  // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
  // same thing as CreateRuntimeFunction if there's already a variable with
  // the same name.
  NSConcreteGlobalBlock
    = new llvm::GlobalVariable(PtrToInt8Ty, false, 
                               llvm::GlobalValue::ExternalLinkage,
                               0, "_NSConcreteGlobalBlock",
                               &getModule());

  return NSConcreteGlobalBlock;
}

llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
  if (NSConcreteStackBlock)
    return NSConcreteStackBlock;

  const llvm::PointerType *PtrToInt8Ty
    = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump's avatar
Mike Stump committed
  // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
  // same thing as CreateRuntimeFunction if there's already a variable with
  // the same name.
  NSConcreteStackBlock
    = new llvm::GlobalVariable(PtrToInt8Ty, false, 
                               llvm::GlobalValue::ExternalLinkage,
                               0, "_NSConcreteStackBlock",
                               &getModule());

  return NSConcreteStackBlock;
}

// FIXME: Push most into CGM, passing down a few bits, like current
// function name.
llvm::Constant *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
  // FIXME: Push up
  bool BlockHasCopyDispose = false;
  bool insideFunction = false;
  bool BlockRefDeclList = false;
  bool BlockByrefDeclList = false;

  std::vector<llvm::Constant*> Elts;
  llvm::Constant *C;

  {
    // C = BuildBlockStructInitlist();
    unsigned int flags = BLOCK_HAS_DESCRIPTOR;

    if (BlockHasCopyDispose)
      flags |= BLOCK_HAS_COPY_DISPOSE;

    // __isa
    C = CGM.getNSConcreteStackBlock();
    if (!insideFunction ||
        (!BlockRefDeclList && !BlockByrefDeclList)) {
      C = CGM.getNSConcreteGlobalBlock();
      flags |= BLOCK_IS_GLOBAL;
    }
    const llvm::PointerType *PtrToInt8Ty
      = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
    C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
    Elts.push_back(C);

    // __flags
    const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
      CGM.getTypes().ConvertType(CGM.getContext().IntTy));
    C = llvm::ConstantInt::get(IntTy, flags);
    Elts.push_back(C);

    // __reserved
    C = llvm::ConstantInt::get(IntTy, 0);
    Elts.push_back(C);

Mike Stump's avatar
Mike Stump committed
    const char *Name = "";
    if (const NamedDecl *ND = dyn_cast<NamedDecl>(CurFuncDecl))
      if (ND->getIdentifier())
        Name = ND->getNameAsCString();
    BlockInfo Info(0, Name);
    llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
    Elts.push_back(Fn);

    // __descriptor
    Elts.push_back(BuildDescriptorBlockDecl());

    // FIXME: Add block_original_ref_decl_list and block_byref_decl_list.
  }
  
  C = llvm::ConstantStruct::get(Elts);

  char Name[32];
  sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
  C = new llvm::GlobalVariable(C->getType(), true,
                               llvm::GlobalValue::InternalLinkage,
                               C, Name, &CGM.getModule());
  QualType BPT = BE->getType();
  C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
const llvm::Type *CodeGenModule::getBlockDescriptorType() {
  if (BlockDescriptorType)
    return BlockDescriptorType;

Mike Stump's avatar
Mike Stump committed
  const llvm::Type *UnsignedLongTy =
    getTypes().ConvertType(getContext().UnsignedLongTy);
Mike Stump's avatar
Mike Stump committed

  // struct __block_descriptor {
  //   unsigned long reserved;
  //   unsigned long block_size;
  // };
Mike Stump's avatar
Mike Stump committed
  BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
                                              UnsignedLongTy,
                                              NULL);

  getModule().addTypeName("struct.__block_descriptor",
                          BlockDescriptorType);

  return BlockDescriptorType;
const llvm::Type *
CodeGenModule::getGenericBlockLiteralType() {
  if (GenericBlockLiteralType)
    return GenericBlockLiteralType;

Mike Stump's avatar
Mike Stump committed
  const llvm::Type *Int8PtrTy =
    llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Mike Stump's avatar
Mike Stump committed

  const llvm::Type *BlockDescPtrTy =
    llvm::PointerType::getUnqual(getBlockDescriptorType());
Mike Stump's avatar
Mike Stump committed

  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;
  // };
  GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
                                                  Int8PtrTy,
                                                  BlockDescPtrTy,
                                                  NULL);
Mike Stump's avatar
Mike Stump committed

  getModule().addTypeName("struct.__block_literal_generic",
                          GenericBlockLiteralType);
Mike Stump's avatar
Mike Stump committed

  return GenericBlockLiteralType;
const llvm::Type *
CodeGenModule::getGenericExtendedBlockLiteralType() {
  if (GenericExtendedBlockLiteralType)
    return GenericExtendedBlockLiteralType;

  const llvm::Type *Int8PtrTy =
    llvm::PointerType::getUnqual(llvm::Type::Int8Ty);

  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;
  //   void *__copy_func_helper_decl;
  //   void *__destroy_func_decl;
  // };
  GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
                                                          IntTy,
                                                          IntTy,
                                                          Int8PtrTy,
                                                          BlockDescPtrTy,
                                                          Int8PtrTy,
                                                          Int8PtrTy,
                                                          NULL);

  getModule().addTypeName("struct.__block_literal_extended_generic",
                          GenericExtendedBlockLiteralType);

  return GenericExtendedBlockLiteralType;
}

Mike Stump's avatar
Mike Stump committed
/// getBlockFunctionType - Given a BlockPointerType, will return the
/// function type for the block, including the first block literal argument.
static QualType getBlockFunctionType(ASTContext &Ctx,
  const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType());
Mike Stump's avatar
Mike Stump committed

  llvm::SmallVector<QualType, 8> Types;
  Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
Mike Stump's avatar
Mike Stump committed

  for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(),
       e = FTy->arg_type_end(); i != e; ++i)
    Types.push_back(*i);
Mike Stump's avatar
Mike Stump committed

  return Ctx.getFunctionType(FTy->getResultType(),
Mike Stump's avatar
Mike Stump committed
                             &Types[0], Types.size(),
RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
Mike Stump's avatar
Mike Stump committed
  const BlockPointerType *BPT =
    E->getCallee()->getType()->getAsBlockPointerType();
Mike Stump's avatar
Mike Stump committed

  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.
Mike Stump's avatar
Mike Stump committed
  llvm::Value *BlockLiteral =
    Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");

  // Get the function pointer from the literal.
  llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
  llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp");

  // Cast the function pointer to the right type.
Mike Stump's avatar
Mike Stump committed
  const llvm::Type *BlockFTy =
    ConvertType(getBlockFunctionType(getContext(), BPT));
  const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
  Func = Builder.CreateBitCast(Func, BlockFTyPtr);

Mike Stump's avatar
Mike Stump committed
  BlockLiteral =
    Builder.CreateBitCast(BlockLiteral,
                          llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
                          "tmp");
Mike Stump's avatar
Mike Stump committed

  // Add the block literal.
  QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
  CallArgList Args;
  Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
Mike Stump's avatar
Mike Stump committed

  // And the rest of the arguments.
Mike Stump's avatar
Mike Stump committed
  for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
Mike Stump's avatar
Mike Stump committed
    Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
Mike Stump's avatar
Mike Stump committed

  // And call the block.
Mike Stump's avatar
Mike Stump committed
  return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
CodeGenModule::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));
Mike Stump's avatar
Mike Stump committed

Mike Stump's avatar
Mike Stump committed

  // Reserved
  DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
Mike Stump's avatar
Mike Stump committed

  // Block literal size. For global blocks we just use the size of the generic
  // block literal struct.
Mike Stump's avatar
Mike Stump committed
  uint64_t BlockLiteralSize =
    TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
  DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
Mike Stump's avatar
Mike Stump committed

  llvm::Constant *DescriptorStruct =
    llvm::ConstantStruct::get(&DescriptorFields[0], 2);
Mike Stump's avatar
Mike Stump committed

  llvm::GlobalVariable *Descriptor =
    new llvm::GlobalVariable(DescriptorStruct->getType(), true,
Mike Stump's avatar
Mike Stump committed
                             llvm::GlobalVariable::InternalLinkage,
                             DescriptorStruct, "__block_descriptor_global",
Mike Stump's avatar
Mike Stump committed

  // Generate the constants for the block literal.
  llvm::Constant *LiteralFields[5];
Mike Stump's avatar
Mike Stump committed

  CodeGenFunction::BlockInfo Info(0, n);
  llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info);
Mike Stump's avatar
Mike Stump committed

  LiteralFields[0] = getNSConcreteGlobalBlock();
Mike Stump's avatar
Mike Stump committed

  LiteralFields[1] = llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL);
Mike Stump's avatar
Mike Stump committed

  LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
Mike Stump's avatar
Mike Stump committed

Mike Stump's avatar
Mike Stump committed

  // Descriptor
  LiteralFields[4] = Descriptor;
Mike Stump's avatar
Mike Stump committed

  llvm::Constant *BlockLiteralStruct =
    llvm::ConstantStruct::get(&LiteralFields[0], 5);
Mike Stump's avatar
Mike Stump committed

  llvm::GlobalVariable *BlockLiteral =
    new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
Mike Stump's avatar
Mike Stump committed
                             llvm::GlobalVariable::InternalLinkage,
                             BlockLiteralStruct, "__block_literal_global",
Mike Stump's avatar
Mike Stump committed

  return BlockLiteral;
}

llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr,
                                                       const BlockInfo& Info)
{
Mike Stump's avatar
Mike Stump committed
  const FunctionTypeProto *FTy =
    cast<FunctionTypeProto>(Expr->getFunctionType());
Mike Stump's avatar
Mike Stump committed

Mike Stump's avatar
Mike Stump committed

  const BlockDecl *BD = Expr->getBlockDecl();

  // FIXME: This leaks
Mike Stump's avatar
Mike Stump committed
  ImplicitParamDecl *SelfDecl =
    ImplicitParamDecl::Create(getContext(), 0,
                              SourceLocation(), 0,
                              getContext().getPointerType(getContext().VoidTy));
Mike Stump's avatar
Mike Stump committed

  Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
Mike Stump's avatar
Mike Stump committed

  for (BlockDecl::param_iterator i = BD->param_begin(),
    Args.push_back(std::make_pair(*i, (*i)->getType()));
Mike Stump's avatar
Mike Stump committed

  const CGFunctionInfo &FI =
    CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);

  std::string Name = std::string("__") + Info.Name + "_block_invoke_";
  CodeGenTypes &Types = CGM.getTypes();
  const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
Mike Stump's avatar
Mike Stump committed

  llvm::Function *Fn =
    llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
                           Name,
                           &CGM.getModule());
Mike Stump's avatar
Mike Stump committed

  StartFunction(BD, FTy->getResultType(), Fn, Args,
                Expr->getBody()->getLocEnd());
  EmitStmt(Expr->getBody());
  FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc());

  return Fn;
}