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 | |
Devang Patel | bcbd03a | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 14 | #include "CGDebugInfo.h" |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 15 | #include "CGObjCRuntime.h" |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 18 | #include "TargetInfo.h" |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclObjC.h" |
Chris Lattner | 16f0049 | 2009-04-26 01:32:48 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtObjC.h" |
Daniel Dunbar | e66f4e3 | 2008-09-03 00:27:26 +0000 | [diff] [blame] | 22 | #include "clang/Basic/Diagnostic.h" |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/STLExtras.h" |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 25 | #include "llvm/InlineAsm.h" |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 26 | using namespace clang; |
| 27 | using namespace CodeGen; |
| 28 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 29 | typedef llvm::PointerIntPair<llvm::Value*,1,bool> TryEmitResult; |
| 30 | static TryEmitResult |
| 31 | tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e); |
| 32 | |
| 33 | /// Given the address of a variable of pointer type, find the correct |
| 34 | /// null to store into it. |
| 35 | static llvm::Constant *getNullForVariable(llvm::Value *addr) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 36 | llvm::Type *type = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 37 | cast<llvm::PointerType>(addr->getType())->getElementType(); |
| 38 | return llvm::ConstantPointerNull::get(cast<llvm::PointerType>(type)); |
| 39 | } |
| 40 | |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 41 | /// Emits an instance of NSConstantString representing the object. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | llvm::Value *CodeGenFunction::EmitObjCStringLiteral(const ObjCStringLiteral *E) |
Daniel Dunbar | 71fcec9 | 2008-11-25 21:53:21 +0000 | [diff] [blame] | 43 | { |
David Chisnall | 0d13f6f | 2010-01-23 02:40:42 +0000 | [diff] [blame] | 44 | llvm::Constant *C = |
| 45 | CGM.getObjCRuntime().GenerateConstantString(E->getString()); |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 46 | // FIXME: This bitcast should just be made an invariant on the Runtime. |
Owen Anderson | 3c4972d | 2009-07-29 18:54:39 +0000 | [diff] [blame] | 47 | return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType())); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /// Emit a selector. |
| 51 | llvm::Value *CodeGenFunction::EmitObjCSelectorExpr(const ObjCSelectorExpr *E) { |
| 52 | // Untyped selector. |
| 53 | // Note that this implementation allows for non-constant strings to be passed |
| 54 | // as arguments to @selector(). Currently, the only thing preventing this |
| 55 | // behaviour is the type checking in the front end. |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 56 | return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Daniel Dunbar | ed7c618 | 2008-08-20 00:28:19 +0000 | [diff] [blame] | 59 | llvm::Value *CodeGenFunction::EmitObjCProtocolExpr(const ObjCProtocolExpr *E) { |
| 60 | // FIXME: This should pass the Decl not the name. |
| 61 | return CGM.getObjCRuntime().GenerateProtocolRef(Builder, E->getProtocol()); |
| 62 | } |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 63 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 64 | /// \brief Adjust the type of the result of an Objective-C message send |
| 65 | /// expression when the method has a related result type. |
| 66 | static RValue AdjustRelatedResultType(CodeGenFunction &CGF, |
| 67 | const Expr *E, |
| 68 | const ObjCMethodDecl *Method, |
| 69 | RValue Result) { |
| 70 | if (!Method) |
| 71 | return Result; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 72 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 73 | if (!Method->hasRelatedResultType() || |
| 74 | CGF.getContext().hasSameType(E->getType(), Method->getResultType()) || |
| 75 | !Result.isScalar()) |
| 76 | return Result; |
| 77 | |
| 78 | // We have applied a related result type. Cast the rvalue appropriately. |
| 79 | return RValue::get(CGF.Builder.CreateBitCast(Result.getScalarVal(), |
| 80 | CGF.ConvertType(E->getType()))); |
| 81 | } |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 82 | |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 83 | RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E, |
| 84 | ReturnValueSlot Return) { |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 85 | // Only the lookup mechanism and first two arguments of the method |
| 86 | // implementation vary between runtimes. We can get the receiver and |
| 87 | // arguments in generic code. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 88 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 89 | bool isDelegateInit = E->isDelegateInitCall(); |
| 90 | |
| 91 | // We don't retain the receiver in delegate init calls, and this is |
| 92 | // safe because the receiver value is always loaded from 'self', |
| 93 | // which we zero out. We don't want to Block_copy block receivers, |
| 94 | // though. |
| 95 | bool retainSelf = |
| 96 | (!isDelegateInit && |
| 97 | CGM.getLangOptions().ObjCAutoRefCount && |
| 98 | E->getMethodDecl() && |
| 99 | E->getMethodDecl()->hasAttr<NSConsumesSelfAttr>()); |
| 100 | |
Daniel Dunbar | 208ff5e | 2008-08-11 18:12:00 +0000 | [diff] [blame] | 101 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 102 | bool isSuperMessage = false; |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 103 | bool isClassMessage = false; |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 104 | ObjCInterfaceDecl *OID = 0; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 105 | // Find the receiver |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 106 | QualType ReceiverType; |
Daniel Dunbar | 0b647a6 | 2010-04-22 03:17:06 +0000 | [diff] [blame] | 107 | llvm::Value *Receiver = 0; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 108 | switch (E->getReceiverKind()) { |
| 109 | case ObjCMessageExpr::Instance: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 110 | ReceiverType = E->getInstanceReceiver()->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 111 | if (retainSelf) { |
| 112 | TryEmitResult ter = tryEmitARCRetainScalarExpr(*this, |
| 113 | E->getInstanceReceiver()); |
| 114 | Receiver = ter.getPointer(); |
| 115 | if (!ter.getInt()) |
| 116 | Receiver = EmitARCRetainNonBlock(Receiver); |
| 117 | } else |
| 118 | Receiver = EmitScalarExpr(E->getInstanceReceiver()); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 119 | break; |
Daniel Dunbar | ddb2a3d | 2008-08-16 00:25:02 +0000 | [diff] [blame] | 120 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 121 | case ObjCMessageExpr::Class: { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 122 | ReceiverType = E->getClassReceiver(); |
| 123 | const ObjCObjectType *ObjTy = ReceiverType->getAs<ObjCObjectType>(); |
John McCall | 3031c63 | 2010-05-17 20:12:43 +0000 | [diff] [blame] | 124 | assert(ObjTy && "Invalid Objective-C class message send"); |
| 125 | OID = ObjTy->getInterface(); |
| 126 | assert(OID && "Invalid Objective-C class message send"); |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 127 | Receiver = Runtime.GetClass(Builder, OID); |
Daniel Dunbar | f56f191 | 2008-08-25 08:19:24 +0000 | [diff] [blame] | 128 | isClassMessage = true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 129 | |
| 130 | if (retainSelf) |
| 131 | Receiver = EmitARCRetainNonBlock(Receiver); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 132 | break; |
| 133 | } |
| 134 | |
| 135 | case ObjCMessageExpr::SuperInstance: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 136 | ReceiverType = E->getSuperType(); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 137 | Receiver = LoadObjCSelf(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 138 | isSuperMessage = true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 139 | |
| 140 | if (retainSelf) |
| 141 | Receiver = EmitARCRetainNonBlock(Receiver); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 142 | break; |
| 143 | |
| 144 | case ObjCMessageExpr::SuperClass: |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 145 | ReceiverType = E->getSuperType(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 146 | Receiver = LoadObjCSelf(); |
| 147 | isSuperMessage = true; |
| 148 | isClassMessage = true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 149 | |
| 150 | if (retainSelf) |
| 151 | Receiver = EmitARCRetainNonBlock(Receiver); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 152 | break; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 153 | } |
| 154 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 155 | QualType ResultType = |
| 156 | E->getMethodDecl() ? E->getMethodDecl()->getResultType() : E->getType(); |
| 157 | |
Daniel Dunbar | 19cd87e | 2008-08-30 03:02:31 +0000 | [diff] [blame] | 158 | CallArgList Args; |
Anders Carlsson | 131038e | 2009-04-18 20:29:27 +0000 | [diff] [blame] | 159 | EmitCallArgs(Args, E->getMethodDecl(), E->arg_begin(), E->arg_end()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 161 | // For delegate init calls in ARC, do an unsafe store of null into |
| 162 | // self. This represents the call taking direct ownership of that |
| 163 | // value. We have to do this after emitting the other call |
| 164 | // arguments because they might also reference self, but we don't |
| 165 | // have to worry about any of them modifying self because that would |
| 166 | // be an undefined read and write of an object in unordered |
| 167 | // expressions. |
| 168 | if (isDelegateInit) { |
| 169 | assert(getLangOptions().ObjCAutoRefCount && |
| 170 | "delegate init calls should only be marked in ARC"); |
| 171 | |
| 172 | // Do an unsafe store of null into self. |
| 173 | llvm::Value *selfAddr = |
| 174 | LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; |
| 175 | assert(selfAddr && "no self entry for a delegate init call?"); |
| 176 | |
| 177 | Builder.CreateStore(getNullForVariable(selfAddr), selfAddr); |
| 178 | } |
Anders Carlsson | 7e70fb2 | 2010-06-21 20:59:55 +0000 | [diff] [blame] | 179 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 180 | RValue result; |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 181 | if (isSuperMessage) { |
Chris Lattner | 9384c76 | 2008-06-26 04:42:20 +0000 | [diff] [blame] | 182 | // super is only valid in an Objective-C method |
| 183 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
Fariborz Jahanian | 7ce7792 | 2009-02-28 20:07:56 +0000 | [diff] [blame] | 184 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 185 | result = Runtime.GenerateMessageSendSuper(*this, Return, ResultType, |
| 186 | E->getSelector(), |
| 187 | OMD->getClassInterface(), |
| 188 | isCategoryImpl, |
| 189 | Receiver, |
| 190 | isClassMessage, |
| 191 | Args, |
| 192 | E->getMethodDecl()); |
| 193 | } else { |
| 194 | result = Runtime.GenerateMessageSend(*this, Return, ResultType, |
| 195 | E->getSelector(), |
| 196 | Receiver, Args, OID, |
| 197 | E->getMethodDecl()); |
Chris Lattner | 8fdf328 | 2008-06-24 17:04:18 +0000 | [diff] [blame] | 198 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 199 | |
| 200 | // For delegate init calls in ARC, implicitly store the result of |
| 201 | // the call back into self. This takes ownership of the value. |
| 202 | if (isDelegateInit) { |
| 203 | llvm::Value *selfAddr = |
| 204 | LocalDeclMap[cast<ObjCMethodDecl>(CurCodeDecl)->getSelfDecl()]; |
| 205 | llvm::Value *newSelf = result.getScalarVal(); |
| 206 | |
| 207 | // The delegate return type isn't necessarily a matching type; in |
| 208 | // fact, it's quite likely to be 'id'. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 209 | llvm::Type *selfTy = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 210 | cast<llvm::PointerType>(selfAddr->getType())->getElementType(); |
| 211 | newSelf = Builder.CreateBitCast(newSelf, selfTy); |
| 212 | |
| 213 | Builder.CreateStore(newSelf, selfAddr); |
| 214 | } |
| 215 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 216 | return AdjustRelatedResultType(*this, E, E->getMethodDecl(), result); |
Anders Carlsson | 5508518 | 2007-08-21 17:43:55 +0000 | [diff] [blame] | 217 | } |
| 218 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 219 | namespace { |
| 220 | struct FinishARCDealloc : EHScopeStack::Cleanup { |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 221 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 222 | const ObjCMethodDecl *method = cast<ObjCMethodDecl>(CGF.CurCodeDecl); |
John McCall | 799d34e | 2011-07-13 18:26:47 +0000 | [diff] [blame] | 223 | |
| 224 | const ObjCImplDecl *impl = cast<ObjCImplDecl>(method->getDeclContext()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 225 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
| 226 | if (!iface->getSuperClass()) return; |
| 227 | |
John McCall | 799d34e | 2011-07-13 18:26:47 +0000 | [diff] [blame] | 228 | bool isCategory = isa<ObjCCategoryImplDecl>(impl); |
| 229 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 230 | // Call [super dealloc] if we have a superclass. |
| 231 | llvm::Value *self = CGF.LoadObjCSelf(); |
| 232 | |
| 233 | CallArgList args; |
| 234 | CGF.CGM.getObjCRuntime().GenerateMessageSendSuper(CGF, ReturnValueSlot(), |
| 235 | CGF.getContext().VoidTy, |
| 236 | method->getSelector(), |
| 237 | iface, |
John McCall | 799d34e | 2011-07-13 18:26:47 +0000 | [diff] [blame] | 238 | isCategory, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 239 | self, |
| 240 | /*is class msg*/ false, |
| 241 | args, |
| 242 | method); |
| 243 | } |
| 244 | }; |
| 245 | } |
| 246 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 247 | /// StartObjCMethod - Begin emission of an ObjCMethod. This generates |
| 248 | /// the LLVM function and sets the other context used by |
| 249 | /// CodeGenFunction. |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 250 | void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD, |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 251 | const ObjCContainerDecl *CD, |
| 252 | SourceLocation StartLoc) { |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 253 | FunctionArgList args; |
Devang Patel | 4800ea6 | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 254 | // Check if we should generate debug info for this method. |
Devang Patel | aa11289 | 2011-03-07 18:45:56 +0000 | [diff] [blame] | 255 | if (CGM.getModuleDebugInfo() && !OMD->hasAttr<NoDebugAttr>()) |
| 256 | DebugInfo = CGM.getModuleDebugInfo(); |
Devang Patel | 4800ea6 | 2010-04-05 21:09:15 +0000 | [diff] [blame] | 257 | |
Fariborz Jahanian | 679a502 | 2009-01-10 21:06:09 +0000 | [diff] [blame] | 258 | llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD, CD); |
Daniel Dunbar | f80519b | 2008-09-04 23:41:35 +0000 | [diff] [blame] | 259 | |
Daniel Dunbar | 0e4f40e | 2009-04-17 00:48:04 +0000 | [diff] [blame] | 260 | const CGFunctionInfo &FI = CGM.getTypes().getFunctionInfo(OMD); |
| 261 | CGM.SetInternalFunctionAttributes(OMD, Fn, FI); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 262 | |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 263 | args.push_back(OMD->getSelfDecl()); |
| 264 | args.push_back(OMD->getCmdDecl()); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 266 | for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), |
| 267 | E = OMD->param_end(); PI != E; ++PI) |
John McCall | d26bc76 | 2011-03-09 04:27:21 +0000 | [diff] [blame] | 268 | args.push_back(*PI); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 269 | |
Peter Collingbourne | 1411047 | 2011-01-13 18:57:25 +0000 | [diff] [blame] | 270 | CurGD = OMD; |
| 271 | |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 272 | StartFunction(OMD, OMD->getResultType(), Fn, FI, args, StartLoc); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 273 | |
| 274 | // In ARC, certain methods get an extra cleanup. |
| 275 | if (CGM.getLangOptions().ObjCAutoRefCount && |
| 276 | OMD->isInstanceMethod() && |
| 277 | OMD->getSelector().isUnarySelector()) { |
| 278 | const IdentifierInfo *ident = |
| 279 | OMD->getSelector().getIdentifierInfoForSlot(0); |
| 280 | if (ident->isStr("dealloc")) |
| 281 | EHStack.pushCleanup<FinishARCDealloc>(getARCCleanupKind()); |
| 282 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 283 | } |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 284 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 285 | static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
| 286 | LValue lvalue, QualType type); |
| 287 | |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 288 | void CodeGenFunction::GenerateObjCGetterBody(ObjCIvarDecl *Ivar, |
| 289 | bool IsAtomic, bool IsStrong) { |
| 290 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 291 | Ivar, 0); |
| 292 | llvm::Value *GetCopyStructFn = |
| 293 | CGM.getObjCRuntime().GetGetStructFunction(); |
| 294 | CodeGenTypes &Types = CGM.getTypes(); |
| 295 | // objc_copyStruct (ReturnValue, &structIvar, |
| 296 | // sizeof (Type of Ivar), isAtomic, false); |
| 297 | CallArgList Args; |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 298 | RValue RV = RValue::get(Builder.CreateBitCast(ReturnValue, VoidPtrTy)); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 299 | Args.add(RV, getContext().VoidPtrTy); |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 300 | RV = RValue::get(Builder.CreateBitCast(LV.getAddress(), VoidPtrTy)); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 301 | Args.add(RV, getContext().VoidPtrTy); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 302 | // sizeof (Type of Ivar) |
| 303 | CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType()); |
| 304 | llvm::Value *SizeVal = |
John McCall | 0774cb8 | 2011-05-15 01:53:33 +0000 | [diff] [blame] | 305 | llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 306 | Size.getQuantity()); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 307 | Args.add(RValue::get(SizeVal), getContext().LongTy); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 308 | llvm::Value *isAtomic = |
| 309 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), |
| 310 | IsAtomic ? 1 : 0); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 311 | Args.add(RValue::get(isAtomic), getContext().BoolTy); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 312 | llvm::Value *hasStrong = |
| 313 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), |
| 314 | IsStrong ? 1 : 0); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 315 | Args.add(RValue::get(hasStrong), getContext().BoolTy); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 316 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
| 317 | FunctionType::ExtInfo()), |
| 318 | GetCopyStructFn, ReturnValueSlot(), Args); |
| 319 | } |
| 320 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 321 | /// Generate an Objective-C method. An Objective-C method is a C function with |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 322 | /// its pointer, name, and types registered in the class struture. |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 323 | void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) { |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 324 | StartObjCMethod(OMD, OMD->getClassInterface(), OMD->getLocStart()); |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 325 | EmitStmt(OMD->getBody()); |
| 326 | FinishFunction(OMD->getBodyRBrace()); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 329 | // FIXME: I wasn't sure about the synthesis approach. If we end up generating an |
| 330 | // AST for the whole body we can just fall back to having a GenerateFunction |
| 331 | // which takes the body Stmt. |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 332 | |
| 333 | /// GenerateObjCGetter - Generate an Objective-C property getter |
Steve Naroff | 489034c | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 334 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize |
| 335 | /// is illegal within a category. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 336 | void CodeGenFunction::GenerateObjCGetter(ObjCImplementationDecl *IMP, |
| 337 | const ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 338 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 339 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 340 | bool IsAtomic = |
| 341 | !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 342 | ObjCMethodDecl *OMD = PD->getGetterMethodDecl(); |
| 343 | assert(OMD && "Invalid call to generate getter (empty method)"); |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 344 | StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart()); |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 345 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 346 | // Determine if we should use an objc_getProperty call for |
Fariborz Jahanian | 447d7ae | 2008-12-08 23:56:17 +0000 | [diff] [blame] | 347 | // this. Non-atomic properties are directly evaluated. |
| 348 | // atomic 'copy' and 'retain' properties are also directly |
| 349 | // evaluated in gc-only mode. |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 350 | if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly && |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 351 | IsAtomic && |
Fariborz Jahanian | 447d7ae | 2008-12-08 23:56:17 +0000 | [diff] [blame] | 352 | (PD->getSetterKind() == ObjCPropertyDecl::Copy || |
| 353 | PD->getSetterKind() == ObjCPropertyDecl::Retain)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | llvm::Value *GetPropertyFn = |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 355 | CGM.getObjCRuntime().GetPropertyGetFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 357 | if (!GetPropertyFn) { |
| 358 | CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy"); |
| 359 | FinishFunction(); |
| 360 | return; |
| 361 | } |
| 362 | |
| 363 | // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true). |
| 364 | // FIXME: Can't this be simpler? This might even be worse than the |
| 365 | // corresponding gcc code. |
| 366 | CodeGenTypes &Types = CGM.getTypes(); |
| 367 | ValueDecl *Cmd = OMD->getCmdDecl(); |
| 368 | llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd"); |
| 369 | QualType IdTy = getContext().getObjCIdType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 370 | llvm::Value *SelfAsId = |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 371 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 372 | llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 373 | llvm::Value *True = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 374 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 375 | CallArgList Args; |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 376 | Args.add(RValue::get(SelfAsId), IdTy); |
| 377 | Args.add(RValue::get(CmdVal), Cmd->getType()); |
| 378 | Args.add(RValue::get(Offset), getContext().getPointerDiffType()); |
| 379 | Args.add(RValue::get(True), getContext().BoolTy); |
Daniel Dunbar | e4be5a6 | 2009-02-03 23:43:59 +0000 | [diff] [blame] | 380 | // FIXME: We shouldn't need to get the function info here, the |
| 381 | // runtime already should have computed it to build the function. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 382 | RValue RV = EmitCall(Types.getFunctionInfo(PD->getType(), Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 383 | FunctionType::ExtInfo()), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 384 | GetPropertyFn, ReturnValueSlot(), Args); |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 385 | // We need to fix the type here. Ivars with copy & retain are |
| 386 | // always objects so we don't need to worry about complex or |
| 387 | // aggregates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(), |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 389 | Types.ConvertType(PD->getType()))); |
| 390 | EmitReturnOfRValue(RV, PD->getType()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 391 | |
| 392 | // objc_getProperty does an autorelease, so we should suppress ours. |
| 393 | AutoreleaseResult = false; |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 394 | } else { |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 395 | const llvm::Triple &Triple = getContext().Target.getTriple(); |
| 396 | QualType IVART = Ivar->getType(); |
| 397 | if (IsAtomic && |
| 398 | IVART->isScalarType() && |
| 399 | (Triple.getArch() == llvm::Triple::arm || |
| 400 | Triple.getArch() == llvm::Triple::thumb) && |
| 401 | (getContext().getTypeSizeInChars(IVART) |
| 402 | > CharUnits::fromQuantity(4)) && |
| 403 | CGM.getObjCRuntime().GetGetStructFunction()) { |
| 404 | GenerateObjCGetterBody(Ivar, true, false); |
| 405 | } |
Fariborz Jahanian | 1d3a61a | 2011-04-05 21:41:23 +0000 | [diff] [blame] | 406 | else if (IsAtomic && |
| 407 | (IVART->isScalarType() && !IVART->isRealFloatingType()) && |
| 408 | Triple.getArch() == llvm::Triple::x86 && |
| 409 | (getContext().getTypeSizeInChars(IVART) |
| 410 | > CharUnits::fromQuantity(4)) && |
| 411 | CGM.getObjCRuntime().GetGetStructFunction()) { |
| 412 | GenerateObjCGetterBody(Ivar, true, false); |
| 413 | } |
| 414 | else if (IsAtomic && |
| 415 | (IVART->isScalarType() && !IVART->isRealFloatingType()) && |
| 416 | Triple.getArch() == llvm::Triple::x86_64 && |
| 417 | (getContext().getTypeSizeInChars(IVART) |
| 418 | > CharUnits::fromQuantity(8)) && |
| 419 | CGM.getObjCRuntime().GetGetStructFunction()) { |
| 420 | GenerateObjCGetterBody(Ivar, true, false); |
| 421 | } |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 422 | else if (IVART->isAnyComplexType()) { |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 423 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 424 | Ivar, 0); |
Fariborz Jahanian | 1b23fe6 | 2010-03-25 21:56:43 +0000 | [diff] [blame] | 425 | ComplexPairTy Pair = LoadComplexFromAddr(LV.getAddress(), |
| 426 | LV.isVolatileQualified()); |
| 427 | StoreComplexToAddr(Pair, ReturnValue, LV.isVolatileQualified()); |
| 428 | } |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 429 | else if (hasAggregateLLVMType(IVART)) { |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 430 | bool IsStrong = false; |
Fariborz Jahanian | 5fb6509 | 2011-04-05 23:01:27 +0000 | [diff] [blame] | 431 | if ((IsStrong = IvarTypeWithAggrGCObjects(IVART)) |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 432 | && CurFnInfo->getReturnInfo().getKind() == ABIArgInfo::Indirect |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 433 | && CGM.getObjCRuntime().GetGetStructFunction()) { |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 434 | GenerateObjCGetterBody(Ivar, IsAtomic, IsStrong); |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 435 | } |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 436 | else { |
Fariborz Jahanian | 01cb307 | 2011-04-06 16:05:26 +0000 | [diff] [blame] | 437 | const CXXRecordDecl *classDecl = IVART->getAsCXXRecordDecl(); |
| 438 | |
| 439 | if (PID->getGetterCXXConstructor() && |
Sean Hunt | 023df37 | 2011-05-09 18:22:59 +0000 | [diff] [blame] | 440 | classDecl && !classDecl->hasTrivialDefaultConstructor()) { |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 441 | ReturnStmt *Stmt = |
| 442 | new (getContext()) ReturnStmt(SourceLocation(), |
Douglas Gregor | 5077c38 | 2010-05-15 06:01:05 +0000 | [diff] [blame] | 443 | PID->getGetterCXXConstructor(), |
| 444 | 0); |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 445 | EmitReturnStmt(*Stmt); |
Fariborz Jahanian | 1d3a61a | 2011-04-05 21:41:23 +0000 | [diff] [blame] | 446 | } else if (IsAtomic && |
| 447 | !IVART->isAnyComplexType() && |
| 448 | Triple.getArch() == llvm::Triple::x86 && |
| 449 | (getContext().getTypeSizeInChars(IVART) |
| 450 | > CharUnits::fromQuantity(4)) && |
| 451 | CGM.getObjCRuntime().GetGetStructFunction()) { |
| 452 | GenerateObjCGetterBody(Ivar, true, false); |
| 453 | } |
| 454 | else if (IsAtomic && |
| 455 | !IVART->isAnyComplexType() && |
| 456 | Triple.getArch() == llvm::Triple::x86_64 && |
| 457 | (getContext().getTypeSizeInChars(IVART) |
| 458 | > CharUnits::fromQuantity(8)) && |
| 459 | CGM.getObjCRuntime().GetGetStructFunction()) { |
| 460 | GenerateObjCGetterBody(Ivar, true, false); |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 461 | } |
| 462 | else { |
| 463 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 464 | Ivar, 0); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 465 | EmitAggregateCopy(ReturnValue, LV.getAddress(), IVART); |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 466 | } |
| 467 | } |
John McCall | ba3dd90 | 2011-07-22 05:23:13 +0000 | [diff] [blame^] | 468 | } else { |
| 469 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), |
| 470 | Ivar, 0); |
| 471 | QualType propType = PD->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 472 | |
John McCall | ba3dd90 | 2011-07-22 05:23:13 +0000 | [diff] [blame^] | 473 | llvm::Value *value; |
| 474 | if (propType->isReferenceType()) { |
| 475 | value = LV.getAddress(); |
| 476 | } else { |
| 477 | // We want to load and autoreleaseReturnValue ARC __weak ivars. |
| 478 | if (LV.getQuals().getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 479 | value = emitARCRetainLoadOfScalar(*this, LV, IVART); |
| 480 | |
| 481 | // Otherwise we want to do a simple load, suppressing the |
| 482 | // final autorelease. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 483 | } else { |
John McCall | ba3dd90 | 2011-07-22 05:23:13 +0000 | [diff] [blame^] | 484 | value = EmitLoadOfLValue(LV).getScalarVal(); |
| 485 | AutoreleaseResult = false; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 486 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 487 | |
John McCall | ba3dd90 | 2011-07-22 05:23:13 +0000 | [diff] [blame^] | 488 | value = Builder.CreateBitCast(value, ConvertType(propType)); |
| 489 | } |
| 490 | |
| 491 | EmitReturnOfRValue(RValue::get(value), propType); |
Fariborz Jahanian | ed1d29d | 2009-03-03 18:49:40 +0000 | [diff] [blame] | 492 | } |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 493 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 494 | |
| 495 | FinishFunction(); |
| 496 | } |
| 497 | |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 498 | void CodeGenFunction::GenerateObjCAtomicSetterBody(ObjCMethodDecl *OMD, |
| 499 | ObjCIvarDecl *Ivar) { |
| 500 | // objc_copyStruct (&structIvar, &Arg, |
| 501 | // sizeof (struct something), true, false); |
| 502 | llvm::Value *GetCopyStructFn = |
| 503 | CGM.getObjCRuntime().GetSetStructFunction(); |
| 504 | CodeGenTypes &Types = CGM.getTypes(); |
| 505 | CallArgList Args; |
| 506 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), LoadObjCSelf(), Ivar, 0); |
| 507 | RValue RV = |
| 508 | RValue::get(Builder.CreateBitCast(LV.getAddress(), |
| 509 | Types.ConvertType(getContext().VoidPtrTy))); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 510 | Args.add(RV, getContext().VoidPtrTy); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 511 | llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()]; |
| 512 | llvm::Value *ArgAsPtrTy = |
| 513 | Builder.CreateBitCast(Arg, |
| 514 | Types.ConvertType(getContext().VoidPtrTy)); |
| 515 | RV = RValue::get(ArgAsPtrTy); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 516 | Args.add(RV, getContext().VoidPtrTy); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 517 | // sizeof (Type of Ivar) |
| 518 | CharUnits Size = getContext().getTypeSizeInChars(Ivar->getType()); |
| 519 | llvm::Value *SizeVal = |
| 520 | llvm::ConstantInt::get(Types.ConvertType(getContext().LongTy), |
| 521 | Size.getQuantity()); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 522 | Args.add(RValue::get(SizeVal), getContext().LongTy); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 523 | llvm::Value *True = |
| 524 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 525 | Args.add(RValue::get(True), getContext().BoolTy); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 526 | llvm::Value *False = |
| 527 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 528 | Args.add(RValue::get(False), getContext().BoolTy); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 529 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
| 530 | FunctionType::ExtInfo()), |
| 531 | GetCopyStructFn, ReturnValueSlot(), Args); |
| 532 | } |
| 533 | |
Fariborz Jahanian | 01cb307 | 2011-04-06 16:05:26 +0000 | [diff] [blame] | 534 | static bool |
| 535 | IvarAssignHasTrvialAssignment(const ObjCPropertyImplDecl *PID, |
| 536 | QualType IvarT) { |
| 537 | bool HasTrvialAssignment = true; |
| 538 | if (PID->getSetterCXXAssignment()) { |
| 539 | const CXXRecordDecl *classDecl = IvarT->getAsCXXRecordDecl(); |
| 540 | HasTrvialAssignment = |
| 541 | (!classDecl || classDecl->hasTrivialCopyAssignment()); |
| 542 | } |
| 543 | return HasTrvialAssignment; |
| 544 | } |
| 545 | |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 546 | /// GenerateObjCSetter - Generate an Objective-C property setter |
Steve Naroff | 489034c | 2009-01-10 22:55:25 +0000 | [diff] [blame] | 547 | /// function. The given Decl must be an ObjCImplementationDecl. @synthesize |
| 548 | /// is illegal within a category. |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 549 | void CodeGenFunction::GenerateObjCSetter(ObjCImplementationDecl *IMP, |
| 550 | const ObjCPropertyImplDecl *PID) { |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 551 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 552 | const ObjCPropertyDecl *PD = PID->getPropertyDecl(); |
| 553 | ObjCMethodDecl *OMD = PD->getSetterMethodDecl(); |
| 554 | assert(OMD && "Invalid call to generate setter (empty method)"); |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 555 | StartObjCMethod(OMD, IMP->getClassInterface(), PID->getLocStart()); |
Fariborz Jahanian | 1d3a61a | 2011-04-05 21:41:23 +0000 | [diff] [blame] | 556 | const llvm::Triple &Triple = getContext().Target.getTriple(); |
| 557 | QualType IVART = Ivar->getType(); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 558 | bool IsCopy = PD->getSetterKind() == ObjCPropertyDecl::Copy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | bool IsAtomic = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 560 | !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic); |
| 561 | |
| 562 | // Determine if we should use an objc_setProperty call for |
| 563 | // this. Properties with 'copy' semantics always use it, as do |
| 564 | // non-atomic properties with 'release' semantics as long as we are |
| 565 | // not in gc-only mode. |
| 566 | if (IsCopy || |
| 567 | (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly && |
| 568 | PD->getSetterKind() == ObjCPropertyDecl::Retain)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 569 | llvm::Value *SetPropertyFn = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 570 | CGM.getObjCRuntime().GetPropertySetFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 571 | |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 572 | if (!SetPropertyFn) { |
| 573 | CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy"); |
| 574 | FinishFunction(); |
| 575 | return; |
| 576 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 577 | |
| 578 | // Emit objc_setProperty((id) self, _cmd, offset, arg, |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 579 | // <is-atomic>, <is-copy>). |
| 580 | // FIXME: Can't this be simpler? This might even be worse than the |
| 581 | // corresponding gcc code. |
| 582 | CodeGenTypes &Types = CGM.getTypes(); |
| 583 | ValueDecl *Cmd = OMD->getCmdDecl(); |
| 584 | llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd"); |
| 585 | QualType IdTy = getContext().getObjCIdType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | llvm::Value *SelfAsId = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 587 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
Fariborz Jahanian | fef30b5 | 2008-12-09 20:23:04 +0000 | [diff] [blame] | 588 | llvm::Value *Offset = EmitIvarOffset(IMP->getClassInterface(), Ivar); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 589 | llvm::Value *Arg = LocalDeclMap[*OMD->param_begin()]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | llvm::Value *ArgAsId = |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 591 | Builder.CreateBitCast(Builder.CreateLoad(Arg, "arg"), |
| 592 | Types.ConvertType(IdTy)); |
| 593 | llvm::Value *True = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 594 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 1); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 595 | llvm::Value *False = |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 596 | llvm::ConstantInt::get(Types.ConvertType(getContext().BoolTy), 0); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 597 | CallArgList Args; |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 598 | Args.add(RValue::get(SelfAsId), IdTy); |
| 599 | Args.add(RValue::get(CmdVal), Cmd->getType()); |
| 600 | Args.add(RValue::get(Offset), getContext().getPointerDiffType()); |
| 601 | Args.add(RValue::get(ArgAsId), IdTy); |
| 602 | Args.add(RValue::get(IsAtomic ? True : False), getContext().BoolTy); |
| 603 | Args.add(RValue::get(IsCopy ? True : False), getContext().BoolTy); |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 604 | // FIXME: We shouldn't need to get the function info here, the runtime |
| 605 | // already should have computed it to build the function. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 606 | EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 607 | FunctionType::ExtInfo()), |
| 608 | SetPropertyFn, |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 609 | ReturnValueSlot(), Args); |
Fariborz Jahanian | 1d3a61a | 2011-04-05 21:41:23 +0000 | [diff] [blame] | 610 | } else if (IsAtomic && hasAggregateLLVMType(IVART) && |
| 611 | !IVART->isAnyComplexType() && |
Fariborz Jahanian | 01cb307 | 2011-04-06 16:05:26 +0000 | [diff] [blame] | 612 | IvarAssignHasTrvialAssignment(PID, IVART) && |
Fariborz Jahanian | 1d3a61a | 2011-04-05 21:41:23 +0000 | [diff] [blame] | 613 | ((Triple.getArch() == llvm::Triple::x86 && |
| 614 | (getContext().getTypeSizeInChars(IVART) |
| 615 | > CharUnits::fromQuantity(4))) || |
| 616 | (Triple.getArch() == llvm::Triple::x86_64 && |
| 617 | (getContext().getTypeSizeInChars(IVART) |
| 618 | > CharUnits::fromQuantity(8)))) |
David Chisnall | 8fac25d | 2010-12-26 22:13:16 +0000 | [diff] [blame] | 619 | && CGM.getObjCRuntime().GetSetStructFunction()) { |
Fariborz Jahanian | 1d3a61a | 2011-04-05 21:41:23 +0000 | [diff] [blame] | 620 | // objc_copyStruct (&structIvar, &Arg, |
| 621 | // sizeof (struct something), true, false); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 622 | GenerateObjCAtomicSetterBody(OMD, Ivar); |
Fariborz Jahanian | 97a73cd | 2010-05-06 15:45:36 +0000 | [diff] [blame] | 623 | } else if (PID->getSetterCXXAssignment()) { |
John McCall | 2a41637 | 2010-12-05 02:00:02 +0000 | [diff] [blame] | 624 | EmitIgnoredExpr(PID->getSetterCXXAssignment()); |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 625 | } else { |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 626 | if (IsAtomic && |
| 627 | IVART->isScalarType() && |
| 628 | (Triple.getArch() == llvm::Triple::arm || |
| 629 | Triple.getArch() == llvm::Triple::thumb) && |
| 630 | (getContext().getTypeSizeInChars(IVART) |
| 631 | > CharUnits::fromQuantity(4)) && |
| 632 | CGM.getObjCRuntime().GetGetStructFunction()) { |
| 633 | GenerateObjCAtomicSetterBody(OMD, Ivar); |
| 634 | } |
Fariborz Jahanian | 1d3a61a | 2011-04-05 21:41:23 +0000 | [diff] [blame] | 635 | else if (IsAtomic && |
| 636 | (IVART->isScalarType() && !IVART->isRealFloatingType()) && |
| 637 | Triple.getArch() == llvm::Triple::x86 && |
| 638 | (getContext().getTypeSizeInChars(IVART) |
| 639 | > CharUnits::fromQuantity(4)) && |
| 640 | CGM.getObjCRuntime().GetGetStructFunction()) { |
| 641 | GenerateObjCAtomicSetterBody(OMD, Ivar); |
| 642 | } |
| 643 | else if (IsAtomic && |
| 644 | (IVART->isScalarType() && !IVART->isRealFloatingType()) && |
| 645 | Triple.getArch() == llvm::Triple::x86_64 && |
| 646 | (getContext().getTypeSizeInChars(IVART) |
| 647 | > CharUnits::fromQuantity(8)) && |
| 648 | CGM.getObjCRuntime().GetGetStructFunction()) { |
| 649 | GenerateObjCAtomicSetterBody(OMD, Ivar); |
| 650 | } |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 651 | else { |
| 652 | // FIXME: Find a clean way to avoid AST node creation. |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 653 | SourceLocation Loc = PID->getLocStart(); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 654 | ValueDecl *Self = OMD->getSelfDecl(); |
| 655 | ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl(); |
| 656 | DeclRefExpr Base(Self, Self->getType(), VK_RValue, Loc); |
| 657 | ParmVarDecl *ArgDecl = *OMD->param_begin(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 658 | QualType T = ArgDecl->getType(); |
| 659 | if (T->isReferenceType()) |
| 660 | T = cast<ReferenceType>(T)->getPointeeType(); |
| 661 | DeclRefExpr Arg(ArgDecl, T, VK_LValue, Loc); |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 662 | ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true); |
Daniel Dunbar | 45e8423 | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 663 | |
Fariborz Jahanian | 2846b97 | 2011-02-18 19:15:13 +0000 | [diff] [blame] | 664 | // The property type can differ from the ivar type in some situations with |
| 665 | // Objective-C pointer types, we can always bit cast the RHS in these cases. |
| 666 | if (getContext().getCanonicalType(Ivar->getType()) != |
| 667 | getContext().getCanonicalType(ArgDecl->getType())) { |
| 668 | ImplicitCastExpr ArgCasted(ImplicitCastExpr::OnStack, |
| 669 | Ivar->getType(), CK_BitCast, &Arg, |
| 670 | VK_RValue); |
| 671 | BinaryOperator Assign(&IvarRef, &ArgCasted, BO_Assign, |
| 672 | Ivar->getType(), VK_RValue, OK_Ordinary, Loc); |
| 673 | EmitStmt(&Assign); |
| 674 | } else { |
| 675 | BinaryOperator Assign(&IvarRef, &Arg, BO_Assign, |
| 676 | Ivar->getType(), VK_RValue, OK_Ordinary, Loc); |
| 677 | EmitStmt(&Assign); |
| 678 | } |
Daniel Dunbar | 45e8423 | 2009-10-27 19:21:30 +0000 | [diff] [blame] | 679 | } |
Daniel Dunbar | 86957eb | 2008-09-24 06:32:09 +0000 | [diff] [blame] | 680 | } |
Daniel Dunbar | af05bb9 | 2008-08-26 08:29:31 +0000 | [diff] [blame] | 681 | |
| 682 | FinishFunction(); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 683 | } |
| 684 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 685 | namespace { |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 686 | struct DestroyIvar : EHScopeStack::Cleanup { |
| 687 | private: |
| 688 | llvm::Value *addr; |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 689 | const ObjCIvarDecl *ivar; |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 690 | CodeGenFunction::Destroyer &destroyer; |
| 691 | bool useEHCleanupForArray; |
| 692 | public: |
| 693 | DestroyIvar(llvm::Value *addr, const ObjCIvarDecl *ivar, |
| 694 | CodeGenFunction::Destroyer *destroyer, |
| 695 | bool useEHCleanupForArray) |
| 696 | : addr(addr), ivar(ivar), destroyer(*destroyer), |
| 697 | useEHCleanupForArray(useEHCleanupForArray) {} |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 698 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 699 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 700 | LValue lvalue |
| 701 | = CGF.EmitLValueForIvar(CGF.TypeOfSelfObject(), addr, ivar, /*CVR*/ 0); |
| 702 | CGF.emitDestroy(lvalue.getAddress(), ivar->getType(), destroyer, |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 703 | flags.isForNormalCleanup() && useEHCleanupForArray); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 704 | } |
| 705 | }; |
| 706 | } |
| 707 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 708 | /// Like CodeGenFunction::destroyARCStrong, but do it with a call. |
| 709 | static void destroyARCStrongWithStore(CodeGenFunction &CGF, |
| 710 | llvm::Value *addr, |
| 711 | QualType type) { |
| 712 | llvm::Value *null = getNullForVariable(addr); |
| 713 | CGF.EmitARCStoreStrongCall(addr, null, /*ignored*/ true); |
| 714 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 715 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 716 | static void emitCXXDestructMethod(CodeGenFunction &CGF, |
| 717 | ObjCImplementationDecl *impl) { |
| 718 | CodeGenFunction::RunCleanupsScope scope(CGF); |
| 719 | |
| 720 | llvm::Value *self = CGF.LoadObjCSelf(); |
| 721 | |
Jordy Rose | db8264e | 2011-07-22 02:08:32 +0000 | [diff] [blame] | 722 | const ObjCInterfaceDecl *iface = impl->getClassInterface(); |
| 723 | for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin(); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 724 | ivar; ivar = ivar->getNextIvar()) { |
| 725 | QualType type = ivar->getType(); |
| 726 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 727 | // Check whether the ivar is a destructible type. |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 728 | QualType::DestructionKind dtorKind = type.isDestructedType(); |
| 729 | if (!dtorKind) continue; |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 730 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 731 | CodeGenFunction::Destroyer *destroyer = 0; |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 732 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 733 | // Use a call to objc_storeStrong to destroy strong ivars, for the |
| 734 | // general benefit of the tools. |
| 735 | if (dtorKind == QualType::DK_objc_strong_lifetime) { |
| 736 | destroyer = &destroyARCStrongWithStore; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 737 | |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 738 | // Otherwise use the default for the destruction kind. |
| 739 | } else { |
| 740 | destroyer = &CGF.getDestroyer(dtorKind); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 741 | } |
John McCall | 9928c48 | 2011-07-12 16:41:08 +0000 | [diff] [blame] | 742 | |
| 743 | CleanupKind cleanupKind = CGF.getCleanupKind(dtorKind); |
| 744 | |
| 745 | CGF.EHStack.pushCleanup<DestroyIvar>(cleanupKind, self, ivar, destroyer, |
| 746 | cleanupKind & EHCleanup); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | assert(scope.requiresCleanups() && "nothing to do in .cxx_destruct?"); |
| 750 | } |
| 751 | |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 752 | void CodeGenFunction::GenerateObjCCtorDtorMethod(ObjCImplementationDecl *IMP, |
| 753 | ObjCMethodDecl *MD, |
| 754 | bool ctor) { |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 755 | MD->createImplicitParams(CGM.getContext(), IMP->getClassInterface()); |
Devang Patel | 8d3f897 | 2011-05-19 23:37:41 +0000 | [diff] [blame] | 756 | StartObjCMethod(MD, IMP->getClassInterface(), MD->getLocStart()); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 757 | |
| 758 | // Emit .cxx_construct. |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 759 | if (ctor) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 760 | // Suppress the final autorelease in ARC. |
| 761 | AutoreleaseResult = false; |
| 762 | |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 763 | llvm::SmallVector<CXXCtorInitializer *, 8> IvarInitializers; |
| 764 | for (ObjCImplementationDecl::init_const_iterator B = IMP->init_begin(), |
| 765 | E = IMP->init_end(); B != E; ++B) { |
| 766 | CXXCtorInitializer *IvarInit = (*B); |
Francois Pichet | 00eb3f9 | 2010-12-04 09:14:42 +0000 | [diff] [blame] | 767 | FieldDecl *Field = IvarInit->getAnyMember(); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 768 | ObjCIvarDecl *Ivar = cast<ObjCIvarDecl>(Field); |
Fariborz Jahanian | 9b4d4fc | 2010-04-28 22:30:33 +0000 | [diff] [blame] | 769 | LValue LV = EmitLValueForIvar(TypeOfSelfObject(), |
| 770 | LoadObjCSelf(), Ivar, 0); |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 771 | EmitAggExpr(IvarInit->getInit(), AggValueSlot::forLValue(LV, true)); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 772 | } |
| 773 | // constructor returns 'self'. |
| 774 | CodeGenTypes &Types = CGM.getTypes(); |
| 775 | QualType IdTy(CGM.getContext().getObjCIdType()); |
| 776 | llvm::Value *SelfAsId = |
| 777 | Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy)); |
| 778 | EmitReturnOfRValue(RValue::get(SelfAsId), IdTy); |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 779 | |
| 780 | // Emit .cxx_destruct. |
Chandler Carruth | bc397cf | 2010-05-06 00:20:39 +0000 | [diff] [blame] | 781 | } else { |
John McCall | e81ac69 | 2011-03-22 07:05:39 +0000 | [diff] [blame] | 782 | emitCXXDestructMethod(*this, IMP); |
Fariborz Jahanian | 109dfc6 | 2010-04-28 21:28:56 +0000 | [diff] [blame] | 783 | } |
| 784 | FinishFunction(); |
| 785 | } |
| 786 | |
Fariborz Jahanian | 0b2bd47 | 2010-04-13 00:38:05 +0000 | [diff] [blame] | 787 | bool CodeGenFunction::IndirectObjCSetterArg(const CGFunctionInfo &FI) { |
| 788 | CGFunctionInfo::const_arg_iterator it = FI.arg_begin(); |
| 789 | it++; it++; |
| 790 | const ABIArgInfo &AI = it->info; |
| 791 | // FIXME. Is this sufficient check? |
| 792 | return (AI.getKind() == ABIArgInfo::Indirect); |
| 793 | } |
| 794 | |
Fariborz Jahanian | 15bd588 | 2010-04-13 18:32:24 +0000 | [diff] [blame] | 795 | bool CodeGenFunction::IvarTypeWithAggrGCObjects(QualType Ty) { |
| 796 | if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC) |
| 797 | return false; |
| 798 | if (const RecordType *FDTTy = Ty.getTypePtr()->getAs<RecordType>()) |
| 799 | return FDTTy->getDecl()->hasObjectMember(); |
| 800 | return false; |
| 801 | } |
| 802 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 803 | llvm::Value *CodeGenFunction::LoadObjCSelf() { |
Daniel Dunbar | b7ec246 | 2008-08-16 03:19:19 +0000 | [diff] [blame] | 804 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 805 | return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self"); |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 808 | QualType CodeGenFunction::TypeOfSelfObject() { |
| 809 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl); |
| 810 | ImplicitParamDecl *selfDecl = OMD->getSelfDecl(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 811 | const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>( |
| 812 | getContext().getCanonicalType(selfDecl->getType())); |
Fariborz Jahanian | 45012a7 | 2009-02-03 00:09:52 +0000 | [diff] [blame] | 813 | return PTy->getPointeeType(); |
| 814 | } |
| 815 | |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 816 | LValue |
| 817 | CodeGenFunction::EmitObjCPropertyRefLValue(const ObjCPropertyRefExpr *E) { |
| 818 | // This is a special l-value that just issues sends when we load or |
| 819 | // store through it. |
| 820 | |
| 821 | // For certain base kinds, we need to emit the base immediately. |
| 822 | llvm::Value *Base; |
| 823 | if (E->isSuperReceiver()) |
| 824 | Base = LoadObjCSelf(); |
| 825 | else if (E->isClassReceiver()) |
| 826 | Base = CGM.getObjCRuntime().GetClass(Builder, E->getClassReceiver()); |
| 827 | else |
| 828 | Base = EmitScalarExpr(E->getBase()); |
| 829 | return LValue::MakePropertyRef(E, Base); |
| 830 | } |
| 831 | |
| 832 | static RValue GenerateMessageSendSuper(CodeGenFunction &CGF, |
| 833 | ReturnValueSlot Return, |
| 834 | QualType ResultType, |
| 835 | Selector S, |
| 836 | llvm::Value *Receiver, |
| 837 | const CallArgList &CallArgs) { |
| 838 | const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CGF.CurFuncDecl); |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 839 | bool isClassMessage = OMD->isClassMethod(); |
| 840 | bool isCategoryImpl = isa<ObjCCategoryImplDecl>(OMD->getDeclContext()); |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 841 | return CGF.CGM.getObjCRuntime() |
| 842 | .GenerateMessageSendSuper(CGF, Return, ResultType, |
| 843 | S, OMD->getClassInterface(), |
| 844 | isCategoryImpl, Receiver, |
| 845 | isClassMessage, CallArgs); |
Fariborz Jahanian | f469557 | 2009-03-20 19:18:21 +0000 | [diff] [blame] | 846 | } |
| 847 | |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 848 | RValue CodeGenFunction::EmitLoadOfPropertyRefLValue(LValue LV, |
| 849 | ReturnValueSlot Return) { |
| 850 | const ObjCPropertyRefExpr *E = LV.getPropertyRefExpr(); |
Fariborz Jahanian | 68af13f | 2011-03-30 16:11:20 +0000 | [diff] [blame] | 851 | QualType ResultType = E->getGetterResultType(); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 852 | Selector S; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 853 | const ObjCMethodDecl *method; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 854 | if (E->isExplicitProperty()) { |
| 855 | const ObjCPropertyDecl *Property = E->getExplicitProperty(); |
| 856 | S = Property->getGetterName(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 857 | method = Property->getGetterMethodDecl(); |
Mike Stump | b3589f4 | 2009-07-30 22:28:39 +0000 | [diff] [blame] | 858 | } else { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 859 | method = E->getImplicitPropertyGetter(); |
| 860 | S = method->getSelector(); |
Fariborz Jahanian | 43f4470 | 2008-11-22 22:30:21 +0000 | [diff] [blame] | 861 | } |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 862 | |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 863 | llvm::Value *Receiver = LV.getPropertyRefBaseAddr(); |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 864 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 865 | if (CGM.getLangOptions().ObjCAutoRefCount) { |
| 866 | QualType receiverType; |
| 867 | if (E->isSuperReceiver()) |
| 868 | receiverType = E->getSuperReceiverType(); |
| 869 | else if (E->isClassReceiver()) |
| 870 | receiverType = getContext().getObjCClassType(); |
| 871 | else |
| 872 | receiverType = E->getBase()->getType(); |
| 873 | } |
| 874 | |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 875 | // Accesses to 'super' follow a different code path. |
| 876 | if (E->isSuperReceiver()) |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 877 | return AdjustRelatedResultType(*this, E, method, |
| 878 | GenerateMessageSendSuper(*this, Return, |
| 879 | ResultType, |
| 880 | S, Receiver, |
| 881 | CallArgList())); |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 882 | const ObjCInterfaceDecl *ReceiverClass |
| 883 | = (E->isClassReceiver() ? E->getClassReceiver() : 0); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 884 | return AdjustRelatedResultType(*this, E, method, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 885 | CGM.getObjCRuntime(). |
| 886 | GenerateMessageSend(*this, Return, ResultType, S, |
| 887 | Receiver, CallArgList(), ReceiverClass)); |
Daniel Dunbar | 9c3fc70 | 2008-08-27 06:57:25 +0000 | [diff] [blame] | 888 | } |
| 889 | |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 890 | void CodeGenFunction::EmitStoreThroughPropertyRefLValue(RValue Src, |
| 891 | LValue Dst) { |
| 892 | const ObjCPropertyRefExpr *E = Dst.getPropertyRefExpr(); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 893 | Selector S = E->getSetterSelector(); |
Fariborz Jahanian | 68af13f | 2011-03-30 16:11:20 +0000 | [diff] [blame] | 894 | QualType ArgType = E->getSetterArgType(); |
| 895 | |
Fariborz Jahanian | b19c76e | 2011-02-08 22:33:23 +0000 | [diff] [blame] | 896 | // FIXME. Other than scalars, AST is not adequate for setter and |
| 897 | // getter type mismatches which require conversion. |
| 898 | if (Src.isScalar()) { |
| 899 | llvm::Value *SrcVal = Src.getScalarVal(); |
| 900 | QualType DstType = getContext().getCanonicalType(ArgType); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 901 | llvm::Type *DstTy = ConvertType(DstType); |
Fariborz Jahanian | b19c76e | 2011-02-08 22:33:23 +0000 | [diff] [blame] | 902 | if (SrcVal->getType() != DstTy) |
| 903 | Src = |
| 904 | RValue::get(EmitScalarConversion(SrcVal, E->getType(), DstType)); |
| 905 | } |
| 906 | |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 907 | CallArgList Args; |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 908 | Args.add(Src, ArgType); |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 909 | |
| 910 | llvm::Value *Receiver = Dst.getPropertyRefBaseAddr(); |
| 911 | QualType ResultType = getContext().VoidTy; |
| 912 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 913 | if (E->isSuperReceiver()) { |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 914 | GenerateMessageSendSuper(*this, ReturnValueSlot(), |
| 915 | ResultType, S, Receiver, Args); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 916 | return; |
| 917 | } |
| 918 | |
John McCall | 119a1c6 | 2010-12-04 02:32:38 +0000 | [diff] [blame] | 919 | const ObjCInterfaceDecl *ReceiverClass |
| 920 | = (E->isClassReceiver() ? E->getClassReceiver() : 0); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 921 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 922 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
John McCall | e68b984 | 2010-12-04 03:11:00 +0000 | [diff] [blame] | 923 | ResultType, S, Receiver, Args, |
| 924 | ReceiverClass); |
Daniel Dunbar | 85c59ed | 2008-08-29 08:11:39 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Chris Lattner | 74391b4 | 2009-03-22 21:03:39 +0000 | [diff] [blame] | 927 | void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S){ |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | llvm::Constant *EnumerationMutationFn = |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 929 | CGM.getObjCRuntime().EnumerationMutationFunction(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 930 | |
Daniel Dunbar | c1cf4a5 | 2008-09-24 04:04:31 +0000 | [diff] [blame] | 931 | if (!EnumerationMutationFn) { |
| 932 | CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime"); |
| 933 | return; |
| 934 | } |
| 935 | |
Devang Patel | bcbd03a | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 936 | CGDebugInfo *DI = getDebugInfo(); |
| 937 | if (DI) { |
| 938 | DI->setLocation(S.getSourceRange().getBegin()); |
| 939 | DI->EmitRegionStart(Builder); |
| 940 | } |
| 941 | |
Devang Patel | 9d99f2d | 2011-06-13 23:15:32 +0000 | [diff] [blame] | 942 | // The local variable comes into scope immediately. |
| 943 | AutoVarEmission variable = AutoVarEmission::invalid(); |
| 944 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) |
| 945 | variable = EmitAutoVarAlloca(*cast<VarDecl>(SD->getSingleDecl())); |
| 946 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 947 | JumpDest LoopEnd = getJumpDestInCurrentScope("forcoll.end"); |
| 948 | JumpDest AfterBody = getJumpDestInCurrentScope("forcoll.next"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 949 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 950 | // Fast enumeration state. |
| 951 | QualType StateTy = getContext().getObjCFastEnumerationStateType(); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 952 | llvm::Value *StatePtr = CreateMemTemp(StateTy, "state.ptr"); |
Anders Carlsson | 1884eb0 | 2010-05-22 17:35:42 +0000 | [diff] [blame] | 953 | EmitNullInitialization(StatePtr, StateTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 955 | // Number of elements in the items array. |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 956 | static const unsigned NumItems = 16; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 957 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 958 | // Fetch the countByEnumeratingWithState:objects:count: selector. |
Benjamin Kramer | ad46886 | 2010-03-30 11:36:44 +0000 | [diff] [blame] | 959 | IdentifierInfo *II[] = { |
| 960 | &CGM.getContext().Idents.get("countByEnumeratingWithState"), |
| 961 | &CGM.getContext().Idents.get("objects"), |
| 962 | &CGM.getContext().Idents.get("count") |
| 963 | }; |
| 964 | Selector FastEnumSel = |
| 965 | CGM.getContext().Selectors.getSelector(llvm::array_lengthof(II), &II[0]); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 966 | |
| 967 | QualType ItemsTy = |
| 968 | getContext().getConstantArrayType(getContext().getObjCIdType(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 969 | llvm::APInt(32, NumItems), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 970 | ArrayType::Normal, 0); |
Daniel Dunbar | 195337d | 2010-02-09 02:48:28 +0000 | [diff] [blame] | 971 | llvm::Value *ItemsPtr = CreateMemTemp(ItemsTy, "items.ptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 973 | // Emit the collection pointer. |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 974 | llvm::Value *Collection = EmitScalarExpr(S.getCollection()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 975 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 976 | // Send it our message: |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 977 | CallArgList Args; |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 978 | |
| 979 | // The first argument is a temporary of the enumeration-state type. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 980 | Args.add(RValue::get(StatePtr), getContext().getPointerType(StateTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 981 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 982 | // The second argument is a temporary array with space for NumItems |
| 983 | // pointers. We'll actually be loading elements from the array |
| 984 | // pointer written into the control state; this buffer is so that |
| 985 | // collections that *aren't* backed by arrays can still queue up |
| 986 | // batches of elements. |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 987 | Args.add(RValue::get(ItemsPtr), getContext().getPointerType(ItemsTy)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 988 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 989 | // The third argument is the capacity of that temporary array. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 990 | llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy); |
Owen Anderson | 4a28d5d | 2009-07-24 23:12:58 +0000 | [diff] [blame] | 991 | llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems); |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 992 | Args.add(RValue::get(Count), getContext().UnsignedLongTy); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 994 | // Start the enumeration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 995 | RValue CountRV = |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 996 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 997 | getContext().UnsignedLongTy, |
| 998 | FastEnumSel, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 999 | Collection, Args); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1000 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1001 | // The initial number of objects that were returned in the buffer. |
| 1002 | llvm::Value *initialBufferLimit = CountRV.getScalarVal(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1003 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1004 | llvm::BasicBlock *EmptyBB = createBasicBlock("forcoll.empty"); |
| 1005 | llvm::BasicBlock *LoopInitBB = createBasicBlock("forcoll.loopinit"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1006 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1007 | llvm::Value *zero = llvm::Constant::getNullValue(UnsignedLongLTy); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1008 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1009 | // If the limit pointer was zero to begin with, the collection is |
| 1010 | // empty; skip all this. |
| 1011 | Builder.CreateCondBr(Builder.CreateICmpEQ(initialBufferLimit, zero, "iszero"), |
| 1012 | EmptyBB, LoopInitBB); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1013 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1014 | // Otherwise, initialize the loop. |
| 1015 | EmitBlock(LoopInitBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1017 | // Save the initial mutations value. This is the value at an |
| 1018 | // address that was written into the state object by |
| 1019 | // countByEnumeratingWithState:objects:count:. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | llvm::Value *StateMutationsPtrPtr = |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1021 | Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1022 | llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1023 | "mutationsptr"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1024 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1025 | llvm::Value *initialMutations = |
| 1026 | Builder.CreateLoad(StateMutationsPtr, "forcoll.initial-mutations"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1028 | // Start looping. This is the point we return to whenever we have a |
| 1029 | // fresh, non-empty batch of objects. |
| 1030 | llvm::BasicBlock *LoopBodyBB = createBasicBlock("forcoll.loopbody"); |
| 1031 | EmitBlock(LoopBodyBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1032 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1033 | // The current index into the buffer. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1034 | llvm::PHINode *index = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.index"); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1035 | index->addIncoming(zero, LoopInitBB); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1036 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1037 | // The current buffer size. |
Jay Foad | bbf3bac | 2011-03-30 11:28:58 +0000 | [diff] [blame] | 1038 | llvm::PHINode *count = Builder.CreatePHI(UnsignedLongLTy, 3, "forcoll.count"); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1039 | count->addIncoming(initialBufferLimit, LoopInitBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1040 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1041 | // Check whether the mutations value has changed from where it was |
| 1042 | // at start. StateMutationsPtr should actually be invariant between |
| 1043 | // refreshes. |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1044 | StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr"); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1045 | llvm::Value *currentMutations |
| 1046 | = Builder.CreateLoad(StateMutationsPtr, "statemutations"); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1047 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1048 | llvm::BasicBlock *WasMutatedBB = createBasicBlock("forcoll.mutated"); |
Dan Gohman | 361cf98 | 2011-03-02 22:39:34 +0000 | [diff] [blame] | 1049 | llvm::BasicBlock *WasNotMutatedBB = createBasicBlock("forcoll.notmutated"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1051 | Builder.CreateCondBr(Builder.CreateICmpEQ(currentMutations, initialMutations), |
| 1052 | WasNotMutatedBB, WasMutatedBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1054 | // If so, call the enumeration-mutation function. |
| 1055 | EmitBlock(WasMutatedBB); |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1056 | llvm::Value *V = |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1057 | Builder.CreateBitCast(Collection, |
Anders Carlsson | 2abd89c | 2008-08-31 04:05:03 +0000 | [diff] [blame] | 1058 | ConvertType(getContext().getObjCIdType()), |
| 1059 | "tmp"); |
Daniel Dunbar | 2b2105e | 2009-02-03 23:55:40 +0000 | [diff] [blame] | 1060 | CallArgList Args2; |
Eli Friedman | 04c9a49 | 2011-05-02 17:57:46 +0000 | [diff] [blame] | 1061 | Args2.add(RValue::get(V), getContext().getObjCIdType()); |
Mike Stump | f5408fe | 2009-05-16 07:57:57 +0000 | [diff] [blame] | 1062 | // FIXME: We shouldn't need to get the function info here, the runtime already |
| 1063 | // should have computed it to build the function. |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1064 | EmitCall(CGM.getTypes().getFunctionInfo(getContext().VoidTy, Args2, |
Rafael Espindola | 264ba48 | 2010-03-30 20:24:48 +0000 | [diff] [blame] | 1065 | FunctionType::ExtInfo()), |
Anders Carlsson | f3c47c9 | 2009-12-24 19:25:24 +0000 | [diff] [blame] | 1066 | EnumerationMutationFn, ReturnValueSlot(), Args2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1067 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1068 | // Otherwise, or if the mutation function returns, just continue. |
| 1069 | EmitBlock(WasNotMutatedBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1071 | // Initialize the element variable. |
| 1072 | RunCleanupsScope elementVariableScope(*this); |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1073 | bool elementIsVariable; |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1074 | LValue elementLValue; |
| 1075 | QualType elementType; |
| 1076 | if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) { |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1077 | // Initialize the variable, in case it's a __block variable or something. |
| 1078 | EmitAutoVarInit(variable); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1079 | |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1080 | const VarDecl* D = cast<VarDecl>(SD->getSingleDecl()); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1081 | DeclRefExpr tempDRE(const_cast<VarDecl*>(D), D->getType(), |
| 1082 | VK_LValue, SourceLocation()); |
| 1083 | elementLValue = EmitLValue(&tempDRE); |
| 1084 | elementType = D->getType(); |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1085 | elementIsVariable = true; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1086 | |
| 1087 | if (D->isARCPseudoStrong()) |
| 1088 | elementLValue.getQuals().setObjCLifetime(Qualifiers::OCL_ExplicitNone); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1089 | } else { |
| 1090 | elementLValue = LValue(); // suppress warning |
| 1091 | elementType = cast<Expr>(S.getElement())->getType(); |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1092 | elementIsVariable = false; |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1093 | } |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1094 | llvm::Type *convertedElementType = ConvertType(elementType); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1095 | |
| 1096 | // Fetch the buffer out of the enumeration state. |
| 1097 | // TODO: this pointer should actually be invariant between |
| 1098 | // refreshes, which would help us do certain loop optimizations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1099 | llvm::Value *StateItemsPtr = |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1100 | Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr"); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1101 | llvm::Value *EnumStateItems = |
| 1102 | Builder.CreateLoad(StateItemsPtr, "stateitems"); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1103 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1104 | // Fetch the value at the current index from the buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | llvm::Value *CurrentItemPtr = |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1106 | Builder.CreateGEP(EnumStateItems, index, "currentitem.ptr"); |
| 1107 | llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1109 | // Cast that value to the right type. |
| 1110 | CurrentItem = Builder.CreateBitCast(CurrentItem, convertedElementType, |
| 1111 | "currentitem"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1113 | // Make sure we have an l-value. Yes, this gets evaluated every |
| 1114 | // time through the loop. |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1115 | if (!elementIsVariable) { |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1116 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1117 | EmitStoreThroughLValue(RValue::get(CurrentItem), elementLValue); |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 1118 | } else { |
| 1119 | EmitScalarInit(CurrentItem, elementLValue); |
| 1120 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1122 | // If we do have an element variable, this assignment is the end of |
| 1123 | // its initialization. |
| 1124 | if (elementIsVariable) |
| 1125 | EmitAutoVarCleanups(variable); |
| 1126 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1127 | // Perform the loop body, setting up break and continue labels. |
Anders Carlsson | e4b6d34 | 2009-02-10 05:52:02 +0000 | [diff] [blame] | 1128 | BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody)); |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1129 | { |
| 1130 | RunCleanupsScope Scope(*this); |
| 1131 | EmitStmt(S.getBody()); |
| 1132 | } |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1133 | BreakContinueStack.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1135 | // Destroy the element variable now. |
| 1136 | elementVariableScope.ForceCleanup(); |
| 1137 | |
| 1138 | // Check whether there are more elements. |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1139 | EmitBlock(AfterBody.getBlock()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1140 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1141 | llvm::BasicBlock *FetchMoreBB = createBasicBlock("forcoll.refetch"); |
Fariborz Jahanian | f0906c4 | 2009-01-06 18:56:31 +0000 | [diff] [blame] | 1142 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1143 | // First we check in the local buffer. |
| 1144 | llvm::Value *indexPlusOne |
| 1145 | = Builder.CreateAdd(index, llvm::ConstantInt::get(UnsignedLongLTy, 1)); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1146 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1147 | // If we haven't overrun the buffer yet, we can continue. |
| 1148 | Builder.CreateCondBr(Builder.CreateICmpULT(indexPlusOne, count), |
| 1149 | LoopBodyBB, FetchMoreBB); |
| 1150 | |
| 1151 | index->addIncoming(indexPlusOne, AfterBody.getBlock()); |
| 1152 | count->addIncoming(count, AfterBody.getBlock()); |
| 1153 | |
| 1154 | // Otherwise, we have to fetch more elements. |
| 1155 | EmitBlock(FetchMoreBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | |
| 1157 | CountRV = |
John McCall | ef072fd | 2010-05-22 01:48:05 +0000 | [diff] [blame] | 1158 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1159 | getContext().UnsignedLongTy, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | FastEnumSel, |
David Chisnall | c6cd5fd | 2010-04-28 19:33:36 +0000 | [diff] [blame] | 1161 | Collection, Args); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1163 | // If we got a zero count, we're done. |
| 1164 | llvm::Value *refetchCount = CountRV.getScalarVal(); |
| 1165 | |
| 1166 | // (note that the message send might split FetchMoreBB) |
| 1167 | index->addIncoming(zero, Builder.GetInsertBlock()); |
| 1168 | count->addIncoming(refetchCount, Builder.GetInsertBlock()); |
| 1169 | |
| 1170 | Builder.CreateCondBr(Builder.CreateICmpEQ(refetchCount, zero), |
| 1171 | EmptyBB, LoopBodyBB); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1172 | |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1173 | // No more elements. |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1174 | EmitBlock(EmptyBB); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1175 | |
John McCall | 57b3b6a | 2011-02-22 07:16:58 +0000 | [diff] [blame] | 1176 | if (!elementIsVariable) { |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1177 | // If the element was not a declaration, set it to be null. |
| 1178 | |
John McCall | d88687f | 2011-01-07 01:49:06 +0000 | [diff] [blame] | 1179 | llvm::Value *null = llvm::Constant::getNullValue(convertedElementType); |
| 1180 | elementLValue = EmitLValue(cast<Expr>(S.getElement())); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1181 | EmitStoreThroughLValue(RValue::get(null), elementLValue); |
Anders Carlsson | f484c31 | 2008-08-31 02:33:12 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Devang Patel | bcbd03a | 2011-01-19 01:36:36 +0000 | [diff] [blame] | 1184 | if (DI) { |
| 1185 | DI->setLocation(S.getSourceRange().getEnd()); |
| 1186 | DI->EmitRegionEnd(Builder); |
| 1187 | } |
| 1188 | |
John McCall | ff8e115 | 2010-07-23 21:56:41 +0000 | [diff] [blame] | 1189 | EmitBlock(LoopEnd.getBlock()); |
Anders Carlsson | 3d8400d | 2008-08-30 19:51:14 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1192 | void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1193 | CGM.getObjCRuntime().EmitTryStmt(*this, S); |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S) { |
Anders Carlsson | 64d5d6c | 2008-09-09 10:04:29 +0000 | [diff] [blame] | 1197 | CGM.getObjCRuntime().EmitThrowStmt(*this, S); |
| 1198 | } |
| 1199 | |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 1200 | void CodeGenFunction::EmitObjCAtSynchronizedStmt( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | const ObjCAtSynchronizedStmt &S) { |
John McCall | f1549f6 | 2010-07-06 01:34:17 +0000 | [diff] [blame] | 1202 | CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S); |
Chris Lattner | 10cac6f | 2008-11-15 21:26:17 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1205 | /// Produce the code for a CK_ObjCProduceObject. Just does a |
| 1206 | /// primitive retain. |
| 1207 | llvm::Value *CodeGenFunction::EmitObjCProduceObject(QualType type, |
| 1208 | llvm::Value *value) { |
| 1209 | return EmitARCRetain(type, value); |
| 1210 | } |
| 1211 | |
| 1212 | namespace { |
| 1213 | struct CallObjCRelease : EHScopeStack::Cleanup { |
| 1214 | CallObjCRelease(QualType type, llvm::Value *ptr, llvm::Value *condition) |
| 1215 | : type(type), ptr(ptr), condition(condition) {} |
| 1216 | QualType type; |
| 1217 | llvm::Value *ptr; |
| 1218 | llvm::Value *condition; |
| 1219 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1220 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1221 | llvm::Value *object; |
| 1222 | |
| 1223 | // If we're in a conditional branch, we had to stash away in an |
| 1224 | // alloca the pointer to be released. |
| 1225 | llvm::BasicBlock *cont = 0; |
| 1226 | if (condition) { |
| 1227 | llvm::BasicBlock *release = CGF.createBasicBlock("release.yes"); |
| 1228 | cont = CGF.createBasicBlock("release.cont"); |
| 1229 | |
| 1230 | llvm::Value *cond = CGF.Builder.CreateLoad(condition); |
| 1231 | CGF.Builder.CreateCondBr(cond, release, cont); |
| 1232 | CGF.EmitBlock(release); |
| 1233 | object = CGF.Builder.CreateLoad(ptr); |
| 1234 | } else { |
| 1235 | object = ptr; |
| 1236 | } |
| 1237 | |
| 1238 | CGF.EmitARCRelease(object, /*precise*/ true); |
| 1239 | |
| 1240 | if (cont) CGF.EmitBlock(cont); |
| 1241 | } |
| 1242 | }; |
| 1243 | } |
| 1244 | |
| 1245 | /// Produce the code for a CK_ObjCConsumeObject. Does a primitive |
| 1246 | /// release at the end of the full-expression. |
| 1247 | llvm::Value *CodeGenFunction::EmitObjCConsumeObject(QualType type, |
| 1248 | llvm::Value *object) { |
| 1249 | // If we're in a conditional branch, we need to make the cleanup |
| 1250 | // conditional. FIXME: this really needs to be supported by the |
| 1251 | // environment. |
| 1252 | llvm::AllocaInst *cond; |
| 1253 | llvm::Value *ptr; |
| 1254 | if (isInConditionalBranch()) { |
| 1255 | cond = CreateTempAlloca(Builder.getInt1Ty(), "release.cond"); |
| 1256 | ptr = CreateTempAlloca(object->getType(), "release.value"); |
| 1257 | |
| 1258 | // The alloca is false until we get here. |
| 1259 | // FIXME: er. doesn't this need to be set at the start of the condition? |
| 1260 | InitTempAlloca(cond, Builder.getFalse()); |
| 1261 | |
| 1262 | // Then it turns true. |
| 1263 | Builder.CreateStore(Builder.getTrue(), cond); |
| 1264 | Builder.CreateStore(object, ptr); |
| 1265 | } else { |
| 1266 | cond = 0; |
| 1267 | ptr = object; |
| 1268 | } |
| 1269 | |
| 1270 | EHStack.pushCleanup<CallObjCRelease>(getARCCleanupKind(), type, ptr, cond); |
| 1271 | return object; |
| 1272 | } |
| 1273 | |
| 1274 | llvm::Value *CodeGenFunction::EmitObjCExtendObjectLifetime(QualType type, |
| 1275 | llvm::Value *value) { |
| 1276 | return EmitARCRetainAutorelease(type, value); |
| 1277 | } |
| 1278 | |
| 1279 | |
| 1280 | static llvm::Constant *createARCRuntimeFunction(CodeGenModule &CGM, |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1281 | llvm::FunctionType *type, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1282 | llvm::StringRef fnName) { |
| 1283 | llvm::Constant *fn = CGM.CreateRuntimeFunction(type, fnName); |
| 1284 | |
| 1285 | // In -fobjc-no-arc-runtime, emit weak references to the runtime |
| 1286 | // support library. |
John McCall | 9f084a3 | 2011-07-06 00:26:06 +0000 | [diff] [blame] | 1287 | if (!CGM.getCodeGenOpts().ObjCRuntimeHasARC) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1288 | if (llvm::Function *f = dyn_cast<llvm::Function>(fn)) |
| 1289 | f->setLinkage(llvm::Function::ExternalWeakLinkage); |
| 1290 | |
| 1291 | return fn; |
| 1292 | } |
| 1293 | |
| 1294 | /// Perform an operation having the signature |
| 1295 | /// i8* (i8*) |
| 1296 | /// where a null input causes a no-op and returns null. |
| 1297 | static llvm::Value *emitARCValueOperation(CodeGenFunction &CGF, |
| 1298 | llvm::Value *value, |
| 1299 | llvm::Constant *&fn, |
| 1300 | llvm::StringRef fnName) { |
| 1301 | if (isa<llvm::ConstantPointerNull>(value)) return value; |
| 1302 | |
| 1303 | if (!fn) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1304 | std::vector<llvm::Type*> args(1, CGF.Int8PtrTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1305 | llvm::FunctionType *fnType = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1306 | llvm::FunctionType::get(CGF.Int8PtrTy, args, false); |
| 1307 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); |
| 1308 | } |
| 1309 | |
| 1310 | // Cast the argument to 'id'. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1311 | llvm::Type *origType = value->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1312 | value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); |
| 1313 | |
| 1314 | // Call the function. |
| 1315 | llvm::CallInst *call = CGF.Builder.CreateCall(fn, value); |
| 1316 | call->setDoesNotThrow(); |
| 1317 | |
| 1318 | // Cast the result back to the original type. |
| 1319 | return CGF.Builder.CreateBitCast(call, origType); |
| 1320 | } |
| 1321 | |
| 1322 | /// Perform an operation having the following signature: |
| 1323 | /// i8* (i8**) |
| 1324 | static llvm::Value *emitARCLoadOperation(CodeGenFunction &CGF, |
| 1325 | llvm::Value *addr, |
| 1326 | llvm::Constant *&fn, |
| 1327 | llvm::StringRef fnName) { |
| 1328 | if (!fn) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1329 | std::vector<llvm::Type*> args(1, CGF.Int8PtrPtrTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1330 | llvm::FunctionType *fnType = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1331 | llvm::FunctionType::get(CGF.Int8PtrTy, args, false); |
| 1332 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); |
| 1333 | } |
| 1334 | |
| 1335 | // Cast the argument to 'id*'. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1336 | llvm::Type *origType = addr->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1337 | addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); |
| 1338 | |
| 1339 | // Call the function. |
| 1340 | llvm::CallInst *call = CGF.Builder.CreateCall(fn, addr); |
| 1341 | call->setDoesNotThrow(); |
| 1342 | |
| 1343 | // Cast the result back to a dereference of the original type. |
| 1344 | llvm::Value *result = call; |
| 1345 | if (origType != CGF.Int8PtrPtrTy) |
| 1346 | result = CGF.Builder.CreateBitCast(result, |
| 1347 | cast<llvm::PointerType>(origType)->getElementType()); |
| 1348 | |
| 1349 | return result; |
| 1350 | } |
| 1351 | |
| 1352 | /// Perform an operation having the following signature: |
| 1353 | /// i8* (i8**, i8*) |
| 1354 | static llvm::Value *emitARCStoreOperation(CodeGenFunction &CGF, |
| 1355 | llvm::Value *addr, |
| 1356 | llvm::Value *value, |
| 1357 | llvm::Constant *&fn, |
| 1358 | llvm::StringRef fnName, |
| 1359 | bool ignored) { |
| 1360 | assert(cast<llvm::PointerType>(addr->getType())->getElementType() |
| 1361 | == value->getType()); |
| 1362 | |
| 1363 | if (!fn) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1364 | std::vector<llvm::Type*> argTypes(2); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1365 | argTypes[0] = CGF.Int8PtrPtrTy; |
| 1366 | argTypes[1] = CGF.Int8PtrTy; |
| 1367 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1368 | llvm::FunctionType *fnType |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1369 | = llvm::FunctionType::get(CGF.Int8PtrTy, argTypes, false); |
| 1370 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); |
| 1371 | } |
| 1372 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1373 | llvm::Type *origType = value->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1374 | |
| 1375 | addr = CGF.Builder.CreateBitCast(addr, CGF.Int8PtrPtrTy); |
| 1376 | value = CGF.Builder.CreateBitCast(value, CGF.Int8PtrTy); |
| 1377 | |
| 1378 | llvm::CallInst *result = CGF.Builder.CreateCall2(fn, addr, value); |
| 1379 | result->setDoesNotThrow(); |
| 1380 | |
| 1381 | if (ignored) return 0; |
| 1382 | |
| 1383 | return CGF.Builder.CreateBitCast(result, origType); |
| 1384 | } |
| 1385 | |
| 1386 | /// Perform an operation having the following signature: |
| 1387 | /// void (i8**, i8**) |
| 1388 | static void emitARCCopyOperation(CodeGenFunction &CGF, |
| 1389 | llvm::Value *dst, |
| 1390 | llvm::Value *src, |
| 1391 | llvm::Constant *&fn, |
| 1392 | llvm::StringRef fnName) { |
| 1393 | assert(dst->getType() == src->getType()); |
| 1394 | |
| 1395 | if (!fn) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1396 | std::vector<llvm::Type*> argTypes(2, CGF.Int8PtrPtrTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1397 | llvm::FunctionType *fnType |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1398 | = llvm::FunctionType::get(CGF.Builder.getVoidTy(), argTypes, false); |
| 1399 | fn = createARCRuntimeFunction(CGF.CGM, fnType, fnName); |
| 1400 | } |
| 1401 | |
| 1402 | dst = CGF.Builder.CreateBitCast(dst, CGF.Int8PtrPtrTy); |
| 1403 | src = CGF.Builder.CreateBitCast(src, CGF.Int8PtrPtrTy); |
| 1404 | |
| 1405 | llvm::CallInst *result = CGF.Builder.CreateCall2(fn, dst, src); |
| 1406 | result->setDoesNotThrow(); |
| 1407 | } |
| 1408 | |
| 1409 | /// Produce the code to do a retain. Based on the type, calls one of: |
| 1410 | /// call i8* @objc_retain(i8* %value) |
| 1411 | /// call i8* @objc_retainBlock(i8* %value) |
| 1412 | llvm::Value *CodeGenFunction::EmitARCRetain(QualType type, llvm::Value *value) { |
| 1413 | if (type->isBlockPointerType()) |
| 1414 | return EmitARCRetainBlock(value); |
| 1415 | else |
| 1416 | return EmitARCRetainNonBlock(value); |
| 1417 | } |
| 1418 | |
| 1419 | /// Retain the given object, with normal retain semantics. |
| 1420 | /// call i8* @objc_retain(i8* %value) |
| 1421 | llvm::Value *CodeGenFunction::EmitARCRetainNonBlock(llvm::Value *value) { |
| 1422 | return emitARCValueOperation(*this, value, |
| 1423 | CGM.getARCEntrypoints().objc_retain, |
| 1424 | "objc_retain"); |
| 1425 | } |
| 1426 | |
| 1427 | /// Retain the given block, with _Block_copy semantics. |
| 1428 | /// call i8* @objc_retainBlock(i8* %value) |
| 1429 | llvm::Value *CodeGenFunction::EmitARCRetainBlock(llvm::Value *value) { |
| 1430 | return emitARCValueOperation(*this, value, |
| 1431 | CGM.getARCEntrypoints().objc_retainBlock, |
| 1432 | "objc_retainBlock"); |
| 1433 | } |
| 1434 | |
| 1435 | /// Retain the given object which is the result of a function call. |
| 1436 | /// call i8* @objc_retainAutoreleasedReturnValue(i8* %value) |
| 1437 | /// |
| 1438 | /// Yes, this function name is one character away from a different |
| 1439 | /// call with completely different semantics. |
| 1440 | llvm::Value * |
| 1441 | CodeGenFunction::EmitARCRetainAutoreleasedReturnValue(llvm::Value *value) { |
| 1442 | // Fetch the void(void) inline asm which marks that we're going to |
| 1443 | // retain the autoreleased return value. |
| 1444 | llvm::InlineAsm *&marker |
| 1445 | = CGM.getARCEntrypoints().retainAutoreleasedReturnValueMarker; |
| 1446 | if (!marker) { |
| 1447 | llvm::StringRef assembly |
| 1448 | = CGM.getTargetCodeGenInfo() |
| 1449 | .getARCRetainAutoreleasedReturnValueMarker(); |
| 1450 | |
| 1451 | // If we have an empty assembly string, there's nothing to do. |
| 1452 | if (assembly.empty()) { |
| 1453 | |
| 1454 | // Otherwise, at -O0, build an inline asm that we're going to call |
| 1455 | // in a moment. |
| 1456 | } else if (CGM.getCodeGenOpts().OptimizationLevel == 0) { |
| 1457 | llvm::FunctionType *type = |
| 1458 | llvm::FunctionType::get(llvm::Type::getVoidTy(getLLVMContext()), |
| 1459 | /*variadic*/ false); |
| 1460 | |
| 1461 | marker = llvm::InlineAsm::get(type, assembly, "", /*sideeffects*/ true); |
| 1462 | |
| 1463 | // If we're at -O1 and above, we don't want to litter the code |
| 1464 | // with this marker yet, so leave a breadcrumb for the ARC |
| 1465 | // optimizer to pick up. |
| 1466 | } else { |
| 1467 | llvm::NamedMDNode *metadata = |
| 1468 | CGM.getModule().getOrInsertNamedMetadata( |
| 1469 | "clang.arc.retainAutoreleasedReturnValueMarker"); |
| 1470 | assert(metadata->getNumOperands() <= 1); |
| 1471 | if (metadata->getNumOperands() == 0) { |
| 1472 | llvm::Value *string = llvm::MDString::get(getLLVMContext(), assembly); |
| 1473 | llvm::Value *args[] = { string }; |
| 1474 | metadata->addOperand(llvm::MDNode::get(getLLVMContext(), args)); |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 | |
| 1479 | // Call the marker asm if we made one, which we do only at -O0. |
| 1480 | if (marker) Builder.CreateCall(marker); |
| 1481 | |
| 1482 | return emitARCValueOperation(*this, value, |
| 1483 | CGM.getARCEntrypoints().objc_retainAutoreleasedReturnValue, |
| 1484 | "objc_retainAutoreleasedReturnValue"); |
| 1485 | } |
| 1486 | |
| 1487 | /// Release the given object. |
| 1488 | /// call void @objc_release(i8* %value) |
| 1489 | void CodeGenFunction::EmitARCRelease(llvm::Value *value, bool precise) { |
| 1490 | if (isa<llvm::ConstantPointerNull>(value)) return; |
| 1491 | |
| 1492 | llvm::Constant *&fn = CGM.getARCEntrypoints().objc_release; |
| 1493 | if (!fn) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1494 | std::vector<llvm::Type*> args(1, Int8PtrTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1495 | llvm::FunctionType *fnType = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1496 | llvm::FunctionType::get(Builder.getVoidTy(), args, false); |
| 1497 | fn = createARCRuntimeFunction(CGM, fnType, "objc_release"); |
| 1498 | } |
| 1499 | |
| 1500 | // Cast the argument to 'id'. |
| 1501 | value = Builder.CreateBitCast(value, Int8PtrTy); |
| 1502 | |
| 1503 | // Call objc_release. |
| 1504 | llvm::CallInst *call = Builder.CreateCall(fn, value); |
| 1505 | call->setDoesNotThrow(); |
| 1506 | |
| 1507 | if (!precise) { |
| 1508 | llvm::SmallVector<llvm::Value*,1> args; |
| 1509 | call->setMetadata("clang.imprecise_release", |
| 1510 | llvm::MDNode::get(Builder.getContext(), args)); |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | /// Store into a strong object. Always calls this: |
| 1515 | /// call void @objc_storeStrong(i8** %addr, i8* %value) |
| 1516 | llvm::Value *CodeGenFunction::EmitARCStoreStrongCall(llvm::Value *addr, |
| 1517 | llvm::Value *value, |
| 1518 | bool ignored) { |
| 1519 | assert(cast<llvm::PointerType>(addr->getType())->getElementType() |
| 1520 | == value->getType()); |
| 1521 | |
| 1522 | llvm::Constant *&fn = CGM.getARCEntrypoints().objc_storeStrong; |
| 1523 | if (!fn) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1524 | llvm::Type *argTypes[] = { Int8PtrPtrTy, Int8PtrTy }; |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1525 | llvm::FunctionType *fnType |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1526 | = llvm::FunctionType::get(Builder.getVoidTy(), argTypes, false); |
| 1527 | fn = createARCRuntimeFunction(CGM, fnType, "objc_storeStrong"); |
| 1528 | } |
| 1529 | |
| 1530 | addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); |
| 1531 | llvm::Value *castValue = Builder.CreateBitCast(value, Int8PtrTy); |
| 1532 | |
| 1533 | Builder.CreateCall2(fn, addr, castValue)->setDoesNotThrow(); |
| 1534 | |
| 1535 | if (ignored) return 0; |
| 1536 | return value; |
| 1537 | } |
| 1538 | |
| 1539 | /// Store into a strong object. Sometimes calls this: |
| 1540 | /// call void @objc_storeStrong(i8** %addr, i8* %value) |
| 1541 | /// Other times, breaks it down into components. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1542 | llvm::Value *CodeGenFunction::EmitARCStoreStrong(LValue dst, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1543 | llvm::Value *newValue, |
| 1544 | bool ignored) { |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1545 | QualType type = dst.getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1546 | bool isBlock = type->isBlockPointerType(); |
| 1547 | |
| 1548 | // Use a store barrier at -O0 unless this is a block type or the |
| 1549 | // lvalue is inadequately aligned. |
| 1550 | if (shouldUseFusedARCCalls() && |
| 1551 | !isBlock && |
| 1552 | !(dst.getAlignment() && dst.getAlignment() < PointerAlignInBytes)) { |
| 1553 | return EmitARCStoreStrongCall(dst.getAddress(), newValue, ignored); |
| 1554 | } |
| 1555 | |
| 1556 | // Otherwise, split it out. |
| 1557 | |
| 1558 | // Retain the new value. |
| 1559 | newValue = EmitARCRetain(type, newValue); |
| 1560 | |
| 1561 | // Read the old value. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1562 | llvm::Value *oldValue = EmitLoadOfScalar(dst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1563 | |
| 1564 | // Store. We do this before the release so that any deallocs won't |
| 1565 | // see the old value. |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1566 | EmitStoreOfScalar(newValue, dst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1567 | |
| 1568 | // Finally, release the old value. |
| 1569 | EmitARCRelease(oldValue, /*precise*/ false); |
| 1570 | |
| 1571 | return newValue; |
| 1572 | } |
| 1573 | |
| 1574 | /// Autorelease the given object. |
| 1575 | /// call i8* @objc_autorelease(i8* %value) |
| 1576 | llvm::Value *CodeGenFunction::EmitARCAutorelease(llvm::Value *value) { |
| 1577 | return emitARCValueOperation(*this, value, |
| 1578 | CGM.getARCEntrypoints().objc_autorelease, |
| 1579 | "objc_autorelease"); |
| 1580 | } |
| 1581 | |
| 1582 | /// Autorelease the given object. |
| 1583 | /// call i8* @objc_autoreleaseReturnValue(i8* %value) |
| 1584 | llvm::Value * |
| 1585 | CodeGenFunction::EmitARCAutoreleaseReturnValue(llvm::Value *value) { |
| 1586 | return emitARCValueOperation(*this, value, |
| 1587 | CGM.getARCEntrypoints().objc_autoreleaseReturnValue, |
| 1588 | "objc_autoreleaseReturnValue"); |
| 1589 | } |
| 1590 | |
| 1591 | /// Do a fused retain/autorelease of the given object. |
| 1592 | /// call i8* @objc_retainAutoreleaseReturnValue(i8* %value) |
| 1593 | llvm::Value * |
| 1594 | CodeGenFunction::EmitARCRetainAutoreleaseReturnValue(llvm::Value *value) { |
| 1595 | return emitARCValueOperation(*this, value, |
| 1596 | CGM.getARCEntrypoints().objc_retainAutoreleaseReturnValue, |
| 1597 | "objc_retainAutoreleaseReturnValue"); |
| 1598 | } |
| 1599 | |
| 1600 | /// Do a fused retain/autorelease of the given object. |
| 1601 | /// call i8* @objc_retainAutorelease(i8* %value) |
| 1602 | /// or |
| 1603 | /// %retain = call i8* @objc_retainBlock(i8* %value) |
| 1604 | /// call i8* @objc_autorelease(i8* %retain) |
| 1605 | llvm::Value *CodeGenFunction::EmitARCRetainAutorelease(QualType type, |
| 1606 | llvm::Value *value) { |
| 1607 | if (!type->isBlockPointerType()) |
| 1608 | return EmitARCRetainAutoreleaseNonBlock(value); |
| 1609 | |
| 1610 | if (isa<llvm::ConstantPointerNull>(value)) return value; |
| 1611 | |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1612 | llvm::Type *origType = value->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1613 | value = Builder.CreateBitCast(value, Int8PtrTy); |
| 1614 | value = EmitARCRetainBlock(value); |
| 1615 | value = EmitARCAutorelease(value); |
| 1616 | return Builder.CreateBitCast(value, origType); |
| 1617 | } |
| 1618 | |
| 1619 | /// Do a fused retain/autorelease of the given object. |
| 1620 | /// call i8* @objc_retainAutorelease(i8* %value) |
| 1621 | llvm::Value * |
| 1622 | CodeGenFunction::EmitARCRetainAutoreleaseNonBlock(llvm::Value *value) { |
| 1623 | return emitARCValueOperation(*this, value, |
| 1624 | CGM.getARCEntrypoints().objc_retainAutorelease, |
| 1625 | "objc_retainAutorelease"); |
| 1626 | } |
| 1627 | |
| 1628 | /// i8* @objc_loadWeak(i8** %addr) |
| 1629 | /// Essentially objc_autorelease(objc_loadWeakRetained(addr)). |
| 1630 | llvm::Value *CodeGenFunction::EmitARCLoadWeak(llvm::Value *addr) { |
| 1631 | return emitARCLoadOperation(*this, addr, |
| 1632 | CGM.getARCEntrypoints().objc_loadWeak, |
| 1633 | "objc_loadWeak"); |
| 1634 | } |
| 1635 | |
| 1636 | /// i8* @objc_loadWeakRetained(i8** %addr) |
| 1637 | llvm::Value *CodeGenFunction::EmitARCLoadWeakRetained(llvm::Value *addr) { |
| 1638 | return emitARCLoadOperation(*this, addr, |
| 1639 | CGM.getARCEntrypoints().objc_loadWeakRetained, |
| 1640 | "objc_loadWeakRetained"); |
| 1641 | } |
| 1642 | |
| 1643 | /// i8* @objc_storeWeak(i8** %addr, i8* %value) |
| 1644 | /// Returns %value. |
| 1645 | llvm::Value *CodeGenFunction::EmitARCStoreWeak(llvm::Value *addr, |
| 1646 | llvm::Value *value, |
| 1647 | bool ignored) { |
| 1648 | return emitARCStoreOperation(*this, addr, value, |
| 1649 | CGM.getARCEntrypoints().objc_storeWeak, |
| 1650 | "objc_storeWeak", ignored); |
| 1651 | } |
| 1652 | |
| 1653 | /// i8* @objc_initWeak(i8** %addr, i8* %value) |
| 1654 | /// Returns %value. %addr is known to not have a current weak entry. |
| 1655 | /// Essentially equivalent to: |
| 1656 | /// *addr = nil; objc_storeWeak(addr, value); |
| 1657 | void CodeGenFunction::EmitARCInitWeak(llvm::Value *addr, llvm::Value *value) { |
| 1658 | // If we're initializing to null, just write null to memory; no need |
| 1659 | // to get the runtime involved. But don't do this if optimization |
| 1660 | // is enabled, because accounting for this would make the optimizer |
| 1661 | // much more complicated. |
| 1662 | if (isa<llvm::ConstantPointerNull>(value) && |
| 1663 | CGM.getCodeGenOpts().OptimizationLevel == 0) { |
| 1664 | Builder.CreateStore(value, addr); |
| 1665 | return; |
| 1666 | } |
| 1667 | |
| 1668 | emitARCStoreOperation(*this, addr, value, |
| 1669 | CGM.getARCEntrypoints().objc_initWeak, |
| 1670 | "objc_initWeak", /*ignored*/ true); |
| 1671 | } |
| 1672 | |
| 1673 | /// void @objc_destroyWeak(i8** %addr) |
| 1674 | /// Essentially objc_storeWeak(addr, nil). |
| 1675 | void CodeGenFunction::EmitARCDestroyWeak(llvm::Value *addr) { |
| 1676 | llvm::Constant *&fn = CGM.getARCEntrypoints().objc_destroyWeak; |
| 1677 | if (!fn) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1678 | std::vector<llvm::Type*> args(1, Int8PtrPtrTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1679 | llvm::FunctionType *fnType = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1680 | llvm::FunctionType::get(Builder.getVoidTy(), args, false); |
| 1681 | fn = createARCRuntimeFunction(CGM, fnType, "objc_destroyWeak"); |
| 1682 | } |
| 1683 | |
| 1684 | // Cast the argument to 'id*'. |
| 1685 | addr = Builder.CreateBitCast(addr, Int8PtrPtrTy); |
| 1686 | |
| 1687 | llvm::CallInst *call = Builder.CreateCall(fn, addr); |
| 1688 | call->setDoesNotThrow(); |
| 1689 | } |
| 1690 | |
| 1691 | /// void @objc_moveWeak(i8** %dest, i8** %src) |
| 1692 | /// Disregards the current value in %dest. Leaves %src pointing to nothing. |
| 1693 | /// Essentially (objc_copyWeak(dest, src), objc_destroyWeak(src)). |
| 1694 | void CodeGenFunction::EmitARCMoveWeak(llvm::Value *dst, llvm::Value *src) { |
| 1695 | emitARCCopyOperation(*this, dst, src, |
| 1696 | CGM.getARCEntrypoints().objc_moveWeak, |
| 1697 | "objc_moveWeak"); |
| 1698 | } |
| 1699 | |
| 1700 | /// void @objc_copyWeak(i8** %dest, i8** %src) |
| 1701 | /// Disregards the current value in %dest. Essentially |
| 1702 | /// objc_release(objc_initWeak(dest, objc_readWeakRetained(src))) |
| 1703 | void CodeGenFunction::EmitARCCopyWeak(llvm::Value *dst, llvm::Value *src) { |
| 1704 | emitARCCopyOperation(*this, dst, src, |
| 1705 | CGM.getARCEntrypoints().objc_copyWeak, |
| 1706 | "objc_copyWeak"); |
| 1707 | } |
| 1708 | |
| 1709 | /// Produce the code to do a objc_autoreleasepool_push. |
| 1710 | /// call i8* @objc_autoreleasePoolPush(void) |
| 1711 | llvm::Value *CodeGenFunction::EmitObjCAutoreleasePoolPush() { |
| 1712 | llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPush; |
| 1713 | if (!fn) { |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1714 | llvm::FunctionType *fnType = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1715 | llvm::FunctionType::get(Int8PtrTy, false); |
| 1716 | fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPush"); |
| 1717 | } |
| 1718 | |
| 1719 | llvm::CallInst *call = Builder.CreateCall(fn); |
| 1720 | call->setDoesNotThrow(); |
| 1721 | |
| 1722 | return call; |
| 1723 | } |
| 1724 | |
| 1725 | /// Produce the code to do a primitive release. |
| 1726 | /// call void @objc_autoreleasePoolPop(i8* %ptr) |
| 1727 | void CodeGenFunction::EmitObjCAutoreleasePoolPop(llvm::Value *value) { |
| 1728 | assert(value->getType() == Int8PtrTy); |
| 1729 | |
| 1730 | llvm::Constant *&fn = CGM.getRREntrypoints().objc_autoreleasePoolPop; |
| 1731 | if (!fn) { |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 1732 | std::vector<llvm::Type*> args(1, Int8PtrTy); |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1733 | llvm::FunctionType *fnType = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1734 | llvm::FunctionType::get(Builder.getVoidTy(), args, false); |
| 1735 | |
| 1736 | // We don't want to use a weak import here; instead we should not |
| 1737 | // fall into this path. |
| 1738 | fn = createARCRuntimeFunction(CGM, fnType, "objc_autoreleasePoolPop"); |
| 1739 | } |
| 1740 | |
| 1741 | llvm::CallInst *call = Builder.CreateCall(fn, value); |
| 1742 | call->setDoesNotThrow(); |
| 1743 | } |
| 1744 | |
| 1745 | /// Produce the code to do an MRR version objc_autoreleasepool_push. |
| 1746 | /// Which is: [[NSAutoreleasePool alloc] init]; |
| 1747 | /// Where alloc is declared as: + (id) alloc; in NSAutoreleasePool class. |
| 1748 | /// init is declared as: - (id) init; in its NSObject super class. |
| 1749 | /// |
| 1750 | llvm::Value *CodeGenFunction::EmitObjCMRRAutoreleasePoolPush() { |
| 1751 | CGObjCRuntime &Runtime = CGM.getObjCRuntime(); |
| 1752 | llvm::Value *Receiver = Runtime.EmitNSAutoreleasePoolClassRef(Builder); |
| 1753 | // [NSAutoreleasePool alloc] |
| 1754 | IdentifierInfo *II = &CGM.getContext().Idents.get("alloc"); |
| 1755 | Selector AllocSel = getContext().Selectors.getSelector(0, &II); |
| 1756 | CallArgList Args; |
| 1757 | RValue AllocRV = |
| 1758 | Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
| 1759 | getContext().getObjCIdType(), |
| 1760 | AllocSel, Receiver, Args); |
| 1761 | |
| 1762 | // [Receiver init] |
| 1763 | Receiver = AllocRV.getScalarVal(); |
| 1764 | II = &CGM.getContext().Idents.get("init"); |
| 1765 | Selector InitSel = getContext().Selectors.getSelector(0, &II); |
| 1766 | RValue InitRV = |
| 1767 | Runtime.GenerateMessageSend(*this, ReturnValueSlot(), |
| 1768 | getContext().getObjCIdType(), |
| 1769 | InitSel, Receiver, Args); |
| 1770 | return InitRV.getScalarVal(); |
| 1771 | } |
| 1772 | |
| 1773 | /// Produce the code to do a primitive release. |
| 1774 | /// [tmp drain]; |
| 1775 | void CodeGenFunction::EmitObjCMRRAutoreleasePoolPop(llvm::Value *Arg) { |
| 1776 | IdentifierInfo *II = &CGM.getContext().Idents.get("drain"); |
| 1777 | Selector DrainSel = getContext().Selectors.getSelector(0, &II); |
| 1778 | CallArgList Args; |
| 1779 | CGM.getObjCRuntime().GenerateMessageSend(*this, ReturnValueSlot(), |
| 1780 | getContext().VoidTy, DrainSel, Arg, Args); |
| 1781 | } |
| 1782 | |
John McCall | bdc4d80 | 2011-07-09 01:37:26 +0000 | [diff] [blame] | 1783 | void CodeGenFunction::destroyARCStrongPrecise(CodeGenFunction &CGF, |
| 1784 | llvm::Value *addr, |
| 1785 | QualType type) { |
| 1786 | llvm::Value *ptr = CGF.Builder.CreateLoad(addr, "strongdestroy"); |
| 1787 | CGF.EmitARCRelease(ptr, /*precise*/ true); |
| 1788 | } |
| 1789 | |
| 1790 | void CodeGenFunction::destroyARCStrongImprecise(CodeGenFunction &CGF, |
| 1791 | llvm::Value *addr, |
| 1792 | QualType type) { |
| 1793 | llvm::Value *ptr = CGF.Builder.CreateLoad(addr, "strongdestroy"); |
| 1794 | CGF.EmitARCRelease(ptr, /*precise*/ false); |
| 1795 | } |
| 1796 | |
| 1797 | void CodeGenFunction::destroyARCWeak(CodeGenFunction &CGF, |
| 1798 | llvm::Value *addr, |
| 1799 | QualType type) { |
| 1800 | CGF.EmitARCDestroyWeak(addr); |
| 1801 | } |
| 1802 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1803 | namespace { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1804 | struct CallObjCAutoreleasePoolObject : EHScopeStack::Cleanup { |
| 1805 | llvm::Value *Token; |
| 1806 | |
| 1807 | CallObjCAutoreleasePoolObject(llvm::Value *token) : Token(token) {} |
| 1808 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1809 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1810 | CGF.EmitObjCAutoreleasePoolPop(Token); |
| 1811 | } |
| 1812 | }; |
| 1813 | struct CallObjCMRRAutoreleasePoolObject : EHScopeStack::Cleanup { |
| 1814 | llvm::Value *Token; |
| 1815 | |
| 1816 | CallObjCMRRAutoreleasePoolObject(llvm::Value *token) : Token(token) {} |
| 1817 | |
John McCall | ad346f4 | 2011-07-12 20:27:29 +0000 | [diff] [blame] | 1818 | void Emit(CodeGenFunction &CGF, Flags flags) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1819 | CGF.EmitObjCMRRAutoreleasePoolPop(Token); |
| 1820 | } |
| 1821 | }; |
| 1822 | } |
| 1823 | |
| 1824 | void CodeGenFunction::EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr) { |
| 1825 | if (CGM.getLangOptions().ObjCAutoRefCount) |
| 1826 | EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, Ptr); |
| 1827 | else |
| 1828 | EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, Ptr); |
| 1829 | } |
| 1830 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1831 | static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
| 1832 | LValue lvalue, |
| 1833 | QualType type) { |
| 1834 | switch (type.getObjCLifetime()) { |
| 1835 | case Qualifiers::OCL_None: |
| 1836 | case Qualifiers::OCL_ExplicitNone: |
| 1837 | case Qualifiers::OCL_Strong: |
| 1838 | case Qualifiers::OCL_Autoreleasing: |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1839 | return TryEmitResult(CGF.EmitLoadOfLValue(lvalue).getScalarVal(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1840 | false); |
| 1841 | |
| 1842 | case Qualifiers::OCL_Weak: |
| 1843 | return TryEmitResult(CGF.EmitARCLoadWeakRetained(lvalue.getAddress()), |
| 1844 | true); |
| 1845 | } |
| 1846 | |
| 1847 | llvm_unreachable("impossible lifetime!"); |
| 1848 | return TryEmitResult(); |
| 1849 | } |
| 1850 | |
| 1851 | static TryEmitResult tryEmitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
| 1852 | const Expr *e) { |
| 1853 | e = e->IgnoreParens(); |
| 1854 | QualType type = e->getType(); |
| 1855 | |
| 1856 | // As a very special optimization, in ARC++, if the l-value is the |
| 1857 | // result of a non-volatile assignment, do a simple retain of the |
| 1858 | // result of the call to objc_storeWeak instead of reloading. |
| 1859 | if (CGF.getLangOptions().CPlusPlus && |
| 1860 | !type.isVolatileQualified() && |
| 1861 | type.getObjCLifetime() == Qualifiers::OCL_Weak && |
| 1862 | isa<BinaryOperator>(e) && |
| 1863 | cast<BinaryOperator>(e)->getOpcode() == BO_Assign) |
| 1864 | return TryEmitResult(CGF.EmitScalarExpr(e), false); |
| 1865 | |
| 1866 | return tryEmitARCRetainLoadOfScalar(CGF, CGF.EmitLValue(e), type); |
| 1867 | } |
| 1868 | |
| 1869 | static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, |
| 1870 | llvm::Value *value); |
| 1871 | |
| 1872 | /// Given that the given expression is some sort of call (which does |
| 1873 | /// not return retained), emit a retain following it. |
| 1874 | static llvm::Value *emitARCRetainCall(CodeGenFunction &CGF, const Expr *e) { |
| 1875 | llvm::Value *value = CGF.EmitScalarExpr(e); |
| 1876 | return emitARCRetainAfterCall(CGF, value); |
| 1877 | } |
| 1878 | |
| 1879 | static llvm::Value *emitARCRetainAfterCall(CodeGenFunction &CGF, |
| 1880 | llvm::Value *value) { |
| 1881 | if (llvm::CallInst *call = dyn_cast<llvm::CallInst>(value)) { |
| 1882 | CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); |
| 1883 | |
| 1884 | // Place the retain immediately following the call. |
| 1885 | CGF.Builder.SetInsertPoint(call->getParent(), |
| 1886 | ++llvm::BasicBlock::iterator(call)); |
| 1887 | value = CGF.EmitARCRetainAutoreleasedReturnValue(value); |
| 1888 | |
| 1889 | CGF.Builder.restoreIP(ip); |
| 1890 | return value; |
| 1891 | } else if (llvm::InvokeInst *invoke = dyn_cast<llvm::InvokeInst>(value)) { |
| 1892 | CGBuilderTy::InsertPoint ip = CGF.Builder.saveIP(); |
| 1893 | |
| 1894 | // Place the retain at the beginning of the normal destination block. |
| 1895 | llvm::BasicBlock *BB = invoke->getNormalDest(); |
| 1896 | CGF.Builder.SetInsertPoint(BB, BB->begin()); |
| 1897 | value = CGF.EmitARCRetainAutoreleasedReturnValue(value); |
| 1898 | |
| 1899 | CGF.Builder.restoreIP(ip); |
| 1900 | return value; |
| 1901 | |
| 1902 | // Bitcasts can arise because of related-result returns. Rewrite |
| 1903 | // the operand. |
| 1904 | } else if (llvm::BitCastInst *bitcast = dyn_cast<llvm::BitCastInst>(value)) { |
| 1905 | llvm::Value *operand = bitcast->getOperand(0); |
| 1906 | operand = emitARCRetainAfterCall(CGF, operand); |
| 1907 | bitcast->setOperand(0, operand); |
| 1908 | return bitcast; |
| 1909 | |
| 1910 | // Generic fall-back case. |
| 1911 | } else { |
| 1912 | // Retain using the non-block variant: we never need to do a copy |
| 1913 | // of a block that's been returned to us. |
| 1914 | return CGF.EmitARCRetainNonBlock(value); |
| 1915 | } |
| 1916 | } |
| 1917 | |
| 1918 | static TryEmitResult |
| 1919 | tryEmitARCRetainScalarExpr(CodeGenFunction &CGF, const Expr *e) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1920 | // The desired result type, if it differs from the type of the |
| 1921 | // ultimate opaque expression. |
Chris Lattner | 2acc6e3 | 2011-07-18 04:24:23 +0000 | [diff] [blame] | 1922 | llvm::Type *resultType = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1923 | |
Douglas Gregor | d1bd98a | 2011-06-22 16:32:26 +0000 | [diff] [blame] | 1924 | // If we're loading retained from a __strong xvalue, we can avoid |
| 1925 | // an extra retain/release pair by zeroing out the source of this |
| 1926 | // "move" operation. |
John McCall | df7b091 | 2011-06-25 02:26:44 +0000 | [diff] [blame] | 1927 | if (e->isXValue() && !e->getType().isConstQualified() && |
Douglas Gregor | d1bd98a | 2011-06-22 16:32:26 +0000 | [diff] [blame] | 1928 | e->getType().getObjCLifetime() == Qualifiers::OCL_Strong) { |
| 1929 | // Emit the lvalue |
| 1930 | LValue lv = CGF.EmitLValue(e); |
| 1931 | |
| 1932 | // Load the object pointer and cast it to the appropriate type. |
| 1933 | QualType exprType = e->getType(); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1934 | llvm::Value *result = CGF.EmitLoadOfLValue(lv).getScalarVal(); |
Douglas Gregor | d1bd98a | 2011-06-22 16:32:26 +0000 | [diff] [blame] | 1935 | |
| 1936 | if (resultType) |
| 1937 | result = CGF.Builder.CreateBitCast(result, resultType); |
| 1938 | |
| 1939 | // Set the source pointer to NULL. |
| 1940 | llvm::Value *null |
| 1941 | = llvm::ConstantPointerNull::get( |
| 1942 | cast<llvm::PointerType>(CGF.ConvertType(exprType))); |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 1943 | CGF.EmitStoreOfScalar(null, lv); |
Douglas Gregor | d1bd98a | 2011-06-22 16:32:26 +0000 | [diff] [blame] | 1944 | |
| 1945 | return TryEmitResult(result, true); |
| 1946 | } |
| 1947 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1948 | while (true) { |
| 1949 | e = e->IgnoreParens(); |
| 1950 | |
| 1951 | // There's a break at the end of this if-chain; anything |
| 1952 | // that wants to keep looping has to explicitly continue. |
| 1953 | if (const CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 1954 | switch (ce->getCastKind()) { |
| 1955 | // No-op casts don't change the type, so we just ignore them. |
| 1956 | case CK_NoOp: |
| 1957 | e = ce->getSubExpr(); |
| 1958 | continue; |
| 1959 | |
| 1960 | case CK_LValueToRValue: { |
| 1961 | TryEmitResult loadResult |
| 1962 | = tryEmitARCRetainLoadOfScalar(CGF, ce->getSubExpr()); |
| 1963 | if (resultType) { |
| 1964 | llvm::Value *value = loadResult.getPointer(); |
| 1965 | value = CGF.Builder.CreateBitCast(value, resultType); |
| 1966 | loadResult.setPointer(value); |
| 1967 | } |
| 1968 | return loadResult; |
| 1969 | } |
| 1970 | |
| 1971 | // These casts can change the type, so remember that and |
| 1972 | // soldier on. We only need to remember the outermost such |
| 1973 | // cast, though. |
| 1974 | case CK_AnyPointerToObjCPointerCast: |
| 1975 | case CK_AnyPointerToBlockPointerCast: |
| 1976 | case CK_BitCast: |
| 1977 | if (!resultType) |
| 1978 | resultType = CGF.ConvertType(ce->getType()); |
| 1979 | e = ce->getSubExpr(); |
| 1980 | assert(e->getType()->hasPointerRepresentation()); |
| 1981 | continue; |
| 1982 | |
| 1983 | // For consumptions, just emit the subexpression and thus elide |
| 1984 | // the retain/release pair. |
| 1985 | case CK_ObjCConsumeObject: { |
| 1986 | llvm::Value *result = CGF.EmitScalarExpr(ce->getSubExpr()); |
| 1987 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); |
| 1988 | return TryEmitResult(result, true); |
| 1989 | } |
| 1990 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 1991 | // For reclaims, emit the subexpression as a retained call and |
| 1992 | // skip the consumption. |
| 1993 | case CK_ObjCReclaimReturnedObject: { |
| 1994 | llvm::Value *result = emitARCRetainCall(CGF, ce->getSubExpr()); |
| 1995 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); |
| 1996 | return TryEmitResult(result, true); |
| 1997 | } |
| 1998 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1999 | case CK_GetObjCProperty: { |
| 2000 | llvm::Value *result = emitARCRetainCall(CGF, ce); |
| 2001 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); |
| 2002 | return TryEmitResult(result, true); |
| 2003 | } |
| 2004 | |
| 2005 | default: |
| 2006 | break; |
| 2007 | } |
| 2008 | |
| 2009 | // Skip __extension__. |
| 2010 | } else if (const UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 2011 | if (op->getOpcode() == UO_Extension) { |
| 2012 | e = op->getSubExpr(); |
| 2013 | continue; |
| 2014 | } |
| 2015 | |
| 2016 | // For calls and message sends, use the retained-call logic. |
| 2017 | // Delegate inits are a special case in that they're the only |
| 2018 | // returns-retained expression that *isn't* surrounded by |
| 2019 | // a consume. |
| 2020 | } else if (isa<CallExpr>(e) || |
| 2021 | (isa<ObjCMessageExpr>(e) && |
| 2022 | !cast<ObjCMessageExpr>(e)->isDelegateInitCall())) { |
| 2023 | llvm::Value *result = emitARCRetainCall(CGF, e); |
| 2024 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); |
| 2025 | return TryEmitResult(result, true); |
| 2026 | } |
| 2027 | |
| 2028 | // Conservatively halt the search at any other expression kind. |
| 2029 | break; |
| 2030 | } |
| 2031 | |
| 2032 | // We didn't find an obvious production, so emit what we've got and |
| 2033 | // tell the caller that we didn't manage to retain. |
| 2034 | llvm::Value *result = CGF.EmitScalarExpr(e); |
| 2035 | if (resultType) result = CGF.Builder.CreateBitCast(result, resultType); |
| 2036 | return TryEmitResult(result, false); |
| 2037 | } |
| 2038 | |
| 2039 | static llvm::Value *emitARCRetainLoadOfScalar(CodeGenFunction &CGF, |
| 2040 | LValue lvalue, |
| 2041 | QualType type) { |
| 2042 | TryEmitResult result = tryEmitARCRetainLoadOfScalar(CGF, lvalue, type); |
| 2043 | llvm::Value *value = result.getPointer(); |
| 2044 | if (!result.getInt()) |
| 2045 | value = CGF.EmitARCRetain(type, value); |
| 2046 | return value; |
| 2047 | } |
| 2048 | |
| 2049 | /// EmitARCRetainScalarExpr - Semantically equivalent to |
| 2050 | /// EmitARCRetainObject(e->getType(), EmitScalarExpr(e)), but making a |
| 2051 | /// best-effort attempt to peephole expressions that naturally produce |
| 2052 | /// retained objects. |
| 2053 | llvm::Value *CodeGenFunction::EmitARCRetainScalarExpr(const Expr *e) { |
| 2054 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); |
| 2055 | llvm::Value *value = result.getPointer(); |
| 2056 | if (!result.getInt()) |
| 2057 | value = EmitARCRetain(e->getType(), value); |
| 2058 | return value; |
| 2059 | } |
| 2060 | |
| 2061 | llvm::Value * |
| 2062 | CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr(const Expr *e) { |
| 2063 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e); |
| 2064 | llvm::Value *value = result.getPointer(); |
| 2065 | if (result.getInt()) |
| 2066 | value = EmitARCAutorelease(value); |
| 2067 | else |
| 2068 | value = EmitARCRetainAutorelease(e->getType(), value); |
| 2069 | return value; |
| 2070 | } |
| 2071 | |
| 2072 | std::pair<LValue,llvm::Value*> |
| 2073 | CodeGenFunction::EmitARCStoreStrong(const BinaryOperator *e, |
| 2074 | bool ignored) { |
| 2075 | // Evaluate the RHS first. |
| 2076 | TryEmitResult result = tryEmitARCRetainScalarExpr(*this, e->getRHS()); |
| 2077 | llvm::Value *value = result.getPointer(); |
| 2078 | |
| 2079 | LValue lvalue = EmitLValue(e->getLHS()); |
| 2080 | |
| 2081 | // If the RHS was emitted retained, expand this. |
| 2082 | if (result.getInt()) { |
| 2083 | llvm::Value *oldValue = |
| 2084 | EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatileQualified(), |
| 2085 | lvalue.getAlignment(), e->getType(), |
| 2086 | lvalue.getTBAAInfo()); |
| 2087 | EmitStoreOfScalar(value, lvalue.getAddress(), |
| 2088 | lvalue.isVolatileQualified(), lvalue.getAlignment(), |
| 2089 | e->getType(), lvalue.getTBAAInfo()); |
| 2090 | EmitARCRelease(oldValue, /*precise*/ false); |
| 2091 | } else { |
John McCall | 545d996 | 2011-06-25 02:11:03 +0000 | [diff] [blame] | 2092 | value = EmitARCStoreStrong(lvalue, value, ignored); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | return std::pair<LValue,llvm::Value*>(lvalue, value); |
| 2096 | } |
| 2097 | |
| 2098 | std::pair<LValue,llvm::Value*> |
| 2099 | CodeGenFunction::EmitARCStoreAutoreleasing(const BinaryOperator *e) { |
| 2100 | llvm::Value *value = EmitARCRetainAutoreleaseScalarExpr(e->getRHS()); |
| 2101 | LValue lvalue = EmitLValue(e->getLHS()); |
| 2102 | |
| 2103 | EmitStoreOfScalar(value, lvalue.getAddress(), |
| 2104 | lvalue.isVolatileQualified(), lvalue.getAlignment(), |
| 2105 | e->getType(), lvalue.getTBAAInfo()); |
| 2106 | |
| 2107 | return std::pair<LValue,llvm::Value*>(lvalue, value); |
| 2108 | } |
| 2109 | |
| 2110 | void CodeGenFunction::EmitObjCAutoreleasePoolStmt( |
| 2111 | const ObjCAutoreleasePoolStmt &ARPS) { |
| 2112 | const Stmt *subStmt = ARPS.getSubStmt(); |
| 2113 | const CompoundStmt &S = cast<CompoundStmt>(*subStmt); |
| 2114 | |
| 2115 | CGDebugInfo *DI = getDebugInfo(); |
| 2116 | if (DI) { |
| 2117 | DI->setLocation(S.getLBracLoc()); |
| 2118 | DI->EmitRegionStart(Builder); |
| 2119 | } |
| 2120 | |
| 2121 | // Keep track of the current cleanup stack depth. |
| 2122 | RunCleanupsScope Scope(*this); |
John McCall | 9f084a3 | 2011-07-06 00:26:06 +0000 | [diff] [blame] | 2123 | if (CGM.getCodeGenOpts().ObjCRuntimeHasARC) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2124 | llvm::Value *token = EmitObjCAutoreleasePoolPush(); |
| 2125 | EHStack.pushCleanup<CallObjCAutoreleasePoolObject>(NormalCleanup, token); |
| 2126 | } else { |
| 2127 | llvm::Value *token = EmitObjCMRRAutoreleasePoolPush(); |
| 2128 | EHStack.pushCleanup<CallObjCMRRAutoreleasePoolObject>(NormalCleanup, token); |
| 2129 | } |
| 2130 | |
| 2131 | for (CompoundStmt::const_body_iterator I = S.body_begin(), |
| 2132 | E = S.body_end(); I != E; ++I) |
| 2133 | EmitStmt(*I); |
| 2134 | |
| 2135 | if (DI) { |
| 2136 | DI->setLocation(S.getRBracLoc()); |
| 2137 | DI->EmitRegionEnd(Builder); |
| 2138 | } |
| 2139 | } |
John McCall | 0c24c80 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2140 | |
| 2141 | /// EmitExtendGCLifetime - Given a pointer to an Objective-C object, |
| 2142 | /// make sure it survives garbage collection until this point. |
| 2143 | void CodeGenFunction::EmitExtendGCLifetime(llvm::Value *object) { |
| 2144 | // We just use an inline assembly. |
Chris Lattner | 9cbe4f0 | 2011-07-09 17:41:47 +0000 | [diff] [blame] | 2145 | llvm::Type *paramTypes[] = { VoidPtrTy }; |
John McCall | 0c24c80 | 2011-06-24 23:21:27 +0000 | [diff] [blame] | 2146 | llvm::FunctionType *extenderType |
| 2147 | = llvm::FunctionType::get(VoidTy, paramTypes, /*variadic*/ false); |
| 2148 | llvm::Value *extender |
| 2149 | = llvm::InlineAsm::get(extenderType, |
| 2150 | /* assembly */ "", |
| 2151 | /* constraints */ "r", |
| 2152 | /* side effects */ true); |
| 2153 | |
| 2154 | object = Builder.CreateBitCast(object, VoidPtrTy); |
| 2155 | Builder.CreateCall(extender, object)->setDoesNotThrow(); |
| 2156 | } |
| 2157 | |
Ted Kremenek | 2979ec7 | 2008-04-09 15:51:31 +0000 | [diff] [blame] | 2158 | CGObjCRuntime::~CGObjCRuntime() {} |