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