blob: 058278cfa4ea67040750921c4f23364719dcec78 [file] [log] [blame]
Anders Carlsson55085182007-08-21 17:43:55 +00001//===---- CGBuiltin.cpp - Emit LLVM Code for builtins ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Anders Carlsson55085182007-08-21 17:43:55 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This contains code to emit Objective-C code as LLVM code.
11//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek2979ec72008-04-09 15:51:31 +000014#include "CGObjCRuntime.h"
Anders Carlsson55085182007-08-21 17:43:55 +000015#include "CodeGenFunction.h"
16#include "CodeGenModule.h"
Daniel Dunbar85c59ed2008-08-29 08:11:39 +000017#include "clang/AST/ASTContext.h"
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000018#include "clang/AST/DeclObjC.h"
Daniel Dunbare66f4e32008-09-03 00:27:26 +000019#include "clang/Basic/Diagnostic.h"
Anders Carlsson3d8400d2008-08-30 19:51:14 +000020#include "llvm/ADT/STLExtras.h"
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +000021#include "llvm/Target/TargetData.h"
Chris Lattner41110242008-06-17 18:05:57 +000022
Anders Carlsson55085182007-08-21 17:43:55 +000023using namespace clang;
24using namespace CodeGen;
25
Chris Lattner8fdf3282008-06-24 17:04:18 +000026/// Emits an instance of NSConstantString representing the object.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000027llvm::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 Dunbared7c6182008-08-20 00:28:19 +000030 // FIXME: This bitcast should just be made an invariant on the Runtime.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031 return llvm::ConstantExpr::getBitCast(C, ConvertType(E->getType()));
Chris Lattner8fdf3282008-06-24 17:04:18 +000032}
33
34/// Emit a selector.
35llvm::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 Dunbar208ff5e2008-08-11 18:12:00 +000040 return CGM.getObjCRuntime().GetSelector(Builder, E->getSelector());
Chris Lattner8fdf3282008-06-24 17:04:18 +000041}
42
Daniel Dunbared7c6182008-08-20 00:28:19 +000043llvm::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 Lattner8fdf3282008-06-24 17:04:18 +000047
48
Daniel Dunbar8f2926b2008-08-23 03:46:30 +000049RValue CodeGenFunction::EmitObjCMessageExpr(const ObjCMessageExpr *E) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000050 // 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 Dunbar208ff5e2008-08-11 18:12:00 +000054 CGObjCRuntime &Runtime = CGM.getObjCRuntime();
Chris Lattner8fdf3282008-06-24 17:04:18 +000055 const Expr *ReceiverExpr = E->getReceiver();
56 bool isSuperMessage = false;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000057 bool isClassMessage = false;
Chris Lattner8fdf3282008-06-24 17:04:18 +000058 // Find the receiver
59 llvm::Value *Receiver;
60 if (!ReceiverExpr) {
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000061 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) {
66 assert(!strcmp(E->getClassName()->getName(), "super") &&
67 "Unexpected missing class interface in message send.");
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +000068 isSuperMessage = true;
Daniel Dunbarf56f1912008-08-25 08:19:24 +000069 Receiver = LoadObjCSelf();
70 } else {
71 Receiver = Runtime.GetClass(Builder, OID);
Chris Lattner8fdf3282008-06-24 17:04:18 +000072 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +000073
74 isClassMessage = true;
Douglas Gregorcd9b46e2008-11-04 14:56:14 +000075 } else if (isa<ObjCSuperExpr>(E->getReceiver())) {
Chris Lattner8fdf3282008-06-24 17:04:18 +000076 isSuperMessage = true;
77 Receiver = LoadObjCSelf();
78 } else {
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000079 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner8fdf3282008-06-24 17:04:18 +000080 }
81
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000082 CallArgList Args;
83 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
84 i != e; ++i)
Daniel Dunbar46f45b92008-09-09 01:06:48 +000085 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType()));
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000086
Chris Lattner8fdf3282008-06-24 17:04:18 +000087 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000088 // super is only valid in an Objective-C method
89 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000090 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
91 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000092 OMD->getClassInterface(),
93 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000094 isClassMessage,
95 Args);
Chris Lattner8fdf3282008-06-24 17:04:18 +000096 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000097 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
98 Receiver, isClassMessage, Args);
Anders Carlsson55085182007-08-21 17:43:55 +000099}
100
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000101/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
102/// the LLVM function and sets the other context used by
103/// CodeGenFunction.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000104void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000105 FunctionArgList Args;
106 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000107
Daniel Dunbar7c086512008-09-09 23:14:03 +0000108 CGM.SetMethodAttributes(OMD, Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000109
Daniel Dunbar7c086512008-09-09 23:14:03 +0000110 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 Lattner41110242008-06-17 18:05:57 +0000114
Daniel Dunbar7c086512008-09-09 23:14:03 +0000115 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 Lattner41110242008-06-17 18:05:57 +0000118 }
Chris Lattner41110242008-06-17 18:05:57 +0000119
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000120 StartFunction(OMD, OMD->getResultType(), Fn, Args, OMD->getLocEnd());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000121}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000122
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000123/// 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.
125void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
126 StartObjCMethod(OMD);
127 EmitStmt(OMD->getBody());
Daniel Dunbar2284ac92008-10-18 18:22:23 +0000128 FinishFunction(cast<CompoundStmt>(OMD->getBody())->getRBracLoc());
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000129}
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.
138void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000139 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000140 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 Dunbarc1cf4a52008-09-24 04:04:31 +0000148 // 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 Dunbaraf05bb92008-08-26 08:29:31 +0000193
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.
200void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000201 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000202 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 Dunbaraf05bb92008-08-26 08:29:31 +0000209
Daniel Dunbar86957eb2008-09-24 06:32:09 +0000210 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 Dunbaraf05bb92008-08-26 08:29:31 +0000272
273 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000274}
275
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000276llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000277 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
278 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000279}
280
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000281RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
282 // Determine getter selector.
283 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000284 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
285 S = E->getGetterMethod()->getSelector();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000286 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000287 S = E->getProperty()->getGetterName();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000288 }
289
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000290 return CGM.getObjCRuntime().
291 GenerateMessageSend(*this, E->getType(), S,
292 EmitScalarExpr(E->getBase()),
293 false, CallArgList());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000294}
295
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000296void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
297 RValue Src) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000298 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000299 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.
306 SourceRange Range = E->getSourceRange();
307 CGM.getDiags().Report(getContext().getFullLoc(E->getLocStart()),
308 diag::err_typecheck_assign_const, 0, 0,
309 &Range, 1);
310 return;
311 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000312 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000313 S = E->getProperty()->getSetterName();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000314 }
315
316 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000317 Args.push_back(std::make_pair(Src, E->getType()));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000318 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
319 EmitScalarExpr(E->getBase()),
320 false, Args);
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000321}
322
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000323void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
324{
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000325 llvm::Function *EnumerationMutationFn =
326 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000327 llvm::Value *DeclAddress;
328 QualType ElementTy;
329
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000330 if (!EnumerationMutationFn) {
331 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
332 return;
333 }
334
Anders Carlssonf484c312008-08-31 02:33:12 +0000335 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
336 EmitStmt(SD);
Daniel Dunbara448fb22008-11-11 23:11:34 +0000337 assert(HaveInsertPoint() && "DeclStmt destroyed insert point!");
Ted Kremenek39741ce2008-10-06 20:59:48 +0000338 const ScopedDecl* D = SD->getSolitaryDecl();
339 ElementTy = cast<ValueDecl>(D)->getType();
340 DeclAddress = LocalDeclMap[D];
Anders Carlssonf484c312008-08-31 02:33:12 +0000341 } else {
342 ElementTy = cast<Expr>(S.getElement())->getType();
343 DeclAddress = 0;
344 }
345
346 // Fast enumeration state.
347 QualType StateTy = getContext().getObjCFastEnumerationStateType();
348 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
349 "state.ptr");
350 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000351 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000352
353 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000354 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000355
356 // Get selector
357 llvm::SmallVector<IdentifierInfo*, 3> II;
358 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
359 II.push_back(&CGM.getContext().Idents.get("objects"));
360 II.push_back(&CGM.getContext().Idents.get("count"));
361 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
362 &II[0]);
363
364 QualType ItemsTy =
365 getContext().getConstantArrayType(getContext().getObjCIdType(),
366 llvm::APInt(32, NumItems),
367 ArrayType::Normal, 0);
368 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
369
370 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
371
372 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000373 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000374 getContext().getPointerType(StateTy)));
375
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000376 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000377 getContext().getPointerType(ItemsTy)));
378
379 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
380 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000381 Args.push_back(std::make_pair(RValue::get(Count),
382 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000383
384 RValue CountRV =
385 CGM.getObjCRuntime().GenerateMessageSend(*this,
386 getContext().UnsignedLongTy,
387 FastEnumSel,
388 Collection, false, Args);
389
390 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
391 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
392
Daniel Dunbar55e87422008-11-11 02:29:29 +0000393 llvm::BasicBlock *NoElements = createBasicBlock("noelements");
394 llvm::BasicBlock *SetStartMutations = createBasicBlock("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000395
396 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
397 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
398
399 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000400 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000401
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000402 EmitBlock(SetStartMutations);
403
404 llvm::Value *StartMutationsPtr =
405 CreateTempAlloca(UnsignedLongLTy);
406
407 llvm::Value *StateMutationsPtrPtr =
408 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
409 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
410 "mutationsptr");
411
412 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
413 "mutations");
414
415 Builder.CreateStore(StateMutations, StartMutationsPtr);
416
Daniel Dunbar55e87422008-11-11 02:29:29 +0000417 llvm::BasicBlock *LoopStart = createBasicBlock("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000418 EmitBlock(LoopStart);
419
Anders Carlssonf484c312008-08-31 02:33:12 +0000420 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
421 Builder.CreateStore(Zero, CounterPtr);
422
Daniel Dunbar55e87422008-11-11 02:29:29 +0000423 llvm::BasicBlock *LoopBody = createBasicBlock("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000424 EmitBlock(LoopBody);
425
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000426 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
427 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
428
429 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
430 "mutations");
431 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
432 StartMutations,
433 "tobool");
434
435
Daniel Dunbar55e87422008-11-11 02:29:29 +0000436 llvm::BasicBlock *WasMutated = createBasicBlock("wasmutated");
437 llvm::BasicBlock *WasNotMutated = createBasicBlock("wasnotmutated");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000438
439 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
440
441 EmitBlock(WasMutated);
442 llvm::Value *V =
443 Builder.CreateBitCast(Collection,
444 ConvertType(getContext().getObjCIdType()),
445 "tmp");
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000446 Builder.CreateCall(EnumerationMutationFn, V);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000447
448 EmitBlock(WasNotMutated);
449
Anders Carlssonf484c312008-08-31 02:33:12 +0000450 llvm::Value *StateItemsPtr =
451 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
452
453 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
454
455 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
456 "stateitems");
457
458 llvm::Value *CurrentItemPtr =
459 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
460
461 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
462
463 // Cast the item to the right type.
464 CurrentItem = Builder.CreateBitCast(CurrentItem,
465 ConvertType(ElementTy), "tmp");
466
467 if (!DeclAddress) {
468 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
469
470 // Set the value to null.
471 Builder.CreateStore(CurrentItem, LV.getAddress());
472 } else
473 Builder.CreateStore(CurrentItem, DeclAddress);
474
475 // Increment the counter.
476 Counter = Builder.CreateAdd(Counter,
477 llvm::ConstantInt::get(UnsignedLongLTy, 1));
478 Builder.CreateStore(Counter, CounterPtr);
479
Daniel Dunbar55e87422008-11-11 02:29:29 +0000480 llvm::BasicBlock *LoopEnd = createBasicBlock("loopend");
481 llvm::BasicBlock *AfterBody = createBasicBlock("afterbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000482
483 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
484
485 EmitStmt(S.getBody());
486
487 BreakContinueStack.pop_back();
488
489 EmitBlock(AfterBody);
490
Daniel Dunbar55e87422008-11-11 02:29:29 +0000491 llvm::BasicBlock *FetchMore = createBasicBlock("fetchmore");
Anders Carlssonf484c312008-08-31 02:33:12 +0000492
493 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000494 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000495
496 // Fetch more elements.
497 EmitBlock(FetchMore);
498
499 CountRV =
500 CGM.getObjCRuntime().GenerateMessageSend(*this,
501 getContext().UnsignedLongTy,
502 FastEnumSel,
503 Collection, false, Args);
504 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
505 Limit = Builder.CreateLoad(LimitPtr);
506
507 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
508 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
509
510 // No more elements.
511 EmitBlock(NoElements);
512
513 if (!DeclAddress) {
514 // If the element was not a declaration, set it to be null.
515
516 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
517
518 // Set the value to null.
519 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
520 LV.getAddress());
521 }
522
523 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000524}
525
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000526void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
527{
528 CGM.getObjCRuntime().EmitTryStmt(*this, S);
529}
530
531void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
532{
533 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
534}
535
Chris Lattner10cac6f2008-11-15 21:26:17 +0000536void CodeGenFunction::EmitObjCAtSynchronizedStmt(
537 const ObjCAtSynchronizedStmt &S)
538{
539 CGM.getObjCRuntime().EmitSynchronizedStmt(*this, S);
540}
541
Ted Kremenek2979ec72008-04-09 15:51:31 +0000542CGObjCRuntime::~CGObjCRuntime() {}