blob: 3ea7e4b38c948661fc8b9b72d06a6d4c50598269 [file] [log] [blame]
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001//===------- CGObjCMac.cpp - Interface to Apple Objective-C Runtime -------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerfc8f0e12011-04-15 05:22:18 +000010// This provides Objective-C code generation targeting the Apple runtime.
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "CGObjCRuntime.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000015
Daniel Dunbar198bcb42010-03-31 01:09:11 +000016#include "CGRecordLayout.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000017#include "CodeGenModule.h"
Daniel Dunbarb7ec2462008-08-16 03:19:19 +000018#include "CodeGenFunction.h"
John McCalld16c2cf2011-02-08 08:22:06 +000019#include "CGBlocks.h"
John McCall36f893c2011-01-28 11:13:47 +000020#include "CGCleanup.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000021#include "clang/AST/ASTContext.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000022#include "clang/AST/Decl.h"
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000023#include "clang/AST/DeclObjC.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000024#include "clang/AST/RecordLayout.h"
Chris Lattner16f00492009-04-26 01:32:48 +000025#include "clang/AST/StmtObjC.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000026#include "clang/Basic/LangOptions.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000027#include "clang/Frontend/CodeGenOptions.h"
Daniel Dunbarf77ac862008-08-11 21:35:06 +000028
John McCall87bb5822010-07-31 23:20:56 +000029#include "llvm/InlineAsm.h"
30#include "llvm/IntrinsicInst.h"
Owen Anderson69243822009-07-13 04:10:07 +000031#include "llvm/LLVMContext.h"
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000032#include "llvm/Module.h"
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +000033#include "llvm/ADT/DenseSet.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000034#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallString.h"
Fariborz Jahanian191dcd72009-12-12 21:26:21 +000036#include "llvm/ADT/SmallPtrSet.h"
John McCall8e3f8612010-07-13 22:12:14 +000037#include "llvm/Support/CallSite.h"
Daniel Dunbar33063492009-09-07 00:20:42 +000038#include "llvm/Support/raw_ostream.h"
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +000039#include "llvm/Target/TargetData.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000040#include <cstdio>
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000041
42using namespace clang;
Daniel Dunbar46f45b92008-09-09 01:06:48 +000043using namespace CodeGen;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000044
Daniel Dunbar97776872009-04-22 07:32:20 +000045
Daniel Dunbarc17a4d32008-08-11 02:45:11 +000046namespace {
Daniel Dunbarbbce49b2008-08-12 00:12:39 +000047
Daniel Dunbar6bff2512009-08-03 17:06:42 +000048typedef std::vector<llvm::Constant*> ConstantVector;
Daniel Dunbarae226fa2008-08-27 02:31:56 +000049
Daniel Dunbar6bff2512009-08-03 17:06:42 +000050// FIXME: We should find a nicer way to make the labels for metadata, string
51// concatenation is lame.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +000052
Fariborz Jahanianee0af742009-01-21 22:04:16 +000053class ObjCCommonTypesHelper {
Owen Andersona1cf15f2009-07-14 23:10:40 +000054protected:
55 llvm::LLVMContext &VMContext;
Daniel Dunbar6bff2512009-08-03 17:06:42 +000056
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000057private:
John McCall0774cb82011-05-15 01:53:33 +000058 // The types of these functions don't really matter because we
59 // should always bitcast before calling them.
60
61 /// id objc_msgSend (id, SEL, ...)
62 ///
63 /// The default messenger, used for sends whose ABI is unchanged from
64 /// the all-integer/pointer case.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000065 llvm::Constant *getMessageSendFn() const {
John McCallf85e1932011-06-15 23:02:42 +000066 // Add the non-lazy-bind attribute, since objc_msgSend is likely to
67 // be called a lot.
Chris Lattner9cbe4f02011-07-09 17:41:47 +000068 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000069 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
70 params, true),
John McCallf85e1932011-06-15 23:02:42 +000071 "objc_msgSend",
72 llvm::Attribute::NonLazyBind);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000073 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000074
John McCall0774cb82011-05-15 01:53:33 +000075 /// void objc_msgSend_stret (id, SEL, ...)
76 ///
77 /// The messenger used when the return value is an aggregate returned
78 /// by indirect reference in the first argument, and therefore the
79 /// self and selector parameters are shifted over by one.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000080 llvm::Constant *getMessageSendStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000081 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000082 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy,
83 params, true),
84 "objc_msgSend_stret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000085
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000086 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +000087
John McCall0774cb82011-05-15 01:53:33 +000088 /// [double | long double] objc_msgSend_fpret(id self, SEL op, ...)
89 ///
90 /// The messenger used when the return value is returned on the x87
91 /// floating-point stack; without a special entrypoint, the nil case
92 /// would be unbalanced.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +000093 llvm::Constant *getMessageSendFpretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +000094 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +000095 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(
Owen Anderson0032b272009-08-13 21:57:51 +000096 llvm::Type::getDoubleTy(VMContext),
John McCall0774cb82011-05-15 01:53:33 +000097 params, true),
98 "objc_msgSend_fpret");
Daniel Dunbar6bff2512009-08-03 17:06:42 +000099
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000100 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000101
Anders Carlssoneea64802011-10-31 16:27:11 +0000102 /// _Complex long double objc_msgSend_fp2ret(id self, SEL op, ...)
103 ///
104 /// The messenger used when the return value is returned in two values on the
105 /// x87 floating point stack; without a special entrypoint, the nil case
106 /// would be unbalanced. Only used on 64-bit X86.
107 llvm::Constant *getMessageSendFp2retFn() const {
108 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
109 llvm::Type *longDoubleType = llvm::Type::getX86_FP80Ty(VMContext);
110 llvm::Type *resultType =
111 llvm::StructType::get(longDoubleType, longDoubleType, NULL);
112
113 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(resultType,
114 params, true),
115 "objc_msgSend_fp2ret");
116 }
117
John McCall0774cb82011-05-15 01:53:33 +0000118 /// id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
119 ///
120 /// The messenger used for super calls, which have different dispatch
121 /// semantics. The class passed is the superclass of the current
122 /// class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000123 llvm::Constant *getMessageSendSuperFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000124 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000125 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000126 params, true),
127 "objc_msgSendSuper");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000128 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000129
John McCall0774cb82011-05-15 01:53:33 +0000130 /// id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
131 ///
132 /// A slightly different messenger used for super calls. The class
133 /// passed is the current class.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000134 llvm::Constant *getMessageSendSuperFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000135 llvm::Type *params[] = { SuperPtrTy, SelectorPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000136 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000137 params, true),
138 "objc_msgSendSuper2");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000139 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000140
John McCall0774cb82011-05-15 01:53:33 +0000141 /// void objc_msgSendSuper_stret(void *stretAddr, struct objc_super *super,
142 /// SEL op, ...)
143 ///
144 /// The messenger used for super calls which return an aggregate indirectly.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000145 llvm::Constant *getMessageSendSuperStretFn() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000146 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000147 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000148 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000149 "objc_msgSendSuper_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000150 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000151
John McCall0774cb82011-05-15 01:53:33 +0000152 /// void objc_msgSendSuper2_stret(void * stretAddr, struct objc_super *super,
153 /// SEL op, ...)
154 ///
155 /// objc_msgSendSuper_stret with the super2 semantics.
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000156 llvm::Constant *getMessageSendSuperStretFn2() const {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000157 llvm::Type *params[] = { Int8PtrTy, SuperPtrTy, SelectorPtrTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000158 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000159 llvm::FunctionType::get(CGM.VoidTy, params, true),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000160 "objc_msgSendSuper2_stret");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000161 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000162
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000163 llvm::Constant *getMessageSendSuperFpretFn() const {
164 // There is no objc_msgSendSuper_fpret? How can that work?
165 return getMessageSendSuperFn();
166 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000167
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000168 llvm::Constant *getMessageSendSuperFpretFn2() const {
169 // There is no objc_msgSendSuper_fpret? How can that work?
170 return getMessageSendSuperFn2();
171 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000172
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000173protected:
174 CodeGen::CodeGenModule &CGM;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000175
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000176public:
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000177 llvm::Type *ShortTy, *IntTy, *LongTy, *LongLongTy;
Bob Wilsondc8dab62011-11-30 01:57:58 +0000178 llvm::Type *Int8PtrTy, *Int8PtrPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000179
Daniel Dunbar2bedbf82008-08-12 05:28:47 +0000180 /// ObjectPtrTy - LLVM type for object handles (typeof(id))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000181 llvm::Type *ObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000182
Fariborz Jahanian6d657c42008-11-18 20:18:11 +0000183 /// PtrObjectPtrTy - LLVM type for id *
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000184 llvm::Type *PtrObjectPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000185
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000186 /// SelectorPtrTy - LLVM type for selector handles (typeof(SEL))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000187 llvm::Type *SelectorPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000188 /// ProtocolPtrTy - LLVM type for external protocol handles
189 /// (typeof(Protocol))
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000190 llvm::Type *ExternalProtocolPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000191
Daniel Dunbar19cd87e2008-08-30 03:02:31 +0000192 // SuperCTy - clang type for struct objc_super.
193 QualType SuperCTy;
194 // SuperPtrCTy - clang type for struct objc_super *.
195 QualType SuperPtrCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000196
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000197 /// SuperTy - LLVM type for struct objc_super.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000198 llvm::StructType *SuperTy;
Daniel Dunbar14c80b72008-08-23 09:25:55 +0000199 /// SuperPtrTy - LLVM type for struct objc_super *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000200 llvm::Type *SuperPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000201
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000202 /// PropertyTy - LLVM type for struct objc_property (struct _prop_t
203 /// in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000204 llvm::StructType *PropertyTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000205
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000206 /// PropertyListTy - LLVM type for struct objc_property_list
207 /// (_prop_list_t in GCC parlance).
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000208 llvm::StructType *PropertyListTy;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000209 /// PropertyListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000210 llvm::Type *PropertyListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000211
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000212 // MethodTy - LLVM type for struct objc_method.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000213 llvm::StructType *MethodTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000214
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000215 /// CacheTy - LLVM type for struct objc_cache.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000216 llvm::Type *CacheTy;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000217 /// CachePtrTy - LLVM type for struct objc_cache *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000218 llvm::Type *CachePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000219
Chris Lattner72db6c32009-04-22 02:44:54 +0000220 llvm::Constant *getGetPropertyFn() {
221 CodeGen::CodeGenTypes &Types = CGM.getTypes();
222 ASTContext &Ctx = CGM.getContext();
223 // id objc_getProperty (id, SEL, ptrdiff_t, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000224 SmallVector<CanQualType,4> Params;
John McCallead608a2010-02-26 00:48:12 +0000225 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
226 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000227 Params.push_back(IdType);
228 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000229 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000230 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000231 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000232 Types.GetFunctionType(Types.getFunctionInfo(IdType, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000233 FunctionType::ExtInfo()),
234 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000235 return CGM.CreateRuntimeFunction(FTy, "objc_getProperty");
236 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000237
Chris Lattner72db6c32009-04-22 02:44:54 +0000238 llvm::Constant *getSetPropertyFn() {
239 CodeGen::CodeGenTypes &Types = CGM.getTypes();
240 ASTContext &Ctx = CGM.getContext();
241 // void objc_setProperty (id, SEL, ptrdiff_t, id, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000242 SmallVector<CanQualType,6> Params;
John McCallead608a2010-02-26 00:48:12 +0000243 CanQualType IdType = Ctx.getCanonicalParamType(Ctx.getObjCIdType());
244 CanQualType SelType = Ctx.getCanonicalParamType(Ctx.getObjCSelType());
Chris Lattner72db6c32009-04-22 02:44:54 +0000245 Params.push_back(IdType);
246 Params.push_back(SelType);
David Chisnallab5824e2011-03-22 20:03:13 +0000247 Params.push_back(Ctx.getPointerDiffType()->getCanonicalTypeUnqualified());
Chris Lattner72db6c32009-04-22 02:44:54 +0000248 Params.push_back(IdType);
249 Params.push_back(Ctx.BoolTy);
250 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000251 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000252 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000253 FunctionType::ExtInfo()),
254 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000255 return CGM.CreateRuntimeFunction(FTy, "objc_setProperty");
256 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000257
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000258
259 llvm::Constant *getCopyStructFn() {
260 CodeGen::CodeGenTypes &Types = CGM.getTypes();
261 ASTContext &Ctx = CGM.getContext();
262 // void objc_copyStruct (void *, const void *, size_t, bool, bool)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000263 SmallVector<CanQualType,5> Params;
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000264 Params.push_back(Ctx.VoidPtrTy);
265 Params.push_back(Ctx.VoidPtrTy);
266 Params.push_back(Ctx.LongTy);
267 Params.push_back(Ctx.BoolTy);
268 Params.push_back(Ctx.BoolTy);
Chris Lattner2acc6e32011-07-18 04:24:23 +0000269 llvm::FunctionType *FTy =
Fariborz Jahanian6cc59062010-04-12 18:18:10 +0000270 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
271 FunctionType::ExtInfo()),
272 false);
273 return CGM.CreateRuntimeFunction(FTy, "objc_copyStruct");
274 }
275
Fariborz Jahaniane3173022012-01-06 18:07:23 +0000276 /// This routine declares and returns address of:
277 /// void objc_copyCppObjectAtomic(
278 /// void *dest, const void *src,
279 /// void (*copyHelper) (void *dest, const void *source));
280 llvm::Constant *getCppAtomicObjectFunction() {
281 CodeGen::CodeGenTypes &Types = CGM.getTypes();
282 ASTContext &Ctx = CGM.getContext();
283 /// void objc_copyCppObjectAtomic(void *dest, const void *src, void *helper);
284 SmallVector<CanQualType,3> Params;
285 Params.push_back(Ctx.VoidPtrTy);
286 Params.push_back(Ctx.VoidPtrTy);
287 Params.push_back(Ctx.VoidPtrTy);
288 llvm::FunctionType *FTy =
289 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
290 FunctionType::ExtInfo()),
291 false);
292 return CGM.CreateRuntimeFunction(FTy, "objc_copyCppObjectAtomic");
293 }
294
Chris Lattner72db6c32009-04-22 02:44:54 +0000295 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000296 CodeGen::CodeGenTypes &Types = CGM.getTypes();
297 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000298 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000299 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000300 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000301 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000302 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000303 FunctionType::ExtInfo()),
304 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000305 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
306 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000307
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000308 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000309 llvm::Constant *getGcReadWeakFn() {
310 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000311 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000312 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000313 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000314 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000315 }
316
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000317 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000318 llvm::Constant *getGcAssignWeakFn() {
319 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000320 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000321 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000322 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000323 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
324 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000325
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000326 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000327 llvm::Constant *getGcAssignGlobalFn() {
328 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000329 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000330 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000331 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000332 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
333 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000334
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000335 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
336 llvm::Constant *getGcAssignThreadLocalFn() {
337 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000338 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000339 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000340 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000341 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
342 }
343
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000344 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000345 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000346 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000347 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
348 CGM.PtrDiffTy };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000349 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000350 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000351 return CGM.CreateRuntimeFunction(FTy, "objc_assign_ivar");
352 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000353
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000354 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
355 llvm::Constant *GcMemmoveCollectableFn() {
356 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000357 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000358 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000359 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
360 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000361
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000362 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000363 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000364 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000365 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000366 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000367 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000368 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
369 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000370
371 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000372 llvm::Constant *getExceptionThrowFn() {
373 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000374 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000375 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000376 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000377 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
378 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000379
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000380 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
381 llvm::Constant *getExceptionRethrowFn() {
382 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000383 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000384 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
385 }
386
Daniel Dunbar1c566672009-02-24 01:43:46 +0000387 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000388 llvm::Constant *getSyncEnterFn() {
389 // void objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000390 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000391 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000392 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000393 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
394 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000395
Daniel Dunbar1c566672009-02-24 01:43:46 +0000396 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000397 llvm::Constant *getSyncExitFn() {
398 // void objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000399 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000400 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000401 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000402 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
403 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000404
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000405 llvm::Constant *getSendFn(bool IsSuper) const {
406 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
407 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000408
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000409 llvm::Constant *getSendFn2(bool IsSuper) const {
410 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
411 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000412
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000413 llvm::Constant *getSendStretFn(bool IsSuper) const {
414 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
415 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000416
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000417 llvm::Constant *getSendStretFn2(bool IsSuper) const {
418 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
419 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000420
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000421 llvm::Constant *getSendFpretFn(bool IsSuper) const {
422 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
423 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000424
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000425 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
426 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
427 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000428
Anders Carlssoneea64802011-10-31 16:27:11 +0000429 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
430 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
431 }
432
433 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
434 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
435 }
436
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000437 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
438 ~ObjCCommonTypesHelper(){}
439};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000440
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000441/// ObjCTypesHelper - Helper class that encapsulates lazy
442/// construction of varies types used during ObjC generation.
443class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000444public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000445 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000446 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000447 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000448 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000449 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000450 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000451
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000452 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000453 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000454 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000455 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000456 /// ProtocolExtensionTy - LLVM type for struct
457 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000458 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000459 /// ProtocolExtensionTy - LLVM type for struct
460 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000461 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000462 /// MethodDescriptionTy - LLVM type for struct
463 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000464 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000465 /// MethodDescriptionListTy - LLVM type for struct
466 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000467 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000468 /// MethodDescriptionListPtrTy - LLVM type for struct
469 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000470 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000471 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000472 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000473 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000474 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000475 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000476 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000477 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000478 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000479 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000480 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000481 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000482 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000483 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000484 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000485 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000486 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000487 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000488 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000489 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000490 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000491 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000492 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000493 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000494 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000495
Anders Carlsson124526b2008-09-09 10:10:21 +0000496 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000497 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000498
Anders Carlsson124526b2008-09-09 10:10:21 +0000499 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000500 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000501 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000502 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000503 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000504 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000505 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000506
507 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000508 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000509 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000510 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000511 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000512 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000513 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000514
515 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000516 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000517 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000518 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000519 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000520 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000521 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000522
Anders Carlsson124526b2008-09-09 10:10:21 +0000523 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000524 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000525 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000526 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000527 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000528 "objc_exception_match");
529
Chris Lattner34b02a12009-04-22 02:26:14 +0000530 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000531
Anders Carlsson124526b2008-09-09 10:10:21 +0000532 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000533 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000534 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000535 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000536 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
537 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000538 "_setjmp",
539 llvm::Attribute::ReturnsTwice);
Chris Lattner34b02a12009-04-22 02:26:14 +0000540 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000541
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000542public:
543 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000544 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000545};
546
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000547/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000548/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000549class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000550public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000551
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000552 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000553 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000554
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000555 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000556 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000557
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000558 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000559 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000560
Daniel Dunbar948e2582009-02-15 07:36:20 +0000561 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000562 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000563
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000564 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000565 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000566
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000567 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000568 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000569
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000570 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000571 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000572
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000573 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000574 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000575
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000576 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000577 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000578
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000579 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000580 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000581
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000582 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000583 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000584
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000585 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000586 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000587
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000588 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000589 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000590
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000591 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000592 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000593
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000594 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000595
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000596 // MessageRefTy - LLVM for:
597 // struct _message_ref_t {
598 // IMP messenger;
599 // SEL name;
600 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000601 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000602 // MessageRefCTy - clang type for struct _message_ref_t
603 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000604
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000605 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000606 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000607 // MessageRefCPtrTy - clang type for struct _message_ref_t*
608 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000609
Fariborz Jahanianef163782009-02-05 01:13:09 +0000610 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000611 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000612
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000613 // SuperMessageRefTy - LLVM for:
614 // struct _super_message_ref_t {
615 // SUPER_IMP messenger;
616 // SEL name;
617 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000618 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000619
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000620 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000621 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000622
Chris Lattner1c02f862009-04-22 02:53:24 +0000623 llvm::Constant *getMessageSendFixupFn() {
624 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000625 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000626 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000627 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000628 "objc_msgSend_fixup");
629 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000630
Chris Lattner1c02f862009-04-22 02:53:24 +0000631 llvm::Constant *getMessageSendFpretFixupFn() {
632 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000633 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000634 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000635 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000636 "objc_msgSend_fpret_fixup");
637 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000638
Chris Lattner1c02f862009-04-22 02:53:24 +0000639 llvm::Constant *getMessageSendStretFixupFn() {
640 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000641 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000642 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000643 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000644 "objc_msgSend_stret_fixup");
645 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000646
Chris Lattner1c02f862009-04-22 02:53:24 +0000647 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000648 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000649 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000650 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000651 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000652 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000653 "objc_msgSendSuper2_fixup");
654 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000655
Chris Lattner1c02f862009-04-22 02:53:24 +0000656 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000657 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000658 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000659 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000660 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000661 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000662 "objc_msgSendSuper2_stret_fixup");
663 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000664
Chris Lattner8a569112009-04-22 02:15:23 +0000665 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000666 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000667 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000668
Chris Lattner8a569112009-04-22 02:15:23 +0000669 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000670
Chris Lattner8a569112009-04-22 02:15:23 +0000671 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000672 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000673 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000674 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000675 "objc_begin_catch");
676 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000677
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000678 llvm::StructType *EHTypeTy;
679 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000680
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000681 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
682 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000683};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000684
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000685class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000686public:
687 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000688 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000689 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000690 unsigned ivar_bytepos;
691 unsigned ivar_size;
692 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000693 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000694
695 // Allow sorting based on byte pos.
696 bool operator<(const GC_IVAR &b) const {
697 return ivar_bytepos < b.ivar_bytepos;
698 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000699 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000700
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000701 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000702 public:
703 unsigned skip;
704 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000705 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000706 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000707 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000708
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000709protected:
710 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000711 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000712 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000713 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000714
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000715 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000716 SmallVector<GC_IVAR, 16> SkipIvars;
717 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000718
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000719 /// LazySymbols - Symbols to generate a lazy reference for. See
720 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000721 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000722
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000723 /// DefinedSymbols - External symbols which are defined by this
724 /// module. The symbols in this list and LazySymbols are used to add
725 /// special linker symbols which ensure that Objective-C modules are
726 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000727 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000728
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000729 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000730 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000731
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000732 /// MethodVarNames - uniqued method variable names.
733 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000734
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000735 /// DefinedCategoryNames - list of category names in form Class_Category.
736 llvm::SetVector<std::string> DefinedCategoryNames;
737
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000738 /// MethodVarTypes - uniqued method type signatures. We have to use
739 /// a StringMap here because have no other unique reference.
740 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000741
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000742 /// MethodDefinitions - map of methods which have been defined in
743 /// this translation unit.
744 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000745
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000746 /// PropertyNames - uniqued method variable names.
747 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000748
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000749 /// ClassReferences - uniqued class references.
750 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000751
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000752 /// SelectorReferences - uniqued selector references.
753 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000754
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000755 /// Protocols - Protocols for which an objc_protocol structure has
756 /// been emitted. Forward declarations are handled by creating an
757 /// empty structure whose initializer is filled in when/if defined.
758 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000759
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000760 /// DefinedProtocols - Protocols which have actually been
761 /// defined. We should not need this, see FIXME in GenerateProtocol.
762 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000763
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000764 /// DefinedClasses - List of defined classes.
765 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000766
767 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
768 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000769
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000770 /// DefinedCategories - List of defined categories.
771 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000772
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000773 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
774 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000775
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000776 /// GetNameForMethod - Return a name for the given method.
777 /// \param[out] NameOut - The return value.
778 void GetNameForMethod(const ObjCMethodDecl *OMD,
779 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000780 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000781
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000782 /// GetMethodVarName - Return a unique constant for the given
783 /// selector's name. The return value has type char *.
784 llvm::Constant *GetMethodVarName(Selector Sel);
785 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000786
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000787 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000788 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000789
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000790 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000791 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
792 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000793 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000794
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000795 /// GetPropertyName - Return a unique constant for the given
796 /// name. The return value has type char *.
797 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000798
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000799 // FIXME: This can be dropped once string functions are unified.
800 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
801 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000802
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000803 /// GetClassName - Return a unique constant for the given selector's
804 /// name. The return value has type char *.
805 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000806
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000807 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
808
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000809 /// BuildIvarLayout - Builds ivar layout bitmap for the class
810 /// implementation for the __strong or __weak case.
811 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000812 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
813 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000814
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000815 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000816
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000817 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000818 unsigned int BytePos, bool ForStrongLayout,
819 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000820 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000821 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000822 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +0000823 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000824 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000825 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000826
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000827 /// GetIvarLayoutName - Returns a unique constant for the given
828 /// ivar layout bitmap.
829 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
830 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000831
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000832 /// EmitPropertyList - Emit the given property list. The return
833 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000834 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000835 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000836 const ObjCContainerDecl *OCD,
837 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000838
Bob Wilsondc8dab62011-11-30 01:57:58 +0000839 /// EmitProtocolMethodTypes - Generate the array of extended method type
840 /// strings. The return value has type Int8PtrPtrTy.
841 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
842 const ConstantVector &MethodTypes,
843 const ObjCCommonTypesHelper &ObjCTypes);
844
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000845 /// PushProtocolProperties - Push protocol's property on the input stack.
846 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
847 std::vector<llvm::Constant*> &Properties,
848 const Decl *Container,
849 const ObjCProtocolDecl *PROTO,
850 const ObjCCommonTypesHelper &ObjCTypes);
851
Fariborz Jahanianda320092009-01-29 19:24:30 +0000852 /// GetProtocolRef - Return a reference to the internal protocol
853 /// description, creating an empty one if it has not been
854 /// defined. The return value has type ProtocolPtrTy.
855 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000856
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000857 /// CreateMetadataVar - Create a global variable with internal
858 /// linkage for use by the Objective-C runtime.
859 ///
860 /// This is a convenience wrapper which not only creates the
861 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000862 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000863 ///
864 /// \param Name - The variable name.
865 /// \param Init - The variable initializer; this is also used to
866 /// define the type of the variable.
867 /// \param Section - The section the variable should go into, or 0.
868 /// \param Align - The alignment for the variable, or 0.
869 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000870 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000871 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000872 llvm::Constant *Init,
873 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000874 unsigned Align,
875 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000876
John McCall944c8432011-05-14 03:10:52 +0000877 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
878 ReturnValueSlot Return,
879 QualType ResultType,
880 llvm::Value *Sel,
881 llvm::Value *Arg0,
882 QualType Arg0Ty,
883 bool IsSuper,
884 const CallArgList &CallArgs,
885 const ObjCMethodDecl *OMD,
886 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000887
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000888 /// EmitImageInfo - Emit the image info marker used to encode some module
889 /// level information.
890 void EmitImageInfo();
891
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000892public:
Owen Anderson69243822009-07-13 04:10:07 +0000893 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000894 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000895
David Chisnall0d13f6f2010-01-23 02:40:42 +0000896 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000897
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000898 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
899 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000900
Fariborz Jahanianda320092009-01-29 19:24:30 +0000901 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000902
Fariborz Jahanianda320092009-01-29 19:24:30 +0000903 /// GetOrEmitProtocol - Get the protocol object for the given
904 /// declaration, emitting it if necessary. The return value has type
905 /// ProtocolPtrTy.
906 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000907
Fariborz Jahanianda320092009-01-29 19:24:30 +0000908 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
909 /// object for the given declaration, emitting it if needed. These
910 /// forward references will be filled in with empty bodies if no
911 /// definition is seen. The return value has type ProtocolPtrTy.
912 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000913 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
914 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000915
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000916};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000917
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000918class CGObjCMac : public CGObjCCommonMac {
919private:
920 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000921
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000922 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000923 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000924 void EmitModuleInfo();
925
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000926 /// EmitModuleSymols - Emit module symbols, the list of defined
927 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000928 llvm::Constant *EmitModuleSymbols();
929
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000930 /// FinishModule - Write out global data structures at the end of
931 /// processing a translation unit.
932 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000933
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000934 /// EmitClassExtension - Generate the class extension structure used
935 /// to store the weak ivar layout and properties. The return value
936 /// has type ClassExtensionPtrTy.
937 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
938
939 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
940 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000941 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000942 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000943
John McCallf85e1932011-06-15 23:02:42 +0000944 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
945 IdentifierInfo *II);
946
947 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
948
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000949 /// EmitSuperClassRef - Emits reference to class's main metadata class.
950 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000951
952 /// EmitIvarList - Emit the ivar list for the given
953 /// implementation. If ForClass is true the list of class ivars
954 /// (i.e. metaclass ivars) is emitted, otherwise the list of
955 /// interface ivars will be emitted. The return value has type
956 /// IvarListPtrTy.
957 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000958 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000959
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000960 /// EmitMetaClass - Emit a forward reference to the class structure
961 /// for the metaclass of the given interface. The return value has
962 /// type ClassPtrTy.
963 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
964
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000965 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000966 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000967 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
968 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000969 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000970
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000971 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000972
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000973 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000974
975 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000976 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000977 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000978 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000979 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000980
981 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000982 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000983 /// - TypeName: The name for the type containing the methods.
984 /// - IsProtocol: True iff these methods are for a protocol.
985 /// - ClassMethds: True iff these are class methods.
986 /// - Required: When true, only "required" methods are
987 /// listed. Similarly, when false only "optional" methods are
988 /// listed. For classes this should always be true.
989 /// - begin, end: The method list to output.
990 ///
991 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000992 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000993 const char *Section,
994 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000995
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000996 /// GetOrEmitProtocol - Get the protocol object for the given
997 /// declaration, emitting it if necessary. The return value has type
998 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000999 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001000
1001 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1002 /// object for the given declaration, emitting it if needed. These
1003 /// forward references will be filled in with empty bodies if no
1004 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +00001005 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001006
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001007 /// EmitProtocolExtension - Generate the protocol extension
1008 /// structure used to store optional instance and class methods, and
1009 /// protocol properties. The return value has type
1010 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001011 llvm::Constant *
1012 EmitProtocolExtension(const ObjCProtocolDecl *PD,
1013 const ConstantVector &OptInstanceMethods,
Bob Wilsondc8dab62011-11-30 01:57:58 +00001014 const ConstantVector &OptClassMethods,
1015 const ConstantVector &MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001016
1017 /// EmitProtocolList - Generate the list of referenced
1018 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001019 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001020 ObjCProtocolDecl::protocol_iterator begin,
1021 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001022
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001023 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1024 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001025 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1026 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001027
1028public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001029 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001030
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001031 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001032
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001033 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001034 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001035 QualType ResultType,
1036 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001037 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001038 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001039 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001040 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001041
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001042 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001043 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001044 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001045 QualType ResultType,
1046 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001047 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001048 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001049 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001050 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001051 const CallArgList &CallArgs,
1052 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001053
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001054 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001055 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001056
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001057 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1058 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001059
1060 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1061 /// untyped one.
1062 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1063 const ObjCMethodDecl *Method);
1064
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001065 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001066
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001067 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001068
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001069 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001070
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001071 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001072 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001073
Chris Lattner74391b42009-03-22 21:03:39 +00001074 virtual llvm::Constant *GetPropertyGetFunction();
1075 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001076 virtual llvm::Constant *GetGetStructFunction();
1077 virtual llvm::Constant *GetSetStructFunction();
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001078 virtual llvm::Constant *GetCppAtomicObjectFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001079 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001080
John McCallf1549f62010-07-06 01:34:17 +00001081 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1082 const ObjCAtTryStmt &S);
1083 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1084 const ObjCAtSynchronizedStmt &S);
1085 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001086 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1087 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001088 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001089 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001090 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001091 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001092 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001093 llvm::Value *src, llvm::Value *dest,
1094 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001095 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001096 llvm::Value *src, llvm::Value *dest,
1097 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001098 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1099 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001100 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1101 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001102 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001103
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001104 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1105 QualType ObjectTy,
1106 llvm::Value *BaseValue,
1107 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001108 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001109 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001110 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001111 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001112
1113 /// GetClassGlobal - Return the global variable for the Objective-C
1114 /// class of the given name.
1115 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001116 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001117 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001118};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001119
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001120class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001121private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001122 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001123 llvm::GlobalVariable* ObjCEmptyCacheVar;
1124 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001125
Daniel Dunbar11394522009-04-18 08:51:00 +00001126 /// SuperClassReferences - uniqued super class references.
1127 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001128
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001129 /// MetaClassReferences - uniqued meta class references.
1130 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001131
1132 /// EHTypeReferences - uniqued class ehtype references.
1133 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001134
John McCall944c8432011-05-14 03:10:52 +00001135 /// VTableDispatchMethods - List of methods for which we generate
1136 /// vtable-based message dispatch.
1137 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001138
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001139 /// DefinedMetaClasses - List of defined meta-classes.
1140 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1141
John McCall944c8432011-05-14 03:10:52 +00001142 /// isVTableDispatchedSelector - Returns true if SEL is a
1143 /// vtable-based selector.
1144 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001145
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001146 /// FinishNonFragileABIModule - Write out global data structures at the end of
1147 /// processing a translation unit.
1148 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001149
Daniel Dunbar463b8762009-05-15 21:48:48 +00001150 /// AddModuleClassList - Add the given list of class pointers to the
1151 /// module with the provided symbol and section names.
1152 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1153 const char *SymbolName,
1154 const char *SectionName);
1155
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001156 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1157 unsigned InstanceStart,
1158 unsigned InstanceSize,
1159 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001160 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001161 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001162 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001163 llvm::Constant *ClassRoGV,
1164 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001165
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001166 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001167
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001168 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001169
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001170 /// EmitMethodList - Emit the method list for the given
1171 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001172 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001173 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001174 const ConstantVector &Methods);
1175 /// EmitIvarList - Emit the ivar list for the given
1176 /// implementation. If ForClass is true the list of class ivars
1177 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1178 /// interface ivars will be emitted. The return value has type
1179 /// IvarListnfABIPtrTy.
1180 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001181
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001182 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001183 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001184 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001185
Fariborz Jahanianda320092009-01-29 19:24:30 +00001186 /// GetOrEmitProtocol - Get the protocol object for the given
1187 /// declaration, emitting it if necessary. The return value has type
1188 /// ProtocolPtrTy.
1189 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001190
Fariborz Jahanianda320092009-01-29 19:24:30 +00001191 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1192 /// object for the given declaration, emitting it if needed. These
1193 /// forward references will be filled in with empty bodies if no
1194 /// definition is seen. The return value has type ProtocolPtrTy.
1195 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001196
Fariborz Jahanianda320092009-01-29 19:24:30 +00001197 /// EmitProtocolList - Generate the list of referenced
1198 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001199 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001200 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001201 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001202
John McCall944c8432011-05-14 03:10:52 +00001203 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1204 ReturnValueSlot Return,
1205 QualType ResultType,
1206 Selector Sel,
1207 llvm::Value *Receiver,
1208 QualType Arg0Ty,
1209 bool IsSuper,
1210 const CallArgList &CallArgs,
1211 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001212
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001213 /// GetClassGlobal - Return the global variable for the Objective-C
1214 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001215 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001216
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001217 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001218 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001219 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001220 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001221
1222 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1223 IdentifierInfo *II);
1224
1225 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001226
Daniel Dunbar11394522009-04-18 08:51:00 +00001227 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1228 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001229 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1230 const ObjCInterfaceDecl *ID);
1231
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001232 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1233 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001234 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001235 const ObjCInterfaceDecl *ID);
1236
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001237 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1238 /// the given ivar.
1239 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001240 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001241 const ObjCInterfaceDecl *ID,
1242 const ObjCIvarDecl *Ivar);
1243
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001244 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1245 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001246 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1247 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001248
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001249 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001250 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001251 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001252 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001253
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001254 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001255 return "OBJC_METACLASS_$_";
1256 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001257
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001258 const char *getClassSymbolPrefix() const {
1259 return "OBJC_CLASS_$_";
1260 }
1261
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001262 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001263 uint32_t &InstanceStart,
1264 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001265
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001266 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001267 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001268 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1269 return CGM.getContext().Selectors.getSelector(0, &II);
1270 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001271
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001272 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001273 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1274 return CGM.getContext().Selectors.getSelector(1, &II);
1275 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001276
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001277 /// ImplementationIsNonLazy - Check whether the given category or
1278 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001279 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001280
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001281public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001282 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001283 // FIXME. All stubs for now!
1284 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001285
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001286 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001287 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001288 QualType ResultType,
1289 Selector Sel,
1290 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001291 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001292 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001293 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001294
1295 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001296 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001297 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001298 QualType ResultType,
1299 Selector Sel,
1300 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001301 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001302 llvm::Value *Receiver,
1303 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001304 const CallArgList &CallArgs,
1305 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001306
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001307 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001308 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001309
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001310 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1311 bool lvalue = false)
1312 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001313
1314 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1315 /// untyped one.
1316 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1317 const ObjCMethodDecl *Method)
1318 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001319
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001320 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001321
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001322 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001323 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001324 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001325
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001326 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001327
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001328 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001329 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001330 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001331 virtual llvm::Constant *GetPropertySetFunction() {
1332 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001333 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001334
David Chisnall8fac25d2010-12-26 22:13:16 +00001335 virtual llvm::Constant *GetSetStructFunction() {
1336 return ObjCTypes.getCopyStructFn();
1337 }
1338 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001339 return ObjCTypes.getCopyStructFn();
1340 }
Fariborz Jahaniane3173022012-01-06 18:07:23 +00001341 virtual llvm::Constant *GetCppAtomicObjectFunction() {
1342 return ObjCTypes.getCppAtomicObjectFunction();
1343 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001344
Chris Lattner74391b42009-03-22 21:03:39 +00001345 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001346 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001347 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001348
John McCallf1549f62010-07-06 01:34:17 +00001349 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1350 const ObjCAtTryStmt &S);
1351 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1352 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001353 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001354 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001355 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001356 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001357 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001358 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001359 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001360 llvm::Value *src, llvm::Value *dest,
1361 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001362 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001363 llvm::Value *src, llvm::Value *dest,
1364 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001365 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001366 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001367 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1368 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001369 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001370 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1371 QualType ObjectTy,
1372 llvm::Value *BaseValue,
1373 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001374 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001375 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001376 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001377 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001378};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001379
John McCallcba681a2011-05-14 21:12:11 +00001380/// A helper class for performing the null-initialization of a return
1381/// value.
1382struct NullReturnState {
1383 llvm::BasicBlock *NullBB;
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001384 llvm::BasicBlock *callBB;
1385 NullReturnState() : NullBB(0), callBB(0) {}
John McCallcba681a2011-05-14 21:12:11 +00001386
1387 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1388 // Make blocks for the null-init and call edges.
1389 NullBB = CGF.createBasicBlock("msgSend.nullinit");
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001390 callBB = CGF.createBasicBlock("msgSend.call");
John McCallcba681a2011-05-14 21:12:11 +00001391
1392 // Check for a null receiver and, if there is one, jump to the
1393 // null-init test.
1394 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1395 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1396
1397 // Otherwise, start performing the call.
1398 CGF.EmitBlock(callBB);
1399 }
1400
1401 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1402 if (!NullBB) return result;
1403
1404 // Finish the call path.
1405 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1406 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1407
1408 // Emit the null-init block and perform the null-initialization there.
1409 CGF.EmitBlock(NullBB);
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001410 if (!resultType->isAnyComplexType()) {
1411 assert(result.isAggregate() && "null init of non-aggregate result?");
1412 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1413 // Jump to the continuation block.
1414 CGF.EmitBlock(contBB);
1415 return result;
1416 }
John McCallcba681a2011-05-14 21:12:11 +00001417
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001418 // _Complex type
1419 // FIXME. Now easy to handle any other scalar type whose result is returned
1420 // in memory due to ABI limitations.
John McCallcba681a2011-05-14 21:12:11 +00001421 CGF.EmitBlock(contBB);
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001422 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1423 llvm::Type *MemberType = CallCV.first->getType();
1424 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1425 // Create phi instruction for scalar complex value.
1426 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1427 PHIReal->addIncoming(ZeroCV, NullBB);
1428 PHIReal->addIncoming(CallCV.first, callBB);
1429 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1430 PHIImag->addIncoming(ZeroCV, NullBB);
1431 PHIImag->addIncoming(CallCV.second, callBB);
1432 return RValue::getComplex(PHIReal, PHIImag);
John McCallcba681a2011-05-14 21:12:11 +00001433 }
1434};
1435
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001436} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001437
1438/* *** Helper Functions *** */
1439
1440/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001441static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001442 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001443 unsigned idx0,
1444 unsigned idx1) {
1445 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001446 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1447 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001448 };
Jay Foada5c04342011-07-21 14:31:17 +00001449 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001450}
1451
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001452/// hasObjCExceptionAttribute - Return true if this class or any super
1453/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001454static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001455 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001456 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001457 return true;
1458 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001459 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001460 return false;
1461}
1462
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001463/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001464
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001465CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001466 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001467 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001468 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001469}
1470
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001471/// GetClass - Return a reference to the class for the given interface
1472/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001473llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001474 const ObjCInterfaceDecl *ID) {
1475 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001476}
1477
1478/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001479llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1480 bool lval) {
1481 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001482}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001483llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001484 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001485 return EmitSelector(Builder, Method->getSelector());
1486}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001487
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001488llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001489 if (T->isObjCIdType() ||
1490 T->isObjCQualifiedIdType()) {
1491 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001492 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001493 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001494 if (T->isObjCClassType() ||
1495 T->isObjCQualifiedClassType()) {
1496 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001497 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001498 }
1499 if (T->isObjCObjectPointerType())
1500 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1501
John McCall5a180392010-07-24 00:37:23 +00001502 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1503 return 0;
1504}
1505
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001506/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001507/*
1508 struct __builtin_CFString {
1509 const int *isa; // point to __CFConstantStringClassReference
1510 int flags;
1511 const char *str;
1512 long length;
1513 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001514*/
1515
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001516/// or Generate a constant NSString object.
1517/*
1518 struct __builtin_NSString {
1519 const int *isa; // point to __NSConstantStringClassReference
1520 const char *str;
1521 unsigned int length;
1522 };
1523*/
1524
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001525llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001526 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001527 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1528 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001529 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001530}
1531
1532/// Generates a message send where the super is the receiver. This is
1533/// a message send to self with special delivery semantics indicating
1534/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001535CodeGen::RValue
1536CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001537 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001538 QualType ResultType,
1539 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001540 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001541 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001542 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001543 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001544 const CodeGen::CallArgList &CallArgs,
1545 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001546 // Create and init a super structure; this is a (receiver, class)
1547 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001548 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001549 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001550 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001551 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001552 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001553 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001554
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001555 // If this is a class message the metaclass is passed as the target.
1556 llvm::Value *Target;
1557 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001558 if (isCategoryImpl) {
1559 // Message sent to 'super' in a class method defined in a category
1560 // implementation requires an odd treatment.
1561 // If we are in a class method, we must retrieve the
1562 // _metaclass_ for the current class, pointed at by
1563 // the class's "isa" pointer. The following assumes that
1564 // isa" is the first ivar in a class (which it must be).
1565 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1566 Target = CGF.Builder.CreateStructGEP(Target, 0);
1567 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001568 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001569 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1570 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1571 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1572 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001573 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001574 }
1575 else if (isCategoryImpl)
1576 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1577 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001578 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1579 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1580 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001581 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001582 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1583 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001584 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001585 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001586 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001587 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001588 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001589 return EmitMessageSend(CGF, Return, ResultType,
1590 EmitSelector(CGF.Builder, Sel),
1591 ObjCSuper, ObjCTypes.SuperPtrCTy,
1592 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001593}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001594
1595/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001596CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001597 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001598 QualType ResultType,
1599 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001600 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001601 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001602 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001603 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001604 return EmitMessageSend(CGF, Return, ResultType,
1605 EmitSelector(CGF.Builder, Sel),
1606 Receiver, CGF.getContext().getObjCIdType(),
1607 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001608}
1609
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001610CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001611CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1612 ReturnValueSlot Return,
1613 QualType ResultType,
1614 llvm::Value *Sel,
1615 llvm::Value *Arg0,
1616 QualType Arg0Ty,
1617 bool IsSuper,
1618 const CallArgList &CallArgs,
1619 const ObjCMethodDecl *Method,
1620 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001621 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001622 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001623 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001624 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1625 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001626 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001627
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001628 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001629 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001630 FunctionType::ExtInfo());
Chris Lattner2acc6e32011-07-18 04:24:23 +00001631 llvm::FunctionType *FTy =
Daniel Dunbar67939662009-09-17 04:01:40 +00001632 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001633
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001634 if (Method)
1635 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1636 CGM.getContext().getCanonicalType(ResultType) &&
1637 "Result type mismatch!");
1638
John McCallcba681a2011-05-14 21:12:11 +00001639 NullReturnState nullReturn;
1640
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001641 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001642 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallcba681a2011-05-14 21:12:11 +00001643 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001644 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001645 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001646 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1647 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1648 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001649 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1650 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1651 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001652 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001653 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001654 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001655 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001656 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallcba681a2011-05-14 21:12:11 +00001657 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1658 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001659}
1660
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001661static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1662 if (FQT.isObjCGCStrong())
1663 return Qualifiers::Strong;
1664
John McCallf85e1932011-06-15 23:02:42 +00001665 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001666 return Qualifiers::Weak;
1667
1668 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1669 return Qualifiers::Strong;
1670
1671 if (const PointerType *PT = FQT->getAs<PointerType>())
1672 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1673
1674 return Qualifiers::GCNone;
1675}
1676
John McCall6b5a61b2011-02-07 10:33:21 +00001677llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1678 const CGBlockInfo &blockInfo) {
1679 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001680 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001681
Douglas Gregore289d812011-09-13 17:21:33 +00001682 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00001683 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001684 return nullPtr;
1685
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001686 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001687 SkipIvars.clear();
1688 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001689 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1690 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001691
Fariborz Jahanian81979822010-09-09 00:21:45 +00001692 // __isa is the first field in block descriptor and must assume by runtime's
1693 // convention that it is GC'able.
1694 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001695
1696 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1697
1698 // Calculate the basic layout of the block structure.
1699 const llvm::StructLayout *layout =
1700 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1701
1702 // Ignore the optional 'this' capture: C++ objects are not assumed
1703 // to be GC'ed.
1704
1705 // Walk the captured variables.
1706 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1707 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1708 const VarDecl *variable = ci->getVariable();
1709 QualType type = variable->getType();
1710
1711 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1712
1713 // Ignore constant captures.
1714 if (capture.isConstant()) continue;
1715
1716 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1717
1718 // __block variables are passed by their descriptor address.
1719 if (ci->isByRef()) {
1720 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001721 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001722 }
1723
1724 assert(!type->isArrayType() && "array variable should not be caught");
1725 if (const RecordType *record = type->getAs<RecordType>()) {
1726 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001727 continue;
1728 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001729
John McCall6b5a61b2011-02-07 10:33:21 +00001730 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1731 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1732
1733 if (GCAttr == Qualifiers::Strong)
1734 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1735 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001736 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001737 SkipIvars.push_back(GC_IVAR(fieldOffset,
1738 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001739 }
1740
1741 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001742 return nullPtr;
1743
1744 // Sort on byte position; captures might not be allocated in order,
1745 // and unions can do funny things.
1746 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1747 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001748
1749 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001750 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001751 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1752 printf("\n block variable layout for block: ");
1753 const unsigned char *s = (unsigned char*)BitMap.c_str();
1754 for (unsigned i = 0; i < BitMap.size(); i++)
1755 if (!(s[i] & 0xf0))
1756 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1757 else
1758 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1759 printf("\n");
1760 }
1761
1762 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001763}
1764
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001765llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001766 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001767 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001768 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001769 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1770
Owen Anderson3c4972d2009-07-29 18:54:39 +00001771 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001772 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001773}
1774
Fariborz Jahanianda320092009-01-29 19:24:30 +00001775void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001776 // FIXME: We shouldn't need this, the protocol decl should contain enough
1777 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001778 DefinedProtocols.insert(PD->getIdentifier());
1779
1780 // If we have generated a forward reference to this protocol, emit
1781 // it now. Otherwise do nothing, the protocol objects are lazily
1782 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001783 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001784 GetOrEmitProtocol(PD);
1785}
1786
Fariborz Jahanianda320092009-01-29 19:24:30 +00001787llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001788 if (DefinedProtocols.count(PD->getIdentifier()))
1789 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001790
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001791 return GetOrEmitProtocolRef(PD);
1792}
1793
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001794/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001795// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1796struct _objc_protocol {
1797struct _objc_protocol_extension *isa;
1798char *protocol_name;
1799struct _objc_protocol_list *protocol_list;
1800struct _objc__method_prototype_list *instance_methods;
1801struct _objc__method_prototype_list *class_methods
1802};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001803
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001804See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001805*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001806llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1807 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1808
1809 // Early exit if a defining object has already been generated.
1810 if (Entry && Entry->hasInitializer())
1811 return Entry;
1812
Douglas Gregor1d784b22012-01-01 19:51:50 +00001813 // Use the protocol definition, if there is one.
1814 if (const ObjCProtocolDecl *Def = PD->getDefinition())
1815 PD = Def;
1816
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001817 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001818 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001819 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1820
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001821 // Construct method lists.
1822 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1823 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001824 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001825 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001826 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001827 ObjCMethodDecl *MD = *i;
1828 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001829 if (!C)
1830 return GetOrEmitProtocolRef(PD);
1831
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001832 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1833 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001834 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001835 } else {
1836 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001837 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001838 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001839 }
1840
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001841 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001842 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001843 ObjCMethodDecl *MD = *i;
1844 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001845 if (!C)
1846 return GetOrEmitProtocolRef(PD);
1847
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001848 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1849 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001850 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001851 } else {
1852 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001853 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001854 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001855 }
1856
Bob Wilsondc8dab62011-11-30 01:57:58 +00001857 MethodTypesExt.insert(MethodTypesExt.end(),
1858 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1859
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001860 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00001861 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1862 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001863 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001864 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001865 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001866 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001867 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001868 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001869 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001870 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001871 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001872 ClassMethods)
1873 };
Owen Anderson08e25242009-07-27 22:29:56 +00001874 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001875 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001876
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001877 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001878 // Already created, fix the linkage and update the initializer.
1879 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001880 Entry->setInitializer(Init);
1881 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001882 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001883 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001884 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001885 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001886 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001887 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001888 // FIXME: Is this necessary? Why only for protocol?
1889 Entry->setAlignment(4);
1890 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001891 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001892
1893 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001894}
1895
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001896llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001897 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1898
1899 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001900 // We use the initializer as a marker of whether this is a forward
1901 // reference or not. At module finalization we add the empty
1902 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001903 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001904 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001905 llvm::GlobalValue::ExternalLinkage,
1906 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001907 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001908 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001909 // FIXME: Is this necessary? Why only for protocol?
1910 Entry->setAlignment(4);
1911 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001912
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001913 return Entry;
1914}
1915
1916/*
1917 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001918 uint32_t size;
1919 struct objc_method_description_list *optional_instance_methods;
1920 struct objc_method_description_list *optional_class_methods;
1921 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001922 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001923 };
1924*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001925llvm::Constant *
1926CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1927 const ConstantVector &OptInstanceMethods,
Bob Wilsondc8dab62011-11-30 01:57:58 +00001928 const ConstantVector &OptClassMethods,
1929 const ConstantVector &MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001930 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001931 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001932 llvm::Constant *Values[] = {
1933 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001934 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001935 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001936 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001937 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001938 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001939 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001940 OptClassMethods),
1941 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00001942 ObjCTypes),
1943 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
1944 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001945 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001946
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001947 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001948 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00001949 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001950 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001951
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001952 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001953 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001954
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001955 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001956 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001957 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001958 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001959}
1960
1961/*
1962 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001963 struct objc_protocol_list *next;
1964 long count;
1965 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001966 };
1967*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001968llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00001969CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001970 ObjCProtocolDecl::protocol_iterator begin,
1971 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001972 std::vector<llvm::Constant*> ProtocolRefs;
1973
Daniel Dunbardbc93372008-08-21 21:57:41 +00001974 for (; begin != end; ++begin)
1975 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001976
1977 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001978 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001979 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001980
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001981 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001982 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001983
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001984 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001985 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001986 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001987 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001988 ProtocolRefs.size() - 1);
1989 Values[2] =
1990 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1991 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001992 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001993
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001994 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001995 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001996 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001997 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001998 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001999}
2000
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002001void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
2002 std::vector<llvm::Constant*> &Properties,
2003 const Decl *Container,
2004 const ObjCProtocolDecl *PROTO,
2005 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002006 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
2007 E = PROTO->protocol_end(); P != E; ++P)
2008 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
2009 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
2010 E = PROTO->prop_end(); I != E; ++I) {
2011 const ObjCPropertyDecl *PD = *I;
2012 if (!PropertySet.insert(PD->getIdentifier()))
2013 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002014 llvm::Constant *Prop[] = {
2015 GetPropertyName(PD->getIdentifier()),
2016 GetPropertyTypeString(PD, Container)
2017 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002018 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
2019 }
2020}
2021
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002022/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002023 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002024 const char * const name;
2025 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002026 };
2027
2028 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002029 uint32_t entsize; // sizeof (struct _objc_property)
2030 uint32_t prop_count;
2031 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002032 };
2033*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002034llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002035 const Decl *Container,
2036 const ObjCContainerDecl *OCD,
2037 const ObjCCommonTypesHelper &ObjCTypes) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002038 std::vector<llvm::Constant*> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002039 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002040 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2041 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00002042 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002043 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002044 llvm::Constant *Prop[] = {
2045 GetPropertyName(PD->getIdentifier()),
2046 GetPropertyTypeString(PD, Container)
2047 };
Owen Anderson08e25242009-07-27 22:29:56 +00002048 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002049 Prop));
2050 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002051 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002052 for (ObjCInterfaceDecl::all_protocol_iterator
2053 P = OID->all_referenced_protocol_begin(),
2054 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002055 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2056 ObjCTypes);
2057 }
2058 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2059 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2060 E = CD->protocol_end(); P != E; ++P)
2061 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2062 ObjCTypes);
2063 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002064
2065 // Return null for empty list.
2066 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002067 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002068
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002069 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002070 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002071 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002072 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2073 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002074 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002075 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002076 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002077 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002078
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002079 llvm::GlobalVariable *GV =
2080 CreateMetadataVar(Name, Init,
2081 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002082 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002083 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002084 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002085 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002086}
2087
Bob Wilsondc8dab62011-11-30 01:57:58 +00002088llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2089 const ConstantVector &MethodTypes,
2090 const ObjCCommonTypesHelper &ObjCTypes) {
2091 // Return null for empty list.
2092 if (MethodTypes.empty())
2093 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2094
2095 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2096 MethodTypes.size());
2097 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2098
2099 llvm::GlobalVariable *GV =
2100 CreateMetadataVar(Name, Init,
2101 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2102 (ObjCABI == 2) ? 8 : 4,
2103 true);
2104 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2105}
2106
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002107/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002108 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002109 int count;
2110 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002111 };
2112*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002113llvm::Constant *
2114CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002115 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002116 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002117 ObjCTypes.SelectorPtrTy),
2118 GetMethodVarType(MD)
2119 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002120 if (!Desc[1])
2121 return 0;
2122
Owen Anderson08e25242009-07-27 22:29:56 +00002123 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002124 Desc);
2125}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002126
Chris Lattner5f9e2722011-07-23 10:55:15 +00002127llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002128 const char *Section,
2129 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002130 // Return null for empty list.
2131 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002132 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002133
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002134 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002135 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002136 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002137 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002138 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002139 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002140
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002141 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002142 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002143 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002144}
2145
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002146/*
2147 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002148 char *category_name;
2149 char *class_name;
2150 struct _objc_method_list *instance_methods;
2151 struct _objc_method_list *class_methods;
2152 struct _objc_protocol_list *protocols;
2153 uint32_t size; // <rdar://4585769>
2154 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002155 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002156*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002157void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002158 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002159
Mike Stumpf5408fe2009-05-16 07:57:57 +00002160 // FIXME: This is poor design, the OCD should have a pointer to the category
2161 // decl. Additionally, note that Category can be null for the @implementation
2162 // w/o an @interface case. Sema should just create one for us as it does for
2163 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002164 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002165 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002166 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002167
2168 llvm::SmallString<256> ExtName;
2169 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2170 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002171
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002172 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002173 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002174 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002175 // Instance methods should always be defined.
2176 InstanceMethods.push_back(GetMethodConstant(*i));
2177 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002178 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002179 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002180 // Class methods should always be defined.
2181 ClassMethods.push_back(GetMethodConstant(*i));
2182 }
2183
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002184 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002185 Values[0] = GetClassName(OCD->getIdentifier());
2186 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002187 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002188 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002189 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002190 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002191 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002192 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002193 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002194 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002195 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002196 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002197 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002198 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002199 Category->protocol_begin(),
2200 Category->protocol_end());
2201 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002202 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002203 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002204 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002205
2206 // If there is no category @interface then there can be no properties.
2207 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002208 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002209 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002210 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002211 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002212 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002213
Owen Anderson08e25242009-07-27 22:29:56 +00002214 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002215 Values);
2216
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002217 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002218 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002219 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002220 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002221 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002222 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002223 // method definition entries must be clear for next implementation.
2224 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002225}
2226
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002227// FIXME: Get from somewhere?
2228enum ClassFlags {
2229 eClassFlags_Factory = 0x00001,
2230 eClassFlags_Meta = 0x00002,
2231 // <rdr://5142207>
2232 eClassFlags_HasCXXStructors = 0x02000,
2233 eClassFlags_Hidden = 0x20000,
2234 eClassFlags_ABI2_Hidden = 0x00010,
2235 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2236};
2237
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002238/*
2239 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002240 Class isa;
2241 Class super_class;
2242 const char *name;
2243 long version;
2244 long info;
2245 long instance_size;
2246 struct _objc_ivar_list *ivars;
2247 struct _objc_method_list *methods;
2248 struct _objc_cache *cache;
2249 struct _objc_protocol_list *protocols;
2250 // Objective-C 1.0 extensions (<rdr://4585769>)
2251 const char *ivar_layout;
2252 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002253 };
2254
2255 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002256*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002257void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002258 DefinedSymbols.insert(ID->getIdentifier());
2259
Chris Lattner8ec03f52008-11-24 03:54:41 +00002260 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002261 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002262 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002263 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002264 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002265 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002266 Interface->all_referenced_protocol_begin(),
2267 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002268 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002269 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002270 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002271 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002272 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002273
2274 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002275 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002276 Flags |= eClassFlags_Hidden;
2277
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002278 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002279 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002280 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002281 // Instance methods should always be defined.
2282 InstanceMethods.push_back(GetMethodConstant(*i));
2283 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002284 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002285 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002286 // Class methods should always be defined.
2287 ClassMethods.push_back(GetMethodConstant(*i));
2288 }
2289
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002290 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002291 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002292 ObjCPropertyImplDecl *PID = *i;
2293
2294 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2295 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2296
2297 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2298 if (llvm::Constant *C = GetMethodConstant(MD))
2299 InstanceMethods.push_back(C);
2300 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2301 if (llvm::Constant *C = GetMethodConstant(MD))
2302 InstanceMethods.push_back(C);
2303 }
2304 }
2305
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002306 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002307 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002308 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002309 // Record a reference to the super class.
2310 LazySymbols.insert(Super->getIdentifier());
2311
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002312 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002313 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002314 ObjCTypes.ClassPtrTy);
2315 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002316 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002317 }
2318 Values[ 2] = GetClassName(ID->getIdentifier());
2319 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002320 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2321 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2322 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002323 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002324 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002325 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002326 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002327 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002328 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002329 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002330 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002331 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002332 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002333 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002334 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002335 std::string Name("\01L_OBJC_CLASS_");
2336 Name += ClassName;
2337 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2338 // Check for a forward reference.
2339 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2340 if (GV) {
2341 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2342 "Forward metaclass reference has incorrect type.");
2343 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2344 GV->setInitializer(Init);
2345 GV->setSection(Section);
2346 GV->setAlignment(4);
2347 CGM.AddUsedGlobal(GV);
2348 }
2349 else
2350 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002351 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002352 // method definition entries must be clear for next implementation.
2353 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002354}
2355
2356llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2357 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002358 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002359 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002360 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002361
John McCall1fb0caa2010-10-22 21:05:15 +00002362 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002363 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002364
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002365 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002366 // The isa for the metaclass is the root of the hierarchy.
2367 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2368 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2369 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002370 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002371 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002372 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002373 // The super class for the metaclass is emitted as the name of the
2374 // super class. The runtime fixes this up to point to the
2375 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002376 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002377 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002378 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002379 ObjCTypes.ClassPtrTy);
2380 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002381 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002382 }
2383 Values[ 2] = GetClassName(ID->getIdentifier());
2384 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002385 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2386 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2387 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002388 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002389 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002390 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002391 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002392 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002393 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002394 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002395 Values[ 9] = Protocols;
2396 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002397 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002398 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002399 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002400 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002401 Values);
2402
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002403 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002404 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002405
2406 // Check for a forward reference.
2407 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2408 if (GV) {
2409 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2410 "Forward metaclass reference has incorrect type.");
2411 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2412 GV->setInitializer(Init);
2413 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002414 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002415 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002416 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002417 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002418 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002419 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002420 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002421
2422 return GV;
2423}
2424
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002425llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002426 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002427
Mike Stumpf5408fe2009-05-16 07:57:57 +00002428 // FIXME: Should we look these up somewhere other than the module. Its a bit
2429 // silly since we only generate these while processing an implementation, so
2430 // exactly one pointer would work if know when we entered/exitted an
2431 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002432
2433 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002434 // Previously, metaclass with internal linkage may have been defined.
2435 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002436 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2437 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002438 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2439 "Forward metaclass reference has incorrect type.");
2440 return GV;
2441 } else {
2442 // Generate as an external reference to keep a consistent
2443 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002444 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002445 llvm::GlobalValue::ExternalLinkage,
2446 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002447 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002448 }
2449}
2450
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002451llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2452 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2453
2454 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2455 true)) {
2456 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2457 "Forward class metadata reference has incorrect type.");
2458 return GV;
2459 } else {
2460 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2461 llvm::GlobalValue::ExternalLinkage,
2462 0,
2463 Name);
2464 }
2465}
2466
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002467/*
2468 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002469 uint32_t size;
2470 const char *weak_ivar_layout;
2471 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002472 };
2473*/
2474llvm::Constant *
2475CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002476 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002477 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002478
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002479 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002480 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002481 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002482 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002483 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002484
2485 // Return null if no extension bits are used.
2486 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002487 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002488
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002489 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002490 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002491 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002492 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002493 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002494}
2495
2496/*
2497 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002498 char *ivar_name;
2499 char *ivar_type;
2500 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002501 };
2502
2503 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002504 int ivar_count;
2505 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002506 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002507*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002508llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002509 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002510 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002511
2512 // When emitting the root class GCC emits ivar entries for the
2513 // actual class structure. It is not clear if we need to follow this
2514 // behavior; for now lets try and get away with not doing it. If so,
2515 // the cleanest solution would be to make up an ObjCInterfaceDecl
2516 // for the class.
2517 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002518 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002519
Jordy Rosedb8264e2011-07-22 02:08:32 +00002520 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002521
Jordy Rosedb8264e2011-07-22 02:08:32 +00002522 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002523 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002524 // Ignore unnamed bit-fields.
2525 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002526 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002527 llvm::Constant *Ivar[] = {
2528 GetMethodVarName(IVD->getIdentifier()),
2529 GetMethodVarType(IVD),
2530 llvm::ConstantInt::get(ObjCTypes.IntTy,
2531 ComputeIvarBaseOffset(CGM, OID, IVD))
2532 };
Owen Anderson08e25242009-07-27 22:29:56 +00002533 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002534 }
2535
2536 // Return null for empty list.
2537 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002538 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002539
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002540 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002541 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002542 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002543 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002544 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002545 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002546
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002547 llvm::GlobalVariable *GV;
2548 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002549 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002550 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002551 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002552 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002553 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002554 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002555 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002556 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002557}
2558
2559/*
2560 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002561 SEL method_name;
2562 char *method_types;
2563 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002564 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002565
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002566 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002567 struct objc_method_list *obsolete;
2568 int count;
2569 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002570 };
2571*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002572
2573/// GetMethodConstant - Return a struct objc_method constant for the
2574/// given method if it has been defined. The result is null if the
2575/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002576llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002577 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002578 if (!Fn)
2579 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002580
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002581 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00002582 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002583 ObjCTypes.SelectorPtrTy),
2584 GetMethodVarType(MD),
2585 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2586 };
Owen Anderson08e25242009-07-27 22:29:56 +00002587 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002588}
2589
Chris Lattner5f9e2722011-07-23 10:55:15 +00002590llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002591 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002592 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002593 // Return null for empty list.
2594 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002595 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002596
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002597 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002598 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002599 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002600 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002601 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002602 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002603 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002604
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002605 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002606 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002607}
2608
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002609llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002610 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002611 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002612 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002613
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002614 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002615 llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002616 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002617 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002618 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002619 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002620 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002621 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002622 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002623
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002624 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002625}
2626
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002627llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002628CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002629 llvm::Constant *Init,
2630 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002631 unsigned Align,
2632 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002633 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002634 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002635 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002636 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002637 if (Section)
2638 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002639 if (Align)
2640 GV->setAlignment(Align);
2641 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002642 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002643 return GV;
2644}
2645
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002646llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002647 // Abuse this interface function as a place to finalize.
2648 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002649 return NULL;
2650}
2651
Chris Lattner74391b42009-03-22 21:03:39 +00002652llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002653 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002654}
2655
Chris Lattner74391b42009-03-22 21:03:39 +00002656llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002657 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002658}
2659
David Chisnall8fac25d2010-12-26 22:13:16 +00002660llvm::Constant *CGObjCMac::GetGetStructFunction() {
2661 return ObjCTypes.getCopyStructFn();
2662}
2663llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002664 return ObjCTypes.getCopyStructFn();
2665}
2666
Fariborz Jahaniane3173022012-01-06 18:07:23 +00002667llvm::Constant *CGObjCMac::GetCppAtomicObjectFunction() {
2668 return ObjCTypes.getCppAtomicObjectFunction();
2669}
2670
Chris Lattner74391b42009-03-22 21:03:39 +00002671llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002672 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002673}
2674
John McCallf1549f62010-07-06 01:34:17 +00002675void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2676 return EmitTryOrSynchronizedStmt(CGF, S);
2677}
2678
2679void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2680 const ObjCAtSynchronizedStmt &S) {
2681 return EmitTryOrSynchronizedStmt(CGF, S);
2682}
2683
John McCallcc505292010-07-21 06:59:36 +00002684namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002685 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002686 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002687 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002688 llvm::Value *CallTryExitVar;
2689 llvm::Value *ExceptionData;
2690 ObjCTypesHelper &ObjCTypes;
2691 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002692 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002693 llvm::Value *CallTryExitVar,
2694 llvm::Value *ExceptionData,
2695 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002696 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002697 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2698
John McCallad346f42011-07-12 20:27:29 +00002699 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002700 // Check whether we need to call objc_exception_try_exit.
2701 // In optimized code, this branch will always be folded.
2702 llvm::BasicBlock *FinallyCallExit =
2703 CGF.createBasicBlock("finally.call_exit");
2704 llvm::BasicBlock *FinallyNoCallExit =
2705 CGF.createBasicBlock("finally.no_call_exit");
2706 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2707 FinallyCallExit, FinallyNoCallExit);
2708
2709 CGF.EmitBlock(FinallyCallExit);
2710 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2711 ->setDoesNotThrow();
2712
2713 CGF.EmitBlock(FinallyNoCallExit);
2714
2715 if (isa<ObjCAtTryStmt>(S)) {
2716 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002717 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2718 // Save the current cleanup destination in case there's
2719 // control flow inside the finally statement.
2720 llvm::Value *CurCleanupDest =
2721 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2722
John McCallcc505292010-07-21 06:59:36 +00002723 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2724
John McCalld96a8e72010-08-11 00:16:14 +00002725 if (CGF.HaveInsertPoint()) {
2726 CGF.Builder.CreateStore(CurCleanupDest,
2727 CGF.getNormalCleanupDestSlot());
2728 } else {
2729 // Currently, the end of the cleanup must always exist.
2730 CGF.EnsureInsertPoint();
2731 }
2732 }
John McCallcc505292010-07-21 06:59:36 +00002733 } else {
2734 // Emit objc_sync_exit(expr); as finally's sole statement for
2735 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002736 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002737 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2738 ->setDoesNotThrow();
2739 }
2740 }
2741 };
John McCall87bb5822010-07-31 23:20:56 +00002742
2743 class FragileHazards {
2744 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002745 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002746 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2747
2748 llvm::InlineAsm *ReadHazard;
2749 llvm::InlineAsm *WriteHazard;
2750
2751 llvm::FunctionType *GetAsmFnType();
2752
2753 void collectLocals();
2754 void emitReadHazard(CGBuilderTy &Builder);
2755
2756 public:
2757 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002758
John McCall87bb5822010-07-31 23:20:56 +00002759 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002760 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002761 };
2762}
2763
2764/// Create the fragile-ABI read and write hazards based on the current
2765/// state of the function, which is presumed to be immediately prior
2766/// to a @try block. These hazards are used to maintain correct
2767/// semantics in the face of optimization and the fragile ABI's
2768/// cavalier use of setjmp/longjmp.
2769FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2770 collectLocals();
2771
2772 if (Locals.empty()) return;
2773
2774 // Collect all the blocks in the function.
2775 for (llvm::Function::iterator
2776 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2777 BlocksBeforeTry.insert(&*I);
2778
2779 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2780
2781 // Create a read hazard for the allocas. This inhibits dead-store
2782 // optimizations and forces the values to memory. This hazard is
2783 // inserted before any 'throwing' calls in the protected scope to
2784 // reflect the possibility that the variables might be read from the
2785 // catch block if the call throws.
2786 {
2787 std::string Constraint;
2788 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2789 if (I) Constraint += ',';
2790 Constraint += "*m";
2791 }
2792
2793 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2794 }
2795
2796 // Create a write hazard for the allocas. This inhibits folding
2797 // loads across the hazard. This hazard is inserted at the
2798 // beginning of the catch path to reflect the possibility that the
2799 // variables might have been written within the protected scope.
2800 {
2801 std::string Constraint;
2802 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2803 if (I) Constraint += ',';
2804 Constraint += "=*m";
2805 }
2806
2807 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2808 }
2809}
2810
2811/// Emit a write hazard at the current location.
2812void FragileHazards::emitWriteHazard() {
2813 if (Locals.empty()) return;
2814
Jay Foad4c7d9f12011-07-15 08:37:34 +00002815 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002816}
2817
John McCall87bb5822010-07-31 23:20:56 +00002818void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2819 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002820 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002821}
2822
2823/// Emit read hazards in all the protected blocks, i.e. all the blocks
2824/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002825void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002826 if (Locals.empty()) return;
2827
2828 CGBuilderTy Builder(CGF.getLLVMContext());
2829
2830 // Iterate through all blocks, skipping those prior to the try.
2831 for (llvm::Function::iterator
2832 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2833 llvm::BasicBlock &BB = *FI;
2834 if (BlocksBeforeTry.count(&BB)) continue;
2835
2836 // Walk through all the calls in the block.
2837 for (llvm::BasicBlock::iterator
2838 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2839 llvm::Instruction &I = *BI;
2840
2841 // Ignore instructions that aren't non-intrinsic calls.
2842 // These are the only calls that can possibly call longjmp.
2843 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2844 if (isa<llvm::IntrinsicInst>(I))
2845 continue;
2846
2847 // Ignore call sites marked nounwind. This may be questionable,
2848 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2849 llvm::CallSite CS(&I);
2850 if (CS.doesNotThrow()) continue;
2851
John McCall0b251722010-08-04 05:59:32 +00002852 // Insert a read hazard before the call. This will ensure that
2853 // any writes to the locals are performed before making the
2854 // call. If the call throws, then this is sufficient to
2855 // guarantee correctness as long as it doesn't also write to any
2856 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002857 Builder.SetInsertPoint(&BB, BI);
2858 emitReadHazard(Builder);
2859 }
2860 }
2861}
2862
2863static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2864 if (V) S.insert(V);
2865}
2866
2867void FragileHazards::collectLocals() {
2868 // Compute a set of allocas to ignore.
2869 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2870 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2871 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002872
2873 // Collect all the allocas currently in the function. This is
2874 // probably way too aggressive.
2875 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2876 for (llvm::BasicBlock::iterator
2877 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2878 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2879 Locals.push_back(&*I);
2880}
2881
2882llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002883 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00002884 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2885 tys[i] = Locals[i]->getType();
2886 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00002887}
2888
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002889/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002890
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002891 Objective-C setjmp-longjmp (sjlj) Exception Handling
2892 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002893
John McCallf1549f62010-07-06 01:34:17 +00002894 A catch buffer is a setjmp buffer plus:
2895 - a pointer to the exception that was caught
2896 - a pointer to the previous exception data buffer
2897 - two pointers of reserved storage
2898 Therefore catch buffers form a stack, with a pointer to the top
2899 of the stack kept in thread-local storage.
2900
2901 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2902 objc_exception_try_exit pops the given catch buffer, which is
2903 required to be the top of the EH stack.
2904 objc_exception_throw pops the top of the EH stack, writes the
2905 thrown exception into the appropriate field, and longjmps
2906 to the setjmp buffer. It crashes the process (with a printf
2907 and an abort()) if there are no catch buffers on the stack.
2908 objc_exception_extract just reads the exception pointer out of the
2909 catch buffer.
2910
2911 There's no reason an implementation couldn't use a light-weight
2912 setjmp here --- something like __builtin_setjmp, but API-compatible
2913 with the heavyweight setjmp. This will be more important if we ever
2914 want to implement correct ObjC/C++ exception interactions for the
2915 fragile ABI.
2916
2917 Note that for this use of setjmp/longjmp to be correct, we may need
2918 to mark some local variables volatile: if a non-volatile local
2919 variable is modified between the setjmp and the longjmp, it has
2920 indeterminate value. For the purposes of LLVM IR, it may be
2921 sufficient to make loads and stores within the @try (to variables
2922 declared outside the @try) volatile. This is necessary for
2923 optimized correctness, but is not currently being done; this is
2924 being tracked as rdar://problem/8160285
2925
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002926 The basic framework for a @try-catch-finally is as follows:
2927 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002928 objc_exception_data d;
2929 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002930 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002931
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002932 objc_exception_try_enter(&d);
2933 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002934 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002935 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002936 // exception path
2937 id _caught = objc_exception_extract(&d);
2938
2939 // enter new try scope for handlers
2940 if (!setjmp(d.jmp_buf)) {
2941 ... match exception and execute catch blocks ...
2942
2943 // fell off end, rethrow.
2944 _rethrow = _caught;
2945 ... jump-through-finally to finally_rethrow ...
2946 } else {
2947 // exception in catch block
2948 _rethrow = objc_exception_extract(&d);
2949 _call_try_exit = false;
2950 ... jump-through-finally to finally_rethrow ...
2951 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002952 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002953 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002954
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002955 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002956 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002957 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002958
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002959 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002960 ... dispatch to finally destination ...
2961
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002962 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002963 objc_exception_throw(_rethrow);
2964
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002965 finally_end:
2966 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002967
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002968 This framework differs slightly from the one gcc uses, in that gcc
2969 uses _rethrow to determine if objc_exception_try_exit should be called
2970 and if the object should be rethrown. This breaks in the face of
2971 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002972
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002973 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002974
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002975 - If there are no catch blocks, then we avoid emitting the second
2976 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002977
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002978 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2979 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002980
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002981 - FIXME: If there is no @finally block we can do a few more
2982 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002983
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002984 Rethrows and Jumps-Through-Finally
2985 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002986
John McCallf1549f62010-07-06 01:34:17 +00002987 '@throw;' is supported by pushing the currently-caught exception
2988 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002989
John McCallf1549f62010-07-06 01:34:17 +00002990 Branches through the @finally block are handled with an ordinary
2991 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2992 exceptions are not compatible with C++ exceptions, and this is
2993 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002994
John McCallf1549f62010-07-06 01:34:17 +00002995 @synchronized(expr) { stmt; } is emitted as if it were:
2996 id synch_value = expr;
2997 objc_sync_enter(synch_value);
2998 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002999*/
3000
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00003001void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
3002 const Stmt &S) {
3003 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00003004
3005 // A destination for the fall-through edges of the catch handlers to
3006 // jump to.
3007 CodeGenFunction::JumpDest FinallyEnd =
3008 CGF.getJumpDestInCurrentScope("finally.end");
3009
3010 // A destination for the rethrow edge of the catch handlers to jump
3011 // to.
3012 CodeGenFunction::JumpDest FinallyRethrow =
3013 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003014
Daniel Dunbar1c566672009-02-24 01:43:46 +00003015 // For @synchronized, call objc_sync_enter(sync.expr). The
3016 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00003017 // @synchronized. We can't avoid a temp here because we need the
3018 // value to be preserved. If the backend ever does liveness
3019 // correctly after setjmp, this will be unnecessary.
3020 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00003021 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00003022 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00003023 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
3024 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00003025 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
3026 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00003027
3028 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
3029 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00003030 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003031
John McCall0b251722010-08-04 05:59:32 +00003032 // Allocate memory for the setjmp buffer. This needs to be kept
3033 // live throughout the try and catch blocks.
3034 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3035 "exceptiondata.ptr");
3036
John McCall87bb5822010-07-31 23:20:56 +00003037 // Create the fragile hazards. Note that this will not capture any
3038 // of the allocas required for exception processing, but will
3039 // capture the current basic block (which extends all the way to the
3040 // setjmp call) as "before the @try".
3041 FragileHazards Hazards(CGF);
3042
John McCallf1549f62010-07-06 01:34:17 +00003043 // Create a flag indicating whether the cleanup needs to call
3044 // objc_exception_try_exit. This is true except when
3045 // - no catches match and we're branching through the cleanup
3046 // just to rethrow the exception, or
3047 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003048 // The setjmp-safety rule here is that we should always store to this
3049 // variable in a place that dominates the branch through the cleanup
3050 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003051 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003052 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003053
John McCall9e2213d2010-10-04 23:42:51 +00003054 // A slot containing the exception to rethrow. Only needed when we
3055 // have both a @catch and a @finally.
3056 llvm::Value *PropagatingExnVar = 0;
3057
John McCallf1549f62010-07-06 01:34:17 +00003058 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003059 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003060 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003061 CallTryExitVar,
3062 ExceptionData,
3063 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003064
3065 // Enter a try block:
3066 // - Call objc_exception_try_enter to push ExceptionData on top of
3067 // the EH stack.
3068 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3069 ->setDoesNotThrow();
3070
3071 // - Call setjmp on the exception data buffer.
3072 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3073 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3074 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003075 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003076 llvm::CallInst *SetJmpResult =
3077 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3078 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003079 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003080
3081 // If setjmp returned 0, enter the protected block; otherwise,
3082 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003083 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3084 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003085 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003086 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3087 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003088
John McCallf1549f62010-07-06 01:34:17 +00003089 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003090 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003091 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003092 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003093 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003094
3095 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003096
John McCallf1549f62010-07-06 01:34:17 +00003097 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003098 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003099
John McCall87bb5822010-07-31 23:20:56 +00003100 // Don't optimize loads of the in-scope locals across this point.
3101 Hazards.emitWriteHazard();
3102
John McCallf1549f62010-07-06 01:34:17 +00003103 // For a @synchronized (or a @try with no catches), just branch
3104 // through the cleanup to the rethrow block.
3105 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3106 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003107 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003108 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003109
3110 // Otherwise, we have to match against the caught exceptions.
3111 } else {
John McCall0b251722010-08-04 05:59:32 +00003112 // Retrieve the exception object. We may emit multiple blocks but
3113 // nothing can cross this so the value is already in SSA form.
3114 llvm::CallInst *Caught =
3115 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3116 ExceptionData, "caught");
3117 Caught->setDoesNotThrow();
3118
John McCallf1549f62010-07-06 01:34:17 +00003119 // Push the exception to rethrow onto the EH value stack for the
3120 // benefit of any @throws in the handlers.
3121 CGF.ObjCEHValueStack.push_back(Caught);
3122
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003123 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003124
John McCall0b251722010-08-04 05:59:32 +00003125 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003126
John McCall0b251722010-08-04 05:59:32 +00003127 llvm::BasicBlock *CatchBlock = 0;
3128 llvm::BasicBlock *CatchHandler = 0;
3129 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003130 // Save the currently-propagating exception before
3131 // objc_exception_try_enter clears the exception slot.
3132 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3133 "propagating_exception");
3134 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3135
John McCall0b251722010-08-04 05:59:32 +00003136 // Enter a new exception try block (in case a @catch block
3137 // throws an exception).
3138 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3139 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003140
John McCall0b251722010-08-04 05:59:32 +00003141 llvm::CallInst *SetJmpResult =
3142 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3143 "setjmp.result");
3144 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003145 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003146
John McCall0b251722010-08-04 05:59:32 +00003147 llvm::Value *Threw =
3148 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3149
3150 CatchBlock = CGF.createBasicBlock("catch");
3151 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3152 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3153
3154 CGF.EmitBlock(CatchBlock);
3155 }
3156
3157 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003158
Daniel Dunbar55e40722008-09-27 07:03:52 +00003159 // Handle catch list. As a special case we check if everything is
3160 // matched and avoid generating code for falling off the end if
3161 // so.
3162 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003163 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3164 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003165
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003166 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003167 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003168
Anders Carlsson80f25672008-09-09 17:59:25 +00003169 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003170 if (!CatchParam) {
3171 AllMatched = true;
3172 } else {
John McCall183700f2009-09-21 23:43:11 +00003173 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003174
John McCallf1549f62010-07-06 01:34:17 +00003175 // catch(id e) always matches under this ABI, since only
3176 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003177 // FIXME: For the time being we also match id<X>; this should
3178 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003179 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003180 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003181 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003182
John McCallf1549f62010-07-06 01:34:17 +00003183 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003184 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003185 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3186
Anders Carlssondde0a942008-09-11 09:15:33 +00003187 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003188 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003189 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003190
3191 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003192 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003193 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003194
Anders Carlssondde0a942008-09-11 09:15:33 +00003195 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003196
3197 // The scope of the catch variable ends right here.
3198 CatchVarCleanups.ForceCleanup();
3199
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003200 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003201 break;
3202 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003203
Steve Naroff14108da2009-07-10 23:34:53 +00003204 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003205 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003206
3207 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003208 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3209 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003210
3211 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003212 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003213
John McCallf1549f62010-07-06 01:34:17 +00003214 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003215 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3216 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003217 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003218
John McCallf1549f62010-07-06 01:34:17 +00003219 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3220 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003221
3222 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003223 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003224
Anders Carlsson80f25672008-09-09 17:59:25 +00003225 // Emit the @catch block.
3226 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003227
3228 // Collect any cleanups for the catch variable. The scope lasts until
3229 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003230 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003231
John McCallb6bbcc92010-10-15 04:57:14 +00003232 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003233 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003234
John McCallf1549f62010-07-06 01:34:17 +00003235 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003236 llvm::Value *Tmp =
3237 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003238 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003239 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003240
Anders Carlssondde0a942008-09-11 09:15:33 +00003241 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003242
3243 // We're done with the catch variable.
3244 CatchVarCleanups.ForceCleanup();
3245
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003246 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003247
Anders Carlsson80f25672008-09-09 17:59:25 +00003248 CGF.EmitBlock(NextCatchBlock);
3249 }
3250
John McCallf1549f62010-07-06 01:34:17 +00003251 CGF.ObjCEHValueStack.pop_back();
3252
John McCall0b251722010-08-04 05:59:32 +00003253 // If nothing wanted anything to do with the caught exception,
3254 // kill the extract call.
3255 if (Caught->use_empty())
3256 Caught->eraseFromParent();
3257
3258 if (!AllMatched)
3259 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3260
3261 if (HasFinally) {
3262 // Emit the exception handler for the @catch blocks.
3263 CGF.EmitBlock(CatchHandler);
3264
3265 // In theory we might now need a write hazard, but actually it's
3266 // unnecessary because there's no local-accessing code between
3267 // the try's write hazard and here.
3268 //Hazards.emitWriteHazard();
3269
John McCall9e2213d2010-10-04 23:42:51 +00003270 // Extract the new exception and save it to the
3271 // propagating-exception slot.
3272 assert(PropagatingExnVar);
3273 llvm::CallInst *NewCaught =
3274 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3275 ExceptionData, "caught");
3276 NewCaught->setDoesNotThrow();
3277 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3278
John McCall0b251722010-08-04 05:59:32 +00003279 // Don't pop the catch handler; the throw already did.
3280 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003281 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003282 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003283 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003284
John McCall87bb5822010-07-31 23:20:56 +00003285 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003286 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003287
John McCallf1549f62010-07-06 01:34:17 +00003288 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003289 CGF.Builder.restoreIP(TryFallthroughIP);
3290 if (CGF.HaveInsertPoint())
3291 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003292 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003293 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003294
John McCallf1549f62010-07-06 01:34:17 +00003295 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003296 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003297 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003298 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003299 // If we have a propagating-exception variable, check it.
3300 llvm::Value *PropagatingExn;
3301 if (PropagatingExnVar) {
3302 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003303
John McCall9e2213d2010-10-04 23:42:51 +00003304 // Otherwise, just look in the buffer for the exception to throw.
3305 } else {
3306 llvm::CallInst *Caught =
3307 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3308 ExceptionData);
3309 Caught->setDoesNotThrow();
3310 PropagatingExn = Caught;
3311 }
3312
3313 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003314 ->setDoesNotThrow();
3315 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003316 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003317
John McCall87bb5822010-07-31 23:20:56 +00003318 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003319}
3320
3321void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003322 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003323 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003324
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003325 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00003326 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003327 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00003328 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003329 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003330 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003331 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003332 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003333 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003334
John McCallf1549f62010-07-06 01:34:17 +00003335 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3336 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003337 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003338
3339 // Clear the insertion point to indicate we are in unreachable code.
3340 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003341}
3342
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003343/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003344/// object: objc_read_weak (id *src)
3345///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003346llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003347 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003348 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003349 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3350 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3351 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003352 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003353 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003354 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003355 return read_weak;
3356}
3357
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003358/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3359/// objc_assign_weak (id src, id *dst)
3360///
3361void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003362 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003363 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003364 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003365 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003366 assert(Size <= 8 && "does not support size > 8");
3367 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003368 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003369 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3370 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003371 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3372 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003373 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003374 src, dst, "weakassign");
3375 return;
3376}
3377
Fariborz Jahanian58626502008-11-19 00:59:10 +00003378/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3379/// objc_assign_global (id src, id *dst)
3380///
3381void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003382 llvm::Value *src, llvm::Value *dst,
3383 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003384 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003385 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003386 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003387 assert(Size <= 8 && "does not support size > 8");
3388 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003389 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003390 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3391 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003392 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3393 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003394 if (!threadlocal)
3395 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3396 src, dst, "globalassign");
3397 else
3398 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3399 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003400 return;
3401}
3402
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003403/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003404/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003405///
3406void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003407 llvm::Value *src, llvm::Value *dst,
3408 llvm::Value *ivarOffset) {
3409 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003410 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003411 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003412 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003413 assert(Size <= 8 && "does not support size > 8");
3414 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003415 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003416 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3417 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003418 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3419 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003420 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3421 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003422 return;
3423}
3424
Fariborz Jahanian58626502008-11-19 00:59:10 +00003425/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3426/// objc_assign_strongCast (id src, id *dst)
3427///
3428void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003429 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003430 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003431 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003432 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003433 assert(Size <= 8 && "does not support size > 8");
3434 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003435 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003436 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3437 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003438 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3439 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003440 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003441 src, dst, "weakassign");
3442 return;
3443}
3444
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003445void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003446 llvm::Value *DestPtr,
3447 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003448 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003449 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3450 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003451 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003452 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003453 return;
3454}
3455
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003456/// EmitObjCValueForIvar - Code Gen for ivar reference.
3457///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003458LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3459 QualType ObjectTy,
3460 llvm::Value *BaseValue,
3461 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003462 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003463 const ObjCInterfaceDecl *ID =
3464 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003465 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3466 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003467}
3468
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003469llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003470 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003471 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003472 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003473 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003474 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3475 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003476}
3477
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003478/* *** Private Interface *** */
3479
3480/// EmitImageInfo - Emit the image info marker used to encode some module
3481/// level information.
3482///
3483/// See: <rdr://4810609&4810587&4810587>
3484/// struct IMAGE_INFO {
3485/// unsigned version;
3486/// unsigned flags;
3487/// };
3488enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003489 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003490 eImageInfo_GarbageCollected = (1 << 1),
3491 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003492 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3493
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003494 // A flag indicating that the module has no instances of a @synthesize of a
3495 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003496 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003497};
3498
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003499void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003500 unsigned version = 0; // Version is unused?
3501 unsigned flags = 0;
3502
3503 // FIXME: Fix and continue?
Douglas Gregore289d812011-09-13 17:21:33 +00003504 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003505 flags |= eImageInfo_GarbageCollected;
Douglas Gregore289d812011-09-13 17:21:33 +00003506 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003507 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003508
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003509 // We never allow @synthesize of a superclass property.
3510 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003511
Chris Lattner2acc6e32011-07-18 04:24:23 +00003512 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00003513
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003514 // Emitted as int[2];
3515 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003516 llvm::ConstantInt::get(Int32Ty, version),
3517 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003518 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003519 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003520
3521 const char *Section;
3522 if (ObjCABI == 1)
3523 Section = "__OBJC, __image_info,regular";
3524 else
3525 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003526 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003527 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad97357602011-06-22 09:24:39 +00003528 llvm::ConstantArray::get(AT, values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003529 Section,
3530 0,
3531 true);
3532 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003533}
3534
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003535
3536// struct objc_module {
3537// unsigned long version;
3538// unsigned long size;
3539// const char *name;
3540// Symtab symtab;
3541// };
3542
3543// FIXME: Get from somewhere
3544static const int ModuleVersion = 7;
3545
3546void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003547 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003548
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003549 llvm::Constant *Values[] = {
3550 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3551 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3552 // This used to be the filename, now it is unused. <rdr://4327263>
3553 GetClassName(&CGM.getContext().Idents.get("")),
3554 EmitModuleSymbols()
3555 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003556 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003557 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003558 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003559 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003560}
3561
3562llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003563 unsigned NumClasses = DefinedClasses.size();
3564 unsigned NumCategories = DefinedCategories.size();
3565
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003566 // Return null if no symbols were defined.
3567 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003568 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003569
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003570 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003571 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003572 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003573 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3574 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003575
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003576 // The runtime expects exactly the list of defined classes followed
3577 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003578 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003579 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003580 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003581 ObjCTypes.Int8PtrTy);
3582 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003583 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003584 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003585 ObjCTypes.Int8PtrTy);
3586
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003587 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003588 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003589 NumClasses + NumCategories),
3590 Symbols);
3591
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003592 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003593
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003594 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003595 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3596 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003597 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003598 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003599}
3600
John McCallf85e1932011-06-15 23:02:42 +00003601llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3602 IdentifierInfo *II) {
3603 LazySymbols.insert(II);
3604
3605 llvm::GlobalVariable *&Entry = ClassReferences[II];
3606
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003607 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003608 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003609 llvm::ConstantExpr::getBitCast(GetClassName(II),
3610 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003611 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003612 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3613 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3614 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003615 }
John McCallf85e1932011-06-15 23:02:42 +00003616
Benjamin Kramer578faa82011-09-27 21:06:10 +00003617 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003618}
3619
John McCallf85e1932011-06-15 23:02:42 +00003620llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3621 const ObjCInterfaceDecl *ID) {
3622 return EmitClassRefFromId(Builder, ID->getIdentifier());
3623}
3624
3625llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3626 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3627 return EmitClassRefFromId(Builder, II);
3628}
3629
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003630llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3631 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003632 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003633
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003634 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003635 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003636 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003637 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003638 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003639 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3640 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003641 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003642 }
3643
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003644 if (lvalue)
3645 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00003646 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003647}
3648
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003649llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003650 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003651
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003652 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003653 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003654 llvm::ConstantArray::get(VMContext,
3655 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003656 ((ObjCABI == 2) ?
3657 "__TEXT,__objc_classname,cstring_literals" :
3658 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003659 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003660
Owen Andersona1cf15f2009-07-14 23:10:40 +00003661 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003662}
3663
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003664llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3665 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3666 I = MethodDefinitions.find(MD);
3667 if (I != MethodDefinitions.end())
3668 return I->second;
3669
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003670 return NULL;
3671}
3672
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003673/// GetIvarLayoutName - Returns a unique constant for the given
3674/// ivar layout bitmap.
3675llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003676 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003677 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003678}
3679
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003680void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003681 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003682 bool ForStrongLayout,
3683 bool &HasUnion) {
3684 const RecordDecl *RD = RT->getDecl();
3685 // FIXME - Use iterator.
Jordy Rosedb8264e2011-07-22 02:08:32 +00003686 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2acc6e32011-07-18 04:24:23 +00003687 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003688 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003689 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003690
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003691 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3692 ForStrongLayout, HasUnion);
3693}
3694
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003695void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003696 const llvm::StructLayout *Layout,
3697 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +00003698 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003699 unsigned int BytePos, bool ForStrongLayout,
3700 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003701 bool IsUnion = (RD && RD->isUnion());
3702 uint64_t MaxUnionIvarSize = 0;
3703 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003704 const FieldDecl *MaxField = 0;
3705 const FieldDecl *MaxSkippedField = 0;
3706 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003707 uint64_t MaxFieldOffset = 0;
3708 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003709 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003710 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003711
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003712 if (RecFields.empty())
3713 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003714 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3715 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCallf85e1932011-06-15 23:02:42 +00003716 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003717 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003718 FirstFieldDelta =
3719 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3720 }
3721
Chris Lattnerf1690852009-03-31 08:48:01 +00003722 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003723 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003724 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003725 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003726 // Note that 'i' here is actually the field index inside RD of Field,
3727 // although this dependency is hidden.
3728 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003729 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003730 } else
John McCallf85e1932011-06-15 23:02:42 +00003731 FieldOffset =
3732 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003733
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003734 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003735 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003736 LastFieldBitfieldOrUnnamed = Field;
3737 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003738 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003739 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003740
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003741 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003742 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003743 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003744 if (FQT->isUnionType())
3745 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003746
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003747 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003748 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003749 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003750 continue;
3751 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003752
Chris Lattnerf1690852009-03-31 08:48:01 +00003753 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003754 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003755 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003756 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003757 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003758 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003759 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3760 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003761 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003762 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003763 FQT = CArray->getElementType();
3764 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003765
3766 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003767 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003768 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003769 int OldIndex = IvarsInfo.size() - 1;
3770 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003771
Ted Kremenek6217b802009-07-29 21:53:49 +00003772 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003773 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003774 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003775
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003776 // Replicate layout information for each array element. Note that
3777 // one element is already done.
3778 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003779 for (int FirstIndex = IvarsInfo.size() - 1,
3780 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003781 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003782 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3783 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3784 IvarsInfo[i].ivar_size));
3785 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3786 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3787 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003788 }
3789 continue;
3790 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003791 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003792 // At this point, we are done with Record/Union and array there of.
3793 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003794 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003795
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003796 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003797 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3798 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003799 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003800 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003801 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003802 MaxUnionIvarSize = UnionIvarSize;
3803 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003804 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003805 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003806 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003807 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003808 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003809 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003810 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003811 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3812 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003813 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003814 // FIXME: Why the asymmetry? We divide by word size in bits on other
3815 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003816 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003817 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003818 MaxSkippedUnionIvarSize = UnionIvarSize;
3819 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003820 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003821 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003822 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003823 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003824 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003825 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003826 }
3827 }
3828 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003829
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003830 if (LastFieldBitfieldOrUnnamed) {
3831 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3832 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003833 uint64_t BitFieldSize
3834 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003835 GC_IVAR skivar;
3836 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3837 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3838 + ((BitFieldSize % ByteSizeInBits) != 0);
3839 SkipIvars.push_back(skivar);
3840 } else {
3841 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3842 // Last field was unnamed. Must update skip info.
3843 unsigned FieldSize
3844 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3845 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3846 FieldSize / ByteSizeInBits));
3847 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003848 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003849
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003850 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003851 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003852 MaxUnionIvarSize));
3853 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003854 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003855 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003856}
3857
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003858/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3859/// the computations and returning the layout bitmap (for ivar or blocks) in
3860/// the given argument BitMap string container. Routine reads
3861/// two containers, IvarsInfo and SkipIvars which are assumed to be
3862/// filled already by the caller.
3863llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003864 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2acc6e32011-07-18 04:24:23 +00003865 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003866
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003867 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00003868 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003869 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003870 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003871 if (IvarsInfo[0].ivar_bytepos == 0) {
3872 WordsToSkip = 0;
3873 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003874 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003875 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3876 WordsToScan = IvarsInfo[0].ivar_size;
3877 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003878 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003879 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003880 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003881 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003882 // consecutive 'scanned' object pointers.
3883 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003884 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003885 // Skip over 'gc'able object pointer which lay over each other.
3886 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3887 continue;
3888 // Must skip over 1 or more words. We save current skip/scan values
3889 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003890 SKIP_SCAN SkScan;
3891 SkScan.skip = WordsToSkip;
3892 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003893 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003894
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003895 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003896 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3897 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003898 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003899 WordsToSkip = 0;
3900 WordsToScan = IvarsInfo[i].ivar_size;
3901 }
3902 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003903 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003904 SKIP_SCAN SkScan;
3905 SkScan.skip = WordsToSkip;
3906 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003907 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003908 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003909
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003910 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003911 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003912 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003913 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003914 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003915 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003916 IvarsInfo[LastIndex].ivar_bytepos +
3917 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003918 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003919 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003920 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003921 SKIP_SCAN SkScan;
3922 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3923 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003924 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003925 }
3926 }
3927 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3928 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003929 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003930 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003931 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3932 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3933 // 0xM0 followed by 0x0N detected.
3934 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3935 for (int j = i+1; j < SkipScan; j++)
3936 SkipScanIvars[j] = SkipScanIvars[j+1];
3937 --SkipScan;
3938 }
3939 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003940
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003941 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003942 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003943 unsigned char byte;
3944 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3945 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3946 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3947 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003948
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003949 // first skip big.
3950 for (unsigned int ix = 0; ix < skip_big; ix++)
3951 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003952
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003953 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003954 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003955 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003956 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003957 byte |= 0xf;
3958 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003959 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003960 byte |= scan_small;
3961 scan_small = 0;
3962 }
3963 BitMap += byte;
3964 }
3965 // next scan big
3966 for (unsigned int ix = 0; ix < scan_big; ix++)
3967 BitMap += (unsigned char)(0x0f);
3968 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003969 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003970 byte = scan_small;
3971 BitMap += byte;
3972 }
3973 }
3974 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003975 unsigned char zero = 0;
3976 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003977
3978 llvm::GlobalVariable * Entry =
3979 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3980 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003981 ((ObjCABI == 2) ?
3982 "__TEXT,__objc_classname,cstring_literals" :
3983 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003984 1, true);
3985 return getConstantGEP(VMContext, Entry, 0, 0);
3986}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003987
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003988/// BuildIvarLayout - Builds ivar layout bitmap for the class
3989/// implementation for the __strong or __weak case.
3990/// The layout map displays which words in ivar list must be skipped
3991/// and which must be scanned by GC (see below). String is built of bytes.
3992/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3993/// of words to skip and right nibble is count of words to scan. So, each
3994/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3995/// represented by a 0x00 byte which also ends the string.
3996/// 1. when ForStrongLayout is true, following ivars are scanned:
3997/// - id, Class
3998/// - object *
3999/// - __strong anything
4000///
4001/// 2. When ForStrongLayout is false, following ivars are scanned:
4002/// - __weak anything
4003///
4004llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
4005 const ObjCImplementationDecl *OMD,
4006 bool ForStrongLayout) {
4007 bool hasUnion = false;
4008
Chris Lattner2acc6e32011-07-18 04:24:23 +00004009 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregore289d812011-09-13 17:21:33 +00004010 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00004011 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004012 return llvm::Constant::getNullValue(PtrTy);
4013
Jordy Rosedb8264e2011-07-22 02:08:32 +00004014 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
4015 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004016 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004017 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004018 IVD; IVD = IVD->getNextIvar())
4019 RecFields.push_back(cast<FieldDecl>(IVD));
4020 }
4021 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00004022 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00004023 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004024
Jordy Rosedb8264e2011-07-22 02:08:32 +00004025 // FIXME: This is not ideal; we shouldn't have to do this copy.
4026 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00004027 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004028
4029 if (RecFields.empty())
4030 return llvm::Constant::getNullValue(PtrTy);
4031
4032 SkipIvars.clear();
4033 IvarsInfo.clear();
4034
4035 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4036 if (IvarsInfo.empty())
4037 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004038 // Sort on byte position in case we encounterred a union nested in
4039 // the ivar list.
4040 if (hasUnion && !IvarsInfo.empty())
4041 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4042 if (hasUnion && !SkipIvars.empty())
4043 std::sort(SkipIvars.begin(), SkipIvars.end());
4044
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004045 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004046 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004047
4048 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004049 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004050 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004051 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004052 const unsigned char *s = (unsigned char*)BitMap.c_str();
4053 for (unsigned i = 0; i < BitMap.size(); i++)
4054 if (!(s[i] & 0xf0))
4055 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4056 else
4057 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4058 printf("\n");
4059 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004060 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004061}
4062
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004063llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004064 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4065
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004066 // FIXME: Avoid std::string copying.
4067 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004068 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00004069 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004070 ((ObjCABI == 2) ?
4071 "__TEXT,__objc_methname,cstring_literals" :
4072 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004073 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004074
Owen Andersona1cf15f2009-07-14 23:10:40 +00004075 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004076}
4077
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004078// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004079llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004080 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4081}
4082
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004083llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004084 std::string TypeStr;
4085 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4086
4087 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004088
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004089 if (!Entry)
4090 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004091 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004092 ((ObjCABI == 2) ?
4093 "__TEXT,__objc_methtype,cstring_literals" :
4094 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004095 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004096
Owen Andersona1cf15f2009-07-14 23:10:40 +00004097 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004098}
4099
Bob Wilsondc8dab62011-11-30 01:57:58 +00004100llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4101 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004102 std::string TypeStr;
Douglas Gregorf968d832011-05-27 01:19:52 +00004103 if (CGM.getContext().getObjCEncodingForMethodDecl(
4104 const_cast<ObjCMethodDecl*>(D),
Bob Wilsondc8dab62011-11-30 01:57:58 +00004105 TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004106 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004107
4108 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4109
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004110 if (!Entry)
4111 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004112 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004113 ((ObjCABI == 2) ?
4114 "__TEXT,__objc_methtype,cstring_literals" :
4115 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004116 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004117
Owen Andersona1cf15f2009-07-14 23:10:40 +00004118 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004119}
4120
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004121// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004122llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004123 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004124
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004125 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004126 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004127 llvm::ConstantArray::get(VMContext,
4128 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004129 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004130 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004131
Owen Andersona1cf15f2009-07-14 23:10:40 +00004132 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004133}
4134
4135// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004136// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004137llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004138CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4139 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004140 std::string TypeStr;
4141 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004142 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4143}
4144
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004145void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004146 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004147 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004148 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004149 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004150 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4151 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004152 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004153 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004154 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004155 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004156}
4157
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004158void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004159 EmitModuleInfo();
4160
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004161 // Emit the dummy bodies for any protocols which were referenced but
4162 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004163 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004164 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4165 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004166 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004167
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004168 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004169 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004170 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004171 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004172 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004173 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004174 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004175 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004176 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004177 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004178 }
4179
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004180 // Add assembler directives to add lazy undefined symbol references
4181 // for classes which are referenced but not defined. This is
4182 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004183 //
4184 // FIXME: It would be nice if we had an LLVM construct for this.
4185 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4186 llvm::SmallString<256> Asm;
4187 Asm += CGM.getModule().getModuleInlineAsm();
4188 if (!Asm.empty() && Asm.back() != '\n')
4189 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004190
Daniel Dunbar33063492009-09-07 00:20:42 +00004191 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004192 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4193 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004194 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4195 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004196 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004197 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004198 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004199 }
4200
4201 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4202 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4203 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4204 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004205
Daniel Dunbar33063492009-09-07 00:20:42 +00004206 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004207 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004208}
4209
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004210CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004211 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004212 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004213 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004214 ObjCABI = 2;
4215}
4216
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004217/* *** */
4218
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004219ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004220 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004221 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4222 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004223
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004224 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004225 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004226 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004227 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004228 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Bob Wilsondc8dab62011-11-30 01:57:58 +00004229 Int8PtrPtrTy = llvm::PointerType::getUnqual(Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004230
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004231 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004232 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004233 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004234
Mike Stumpf5408fe2009-05-16 07:57:57 +00004235 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4236 // comes out a bit cleaner.
Chris Lattner2acc6e32011-07-18 04:24:23 +00004237 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004238 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004239
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004240 // I'm not sure I like this. The implicit coordination is a bit
4241 // gross. We should solve this in a reasonable fashion because this
4242 // is a pretty common task (match some runtime data structure with
4243 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004244
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004245 // FIXME: This is leaked.
4246 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004247
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004248 // struct _objc_super {
4249 // id self;
4250 // Class cls;
4251 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004252 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004253 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004254 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004255 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004256 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004257 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004258 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004259 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004260 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004261
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004262 SuperCTy = Ctx.getTagDeclType(RD);
4263 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004264
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004265 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004266 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4267
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004268 // struct _prop_t {
4269 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004270 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004271 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004272 PropertyTy = llvm::StructType::create("struct._prop_t",
4273 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004274
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004275 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004276 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004277 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004278 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004279 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004280 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004281 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4282 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004283 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004284 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004285
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004286 // struct _objc_method {
4287 // SEL _cmd;
4288 // char *method_type;
4289 // char *_imp;
4290 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004291 MethodTy = llvm::StructType::create("struct._objc_method",
4292 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4293 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004294
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004295 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004296 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004297 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004298
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004299}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004300
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004301ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004302 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004303 // struct _objc_method_description {
4304 // SEL name;
4305 // char *types;
4306 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004307 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004308 llvm::StructType::create("struct._objc_method_description",
4309 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004310
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004311 // struct _objc_method_description_list {
4312 // int count;
4313 // struct _objc_method_description[1];
4314 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004315 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004316 llvm::StructType::create("struct._objc_method_description_list",
4317 IntTy,
4318 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004319
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004320 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004321 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004322 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004323
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004324 // Protocol description structures
4325
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004326 // struct _objc_protocol_extension {
4327 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4328 // struct _objc_method_description_list *optional_instance_methods;
4329 // struct _objc_method_description_list *optional_class_methods;
4330 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00004331 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004332 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004333 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004334 llvm::StructType::create("struct._objc_protocol_extension",
4335 IntTy, MethodDescriptionListPtrTy,
4336 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004337 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004338
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004339 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004340 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004341
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004342 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004343
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004344 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004345 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004346
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004347 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004348 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004349 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004350 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004351 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004352 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004353
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004354 // struct _objc_protocol {
4355 // struct _objc_protocol_extension *isa;
4356 // char *protocol_name;
4357 // struct _objc_protocol **_objc_protocol_list;
4358 // struct _objc_method_description_list *instance_methods;
4359 // struct _objc_method_description_list *class_methods;
4360 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004361 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4362 llvm::PointerType::getUnqual(ProtocolListTy),
4363 MethodDescriptionListPtrTy,
4364 MethodDescriptionListPtrTy,
4365 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004366
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004367 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004368 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004369
Owen Anderson96e0fc72009-07-29 22:16:19 +00004370 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004371
4372 // Class description structures
4373
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004374 // struct _objc_ivar {
4375 // char *ivar_name;
4376 // char *ivar_type;
4377 // int ivar_offset;
4378 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004379 IvarTy = llvm::StructType::create("struct._objc_ivar",
4380 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004381
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004382 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004383 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004384 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004385 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004386
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004387 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004388 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004389 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004390 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004391
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004392 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004393 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004394 llvm::StructType::create("struct._objc_class_extension",
4395 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004396 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004397
Chris Lattnerc1c20112011-08-12 17:43:31 +00004398 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004399
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004400 // struct _objc_class {
4401 // Class isa;
4402 // Class super_class;
4403 // char *name;
4404 // long version;
4405 // long info;
4406 // long instance_size;
4407 // struct _objc_ivar_list *ivars;
4408 // struct _objc_method_list *methods;
4409 // struct _objc_cache *cache;
4410 // struct _objc_protocol_list *protocols;
4411 // char *ivar_layout;
4412 // struct _objc_class_ext *ext;
4413 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004414 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4415 llvm::PointerType::getUnqual(ClassTy),
4416 Int8PtrTy,
4417 LongTy,
4418 LongTy,
4419 LongTy,
4420 IvarListPtrTy,
4421 MethodListPtrTy,
4422 CachePtrTy,
4423 ProtocolListPtrTy,
4424 Int8PtrTy,
4425 ClassExtensionPtrTy,
4426 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004427
Owen Anderson96e0fc72009-07-29 22:16:19 +00004428 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004429
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004430 // struct _objc_category {
4431 // char *category_name;
4432 // char *class_name;
4433 // struct _objc_method_list *instance_method;
4434 // struct _objc_method_list *class_method;
4435 // uint32_t size; // sizeof(struct _objc_category)
4436 // struct _objc_property_list *instance_properties;// category's @property
4437 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004438 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004439 llvm::StructType::create("struct._objc_category",
4440 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4441 MethodListPtrTy, ProtocolListPtrTy,
4442 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004443
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004444 // Global metadata structures
4445
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004446 // struct _objc_symtab {
4447 // long sel_ref_cnt;
4448 // SEL *refs;
4449 // short cls_def_cnt;
4450 // short cat_def_cnt;
4451 // char *defs[cls_def_cnt + cat_def_cnt];
4452 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004453 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004454 llvm::StructType::create("struct._objc_symtab",
4455 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4456 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004457 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004458
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004459 // struct _objc_module {
4460 // long version;
4461 // long size; // sizeof(struct _objc_module)
4462 // char *name;
4463 // struct _objc_symtab* symtab;
4464 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004465 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004466 llvm::StructType::create("struct._objc_module",
4467 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004468
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004469
Mike Stumpf5408fe2009-05-16 07:57:57 +00004470 // FIXME: This is the size of the setjmp buffer and should be target
4471 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004472 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004473
Anders Carlsson124526b2008-09-09 10:10:21 +00004474 // Exceptions
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004475 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004476 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004477
4478 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004479 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004480 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4481 SetJmpBufferSize),
4482 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004483
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004484}
4485
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004486ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004487 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004488 // struct _method_list_t {
4489 // uint32_t entsize; // sizeof(struct _objc_method)
4490 // uint32_t method_count;
4491 // struct _objc_method method_list[method_count];
4492 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004493 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004494 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4495 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004496 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004497 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004498
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004499 // struct _protocol_t {
4500 // id isa; // NULL
4501 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004502 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004503 // const struct method_list_t * const instance_methods;
4504 // const struct method_list_t * const class_methods;
4505 // const struct method_list_t *optionalInstanceMethods;
4506 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004507 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004508 // const uint32_t size; // sizeof(struct _protocol_t)
4509 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00004510 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004511 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004512
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004513 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004514 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004515 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004516
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004517 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004518 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4519 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4520 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4521 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004522 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4523 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004524
4525 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004526 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004527
Fariborz Jahanianda320092009-01-29 19:24:30 +00004528 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004529 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004530 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004531 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004532 ProtocolListnfABITy->setBody(LongTy,
4533 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4534 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004535
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004536 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004537 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004538
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004539 // struct _ivar_t {
4540 // unsigned long int *offset; // pointer to ivar offset location
4541 // char *name;
4542 // char *type;
4543 // uint32_t alignment;
4544 // uint32_t size;
4545 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004546 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004547 llvm::StructType::create("struct._ivar_t",
4548 llvm::PointerType::getUnqual(LongTy),
4549 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004550
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004551 // struct _ivar_list_t {
4552 // uint32 entsize; // sizeof(struct _ivar_t)
4553 // uint32 count;
4554 // struct _iver_t list[count];
4555 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004556 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004557 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4558 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004559
Owen Anderson96e0fc72009-07-29 22:16:19 +00004560 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004561
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004562 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004563 // uint32_t const flags;
4564 // uint32_t const instanceStart;
4565 // uint32_t const instanceSize;
4566 // uint32_t const reserved; // only when building for 64bit targets
4567 // const uint8_t * const ivarLayout;
4568 // const char *const name;
4569 // const struct _method_list_t * const baseMethods;
4570 // const struct _objc_protocol_list *const baseProtocols;
4571 // const struct _ivar_list_t *const ivars;
4572 // const uint8_t * const weakIvarLayout;
4573 // const struct _prop_list_t * const properties;
4574 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004575
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004576 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004577 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4578 IntTy, IntTy, IntTy, Int8PtrTy,
4579 Int8PtrTy, MethodListnfABIPtrTy,
4580 ProtocolListnfABIPtrTy,
4581 IvarListnfABIPtrTy,
4582 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004583
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004584 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004585 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004586 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4587 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004588
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004589 // struct _class_t {
4590 // struct _class_t *isa;
4591 // struct _class_t * const superclass;
4592 // void *cache;
4593 // IMP *vtable;
4594 // struct class_ro_t *ro;
4595 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004596
Chris Lattnerc1c20112011-08-12 17:43:31 +00004597 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004598 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4599 llvm::PointerType::getUnqual(ClassnfABITy),
4600 CachePtrTy,
4601 llvm::PointerType::getUnqual(ImpnfABITy),
4602 llvm::PointerType::getUnqual(ClassRonfABITy),
4603 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004604
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004605 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004606 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004607
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004608 // struct _category_t {
4609 // const char * const name;
4610 // struct _class_t *const cls;
4611 // const struct _method_list_t * const instance_methods;
4612 // const struct _method_list_t * const class_methods;
4613 // const struct _protocol_list_t * const protocols;
4614 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004615 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004616 CategorynfABITy = llvm::StructType::create("struct._category_t",
4617 Int8PtrTy, ClassnfABIPtrTy,
4618 MethodListnfABIPtrTy,
4619 MethodListnfABIPtrTy,
4620 ProtocolListnfABIPtrTy,
4621 PropertyListPtrTy,
4622 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004623
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004624 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004625 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4626 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004627
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004628 // MessageRefTy - LLVM for:
4629 // struct _message_ref_t {
4630 // IMP messenger;
4631 // SEL name;
4632 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004633
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004634 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004635 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004636 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004637 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004638 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004639 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004640 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004641 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004642 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004643 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004644
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004645 MessageRefCTy = Ctx.getTagDeclType(RD);
4646 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4647 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004648
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004649 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004650 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004651
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004652 // SuperMessageRefTy - LLVM for:
4653 // struct _super_message_ref_t {
4654 // SUPER_IMP messenger;
4655 // SEL name;
4656 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004657 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004658 llvm::StructType::create("struct._super_message_ref_t",
4659 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004660
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004661 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004662 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004663
Daniel Dunbare588b992009-03-01 04:46:24 +00004664
4665 // struct objc_typeinfo {
4666 // const void** vtable; // objc_ehtype_vtable + 2
4667 // const char* name; // c++ typeinfo string
4668 // Class cls;
4669 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004670 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004671 llvm::StructType::create("struct._objc_typeinfo",
4672 llvm::PointerType::getUnqual(Int8PtrTy),
4673 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004674 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004675}
4676
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004677llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004678 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004679
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004680 return NULL;
4681}
4682
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004683void CGObjCNonFragileABIMac::AddModuleClassList(const
4684 std::vector<llvm::GlobalValue*>
4685 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004686 const char *SymbolName,
4687 const char *SectionName) {
4688 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004689
Daniel Dunbar463b8762009-05-15 21:48:48 +00004690 if (!NumClasses)
4691 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004692
Daniel Dunbar463b8762009-05-15 21:48:48 +00004693 std::vector<llvm::Constant*> Symbols(NumClasses);
4694 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004695 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004696 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004697 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004698 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004699 NumClasses),
4700 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004701
Daniel Dunbar463b8762009-05-15 21:48:48 +00004702 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004703 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004704 llvm::GlobalValue::InternalLinkage,
4705 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004706 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004707 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004708 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004709 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004710}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004711
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004712void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4713 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004714
Daniel Dunbar463b8762009-05-15 21:48:48 +00004715 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004716 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004717 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004718 "\01L_OBJC_LABEL_CLASS_$",
4719 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004720
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004721 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4722 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4723 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4724 continue;
4725 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004726 }
4727
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004728 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4729 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4730 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4731 continue;
4732 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4733 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004734
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004735 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004736 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4737 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004738
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004739 // Build list of all implemented category addresses in array
4740 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004741 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004742 "\01L_OBJC_LABEL_CATEGORY_$",
4743 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004744 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004745 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4746 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004747
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004748 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004749}
4750
John McCall944c8432011-05-14 03:10:52 +00004751/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4752/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004753/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004754/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004755bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4756 // At various points we've experimented with using vtable-based
4757 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004758 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4759 default:
John McCall944c8432011-05-14 03:10:52 +00004760 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004761 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004762 return false;
John McCall944c8432011-05-14 03:10:52 +00004763 case CodeGenOptions::NonLegacy:
4764 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004765 case CodeGenOptions::Mixed:
4766 break;
4767 }
4768
4769 // If so, see whether this selector is in the white-list of things which must
4770 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004771 if (VTableDispatchMethods.empty()) {
4772 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4773 VTableDispatchMethods.insert(GetNullarySelector("class"));
4774 VTableDispatchMethods.insert(GetNullarySelector("self"));
4775 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4776 VTableDispatchMethods.insert(GetNullarySelector("length"));
4777 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004778
John McCall944c8432011-05-14 03:10:52 +00004779 // These are vtable-based if GC is disabled.
4780 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004781 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00004782 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4783 VTableDispatchMethods.insert(GetNullarySelector("release"));
4784 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4785 }
4786
4787 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4788 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4789 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4790 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4791 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4792 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4793 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4794
4795 // These are vtable-based if GC is enabled.
4796 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004797 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00004798 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4799 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4800
4801 // "countByEnumeratingWithState:objects:count"
4802 IdentifierInfo *KeyIdents[] = {
4803 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4804 &CGM.getContext().Idents.get("objects"),
4805 &CGM.getContext().Idents.get("count")
4806 };
4807 VTableDispatchMethods.insert(
4808 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4809 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004810 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004811
John McCall944c8432011-05-14 03:10:52 +00004812 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004813}
4814
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004815// Metadata flags
4816enum MetaDataDlags {
4817 CLS = 0x0,
4818 CLS_META = 0x1,
4819 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004820 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004821 CLS_EXCEPTION = 0x20,
4822
4823 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4824 CLS_HAS_IVAR_RELEASER = 0x40,
4825 /// class was compiled with -fobjc-arr
4826 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004827};
4828/// BuildClassRoTInitializer - generate meta-data for:
4829/// struct _class_ro_t {
4830/// uint32_t const flags;
4831/// uint32_t const instanceStart;
4832/// uint32_t const instanceSize;
4833/// uint32_t const reserved; // only when building for 64bit targets
4834/// const uint8_t * const ivarLayout;
4835/// const char *const name;
4836/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004837/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004838/// const struct _ivar_list_t *const ivars;
4839/// const uint8_t * const weakIvarLayout;
4840/// const struct _prop_list_t * const properties;
4841/// }
4842///
4843llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004844 unsigned flags,
4845 unsigned InstanceStart,
4846 unsigned InstanceSize,
4847 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004848 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004849 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004850
4851 if (CGM.getLangOptions().ObjCAutoRefCount)
4852 flags |= CLS_COMPILED_BY_ARC;
4853
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004854 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4855 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4856 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004857 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004858 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4859 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004860 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004861 // const struct _method_list_t * const baseMethods;
4862 std::vector<llvm::Constant*> Methods;
4863 std::string MethodListName("\01l_OBJC_$_");
4864 if (flags & CLS_META) {
4865 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004866 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004867 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004868 // Class methods should always be defined.
4869 Methods.push_back(GetMethodConstant(*i));
4870 }
4871 } else {
4872 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004873 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004874 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004875 // Instance methods should always be defined.
4876 Methods.push_back(GetMethodConstant(*i));
4877 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004878 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004879 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004880 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004881
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004882 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4883 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004884
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004885 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4886 if (llvm::Constant *C = GetMethodConstant(MD))
4887 Methods.push_back(C);
4888 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4889 if (llvm::Constant *C = GetMethodConstant(MD))
4890 Methods.push_back(C);
4891 }
4892 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004893 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004894 Values[ 5] = EmitMethodList(MethodListName,
4895 "__DATA, __objc_const", Methods);
4896
Fariborz Jahanianda320092009-01-29 19:24:30 +00004897 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4898 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004899 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004900 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004901 OID->all_referenced_protocol_begin(),
4902 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004903
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004904 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004905 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004906 else
4907 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004908 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4909 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004910 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004911 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004912 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004913 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4914 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004915 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004916 Values);
4917 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004918 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4919 llvm::GlobalValue::InternalLinkage,
4920 Init,
4921 (flags & CLS_META) ?
4922 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4923 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004924 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004925 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004926 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004927 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004928
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004929}
4930
4931/// BuildClassMetaData - This routine defines that to-level meta-data
4932/// for the given ClassName for:
4933/// struct _class_t {
4934/// struct _class_t *isa;
4935/// struct _class_t * const superclass;
4936/// void *cache;
4937/// IMP *vtable;
4938/// struct class_ro_t *ro;
4939/// }
4940///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004941llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004942 std::string &ClassName,
4943 llvm::Constant *IsAGV,
4944 llvm::Constant *SuperClassGV,
4945 llvm::Constant *ClassRoGV,
4946 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004947 llvm::Constant *Values[] = {
4948 IsAGV,
4949 SuperClassGV,
4950 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
4951 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
4952 ClassRoGV // &CLASS_RO_GV
4953 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004954 if (!Values[1])
4955 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004956 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004957 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004958 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4959 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004960 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004961 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004962 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004963 if (HiddenVisibility)
4964 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004965 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004966}
4967
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004968bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004969CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004970 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004971}
4972
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004973void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004974 uint32_t &InstanceStart,
4975 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004976 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004977 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004978
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004979 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00004980 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004981
4982 // If there are no fields, the start is the same as the end.
4983 if (!RL.getFieldCount())
4984 InstanceStart = InstanceSize;
4985 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00004986 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004987}
4988
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004989void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4990 std::string ClassName = ID->getNameAsString();
4991 if (!ObjCEmptyCacheVar) {
4992 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004993 CGM.getModule(),
4994 ObjCTypes.CacheTy,
4995 false,
4996 llvm::GlobalValue::ExternalLinkage,
4997 0,
4998 "_objc_empty_cache");
4999
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005000 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005001 CGM.getModule(),
5002 ObjCTypes.ImpnfABITy,
5003 false,
5004 llvm::GlobalValue::ExternalLinkage,
5005 0,
5006 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005007 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005008 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005009 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00005010 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005011 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00005012 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005013 uint32_t InstanceSize = InstanceStart;
5014 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005015 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
5016 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005017
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005018 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005019
5020 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00005021 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005022 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005023 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005024 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005025 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005026 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005027 // class is root
5028 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005029 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005030 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005031 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005032 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005033 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5034 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5035 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005036 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005037 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005038 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005039 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005040 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005041 ObjCMetaClassName +
5042 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005043 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005044 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005045 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005046 }
5047 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5048 InstanceStart,
5049 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005050 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005051 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005052 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5053 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005054 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005055
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005056 // Metadata for the class
5057 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005058 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005059 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005060 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005061 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005062
Douglas Gregor68584ed2009-06-18 16:11:24 +00005063 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005064 flags |= CLS_EXCEPTION;
5065
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005066 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005067 flags |= CLS_ROOT;
5068 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005069 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005070 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005071 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005072 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005073 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005074 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005075 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005076 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005077 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005078 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005079 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005080 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005081 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005082
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005083 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005084 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005085 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5086 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005087 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005088
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005089 // Determine if this class is also "non-lazy".
5090 if (ImplementationIsNonLazy(ID))
5091 DefinedNonLazyClasses.push_back(ClassMD);
5092
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005093 // Force the definition of the EHType if necessary.
5094 if (flags & CLS_EXCEPTION)
5095 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005096 // Make sure method definition entries are all clear for next implementation.
5097 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005098}
5099
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005100/// GenerateProtocolRef - This routine is called to generate code for
5101/// a protocol reference expression; as in:
5102/// @code
5103/// @protocol(Proto1);
5104/// @endcode
5105/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5106/// which will hold address of the protocol meta-data.
5107///
5108llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005109 const ObjCProtocolDecl *PD) {
5110
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005111 // This routine is called for @protocol only. So, we must build definition
5112 // of protocol's meta-data (not a reference to it!)
5113 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005114 llvm::Constant *Init =
5115 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5116 ObjCTypes.ExternalProtocolPtrTy);
5117
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005118 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005119 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005120
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005121 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5122 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005123 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005124 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005125 CGM.getModule(),
5126 Init->getType(), false,
5127 llvm::GlobalValue::WeakAnyLinkage,
5128 Init,
5129 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005130 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5131 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005132 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005133 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005134}
5135
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005136/// GenerateCategory - Build metadata for a category implementation.
5137/// struct _category_t {
5138/// const char * const name;
5139/// struct _class_t *const cls;
5140/// const struct _method_list_t * const instance_methods;
5141/// const struct _method_list_t * const class_methods;
5142/// const struct _protocol_list_t * const protocols;
5143/// const struct _prop_list_t * const properties;
5144/// }
5145///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005146void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005147 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005148 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005149 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5150 "_$_" + OCD->getNameAsString());
5151 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005152 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005153
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005154 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005155 Values[0] = GetClassName(OCD->getIdentifier());
5156 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005157 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005158 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005159 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5160
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005161 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005162 std::vector<llvm::Constant*> Methods;
5163 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005164 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005165 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005166
5167 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005168 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005169 // Instance methods should always be defined.
5170 Methods.push_back(GetMethodConstant(*i));
5171 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005172
5173 Values[2] = EmitMethodList(MethodListName,
5174 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005175 Methods);
5176
5177 MethodListName = Prefix;
5178 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5179 OCD->getNameAsString();
5180 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005181 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005182 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005183 // Class methods should always be defined.
5184 Methods.push_back(GetMethodConstant(*i));
5185 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005186
5187 Values[3] = EmitMethodList(MethodListName,
5188 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005189 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005190 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005191 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005192 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005193 llvm::SmallString<256> ExtName;
5194 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5195 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005196 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005197 + Interface->getName() + "_$_"
5198 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005199 Category->protocol_begin(),
5200 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005201 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5202 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005203 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005204 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5205 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005206 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005207
5208 llvm::Constant *Init =
5209 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005210 Values);
5211 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005212 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005213 false,
5214 llvm::GlobalValue::InternalLinkage,
5215 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005216 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005217 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005218 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005219 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005220 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005221 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005222
5223 // Determine if this category is also "non-lazy".
5224 if (ImplementationIsNonLazy(OCD))
5225 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005226 // method definition entries must be clear for next implementation.
5227 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005228}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005229
5230/// GetMethodConstant - Return a struct objc_method constant for the
5231/// given method if it has been defined. The result is null if the
5232/// method has not been defined. The return value has type MethodPtrTy.
5233llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005234 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005235 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005236 if (!Fn)
5237 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005238
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005239 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005240 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005241 ObjCTypes.SelectorPtrTy),
5242 GetMethodVarType(MD),
5243 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5244 };
Owen Anderson08e25242009-07-27 22:29:56 +00005245 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005246}
5247
5248/// EmitMethodList - Build meta-data for method declarations
5249/// struct _method_list_t {
5250/// uint32_t entsize; // sizeof(struct _objc_method)
5251/// uint32_t method_count;
5252/// struct _objc_method method_list[method_count];
5253/// }
5254///
Chris Lattner5f9e2722011-07-23 10:55:15 +00005255llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005256 const char *Section,
5257 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005258 // Return null for empty list.
5259 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005260 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005261
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005262 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005263 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005264 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005265 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005266 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005267 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005268 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005269 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005270 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005271 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005272
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005273 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005274 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005275 llvm::GlobalValue::InternalLinkage, Init, Name);
5276 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005277 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005278 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005279 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005280}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005281
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005282/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5283/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005284llvm::GlobalVariable *
5285CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5286 const ObjCIvarDecl *Ivar) {
5287 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005288 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005289 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005290 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005291 CGM.getModule().getGlobalVariable(Name);
5292 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005293 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005294 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005295 false,
5296 llvm::GlobalValue::ExternalLinkage,
5297 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005298 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005299 return IvarOffsetGV;
5300}
5301
Daniel Dunbare83be122010-04-02 21:14:02 +00005302llvm::Constant *
5303CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5304 const ObjCIvarDecl *Ivar,
5305 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005306 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005307 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005308 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005309 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005310 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005311
Mike Stumpf5408fe2009-05-16 07:57:57 +00005312 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5313 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005314 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5315 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005316 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005317 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005318 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005319 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005320 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005321 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005322}
5323
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005324/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005325/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005326/// IvarListnfABIPtrTy.
5327/// struct _ivar_t {
5328/// unsigned long int *offset; // pointer to ivar offset location
5329/// char *name;
5330/// char *type;
5331/// uint32_t alignment;
5332/// uint32_t size;
5333/// }
5334/// struct _ivar_list_t {
5335/// uint32 entsize; // sizeof(struct _ivar_t)
5336/// uint32 count;
5337/// struct _iver_t list[count];
5338/// }
5339///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005340
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005341llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005342 const ObjCImplementationDecl *ID) {
5343
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005344 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005345
Jordy Rosedb8264e2011-07-22 02:08:32 +00005346 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005347 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005348
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005349 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005350
Jordy Rosedb8264e2011-07-22 02:08:32 +00005351 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005352 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005353 // Ignore unnamed bit-fields.
5354 if (!IVD->getDeclName())
5355 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005356 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005357 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005358 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005359 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5360 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005361 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005362 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005363 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005364 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005365 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005366 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005367 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005368 // NOTE. Size of a bitfield does not match gcc's, because of the
5369 // way bitfields are treated special in each. But I am told that
5370 // 'size' for bitfield ivars is ignored by the runtime so it does
5371 // not matter. If it matters, there is enough info to get the
5372 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005373 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005374 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005375 }
5376 // Return null for empty list.
5377 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005378 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005379
5380 llvm::Constant *Values[3];
Duncan Sands9408c452009-05-09 07:08:47 +00005381 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005382 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5383 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005384 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005385 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005386 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005387 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005388 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5389 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005390 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005391 llvm::GlobalValue::InternalLinkage,
5392 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005393 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005394 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005395 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005396 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005397
Chris Lattnerad64e022009-07-17 23:57:13 +00005398 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005399 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005400}
5401
5402llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005403 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005404 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005405
Fariborz Jahanianda320092009-01-29 19:24:30 +00005406 if (!Entry) {
5407 // We use the initializer as a marker of whether this is a forward
5408 // reference or not. At module finalization we add the empty
5409 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005410 Entry =
5411 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5412 llvm::GlobalValue::ExternalLinkage,
5413 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005414 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005415 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005416 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005417
Fariborz Jahanianda320092009-01-29 19:24:30 +00005418 return Entry;
5419}
5420
5421/// GetOrEmitProtocol - Generate the protocol meta-data:
5422/// @code
5423/// struct _protocol_t {
5424/// id isa; // NULL
5425/// const char * const protocol_name;
5426/// const struct _protocol_list_t * protocol_list; // super protocols
5427/// const struct method_list_t * const instance_methods;
5428/// const struct method_list_t * const class_methods;
5429/// const struct method_list_t *optionalInstanceMethods;
5430/// const struct method_list_t *optionalClassMethods;
5431/// const struct _prop_list_t * properties;
5432/// const uint32_t size; // sizeof(struct _protocol_t)
5433/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005434/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005435/// }
5436/// @endcode
5437///
5438
5439llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005440 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005441 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005442
Fariborz Jahanianda320092009-01-29 19:24:30 +00005443 // Early exit if a defining object has already been generated.
5444 if (Entry && Entry->hasInitializer())
5445 return Entry;
5446
Douglas Gregor1d784b22012-01-01 19:51:50 +00005447 // Use the protocol definition, if there is one.
5448 if (const ObjCProtocolDecl *Def = PD->getDefinition())
5449 PD = Def;
5450
Fariborz Jahanianda320092009-01-29 19:24:30 +00005451 // Construct method lists.
5452 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5453 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005454 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005455 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005456 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005457 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005458 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005459 if (!C)
5460 return GetOrEmitProtocolRef(PD);
5461
Fariborz Jahanianda320092009-01-29 19:24:30 +00005462 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5463 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005464 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005465 } else {
5466 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005467 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005468 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005469 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005470
5471 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005472 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005473 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005474 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005475 if (!C)
5476 return GetOrEmitProtocolRef(PD);
5477
Fariborz Jahanianda320092009-01-29 19:24:30 +00005478 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5479 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005480 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005481 } else {
5482 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005483 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005484 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005485 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005486
Bob Wilsondc8dab62011-11-30 01:57:58 +00005487 MethodTypesExt.insert(MethodTypesExt.end(),
5488 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5489
5490 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00005491 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005492 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005493 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005494 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5495 PD->protocol_begin(),
5496 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005497
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005498 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005499 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005500 "__DATA, __objc_const",
5501 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005502 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005503 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005504 "__DATA, __objc_const",
5505 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005506 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005507 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005508 "__DATA, __objc_const",
5509 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005510 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005511 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005512 "__DATA, __objc_const",
5513 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005514 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005515 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005516 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005517 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005518 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005519 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005520 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5521 + PD->getName(),
5522 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005523 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005524 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005525
Fariborz Jahanianda320092009-01-29 19:24:30 +00005526 if (Entry) {
5527 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005528 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005529 Entry->setInitializer(Init);
5530 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005531 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005532 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5533 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5534 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005535 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005536 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005537 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005538 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005539 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005540 CGM.AddUsedGlobal(Entry);
5541
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005542 // Use this protocol meta-data to build protocol list table in section
5543 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005544 llvm::GlobalVariable *PTGV =
5545 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5546 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5547 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005548 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005549 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005550 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005551 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005552 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005553 return Entry;
5554}
5555
5556/// EmitProtocolList - Generate protocol list meta-data:
5557/// @code
5558/// struct _protocol_list_t {
5559/// long protocol_count; // Note, this is 32/64 bit
5560/// struct _protocol_t[protocol_count];
5561/// }
5562/// @endcode
5563///
5564llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005565CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005566 ObjCProtocolDecl::protocol_iterator begin,
5567 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005568 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005569
Fariborz Jahanianda320092009-01-29 19:24:30 +00005570 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005571 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005572 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005573
Daniel Dunbar948e2582009-02-15 07:36:20 +00005574 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005575 llvm::SmallString<256> TmpName;
5576 Name.toVector(TmpName);
5577 llvm::GlobalVariable *GV =
5578 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005579 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005580 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005581
Daniel Dunbar948e2582009-02-15 07:36:20 +00005582 for (; begin != end; ++begin)
5583 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5584
Fariborz Jahanianda320092009-01-29 19:24:30 +00005585 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005586 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005587 ObjCTypes.ProtocolnfABIPtrTy));
5588
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005589 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005590 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005591 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005592 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005593 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005594 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005595 ProtocolRefs.size()),
5596 ProtocolRefs);
5597
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005598 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005599 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005600 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005601 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005602 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005603 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005604 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005605 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005606 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005607 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005608}
5609
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005610/// GetMethodDescriptionConstant - This routine build following meta-data:
5611/// struct _objc_method {
5612/// SEL _cmd;
5613/// char *method_type;
5614/// char *_imp;
5615/// }
5616
5617llvm::Constant *
5618CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005619 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005620 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005621 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5622 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005623 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005624 if (!Desc[1])
5625 return 0;
5626
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005627 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005628 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005629 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005630}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005631
5632/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5633/// This code gen. amounts to generating code for:
5634/// @code
5635/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5636/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005637///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005638LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005639 CodeGen::CodeGenFunction &CGF,
5640 QualType ObjectTy,
5641 llvm::Value *BaseValue,
5642 const ObjCIvarDecl *Ivar,
5643 unsigned CVRQualifiers) {
5644 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005645 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5646 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005647}
5648
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005649llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005650 CodeGen::CodeGenFunction &CGF,
5651 const ObjCInterfaceDecl *Interface,
5652 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005653 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005654}
5655
John McCallb1e81442011-05-13 23:16:18 +00005656static void appendSelectorForMessageRefTable(std::string &buffer,
5657 Selector selector) {
5658 if (selector.isUnarySelector()) {
5659 buffer += selector.getNameForSlot(0);
5660 return;
5661 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005662
John McCallb1e81442011-05-13 23:16:18 +00005663 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5664 buffer += selector.getNameForSlot(i);
5665 buffer += '_';
5666 }
5667}
5668
John McCall944c8432011-05-14 03:10:52 +00005669/// Emit a "v-table" message send. We emit a weak hidden-visibility
5670/// struct, initially containing the selector pointer and a pointer to
5671/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5672/// load and call the function pointer, passing the address of the
5673/// struct as the second parameter. The runtime determines whether
5674/// the selector is currently emitted using vtable dispatch; if so, it
5675/// substitutes a stub function which simply tail-calls through the
5676/// appropriate vtable slot, and if not, it substitues a stub function
5677/// which tail-calls objc_msgSend. Both stubs adjust the selector
5678/// argument to correctly point to the selector.
5679RValue
5680CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5681 ReturnValueSlot returnSlot,
5682 QualType resultType,
5683 Selector selector,
5684 llvm::Value *arg0,
5685 QualType arg0Type,
5686 bool isSuper,
5687 const CallArgList &formalArgs,
5688 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005689 // Compute the actual arguments.
5690 CallArgList args;
5691
John McCall944c8432011-05-14 03:10:52 +00005692 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005693 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005694 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5695 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005696
John McCall944c8432011-05-14 03:10:52 +00005697 // Second argument: a pointer to the message ref structure. Leave
5698 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005699 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5700
5701 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5702
5703 const CGFunctionInfo &fnInfo =
5704 CGM.getTypes().getFunctionInfo(resultType, args,
5705 FunctionType::ExtInfo());
5706
John McCallcba681a2011-05-14 21:12:11 +00005707 NullReturnState nullReturn;
5708
John McCall944c8432011-05-14 03:10:52 +00005709 // Find the function to call and the mangled name for the message
5710 // ref structure. Using a different mangled name wouldn't actually
5711 // be a problem; it would just be a waste.
5712 //
5713 // The runtime currently never uses vtable dispatch for anything
5714 // except normal, non-super message-sends.
5715 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005716 llvm::Constant *fn = 0;
5717 std::string messageRefName("\01l_");
5718 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005719 if (isSuper) {
5720 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5721 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005722 } else {
John McCallcba681a2011-05-14 21:12:11 +00005723 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005724 fn = ObjCTypes.getMessageSendStretFixupFn();
5725 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005726 }
John McCallb1e81442011-05-13 23:16:18 +00005727 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5728 fn = ObjCTypes.getMessageSendFpretFixupFn();
5729 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005730 } else {
John McCallb1e81442011-05-13 23:16:18 +00005731 if (isSuper) {
5732 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5733 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005734 } else {
John McCallb1e81442011-05-13 23:16:18 +00005735 fn = ObjCTypes.getMessageSendFixupFn();
5736 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005737 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005738 }
John McCallb1e81442011-05-13 23:16:18 +00005739 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5740 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005741
John McCallb1e81442011-05-13 23:16:18 +00005742 // Append the selector name, except use underscores anywhere we
5743 // would have used colons.
5744 appendSelectorForMessageRefTable(messageRefName, selector);
5745
5746 llvm::GlobalVariable *messageRef
5747 = CGM.getModule().getGlobalVariable(messageRefName);
5748 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005749 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005750 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005751 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005752 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5753 init->getType(),
5754 /*constant*/ false,
5755 llvm::GlobalValue::WeakAnyLinkage,
5756 init,
5757 messageRefName);
5758 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5759 messageRef->setAlignment(16);
5760 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5761 }
5762 llvm::Value *mref =
5763 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5764
John McCall944c8432011-05-14 03:10:52 +00005765 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005766 args[1].RV = RValue::get(mref);
5767
5768 // Load the function to call from the message ref table.
5769 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5770 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5771
5772 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2acc6e32011-07-18 04:24:23 +00005773 llvm::FunctionType *fnType =
John McCallb1e81442011-05-13 23:16:18 +00005774 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5775 callee = CGF.Builder.CreateBitCast(callee,
5776 llvm::PointerType::getUnqual(fnType));
5777
John McCallcba681a2011-05-14 21:12:11 +00005778 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5779 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005780}
5781
5782/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005783CodeGen::RValue
5784CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005785 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005786 QualType ResultType,
5787 Selector Sel,
5788 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005789 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005790 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005791 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005792 return isVTableDispatchedSelector(Sel)
5793 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005794 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005795 false, CallArgs, Method)
5796 : EmitMessageSend(CGF, Return, ResultType,
5797 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005798 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005799 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005800}
5801
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005802llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005803CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005804 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5805
Daniel Dunbardfff2302009-03-02 05:18:14 +00005806 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005807 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005808 false, llvm::GlobalValue::ExternalLinkage,
5809 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005810 }
5811
5812 return GV;
5813}
5814
John McCallf85e1932011-06-15 23:02:42 +00005815llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5816 IdentifierInfo *II) {
5817 llvm::GlobalVariable *&Entry = ClassReferences[II];
5818
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005819 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005820 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005821 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005822 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005823 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5824 false, llvm::GlobalValue::InternalLinkage,
5825 ClassGV,
5826 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005827 Entry->setAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005828 CGM.getTargetData().getABITypeAlignment(
5829 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005830 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005831 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005832 }
John McCallf85e1932011-06-15 23:02:42 +00005833
Benjamin Kramer578faa82011-09-27 21:06:10 +00005834 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005835}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005836
John McCallf85e1932011-06-15 23:02:42 +00005837llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5838 const ObjCInterfaceDecl *ID) {
5839 return EmitClassRefFromId(Builder, ID->getIdentifier());
5840}
5841
5842llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5843 CGBuilderTy &Builder) {
5844 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5845 return EmitClassRefFromId(Builder, II);
5846}
5847
Daniel Dunbar11394522009-04-18 08:51:00 +00005848llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005849CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005850 const ObjCInterfaceDecl *ID) {
5851 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005852
Daniel Dunbar11394522009-04-18 08:51:00 +00005853 if (!Entry) {
5854 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5855 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005856 Entry =
5857 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005858 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005859 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005860 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005861 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005862 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005863 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005864 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005865 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005866 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005867
Benjamin Kramer578faa82011-09-27 21:06:10 +00005868 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005869}
5870
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005871/// EmitMetaClassRef - Return a Value * of the address of _class_t
5872/// meta-data
5873///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005874llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5875 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005876 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5877 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005878 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005879
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005880 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005881 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005882 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005883 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005884 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005885 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005886 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005887 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005888 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005889 ObjCTypes.ClassnfABIPtrTy));
5890
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005891 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005892 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005893
Benjamin Kramer578faa82011-09-27 21:06:10 +00005894 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005895}
5896
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005897/// GetClass - Return a reference to the class for the given interface
5898/// decl.
5899llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5900 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005901 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005902 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5903 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5904 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5905 }
5906
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005907 return EmitClassRef(Builder, ID);
5908}
5909
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005910/// Generates a message send where the super is the receiver. This is
5911/// a message send to self with special delivery semantics indicating
5912/// which class's method should be called.
5913CodeGen::RValue
5914CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005915 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005916 QualType ResultType,
5917 Selector Sel,
5918 const ObjCInterfaceDecl *Class,
5919 bool isCategoryImpl,
5920 llvm::Value *Receiver,
5921 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005922 const CodeGen::CallArgList &CallArgs,
5923 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005924 // ...
5925 // Create and init a super structure; this is a (receiver, class)
5926 // pair we will pass to objc_msgSendSuper.
5927 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00005928 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005929
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005930 llvm::Value *ReceiverAsObject =
5931 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5932 CGF.Builder.CreateStore(ReceiverAsObject,
5933 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005934
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005935 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005936 llvm::Value *Target;
5937 if (IsClassMessage) {
5938 if (isCategoryImpl) {
5939 // Message sent to "super' in a class method defined in
5940 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005941 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005942 Target = CGF.Builder.CreateStructGEP(Target, 0);
5943 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005944 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005945 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005946 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005947 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005948
Mike Stumpf5408fe2009-05-16 07:57:57 +00005949 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5950 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00005951 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005952 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5953 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5954 CGF.Builder.CreateStore(Target,
5955 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005956
John McCall944c8432011-05-14 03:10:52 +00005957 return (isVTableDispatchedSelector(Sel))
5958 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005959 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00005960 true, CallArgs, Method)
5961 : EmitMessageSend(CGF, Return, ResultType,
5962 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005963 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00005964 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005965}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005966
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005967llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005968 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005969 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005970
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005971 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005972 llvm::Constant *Casted =
5973 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5974 ObjCTypes.SelectorPtrTy);
5975 Entry =
5976 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5977 llvm::GlobalValue::InternalLinkage,
5978 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005979 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005980 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005981 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005982
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005983 if (lval)
5984 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00005985 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
5986
5987 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5988 llvm::MDNode::get(VMContext,
5989 ArrayRef<llvm::Value*>()));
5990 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005991}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005992/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005993/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005994///
5995void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005996 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005997 llvm::Value *dst,
5998 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005999 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006000 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006001 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006002 assert(Size <= 8 && "does not support size > 8");
6003 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6004 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006005 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6006 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006007 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6008 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00006009 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
6010 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006011 return;
6012}
6013
6014/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
6015/// objc_assign_strongCast (id src, id *dst)
6016///
6017void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006018 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006019 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006020 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006021 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006022 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006023 assert(Size <= 8 && "does not support size > 8");
6024 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006025 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006026 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6027 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006028 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6029 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00006030 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006031 src, dst, "weakassign");
6032 return;
6033}
6034
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006035void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006036 CodeGen::CodeGenFunction &CGF,
6037 llvm::Value *DestPtr,
6038 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006039 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006040 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6041 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006042 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006043 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006044 return;
6045}
6046
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006047/// EmitObjCWeakRead - Code gen for loading value of a __weak
6048/// object: objc_read_weak (id *src)
6049///
6050llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006051 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006052 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006053 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006054 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6055 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006056 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006057 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006058 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006059 return read_weak;
6060}
6061
6062/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6063/// objc_assign_weak (id src, id *dst)
6064///
6065void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006066 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006067 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006068 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006069 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006070 assert(Size <= 8 && "does not support size > 8");
6071 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6072 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006073 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6074 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006075 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6076 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006077 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006078 src, dst, "weakassign");
6079 return;
6080}
6081
6082/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6083/// objc_assign_global (id src, id *dst)
6084///
6085void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006086 llvm::Value *src, llvm::Value *dst,
6087 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006088 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006089 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006090 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006091 assert(Size <= 8 && "does not support size > 8");
6092 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6093 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006094 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6095 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006096 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6097 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006098 if (!threadlocal)
6099 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6100 src, dst, "globalassign");
6101 else
6102 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6103 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006104 return;
6105}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006106
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006107void
John McCallf1549f62010-07-06 01:34:17 +00006108CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6109 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006110 EmitAtSynchronizedStmt(CGF, S,
6111 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6112 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006113}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006114
John McCall5a180392010-07-24 00:37:23 +00006115llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006116CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006117 // There's a particular fixed type info for 'id'.
6118 if (T->isObjCIdType() ||
6119 T->isObjCQualifiedIdType()) {
6120 llvm::Constant *IDEHType =
6121 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6122 if (!IDEHType)
6123 IDEHType =
6124 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6125 false,
6126 llvm::GlobalValue::ExternalLinkage,
6127 0, "OBJC_EHTYPE_id");
6128 return IDEHType;
6129 }
6130
6131 // All other types should be Objective-C interface pointer types.
6132 const ObjCObjectPointerType *PT =
6133 T->getAs<ObjCObjectPointerType>();
6134 assert(PT && "Invalid @catch type.");
6135 const ObjCInterfaceType *IT = PT->getInterfaceType();
6136 assert(IT && "Invalid @catch type.");
6137 return GetInterfaceEHType(IT->getDecl(), false);
6138}
6139
John McCallf1549f62010-07-06 01:34:17 +00006140void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6141 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006142 EmitTryCatchStmt(CGF, S,
6143 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6144 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6145 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006146}
6147
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006148/// EmitThrowStmt - Generate code for a throw statement.
6149void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6150 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006151 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006152 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006153 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006154 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006155 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006156 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006157 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006158 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006159 }
6160
John McCall7ec404c2010-10-16 08:21:07 +00006161 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006162 CGF.Builder.ClearInsertionPoint();
6163}
Daniel Dunbare588b992009-03-01 04:46:24 +00006164
John McCall5a180392010-07-24 00:37:23 +00006165llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006166CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006167 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006168 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006169
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006170 // If we don't need a definition, return the entry if found or check
6171 // if we use an external reference.
6172 if (!ForDefinition) {
6173 if (Entry)
6174 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006175
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006176 // If this type (or a super class) has the __objc_exception__
6177 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006178 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006179 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006180 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006181 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006182 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006183 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006184 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006185 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006186
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006187 // Otherwise we need to either make a new entry or fill in the
6188 // initializer.
6189 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006190 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006191 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006192 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006193 CGM.getModule().getGlobalVariable(VTableName);
6194 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006195 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6196 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006197 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006198 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006199
Chris Lattner77b89b82010-06-27 07:15:29 +00006200 llvm::Value *VTableIdx =
6201 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006202
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006203 llvm::Constant *Values[] = {
6204 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6205 GetClassName(ID->getIdentifier()),
6206 GetClassGlobal(ClassName)
6207 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006208 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006209 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006210
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006211 if (Entry) {
6212 Entry->setInitializer(Init);
6213 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006214 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006215 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006216 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006217 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006218 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006219 }
6220
John McCall1fb0caa2010-10-22 21:05:15 +00006221 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006222 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006223 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6224 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006225
6226 if (ForDefinition) {
6227 Entry->setSection("__DATA,__objc_const");
6228 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6229 } else {
6230 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6231 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006232
6233 return Entry;
6234}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006235
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006236/* *** */
6237
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006238CodeGen::CGObjCRuntime *
6239CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006240 if (CGM.getLangOptions().ObjCNonFragileABI)
6241 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006242 return new CGObjCMac(CGM);
6243}