blob: 336d661001a3359e85f91007aa92f3b7469bf6c4 [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;
Chris Lattnerd9f69102008-08-10 01:53:14 +000075 } else if (const PredefinedExpr *PDE =
76 dyn_cast<PredefinedExpr>(E->getReceiver())) {
77 assert(PDE->getIdentType() == PredefinedExpr::ObjCSuper);
Chris Lattner8fdf3282008-06-24 17:04:18 +000078 isSuperMessage = true;
79 Receiver = LoadObjCSelf();
80 } else {
Daniel Dunbar2bedbf82008-08-12 05:28:47 +000081 Receiver = EmitScalarExpr(E->getReceiver());
Chris Lattner8fdf3282008-06-24 17:04:18 +000082 }
83
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000084 CallArgList Args;
85 for (CallExpr::const_arg_iterator i = E->arg_begin(), e = E->arg_end();
86 i != e; ++i)
Daniel Dunbar46f45b92008-09-09 01:06:48 +000087 Args.push_back(std::make_pair(EmitAnyExprToTemp(*i), (*i)->getType()));
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000088
Chris Lattner8fdf3282008-06-24 17:04:18 +000089 if (isSuperMessage) {
Chris Lattner9384c762008-06-26 04:42:20 +000090 // super is only valid in an Objective-C method
91 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000092 return Runtime.GenerateMessageSendSuper(*this, E->getType(),
93 E->getSelector(),
Daniel Dunbarf56f1912008-08-25 08:19:24 +000094 OMD->getClassInterface(),
95 Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +000096 isClassMessage,
97 Args);
Chris Lattner8fdf3282008-06-24 17:04:18 +000098 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +000099 return Runtime.GenerateMessageSend(*this, E->getType(), E->getSelector(),
100 Receiver, isClassMessage, Args);
Anders Carlsson55085182007-08-21 17:43:55 +0000101}
102
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000103/// StartObjCMethod - Begin emission of an ObjCMethod. This generates
104/// the LLVM function and sets the other context used by
105/// CodeGenFunction.
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000106void CodeGenFunction::StartObjCMethod(const ObjCMethodDecl *OMD) {
Daniel Dunbar7c086512008-09-09 23:14:03 +0000107 FunctionArgList Args;
108 llvm::Function *Fn = CGM.getObjCRuntime().GenerateMethod(OMD);
Daniel Dunbarf80519b2008-09-04 23:41:35 +0000109
Daniel Dunbar7c086512008-09-09 23:14:03 +0000110 CGM.SetMethodAttributes(OMD, Fn);
Chris Lattner41110242008-06-17 18:05:57 +0000111
Daniel Dunbar7c086512008-09-09 23:14:03 +0000112 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 Lattner41110242008-06-17 18:05:57 +0000116
Daniel Dunbar7c086512008-09-09 23:14:03 +0000117 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 Lattner41110242008-06-17 18:05:57 +0000120 }
Chris Lattner41110242008-06-17 18:05:57 +0000121
Daniel Dunbar7c086512008-09-09 23:14:03 +0000122 StartFunction(OMD, OMD->getResultType(), Fn, Args);
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000123}
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000124
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000125/// 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.
127void CodeGenFunction::GenerateObjCMethod(const ObjCMethodDecl *OMD) {
128 StartObjCMethod(OMD);
129 EmitStmt(OMD->getBody());
130
131 const CompoundStmt *S = dyn_cast<CompoundStmt>(OMD->getBody());
132 if (S) {
133 FinishFunction(S->getRBracLoc());
134 } else {
135 FinishFunction();
136 }
137}
138
139// FIXME: I wasn't sure about the synthesis approach. If we end up
140// generating an AST for the whole body we can just fall back to
141// having a GenerateFunction which takes the body Stmt.
142
143/// GenerateObjCGetter - Generate an Objective-C property getter
144/// function. The given Decl must be either an ObjCCategoryImplDecl
145/// or an ObjCImplementationDecl.
146void CodeGenFunction::GenerateObjCGetter(const ObjCPropertyImplDecl *PID) {
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000147 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000148 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
149 ObjCMethodDecl *OMD = PD->getGetterMethodDecl();
150 assert(OMD && "Invalid call to generate getter (empty method)");
151 // FIXME: This is rather murky, we create this here since they will
152 // not have been created by Sema for us.
153 OMD->createImplicitParams(getContext());
154 StartObjCMethod(OMD);
155
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000156 // Determine if we should use an objc_getProperty call for
157 // this. Non-atomic and properties with assign semantics are
158 // directly evaluated, and in gc-only mode we don't need it at all.
159 if (CGM.getLangOptions().getGCMode() != LangOptions::GCOnly &&
160 PD->getSetterKind() != ObjCPropertyDecl::Assign &&
161 !(PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)) {
162 llvm::Value *GetPropertyFn =
163 CGM.getObjCRuntime().GetPropertyGetFunction();
164
165 if (!GetPropertyFn) {
166 CGM.ErrorUnsupported(PID, "Obj-C getter requiring atomic copy");
167 FinishFunction();
168 return;
169 }
170
171 // Return (ivar-type) objc_getProperty((id) self, _cmd, offset, true).
172 // FIXME: Can't this be simpler? This might even be worse than the
173 // corresponding gcc code.
174 CodeGenTypes &Types = CGM.getTypes();
175 ValueDecl *Cmd = OMD->getCmdDecl();
176 llvm::Value *CmdVal = Builder.CreateLoad(LocalDeclMap[Cmd], "cmd");
177 QualType IdTy = getContext().getObjCIdType();
178 llvm::Value *SelfAsId =
179 Builder.CreateBitCast(LoadObjCSelf(), Types.ConvertType(IdTy));
180 llvm::Value *Offset = EmitIvarOffset(OMD->getClassInterface(), Ivar);
181 llvm::Value *True =
182 llvm::ConstantInt::get(Types.ConvertTypeForMem(getContext().BoolTy), 1);
183 CallArgList Args;
184 Args.push_back(std::make_pair(RValue::get(SelfAsId), IdTy));
185 Args.push_back(std::make_pair(RValue::get(CmdVal), Cmd->getType()));
186 Args.push_back(std::make_pair(RValue::get(Offset), getContext().LongTy));
187 Args.push_back(std::make_pair(RValue::get(True), getContext().BoolTy));
188 RValue RV = EmitCall(GetPropertyFn, PD->getType(), Args);
189 // We need to fix the type here. Ivars with copy & retain are
190 // always objects so we don't need to worry about complex or
191 // aggregates.
192 RV = RValue::get(Builder.CreateBitCast(RV.getScalarVal(),
193 Types.ConvertType(PD->getType())));
194 EmitReturnOfRValue(RV, PD->getType());
195 } else {
196 EmitReturnOfRValue(EmitLoadOfLValue(EmitLValueForIvar(LoadObjCSelf(),
197 Ivar, 0),
198 Ivar->getType()),
199 PD->getType());
200 }
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000201
202 FinishFunction();
203}
204
205/// GenerateObjCSetter - Generate an Objective-C property setter
206/// function. The given Decl must be either an ObjCCategoryImplDecl
207/// or an ObjCImplementationDecl.
208void CodeGenFunction::GenerateObjCSetter(const ObjCPropertyImplDecl *PID) {
209 const ObjCPropertyDecl *PD = PID->getPropertyDecl();
210 ObjCMethodDecl *OMD = PD->getSetterMethodDecl();
211 assert(OMD && "Invalid call to generate setter (empty method)");
212 // FIXME: This is rather murky, we create this here since they will
213 // not have been created by Sema for us.
214 OMD->createImplicitParams(getContext());
215 StartObjCMethod(OMD);
216
217 switch (PD->getSetterKind()) {
218 case ObjCPropertyDecl::Assign: break;
219 case ObjCPropertyDecl::Copy:
220 CGM.ErrorUnsupported(PID, "Obj-C setter with 'copy'");
221 break;
222 case ObjCPropertyDecl::Retain:
223 CGM.ErrorUnsupported(PID, "Obj-C setter with 'retain'");
224 break;
225 }
226
227 // FIXME: What about nonatomic?
228 SourceLocation Loc = PD->getLocation();
229 ValueDecl *Self = OMD->getSelfDecl();
230 ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
231 DeclRefExpr Base(Self, Self->getType(), Loc);
232 ParmVarDecl *ArgDecl = OMD->getParamDecl(0);
233 DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
234 ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
235 true, true);
236 BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
237 Ivar->getType(), Loc);
238 EmitStmt(&Assign);
239
240 FinishFunction();
Chris Lattner41110242008-06-17 18:05:57 +0000241}
242
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000243llvm::Value *CodeGenFunction::LoadObjCSelf() {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +0000244 const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
245 return Builder.CreateLoad(LocalDeclMap[OMD->getSelfDecl()], "self");
Chris Lattner41110242008-06-17 18:05:57 +0000246}
247
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000248RValue CodeGenFunction::EmitObjCPropertyGet(const ObjCPropertyRefExpr *E) {
249 // Determine getter selector.
250 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000251 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
252 S = E->getGetterMethod()->getSelector();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000253 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000254 S = E->getProperty()->getGetterName();
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000255 }
256
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000257 return CGM.getObjCRuntime().
258 GenerateMessageSend(*this, E->getType(), S,
259 EmitScalarExpr(E->getBase()),
260 false, CallArgList());
Daniel Dunbar9c3fc702008-08-27 06:57:25 +0000261}
262
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000263void CodeGenFunction::EmitObjCPropertySet(const ObjCPropertyRefExpr *E,
264 RValue Src) {
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000265 Selector S;
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000266 if (E->getKind() == ObjCPropertyRefExpr::MethodRef) {
267 ObjCMethodDecl *Setter = E->getSetterMethod();
268
269 if (Setter) {
270 S = Setter->getSelector();
271 } else {
272 // FIXME: This should be diagnosed by sema.
273 SourceRange Range = E->getSourceRange();
274 CGM.getDiags().Report(getContext().getFullLoc(E->getLocStart()),
275 diag::err_typecheck_assign_const, 0, 0,
276 &Range, 1);
277 return;
278 }
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000279 } else {
Daniel Dunbare66f4e32008-09-03 00:27:26 +0000280 S = E->getProperty()->getSetterName();
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000281 }
282
283 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000284 Args.push_back(std::make_pair(Src, E->getType()));
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000285 CGM.getObjCRuntime().GenerateMessageSend(*this, getContext().VoidTy, S,
286 EmitScalarExpr(E->getBase()),
287 false, Args);
Daniel Dunbar85c59ed2008-08-29 08:11:39 +0000288}
289
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000290void CodeGenFunction::EmitObjCForCollectionStmt(const ObjCForCollectionStmt &S)
291{
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000292 llvm::Function *EnumerationMutationFn =
293 CGM.getObjCRuntime().EnumerationMutationFunction();
Anders Carlssonf484c312008-08-31 02:33:12 +0000294 llvm::Value *DeclAddress;
295 QualType ElementTy;
296
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000297 if (!EnumerationMutationFn) {
298 CGM.ErrorUnsupported(&S, "Obj-C fast enumeration for this runtime");
299 return;
300 }
301
Anders Carlssonf484c312008-08-31 02:33:12 +0000302 if (const DeclStmt *SD = dyn_cast<DeclStmt>(S.getElement())) {
303 EmitStmt(SD);
304
305 ElementTy = cast<ValueDecl>(SD->getDecl())->getType();
306 DeclAddress = LocalDeclMap[SD->getDecl()];
307 } else {
308 ElementTy = cast<Expr>(S.getElement())->getType();
309 DeclAddress = 0;
310 }
311
312 // Fast enumeration state.
313 QualType StateTy = getContext().getObjCFastEnumerationStateType();
314 llvm::AllocaInst *StatePtr = CreateTempAlloca(ConvertType(StateTy),
315 "state.ptr");
316 StatePtr->setAlignment(getContext().getTypeAlign(StateTy) >> 3);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000317 EmitMemSetToZero(StatePtr, StateTy);
Anders Carlssonf484c312008-08-31 02:33:12 +0000318
319 // Number of elements in the items array.
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000320 static const unsigned NumItems = 16;
Anders Carlssonf484c312008-08-31 02:33:12 +0000321
322 // Get selector
323 llvm::SmallVector<IdentifierInfo*, 3> II;
324 II.push_back(&CGM.getContext().Idents.get("countByEnumeratingWithState"));
325 II.push_back(&CGM.getContext().Idents.get("objects"));
326 II.push_back(&CGM.getContext().Idents.get("count"));
327 Selector FastEnumSel = CGM.getContext().Selectors.getSelector(II.size(),
328 &II[0]);
329
330 QualType ItemsTy =
331 getContext().getConstantArrayType(getContext().getObjCIdType(),
332 llvm::APInt(32, NumItems),
333 ArrayType::Normal, 0);
334 llvm::Value *ItemsPtr = CreateTempAlloca(ConvertType(ItemsTy), "items.ptr");
335
336 llvm::Value *Collection = EmitScalarExpr(S.getCollection());
337
338 CallArgList Args;
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000339 Args.push_back(std::make_pair(RValue::get(StatePtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000340 getContext().getPointerType(StateTy)));
341
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000342 Args.push_back(std::make_pair(RValue::get(ItemsPtr),
Anders Carlssonf484c312008-08-31 02:33:12 +0000343 getContext().getPointerType(ItemsTy)));
344
345 const llvm::Type *UnsignedLongLTy = ConvertType(getContext().UnsignedLongTy);
346 llvm::Constant *Count = llvm::ConstantInt::get(UnsignedLongLTy, NumItems);
Daniel Dunbar46f45b92008-09-09 01:06:48 +0000347 Args.push_back(std::make_pair(RValue::get(Count),
348 getContext().UnsignedLongTy));
Anders Carlssonf484c312008-08-31 02:33:12 +0000349
350 RValue CountRV =
351 CGM.getObjCRuntime().GenerateMessageSend(*this,
352 getContext().UnsignedLongTy,
353 FastEnumSel,
354 Collection, false, Args);
355
356 llvm::Value *LimitPtr = CreateTempAlloca(UnsignedLongLTy, "limit.ptr");
357 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
358
359 llvm::BasicBlock *NoElements = llvm::BasicBlock::Create("noelements");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000360 llvm::BasicBlock *SetStartMutations =
361 llvm::BasicBlock::Create("setstartmutations");
Anders Carlssonf484c312008-08-31 02:33:12 +0000362
363 llvm::Value *Limit = Builder.CreateLoad(LimitPtr);
364 llvm::Value *Zero = llvm::Constant::getNullValue(UnsignedLongLTy);
365
366 llvm::Value *IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000367 Builder.CreateCondBr(IsZero, NoElements, SetStartMutations);
Anders Carlssonf484c312008-08-31 02:33:12 +0000368
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000369 EmitBlock(SetStartMutations);
370
371 llvm::Value *StartMutationsPtr =
372 CreateTempAlloca(UnsignedLongLTy);
373
374 llvm::Value *StateMutationsPtrPtr =
375 Builder.CreateStructGEP(StatePtr, 2, "mutationsptr.ptr");
376 llvm::Value *StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr,
377 "mutationsptr");
378
379 llvm::Value *StateMutations = Builder.CreateLoad(StateMutationsPtr,
380 "mutations");
381
382 Builder.CreateStore(StateMutations, StartMutationsPtr);
383
384 llvm::BasicBlock *LoopStart = llvm::BasicBlock::Create("loopstart");
Anders Carlssonf484c312008-08-31 02:33:12 +0000385 EmitBlock(LoopStart);
386
Anders Carlssonf484c312008-08-31 02:33:12 +0000387 llvm::Value *CounterPtr = CreateTempAlloca(UnsignedLongLTy, "counter.ptr");
388 Builder.CreateStore(Zero, CounterPtr);
389
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000390 llvm::BasicBlock *LoopBody = llvm::BasicBlock::Create("loopbody");
Anders Carlssonf484c312008-08-31 02:33:12 +0000391 EmitBlock(LoopBody);
392
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000393 StateMutationsPtr = Builder.CreateLoad(StateMutationsPtrPtr, "mutationsptr");
394 StateMutations = Builder.CreateLoad(StateMutationsPtr, "statemutations");
395
396 llvm::Value *StartMutations = Builder.CreateLoad(StartMutationsPtr,
397 "mutations");
398 llvm::Value *MutationsEqual = Builder.CreateICmpEQ(StateMutations,
399 StartMutations,
400 "tobool");
401
402
403 llvm::BasicBlock *WasMutated = llvm::BasicBlock::Create("wasmutated");
404 llvm::BasicBlock *WasNotMutated = llvm::BasicBlock::Create("wasnotmutated");
405
406 Builder.CreateCondBr(MutationsEqual, WasNotMutated, WasMutated);
407
408 EmitBlock(WasMutated);
409 llvm::Value *V =
410 Builder.CreateBitCast(Collection,
411 ConvertType(getContext().getObjCIdType()),
412 "tmp");
Daniel Dunbarc1cf4a52008-09-24 04:04:31 +0000413 Builder.CreateCall(EnumerationMutationFn, V);
Anders Carlsson2abd89c2008-08-31 04:05:03 +0000414
415 EmitBlock(WasNotMutated);
416
Anders Carlssonf484c312008-08-31 02:33:12 +0000417 llvm::Value *StateItemsPtr =
418 Builder.CreateStructGEP(StatePtr, 1, "stateitems.ptr");
419
420 llvm::Value *Counter = Builder.CreateLoad(CounterPtr, "counter");
421
422 llvm::Value *EnumStateItems = Builder.CreateLoad(StateItemsPtr,
423 "stateitems");
424
425 llvm::Value *CurrentItemPtr =
426 Builder.CreateGEP(EnumStateItems, Counter, "currentitem.ptr");
427
428 llvm::Value *CurrentItem = Builder.CreateLoad(CurrentItemPtr, "currentitem");
429
430 // Cast the item to the right type.
431 CurrentItem = Builder.CreateBitCast(CurrentItem,
432 ConvertType(ElementTy), "tmp");
433
434 if (!DeclAddress) {
435 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
436
437 // Set the value to null.
438 Builder.CreateStore(CurrentItem, LV.getAddress());
439 } else
440 Builder.CreateStore(CurrentItem, DeclAddress);
441
442 // Increment the counter.
443 Counter = Builder.CreateAdd(Counter,
444 llvm::ConstantInt::get(UnsignedLongLTy, 1));
445 Builder.CreateStore(Counter, CounterPtr);
446
447 llvm::BasicBlock *LoopEnd = llvm::BasicBlock::Create("loopend");
448 llvm::BasicBlock *AfterBody = llvm::BasicBlock::Create("afterbody");
449
450 BreakContinueStack.push_back(BreakContinue(LoopEnd, AfterBody));
451
452 EmitStmt(S.getBody());
453
454 BreakContinueStack.pop_back();
455
456 EmitBlock(AfterBody);
457
458 llvm::BasicBlock *FetchMore = llvm::BasicBlock::Create("fetchmore");
459
460 llvm::Value *IsLess = Builder.CreateICmpULT(Counter, Limit, "isless");
Daniel Dunbarfe2b2c02008-09-04 21:54:37 +0000461 Builder.CreateCondBr(IsLess, LoopBody, FetchMore);
Anders Carlssonf484c312008-08-31 02:33:12 +0000462
463 // Fetch more elements.
464 EmitBlock(FetchMore);
465
466 CountRV =
467 CGM.getObjCRuntime().GenerateMessageSend(*this,
468 getContext().UnsignedLongTy,
469 FastEnumSel,
470 Collection, false, Args);
471 Builder.CreateStore(CountRV.getScalarVal(), LimitPtr);
472 Limit = Builder.CreateLoad(LimitPtr);
473
474 IsZero = Builder.CreateICmpEQ(Limit, Zero, "iszero");
475 Builder.CreateCondBr(IsZero, NoElements, LoopStart);
476
477 // No more elements.
478 EmitBlock(NoElements);
479
480 if (!DeclAddress) {
481 // If the element was not a declaration, set it to be null.
482
483 LValue LV = EmitLValue(cast<Expr>(S.getElement()));
484
485 // Set the value to null.
486 Builder.CreateStore(llvm::Constant::getNullValue(ConvertType(ElementTy)),
487 LV.getAddress());
488 }
489
490 EmitBlock(LoopEnd);
Anders Carlsson3d8400d2008-08-30 19:51:14 +0000491}
492
Anders Carlsson64d5d6c2008-09-09 10:04:29 +0000493void CodeGenFunction::EmitObjCAtTryStmt(const ObjCAtTryStmt &S)
494{
495 CGM.getObjCRuntime().EmitTryStmt(*this, S);
496}
497
498void CodeGenFunction::EmitObjCAtThrowStmt(const ObjCAtThrowStmt &S)
499{
500 CGM.getObjCRuntime().EmitThrowStmt(*this, S);
501}
502
Ted Kremenek2979ec72008-04-09 15:51:31 +0000503CGObjCRuntime::~CGObjCRuntime() {}