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