Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 1 | //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit blocks. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CodeGenFunction.h" |
| 15 | #include "CodeGenModule.h" |
| 16 | #include "llvm/Module.h" |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 17 | #include "llvm/Target/TargetData.h" |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 18 | |
| 19 | #include <algorithm> |
| 20 | |
| 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 24 | // Block flags |
| 25 | enum { |
| 26 | IsGlobal = 1 << 28 |
| 27 | }; |
| 28 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 29 | const llvm::Type *CodeGenModule::getBlockDescriptorType() { |
| 30 | if (BlockDescriptorType) |
| 31 | return BlockDescriptorType; |
| 32 | |
| 33 | const llvm::Type *UnsignedLongTy = |
| 34 | getTypes().ConvertType(getContext().UnsignedLongTy); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 35 | |
Mike Stump | ab69514 | 2009-02-13 15:16:56 +0000 | [diff] [blame] | 36 | // struct __block_descriptor { |
| 37 | // unsigned long reserved; |
| 38 | // unsigned long block_size; |
| 39 | // }; |
| 40 | BlockDescriptorType = llvm::StructType::get(UnsignedLongTy, |
| 41 | UnsignedLongTy, |
| 42 | NULL); |
| 43 | |
| 44 | getModule().addTypeName("struct.__block_descriptor", |
| 45 | BlockDescriptorType); |
| 46 | |
| 47 | return BlockDescriptorType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame^] | 50 | const llvm::Type * |
| 51 | CodeGenModule::getGenericBlockLiteralType() { |
| 52 | if (GenericBlockLiteralType) |
| 53 | return GenericBlockLiteralType; |
| 54 | |
| 55 | const llvm::Type *Int8PtrTy = |
| 56 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 57 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame^] | 58 | const llvm::Type *BlockDescPtrTy = |
| 59 | llvm::PointerType::getUnqual(getBlockDescriptorType()); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 60 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame^] | 61 | // struct __block_literal_generic { |
| 62 | // void *isa; |
| 63 | // int flags; |
| 64 | // int reserved; |
| 65 | // void (*invoke)(void *); |
| 66 | // struct __block_descriptor *descriptor; |
| 67 | // }; |
| 68 | GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy, |
| 69 | llvm::Type::Int32Ty, |
| 70 | llvm::Type::Int32Ty, |
| 71 | Int8PtrTy, |
| 72 | BlockDescPtrTy, |
| 73 | NULL); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 74 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame^] | 75 | getModule().addTypeName("struct.__block_literal_generic", |
| 76 | GenericBlockLiteralType); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 77 | |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame^] | 78 | return GenericBlockLiteralType; |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /// getBlockFunctionType - Given a BlockPointerType, will return the |
| 82 | /// function type for the block, including the first block literal argument. |
| 83 | static QualType getBlockFunctionType(ASTContext &Ctx, |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 84 | const BlockPointerType *BPT) { |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 85 | const FunctionTypeProto *FTy = cast<FunctionTypeProto>(BPT->getPointeeType()); |
| 86 | |
| 87 | llvm::SmallVector<QualType, 8> Types; |
| 88 | Types.push_back(Ctx.getPointerType(Ctx.VoidTy)); |
| 89 | |
| 90 | for (FunctionTypeProto::arg_type_iterator i = FTy->arg_type_begin(), |
| 91 | e = FTy->arg_type_end(); i != e; ++i) |
| 92 | Types.push_back(*i); |
| 93 | |
| 94 | return Ctx.getFunctionType(FTy->getResultType(), |
| 95 | &Types[0], Types.size(), |
| 96 | FTy->isVariadic(), 0); |
| 97 | } |
| 98 | |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 99 | RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) { |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 100 | const BlockPointerType *BPT = |
| 101 | E->getCallee()->getType()->getAsBlockPointerType(); |
| 102 | |
| 103 | llvm::Value *Callee = EmitScalarExpr(E->getCallee()); |
| 104 | |
| 105 | // Get a pointer to the generic block literal. |
| 106 | const llvm::Type *BlockLiteralTy = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame^] | 107 | llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType()); |
Anders Carlsson | acfde80 | 2009-02-12 00:39:25 +0000 | [diff] [blame] | 108 | |
| 109 | // Bitcast the callee to a block literal. |
| 110 | llvm::Value *BlockLiteral = |
| 111 | Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal"); |
| 112 | |
| 113 | // Get the function pointer from the literal. |
| 114 | llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp"); |
| 115 | llvm::Value *Func = Builder.CreateLoad(FuncPtr, FuncPtr, "tmp"); |
| 116 | |
| 117 | // Cast the function pointer to the right type. |
| 118 | const llvm::Type *BlockFTy = |
| 119 | ConvertType(getBlockFunctionType(getContext(), BPT)); |
| 120 | const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy); |
| 121 | Func = Builder.CreateBitCast(Func, BlockFTyPtr); |
| 122 | |
| 123 | BlockLiteral = |
| 124 | Builder.CreateBitCast(BlockLiteral, |
| 125 | llvm::PointerType::getUnqual(llvm::Type::Int8Ty), |
| 126 | "tmp"); |
| 127 | |
| 128 | // Add the block literal. |
| 129 | QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy); |
| 130 | CallArgList Args; |
| 131 | Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy)); |
| 132 | |
| 133 | // And the rest of the arguments. |
| 134 | for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end(); |
| 135 | i != e; ++i) |
| 136 | Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), |
| 137 | i->getType())); |
| 138 | |
| 139 | // And call the block. |
| 140 | return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args), |
| 141 | Func, Args); |
| 142 | } |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 143 | |
| 144 | llvm::Constant *CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE) { |
| 145 | if (!NSConcreteGlobalBlock) { |
| 146 | const llvm::Type *Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty); |
| 147 | |
| 148 | // FIXME: Wee should have a CodeGenModule::AddRuntimeVariable that does the |
| 149 | // same thing as CreateRuntimeFunction if there's already a variable with |
| 150 | // the same name. |
| 151 | NSConcreteGlobalBlock = |
| 152 | new llvm::GlobalVariable(Ty, false, |
| 153 | llvm::GlobalVariable::ExternalLinkage, 0, |
| 154 | "_NSConcreteGlobalBlock", &getModule()); |
| 155 | } |
| 156 | |
| 157 | // Generate the block descriptor. |
| 158 | const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy); |
| 159 | |
| 160 | llvm::Constant *DescriptorFields[2]; |
| 161 | |
| 162 | // Reserved |
| 163 | DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy); |
| 164 | |
| 165 | // Block literal size. For global blocks we just use the size of the generic |
| 166 | // block literal struct. |
| 167 | uint64_t BlockLiteralSize = |
Mike Stump | 9b8a797 | 2009-02-13 15:25:34 +0000 | [diff] [blame^] | 168 | TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8; |
Anders Carlsson | d5cab54 | 2009-02-12 17:55:02 +0000 | [diff] [blame] | 169 | DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize); |
| 170 | |
| 171 | llvm::Constant *DescriptorStruct = |
| 172 | llvm::ConstantStruct::get(&DescriptorFields[0], 2); |
| 173 | |
| 174 | llvm::GlobalVariable *Descriptor = |
| 175 | new llvm::GlobalVariable(DescriptorStruct->getType(), true, |
| 176 | llvm::GlobalVariable::InternalLinkage, |
| 177 | DescriptorStruct, "__block_descriptor_global", |
| 178 | &getModule()); |
| 179 | |
| 180 | // Generate the constants for the block literal. |
| 181 | llvm::Constant *LiteralFields[5]; |
| 182 | |
| 183 | CodeGenFunction::BlockInfo Info(0, "global"); |
| 184 | llvm::Function *Fn = CodeGenFunction(*this).GenerateBlockFunction(BE, Info); |
| 185 | |
| 186 | // isa |
| 187 | LiteralFields[0] = NSConcreteGlobalBlock; |
| 188 | |
| 189 | // Flags |
| 190 | LiteralFields[1] = llvm::ConstantInt::get(llvm::Type::Int32Ty, IsGlobal); |
| 191 | |
| 192 | // Reserved |
| 193 | LiteralFields[2] = llvm::Constant::getNullValue(llvm::Type::Int32Ty); |
| 194 | |
| 195 | // Function |
| 196 | LiteralFields[3] = Fn; |
| 197 | |
| 198 | // Descriptor |
| 199 | LiteralFields[4] = Descriptor; |
| 200 | |
| 201 | llvm::Constant *BlockLiteralStruct = |
| 202 | llvm::ConstantStruct::get(&LiteralFields[0], 5); |
| 203 | |
| 204 | llvm::GlobalVariable *BlockLiteral = |
| 205 | new llvm::GlobalVariable(BlockLiteralStruct->getType(), true, |
| 206 | llvm::GlobalVariable::InternalLinkage, |
| 207 | BlockLiteralStruct, "__block_literal_global", |
| 208 | &getModule()); |
| 209 | |
| 210 | return BlockLiteral; |
| 211 | } |
| 212 | |
| 213 | llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *Expr, |
| 214 | const BlockInfo& Info) |
| 215 | { |
| 216 | const FunctionTypeProto *FTy = |
| 217 | cast<FunctionTypeProto>(Expr->getFunctionType()); |
| 218 | |
| 219 | FunctionArgList Args; |
| 220 | |
| 221 | const BlockDecl *BD = Expr->getBlockDecl(); |
| 222 | |
| 223 | // FIXME: This leaks |
| 224 | ImplicitParamDecl *SelfDecl = |
| 225 | ImplicitParamDecl::Create(getContext(), 0, |
| 226 | SourceLocation(), 0, |
| 227 | getContext().getPointerType(getContext().VoidTy)); |
| 228 | |
| 229 | Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType())); |
| 230 | |
| 231 | for (BlockDecl::param_iterator i = BD->param_begin(), |
| 232 | e = BD->param_end(); i != e; ++i) |
| 233 | Args.push_back(std::make_pair(*e, (*e)->getType())); |
| 234 | |
| 235 | const CGFunctionInfo &FI = |
| 236 | CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args); |
| 237 | |
| 238 | std::string Name = std::string("__block_function_") + Info.NameSuffix; |
| 239 | |
| 240 | CodeGenTypes &Types = CGM.getTypes(); |
| 241 | const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic()); |
| 242 | |
| 243 | llvm::Function *Fn = |
| 244 | llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage, |
| 245 | Name, |
| 246 | &CGM.getModule()); |
| 247 | |
| 248 | StartFunction(BD, FTy->getResultType(), Fn, Args, |
| 249 | Expr->getBody()->getLocEnd()); |
| 250 | EmitStmt(Expr->getBody()); |
| 251 | FinishFunction(cast<CompoundStmt>(Expr->getBody())->getRBracLoc()); |
| 252 | |
| 253 | return Fn; |
| 254 | } |
| 255 | |
| 256 | |