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