Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 1 | //===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit Objective-C code as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 14 | #include "CGObjCRuntime.h" |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 15 | #include "CodeGenFunction.h" |
| 16 | #include "CodeGenModule.h" |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 20 | |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 24 | /// Emits an instance of NSConstantString representing the object. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 25 | llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) { |
| 26 | std::string String(E->getString()->getStrData(), E->getString()->getByteLength()); |
| 27 | llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String); |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 28 | // FIXME: This bitcast should just be made an invariant on the Runtime. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 29 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /// Emit a selector. |
| 33 | llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { |
| 34 | // Untyped selector. |
| 35 | // Note that this implementation allows for non-constant strings to be passed |
| 36 | // as arguments to @selector(). Currently, the only thing preventing this |
| 37 | // behaviour is the type checking in the front end. |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 38 | return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 41 | llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { |
| 42 | // FIXME: This should pass the Decl not the name. |
| 43 | return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol()); |
| 44 | } |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 45 | |
| 46 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 47 | RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) { |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 48 | // Only the lookup mechanism and first two arguments of the method |
| 49 | // implementation vary between runtimes. We can get the receiver and |
| 50 | // arguments in generic code. |
| 51 | |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 52 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 53 | const Expr *ReceiverExpr = E->getReceiver(); |
| 54 | bool isSuperMessage = false; |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 55 | bool isClassMessage = false; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 56 | // Find the receiver |
| 57 | llvm::Value *Receiver; |
| 58 | if (!ReceiverExpr) { |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 59 | const ObjCInterfaceDecl *OID = E->getClassInfo().first; |
| 60 | |
| 61 | // Very special case, super send in class method. The receiver is |
| 62 | // self (the class object) and the send uses super semantics. |
| 63 | if (!OID) { |
| 64 | assert(!strcmp(E->getClassName()->getName(), "super") && |
| 65 | "Unexpected missing class interface in message send."); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 66 | isSuperMessage = true; |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 67 | Receiver = LoadObjCSelf(); |
| 68 | } else { |
| 69 | Receiver = Runtime.GetClass(Builder, OID); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 70 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 71 | |
| 72 | isClassMessage = true; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 73 | } else if (const PredefinedExpr *PDE = |
| 74 | dyn_cast<PredefinedExpr>(E->getReceiver())) { |
| 75 | assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 76 | isSuperMessage = true; |
| 77 | Receiver = LoadObjCSelf(); |
| 78 | } else { |
Daniel Dunbar | 2bedbf8 | 2008-08-12 05:28:47 +0000 | [diff] [blame] | 79 | Receiver = EmitScalarExpr(E->getReceiver()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 82 | CallArgList Args; |
| 83 | for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end(); |
| 84 | i != e; ++i) |
| 85 | EmitCallArg(*i, Args); |
| 86 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 87 | if (isSuperMessage) { |
Chris Lattner | 9384c76 | 2008-06-26 04:42:20 +0000 | [diff] [blame] | 88 | // super is only valid in an Objective-C method |
| 89 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 90 | return Runtime.GenerateMessageSendSuper(*this, E->getType(), |
| 91 | E->getSelector(), |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 92 | OMD->getClassInterface(), |
| 93 | Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 94 | isClassMessage, |
| 95 | Args); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 96 | } |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 97 | return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(), |
| 98 | Receiver, isClassMessage, Args); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 101 | /// StartObjCMethod - Begin emission of an ObjCMethod. This generates |
| 102 | /// the LLVM function and sets the other context used by |
| 103 | /// CodeGenFunction. |
| 104 | |
| 105 | // FIXME: This should really be merged with GenerateCode. |
| 106 | void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) { |
Daniel Dunbar | 7ded7f4 | 2008-08-15 22:20:32 +0000 | [diff] [blame] | 107 | CurFn = CGM.getObjCRuntime().GenerateMethod(OMD); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 108 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
| 109 | |
| 110 | // Create a marker to make it easy to insert allocas into the entryblock |
| 111 | // later. Don't create this with the builder, because we don't want it |
| 112 | // folded. |
| 113 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 114 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 115 | EntryBB); |
| 116 | |
| 117 | FnRetTy = OMD->getResultType(); |
| 118 | CurFuncDecl = OMD; |
| 119 | |
| 120 | Builder.SetInsertPoint(EntryBB); |
| 121 | |
| 122 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 123 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 124 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 125 | // Name the struct return argument. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 126 | if (hasAggregateLLVMType(OMD->getResultType())) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 127 | AI->setName("agg.result"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 128 | ++AI; |
| 129 | } |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 130 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 131 | // Add implicit parameters to the decl map. |
| 132 | EmitParmDecl(*OMD->getSelfDecl(), AI); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 133 | ++AI; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 134 | |
| 135 | EmitParmDecl(*OMD->getCmdDecl(), AI); |
| 136 | ++AI; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 137 | |
| 138 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) { |
| 139 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 140 | EmitParmDecl(*OMD->getParamDecl(i), AI); |
| 141 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 142 | assert(AI == CurFn->arg_end() && "Argument mismatch"); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 143 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 144 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 145 | /// Generate an Objective-C method. An Objective-C method is a C function with |
| 146 | /// its pointer, name, and types registered in the class struture. |
| 147 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
| 148 | StartObjCMethod(OMD); |
| 149 | EmitStmt(OMD->getBody()); |
| 150 | |
| 151 | const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody()); |
| 152 | if (S) { |
| 153 | FinishFunction(S->getRBracLoc()); |
| 154 | } else { |
| 155 | FinishFunction(); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // FIXME: I wasn't sure about the synthesis approach. If we end up |
| 160 | // generating an AST for the whole body we can just fall back to |
| 161 | // having a GenerateFunction which takes the body Stmt. |
| 162 | |
| 163 | /// GenerateObjCGetter - Generate an Objective-C property getter |
| 164 | /// function. The given Decl must be either an ObjCCategoryImplDecl |
| 165 | /// or an ObjCImplementationDecl. |
| 166 | void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) { |
| 167 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 168 | ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); |
| 169 | assert(OMD && "Invalid call to generate getter (empty method)"); |
| 170 | // FIXME: This is rather murky, we create this here since they will |
| 171 | // not have been created by Sema for us. |
| 172 | OMD->createImplicitParams(getContext()); |
| 173 | StartObjCMethod(OMD); |
| 174 | |
| 175 | // FIXME: What about nonatomic? |
| 176 | SourceLocation Loc = PD->getLocation(); |
| 177 | ValueDecl *Self = OMD->getSelfDecl(); |
| 178 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
| 179 | DeclRefExpr Base(Self, Self->getType(), Loc); |
| 180 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, |
| 181 | true, true); |
| 182 | ReturnStmt Return(Loc, &IvarRef); |
| 183 | EmitStmt(&Return); |
| 184 | |
| 185 | FinishFunction(); |
| 186 | } |
| 187 | |
| 188 | /// GenerateObjCSetter - Generate an Objective-C property setter |
| 189 | /// function. The given Decl must be either an ObjCCategoryImplDecl |
| 190 | /// or an ObjCImplementationDecl. |
| 191 | void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) { |
| 192 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 193 | ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); |
| 194 | assert(OMD && "Invalid call to generate setter (empty method)"); |
| 195 | // FIXME: This is rather murky, we create this here since they will |
| 196 | // not have been created by Sema for us. |
| 197 | OMD->createImplicitParams(getContext()); |
| 198 | StartObjCMethod(OMD); |
| 199 | |
| 200 | switch (PD->getSetterKind()) { |
| 201 | case ObjCPropertyDecl::Assign: break; |
| 202 | case ObjCPropertyDecl::Copy: |
| 203 | CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'"); |
| 204 | break; |
| 205 | case ObjCPropertyDecl::Retain: |
| 206 | CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'"); |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | // FIXME: What about nonatomic? |
| 211 | SourceLocation Loc = PD->getLocation(); |
| 212 | ValueDecl *Self = OMD->getSelfDecl(); |
| 213 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
| 214 | DeclRefExpr Base(Self, Self->getType(), Loc); |
| 215 | ParmVarDecl *ArgDecl = OMD->getParamDecl(0); |
| 216 | DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc); |
| 217 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, |
| 218 | true, true); |
| 219 | BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign, |
| 220 | Ivar->getType(), Loc); |
| 221 | EmitStmt(&Assign); |
| 222 | |
| 223 | FinishFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 226 | llvm::Value *CodeGenFunction::LoadObjCSelf(void) { |
| 227 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 228 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 231 | RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) { |
| 232 | // Determine getter selector. |
| 233 | Selector S; |
| 234 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(E->getDecl())) { |
| 235 | S = MD->getSelector(); |
| 236 | } else { |
| 237 | S = cast<ObjCPropertyDecl>(E->getDecl())->getGetterName(); |
| 238 | } |
| 239 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 240 | return CGM.getObjCRuntime(). |
| 241 | GenerateMessageSend(*this, E->getType(), S, |
| 242 | EmitScalarExpr(E->getBase()), |
| 243 | false, CallArgList()); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 246 | void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E, |
| 247 | RValue Src) { |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 248 | Selector S; |
| 249 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(E->getDecl())) { |
| 250 | S = PD->getSetterName(); |
| 251 | } else { |
| 252 | // FIXME: How can we have a method decl here? |
| 253 | ErrorUnsupported(E, "Objective-C property setter call"); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | CallArgList Args; |
| 258 | EmitCallArg(Src, E->getType(), Args); |
| 259 | CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S, |
| 260 | EmitScalarExpr(E->getBase()), |
| 261 | false, Args); |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 264 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S) |
| 265 | { |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 266 | llvm::Value *DeclAddress; |
| 267 | QualType ElementTy; |
| 268 | |
| 269 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { |
| 270 | EmitStmt(SD); |
| 271 | |
| 272 | ElementTy = cast<ValueDecl>(SD->getDecl())->getType(); |
| 273 | DeclAddress = LocalDeclMap[SD->getDecl()]; |
| 274 | } else { |
| 275 | ElementTy = cast<Expr>(S.getElement())->getType(); |
| 276 | DeclAddress = 0; |
| 277 | } |
| 278 | |
| 279 | // Fast enumeration state. |
| 280 | QualType StateTy = getContext().getObjCFastEnumerationStateType(); |
| 281 | llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy), |
| 282 | "state.ptr"); |
| 283 | StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame^] | 284 | EmitMemSetToZero(StatePtr, StateTy); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 285 | |
| 286 | // Number of elements in the items array. |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame^] | 287 | static const unsigned NumItems = 16; |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 288 | |
| 289 | // Get selector |
| 290 | llvm::SmallVector<IdentifierInfo*, 3> II; |
| 291 | II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState")); |
| 292 | II.push_back(&CGM.getContext().Idents.get("objects")); |
| 293 | II.push_back(&CGM.getContext().Idents.get("count")); |
| 294 | Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(), |
| 295 | &II[0]); |
| 296 | |
| 297 | QualType ItemsTy = |
| 298 | getContext().getConstantArrayType(getContext().getObjCIdType(), |
| 299 | llvm::APInt(32, NumItems), |
| 300 | ArrayType::Normal, 0); |
| 301 | llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr"); |
| 302 | |
| 303 | llvm::Value *Collection = EmitScalarExpr(S.getCollection()); |
| 304 | |
| 305 | CallArgList Args; |
| 306 | Args.push_back(std::make_pair(StatePtr, |
| 307 | getContext().getPointerType(StateTy))); |
| 308 | |
| 309 | Args.push_back(std::make_pair(ItemsPtr, |
| 310 | getContext().getPointerType(ItemsTy))); |
| 311 | |
| 312 | const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); |
| 313 | llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); |
| 314 | Args.push_back(std::make_pair(Count, getContext().UnsignedLongTy)); |
| 315 | |
| 316 | RValue CountRV = |
| 317 | CGM.getObjCRuntime().GenerateMessageSend(*this, |
| 318 | getContext().UnsignedLongTy, |
| 319 | FastEnumSel, |
| 320 | Collection, false, Args); |
| 321 | |
| 322 | llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr"); |
| 323 | Builder.CreateStore(CountRV.getScalarVal(), LimitPtr); |
| 324 | |
| 325 | llvm::BasicBlock *NoElements = llvm::BasicBlock::Create("noelements"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame^] | 326 | llvm::BasicBlock *SetStartMutations = |
| 327 | llvm::BasicBlock::Create("setstartmutations"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 328 | |
| 329 | llvm::Value *Limit = Builder.CreateLoad(LimitPtr); |
| 330 | llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy); |
| 331 | |
| 332 | llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame^] | 333 | Builder.CreateCondBr(IsZero, NoElements, SetStartMutations); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 334 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame^] | 335 | EmitBlock(SetStartMutations); |
| 336 | |
| 337 | llvm::Value *StartMutationsPtr = |
| 338 | CreateTempAlloca(UnsignedLongLTy); |
| 339 | |
| 340 | llvm::Value *StateMutationsPtrPtr = |
| 341 | Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); |
| 342 | llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, |
| 343 | "mutationsptr"); |
| 344 | |
| 345 | llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr, |
| 346 | "mutations"); |
| 347 | |
| 348 | Builder.CreateStore(StateMutations, StartMutationsPtr); |
| 349 | |
| 350 | llvm::BasicBlock *LoopStart = llvm::BasicBlock::Create("loopstart"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 351 | EmitBlock(LoopStart); |
| 352 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 353 | llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr"); |
| 354 | Builder.CreateStore(Zero, CounterPtr); |
| 355 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame^] | 356 | llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("loopbody"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 357 | EmitBlock(LoopBody); |
| 358 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame^] | 359 | StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); |
| 360 | StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations"); |
| 361 | |
| 362 | llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr, |
| 363 | "mutations"); |
| 364 | llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations, |
| 365 | StartMutations, |
| 366 | "tobool"); |
| 367 | |
| 368 | |
| 369 | llvm::BasicBlock *WasMutated = llvm::BasicBlock::Create("wasmutated"); |
| 370 | llvm::BasicBlock *WasNotMutated = llvm::BasicBlock::Create("wasnotmutated"); |
| 371 | |
| 372 | Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated); |
| 373 | |
| 374 | EmitBlock(WasMutated); |
| 375 | llvm::Value *V = |
| 376 | Builder.CreateBitCast(Collection, |
| 377 | ConvertType(getContext().getObjCIdType()), |
| 378 | "tmp"); |
| 379 | Builder.CreateCall(CGM.getObjCRuntime().EnumerationMutationFunction(), |
| 380 | V); |
| 381 | |
| 382 | EmitBlock(WasNotMutated); |
| 383 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 384 | llvm::Value *StateItemsPtr = |
| 385 | Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); |
| 386 | |
| 387 | llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter"); |
| 388 | |
| 389 | llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr, |
| 390 | "stateitems"); |
| 391 | |
| 392 | llvm::Value *CurrentItemPtr = |
| 393 | Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr"); |
| 394 | |
| 395 | llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem"); |
| 396 | |
| 397 | // Cast the item to the right type. |
| 398 | CurrentItem = Builder.CreateBitCast(CurrentItem, |
| 399 | ConvertType(ElementTy), "tmp"); |
| 400 | |
| 401 | if (!DeclAddress) { |
| 402 | LValue LV = EmitLValue(cast<Expr>(S.getElement())); |
| 403 | |
| 404 | // Set the value to null. |
| 405 | Builder.CreateStore(CurrentItem, LV.getAddress()); |
| 406 | } else |
| 407 | Builder.CreateStore(CurrentItem, DeclAddress); |
| 408 | |
| 409 | // Increment the counter. |
| 410 | Counter = Builder.CreateAdd(Counter, |
| 411 | llvm::ConstantInt::get(UnsignedLongLTy, 1)); |
| 412 | Builder.CreateStore(Counter, CounterPtr); |
| 413 | |
| 414 | llvm::BasicBlock *LoopEnd = llvm::BasicBlock::Create("loopend"); |
| 415 | llvm::BasicBlock *AfterBody = llvm::BasicBlock::Create("afterbody"); |
| 416 | |
| 417 | BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); |
| 418 | |
| 419 | EmitStmt(S.getBody()); |
| 420 | |
| 421 | BreakContinueStack.pop_back(); |
| 422 | |
| 423 | EmitBlock(AfterBody); |
| 424 | |
| 425 | llvm::BasicBlock *FetchMore = llvm::BasicBlock::Create("fetchmore"); |
| 426 | |
| 427 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame^] | 428 | Builder.CreateCondBr(IsLess, LoopStart, FetchMore); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 429 | |
| 430 | // Fetch more elements. |
| 431 | EmitBlock(FetchMore); |
| 432 | |
| 433 | CountRV = |
| 434 | CGM.getObjCRuntime().GenerateMessageSend(*this, |
| 435 | getContext().UnsignedLongTy, |
| 436 | FastEnumSel, |
| 437 | Collection, false, Args); |
| 438 | Builder.CreateStore(CountRV.getScalarVal(), LimitPtr); |
| 439 | Limit = Builder.CreateLoad(LimitPtr); |
| 440 | |
| 441 | IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero"); |
| 442 | Builder.CreateCondBr(IsZero, NoElements, LoopStart); |
| 443 | |
| 444 | // No more elements. |
| 445 | EmitBlock(NoElements); |
| 446 | |
| 447 | if (!DeclAddress) { |
| 448 | // If the element was not a declaration, set it to be null. |
| 449 | |
| 450 | LValue LV = EmitLValue(cast<Expr>(S.getElement())); |
| 451 | |
| 452 | // Set the value to null. |
| 453 | Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)), |
| 454 | LV.getAddress()); |
| 455 | } |
| 456 | |
| 457 | EmitBlock(LoopEnd); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 460 | CGObjCRuntime::~CGObjCRuntime() {} |