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