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) |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 86 | Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType())); |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 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); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 109 | |
| 110 | CGM.SetMethodAttributes(OMD, CurFn); |
| 111 | |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 112 | llvm::BasicBlock *EntryBB = llvm::BasicBlock::Create("entry", CurFn); |
| 113 | |
| 114 | // Create a marker to make it easy to insert allocas into the entryblock |
| 115 | // later. Don't create this with the builder, because we don't want it |
| 116 | // folded. |
| 117 | llvm::Value *Undef = llvm::UndefValue::get(llvm::Type::Int32Ty); |
| 118 | AllocaInsertPt = new llvm::BitCastInst(Undef, llvm::Type::Int32Ty, "allocapt", |
| 119 | EntryBB); |
| 120 | |
| 121 | FnRetTy = OMD->getResultType(); |
| 122 | CurFuncDecl = OMD; |
| 123 | |
| 124 | Builder.SetInsertPoint(EntryBB); |
| 125 | |
| 126 | // Emit allocs for param decls. Give the LLVM Argument nodes names. |
| 127 | llvm::Function::arg_iterator AI = CurFn->arg_begin(); |
| 128 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 129 | // Name the struct return argument. |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 130 | if (hasAggregateLLVMType(OMD->getResultType())) { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 131 | AI->setName("agg.result"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 132 | ++AI; |
| 133 | } |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 134 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 135 | // Add implicit parameters to the decl map. |
| 136 | EmitParmDecl(*OMD->getSelfDecl(), AI); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 137 | ++AI; |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 138 | |
| 139 | EmitParmDecl(*OMD->getCmdDecl(), AI); |
| 140 | ++AI; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 141 | |
| 142 | for (unsigned i = 0, e = OMD->getNumParams(); i != e; ++i, ++AI) { |
| 143 | assert(AI != CurFn->arg_end() && "Argument mismatch!"); |
| 144 | EmitParmDecl(*OMD->getParamDecl(i), AI); |
| 145 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 146 | assert(AI == CurFn->arg_end() && "Argument mismatch"); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 147 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 148 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 149 | /// Generate an Objective-C method. An Objective-C method is a C function with |
| 150 | /// its pointer, name, and types registered in the class struture. |
| 151 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
| 152 | StartObjCMethod(OMD); |
| 153 | EmitStmt(OMD->getBody()); |
| 154 | |
| 155 | const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody()); |
| 156 | if (S) { |
| 157 | FinishFunction(S->getRBracLoc()); |
| 158 | } else { |
| 159 | FinishFunction(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // FIXME: I wasn't sure about the synthesis approach. If we end up |
| 164 | // generating an AST for the whole body we can just fall back to |
| 165 | // having a GenerateFunction which takes the body Stmt. |
| 166 | |
| 167 | /// GenerateObjCGetter - Generate an Objective-C property getter |
| 168 | /// function. The given Decl must be either an ObjCCategoryImplDecl |
| 169 | /// or an ObjCImplementationDecl. |
| 170 | void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) { |
| 171 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 172 | ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); |
| 173 | assert(OMD && "Invalid call to generate getter (empty method)"); |
| 174 | // FIXME: This is rather murky, we create this here since they will |
| 175 | // not have been created by Sema for us. |
| 176 | OMD->createImplicitParams(getContext()); |
| 177 | StartObjCMethod(OMD); |
| 178 | |
| 179 | // FIXME: What about nonatomic? |
| 180 | SourceLocation Loc = PD->getLocation(); |
| 181 | ValueDecl *Self = OMD->getSelfDecl(); |
| 182 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
| 183 | DeclRefExpr Base(Self, Self->getType(), Loc); |
| 184 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, |
| 185 | true, true); |
| 186 | ReturnStmt Return(Loc, &IvarRef); |
| 187 | EmitStmt(&Return); |
| 188 | |
| 189 | FinishFunction(); |
| 190 | } |
| 191 | |
| 192 | /// GenerateObjCSetter - Generate an Objective-C property setter |
| 193 | /// function. The given Decl must be either an ObjCCategoryImplDecl |
| 194 | /// or an ObjCImplementationDecl. |
| 195 | void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) { |
| 196 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 197 | ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); |
| 198 | assert(OMD && "Invalid call to generate setter (empty method)"); |
| 199 | // FIXME: This is rather murky, we create this here since they will |
| 200 | // not have been created by Sema for us. |
| 201 | OMD->createImplicitParams(getContext()); |
| 202 | StartObjCMethod(OMD); |
| 203 | |
| 204 | switch (PD->getSetterKind()) { |
| 205 | case ObjCPropertyDecl::Assign: break; |
| 206 | case ObjCPropertyDecl::Copy: |
| 207 | CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'"); |
| 208 | break; |
| 209 | case ObjCPropertyDecl::Retain: |
| 210 | CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'"); |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | // FIXME: What about nonatomic? |
| 215 | SourceLocation Loc = PD->getLocation(); |
| 216 | ValueDecl *Self = OMD->getSelfDecl(); |
| 217 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
| 218 | DeclRefExpr Base(Self, Self->getType(), Loc); |
| 219 | ParmVarDecl *ArgDecl = OMD->getParamDecl(0); |
| 220 | DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc); |
| 221 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, |
| 222 | true, true); |
| 223 | BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign, |
| 224 | Ivar->getType(), Loc); |
| 225 | EmitStmt(&Assign); |
| 226 | |
| 227 | FinishFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 230 | llvm::Value *CodeGenFunction::LoadObjCSelf(void) { |
| 231 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 232 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 235 | RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) { |
| 236 | // Determine getter selector. |
| 237 | Selector S; |
Daniel Dunbar | e66f4e3 | 2008-09-03 00:27:26 +0000 | [diff] [blame] | 238 | if (E->getKind() == ObjCPropertyRefExpr::MethodRef) { |
| 239 | S = E->getGetterMethod()->getSelector(); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 240 | } else { |
Daniel Dunbar | e66f4e3 | 2008-09-03 00:27:26 +0000 | [diff] [blame] | 241 | S = E->getProperty()->getGetterName(); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 244 | return CGM.getObjCRuntime(). |
| 245 | GenerateMessageSend(*this, E->getType(), S, |
| 246 | EmitScalarExpr(E->getBase()), |
| 247 | false, CallArgList()); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 250 | void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E, |
| 251 | RValue Src) { |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 252 | Selector S; |
Daniel Dunbar | e66f4e3 | 2008-09-03 00:27:26 +0000 | [diff] [blame] | 253 | if (E->getKind() == ObjCPropertyRefExpr::MethodRef) { |
| 254 | ObjCMethodDecl *Setter = E->getSetterMethod(); |
| 255 | |
| 256 | if (Setter) { |
| 257 | S = Setter->getSelector(); |
| 258 | } else { |
| 259 | // FIXME: This should be diagnosed by sema. |
| 260 | SourceRange Range = E->getSourceRange(); |
| 261 | CGM.getDiags().Report(getContext().getFullLoc(E->getLocStart()), |
| 262 | diag::err_typecheck_assign_const, 0, 0, |
| 263 | &Range, 1); |
| 264 | return; |
| 265 | } |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 266 | } else { |
Daniel Dunbar | e66f4e3 | 2008-09-03 00:27:26 +0000 | [diff] [blame] | 267 | S = E->getProperty()->getSetterName(); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | CallArgList Args; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 271 | Args.push_back(std::make_pair(Src, E->getType())); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 272 | CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S, |
| 273 | EmitScalarExpr(E->getBase()), |
| 274 | false, Args); |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 277 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S) |
| 278 | { |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 279 | llvm::Value *DeclAddress; |
| 280 | QualType ElementTy; |
| 281 | |
| 282 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { |
| 283 | EmitStmt(SD); |
| 284 | |
| 285 | ElementTy = cast<ValueDecl>(SD->getDecl())->getType(); |
| 286 | DeclAddress = LocalDeclMap[SD->getDecl()]; |
| 287 | } else { |
| 288 | ElementTy = cast<Expr>(S.getElement())->getType(); |
| 289 | DeclAddress = 0; |
| 290 | } |
| 291 | |
| 292 | // Fast enumeration state. |
| 293 | QualType StateTy = getContext().getObjCFastEnumerationStateType(); |
| 294 | llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy), |
| 295 | "state.ptr"); |
| 296 | StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 297 | EmitMemSetToZero(StatePtr, StateTy); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 298 | |
| 299 | // Number of elements in the items array. |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 300 | static const unsigned NumItems = 16; |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 301 | |
| 302 | // Get selector |
| 303 | llvm::SmallVector<IdentifierInfo*, 3> II; |
| 304 | II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState")); |
| 305 | II.push_back(&CGM.getContext().Idents.get("objects")); |
| 306 | II.push_back(&CGM.getContext().Idents.get("count")); |
| 307 | Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(), |
| 308 | &II[0]); |
| 309 | |
| 310 | QualType ItemsTy = |
| 311 | getContext().getConstantArrayType(getContext().getObjCIdType(), |
| 312 | llvm::APInt(32, NumItems), |
| 313 | ArrayType::Normal, 0); |
| 314 | llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr"); |
| 315 | |
| 316 | llvm::Value *Collection = EmitScalarExpr(S.getCollection()); |
| 317 | |
| 318 | CallArgList Args; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 319 | Args.push_back(std::make_pair(RValue::get(StatePtr), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 320 | getContext().getPointerType(StateTy))); |
| 321 | |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 322 | Args.push_back(std::make_pair(RValue::get(ItemsPtr), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 323 | getContext().getPointerType(ItemsTy))); |
| 324 | |
| 325 | const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); |
| 326 | llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 327 | Args.push_back(std::make_pair(RValue::get(Count), |
| 328 | getContext().UnsignedLongTy)); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 329 | |
| 330 | RValue CountRV = |
| 331 | CGM.getObjCRuntime().GenerateMessageSend(*this, |
| 332 | getContext().UnsignedLongTy, |
| 333 | FastEnumSel, |
| 334 | Collection, false, Args); |
| 335 | |
| 336 | llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr"); |
| 337 | Builder.CreateStore(CountRV.getScalarVal(), LimitPtr); |
| 338 | |
| 339 | llvm::BasicBlock *NoElements = llvm::BasicBlock::Create("noelements"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 340 | llvm::BasicBlock *SetStartMutations = |
| 341 | llvm::BasicBlock::Create("setstartmutations"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 342 | |
| 343 | llvm::Value *Limit = Builder.CreateLoad(LimitPtr); |
| 344 | llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy); |
| 345 | |
| 346 | llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 347 | Builder.CreateCondBr(IsZero, NoElements, SetStartMutations); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 348 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 349 | EmitBlock(SetStartMutations); |
| 350 | |
| 351 | llvm::Value *StartMutationsPtr = |
| 352 | CreateTempAlloca(UnsignedLongLTy); |
| 353 | |
| 354 | llvm::Value *StateMutationsPtrPtr = |
| 355 | Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); |
| 356 | llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, |
| 357 | "mutationsptr"); |
| 358 | |
| 359 | llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr, |
| 360 | "mutations"); |
| 361 | |
| 362 | Builder.CreateStore(StateMutations, StartMutationsPtr); |
| 363 | |
| 364 | llvm::BasicBlock *LoopStart = llvm::BasicBlock::Create("loopstart"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 365 | EmitBlock(LoopStart); |
| 366 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 367 | llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr"); |
| 368 | Builder.CreateStore(Zero, CounterPtr); |
| 369 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 370 | llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("loopbody"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 371 | EmitBlock(LoopBody); |
| 372 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 373 | StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); |
| 374 | StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations"); |
| 375 | |
| 376 | llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr, |
| 377 | "mutations"); |
| 378 | llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations, |
| 379 | StartMutations, |
| 380 | "tobool"); |
| 381 | |
| 382 | |
| 383 | llvm::BasicBlock *WasMutated = llvm::BasicBlock::Create("wasmutated"); |
| 384 | llvm::BasicBlock *WasNotMutated = llvm::BasicBlock::Create("wasnotmutated"); |
| 385 | |
| 386 | Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated); |
| 387 | |
| 388 | EmitBlock(WasMutated); |
| 389 | llvm::Value *V = |
| 390 | Builder.CreateBitCast(Collection, |
| 391 | ConvertType(getContext().getObjCIdType()), |
| 392 | "tmp"); |
| 393 | Builder.CreateCall(CGM.getObjCRuntime().EnumerationMutationFunction(), |
| 394 | V); |
| 395 | |
| 396 | EmitBlock(WasNotMutated); |
| 397 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 398 | llvm::Value *StateItemsPtr = |
| 399 | Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); |
| 400 | |
| 401 | llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter"); |
| 402 | |
| 403 | llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr, |
| 404 | "stateitems"); |
| 405 | |
| 406 | llvm::Value *CurrentItemPtr = |
| 407 | Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr"); |
| 408 | |
| 409 | llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem"); |
| 410 | |
| 411 | // Cast the item to the right type. |
| 412 | CurrentItem = Builder.CreateBitCast(CurrentItem, |
| 413 | ConvertType(ElementTy), "tmp"); |
| 414 | |
| 415 | if (!DeclAddress) { |
| 416 | LValue LV = EmitLValue(cast<Expr>(S.getElement())); |
| 417 | |
| 418 | // Set the value to null. |
| 419 | Builder.CreateStore(CurrentItem, LV.getAddress()); |
| 420 | } else |
| 421 | Builder.CreateStore(CurrentItem, DeclAddress); |
| 422 | |
| 423 | // Increment the counter. |
| 424 | Counter = Builder.CreateAdd(Counter, |
| 425 | llvm::ConstantInt::get(UnsignedLongLTy, 1)); |
| 426 | Builder.CreateStore(Counter, CounterPtr); |
| 427 | |
| 428 | llvm::BasicBlock *LoopEnd = llvm::BasicBlock::Create("loopend"); |
| 429 | llvm::BasicBlock *AfterBody = llvm::BasicBlock::Create("afterbody"); |
| 430 | |
| 431 | BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); |
| 432 | |
| 433 | EmitStmt(S.getBody()); |
| 434 | |
| 435 | BreakContinueStack.pop_back(); |
| 436 | |
| 437 | EmitBlock(AfterBody); |
| 438 | |
| 439 | llvm::BasicBlock *FetchMore = llvm::BasicBlock::Create("fetchmore"); |
| 440 | |
| 441 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless"); |
Daniel Dunbar | fe2b2c0 | 2008-09-04 21:54:37 +0000 | [diff] [blame] | 442 | Builder.CreateCondBr(IsLess, LoopBody, FetchMore); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 443 | |
| 444 | // Fetch more elements. |
| 445 | EmitBlock(FetchMore); |
| 446 | |
| 447 | CountRV = |
| 448 | CGM.getObjCRuntime().GenerateMessageSend(*this, |
| 449 | getContext().UnsignedLongTy, |
| 450 | FastEnumSel, |
| 451 | Collection, false, Args); |
| 452 | Builder.CreateStore(CountRV.getScalarVal(), LimitPtr); |
| 453 | Limit = Builder.CreateLoad(LimitPtr); |
| 454 | |
| 455 | IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero"); |
| 456 | Builder.CreateCondBr(IsZero, NoElements, LoopStart); |
| 457 | |
| 458 | // No more elements. |
| 459 | EmitBlock(NoElements); |
| 460 | |
| 461 | if (!DeclAddress) { |
| 462 | // If the element was not a declaration, set it to be null. |
| 463 | |
| 464 | LValue LV = EmitLValue(cast<Expr>(S.getElement())); |
| 465 | |
| 466 | // Set the value to null. |
| 467 | Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)), |
| 468 | LV.getAddress()); |
| 469 | } |
| 470 | |
| 471 | EmitBlock(LoopEnd); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame^] | 474 | void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) |
| 475 | { |
| 476 | CGM.getObjCRuntime().EmitTryStmt(*this, S); |
| 477 | } |
| 478 | |
| 479 | void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) |
| 480 | { |
| 481 | CGM.getObjCRuntime().EmitThrowStmt(*this, S); |
| 482 | } |
| 483 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 484 | CGObjCRuntime::~CGObjCRuntime() {} |