blob: aaffa6c98e7d6501949a94d63dc5b5f159473e2f [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"
John McCall5c08ab92010-07-13 22:12:14 +000035#include "llvm/Support/CallSite.h"
Daniel Dunbard027a922009-09-07 00:20:42 +000036#include "llvm/Support/raw_ostream.h"
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +000037#include "llvm/Target/TargetData.h"
Torok Edwindb714922009-08-24 13:25:12 +000038#include <cstdio>
Daniel Dunbar303e2c22008-08-11 02:45:11 +000039
40using namespace clang;
Daniel Dunbar41cf9de2008-09-09 01:06:48 +000041using namespace CodeGen;
Daniel Dunbar303e2c22008-08-11 02:45:11 +000042
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000043// Common CGObjCRuntime functions, these don't belong here, but they
44// don't belong in CGObjCRuntime either so we will live with it for
45// now.
46
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000047static uint64_t LookupFieldBitOffset(CodeGen::CodeGenModule &CGM,
48 const ObjCInterfaceDecl *OID,
Daniel Dunbar961202372009-05-03 12:57:56 +000049 const ObjCImplementationDecl *ID,
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000050 const ObjCIvarDecl *Ivar) {
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000051 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000052
Daniel Dunbar7e5aba42010-04-02 22:29:40 +000053 // FIXME: We should eliminate the need to have ObjCImplementationDecl passed
54 // in here; it should never be necessary because that should be the lexical
55 // decl context for the ivar.
Daniel Dunbar031d4d42010-04-02 15:43:29 +000056
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000057 // If we know have an implementation (and the ivar is in it) then
58 // look up in the implementation layout.
Daniel Dunbar59e476b2009-08-03 17:06:42 +000059 const ASTRecordLayout *RL;
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000060 if (ID && ID->getClassInterface() == Container)
61 RL = &CGM.getContext().getASTObjCImplementationLayout(ID);
62 else
63 RL = &CGM.getContext().getASTObjCInterfaceLayout(Container);
Daniel Dunbar8c7f9812010-04-02 21:14:02 +000064
65 // Compute field index.
66 //
67 // FIXME: The index here is closely tied to how ASTContext::getObjCLayout is
68 // implemented. This should be fixed to get the information from the layout
69 // directly.
70 unsigned Index = 0;
71 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
72 CGM.getContext().ShallowCollectObjCIvars(Container, Ivars);
73 for (unsigned k = 0, e = Ivars.size(); k != e; ++k) {
74 if (Ivar == Ivars[k])
75 break;
76 ++Index;
77 }
78 assert(Index != Ivars.size() && "Ivar is not inside container!");
79
Daniel Dunbar80b4eef2009-05-03 13:15:50 +000080 return RL->getFieldOffset(Index);
Daniel Dunbar0cec95f2009-05-03 08:55:17 +000081}
82
83uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
84 const ObjCInterfaceDecl *OID,
85 const ObjCIvarDecl *Ivar) {
Daniel Dunbar961202372009-05-03 12:57:56 +000086 return LookupFieldBitOffset(CGM, OID, 0, Ivar) / 8;
87}
88
89uint64_t CGObjCRuntime::ComputeIvarBaseOffset(CodeGen::CodeGenModule &CGM,
90 const ObjCImplementationDecl *OID,
91 const ObjCIvarDecl *Ivar) {
92 return LookupFieldBitOffset(CGM, OID->getClassInterface(), OID, Ivar) / 8;
Daniel Dunbar9fd114d2009-04-22 07:32:20 +000093}
94
95LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
96 const ObjCInterfaceDecl *OID,
97 llvm::Value *BaseValue,
98 const ObjCIvarDecl *Ivar,
99 unsigned CVRQualifiers,
100 llvm::Value *Offset) {
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000101 // Compute (type*) ( (char *) BaseValue + Offset)
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000102 const llvm::Type *I8Ptr = llvm::Type::getInt8PtrTy(CGF.getLLVMContext());
Daniel Dunbar0cec95f2009-05-03 08:55:17 +0000103 QualType IvarTy = Ivar->getType();
104 const llvm::Type *LTy = CGF.CGM.getTypes().ConvertTypeForMem(IvarTy);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000105 llvm::Value *V = CGF.Builder.CreateBitCast(BaseValue, I8Ptr);
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000106 V = CGF.Builder.CreateGEP(V, Offset, "add.ptr");
Owen Anderson9793f0e2009-07-29 22:16:19 +0000107 V = CGF.Builder.CreateBitCast(V, llvm::PointerType::getUnqual(LTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000108
John McCall8ccfcb52009-09-24 19:53:00 +0000109 Qualifiers Quals = CGF.MakeQualifiers(IvarTy);
110 Quals.addCVRQualifiers(CVRQualifiers);
111
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000112 if (!Ivar->isBitField())
113 return LValue::MakeAddr(V, Quals);
Daniel Dunbar961202372009-05-03 12:57:56 +0000114
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000115 // We need to compute the bit offset for the bit-field, the offset is to the
116 // byte. Note, there is a subtle invariant here: we can only call this routine
117 // on non-synthesized ivars but we may be called for synthesized ivars.
118 // However, a synthesized ivar can never be a bit-field, so this is safe.
119 uint64_t BitOffset = LookupFieldBitOffset(CGF.CGM, OID, 0, Ivar) % 8;
120 uint64_t BitFieldSize =
121 Ivar->getBitWidth()->EvaluateAsInt(CGF.getContext()).getZExtValue();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000122
Daniel Dunbardc406b82010-04-05 21:36:35 +0000123 // Allocate a new CGBitFieldInfo object to describe this access.
124 //
125 // FIXME: This is incredibly wasteful, these should be uniqued or part of some
126 // layout object. However, this is blocked on other cleanups to the
127 // Objective-C code, so for now we just live with allocating a bunch of these
128 // objects.
Daniel Dunbardc406b82010-04-05 21:36:35 +0000129
Daniel Dunbarb935b932010-04-13 20:58:55 +0000130 // We always construct a single, possibly unaligned, access for this case.
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000131 CGBitFieldInfo::AccessInfo AI;
Daniel Dunbarb935b932010-04-13 20:58:55 +0000132 AI.FieldIndex = 0;
133 AI.FieldByteOffset = 0;
134 AI.FieldBitStart = BitOffset;
135 AI.AccessWidth = CGF.CGM.getContext().getTypeSize(IvarTy);
136 AI.AccessAlignment = 0;
137 AI.TargetBitOffset = 0;
138 AI.TargetBitWidth = BitFieldSize;
139
Daniel Dunbar9c78d632010-04-15 05:09:32 +0000140 CGBitFieldInfo *Info =
141 new (CGF.CGM.getContext()) CGBitFieldInfo(BitFieldSize, 1, &AI,
142 IvarTy->isSignedIntegerType());
143
Daniel Dunbarb76c4cd2010-04-05 16:20:33 +0000144 // FIXME: We need to set a very conservative alignment on this, or make sure
145 // that the runtime is doing the right thing.
Daniel Dunbar196ea442010-04-06 01:07:44 +0000146 return LValue::MakeBitfield(V, *Info, Quals.getCVRQualifiers());
Daniel Dunbar9fd114d2009-04-22 07:32:20 +0000147}
148
149///
150
Daniel Dunbar303e2c22008-08-11 02:45:11 +0000151namespace {
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000152
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000153typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbareb1f9a22008-08-27 02:31:56 +0000154
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000155// FIXME: We should find a nicer way to make the labels for metadata, string
156// concatenation is lame.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000157
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000158class ObjCCommonTypesHelper {
Owen Anderson170229f2009-07-14 23:10:40 +0000159protected:
160 llvm::LLVMContext &VMContext;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000161
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000162private:
163 llvm::Constant *getMessageSendFn() const {
164 // id objc_msgSend (id, SEL, ...)
165 std::vector<const llvm::Type*> Params;
166 Params.push_back(ObjectPtrTy);
167 Params.push_back(SelectorPtrTy);
168 return
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000169 CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
170 Params, true),
171 "objc_msgSend");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000172 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000173
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000174 llvm::Constant *getMessageSendStretFn() const {
175 // id objc_msgSend_stret (id, SEL, ...)
176 std::vector<const llvm::Type*> Params;
177 Params.push_back(ObjectPtrTy);
178 Params.push_back(SelectorPtrTy);
179 return
Owen Anderson41a75022009-08-13 21:57:51 +0000180 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000181 Params, true),
182 "objc_msgSend_stret");
183
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000184 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000185
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000186 llvm::Constant *getMessageSendFpretFn() const {
187 // FIXME: This should be long double on x86_64?
188 // [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
189 std::vector<const llvm::Type*> Params;
190 Params.push_back(ObjectPtrTy);
191 Params.push_back(SelectorPtrTy);
192 return
Owen Anderson41a75022009-08-13 21:57:51 +0000193 CGM.CreateRuntimeFunction(llvm::FunctionType::get(
194 llvm::Type::getDoubleTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000195 Params,
196 true),
197 "objc_msgSend_fpret");
198
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000199 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000200
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000201 llvm::Constant *getMessageSendSuperFn() const {
202 // id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
203 const char *SuperName = "objc_msgSendSuper";
204 std::vector<const llvm::Type*> Params;
205 Params.push_back(SuperPtrTy);
206 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000207 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000208 Params, true),
209 SuperName);
210 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000211
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000212 llvm::Constant *getMessageSendSuperFn2() const {
213 // id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
214 const char *SuperName = "objc_msgSendSuper2";
215 std::vector<const llvm::Type*> Params;
216 Params.push_back(SuperPtrTy);
217 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000218 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000219 Params, true),
220 SuperName);
221 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000222
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000223 llvm::Constant *getMessageSendSuperStretFn() const {
224 // void objc_msgSendSuper_stret(void * stretAddr, struct objc_super *super,
225 // SEL op, ...)
226 std::vector<const llvm::Type*> Params;
227 Params.push_back(Int8PtrTy);
228 Params.push_back(SuperPtrTy);
229 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000230 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000231 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000232 Params, true),
233 "objc_msgSendSuper_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000234 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000235
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000236 llvm::Constant *getMessageSendSuperStretFn2() const {
237 // void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
238 // SEL op, ...)
239 std::vector<const llvm::Type*> Params;
240 Params.push_back(Int8PtrTy);
241 Params.push_back(SuperPtrTy);
242 Params.push_back(SelectorPtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000243 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000244 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000245 Params, true),
246 "objc_msgSendSuper2_stret");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000247 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000248
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000249 llvm::Constant *getMessageSendSuperFpretFn() const {
250 // There is no objc_msgSendSuper_fpret? How can that work?
251 return getMessageSendSuperFn();
252 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000253
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000254 llvm::Constant *getMessageSendSuperFpretFn2() const {
255 // There is no objc_msgSendSuper_fpret? How can that work?
256 return getMessageSendSuperFn2();
257 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000258
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000259protected:
260 CodeGen::CodeGenModule &CGM;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000261
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000262public:
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +0000263 const llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000264 const llvm::Type *Int8PtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000265
Daniel Dunbar5d715592008-08-12 05:28:47 +0000266 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
267 const llvm::Type *ObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000268
Fariborz Jahanian406b1172008-11-18 20:18:11 +0000269 /// PtrObjectPtrTy - LLVM type for id *
270 const llvm::Type *PtrObjectPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000271
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000272 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Daniel Dunbar5d715592008-08-12 05:28:47 +0000273 const llvm::Type *SelectorPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000274 /// ProtocolPtrTy - LLVM type for external protocol handles
275 /// (typeof(Protocol))
276 const llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000277
Daniel Dunbarc722b852008-08-30 03:02:31 +0000278 // SuperCTy - clang type for struct objc_super.
279 QualType SuperCTy;
280 // SuperPtrCTy - clang type for struct objc_super *.
281 QualType SuperPtrCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000282
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000283 /// SuperTy - LLVM type for struct objc_super.
284 const llvm::StructType *SuperTy;
Daniel Dunbar97ff50d2008-08-23 09:25:55 +0000285 /// SuperPtrTy - LLVM type for struct objc_super *.
286 const llvm::Type *SuperPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000287
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000288 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
289 /// in GCC parlance).
290 const llvm::StructType *PropertyTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000291
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000292 /// PropertyListTy - LLVM type for struct objc_property_list
293 /// (_prop_list_t in GCC parlance).
294 const llvm::StructType *PropertyListTy;
295 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
296 const llvm::Type *PropertyListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000297
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000298 // MethodTy - LLVM type for struct objc_method.
299 const llvm::StructType *MethodTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000300
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000301 /// CacheTy - LLVM type for struct objc_cache.
302 const llvm::Type *CacheTy;
303 /// CachePtrTy - LLVM type for struct objc_cache *.
304 const llvm::Type *CachePtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000305
Chris Lattnerce8754e2009-04-22 02:44:54 +0000306 llvm::Constant *getGetPropertyFn() {
307 CodeGen::CodeGenTypes &Types = CGM.getTypes();
308 ASTContext &Ctx = CGM.getContext();
309 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
John McCall2da83a32010-02-26 00:48:12 +0000310 llvm::SmallVector<CanQualType,4> Params;
311 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
312 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000313 Params.push_back(IdType);
314 Params.push_back(SelType);
315 Params.push_back(Ctx.LongTy);
316 Params.push_back(Ctx.BoolTy);
317 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000318 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000319 FunctionType::ExtInfo()),
320 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000321 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
322 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000323
Chris Lattnerce8754e2009-04-22 02:44:54 +0000324 llvm::Constant *getSetPropertyFn() {
325 CodeGen::CodeGenTypes &Types = CGM.getTypes();
326 ASTContext &Ctx = CGM.getContext();
327 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
John McCall2da83a32010-02-26 00:48:12 +0000328 llvm::SmallVector<CanQualType,6> Params;
329 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
330 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattnerce8754e2009-04-22 02:44:54 +0000331 Params.push_back(IdType);
332 Params.push_back(SelType);
333 Params.push_back(Ctx.LongTy);
334 Params.push_back(IdType);
335 Params.push_back(Ctx.BoolTy);
336 Params.push_back(Ctx.BoolTy);
337 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000338 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000339 FunctionType::ExtInfo()),
340 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000341 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
342 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000343
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +0000344
345 llvm::Constant *getCopyStructFn() {
346 CodeGen::CodeGenTypes &Types = CGM.getTypes();
347 ASTContext &Ctx = CGM.getContext();
348 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
349 llvm::SmallVector<CanQualType,5> Params;
350 Params.push_back(Ctx.VoidPtrTy);
351 Params.push_back(Ctx.VoidPtrTy);
352 Params.push_back(Ctx.LongTy);
353 Params.push_back(Ctx.BoolTy);
354 Params.push_back(Ctx.BoolTy);
355 const llvm::FunctionType *FTy =
356 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
357 FunctionType::ExtInfo()),
358 false);
359 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
360 }
361
Chris Lattnerce8754e2009-04-22 02:44:54 +0000362 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000363 CodeGen::CodeGenTypes &Types = CGM.getTypes();
364 ASTContext &Ctx = CGM.getContext();
Chris Lattnerce8754e2009-04-22 02:44:54 +0000365 // void objc_enumerationMutation (id)
John McCall2da83a32010-02-26 00:48:12 +0000366 llvm::SmallVector<CanQualType,1> Params;
367 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Daniel Dunbar9d82da42009-07-11 20:32:50 +0000368 const llvm::FunctionType *FTy =
John McCallab26cfa2010-02-05 21:31:56 +0000369 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindolac50c27c2010-03-30 20:24:48 +0000370 FunctionType::ExtInfo()),
371 false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000372 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
373 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000374
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000375 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattnerce8754e2009-04-22 02:44:54 +0000376 llvm::Constant *getGcReadWeakFn() {
377 // id objc_read_weak (id *)
378 std::vector<const llvm::Type*> Args;
379 Args.push_back(ObjectPtrTy->getPointerTo());
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000380 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000381 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattnerce8754e2009-04-22 02:44:54 +0000382 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000383 }
384
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000385 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000386 llvm::Constant *getGcAssignWeakFn() {
387 // id objc_assign_weak (id, id *)
388 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
389 Args.push_back(ObjectPtrTy->getPointerTo());
390 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000391 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner6fdd57c2009-04-17 22:12:36 +0000392 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
393 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000394
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000395 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000396 llvm::Constant *getGcAssignGlobalFn() {
397 // id objc_assign_global(id, id *)
398 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
399 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000400 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000401 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000402 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
403 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000404
Fariborz Jahanian217af242010-07-20 20:30:03 +0000405 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
406 llvm::Constant *getGcAssignThreadLocalFn() {
407 // id objc_assign_threadlocal(id src, id * dest)
408 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
409 Args.push_back(ObjectPtrTy->getPointerTo());
410 llvm::FunctionType *FTy =
411 llvm::FunctionType::get(ObjectPtrTy, Args, false);
412 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
413 }
414
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000415 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000416 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000417 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner0a696a422009-04-22 02:38:11 +0000418 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
419 Args.push_back(ObjectPtrTy->getPointerTo());
Fariborz Jahanian7a95d722009-09-24 22:25:38 +0000420 Args.push_back(LongTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000421 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000422 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000423 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
424 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000425
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000426 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
427 llvm::Constant *GcMemmoveCollectableFn() {
428 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
429 std::vector<const llvm::Type*> Args(1, Int8PtrTy);
430 Args.push_back(Int8PtrTy);
431 Args.push_back(LongTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000432 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, Args, false);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +0000433 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
434 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000435
Fariborz Jahanianeee54df2009-01-22 00:37:21 +0000436 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000437 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian217af242010-07-20 20:30:03 +0000438 // id objc_assign_strongCast(id, id *)
Chris Lattner0a696a422009-04-22 02:38:11 +0000439 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
440 Args.push_back(ObjectPtrTy->getPointerTo());
Owen Anderson170229f2009-07-14 23:10:40 +0000441 llvm::FunctionType *FTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +0000442 llvm::FunctionType::get(ObjectPtrTy, Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000443 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
444 }
Anders Carlsson9ab53d12009-02-16 22:59:18 +0000445
446 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000447 llvm::Constant *getExceptionThrowFn() {
448 // void objc_exception_throw(id)
449 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
450 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000451 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000452 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
453 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000454
Fariborz Jahanian3336de12010-05-28 17:34:43 +0000455 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
456 llvm::Constant *getExceptionRethrowFn() {
457 // void objc_exception_rethrow(void)
458 std::vector<const llvm::Type*> Args;
459 llvm::FunctionType *FTy =
460 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, true);
461 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
462 }
463
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000464 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000465 llvm::Constant *getSyncEnterFn() {
466 // void objc_sync_enter (id)
467 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
468 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000469 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000470 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
471 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000472
Daniel Dunbar94ceb612009-02-24 01:43:46 +0000473 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattner0a696a422009-04-22 02:38:11 +0000474 llvm::Constant *getSyncExitFn() {
475 // void objc_sync_exit (id)
476 std::vector<const llvm::Type*> Args(1, ObjectPtrTy);
477 llvm::FunctionType *FTy =
Owen Anderson41a75022009-08-13 21:57:51 +0000478 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext), Args, false);
Chris Lattner0a696a422009-04-22 02:38:11 +0000479 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
480 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000481
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000482 llvm::Constant *getSendFn(bool IsSuper) const {
483 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
484 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000485
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000486 llvm::Constant *getSendFn2(bool IsSuper) const {
487 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
488 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000489
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000490 llvm::Constant *getSendStretFn(bool IsSuper) const {
491 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
492 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000493
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000494 llvm::Constant *getSendStretFn2(bool IsSuper) const {
495 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
496 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000497
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000498 llvm::Constant *getSendFpretFn(bool IsSuper) const {
499 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
500 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000501
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000502 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
503 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
504 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000505
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000506 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
507 ~ObjCCommonTypesHelper(){}
508};
Daniel Dunbarf6397fe2008-08-23 04:28:29 +0000509
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000510/// ObjCTypesHelper - Helper class that encapsulates lazy
511/// construction of varies types used during ObjC generation.
512class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000513public:
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000514 /// SymtabTy - LLVM type for struct objc_symtab.
515 const llvm::StructType *SymtabTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000516 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
517 const llvm::Type *SymtabPtrTy;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000518 /// ModuleTy - LLVM type for struct objc_module.
519 const llvm::StructType *ModuleTy;
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000520
Daniel Dunbarb036db82008-08-13 03:21:16 +0000521 /// ProtocolTy - LLVM type for struct objc_protocol.
522 const llvm::StructType *ProtocolTy;
523 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
524 const llvm::Type *ProtocolPtrTy;
525 /// ProtocolExtensionTy - LLVM type for struct
526 /// objc_protocol_extension.
527 const llvm::StructType *ProtocolExtensionTy;
528 /// ProtocolExtensionTy - LLVM type for struct
529 /// objc_protocol_extension *.
530 const llvm::Type *ProtocolExtensionPtrTy;
531 /// MethodDescriptionTy - LLVM type for struct
532 /// objc_method_description.
533 const llvm::StructType *MethodDescriptionTy;
534 /// MethodDescriptionListTy - LLVM type for struct
535 /// objc_method_description_list.
536 const llvm::StructType *MethodDescriptionListTy;
537 /// MethodDescriptionListPtrTy - LLVM type for struct
538 /// objc_method_description_list *.
539 const llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbarb036db82008-08-13 03:21:16 +0000540 /// ProtocolListTy - LLVM type for struct objc_property_list.
541 const llvm::Type *ProtocolListTy;
542 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
543 const llvm::Type *ProtocolListPtrTy;
Daniel Dunbar938a77f2008-08-22 20:34:54 +0000544 /// CategoryTy - LLVM type for struct objc_category.
545 const llvm::StructType *CategoryTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000546 /// ClassTy - LLVM type for struct objc_class.
547 const llvm::StructType *ClassTy;
548 /// ClassPtrTy - LLVM type for struct objc_class *.
549 const llvm::Type *ClassPtrTy;
550 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
551 const llvm::StructType *ClassExtensionTy;
552 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
553 const llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000554 // IvarTy - LLVM type for struct objc_ivar.
555 const llvm::StructType *IvarTy;
556 /// IvarListTy - LLVM type for struct objc_ivar_list.
557 const llvm::Type *IvarListTy;
558 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
559 const llvm::Type *IvarListPtrTy;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000560 /// MethodListTy - LLVM type for struct objc_method_list.
561 const llvm::Type *MethodListTy;
562 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
563 const llvm::Type *MethodListPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000564
Anders Carlsson9ff22482008-09-09 10:10:21 +0000565 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
566 const llvm::Type *ExceptionDataTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000567
Anders Carlsson9ff22482008-09-09 10:10:21 +0000568 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000569 llvm::Constant *getExceptionTryEnterFn() {
570 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000571 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000572 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000573 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000574 Params, false),
575 "objc_exception_try_enter");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000576 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000577
578 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000579 llvm::Constant *getExceptionTryExitFn() {
580 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000581 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
Owen Anderson170229f2009-07-14 23:10:40 +0000582 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000583 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000584 Params, false),
585 "objc_exception_try_exit");
Chris Lattnerc6406db2009-04-22 02:26:14 +0000586 }
Anders Carlsson9ff22482008-09-09 10:10:21 +0000587
588 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000589 llvm::Constant *getExceptionExtractFn() {
590 std::vector<const llvm::Type*> Params;
Owen Anderson9793f0e2009-07-29 22:16:19 +0000591 Params.push_back(llvm::PointerType::getUnqual(ExceptionDataTy));
592 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattnerc6406db2009-04-22 02:26:14 +0000593 Params, false),
594 "objc_exception_extract");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000595
Chris Lattnerc6406db2009-04-22 02:26:14 +0000596 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000597
Anders Carlsson9ff22482008-09-09 10:10:21 +0000598 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000599 llvm::Constant *getExceptionMatchFn() {
600 std::vector<const llvm::Type*> Params;
601 Params.push_back(ClassPtrTy);
602 Params.push_back(ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000603 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000604 llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000605 Params, false),
606 "objc_exception_match");
607
Chris Lattnerc6406db2009-04-22 02:26:14 +0000608 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000609
Anders Carlsson9ff22482008-09-09 10:10:21 +0000610 /// SetJmpFn - LLVM _setjmp function.
Chris Lattnerc6406db2009-04-22 02:26:14 +0000611 llvm::Constant *getSetJmpFn() {
612 std::vector<const llvm::Type*> Params;
Benjamin Kramerabd5b902009-10-13 10:07:13 +0000613 Params.push_back(llvm::Type::getInt32PtrTy(VMContext));
Chris Lattnerc6406db2009-04-22 02:26:14 +0000614 return
Owen Anderson41a75022009-08-13 21:57:51 +0000615 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerc6406db2009-04-22 02:26:14 +0000616 Params, false),
617 "_setjmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000618
Chris Lattnerc6406db2009-04-22 02:26:14 +0000619 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000620
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000621public:
622 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000623 ~ObjCTypesHelper() {}
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000624};
625
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000626/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000627/// modern abi
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000628class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000629public:
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000630
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000631 // MethodListnfABITy - LLVM for struct _method_list_t
632 const llvm::StructType *MethodListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000633
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000634 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
635 const llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000636
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000637 // ProtocolnfABITy = LLVM for struct _protocol_t
638 const llvm::StructType *ProtocolnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000639
Daniel Dunbar8de90f02009-02-15 07:36:20 +0000640 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
641 const llvm::Type *ProtocolnfABIPtrTy;
642
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000643 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
644 const llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000645
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000646 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
647 const llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000648
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000649 // ClassnfABITy - LLVM for struct _class_t
650 const llvm::StructType *ClassnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000651
Fariborz Jahanian71394042009-01-23 23:53:38 +0000652 // ClassnfABIPtrTy - LLVM for struct _class_t*
653 const llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000654
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000655 // IvarnfABITy - LLVM for struct _ivar_t
656 const llvm::StructType *IvarnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000657
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000658 // IvarListnfABITy - LLVM for struct _ivar_list_t
659 const llvm::StructType *IvarListnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000660
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000661 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
662 const llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000663
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000664 // ClassRonfABITy - LLVM for struct _class_ro_t
665 const llvm::StructType *ClassRonfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000666
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000667 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
668 const llvm::Type *ImpnfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000669
Fariborz Jahanian0232c052009-01-23 01:46:23 +0000670 // CategorynfABITy - LLVM for struct _category_t
671 const llvm::StructType *CategorynfABITy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000672
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000673 // New types for nonfragile abi messaging.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000674
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000675 // MessageRefTy - LLVM for:
676 // struct _message_ref_t {
677 // IMP messenger;
678 // SEL name;
679 // };
680 const llvm::StructType *MessageRefTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000681 // MessageRefCTy - clang type for struct _message_ref_t
682 QualType MessageRefCTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000683
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000684 // MessageRefPtrTy - LLVM for struct _message_ref_t*
685 const llvm::Type *MessageRefPtrTy;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +0000686 // MessageRefCPtrTy - clang type for struct _message_ref_t*
687 QualType MessageRefCPtrTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000688
Fariborz Jahanian4e87c832009-02-05 01:13:09 +0000689 // MessengerTy - Type of the messenger (shown as IMP above)
690 const llvm::FunctionType *MessengerTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000691
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000692 // SuperMessageRefTy - LLVM for:
693 // struct _super_message_ref_t {
694 // SUPER_IMP messenger;
695 // SEL name;
696 // };
697 const llvm::StructType *SuperMessageRefTy;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000698
Fariborz Jahanian82c72e12009-02-03 23:49:23 +0000699 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
700 const llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000701
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000702 llvm::Constant *getMessageSendFixupFn() {
703 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
704 std::vector<const llvm::Type*> Params;
705 Params.push_back(ObjectPtrTy);
706 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000707 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000708 Params, true),
709 "objc_msgSend_fixup");
710 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000711
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000712 llvm::Constant *getMessageSendFpretFixupFn() {
713 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
714 std::vector<const llvm::Type*> Params;
715 Params.push_back(ObjectPtrTy);
716 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000717 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000718 Params, true),
719 "objc_msgSend_fpret_fixup");
720 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000721
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000722 llvm::Constant *getMessageSendStretFixupFn() {
723 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
724 std::vector<const llvm::Type*> Params;
725 Params.push_back(ObjectPtrTy);
726 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000727 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000728 Params, true),
729 "objc_msgSend_stret_fixup");
730 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000731
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000732 llvm::Constant *getMessageSendIdFixupFn() {
733 // id objc_msgSendId_fixup(id, struct message_ref_t*, ...)
734 std::vector<const llvm::Type*> Params;
735 Params.push_back(ObjectPtrTy);
736 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000737 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000738 Params, true),
739 "objc_msgSendId_fixup");
740 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000741
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000742 llvm::Constant *getMessageSendIdStretFixupFn() {
743 // id objc_msgSendId_stret_fixup(id, struct message_ref_t*, ...)
744 std::vector<const llvm::Type*> Params;
745 Params.push_back(ObjectPtrTy);
746 Params.push_back(MessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000747 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000748 Params, true),
749 "objc_msgSendId_stret_fixup");
750 }
751 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000752 // id objc_msgSendSuper2_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_fixup");
760 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000761
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000762 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000763 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000764 // struct _super_message_ref_t*, ...)
765 std::vector<const llvm::Type*> Params;
766 Params.push_back(SuperPtrTy);
767 Params.push_back(SuperMessageRefPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000768 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
Chris Lattner2dfdb3e2009-04-22 02:53:24 +0000769 Params, true),
770 "objc_msgSendSuper2_stret_fixup");
771 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000772
773
774
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000775 /// EHPersonalityPtr - LLVM value for an i8* to the Objective-C
776 /// exception personality function.
Chris Lattnerdcceee72009-04-06 16:53:45 +0000777 llvm::Value *getEHPersonalityPtr() {
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000778 llvm::Constant *Personality =
Owen Anderson41a75022009-08-13 21:57:51 +0000779 CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getInt32Ty(VMContext),
Chris Lattnerdcceee72009-04-06 16:53:45 +0000780 true),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000781 "__objc_personality_v0");
Owen Andersonade90fd2009-07-29 18:54:39 +0000782 return llvm::ConstantExpr::getBitCast(Personality, Int8PtrTy);
Chris Lattnerdcceee72009-04-06 16:53:45 +0000783 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000784
Chris Lattnera7c00b42009-04-22 02:15:23 +0000785 llvm::Constant *getUnwindResumeOrRethrowFn() {
786 std::vector<const llvm::Type*> Params;
787 Params.push_back(Int8PtrTy);
Owen Anderson170229f2009-07-14 23:10:40 +0000788 return CGM.CreateRuntimeFunction(
Owen Anderson41a75022009-08-13 21:57:51 +0000789 llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000790 Params, false),
Daniel Dunbar3241d402010-02-10 18:49:11 +0000791 (CGM.getLangOptions().SjLjExceptions ? "_Unwind_SjLj_Resume" :
792 "_Unwind_Resume_or_Rethrow"));
Chris Lattnera7c00b42009-04-22 02:15:23 +0000793 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000794
Chris Lattnera7c00b42009-04-22 02:15:23 +0000795 llvm::Constant *getObjCEndCatchFn() {
Owen Anderson41a75022009-08-13 21:57:51 +0000796 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(llvm::Type::getVoidTy(VMContext),
Chris Lattner3dd1b4b2009-07-01 04:13:52 +0000797 false),
Chris Lattnera7c00b42009-04-22 02:15:23 +0000798 "objc_end_catch");
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000799
Chris Lattnera7c00b42009-04-22 02:15:23 +0000800 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000801
Chris Lattnera7c00b42009-04-22 02:15:23 +0000802 llvm::Constant *getObjCBeginCatchFn() {
803 std::vector<const llvm::Type*> Params;
804 Params.push_back(Int8PtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +0000805 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
Chris Lattnera7c00b42009-04-22 02:15:23 +0000806 Params, false),
807 "objc_begin_catch");
808 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +0000809
810 const llvm::StructType *EHTypeTy;
811 const llvm::Type *EHTypePtrTy;
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +0000812
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +0000813 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
814 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000815};
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000816
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000817class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000818public:
819 // FIXME - accessibility
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000820 class GC_IVAR {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +0000821 public:
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000822 unsigned ivar_bytepos;
823 unsigned ivar_size;
824 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000825 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar22b0ada2009-04-23 01:29:05 +0000826
827 // Allow sorting based on byte pos.
828 bool operator<(const GC_IVAR &b) const {
829 return ivar_bytepos < b.ivar_bytepos;
830 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000831 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000832
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000833 class SKIP_SCAN {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000834 public:
835 unsigned skip;
836 unsigned scan;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000837 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar7b89ace2009-05-03 13:44:42 +0000838 : skip(_skip), scan(_scan) {}
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000839 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000840
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000841protected:
842 CodeGen::CodeGenModule &CGM;
Owen Andersonae86c192009-07-13 04:10:07 +0000843 llvm::LLVMContext &VMContext;
Fariborz Jahanian279eda62009-01-21 22:04:16 +0000844 // FIXME! May not be needing this after all.
Daniel Dunbar8b8683f2008-08-12 00:12:39 +0000845 unsigned ObjCABI;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000846
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +0000847 // gc ivar layout bitmap calculation helper caches.
848 llvm::SmallVector<GC_IVAR, 16> SkipIvars;
849 llvm::SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000850
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000851 /// LazySymbols - Symbols to generate a lazy reference for. See
852 /// DefinedSymbols and FinishModule().
Daniel Dunbard027a922009-09-07 00:20:42 +0000853 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000854
Daniel Dunbarc61d0e92008-08-25 06:02:07 +0000855 /// DefinedSymbols - External symbols which are defined by this
856 /// module. The symbols in this list and LazySymbols are used to add
857 /// special linker symbols which ensure that Objective-C modules are
858 /// linked properly.
Daniel Dunbard027a922009-09-07 00:20:42 +0000859 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000860
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +0000861 /// ClassNames - uniqued class names.
Daniel Dunbarb036db82008-08-13 03:21:16 +0000862 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000863
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000864 /// MethodVarNames - uniqued method variable names.
865 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000866
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +0000867 /// DefinedCategoryNames - list of category names in form Class_Category.
868 llvm::SetVector<std::string> DefinedCategoryNames;
869
Daniel Dunbarb036db82008-08-13 03:21:16 +0000870 /// MethodVarTypes - uniqued method type signatures. We have to use
871 /// a StringMap here because have no other unique reference.
872 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000873
Daniel Dunbar3c76cb52008-08-26 21:51:14 +0000874 /// MethodDefinitions - map of methods which have been defined in
875 /// this translation unit.
876 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000877
Daniel Dunbar80a840b2008-08-23 00:19:03 +0000878 /// PropertyNames - uniqued method variable names.
879 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000880
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000881 /// ClassReferences - uniqued class references.
882 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000883
Daniel Dunbarcb515c82008-08-12 03:39:23 +0000884 /// SelectorReferences - uniqued selector references.
885 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000886
Daniel Dunbarb036db82008-08-13 03:21:16 +0000887 /// Protocols - Protocols for which an objc_protocol structure has
888 /// been emitted. Forward declarations are handled by creating an
889 /// empty structure whose initializer is filled in when/if defined.
890 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000891
Daniel Dunbarc475d422008-10-29 22:36:39 +0000892 /// DefinedProtocols - Protocols which have actually been
893 /// defined. We should not need this, see FIXME in GenerateProtocol.
894 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000895
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000896 /// DefinedClasses - List of defined classes.
897 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000898
899 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
900 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000901
Daniel Dunbar22d82ed2008-08-21 04:36:09 +0000902 /// DefinedCategories - List of defined categories.
903 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000904
Daniel Dunbar9a017d72009-05-15 22:33:15 +0000905 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
906 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000907
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000908 /// GetNameForMethod - Return a name for the given method.
909 /// \param[out] NameOut - The return value.
910 void GetNameForMethod(const ObjCMethodDecl *OMD,
911 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +0000912 llvm::SmallVectorImpl<char> &NameOut);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000913
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000914 /// GetMethodVarName - Return a unique constant for the given
915 /// selector's name. The return value has type char *.
916 llvm::Constant *GetMethodVarName(Selector Sel);
917 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
918 llvm::Constant *GetMethodVarName(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000919
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000920 /// GetMethodVarType - Return a unique constant for the given
921 /// selector's name. The return value has type char *.
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000922
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000923 // FIXME: This is a horrible name.
924 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D);
Daniel Dunbarf5c18462009-04-20 06:54:31 +0000925 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000926
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000927 /// GetPropertyName - Return a unique constant for the given
928 /// name. The return value has type char *.
929 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000930
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +0000931 // FIXME: This can be dropped once string functions are unified.
932 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
933 const Decl *Container);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000934
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +0000935 /// GetClassName - Return a unique constant for the given selector's
936 /// name. The return value has type char *.
937 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000938
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000939 /// BuildIvarLayout - Builds ivar layout bitmap for the class
940 /// implementation for the __strong or __weak case.
941 ///
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000942 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
943 bool ForStrongLayout);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000944
Daniel Dunbar15bd8882009-05-03 14:10:34 +0000945 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000946 unsigned int BytePos, bool ForStrongLayout,
947 bool &HasUnion);
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +0000948 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000949 const llvm::StructLayout *Layout,
Fariborz Jahanian524bb202009-03-10 16:22:08 +0000950 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +0000951 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +0000952 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahaniana123b642009-04-24 16:17:09 +0000953 bool &HasUnion);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +0000954
Fariborz Jahanian01dff422009-03-05 19:17:31 +0000955 /// GetIvarLayoutName - Returns a unique constant for the given
956 /// ivar layout bitmap.
957 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
958 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000959
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000960 /// EmitPropertyList - Emit the given property list. The return
961 /// value has type PropertyListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000962 llvm::Constant *EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000963 const Decl *Container,
Fariborz Jahanian066347e2009-01-28 22:18:42 +0000964 const ObjCContainerDecl *OCD,
965 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +0000966
Fariborz Jahanian751c1e72009-12-12 21:26:21 +0000967 /// PushProtocolProperties - Push protocol's property on the input stack.
968 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
969 std::vector<llvm::Constant*> &Properties,
970 const Decl *Container,
971 const ObjCProtocolDecl *PROTO,
972 const ObjCCommonTypesHelper &ObjCTypes);
973
Fariborz Jahanian56b3b772009-01-29 19:24:30 +0000974 /// GetProtocolRef - Return a reference to the internal protocol
975 /// description, creating an empty one if it has not been
976 /// defined. The return value has type ProtocolPtrTy.
977 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanian67726212009-03-08 20:18:37 +0000978
Daniel Dunbar30c65362009-03-09 20:09:19 +0000979 /// CreateMetadataVar - Create a global variable with internal
980 /// linkage for use by the Objective-C runtime.
981 ///
982 /// This is a convenience wrapper which not only creates the
983 /// variable, but also sets the section and alignment and adds the
Chris Lattnerf56501c2009-07-17 23:57:13 +0000984 /// global to the "llvm.used" list.
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000985 ///
986 /// \param Name - The variable name.
987 /// \param Init - The variable initializer; this is also used to
988 /// define the type of the variable.
989 /// \param Section - The section the variable should go into, or 0.
990 /// \param Align - The alignment for the variable, or 0.
991 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbar4527d302009-04-14 17:42:51 +0000992 /// "llvm.used".
Daniel Dunbar349e6fb2009-10-18 20:48:59 +0000993 llvm::GlobalVariable *CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +0000994 llvm::Constant *Init,
995 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +0000996 unsigned Align,
997 bool AddToUsed);
Daniel Dunbar30c65362009-03-09 20:09:19 +0000998
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +0000999 CodeGen::RValue EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001000 ReturnValueSlot Return,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001001 QualType ResultType,
1002 llvm::Value *Sel,
1003 llvm::Value *Arg0,
1004 QualType Arg0Ty,
1005 bool IsSuper,
1006 const CallArgList &CallArgs,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001007 const ObjCMethodDecl *OMD,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001008 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbarf5c18462009-04-20 06:54:31 +00001009
Daniel Dunbar5e639272010-04-25 20:39:01 +00001010 /// EmitImageInfo - Emit the image info marker used to encode some module
1011 /// level information.
1012 void EmitImageInfo();
1013
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001014public:
Owen Andersonae86c192009-07-13 04:10:07 +00001015 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump11289f42009-09-09 15:08:12 +00001016 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001017
David Chisnall481e3a82010-01-23 02:40:42 +00001018 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001019
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001020 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
1021 const ObjCContainerDecl *CD=0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001022
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001023 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001024
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001025 /// GetOrEmitProtocol - Get the protocol object for the given
1026 /// declaration, emitting it if necessary. The return value has type
1027 /// ProtocolPtrTy.
1028 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001029
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001030 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1031 /// object for the given declaration, emitting it if needed. These
1032 /// forward references will be filled in with empty bodies if no
1033 /// definition is seen. The return value has type ProtocolPtrTy.
1034 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001035};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001036
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001037class CGObjCMac : public CGObjCCommonMac {
1038private:
1039 ObjCTypesHelper ObjCTypes;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001040
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001041 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001042 /// information.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001043 void EmitModuleInfo();
1044
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001045 /// EmitModuleSymols - Emit module symbols, the list of defined
1046 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00001047 llvm::Constant *EmitModuleSymbols();
1048
Daniel Dunbar3ad53482008-08-11 21:35:06 +00001049 /// FinishModule - Write out global data structures at the end of
1050 /// processing a translation unit.
1051 void FinishModule();
Daniel Dunbarb036db82008-08-13 03:21:16 +00001052
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001053 /// EmitClassExtension - Generate the class extension structure used
1054 /// to store the weak ivar layout and properties. The return value
1055 /// has type ClassExtensionPtrTy.
1056 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
1057
1058 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1059 /// for the given class.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001060 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001061 const ObjCInterfaceDecl *ID);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001062
1063 /// EmitSuperClassRef - Emits reference to class's main metadata class.
1064 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001065
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001066 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001067 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001068 QualType ResultType,
1069 Selector Sel,
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001070 llvm::Value *Arg0,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001071 QualType Arg0Ty,
1072 bool IsSuper,
1073 const CallArgList &CallArgs);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001074
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001075 /// EmitIvarList - Emit the ivar list for the given
1076 /// implementation. If ForClass is true the list of class ivars
1077 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1078 /// interface ivars will be emitted. The return value has type
1079 /// IvarListPtrTy.
1080 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00001081 bool ForClass);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001082
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001083 /// EmitMetaClass - Emit a forward reference to the class structure
1084 /// for the metaclass of the given interface. The return value has
1085 /// type ClassPtrTy.
1086 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
1087
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001088 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001089 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001090 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
1091 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001092 const ConstantVector &Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001093
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001094 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001095
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001096 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001097
1098 /// EmitMethodList - Emit the method list for the given
Daniel Dunbar89654ee2008-08-26 08:29:31 +00001099 /// implementation. The return value has type MethodListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001100 llvm::Constant *EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00001101 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001102 const ConstantVector &Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001103
1104 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001105 /// method declarations.
Daniel Dunbarb036db82008-08-13 03:21:16 +00001106 /// - TypeName: The name for the type containing the methods.
1107 /// - IsProtocol: True iff these methods are for a protocol.
1108 /// - ClassMethds: True iff these are class methods.
1109 /// - Required: When true, only "required" methods are
1110 /// listed. Similarly, when false only "optional" methods are
1111 /// listed. For classes this should always be true.
1112 /// - begin, end: The method list to output.
1113 ///
1114 /// The return value has type MethodDescriptionListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001115 llvm::Constant *EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001116 const char *Section,
1117 const ConstantVector &Methods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001118
Daniel Dunbarc475d422008-10-29 22:36:39 +00001119 /// GetOrEmitProtocol - Get the protocol object for the given
1120 /// declaration, emitting it if necessary. The return value has type
1121 /// ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001122 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001123
1124 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1125 /// object for the given declaration, emitting it if needed. These
1126 /// forward references will be filled in with empty bodies if no
1127 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001128 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001129
Daniel Dunbarb036db82008-08-13 03:21:16 +00001130 /// EmitProtocolExtension - Generate the protocol extension
1131 /// structure used to store optional instance and class methods, and
1132 /// protocol properties. The return value has type
1133 /// ProtocolExtensionPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001134 llvm::Constant *
1135 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1136 const ConstantVector &OptInstanceMethods,
1137 const ConstantVector &OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001138
1139 /// EmitProtocolList - Generate the list of referenced
1140 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001141 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001142 ObjCProtocolDecl::protocol_iterator begin,
1143 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001144
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001145 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1146 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001147 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1148 bool lval=false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001149
1150public:
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001151 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001152
Fariborz Jahanian71394042009-01-23 23:53:38 +00001153 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001154
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001155 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001156 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001157 QualType ResultType,
1158 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001159 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001160 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001161 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001162 const ObjCMethodDecl *Method);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001163
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001164 virtual CodeGen::RValue
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001165 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001166 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001167 QualType ResultType,
1168 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001169 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001170 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001171 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001172 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001173 const CallArgList &CallArgs,
1174 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001175
Daniel Dunbarcb463852008-11-01 01:53:16 +00001176 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001177 const ObjCInterfaceDecl *ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001178
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001179 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1180 bool lval = false);
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001181
1182 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1183 /// untyped one.
1184 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1185 const ObjCMethodDecl *Method);
1186
John McCall2ca705e2010-07-24 00:37:23 +00001187 virtual llvm::Constant *GetEHType(QualType T);
1188
Daniel Dunbar92992502008-08-15 22:20:32 +00001189 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001190
Daniel Dunbar92992502008-08-15 22:20:32 +00001191 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001192
Daniel Dunbarcb463852008-11-01 01:53:16 +00001193 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001194 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001195
Chris Lattnerd4808922009-03-22 21:03:39 +00001196 virtual llvm::Constant *GetPropertyGetFunction();
1197 virtual llvm::Constant *GetPropertySetFunction();
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001198 virtual llvm::Constant *GetCopyStructFunction();
Chris Lattnerd4808922009-03-22 21:03:39 +00001199 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001200
John McCallbd309292010-07-06 01:34:17 +00001201 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1202 const ObjCAtTryStmt &S);
1203 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1204 const ObjCAtSynchronizedStmt &S);
1205 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson1963b0c2008-09-09 10:04:29 +00001206 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1207 const ObjCAtThrowStmt &S);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001208 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001209 llvm::Value *AddrWeakObj);
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00001210 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001211 llvm::Value *src, llvm::Value *dst);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001212 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001213 llvm::Value *src, llvm::Value *dest,
1214 bool threadlocal = false);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00001215 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001216 llvm::Value *src, llvm::Value *dest,
1217 llvm::Value *ivarOffset);
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00001218 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1219 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001220 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1221 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001222 llvm::Value *size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001223
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001224 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1225 QualType ObjectTy,
1226 llvm::Value *BaseValue,
1227 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001228 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001229 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001230 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001231 const ObjCIvarDecl *Ivar);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001232};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001233
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001234class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001235private:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001236 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanian71394042009-01-23 23:53:38 +00001237 llvm::GlobalVariable* ObjCEmptyCacheVar;
1238 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001239
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001240 /// SuperClassReferences - uniqued super class references.
1241 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001242
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001243 /// MetaClassReferences - uniqued meta class references.
1244 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001245
1246 /// EHTypeReferences - uniqued class ehtype references.
1247 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001248
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001249 /// NonLegacyDispatchMethods - List of methods for which we do *not* generate
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001250 /// legacy messaging dispatch.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001251 llvm::DenseSet<Selector> NonLegacyDispatchMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001252
Fariborz Jahanian67260552009-11-17 21:37:35 +00001253 /// DefinedMetaClasses - List of defined meta-classes.
1254 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1255
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001256 /// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001257 /// NonLegacyDispatchMethods; false otherwise.
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001258 bool LegacyDispatchedSelector(Selector Sel);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001259
Fariborz Jahanian71394042009-01-23 23:53:38 +00001260 /// FinishNonFragileABIModule - Write out global data structures at the end of
1261 /// processing a translation unit.
1262 void FinishNonFragileABIModule();
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001263
Daniel Dunbar19573e72009-05-15 21:48:48 +00001264 /// AddModuleClassList - Add the given list of class pointers to the
1265 /// module with the provided symbol and section names.
1266 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1267 const char *SymbolName,
1268 const char *SectionName);
1269
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001270 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1271 unsigned InstanceStart,
1272 unsigned InstanceSize,
1273 const ObjCImplementationDecl *ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001274 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001275 llvm::Constant *IsAGV,
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00001276 llvm::Constant *SuperClassGV,
Fariborz Jahanian82208252009-01-31 00:59:10 +00001277 llvm::Constant *ClassRoGV,
1278 bool HiddenVisibility);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001279
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001280 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001281
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00001282 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001283
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001284 /// EmitMethodList - Emit the method list for the given
1285 /// implementation. The return value has type MethodListnfABITy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001286 llvm::Constant *EmitMethodList(llvm::Twine Name,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00001287 const char *Section,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00001288 const ConstantVector &Methods);
1289 /// EmitIvarList - Emit the ivar list for the given
1290 /// implementation. If ForClass is true the list of class ivars
1291 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1292 /// interface ivars will be emitted. The return value has type
1293 /// IvarListnfABIPtrTy.
1294 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001295
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001296 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00001297 const ObjCIvarDecl *Ivar,
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00001298 unsigned long int offset);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001299
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001300 /// GetOrEmitProtocol - Get the protocol object for the given
1301 /// declaration, emitting it if necessary. The return value has type
1302 /// ProtocolPtrTy.
1303 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001304
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001305 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1306 /// object for the given declaration, emitting it if needed. These
1307 /// forward references will be filled in with empty bodies if no
1308 /// definition is seen. The return value has type ProtocolPtrTy.
1309 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001310
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001311 /// EmitProtocolList - Generate the list of referenced
1312 /// protocols. The return value has type ProtocolListPtrTy.
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001313 llvm::Constant *EmitProtocolList(llvm::Twine Name,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001314 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001315 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001316
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001317 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001318 ReturnValueSlot Return,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001319 QualType ResultType,
1320 Selector Sel,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00001321 llvm::Value *Receiver,
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00001322 QualType Arg0Ty,
1323 bool IsSuper,
1324 const CallArgList &CallArgs);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00001325
1326 /// GetClassGlobal - Return the global variable for the Objective-C
1327 /// class of the given name.
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00001328 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001329
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001330 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001331 /// for the given class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001332 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001333 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001334
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00001335 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1336 /// for the given super class reference.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001337 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1338 const ObjCInterfaceDecl *ID);
1339
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001340 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1341 /// meta-data
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001342 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00001343 const ObjCInterfaceDecl *ID);
1344
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00001345 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1346 /// the given ivar.
1347 ///
Daniel Dunbara1060522009-04-19 00:31:15 +00001348 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001349 const ObjCInterfaceDecl *ID,
1350 const ObjCIvarDecl *Ivar);
1351
Fariborz Jahanian74b77222009-02-11 20:51:17 +00001352 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1353 /// for the given selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001354 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1355 bool lval=false);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001356
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001357 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbarb1559a42009-03-01 04:46:24 +00001358 /// interface. The return value has type EHTypePtrTy.
John McCall2ca705e2010-07-24 00:37:23 +00001359 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001360 bool ForDefinition);
Daniel Dunbar15894b72009-04-07 05:48:37 +00001361
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001362 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar15894b72009-04-07 05:48:37 +00001363 return "OBJC_METACLASS_$_";
1364 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001365
Daniel Dunbar15894b72009-04-07 05:48:37 +00001366 const char *getClassSymbolPrefix() const {
1367 return "OBJC_CLASS_$_";
1368 }
1369
Daniel Dunbar961202372009-05-03 12:57:56 +00001370 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00001371 uint32_t &InstanceStart,
1372 uint32_t &InstanceSize);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001373
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001374 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001375 Selector GetNullarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001376 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1377 return CGM.getContext().Selectors.getSelector(0, &II);
1378 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001379
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001380 Selector GetUnarySelector(const char* name) const {
Fariborz Jahaniane4128642009-05-12 20:06:41 +00001381 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1382 return CGM.getContext().Selectors.getSelector(1, &II);
1383 }
Daniel Dunbar554fd792009-04-19 23:41:48 +00001384
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001385 /// ImplementationIsNonLazy - Check whether the given category or
1386 /// class implementation is "non-lazy".
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00001387 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00001388
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001389public:
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00001390 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001391 // FIXME. All stubs for now!
1392 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001393
Fariborz Jahanian71394042009-01-23 23:53:38 +00001394 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001395 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001396 QualType ResultType,
1397 Selector Sel,
1398 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001399 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001400 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001401 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001402
1403 virtual CodeGen::RValue
Fariborz Jahanian71394042009-01-23 23:53:38 +00001404 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001405 ReturnValueSlot Return,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001406 QualType ResultType,
1407 Selector Sel,
1408 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001409 bool isCategoryImpl,
Fariborz Jahanian71394042009-01-23 23:53:38 +00001410 llvm::Value *Receiver,
1411 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001412 const CallArgList &CallArgs,
1413 const ObjCMethodDecl *Method);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001414
Fariborz Jahanian71394042009-01-23 23:53:38 +00001415 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00001416 const ObjCInterfaceDecl *ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001417
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001418 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1419 bool lvalue = false)
1420 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001421
1422 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1423 /// untyped one.
1424 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1425 const ObjCMethodDecl *Method)
1426 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001427
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00001428 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001429
Fariborz Jahanian71394042009-01-23 23:53:38 +00001430 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001431 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian097feda2009-01-30 18:58:59 +00001432 const ObjCProtocolDecl *PD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001433
John McCall2ca705e2010-07-24 00:37:23 +00001434 virtual llvm::Constant *GetEHType(QualType T);
1435
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001436 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001437 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001438 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001439 virtual llvm::Constant *GetPropertySetFunction() {
1440 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001441 }
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00001442
1443 virtual llvm::Constant *GetCopyStructFunction() {
1444 return ObjCTypes.getCopyStructFn();
1445 }
1446
Chris Lattnerd4808922009-03-22 21:03:39 +00001447 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00001448 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbard73ea8162009-02-16 18:48:45 +00001449 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001450
John McCallbd309292010-07-06 01:34:17 +00001451 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1452 const ObjCAtTryStmt &S);
1453 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1454 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001455 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlsson9ab53d12009-02-16 22:59:18 +00001456 const ObjCAtThrowStmt &S);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001457 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001458 llvm::Value *AddrWeakObj);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001459 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001460 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001461 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00001462 llvm::Value *src, llvm::Value *dest,
1463 bool threadlocal = false);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001464 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00001465 llvm::Value *src, llvm::Value *dest,
1466 llvm::Value *ivarOffset);
Fariborz Jahanian71394042009-01-23 23:53:38 +00001467 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian06292952009-02-16 22:52:32 +00001468 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00001469 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1470 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00001471 llvm::Value *size);
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001472 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1473 QualType ObjectTy,
1474 llvm::Value *BaseValue,
1475 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00001476 unsigned CVRQualifiers);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001477 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00001478 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00001479 const ObjCIvarDecl *Ivar);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001480};
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001481
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001482} // end anonymous namespace
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001483
1484/* *** Helper Functions *** */
1485
1486/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Anderson170229f2009-07-14 23:10:40 +00001487static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001488 llvm::Constant *C,
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001489 unsigned idx0,
1490 unsigned idx1) {
1491 llvm::Value *Idxs[] = {
Owen Anderson41a75022009-08-13 21:57:51 +00001492 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1493 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001494 };
Owen Andersonade90fd2009-07-29 18:54:39 +00001495 return llvm::ConstantExpr::getGetElementPtr(C, Idxs, 2);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001496}
1497
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001498/// hasObjCExceptionAttribute - Return true if this class or any super
1499/// class has the __objc_exception__ attribute.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001500static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001501 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001502 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001503 return true;
1504 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor78bd61f2009-06-18 16:11:24 +00001505 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00001506 return false;
1507}
1508
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001509/* *** CGObjCMac Public Interface *** */
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001510
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001511CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00001512 ObjCTypes(cgm) {
Fariborz Jahanian279eda62009-01-21 22:04:16 +00001513 ObjCABI = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001514 EmitImageInfo();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001515}
1516
Daniel Dunbar7c6d3a72008-08-16 00:25:02 +00001517/// GetClass - Return a reference to the class for the given interface
1518/// decl.
Daniel Dunbarcb463852008-11-01 01:53:16 +00001519llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001520 const ObjCInterfaceDecl *ID) {
1521 return EmitClassRef(Builder, ID);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001522}
1523
1524/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00001525llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1526 bool lval) {
1527 return EmitSelector(Builder, Sel, lval);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001528}
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001529llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001530 *Method) {
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001531 return EmitSelector(Builder, Method->getSelector());
1532}
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001533
John McCall2ca705e2010-07-24 00:37:23 +00001534llvm::Constant *CGObjCMac::GetEHType(QualType T) {
1535 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1536 return 0;
1537}
1538
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001539/// Generate a constant CFString object.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001540/*
1541 struct __builtin_CFString {
1542 const int *isa; // point to __CFConstantStringClassReference
1543 int flags;
1544 const char *str;
1545 long length;
1546 };
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00001547*/
1548
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001549/// or Generate a constant NSString object.
1550/*
1551 struct __builtin_NSString {
1552 const int *isa; // point to __NSConstantStringClassReference
1553 const char *str;
1554 unsigned int length;
1555 };
1556*/
1557
Fariborz Jahanian71394042009-01-23 23:53:38 +00001558llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall481e3a82010-01-23 02:40:42 +00001559 const StringLiteral *SL) {
Fariborz Jahanian63408e82010-04-22 20:26:39 +00001560 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1561 CGM.GetAddrOfConstantCFString(SL) :
1562 CGM.GetAddrOfConstantNSString(SL));
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001563}
1564
1565/// Generates a message send where the super is the receiver. This is
1566/// a message send to self with special delivery semantics indicating
1567/// which class's method should be called.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001568CodeGen::RValue
1569CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001570 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001571 QualType ResultType,
1572 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001573 const ObjCInterfaceDecl *Class,
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001574 bool isCategoryImpl,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001575 llvm::Value *Receiver,
Daniel Dunbarc722b852008-08-30 03:02:31 +00001576 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001577 const CodeGen::CallArgList &CallArgs,
1578 const ObjCMethodDecl *Method) {
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001579 // Create and init a super structure; this is a (receiver, class)
1580 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001581 llvm::Value *ObjCSuper =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001582 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001583 llvm::Value *ReceiverAsObject =
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001584 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001585 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001586 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbarf6397fe2008-08-23 04:28:29 +00001587
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001588 // If this is a class message the metaclass is passed as the target.
1589 llvm::Value *Target;
1590 if (IsClassMessage) {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001591 if (isCategoryImpl) {
1592 // Message sent to 'super' in a class method defined in a category
1593 // implementation requires an odd treatment.
1594 // If we are in a class method, we must retrieve the
1595 // _metaclass_ for the current class, pointed at by
1596 // the class's "isa" pointer. The following assumes that
1597 // isa" is the first ivar in a class (which it must be).
1598 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1599 Target = CGF.Builder.CreateStructGEP(Target, 0);
1600 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00001601 } else {
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00001602 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1603 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1604 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1605 Target = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001606 }
Fariborz Jahanianda2efb02009-11-14 02:18:31 +00001607 }
1608 else if (isCategoryImpl)
1609 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1610 else {
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00001611 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1612 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1613 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001614 }
Mike Stump18bb9282009-05-16 07:57:57 +00001615 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1616 // ObjCTypes types.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001617 const llvm::Type *ClassTy =
Daniel Dunbarc722b852008-08-30 03:02:31 +00001618 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbarc475d422008-10-29 22:36:39 +00001619 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001620 CGF.Builder.CreateStore(Target,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001621 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall78a15112010-05-22 01:48:05 +00001622 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001623 EmitSelector(CGF.Builder, Sel),
1624 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001625 true, CallArgs, Method, ObjCTypes);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001626}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001627
1628/// Generate code for a message send expression.
Daniel Dunbar97db84c2008-08-23 03:46:30 +00001629CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001630 ReturnValueSlot Return,
Daniel Dunbar4b8c6db2008-08-30 05:35:15 +00001631 QualType ResultType,
1632 Selector Sel,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00001633 llvm::Value *Receiver,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001634 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00001635 const ObjCInterfaceDecl *Class,
Fariborz Jahanianf3648b82009-05-05 21:36:57 +00001636 const ObjCMethodDecl *Method) {
John McCall78a15112010-05-22 01:48:05 +00001637 return EmitLegacyMessageSend(CGF, Return, ResultType,
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001638 EmitSelector(CGF.Builder, Sel),
1639 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001640 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00001641}
1642
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001643CodeGen::RValue
1644CGObjCCommonMac::EmitLegacyMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00001645 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00001646 QualType ResultType,
1647 llvm::Value *Sel,
1648 llvm::Value *Arg0,
1649 QualType Arg0Ty,
1650 bool IsSuper,
1651 const CallArgList &CallArgs,
1652 const ObjCMethodDecl *Method,
1653 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbarc722b852008-08-30 03:02:31 +00001654 CallArgList ActualArgs;
Fariborz Jahanian969bc682009-04-24 21:07:43 +00001655 if (!IsSuper)
1656 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar41cf9de2008-09-09 01:06:48 +00001657 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001658 ActualArgs.push_back(std::make_pair(RValue::get(Sel),
Daniel Dunbarc722b852008-08-30 03:02:31 +00001659 CGF.getContext().getObjCSelType()));
1660 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001661
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00001662 CodeGenTypes &Types = CGM.getTypes();
John McCallab26cfa2010-02-05 21:31:56 +00001663 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00001664 FunctionType::ExtInfo());
Daniel Dunbardf0e62d2009-09-17 04:01:40 +00001665 const llvm::FunctionType *FTy =
1666 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001667
Anders Carlsson280e61f12010-06-21 20:59:55 +00001668 if (Method)
1669 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1670 CGM.getContext().getCanonicalType(ResultType) &&
1671 "Result type mismatch!");
1672
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001673 llvm::Constant *Fn = NULL;
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001674 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001675 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001676 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001677 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1678 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1679 : ObjCTypes.getSendFpretFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001680 } else {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00001681 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001682 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar3c683f5b2008-10-17 03:24:53 +00001683 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00001684 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00001685 return CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001686}
1687
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001688llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar89da6ad2008-08-13 00:59:25 +00001689 const ObjCProtocolDecl *PD) {
Daniel Dunbar7050c552008-09-04 04:33:15 +00001690 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001691 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar7050c552008-09-04 04:33:15 +00001692 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1693
Owen Andersonade90fd2009-07-29 18:54:39 +00001694 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001695 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00001696}
1697
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001698void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stump18bb9282009-05-16 07:57:57 +00001699 // FIXME: We shouldn't need this, the protocol decl should contain enough
1700 // information to tell us whether this was a declaration or a definition.
Daniel Dunbarc475d422008-10-29 22:36:39 +00001701 DefinedProtocols.insert(PD->getIdentifier());
1702
1703 // If we have generated a forward reference to this protocol, emit
1704 // it now. Otherwise do nothing, the protocol objects are lazily
1705 // emitted.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001706 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbarc475d422008-10-29 22:36:39 +00001707 GetOrEmitProtocol(PD);
1708}
1709
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00001710llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001711 if (DefinedProtocols.count(PD->getIdentifier()))
1712 return GetOrEmitProtocol(PD);
1713 return GetOrEmitProtocolRef(PD);
1714}
1715
Daniel Dunbarb036db82008-08-13 03:21:16 +00001716/*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001717// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1718struct _objc_protocol {
1719struct _objc_protocol_extension *isa;
1720char *protocol_name;
1721struct _objc_protocol_list *protocol_list;
1722struct _objc__method_prototype_list *instance_methods;
1723struct _objc__method_prototype_list *class_methods
1724};
Daniel Dunbarb036db82008-08-13 03:21:16 +00001725
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001726See EmitProtocolExtension().
Daniel Dunbarb036db82008-08-13 03:21:16 +00001727*/
Daniel Dunbarc475d422008-10-29 22:36:39 +00001728llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1729 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1730
1731 // Early exit if a defining object has already been generated.
1732 if (Entry && Entry->hasInitializer())
1733 return Entry;
1734
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001735 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stump18bb9282009-05-16 07:57:57 +00001736 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00001737 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1738
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001739 // Construct method lists.
1740 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1741 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001742 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001743 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001744 ObjCMethodDecl *MD = *i;
1745 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1746 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1747 OptInstanceMethods.push_back(C);
1748 } else {
1749 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001750 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001751 }
1752
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001753 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001754 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001755 ObjCMethodDecl *MD = *i;
1756 llvm::Constant *C = GetMethodDescriptionConstant(MD);
1757 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1758 OptClassMethods.push_back(C);
1759 } else {
1760 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001761 }
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001762 }
1763
Daniel Dunbarb036db82008-08-13 03:21:16 +00001764 std::vector<llvm::Constant*> Values(5);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001765 Values[0] = EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001766 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001767 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001768 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00001769 PD->protocol_begin(),
1770 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001771 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001772 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001773 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1774 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001775 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001776 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001777 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1778 ClassMethods);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001779 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001780 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001781
Daniel Dunbarb036db82008-08-13 03:21:16 +00001782 if (Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001783 // Already created, fix the linkage and update the initializer.
1784 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001785 Entry->setInitializer(Init);
1786 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001787 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001788 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarb036db82008-08-13 03:21:16 +00001789 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001790 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001791 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001792 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001793 // FIXME: Is this necessary? Why only for protocol?
1794 Entry->setAlignment(4);
1795 }
Chris Lattnerf56501c2009-07-17 23:57:13 +00001796 CGM.AddUsedGlobal(Entry);
Daniel Dunbarc475d422008-10-29 22:36:39 +00001797
1798 return Entry;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001799}
1800
Daniel Dunbarc475d422008-10-29 22:36:39 +00001801llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001802 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1803
1804 if (!Entry) {
Daniel Dunbarc475d422008-10-29 22:36:39 +00001805 // We use the initializer as a marker of whether this is a forward
1806 // reference or not. At module finalization we add the empty
1807 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001808 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00001809 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbarc475d422008-10-29 22:36:39 +00001810 llvm::GlobalValue::ExternalLinkage,
1811 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001812 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbarb036db82008-08-13 03:21:16 +00001813 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbarb036db82008-08-13 03:21:16 +00001814 // FIXME: Is this necessary? Why only for protocol?
1815 Entry->setAlignment(4);
1816 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001817
Daniel Dunbarb036db82008-08-13 03:21:16 +00001818 return Entry;
1819}
1820
1821/*
1822 struct _objc_protocol_extension {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001823 uint32_t size;
1824 struct objc_method_description_list *optional_instance_methods;
1825 struct objc_method_description_list *optional_class_methods;
1826 struct objc_property_list *instance_properties;
Daniel Dunbarb036db82008-08-13 03:21:16 +00001827 };
1828*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001829llvm::Constant *
1830CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1831 const ConstantVector &OptInstanceMethods,
1832 const ConstantVector &OptClassMethods) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001833 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001834 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001835 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001836 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001837 Values[1] =
1838 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001839 + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001840 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
1841 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001842 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001843 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001844 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
1845 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001846 Values[3] = EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00001847 0, PD, ObjCTypes);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001848
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001849 // Return null if no extension bits are used.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001850 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Daniel Dunbarb036db82008-08-13 03:21:16 +00001851 Values[3]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00001852 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001853
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001854 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00001855 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001856
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001857 // No special section, but goes in llvm.used
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001858 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001859 Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001860 0, 0, true);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001861}
1862
1863/*
1864 struct objc_protocol_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001865 struct objc_protocol_list *next;
1866 long count;
1867 Protocol *list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001868 };
1869*/
Daniel Dunbardec75f82008-08-21 21:57:41 +00001870llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001871CGObjCMac::EmitProtocolList(llvm::Twine Name,
Daniel Dunbardec75f82008-08-21 21:57:41 +00001872 ObjCProtocolDecl::protocol_iterator begin,
1873 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00001874 std::vector<llvm::Constant*> ProtocolRefs;
1875
Daniel Dunbardec75f82008-08-21 21:57:41 +00001876 for (; begin != end; ++begin)
1877 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001878
1879 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001880 if (ProtocolRefs.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001881 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001882
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001883 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00001884 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbarb036db82008-08-13 03:21:16 +00001885
1886 std::vector<llvm::Constant*> Values(3);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00001887 // This field is only used by the runtime.
Owen Anderson0b75f232009-07-31 20:28:54 +00001888 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001889 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001890 ProtocolRefs.size() - 1);
1891 Values[2] =
1892 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1893 ProtocolRefs.size()),
Daniel Dunbarb036db82008-08-13 03:21:16 +00001894 ProtocolRefs);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001895
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001896 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001897 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00001898 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00001899 4, false);
Owen Andersonade90fd2009-07-29 18:54:39 +00001900 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00001901}
1902
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001903void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1904 std::vector<llvm::Constant*> &Properties,
1905 const Decl *Container,
1906 const ObjCProtocolDecl *PROTO,
1907 const ObjCCommonTypesHelper &ObjCTypes) {
1908 std::vector<llvm::Constant*> Prop(2);
1909 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1910 E = PROTO->protocol_end(); P != E; ++P)
1911 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1912 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1913 E = PROTO->prop_end(); I != E; ++I) {
1914 const ObjCPropertyDecl *PD = *I;
1915 if (!PropertySet.insert(PD->getIdentifier()))
1916 continue;
1917 Prop[0] = GetPropertyName(PD->getIdentifier());
1918 Prop[1] = GetPropertyTypeString(PD, Container);
1919 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1920 }
1921}
1922
Daniel Dunbarb036db82008-08-13 03:21:16 +00001923/*
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001924 struct _objc_property {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001925 const char * const name;
1926 const char * const attributes;
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001927 };
1928
1929 struct _objc_property_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001930 uint32_t entsize; // sizeof (struct _objc_property)
1931 uint32_t prop_count;
1932 struct _objc_property[prop_count];
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001933 };
1934*/
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00001935llvm::Constant *CGObjCCommonMac::EmitPropertyList(llvm::Twine Name,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001936 const Decl *Container,
1937 const ObjCContainerDecl *OCD,
1938 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001939 std::vector<llvm::Constant*> Properties, Prop(2);
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001940 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001941 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
1942 E = OCD->prop_end(); I != E; ++I) {
Steve Naroffba3dc382009-01-11 12:47:58 +00001943 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001944 PropertySet.insert(PD->getIdentifier());
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001945 Prop[0] = GetPropertyName(PD->getIdentifier());
Daniel Dunbar4932b362008-08-28 04:38:10 +00001946 Prop[1] = GetPropertyTypeString(PD, Container);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001947 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001948 Prop));
1949 }
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001950 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Fariborz Jahanian751c1e72009-12-12 21:26:21 +00001951 for (ObjCInterfaceDecl::protocol_iterator P = OID->protocol_begin(),
1952 E = OID->protocol_end(); P != E; ++P)
Fariborz Jahanian7966aff2010-06-22 16:33:55 +00001953 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1954 ObjCTypes);
1955 }
1956 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
1957 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
1958 E = CD->protocol_end(); P != E; ++P)
1959 PushProtocolProperties(PropertySet, Properties, Container, (*P),
1960 ObjCTypes);
1961 }
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001962
1963 // Return null for empty list.
1964 if (Properties.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00001965 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001966
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001967 unsigned PropertySize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00001968 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001969 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00001970 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
1971 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001972 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001973 Properties.size());
Owen Anderson47034e12009-07-28 18:33:04 +00001974 Values[2] = llvm::ConstantArray::get(AT, Properties);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00001975 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001976
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001977 llvm::GlobalVariable *GV =
1978 CreateMetadataVar(Name, Init,
1979 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001980 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001981 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbarb25452a2009-04-15 02:56:18 +00001982 true);
Owen Andersonade90fd2009-07-29 18:54:39 +00001983 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00001984}
1985
1986/*
Daniel Dunbarb036db82008-08-13 03:21:16 +00001987 struct objc_method_description_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001988 int count;
1989 struct objc_method_description list[];
Daniel Dunbarb036db82008-08-13 03:21:16 +00001990 };
1991*/
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001992llvm::Constant *
1993CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
1994 std::vector<llvm::Constant*> Desc(2);
Owen Anderson170229f2009-07-14 23:10:40 +00001995 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00001996 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
1997 ObjCTypes.SelectorPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00001998 Desc[1] = GetMethodVarType(MD);
Owen Anderson0e0189d2009-07-27 22:29:56 +00001999 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002000 Desc);
2001}
Daniel Dunbarb036db82008-08-13 03:21:16 +00002002
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002003llvm::Constant *CGObjCMac::EmitMethodDescList(llvm::Twine Name,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002004 const char *Section,
2005 const ConstantVector &Methods) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00002006 // Return null for empty list.
2007 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002008 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002009
2010 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002011 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002012 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002013 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002014 Values[1] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002015 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbarb036db82008-08-13 03:21:16 +00002016
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002017 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002018 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbarb036db82008-08-13 03:21:16 +00002019 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002020}
2021
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002022/*
2023 struct _objc_category {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002024 char *category_name;
2025 char *class_name;
2026 struct _objc_method_list *instance_methods;
2027 struct _objc_method_list *class_methods;
2028 struct _objc_protocol_list *protocols;
2029 uint32_t size; // <rdar://4585769>
2030 struct _objc_property_list *instance_properties;
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002031 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002032*/
Daniel Dunbar92992502008-08-15 22:20:32 +00002033void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002034 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002035
Mike Stump18bb9282009-05-16 07:57:57 +00002036 // FIXME: This is poor design, the OCD should have a pointer to the category
2037 // decl. Additionally, note that Category can be null for the @implementation
2038 // w/o an @interface case. Sema should just create one for us as it does for
2039 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002040 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002041 const ObjCCategoryDecl *Category =
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002042 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002043
2044 llvm::SmallString<256> ExtName;
2045 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2046 << OCD->getName();
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002047
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002048 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002049 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002050 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002051 // Instance methods should always be defined.
2052 InstanceMethods.push_back(GetMethodConstant(*i));
2053 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002054 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002055 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002056 // Class methods should always be defined.
2057 ClassMethods.push_back(GetMethodConstant(*i));
2058 }
2059
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002060 std::vector<llvm::Constant*> Values(7);
2061 Values[0] = GetClassName(OCD->getIdentifier());
2062 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahaniane55f8662009-04-29 20:40:05 +00002063 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002064 Values[2] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002065 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002066 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002067 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002068 Values[3] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002069 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002070 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002071 ClassMethods);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002072 if (Category) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002073 Values[4] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002074 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002075 Category->protocol_begin(),
2076 Category->protocol_end());
2077 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002078 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002079 }
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002080 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002081
2082 // If there is no category @interface then there can be no properties.
2083 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002084 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002085 OCD, Category, ObjCTypes);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002086 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002087 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar28e76ca2008-08-26 23:03:11 +00002088 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002089
Owen Anderson0e0189d2009-07-27 22:29:56 +00002090 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002091 Values);
2092
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002093 llvm::GlobalVariable *GV =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002094 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002095 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002096 4, true);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002097 DefinedCategories.push_back(GV);
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00002098 DefinedCategoryNames.insert(ExtName.str());
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002099}
2100
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002101// FIXME: Get from somewhere?
2102enum ClassFlags {
2103 eClassFlags_Factory = 0x00001,
2104 eClassFlags_Meta = 0x00002,
2105 // <rdr://5142207>
2106 eClassFlags_HasCXXStructors = 0x02000,
2107 eClassFlags_Hidden = 0x20000,
2108 eClassFlags_ABI2_Hidden = 0x00010,
2109 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2110};
2111
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002112/*
2113 struct _objc_class {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002114 Class isa;
2115 Class super_class;
2116 const char *name;
2117 long version;
2118 long info;
2119 long instance_size;
2120 struct _objc_ivar_list *ivars;
2121 struct _objc_method_list *methods;
2122 struct _objc_cache *cache;
2123 struct _objc_protocol_list *protocols;
2124 // Objective-C 1.0 extensions (<rdr://4585769>)
2125 const char *ivar_layout;
2126 struct _objc_class_ext *ext;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002127 };
2128
2129 See EmitClassExtension();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002130*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002131void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002132 DefinedSymbols.insert(ID->getIdentifier());
2133
Chris Lattner86d7d912008-11-24 03:54:41 +00002134 std::string ClassName = ID->getNameAsString();
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002135 // FIXME: Gross
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002136 ObjCInterfaceDecl *Interface =
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002137 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002138 llvm::Constant *Protocols =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002139 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Daniel Dunbardec75f82008-08-21 21:57:41 +00002140 Interface->protocol_begin(),
2141 Interface->protocol_end());
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002142 unsigned Flags = eClassFlags_Factory;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00002143 if (ID->getNumIvarInitializers())
2144 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002145 unsigned Size =
Daniel Dunbar12119b92009-05-03 10:46:44 +00002146 CGM.getContext().getASTObjCImplementationLayout(ID).getSize() / 8;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002147
2148 // FIXME: Set CXX-structors flag.
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002149 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002150 Flags |= eClassFlags_Hidden;
2151
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002152 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002153 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002154 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002155 // Instance methods should always be defined.
2156 InstanceMethods.push_back(GetMethodConstant(*i));
2157 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002158 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002159 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002160 // Class methods should always be defined.
2161 ClassMethods.push_back(GetMethodConstant(*i));
2162 }
2163
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002164 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00002165 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002166 ObjCPropertyImplDecl *PID = *i;
2167
2168 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2169 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2170
2171 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2172 if (llvm::Constant *C = GetMethodConstant(MD))
2173 InstanceMethods.push_back(C);
2174 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2175 if (llvm::Constant *C = GetMethodConstant(MD))
2176 InstanceMethods.push_back(C);
2177 }
2178 }
2179
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002180 std::vector<llvm::Constant*> Values(12);
Daniel Dunbarccf61832009-05-03 08:56:52 +00002181 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002182 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00002183 // Record a reference to the super class.
2184 LazySymbols.insert(Super->getIdentifier());
2185
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002186 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002187 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002188 ObjCTypes.ClassPtrTy);
2189 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002190 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002191 }
2192 Values[ 2] = GetClassName(ID->getIdentifier());
2193 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002194 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2195 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2196 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002197 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002198 Values[ 7] =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002199 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbar80a840b2008-08-23 00:19:03 +00002200 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002201 InstanceMethods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002202 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002203 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002204 Values[ 9] = Protocols;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002205 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002206 Values[11] = EmitClassExtension(ID);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002207 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002208 Values);
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002209 std::string Name("\01L_OBJC_CLASS_");
2210 Name += ClassName;
2211 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2212 // Check for a forward reference.
2213 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2214 if (GV) {
2215 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2216 "Forward metaclass reference has incorrect type.");
2217 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2218 GV->setInitializer(Init);
2219 GV->setSection(Section);
2220 GV->setAlignment(4);
2221 CGM.AddUsedGlobal(GV);
2222 }
2223 else
2224 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002225 DefinedClasses.push_back(GV);
2226}
2227
2228llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2229 llvm::Constant *Protocols,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002230 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002231 unsigned Flags = eClassFlags_Meta;
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002232 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002233
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00002234 if (CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden)
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002235 Flags |= eClassFlags_Hidden;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002236
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002237 std::vector<llvm::Constant*> Values(12);
2238 // The isa for the metaclass is the root of the hierarchy.
2239 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2240 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2241 Root = Super;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002242 Values[ 0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002243 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002244 ObjCTypes.ClassPtrTy);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002245 // The super class for the metaclass is emitted as the name of the
2246 // super class. The runtime fixes this up to point to the
2247 // *metaclass* for the super class.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002248 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002249 Values[ 1] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002250 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002251 ObjCTypes.ClassPtrTy);
2252 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00002253 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002254 }
2255 Values[ 2] = GetClassName(ID->getIdentifier());
2256 // Version is always 0.
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002257 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2258 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2259 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002260 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002261 Values[ 7] =
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002262 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002263 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002264 Methods);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002265 // cache is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002266 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002267 Values[ 9] = Protocols;
2268 // ivar_layout for metaclass is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00002269 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002270 // The class extension is always unused for metaclasses.
Owen Anderson0b75f232009-07-31 20:28:54 +00002271 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002272 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002273 Values);
2274
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002275 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner86d7d912008-11-24 03:54:41 +00002276 Name += ID->getNameAsCString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002277
2278 // Check for a forward reference.
2279 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2280 if (GV) {
2281 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2282 "Forward metaclass reference has incorrect type.");
2283 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2284 GV->setInitializer(Init);
2285 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00002286 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002287 llvm::GlobalValue::InternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002288 Init, Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002289 }
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002290 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbarae333842009-03-09 22:18:41 +00002291 GV->setAlignment(4);
Chris Lattnerf56501c2009-07-17 23:57:13 +00002292 CGM.AddUsedGlobal(GV);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002293
2294 return GV;
2295}
2296
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002297llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerf3d3fae2008-11-24 05:29:24 +00002298 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002299
Mike Stump18bb9282009-05-16 07:57:57 +00002300 // FIXME: Should we look these up somewhere other than the module. Its a bit
2301 // silly since we only generate these while processing an implementation, so
2302 // exactly one pointer would work if know when we entered/exitted an
2303 // implementation block.
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002304
2305 // Check for an existing forward reference.
Fariborz Jahanian475831b2009-01-07 20:11:22 +00002306 // Previously, metaclass with internal linkage may have been defined.
2307 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002308 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2309 true)) {
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002310 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2311 "Forward metaclass reference has incorrect type.");
2312 return GV;
2313 } else {
2314 // Generate as an external reference to keep a consistent
2315 // module. This will be patched up when we emit the metaclass.
Owen Andersonc10c8d32009-07-08 19:05:04 +00002316 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002317 llvm::GlobalValue::ExternalLinkage,
2318 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00002319 Name);
Daniel Dunbarca8531a2008-08-25 08:19:24 +00002320 }
2321}
2322
Fariborz Jahanianeb80c982009-11-12 20:14:24 +00002323llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2324 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2325
2326 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2327 true)) {
2328 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2329 "Forward class metadata reference has incorrect type.");
2330 return GV;
2331 } else {
2332 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2333 llvm::GlobalValue::ExternalLinkage,
2334 0,
2335 Name);
2336 }
2337}
2338
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002339/*
2340 struct objc_class_ext {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002341 uint32_t size;
2342 const char *weak_ivar_layout;
2343 struct _objc_property_list *properties;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002344 };
2345*/
2346llvm::Constant *
2347CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002348 uint64_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00002349 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002350
2351 std::vector<llvm::Constant*> Values(3);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002352 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian6df69862009-04-22 23:00:43 +00002353 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002354 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian066347e2009-01-28 22:18:42 +00002355 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002356
2357 // Return null if no extension bits are used.
2358 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Anderson0b75f232009-07-31 20:28:54 +00002359 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002360
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002361 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00002362 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002363 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002364 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002365 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002366}
2367
2368/*
2369 struct objc_ivar {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002370 char *ivar_name;
2371 char *ivar_type;
2372 int ivar_offset;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002373 };
2374
2375 struct objc_ivar_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002376 int ivar_count;
2377 struct objc_ivar list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002378 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002379*/
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002380llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002381 bool ForClass) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002382 std::vector<llvm::Constant*> Ivars, Ivar(3);
2383
2384 // When emitting the root class GCC emits ivar entries for the
2385 // actual class structure. It is not clear if we need to follow this
2386 // behavior; for now lets try and get away with not doing it. If so,
2387 // the cleanest solution would be to make up an ObjCInterfaceDecl
2388 // for the class.
2389 if (ForClass)
Owen Anderson0b75f232009-07-31 20:28:54 +00002390 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002391
2392 ObjCInterfaceDecl *OID =
Fariborz Jahanianb042a592009-01-28 19:12:34 +00002393 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002394
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002395 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002396 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002397
Daniel Dunbarf5c18462009-04-20 06:54:31 +00002398 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
2399 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00002400 // Ignore unnamed bit-fields.
2401 if (!IVD->getDeclName())
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002402 continue;
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00002403 Ivar[0] = GetMethodVarName(IVD->getIdentifier());
2404 Ivar[1] = GetMethodVarType(IVD);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002405 Ivar[2] = llvm::ConstantInt::get(ObjCTypes.IntTy,
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00002406 ComputeIvarBaseOffset(CGM, OID, IVD));
Owen Anderson0e0189d2009-07-27 22:29:56 +00002407 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002408 }
2409
2410 // Return null for empty list.
2411 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002412 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002413
2414 std::vector<llvm::Constant*> Values(2);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002415 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002416 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002417 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002418 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002419 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002420
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002421 llvm::GlobalVariable *GV;
2422 if (ForClass)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002423 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002424 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00002425 4, true);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002426 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002427 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00002428 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002429 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002430 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002431}
2432
2433/*
2434 struct objc_method {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002435 SEL method_name;
2436 char *method_types;
2437 void *method;
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002438 };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002439
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002440 struct objc_method_list {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002441 struct objc_method_list *obsolete;
2442 int count;
2443 struct objc_method methods_list[count];
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002444 };
2445*/
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002446
2447/// GetMethodConstant - Return a struct objc_method constant for the
2448/// given method if it has been defined. The result is null if the
2449/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002450llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002451 // FIXME: Use DenseMap::lookup
2452 llvm::Function *Fn = MethodDefinitions[MD];
2453 if (!Fn)
2454 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002455
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002456 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002457 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00002458 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002459 ObjCTypes.SelectorPtrTy);
2460 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00002461 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00002462 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002463}
2464
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002465llvm::Constant *CGObjCMac::EmitMethodList(llvm::Twine Name,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00002466 const char *Section,
Daniel Dunbareb1f9a22008-08-27 02:31:56 +00002467 const ConstantVector &Methods) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002468 // Return null for empty list.
2469 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00002470 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002471
2472 std::vector<llvm::Constant*> Values(3);
Owen Anderson0b75f232009-07-31 20:28:54 +00002473 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00002474 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00002475 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002476 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00002477 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00002478 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002479
Daniel Dunbarb25452a2009-04-15 02:56:18 +00002480 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00002481 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00002482 ObjCTypes.MethodListPtrTy);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002483}
2484
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00002485llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002486 const ObjCContainerDecl *CD) {
Daniel Dunbard2386812009-10-19 01:21:19 +00002487 llvm::SmallString<256> Name;
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00002488 GetNameForMethod(OMD, CD, Name);
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002489
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002490 CodeGenTypes &Types = CGM.getTypes();
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002491 const llvm::FunctionType *MethodTy =
Daniel Dunbarbf8c24a2009-02-02 23:23:47 +00002492 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002493 llvm::Function *Method =
Daniel Dunbar7a95ca32008-09-10 04:01:49 +00002494 llvm::Function::Create(MethodTy,
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002495 llvm::GlobalValue::InternalLinkage,
Daniel Dunbard2386812009-10-19 01:21:19 +00002496 Name.str(),
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002497 &CGM.getModule());
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00002498 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002499
Daniel Dunbara94ecd22008-08-16 03:19:19 +00002500 return Method;
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002501}
2502
Daniel Dunbar30c65362009-03-09 20:09:19 +00002503llvm::GlobalVariable *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00002504CGObjCCommonMac::CreateMetadataVar(llvm::Twine Name,
Daniel Dunbar30c65362009-03-09 20:09:19 +00002505 llvm::Constant *Init,
2506 const char *Section,
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002507 unsigned Align,
2508 bool AddToUsed) {
Daniel Dunbar30c65362009-03-09 20:09:19 +00002509 const llvm::Type *Ty = Init->getType();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002510 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00002511 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerf56501c2009-07-17 23:57:13 +00002512 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002513 if (Section)
2514 GV->setSection(Section);
Daniel Dunbar463cc8a2009-03-09 20:50:13 +00002515 if (Align)
2516 GV->setAlignment(Align);
2517 if (AddToUsed)
Chris Lattnerf56501c2009-07-17 23:57:13 +00002518 CGM.AddUsedGlobal(GV);
Daniel Dunbar30c65362009-03-09 20:09:19 +00002519 return GV;
2520}
2521
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002522llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00002523 // Abuse this interface function as a place to finalize.
2524 FinishModule();
Daniel Dunbar303e2c22008-08-11 02:45:11 +00002525 return NULL;
2526}
2527
Chris Lattnerd4808922009-03-22 21:03:39 +00002528llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002529 return ObjCTypes.getGetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002530}
2531
Chris Lattnerd4808922009-03-22 21:03:39 +00002532llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002533 return ObjCTypes.getSetPropertyFn();
Daniel Dunbara91c3e02008-09-24 03:38:44 +00002534}
2535
Fariborz Jahanian5a8c2032010-04-12 18:18:10 +00002536llvm::Constant *CGObjCMac::GetCopyStructFunction() {
2537 return ObjCTypes.getCopyStructFn();
2538}
2539
Chris Lattnerd4808922009-03-22 21:03:39 +00002540llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattnerce8754e2009-04-22 02:44:54 +00002541 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson3f35a262008-08-31 04:05:03 +00002542}
2543
John McCallbd309292010-07-06 01:34:17 +00002544void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2545 return EmitTryOrSynchronizedStmt(CGF, S);
2546}
2547
2548void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2549 const ObjCAtSynchronizedStmt &S) {
2550 return EmitTryOrSynchronizedStmt(CGF, S);
2551}
2552
John McCall65bea082010-07-21 06:59:36 +00002553namespace {
John McCallcda666c2010-07-21 07:22:38 +00002554 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCall65bea082010-07-21 06:59:36 +00002555 const Stmt &S;
2556 llvm::Value *SyncArg;
2557 llvm::Value *CallTryExitVar;
2558 llvm::Value *ExceptionData;
2559 ObjCTypesHelper &ObjCTypes;
2560 PerformFragileFinally(const Stmt *S,
2561 llvm::Value *SyncArg,
2562 llvm::Value *CallTryExitVar,
2563 llvm::Value *ExceptionData,
2564 ObjCTypesHelper *ObjCTypes)
2565 : S(*S), SyncArg(SyncArg), CallTryExitVar(CallTryExitVar),
2566 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2567
2568 void Emit(CodeGenFunction &CGF, bool IsForEH) {
2569 // Check whether we need to call objc_exception_try_exit.
2570 // In optimized code, this branch will always be folded.
2571 llvm::BasicBlock *FinallyCallExit =
2572 CGF.createBasicBlock("finally.call_exit");
2573 llvm::BasicBlock *FinallyNoCallExit =
2574 CGF.createBasicBlock("finally.no_call_exit");
2575 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2576 FinallyCallExit, FinallyNoCallExit);
2577
2578 CGF.EmitBlock(FinallyCallExit);
2579 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2580 ->setDoesNotThrow();
2581
2582 CGF.EmitBlock(FinallyNoCallExit);
2583
2584 if (isa<ObjCAtTryStmt>(S)) {
2585 if (const ObjCAtFinallyStmt* FinallyStmt =
2586 cast<ObjCAtTryStmt>(S).getFinallyStmt())
2587 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2588
2589 // Currently, the end of the cleanup must always exist.
2590 CGF.EnsureInsertPoint();
2591 } else {
2592 // Emit objc_sync_exit(expr); as finally's sole statement for
2593 // @synchronized.
2594 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2595 ->setDoesNotThrow();
2596 }
2597 }
2598 };
2599}
2600
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002601/*
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002602
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002603 Objective-C setjmp-longjmp (sjlj) Exception Handling
2604 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002605
John McCallbd309292010-07-06 01:34:17 +00002606 A catch buffer is a setjmp buffer plus:
2607 - a pointer to the exception that was caught
2608 - a pointer to the previous exception data buffer
2609 - two pointers of reserved storage
2610 Therefore catch buffers form a stack, with a pointer to the top
2611 of the stack kept in thread-local storage.
2612
2613 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2614 objc_exception_try_exit pops the given catch buffer, which is
2615 required to be the top of the EH stack.
2616 objc_exception_throw pops the top of the EH stack, writes the
2617 thrown exception into the appropriate field, and longjmps
2618 to the setjmp buffer. It crashes the process (with a printf
2619 and an abort()) if there are no catch buffers on the stack.
2620 objc_exception_extract just reads the exception pointer out of the
2621 catch buffer.
2622
2623 There's no reason an implementation couldn't use a light-weight
2624 setjmp here --- something like __builtin_setjmp, but API-compatible
2625 with the heavyweight setjmp. This will be more important if we ever
2626 want to implement correct ObjC/C++ exception interactions for the
2627 fragile ABI.
2628
2629 Note that for this use of setjmp/longjmp to be correct, we may need
2630 to mark some local variables volatile: if a non-volatile local
2631 variable is modified between the setjmp and the longjmp, it has
2632 indeterminate value. For the purposes of LLVM IR, it may be
2633 sufficient to make loads and stores within the @try (to variables
2634 declared outside the @try) volatile. This is necessary for
2635 optimized correctness, but is not currently being done; this is
2636 being tracked as rdar://problem/8160285
2637
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002638 The basic framework for a @try-catch-finally is as follows:
2639 {
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002640 objc_exception_data d;
2641 id _rethrow = null;
Anders Carlssonda0e4562009-02-07 21:26:04 +00002642 bool _call_try_exit = true;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002643
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002644 objc_exception_try_enter(&d);
2645 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002646 ... try body ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002647 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002648 // exception path
2649 id _caught = objc_exception_extract(&d);
2650
2651 // enter new try scope for handlers
2652 if (!setjmp(d.jmp_buf)) {
2653 ... match exception and execute catch blocks ...
2654
2655 // fell off end, rethrow.
2656 _rethrow = _caught;
2657 ... jump-through-finally to finally_rethrow ...
2658 } else {
2659 // exception in catch block
2660 _rethrow = objc_exception_extract(&d);
2661 _call_try_exit = false;
2662 ... jump-through-finally to finally_rethrow ...
2663 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002664 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002665 ... jump-through-finally to finally_end ...
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002666
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002667 finally:
Anders Carlssonda0e4562009-02-07 21:26:04 +00002668 if (_call_try_exit)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002669 objc_exception_try_exit(&d);
Anders Carlssonda0e4562009-02-07 21:26:04 +00002670
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002671 ... finally block ....
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002672 ... dispatch to finally destination ...
2673
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002674 finally_rethrow:
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002675 objc_exception_throw(_rethrow);
2676
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002677 finally_end:
2678 }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002679
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002680 This framework differs slightly from the one gcc uses, in that gcc
2681 uses _rethrow to determine if objc_exception_try_exit should be called
2682 and if the object should be rethrown. This breaks in the face of
2683 throwing nil and introduces unnecessary branches.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002684
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002685 We specialize this framework for a few particular circumstances:
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002686
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002687 - If there are no catch blocks, then we avoid emitting the second
2688 exception handling context.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002689
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002690 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2691 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002692
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002693 - FIXME: If there is no @finally block we can do a few more
2694 simplifications.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002695
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002696 Rethrows and Jumps-Through-Finally
2697 --
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002698
John McCallbd309292010-07-06 01:34:17 +00002699 '@throw;' is supported by pushing the currently-caught exception
2700 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002701
John McCallbd309292010-07-06 01:34:17 +00002702 Branches through the @finally block are handled with an ordinary
2703 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2704 exceptions are not compatible with C++ exceptions, and this is
2705 hardly the only place where this will go wrong.
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002706
John McCallbd309292010-07-06 01:34:17 +00002707 @synchronized(expr) { stmt; } is emitted as if it were:
2708 id synch_value = expr;
2709 objc_sync_enter(synch_value);
2710 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002711*/
2712
Fariborz Jahanianc2ad6dc2008-11-21 00:49:24 +00002713void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2714 const Stmt &S) {
2715 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallbd309292010-07-06 01:34:17 +00002716
2717 // A destination for the fall-through edges of the catch handlers to
2718 // jump to.
2719 CodeGenFunction::JumpDest FinallyEnd =
2720 CGF.getJumpDestInCurrentScope("finally.end");
2721
2722 // A destination for the rethrow edge of the catch handlers to jump
2723 // to.
2724 CodeGenFunction::JumpDest FinallyRethrow =
2725 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002726
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002727 // For @synchronized, call objc_sync_enter(sync.expr). The
2728 // evaluation of the expression must occur before we enter the
2729 // @synchronized. We can safely avoid a temp here because jumps into
2730 // @synchronized are illegal & this will dominate uses.
2731 llvm::Value *SyncArg = 0;
2732 if (!isTry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002733 SyncArg =
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002734 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2735 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallbd309292010-07-06 01:34:17 +00002736 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2737 ->setDoesNotThrow();
Daniel Dunbar94ceb612009-02-24 01:43:46 +00002738 }
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002739
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002740 // Allocate memory for the exception data and rethrow pointer.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002741 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
2742 "exceptiondata.ptr");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002743 llvm::Value *RethrowPtr = CGF.CreateTempAlloca(ObjCTypes.ObjectPtrTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00002744 "_rethrow");
John McCallbd309292010-07-06 01:34:17 +00002745
2746 // Create a flag indicating whether the cleanup needs to call
2747 // objc_exception_try_exit. This is true except when
2748 // - no catches match and we're branching through the cleanup
2749 // just to rethrow the exception, or
2750 // - a catch matched and we're falling out of the catch handler.
2751 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlssonda0e4562009-02-07 21:26:04 +00002752 "_call_try_exit");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002753 CGF.Builder.CreateStore(llvm::ConstantInt::getTrue(VMContext),
John McCallbd309292010-07-06 01:34:17 +00002754 CallTryExitVar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002755
John McCallbd309292010-07-06 01:34:17 +00002756 // Push a normal cleanup to leave the try scope.
John McCallcda666c2010-07-21 07:22:38 +00002757 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
2758 SyncArg,
2759 CallTryExitVar,
2760 ExceptionData,
2761 &ObjCTypes);
John McCallbd309292010-07-06 01:34:17 +00002762
2763 // Enter a try block:
2764 // - Call objc_exception_try_enter to push ExceptionData on top of
2765 // the EH stack.
2766 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2767 ->setDoesNotThrow();
2768
2769 // - Call setjmp on the exception data buffer.
2770 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
2771 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
2772 llvm::Value *SetJmpBuffer =
2773 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, GEPIndexes+3, "setjmp_buffer");
2774 llvm::CallInst *SetJmpResult =
2775 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
2776 SetJmpResult->setDoesNotThrow();
2777
2778 // If setjmp returned 0, enter the protected block; otherwise,
2779 // branch to the handler.
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002780 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
2781 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallbd309292010-07-06 01:34:17 +00002782 llvm::Value *DidCatch =
2783 CGF.Builder.CreateIsNull(SetJmpResult, "did_catch_exception");
2784 CGF.Builder.CreateCondBr(DidCatch, TryBlock, TryHandler);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002785
John McCallbd309292010-07-06 01:34:17 +00002786 // Emit the protected block.
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002787 CGF.EmitBlock(TryBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002788 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallbd309292010-07-06 01:34:17 +00002789 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002790 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002791
John McCallbd309292010-07-06 01:34:17 +00002792 // Emit the exception handler block.
Daniel Dunbar7f086782008-09-27 23:30:04 +00002793 CGF.EmitBlock(TryHandler);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002794
2795 // Retrieve the exception object. We may emit multiple blocks but
2796 // nothing can cross this so the value is already in SSA form.
John McCallbd309292010-07-06 01:34:17 +00002797 llvm::CallInst *Caught =
Chris Lattnerc6406db2009-04-22 02:26:14 +00002798 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2799 ExceptionData, "caught");
John McCallbd309292010-07-06 01:34:17 +00002800 Caught->setDoesNotThrow();
2801
2802 // Remember the exception to rethrow.
2803 CGF.Builder.CreateStore(Caught, RethrowPtr);
2804
2805 // Note: at this point, objc_exception_throw already popped the
2806 // catch handler, so anything that branches to the cleanup needs
2807 // to set CallTryExitVar to false.
2808
2809 // For a @synchronized (or a @try with no catches), just branch
2810 // through the cleanup to the rethrow block.
2811 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
2812 // Tell the cleanup not to re-pop the exit.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002813 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
John McCallbd309292010-07-06 01:34:17 +00002814 CallTryExitVar);
2815
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002816 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallbd309292010-07-06 01:34:17 +00002817
2818 // Otherwise, we have to match against the caught exceptions.
2819 } else {
2820 // Push the exception to rethrow onto the EH value stack for the
2821 // benefit of any @throws in the handlers.
2822 CGF.ObjCEHValueStack.push_back(Caught);
2823
Douglas Gregor96c79492010-04-23 22:50:49 +00002824 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
2825
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002826 // Enter a new exception try block (in case a @catch block throws
John McCallbd309292010-07-06 01:34:17 +00002827 // an exception). Now CallTryExitVar (currently true) is back in
2828 // synch with reality.
2829 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
2830 ->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002831
John McCallbd309292010-07-06 01:34:17 +00002832 llvm::CallInst *SetJmpResult =
2833 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
2834 "setjmp.result");
2835 SetJmpResult->setDoesNotThrow();
2836
2837 llvm::Value *Threw =
2838 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002839
Daniel Dunbar75283ff2008-11-11 02:29:29 +00002840 llvm::BasicBlock *CatchBlock = CGF.createBasicBlock("catch");
John McCallbd309292010-07-06 01:34:17 +00002841 llvm::BasicBlock *CatchHandler = CGF.createBasicBlock("catch_for_catch");
Daniel Dunbar7f086782008-09-27 23:30:04 +00002842 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002843
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002844 CGF.EmitBlock(CatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002845
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002846 // Handle catch list. As a special case we check if everything is
2847 // matched and avoid generating code for falling off the end if
2848 // so.
2849 bool AllMatched = false;
Douglas Gregor96c79492010-04-23 22:50:49 +00002850 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
2851 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002852
Douglas Gregor46a572b2010-04-26 16:46:50 +00002853 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff7cae42b2009-07-10 23:34:53 +00002854 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar523208f2008-09-27 07:36:24 +00002855
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002856 // catch(...) always matches.
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002857 if (!CatchParam) {
2858 AllMatched = true;
2859 } else {
John McCall9dd450b2009-09-21 23:43:11 +00002860 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002861
John McCallbd309292010-07-06 01:34:17 +00002862 // catch(id e) always matches under this ABI, since only
2863 // ObjC exceptions end up here in the first place.
Daniel Dunbar86919f42008-09-27 22:21:14 +00002864 // FIXME: For the time being we also match id<X>; this should
2865 // be rejected by Sema instead.
Eli Friedman55179ca2009-07-11 00:57:02 +00002866 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002867 AllMatched = true;
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002868 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002869
John McCallbd309292010-07-06 01:34:17 +00002870 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002871 if (AllMatched) {
John McCallbd309292010-07-06 01:34:17 +00002872 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
2873
Anders Carlsson9396a892008-09-11 09:15:33 +00002874 if (CatchParam) {
Steve Naroff371b8fb2009-03-03 19:52:17 +00002875 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00002876 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallbd309292010-07-06 01:34:17 +00002877
2878 // These types work out because ConvertType(id) == i8*.
Steve Naroff371b8fb2009-03-03 19:52:17 +00002879 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlsson9396a892008-09-11 09:15:33 +00002880 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002881
Anders Carlsson9396a892008-09-11 09:15:33 +00002882 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00002883
2884 // The scope of the catch variable ends right here.
2885 CatchVarCleanups.ForceCleanup();
2886
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002887 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002888 break;
2889 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002890
Steve Naroff7cae42b2009-07-10 23:34:53 +00002891 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall96fa4842010-05-17 21:00:27 +00002892 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallbd309292010-07-06 01:34:17 +00002893
2894 // FIXME: @catch (Class c) ?
John McCall96fa4842010-05-17 21:00:27 +00002895 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
2896 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002897
2898 // Check if the @catch block matches the exception object.
John McCall96fa4842010-05-17 21:00:27 +00002899 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002900
John McCallbd309292010-07-06 01:34:17 +00002901 llvm::CallInst *Match =
Chris Lattnerc6406db2009-04-22 02:26:14 +00002902 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
2903 Class, Caught, "match");
John McCallbd309292010-07-06 01:34:17 +00002904 Match->setDoesNotThrow();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002905
John McCallbd309292010-07-06 01:34:17 +00002906 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
2907 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002908
2909 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbar7f086782008-09-27 23:30:04 +00002910 MatchedBlock, NextCatchBlock);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002911
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002912 // Emit the @catch block.
2913 CGF.EmitBlock(MatchedBlock);
John McCallbd309292010-07-06 01:34:17 +00002914
2915 // Collect any cleanups for the catch variable. The scope lasts until
2916 // the end of the catch body.
2917 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
2918
Steve Naroff371b8fb2009-03-03 19:52:17 +00002919 CGF.EmitLocalBlockVarDecl(*CatchParam);
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00002920 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002921
John McCallbd309292010-07-06 01:34:17 +00002922 // Initialize the catch variable.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002923 llvm::Value *Tmp =
2924 CGF.Builder.CreateBitCast(Caught,
2925 CGF.ConvertType(CatchParam->getType()),
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002926 "tmp");
Steve Naroff371b8fb2009-03-03 19:52:17 +00002927 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002928
Anders Carlsson9396a892008-09-11 09:15:33 +00002929 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallbd309292010-07-06 01:34:17 +00002930
2931 // We're done with the catch variable.
2932 CatchVarCleanups.ForceCleanup();
2933
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002934 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002935
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002936 CGF.EmitBlock(NextCatchBlock);
2937 }
2938
John McCallbd309292010-07-06 01:34:17 +00002939 CGF.ObjCEHValueStack.pop_back();
2940
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002941 if (!AllMatched) {
2942 // None of the handlers caught the exception, so store it to be
2943 // rethrown at the end of the @finally block.
2944 CGF.Builder.CreateStore(Caught, RethrowPtr);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002945 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002946 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002947
Daniel Dunbarb22ff592008-09-27 07:03:52 +00002948 // Emit the exception handler for the @catch blocks.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002949 CGF.EmitBlock(CatchHandler);
John McCallbd309292010-07-06 01:34:17 +00002950
2951 // Rethrow the new exception, not the old one.
2952 Caught = CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
2953 ExceptionData);
2954 Caught->setDoesNotThrow();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002955 CGF.Builder.CreateStore(Caught, RethrowPtr);
John McCallbd309292010-07-06 01:34:17 +00002956
2957 // Don't pop the catch handler; the throw already did.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002958 CGF.Builder.CreateStore(llvm::ConstantInt::getFalse(VMContext),
John McCallbd309292010-07-06 01:34:17 +00002959 CallTryExitVar);
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002960 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002961 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002962
John McCallbd309292010-07-06 01:34:17 +00002963 // Pop the cleanup.
2964 CGF.PopCleanupBlock();
John McCallad5d61e2010-07-23 21:56:41 +00002965 CGF.EmitBlock(FinallyEnd.getBlock());
Anders Carlssonbfee7e92009-02-09 20:38:58 +00002966
John McCallbd309292010-07-06 01:34:17 +00002967 // Emit the rethrow block.
2968 CGF.Builder.ClearInsertionPoint();
John McCallad5d61e2010-07-23 21:56:41 +00002969 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallbd309292010-07-06 01:34:17 +00002970 if (CGF.HaveInsertPoint()) {
2971 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(),
2972 CGF.Builder.CreateLoad(RethrowPtr))
2973 ->setDoesNotThrow();
2974 CGF.Builder.CreateUnreachable();
Fariborz Jahaniane2caaaa2008-11-21 19:21:53 +00002975 }
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002976
John McCallad5d61e2010-07-23 21:56:41 +00002977 CGF.Builder.SetInsertPoint(FinallyEnd.getBlock());
Anders Carlsson1963b0c2008-09-09 10:04:29 +00002978}
2979
2980void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2efd5382008-09-30 01:06:03 +00002981 const ObjCAtThrowStmt &S) {
Anders Carlssone005aa12008-09-09 16:16:55 +00002982 llvm::Value *ExceptionAsObject;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002983
Anders Carlssone005aa12008-09-09 16:16:55 +00002984 if (const Expr *ThrowExpr = S.getThrowExpr()) {
2985 llvm::Value *Exception = CGF.EmitScalarExpr(ThrowExpr);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002986 ExceptionAsObject =
Anders Carlssone005aa12008-09-09 16:16:55 +00002987 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
2988 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002989 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbard3dcb4f82008-09-28 01:03:14 +00002990 "Unexpected rethrow outside @catch block.");
Anders Carlssonbf8a1be2009-02-07 21:37:21 +00002991 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlssone005aa12008-09-09 16:16:55 +00002992 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00002993
John McCallbd309292010-07-06 01:34:17 +00002994 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
2995 ->setDoesNotReturn();
Anders Carlsson4f1c7c32008-09-09 17:59:25 +00002996 CGF.Builder.CreateUnreachable();
Daniel Dunbar5c7e3932008-11-11 23:11:34 +00002997
2998 // Clear the insertion point to indicate we are in unreachable code.
2999 CGF.Builder.ClearInsertionPoint();
Anders Carlsson1963b0c2008-09-09 10:04:29 +00003000}
3001
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003002/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003003/// object: objc_read_weak (id *src)
3004///
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003005llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003006 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00003007 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003008 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3009 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3010 ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00003011 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003012 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00003013 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanianf5125d12008-11-18 21:45:40 +00003014 return read_weak;
3015}
3016
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003017/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3018/// objc_assign_weak (id src, id *dst)
3019///
3020void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003021 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003022 const llvm::Type * SrcTy = src->getType();
3023 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003024 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003025 assert(Size <= 8 && "does not support size > 8");
3026 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003027 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003028 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3029 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003030 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3031 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00003032 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian83f45b552008-11-18 22:37:34 +00003033 src, dst, "weakassign");
3034 return;
3035}
3036
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003037/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3038/// objc_assign_global (id src, id *dst)
3039///
3040void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00003041 llvm::Value *src, llvm::Value *dst,
3042 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003043 const llvm::Type * SrcTy = src->getType();
3044 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003045 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003046 assert(Size <= 8 && "does not support size > 8");
3047 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003048 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003049 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3050 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003051 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3052 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00003053 if (!threadlocal)
3054 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3055 src, dst, "globalassign");
3056 else
3057 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3058 src, dst, "threadlocalassign");
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003059 return;
3060}
3061
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003062/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003063/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003064///
3065void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003066 llvm::Value *src, llvm::Value *dst,
3067 llvm::Value *ivarOffset) {
3068 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003069 const llvm::Type * SrcTy = src->getType();
3070 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003071 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003072 assert(Size <= 8 && "does not support size > 8");
3073 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003074 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003075 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3076 }
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003077 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3078 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00003079 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3080 src, dst, ivarOffset);
Fariborz Jahaniane881b532008-11-20 19:23:36 +00003081 return;
3082}
3083
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003084/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3085/// objc_assign_strongCast (id src, id *dst)
3086///
3087void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00003088 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003089 const llvm::Type * SrcTy = src->getType();
3090 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003091 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003092 assert(Size <= 8 && "does not support size > 8");
3093 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003094 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00003095 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3096 }
Fariborz Jahanian50a12702008-11-19 17:34:06 +00003097 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3098 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00003099 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahaniand7db9642008-11-19 00:59:10 +00003100 src, dst, "weakassign");
3101 return;
3102}
3103
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003104void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003105 llvm::Value *DestPtr,
3106 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003107 llvm::Value *size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003108 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3109 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003110 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00003111 DestPtr, SrcPtr, size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00003112 return;
3113}
3114
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003115/// EmitObjCValueForIvar - Code Gen for ivar reference.
3116///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003117LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3118 QualType ObjectTy,
3119 llvm::Value *BaseValue,
3120 const ObjCIvarDecl *Ivar,
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00003121 unsigned CVRQualifiers) {
John McCall8b07ec22010-05-15 11:32:37 +00003122 const ObjCInterfaceDecl *ID =
3123 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003124 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3125 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian9f84b782009-02-02 20:02:29 +00003126}
3127
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003128llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar722f4242009-04-22 05:08:15 +00003129 const ObjCInterfaceDecl *Interface,
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003130 const ObjCIvarDecl *Ivar) {
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00003131 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003132 return llvm::ConstantInt::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003133 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3134 Offset);
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00003135}
3136
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003137/* *** Private Interface *** */
3138
3139/// EmitImageInfo - Emit the image info marker used to encode some module
3140/// level information.
3141///
3142/// See: <rdr://4810609&4810587&4810587>
3143/// struct IMAGE_INFO {
3144/// unsigned version;
3145/// unsigned flags;
3146/// };
3147enum ImageInfoFlags {
Daniel Dunbar5e639272010-04-25 20:39:01 +00003148 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003149 eImageInfo_GarbageCollected = (1 << 1),
3150 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003151 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3152
Daniel Dunbar5e639272010-04-25 20:39:01 +00003153 // A flag indicating that the module has no instances of a @synthesize of a
3154 // superclass variable. <rdar://problem/6803242>
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003155 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003156};
3157
Daniel Dunbar5e639272010-04-25 20:39:01 +00003158void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003159 unsigned version = 0; // Version is unused?
3160 unsigned flags = 0;
3161
3162 // FIXME: Fix and continue?
3163 if (CGM.getLangOptions().getGCMode() != LangOptions::NonGC)
3164 flags |= eImageInfo_GarbageCollected;
3165 if (CGM.getLangOptions().getGCMode() == LangOptions::GCOnly)
3166 flags |= eImageInfo_GCOnly;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003167
Daniel Dunbar75e909f2009-04-20 07:11:47 +00003168 // We never allow @synthesize of a superclass property.
3169 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003170
Chris Lattner5e016ae2010-06-27 07:15:29 +00003171 const llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
3172
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003173 // Emitted as int[2];
3174 llvm::Constant *values[2] = {
Chris Lattner5e016ae2010-06-27 07:15:29 +00003175 llvm::ConstantInt::get(Int32Ty, version),
3176 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003177 };
Chris Lattner5e016ae2010-06-27 07:15:29 +00003178 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003179
3180 const char *Section;
3181 if (ObjCABI == 1)
3182 Section = "__OBJC, __image_info,regular";
3183 else
3184 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003185 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003186 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Owen Anderson47034e12009-07-28 18:33:04 +00003187 llvm::ConstantArray::get(AT, values, 2),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003188 Section,
3189 0,
3190 true);
3191 GV->setConstant(true);
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003192}
3193
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003194
3195// struct objc_module {
3196// unsigned long version;
3197// unsigned long size;
3198// const char *name;
3199// Symtab symtab;
3200// };
3201
3202// FIXME: Get from somewhere
3203static const int ModuleVersion = 7;
3204
3205void CGObjCMac::EmitModuleInfo() {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003206 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003207
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003208 std::vector<llvm::Constant*> Values(4);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003209 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion);
3210 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Daniel Dunbar92992502008-08-15 22:20:32 +00003211 // This used to be the filename, now it is unused. <rdr://4327263>
Daniel Dunbarb036db82008-08-13 03:21:16 +00003212 Values[2] = GetClassName(&CGM.getContext().Idents.get(""));
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003213 Values[3] = EmitModuleSymbols();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003214 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson0e0189d2009-07-27 22:29:56 +00003215 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003216 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbarae333842009-03-09 22:18:41 +00003217 4, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003218}
3219
3220llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003221 unsigned NumClasses = DefinedClasses.size();
3222 unsigned NumCategories = DefinedCategories.size();
3223
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003224 // Return null if no symbols were defined.
3225 if (!NumClasses && !NumCategories)
Owen Anderson0b75f232009-07-31 20:28:54 +00003226 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003227
3228 std::vector<llvm::Constant*> Values(5);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003229 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Anderson0b75f232009-07-31 20:28:54 +00003230 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00003231 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3232 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003233
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003234 // The runtime expects exactly the list of defined classes followed
3235 // by the list of defined categories, in a single array.
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003236 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003237 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00003238 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003239 ObjCTypes.Int8PtrTy);
3240 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003241 Symbols[NumClasses + i] =
Owen Andersonade90fd2009-07-29 18:54:39 +00003242 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar938a77f2008-08-22 20:34:54 +00003243 ObjCTypes.Int8PtrTy);
3244
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003245 Values[4] =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003246 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003247 NumClasses + NumCategories),
3248 Symbols);
3249
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00003250 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003251
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003252 llvm::GlobalVariable *GV =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003253 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3254 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003255 4, true);
Owen Andersonade90fd2009-07-29 18:54:39 +00003256 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003257}
3258
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003259llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003260 const ObjCInterfaceDecl *ID) {
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003261 LazySymbols.insert(ID->getIdentifier());
3262
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003263 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003264
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003265 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003266 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003267 llvm::ConstantExpr::getBitCast(GetClassName(ID->getIdentifier()),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003268 ObjCTypes.ClassPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003269 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003270 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3271 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003272 4, true);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003273 }
3274
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003275 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003276}
3277
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003278llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3279 bool lvalue) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003280 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003281
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003282 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003283 llvm::Constant *Casted =
Owen Andersonade90fd2009-07-29 18:54:39 +00003284 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003285 ObjCTypes.SelectorPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003286 Entry =
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003287 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3288 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbarb25452a2009-04-15 02:56:18 +00003289 4, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003290 }
3291
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00003292 if (lvalue)
3293 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00003294 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003295}
3296
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00003297llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbarb036db82008-08-13 03:21:16 +00003298 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003299
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003300 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003301 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003302 llvm::ConstantArray::get(VMContext,
3303 Ident->getNameStart()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003304 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003305 1, true);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003306
Owen Anderson170229f2009-07-14 23:10:40 +00003307 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003308}
3309
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003310/// GetIvarLayoutName - Returns a unique constant for the given
3311/// ivar layout bitmap.
3312llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003313 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Anderson0b75f232009-07-31 20:28:54 +00003314 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahanian01dff422009-03-05 19:17:31 +00003315}
3316
John McCall8ccfcb52009-09-24 19:53:00 +00003317static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003318 if (FQT.isObjCGCStrong())
John McCall8ccfcb52009-09-24 19:53:00 +00003319 return Qualifiers::Strong;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003320
3321 if (FQT.isObjCGCWeak())
John McCall8ccfcb52009-09-24 19:53:00 +00003322 return Qualifiers::Weak;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003323
Fariborz Jahanian10e9bff2009-09-11 17:39:05 +00003324 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
John McCall8ccfcb52009-09-24 19:53:00 +00003325 return Qualifiers::Strong;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003326
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003327 if (const PointerType *PT = FQT->getAs<PointerType>())
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003328 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003329
John McCall8ccfcb52009-09-24 19:53:00 +00003330 return Qualifiers::GCNone;
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003331}
3332
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003333void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003334 unsigned int BytePos,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003335 bool ForStrongLayout,
3336 bool &HasUnion) {
3337 const RecordDecl *RD = RT->getDecl();
3338 // FIXME - Use iterator.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003339 llvm::SmallVector<FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003340 const llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003341 const llvm::StructLayout *RecLayout =
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003342 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003343
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003344 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3345 ForStrongLayout, HasUnion);
3346}
3347
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003348void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003349 const llvm::StructLayout *Layout,
3350 const RecordDecl *RD,
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003351 const llvm::SmallVectorImpl<FieldDecl*> &RecFields,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003352 unsigned int BytePos, bool ForStrongLayout,
3353 bool &HasUnion) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003354 bool IsUnion = (RD && RD->isUnion());
3355 uint64_t MaxUnionIvarSize = 0;
3356 uint64_t MaxSkippedUnionIvarSize = 0;
3357 FieldDecl *MaxField = 0;
3358 FieldDecl *MaxSkippedField = 0;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003359 FieldDecl *LastFieldBitfield = 0;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003360 uint64_t MaxFieldOffset = 0;
3361 uint64_t MaxSkippedFieldOffset = 0;
3362 uint64_t LastBitfieldOffset = 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003363
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003364 if (RecFields.empty())
3365 return;
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003366 unsigned WordSizeInBits = CGM.getContext().Target.getPointerWidth(0);
3367 unsigned ByteSizeInBits = CGM.getContext().Target.getCharWidth();
3368
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003369 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003370 FieldDecl *Field = RecFields[i];
Daniel Dunbar62b10702009-05-03 23:35:23 +00003371 uint64_t FieldOffset;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003372 if (RD) {
Daniel Dunbard51c1a62010-04-14 17:02:21 +00003373 // Note that 'i' here is actually the field index inside RD of Field,
3374 // although this dependency is hidden.
3375 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
3376 FieldOffset = RL.getFieldOffset(i) / 8;
Anders Carlssone2c6baf2009-07-24 17:23:54 +00003377 } else
Daniel Dunbar62b10702009-05-03 23:35:23 +00003378 FieldOffset = ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field));
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003379
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003380 // Skip over unnamed or bitfields
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003381 if (!Field->getIdentifier() || Field->isBitField()) {
3382 LastFieldBitfield = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003383 LastBitfieldOffset = FieldOffset;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003384 continue;
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003385 }
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003386
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003387 LastFieldBitfield = 0;
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003388 QualType FQT = Field->getType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003389 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003390 if (FQT->isUnionType())
3391 HasUnion = true;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003392
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003393 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003394 BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003395 ForStrongLayout, HasUnion);
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003396 continue;
3397 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003398
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003399 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003400 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003401 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003402 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003403 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003404 FQT = CArray->getElementType();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003405 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3406 const ConstantArrayType *CArray =
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003407 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003408 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003409 FQT = CArray->getElementType();
3410 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003411
3412 assert(!FQT->isUnionType() &&
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003413 "layout for array of unions not supported");
3414 if (FQT->isRecordType()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003415 int OldIndex = IvarsInfo.size() - 1;
3416 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003417
Ted Kremenekc23c7e62009-07-29 21:53:49 +00003418 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003419 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003420 ForStrongLayout, HasUnion);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003421
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003422 // Replicate layout information for each array element. Note that
3423 // one element is already done.
3424 uint64_t ElIx = 1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003425 for (int FirstIndex = IvarsInfo.size() - 1,
3426 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003427 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003428 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3429 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3430 IvarsInfo[i].ivar_size));
3431 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3432 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3433 SkipIvars[i].ivar_size));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003434 }
3435 continue;
3436 }
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003437 }
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003438 // At this point, we are done with Record/Union and array there of.
3439 // For other arrays we are down to its element type.
John McCall8ccfcb52009-09-24 19:53:00 +00003440 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar7abf83c2009-05-03 13:55:09 +00003441
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003442 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall8ccfcb52009-09-24 19:53:00 +00003443 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3444 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003445 if (IsUnion) {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003446 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003447 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003448 MaxUnionIvarSize = UnionIvarSize;
3449 MaxField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003450 MaxFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003451 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003452 } else {
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003453 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003454 FieldSize / WordSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003455 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003456 } else if ((ForStrongLayout &&
John McCall8ccfcb52009-09-24 19:53:00 +00003457 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3458 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar22007d32009-05-03 13:32:01 +00003459 if (IsUnion) {
Mike Stump18bb9282009-05-16 07:57:57 +00003460 // FIXME: Why the asymmetry? We divide by word size in bits on other
3461 // side.
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003462 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar22007d32009-05-03 13:32:01 +00003463 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003464 MaxSkippedUnionIvarSize = UnionIvarSize;
3465 MaxSkippedField = Field;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003466 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003467 }
Daniel Dunbar22007d32009-05-03 13:32:01 +00003468 } else {
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003469 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar94f46dc2009-05-03 14:17:18 +00003470 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003471 FieldSize / ByteSizeInBits));
Fariborz Jahanian3b0f8862009-03-11 00:07:04 +00003472 }
3473 }
3474 }
Daniel Dunbar15bd8882009-05-03 14:10:34 +00003475
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003476 if (LastFieldBitfield) {
3477 // Last field was a bitfield. Must update skip info.
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003478 Expr *BitWidth = LastFieldBitfield->getBitWidth();
3479 uint64_t BitFieldSize =
Eli Friedman1c4a1752009-04-26 19:19:15 +00003480 BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Daniel Dunbar22007d32009-05-03 13:32:01 +00003481 GC_IVAR skivar;
Daniel Dunbar5b743912009-05-03 23:31:46 +00003482 skivar.ivar_bytepos = BytePos + LastBitfieldOffset;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003483 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3484 + ((BitFieldSize % ByteSizeInBits) != 0);
3485 SkipIvars.push_back(skivar);
Fariborz Jahanianf5fec022009-04-21 18:33:06 +00003486 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003487
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003488 if (MaxField)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003489 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003490 MaxUnionIvarSize));
3491 if (MaxSkippedField)
Daniel Dunbar5b743912009-05-03 23:31:46 +00003492 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar7b89ace2009-05-03 13:44:42 +00003493 MaxSkippedUnionIvarSize));
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003494}
3495
3496/// BuildIvarLayout - Builds ivar layout bitmap for the class
3497/// implementation for the __strong or __weak case.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003498/// The layout map displays which words in ivar list must be skipped
3499/// and which must be scanned by GC (see below). String is built of bytes.
3500/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003501/// of words to skip and right nibble is count of words to scan. So, each
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003502/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003503/// represented by a 0x00 byte which also ends the string.
3504/// 1. when ForStrongLayout is true, following ivars are scanned:
3505/// - id, Class
3506/// - object *
3507/// - __strong anything
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003508///
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003509/// 2. When ForStrongLayout is false, following ivars are scanned:
3510/// - __weak anything
3511///
Fariborz Jahanian524bb202009-03-10 16:22:08 +00003512llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003513 const ObjCImplementationDecl *OMD,
3514 bool ForStrongLayout) {
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003515 bool hasUnion = false;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003516
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003517 unsigned int WordsToScan, WordsToSkip;
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003518 const llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003519 if (CGM.getLangOptions().getGCMode() == LangOptions::NonGC)
Owen Anderson0b75f232009-07-31 20:28:54 +00003520 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003521
Chris Lattner5b36ddb2009-03-31 08:48:01 +00003522 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003523 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003524 CGM.getContext().CollectObjCIvars(OI, RecFields);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003525
Daniel Dunbar98ba9642009-05-04 04:10:48 +00003526 // Add this implementations synthesized ivars.
Fariborz Jahanian0f44d812009-05-12 18:14:29 +00003527 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
Fariborz Jahanianaef66222010-02-19 00:31:17 +00003528 CGM.getContext().CollectNonClassIvars(OI, Ivars);
Fariborz Jahanian0f44d812009-05-12 18:14:29 +00003529 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
3530 RecFields.push_back(cast<FieldDecl>(Ivars[k]));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003531
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003532 if (RecFields.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003533 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003534
3535 SkipIvars.clear();
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003536 IvarsInfo.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003537
Daniel Dunbar36e2a1e2009-05-03 21:05:10 +00003538 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003539 if (IvarsInfo.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00003540 return llvm::Constant::getNullValue(PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003541
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003542 // Sort on byte position in case we encounterred a union nested in
3543 // the ivar list.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003544 if (hasUnion && !IvarsInfo.empty())
Daniel Dunbar22b0ada2009-04-23 01:29:05 +00003545 std::sort(IvarsInfo.begin(), IvarsInfo.end());
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003546 if (hasUnion && !SkipIvars.empty())
Daniel Dunbar22b0ada2009-04-23 01:29:05 +00003547 std::sort(SkipIvars.begin(), SkipIvars.end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003548
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003549 // Build the string of skip/scan nibbles
Fariborz Jahaniance5676572009-04-24 17:15:27 +00003550 llvm::SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003551 unsigned int WordSize =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00003552 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003553 if (IvarsInfo[0].ivar_bytepos == 0) {
3554 WordsToSkip = 0;
3555 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003556 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003557 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3558 WordsToScan = IvarsInfo[0].ivar_size;
3559 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003560 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003561 unsigned int TailPrevGCObjC =
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003562 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003563 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003564 // consecutive 'scanned' object pointers.
3565 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003566 } else {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003567 // Skip over 'gc'able object pointer which lay over each other.
3568 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3569 continue;
3570 // Must skip over 1 or more words. We save current skip/scan values
3571 // and start a new pair.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003572 SKIP_SCAN SkScan;
3573 SkScan.skip = WordsToSkip;
3574 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003575 SkipScanIvars.push_back(SkScan);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003576
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003577 // Skip the hole.
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003578 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3579 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003580 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003581 WordsToSkip = 0;
3582 WordsToScan = IvarsInfo[i].ivar_size;
3583 }
3584 }
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003585 if (WordsToScan > 0) {
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003586 SKIP_SCAN SkScan;
3587 SkScan.skip = WordsToSkip;
3588 SkScan.scan = WordsToScan;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003589 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003590 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003591
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003592 if (!SkipIvars.empty()) {
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003593 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003594 int LastByteSkipped =
3595 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003596 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003597 int LastByteScanned =
3598 IvarsInfo[LastIndex].ivar_bytepos +
3599 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003600 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramerd20ef752009-12-25 15:43:36 +00003601 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003602 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanian1bf72882009-03-12 22:50:49 +00003603 SKIP_SCAN SkScan;
3604 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3605 SkScan.scan = 0;
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003606 SkipScanIvars.push_back(SkScan);
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003607 }
3608 }
3609 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3610 // as 0xMN.
Fariborz Jahaniana123b642009-04-24 16:17:09 +00003611 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003612 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003613 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3614 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3615 // 0xM0 followed by 0x0N detected.
3616 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3617 for (int j = i+1; j < SkipScan; j++)
3618 SkipScanIvars[j] = SkipScanIvars[j+1];
3619 --SkipScan;
3620 }
3621 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003622
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003623 // Generate the string.
3624 std::string BitMap;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003625 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003626 unsigned char byte;
3627 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3628 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3629 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3630 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
3631
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003632 // first skip big.
3633 for (unsigned int ix = 0; ix < skip_big; ix++)
3634 BitMap += (unsigned char)(0xf0);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003635
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003636 // next (skip small, scan)
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003637 if (skip_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003638 byte = skip_small << 4;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003639 if (scan_big > 0) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003640 byte |= 0xf;
3641 --scan_big;
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003642 } else if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003643 byte |= scan_small;
3644 scan_small = 0;
3645 }
3646 BitMap += byte;
3647 }
3648 // next scan big
3649 for (unsigned int ix = 0; ix < scan_big; ix++)
3650 BitMap += (unsigned char)(0x0f);
3651 // last scan small
Daniel Dunbard22aa4a2009-05-03 23:21:22 +00003652 if (scan_small) {
Fariborz Jahaniancbaf73c2009-03-11 20:59:05 +00003653 byte = scan_small;
3654 BitMap += byte;
3655 }
3656 }
3657 // null terminate string.
Fariborz Jahanianf909f922009-03-25 22:36:49 +00003658 unsigned char zero = 0;
3659 BitMap += zero;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003660
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003661 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003662 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian80c9ce22009-04-20 22:03:45 +00003663 ForStrongLayout ? "strong" : "weak",
3664 OMD->getClassInterface()->getNameAsCString());
3665 const unsigned char *s = (unsigned char*)BitMap.c_str();
3666 for (unsigned i = 0; i < BitMap.size(); i++)
3667 if (!(s[i] & 0xf0))
3668 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
3669 else
3670 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
3671 printf("\n");
3672 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003673 llvm::GlobalVariable * Entry =
3674 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003675 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003676 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003677 1, true);
3678 return getConstantGEP(VMContext, Entry, 0, 0);
Fariborz Jahanianc559f3f2009-03-05 22:39:55 +00003679}
3680
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003681llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003682 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
3683
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003684 // FIXME: Avoid std::string copying.
3685 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003686 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson41a75022009-08-13 21:57:51 +00003687 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003688 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003689 1, true);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003690
Owen Anderson170229f2009-07-14 23:10:40 +00003691 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003692}
3693
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003694// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003695llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003696 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
3697}
3698
3699// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003700llvm::Constant *CGObjCCommonMac::GetMethodVarName(const std::string &Name) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003701 return GetMethodVarName(&CGM.getContext().Idents.get(Name));
3702}
3703
Daniel Dunbarf5c18462009-04-20 06:54:31 +00003704llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003705 std::string TypeStr;
3706 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
3707
3708 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbarb036db82008-08-13 03:21:16 +00003709
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003710 if (!Entry)
3711 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003712 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003713 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003714 1, true);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003715
Owen Anderson170229f2009-07-14 23:10:40 +00003716 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarcb515c82008-08-12 03:39:23 +00003717}
3718
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003719llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D) {
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003720 std::string TypeStr;
Daniel Dunbar3c76cb52008-08-26 21:51:14 +00003721 CGM.getContext().getObjCEncodingForMethodDecl(const_cast<ObjCMethodDecl*>(D),
3722 TypeStr);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003723
3724 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
3725
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003726 if (!Entry)
3727 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson41a75022009-08-13 21:57:51 +00003728 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbarcda43072010-07-29 22:57:21 +00003729 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003730 1, true);
Devang Patel4b6e4bb2009-03-04 18:21:39 +00003731
Owen Anderson170229f2009-07-14 23:10:40 +00003732 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003733}
3734
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003735// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003736llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003737 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003738
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003739 if (!Entry)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003740 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00003741 llvm::ConstantArray::get(VMContext,
3742 Ident->getNameStart()),
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003743 "__TEXT,__cstring,cstring_literals",
Daniel Dunbar3241fae2009-04-14 23:14:47 +00003744 1, true);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003745
Owen Anderson170229f2009-07-14 23:10:40 +00003746 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003747}
3748
3749// FIXME: Merge into a single cstring creation function.
Daniel Dunbar4932b362008-08-28 04:38:10 +00003750// FIXME: This Decl should be more precise.
Daniel Dunbarc2d4b622009-03-09 21:49:58 +00003751llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003752CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
3753 const Decl *Container) {
Daniel Dunbar4932b362008-08-28 04:38:10 +00003754 std::string TypeStr;
3755 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbar80a840b2008-08-23 00:19:03 +00003756 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
3757}
3758
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003759void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian0b1ccdc2009-01-21 23:34:32 +00003760 const ObjCContainerDecl *CD,
Daniel Dunbard2386812009-10-19 01:21:19 +00003761 llvm::SmallVectorImpl<char> &Name) {
3762 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian0196a1c2009-01-10 21:06:09 +00003763 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbard2386812009-10-19 01:21:19 +00003764 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
3765 << '[' << CD->getName();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003766 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbard2386812009-10-19 01:21:19 +00003767 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramerb11416d2010-04-17 09:33:03 +00003768 OS << '(' << CID << ')';
Daniel Dunbard2386812009-10-19 01:21:19 +00003769 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbara94ecd22008-08-16 03:19:19 +00003770}
3771
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003772void CGObjCMac::FinishModule() {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003773 EmitModuleInfo();
3774
Daniel Dunbarc475d422008-10-29 22:36:39 +00003775 // Emit the dummy bodies for any protocols which were referenced but
3776 // never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003777 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerf56501c2009-07-17 23:57:13 +00003778 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
3779 if (I->second->hasInitializer())
Daniel Dunbarc475d422008-10-29 22:36:39 +00003780 continue;
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003781
Daniel Dunbarc475d422008-10-29 22:36:39 +00003782 std::vector<llvm::Constant*> Values(5);
Owen Anderson0b75f232009-07-31 20:28:54 +00003783 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003784 Values[1] = GetClassName(I->first);
Owen Anderson0b75f232009-07-31 20:28:54 +00003785 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarc475d422008-10-29 22:36:39 +00003786 Values[3] = Values[4] =
Owen Anderson0b75f232009-07-31 20:28:54 +00003787 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerf56501c2009-07-17 23:57:13 +00003788 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson0e0189d2009-07-27 22:29:56 +00003789 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbarc475d422008-10-29 22:36:39 +00003790 Values));
Chris Lattnerf56501c2009-07-17 23:57:13 +00003791 CGM.AddUsedGlobal(I->second);
Daniel Dunbarc475d422008-10-29 22:36:39 +00003792 }
3793
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003794 // Add assembler directives to add lazy undefined symbol references
3795 // for classes which are referenced but not defined. This is
3796 // important for correct linker interaction.
Daniel Dunbard027a922009-09-07 00:20:42 +00003797 //
3798 // FIXME: It would be nice if we had an LLVM construct for this.
3799 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
3800 llvm::SmallString<256> Asm;
3801 Asm += CGM.getModule().getModuleInlineAsm();
3802 if (!Asm.empty() && Asm.back() != '\n')
3803 Asm += '\n';
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003804
Daniel Dunbard027a922009-09-07 00:20:42 +00003805 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbard027a922009-09-07 00:20:42 +00003806 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
3807 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar07d07852009-10-18 21:17:35 +00003808 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
3809 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnera299f2c2010-04-17 18:26:20 +00003810 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00003811 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnera299f2c2010-04-17 18:26:20 +00003812 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanian9adb2e62010-06-21 22:05:18 +00003813 }
3814
3815 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
3816 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
3817 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
3818 }
Chris Lattnera299f2c2010-04-17 18:26:20 +00003819
Daniel Dunbard027a922009-09-07 00:20:42 +00003820 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbarc61d0e92008-08-25 06:02:07 +00003821 }
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003822}
3823
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003824CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003825 : CGObjCCommonMac(cgm),
Mike Stump11289f42009-09-09 15:08:12 +00003826 ObjCTypes(cgm) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00003827 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003828 ObjCABI = 2;
3829}
3830
Daniel Dunbar3ad53482008-08-11 21:35:06 +00003831/* *** */
3832
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003833ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00003834 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003835 CodeGen::CodeGenTypes &Types = CGM.getTypes();
3836 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003837
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003838 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003839 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003840 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00003841 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramerabd5b902009-10-13 10:07:13 +00003842 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003843
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003844 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00003845 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003846 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003847
Mike Stump18bb9282009-05-16 07:57:57 +00003848 // FIXME: It would be nice to unify this with the opaque type, so that the IR
3849 // comes out a bit cleaner.
Daniel Dunbarb036db82008-08-13 03:21:16 +00003850 const llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson9793f0e2009-07-29 22:16:19 +00003851 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003852
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003853 // I'm not sure I like this. The implicit coordination is a bit
3854 // gross. We should solve this in a reasonable fashion because this
3855 // is a pretty common task (match some runtime data structure with
3856 // an LLVM data structure).
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003857
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003858 // FIXME: This is leaked.
3859 // FIXME: Merge with rewriter code?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003860
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003861 // struct _objc_super {
3862 // id self;
3863 // Class cls;
3864 // }
Abramo Bagnara6150c882010-05-11 21:36:43 +00003865 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00003866 Ctx.getTranslationUnitDecl(),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003867 SourceLocation(),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003868 &Ctx.Idents.get("_objc_super"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003869 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00003870 Ctx.getObjCIdType(), 0, 0, false));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003871 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00003872 Ctx.getObjCClassType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00003873 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003874
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003875 SuperCTy = Ctx.getTagDeclType(RD);
3876 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003877
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003878 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003879 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
3880
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003881 // struct _prop_t {
3882 // char *name;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003883 // char *attributes;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003884 // }
Owen Anderson758428f2009-08-05 23:18:46 +00003885 PropertyTy = llvm::StructType::get(VMContext, Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003886 CGM.getModule().addTypeName("struct._prop_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003887 PropertyTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003888
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003889 // struct _prop_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00003890 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003891 // uint32_t count_of_properties;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00003892 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003893 // }
Owen Anderson758428f2009-08-05 23:18:46 +00003894 PropertyListTy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003895 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00003896 llvm::ArrayType::get(PropertyTy, 0),
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003897 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003898 CGM.getModule().addTypeName("struct._prop_list_t",
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003899 PropertyListTy);
3900 // struct _prop_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00003901 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003902
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003903 // struct _objc_method {
3904 // SEL _cmd;
3905 // char *method_type;
3906 // char *_imp;
3907 // }
Owen Anderson758428f2009-08-05 23:18:46 +00003908 MethodTy = llvm::StructType::get(VMContext, SelectorPtrTy,
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00003909 Int8PtrTy,
3910 Int8PtrTy,
3911 NULL);
3912 CGM.getModule().addTypeName("struct._objc_method", MethodTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003913
Fariborz Jahanian0232c052009-01-23 01:46:23 +00003914 // struct _objc_cache *
Owen Andersonc36edfe2009-08-13 23:27:53 +00003915 CacheTy = llvm::OpaqueType::get(VMContext);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00003916 CGM.getModule().addTypeName("struct._objc_cache", CacheTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00003917 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003918}
Daniel Dunbar7bd00bd2008-08-12 06:48:42 +00003919
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003920ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00003921 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003922 // struct _objc_method_description {
3923 // SEL name;
3924 // char *types;
3925 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003926 MethodDescriptionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00003927 llvm::StructType::get(VMContext, SelectorPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003928 Int8PtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00003929 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003930 CGM.getModule().addTypeName("struct._objc_method_description",
Daniel Dunbarb036db82008-08-13 03:21:16 +00003931 MethodDescriptionTy);
3932
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003933 // struct _objc_method_description_list {
3934 // int count;
3935 // struct _objc_method_description[1];
3936 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003937 MethodDescriptionListTy =
Owen Anderson758428f2009-08-05 23:18:46 +00003938 llvm::StructType::get(VMContext, IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00003939 llvm::ArrayType::get(MethodDescriptionTy, 0),
Daniel Dunbarb036db82008-08-13 03:21:16 +00003940 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003941 CGM.getModule().addTypeName("struct._objc_method_description_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00003942 MethodDescriptionListTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003943
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003944 // struct _objc_method_description_list *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003945 MethodDescriptionListPtrTy =
Owen Anderson9793f0e2009-07-29 22:16:19 +00003946 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003947
Daniel Dunbarb036db82008-08-13 03:21:16 +00003948 // Protocol description structures
3949
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003950 // struct _objc_protocol_extension {
3951 // uint32_t size; // sizeof(struct _objc_protocol_extension)
3952 // struct _objc_method_description_list *optional_instance_methods;
3953 // struct _objc_method_description_list *optional_class_methods;
3954 // struct _objc_property_list *instance_properties;
3955 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003956 ProtocolExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00003957 llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003958 MethodDescriptionListPtrTy,
3959 MethodDescriptionListPtrTy,
Daniel Dunbarb036db82008-08-13 03:21:16 +00003960 PropertyListPtrTy,
3961 NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003962 CGM.getModule().addTypeName("struct._objc_protocol_extension",
Daniel Dunbarb036db82008-08-13 03:21:16 +00003963 ProtocolExtensionTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003964
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003965 // struct _objc_protocol_extension *
Owen Anderson9793f0e2009-07-29 22:16:19 +00003966 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003967
Daniel Dunbarc475d422008-10-29 22:36:39 +00003968 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbarb036db82008-08-13 03:21:16 +00003969
Owen Andersonc36edfe2009-08-13 23:27:53 +00003970 llvm::PATypeHolder ProtocolTyHolder = llvm::OpaqueType::get(VMContext);
3971 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003972
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003973 const llvm::Type *T =
Mike Stump11289f42009-09-09 15:08:12 +00003974 llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00003975 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003976 LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00003977 llvm::ArrayType::get(ProtocolTyHolder, 0),
Fariborz Jahanian279eda62009-01-21 22:04:16 +00003978 NULL);
Daniel Dunbarb036db82008-08-13 03:21:16 +00003979 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(T);
3980
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003981 // struct _objc_protocol {
3982 // struct _objc_protocol_extension *isa;
3983 // char *protocol_name;
3984 // struct _objc_protocol **_objc_protocol_list;
3985 // struct _objc_method_description_list *instance_methods;
3986 // struct _objc_method_description_list *class_methods;
3987 // }
Owen Anderson758428f2009-08-05 23:18:46 +00003988 T = llvm::StructType::get(VMContext, ProtocolExtensionPtrTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00003989 Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003990 llvm::PointerType::getUnqual(ProtocolListTyHolder),
Daniel Dunbarb036db82008-08-13 03:21:16 +00003991 MethodDescriptionListPtrTy,
3992 MethodDescriptionListPtrTy,
3993 NULL);
3994 cast<llvm::OpaqueType>(ProtocolTyHolder.get())->refineAbstractTypeTo(T);
3995
3996 ProtocolListTy = cast<llvm::StructType>(ProtocolListTyHolder.get());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00003997 CGM.getModule().addTypeName("struct._objc_protocol_list",
Daniel Dunbarb036db82008-08-13 03:21:16 +00003998 ProtocolListTy);
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00003999 // struct _objc_protocol_list *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004000 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbarb036db82008-08-13 03:21:16 +00004001
4002 ProtocolTy = cast<llvm::StructType>(ProtocolTyHolder.get());
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004003 CGM.getModule().addTypeName("struct._objc_protocol", ProtocolTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004004 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004005
4006 // Class description structures
4007
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004008 // struct _objc_ivar {
4009 // char *ivar_name;
4010 // char *ivar_type;
4011 // int ivar_offset;
4012 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004013 IvarTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004014 Int8PtrTy,
4015 IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004016 NULL);
4017 CGM.getModule().addTypeName("struct._objc_ivar", IvarTy);
4018
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004019 // struct _objc_ivar_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004020 IvarListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004021 CGM.getModule().addTypeName("struct._objc_ivar_list", IvarListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004022 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004023
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004024 // struct _objc_method_list *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004025 MethodListTy = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004026 CGM.getModule().addTypeName("struct._objc_method_list", MethodListTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004027 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004028
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004029 // struct _objc_class_extension *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004030 ClassExtensionTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004031 llvm::StructType::get(VMContext, IntTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004032 Int8PtrTy,
4033 PropertyListPtrTy,
4034 NULL);
4035 CGM.getModule().addTypeName("struct._objc_class_extension", ClassExtensionTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004036 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004037
Owen Andersonc36edfe2009-08-13 23:27:53 +00004038 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004039
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004040 // struct _objc_class {
4041 // Class isa;
4042 // Class super_class;
4043 // char *name;
4044 // long version;
4045 // long info;
4046 // long instance_size;
4047 // struct _objc_ivar_list *ivars;
4048 // struct _objc_method_list *methods;
4049 // struct _objc_cache *cache;
4050 // struct _objc_protocol_list *protocols;
4051 // char *ivar_layout;
4052 // struct _objc_class_ext *ext;
4053 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004054 T = llvm::StructType::get(VMContext,
4055 llvm::PointerType::getUnqual(ClassTyHolder),
Owen Anderson9793f0e2009-07-29 22:16:19 +00004056 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004057 Int8PtrTy,
4058 LongTy,
4059 LongTy,
4060 LongTy,
4061 IvarListPtrTy,
4062 MethodListPtrTy,
4063 CachePtrTy,
4064 ProtocolListPtrTy,
4065 Int8PtrTy,
4066 ClassExtensionPtrTy,
4067 NULL);
4068 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(T);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004069
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004070 ClassTy = cast<llvm::StructType>(ClassTyHolder.get());
4071 CGM.getModule().addTypeName("struct._objc_class", ClassTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004072 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004073
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004074 // struct _objc_category {
4075 // char *category_name;
4076 // char *class_name;
4077 // struct _objc_method_list *instance_method;
4078 // struct _objc_method_list *class_method;
4079 // uint32_t size; // sizeof(struct _objc_category)
4080 // struct _objc_property_list *instance_properties;// category's @property
4081 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004082 CategoryTy = llvm::StructType::get(VMContext, Int8PtrTy,
Daniel Dunbar938a77f2008-08-22 20:34:54 +00004083 Int8PtrTy,
4084 MethodListPtrTy,
4085 MethodListPtrTy,
4086 ProtocolListPtrTy,
4087 IntTy,
4088 PropertyListPtrTy,
4089 NULL);
4090 CGM.getModule().addTypeName("struct._objc_category", CategoryTy);
4091
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004092 // Global metadata structures
4093
Fariborz Jahanian4b4c8262009-01-21 00:39:53 +00004094 // struct _objc_symtab {
4095 // long sel_ref_cnt;
4096 // SEL *refs;
4097 // short cls_def_cnt;
4098 // short cat_def_cnt;
4099 // char *defs[cls_def_cnt + cat_def_cnt];
4100 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004101 SymtabTy = llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004102 SelectorPtrTy,
4103 ShortTy,
4104 ShortTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004105 llvm::ArrayType::get(Int8PtrTy, 0),
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004106 NULL);
4107 CGM.getModule().addTypeName("struct._objc_symtab", SymtabTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004108 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004109
Fariborz Jahanianeee54df2009-01-22 00:37:21 +00004110 // struct _objc_module {
4111 // long version;
4112 // long size; // sizeof(struct _objc_module)
4113 // char *name;
4114 // struct _objc_symtab* symtab;
4115 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004116 ModuleTy =
Owen Anderson758428f2009-08-05 23:18:46 +00004117 llvm::StructType::get(VMContext, LongTy,
Daniel Dunbar22d82ed2008-08-21 04:36:09 +00004118 LongTy,
4119 Int8PtrTy,
4120 SymtabPtrTy,
4121 NULL);
4122 CGM.getModule().addTypeName("struct._objc_module", ModuleTy);
Daniel Dunbar97ff50d2008-08-23 09:25:55 +00004123
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004124
Mike Stump18bb9282009-05-16 07:57:57 +00004125 // FIXME: This is the size of the setjmp buffer and should be target
4126 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson9ff22482008-09-09 10:10:21 +00004127 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004128
Anders Carlsson9ff22482008-09-09 10:10:21 +00004129 // Exceptions
Owen Anderson9793f0e2009-07-29 22:16:19 +00004130 const llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramerabd5b902009-10-13 10:07:13 +00004131 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004132
4133 ExceptionDataTy =
Chris Lattner5e016ae2010-06-27 07:15:29 +00004134 llvm::StructType::get(VMContext,
4135 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4136 SetJmpBufferSize),
Anders Carlsson9ff22482008-09-09 10:10:21 +00004137 StackPtrTy, NULL);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004138 CGM.getModule().addTypeName("struct._objc_exception_data",
Anders Carlsson9ff22482008-09-09 10:10:21 +00004139 ExceptionDataTy);
4140
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004141}
4142
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004143ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump11289f42009-09-09 15:08:12 +00004144 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004145 // struct _method_list_t {
4146 // uint32_t entsize; // sizeof(struct _objc_method)
4147 // uint32_t method_count;
4148 // struct _objc_method method_list[method_count];
4149 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004150 MethodListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004151 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004152 llvm::ArrayType::get(MethodTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004153 NULL);
4154 CGM.getModule().addTypeName("struct.__method_list_t",
4155 MethodListnfABITy);
4156 // struct method_list_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004157 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004158
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004159 // struct _protocol_t {
4160 // id isa; // NULL
4161 // const char * const protocol_name;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004162 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004163 // const struct method_list_t * const instance_methods;
4164 // const struct method_list_t * const class_methods;
4165 // const struct method_list_t *optionalInstanceMethods;
4166 // const struct method_list_t *optionalClassMethods;
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004167 // const struct _prop_list_t * properties;
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004168 // const uint32_t size; // sizeof(struct _protocol_t)
4169 // const uint32_t flags; // = 0
4170 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004171
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004172 // Holder for struct _protocol_list_t *
Owen Andersonc36edfe2009-08-13 23:27:53 +00004173 llvm::PATypeHolder ProtocolListTyHolder = llvm::OpaqueType::get(VMContext);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004174
Owen Anderson758428f2009-08-05 23:18:46 +00004175 ProtocolnfABITy = llvm::StructType::get(VMContext, ObjectPtrTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004176 Int8PtrTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004177 llvm::PointerType::getUnqual(
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004178 ProtocolListTyHolder),
4179 MethodListnfABIPtrTy,
4180 MethodListnfABIPtrTy,
4181 MethodListnfABIPtrTy,
4182 MethodListnfABIPtrTy,
4183 PropertyListPtrTy,
4184 IntTy,
4185 IntTy,
4186 NULL);
4187 CGM.getModule().addTypeName("struct._protocol_t",
4188 ProtocolnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004189
4190 // struct _protocol_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004191 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004192
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004193 // struct _protocol_list_t {
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004194 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004195 // struct _protocol_t *[protocol_count];
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004196 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004197 ProtocolListnfABITy = llvm::StructType::get(VMContext, LongTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004198 llvm::ArrayType::get(
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004199 ProtocolnfABIPtrTy, 0),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004200 NULL);
4201 CGM.getModule().addTypeName("struct._objc_protocol_list",
4202 ProtocolListnfABITy);
Daniel Dunbar8de90f02009-02-15 07:36:20 +00004203 cast<llvm::OpaqueType>(ProtocolListTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004204 ProtocolListnfABITy);
4205
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004206 // struct _objc_protocol_list*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004207 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004208
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004209 // struct _ivar_t {
4210 // unsigned long int *offset; // pointer to ivar offset location
4211 // char *name;
4212 // char *type;
4213 // uint32_t alignment;
4214 // uint32_t size;
4215 // }
Mike Stump11289f42009-09-09 15:08:12 +00004216 IvarnfABITy = llvm::StructType::get(VMContext,
Owen Anderson758428f2009-08-05 23:18:46 +00004217 llvm::PointerType::getUnqual(LongTy),
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004218 Int8PtrTy,
4219 Int8PtrTy,
4220 IntTy,
4221 IntTy,
4222 NULL);
4223 CGM.getModule().addTypeName("struct._ivar_t", IvarnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004224
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004225 // struct _ivar_list_t {
4226 // uint32 entsize; // sizeof(struct _ivar_t)
4227 // uint32 count;
4228 // struct _iver_t list[count];
4229 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004230 IvarListnfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004231 IntTy,
Owen Anderson9793f0e2009-07-29 22:16:19 +00004232 llvm::ArrayType::get(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004233 IvarnfABITy, 0),
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004234 NULL);
4235 CGM.getModule().addTypeName("struct._ivar_list_t", IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004236
Owen Anderson9793f0e2009-07-29 22:16:19 +00004237 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004238
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004239 // struct _class_ro_t {
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00004240 // uint32_t const flags;
4241 // uint32_t const instanceStart;
4242 // uint32_t const instanceSize;
4243 // uint32_t const reserved; // only when building for 64bit targets
4244 // const uint8_t * const ivarLayout;
4245 // const char *const name;
4246 // const struct _method_list_t * const baseMethods;
4247 // const struct _objc_protocol_list *const baseProtocols;
4248 // const struct _ivar_list_t *const ivars;
4249 // const uint8_t * const weakIvarLayout;
4250 // const struct _prop_list_t * const properties;
4251 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004252
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004253 // FIXME. Add 'reserved' field in 64bit abi mode!
Owen Anderson758428f2009-08-05 23:18:46 +00004254 ClassRonfABITy = llvm::StructType::get(VMContext, IntTy,
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004255 IntTy,
4256 IntTy,
4257 Int8PtrTy,
4258 Int8PtrTy,
4259 MethodListnfABIPtrTy,
4260 ProtocolListnfABIPtrTy,
4261 IvarListnfABIPtrTy,
4262 Int8PtrTy,
4263 PropertyListPtrTy,
4264 NULL);
4265 CGM.getModule().addTypeName("struct._class_ro_t",
4266 ClassRonfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004267
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004268 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
4269 std::vector<const llvm::Type*> Params;
4270 Params.push_back(ObjectPtrTy);
4271 Params.push_back(SelectorPtrTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004272 ImpnfABITy = llvm::PointerType::getUnqual(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004273 llvm::FunctionType::get(ObjectPtrTy, Params, false));
4274
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004275 // struct _class_t {
4276 // struct _class_t *isa;
4277 // struct _class_t * const superclass;
4278 // void *cache;
4279 // IMP *vtable;
4280 // struct class_ro_t *ro;
4281 // }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004282
Owen Andersonc36edfe2009-08-13 23:27:53 +00004283 llvm::PATypeHolder ClassTyHolder = llvm::OpaqueType::get(VMContext);
Owen Anderson170229f2009-07-14 23:10:40 +00004284 ClassnfABITy =
Owen Anderson758428f2009-08-05 23:18:46 +00004285 llvm::StructType::get(VMContext,
4286 llvm::PointerType::getUnqual(ClassTyHolder),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004287 llvm::PointerType::getUnqual(ClassTyHolder),
4288 CachePtrTy,
4289 llvm::PointerType::getUnqual(ImpnfABITy),
4290 llvm::PointerType::getUnqual(ClassRonfABITy),
4291 NULL);
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004292 CGM.getModule().addTypeName("struct._class_t", ClassnfABITy);
4293
4294 cast<llvm::OpaqueType>(ClassTyHolder.get())->refineAbstractTypeTo(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004295 ClassnfABITy);
4296
Fariborz Jahanian71394042009-01-23 23:53:38 +00004297 // LLVM for struct _class_t *
Owen Anderson9793f0e2009-07-29 22:16:19 +00004298 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004299
Fariborz Jahanian0232c052009-01-23 01:46:23 +00004300 // struct _category_t {
4301 // const char * const name;
4302 // struct _class_t *const cls;
4303 // const struct _method_list_t * const instance_methods;
4304 // const struct _method_list_t * const class_methods;
4305 // const struct _protocol_list_t * const protocols;
4306 // const struct _prop_list_t * const properties;
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004307 // }
Owen Anderson758428f2009-08-05 23:18:46 +00004308 CategorynfABITy = llvm::StructType::get(VMContext, Int8PtrTy,
Fariborz Jahanian71394042009-01-23 23:53:38 +00004309 ClassnfABIPtrTy,
Fariborz Jahanian5a63e4c2009-01-23 17:41:22 +00004310 MethodListnfABIPtrTy,
4311 MethodListnfABIPtrTy,
4312 ProtocolListnfABIPtrTy,
4313 PropertyListPtrTy,
4314 NULL);
4315 CGM.getModule().addTypeName("struct._category_t", CategorynfABITy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004316
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004317 // New types for nonfragile abi messaging.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004318 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4319 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004320
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004321 // MessageRefTy - LLVM for:
4322 // struct _message_ref_t {
4323 // IMP messenger;
4324 // SEL name;
4325 // };
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004326
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004327 // First the clang type for struct _message_ref_t
Abramo Bagnara6150c882010-05-11 21:36:43 +00004328 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbar0c005372010-04-29 16:29:11 +00004329 Ctx.getTranslationUnitDecl(),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004330 SourceLocation(),
4331 &Ctx.Idents.get("_message_ref_t"));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004332 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004333 Ctx.VoidPtrTy, 0, 0, false));
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004334 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), 0,
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00004335 Ctx.getObjCSelType(), 0, 0, false));
Douglas Gregord5058122010-02-11 01:19:42 +00004336 RD->completeDefinition();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004337
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00004338 MessageRefCTy = Ctx.getTagDeclType(RD);
4339 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4340 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004341
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004342 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson9793f0e2009-07-29 22:16:19 +00004343 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004344
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004345 // SuperMessageRefTy - LLVM for:
4346 // struct _super_message_ref_t {
4347 // SUPER_IMP messenger;
4348 // SEL name;
4349 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004350 SuperMessageRefTy = llvm::StructType::get(VMContext, ImpnfABITy,
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004351 SelectorPtrTy,
4352 NULL);
4353 CGM.getModule().addTypeName("struct._super_message_ref_t", SuperMessageRefTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004354
Fariborz Jahanian82c72e12009-02-03 23:49:23 +00004355 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004356 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
4357
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004358
4359 // struct objc_typeinfo {
4360 // const void** vtable; // objc_ehtype_vtable + 2
4361 // const char* name; // c++ typeinfo string
4362 // Class cls;
4363 // };
Owen Anderson758428f2009-08-05 23:18:46 +00004364 EHTypeTy = llvm::StructType::get(VMContext,
4365 llvm::PointerType::getUnqual(Int8PtrTy),
Daniel Dunbarb1559a42009-03-01 04:46:24 +00004366 Int8PtrTy,
4367 ClassnfABIPtrTy,
4368 NULL);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00004369 CGM.getModule().addTypeName("struct._objc_typeinfo", EHTypeTy);
Owen Anderson9793f0e2009-07-29 22:16:19 +00004370 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00004371}
4372
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004373llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanian71394042009-01-23 23:53:38 +00004374 FinishNonFragileABIModule();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004375
Fariborz Jahanian71394042009-01-23 23:53:38 +00004376 return NULL;
4377}
4378
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004379void CGObjCNonFragileABIMac::AddModuleClassList(const
4380 std::vector<llvm::GlobalValue*>
4381 &Container,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004382 const char *SymbolName,
4383 const char *SectionName) {
4384 unsigned NumClasses = Container.size();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004385
Daniel Dunbar19573e72009-05-15 21:48:48 +00004386 if (!NumClasses)
4387 return;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004388
Daniel Dunbar19573e72009-05-15 21:48:48 +00004389 std::vector<llvm::Constant*> Symbols(NumClasses);
4390 for (unsigned i=0; i<NumClasses; i++)
Owen Andersonade90fd2009-07-29 18:54:39 +00004391 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar19573e72009-05-15 21:48:48 +00004392 ObjCTypes.Int8PtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004393 llvm::Constant* Init =
Owen Anderson9793f0e2009-07-29 22:16:19 +00004394 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004395 NumClasses),
4396 Symbols);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004397
Daniel Dunbar19573e72009-05-15 21:48:48 +00004398 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004399 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004400 llvm::GlobalValue::InternalLinkage,
4401 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004402 SymbolName);
Daniel Dunbar710cb202010-04-25 20:39:32 +00004403 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar19573e72009-05-15 21:48:48 +00004404 GV->setSection(SectionName);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004405 CGM.AddUsedGlobal(GV);
Daniel Dunbar19573e72009-05-15 21:48:48 +00004406}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004407
Fariborz Jahanian71394042009-01-23 23:53:38 +00004408void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4409 // nonfragile abi has no module definition.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004410
Daniel Dunbar19573e72009-05-15 21:48:48 +00004411 // Build list of all implemented class addresses in array
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004412 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004413 AddModuleClassList(DefinedClasses,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004414 "\01L_OBJC_LABEL_CLASS_$",
4415 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahanian67260552009-11-17 21:37:35 +00004416
Fariborz Jahanian67260552009-11-17 21:37:35 +00004417 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4418 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4419 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4420 continue;
4421 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004422 }
4423
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004424 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4425 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4426 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4427 continue;
4428 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4429 }
Fariborz Jahanian67260552009-11-17 21:37:35 +00004430
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004431 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004432 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4433 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004434
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004435 // Build list of all implemented category addresses in array
4436 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004437 AddModuleClassList(DefinedCategories,
Daniel Dunbar19573e72009-05-15 21:48:48 +00004438 "\01L_OBJC_LABEL_CATEGORY_$",
4439 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004440 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004441 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4442 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004443
Daniel Dunbar5e639272010-04-25 20:39:01 +00004444 EmitImageInfo();
Fariborz Jahanian71394042009-01-23 23:53:38 +00004445}
4446
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004447/// LegacyDispatchedSelector - Returns true if SEL is not in the list of
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004448/// NonLegacyDispatchMethods; false otherwise. What this means is that
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004449/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004450/// message dispatch call for all the rest.
4451///
4452bool CGObjCNonFragileABIMac::LegacyDispatchedSelector(Selector Sel) {
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004453 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4454 default:
4455 assert(0 && "Invalid dispatch method!");
4456 case CodeGenOptions::Legacy:
Daniel Dunbarca5e3eb2010-02-01 21:07:33 +00004457 return true;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004458 case CodeGenOptions::NonLegacy:
Fariborz Jahaniandfb39832010-04-19 17:53:30 +00004459 return false;
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004460 case CodeGenOptions::Mixed:
4461 break;
4462 }
4463
4464 // If so, see whether this selector is in the white-list of things which must
4465 // use the new dispatch convention. We lazily build a dense set for this.
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004466 if (NonLegacyDispatchMethods.empty()) {
4467 NonLegacyDispatchMethods.insert(GetNullarySelector("alloc"));
4468 NonLegacyDispatchMethods.insert(GetNullarySelector("class"));
4469 NonLegacyDispatchMethods.insert(GetNullarySelector("self"));
4470 NonLegacyDispatchMethods.insert(GetNullarySelector("isFlipped"));
4471 NonLegacyDispatchMethods.insert(GetNullarySelector("length"));
4472 NonLegacyDispatchMethods.insert(GetNullarySelector("count"));
4473 NonLegacyDispatchMethods.insert(GetNullarySelector("retain"));
4474 NonLegacyDispatchMethods.insert(GetNullarySelector("release"));
4475 NonLegacyDispatchMethods.insert(GetNullarySelector("autorelease"));
4476 NonLegacyDispatchMethods.insert(GetNullarySelector("hash"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004477
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004478 NonLegacyDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4479 NonLegacyDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4480 NonLegacyDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4481 NonLegacyDispatchMethods.insert(GetUnarySelector("objectForKey"));
4482 NonLegacyDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4483 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4484 NonLegacyDispatchMethods.insert(GetUnarySelector("isEqual"));
4485 NonLegacyDispatchMethods.insert(GetUnarySelector("addObject"));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004486 // "countByEnumeratingWithState:objects:count"
Fariborz Jahanian18801362009-05-13 16:19:02 +00004487 IdentifierInfo *KeyIdents[] = {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004488 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4489 &CGM.getContext().Idents.get("objects"),
4490 &CGM.getContext().Idents.get("count")
Fariborz Jahanian18801362009-05-13 16:19:02 +00004491 };
4492 NonLegacyDispatchMethods.insert(
4493 CGM.getContext().Selectors.getSelector(3, KeyIdents));
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004494 }
Daniel Dunbarfca18c1b42010-04-24 17:56:46 +00004495
Fariborz Jahaniane4128642009-05-12 20:06:41 +00004496 return (NonLegacyDispatchMethods.count(Sel) == 0);
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00004497}
4498
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004499// Metadata flags
4500enum MetaDataDlags {
4501 CLS = 0x0,
4502 CLS_META = 0x1,
4503 CLS_ROOT = 0x2,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004504 OBJC2_CLS_HIDDEN = 0x10,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004505 CLS_EXCEPTION = 0x20
4506};
4507/// BuildClassRoTInitializer - generate meta-data for:
4508/// struct _class_ro_t {
4509/// uint32_t const flags;
4510/// uint32_t const instanceStart;
4511/// uint32_t const instanceSize;
4512/// uint32_t const reserved; // only when building for 64bit targets
4513/// const uint8_t * const ivarLayout;
4514/// const char *const name;
4515/// const struct _method_list_t * const baseMethods;
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004516/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004517/// const struct _ivar_list_t *const ivars;
4518/// const uint8_t * const weakIvarLayout;
4519/// const struct _prop_list_t * const properties;
4520/// }
4521///
4522llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004523 unsigned flags,
4524 unsigned InstanceStart,
4525 unsigned InstanceSize,
4526 const ObjCImplementationDecl *ID) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004527 std::string ClassName = ID->getNameAsString();
4528 std::vector<llvm::Constant*> Values(10); // 11 for 64bit targets!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004529 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4530 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4531 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004532 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004533 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4534 : BuildIvarLayout(ID, true);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004535 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004536 // const struct _method_list_t * const baseMethods;
4537 std::vector<llvm::Constant*> Methods;
4538 std::string MethodListName("\01l_OBJC_$_");
4539 if (flags & CLS_META) {
4540 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004541 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004542 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004543 // Class methods should always be defined.
4544 Methods.push_back(GetMethodConstant(*i));
4545 }
4546 } else {
4547 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004548 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004549 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004550 // Instance methods should always be defined.
4551 Methods.push_back(GetMethodConstant(*i));
4552 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004553 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004554 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004555 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004556
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004557 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4558 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004559
Fariborz Jahaniand27a8202009-01-28 22:46:49 +00004560 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4561 if (llvm::Constant *C = GetMethodConstant(MD))
4562 Methods.push_back(C);
4563 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4564 if (llvm::Constant *C = GetMethodConstant(MD))
4565 Methods.push_back(C);
4566 }
4567 }
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004568 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004569 Values[ 5] = EmitMethodList(MethodListName,
4570 "__DATA, __objc_const", Methods);
4571
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004572 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4573 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004574 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004575 + OID->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00004576 OID->protocol_begin(),
4577 OID->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004578
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004579 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004580 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004581 else
4582 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004583 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4584 : BuildIvarLayout(ID, false);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004585 if (flags & CLS_META)
Owen Anderson0b75f232009-07-31 20:28:54 +00004586 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004587 else
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004588 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4589 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004590 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004591 Values);
4592 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004593 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4594 llvm::GlobalValue::InternalLinkage,
4595 Init,
4596 (flags & CLS_META) ?
4597 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4598 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004599 CLASS_RO_GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004600 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004601 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004602 return CLASS_RO_GV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004603
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004604}
4605
4606/// BuildClassMetaData - This routine defines that to-level meta-data
4607/// for the given ClassName for:
4608/// struct _class_t {
4609/// struct _class_t *isa;
4610/// struct _class_t * const superclass;
4611/// void *cache;
4612/// IMP *vtable;
4613/// struct class_ro_t *ro;
4614/// }
4615///
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004616llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004617 std::string &ClassName,
4618 llvm::Constant *IsAGV,
4619 llvm::Constant *SuperClassGV,
4620 llvm::Constant *ClassRoGV,
4621 bool HiddenVisibility) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004622 std::vector<llvm::Constant*> Values(5);
4623 Values[0] = IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004624 Values[1] = SuperClassGV;
4625 if (!Values[1])
4626 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004627 Values[2] = ObjCEmptyCacheVar; // &ObjCEmptyCacheVar
4628 Values[3] = ObjCEmptyVtableVar; // &ObjCEmptyVtableVar
4629 Values[4] = ClassRoGV; // &CLASS_RO_GV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004630 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004631 Values);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004632 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4633 GV->setInitializer(Init);
Fariborz Jahanian04087232009-01-31 01:07:39 +00004634 GV->setSection("__DATA, __objc_data");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004635 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004636 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahanian82208252009-01-31 00:59:10 +00004637 if (HiddenVisibility)
4638 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004639 return GV;
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004640}
4641
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004642bool
Fariborz Jahaniana6bed832009-05-21 01:03:45 +00004643CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004644 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004645}
4646
Daniel Dunbar961202372009-05-03 12:57:56 +00004647void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbar554fd792009-04-19 23:41:48 +00004648 uint32_t &InstanceStart,
4649 uint32_t &InstanceSize) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004650 const ASTRecordLayout &RL =
Daniel Dunbar9252ee12009-05-04 21:26:30 +00004651 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004652
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004653 // InstanceSize is really instance end.
Anders Carlsson27b50132009-07-18 21:26:44 +00004654 InstanceSize = llvm::RoundUpToAlignment(RL.getDataSize(), 8) / 8;
Daniel Dunbar9b042e02009-05-04 23:23:09 +00004655
4656 // If there are no fields, the start is the same as the end.
4657 if (!RL.getFieldCount())
4658 InstanceStart = InstanceSize;
4659 else
4660 InstanceStart = RL.getFieldOffset(0) / 8;
Daniel Dunbar554fd792009-04-19 23:41:48 +00004661}
4662
Fariborz Jahanian71394042009-01-23 23:53:38 +00004663void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4664 std::string ClassName = ID->getNameAsString();
4665 if (!ObjCEmptyCacheVar) {
4666 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004667 CGM.getModule(),
4668 ObjCTypes.CacheTy,
4669 false,
4670 llvm::GlobalValue::ExternalLinkage,
4671 0,
4672 "_objc_empty_cache");
4673
Fariborz Jahanian71394042009-01-23 23:53:38 +00004674 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004675 CGM.getModule(),
4676 ObjCTypes.ImpnfABITy,
4677 false,
4678 llvm::GlobalValue::ExternalLinkage,
4679 0,
4680 "_objc_empty_vtable");
Fariborz Jahanian71394042009-01-23 23:53:38 +00004681 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004682 assert(ID->getClassInterface() &&
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004683 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbare3f5cfc2009-04-20 20:18:54 +00004684 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004685 uint32_t InstanceStart =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004686 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004687 uint32_t InstanceSize = InstanceStart;
4688 uint32_t flags = CLS_META;
Daniel Dunbar15894b72009-04-07 05:48:37 +00004689 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4690 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004691
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004692 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004693
4694 bool classIsHidden =
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00004695 CGM.getDeclVisibilityMode(ID->getClassInterface()) == LangOptions::Hidden;
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;
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004700 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004701 // class is root
4702 flags |= CLS_ROOT;
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004703 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004704 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004705 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004706 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004707 const ObjCInterfaceDecl *Root = ID->getClassInterface();
4708 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
4709 Root = Super;
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004710 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004711 if (Root->hasAttr<WeakImportAttr>())
4712 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004713 // work on super class metadata symbol.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004714 std::string SuperClassName =
Fariborz Jahaniana6227fd2009-12-01 18:25:24 +00004715 ObjCMetaClassName +
4716 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00004717 SuperClassGV = GetClassGlobal(SuperClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004718 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4719 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian9e3ad522009-01-24 20:21:50 +00004720 }
4721 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
4722 InstanceStart,
4723 InstanceSize,ID);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004724 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004725 llvm::GlobalVariable *MetaTClass =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004726 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
4727 classIsHidden);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004728 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar15894b72009-04-07 05:48:37 +00004729
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004730 // Metadata for the class
4731 flags = CLS;
Fariborz Jahanian82208252009-01-31 00:59:10 +00004732 if (classIsHidden)
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004733 flags |= OBJC2_CLS_HIDDEN;
Fariborz Jahanian0dec1e02010-04-28 21:28:56 +00004734 if (ID->getNumIvarInitializers())
4735 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004736
Douglas Gregor78bd61f2009-06-18 16:11:24 +00004737 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004738 flags |= CLS_EXCEPTION;
4739
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004740 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004741 flags |= CLS_ROOT;
4742 SuperClassGV = 0;
Chris Lattnerb433b272009-04-19 06:02:28 +00004743 } else {
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004744 // Has a root. Current class is not a root.
Fariborz Jahanian03b300b2009-02-26 18:23:47 +00004745 std::string RootClassName =
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004746 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004747 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Fariborz Jahanian67260552009-11-17 21:37:35 +00004748 if (ID->getClassInterface()->getSuperClass()->hasAttr<WeakImportAttr>())
4749 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004750 }
Daniel Dunbar961202372009-05-03 12:57:56 +00004751 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004752 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004753 InstanceStart,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004754 InstanceSize,
Fariborz Jahaniana887e632009-01-24 23:43:01 +00004755 ID);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004756
Fariborz Jahanian4723fb72009-01-24 21:21:53 +00004757 TClassName = ObjCClassName + ClassName;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004758 llvm::GlobalVariable *ClassMD =
Fariborz Jahanian82208252009-01-31 00:59:10 +00004759 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
4760 classIsHidden);
Fariborz Jahanian279abd32009-01-30 20:55:31 +00004761 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004762
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004763 // Determine if this class is also "non-lazy".
4764 if (ImplementationIsNonLazy(ID))
4765 DefinedNonLazyClasses.push_back(ClassMD);
4766
Daniel Dunbar8f28d012009-04-08 04:21:03 +00004767 // Force the definition of the EHType if necessary.
4768 if (flags & CLS_EXCEPTION)
4769 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian71394042009-01-23 23:53:38 +00004770}
4771
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004772/// GenerateProtocolRef - This routine is called to generate code for
4773/// a protocol reference expression; as in:
4774/// @code
4775/// @protocol(Proto1);
4776/// @endcode
4777/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
4778/// which will hold address of the protocol meta-data.
4779///
4780llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004781 const ObjCProtocolDecl *PD) {
4782
Fariborz Jahanian464423d2009-04-10 18:47:34 +00004783 // This routine is called for @protocol only. So, we must build definition
4784 // of protocol's meta-data (not a reference to it!)
4785 //
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004786 llvm::Constant *Init =
4787 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
4788 ObjCTypes.ExternalProtocolPtrTy);
4789
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004790 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
4791 ProtocolName += PD->getNameAsCString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004792
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004793 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
4794 if (PTGV)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00004795 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004796 PTGV = new llvm::GlobalVariable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004797 CGM.getModule(),
4798 Init->getType(), false,
4799 llvm::GlobalValue::WeakAnyLinkage,
4800 Init,
4801 ProtocolName);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004802 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
4803 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004804 CGM.AddUsedGlobal(PTGV);
Daniel Dunbarc76493a2009-11-29 21:23:36 +00004805 return Builder.CreateLoad(PTGV, "tmp");
Fariborz Jahanian097feda2009-01-30 18:58:59 +00004806}
4807
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004808/// GenerateCategory - Build metadata for a category implementation.
4809/// struct _category_t {
4810/// const char * const name;
4811/// struct _class_t *const cls;
4812/// const struct _method_list_t * const instance_methods;
4813/// const struct _method_list_t * const class_methods;
4814/// const struct _protocol_list_t * const protocols;
4815/// const struct _prop_list_t * const properties;
4816/// }
4817///
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004818void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004819 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004820 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004821 std::string ExtCatName(Prefix + Interface->getNameAsString()+
4822 "_$_" + OCD->getNameAsString());
4823 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar15894b72009-04-07 05:48:37 +00004824 Interface->getNameAsString());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004825
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004826 std::vector<llvm::Constant*> Values(6);
4827 Values[0] = GetClassName(OCD->getIdentifier());
4828 // meta-class entry symbol
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00004829 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Fariborz Jahanian3ad8dcf2009-11-17 22:02:21 +00004830 if (Interface->hasAttr<WeakImportAttr>())
4831 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
4832
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004833 Values[1] = ClassGV;
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004834 std::vector<llvm::Constant*> Methods;
4835 std::string MethodListName(Prefix);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004836 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004837 "_$_" + OCD->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004838
4839 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004840 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004841 // Instance methods should always be defined.
4842 Methods.push_back(GetMethodConstant(*i));
4843 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004844
4845 Values[2] = EmitMethodList(MethodListName,
4846 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004847 Methods);
4848
4849 MethodListName = Prefix;
4850 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
4851 OCD->getNameAsString();
4852 Methods.clear();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004853 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00004854 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004855 // Class methods should always be defined.
4856 Methods.push_back(GetMethodConstant(*i));
4857 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004858
4859 Values[3] = EmitMethodList(MethodListName,
4860 "__DATA, __objc_const",
Fariborz Jahanian2612e142009-01-26 22:58:07 +00004861 Methods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004862 const ObjCCategoryDecl *Category =
Fariborz Jahanian066347e2009-01-28 22:18:42 +00004863 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00004864 if (Category) {
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004865 llvm::SmallString<256> ExtName;
4866 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
4867 << OCD->getName();
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00004868 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004869 + Interface->getName() + "_$_"
4870 + Category->getName(),
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00004871 Category->protocol_begin(),
4872 Category->protocol_end());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004873 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
4874 OCD, Category, ObjCTypes);
Mike Stump658fe022009-07-30 22:28:39 +00004875 } else {
Owen Anderson0b75f232009-07-31 20:28:54 +00004876 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
4877 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahaniand8fc1052009-02-13 17:52:22 +00004878 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004879
4880 llvm::Constant *Init =
4881 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004882 Values);
4883 llvm::GlobalVariable *GCATV
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004884 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004885 false,
4886 llvm::GlobalValue::InternalLinkage,
4887 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004888 ExtCatName);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004889 GCATV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004890 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004891 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerf56501c2009-07-17 23:57:13 +00004892 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004893 DefinedCategories.push_back(GCATV);
Daniel Dunbar9a017d72009-05-15 22:33:15 +00004894
4895 // Determine if this category is also "non-lazy".
4896 if (ImplementationIsNonLazy(OCD))
4897 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian0c8d0602009-01-26 18:32:24 +00004898}
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004899
4900/// GetMethodConstant - Return a struct objc_method constant for the
4901/// given method if it has been defined. The result is null if the
4902/// method has not been defined. The return value has type MethodPtrTy.
4903llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004904 const ObjCMethodDecl *MD) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004905 // FIXME: Use DenseMap::lookup
4906 llvm::Function *Fn = MethodDefinitions[MD];
4907 if (!Fn)
4908 return 0;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004909
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004910 std::vector<llvm::Constant*> Method(3);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004911 Method[0] =
Owen Andersonade90fd2009-07-29 18:54:39 +00004912 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004913 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004914 Method[1] = GetMethodVarType(MD);
Owen Andersonade90fd2009-07-29 18:54:39 +00004915 Method[2] = llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00004916 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004917}
4918
4919/// EmitMethodList - Build meta-data for method declarations
4920/// struct _method_list_t {
4921/// uint32_t entsize; // sizeof(struct _objc_method)
4922/// uint32_t method_count;
4923/// struct _objc_method method_list[method_count];
4924/// }
4925///
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00004926llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(llvm::Twine Name,
4927 const char *Section,
4928 const ConstantVector &Methods) {
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004929 // Return null for empty list.
4930 if (Methods.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00004931 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004932
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004933 std::vector<llvm::Constant*> Values(3);
4934 // sizeof(struct _objc_method)
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00004935 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004936 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004937 // method_count
Owen Andersonb7a2fe62009-07-24 23:12:58 +00004938 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00004939 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004940 Methods.size());
Owen Anderson47034e12009-07-28 18:33:04 +00004941 Values[2] = llvm::ConstantArray::get(AT, Methods);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00004942 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004943
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004944 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004945 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004946 llvm::GlobalValue::InternalLinkage,
4947 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004948 Name);
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004949 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004950 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004951 GV->setSection(Section);
Chris Lattnerf56501c2009-07-17 23:57:13 +00004952 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00004953 return llvm::ConstantExpr::getBitCast(GV,
Fariborz Jahanian99113fd2009-01-26 21:38:32 +00004954 ObjCTypes.MethodListnfABIPtrTy);
4955}
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004956
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00004957/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
4958/// the given ivar.
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00004959llvm::GlobalVariable *
4960CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
4961 const ObjCIvarDecl *Ivar) {
4962 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbar3f86b9c2009-05-05 00:36:57 +00004963 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregorbcced4e2009-04-09 21:40:53 +00004964 '.' + Ivar->getNameAsString();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004965 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00004966 CGM.getModule().getGlobalVariable(Name);
4967 if (!IvarOffsetGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004968 IvarOffsetGV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00004969 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00004970 false,
4971 llvm::GlobalValue::ExternalLinkage,
4972 0,
Owen Andersonc10c8d32009-07-08 19:05:04 +00004973 Name);
Fariborz Jahanian4e7ae062009-02-10 20:21:06 +00004974 return IvarOffsetGV;
4975}
4976
Daniel Dunbar8c7f9812010-04-02 21:14:02 +00004977llvm::Constant *
4978CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
4979 const ObjCIvarDecl *Ivar,
4980 unsigned long int Offset) {
Daniel Dunbarbf90b332009-04-19 00:44:02 +00004981 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00004982 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbarbf90b332009-04-19 00:44:02 +00004983 Offset));
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00004984 IvarOffsetGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00004985 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbarbf90b332009-04-19 00:44:02 +00004986
Mike Stump18bb9282009-05-16 07:57:57 +00004987 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
4988 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbarbf90b332009-04-19 00:44:02 +00004989 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
4990 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
4991 CGM.getDeclVisibilityMode(ID) == LangOptions::Hidden)
Fariborz Jahanian3d3426f2009-01-28 01:36:42 +00004992 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00004993 else
Fariborz Jahanianbc3c77b2009-04-06 18:30:00 +00004994 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004995 IvarOffsetGV->setSection("__DATA, __objc_const");
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00004996 return IvarOffsetGV;
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00004997}
4998
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00004999/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005000/// implementation. The return value has type
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005001/// IvarListnfABIPtrTy.
5002/// struct _ivar_t {
5003/// unsigned long int *offset; // pointer to ivar offset location
5004/// char *name;
5005/// char *type;
5006/// uint32_t alignment;
5007/// uint32_t size;
5008/// }
5009/// struct _ivar_list_t {
5010/// uint32 entsize; // sizeof(struct _ivar_t)
5011/// uint32 count;
5012/// struct _iver_t list[count];
5013/// }
5014///
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005015
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005016llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005017 const ObjCImplementationDecl *ID) {
5018
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005019 std::vector<llvm::Constant*> Ivars, Ivar(5);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005020
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005021 const ObjCInterfaceDecl *OID = ID->getClassInterface();
5022 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005023
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005024 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005025
Daniel Dunbarae032262009-04-20 00:33:43 +00005026 // Collect declared and synthesized ivars in a small vector.
Fariborz Jahanian63a224a2009-03-31 18:11:23 +00005027 llvm::SmallVector<ObjCIvarDecl*, 16> OIvars;
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005028 CGM.getContext().ShallowCollectObjCIvars(OID, OIvars);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005029
Daniel Dunbarf5c18462009-04-20 06:54:31 +00005030 for (unsigned i = 0, e = OIvars.size(); i != e; ++i) {
5031 ObjCIvarDecl *IVD = OIvars[i];
Fariborz Jahanian7c809592009-06-04 01:19:09 +00005032 // Ignore unnamed bit-fields.
5033 if (!IVD->getDeclName())
5034 continue;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005035 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar961202372009-05-03 12:57:56 +00005036 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005037 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5038 Ivar[2] = GetMethodVarType(IVD);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005039 const llvm::Type *FieldTy =
Daniel Dunbar725dc2c2009-04-22 08:22:17 +00005040 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005041 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005042 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005043 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005044 Align = llvm::Log2_32(Align);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005045 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbarae032262009-04-20 00:33:43 +00005046 // NOTE. Size of a bitfield does not match gcc's, because of the
5047 // way bitfields are treated special in each. But I am told that
5048 // 'size' for bitfield ivars is ignored by the runtime so it does
5049 // not matter. If it matters, there is enough info to get the
5050 // bitfield right!
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005051 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005052 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005053 }
5054 // Return null for empty list.
5055 if (Ivars.empty())
Owen Anderson0b75f232009-07-31 20:28:54 +00005056 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005057 std::vector<llvm::Constant*> Values(3);
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005058 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005059 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5060 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson9793f0e2009-07-29 22:16:19 +00005061 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005062 Ivars.size());
Owen Anderson47034e12009-07-28 18:33:04 +00005063 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005064 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005065 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5066 llvm::GlobalVariable *GV =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005067 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian7415caa2009-01-27 19:38:51 +00005068 llvm::GlobalValue::InternalLinkage,
5069 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005070 Prefix + OID->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005071 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005072 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian40a4bcd2009-01-28 01:05:23 +00005073 GV->setSection("__DATA, __objc_const");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005074
Chris Lattnerf56501c2009-07-17 23:57:13 +00005075 CGM.AddUsedGlobal(GV);
Owen Andersonade90fd2009-07-29 18:54:39 +00005076 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005077}
5078
5079llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
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 if (!Entry) {
5084 // We use the initializer as a marker of whether this is a forward
5085 // reference or not. At module finalization we add the empty
5086 // contents for protocols which were referenced but never defined.
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005087 Entry =
5088 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5089 llvm::GlobalValue::ExternalLinkage,
5090 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005091 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005092 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005093 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005094
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005095 return Entry;
5096}
5097
5098/// GetOrEmitProtocol - Generate the protocol meta-data:
5099/// @code
5100/// struct _protocol_t {
5101/// id isa; // NULL
5102/// const char * const protocol_name;
5103/// const struct _protocol_list_t * protocol_list; // super protocols
5104/// const struct method_list_t * const instance_methods;
5105/// const struct method_list_t * const class_methods;
5106/// const struct method_list_t *optionalInstanceMethods;
5107/// const struct method_list_t *optionalClassMethods;
5108/// const struct _prop_list_t * properties;
5109/// const uint32_t size; // sizeof(struct _protocol_t)
5110/// const uint32_t flags; // = 0
5111/// }
5112/// @endcode
5113///
5114
5115llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005116 const ObjCProtocolDecl *PD) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005117 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005118
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005119 // Early exit if a defining object has already been generated.
5120 if (Entry && Entry->hasInitializer())
5121 return Entry;
5122
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005123 // Construct method lists.
5124 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5125 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005126 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005127 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005128 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005129 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005130 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5131 OptInstanceMethods.push_back(C);
5132 } else {
5133 InstanceMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005134 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005135 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005136
5137 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00005138 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005139 ObjCMethodDecl *MD = *i;
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005140 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005141 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5142 OptClassMethods.push_back(C);
5143 } else {
5144 ClassMethods.push_back(C);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005145 }
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005146 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005147
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005148 std::vector<llvm::Constant*> Values(10);
5149 // isa is NULL
Owen Anderson0b75f232009-07-31 20:28:54 +00005150 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005151 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005152 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5153 PD->protocol_begin(),
5154 PD->protocol_end());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005155
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005156 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005157 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005158 "__DATA, __objc_const",
5159 InstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005160 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005161 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005162 "__DATA, __objc_const",
5163 ClassMethods);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005164 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005165 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005166 "__DATA, __objc_const",
5167 OptInstanceMethods);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005168 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005169 + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005170 "__DATA, __objc_const",
5171 OptClassMethods);
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005172 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005173 0, PD, ObjCTypes);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005174 uint32_t Size =
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005175 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005176 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson0b75f232009-07-31 20:28:54 +00005177 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005178 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005179 Values);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005180
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005181 if (Entry) {
5182 // Already created, fix the linkage and update the initializer.
Mike Stumpa6ca3342009-03-07 16:33:28 +00005183 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005184 Entry->setInitializer(Init);
5185 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005186 Entry =
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005187 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5188 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5189 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005190 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005191 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005192 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005193 }
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005194 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005195 CGM.AddUsedGlobal(Entry);
5196
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005197 // Use this protocol meta-data to build protocol list table in section
5198 // __DATA, __objc_protolist
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005199 llvm::GlobalVariable *PTGV =
5200 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5201 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5202 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005203 PTGV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005204 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbarb25452a2009-04-15 02:56:18 +00005205 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian61cd4b52009-01-29 20:10:59 +00005206 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerf56501c2009-07-17 23:57:13 +00005207 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005208 return Entry;
5209}
5210
5211/// EmitProtocolList - Generate protocol list meta-data:
5212/// @code
5213/// struct _protocol_list_t {
5214/// long protocol_count; // Note, this is 32/64 bit
5215/// struct _protocol_t[protocol_count];
5216/// }
5217/// @endcode
5218///
5219llvm::Constant *
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005220CGObjCNonFragileABIMac::EmitProtocolList(llvm::Twine Name,
5221 ObjCProtocolDecl::protocol_iterator begin,
5222 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005223 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005224
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005225 // Just return null for empty protocol lists
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005226 if (begin == end)
Owen Anderson0b75f232009-07-31 20:28:54 +00005227 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005228
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005229 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005230 llvm::SmallString<256> TmpName;
5231 Name.toVector(TmpName);
5232 llvm::GlobalVariable *GV =
5233 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005234 if (GV)
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005235 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005236
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005237 for (; begin != end; ++begin)
5238 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5239
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005240 // This list is null terminated.
Owen Anderson0b75f232009-07-31 20:28:54 +00005241 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005242 ObjCTypes.ProtocolnfABIPtrTy));
5243
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005244 std::vector<llvm::Constant*> Values(2);
Owen Anderson170229f2009-07-14 23:10:40 +00005245 Values[0] =
Owen Andersonb7a2fe62009-07-24 23:12:58 +00005246 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005247 Values[1] =
Owen Anderson47034e12009-07-28 18:33:04 +00005248 llvm::ConstantArray::get(
Owen Anderson9793f0e2009-07-29 22:16:19 +00005249 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005250 ProtocolRefs.size()),
5251 ProtocolRefs);
5252
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005253 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005254 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005255 llvm::GlobalValue::InternalLinkage,
5256 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005257 Name);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005258 GV->setSection("__DATA, __objc_const");
Fariborz Jahanianc22f2362009-01-31 02:43:27 +00005259 GV->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005260 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerf56501c2009-07-17 23:57:13 +00005261 CGM.AddUsedGlobal(GV);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005262 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar8de90f02009-02-15 07:36:20 +00005263 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanian56b3b772009-01-29 19:24:30 +00005264}
5265
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005266/// GetMethodDescriptionConstant - This routine build following meta-data:
5267/// struct _objc_method {
5268/// SEL _cmd;
5269/// char *method_type;
5270/// char *_imp;
5271/// }
5272
5273llvm::Constant *
5274CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
5275 std::vector<llvm::Constant*> Desc(3);
Owen Anderson170229f2009-07-14 23:10:40 +00005276 Desc[0] =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005277 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5278 ObjCTypes.SelectorPtrTy);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005279 Desc[1] = GetMethodVarType(MD);
Fariborz Jahanian097feda2009-01-30 18:58:59 +00005280 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Anderson0b75f232009-07-31 20:28:54 +00005281 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson0e0189d2009-07-27 22:29:56 +00005282 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahaniand9c28b82009-01-30 00:46:37 +00005283}
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005284
5285/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5286/// This code gen. amounts to generating code for:
5287/// @code
5288/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5289/// @encode
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005290///
Fariborz Jahanian712bfa62009-02-03 19:03:09 +00005291LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall96fa4842010-05-17 21:00:27 +00005292 CodeGen::CodeGenFunction &CGF,
5293 QualType ObjectTy,
5294 llvm::Value *BaseValue,
5295 const ObjCIvarDecl *Ivar,
5296 unsigned CVRQualifiers) {
5297 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar9fd114d2009-04-22 07:32:20 +00005298 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5299 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanianc88a70d2009-02-03 00:09:52 +00005300}
5301
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005302llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005303 CodeGen::CodeGenFunction &CGF,
5304 const ObjCInterfaceDecl *Interface,
5305 const ObjCIvarDecl *Ivar) {
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005306 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanian21fc74c2009-02-10 19:02:04 +00005307}
5308
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005309CodeGen::RValue CGObjCNonFragileABIMac::EmitMessageSend(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005310 CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005311 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005312 QualType ResultType,
5313 Selector Sel,
5314 llvm::Value *Receiver,
5315 QualType Arg0Ty,
5316 bool IsSuper,
5317 const CallArgList &CallArgs) {
Mike Stump18bb9282009-05-16 07:57:57 +00005318 // FIXME. Even though IsSuper is passes. This function doese not handle calls
5319 // to 'super' receivers.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005320 CodeGenTypes &Types = CGM.getTypes();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005321 llvm::Value *Arg0 = Receiver;
5322 if (!IsSuper)
5323 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005324
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005325 // Find the message function name.
Mike Stump18bb9282009-05-16 07:57:57 +00005326 // FIXME. This is too much work to get the ABI-specific result type needed to
5327 // find the message name.
John McCall2da83a32010-02-26 00:48:12 +00005328 const CGFunctionInfo &FnInfo
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005329 = Types.getFunctionInfo(ResultType, CallArgList(),
5330 FunctionType::ExtInfo());
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005331 llvm::Constant *Fn = 0;
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005332 std::string Name("\01l_");
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005333 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
Fariborz Jahanian90655412009-02-05 18:00:27 +00005334#if 0
5335 // unlike what is documented. gcc never generates this API!!
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005336 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005337 Fn = ObjCTypes.getMessageSendIdStretFixupFn();
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005338 // FIXME. Is there a better way of getting these names.
5339 // They are available in RuntimeFunctions vector pair.
5340 Name += "objc_msgSendId_stret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005341 } else
Fariborz Jahanian90655412009-02-05 18:00:27 +00005342#endif
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005343 if (IsSuper) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005344 Fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005345 Name += "objc_msgSendSuper2_stret_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005346 } else {
5347 Fn = ObjCTypes.getMessageSendStretFixupFn();
5348 Name += "objc_msgSend_stret_fixup";
5349 }
Daniel Dunbar6f2e8392010-07-14 23:39:36 +00005350 } else if (!IsSuper && CGM.ReturnTypeUsesFPRet(ResultType)) {
5351 Fn = ObjCTypes.getMessageSendFpretFixupFn();
5352 Name += "objc_msgSend_fpret_fixup";
Mike Stump658fe022009-07-30 22:28:39 +00005353 } else {
Fariborz Jahanian90655412009-02-05 18:00:27 +00005354#if 0
5355// unlike what is documented. gcc never generates this API!!
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005356 if (Receiver->getType() == ObjCTypes.ObjectPtrTy) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005357 Fn = ObjCTypes.getMessageSendIdFixupFn();
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005358 Name += "objc_msgSendId_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005359 } else
Fariborz Jahanian90655412009-02-05 18:00:27 +00005360#endif
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005361 if (IsSuper) {
Chris Lattner2dfdb3e2009-04-22 02:53:24 +00005362 Fn = ObjCTypes.getMessageSendSuper2FixupFn();
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005363 Name += "objc_msgSendSuper2_fixup";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005364 } else {
5365 Fn = ObjCTypes.getMessageSendFixupFn();
5366 Name += "objc_msgSend_fixup";
5367 }
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005368 }
Fariborz Jahaniane29b4f02009-04-30 23:08:58 +00005369 assert(Fn && "CGObjCNonFragileABIMac::EmitMessageSend");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005370 Name += '_';
5371 std::string SelName(Sel.getAsString());
5372 // Replace all ':' in selector name with '_' ouch!
Mike Stump11289f42009-09-09 15:08:12 +00005373 for (unsigned i = 0; i < SelName.size(); i++)
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005374 if (SelName[i] == ':')
5375 SelName[i] = '_';
5376 Name += SelName;
5377 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5378 if (!GV) {
Daniel Dunbare60aa052009-04-15 19:03:14 +00005379 // Build message ref table entry.
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005380 std::vector<llvm::Constant*> Values(2);
5381 Values[0] = Fn;
5382 Values[1] = GetMethodVarName(Sel);
Nick Lewycky41eaf0a2009-09-19 20:00:52 +00005383 llvm::Constant *Init = llvm::ConstantStruct::get(VMContext, Values, false);
Owen Andersonc10c8d32009-07-08 19:05:04 +00005384 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Mike Stumpa6ca3342009-03-07 16:33:28 +00005385 llvm::GlobalValue::WeakAnyLinkage,
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005386 Init,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005387 Name);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005388 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar24645c92009-04-15 19:04:46 +00005389 GV->setAlignment(16);
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005390 GV->setSection("__DATA, __objc_msgrefs, coalesced");
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005391 }
5392 llvm::Value *Arg1 = CGF.Builder.CreateBitCast(GV, ObjCTypes.MessageRefPtrTy);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005393
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005394 CallArgList ActualArgs;
5395 ActualArgs.push_back(std::make_pair(RValue::get(Arg0), Arg0Ty));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005396 ActualArgs.push_back(std::make_pair(RValue::get(Arg1),
Fariborz Jahaniane4dc35d2009-02-04 20:42:28 +00005397 ObjCTypes.MessageRefCPtrTy));
5398 ActualArgs.insert(ActualArgs.end(), CallArgs.begin(), CallArgs.end());
John McCallab26cfa2010-02-05 21:31:56 +00005399 const CGFunctionInfo &FnInfo1 = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindolac50c27c2010-03-30 20:24:48 +00005400 FunctionType::ExtInfo());
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005401 llvm::Value *Callee = CGF.Builder.CreateStructGEP(Arg1, 0);
5402 Callee = CGF.Builder.CreateLoad(Callee);
Fariborz Jahanian35afdfc2009-02-14 21:25:36 +00005403 const llvm::FunctionType *FTy = Types.GetFunctionType(FnInfo1, true);
Fariborz Jahanian4e87c832009-02-05 01:13:09 +00005404 Callee = CGF.Builder.CreateBitCast(Callee,
Owen Anderson9793f0e2009-07-29 22:16:19 +00005405 llvm::PointerType::getUnqual(FTy));
John McCall78a15112010-05-22 01:48:05 +00005406 return CGF.EmitCall(FnInfo1, Callee, Return, ActualArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005407}
5408
5409/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005410CodeGen::RValue
5411CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005412 ReturnValueSlot Return,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005413 QualType ResultType,
5414 Selector Sel,
5415 llvm::Value *Receiver,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005416 const CallArgList &CallArgs,
David Chisnall01aa4672010-04-28 19:33:36 +00005417 const ObjCInterfaceDecl *Class,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005418 const ObjCMethodDecl *Method) {
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005419 return LegacyDispatchedSelector(Sel)
John McCall78a15112010-05-22 01:48:05 +00005420 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5421 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005422 Receiver, CGF.getContext().getObjCIdType(),
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005423 false, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005424 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005425 Receiver, CGF.getContext().getObjCIdType(),
5426 false, CallArgs);
Fariborz Jahanian3d9296e2009-02-04 00:22:57 +00005427}
5428
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005429llvm::GlobalVariable *
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005430CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005431 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5432
Daniel Dunbara6468342009-03-02 05:18:14 +00005433 if (!GV) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005434 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005435 false, llvm::GlobalValue::ExternalLinkage,
5436 0, Name);
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005437 }
5438
5439 return GV;
5440}
5441
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005442llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5443 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005444 llvm::GlobalVariable *&Entry = ClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005445
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005446 if (!Entry) {
Daniel Dunbar15894b72009-04-07 05:48:37 +00005447 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarc6928bb2009-03-01 04:40:10 +00005448 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005449 Entry =
5450 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005451 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005452 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005453 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005454 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005455 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005456 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005457 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005458 CGM.AddUsedGlobal(Entry);
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005459 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005460
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005461 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005462}
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005463
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005464llvm::Value *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005465CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005466 const ObjCInterfaceDecl *ID) {
5467 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005468
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005469 if (!Entry) {
5470 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5471 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005472 Entry =
5473 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005474 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005475 ClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005476 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005477 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005478 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005479 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005480 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005481 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005482 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005483
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005484 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005485}
5486
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005487/// EmitMetaClassRef - Return a Value * of the address of _class_t
5488/// meta-data
5489///
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005490llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5491 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005492 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5493 if (Entry)
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005494 return Builder.CreateLoad(Entry, "tmp");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005495
Daniel Dunbar15894b72009-04-07 05:48:37 +00005496 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005497 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005498 Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005499 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005500 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005501 MetaClassGV,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005502 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005503 Entry->setAlignment(
Daniel Dunbar710cb202010-04-25 20:39:32 +00005504 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005505 ObjCTypes.ClassnfABIPtrTy));
5506
Daniel Dunbare60aa052009-04-15 19:03:14 +00005507 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005508 CGM.AddUsedGlobal(Entry);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005509
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005510 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005511}
5512
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005513/// GetClass - Return a reference to the class for the given interface
5514/// decl.
5515llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5516 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian95ace552009-11-17 22:42:00 +00005517 if (ID->hasAttr<WeakImportAttr>()) {
5518 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5519 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5520 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5521 }
5522
Fariborz Jahanian33f66e62009-02-05 20:41:40 +00005523 return EmitClassRef(Builder, ID);
5524}
5525
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005526/// Generates a message send where the super is the receiver. This is
5527/// a message send to self with special delivery semantics indicating
5528/// which class's method should be called.
5529CodeGen::RValue
5530CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCall78a15112010-05-22 01:48:05 +00005531 ReturnValueSlot Return,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005532 QualType ResultType,
5533 Selector Sel,
5534 const ObjCInterfaceDecl *Class,
5535 bool isCategoryImpl,
5536 llvm::Value *Receiver,
5537 bool IsClassMessage,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005538 const CodeGen::CallArgList &CallArgs,
5539 const ObjCMethodDecl *Method) {
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005540 // ...
5541 // Create and init a super structure; this is a (receiver, class)
5542 // pair we will pass to objc_msgSendSuper.
5543 llvm::Value *ObjCSuper =
5544 CGF.Builder.CreateAlloca(ObjCTypes.SuperTy, 0, "objc_super");
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005545
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005546 llvm::Value *ReceiverAsObject =
5547 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5548 CGF.Builder.CreateStore(ReceiverAsObject,
5549 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005550
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005551 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005552 llvm::Value *Target;
5553 if (IsClassMessage) {
5554 if (isCategoryImpl) {
5555 // Message sent to "super' in a class method defined in
5556 // a category implementation.
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005557 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005558 Target = CGF.Builder.CreateStructGEP(Target, 0);
5559 Target = CGF.Builder.CreateLoad(Target);
Mike Stump658fe022009-07-30 22:28:39 +00005560 } else
Fariborz Jahanianbac73ac2009-02-28 20:07:56 +00005561 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stump658fe022009-07-30 22:28:39 +00005562 } else
Daniel Dunbar508a7dd2009-04-18 08:51:00 +00005563 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005564
Mike Stump18bb9282009-05-16 07:57:57 +00005565 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5566 // ObjCTypes types.
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005567 const llvm::Type *ClassTy =
5568 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5569 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5570 CGF.Builder.CreateStore(Target,
5571 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005572
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005573 return (LegacyDispatchedSelector(Sel))
John McCall78a15112010-05-22 01:48:05 +00005574 ? EmitLegacyMessageSend(CGF, Return, ResultType,
5575 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005576 ObjCSuper, ObjCTypes.SuperPtrCTy,
Daniel Dunbaraff9fca2009-09-17 04:01:22 +00005577 true, CallArgs, Method, ObjCTypes)
John McCall78a15112010-05-22 01:48:05 +00005578 : EmitMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005579 ObjCSuper, ObjCTypes.SuperPtrCTy,
5580 true, CallArgs);
Fariborz Jahanian6b7cd6e2009-02-06 20:09:23 +00005581}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005582
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005583llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005584 Selector Sel, bool lval) {
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005585 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005586
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005587 if (!Entry) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005588 llvm::Constant *Casted =
5589 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5590 ObjCTypes.SelectorPtrTy);
5591 Entry =
5592 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5593 llvm::GlobalValue::InternalLinkage,
5594 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahanian5d5ed2d2009-05-11 19:25:47 +00005595 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerf56501c2009-07-17 23:57:13 +00005596 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005597 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005598
Fariborz Jahanian9240f3d2010-06-17 19:56:20 +00005599 if (lval)
5600 return Entry;
Daniel Dunbarc76493a2009-11-29 21:23:36 +00005601 return Builder.CreateLoad(Entry, "tmp");
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005602}
Fariborz Jahanian06292952009-02-16 22:52:32 +00005603/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005604/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian06292952009-02-16 22:52:32 +00005605///
5606void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005607 llvm::Value *src,
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005608 llvm::Value *dst,
5609 llvm::Value *ivarOffset) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005610 const llvm::Type * SrcTy = src->getType();
5611 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005612 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005613 assert(Size <= 8 && "does not support size > 8");
5614 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5615 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005616 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5617 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005618 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5619 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian7a95d722009-09-24 22:25:38 +00005620 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5621 src, dst, ivarOffset);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005622 return;
5623}
5624
5625/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5626/// objc_assign_strongCast (id src, id *dst)
5627///
5628void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005629 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005630 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005631 const llvm::Type * SrcTy = src->getType();
5632 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005633 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005634 assert(Size <= 8 && "does not support size > 8");
5635 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005636 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005637 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5638 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005639 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5640 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner0a696a422009-04-22 02:38:11 +00005641 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005642 src, dst, "weakassign");
5643 return;
5644}
5645
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005646void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005647 CodeGen::CodeGenFunction &CGF,
5648 llvm::Value *DestPtr,
5649 llvm::Value *SrcPtr,
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005650 llvm::Value *Size) {
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005651 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
5652 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005653 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian021510e2010-06-15 22:44:06 +00005654 DestPtr, SrcPtr, Size);
Fariborz Jahanian5f21d2f2009-07-08 01:18:33 +00005655 return;
5656}
5657
Fariborz Jahanian06292952009-02-16 22:52:32 +00005658/// EmitObjCWeakRead - Code gen for loading value of a __weak
5659/// object: objc_read_weak (id *src)
5660///
5661llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005662 CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005663 llvm::Value *AddrWeakObj) {
Eli Friedmana374b682009-03-07 03:57:15 +00005664 const llvm::Type* DestTy =
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005665 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
5666 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerce8754e2009-04-22 02:44:54 +00005667 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005668 AddrWeakObj, "weakread");
Eli Friedmana374b682009-03-07 03:57:15 +00005669 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian06292952009-02-16 22:52:32 +00005670 return read_weak;
5671}
5672
5673/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
5674/// objc_assign_weak (id src, id *dst)
5675///
5676void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump11289f42009-09-09 15:08:12 +00005677 llvm::Value *src, llvm::Value *dst) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005678 const llvm::Type * SrcTy = src->getType();
5679 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005680 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005681 assert(Size <= 8 && "does not support size > 8");
5682 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5683 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005684 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5685 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005686 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5687 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner6fdd57c2009-04-17 22:12:36 +00005688 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian06292952009-02-16 22:52:32 +00005689 src, dst, "weakassign");
5690 return;
5691}
5692
5693/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
5694/// objc_assign_global (id src, id *dst)
5695///
5696void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian217af242010-07-20 20:30:03 +00005697 llvm::Value *src, llvm::Value *dst,
5698 bool threadlocal) {
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005699 const llvm::Type * SrcTy = src->getType();
5700 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sandsc76fe8b2009-05-09 07:08:47 +00005701 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanianaedcfa42009-03-23 19:10:40 +00005702 assert(Size <= 8 && "does not support size > 8");
5703 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5704 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian1b074a32009-03-13 00:42:52 +00005705 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5706 }
Fariborz Jahanian06292952009-02-16 22:52:32 +00005707 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5708 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian217af242010-07-20 20:30:03 +00005709 if (!threadlocal)
5710 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
5711 src, dst, "globalassign");
5712 else
5713 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
5714 src, dst, "threadlocalassign");
Fariborz Jahanian06292952009-02-16 22:52:32 +00005715 return;
5716}
Fariborz Jahanian74b77222009-02-11 20:51:17 +00005717
John McCallc20acd32010-07-21 00:41:47 +00005718namespace {
John McCallcda666c2010-07-21 07:22:38 +00005719 struct CallSyncExit : EHScopeStack::Cleanup {
John McCallc20acd32010-07-21 00:41:47 +00005720 llvm::Value *SyncExitFn;
5721 llvm::Value *SyncArg;
5722 CallSyncExit(llvm::Value *SyncExitFn, llvm::Value *SyncArg)
5723 : SyncExitFn(SyncExitFn), SyncArg(SyncArg) {}
5724
5725 void Emit(CodeGenFunction &CGF, bool IsForEHCleanup) {
5726 CGF.Builder.CreateCall(SyncExitFn, SyncArg)->setDoesNotThrow();
5727 }
5728 };
5729}
5730
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005731void
John McCallbd309292010-07-06 01:34:17 +00005732CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
5733 const ObjCAtSynchronizedStmt &S) {
5734 // Evaluate the lock operand. This should dominate the cleanup.
5735 llvm::Value *SyncArg = CGF.EmitScalarExpr(S.getSynchExpr());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005736
John McCallbd309292010-07-06 01:34:17 +00005737 // Acquire the lock.
5738 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
5739 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
5740 ->setDoesNotThrow();
5741
5742 // Register an all-paths cleanup to release the lock.
John McCallcda666c2010-07-21 07:22:38 +00005743 CGF.EHStack.pushCleanup<CallSyncExit>(NormalAndEHCleanup,
5744 ObjCTypes.getSyncExitFn(),
5745 SyncArg);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005746
John McCallbd309292010-07-06 01:34:17 +00005747 // Emit the body of the statement.
5748 CGF.EmitStmt(S.getSynchBody());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005749
John McCallbd309292010-07-06 01:34:17 +00005750 // Pop the lock-release cleanup.
5751 CGF.PopCleanupBlock();
5752}
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005753
John McCallbd309292010-07-06 01:34:17 +00005754namespace {
5755 struct CatchHandler {
5756 const VarDecl *Variable;
5757 const Stmt *Body;
5758 llvm::BasicBlock *Block;
5759 llvm::Value *TypeInfo;
5760 };
John McCall5c08ab92010-07-13 22:12:14 +00005761
John McCallcda666c2010-07-21 07:22:38 +00005762 struct CallObjCEndCatch : EHScopeStack::Cleanup {
John McCall5c08ab92010-07-13 22:12:14 +00005763 CallObjCEndCatch(bool MightThrow, llvm::Value *Fn) :
5764 MightThrow(MightThrow), Fn(Fn) {}
5765 bool MightThrow;
5766 llvm::Value *Fn;
5767
5768 void Emit(CodeGenFunction &CGF, bool IsForEH) {
5769 if (!MightThrow) {
5770 CGF.Builder.CreateCall(Fn)->setDoesNotThrow();
5771 return;
5772 }
5773
5774 CGF.EmitCallOrInvoke(Fn, 0, 0);
5775 }
5776 };
John McCallbd309292010-07-06 01:34:17 +00005777}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005778
John McCall2ca705e2010-07-24 00:37:23 +00005779llvm::Constant *
5780CGObjCNonFragileABIMac::GetEHType(QualType T) {
5781 // There's a particular fixed type info for 'id'.
5782 if (T->isObjCIdType() ||
5783 T->isObjCQualifiedIdType()) {
5784 llvm::Constant *IDEHType =
5785 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
5786 if (!IDEHType)
5787 IDEHType =
5788 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
5789 false,
5790 llvm::GlobalValue::ExternalLinkage,
5791 0, "OBJC_EHTYPE_id");
5792 return IDEHType;
5793 }
5794
5795 // All other types should be Objective-C interface pointer types.
5796 const ObjCObjectPointerType *PT =
5797 T->getAs<ObjCObjectPointerType>();
5798 assert(PT && "Invalid @catch type.");
5799 const ObjCInterfaceType *IT = PT->getInterfaceType();
5800 assert(IT && "Invalid @catch type.");
5801 return GetInterfaceEHType(IT->getDecl(), false);
5802}
5803
John McCallbd309292010-07-06 01:34:17 +00005804void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
5805 const ObjCAtTryStmt &S) {
5806 // Jump destination for falling out of catch bodies.
5807 CodeGenFunction::JumpDest Cont;
5808 if (S.getNumCatchStmts())
5809 Cont = CGF.getJumpDestInCurrentScope("eh.cont");
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005810
John McCallbd309292010-07-06 01:34:17 +00005811 CodeGenFunction::FinallyInfo FinallyInfo;
5812 if (const ObjCAtFinallyStmt *Finally = S.getFinallyStmt())
5813 FinallyInfo = CGF.EnterFinallyBlock(Finally->getFinallyBody(),
5814 ObjCTypes.getObjCBeginCatchFn(),
5815 ObjCTypes.getObjCEndCatchFn(),
5816 ObjCTypes.getExceptionRethrowFn());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005817
John McCallbd309292010-07-06 01:34:17 +00005818 llvm::SmallVector<CatchHandler, 8> Handlers;
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005819
John McCallbd309292010-07-06 01:34:17 +00005820 // Enter the catch, if there is one.
5821 if (S.getNumCatchStmts()) {
5822 for (unsigned I = 0, N = S.getNumCatchStmts(); I != N; ++I) {
5823 const ObjCAtCatchStmt *CatchStmt = S.getCatchStmt(I);
Douglas Gregor46a572b2010-04-26 16:46:50 +00005824 const VarDecl *CatchDecl = CatchStmt->getCatchParamDecl();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005825
John McCallbd309292010-07-06 01:34:17 +00005826 Handlers.push_back(CatchHandler());
5827 CatchHandler &Handler = Handlers.back();
5828 Handler.Variable = CatchDecl;
5829 Handler.Body = CatchStmt->getCatchBody();
5830 Handler.Block = CGF.createBasicBlock("catch");
5831
5832 // @catch(...) always matches.
Douglas Gregor96c79492010-04-23 22:50:49 +00005833 if (!CatchDecl) {
John McCallbd309292010-07-06 01:34:17 +00005834 Handler.TypeInfo = 0; // catch-all
5835 // Don't consider any other catches.
Douglas Gregor96c79492010-04-23 22:50:49 +00005836 break;
5837 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005838
John McCall2ca705e2010-07-24 00:37:23 +00005839 Handler.TypeInfo = GetEHType(CatchDecl->getType());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005840 }
John McCallbd309292010-07-06 01:34:17 +00005841
5842 EHCatchScope *Catch = CGF.EHStack.pushCatch(Handlers.size());
5843 for (unsigned I = 0, E = Handlers.size(); I != E; ++I)
5844 Catch->setHandler(I, Handlers[I].TypeInfo, Handlers[I].Block);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005845 }
John McCallbd309292010-07-06 01:34:17 +00005846
5847 // Emit the try body.
5848 CGF.EmitStmt(S.getTryBody());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005849
John McCallbd309292010-07-06 01:34:17 +00005850 // Leave the try.
5851 if (S.getNumCatchStmts())
5852 CGF.EHStack.popCatch();
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005853
John McCallbd309292010-07-06 01:34:17 +00005854 // Remember where we were.
5855 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005856
John McCallbd309292010-07-06 01:34:17 +00005857 // Emit the handlers.
5858 for (unsigned I = 0, E = Handlers.size(); I != E; ++I) {
5859 CatchHandler &Handler = Handlers[I];
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005860
John McCallbd309292010-07-06 01:34:17 +00005861 CGF.EmitBlock(Handler.Block);
5862 llvm::Value *RawExn = CGF.Builder.CreateLoad(CGF.getExceptionSlot());
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005863
John McCallbd309292010-07-06 01:34:17 +00005864 // Enter the catch.
5865 llvm::CallInst *Exn =
5866 CGF.Builder.CreateCall(ObjCTypes.getObjCBeginCatchFn(), RawExn,
5867 "exn.adjusted");
5868 Exn->setDoesNotThrow();
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005869
John McCallbd309292010-07-06 01:34:17 +00005870 // Add a cleanup to leave the catch.
John McCall5c08ab92010-07-13 22:12:14 +00005871 bool EndCatchMightThrow = (Handler.Variable == 0);
John McCallcda666c2010-07-21 07:22:38 +00005872 CGF.EHStack.pushCleanup<CallObjCEndCatch>(NormalAndEHCleanup,
5873 EndCatchMightThrow,
5874 ObjCTypes.getObjCEndCatchFn());
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005875
John McCallbd309292010-07-06 01:34:17 +00005876 // Bind the catch parameter if it exists.
5877 if (const VarDecl *CatchParam = Handler.Variable) {
5878 const llvm::Type *CatchType = CGF.ConvertType(CatchParam->getType());
5879 llvm::Value *CastExn = CGF.Builder.CreateBitCast(Exn, CatchType);
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005880
John McCallbd309292010-07-06 01:34:17 +00005881 CGF.EmitLocalBlockVarDecl(*CatchParam);
5882 CGF.Builder.CreateStore(CastExn, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005883 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005884
John McCallbd309292010-07-06 01:34:17 +00005885 CGF.ObjCEHValueStack.push_back(Exn);
5886 CGF.EmitStmt(Handler.Body);
5887 CGF.ObjCEHValueStack.pop_back();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005888
John McCallbd309292010-07-06 01:34:17 +00005889 // Leave the earlier cleanup.
5890 CGF.PopCleanupBlock();
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005891
John McCallbd309292010-07-06 01:34:17 +00005892 CGF.EmitBranchThroughCleanup(Cont);
5893 }
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005894
John McCallbd309292010-07-06 01:34:17 +00005895 // Go back to the try-statement fallthrough.
5896 CGF.Builder.restoreIP(SavedIP);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005897
John McCallbd309292010-07-06 01:34:17 +00005898 // Pop out of the normal cleanup on the finally.
5899 if (S.getFinallyStmt())
5900 CGF.ExitFinallyBlock(FinallyInfo);
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005901
John McCallad5d61e2010-07-23 21:56:41 +00005902 if (Cont.isValid())
5903 CGF.EmitBlock(Cont.getBlock());
Daniel Dunbar0b0dcd92009-02-24 07:47:38 +00005904}
5905
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005906/// EmitThrowStmt - Generate code for a throw statement.
5907void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
5908 const ObjCAtThrowStmt &S) {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005909 llvm::Value *Exception;
Fariborz Jahanian3336de12010-05-28 17:34:43 +00005910 llvm::Constant *FunctionThrowOrRethrow;
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005911 if (const Expr *ThrowExpr = S.getThrowExpr()) {
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005912 Exception = CGF.EmitScalarExpr(ThrowExpr);
Fariborz Jahanian3336de12010-05-28 17:34:43 +00005913 FunctionThrowOrRethrow = ObjCTypes.getExceptionThrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005914 } else {
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005915 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005916 "Unexpected rethrow outside @catch block.");
5917 Exception = CGF.ObjCEHValueStack.back();
Fariborz Jahanian3336de12010-05-28 17:34:43 +00005918 FunctionThrowOrRethrow = ObjCTypes.getExceptionRethrowFn();
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005919 }
5920
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005921 llvm::Value *ExceptionAsObject =
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005922 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy, "tmp");
5923 llvm::BasicBlock *InvokeDest = CGF.getInvokeDest();
5924 if (InvokeDest) {
Fariborz Jahanian3336de12010-05-28 17:34:43 +00005925 CGF.Builder.CreateInvoke(FunctionThrowOrRethrow,
John McCallbd309292010-07-06 01:34:17 +00005926 CGF.getUnreachableBlock(), InvokeDest,
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005927 &ExceptionAsObject, &ExceptionAsObject + 1);
John McCallbd309292010-07-06 01:34:17 +00005928 } else {
5929 CGF.Builder.CreateCall(FunctionThrowOrRethrow, ExceptionAsObject)
5930 ->setDoesNotReturn();
5931 CGF.Builder.CreateUnreachable();
5932 }
Daniel Dunbar76b7acc2009-03-02 06:08:11 +00005933
Anders Carlsson9ab53d12009-02-16 22:59:18 +00005934 // Clear the insertion point to indicate we are in unreachable code.
5935 CGF.Builder.ClearInsertionPoint();
5936}
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005937
John McCall2ca705e2010-07-24 00:37:23 +00005938llvm::Constant *
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005939CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005940 bool ForDefinition) {
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005941 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005942
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005943 // If we don't need a definition, return the entry if found or check
5944 // if we use an external reference.
5945 if (!ForDefinition) {
5946 if (Entry)
5947 return Entry;
Daniel Dunbard7beeea2009-04-07 06:43:45 +00005948
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005949 // If this type (or a super class) has the __objc_exception__
5950 // attribute, emit an external reference.
Douglas Gregor78bd61f2009-06-18 16:11:24 +00005951 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005952 return Entry =
Owen Andersonc10c8d32009-07-08 19:05:04 +00005953 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005954 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005955 0,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005956 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00005957 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005958 }
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005959
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005960 // Otherwise we need to either make a new entry or fill in the
5961 // initializer.
5962 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar15894b72009-04-07 05:48:37 +00005963 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005964 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005965 llvm::GlobalVariable *VTableGV =
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005966 CGM.getModule().getGlobalVariable(VTableName);
5967 if (!VTableGV)
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005968 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
5969 false,
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005970 llvm::GlobalValue::ExternalLinkage,
Owen Andersonc10c8d32009-07-08 19:05:04 +00005971 0, VTableName);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005972
Chris Lattner5e016ae2010-06-27 07:15:29 +00005973 llvm::Value *VTableIdx =
5974 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005975
5976 std::vector<llvm::Constant*> Values(3);
Owen Andersonade90fd2009-07-29 18:54:39 +00005977 Values[0] = llvm::ConstantExpr::getGetElementPtr(VTableGV, &VTableIdx, 1);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005978 Values[1] = GetClassName(ID->getIdentifier());
Fariborz Jahanian899e7eb2009-04-14 18:41:56 +00005979 Values[2] = GetClassGlobal(ClassName);
Owen Anderson170229f2009-07-14 23:10:40 +00005980 llvm::Constant *Init =
Owen Anderson0e0189d2009-07-27 22:29:56 +00005981 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbarb1559a42009-03-01 04:46:24 +00005982
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005983 if (Entry) {
5984 Entry->setInitializer(Init);
5985 } else {
Owen Andersonc10c8d32009-07-08 19:05:04 +00005986 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005987 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar59e476b2009-08-03 17:06:42 +00005988 Init,
Daniel Dunbar349e6fb2009-10-18 20:48:59 +00005989 ("OBJC_EHTYPE_$_" +
Daniel Dunbar07d07852009-10-18 21:17:35 +00005990 ID->getIdentifier()->getName()));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005991 }
5992
Daniel Dunbarf5f359f2009-04-14 06:00:08 +00005993 if (CGM.getLangOptions().getVisibilityMode() == LangOptions::Hidden)
Daniel Dunbar15894b72009-04-07 05:48:37 +00005994 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar710cb202010-04-25 20:39:32 +00005995 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
5996 ObjCTypes.EHTypeTy));
Daniel Dunbar8f28d012009-04-08 04:21:03 +00005997
5998 if (ForDefinition) {
5999 Entry->setSection("__DATA,__objc_const");
6000 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6001 } else {
6002 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6003 }
Daniel Dunbarb1559a42009-03-01 04:46:24 +00006004
6005 return Entry;
6006}
Daniel Dunbar59e476b2009-08-03 17:06:42 +00006007
Daniel Dunbar8b8683f2008-08-12 00:12:39 +00006008/* *** */
6009
Daniel Dunbarb036db82008-08-13 03:21:16 +00006010CodeGen::CGObjCRuntime *
6011CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
Daniel Dunbar303e2c22008-08-11 02:45:11 +00006012 return new CGObjCMac(CGM);
6013}
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006014
6015CodeGen::CGObjCRuntime *
Fariborz Jahanianb15a3d52009-01-22 23:02:58 +00006016CodeGen::CreateMacNonFragileABIObjCRuntime(CodeGen::CodeGenModule &CGM) {
Fariborz Jahanian71394042009-01-23 23:53:38 +00006017 return new CGObjCNonFragileABIMac(CGM);
Fariborz Jahanian279eda62009-01-21 22:04:16 +00006018}