blob: 72935373027241de3427405ec34f03f85b140a3d [file] [log] [blame]
Daniel Dunbar303e2c22008-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 Dunbar3ad53482008-08-11 21:35:06 +000015
Daniel Dunbar034299e2010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbara94ecd22008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCallbd309292010-07-06 01:34:17 +000019#include "CGException.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000020#include "clang/AST/ASTContext.h"
Daniel Dunbar221fa942008-08-11 04:54:23 +000021#include "clang/AST/Decl.h"
Daniel Dunbarb036db82008-08-13 03:21:16 +000022#include "clang/AST/DeclObjC.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000023#include "clang/AST/RecordLayout.h"
Chris Lattnerf0b64d72009-04-26 01:32:48 +000024#include "clang/AST/StmtObjC.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000025#include "clang/Basic/LangOptions.h"
Chandler Carruth85098242010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbar3ad53482008-08-11 21:35:06 +000027
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +000028#include "llvm/Intrinsics.h"
Owen Andersonae86c192009-07-13 04:10:07 +000029#include "llvm/LLVMContext.h"
Daniel Dunbar8b8683f2008-08-12 00:12:39 +000030#include "llvm/Module.h"
Daniel Dunbarc475d422008-10-29 22:36:39 +000031#include "llvm/ADT/DenseSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000032#include "llvm/ADT/SetVector.h"
33#include "llvm/ADT/SmallString.h"
Fariborz Jahanian751c1e72009-12-12 21:26:21 +000034#include "llvm/ADT/SmallPtrSet.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000035#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000036#include "llvm/Target/TargetData.h"
Torok Edwindb714922009-08-24 13:25:12 +000037#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000038
39using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000040using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000041
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000042// Common CGObjCRuntime functions, these don't belong here, but they
43// don't belong in CGObjCRuntime either so we will live with it for
44// now.
45
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000046static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
47 const ObjCInterfaceDecl *OID,
Daniel Dunbar961202372009-05-03 12:57:56 +000048 const ObjCImplementationDecl *ID,
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000049 const ObjCIvarDecl *Ivar) {
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000050 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000051
Daniel Dunbar7e5aba42010-04-02 22:29:40 +000052 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
53 // in here; it should never be necessary because that should be the lexical
54 // decl context for the ivar.
Daniel Dunbar031d4d42010-04-02 15:43:29 +000055
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000056 // If we know have an implementation (and the ivar is in it) then
57 // look up in the implementation layout.
Daniel Dunbar59e476b2009-08-03 17:06:42 +000058 const ASTRecordLayout *RL;
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000059 if (ID && ID->getClassInterface() == Container)
60 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
61 else
62 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000063
64 // Compute field index.
65 //
66 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
67 // implemented. This should be fixed to get the information from the layout
68 // directly.
69 unsigned Index = 0;
70 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
71 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
72 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
73 if (Ivar == Ivars[k])
74 break;
75 ++Index;
76 }
77 assert(Index != Ivars.size() && "Ivar is not inside container!");
78
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000079 return RL->getFieldOffset(Index);
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000080}
81
82uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
83 const ObjCInterfaceDecl *OID,
84 const ObjCIvarDecl *Ivar) {
Daniel Dunbar961202372009-05-03 12:57:56 +000085 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
86}
87
88uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
89 const ObjCImplementationDecl *OID,
90 const ObjCIvarDecl *Ivar) {
91 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000092}
93
94LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
95 const ObjCInterfaceDecl *OID,
96 llvm::Value *BaseValue,
97 const ObjCIvarDecl *Ivar,
98 unsigned CVRQualifiers,
99 llvm::Value *Offset) {
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000100 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000101 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000102 QualType IvarTy = Ivar->getType();
103 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000104 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000105 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson9793f0e2009-07-29 22:16:19 +0000106 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000107
John McCall8ccfcb52009-09-24 19:53:00 +0000108 Qualifiers Quals = CGF.MakeQualifiers(IvarTy);
109 Quals.addCVRQualifiers(CVRQualifiers);
110
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000111 if (!Ivar->isBitField())
112 return LValue::MakeAddr(V, Quals);
Daniel Dunbar961202372009-05-03 12:57:56 +0000113
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000114 // We need to compute the bit offset for the bit-field, the offset is to the
115 // byte. Note, there is a subtle invariant here: we can only call this routine
116 // on non-synthesized ivars but we may be called for synthesized ivars.
117 // However, a synthesized ivar can never be a bit-field, so this is safe.
118 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
119 uint64_t BitFieldSize =
120 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000121
Daniel Dunbardc406b82010-04-05 21:36:35 +0000122 // Allocate a new CGBitFieldInfo object to describe this access.
123 //
124 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
125 // layout object. However, this is blocked on other cleanups to the
126 // Objective-C code, so for now we just live with allocating a bunch of these
127 // objects.
Daniel Dunbardc406b82010-04-05 21:36:35 +0000128
Daniel Dunbarb935b932010-04-13 20:58:55 +0000129 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000130 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000131 AI.FieldIndex = 0;
132 AI.FieldByteOffset = 0;
133 AI.FieldBitStart = BitOffset;
134 AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy);
135 AI.AccessAlignment = 0;
136 AI.TargetBitOffset = 0;
137 AI.TargetBitWidth = BitFieldSize;
138
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000139 CGBitFieldInfo *Info =
140 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
141 IvarTy->isSignedIntegerType());
142
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000143 // FIXME: We need to set a very conservative alignment on this, or make sure
144 // that the runtime is doing the right thing.
Daniel Dunbar196ea442010-04-06 01:07:44 +0000145 return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000146}
147
148///
149
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000150namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000151
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000152typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000153
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000154// FIXME: We should find a nicer way to make the labels for metadata, string
155// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000156
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000157class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +0000158protected:
159 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000160
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000161private:
162 llvm::Constant *getMessageSendFn() const {
163 // id objc_msgSend (id, SEL, ...)
164 std::vector<const llvm::Type*> Params;
165 Params.push_back(ObjectPtrTy);
166 Params.push_back(SelectorPtrTy);
167 return
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000168 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
169 Params, true),
170 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000171 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000172
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000173 llvm::Constant *getMessageSendStretFn() const {
174 // id objc_msgSend_stret (id, SEL, ...)
175 std::vector<const llvm::Type*> Params;
176 Params.push_back(ObjectPtrTy);
177 Params.push_back(SelectorPtrTy);
178 return
Owen Anderson41a75022009-08-13 21:57:51 +0000179 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000180 Params, true),
181 "objc_msgSend_stret");
182
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000183 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000184
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000185 llvm::Constant *getMessageSendFpretFn() const {
186 // FIXME: This should be long double on x86_64?
187 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
188 std::vector<const llvm::Type*> Params;
189 Params.push_back(ObjectPtrTy);
190 Params.push_back(SelectorPtrTy);
191 return
Owen Anderson41a75022009-08-13 21:57:51 +0000192 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
193 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000194 Params,
195 true),
196 "objc_msgSend_fpret");
197
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000198 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000199
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000200 llvm::Constant *getMessageSendSuperFn() const {
201 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
202 const char *SuperName = "objc_msgSendSuper";
203 std::vector<const llvm::Type*> Params;
204 Params.push_back(SuperPtrTy);
205 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000206 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000207 Params, true),
208 SuperName);
209 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000210
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000211 llvm::Constant *getMessageSendSuperFn2() const {
212 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
213 const char *SuperName = "objc_msgSendSuper2";
214 std::vector<const llvm::Type*> Params;
215 Params.push_back(SuperPtrTy);
216 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000217 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000218 Params, true),
219 SuperName);
220 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000221
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000222 llvm::Constant *getMessageSendSuperStretFn() const {
223 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
224 // SEL op, ...)
225 std::vector<const llvm::Type*> Params;
226 Params.push_back(Int8PtrTy);
227 Params.push_back(SuperPtrTy);
228 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000229 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000230 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000231 Params, true),
232 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000233 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000234
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000235 llvm::Constant *getMessageSendSuperStretFn2() const {
236 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
237 // SEL op, ...)
238 std::vector<const llvm::Type*> Params;
239 Params.push_back(Int8PtrTy);
240 Params.push_back(SuperPtrTy);
241 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000242 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000243 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000244 Params, true),
245 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000246 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000247
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000248 llvm::Constant *getMessageSendSuperFpretFn() const {
249 // There is no objc_msgSendSuper_fpret? How can that work?
250 return getMessageSendSuperFn();
251 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000252
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000253 llvm::Constant *getMessageSendSuperFpretFn2() const {
254 // There is no objc_msgSendSuper_fpret? How can that work?
255 return getMessageSendSuperFn2();
256 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000257
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000258protected:
259 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000260
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000261public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000262 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000263 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000264
Daniel Dunbar5d715592008-08-12 05:28:47 +0000265 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
266 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000267
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000268 /// PtrObjectPtrTy - LLVM type for id *
269 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000270
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000271 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000272 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000273 /// ProtocolPtrTy - LLVM type for external protocol handles
274 /// (typeof(Protocol))
275 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000276
Daniel Dunbarc722b852008-08-30 03:02:31 +0000277 // SuperCTy - clang type for struct objc_super.
278 QualType SuperCTy;
279 // SuperPtrCTy - clang type for struct objc_super *.
280 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000281
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000282 /// SuperTy - LLVM type for struct objc_super.
283 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000284 /// SuperPtrTy - LLVM type for struct objc_super *.
285 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000286
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000287 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
288 /// in GCC parlance).
289 const llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000290
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000291 /// PropertyListTy - LLVM type for struct objc_property_list
292 /// (_prop_list_t in GCC parlance).
293 const llvm::StructType *PropertyListTy;
294 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
295 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000296
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000297 // MethodTy - LLVM type for struct objc_method.
298 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000299
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000300 /// CacheTy - LLVM type for struct objc_cache.
301 const llvm::Type *CacheTy;
302 /// CachePtrTy - LLVM type for struct objc_cache *.
303 const llvm::Type *CachePtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000304
Chris Lattnerce8754e2009-04-22 02:44:54 +0000305 llvm::Constant *getGetPropertyFn() {
306 CodeGen::CodeGenTypes &Types = CGM.getTypes();
307 ASTContext &Ctx = CGM.getContext();
308 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCall2da83a32010-02-26 00:48:12 +0000309 llvm::SmallVector<CanQualType,4> Params;
310 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
311 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000312 Params.push_back(IdType);
313 Params.push_back(SelType);
314 Params.push_back(Ctx.LongTy);
315 Params.push_back(Ctx.BoolTy);
316 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000317 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000318 FunctionType::ExtInfo()),
319 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000320 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
321 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000322
Chris Lattnerce8754e2009-04-22 02:44:54 +0000323 llvm::Constant *getSetPropertyFn() {
324 CodeGen::CodeGenTypes &Types = CGM.getTypes();
325 ASTContext &Ctx = CGM.getContext();
326 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCall2da83a32010-02-26 00:48:12 +0000327 llvm::SmallVector<CanQualType,6> Params;
328 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
329 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000330 Params.push_back(IdType);
331 Params.push_back(SelType);
332 Params.push_back(Ctx.LongTy);
333 Params.push_back(IdType);
334 Params.push_back(Ctx.BoolTy);
335 Params.push_back(Ctx.BoolTy);
336 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000337 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000338 FunctionType::ExtInfo()),
339 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
341 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000342
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000343
344 llvm::Constant *getCopyStructFn() {
345 CodeGen::CodeGenTypes &Types = CGM.getTypes();
346 ASTContext &Ctx = CGM.getContext();
347 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
348 llvm::SmallVector<CanQualType,5> Params;
349 Params.push_back(Ctx.VoidPtrTy);
350 Params.push_back(Ctx.VoidPtrTy);
351 Params.push_back(Ctx.LongTy);
352 Params.push_back(Ctx.BoolTy);
353 Params.push_back(Ctx.BoolTy);
354 const llvm::FunctionType *FTy =
355 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
356 FunctionType::ExtInfo()),
357 false);
358 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
359 }
360
Chris Lattnerce8754e2009-04-22 02:44:54 +0000361 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000362 CodeGen::CodeGenTypes &Types = CGM.getTypes();
363 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000364 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000365 llvm::SmallVector<CanQualType,1> Params;
366 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000367 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000368 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000369 FunctionType::ExtInfo()),
370 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000371 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
372 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000373
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000374 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000375 llvm::Constant *getGcReadWeakFn() {
376 // id objc_read_weak (id *)
377 std::vector<const llvm::Type*> Args;
378 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000379 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000380 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000381 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000382 }
383
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000384 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000385 llvm::Constant *getGcAssignWeakFn() {
386 // id objc_assign_weak (id, id *)
387 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
388 Args.push_back(ObjectPtrTy->getPointerTo());
389 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000390 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000391 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
392 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000393
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000394 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000395 llvm::Constant *getGcAssignGlobalFn() {
396 // id objc_assign_global(id, id *)
397 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
398 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000399 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000400 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000401 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
402 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000403
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000404 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000405 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000406 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner0a696a422009-04-22 02:38:11 +0000407 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
408 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000409 Args.push_back(LongTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000410 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000411 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000412 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
413 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000414
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000415 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
416 llvm::Constant *GcMemmoveCollectableFn() {
417 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
418 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
419 Args.push_back(Int8PtrTy);
420 Args.push_back(LongTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000421 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000422 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
423 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000424
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000425 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000426 llvm::Constant *getGcAssignStrongCastFn() {
427 // id objc_assign_global(id, id *)
428 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
429 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000430 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000431 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000432 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
433 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000434
435 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000436 llvm::Constant *getExceptionThrowFn() {
437 // void objc_exception_throw(id)
438 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
439 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000440 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000441 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
442 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000443
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000444 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
445 llvm::Constant *getExceptionRethrowFn() {
446 // void objc_exception_rethrow(void)
447 std::vector<const llvm::Type*> Args;
448 llvm::FunctionType *FTy =
449 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
450 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
451 }
452
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000453 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000454 llvm::Constant *getSyncEnterFn() {
455 // void objc_sync_enter (id)
456 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
457 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000458 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000459 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
460 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000461
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000462 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000463 llvm::Constant *getSyncExitFn() {
464 // void objc_sync_exit (id)
465 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
466 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000467 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000468 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
469 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000470
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000471 llvm::Constant *getSendFn(bool IsSuper) const {
472 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
473 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000474
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000475 llvm::Constant *getSendFn2(bool IsSuper) const {
476 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
477 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000478
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000479 llvm::Constant *getSendStretFn(bool IsSuper) const {
480 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
481 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000482
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000483 llvm::Constant *getSendStretFn2(bool IsSuper) const {
484 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
485 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000486
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000487 llvm::Constant *getSendFpretFn(bool IsSuper) const {
488 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
489 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000490
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000491 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
492 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
493 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000494
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000495 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
496 ~ObjCCommonTypesHelper(){}
497};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000498
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000499/// ObjCTypesHelper - Helper class that encapsulates lazy
500/// construction of varies types used during ObjC generation.
501class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000502public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000503 /// SymtabTy - LLVM type for struct objc_symtab.
504 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000505 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
506 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000507 /// ModuleTy - LLVM type for struct objc_module.
508 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000509
Daniel Dunbarb036db82008-08-13 03:21:16 +0000510 /// ProtocolTy - LLVM type for struct objc_protocol.
511 const llvm::StructType *ProtocolTy;
512 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
513 const llvm::Type *ProtocolPtrTy;
514 /// ProtocolExtensionTy - LLVM type for struct
515 /// objc_protocol_extension.
516 const llvm::StructType *ProtocolExtensionTy;
517 /// ProtocolExtensionTy - LLVM type for struct
518 /// objc_protocol_extension *.
519 const llvm::Type *ProtocolExtensionPtrTy;
520 /// MethodDescriptionTy - LLVM type for struct
521 /// objc_method_description.
522 const llvm::StructType *MethodDescriptionTy;
523 /// MethodDescriptionListTy - LLVM type for struct
524 /// objc_method_description_list.
525 const llvm::StructType *MethodDescriptionListTy;
526 /// MethodDescriptionListPtrTy - LLVM type for struct
527 /// objc_method_description_list *.
528 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000529 /// ProtocolListTy - LLVM type for struct objc_property_list.
530 const llvm::Type *ProtocolListTy;
531 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
532 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000533 /// CategoryTy - LLVM type for struct objc_category.
534 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000535 /// ClassTy - LLVM type for struct objc_class.
536 const llvm::StructType *ClassTy;
537 /// ClassPtrTy - LLVM type for struct objc_class *.
538 const llvm::Type *ClassPtrTy;
539 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
540 const llvm::StructType *ClassExtensionTy;
541 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
542 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000543 // IvarTy - LLVM type for struct objc_ivar.
544 const llvm::StructType *IvarTy;
545 /// IvarListTy - LLVM type for struct objc_ivar_list.
546 const llvm::Type *IvarListTy;
547 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
548 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000549 /// MethodListTy - LLVM type for struct objc_method_list.
550 const llvm::Type *MethodListTy;
551 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
552 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000553
Anders Carlsson9ff22482008-09-09 10:10:21 +0000554 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
555 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000556
Anders Carlsson9ff22482008-09-09 10:10:21 +0000557 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000558 llvm::Constant *getExceptionTryEnterFn() {
559 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000560 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000561 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000562 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000563 Params, false),
564 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000565 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000566
567 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000568 llvm::Constant *getExceptionTryExitFn() {
569 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000570 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000571 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000572 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000573 Params, false),
574 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000575 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000576
577 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000578 llvm::Constant *getExceptionExtractFn() {
579 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000580 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
581 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattnerc6406db2009-04-22 02:26:14 +0000582 Params, false),
583 "objc_exception_extract");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000584
Chris Lattnerc6406db2009-04-22 02:26:14 +0000585 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000586
Anders Carlsson9ff22482008-09-09 10:10:21 +0000587 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000588 llvm::Constant *getExceptionMatchFn() {
589 std::vector<const llvm::Type*> Params;
590 Params.push_back(ClassPtrTy);
591 Params.push_back(ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000592 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000593 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000594 Params, false),
595 "objc_exception_match");
596
Chris Lattnerc6406db2009-04-22 02:26:14 +0000597 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000598
Anders Carlsson9ff22482008-09-09 10:10:21 +0000599 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000600 llvm::Constant *getSetJmpFn() {
601 std::vector<const llvm::Type*> Params;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000602 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000603 return
Owen Anderson41a75022009-08-13 21:57:51 +0000604 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000605 Params, false),
606 "_setjmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000607
Chris Lattnerc6406db2009-04-22 02:26:14 +0000608 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000609
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000610public:
611 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000612 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000613};
614
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000615/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000616/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000617class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000618public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000619
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000620 // MethodListnfABITy - LLVM for struct _method_list_t
621 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000622
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000623 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
624 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000625
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000626 // ProtocolnfABITy = LLVM for struct _protocol_t
627 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000628
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000629 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
630 const llvm::Type *ProtocolnfABIPtrTy;
631
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000632 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
633 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000634
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000635 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
636 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000637
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000638 // ClassnfABITy - LLVM for struct _class_t
639 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000640
Fariborz Jahanian71394042009-01-23 23:53:38 +0000641 // ClassnfABIPtrTy - LLVM for struct _class_t*
642 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000643
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000644 // IvarnfABITy - LLVM for struct _ivar_t
645 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000646
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000647 // IvarListnfABITy - LLVM for struct _ivar_list_t
648 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000649
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000650 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
651 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000652
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000653 // ClassRonfABITy - LLVM for struct _class_ro_t
654 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000655
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000656 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
657 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000658
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000659 // CategorynfABITy - LLVM for struct _category_t
660 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000661
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000662 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000663
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000664 // MessageRefTy - LLVM for:
665 // struct _message_ref_t {
666 // IMP messenger;
667 // SEL name;
668 // };
669 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000670 // MessageRefCTy - clang type for struct _message_ref_t
671 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000672
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000673 // MessageRefPtrTy - LLVM for struct _message_ref_t*
674 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000675 // MessageRefCPtrTy - clang type for struct _message_ref_t*
676 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000677
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000678 // MessengerTy - Type of the messenger (shown as IMP above)
679 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000680
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000681 // SuperMessageRefTy - LLVM for:
682 // struct _super_message_ref_t {
683 // SUPER_IMP messenger;
684 // SEL name;
685 // };
686 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000687
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000688 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
689 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000690
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000691 llvm::Constant *getMessageSendFixupFn() {
692 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
693 std::vector<const llvm::Type*> Params;
694 Params.push_back(ObjectPtrTy);
695 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000696 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000697 Params, true),
698 "objc_msgSend_fixup");
699 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000700
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000701 llvm::Constant *getMessageSendFpretFixupFn() {
702 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
703 std::vector<const llvm::Type*> Params;
704 Params.push_back(ObjectPtrTy);
705 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000706 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000707 Params, true),
708 "objc_msgSend_fpret_fixup");
709 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000710
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000711 llvm::Constant *getMessageSendStretFixupFn() {
712 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
713 std::vector<const llvm::Type*> Params;
714 Params.push_back(ObjectPtrTy);
715 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000716 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000717 Params, true),
718 "objc_msgSend_stret_fixup");
719 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000720
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000721 llvm::Constant *getMessageSendIdFixupFn() {
722 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
723 std::vector<const llvm::Type*> Params;
724 Params.push_back(ObjectPtrTy);
725 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000726 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000727 Params, true),
728 "objc_msgSendId_fixup");
729 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000730
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000731 llvm::Constant *getMessageSendIdStretFixupFn() {
732 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
733 std::vector<const llvm::Type*> Params;
734 Params.push_back(ObjectPtrTy);
735 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000736 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000737 Params, true),
738 "objc_msgSendId_stret_fixup");
739 }
740 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000741 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000742 // struct _super_message_ref_t*, ...)
743 std::vector<const llvm::Type*> Params;
744 Params.push_back(SuperPtrTy);
745 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000746 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000747 Params, true),
748 "objc_msgSendSuper2_fixup");
749 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000750
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000751 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000752 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-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 Anderson9793f0e2009-07-29 22:16:19 +0000757 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000758 Params, true),
759 "objc_msgSendSuper2_stret_fixup");
760 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000761
762
763
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000764 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
765 /// exception personality function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000766 llvm::Value *getEHPersonalityPtr() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000767 llvm::Constant *Personality =
Owen Anderson41a75022009-08-13 21:57:51 +0000768 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerdcceee72009-04-06 16:53:45 +0000769 true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000770 "__objc_personality_v0");
Owen Andersonade90fd2009-07-29 18:54:39 +0000771 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000772 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000773
Chris Lattnera7c00b42009-04-22 02:15:23 +0000774 llvm::Constant *getUnwindResumeOrRethrowFn() {
775 std::vector<const llvm::Type*> Params;
776 Params.push_back(Int8PtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000777 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000778 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000779 Params, false),
Daniel Dunbar3241d402010-02-10 18:49:11 +0000780 (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" :
781 "_Unwind_Resume_or_Rethrow"));
Chris Lattnera7c00b42009-04-22 02:15:23 +0000782 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000783
Chris Lattnera7c00b42009-04-22 02:15:23 +0000784 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson41a75022009-08-13 21:57:51 +0000785 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +0000786 false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000787 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000788
Chris Lattnera7c00b42009-04-22 02:15:23 +0000789 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000790
Chris Lattnera7c00b42009-04-22 02:15:23 +0000791 llvm::Constant *getObjCBeginCatchFn() {
792 std::vector<const llvm::Type*> Params;
793 Params.push_back(Int8PtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000794 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattnera7c00b42009-04-22 02:15:23 +0000795 Params, false),
796 "objc_begin_catch");
797 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000798
799 const llvm::StructType *EHTypeTy;
800 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000801
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000802 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
803 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000804};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000805
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000806class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000807public:
808 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000809 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000810 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000811 unsigned ivar_bytepos;
812 unsigned ivar_size;
813 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000814 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000815
816 // Allow sorting based on byte pos.
817 bool operator<(const GC_IVAR &b) const {
818 return ivar_bytepos < b.ivar_bytepos;
819 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000820 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000821
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000822 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000823 public:
824 unsigned skip;
825 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000826 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000827 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000828 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000829
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000830protected:
831 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000832 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000833 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000834 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000835
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000836 // gc ivar layout bitmap calculation helper caches.
837 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
838 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000839
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000840 /// LazySymbols - Symbols to generate a lazy reference for. See
841 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000842 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000843
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000844 /// DefinedSymbols - External symbols which are defined by this
845 /// module. The symbols in this list and LazySymbols are used to add
846 /// special linker symbols which ensure that Objective-C modules are
847 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000848 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000849
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000850 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000851 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000852
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000853 /// MethodVarNames - uniqued method variable names.
854 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000855
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000856 /// DefinedCategoryNames - list of category names in form Class_Category.
857 llvm::SetVector<std::string> DefinedCategoryNames;
858
Daniel Dunbarb036db82008-08-13 03:21:16 +0000859 /// MethodVarTypes - uniqued method type signatures. We have to use
860 /// a StringMap here because have no other unique reference.
861 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000862
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000863 /// MethodDefinitions - map of methods which have been defined in
864 /// this translation unit.
865 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000866
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000867 /// PropertyNames - uniqued method variable names.
868 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000869
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000870 /// ClassReferences - uniqued class references.
871 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000872
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000873 /// SelectorReferences - uniqued selector references.
874 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000875
Daniel Dunbarb036db82008-08-13 03:21:16 +0000876 /// Protocols - Protocols for which an objc_protocol structure has
877 /// been emitted. Forward declarations are handled by creating an
878 /// empty structure whose initializer is filled in when/if defined.
879 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000880
Daniel Dunbarc475d422008-10-29 22:36:39 +0000881 /// DefinedProtocols - Protocols which have actually been
882 /// defined. We should not need this, see FIXME in GenerateProtocol.
883 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000884
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000885 /// DefinedClasses - List of defined classes.
886 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000887
888 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
889 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000890
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000891 /// DefinedCategories - List of defined categories.
892 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000893
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000894 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
895 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000896
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000897 /// GetNameForMethod - Return a name for the given method.
898 /// \param[out] NameOut - The return value.
899 void GetNameForMethod(const ObjCMethodDecl *OMD,
900 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000901 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000902
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000903 /// GetMethodVarName - Return a unique constant for the given
904 /// selector's name. The return value has type char *.
905 llvm::Constant *GetMethodVarName(Selector Sel);
906 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
907 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000908
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000909 /// GetMethodVarType - Return a unique constant for the given
910 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000911
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000912 // FIXME: This is a horrible name.
913 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000914 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000915
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000916 /// GetPropertyName - Return a unique constant for the given
917 /// name. The return value has type char *.
918 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000919
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000920 // FIXME: This can be dropped once string functions are unified.
921 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
922 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000923
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000924 /// GetClassName - Return a unique constant for the given selector's
925 /// name. The return value has type char *.
926 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000927
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000928 /// BuildIvarLayout - Builds ivar layout bitmap for the class
929 /// implementation for the __strong or __weak case.
930 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000931 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
932 bool ForStrongLayout);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000933
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000934 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000935 unsigned int BytePos, bool ForStrongLayout,
936 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000937 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000938 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000939 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000940 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000941 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000942 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000943
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000944 /// GetIvarLayoutName - Returns a unique constant for the given
945 /// ivar layout bitmap.
946 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
947 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000948
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000949 /// EmitPropertyList - Emit the given property list. The return
950 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000951 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000952 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000953 const ObjCContainerDecl *OCD,
954 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000955
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000956 /// PushProtocolProperties - Push protocol's property on the input stack.
957 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
958 std::vector<llvm::Constant*> &Properties,
959 const Decl *Container,
960 const ObjCProtocolDecl *PROTO,
961 const ObjCCommonTypesHelper &ObjCTypes);
962
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000963 /// GetProtocolRef - Return a reference to the internal protocol
964 /// description, creating an empty one if it has not been
965 /// defined. The return value has type ProtocolPtrTy.
966 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000967
Daniel Dunbar30c65362009-03-09 20:09:19 +0000968 /// CreateMetadataVar - Create a global variable with internal
969 /// linkage for use by the Objective-C runtime.
970 ///
971 /// This is a convenience wrapper which not only creates the
972 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000973 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000974 ///
975 /// \param Name - The variable name.
976 /// \param Init - The variable initializer; this is also used to
977 /// define the type of the variable.
978 /// \param Section - The section the variable should go into, or 0.
979 /// \param Align - The alignment for the variable, or 0.
980 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000981 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000982 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000983 llvm::Constant *Init,
984 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000985 unsigned Align,
986 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000987
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000988 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +0000989 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000990 QualType ResultType,
991 llvm::Value *Sel,
992 llvm::Value *Arg0,
993 QualType Arg0Ty,
994 bool IsSuper,
995 const CallArgList &CallArgs,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +0000996 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000997 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000998
Daniel Dunbar5e639272010-04-25 20:39:01 +0000999 /// EmitImageInfo - Emit the image info marker used to encode some module
1000 /// level information.
1001 void EmitImageInfo();
1002
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001003public:
Owen Andersonae86c192009-07-13 04:10:07 +00001004 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +00001005 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001006
David Chisnall481e3a82010-01-23 02:40:42 +00001007 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001008
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001009 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1010 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001011
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001012 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001013
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001014 /// GetOrEmitProtocol - Get the protocol object for the given
1015 /// declaration, emitting it if necessary. The return value has type
1016 /// ProtocolPtrTy.
1017 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001018
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001019 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1020 /// object for the given declaration, emitting it if needed. These
1021 /// forward references will be filled in with empty bodies if no
1022 /// definition is seen. The return value has type ProtocolPtrTy.
1023 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001024};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001025
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001026class CGObjCMac : public CGObjCCommonMac {
1027private:
1028 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001029
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001030 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001031 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001032 void EmitModuleInfo();
1033
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001034 /// EmitModuleSymols - Emit module symbols, the list of defined
1035 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001036 llvm::Constant *EmitModuleSymbols();
1037
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001038 /// FinishModule - Write out global data structures at the end of
1039 /// processing a translation unit.
1040 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001041
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001042 /// EmitClassExtension - Generate the class extension structure used
1043 /// to store the weak ivar layout and properties. The return value
1044 /// has type ClassExtensionPtrTy.
1045 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1046
1047 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1048 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001049 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001050 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001051
1052 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1053 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001054
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001055 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001056 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001057 QualType ResultType,
1058 Selector Sel,
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001059 llvm::Value *Arg0,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001060 QualType Arg0Ty,
1061 bool IsSuper,
1062 const CallArgList &CallArgs);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001063
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001064 /// EmitIvarList - Emit the ivar list for the given
1065 /// implementation. If ForClass is true the list of class ivars
1066 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1067 /// interface ivars will be emitted. The return value has type
1068 /// IvarListPtrTy.
1069 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001070 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001071
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001072 /// EmitMetaClass - Emit a forward reference to the class structure
1073 /// for the metaclass of the given interface. The return value has
1074 /// type ClassPtrTy.
1075 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1076
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001077 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001078 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001079 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1080 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001081 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001082
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001083 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001084
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001085 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001086
1087 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001088 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001089 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001090 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001091 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001092
1093 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001094 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001095 /// - TypeName: The name for the type containing the methods.
1096 /// - IsProtocol: True iff these methods are for a protocol.
1097 /// - ClassMethds: True iff these are class methods.
1098 /// - Required: When true, only "required" methods are
1099 /// listed. Similarly, when false only "optional" methods are
1100 /// listed. For classes this should always be true.
1101 /// - begin, end: The method list to output.
1102 ///
1103 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001104 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001105 const char *Section,
1106 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001107
Daniel Dunbarc475d422008-10-29 22:36:39 +00001108 /// GetOrEmitProtocol - Get the protocol object for the given
1109 /// declaration, emitting it if necessary. The return value has type
1110 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001111 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001112
1113 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1114 /// object for the given declaration, emitting it if needed. These
1115 /// forward references will be filled in with empty bodies if no
1116 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001117 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001118
Daniel Dunbarb036db82008-08-13 03:21:16 +00001119 /// EmitProtocolExtension - Generate the protocol extension
1120 /// structure used to store optional instance and class methods, and
1121 /// protocol properties. The return value has type
1122 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001123 llvm::Constant *
1124 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1125 const ConstantVector &OptInstanceMethods,
1126 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001127
1128 /// EmitProtocolList - Generate the list of referenced
1129 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001130 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001131 ObjCProtocolDecl::protocol_iterator begin,
1132 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001133
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001134 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1135 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001136 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1137 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001138
1139public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001140 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001141
Fariborz Jahanian71394042009-01-23 23:53:38 +00001142 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001143
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001144 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001145 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001146 QualType ResultType,
1147 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001148 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001149 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001150 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001151 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001152
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001153 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001154 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001155 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001156 QualType ResultType,
1157 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001158 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001159 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001160 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001161 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001162 const CallArgList &CallArgs,
1163 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001164
Daniel Dunbarcb463852008-11-01 01:53:16 +00001165 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001166 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001167
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001168 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1169 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001170
1171 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1172 /// untyped one.
1173 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1174 const ObjCMethodDecl *Method);
1175
Daniel Dunbar92992502008-08-15 22:20:32 +00001176 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001177
Daniel Dunbar92992502008-08-15 22:20:32 +00001178 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001179
Daniel Dunbarcb463852008-11-01 01:53:16 +00001180 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001181 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001182
Chris Lattnerd4808922009-03-22 21:03:39 +00001183 virtual llvm::Constant *GetPropertyGetFunction();
1184 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001185 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001186 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001187
John McCallbd309292010-07-06 01:34:17 +00001188 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1189 const ObjCAtTryStmt &S);
1190 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1191 const ObjCAtSynchronizedStmt &S);
1192 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001193 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1194 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001195 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001196 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001197 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001198 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001199 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
1200 llvm::Value *src, llvm::Value *dest);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001201 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001202 llvm::Value *src, llvm::Value *dest,
1203 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001204 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1205 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001206 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1207 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001208 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001209
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001210 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1211 QualType ObjectTy,
1212 llvm::Value *BaseValue,
1213 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001214 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001215 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001216 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001217 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001218};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001219
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001220class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001221private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001222 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001223 llvm::GlobalVariable* ObjCEmptyCacheVar;
1224 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001225
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001226 /// SuperClassReferences - uniqued super class references.
1227 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001228
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001229 /// MetaClassReferences - uniqued meta class references.
1230 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001231
1232 /// EHTypeReferences - uniqued class ehtype references.
1233 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001234
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001235 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001236 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001237 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001238
Fariborz Jahanian67260552009-11-17 21:37:35 +00001239 /// DefinedMetaClasses - List of defined meta-classes.
1240 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1241
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001242 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001243 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001244 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001245
Fariborz Jahanian71394042009-01-23 23:53:38 +00001246 /// FinishNonFragileABIModule - Write out global data structures at the end of
1247 /// processing a translation unit.
1248 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001249
Daniel Dunbar19573e72009-05-15 21:48:48 +00001250 /// AddModuleClassList - Add the given list of class pointers to the
1251 /// module with the provided symbol and section names.
1252 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1253 const char *SymbolName,
1254 const char *SectionName);
1255
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001256 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1257 unsigned InstanceStart,
1258 unsigned InstanceSize,
1259 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001260 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001261 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001262 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001263 llvm::Constant *ClassRoGV,
1264 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001265
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001266 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001267
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001268 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001269
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001270 /// EmitMethodList - Emit the method list for the given
1271 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001272 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001273 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001274 const ConstantVector &Methods);
1275 /// EmitIvarList - Emit the ivar list for the given
1276 /// implementation. If ForClass is true the list of class ivars
1277 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1278 /// interface ivars will be emitted. The return value has type
1279 /// IvarListnfABIPtrTy.
1280 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001281
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001282 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001283 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001284 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001285
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001286 /// GetOrEmitProtocol - Get the protocol object for the given
1287 /// declaration, emitting it if necessary. The return value has type
1288 /// ProtocolPtrTy.
1289 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001290
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001291 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1292 /// object for the given declaration, emitting it if needed. These
1293 /// forward references will be filled in with empty bodies if no
1294 /// definition is seen. The return value has type ProtocolPtrTy.
1295 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001296
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001297 /// EmitProtocolList - Generate the list of referenced
1298 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001299 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001300 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001301 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001302
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001303 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001304 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001305 QualType ResultType,
1306 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001307 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001308 QualType Arg0Ty,
1309 bool IsSuper,
1310 const CallArgList &CallArgs);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001311
1312 /// GetClassGlobal - Return the global variable for the Objective-C
1313 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001314 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001315
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001316 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001317 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001318 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001319 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001320
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001321 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1322 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001323 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1324 const ObjCInterfaceDecl *ID);
1325
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001326 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1327 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001328 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001329 const ObjCInterfaceDecl *ID);
1330
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001331 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1332 /// the given ivar.
1333 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001334 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001335 const ObjCInterfaceDecl *ID,
1336 const ObjCIvarDecl *Ivar);
1337
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001338 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1339 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001340 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1341 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001342
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001343 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001344 /// interface. The return value has type EHTypePtrTy.
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001345 llvm::Value *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
1346 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001347
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001348 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001349 return "OBJC_METACLASS_$_";
1350 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001351
Daniel Dunbar15894b72009-04-07 05:48:37 +00001352 const char *getClassSymbolPrefix() const {
1353 return "OBJC_CLASS_$_";
1354 }
1355
Daniel Dunbar961202372009-05-03 12:57:56 +00001356 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001357 uint32_t &InstanceStart,
1358 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001359
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001360 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001361 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001362 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1363 return CGM.getContext().Selectors.getSelector(0, &II);
1364 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001365
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001366 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001367 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1368 return CGM.getContext().Selectors.getSelector(1, &II);
1369 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001370
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001371 /// ImplementationIsNonLazy - Check whether the given category or
1372 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001373 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001374
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001375public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001376 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001377 // FIXME. All stubs for now!
1378 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001379
Fariborz Jahanian71394042009-01-23 23:53:38 +00001380 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001381 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001382 QualType ResultType,
1383 Selector Sel,
1384 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001385 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001386 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001387 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001388
1389 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001390 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001391 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001392 QualType ResultType,
1393 Selector Sel,
1394 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001395 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001396 llvm::Value *Receiver,
1397 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001398 const CallArgList &CallArgs,
1399 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001400
Fariborz Jahanian71394042009-01-23 23:53:38 +00001401 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001402 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001403
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001404 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1405 bool lvalue = false)
1406 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001407
1408 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1409 /// untyped one.
1410 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1411 const ObjCMethodDecl *Method)
1412 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001413
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001414 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001415
Fariborz Jahanian71394042009-01-23 23:53:38 +00001416 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001417 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001418 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001419
1420 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001421 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001422 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001423 virtual llvm::Constant *GetPropertySetFunction() {
1424 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001425 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001426
1427 virtual llvm::Constant *GetCopyStructFunction() {
1428 return ObjCTypes.getCopyStructFn();
1429 }
1430
Chris Lattnerd4808922009-03-22 21:03:39 +00001431 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001432 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001433 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001434
John McCallbd309292010-07-06 01:34:17 +00001435 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1436 const ObjCAtTryStmt &S);
1437 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1438 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001439 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001440 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001441 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001442 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001443 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001444 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001445 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001446 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001447 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001448 llvm::Value *src, llvm::Value *dest,
1449 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001450 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001451 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001452 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1453 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001454 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001455 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1456 QualType ObjectTy,
1457 llvm::Value *BaseValue,
1458 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001459 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001460 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001461 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001462 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001463};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001464
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001465} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001466
1467/* *** Helper Functions *** */
1468
1469/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001470static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001471 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001472 unsigned idx0,
1473 unsigned idx1) {
1474 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001475 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1476 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001477 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001478 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001479}
1480
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001481/// hasObjCExceptionAttribute - Return true if this class or any super
1482/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001483static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001484 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001485 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001486 return true;
1487 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001488 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001489 return false;
1490}
1491
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001492/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001493
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001494CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001495 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001496 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001497 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001498}
1499
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001500/// GetClass - Return a reference to the class for the given interface
1501/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001502llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001503 const ObjCInterfaceDecl *ID) {
1504 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001505}
1506
1507/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001508llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1509 bool lval) {
1510 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001511}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001512llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001513 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001514 return EmitSelector(Builder, Method->getSelector());
1515}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001516
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001517/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001518/*
1519 struct __builtin_CFString {
1520 const int *isa; // point to __CFConstantStringClassReference
1521 int flags;
1522 const char *str;
1523 long length;
1524 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001525*/
1526
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001527/// or Generate a constant NSString object.
1528/*
1529 struct __builtin_NSString {
1530 const int *isa; // point to __NSConstantStringClassReference
1531 const char *str;
1532 unsigned int length;
1533 };
1534*/
1535
Fariborz Jahanian71394042009-01-23 23:53:38 +00001536llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001537 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001538 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1539 CGM.GetAddrOfConstantCFString(SL) :
1540 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001541}
1542
1543/// Generates a message send where the super is the receiver. This is
1544/// a message send to self with special delivery semantics indicating
1545/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001546CodeGen::RValue
1547CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001548 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001549 QualType ResultType,
1550 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001551 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001552 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001553 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001554 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001555 const CodeGen::CallArgList &CallArgs,
1556 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001557 // Create and init a super structure; this is a (receiver, class)
1558 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001559 llvm::Value *ObjCSuper =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001560 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001561 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001562 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001563 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001564 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001565
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001566 // If this is a class message the metaclass is passed as the target.
1567 llvm::Value *Target;
1568 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001569 if (isCategoryImpl) {
1570 // Message sent to 'super' in a class method defined in a category
1571 // implementation requires an odd treatment.
1572 // If we are in a class method, we must retrieve the
1573 // _metaclass_ for the current class, pointed at by
1574 // the class's "isa" pointer. The following assumes that
1575 // isa" is the first ivar in a class (which it must be).
1576 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1577 Target = CGF.Builder.CreateStructGEP(Target, 0);
1578 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001579 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001580 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1581 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1582 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1583 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001584 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001585 }
1586 else if (isCategoryImpl)
1587 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1588 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001589 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1590 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1591 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001592 }
Mike Stump18bb9282009-05-16 07:57:57 +00001593 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1594 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001595 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001596 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001597 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001598 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001599 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001600 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001601 EmitSelector(CGF.Builder, Sel),
1602 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001603 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001604}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001605
1606/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001607CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001608 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001609 QualType ResultType,
1610 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001611 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001612 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001613 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001614 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001615 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001616 EmitSelector(CGF.Builder, Sel),
1617 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001618 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001619}
1620
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001621CodeGen::RValue
1622CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001623 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001624 QualType ResultType,
1625 llvm::Value *Sel,
1626 llvm::Value *Arg0,
1627 QualType Arg0Ty,
1628 bool IsSuper,
1629 const CallArgList &CallArgs,
1630 const ObjCMethodDecl *Method,
1631 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001632 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001633 if (!IsSuper)
1634 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001635 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001636 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001637 CGF.getContext().getObjCSelType()));
1638 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001639
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001640 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001641 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001642 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001643 const llvm::FunctionType *FTy =
1644 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001645
Anders Carlsson280e61f12010-06-21 20:59:55 +00001646 if (Method)
1647 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1648 CGM.getContext().getCanonicalType(ResultType) &&
1649 "Result type mismatch!");
1650
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001651 llvm::Constant *Fn = NULL;
Daniel Dunbard931a872009-02-02 22:03:45 +00001652 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001653 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001654 : ObjCTypes.getSendStretFn(IsSuper);
Douglas Gregor49b4d732010-06-22 23:07:26 +00001655 } else if (ResultType->isRealFloatingType()) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001656 if (ObjCABI == 2) {
John McCall9dd450b2009-09-21 23:43:11 +00001657 if (const BuiltinType *BT = ResultType->getAs<BuiltinType>()) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001658 BuiltinType::Kind k = BT->getKind();
1659 Fn = (k == BuiltinType::LongDouble) ? ObjCTypes.getSendFpretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001660 : ObjCTypes.getSendFn2(IsSuper);
Daniel Dunbar76d864c2009-06-10 04:38:50 +00001661 } else {
1662 Fn = ObjCTypes.getSendFn2(IsSuper);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001663 }
Mike Stump658fe022009-07-30 22:28:39 +00001664 } else
Mike Stump18bb9282009-05-16 07:57:57 +00001665 // FIXME. This currently matches gcc's API for x86-32. May need to change
1666 // for others if we have their API.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001667 Fn = ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001668 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001669 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001670 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001671 }
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001672 assert(Fn && "EmitLegacyMessageSend - unknown API");
Owen Andersonade90fd2009-07-29 18:54:39 +00001673 Fn = llvm::ConstantExpr::getBitCast(Fn,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001674 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001675 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001676}
1677
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001678llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001679 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001680 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001681 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001682 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1683
Owen Andersonade90fd2009-07-29 18:54:39 +00001684 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001685 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001686}
1687
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001688void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001689 // FIXME: We shouldn't need this, the protocol decl should contain enough
1690 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001691 DefinedProtocols.insert(PD->getIdentifier());
1692
1693 // If we have generated a forward reference to this protocol, emit
1694 // it now. Otherwise do nothing, the protocol objects are lazily
1695 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001696 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001697 GetOrEmitProtocol(PD);
1698}
1699
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001700llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001701 if (DefinedProtocols.count(PD->getIdentifier()))
1702 return GetOrEmitProtocol(PD);
1703 return GetOrEmitProtocolRef(PD);
1704}
1705
Daniel Dunbarb036db82008-08-13 03:21:16 +00001706/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001707// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1708struct _objc_protocol {
1709struct _objc_protocol_extension *isa;
1710char *protocol_name;
1711struct _objc_protocol_list *protocol_list;
1712struct _objc__method_prototype_list *instance_methods;
1713struct _objc__method_prototype_list *class_methods
1714};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001715
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001716See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001717*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001718llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1719 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1720
1721 // Early exit if a defining object has already been generated.
1722 if (Entry && Entry->hasInitializer())
1723 return Entry;
1724
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001725 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001726 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001727 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1728
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001729 // Construct method lists.
1730 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1731 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001732 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001733 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001734 ObjCMethodDecl *MD = *i;
1735 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1736 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1737 OptInstanceMethods.push_back(C);
1738 } else {
1739 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001740 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001741 }
1742
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001743 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001744 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001745 ObjCMethodDecl *MD = *i;
1746 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1747 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1748 OptClassMethods.push_back(C);
1749 } else {
1750 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001751 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001752 }
1753
Daniel Dunbarb036db82008-08-13 03:21:16 +00001754 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001755 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001756 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001757 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001758 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001759 PD->protocol_begin(),
1760 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001761 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001762 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001763 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1764 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001765 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001766 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001767 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1768 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001769 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001770 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001771
Daniel Dunbarb036db82008-08-13 03:21:16 +00001772 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001773 // Already created, fix the linkage and update the initializer.
1774 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001775 Entry->setInitializer(Init);
1776 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001777 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001778 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001779 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001780 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001781 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001782 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001783 // FIXME: Is this necessary? Why only for protocol?
1784 Entry->setAlignment(4);
1785 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001786 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001787
1788 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001789}
1790
Daniel Dunbarc475d422008-10-29 22:36:39 +00001791llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001792 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1793
1794 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001795 // We use the initializer as a marker of whether this is a forward
1796 // reference or not. At module finalization we add the empty
1797 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001798 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001799 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001800 llvm::GlobalValue::ExternalLinkage,
1801 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001802 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001803 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001804 // FIXME: Is this necessary? Why only for protocol?
1805 Entry->setAlignment(4);
1806 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001807
Daniel Dunbarb036db82008-08-13 03:21:16 +00001808 return Entry;
1809}
1810
1811/*
1812 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001813 uint32_t size;
1814 struct objc_method_description_list *optional_instance_methods;
1815 struct objc_method_description_list *optional_class_methods;
1816 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001817 };
1818*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001819llvm::Constant *
1820CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1821 const ConstantVector &OptInstanceMethods,
1822 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001823 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001824 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001825 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001826 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001827 Values[1] =
1828 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001829 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001830 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1831 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001832 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001833 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001834 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1835 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001836 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001837 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001838
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001839 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001840 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001841 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001842 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001843
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001844 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001845 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001846
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001847 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001848 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001849 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001850 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001851}
1852
1853/*
1854 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001855 struct objc_protocol_list *next;
1856 long count;
1857 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001858 };
1859*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001860llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001861CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001862 ObjCProtocolDecl::protocol_iterator begin,
1863 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001864 std::vector<llvm::Constant*> ProtocolRefs;
1865
Daniel Dunbardec75f82008-08-21 21:57:41 +00001866 for (; begin != end; ++begin)
1867 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001868
1869 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001870 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001871 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001872
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001873 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001874 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001875
1876 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001877 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001878 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001879 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001880 ProtocolRefs.size() - 1);
1881 Values[2] =
1882 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1883 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001884 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001885
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001886 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001887 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001888 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001889 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001890 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001891}
1892
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001893void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1894 std::vector<llvm::Constant*> &Properties,
1895 const Decl *Container,
1896 const ObjCProtocolDecl *PROTO,
1897 const ObjCCommonTypesHelper &ObjCTypes) {
1898 std::vector<llvm::Constant*> Prop(2);
1899 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1900 E = PROTO->protocol_end(); P != E; ++P)
1901 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1902 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1903 E = PROTO->prop_end(); I != E; ++I) {
1904 const ObjCPropertyDecl *PD = *I;
1905 if (!PropertySet.insert(PD->getIdentifier()))
1906 continue;
1907 Prop[0] = GetPropertyName(PD->getIdentifier());
1908 Prop[1] = GetPropertyTypeString(PD, Container);
1909 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1910 }
1911}
1912
Daniel Dunbarb036db82008-08-13 03:21:16 +00001913/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001914 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001915 const char * const name;
1916 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001917 };
1918
1919 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001920 uint32_t entsize; // sizeof (struct _objc_property)
1921 uint32_t prop_count;
1922 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001923 };
1924*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001925llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001926 const Decl *Container,
1927 const ObjCContainerDecl *OCD,
1928 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001929 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001930 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001931 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1932 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001933 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001934 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001935 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00001936 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001937 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001938 Prop));
1939 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001940 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001941 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
1942 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001943 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1944 ObjCTypes);
1945 }
1946 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1947 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1948 E = CD->protocol_end(); P != E; ++P)
1949 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1950 ObjCTypes);
1951 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001952
1953 // Return null for empty list.
1954 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001955 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001956
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001957 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001958 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001959 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001960 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1961 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001962 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001963 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001964 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001965 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001966
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001967 llvm::GlobalVariable *GV =
1968 CreateMetadataVar(Name, Init,
1969 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001970 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001971 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001972 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00001973 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001974}
1975
1976/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00001977 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001978 int count;
1979 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001980 };
1981*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001982llvm::Constant *
1983CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1984 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00001985 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001986 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1987 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001988 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001989 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001990 Desc);
1991}
Daniel Dunbarb036db82008-08-13 03:21:16 +00001992
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001993llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001994 const char *Section,
1995 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001996 // Return null for empty list.
1997 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001998 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001999
2000 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002001 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002002 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002003 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002004 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002005 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002006
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002007 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002008 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002009 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002010}
2011
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002012/*
2013 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002014 char *category_name;
2015 char *class_name;
2016 struct _objc_method_list *instance_methods;
2017 struct _objc_method_list *class_methods;
2018 struct _objc_protocol_list *protocols;
2019 uint32_t size; // <rdar://4585769>
2020 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002021 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002022*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002023void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002024 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002025
Mike Stump18bb9282009-05-16 07:57:57 +00002026 // FIXME: This is poor design, the OCD should have a pointer to the category
2027 // decl. Additionally, note that Category can be null for the @implementation
2028 // w/o an @interface case. Sema should just create one for us as it does for
2029 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002030 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002031 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002032 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002033
2034 llvm::SmallString<256> ExtName;
2035 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2036 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002037
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002038 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002039 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002040 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002041 // Instance methods should always be defined.
2042 InstanceMethods.push_back(GetMethodConstant(*i));
2043 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002044 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002045 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002046 // Class methods should always be defined.
2047 ClassMethods.push_back(GetMethodConstant(*i));
2048 }
2049
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002050 std::vector<llvm::Constant*> Values(7);
2051 Values[0] = GetClassName(OCD->getIdentifier());
2052 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002053 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002054 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002055 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002056 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002057 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002058 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002059 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002060 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002061 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002062 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002063 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002064 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002065 Category->protocol_begin(),
2066 Category->protocol_end());
2067 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002068 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002069 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002070 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002071
2072 // If there is no category @interface then there can be no properties.
2073 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002074 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002075 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002076 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002077 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002078 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002079
Owen Anderson0e0189d2009-07-27 22:29:56 +00002080 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002081 Values);
2082
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002083 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002084 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002085 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002086 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002087 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002088 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002089}
2090
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002091// FIXME: Get from somewhere?
2092enum ClassFlags {
2093 eClassFlags_Factory = 0x00001,
2094 eClassFlags_Meta = 0x00002,
2095 // <rdr://5142207>
2096 eClassFlags_HasCXXStructors = 0x02000,
2097 eClassFlags_Hidden = 0x20000,
2098 eClassFlags_ABI2_Hidden = 0x00010,
2099 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2100};
2101
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002102/*
2103 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002104 Class isa;
2105 Class super_class;
2106 const char *name;
2107 long version;
2108 long info;
2109 long instance_size;
2110 struct _objc_ivar_list *ivars;
2111 struct _objc_method_list *methods;
2112 struct _objc_cache *cache;
2113 struct _objc_protocol_list *protocols;
2114 // Objective-C 1.0 extensions (<rdr://4585769>)
2115 const char *ivar_layout;
2116 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002117 };
2118
2119 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002120*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002121void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002122 DefinedSymbols.insert(ID->getIdentifier());
2123
Chris Lattner86d7d912008-11-24 03:54:41 +00002124 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002125 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002126 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002127 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002128 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002129 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00002130 Interface->protocol_begin(),
2131 Interface->protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002132 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002133 if (ID->getNumIvarInitializers())
2134 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002135 unsigned Size =
Daniel Dunbar12119b92009-05-03 10:46:44 +00002136 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002137
2138 // FIXME: Set CXX-structors flag.
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002139 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002140 Flags |= eClassFlags_Hidden;
2141
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002142 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002143 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002144 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002145 // Instance methods should always be defined.
2146 InstanceMethods.push_back(GetMethodConstant(*i));
2147 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002148 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002149 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002150 // Class methods should always be defined.
2151 ClassMethods.push_back(GetMethodConstant(*i));
2152 }
2153
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002154 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002155 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002156 ObjCPropertyImplDecl *PID = *i;
2157
2158 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2159 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2160
2161 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2162 if (llvm::Constant *C = GetMethodConstant(MD))
2163 InstanceMethods.push_back(C);
2164 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2165 if (llvm::Constant *C = GetMethodConstant(MD))
2166 InstanceMethods.push_back(C);
2167 }
2168 }
2169
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002170 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002171 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002172 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002173 // Record a reference to the super class.
2174 LazySymbols.insert(Super->getIdentifier());
2175
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002176 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002177 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002178 ObjCTypes.ClassPtrTy);
2179 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002180 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002181 }
2182 Values[ 2] = GetClassName(ID->getIdentifier());
2183 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002184 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2185 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2186 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002187 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002188 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002189 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002190 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002191 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002192 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002193 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002194 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002195 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002196 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002197 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002198 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002199 std::string Name("\01L_OBJC_CLASS_");
2200 Name += ClassName;
2201 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2202 // Check for a forward reference.
2203 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2204 if (GV) {
2205 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2206 "Forward metaclass reference has incorrect type.");
2207 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2208 GV->setInitializer(Init);
2209 GV->setSection(Section);
2210 GV->setAlignment(4);
2211 CGM.AddUsedGlobal(GV);
2212 }
2213 else
2214 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002215 DefinedClasses.push_back(GV);
2216}
2217
2218llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2219 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002220 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002221 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002222 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002223
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002224 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002225 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002226
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002227 std::vector<llvm::Constant*> Values(12);
2228 // The isa for the metaclass is the root of the hierarchy.
2229 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2230 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2231 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002232 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002233 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002234 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002235 // The super class for the metaclass is emitted as the name of the
2236 // super class. The runtime fixes this up to point to the
2237 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002238 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002239 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002240 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002241 ObjCTypes.ClassPtrTy);
2242 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002243 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002244 }
2245 Values[ 2] = GetClassName(ID->getIdentifier());
2246 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002247 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2248 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2249 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002250 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002251 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002252 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002253 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002254 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002255 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002256 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002257 Values[ 9] = Protocols;
2258 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002259 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002260 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002261 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002262 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002263 Values);
2264
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002265 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002266 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002267
2268 // Check for a forward reference.
2269 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2270 if (GV) {
2271 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2272 "Forward metaclass reference has incorrect type.");
2273 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2274 GV->setInitializer(Init);
2275 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002276 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002277 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002278 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002279 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002280 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002281 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002282 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002283
2284 return GV;
2285}
2286
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002287llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002288 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002289
Mike Stump18bb9282009-05-16 07:57:57 +00002290 // FIXME: Should we look these up somewhere other than the module. Its a bit
2291 // silly since we only generate these while processing an implementation, so
2292 // exactly one pointer would work if know when we entered/exitted an
2293 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002294
2295 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002296 // Previously, metaclass with internal linkage may have been defined.
2297 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002298 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2299 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002300 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2301 "Forward metaclass reference has incorrect type.");
2302 return GV;
2303 } else {
2304 // Generate as an external reference to keep a consistent
2305 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002306 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002307 llvm::GlobalValue::ExternalLinkage,
2308 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002309 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002310 }
2311}
2312
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002313llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2314 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2315
2316 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2317 true)) {
2318 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2319 "Forward class metadata reference has incorrect type.");
2320 return GV;
2321 } else {
2322 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2323 llvm::GlobalValue::ExternalLinkage,
2324 0,
2325 Name);
2326 }
2327}
2328
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002329/*
2330 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002331 uint32_t size;
2332 const char *weak_ivar_layout;
2333 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002334 };
2335*/
2336llvm::Constant *
2337CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002338 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002339 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002340
2341 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002342 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002343 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002344 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002345 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002346
2347 // Return null if no extension bits are used.
2348 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002349 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002350
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002351 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002352 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002353 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002354 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002355 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002356}
2357
2358/*
2359 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002360 char *ivar_name;
2361 char *ivar_type;
2362 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002363 };
2364
2365 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002366 int ivar_count;
2367 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002368 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002369*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002370llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002371 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002372 std::vector<llvm::Constant*> Ivars, Ivar(3);
2373
2374 // When emitting the root class GCC emits ivar entries for the
2375 // actual class structure. It is not clear if we need to follow this
2376 // behavior; for now lets try and get away with not doing it. If so,
2377 // the cleanest solution would be to make up an ObjCInterfaceDecl
2378 // for the class.
2379 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002380 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002381
2382 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002383 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002384
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002385 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002386 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002387
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002388 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2389 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002390 // Ignore unnamed bit-fields.
2391 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002392 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002393 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2394 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002395 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002396 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002397 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002398 }
2399
2400 // Return null for empty list.
2401 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002402 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002403
2404 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002405 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002406 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002407 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002408 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002409 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002410
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002411 llvm::GlobalVariable *GV;
2412 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002413 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002414 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002415 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002416 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002417 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002418 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002419 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002420 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002421}
2422
2423/*
2424 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002425 SEL method_name;
2426 char *method_types;
2427 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002428 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002429
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002430 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002431 struct objc_method_list *obsolete;
2432 int count;
2433 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002434 };
2435*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002436
2437/// GetMethodConstant - Return a struct objc_method constant for the
2438/// given method if it has been defined. The result is null if the
2439/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002440llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002441 // FIXME: Use DenseMap::lookup
2442 llvm::Function *Fn = MethodDefinitions[MD];
2443 if (!Fn)
2444 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002445
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002446 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002447 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002448 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002449 ObjCTypes.SelectorPtrTy);
2450 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002451 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002452 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002453}
2454
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002455llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002456 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002457 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002458 // Return null for empty list.
2459 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002460 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002461
2462 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002463 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002464 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002465 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002466 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002467 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002468 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002469
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002470 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002471 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002472 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002473}
2474
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002475llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002476 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002477 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002478 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002479
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002480 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002481 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002482 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002483 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002484 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002485 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002486 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002487 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002488 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002489
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002490 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002491}
2492
Daniel Dunbar30c65362009-03-09 20:09:19 +00002493llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002494CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002495 llvm::Constant *Init,
2496 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002497 unsigned Align,
2498 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002499 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002500 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002501 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002502 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002503 if (Section)
2504 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002505 if (Align)
2506 GV->setAlignment(Align);
2507 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002508 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002509 return GV;
2510}
2511
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002512llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002513 // Abuse this interface function as a place to finalize.
2514 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002515 return NULL;
2516}
2517
Chris Lattnerd4808922009-03-22 21:03:39 +00002518llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002519 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002520}
2521
Chris Lattnerd4808922009-03-22 21:03:39 +00002522llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002523 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002524}
2525
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002526llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2527 return ObjCTypes.getCopyStructFn();
2528}
2529
Chris Lattnerd4808922009-03-22 21:03:39 +00002530llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002531 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002532}
2533
John McCallbd309292010-07-06 01:34:17 +00002534void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2535 return EmitTryOrSynchronizedStmt(CGF, S);
2536}
2537
2538void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2539 const ObjCAtSynchronizedStmt &S) {
2540 return EmitTryOrSynchronizedStmt(CGF, S);
2541}
2542
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002543/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002544
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002545 Objective-C setjmp-longjmp (sjlj) Exception Handling
2546 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002547
John McCallbd309292010-07-06 01:34:17 +00002548 A catch buffer is a setjmp buffer plus:
2549 - a pointer to the exception that was caught
2550 - a pointer to the previous exception data buffer
2551 - two pointers of reserved storage
2552 Therefore catch buffers form a stack, with a pointer to the top
2553 of the stack kept in thread-local storage.
2554
2555 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2556 objc_exception_try_exit pops the given catch buffer, which is
2557 required to be the top of the EH stack.
2558 objc_exception_throw pops the top of the EH stack, writes the
2559 thrown exception into the appropriate field, and longjmps
2560 to the setjmp buffer. It crashes the process (with a printf
2561 and an abort()) if there are no catch buffers on the stack.
2562 objc_exception_extract just reads the exception pointer out of the
2563 catch buffer.
2564
2565 There's no reason an implementation couldn't use a light-weight
2566 setjmp here --- something like __builtin_setjmp, but API-compatible
2567 with the heavyweight setjmp. This will be more important if we ever
2568 want to implement correct ObjC/C++ exception interactions for the
2569 fragile ABI.
2570
2571 Note that for this use of setjmp/longjmp to be correct, we may need
2572 to mark some local variables volatile: if a non-volatile local
2573 variable is modified between the setjmp and the longjmp, it has
2574 indeterminate value. For the purposes of LLVM IR, it may be
2575 sufficient to make loads and stores within the @try (to variables
2576 declared outside the @try) volatile. This is necessary for
2577 optimized correctness, but is not currently being done; this is
2578 being tracked as rdar://problem/8160285
2579
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002580 The basic framework for a @try-catch-finally is as follows:
2581 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002582 objc_exception_data d;
2583 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002584 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002585
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002586 objc_exception_try_enter(&d);
2587 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002588 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002589 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002590 // exception path
2591 id _caught = objc_exception_extract(&d);
2592
2593 // enter new try scope for handlers
2594 if (!setjmp(d.jmp_buf)) {
2595 ... match exception and execute catch blocks ...
2596
2597 // fell off end, rethrow.
2598 _rethrow = _caught;
2599 ... jump-through-finally to finally_rethrow ...
2600 } else {
2601 // exception in catch block
2602 _rethrow = objc_exception_extract(&d);
2603 _call_try_exit = false;
2604 ... jump-through-finally to finally_rethrow ...
2605 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002606 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002607 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002608
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002609 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002610 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002611 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002612
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002613 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002614 ... dispatch to finally destination ...
2615
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002616 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002617 objc_exception_throw(_rethrow);
2618
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002619 finally_end:
2620 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002621
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002622 This framework differs slightly from the one gcc uses, in that gcc
2623 uses _rethrow to determine if objc_exception_try_exit should be called
2624 and if the object should be rethrown. This breaks in the face of
2625 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002626
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002627 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002628
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002629 - If there are no catch blocks, then we avoid emitting the second
2630 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002631
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002632 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2633 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002634
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002635 - FIXME: If there is no @finally block we can do a few more
2636 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002637
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002638 Rethrows and Jumps-Through-Finally
2639 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002640
John McCallbd309292010-07-06 01:34:17 +00002641 '@throw;' is supported by pushing the currently-caught exception
2642 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002643
John McCallbd309292010-07-06 01:34:17 +00002644 Branches through the @finally block are handled with an ordinary
2645 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2646 exceptions are not compatible with C++ exceptions, and this is
2647 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002648
John McCallbd309292010-07-06 01:34:17 +00002649 @synchronized(expr) { stmt; } is emitted as if it were:
2650 id synch_value = expr;
2651 objc_sync_enter(synch_value);
2652 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002653*/
2654
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002655void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2656 const Stmt &S) {
2657 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002658
2659 // A destination for the fall-through edges of the catch handlers to
2660 // jump to.
2661 CodeGenFunction::JumpDest FinallyEnd =
2662 CGF.getJumpDestInCurrentScope("finally.end");
2663
2664 // A destination for the rethrow edge of the catch handlers to jump
2665 // to.
2666 CodeGenFunction::JumpDest FinallyRethrow =
2667 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002668
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002669 // For @synchronized, call objc_sync_enter(sync.expr). The
2670 // evaluation of the expression must occur before we enter the
2671 // @synchronized. We can safely avoid a temp here because jumps into
2672 // @synchronized are illegal & this will dominate uses.
2673 llvm::Value *SyncArg = 0;
2674 if (!isTry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002675 SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002676 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2677 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002678 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2679 ->setDoesNotThrow();
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002680 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002681
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002682 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002683 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2684 "exceptiondata.ptr");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002685 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00002686 "_rethrow");
John McCallbd309292010-07-06 01:34:17 +00002687
2688 // Create a flag indicating whether the cleanup needs to call
2689 // objc_exception_try_exit. This is true except when
2690 // - no catches match and we're branching through the cleanup
2691 // just to rethrow the exception, or
2692 // - a catch matched and we're falling out of the catch handler.
2693 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002694 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002695 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext),
John McCallbd309292010-07-06 01:34:17 +00002696 CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002697
John McCallbd309292010-07-06 01:34:17 +00002698 // Push a normal cleanup to leave the try scope.
2699 {
John McCall2b7fc382010-07-13 20:32:21 +00002700 CodeGenFunction::CleanupBlock FinallyScope(CGF, NormalCleanup);
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002701
John McCallbd309292010-07-06 01:34:17 +00002702 // Check whether we need to call objc_exception_try_exit.
2703 // In optimized code, this branch will always be folded.
2704 llvm::BasicBlock *FinallyCallExit =
2705 CGF.createBasicBlock("finally.call_exit");
2706 llvm::BasicBlock *FinallyNoCallExit =
2707 CGF.createBasicBlock("finally.no_call_exit");
2708 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2709 FinallyCallExit, FinallyNoCallExit);
2710
2711 CGF.EmitBlock(FinallyCallExit);
2712 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2713 ->setDoesNotThrow();
2714
2715 CGF.EmitBlock(FinallyNoCallExit);
2716
2717 if (isTry) {
2718 if (const ObjCAtFinallyStmt* FinallyStmt =
2719 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2720 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2721
2722 // ~CleanupBlock requires there to be an exit block.
2723 CGF.EnsureInsertPoint();
2724 } else {
2725 // Emit objc_sync_exit(expr); as finally's sole statement for
2726 // @synchronized.
2727 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2728 ->setDoesNotThrow();
2729 }
2730 }
2731
2732 // Enter a try block:
2733 // - Call objc_exception_try_enter to push ExceptionData on top of
2734 // the EH stack.
2735 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2736 ->setDoesNotThrow();
2737
2738 // - Call setjmp on the exception data buffer.
2739 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2740 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2741 llvm::Value *SetJmpBuffer =
2742 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2743 llvm::CallInst *SetJmpResult =
2744 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2745 SetJmpResult->setDoesNotThrow();
2746
2747 // If setjmp returned 0, enter the protected block; otherwise,
2748 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002749 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2750 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002751 llvm::Value *DidCatch =
2752 CGF.Builder.CreateIsNull(SetJmpResult, "did_catch_exception");
2753 CGF.Builder.CreateCondBr(DidCatch, TryBlock, TryHandler);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002754
John McCallbd309292010-07-06 01:34:17 +00002755 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002756 CGF.EmitBlock(TryBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002757 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00002758 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002759 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002760
John McCallbd309292010-07-06 01:34:17 +00002761 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00002762 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002763
2764 // Retrieve the exception object. We may emit multiple blocks but
2765 // nothing can cross this so the value is already in SSA form.
John McCallbd309292010-07-06 01:34:17 +00002766 llvm::CallInst *Caught =
Chris Lattnerc6406db2009-04-22 02:26:14 +00002767 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2768 ExceptionData, "caught");
John McCallbd309292010-07-06 01:34:17 +00002769 Caught->setDoesNotThrow();
2770
2771 // Remember the exception to rethrow.
2772 CGF.Builder.CreateStore(Caught, RethrowPtr);
2773
2774 // Note: at this point, objc_exception_throw already popped the
2775 // catch handler, so anything that branches to the cleanup needs
2776 // to set CallTryExitVar to false.
2777
2778 // For a @synchronized (or a @try with no catches), just branch
2779 // through the cleanup to the rethrow block.
2780 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2781 // Tell the cleanup not to re-pop the exit.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002782 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
John McCallbd309292010-07-06 01:34:17 +00002783 CallTryExitVar);
2784
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002785 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00002786
2787 // Otherwise, we have to match against the caught exceptions.
2788 } else {
2789 // Push the exception to rethrow onto the EH value stack for the
2790 // benefit of any @throws in the handlers.
2791 CGF.ObjCEHValueStack.push_back(Caught);
2792
Douglas Gregor96c79492010-04-23 22:50:49 +00002793 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
2794
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002795 // Enter a new exception try block (in case a @catch block throws
John McCallbd309292010-07-06 01:34:17 +00002796 // an exception). Now CallTryExitVar (currently true) is back in
2797 // synch with reality.
2798 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2799 ->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002800
John McCallbd309292010-07-06 01:34:17 +00002801 llvm::CallInst *SetJmpResult =
2802 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2803 "setjmp.result");
2804 SetJmpResult->setDoesNotThrow();
2805
2806 llvm::Value *Threw =
2807 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002808
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002809 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
John McCallbd309292010-07-06 01:34:17 +00002810 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch_for_catch");
Daniel Dunbar7f086782008-09-27 23:30:04 +00002811 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002812
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002813 CGF.EmitBlock(CatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002814
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002815 // Handle catch list. As a special case we check if everything is
2816 // matched and avoid generating code for falling off the end if
2817 // so.
2818 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00002819 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
2820 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002821
Douglas Gregor46a572b2010-04-26 16:46:50 +00002822 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00002823 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00002824
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002825 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002826 if (!CatchParam) {
2827 AllMatched = true;
2828 } else {
John McCall9dd450b2009-09-21 23:43:11 +00002829 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002830
John McCallbd309292010-07-06 01:34:17 +00002831 // catch(id e) always matches under this ABI, since only
2832 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00002833 // FIXME: For the time being we also match id<X>; this should
2834 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00002835 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002836 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002837 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002838
John McCallbd309292010-07-06 01:34:17 +00002839 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002840 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00002841 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
2842
Anders Carlsson9396a892008-09-11 09:15:33 +00002843 if (CatchParam) {
Steve Naroff371b8fb2009-03-03 19:52:17 +00002844 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00002845 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00002846
2847 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00002848 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00002849 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002850
Anders Carlsson9396a892008-09-11 09:15:33 +00002851 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00002852
2853 // The scope of the catch variable ends right here.
2854 CatchVarCleanups.ForceCleanup();
2855
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002856 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002857 break;
2858 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002859
Steve Naroff7cae42b2009-07-10 23:34:53 +00002860 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00002861 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00002862
2863 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00002864 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
2865 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002866
2867 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00002868 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002869
John McCallbd309292010-07-06 01:34:17 +00002870 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00002871 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2872 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00002873 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002874
John McCallbd309292010-07-06 01:34:17 +00002875 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
2876 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002877
2878 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00002879 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002880
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002881 // Emit the @catch block.
2882 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00002883
2884 // Collect any cleanups for the catch variable. The scope lasts until
2885 // the end of the catch body.
2886 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
2887
Steve Naroff371b8fb2009-03-03 19:52:17 +00002888 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00002889 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002890
John McCallbd309292010-07-06 01:34:17 +00002891 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002892 llvm::Value *Tmp =
2893 CGF.Builder.CreateBitCast(Caught,
2894 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002895 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00002896 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002897
Anders Carlsson9396a892008-09-11 09:15:33 +00002898 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00002899
2900 // We're done with the catch variable.
2901 CatchVarCleanups.ForceCleanup();
2902
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002903 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002904
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002905 CGF.EmitBlock(NextCatchBlock);
2906 }
2907
John McCallbd309292010-07-06 01:34:17 +00002908 CGF.ObjCEHValueStack.pop_back();
2909
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002910 if (!AllMatched) {
2911 // None of the handlers caught the exception, so store it to be
2912 // rethrown at the end of the @finally block.
2913 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002914 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002915 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002916
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002917 // Emit the exception handler for the @catch blocks.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002918 CGF.EmitBlock(CatchHandler);
John McCallbd309292010-07-06 01:34:17 +00002919
2920 // Rethrow the new exception, not the old one.
2921 Caught = CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2922 ExceptionData);
2923 Caught->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002924 CGF.Builder.CreateStore(Caught, RethrowPtr);
John McCallbd309292010-07-06 01:34:17 +00002925
2926 // Don't pop the catch handler; the throw already did.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002927 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
John McCallbd309292010-07-06 01:34:17 +00002928 CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002929 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002930 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002931
John McCallbd309292010-07-06 01:34:17 +00002932 // Pop the cleanup.
2933 CGF.PopCleanupBlock();
2934 CGF.EmitBlock(FinallyEnd.Block);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002935
John McCallbd309292010-07-06 01:34:17 +00002936 // Emit the rethrow block.
2937 CGF.Builder.ClearInsertionPoint();
2938 CGF.EmitBlock(FinallyRethrow.Block, true);
2939 if (CGF.HaveInsertPoint()) {
2940 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
2941 CGF.Builder.CreateLoad(RethrowPtr))
2942 ->setDoesNotThrow();
2943 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00002944 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002945
John McCallbd309292010-07-06 01:34:17 +00002946 CGF.Builder.SetInsertPoint(FinallyEnd.Block);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00002947}
2948
2949void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002950 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00002951 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002952
Anders Carlssone005aa12008-09-09 16:16:55 +00002953 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2954 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002955 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00002956 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2957 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002958 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002959 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00002960 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00002961 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002962
John McCallbd309292010-07-06 01:34:17 +00002963 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
2964 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002965 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00002966
2967 // Clear the insertion point to indicate we are in unreachable code.
2968 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00002969}
2970
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00002971/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00002972/// object: objc_read_weak (id *src)
2973///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00002974llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00002975 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00002976 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002977 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
2978 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
2979 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00002980 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00002981 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00002982 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00002983 return read_weak;
2984}
2985
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00002986/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
2987/// objc_assign_weak (id src, id *dst)
2988///
2989void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00002990 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00002991 const llvm::Type * SrcTy = src->getType();
2992 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002993 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00002994 assert(Size <= 8 && "does not support size > 8");
2995 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002996 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00002997 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
2998 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00002999 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3000 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003001 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003002 src, dst, "weakassign");
3003 return;
3004}
3005
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003006/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3007/// objc_assign_global (id src, id *dst)
3008///
3009void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003010 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003011 const llvm::Type * SrcTy = src->getType();
3012 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003013 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003014 assert(Size <= 8 && "does not support size > 8");
3015 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003016 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003017 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3018 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003019 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3020 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003021 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003022 src, dst, "globalassign");
3023 return;
3024}
3025
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003026/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003027/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003028///
3029void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003030 llvm::Value *src, llvm::Value *dst,
3031 llvm::Value *ivarOffset) {
3032 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003033 const llvm::Type * SrcTy = src->getType();
3034 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003035 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003036 assert(Size <= 8 && "does not support size > 8");
3037 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003038 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003039 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3040 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003041 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3042 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003043 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3044 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003045 return;
3046}
3047
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003048/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3049/// objc_assign_strongCast (id src, id *dst)
3050///
3051void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003052 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003053 const llvm::Type * SrcTy = src->getType();
3054 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003055 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003056 assert(Size <= 8 && "does not support size > 8");
3057 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003058 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003059 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3060 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003061 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3062 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003063 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003064 src, dst, "weakassign");
3065 return;
3066}
3067
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003068void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003069 llvm::Value *DestPtr,
3070 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003071 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003072 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3073 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003074 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003075 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003076 return;
3077}
3078
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003079/// EmitObjCValueForIvar - Code Gen for ivar reference.
3080///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003081LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3082 QualType ObjectTy,
3083 llvm::Value *BaseValue,
3084 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003085 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003086 const ObjCInterfaceDecl *ID =
3087 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003088 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3089 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003090}
3091
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003092llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003093 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003094 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003095 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003096 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003097 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3098 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003099}
3100
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003101/* *** Private Interface *** */
3102
3103/// EmitImageInfo - Emit the image info marker used to encode some module
3104/// level information.
3105///
3106/// See: <rdr://4810609&4810587&4810587>
3107/// struct IMAGE_INFO {
3108/// unsigned version;
3109/// unsigned flags;
3110/// };
3111enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003112 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003113 eImageInfo_GarbageCollected = (1 << 1),
3114 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003115 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3116
Daniel Dunbar5e639272010-04-25 20:39:01 +00003117 // A flag indicating that the module has no instances of a @synthesize of a
3118 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003119 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003120};
3121
Daniel Dunbar5e639272010-04-25 20:39:01 +00003122void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003123 unsigned version = 0; // Version is unused?
3124 unsigned flags = 0;
3125
3126 // FIXME: Fix and continue?
3127 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3128 flags |= eImageInfo_GarbageCollected;
3129 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3130 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003131
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003132 // We never allow @synthesize of a superclass property.
3133 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003134
Chris Lattner5e016ae2010-06-27 07:15:29 +00003135 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3136
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003137 // Emitted as int[2];
3138 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003139 llvm::ConstantInt::get(Int32Ty, version),
3140 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003141 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003142 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003143
3144 const char *Section;
3145 if (ObjCABI == 1)
3146 Section = "__OBJC, __image_info,regular";
3147 else
3148 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003149 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003150 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003151 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003152 Section,
3153 0,
3154 true);
3155 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003156}
3157
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003158
3159// struct objc_module {
3160// unsigned long version;
3161// unsigned long size;
3162// const char *name;
3163// Symtab symtab;
3164// };
3165
3166// FIXME: Get from somewhere
3167static const int ModuleVersion = 7;
3168
3169void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003170 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003171
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003172 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003173 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3174 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003175 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003176 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003177 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003178 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003179 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003180 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003181 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003182}
3183
3184llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003185 unsigned NumClasses = DefinedClasses.size();
3186 unsigned NumCategories = DefinedCategories.size();
3187
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003188 // Return null if no symbols were defined.
3189 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003190 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003191
3192 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003193 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003194 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003195 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3196 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003197
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003198 // The runtime expects exactly the list of defined classes followed
3199 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003200 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003201 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003202 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003203 ObjCTypes.Int8PtrTy);
3204 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003205 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003206 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003207 ObjCTypes.Int8PtrTy);
3208
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003209 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003210 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003211 NumClasses + NumCategories),
3212 Symbols);
3213
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003214 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003215
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003216 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003217 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3218 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003219 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003220 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003221}
3222
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003223llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003224 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003225 LazySymbols.insert(ID->getIdentifier());
3226
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003227 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003228
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003229 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003230 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003231 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003232 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003233 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003234 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3235 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003236 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003237 }
3238
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003239 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003240}
3241
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003242llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3243 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003244 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003245
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003246 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003247 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003248 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003249 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003250 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003251 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3252 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003253 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003254 }
3255
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003256 if (lvalue)
3257 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003258 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003259}
3260
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003261llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003262 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003263
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003264 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003265 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003266 llvm::ConstantArray::get(VMContext,
3267 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003268 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003269 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003270
Owen Anderson170229f2009-07-14 23:10:40 +00003271 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003272}
3273
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003274/// GetIvarLayoutName - Returns a unique constant for the given
3275/// ivar layout bitmap.
3276llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003277 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003278 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003279}
3280
John McCall8ccfcb52009-09-24 19:53:00 +00003281static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003282 if (FQT.isObjCGCStrong())
John McCall8ccfcb52009-09-24 19:53:00 +00003283 return Qualifiers::Strong;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003284
3285 if (FQT.isObjCGCWeak())
John McCall8ccfcb52009-09-24 19:53:00 +00003286 return Qualifiers::Weak;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003287
Fariborz Jahanian10e9bff2009-09-11 17:39:05 +00003288 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003289 return Qualifiers::Strong;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003290
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003291 if (const PointerType *PT = FQT->getAs<PointerType>())
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003292 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003293
John McCall8ccfcb52009-09-24 19:53:00 +00003294 return Qualifiers::GCNone;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003295}
3296
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003297void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003298 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003299 bool ForStrongLayout,
3300 bool &HasUnion) {
3301 const RecordDecl *RD = RT->getDecl();
3302 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003303 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003304 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003305 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003306 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003307
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003308 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3309 ForStrongLayout, HasUnion);
3310}
3311
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003312void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003313 const llvm::StructLayout *Layout,
3314 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003315 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003316 unsigned int BytePos, bool ForStrongLayout,
3317 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003318 bool IsUnion = (RD && RD->isUnion());
3319 uint64_t MaxUnionIvarSize = 0;
3320 uint64_t MaxSkippedUnionIvarSize = 0;
3321 FieldDecl *MaxField = 0;
3322 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003323 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003324 uint64_t MaxFieldOffset = 0;
3325 uint64_t MaxSkippedFieldOffset = 0;
3326 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003327
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003328 if (RecFields.empty())
3329 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003330 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3331 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3332
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003333 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003334 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003335 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003336 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003337 // Note that 'i' here is actually the field index inside RD of Field,
3338 // although this dependency is hidden.
3339 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3340 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003341 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003342 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003343
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003344 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003345 if (!Field->getIdentifier() || Field->isBitField()) {
3346 LastFieldBitfield = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003347 LastBitfieldOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003348 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003349 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003350
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003351 LastFieldBitfield = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003352 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003353 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003354 if (FQT->isUnionType())
3355 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003356
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003357 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003358 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003359 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003360 continue;
3361 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003362
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003363 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003364 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003365 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003366 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003367 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003368 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003369 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3370 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003371 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003372 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003373 FQT = CArray->getElementType();
3374 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003375
3376 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003377 "layout for array of unions not supported");
3378 if (FQT->isRecordType()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003379 int OldIndex = IvarsInfo.size() - 1;
3380 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003381
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003382 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003383 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003384 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003385
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003386 // Replicate layout information for each array element. Note that
3387 // one element is already done.
3388 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003389 for (int FirstIndex = IvarsInfo.size() - 1,
3390 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003391 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003392 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3393 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3394 IvarsInfo[i].ivar_size));
3395 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3396 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3397 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003398 }
3399 continue;
3400 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003401 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003402 // At this point, we are done with Record/Union and array there of.
3403 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003404 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003405
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003406 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003407 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3408 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003409 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003410 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003411 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003412 MaxUnionIvarSize = UnionIvarSize;
3413 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003414 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003415 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003416 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003417 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003418 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003419 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003420 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003421 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3422 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003423 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003424 // FIXME: Why the asymmetry? We divide by word size in bits on other
3425 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003426 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003427 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003428 MaxSkippedUnionIvarSize = UnionIvarSize;
3429 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003430 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003431 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003432 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003433 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003434 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003435 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003436 }
3437 }
3438 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003439
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003440 if (LastFieldBitfield) {
3441 // Last field was a bitfield. Must update skip info.
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003442 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3443 uint64_t BitFieldSize =
Eli Friedman1c4a1752009-04-26 19:19:15 +00003444 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar22007d32009-05-03 13:32:01 +00003445 GC_IVAR skivar;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003446 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003447 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3448 + ((BitFieldSize % ByteSizeInBits) != 0);
3449 SkipIvars.push_back(skivar);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003450 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003451
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003452 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003453 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003454 MaxUnionIvarSize));
3455 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003456 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003457 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003458}
3459
3460/// BuildIvarLayout - Builds ivar layout bitmap for the class
3461/// implementation for the __strong or __weak case.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003462/// The layout map displays which words in ivar list must be skipped
3463/// and which must be scanned by GC (see below). String is built of bytes.
3464/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003465/// of words to skip and right nibble is count of words to scan. So, each
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003466/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003467/// represented by a 0x00 byte which also ends the string.
3468/// 1. when ForStrongLayout is true, following ivars are scanned:
3469/// - id, Class
3470/// - object *
3471/// - __strong anything
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003472///
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003473/// 2. When ForStrongLayout is false, following ivars are scanned:
3474/// - __weak anything
3475///
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003476llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003477 const ObjCImplementationDecl *OMD,
3478 bool ForStrongLayout) {
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003479 bool hasUnion = false;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003480
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003481 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003482 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003483 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Anderson0b75f232009-07-31 20:28:54 +00003484 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003485
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003486 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003487 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003488 CGM.getContext().CollectObjCIvars(OI, RecFields);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003489
Daniel Dunbar98ba9642009-05-04 04:10:48 +00003490 // Add this implementations synthesized ivars.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +00003491 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanianaef66222010-02-19 00:31:17 +00003492 CGM.getContext().CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian0f44d812009-05-12 18:14:29 +00003493 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3494 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003495
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003496 if (RecFields.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003497 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003498
3499 SkipIvars.clear();
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003500 IvarsInfo.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003501
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003502 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003503 if (IvarsInfo.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003504 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003505
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003506 // Sort on byte position in case we encounterred a union nested in
3507 // the ivar list.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003508 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar22b0ada2009-04-23 01:29:05 +00003509 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003510 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar22b0ada2009-04-23 01:29:05 +00003511 std::sort(SkipIvars.begin(), SkipIvars.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003512
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003513 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003514 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003515 unsigned int WordSize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003516 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003517 if (IvarsInfo[0].ivar_bytepos == 0) {
3518 WordsToSkip = 0;
3519 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003520 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003521 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3522 WordsToScan = IvarsInfo[0].ivar_size;
3523 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003524 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003525 unsigned int TailPrevGCObjC =
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003526 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003527 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003528 // consecutive 'scanned' object pointers.
3529 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003530 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003531 // Skip over 'gc'able object pointer which lay over each other.
3532 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3533 continue;
3534 // Must skip over 1 or more words. We save current skip/scan values
3535 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003536 SKIP_SCAN SkScan;
3537 SkScan.skip = WordsToSkip;
3538 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003539 SkipScanIvars.push_back(SkScan);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003540
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003541 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003542 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3543 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003544 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003545 WordsToSkip = 0;
3546 WordsToScan = IvarsInfo[i].ivar_size;
3547 }
3548 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003549 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003550 SKIP_SCAN SkScan;
3551 SkScan.skip = WordsToSkip;
3552 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003553 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003554 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003555
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003556 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003557 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003558 int LastByteSkipped =
3559 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003560 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003561 int LastByteScanned =
3562 IvarsInfo[LastIndex].ivar_bytepos +
3563 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003564 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003565 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003566 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003567 SKIP_SCAN SkScan;
3568 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3569 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003570 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003571 }
3572 }
3573 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3574 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003575 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003576 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003577 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3578 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3579 // 0xM0 followed by 0x0N detected.
3580 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3581 for (int j = i+1; j < SkipScan; j++)
3582 SkipScanIvars[j] = SkipScanIvars[j+1];
3583 --SkipScan;
3584 }
3585 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003586
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003587 // Generate the string.
3588 std::string BitMap;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003589 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003590 unsigned char byte;
3591 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3592 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3593 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3594 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3595
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003596 // first skip big.
3597 for (unsigned int ix = 0; ix < skip_big; ix++)
3598 BitMap += (unsigned char)(0xf0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003599
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003600 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003601 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003602 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003603 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003604 byte |= 0xf;
3605 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003606 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003607 byte |= scan_small;
3608 scan_small = 0;
3609 }
3610 BitMap += byte;
3611 }
3612 // next scan big
3613 for (unsigned int ix = 0; ix < scan_big; ix++)
3614 BitMap += (unsigned char)(0x0f);
3615 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003616 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003617 byte = scan_small;
3618 BitMap += byte;
3619 }
3620 }
3621 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003622 unsigned char zero = 0;
3623 BitMap += zero;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003624
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003625 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003626 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003627 ForStrongLayout ? "strong" : "weak",
3628 OMD->getClassInterface()->getNameAsCString());
3629 const unsigned char *s = (unsigned char*)BitMap.c_str();
3630 for (unsigned i = 0; i < BitMap.size(); i++)
3631 if (!(s[i] & 0xf0))
3632 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3633 else
3634 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3635 printf("\n");
3636 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003637 llvm::GlobalVariable * Entry =
3638 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003639 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003640 "__TEXT,__cstring,cstring_literals",
3641 1, true);
3642 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003643}
3644
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003645llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003646 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3647
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003648 // FIXME: Avoid std::string copying.
3649 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003650 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003651 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003652 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003653 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003654
Owen Anderson170229f2009-07-14 23:10:40 +00003655 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003656}
3657
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003658// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003659llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003660 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3661}
3662
3663// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003664llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003665 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3666}
3667
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003668llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003669 std::string TypeStr;
3670 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3671
3672 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003673
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003674 if (!Entry)
3675 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003676 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003677 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003678 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003679
Owen Anderson170229f2009-07-14 23:10:40 +00003680 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003681}
3682
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003683llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003684 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003685 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3686 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003687
3688 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3689
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003690 if (!Entry)
3691 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003692 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003693 "__TEXT,__cstring,cstring_literals",
3694 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003695
Owen Anderson170229f2009-07-14 23:10:40 +00003696 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003697}
3698
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003699// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003700llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003701 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003702
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003703 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003704 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003705 llvm::ConstantArray::get(VMContext,
3706 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003707 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003708 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003709
Owen Anderson170229f2009-07-14 23:10:40 +00003710 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003711}
3712
3713// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00003714// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003715llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003716CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3717 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003718 std::string TypeStr;
3719 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003720 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3721}
3722
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003723void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003724 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00003725 llvm::SmallVectorImpl<char> &Name) {
3726 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003727 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00003728 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3729 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003730 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00003731 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00003732 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00003733 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003734}
3735
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003736void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003737 EmitModuleInfo();
3738
Daniel Dunbarc475d422008-10-29 22:36:39 +00003739 // Emit the dummy bodies for any protocols which were referenced but
3740 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003741 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00003742 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3743 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00003744 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003745
Daniel Dunbarc475d422008-10-29 22:36:39 +00003746 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00003747 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003748 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00003749 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00003750 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00003751 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003752 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00003753 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00003754 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00003755 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00003756 }
3757
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003758 // Add assembler directives to add lazy undefined symbol references
3759 // for classes which are referenced but not defined. This is
3760 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00003761 //
3762 // FIXME: It would be nice if we had an LLVM construct for this.
3763 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
3764 llvm::SmallString<256> Asm;
3765 Asm += CGM.getModule().getModuleInlineAsm();
3766 if (!Asm.empty() && Asm.back() != '\n')
3767 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003768
Daniel Dunbard027a922009-09-07 00:20:42 +00003769 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00003770 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3771 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003772 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3773 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00003774 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00003775 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00003776 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00003777 }
3778
3779 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
3780 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
3781 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
3782 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00003783
Daniel Dunbard027a922009-09-07 00:20:42 +00003784 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003785 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003786}
3787
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003788CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003789 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00003790 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00003791 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003792 ObjCABI = 2;
3793}
3794
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003795/* *** */
3796
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003797ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00003798 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003799 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3800 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003801
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003802 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003803 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003804 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003805 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003806 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003807
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003808 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00003809 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003810 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003811
Mike Stump18bb9282009-05-16 07:57:57 +00003812 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3813 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00003814 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00003815 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003816
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003817 // I'm not sure I like this. The implicit coordination is a bit
3818 // gross. We should solve this in a reasonable fashion because this
3819 // is a pretty common task (match some runtime data structure with
3820 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003821
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003822 // FIXME: This is leaked.
3823 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003824
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003825 // struct _objc_super {
3826 // id self;
3827 // Class cls;
3828 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00003829 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00003830 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003831 SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003832 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003833 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00003834 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003835 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00003836 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00003837 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003838
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003839 SuperCTy = Ctx.getTagDeclType(RD);
3840 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003841
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003842 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003843 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3844
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003845 // struct _prop_t {
3846 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003847 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003848 // }
Owen Anderson758428f2009-08-05 23:18:46 +00003849 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003850 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003851 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003852
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003853 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00003854 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003855 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00003856 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003857 // }
Owen Anderson758428f2009-08-05 23:18:46 +00003858 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003859 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00003860 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003861 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003862 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003863 PropertyListTy);
3864 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00003865 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003866
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003867 // struct _objc_method {
3868 // SEL _cmd;
3869 // char *method_type;
3870 // char *_imp;
3871 // }
Owen Anderson758428f2009-08-05 23:18:46 +00003872 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003873 Int8PtrTy,
3874 Int8PtrTy,
3875 NULL);
3876 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003877
Fariborz Jahanian0232c052009-01-23 01:46:23 +00003878 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00003879 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00003880 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00003881 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003882}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003883
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003884ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00003885 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003886 // struct _objc_method_description {
3887 // SEL name;
3888 // char *types;
3889 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003890 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00003891 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003892 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00003893 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003894 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00003895 MethodDescriptionTy);
3896
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003897 // struct _objc_method_description_list {
3898 // int count;
3899 // struct _objc_method_description[1];
3900 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003901 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00003902 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00003903 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00003904 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003905 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00003906 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003907
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003908 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003909 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003910 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003911
Daniel Dunbarb036db82008-08-13 03:21:16 +00003912 // Protocol description structures
3913
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003914 // struct _objc_protocol_extension {
3915 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3916 // struct _objc_method_description_list *optional_instance_methods;
3917 // struct _objc_method_description_list *optional_class_methods;
3918 // struct _objc_property_list *instance_properties;
3919 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003920 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00003921 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003922 MethodDescriptionListPtrTy,
3923 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00003924 PropertyListPtrTy,
3925 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003926 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00003927 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003928
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003929 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00003930 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003931
Daniel Dunbarc475d422008-10-29 22:36:39 +00003932 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00003933
Owen Andersonc36edfe2009-08-13 23:27:53 +00003934 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
3935 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003936
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003937 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00003938 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00003939 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003940 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00003941 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003942 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003943 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3944
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003945 // struct _objc_protocol {
3946 // struct _objc_protocol_extension *isa;
3947 // char *protocol_name;
3948 // struct _objc_protocol **_objc_protocol_list;
3949 // struct _objc_method_description_list *instance_methods;
3950 // struct _objc_method_description_list *class_methods;
3951 // }
Owen Anderson758428f2009-08-05 23:18:46 +00003952 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003953 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003954 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00003955 MethodDescriptionListPtrTy,
3956 MethodDescriptionListPtrTy,
3957 NULL);
3958 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3959
3960 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003961 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00003962 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003963 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00003964 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003965
3966 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003967 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00003968 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003969
3970 // Class description structures
3971
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003972 // struct _objc_ivar {
3973 // char *ivar_name;
3974 // char *ivar_type;
3975 // int ivar_offset;
3976 // }
Owen Anderson758428f2009-08-05 23:18:46 +00003977 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003978 Int8PtrTy,
3979 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003980 NULL);
3981 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
3982
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003983 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00003984 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003985 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00003986 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003987
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003988 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00003989 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003990 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00003991 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003992
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003993 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003994 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00003995 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003996 Int8PtrTy,
3997 PropertyListPtrTy,
3998 NULL);
3999 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004000 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004001
Owen Andersonc36edfe2009-08-13 23:27:53 +00004002 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004003
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004004 // struct _objc_class {
4005 // Class isa;
4006 // Class super_class;
4007 // char *name;
4008 // long version;
4009 // long info;
4010 // long instance_size;
4011 // struct _objc_ivar_list *ivars;
4012 // struct _objc_method_list *methods;
4013 // struct _objc_cache *cache;
4014 // struct _objc_protocol_list *protocols;
4015 // char *ivar_layout;
4016 // struct _objc_class_ext *ext;
4017 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004018 T = llvm::StructType::get(VMContext,
4019 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004020 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004021 Int8PtrTy,
4022 LongTy,
4023 LongTy,
4024 LongTy,
4025 IvarListPtrTy,
4026 MethodListPtrTy,
4027 CachePtrTy,
4028 ProtocolListPtrTy,
4029 Int8PtrTy,
4030 ClassExtensionPtrTy,
4031 NULL);
4032 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004033
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004034 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4035 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004036 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004037
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004038 // struct _objc_category {
4039 // char *category_name;
4040 // char *class_name;
4041 // struct _objc_method_list *instance_method;
4042 // struct _objc_method_list *class_method;
4043 // uint32_t size; // sizeof(struct _objc_category)
4044 // struct _objc_property_list *instance_properties;// category's @property
4045 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004046 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004047 Int8PtrTy,
4048 MethodListPtrTy,
4049 MethodListPtrTy,
4050 ProtocolListPtrTy,
4051 IntTy,
4052 PropertyListPtrTy,
4053 NULL);
4054 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4055
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004056 // Global metadata structures
4057
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004058 // struct _objc_symtab {
4059 // long sel_ref_cnt;
4060 // SEL *refs;
4061 // short cls_def_cnt;
4062 // short cat_def_cnt;
4063 // char *defs[cls_def_cnt + cat_def_cnt];
4064 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004065 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004066 SelectorPtrTy,
4067 ShortTy,
4068 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004069 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004070 NULL);
4071 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004072 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004073
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004074 // struct _objc_module {
4075 // long version;
4076 // long size; // sizeof(struct _objc_module)
4077 // char *name;
4078 // struct _objc_symtab* symtab;
4079 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004080 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004081 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004082 LongTy,
4083 Int8PtrTy,
4084 SymtabPtrTy,
4085 NULL);
4086 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004087
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004088
Mike Stump18bb9282009-05-16 07:57:57 +00004089 // FIXME: This is the size of the setjmp buffer and should be target
4090 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004091 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004092
Anders Carlsson9ff22482008-09-09 10:10:21 +00004093 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004094 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004095 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004096
4097 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004098 llvm::StructType::get(VMContext,
4099 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4100 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004101 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004102 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004103 ExceptionDataTy);
4104
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004105}
4106
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004107ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004108 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004109 // struct _method_list_t {
4110 // uint32_t entsize; // sizeof(struct _objc_method)
4111 // uint32_t method_count;
4112 // struct _objc_method method_list[method_count];
4113 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004114 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004115 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004116 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004117 NULL);
4118 CGM.getModule().addTypeName("struct.__method_list_t",
4119 MethodListnfABITy);
4120 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004121 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004122
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004123 // struct _protocol_t {
4124 // id isa; // NULL
4125 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004126 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004127 // const struct method_list_t * const instance_methods;
4128 // const struct method_list_t * const class_methods;
4129 // const struct method_list_t *optionalInstanceMethods;
4130 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004131 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004132 // const uint32_t size; // sizeof(struct _protocol_t)
4133 // const uint32_t flags; // = 0
4134 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004135
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004136 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004137 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004138
Owen Anderson758428f2009-08-05 23:18:46 +00004139 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004140 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004141 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004142 ProtocolListTyHolder),
4143 MethodListnfABIPtrTy,
4144 MethodListnfABIPtrTy,
4145 MethodListnfABIPtrTy,
4146 MethodListnfABIPtrTy,
4147 PropertyListPtrTy,
4148 IntTy,
4149 IntTy,
4150 NULL);
4151 CGM.getModule().addTypeName("struct._protocol_t",
4152 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004153
4154 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004155 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004156
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004157 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004158 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004159 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004160 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004161 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004162 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004163 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004164 NULL);
4165 CGM.getModule().addTypeName("struct._objc_protocol_list",
4166 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004167 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004168 ProtocolListnfABITy);
4169
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004170 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004171 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004172
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004173 // struct _ivar_t {
4174 // unsigned long int *offset; // pointer to ivar offset location
4175 // char *name;
4176 // char *type;
4177 // uint32_t alignment;
4178 // uint32_t size;
4179 // }
Mike Stump11289f42009-09-09 15:08:12 +00004180 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004181 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004182 Int8PtrTy,
4183 Int8PtrTy,
4184 IntTy,
4185 IntTy,
4186 NULL);
4187 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004188
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004189 // struct _ivar_list_t {
4190 // uint32 entsize; // sizeof(struct _ivar_t)
4191 // uint32 count;
4192 // struct _iver_t list[count];
4193 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004194 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004195 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004196 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004197 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004198 NULL);
4199 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004200
Owen Anderson9793f0e2009-07-29 22:16:19 +00004201 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004202
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004203 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004204 // uint32_t const flags;
4205 // uint32_t const instanceStart;
4206 // uint32_t const instanceSize;
4207 // uint32_t const reserved; // only when building for 64bit targets
4208 // const uint8_t * const ivarLayout;
4209 // const char *const name;
4210 // const struct _method_list_t * const baseMethods;
4211 // const struct _objc_protocol_list *const baseProtocols;
4212 // const struct _ivar_list_t *const ivars;
4213 // const uint8_t * const weakIvarLayout;
4214 // const struct _prop_list_t * const properties;
4215 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004216
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004217 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004218 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004219 IntTy,
4220 IntTy,
4221 Int8PtrTy,
4222 Int8PtrTy,
4223 MethodListnfABIPtrTy,
4224 ProtocolListnfABIPtrTy,
4225 IvarListnfABIPtrTy,
4226 Int8PtrTy,
4227 PropertyListPtrTy,
4228 NULL);
4229 CGM.getModule().addTypeName("struct._class_ro_t",
4230 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004231
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004232 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4233 std::vector<const llvm::Type*> Params;
4234 Params.push_back(ObjectPtrTy);
4235 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004236 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004237 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4238
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004239 // struct _class_t {
4240 // struct _class_t *isa;
4241 // struct _class_t * const superclass;
4242 // void *cache;
4243 // IMP *vtable;
4244 // struct class_ro_t *ro;
4245 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004246
Owen Andersonc36edfe2009-08-13 23:27:53 +00004247 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004248 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004249 llvm::StructType::get(VMContext,
4250 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004251 llvm::PointerType::getUnqual(ClassTyHolder),
4252 CachePtrTy,
4253 llvm::PointerType::getUnqual(ImpnfABITy),
4254 llvm::PointerType::getUnqual(ClassRonfABITy),
4255 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004256 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4257
4258 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004259 ClassnfABITy);
4260
Fariborz Jahanian71394042009-01-23 23:53:38 +00004261 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004262 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004263
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004264 // struct _category_t {
4265 // const char * const name;
4266 // struct _class_t *const cls;
4267 // const struct _method_list_t * const instance_methods;
4268 // const struct _method_list_t * const class_methods;
4269 // const struct _protocol_list_t * const protocols;
4270 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004271 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004272 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004273 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004274 MethodListnfABIPtrTy,
4275 MethodListnfABIPtrTy,
4276 ProtocolListnfABIPtrTy,
4277 PropertyListPtrTy,
4278 NULL);
4279 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004280
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004281 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004282 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4283 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004284
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004285 // MessageRefTy - LLVM for:
4286 // struct _message_ref_t {
4287 // IMP messenger;
4288 // SEL name;
4289 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004290
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004291 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004292 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004293 Ctx.getTranslationUnitDecl(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004294 SourceLocation(),
4295 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004296 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004297 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004298 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004299 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004300 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004301
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004302 MessageRefCTy = Ctx.getTagDeclType(RD);
4303 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4304 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004305
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004306 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004307 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004308
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004309 // SuperMessageRefTy - LLVM for:
4310 // struct _super_message_ref_t {
4311 // SUPER_IMP messenger;
4312 // SEL name;
4313 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004314 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004315 SelectorPtrTy,
4316 NULL);
4317 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004318
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004319 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004320 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4321
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004322
4323 // struct objc_typeinfo {
4324 // const void** vtable; // objc_ehtype_vtable + 2
4325 // const char* name; // c++ typeinfo string
4326 // Class cls;
4327 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004328 EHTypeTy = llvm::StructType::get(VMContext,
4329 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004330 Int8PtrTy,
4331 ClassnfABIPtrTy,
4332 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004333 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004334 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004335}
4336
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004337llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004338 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004339
Fariborz Jahanian71394042009-01-23 23:53:38 +00004340 return NULL;
4341}
4342
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004343void CGObjCNonFragileABIMac::AddModuleClassList(const
4344 std::vector<llvm::GlobalValue*>
4345 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004346 const char *SymbolName,
4347 const char *SectionName) {
4348 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004349
Daniel Dunbar19573e72009-05-15 21:48:48 +00004350 if (!NumClasses)
4351 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004352
Daniel Dunbar19573e72009-05-15 21:48:48 +00004353 std::vector<llvm::Constant*> Symbols(NumClasses);
4354 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004355 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004356 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004357 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004358 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004359 NumClasses),
4360 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004361
Daniel Dunbar19573e72009-05-15 21:48:48 +00004362 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004363 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004364 llvm::GlobalValue::InternalLinkage,
4365 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004366 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004367 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004368 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004369 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004370}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004371
Fariborz Jahanian71394042009-01-23 23:53:38 +00004372void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4373 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004374
Daniel Dunbar19573e72009-05-15 21:48:48 +00004375 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004376 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004377 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004378 "\01L_OBJC_LABEL_CLASS_$",
4379 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004380
Fariborz Jahanian67260552009-11-17 21:37:35 +00004381 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4382 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4383 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4384 continue;
4385 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004386 }
4387
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004388 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4389 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4390 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4391 continue;
4392 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4393 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004394
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004395 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004396 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4397 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004398
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004399 // Build list of all implemented category addresses in array
4400 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004401 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004402 "\01L_OBJC_LABEL_CATEGORY_$",
4403 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004404 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004405 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4406 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004407
Daniel Dunbar5e639272010-04-25 20:39:01 +00004408 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004409}
4410
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004411/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004412/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004413/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004414/// message dispatch call for all the rest.
4415///
4416bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004417 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4418 default:
4419 assert(0 && "Invalid dispatch method!");
4420 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004421 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004422 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004423 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004424 case CodeGenOptions::Mixed:
4425 break;
4426 }
4427
4428 // If so, see whether this selector is in the white-list of things which must
4429 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004430 if (NonLegacyDispatchMethods.empty()) {
4431 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4432 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4433 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4434 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4435 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4436 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4437 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4438 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4439 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4440 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004441
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004442 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4443 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4444 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4445 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4446 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4447 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4448 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4449 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004450 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004451 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004452 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4453 &CGM.getContext().Idents.get("objects"),
4454 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004455 };
4456 NonLegacyDispatchMethods.insert(
4457 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004458 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004459
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004460 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004461}
4462
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004463// Metadata flags
4464enum MetaDataDlags {
4465 CLS = 0x0,
4466 CLS_META = 0x1,
4467 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004468 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004469 CLS_EXCEPTION = 0x20
4470};
4471/// BuildClassRoTInitializer - generate meta-data for:
4472/// struct _class_ro_t {
4473/// uint32_t const flags;
4474/// uint32_t const instanceStart;
4475/// uint32_t const instanceSize;
4476/// uint32_t const reserved; // only when building for 64bit targets
4477/// const uint8_t * const ivarLayout;
4478/// const char *const name;
4479/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004480/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004481/// const struct _ivar_list_t *const ivars;
4482/// const uint8_t * const weakIvarLayout;
4483/// const struct _prop_list_t * const properties;
4484/// }
4485///
4486llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004487 unsigned flags,
4488 unsigned InstanceStart,
4489 unsigned InstanceSize,
4490 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004491 std::string ClassName = ID->getNameAsString();
4492 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004493 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4494 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4495 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004496 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004497 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4498 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004499 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004500 // const struct _method_list_t * const baseMethods;
4501 std::vector<llvm::Constant*> Methods;
4502 std::string MethodListName("\01l_OBJC_$_");
4503 if (flags & CLS_META) {
4504 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004505 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004506 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004507 // Class methods should always be defined.
4508 Methods.push_back(GetMethodConstant(*i));
4509 }
4510 } else {
4511 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004512 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004513 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004514 // Instance methods should always be defined.
4515 Methods.push_back(GetMethodConstant(*i));
4516 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004517 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004518 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004519 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004520
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004521 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4522 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004523
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004524 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4525 if (llvm::Constant *C = GetMethodConstant(MD))
4526 Methods.push_back(C);
4527 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4528 if (llvm::Constant *C = GetMethodConstant(MD))
4529 Methods.push_back(C);
4530 }
4531 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004532 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004533 Values[ 5] = EmitMethodList(MethodListName,
4534 "__DATA, __objc_const", Methods);
4535
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004536 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4537 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004538 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004539 + OID->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004540 OID->protocol_begin(),
4541 OID->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004542
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004543 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004544 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004545 else
4546 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004547 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4548 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004549 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004550 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004551 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004552 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4553 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004554 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004555 Values);
4556 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004557 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4558 llvm::GlobalValue::InternalLinkage,
4559 Init,
4560 (flags & CLS_META) ?
4561 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4562 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004563 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004564 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004565 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004566 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004567
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004568}
4569
4570/// BuildClassMetaData - This routine defines that to-level meta-data
4571/// for the given ClassName for:
4572/// struct _class_t {
4573/// struct _class_t *isa;
4574/// struct _class_t * const superclass;
4575/// void *cache;
4576/// IMP *vtable;
4577/// struct class_ro_t *ro;
4578/// }
4579///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004580llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004581 std::string &ClassName,
4582 llvm::Constant *IsAGV,
4583 llvm::Constant *SuperClassGV,
4584 llvm::Constant *ClassRoGV,
4585 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004586 std::vector<llvm::Constant*> Values(5);
4587 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004588 Values[1] = SuperClassGV;
4589 if (!Values[1])
4590 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004591 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4592 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4593 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004594 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004595 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004596 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4597 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004598 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004599 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004600 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004601 if (HiddenVisibility)
4602 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004603 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004604}
4605
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004606bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004607CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004608 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004609}
4610
Daniel Dunbar961202372009-05-03 12:57:56 +00004611void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004612 uint32_t &InstanceStart,
4613 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004614 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004615 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004616
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004617 // InstanceSize is really instance end.
Anders Carlsson27b50132009-07-18 21:26:44 +00004618 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004619
4620 // If there are no fields, the start is the same as the end.
4621 if (!RL.getFieldCount())
4622 InstanceStart = InstanceSize;
4623 else
4624 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbar554fd792009-04-19 23:41:48 +00004625}
4626
Fariborz Jahanian71394042009-01-23 23:53:38 +00004627void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4628 std::string ClassName = ID->getNameAsString();
4629 if (!ObjCEmptyCacheVar) {
4630 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004631 CGM.getModule(),
4632 ObjCTypes.CacheTy,
4633 false,
4634 llvm::GlobalValue::ExternalLinkage,
4635 0,
4636 "_objc_empty_cache");
4637
Fariborz Jahanian71394042009-01-23 23:53:38 +00004638 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004639 CGM.getModule(),
4640 ObjCTypes.ImpnfABITy,
4641 false,
4642 llvm::GlobalValue::ExternalLinkage,
4643 0,
4644 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004645 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004646 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004647 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004648 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004649 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004650 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004651 uint32_t InstanceSize = InstanceStart;
4652 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004653 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4654 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004655
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004656 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004657
4658 bool classIsHidden =
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00004659 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004660 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004661 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004662 if (ID->getNumIvarInitializers())
4663 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004664 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004665 // class is root
4666 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004667 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004668 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004669 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004670 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004671 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4672 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4673 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004674 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004675 if (Root->hasAttr<WeakImportAttr>())
4676 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004677 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004678 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004679 ObjCMetaClassName +
4680 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004681 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004682 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4683 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004684 }
4685 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4686 InstanceStart,
4687 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004688 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004689 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004690 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4691 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004692 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004693
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004694 // Metadata for the class
4695 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004696 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004697 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004698 if (ID->getNumIvarInitializers())
4699 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004700
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004701 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004702 flags |= CLS_EXCEPTION;
4703
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004704 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004705 flags |= CLS_ROOT;
4706 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004707 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004708 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004709 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004710 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004711 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004712 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4713 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004714 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004715 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004716 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004717 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004718 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004719 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004720
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004721 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004722 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004723 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4724 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004725 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004726
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004727 // Determine if this class is also "non-lazy".
4728 if (ImplementationIsNonLazy(ID))
4729 DefinedNonLazyClasses.push_back(ClassMD);
4730
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004731 // Force the definition of the EHType if necessary.
4732 if (flags & CLS_EXCEPTION)
4733 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00004734}
4735
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004736/// GenerateProtocolRef - This routine is called to generate code for
4737/// a protocol reference expression; as in:
4738/// @code
4739/// @protocol(Proto1);
4740/// @endcode
4741/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4742/// which will hold address of the protocol meta-data.
4743///
4744llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004745 const ObjCProtocolDecl *PD) {
4746
Fariborz Jahanian464423d2009-04-10 18:47:34 +00004747 // This routine is called for @protocol only. So, we must build definition
4748 // of protocol's meta-data (not a reference to it!)
4749 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004750 llvm::Constant *Init =
4751 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4752 ObjCTypes.ExternalProtocolPtrTy);
4753
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004754 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4755 ProtocolName += PD->getNameAsCString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004756
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004757 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4758 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00004759 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004760 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004761 CGM.getModule(),
4762 Init->getType(), false,
4763 llvm::GlobalValue::WeakAnyLinkage,
4764 Init,
4765 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004766 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4767 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004768 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00004769 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004770}
4771
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004772/// GenerateCategory - Build metadata for a category implementation.
4773/// struct _category_t {
4774/// const char * const name;
4775/// struct _class_t *const cls;
4776/// const struct _method_list_t * const instance_methods;
4777/// const struct _method_list_t * const class_methods;
4778/// const struct _protocol_list_t * const protocols;
4779/// const struct _prop_list_t * const properties;
4780/// }
4781///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004782void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004783 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004784 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004785 std::string ExtCatName(Prefix + Interface->getNameAsString()+
4786 "_$_" + OCD->getNameAsString());
4787 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00004788 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004789
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004790 std::vector<llvm::Constant*> Values(6);
4791 Values[0] = GetClassName(OCD->getIdentifier());
4792 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004793 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00004794 if (Interface->hasAttr<WeakImportAttr>())
4795 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
4796
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004797 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004798 std::vector<llvm::Constant*> Methods;
4799 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004800 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004801 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004802
4803 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004804 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004805 // Instance methods should always be defined.
4806 Methods.push_back(GetMethodConstant(*i));
4807 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004808
4809 Values[2] = EmitMethodList(MethodListName,
4810 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004811 Methods);
4812
4813 MethodListName = Prefix;
4814 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4815 OCD->getNameAsString();
4816 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004817 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004818 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004819 // Class methods should always be defined.
4820 Methods.push_back(GetMethodConstant(*i));
4821 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004822
4823 Values[3] = EmitMethodList(MethodListName,
4824 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004825 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004826 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004827 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00004828 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004829 llvm::SmallString<256> ExtName;
4830 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
4831 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00004832 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004833 + Interface->getName() + "_$_"
4834 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00004835 Category->protocol_begin(),
4836 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004837 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
4838 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00004839 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00004840 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4841 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00004842 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004843
4844 llvm::Constant *Init =
4845 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004846 Values);
4847 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004848 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004849 false,
4850 llvm::GlobalValue::InternalLinkage,
4851 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004852 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004853 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004854 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004855 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00004856 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004857 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004858
4859 // Determine if this category is also "non-lazy".
4860 if (ImplementationIsNonLazy(OCD))
4861 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004862}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004863
4864/// GetMethodConstant - Return a struct objc_method constant for the
4865/// given method if it has been defined. The result is null if the
4866/// method has not been defined. The return value has type MethodPtrTy.
4867llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004868 const ObjCMethodDecl *MD) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004869 // FIXME: Use DenseMap::lookup
4870 llvm::Function *Fn = MethodDefinitions[MD];
4871 if (!Fn)
4872 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004873
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004874 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004875 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00004876 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004877 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004878 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00004879 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004880 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004881}
4882
4883/// EmitMethodList - Build meta-data for method declarations
4884/// struct _method_list_t {
4885/// uint32_t entsize; // sizeof(struct _objc_method)
4886/// uint32_t method_count;
4887/// struct _objc_method method_list[method_count];
4888/// }
4889///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004890llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
4891 const char *Section,
4892 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004893 // Return null for empty list.
4894 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00004895 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004896
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004897 std::vector<llvm::Constant*> Values(3);
4898 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004899 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004900 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004901 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004902 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004903 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004904 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00004905 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00004906 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004907
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004908 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004909 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004910 llvm::GlobalValue::InternalLinkage,
4911 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004912 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004913 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004914 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004915 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004916 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00004917 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004918 ObjCTypes.MethodListnfABIPtrTy);
4919}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004920
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00004921/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4922/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00004923llvm::GlobalVariable *
4924CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
4925 const ObjCIvarDecl *Ivar) {
4926 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00004927 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00004928 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004929 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00004930 CGM.getModule().getGlobalVariable(Name);
4931 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004932 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004933 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00004934 false,
4935 llvm::GlobalValue::ExternalLinkage,
4936 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004937 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00004938 return IvarOffsetGV;
4939}
4940
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00004941llvm::Constant *
4942CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
4943 const ObjCIvarDecl *Ivar,
4944 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00004945 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004946 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00004947 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004948 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004949 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00004950
Mike Stump18bb9282009-05-16 07:57:57 +00004951 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4952 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00004953 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4954 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4955 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00004956 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00004957 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00004958 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004959 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00004960 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004961}
4962
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004963/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00004964/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004965/// IvarListnfABIPtrTy.
4966/// struct _ivar_t {
4967/// unsigned long int *offset; // pointer to ivar offset location
4968/// char *name;
4969/// char *type;
4970/// uint32_t alignment;
4971/// uint32_t size;
4972/// }
4973/// struct _ivar_list_t {
4974/// uint32 entsize; // sizeof(struct _ivar_t)
4975/// uint32 count;
4976/// struct _iver_t list[count];
4977/// }
4978///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004979
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004980llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004981 const ObjCImplementationDecl *ID) {
4982
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004983 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004984
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004985 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4986 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004987
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004988 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004989
Daniel Dunbarae032262009-04-20 00:33:43 +00004990 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00004991 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00004992 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004993
Daniel Dunbarf5c18462009-04-20 06:54:31 +00004994 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
4995 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00004996 // Ignore unnamed bit-fields.
4997 if (!IVD->getDeclName())
4998 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004999 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005000 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005001 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5002 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005003 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005004 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005005 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005006 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005007 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005008 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005009 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005010 // NOTE. Size of a bitfield does not match gcc's, because of the
5011 // way bitfields are treated special in each. But I am told that
5012 // 'size' for bitfield ivars is ignored by the runtime so it does
5013 // not matter. If it matters, there is enough info to get the
5014 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005015 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005016 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005017 }
5018 // Return null for empty list.
5019 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005020 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005021 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005022 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005023 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5024 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005025 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005026 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005027 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005028 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005029 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5030 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005031 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005032 llvm::GlobalValue::InternalLinkage,
5033 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005034 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005035 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005036 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005037 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005038
Chris Lattnerf56501c2009-07-17 23:57:13 +00005039 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005040 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005041}
5042
5043llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005044 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005045 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005046
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005047 if (!Entry) {
5048 // We use the initializer as a marker of whether this is a forward
5049 // reference or not. At module finalization we add the empty
5050 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005051 Entry =
5052 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5053 llvm::GlobalValue::ExternalLinkage,
5054 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005055 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005056 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005057 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005058
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005059 return Entry;
5060}
5061
5062/// GetOrEmitProtocol - Generate the protocol meta-data:
5063/// @code
5064/// struct _protocol_t {
5065/// id isa; // NULL
5066/// const char * const protocol_name;
5067/// const struct _protocol_list_t * protocol_list; // super protocols
5068/// const struct method_list_t * const instance_methods;
5069/// const struct method_list_t * const class_methods;
5070/// const struct method_list_t *optionalInstanceMethods;
5071/// const struct method_list_t *optionalClassMethods;
5072/// const struct _prop_list_t * properties;
5073/// const uint32_t size; // sizeof(struct _protocol_t)
5074/// const uint32_t flags; // = 0
5075/// }
5076/// @endcode
5077///
5078
5079llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005080 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005081 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005082
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005083 // Early exit if a defining object has already been generated.
5084 if (Entry && Entry->hasInitializer())
5085 return Entry;
5086
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005087 // Construct method lists.
5088 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5089 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005090 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005091 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005092 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005093 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005094 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5095 OptInstanceMethods.push_back(C);
5096 } else {
5097 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005098 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005099 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005100
5101 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005102 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005103 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005104 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005105 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5106 OptClassMethods.push_back(C);
5107 } else {
5108 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005109 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005110 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005111
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005112 std::vector<llvm::Constant*> Values(10);
5113 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005114 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005115 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005116 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5117 PD->protocol_begin(),
5118 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005119
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005120 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005121 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005122 "__DATA, __objc_const",
5123 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005124 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005125 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005126 "__DATA, __objc_const",
5127 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005128 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005129 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005130 "__DATA, __objc_const",
5131 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005132 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005133 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005134 "__DATA, __objc_const",
5135 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005136 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005137 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005138 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005139 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005140 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005141 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005142 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005143 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005144
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005145 if (Entry) {
5146 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005147 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005148 Entry->setInitializer(Init);
5149 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005150 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005151 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5152 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5153 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005154 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005155 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005156 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005157 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005158 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005159 CGM.AddUsedGlobal(Entry);
5160
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005161 // Use this protocol meta-data to build protocol list table in section
5162 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005163 llvm::GlobalVariable *PTGV =
5164 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5165 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5166 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005167 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005168 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005169 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005170 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005171 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005172 return Entry;
5173}
5174
5175/// EmitProtocolList - Generate protocol list meta-data:
5176/// @code
5177/// struct _protocol_list_t {
5178/// long protocol_count; // Note, this is 32/64 bit
5179/// struct _protocol_t[protocol_count];
5180/// }
5181/// @endcode
5182///
5183llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005184CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5185 ObjCProtocolDecl::protocol_iterator begin,
5186 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005187 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005188
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005189 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005190 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005191 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005192
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005193 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005194 llvm::SmallString<256> TmpName;
5195 Name.toVector(TmpName);
5196 llvm::GlobalVariable *GV =
5197 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005198 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005199 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005200
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005201 for (; begin != end; ++begin)
5202 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5203
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005204 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005205 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005206 ObjCTypes.ProtocolnfABIPtrTy));
5207
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005208 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005209 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005210 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005211 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005212 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005213 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005214 ProtocolRefs.size()),
5215 ProtocolRefs);
5216
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005217 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005218 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005219 llvm::GlobalValue::InternalLinkage,
5220 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005221 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005222 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005223 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005224 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005225 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005226 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005227 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005228}
5229
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005230/// GetMethodDescriptionConstant - This routine build following meta-data:
5231/// struct _objc_method {
5232/// SEL _cmd;
5233/// char *method_type;
5234/// char *_imp;
5235/// }
5236
5237llvm::Constant *
5238CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5239 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005240 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005241 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5242 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005243 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005244 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005245 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005246 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005247}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005248
5249/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5250/// This code gen. amounts to generating code for:
5251/// @code
5252/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5253/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005254///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005255LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005256 CodeGen::CodeGenFunction &CGF,
5257 QualType ObjectTy,
5258 llvm::Value *BaseValue,
5259 const ObjCIvarDecl *Ivar,
5260 unsigned CVRQualifiers) {
5261 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005262 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5263 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005264}
5265
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005266llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005267 CodeGen::CodeGenFunction &CGF,
5268 const ObjCInterfaceDecl *Interface,
5269 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005270 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005271}
5272
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005273CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005274 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005275 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005276 QualType ResultType,
5277 Selector Sel,
5278 llvm::Value *Receiver,
5279 QualType Arg0Ty,
5280 bool IsSuper,
5281 const CallArgList &CallArgs) {
Mike Stump18bb9282009-05-16 07:57:57 +00005282 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5283 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005284 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005285 llvm::Value *Arg0 = Receiver;
5286 if (!IsSuper)
5287 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005288
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005289 // Find the message function name.
Mike Stump18bb9282009-05-16 07:57:57 +00005290 // FIXME. This is too much work to get the ABI-specific result type needed to
5291 // find the message name.
John McCall2da83a32010-02-26 00:48:12 +00005292 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005293 = Types.getFunctionInfo(ResultType, CallArgList(),
5294 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005295 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005296 std::string Name("\01l_");
5297 if (CGM.ReturnTypeUsesSret(FnInfo)) {
Fariborz Jahanian90655412009-02-05 18:00:27 +00005298#if 0
5299 // unlike what is documented. gcc never generates this API!!
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005300 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005301 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005302 // FIXME. Is there a better way of getting these names.
5303 // They are available in RuntimeFunctions vector pair.
5304 Name += "objc_msgSendId_stret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005305 } else
Fariborz Jahanian90655412009-02-05 18:00:27 +00005306#endif
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005307 if (IsSuper) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005308 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005309 Name += "objc_msgSendSuper2_stret_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005310 } else {
5311 Fn = ObjCTypes.getMessageSendStretFixupFn();
5312 Name += "objc_msgSend_stret_fixup";
5313 }
Douglas Gregor49b4d732010-06-22 23:07:26 +00005314 } else if (!IsSuper && ResultType->isRealFloatingType()) {
Daniel Dunbarc7d0e652009-06-26 18:32:06 +00005315 if (ResultType->isSpecificBuiltinType(BuiltinType::LongDouble)) {
5316 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5317 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005318 } else {
Daniel Dunbarc7d0e652009-06-26 18:32:06 +00005319 Fn = ObjCTypes.getMessageSendFixupFn();
5320 Name += "objc_msgSend_fixup";
Fariborz Jahanian18c435a2009-04-30 16:31:11 +00005321 }
Mike Stump658fe022009-07-30 22:28:39 +00005322 } else {
Fariborz Jahanian90655412009-02-05 18:00:27 +00005323#if 0
5324// unlike what is documented. gcc never generates this API!!
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005325 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005326 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005327 Name += "objc_msgSendId_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005328 } else
Fariborz Jahanian90655412009-02-05 18:00:27 +00005329#endif
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005330 if (IsSuper) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005331 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005332 Name += "objc_msgSendSuper2_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005333 } else {
5334 Fn = ObjCTypes.getMessageSendFixupFn();
5335 Name += "objc_msgSend_fixup";
5336 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005337 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005338 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005339 Name += '_';
5340 std::string SelName(Sel.getAsString());
5341 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005342 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005343 if (SelName[i] == ':')
5344 SelName[i] = '_';
5345 Name += SelName;
5346 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5347 if (!GV) {
Daniel Dunbare60aa052009-04-15 19:03:14 +00005348 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005349 std::vector<llvm::Constant*> Values(2);
5350 Values[0] = Fn;
5351 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005352 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005353 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005354 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005355 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005356 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005357 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005358 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005359 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005360 }
5361 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005362
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005363 CallArgList ActualArgs;
5364 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005365 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005366 ObjCTypes.MessageRefCPtrTy));
5367 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005368 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005369 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005370 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5371 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian35afdfc2009-02-14 21:25:36 +00005372 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005373 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005374 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005375 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005376}
5377
5378/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005379CodeGen::RValue
5380CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005381 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005382 QualType ResultType,
5383 Selector Sel,
5384 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005385 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005386 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005387 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005388 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005389 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5390 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005391 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005392 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005393 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005394 Receiver, CGF.getContext().getObjCIdType(),
5395 false, CallArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005396}
5397
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005398llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005399CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005400 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5401
Daniel Dunbara6468342009-03-02 05:18:14 +00005402 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005403 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005404 false, llvm::GlobalValue::ExternalLinkage,
5405 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005406 }
5407
5408 return GV;
5409}
5410
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005411llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5412 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005413 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005414
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005415 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005416 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005417 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005418 Entry =
5419 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005420 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005421 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005422 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005423 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005424 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005425 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005426 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005427 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005428 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005429
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005430 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005431}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005432
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005433llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005434CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005435 const ObjCInterfaceDecl *ID) {
5436 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005437
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005438 if (!Entry) {
5439 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5440 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005441 Entry =
5442 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005443 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005444 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005445 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005446 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005447 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005448 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005449 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005450 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005451 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005452
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005453 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005454}
5455
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005456/// EmitMetaClassRef - Return a Value * of the address of _class_t
5457/// meta-data
5458///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005459llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5460 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005461 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5462 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005463 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005464
Daniel Dunbar15894b72009-04-07 05:48:37 +00005465 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005466 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005467 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005468 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005469 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005470 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005471 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005472 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005473 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005474 ObjCTypes.ClassnfABIPtrTy));
5475
Daniel Dunbare60aa052009-04-15 19:03:14 +00005476 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005477 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005478
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005479 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005480}
5481
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005482/// GetClass - Return a reference to the class for the given interface
5483/// decl.
5484llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5485 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005486 if (ID->hasAttr<WeakImportAttr>()) {
5487 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5488 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5489 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5490 }
5491
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005492 return EmitClassRef(Builder, ID);
5493}
5494
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005495/// Generates a message send where the super is the receiver. This is
5496/// a message send to self with special delivery semantics indicating
5497/// which class's method should be called.
5498CodeGen::RValue
5499CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005500 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005501 QualType ResultType,
5502 Selector Sel,
5503 const ObjCInterfaceDecl *Class,
5504 bool isCategoryImpl,
5505 llvm::Value *Receiver,
5506 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005507 const CodeGen::CallArgList &CallArgs,
5508 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005509 // ...
5510 // Create and init a super structure; this is a (receiver, class)
5511 // pair we will pass to objc_msgSendSuper.
5512 llvm::Value *ObjCSuper =
5513 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005514
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005515 llvm::Value *ReceiverAsObject =
5516 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5517 CGF.Builder.CreateStore(ReceiverAsObject,
5518 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005519
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005520 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005521 llvm::Value *Target;
5522 if (IsClassMessage) {
5523 if (isCategoryImpl) {
5524 // Message sent to "super' in a class method defined in
5525 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005526 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005527 Target = CGF.Builder.CreateStructGEP(Target, 0);
5528 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005529 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005530 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005531 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005532 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005533
Mike Stump18bb9282009-05-16 07:57:57 +00005534 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5535 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005536 const llvm::Type *ClassTy =
5537 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5538 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5539 CGF.Builder.CreateStore(Target,
5540 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005541
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005542 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005543 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5544 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005545 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005546 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005547 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005548 ObjCSuper, ObjCTypes.SuperPtrCTy,
5549 true, CallArgs);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005550}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005551
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005552llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005553 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005554 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005555
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005556 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005557 llvm::Constant *Casted =
5558 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5559 ObjCTypes.SelectorPtrTy);
5560 Entry =
5561 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5562 llvm::GlobalValue::InternalLinkage,
5563 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005564 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005565 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005566 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005567
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005568 if (lval)
5569 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005570 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005571}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005572/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005573/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005574///
5575void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005576 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005577 llvm::Value *dst,
5578 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005579 const llvm::Type * SrcTy = src->getType();
5580 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005581 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005582 assert(Size <= 8 && "does not support size > 8");
5583 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5584 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005585 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5586 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005587 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5588 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005589 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5590 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005591 return;
5592}
5593
5594/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5595/// objc_assign_strongCast (id src, id *dst)
5596///
5597void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005598 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005599 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005600 const llvm::Type * SrcTy = src->getType();
5601 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005602 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005603 assert(Size <= 8 && "does not support size > 8");
5604 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005605 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005606 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5607 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005608 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5609 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005610 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005611 src, dst, "weakassign");
5612 return;
5613}
5614
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005615void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005616 CodeGen::CodeGenFunction &CGF,
5617 llvm::Value *DestPtr,
5618 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005619 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005620 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5621 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005622 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005623 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005624 return;
5625}
5626
Fariborz Jahanian06292952009-02-16 22:52:32 +00005627/// EmitObjCWeakRead - Code gen for loading value of a __weak
5628/// object: objc_read_weak (id *src)
5629///
5630llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005631 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005632 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005633 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005634 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5635 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005636 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005637 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005638 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005639 return read_weak;
5640}
5641
5642/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5643/// objc_assign_weak (id src, id *dst)
5644///
5645void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005646 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005647 const llvm::Type * SrcTy = src->getType();
5648 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005649 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005650 assert(Size <= 8 && "does not support size > 8");
5651 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5652 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005653 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5654 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005655 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5656 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005657 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005658 src, dst, "weakassign");
5659 return;
5660}
5661
5662/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5663/// objc_assign_global (id src, id *dst)
5664///
5665void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005666 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005667 const llvm::Type * SrcTy = src->getType();
5668 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005669 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005670 assert(Size <= 8 && "does not support size > 8");
5671 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5672 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005673 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5674 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005675 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5676 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005677 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005678 src, dst, "globalassign");
5679 return;
5680}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005681
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005682void
John McCallbd309292010-07-06 01:34:17 +00005683CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5684 const ObjCAtSynchronizedStmt &S) {
5685 // Evaluate the lock operand. This should dominate the cleanup.
5686 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005687
John McCallbd309292010-07-06 01:34:17 +00005688 // Acquire the lock.
5689 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5690 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5691 ->setDoesNotThrow();
5692
5693 // Register an all-paths cleanup to release the lock.
5694 {
John McCall2b7fc382010-07-13 20:32:21 +00005695 CodeGenFunction::CleanupBlock ReleaseScope(CGF, NormalAndEHCleanup);
John McCallbd309292010-07-06 01:34:17 +00005696
5697 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
5698 ->setDoesNotThrow();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005699 }
5700
John McCallbd309292010-07-06 01:34:17 +00005701 // Emit the body of the statement.
5702 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005703
John McCallbd309292010-07-06 01:34:17 +00005704 // Pop the lock-release cleanup.
5705 CGF.PopCleanupBlock();
5706}
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005707
John McCallbd309292010-07-06 01:34:17 +00005708namespace {
5709 struct CatchHandler {
5710 const VarDecl *Variable;
5711 const Stmt *Body;
5712 llvm::BasicBlock *Block;
5713 llvm::Value *TypeInfo;
5714 };
5715}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005716
John McCallbd309292010-07-06 01:34:17 +00005717void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5718 const ObjCAtTryStmt &S) {
5719 // Jump destination for falling out of catch bodies.
5720 CodeGenFunction::JumpDest Cont;
5721 if (S.getNumCatchStmts())
5722 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005723
John McCallbd309292010-07-06 01:34:17 +00005724 CodeGenFunction::FinallyInfo FinallyInfo;
5725 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
5726 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
5727 ObjCTypes.getObjCBeginCatchFn(),
5728 ObjCTypes.getObjCEndCatchFn(),
5729 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005730
John McCallbd309292010-07-06 01:34:17 +00005731 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005732
John McCallbd309292010-07-06 01:34:17 +00005733 // Enter the catch, if there is one.
5734 if (S.getNumCatchStmts()) {
5735 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
5736 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregor46a572b2010-04-26 16:46:50 +00005737 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005738
John McCallbd309292010-07-06 01:34:17 +00005739 Handlers.push_back(CatchHandler());
5740 CatchHandler &Handler = Handlers.back();
5741 Handler.Variable = CatchDecl;
5742 Handler.Body = CatchStmt->getCatchBody();
5743 Handler.Block = CGF.createBasicBlock("catch");
5744
5745 // @catch(...) always matches.
Douglas Gregor96c79492010-04-23 22:50:49 +00005746 if (!CatchDecl) {
John McCallbd309292010-07-06 01:34:17 +00005747 Handler.TypeInfo = 0; // catch-all
5748 // Don't consider any other catches.
Douglas Gregor96c79492010-04-23 22:50:49 +00005749 break;
5750 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005751
John McCallbd309292010-07-06 01:34:17 +00005752 // There's a particular fixed type info for 'id'.
Douglas Gregor96c79492010-04-23 22:50:49 +00005753 if (CatchDecl->getType()->isObjCIdType() ||
5754 CatchDecl->getType()->isObjCQualifiedIdType()) {
5755 llvm::Value *IDEHType =
5756 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5757 if (!IDEHType)
5758 IDEHType =
5759 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5760 false,
5761 llvm::GlobalValue::ExternalLinkage,
5762 0, "OBJC_EHTYPE_id");
John McCallbd309292010-07-06 01:34:17 +00005763 Handler.TypeInfo = IDEHType;
Douglas Gregor96c79492010-04-23 22:50:49 +00005764 } else {
5765 // All other types should be Objective-C interface pointer types.
5766 const ObjCObjectPointerType *PT =
5767 CatchDecl->getType()->getAs<ObjCObjectPointerType>();
5768 assert(PT && "Invalid @catch type.");
5769 const ObjCInterfaceType *IT = PT->getInterfaceType();
5770 assert(IT && "Invalid @catch type.");
John McCallbd309292010-07-06 01:34:17 +00005771 Handler.TypeInfo = GetInterfaceEHType(IT->getDecl(), false);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005772 }
5773 }
John McCallbd309292010-07-06 01:34:17 +00005774
5775 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
5776 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
5777 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005778 }
John McCallbd309292010-07-06 01:34:17 +00005779
5780 // Emit the try body.
5781 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005782
John McCallbd309292010-07-06 01:34:17 +00005783 // Leave the try.
5784 if (S.getNumCatchStmts())
5785 CGF.EHStack.popCatch();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005786
John McCallbd309292010-07-06 01:34:17 +00005787 // Remember where we were.
5788 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005789
John McCallbd309292010-07-06 01:34:17 +00005790 // Emit the handlers.
5791 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
5792 CatchHandler &Handler = Handlers[I];
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005793
John McCallbd309292010-07-06 01:34:17 +00005794 CGF.EmitBlock(Handler.Block);
5795 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005796
John McCallbd309292010-07-06 01:34:17 +00005797 // Enter the catch.
5798 llvm::CallInst *Exn =
5799 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
5800 "exn.adjusted");
5801 Exn->setDoesNotThrow();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005802
John McCallbd309292010-07-06 01:34:17 +00005803 // Add a cleanup to leave the catch.
5804 {
John McCall2b7fc382010-07-13 20:32:21 +00005805 CodeGenFunction::CleanupBlock EndCatchBlock(CGF, NormalAndEHCleanup);
John McCallbd309292010-07-06 01:34:17 +00005806
5807 // __objc_end_catch never throws.
5808 CGF.Builder.CreateCall(ObjCTypes.getObjCEndCatchFn())
5809 ->setDoesNotThrow();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005810 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005811
John McCallbd309292010-07-06 01:34:17 +00005812 // Bind the catch parameter if it exists.
5813 if (const VarDecl *CatchParam = Handler.Variable) {
5814 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
5815 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005816
John McCallbd309292010-07-06 01:34:17 +00005817 CGF.EmitLocalBlockVarDecl(*CatchParam);
5818 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005819 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005820
John McCallbd309292010-07-06 01:34:17 +00005821 CGF.ObjCEHValueStack.push_back(Exn);
5822 CGF.EmitStmt(Handler.Body);
5823 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005824
John McCallbd309292010-07-06 01:34:17 +00005825 // Leave the earlier cleanup.
5826 CGF.PopCleanupBlock();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005827
John McCallbd309292010-07-06 01:34:17 +00005828 CGF.EmitBranchThroughCleanup(Cont);
5829 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005830
John McCallbd309292010-07-06 01:34:17 +00005831 // Go back to the try-statement fallthrough.
5832 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005833
John McCallbd309292010-07-06 01:34:17 +00005834 // Pop out of the normal cleanup on the finally.
5835 if (S.getFinallyStmt())
5836 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005837
John McCallbd309292010-07-06 01:34:17 +00005838 if (Cont.Block)
5839 CGF.EmitBlock(Cont.Block);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005840}
5841
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005842/// EmitThrowStmt - Generate code for a throw statement.
5843void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5844 const ObjCAtThrowStmt &S) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005845 llvm::Value *Exception;
Fariborz Jahanian3336de12010-05-28 17:34:43 +00005846 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005847 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005848 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian3336de12010-05-28 17:34:43 +00005849 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005850 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005851 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005852 "Unexpected rethrow outside @catch block.");
5853 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian3336de12010-05-28 17:34:43 +00005854 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005855 }
5856
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005857 llvm::Value *ExceptionAsObject =
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005858 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5859 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5860 if (InvokeDest) {
Fariborz Jahanian3336de12010-05-28 17:34:43 +00005861 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallbd309292010-07-06 01:34:17 +00005862 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005863 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallbd309292010-07-06 01:34:17 +00005864 } else {
5865 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
5866 ->setDoesNotReturn();
5867 CGF.Builder.CreateUnreachable();
5868 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005869
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005870 // Clear the insertion point to indicate we are in unreachable code.
5871 CGF.Builder.ClearInsertionPoint();
5872}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005873
5874llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005875CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005876 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005877 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005878
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005879 // If we don't need a definition, return the entry if found or check
5880 // if we use an external reference.
5881 if (!ForDefinition) {
5882 if (Entry)
5883 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00005884
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005885 // If this type (or a super class) has the __objc_exception__
5886 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005887 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005888 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005889 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005890 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005891 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005892 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00005893 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005894 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005895
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005896 // Otherwise we need to either make a new entry or fill in the
5897 // initializer.
5898 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00005899 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005900 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005901 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005902 CGM.getModule().getGlobalVariable(VTableName);
5903 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005904 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5905 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005906 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005907 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005908
Chris Lattner5e016ae2010-06-27 07:15:29 +00005909 llvm::Value *VTableIdx =
5910 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005911
5912 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00005913 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005914 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005915 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00005916 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00005917 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005918
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005919 if (Entry) {
5920 Entry->setInitializer(Init);
5921 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00005922 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005923 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005924 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005925 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00005926 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005927 }
5928
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005929 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar15894b72009-04-07 05:48:37 +00005930 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00005931 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
5932 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005933
5934 if (ForDefinition) {
5935 Entry->setSection("__DATA,__objc_const");
5936 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
5937 } else {
5938 Entry->setSection("__DATA,__datacoal_nt,coalesced");
5939 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005940
5941 return Entry;
5942}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005943
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00005944/* *** */
5945
Daniel Dunbarb036db82008-08-13 03:21:16 +00005946CodeGen::CGObjCRuntime *
5947CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar303e2c22008-08-11 02:45:11 +00005948 return new CGObjCMac(CGM);
5949}
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005950
5951CodeGen::CGObjCRuntime *
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00005952CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00005953 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00005954}