blob: b9e849559679654dd51f2d2700d5afa682ca319b [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides Objective-C code generation targetting the Apple runtime.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
16#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000017#include "CodeGenFunction.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000018#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000019#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000020#include "clang/AST/DeclObjC.h"
Daniel Dunbar2bebbf02009-05-03 10:46:44 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000022#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000023#include "clang/Basic/LangOptions.h"
24
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000025#include "llvm/Intrinsics.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000026#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000027#include "llvm/ADT/DenseSet.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000028#include "llvm/Target/TargetData.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000029#include <sstream>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000030
31using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000032using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000033
Daniel Dunbar97776872009-04-22 07:32:20 +000034// Common CGObjCRuntime functions, these don't belong here, but they
35// don't belong in CGObjCRuntime either so we will live with it for
36// now.
37
Daniel Dunbar532d4da2009-05-03 13:15:50 +000038/// FindIvarInterface - Find the interface containing the ivar.
Daniel Dunbara2435782009-04-22 12:00:04 +000039///
Daniel Dunbar532d4da2009-05-03 13:15:50 +000040/// FIXME: We shouldn't need to do this, the containing context should
41/// be fixed.
42static const ObjCInterfaceDecl *FindIvarInterface(ASTContext &Context,
43 const ObjCInterfaceDecl *OID,
44 const ObjCIvarDecl *OIVD,
45 unsigned &Index) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000046 // FIXME: The index here is closely tied to how
47 // ASTContext::getObjCLayout is implemented. This should be fixed to
48 // get the information from the layout directly.
49 Index = 0;
Fariborz Jahanian98200742009-05-12 18:14:29 +000050 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +000051 Context.ShallowCollectObjCIvars(OID, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +000052 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
53 if (OIVD == Ivars[k])
54 return OID;
55 ++Index;
Daniel Dunbara80a0f62009-04-22 17:43:55 +000056 }
Fariborz Jahanian98200742009-05-12 18:14:29 +000057
Daniel Dunbar532d4da2009-05-03 13:15:50 +000058 // Otherwise check in the super class.
Daniel Dunbara81419d2009-05-05 00:36:57 +000059 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Daniel Dunbar532d4da2009-05-03 13:15:50 +000060 return FindIvarInterface(Context, Super, OIVD, Index);
61
62 return 0;
Daniel Dunbara2435782009-04-22 12:00:04 +000063}
64
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000065static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
66 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000067 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000068 const ObjCIvarDecl *Ivar) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +000069 unsigned Index;
70 const ObjCInterfaceDecl *Container =
71 FindIvarInterface(CGM.getContext(), OID, Ivar, Index);
72 assert(Container && "Unable to find ivar container");
73
74 // If we know have an implementation (and the ivar is in it) then
75 // look up in the implementation layout.
76 const ASTRecordLayout *RL;
77 if (ID && ID->getClassInterface() == Container)
78 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
79 else
80 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
81 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000082}
83
84uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
85 const ObjCInterfaceDecl *OID,
86 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000087 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
88}
89
90uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
91 const ObjCImplementationDecl *OID,
92 const ObjCIvarDecl *Ivar) {
93 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000094}
95
96LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
97 const ObjCInterfaceDecl *OID,
98 llvm::Value *BaseValue,
99 const ObjCIvarDecl *Ivar,
100 unsigned CVRQualifiers,
101 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000102 // Compute (type*) ( (char *) BaseValue + Offset)
Daniel Dunbar97776872009-04-22 07:32:20 +0000103 llvm::Type *I8Ptr = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000104 QualType IvarTy = Ivar->getType();
105 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000106 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000107 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000108 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000109
110 if (Ivar->isBitField()) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000111 // We need to compute the bit offset for the bit-field, the offset
112 // is to the byte. Note, there is a subtle invariant here: we can
113 // only call this routine on non-sythesized ivars but we may be
114 // called for synthesized ivars. However, a synthesized ivar can
115 // never be a bit-field so this is safe.
116 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
117
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000118 uint64_t BitFieldSize =
119 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
120 return LValue::MakeBitfield(V, BitOffset, BitFieldSize,
Daniel Dunbare38df862009-05-03 07:52:00 +0000121 IvarTy->isSignedIntegerType(),
122 IvarTy.getCVRQualifiers()|CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000123 }
124
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000125 LValue LV = LValue::MakeAddr(V, IvarTy.getCVRQualifiers()|CVRQualifiers,
126 CGF.CGM.getContext().getObjCGCAttrKind(IvarTy));
Daniel Dunbar97776872009-04-22 07:32:20 +0000127 LValue::SetObjCIvar(LV, true);
128 return LV;
129}
130
131///
132
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000133namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000134
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000135 typedef std::vector<llvm::Constant*> ConstantVector;
136
Mike Stumpf5408fe2009-05-16 07:57:57 +0000137 // FIXME: We should find a nicer way to make the labels for metadata, string
138 // concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000139
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000140class ObjCCommonTypesHelper {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000141private:
142 llvm::Constant *getMessageSendFn() const {
143 // id objc_msgSend (id, SEL, ...)
144 std::vector<const llvm::Type*> Params;
145 Params.push_back(ObjectPtrTy);
146 Params.push_back(SelectorPtrTy);
147 return
148 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
149 Params, true),
150 "objc_msgSend");
151 }
152
153 llvm::Constant *getMessageSendStretFn() const {
154 // id objc_msgSend_stret (id, SEL, ...)
155 std::vector<const llvm::Type*> Params;
156 Params.push_back(ObjectPtrTy);
157 Params.push_back(SelectorPtrTy);
158 return
159 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
160 Params, true),
161 "objc_msgSend_stret");
162
163 }
164
165 llvm::Constant *getMessageSendFpretFn() const {
166 // FIXME: This should be long double on x86_64?
167 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
168 std::vector<const llvm::Type*> Params;
169 Params.push_back(ObjectPtrTy);
170 Params.push_back(SelectorPtrTy);
171 return
172 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::DoubleTy,
173 Params,
174 true),
175 "objc_msgSend_fpret");
176
177 }
178
179 llvm::Constant *getMessageSendSuperFn() const {
180 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
181 const char *SuperName = "objc_msgSendSuper";
182 std::vector<const llvm::Type*> Params;
183 Params.push_back(SuperPtrTy);
184 Params.push_back(SelectorPtrTy);
185 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
186 Params, true),
187 SuperName);
188 }
189
190 llvm::Constant *getMessageSendSuperFn2() const {
191 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
192 const char *SuperName = "objc_msgSendSuper2";
193 std::vector<const llvm::Type*> Params;
194 Params.push_back(SuperPtrTy);
195 Params.push_back(SelectorPtrTy);
196 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
197 Params, true),
198 SuperName);
199 }
200
201 llvm::Constant *getMessageSendSuperStretFn() const {
202 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
203 // SEL op, ...)
204 std::vector<const llvm::Type*> Params;
205 Params.push_back(Int8PtrTy);
206 Params.push_back(SuperPtrTy);
207 Params.push_back(SelectorPtrTy);
208 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
209 Params, true),
210 "objc_msgSendSuper_stret");
211 }
212
213 llvm::Constant *getMessageSendSuperStretFn2() const {
214 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
215 // SEL op, ...)
216 std::vector<const llvm::Type*> Params;
217 Params.push_back(Int8PtrTy);
218 Params.push_back(SuperPtrTy);
219 Params.push_back(SelectorPtrTy);
220 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
221 Params, true),
222 "objc_msgSendSuper2_stret");
223 }
224
225 llvm::Constant *getMessageSendSuperFpretFn() const {
226 // There is no objc_msgSendSuper_fpret? How can that work?
227 return getMessageSendSuperFn();
228 }
229
230 llvm::Constant *getMessageSendSuperFpretFn2() const {
231 // There is no objc_msgSendSuper_fpret? How can that work?
232 return getMessageSendSuperFn2();
233 }
234
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000235protected:
236 CodeGen::CodeGenModule &CGM;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000237
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000238public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000239 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000240 const llvm::Type *Int8PtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000241
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000242 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
243 const llvm::Type *ObjectPtrTy;
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000244
245 /// PtrObjectPtrTy - LLVM type for id *
246 const llvm::Type *PtrObjectPtrTy;
247
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000248 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000249 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000250 /// ProtocolPtrTy - LLVM type for external protocol handles
251 /// (typeof(Protocol))
252 const llvm::Type *ExternalProtocolPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000253
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000254 // SuperCTy - clang type for struct objc_super.
255 QualType SuperCTy;
256 // SuperPtrCTy - clang type for struct objc_super *.
257 QualType SuperPtrCTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000258
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000259 /// SuperTy - LLVM type for struct objc_super.
260 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000261 /// SuperPtrTy - LLVM type for struct objc_super *.
262 const llvm::Type *SuperPtrTy;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000263
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000264 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
265 /// in GCC parlance).
266 const llvm::StructType *PropertyTy;
267
268 /// PropertyListTy - LLVM type for struct objc_property_list
269 /// (_prop_list_t in GCC parlance).
270 const llvm::StructType *PropertyListTy;
271 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
272 const llvm::Type *PropertyListPtrTy;
273
274 // MethodTy - LLVM type for struct objc_method.
275 const llvm::StructType *MethodTy;
276
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000277 /// CacheTy - LLVM type for struct objc_cache.
278 const llvm::Type *CacheTy;
279 /// CachePtrTy - LLVM type for struct objc_cache *.
280 const llvm::Type *CachePtrTy;
281
Chris Lattner72db6c32009-04-22 02:44:54 +0000282 llvm::Constant *getGetPropertyFn() {
283 CodeGen::CodeGenTypes &Types = CGM.getTypes();
284 ASTContext &Ctx = CGM.getContext();
285 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
286 llvm::SmallVector<QualType,16> Params;
287 QualType IdType = Ctx.getObjCIdType();
288 QualType SelType = Ctx.getObjCSelType();
289 Params.push_back(IdType);
290 Params.push_back(SelType);
291 Params.push_back(Ctx.LongTy);
292 Params.push_back(Ctx.BoolTy);
293 const llvm::FunctionType *FTy =
294 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params), false);
295 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
296 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000297
Chris Lattner72db6c32009-04-22 02:44:54 +0000298 llvm::Constant *getSetPropertyFn() {
299 CodeGen::CodeGenTypes &Types = CGM.getTypes();
300 ASTContext &Ctx = CGM.getContext();
301 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
302 llvm::SmallVector<QualType,16> Params;
303 QualType IdType = Ctx.getObjCIdType();
304 QualType SelType = Ctx.getObjCSelType();
305 Params.push_back(IdType);
306 Params.push_back(SelType);
307 Params.push_back(Ctx.LongTy);
308 Params.push_back(IdType);
309 Params.push_back(Ctx.BoolTy);
310 Params.push_back(Ctx.BoolTy);
311 const llvm::FunctionType *FTy =
312 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params), false);
313 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
314 }
315
316 llvm::Constant *getEnumerationMutationFn() {
317 // void objc_enumerationMutation (id)
318 std::vector<const llvm::Type*> Args;
319 Args.push_back(ObjectPtrTy);
320 llvm::FunctionType *FTy =
321 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
322 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
323 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000324
325 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000326 llvm::Constant *getGcReadWeakFn() {
327 // id objc_read_weak (id *)
328 std::vector<const llvm::Type*> Args;
329 Args.push_back(ObjectPtrTy->getPointerTo());
330 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
331 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
332 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000333
334 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000335 llvm::Constant *getGcAssignWeakFn() {
336 // id objc_assign_weak (id, id *)
337 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
338 Args.push_back(ObjectPtrTy->getPointerTo());
339 llvm::FunctionType *FTy =
340 llvm::FunctionType::get(ObjectPtrTy, Args, false);
341 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
342 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000343
344 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000345 llvm::Constant *getGcAssignGlobalFn() {
346 // id objc_assign_global(id, id *)
347 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
348 Args.push_back(ObjectPtrTy->getPointerTo());
349 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
350 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
351 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000352
353 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000354 llvm::Constant *getGcAssignIvarFn() {
355 // id objc_assign_ivar(id, id *)
356 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
357 Args.push_back(ObjectPtrTy->getPointerTo());
358 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
359 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
360 }
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000361
362 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000363 llvm::Constant *getGcAssignStrongCastFn() {
364 // id objc_assign_global(id, id *)
365 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
366 Args.push_back(ObjectPtrTy->getPointerTo());
367 llvm::FunctionType *FTy = llvm::FunctionType::get(ObjectPtrTy, Args, false);
368 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
369 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000370
371 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000372 llvm::Constant *getExceptionThrowFn() {
373 // void objc_exception_throw(id)
374 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
375 llvm::FunctionType *FTy =
376 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
377 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
378 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000379
Daniel Dunbar1c566672009-02-24 01:43:46 +0000380 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000381 llvm::Constant *getSyncEnterFn() {
382 // void objc_sync_enter (id)
383 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
384 llvm::FunctionType *FTy =
385 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
386 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
387 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000388
389 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000390 llvm::Constant *getSyncExitFn() {
391 // void objc_sync_exit (id)
392 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
393 llvm::FunctionType *FTy =
394 llvm::FunctionType::get(llvm::Type::VoidTy, Args, false);
395 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
396 }
Daniel Dunbar1c566672009-02-24 01:43:46 +0000397
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000398 llvm::Constant *getSendFn(bool IsSuper) const {
399 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
400 }
401
402 llvm::Constant *getSendFn2(bool IsSuper) const {
403 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
404 }
405
406 llvm::Constant *getSendStretFn(bool IsSuper) const {
407 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
408 }
409
410 llvm::Constant *getSendStretFn2(bool IsSuper) const {
411 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
412 }
413
414 llvm::Constant *getSendFpretFn(bool IsSuper) const {
415 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
416 }
417
418 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
419 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
420 }
421
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000422 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
423 ~ObjCCommonTypesHelper(){}
424};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000425
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000426/// ObjCTypesHelper - Helper class that encapsulates lazy
427/// construction of varies types used during ObjC generation.
428class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000429public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000430 /// SymtabTy - LLVM type for struct objc_symtab.
431 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000432 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
433 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000434 /// ModuleTy - LLVM type for struct objc_module.
435 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000436
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000437 /// ProtocolTy - LLVM type for struct objc_protocol.
438 const llvm::StructType *ProtocolTy;
439 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
440 const llvm::Type *ProtocolPtrTy;
441 /// ProtocolExtensionTy - LLVM type for struct
442 /// objc_protocol_extension.
443 const llvm::StructType *ProtocolExtensionTy;
444 /// ProtocolExtensionTy - LLVM type for struct
445 /// objc_protocol_extension *.
446 const llvm::Type *ProtocolExtensionPtrTy;
447 /// MethodDescriptionTy - LLVM type for struct
448 /// objc_method_description.
449 const llvm::StructType *MethodDescriptionTy;
450 /// MethodDescriptionListTy - LLVM type for struct
451 /// objc_method_description_list.
452 const llvm::StructType *MethodDescriptionListTy;
453 /// MethodDescriptionListPtrTy - LLVM type for struct
454 /// objc_method_description_list *.
455 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000456 /// ProtocolListTy - LLVM type for struct objc_property_list.
457 const llvm::Type *ProtocolListTy;
458 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
459 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000460 /// CategoryTy - LLVM type for struct objc_category.
461 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000462 /// ClassTy - LLVM type for struct objc_class.
463 const llvm::StructType *ClassTy;
464 /// ClassPtrTy - LLVM type for struct objc_class *.
465 const llvm::Type *ClassPtrTy;
466 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
467 const llvm::StructType *ClassExtensionTy;
468 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
469 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000470 // IvarTy - LLVM type for struct objc_ivar.
471 const llvm::StructType *IvarTy;
472 /// IvarListTy - LLVM type for struct objc_ivar_list.
473 const llvm::Type *IvarListTy;
474 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
475 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000476 /// MethodListTy - LLVM type for struct objc_method_list.
477 const llvm::Type *MethodListTy;
478 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
479 const llvm::Type *MethodListPtrTy;
Anders Carlsson124526b2008-09-09 10:10:21 +0000480
481 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
482 const llvm::Type *ExceptionDataTy;
483
Anders Carlsson124526b2008-09-09 10:10:21 +0000484 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000485 llvm::Constant *getExceptionTryEnterFn() {
486 std::vector<const llvm::Type*> Params;
487 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
488 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
489 Params, false),
490 "objc_exception_try_enter");
491 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000492
493 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000494 llvm::Constant *getExceptionTryExitFn() {
495 std::vector<const llvm::Type*> Params;
496 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
497 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
498 Params, false),
499 "objc_exception_try_exit");
500 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000501
502 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000503 llvm::Constant *getExceptionExtractFn() {
504 std::vector<const llvm::Type*> Params;
505 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
506 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
507 Params, false),
508 "objc_exception_extract");
509
510 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000511
512 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000513 llvm::Constant *getExceptionMatchFn() {
514 std::vector<const llvm::Type*> Params;
515 Params.push_back(ClassPtrTy);
516 Params.push_back(ObjectPtrTy);
517 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
518 Params, false),
519 "objc_exception_match");
520
521 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000522
523 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000524 llvm::Constant *getSetJmpFn() {
525 std::vector<const llvm::Type*> Params;
526 Params.push_back(llvm::PointerType::getUnqual(llvm::Type::Int32Ty));
527 return
528 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
529 Params, false),
530 "_setjmp");
531
532 }
Chris Lattner10cac6f2008-11-15 21:26:17 +0000533
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000534public:
535 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000536 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000537};
538
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000539/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000540/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000541class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000542public:
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000543
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000544 // MethodListnfABITy - LLVM for struct _method_list_t
545 const llvm::StructType *MethodListnfABITy;
546
547 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
548 const llvm::Type *MethodListnfABIPtrTy;
549
550 // ProtocolnfABITy = LLVM for struct _protocol_t
551 const llvm::StructType *ProtocolnfABITy;
552
Daniel Dunbar948e2582009-02-15 07:36:20 +0000553 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
554 const llvm::Type *ProtocolnfABIPtrTy;
555
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000556 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
557 const llvm::StructType *ProtocolListnfABITy;
558
559 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
560 const llvm::Type *ProtocolListnfABIPtrTy;
561
562 // ClassnfABITy - LLVM for struct _class_t
563 const llvm::StructType *ClassnfABITy;
564
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000565 // ClassnfABIPtrTy - LLVM for struct _class_t*
566 const llvm::Type *ClassnfABIPtrTy;
567
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000568 // IvarnfABITy - LLVM for struct _ivar_t
569 const llvm::StructType *IvarnfABITy;
570
571 // IvarListnfABITy - LLVM for struct _ivar_list_t
572 const llvm::StructType *IvarListnfABITy;
573
574 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
575 const llvm::Type *IvarListnfABIPtrTy;
576
577 // ClassRonfABITy - LLVM for struct _class_ro_t
578 const llvm::StructType *ClassRonfABITy;
579
580 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
581 const llvm::Type *ImpnfABITy;
582
583 // CategorynfABITy - LLVM for struct _category_t
584 const llvm::StructType *CategorynfABITy;
585
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000586 // New types for nonfragile abi messaging.
587
588 // MessageRefTy - LLVM for:
589 // struct _message_ref_t {
590 // IMP messenger;
591 // SEL name;
592 // };
593 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000594 // MessageRefCTy - clang type for struct _message_ref_t
595 QualType MessageRefCTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000596
597 // MessageRefPtrTy - LLVM for struct _message_ref_t*
598 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000599 // MessageRefCPtrTy - clang type for struct _message_ref_t*
600 QualType MessageRefCPtrTy;
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000601
Fariborz Jahanianef163782009-02-05 01:13:09 +0000602 // MessengerTy - Type of the messenger (shown as IMP above)
603 const llvm::FunctionType *MessengerTy;
604
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000605 // SuperMessageRefTy - LLVM for:
606 // struct _super_message_ref_t {
607 // SUPER_IMP messenger;
608 // SEL name;
609 // };
610 const llvm::StructType *SuperMessageRefTy;
611
612 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
613 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000614
Chris Lattner1c02f862009-04-22 02:53:24 +0000615 llvm::Constant *getMessageSendFixupFn() {
616 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
617 std::vector<const llvm::Type*> Params;
618 Params.push_back(ObjectPtrTy);
619 Params.push_back(MessageRefPtrTy);
620 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
621 Params, true),
622 "objc_msgSend_fixup");
623 }
624
625 llvm::Constant *getMessageSendFpretFixupFn() {
626 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
627 std::vector<const llvm::Type*> Params;
628 Params.push_back(ObjectPtrTy);
629 Params.push_back(MessageRefPtrTy);
630 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
631 Params, true),
632 "objc_msgSend_fpret_fixup");
633 }
634
635 llvm::Constant *getMessageSendStretFixupFn() {
636 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
637 std::vector<const llvm::Type*> Params;
638 Params.push_back(ObjectPtrTy);
639 Params.push_back(MessageRefPtrTy);
640 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
641 Params, true),
642 "objc_msgSend_stret_fixup");
643 }
644
645 llvm::Constant *getMessageSendIdFixupFn() {
646 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
647 std::vector<const llvm::Type*> Params;
648 Params.push_back(ObjectPtrTy);
649 Params.push_back(MessageRefPtrTy);
650 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
651 Params, true),
652 "objc_msgSendId_fixup");
653 }
654
655 llvm::Constant *getMessageSendIdStretFixupFn() {
656 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
657 std::vector<const llvm::Type*> Params;
658 Params.push_back(ObjectPtrTy);
659 Params.push_back(MessageRefPtrTy);
660 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
661 Params, true),
662 "objc_msgSendId_stret_fixup");
663 }
664 llvm::Constant *getMessageSendSuper2FixupFn() {
665 // id objc_msgSendSuper2_fixup (struct objc_super *,
666 // struct _super_message_ref_t*, ...)
667 std::vector<const llvm::Type*> Params;
668 Params.push_back(SuperPtrTy);
669 Params.push_back(SuperMessageRefPtrTy);
670 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
671 Params, true),
672 "objc_msgSendSuper2_fixup");
673 }
674
675 llvm::Constant *getMessageSendSuper2StretFixupFn() {
676 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
677 // struct _super_message_ref_t*, ...)
678 std::vector<const llvm::Type*> Params;
679 Params.push_back(SuperPtrTy);
680 Params.push_back(SuperMessageRefPtrTy);
681 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
682 Params, true),
683 "objc_msgSendSuper2_stret_fixup");
684 }
685
686
687
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000688 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
689 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000690 llvm::Value *getEHPersonalityPtr() {
691 llvm::Constant *Personality =
692 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::Int32Ty,
693 std::vector<const llvm::Type*>(),
694 true),
695 "__objc_personality_v0");
696 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
697 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000698
Chris Lattner8a569112009-04-22 02:15:23 +0000699 llvm::Constant *getUnwindResumeOrRethrowFn() {
700 std::vector<const llvm::Type*> Params;
701 Params.push_back(Int8PtrTy);
702 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
703 Params, false),
704 "_Unwind_Resume_or_Rethrow");
705 }
706
707 llvm::Constant *getObjCEndCatchFn() {
708 std::vector<const llvm::Type*> Params;
709 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::VoidTy,
710 Params, false),
711 "objc_end_catch");
712
713 }
714
715 llvm::Constant *getObjCBeginCatchFn() {
716 std::vector<const llvm::Type*> Params;
717 Params.push_back(Int8PtrTy);
718 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
719 Params, false),
720 "objc_begin_catch");
721 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000722
723 const llvm::StructType *EHTypeTy;
724 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000725
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000726 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
727 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000728};
729
730class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000731public:
732 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000733 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000734 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000735 unsigned ivar_bytepos;
736 unsigned ivar_size;
737 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
738 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000739
740 // Allow sorting based on byte pos.
741 bool operator<(const GC_IVAR &b) const {
742 return ivar_bytepos < b.ivar_bytepos;
743 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000744 };
745
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000746 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000747 public:
748 unsigned skip;
749 unsigned scan;
750 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
751 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000752 };
753
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000754protected:
755 CodeGen::CodeGenModule &CGM;
756 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000757 unsigned ObjCABI;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000758
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000759 // gc ivar layout bitmap calculation helper caches.
760 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
761 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000762
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000763 /// LazySymbols - Symbols to generate a lazy reference for. See
764 /// DefinedSymbols and FinishModule().
765 std::set<IdentifierInfo*> LazySymbols;
766
767 /// DefinedSymbols - External symbols which are defined by this
768 /// module. The symbols in this list and LazySymbols are used to add
769 /// special linker symbols which ensure that Objective-C modules are
770 /// linked properly.
771 std::set<IdentifierInfo*> DefinedSymbols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000772
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000773 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000774 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000775
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000776 /// MethodVarNames - uniqued method variable names.
777 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000778
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000779 /// MethodVarTypes - uniqued method type signatures. We have to use
780 /// a StringMap here because have no other unique reference.
781 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000782
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000783 /// MethodDefinitions - map of methods which have been defined in
784 /// this translation unit.
785 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000786
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000787 /// PropertyNames - uniqued method variable names.
788 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000789
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000790 /// ClassReferences - uniqued class references.
791 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000792
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000793 /// SelectorReferences - uniqued selector references.
794 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000795
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000796 /// Protocols - Protocols for which an objc_protocol structure has
797 /// been emitted. Forward declarations are handled by creating an
798 /// empty structure whose initializer is filled in when/if defined.
799 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000800
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000801 /// DefinedProtocols - Protocols which have actually been
802 /// defined. We should not need this, see FIXME in GenerateProtocol.
803 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000804
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000805 /// DefinedClasses - List of defined classes.
806 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000807
808 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
809 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000810
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000811 /// DefinedCategories - List of defined categories.
812 std::vector<llvm::GlobalValue*> DefinedCategories;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000813
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000814 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
815 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
816
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000817 /// UsedGlobals - List of globals to pack into the llvm.used metadata
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000818 /// to prevent them from being clobbered.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000819 std::vector<llvm::GlobalVariable*> UsedGlobals;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000820
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000821 /// GetNameForMethod - Return a name for the given method.
822 /// \param[out] NameOut - The return value.
823 void GetNameForMethod(const ObjCMethodDecl *OMD,
824 const ObjCContainerDecl *CD,
825 std::string &NameOut);
826
827 /// GetMethodVarName - Return a unique constant for the given
828 /// selector's name. The return value has type char *.
829 llvm::Constant *GetMethodVarName(Selector Sel);
830 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
831 llvm::Constant *GetMethodVarName(const std::string &Name);
832
833 /// GetMethodVarType - Return a unique constant for the given
834 /// selector's name. The return value has type char *.
835
836 // FIXME: This is a horrible name.
837 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000838 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000839
840 /// GetPropertyName - Return a unique constant for the given
841 /// name. The return value has type char *.
842 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
843
844 // FIXME: This can be dropped once string functions are unified.
845 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
846 const Decl *Container);
847
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000848 /// GetClassName - Return a unique constant for the given selector's
849 /// name. The return value has type char *.
850 llvm::Constant *GetClassName(IdentifierInfo *Ident);
851
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000852 /// BuildIvarLayout - Builds ivar layout bitmap for the class
853 /// implementation for the __strong or __weak case.
854 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000855 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
856 bool ForStrongLayout);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000857
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000858 void BuildAggrIvarRecordLayout(const RecordType *RT,
859 unsigned int BytePos, bool ForStrongLayout,
860 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000861 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000862 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000863 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000864 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000865 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000866 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000867
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000868 /// GetIvarLayoutName - Returns a unique constant for the given
869 /// ivar layout bitmap.
870 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
871 const ObjCCommonTypesHelper &ObjCTypes);
872
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000873 /// EmitPropertyList - Emit the given property list. The return
874 /// value has type PropertyListPtrTy.
875 llvm::Constant *EmitPropertyList(const std::string &Name,
876 const Decl *Container,
877 const ObjCContainerDecl *OCD,
878 const ObjCCommonTypesHelper &ObjCTypes);
879
Fariborz Jahanianda320092009-01-29 19:24:30 +0000880 /// GetProtocolRef - Return a reference to the internal protocol
881 /// description, creating an empty one if it has not been
882 /// defined. The return value has type ProtocolPtrTy.
883 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000884
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000885 /// CreateMetadataVar - Create a global variable with internal
886 /// linkage for use by the Objective-C runtime.
887 ///
888 /// This is a convenience wrapper which not only creates the
889 /// variable, but also sets the section and alignment and adds the
890 /// global to the UsedGlobals list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000891 ///
892 /// \param Name - The variable name.
893 /// \param Init - The variable initializer; this is also used to
894 /// define the type of the variable.
895 /// \param Section - The section the variable should go into, or 0.
896 /// \param Align - The alignment for the variable, or 0.
897 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000898 /// "llvm.used".
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000899 llvm::GlobalVariable *CreateMetadataVar(const std::string &Name,
900 llvm::Constant *Init,
901 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000902 unsigned Align,
903 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000904
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000905 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
906 QualType ResultType,
907 llvm::Value *Sel,
908 llvm::Value *Arg0,
909 QualType Arg0Ty,
910 bool IsSuper,
911 const CallArgList &CallArgs,
912 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000913
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +0000914 virtual void MergeMetadataGlobals(std::vector<llvm::Constant*> &UsedArray);
915
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000916public:
917 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) : CGM(cgm)
918 { }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000919
Steve Naroff33fdb732009-03-31 16:53:37 +0000920 virtual llvm::Constant *GenerateConstantString(const ObjCStringLiteral *SL);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000921
922 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
923 const ObjCContainerDecl *CD=0);
Fariborz Jahanianda320092009-01-29 19:24:30 +0000924
925 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
926
927 /// GetOrEmitProtocol - Get the protocol object for the given
928 /// declaration, emitting it if necessary. The return value has type
929 /// ProtocolPtrTy.
930 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
931
932 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
933 /// object for the given declaration, emitting it if needed. These
934 /// forward references will be filled in with empty bodies if no
935 /// definition is seen. The return value has type ProtocolPtrTy.
936 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000937};
938
939class CGObjCMac : public CGObjCCommonMac {
940private:
941 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000942 /// EmitImageInfo - Emit the image info marker used to encode some module
943 /// level information.
944 void EmitImageInfo();
945
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000946 /// EmitModuleInfo - Another marker encoding module level
947 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000948 void EmitModuleInfo();
949
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000950 /// EmitModuleSymols - Emit module symbols, the list of defined
951 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000952 llvm::Constant *EmitModuleSymbols();
953
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000954 /// FinishModule - Write out global data structures at the end of
955 /// processing a translation unit.
956 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000957
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000958 /// EmitClassExtension - Generate the class extension structure used
959 /// to store the weak ivar layout and properties. The return value
960 /// has type ClassExtensionPtrTy.
961 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
962
963 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
964 /// for the given class.
Daniel Dunbar45d196b2008-11-01 01:53:16 +0000965 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000966 const ObjCInterfaceDecl *ID);
967
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000968 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +0000969 QualType ResultType,
970 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000971 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000972 QualType Arg0Ty,
973 bool IsSuper,
974 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000975
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000976 /// EmitIvarList - Emit the ivar list for the given
977 /// implementation. If ForClass is true the list of class ivars
978 /// (i.e. metaclass ivars) is emitted, otherwise the list of
979 /// interface ivars will be emitted. The return value has type
980 /// IvarListPtrTy.
981 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000982 bool ForClass);
983
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000984 /// EmitMetaClass - Emit a forward reference to the class structure
985 /// for the metaclass of the given interface. The return value has
986 /// type ClassPtrTy.
987 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
988
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000989 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000990 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000991 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
992 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000993 const ConstantVector &Methods);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000994
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000995 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000996
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000997 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000998
999 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001000 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001001 llvm::Constant *EmitMethodList(const std::string &Name,
1002 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001003 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001004
1005 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001006 /// method declarations.
1007 /// - TypeName: The name for the type containing the methods.
1008 /// - IsProtocol: True iff these methods are for a protocol.
1009 /// - ClassMethds: True iff these are class methods.
1010 /// - Required: When true, only "required" methods are
1011 /// listed. Similarly, when false only "optional" methods are
1012 /// listed. For classes this should always be true.
1013 /// - begin, end: The method list to output.
1014 ///
1015 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001016 llvm::Constant *EmitMethodDescList(const std::string &Name,
1017 const char *Section,
1018 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001019
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001020 /// GetOrEmitProtocol - Get the protocol object for the given
1021 /// declaration, emitting it if necessary. The return value has type
1022 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001023 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001024
1025 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1026 /// object for the given declaration, emitting it if needed. These
1027 /// forward references will be filled in with empty bodies if no
1028 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001029 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001030
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001031 /// EmitProtocolExtension - Generate the protocol extension
1032 /// structure used to store optional instance and class methods, and
1033 /// protocol properties. The return value has type
1034 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001035 llvm::Constant *
1036 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1037 const ConstantVector &OptInstanceMethods,
1038 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001039
1040 /// EmitProtocolList - Generate the list of referenced
1041 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbardbc93372008-08-21 21:57:41 +00001042 llvm::Constant *EmitProtocolList(const std::string &Name,
1043 ObjCProtocolDecl::protocol_iterator begin,
1044 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001045
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001046 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1047 /// for the given selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001048 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001049
Fariborz Jahanianda320092009-01-29 19:24:30 +00001050 public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001051 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001052
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001053 virtual llvm::Function *ModuleInitFunction();
1054
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001055 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001056 QualType ResultType,
1057 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001058 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001059 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001060 const CallArgList &CallArgs,
1061 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001062
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001063 virtual CodeGen::RValue
1064 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001065 QualType ResultType,
1066 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001067 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001068 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001069 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001070 bool IsClassMessage,
1071 const CallArgList &CallArgs);
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001072
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001073 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001074 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001075
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001076 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001077
1078 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1079 /// untyped one.
1080 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1081 const ObjCMethodDecl *Method);
1082
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001083 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001084
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001085 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001086
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001087 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001088 const ObjCProtocolDecl *PD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00001089
Chris Lattner74391b42009-03-22 21:03:39 +00001090 virtual llvm::Constant *GetPropertyGetFunction();
1091 virtual llvm::Constant *GetPropertySetFunction();
1092 virtual llvm::Constant *EnumerationMutationFunction();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001093
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00001094 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1095 const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001096 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1097 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001098 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00001099 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001100 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
1101 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001102 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1103 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001104 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
1105 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001106 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1107 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00001108
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001109 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1110 QualType ObjectTy,
1111 llvm::Value *BaseValue,
1112 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001113 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001114 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001115 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001116 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001117};
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001118
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001119class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001120private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001121 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001122 llvm::GlobalVariable* ObjCEmptyCacheVar;
1123 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001124
Daniel Dunbar11394522009-04-18 08:51:00 +00001125 /// SuperClassReferences - uniqued super class references.
1126 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
1127
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001128 /// MetaClassReferences - uniqued meta class references.
1129 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001130
1131 /// EHTypeReferences - uniqued class ehtype references.
1132 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001133
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001134 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001135 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001136 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001137
1138 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001139 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001140 bool LegacyDispatchedSelector(Selector Sel);
1141
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001142 /// FinishNonFragileABIModule - Write out global data structures at the end of
1143 /// processing a translation unit.
1144 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001145
Daniel Dunbar463b8762009-05-15 21:48:48 +00001146 /// AddModuleClassList - Add the given list of class pointers to the
1147 /// module with the provided symbol and section names.
1148 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1149 const char *SymbolName,
1150 const char *SectionName);
1151
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001152 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1153 unsigned InstanceStart,
1154 unsigned InstanceSize,
1155 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001156 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
1157 llvm::Constant *IsAGV,
1158 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001159 llvm::Constant *ClassRoGV,
1160 bool HiddenVisibility);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001161
1162 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
1163
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001164 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
1165
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001166 /// EmitMethodList - Emit the method list for the given
1167 /// implementation. The return value has type MethodListnfABITy.
1168 llvm::Constant *EmitMethodList(const std::string &Name,
1169 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001170 const ConstantVector &Methods);
1171 /// EmitIvarList - Emit the ivar list for the given
1172 /// implementation. If ForClass is true the list of class ivars
1173 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1174 /// interface ivars will be emitted. The return value has type
1175 /// IvarListnfABIPtrTy.
1176 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00001177
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001178 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001179 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001180 unsigned long int offset);
1181
Fariborz Jahanianda320092009-01-29 19:24:30 +00001182 /// GetOrEmitProtocol - Get the protocol object for the given
1183 /// declaration, emitting it if necessary. The return value has type
1184 /// ProtocolPtrTy.
1185 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
1186
1187 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1188 /// object for the given declaration, emitting it if needed. These
1189 /// forward references will be filled in with empty bodies if no
1190 /// definition is seen. The return value has type ProtocolPtrTy.
1191 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
1192
1193 /// EmitProtocolList - Generate the list of referenced
1194 /// protocols. The return value has type ProtocolListPtrTy.
1195 llvm::Constant *EmitProtocolList(const std::string &Name,
1196 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001197 ObjCProtocolDecl::protocol_iterator end);
1198
1199 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1200 QualType ResultType,
1201 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001202 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001203 QualType Arg0Ty,
1204 bool IsSuper,
1205 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001206
1207 /// GetClassGlobal - Return the global variable for the Objective-C
1208 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001209 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
1210
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001211 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001212 /// for the given class reference.
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001213 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001214 const ObjCInterfaceDecl *ID);
1215
1216 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1217 /// for the given super class reference.
1218 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1219 const ObjCInterfaceDecl *ID);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001220
1221 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1222 /// meta-data
1223 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
1224 const ObjCInterfaceDecl *ID);
1225
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001226 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1227 /// the given ivar.
1228 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001229 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00001230 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001231 const ObjCIvarDecl *Ivar);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001232
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001233 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1234 /// for the given selector.
1235 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel);
Daniel Dunbare588b992009-03-01 04:46:24 +00001236
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001237 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001238 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001239 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1240 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001241
1242 const char *getMetaclassSymbolPrefix() const {
1243 return "OBJC_METACLASS_$_";
1244 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00001245
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001246 const char *getClassSymbolPrefix() const {
1247 return "OBJC_CLASS_$_";
1248 }
1249
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001250 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001251 uint32_t &InstanceStart,
1252 uint32_t &InstanceSize);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001253
1254 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001255 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001256 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1257 return CGM.getContext().Selectors.getSelector(0, &II);
1258 }
1259
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001260 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001261 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1262 return CGM.getContext().Selectors.getSelector(1, &II);
1263 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001264
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001265 /// ImplementationIsNonLazy - Check whether the given category or
1266 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001267 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001268
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001269public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001270 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001271 // FIXME. All stubs for now!
1272 virtual llvm::Function *ModuleInitFunction();
1273
1274 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
1275 QualType ResultType,
1276 Selector Sel,
1277 llvm::Value *Receiver,
1278 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001279 const CallArgList &CallArgs,
1280 const ObjCMethodDecl *Method);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001281
1282 virtual CodeGen::RValue
1283 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
1284 QualType ResultType,
1285 Selector Sel,
1286 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001287 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001288 llvm::Value *Receiver,
1289 bool IsClassMessage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001290 const CallArgList &CallArgs);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001291
1292 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001293 const ObjCInterfaceDecl *ID);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001294
1295 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel)
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001296 { return EmitSelector(Builder, Sel); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001297
1298 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1299 /// untyped one.
1300 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1301 const ObjCMethodDecl *Method)
1302 { return EmitSelector(Builder, Method->getSelector()); }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001303
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001304 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001305
1306 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001307 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001308 const ObjCProtocolDecl *PD);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001309
Chris Lattner74391b42009-03-22 21:03:39 +00001310 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001311 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001312 }
Chris Lattner74391b42009-03-22 21:03:39 +00001313 virtual llvm::Constant *GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001314 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001315 }
Chris Lattner74391b42009-03-22 21:03:39 +00001316 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001317 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001318 }
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001319
1320 virtual void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00001321 const Stmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001322 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001323 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001324 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001325 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001326 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001327 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001328 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001329 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001330 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001331 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001332 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001333 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001334 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1335 QualType ObjectTy,
1336 llvm::Value *BaseValue,
1337 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001338 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001339 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001340 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001341 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001342};
1343
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001344} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001345
1346/* *** Helper Functions *** */
1347
1348/// getConstantGEP() - Help routine to construct simple GEPs.
1349static llvm::Constant *getConstantGEP(llvm::Constant *C,
1350 unsigned idx0,
1351 unsigned idx1) {
1352 llvm::Value *Idxs[] = {
1353 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx0),
1354 llvm::ConstantInt::get(llvm::Type::Int32Ty, idx1)
1355 };
1356 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
1357}
1358
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001359/// hasObjCExceptionAttribute - Return true if this class or any super
1360/// class has the __objc_exception__ attribute.
Douglas Gregor68584ed2009-06-18 16:11:24 +00001361static bool hasObjCExceptionAttribute(ASTContext &Context,
1362 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001363 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001364 return true;
1365 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001366 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001367 return false;
1368}
1369
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001370/* *** CGObjCMac Public Interface *** */
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001371
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001372CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
1373 ObjCTypes(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001374{
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001375 ObjCABI = 1;
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001376 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001377}
1378
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001379/// GetClass - Return a reference to the class for the given interface
1380/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001381llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001382 const ObjCInterfaceDecl *ID) {
1383 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001384}
1385
1386/// GetSelector - Return the pointer to the unique'd string for this selector.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001387llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00001388 return EmitSelector(Builder, Sel);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001389}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001390llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
1391 *Method) {
1392 return EmitSelector(Builder, Method->getSelector());
1393}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001394
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001395/// Generate a constant CFString object.
1396/*
1397 struct __builtin_CFString {
1398 const int *isa; // point to __CFConstantStringClassReference
1399 int flags;
1400 const char *str;
1401 long length;
1402 };
1403*/
1404
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001405llvm::Constant *CGObjCCommonMac::GenerateConstantString(
Steve Naroff33fdb732009-03-31 16:53:37 +00001406 const ObjCStringLiteral *SL) {
Steve Naroff8d4141f2009-04-01 13:55:36 +00001407 return CGM.GetAddrOfConstantCFString(SL->getString());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001408}
1409
1410/// Generates a message send where the super is the receiver. This is
1411/// a message send to self with special delivery semantics indicating
1412/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001413CodeGen::RValue
1414CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001415 QualType ResultType,
1416 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001417 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001418 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001419 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001420 bool IsClassMessage,
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001421 const CodeGen::CallArgList &CallArgs) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001422 // Create and init a super structure; this is a (receiver, class)
1423 // pair we will pass to objc_msgSendSuper.
1424 llvm::Value *ObjCSuper =
1425 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
1426 llvm::Value *ReceiverAsObject =
1427 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
1428 CGF.Builder.CreateStore(ReceiverAsObject,
1429 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001430
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001431 // If this is a class message the metaclass is passed as the target.
1432 llvm::Value *Target;
1433 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001434 if (isCategoryImpl) {
1435 // Message sent to 'super' in a class method defined in a category
1436 // implementation requires an odd treatment.
1437 // If we are in a class method, we must retrieve the
1438 // _metaclass_ for the current class, pointed at by
1439 // the class's "isa" pointer. The following assumes that
1440 // isa" is the first ivar in a class (which it must be).
1441 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1442 Target = CGF.Builder.CreateStructGEP(Target, 0);
1443 Target = CGF.Builder.CreateLoad(Target);
1444 }
1445 else {
1446 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1447 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1448 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1449 Target = Super;
1450 }
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001451 } else {
1452 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1453 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001454 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1455 // ObjCTypes types.
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001456 const llvm::Type *ClassTy =
1457 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001458 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001459 CGF.Builder.CreateStore(Target,
1460 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001461 return EmitLegacyMessageSend(CGF, ResultType,
1462 EmitSelector(CGF.Builder, Sel),
1463 ObjCSuper, ObjCTypes.SuperPtrCTy,
1464 true, CallArgs, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001465}
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001466
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001467/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001468CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001469 QualType ResultType,
1470 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001471 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001472 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001473 const CallArgList &CallArgs,
1474 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001475 return EmitLegacyMessageSend(CGF, ResultType,
1476 EmitSelector(CGF.Builder, Sel),
1477 Receiver, CGF.getContext().getObjCIdType(),
1478 false, CallArgs, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001479}
1480
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001481CodeGen::RValue CGObjCCommonMac::EmitLegacyMessageSend(
1482 CodeGen::CodeGenFunction &CGF,
1483 QualType ResultType,
1484 llvm::Value *Sel,
1485 llvm::Value *Arg0,
1486 QualType Arg0Ty,
1487 bool IsSuper,
1488 const CallArgList &CallArgs,
1489 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001490 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001491 if (!IsSuper)
1492 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001493 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001494 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001495 CGF.getContext().getObjCSelType()));
1496 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001497
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001498 CodeGenTypes &Types = CGM.getTypes();
1499 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs);
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001500 // In 64bit ABI, type must be assumed VARARG. In 32bit abi,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001501 // it seems not to matter.
1502 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo, (ObjCABI == 2));
1503
1504 llvm::Constant *Fn = NULL;
Daniel Dunbar88b53962009-02-02 22:03:45 +00001505 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001506 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
1507 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001508 } else if (ResultType->isFloatingType()) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001509 if (ObjCABI == 2) {
1510 if (const BuiltinType *BT = ResultType->getAsBuiltinType()) {
1511 BuiltinType::Kind k = BT->getKind();
1512 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
1513 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar42f963d2009-06-10 04:38:50 +00001514 } else {
1515 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001516 }
1517 }
1518 else
Mike Stumpf5408fe2009-05-16 07:57:57 +00001519 // FIXME. This currently matches gcc's API for x86-32. May need to change
1520 // for others if we have their API.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001521 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001522 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001523 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
1524 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001525 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001526 assert(Fn && "EmitLegacyMessageSend - unknown API");
Daniel Dunbar62d5c1b2008-09-10 07:00:50 +00001527 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
Daniel Dunbar88b53962009-02-02 22:03:45 +00001528 return CGF.EmitCall(FnInfo, Fn, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001529}
1530
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001531llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001532 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001533 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001534 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001535 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1536
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001537 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
1538 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001539}
1540
Fariborz Jahanianda320092009-01-29 19:24:30 +00001541void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001542 // FIXME: We shouldn't need this, the protocol decl should contain enough
1543 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001544 DefinedProtocols.insert(PD->getIdentifier());
1545
1546 // If we have generated a forward reference to this protocol, emit
1547 // it now. Otherwise do nothing, the protocol objects are lazily
1548 // emitted.
1549 if (Protocols.count(PD->getIdentifier()))
1550 GetOrEmitProtocol(PD);
1551}
1552
Fariborz Jahanianda320092009-01-29 19:24:30 +00001553llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001554 if (DefinedProtocols.count(PD->getIdentifier()))
1555 return GetOrEmitProtocol(PD);
1556 return GetOrEmitProtocolRef(PD);
1557}
1558
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001559/*
1560 // APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1561 struct _objc_protocol {
1562 struct _objc_protocol_extension *isa;
1563 char *protocol_name;
1564 struct _objc_protocol_list *protocol_list;
1565 struct _objc__method_prototype_list *instance_methods;
1566 struct _objc__method_prototype_list *class_methods
1567 };
1568
1569 See EmitProtocolExtension().
1570*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001571llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1572 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1573
1574 // Early exit if a defining object has already been generated.
1575 if (Entry && Entry->hasInitializer())
1576 return Entry;
1577
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001578 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001579 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001580 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1581
Chris Lattner8ec03f52008-11-24 03:54:41 +00001582 const char *ProtocolName = PD->getNameAsCString();
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001583
1584 // Construct method lists.
1585 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1586 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00001587 for (ObjCProtocolDecl::instmeth_iterator
1588 i = PD->instmeth_begin(CGM.getContext()),
1589 e = PD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001590 ObjCMethodDecl *MD = *i;
1591 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1592 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1593 OptInstanceMethods.push_back(C);
1594 } else {
1595 InstanceMethods.push_back(C);
1596 }
1597 }
1598
Douglas Gregor6ab35242009-04-09 21:40:53 +00001599 for (ObjCProtocolDecl::classmeth_iterator
1600 i = PD->classmeth_begin(CGM.getContext()),
1601 e = PD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001602 ObjCMethodDecl *MD = *i;
1603 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1604 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1605 OptClassMethods.push_back(C);
1606 } else {
1607 ClassMethods.push_back(C);
1608 }
1609 }
1610
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001611 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001612 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001613 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001614 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001615 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001616 PD->protocol_begin(),
1617 PD->protocol_end());
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001618 Values[3] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001619 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_"
1620 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001621 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1622 InstanceMethods);
1623 Values[4] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001624 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_"
1625 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001626 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1627 ClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001628 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
1629 Values);
1630
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001631 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001632 // Already created, fix the linkage and update the initializer.
1633 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001634 Entry->setInitializer(Init);
1635 } else {
1636 Entry =
1637 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
1638 llvm::GlobalValue::InternalLinkage,
1639 Init,
1640 std::string("\01L_OBJC_PROTOCOL_")+ProtocolName,
1641 &CGM.getModule());
1642 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001643 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001644 UsedGlobals.push_back(Entry);
1645 // FIXME: Is this necessary? Why only for protocol?
1646 Entry->setAlignment(4);
1647 }
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001648
1649 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001650}
1651
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001652llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001653 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1654
1655 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001656 // We use the initializer as a marker of whether this is a forward
1657 // reference or not. At module finalization we add the empty
1658 // contents for protocols which were referenced but never defined.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001659 Entry =
1660 new llvm::GlobalVariable(ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001661 llvm::GlobalValue::ExternalLinkage,
1662 0,
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001663 "\01L_OBJC_PROTOCOL_" + PD->getNameAsString(),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001664 &CGM.getModule());
1665 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00001666 Entry->setAlignment(4);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001667 UsedGlobals.push_back(Entry);
1668 // FIXME: Is this necessary? Why only for protocol?
1669 Entry->setAlignment(4);
1670 }
1671
1672 return Entry;
1673}
1674
1675/*
1676 struct _objc_protocol_extension {
1677 uint32_t size;
1678 struct objc_method_description_list *optional_instance_methods;
1679 struct objc_method_description_list *optional_class_methods;
1680 struct objc_property_list *instance_properties;
1681 };
1682*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001683llvm::Constant *
1684CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1685 const ConstantVector &OptInstanceMethods,
1686 const ConstantVector &OptClassMethods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001687 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001688 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001689 std::vector<llvm::Constant*> Values(4);
1690 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001691 Values[1] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001692 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
1693 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001694 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1695 OptInstanceMethods);
1696 Values[2] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001697 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_"
1698 + PD->getNameAsString(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001699 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1700 OptClassMethods);
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001701 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" +
1702 PD->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001703 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001704
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001705 // Return null if no extension bits are used.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001706 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
1707 Values[3]->isNullValue())
1708 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
1709
1710 llvm::Constant *Init =
1711 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001712
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001713 // No special section, but goes in llvm.used
1714 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getNameAsString(),
1715 Init,
1716 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001717}
1718
1719/*
1720 struct objc_protocol_list {
1721 struct objc_protocol_list *next;
1722 long count;
1723 Protocol *list[];
1724 };
1725*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001726llvm::Constant *
1727CGObjCMac::EmitProtocolList(const std::string &Name,
1728 ObjCProtocolDecl::protocol_iterator begin,
1729 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001730 std::vector<llvm::Constant*> ProtocolRefs;
1731
Daniel Dunbardbc93372008-08-21 21:57:41 +00001732 for (; begin != end; ++begin)
1733 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001734
1735 // Just return null for empty protocol lists
1736 if (ProtocolRefs.empty())
1737 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1738
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001739 // This list is null terminated.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001740 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
1741
1742 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001743 // This field is only used by the runtime.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001744 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1745 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
1746 Values[2] =
1747 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1748 ProtocolRefs.size()),
1749 ProtocolRefs);
1750
1751 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1752 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001753 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001754 4, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001755 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
1756}
1757
1758/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001759 struct _objc_property {
1760 const char * const name;
1761 const char * const attributes;
1762 };
1763
1764 struct _objc_property_list {
1765 uint32_t entsize; // sizeof (struct _objc_property)
1766 uint32_t prop_count;
1767 struct _objc_property[prop_count];
1768 };
1769*/
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001770llvm::Constant *CGObjCCommonMac::EmitPropertyList(const std::string &Name,
1771 const Decl *Container,
1772 const ObjCContainerDecl *OCD,
1773 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001774 std::vector<llvm::Constant*> Properties, Prop(2);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001775 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(CGM.getContext()),
1776 E = OCD->prop_end(CGM.getContext()); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001777 const ObjCPropertyDecl *PD = *I;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001778 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001779 Prop[1] = GetPropertyTypeString(PD, Container);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001780 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
1781 Prop));
1782 }
1783
1784 // Return null for empty list.
1785 if (Properties.empty())
1786 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1787
1788 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001789 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001790 std::vector<llvm::Constant*> Values(3);
1791 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1792 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
1793 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
1794 Properties.size());
1795 Values[2] = llvm::ConstantArray::get(AT, Properties);
1796 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1797
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001798 llvm::GlobalVariable *GV =
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001799 CreateMetadataVar(Name, Init,
1800 (ObjCABI == 2) ? "__DATA, __objc_const" :
1801 "__OBJC,__property,regular,no_dead_strip",
1802 (ObjCABI == 2) ? 8 : 4,
1803 true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001804 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001805}
1806
1807/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001808 struct objc_method_description_list {
1809 int count;
1810 struct objc_method_description list[];
1811 };
1812*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001813llvm::Constant *
1814CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1815 std::vector<llvm::Constant*> Desc(2);
1816 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1817 ObjCTypes.SelectorPtrTy);
1818 Desc[1] = GetMethodVarType(MD);
1819 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
1820 Desc);
1821}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001822
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001823llvm::Constant *CGObjCMac::EmitMethodDescList(const std::string &Name,
1824 const char *Section,
1825 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001826 // Return null for empty list.
1827 if (Methods.empty())
1828 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
1829
1830 std::vector<llvm::Constant*> Values(2);
1831 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
1832 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
1833 Methods.size());
1834 Values[1] = llvm::ConstantArray::get(AT, Methods);
1835 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
1836
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001837 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001838 return llvm::ConstantExpr::getBitCast(GV,
1839 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001840}
1841
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001842/*
1843 struct _objc_category {
1844 char *category_name;
1845 char *class_name;
1846 struct _objc_method_list *instance_methods;
1847 struct _objc_method_list *class_methods;
1848 struct _objc_protocol_list *protocols;
1849 uint32_t size; // <rdar://4585769>
1850 struct _objc_property_list *instance_properties;
1851 };
1852 */
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001853void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00001854 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001855
Mike Stumpf5408fe2009-05-16 07:57:57 +00001856 // FIXME: This is poor design, the OCD should have a pointer to the category
1857 // decl. Additionally, note that Category can be null for the @implementation
1858 // w/o an @interface case. Sema should just create one for us as it does for
1859 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001860 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001861 const ObjCCategoryDecl *Category =
1862 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001863 std::string ExtName(Interface->getNameAsString() + "_" +
1864 OCD->getNameAsString());
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001865
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001866 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001867 for (ObjCCategoryImplDecl::instmeth_iterator
1868 i = OCD->instmeth_begin(CGM.getContext()),
1869 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001870 // Instance methods should always be defined.
1871 InstanceMethods.push_back(GetMethodConstant(*i));
1872 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001873 for (ObjCCategoryImplDecl::classmeth_iterator
1874 i = OCD->classmeth_begin(CGM.getContext()),
1875 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001876 // Class methods should always be defined.
1877 ClassMethods.push_back(GetMethodConstant(*i));
1878 }
1879
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001880 std::vector<llvm::Constant*> Values(7);
1881 Values[0] = GetClassName(OCD->getIdentifier());
1882 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00001883 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001884 Values[2] =
1885 EmitMethodList(std::string("\01L_OBJC_CATEGORY_INSTANCE_METHODS_") +
1886 ExtName,
1887 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001888 InstanceMethods);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001889 Values[3] =
1890 EmitMethodList(std::string("\01L_OBJC_CATEGORY_CLASS_METHODS_") + ExtName,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001891 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001892 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001893 if (Category) {
1894 Values[4] =
1895 EmitProtocolList(std::string("\01L_OBJC_CATEGORY_PROTOCOLS_") + ExtName,
1896 Category->protocol_begin(),
1897 Category->protocol_end());
1898 } else {
1899 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
1900 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001901 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001902
1903 // If there is no category @interface then there can be no properties.
1904 if (Category) {
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001905 Values[6] = EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001906 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00001907 } else {
1908 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
1909 }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001910
1911 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
1912 Values);
1913
1914 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001915 CreateMetadataVar(std::string("\01L_OBJC_CATEGORY_")+ExtName, Init,
1916 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001917 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001918 DefinedCategories.push_back(GV);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001919}
1920
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001921// FIXME: Get from somewhere?
1922enum ClassFlags {
1923 eClassFlags_Factory = 0x00001,
1924 eClassFlags_Meta = 0x00002,
1925 // <rdr://5142207>
1926 eClassFlags_HasCXXStructors = 0x02000,
1927 eClassFlags_Hidden = 0x20000,
1928 eClassFlags_ABI2_Hidden = 0x00010,
1929 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
1930};
1931
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001932/*
1933 struct _objc_class {
1934 Class isa;
1935 Class super_class;
1936 const char *name;
1937 long version;
1938 long info;
1939 long instance_size;
1940 struct _objc_ivar_list *ivars;
1941 struct _objc_method_list *methods;
1942 struct _objc_cache *cache;
1943 struct _objc_protocol_list *protocols;
1944 // Objective-C 1.0 extensions (<rdr://4585769>)
1945 const char *ivar_layout;
1946 struct _objc_class_ext *ext;
1947 };
1948
1949 See EmitClassExtension();
1950 */
1951void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001952 DefinedSymbols.insert(ID->getIdentifier());
1953
Chris Lattner8ec03f52008-11-24 03:54:41 +00001954 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001955 // FIXME: Gross
1956 ObjCInterfaceDecl *Interface =
1957 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbardbc93372008-08-21 21:57:41 +00001958 llvm::Constant *Protocols =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00001959 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getNameAsString(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001960 Interface->protocol_begin(),
1961 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001962 unsigned Flags = eClassFlags_Factory;
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00001963 unsigned Size =
1964 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001965
1966 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00001967 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001968 Flags |= eClassFlags_Hidden;
1969
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001970 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Douglas Gregor653f1b12009-04-23 01:02:12 +00001971 for (ObjCImplementationDecl::instmeth_iterator
1972 i = ID->instmeth_begin(CGM.getContext()),
1973 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001974 // Instance methods should always be defined.
1975 InstanceMethods.push_back(GetMethodConstant(*i));
1976 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00001977 for (ObjCImplementationDecl::classmeth_iterator
1978 i = ID->classmeth_begin(CGM.getContext()),
1979 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001980 // Class methods should always be defined.
1981 ClassMethods.push_back(GetMethodConstant(*i));
1982 }
1983
Douglas Gregor653f1b12009-04-23 01:02:12 +00001984 for (ObjCImplementationDecl::propimpl_iterator
1985 i = ID->propimpl_begin(CGM.getContext()),
1986 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00001987 ObjCPropertyImplDecl *PID = *i;
1988
1989 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
1990 ObjCPropertyDecl *PD = PID->getPropertyDecl();
1991
1992 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
1993 if (llvm::Constant *C = GetMethodConstant(MD))
1994 InstanceMethods.push_back(C);
1995 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
1996 if (llvm::Constant *C = GetMethodConstant(MD))
1997 InstanceMethods.push_back(C);
1998 }
1999 }
2000
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002001 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002002 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002003 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002004 // Record a reference to the super class.
2005 LazySymbols.insert(Super->getIdentifier());
2006
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002007 Values[ 1] =
2008 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
2009 ObjCTypes.ClassPtrTy);
2010 } else {
2011 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
2012 }
2013 Values[ 2] = GetClassName(ID->getIdentifier());
2014 // Version is always 0.
2015 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2016 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2017 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002018 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002019 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002020 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getNameAsString(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002021 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002022 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002023 // cache is always NULL.
2024 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
2025 Values[ 9] = Protocols;
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002026 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002027 Values[11] = EmitClassExtension(ID);
2028 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
2029 Values);
2030
2031 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002032 CreateMetadataVar(std::string("\01L_OBJC_CLASS_")+ClassName, Init,
2033 "__OBJC,__class,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002034 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002035 DefinedClasses.push_back(GV);
2036}
2037
2038llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2039 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002040 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002041 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002042 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002043
Daniel Dunbar04d40782009-04-14 06:00:08 +00002044 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002045 Flags |= eClassFlags_Hidden;
2046
2047 std::vector<llvm::Constant*> Values(12);
2048 // The isa for the metaclass is the root of the hierarchy.
2049 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2050 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2051 Root = Super;
2052 Values[ 0] =
2053 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
2054 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002055 // The super class for the metaclass is emitted as the name of the
2056 // super class. The runtime fixes this up to point to the
2057 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002058 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
2059 Values[ 1] =
2060 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
2061 ObjCTypes.ClassPtrTy);
2062 } else {
2063 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
2064 }
2065 Values[ 2] = GetClassName(ID->getIdentifier());
2066 // Version is always 0.
2067 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2068 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2069 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002070 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002071 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002072 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002073 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002074 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002075 // cache is always NULL.
2076 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
2077 Values[ 9] = Protocols;
2078 // ivar_layout for metaclass is always NULL.
2079 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2080 // The class extension is always unused for metaclasses.
2081 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2082 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
2083 Values);
2084
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002085 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002086 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002087
2088 // Check for a forward reference.
2089 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2090 if (GV) {
2091 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2092 "Forward metaclass reference has incorrect type.");
2093 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2094 GV->setInitializer(Init);
2095 } else {
2096 GV = new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2097 llvm::GlobalValue::InternalLinkage,
2098 Init, Name,
2099 &CGM.getModule());
2100 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002101 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002102 GV->setAlignment(4);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002103 UsedGlobals.push_back(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002104
2105 return GV;
2106}
2107
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002108llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002109 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002110
Mike Stumpf5408fe2009-05-16 07:57:57 +00002111 // FIXME: Should we look these up somewhere other than the module. Its a bit
2112 // silly since we only generate these while processing an implementation, so
2113 // exactly one pointer would work if know when we entered/exitted an
2114 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002115
2116 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002117 // Previously, metaclass with internal linkage may have been defined.
2118 // pass 'true' as 2nd argument so it is returned.
2119 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002120 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2121 "Forward metaclass reference has incorrect type.");
2122 return GV;
2123 } else {
2124 // Generate as an external reference to keep a consistent
2125 // module. This will be patched up when we emit the metaclass.
2126 return new llvm::GlobalVariable(ObjCTypes.ClassTy, false,
2127 llvm::GlobalValue::ExternalLinkage,
2128 0,
2129 Name,
2130 &CGM.getModule());
2131 }
2132}
2133
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002134/*
2135 struct objc_class_ext {
2136 uint32_t size;
2137 const char *weak_ivar_layout;
2138 struct _objc_property_list *properties;
2139 };
2140*/
2141llvm::Constant *
2142CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
2143 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002144 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002145
2146 std::vector<llvm::Constant*> Values(3);
2147 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002148 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002149 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002150 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002151
2152 // Return null if no extension bits are used.
2153 if (Values[1]->isNullValue() && Values[2]->isNullValue())
2154 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
2155
2156 llvm::Constant *Init =
2157 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002158 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002159 Init, "__OBJC,__class_ext,regular,no_dead_strip",
2160 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002161}
2162
2163/*
2164 struct objc_ivar {
2165 char *ivar_name;
2166 char *ivar_type;
2167 int ivar_offset;
2168 };
2169
2170 struct objc_ivar_list {
2171 int ivar_count;
2172 struct objc_ivar list[count];
2173 };
2174 */
2175llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002176 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002177 std::vector<llvm::Constant*> Ivars, Ivar(3);
2178
2179 // When emitting the root class GCC emits ivar entries for the
2180 // actual class structure. It is not clear if we need to follow this
2181 // behavior; for now lets try and get away with not doing it. If so,
2182 // the cleanest solution would be to make up an ObjCInterfaceDecl
2183 // for the class.
2184 if (ForClass)
2185 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002186
2187 ObjCInterfaceDecl *OID =
2188 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002189
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002190 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002191 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002192
2193 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2194 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002195 // Ignore unnamed bit-fields.
2196 if (!IVD->getDeclName())
2197 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002198 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2199 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00002200 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002201 ComputeIvarBaseOffset(CGM, OID, IVD));
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002202 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002203 }
2204
2205 // Return null for empty list.
2206 if (Ivars.empty())
2207 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
2208
2209 std::vector<llvm::Constant*> Values(2);
2210 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
2211 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
2212 Ivars.size());
2213 Values[1] = llvm::ConstantArray::get(AT, Ivars);
2214 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2215
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002216 llvm::GlobalVariable *GV;
2217 if (ForClass)
2218 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getNameAsString(),
Daniel Dunbar58a29122009-03-09 22:18:41 +00002219 Init, "__OBJC,__class_vars,regular,no_dead_strip",
2220 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002221 else
2222 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_"
2223 + ID->getNameAsString(),
2224 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002225 4, true);
2226 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002227}
2228
2229/*
2230 struct objc_method {
2231 SEL method_name;
2232 char *method_types;
2233 void *method;
2234 };
2235
2236 struct objc_method_list {
2237 struct objc_method_list *obsolete;
2238 int count;
2239 struct objc_method methods_list[count];
2240 };
2241*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002242
2243/// GetMethodConstant - Return a struct objc_method constant for the
2244/// given method if it has been defined. The result is null if the
2245/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002246llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002247 // FIXME: Use DenseMap::lookup
2248 llvm::Function *Fn = MethodDefinitions[MD];
2249 if (!Fn)
2250 return 0;
2251
2252 std::vector<llvm::Constant*> Method(3);
2253 Method[0] =
2254 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2255 ObjCTypes.SelectorPtrTy);
2256 Method[1] = GetMethodVarType(MD);
2257 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
2258 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
2259}
2260
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002261llvm::Constant *CGObjCMac::EmitMethodList(const std::string &Name,
2262 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002263 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002264 // Return null for empty list.
2265 if (Methods.empty())
2266 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
2267
2268 std::vector<llvm::Constant*> Values(3);
2269 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2270 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
2271 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
2272 Methods.size());
2273 Values[2] = llvm::ConstantArray::get(AT, Methods);
2274 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2275
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002276 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002277 return llvm::ConstantExpr::getBitCast(GV,
2278 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002279}
2280
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002281llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbarbb36d332009-02-02 21:43:58 +00002282 const ObjCContainerDecl *CD) {
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002283 std::string Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002284 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002285
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002286 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002287 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002288 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002289 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002290 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002291 llvm::GlobalValue::InternalLinkage,
2292 Name,
2293 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002294 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002295
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002296 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002297}
2298
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002299llvm::GlobalVariable *
2300CGObjCCommonMac::CreateMetadataVar(const std::string &Name,
2301 llvm::Constant *Init,
2302 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002303 unsigned Align,
2304 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002305 const llvm::Type *Ty = Init->getType();
2306 llvm::GlobalVariable *GV =
2307 new llvm::GlobalVariable(Ty, false,
2308 llvm::GlobalValue::InternalLinkage,
2309 Init,
2310 Name,
2311 &CGM.getModule());
2312 if (Section)
2313 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002314 if (Align)
2315 GV->setAlignment(Align);
2316 if (AddToUsed)
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002317 UsedGlobals.push_back(GV);
2318 return GV;
2319}
2320
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002321llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002322 // Abuse this interface function as a place to finalize.
2323 FinishModule();
2324
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002325 return NULL;
2326}
2327
Chris Lattner74391b42009-03-22 21:03:39 +00002328llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002329 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002330}
2331
Chris Lattner74391b42009-03-22 21:03:39 +00002332llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002333 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002334}
2335
Chris Lattner74391b42009-03-22 21:03:39 +00002336llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002337 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002338}
2339
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002340/*
2341
2342Objective-C setjmp-longjmp (sjlj) Exception Handling
2343--
2344
2345The basic framework for a @try-catch-finally is as follows:
2346{
2347 objc_exception_data d;
2348 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002349 bool _call_try_exit = true;
2350
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002351 objc_exception_try_enter(&d);
2352 if (!setjmp(d.jmp_buf)) {
2353 ... try body ...
2354 } else {
2355 // exception path
2356 id _caught = objc_exception_extract(&d);
2357
2358 // enter new try scope for handlers
2359 if (!setjmp(d.jmp_buf)) {
2360 ... match exception and execute catch blocks ...
2361
2362 // fell off end, rethrow.
2363 _rethrow = _caught;
Daniel Dunbar898d5082008-09-30 01:06:03 +00002364 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002365 } else {
2366 // exception in catch block
2367 _rethrow = objc_exception_extract(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002368 _call_try_exit = false;
2369 ... jump-through-finally to finally_rethrow ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002370 }
2371 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002372 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002373
2374finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002375 if (_call_try_exit)
2376 objc_exception_try_exit(&d);
2377
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002378 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002379 ... dispatch to finally destination ...
2380
2381finally_rethrow:
2382 objc_exception_throw(_rethrow);
2383
2384finally_end:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002385}
2386
2387This framework differs slightly from the one gcc uses, in that gcc
Daniel Dunbar898d5082008-09-30 01:06:03 +00002388uses _rethrow to determine if objc_exception_try_exit should be called
2389and if the object should be rethrown. This breaks in the face of
2390throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002391
2392We specialize this framework for a few particular circumstances:
2393
2394 - If there are no catch blocks, then we avoid emitting the second
2395 exception handling context.
2396
2397 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2398 e)) we avoid emitting the code to rethrow an uncaught exception.
2399
2400 - FIXME: If there is no @finally block we can do a few more
2401 simplifications.
2402
2403Rethrows and Jumps-Through-Finally
2404--
2405
2406Support for implicit rethrows and jumping through the finally block is
2407handled by storing the current exception-handling context in
2408ObjCEHStack.
2409
Daniel Dunbar898d5082008-09-30 01:06:03 +00002410In order to implement proper @finally semantics, we support one basic
2411mechanism for jumping through the finally block to an arbitrary
2412destination. Constructs which generate exits from a @try or @catch
2413block use this mechanism to implement the proper semantics by chaining
2414jumps, as necessary.
2415
2416This mechanism works like the one used for indirect goto: we
2417arbitrarily assign an ID to each destination and store the ID for the
2418destination in a variable prior to entering the finally block. At the
2419end of the finally block we simply create a switch to the proper
2420destination.
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002421
2422Code gen for @synchronized(expr) stmt;
2423Effectively generating code for:
2424objc_sync_enter(expr);
2425@try stmt @finally { objc_sync_exit(expr); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002426*/
2427
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002428void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2429 const Stmt &S) {
2430 bool isTry = isa<ObjCAtTryStmt>(S);
Daniel Dunbar898d5082008-09-30 01:06:03 +00002431 // Create various blocks we refer to for handling @finally.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002432 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002433 llvm::BasicBlock *FinallyExit = CGF.createBasicBlock("finally.exit");
Daniel Dunbar55e87422008-11-11 02:29:29 +00002434 llvm::BasicBlock *FinallyNoExit = CGF.createBasicBlock("finally.noexit");
2435 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
2436 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
Daniel Dunbar1c566672009-02-24 01:43:46 +00002437
2438 // For @synchronized, call objc_sync_enter(sync.expr). The
2439 // evaluation of the expression must occur before we enter the
2440 // @synchronized. We can safely avoid a temp here because jumps into
2441 // @synchronized are illegal & this will dominate uses.
2442 llvm::Value *SyncArg = 0;
2443 if (!isTry) {
2444 SyncArg =
2445 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2446 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00002447 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002448 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002449
2450 // Push an EH context entry, used for handling rethrows and jumps
2451 // through finally.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002452 CGF.PushCleanupBlock(FinallyBlock);
2453
Anders Carlsson273558f2009-02-07 21:37:21 +00002454 CGF.ObjCEHValueStack.push_back(0);
2455
Daniel Dunbar898d5082008-09-30 01:06:03 +00002456 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002457 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2458 "exceptiondata.ptr");
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002459 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
2460 "_rethrow");
Anders Carlsson190d00e2009-02-07 21:26:04 +00002461 llvm::Value *CallTryExitPtr = CGF.CreateTempAlloca(llvm::Type::Int1Ty,
2462 "_call_try_exit");
2463 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(), CallTryExitPtr);
2464
Anders Carlsson80f25672008-09-09 17:59:25 +00002465 // Enter a new try block and call setjmp.
Chris Lattner34b02a12009-04-22 02:26:14 +00002466 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Anders Carlsson80f25672008-09-09 17:59:25 +00002467 llvm::Value *JmpBufPtr = CGF.Builder.CreateStructGEP(ExceptionData, 0,
2468 "jmpbufarray");
2469 JmpBufPtr = CGF.Builder.CreateStructGEP(JmpBufPtr, 0, "tmp");
Chris Lattner34b02a12009-04-22 02:26:14 +00002470 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002471 JmpBufPtr, "result");
Daniel Dunbar898d5082008-09-30 01:06:03 +00002472
Daniel Dunbar55e87422008-11-11 02:29:29 +00002473 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2474 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002475 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(SetJmpResult, "threw"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002476 TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002477
2478 // Emit the @try block.
2479 CGF.EmitBlock(TryBlock);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002480 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
2481 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002482 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002483
2484 // Emit the "exception in @try" block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002485 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002486
2487 // Retrieve the exception object. We may emit multiple blocks but
2488 // nothing can cross this so the value is already in SSA form.
Chris Lattner34b02a12009-04-22 02:26:14 +00002489 llvm::Value *Caught =
2490 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2491 ExceptionData, "caught");
Anders Carlsson273558f2009-02-07 21:37:21 +00002492 CGF.ObjCEHValueStack.back() = Caught;
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002493 if (!isTry)
2494 {
2495 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002496 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002497 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002498 }
2499 else if (const ObjCAtCatchStmt* CatchStmt =
2500 cast<ObjCAtTryStmt>(S).getCatchStmts())
2501 {
Daniel Dunbar55e40722008-09-27 07:03:52 +00002502 // Enter a new exception try block (in case a @catch block throws
2503 // an exception).
Chris Lattner34b02a12009-04-22 02:26:14 +00002504 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002505
Chris Lattner34b02a12009-04-22 02:26:14 +00002506 llvm::Value *SetJmpResult = CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(),
Anders Carlsson80f25672008-09-09 17:59:25 +00002507 JmpBufPtr, "result");
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002508 llvm::Value *Threw = CGF.Builder.CreateIsNotNull(SetJmpResult, "threw");
Anders Carlsson80f25672008-09-09 17:59:25 +00002509
Daniel Dunbar55e87422008-11-11 02:29:29 +00002510 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
2511 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch.handler");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002512 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002513
2514 CGF.EmitBlock(CatchBlock);
2515
Daniel Dunbar55e40722008-09-27 07:03:52 +00002516 // Handle catch list. As a special case we check if everything is
2517 // matched and avoid generating code for falling off the end if
2518 // so.
2519 bool AllMatched = false;
Anders Carlsson80f25672008-09-09 17:59:25 +00002520 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbar55e87422008-11-11 02:29:29 +00002521 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
Anders Carlsson80f25672008-09-09 17:59:25 +00002522
Steve Naroff7ba138a2009-03-03 19:52:17 +00002523 const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Daniel Dunbar129271a2008-09-27 07:36:24 +00002524 const PointerType *PT = 0;
2525
Anders Carlsson80f25672008-09-09 17:59:25 +00002526 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002527 if (!CatchParam) {
2528 AllMatched = true;
2529 } else {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002530 PT = CatchParam->getType()->getAsPointerType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002531
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002532 // catch(id e) always matches.
2533 // FIXME: For the time being we also match id<X>; this should
2534 // be rejected by Sema instead.
Steve Naroff389bf462009-02-12 17:52:19 +00002535 if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
Steve Naroff7ba138a2009-03-03 19:52:17 +00002536 CatchParam->getType()->isObjCQualifiedIdType())
Daniel Dunbar55e40722008-09-27 07:03:52 +00002537 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002538 }
2539
Daniel Dunbar55e40722008-09-27 07:03:52 +00002540 if (AllMatched) {
Anders Carlssondde0a942008-09-11 09:15:33 +00002541 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002542 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002543 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002544 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002545 }
Anders Carlsson1452f552008-09-11 08:21:54 +00002546
Anders Carlssondde0a942008-09-11 09:15:33 +00002547 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002548 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002549 break;
2550 }
2551
Daniel Dunbar129271a2008-09-27 07:36:24 +00002552 assert(PT && "Unexpected non-pointer type in @catch");
2553 QualType T = PT->getPointeeType();
Anders Carlsson4b7ff6e2008-09-11 06:35:14 +00002554 const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
Anders Carlsson80f25672008-09-09 17:59:25 +00002555 assert(ObjCType && "Catch parameter must have Objective-C type!");
2556
2557 // Check if the @catch block matches the exception object.
2558 llvm::Value *Class = EmitClassRef(CGF.Builder, ObjCType->getDecl());
2559
Chris Lattner34b02a12009-04-22 02:26:14 +00002560 llvm::Value *Match =
2561 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2562 Class, Caught, "match");
Anders Carlsson80f25672008-09-09 17:59:25 +00002563
Daniel Dunbar55e87422008-11-11 02:29:29 +00002564 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("matched");
Anders Carlsson80f25672008-09-09 17:59:25 +00002565
Daniel Dunbar91cd3202008-10-02 17:05:36 +00002566 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002567 MatchedBlock, NextCatchBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00002568
2569 // Emit the @catch block.
2570 CGF.EmitBlock(MatchedBlock);
Steve Naroff7ba138a2009-03-03 19:52:17 +00002571 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002572 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002573
2574 llvm::Value *Tmp =
Steve Naroff7ba138a2009-03-03 19:52:17 +00002575 CGF.Builder.CreateBitCast(Caught, CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002576 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002577 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002578
2579 CGF.EmitStmt(CatchStmt->getCatchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002580 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002581
2582 CGF.EmitBlock(NextCatchBlock);
2583 }
2584
Daniel Dunbar55e40722008-09-27 07:03:52 +00002585 if (!AllMatched) {
2586 // None of the handlers caught the exception, so store it to be
2587 // rethrown at the end of the @finally block.
2588 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002589 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002590 }
2591
2592 // Emit the exception handler for the @catch blocks.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002593 CGF.EmitBlock(CatchHandler);
Chris Lattner34b02a12009-04-22 02:26:14 +00002594 CGF.Builder.CreateStore(
2595 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2596 ExceptionData),
Daniel Dunbar55e40722008-09-27 07:03:52 +00002597 RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002598 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002599 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002600 } else {
Anders Carlsson80f25672008-09-09 17:59:25 +00002601 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002602 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(), CallTryExitPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002603 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002604 }
2605
Daniel Dunbar898d5082008-09-30 01:06:03 +00002606 // Pop the exception-handling stack entry. It is important to do
2607 // this now, because the code in the @finally block is not in this
2608 // context.
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002609 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
2610
Anders Carlsson273558f2009-02-07 21:37:21 +00002611 CGF.ObjCEHValueStack.pop_back();
2612
Anders Carlsson80f25672008-09-09 17:59:25 +00002613 // Emit the @finally block.
2614 CGF.EmitBlock(FinallyBlock);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002615 llvm::Value* CallTryExit = CGF.Builder.CreateLoad(CallTryExitPtr, "tmp");
2616
2617 CGF.Builder.CreateCondBr(CallTryExit, FinallyExit, FinallyNoExit);
2618
2619 CGF.EmitBlock(FinallyExit);
Chris Lattner34b02a12009-04-22 02:26:14 +00002620 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData);
Daniel Dunbar129271a2008-09-27 07:36:24 +00002621
2622 CGF.EmitBlock(FinallyNoExit);
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002623 if (isTry) {
2624 if (const ObjCAtFinallyStmt* FinallyStmt =
2625 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2626 CGF.EmitStmt(FinallyStmt->getFinallyBody());
Daniel Dunbar1c566672009-02-24 01:43:46 +00002627 } else {
2628 // Emit objc_sync_exit(expr); as finally's sole statement for
2629 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00002630 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002631 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002632
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002633 // Emit the switch block
2634 if (Info.SwitchBlock)
2635 CGF.EmitBlock(Info.SwitchBlock);
2636 if (Info.EndBlock)
2637 CGF.EmitBlock(Info.EndBlock);
2638
Daniel Dunbar898d5082008-09-30 01:06:03 +00002639 CGF.EmitBlock(FinallyRethrow);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002640 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar898d5082008-09-30 01:06:03 +00002641 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002642 CGF.Builder.CreateUnreachable();
Daniel Dunbar898d5082008-09-30 01:06:03 +00002643
2644 CGF.EmitBlock(FinallyEnd);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002645}
2646
2647void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002648 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002649 llvm::Value *ExceptionAsObject;
2650
2651 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2652 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
2653 ExceptionAsObject =
2654 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2655 } else {
Anders Carlsson273558f2009-02-07 21:37:21 +00002656 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002657 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002658 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002659 }
2660
Chris Lattnerbbccd612009-04-22 02:38:11 +00002661 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Anders Carlsson80f25672008-09-09 17:59:25 +00002662 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002663
2664 // Clear the insertion point to indicate we are in unreachable code.
2665 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002666}
2667
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002668/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002669/// object: objc_read_weak (id *src)
2670///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002671llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002672 llvm::Value *AddrWeakObj)
2673{
Eli Friedman8339b352009-03-07 03:57:15 +00002674 const llvm::Type* DestTy =
2675 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002676 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00002677 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002678 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00002679 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002680 return read_weak;
2681}
2682
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002683/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2684/// objc_assign_weak (id src, id *dst)
2685///
2686void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
2687 llvm::Value *src, llvm::Value *dst)
2688{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002689 const llvm::Type * SrcTy = src->getType();
2690 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002691 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002692 assert(Size <= 8 && "does not support size > 8");
2693 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2694 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002695 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2696 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002697 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2698 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00002699 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002700 src, dst, "weakassign");
2701 return;
2702}
2703
Fariborz Jahanian58626502008-11-19 00:59:10 +00002704/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
2705/// objc_assign_global (id src, id *dst)
2706///
2707void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
2708 llvm::Value *src, llvm::Value *dst)
2709{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002710 const llvm::Type * SrcTy = src->getType();
2711 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002712 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002713 assert(Size <= 8 && "does not support size > 8");
2714 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2715 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002716 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2717 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002718 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2719 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002720 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002721 src, dst, "globalassign");
2722 return;
2723}
2724
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002725/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
2726/// objc_assign_ivar (id src, id *dst)
2727///
2728void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
2729 llvm::Value *src, llvm::Value *dst)
2730{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002731 const llvm::Type * SrcTy = src->getType();
2732 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002733 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002734 assert(Size <= 8 && "does not support size > 8");
2735 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2736 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002737 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2738 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002739 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2740 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002741 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00002742 src, dst, "assignivar");
2743 return;
2744}
2745
Fariborz Jahanian58626502008-11-19 00:59:10 +00002746/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
2747/// objc_assign_strongCast (id src, id *dst)
2748///
2749void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
2750 llvm::Value *src, llvm::Value *dst)
2751{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002752 const llvm::Type * SrcTy = src->getType();
2753 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00002754 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00002755 assert(Size <= 8 && "does not support size > 8");
2756 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
2757 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00002758 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2759 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00002760 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
2761 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00002762 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00002763 src, dst, "weakassign");
2764 return;
2765}
2766
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002767/// EmitObjCValueForIvar - Code Gen for ivar reference.
2768///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002769LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
2770 QualType ObjectTy,
2771 llvm::Value *BaseValue,
2772 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00002773 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00002774 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00002775 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
2776 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00002777}
2778
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002779llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00002780 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002781 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00002782 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00002783 return llvm::ConstantInt::get(
2784 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
2785 Offset);
2786}
2787
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002788/* *** Private Interface *** */
2789
2790/// EmitImageInfo - Emit the image info marker used to encode some module
2791/// level information.
2792///
2793/// See: <rdr://4810609&4810587&4810587>
2794/// struct IMAGE_INFO {
2795/// unsigned version;
2796/// unsigned flags;
2797/// };
2798enum ImageInfoFlags {
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002799 eImageInfo_FixAndContinue = (1 << 0), // FIXME: Not sure what
2800 // this implies.
2801 eImageInfo_GarbageCollected = (1 << 1),
2802 eImageInfo_GCOnly = (1 << 2),
2803 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
2804
2805 // A flag indicating that the module has no instances of an
2806 // @synthesize of a superclass variable. <rdar://problem/6803242>
2807 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002808};
2809
2810void CGObjCMac::EmitImageInfo() {
2811 unsigned version = 0; // Version is unused?
2812 unsigned flags = 0;
2813
2814 // FIXME: Fix and continue?
2815 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
2816 flags |= eImageInfo_GarbageCollected;
2817 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
2818 flags |= eImageInfo_GCOnly;
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00002819
2820 // We never allow @synthesize of a superclass property.
2821 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002822
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002823 // Emitted as int[2];
2824 llvm::Constant *values[2] = {
2825 llvm::ConstantInt::get(llvm::Type::Int32Ty, version),
2826 llvm::ConstantInt::get(llvm::Type::Int32Ty, flags)
2827 };
2828 llvm::ArrayType *AT = llvm::ArrayType::get(llvm::Type::Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002829
2830 const char *Section;
2831 if (ObjCABI == 1)
2832 Section = "__OBJC, __image_info,regular";
2833 else
2834 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002835 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002836 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
2837 llvm::ConstantArray::get(AT, values, 2),
2838 Section,
2839 0,
2840 true);
2841 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002842}
2843
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002844
2845// struct objc_module {
2846// unsigned long version;
2847// unsigned long size;
2848// const char *name;
2849// Symtab symtab;
2850// };
2851
2852// FIXME: Get from somewhere
2853static const int ModuleVersion = 7;
2854
2855void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00002856 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002857
2858 std::vector<llvm::Constant*> Values(4);
2859 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
2860 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002861 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002862 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002863 Values[3] = EmitModuleSymbols();
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002864 CreateMetadataVar("\01L_OBJC_MODULES",
2865 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
2866 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002867 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002868}
2869
2870llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002871 unsigned NumClasses = DefinedClasses.size();
2872 unsigned NumCategories = DefinedCategories.size();
2873
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002874 // Return null if no symbols were defined.
2875 if (!NumClasses && !NumCategories)
2876 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
2877
2878 std::vector<llvm::Constant*> Values(5);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002879 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2880 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
2881 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
2882 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
2883
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002884 // The runtime expects exactly the list of defined classes followed
2885 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002886 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002887 for (unsigned i=0; i<NumClasses; i++)
2888 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
2889 ObjCTypes.Int8PtrTy);
2890 for (unsigned i=0; i<NumCategories; i++)
2891 Symbols[NumClasses + i] =
2892 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
2893 ObjCTypes.Int8PtrTy);
2894
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002895 Values[4] =
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002896 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002897 NumClasses + NumCategories),
2898 Symbols);
2899
2900 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
2901
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002902 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002903 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
2904 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002905 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002906 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
2907}
2908
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002909llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002910 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002911 LazySymbols.insert(ID->getIdentifier());
2912
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002913 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
2914
2915 if (!Entry) {
2916 llvm::Constant *Casted =
2917 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
2918 ObjCTypes.ClassPtrTy);
2919 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002920 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
2921 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002922 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002923 }
2924
2925 return Builder.CreateLoad(Entry, false, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002926}
2927
Daniel Dunbar45d196b2008-11-01 01:53:16 +00002928llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002929 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
2930
2931 if (!Entry) {
2932 llvm::Constant *Casted =
2933 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
2934 ObjCTypes.SelectorPtrTy);
2935 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002936 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
2937 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002938 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00002939 }
2940
2941 return Builder.CreateLoad(Entry, false, "tmp");
2942}
2943
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00002944llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002945 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002946
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002947 if (!Entry)
2948 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
2949 llvm::ConstantArray::get(Ident->getName()),
2950 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00002951 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002952
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002953 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00002954}
2955
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00002956/// GetIvarLayoutName - Returns a unique constant for the given
2957/// ivar layout bitmap.
2958llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
2959 const ObjCCommonTypesHelper &ObjCTypes) {
2960 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
2961}
2962
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002963static QualType::GCAttrTypes GetGCAttrTypeForType(ASTContext &Ctx,
2964 QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002965 if (FQT.isObjCGCStrong())
2966 return QualType::Strong;
2967
2968 if (FQT.isObjCGCWeak())
2969 return QualType::Weak;
2970
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002971 if (Ctx.isObjCObjectPointerType(FQT))
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002972 return QualType::Strong;
2973
2974 if (const PointerType *PT = FQT->getAsPointerType())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002975 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00002976
2977 return QualType::GCNone;
2978}
2979
Daniel Dunbard58edcb2009-05-03 14:10:34 +00002980void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
2981 unsigned int BytePos,
2982 bool ForStrongLayout,
2983 bool &HasUnion) {
2984 const RecordDecl *RD = RT->getDecl();
2985 // FIXME - Use iterator.
2986 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(CGM.getContext()),
2987 RD->field_end(CGM.getContext()));
2988 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
2989 const llvm::StructLayout *RecLayout =
2990 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
2991
2992 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
2993 ForStrongLayout, HasUnion);
2994}
2995
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00002996void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00002997 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00002998 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00002999 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003000 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003001 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003002 bool IsUnion = (RD && RD->isUnion());
3003 uint64_t MaxUnionIvarSize = 0;
3004 uint64_t MaxSkippedUnionIvarSize = 0;
3005 FieldDecl *MaxField = 0;
3006 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003007 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003008 uint64_t MaxFieldOffset = 0;
3009 uint64_t MaxSkippedFieldOffset = 0;
3010 uint64_t LastBitfieldOffset = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003011
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003012 if (RecFields.empty())
3013 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003014 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3015 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3016
Chris Lattnerf1690852009-03-31 08:48:01 +00003017 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003018 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003019 uint64_t FieldOffset;
3020 if (RD)
3021 FieldOffset =
3022 Layout->getElementOffset(CGM.getTypes().getLLVMFieldNo(Field));
3023 else
3024 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003025
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003026 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003027 if (!Field->getIdentifier() || Field->isBitField()) {
3028 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003029 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003030 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003031 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003032
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003033 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003034 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003035 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003036 if (FQT->isUnionType())
3037 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003038
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003039 BuildAggrIvarRecordLayout(FQT->getAsRecordType(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003040 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003041 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003042 continue;
3043 }
Chris Lattnerf1690852009-03-31 08:48:01 +00003044
3045 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003046 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003047 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003048 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003049 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003050 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003051 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3052 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003053 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003054 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003055 FQT = CArray->getElementType();
3056 }
3057
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003058 assert(!FQT->isUnionType() &&
3059 "layout for array of unions not supported");
3060 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003061 int OldIndex = IvarsInfo.size() - 1;
3062 int OldSkIndex = SkipIvars.size() -1;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003063
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003064 const RecordType *RT = FQT->getAsRecordType();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003065 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003066 ForStrongLayout, HasUnion);
3067
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003068 // Replicate layout information for each array element. Note that
3069 // one element is already done.
3070 uint64_t ElIx = 1;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003071 for (int FirstIndex = IvarsInfo.size() - 1,
3072 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003073 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003074 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3075 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3076 IvarsInfo[i].ivar_size));
3077 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3078 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3079 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003080 }
3081 continue;
3082 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003083 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003084 // At this point, we are done with Record/Union and array there of.
3085 // For other arrays we are down to its element type.
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003086 QualType::GCAttrTypes GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003087
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003088 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003089 if ((ForStrongLayout && GCAttr == QualType::Strong)
3090 || (!ForStrongLayout && GCAttr == QualType::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003091 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003092 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003093 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003094 MaxUnionIvarSize = UnionIvarSize;
3095 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003096 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003097 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003098 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003099 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003100 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003101 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003102 } else if ((ForStrongLayout &&
3103 (GCAttr == QualType::GCNone || GCAttr == QualType::Weak))
3104 || (!ForStrongLayout && GCAttr != QualType::Weak)) {
3105 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003106 // FIXME: Why the asymmetry? We divide by word size in bits on other
3107 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003108 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003109 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003110 MaxSkippedUnionIvarSize = UnionIvarSize;
3111 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003112 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003113 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003114 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003115 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003116 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003117 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003118 }
3119 }
3120 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003121
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003122 if (LastFieldBitfield) {
3123 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003124 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3125 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003126 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003127 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003128 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003129 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3130 + ((BitFieldSize % ByteSizeInBits) != 0);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003131 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003132 }
3133
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003134 if (MaxField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003135 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003136 MaxUnionIvarSize));
3137 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003138 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003139 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003140}
3141
3142/// BuildIvarLayout - Builds ivar layout bitmap for the class
3143/// implementation for the __strong or __weak case.
3144/// The layout map displays which words in ivar list must be skipped
3145/// and which must be scanned by GC (see below). String is built of bytes.
3146/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3147/// of words to skip and right nibble is count of words to scan. So, each
3148/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3149/// represented by a 0x00 byte which also ends the string.
3150/// 1. when ForStrongLayout is true, following ivars are scanned:
3151/// - id, Class
3152/// - object *
3153/// - __strong anything
3154///
3155/// 2. When ForStrongLayout is false, following ivars are scanned:
3156/// - __weak anything
3157///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003158llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003159 const ObjCImplementationDecl *OMD,
3160 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003161 bool hasUnion = false;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003162
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003163 unsigned int WordsToScan, WordsToSkip;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003164 const llvm::Type *PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3165 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3166 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003167
Chris Lattnerf1690852009-03-31 08:48:01 +00003168 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003169 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003170 CGM.getContext().CollectObjCIvars(OI, RecFields);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003171
Daniel Dunbar37153282009-05-04 04:10:48 +00003172 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003173 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
3174 CGM.getContext().CollectSynthesizedIvars(OI, Ivars);
3175 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3176 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3177
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003178 if (RecFields.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003179 return llvm::Constant::getNullValue(PtrTy);
Chris Lattnerf1690852009-03-31 08:48:01 +00003180
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003181 SkipIvars.clear();
3182 IvarsInfo.clear();
Fariborz Jahanian21e6f172009-03-11 21:42:00 +00003183
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003184 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003185 if (IvarsInfo.empty())
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003186 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003187
3188 // Sort on byte position in case we encounterred a union nested in
3189 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003190 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003191 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003192 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003193 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003194
3195 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003196 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003197 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003198 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003199 if (IvarsInfo[0].ivar_bytepos == 0) {
3200 WordsToSkip = 0;
3201 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003202 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003203 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3204 WordsToScan = IvarsInfo[0].ivar_size;
3205 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003206 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003207 unsigned int TailPrevGCObjC =
3208 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003209 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003210 // consecutive 'scanned' object pointers.
3211 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003212 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003213 // Skip over 'gc'able object pointer which lay over each other.
3214 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3215 continue;
3216 // Must skip over 1 or more words. We save current skip/scan values
3217 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003218 SKIP_SCAN SkScan;
3219 SkScan.skip = WordsToSkip;
3220 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003221 SkipScanIvars.push_back(SkScan);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003222
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003223 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003224 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3225 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003226 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003227 WordsToSkip = 0;
3228 WordsToScan = IvarsInfo[i].ivar_size;
3229 }
3230 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003231 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003232 SKIP_SCAN SkScan;
3233 SkScan.skip = WordsToSkip;
3234 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003235 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003236 }
3237
3238 bool BytesSkipped = false;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003239 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003240 unsigned int LastIndex = SkipIvars.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003241 int LastByteSkipped =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003242 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
3243 LastIndex = IvarsInfo.size()-1;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003244 int LastByteScanned =
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003245 IvarsInfo[LastIndex].ivar_bytepos +
3246 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003247 BytesSkipped = (LastByteSkipped > LastByteScanned);
3248 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003249 if (BytesSkipped) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003250 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003251 SKIP_SCAN SkScan;
3252 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3253 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003254 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003255 }
3256 }
3257 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3258 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003259 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003260 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003261 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3262 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3263 // 0xM0 followed by 0x0N detected.
3264 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3265 for (int j = i+1; j < SkipScan; j++)
3266 SkipScanIvars[j] = SkipScanIvars[j+1];
3267 --SkipScan;
3268 }
3269 }
3270
3271 // Generate the string.
3272 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003273 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003274 unsigned char byte;
3275 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3276 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3277 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3278 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3279
3280 if (skip_small > 0 || skip_big > 0)
3281 BytesSkipped = true;
3282 // first skip big.
3283 for (unsigned int ix = 0; ix < skip_big; ix++)
3284 BitMap += (unsigned char)(0xf0);
3285
3286 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003287 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003288 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003289 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003290 byte |= 0xf;
3291 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003292 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003293 byte |= scan_small;
3294 scan_small = 0;
3295 }
3296 BitMap += byte;
3297 }
3298 // next scan big
3299 for (unsigned int ix = 0; ix < scan_big; ix++)
3300 BitMap += (unsigned char)(0x0f);
3301 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003302 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003303 byte = scan_small;
3304 BitMap += byte;
3305 }
3306 }
3307 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003308 unsigned char zero = 0;
3309 BitMap += zero;
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003310
3311 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
3312 printf("\n%s ivar layout for class '%s': ",
3313 ForStrongLayout ? "strong" : "weak",
3314 OMD->getClassInterface()->getNameAsCString());
3315 const unsigned char *s = (unsigned char*)BitMap.c_str();
3316 for (unsigned i = 0; i < BitMap.size(); i++)
3317 if (!(s[i] & 0xf0))
3318 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3319 else
3320 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3321 printf("\n");
3322 }
3323
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003324 // if ivar_layout bitmap is all 1 bits (nothing skipped) then use NULL as
3325 // final layout.
3326 if (ForStrongLayout && !BytesSkipped)
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003327 return llvm::Constant::getNullValue(PtrTy);
3328 llvm::GlobalVariable * Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3329 llvm::ConstantArray::get(BitMap.c_str()),
3330 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003331 1, true);
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003332 return getConstantGEP(Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003333}
3334
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003335llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003336 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3337
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003338 // FIXME: Avoid std::string copying.
3339 if (!Entry)
3340 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
3341 llvm::ConstantArray::get(Sel.getAsString()),
3342 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003343 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003344
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003345 return getConstantGEP(Entry, 0, 0);
3346}
3347
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003348// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003349llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003350 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3351}
3352
3353// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003354llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003355 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3356}
3357
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003358llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003359 std::string TypeStr;
3360 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3361
3362 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003363
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003364 if (!Entry)
3365 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3366 llvm::ConstantArray::get(TypeStr),
3367 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003368 1, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003369
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003370 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003371}
3372
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003373llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003374 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003375 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3376 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003377
3378 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3379
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003380 if (!Entry)
3381 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
3382 llvm::ConstantArray::get(TypeStr),
3383 "__TEXT,__cstring,cstring_literals",
3384 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003385
3386 return getConstantGEP(Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003387}
3388
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003389// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003390llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003391 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
3392
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003393 if (!Entry)
3394 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
3395 llvm::ConstantArray::get(Ident->getName()),
3396 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003397 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003398
3399 return getConstantGEP(Entry, 0, 0);
3400}
3401
3402// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003403// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003404llvm::Constant *
3405 CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3406 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003407 std::string TypeStr;
3408 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003409 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3410}
3411
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003412void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
3413 const ObjCContainerDecl *CD,
3414 std::string &NameOut) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00003415 NameOut = '\01';
3416 NameOut += (D->isInstanceMethod() ? '-' : '+');
Chris Lattner077bf5e2008-11-24 03:33:13 +00003417 NameOut += '[';
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003418 assert (CD && "Missing container decl in GetNameForMethod");
3419 NameOut += CD->getNameAsString();
Fariborz Jahanian1e9aef32009-04-16 18:34:20 +00003420 if (const ObjCCategoryImplDecl *CID =
3421 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext())) {
3422 NameOut += '(';
3423 NameOut += CID->getNameAsString();
3424 NameOut+= ')';
3425 }
Chris Lattner077bf5e2008-11-24 03:33:13 +00003426 NameOut += ' ';
3427 NameOut += D->getSelector().getAsString();
3428 NameOut += ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003429}
3430
Fariborz Jahanianc38e9af2009-06-23 21:47:46 +00003431void CGObjCCommonMac::MergeMetadataGlobals(
3432 std::vector<llvm::Constant*> &UsedArray) {
3433 llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3434 for (std::vector<llvm::GlobalVariable*>::iterator i = UsedGlobals.begin(),
3435 e = UsedGlobals.end(); i != e; ++i) {
3436 UsedArray.push_back(llvm::ConstantExpr::getBitCast(cast<llvm::Constant>(*i),
3437 i8PTy));
3438 }
3439}
3440
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003441void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003442 EmitModuleInfo();
3443
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003444 // Emit the dummy bodies for any protocols which were referenced but
3445 // never defined.
3446 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
3447 i = Protocols.begin(), e = Protocols.end(); i != e; ++i) {
3448 if (i->second->hasInitializer())
3449 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003450
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003451 std::vector<llvm::Constant*> Values(5);
3452 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
3453 Values[1] = GetClassName(i->first);
3454 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
3455 Values[3] = Values[4] =
3456 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
3457 i->second->setLinkage(llvm::GlobalValue::InternalLinkage);
3458 i->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
3459 Values));
3460 }
3461
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003462 // Add assembler directives to add lazy undefined symbol references
3463 // for classes which are referenced but not defined. This is
3464 // important for correct linker interaction.
3465
3466 // FIXME: Uh, this isn't particularly portable.
3467 std::stringstream s;
Anders Carlsson565c99f2008-12-10 02:21:04 +00003468
3469 if (!CGM.getModule().getModuleInlineAsm().empty())
3470 s << "\n";
3471
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003472 for (std::set<IdentifierInfo*>::iterator i = LazySymbols.begin(),
3473 e = LazySymbols.end(); i != e; ++i) {
3474 s << "\t.lazy_reference .objc_class_name_" << (*i)->getName() << "\n";
3475 }
3476 for (std::set<IdentifierInfo*>::iterator i = DefinedSymbols.begin(),
3477 e = DefinedSymbols.end(); i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003478 s << "\t.objc_class_name_" << (*i)->getName() << "=0\n"
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003479 << "\t.globl .objc_class_name_" << (*i)->getName() << "\n";
3480 }
Anders Carlsson565c99f2008-12-10 02:21:04 +00003481
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003482 CGM.getModule().appendModuleInlineAsm(s.str());
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003483}
3484
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003485CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003486 : CGObjCCommonMac(cgm),
3487 ObjCTypes(cgm)
3488{
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003489 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003490 ObjCABI = 2;
3491}
3492
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003493/* *** */
3494
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003495ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
3496: CGM(cgm)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003497{
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003498 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3499 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003500
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003501 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003502 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003503 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003504 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003505 Int8PtrTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
3506
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003507 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Fariborz Jahanian6d657c42008-11-18 20:18:11 +00003508 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003509 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003510
Mike Stumpf5408fe2009-05-16 07:57:57 +00003511 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3512 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003513 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
3514 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003515
3516 // I'm not sure I like this. The implicit coordination is a bit
3517 // gross. We should solve this in a reasonable fashion because this
3518 // is a pretty common task (match some runtime data structure with
3519 // an LLVM data structure).
3520
3521 // FIXME: This is leaked.
3522 // FIXME: Merge with rewriter code?
3523
3524 // struct _objc_super {
3525 // id self;
3526 // Class cls;
3527 // }
3528 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3529 SourceLocation(),
3530 &Ctx.Idents.get("_objc_super"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003531 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3532 Ctx.getObjCIdType(), 0, false));
3533 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3534 Ctx.getObjCClassType(), 0, false));
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003535 RD->completeDefinition(Ctx);
3536
3537 SuperCTy = Ctx.getTagDeclType(RD);
3538 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
3539
3540 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003541 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3542
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003543 // struct _prop_t {
3544 // char *name;
3545 // char *attributes;
3546 // }
Chris Lattner1c02f862009-04-22 02:53:24 +00003547 PropertyTy = llvm::StructType::get(Int8PtrTy, Int8PtrTy, NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003548 CGM.getModule().addTypeName("struct._prop_t",
3549 PropertyTy);
3550
3551 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003552 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003553 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003554 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003555 // }
3556 PropertyListTy = llvm::StructType::get(IntTy,
3557 IntTy,
3558 llvm::ArrayType::get(PropertyTy, 0),
3559 NULL);
3560 CGM.getModule().addTypeName("struct._prop_list_t",
3561 PropertyListTy);
3562 // struct _prop_list_t *
3563 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
3564
3565 // struct _objc_method {
3566 // SEL _cmd;
3567 // char *method_type;
3568 // char *_imp;
3569 // }
3570 MethodTy = llvm::StructType::get(SelectorPtrTy,
3571 Int8PtrTy,
3572 Int8PtrTy,
3573 NULL);
3574 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003575
3576 // struct _objc_cache *
3577 CacheTy = llvm::OpaqueType::get();
3578 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
3579 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003580}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003581
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003582ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
3583 : ObjCCommonTypesHelper(cgm)
3584{
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003585 // struct _objc_method_description {
3586 // SEL name;
3587 // char *types;
3588 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003589 MethodDescriptionTy =
3590 llvm::StructType::get(SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003591 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003592 NULL);
3593 CGM.getModule().addTypeName("struct._objc_method_description",
3594 MethodDescriptionTy);
3595
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003596 // struct _objc_method_description_list {
3597 // int count;
3598 // struct _objc_method_description[1];
3599 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003600 MethodDescriptionListTy =
3601 llvm::StructType::get(IntTy,
3602 llvm::ArrayType::get(MethodDescriptionTy, 0),
3603 NULL);
3604 CGM.getModule().addTypeName("struct._objc_method_description_list",
3605 MethodDescriptionListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003606
3607 // struct _objc_method_description_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003608 MethodDescriptionListPtrTy =
3609 llvm::PointerType::getUnqual(MethodDescriptionListTy);
3610
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003611 // Protocol description structures
3612
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003613 // struct _objc_protocol_extension {
3614 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3615 // struct _objc_method_description_list *optional_instance_methods;
3616 // struct _objc_method_description_list *optional_class_methods;
3617 // struct _objc_property_list *instance_properties;
3618 // }
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003619 ProtocolExtensionTy =
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003620 llvm::StructType::get(IntTy,
3621 MethodDescriptionListPtrTy,
3622 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003623 PropertyListPtrTy,
3624 NULL);
3625 CGM.getModule().addTypeName("struct._objc_protocol_extension",
3626 ProtocolExtensionTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003627
3628 // struct _objc_protocol_extension *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003629 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
3630
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003631 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003632
3633 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get();
3634 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3635
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003636 const llvm::Type *T =
3637 llvm::StructType::get(llvm::PointerType::getUnqual(ProtocolListTyHolder),
3638 LongTy,
3639 llvm::ArrayType::get(ProtocolTyHolder, 0),
3640 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003641 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3642
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003643 // struct _objc_protocol {
3644 // struct _objc_protocol_extension *isa;
3645 // char *protocol_name;
3646 // struct _objc_protocol **_objc_protocol_list;
3647 // struct _objc_method_description_list *instance_methods;
3648 // struct _objc_method_description_list *class_methods;
3649 // }
3650 T = llvm::StructType::get(ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003651 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003652 llvm::PointerType::getUnqual(ProtocolListTyHolder),
3653 MethodDescriptionListPtrTy,
3654 MethodDescriptionListPtrTy,
3655 NULL);
3656 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3657
3658 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
3659 CGM.getModule().addTypeName("struct._objc_protocol_list",
3660 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003661 // struct _objc_protocol_list *
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003662 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
3663
3664 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003665 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003666 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003667
3668 // Class description structures
3669
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003670 // struct _objc_ivar {
3671 // char *ivar_name;
3672 // char *ivar_type;
3673 // int ivar_offset;
3674 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003675 IvarTy = llvm::StructType::get(Int8PtrTy,
3676 Int8PtrTy,
3677 IntTy,
3678 NULL);
3679 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3680
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003681 // struct _objc_ivar_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003682 IvarListTy = llvm::OpaqueType::get();
3683 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
3684 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
3685
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003686 // struct _objc_method_list *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003687 MethodListTy = llvm::OpaqueType::get();
3688 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
3689 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
3690
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003691 // struct _objc_class_extension *
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003692 ClassExtensionTy =
3693 llvm::StructType::get(IntTy,
3694 Int8PtrTy,
3695 PropertyListPtrTy,
3696 NULL);
3697 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
3698 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
3699
3700 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3701
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003702 // struct _objc_class {
3703 // Class isa;
3704 // Class super_class;
3705 // char *name;
3706 // long version;
3707 // long info;
3708 // long instance_size;
3709 // struct _objc_ivar_list *ivars;
3710 // struct _objc_method_list *methods;
3711 // struct _objc_cache *cache;
3712 // struct _objc_protocol_list *protocols;
3713 // char *ivar_layout;
3714 // struct _objc_class_ext *ext;
3715 // };
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003716 T = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3717 llvm::PointerType::getUnqual(ClassTyHolder),
3718 Int8PtrTy,
3719 LongTy,
3720 LongTy,
3721 LongTy,
3722 IvarListPtrTy,
3723 MethodListPtrTy,
3724 CachePtrTy,
3725 ProtocolListPtrTy,
3726 Int8PtrTy,
3727 ClassExtensionPtrTy,
3728 NULL);
3729 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
3730
3731 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
3732 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
3733 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
3734
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003735 // struct _objc_category {
3736 // char *category_name;
3737 // char *class_name;
3738 // struct _objc_method_list *instance_method;
3739 // struct _objc_method_list *class_method;
3740 // uint32_t size; // sizeof(struct _objc_category)
3741 // struct _objc_property_list *instance_properties;// category's @property
3742 // }
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003743 CategoryTy = llvm::StructType::get(Int8PtrTy,
3744 Int8PtrTy,
3745 MethodListPtrTy,
3746 MethodListPtrTy,
3747 ProtocolListPtrTy,
3748 IntTy,
3749 PropertyListPtrTy,
3750 NULL);
3751 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
3752
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003753 // Global metadata structures
3754
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003755 // struct _objc_symtab {
3756 // long sel_ref_cnt;
3757 // SEL *refs;
3758 // short cls_def_cnt;
3759 // short cat_def_cnt;
3760 // char *defs[cls_def_cnt + cat_def_cnt];
3761 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003762 SymtabTy = llvm::StructType::get(LongTy,
3763 SelectorPtrTy,
3764 ShortTy,
3765 ShortTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003766 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003767 NULL);
3768 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
3769 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
3770
Fariborz Jahaniandb286862009-01-22 00:37:21 +00003771 // struct _objc_module {
3772 // long version;
3773 // long size; // sizeof(struct _objc_module)
3774 // char *name;
3775 // struct _objc_symtab* symtab;
3776 // }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003777 ModuleTy =
3778 llvm::StructType::get(LongTy,
3779 LongTy,
3780 Int8PtrTy,
3781 SymtabPtrTy,
3782 NULL);
3783 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00003784
Anders Carlsson2abd89c2008-08-31 04:05:03 +00003785
Mike Stumpf5408fe2009-05-16 07:57:57 +00003786 // FIXME: This is the size of the setjmp buffer and should be target
3787 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00003788 uint64_t SetJmpBufferSize = 18;
3789
3790 // Exceptions
3791 const llvm::Type *StackPtrTy =
Daniel Dunbar10004912008-09-27 06:32:25 +00003792 llvm::ArrayType::get(llvm::PointerType::getUnqual(llvm::Type::Int8Ty), 4);
Anders Carlsson124526b2008-09-09 10:10:21 +00003793
3794 ExceptionDataTy =
3795 llvm::StructType::get(llvm::ArrayType::get(llvm::Type::Int32Ty,
3796 SetJmpBufferSize),
3797 StackPtrTy, NULL);
3798 CGM.getModule().addTypeName("struct._objc_exception_data",
3799 ExceptionDataTy);
3800
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00003801}
3802
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003803ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003804: ObjCCommonTypesHelper(cgm)
3805{
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003806 // struct _method_list_t {
3807 // uint32_t entsize; // sizeof(struct _objc_method)
3808 // uint32_t method_count;
3809 // struct _objc_method method_list[method_count];
3810 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003811 MethodListnfABITy = llvm::StructType::get(IntTy,
3812 IntTy,
3813 llvm::ArrayType::get(MethodTy, 0),
3814 NULL);
3815 CGM.getModule().addTypeName("struct.__method_list_t",
3816 MethodListnfABITy);
3817 // struct method_list_t *
3818 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003819
3820 // struct _protocol_t {
3821 // id isa; // NULL
3822 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003823 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003824 // const struct method_list_t * const instance_methods;
3825 // const struct method_list_t * const class_methods;
3826 // const struct method_list_t *optionalInstanceMethods;
3827 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003828 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003829 // const uint32_t size; // sizeof(struct _protocol_t)
3830 // const uint32_t flags; // = 0
3831 // }
3832
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003833 // Holder for struct _protocol_list_t *
3834 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get();
3835
3836 ProtocolnfABITy = llvm::StructType::get(ObjectPtrTy,
3837 Int8PtrTy,
3838 llvm::PointerType::getUnqual(
3839 ProtocolListTyHolder),
3840 MethodListnfABIPtrTy,
3841 MethodListnfABIPtrTy,
3842 MethodListnfABIPtrTy,
3843 MethodListnfABIPtrTy,
3844 PropertyListPtrTy,
3845 IntTy,
3846 IntTy,
3847 NULL);
3848 CGM.getModule().addTypeName("struct._protocol_t",
3849 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003850
3851 // struct _protocol_t*
3852 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003853
Fariborz Jahanianda320092009-01-29 19:24:30 +00003854 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003855 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00003856 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003857 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003858 ProtocolListnfABITy = llvm::StructType::get(LongTy,
3859 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00003860 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003861 NULL);
3862 CGM.getModule().addTypeName("struct._objc_protocol_list",
3863 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00003864 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
3865 ProtocolListnfABITy);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003866
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003867 // struct _objc_protocol_list*
3868 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003869
3870 // struct _ivar_t {
3871 // unsigned long int *offset; // pointer to ivar offset location
3872 // char *name;
3873 // char *type;
3874 // uint32_t alignment;
3875 // uint32_t size;
3876 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003877 IvarnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(LongTy),
3878 Int8PtrTy,
3879 Int8PtrTy,
3880 IntTy,
3881 IntTy,
3882 NULL);
3883 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
3884
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003885 // struct _ivar_list_t {
3886 // uint32 entsize; // sizeof(struct _ivar_t)
3887 // uint32 count;
3888 // struct _iver_t list[count];
3889 // }
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003890 IvarListnfABITy = llvm::StructType::get(IntTy,
3891 IntTy,
3892 llvm::ArrayType::get(
3893 IvarnfABITy, 0),
3894 NULL);
3895 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
3896
3897 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003898
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003899 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003900 // uint32_t const flags;
3901 // uint32_t const instanceStart;
3902 // uint32_t const instanceSize;
3903 // uint32_t const reserved; // only when building for 64bit targets
3904 // const uint8_t * const ivarLayout;
3905 // const char *const name;
3906 // const struct _method_list_t * const baseMethods;
3907 // const struct _objc_protocol_list *const baseProtocols;
3908 // const struct _ivar_list_t *const ivars;
3909 // const uint8_t * const weakIvarLayout;
3910 // const struct _prop_list_t * const properties;
3911 // }
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003912
3913 // FIXME. Add 'reserved' field in 64bit abi mode!
3914 ClassRonfABITy = llvm::StructType::get(IntTy,
3915 IntTy,
3916 IntTy,
3917 Int8PtrTy,
3918 Int8PtrTy,
3919 MethodListnfABIPtrTy,
3920 ProtocolListnfABIPtrTy,
3921 IvarListnfABIPtrTy,
3922 Int8PtrTy,
3923 PropertyListPtrTy,
3924 NULL);
3925 CGM.getModule().addTypeName("struct._class_ro_t",
3926 ClassRonfABITy);
3927
3928 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
3929 std::vector<const llvm::Type*> Params;
3930 Params.push_back(ObjectPtrTy);
3931 Params.push_back(SelectorPtrTy);
3932 ImpnfABITy = llvm::PointerType::getUnqual(
3933 llvm::FunctionType::get(ObjectPtrTy, Params, false));
3934
3935 // struct _class_t {
3936 // struct _class_t *isa;
3937 // struct _class_t * const superclass;
3938 // void *cache;
3939 // IMP *vtable;
3940 // struct class_ro_t *ro;
3941 // }
3942
3943 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get();
3944 ClassnfABITy = llvm::StructType::get(llvm::PointerType::getUnqual(ClassTyHolder),
3945 llvm::PointerType::getUnqual(ClassTyHolder),
3946 CachePtrTy,
3947 llvm::PointerType::getUnqual(ImpnfABITy),
3948 llvm::PointerType::getUnqual(
3949 ClassRonfABITy),
3950 NULL);
3951 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
3952
3953 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
3954 ClassnfABITy);
3955
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003956 // LLVM for struct _class_t *
3957 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
3958
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003959 // struct _category_t {
3960 // const char * const name;
3961 // struct _class_t *const cls;
3962 // const struct _method_list_t * const instance_methods;
3963 // const struct _method_list_t * const class_methods;
3964 // const struct _protocol_list_t * const protocols;
3965 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003966 // }
3967 CategorynfABITy = llvm::StructType::get(Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003968 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00003969 MethodListnfABIPtrTy,
3970 MethodListnfABIPtrTy,
3971 ProtocolListnfABIPtrTy,
3972 PropertyListPtrTy,
3973 NULL);
3974 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003975
3976 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003977 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3978 ASTContext &Ctx = CGM.getContext();
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003979
3980 // MessageRefTy - LLVM for:
3981 // struct _message_ref_t {
3982 // IMP messenger;
3983 // SEL name;
3984 // };
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003985
3986 // First the clang type for struct _message_ref_t
3987 RecordDecl *RD = RecordDecl::Create(Ctx, TagDecl::TK_struct, 0,
3988 SourceLocation(),
3989 &Ctx.Idents.get("_message_ref_t"));
Douglas Gregor6ab35242009-04-09 21:40:53 +00003990 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3991 Ctx.VoidPtrTy, 0, false));
3992 RD->addDecl(Ctx, FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
3993 Ctx.getObjCSelType(), 0, false));
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00003994 RD->completeDefinition(Ctx);
3995
3996 MessageRefCTy = Ctx.getTagDeclType(RD);
3997 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
3998 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00003999
4000 // MessageRefPtrTy - LLVM for struct _message_ref_t*
4001 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
4002
4003 // SuperMessageRefTy - LLVM for:
4004 // struct _super_message_ref_t {
4005 // SUPER_IMP messenger;
4006 // SEL name;
4007 // };
4008 SuperMessageRefTy = llvm::StructType::get(ImpnfABITy,
4009 SelectorPtrTy,
4010 NULL);
4011 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
4012
4013 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
4014 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4015
Daniel Dunbare588b992009-03-01 04:46:24 +00004016
4017 // struct objc_typeinfo {
4018 // const void** vtable; // objc_ehtype_vtable + 2
4019 // const char* name; // c++ typeinfo string
4020 // Class cls;
4021 // };
4022 EHTypeTy = llvm::StructType::get(llvm::PointerType::getUnqual(Int8PtrTy),
4023 Int8PtrTy,
4024 ClassnfABIPtrTy,
4025 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004026 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Daniel Dunbare588b992009-03-01 04:46:24 +00004027 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004028}
4029
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004030llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
4031 FinishNonFragileABIModule();
4032
4033 return NULL;
4034}
4035
Daniel Dunbar463b8762009-05-15 21:48:48 +00004036void CGObjCNonFragileABIMac::AddModuleClassList(const
4037 std::vector<llvm::GlobalValue*>
4038 &Container,
4039 const char *SymbolName,
4040 const char *SectionName) {
4041 unsigned NumClasses = Container.size();
4042
4043 if (!NumClasses)
4044 return;
4045
4046 std::vector<llvm::Constant*> Symbols(NumClasses);
4047 for (unsigned i=0; i<NumClasses; i++)
4048 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
4049 ObjCTypes.Int8PtrTy);
4050 llvm::Constant* Init =
4051 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
4052 NumClasses),
4053 Symbols);
4054
4055 llvm::GlobalVariable *GV =
4056 new llvm::GlobalVariable(Init->getType(), false,
4057 llvm::GlobalValue::InternalLinkage,
4058 Init,
4059 SymbolName,
4060 &CGM.getModule());
4061 GV->setAlignment(8);
4062 GV->setSection(SectionName);
4063 UsedGlobals.push_back(GV);
4064}
4065
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004066void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4067 // nonfragile abi has no module definition.
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004068
Daniel Dunbar463b8762009-05-15 21:48:48 +00004069 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004070 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004071 AddModuleClassList(DefinedClasses,
4072 "\01L_OBJC_LABEL_CLASS_$",
4073 "__DATA, __objc_classlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004074 AddModuleClassList(DefinedNonLazyClasses,
4075 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4076 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004077
4078 // Build list of all implemented category addresses in array
4079 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar463b8762009-05-15 21:48:48 +00004080 AddModuleClassList(DefinedCategories,
4081 "\01L_OBJC_LABEL_CATEGORY_$",
4082 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004083 AddModuleClassList(DefinedNonLazyCategories,
4084 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4085 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004086
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004087 // static int L_OBJC_IMAGE_INFO[2] = { 0, flags };
4088 // FIXME. flags can be 0 | 1 | 2 | 6. For now just use 0
4089 std::vector<llvm::Constant*> Values(2);
4090 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, 0);
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004091 unsigned int flags = 0;
Fariborz Jahanian66a5c2c2009-02-24 23:34:44 +00004092 // FIXME: Fix and continue?
4093 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
4094 flags |= eImageInfo_GarbageCollected;
4095 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
4096 flags |= eImageInfo_GCOnly;
Fariborz Jahanian067986e2009-02-24 21:08:09 +00004097 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004098 llvm::Constant* Init = llvm::ConstantArray::get(
4099 llvm::ArrayType::get(ObjCTypes.IntTy, 2),
4100 Values);
4101 llvm::GlobalVariable *IMGV =
4102 new llvm::GlobalVariable(Init->getType(), false,
4103 llvm::GlobalValue::InternalLinkage,
4104 Init,
4105 "\01L_OBJC_IMAGE_INFO",
4106 &CGM.getModule());
4107 IMGV->setSection("__DATA, __objc_imageinfo, regular, no_dead_strip");
Daniel Dunbar325f7582009-04-23 08:03:21 +00004108 IMGV->setConstant(true);
Fariborz Jahanian0f6610e2009-01-30 22:07:48 +00004109 UsedGlobals.push_back(IMGV);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004110}
4111
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004112/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004113/// NonLegacyDispatchMethods; false otherwise. What this means is that
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004114/// except for the 19 selectors in the list, we generate 32bit-style
4115/// message dispatch call for all the rest.
4116///
4117bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004118 if (NonLegacyDispatchMethods.empty()) {
4119 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4120 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4121 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4122 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4123 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4124 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4125 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4126 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4127 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4128 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
4129
4130 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4131 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4132 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4133 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4134 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4135 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4136 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4137 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004138 // "countByEnumeratingWithState:objects:count"
4139 IdentifierInfo *KeyIdents[] = {
4140 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4141 &CGM.getContext().Idents.get("objects"),
4142 &CGM.getContext().Idents.get("count")
4143 };
4144 NonLegacyDispatchMethods.insert(
4145 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004146 }
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004147 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004148}
4149
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004150// Metadata flags
4151enum MetaDataDlags {
4152 CLS = 0x0,
4153 CLS_META = 0x1,
4154 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004155 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004156 CLS_EXCEPTION = 0x20
4157};
4158/// BuildClassRoTInitializer - generate meta-data for:
4159/// struct _class_ro_t {
4160/// uint32_t const flags;
4161/// uint32_t const instanceStart;
4162/// uint32_t const instanceSize;
4163/// uint32_t const reserved; // only when building for 64bit targets
4164/// const uint8_t * const ivarLayout;
4165/// const char *const name;
4166/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004167/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004168/// const struct _ivar_list_t *const ivars;
4169/// const uint8_t * const weakIvarLayout;
4170/// const struct _prop_list_t * const properties;
4171/// }
4172///
4173llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
4174 unsigned flags,
4175 unsigned InstanceStart,
4176 unsigned InstanceSize,
4177 const ObjCImplementationDecl *ID) {
4178 std::string ClassName = ID->getNameAsString();
4179 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
4180 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4181 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4182 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
4183 // FIXME. For 64bit targets add 0 here.
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004184 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4185 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004186 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004187 // const struct _method_list_t * const baseMethods;
4188 std::vector<llvm::Constant*> Methods;
4189 std::string MethodListName("\01l_OBJC_$_");
4190 if (flags & CLS_META) {
4191 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004192 for (ObjCImplementationDecl::classmeth_iterator
4193 i = ID->classmeth_begin(CGM.getContext()),
4194 e = ID->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004195 // Class methods should always be defined.
4196 Methods.push_back(GetMethodConstant(*i));
4197 }
4198 } else {
4199 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004200 for (ObjCImplementationDecl::instmeth_iterator
4201 i = ID->instmeth_begin(CGM.getContext()),
4202 e = ID->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004203 // Instance methods should always be defined.
4204 Methods.push_back(GetMethodConstant(*i));
4205 }
Douglas Gregor653f1b12009-04-23 01:02:12 +00004206 for (ObjCImplementationDecl::propimpl_iterator
4207 i = ID->propimpl_begin(CGM.getContext()),
4208 e = ID->propimpl_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004209 ObjCPropertyImplDecl *PID = *i;
4210
4211 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4212 ObjCPropertyDecl *PD = PID->getPropertyDecl();
4213
4214 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4215 if (llvm::Constant *C = GetMethodConstant(MD))
4216 Methods.push_back(C);
4217 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4218 if (llvm::Constant *C = GetMethodConstant(MD))
4219 Methods.push_back(C);
4220 }
4221 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004222 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004223 Values[ 5] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004224 "__DATA, __objc_const", Methods);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004225
4226 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4227 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
4228 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
4229 + OID->getNameAsString(),
4230 OID->protocol_begin(),
4231 OID->protocol_end());
4232
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004233 if (flags & CLS_META)
4234 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4235 else
4236 Values[ 7] = EmitIvarList(ID);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00004237 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4238 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004239 if (flags & CLS_META)
4240 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4241 else
4242 Values[ 9] =
4243 EmitPropertyList(
4244 "\01l_OBJC_$_PROP_LIST_" + ID->getNameAsString(),
4245 ID, ID->getClassInterface(), ObjCTypes);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004246 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
4247 Values);
4248 llvm::GlobalVariable *CLASS_RO_GV =
4249 new llvm::GlobalVariable(ObjCTypes.ClassRonfABITy, false,
4250 llvm::GlobalValue::InternalLinkage,
4251 Init,
4252 (flags & CLS_META) ?
4253 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4254 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName,
4255 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004256 CLASS_RO_GV->setAlignment(
4257 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004258 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004259 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004260
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004261}
4262
4263/// BuildClassMetaData - This routine defines that to-level meta-data
4264/// for the given ClassName for:
4265/// struct _class_t {
4266/// struct _class_t *isa;
4267/// struct _class_t * const superclass;
4268/// void *cache;
4269/// IMP *vtable;
4270/// struct class_ro_t *ro;
4271/// }
4272///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004273llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
4274 std::string &ClassName,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004275 llvm::Constant *IsAGV,
4276 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004277 llvm::Constant *ClassRoGV,
4278 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004279 std::vector<llvm::Constant*> Values(5);
4280 Values[0] = IsAGV;
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004281 Values[1] = SuperClassGV
4282 ? SuperClassGV
4283 : llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004284 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4285 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4286 Values[4] = ClassRoGV; // &CLASS_RO_GV
4287 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
4288 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004289 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4290 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004291 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004292 GV->setAlignment(
4293 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004294 if (HiddenVisibility)
4295 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004296 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004297}
4298
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004299bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004300CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
4301 return OD->getClassMethod(CGM.getContext(), GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004302}
4303
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004304void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004305 uint32_t &InstanceStart,
4306 uint32_t &InstanceSize) {
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004307 const ASTRecordLayout &RL =
4308 CGM.getContext().getASTObjCImplementationLayout(OID);
4309
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004310 // InstanceSize is really instance end.
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004311 InstanceSize = llvm::RoundUpToAlignment(RL.getNextOffset(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004312
4313 // If there are no fields, the start is the same as the end.
4314 if (!RL.getFieldCount())
4315 InstanceStart = InstanceSize;
4316 else
4317 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004318}
4319
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004320void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4321 std::string ClassName = ID->getNameAsString();
4322 if (!ObjCEmptyCacheVar) {
4323 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004324 ObjCTypes.CacheTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004325 false,
4326 llvm::GlobalValue::ExternalLinkage,
4327 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004328 "_objc_empty_cache",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004329 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004330
4331 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004332 ObjCTypes.ImpnfABITy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004333 false,
4334 llvm::GlobalValue::ExternalLinkage,
4335 0,
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004336 "_objc_empty_vtable",
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004337 &CGM.getModule());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004338 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004339 assert(ID->getClassInterface() &&
4340 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004341 // FIXME: Is this correct (that meta class size is never computed)?
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004342 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004343 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004344 uint32_t InstanceSize = InstanceStart;
4345 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004346 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4347 std::string ObjCClassName(getClassSymbolPrefix());
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004348
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004349 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004350
Daniel Dunbar04d40782009-04-14 06:00:08 +00004351 bool classIsHidden =
4352 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004353 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004354 flags |= OBJC2_CLS_HIDDEN;
4355 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004356 // class is root
4357 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004358 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004359 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004360 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004361 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004362 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4363 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4364 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004365 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004366 // work on super class metadata symbol.
4367 std::string SuperClassName =
4368 ObjCMetaClassName + ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004369 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004370 }
4371 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4372 InstanceStart,
4373 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004374 std::string TClassName = ObjCMetaClassName + ClassName;
4375 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004376 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4377 classIsHidden);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004378
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004379 // Metadata for the class
4380 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004381 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004382 flags |= OBJC2_CLS_HIDDEN;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004383
Douglas Gregor68584ed2009-06-18 16:11:24 +00004384 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004385 flags |= CLS_EXCEPTION;
4386
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004387 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004388 flags |= CLS_ROOT;
4389 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004390 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004391 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004392 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004393 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004394 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004395 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004396 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004397 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004398 InstanceStart,
4399 InstanceSize,
4400 ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004401
4402 TClassName = ObjCClassName + ClassName;
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004403 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004404 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4405 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004406 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004407
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004408 // Determine if this class is also "non-lazy".
4409 if (ImplementationIsNonLazy(ID))
4410 DefinedNonLazyClasses.push_back(ClassMD);
4411
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004412 // Force the definition of the EHType if necessary.
4413 if (flags & CLS_EXCEPTION)
4414 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004415}
4416
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004417/// GenerateProtocolRef - This routine is called to generate code for
4418/// a protocol reference expression; as in:
4419/// @code
4420/// @protocol(Proto1);
4421/// @endcode
4422/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4423/// which will hold address of the protocol meta-data.
4424///
4425llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
4426 const ObjCProtocolDecl *PD) {
4427
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004428 // This routine is called for @protocol only. So, we must build definition
4429 // of protocol's meta-data (not a reference to it!)
4430 //
4431 llvm::Constant *Init = llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004432 ObjCTypes.ExternalProtocolPtrTy);
4433
4434 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4435 ProtocolName += PD->getNameAsCString();
4436
4437 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4438 if (PTGV)
4439 return Builder.CreateLoad(PTGV, false, "tmp");
4440 PTGV = new llvm::GlobalVariable(
4441 Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00004442 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004443 Init,
4444 ProtocolName,
4445 &CGM.getModule());
4446 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4447 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4448 UsedGlobals.push_back(PTGV);
4449 return Builder.CreateLoad(PTGV, false, "tmp");
4450}
4451
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004452/// GenerateCategory - Build metadata for a category implementation.
4453/// struct _category_t {
4454/// const char * const name;
4455/// struct _class_t *const cls;
4456/// const struct _method_list_t * const instance_methods;
4457/// const struct _method_list_t * const class_methods;
4458/// const struct _protocol_list_t * const protocols;
4459/// const struct _prop_list_t * const properties;
4460/// }
4461///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004462void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004463 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004464 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
4465 std::string ExtCatName(Prefix + Interface->getNameAsString()+
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004466 "_$_" + OCD->getNameAsString());
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004467 std::string ExtClassName(getClassSymbolPrefix() +
4468 Interface->getNameAsString());
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004469
4470 std::vector<llvm::Constant*> Values(6);
4471 Values[0] = GetClassName(OCD->getIdentifier());
4472 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004473 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004474 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004475 std::vector<llvm::Constant*> Methods;
4476 std::string MethodListName(Prefix);
4477 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
4478 "_$_" + OCD->getNameAsString();
4479
Douglas Gregor653f1b12009-04-23 01:02:12 +00004480 for (ObjCCategoryImplDecl::instmeth_iterator
4481 i = OCD->instmeth_begin(CGM.getContext()),
4482 e = OCD->instmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004483 // Instance methods should always be defined.
4484 Methods.push_back(GetMethodConstant(*i));
4485 }
4486
4487 Values[2] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004488 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004489 Methods);
4490
4491 MethodListName = Prefix;
4492 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4493 OCD->getNameAsString();
4494 Methods.clear();
Douglas Gregor653f1b12009-04-23 01:02:12 +00004495 for (ObjCCategoryImplDecl::classmeth_iterator
4496 i = OCD->classmeth_begin(CGM.getContext()),
4497 e = OCD->classmeth_end(CGM.getContext()); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004498 // Class methods should always be defined.
4499 Methods.push_back(GetMethodConstant(*i));
4500 }
4501
4502 Values[3] = EmitMethodList(MethodListName,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004503 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004504 Methods);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004505 const ObjCCategoryDecl *Category =
4506 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004507 if (Category) {
4508 std::string ExtName(Interface->getNameAsString() + "_$_" +
4509 OCD->getNameAsString());
4510 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
4511 + Interface->getNameAsString() + "_$_"
4512 + Category->getNameAsString(),
4513 Category->protocol_begin(),
4514 Category->protocol_end());
4515 Values[5] =
4516 EmitPropertyList(std::string("\01l_OBJC_$_PROP_LIST_") + ExtName,
4517 OCD, Category, ObjCTypes);
4518 }
4519 else {
4520 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4521 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
4522 }
4523
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004524 llvm::Constant *Init =
4525 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
4526 Values);
4527 llvm::GlobalVariable *GCATV
4528 = new llvm::GlobalVariable(ObjCTypes.CategorynfABITy,
4529 false,
4530 llvm::GlobalValue::InternalLinkage,
4531 Init,
4532 ExtCatName,
4533 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004534 GCATV->setAlignment(
4535 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004536 GCATV->setSection("__DATA, __objc_const");
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004537 UsedGlobals.push_back(GCATV);
4538 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004539
4540 // Determine if this category is also "non-lazy".
4541 if (ImplementationIsNonLazy(OCD))
4542 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004543}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004544
4545/// GetMethodConstant - Return a struct objc_method constant for the
4546/// given method if it has been defined. The result is null if the
4547/// method has not been defined. The return value has type MethodPtrTy.
4548llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
4549 const ObjCMethodDecl *MD) {
4550 // FIXME: Use DenseMap::lookup
4551 llvm::Function *Fn = MethodDefinitions[MD];
4552 if (!Fn)
4553 return 0;
4554
4555 std::vector<llvm::Constant*> Method(3);
4556 Method[0] =
4557 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4558 ObjCTypes.SelectorPtrTy);
4559 Method[1] = GetMethodVarType(MD);
4560 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
4561 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
4562}
4563
4564/// EmitMethodList - Build meta-data for method declarations
4565/// struct _method_list_t {
4566/// uint32_t entsize; // sizeof(struct _objc_method)
4567/// uint32_t method_count;
4568/// struct _objc_method method_list[method_count];
4569/// }
4570///
4571llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(
4572 const std::string &Name,
4573 const char *Section,
4574 const ConstantVector &Methods) {
4575 // Return null for empty list.
4576 if (Methods.empty())
4577 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
4578
4579 std::vector<llvm::Constant*> Values(3);
4580 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004581 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004582 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4583 // method_count
4584 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
4585 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
4586 Methods.size());
4587 Values[2] = llvm::ConstantArray::get(AT, Methods);
4588 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4589
4590 llvm::GlobalVariable *GV =
4591 new llvm::GlobalVariable(Init->getType(), false,
4592 llvm::GlobalValue::InternalLinkage,
4593 Init,
4594 Name,
4595 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004596 GV->setAlignment(
4597 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004598 GV->setSection(Section);
4599 UsedGlobals.push_back(GV);
4600 return llvm::ConstantExpr::getBitCast(GV,
4601 ObjCTypes.MethodListnfABIPtrTy);
4602}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004603
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004604/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4605/// the given ivar.
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004606llvm::GlobalVariable * CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(
Fariborz Jahanian01a0c362009-02-12 18:51:23 +00004607 const ObjCInterfaceDecl *ID,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004608 const ObjCIvarDecl *Ivar) {
Daniel Dunbara81419d2009-05-05 00:36:57 +00004609 // FIXME: We shouldn't need to do this lookup.
4610 unsigned Index;
4611 const ObjCInterfaceDecl *Container =
4612 FindIvarInterface(CGM.getContext(), ID, Ivar, Index);
4613 assert(Container && "Unable to find ivar container!");
4614 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004615 '.' + Ivar->getNameAsString();
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004616 llvm::GlobalVariable *IvarOffsetGV =
4617 CGM.getModule().getGlobalVariable(Name);
4618 if (!IvarOffsetGV)
4619 IvarOffsetGV =
4620 new llvm::GlobalVariable(ObjCTypes.LongTy,
4621 false,
4622 llvm::GlobalValue::ExternalLinkage,
4623 0,
4624 Name,
4625 &CGM.getModule());
4626 return IvarOffsetGV;
4627}
4628
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004629llvm::Constant * CGObjCNonFragileABIMac::EmitIvarOffsetVar(
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004630 const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004631 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004632 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004633 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
4634 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
4635 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004636 IvarOffsetGV->setAlignment(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004637 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004638
Mike Stumpf5408fe2009-05-16 07:57:57 +00004639 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4640 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004641 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4642 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4643 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004644 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004645 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004646 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004647 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004648 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004649}
4650
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004651/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00004652/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004653/// IvarListnfABIPtrTy.
4654/// struct _ivar_t {
4655/// unsigned long int *offset; // pointer to ivar offset location
4656/// char *name;
4657/// char *type;
4658/// uint32_t alignment;
4659/// uint32_t size;
4660/// }
4661/// struct _ivar_list_t {
4662/// uint32 entsize; // sizeof(struct _ivar_t)
4663/// uint32 count;
4664/// struct _iver_t list[count];
4665/// }
4666///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004667
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004668llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
4669 const ObjCImplementationDecl *ID) {
4670
4671 std::vector<llvm::Constant*> Ivars, Ivar(5);
4672
4673 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4674 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
4675
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004676 // FIXME. Consolidate this with similar code in GenerateClass.
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00004677
Daniel Dunbar91636d62009-04-20 00:33:43 +00004678 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00004679 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004680 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Fariborz Jahanian99eee362009-04-01 19:37:34 +00004681
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004682 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4683 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00004684 // Ignore unnamed bit-fields.
4685 if (!IVD->getDeclName())
4686 continue;
Daniel Dunbar3eec8aa2009-04-20 05:53:40 +00004687 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004688 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004689 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
4690 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004691 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004692 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00004693 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004694 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00004695 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004696 Align = llvm::Log2_32(Align);
4697 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00004698 // NOTE. Size of a bitfield does not match gcc's, because of the
4699 // way bitfields are treated special in each. But I am told that
4700 // 'size' for bitfield ivars is ignored by the runtime so it does
4701 // not matter. If it matters, there is enough info to get the
4702 // bitfield right!
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004703 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4704 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
4705 }
4706 // Return null for empty list.
4707 if (Ivars.empty())
4708 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
4709 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00004710 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004711 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4712 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
4713 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
4714 Ivars.size());
4715 Values[2] = llvm::ConstantArray::get(AT, Ivars);
4716 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4717 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
4718 llvm::GlobalVariable *GV =
4719 new llvm::GlobalVariable(Init->getType(), false,
4720 llvm::GlobalValue::InternalLinkage,
4721 Init,
4722 Prefix + OID->getNameAsString(),
4723 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004724 GV->setAlignment(
4725 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004726 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004727
4728 UsedGlobals.push_back(GV);
4729 return llvm::ConstantExpr::getBitCast(GV,
4730 ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004731}
4732
4733llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
4734 const ObjCProtocolDecl *PD) {
4735 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4736
4737 if (!Entry) {
4738 // We use the initializer as a marker of whether this is a forward
4739 // reference or not. At module finalization we add the empty
4740 // contents for protocols which were referenced but never defined.
4741 Entry =
4742 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
4743 llvm::GlobalValue::ExternalLinkage,
4744 0,
4745 "\01l_OBJC_PROTOCOL_$_" + PD->getNameAsString(),
4746 &CGM.getModule());
4747 Entry->setSection("__DATA,__datacoal_nt,coalesced");
4748 UsedGlobals.push_back(Entry);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004749 }
4750
4751 return Entry;
4752}
4753
4754/// GetOrEmitProtocol - Generate the protocol meta-data:
4755/// @code
4756/// struct _protocol_t {
4757/// id isa; // NULL
4758/// const char * const protocol_name;
4759/// const struct _protocol_list_t * protocol_list; // super protocols
4760/// const struct method_list_t * const instance_methods;
4761/// const struct method_list_t * const class_methods;
4762/// const struct method_list_t *optionalInstanceMethods;
4763/// const struct method_list_t *optionalClassMethods;
4764/// const struct _prop_list_t * properties;
4765/// const uint32_t size; // sizeof(struct _protocol_t)
4766/// const uint32_t flags; // = 0
4767/// }
4768/// @endcode
4769///
4770
4771llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
4772 const ObjCProtocolDecl *PD) {
4773 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
4774
4775 // Early exit if a defining object has already been generated.
4776 if (Entry && Entry->hasInitializer())
4777 return Entry;
4778
4779 const char *ProtocolName = PD->getNameAsCString();
4780
4781 // Construct method lists.
4782 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
4783 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Douglas Gregor6ab35242009-04-09 21:40:53 +00004784 for (ObjCProtocolDecl::instmeth_iterator
4785 i = PD->instmeth_begin(CGM.getContext()),
4786 e = PD->instmeth_end(CGM.getContext());
4787 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004788 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004789 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004790 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4791 OptInstanceMethods.push_back(C);
4792 } else {
4793 InstanceMethods.push_back(C);
4794 }
4795 }
4796
Douglas Gregor6ab35242009-04-09 21:40:53 +00004797 for (ObjCProtocolDecl::classmeth_iterator
4798 i = PD->classmeth_begin(CGM.getContext()),
4799 e = PD->classmeth_end(CGM.getContext());
4800 i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00004801 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004802 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004803 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
4804 OptClassMethods.push_back(C);
4805 } else {
4806 ClassMethods.push_back(C);
4807 }
4808 }
4809
4810 std::vector<llvm::Constant*> Values(10);
4811 // isa is NULL
4812 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
4813 Values[1] = GetClassName(PD->getIdentifier());
4814 Values[2] = EmitProtocolList(
4815 "\01l_OBJC_$_PROTOCOL_REFS_" + PD->getNameAsString(),
4816 PD->protocol_begin(),
4817 PD->protocol_end());
4818
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004819 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004820 + PD->getNameAsString(),
4821 "__DATA, __objc_const",
4822 InstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004823 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004824 + PD->getNameAsString(),
4825 "__DATA, __objc_const",
4826 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004827 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004828 + PD->getNameAsString(),
4829 "__DATA, __objc_const",
4830 OptInstanceMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004831 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Fariborz Jahanianda320092009-01-29 19:24:30 +00004832 + PD->getNameAsString(),
4833 "__DATA, __objc_const",
4834 OptClassMethods);
4835 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getNameAsString(),
4836 0, PD, ObjCTypes);
4837 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00004838 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004839 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
4840 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
4841 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
4842 Values);
4843
4844 if (Entry) {
4845 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00004846 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004847 Entry->setInitializer(Init);
4848 } else {
4849 Entry =
4850 new llvm::GlobalVariable(ObjCTypes.ProtocolnfABITy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004851 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004852 Init,
4853 std::string("\01l_OBJC_PROTOCOL_$_")+ProtocolName,
4854 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004855 Entry->setAlignment(
4856 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004857 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00004858 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004859 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
4860
4861 // Use this protocol meta-data to build protocol list table in section
4862 // __DATA, __objc_protolist
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004863 llvm::GlobalVariable *PTGV = new llvm::GlobalVariable(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004864 ObjCTypes.ProtocolnfABIPtrTy, false,
Mike Stump286acbd2009-03-07 16:33:28 +00004865 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004866 Entry,
4867 std::string("\01l_OBJC_LABEL_PROTOCOL_$_")
4868 +ProtocolName,
4869 &CGM.getModule());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004870 PTGV->setAlignment(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004871 CGM.getTargetData().getPrefTypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00004872 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00004873 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
4874 UsedGlobals.push_back(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004875 return Entry;
4876}
4877
4878/// EmitProtocolList - Generate protocol list meta-data:
4879/// @code
4880/// struct _protocol_list_t {
4881/// long protocol_count; // Note, this is 32/64 bit
4882/// struct _protocol_t[protocol_count];
4883/// }
4884/// @endcode
4885///
4886llvm::Constant *
4887CGObjCNonFragileABIMac::EmitProtocolList(const std::string &Name,
4888 ObjCProtocolDecl::protocol_iterator begin,
4889 ObjCProtocolDecl::protocol_iterator end) {
4890 std::vector<llvm::Constant*> ProtocolRefs;
4891
Fariborz Jahanianda320092009-01-29 19:24:30 +00004892 // Just return null for empty protocol lists
Daniel Dunbar948e2582009-02-15 07:36:20 +00004893 if (begin == end)
Fariborz Jahanianda320092009-01-29 19:24:30 +00004894 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4895
Daniel Dunbar948e2582009-02-15 07:36:20 +00004896 // FIXME: We shouldn't need to do this lookup here, should we?
Fariborz Jahanianda320092009-01-29 19:24:30 +00004897 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name, true);
4898 if (GV)
Daniel Dunbar948e2582009-02-15 07:36:20 +00004899 return llvm::ConstantExpr::getBitCast(GV,
4900 ObjCTypes.ProtocolListnfABIPtrTy);
4901
4902 for (; begin != end; ++begin)
4903 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
4904
Fariborz Jahanianda320092009-01-29 19:24:30 +00004905 // This list is null terminated.
4906 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004907 ObjCTypes.ProtocolnfABIPtrTy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004908
4909 std::vector<llvm::Constant*> Values(2);
4910 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
4911 Values[1] =
Daniel Dunbar948e2582009-02-15 07:36:20 +00004912 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00004913 ProtocolRefs.size()),
4914 ProtocolRefs);
4915
4916 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
4917 GV = new llvm::GlobalVariable(Init->getType(), false,
4918 llvm::GlobalValue::InternalLinkage,
4919 Init,
4920 Name,
4921 &CGM.getModule());
4922 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004923 GV->setAlignment(
4924 CGM.getTargetData().getPrefTypeAlignment(Init->getType()));
Fariborz Jahanianda320092009-01-29 19:24:30 +00004925 UsedGlobals.push_back(GV);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004926 return llvm::ConstantExpr::getBitCast(GV,
4927 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00004928}
4929
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004930/// GetMethodDescriptionConstant - This routine build following meta-data:
4931/// struct _objc_method {
4932/// SEL _cmd;
4933/// char *method_type;
4934/// char *_imp;
4935/// }
4936
4937llvm::Constant *
4938CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
4939 std::vector<llvm::Constant*> Desc(3);
4940 Desc[0] = llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
4941 ObjCTypes.SelectorPtrTy);
4942 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004943 // Protocol methods have no implementation. So, this entry is always NULL.
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00004944 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
4945 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
4946}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004947
4948/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
4949/// This code gen. amounts to generating code for:
4950/// @code
4951/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
4952/// @encode
4953///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00004954LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004955 CodeGen::CodeGenFunction &CGF,
4956 QualType ObjectTy,
4957 llvm::Value *BaseValue,
4958 const ObjCIvarDecl *Ivar,
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004959 unsigned CVRQualifiers) {
Daniel Dunbar525c9b72009-04-21 01:19:28 +00004960 const ObjCInterfaceDecl *ID = ObjectTy->getAsObjCInterfaceType()->getDecl();
Daniel Dunbar97776872009-04-22 07:32:20 +00004961 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
4962 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004963}
4964
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004965llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
4966 CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00004967 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004968 const ObjCIvarDecl *Ivar) {
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00004969 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),
4970 false, "ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00004971}
4972
Fariborz Jahanian46551122009-02-04 00:22:57 +00004973CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
4974 CodeGen::CodeGenFunction &CGF,
4975 QualType ResultType,
4976 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004977 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00004978 QualType Arg0Ty,
4979 bool IsSuper,
4980 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00004981 // FIXME. Even though IsSuper is passes. This function doese not handle calls
4982 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004983 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00004984 llvm::Value *Arg0 = Receiver;
4985 if (!IsSuper)
4986 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004987
4988 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00004989 // FIXME. This is too much work to get the ABI-specific result type needed to
4990 // find the message name.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004991 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType,
4992 llvm::SmallVector<QualType, 16>());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00004993 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004994 std::string Name("\01l_");
4995 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00004996#if 0
4997 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004998 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00004999 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005000 // FIXME. Is there a better way of getting these names.
5001 // They are available in RuntimeFunctions vector pair.
5002 Name += "objc_msgSendId_stret_fixup";
5003 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005004 else
5005#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005006 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005007 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005008 Name += "objc_msgSendSuper2_stret_fixup";
5009 }
5010 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005011 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005012 Fn = ObjCTypes.getMessageSendStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005013 Name += "objc_msgSend_stret_fixup";
5014 }
5015 }
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005016 else if (!IsSuper && ResultType->isFloatingType()) {
Daniel Dunbarc0183e82009-06-26 18:32:06 +00005017 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5018 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5019 Name += "objc_msgSend_fpret_fixup";
5020 }
5021 else {
5022 Fn = ObjCTypes.getMessageSendFixupFn();
5023 Name += "objc_msgSend_fixup";
Fariborz Jahanian5b2bad02009-04-30 16:31:11 +00005024 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005025 }
5026 else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005027#if 0
5028// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005029 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005030 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005031 Name += "objc_msgSendId_fixup";
5032 }
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005033 else
5034#endif
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005035 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005036 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005037 Name += "objc_msgSendSuper2_fixup";
5038 }
5039 else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005040 {
Chris Lattner1c02f862009-04-22 02:53:24 +00005041 Fn = ObjCTypes.getMessageSendFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005042 Name += "objc_msgSend_fixup";
5043 }
5044 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005045 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005046 Name += '_';
5047 std::string SelName(Sel.getAsString());
5048 // Replace all ':' in selector name with '_' ouch!
5049 for(unsigned i = 0; i < SelName.size(); i++)
5050 if (SelName[i] == ':')
5051 SelName[i] = '_';
5052 Name += SelName;
5053 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5054 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005055 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005056 std::vector<llvm::Constant*> Values(2);
5057 Values[0] = Fn;
5058 Values[1] = GetMethodVarName(Sel);
5059 llvm::Constant *Init = llvm::ConstantStruct::get(Values);
5060 GV = new llvm::GlobalVariable(Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005061 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005062 Init,
5063 Name,
5064 &CGM.getModule());
5065 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005066 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005067 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005068 }
5069 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005070
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005071 CallArgList ActualArgs;
5072 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
5073 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
5074 ObjCTypes.MessageRefCPtrTy));
5075 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005076 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs);
5077 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5078 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005079 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005080 Callee = CGF.Builder.CreateBitCast(Callee,
5081 llvm::PointerType::getUnqual(FTy));
5082 return CGF.EmitCall(FnInfo1, Callee, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005083}
5084
5085/// Generate code for a message send expression in the nonfragile abi.
5086CodeGen::RValue CGObjCNonFragileABIMac::GenerateMessageSend(
5087 CodeGen::CodeGenFunction &CGF,
5088 QualType ResultType,
5089 Selector Sel,
5090 llvm::Value *Receiver,
5091 bool IsClassMessage,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00005092 const CallArgList &CallArgs,
5093 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005094 return LegacyDispatchedSelector(Sel)
5095 ? EmitLegacyMessageSend(CGF, ResultType, EmitSelector(CGF.Builder, Sel),
5096 Receiver, CGF.getContext().getObjCIdType(),
5097 false, CallArgs, ObjCTypes)
5098 : EmitMessageSend(CGF, ResultType, Sel,
5099 Receiver, CGF.getContext().getObjCIdType(),
5100 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005101}
5102
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005103llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005104CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005105 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5106
Daniel Dunbardfff2302009-03-02 05:18:14 +00005107 if (!GV) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005108 GV = new llvm::GlobalVariable(ObjCTypes.ClassnfABITy, false,
5109 llvm::GlobalValue::ExternalLinkage,
5110 0, Name, &CGM.getModule());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005111 }
5112
5113 return GV;
5114}
5115
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005116llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005117 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005118 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
5119
5120 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005121 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005122 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005123 Entry =
5124 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5125 llvm::GlobalValue::InternalLinkage,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005126 ClassGV,
Daniel Dunbar11394522009-04-18 08:51:00 +00005127 "\01L_OBJC_CLASSLIST_REFERENCES_$_",
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005128 &CGM.getModule());
5129 Entry->setAlignment(
5130 CGM.getTargetData().getPrefTypeAlignment(
5131 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005132 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
5133 UsedGlobals.push_back(Entry);
5134 }
5135
5136 return Builder.CreateLoad(Entry, false, "tmp");
5137}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005138
Daniel Dunbar11394522009-04-18 08:51:00 +00005139llvm::Value *
5140CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
5141 const ObjCInterfaceDecl *ID) {
5142 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
5143
5144 if (!Entry) {
5145 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5146 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5147 Entry =
5148 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5149 llvm::GlobalValue::InternalLinkage,
5150 ClassGV,
5151 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5152 &CGM.getModule());
5153 Entry->setAlignment(
5154 CGM.getTargetData().getPrefTypeAlignment(
5155 ObjCTypes.ClassnfABIPtrTy));
5156 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005157 UsedGlobals.push_back(Entry);
5158 }
5159
5160 return Builder.CreateLoad(Entry, false, "tmp");
5161}
5162
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005163/// EmitMetaClassRef - Return a Value * of the address of _class_t
5164/// meta-data
5165///
5166llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5167 const ObjCInterfaceDecl *ID) {
5168 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5169 if (Entry)
5170 return Builder.CreateLoad(Entry, false, "tmp");
5171
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005172 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005173 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005174 Entry =
5175 new llvm::GlobalVariable(ObjCTypes.ClassnfABIPtrTy, false,
5176 llvm::GlobalValue::InternalLinkage,
5177 MetaClassGV,
5178 "\01L_OBJC_CLASSLIST_SUP_REFS_$_",
5179 &CGM.getModule());
5180 Entry->setAlignment(
5181 CGM.getTargetData().getPrefTypeAlignment(
5182 ObjCTypes.ClassnfABIPtrTy));
5183
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005184 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005185 UsedGlobals.push_back(Entry);
5186
5187 return Builder.CreateLoad(Entry, false, "tmp");
5188}
5189
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005190/// GetClass - Return a reference to the class for the given interface
5191/// decl.
5192llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5193 const ObjCInterfaceDecl *ID) {
5194 return EmitClassRef(Builder, ID);
5195}
5196
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005197/// Generates a message send where the super is the receiver. This is
5198/// a message send to self with special delivery semantics indicating
5199/// which class's method should be called.
5200CodeGen::RValue
5201CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
5202 QualType ResultType,
5203 Selector Sel,
5204 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005205 bool isCategoryImpl,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005206 llvm::Value *Receiver,
5207 bool IsClassMessage,
5208 const CodeGen::CallArgList &CallArgs) {
5209 // ...
5210 // Create and init a super structure; this is a (receiver, class)
5211 // pair we will pass to objc_msgSendSuper.
5212 llvm::Value *ObjCSuper =
5213 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
5214
5215 llvm::Value *ReceiverAsObject =
5216 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5217 CGF.Builder.CreateStore(ReceiverAsObject,
5218 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
5219
5220 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005221 llvm::Value *Target;
5222 if (IsClassMessage) {
5223 if (isCategoryImpl) {
5224 // Message sent to "super' in a class method defined in
5225 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005226 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005227 Target = CGF.Builder.CreateStructGEP(Target, 0);
5228 Target = CGF.Builder.CreateLoad(Target);
5229 }
5230 else
5231 Target = EmitMetaClassRef(CGF.Builder, Class);
5232 }
5233 else
Daniel Dunbar11394522009-04-18 08:51:00 +00005234 Target = EmitSuperClassRef(CGF.Builder, Class);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005235
Mike Stumpf5408fe2009-05-16 07:57:57 +00005236 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5237 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005238 const llvm::Type *ClassTy =
5239 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5240 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5241 CGF.Builder.CreateStore(Target,
5242 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
5243
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005244 return (LegacyDispatchedSelector(Sel))
5245 ? EmitLegacyMessageSend(CGF, ResultType,EmitSelector(CGF.Builder, Sel),
5246 ObjCSuper, ObjCTypes.SuperPtrCTy,
5247 true, CallArgs,
5248 ObjCTypes)
5249 : EmitMessageSend(CGF, ResultType, Sel,
5250 ObjCSuper, ObjCTypes.SuperPtrCTy,
5251 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005252}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005253
5254llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
5255 Selector Sel) {
5256 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
5257
5258 if (!Entry) {
5259 llvm::Constant *Casted =
5260 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5261 ObjCTypes.SelectorPtrTy);
5262 Entry =
5263 new llvm::GlobalVariable(ObjCTypes.SelectorPtrTy, false,
5264 llvm::GlobalValue::InternalLinkage,
5265 Casted, "\01L_OBJC_SELECTOR_REFERENCES_",
5266 &CGM.getModule());
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005267 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005268 UsedGlobals.push_back(Entry);
5269 }
5270
5271 return Builder.CreateLoad(Entry, false, "tmp");
5272}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005273/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
5274/// objc_assign_ivar (id src, id *dst)
5275///
5276void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
5277 llvm::Value *src, llvm::Value *dst)
5278{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005279 const llvm::Type * SrcTy = src->getType();
5280 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005281 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005282 assert(Size <= 8 && "does not support size > 8");
5283 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5284 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005285 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5286 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005287 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5288 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005289 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignIvarFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005290 src, dst, "assignivar");
5291 return;
5292}
5293
5294/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5295/// objc_assign_strongCast (id src, id *dst)
5296///
5297void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
5298 CodeGen::CodeGenFunction &CGF,
5299 llvm::Value *src, llvm::Value *dst)
5300{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005301 const llvm::Type * SrcTy = src->getType();
5302 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005303 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005304 assert(Size <= 8 && "does not support size > 8");
5305 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5306 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005307 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5308 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005309 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5310 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005311 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005312 src, dst, "weakassign");
5313 return;
5314}
5315
5316/// EmitObjCWeakRead - Code gen for loading value of a __weak
5317/// object: objc_read_weak (id *src)
5318///
5319llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
5320 CodeGen::CodeGenFunction &CGF,
5321 llvm::Value *AddrWeakObj)
5322{
Eli Friedman8339b352009-03-07 03:57:15 +00005323 const llvm::Type* DestTy =
5324 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005325 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005326 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005327 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005328 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005329 return read_weak;
5330}
5331
5332/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5333/// objc_assign_weak (id src, id *dst)
5334///
5335void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
5336 llvm::Value *src, llvm::Value *dst)
5337{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005338 const llvm::Type * SrcTy = src->getType();
5339 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005340 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005341 assert(Size <= 8 && "does not support size > 8");
5342 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5343 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005344 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5345 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005346 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5347 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005348 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005349 src, dst, "weakassign");
5350 return;
5351}
5352
5353/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5354/// objc_assign_global (id src, id *dst)
5355///
5356void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
5357 llvm::Value *src, llvm::Value *dst)
5358{
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005359 const llvm::Type * SrcTy = src->getType();
5360 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005361 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005362 assert(Size <= 8 && "does not support size > 8");
5363 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5364 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005365 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5366 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005367 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5368 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005369 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005370 src, dst, "globalassign");
5371 return;
5372}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005373
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005374void
5375CGObjCNonFragileABIMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5376 const Stmt &S) {
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005377 bool isTry = isa<ObjCAtTryStmt>(S);
5378 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
5379 llvm::BasicBlock *PrevLandingPad = CGF.getInvokeDest();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005380 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005381 llvm::BasicBlock *FinallyBlock = CGF.createBasicBlock("finally");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005382 llvm::BasicBlock *FinallyRethrow = CGF.createBasicBlock("finally.throw");
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005383 llvm::BasicBlock *FinallyEnd = CGF.createBasicBlock("finally.end");
5384
5385 // For @synchronized, call objc_sync_enter(sync.expr). The
5386 // evaluation of the expression must occur before we enter the
5387 // @synchronized. We can safely avoid a temp here because jumps into
5388 // @synchronized are illegal & this will dominate uses.
5389 llvm::Value *SyncArg = 0;
5390 if (!isTry) {
5391 SyncArg =
5392 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
5393 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005394 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005395 }
5396
5397 // Push an EH context entry, used for handling rethrows and jumps
5398 // through finally.
5399 CGF.PushCleanupBlock(FinallyBlock);
5400
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005401 CGF.setInvokeDest(TryHandler);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005402
5403 CGF.EmitBlock(TryBlock);
5404 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
5405 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
5406 CGF.EmitBranchThroughCleanup(FinallyEnd);
5407
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005408 // Emit the exception handler.
5409
5410 CGF.EmitBlock(TryHandler);
5411
5412 llvm::Value *llvm_eh_exception =
5413 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_exception);
5414 llvm::Value *llvm_eh_selector_i64 =
5415 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_selector_i64);
5416 llvm::Value *llvm_eh_typeid_for_i64 =
5417 CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for_i64);
5418 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5419 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(Exc->getType(), "_rethrow");
5420
5421 llvm::SmallVector<llvm::Value*, 8> SelectorArgs;
5422 SelectorArgs.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005423 SelectorArgs.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005424
5425 // Construct the lists of (type, catch body) to handle.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005426 llvm::SmallVector<std::pair<const ParmVarDecl*, const Stmt*>, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005427 bool HasCatchAll = false;
5428 if (isTry) {
5429 if (const ObjCAtCatchStmt* CatchStmt =
5430 cast<ObjCAtTryStmt>(S).getCatchStmts()) {
5431 for (; CatchStmt; CatchStmt = CatchStmt->getNextCatchStmt()) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005432 const ParmVarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Steve Naroff7ba138a2009-03-03 19:52:17 +00005433 Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005434
5435 // catch(...) always matches.
Steve Naroff7ba138a2009-03-03 19:52:17 +00005436 if (!CatchDecl) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005437 // Use i8* null here to signal this is a catch all, not a cleanup.
5438 llvm::Value *Null = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
5439 SelectorArgs.push_back(Null);
5440 HasCatchAll = true;
5441 break;
5442 }
5443
Daniel Dunbarede8de92009-03-06 00:01:21 +00005444 if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
5445 CatchDecl->getType()->isObjCQualifiedIdType()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005446 llvm::Value *IDEHType =
5447 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5448 if (!IDEHType)
5449 IDEHType =
5450 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5451 llvm::GlobalValue::ExternalLinkage,
5452 0, "OBJC_EHTYPE_id", &CGM.getModule());
5453 SelectorArgs.push_back(IDEHType);
5454 HasCatchAll = true;
5455 break;
5456 }
5457
5458 // All other types should be Objective-C interface pointer types.
Daniel Dunbarede8de92009-03-06 00:01:21 +00005459 const PointerType *PT = CatchDecl->getType()->getAsPointerType();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005460 assert(PT && "Invalid @catch type.");
5461 const ObjCInterfaceType *IT =
5462 PT->getPointeeType()->getAsObjCInterfaceType();
5463 assert(IT && "Invalid @catch type.");
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005464 llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005465 SelectorArgs.push_back(EHType);
5466 }
5467 }
5468 }
5469
5470 // We use a cleanup unless there was already a catch all.
5471 if (!HasCatchAll) {
5472 SelectorArgs.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty, 0));
Daniel Dunbarede8de92009-03-06 00:01:21 +00005473 Handlers.push_back(std::make_pair((const ParmVarDecl*) 0, (const Stmt*) 0));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005474 }
5475
5476 llvm::Value *Selector =
5477 CGF.Builder.CreateCall(llvm_eh_selector_i64,
5478 SelectorArgs.begin(), SelectorArgs.end(),
5479 "selector");
5480 for (unsigned i = 0, e = Handlers.size(); i != e; ++i) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005481 const ParmVarDecl *CatchParam = Handlers[i].first;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005482 const Stmt *CatchBody = Handlers[i].second;
5483
5484 llvm::BasicBlock *Next = 0;
5485
5486 // The last handler always matches.
5487 if (i + 1 != e) {
5488 assert(CatchParam && "Only last handler can be a catch all.");
5489
5490 llvm::BasicBlock *Match = CGF.createBasicBlock("match");
5491 Next = CGF.createBasicBlock("catch.next");
5492 llvm::Value *Id =
5493 CGF.Builder.CreateCall(llvm_eh_typeid_for_i64,
5494 CGF.Builder.CreateBitCast(SelectorArgs[i+2],
5495 ObjCTypes.Int8PtrTy));
5496 CGF.Builder.CreateCondBr(CGF.Builder.CreateICmpEQ(Selector, Id),
5497 Match, Next);
5498
5499 CGF.EmitBlock(Match);
5500 }
5501
5502 if (CatchBody) {
5503 llvm::BasicBlock *MatchEnd = CGF.createBasicBlock("match.end");
5504 llvm::BasicBlock *MatchHandler = CGF.createBasicBlock("match.handler");
5505
5506 // Cleanups must call objc_end_catch.
5507 //
Mike Stumpf5408fe2009-05-16 07:57:57 +00005508 // FIXME: It seems incorrect for objc_begin_catch to be inside this
5509 // context, but this matches gcc.
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005510 CGF.PushCleanupBlock(MatchEnd);
5511 CGF.setInvokeDest(MatchHandler);
5512
5513 llvm::Value *ExcObject =
Chris Lattner8a569112009-04-22 02:15:23 +00005514 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), Exc);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005515
5516 // Bind the catch parameter if it exists.
5517 if (CatchParam) {
Daniel Dunbarede8de92009-03-06 00:01:21 +00005518 ExcObject =
5519 CGF.Builder.CreateBitCast(ExcObject,
5520 CGF.ConvertType(CatchParam->getType()));
5521 // CatchParam is a ParmVarDecl because of the grammar
5522 // construction used to handle this, but for codegen purposes
5523 // we treat this as a local decl.
5524 CGF.EmitLocalBlockVarDecl(*CatchParam);
5525 CGF.Builder.CreateStore(ExcObject, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005526 }
5527
5528 CGF.ObjCEHValueStack.push_back(ExcObject);
5529 CGF.EmitStmt(CatchBody);
5530 CGF.ObjCEHValueStack.pop_back();
5531
5532 CGF.EmitBranchThroughCleanup(FinallyEnd);
5533
5534 CGF.EmitBlock(MatchHandler);
5535
5536 llvm::Value *Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5537 // We are required to emit this call to satisfy LLVM, even
5538 // though we don't use the result.
5539 llvm::SmallVector<llvm::Value*, 8> Args;
5540 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005541 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005542 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5543 0));
5544 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5545 CGF.Builder.CreateStore(Exc, RethrowPtr);
5546 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5547
5548 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5549
5550 CGF.EmitBlock(MatchEnd);
5551
5552 // Unfortunately, we also have to generate another EH frame here
5553 // in case this throws.
5554 llvm::BasicBlock *MatchEndHandler =
5555 CGF.createBasicBlock("match.end.handler");
5556 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattner8a569112009-04-22 02:15:23 +00005557 CGF.Builder.CreateInvoke(ObjCTypes.getObjCEndCatchFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005558 Cont, MatchEndHandler,
5559 Args.begin(), Args.begin());
5560
5561 CGF.EmitBlock(Cont);
5562 if (Info.SwitchBlock)
5563 CGF.EmitBlock(Info.SwitchBlock);
5564 if (Info.EndBlock)
5565 CGF.EmitBlock(Info.EndBlock);
5566
5567 CGF.EmitBlock(MatchEndHandler);
5568 Exc = CGF.Builder.CreateCall(llvm_eh_exception, "exc");
5569 // We are required to emit this call to satisfy LLVM, even
5570 // though we don't use the result.
5571 Args.clear();
5572 Args.push_back(Exc);
Chris Lattnerb02e53b2009-04-06 16:53:45 +00005573 Args.push_back(ObjCTypes.getEHPersonalityPtr());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005574 Args.push_back(llvm::ConstantInt::get(llvm::Type::Int32Ty,
5575 0));
5576 CGF.Builder.CreateCall(llvm_eh_selector_i64, Args.begin(), Args.end());
5577 CGF.Builder.CreateStore(Exc, RethrowPtr);
5578 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5579
5580 if (Next)
5581 CGF.EmitBlock(Next);
5582 } else {
5583 assert(!Next && "catchup should be last handler.");
5584
5585 CGF.Builder.CreateStore(Exc, RethrowPtr);
5586 CGF.EmitBranchThroughCleanup(FinallyRethrow);
5587 }
5588 }
5589
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005590 // Pop the cleanup entry, the @finally is outside this cleanup
5591 // scope.
5592 CodeGenFunction::CleanupBlockInfo Info = CGF.PopCleanupBlock();
5593 CGF.setInvokeDest(PrevLandingPad);
5594
5595 CGF.EmitBlock(FinallyBlock);
5596
5597 if (isTry) {
5598 if (const ObjCAtFinallyStmt* FinallyStmt =
5599 cast<ObjCAtTryStmt>(S).getFinallyStmt())
5600 CGF.EmitStmt(FinallyStmt->getFinallyBody());
5601 } else {
5602 // Emit 'objc_sync_exit(expr)' as finally's sole statement for
5603 // @synchronized.
Chris Lattnerbbccd612009-04-22 02:38:11 +00005604 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005605 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005606
5607 if (Info.SwitchBlock)
5608 CGF.EmitBlock(Info.SwitchBlock);
5609 if (Info.EndBlock)
5610 CGF.EmitBlock(Info.EndBlock);
5611
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005612 // Branch around the rethrow code.
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005613 CGF.EmitBranch(FinallyEnd);
5614
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005615 CGF.EmitBlock(FinallyRethrow);
Chris Lattner8a569112009-04-22 02:15:23 +00005616 CGF.Builder.CreateCall(ObjCTypes.getUnwindResumeOrRethrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005617 CGF.Builder.CreateLoad(RethrowPtr));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005618 CGF.Builder.CreateUnreachable();
5619
5620 CGF.EmitBlock(FinallyEnd);
5621}
5622
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005623/// EmitThrowStmt - Generate code for a throw statement.
5624void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5625 const ObjCAtThrowStmt &S) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005626 llvm::Value *Exception;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005627 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005628 Exception = CGF.EmitScalarExpr(ThrowExpr);
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005629 } else {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005630 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
5631 "Unexpected rethrow outside @catch block.");
5632 Exception = CGF.ObjCEHValueStack.back();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005633 }
5634
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005635 llvm::Value *ExceptionAsObject =
5636 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5637 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5638 if (InvokeDest) {
5639 llvm::BasicBlock *Cont = CGF.createBasicBlock("invoke.cont");
Chris Lattnerbbccd612009-04-22 02:38:11 +00005640 CGF.Builder.CreateInvoke(ObjCTypes.getExceptionThrowFn(),
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005641 Cont, InvokeDest,
5642 &ExceptionAsObject, &ExceptionAsObject + 1);
5643 CGF.EmitBlock(Cont);
5644 } else
Chris Lattnerbbccd612009-04-22 02:38:11 +00005645 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005646 CGF.Builder.CreateUnreachable();
5647
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005648 // Clear the insertion point to indicate we are in unreachable code.
5649 CGF.Builder.ClearInsertionPoint();
5650}
Daniel Dunbare588b992009-03-01 04:46:24 +00005651
5652llvm::Value *
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005653CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
5654 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005655 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005656
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005657 // If we don't need a definition, return the entry if found or check
5658 // if we use an external reference.
5659 if (!ForDefinition) {
5660 if (Entry)
5661 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005662
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005663 // If this type (or a super class) has the __objc_exception__
5664 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005665 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005666 return Entry =
5667 new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5668 llvm::GlobalValue::ExternalLinkage,
5669 0,
5670 (std::string("OBJC_EHTYPE_$_") +
5671 ID->getIdentifier()->getName()),
5672 &CGM.getModule());
5673 }
5674
5675 // Otherwise we need to either make a new entry or fill in the
5676 // initializer.
5677 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005678 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005679 std::string VTableName = "objc_ehtype_vtable";
5680 llvm::GlobalVariable *VTableGV =
5681 CGM.getModule().getGlobalVariable(VTableName);
5682 if (!VTableGV)
5683 VTableGV = new llvm::GlobalVariable(ObjCTypes.Int8PtrTy, false,
5684 llvm::GlobalValue::ExternalLinkage,
5685 0, VTableName, &CGM.getModule());
5686
5687 llvm::Value *VTableIdx = llvm::ConstantInt::get(llvm::Type::Int32Ty, 2);
5688
5689 std::vector<llvm::Constant*> Values(3);
5690 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
5691 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005692 Values[2] = GetClassGlobal(ClassName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005693 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
5694
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005695 if (Entry) {
5696 Entry->setInitializer(Init);
5697 } else {
5698 Entry = new llvm::GlobalVariable(ObjCTypes.EHTypeTy, false,
5699 llvm::GlobalValue::WeakAnyLinkage,
5700 Init,
5701 (std::string("OBJC_EHTYPE_$_") +
5702 ID->getIdentifier()->getName()),
5703 &CGM.getModule());
5704 }
5705
Daniel Dunbar04d40782009-04-14 06:00:08 +00005706 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005707 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005708 Entry->setAlignment(8);
5709
5710 if (ForDefinition) {
5711 Entry->setSection("__DATA,__objc_const");
5712 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5713 } else {
5714 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5715 }
Daniel Dunbare588b992009-03-01 04:46:24 +00005716
5717 return Entry;
5718}
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005719
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00005720/* *** */
5721
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00005722CodeGen::CGObjCRuntime *
5723CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00005724 return new CGObjCMac(CGM);
5725}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005726
5727CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00005728CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005729 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00005730}