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 | 71fcec9 | 2008-11-25 21:53:21 +0000 | [diff] [blame] | 27 | llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) |
| 28 | { |
| 29 | std::string String(E->getString()->getStrData(), |
| 30 | E->getString()->getByteLength()); |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 31 | llvm::Constant *C = CGM.getObjCRuntime().GenerateConstantString(String); |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 32 | // FIXME: This bitcast should just be made an invariant on the Runtime. |
Daniel Dunbar | bbce49b | 2008-08-12 00:12:39 +0000 | [diff] [blame] | 33 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /// Emit a selector. |
| 37 | llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { |
| 38 | // Untyped selector. |
| 39 | // Note that this implementation allows for non-constant strings to be passed |
| 40 | // as arguments to @selector(). Currently, the only thing preventing this |
| 41 | // behaviour is the type checking in the front end. |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 42 | return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 45 | llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { |
| 46 | // FIXME: This should pass the Decl not the name. |
| 47 | return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol()); |
| 48 | } |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 49 | |
| 50 | |
Daniel Dunbar | 8f2926b | 2008-08-23 03:46:30 +0000 | [diff] [blame] | 51 | RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) { |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 52 | // Only the lookup mechanism and first two arguments of the method |
| 53 | // implementation vary between runtimes. We can get the receiver and |
| 54 | // arguments in generic code. |
| 55 | |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 56 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 57 | const Expr *ReceiverExpr = E->getReceiver(); |
| 58 | bool isSuperMessage = false; |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 59 | bool isClassMessage = false; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 60 | // Find the receiver |
| 61 | llvm::Value *Receiver; |
| 62 | if (!ReceiverExpr) { |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 63 | const ObjCInterfaceDecl *OID = E->getClassInfo().first; |
| 64 | |
| 65 | // Very special case, super send in class method. The receiver is |
| 66 | // self (the class object) and the send uses super semantics. |
| 67 | if (!OID) { |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 68 | assert(E->getClassName()->isStr("super") && |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 69 | "Unexpected missing class interface in message send."); |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 70 | isSuperMessage = true; |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 71 | Receiver = LoadObjCSelf(); |
| 72 | } else { |
| 73 | Receiver = Runtime.GetClass(Builder, OID); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 74 | } |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 75 | |
| 76 | isClassMessage = true; |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 77 | } else if (isa<ObjCSuperExpr>(E->getReceiver())) { |
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); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 92 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 93 | return Runtime.GenerateMessageSendSuper(*this, E->getType(), |
| 94 | E->getSelector(), |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 95 | OMD->getClassInterface(), |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 96 | isCategoryImpl, |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 97 | Receiver, |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 98 | isClassMessage, |
| 99 | Args); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 100 | } |
Daniel Dunbar | 7f8ea5c | 2008-08-30 05:35:15 +0000 | [diff] [blame] | 101 | return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(), |
| 102 | Receiver, isClassMessage, Args); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 105 | /// StartObjCMethod - Begin emission of an ObjCMethod. This generates |
| 106 | /// the LLVM function and sets the other context used by |
| 107 | /// CodeGenFunction. |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 108 | void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, |
| 109 | const ObjCContainerDecl *CD) { |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 110 | FunctionArgList Args; |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 111 | llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 112 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 113 | CGM.SetMethodAttributes(OMD, Fn); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 114 | |
Daniel Dunbar | 7c08651 | 2008-09-09 23:14:03 +0000 | [diff] [blame] | 115 | Args.push_back(std::make_pair(OMD->getSelfDecl(), |
| 116 | OMD->getSelfDecl()->getType())); |
| 117 | Args.push_back(std::make_pair(OMD->getCmdDecl(), |
| 118 | OMD->getCmdDecl()->getType())); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 120 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 121 | E = OMD->param_end(); PI != E; ++PI) |
| 122 | Args.push_back(std::make_pair(*PI, (*PI)->getType())); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 123 | |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 124 | StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 125 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 126 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 127 | /// Generate an Objective-C method. An Objective-C method is a C function with |
| 128 | /// its pointer, name, and types registered in the class struture. |
| 129 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
Devang Patel | 1d6a451 | 2009-02-25 01:09:46 +0000 | [diff] [blame] | 130 | // Check if we should generate debug info for this method. |
| 131 | if (CGM.getDebugInfo() && !OMD->getAttr<NodebugAttr>()) |
| 132 | DebugInfo = CGM.getDebugInfo(); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 133 | StartObjCMethod(OMD, OMD->getClassInterface()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 134 | EmitStmt(OMD->getBody()); |
Daniel Dunbar | 2284ac9 | 2008-10-18 18:22:23 +0000 | [diff] [blame] | 135 | FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // FIXME: I wasn't sure about the synthesis approach. If we end up |
| 139 | // generating an AST for the whole body we can just fall back to |
| 140 | // having a GenerateFunction which takes the body Stmt. |
| 141 | |
| 142 | /// GenerateObjCGetter - Generate an Objective-C property getter |
Steve Naroff | 489034c | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 143 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize |
| 144 | /// is illegal within a category. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 145 | void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, |
| 146 | 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. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 153 | OMD->createImplicitParams(getContext(), IMP->getClassInterface()); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 154 | StartObjCMethod(OMD, IMP->getClassInterface()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 155 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 156 | // Determine if we should use an objc_getProperty call for |
Fariborz Jahanian | 447d7ae | 2008-12-08 23:56:17 +0000 | [diff] [blame] | 157 | // this. Non-atomic properties are directly evaluated. |
| 158 | // atomic 'copy' and 'retain' properties are also directly |
| 159 | // evaluated in gc-only mode. |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 160 | if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly && |
Fariborz Jahanian | 447d7ae | 2008-12-08 23:56:17 +0000 | [diff] [blame] | 161 | !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic) && |
| 162 | (PD->getSetterKind() == ObjCPropertyDecl::Copy || |
| 163 | PD->getSetterKind() == ObjCPropertyDecl::Retain)) { |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 164 | llvm::Value *GetPropertyFn = |
| 165 | CGM.getObjCRuntime().GetPropertyGetFunction(); |
| 166 | |
| 167 | if (!GetPropertyFn) { |
| 168 | CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy"); |
| 169 | FinishFunction(); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). |
| 174 | // FIXME: Can't this be simpler? This might even be worse than the |
| 175 | // corresponding gcc code. |
| 176 | CodeGenTypes &Types = CGM.getTypes(); |
| 177 | ValueDecl *Cmd = OMD->getCmdDecl(); |
| 178 | llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd"); |
| 179 | QualType IdTy = getContext().getObjCIdType(); |
| 180 | llvm::Value *SelfAsId = |
| 181 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 182 | llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 183 | llvm::Value *True = |
Daniel Dunbar | be395f6 | 2009-02-04 00:55:44 +0000 | [diff] [blame] | 184 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 185 | CallArgList Args; |
| 186 | Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy)); |
| 187 | Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType())); |
| 188 | Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy)); |
| 189 | Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy)); |
Daniel Dunbar | e4be5a6 | 2009-02-03 23:43:59 +0000 | [diff] [blame] | 190 | // FIXME: We shouldn't need to get the function info here, the |
| 191 | // runtime already should have computed it to build the function. |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 192 | RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args), |
Daniel Dunbar | 88b5396 | 2009-02-02 22:03:45 +0000 | [diff] [blame] | 193 | GetPropertyFn, Args); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 194 | // We need to fix the type here. Ivars with copy & retain are |
| 195 | // always objects so we don't need to worry about complex or |
| 196 | // aggregates. |
| 197 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), |
| 198 | Types.ConvertType(PD->getType()))); |
| 199 | EmitReturnOfRValue(RV, PD->getType()); |
| 200 | } else { |
Fariborz Jahanian | fd64bb6 | 2008-12-15 20:35:07 +0000 | [diff] [blame] | 201 | FieldDecl *Field = |
| 202 | IMP->getClassInterface()->lookupFieldDeclForIvar(getContext(), Ivar); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 203 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), |
| 204 | LoadObjCSelf(), Ivar, Field, 0); |
Fariborz Jahanian | 6010bca | 2008-11-26 22:36:09 +0000 | [diff] [blame] | 205 | if (hasAggregateLLVMType(Ivar->getType())) { |
| 206 | EmitAggregateCopy(ReturnValue, LV.getAddress(), Ivar->getType()); |
| 207 | } |
Fariborz Jahanian | ed1d29d | 2009-03-03 18:49:40 +0000 | [diff] [blame] | 208 | else { |
| 209 | CodeGenTypes &Types = CGM.getTypes(); |
| 210 | RValue RV = EmitLoadOfLValue(LV, Ivar->getType()); |
| 211 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), |
| 212 | Types.ConvertType(PD->getType()))); |
| 213 | EmitReturnOfRValue(RV, PD->getType()); |
| 214 | } |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 215 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 216 | |
| 217 | FinishFunction(); |
| 218 | } |
| 219 | |
| 220 | /// GenerateObjCSetter - Generate an Objective-C property setter |
Steve Naroff | 489034c | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 221 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize |
| 222 | /// is illegal within a category. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 223 | void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, |
| 224 | const ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 225 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 226 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 227 | ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); |
| 228 | assert(OMD && "Invalid call to generate setter (empty method)"); |
| 229 | // FIXME: This is rather murky, we create this here since they will |
| 230 | // not have been created by Sema for us. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 231 | OMD->createImplicitParams(getContext(), IMP->getClassInterface()); |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 232 | StartObjCMethod(OMD, IMP->getClassInterface()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 233 | |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 234 | bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy; |
| 235 | bool IsAtomic = |
| 236 | !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 237 | |
| 238 | // Determine if we should use an objc_setProperty call for |
| 239 | // this. Properties with 'copy' semantics always use it, as do |
| 240 | // non-atomic properties with 'release' semantics as long as we are |
| 241 | // not in gc-only mode. |
| 242 | if (IsCopy || |
| 243 | (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly && |
| 244 | PD->getSetterKind() == ObjCPropertyDecl::Retain)) { |
| 245 | llvm::Value *SetPropertyFn = |
| 246 | CGM.getObjCRuntime().GetPropertySetFunction(); |
| 247 | |
| 248 | if (!SetPropertyFn) { |
| 249 | CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy"); |
| 250 | FinishFunction(); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | // Emit objc_setProperty((id) self, _cmd, offset, arg, |
| 255 | // <is-atomic>, <is-copy>). |
| 256 | // FIXME: Can't this be simpler? This might even be worse than the |
| 257 | // corresponding gcc code. |
| 258 | CodeGenTypes &Types = CGM.getTypes(); |
| 259 | ValueDecl *Cmd = OMD->getCmdDecl(); |
| 260 | llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd"); |
| 261 | QualType IdTy = getContext().getObjCIdType(); |
| 262 | llvm::Value *SelfAsId = |
| 263 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 264 | llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 265 | llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()]; |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 266 | llvm::Value *ArgAsId = |
| 267 | Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"), |
| 268 | Types.ConvertType(IdTy)); |
| 269 | llvm::Value *True = |
Daniel Dunbar | be395f6 | 2009-02-04 00:55:44 +0000 | [diff] [blame] | 270 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 271 | llvm::Value *False = |
Daniel Dunbar | be395f6 | 2009-02-04 00:55:44 +0000 | [diff] [blame] | 272 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 273 | CallArgList Args; |
| 274 | Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy)); |
| 275 | Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType())); |
| 276 | Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy)); |
| 277 | Args.push_back(std::make_pair(RValue::get(ArgAsId), IdTy)); |
| 278 | Args.push_back(std::make_pair(RValue::get(IsAtomic ? True : False), |
| 279 | getContext().BoolTy)); |
| 280 | Args.push_back(std::make_pair(RValue::get(IsCopy ? True : False), |
| 281 | getContext().BoolTy)); |
Daniel Dunbar | e4be5a6 | 2009-02-03 23:43:59 +0000 | [diff] [blame] | 282 | // FIXME: We shouldn't need to get the function info here, the |
| 283 | // runtime already should have computed it to build the function. |
| 284 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args), |
Daniel Dunbar | 541b63b | 2009-02-02 23:23:47 +0000 | [diff] [blame] | 285 | SetPropertyFn, Args); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 286 | } else { |
| 287 | SourceLocation Loc = PD->getLocation(); |
| 288 | ValueDecl *Self = OMD->getSelfDecl(); |
| 289 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
| 290 | DeclRefExpr Base(Self, Self->getType(), Loc); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 291 | ParmVarDecl *ArgDecl = *OMD->param_begin(); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 292 | DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc); |
Fariborz Jahanian | aaa63a7 | 2008-12-13 22:20:28 +0000 | [diff] [blame] | 293 | ObjCInterfaceDecl *OI = IMP->getClassInterface(); |
Fariborz Jahanian | efc4c4b | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 294 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 295 | true, true); |
Fariborz Jahanian | efc4c4b | 2008-12-18 17:29:46 +0000 | [diff] [blame] | 296 | getContext().setFieldDecl(OI, Ivar, &IvarRef); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 297 | BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign, |
| 298 | Ivar->getType(), Loc); |
| 299 | EmitStmt(&Assign); |
| 300 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 301 | |
| 302 | FinishFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 305 | llvm::Value *CodeGenFunction::LoadObjCSelf() { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 306 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 307 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 310 | QualType CodeGenFunction::TypeOfSelfObject() { |
| 311 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 312 | ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); |
| 313 | const PointerType *PTy = |
| 314 | cast<PointerType>(getContext().getCanonicalType(selfDecl->getType())); |
| 315 | return PTy->getPointeeType(); |
| 316 | } |
| 317 | |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame^] | 318 | RValue CodeGenFunction::EmitObjCSuperPropertyGet(const Expr *Exp, |
| 319 | const Selector &S) { |
| 320 | llvm::Value *Receiver = LoadObjCSelf(); |
| 321 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 322 | bool isClassMessage = OMD->isClassMethod(); |
| 323 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
| 324 | return CGM.getObjCRuntime().GenerateMessageSendSuper(*this, |
| 325 | Exp->getType(), |
| 326 | S, |
| 327 | OMD->getClassInterface(), |
| 328 | isCategoryImpl, |
| 329 | Receiver, |
| 330 | isClassMessage, |
| 331 | CallArgList()); |
| 332 | |
| 333 | } |
| 334 | |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 335 | RValue CodeGenFunction::EmitObjCPropertyGet(const Expr *Exp) { |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 336 | // FIXME: Split it into two separate routines. |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 337 | if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) { |
| 338 | Selector S = E->getProperty()->getGetterName(); |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame^] | 339 | if (isa<ObjCSuperExpr>(E->getBase())) |
| 340 | return EmitObjCSuperPropertyGet(E, S); |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 341 | return CGM.getObjCRuntime(). |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 342 | GenerateMessageSend(*this, Exp->getType(), S, |
| 343 | EmitScalarExpr(E->getBase()), |
| 344 | false, CallArgList()); |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 345 | } |
Daniel Dunbar | f185319 | 2009-01-15 18:32:35 +0000 | [diff] [blame] | 346 | else { |
Daniel Dunbar | f479cea | 2009-01-16 01:50:29 +0000 | [diff] [blame] | 347 | const ObjCKVCRefExpr *KE = cast<ObjCKVCRefExpr>(Exp); |
| 348 | Selector S = KE->getGetterMethod()->getSelector(); |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 349 | llvm::Value *Receiver; |
| 350 | if (KE->getClassProp()) { |
| 351 | const ObjCInterfaceDecl *OID = KE->getClassProp(); |
| 352 | Receiver = CGM.getObjCRuntime().GetClass(Builder, OID); |
| 353 | } |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame^] | 354 | else if (isa<ObjCSuperExpr>(KE->getBase())) |
| 355 | return EmitObjCSuperPropertyGet(KE, S); |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 356 | else |
| 357 | Receiver = EmitScalarExpr(KE->getBase()); |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 358 | return CGM.getObjCRuntime(). |
| 359 | GenerateMessageSend(*this, Exp->getType(), S, |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 360 | Receiver, |
| 361 | KE->getClassProp() != 0, CallArgList()); |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 362 | } |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame^] | 365 | void CodeGenFunction::EmitObjCSuperPropertySet(const Expr *Exp, |
| 366 | const Selector &S, |
| 367 | RValue Src) { |
| 368 | CallArgList Args; |
| 369 | llvm::Value *Receiver = LoadObjCSelf(); |
| 370 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 371 | bool isClassMessage = OMD->isClassMethod(); |
| 372 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
| 373 | Args.push_back(std::make_pair(Src, Exp->getType())); |
| 374 | CGM.getObjCRuntime().GenerateMessageSendSuper(*this, |
| 375 | Exp->getType(), |
| 376 | S, |
| 377 | OMD->getClassInterface(), |
| 378 | isCategoryImpl, |
| 379 | Receiver, |
| 380 | isClassMessage, |
| 381 | Args); |
| 382 | return; |
| 383 | } |
| 384 | |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 385 | void CodeGenFunction::EmitObjCPropertySet(const Expr *Exp, |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 386 | RValue Src) { |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 387 | // FIXME: Split it into two separate routines. |
| 388 | if (const ObjCPropertyRefExpr *E = dyn_cast<ObjCPropertyRefExpr>(Exp)) { |
| 389 | Selector S = E->getProperty()->getSetterName(); |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame^] | 390 | if (isa<ObjCSuperExpr>(E->getBase())) { |
| 391 | EmitObjCSuperPropertySet(E, S, Src); |
| 392 | return; |
| 393 | } |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 394 | CallArgList Args; |
| 395 | Args.push_back(std::make_pair(Src, E->getType())); |
| 396 | CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S, |
| 397 | EmitScalarExpr(E->getBase()), |
| 398 | false, Args); |
| 399 | } |
| 400 | else if (const ObjCKVCRefExpr *E = dyn_cast<ObjCKVCRefExpr>(Exp)) { |
| 401 | Selector S = E->getSetterMethod()->getSelector(); |
| 402 | CallArgList Args; |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 403 | llvm::Value *Receiver; |
| 404 | if (E->getClassProp()) { |
| 405 | const ObjCInterfaceDecl *OID = E->getClassProp(); |
| 406 | Receiver = CGM.getObjCRuntime().GetClass(Builder, OID); |
| 407 | } |
Fariborz Jahanian | 8e5d232 | 2009-03-20 17:22:23 +0000 | [diff] [blame] | 408 | else if (isa<ObjCSuperExpr>(E->getBase())) { |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame^] | 409 | EmitObjCSuperPropertySet(E, S, Src); |
Fariborz Jahanian | 8e5d232 | 2009-03-20 17:22:23 +0000 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | else |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 413 | Receiver = EmitScalarExpr(E->getBase()); |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 414 | Args.push_back(std::make_pair(Src, E->getType())); |
| 415 | CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S, |
Fariborz Jahanian | 3523d4f | 2009-03-10 18:03:11 +0000 | [diff] [blame] | 416 | Receiver, |
| 417 | E->getClassProp() != 0, Args); |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 418 | } |
| 419 | else |
| 420 | assert (0 && "bad expression node in EmitObjCPropertySet"); |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 423 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S) |
| 424 | { |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 425 | llvm::Function *EnumerationMutationFn = |
| 426 | CGM.getObjCRuntime().EnumerationMutationFunction(); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 427 | llvm::Value *DeclAddress; |
| 428 | QualType ElementTy; |
| 429 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 430 | if (!EnumerationMutationFn) { |
| 431 | CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); |
| 432 | return; |
| 433 | } |
| 434 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 435 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { |
| 436 | EmitStmt(SD); |
Daniel Dunbar | a448fb2 | 2008-11-11 23:11:34 +0000 | [diff] [blame] | 437 | assert(HaveInsertPoint() && "DeclStmt destroyed insert point!"); |
Douglas Gregor | 4afa39d | 2009-01-20 01:17:11 +0000 | [diff] [blame] | 438 | const Decl* D = SD->getSolitaryDecl(); |
Ted Kremenek | 39741ce | 2008-10-06 20:59:48 +0000 | [diff] [blame] | 439 | ElementTy = cast<ValueDecl>(D)->getType(); |
| 440 | DeclAddress = LocalDeclMap[D]; |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 441 | } else { |
| 442 | ElementTy = cast<Expr>(S.getElement())->getType(); |
| 443 | DeclAddress = 0; |
| 444 | } |
| 445 | |
| 446 | // Fast enumeration state. |
| 447 | QualType StateTy = getContext().getObjCFastEnumerationStateType(); |
| 448 | llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy), |
| 449 | "state.ptr"); |
| 450 | StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 451 | EmitMemSetToZero(StatePtr, StateTy); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 452 | |
| 453 | // Number of elements in the items array. |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 454 | static const unsigned NumItems = 16; |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 455 | |
| 456 | // Get selector |
| 457 | llvm::SmallVector<IdentifierInfo*, 3> II; |
| 458 | II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState")); |
| 459 | II.push_back(&CGM.getContext().Idents.get("objects")); |
| 460 | II.push_back(&CGM.getContext().Idents.get("count")); |
| 461 | Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(), |
| 462 | &II[0]); |
| 463 | |
| 464 | QualType ItemsTy = |
| 465 | getContext().getConstantArrayType(getContext().getObjCIdType(), |
| 466 | llvm::APInt(32, NumItems), |
| 467 | ArrayType::Normal, 0); |
| 468 | llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr"); |
| 469 | |
| 470 | llvm::Value *Collection = EmitScalarExpr(S.getCollection()); |
| 471 | |
| 472 | CallArgList Args; |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 473 | Args.push_back(std::make_pair(RValue::get(StatePtr), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 474 | getContext().getPointerType(StateTy))); |
| 475 | |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 476 | Args.push_back(std::make_pair(RValue::get(ItemsPtr), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 477 | getContext().getPointerType(ItemsTy))); |
| 478 | |
| 479 | const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); |
| 480 | llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); |
Daniel Dunbar | 46f45b9 | 2008-09-09 01:06:48 +0000 | [diff] [blame] | 481 | Args.push_back(std::make_pair(RValue::get(Count), |
| 482 | getContext().UnsignedLongTy)); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 483 | |
| 484 | RValue CountRV = |
| 485 | CGM.getObjCRuntime().GenerateMessageSend(*this, |
| 486 | getContext().UnsignedLongTy, |
| 487 | FastEnumSel, |
| 488 | Collection, false, Args); |
| 489 | |
| 490 | llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr"); |
| 491 | Builder.CreateStore(CountRV.getScalarVal(), LimitPtr); |
| 492 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 493 | llvm::BasicBlock *NoElements = createBasicBlock("noelements"); |
| 494 | llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 495 | |
| 496 | llvm::Value *Limit = Builder.CreateLoad(LimitPtr); |
| 497 | llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy); |
| 498 | |
| 499 | llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 500 | Builder.CreateCondBr(IsZero, NoElements, SetStartMutations); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 501 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 502 | EmitBlock(SetStartMutations); |
| 503 | |
| 504 | llvm::Value *StartMutationsPtr = |
| 505 | CreateTempAlloca(UnsignedLongLTy); |
| 506 | |
| 507 | llvm::Value *StateMutationsPtrPtr = |
| 508 | Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); |
| 509 | llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, |
| 510 | "mutationsptr"); |
| 511 | |
| 512 | llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr, |
| 513 | "mutations"); |
| 514 | |
| 515 | Builder.CreateStore(StateMutations, StartMutationsPtr); |
| 516 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 517 | llvm::BasicBlock *LoopStart = createBasicBlock("loopstart"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 518 | EmitBlock(LoopStart); |
| 519 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 520 | llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr"); |
| 521 | Builder.CreateStore(Zero, CounterPtr); |
| 522 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 523 | llvm::BasicBlock *LoopBody = createBasicBlock("loopbody"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 524 | EmitBlock(LoopBody); |
| 525 | |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 526 | StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); |
| 527 | StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations"); |
| 528 | |
| 529 | llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr, |
| 530 | "mutations"); |
| 531 | llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations, |
| 532 | StartMutations, |
| 533 | "tobool"); |
| 534 | |
| 535 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 536 | llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated"); |
| 537 | llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 538 | |
| 539 | Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated); |
| 540 | |
| 541 | EmitBlock(WasMutated); |
| 542 | llvm::Value *V = |
| 543 | Builder.CreateBitCast(Collection, |
| 544 | ConvertType(getContext().getObjCIdType()), |
| 545 | "tmp"); |
Daniel Dunbar | 2b2105e | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 546 | CallArgList Args2; |
| 547 | Args2.push_back(std::make_pair(RValue::get(V), |
| 548 | getContext().getObjCIdType())); |
| 549 | // FIXME: We shouldn't need to get the function info here, the |
| 550 | // runtime already should have computed it to build the function. |
Daniel Dunbar | 90350b6 | 2009-02-04 22:00:33 +0000 | [diff] [blame] | 551 | EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2), |
Daniel Dunbar | 2b2105e | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 552 | EnumerationMutationFn, Args2); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 553 | |
| 554 | EmitBlock(WasNotMutated); |
| 555 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 556 | llvm::Value *StateItemsPtr = |
| 557 | Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); |
| 558 | |
| 559 | llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter"); |
| 560 | |
| 561 | llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr, |
| 562 | "stateitems"); |
| 563 | |
| 564 | llvm::Value *CurrentItemPtr = |
| 565 | Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr"); |
| 566 | |
| 567 | llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem"); |
| 568 | |
| 569 | // Cast the item to the right type. |
| 570 | CurrentItem = Builder.CreateBitCast(CurrentItem, |
| 571 | ConvertType(ElementTy), "tmp"); |
| 572 | |
| 573 | if (!DeclAddress) { |
| 574 | LValue LV = EmitLValue(cast<Expr>(S.getElement())); |
| 575 | |
| 576 | // Set the value to null. |
| 577 | Builder.CreateStore(CurrentItem, LV.getAddress()); |
| 578 | } else |
| 579 | Builder.CreateStore(CurrentItem, DeclAddress); |
| 580 | |
| 581 | // Increment the counter. |
| 582 | Counter = Builder.CreateAdd(Counter, |
| 583 | llvm::ConstantInt::get(UnsignedLongLTy, 1)); |
| 584 | Builder.CreateStore(Counter, CounterPtr); |
| 585 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 586 | llvm::BasicBlock *LoopEnd = createBasicBlock("loopend"); |
| 587 | llvm::BasicBlock *AfterBody = createBasicBlock("afterbody"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 588 | |
Anders Carlsson | e4b6d34 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 589 | BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 590 | |
| 591 | EmitStmt(S.getBody()); |
| 592 | |
| 593 | BreakContinueStack.pop_back(); |
| 594 | |
| 595 | EmitBlock(AfterBody); |
| 596 | |
Daniel Dunbar | 55e8742 | 2008-11-11 02:29:29 +0000 | [diff] [blame] | 597 | llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore"); |
Fariborz Jahanian | f0906c4 | 2009-01-06 18:56:31 +0000 | [diff] [blame] | 598 | |
| 599 | Counter = Builder.CreateLoad(CounterPtr); |
| 600 | Limit = Builder.CreateLoad(LimitPtr); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 601 | llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless"); |
Daniel Dunbar | fe2b2c0 | 2008-09-04 21:54:37 +0000 | [diff] [blame] | 602 | Builder.CreateCondBr(IsLess, LoopBody, FetchMore); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 603 | |
| 604 | // Fetch more elements. |
| 605 | EmitBlock(FetchMore); |
| 606 | |
| 607 | CountRV = |
| 608 | CGM.getObjCRuntime().GenerateMessageSend(*this, |
| 609 | getContext().UnsignedLongTy, |
| 610 | FastEnumSel, |
| 611 | Collection, false, Args); |
| 612 | Builder.CreateStore(CountRV.getScalarVal(), LimitPtr); |
| 613 | Limit = Builder.CreateLoad(LimitPtr); |
| 614 | |
| 615 | IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero"); |
| 616 | Builder.CreateCondBr(IsZero, NoElements, LoopStart); |
| 617 | |
| 618 | // No more elements. |
| 619 | EmitBlock(NoElements); |
| 620 | |
| 621 | if (!DeclAddress) { |
| 622 | // If the element was not a declaration, set it to be null. |
| 623 | |
| 624 | LValue LV = EmitLValue(cast<Expr>(S.getElement())); |
| 625 | |
| 626 | // Set the value to null. |
| 627 | Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)), |
| 628 | LV.getAddress()); |
| 629 | } |
| 630 | |
| 631 | EmitBlock(LoopEnd); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 634 | void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) |
| 635 | { |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 636 | CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) |
| 640 | { |
| 641 | CGM.getObjCRuntime().EmitThrowStmt(*this, S); |
| 642 | } |
| 643 | |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 644 | void CodeGenFunction::EmitObjCAtSynchronizedStmt( |
| 645 | const ObjCAtSynchronizedStmt &S) |
| 646 | { |
Fariborz Jahanian | bd71be4 | 2008-11-21 00:49:24 +0000 | [diff] [blame] | 647 | CGM.getObjCRuntime().EmitTryOrSynchronizedStmt(*this, S); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 650 | CGObjCRuntime::~CGObjCRuntime() {} |