blob: d917c7d8a87e186a65cf278a8c2af0ceca5f0ae1 [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 McCallf1549f62010-07-06 01:34:17 +000019#include "CGException.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
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +000028#include "llvm/Intrinsics.h"
Owen Anderson69243822009-07-13 04:10:07 +000029#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000030#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000031#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000032#include "llvm/ADT/SetVector.h"
33#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000034#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000035#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000036#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000037#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000038#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000039
40using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000041using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000042
Daniel Dunbar97776872009-04-22 07:32:20 +000043// Common CGObjCRuntime functions, these don't belong here, but they
44// don't belong in CGObjCRuntime either so we will live with it for
45// now.
46
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000047static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
48 const ObjCInterfaceDecl *OID,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000049 const ObjCImplementationDecl *ID,
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000050 const ObjCIvarDecl *Ivar) {
Daniel Dunbare83be122010-04-02 21:14:02 +000051 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar532d4da2009-05-03 13:15:50 +000052
Daniel Dunbar61ac1d22010-04-02 22:29:40 +000053 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
54 // in here; it should never be necessary because that should be the lexical
55 // decl context for the ivar.
Daniel Dunbar3a2c80f2010-04-02 15:43:29 +000056
Daniel Dunbar532d4da2009-05-03 13:15:50 +000057 // If we know have an implementation (and the ivar is in it) then
58 // look up in the implementation layout.
Daniel Dunbar6bff2512009-08-03 17:06:42 +000059 const ASTRecordLayout *RL;
Daniel Dunbar532d4da2009-05-03 13:15:50 +000060 if (ID && ID->getClassInterface() == Container)
61 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
62 else
63 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbare83be122010-04-02 21:14:02 +000064
65 // Compute field index.
66 //
67 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
68 // implemented. This should be fixed to get the information from the layout
69 // directly.
70 unsigned Index = 0;
71 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
72 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
73 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
74 if (Ivar == Ivars[k])
75 break;
76 ++Index;
77 }
78 assert(Index != Ivars.size() && "Ivar is not inside container!");
79
Daniel Dunbar532d4da2009-05-03 13:15:50 +000080 return RL->getFieldOffset(Index);
Daniel Dunbar1d7e5392009-05-03 08:55:17 +000081}
82
83uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
84 const ObjCInterfaceDecl *OID,
85 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +000086 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
87}
88
89uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
90 const ObjCImplementationDecl *OID,
91 const ObjCIvarDecl *Ivar) {
92 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar97776872009-04-22 07:32:20 +000093}
94
95LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
96 const ObjCInterfaceDecl *OID,
97 llvm::Value *BaseValue,
98 const ObjCIvarDecl *Ivar,
99 unsigned CVRQualifiers,
100 llvm::Value *Offset) {
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000101 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000102 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar1d7e5392009-05-03 08:55:17 +0000103 QualType IvarTy = Ivar->getType();
104 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar97776872009-04-22 07:32:20 +0000105 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar97776872009-04-22 07:32:20 +0000106 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson96e0fc72009-07-29 22:16:19 +0000107 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000108
John McCall0953e762009-09-24 19:53:00 +0000109 Qualifiers Quals = CGF.MakeQualifiers(IvarTy);
110 Quals.addCVRQualifiers(CVRQualifiers);
111
Daniel Dunbar56229f52010-04-05 16:20:33 +0000112 if (!Ivar->isBitField())
113 return LValue::MakeAddr(V, Quals);
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +0000114
Daniel Dunbar56229f52010-04-05 16:20:33 +0000115 // We need to compute the bit offset for the bit-field, the offset is to the
116 // byte. Note, there is a subtle invariant here: we can only call this routine
117 // on non-synthesized ivars but we may be called for synthesized ivars.
118 // However, a synthesized ivar can never be a bit-field, so this is safe.
119 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
120 uint64_t BitFieldSize =
121 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar97776872009-04-22 07:32:20 +0000122
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000123 // Allocate a new CGBitFieldInfo object to describe this access.
124 //
125 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
126 // layout object. However, this is blocked on other cleanups to the
127 // Objective-C code, so for now we just live with allocating a bunch of these
128 // objects.
Daniel Dunbarf0fe5bc2010-04-05 21:36:35 +0000129
Daniel Dunbarab970f92010-04-13 20:58:55 +0000130 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar2df25692010-04-15 05:09:32 +0000131 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarab970f92010-04-13 20:58:55 +0000132 AI.FieldIndex = 0;
133 AI.FieldByteOffset = 0;
134 AI.FieldBitStart = BitOffset;
135 AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy);
136 AI.AccessAlignment = 0;
137 AI.TargetBitOffset = 0;
138 AI.TargetBitWidth = BitFieldSize;
139
Daniel Dunbar2df25692010-04-15 05:09:32 +0000140 CGBitFieldInfo *Info =
141 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
142 IvarTy->isSignedIntegerType());
143
Daniel Dunbar56229f52010-04-05 16:20:33 +0000144 // FIXME: We need to set a very conservative alignment on this, or make sure
145 // that the runtime is doing the right thing.
Daniel Dunbarefbf4872010-04-06 01:07:44 +0000146 return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());
Daniel Dunbar97776872009-04-22 07:32:20 +0000147}
148
149///
150
Daniel Dunbarc17a4d32008-08-11 02:45:11 +0000151namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000152
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000153typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000154
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000155// FIXME: We should find a nicer way to make the labels for metadata, string
156// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000157
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000158class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +0000159protected:
160 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000161
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000162private:
163 llvm::Constant *getMessageSendFn() const {
164 // id objc_msgSend (id, SEL, ...)
165 std::vector<const llvm::Type*> Params;
166 Params.push_back(ObjectPtrTy);
167 Params.push_back(SelectorPtrTy);
168 return
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000169 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
170 Params, true),
171 "objc_msgSend");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000172 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000173
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000174 llvm::Constant *getMessageSendStretFn() const {
175 // id objc_msgSend_stret (id, SEL, ...)
176 std::vector<const llvm::Type*> Params;
177 Params.push_back(ObjectPtrTy);
178 Params.push_back(SelectorPtrTy);
179 return
Owen Anderson0032b272009-08-13 21:57:51 +0000180 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000181 Params, true),
182 "objc_msgSend_stret");
183
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000184 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000185
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000186 llvm::Constant *getMessageSendFpretFn() const {
187 // FIXME: This should be long double on x86_64?
188 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
189 std::vector<const llvm::Type*> Params;
190 Params.push_back(ObjectPtrTy);
191 Params.push_back(SelectorPtrTy);
192 return
Owen Anderson0032b272009-08-13 21:57:51 +0000193 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
194 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000195 Params,
196 true),
197 "objc_msgSend_fpret");
198
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000199 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000200
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000201 llvm::Constant *getMessageSendSuperFn() const {
202 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
203 const char *SuperName = "objc_msgSendSuper";
204 std::vector<const llvm::Type*> Params;
205 Params.push_back(SuperPtrTy);
206 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000207 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000208 Params, true),
209 SuperName);
210 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000211
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000212 llvm::Constant *getMessageSendSuperFn2() const {
213 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
214 const char *SuperName = "objc_msgSendSuper2";
215 std::vector<const llvm::Type*> Params;
216 Params.push_back(SuperPtrTy);
217 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000218 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000219 Params, true),
220 SuperName);
221 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000222
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000223 llvm::Constant *getMessageSendSuperStretFn() const {
224 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
225 // SEL op, ...)
226 std::vector<const llvm::Type*> Params;
227 Params.push_back(Int8PtrTy);
228 Params.push_back(SuperPtrTy);
229 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000230 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000231 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000232 Params, true),
233 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000234 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000235
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000236 llvm::Constant *getMessageSendSuperStretFn2() const {
237 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
238 // SEL op, ...)
239 std::vector<const llvm::Type*> Params;
240 Params.push_back(Int8PtrTy);
241 Params.push_back(SuperPtrTy);
242 Params.push_back(SelectorPtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000243 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000244 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000245 Params, true),
246 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000247 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000248
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000249 llvm::Constant *getMessageSendSuperFpretFn() const {
250 // There is no objc_msgSendSuper_fpret? How can that work?
251 return getMessageSendSuperFn();
252 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000253
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000254 llvm::Constant *getMessageSendSuperFpretFn2() const {
255 // There is no objc_msgSendSuper_fpret? How can that work?
256 return getMessageSendSuperFn2();
257 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000258
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000259protected:
260 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000261
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000262public:
Fariborz Jahanian0a855d02009-03-23 19:10:40 +0000263 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000264 const llvm::Type *Int8PtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000265
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000266 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
267 const llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000268
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000269 /// PtrObjectPtrTy - LLVM type for id *
270 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000271
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000272 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000273 const llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000274 /// ProtocolPtrTy - LLVM type for external protocol handles
275 /// (typeof(Protocol))
276 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000277
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000278 // SuperCTy - clang type for struct objc_super.
279 QualType SuperCTy;
280 // SuperPtrCTy - clang type for struct objc_super *.
281 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000282
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000283 /// SuperTy - LLVM type for struct objc_super.
284 const llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000285 /// SuperPtrTy - LLVM type for struct objc_super *.
286 const llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000287
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000288 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
289 /// in GCC parlance).
290 const llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000291
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000292 /// PropertyListTy - LLVM type for struct objc_property_list
293 /// (_prop_list_t in GCC parlance).
294 const llvm::StructType *PropertyListTy;
295 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
296 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000297
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000298 // MethodTy - LLVM type for struct objc_method.
299 const llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000300
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000301 /// CacheTy - LLVM type for struct objc_cache.
302 const llvm::Type *CacheTy;
303 /// CachePtrTy - LLVM type for struct objc_cache *.
304 const llvm::Type *CachePtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000305
Chris Lattner72db6c32009-04-22 02:44:54 +0000306 llvm::Constant *getGetPropertyFn() {
307 CodeGen::CodeGenTypes &Types = CGM.getTypes();
308 ASTContext &Ctx = CGM.getContext();
309 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCallead608a2010-02-26 00:48:12 +0000310 llvm::SmallVector<CanQualType,4> Params;
311 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
312 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000313 Params.push_back(IdType);
314 Params.push_back(SelType);
315 Params.push_back(Ctx.LongTy);
316 Params.push_back(Ctx.BoolTy);
317 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000318 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000319 FunctionType::ExtInfo()),
320 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
322 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000323
Chris Lattner72db6c32009-04-22 02:44:54 +0000324 llvm::Constant *getSetPropertyFn() {
325 CodeGen::CodeGenTypes &Types = CGM.getTypes();
326 ASTContext &Ctx = CGM.getContext();
327 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCallead608a2010-02-26 00:48:12 +0000328 llvm::SmallVector<CanQualType,6> Params;
329 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
330 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000331 Params.push_back(IdType);
332 Params.push_back(SelType);
333 Params.push_back(Ctx.LongTy);
334 Params.push_back(IdType);
335 Params.push_back(Ctx.BoolTy);
336 Params.push_back(Ctx.BoolTy);
337 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000338 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000339 FunctionType::ExtInfo()),
340 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000341 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
342 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000343
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000344
345 llvm::Constant *getCopyStructFn() {
346 CodeGen::CodeGenTypes &Types = CGM.getTypes();
347 ASTContext &Ctx = CGM.getContext();
348 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
349 llvm::SmallVector<CanQualType,5> Params;
350 Params.push_back(Ctx.VoidPtrTy);
351 Params.push_back(Ctx.VoidPtrTy);
352 Params.push_back(Ctx.LongTy);
353 Params.push_back(Ctx.BoolTy);
354 Params.push_back(Ctx.BoolTy);
355 const llvm::FunctionType *FTy =
356 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
357 FunctionType::ExtInfo()),
358 false);
359 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
360 }
361
Chris Lattner72db6c32009-04-22 02:44:54 +0000362 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000363 CodeGen::CodeGenTypes &Types = CGM.getTypes();
364 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000365 // void objc_enumerationMutation (id)
John McCallead608a2010-02-26 00:48:12 +0000366 llvm::SmallVector<CanQualType,1> Params;
367 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000368 const llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000369 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000370 FunctionType::ExtInfo()),
371 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000372 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
373 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000374
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000375 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000376 llvm::Constant *getGcReadWeakFn() {
377 // id objc_read_weak (id *)
378 std::vector<const llvm::Type*> Args;
379 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000380 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000381 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000382 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000383 }
384
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000385 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000386 llvm::Constant *getGcAssignWeakFn() {
387 // id objc_assign_weak (id, id *)
388 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
389 Args.push_back(ObjectPtrTy->getPointerTo());
390 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000391 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000392 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
393 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000394
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000395 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000396 llvm::Constant *getGcAssignGlobalFn() {
397 // id objc_assign_global(id, id *)
398 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
399 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000400 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000401 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000402 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
403 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000404
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000405 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
406 llvm::Constant *getGcAssignThreadLocalFn() {
407 // id objc_assign_threadlocal(id src, id * dest)
408 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
409 Args.push_back(ObjectPtrTy->getPointerTo());
410 llvm::FunctionType *FTy =
411 llvm::FunctionType::get(ObjectPtrTy, Args, false);
412 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
413 }
414
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000415 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000416 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000417 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000418 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
419 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000420 Args.push_back(LongTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000421 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000422 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000423 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
424 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000425
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000426 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
427 llvm::Constant *GcMemmoveCollectableFn() {
428 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
429 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
430 Args.push_back(Int8PtrTy);
431 Args.push_back(LongTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000432 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000433 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
434 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000435
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000436 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000437 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000438 // id objc_assign_strongCast(id, id *)
Chris Lattnerbbccd612009-04-22 02:38:11 +0000439 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
440 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Andersona1cf15f2009-07-14 23:10:40 +0000441 llvm::FunctionType *FTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +0000442 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000443 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
444 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000445
446 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000447 llvm::Constant *getExceptionThrowFn() {
448 // void objc_exception_throw(id)
449 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
450 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000451 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000452 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
453 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000454
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000455 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
456 llvm::Constant *getExceptionRethrowFn() {
457 // void objc_exception_rethrow(void)
458 std::vector<const llvm::Type*> Args;
459 llvm::FunctionType *FTy =
460 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
461 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
462 }
463
Daniel Dunbar1c566672009-02-24 01:43:46 +0000464 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000465 llvm::Constant *getSyncEnterFn() {
466 // void objc_sync_enter (id)
467 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
468 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000469 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000470 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
471 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000472
Daniel Dunbar1c566672009-02-24 01:43:46 +0000473 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000474 llvm::Constant *getSyncExitFn() {
475 // void objc_sync_exit (id)
476 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
477 llvm::FunctionType *FTy =
Owen Anderson0032b272009-08-13 21:57:51 +0000478 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000479 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
480 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000481
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000482 llvm::Constant *getSendFn(bool IsSuper) const {
483 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
484 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000485
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000486 llvm::Constant *getSendFn2(bool IsSuper) const {
487 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
488 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000489
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000490 llvm::Constant *getSendStretFn(bool IsSuper) const {
491 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
492 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000493
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000494 llvm::Constant *getSendStretFn2(bool IsSuper) const {
495 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
496 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000497
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000498 llvm::Constant *getSendFpretFn(bool IsSuper) const {
499 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
500 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000501
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000502 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
503 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
504 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000505
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000506 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
507 ~ObjCCommonTypesHelper(){}
508};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000509
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000510/// ObjCTypesHelper - Helper class that encapsulates lazy
511/// construction of varies types used during ObjC generation.
512class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000513public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000514 /// SymtabTy - LLVM type for struct objc_symtab.
515 const llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000516 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
517 const llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000518 /// ModuleTy - LLVM type for struct objc_module.
519 const llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000520
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000521 /// ProtocolTy - LLVM type for struct objc_protocol.
522 const llvm::StructType *ProtocolTy;
523 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
524 const llvm::Type *ProtocolPtrTy;
525 /// ProtocolExtensionTy - LLVM type for struct
526 /// objc_protocol_extension.
527 const llvm::StructType *ProtocolExtensionTy;
528 /// ProtocolExtensionTy - LLVM type for struct
529 /// objc_protocol_extension *.
530 const llvm::Type *ProtocolExtensionPtrTy;
531 /// MethodDescriptionTy - LLVM type for struct
532 /// objc_method_description.
533 const llvm::StructType *MethodDescriptionTy;
534 /// MethodDescriptionListTy - LLVM type for struct
535 /// objc_method_description_list.
536 const llvm::StructType *MethodDescriptionListTy;
537 /// MethodDescriptionListPtrTy - LLVM type for struct
538 /// objc_method_description_list *.
539 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000540 /// ProtocolListTy - LLVM type for struct objc_property_list.
541 const llvm::Type *ProtocolListTy;
542 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
543 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000544 /// CategoryTy - LLVM type for struct objc_category.
545 const llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000546 /// ClassTy - LLVM type for struct objc_class.
547 const llvm::StructType *ClassTy;
548 /// ClassPtrTy - LLVM type for struct objc_class *.
549 const llvm::Type *ClassPtrTy;
550 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
551 const llvm::StructType *ClassExtensionTy;
552 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
553 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000554 // IvarTy - LLVM type for struct objc_ivar.
555 const llvm::StructType *IvarTy;
556 /// IvarListTy - LLVM type for struct objc_ivar_list.
557 const llvm::Type *IvarListTy;
558 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
559 const llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000560 /// MethodListTy - LLVM type for struct objc_method_list.
561 const llvm::Type *MethodListTy;
562 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
563 const llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000564
Anders Carlsson124526b2008-09-09 10:10:21 +0000565 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
566 const llvm::Type *ExceptionDataTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000567
Anders Carlsson124526b2008-09-09 10:10:21 +0000568 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000569 llvm::Constant *getExceptionTryEnterFn() {
570 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000571 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000572 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000573 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000574 Params, false),
575 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000576 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000577
578 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000579 llvm::Constant *getExceptionTryExitFn() {
580 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000581 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Andersona1cf15f2009-07-14 23:10:40 +0000582 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000583 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000584 Params, false),
585 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000586 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000587
588 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000589 llvm::Constant *getExceptionExtractFn() {
590 std::vector<const llvm::Type*> Params;
Owen Anderson96e0fc72009-07-29 22:16:19 +0000591 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
592 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner34b02a12009-04-22 02:26:14 +0000593 Params, false),
594 "objc_exception_extract");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000595
Chris Lattner34b02a12009-04-22 02:26:14 +0000596 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000597
Anders Carlsson124526b2008-09-09 10:10:21 +0000598 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000599 llvm::Constant *getExceptionMatchFn() {
600 std::vector<const llvm::Type*> Params;
601 Params.push_back(ClassPtrTy);
602 Params.push_back(ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000603 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000604 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000605 Params, false),
606 "objc_exception_match");
607
Chris Lattner34b02a12009-04-22 02:26:14 +0000608 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000609
Anders Carlsson124526b2008-09-09 10:10:21 +0000610 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000611 llvm::Constant *getSetJmpFn() {
612 std::vector<const llvm::Type*> Params;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +0000613 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattner34b02a12009-04-22 02:26:14 +0000614 return
Owen Anderson0032b272009-08-13 21:57:51 +0000615 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattner34b02a12009-04-22 02:26:14 +0000616 Params, false),
617 "_setjmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000618
Chris Lattner34b02a12009-04-22 02:26:14 +0000619 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000620
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000621public:
622 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000623 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000624};
625
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000626/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000627/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000628class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000629public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000630
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000631 // MethodListnfABITy - LLVM for struct _method_list_t
632 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000633
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000634 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
635 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000636
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000637 // ProtocolnfABITy = LLVM for struct _protocol_t
638 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000639
Daniel Dunbar948e2582009-02-15 07:36:20 +0000640 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
641 const llvm::Type *ProtocolnfABIPtrTy;
642
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000643 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
644 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000645
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000646 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
647 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000648
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000649 // ClassnfABITy - LLVM for struct _class_t
650 const llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000651
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000652 // ClassnfABIPtrTy - LLVM for struct _class_t*
653 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000654
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000655 // IvarnfABITy - LLVM for struct _ivar_t
656 const llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000657
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000658 // IvarListnfABITy - LLVM for struct _ivar_list_t
659 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000660
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000661 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
662 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000663
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000664 // ClassRonfABITy - LLVM for struct _class_ro_t
665 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000666
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000667 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
668 const llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000669
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000670 // CategorynfABITy - LLVM for struct _category_t
671 const llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000672
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000673 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000674
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000675 // MessageRefTy - LLVM for:
676 // struct _message_ref_t {
677 // IMP messenger;
678 // SEL name;
679 // };
680 const llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000681 // MessageRefCTy - clang type for struct _message_ref_t
682 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000683
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000684 // MessageRefPtrTy - LLVM for struct _message_ref_t*
685 const llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000686 // MessageRefCPtrTy - clang type for struct _message_ref_t*
687 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000688
Fariborz Jahanianef163782009-02-05 01:13:09 +0000689 // MessengerTy - Type of the messenger (shown as IMP above)
690 const llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000691
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000692 // SuperMessageRefTy - LLVM for:
693 // struct _super_message_ref_t {
694 // SUPER_IMP messenger;
695 // SEL name;
696 // };
697 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000698
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000699 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
700 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000701
Chris Lattner1c02f862009-04-22 02:53:24 +0000702 llvm::Constant *getMessageSendFixupFn() {
703 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
704 std::vector<const llvm::Type*> Params;
705 Params.push_back(ObjectPtrTy);
706 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000707 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000708 Params, true),
709 "objc_msgSend_fixup");
710 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000711
Chris Lattner1c02f862009-04-22 02:53:24 +0000712 llvm::Constant *getMessageSendFpretFixupFn() {
713 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
714 std::vector<const llvm::Type*> Params;
715 Params.push_back(ObjectPtrTy);
716 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000717 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000718 Params, true),
719 "objc_msgSend_fpret_fixup");
720 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000721
Chris Lattner1c02f862009-04-22 02:53:24 +0000722 llvm::Constant *getMessageSendStretFixupFn() {
723 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
724 std::vector<const llvm::Type*> Params;
725 Params.push_back(ObjectPtrTy);
726 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000727 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000728 Params, true),
729 "objc_msgSend_stret_fixup");
730 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000731
Chris Lattner1c02f862009-04-22 02:53:24 +0000732 llvm::Constant *getMessageSendIdFixupFn() {
733 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
734 std::vector<const llvm::Type*> Params;
735 Params.push_back(ObjectPtrTy);
736 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000737 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000738 Params, true),
739 "objc_msgSendId_fixup");
740 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000741
Chris Lattner1c02f862009-04-22 02:53:24 +0000742 llvm::Constant *getMessageSendIdStretFixupFn() {
743 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
744 std::vector<const llvm::Type*> Params;
745 Params.push_back(ObjectPtrTy);
746 Params.push_back(MessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000747 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000748 Params, true),
749 "objc_msgSendId_stret_fixup");
750 }
751 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000752 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000753 // struct _super_message_ref_t*, ...)
754 std::vector<const llvm::Type*> Params;
755 Params.push_back(SuperPtrTy);
756 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000757 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000758 Params, true),
759 "objc_msgSendSuper2_fixup");
760 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000761
Chris Lattner1c02f862009-04-22 02:53:24 +0000762 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000763 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000764 // struct _super_message_ref_t*, ...)
765 std::vector<const llvm::Type*> Params;
766 Params.push_back(SuperPtrTy);
767 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000768 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner1c02f862009-04-22 02:53:24 +0000769 Params, true),
770 "objc_msgSendSuper2_stret_fixup");
771 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000772
773
774
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000775 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
776 /// exception personality function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000777 llvm::Value *getEHPersonalityPtr() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000778 llvm::Constant *Personality =
Owen Anderson0032b272009-08-13 21:57:51 +0000779 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000780 true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000781 "__objc_personality_v0");
Owen Anderson3c4972d2009-07-29 18:54:39 +0000782 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000783 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000784
Chris Lattner8a569112009-04-22 02:15:23 +0000785 llvm::Constant *getUnwindResumeOrRethrowFn() {
786 std::vector<const llvm::Type*> Params;
787 Params.push_back(Int8PtrTy);
Owen Andersona1cf15f2009-07-14 23:10:40 +0000788 return CGM.CreateRuntimeFunction(
Owen Anderson0032b272009-08-13 21:57:51 +0000789 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000790 Params, false),
Daniel Dunbarb2987d12010-02-10 18:49:11 +0000791 (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" :
792 "_Unwind_Resume_or_Rethrow"));
Chris Lattner8a569112009-04-22 02:15:23 +0000793 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000794
Chris Lattner8a569112009-04-22 02:15:23 +0000795 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson0032b272009-08-13 21:57:51 +0000796 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattnerb59761b2009-07-01 04:13:52 +0000797 false),
Chris Lattner8a569112009-04-22 02:15:23 +0000798 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000799
Chris Lattner8a569112009-04-22 02:15:23 +0000800 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000801
Chris Lattner8a569112009-04-22 02:15:23 +0000802 llvm::Constant *getObjCBeginCatchFn() {
803 std::vector<const llvm::Type*> Params;
804 Params.push_back(Int8PtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +0000805 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattner8a569112009-04-22 02:15:23 +0000806 Params, false),
807 "objc_begin_catch");
808 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000809
810 const llvm::StructType *EHTypeTy;
811 const llvm::Type *EHTypePtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000812
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000813 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
814 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000815};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000816
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000817class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000818public:
819 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000820 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000821 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000822 unsigned ivar_bytepos;
823 unsigned ivar_size;
824 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000825 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000826
827 // Allow sorting based on byte pos.
828 bool operator<(const GC_IVAR &b) const {
829 return ivar_bytepos < b.ivar_bytepos;
830 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000831 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000832
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000833 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000834 public:
835 unsigned skip;
836 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000837 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000838 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000839 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000840
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000841protected:
842 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000843 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000844 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000845 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000846
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000847 // gc ivar layout bitmap calculation helper caches.
848 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
849 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000850
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000851 /// LazySymbols - Symbols to generate a lazy reference for. See
852 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000853 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000854
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000855 /// DefinedSymbols - External symbols which are defined by this
856 /// module. The symbols in this list and LazySymbols are used to add
857 /// special linker symbols which ensure that Objective-C modules are
858 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000859 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000860
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000861 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000862 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000863
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000864 /// MethodVarNames - uniqued method variable names.
865 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000866
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000867 /// DefinedCategoryNames - list of category names in form Class_Category.
868 llvm::SetVector<std::string> DefinedCategoryNames;
869
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000870 /// MethodVarTypes - uniqued method type signatures. We have to use
871 /// a StringMap here because have no other unique reference.
872 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000873
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000874 /// MethodDefinitions - map of methods which have been defined in
875 /// this translation unit.
876 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000877
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000878 /// PropertyNames - uniqued method variable names.
879 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000880
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000881 /// ClassReferences - uniqued class references.
882 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000883
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000884 /// SelectorReferences - uniqued selector references.
885 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000886
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000887 /// Protocols - Protocols for which an objc_protocol structure has
888 /// been emitted. Forward declarations are handled by creating an
889 /// empty structure whose initializer is filled in when/if defined.
890 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000891
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000892 /// DefinedProtocols - Protocols which have actually been
893 /// defined. We should not need this, see FIXME in GenerateProtocol.
894 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000895
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000896 /// DefinedClasses - List of defined classes.
897 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000898
899 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
900 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000901
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000902 /// DefinedCategories - List of defined categories.
903 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000904
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000905 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
906 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000907
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000908 /// GetNameForMethod - Return a name for the given method.
909 /// \param[out] NameOut - The return value.
910 void GetNameForMethod(const ObjCMethodDecl *OMD,
911 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +0000912 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000913
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000914 /// GetMethodVarName - Return a unique constant for the given
915 /// selector's name. The return value has type char *.
916 llvm::Constant *GetMethodVarName(Selector Sel);
917 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
918 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000919
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000920 /// GetMethodVarType - Return a unique constant for the given
921 /// selector's name. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000922
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000923 // FIXME: This is a horrible name.
924 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000925 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000926
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000927 /// GetPropertyName - Return a unique constant for the given
928 /// name. The return value has type char *.
929 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000930
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000931 // FIXME: This can be dropped once string functions are unified.
932 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
933 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000934
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000935 /// GetClassName - Return a unique constant for the given selector's
936 /// name. The return value has type char *.
937 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000938
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000939 /// BuildIvarLayout - Builds ivar layout bitmap for the class
940 /// implementation for the __strong or __weak case.
941 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000942 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
943 bool ForStrongLayout);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000944
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000945 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000946 unsigned int BytePos, bool ForStrongLayout,
947 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000948 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000949 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000950 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +0000951 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000952 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000953 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000954
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000955 /// GetIvarLayoutName - Returns a unique constant for the given
956 /// ivar layout bitmap.
957 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
958 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000959
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000960 /// EmitPropertyList - Emit the given property list. The return
961 /// value has type PropertyListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000962 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000963 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000964 const ObjCContainerDecl *OCD,
965 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000966
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000967 /// PushProtocolProperties - Push protocol's property on the input stack.
968 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
969 std::vector<llvm::Constant*> &Properties,
970 const Decl *Container,
971 const ObjCProtocolDecl *PROTO,
972 const ObjCCommonTypesHelper &ObjCTypes);
973
Fariborz Jahanianda320092009-01-29 19:24:30 +0000974 /// GetProtocolRef - Return a reference to the internal protocol
975 /// description, creating an empty one if it has not been
976 /// defined. The return value has type ProtocolPtrTy.
977 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000978
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000979 /// CreateMetadataVar - Create a global variable with internal
980 /// linkage for use by the Objective-C runtime.
981 ///
982 /// This is a convenience wrapper which not only creates the
983 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000984 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000985 ///
986 /// \param Name - The variable name.
987 /// \param Init - The variable initializer; this is also used to
988 /// define the type of the variable.
989 /// \param Section - The section the variable should go into, or 0.
990 /// \param Align - The alignment for the variable, or 0.
991 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000992 /// "llvm.used".
Daniel Dunbar9c29bf52009-10-18 20:48:59 +0000993 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000994 llvm::Constant *Init,
995 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000996 unsigned Align,
997 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000998
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000999 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001000 ReturnValueSlot Return,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001001 QualType ResultType,
1002 llvm::Value *Sel,
1003 llvm::Value *Arg0,
1004 QualType Arg0Ty,
1005 bool IsSuper,
1006 const CallArgList &CallArgs,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001007 const ObjCMethodDecl *OMD,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001008 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00001009
Daniel Dunbarfce176b2010-04-25 20:39:01 +00001010 /// EmitImageInfo - Emit the image info marker used to encode some module
1011 /// level information.
1012 void EmitImageInfo();
1013
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001014public:
Owen Anderson69243822009-07-13 04:10:07 +00001015 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +00001016 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001017
David Chisnall0d13f6f2010-01-23 02:40:42 +00001018 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001019
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001020 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1021 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001022
Fariborz Jahanianda320092009-01-29 19:24:30 +00001023 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001024
Fariborz Jahanianda320092009-01-29 19:24:30 +00001025 /// GetOrEmitProtocol - Get the protocol object for the given
1026 /// declaration, emitting it if necessary. The return value has type
1027 /// ProtocolPtrTy.
1028 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001029
Fariborz Jahanianda320092009-01-29 19:24:30 +00001030 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1031 /// object for the given declaration, emitting it if needed. These
1032 /// forward references will be filled in with empty bodies if no
1033 /// definition is seen. The return value has type ProtocolPtrTy.
1034 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001035};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001036
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001037class CGObjCMac : public CGObjCCommonMac {
1038private:
1039 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001040
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001041 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001042 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001043 void EmitModuleInfo();
1044
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001045 /// EmitModuleSymols - Emit module symbols, the list of defined
1046 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00001047 llvm::Constant *EmitModuleSymbols();
1048
Daniel Dunbarf77ac862008-08-11 21:35:06 +00001049 /// FinishModule - Write out global data structures at the end of
1050 /// processing a translation unit.
1051 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001052
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001053 /// EmitClassExtension - Generate the class extension structure used
1054 /// to store the weak ivar layout and properties. The return value
1055 /// has type ClassExtensionPtrTy.
1056 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1057
1058 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1059 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001060 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001061 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001062
1063 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1064 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001065
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001066 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001067 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001068 QualType ResultType,
1069 Selector Sel,
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001070 llvm::Value *Arg0,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001071 QualType Arg0Ty,
1072 bool IsSuper,
1073 const CallArgList &CallArgs);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001074
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001075 /// EmitIvarList - Emit the ivar list for the given
1076 /// implementation. If ForClass is true the list of class ivars
1077 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1078 /// interface ivars will be emitted. The return value has type
1079 /// IvarListPtrTy.
1080 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00001081 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001082
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001083 /// EmitMetaClass - Emit a forward reference to the class structure
1084 /// for the metaclass of the given interface. The return value has
1085 /// type ClassPtrTy.
1086 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1087
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001088 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001089 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001090 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1091 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001092 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001093
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001094 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001095
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001096 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001097
1098 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +00001099 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001100 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00001101 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001102 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001103
1104 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001105 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001106 /// - TypeName: The name for the type containing the methods.
1107 /// - IsProtocol: True iff these methods are for a protocol.
1108 /// - ClassMethds: True iff these are class methods.
1109 /// - Required: When true, only "required" methods are
1110 /// listed. Similarly, when false only "optional" methods are
1111 /// listed. For classes this should always be true.
1112 /// - begin, end: The method list to output.
1113 ///
1114 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001115 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001116 const char *Section,
1117 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001118
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001119 /// GetOrEmitProtocol - Get the protocol object for the given
1120 /// declaration, emitting it if necessary. The return value has type
1121 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001122 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001123
1124 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1125 /// object for the given declaration, emitting it if needed. These
1126 /// forward references will be filled in with empty bodies if no
1127 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001128 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001129
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001130 /// EmitProtocolExtension - Generate the protocol extension
1131 /// structure used to store optional instance and class methods, and
1132 /// protocol properties. The return value has type
1133 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001134 llvm::Constant *
1135 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1136 const ConstantVector &OptInstanceMethods,
1137 const ConstantVector &OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001138
1139 /// EmitProtocolList - Generate the list of referenced
1140 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001141 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001142 ObjCProtocolDecl::protocol_iterator begin,
1143 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001144
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001145 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1146 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001147 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1148 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001149
1150public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001151 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001152
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001153 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001154
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001155 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001156 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001157 QualType ResultType,
1158 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001159 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001160 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001161 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001162 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001163
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001164 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001165 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001166 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001167 QualType ResultType,
1168 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001169 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001170 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001171 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001172 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001173 const CallArgList &CallArgs,
1174 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001175
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001176 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001177 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001178
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001179 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1180 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001181
1182 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1183 /// untyped one.
1184 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1185 const ObjCMethodDecl *Method);
1186
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001187 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001188
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001189 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001190
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001191 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001192 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001193
Chris Lattner74391b42009-03-22 21:03:39 +00001194 virtual llvm::Constant *GetPropertyGetFunction();
1195 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001196 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001197 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001198
John McCallf1549f62010-07-06 01:34:17 +00001199 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1200 const ObjCAtTryStmt &S);
1201 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1202 const ObjCAtSynchronizedStmt &S);
1203 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001204 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1205 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001206 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001207 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001208 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001209 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001210 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001211 llvm::Value *src, llvm::Value *dest,
1212 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001213 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001214 llvm::Value *src, llvm::Value *dest,
1215 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001216 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1217 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001218 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1219 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001220 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001221
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001222 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1223 QualType ObjectTy,
1224 llvm::Value *BaseValue,
1225 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001226 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001227 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001228 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001229 const ObjCIvarDecl *Ivar);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001230};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001231
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001232class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001233private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001234 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001235 llvm::GlobalVariable* ObjCEmptyCacheVar;
1236 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001237
Daniel Dunbar11394522009-04-18 08:51:00 +00001238 /// SuperClassReferences - uniqued super class references.
1239 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001240
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001241 /// MetaClassReferences - uniqued meta class references.
1242 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001243
1244 /// EHTypeReferences - uniqued class ehtype references.
1245 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001246
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001247 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001248 /// legacy messaging dispatch.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001249 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001250
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001251 /// DefinedMetaClasses - List of defined meta-classes.
1252 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1253
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001254 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001255 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001256 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001257
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001258 /// FinishNonFragileABIModule - Write out global data structures at the end of
1259 /// processing a translation unit.
1260 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001261
Daniel Dunbar463b8762009-05-15 21:48:48 +00001262 /// AddModuleClassList - Add the given list of class pointers to the
1263 /// module with the provided symbol and section names.
1264 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1265 const char *SymbolName,
1266 const char *SectionName);
1267
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001268 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1269 unsigned InstanceStart,
1270 unsigned InstanceSize,
1271 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001272 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001273 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001274 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001275 llvm::Constant *ClassRoGV,
1276 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001277
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001278 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001279
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001280 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001281
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001282 /// EmitMethodList - Emit the method list for the given
1283 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001284 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001285 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001286 const ConstantVector &Methods);
1287 /// EmitIvarList - Emit the ivar list for the given
1288 /// implementation. If ForClass is true the list of class ivars
1289 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1290 /// interface ivars will be emitted. The return value has type
1291 /// IvarListnfABIPtrTy.
1292 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001293
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001294 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001295 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001296 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001297
Fariborz Jahanianda320092009-01-29 19:24:30 +00001298 /// GetOrEmitProtocol - Get the protocol object for the given
1299 /// declaration, emitting it if necessary. The return value has type
1300 /// ProtocolPtrTy.
1301 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001302
Fariborz Jahanianda320092009-01-29 19:24:30 +00001303 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1304 /// object for the given declaration, emitting it if needed. These
1305 /// forward references will be filled in with empty bodies if no
1306 /// definition is seen. The return value has type ProtocolPtrTy.
1307 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001308
Fariborz Jahanianda320092009-01-29 19:24:30 +00001309 /// EmitProtocolList - Generate the list of referenced
1310 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001311 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001312 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001313 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001314
Fariborz Jahanian46551122009-02-04 00:22:57 +00001315 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001316 ReturnValueSlot Return,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001317 QualType ResultType,
1318 Selector Sel,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00001319 llvm::Value *Receiver,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001320 QualType Arg0Ty,
1321 bool IsSuper,
1322 const CallArgList &CallArgs);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001323
1324 /// GetClassGlobal - Return the global variable for the Objective-C
1325 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001326 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001327
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001328 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001329 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001330 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001331 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001332
Daniel Dunbar11394522009-04-18 08:51:00 +00001333 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1334 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001335 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1336 const ObjCInterfaceDecl *ID);
1337
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001338 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1339 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001340 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001341 const ObjCInterfaceDecl *ID);
1342
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001343 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1344 /// the given ivar.
1345 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001346 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001347 const ObjCInterfaceDecl *ID,
1348 const ObjCIvarDecl *Ivar);
1349
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001350 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1351 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001352 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1353 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001354
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001355 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001356 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001357 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1358 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001359
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001360 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001361 return "OBJC_METACLASS_$_";
1362 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001363
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001364 const char *getClassSymbolPrefix() const {
1365 return "OBJC_CLASS_$_";
1366 }
1367
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001368 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001369 uint32_t &InstanceStart,
1370 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001371
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001372 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001373 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001374 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1375 return CGM.getContext().Selectors.getSelector(0, &II);
1376 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001377
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001378 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001379 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1380 return CGM.getContext().Selectors.getSelector(1, &II);
1381 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001382
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001383 /// ImplementationIsNonLazy - Check whether the given category or
1384 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001385 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001386
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001387public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001388 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001389 // FIXME. All stubs for now!
1390 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001391
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001392 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001393 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001394 QualType ResultType,
1395 Selector Sel,
1396 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001397 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001398 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001399 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001400
1401 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001402 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001403 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001404 QualType ResultType,
1405 Selector Sel,
1406 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001407 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001408 llvm::Value *Receiver,
1409 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001410 const CallArgList &CallArgs,
1411 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001412
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001413 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001414 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001415
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001416 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1417 bool lvalue = false)
1418 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001419
1420 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1421 /// untyped one.
1422 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1423 const ObjCMethodDecl *Method)
1424 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001425
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001426 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001427
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001428 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001429 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001430 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001431
1432 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001433 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001434 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001435 virtual llvm::Constant *GetPropertySetFunction() {
1436 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001437 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001438
1439 virtual llvm::Constant *GetCopyStructFunction() {
1440 return ObjCTypes.getCopyStructFn();
1441 }
1442
Chris Lattner74391b42009-03-22 21:03:39 +00001443 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001444 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001445 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001446
John McCallf1549f62010-07-06 01:34:17 +00001447 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1448 const ObjCAtTryStmt &S);
1449 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1450 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001451 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001452 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001453 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001454 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001455 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001456 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001457 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001458 llvm::Value *src, llvm::Value *dest,
1459 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001460 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001461 llvm::Value *src, llvm::Value *dest,
1462 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001463 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001464 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001465 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1466 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001467 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001468 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1469 QualType ObjectTy,
1470 llvm::Value *BaseValue,
1471 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001472 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001473 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001474 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001475 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001476};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001477
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001478} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001479
1480/* *** Helper Functions *** */
1481
1482/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001483static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001484 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001485 unsigned idx0,
1486 unsigned idx1) {
1487 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001488 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1489 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001490 };
Owen Anderson3c4972d2009-07-29 18:54:39 +00001491 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001492}
1493
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001494/// hasObjCExceptionAttribute - Return true if this class or any super
1495/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001496static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001497 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001498 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001499 return true;
1500 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001501 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001502 return false;
1503}
1504
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001505/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001506
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001507CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001508 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001509 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001510 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001511}
1512
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001513/// GetClass - Return a reference to the class for the given interface
1514/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001515llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001516 const ObjCInterfaceDecl *ID) {
1517 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001518}
1519
1520/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001521llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1522 bool lval) {
1523 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001524}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001525llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001526 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001527 return EmitSelector(Builder, Method->getSelector());
1528}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001529
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001530/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001531/*
1532 struct __builtin_CFString {
1533 const int *isa; // point to __CFConstantStringClassReference
1534 int flags;
1535 const char *str;
1536 long length;
1537 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001538*/
1539
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001540/// or Generate a constant NSString object.
1541/*
1542 struct __builtin_NSString {
1543 const int *isa; // point to __NSConstantStringClassReference
1544 const char *str;
1545 unsigned int length;
1546 };
1547*/
1548
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001549llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001550 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001551 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1552 CGM.GetAddrOfConstantCFString(SL) :
1553 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001554}
1555
1556/// Generates a message send where the super is the receiver. This is
1557/// a message send to self with special delivery semantics indicating
1558/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001559CodeGen::RValue
1560CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001561 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001562 QualType ResultType,
1563 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001564 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001565 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001566 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001567 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001568 const CodeGen::CallArgList &CallArgs,
1569 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001570 // Create and init a super structure; this is a (receiver, class)
1571 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001572 llvm::Value *ObjCSuper =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001573 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001574 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001575 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001576 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001577 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001578
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001579 // If this is a class message the metaclass is passed as the target.
1580 llvm::Value *Target;
1581 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001582 if (isCategoryImpl) {
1583 // Message sent to 'super' in a class method defined in a category
1584 // implementation requires an odd treatment.
1585 // If we are in a class method, we must retrieve the
1586 // _metaclass_ for the current class, pointed at by
1587 // the class's "isa" pointer. The following assumes that
1588 // isa" is the first ivar in a class (which it must be).
1589 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1590 Target = CGF.Builder.CreateStructGEP(Target, 0);
1591 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001592 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001593 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1594 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1595 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1596 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001597 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001598 }
1599 else if (isCategoryImpl)
1600 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1601 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001602 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1603 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1604 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001605 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001606 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1607 // ObjCTypes types.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001608 const llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001609 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001610 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001611 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001612 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCallef072fd2010-05-22 01:48:05 +00001613 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001614 EmitSelector(CGF.Builder, Sel),
1615 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001616 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001617}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001618
1619/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001620CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001621 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001622 QualType ResultType,
1623 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001624 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001625 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001626 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001627 const ObjCMethodDecl *Method) {
John McCallef072fd2010-05-22 01:48:05 +00001628 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001629 EmitSelector(CGF.Builder, Sel),
1630 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001631 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001632}
1633
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001634CodeGen::RValue
1635CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001636 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001637 QualType ResultType,
1638 llvm::Value *Sel,
1639 llvm::Value *Arg0,
1640 QualType Arg0Ty,
1641 bool IsSuper,
1642 const CallArgList &CallArgs,
1643 const ObjCMethodDecl *Method,
1644 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001645 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001646 if (!IsSuper)
1647 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar46f45b92008-09-09 01:06:48 +00001648 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001649 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001650 CGF.getContext().getObjCSelType()));
1651 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001652
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001653 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001654 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001655 FunctionType::ExtInfo());
Daniel Dunbar67939662009-09-17 04:01:40 +00001656 const llvm::FunctionType *FTy =
1657 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001658
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001659 if (Method)
1660 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1661 CGM.getContext().getCanonicalType(ResultType) &&
1662 "Result type mismatch!");
1663
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001664 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001665 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001666 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001667 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001668 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1669 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1670 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001671 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001672 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001673 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001674 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001675 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00001676 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001677}
1678
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001679llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001680 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001681 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001682 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001683 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1684
Owen Anderson3c4972d2009-07-29 18:54:39 +00001685 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001686 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001687}
1688
Fariborz Jahanianda320092009-01-29 19:24:30 +00001689void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001690 // FIXME: We shouldn't need this, the protocol decl should contain enough
1691 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001692 DefinedProtocols.insert(PD->getIdentifier());
1693
1694 // If we have generated a forward reference to this protocol, emit
1695 // it now. Otherwise do nothing, the protocol objects are lazily
1696 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001697 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001698 GetOrEmitProtocol(PD);
1699}
1700
Fariborz Jahanianda320092009-01-29 19:24:30 +00001701llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001702 if (DefinedProtocols.count(PD->getIdentifier()))
1703 return GetOrEmitProtocol(PD);
1704 return GetOrEmitProtocolRef(PD);
1705}
1706
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001707/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001708// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1709struct _objc_protocol {
1710struct _objc_protocol_extension *isa;
1711char *protocol_name;
1712struct _objc_protocol_list *protocol_list;
1713struct _objc__method_prototype_list *instance_methods;
1714struct _objc__method_prototype_list *class_methods
1715};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001716
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001717See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001718*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001719llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1720 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1721
1722 // Early exit if a defining object has already been generated.
1723 if (Entry && Entry->hasInitializer())
1724 return Entry;
1725
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001726 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001727 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001728 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1729
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001730 // Construct method lists.
1731 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1732 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001733 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001734 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001735 ObjCMethodDecl *MD = *i;
1736 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1737 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1738 OptInstanceMethods.push_back(C);
1739 } else {
1740 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001741 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001742 }
1743
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001744 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001745 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001746 ObjCMethodDecl *MD = *i;
1747 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1748 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1749 OptClassMethods.push_back(C);
1750 } else {
1751 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001752 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001753 }
1754
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001755 std::vector<llvm::Constant*> Values(5);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001756 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001757 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001758 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001759 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00001760 PD->protocol_begin(),
1761 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001762 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001763 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001764 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1765 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001766 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001767 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001768 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1769 ClassMethods);
Owen Anderson08e25242009-07-27 22:29:56 +00001770 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001771 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001772
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001773 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001774 // Already created, fix the linkage and update the initializer.
1775 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001776 Entry->setInitializer(Init);
1777 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001778 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001779 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001780 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001781 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001782 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001783 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001784 // FIXME: Is this necessary? Why only for protocol?
1785 Entry->setAlignment(4);
1786 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001787 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001788
1789 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001790}
1791
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001792llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001793 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1794
1795 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001796 // We use the initializer as a marker of whether this is a forward
1797 // reference or not. At module finalization we add the empty
1798 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001799 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001800 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001801 llvm::GlobalValue::ExternalLinkage,
1802 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001803 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001804 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001805 // FIXME: Is this necessary? Why only for protocol?
1806 Entry->setAlignment(4);
1807 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001808
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001809 return Entry;
1810}
1811
1812/*
1813 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001814 uint32_t size;
1815 struct objc_method_description_list *optional_instance_methods;
1816 struct objc_method_description_list *optional_class_methods;
1817 struct objc_property_list *instance_properties;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001818 };
1819*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001820llvm::Constant *
1821CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1822 const ConstantVector &OptInstanceMethods,
1823 const ConstantVector &OptClassMethods) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001824 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001825 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001826 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001827 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001828 Values[1] =
1829 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001830 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001831 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1832 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001833 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001834 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001835 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1836 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001837 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00001838 0, PD, ObjCTypes);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001839
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001840 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001841 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001842 Values[3]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001843 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001844
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001845 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001846 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001847
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001848 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001849 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001850 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001851 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001852}
1853
1854/*
1855 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001856 struct objc_protocol_list *next;
1857 long count;
1858 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001859 };
1860*/
Daniel Dunbardbc933702008-08-21 21:57:41 +00001861llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001862CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardbc933702008-08-21 21:57:41 +00001863 ObjCProtocolDecl::protocol_iterator begin,
1864 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001865 std::vector<llvm::Constant*> ProtocolRefs;
1866
Daniel Dunbardbc933702008-08-21 21:57:41 +00001867 for (; begin != end; ++begin)
1868 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001869
1870 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001871 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001872 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001873
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001874 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001875 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001876
1877 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001878 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001879 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001880 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001881 ProtocolRefs.size() - 1);
1882 Values[2] =
1883 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1884 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001885 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001886
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001887 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001888 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001889 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001890 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001891 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001892}
1893
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001894void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1895 std::vector<llvm::Constant*> &Properties,
1896 const Decl *Container,
1897 const ObjCProtocolDecl *PROTO,
1898 const ObjCCommonTypesHelper &ObjCTypes) {
1899 std::vector<llvm::Constant*> Prop(2);
1900 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1901 E = PROTO->protocol_end(); P != E; ++P)
1902 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1903 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1904 E = PROTO->prop_end(); I != E; ++I) {
1905 const ObjCPropertyDecl *PD = *I;
1906 if (!PropertySet.insert(PD->getIdentifier()))
1907 continue;
1908 Prop[0] = GetPropertyName(PD->getIdentifier());
1909 Prop[1] = GetPropertyTypeString(PD, Container);
1910 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1911 }
1912}
1913
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001914/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001915 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001916 const char * const name;
1917 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001918 };
1919
1920 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001921 uint32_t entsize; // sizeof (struct _objc_property)
1922 uint32_t prop_count;
1923 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001924 };
1925*/
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001926llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001927 const Decl *Container,
1928 const ObjCContainerDecl *OCD,
1929 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001930 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001931 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001932 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1933 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00001934 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001935 PropertySet.insert(PD->getIdentifier());
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001936 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001937 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson08e25242009-07-27 22:29:56 +00001938 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001939 Prop));
1940 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001941 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001942 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
1943 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00001944 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1945 ObjCTypes);
1946 }
1947 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1948 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1949 E = CD->protocol_end(); P != E; ++P)
1950 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1951 ObjCTypes);
1952 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001953
1954 // Return null for empty list.
1955 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001956 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001957
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001958 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00001959 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001960 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001961 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1962 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001963 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001964 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00001965 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00001966 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001967
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001968 llvm::GlobalVariable *GV =
1969 CreateMetadataVar(Name, Init,
1970 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001971 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001972 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00001973 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001974 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001975}
1976
1977/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001978 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001979 int count;
1980 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001981 };
1982*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001983llvm::Constant *
1984CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1985 std::vector<llvm::Constant*> Desc(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00001986 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001987 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1988 ObjCTypes.SelectorPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001989 Desc[1] = GetMethodVarType(MD);
Owen Anderson08e25242009-07-27 22:29:56 +00001990 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001991 Desc);
1992}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001993
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001994llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001995 const char *Section,
1996 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001997 // Return null for empty list.
1998 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001999 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002000
2001 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002002 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002003 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002004 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002005 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002006 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002007
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002008 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002009 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002010 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002011}
2012
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002013/*
2014 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002015 char *category_name;
2016 char *class_name;
2017 struct _objc_method_list *instance_methods;
2018 struct _objc_method_list *class_methods;
2019 struct _objc_protocol_list *protocols;
2020 uint32_t size; // <rdar://4585769>
2021 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002022 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002023*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002024void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002025 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002026
Mike Stumpf5408fe2009-05-16 07:57:57 +00002027 // FIXME: This is poor design, the OCD should have a pointer to the category
2028 // decl. Additionally, note that Category can be null for the @implementation
2029 // w/o an @interface case. Sema should just create one for us as it does for
2030 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002031 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002032 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002033 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002034
2035 llvm::SmallString<256> ExtName;
2036 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2037 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002038
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002039 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002040 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002041 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002042 // Instance methods should always be defined.
2043 InstanceMethods.push_back(GetMethodConstant(*i));
2044 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002045 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002046 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002047 // Class methods should always be defined.
2048 ClassMethods.push_back(GetMethodConstant(*i));
2049 }
2050
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002051 std::vector<llvm::Constant*> Values(7);
2052 Values[0] = GetClassName(OCD->getIdentifier());
2053 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002054 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002055 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002056 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002057 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002058 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002059 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002060 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002061 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002062 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002063 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002064 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002065 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002066 Category->protocol_begin(),
2067 Category->protocol_end());
2068 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002069 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002070 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002071 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002072
2073 // If there is no category @interface then there can be no properties.
2074 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002075 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002076 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002077 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002078 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002079 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002080
Owen Anderson08e25242009-07-27 22:29:56 +00002081 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002082 Values);
2083
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002084 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002085 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002086 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002087 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002088 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002089 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002090}
2091
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002092// FIXME: Get from somewhere?
2093enum ClassFlags {
2094 eClassFlags_Factory = 0x00001,
2095 eClassFlags_Meta = 0x00002,
2096 // <rdr://5142207>
2097 eClassFlags_HasCXXStructors = 0x02000,
2098 eClassFlags_Hidden = 0x20000,
2099 eClassFlags_ABI2_Hidden = 0x00010,
2100 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2101};
2102
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002103/*
2104 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002105 Class isa;
2106 Class super_class;
2107 const char *name;
2108 long version;
2109 long info;
2110 long instance_size;
2111 struct _objc_ivar_list *ivars;
2112 struct _objc_method_list *methods;
2113 struct _objc_cache *cache;
2114 struct _objc_protocol_list *protocols;
2115 // Objective-C 1.0 extensions (<rdr://4585769>)
2116 const char *ivar_layout;
2117 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002118 };
2119
2120 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002121*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002122void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002123 DefinedSymbols.insert(ID->getIdentifier());
2124
Chris Lattner8ec03f52008-11-24 03:54:41 +00002125 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002126 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002127 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002128 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002129 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002130 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardbc933702008-08-21 21:57:41 +00002131 Interface->protocol_begin(),
2132 Interface->protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002133 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002134 if (ID->getNumIvarInitializers())
2135 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002136 unsigned Size =
Daniel Dunbar2bebbf02009-05-03 10:46:44 +00002137 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002138
2139 // FIXME: Set CXX-structors flag.
Daniel Dunbar04d40782009-04-14 06:00:08 +00002140 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002141 Flags |= eClassFlags_Hidden;
2142
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002143 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002144 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002145 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002146 // Instance methods should always be defined.
2147 InstanceMethods.push_back(GetMethodConstant(*i));
2148 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002149 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002150 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002151 // Class methods should always be defined.
2152 ClassMethods.push_back(GetMethodConstant(*i));
2153 }
2154
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002155 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002156 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002157 ObjCPropertyImplDecl *PID = *i;
2158
2159 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2160 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2161
2162 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2163 if (llvm::Constant *C = GetMethodConstant(MD))
2164 InstanceMethods.push_back(C);
2165 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2166 if (llvm::Constant *C = GetMethodConstant(MD))
2167 InstanceMethods.push_back(C);
2168 }
2169 }
2170
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002171 std::vector<llvm::Constant*> Values(12);
Daniel Dunbar5384b092009-05-03 08:56:52 +00002172 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002173 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002174 // Record a reference to the super class.
2175 LazySymbols.insert(Super->getIdentifier());
2176
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002177 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002178 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002179 ObjCTypes.ClassPtrTy);
2180 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002181 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002182 }
2183 Values[ 2] = GetClassName(ID->getIdentifier());
2184 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002185 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2186 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2187 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002188 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002189 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002190 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002191 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002192 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002193 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002194 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002195 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002196 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002197 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002198 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002199 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002200 std::string Name("\01L_OBJC_CLASS_");
2201 Name += ClassName;
2202 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2203 // Check for a forward reference.
2204 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2205 if (GV) {
2206 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2207 "Forward metaclass reference has incorrect type.");
2208 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2209 GV->setInitializer(Init);
2210 GV->setSection(Section);
2211 GV->setAlignment(4);
2212 CGM.AddUsedGlobal(GV);
2213 }
2214 else
2215 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002216 DefinedClasses.push_back(GV);
2217}
2218
2219llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2220 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002221 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002222 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002223 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002224
Daniel Dunbar04d40782009-04-14 06:00:08 +00002225 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002226 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002227
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002228 std::vector<llvm::Constant*> Values(12);
2229 // The isa for the metaclass is the root of the hierarchy.
2230 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2231 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2232 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002233 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002234 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002235 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002236 // The super class for the metaclass is emitted as the name of the
2237 // super class. The runtime fixes this up to point to the
2238 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002239 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002240 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002241 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002242 ObjCTypes.ClassPtrTy);
2243 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002244 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002245 }
2246 Values[ 2] = GetClassName(ID->getIdentifier());
2247 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002248 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2249 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2250 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002251 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002252 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002253 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002254 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002255 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002256 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002257 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002258 Values[ 9] = Protocols;
2259 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002260 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002261 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002262 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
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);
2265
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002266 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002267 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002268
2269 // Check for a forward reference.
2270 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2271 if (GV) {
2272 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2273 "Forward metaclass reference has incorrect type.");
2274 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2275 GV->setInitializer(Init);
2276 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002277 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002278 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002279 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002280 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002281 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002282 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002283 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002284
2285 return GV;
2286}
2287
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002288llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002289 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002290
Mike Stumpf5408fe2009-05-16 07:57:57 +00002291 // FIXME: Should we look these up somewhere other than the module. Its a bit
2292 // silly since we only generate these while processing an implementation, so
2293 // exactly one pointer would work if know when we entered/exitted an
2294 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002295
2296 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002297 // Previously, metaclass with internal linkage may have been defined.
2298 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002299 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2300 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002301 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2302 "Forward metaclass reference has incorrect type.");
2303 return GV;
2304 } else {
2305 // Generate as an external reference to keep a consistent
2306 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002307 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002308 llvm::GlobalValue::ExternalLinkage,
2309 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002310 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002311 }
2312}
2313
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002314llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2315 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2316
2317 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2318 true)) {
2319 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2320 "Forward class metadata reference has incorrect type.");
2321 return GV;
2322 } else {
2323 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2324 llvm::GlobalValue::ExternalLinkage,
2325 0,
2326 Name);
2327 }
2328}
2329
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002330/*
2331 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002332 uint32_t size;
2333 const char *weak_ivar_layout;
2334 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002335 };
2336*/
2337llvm::Constant *
2338CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002339 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002340 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002341
2342 std::vector<llvm::Constant*> Values(3);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002343 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002344 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002345 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002346 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002347
2348 // Return null if no extension bits are used.
2349 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002350 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002351
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002352 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002353 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002354 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002355 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002356 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002357}
2358
2359/*
2360 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002361 char *ivar_name;
2362 char *ivar_type;
2363 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002364 };
2365
2366 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002367 int ivar_count;
2368 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002369 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002370*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002371llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002372 bool ForClass) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002373 std::vector<llvm::Constant*> Ivars, Ivar(3);
2374
2375 // When emitting the root class GCC emits ivar entries for the
2376 // actual class structure. It is not clear if we need to follow this
2377 // behavior; for now lets try and get away with not doing it. If so,
2378 // the cleanest solution would be to make up an ObjCInterfaceDecl
2379 // for the class.
2380 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002381 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002382
2383 ObjCInterfaceDecl *OID =
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002384 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002385
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002386 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002387 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002388
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00002389 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2390 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002391 // Ignore unnamed bit-fields.
2392 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002393 continue;
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00002394 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2395 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002396 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar97776872009-04-22 07:32:20 +00002397 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson08e25242009-07-27 22:29:56 +00002398 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002399 }
2400
2401 // Return null for empty list.
2402 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002403 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002404
2405 std::vector<llvm::Constant*> Values(2);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002406 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002407 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002408 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002409 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002410 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002411
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002412 llvm::GlobalVariable *GV;
2413 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002414 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002415 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002416 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002417 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002418 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002419 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002420 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002421 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002422}
2423
2424/*
2425 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002426 SEL method_name;
2427 char *method_types;
2428 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002429 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002430
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002431 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002432 struct objc_method_list *obsolete;
2433 int count;
2434 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002435 };
2436*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002437
2438/// GetMethodConstant - Return a struct objc_method constant for the
2439/// given method if it has been defined. The result is null if the
2440/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002441llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002442 // FIXME: Use DenseMap::lookup
2443 llvm::Function *Fn = MethodDefinitions[MD];
2444 if (!Fn)
2445 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002446
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002447 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002448 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002449 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002450 ObjCTypes.SelectorPtrTy);
2451 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002452 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002453 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002454}
2455
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002456llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002457 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002458 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002459 // Return null for empty list.
2460 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002461 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002462
2463 std::vector<llvm::Constant*> Values(3);
Owen Andersonc9c88b42009-07-31 20:28:54 +00002464 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002465 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002466 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002467 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002468 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00002469 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002470
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002471 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002472 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002473 ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002474}
2475
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002476llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002477 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002478 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002479 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002480
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002481 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002482 const llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002483 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002484 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002485 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002486 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002487 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002488 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002489 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002490
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002491 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002492}
2493
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002494llvm::GlobalVariable *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002495CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002496 llvm::Constant *Init,
2497 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002498 unsigned Align,
2499 bool AddToUsed) {
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002500 const llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002501 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002502 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002503 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002504 if (Section)
2505 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002506 if (Align)
2507 GV->setAlignment(Align);
2508 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002509 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002510 return GV;
2511}
2512
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002513llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002514 // Abuse this interface function as a place to finalize.
2515 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002516 return NULL;
2517}
2518
Chris Lattner74391b42009-03-22 21:03:39 +00002519llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002520 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002521}
2522
Chris Lattner74391b42009-03-22 21:03:39 +00002523llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002524 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002525}
2526
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002527llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2528 return ObjCTypes.getCopyStructFn();
2529}
2530
Chris Lattner74391b42009-03-22 21:03:39 +00002531llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002532 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002533}
2534
John McCallf1549f62010-07-06 01:34:17 +00002535void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2536 return EmitTryOrSynchronizedStmt(CGF, S);
2537}
2538
2539void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2540 const ObjCAtSynchronizedStmt &S) {
2541 return EmitTryOrSynchronizedStmt(CGF, S);
2542}
2543
John McCallcc505292010-07-21 06:59:36 +00002544namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002545 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002546 const Stmt &S;
2547 llvm::Value *SyncArg;
2548 llvm::Value *CallTryExitVar;
2549 llvm::Value *ExceptionData;
2550 ObjCTypesHelper &ObjCTypes;
2551 PerformFragileFinally(const Stmt *S,
2552 llvm::Value *SyncArg,
2553 llvm::Value *CallTryExitVar,
2554 llvm::Value *ExceptionData,
2555 ObjCTypesHelper *ObjCTypes)
2556 : S(*S), SyncArg(SyncArg), CallTryExitVar(CallTryExitVar),
2557 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2558
2559 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2560 // Check whether we need to call objc_exception_try_exit.
2561 // In optimized code, this branch will always be folded.
2562 llvm::BasicBlock *FinallyCallExit =
2563 CGF.createBasicBlock("finally.call_exit");
2564 llvm::BasicBlock *FinallyNoCallExit =
2565 CGF.createBasicBlock("finally.no_call_exit");
2566 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2567 FinallyCallExit, FinallyNoCallExit);
2568
2569 CGF.EmitBlock(FinallyCallExit);
2570 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2571 ->setDoesNotThrow();
2572
2573 CGF.EmitBlock(FinallyNoCallExit);
2574
2575 if (isa<ObjCAtTryStmt>(S)) {
2576 if (const ObjCAtFinallyStmt* FinallyStmt =
2577 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2578 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2579
2580 // Currently, the end of the cleanup must always exist.
2581 CGF.EnsureInsertPoint();
2582 } else {
2583 // Emit objc_sync_exit(expr); as finally's sole statement for
2584 // @synchronized.
2585 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2586 ->setDoesNotThrow();
2587 }
2588 }
2589 };
2590}
2591
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002592/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002593
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002594 Objective-C setjmp-longjmp (sjlj) Exception Handling
2595 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002596
John McCallf1549f62010-07-06 01:34:17 +00002597 A catch buffer is a setjmp buffer plus:
2598 - a pointer to the exception that was caught
2599 - a pointer to the previous exception data buffer
2600 - two pointers of reserved storage
2601 Therefore catch buffers form a stack, with a pointer to the top
2602 of the stack kept in thread-local storage.
2603
2604 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2605 objc_exception_try_exit pops the given catch buffer, which is
2606 required to be the top of the EH stack.
2607 objc_exception_throw pops the top of the EH stack, writes the
2608 thrown exception into the appropriate field, and longjmps
2609 to the setjmp buffer. It crashes the process (with a printf
2610 and an abort()) if there are no catch buffers on the stack.
2611 objc_exception_extract just reads the exception pointer out of the
2612 catch buffer.
2613
2614 There's no reason an implementation couldn't use a light-weight
2615 setjmp here --- something like __builtin_setjmp, but API-compatible
2616 with the heavyweight setjmp. This will be more important if we ever
2617 want to implement correct ObjC/C++ exception interactions for the
2618 fragile ABI.
2619
2620 Note that for this use of setjmp/longjmp to be correct, we may need
2621 to mark some local variables volatile: if a non-volatile local
2622 variable is modified between the setjmp and the longjmp, it has
2623 indeterminate value. For the purposes of LLVM IR, it may be
2624 sufficient to make loads and stores within the @try (to variables
2625 declared outside the @try) volatile. This is necessary for
2626 optimized correctness, but is not currently being done; this is
2627 being tracked as rdar://problem/8160285
2628
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002629 The basic framework for a @try-catch-finally is as follows:
2630 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002631 objc_exception_data d;
2632 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002633 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002634
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002635 objc_exception_try_enter(&d);
2636 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002637 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002638 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002639 // exception path
2640 id _caught = objc_exception_extract(&d);
2641
2642 // enter new try scope for handlers
2643 if (!setjmp(d.jmp_buf)) {
2644 ... match exception and execute catch blocks ...
2645
2646 // fell off end, rethrow.
2647 _rethrow = _caught;
2648 ... jump-through-finally to finally_rethrow ...
2649 } else {
2650 // exception in catch block
2651 _rethrow = objc_exception_extract(&d);
2652 _call_try_exit = false;
2653 ... jump-through-finally to finally_rethrow ...
2654 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002655 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002656 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002657
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002658 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002659 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002660 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002661
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002662 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002663 ... dispatch to finally destination ...
2664
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002665 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002666 objc_exception_throw(_rethrow);
2667
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002668 finally_end:
2669 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002670
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002671 This framework differs slightly from the one gcc uses, in that gcc
2672 uses _rethrow to determine if objc_exception_try_exit should be called
2673 and if the object should be rethrown. This breaks in the face of
2674 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002675
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002676 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002677
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002678 - If there are no catch blocks, then we avoid emitting the second
2679 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002680
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002681 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2682 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002683
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002684 - FIXME: If there is no @finally block we can do a few more
2685 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002686
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002687 Rethrows and Jumps-Through-Finally
2688 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002689
John McCallf1549f62010-07-06 01:34:17 +00002690 '@throw;' is supported by pushing the currently-caught exception
2691 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002692
John McCallf1549f62010-07-06 01:34:17 +00002693 Branches through the @finally block are handled with an ordinary
2694 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2695 exceptions are not compatible with C++ exceptions, and this is
2696 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002697
John McCallf1549f62010-07-06 01:34:17 +00002698 @synchronized(expr) { stmt; } is emitted as if it were:
2699 id synch_value = expr;
2700 objc_sync_enter(synch_value);
2701 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002702*/
2703
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002704void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2705 const Stmt &S) {
2706 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002707
2708 // A destination for the fall-through edges of the catch handlers to
2709 // jump to.
2710 CodeGenFunction::JumpDest FinallyEnd =
2711 CGF.getJumpDestInCurrentScope("finally.end");
2712
2713 // A destination for the rethrow edge of the catch handlers to jump
2714 // to.
2715 CodeGenFunction::JumpDest FinallyRethrow =
2716 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002717
Daniel Dunbar1c566672009-02-24 01:43:46 +00002718 // For @synchronized, call objc_sync_enter(sync.expr). The
2719 // evaluation of the expression must occur before we enter the
2720 // @synchronized. We can safely avoid a temp here because jumps into
2721 // @synchronized are illegal & this will dominate uses.
2722 llvm::Value *SyncArg = 0;
2723 if (!isTry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002724 SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002725 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2726 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002727 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2728 ->setDoesNotThrow();
Daniel Dunbar1c566672009-02-24 01:43:46 +00002729 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002730
Daniel Dunbar898d5082008-09-30 01:06:03 +00002731 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson80f25672008-09-09 17:59:25 +00002732 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2733 "exceptiondata.ptr");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002734 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00002735 "_rethrow");
John McCallf1549f62010-07-06 01:34:17 +00002736
2737 // Create a flag indicating whether the cleanup needs to call
2738 // objc_exception_try_exit. This is true except when
2739 // - no catches match and we're branching through the cleanup
2740 // just to rethrow the exception, or
2741 // - a catch matched and we're falling out of the catch handler.
2742 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00002743 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002744 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext),
John McCallf1549f62010-07-06 01:34:17 +00002745 CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002746
John McCallf1549f62010-07-06 01:34:17 +00002747 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00002748 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
2749 SyncArg,
2750 CallTryExitVar,
2751 ExceptionData,
2752 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00002753
2754 // Enter a try block:
2755 // - Call objc_exception_try_enter to push ExceptionData on top of
2756 // the EH stack.
2757 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2758 ->setDoesNotThrow();
2759
2760 // - Call setjmp on the exception data buffer.
2761 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2762 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2763 llvm::Value *SetJmpBuffer =
2764 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2765 llvm::CallInst *SetJmpResult =
2766 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2767 SetJmpResult->setDoesNotThrow();
2768
2769 // If setjmp returned 0, enter the protected block; otherwise,
2770 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00002771 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2772 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00002773 llvm::Value *DidCatch =
2774 CGF.Builder.CreateIsNull(SetJmpResult, "did_catch_exception");
2775 CGF.Builder.CreateCondBr(DidCatch, TryBlock, TryHandler);
Anders Carlsson80f25672008-09-09 17:59:25 +00002776
John McCallf1549f62010-07-06 01:34:17 +00002777 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00002778 CGF.EmitBlock(TryBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002779 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00002780 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002781 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002782
John McCallf1549f62010-07-06 01:34:17 +00002783 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002784 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002785
2786 // Retrieve the exception object. We may emit multiple blocks but
2787 // nothing can cross this so the value is already in SSA form.
John McCallf1549f62010-07-06 01:34:17 +00002788 llvm::CallInst *Caught =
Chris Lattner34b02a12009-04-22 02:26:14 +00002789 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2790 ExceptionData, "caught");
John McCallf1549f62010-07-06 01:34:17 +00002791 Caught->setDoesNotThrow();
2792
2793 // Remember the exception to rethrow.
2794 CGF.Builder.CreateStore(Caught, RethrowPtr);
2795
2796 // Note: at this point, objc_exception_throw already popped the
2797 // catch handler, so anything that branches to the cleanup needs
2798 // to set CallTryExitVar to false.
2799
2800 // For a @synchronized (or a @try with no catches), just branch
2801 // through the cleanup to the rethrow block.
2802 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2803 // Tell the cleanup not to re-pop the exit.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002804 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
John McCallf1549f62010-07-06 01:34:17 +00002805 CallTryExitVar);
2806
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002807 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00002808
2809 // Otherwise, we have to match against the caught exceptions.
2810 } else {
2811 // Push the exception to rethrow onto the EH value stack for the
2812 // benefit of any @throws in the handlers.
2813 CGF.ObjCEHValueStack.push_back(Caught);
2814
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002815 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
2816
Daniel Dunbar55e40722008-09-27 07:03:52 +00002817 // Enter a new exception try block (in case a @catch block throws
John McCallf1549f62010-07-06 01:34:17 +00002818 // an exception). Now CallTryExitVar (currently true) is back in
2819 // synch with reality.
2820 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2821 ->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002822
John McCallf1549f62010-07-06 01:34:17 +00002823 llvm::CallInst *SetJmpResult =
2824 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2825 "setjmp.result");
2826 SetJmpResult->setDoesNotThrow();
2827
2828 llvm::Value *Threw =
2829 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
Anders Carlsson80f25672008-09-09 17:59:25 +00002830
Daniel Dunbar55e87422008-11-11 02:29:29 +00002831 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
John McCallf1549f62010-07-06 01:34:17 +00002832 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch_for_catch");
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002833 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002834
Anders Carlsson80f25672008-09-09 17:59:25 +00002835 CGF.EmitBlock(CatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002836
Daniel Dunbar55e40722008-09-27 07:03:52 +00002837 // Handle catch list. As a special case we check if everything is
2838 // matched and avoid generating code for falling off the end if
2839 // so.
2840 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00002841 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
2842 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00002843
Douglas Gregorc00d8e12010-04-26 16:46:50 +00002844 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00002845 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00002846
Anders Carlsson80f25672008-09-09 17:59:25 +00002847 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00002848 if (!CatchParam) {
2849 AllMatched = true;
2850 } else {
John McCall183700f2009-09-21 23:43:11 +00002851 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002852
John McCallf1549f62010-07-06 01:34:17 +00002853 // catch(id e) always matches under this ABI, since only
2854 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00002855 // FIXME: For the time being we also match id<X>; this should
2856 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00002857 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00002858 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00002859 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002860
John McCallf1549f62010-07-06 01:34:17 +00002861 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002862 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00002863 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
2864
Anders Carlssondde0a942008-09-11 09:15:33 +00002865 if (CatchParam) {
Steve Naroff7ba138a2009-03-03 19:52:17 +00002866 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002867 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00002868
2869 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00002870 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00002871 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002872
Anders Carlssondde0a942008-09-11 09:15:33 +00002873 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00002874
2875 // The scope of the catch variable ends right here.
2876 CatchVarCleanups.ForceCleanup();
2877
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002878 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00002879 break;
2880 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002881
Steve Naroff14108da2009-07-10 23:34:53 +00002882 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00002883 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00002884
2885 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00002886 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
2887 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00002888
2889 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00002890 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002891
John McCallf1549f62010-07-06 01:34:17 +00002892 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00002893 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2894 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00002895 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002896
John McCallf1549f62010-07-06 01:34:17 +00002897 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
2898 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002899
2900 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00002901 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002902
Anders Carlsson80f25672008-09-09 17:59:25 +00002903 // Emit the @catch block.
2904 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00002905
2906 // Collect any cleanups for the catch variable. The scope lasts until
2907 // the end of the catch body.
2908 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
2909
Steve Naroff7ba138a2009-03-03 19:52:17 +00002910 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00002911 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002912
John McCallf1549f62010-07-06 01:34:17 +00002913 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002914 llvm::Value *Tmp =
2915 CGF.Builder.CreateBitCast(Caught,
2916 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002917 "tmp");
Steve Naroff7ba138a2009-03-03 19:52:17 +00002918 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002919
Anders Carlssondde0a942008-09-11 09:15:33 +00002920 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00002921
2922 // We're done with the catch variable.
2923 CatchVarCleanups.ForceCleanup();
2924
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002925 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002926
Anders Carlsson80f25672008-09-09 17:59:25 +00002927 CGF.EmitBlock(NextCatchBlock);
2928 }
2929
John McCallf1549f62010-07-06 01:34:17 +00002930 CGF.ObjCEHValueStack.pop_back();
2931
Daniel Dunbar55e40722008-09-27 07:03:52 +00002932 if (!AllMatched) {
2933 // None of the handlers caught the exception, so store it to be
2934 // rethrown at the end of the @finally block.
2935 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002936 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00002937 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002938
Daniel Dunbar55e40722008-09-27 07:03:52 +00002939 // Emit the exception handler for the @catch blocks.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002940 CGF.EmitBlock(CatchHandler);
John McCallf1549f62010-07-06 01:34:17 +00002941
2942 // Rethrow the new exception, not the old one.
2943 Caught = CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2944 ExceptionData);
2945 Caught->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00002946 CGF.Builder.CreateStore(Caught, RethrowPtr);
John McCallf1549f62010-07-06 01:34:17 +00002947
2948 // Don't pop the catch handler; the throw already did.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002949 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
John McCallf1549f62010-07-06 01:34:17 +00002950 CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002951 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson80f25672008-09-09 17:59:25 +00002952 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002953
John McCallf1549f62010-07-06 01:34:17 +00002954 // Pop the cleanup.
2955 CGF.PopCleanupBlock();
John McCallff8e1152010-07-23 21:56:41 +00002956 CGF.EmitBlock(FinallyEnd.getBlock());
Anders Carlssonf3a79a92009-02-09 20:38:58 +00002957
John McCallf1549f62010-07-06 01:34:17 +00002958 // Emit the rethrow block.
2959 CGF.Builder.ClearInsertionPoint();
John McCallff8e1152010-07-23 21:56:41 +00002960 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00002961 if (CGF.HaveInsertPoint()) {
2962 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
2963 CGF.Builder.CreateLoad(RethrowPtr))
2964 ->setDoesNotThrow();
2965 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00002966 }
Anders Carlsson80f25672008-09-09 17:59:25 +00002967
John McCallff8e1152010-07-23 21:56:41 +00002968 CGF.Builder.SetInsertPoint(FinallyEnd.getBlock());
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002969}
2970
2971void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00002972 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002973 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002974
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002975 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2976 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002977 ExceptionAsObject =
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002978 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2979 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002980 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002981 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00002982 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00002983 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002984
John McCallf1549f62010-07-06 01:34:17 +00002985 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
2986 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00002987 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00002988
2989 // Clear the insertion point to indicate we are in unreachable code.
2990 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00002991}
2992
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002993/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00002994/// object: objc_read_weak (id *src)
2995///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00002996llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00002997 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00002998 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002999 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3000 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3001 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003002 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003003 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003004 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003005 return read_weak;
3006}
3007
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003008/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3009/// objc_assign_weak (id src, id *dst)
3010///
3011void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003012 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003013 const llvm::Type * SrcTy = src->getType();
3014 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003015 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003016 assert(Size <= 8 && "does not support size > 8");
3017 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003018 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003019 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3020 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003021 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3022 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003023 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003024 src, dst, "weakassign");
3025 return;
3026}
3027
Fariborz Jahanian58626502008-11-19 00:59:10 +00003028/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3029/// objc_assign_global (id src, id *dst)
3030///
3031void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003032 llvm::Value *src, llvm::Value *dst,
3033 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003034 const llvm::Type * SrcTy = src->getType();
3035 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003036 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003037 assert(Size <= 8 && "does not support size > 8");
3038 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003039 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003040 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3041 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003042 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3043 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003044 if (!threadlocal)
3045 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3046 src, dst, "globalassign");
3047 else
3048 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3049 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003050 return;
3051}
3052
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003053/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003054/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003055///
3056void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003057 llvm::Value *src, llvm::Value *dst,
3058 llvm::Value *ivarOffset) {
3059 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003060 const llvm::Type * SrcTy = src->getType();
3061 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003062 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003063 assert(Size <= 8 && "does not support size > 8");
3064 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003065 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003066 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3067 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003068 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3069 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003070 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3071 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003072 return;
3073}
3074
Fariborz Jahanian58626502008-11-19 00:59:10 +00003075/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3076/// objc_assign_strongCast (id src, id *dst)
3077///
3078void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003079 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003080 const llvm::Type * SrcTy = src->getType();
3081 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003082 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003083 assert(Size <= 8 && "does not support size > 8");
3084 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003085 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003086 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3087 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003088 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3089 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003090 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003091 src, dst, "weakassign");
3092 return;
3093}
3094
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003095void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003096 llvm::Value *DestPtr,
3097 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003098 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003099 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3100 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003101 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003102 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003103 return;
3104}
3105
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003106/// EmitObjCValueForIvar - Code Gen for ivar reference.
3107///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003108LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3109 QualType ObjectTy,
3110 llvm::Value *BaseValue,
3111 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003112 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003113 const ObjCInterfaceDecl *ID =
3114 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003115 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3116 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003117}
3118
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003119llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003120 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003121 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003122 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003123 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003124 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3125 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003126}
3127
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003128/* *** Private Interface *** */
3129
3130/// EmitImageInfo - Emit the image info marker used to encode some module
3131/// level information.
3132///
3133/// See: <rdr://4810609&4810587&4810587>
3134/// struct IMAGE_INFO {
3135/// unsigned version;
3136/// unsigned flags;
3137/// };
3138enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003139 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003140 eImageInfo_GarbageCollected = (1 << 1),
3141 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003142 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3143
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003144 // A flag indicating that the module has no instances of a @synthesize of a
3145 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003146 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003147};
3148
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003149void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003150 unsigned version = 0; // Version is unused?
3151 unsigned flags = 0;
3152
3153 // FIXME: Fix and continue?
3154 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3155 flags |= eImageInfo_GarbageCollected;
3156 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3157 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003158
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003159 // We never allow @synthesize of a superclass property.
3160 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003161
Chris Lattner77b89b82010-06-27 07:15:29 +00003162 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3163
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003164 // Emitted as int[2];
3165 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003166 llvm::ConstantInt::get(Int32Ty, version),
3167 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003168 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003169 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003170
3171 const char *Section;
3172 if (ObjCABI == 1)
3173 Section = "__OBJC, __image_info,regular";
3174 else
3175 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003176 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003177 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson7db6d832009-07-28 18:33:04 +00003178 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003179 Section,
3180 0,
3181 true);
3182 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003183}
3184
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003185
3186// struct objc_module {
3187// unsigned long version;
3188// unsigned long size;
3189// const char *name;
3190// Symtab symtab;
3191// };
3192
3193// FIXME: Get from somewhere
3194static const int ModuleVersion = 7;
3195
3196void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003197 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003198
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003199 std::vector<llvm::Constant*> Values(4);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003200 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3201 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00003202 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003203 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003204 Values[3] = EmitModuleSymbols();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003205 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003206 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003207 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003208 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003209}
3210
3211llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003212 unsigned NumClasses = DefinedClasses.size();
3213 unsigned NumCategories = DefinedCategories.size();
3214
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003215 // Return null if no symbols were defined.
3216 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003217 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003218
3219 std::vector<llvm::Constant*> Values(5);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003220 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003221 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003222 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3223 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003224
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003225 // The runtime expects exactly the list of defined classes followed
3226 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003227 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003228 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003229 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003230 ObjCTypes.Int8PtrTy);
3231 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003232 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003233 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003234 ObjCTypes.Int8PtrTy);
3235
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003236 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003237 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003238 NumClasses + NumCategories),
3239 Symbols);
3240
Nick Lewycky0d36dd22009-09-19 20:00:52 +00003241 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003242
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003243 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003244 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3245 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003246 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003247 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003248}
3249
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003250llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003251 const ObjCInterfaceDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003252 LazySymbols.insert(ID->getIdentifier());
3253
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003254 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003255
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003256 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003257 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003258 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003259 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003260 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003261 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3262 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003263 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003264 }
3265
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003266 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003267}
3268
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003269llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3270 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003271 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003272
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003273 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003274 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003275 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003276 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003277 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003278 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3279 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003280 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003281 }
3282
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003283 if (lvalue)
3284 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00003285 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003286}
3287
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003288llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003289 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003290
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003291 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003292 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003293 llvm::ConstantArray::get(VMContext,
3294 Ident->getNameStart()),
Jim Grosbach35eee092010-07-20 16:20:26 +00003295 ((ObjCABI == 2) ?
3296 "__TEXT,__objc_classname,cstring_literals" :
3297 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003298 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003299
Owen Andersona1cf15f2009-07-14 23:10:40 +00003300 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003301}
3302
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003303/// GetIvarLayoutName - Returns a unique constant for the given
3304/// ivar layout bitmap.
3305llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003306 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003307 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003308}
3309
John McCall0953e762009-09-24 19:53:00 +00003310static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003311 if (FQT.isObjCGCStrong())
John McCall0953e762009-09-24 19:53:00 +00003312 return Qualifiers::Strong;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003313
3314 if (FQT.isObjCGCWeak())
John McCall0953e762009-09-24 19:53:00 +00003315 return Qualifiers::Weak;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003316
Fariborz Jahanian039e6a12009-09-11 17:39:05 +00003317 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
John McCall0953e762009-09-24 19:53:00 +00003318 return Qualifiers::Strong;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003319
Ted Kremenek6217b802009-07-29 21:53:49 +00003320 if (const PointerType *PT = FQT->getAs<PointerType>())
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003321 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003322
John McCall0953e762009-09-24 19:53:00 +00003323 return Qualifiers::GCNone;
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003324}
3325
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003326void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003327 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003328 bool ForStrongLayout,
3329 bool &HasUnion) {
3330 const RecordDecl *RD = RT->getDecl();
3331 // FIXME - Use iterator.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003332 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003333 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003334 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003335 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003336
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003337 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3338 ForStrongLayout, HasUnion);
3339}
3340
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003341void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003342 const llvm::StructLayout *Layout,
3343 const RecordDecl *RD,
Chris Lattnerf1690852009-03-31 08:48:01 +00003344 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003345 unsigned int BytePos, bool ForStrongLayout,
3346 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003347 bool IsUnion = (RD && RD->isUnion());
3348 uint64_t MaxUnionIvarSize = 0;
3349 uint64_t MaxSkippedUnionIvarSize = 0;
3350 FieldDecl *MaxField = 0;
3351 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003352 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003353 uint64_t MaxFieldOffset = 0;
3354 uint64_t MaxSkippedFieldOffset = 0;
3355 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003356
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003357 if (RecFields.empty())
3358 return;
Chris Lattnerf1690852009-03-31 08:48:01 +00003359 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3360 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3361
Chris Lattnerf1690852009-03-31 08:48:01 +00003362 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003363 FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003364 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003365 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003366 // Note that 'i' here is actually the field index inside RD of Field,
3367 // although this dependency is hidden.
3368 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3369 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003370 } else
Daniel Dunbare05cc982009-05-03 23:35:23 +00003371 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003372
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003373 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003374 if (!Field->getIdentifier() || Field->isBitField()) {
3375 LastFieldBitfield = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003376 LastBitfieldOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003377 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003378 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003379
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003380 LastFieldBitfield = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003381 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003382 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003383 if (FQT->isUnionType())
3384 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003385
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003386 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003387 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003388 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003389 continue;
3390 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003391
Chris Lattnerf1690852009-03-31 08:48:01 +00003392 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003393 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003394 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003395 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003396 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003397 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003398 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3399 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003400 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003401 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003402 FQT = CArray->getElementType();
3403 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003404
3405 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003406 "layout for array of unions not supported");
3407 if (FQT->isRecordType()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003408 int OldIndex = IvarsInfo.size() - 1;
3409 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003410
Ted Kremenek6217b802009-07-29 21:53:49 +00003411 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003412 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003413 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003414
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003415 // Replicate layout information for each array element. Note that
3416 // one element is already done.
3417 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003418 for (int FirstIndex = IvarsInfo.size() - 1,
3419 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003420 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003421 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3422 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3423 IvarsInfo[i].ivar_size));
3424 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3425 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3426 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003427 }
3428 continue;
3429 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003430 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003431 // At this point, we are done with Record/Union and array there of.
3432 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003433 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003434
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003435 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003436 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3437 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003438 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003439 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003440 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003441 MaxUnionIvarSize = UnionIvarSize;
3442 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003443 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003444 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003445 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003446 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003447 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003448 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003449 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003450 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3451 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003452 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003453 // FIXME: Why the asymmetry? We divide by word size in bits on other
3454 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003455 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003456 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003457 MaxSkippedUnionIvarSize = UnionIvarSize;
3458 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003459 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003460 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003461 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003462 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003463 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003464 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003465 }
3466 }
3467 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003468
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003469 if (LastFieldBitfield) {
3470 // Last field was a bitfield. Must update skip info.
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003471 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3472 uint64_t BitFieldSize =
Eli Friedman9a901bb2009-04-26 19:19:15 +00003473 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar487993b2009-05-03 13:32:01 +00003474 GC_IVAR skivar;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003475 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003476 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3477 + ((BitFieldSize % ByteSizeInBits) != 0);
3478 SkipIvars.push_back(skivar);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003479 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003480
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003481 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003482 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003483 MaxUnionIvarSize));
3484 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003485 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003486 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003487}
3488
3489/// BuildIvarLayout - Builds ivar layout bitmap for the class
3490/// implementation for the __strong or __weak case.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003491/// The layout map displays which words in ivar list must be skipped
3492/// and which must be scanned by GC (see below). String is built of bytes.
3493/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003494/// of words to skip and right nibble is count of words to scan. So, each
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003495/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003496/// represented by a 0x00 byte which also ends the string.
3497/// 1. when ForStrongLayout is true, following ivars are scanned:
3498/// - id, Class
3499/// - object *
3500/// - __strong anything
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003501///
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003502/// 2. When ForStrongLayout is false, following ivars are scanned:
3503/// - __weak anything
3504///
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003505llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003506 const ObjCImplementationDecl *OMD,
3507 bool ForStrongLayout) {
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003508 bool hasUnion = false;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003509
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003510 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003511 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003512 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003513 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003514
Chris Lattnerf1690852009-03-31 08:48:01 +00003515 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003516 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003517 CGM.getContext().CollectObjCIvars(OI, RecFields);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003518
Daniel Dunbar37153282009-05-04 04:10:48 +00003519 // Add this implementations synthesized ivars.
Fariborz Jahanian98200742009-05-12 18:14:29 +00003520 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanian11062e12010-02-19 00:31:17 +00003521 CGM.getContext().CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian98200742009-05-12 18:14:29 +00003522 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3523 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003524
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003525 if (RecFields.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003526 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003527
3528 SkipIvars.clear();
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003529 IvarsInfo.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003530
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003531 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003532 if (IvarsInfo.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00003533 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003534
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003535 // Sort on byte position in case we encounterred a union nested in
3536 // the ivar list.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003537 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003538 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003539 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar0941b492009-04-23 01:29:05 +00003540 std::sort(SkipIvars.begin(), SkipIvars.end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003541
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003542 // Build the string of skip/scan nibbles
Fariborz Jahanian8c2f2d12009-04-24 17:15:27 +00003543 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003544 unsigned int WordSize =
Duncan Sands9408c452009-05-09 07:08:47 +00003545 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003546 if (IvarsInfo[0].ivar_bytepos == 0) {
3547 WordsToSkip = 0;
3548 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003549 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003550 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3551 WordsToScan = IvarsInfo[0].ivar_size;
3552 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003553 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003554 unsigned int TailPrevGCObjC =
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003555 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003556 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003557 // consecutive 'scanned' object pointers.
3558 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003559 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003560 // Skip over 'gc'able object pointer which lay over each other.
3561 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3562 continue;
3563 // Must skip over 1 or more words. We save current skip/scan values
3564 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003565 SKIP_SCAN SkScan;
3566 SkScan.skip = WordsToSkip;
3567 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003568 SkipScanIvars.push_back(SkScan);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003569
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003570 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003571 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3572 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003573 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003574 WordsToSkip = 0;
3575 WordsToScan = IvarsInfo[i].ivar_size;
3576 }
3577 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003578 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003579 SKIP_SCAN SkScan;
3580 SkScan.skip = WordsToSkip;
3581 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003582 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003583 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003584
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003585 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003586 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003587 int LastByteSkipped =
3588 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003589 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003590 int LastByteScanned =
3591 IvarsInfo[LastIndex].ivar_bytepos +
3592 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003593 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003594 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003595 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003596 SKIP_SCAN SkScan;
3597 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3598 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003599 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003600 }
3601 }
3602 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3603 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003604 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003605 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003606 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3607 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3608 // 0xM0 followed by 0x0N detected.
3609 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3610 for (int j = i+1; j < SkipScan; j++)
3611 SkipScanIvars[j] = SkipScanIvars[j+1];
3612 --SkipScan;
3613 }
3614 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003615
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003616 // Generate the string.
3617 std::string BitMap;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003618 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003619 unsigned char byte;
3620 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3621 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3622 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3623 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3624
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003625 // first skip big.
3626 for (unsigned int ix = 0; ix < skip_big; ix++)
3627 BitMap += (unsigned char)(0xf0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003628
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003629 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003630 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003631 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003632 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003633 byte |= 0xf;
3634 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003635 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003636 byte |= scan_small;
3637 scan_small = 0;
3638 }
3639 BitMap += byte;
3640 }
3641 // next scan big
3642 for (unsigned int ix = 0; ix < scan_big; ix++)
3643 BitMap += (unsigned char)(0x0f);
3644 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003645 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003646 byte = scan_small;
3647 BitMap += byte;
3648 }
3649 }
3650 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003651 unsigned char zero = 0;
3652 BitMap += zero;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003653
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003654 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003655 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00003656 ForStrongLayout ? "strong" : "weak",
3657 OMD->getClassInterface()->getNameAsCString());
3658 const unsigned char *s = (unsigned char*)BitMap.c_str();
3659 for (unsigned i = 0; i < BitMap.size(); i++)
3660 if (!(s[i] & 0xf0))
3661 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3662 else
3663 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3664 printf("\n");
3665 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003666 llvm::GlobalVariable * Entry =
3667 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003668 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Jim Grosbach35eee092010-07-20 16:20:26 +00003669 ((ObjCABI == 2) ?
3670 "__TEXT,__objc_classname,cstring_literals" :
3671 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003672 1, true);
3673 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003674}
3675
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003676llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003677 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3678
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003679 // FIXME: Avoid std::string copying.
3680 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003681 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00003682 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Jim Grosbach35eee092010-07-20 16:20:26 +00003683 ((ObjCABI == 2) ?
3684 "__TEXT,__objc_methname,cstring_literals" :
3685 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003686 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003687
Owen Andersona1cf15f2009-07-14 23:10:40 +00003688 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003689}
3690
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003691// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003692llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003693 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3694}
3695
3696// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003697llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003698 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3699}
3700
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00003701llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00003702 std::string TypeStr;
3703 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3704
3705 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003706
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003707 if (!Entry)
3708 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003709 llvm::ConstantArray::get(VMContext, TypeStr),
Jim Grosbach35eee092010-07-20 16:20:26 +00003710 ((ObjCABI == 2) ?
3711 "__TEXT,__objc_methtype,cstring_literals" :
3712 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003713 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003714
Owen Andersona1cf15f2009-07-14 23:10:40 +00003715 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003716}
3717
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003718llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003719 std::string TypeStr;
Daniel Dunbarc45ef602008-08-26 21:51:14 +00003720 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3721 TypeStr);
Devang Patel7794bb82009-03-04 18:21:39 +00003722
3723 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3724
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003725 if (!Entry)
3726 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00003727 llvm::ConstantArray::get(VMContext, TypeStr),
Jim Grosbach35eee092010-07-20 16:20:26 +00003728 ((ObjCABI == 2) ?
3729 "__TEXT,__objc_methtype,cstring_literals" :
3730 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003731 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00003732
Owen Andersona1cf15f2009-07-14 23:10:40 +00003733 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003734}
3735
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003736// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003737llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003738 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003739
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003740 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003741 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003742 llvm::ConstantArray::get(VMContext,
3743 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003744 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003745 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003746
Owen Andersona1cf15f2009-07-14 23:10:40 +00003747 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003748}
3749
3750// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003751// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003752llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003753CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3754 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00003755 std::string TypeStr;
3756 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00003757 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3758}
3759
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003760void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00003761 const ObjCContainerDecl *CD,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003762 llvm::SmallVectorImpl<char> &Name) {
3763 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00003764 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003765 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3766 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003767 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003768 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00003769 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00003770 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00003771}
3772
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003773void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003774 EmitModuleInfo();
3775
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003776 // Emit the dummy bodies for any protocols which were referenced but
3777 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003778 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00003779 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3780 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003781 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003782
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003783 std::vector<llvm::Constant*> Values(5);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003784 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003785 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003786 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003787 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00003788 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00003789 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00003790 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003791 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00003792 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003793 }
3794
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003795 // Add assembler directives to add lazy undefined symbol references
3796 // for classes which are referenced but not defined. This is
3797 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00003798 //
3799 // FIXME: It would be nice if we had an LLVM construct for this.
3800 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
3801 llvm::SmallString<256> Asm;
3802 Asm += CGM.getModule().getModuleInlineAsm();
3803 if (!Asm.empty() && Asm.back() != '\n')
3804 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003805
Daniel Dunbar33063492009-09-07 00:20:42 +00003806 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00003807 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3808 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00003809 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3810 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00003811 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00003812 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00003813 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00003814 }
3815
3816 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
3817 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
3818 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
3819 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00003820
Daniel Dunbar33063492009-09-07 00:20:42 +00003821 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003822 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003823}
3824
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003825CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003826 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00003827 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00003828 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003829 ObjCABI = 2;
3830}
3831
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003832/* *** */
3833
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003834ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00003835 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003836 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3837 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003838
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003839 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003840 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003841 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003842 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00003843 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003844
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003845 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003846 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003847 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003848
Mike Stumpf5408fe2009-05-16 07:57:57 +00003849 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3850 // comes out a bit cleaner.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003851 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00003852 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003853
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003854 // I'm not sure I like this. The implicit coordination is a bit
3855 // gross. We should solve this in a reasonable fashion because this
3856 // is a pretty common task (match some runtime data structure with
3857 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003858
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003859 // FIXME: This is leaked.
3860 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003861
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003862 // struct _objc_super {
3863 // id self;
3864 // Class cls;
3865 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00003866 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00003867 Ctx.getTranslationUnitDecl(),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003868 SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003869 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003870 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00003871 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003872 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00003873 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00003874 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003875
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003876 SuperCTy = Ctx.getTagDeclType(RD);
3877 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003878
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003879 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003880 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3881
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003882 // struct _prop_t {
3883 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003884 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003885 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003886 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003887 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003888 PropertyTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003889
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003890 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003891 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003892 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003893 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003894 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003895 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003896 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003897 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003898 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003899 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003900 PropertyListTy);
3901 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003902 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003903
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003904 // struct _objc_method {
3905 // SEL _cmd;
3906 // char *method_type;
3907 // char *_imp;
3908 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003909 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00003910 Int8PtrTy,
3911 Int8PtrTy,
3912 NULL);
3913 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003914
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003915 // struct _objc_cache *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00003916 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00003917 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00003918 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003919}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003920
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003921ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00003922 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003923 // struct _objc_method_description {
3924 // SEL name;
3925 // char *types;
3926 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003927 MethodDescriptionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003928 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003929 Int8PtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003930 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003931 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003932 MethodDescriptionTy);
3933
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003934 // struct _objc_method_description_list {
3935 // int count;
3936 // struct _objc_method_description[1];
3937 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003938 MethodDescriptionListTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003939 llvm::StructType::get(VMContext, IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003940 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003941 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003942 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003943 MethodDescriptionListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003944
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003945 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003946 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003947 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003948
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003949 // Protocol description structures
3950
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003951 // struct _objc_protocol_extension {
3952 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3953 // struct _objc_method_description_list *optional_instance_methods;
3954 // struct _objc_method_description_list *optional_class_methods;
3955 // struct _objc_property_list *instance_properties;
3956 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003957 ProtocolExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00003958 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003959 MethodDescriptionListPtrTy,
3960 MethodDescriptionListPtrTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003961 PropertyListPtrTy,
3962 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003963 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003964 ProtocolExtensionTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003965
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003966 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00003967 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003968
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00003969 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003970
Owen Anderson8c8f69e2009-08-13 23:27:53 +00003971 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
3972 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003973
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003974 const llvm::Type *T =
Mike Stump1eb44332009-09-09 15:08:12 +00003975 llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00003976 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003977 LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00003978 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00003979 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003980 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3981
Fariborz Jahanian10a42312009-01-21 00:39:53 +00003982 // struct _objc_protocol {
3983 // struct _objc_protocol_extension *isa;
3984 // char *protocol_name;
3985 // struct _objc_protocol **_objc_protocol_list;
3986 // struct _objc_method_description_list *instance_methods;
3987 // struct _objc_method_description_list *class_methods;
3988 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00003989 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003990 Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003991 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003992 MethodDescriptionListPtrTy,
3993 MethodDescriptionListPtrTy,
3994 NULL);
3995 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3996
3997 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003998 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003999 ProtocolListTy);
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004000 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004001 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004002
4003 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004004 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004005 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004006
4007 // Class description structures
4008
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004009 // struct _objc_ivar {
4010 // char *ivar_name;
4011 // char *ivar_type;
4012 // int ivar_offset;
4013 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004014 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004015 Int8PtrTy,
4016 IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004017 NULL);
4018 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4019
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004020 // struct _objc_ivar_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004021 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004022 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004023 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004024
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004025 // struct _objc_method_list *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004026 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004027 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004028 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004029
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004030 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004031 ClassExtensionTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004032 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004033 Int8PtrTy,
4034 PropertyListPtrTy,
4035 NULL);
4036 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004037 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004038
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004039 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004040
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004041 // struct _objc_class {
4042 // Class isa;
4043 // Class super_class;
4044 // char *name;
4045 // long version;
4046 // long info;
4047 // long instance_size;
4048 // struct _objc_ivar_list *ivars;
4049 // struct _objc_method_list *methods;
4050 // struct _objc_cache *cache;
4051 // struct _objc_protocol_list *protocols;
4052 // char *ivar_layout;
4053 // struct _objc_class_ext *ext;
4054 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004055 T = llvm::StructType::get(VMContext,
4056 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson96e0fc72009-07-29 22:16:19 +00004057 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004058 Int8PtrTy,
4059 LongTy,
4060 LongTy,
4061 LongTy,
4062 IvarListPtrTy,
4063 MethodListPtrTy,
4064 CachePtrTy,
4065 ProtocolListPtrTy,
4066 Int8PtrTy,
4067 ClassExtensionPtrTy,
4068 NULL);
4069 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004070
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004071 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4072 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004073 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004074
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004075 // struct _objc_category {
4076 // char *category_name;
4077 // char *class_name;
4078 // struct _objc_method_list *instance_method;
4079 // struct _objc_method_list *class_method;
4080 // uint32_t size; // sizeof(struct _objc_category)
4081 // struct _objc_property_list *instance_properties;// category's @property
4082 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004083 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004084 Int8PtrTy,
4085 MethodListPtrTy,
4086 MethodListPtrTy,
4087 ProtocolListPtrTy,
4088 IntTy,
4089 PropertyListPtrTy,
4090 NULL);
4091 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4092
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004093 // Global metadata structures
4094
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004095 // struct _objc_symtab {
4096 // long sel_ref_cnt;
4097 // SEL *refs;
4098 // short cls_def_cnt;
4099 // short cat_def_cnt;
4100 // char *defs[cls_def_cnt + cat_def_cnt];
4101 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004102 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004103 SelectorPtrTy,
4104 ShortTy,
4105 ShortTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004106 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004107 NULL);
4108 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004109 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004110
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004111 // struct _objc_module {
4112 // long version;
4113 // long size; // sizeof(struct _objc_module)
4114 // char *name;
4115 // struct _objc_symtab* symtab;
4116 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004117 ModuleTy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004118 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004119 LongTy,
4120 Int8PtrTy,
4121 SymtabPtrTy,
4122 NULL);
4123 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004124
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004125
Mike Stumpf5408fe2009-05-16 07:57:57 +00004126 // FIXME: This is the size of the setjmp buffer and should be target
4127 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004128 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004129
Anders Carlsson124526b2008-09-09 10:10:21 +00004130 // Exceptions
Owen Anderson96e0fc72009-07-29 22:16:19 +00004131 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004132 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004133
4134 ExceptionDataTy =
Chris Lattner77b89b82010-06-27 07:15:29 +00004135 llvm::StructType::get(VMContext,
4136 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4137 SetJmpBufferSize),
Anders Carlsson124526b2008-09-09 10:10:21 +00004138 StackPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004139 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson124526b2008-09-09 10:10:21 +00004140 ExceptionDataTy);
4141
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004142}
4143
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004144ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004145 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004146 // struct _method_list_t {
4147 // uint32_t entsize; // sizeof(struct _objc_method)
4148 // uint32_t method_count;
4149 // struct _objc_method method_list[method_count];
4150 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004151 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004152 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004153 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004154 NULL);
4155 CGM.getModule().addTypeName("struct.__method_list_t",
4156 MethodListnfABITy);
4157 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004158 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004159
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004160 // struct _protocol_t {
4161 // id isa; // NULL
4162 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004163 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004164 // const struct method_list_t * const instance_methods;
4165 // const struct method_list_t * const class_methods;
4166 // const struct method_list_t *optionalInstanceMethods;
4167 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004168 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004169 // const uint32_t size; // sizeof(struct _protocol_t)
4170 // const uint32_t flags; // = 0
4171 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004172
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004173 // Holder for struct _protocol_list_t *
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004174 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004175
Owen Anderson47a434f2009-08-05 23:18:46 +00004176 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004177 Int8PtrTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004178 llvm::PointerType::getUnqual(
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004179 ProtocolListTyHolder),
4180 MethodListnfABIPtrTy,
4181 MethodListnfABIPtrTy,
4182 MethodListnfABIPtrTy,
4183 MethodListnfABIPtrTy,
4184 PropertyListPtrTy,
4185 IntTy,
4186 IntTy,
4187 NULL);
4188 CGM.getModule().addTypeName("struct._protocol_t",
4189 ProtocolnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004190
4191 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004192 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004193
Fariborz Jahanianda320092009-01-29 19:24:30 +00004194 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004195 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004196 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004197 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004198 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004199 llvm::ArrayType::get(
Daniel Dunbar948e2582009-02-15 07:36:20 +00004200 ProtocolnfABIPtrTy, 0),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004201 NULL);
4202 CGM.getModule().addTypeName("struct._objc_protocol_list",
4203 ProtocolListnfABITy);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004204 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004205 ProtocolListnfABITy);
4206
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004207 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004208 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004209
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004210 // struct _ivar_t {
4211 // unsigned long int *offset; // pointer to ivar offset location
4212 // char *name;
4213 // char *type;
4214 // uint32_t alignment;
4215 // uint32_t size;
4216 // }
Mike Stump1eb44332009-09-09 15:08:12 +00004217 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson47a434f2009-08-05 23:18:46 +00004218 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004219 Int8PtrTy,
4220 Int8PtrTy,
4221 IntTy,
4222 IntTy,
4223 NULL);
4224 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004225
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004226 // struct _ivar_list_t {
4227 // uint32 entsize; // sizeof(struct _ivar_t)
4228 // uint32 count;
4229 // struct _iver_t list[count];
4230 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004231 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004232 IntTy,
Owen Anderson96e0fc72009-07-29 22:16:19 +00004233 llvm::ArrayType::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004234 IvarnfABITy, 0),
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004235 NULL);
4236 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004237
Owen Anderson96e0fc72009-07-29 22:16:19 +00004238 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004239
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004240 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004241 // uint32_t const flags;
4242 // uint32_t const instanceStart;
4243 // uint32_t const instanceSize;
4244 // uint32_t const reserved; // only when building for 64bit targets
4245 // const uint8_t * const ivarLayout;
4246 // const char *const name;
4247 // const struct _method_list_t * const baseMethods;
4248 // const struct _objc_protocol_list *const baseProtocols;
4249 // const struct _ivar_list_t *const ivars;
4250 // const uint8_t * const weakIvarLayout;
4251 // const struct _prop_list_t * const properties;
4252 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004253
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004254 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson47a434f2009-08-05 23:18:46 +00004255 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004256 IntTy,
4257 IntTy,
4258 Int8PtrTy,
4259 Int8PtrTy,
4260 MethodListnfABIPtrTy,
4261 ProtocolListnfABIPtrTy,
4262 IvarListnfABIPtrTy,
4263 Int8PtrTy,
4264 PropertyListPtrTy,
4265 NULL);
4266 CGM.getModule().addTypeName("struct._class_ro_t",
4267 ClassRonfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004268
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004269 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4270 std::vector<const llvm::Type*> Params;
4271 Params.push_back(ObjectPtrTy);
4272 Params.push_back(SelectorPtrTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004273 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004274 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4275
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004276 // struct _class_t {
4277 // struct _class_t *isa;
4278 // struct _class_t * const superclass;
4279 // void *cache;
4280 // IMP *vtable;
4281 // struct class_ro_t *ro;
4282 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004283
Owen Anderson8c8f69e2009-08-13 23:27:53 +00004284 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Andersona1cf15f2009-07-14 23:10:40 +00004285 ClassnfABITy =
Owen Anderson47a434f2009-08-05 23:18:46 +00004286 llvm::StructType::get(VMContext,
4287 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004288 llvm::PointerType::getUnqual(ClassTyHolder),
4289 CachePtrTy,
4290 llvm::PointerType::getUnqual(ImpnfABITy),
4291 llvm::PointerType::getUnqual(ClassRonfABITy),
4292 NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004293 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4294
4295 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004296 ClassnfABITy);
4297
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004298 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004299 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004300
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004301 // struct _category_t {
4302 // const char * const name;
4303 // struct _class_t *const cls;
4304 // const struct _method_list_t * const instance_methods;
4305 // const struct _method_list_t * const class_methods;
4306 // const struct _protocol_list_t * const protocols;
4307 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004308 // }
Owen Anderson47a434f2009-08-05 23:18:46 +00004309 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004310 ClassnfABIPtrTy,
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004311 MethodListnfABIPtrTy,
4312 MethodListnfABIPtrTy,
4313 ProtocolListnfABIPtrTy,
4314 PropertyListPtrTy,
4315 NULL);
4316 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004317
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004318 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004319 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4320 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004321
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004322 // MessageRefTy - LLVM for:
4323 // struct _message_ref_t {
4324 // IMP messenger;
4325 // SEL name;
4326 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004327
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004328 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004329 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004330 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004331 SourceLocation(),
4332 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004333 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004334 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004335 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00004336 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004337 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004338
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004339 MessageRefCTy = Ctx.getTagDeclType(RD);
4340 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4341 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004342
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004343 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004344 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004345
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004346 // SuperMessageRefTy - LLVM for:
4347 // struct _super_message_ref_t {
4348 // SUPER_IMP messenger;
4349 // SEL name;
4350 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004351 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004352 SelectorPtrTy,
4353 NULL);
4354 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004355
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004356 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004357 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4358
Daniel Dunbare588b992009-03-01 04:46:24 +00004359
4360 // struct objc_typeinfo {
4361 // const void** vtable; // objc_ehtype_vtable + 2
4362 // const char* name; // c++ typeinfo string
4363 // Class cls;
4364 // };
Owen Anderson47a434f2009-08-05 23:18:46 +00004365 EHTypeTy = llvm::StructType::get(VMContext,
4366 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbare588b992009-03-01 04:46:24 +00004367 Int8PtrTy,
4368 ClassnfABIPtrTy,
4369 NULL);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00004370 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004371 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004372}
4373
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004374llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004375 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004376
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004377 return NULL;
4378}
4379
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004380void CGObjCNonFragileABIMac::AddModuleClassList(const
4381 std::vector<llvm::GlobalValue*>
4382 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004383 const char *SymbolName,
4384 const char *SectionName) {
4385 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004386
Daniel Dunbar463b8762009-05-15 21:48:48 +00004387 if (!NumClasses)
4388 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004389
Daniel Dunbar463b8762009-05-15 21:48:48 +00004390 std::vector<llvm::Constant*> Symbols(NumClasses);
4391 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004392 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004393 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004394 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004395 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004396 NumClasses),
4397 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004398
Daniel Dunbar463b8762009-05-15 21:48:48 +00004399 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004400 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004401 llvm::GlobalValue::InternalLinkage,
4402 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004403 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004404 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004405 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004406 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004407}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004408
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004409void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4410 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004411
Daniel Dunbar463b8762009-05-15 21:48:48 +00004412 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004413 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004414 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004415 "\01L_OBJC_LABEL_CLASS_$",
4416 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004417
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004418 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4419 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4420 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4421 continue;
4422 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004423 }
4424
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004425 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4426 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4427 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4428 continue;
4429 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4430 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004431
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004432 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004433 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4434 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004435
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004436 // Build list of all implemented category addresses in array
4437 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004438 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004439 "\01L_OBJC_LABEL_CATEGORY_$",
4440 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004441 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004442 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4443 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004444
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004445 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004446}
4447
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004448/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004449/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004450/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004451/// message dispatch call for all the rest.
4452///
4453bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004454 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4455 default:
4456 assert(0 && "Invalid dispatch method!");
4457 case CodeGenOptions::Legacy:
Daniel Dunbar2feefe82010-02-01 21:07:33 +00004458 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004459 case CodeGenOptions::NonLegacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004460 return false;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004461 case CodeGenOptions::Mixed:
4462 break;
4463 }
4464
4465 // If so, see whether this selector is in the white-list of things which must
4466 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004467 if (NonLegacyDispatchMethods.empty()) {
4468 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4469 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4470 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4471 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4472 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4473 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4474 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4475 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4476 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4477 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004478
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004479 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4480 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4481 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4482 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4483 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4484 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4485 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4486 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004487 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004488 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004489 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4490 &CGM.getContext().Idents.get("objects"),
4491 &CGM.getContext().Idents.get("count")
Fariborz Jahanianbe53be42009-05-13 16:19:02 +00004492 };
4493 NonLegacyDispatchMethods.insert(
4494 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004495 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004496
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00004497 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004498}
4499
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004500// Metadata flags
4501enum MetaDataDlags {
4502 CLS = 0x0,
4503 CLS_META = 0x1,
4504 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004505 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004506 CLS_EXCEPTION = 0x20
4507};
4508/// BuildClassRoTInitializer - generate meta-data for:
4509/// struct _class_ro_t {
4510/// uint32_t const flags;
4511/// uint32_t const instanceStart;
4512/// uint32_t const instanceSize;
4513/// uint32_t const reserved; // only when building for 64bit targets
4514/// const uint8_t * const ivarLayout;
4515/// const char *const name;
4516/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004517/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004518/// const struct _ivar_list_t *const ivars;
4519/// const uint8_t * const weakIvarLayout;
4520/// const struct _prop_list_t * const properties;
4521/// }
4522///
4523llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004524 unsigned flags,
4525 unsigned InstanceStart,
4526 unsigned InstanceSize,
4527 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004528 std::string ClassName = ID->getNameAsString();
4529 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004530 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4531 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4532 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004533 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004534 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4535 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004536 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004537 // const struct _method_list_t * const baseMethods;
4538 std::vector<llvm::Constant*> Methods;
4539 std::string MethodListName("\01l_OBJC_$_");
4540 if (flags & CLS_META) {
4541 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004542 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004543 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004544 // Class methods should always be defined.
4545 Methods.push_back(GetMethodConstant(*i));
4546 }
4547 } else {
4548 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004549 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004550 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004551 // Instance methods should always be defined.
4552 Methods.push_back(GetMethodConstant(*i));
4553 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004554 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004555 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004556 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004557
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004558 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4559 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004560
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004561 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4562 if (llvm::Constant *C = GetMethodConstant(MD))
4563 Methods.push_back(C);
4564 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4565 if (llvm::Constant *C = GetMethodConstant(MD))
4566 Methods.push_back(C);
4567 }
4568 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004569 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004570 Values[ 5] = EmitMethodList(MethodListName,
4571 "__DATA, __objc_const", Methods);
4572
Fariborz Jahanianda320092009-01-29 19:24:30 +00004573 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4574 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004575 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004576 + OID->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00004577 OID->protocol_begin(),
4578 OID->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004579
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004580 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004581 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004582 else
4583 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004584 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4585 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004586 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004587 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004588 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004589 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4590 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004591 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004592 Values);
4593 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004594 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4595 llvm::GlobalValue::InternalLinkage,
4596 Init,
4597 (flags & CLS_META) ?
4598 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4599 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004600 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004601 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004602 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004603 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004604
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004605}
4606
4607/// BuildClassMetaData - This routine defines that to-level meta-data
4608/// for the given ClassName for:
4609/// struct _class_t {
4610/// struct _class_t *isa;
4611/// struct _class_t * const superclass;
4612/// void *cache;
4613/// IMP *vtable;
4614/// struct class_ro_t *ro;
4615/// }
4616///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004617llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004618 std::string &ClassName,
4619 llvm::Constant *IsAGV,
4620 llvm::Constant *SuperClassGV,
4621 llvm::Constant *ClassRoGV,
4622 bool HiddenVisibility) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004623 std::vector<llvm::Constant*> Values(5);
4624 Values[0] = IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004625 Values[1] = SuperClassGV;
4626 if (!Values[1])
4627 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004628 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4629 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4630 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004631 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004632 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004633 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4634 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004635 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004636 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004637 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004638 if (HiddenVisibility)
4639 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004640 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004641}
4642
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004643bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004644CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004645 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004646}
4647
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004648void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004649 uint32_t &InstanceStart,
4650 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004651 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004652 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004653
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004654 // InstanceSize is really instance end.
Anders Carlsson243a6852009-07-18 21:26:44 +00004655 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004656
4657 // If there are no fields, the start is the same as the end.
4658 if (!RL.getFieldCount())
4659 InstanceStart = InstanceSize;
4660 else
4661 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004662}
4663
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004664void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4665 std::string ClassName = ID->getNameAsString();
4666 if (!ObjCEmptyCacheVar) {
4667 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004668 CGM.getModule(),
4669 ObjCTypes.CacheTy,
4670 false,
4671 llvm::GlobalValue::ExternalLinkage,
4672 0,
4673 "_objc_empty_cache");
4674
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004675 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004676 CGM.getModule(),
4677 ObjCTypes.ImpnfABITy,
4678 false,
4679 llvm::GlobalValue::ExternalLinkage,
4680 0,
4681 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004682 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004683 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004684 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004685 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004686 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004687 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004688 uint32_t InstanceSize = InstanceStart;
4689 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004690 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4691 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004692
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004693 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004694
4695 bool classIsHidden =
Daniel Dunbar04d40782009-04-14 06:00:08 +00004696 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004697 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004698 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004699 if (ID->getNumIvarInitializers())
4700 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004701 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004702 // class is root
4703 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004704 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004705 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004706 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004707 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004708 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4709 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4710 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004711 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004712 if (Root->hasAttr<WeakImportAttr>())
4713 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004714 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004715 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004716 ObjCMetaClassName +
4717 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004718 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004719 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4720 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004721 }
4722 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4723 InstanceStart,
4724 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004725 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004726 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004727 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4728 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004729 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004730
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004731 // Metadata for the class
4732 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004733 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004734 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004735 if (ID->getNumIvarInitializers())
4736 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004737
Douglas Gregor68584ed2009-06-18 16:11:24 +00004738 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004739 flags |= CLS_EXCEPTION;
4740
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004741 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004742 flags |= CLS_ROOT;
4743 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00004744 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004745 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00004746 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004747 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004748 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004749 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4750 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004751 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004752 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004753 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004754 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004755 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00004756 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004757
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004758 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004759 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004760 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4761 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004762 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004763
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004764 // Determine if this class is also "non-lazy".
4765 if (ImplementationIsNonLazy(ID))
4766 DefinedNonLazyClasses.push_back(ClassMD);
4767
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00004768 // Force the definition of the EHType if necessary.
4769 if (flags & CLS_EXCEPTION)
4770 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004771}
4772
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004773/// GenerateProtocolRef - This routine is called to generate code for
4774/// a protocol reference expression; as in:
4775/// @code
4776/// @protocol(Proto1);
4777/// @endcode
4778/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4779/// which will hold address of the protocol meta-data.
4780///
4781llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004782 const ObjCProtocolDecl *PD) {
4783
Fariborz Jahanian960cd062009-04-10 18:47:34 +00004784 // This routine is called for @protocol only. So, we must build definition
4785 // of protocol's meta-data (not a reference to it!)
4786 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004787 llvm::Constant *Init =
4788 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4789 ObjCTypes.ExternalProtocolPtrTy);
4790
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004791 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4792 ProtocolName += PD->getNameAsCString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004793
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004794 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4795 if (PTGV)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00004796 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004797 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004798 CGM.getModule(),
4799 Init->getType(), false,
4800 llvm::GlobalValue::WeakAnyLinkage,
4801 Init,
4802 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004803 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4804 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00004805 CGM.AddUsedGlobal(PTGV);
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00004806 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00004807}
4808
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004809/// GenerateCategory - Build metadata for a category implementation.
4810/// struct _category_t {
4811/// const char * const name;
4812/// struct _class_t *const cls;
4813/// const struct _method_list_t * const instance_methods;
4814/// const struct _method_list_t * const class_methods;
4815/// const struct _protocol_list_t * const protocols;
4816/// const struct _prop_list_t * const properties;
4817/// }
4818///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004819void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004820 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004821 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004822 std::string ExtCatName(Prefix + Interface->getNameAsString()+
4823 "_$_" + OCD->getNameAsString());
4824 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004825 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004826
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004827 std::vector<llvm::Constant*> Values(6);
4828 Values[0] = GetClassName(OCD->getIdentifier());
4829 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004830 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00004831 if (Interface->hasAttr<WeakImportAttr>())
4832 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
4833
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004834 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004835 std::vector<llvm::Constant*> Methods;
4836 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004837 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004838 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004839
4840 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004841 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004842 // Instance methods should always be defined.
4843 Methods.push_back(GetMethodConstant(*i));
4844 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004845
4846 Values[2] = EmitMethodList(MethodListName,
4847 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004848 Methods);
4849
4850 MethodListName = Prefix;
4851 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4852 OCD->getNameAsString();
4853 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004854 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004855 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004856 // Class methods should always be defined.
4857 Methods.push_back(GetMethodConstant(*i));
4858 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004859
4860 Values[3] = EmitMethodList(MethodListName,
4861 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004862 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004863 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004864 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004865 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004866 llvm::SmallString<256> ExtName;
4867 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
4868 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004869 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004870 + Interface->getName() + "_$_"
4871 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004872 Category->protocol_begin(),
4873 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004874 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
4875 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00004876 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00004877 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4878 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00004879 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004880
4881 llvm::Constant *Init =
4882 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004883 Values);
4884 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004885 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004886 false,
4887 llvm::GlobalValue::InternalLinkage,
4888 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004889 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004890 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004891 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004892 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00004893 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004894 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004895
4896 // Determine if this category is also "non-lazy".
4897 if (ImplementationIsNonLazy(OCD))
4898 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00004899}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004900
4901/// GetMethodConstant - Return a struct objc_method constant for the
4902/// given method if it has been defined. The result is null if the
4903/// method has not been defined. The return value has type MethodPtrTy.
4904llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004905 const ObjCMethodDecl *MD) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004906 // FIXME: Use DenseMap::lookup
4907 llvm::Function *Fn = MethodDefinitions[MD];
4908 if (!Fn)
4909 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004910
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004911 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004912 Method[0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00004913 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004914 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004915 Method[1] = GetMethodVarType(MD);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004916 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00004917 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004918}
4919
4920/// EmitMethodList - Build meta-data for method declarations
4921/// struct _method_list_t {
4922/// uint32_t entsize; // sizeof(struct _objc_method)
4923/// uint32_t method_count;
4924/// struct _objc_method method_list[method_count];
4925/// }
4926///
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004927llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
4928 const char *Section,
4929 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004930 // Return null for empty list.
4931 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00004932 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004933
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004934 std::vector<llvm::Constant*> Values(3);
4935 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00004936 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004937 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004938 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004939 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004940 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004941 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00004942 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00004943 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004944
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004945 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004946 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004947 llvm::GlobalValue::InternalLinkage,
4948 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004949 Name);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004950 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004951 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004952 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00004953 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00004954 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004955 ObjCTypes.MethodListnfABIPtrTy);
4956}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004957
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004958/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4959/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00004960llvm::GlobalVariable *
4961CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
4962 const ObjCIvarDecl *Ivar) {
4963 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00004964 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00004965 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004966 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004967 CGM.getModule().getGlobalVariable(Name);
4968 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004969 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004970 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004971 false,
4972 llvm::GlobalValue::ExternalLinkage,
4973 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00004974 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00004975 return IvarOffsetGV;
4976}
4977
Daniel Dunbare83be122010-04-02 21:14:02 +00004978llvm::Constant *
4979CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
4980 const ObjCIvarDecl *Ivar,
4981 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00004982 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004983 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00004984 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004985 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004986 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00004987
Mike Stumpf5408fe2009-05-16 07:57:57 +00004988 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4989 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00004990 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4991 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4992 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00004993 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00004994 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00004995 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004996 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00004997 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004998}
4999
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005000/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005001/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005002/// IvarListnfABIPtrTy.
5003/// struct _ivar_t {
5004/// unsigned long int *offset; // pointer to ivar offset location
5005/// char *name;
5006/// char *type;
5007/// uint32_t alignment;
5008/// uint32_t size;
5009/// }
5010/// struct _ivar_list_t {
5011/// uint32 entsize; // sizeof(struct _ivar_t)
5012/// uint32 count;
5013/// struct _iver_t list[count];
5014/// }
5015///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005016
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005017llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005018 const ObjCImplementationDecl *ID) {
5019
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005020 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005021
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005022 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5023 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005024
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005025 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005026
Daniel Dunbar91636d62009-04-20 00:33:43 +00005027 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian18191882009-03-31 18:11:23 +00005028 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005029 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005030
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005031 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5032 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005033 // Ignore unnamed bit-fields.
5034 if (!IVD->getDeclName())
5035 continue;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005036 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005037 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005038 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5039 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005040 const llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005041 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005042 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005043 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005044 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005045 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005046 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005047 // NOTE. Size of a bitfield does not match gcc's, because of the
5048 // way bitfields are treated special in each. But I am told that
5049 // 'size' for bitfield ivars is ignored by the runtime so it does
5050 // not matter. If it matters, there is enough info to get the
5051 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005052 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005053 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005054 }
5055 // Return null for empty list.
5056 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005057 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005058 std::vector<llvm::Constant*> Values(3);
Duncan Sands9408c452009-05-09 07:08:47 +00005059 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005060 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5061 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005062 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005063 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005064 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005065 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005066 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5067 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005068 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005069 llvm::GlobalValue::InternalLinkage,
5070 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005071 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005072 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005073 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005074 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005075
Chris Lattnerad64e022009-07-17 23:57:13 +00005076 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005077 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005078}
5079
5080llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005081 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005082 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005083
Fariborz Jahanianda320092009-01-29 19:24:30 +00005084 if (!Entry) {
5085 // We use the initializer as a marker of whether this is a forward
5086 // reference or not. At module finalization we add the empty
5087 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005088 Entry =
5089 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5090 llvm::GlobalValue::ExternalLinkage,
5091 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005092 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005093 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005094 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005095
Fariborz Jahanianda320092009-01-29 19:24:30 +00005096 return Entry;
5097}
5098
5099/// GetOrEmitProtocol - Generate the protocol meta-data:
5100/// @code
5101/// struct _protocol_t {
5102/// id isa; // NULL
5103/// const char * const protocol_name;
5104/// const struct _protocol_list_t * protocol_list; // super protocols
5105/// const struct method_list_t * const instance_methods;
5106/// const struct method_list_t * const class_methods;
5107/// const struct method_list_t *optionalInstanceMethods;
5108/// const struct method_list_t *optionalClassMethods;
5109/// const struct _prop_list_t * properties;
5110/// const uint32_t size; // sizeof(struct _protocol_t)
5111/// const uint32_t flags; // = 0
5112/// }
5113/// @endcode
5114///
5115
5116llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005117 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005118 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005119
Fariborz Jahanianda320092009-01-29 19:24:30 +00005120 // Early exit if a defining object has already been generated.
5121 if (Entry && Entry->hasInitializer())
5122 return Entry;
5123
Fariborz Jahanianda320092009-01-29 19:24:30 +00005124 // Construct method lists.
5125 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5126 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005127 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005128 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005129 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005130 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005131 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5132 OptInstanceMethods.push_back(C);
5133 } else {
5134 InstanceMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005135 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005136 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005137
5138 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005139 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005140 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005141 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005142 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5143 OptClassMethods.push_back(C);
5144 } else {
5145 ClassMethods.push_back(C);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005146 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005147 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005148
Fariborz Jahanianda320092009-01-29 19:24:30 +00005149 std::vector<llvm::Constant*> Values(10);
5150 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005151 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005152 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005153 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5154 PD->protocol_begin(),
5155 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005156
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005157 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005158 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005159 "__DATA, __objc_const",
5160 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005161 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005162 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005163 "__DATA, __objc_const",
5164 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005165 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005166 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005167 "__DATA, __objc_const",
5168 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005169 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005170 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005171 "__DATA, __objc_const",
5172 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005173 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005174 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005175 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005176 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005177 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005178 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005179 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005180 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005181
Fariborz Jahanianda320092009-01-29 19:24:30 +00005182 if (Entry) {
5183 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005184 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005185 Entry->setInitializer(Init);
5186 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005187 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005188 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5189 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5190 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005191 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005192 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005193 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005194 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005195 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005196 CGM.AddUsedGlobal(Entry);
5197
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005198 // Use this protocol meta-data to build protocol list table in section
5199 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005200 llvm::GlobalVariable *PTGV =
5201 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5202 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5203 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005204 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005205 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005206 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005207 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005208 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005209 return Entry;
5210}
5211
5212/// EmitProtocolList - Generate protocol list meta-data:
5213/// @code
5214/// struct _protocol_list_t {
5215/// long protocol_count; // Note, this is 32/64 bit
5216/// struct _protocol_t[protocol_count];
5217/// }
5218/// @endcode
5219///
5220llvm::Constant *
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005221CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5222 ObjCProtocolDecl::protocol_iterator begin,
5223 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005224 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005225
Fariborz Jahanianda320092009-01-29 19:24:30 +00005226 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005227 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005228 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005229
Daniel Dunbar948e2582009-02-15 07:36:20 +00005230 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005231 llvm::SmallString<256> TmpName;
5232 Name.toVector(TmpName);
5233 llvm::GlobalVariable *GV =
5234 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005235 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005236 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005237
Daniel Dunbar948e2582009-02-15 07:36:20 +00005238 for (; begin != end; ++begin)
5239 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5240
Fariborz Jahanianda320092009-01-29 19:24:30 +00005241 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005242 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005243 ObjCTypes.ProtocolnfABIPtrTy));
5244
Fariborz Jahanianda320092009-01-29 19:24:30 +00005245 std::vector<llvm::Constant*> Values(2);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005246 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005247 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005248 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005249 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005250 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005251 ProtocolRefs.size()),
5252 ProtocolRefs);
5253
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005254 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005255 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005256 llvm::GlobalValue::InternalLinkage,
5257 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005258 Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005259 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005260 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005261 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005262 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005263 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005264 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005265}
5266
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005267/// GetMethodDescriptionConstant - This routine build following meta-data:
5268/// struct _objc_method {
5269/// SEL _cmd;
5270/// char *method_type;
5271/// char *_imp;
5272/// }
5273
5274llvm::Constant *
5275CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5276 std::vector<llvm::Constant*> Desc(3);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005277 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005278 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5279 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005280 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005281 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005282 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005283 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005284}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005285
5286/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5287/// This code gen. amounts to generating code for:
5288/// @code
5289/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5290/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005291///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005292LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005293 CodeGen::CodeGenFunction &CGF,
5294 QualType ObjectTy,
5295 llvm::Value *BaseValue,
5296 const ObjCIvarDecl *Ivar,
5297 unsigned CVRQualifiers) {
5298 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005299 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5300 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005301}
5302
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005303llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005304 CodeGen::CodeGenFunction &CGF,
5305 const ObjCInterfaceDecl *Interface,
5306 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005307 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005308}
5309
Fariborz Jahanian46551122009-02-04 00:22:57 +00005310CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005311 CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005312 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005313 QualType ResultType,
5314 Selector Sel,
5315 llvm::Value *Receiver,
5316 QualType Arg0Ty,
5317 bool IsSuper,
5318 const CallArgList &CallArgs) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00005319 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5320 // to 'super' receivers.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005321 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005322 llvm::Value *Arg0 = Receiver;
5323 if (!IsSuper)
5324 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005325
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005326 // Find the message function name.
Mike Stumpf5408fe2009-05-16 07:57:57 +00005327 // FIXME. This is too much work to get the ABI-specific result type needed to
5328 // find the message name.
John McCallead608a2010-02-26 00:48:12 +00005329 const CGFunctionInfo &FnInfo
Rafael Espindola264ba482010-03-30 20:24:48 +00005330 = Types.getFunctionInfo(ResultType, CallArgList(),
5331 FunctionType::ExtInfo());
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005332 llvm::Constant *Fn = 0;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005333 std::string Name("\01l_");
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005334 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005335#if 0
5336 // unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005337 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005338 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005339 // FIXME. Is there a better way of getting these names.
5340 // They are available in RuntimeFunctions vector pair.
5341 Name += "objc_msgSendId_stret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005342 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005343#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005344 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005345 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005346 Name += "objc_msgSendSuper2_stret_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005347 } else {
5348 Fn = ObjCTypes.getMessageSendStretFixupFn();
5349 Name += "objc_msgSend_stret_fixup";
5350 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00005351 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5352 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5353 Name += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005354 } else {
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005355#if 0
5356// unlike what is documented. gcc never generates this API!!
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005357 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005358 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005359 Name += "objc_msgSendId_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005360 } else
Fariborz Jahanianc1708522009-02-05 18:00:27 +00005361#endif
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005362 if (IsSuper) {
Chris Lattner1c02f862009-04-22 02:53:24 +00005363 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005364 Name += "objc_msgSendSuper2_fixup";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005365 } else {
5366 Fn = ObjCTypes.getMessageSendFixupFn();
5367 Name += "objc_msgSend_fixup";
5368 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005369 }
Fariborz Jahanian70b51c72009-04-30 23:08:58 +00005370 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005371 Name += '_';
5372 std::string SelName(Sel.getAsString());
5373 // Replace all ':' in selector name with '_' ouch!
Mike Stump1eb44332009-09-09 15:08:12 +00005374 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005375 if (SelName[i] == ':')
5376 SelName[i] = '_';
5377 Name += SelName;
5378 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5379 if (!GV) {
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005380 // Build message ref table entry.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005381 std::vector<llvm::Constant*> Values(2);
5382 Values[0] = Fn;
5383 Values[1] = GetMethodVarName(Sel);
Nick Lewycky0d36dd22009-09-19 20:00:52 +00005384 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Anderson1c431b32009-07-08 19:05:04 +00005385 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stump286acbd2009-03-07 16:33:28 +00005386 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005387 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005388 Name);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005389 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf59c1a62009-04-15 19:04:46 +00005390 GV->setAlignment(16);
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005391 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005392 }
5393 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005394
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005395 CallArgList ActualArgs;
5396 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005397 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005398 ObjCTypes.MessageRefCPtrTy));
5399 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCall04a67a62010-02-05 21:31:56 +00005400 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00005401 FunctionType::ExtInfo());
Fariborz Jahanianef163782009-02-05 01:13:09 +00005402 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5403 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian3ab75bd2009-02-14 21:25:36 +00005404 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanianef163782009-02-05 01:13:09 +00005405 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson96e0fc72009-07-29 22:16:19 +00005406 llvm::PointerType::getUnqual(FTy));
John McCallef072fd2010-05-22 01:48:05 +00005407 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005408}
5409
5410/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005411CodeGen::RValue
5412CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005413 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005414 QualType ResultType,
5415 Selector Sel,
5416 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005417 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005418 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005419 const ObjCMethodDecl *Method) {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005420 return LegacyDispatchedSelector(Sel)
John McCallef072fd2010-05-22 01:48:05 +00005421 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5422 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005423 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005424 false, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005425 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005426 Receiver, CGF.getContext().getObjCIdType(),
5427 false, CallArgs);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005428}
5429
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005430llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005431CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005432 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5433
Daniel Dunbardfff2302009-03-02 05:18:14 +00005434 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005435 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005436 false, llvm::GlobalValue::ExternalLinkage,
5437 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005438 }
5439
5440 return GV;
5441}
5442
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005443llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5444 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005445 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005446
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005447 if (!Entry) {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005448 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005449 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005450 Entry =
5451 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005452 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005453 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005454 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005455 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005456 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005457 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005458 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005459 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005460 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005461
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005462 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar11394522009-04-18 08:51:00 +00005463}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005464
Daniel Dunbar11394522009-04-18 08:51:00 +00005465llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005466CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005467 const ObjCInterfaceDecl *ID) {
5468 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005469
Daniel Dunbar11394522009-04-18 08:51:00 +00005470 if (!Entry) {
5471 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5472 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005473 Entry =
5474 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005475 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005476 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005477 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005478 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005479 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005480 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005481 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005482 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005483 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005484
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005485 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005486}
5487
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005488/// EmitMetaClassRef - Return a Value * of the address of _class_t
5489/// meta-data
5490///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005491llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5492 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005493 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5494 if (Entry)
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005495 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005496
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005497 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005498 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005499 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005500 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005501 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005502 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005503 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005504 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005505 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005506 ObjCTypes.ClassnfABIPtrTy));
5507
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005508 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005509 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005510
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005511 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005512}
5513
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005514/// GetClass - Return a reference to the class for the given interface
5515/// decl.
5516llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5517 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005518 if (ID->hasAttr<WeakImportAttr>()) {
5519 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5520 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5521 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5522 }
5523
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005524 return EmitClassRef(Builder, ID);
5525}
5526
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005527/// Generates a message send where the super is the receiver. This is
5528/// a message send to self with special delivery semantics indicating
5529/// which class's method should be called.
5530CodeGen::RValue
5531CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005532 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005533 QualType ResultType,
5534 Selector Sel,
5535 const ObjCInterfaceDecl *Class,
5536 bool isCategoryImpl,
5537 llvm::Value *Receiver,
5538 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005539 const CodeGen::CallArgList &CallArgs,
5540 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005541 // ...
5542 // Create and init a super structure; this is a (receiver, class)
5543 // pair we will pass to objc_msgSendSuper.
5544 llvm::Value *ObjCSuper =
5545 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005546
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005547 llvm::Value *ReceiverAsObject =
5548 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5549 CGF.Builder.CreateStore(ReceiverAsObject,
5550 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005551
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005552 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005553 llvm::Value *Target;
5554 if (IsClassMessage) {
5555 if (isCategoryImpl) {
5556 // Message sent to "super' in a class method defined in
5557 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005558 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005559 Target = CGF.Builder.CreateStructGEP(Target, 0);
5560 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005561 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005562 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005563 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005564 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005565
Mike Stumpf5408fe2009-05-16 07:57:57 +00005566 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5567 // ObjCTypes types.
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005568 const llvm::Type *ClassTy =
5569 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5570 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5571 CGF.Builder.CreateStore(Target,
5572 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005573
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005574 return (LegacyDispatchedSelector(Sel))
John McCallef072fd2010-05-22 01:48:05 +00005575 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5576 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005577 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005578 true, CallArgs, Method, ObjCTypes)
John McCallef072fd2010-05-22 01:48:05 +00005579 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005580 ObjCSuper, ObjCTypes.SuperPtrCTy,
5581 true, CallArgs);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005582}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005583
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005584llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005585 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005586 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005587
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005588 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005589 llvm::Constant *Casted =
5590 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5591 ObjCTypes.SelectorPtrTy);
5592 Entry =
5593 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5594 llvm::GlobalValue::InternalLinkage,
5595 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005596 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005597 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005598 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005599
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005600 if (lval)
5601 return Entry;
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005602 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005603}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005604/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005605/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005606///
5607void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005608 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005609 llvm::Value *dst,
5610 llvm::Value *ivarOffset) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005611 const llvm::Type * SrcTy = src->getType();
5612 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005613 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005614 assert(Size <= 8 && "does not support size > 8");
5615 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5616 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005617 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5618 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005619 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5620 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005621 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5622 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005623 return;
5624}
5625
5626/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5627/// objc_assign_strongCast (id src, id *dst)
5628///
5629void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005630 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005631 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005632 const llvm::Type * SrcTy = src->getType();
5633 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005634 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005635 assert(Size <= 8 && "does not support size > 8");
5636 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005637 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005638 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5639 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005640 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5641 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005642 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005643 src, dst, "weakassign");
5644 return;
5645}
5646
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005647void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005648 CodeGen::CodeGenFunction &CGF,
5649 llvm::Value *DestPtr,
5650 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005651 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005652 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5653 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005654 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00005655 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00005656 return;
5657}
5658
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005659/// EmitObjCWeakRead - Code gen for loading value of a __weak
5660/// object: objc_read_weak (id *src)
5661///
5662llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005663 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005664 llvm::Value *AddrWeakObj) {
Eli Friedman8339b352009-03-07 03:57:15 +00005665 const llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005666 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5667 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00005668 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005669 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00005670 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005671 return read_weak;
5672}
5673
5674/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5675/// objc_assign_weak (id src, id *dst)
5676///
5677void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005678 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005679 const llvm::Type * SrcTy = src->getType();
5680 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005681 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005682 assert(Size <= 8 && "does not support size > 8");
5683 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5684 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005685 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5686 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005687 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5688 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00005689 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005690 src, dst, "weakassign");
5691 return;
5692}
5693
5694/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5695/// objc_assign_global (id src, id *dst)
5696///
5697void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005698 llvm::Value *src, llvm::Value *dst,
5699 bool threadlocal) {
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005700 const llvm::Type * SrcTy = src->getType();
5701 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005702 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005703 assert(Size <= 8 && "does not support size > 8");
5704 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5705 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005706 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5707 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005708 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5709 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00005710 if (!threadlocal)
5711 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5712 src, dst, "globalassign");
5713 else
5714 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5715 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005716 return;
5717}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005718
John McCall740e8072010-07-21 00:41:47 +00005719namespace {
John McCall1f0fca52010-07-21 07:22:38 +00005720 struct CallSyncExit : EHScopeStack::Cleanup {
John McCall740e8072010-07-21 00:41:47 +00005721 llvm::Value *SyncExitFn;
5722 llvm::Value *SyncArg;
5723 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5724 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5725
5726 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5727 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5728 }
5729 };
5730}
5731
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005732void
John McCallf1549f62010-07-06 01:34:17 +00005733CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5734 const ObjCAtSynchronizedStmt &S) {
5735 // Evaluate the lock operand. This should dominate the cleanup.
5736 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005737
John McCallf1549f62010-07-06 01:34:17 +00005738 // Acquire the lock.
5739 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5740 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5741 ->setDoesNotThrow();
5742
5743 // Register an all-paths cleanup to release the lock.
John McCall1f0fca52010-07-21 07:22:38 +00005744 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5745 ObjCTypes.getSyncExitFn(),
5746 SyncArg);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005747
John McCallf1549f62010-07-06 01:34:17 +00005748 // Emit the body of the statement.
5749 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005750
John McCallf1549f62010-07-06 01:34:17 +00005751 // Pop the lock-release cleanup.
5752 CGF.PopCleanupBlock();
5753}
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005754
John McCallf1549f62010-07-06 01:34:17 +00005755namespace {
5756 struct CatchHandler {
5757 const VarDecl *Variable;
5758 const Stmt *Body;
5759 llvm::BasicBlock *Block;
5760 llvm::Value *TypeInfo;
5761 };
John McCall8e3f8612010-07-13 22:12:14 +00005762
John McCall1f0fca52010-07-21 07:22:38 +00005763 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall8e3f8612010-07-13 22:12:14 +00005764 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
5765 MightThrow(MightThrow), Fn(Fn) {}
5766 bool MightThrow;
5767 llvm::Value *Fn;
5768
5769 void Emit(CodeGenFunction &CGF, bool IsForEH) {
5770 if (!MightThrow) {
5771 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
5772 return;
5773 }
5774
5775 CGF.EmitCallOrInvoke(Fn, 0, 0);
5776 }
5777 };
John McCallf1549f62010-07-06 01:34:17 +00005778}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005779
John McCallf1549f62010-07-06 01:34:17 +00005780void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5781 const ObjCAtTryStmt &S) {
5782 // Jump destination for falling out of catch bodies.
5783 CodeGenFunction::JumpDest Cont;
5784 if (S.getNumCatchStmts())
5785 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005786
John McCallf1549f62010-07-06 01:34:17 +00005787 CodeGenFunction::FinallyInfo FinallyInfo;
5788 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
5789 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
5790 ObjCTypes.getObjCBeginCatchFn(),
5791 ObjCTypes.getObjCEndCatchFn(),
5792 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005793
John McCallf1549f62010-07-06 01:34:17 +00005794 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005795
John McCallf1549f62010-07-06 01:34:17 +00005796 // Enter the catch, if there is one.
5797 if (S.getNumCatchStmts()) {
5798 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
5799 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregorc00d8e12010-04-26 16:46:50 +00005800 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005801
John McCallf1549f62010-07-06 01:34:17 +00005802 Handlers.push_back(CatchHandler());
5803 CatchHandler &Handler = Handlers.back();
5804 Handler.Variable = CatchDecl;
5805 Handler.Body = CatchStmt->getCatchBody();
5806 Handler.Block = CGF.createBasicBlock("catch");
5807
5808 // @catch(...) always matches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005809 if (!CatchDecl) {
John McCallf1549f62010-07-06 01:34:17 +00005810 Handler.TypeInfo = 0; // catch-all
5811 // Don't consider any other catches.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005812 break;
5813 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005814
John McCallf1549f62010-07-06 01:34:17 +00005815 // There's a particular fixed type info for 'id'.
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005816 if (CatchDecl->getType()->isObjCIdType() ||
5817 CatchDecl->getType()->isObjCQualifiedIdType()) {
5818 llvm::Value *IDEHType =
5819 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5820 if (!IDEHType)
5821 IDEHType =
5822 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5823 false,
5824 llvm::GlobalValue::ExternalLinkage,
5825 0, "OBJC_EHTYPE_id");
John McCallf1549f62010-07-06 01:34:17 +00005826 Handler.TypeInfo = IDEHType;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00005827 } else {
5828 // All other types should be Objective-C interface pointer types.
5829 const ObjCObjectPointerType *PT =
5830 CatchDecl->getType()->getAs<ObjCObjectPointerType>();
5831 assert(PT && "Invalid @catch type.");
5832 const ObjCInterfaceType *IT = PT->getInterfaceType();
5833 assert(IT && "Invalid @catch type.");
John McCallf1549f62010-07-06 01:34:17 +00005834 Handler.TypeInfo = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005835 }
5836 }
John McCallf1549f62010-07-06 01:34:17 +00005837
5838 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
5839 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
5840 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005841 }
John McCallf1549f62010-07-06 01:34:17 +00005842
5843 // Emit the try body.
5844 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005845
John McCallf1549f62010-07-06 01:34:17 +00005846 // Leave the try.
5847 if (S.getNumCatchStmts())
5848 CGF.EHStack.popCatch();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005849
John McCallf1549f62010-07-06 01:34:17 +00005850 // Remember where we were.
5851 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005852
John McCallf1549f62010-07-06 01:34:17 +00005853 // Emit the handlers.
5854 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
5855 CatchHandler &Handler = Handlers[I];
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005856
John McCallf1549f62010-07-06 01:34:17 +00005857 CGF.EmitBlock(Handler.Block);
5858 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005859
John McCallf1549f62010-07-06 01:34:17 +00005860 // Enter the catch.
5861 llvm::CallInst *Exn =
5862 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
5863 "exn.adjusted");
5864 Exn->setDoesNotThrow();
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005865
John McCallf1549f62010-07-06 01:34:17 +00005866 // Add a cleanup to leave the catch.
John McCall8e3f8612010-07-13 22:12:14 +00005867 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCall1f0fca52010-07-21 07:22:38 +00005868 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
5869 EndCatchMightThrow,
5870 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005871
John McCallf1549f62010-07-06 01:34:17 +00005872 // Bind the catch parameter if it exists.
5873 if (const VarDecl *CatchParam = Handler.Variable) {
5874 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
5875 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005876
John McCallf1549f62010-07-06 01:34:17 +00005877 CGF.EmitLocalBlockVarDecl(*CatchParam);
5878 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005879 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005880
John McCallf1549f62010-07-06 01:34:17 +00005881 CGF.ObjCEHValueStack.push_back(Exn);
5882 CGF.EmitStmt(Handler.Body);
5883 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005884
John McCallf1549f62010-07-06 01:34:17 +00005885 // Leave the earlier cleanup.
5886 CGF.PopCleanupBlock();
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005887
John McCallf1549f62010-07-06 01:34:17 +00005888 CGF.EmitBranchThroughCleanup(Cont);
5889 }
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005890
John McCallf1549f62010-07-06 01:34:17 +00005891 // Go back to the try-statement fallthrough.
5892 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005893
John McCallf1549f62010-07-06 01:34:17 +00005894 // Pop out of the normal cleanup on the finally.
5895 if (S.getFinallyStmt())
5896 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005897
John McCallff8e1152010-07-23 21:56:41 +00005898 if (Cont.isValid())
5899 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00005900}
5901
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005902/// EmitThrowStmt - Generate code for a throw statement.
5903void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5904 const ObjCAtThrowStmt &S) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005905 llvm::Value *Exception;
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005906 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005907 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005908 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005909 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005910 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005911 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005912 "Unexpected rethrow outside @catch block.");
5913 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005914 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005915 }
5916
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005917 llvm::Value *ExceptionAsObject =
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005918 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5919 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5920 if (InvokeDest) {
Fariborz Jahanian69677ea2010-05-28 17:34:43 +00005921 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallf1549f62010-07-06 01:34:17 +00005922 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005923 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallf1549f62010-07-06 01:34:17 +00005924 } else {
5925 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
5926 ->setDoesNotReturn();
5927 CGF.Builder.CreateUnreachable();
5928 }
Daniel Dunbar4ff36842009-03-02 06:08:11 +00005929
Anders Carlssonf57c5b22009-02-16 22:59:18 +00005930 // Clear the insertion point to indicate we are in unreachable code.
5931 CGF.Builder.ClearInsertionPoint();
5932}
Daniel Dunbare588b992009-03-01 04:46:24 +00005933
5934llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005935CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005936 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00005937 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00005938
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005939 // If we don't need a definition, return the entry if found or check
5940 // if we use an external reference.
5941 if (!ForDefinition) {
5942 if (Entry)
5943 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00005944
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005945 // If this type (or a super class) has the __objc_exception__
5946 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00005947 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005948 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005949 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005950 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005951 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005952 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00005953 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005954 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005955
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005956 // Otherwise we need to either make a new entry or fill in the
5957 // initializer.
5958 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005959 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00005960 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005961 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00005962 CGM.getModule().getGlobalVariable(VTableName);
5963 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005964 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5965 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00005966 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00005967 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00005968
Chris Lattner77b89b82010-06-27 07:15:29 +00005969 llvm::Value *VTableIdx =
5970 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00005971
5972 std::vector<llvm::Constant*> Values(3);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005973 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbare588b992009-03-01 04:46:24 +00005974 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005975 Values[2] = GetClassGlobal(ClassName);
Owen Andersona1cf15f2009-07-14 23:10:40 +00005976 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00005977 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00005978
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005979 if (Entry) {
5980 Entry->setInitializer(Init);
5981 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00005982 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005983 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005984 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005985 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00005986 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005987 }
5988
Daniel Dunbar04d40782009-04-14 06:00:08 +00005989 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005990 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005991 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
5992 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005993
5994 if (ForDefinition) {
5995 Entry->setSection("__DATA,__objc_const");
5996 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5997 } else {
5998 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5999 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006000
6001 return Entry;
6002}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006003
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006004/* *** */
6005
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006006CodeGen::CGObjCRuntime *
6007CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006008 return new CGObjCMac(CGM);
6009}
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006010
6011CodeGen::CGObjCRuntime *
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00006012CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00006013 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00006014}