12437cbfaSAnders Carlsson //===--- CGBlocks.cpp - Emit LLVM Code for declarations -------------------===//
22437cbfaSAnders Carlsson //
32437cbfaSAnders Carlsson //                     The LLVM Compiler Infrastructure
42437cbfaSAnders Carlsson //
52437cbfaSAnders Carlsson // This file is distributed under the University of Illinois Open Source
62437cbfaSAnders Carlsson // License. See LICENSE.TXT for details.
72437cbfaSAnders Carlsson //
82437cbfaSAnders Carlsson //===----------------------------------------------------------------------===//
92437cbfaSAnders Carlsson //
102437cbfaSAnders Carlsson // This contains code to emit blocks.
112437cbfaSAnders Carlsson //
122437cbfaSAnders Carlsson //===----------------------------------------------------------------------===//
132437cbfaSAnders Carlsson 
142437cbfaSAnders Carlsson #include "CodeGenFunction.h"
152437cbfaSAnders Carlsson #include "CodeGenModule.h"
162437cbfaSAnders Carlsson #include "llvm/Module.h"
176a60fa24SAnders Carlsson #include "llvm/Target/TargetData.h"
182437cbfaSAnders Carlsson 
192437cbfaSAnders Carlsson #include <algorithm>
202437cbfaSAnders Carlsson 
212437cbfaSAnders Carlsson using namespace clang;
222437cbfaSAnders Carlsson using namespace CodeGen;
232437cbfaSAnders Carlsson 
24*0874160bSMike Stump // Temporary code to enable testing of __block variables
25*0874160bSMike Stump // #include "clang/Frontend/CompileOptions.h"
26*0874160bSMike Stump #include "llvm/Support/CommandLine.h"
27*0874160bSMike Stump static llvm::cl::opt<bool>
28*0874160bSMike Stump Enable__block("f__block",
29*0874160bSMike Stump               // See all the FIXMEs for the various work that needs to be done
30*0874160bSMike Stump               llvm::cl::desc("temporary option to turn on __block precessing "
31*0874160bSMike Stump                              "even though the code isn't done yet"),
32*0874160bSMike Stump               llvm::cl::ValueDisallowed, llvm::cl::AllowInverse,
33*0874160bSMike Stump               llvm::cl::ZeroOrMore);
34*0874160bSMike Stump 
35*0874160bSMike Stump 
36cb2fbcb0SMike Stump llvm::Constant *CodeGenFunction::BuildDescriptorBlockDecl(uint64_t Size) {
3785284bacSMike Stump   const llvm::PointerType *PtrToInt8Ty
3885284bacSMike Stump     = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
39c2c38331SMike Stump   const llvm::Type *UnsignedLongTy
40c2c38331SMike Stump     = CGM.getTypes().ConvertType(getContext().UnsignedLongTy);
4185284bacSMike Stump   llvm::Constant *C;
4285284bacSMike Stump   std::vector<llvm::Constant*> Elts;
4385284bacSMike Stump 
4485284bacSMike Stump   // reserved
45c2c38331SMike Stump   C = llvm::ConstantInt::get(UnsignedLongTy, 0);
4685284bacSMike Stump   Elts.push_back(C);
4785284bacSMike Stump 
4885284bacSMike Stump   // Size
492ac40a9fSMike Stump   // FIXME: What is the right way to say this doesn't fit?  We should give
502ac40a9fSMike Stump   // a user diagnostic in that case.  Better fix would be to change the
512ac40a9fSMike Stump   // API to size_t.
52cb2fbcb0SMike Stump   C = llvm::ConstantInt::get(UnsignedLongTy, Size);
5385284bacSMike Stump   Elts.push_back(C);
5485284bacSMike Stump 
5585284bacSMike Stump   if (BlockHasCopyDispose) {
5685284bacSMike Stump     // copy_func_helper_decl
57cb2fbcb0SMike Stump     // FIXME: implement
58c2c38331SMike Stump     C = llvm::ConstantInt::get(UnsignedLongTy, 0);
5985284bacSMike Stump     C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
6085284bacSMike Stump     Elts.push_back(C);
6185284bacSMike Stump 
6285284bacSMike Stump     // destroy_func_decl
63cb2fbcb0SMike Stump     // FIXME: implement
64c2c38331SMike Stump     C = llvm::ConstantInt::get(UnsignedLongTy, 0);
6585284bacSMike Stump     C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
6685284bacSMike Stump     Elts.push_back(C);
6785284bacSMike Stump   }
6885284bacSMike Stump 
6985284bacSMike Stump   C = llvm::ConstantStruct::get(Elts);
7085284bacSMike Stump 
7185284bacSMike Stump   C = new llvm::GlobalVariable(C->getType(), true,
7285284bacSMike Stump                                llvm::GlobalValue::InternalLinkage,
73499ae7ecSMike Stump                                C, "__block_descriptor_tmp", &CGM.getModule());
7485284bacSMike Stump   return C;
7585284bacSMike Stump }
7685284bacSMike Stump 
77971f9b66SMike Stump llvm::Constant *CodeGenModule::getNSConcreteGlobalBlock() {
78971f9b66SMike Stump   if (NSConcreteGlobalBlock)
79971f9b66SMike Stump     return NSConcreteGlobalBlock;
80971f9b66SMike Stump 
81971f9b66SMike Stump   const llvm::PointerType *PtrToInt8Ty
82971f9b66SMike Stump     = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
8352d9c49dSMike Stump   // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
84971f9b66SMike Stump   // same thing as CreateRuntimeFunction if there's already a variable with
85971f9b66SMike Stump   // the same name.
86971f9b66SMike Stump   NSConcreteGlobalBlock
87971f9b66SMike Stump     = new llvm::GlobalVariable(PtrToInt8Ty, false,
88971f9b66SMike Stump                                llvm::GlobalValue::ExternalLinkage,
89971f9b66SMike Stump                                0, "_NSConcreteGlobalBlock",
90971f9b66SMike Stump                                &getModule());
91971f9b66SMike Stump 
92971f9b66SMike Stump   return NSConcreteGlobalBlock;
93971f9b66SMike Stump }
94971f9b66SMike Stump 
957ab278dbSMike Stump llvm::Constant *CodeGenModule::getNSConcreteStackBlock() {
967ab278dbSMike Stump   if (NSConcreteStackBlock)
977ab278dbSMike Stump     return NSConcreteStackBlock;
987ab278dbSMike Stump 
997ab278dbSMike Stump   const llvm::PointerType *PtrToInt8Ty
1007ab278dbSMike Stump     = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
10152d9c49dSMike Stump   // FIXME: We should have a CodeGenModule::AddRuntimeVariable that does the
1027ab278dbSMike Stump   // same thing as CreateRuntimeFunction if there's already a variable with
1037ab278dbSMike Stump   // the same name.
1047ab278dbSMike Stump   NSConcreteStackBlock
1057ab278dbSMike Stump     = new llvm::GlobalVariable(PtrToInt8Ty, false,
1067ab278dbSMike Stump                                llvm::GlobalValue::ExternalLinkage,
1077ab278dbSMike Stump                                0, "_NSConcreteStackBlock",
1087ab278dbSMike Stump                                &getModule());
1097ab278dbSMike Stump 
1107ab278dbSMike Stump   return NSConcreteStackBlock;
1117ab278dbSMike Stump }
1127ab278dbSMike Stump 
113ed5e69feSAnders Carlsson static void CollectBlockDeclRefInfo(const Stmt *S,
114ed5e69feSAnders Carlsson                                     CodeGenFunction::BlockInfo &Info) {
115ed5e69feSAnders Carlsson   for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
116ed5e69feSAnders Carlsson        I != E; ++I)
117dd942713SDaniel Dunbar     if (*I)
118ed5e69feSAnders Carlsson       CollectBlockDeclRefInfo(*I, Info);
119ed5e69feSAnders Carlsson 
120ed5e69feSAnders Carlsson   if (const BlockDeclRefExpr *DE = dyn_cast<BlockDeclRefExpr>(S)) {
121ed5e69feSAnders Carlsson     // FIXME: Handle enums.
122ed5e69feSAnders Carlsson     if (isa<FunctionDecl>(DE->getDecl()))
123ed5e69feSAnders Carlsson       return;
124ed5e69feSAnders Carlsson 
125ed5e69feSAnders Carlsson     if (DE->isByRef())
126ed5e69feSAnders Carlsson       Info.ByRefDeclRefs.push_back(DE);
127ed5e69feSAnders Carlsson     else
128ed5e69feSAnders Carlsson       Info.ByCopyDeclRefs.push_back(DE);
129ed5e69feSAnders Carlsson   }
130ed5e69feSAnders Carlsson }
131ed5e69feSAnders Carlsson 
132ed5e69feSAnders Carlsson /// CanBlockBeGlobal - Given a BlockInfo struct, determines if a block
133ed5e69feSAnders Carlsson /// can be declared as a global variable instead of on the stack.
134ed5e69feSAnders Carlsson static bool CanBlockBeGlobal(const CodeGenFunction::BlockInfo &Info)
135ed5e69feSAnders Carlsson {
136ed5e69feSAnders Carlsson   return Info.ByRefDeclRefs.empty() && Info.ByCopyDeclRefs.empty();
137ed5e69feSAnders Carlsson }
138ed5e69feSAnders Carlsson 
1395d2534adSMike Stump // FIXME: Push most into CGM, passing down a few bits, like current
1405d2534adSMike Stump // function name.
141b750d928SMike Stump llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
142ed5e69feSAnders Carlsson 
143ed5e69feSAnders Carlsson   std::string Name = CurFn->getName();
144ed5e69feSAnders Carlsson   CodeGenFunction::BlockInfo Info(0, Name.c_str());
145ed5e69feSAnders Carlsson   CollectBlockDeclRefInfo(BE->getBody(), Info);
146ed5e69feSAnders Carlsson 
147ed5e69feSAnders Carlsson   // Check if the block can be global.
14897d01d50SMike Stump   // FIXME: This test doesn't work for nested blocks yet.  Longer
14997d01d50SMike Stump   // term, I'd like to just have one code path.  We should move
15097d01d50SMike Stump   // this function into CGM and pass CGF, then we can just check to
15197d01d50SMike Stump   // see if CGF is 0.
15297d01d50SMike Stump   if (0 && CanBlockBeGlobal(Info))
153ed5e69feSAnders Carlsson     return CGM.GetAddrOfGlobalBlock(BE, Name.c_str());
15485284bacSMike Stump 
15585284bacSMike Stump   std::vector<llvm::Constant*> Elts;
15685284bacSMike Stump   llvm::Constant *C;
157b750d928SMike Stump   llvm::Value *V;
15885284bacSMike Stump 
15985284bacSMike Stump   {
16085284bacSMike Stump     // C = BuildBlockStructInitlist();
16185284bacSMike Stump     unsigned int flags = BLOCK_HAS_DESCRIPTOR;
16285284bacSMike Stump 
16385284bacSMike Stump     if (BlockHasCopyDispose)
16485284bacSMike Stump       flags |= BLOCK_HAS_COPY_DISPOSE;
16585284bacSMike Stump 
166499ae7ecSMike Stump     // __isa
1677ab278dbSMike Stump     C = CGM.getNSConcreteStackBlock();
1687ab278dbSMike Stump     const llvm::PointerType *PtrToInt8Ty
1697ab278dbSMike Stump       = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
17085284bacSMike Stump     C = llvm::ConstantExpr::getBitCast(C, PtrToInt8Ty);
17185284bacSMike Stump     Elts.push_back(C);
17285284bacSMike Stump 
17385284bacSMike Stump     // __flags
17485284bacSMike Stump     const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
17585284bacSMike Stump       CGM.getTypes().ConvertType(CGM.getContext().IntTy));
17685284bacSMike Stump     C = llvm::ConstantInt::get(IntTy, flags);
17785284bacSMike Stump     Elts.push_back(C);
17885284bacSMike Stump 
17985284bacSMike Stump     // __reserved
18085284bacSMike Stump     C = llvm::ConstantInt::get(IntTy, 0);
18185284bacSMike Stump     Elts.push_back(C);
18285284bacSMike Stump 
1835d2534adSMike Stump     // __invoke
184b750d928SMike Stump     uint64_t subBlockSize, subBlockAlign;
1851db7d04bSMike Stump     llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
186cb2fbcb0SMike Stump     llvm::Function *Fn
187b750d928SMike Stump       = CodeGenFunction(CGM).GenerateBlockFunction(BE, Info, subBlockSize,
188b750d928SMike Stump                                                    subBlockAlign, subBlockDeclRefDecls);
1892d5a2878SMike Stump     Elts.push_back(Fn);
19085284bacSMike Stump 
19185284bacSMike Stump     // __descriptor
192cb2fbcb0SMike Stump     Elts.push_back(BuildDescriptorBlockDecl(subBlockSize));
19385284bacSMike Stump 
194b750d928SMike Stump     if (subBlockDeclRefDecls.size() == 0) {
1953a139f37SMike Stump       // Optimize to being a global block.
1963a139f37SMike Stump       Elts[0] = CGM.getNSConcreteGlobalBlock();
1973a139f37SMike Stump       Elts[1] = llvm::ConstantInt::get(IntTy, flags|BLOCK_IS_GLOBAL);
1983a139f37SMike Stump 
19985284bacSMike Stump       C = llvm::ConstantStruct::get(Elts);
20085284bacSMike Stump 
20185284bacSMike Stump       char Name[32];
2029319db8aSMike Stump       sprintf(Name, "__block_holder_tmp_%d", CGM.getGlobalUniqueCount());
20385284bacSMike Stump       C = new llvm::GlobalVariable(C->getType(), true,
20485284bacSMike Stump                                    llvm::GlobalValue::InternalLinkage,
20585284bacSMike Stump                                    C, Name, &CGM.getModule());
2065d2534adSMike Stump       QualType BPT = BE->getType();
2075d2534adSMike Stump       C = llvm::ConstantExpr::getBitCast(C, ConvertType(BPT));
20885284bacSMike Stump       return C;
20985284bacSMike Stump     }
21085284bacSMike Stump 
211b750d928SMike Stump     std::vector<const llvm::Type *> Types(5+subBlockDeclRefDecls.size());
212b750d928SMike Stump     for (int i=0; i<5; ++i)
213b750d928SMike Stump       Types[i] = Elts[i]->getType();
21485284bacSMike Stump 
2151db7d04bSMike Stump     for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i) {
2161db7d04bSMike Stump       const Expr *E = subBlockDeclRefDecls[i];
2171db7d04bSMike Stump       const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
2181db7d04bSMike Stump       QualType Ty = E->getType();
21997d01d50SMike Stump       if (BDRE && BDRE->isByRef()) {
22097d01d50SMike Stump         uint64_t Align = getContext().getDeclAlignInBytes(BDRE->getDecl());
22197d01d50SMike Stump         Types[i+5] = llvm::PointerType::get(BuildByRefType(Ty, Align), 0);
22297d01d50SMike Stump       } else
2231db7d04bSMike Stump         Types[i+5] = ConvertType(Ty);
2241db7d04bSMike Stump     }
225b750d928SMike Stump 
226b750d928SMike Stump     llvm::Type *Ty = llvm::StructType::get(Types, true);
227b750d928SMike Stump 
228b750d928SMike Stump     llvm::AllocaInst *A = CreateTempAlloca(Ty);
229b750d928SMike Stump     A->setAlignment(subBlockAlign);
230b750d928SMike Stump     V = A;
231b750d928SMike Stump 
232b750d928SMike Stump     for (unsigned i=0; i<5; ++i)
233b750d928SMike Stump       Builder.CreateStore(Elts[i], Builder.CreateStructGEP(V, i, "block.tmp"));
234b750d928SMike Stump 
235b750d928SMike Stump     for (unsigned i=0; i < subBlockDeclRefDecls.size(); ++i)
236b750d928SMike Stump       {
2371db7d04bSMike Stump         // FIXME: Push const down.
2381db7d04bSMike Stump         Expr *E = const_cast<Expr*>(subBlockDeclRefDecls[i]);
2391db7d04bSMike Stump         DeclRefExpr *DR;
2401db7d04bSMike Stump         ValueDecl *VD;
241b750d928SMike Stump 
2421db7d04bSMike Stump         DR = dyn_cast<DeclRefExpr>(E);
2431db7d04bSMike Stump         // Skip padding.
2441db7d04bSMike Stump         if (DR) continue;
2451db7d04bSMike Stump 
2461db7d04bSMike Stump         BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
2471db7d04bSMike Stump         VD = BDRE->getDecl();
2481db7d04bSMike Stump 
24997d01d50SMike Stump         llvm::Value* Addr = Builder.CreateStructGEP(V, i+5, "tmp");
2501db7d04bSMike Stump         // FIXME: I want a better way to do this.
2511db7d04bSMike Stump         if (LocalDeclMap[VD]) {
25297d01d50SMike Stump           if (BDRE->isByRef()) {
25397d01d50SMike Stump             const llvm::Type *Ty = Types[i+5];
25497d01d50SMike Stump             llvm::Value *Loc = LocalDeclMap[VD];
25597d01d50SMike Stump             Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
25697d01d50SMike Stump             Loc = Builder.CreateLoad(Loc, false);
25797d01d50SMike Stump             Loc = Builder.CreateBitCast(Loc, Ty);
25897d01d50SMike Stump             Builder.CreateStore(Loc, Addr);
25997d01d50SMike Stump             continue;
26097d01d50SMike Stump           } else
2611db7d04bSMike Stump             E = new (getContext()) DeclRefExpr (cast<NamedDecl>(VD),
2621db7d04bSMike Stump                                                 VD->getType(), SourceLocation(),
2631db7d04bSMike Stump                                                 false, false);
2641db7d04bSMike Stump         }
26597d01d50SMike Stump         if (BDRE->isByRef()) {
26697d01d50SMike Stump           // FIXME: __block in nested literals
2671db7d04bSMike Stump           E = new (getContext())
2681db7d04bSMike Stump             UnaryOperator(E, UnaryOperator::AddrOf,
2691db7d04bSMike Stump                           getContext().getPointerType(E->getType()),
2701db7d04bSMike Stump                           SourceLocation());
27197d01d50SMike Stump         }
2721db7d04bSMike Stump 
2731db7d04bSMike Stump         RValue r = EmitAnyExpr(E, Addr, false);
27497d01d50SMike Stump         if (r.isScalar()) {
27597d01d50SMike Stump           llvm::Value *Loc = r.getScalarVal();
27697d01d50SMike Stump           const llvm::Type *Ty = Types[i+5];
27797d01d50SMike Stump           if  (BDRE->isByRef()) {
27897d01d50SMike Stump             Loc = Builder.CreateBitCast(Loc, Ty);
27997d01d50SMike Stump             Loc = Builder.CreateStructGEP(Loc, 1, "forwarding");
28097d01d50SMike Stump             Loc = Builder.CreateBitCast(Loc, Ty);
28197d01d50SMike Stump           }
28297d01d50SMike Stump           Builder.CreateStore(Loc, Addr);
28397d01d50SMike Stump         } else if (r.isComplex())
284b750d928SMike Stump           // FIXME: implement
285b750d928SMike Stump           ErrorUnsupported(BE, "complex in block literal");
286b750d928SMike Stump         else if (r.isAggregate())
287b750d928SMike Stump           ; // Already created into the destination
288b750d928SMike Stump         else
289b750d928SMike Stump           assert (0 && "bad block variable");
290b750d928SMike Stump         // FIXME: Ensure that the offset created by the backend for
291b750d928SMike Stump         // the struct matches the previously computed offset in BlockDecls.
292b750d928SMike Stump       }
293b750d928SMike Stump   }
294b750d928SMike Stump 
295b750d928SMike Stump   QualType BPT = BE->getType();
296b750d928SMike Stump   return Builder.CreateBitCast(V, ConvertType(BPT));
297b750d928SMike Stump }
29885284bacSMike Stump 
29985284bacSMike Stump 
300650c932dSMike Stump const llvm::Type *CodeGenModule::getBlockDescriptorType() {
301650c932dSMike Stump   if (BlockDescriptorType)
302650c932dSMike Stump     return BlockDescriptorType;
3032437cbfaSAnders Carlsson 
3042437cbfaSAnders Carlsson   const llvm::Type *UnsignedLongTy =
305650c932dSMike Stump     getTypes().ConvertType(getContext().UnsignedLongTy);
3062437cbfaSAnders Carlsson 
3072437cbfaSAnders Carlsson   // struct __block_descriptor {
3082437cbfaSAnders Carlsson   //   unsigned long reserved;
3092437cbfaSAnders Carlsson   //   unsigned long block_size;
3102437cbfaSAnders Carlsson   // };
311650c932dSMike Stump   BlockDescriptorType = llvm::StructType::get(UnsignedLongTy,
3122437cbfaSAnders Carlsson                                               UnsignedLongTy,
3132437cbfaSAnders Carlsson                                               NULL);
3142437cbfaSAnders Carlsson 
315650c932dSMike Stump   getModule().addTypeName("struct.__block_descriptor",
316650c932dSMike Stump                           BlockDescriptorType);
3172437cbfaSAnders Carlsson 
318650c932dSMike Stump   return BlockDescriptorType;
3192437cbfaSAnders Carlsson }
3202437cbfaSAnders Carlsson 
321005c9a62SMike Stump const llvm::Type *
322005c9a62SMike Stump CodeGenModule::getGenericBlockLiteralType() {
323005c9a62SMike Stump   if (GenericBlockLiteralType)
324005c9a62SMike Stump     return GenericBlockLiteralType;
3252437cbfaSAnders Carlsson 
3262437cbfaSAnders Carlsson   const llvm::Type *Int8PtrTy =
3272437cbfaSAnders Carlsson     llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3282437cbfaSAnders Carlsson 
3292437cbfaSAnders Carlsson   const llvm::Type *BlockDescPtrTy =
330005c9a62SMike Stump     llvm::PointerType::getUnqual(getBlockDescriptorType());
3312437cbfaSAnders Carlsson 
33292bbd6d4SMike Stump   const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
33392bbd6d4SMike Stump     getTypes().ConvertType(getContext().IntTy));
33492bbd6d4SMike Stump 
3352437cbfaSAnders Carlsson   // struct __block_literal_generic {
3365d2534adSMike Stump   //   void *__isa;
3375d2534adSMike Stump   //   int __flags;
3385d2534adSMike Stump   //   int __reserved;
3395d2534adSMike Stump   //   void (*__invoke)(void *);
3405d2534adSMike Stump   //   struct __block_descriptor *__descriptor;
3412437cbfaSAnders Carlsson   // };
342005c9a62SMike Stump   GenericBlockLiteralType = llvm::StructType::get(Int8PtrTy,
34392bbd6d4SMike Stump                                                   IntTy,
34492bbd6d4SMike Stump                                                   IntTy,
3452437cbfaSAnders Carlsson                                                   Int8PtrTy,
3462437cbfaSAnders Carlsson                                                   BlockDescPtrTy,
3472437cbfaSAnders Carlsson                                                   NULL);
3482437cbfaSAnders Carlsson 
349005c9a62SMike Stump   getModule().addTypeName("struct.__block_literal_generic",
350005c9a62SMike Stump                           GenericBlockLiteralType);
3512437cbfaSAnders Carlsson 
352005c9a62SMike Stump   return GenericBlockLiteralType;
3532437cbfaSAnders Carlsson }
3542437cbfaSAnders Carlsson 
3555d2534adSMike Stump const llvm::Type *
3565d2534adSMike Stump CodeGenModule::getGenericExtendedBlockLiteralType() {
3575d2534adSMike Stump   if (GenericExtendedBlockLiteralType)
3585d2534adSMike Stump     return GenericExtendedBlockLiteralType;
3595d2534adSMike Stump 
3605d2534adSMike Stump   const llvm::Type *Int8PtrTy =
3615d2534adSMike Stump     llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3625d2534adSMike Stump 
3635d2534adSMike Stump   const llvm::Type *BlockDescPtrTy =
3645d2534adSMike Stump     llvm::PointerType::getUnqual(getBlockDescriptorType());
3655d2534adSMike Stump 
3665d2534adSMike Stump   const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
3675d2534adSMike Stump     getTypes().ConvertType(getContext().IntTy));
3685d2534adSMike Stump 
3695d2534adSMike Stump   // struct __block_literal_generic {
3705d2534adSMike Stump   //   void *__isa;
3715d2534adSMike Stump   //   int __flags;
3725d2534adSMike Stump   //   int __reserved;
3735d2534adSMike Stump   //   void (*__invoke)(void *);
3745d2534adSMike Stump   //   struct __block_descriptor *__descriptor;
3755d2534adSMike Stump   //   void *__copy_func_helper_decl;
3765d2534adSMike Stump   //   void *__destroy_func_decl;
3775d2534adSMike Stump   // };
3785d2534adSMike Stump   GenericExtendedBlockLiteralType = llvm::StructType::get(Int8PtrTy,
3795d2534adSMike Stump                                                           IntTy,
3805d2534adSMike Stump                                                           IntTy,
3815d2534adSMike Stump                                                           Int8PtrTy,
3825d2534adSMike Stump                                                           BlockDescPtrTy,
3835d2534adSMike Stump                                                           Int8PtrTy,
3845d2534adSMike Stump                                                           Int8PtrTy,
3855d2534adSMike Stump                                                           NULL);
3865d2534adSMike Stump 
3875d2534adSMike Stump   getModule().addTypeName("struct.__block_literal_extended_generic",
3885d2534adSMike Stump                           GenericExtendedBlockLiteralType);
3895d2534adSMike Stump 
3905d2534adSMike Stump   return GenericExtendedBlockLiteralType;
3915d2534adSMike Stump }
3925d2534adSMike Stump 
3932437cbfaSAnders Carlsson /// getBlockFunctionType - Given a BlockPointerType, will return the
3942437cbfaSAnders Carlsson /// function type for the block, including the first block literal argument.
3952437cbfaSAnders Carlsson static QualType getBlockFunctionType(ASTContext &Ctx,
3966a60fa24SAnders Carlsson                                      const BlockPointerType *BPT) {
397deaad8ccSDouglas Gregor   const FunctionProtoType *FTy = cast<FunctionProtoType>(BPT->getPointeeType());
3982437cbfaSAnders Carlsson 
3992437cbfaSAnders Carlsson   llvm::SmallVector<QualType, 8> Types;
4002437cbfaSAnders Carlsson   Types.push_back(Ctx.getPointerType(Ctx.VoidTy));
4012437cbfaSAnders Carlsson 
402deaad8ccSDouglas Gregor   for (FunctionProtoType::arg_type_iterator i = FTy->arg_type_begin(),
4032437cbfaSAnders Carlsson        e = FTy->arg_type_end(); i != e; ++i)
4042437cbfaSAnders Carlsson     Types.push_back(*i);
4052437cbfaSAnders Carlsson 
4062437cbfaSAnders Carlsson   return Ctx.getFunctionType(FTy->getResultType(),
4072437cbfaSAnders Carlsson                              &Types[0], Types.size(),
4082437cbfaSAnders Carlsson                              FTy->isVariadic(), 0);
4092437cbfaSAnders Carlsson }
4102437cbfaSAnders Carlsson 
4116a60fa24SAnders Carlsson RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr* E) {
4122437cbfaSAnders Carlsson   const BlockPointerType *BPT =
4132437cbfaSAnders Carlsson     E->getCallee()->getType()->getAsBlockPointerType();
4142437cbfaSAnders Carlsson 
4152437cbfaSAnders Carlsson   llvm::Value *Callee = EmitScalarExpr(E->getCallee());
4162437cbfaSAnders Carlsson 
4172437cbfaSAnders Carlsson   // Get a pointer to the generic block literal.
4182437cbfaSAnders Carlsson   const llvm::Type *BlockLiteralTy =
419005c9a62SMike Stump     llvm::PointerType::getUnqual(CGM.getGenericBlockLiteralType());
4202437cbfaSAnders Carlsson 
4212437cbfaSAnders Carlsson   // Bitcast the callee to a block literal.
4222437cbfaSAnders Carlsson   llvm::Value *BlockLiteral =
4232437cbfaSAnders Carlsson     Builder.CreateBitCast(Callee, BlockLiteralTy, "block.literal");
4242437cbfaSAnders Carlsson 
4252437cbfaSAnders Carlsson   // Get the function pointer from the literal.
4262437cbfaSAnders Carlsson   llvm::Value *FuncPtr = Builder.CreateStructGEP(BlockLiteral, 3, "tmp");
427624497c2SMike Stump   llvm::Value *Func = Builder.CreateLoad(FuncPtr, false, "tmp");
4282437cbfaSAnders Carlsson 
4292437cbfaSAnders Carlsson   // Cast the function pointer to the right type.
4302437cbfaSAnders Carlsson   const llvm::Type *BlockFTy =
4312437cbfaSAnders Carlsson     ConvertType(getBlockFunctionType(getContext(), BPT));
4322437cbfaSAnders Carlsson   const llvm::Type *BlockFTyPtr = llvm::PointerType::getUnqual(BlockFTy);
4332437cbfaSAnders Carlsson   Func = Builder.CreateBitCast(Func, BlockFTyPtr);
4342437cbfaSAnders Carlsson 
4352437cbfaSAnders Carlsson   BlockLiteral =
4362437cbfaSAnders Carlsson     Builder.CreateBitCast(BlockLiteral,
4372437cbfaSAnders Carlsson                           llvm::PointerType::getUnqual(llvm::Type::Int8Ty),
4382437cbfaSAnders Carlsson                           "tmp");
4392437cbfaSAnders Carlsson 
4402437cbfaSAnders Carlsson   // Add the block literal.
4412437cbfaSAnders Carlsson   QualType VoidPtrTy = getContext().getPointerType(getContext().VoidTy);
4422437cbfaSAnders Carlsson   CallArgList Args;
4432437cbfaSAnders Carlsson   Args.push_back(std::make_pair(RValue::get(BlockLiteral), VoidPtrTy));
4442437cbfaSAnders Carlsson 
4452437cbfaSAnders Carlsson   // And the rest of the arguments.
4462437cbfaSAnders Carlsson   for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
4472437cbfaSAnders Carlsson        i != e; ++i)
4482437cbfaSAnders Carlsson     Args.push_back(std::make_pair(EmitAnyExprToTemp(*i),
4492437cbfaSAnders Carlsson                                   i->getType()));
4502437cbfaSAnders Carlsson 
4512437cbfaSAnders Carlsson   // And call the block.
4522437cbfaSAnders Carlsson   return EmitCall(CGM.getTypes().getFunctionInfo(E->getType(), Args),
4532437cbfaSAnders Carlsson                   Func, Args);
4542437cbfaSAnders Carlsson }
4556a60fa24SAnders Carlsson 
45697d01d50SMike Stump llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const BlockDeclRefExpr *E) {
45797d01d50SMike Stump   uint64_t &offset = BlockDecls[E->getDecl()];
45897d01d50SMike Stump 
45997d01d50SMike Stump   const llvm::Type *Ty;
46097d01d50SMike Stump   Ty = CGM.getTypes().ConvertType(E->getDecl()->getType());
46197d01d50SMike Stump 
46297d01d50SMike Stump   // FIXME: add support for copy/dispose helpers.
463*0874160bSMike Stump   if (!Enable__block && E->isByRef())
46497d01d50SMike Stump     ErrorUnsupported(E, "__block variable in block literal");
46597d01d50SMike Stump   else if (E->getType()->isBlockPointerType())
46697d01d50SMike Stump     ErrorUnsupported(E, "block pointer in block literal");
46797d01d50SMike Stump   else if (E->getDecl()->getAttr<ObjCNSObjectAttr>() ||
46897d01d50SMike Stump            getContext().isObjCNSObjectType(E->getType()))
46997d01d50SMike Stump     ErrorUnsupported(E, "__attribute__((NSObject)) variable in block "
47097d01d50SMike Stump                      "literal");
47197d01d50SMike Stump   else if (getContext().isObjCObjectPointerType(E->getType()))
47297d01d50SMike Stump     ErrorUnsupported(E, "Objective-C variable in block literal");
47397d01d50SMike Stump 
47497d01d50SMike Stump   // See if we have already allocated an offset for this variable.
47597d01d50SMike Stump   if (offset == 0) {
47697d01d50SMike Stump     // if not, allocate one now.
47797d01d50SMike Stump     offset = getBlockOffset(E);
47897d01d50SMike Stump   }
47997d01d50SMike Stump 
48097d01d50SMike Stump   llvm::Value *BlockLiteral = LoadBlockStruct();
48197d01d50SMike Stump   llvm::Value *V = Builder.CreateGEP(BlockLiteral,
48297d01d50SMike Stump                                      llvm::ConstantInt::get(llvm::Type::Int64Ty,
48397d01d50SMike Stump                                                             offset),
48497d01d50SMike Stump                                      "tmp");
48597d01d50SMike Stump   if (E->isByRef()) {
48697d01d50SMike Stump     bool needsCopyDispose = BlockRequiresCopying(E->getType());
48797d01d50SMike Stump     uint64_t Align = getContext().getDeclAlignInBytes(E->getDecl());
48897d01d50SMike Stump     const llvm::Type *PtrStructTy
48997d01d50SMike Stump       = llvm::PointerType::get(BuildByRefType(E->getType(), Align), 0);
49097d01d50SMike Stump     Ty = PtrStructTy;
49197d01d50SMike Stump     Ty = llvm::PointerType::get(Ty, 0);
49297d01d50SMike Stump     V = Builder.CreateBitCast(V, Ty);
49397d01d50SMike Stump     V = Builder.CreateLoad(V, false);
49497d01d50SMike Stump     V = Builder.CreateStructGEP(V, 1, "forwarding");
49597d01d50SMike Stump     V = Builder.CreateLoad(V, false);
49697d01d50SMike Stump     V = Builder.CreateBitCast(V, PtrStructTy);
49797d01d50SMike Stump     V = Builder.CreateStructGEP(V, needsCopyDispose*2 + 4, "x");
49897d01d50SMike Stump   } else {
49997d01d50SMike Stump     Ty = llvm::PointerType::get(Ty, 0);
50097d01d50SMike Stump     V = Builder.CreateBitCast(V, Ty);
50197d01d50SMike Stump   }
50297d01d50SMike Stump   return V;
50397d01d50SMike Stump }
50497d01d50SMike Stump 
5052d5a2878SMike Stump llvm::Constant *
5060e7d7b68SMike Stump CodeGenModule::GetAddrOfGlobalBlock(const BlockExpr *BE, const char * n) {
5076a60fa24SAnders Carlsson   // Generate the block descriptor.
5086a60fa24SAnders Carlsson   const llvm::Type *UnsignedLongTy = Types.ConvertType(Context.UnsignedLongTy);
50992bbd6d4SMike Stump   const llvm::IntegerType *IntTy = cast<llvm::IntegerType>(
51092bbd6d4SMike Stump     getTypes().ConvertType(getContext().IntTy));
5116a60fa24SAnders Carlsson 
5126a60fa24SAnders Carlsson   llvm::Constant *DescriptorFields[2];
5136a60fa24SAnders Carlsson 
5146a60fa24SAnders Carlsson   // Reserved
5156a60fa24SAnders Carlsson   DescriptorFields[0] = llvm::Constant::getNullValue(UnsignedLongTy);
5166a60fa24SAnders Carlsson 
5176a60fa24SAnders Carlsson   // Block literal size. For global blocks we just use the size of the generic
5186a60fa24SAnders Carlsson   // block literal struct.
5196a60fa24SAnders Carlsson   uint64_t BlockLiteralSize =
520005c9a62SMike Stump     TheTargetData.getTypeStoreSizeInBits(getGenericBlockLiteralType()) / 8;
5216a60fa24SAnders Carlsson   DescriptorFields[1] = llvm::ConstantInt::get(UnsignedLongTy,BlockLiteralSize);
5226a60fa24SAnders Carlsson 
5236a60fa24SAnders Carlsson   llvm::Constant *DescriptorStruct =
5246a60fa24SAnders Carlsson     llvm::ConstantStruct::get(&DescriptorFields[0], 2);
5256a60fa24SAnders Carlsson 
5266a60fa24SAnders Carlsson   llvm::GlobalVariable *Descriptor =
5276a60fa24SAnders Carlsson     new llvm::GlobalVariable(DescriptorStruct->getType(), true,
5286a60fa24SAnders Carlsson                              llvm::GlobalVariable::InternalLinkage,
5296a60fa24SAnders Carlsson                              DescriptorStruct, "__block_descriptor_global",
5306a60fa24SAnders Carlsson                              &getModule());
5316a60fa24SAnders Carlsson 
5326a60fa24SAnders Carlsson   // Generate the constants for the block literal.
5336a60fa24SAnders Carlsson   llvm::Constant *LiteralFields[5];
5346a60fa24SAnders Carlsson 
5352d5a2878SMike Stump   CodeGenFunction::BlockInfo Info(0, n);
536b750d928SMike Stump   uint64_t subBlockSize, subBlockAlign;
5371db7d04bSMike Stump   llvm::SmallVector<const Expr *, 8> subBlockDeclRefDecls;
538cb2fbcb0SMike Stump   llvm::Function *Fn
539b750d928SMike Stump     = CodeGenFunction(*this).GenerateBlockFunction(BE, Info, subBlockSize,
540b750d928SMike Stump                                                    subBlockAlign,
541b750d928SMike Stump                                                    subBlockDeclRefDecls);
542cb2fbcb0SMike Stump   assert(subBlockSize == BlockLiteralSize
543cb2fbcb0SMike Stump          && "no imports allowed for global block");
5446a60fa24SAnders Carlsson 
5456a60fa24SAnders Carlsson   // isa
546971f9b66SMike Stump   LiteralFields[0] = getNSConcreteGlobalBlock();
5476a60fa24SAnders Carlsson 
5486a60fa24SAnders Carlsson   // Flags
5495878c797SAnders Carlsson   LiteralFields[1] =
5505878c797SAnders Carlsson     llvm::ConstantInt::get(IntTy, BLOCK_IS_GLOBAL | BLOCK_HAS_DESCRIPTOR);
5516a60fa24SAnders Carlsson 
5526a60fa24SAnders Carlsson   // Reserved
55392bbd6d4SMike Stump   LiteralFields[2] = llvm::Constant::getNullValue(IntTy);
5546a60fa24SAnders Carlsson 
5556a60fa24SAnders Carlsson   // Function
5566a60fa24SAnders Carlsson   LiteralFields[3] = Fn;
5576a60fa24SAnders Carlsson 
5586a60fa24SAnders Carlsson   // Descriptor
5596a60fa24SAnders Carlsson   LiteralFields[4] = Descriptor;
5606a60fa24SAnders Carlsson 
5616a60fa24SAnders Carlsson   llvm::Constant *BlockLiteralStruct =
5626a60fa24SAnders Carlsson     llvm::ConstantStruct::get(&LiteralFields[0], 5);
5636a60fa24SAnders Carlsson 
5646a60fa24SAnders Carlsson   llvm::GlobalVariable *BlockLiteral =
5656a60fa24SAnders Carlsson     new llvm::GlobalVariable(BlockLiteralStruct->getType(), true,
5666a60fa24SAnders Carlsson                              llvm::GlobalVariable::InternalLinkage,
5676a60fa24SAnders Carlsson                              BlockLiteralStruct, "__block_literal_global",
5686a60fa24SAnders Carlsson                              &getModule());
5696a60fa24SAnders Carlsson 
5706a60fa24SAnders Carlsson   return BlockLiteral;
5716a60fa24SAnders Carlsson }
5726a60fa24SAnders Carlsson 
573cb2fbcb0SMike Stump llvm::Value *CodeGenFunction::LoadBlockStruct() {
574cb2fbcb0SMike Stump   return Builder.CreateLoad(LocalDeclMap[getBlockStructDecl()], "self");
575cb2fbcb0SMike Stump }
576cb2fbcb0SMike Stump 
5773385fe12SChris Lattner llvm::Function *CodeGenFunction::GenerateBlockFunction(const BlockExpr *BExpr,
578cb2fbcb0SMike Stump                                                        const BlockInfo& Info,
579b750d928SMike Stump                                                        uint64_t &Size,
580b750d928SMike Stump                                                        uint64_t &Align,
5811db7d04bSMike Stump                          llvm::SmallVector<const Expr *, 8> &subBlockDeclRefDecls) {
582deaad8ccSDouglas Gregor   const FunctionProtoType *FTy =
5833385fe12SChris Lattner     cast<FunctionProtoType>(BExpr->getFunctionType());
5846a60fa24SAnders Carlsson 
5856a60fa24SAnders Carlsson   FunctionArgList Args;
5866a60fa24SAnders Carlsson 
5873385fe12SChris Lattner   const BlockDecl *BD = BExpr->getBlockDecl();
5886a60fa24SAnders Carlsson 
5896a60fa24SAnders Carlsson   // FIXME: This leaks
5906a60fa24SAnders Carlsson   ImplicitParamDecl *SelfDecl =
5916a60fa24SAnders Carlsson     ImplicitParamDecl::Create(getContext(), 0,
5926a60fa24SAnders Carlsson                               SourceLocation(), 0,
5936a60fa24SAnders Carlsson                               getContext().getPointerType(getContext().VoidTy));
5946a60fa24SAnders Carlsson 
5956a60fa24SAnders Carlsson   Args.push_back(std::make_pair(SelfDecl, SelfDecl->getType()));
596cb2fbcb0SMike Stump   BlockStructDecl = SelfDecl;
5976a60fa24SAnders Carlsson 
5986a60fa24SAnders Carlsson   for (BlockDecl::param_iterator i = BD->param_begin(),
5996a60fa24SAnders Carlsson        e = BD->param_end(); i != e; ++i)
600eecd39f2SMike Stump     Args.push_back(std::make_pair(*i, (*i)->getType()));
6016a60fa24SAnders Carlsson 
6026a60fa24SAnders Carlsson   const CGFunctionInfo &FI =
6036a60fa24SAnders Carlsson     CGM.getTypes().getFunctionInfo(FTy->getResultType(), Args);
6046a60fa24SAnders Carlsson 
6052d5a2878SMike Stump   std::string Name = std::string("__") + Info.Name + "_block_invoke_";
6066a60fa24SAnders Carlsson   CodeGenTypes &Types = CGM.getTypes();
6076a60fa24SAnders Carlsson   const llvm::FunctionType *LTy = Types.GetFunctionType(FI, FTy->isVariadic());
6086a60fa24SAnders Carlsson 
6096a60fa24SAnders Carlsson   llvm::Function *Fn =
6106a60fa24SAnders Carlsson     llvm::Function::Create(LTy, llvm::GlobalValue::InternalLinkage,
6116a60fa24SAnders Carlsson                            Name,
6126a60fa24SAnders Carlsson                            &CGM.getModule());
6136a60fa24SAnders Carlsson 
6146a60fa24SAnders Carlsson   StartFunction(BD, FTy->getResultType(), Fn, Args,
6153385fe12SChris Lattner                 BExpr->getBody()->getLocEnd());
6163385fe12SChris Lattner   EmitStmt(BExpr->getBody());
6173385fe12SChris Lattner   FinishFunction(cast<CompoundStmt>(BExpr->getBody())->getRBracLoc());
6186a60fa24SAnders Carlsson 
619b750d928SMike Stump   // The runtime needs a minimum alignment of a void *.
620b750d928SMike Stump   uint64_t MinAlign = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
621b750d928SMike Stump   BlockOffset = llvm::RoundUpToAlignment(BlockOffset, MinAlign);
622b750d928SMike Stump 
623cb2fbcb0SMike Stump   Size = BlockOffset;
624b750d928SMike Stump   Align = BlockAlign;
625b750d928SMike Stump   subBlockDeclRefDecls = BlockDeclRefDecls;
626cb2fbcb0SMike Stump 
6276a60fa24SAnders Carlsson   return Fn;
6286a60fa24SAnders Carlsson }
6291db7d04bSMike Stump 
6301db7d04bSMike Stump uint64_t CodeGenFunction::getBlockOffset(const BlockDeclRefExpr *BDRE) {
6311db7d04bSMike Stump   const ValueDecl *D = dyn_cast<ValueDecl>(BDRE->getDecl());
6321db7d04bSMike Stump 
6331db7d04bSMike Stump   uint64_t Size = getContext().getTypeSize(D->getType()) / 8;
6341db7d04bSMike Stump   uint64_t Align = getContext().getDeclAlignInBytes(D);
6351db7d04bSMike Stump 
6361db7d04bSMike Stump   if (BDRE->isByRef()) {
6371db7d04bSMike Stump     Size = getContext().getTypeSize(getContext().VoidPtrTy) / 8;
6381db7d04bSMike Stump     Align = getContext().getTypeAlign(getContext().VoidPtrTy) / 8;
6391db7d04bSMike Stump   }
6401db7d04bSMike Stump 
6411db7d04bSMike Stump   assert ((Align > 0) && "alignment must be 1 byte or more");
6421db7d04bSMike Stump 
6431db7d04bSMike Stump   uint64_t OldOffset = BlockOffset;
6441db7d04bSMike Stump 
6451db7d04bSMike Stump   // Ensure proper alignment, even if it means we have to have a gap
6461db7d04bSMike Stump   BlockOffset = llvm::RoundUpToAlignment(BlockOffset, Align);
6471db7d04bSMike Stump   BlockAlign = std::max(Align, BlockAlign);
6481db7d04bSMike Stump 
6491db7d04bSMike Stump   uint64_t Pad = BlockOffset - OldOffset;
6501db7d04bSMike Stump   if (Pad) {
6511db7d04bSMike Stump     llvm::ArrayType::get(llvm::Type::Int8Ty, Pad);
6521db7d04bSMike Stump     QualType PadTy = getContext().getConstantArrayType(getContext().CharTy,
6531db7d04bSMike Stump                                                        llvm::APInt(32, Pad),
6541db7d04bSMike Stump                                                        ArrayType::Normal, 0);
6551db7d04bSMike Stump     ValueDecl *PadDecl = VarDecl::Create(getContext(), 0, SourceLocation(),
6561db7d04bSMike Stump                                          0, QualType(PadTy), VarDecl::None,
6571db7d04bSMike Stump                                          SourceLocation());
6581db7d04bSMike Stump     Expr *E;
6591db7d04bSMike Stump     E = new (getContext()) DeclRefExpr(PadDecl, PadDecl->getType(),
6601db7d04bSMike Stump                                        SourceLocation(), false, false);
6611db7d04bSMike Stump     BlockDeclRefDecls.push_back(E);
6621db7d04bSMike Stump   }
6631db7d04bSMike Stump   BlockDeclRefDecls.push_back(BDRE);
6641db7d04bSMike Stump 
6651db7d04bSMike Stump   BlockOffset += Size;
6661db7d04bSMike Stump   return BlockOffset-Size;
6671db7d04bSMike Stump }
66897d01d50SMike Stump 
66997d01d50SMike Stump llvm::Value *CodeGenFunction::BuildCopyHelper(int flag) {
67097d01d50SMike Stump   const llvm::PointerType *PtrToInt8Ty
67197d01d50SMike Stump     = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
67297d01d50SMike Stump   // FIXME: implement
67397d01d50SMike Stump   llvm::Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty, 43);
67497d01d50SMike Stump   V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "tmp");
67597d01d50SMike Stump   V = Builder.CreateBitCast(V, PtrToInt8Ty, "tmp");
67697d01d50SMike Stump   return V;
67797d01d50SMike Stump }
67897d01d50SMike Stump 
67997d01d50SMike Stump llvm::Value *CodeGenFunction::BuildDestroyHelper(int flag) {
68097d01d50SMike Stump   const llvm::PointerType *PtrToInt8Ty
68197d01d50SMike Stump     = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
68297d01d50SMike Stump   // FIXME: implement
68397d01d50SMike Stump   llvm::Value *V = llvm::ConstantInt::get(llvm::Type::Int32Ty, 44);
68497d01d50SMike Stump   V = Builder.CreateIntToPtr(V, PtrToInt8Ty, "tmp");
68597d01d50SMike Stump   V = Builder.CreateBitCast(V, PtrToInt8Ty, "tmp");
68697d01d50SMike Stump   return V;
68797d01d50SMike Stump }
688