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