blob: 24c080e517b98229a80a66d371d21dab6dd70106 [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
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCall36f893c2011-01-28 11:13:47 +000019#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000027
John McCall87bb5822010-07-31 23:20:56 +000028#include "llvm/InlineAsm.h"
29#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000030#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000031#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000032#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000033#include "llvm/ADT/SetVector.h"
34#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000035#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000036#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000037#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000038#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000039#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000040
41using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000042using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000043
Daniel Dunbar97776872009-04-22 07:32:20 +000044// Common CGObjCRuntime functions, these don't belong here, but they
45// don't belong in CGObjCRuntime either so we will live with it for
46// now.
47
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000048static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
49 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000050 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000051 const ObjCIvarDecl *Ivar) {
Daniel Dunbare83be122010-04-02 21:14:02 +000052 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar532d4da2009-05-03 13:15:50 +000053
Daniel Dunbar61ac1d22010-04-02 22:29:40 +000054 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
55 // in here; it should never be necessary because that should be the lexical
56 // decl context for the ivar.
Daniel Dunbar3a2c80f2010-04-02 15:43:29 +000057
Daniel Dunbar532d4da2009-05-03 13:15:50 +000058 // If we know have an implementation (and the ivar is in it) then
59 // look up in the implementation layout.
Daniel Dunbar6bff2512009-08-03 17:06:42 +000060 const ASTRecordLayout *RL;
Daniel Dunbar532d4da2009-05-03 13:15:50 +000061 if (ID && ID->getClassInterface() == Container)
62 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
63 else
64 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbare83be122010-04-02 21:14:02 +000065
66 // Compute field index.
67 //
68 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
69 // implemented. This should be fixed to get the information from the layout
70 // directly.
71 unsigned Index = 0;
72 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
73 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
74 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
75 if (Ivar == Ivars[k])
76 break;
77 ++Index;
78 }
79 assert(Index != Ivars.size() && "Ivar is not inside container!");
David Chisnalle0d98762010-11-03 16:12:44 +000080 assert(Index < RL->getFieldCount() && "Ivar is not inside record layout!");
Daniel Dunbare83be122010-04-02 21:14:02 +000081
Daniel Dunbar532d4da2009-05-03 13:15:50 +000082 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000083}
84
85uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
86 const ObjCInterfaceDecl *OID,
87 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000088 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
89}
90
91uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
92 const ObjCImplementationDecl *OID,
93 const ObjCIvarDecl *Ivar) {
94 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000095}
96
97LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
98 const ObjCInterfaceDecl *OID,
99 llvm::Value *BaseValue,
100 const ObjCIvarDecl *Ivar,
101 unsigned CVRQualifiers,
102 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000103 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000104 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000105 QualType IvarTy = Ivar->getType();
106 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000107 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000108 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000109 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000110
Daniel Dunbar6d5eb762010-08-21 03:44:13 +0000111 if (!Ivar->isBitField()) {
112 LValue LV = CGF.MakeAddrLValue(V, IvarTy);
113 LV.getQuals().addCVRQualifiers(CVRQualifiers);
114 return LV;
115 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000116
Daniel Dunbarc5c446c2010-09-02 23:53:31 +0000117 // We need to compute an access strategy for this bit-field. We are given the
118 // offset to the first byte in the bit-field, the sub-byte offset is taken
119 // from the original layout. We reuse the normal bit-field access strategy by
120 // treating this as an access to a struct where the bit-field is in byte 0,
121 // and adjust the containing type size as appropriate.
122 //
123 // FIXME: Note that currently we make a very conservative estimate of the
124 // alignment of the bit-field, because (a) it is not clear what guarantees the
125 // runtime makes us, and (b) we don't have a way to specify that the struct is
126 // at an alignment plus offset.
127 //
128 // Note, there is a subtle invariant here: we can only call this routine on
129 // non-synthesized ivars but we may be called for synthesized ivars. However,
130 // a synthesized ivar can never be a bit-field, so this is safe.
131 const ASTRecordLayout &RL =
132 CGF.CGM.getContext().getASTObjCInterfaceLayout(OID);
133 uint64_t TypeSizeInBits = RL.getSize();
134 uint64_t FieldBitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar);
135 uint64_t BitOffset = FieldBitOffset % 8;
136 uint64_t ContainingTypeAlign = 8;
137 uint64_t ContainingTypeSize = TypeSizeInBits - (FieldBitOffset - BitOffset);
Daniel Dunbar56229f52010-04-05 16:20:33 +0000138 uint64_t BitFieldSize =
139 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar97776872009-04-22 07:32:20 +0000140
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000141 // Allocate a new CGBitFieldInfo object to describe this access.
142 //
143 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
144 // layout object. However, this is blocked on other cleanups to the
145 // Objective-C code, so for now we just live with allocating a bunch of these
146 // objects.
Daniel Dunbarc5c446c2010-09-02 23:53:31 +0000147 CGBitFieldInfo *Info = new (CGF.CGM.getContext()) CGBitFieldInfo(
148 CGBitFieldInfo::MakeInfo(CGF.CGM.getTypes(), Ivar, BitOffset, BitFieldSize,
149 ContainingTypeSize, ContainingTypeAlign));
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000150
Daniel Dunbar76695ca2010-08-21 04:05:54 +0000151 return LValue::MakeBitfield(V, *Info,
152 IvarTy.getCVRQualifiers() | CVRQualifiers);
Daniel Dunbar97776872009-04-22 07:32:20 +0000153}
154
155///
156
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000157namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000158
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000159typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000160
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000161// FIXME: We should find a nicer way to make the labels for metadata, string
162// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000163
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000164class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000165protected:
166 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000167
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000168private:
169 llvm::Constant *getMessageSendFn() const {
170 // id objc_msgSend (id, SEL, ...)
171 std::vector<const llvm::Type*> Params;
172 Params.push_back(ObjectPtrTy);
173 Params.push_back(SelectorPtrTy);
174 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000175 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
176 Params, true),
177 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000178 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000179
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000180 llvm::Constant *getMessageSendStretFn() const {
181 // id objc_msgSend_stret (id, SEL, ...)
182 std::vector<const llvm::Type*> Params;
183 Params.push_back(ObjectPtrTy);
184 Params.push_back(SelectorPtrTy);
185 return
Owen Anderson0032b272009-08-13 21:57:51 +0000186 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000187 Params, true),
188 "objc_msgSend_stret");
189
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000190 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000191
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000192 llvm::Constant *getMessageSendFpretFn() const {
193 // FIXME: This should be long double on x86_64?
194 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
195 std::vector<const llvm::Type*> Params;
196 Params.push_back(ObjectPtrTy);
197 Params.push_back(SelectorPtrTy);
198 return
Owen Anderson0032b272009-08-13 21:57:51 +0000199 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
200 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000201 Params,
202 true),
203 "objc_msgSend_fpret");
204
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000205 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000206
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000207 llvm::Constant *getMessageSendSuperFn() const {
208 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
209 const char *SuperName = "objc_msgSendSuper";
210 std::vector<const llvm::Type*> Params;
211 Params.push_back(SuperPtrTy);
212 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000213 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000214 Params, true),
215 SuperName);
216 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000217
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000218 llvm::Constant *getMessageSendSuperFn2() const {
219 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
220 const char *SuperName = "objc_msgSendSuper2";
221 std::vector<const llvm::Type*> Params;
222 Params.push_back(SuperPtrTy);
223 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000224 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000225 Params, true),
226 SuperName);
227 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000228
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000229 llvm::Constant *getMessageSendSuperStretFn() const {
230 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
231 // SEL op, ...)
232 std::vector<const llvm::Type*> Params;
233 Params.push_back(Int8PtrTy);
234 Params.push_back(SuperPtrTy);
235 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000236 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000237 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000238 Params, true),
239 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000240 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000241
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000242 llvm::Constant *getMessageSendSuperStretFn2() const {
243 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
244 // SEL op, ...)
245 std::vector<const llvm::Type*> Params;
246 Params.push_back(Int8PtrTy);
247 Params.push_back(SuperPtrTy);
248 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000249 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000250 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000251 Params, true),
252 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000253 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000254
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000255 llvm::Constant *getMessageSendSuperFpretFn() const {
256 // There is no objc_msgSendSuper_fpret? How can that work?
257 return getMessageSendSuperFn();
258 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000259
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000260 llvm::Constant *getMessageSendSuperFpretFn2() const {
261 // There is no objc_msgSendSuper_fpret? How can that work?
262 return getMessageSendSuperFn2();
263 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000264
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000265protected:
266 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000267
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000268public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000269 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000270 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000271
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000272 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
273 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000274
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000275 /// PtrObjectPtrTy - LLVM type for id *
276 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000277
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000278 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000279 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000280 /// ProtocolPtrTy - LLVM type for external protocol handles
281 /// (typeof(Protocol))
282 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000283
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000284 // SuperCTy - clang type for struct objc_super.
285 QualType SuperCTy;
286 // SuperPtrCTy - clang type for struct objc_super *.
287 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000288
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000289 /// SuperTy - LLVM type for struct objc_super.
290 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000291 /// SuperPtrTy - LLVM type for struct objc_super *.
292 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000293
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000294 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
295 /// in GCC parlance).
296 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000297
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000298 /// PropertyListTy - LLVM type for struct objc_property_list
299 /// (_prop_list_t in GCC parlance).
300 const llvm::StructType *PropertyListTy;
301 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
302 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000303
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000304 // MethodTy - LLVM type for struct objc_method.
305 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000306
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000307 /// CacheTy - LLVM type for struct objc_cache.
308 const llvm::Type *CacheTy;
309 /// CachePtrTy - LLVM type for struct objc_cache *.
310 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000311
Chris Lattner72db6c32009-04-22 02:44:54 +0000312 llvm::Constant *getGetPropertyFn() {
313 CodeGen::CodeGenTypes &Types = CGM.getTypes();
314 ASTContext &Ctx = CGM.getContext();
315 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000316 llvm::SmallVector<CanQualType,4> Params;
317 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
318 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000319 Params.push_back(IdType);
320 Params.push_back(SelType);
321 Params.push_back(Ctx.LongTy);
322 Params.push_back(Ctx.BoolTy);
323 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000324 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000325 FunctionType::ExtInfo()),
326 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000327 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
328 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000329
Chris Lattner72db6c32009-04-22 02:44:54 +0000330 llvm::Constant *getSetPropertyFn() {
331 CodeGen::CodeGenTypes &Types = CGM.getTypes();
332 ASTContext &Ctx = CGM.getContext();
333 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000334 llvm::SmallVector<CanQualType,6> Params;
335 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
336 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000337 Params.push_back(IdType);
338 Params.push_back(SelType);
339 Params.push_back(Ctx.LongTy);
340 Params.push_back(IdType);
341 Params.push_back(Ctx.BoolTy);
342 Params.push_back(Ctx.BoolTy);
343 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000344 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000345 FunctionType::ExtInfo()),
346 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000347 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
348 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000349
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000350
351 llvm::Constant *getCopyStructFn() {
352 CodeGen::CodeGenTypes &Types = CGM.getTypes();
353 ASTContext &Ctx = CGM.getContext();
354 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
355 llvm::SmallVector<CanQualType,5> Params;
356 Params.push_back(Ctx.VoidPtrTy);
357 Params.push_back(Ctx.VoidPtrTy);
358 Params.push_back(Ctx.LongTy);
359 Params.push_back(Ctx.BoolTy);
360 Params.push_back(Ctx.BoolTy);
361 const llvm::FunctionType *FTy =
362 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
363 FunctionType::ExtInfo()),
364 false);
365 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
366 }
367
Chris Lattner72db6c32009-04-22 02:44:54 +0000368 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000369 CodeGen::CodeGenTypes &Types = CGM.getTypes();
370 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000371 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000372 llvm::SmallVector<CanQualType,1> Params;
373 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000374 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000375 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000376 FunctionType::ExtInfo()),
377 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000378 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
379 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000380
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000381 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000382 llvm::Constant *getGcReadWeakFn() {
383 // id objc_read_weak (id *)
384 std::vector<const llvm::Type*> Args;
385 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000386 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000387 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000388 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000389 }
390
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000391 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000392 llvm::Constant *getGcAssignWeakFn() {
393 // id objc_assign_weak (id, id *)
394 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
395 Args.push_back(ObjectPtrTy->getPointerTo());
396 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000397 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000398 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
399 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000400
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000401 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000402 llvm::Constant *getGcAssignGlobalFn() {
403 // id objc_assign_global(id, id *)
404 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
405 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000406 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000407 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000408 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
409 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000410
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000411 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
412 llvm::Constant *getGcAssignThreadLocalFn() {
413 // id objc_assign_threadlocal(id src, id * dest)
414 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
415 Args.push_back(ObjectPtrTy->getPointerTo());
416 llvm::FunctionType *FTy =
417 llvm::FunctionType::get(ObjectPtrTy, Args, false);
418 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
419 }
420
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000421 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000422 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000423 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000424 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
425 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000426 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000427 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000428 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000429 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
430 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000431
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000432 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
433 llvm::Constant *GcMemmoveCollectableFn() {
434 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
435 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
436 Args.push_back(Int8PtrTy);
437 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000438 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000439 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
440 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000441
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000442 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000443 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000444 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000445 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
446 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000447 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000448 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000449 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
450 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000451
452 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000453 llvm::Constant *getExceptionThrowFn() {
454 // void objc_exception_throw(id)
455 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
456 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000457 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000458 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
459 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000460
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000461 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
462 llvm::Constant *getExceptionRethrowFn() {
463 // void objc_exception_rethrow(void)
464 std::vector<const llvm::Type*> Args;
465 llvm::FunctionType *FTy =
John McCall7ec404c2010-10-16 08:21:07 +0000466 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000467 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
468 }
469
Daniel Dunbar1c566672009-02-24 01:43:46 +0000470 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000471 llvm::Constant *getSyncEnterFn() {
472 // void objc_sync_enter (id)
473 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
474 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000475 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000476 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
477 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000478
Daniel Dunbar1c566672009-02-24 01:43:46 +0000479 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000480 llvm::Constant *getSyncExitFn() {
481 // void objc_sync_exit (id)
482 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
483 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000484 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000485 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
486 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000487
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000488 llvm::Constant *getSendFn(bool IsSuper) const {
489 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
490 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000491
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000492 llvm::Constant *getSendFn2(bool IsSuper) const {
493 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
494 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000495
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000496 llvm::Constant *getSendStretFn(bool IsSuper) const {
497 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
498 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000499
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000500 llvm::Constant *getSendStretFn2(bool IsSuper) const {
501 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
502 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000503
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000504 llvm::Constant *getSendFpretFn(bool IsSuper) const {
505 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
506 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000507
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000508 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
509 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
510 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000511
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000512 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
513 ~ObjCCommonTypesHelper(){}
514};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000515
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000516/// ObjCTypesHelper - Helper class that encapsulates lazy
517/// construction of varies types used during ObjC generation.
518class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000519public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000520 /// SymtabTy - LLVM type for struct objc_symtab.
521 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000522 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
523 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000524 /// ModuleTy - LLVM type for struct objc_module.
525 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000526
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000527 /// ProtocolTy - LLVM type for struct objc_protocol.
528 const llvm::StructType *ProtocolTy;
529 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
530 const llvm::Type *ProtocolPtrTy;
531 /// ProtocolExtensionTy - LLVM type for struct
532 /// objc_protocol_extension.
533 const llvm::StructType *ProtocolExtensionTy;
534 /// ProtocolExtensionTy - LLVM type for struct
535 /// objc_protocol_extension *.
536 const llvm::Type *ProtocolExtensionPtrTy;
537 /// MethodDescriptionTy - LLVM type for struct
538 /// objc_method_description.
539 const llvm::StructType *MethodDescriptionTy;
540 /// MethodDescriptionListTy - LLVM type for struct
541 /// objc_method_description_list.
542 const llvm::StructType *MethodDescriptionListTy;
543 /// MethodDescriptionListPtrTy - LLVM type for struct
544 /// objc_method_description_list *.
545 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000546 /// ProtocolListTy - LLVM type for struct objc_property_list.
547 const llvm::Type *ProtocolListTy;
548 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
549 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000550 /// CategoryTy - LLVM type for struct objc_category.
551 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000552 /// ClassTy - LLVM type for struct objc_class.
553 const llvm::StructType *ClassTy;
554 /// ClassPtrTy - LLVM type for struct objc_class *.
555 const llvm::Type *ClassPtrTy;
556 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
557 const llvm::StructType *ClassExtensionTy;
558 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
559 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000560 // IvarTy - LLVM type for struct objc_ivar.
561 const llvm::StructType *IvarTy;
562 /// IvarListTy - LLVM type for struct objc_ivar_list.
563 const llvm::Type *IvarListTy;
564 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
565 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000566 /// MethodListTy - LLVM type for struct objc_method_list.
567 const llvm::Type *MethodListTy;
568 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
569 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000570
Anders Carlsson124526b2008-09-09 10:10:21 +0000571 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
572 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000573
Anders Carlsson124526b2008-09-09 10:10:21 +0000574 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000575 llvm::Constant *getExceptionTryEnterFn() {
576 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000577 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000578 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000579 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000580 Params, false),
581 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000582 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000583
584 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000585 llvm::Constant *getExceptionTryExitFn() {
586 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000587 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000588 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000589 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000590 Params, false),
591 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000592 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000593
594 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000595 llvm::Constant *getExceptionExtractFn() {
596 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000597 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
598 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000599 Params, false),
600 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000601
Chris Lattner34b02a12009-04-22 02:26:14 +0000602 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000603
Anders Carlsson124526b2008-09-09 10:10:21 +0000604 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000605 llvm::Constant *getExceptionMatchFn() {
606 std::vector<const llvm::Type*> Params;
607 Params.push_back(ClassPtrTy);
608 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000609 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000610 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000611 Params, false),
612 "objc_exception_match");
613
Chris Lattner34b02a12009-04-22 02:26:14 +0000614 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000615
Anders Carlsson124526b2008-09-09 10:10:21 +0000616 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000617 llvm::Constant *getSetJmpFn() {
618 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000619 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000620 return
Owen Anderson0032b272009-08-13 21:57:51 +0000621 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000622 Params, false),
623 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000624
Chris Lattner34b02a12009-04-22 02:26:14 +0000625 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000626
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000627public:
628 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000629 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000630};
631
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000632/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000633/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000634class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000635public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000636
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000637 // MethodListnfABITy - LLVM for struct _method_list_t
638 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000639
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000640 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
641 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000642
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000643 // ProtocolnfABITy = LLVM for struct _protocol_t
644 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000645
Daniel Dunbar948e2582009-02-15 07:36:20 +0000646 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
647 const llvm::Type *ProtocolnfABIPtrTy;
648
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000649 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
650 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000651
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000652 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
653 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000654
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000655 // ClassnfABITy - LLVM for struct _class_t
656 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000657
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000658 // ClassnfABIPtrTy - LLVM for struct _class_t*
659 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000660
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000661 // IvarnfABITy - LLVM for struct _ivar_t
662 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000663
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000664 // IvarListnfABITy - LLVM for struct _ivar_list_t
665 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000666
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000667 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
668 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000669
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000670 // ClassRonfABITy - LLVM for struct _class_ro_t
671 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000672
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000673 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
674 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000675
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000676 // CategorynfABITy - LLVM for struct _category_t
677 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000678
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000679 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000680
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000681 // MessageRefTy - LLVM for:
682 // struct _message_ref_t {
683 // IMP messenger;
684 // SEL name;
685 // };
686 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000687 // MessageRefCTy - clang type for struct _message_ref_t
688 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000689
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000690 // MessageRefPtrTy - LLVM for struct _message_ref_t*
691 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000692 // MessageRefCPtrTy - clang type for struct _message_ref_t*
693 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000694
Fariborz Jahanianef163782009-02-05 01:13:09 +0000695 // MessengerTy - Type of the messenger (shown as IMP above)
696 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000697
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000698 // SuperMessageRefTy - LLVM for:
699 // struct _super_message_ref_t {
700 // SUPER_IMP messenger;
701 // SEL name;
702 // };
703 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000704
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000705 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
706 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000707
Chris Lattner1c02f862009-04-22 02:53:24 +0000708 llvm::Constant *getMessageSendFixupFn() {
709 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
710 std::vector<const llvm::Type*> Params;
711 Params.push_back(ObjectPtrTy);
712 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000713 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000714 Params, true),
715 "objc_msgSend_fixup");
716 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000717
Chris Lattner1c02f862009-04-22 02:53:24 +0000718 llvm::Constant *getMessageSendFpretFixupFn() {
719 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
720 std::vector<const llvm::Type*> Params;
721 Params.push_back(ObjectPtrTy);
722 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000723 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000724 Params, true),
725 "objc_msgSend_fpret_fixup");
726 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000727
Chris Lattner1c02f862009-04-22 02:53:24 +0000728 llvm::Constant *getMessageSendStretFixupFn() {
729 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
730 std::vector<const llvm::Type*> Params;
731 Params.push_back(ObjectPtrTy);
732 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000733 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000734 Params, true),
735 "objc_msgSend_stret_fixup");
736 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000737
Chris Lattner1c02f862009-04-22 02:53:24 +0000738 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000739 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000740 // struct _super_message_ref_t*, ...)
741 std::vector<const llvm::Type*> Params;
742 Params.push_back(SuperPtrTy);
743 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000744 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000745 Params, true),
746 "objc_msgSendSuper2_fixup");
747 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000748
Chris Lattner1c02f862009-04-22 02:53:24 +0000749 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000750 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000751 // struct _super_message_ref_t*, ...)
752 std::vector<const llvm::Type*> Params;
753 Params.push_back(SuperPtrTy);
754 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000755 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000756 Params, true),
757 "objc_msgSendSuper2_stret_fixup");
758 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000759
Chris Lattner8a569112009-04-22 02:15:23 +0000760 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000761 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000762 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000763 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000764
Chris Lattner8a569112009-04-22 02:15:23 +0000765 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000766
Chris Lattner8a569112009-04-22 02:15:23 +0000767 llvm::Constant *getObjCBeginCatchFn() {
768 std::vector<const llvm::Type*> Params;
769 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000770 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000771 Params, false),
772 "objc_begin_catch");
773 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000774
775 const llvm::StructType *EHTypeTy;
776 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000777
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000778 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
779 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000780};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000781
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000782class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000783public:
784 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000785 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000786 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000787 unsigned ivar_bytepos;
788 unsigned ivar_size;
789 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000790 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000791
792 // Allow sorting based on byte pos.
793 bool operator<(const GC_IVAR &b) const {
794 return ivar_bytepos < b.ivar_bytepos;
795 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000796 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000797
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000798 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000799 public:
800 unsigned skip;
801 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000802 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000803 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000804 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000805
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000806protected:
807 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000808 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000809 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000810 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000811
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000812 // gc ivar layout bitmap calculation helper caches.
813 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
814 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000815
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000816 /// LazySymbols - Symbols to generate a lazy reference for. See
817 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000818 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000819
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000820 /// DefinedSymbols - External symbols which are defined by this
821 /// module. The symbols in this list and LazySymbols are used to add
822 /// special linker symbols which ensure that Objective-C modules are
823 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000824 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000825
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000826 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000827 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000828
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000829 /// MethodVarNames - uniqued method variable names.
830 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000831
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000832 /// DefinedCategoryNames - list of category names in form Class_Category.
833 llvm::SetVector<std::string> DefinedCategoryNames;
834
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000835 /// MethodVarTypes - uniqued method type signatures. We have to use
836 /// a StringMap here because have no other unique reference.
837 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000838
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000839 /// MethodDefinitions - map of methods which have been defined in
840 /// this translation unit.
841 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000842
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000843 /// PropertyNames - uniqued method variable names.
844 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000845
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000846 /// ClassReferences - uniqued class references.
847 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000848
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000849 /// SelectorReferences - uniqued selector references.
850 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000851
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000852 /// Protocols - Protocols for which an objc_protocol structure has
853 /// been emitted. Forward declarations are handled by creating an
854 /// empty structure whose initializer is filled in when/if defined.
855 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000856
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000857 /// DefinedProtocols - Protocols which have actually been
858 /// defined. We should not need this, see FIXME in GenerateProtocol.
859 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000860
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000861 /// DefinedClasses - List of defined classes.
862 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000863
864 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
865 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000866
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000867 /// DefinedCategories - List of defined categories.
868 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000869
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000870 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
871 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000872
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000873 /// GetNameForMethod - Return a name for the given method.
874 /// \param[out] NameOut - The return value.
875 void GetNameForMethod(const ObjCMethodDecl *OMD,
876 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000877 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000878
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000879 /// GetMethodVarName - Return a unique constant for the given
880 /// selector's name. The return value has type char *.
881 llvm::Constant *GetMethodVarName(Selector Sel);
882 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000883
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000884 /// GetMethodVarType - Return a unique constant for the given
885 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000886
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000887 // FIXME: This is a horrible name.
888 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000889 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000890
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000891 /// GetPropertyName - Return a unique constant for the given
892 /// name. The return value has type char *.
893 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000894
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000895 // FIXME: This can be dropped once string functions are unified.
896 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
897 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000898
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000899 /// GetClassName - Return a unique constant for the given selector's
900 /// name. The return value has type char *.
901 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000902
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000903 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
904
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000905 /// BuildIvarLayout - Builds ivar layout bitmap for the class
906 /// implementation for the __strong or __weak case.
907 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000908 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
909 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000910
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000911 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000912
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000913 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000914 unsigned int BytePos, bool ForStrongLayout,
915 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000916 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000917 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000918 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000919 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000920 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000921 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000922
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000923 /// GetIvarLayoutName - Returns a unique constant for the given
924 /// ivar layout bitmap.
925 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
926 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000927
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000928 /// EmitPropertyList - Emit the given property list. The return
929 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000930 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000931 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000932 const ObjCContainerDecl *OCD,
933 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000934
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000935 /// PushProtocolProperties - Push protocol's property on the input stack.
936 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
937 std::vector<llvm::Constant*> &Properties,
938 const Decl *Container,
939 const ObjCProtocolDecl *PROTO,
940 const ObjCCommonTypesHelper &ObjCTypes);
941
Fariborz Jahanianda320092009-01-29 19:24:30 +0000942 /// GetProtocolRef - Return a reference to the internal protocol
943 /// description, creating an empty one if it has not been
944 /// defined. The return value has type ProtocolPtrTy.
945 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000946
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000947 /// CreateMetadataVar - Create a global variable with internal
948 /// linkage for use by the Objective-C runtime.
949 ///
950 /// This is a convenience wrapper which not only creates the
951 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000952 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000953 ///
954 /// \param Name - The variable name.
955 /// \param Init - The variable initializer; this is also used to
956 /// define the type of the variable.
957 /// \param Section - The section the variable should go into, or 0.
958 /// \param Align - The alignment for the variable, or 0.
959 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000960 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000961 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000962 llvm::Constant *Init,
963 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000964 unsigned Align,
965 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000966
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000967 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +0000968 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000969 QualType ResultType,
970 llvm::Value *Sel,
971 llvm::Value *Arg0,
972 QualType Arg0Ty,
973 bool IsSuper,
974 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +0000975 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000976 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000977
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000978 /// EmitImageInfo - Emit the image info marker used to encode some module
979 /// level information.
980 void EmitImageInfo();
981
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000982public:
Owen Anderson69243822009-07-13 04:10:07 +0000983 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000984 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000985
David Chisnall0d13f6f2010-01-23 02:40:42 +0000986 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000987
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000988 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
989 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000990
Fariborz Jahanianda320092009-01-29 19:24:30 +0000991 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000992
Fariborz Jahanianda320092009-01-29 19:24:30 +0000993 /// GetOrEmitProtocol - Get the protocol object for the given
994 /// declaration, emitting it if necessary. The return value has type
995 /// ProtocolPtrTy.
996 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000997
Fariborz Jahanianda320092009-01-29 19:24:30 +0000998 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
999 /// object for the given declaration, emitting it if needed. These
1000 /// forward references will be filled in with empty bodies if no
1001 /// definition is seen. The return value has type ProtocolPtrTy.
1002 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001003 virtual llvm::Constant *GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001004 const llvm::SmallVectorImpl<const Expr *> &);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001005
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001006};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001007
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001008class CGObjCMac : public CGObjCCommonMac {
1009private:
1010 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001011
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001012 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001013 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001014 void EmitModuleInfo();
1015
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001016 /// EmitModuleSymols - Emit module symbols, the list of defined
1017 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001018 llvm::Constant *EmitModuleSymbols();
1019
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001020 /// FinishModule - Write out global data structures at the end of
1021 /// processing a translation unit.
1022 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001023
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001024 /// EmitClassExtension - Generate the class extension structure used
1025 /// to store the weak ivar layout and properties. The return value
1026 /// has type ClassExtensionPtrTy.
1027 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1028
1029 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1030 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001031 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001032 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001033
1034 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1035 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001036
1037 /// EmitIvarList - Emit the ivar list for the given
1038 /// implementation. If ForClass is true the list of class ivars
1039 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1040 /// interface ivars will be emitted. The return value has type
1041 /// IvarListPtrTy.
1042 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001043 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001044
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001045 /// EmitMetaClass - Emit a forward reference to the class structure
1046 /// for the metaclass of the given interface. The return value has
1047 /// type ClassPtrTy.
1048 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1049
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001050 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001051 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001052 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1053 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001054 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001055
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001056 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001057
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001058 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001059
1060 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001061 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001062 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001063 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001064 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001065
1066 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001067 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001068 /// - TypeName: The name for the type containing the methods.
1069 /// - IsProtocol: True iff these methods are for a protocol.
1070 /// - ClassMethds: True iff these are class methods.
1071 /// - Required: When true, only "required" methods are
1072 /// listed. Similarly, when false only "optional" methods are
1073 /// listed. For classes this should always be true.
1074 /// - begin, end: The method list to output.
1075 ///
1076 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001077 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001078 const char *Section,
1079 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001080
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001081 /// GetOrEmitProtocol - Get the protocol object for the given
1082 /// declaration, emitting it if necessary. The return value has type
1083 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001084 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001085
1086 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1087 /// object for the given declaration, emitting it if needed. These
1088 /// forward references will be filled in with empty bodies if no
1089 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001090 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001091
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001092 /// EmitProtocolExtension - Generate the protocol extension
1093 /// structure used to store optional instance and class methods, and
1094 /// protocol properties. The return value has type
1095 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001096 llvm::Constant *
1097 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1098 const ConstantVector &OptInstanceMethods,
1099 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001100
1101 /// EmitProtocolList - Generate the list of referenced
1102 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001103 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001104 ObjCProtocolDecl::protocol_iterator begin,
1105 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001106
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001107 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1108 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001109 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1110 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001111
1112public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001113 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001114
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001115 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001116
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001117 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001118 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001119 QualType ResultType,
1120 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001121 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001122 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001123 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001124 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001125
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001126 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001127 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001128 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001129 QualType ResultType,
1130 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001131 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001132 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001133 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001134 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001135 const CallArgList &CallArgs,
1136 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001137
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001138 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001139 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001140
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001141 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1142 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001143
1144 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1145 /// untyped one.
1146 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1147 const ObjCMethodDecl *Method);
1148
John McCall5a180392010-07-24 00:37:23 +00001149 virtual llvm::Constant *GetEHType(QualType T);
1150
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001151 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001152
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001153 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001154
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001155 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001156 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001157
Chris Lattner74391b42009-03-22 21:03:39 +00001158 virtual llvm::Constant *GetPropertyGetFunction();
1159 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001160 virtual llvm::Constant *GetGetStructFunction();
1161 virtual llvm::Constant *GetSetStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001162 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001163
John McCallf1549f62010-07-06 01:34:17 +00001164 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1165 const ObjCAtTryStmt &S);
1166 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1167 const ObjCAtSynchronizedStmt &S);
1168 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001169 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1170 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001171 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001172 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001173 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001174 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001175 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001176 llvm::Value *src, llvm::Value *dest,
1177 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001178 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001179 llvm::Value *src, llvm::Value *dest,
1180 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001181 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1182 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001183 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1184 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001185 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001186
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001187 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1188 QualType ObjectTy,
1189 llvm::Value *BaseValue,
1190 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001191 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001192 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001193 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001194 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001195};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001196
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001197class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001198private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001199 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001200 llvm::GlobalVariable* ObjCEmptyCacheVar;
1201 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001202
Daniel Dunbar11394522009-04-18 08:51:00 +00001203 /// SuperClassReferences - uniqued super class references.
1204 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001205
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001206 /// MetaClassReferences - uniqued meta class references.
1207 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001208
1209 /// EHTypeReferences - uniqued class ehtype references.
1210 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001211
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001212 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001213 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001214 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001215
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001216 /// DefinedMetaClasses - List of defined meta-classes.
1217 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1218
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001219 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001220 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001221 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001222
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001223 /// FinishNonFragileABIModule - Write out global data structures at the end of
1224 /// processing a translation unit.
1225 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001226
Daniel Dunbar463b8762009-05-15 21:48:48 +00001227 /// AddModuleClassList - Add the given list of class pointers to the
1228 /// module with the provided symbol and section names.
1229 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1230 const char *SymbolName,
1231 const char *SectionName);
1232
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001233 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1234 unsigned InstanceStart,
1235 unsigned InstanceSize,
1236 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001237 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001238 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001239 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001240 llvm::Constant *ClassRoGV,
1241 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001242
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001243 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001244
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001245 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001246
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001247 /// EmitMethodList - Emit the method list for the given
1248 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001249 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001250 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001251 const ConstantVector &Methods);
1252 /// EmitIvarList - Emit the ivar list for the given
1253 /// implementation. If ForClass is true the list of class ivars
1254 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1255 /// interface ivars will be emitted. The return value has type
1256 /// IvarListnfABIPtrTy.
1257 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001258
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001259 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001260 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001261 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001262
Fariborz Jahanianda320092009-01-29 19:24:30 +00001263 /// GetOrEmitProtocol - Get the protocol object for the given
1264 /// declaration, emitting it if necessary. The return value has type
1265 /// ProtocolPtrTy.
1266 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001267
Fariborz Jahanianda320092009-01-29 19:24:30 +00001268 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1269 /// object for the given declaration, emitting it if needed. These
1270 /// forward references will be filled in with empty bodies if no
1271 /// definition is seen. The return value has type ProtocolPtrTy.
1272 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001273
Fariborz Jahanianda320092009-01-29 19:24:30 +00001274 /// EmitProtocolList - Generate the list of referenced
1275 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001276 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001277 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001278 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001279
Fariborz Jahanian46551122009-02-04 00:22:57 +00001280 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001281 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001282 QualType ResultType,
1283 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001284 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001285 QualType Arg0Ty,
1286 bool IsSuper,
1287 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001288
1289 /// GetClassGlobal - Return the global variable for the Objective-C
1290 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001291 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001292
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001293 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001294 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001295 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001296 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001297
Daniel Dunbar11394522009-04-18 08:51:00 +00001298 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1299 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001300 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1301 const ObjCInterfaceDecl *ID);
1302
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001303 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1304 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001305 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001306 const ObjCInterfaceDecl *ID);
1307
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001308 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1309 /// the given ivar.
1310 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001311 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001312 const ObjCInterfaceDecl *ID,
1313 const ObjCIvarDecl *Ivar);
1314
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001315 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1316 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001317 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1318 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001319
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001320 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001321 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001322 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001323 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001324
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001325 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001326 return "OBJC_METACLASS_$_";
1327 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001328
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001329 const char *getClassSymbolPrefix() const {
1330 return "OBJC_CLASS_$_";
1331 }
1332
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001333 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001334 uint32_t &InstanceStart,
1335 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001336
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001337 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001338 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001339 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1340 return CGM.getContext().Selectors.getSelector(0, &II);
1341 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001342
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001343 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001344 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1345 return CGM.getContext().Selectors.getSelector(1, &II);
1346 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001347
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001348 /// ImplementationIsNonLazy - Check whether the given category or
1349 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001350 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001351
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001352public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001353 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001354 // FIXME. All stubs for now!
1355 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001356
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001357 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001358 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001359 QualType ResultType,
1360 Selector Sel,
1361 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001362 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001363 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001364 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001365
1366 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001367 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001368 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001369 QualType ResultType,
1370 Selector Sel,
1371 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001372 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001373 llvm::Value *Receiver,
1374 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001375 const CallArgList &CallArgs,
1376 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001377
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001378 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001379 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001380
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001381 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1382 bool lvalue = false)
1383 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001384
1385 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1386 /// untyped one.
1387 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1388 const ObjCMethodDecl *Method)
1389 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001390
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001391 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001392
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001393 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001394 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001395 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001396
John McCall5a180392010-07-24 00:37:23 +00001397 virtual llvm::Constant *GetEHType(QualType T);
1398
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001399 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001400 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001401 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001402 virtual llvm::Constant *GetPropertySetFunction() {
1403 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001404 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001405
David Chisnall8fac25d2010-12-26 22:13:16 +00001406 virtual llvm::Constant *GetSetStructFunction() {
1407 return ObjCTypes.getCopyStructFn();
1408 }
1409 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001410 return ObjCTypes.getCopyStructFn();
1411 }
1412
Chris Lattner74391b42009-03-22 21:03:39 +00001413 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001414 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001415 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001416
John McCallf1549f62010-07-06 01:34:17 +00001417 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1418 const ObjCAtTryStmt &S);
1419 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1420 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001421 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001422 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001423 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001424 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001425 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001426 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001427 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001428 llvm::Value *src, llvm::Value *dest,
1429 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001430 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001431 llvm::Value *src, llvm::Value *dest,
1432 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001433 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001434 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001435 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1436 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001437 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001438 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1439 QualType ObjectTy,
1440 llvm::Value *BaseValue,
1441 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001442 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001443 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001444 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001445 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001446};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001447
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001448} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001449
1450/* *** Helper Functions *** */
1451
1452/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001453static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001454 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001455 unsigned idx0,
1456 unsigned idx1) {
1457 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001458 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1459 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001460 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001461 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001462}
1463
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001464/// hasObjCExceptionAttribute - Return true if this class or any super
1465/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001466static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001467 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001468 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001469 return true;
1470 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001471 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001472 return false;
1473}
1474
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001475/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001476
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001477CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001478 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001479 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001480 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001481}
1482
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001483/// GetClass - Return a reference to the class for the given interface
1484/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001485llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001486 const ObjCInterfaceDecl *ID) {
1487 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001488}
1489
1490/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001491llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1492 bool lval) {
1493 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001494}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001495llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001496 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001497 return EmitSelector(Builder, Method->getSelector());
1498}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001499
John McCall5a180392010-07-24 00:37:23 +00001500llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1501 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1502 return 0;
1503}
1504
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001505/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001506/*
1507 struct __builtin_CFString {
1508 const int *isa; // point to __CFConstantStringClassReference
1509 int flags;
1510 const char *str;
1511 long length;
1512 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001513*/
1514
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001515/// or Generate a constant NSString object.
1516/*
1517 struct __builtin_NSString {
1518 const int *isa; // point to __NSConstantStringClassReference
1519 const char *str;
1520 unsigned int length;
1521 };
1522*/
1523
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001524llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001525 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001526 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1527 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001528 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001529}
1530
1531/// Generates a message send where the super is the receiver. This is
1532/// a message send to self with special delivery semantics indicating
1533/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001534CodeGen::RValue
1535CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001536 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001537 QualType ResultType,
1538 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001539 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001540 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001541 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001542 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001543 const CodeGen::CallArgList &CallArgs,
1544 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001545 // Create and init a super structure; this is a (receiver, class)
1546 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001547 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001548 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001549 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001550 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001551 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001552 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001553
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001554 // If this is a class message the metaclass is passed as the target.
1555 llvm::Value *Target;
1556 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001557 if (isCategoryImpl) {
1558 // Message sent to 'super' in a class method defined in a category
1559 // implementation requires an odd treatment.
1560 // If we are in a class method, we must retrieve the
1561 // _metaclass_ for the current class, pointed at by
1562 // the class's "isa" pointer. The following assumes that
1563 // isa" is the first ivar in a class (which it must be).
1564 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1565 Target = CGF.Builder.CreateStructGEP(Target, 0);
1566 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001567 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001568 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1569 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1570 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1571 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001572 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001573 }
1574 else if (isCategoryImpl)
1575 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1576 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001577 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1578 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1579 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001580 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001581 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1582 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001583 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001584 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001585 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001586 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001587 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001588 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001589 EmitSelector(CGF.Builder, Sel),
1590 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001591 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001592}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001593
1594/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001595CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001596 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001597 QualType ResultType,
1598 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001599 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001600 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001601 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001602 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001603 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001604 EmitSelector(CGF.Builder, Sel),
1605 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001606 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001607}
1608
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001609CodeGen::RValue
1610CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001611 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001612 QualType ResultType,
1613 llvm::Value *Sel,
1614 llvm::Value *Arg0,
1615 QualType Arg0Ty,
1616 bool IsSuper,
1617 const CallArgList &CallArgs,
1618 const ObjCMethodDecl *Method,
1619 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001620 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001621 if (!IsSuper)
1622 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001623 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001624 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001625 CGF.getContext().getObjCSelType()));
1626 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001627
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001628 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001629 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001630 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001631 const llvm::FunctionType *FTy =
1632 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001633
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001634 if (Method)
1635 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1636 CGM.getContext().getCanonicalType(ResultType) &&
1637 "Result type mismatch!");
1638
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001639 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001640 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001641 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001642 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001643 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1644 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1645 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001646 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001647 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001648 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001649 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001650 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001651 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001652}
1653
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001654static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1655 if (FQT.isObjCGCStrong())
1656 return Qualifiers::Strong;
1657
1658 if (FQT.isObjCGCWeak())
1659 return Qualifiers::Weak;
1660
1661 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1662 return Qualifiers::Strong;
1663
1664 if (const PointerType *PT = FQT->getAs<PointerType>())
1665 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1666
1667 return Qualifiers::GCNone;
1668}
1669
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001670llvm::Constant *CGObjCCommonMac::GCBlockLayout(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001671 const llvm::SmallVectorImpl<const Expr *> &BlockLayout) {
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001672 llvm::Constant *NullPtr =
1673 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
Fariborz Jahanianfb550312010-09-13 16:09:44 +00001674 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001675 return NullPtr;
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001676 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001677 SkipIvars.clear();
1678 IvarsInfo.clear();
1679 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
1680 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
1681
Fariborz Jahanian81979822010-09-09 00:21:45 +00001682 // __isa is the first field in block descriptor and must assume by runtime's
1683 // convention that it is GC'able.
1684 IvarsInfo.push_back(GC_IVAR(0, 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001685 for (size_t i = 0; i < BlockLayout.size(); ++i) {
1686 const Expr *E = BlockLayout[i];
1687 const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E);
1688 if (!BDRE)
1689 continue;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001690 const ValueDecl *VD = BDRE->getDecl();
1691 CharUnits Offset = CGF.BlockDecls[VD];
1692 uint64_t FieldOffset = Offset.getQuantity();
1693 QualType Ty = VD->getType();
1694 assert(!Ty->isArrayType() &&
1695 "Array block variable should have been caught");
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001696 if ((Ty->isRecordType() || Ty->isUnionType()) && !BDRE->isByRef()) {
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001697 BuildAggrIvarRecordLayout(Ty->getAs<RecordType>(),
1698 FieldOffset,
1699 true,
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001700 hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001701 continue;
1702 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001703
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001704 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), Ty);
1705 unsigned FieldSize = CGM.getContext().getTypeSize(Ty);
1706 // __block variables are passed by their descriptior address. So, size
1707 // must reflect this.
1708 if (BDRE->isByRef())
1709 FieldSize = WordSizeInBits;
1710 if (GCAttr == Qualifiers::Strong || BDRE->isByRef())
1711 IvarsInfo.push_back(GC_IVAR(FieldOffset,
1712 FieldSize / WordSizeInBits));
1713 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
1714 SkipIvars.push_back(GC_IVAR(FieldOffset,
1715 FieldSize / ByteSizeInBits));
1716 }
1717
1718 if (IvarsInfo.empty())
1719 return NullPtr;
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001720 // Sort on byte position in case we encounterred a union nested in
1721 // block variable type's aggregate type.
1722 if (hasUnion && !IvarsInfo.empty())
1723 std::sort(IvarsInfo.begin(), IvarsInfo.end());
1724 if (hasUnion && !SkipIvars.empty())
1725 std::sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001726
1727 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001728 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001729 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1730 printf("\n block variable layout for block: ");
1731 const unsigned char *s = (unsigned char*)BitMap.c_str();
1732 for (unsigned i = 0; i < BitMap.size(); i++)
1733 if (!(s[i] & 0xf0))
1734 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1735 else
1736 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1737 printf("\n");
1738 }
1739
1740 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001741}
1742
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001743llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001744 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001745 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001746 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001747 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1748
Owen Anderson3c4972d2009-07-29 18:54:39 +00001749 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001750 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001751}
1752
Fariborz Jahanianda320092009-01-29 19:24:30 +00001753void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001754 // FIXME: We shouldn't need this, the protocol decl should contain enough
1755 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001756 DefinedProtocols.insert(PD->getIdentifier());
1757
1758 // If we have generated a forward reference to this protocol, emit
1759 // it now. Otherwise do nothing, the protocol objects are lazily
1760 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001761 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001762 GetOrEmitProtocol(PD);
1763}
1764
Fariborz Jahanianda320092009-01-29 19:24:30 +00001765llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001766 if (DefinedProtocols.count(PD->getIdentifier()))
1767 return GetOrEmitProtocol(PD);
1768 return GetOrEmitProtocolRef(PD);
1769}
1770
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001771/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001772// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1773struct _objc_protocol {
1774struct _objc_protocol_extension *isa;
1775char *protocol_name;
1776struct _objc_protocol_list *protocol_list;
1777struct _objc__method_prototype_list *instance_methods;
1778struct _objc__method_prototype_list *class_methods
1779};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001780
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001781See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001782*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001783llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1784 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1785
1786 // Early exit if a defining object has already been generated.
1787 if (Entry && Entry->hasInitializer())
1788 return Entry;
1789
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001790 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001791 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001792 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1793
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001794 // Construct method lists.
1795 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1796 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001797 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001798 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001799 ObjCMethodDecl *MD = *i;
1800 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1801 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1802 OptInstanceMethods.push_back(C);
1803 } else {
1804 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001805 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001806 }
1807
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001808 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001809 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001810 ObjCMethodDecl *MD = *i;
1811 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1812 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1813 OptClassMethods.push_back(C);
1814 } else {
1815 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001816 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001817 }
1818
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001819 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001820 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001821 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001822 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001823 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001824 PD->protocol_begin(),
1825 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001826 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001827 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001828 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1829 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001830 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001831 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001832 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1833 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001834 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001835 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001836
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001837 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001838 // Already created, fix the linkage and update the initializer.
1839 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001840 Entry->setInitializer(Init);
1841 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001842 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001843 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001844 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001845 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001846 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001847 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001848 // FIXME: Is this necessary? Why only for protocol?
1849 Entry->setAlignment(4);
1850 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001851 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001852
1853 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001854}
1855
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001856llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001857 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1858
1859 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001860 // We use the initializer as a marker of whether this is a forward
1861 // reference or not. At module finalization we add the empty
1862 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001863 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001864 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001865 llvm::GlobalValue::ExternalLinkage,
1866 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001867 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001868 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001869 // FIXME: Is this necessary? Why only for protocol?
1870 Entry->setAlignment(4);
1871 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001872
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001873 return Entry;
1874}
1875
1876/*
1877 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001878 uint32_t size;
1879 struct objc_method_description_list *optional_instance_methods;
1880 struct objc_method_description_list *optional_class_methods;
1881 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001882 };
1883*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001884llvm::Constant *
1885CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1886 const ConstantVector &OptInstanceMethods,
1887 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001888 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001889 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001890 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001891 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001892 Values[1] =
1893 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001894 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001895 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1896 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001897 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001898 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001899 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1900 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001901 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001902 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001903
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001904 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001905 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001906 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001907 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001908
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001909 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001910 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001911
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001912 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001913 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001914 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001915 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001916}
1917
1918/*
1919 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001920 struct objc_protocol_list *next;
1921 long count;
1922 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001923 };
1924*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001925llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001926CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001927 ObjCProtocolDecl::protocol_iterator begin,
1928 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001929 std::vector<llvm::Constant*> ProtocolRefs;
1930
Daniel Dunbardbc933702008-08-21 21:57:41 +00001931 for (; begin != end; ++begin)
1932 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001933
1934 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001935 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001936 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001937
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001938 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001939 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001940
1941 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001942 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001943 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001944 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001945 ProtocolRefs.size() - 1);
1946 Values[2] =
1947 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1948 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001949 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001950
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001951 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001952 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001953 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001954 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001955 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001956}
1957
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001958void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1959 std::vector<llvm::Constant*> &Properties,
1960 const Decl *Container,
1961 const ObjCProtocolDecl *PROTO,
1962 const ObjCCommonTypesHelper &ObjCTypes) {
1963 std::vector<llvm::Constant*> Prop(2);
1964 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1965 E = PROTO->protocol_end(); P != E; ++P)
1966 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1967 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1968 E = PROTO->prop_end(); I != E; ++I) {
1969 const ObjCPropertyDecl *PD = *I;
1970 if (!PropertySet.insert(PD->getIdentifier()))
1971 continue;
1972 Prop[0] = GetPropertyName(PD->getIdentifier());
1973 Prop[1] = GetPropertyTypeString(PD, Container);
1974 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1975 }
1976}
1977
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001978/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001979 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001980 const char * const name;
1981 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001982 };
1983
1984 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001985 uint32_t entsize; // sizeof (struct _objc_property)
1986 uint32_t prop_count;
1987 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001988 };
1989*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001990llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001991 const Decl *Container,
1992 const ObjCContainerDecl *OCD,
1993 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001994 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001995 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001996 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1997 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001998 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001999 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002000 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002001 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00002002 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002003 Prop));
2004 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002005 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002006 for (ObjCInterfaceDecl::all_protocol_iterator
2007 P = OID->all_referenced_protocol_begin(),
2008 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002009 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2010 ObjCTypes);
2011 }
2012 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2013 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2014 E = CD->protocol_end(); P != E; ++P)
2015 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2016 ObjCTypes);
2017 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002018
2019 // Return null for empty list.
2020 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002021 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002022
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002023 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002024 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002025 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002026 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2027 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002028 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002029 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002030 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002031 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002032
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002033 llvm::GlobalVariable *GV =
2034 CreateMetadataVar(Name, Init,
2035 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002036 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002037 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002038 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002039 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002040}
2041
2042/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002043 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002044 int count;
2045 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002046 };
2047*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002048llvm::Constant *
2049CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
2050 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00002051 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002052 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
2053 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002054 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00002055 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002056 Desc);
2057}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002058
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002059llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002060 const char *Section,
2061 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002062 // Return null for empty list.
2063 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002064 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002065
2066 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002067 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002068 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002069 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002070 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002071 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002072
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002073 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002074 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002075 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002076}
2077
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002078/*
2079 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002080 char *category_name;
2081 char *class_name;
2082 struct _objc_method_list *instance_methods;
2083 struct _objc_method_list *class_methods;
2084 struct _objc_protocol_list *protocols;
2085 uint32_t size; // <rdar://4585769>
2086 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002087 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002088*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002089void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002090 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002091
Mike Stumpf5408fe2009-05-16 07:57:57 +00002092 // FIXME: This is poor design, the OCD should have a pointer to the category
2093 // decl. Additionally, note that Category can be null for the @implementation
2094 // w/o an @interface case. Sema should just create one for us as it does for
2095 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002096 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002097 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002098 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002099
2100 llvm::SmallString<256> ExtName;
2101 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2102 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002103
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002104 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002105 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002106 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002107 // Instance methods should always be defined.
2108 InstanceMethods.push_back(GetMethodConstant(*i));
2109 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002110 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002111 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002112 // Class methods should always be defined.
2113 ClassMethods.push_back(GetMethodConstant(*i));
2114 }
2115
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002116 std::vector<llvm::Constant*> Values(7);
2117 Values[0] = GetClassName(OCD->getIdentifier());
2118 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002119 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002120 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002121 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002122 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002123 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002124 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002125 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002126 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002127 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002128 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002129 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002130 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002131 Category->protocol_begin(),
2132 Category->protocol_end());
2133 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002134 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002135 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002136 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002137
2138 // If there is no category @interface then there can be no properties.
2139 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002140 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002141 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002142 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002143 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002144 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002145
Owen Anderson08e25242009-07-27 22:29:56 +00002146 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002147 Values);
2148
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002149 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002150 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002151 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002152 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002153 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002154 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002155}
2156
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002157// FIXME: Get from somewhere?
2158enum ClassFlags {
2159 eClassFlags_Factory = 0x00001,
2160 eClassFlags_Meta = 0x00002,
2161 // <rdr://5142207>
2162 eClassFlags_HasCXXStructors = 0x02000,
2163 eClassFlags_Hidden = 0x20000,
2164 eClassFlags_ABI2_Hidden = 0x00010,
2165 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2166};
2167
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002168/*
2169 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002170 Class isa;
2171 Class super_class;
2172 const char *name;
2173 long version;
2174 long info;
2175 long instance_size;
2176 struct _objc_ivar_list *ivars;
2177 struct _objc_method_list *methods;
2178 struct _objc_cache *cache;
2179 struct _objc_protocol_list *protocols;
2180 // Objective-C 1.0 extensions (<rdr://4585769>)
2181 const char *ivar_layout;
2182 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002183 };
2184
2185 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002186*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002187void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002188 DefinedSymbols.insert(ID->getIdentifier());
2189
Chris Lattner8ec03f52008-11-24 03:54:41 +00002190 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002191 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002192 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002193 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002194 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002195 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002196 Interface->all_referenced_protocol_begin(),
2197 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002198 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002199 if (ID->getNumIvarInitializers())
2200 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002201 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002202 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002203
2204 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002205 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002206 Flags |= eClassFlags_Hidden;
2207
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002208 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002209 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002210 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002211 // Instance methods should always be defined.
2212 InstanceMethods.push_back(GetMethodConstant(*i));
2213 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002214 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002215 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002216 // Class methods should always be defined.
2217 ClassMethods.push_back(GetMethodConstant(*i));
2218 }
2219
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002220 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002221 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002222 ObjCPropertyImplDecl *PID = *i;
2223
2224 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2225 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2226
2227 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2228 if (llvm::Constant *C = GetMethodConstant(MD))
2229 InstanceMethods.push_back(C);
2230 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2231 if (llvm::Constant *C = GetMethodConstant(MD))
2232 InstanceMethods.push_back(C);
2233 }
2234 }
2235
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002236 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002237 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002238 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002239 // Record a reference to the super class.
2240 LazySymbols.insert(Super->getIdentifier());
2241
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002242 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002243 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002244 ObjCTypes.ClassPtrTy);
2245 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002246 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002247 }
2248 Values[ 2] = GetClassName(ID->getIdentifier());
2249 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002250 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2251 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2252 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002253 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002254 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002255 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002256 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002257 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002258 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002259 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002260 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002261 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002262 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002263 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002264 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002265 std::string Name("\01L_OBJC_CLASS_");
2266 Name += ClassName;
2267 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2268 // Check for a forward reference.
2269 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2270 if (GV) {
2271 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2272 "Forward metaclass reference has incorrect type.");
2273 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2274 GV->setInitializer(Init);
2275 GV->setSection(Section);
2276 GV->setAlignment(4);
2277 CGM.AddUsedGlobal(GV);
2278 }
2279 else
2280 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002281 DefinedClasses.push_back(GV);
2282}
2283
2284llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2285 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002286 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002287 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002288 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002289
John McCall1fb0caa2010-10-22 21:05:15 +00002290 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002291 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002292
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002293 std::vector<llvm::Constant*> Values(12);
2294 // The isa for the metaclass is the root of the hierarchy.
2295 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2296 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2297 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002298 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002299 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002300 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002301 // The super class for the metaclass is emitted as the name of the
2302 // super class. The runtime fixes this up to point to the
2303 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002304 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002305 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002306 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002307 ObjCTypes.ClassPtrTy);
2308 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002309 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002310 }
2311 Values[ 2] = GetClassName(ID->getIdentifier());
2312 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002313 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2314 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2315 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002316 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002317 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002318 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002319 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002320 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002321 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002322 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002323 Values[ 9] = Protocols;
2324 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002325 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002326 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002327 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002328 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002329 Values);
2330
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002331 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002332 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002333
2334 // Check for a forward reference.
2335 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2336 if (GV) {
2337 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2338 "Forward metaclass reference has incorrect type.");
2339 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2340 GV->setInitializer(Init);
2341 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002342 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002343 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002344 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002345 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002346 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002347 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002348 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002349
2350 return GV;
2351}
2352
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002353llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002354 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002355
Mike Stumpf5408fe2009-05-16 07:57:57 +00002356 // FIXME: Should we look these up somewhere other than the module. Its a bit
2357 // silly since we only generate these while processing an implementation, so
2358 // exactly one pointer would work if know when we entered/exitted an
2359 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002360
2361 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002362 // Previously, metaclass with internal linkage may have been defined.
2363 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002364 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2365 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002366 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2367 "Forward metaclass reference has incorrect type.");
2368 return GV;
2369 } else {
2370 // Generate as an external reference to keep a consistent
2371 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002372 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002373 llvm::GlobalValue::ExternalLinkage,
2374 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002375 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002376 }
2377}
2378
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002379llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2380 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2381
2382 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2383 true)) {
2384 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2385 "Forward class metadata reference has incorrect type.");
2386 return GV;
2387 } else {
2388 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2389 llvm::GlobalValue::ExternalLinkage,
2390 0,
2391 Name);
2392 }
2393}
2394
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002395/*
2396 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002397 uint32_t size;
2398 const char *weak_ivar_layout;
2399 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002400 };
2401*/
2402llvm::Constant *
2403CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002404 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002405 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002406
2407 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002408 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002409 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002410 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002411 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002412
2413 // Return null if no extension bits are used.
2414 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002415 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002416
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002417 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002418 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002419 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002420 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002421 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002422}
2423
2424/*
2425 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002426 char *ivar_name;
2427 char *ivar_type;
2428 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002429 };
2430
2431 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002432 int ivar_count;
2433 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002434 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002435*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002436llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002437 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002438 std::vector<llvm::Constant*> Ivars, Ivar(3);
2439
2440 // When emitting the root class GCC emits ivar entries for the
2441 // actual class structure. It is not clear if we need to follow this
2442 // behavior; for now lets try and get away with not doing it. If so,
2443 // the cleanest solution would be to make up an ObjCInterfaceDecl
2444 // for the class.
2445 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002446 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002447
2448 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002449 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002450
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002451 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002452 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002453
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002454 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2455 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002456 // Ignore unnamed bit-fields.
2457 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002458 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002459 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2460 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002461 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002462 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002463 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002464 }
2465
2466 // Return null for empty list.
2467 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002468 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002469
2470 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002471 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002472 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002473 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002474 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002475 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002476
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002477 llvm::GlobalVariable *GV;
2478 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002479 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002480 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002481 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002482 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002483 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002484 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002485 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002486 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002487}
2488
2489/*
2490 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002491 SEL method_name;
2492 char *method_types;
2493 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002494 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002495
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002496 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002497 struct objc_method_list *obsolete;
2498 int count;
2499 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002500 };
2501*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002502
2503/// GetMethodConstant - Return a struct objc_method constant for the
2504/// given method if it has been defined. The result is null if the
2505/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002506llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002507 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002508 if (!Fn)
2509 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002510
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002511 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002512 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002513 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002514 ObjCTypes.SelectorPtrTy);
2515 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002516 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002517 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002518}
2519
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002520llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002521 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002522 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002523 // Return null for empty list.
2524 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002525 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002526
2527 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002528 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002529 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002530 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002531 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002532 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002533 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002534
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002535 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002536 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002537 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002538}
2539
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002540llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002541 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002542 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002543 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002544
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002545 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002546 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002547 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002548 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002549 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002550 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002551 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002552 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002553 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002554
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002555 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002556}
2557
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002558llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002559CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002560 llvm::Constant *Init,
2561 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002562 unsigned Align,
2563 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002564 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002565 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002566 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002567 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002568 if (Section)
2569 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002570 if (Align)
2571 GV->setAlignment(Align);
2572 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002573 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002574 return GV;
2575}
2576
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002577llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002578 // Abuse this interface function as a place to finalize.
2579 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002580 return NULL;
2581}
2582
Chris Lattner74391b42009-03-22 21:03:39 +00002583llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002584 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002585}
2586
Chris Lattner74391b42009-03-22 21:03:39 +00002587llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002588 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002589}
2590
David Chisnall8fac25d2010-12-26 22:13:16 +00002591llvm::Constant *CGObjCMac::GetGetStructFunction() {
2592 return ObjCTypes.getCopyStructFn();
2593}
2594llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002595 return ObjCTypes.getCopyStructFn();
2596}
2597
Chris Lattner74391b42009-03-22 21:03:39 +00002598llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002599 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002600}
2601
John McCallf1549f62010-07-06 01:34:17 +00002602void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2603 return EmitTryOrSynchronizedStmt(CGF, S);
2604}
2605
2606void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2607 const ObjCAtSynchronizedStmt &S) {
2608 return EmitTryOrSynchronizedStmt(CGF, S);
2609}
2610
John McCallcc505292010-07-21 06:59:36 +00002611namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002612 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002613 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002614 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002615 llvm::Value *CallTryExitVar;
2616 llvm::Value *ExceptionData;
2617 ObjCTypesHelper &ObjCTypes;
2618 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002619 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002620 llvm::Value *CallTryExitVar,
2621 llvm::Value *ExceptionData,
2622 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002623 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002624 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2625
2626 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2627 // Check whether we need to call objc_exception_try_exit.
2628 // In optimized code, this branch will always be folded.
2629 llvm::BasicBlock *FinallyCallExit =
2630 CGF.createBasicBlock("finally.call_exit");
2631 llvm::BasicBlock *FinallyNoCallExit =
2632 CGF.createBasicBlock("finally.no_call_exit");
2633 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2634 FinallyCallExit, FinallyNoCallExit);
2635
2636 CGF.EmitBlock(FinallyCallExit);
2637 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2638 ->setDoesNotThrow();
2639
2640 CGF.EmitBlock(FinallyNoCallExit);
2641
2642 if (isa<ObjCAtTryStmt>(S)) {
2643 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002644 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2645 // Save the current cleanup destination in case there's
2646 // control flow inside the finally statement.
2647 llvm::Value *CurCleanupDest =
2648 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2649
John McCallcc505292010-07-21 06:59:36 +00002650 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2651
John McCalld96a8e72010-08-11 00:16:14 +00002652 if (CGF.HaveInsertPoint()) {
2653 CGF.Builder.CreateStore(CurCleanupDest,
2654 CGF.getNormalCleanupDestSlot());
2655 } else {
2656 // Currently, the end of the cleanup must always exist.
2657 CGF.EnsureInsertPoint();
2658 }
2659 }
John McCallcc505292010-07-21 06:59:36 +00002660 } else {
2661 // Emit objc_sync_exit(expr); as finally's sole statement for
2662 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002663 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002664 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2665 ->setDoesNotThrow();
2666 }
2667 }
2668 };
John McCall87bb5822010-07-31 23:20:56 +00002669
2670 class FragileHazards {
2671 CodeGenFunction &CGF;
2672 llvm::SmallVector<llvm::Value*, 20> Locals;
2673 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2674
2675 llvm::InlineAsm *ReadHazard;
2676 llvm::InlineAsm *WriteHazard;
2677
2678 llvm::FunctionType *GetAsmFnType();
2679
2680 void collectLocals();
2681 void emitReadHazard(CGBuilderTy &Builder);
2682
2683 public:
2684 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002685
John McCall87bb5822010-07-31 23:20:56 +00002686 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002687 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002688 };
2689}
2690
2691/// Create the fragile-ABI read and write hazards based on the current
2692/// state of the function, which is presumed to be immediately prior
2693/// to a @try block. These hazards are used to maintain correct
2694/// semantics in the face of optimization and the fragile ABI's
2695/// cavalier use of setjmp/longjmp.
2696FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2697 collectLocals();
2698
2699 if (Locals.empty()) return;
2700
2701 // Collect all the blocks in the function.
2702 for (llvm::Function::iterator
2703 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2704 BlocksBeforeTry.insert(&*I);
2705
2706 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2707
2708 // Create a read hazard for the allocas. This inhibits dead-store
2709 // optimizations and forces the values to memory. This hazard is
2710 // inserted before any 'throwing' calls in the protected scope to
2711 // reflect the possibility that the variables might be read from the
2712 // catch block if the call throws.
2713 {
2714 std::string Constraint;
2715 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2716 if (I) Constraint += ',';
2717 Constraint += "*m";
2718 }
2719
2720 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2721 }
2722
2723 // Create a write hazard for the allocas. This inhibits folding
2724 // loads across the hazard. This hazard is inserted at the
2725 // beginning of the catch path to reflect the possibility that the
2726 // variables might have been written within the protected scope.
2727 {
2728 std::string Constraint;
2729 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2730 if (I) Constraint += ',';
2731 Constraint += "=*m";
2732 }
2733
2734 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2735 }
2736}
2737
2738/// Emit a write hazard at the current location.
2739void FragileHazards::emitWriteHazard() {
2740 if (Locals.empty()) return;
2741
2742 CGF.Builder.CreateCall(WriteHazard, Locals.begin(), Locals.end())
2743 ->setDoesNotThrow();
2744}
2745
John McCall87bb5822010-07-31 23:20:56 +00002746void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2747 assert(!Locals.empty());
2748 Builder.CreateCall(ReadHazard, Locals.begin(), Locals.end())
2749 ->setDoesNotThrow();
2750}
2751
2752/// Emit read hazards in all the protected blocks, i.e. all the blocks
2753/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002754void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002755 if (Locals.empty()) return;
2756
2757 CGBuilderTy Builder(CGF.getLLVMContext());
2758
2759 // Iterate through all blocks, skipping those prior to the try.
2760 for (llvm::Function::iterator
2761 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2762 llvm::BasicBlock &BB = *FI;
2763 if (BlocksBeforeTry.count(&BB)) continue;
2764
2765 // Walk through all the calls in the block.
2766 for (llvm::BasicBlock::iterator
2767 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2768 llvm::Instruction &I = *BI;
2769
2770 // Ignore instructions that aren't non-intrinsic calls.
2771 // These are the only calls that can possibly call longjmp.
2772 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2773 if (isa<llvm::IntrinsicInst>(I))
2774 continue;
2775
2776 // Ignore call sites marked nounwind. This may be questionable,
2777 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2778 llvm::CallSite CS(&I);
2779 if (CS.doesNotThrow()) continue;
2780
John McCall0b251722010-08-04 05:59:32 +00002781 // Insert a read hazard before the call. This will ensure that
2782 // any writes to the locals are performed before making the
2783 // call. If the call throws, then this is sufficient to
2784 // guarantee correctness as long as it doesn't also write to any
2785 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002786 Builder.SetInsertPoint(&BB, BI);
2787 emitReadHazard(Builder);
2788 }
2789 }
2790}
2791
2792static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2793 if (V) S.insert(V);
2794}
2795
2796void FragileHazards::collectLocals() {
2797 // Compute a set of allocas to ignore.
2798 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2799 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2800 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
2801 addIfPresent(AllocasToIgnore, CGF.EHCleanupDest);
2802
2803 // Collect all the allocas currently in the function. This is
2804 // probably way too aggressive.
2805 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2806 for (llvm::BasicBlock::iterator
2807 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2808 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2809 Locals.push_back(&*I);
2810}
2811
2812llvm::FunctionType *FragileHazards::GetAsmFnType() {
2813 std::vector<const llvm::Type *> Tys(Locals.size());
2814 for (unsigned I = 0, E = Locals.size(); I != E; ++I)
2815 Tys[I] = Locals[I]->getType();
2816 return llvm::FunctionType::get(CGF.Builder.getVoidTy(), Tys, false);
John McCallcc505292010-07-21 06:59:36 +00002817}
2818
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002819/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002820
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002821 Objective-C setjmp-longjmp (sjlj) Exception Handling
2822 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002823
John McCallf1549f62010-07-06 01:34:17 +00002824 A catch buffer is a setjmp buffer plus:
2825 - a pointer to the exception that was caught
2826 - a pointer to the previous exception data buffer
2827 - two pointers of reserved storage
2828 Therefore catch buffers form a stack, with a pointer to the top
2829 of the stack kept in thread-local storage.
2830
2831 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2832 objc_exception_try_exit pops the given catch buffer, which is
2833 required to be the top of the EH stack.
2834 objc_exception_throw pops the top of the EH stack, writes the
2835 thrown exception into the appropriate field, and longjmps
2836 to the setjmp buffer. It crashes the process (with a printf
2837 and an abort()) if there are no catch buffers on the stack.
2838 objc_exception_extract just reads the exception pointer out of the
2839 catch buffer.
2840
2841 There's no reason an implementation couldn't use a light-weight
2842 setjmp here --- something like __builtin_setjmp, but API-compatible
2843 with the heavyweight setjmp. This will be more important if we ever
2844 want to implement correct ObjC/C++ exception interactions for the
2845 fragile ABI.
2846
2847 Note that for this use of setjmp/longjmp to be correct, we may need
2848 to mark some local variables volatile: if a non-volatile local
2849 variable is modified between the setjmp and the longjmp, it has
2850 indeterminate value. For the purposes of LLVM IR, it may be
2851 sufficient to make loads and stores within the @try (to variables
2852 declared outside the @try) volatile. This is necessary for
2853 optimized correctness, but is not currently being done; this is
2854 being tracked as rdar://problem/8160285
2855
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002856 The basic framework for a @try-catch-finally is as follows:
2857 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002858 objc_exception_data d;
2859 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002860 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002861
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002862 objc_exception_try_enter(&d);
2863 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002864 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002865 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002866 // exception path
2867 id _caught = objc_exception_extract(&d);
2868
2869 // enter new try scope for handlers
2870 if (!setjmp(d.jmp_buf)) {
2871 ... match exception and execute catch blocks ...
2872
2873 // fell off end, rethrow.
2874 _rethrow = _caught;
2875 ... jump-through-finally to finally_rethrow ...
2876 } else {
2877 // exception in catch block
2878 _rethrow = objc_exception_extract(&d);
2879 _call_try_exit = false;
2880 ... jump-through-finally to finally_rethrow ...
2881 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002882 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002883 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002884
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002885 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002886 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002887 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002888
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002889 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002890 ... dispatch to finally destination ...
2891
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002892 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002893 objc_exception_throw(_rethrow);
2894
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002895 finally_end:
2896 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002897
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002898 This framework differs slightly from the one gcc uses, in that gcc
2899 uses _rethrow to determine if objc_exception_try_exit should be called
2900 and if the object should be rethrown. This breaks in the face of
2901 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002902
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002903 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002904
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002905 - If there are no catch blocks, then we avoid emitting the second
2906 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002907
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002908 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2909 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002910
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002911 - FIXME: If there is no @finally block we can do a few more
2912 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002913
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002914 Rethrows and Jumps-Through-Finally
2915 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002916
John McCallf1549f62010-07-06 01:34:17 +00002917 '@throw;' is supported by pushing the currently-caught exception
2918 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002919
John McCallf1549f62010-07-06 01:34:17 +00002920 Branches through the @finally block are handled with an ordinary
2921 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2922 exceptions are not compatible with C++ exceptions, and this is
2923 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002924
John McCallf1549f62010-07-06 01:34:17 +00002925 @synchronized(expr) { stmt; } is emitted as if it were:
2926 id synch_value = expr;
2927 objc_sync_enter(synch_value);
2928 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002929*/
2930
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002931void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2932 const Stmt &S) {
2933 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002934
2935 // A destination for the fall-through edges of the catch handlers to
2936 // jump to.
2937 CodeGenFunction::JumpDest FinallyEnd =
2938 CGF.getJumpDestInCurrentScope("finally.end");
2939
2940 // A destination for the rethrow edge of the catch handlers to jump
2941 // to.
2942 CodeGenFunction::JumpDest FinallyRethrow =
2943 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002944
Daniel Dunbar1c566672009-02-24 01:43:46 +00002945 // For @synchronized, call objc_sync_enter(sync.expr). The
2946 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002947 // @synchronized. We can't avoid a temp here because we need the
2948 // value to be preserved. If the backend ever does liveness
2949 // correctly after setjmp, this will be unnecessary.
2950 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002951 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002952 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002953 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2954 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002955 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2956 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002957
2958 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2959 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002960 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002961
John McCall0b251722010-08-04 05:59:32 +00002962 // Allocate memory for the setjmp buffer. This needs to be kept
2963 // live throughout the try and catch blocks.
2964 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2965 "exceptiondata.ptr");
2966
John McCall87bb5822010-07-31 23:20:56 +00002967 // Create the fragile hazards. Note that this will not capture any
2968 // of the allocas required for exception processing, but will
2969 // capture the current basic block (which extends all the way to the
2970 // setjmp call) as "before the @try".
2971 FragileHazards Hazards(CGF);
2972
John McCallf1549f62010-07-06 01:34:17 +00002973 // Create a flag indicating whether the cleanup needs to call
2974 // objc_exception_try_exit. This is true except when
2975 // - no catches match and we're branching through the cleanup
2976 // just to rethrow the exception, or
2977 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00002978 // The setjmp-safety rule here is that we should always store to this
2979 // variable in a place that dominates the branch through the cleanup
2980 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00002981 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002982 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002983
John McCall9e2213d2010-10-04 23:42:51 +00002984 // A slot containing the exception to rethrow. Only needed when we
2985 // have both a @catch and a @finally.
2986 llvm::Value *PropagatingExnVar = 0;
2987
John McCallf1549f62010-07-06 01:34:17 +00002988 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002989 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00002990 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00002991 CallTryExitVar,
2992 ExceptionData,
2993 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002994
2995 // Enter a try block:
2996 // - Call objc_exception_try_enter to push ExceptionData on top of
2997 // the EH stack.
2998 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2999 ->setDoesNotThrow();
3000
3001 // - Call setjmp on the exception data buffer.
3002 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3003 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3004 llvm::Value *SetJmpBuffer =
3005 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
3006 llvm::CallInst *SetJmpResult =
3007 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3008 SetJmpResult->setDoesNotThrow();
3009
3010 // If setjmp returned 0, enter the protected block; otherwise,
3011 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003012 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3013 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003014 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003015 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3016 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003017
John McCallf1549f62010-07-06 01:34:17 +00003018 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003019 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003020 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003021 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003022 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003023
3024 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003025
John McCallf1549f62010-07-06 01:34:17 +00003026 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003027 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003028
John McCall87bb5822010-07-31 23:20:56 +00003029 // Don't optimize loads of the in-scope locals across this point.
3030 Hazards.emitWriteHazard();
3031
John McCallf1549f62010-07-06 01:34:17 +00003032 // For a @synchronized (or a @try with no catches), just branch
3033 // through the cleanup to the rethrow block.
3034 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3035 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003036 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003037 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003038
3039 // Otherwise, we have to match against the caught exceptions.
3040 } else {
John McCall0b251722010-08-04 05:59:32 +00003041 // Retrieve the exception object. We may emit multiple blocks but
3042 // nothing can cross this so the value is already in SSA form.
3043 llvm::CallInst *Caught =
3044 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3045 ExceptionData, "caught");
3046 Caught->setDoesNotThrow();
3047
John McCallf1549f62010-07-06 01:34:17 +00003048 // Push the exception to rethrow onto the EH value stack for the
3049 // benefit of any @throws in the handlers.
3050 CGF.ObjCEHValueStack.push_back(Caught);
3051
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003052 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003053
John McCall0b251722010-08-04 05:59:32 +00003054 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003055
John McCall0b251722010-08-04 05:59:32 +00003056 llvm::BasicBlock *CatchBlock = 0;
3057 llvm::BasicBlock *CatchHandler = 0;
3058 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003059 // Save the currently-propagating exception before
3060 // objc_exception_try_enter clears the exception slot.
3061 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3062 "propagating_exception");
3063 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3064
John McCall0b251722010-08-04 05:59:32 +00003065 // Enter a new exception try block (in case a @catch block
3066 // throws an exception).
3067 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3068 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003069
John McCall0b251722010-08-04 05:59:32 +00003070 llvm::CallInst *SetJmpResult =
3071 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3072 "setjmp.result");
3073 SetJmpResult->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003074
John McCall0b251722010-08-04 05:59:32 +00003075 llvm::Value *Threw =
3076 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3077
3078 CatchBlock = CGF.createBasicBlock("catch");
3079 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3080 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3081
3082 CGF.EmitBlock(CatchBlock);
3083 }
3084
3085 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003086
Daniel Dunbar55e40722008-09-27 07:03:52 +00003087 // Handle catch list. As a special case we check if everything is
3088 // matched and avoid generating code for falling off the end if
3089 // so.
3090 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003091 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3092 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003093
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003094 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003095 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003096
Anders Carlsson80f25672008-09-09 17:59:25 +00003097 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003098 if (!CatchParam) {
3099 AllMatched = true;
3100 } else {
John McCall183700f2009-09-21 23:43:11 +00003101 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003102
John McCallf1549f62010-07-06 01:34:17 +00003103 // catch(id e) always matches under this ABI, since only
3104 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003105 // FIXME: For the time being we also match id<X>; this should
3106 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003107 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003108 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003109 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003110
John McCallf1549f62010-07-06 01:34:17 +00003111 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003112 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003113 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3114
Anders Carlssondde0a942008-09-11 09:15:33 +00003115 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003116 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003117 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003118
3119 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003120 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003121 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003122
Anders Carlssondde0a942008-09-11 09:15:33 +00003123 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003124
3125 // The scope of the catch variable ends right here.
3126 CatchVarCleanups.ForceCleanup();
3127
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003128 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003129 break;
3130 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003131
Steve Naroff14108da2009-07-10 23:34:53 +00003132 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003133 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003134
3135 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003136 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3137 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003138
3139 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003140 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003141
John McCallf1549f62010-07-06 01:34:17 +00003142 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003143 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3144 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003145 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003146
John McCallf1549f62010-07-06 01:34:17 +00003147 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3148 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003149
3150 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003151 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003152
Anders Carlsson80f25672008-09-09 17:59:25 +00003153 // Emit the @catch block.
3154 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003155
3156 // Collect any cleanups for the catch variable. The scope lasts until
3157 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003158 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003159
John McCallb6bbcc92010-10-15 04:57:14 +00003160 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003161 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003162
John McCallf1549f62010-07-06 01:34:17 +00003163 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003164 llvm::Value *Tmp =
3165 CGF.Builder.CreateBitCast(Caught,
3166 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003167 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00003168 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003169
Anders Carlssondde0a942008-09-11 09:15:33 +00003170 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003171
3172 // We're done with the catch variable.
3173 CatchVarCleanups.ForceCleanup();
3174
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003175 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003176
Anders Carlsson80f25672008-09-09 17:59:25 +00003177 CGF.EmitBlock(NextCatchBlock);
3178 }
3179
John McCallf1549f62010-07-06 01:34:17 +00003180 CGF.ObjCEHValueStack.pop_back();
3181
John McCall0b251722010-08-04 05:59:32 +00003182 // If nothing wanted anything to do with the caught exception,
3183 // kill the extract call.
3184 if (Caught->use_empty())
3185 Caught->eraseFromParent();
3186
3187 if (!AllMatched)
3188 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3189
3190 if (HasFinally) {
3191 // Emit the exception handler for the @catch blocks.
3192 CGF.EmitBlock(CatchHandler);
3193
3194 // In theory we might now need a write hazard, but actually it's
3195 // unnecessary because there's no local-accessing code between
3196 // the try's write hazard and here.
3197 //Hazards.emitWriteHazard();
3198
John McCall9e2213d2010-10-04 23:42:51 +00003199 // Extract the new exception and save it to the
3200 // propagating-exception slot.
3201 assert(PropagatingExnVar);
3202 llvm::CallInst *NewCaught =
3203 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3204 ExceptionData, "caught");
3205 NewCaught->setDoesNotThrow();
3206 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3207
John McCall0b251722010-08-04 05:59:32 +00003208 // Don't pop the catch handler; the throw already did.
3209 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003210 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003211 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003212 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003213
John McCall87bb5822010-07-31 23:20:56 +00003214 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003215 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003216
John McCallf1549f62010-07-06 01:34:17 +00003217 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003218 CGF.Builder.restoreIP(TryFallthroughIP);
3219 if (CGF.HaveInsertPoint())
3220 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003221 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003222 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003223
John McCallf1549f62010-07-06 01:34:17 +00003224 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003225 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003226 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003227 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003228 // If we have a propagating-exception variable, check it.
3229 llvm::Value *PropagatingExn;
3230 if (PropagatingExnVar) {
3231 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003232
John McCall9e2213d2010-10-04 23:42:51 +00003233 // Otherwise, just look in the buffer for the exception to throw.
3234 } else {
3235 llvm::CallInst *Caught =
3236 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3237 ExceptionData);
3238 Caught->setDoesNotThrow();
3239 PropagatingExn = Caught;
3240 }
3241
3242 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003243 ->setDoesNotThrow();
3244 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003245 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003246
John McCall87bb5822010-07-31 23:20:56 +00003247 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003248}
3249
3250void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003251 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003252 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003253
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003254 if (const Expr *ThrowExpr = S.getThrowExpr()) {
3255 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003256 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003257 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
3258 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003259 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003260 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003261 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003262 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003263
John McCallf1549f62010-07-06 01:34:17 +00003264 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3265 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003266 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003267
3268 // Clear the insertion point to indicate we are in unreachable code.
3269 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003270}
3271
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003272/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003273/// object: objc_read_weak (id *src)
3274///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003275llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003276 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00003277 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003278 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3279 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3280 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003281 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003282 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003283 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003284 return read_weak;
3285}
3286
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003287/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3288/// objc_assign_weak (id src, id *dst)
3289///
3290void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003291 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003292 const llvm::Type * SrcTy = src->getType();
3293 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003294 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003295 assert(Size <= 8 && "does not support size > 8");
3296 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003297 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003298 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3299 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003300 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3301 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003302 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003303 src, dst, "weakassign");
3304 return;
3305}
3306
Fariborz Jahanian58626502008-11-19 00:59:10 +00003307/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3308/// objc_assign_global (id src, id *dst)
3309///
3310void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003311 llvm::Value *src, llvm::Value *dst,
3312 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003313 const llvm::Type * SrcTy = src->getType();
3314 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003315 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003316 assert(Size <= 8 && "does not support size > 8");
3317 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003318 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003319 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3320 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003321 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3322 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003323 if (!threadlocal)
3324 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3325 src, dst, "globalassign");
3326 else
3327 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3328 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003329 return;
3330}
3331
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003332/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003333/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003334///
3335void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003336 llvm::Value *src, llvm::Value *dst,
3337 llvm::Value *ivarOffset) {
3338 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003339 const llvm::Type * SrcTy = src->getType();
3340 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003341 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003342 assert(Size <= 8 && "does not support size > 8");
3343 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003344 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003345 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3346 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003347 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3348 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003349 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3350 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003351 return;
3352}
3353
Fariborz Jahanian58626502008-11-19 00:59:10 +00003354/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3355/// objc_assign_strongCast (id src, id *dst)
3356///
3357void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003358 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003359 const llvm::Type * SrcTy = src->getType();
3360 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003361 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003362 assert(Size <= 8 && "does not support size > 8");
3363 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003364 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003365 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3366 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003367 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3368 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003369 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003370 src, dst, "weakassign");
3371 return;
3372}
3373
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003374void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003375 llvm::Value *DestPtr,
3376 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003377 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003378 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3379 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003380 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003381 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003382 return;
3383}
3384
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003385/// EmitObjCValueForIvar - Code Gen for ivar reference.
3386///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003387LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3388 QualType ObjectTy,
3389 llvm::Value *BaseValue,
3390 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003391 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003392 const ObjCInterfaceDecl *ID =
3393 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003394 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3395 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003396}
3397
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003398llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003399 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003400 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003401 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003402 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003403 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3404 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003405}
3406
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003407/* *** Private Interface *** */
3408
3409/// EmitImageInfo - Emit the image info marker used to encode some module
3410/// level information.
3411///
3412/// See: <rdr://4810609&4810587&4810587>
3413/// struct IMAGE_INFO {
3414/// unsigned version;
3415/// unsigned flags;
3416/// };
3417enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003418 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003419 eImageInfo_GarbageCollected = (1 << 1),
3420 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003421 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3422
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003423 // A flag indicating that the module has no instances of a @synthesize of a
3424 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003425 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003426};
3427
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003428void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003429 unsigned version = 0; // Version is unused?
3430 unsigned flags = 0;
3431
3432 // FIXME: Fix and continue?
3433 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3434 flags |= eImageInfo_GarbageCollected;
3435 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3436 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003437
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003438 // We never allow @synthesize of a superclass property.
3439 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003440
Chris Lattner77b89b82010-06-27 07:15:29 +00003441 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3442
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003443 // Emitted as int[2];
3444 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003445 llvm::ConstantInt::get(Int32Ty, version),
3446 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003447 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003448 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003449
3450 const char *Section;
3451 if (ObjCABI == 1)
3452 Section = "__OBJC, __image_info,regular";
3453 else
3454 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003455 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003456 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003457 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003458 Section,
3459 0,
3460 true);
3461 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003462}
3463
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003464
3465// struct objc_module {
3466// unsigned long version;
3467// unsigned long size;
3468// const char *name;
3469// Symtab symtab;
3470// };
3471
3472// FIXME: Get from somewhere
3473static const int ModuleVersion = 7;
3474
3475void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003476 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003477
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003478 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003479 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3480 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003481 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003482 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003483 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003484 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003485 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003486 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003487 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003488}
3489
3490llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003491 unsigned NumClasses = DefinedClasses.size();
3492 unsigned NumCategories = DefinedCategories.size();
3493
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003494 // Return null if no symbols were defined.
3495 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003496 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003497
3498 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003499 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003500 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003501 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3502 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003503
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003504 // The runtime expects exactly the list of defined classes followed
3505 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003506 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003507 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003508 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003509 ObjCTypes.Int8PtrTy);
3510 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003511 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003512 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003513 ObjCTypes.Int8PtrTy);
3514
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003515 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003516 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003517 NumClasses + NumCategories),
3518 Symbols);
3519
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003520 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003521
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003522 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003523 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3524 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003525 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003526 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003527}
3528
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003529llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003530 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003531 LazySymbols.insert(ID->getIdentifier());
3532
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003533 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003534
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003535 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003536 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003537 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003538 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003539 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003540 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3541 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003542 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003543 }
3544
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003545 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003546}
3547
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003548llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3549 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003550 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003551
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003552 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003553 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003554 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003555 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003556 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003557 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3558 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003559 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003560 }
3561
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003562 if (lvalue)
3563 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003564 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003565}
3566
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003567llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003568 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003569
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003570 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003571 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003572 llvm::ConstantArray::get(VMContext,
3573 Ident->getNameStart()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003574 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003575 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003576
Owen Andersona1cf15f2009-07-14 23:10:40 +00003577 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003578}
3579
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003580llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3581 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3582 I = MethodDefinitions.find(MD);
3583 if (I != MethodDefinitions.end())
3584 return I->second;
3585
3586 if (MD->hasBody() && MD->getPCHLevel() > 0) {
3587 // MD isn't emitted yet because it comes from PCH.
3588 CGM.EmitTopLevelDecl(const_cast<ObjCMethodDecl*>(MD));
3589 assert(MethodDefinitions[MD] && "EmitTopLevelDecl didn't emit the method!");
3590 return MethodDefinitions[MD];
3591 }
3592
3593 return NULL;
3594}
3595
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003596/// GetIvarLayoutName - Returns a unique constant for the given
3597/// ivar layout bitmap.
3598llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003599 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003600 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003601}
3602
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003603void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003604 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003605 bool ForStrongLayout,
3606 bool &HasUnion) {
3607 const RecordDecl *RD = RT->getDecl();
3608 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003609 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003610 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003611 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003612 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003613
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003614 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3615 ForStrongLayout, HasUnion);
3616}
3617
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003618void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003619 const llvm::StructLayout *Layout,
3620 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003621 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003622 unsigned int BytePos, bool ForStrongLayout,
3623 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003624 bool IsUnion = (RD && RD->isUnion());
3625 uint64_t MaxUnionIvarSize = 0;
3626 uint64_t MaxSkippedUnionIvarSize = 0;
3627 FieldDecl *MaxField = 0;
3628 FieldDecl *MaxSkippedField = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003629 FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003630 uint64_t MaxFieldOffset = 0;
3631 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003632 uint64_t LastBitfieldOrUnnamedOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003633
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003634 if (RecFields.empty())
3635 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003636 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3637 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3638
Chris Lattnerf1690852009-03-31 08:48:01 +00003639 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003640 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003641 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003642 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003643 // Note that 'i' here is actually the field index inside RD of Field,
3644 // although this dependency is hidden.
3645 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3646 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003647 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003648 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003649
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003650 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003651 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003652 LastFieldBitfieldOrUnnamed = Field;
3653 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003654 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003655 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003656
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003657 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003658 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003659 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003660 if (FQT->isUnionType())
3661 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003662
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003663 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003664 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003665 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003666 continue;
3667 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003668
Chris Lattnerf1690852009-03-31 08:48:01 +00003669 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003670 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003671 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003672 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003673 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003674 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003675 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3676 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003677 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003678 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003679 FQT = CArray->getElementType();
3680 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003681
3682 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003683 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003684 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003685 int OldIndex = IvarsInfo.size() - 1;
3686 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003687
Ted Kremenek6217b802009-07-29 21:53:49 +00003688 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003689 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003690 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003691
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003692 // Replicate layout information for each array element. Note that
3693 // one element is already done.
3694 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003695 for (int FirstIndex = IvarsInfo.size() - 1,
3696 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003697 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003698 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3699 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3700 IvarsInfo[i].ivar_size));
3701 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3702 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3703 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003704 }
3705 continue;
3706 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003707 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003708 // At this point, we are done with Record/Union and array there of.
3709 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003710 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003711
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003712 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003713 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3714 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003715 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003716 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003717 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003718 MaxUnionIvarSize = UnionIvarSize;
3719 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003720 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003721 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003722 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003723 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003724 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003725 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003726 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003727 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3728 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003729 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003730 // FIXME: Why the asymmetry? We divide by word size in bits on other
3731 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003732 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003733 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003734 MaxSkippedUnionIvarSize = UnionIvarSize;
3735 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003736 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003737 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003738 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003739 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003740 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003741 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003742 }
3743 }
3744 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003745
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003746 if (LastFieldBitfieldOrUnnamed) {
3747 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3748 // Last field was a bitfield. Must update skip info.
3749 Expr *BitWidth = LastFieldBitfieldOrUnnamed->getBitWidth();
3750 uint64_t BitFieldSize =
3751 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
3752 GC_IVAR skivar;
3753 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3754 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3755 + ((BitFieldSize % ByteSizeInBits) != 0);
3756 SkipIvars.push_back(skivar);
3757 } else {
3758 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3759 // Last field was unnamed. Must update skip info.
3760 unsigned FieldSize
3761 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3762 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3763 FieldSize / ByteSizeInBits));
3764 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003765 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003766
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003767 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003768 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003769 MaxUnionIvarSize));
3770 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003771 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003772 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003773}
3774
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003775/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3776/// the computations and returning the layout bitmap (for ivar or blocks) in
3777/// the given argument BitMap string container. Routine reads
3778/// two containers, IvarsInfo and SkipIvars which are assumed to be
3779/// filled already by the caller.
3780llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003781 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003782 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003783
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003784 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003785 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003786 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003787 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003788 if (IvarsInfo[0].ivar_bytepos == 0) {
3789 WordsToSkip = 0;
3790 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003791 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003792 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3793 WordsToScan = IvarsInfo[0].ivar_size;
3794 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003795 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003796 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003797 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003798 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003799 // consecutive 'scanned' object pointers.
3800 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003801 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003802 // Skip over 'gc'able object pointer which lay over each other.
3803 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3804 continue;
3805 // Must skip over 1 or more words. We save current skip/scan values
3806 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003807 SKIP_SCAN SkScan;
3808 SkScan.skip = WordsToSkip;
3809 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003810 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003811
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003812 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003813 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3814 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003815 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003816 WordsToSkip = 0;
3817 WordsToScan = IvarsInfo[i].ivar_size;
3818 }
3819 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003820 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003821 SKIP_SCAN SkScan;
3822 SkScan.skip = WordsToSkip;
3823 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003824 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003825 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003826
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003827 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003828 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003829 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003830 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003831 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003832 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003833 IvarsInfo[LastIndex].ivar_bytepos +
3834 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003835 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003836 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003837 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003838 SKIP_SCAN SkScan;
3839 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3840 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003841 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003842 }
3843 }
3844 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3845 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003846 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003847 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003848 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3849 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3850 // 0xM0 followed by 0x0N detected.
3851 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3852 for (int j = i+1; j < SkipScan; j++)
3853 SkipScanIvars[j] = SkipScanIvars[j+1];
3854 --SkipScan;
3855 }
3856 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003857
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003858 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003859 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003860 unsigned char byte;
3861 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3862 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3863 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3864 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003865
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003866 // first skip big.
3867 for (unsigned int ix = 0; ix < skip_big; ix++)
3868 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003869
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003870 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003871 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003872 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003873 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003874 byte |= 0xf;
3875 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003876 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003877 byte |= scan_small;
3878 scan_small = 0;
3879 }
3880 BitMap += byte;
3881 }
3882 // next scan big
3883 for (unsigned int ix = 0; ix < scan_big; ix++)
3884 BitMap += (unsigned char)(0x0f);
3885 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003886 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003887 byte = scan_small;
3888 BitMap += byte;
3889 }
3890 }
3891 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003892 unsigned char zero = 0;
3893 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003894
3895 llvm::GlobalVariable * Entry =
3896 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3897 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
3898 "__TEXT,__cstring,cstring_literals",
3899 1, true);
3900 return getConstantGEP(VMContext, Entry, 0, 0);
3901}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003902
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003903/// BuildIvarLayout - Builds ivar layout bitmap for the class
3904/// implementation for the __strong or __weak case.
3905/// The layout map displays which words in ivar list must be skipped
3906/// and which must be scanned by GC (see below). String is built of bytes.
3907/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3908/// of words to skip and right nibble is count of words to scan. So, each
3909/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3910/// represented by a 0x00 byte which also ends the string.
3911/// 1. when ForStrongLayout is true, following ivars are scanned:
3912/// - id, Class
3913/// - object *
3914/// - __strong anything
3915///
3916/// 2. When ForStrongLayout is false, following ivars are scanned:
3917/// - __weak anything
3918///
3919llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3920 const ObjCImplementationDecl *OMD,
3921 bool ForStrongLayout) {
3922 bool hasUnion = false;
3923
3924 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
3925 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
3926 return llvm::Constant::getNullValue(PtrTy);
3927
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003928 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003929 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003930 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003931
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003932 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003933 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3934 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
3935
3936 if (RecFields.empty())
3937 return llvm::Constant::getNullValue(PtrTy);
3938
3939 SkipIvars.clear();
3940 IvarsInfo.clear();
3941
3942 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
3943 if (IvarsInfo.empty())
3944 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003945 // Sort on byte position in case we encounterred a union nested in
3946 // the ivar list.
3947 if (hasUnion && !IvarsInfo.empty())
3948 std::sort(IvarsInfo.begin(), IvarsInfo.end());
3949 if (hasUnion && !SkipIvars.empty())
3950 std::sort(SkipIvars.begin(), SkipIvars.end());
3951
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003952 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003953 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003954
3955 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003956 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003957 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00003958 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003959 const unsigned char *s = (unsigned char*)BitMap.c_str();
3960 for (unsigned i = 0; i < BitMap.size(); i++)
3961 if (!(s[i] & 0xf0))
3962 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3963 else
3964 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3965 printf("\n");
3966 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003967 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003968}
3969
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003970llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003971 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3972
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003973 // FIXME: Avoid std::string copying.
3974 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003975 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003976 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003977 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003978 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003979
Owen Andersona1cf15f2009-07-14 23:10:40 +00003980 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003981}
3982
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003983// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003984llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003985 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3986}
3987
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003988llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003989 std::string TypeStr;
3990 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3991
3992 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003993
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003994 if (!Entry)
3995 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003996 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00003997 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003998 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003999
Owen Andersona1cf15f2009-07-14 23:10:40 +00004000 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004001}
4002
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004003llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004004 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00004005 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
4006 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00004007
4008 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4009
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004010 if (!Entry)
4011 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004012 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar6633e3c2010-07-29 22:57:21 +00004013 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004014 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004015
Owen Andersona1cf15f2009-07-14 23:10:40 +00004016 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004017}
4018
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004019// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004020llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004021 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004022
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004023 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004024 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004025 llvm::ConstantArray::get(VMContext,
4026 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004027 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004028 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004029
Owen Andersona1cf15f2009-07-14 23:10:40 +00004030 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004031}
4032
4033// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004034// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004035llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004036CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4037 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004038 std::string TypeStr;
4039 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004040 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4041}
4042
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004043void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004044 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004045 llvm::SmallVectorImpl<char> &Name) {
4046 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004047 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004048 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4049 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004050 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004051 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004052 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004053 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004054}
4055
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004056void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004057 EmitModuleInfo();
4058
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004059 // Emit the dummy bodies for any protocols which were referenced but
4060 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004061 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004062 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4063 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004064 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004065
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004066 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004067 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004068 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004069 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004070 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004071 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004072 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004073 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004074 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004075 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004076 }
4077
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004078 // Add assembler directives to add lazy undefined symbol references
4079 // for classes which are referenced but not defined. This is
4080 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004081 //
4082 // FIXME: It would be nice if we had an LLVM construct for this.
4083 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4084 llvm::SmallString<256> Asm;
4085 Asm += CGM.getModule().getModuleInlineAsm();
4086 if (!Asm.empty() && Asm.back() != '\n')
4087 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004088
Daniel Dunbar33063492009-09-07 00:20:42 +00004089 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004090 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4091 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004092 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4093 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004094 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004095 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004096 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004097 }
4098
4099 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4100 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4101 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4102 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004103
Daniel Dunbar33063492009-09-07 00:20:42 +00004104 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004105 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004106}
4107
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004108CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004109 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004110 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004111 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004112 ObjCABI = 2;
4113}
4114
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004115/* *** */
4116
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004117ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004118 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004119 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4120 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004121
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004122 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004123 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004124 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004125 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004126 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004127
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004128 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004129 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004130 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004131
Mike Stumpf5408fe2009-05-16 07:57:57 +00004132 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4133 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004134 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004135 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004136
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004137 // I'm not sure I like this. The implicit coordination is a bit
4138 // gross. We should solve this in a reasonable fashion because this
4139 // is a pretty common task (match some runtime data structure with
4140 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004141
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004142 // FIXME: This is leaked.
4143 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004144
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004145 // struct _objc_super {
4146 // id self;
4147 // Class cls;
4148 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004149 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004150 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004151 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004153 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004154 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004155 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004156 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004157 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004158
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004159 SuperCTy = Ctx.getTagDeclType(RD);
4160 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004161
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004162 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004163 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4164
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004165 // struct _prop_t {
4166 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004167 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004168 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004169 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004170 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004171 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004172
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004173 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004174 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004175 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004176 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004177 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004178 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004179 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004180 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004181 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004182 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004183 PropertyListTy);
4184 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004185 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004186
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004187 // struct _objc_method {
4188 // SEL _cmd;
4189 // char *method_type;
4190 // char *_imp;
4191 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004192 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004193 Int8PtrTy,
4194 Int8PtrTy,
4195 NULL);
4196 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004197
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004198 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004199 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004200 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004201 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004202}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004203
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004204ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004205 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004206 // struct _objc_method_description {
4207 // SEL name;
4208 // char *types;
4209 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004210 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004211 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004212 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004213 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004214 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004215 MethodDescriptionTy);
4216
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004217 // struct _objc_method_description_list {
4218 // int count;
4219 // struct _objc_method_description[1];
4220 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004221 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004222 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004223 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004224 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004225 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004226 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004227
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004228 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004229 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004230 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004231
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004232 // Protocol description structures
4233
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004234 // struct _objc_protocol_extension {
4235 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4236 // struct _objc_method_description_list *optional_instance_methods;
4237 // struct _objc_method_description_list *optional_class_methods;
4238 // struct _objc_property_list *instance_properties;
4239 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004240 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004241 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004242 MethodDescriptionListPtrTy,
4243 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004244 PropertyListPtrTy,
4245 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004246 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004247 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004248
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004249 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004250 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004251
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004252 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004253
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004254 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
4255 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004256
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004257 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00004258 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004259 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004260 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004261 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004262 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004263 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
4264
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004265 // struct _objc_protocol {
4266 // struct _objc_protocol_extension *isa;
4267 // char *protocol_name;
4268 // struct _objc_protocol **_objc_protocol_list;
4269 // struct _objc_method_description_list *instance_methods;
4270 // struct _objc_method_description_list *class_methods;
4271 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004272 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004273 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004274 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004275 MethodDescriptionListPtrTy,
4276 MethodDescriptionListPtrTy,
4277 NULL);
4278 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
4279
4280 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004281 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004282 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004283 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004284 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004285
4286 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004287 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004288 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004289
4290 // Class description structures
4291
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004292 // struct _objc_ivar {
4293 // char *ivar_name;
4294 // char *ivar_type;
4295 // int ivar_offset;
4296 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004297 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004298 Int8PtrTy,
4299 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004300 NULL);
4301 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4302
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004303 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004304 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004305 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004306 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004307
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004308 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004309 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004310 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004311 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004312
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004313 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004314 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004315 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004316 Int8PtrTy,
4317 PropertyListPtrTy,
4318 NULL);
4319 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004320 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004321
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004322 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004323
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004324 // struct _objc_class {
4325 // Class isa;
4326 // Class super_class;
4327 // char *name;
4328 // long version;
4329 // long info;
4330 // long instance_size;
4331 // struct _objc_ivar_list *ivars;
4332 // struct _objc_method_list *methods;
4333 // struct _objc_cache *cache;
4334 // struct _objc_protocol_list *protocols;
4335 // char *ivar_layout;
4336 // struct _objc_class_ext *ext;
4337 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004338 T = llvm::StructType::get(VMContext,
4339 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004340 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004341 Int8PtrTy,
4342 LongTy,
4343 LongTy,
4344 LongTy,
4345 IvarListPtrTy,
4346 MethodListPtrTy,
4347 CachePtrTy,
4348 ProtocolListPtrTy,
4349 Int8PtrTy,
4350 ClassExtensionPtrTy,
4351 NULL);
4352 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004353
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004354 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4355 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004356 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004357
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004358 // struct _objc_category {
4359 // char *category_name;
4360 // char *class_name;
4361 // struct _objc_method_list *instance_method;
4362 // struct _objc_method_list *class_method;
4363 // uint32_t size; // sizeof(struct _objc_category)
4364 // struct _objc_property_list *instance_properties;// category's @property
4365 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004366 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004367 Int8PtrTy,
4368 MethodListPtrTy,
4369 MethodListPtrTy,
4370 ProtocolListPtrTy,
4371 IntTy,
4372 PropertyListPtrTy,
4373 NULL);
4374 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4375
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004376 // Global metadata structures
4377
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004378 // struct _objc_symtab {
4379 // long sel_ref_cnt;
4380 // SEL *refs;
4381 // short cls_def_cnt;
4382 // short cat_def_cnt;
4383 // char *defs[cls_def_cnt + cat_def_cnt];
4384 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004385 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004386 SelectorPtrTy,
4387 ShortTy,
4388 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004389 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004390 NULL);
4391 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004392 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004393
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004394 // struct _objc_module {
4395 // long version;
4396 // long size; // sizeof(struct _objc_module)
4397 // char *name;
4398 // struct _objc_symtab* symtab;
4399 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004400 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004401 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004402 LongTy,
4403 Int8PtrTy,
4404 SymtabPtrTy,
4405 NULL);
4406 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004407
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004408
Mike Stumpf5408fe2009-05-16 07:57:57 +00004409 // FIXME: This is the size of the setjmp buffer and should be target
4410 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004411 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004412
Anders Carlsson124526b2008-09-09 10:10:21 +00004413 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004414 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004415 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004416
4417 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004418 llvm::StructType::get(VMContext,
4419 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4420 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004421 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004422 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004423 ExceptionDataTy);
4424
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004425}
4426
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004427ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004428 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004429 // struct _method_list_t {
4430 // uint32_t entsize; // sizeof(struct _objc_method)
4431 // uint32_t method_count;
4432 // struct _objc_method method_list[method_count];
4433 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004434 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004435 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004436 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004437 NULL);
4438 CGM.getModule().addTypeName("struct.__method_list_t",
4439 MethodListnfABITy);
4440 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004441 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004442
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004443 // struct _protocol_t {
4444 // id isa; // NULL
4445 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004446 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004447 // const struct method_list_t * const instance_methods;
4448 // const struct method_list_t * const class_methods;
4449 // const struct method_list_t *optionalInstanceMethods;
4450 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004451 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004452 // const uint32_t size; // sizeof(struct _protocol_t)
4453 // const uint32_t flags; // = 0
4454 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004455
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004456 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004457 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004458
Owen Anderson47a434f2009-08-05 23:18:46 +00004459 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004460 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004461 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004462 ProtocolListTyHolder),
4463 MethodListnfABIPtrTy,
4464 MethodListnfABIPtrTy,
4465 MethodListnfABIPtrTy,
4466 MethodListnfABIPtrTy,
4467 PropertyListPtrTy,
4468 IntTy,
4469 IntTy,
4470 NULL);
4471 CGM.getModule().addTypeName("struct._protocol_t",
4472 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004473
4474 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004475 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004476
Fariborz Jahanianda320092009-01-29 19:24:30 +00004477 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004478 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004479 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004480 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004481 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004482 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004483 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004484 NULL);
4485 CGM.getModule().addTypeName("struct._objc_protocol_list",
4486 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004487 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004488 ProtocolListnfABITy);
4489
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004490 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004491 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004492
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004493 // struct _ivar_t {
4494 // unsigned long int *offset; // pointer to ivar offset location
4495 // char *name;
4496 // char *type;
4497 // uint32_t alignment;
4498 // uint32_t size;
4499 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004500 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004501 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004502 Int8PtrTy,
4503 Int8PtrTy,
4504 IntTy,
4505 IntTy,
4506 NULL);
4507 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004508
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004509 // struct _ivar_list_t {
4510 // uint32 entsize; // sizeof(struct _ivar_t)
4511 // uint32 count;
4512 // struct _iver_t list[count];
4513 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004514 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004515 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004516 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004517 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004518 NULL);
4519 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004520
Owen Anderson96e0fc72009-07-29 22:16:19 +00004521 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004522
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004523 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004524 // uint32_t const flags;
4525 // uint32_t const instanceStart;
4526 // uint32_t const instanceSize;
4527 // uint32_t const reserved; // only when building for 64bit targets
4528 // const uint8_t * const ivarLayout;
4529 // const char *const name;
4530 // const struct _method_list_t * const baseMethods;
4531 // const struct _objc_protocol_list *const baseProtocols;
4532 // const struct _ivar_list_t *const ivars;
4533 // const uint8_t * const weakIvarLayout;
4534 // const struct _prop_list_t * const properties;
4535 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004536
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004537 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004538 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004539 IntTy,
4540 IntTy,
4541 Int8PtrTy,
4542 Int8PtrTy,
4543 MethodListnfABIPtrTy,
4544 ProtocolListnfABIPtrTy,
4545 IvarListnfABIPtrTy,
4546 Int8PtrTy,
4547 PropertyListPtrTy,
4548 NULL);
4549 CGM.getModule().addTypeName("struct._class_ro_t",
4550 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004551
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004552 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4553 std::vector<const llvm::Type*> Params;
4554 Params.push_back(ObjectPtrTy);
4555 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004556 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004557 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4558
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004559 // struct _class_t {
4560 // struct _class_t *isa;
4561 // struct _class_t * const superclass;
4562 // void *cache;
4563 // IMP *vtable;
4564 // struct class_ro_t *ro;
4565 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004566
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004567 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004568 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004569 llvm::StructType::get(VMContext,
4570 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004571 llvm::PointerType::getUnqual(ClassTyHolder),
4572 CachePtrTy,
4573 llvm::PointerType::getUnqual(ImpnfABITy),
4574 llvm::PointerType::getUnqual(ClassRonfABITy),
4575 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004576 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4577
4578 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004579 ClassnfABITy);
4580
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004581 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004582 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004583
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004584 // struct _category_t {
4585 // const char * const name;
4586 // struct _class_t *const cls;
4587 // const struct _method_list_t * const instance_methods;
4588 // const struct _method_list_t * const class_methods;
4589 // const struct _protocol_list_t * const protocols;
4590 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004591 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004592 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004593 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004594 MethodListnfABIPtrTy,
4595 MethodListnfABIPtrTy,
4596 ProtocolListnfABIPtrTy,
4597 PropertyListPtrTy,
4598 NULL);
4599 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004600
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004601 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004602 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4603 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004604
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004605 // MessageRefTy - LLVM for:
4606 // struct _message_ref_t {
4607 // IMP messenger;
4608 // SEL name;
4609 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004610
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004611 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004612 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004613 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004614 SourceLocation(),
4615 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004616 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004617 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004618 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004619 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004620 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004621
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004622 MessageRefCTy = Ctx.getTagDeclType(RD);
4623 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4624 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004625
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004626 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004627 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004628
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004629 // SuperMessageRefTy - LLVM for:
4630 // struct _super_message_ref_t {
4631 // SUPER_IMP messenger;
4632 // SEL name;
4633 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004634 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004635 SelectorPtrTy,
4636 NULL);
4637 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004638
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004639 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004640 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4641
Daniel Dunbare588b992009-03-01 04:46:24 +00004642
4643 // struct objc_typeinfo {
4644 // const void** vtable; // objc_ehtype_vtable + 2
4645 // const char* name; // c++ typeinfo string
4646 // Class cls;
4647 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004648 EHTypeTy = llvm::StructType::get(VMContext,
4649 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004650 Int8PtrTy,
4651 ClassnfABIPtrTy,
4652 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004653 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004654 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004655}
4656
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004657llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004658 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004659
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004660 return NULL;
4661}
4662
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004663void CGObjCNonFragileABIMac::AddModuleClassList(const
4664 std::vector<llvm::GlobalValue*>
4665 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004666 const char *SymbolName,
4667 const char *SectionName) {
4668 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004669
Daniel Dunbar463b8762009-05-15 21:48:48 +00004670 if (!NumClasses)
4671 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004672
Daniel Dunbar463b8762009-05-15 21:48:48 +00004673 std::vector<llvm::Constant*> Symbols(NumClasses);
4674 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004675 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004676 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004677 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004678 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004679 NumClasses),
4680 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004681
Daniel Dunbar463b8762009-05-15 21:48:48 +00004682 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004683 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004684 llvm::GlobalValue::InternalLinkage,
4685 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004686 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004687 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004688 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004689 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004690}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004691
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004692void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4693 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004694
Daniel Dunbar463b8762009-05-15 21:48:48 +00004695 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004696 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004697 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004698 "\01L_OBJC_LABEL_CLASS_$",
4699 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004700
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004701 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4702 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4703 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4704 continue;
4705 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004706 }
4707
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004708 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4709 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4710 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4711 continue;
4712 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4713 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004714
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004715 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004716 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4717 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004718
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004719 // Build list of all implemented category addresses in array
4720 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004721 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004722 "\01L_OBJC_LABEL_CATEGORY_$",
4723 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004724 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004725 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4726 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004727
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004728 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004729}
4730
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004731/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004732/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004733/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004734/// message dispatch call for all the rest.
4735///
4736bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004737 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4738 default:
4739 assert(0 && "Invalid dispatch method!");
4740 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004741 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004742 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004743 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004744 case CodeGenOptions::Mixed:
4745 break;
4746 }
4747
4748 // If so, see whether this selector is in the white-list of things which must
4749 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004750 if (NonLegacyDispatchMethods.empty()) {
4751 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4752 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4753 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4754 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4755 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4756 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4757 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4758 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4759 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4760 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004761
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004762 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4763 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4764 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4765 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4766 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4767 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4768 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4769 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004770 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004771 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004772 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4773 &CGM.getContext().Idents.get("objects"),
4774 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004775 };
4776 NonLegacyDispatchMethods.insert(
4777 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004778 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004779
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004780 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004781}
4782
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004783// Metadata flags
4784enum MetaDataDlags {
4785 CLS = 0x0,
4786 CLS_META = 0x1,
4787 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004788 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004789 CLS_EXCEPTION = 0x20
4790};
4791/// BuildClassRoTInitializer - generate meta-data for:
4792/// struct _class_ro_t {
4793/// uint32_t const flags;
4794/// uint32_t const instanceStart;
4795/// uint32_t const instanceSize;
4796/// uint32_t const reserved; // only when building for 64bit targets
4797/// const uint8_t * const ivarLayout;
4798/// const char *const name;
4799/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004800/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004801/// const struct _ivar_list_t *const ivars;
4802/// const uint8_t * const weakIvarLayout;
4803/// const struct _prop_list_t * const properties;
4804/// }
4805///
4806llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004807 unsigned flags,
4808 unsigned InstanceStart,
4809 unsigned InstanceSize,
4810 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004811 std::string ClassName = ID->getNameAsString();
4812 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004813 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4814 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4815 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004816 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004817 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4818 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004819 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004820 // const struct _method_list_t * const baseMethods;
4821 std::vector<llvm::Constant*> Methods;
4822 std::string MethodListName("\01l_OBJC_$_");
4823 if (flags & CLS_META) {
4824 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004825 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004826 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004827 // Class methods should always be defined.
4828 Methods.push_back(GetMethodConstant(*i));
4829 }
4830 } else {
4831 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004832 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004833 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004834 // Instance methods should always be defined.
4835 Methods.push_back(GetMethodConstant(*i));
4836 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004837 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004838 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004839 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004840
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004841 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4842 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004843
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004844 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4845 if (llvm::Constant *C = GetMethodConstant(MD))
4846 Methods.push_back(C);
4847 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4848 if (llvm::Constant *C = GetMethodConstant(MD))
4849 Methods.push_back(C);
4850 }
4851 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004852 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004853 Values[ 5] = EmitMethodList(MethodListName,
4854 "__DATA, __objc_const", Methods);
4855
Fariborz Jahanianda320092009-01-29 19:24:30 +00004856 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4857 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004858 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004859 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004860 OID->all_referenced_protocol_begin(),
4861 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004862
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004863 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004864 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004865 else
4866 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004867 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4868 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004869 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004870 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004871 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004872 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4873 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004874 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004875 Values);
4876 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004877 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4878 llvm::GlobalValue::InternalLinkage,
4879 Init,
4880 (flags & CLS_META) ?
4881 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4882 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004883 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004884 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004885 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004886 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004887
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004888}
4889
4890/// BuildClassMetaData - This routine defines that to-level meta-data
4891/// for the given ClassName for:
4892/// struct _class_t {
4893/// struct _class_t *isa;
4894/// struct _class_t * const superclass;
4895/// void *cache;
4896/// IMP *vtable;
4897/// struct class_ro_t *ro;
4898/// }
4899///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004900llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004901 std::string &ClassName,
4902 llvm::Constant *IsAGV,
4903 llvm::Constant *SuperClassGV,
4904 llvm::Constant *ClassRoGV,
4905 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004906 std::vector<llvm::Constant*> Values(5);
4907 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004908 Values[1] = SuperClassGV;
4909 if (!Values[1])
4910 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004911 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4912 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4913 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004914 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004915 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004916 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4917 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004918 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004919 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004920 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004921 if (HiddenVisibility)
4922 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004923 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004924}
4925
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004926bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004927CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004928 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004929}
4930
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004931void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004932 uint32_t &InstanceStart,
4933 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004934 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004935 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004936
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004937 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004938 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004939
4940 // If there are no fields, the start is the same as the end.
4941 if (!RL.getFieldCount())
4942 InstanceStart = InstanceSize;
4943 else
4944 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004945}
4946
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004947void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4948 std::string ClassName = ID->getNameAsString();
4949 if (!ObjCEmptyCacheVar) {
4950 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004951 CGM.getModule(),
4952 ObjCTypes.CacheTy,
4953 false,
4954 llvm::GlobalValue::ExternalLinkage,
4955 0,
4956 "_objc_empty_cache");
4957
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004958 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004959 CGM.getModule(),
4960 ObjCTypes.ImpnfABITy,
4961 false,
4962 llvm::GlobalValue::ExternalLinkage,
4963 0,
4964 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004965 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004966 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004967 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004968 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004969 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004970 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004971 uint32_t InstanceSize = InstanceStart;
4972 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004973 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4974 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004975
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004976 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004977
4978 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004979 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004980 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004981 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004982 if (ID->getNumIvarInitializers())
4983 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004984 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004985 // class is root
4986 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004987 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004988 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004989 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004990 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004991 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4992 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4993 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004994 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004995 if (Root->hasAttr<WeakImportAttr>())
4996 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004997 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004998 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004999 ObjCMetaClassName +
5000 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005001 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005002 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5003 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005004 }
5005 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5006 InstanceStart,
5007 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005008 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005009 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005010 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5011 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005012 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005013
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005014 // Metadata for the class
5015 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005016 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005017 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005018 if (ID->getNumIvarInitializers())
5019 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005020
Douglas Gregor68584ed2009-06-18 16:11:24 +00005021 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005022 flags |= CLS_EXCEPTION;
5023
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005024 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005025 flags |= CLS_ROOT;
5026 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005027 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005028 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005029 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005030 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005031 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005032 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
5033 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005034 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005035 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005036 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005037 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005038 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005039 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005040
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005041 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005042 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005043 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5044 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005045 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005046
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005047 // Determine if this class is also "non-lazy".
5048 if (ImplementationIsNonLazy(ID))
5049 DefinedNonLazyClasses.push_back(ClassMD);
5050
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005051 // Force the definition of the EHType if necessary.
5052 if (flags & CLS_EXCEPTION)
5053 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005054}
5055
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005056/// GenerateProtocolRef - This routine is called to generate code for
5057/// a protocol reference expression; as in:
5058/// @code
5059/// @protocol(Proto1);
5060/// @endcode
5061/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5062/// which will hold address of the protocol meta-data.
5063///
5064llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005065 const ObjCProtocolDecl *PD) {
5066
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005067 // This routine is called for @protocol only. So, we must build definition
5068 // of protocol's meta-data (not a reference to it!)
5069 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005070 llvm::Constant *Init =
5071 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5072 ObjCTypes.ExternalProtocolPtrTy);
5073
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005074 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005075 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005076
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005077 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5078 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005079 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005080 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005081 CGM.getModule(),
5082 Init->getType(), false,
5083 llvm::GlobalValue::WeakAnyLinkage,
5084 Init,
5085 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005086 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5087 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005088 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005089 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005090}
5091
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005092/// GenerateCategory - Build metadata for a category implementation.
5093/// struct _category_t {
5094/// const char * const name;
5095/// struct _class_t *const cls;
5096/// const struct _method_list_t * const instance_methods;
5097/// const struct _method_list_t * const class_methods;
5098/// const struct _protocol_list_t * const protocols;
5099/// const struct _prop_list_t * const properties;
5100/// }
5101///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005102void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005103 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005104 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005105 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5106 "_$_" + OCD->getNameAsString());
5107 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005108 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005109
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005110 std::vector<llvm::Constant*> Values(6);
5111 Values[0] = GetClassName(OCD->getIdentifier());
5112 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005113 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005114 if (Interface->hasAttr<WeakImportAttr>())
5115 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5116
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005117 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005118 std::vector<llvm::Constant*> Methods;
5119 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005120 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005121 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005122
5123 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005124 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005125 // Instance methods should always be defined.
5126 Methods.push_back(GetMethodConstant(*i));
5127 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005128
5129 Values[2] = EmitMethodList(MethodListName,
5130 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005131 Methods);
5132
5133 MethodListName = Prefix;
5134 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5135 OCD->getNameAsString();
5136 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005137 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005138 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005139 // Class methods should always be defined.
5140 Methods.push_back(GetMethodConstant(*i));
5141 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005142
5143 Values[3] = EmitMethodList(MethodListName,
5144 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005145 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005146 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005147 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005148 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005149 llvm::SmallString<256> ExtName;
5150 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5151 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005152 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005153 + Interface->getName() + "_$_"
5154 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005155 Category->protocol_begin(),
5156 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005157 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5158 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005159 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005160 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5161 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005162 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005163
5164 llvm::Constant *Init =
5165 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005166 Values);
5167 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005168 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005169 false,
5170 llvm::GlobalValue::InternalLinkage,
5171 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005172 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005173 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005174 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005175 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005176 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005177 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005178
5179 // Determine if this category is also "non-lazy".
5180 if (ImplementationIsNonLazy(OCD))
5181 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005182}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005183
5184/// GetMethodConstant - Return a struct objc_method constant for the
5185/// given method if it has been defined. The result is null if the
5186/// method has not been defined. The return value has type MethodPtrTy.
5187llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005188 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005189 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005190 if (!Fn)
5191 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005192
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005193 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005194 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00005195 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005196 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005197 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005198 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005199 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005200}
5201
5202/// EmitMethodList - Build meta-data for method declarations
5203/// struct _method_list_t {
5204/// uint32_t entsize; // sizeof(struct _objc_method)
5205/// uint32_t method_count;
5206/// struct _objc_method method_list[method_count];
5207/// }
5208///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005209llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
5210 const char *Section,
5211 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005212 // Return null for empty list.
5213 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005214 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005215
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005216 std::vector<llvm::Constant*> Values(3);
5217 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005218 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005219 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005220 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005221 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005222 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005223 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005224 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005225 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005226
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005227 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005228 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005229 llvm::GlobalValue::InternalLinkage,
5230 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005231 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005232 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005233 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005234 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005235 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005236 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005237 ObjCTypes.MethodListnfABIPtrTy);
5238}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005239
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005240/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5241/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005242llvm::GlobalVariable *
5243CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5244 const ObjCIvarDecl *Ivar) {
5245 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005246 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005247 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005248 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005249 CGM.getModule().getGlobalVariable(Name);
5250 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005251 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005252 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005253 false,
5254 llvm::GlobalValue::ExternalLinkage,
5255 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005256 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005257 return IvarOffsetGV;
5258}
5259
Daniel Dunbare83be122010-04-02 21:14:02 +00005260llvm::Constant *
5261CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5262 const ObjCIvarDecl *Ivar,
5263 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005264 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005265 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005266 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005267 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005268 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005269
Mike Stumpf5408fe2009-05-16 07:57:57 +00005270 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5271 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005272 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5273 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005274 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005275 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005276 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005277 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005278 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005279 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005280}
5281
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005282/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005283/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005284/// IvarListnfABIPtrTy.
5285/// struct _ivar_t {
5286/// unsigned long int *offset; // pointer to ivar offset location
5287/// char *name;
5288/// char *type;
5289/// uint32_t alignment;
5290/// uint32_t size;
5291/// }
5292/// struct _ivar_list_t {
5293/// uint32 entsize; // sizeof(struct _ivar_t)
5294/// uint32 count;
5295/// struct _iver_t list[count];
5296/// }
5297///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005298
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005299llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005300 const ObjCImplementationDecl *ID) {
5301
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005302 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005303
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005304 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5305 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005306
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005307 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005308
Daniel Dunbar91636d62009-04-20 00:33:43 +00005309 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005310 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005311 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005312
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005313 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5314 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005315 // Ignore unnamed bit-fields.
5316 if (!IVD->getDeclName())
5317 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005318 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005319 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005320 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5321 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005322 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005323 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005324 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005325 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005326 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005327 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005328 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005329 // NOTE. Size of a bitfield does not match gcc's, because of the
5330 // way bitfields are treated special in each. But I am told that
5331 // 'size' for bitfield ivars is ignored by the runtime so it does
5332 // not matter. If it matters, there is enough info to get the
5333 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005334 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005335 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005336 }
5337 // Return null for empty list.
5338 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005339 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005340 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005341 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005342 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5343 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005344 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005345 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005346 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005347 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005348 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5349 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005350 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005351 llvm::GlobalValue::InternalLinkage,
5352 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005353 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005354 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005355 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005356 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005357
Chris Lattnerad64e022009-07-17 23:57:13 +00005358 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005359 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005360}
5361
5362llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005363 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005364 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005365
Fariborz Jahanianda320092009-01-29 19:24:30 +00005366 if (!Entry) {
5367 // We use the initializer as a marker of whether this is a forward
5368 // reference or not. At module finalization we add the empty
5369 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005370 Entry =
5371 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5372 llvm::GlobalValue::ExternalLinkage,
5373 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005374 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005375 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005376 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005377
Fariborz Jahanianda320092009-01-29 19:24:30 +00005378 return Entry;
5379}
5380
5381/// GetOrEmitProtocol - Generate the protocol meta-data:
5382/// @code
5383/// struct _protocol_t {
5384/// id isa; // NULL
5385/// const char * const protocol_name;
5386/// const struct _protocol_list_t * protocol_list; // super protocols
5387/// const struct method_list_t * const instance_methods;
5388/// const struct method_list_t * const class_methods;
5389/// const struct method_list_t *optionalInstanceMethods;
5390/// const struct method_list_t *optionalClassMethods;
5391/// const struct _prop_list_t * properties;
5392/// const uint32_t size; // sizeof(struct _protocol_t)
5393/// const uint32_t flags; // = 0
5394/// }
5395/// @endcode
5396///
5397
5398llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005399 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005400 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005401
Fariborz Jahanianda320092009-01-29 19:24:30 +00005402 // Early exit if a defining object has already been generated.
5403 if (Entry && Entry->hasInitializer())
5404 return Entry;
5405
Fariborz Jahanianda320092009-01-29 19:24:30 +00005406 // Construct method lists.
5407 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5408 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005409 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005410 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005411 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005412 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005413 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5414 OptInstanceMethods.push_back(C);
5415 } else {
5416 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005417 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005418 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005419
5420 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005421 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005422 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005423 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005424 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5425 OptClassMethods.push_back(C);
5426 } else {
5427 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005428 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005429 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005430
Fariborz Jahanianda320092009-01-29 19:24:30 +00005431 std::vector<llvm::Constant*> Values(10);
5432 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005433 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005434 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005435 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5436 PD->protocol_begin(),
5437 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005438
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005439 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005440 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005441 "__DATA, __objc_const",
5442 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005443 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005444 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005445 "__DATA, __objc_const",
5446 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005447 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005448 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005449 "__DATA, __objc_const",
5450 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005451 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005452 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005453 "__DATA, __objc_const",
5454 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005455 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005456 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005457 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005458 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005459 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005460 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005461 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005462 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005463
Fariborz Jahanianda320092009-01-29 19:24:30 +00005464 if (Entry) {
5465 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005466 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005467 Entry->setInitializer(Init);
5468 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005469 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005470 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5471 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5472 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005473 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005474 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005475 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005476 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005477 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005478 CGM.AddUsedGlobal(Entry);
5479
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005480 // Use this protocol meta-data to build protocol list table in section
5481 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005482 llvm::GlobalVariable *PTGV =
5483 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5484 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5485 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005486 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005487 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005488 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005489 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005490 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005491 return Entry;
5492}
5493
5494/// EmitProtocolList - Generate protocol list meta-data:
5495/// @code
5496/// struct _protocol_list_t {
5497/// long protocol_count; // Note, this is 32/64 bit
5498/// struct _protocol_t[protocol_count];
5499/// }
5500/// @endcode
5501///
5502llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005503CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5504 ObjCProtocolDecl::protocol_iterator begin,
5505 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005506 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005507
Fariborz Jahanianda320092009-01-29 19:24:30 +00005508 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005509 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005510 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005511
Daniel Dunbar948e2582009-02-15 07:36:20 +00005512 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005513 llvm::SmallString<256> TmpName;
5514 Name.toVector(TmpName);
5515 llvm::GlobalVariable *GV =
5516 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005517 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005518 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005519
Daniel Dunbar948e2582009-02-15 07:36:20 +00005520 for (; begin != end; ++begin)
5521 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5522
Fariborz Jahanianda320092009-01-29 19:24:30 +00005523 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005524 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005525 ObjCTypes.ProtocolnfABIPtrTy));
5526
Fariborz Jahanianda320092009-01-29 19:24:30 +00005527 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005528 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005529 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005530 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005531 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005532 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005533 ProtocolRefs.size()),
5534 ProtocolRefs);
5535
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005536 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005537 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005538 llvm::GlobalValue::InternalLinkage,
5539 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005540 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005541 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005542 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005543 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005544 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005545 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005546 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005547}
5548
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005549/// GetMethodDescriptionConstant - This routine build following meta-data:
5550/// struct _objc_method {
5551/// SEL _cmd;
5552/// char *method_type;
5553/// char *_imp;
5554/// }
5555
5556llvm::Constant *
5557CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5558 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005559 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005560 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5561 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005562 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005563 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005564 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005565 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005566}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005567
5568/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5569/// This code gen. amounts to generating code for:
5570/// @code
5571/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5572/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005573///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005574LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005575 CodeGen::CodeGenFunction &CGF,
5576 QualType ObjectTy,
5577 llvm::Value *BaseValue,
5578 const ObjCIvarDecl *Ivar,
5579 unsigned CVRQualifiers) {
5580 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005581 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5582 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005583}
5584
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005585llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005586 CodeGen::CodeGenFunction &CGF,
5587 const ObjCInterfaceDecl *Interface,
5588 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005589 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005590}
5591
Fariborz Jahanian46551122009-02-04 00:22:57 +00005592CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005593 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005594 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005595 QualType ResultType,
5596 Selector Sel,
5597 llvm::Value *Receiver,
5598 QualType Arg0Ty,
5599 bool IsSuper,
5600 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005601 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5602 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005603 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005604 llvm::Value *Arg0 = Receiver;
5605 if (!IsSuper)
5606 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005607
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005608 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005609 // FIXME. This is too much work to get the ABI-specific result type needed to
5610 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005611 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005612 = Types.getFunctionInfo(ResultType, CallArgList(),
5613 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005614 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005615 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005616 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005617 if (IsSuper) {
5618 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5619 Name += "objc_msgSendSuper2_stret_fixup";
5620 } else {
5621 Fn = ObjCTypes.getMessageSendStretFixupFn();
5622 Name += "objc_msgSend_stret_fixup";
5623 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005624 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5625 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5626 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005627 } else {
Chris Lattner9b7d6702010-08-18 16:09:06 +00005628 if (IsSuper) {
5629 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
5630 Name += "objc_msgSendSuper2_fixup";
5631 } else {
5632 Fn = ObjCTypes.getMessageSendFixupFn();
5633 Name += "objc_msgSend_fixup";
5634 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005635 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005636 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005637 Name += '_';
5638 std::string SelName(Sel.getAsString());
5639 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005640 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005641 if (SelName[i] == ':')
5642 SelName[i] = '_';
5643 Name += SelName;
5644 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5645 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005646 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005647 std::vector<llvm::Constant*> Values(2);
5648 Values[0] = Fn;
5649 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005650 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005651 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005652 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005653 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005654 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005655 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005656 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005657 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005658 }
5659 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005660
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005661 CallArgList ActualArgs;
5662 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005663 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005664 ObjCTypes.MessageRefCPtrTy));
5665 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005666 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005667 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005668 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5669 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005670 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005671 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005672 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005673 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005674}
5675
5676/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005677CodeGen::RValue
5678CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005679 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005680 QualType ResultType,
5681 Selector Sel,
5682 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005683 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005684 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005685 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005686 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005687 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5688 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005689 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005690 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005691 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005692 Receiver, CGF.getContext().getObjCIdType(),
5693 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005694}
5695
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005696llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005697CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005698 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5699
Daniel Dunbardfff2302009-03-02 05:18:14 +00005700 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005701 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005702 false, llvm::GlobalValue::ExternalLinkage,
5703 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005704 }
5705
5706 return GV;
5707}
5708
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005709llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5710 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005711 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005712
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005713 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005714 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005715 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005716 Entry =
5717 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005718 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005719 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005720 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005721 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005722 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005723 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005724 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005725 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005726 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005727
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005728 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005729}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005730
Daniel Dunbar11394522009-04-18 08:51:00 +00005731llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005732CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005733 const ObjCInterfaceDecl *ID) {
5734 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005735
Daniel Dunbar11394522009-04-18 08:51:00 +00005736 if (!Entry) {
5737 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5738 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005739 Entry =
5740 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005741 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005742 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005743 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005744 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005745 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005746 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005747 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005748 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005749 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005750
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005751 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005752}
5753
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005754/// EmitMetaClassRef - Return a Value * of the address of _class_t
5755/// meta-data
5756///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005757llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5758 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005759 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5760 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005761 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005762
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005763 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005764 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005765 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005766 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005767 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005768 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005769 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005770 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005771 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005772 ObjCTypes.ClassnfABIPtrTy));
5773
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005774 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005775 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005776
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005777 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005778}
5779
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005780/// GetClass - Return a reference to the class for the given interface
5781/// decl.
5782llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5783 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005784 if (ID->hasAttr<WeakImportAttr>()) {
5785 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5786 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5787 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5788 }
5789
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005790 return EmitClassRef(Builder, ID);
5791}
5792
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005793/// Generates a message send where the super is the receiver. This is
5794/// a message send to self with special delivery semantics indicating
5795/// which class's method should be called.
5796CodeGen::RValue
5797CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005798 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005799 QualType ResultType,
5800 Selector Sel,
5801 const ObjCInterfaceDecl *Class,
5802 bool isCategoryImpl,
5803 llvm::Value *Receiver,
5804 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005805 const CodeGen::CallArgList &CallArgs,
5806 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005807 // ...
5808 // Create and init a super structure; this is a (receiver, class)
5809 // pair we will pass to objc_msgSendSuper.
5810 llvm::Value *ObjCSuper =
5811 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005812
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005813 llvm::Value *ReceiverAsObject =
5814 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5815 CGF.Builder.CreateStore(ReceiverAsObject,
5816 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005817
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005818 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005819 llvm::Value *Target;
5820 if (IsClassMessage) {
5821 if (isCategoryImpl) {
5822 // Message sent to "super' in a class method defined in
5823 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005824 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005825 Target = CGF.Builder.CreateStructGEP(Target, 0);
5826 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005827 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005828 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005829 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005830 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005831
Mike Stumpf5408fe2009-05-16 07:57:57 +00005832 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5833 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005834 const llvm::Type *ClassTy =
5835 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5836 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5837 CGF.Builder.CreateStore(Target,
5838 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005839
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005840 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005841 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5842 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005843 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005844 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005845 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005846 ObjCSuper, ObjCTypes.SuperPtrCTy,
5847 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005848}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005849
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005850llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005851 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005852 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005853
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005854 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005855 llvm::Constant *Casted =
5856 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5857 ObjCTypes.SelectorPtrTy);
5858 Entry =
5859 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5860 llvm::GlobalValue::InternalLinkage,
5861 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005862 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005863 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005864 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005865
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005866 if (lval)
5867 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005868 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005869}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005870/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005871/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005872///
5873void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005874 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005875 llvm::Value *dst,
5876 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005877 const llvm::Type * SrcTy = src->getType();
5878 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005879 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005880 assert(Size <= 8 && "does not support size > 8");
5881 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5882 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005883 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5884 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005885 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5886 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005887 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5888 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005889 return;
5890}
5891
5892/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5893/// objc_assign_strongCast (id src, id *dst)
5894///
5895void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005896 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005897 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005898 const llvm::Type * SrcTy = src->getType();
5899 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005900 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005901 assert(Size <= 8 && "does not support size > 8");
5902 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005903 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005904 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5905 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005906 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5907 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005908 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005909 src, dst, "weakassign");
5910 return;
5911}
5912
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005913void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005914 CodeGen::CodeGenFunction &CGF,
5915 llvm::Value *DestPtr,
5916 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005917 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005918 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5919 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005920 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005921 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005922 return;
5923}
5924
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005925/// EmitObjCWeakRead - Code gen for loading value of a __weak
5926/// object: objc_read_weak (id *src)
5927///
5928llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005929 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005930 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005931 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005932 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5933 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005934 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005935 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005936 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005937 return read_weak;
5938}
5939
5940/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5941/// objc_assign_weak (id src, id *dst)
5942///
5943void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005944 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005945 const llvm::Type * SrcTy = src->getType();
5946 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005947 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005948 assert(Size <= 8 && "does not support size > 8");
5949 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5950 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005951 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5952 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005953 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5954 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005955 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005956 src, dst, "weakassign");
5957 return;
5958}
5959
5960/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5961/// objc_assign_global (id src, id *dst)
5962///
5963void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005964 llvm::Value *src, llvm::Value *dst,
5965 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005966 const llvm::Type * SrcTy = src->getType();
5967 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005968 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005969 assert(Size <= 8 && "does not support size > 8");
5970 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5971 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005972 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5973 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005974 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5975 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005976 if (!threadlocal)
5977 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5978 src, dst, "globalassign");
5979 else
5980 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5981 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005982 return;
5983}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005984
John McCall740e8072010-07-21 00:41:47 +00005985namespace {
John McCall1f0fca52010-07-21 07:22:38 +00005986 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00005987 llvm::Value *SyncExitFn;
5988 llvm::Value *SyncArg;
5989 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5990 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5991
5992 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5993 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5994 }
5995 };
5996}
5997
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005998void
John McCallf1549f62010-07-06 01:34:17 +00005999CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6000 const ObjCAtSynchronizedStmt &S) {
6001 // Evaluate the lock operand. This should dominate the cleanup.
6002 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006003
John McCallf1549f62010-07-06 01:34:17 +00006004 // Acquire the lock.
6005 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
6006 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
6007 ->setDoesNotThrow();
6008
6009 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00006010 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
6011 ObjCTypes.getSyncExitFn(),
6012 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006013
John McCallf1549f62010-07-06 01:34:17 +00006014 // Emit the body of the statement.
6015 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006016
John McCallf1549f62010-07-06 01:34:17 +00006017 // Pop the lock-release cleanup.
6018 CGF.PopCleanupBlock();
6019}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006020
John McCallf1549f62010-07-06 01:34:17 +00006021namespace {
6022 struct CatchHandler {
6023 const VarDecl *Variable;
6024 const Stmt *Body;
6025 llvm::BasicBlock *Block;
6026 llvm::Value *TypeInfo;
6027 };
John McCall8e3f8612010-07-13 22:12:14 +00006028
John McCall1f0fca52010-07-21 07:22:38 +00006029 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00006030 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
6031 MightThrow(MightThrow), Fn(Fn) {}
6032 bool MightThrow;
6033 llvm::Value *Fn;
6034
6035 void Emit(CodeGenFunction &CGF, bool IsForEH) {
6036 if (!MightThrow) {
6037 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
6038 return;
6039 }
6040
6041 CGF.EmitCallOrInvoke(Fn, 0, 0);
6042 }
6043 };
John McCallf1549f62010-07-06 01:34:17 +00006044}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006045
John McCall5a180392010-07-24 00:37:23 +00006046llvm::Constant *
6047CGObjCNonFragileABIMac::GetEHType(QualType T) {
6048 // There's a particular fixed type info for 'id'.
6049 if (T->isObjCIdType() ||
6050 T->isObjCQualifiedIdType()) {
6051 llvm::Constant *IDEHType =
6052 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6053 if (!IDEHType)
6054 IDEHType =
6055 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6056 false,
6057 llvm::GlobalValue::ExternalLinkage,
6058 0, "OBJC_EHTYPE_id");
6059 return IDEHType;
6060 }
6061
6062 // All other types should be Objective-C interface pointer types.
6063 const ObjCObjectPointerType *PT =
6064 T->getAs<ObjCObjectPointerType>();
6065 assert(PT && "Invalid @catch type.");
6066 const ObjCInterfaceType *IT = PT->getInterfaceType();
6067 assert(IT && "Invalid @catch type.");
6068 return GetInterfaceEHType(IT->getDecl(), false);
6069}
6070
John McCallf1549f62010-07-06 01:34:17 +00006071void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6072 const ObjCAtTryStmt &S) {
6073 // Jump destination for falling out of catch bodies.
6074 CodeGenFunction::JumpDest Cont;
6075 if (S.getNumCatchStmts())
6076 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006077
John McCallf1549f62010-07-06 01:34:17 +00006078 CodeGenFunction::FinallyInfo FinallyInfo;
6079 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
6080 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
6081 ObjCTypes.getObjCBeginCatchFn(),
6082 ObjCTypes.getObjCEndCatchFn(),
6083 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006084
John McCallf1549f62010-07-06 01:34:17 +00006085 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006086
John McCallf1549f62010-07-06 01:34:17 +00006087 // Enter the catch, if there is one.
6088 if (S.getNumCatchStmts()) {
6089 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
6090 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00006091 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006092
John McCallf1549f62010-07-06 01:34:17 +00006093 Handlers.push_back(CatchHandler());
6094 CatchHandler &Handler = Handlers.back();
6095 Handler.Variable = CatchDecl;
6096 Handler.Body = CatchStmt->getCatchBody();
6097 Handler.Block = CGF.createBasicBlock("catch");
6098
6099 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006100 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00006101 Handler.TypeInfo = 0; // catch-all
6102 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00006103 break;
6104 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006105
John McCall5a180392010-07-24 00:37:23 +00006106 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006107 }
John McCallf1549f62010-07-06 01:34:17 +00006108
6109 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
6110 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
6111 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006112 }
John McCallf1549f62010-07-06 01:34:17 +00006113
6114 // Emit the try body.
6115 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006116
John McCallf1549f62010-07-06 01:34:17 +00006117 // Leave the try.
6118 if (S.getNumCatchStmts())
6119 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006120
John McCallf1549f62010-07-06 01:34:17 +00006121 // Remember where we were.
6122 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006123
John McCallf1549f62010-07-06 01:34:17 +00006124 // Emit the handlers.
6125 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
6126 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006127
John McCallf1549f62010-07-06 01:34:17 +00006128 CGF.EmitBlock(Handler.Block);
6129 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006130
John McCallf1549f62010-07-06 01:34:17 +00006131 // Enter the catch.
6132 llvm::CallInst *Exn =
6133 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
6134 "exn.adjusted");
6135 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006136
John McCallf1549f62010-07-06 01:34:17 +00006137 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00006138 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00006139 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
6140 EndCatchMightThrow,
6141 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006142
John McCallf1549f62010-07-06 01:34:17 +00006143 // Bind the catch parameter if it exists.
6144 if (const VarDecl *CatchParam = Handler.Variable) {
6145 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
6146 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006147
John McCallb6bbcc92010-10-15 04:57:14 +00006148 CGF.EmitAutoVarDecl(*CatchParam);
John McCallf1549f62010-07-06 01:34:17 +00006149 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006150 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00006151
John McCallf1549f62010-07-06 01:34:17 +00006152 CGF.ObjCEHValueStack.push_back(Exn);
6153 CGF.EmitStmt(Handler.Body);
6154 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006155
John McCallf1549f62010-07-06 01:34:17 +00006156 // Leave the earlier cleanup.
6157 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006158
John McCallf1549f62010-07-06 01:34:17 +00006159 CGF.EmitBranchThroughCleanup(Cont);
6160 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006161
John McCallf1549f62010-07-06 01:34:17 +00006162 // Go back to the try-statement fallthrough.
6163 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006164
John McCallf1549f62010-07-06 01:34:17 +00006165 // Pop out of the normal cleanup on the finally.
6166 if (S.getFinallyStmt())
6167 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006168
John McCallff8e1152010-07-23 21:56:41 +00006169 if (Cont.isValid())
6170 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006171}
6172
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006173/// EmitThrowStmt - Generate code for a throw statement.
6174void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6175 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006176 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall7ec404c2010-10-16 08:21:07 +00006177 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
John McCallfd186ac2010-10-16 16:34:08 +00006178 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy,
6179 "tmp");
John McCall7ec404c2010-10-16 08:21:07 +00006180 llvm::Value *Args[] = { Exception };
6181 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(),
6182 Args, Args+1)
6183 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006184 } else {
John McCall7ec404c2010-10-16 08:21:07 +00006185 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn(), 0, 0)
6186 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006187 }
6188
John McCall7ec404c2010-10-16 08:21:07 +00006189 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006190 CGF.Builder.ClearInsertionPoint();
6191}
Daniel Dunbare588b992009-03-01 04:46:24 +00006192
John McCall5a180392010-07-24 00:37:23 +00006193llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006194CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006195 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006196 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006197
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006198 // If we don't need a definition, return the entry if found or check
6199 // if we use an external reference.
6200 if (!ForDefinition) {
6201 if (Entry)
6202 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006203
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006204 // If this type (or a super class) has the __objc_exception__
6205 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006206 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006207 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006208 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006209 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006210 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006211 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006212 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006213 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006214
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006215 // Otherwise we need to either make a new entry or fill in the
6216 // initializer.
6217 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006218 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006219 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006220 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006221 CGM.getModule().getGlobalVariable(VTableName);
6222 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006223 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6224 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006225 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006226 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006227
Chris Lattner77b89b82010-06-27 07:15:29 +00006228 llvm::Value *VTableIdx =
6229 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006230
6231 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00006232 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00006233 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00006234 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00006235 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006236 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006237
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006238 if (Entry) {
6239 Entry->setInitializer(Init);
6240 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006241 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006242 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006243 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006244 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006245 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006246 }
6247
John McCall1fb0caa2010-10-22 21:05:15 +00006248 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006249 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006250 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6251 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006252
6253 if (ForDefinition) {
6254 Entry->setSection("__DATA,__objc_const");
6255 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6256 } else {
6257 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6258 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006259
6260 return Entry;
6261}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006262
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006263/* *** */
6264
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006265CodeGen::CGObjCRuntime *
6266CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006267 return new CGObjCMac(CGM);
6268}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006269
6270CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006271CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006272 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006273}