blob: 121395676fdffb17f5fa66d0e25f2ad3b16b07b3 [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
Chris Lattner72db6c32009-04-22 02:44:54 +0000276 llvm::Constant *getEnumerationMutationFn() {
Daniel Dunbarc1ab9002009-07-11 20:32:50 +0000277 CodeGen::CodeGenTypes &Types = CGM.getTypes();
278 ASTContext &Ctx = CGM.getContext();
Chris Lattner72db6c32009-04-22 02:44:54 +0000279 // void objc_enumerationMutation (id)
Chris Lattner5f9e2722011-07-23 10:55:15 +0000280 SmallVector<CanQualType,1> Params;
John McCallead608a2010-02-26 00:48:12 +0000281 Params.push_back(Ctx.getCanonicalParamType(Ctx.getObjCIdType()));
Chris Lattner2acc6e32011-07-18 04:24:23 +0000282 llvm::FunctionType *FTy =
John McCall04a67a62010-02-05 21:31:56 +0000283 Types.GetFunctionType(Types.getFunctionInfo(Ctx.VoidTy, Params,
Rafael Espindola264ba482010-03-30 20:24:48 +0000284 FunctionType::ExtInfo()),
285 false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000286 return CGM.CreateRuntimeFunction(FTy, "objc_enumerationMutation");
287 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000288
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000289 /// GcReadWeakFn -- LLVM objc_read_weak (id *src) function.
Chris Lattner72db6c32009-04-22 02:44:54 +0000290 llvm::Constant *getGcReadWeakFn() {
291 // id objc_read_weak (id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000292 llvm::Type *args[] = { ObjectPtrTy->getPointerTo() };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000293 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000294 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner72db6c32009-04-22 02:44:54 +0000295 return CGM.CreateRuntimeFunction(FTy, "objc_read_weak");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000296 }
297
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000298 /// GcAssignWeakFn -- LLVM objc_assign_weak function.
Chris Lattner96508e12009-04-17 22:12:36 +0000299 llvm::Constant *getGcAssignWeakFn() {
300 // id objc_assign_weak (id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000301 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Chris Lattner96508e12009-04-17 22:12:36 +0000302 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000303 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattner96508e12009-04-17 22:12:36 +0000304 return CGM.CreateRuntimeFunction(FTy, "objc_assign_weak");
305 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000306
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000307 /// GcAssignGlobalFn -- LLVM objc_assign_global function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000308 llvm::Constant *getGcAssignGlobalFn() {
309 // id objc_assign_global(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000310 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000311 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000312 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000313 return CGM.CreateRuntimeFunction(FTy, "objc_assign_global");
314 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000315
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000316 /// GcAssignThreadLocalFn -- LLVM objc_assign_threadlocal function.
317 llvm::Constant *getGcAssignThreadLocalFn() {
318 // id objc_assign_threadlocal(id src, id * dest)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000319 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000320 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000321 llvm::FunctionType::get(ObjectPtrTy, args, false);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000322 return CGM.CreateRuntimeFunction(FTy, "objc_assign_threadlocal");
323 }
324
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000325 /// GcAssignIvarFn -- LLVM objc_assign_ivar function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000326 llvm::Constant *getGcAssignIvarFn() {
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +0000327 // id objc_assign_ivar(id, id *, ptrdiff_t)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000328 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo(),
329 CGM.PtrDiffTy };
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_ivar");
333 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000334
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000335 /// GcMemmoveCollectableFn -- LLVM objc_memmove_collectable function.
336 llvm::Constant *GcMemmoveCollectableFn() {
337 // void *objc_memmove_collectable(void *dst, const void *src, size_t size)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000338 llvm::Type *args[] = { Int8PtrTy, Int8PtrTy, LongTy };
John McCall0774cb82011-05-15 01:53:33 +0000339 llvm::FunctionType *FTy = llvm::FunctionType::get(Int8PtrTy, args, false);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +0000340 return CGM.CreateRuntimeFunction(FTy, "objc_memmove_collectable");
341 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000342
Fariborz Jahaniandb286862009-01-22 00:37:21 +0000343 /// GcAssignStrongCastFn -- LLVM objc_assign_strongCast function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000344 llvm::Constant *getGcAssignStrongCastFn() {
Fariborz Jahanian021a7a62010-07-20 20:30:03 +0000345 // id objc_assign_strongCast(id, id *)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000346 llvm::Type *args[] = { ObjectPtrTy, ObjectPtrTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000347 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000348 llvm::FunctionType::get(ObjectPtrTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000349 return CGM.CreateRuntimeFunction(FTy, "objc_assign_strongCast");
350 }
Anders Carlssonf57c5b22009-02-16 22:59:18 +0000351
352 /// ExceptionThrowFn - LLVM objc_exception_throw function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000353 llvm::Constant *getExceptionThrowFn() {
354 // void objc_exception_throw(id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000355 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000356 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000357 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000358 return CGM.CreateRuntimeFunction(FTy, "objc_exception_throw");
359 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000360
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000361 /// ExceptionRethrowFn - LLVM objc_exception_rethrow function.
362 llvm::Constant *getExceptionRethrowFn() {
363 // void objc_exception_rethrow(void)
John McCall0774cb82011-05-15 01:53:33 +0000364 llvm::FunctionType *FTy = llvm::FunctionType::get(CGM.VoidTy, false);
Fariborz Jahanian69677ea2010-05-28 17:34:43 +0000365 return CGM.CreateRuntimeFunction(FTy, "objc_exception_rethrow");
366 }
367
Daniel Dunbar1c566672009-02-24 01:43:46 +0000368 /// SyncEnterFn - LLVM object_sync_enter function.
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000369 llvm::Constant *getSyncEnterFn() {
370 // void objc_sync_enter (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000371 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000372 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000373 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerb02e53b2009-04-06 16:53:45 +0000374 return CGM.CreateRuntimeFunction(FTy, "objc_sync_enter");
375 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000376
Daniel Dunbar1c566672009-02-24 01:43:46 +0000377 /// SyncExitFn - LLVM object_sync_exit function.
Chris Lattnerbbccd612009-04-22 02:38:11 +0000378 llvm::Constant *getSyncExitFn() {
379 // void objc_sync_exit (id)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000380 llvm::Type *args[] = { ObjectPtrTy };
Chris Lattnerbbccd612009-04-22 02:38:11 +0000381 llvm::FunctionType *FTy =
John McCall0774cb82011-05-15 01:53:33 +0000382 llvm::FunctionType::get(CGM.VoidTy, args, false);
Chris Lattnerbbccd612009-04-22 02:38:11 +0000383 return CGM.CreateRuntimeFunction(FTy, "objc_sync_exit");
384 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000385
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000386 llvm::Constant *getSendFn(bool IsSuper) const {
387 return IsSuper ? getMessageSendSuperFn() : getMessageSendFn();
388 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000389
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000390 llvm::Constant *getSendFn2(bool IsSuper) const {
391 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFn();
392 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000393
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000394 llvm::Constant *getSendStretFn(bool IsSuper) const {
395 return IsSuper ? getMessageSendSuperStretFn() : getMessageSendStretFn();
396 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000397
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000398 llvm::Constant *getSendStretFn2(bool IsSuper) const {
399 return IsSuper ? getMessageSendSuperStretFn2() : getMessageSendStretFn();
400 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000401
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000402 llvm::Constant *getSendFpretFn(bool IsSuper) const {
403 return IsSuper ? getMessageSendSuperFpretFn() : getMessageSendFpretFn();
404 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000405
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +0000406 llvm::Constant *getSendFpretFn2(bool IsSuper) const {
407 return IsSuper ? getMessageSendSuperFpretFn2() : getMessageSendFpretFn();
408 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000409
Anders Carlssoneea64802011-10-31 16:27:11 +0000410 llvm::Constant *getSendFp2retFn(bool IsSuper) const {
411 return IsSuper ? getMessageSendSuperFn() : getMessageSendFp2retFn();
412 }
413
414 llvm::Constant *getSendFp2RetFn2(bool IsSuper) const {
415 return IsSuper ? getMessageSendSuperFn2() : getMessageSendFp2retFn();
416 }
417
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000418 ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm);
419 ~ObjCCommonTypesHelper(){}
420};
Daniel Dunbare8b470d2008-08-23 04:28:29 +0000421
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000422/// ObjCTypesHelper - Helper class that encapsulates lazy
423/// construction of varies types used during ObjC generation.
424class ObjCTypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000425public:
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000426 /// SymtabTy - LLVM type for struct objc_symtab.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000427 llvm::StructType *SymtabTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000428 /// SymtabPtrTy - LLVM type for struct objc_symtab *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000429 llvm::Type *SymtabPtrTy;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000430 /// ModuleTy - LLVM type for struct objc_module.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000431 llvm::StructType *ModuleTy;
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000432
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000433 /// ProtocolTy - LLVM type for struct objc_protocol.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000434 llvm::StructType *ProtocolTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000435 /// ProtocolPtrTy - LLVM type for struct objc_protocol *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000436 llvm::Type *ProtocolPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000437 /// ProtocolExtensionTy - LLVM type for struct
438 /// objc_protocol_extension.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000439 llvm::StructType *ProtocolExtensionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000440 /// ProtocolExtensionTy - LLVM type for struct
441 /// objc_protocol_extension *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000442 llvm::Type *ProtocolExtensionPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000443 /// MethodDescriptionTy - LLVM type for struct
444 /// objc_method_description.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000445 llvm::StructType *MethodDescriptionTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000446 /// MethodDescriptionListTy - LLVM type for struct
447 /// objc_method_description_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000448 llvm::StructType *MethodDescriptionListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000449 /// MethodDescriptionListPtrTy - LLVM type for struct
450 /// objc_method_description_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000451 llvm::Type *MethodDescriptionListPtrTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000452 /// ProtocolListTy - LLVM type for struct objc_property_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000453 llvm::StructType *ProtocolListTy;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000454 /// ProtocolListPtrTy - LLVM type for struct objc_property_list*.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000455 llvm::Type *ProtocolListPtrTy;
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000456 /// CategoryTy - LLVM type for struct objc_category.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000457 llvm::StructType *CategoryTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000458 /// ClassTy - LLVM type for struct objc_class.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000459 llvm::StructType *ClassTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000460 /// ClassPtrTy - LLVM type for struct objc_class *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000461 llvm::Type *ClassPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000462 /// ClassExtensionTy - LLVM type for struct objc_class_ext.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000463 llvm::StructType *ClassExtensionTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000464 /// ClassExtensionPtrTy - LLVM type for struct objc_class_ext *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000465 llvm::Type *ClassExtensionPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000466 // IvarTy - LLVM type for struct objc_ivar.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000467 llvm::StructType *IvarTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000468 /// IvarListTy - LLVM type for struct objc_ivar_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000469 llvm::Type *IvarListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000470 /// IvarListPtrTy - LLVM type for struct objc_ivar_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000471 llvm::Type *IvarListPtrTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000472 /// MethodListTy - LLVM type for struct objc_method_list.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000473 llvm::Type *MethodListTy;
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000474 /// MethodListPtrTy - LLVM type for struct objc_method_list *.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000475 llvm::Type *MethodListPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000476
Anders Carlsson124526b2008-09-09 10:10:21 +0000477 /// ExceptionDataTy - LLVM type for struct _objc_exception_data.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000478 llvm::Type *ExceptionDataTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000479
Anders Carlsson124526b2008-09-09 10:10:21 +0000480 /// ExceptionTryEnterFn - LLVM objc_exception_try_enter function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000481 llvm::Constant *getExceptionTryEnterFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000482 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000483 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000484 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000485 "objc_exception_try_enter");
Chris Lattner34b02a12009-04-22 02:26:14 +0000486 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000487
488 /// ExceptionTryExitFn - LLVM objc_exception_try_exit function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000489 llvm::Constant *getExceptionTryExitFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000490 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Andersona1cf15f2009-07-14 23:10:40 +0000491 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000492 llvm::FunctionType::get(CGM.VoidTy, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000493 "objc_exception_try_exit");
Chris Lattner34b02a12009-04-22 02:26:14 +0000494 }
Anders Carlsson124526b2008-09-09 10:10:21 +0000495
496 /// ExceptionExtractFn - LLVM objc_exception_extract function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000497 llvm::Constant *getExceptionExtractFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000498 llvm::Type *params[] = { ExceptionDataTy->getPointerTo() };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000499 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000500 params, false),
Chris Lattner34b02a12009-04-22 02:26:14 +0000501 "objc_exception_extract");
Chris Lattner34b02a12009-04-22 02:26:14 +0000502 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000503
Anders Carlsson124526b2008-09-09 10:10:21 +0000504 /// ExceptionMatchFn - LLVM objc_exception_match function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000505 llvm::Constant *getExceptionMatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000506 llvm::Type *params[] = { ClassPtrTy, ObjectPtrTy };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000507 return CGM.CreateRuntimeFunction(
John McCall0774cb82011-05-15 01:53:33 +0000508 llvm::FunctionType::get(CGM.Int32Ty, params, false),
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000509 "objc_exception_match");
510
Chris Lattner34b02a12009-04-22 02:26:14 +0000511 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000512
Anders Carlsson124526b2008-09-09 10:10:21 +0000513 /// SetJmpFn - LLVM _setjmp function.
Chris Lattner34b02a12009-04-22 02:26:14 +0000514 llvm::Constant *getSetJmpFn() {
John McCall0774cb82011-05-15 01:53:33 +0000515 // This is specifically the prototype for x86.
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000516 llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
John McCall0774cb82011-05-15 01:53:33 +0000517 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.Int32Ty,
518 params, false),
Bill Wendlinga9e269e2011-11-29 00:10:10 +0000519 "_setjmp",
520 llvm::Attribute::ReturnsTwice);
Chris Lattner34b02a12009-04-22 02:26:14 +0000521 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000522
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000523public:
524 ObjCTypesHelper(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000525 ~ObjCTypesHelper() {}
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000526};
527
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000528/// ObjCNonFragileABITypesHelper - will have all types needed by objective-c's
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000529/// modern abi
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000530class ObjCNonFragileABITypesHelper : public ObjCCommonTypesHelper {
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000531public:
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000532
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000533 // MethodListnfABITy - LLVM for struct _method_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000534 llvm::StructType *MethodListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000535
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000536 // MethodListnfABIPtrTy - LLVM for struct _method_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000537 llvm::Type *MethodListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000538
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000539 // ProtocolnfABITy = LLVM for struct _protocol_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000540 llvm::StructType *ProtocolnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000541
Daniel Dunbar948e2582009-02-15 07:36:20 +0000542 // ProtocolnfABIPtrTy = LLVM for struct _protocol_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000543 llvm::Type *ProtocolnfABIPtrTy;
Daniel Dunbar948e2582009-02-15 07:36:20 +0000544
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000545 // ProtocolListnfABITy - LLVM for struct _objc_protocol_list
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000546 llvm::StructType *ProtocolListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000547
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000548 // ProtocolListnfABIPtrTy - LLVM for struct _objc_protocol_list*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000549 llvm::Type *ProtocolListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000550
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000551 // ClassnfABITy - LLVM for struct _class_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000552 llvm::StructType *ClassnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000553
Fariborz Jahanianaa23b572009-01-23 23:53:38 +0000554 // ClassnfABIPtrTy - LLVM for struct _class_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000555 llvm::Type *ClassnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000556
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000557 // IvarnfABITy - LLVM for struct _ivar_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000558 llvm::StructType *IvarnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000559
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000560 // IvarListnfABITy - LLVM for struct _ivar_list_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000561 llvm::StructType *IvarListnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000562
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000563 // IvarListnfABIPtrTy = LLVM for struct _ivar_list_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000564 llvm::Type *IvarListnfABIPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000565
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000566 // ClassRonfABITy - LLVM for struct _class_ro_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000567 llvm::StructType *ClassRonfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000568
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000569 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000570 llvm::Type *ImpnfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000571
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +0000572 // CategorynfABITy - LLVM for struct _category_t
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000573 llvm::StructType *CategorynfABITy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000574
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000575 // New types for nonfragile abi messaging.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000576
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000577 // MessageRefTy - LLVM for:
578 // struct _message_ref_t {
579 // IMP messenger;
580 // SEL name;
581 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000582 llvm::StructType *MessageRefTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000583 // MessageRefCTy - clang type for struct _message_ref_t
584 QualType MessageRefCTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000585
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000586 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000587 llvm::Type *MessageRefPtrTy;
Fariborz Jahanian83a8a752009-02-04 20:42:28 +0000588 // MessageRefCPtrTy - clang type for struct _message_ref_t*
589 QualType MessageRefCPtrTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000590
Fariborz Jahanianef163782009-02-05 01:13:09 +0000591 // MessengerTy - Type of the messenger (shown as IMP above)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000592 llvm::FunctionType *MessengerTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000593
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000594 // SuperMessageRefTy - LLVM for:
595 // struct _super_message_ref_t {
596 // SUPER_IMP messenger;
597 // SEL name;
598 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000599 llvm::StructType *SuperMessageRefTy;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000600
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +0000601 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000602 llvm::Type *SuperMessageRefPtrTy;
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +0000603
Chris Lattner1c02f862009-04-22 02:53:24 +0000604 llvm::Constant *getMessageSendFixupFn() {
605 // id objc_msgSend_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000606 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000607 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000608 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000609 "objc_msgSend_fixup");
610 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000611
Chris Lattner1c02f862009-04-22 02:53:24 +0000612 llvm::Constant *getMessageSendFpretFixupFn() {
613 // id objc_msgSend_fpret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000614 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000615 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000616 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000617 "objc_msgSend_fpret_fixup");
618 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000619
Chris Lattner1c02f862009-04-22 02:53:24 +0000620 llvm::Constant *getMessageSendStretFixupFn() {
621 // id objc_msgSend_stret_fixup(id, struct message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000622 llvm::Type *params[] = { ObjectPtrTy, MessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000623 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000624 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000625 "objc_msgSend_stret_fixup");
626 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000627
Chris Lattner1c02f862009-04-22 02:53:24 +0000628 llvm::Constant *getMessageSendSuper2FixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000629 // id objc_msgSendSuper2_fixup (struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000630 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000631 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000632 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000633 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000634 "objc_msgSendSuper2_fixup");
635 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000636
Chris Lattner1c02f862009-04-22 02:53:24 +0000637 llvm::Constant *getMessageSendSuper2StretFixupFn() {
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000638 // id objc_msgSendSuper2_stret_fixup(struct objc_super *,
Chris Lattner1c02f862009-04-22 02:53:24 +0000639 // struct _super_message_ref_t*, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000640 llvm::Type *params[] = { SuperPtrTy, SuperMessageRefPtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000641 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(ObjectPtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000642 params, true),
Chris Lattner1c02f862009-04-22 02:53:24 +0000643 "objc_msgSendSuper2_stret_fixup");
644 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000645
Chris Lattner8a569112009-04-22 02:15:23 +0000646 llvm::Constant *getObjCEndCatchFn() {
John McCall0774cb82011-05-15 01:53:33 +0000647 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(CGM.VoidTy, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000648 "objc_end_catch");
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000649
Chris Lattner8a569112009-04-22 02:15:23 +0000650 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000651
Chris Lattner8a569112009-04-22 02:15:23 +0000652 llvm::Constant *getObjCBeginCatchFn() {
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000653 llvm::Type *params[] = { Int8PtrTy };
Owen Anderson96e0fc72009-07-29 22:16:19 +0000654 return CGM.CreateRuntimeFunction(llvm::FunctionType::get(Int8PtrTy,
John McCall0774cb82011-05-15 01:53:33 +0000655 params, false),
Chris Lattner8a569112009-04-22 02:15:23 +0000656 "objc_begin_catch");
657 }
Daniel Dunbare588b992009-03-01 04:46:24 +0000658
Chris Lattner9cbe4f02011-07-09 17:41:47 +0000659 llvm::StructType *EHTypeTy;
660 llvm::Type *EHTypePtrTy;
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +0000661
Fariborz Jahanian30bc5712009-01-22 23:02:58 +0000662 ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm);
663 ~ObjCNonFragileABITypesHelper(){}
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000664};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000665
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000666class CGObjCCommonMac : public CodeGen::CGObjCRuntime {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000667public:
668 // FIXME - accessibility
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000669 class GC_IVAR {
Fariborz Jahanian820e0202009-03-11 00:07:04 +0000670 public:
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000671 unsigned ivar_bytepos;
672 unsigned ivar_size;
673 GC_IVAR(unsigned bytepos = 0, unsigned size = 0)
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000674 : ivar_bytepos(bytepos), ivar_size(size) {}
Daniel Dunbar0941b492009-04-23 01:29:05 +0000675
676 // Allow sorting based on byte pos.
677 bool operator<(const GC_IVAR &b) const {
678 return ivar_bytepos < b.ivar_bytepos;
679 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000680 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000681
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000682 class SKIP_SCAN {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000683 public:
684 unsigned skip;
685 unsigned scan;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000686 SKIP_SCAN(unsigned _skip = 0, unsigned _scan = 0)
Daniel Dunbar8b2926c2009-05-03 13:44:42 +0000687 : skip(_skip), scan(_scan) {}
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000688 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000689
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000690protected:
691 CodeGen::CodeGenModule &CGM;
Owen Anderson69243822009-07-13 04:10:07 +0000692 llvm::LLVMContext &VMContext;
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000693 // FIXME! May not be needing this after all.
Daniel Dunbarbbce49b2008-08-12 00:12:39 +0000694 unsigned ObjCABI;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000695
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +0000696 // gc ivar layout bitmap calculation helper caches.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000697 SmallVector<GC_IVAR, 16> SkipIvars;
698 SmallVector<GC_IVAR, 16> IvarsInfo;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000699
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000700 /// LazySymbols - Symbols to generate a lazy reference for. See
701 /// DefinedSymbols and FinishModule().
Daniel Dunbar33063492009-09-07 00:20:42 +0000702 llvm::SetVector<IdentifierInfo*> LazySymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000703
Daniel Dunbar242d4dc2008-08-25 06:02:07 +0000704 /// DefinedSymbols - External symbols which are defined by this
705 /// module. The symbols in this list and LazySymbols are used to add
706 /// special linker symbols which ensure that Objective-C modules are
707 /// linked properly.
Daniel Dunbar33063492009-09-07 00:20:42 +0000708 llvm::SetVector<IdentifierInfo*> DefinedSymbols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000709
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000710 /// ClassNames - uniqued class names.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000711 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000712
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000713 /// MethodVarNames - uniqued method variable names.
714 llvm::DenseMap<Selector, llvm::GlobalVariable*> MethodVarNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000715
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +0000716 /// DefinedCategoryNames - list of category names in form Class_Category.
717 llvm::SetVector<std::string> DefinedCategoryNames;
718
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000719 /// MethodVarTypes - uniqued method type signatures. We have to use
720 /// a StringMap here because have no other unique reference.
721 llvm::StringMap<llvm::GlobalVariable*> MethodVarTypes;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000722
Daniel Dunbarc45ef602008-08-26 21:51:14 +0000723 /// MethodDefinitions - map of methods which have been defined in
724 /// this translation unit.
725 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*> MethodDefinitions;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000726
Daniel Dunbarc8ef5512008-08-23 00:19:03 +0000727 /// PropertyNames - uniqued method variable names.
728 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> PropertyNames;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000729
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000730 /// ClassReferences - uniqued class references.
731 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> ClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000732
Daniel Dunbar259d93d2008-08-12 03:39:23 +0000733 /// SelectorReferences - uniqued selector references.
734 llvm::DenseMap<Selector, llvm::GlobalVariable*> SelectorReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000735
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000736 /// Protocols - Protocols for which an objc_protocol structure has
737 /// been emitted. Forward declarations are handled by creating an
738 /// empty structure whose initializer is filled in when/if defined.
739 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000740
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000741 /// DefinedProtocols - Protocols which have actually been
742 /// defined. We should not need this, see FIXME in GenerateProtocol.
743 llvm::DenseSet<IdentifierInfo*> DefinedProtocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000744
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000745 /// DefinedClasses - List of defined classes.
746 std::vector<llvm::GlobalValue*> DefinedClasses;
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000747
748 /// DefinedNonLazyClasses - List of defined "non-lazy" classes.
749 std::vector<llvm::GlobalValue*> DefinedNonLazyClasses;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000750
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000751 /// DefinedCategories - List of defined categories.
752 std::vector<llvm::GlobalValue*> DefinedCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000753
Daniel Dunbar74d4b122009-05-15 22:33:15 +0000754 /// DefinedNonLazyCategories - List of defined "non-lazy" categories.
755 std::vector<llvm::GlobalValue*> DefinedNonLazyCategories;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000756
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000757 /// GetNameForMethod - Return a name for the given method.
758 /// \param[out] NameOut - The return value.
759 void GetNameForMethod(const ObjCMethodDecl *OMD,
760 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000761 SmallVectorImpl<char> &NameOut);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000762
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000763 /// GetMethodVarName - Return a unique constant for the given
764 /// selector's name. The return value has type char *.
765 llvm::Constant *GetMethodVarName(Selector Sel);
766 llvm::Constant *GetMethodVarName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000767
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000768 /// GetMethodVarType - Return a unique constant for the given
Bob Wilsondc8dab62011-11-30 01:57:58 +0000769 /// method's type encoding string. The return value has type char *.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000770
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000771 // FIXME: This is a horrible name.
Bob Wilsondc8dab62011-11-30 01:57:58 +0000772 llvm::Constant *GetMethodVarType(const ObjCMethodDecl *D,
773 bool Extended = false);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000774 llvm::Constant *GetMethodVarType(const FieldDecl *D);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000775
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000776 /// GetPropertyName - Return a unique constant for the given
777 /// name. The return value has type char *.
778 llvm::Constant *GetPropertyName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000779
Fariborz Jahanian56210f72009-01-21 23:34:32 +0000780 // FIXME: This can be dropped once string functions are unified.
781 llvm::Constant *GetPropertyTypeString(const ObjCPropertyDecl *PD,
782 const Decl *Container);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000783
Fariborz Jahanian058a1b72009-01-24 20:21:50 +0000784 /// GetClassName - Return a unique constant for the given selector's
785 /// name. The return value has type char *.
786 llvm::Constant *GetClassName(IdentifierInfo *Ident);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000787
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +0000788 llvm::Function *GetMethodDefinition(const ObjCMethodDecl *MD);
789
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000790 /// BuildIvarLayout - Builds ivar layout bitmap for the class
791 /// implementation for the __strong or __weak case.
792 ///
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000793 llvm::Constant *BuildIvarLayout(const ObjCImplementationDecl *OI,
794 bool ForStrongLayout);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +0000795
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +0000796 llvm::Constant *BuildIvarLayoutBitmap(std::string &BitMap);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000797
Daniel Dunbard58edcb2009-05-03 14:10:34 +0000798 void BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000799 unsigned int BytePos, bool ForStrongLayout,
800 bool &HasUnion);
Daniel Dunbar5a5a8032009-05-03 21:05:10 +0000801 void BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000802 const llvm::StructLayout *Layout,
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +0000803 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +0000804 const SmallVectorImpl<const FieldDecl*> &RecFields,
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +0000805 unsigned int BytePos, bool ForStrongLayout,
Fariborz Jahanian81adc052009-04-24 16:17:09 +0000806 bool &HasUnion);
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +0000807
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +0000808 /// GetIvarLayoutName - Returns a unique constant for the given
809 /// ivar layout bitmap.
810 llvm::Constant *GetIvarLayoutName(IdentifierInfo *Ident,
811 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000812
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000813 /// EmitPropertyList - Emit the given property list. The return
814 /// value has type PropertyListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000815 llvm::Constant *EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000816 const Decl *Container,
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +0000817 const ObjCContainerDecl *OCD,
818 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000819
Bob Wilsondc8dab62011-11-30 01:57:58 +0000820 /// EmitProtocolMethodTypes - Generate the array of extended method type
821 /// strings. The return value has type Int8PtrPtrTy.
822 llvm::Constant *EmitProtocolMethodTypes(Twine Name,
823 const ConstantVector &MethodTypes,
824 const ObjCCommonTypesHelper &ObjCTypes);
825
Fariborz Jahanian191dcd72009-12-12 21:26:21 +0000826 /// PushProtocolProperties - Push protocol's property on the input stack.
827 void PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
828 std::vector<llvm::Constant*> &Properties,
829 const Decl *Container,
830 const ObjCProtocolDecl *PROTO,
831 const ObjCCommonTypesHelper &ObjCTypes);
832
Fariborz Jahanianda320092009-01-29 19:24:30 +0000833 /// GetProtocolRef - Return a reference to the internal protocol
834 /// description, creating an empty one if it has not been
835 /// defined. The return value has type ProtocolPtrTy.
836 llvm::Constant *GetProtocolRef(const ObjCProtocolDecl *PD);
Fariborz Jahanianb21f07e2009-03-08 20:18:37 +0000837
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000838 /// CreateMetadataVar - Create a global variable with internal
839 /// linkage for use by the Objective-C runtime.
840 ///
841 /// This is a convenience wrapper which not only creates the
842 /// variable, but also sets the section and alignment and adds the
Chris Lattnerad64e022009-07-17 23:57:13 +0000843 /// global to the "llvm.used" list.
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000844 ///
845 /// \param Name - The variable name.
846 /// \param Init - The variable initializer; this is also used to
847 /// define the type of the variable.
848 /// \param Section - The section the variable should go into, or 0.
849 /// \param Align - The alignment for the variable, or 0.
850 /// \param AddToUsed - Whether the variable should be added to
Daniel Dunbarc1583062009-04-14 17:42:51 +0000851 /// "llvm.used".
Chris Lattner5f9e2722011-07-23 10:55:15 +0000852 llvm::GlobalVariable *CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000853 llvm::Constant *Init,
854 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +0000855 unsigned Align,
856 bool AddToUsed);
Daniel Dunbarfd65d372009-03-09 20:09:19 +0000857
John McCall944c8432011-05-14 03:10:52 +0000858 CodeGen::RValue EmitMessageSend(CodeGen::CodeGenFunction &CGF,
859 ReturnValueSlot Return,
860 QualType ResultType,
861 llvm::Value *Sel,
862 llvm::Value *Arg0,
863 QualType Arg0Ty,
864 bool IsSuper,
865 const CallArgList &CallArgs,
866 const ObjCMethodDecl *OMD,
867 const ObjCCommonTypesHelper &ObjCTypes);
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +0000868
Daniel Dunbarfce176b2010-04-25 20:39:01 +0000869 /// EmitImageInfo - Emit the image info marker used to encode some module
870 /// level information.
871 void EmitImageInfo();
872
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000873public:
Owen Anderson69243822009-07-13 04:10:07 +0000874 CGObjCCommonMac(CodeGen::CodeGenModule &cgm) :
Mike Stump1eb44332009-09-09 15:08:12 +0000875 CGM(cgm), VMContext(cgm.getLLVMContext()) { }
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000876
David Chisnall0d13f6f2010-01-23 02:40:42 +0000877 virtual llvm::Constant *GenerateConstantString(const StringLiteral *SL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000878
Fariborz Jahanian493dab72009-01-26 21:38:32 +0000879 virtual llvm::Function *GenerateMethod(const ObjCMethodDecl *OMD,
880 const ObjCContainerDecl *CD=0);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000881
Fariborz Jahanianda320092009-01-29 19:24:30 +0000882 virtual void GenerateProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000883
Fariborz Jahanianda320092009-01-29 19:24:30 +0000884 /// GetOrEmitProtocol - Get the protocol object for the given
885 /// declaration, emitting it if necessary. The return value has type
886 /// ProtocolPtrTy.
887 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD)=0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000888
Fariborz Jahanianda320092009-01-29 19:24:30 +0000889 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
890 /// object for the given declaration, emitting it if needed. These
891 /// forward references will be filled in with empty bodies if no
892 /// definition is seen. The return value has type ProtocolPtrTy.
893 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD)=0;
John McCall6b5a61b2011-02-07 10:33:21 +0000894 virtual llvm::Constant *BuildGCBlockLayout(CodeGen::CodeGenModule &CGM,
895 const CGBlockInfo &blockInfo);
Fariborz Jahanian89ecd412010-08-04 16:57:49 +0000896
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000897};
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000898
Fariborz Jahanianee0af742009-01-21 22:04:16 +0000899class CGObjCMac : public CGObjCCommonMac {
900private:
901 ObjCTypesHelper ObjCTypes;
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000902
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000903 /// EmitModuleInfo - Another marker encoding module level
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000904 /// information.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000905 void EmitModuleInfo();
906
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000907 /// EmitModuleSymols - Emit module symbols, the list of defined
908 /// classes and categories. The result has type SymtabPtrTy.
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +0000909 llvm::Constant *EmitModuleSymbols();
910
Daniel Dunbarf77ac862008-08-11 21:35:06 +0000911 /// FinishModule - Write out global data structures at the end of
912 /// processing a translation unit.
913 void FinishModule();
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000914
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000915 /// EmitClassExtension - Generate the class extension structure used
916 /// to store the weak ivar layout and properties. The return value
917 /// has type ClassExtensionPtrTy.
918 llvm::Constant *EmitClassExtension(const ObjCImplementationDecl *ID);
919
920 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
921 /// for the given class.
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000922 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000923 const ObjCInterfaceDecl *ID);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000924
John McCallf85e1932011-06-15 23:02:42 +0000925 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
926 IdentifierInfo *II);
927
928 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
929
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +0000930 /// EmitSuperClassRef - Emits reference to class's main metadata class.
931 llvm::Value *EmitSuperClassRef(const ObjCInterfaceDecl *ID);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000932
933 /// EmitIvarList - Emit the ivar list for the given
934 /// implementation. If ForClass is true the list of class ivars
935 /// (i.e. metaclass ivars) is emitted, otherwise the list of
936 /// interface ivars will be emitted. The return value has type
937 /// IvarListPtrTy.
938 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +0000939 bool ForClass);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000940
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000941 /// EmitMetaClass - Emit a forward reference to the class structure
942 /// for the metaclass of the given interface. The return value has
943 /// type ClassPtrTy.
944 llvm::Constant *EmitMetaClassRef(const ObjCInterfaceDecl *ID);
945
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000946 /// EmitMetaClass - Emit a class structure for the metaclass of the
Daniel Dunbarf56f1912008-08-25 08:19:24 +0000947 /// given implementation. The return value has type ClassPtrTy.
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000948 llvm::Constant *EmitMetaClass(const ObjCImplementationDecl *ID,
949 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000950 const ConstantVector &Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000951
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000952 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000953
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000954 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000955
956 /// EmitMethodList - Emit the method list for the given
Daniel Dunbaraf05bb92008-08-26 08:29:31 +0000957 /// implementation. The return value has type MethodListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000958 llvm::Constant *EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +0000959 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000960 const ConstantVector &Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +0000961
962 /// EmitMethodDescList - Emit a method description list for a list of
Daniel Dunbar6bff2512009-08-03 17:06:42 +0000963 /// method declarations.
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000964 /// - TypeName: The name for the type containing the methods.
965 /// - IsProtocol: True iff these methods are for a protocol.
966 /// - ClassMethds: True iff these are class methods.
967 /// - Required: When true, only "required" methods are
968 /// listed. Similarly, when false only "optional" methods are
969 /// listed. For classes this should always be true.
970 /// - begin, end: The method list to output.
971 ///
972 /// The return value has type MethodDescriptionListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000973 llvm::Constant *EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000974 const char *Section,
975 const ConstantVector &Methods);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000976
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000977 /// GetOrEmitProtocol - Get the protocol object for the given
978 /// declaration, emitting it if necessary. The return value has type
979 /// ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000980 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000981
982 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
983 /// object for the given declaration, emitting it if needed. These
984 /// forward references will be filled in with empty bodies if no
985 /// definition is seen. The return value has type ProtocolPtrTy.
Fariborz Jahanianda320092009-01-29 19:24:30 +0000986 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +0000987
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000988 /// EmitProtocolExtension - Generate the protocol extension
989 /// structure used to store optional instance and class methods, and
990 /// protocol properties. The return value has type
991 /// ProtocolExtensionPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +0000992 llvm::Constant *
993 EmitProtocolExtension(const ObjCProtocolDecl *PD,
994 const ConstantVector &OptInstanceMethods,
Bob Wilsondc8dab62011-11-30 01:57:58 +0000995 const ConstantVector &OptClassMethods,
996 const ConstantVector &MethodTypesExt);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +0000997
998 /// EmitProtocolList - Generate the list of referenced
999 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001000 llvm::Constant *EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001001 ObjCProtocolDecl::protocol_iterator begin,
1002 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001003
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001004 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1005 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001006 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1007 bool lval=false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001008
1009public:
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001010 CGObjCMac(CodeGen::CodeGenModule &cgm);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001011
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001012 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001013
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001014 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001015 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001016 QualType ResultType,
1017 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001018 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001019 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001020 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001021 const ObjCMethodDecl *Method);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001022
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001023 virtual CodeGen::RValue
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001024 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001025 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001026 QualType ResultType,
1027 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001028 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001029 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001030 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001031 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001032 const CallArgList &CallArgs,
1033 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001034
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001035 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001036 const ObjCInterfaceDecl *ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001037
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001038 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1039 bool lval = false);
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001040
1041 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1042 /// untyped one.
1043 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1044 const ObjCMethodDecl *Method);
1045
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001046 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001047
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001048 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001049
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00001050 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001051
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001052 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001053 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001054
Chris Lattner74391b42009-03-22 21:03:39 +00001055 virtual llvm::Constant *GetPropertyGetFunction();
1056 virtual llvm::Constant *GetPropertySetFunction();
David Chisnall8fac25d2010-12-26 22:13:16 +00001057 virtual llvm::Constant *GetGetStructFunction();
1058 virtual llvm::Constant *GetSetStructFunction();
Chris Lattner74391b42009-03-22 21:03:39 +00001059 virtual llvm::Constant *EnumerationMutationFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001060
John McCallf1549f62010-07-06 01:34:17 +00001061 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1062 const ObjCAtTryStmt &S);
1063 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1064 const ObjCAtSynchronizedStmt &S);
1065 void EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF, const Stmt &S);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00001066 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
1067 const ObjCAtThrowStmt &S);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001068 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001069 llvm::Value *AddrWeakObj);
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00001070 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001071 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001072 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001073 llvm::Value *src, llvm::Value *dest,
1074 bool threadlocal = false);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00001075 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001076 llvm::Value *src, llvm::Value *dest,
1077 llvm::Value *ivarOffset);
Fariborz Jahanian58626502008-11-19 00:59:10 +00001078 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
1079 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001080 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1081 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001082 llvm::Value *size);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001083
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001084 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1085 QualType ObjectTy,
1086 llvm::Value *BaseValue,
1087 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001088 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001089 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001090 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001091 const ObjCIvarDecl *Ivar);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001092
1093 /// GetClassGlobal - Return the global variable for the Objective-C
1094 /// class of the given name.
1095 virtual llvm::GlobalVariable *GetClassGlobal(const std::string &Name) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001096 llvm_unreachable("CGObjCMac::GetClassGlobal");
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001097 }
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001098};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001099
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001100class CGObjCNonFragileABIMac : public CGObjCCommonMac {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001101private:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001102 ObjCNonFragileABITypesHelper ObjCTypes;
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001103 llvm::GlobalVariable* ObjCEmptyCacheVar;
1104 llvm::GlobalVariable* ObjCEmptyVtableVar;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001105
Daniel Dunbar11394522009-04-18 08:51:00 +00001106 /// SuperClassReferences - uniqued super class references.
1107 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> SuperClassReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001108
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001109 /// MetaClassReferences - uniqued meta class references.
1110 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> MetaClassReferences;
Daniel Dunbare588b992009-03-01 04:46:24 +00001111
1112 /// EHTypeReferences - uniqued class ehtype references.
1113 llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*> EHTypeReferences;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001114
John McCall944c8432011-05-14 03:10:52 +00001115 /// VTableDispatchMethods - List of methods for which we generate
1116 /// vtable-based message dispatch.
1117 llvm::DenseSet<Selector> VTableDispatchMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001118
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00001119 /// DefinedMetaClasses - List of defined meta-classes.
1120 std::vector<llvm::GlobalValue*> DefinedMetaClasses;
1121
John McCall944c8432011-05-14 03:10:52 +00001122 /// isVTableDispatchedSelector - Returns true if SEL is a
1123 /// vtable-based selector.
1124 bool isVTableDispatchedSelector(Selector Sel);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001125
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001126 /// FinishNonFragileABIModule - Write out global data structures at the end of
1127 /// processing a translation unit.
1128 void FinishNonFragileABIModule();
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001129
Daniel Dunbar463b8762009-05-15 21:48:48 +00001130 /// AddModuleClassList - Add the given list of class pointers to the
1131 /// module with the provided symbol and section names.
1132 void AddModuleClassList(const std::vector<llvm::GlobalValue*> &Container,
1133 const char *SymbolName,
1134 const char *SectionName);
1135
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001136 llvm::GlobalVariable * BuildClassRoTInitializer(unsigned flags,
1137 unsigned InstanceStart,
1138 unsigned InstanceSize,
1139 const ObjCImplementationDecl *ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001140 llvm::GlobalVariable * BuildClassMetaData(std::string &ClassName,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001141 llvm::Constant *IsAGV,
Fariborz Jahanian84394a52009-01-24 21:21:53 +00001142 llvm::Constant *SuperClassGV,
Fariborz Jahaniancf555162009-01-31 00:59:10 +00001143 llvm::Constant *ClassRoGV,
1144 bool HiddenVisibility);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001145
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001146 llvm::Constant *GetMethodConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001147
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00001148 llvm::Constant *GetMethodDescriptionConstant(const ObjCMethodDecl *MD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001149
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001150 /// EmitMethodList - Emit the method list for the given
1151 /// implementation. The return value has type MethodListnfABITy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001152 llvm::Constant *EmitMethodList(Twine Name,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00001153 const char *Section,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00001154 const ConstantVector &Methods);
1155 /// EmitIvarList - Emit the ivar list for the given
1156 /// implementation. If ForClass is true the list of class ivars
1157 /// (i.e. metaclass ivars) is emitted, otherwise the list of
1158 /// interface ivars will be emitted. The return value has type
1159 /// IvarListnfABIPtrTy.
1160 llvm::Constant *EmitIvarList(const ObjCImplementationDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001161
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001162 llvm::Constant *EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00001163 const ObjCIvarDecl *Ivar,
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00001164 unsigned long int offset);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001165
Fariborz Jahanianda320092009-01-29 19:24:30 +00001166 /// GetOrEmitProtocol - Get the protocol object for the given
1167 /// declaration, emitting it if necessary. The return value has type
1168 /// ProtocolPtrTy.
1169 virtual llvm::Constant *GetOrEmitProtocol(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001170
Fariborz Jahanianda320092009-01-29 19:24:30 +00001171 /// GetOrEmitProtocolRef - Get a forward reference to the protocol
1172 /// object for the given declaration, emitting it if needed. These
1173 /// forward references will be filled in with empty bodies if no
1174 /// definition is seen. The return value has type ProtocolPtrTy.
1175 virtual llvm::Constant *GetOrEmitProtocolRef(const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001176
Fariborz Jahanianda320092009-01-29 19:24:30 +00001177 /// EmitProtocolList - Generate the list of referenced
1178 /// protocols. The return value has type ProtocolListPtrTy.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001179 llvm::Constant *EmitProtocolList(Twine Name,
Fariborz Jahanianda320092009-01-29 19:24:30 +00001180 ObjCProtocolDecl::protocol_iterator begin,
Fariborz Jahanian46551122009-02-04 00:22:57 +00001181 ObjCProtocolDecl::protocol_iterator end);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001182
John McCall944c8432011-05-14 03:10:52 +00001183 CodeGen::RValue EmitVTableMessageSend(CodeGen::CodeGenFunction &CGF,
1184 ReturnValueSlot Return,
1185 QualType ResultType,
1186 Selector Sel,
1187 llvm::Value *Receiver,
1188 QualType Arg0Ty,
1189 bool IsSuper,
1190 const CallArgList &CallArgs,
1191 const ObjCMethodDecl *Method);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001192
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00001193 /// GetClassGlobal - Return the global variable for the Objective-C
1194 /// class of the given name.
Fariborz Jahanian0f902942009-04-14 18:41:56 +00001195 llvm::GlobalVariable *GetClassGlobal(const std::string &Name);
Fariborz Jahanian6f40e222011-05-17 22:21:16 +00001196
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001197 /// EmitClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
Daniel Dunbar11394522009-04-18 08:51:00 +00001198 /// for the given class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001199 llvm::Value *EmitClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00001200 const ObjCInterfaceDecl *ID);
John McCallf85e1932011-06-15 23:02:42 +00001201
1202 llvm::Value *EmitClassRefFromId(CGBuilderTy &Builder,
1203 IdentifierInfo *II);
1204
1205 llvm::Value *EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001206
Daniel Dunbar11394522009-04-18 08:51:00 +00001207 /// EmitSuperClassRef - Return a Value*, of type ObjCTypes.ClassPtrTy,
1208 /// for the given super class reference.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001209 llvm::Value *EmitSuperClassRef(CGBuilderTy &Builder,
1210 const ObjCInterfaceDecl *ID);
1211
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001212 /// EmitMetaClassRef - Return a Value * of the address of _class_t
1213 /// meta-data
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001214 llvm::Value *EmitMetaClassRef(CGBuilderTy &Builder,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00001215 const ObjCInterfaceDecl *ID);
1216
Fariborz Jahanianed157d32009-02-10 20:21:06 +00001217 /// ObjCIvarOffsetVariable - Returns the ivar offset variable for
1218 /// the given ivar.
1219 ///
Daniel Dunbar5e88bea2009-04-19 00:31:15 +00001220 llvm::GlobalVariable * ObjCIvarOffsetVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001221 const ObjCInterfaceDecl *ID,
1222 const ObjCIvarDecl *Ivar);
1223
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00001224 /// EmitSelector - Return a Value*, of type ObjCTypes.SelectorPtrTy,
1225 /// for the given selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001226 llvm::Value *EmitSelector(CGBuilderTy &Builder, Selector Sel,
1227 bool lval=false);
Daniel Dunbare588b992009-03-01 04:46:24 +00001228
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001229 /// GetInterfaceEHType - Get the cached ehtype for the given Objective-C
Daniel Dunbare588b992009-03-01 04:46:24 +00001230 /// interface. The return value has type EHTypePtrTy.
John McCall5a180392010-07-24 00:37:23 +00001231 llvm::Constant *GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001232 bool ForDefinition);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001233
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001234 const char *getMetaclassSymbolPrefix() const {
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001235 return "OBJC_METACLASS_$_";
1236 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001237
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00001238 const char *getClassSymbolPrefix() const {
1239 return "OBJC_CLASS_$_";
1240 }
1241
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00001242 void GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001243 uint32_t &InstanceStart,
1244 uint32_t &InstanceSize);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001245
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001246 // Shamelessly stolen from Analysis/CFRefCount.cpp
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001247 Selector GetNullarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001248 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1249 return CGM.getContext().Selectors.getSelector(0, &II);
1250 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001251
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001252 Selector GetUnarySelector(const char* name) const {
Fariborz Jahanian4523eb02009-05-12 20:06:41 +00001253 IdentifierInfo* II = &CGM.getContext().Idents.get(name);
1254 return CGM.getContext().Selectors.getSelector(1, &II);
1255 }
Daniel Dunbarb02532a2009-04-19 23:41:48 +00001256
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001257 /// ImplementationIsNonLazy - Check whether the given category or
1258 /// class implementation is "non-lazy".
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00001259 bool ImplementationIsNonLazy(const ObjCImplDecl *OD) const;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00001260
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001261public:
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00001262 CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001263 // FIXME. All stubs for now!
1264 virtual llvm::Function *ModuleInitFunction();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001265
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001266 virtual CodeGen::RValue GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001267 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001268 QualType ResultType,
1269 Selector Sel,
1270 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001271 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001272 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001273 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001274
1275 virtual CodeGen::RValue
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001276 GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001277 ReturnValueSlot Return,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001278 QualType ResultType,
1279 Selector Sel,
1280 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001281 bool isCategoryImpl,
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001282 llvm::Value *Receiver,
1283 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001284 const CallArgList &CallArgs,
1285 const ObjCMethodDecl *Method);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001286
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001287 virtual llvm::Value *GetClass(CGBuilderTy &Builder,
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00001288 const ObjCInterfaceDecl *ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001289
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001290 virtual llvm::Value *GetSelector(CGBuilderTy &Builder, Selector Sel,
1291 bool lvalue = false)
1292 { return EmitSelector(Builder, Sel, lvalue); }
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001293
1294 /// The NeXT/Apple runtimes do not support typed selectors; just emit an
1295 /// untyped one.
1296 virtual llvm::Value *GetSelector(CGBuilderTy &Builder,
1297 const ObjCMethodDecl *Method)
1298 { return EmitSelector(Builder, Method->getSelector()); }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001299
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00001300 virtual void GenerateCategory(const ObjCCategoryImplDecl *CMD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001301
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001302 virtual void GenerateClass(const ObjCImplementationDecl *ClassDecl);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001303 virtual llvm::Value *GenerateProtocolRef(CGBuilderTy &Builder,
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00001304 const ObjCProtocolDecl *PD);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001305
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001306 virtual llvm::Constant *GetEHType(QualType T);
John McCall5a180392010-07-24 00:37:23 +00001307
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001308 virtual llvm::Constant *GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001309 return ObjCTypes.getGetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001310 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001311 virtual llvm::Constant *GetPropertySetFunction() {
1312 return ObjCTypes.getSetPropertyFn();
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001313 }
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001314
David Chisnall8fac25d2010-12-26 22:13:16 +00001315 virtual llvm::Constant *GetSetStructFunction() {
1316 return ObjCTypes.getCopyStructFn();
1317 }
1318 virtual llvm::Constant *GetGetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00001319 return ObjCTypes.getCopyStructFn();
1320 }
1321
Chris Lattner74391b42009-03-22 21:03:39 +00001322 virtual llvm::Constant *EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00001323 return ObjCTypes.getEnumerationMutationFn();
Daniel Dunbar28ed0842009-02-16 18:48:45 +00001324 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001325
John McCallf1549f62010-07-06 01:34:17 +00001326 virtual void EmitTryStmt(CodeGen::CodeGenFunction &CGF,
1327 const ObjCAtTryStmt &S);
1328 virtual void EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
1329 const ObjCAtSynchronizedStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001330 virtual void EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Anders Carlssonf57c5b22009-02-16 22:59:18 +00001331 const ObjCAtThrowStmt &S);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001332 virtual llvm::Value * EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001333 llvm::Value *AddrWeakObj);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001334 virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001335 llvm::Value *src, llvm::Value *dst);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001336 virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00001337 llvm::Value *src, llvm::Value *dest,
1338 bool threadlocal = false);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001339 virtual void EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00001340 llvm::Value *src, llvm::Value *dest,
1341 llvm::Value *ivarOffset);
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001342 virtual void EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00001343 llvm::Value *src, llvm::Value *dest);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00001344 virtual void EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
1345 llvm::Value *dest, llvm::Value *src,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00001346 llvm::Value *size);
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001347 virtual LValue EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
1348 QualType ObjectTy,
1349 llvm::Value *BaseValue,
1350 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00001351 unsigned CVRQualifiers);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001352 virtual llvm::Value *EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00001353 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00001354 const ObjCIvarDecl *Ivar);
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001355};
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001356
John McCallcba681a2011-05-14 21:12:11 +00001357/// A helper class for performing the null-initialization of a return
1358/// value.
1359struct NullReturnState {
1360 llvm::BasicBlock *NullBB;
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001361 llvm::BasicBlock *callBB;
1362 NullReturnState() : NullBB(0), callBB(0) {}
John McCallcba681a2011-05-14 21:12:11 +00001363
1364 void init(CodeGenFunction &CGF, llvm::Value *receiver) {
1365 // Make blocks for the null-init and call edges.
1366 NullBB = CGF.createBasicBlock("msgSend.nullinit");
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001367 callBB = CGF.createBasicBlock("msgSend.call");
John McCallcba681a2011-05-14 21:12:11 +00001368
1369 // Check for a null receiver and, if there is one, jump to the
1370 // null-init test.
1371 llvm::Value *isNull = CGF.Builder.CreateIsNull(receiver);
1372 CGF.Builder.CreateCondBr(isNull, NullBB, callBB);
1373
1374 // Otherwise, start performing the call.
1375 CGF.EmitBlock(callBB);
1376 }
1377
1378 RValue complete(CodeGenFunction &CGF, RValue result, QualType resultType) {
1379 if (!NullBB) return result;
1380
1381 // Finish the call path.
1382 llvm::BasicBlock *contBB = CGF.createBasicBlock("msgSend.cont");
1383 if (CGF.HaveInsertPoint()) CGF.Builder.CreateBr(contBB);
1384
1385 // Emit the null-init block and perform the null-initialization there.
1386 CGF.EmitBlock(NullBB);
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001387 if (!resultType->isAnyComplexType()) {
1388 assert(result.isAggregate() && "null init of non-aggregate result?");
1389 CGF.EmitNullInitialization(result.getAggregateAddr(), resultType);
1390 // Jump to the continuation block.
1391 CGF.EmitBlock(contBB);
1392 return result;
1393 }
John McCallcba681a2011-05-14 21:12:11 +00001394
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001395 // _Complex type
1396 // FIXME. Now easy to handle any other scalar type whose result is returned
1397 // in memory due to ABI limitations.
John McCallcba681a2011-05-14 21:12:11 +00001398 CGF.EmitBlock(contBB);
Fariborz Jahanian6c29eda2011-10-26 20:53:59 +00001399 CodeGenFunction::ComplexPairTy CallCV = result.getComplexVal();
1400 llvm::Type *MemberType = CallCV.first->getType();
1401 llvm::Constant *ZeroCV = llvm::Constant::getNullValue(MemberType);
1402 // Create phi instruction for scalar complex value.
1403 llvm::PHINode *PHIReal = CGF.Builder.CreatePHI(MemberType, 2);
1404 PHIReal->addIncoming(ZeroCV, NullBB);
1405 PHIReal->addIncoming(CallCV.first, callBB);
1406 llvm::PHINode *PHIImag = CGF.Builder.CreatePHI(MemberType, 2);
1407 PHIImag->addIncoming(ZeroCV, NullBB);
1408 PHIImag->addIncoming(CallCV.second, callBB);
1409 return RValue::getComplex(PHIReal, PHIImag);
John McCallcba681a2011-05-14 21:12:11 +00001410 }
1411};
1412
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001413} // end anonymous namespace
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001414
1415/* *** Helper Functions *** */
1416
1417/// getConstantGEP() - Help routine to construct simple GEPs.
Owen Andersona1cf15f2009-07-14 23:10:40 +00001418static llvm::Constant *getConstantGEP(llvm::LLVMContext &VMContext,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001419 llvm::Constant *C,
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001420 unsigned idx0,
1421 unsigned idx1) {
1422 llvm::Value *Idxs[] = {
Owen Anderson0032b272009-08-13 21:57:51 +00001423 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx0),
1424 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), idx1)
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001425 };
Jay Foada5c04342011-07-21 14:31:17 +00001426 return llvm::ConstantExpr::getGetElementPtr(C, Idxs);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001427}
1428
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001429/// hasObjCExceptionAttribute - Return true if this class or any super
1430/// class has the __objc_exception__ attribute.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001431static bool hasObjCExceptionAttribute(ASTContext &Context,
Douglas Gregor68584ed2009-06-18 16:11:24 +00001432 const ObjCInterfaceDecl *OID) {
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001433 if (OID->hasAttr<ObjCExceptionAttr>())
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001434 return true;
1435 if (const ObjCInterfaceDecl *Super = OID->getSuperClass())
Douglas Gregor68584ed2009-06-18 16:11:24 +00001436 return hasObjCExceptionAttribute(Context, Super);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00001437 return false;
1438}
1439
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001440/* *** CGObjCMac Public Interface *** */
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001441
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001442CGObjCMac::CGObjCMac(CodeGen::CodeGenModule &cgm) : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00001443 ObjCTypes(cgm) {
Fariborz Jahanianee0af742009-01-21 22:04:16 +00001444 ObjCABI = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001445 EmitImageInfo();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001446}
1447
Daniel Dunbarddb2a3d2008-08-16 00:25:02 +00001448/// GetClass - Return a reference to the class for the given interface
1449/// decl.
Daniel Dunbar45d196b2008-11-01 01:53:16 +00001450llvm::Value *CGObjCMac::GetClass(CGBuilderTy &Builder,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001451 const ObjCInterfaceDecl *ID) {
1452 return EmitClassRef(Builder, ID);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001453}
1454
1455/// GetSelector - Return the pointer to the unique'd string for this selector.
Fariborz Jahanian03b29602010-06-17 19:56:20 +00001456llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, Selector Sel,
1457 bool lval) {
1458 return EmitSelector(Builder, Sel, lval);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001459}
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001460llvm::Value *CGObjCMac::GetSelector(CGBuilderTy &Builder, const ObjCMethodDecl
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001461 *Method) {
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001462 return EmitSelector(Builder, Method->getSelector());
1463}
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001464
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001465llvm::Constant *CGObjCMac::GetEHType(QualType T) {
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001466 if (T->isObjCIdType() ||
1467 T->isObjCQualifiedIdType()) {
1468 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001469 CGM.getContext().getObjCIdRedefinitionType(), /*ForEH=*/true);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00001470 }
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001471 if (T->isObjCClassType() ||
1472 T->isObjCQualifiedClassType()) {
1473 return CGM.GetAddrOfRTTIDescriptor(
Douglas Gregor01a4cf12011-08-11 20:58:55 +00001474 CGM.getContext().getObjCClassRedefinitionType(), /*ForEH=*/true);
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00001475 }
1476 if (T->isObjCObjectPointerType())
1477 return CGM.GetAddrOfRTTIDescriptor(T, /*ForEH=*/true);
1478
John McCall5a180392010-07-24 00:37:23 +00001479 llvm_unreachable("asking for catch type for ObjC type in fragile runtime");
1480 return 0;
1481}
1482
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001483/// Generate a constant CFString object.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001484/*
1485 struct __builtin_CFString {
1486 const int *isa; // point to __CFConstantStringClassReference
1487 int flags;
1488 const char *str;
1489 long length;
1490 };
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00001491*/
1492
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001493/// or Generate a constant NSString object.
1494/*
1495 struct __builtin_NSString {
1496 const int *isa; // point to __NSConstantStringClassReference
1497 const char *str;
1498 unsigned int length;
1499 };
1500*/
1501
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00001502llvm::Constant *CGObjCCommonMac::GenerateConstantString(
David Chisnall0d13f6f2010-01-23 02:40:42 +00001503 const StringLiteral *SL) {
Fariborz Jahanian33e982b2010-04-22 20:26:39 +00001504 return (CGM.getLangOptions().NoConstantCFStrings == 0 ?
1505 CGM.GetAddrOfConstantCFString(SL) :
Fariborz Jahanian4c733072010-10-19 17:19:29 +00001506 CGM.GetAddrOfConstantString(SL));
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001507}
1508
1509/// Generates a message send where the super is the receiver. This is
1510/// a message send to self with special delivery semantics indicating
1511/// which class's method should be called.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001512CodeGen::RValue
1513CGObjCMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001514 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001515 QualType ResultType,
1516 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001517 const ObjCInterfaceDecl *Class,
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001518 bool isCategoryImpl,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001519 llvm::Value *Receiver,
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001520 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001521 const CodeGen::CallArgList &CallArgs,
1522 const ObjCMethodDecl *Method) {
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001523 // Create and init a super structure; this is a (receiver, class)
1524 // pair we will pass to objc_msgSendSuper.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001525 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00001526 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001527 llvm::Value *ReceiverAsObject =
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001528 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001529 CGF.Builder.CreateStore(ReceiverAsObject,
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001530 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbare8b470d2008-08-23 04:28:29 +00001531
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001532 // If this is a class message the metaclass is passed as the target.
1533 llvm::Value *Target;
1534 if (IsClassMessage) {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001535 if (isCategoryImpl) {
1536 // Message sent to 'super' in a class method defined in a category
1537 // implementation requires an odd treatment.
1538 // If we are in a class method, we must retrieve the
1539 // _metaclass_ for the current class, pointed at by
1540 // the class's "isa" pointer. The following assumes that
1541 // isa" is the first ivar in a class (which it must be).
1542 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1543 Target = CGF.Builder.CreateStructGEP(Target, 0);
1544 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00001545 } else {
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00001546 llvm::Value *MetaClassPtr = EmitMetaClassRef(Class);
1547 llvm::Value *SuperPtr = CGF.Builder.CreateStructGEP(MetaClassPtr, 1);
1548 llvm::Value *Super = CGF.Builder.CreateLoad(SuperPtr);
1549 Target = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001550 }
Fariborz Jahanian182f2682009-11-14 02:18:31 +00001551 }
1552 else if (isCategoryImpl)
1553 Target = EmitClassRef(CGF.Builder, Class->getSuperClass());
1554 else {
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00001555 llvm::Value *ClassPtr = EmitSuperClassRef(Class);
1556 ClassPtr = CGF.Builder.CreateStructGEP(ClassPtr, 1);
1557 Target = CGF.Builder.CreateLoad(ClassPtr);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001558 }
Mike Stumpf5408fe2009-05-16 07:57:57 +00001559 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
1560 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00001561 llvm::Type *ClassTy =
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001562 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001563 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001564 CGF.Builder.CreateStore(Target,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001565 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
John McCall944c8432011-05-14 03:10:52 +00001566 return EmitMessageSend(CGF, Return, ResultType,
1567 EmitSelector(CGF.Builder, Sel),
1568 ObjCSuper, ObjCTypes.SuperPtrCTy,
1569 true, CallArgs, Method, ObjCTypes);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001570}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001571
1572/// Generate code for a message send expression.
Daniel Dunbar8f2926b2008-08-23 03:46:30 +00001573CodeGen::RValue CGObjCMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00001574 ReturnValueSlot Return,
Daniel Dunbar7f8ea5c2008-08-30 05:35:15 +00001575 QualType ResultType,
1576 Selector Sel,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00001577 llvm::Value *Receiver,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001578 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00001579 const ObjCInterfaceDecl *Class,
Fariborz Jahaniandf9ccc62009-05-05 21:36:57 +00001580 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00001581 return EmitMessageSend(CGF, Return, ResultType,
1582 EmitSelector(CGF.Builder, Sel),
1583 Receiver, CGF.getContext().getObjCIdType(),
1584 false, CallArgs, Method, ObjCTypes);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00001585}
1586
Daniel Dunbard6c93d72009-09-17 04:01:22 +00001587CodeGen::RValue
John McCall944c8432011-05-14 03:10:52 +00001588CGObjCCommonMac::EmitMessageSend(CodeGen::CodeGenFunction &CGF,
1589 ReturnValueSlot Return,
1590 QualType ResultType,
1591 llvm::Value *Sel,
1592 llvm::Value *Arg0,
1593 QualType Arg0Ty,
1594 bool IsSuper,
1595 const CallArgList &CallArgs,
1596 const ObjCMethodDecl *Method,
1597 const ObjCCommonTypesHelper &ObjCTypes) {
Daniel Dunbar19cd87e2008-08-30 03:02:31 +00001598 CallArgList ActualArgs;
Fariborz Jahaniand019d962009-04-24 21:07:43 +00001599 if (!IsSuper)
Benjamin Kramer578faa82011-09-27 21:06:10 +00001600 Arg0 = CGF.Builder.CreateBitCast(Arg0, ObjCTypes.ObjectPtrTy);
Eli Friedman04c9a492011-05-02 17:57:46 +00001601 ActualArgs.add(RValue::get(Arg0), Arg0Ty);
1602 ActualArgs.add(RValue::get(Sel), CGF.getContext().getObjCSelType());
John McCallf85e1932011-06-15 23:02:42 +00001603 ActualArgs.addFrom(CallArgs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001604
Daniel Dunbar541b63b2009-02-02 23:23:47 +00001605 CodeGenTypes &Types = CGM.getTypes();
John McCall04a67a62010-02-05 21:31:56 +00001606 const CGFunctionInfo &FnInfo = Types.getFunctionInfo(ResultType, ActualArgs,
Rafael Espindola264ba482010-03-30 20:24:48 +00001607 FunctionType::ExtInfo());
Chris Lattner2acc6e32011-07-18 04:24:23 +00001608 llvm::FunctionType *FTy =
Daniel Dunbar67939662009-09-17 04:01:40 +00001609 Types.GetFunctionType(FnInfo, Method ? Method->isVariadic() : false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001610
Anders Carlsson7e70fb22010-06-21 20:59:55 +00001611 if (Method)
1612 assert(CGM.getContext().getCanonicalType(Method->getResultType()) ==
1613 CGM.getContext().getCanonicalType(ResultType) &&
1614 "Result type mismatch!");
1615
John McCallcba681a2011-05-14 21:12:11 +00001616 NullReturnState nullReturn;
1617
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001618 llvm::Constant *Fn = NULL;
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001619 if (CGM.ReturnTypeUsesSRet(FnInfo)) {
John McCallcba681a2011-05-14 21:12:11 +00001620 if (!IsSuper) nullReturn.init(CGF, Arg0);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001621 Fn = (ObjCABI == 2) ? ObjCTypes.getSendStretFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001622 : ObjCTypes.getSendStretFn(IsSuper);
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001623 } else if (CGM.ReturnTypeUsesFPRet(ResultType)) {
1624 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFpretFn2(IsSuper)
1625 : ObjCTypes.getSendFpretFn(IsSuper);
Anders Carlssoneea64802011-10-31 16:27:11 +00001626 } else if (CGM.ReturnTypeUsesFP2Ret(ResultType)) {
1627 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFp2RetFn2(IsSuper)
1628 : ObjCTypes.getSendFp2retFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001629 } else {
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00001630 Fn = (ObjCABI == 2) ? ObjCTypes.getSendFn2(IsSuper)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001631 : ObjCTypes.getSendFn(IsSuper);
Daniel Dunbar5669e572008-10-17 03:24:53 +00001632 }
Daniel Dunbardacf9dd2010-07-14 23:39:36 +00001633 Fn = llvm::ConstantExpr::getBitCast(Fn, llvm::PointerType::getUnqual(FTy));
John McCallcba681a2011-05-14 21:12:11 +00001634 RValue rvalue = CGF.EmitCall(FnInfo, Fn, Return, ActualArgs);
1635 return nullReturn.complete(CGF, rvalue, ResultType);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001636}
1637
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001638static Qualifiers::GC GetGCAttrTypeForType(ASTContext &Ctx, QualType FQT) {
1639 if (FQT.isObjCGCStrong())
1640 return Qualifiers::Strong;
1641
John McCallf85e1932011-06-15 23:02:42 +00001642 if (FQT.isObjCGCWeak() || FQT.getObjCLifetime() == Qualifiers::OCL_Weak)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001643 return Qualifiers::Weak;
1644
1645 if (FQT->isObjCObjectPointerType() || FQT->isBlockPointerType())
1646 return Qualifiers::Strong;
1647
1648 if (const PointerType *PT = FQT->getAs<PointerType>())
1649 return GetGCAttrTypeForType(Ctx, PT->getPointeeType());
1650
1651 return Qualifiers::GCNone;
1652}
1653
John McCall6b5a61b2011-02-07 10:33:21 +00001654llvm::Constant *CGObjCCommonMac::BuildGCBlockLayout(CodeGenModule &CGM,
1655 const CGBlockInfo &blockInfo) {
1656 llvm::Constant *nullPtr =
Fariborz Jahanian44034db2010-08-04 18:44:59 +00001657 llvm::Constant::getNullValue(llvm::Type::getInt8PtrTy(VMContext));
John McCall6b5a61b2011-02-07 10:33:21 +00001658
Douglas Gregore289d812011-09-13 17:21:33 +00001659 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00001660 !CGM.getLangOptions().ObjCAutoRefCount)
John McCall6b5a61b2011-02-07 10:33:21 +00001661 return nullPtr;
1662
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001663 bool hasUnion = false;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001664 SkipIvars.clear();
1665 IvarsInfo.clear();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001666 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
1667 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001668
Fariborz Jahanian81979822010-09-09 00:21:45 +00001669 // __isa is the first field in block descriptor and must assume by runtime's
1670 // convention that it is GC'able.
1671 IvarsInfo.push_back(GC_IVAR(0, 1));
John McCall6b5a61b2011-02-07 10:33:21 +00001672
1673 const BlockDecl *blockDecl = blockInfo.getBlockDecl();
1674
1675 // Calculate the basic layout of the block structure.
1676 const llvm::StructLayout *layout =
1677 CGM.getTargetData().getStructLayout(blockInfo.StructureType);
1678
1679 // Ignore the optional 'this' capture: C++ objects are not assumed
1680 // to be GC'ed.
1681
1682 // Walk the captured variables.
1683 for (BlockDecl::capture_const_iterator ci = blockDecl->capture_begin(),
1684 ce = blockDecl->capture_end(); ci != ce; ++ci) {
1685 const VarDecl *variable = ci->getVariable();
1686 QualType type = variable->getType();
1687
1688 const CGBlockInfo::Capture &capture = blockInfo.getCapture(variable);
1689
1690 // Ignore constant captures.
1691 if (capture.isConstant()) continue;
1692
1693 uint64_t fieldOffset = layout->getElementOffset(capture.getIndex());
1694
1695 // __block variables are passed by their descriptor address.
1696 if (ci->isByRef()) {
1697 IvarsInfo.push_back(GC_IVAR(fieldOffset, /*size in words*/ 1));
Fariborz Jahanianc5904b42010-09-11 01:27:29 +00001698 continue;
John McCall6b5a61b2011-02-07 10:33:21 +00001699 }
1700
1701 assert(!type->isArrayType() && "array variable should not be caught");
1702 if (const RecordType *record = type->getAs<RecordType>()) {
1703 BuildAggrIvarRecordLayout(record, fieldOffset, true, hasUnion);
Fariborz Jahaniane1a48982010-08-05 21:00:25 +00001704 continue;
1705 }
Fariborz Jahaniana1f024c2010-08-06 16:28:55 +00001706
John McCall6b5a61b2011-02-07 10:33:21 +00001707 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), type);
1708 unsigned fieldSize = CGM.getContext().getTypeSize(type);
1709
1710 if (GCAttr == Qualifiers::Strong)
1711 IvarsInfo.push_back(GC_IVAR(fieldOffset,
1712 fieldSize / WordSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001713 else if (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak)
John McCall6b5a61b2011-02-07 10:33:21 +00001714 SkipIvars.push_back(GC_IVAR(fieldOffset,
1715 fieldSize / ByteSizeInBits));
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001716 }
1717
1718 if (IvarsInfo.empty())
John McCall6b5a61b2011-02-07 10:33:21 +00001719 return nullPtr;
1720
1721 // Sort on byte position; captures might not be allocated in order,
1722 // and unions can do funny things.
1723 llvm::array_pod_sort(IvarsInfo.begin(), IvarsInfo.end());
1724 llvm::array_pod_sort(SkipIvars.begin(), SkipIvars.end());
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001725
1726 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00001727 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00001728 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
1729 printf("\n block variable layout for block: ");
1730 const unsigned char *s = (unsigned char*)BitMap.c_str();
1731 for (unsigned i = 0; i < BitMap.size(); i++)
1732 if (!(s[i] & 0xf0))
1733 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
1734 else
1735 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
1736 printf("\n");
1737 }
1738
1739 return C;
Fariborz Jahanian89ecd412010-08-04 16:57:49 +00001740}
1741
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001742llvm::Value *CGObjCMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbaraf2f62c2008-08-13 00:59:25 +00001743 const ObjCProtocolDecl *PD) {
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001744 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001745 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbarc67876d2008-09-04 04:33:15 +00001746 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1747
Owen Anderson3c4972d2009-07-29 18:54:39 +00001748 return llvm::ConstantExpr::getBitCast(GetProtocolRef(PD),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001749 ObjCTypes.ExternalProtocolPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00001750}
1751
Fariborz Jahanianda320092009-01-29 19:24:30 +00001752void CGObjCCommonMac::GenerateProtocol(const ObjCProtocolDecl *PD) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00001753 // FIXME: We shouldn't need this, the protocol decl should contain enough
1754 // information to tell us whether this was a declaration or a definition.
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001755 DefinedProtocols.insert(PD->getIdentifier());
1756
1757 // If we have generated a forward reference to this protocol, emit
1758 // it now. Otherwise do nothing, the protocol objects are lazily
1759 // emitted.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001760 if (Protocols.count(PD->getIdentifier()))
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001761 GetOrEmitProtocol(PD);
1762}
1763
Fariborz Jahanianda320092009-01-29 19:24:30 +00001764llvm::Constant *CGObjCCommonMac::GetProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001765 if (DefinedProtocols.count(PD->getIdentifier()))
1766 return GetOrEmitProtocol(PD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001767
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001768 return GetOrEmitProtocolRef(PD);
1769}
1770
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001771/*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001772// APPLE LOCAL radar 4585769 - Objective-C 1.0 extensions
1773struct _objc_protocol {
1774struct _objc_protocol_extension *isa;
1775char *protocol_name;
1776struct _objc_protocol_list *protocol_list;
1777struct _objc__method_prototype_list *instance_methods;
1778struct _objc__method_prototype_list *class_methods
1779};
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001780
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001781See EmitProtocolExtension().
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001782*/
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001783llvm::Constant *CGObjCMac::GetOrEmitProtocol(const ObjCProtocolDecl *PD) {
1784 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1785
1786 // Early exit if a defining object has already been generated.
1787 if (Entry && Entry->hasInitializer())
1788 return Entry;
1789
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001790 // FIXME: I don't understand why gcc generates this, or where it is
Mike Stumpf5408fe2009-05-16 07:57:57 +00001791 // resolved. Investigate. Its also wasteful to look this up over and over.
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00001792 LazySymbols.insert(&CGM.getContext().Idents.get("Protocol"));
1793
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001794 // Construct method lists.
1795 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
1796 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001797 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001798 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001799 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001800 ObjCMethodDecl *MD = *i;
1801 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001802 if (!C)
1803 return GetOrEmitProtocolRef(PD);
1804
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001805 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1806 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001807 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001808 } else {
1809 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001810 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001811 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001812 }
1813
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001814 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001815 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001816 ObjCMethodDecl *MD = *i;
1817 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00001818 if (!C)
1819 return GetOrEmitProtocolRef(PD);
1820
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001821 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
1822 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001823 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001824 } else {
1825 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00001826 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001827 }
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001828 }
1829
Bob Wilsondc8dab62011-11-30 01:57:58 +00001830 MethodTypesExt.insert(MethodTypesExt.end(),
1831 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
1832
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001833 llvm::Constant *Values[] = {
Bob Wilsondc8dab62011-11-30 01:57:58 +00001834 EmitProtocolExtension(PD, OptInstanceMethods, OptClassMethods,
1835 MethodTypesExt),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001836 GetClassName(PD->getIdentifier()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001837 EmitProtocolList("\01L_OBJC_PROTOCOL_REFS_" + PD->getName(),
Daniel Dunbardbc93372008-08-21 21:57:41 +00001838 PD->protocol_begin(),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001839 PD->protocol_end()),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001840 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001841 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001842 InstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001843 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001844 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001845 ClassMethods)
1846 };
Owen Anderson08e25242009-07-27 22:29:56 +00001847 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001848 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001849
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001850 if (Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001851 // Already created, fix the linkage and update the initializer.
1852 Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001853 Entry->setInitializer(Init);
1854 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001855 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001856 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001857 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001858 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001859 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001860 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001861 // FIXME: Is this necessary? Why only for protocol?
1862 Entry->setAlignment(4);
1863 }
Chris Lattnerad64e022009-07-17 23:57:13 +00001864 CGM.AddUsedGlobal(Entry);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001865
1866 return Entry;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001867}
1868
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001869llvm::Constant *CGObjCMac::GetOrEmitProtocolRef(const ObjCProtocolDecl *PD) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001870 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
1871
1872 if (!Entry) {
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001873 // We use the initializer as a marker of whether this is a forward
1874 // reference or not. At module finalization we add the empty
1875 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001876 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00001877 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolTy, false,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00001878 llvm::GlobalValue::ExternalLinkage,
1879 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001880 "\01L_OBJC_PROTOCOL_" + PD->getName());
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001881 Entry->setSection("__OBJC,__protocol,regular,no_dead_strip");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001882 // FIXME: Is this necessary? Why only for protocol?
1883 Entry->setAlignment(4);
1884 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001885
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001886 return Entry;
1887}
1888
1889/*
1890 struct _objc_protocol_extension {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001891 uint32_t size;
1892 struct objc_method_description_list *optional_instance_methods;
1893 struct objc_method_description_list *optional_class_methods;
1894 struct objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00001895 const char ** extendedMethodTypes;
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001896 };
1897*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001898llvm::Constant *
1899CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
1900 const ConstantVector &OptInstanceMethods,
Bob Wilsondc8dab62011-11-30 01:57:58 +00001901 const ConstantVector &OptClassMethods,
1902 const ConstantVector &MethodTypesExt) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001903 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00001904 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolExtensionTy);
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001905 llvm::Constant *Values[] = {
1906 llvm::ConstantInt::get(ObjCTypes.IntTy, Size),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001907 EmitMethodDescList("\01L_OBJC_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001908 + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001909 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001910 OptInstanceMethods),
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001911 EmitMethodDescList("\01L_OBJC_PROTOCOL_CLASS_METHODS_OPT_" + PD->getName(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00001912 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001913 OptClassMethods),
1914 EmitPropertyList("\01L_OBJC_$_PROP_PROTO_LIST_" + PD->getName(), 0, PD,
Bob Wilsondc8dab62011-11-30 01:57:58 +00001915 ObjCTypes),
1916 EmitProtocolMethodTypes("\01L_OBJC_PROTOCOL_METHOD_TYPES_" + PD->getName(),
1917 MethodTypesExt, ObjCTypes)
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001918 };
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001919
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001920 // Return null if no extension bits are used.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001921 if (Values[1]->isNullValue() && Values[2]->isNullValue() &&
Bob Wilsondc8dab62011-11-30 01:57:58 +00001922 Values[3]->isNullValue() && Values[4]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001923 return llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001924
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001925 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00001926 llvm::ConstantStruct::get(ObjCTypes.ProtocolExtensionTy, Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001927
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001928 // No special section, but goes in llvm.used
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00001929 return CreateMetadataVar("\01L_OBJC_PROTOCOLEXT_" + PD->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001930 Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001931 0, 0, true);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001932}
1933
1934/*
1935 struct objc_protocol_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001936 struct objc_protocol_list *next;
1937 long count;
1938 Protocol *list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001939 };
1940*/
Daniel Dunbardbc93372008-08-21 21:57:41 +00001941llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00001942CGObjCMac::EmitProtocolList(Twine Name,
Daniel Dunbardbc93372008-08-21 21:57:41 +00001943 ObjCProtocolDecl::protocol_iterator begin,
1944 ObjCProtocolDecl::protocol_iterator end) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001945 std::vector<llvm::Constant*> ProtocolRefs;
1946
Daniel Dunbardbc93372008-08-21 21:57:41 +00001947 for (; begin != end; ++begin)
1948 ProtocolRefs.push_back(GetProtocolRef(*begin));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001949
1950 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001951 if (ProtocolRefs.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00001952 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001953
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001954 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001955 ProtocolRefs.push_back(llvm::Constant::getNullValue(ObjCTypes.ProtocolPtrTy));
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001956
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001957 llvm::Constant *Values[3];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00001958 // This field is only used by the runtime.
Owen Andersonc9c88b42009-07-31 20:28:54 +00001959 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00001960 Values[1] = llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001961 ProtocolRefs.size() - 1);
1962 Values[2] =
1963 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.ProtocolPtrTy,
1964 ProtocolRefs.size()),
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001965 ProtocolRefs);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001966
Chris Lattnerc5cbb902011-06-20 04:01:35 +00001967 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001968 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00001969 CreateMetadataVar(Name, Init, "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00001970 4, false);
Owen Anderson3c4972d2009-07-29 18:54:39 +00001971 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001972}
1973
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001974void CGObjCCommonMac::PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*, 16> &PropertySet,
1975 std::vector<llvm::Constant*> &Properties,
1976 const Decl *Container,
1977 const ObjCProtocolDecl *PROTO,
1978 const ObjCCommonTypesHelper &ObjCTypes) {
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001979 for (ObjCProtocolDecl::protocol_iterator P = PROTO->protocol_begin(),
1980 E = PROTO->protocol_end(); P != E; ++P)
1981 PushProtocolProperties(PropertySet, Properties, Container, (*P), ObjCTypes);
1982 for (ObjCContainerDecl::prop_iterator I = PROTO->prop_begin(),
1983 E = PROTO->prop_end(); I != E; ++I) {
1984 const ObjCPropertyDecl *PD = *I;
1985 if (!PropertySet.insert(PD->getIdentifier()))
1986 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00001987 llvm::Constant *Prop[] = {
1988 GetPropertyName(PD->getIdentifier()),
1989 GetPropertyTypeString(PD, Container)
1990 };
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00001991 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy, Prop));
1992 }
1993}
1994
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00001995/*
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001996 struct _objc_property {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00001997 const char * const name;
1998 const char * const attributes;
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00001999 };
2000
2001 struct _objc_property_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002002 uint32_t entsize; // sizeof (struct _objc_property)
2003 uint32_t prop_count;
2004 struct _objc_property[prop_count];
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002005 };
2006*/
Chris Lattner5f9e2722011-07-23 10:55:15 +00002007llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002008 const Decl *Container,
2009 const ObjCContainerDecl *OCD,
2010 const ObjCCommonTypesHelper &ObjCTypes) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002011 std::vector<llvm::Constant*> Properties;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002012 llvm::SmallPtrSet<const IdentifierInfo*, 16> PropertySet;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002013 for (ObjCContainerDecl::prop_iterator I = OCD->prop_begin(),
2014 E = OCD->prop_end(); I != E; ++I) {
Steve Naroff93983f82009-01-11 12:47:58 +00002015 const ObjCPropertyDecl *PD = *I;
Fariborz Jahanian191dcd72009-12-12 21:26:21 +00002016 PropertySet.insert(PD->getIdentifier());
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002017 llvm::Constant *Prop[] = {
2018 GetPropertyName(PD->getIdentifier()),
2019 GetPropertyTypeString(PD, Container)
2020 };
Owen Anderson08e25242009-07-27 22:29:56 +00002021 Properties.push_back(llvm::ConstantStruct::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002022 Prop));
2023 }
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002024 if (const ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(OCD)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00002025 for (ObjCInterfaceDecl::all_protocol_iterator
2026 P = OID->all_referenced_protocol_begin(),
2027 E = OID->all_referenced_protocol_end(); P != E; ++P)
Fariborz Jahanian6afbdf52010-06-22 16:33:55 +00002028 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2029 ObjCTypes);
2030 }
2031 else if (const ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(OCD)) {
2032 for (ObjCCategoryDecl::protocol_iterator P = CD->protocol_begin(),
2033 E = CD->protocol_end(); P != E; ++P)
2034 PushProtocolProperties(PropertySet, Properties, Container, (*P),
2035 ObjCTypes);
2036 }
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002037
2038 // Return null for empty list.
2039 if (Properties.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002040 return llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002041
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002042 unsigned PropertySize =
Duncan Sands9408c452009-05-09 07:08:47 +00002043 CGM.getTargetData().getTypeAllocSize(ObjCTypes.PropertyTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002044 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002045 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, PropertySize);
2046 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Properties.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002047 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.PropertyTy,
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002048 Properties.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002049 Values[2] = llvm::ConstantArray::get(AT, Properties);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002050 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002051
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002052 llvm::GlobalVariable *GV =
2053 CreateMetadataVar(Name, Init,
2054 (ObjCABI == 2) ? "__DATA, __objc_const" :
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002055 "__OBJC,__property,regular,no_dead_strip",
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002056 (ObjCABI == 2) ? 8 : 4,
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002057 true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002058 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002059}
2060
Bob Wilsondc8dab62011-11-30 01:57:58 +00002061llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
2062 const ConstantVector &MethodTypes,
2063 const ObjCCommonTypesHelper &ObjCTypes) {
2064 // Return null for empty list.
2065 if (MethodTypes.empty())
2066 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
2067
2068 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
2069 MethodTypes.size());
2070 llvm::Constant *Init = llvm::ConstantArray::get(AT, MethodTypes);
2071
2072 llvm::GlobalVariable *GV =
2073 CreateMetadataVar(Name, Init,
2074 (ObjCABI == 2) ? "__DATA, __objc_const" : 0,
2075 (ObjCABI == 2) ? 8 : 4,
2076 true);
2077 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.Int8PtrPtrTy);
2078}
2079
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002080/*
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002081 struct objc_method_description_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002082 int count;
2083 struct objc_method_description list[];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002084 };
2085*/
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002086llvm::Constant *
2087CGObjCMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002088 llvm::Constant *Desc[] = {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002089 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002090 ObjCTypes.SelectorPtrTy),
2091 GetMethodVarType(MD)
2092 };
Douglas Gregorf968d832011-05-27 01:19:52 +00002093 if (!Desc[1])
2094 return 0;
2095
Owen Anderson08e25242009-07-27 22:29:56 +00002096 return llvm::ConstantStruct::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002097 Desc);
2098}
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002099
Chris Lattner5f9e2722011-07-23 10:55:15 +00002100llvm::Constant *CGObjCMac::EmitMethodDescList(Twine Name,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002101 const char *Section,
2102 const ConstantVector &Methods) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002103 // Return null for empty list.
2104 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002105 return llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002106
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002107 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002108 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002109 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodDescriptionTy,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002110 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002111 Values[1] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002112 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002113
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002114 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002115 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00002116 ObjCTypes.MethodDescriptionListPtrTy);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002117}
2118
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002119/*
2120 struct _objc_category {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002121 char *category_name;
2122 char *class_name;
2123 struct _objc_method_list *instance_methods;
2124 struct _objc_method_list *class_methods;
2125 struct _objc_protocol_list *protocols;
2126 uint32_t size; // <rdar://4585769>
2127 struct _objc_property_list *instance_properties;
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002128 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002129*/
Daniel Dunbar7ded7f42008-08-15 22:20:32 +00002130void CGObjCMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Duncan Sands9408c452009-05-09 07:08:47 +00002131 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.CategoryTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002132
Mike Stumpf5408fe2009-05-16 07:57:57 +00002133 // FIXME: This is poor design, the OCD should have a pointer to the category
2134 // decl. Additionally, note that Category can be null for the @implementation
2135 // w/o an @interface case. Sema should just create one for us as it does for
2136 // @implementation so everyone else can live life under a clear blue sky.
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002137 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002138 const ObjCCategoryDecl *Category =
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002139 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002140
2141 llvm::SmallString<256> ExtName;
2142 llvm::raw_svector_ostream(ExtName) << Interface->getName() << '_'
2143 << OCD->getName();
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002144
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002145 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002146 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002147 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002148 // Instance methods should always be defined.
2149 InstanceMethods.push_back(GetMethodConstant(*i));
2150 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002151 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002152 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002153 // Class methods should always be defined.
2154 ClassMethods.push_back(GetMethodConstant(*i));
2155 }
2156
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002157 llvm::Constant *Values[7];
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002158 Values[0] = GetClassName(OCD->getIdentifier());
2159 Values[1] = GetClassName(Interface->getIdentifier());
Fariborz Jahanian679cd7f2009-04-29 20:40:05 +00002160 LazySymbols.insert(Interface->getIdentifier());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002161 Values[2] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002162 EmitMethodList("\01L_OBJC_CATEGORY_INSTANCE_METHODS_" + ExtName.str(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002163 "__OBJC,__cat_inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002164 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002165 Values[3] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002166 EmitMethodList("\01L_OBJC_CATEGORY_CLASS_METHODS_" + ExtName.str(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002167 "__OBJC,__cat_cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002168 ClassMethods);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002169 if (Category) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002170 Values[4] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002171 EmitProtocolList("\01L_OBJC_CATEGORY_PROTOCOLS_" + ExtName.str(),
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002172 Category->protocol_begin(),
2173 Category->protocol_end());
2174 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002175 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002176 }
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002177 Values[5] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002178
2179 // If there is no category @interface then there can be no properties.
2180 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002181 Values[6] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002182 OCD, Category, ObjCTypes);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002183 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002184 Values[6] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Daniel Dunbar86e2f402008-08-26 23:03:11 +00002185 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002186
Owen Anderson08e25242009-07-27 22:29:56 +00002187 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.CategoryTy,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002188 Values);
2189
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002190 llvm::GlobalVariable *GV =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002191 CreateMetadataVar("\01L_OBJC_CATEGORY_" + ExtName.str(), Init,
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002192 "__OBJC,__category,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002193 4, true);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002194 DefinedCategories.push_back(GV);
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00002195 DefinedCategoryNames.insert(ExtName.str());
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002196 // method definition entries must be clear for next implementation.
2197 MethodDefinitions.clear();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002198}
2199
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002200// FIXME: Get from somewhere?
2201enum ClassFlags {
2202 eClassFlags_Factory = 0x00001,
2203 eClassFlags_Meta = 0x00002,
2204 // <rdr://5142207>
2205 eClassFlags_HasCXXStructors = 0x02000,
2206 eClassFlags_Hidden = 0x20000,
2207 eClassFlags_ABI2_Hidden = 0x00010,
2208 eClassFlags_ABI2_HasCXXStructors = 0x00004 // <rdr://4923634>
2209};
2210
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002211/*
2212 struct _objc_class {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002213 Class isa;
2214 Class super_class;
2215 const char *name;
2216 long version;
2217 long info;
2218 long instance_size;
2219 struct _objc_ivar_list *ivars;
2220 struct _objc_method_list *methods;
2221 struct _objc_cache *cache;
2222 struct _objc_protocol_list *protocols;
2223 // Objective-C 1.0 extensions (<rdr://4585769>)
2224 const char *ivar_layout;
2225 struct _objc_class_ext *ext;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002226 };
2227
2228 See EmitClassExtension();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002229*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002230void CGObjCMac::GenerateClass(const ObjCImplementationDecl *ID) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002231 DefinedSymbols.insert(ID->getIdentifier());
2232
Chris Lattner8ec03f52008-11-24 03:54:41 +00002233 std::string ClassName = ID->getNameAsString();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002234 // FIXME: Gross
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002235 ObjCInterfaceDecl *Interface =
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002236 const_cast<ObjCInterfaceDecl*>(ID->getClassInterface());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002237 llvm::Constant *Protocols =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002238 EmitProtocolList("\01L_OBJC_CLASS_PROTOCOLS_" + ID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00002239 Interface->all_referenced_protocol_begin(),
2240 Interface->all_referenced_protocol_end());
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002241 unsigned Flags = eClassFlags_Factory;
John McCallf85e1932011-06-15 23:02:42 +00002242 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00002243 Flags |= eClassFlags_HasCXXStructors;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002244 unsigned Size =
Ken Dyck5f022d82011-02-09 01:59:34 +00002245 CGM.getContext().getASTObjCImplementationLayout(ID).getSize().getQuantity();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002246
2247 // FIXME: Set CXX-structors flag.
John McCall1fb0caa2010-10-22 21:05:15 +00002248 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002249 Flags |= eClassFlags_Hidden;
2250
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002251 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002252 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002253 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002254 // Instance methods should always be defined.
2255 InstanceMethods.push_back(GetMethodConstant(*i));
2256 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002257 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002258 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002259 // Class methods should always be defined.
2260 ClassMethods.push_back(GetMethodConstant(*i));
2261 }
2262
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002263 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002264 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002265 ObjCPropertyImplDecl *PID = *i;
2266
2267 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
2268 ObjCPropertyDecl *PD = PID->getPropertyDecl();
2269
2270 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
2271 if (llvm::Constant *C = GetMethodConstant(MD))
2272 InstanceMethods.push_back(C);
2273 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
2274 if (llvm::Constant *C = GetMethodConstant(MD))
2275 InstanceMethods.push_back(C);
2276 }
2277 }
2278
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002279 llvm::Constant *Values[12];
Daniel Dunbar5384b092009-05-03 08:56:52 +00002280 Values[ 0] = EmitMetaClass(ID, Protocols, ClassMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002281 if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) {
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00002282 // Record a reference to the super class.
2283 LazySymbols.insert(Super->getIdentifier());
2284
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002285 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002286 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002287 ObjCTypes.ClassPtrTy);
2288 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002289 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002290 }
2291 Values[ 2] = GetClassName(ID->getIdentifier());
2292 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002293 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2294 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2295 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002296 Values[ 6] = EmitIvarList(ID, false);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002297 Values[ 7] =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002298 EmitMethodList("\01L_OBJC_INSTANCE_METHODS_" + ID->getName(),
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00002299 "__OBJC,__inst_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002300 InstanceMethods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002301 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002302 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002303 Values[ 9] = Protocols;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002304 Values[10] = BuildIvarLayout(ID, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002305 Values[11] = EmitClassExtension(ID);
Owen Anderson08e25242009-07-27 22:29:56 +00002306 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002307 Values);
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002308 std::string Name("\01L_OBJC_CLASS_");
2309 Name += ClassName;
2310 const char *Section = "__OBJC,__class,regular,no_dead_strip";
2311 // Check for a forward reference.
2312 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2313 if (GV) {
2314 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2315 "Forward metaclass reference has incorrect type.");
2316 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2317 GV->setInitializer(Init);
2318 GV->setSection(Section);
2319 GV->setAlignment(4);
2320 CGM.AddUsedGlobal(GV);
2321 }
2322 else
2323 GV = CreateMetadataVar(Name, Init, Section, 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002324 DefinedClasses.push_back(GV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002325 // method definition entries must be clear for next implementation.
2326 MethodDefinitions.clear();
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002327}
2328
2329llvm::Constant *CGObjCMac::EmitMetaClass(const ObjCImplementationDecl *ID,
2330 llvm::Constant *Protocols,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002331 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002332 unsigned Flags = eClassFlags_Meta;
Duncan Sands9408c452009-05-09 07:08:47 +00002333 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002334
John McCall1fb0caa2010-10-22 21:05:15 +00002335 if (ID->getClassInterface()->getVisibility() == HiddenVisibility)
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002336 Flags |= eClassFlags_Hidden;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002337
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002338 llvm::Constant *Values[12];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002339 // The isa for the metaclass is the root of the hierarchy.
2340 const ObjCInterfaceDecl *Root = ID->getClassInterface();
2341 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
2342 Root = Super;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002343 Values[ 0] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002344 llvm::ConstantExpr::getBitCast(GetClassName(Root->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002345 ObjCTypes.ClassPtrTy);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002346 // The super class for the metaclass is emitted as the name of the
2347 // super class. The runtime fixes this up to point to the
2348 // *metaclass* for the super class.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002349 if (ObjCInterfaceDecl *Super = ID->getClassInterface()->getSuperClass()) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002350 Values[ 1] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00002351 llvm::ConstantExpr::getBitCast(GetClassName(Super->getIdentifier()),
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002352 ObjCTypes.ClassPtrTy);
2353 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00002354 Values[ 1] = llvm::Constant::getNullValue(ObjCTypes.ClassPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002355 }
2356 Values[ 2] = GetClassName(ID->getIdentifier());
2357 // Version is always 0.
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002358 Values[ 3] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
2359 Values[ 4] = llvm::ConstantInt::get(ObjCTypes.LongTy, Flags);
2360 Values[ 5] = llvm::ConstantInt::get(ObjCTypes.LongTy, Size);
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002361 Values[ 6] = EmitIvarList(ID, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002362 Values[ 7] =
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002363 EmitMethodList("\01L_OBJC_CLASS_METHODS_" + ID->getNameAsString(),
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002364 "__OBJC,__cls_meth,regular,no_dead_strip",
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002365 Methods);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002366 // cache is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002367 Values[ 8] = llvm::Constant::getNullValue(ObjCTypes.CachePtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002368 Values[ 9] = Protocols;
2369 // ivar_layout for metaclass is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002370 Values[10] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002371 // The class extension is always unused for metaclasses.
Owen Andersonc9c88b42009-07-31 20:28:54 +00002372 Values[11] = llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00002373 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002374 Values);
2375
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002376 std::string Name("\01L_OBJC_METACLASS_");
Chris Lattner8ec03f52008-11-24 03:54:41 +00002377 Name += ID->getNameAsCString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002378
2379 // Check for a forward reference.
2380 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
2381 if (GV) {
2382 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2383 "Forward metaclass reference has incorrect type.");
2384 GV->setLinkage(llvm::GlobalValue::InternalLinkage);
2385 GV->setInitializer(Init);
2386 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00002387 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002388 llvm::GlobalValue::InternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00002389 Init, Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002390 }
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002391 GV->setSection("__OBJC,__meta_class,regular,no_dead_strip");
Daniel Dunbar58a29122009-03-09 22:18:41 +00002392 GV->setAlignment(4);
Chris Lattnerad64e022009-07-17 23:57:13 +00002393 CGM.AddUsedGlobal(GV);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002394
2395 return GV;
2396}
2397
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002398llvm::Constant *CGObjCMac::EmitMetaClassRef(const ObjCInterfaceDecl *ID) {
Chris Lattnerd9d22dd2008-11-24 05:29:24 +00002399 std::string Name = "\01L_OBJC_METACLASS_" + ID->getNameAsString();
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002400
Mike Stumpf5408fe2009-05-16 07:57:57 +00002401 // FIXME: Should we look these up somewhere other than the module. Its a bit
2402 // silly since we only generate these while processing an implementation, so
2403 // exactly one pointer would work if know when we entered/exitted an
2404 // implementation block.
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002405
2406 // Check for an existing forward reference.
Fariborz Jahanianb0d27942009-01-07 20:11:22 +00002407 // Previously, metaclass with internal linkage may have been defined.
2408 // pass 'true' as 2nd argument so it is returned.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002409 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2410 true)) {
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002411 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2412 "Forward metaclass reference has incorrect type.");
2413 return GV;
2414 } else {
2415 // Generate as an external reference to keep a consistent
2416 // module. This will be patched up when we emit the metaclass.
Owen Anderson1c431b32009-07-08 19:05:04 +00002417 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002418 llvm::GlobalValue::ExternalLinkage,
2419 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00002420 Name);
Daniel Dunbarf56f1912008-08-25 08:19:24 +00002421 }
2422}
2423
Fariborz Jahanianb0069ee2009-11-12 20:14:24 +00002424llvm::Value *CGObjCMac::EmitSuperClassRef(const ObjCInterfaceDecl *ID) {
2425 std::string Name = "\01L_OBJC_CLASS_" + ID->getNameAsString();
2426
2427 if (llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name,
2428 true)) {
2429 assert(GV->getType()->getElementType() == ObjCTypes.ClassTy &&
2430 "Forward class metadata reference has incorrect type.");
2431 return GV;
2432 } else {
2433 return new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassTy, false,
2434 llvm::GlobalValue::ExternalLinkage,
2435 0,
2436 Name);
2437 }
2438}
2439
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002440/*
2441 struct objc_class_ext {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002442 uint32_t size;
2443 const char *weak_ivar_layout;
2444 struct _objc_property_list *properties;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002445 };
2446*/
2447llvm::Constant *
2448CGObjCMac::EmitClassExtension(const ObjCImplementationDecl *ID) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002449 uint64_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00002450 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002451
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002452 llvm::Constant *Values[3];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002453 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanianc71303d2009-04-22 23:00:43 +00002454 Values[1] = BuildIvarLayout(ID, false);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002455 Values[2] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00002456 ID, ID->getClassInterface(), ObjCTypes);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002457
2458 // Return null if no extension bits are used.
2459 if (Values[1]->isNullValue() && Values[2]->isNullValue())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002460 return llvm::Constant::getNullValue(ObjCTypes.ClassExtensionPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002461
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002462 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00002463 llvm::ConstantStruct::get(ObjCTypes.ClassExtensionTy, Values);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002464 return CreateMetadataVar("\01L_OBJC_CLASSEXT_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002465 Init, "__OBJC,__class_ext,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002466 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002467}
2468
2469/*
2470 struct objc_ivar {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002471 char *ivar_name;
2472 char *ivar_type;
2473 int ivar_offset;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002474 };
2475
2476 struct objc_ivar_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002477 int ivar_count;
2478 struct objc_ivar list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002479 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002480*/
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002481llvm::Constant *CGObjCMac::EmitIvarList(const ObjCImplementationDecl *ID,
Fariborz Jahanian46b86c62009-01-28 19:12:34 +00002482 bool ForClass) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002483 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002484
2485 // When emitting the root class GCC emits ivar entries for the
2486 // actual class structure. It is not clear if we need to follow this
2487 // behavior; for now lets try and get away with not doing it. If so,
2488 // the cleanest solution would be to make up an ObjCInterfaceDecl
2489 // for the class.
2490 if (ForClass)
Owen Andersonc9c88b42009-07-31 20:28:54 +00002491 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002492
Jordy Rosedb8264e2011-07-22 02:08:32 +00002493 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002494
Jordy Rosedb8264e2011-07-22 02:08:32 +00002495 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00002496 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00002497 // Ignore unnamed bit-fields.
2498 if (!IVD->getDeclName())
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002499 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002500 llvm::Constant *Ivar[] = {
2501 GetMethodVarName(IVD->getIdentifier()),
2502 GetMethodVarType(IVD),
2503 llvm::ConstantInt::get(ObjCTypes.IntTy,
2504 ComputeIvarBaseOffset(CGM, OID, IVD))
2505 };
Owen Anderson08e25242009-07-27 22:29:56 +00002506 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarTy, Ivar));
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002507 }
2508
2509 // Return null for empty list.
2510 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002511 return llvm::Constant::getNullValue(ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002512
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002513 llvm::Constant *Values[2];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002514 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002515 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002516 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002517 Values[1] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002518 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002519
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002520 llvm::GlobalVariable *GV;
2521 if (ForClass)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002522 GV = CreateMetadataVar("\01L_OBJC_CLASS_VARIABLES_" + ID->getName(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002523 Init, "__OBJC,__class_vars,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00002524 4, true);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002525 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00002526 GV = CreateMetadataVar("\01L_OBJC_INSTANCE_VARIABLES_" + ID->getName(),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00002527 Init, "__OBJC,__instance_vars,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002528 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00002529 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002530}
2531
2532/*
2533 struct objc_method {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002534 SEL method_name;
2535 char *method_types;
2536 void *method;
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002537 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002538
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002539 struct objc_method_list {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002540 struct objc_method_list *obsolete;
2541 int count;
2542 struct objc_method methods_list[count];
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002543 };
2544*/
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002545
2546/// GetMethodConstant - Return a struct objc_method constant for the
2547/// given method if it has been defined. The result is null if the
2548/// method has not been defined. The return value has type MethodPtrTy.
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002549llvm::Constant *CGObjCMac::GetMethodConstant(const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00002550 llvm::Function *Fn = GetMethodDefinition(MD);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002551 if (!Fn)
2552 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002553
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002554 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00002555 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00002556 ObjCTypes.SelectorPtrTy),
2557 GetMethodVarType(MD),
2558 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
2559 };
Owen Anderson08e25242009-07-27 22:29:56 +00002560 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002561}
2562
Chris Lattner5f9e2722011-07-23 10:55:15 +00002563llvm::Constant *CGObjCMac::EmitMethodList(Twine Name,
Daniel Dunbar86e253a2008-08-22 20:34:54 +00002564 const char *Section,
Daniel Dunbarae226fa2008-08-27 02:31:56 +00002565 const ConstantVector &Methods) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002566 // Return null for empty list.
2567 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00002568 return llvm::Constant::getNullValue(ObjCTypes.MethodListPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002569
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002570 llvm::Constant *Values[3];
Owen Andersonc9c88b42009-07-31 20:28:54 +00002571 Values[0] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00002572 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00002573 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002574 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00002575 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002576 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00002577
Daniel Dunbar0bf21992009-04-15 02:56:18 +00002578 llvm::GlobalVariable *GV = CreateMetadataVar(Name, Init, Section, 4, true);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00002579 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002580}
2581
Fariborz Jahanian493dab72009-01-26 21:38:32 +00002582llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002583 const ObjCContainerDecl *CD) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002584 llvm::SmallString<256> Name;
Fariborz Jahanian679a5022009-01-10 21:06:09 +00002585 GetNameForMethod(OMD, CD, Name);
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002586
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002587 CodeGenTypes &Types = CGM.getTypes();
Chris Lattner2acc6e32011-07-18 04:24:23 +00002588 llvm::FunctionType *MethodTy =
Daniel Dunbar541b63b2009-02-02 23:23:47 +00002589 Types.GetFunctionType(Types.getFunctionInfo(OMD), OMD->isVariadic());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002590 llvm::Function *Method =
Daniel Dunbar45c25ba2008-09-10 04:01:49 +00002591 llvm::Function::Create(MethodTy,
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002592 llvm::GlobalValue::InternalLinkage,
Daniel Dunbarc575ce72009-10-19 01:21:19 +00002593 Name.str(),
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002594 &CGM.getModule());
Daniel Dunbarc45ef602008-08-26 21:51:14 +00002595 MethodDefinitions.insert(std::make_pair(OMD, Method));
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002596
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00002597 return Method;
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002598}
2599
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002600llvm::GlobalVariable *
Chris Lattner5f9e2722011-07-23 10:55:15 +00002601CGObjCCommonMac::CreateMetadataVar(Twine Name,
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002602 llvm::Constant *Init,
2603 const char *Section,
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002604 unsigned Align,
2605 bool AddToUsed) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00002606 llvm::Type *Ty = Init->getType();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002607 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00002608 new llvm::GlobalVariable(CGM.getModule(), Ty, false,
Chris Lattnerad64e022009-07-17 23:57:13 +00002609 llvm::GlobalValue::InternalLinkage, Init, Name);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002610 if (Section)
2611 GV->setSection(Section);
Daniel Dunbar35bd7632009-03-09 20:50:13 +00002612 if (Align)
2613 GV->setAlignment(Align);
2614 if (AddToUsed)
Chris Lattnerad64e022009-07-17 23:57:13 +00002615 CGM.AddUsedGlobal(GV);
Daniel Dunbarfd65d372009-03-09 20:09:19 +00002616 return GV;
2617}
2618
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002619llvm::Function *CGObjCMac::ModuleInitFunction() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00002620 // Abuse this interface function as a place to finalize.
2621 FinishModule();
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00002622 return NULL;
2623}
2624
Chris Lattner74391b42009-03-22 21:03:39 +00002625llvm::Constant *CGObjCMac::GetPropertyGetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002626 return ObjCTypes.getGetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002627}
2628
Chris Lattner74391b42009-03-22 21:03:39 +00002629llvm::Constant *CGObjCMac::GetPropertySetFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002630 return ObjCTypes.getSetPropertyFn();
Daniel Dunbar49f66022008-09-24 03:38:44 +00002631}
2632
David Chisnall8fac25d2010-12-26 22:13:16 +00002633llvm::Constant *CGObjCMac::GetGetStructFunction() {
2634 return ObjCTypes.getCopyStructFn();
2635}
2636llvm::Constant *CGObjCMac::GetSetStructFunction() {
Fariborz Jahanian6cc59062010-04-12 18:18:10 +00002637 return ObjCTypes.getCopyStructFn();
2638}
2639
Chris Lattner74391b42009-03-22 21:03:39 +00002640llvm::Constant *CGObjCMac::EnumerationMutationFunction() {
Chris Lattner72db6c32009-04-22 02:44:54 +00002641 return ObjCTypes.getEnumerationMutationFn();
Anders Carlsson2abd89c2008-08-31 04:05:03 +00002642}
2643
John McCallf1549f62010-07-06 01:34:17 +00002644void CGObjCMac::EmitTryStmt(CodeGenFunction &CGF, const ObjCAtTryStmt &S) {
2645 return EmitTryOrSynchronizedStmt(CGF, S);
2646}
2647
2648void CGObjCMac::EmitSynchronizedStmt(CodeGenFunction &CGF,
2649 const ObjCAtSynchronizedStmt &S) {
2650 return EmitTryOrSynchronizedStmt(CGF, S);
2651}
2652
John McCallcc505292010-07-21 06:59:36 +00002653namespace {
John McCall1f0fca52010-07-21 07:22:38 +00002654 struct PerformFragileFinally : EHScopeStack::Cleanup {
John McCallcc505292010-07-21 06:59:36 +00002655 const Stmt &S;
John McCall0b251722010-08-04 05:59:32 +00002656 llvm::Value *SyncArgSlot;
John McCallcc505292010-07-21 06:59:36 +00002657 llvm::Value *CallTryExitVar;
2658 llvm::Value *ExceptionData;
2659 ObjCTypesHelper &ObjCTypes;
2660 PerformFragileFinally(const Stmt *S,
John McCall0b251722010-08-04 05:59:32 +00002661 llvm::Value *SyncArgSlot,
John McCallcc505292010-07-21 06:59:36 +00002662 llvm::Value *CallTryExitVar,
2663 llvm::Value *ExceptionData,
2664 ObjCTypesHelper *ObjCTypes)
John McCall0b251722010-08-04 05:59:32 +00002665 : S(*S), SyncArgSlot(SyncArgSlot), CallTryExitVar(CallTryExitVar),
John McCallcc505292010-07-21 06:59:36 +00002666 ExceptionData(ExceptionData), ObjCTypes(*ObjCTypes) {}
2667
John McCallad346f42011-07-12 20:27:29 +00002668 void Emit(CodeGenFunction &CGF, Flags flags) {
John McCallcc505292010-07-21 06:59:36 +00002669 // Check whether we need to call objc_exception_try_exit.
2670 // In optimized code, this branch will always be folded.
2671 llvm::BasicBlock *FinallyCallExit =
2672 CGF.createBasicBlock("finally.call_exit");
2673 llvm::BasicBlock *FinallyNoCallExit =
2674 CGF.createBasicBlock("finally.no_call_exit");
2675 CGF.Builder.CreateCondBr(CGF.Builder.CreateLoad(CallTryExitVar),
2676 FinallyCallExit, FinallyNoCallExit);
2677
2678 CGF.EmitBlock(FinallyCallExit);
2679 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryExitFn(), ExceptionData)
2680 ->setDoesNotThrow();
2681
2682 CGF.EmitBlock(FinallyNoCallExit);
2683
2684 if (isa<ObjCAtTryStmt>(S)) {
2685 if (const ObjCAtFinallyStmt* FinallyStmt =
John McCalld96a8e72010-08-11 00:16:14 +00002686 cast<ObjCAtTryStmt>(S).getFinallyStmt()) {
2687 // Save the current cleanup destination in case there's
2688 // control flow inside the finally statement.
2689 llvm::Value *CurCleanupDest =
2690 CGF.Builder.CreateLoad(CGF.getNormalCleanupDestSlot());
2691
John McCallcc505292010-07-21 06:59:36 +00002692 CGF.EmitStmt(FinallyStmt->getFinallyBody());
2693
John McCalld96a8e72010-08-11 00:16:14 +00002694 if (CGF.HaveInsertPoint()) {
2695 CGF.Builder.CreateStore(CurCleanupDest,
2696 CGF.getNormalCleanupDestSlot());
2697 } else {
2698 // Currently, the end of the cleanup must always exist.
2699 CGF.EnsureInsertPoint();
2700 }
2701 }
John McCallcc505292010-07-21 06:59:36 +00002702 } else {
2703 // Emit objc_sync_exit(expr); as finally's sole statement for
2704 // @synchronized.
John McCall0b251722010-08-04 05:59:32 +00002705 llvm::Value *SyncArg = CGF.Builder.CreateLoad(SyncArgSlot);
John McCallcc505292010-07-21 06:59:36 +00002706 CGF.Builder.CreateCall(ObjCTypes.getSyncExitFn(), SyncArg)
2707 ->setDoesNotThrow();
2708 }
2709 }
2710 };
John McCall87bb5822010-07-31 23:20:56 +00002711
2712 class FragileHazards {
2713 CodeGenFunction &CGF;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002714 SmallVector<llvm::Value*, 20> Locals;
John McCall87bb5822010-07-31 23:20:56 +00002715 llvm::DenseSet<llvm::BasicBlock*> BlocksBeforeTry;
2716
2717 llvm::InlineAsm *ReadHazard;
2718 llvm::InlineAsm *WriteHazard;
2719
2720 llvm::FunctionType *GetAsmFnType();
2721
2722 void collectLocals();
2723 void emitReadHazard(CGBuilderTy &Builder);
2724
2725 public:
2726 FragileHazards(CodeGenFunction &CGF);
John McCall0b251722010-08-04 05:59:32 +00002727
John McCall87bb5822010-07-31 23:20:56 +00002728 void emitWriteHazard();
John McCall0b251722010-08-04 05:59:32 +00002729 void emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00002730 };
2731}
2732
2733/// Create the fragile-ABI read and write hazards based on the current
2734/// state of the function, which is presumed to be immediately prior
2735/// to a @try block. These hazards are used to maintain correct
2736/// semantics in the face of optimization and the fragile ABI's
2737/// cavalier use of setjmp/longjmp.
2738FragileHazards::FragileHazards(CodeGenFunction &CGF) : CGF(CGF) {
2739 collectLocals();
2740
2741 if (Locals.empty()) return;
2742
2743 // Collect all the blocks in the function.
2744 for (llvm::Function::iterator
2745 I = CGF.CurFn->begin(), E = CGF.CurFn->end(); I != E; ++I)
2746 BlocksBeforeTry.insert(&*I);
2747
2748 llvm::FunctionType *AsmFnTy = GetAsmFnType();
2749
2750 // Create a read hazard for the allocas. This inhibits dead-store
2751 // optimizations and forces the values to memory. This hazard is
2752 // inserted before any 'throwing' calls in the protected scope to
2753 // reflect the possibility that the variables might be read from the
2754 // catch block if the call throws.
2755 {
2756 std::string Constraint;
2757 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2758 if (I) Constraint += ',';
2759 Constraint += "*m";
2760 }
2761
2762 ReadHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2763 }
2764
2765 // Create a write hazard for the allocas. This inhibits folding
2766 // loads across the hazard. This hazard is inserted at the
2767 // beginning of the catch path to reflect the possibility that the
2768 // variables might have been written within the protected scope.
2769 {
2770 std::string Constraint;
2771 for (unsigned I = 0, E = Locals.size(); I != E; ++I) {
2772 if (I) Constraint += ',';
2773 Constraint += "=*m";
2774 }
2775
2776 WriteHazard = llvm::InlineAsm::get(AsmFnTy, "", Constraint, true, false);
2777 }
2778}
2779
2780/// Emit a write hazard at the current location.
2781void FragileHazards::emitWriteHazard() {
2782 if (Locals.empty()) return;
2783
Jay Foad4c7d9f12011-07-15 08:37:34 +00002784 CGF.Builder.CreateCall(WriteHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002785}
2786
John McCall87bb5822010-07-31 23:20:56 +00002787void FragileHazards::emitReadHazard(CGBuilderTy &Builder) {
2788 assert(!Locals.empty());
Jay Foad4c7d9f12011-07-15 08:37:34 +00002789 Builder.CreateCall(ReadHazard, Locals)->setDoesNotThrow();
John McCall87bb5822010-07-31 23:20:56 +00002790}
2791
2792/// Emit read hazards in all the protected blocks, i.e. all the blocks
2793/// which have been inserted since the beginning of the try.
John McCall0b251722010-08-04 05:59:32 +00002794void FragileHazards::emitHazardsInNewBlocks() {
John McCall87bb5822010-07-31 23:20:56 +00002795 if (Locals.empty()) return;
2796
2797 CGBuilderTy Builder(CGF.getLLVMContext());
2798
2799 // Iterate through all blocks, skipping those prior to the try.
2800 for (llvm::Function::iterator
2801 FI = CGF.CurFn->begin(), FE = CGF.CurFn->end(); FI != FE; ++FI) {
2802 llvm::BasicBlock &BB = *FI;
2803 if (BlocksBeforeTry.count(&BB)) continue;
2804
2805 // Walk through all the calls in the block.
2806 for (llvm::BasicBlock::iterator
2807 BI = BB.begin(), BE = BB.end(); BI != BE; ++BI) {
2808 llvm::Instruction &I = *BI;
2809
2810 // Ignore instructions that aren't non-intrinsic calls.
2811 // These are the only calls that can possibly call longjmp.
2812 if (!isa<llvm::CallInst>(I) && !isa<llvm::InvokeInst>(I)) continue;
2813 if (isa<llvm::IntrinsicInst>(I))
2814 continue;
2815
2816 // Ignore call sites marked nounwind. This may be questionable,
2817 // since 'nounwind' doesn't necessarily mean 'does not call longjmp'.
2818 llvm::CallSite CS(&I);
2819 if (CS.doesNotThrow()) continue;
2820
John McCall0b251722010-08-04 05:59:32 +00002821 // Insert a read hazard before the call. This will ensure that
2822 // any writes to the locals are performed before making the
2823 // call. If the call throws, then this is sufficient to
2824 // guarantee correctness as long as it doesn't also write to any
2825 // locals.
John McCall87bb5822010-07-31 23:20:56 +00002826 Builder.SetInsertPoint(&BB, BI);
2827 emitReadHazard(Builder);
2828 }
2829 }
2830}
2831
2832static void addIfPresent(llvm::DenseSet<llvm::Value*> &S, llvm::Value *V) {
2833 if (V) S.insert(V);
2834}
2835
2836void FragileHazards::collectLocals() {
2837 // Compute a set of allocas to ignore.
2838 llvm::DenseSet<llvm::Value*> AllocasToIgnore;
2839 addIfPresent(AllocasToIgnore, CGF.ReturnValue);
2840 addIfPresent(AllocasToIgnore, CGF.NormalCleanupDest);
John McCall87bb5822010-07-31 23:20:56 +00002841
2842 // Collect all the allocas currently in the function. This is
2843 // probably way too aggressive.
2844 llvm::BasicBlock &Entry = CGF.CurFn->getEntryBlock();
2845 for (llvm::BasicBlock::iterator
2846 I = Entry.begin(), E = Entry.end(); I != E; ++I)
2847 if (isa<llvm::AllocaInst>(*I) && !AllocasToIgnore.count(&*I))
2848 Locals.push_back(&*I);
2849}
2850
2851llvm::FunctionType *FragileHazards::GetAsmFnType() {
Chris Lattner5f9e2722011-07-23 10:55:15 +00002852 SmallVector<llvm::Type *, 16> tys(Locals.size());
John McCall0774cb82011-05-15 01:53:33 +00002853 for (unsigned i = 0, e = Locals.size(); i != e; ++i)
2854 tys[i] = Locals[i]->getType();
2855 return llvm::FunctionType::get(CGF.VoidTy, tys, false);
John McCallcc505292010-07-21 06:59:36 +00002856}
2857
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002858/*
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002859
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002860 Objective-C setjmp-longjmp (sjlj) Exception Handling
2861 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002862
John McCallf1549f62010-07-06 01:34:17 +00002863 A catch buffer is a setjmp buffer plus:
2864 - a pointer to the exception that was caught
2865 - a pointer to the previous exception data buffer
2866 - two pointers of reserved storage
2867 Therefore catch buffers form a stack, with a pointer to the top
2868 of the stack kept in thread-local storage.
2869
2870 objc_exception_try_enter pushes a catch buffer onto the EH stack.
2871 objc_exception_try_exit pops the given catch buffer, which is
2872 required to be the top of the EH stack.
2873 objc_exception_throw pops the top of the EH stack, writes the
2874 thrown exception into the appropriate field, and longjmps
2875 to the setjmp buffer. It crashes the process (with a printf
2876 and an abort()) if there are no catch buffers on the stack.
2877 objc_exception_extract just reads the exception pointer out of the
2878 catch buffer.
2879
2880 There's no reason an implementation couldn't use a light-weight
2881 setjmp here --- something like __builtin_setjmp, but API-compatible
2882 with the heavyweight setjmp. This will be more important if we ever
2883 want to implement correct ObjC/C++ exception interactions for the
2884 fragile ABI.
2885
2886 Note that for this use of setjmp/longjmp to be correct, we may need
2887 to mark some local variables volatile: if a non-volatile local
2888 variable is modified between the setjmp and the longjmp, it has
2889 indeterminate value. For the purposes of LLVM IR, it may be
2890 sufficient to make loads and stores within the @try (to variables
2891 declared outside the @try) volatile. This is necessary for
2892 optimized correctness, but is not currently being done; this is
2893 being tracked as rdar://problem/8160285
2894
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002895 The basic framework for a @try-catch-finally is as follows:
2896 {
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002897 objc_exception_data d;
2898 id _rethrow = null;
Anders Carlsson190d00e2009-02-07 21:26:04 +00002899 bool _call_try_exit = true;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002900
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002901 objc_exception_try_enter(&d);
2902 if (!setjmp(d.jmp_buf)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002903 ... try body ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002904 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002905 // exception path
2906 id _caught = objc_exception_extract(&d);
2907
2908 // enter new try scope for handlers
2909 if (!setjmp(d.jmp_buf)) {
2910 ... match exception and execute catch blocks ...
2911
2912 // fell off end, rethrow.
2913 _rethrow = _caught;
2914 ... jump-through-finally to finally_rethrow ...
2915 } else {
2916 // exception in catch block
2917 _rethrow = objc_exception_extract(&d);
2918 _call_try_exit = false;
2919 ... jump-through-finally to finally_rethrow ...
2920 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002921 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00002922 ... jump-through-finally to finally_end ...
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002923
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002924 finally:
Anders Carlsson190d00e2009-02-07 21:26:04 +00002925 if (_call_try_exit)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002926 objc_exception_try_exit(&d);
Anders Carlsson190d00e2009-02-07 21:26:04 +00002927
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002928 ... finally block ....
Daniel Dunbar898d5082008-09-30 01:06:03 +00002929 ... dispatch to finally destination ...
2930
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002931 finally_rethrow:
Daniel Dunbar898d5082008-09-30 01:06:03 +00002932 objc_exception_throw(_rethrow);
2933
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002934 finally_end:
2935 }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002936
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002937 This framework differs slightly from the one gcc uses, in that gcc
2938 uses _rethrow to determine if objc_exception_try_exit should be called
2939 and if the object should be rethrown. This breaks in the face of
2940 throwing nil and introduces unnecessary branches.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002941
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002942 We specialize this framework for a few particular circumstances:
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002943
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002944 - If there are no catch blocks, then we avoid emitting the second
2945 exception handling context.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002946
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002947 - If there is a catch-all catch block (i.e. @catch(...) or @catch(id
2948 e)) we avoid emitting the code to rethrow an uncaught exception.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002949
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002950 - FIXME: If there is no @finally block we can do a few more
2951 simplifications.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002952
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002953 Rethrows and Jumps-Through-Finally
2954 --
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002955
John McCallf1549f62010-07-06 01:34:17 +00002956 '@throw;' is supported by pushing the currently-caught exception
2957 onto ObjCEHStack while the @catch blocks are emitted.
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002958
John McCallf1549f62010-07-06 01:34:17 +00002959 Branches through the @finally block are handled with an ordinary
2960 normal cleanup. We do not register an EH cleanup; fragile-ABI ObjC
2961 exceptions are not compatible with C++ exceptions, and this is
2962 hardly the only place where this will go wrong.
Daniel Dunbar898d5082008-09-30 01:06:03 +00002963
John McCallf1549f62010-07-06 01:34:17 +00002964 @synchronized(expr) { stmt; } is emitted as if it were:
2965 id synch_value = expr;
2966 objc_sync_enter(synch_value);
2967 @try { stmt; } @finally { objc_sync_exit(synch_value); }
Daniel Dunbar18ccc772008-09-28 01:03:14 +00002968*/
2969
Fariborz Jahanianbd71be42008-11-21 00:49:24 +00002970void CGObjCMac::EmitTryOrSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
2971 const Stmt &S) {
2972 bool isTry = isa<ObjCAtTryStmt>(S);
John McCallf1549f62010-07-06 01:34:17 +00002973
2974 // A destination for the fall-through edges of the catch handlers to
2975 // jump to.
2976 CodeGenFunction::JumpDest FinallyEnd =
2977 CGF.getJumpDestInCurrentScope("finally.end");
2978
2979 // A destination for the rethrow edge of the catch handlers to jump
2980 // to.
2981 CodeGenFunction::JumpDest FinallyRethrow =
2982 CGF.getJumpDestInCurrentScope("finally.rethrow");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00002983
Daniel Dunbar1c566672009-02-24 01:43:46 +00002984 // For @synchronized, call objc_sync_enter(sync.expr). The
2985 // evaluation of the expression must occur before we enter the
John McCall0b251722010-08-04 05:59:32 +00002986 // @synchronized. We can't avoid a temp here because we need the
2987 // value to be preserved. If the backend ever does liveness
2988 // correctly after setjmp, this will be unnecessary.
2989 llvm::Value *SyncArgSlot = 0;
Daniel Dunbar1c566672009-02-24 01:43:46 +00002990 if (!isTry) {
John McCall0b251722010-08-04 05:59:32 +00002991 llvm::Value *SyncArg =
Daniel Dunbar1c566672009-02-24 01:43:46 +00002992 CGF.EmitScalarExpr(cast<ObjCAtSynchronizedStmt>(S).getSynchExpr());
2993 SyncArg = CGF.Builder.CreateBitCast(SyncArg, ObjCTypes.ObjectPtrTy);
John McCallf1549f62010-07-06 01:34:17 +00002994 CGF.Builder.CreateCall(ObjCTypes.getSyncEnterFn(), SyncArg)
2995 ->setDoesNotThrow();
John McCall0b251722010-08-04 05:59:32 +00002996
2997 SyncArgSlot = CGF.CreateTempAlloca(SyncArg->getType(), "sync.arg");
2998 CGF.Builder.CreateStore(SyncArg, SyncArgSlot);
Daniel Dunbar1c566672009-02-24 01:43:46 +00002999 }
Daniel Dunbar898d5082008-09-30 01:06:03 +00003000
John McCall0b251722010-08-04 05:59:32 +00003001 // Allocate memory for the setjmp buffer. This needs to be kept
3002 // live throughout the try and catch blocks.
3003 llvm::Value *ExceptionData = CGF.CreateTempAlloca(ObjCTypes.ExceptionDataTy,
3004 "exceptiondata.ptr");
3005
John McCall87bb5822010-07-31 23:20:56 +00003006 // Create the fragile hazards. Note that this will not capture any
3007 // of the allocas required for exception processing, but will
3008 // capture the current basic block (which extends all the way to the
3009 // setjmp call) as "before the @try".
3010 FragileHazards Hazards(CGF);
3011
John McCallf1549f62010-07-06 01:34:17 +00003012 // Create a flag indicating whether the cleanup needs to call
3013 // objc_exception_try_exit. This is true except when
3014 // - no catches match and we're branching through the cleanup
3015 // just to rethrow the exception, or
3016 // - a catch matched and we're falling out of the catch handler.
John McCall0b251722010-08-04 05:59:32 +00003017 // The setjmp-safety rule here is that we should always store to this
3018 // variable in a place that dominates the branch through the cleanup
3019 // without passing through any setjmps.
John McCallf1549f62010-07-06 01:34:17 +00003020 llvm::Value *CallTryExitVar = CGF.CreateTempAlloca(CGF.Builder.getInt1Ty(),
Anders Carlsson190d00e2009-02-07 21:26:04 +00003021 "_call_try_exit");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003022
John McCall9e2213d2010-10-04 23:42:51 +00003023 // A slot containing the exception to rethrow. Only needed when we
3024 // have both a @catch and a @finally.
3025 llvm::Value *PropagatingExnVar = 0;
3026
John McCallf1549f62010-07-06 01:34:17 +00003027 // Push a normal cleanup to leave the try scope.
John McCall1f0fca52010-07-21 07:22:38 +00003028 CGF.EHStack.pushCleanup<PerformFragileFinally>(NormalCleanup, &S,
John McCall0b251722010-08-04 05:59:32 +00003029 SyncArgSlot,
John McCall1f0fca52010-07-21 07:22:38 +00003030 CallTryExitVar,
3031 ExceptionData,
3032 &ObjCTypes);
John McCallf1549f62010-07-06 01:34:17 +00003033
3034 // Enter a try block:
3035 // - Call objc_exception_try_enter to push ExceptionData on top of
3036 // the EH stack.
3037 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3038 ->setDoesNotThrow();
3039
3040 // - Call setjmp on the exception data buffer.
3041 llvm::Constant *Zero = llvm::ConstantInt::get(CGF.Builder.getInt32Ty(), 0);
3042 llvm::Value *GEPIndexes[] = { Zero, Zero, Zero };
3043 llvm::Value *SetJmpBuffer =
Jay Foad0f6ac7c2011-07-22 08:16:57 +00003044 CGF.Builder.CreateGEP(ExceptionData, GEPIndexes, "setjmp_buffer");
John McCallf1549f62010-07-06 01:34:17 +00003045 llvm::CallInst *SetJmpResult =
3046 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer, "setjmp_result");
3047 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003048 SetJmpResult->setCanReturnTwice();
John McCallf1549f62010-07-06 01:34:17 +00003049
3050 // If setjmp returned 0, enter the protected block; otherwise,
3051 // branch to the handler.
Daniel Dunbar55e87422008-11-11 02:29:29 +00003052 llvm::BasicBlock *TryBlock = CGF.createBasicBlock("try");
3053 llvm::BasicBlock *TryHandler = CGF.createBasicBlock("try.handler");
John McCallf1549f62010-07-06 01:34:17 +00003054 llvm::Value *DidCatch =
John McCalld96a8e72010-08-11 00:16:14 +00003055 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3056 CGF.Builder.CreateCondBr(DidCatch, TryHandler, TryBlock);
Anders Carlsson80f25672008-09-09 17:59:25 +00003057
John McCallf1549f62010-07-06 01:34:17 +00003058 // Emit the protected block.
Anders Carlsson80f25672008-09-09 17:59:25 +00003059 CGF.EmitBlock(TryBlock);
John McCall0b251722010-08-04 05:59:32 +00003060 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003061 CGF.EmitStmt(isTry ? cast<ObjCAtTryStmt>(S).getTryBody()
John McCallf1549f62010-07-06 01:34:17 +00003062 : cast<ObjCAtSynchronizedStmt>(S).getSynchBody());
John McCall0b251722010-08-04 05:59:32 +00003063
3064 CGBuilderTy::InsertPoint TryFallthroughIP = CGF.Builder.saveAndClearIP();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003065
John McCallf1549f62010-07-06 01:34:17 +00003066 // Emit the exception handler block.
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003067 CGF.EmitBlock(TryHandler);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003068
John McCall87bb5822010-07-31 23:20:56 +00003069 // Don't optimize loads of the in-scope locals across this point.
3070 Hazards.emitWriteHazard();
3071
John McCallf1549f62010-07-06 01:34:17 +00003072 // For a @synchronized (or a @try with no catches), just branch
3073 // through the cleanup to the rethrow block.
3074 if (!isTry || !cast<ObjCAtTryStmt>(S).getNumCatchStmts()) {
3075 // Tell the cleanup not to re-pop the exit.
John McCall0b251722010-08-04 05:59:32 +00003076 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003077 CGF.EmitBranchThroughCleanup(FinallyRethrow);
John McCallf1549f62010-07-06 01:34:17 +00003078
3079 // Otherwise, we have to match against the caught exceptions.
3080 } else {
John McCall0b251722010-08-04 05:59:32 +00003081 // Retrieve the exception object. We may emit multiple blocks but
3082 // nothing can cross this so the value is already in SSA form.
3083 llvm::CallInst *Caught =
3084 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3085 ExceptionData, "caught");
3086 Caught->setDoesNotThrow();
3087
John McCallf1549f62010-07-06 01:34:17 +00003088 // Push the exception to rethrow onto the EH value stack for the
3089 // benefit of any @throws in the handlers.
3090 CGF.ObjCEHValueStack.push_back(Caught);
3091
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003092 const ObjCAtTryStmt* AtTryStmt = cast<ObjCAtTryStmt>(&S);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003093
John McCall0b251722010-08-04 05:59:32 +00003094 bool HasFinally = (AtTryStmt->getFinallyStmt() != 0);
John McCallf1549f62010-07-06 01:34:17 +00003095
John McCall0b251722010-08-04 05:59:32 +00003096 llvm::BasicBlock *CatchBlock = 0;
3097 llvm::BasicBlock *CatchHandler = 0;
3098 if (HasFinally) {
John McCall9e2213d2010-10-04 23:42:51 +00003099 // Save the currently-propagating exception before
3100 // objc_exception_try_enter clears the exception slot.
3101 PropagatingExnVar = CGF.CreateTempAlloca(Caught->getType(),
3102 "propagating_exception");
3103 CGF.Builder.CreateStore(Caught, PropagatingExnVar);
3104
John McCall0b251722010-08-04 05:59:32 +00003105 // Enter a new exception try block (in case a @catch block
3106 // throws an exception).
3107 CGF.Builder.CreateCall(ObjCTypes.getExceptionTryEnterFn(), ExceptionData)
3108 ->setDoesNotThrow();
Anders Carlsson80f25672008-09-09 17:59:25 +00003109
John McCall0b251722010-08-04 05:59:32 +00003110 llvm::CallInst *SetJmpResult =
3111 CGF.Builder.CreateCall(ObjCTypes.getSetJmpFn(), SetJmpBuffer,
3112 "setjmp.result");
3113 SetJmpResult->setDoesNotThrow();
Bill Wendling6446c3e2011-12-19 23:53:28 +00003114 SetJmpResult->setCanReturnTwice();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003115
John McCall0b251722010-08-04 05:59:32 +00003116 llvm::Value *Threw =
3117 CGF.Builder.CreateIsNotNull(SetJmpResult, "did_catch_exception");
3118
3119 CatchBlock = CGF.createBasicBlock("catch");
3120 CatchHandler = CGF.createBasicBlock("catch_for_catch");
3121 CGF.Builder.CreateCondBr(Threw, CatchHandler, CatchBlock);
3122
3123 CGF.EmitBlock(CatchBlock);
3124 }
3125
3126 CGF.Builder.CreateStore(CGF.Builder.getInt1(HasFinally), CallTryExitVar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003127
Daniel Dunbar55e40722008-09-27 07:03:52 +00003128 // Handle catch list. As a special case we check if everything is
3129 // matched and avoid generating code for falling off the end if
3130 // so.
3131 bool AllMatched = false;
Douglas Gregor8f5e3dd2010-04-23 22:50:49 +00003132 for (unsigned I = 0, N = AtTryStmt->getNumCatchStmts(); I != N; ++I) {
3133 const ObjCAtCatchStmt *CatchStmt = AtTryStmt->getCatchStmt(I);
Anders Carlsson80f25672008-09-09 17:59:25 +00003134
Douglas Gregorc00d8e12010-04-26 16:46:50 +00003135 const VarDecl *CatchParam = CatchStmt->getCatchParamDecl();
Steve Naroff14108da2009-07-10 23:34:53 +00003136 const ObjCObjectPointerType *OPT = 0;
Daniel Dunbar129271a2008-09-27 07:36:24 +00003137
Anders Carlsson80f25672008-09-09 17:59:25 +00003138 // catch(...) always matches.
Daniel Dunbar55e40722008-09-27 07:03:52 +00003139 if (!CatchParam) {
3140 AllMatched = true;
3141 } else {
John McCall183700f2009-09-21 23:43:11 +00003142 OPT = CatchParam->getType()->getAs<ObjCObjectPointerType>();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003143
John McCallf1549f62010-07-06 01:34:17 +00003144 // catch(id e) always matches under this ABI, since only
3145 // ObjC exceptions end up here in the first place.
Daniel Dunbar97f61d12008-09-27 22:21:14 +00003146 // FIXME: For the time being we also match id<X>; this should
3147 // be rejected by Sema instead.
Eli Friedman818e96f2009-07-11 00:57:02 +00003148 if (OPT && (OPT->isObjCIdType() || OPT->isObjCQualifiedIdType()))
Daniel Dunbar55e40722008-09-27 07:03:52 +00003149 AllMatched = true;
Anders Carlsson80f25672008-09-09 17:59:25 +00003150 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003151
John McCallf1549f62010-07-06 01:34:17 +00003152 // If this is a catch-all, we don't need to test anything.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003153 if (AllMatched) {
John McCallf1549f62010-07-06 01:34:17 +00003154 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
3155
Anders Carlssondde0a942008-09-11 09:15:33 +00003156 if (CatchParam) {
John McCallb6bbcc92010-10-15 04:57:14 +00003157 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003158 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
John McCallf1549f62010-07-06 01:34:17 +00003159
3160 // These types work out because ConvertType(id) == i8*.
Steve Naroff7ba138a2009-03-03 19:52:17 +00003161 CGF.Builder.CreateStore(Caught, CGF.GetAddrOfLocalVar(CatchParam));
Anders Carlssondde0a942008-09-11 09:15:33 +00003162 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003163
Anders Carlssondde0a942008-09-11 09:15:33 +00003164 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003165
3166 // The scope of the catch variable ends right here.
3167 CatchVarCleanups.ForceCleanup();
3168
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003169 CGF.EmitBranchThroughCleanup(FinallyEnd);
Anders Carlsson80f25672008-09-09 17:59:25 +00003170 break;
3171 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003172
Steve Naroff14108da2009-07-10 23:34:53 +00003173 assert(OPT && "Unexpected non-object pointer type in @catch");
John McCall506b57e2010-05-17 21:00:27 +00003174 const ObjCObjectType *ObjTy = OPT->getObjectType();
John McCallf1549f62010-07-06 01:34:17 +00003175
3176 // FIXME: @catch (Class c) ?
John McCall506b57e2010-05-17 21:00:27 +00003177 ObjCInterfaceDecl *IDecl = ObjTy->getInterface();
3178 assert(IDecl && "Catch parameter must have Objective-C type!");
Anders Carlsson80f25672008-09-09 17:59:25 +00003179
3180 // Check if the @catch block matches the exception object.
John McCall506b57e2010-05-17 21:00:27 +00003181 llvm::Value *Class = EmitClassRef(CGF.Builder, IDecl);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003182
John McCallf1549f62010-07-06 01:34:17 +00003183 llvm::CallInst *Match =
Chris Lattner34b02a12009-04-22 02:26:14 +00003184 CGF.Builder.CreateCall2(ObjCTypes.getExceptionMatchFn(),
3185 Class, Caught, "match");
John McCallf1549f62010-07-06 01:34:17 +00003186 Match->setDoesNotThrow();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003187
John McCallf1549f62010-07-06 01:34:17 +00003188 llvm::BasicBlock *MatchedBlock = CGF.createBasicBlock("match");
3189 llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch.next");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003190
3191 CGF.Builder.CreateCondBr(CGF.Builder.CreateIsNotNull(Match, "matched"),
Daniel Dunbare4b5ee02008-09-27 23:30:04 +00003192 MatchedBlock, NextCatchBlock);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003193
Anders Carlsson80f25672008-09-09 17:59:25 +00003194 // Emit the @catch block.
3195 CGF.EmitBlock(MatchedBlock);
John McCallf1549f62010-07-06 01:34:17 +00003196
3197 // Collect any cleanups for the catch variable. The scope lasts until
3198 // the end of the catch body.
John McCall0b251722010-08-04 05:59:32 +00003199 CodeGenFunction::RunCleanupsScope CatchVarCleanups(CGF);
John McCallf1549f62010-07-06 01:34:17 +00003200
John McCallb6bbcc92010-10-15 04:57:14 +00003201 CGF.EmitAutoVarDecl(*CatchParam);
Daniel Dunbara448fb22008-11-11 23:11:34 +00003202 assert(CGF.HaveInsertPoint() && "DeclStmt destroyed insert point?");
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003203
John McCallf1549f62010-07-06 01:34:17 +00003204 // Initialize the catch variable.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003205 llvm::Value *Tmp =
3206 CGF.Builder.CreateBitCast(Caught,
Benjamin Kramer578faa82011-09-27 21:06:10 +00003207 CGF.ConvertType(CatchParam->getType()));
Steve Naroff7ba138a2009-03-03 19:52:17 +00003208 CGF.Builder.CreateStore(Tmp, CGF.GetAddrOfLocalVar(CatchParam));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003209
Anders Carlssondde0a942008-09-11 09:15:33 +00003210 CGF.EmitStmt(CatchStmt->getCatchBody());
John McCallf1549f62010-07-06 01:34:17 +00003211
3212 // We're done with the catch variable.
3213 CatchVarCleanups.ForceCleanup();
3214
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003215 CGF.EmitBranchThroughCleanup(FinallyEnd);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003216
Anders Carlsson80f25672008-09-09 17:59:25 +00003217 CGF.EmitBlock(NextCatchBlock);
3218 }
3219
John McCallf1549f62010-07-06 01:34:17 +00003220 CGF.ObjCEHValueStack.pop_back();
3221
John McCall0b251722010-08-04 05:59:32 +00003222 // If nothing wanted anything to do with the caught exception,
3223 // kill the extract call.
3224 if (Caught->use_empty())
3225 Caught->eraseFromParent();
3226
3227 if (!AllMatched)
3228 CGF.EmitBranchThroughCleanup(FinallyRethrow);
3229
3230 if (HasFinally) {
3231 // Emit the exception handler for the @catch blocks.
3232 CGF.EmitBlock(CatchHandler);
3233
3234 // In theory we might now need a write hazard, but actually it's
3235 // unnecessary because there's no local-accessing code between
3236 // the try's write hazard and here.
3237 //Hazards.emitWriteHazard();
3238
John McCall9e2213d2010-10-04 23:42:51 +00003239 // Extract the new exception and save it to the
3240 // propagating-exception slot.
3241 assert(PropagatingExnVar);
3242 llvm::CallInst *NewCaught =
3243 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3244 ExceptionData, "caught");
3245 NewCaught->setDoesNotThrow();
3246 CGF.Builder.CreateStore(NewCaught, PropagatingExnVar);
3247
John McCall0b251722010-08-04 05:59:32 +00003248 // Don't pop the catch handler; the throw already did.
3249 CGF.Builder.CreateStore(CGF.Builder.getFalse(), CallTryExitVar);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003250 CGF.EmitBranchThroughCleanup(FinallyRethrow);
Daniel Dunbar55e40722008-09-27 07:03:52 +00003251 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003252 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003253
John McCall87bb5822010-07-31 23:20:56 +00003254 // Insert read hazards as required in the new blocks.
John McCall0b251722010-08-04 05:59:32 +00003255 Hazards.emitHazardsInNewBlocks();
John McCall87bb5822010-07-31 23:20:56 +00003256
John McCallf1549f62010-07-06 01:34:17 +00003257 // Pop the cleanup.
John McCall0b251722010-08-04 05:59:32 +00003258 CGF.Builder.restoreIP(TryFallthroughIP);
3259 if (CGF.HaveInsertPoint())
3260 CGF.Builder.CreateStore(CGF.Builder.getTrue(), CallTryExitVar);
John McCallf1549f62010-07-06 01:34:17 +00003261 CGF.PopCleanupBlock();
John McCall0b251722010-08-04 05:59:32 +00003262 CGF.EmitBlock(FinallyEnd.getBlock(), true);
Anders Carlssonf3a79a92009-02-09 20:38:58 +00003263
John McCallf1549f62010-07-06 01:34:17 +00003264 // Emit the rethrow block.
John McCall87bb5822010-07-31 23:20:56 +00003265 CGBuilderTy::InsertPoint SavedIP = CGF.Builder.saveAndClearIP();
John McCallff8e1152010-07-23 21:56:41 +00003266 CGF.EmitBlock(FinallyRethrow.getBlock(), true);
John McCallf1549f62010-07-06 01:34:17 +00003267 if (CGF.HaveInsertPoint()) {
John McCall9e2213d2010-10-04 23:42:51 +00003268 // If we have a propagating-exception variable, check it.
3269 llvm::Value *PropagatingExn;
3270 if (PropagatingExnVar) {
3271 PropagatingExn = CGF.Builder.CreateLoad(PropagatingExnVar);
John McCall0b251722010-08-04 05:59:32 +00003272
John McCall9e2213d2010-10-04 23:42:51 +00003273 // Otherwise, just look in the buffer for the exception to throw.
3274 } else {
3275 llvm::CallInst *Caught =
3276 CGF.Builder.CreateCall(ObjCTypes.getExceptionExtractFn(),
3277 ExceptionData);
3278 Caught->setDoesNotThrow();
3279 PropagatingExn = Caught;
3280 }
3281
3282 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), PropagatingExn)
John McCallf1549f62010-07-06 01:34:17 +00003283 ->setDoesNotThrow();
3284 CGF.Builder.CreateUnreachable();
Fariborz Jahanianf2878e52008-11-21 19:21:53 +00003285 }
Anders Carlsson80f25672008-09-09 17:59:25 +00003286
John McCall87bb5822010-07-31 23:20:56 +00003287 CGF.Builder.restoreIP(SavedIP);
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003288}
3289
3290void CGObjCMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar898d5082008-09-30 01:06:03 +00003291 const ObjCAtThrowStmt &S) {
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003292 llvm::Value *ExceptionAsObject;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003293
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003294 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00003295 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003296 ExceptionAsObject =
Benjamin Kramer578faa82011-09-27 21:06:10 +00003297 CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003298 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003299 assert((!CGF.ObjCEHValueStack.empty() && CGF.ObjCEHValueStack.back()) &&
Daniel Dunbar18ccc772008-09-28 01:03:14 +00003300 "Unexpected rethrow outside @catch block.");
Anders Carlsson273558f2009-02-07 21:37:21 +00003301 ExceptionAsObject = CGF.ObjCEHValueStack.back();
Anders Carlsson2b1e3112008-09-09 16:16:55 +00003302 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003303
John McCallf1549f62010-07-06 01:34:17 +00003304 CGF.Builder.CreateCall(ObjCTypes.getExceptionThrowFn(), ExceptionAsObject)
3305 ->setDoesNotReturn();
Anders Carlsson80f25672008-09-09 17:59:25 +00003306 CGF.Builder.CreateUnreachable();
Daniel Dunbara448fb22008-11-11 23:11:34 +00003307
3308 // Clear the insertion point to indicate we are in unreachable code.
3309 CGF.Builder.ClearInsertionPoint();
Anders Carlsson64d5d6c2008-09-09 10:04:29 +00003310}
3311
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003312/// EmitObjCWeakRead - Code gen for loading value of a __weak
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003313/// object: objc_read_weak (id *src)
3314///
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003315llvm::Value * CGObjCMac::EmitObjCWeakRead(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003316 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003317 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003318 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
3319 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj,
3320 ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00003321 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003322 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00003323 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6dc23172008-11-18 21:45:40 +00003324 return read_weak;
3325}
3326
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003327/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
3328/// objc_assign_weak (id src, id *dst)
3329///
3330void CGObjCMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003331 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003332 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003333 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003334 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003335 assert(Size <= 8 && "does not support size > 8");
3336 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003337 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003338 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3339 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003340 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3341 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00003342 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian3e283e32008-11-18 22:37:34 +00003343 src, dst, "weakassign");
3344 return;
3345}
3346
Fariborz Jahanian58626502008-11-19 00:59:10 +00003347/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
3348/// objc_assign_global (id src, id *dst)
3349///
3350void CGObjCMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003351 llvm::Value *src, llvm::Value *dst,
3352 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003353 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003354 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003355 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003356 assert(Size <= 8 && "does not support size > 8");
3357 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003358 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003359 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3360 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003361 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3362 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00003363 if (!threadlocal)
3364 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
3365 src, dst, "globalassign");
3366 else
3367 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
3368 src, dst, "threadlocalassign");
Fariborz Jahanian58626502008-11-19 00:59:10 +00003369 return;
3370}
3371
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003372/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003373/// objc_assign_ivar (id src, id *dst, ptrdiff_t ivaroffset)
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003374///
3375void CGObjCMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003376 llvm::Value *src, llvm::Value *dst,
3377 llvm::Value *ivarOffset) {
3378 assert(ivarOffset && "EmitObjCIvarAssign - ivarOffset is NULL");
Chris Lattner2acc6e32011-07-18 04:24:23 +00003379 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003380 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003381 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003382 assert(Size <= 8 && "does not support size > 8");
3383 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003384 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003385 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3386 }
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003387 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3388 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00003389 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
3390 src, dst, ivarOffset);
Fariborz Jahanian7eda8362008-11-20 19:23:36 +00003391 return;
3392}
3393
Fariborz Jahanian58626502008-11-19 00:59:10 +00003394/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
3395/// objc_assign_strongCast (id src, id *dst)
3396///
3397void CGObjCMac::EmitObjCStrongCastAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00003398 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00003399 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003400 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00003401 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00003402 assert(Size <= 8 && "does not support size > 8");
3403 src = (Size == 4) ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003404 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongLongTy);
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00003405 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
3406 }
Fariborz Jahaniandbd32c22008-11-19 17:34:06 +00003407 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
3408 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00003409 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian58626502008-11-19 00:59:10 +00003410 src, dst, "weakassign");
3411 return;
3412}
3413
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003414void CGObjCMac::EmitGCMemmoveCollectable(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003415 llvm::Value *DestPtr,
3416 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003417 llvm::Value *size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003418 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
3419 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003420 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00003421 DestPtr, SrcPtr, size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00003422 return;
3423}
3424
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003425/// EmitObjCValueForIvar - Code Gen for ivar reference.
3426///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003427LValue CGObjCMac::EmitObjCValueForIvar(CodeGen::CodeGenFunction &CGF,
3428 QualType ObjectTy,
3429 llvm::Value *BaseValue,
3430 const ObjCIvarDecl *Ivar,
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00003431 unsigned CVRQualifiers) {
John McCallc12c5bb2010-05-15 11:32:37 +00003432 const ObjCInterfaceDecl *ID =
3433 ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00003434 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
3435 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian0bb20362009-02-02 20:02:29 +00003436}
3437
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003438llvm::Value *CGObjCMac::EmitIvarOffset(CodeGen::CodeGenFunction &CGF,
Daniel Dunbar2a031922009-04-22 05:08:15 +00003439 const ObjCInterfaceDecl *Interface,
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003440 const ObjCIvarDecl *Ivar) {
Daniel Dunbar97776872009-04-22 07:32:20 +00003441 uint64_t Offset = ComputeIvarBaseOffset(CGM, Interface, Ivar);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003442 return llvm::ConstantInt::get(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003443 CGM.getTypes().ConvertType(CGM.getContext().LongTy),
3444 Offset);
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00003445}
3446
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003447/* *** Private Interface *** */
3448
3449/// EmitImageInfo - Emit the image info marker used to encode some module
3450/// level information.
3451///
3452/// See: <rdr://4810609&4810587&4810587>
3453/// struct IMAGE_INFO {
3454/// unsigned version;
3455/// unsigned flags;
3456/// };
3457enum ImageInfoFlags {
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003458 eImageInfo_FixAndContinue = (1 << 0),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003459 eImageInfo_GarbageCollected = (1 << 1),
3460 eImageInfo_GCOnly = (1 << 2),
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003461 eImageInfo_OptimizedByDyld = (1 << 3), // FIXME: When is this set.
3462
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003463 // A flag indicating that the module has no instances of a @synthesize of a
3464 // superclass variable. <rdar://problem/6803242>
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003465 eImageInfo_CorrectedSynthesize = (1 << 4)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003466};
3467
Daniel Dunbarfce176b2010-04-25 20:39:01 +00003468void CGObjCCommonMac::EmitImageInfo() {
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003469 unsigned version = 0; // Version is unused?
3470 unsigned flags = 0;
3471
3472 // FIXME: Fix and continue?
Douglas Gregore289d812011-09-13 17:21:33 +00003473 if (CGM.getLangOptions().getGC() != LangOptions::NonGC)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003474 flags |= eImageInfo_GarbageCollected;
Douglas Gregore289d812011-09-13 17:21:33 +00003475 if (CGM.getLangOptions().getGC() == LangOptions::GCOnly)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003476 flags |= eImageInfo_GCOnly;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003477
Daniel Dunbarc7c6dc02009-04-20 07:11:47 +00003478 // We never allow @synthesize of a superclass property.
3479 flags |= eImageInfo_CorrectedSynthesize;
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003480
Chris Lattner2acc6e32011-07-18 04:24:23 +00003481 llvm::Type *Int32Ty = llvm::Type::getInt32Ty(VMContext);
Chris Lattner77b89b82010-06-27 07:15:29 +00003482
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003483 // Emitted as int[2];
3484 llvm::Constant *values[2] = {
Chris Lattner77b89b82010-06-27 07:15:29 +00003485 llvm::ConstantInt::get(Int32Ty, version),
3486 llvm::ConstantInt::get(Int32Ty, flags)
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003487 };
Chris Lattner77b89b82010-06-27 07:15:29 +00003488 llvm::ArrayType *AT = llvm::ArrayType::get(Int32Ty, 2);
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003489
3490 const char *Section;
3491 if (ObjCABI == 1)
3492 Section = "__OBJC, __image_info,regular";
3493 else
3494 Section = "__DATA, __objc_imageinfo, regular, no_dead_strip";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003495 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003496 CreateMetadataVar("\01L_OBJC_IMAGE_INFO",
Jay Foad97357602011-06-22 09:24:39 +00003497 llvm::ConstantArray::get(AT, values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003498 Section,
3499 0,
3500 true);
3501 GV->setConstant(true);
Daniel Dunbarf77ac862008-08-11 21:35:06 +00003502}
3503
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003504
3505// struct objc_module {
3506// unsigned long version;
3507// unsigned long size;
3508// const char *name;
3509// Symtab symtab;
3510// };
3511
3512// FIXME: Get from somewhere
3513static const int ModuleVersion = 7;
3514
3515void CGObjCMac::EmitModuleInfo() {
Duncan Sands9408c452009-05-09 07:08:47 +00003516 uint64_t Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.ModuleTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003517
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00003518 llvm::Constant *Values[] = {
3519 llvm::ConstantInt::get(ObjCTypes.LongTy, ModuleVersion),
3520 llvm::ConstantInt::get(ObjCTypes.LongTy, Size),
3521 // This used to be the filename, now it is unused. <rdr://4327263>
3522 GetClassName(&CGM.getContext().Idents.get("")),
3523 EmitModuleSymbols()
3524 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003525 CreateMetadataVar("\01L_OBJC_MODULES",
Owen Anderson08e25242009-07-27 22:29:56 +00003526 llvm::ConstantStruct::get(ObjCTypes.ModuleTy, Values),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003527 "__OBJC,__module_info,regular,no_dead_strip",
Daniel Dunbar58a29122009-03-09 22:18:41 +00003528 4, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003529}
3530
3531llvm::Constant *CGObjCMac::EmitModuleSymbols() {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003532 unsigned NumClasses = DefinedClasses.size();
3533 unsigned NumCategories = DefinedCategories.size();
3534
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003535 // Return null if no symbols were defined.
3536 if (!NumClasses && !NumCategories)
Owen Andersonc9c88b42009-07-31 20:28:54 +00003537 return llvm::Constant::getNullValue(ObjCTypes.SymtabPtrTy);
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00003538
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003539 llvm::Constant *Values[5];
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003540 Values[0] = llvm::ConstantInt::get(ObjCTypes.LongTy, 0);
Owen Andersonc9c88b42009-07-31 20:28:54 +00003541 Values[1] = llvm::Constant::getNullValue(ObjCTypes.SelectorPtrTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00003542 Values[2] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumClasses);
3543 Values[3] = llvm::ConstantInt::get(ObjCTypes.ShortTy, NumCategories);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003544
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003545 // The runtime expects exactly the list of defined classes followed
3546 // by the list of defined categories, in a single array.
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003547 std::vector<llvm::Constant*> Symbols(NumClasses + NumCategories);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003548 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00003549 Symbols[i] = llvm::ConstantExpr::getBitCast(DefinedClasses[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003550 ObjCTypes.Int8PtrTy);
3551 for (unsigned i=0; i<NumCategories; i++)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003552 Symbols[NumClasses + i] =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003553 llvm::ConstantExpr::getBitCast(DefinedCategories[i],
Daniel Dunbar86e253a2008-08-22 20:34:54 +00003554 ObjCTypes.Int8PtrTy);
3555
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003556 Values[4] =
Owen Anderson96e0fc72009-07-29 22:16:19 +00003557 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003558 NumClasses + NumCategories),
3559 Symbols);
3560
Chris Lattnerc5cbb902011-06-20 04:01:35 +00003561 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003562
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003563 llvm::GlobalVariable *GV =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003564 CreateMetadataVar("\01L_OBJC_SYMBOLS", Init,
3565 "__OBJC,__symbols,regular,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003566 4, true);
Owen Anderson3c4972d2009-07-29 18:54:39 +00003567 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.SymtabPtrTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003568}
3569
John McCallf85e1932011-06-15 23:02:42 +00003570llvm::Value *CGObjCMac::EmitClassRefFromId(CGBuilderTy &Builder,
3571 IdentifierInfo *II) {
3572 LazySymbols.insert(II);
3573
3574 llvm::GlobalVariable *&Entry = ClassReferences[II];
3575
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003576 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003577 llvm::Constant *Casted =
John McCallf85e1932011-06-15 23:02:42 +00003578 llvm::ConstantExpr::getBitCast(GetClassName(II),
3579 ObjCTypes.ClassPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003580 Entry =
John McCallf85e1932011-06-15 23:02:42 +00003581 CreateMetadataVar("\01L_OBJC_CLASS_REFERENCES_", Casted,
3582 "__OBJC,__cls_refs,literal_pointers,no_dead_strip",
3583 4, true);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00003584 }
John McCallf85e1932011-06-15 23:02:42 +00003585
Benjamin Kramer578faa82011-09-27 21:06:10 +00003586 return Builder.CreateLoad(Entry);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003587}
3588
John McCallf85e1932011-06-15 23:02:42 +00003589llvm::Value *CGObjCMac::EmitClassRef(CGBuilderTy &Builder,
3590 const ObjCInterfaceDecl *ID) {
3591 return EmitClassRefFromId(Builder, ID->getIdentifier());
3592}
3593
3594llvm::Value *CGObjCMac::EmitNSAutoreleasePoolClassRef(CGBuilderTy &Builder) {
3595 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
3596 return EmitClassRefFromId(Builder, II);
3597}
3598
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003599llvm::Value *CGObjCMac::EmitSelector(CGBuilderTy &Builder, Selector Sel,
3600 bool lvalue) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003601 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003602
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003603 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003604 llvm::Constant *Casted =
Owen Anderson3c4972d2009-07-29 18:54:39 +00003605 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003606 ObjCTypes.SelectorPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003607 Entry =
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003608 CreateMetadataVar("\01L_OBJC_SELECTOR_REFERENCES_", Casted,
3609 "__OBJC,__message_refs,literal_pointers,no_dead_strip",
Daniel Dunbar0bf21992009-04-15 02:56:18 +00003610 4, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003611 }
3612
Fariborz Jahanian03b29602010-06-17 19:56:20 +00003613 if (lvalue)
3614 return Entry;
Benjamin Kramer578faa82011-09-27 21:06:10 +00003615 return Builder.CreateLoad(Entry);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00003616}
3617
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00003618llvm::Constant *CGObjCCommonMac::GetClassName(IdentifierInfo *Ident) {
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00003619 llvm::GlobalVariable *&Entry = ClassNames[Ident];
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003620
Daniel Dunbar63c5b502009-03-09 21:49:58 +00003621 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003622 Entry = CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00003623 llvm::ConstantArray::get(VMContext,
3624 Ident->getNameStart()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003625 ((ObjCABI == 2) ?
3626 "__TEXT,__objc_classname,cstring_literals" :
3627 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00003628 1, true);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003629
Owen Andersona1cf15f2009-07-14 23:10:40 +00003630 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00003631}
3632
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003633llvm::Function *CGObjCCommonMac::GetMethodDefinition(const ObjCMethodDecl *MD) {
3634 llvm::DenseMap<const ObjCMethodDecl*, llvm::Function*>::iterator
3635 I = MethodDefinitions.find(MD);
3636 if (I != MethodDefinitions.end())
3637 return I->second;
3638
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00003639 return NULL;
3640}
3641
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003642/// GetIvarLayoutName - Returns a unique constant for the given
3643/// ivar layout bitmap.
3644llvm::Constant *CGObjCCommonMac::GetIvarLayoutName(IdentifierInfo *Ident,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003645 const ObjCCommonTypesHelper &ObjCTypes) {
Owen Andersonc9c88b42009-07-31 20:28:54 +00003646 return llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Fariborz Jahaniand80d81b2009-03-05 19:17:31 +00003647}
3648
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003649void CGObjCCommonMac::BuildAggrIvarRecordLayout(const RecordType *RT,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003650 unsigned int BytePos,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003651 bool ForStrongLayout,
3652 bool &HasUnion) {
3653 const RecordDecl *RD = RT->getDecl();
3654 // FIXME - Use iterator.
Jordy Rosedb8264e2011-07-22 02:08:32 +00003655 SmallVector<const FieldDecl*, 16> Fields(RD->field_begin(), RD->field_end());
Chris Lattner2acc6e32011-07-18 04:24:23 +00003656 llvm::Type *Ty = CGM.getTypes().ConvertType(QualType(RT, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003657 const llvm::StructLayout *RecLayout =
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003658 CGM.getTargetData().getStructLayout(cast<llvm::StructType>(Ty));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003659
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003660 BuildAggrIvarLayout(0, RecLayout, RD, Fields, BytePos,
3661 ForStrongLayout, HasUnion);
3662}
3663
Daniel Dunbar5a5a8032009-05-03 21:05:10 +00003664void CGObjCCommonMac::BuildAggrIvarLayout(const ObjCImplementationDecl *OI,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003665 const llvm::StructLayout *Layout,
3666 const RecordDecl *RD,
Jordy Rosedb8264e2011-07-22 02:08:32 +00003667 const SmallVectorImpl<const FieldDecl*> &RecFields,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003668 unsigned int BytePos, bool ForStrongLayout,
3669 bool &HasUnion) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003670 bool IsUnion = (RD && RD->isUnion());
3671 uint64_t MaxUnionIvarSize = 0;
3672 uint64_t MaxSkippedUnionIvarSize = 0;
Jordy Rosedb8264e2011-07-22 02:08:32 +00003673 const FieldDecl *MaxField = 0;
3674 const FieldDecl *MaxSkippedField = 0;
3675 const FieldDecl *LastFieldBitfieldOrUnnamed = 0;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003676 uint64_t MaxFieldOffset = 0;
3677 uint64_t MaxSkippedFieldOffset = 0;
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003678 uint64_t LastBitfieldOrUnnamedOffset = 0;
John McCallf85e1932011-06-15 23:02:42 +00003679 uint64_t FirstFieldDelta = 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003680
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003681 if (RecFields.empty())
3682 return;
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003683 unsigned WordSizeInBits = CGM.getContext().getTargetInfo().getPointerWidth(0);
3684 unsigned ByteSizeInBits = CGM.getContext().getTargetInfo().getCharWidth();
John McCallf85e1932011-06-15 23:02:42 +00003685 if (!RD && CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003686 const FieldDecl *FirstField = RecFields[0];
John McCallf85e1932011-06-15 23:02:42 +00003687 FirstFieldDelta =
3688 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(FirstField));
3689 }
3690
Chris Lattnerf1690852009-03-31 08:48:01 +00003691 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003692 const FieldDecl *Field = RecFields[i];
Daniel Dunbare05cc982009-05-03 23:35:23 +00003693 uint64_t FieldOffset;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003694 if (RD) {
Daniel Dunbarf8f8eba2010-04-14 17:02:21 +00003695 // Note that 'i' here is actually the field index inside RD of Field,
3696 // although this dependency is hidden.
3697 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
John McCallf85e1932011-06-15 23:02:42 +00003698 FieldOffset = (RL.getFieldOffset(i) / ByteSizeInBits) - FirstFieldDelta;
Anders Carlssonfc514ab2009-07-24 17:23:54 +00003699 } else
John McCallf85e1932011-06-15 23:02:42 +00003700 FieldOffset =
3701 ComputeIvarBaseOffset(CGM, OI, cast<ObjCIvarDecl>(Field)) - FirstFieldDelta;
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003702
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003703 // Skip over unnamed or bitfields
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003704 if (!Field->getIdentifier() || Field->isBitField()) {
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003705 LastFieldBitfieldOrUnnamed = Field;
3706 LastBitfieldOrUnnamedOffset = FieldOffset;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003707 continue;
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003708 }
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003709
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003710 LastFieldBitfieldOrUnnamed = 0;
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003711 QualType FQT = Field->getType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003712 if (FQT->isRecordType() || FQT->isUnionType()) {
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003713 if (FQT->isUnionType())
3714 HasUnion = true;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003715
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003716 BuildAggrIvarRecordLayout(FQT->getAs<RecordType>(),
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003717 BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003718 ForStrongLayout, HasUnion);
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003719 continue;
3720 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003721
Chris Lattnerf1690852009-03-31 08:48:01 +00003722 if (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003723 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003724 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003725 uint64_t ElCount = CArray->getSize().getZExtValue();
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003726 assert(CArray && "only array with known element size is supported");
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003727 FQT = CArray->getElementType();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003728 while (const ArrayType *Array = CGM.getContext().getAsArrayType(FQT)) {
3729 const ConstantArrayType *CArray =
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003730 dyn_cast_or_null<ConstantArrayType>(Array);
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003731 ElCount *= CArray->getSize().getZExtValue();
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003732 FQT = CArray->getElementType();
3733 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003734
3735 assert(!FQT->isUnionType() &&
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003736 "layout for array of unions not supported");
Fariborz Jahanianf96bdf42011-01-03 19:23:18 +00003737 if (FQT->isRecordType() && ElCount) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003738 int OldIndex = IvarsInfo.size() - 1;
3739 int OldSkIndex = SkipIvars.size() -1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003740
Ted Kremenek6217b802009-07-29 21:53:49 +00003741 const RecordType *RT = FQT->getAs<RecordType>();
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003742 BuildAggrIvarRecordLayout(RT, BytePos + FieldOffset,
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003743 ForStrongLayout, HasUnion);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003744
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003745 // Replicate layout information for each array element. Note that
3746 // one element is already done.
3747 uint64_t ElIx = 1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003748 for (int FirstIndex = IvarsInfo.size() - 1,
3749 FirstSkIndex = SkipIvars.size() - 1 ;ElIx < ElCount; ElIx++) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003750 uint64_t Size = CGM.getContext().getTypeSize(RT)/ByteSizeInBits;
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003751 for (int i = OldIndex+1; i <= FirstIndex; ++i)
3752 IvarsInfo.push_back(GC_IVAR(IvarsInfo[i].ivar_bytepos + Size*ElIx,
3753 IvarsInfo[i].ivar_size));
3754 for (int i = OldSkIndex+1; i <= FirstSkIndex; ++i)
3755 SkipIvars.push_back(GC_IVAR(SkipIvars[i].ivar_bytepos + Size*ElIx,
3756 SkipIvars[i].ivar_size));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003757 }
3758 continue;
3759 }
Fariborz Jahaniana5a10c32009-03-10 16:22:08 +00003760 }
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003761 // At this point, we are done with Record/Union and array there of.
3762 // For other arrays we are down to its element type.
John McCall0953e762009-09-24 19:53:00 +00003763 Qualifiers::GC GCAttr = GetGCAttrTypeForType(CGM.getContext(), FQT);
Daniel Dunbar5e563dd2009-05-03 13:55:09 +00003764
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003765 unsigned FieldSize = CGM.getContext().getTypeSize(Field->getType());
John McCall0953e762009-09-24 19:53:00 +00003766 if ((ForStrongLayout && GCAttr == Qualifiers::Strong)
3767 || (!ForStrongLayout && GCAttr == Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003768 if (IsUnion) {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003769 uint64_t UnionIvarSize = FieldSize / WordSizeInBits;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003770 if (UnionIvarSize > MaxUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003771 MaxUnionIvarSize = UnionIvarSize;
3772 MaxField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003773 MaxFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003774 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003775 } else {
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003776 IvarsInfo.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003777 FieldSize / WordSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003778 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003779 } else if ((ForStrongLayout &&
John McCall0953e762009-09-24 19:53:00 +00003780 (GCAttr == Qualifiers::GCNone || GCAttr == Qualifiers::Weak))
3781 || (!ForStrongLayout && GCAttr != Qualifiers::Weak)) {
Daniel Dunbar487993b2009-05-03 13:32:01 +00003782 if (IsUnion) {
Mike Stumpf5408fe2009-05-16 07:57:57 +00003783 // FIXME: Why the asymmetry? We divide by word size in bits on other
3784 // side.
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003785 uint64_t UnionIvarSize = FieldSize;
Daniel Dunbar487993b2009-05-03 13:32:01 +00003786 if (UnionIvarSize > MaxSkippedUnionIvarSize) {
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003787 MaxSkippedUnionIvarSize = UnionIvarSize;
3788 MaxSkippedField = Field;
Daniel Dunbar900c1982009-05-03 23:31:46 +00003789 MaxSkippedFieldOffset = FieldOffset;
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003790 }
Daniel Dunbar487993b2009-05-03 13:32:01 +00003791 } else {
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003792 // FIXME: Why the asymmetry, we divide by byte size in bits here?
Daniel Dunbar25d583e2009-05-03 14:17:18 +00003793 SkipIvars.push_back(GC_IVAR(BytePos + FieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003794 FieldSize / ByteSizeInBits));
Fariborz Jahanian820e0202009-03-11 00:07:04 +00003795 }
3796 }
3797 }
Daniel Dunbard58edcb2009-05-03 14:10:34 +00003798
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003799 if (LastFieldBitfieldOrUnnamed) {
3800 if (LastFieldBitfieldOrUnnamed->isBitField()) {
3801 // Last field was a bitfield. Must update skip info.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003802 uint64_t BitFieldSize
3803 = LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
Argyrios Kyrtzidis0dc75092010-09-06 12:00:10 +00003804 GC_IVAR skivar;
3805 skivar.ivar_bytepos = BytePos + LastBitfieldOrUnnamedOffset;
3806 skivar.ivar_size = (BitFieldSize / ByteSizeInBits)
3807 + ((BitFieldSize % ByteSizeInBits) != 0);
3808 SkipIvars.push_back(skivar);
3809 } else {
3810 assert(!LastFieldBitfieldOrUnnamed->getIdentifier() &&"Expected unnamed");
3811 // Last field was unnamed. Must update skip info.
3812 unsigned FieldSize
3813 = CGM.getContext().getTypeSize(LastFieldBitfieldOrUnnamed->getType());
3814 SkipIvars.push_back(GC_IVAR(BytePos + LastBitfieldOrUnnamedOffset,
3815 FieldSize / ByteSizeInBits));
3816 }
Fariborz Jahanian7fb16272009-04-21 18:33:06 +00003817 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003818
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003819 if (MaxField)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003820 IvarsInfo.push_back(GC_IVAR(BytePos + MaxFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003821 MaxUnionIvarSize));
3822 if (MaxSkippedField)
Daniel Dunbar900c1982009-05-03 23:31:46 +00003823 SkipIvars.push_back(GC_IVAR(BytePos + MaxSkippedFieldOffset,
Daniel Dunbar8b2926c2009-05-03 13:44:42 +00003824 MaxSkippedUnionIvarSize));
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00003825}
3826
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00003827/// BuildIvarLayoutBitmap - This routine is the horsework for doing all
3828/// the computations and returning the layout bitmap (for ivar or blocks) in
3829/// the given argument BitMap string container. Routine reads
3830/// two containers, IvarsInfo and SkipIvars which are assumed to be
3831/// filled already by the caller.
3832llvm::Constant *CGObjCCommonMac::BuildIvarLayoutBitmap(std::string& BitMap) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003833 unsigned int WordsToScan, WordsToSkip;
Chris Lattner2acc6e32011-07-18 04:24:23 +00003834 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003835
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003836 // Build the string of skip/scan nibbles
Chris Lattner5f9e2722011-07-23 10:55:15 +00003837 SmallVector<SKIP_SCAN, 32> SkipScanIvars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003838 unsigned int WordSize =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003839 CGM.getTypes().getTargetData().getTypeAllocSize(PtrTy);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003840 if (IvarsInfo[0].ivar_bytepos == 0) {
3841 WordsToSkip = 0;
3842 WordsToScan = IvarsInfo[0].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003843 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003844 WordsToSkip = IvarsInfo[0].ivar_bytepos/WordSize;
3845 WordsToScan = IvarsInfo[0].ivar_size;
3846 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003847 for (unsigned int i=1, Last=IvarsInfo.size(); i != Last; i++) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003848 unsigned int TailPrevGCObjC =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003849 IvarsInfo[i-1].ivar_bytepos + IvarsInfo[i-1].ivar_size * WordSize;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003850 if (IvarsInfo[i].ivar_bytepos == TailPrevGCObjC) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003851 // consecutive 'scanned' object pointers.
3852 WordsToScan += IvarsInfo[i].ivar_size;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003853 } else {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003854 // Skip over 'gc'able object pointer which lay over each other.
3855 if (TailPrevGCObjC > IvarsInfo[i].ivar_bytepos)
3856 continue;
3857 // Must skip over 1 or more words. We save current skip/scan values
3858 // and start a new pair.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003859 SKIP_SCAN SkScan;
3860 SkScan.skip = WordsToSkip;
3861 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003862 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003863
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003864 // Skip the hole.
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003865 SkScan.skip = (IvarsInfo[i].ivar_bytepos - TailPrevGCObjC) / WordSize;
3866 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003867 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003868 WordsToSkip = 0;
3869 WordsToScan = IvarsInfo[i].ivar_size;
3870 }
3871 }
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003872 if (WordsToScan > 0) {
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003873 SKIP_SCAN SkScan;
3874 SkScan.skip = WordsToSkip;
3875 SkScan.scan = WordsToScan;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003876 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003877 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003878
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003879 if (!SkipIvars.empty()) {
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003880 unsigned int LastIndex = SkipIvars.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003881 int LastByteSkipped =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003882 SkipIvars[LastIndex].ivar_bytepos + SkipIvars[LastIndex].ivar_size;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003883 LastIndex = IvarsInfo.size()-1;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003884 int LastByteScanned =
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003885 IvarsInfo[LastIndex].ivar_bytepos +
3886 IvarsInfo[LastIndex].ivar_size * WordSize;
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003887 // Compute number of bytes to skip at the tail end of the last ivar scanned.
Benjamin Kramer54d76db2009-12-25 15:43:36 +00003888 if (LastByteSkipped > LastByteScanned) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003889 unsigned int TotalWords = (LastByteSkipped + (WordSize -1)) / WordSize;
Fariborz Jahanianc8ce9c82009-03-12 22:50:49 +00003890 SKIP_SCAN SkScan;
3891 SkScan.skip = TotalWords - (LastByteScanned/WordSize);
3892 SkScan.scan = 0;
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003893 SkipScanIvars.push_back(SkScan);
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003894 }
3895 }
3896 // Mini optimization of nibbles such that an 0xM0 followed by 0x0N is produced
3897 // as 0xMN.
Fariborz Jahanian81adc052009-04-24 16:17:09 +00003898 int SkipScan = SkipScanIvars.size()-1;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003899 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003900 if ((i < SkipScan) && SkipScanIvars[i].skip && SkipScanIvars[i].scan == 0
3901 && SkipScanIvars[i+1].skip == 0 && SkipScanIvars[i+1].scan) {
3902 // 0xM0 followed by 0x0N detected.
3903 SkipScanIvars[i].scan = SkipScanIvars[i+1].scan;
3904 for (int j = i+1; j < SkipScan; j++)
3905 SkipScanIvars[j] = SkipScanIvars[j+1];
3906 --SkipScan;
3907 }
3908 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003909
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003910 // Generate the string.
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003911 for (int i = 0; i <= SkipScan; i++) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003912 unsigned char byte;
3913 unsigned int skip_small = SkipScanIvars[i].skip % 0xf;
3914 unsigned int scan_small = SkipScanIvars[i].scan % 0xf;
3915 unsigned int skip_big = SkipScanIvars[i].skip / 0xf;
3916 unsigned int scan_big = SkipScanIvars[i].scan / 0xf;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003917
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003918 // first skip big.
3919 for (unsigned int ix = 0; ix < skip_big; ix++)
3920 BitMap += (unsigned char)(0xf0);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003921
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003922 // next (skip small, scan)
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003923 if (skip_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003924 byte = skip_small << 4;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003925 if (scan_big > 0) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003926 byte |= 0xf;
3927 --scan_big;
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003928 } else if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003929 byte |= scan_small;
3930 scan_small = 0;
3931 }
3932 BitMap += byte;
3933 }
3934 // next scan big
3935 for (unsigned int ix = 0; ix < scan_big; ix++)
3936 BitMap += (unsigned char)(0x0f);
3937 // last scan small
Daniel Dunbar31682fd2009-05-03 23:21:22 +00003938 if (scan_small) {
Fariborz Jahanian9397e1d2009-03-11 20:59:05 +00003939 byte = scan_small;
3940 BitMap += byte;
3941 }
3942 }
3943 // null terminate string.
Fariborz Jahanian667423a2009-03-25 22:36:49 +00003944 unsigned char zero = 0;
3945 BitMap += zero;
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003946
3947 llvm::GlobalVariable * Entry =
3948 CreateMetadataVar("\01L_OBJC_CLASS_NAME_",
3949 llvm::ConstantArray::get(VMContext, BitMap.c_str()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00003950 ((ObjCABI == 2) ?
3951 "__TEXT,__objc_classname,cstring_literals" :
3952 "__TEXT,__cstring,cstring_literals"),
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003953 1, true);
3954 return getConstantGEP(VMContext, Entry, 0, 0);
3955}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00003956
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003957/// BuildIvarLayout - Builds ivar layout bitmap for the class
3958/// implementation for the __strong or __weak case.
3959/// The layout map displays which words in ivar list must be skipped
3960/// and which must be scanned by GC (see below). String is built of bytes.
3961/// Each byte is divided up in two nibbles (4-bit each). Left nibble is count
3962/// of words to skip and right nibble is count of words to scan. So, each
3963/// nibble represents up to 15 workds to skip or scan. Skipping the rest is
3964/// represented by a 0x00 byte which also ends the string.
3965/// 1. when ForStrongLayout is true, following ivars are scanned:
3966/// - id, Class
3967/// - object *
3968/// - __strong anything
3969///
3970/// 2. When ForStrongLayout is false, following ivars are scanned:
3971/// - __weak anything
3972///
3973llvm::Constant *CGObjCCommonMac::BuildIvarLayout(
3974 const ObjCImplementationDecl *OMD,
3975 bool ForStrongLayout) {
3976 bool hasUnion = false;
3977
Chris Lattner2acc6e32011-07-18 04:24:23 +00003978 llvm::Type *PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Douglas Gregore289d812011-09-13 17:21:33 +00003979 if (CGM.getLangOptions().getGC() == LangOptions::NonGC &&
John McCallf85e1932011-06-15 23:02:42 +00003980 !CGM.getLangOptions().ObjCAutoRefCount)
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003981 return llvm::Constant::getNullValue(PtrTy);
3982
Jordy Rosedb8264e2011-07-22 02:08:32 +00003983 const ObjCInterfaceDecl *OI = OMD->getClassInterface();
3984 SmallVector<const FieldDecl*, 32> RecFields;
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003985 if (CGM.getLangOptions().ObjCAutoRefCount) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003986 for (const ObjCIvarDecl *IVD = OI->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003987 IVD; IVD = IVD->getNextIvar())
3988 RecFields.push_back(cast<FieldDecl>(IVD));
3989 }
3990 else {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003991 SmallVector<const ObjCIvarDecl*, 32> Ivars;
John McCallf85e1932011-06-15 23:02:42 +00003992 CGM.getContext().DeepCollectObjCIvars(OI, true, Ivars);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003993
Jordy Rosedb8264e2011-07-22 02:08:32 +00003994 // FIXME: This is not ideal; we shouldn't have to do this copy.
3995 RecFields.append(Ivars.begin(), Ivars.end());
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00003996 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00003997
3998 if (RecFields.empty())
3999 return llvm::Constant::getNullValue(PtrTy);
4000
4001 SkipIvars.clear();
4002 IvarsInfo.clear();
4003
4004 BuildAggrIvarLayout(OMD, 0, 0, RecFields, 0, ForStrongLayout, hasUnion);
4005 if (IvarsInfo.empty())
4006 return llvm::Constant::getNullValue(PtrTy);
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004007 // Sort on byte position in case we encounterred a union nested in
4008 // the ivar list.
4009 if (hasUnion && !IvarsInfo.empty())
4010 std::sort(IvarsInfo.begin(), IvarsInfo.end());
4011 if (hasUnion && !SkipIvars.empty())
4012 std::sort(SkipIvars.begin(), SkipIvars.end());
4013
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004014 std::string BitMap;
Fariborz Jahanianb8fd2eb2010-08-05 00:19:48 +00004015 llvm::Constant *C = BuildIvarLayoutBitmap(BitMap);
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004016
4017 if (CGM.getLangOptions().ObjCGCBitmapPrint) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004018 printf("\n%s ivar layout for class '%s': ",
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004019 ForStrongLayout ? "strong" : "weak",
Daniel Dunbar4087f272010-08-17 22:39:59 +00004020 OMD->getClassInterface()->getName().data());
Fariborz Jahanian3d2ad662009-04-20 22:03:45 +00004021 const unsigned char *s = (unsigned char*)BitMap.c_str();
4022 for (unsigned i = 0; i < BitMap.size(); i++)
4023 if (!(s[i] & 0xf0))
4024 printf("0x0%x%s", s[i], s[i] != 0 ? ", " : "");
4025 else
4026 printf("0x%x%s", s[i], s[i] != 0 ? ", " : "");
4027 printf("\n");
4028 }
Fariborz Jahanian93ce50d2010-08-04 23:55:24 +00004029 return C;
Fariborz Jahaniand61a50a2009-03-05 22:39:55 +00004030}
4031
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004032llvm::Constant *CGObjCCommonMac::GetMethodVarName(Selector Sel) {
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004033 llvm::GlobalVariable *&Entry = MethodVarNames[Sel];
4034
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004035 // FIXME: Avoid std::string copying.
4036 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004037 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_NAME_",
Owen Anderson0032b272009-08-13 21:57:51 +00004038 llvm::ConstantArray::get(VMContext, Sel.getAsString()),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004039 ((ObjCABI == 2) ?
4040 "__TEXT,__objc_methname,cstring_literals" :
4041 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004042 1, true);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004043
Owen Andersona1cf15f2009-07-14 23:10:40 +00004044 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004045}
4046
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004047// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004048llvm::Constant *CGObjCCommonMac::GetMethodVarName(IdentifierInfo *ID) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004049 return GetMethodVarName(CGM.getContext().Selectors.getNullarySelector(ID));
4050}
4051
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00004052llvm::Constant *CGObjCCommonMac::GetMethodVarType(const FieldDecl *Field) {
Devang Patel7794bb82009-03-04 18:21:39 +00004053 std::string TypeStr;
4054 CGM.getContext().getObjCEncodingForType(Field->getType(), TypeStr, Field);
4055
4056 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004057
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004058 if (!Entry)
4059 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004060 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004061 ((ObjCABI == 2) ?
4062 "__TEXT,__objc_methtype,cstring_literals" :
4063 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004064 1, true);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004065
Owen Andersona1cf15f2009-07-14 23:10:40 +00004066 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar259d93d2008-08-12 03:39:23 +00004067}
4068
Bob Wilsondc8dab62011-11-30 01:57:58 +00004069llvm::Constant *CGObjCCommonMac::GetMethodVarType(const ObjCMethodDecl *D,
4070 bool Extended) {
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004071 std::string TypeStr;
Douglas Gregorf968d832011-05-27 01:19:52 +00004072 if (CGM.getContext().getObjCEncodingForMethodDecl(
4073 const_cast<ObjCMethodDecl*>(D),
Bob Wilsondc8dab62011-11-30 01:57:58 +00004074 TypeStr, Extended))
Douglas Gregorf968d832011-05-27 01:19:52 +00004075 return 0;
Devang Patel7794bb82009-03-04 18:21:39 +00004076
4077 llvm::GlobalVariable *&Entry = MethodVarTypes[TypeStr];
4078
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004079 if (!Entry)
4080 Entry = CreateMetadataVar("\01L_OBJC_METH_VAR_TYPE_",
Owen Anderson0032b272009-08-13 21:57:51 +00004081 llvm::ConstantArray::get(VMContext, TypeStr),
Daniel Dunbaraf19ac42011-03-25 20:09:09 +00004082 ((ObjCABI == 2) ?
4083 "__TEXT,__objc_methtype,cstring_literals" :
4084 "__TEXT,__cstring,cstring_literals"),
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004085 1, true);
Devang Patel7794bb82009-03-04 18:21:39 +00004086
Owen Andersona1cf15f2009-07-14 23:10:40 +00004087 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004088}
4089
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004090// FIXME: Merge into a single cstring creation function.
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004091llvm::Constant *CGObjCCommonMac::GetPropertyName(IdentifierInfo *Ident) {
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004092 llvm::GlobalVariable *&Entry = PropertyNames[Ident];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004093
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004094 if (!Entry)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004095 Entry = CreateMetadataVar("\01L_OBJC_PROP_NAME_ATTR_",
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004096 llvm::ConstantArray::get(VMContext,
4097 Ident->getNameStart()),
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004098 "__TEXT,__cstring,cstring_literals",
Daniel Dunbarb90bb002009-04-14 23:14:47 +00004099 1, true);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004100
Owen Andersona1cf15f2009-07-14 23:10:40 +00004101 return getConstantGEP(VMContext, Entry, 0, 0);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004102}
4103
4104// FIXME: Merge into a single cstring creation function.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004105// FIXME: This Decl should be more precise.
Daniel Dunbar63c5b502009-03-09 21:49:58 +00004106llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004107CGObjCCommonMac::GetPropertyTypeString(const ObjCPropertyDecl *PD,
4108 const Decl *Container) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00004109 std::string TypeStr;
4110 CGM.getContext().getObjCEncodingForPropertyDecl(PD, Container, TypeStr);
Daniel Dunbarc8ef5512008-08-23 00:19:03 +00004111 return GetPropertyName(&CGM.getContext().Idents.get(TypeStr));
4112}
4113
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004114void CGObjCCommonMac::GetNameForMethod(const ObjCMethodDecl *D,
Fariborz Jahanian56210f72009-01-21 23:34:32 +00004115 const ObjCContainerDecl *CD,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004116 SmallVectorImpl<char> &Name) {
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004117 llvm::raw_svector_ostream OS(Name);
Fariborz Jahanian679a5022009-01-10 21:06:09 +00004118 assert (CD && "Missing container decl in GetNameForMethod");
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004119 OS << '\01' << (D->isInstanceMethod() ? '-' : '+')
4120 << '[' << CD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004121 if (const ObjCCategoryImplDecl *CID =
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004122 dyn_cast<ObjCCategoryImplDecl>(D->getDeclContext()))
Benjamin Kramer900fc632010-04-17 09:33:03 +00004123 OS << '(' << CID << ')';
Daniel Dunbarc575ce72009-10-19 01:21:19 +00004124 OS << ' ' << D->getSelector().getAsString() << ']';
Daniel Dunbarb7ec2462008-08-16 03:19:19 +00004125}
4126
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004127void CGObjCMac::FinishModule() {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004128 EmitModuleInfo();
4129
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004130 // Emit the dummy bodies for any protocols which were referenced but
4131 // never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004132 for (llvm::DenseMap<IdentifierInfo*, llvm::GlobalVariable*>::iterator
Chris Lattnerad64e022009-07-17 23:57:13 +00004133 I = Protocols.begin(), e = Protocols.end(); I != e; ++I) {
4134 if (I->second->hasInitializer())
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004135 continue;
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004136
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004137 llvm::Constant *Values[5];
Owen Andersonc9c88b42009-07-31 20:28:54 +00004138 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ProtocolExtensionPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004139 Values[1] = GetClassName(I->first);
Owen Andersonc9c88b42009-07-31 20:28:54 +00004140 Values[2] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListPtrTy);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004141 Values[3] = Values[4] =
Owen Andersonc9c88b42009-07-31 20:28:54 +00004142 llvm::Constant::getNullValue(ObjCTypes.MethodDescriptionListPtrTy);
Chris Lattnerad64e022009-07-17 23:57:13 +00004143 I->second->setLinkage(llvm::GlobalValue::InternalLinkage);
Owen Anderson08e25242009-07-27 22:29:56 +00004144 I->second->setInitializer(llvm::ConstantStruct::get(ObjCTypes.ProtocolTy,
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004145 Values));
Chris Lattnerad64e022009-07-17 23:57:13 +00004146 CGM.AddUsedGlobal(I->second);
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004147 }
4148
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004149 // Add assembler directives to add lazy undefined symbol references
4150 // for classes which are referenced but not defined. This is
4151 // important for correct linker interaction.
Daniel Dunbar33063492009-09-07 00:20:42 +00004152 //
4153 // FIXME: It would be nice if we had an LLVM construct for this.
4154 if (!LazySymbols.empty() || !DefinedSymbols.empty()) {
4155 llvm::SmallString<256> Asm;
4156 Asm += CGM.getModule().getModuleInlineAsm();
4157 if (!Asm.empty() && Asm.back() != '\n')
4158 Asm += '\n';
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004159
Daniel Dunbar33063492009-09-07 00:20:42 +00004160 llvm::raw_svector_ostream OS(Asm);
Daniel Dunbar33063492009-09-07 00:20:42 +00004161 for (llvm::SetVector<IdentifierInfo*>::iterator I = DefinedSymbols.begin(),
4162 e = DefinedSymbols.end(); I != e; ++I)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00004163 OS << "\t.objc_class_name_" << (*I)->getName() << "=0\n"
4164 << "\t.globl .objc_class_name_" << (*I)->getName() << "\n";
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004165 for (llvm::SetVector<IdentifierInfo*>::iterator I = LazySymbols.begin(),
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004166 e = LazySymbols.end(); I != e; ++I) {
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004167 OS << "\t.lazy_reference .objc_class_name_" << (*I)->getName() << "\n";
Fariborz Jahanianb9c5b3d2010-06-21 22:05:18 +00004168 }
4169
4170 for (size_t i = 0; i < DefinedCategoryNames.size(); ++i) {
4171 OS << "\t.objc_category_name_" << DefinedCategoryNames[i] << "=0\n"
4172 << "\t.globl .objc_category_name_" << DefinedCategoryNames[i] << "\n";
4173 }
Chris Lattnerafd5eda2010-04-17 18:26:20 +00004174
Daniel Dunbar33063492009-09-07 00:20:42 +00004175 CGM.getModule().setModuleInlineAsm(OS.str());
Daniel Dunbar242d4dc2008-08-25 06:02:07 +00004176 }
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004177}
4178
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004179CGObjCNonFragileABIMac::CGObjCNonFragileABIMac(CodeGen::CodeGenModule &cgm)
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004180 : CGObjCCommonMac(cgm),
Mike Stump1eb44332009-09-09 15:08:12 +00004181 ObjCTypes(cgm) {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004182 ObjCEmptyCacheVar = ObjCEmptyVtableVar = NULL;
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004183 ObjCABI = 2;
4184}
4185
Daniel Dunbarf77ac862008-08-11 21:35:06 +00004186/* *** */
4187
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004188ObjCCommonTypesHelper::ObjCCommonTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004189 : VMContext(cgm.getLLVMContext()), CGM(cgm) {
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004190 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4191 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004192
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004193 ShortTy = Types.ConvertType(Ctx.ShortTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004194 IntTy = Types.ConvertType(Ctx.IntTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004195 LongTy = Types.ConvertType(Ctx.LongTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00004196 LongLongTy = Types.ConvertType(Ctx.LongLongTy);
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004197 Int8PtrTy = llvm::Type::getInt8PtrTy(VMContext);
Bob Wilsondc8dab62011-11-30 01:57:58 +00004198 Int8PtrPtrTy = llvm::PointerType::getUnqual(Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004199
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004200 ObjectPtrTy = Types.ConvertType(Ctx.getObjCIdType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004201 PtrObjectPtrTy = llvm::PointerType::getUnqual(ObjectPtrTy);
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004202 SelectorPtrTy = Types.ConvertType(Ctx.getObjCSelType());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004203
Mike Stumpf5408fe2009-05-16 07:57:57 +00004204 // FIXME: It would be nice to unify this with the opaque type, so that the IR
4205 // comes out a bit cleaner.
Chris Lattner2acc6e32011-07-18 04:24:23 +00004206 llvm::Type *T = Types.ConvertType(Ctx.getObjCProtoType());
Owen Anderson96e0fc72009-07-29 22:16:19 +00004207 ExternalProtocolPtrTy = llvm::PointerType::getUnqual(T);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004208
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004209 // I'm not sure I like this. The implicit coordination is a bit
4210 // gross. We should solve this in a reasonable fashion because this
4211 // is a pretty common task (match some runtime data structure with
4212 // an LLVM data structure).
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004213
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004214 // FIXME: This is leaked.
4215 // FIXME: Merge with rewriter code?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004216
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004217 // struct _objc_super {
4218 // id self;
4219 // Class cls;
4220 // }
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004221 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004222 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004223 SourceLocation(), SourceLocation(),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004224 &Ctx.Idents.get("_objc_super"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004225 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004226 Ctx.getObjCIdType(), 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004227 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004228 Ctx.getObjCClassType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004229 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004230
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004231 SuperCTy = Ctx.getTagDeclType(RD);
4232 SuperPtrCTy = Ctx.getPointerType(SuperCTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004233
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004234 SuperTy = cast<llvm::StructType>(Types.ConvertType(SuperCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004235 SuperPtrTy = llvm::PointerType::getUnqual(SuperTy);
4236
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004237 // struct _prop_t {
4238 // char *name;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004239 // char *attributes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004240 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004241 PropertyTy = llvm::StructType::create("struct._prop_t",
4242 Int8PtrTy, Int8PtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004243
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004244 // struct _prop_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004245 // uint32_t entsize; // sizeof(struct _prop_t)
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004246 // uint32_t count_of_properties;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004247 // struct _prop_t prop_list[count_of_properties];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004248 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004249 PropertyListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004250 llvm::StructType::create("struct._prop_list_t", IntTy, IntTy,
4251 llvm::ArrayType::get(PropertyTy, 0), NULL);
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004252 // struct _prop_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004253 PropertyListPtrTy = llvm::PointerType::getUnqual(PropertyListTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004254
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004255 // struct _objc_method {
4256 // SEL _cmd;
4257 // char *method_type;
4258 // char *_imp;
4259 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004260 MethodTy = llvm::StructType::create("struct._objc_method",
4261 SelectorPtrTy, Int8PtrTy, Int8PtrTy,
4262 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004263
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004264 // struct _objc_cache *
Chris Lattnerc1c20112011-08-12 17:43:31 +00004265 CacheTy = llvm::StructType::create(VMContext, "struct._objc_cache");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004266 CachePtrTy = llvm::PointerType::getUnqual(CacheTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004267
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004268}
Daniel Dunbar4e2d7d02008-08-12 06:48:42 +00004269
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004270ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004271 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004272 // struct _objc_method_description {
4273 // SEL name;
4274 // char *types;
4275 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004276 MethodDescriptionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004277 llvm::StructType::create("struct._objc_method_description",
4278 SelectorPtrTy, Int8PtrTy, NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004279
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004280 // struct _objc_method_description_list {
4281 // int count;
4282 // struct _objc_method_description[1];
4283 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004284 MethodDescriptionListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004285 llvm::StructType::create("struct._objc_method_description_list",
4286 IntTy,
4287 llvm::ArrayType::get(MethodDescriptionTy, 0),NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004288
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004289 // struct _objc_method_description_list *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004290 MethodDescriptionListPtrTy =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004291 llvm::PointerType::getUnqual(MethodDescriptionListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004292
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004293 // Protocol description structures
4294
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004295 // struct _objc_protocol_extension {
4296 // uint32_t size; // sizeof(struct _objc_protocol_extension)
4297 // struct _objc_method_description_list *optional_instance_methods;
4298 // struct _objc_method_description_list *optional_class_methods;
4299 // struct _objc_property_list *instance_properties;
Bob Wilsondc8dab62011-11-30 01:57:58 +00004300 // const char ** extendedMethodTypes;
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004301 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004302 ProtocolExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004303 llvm::StructType::create("struct._objc_protocol_extension",
4304 IntTy, MethodDescriptionListPtrTy,
4305 MethodDescriptionListPtrTy, PropertyListPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004306 Int8PtrPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004307
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004308 // struct _objc_protocol_extension *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004309 ProtocolExtensionPtrTy = llvm::PointerType::getUnqual(ProtocolExtensionTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004310
Daniel Dunbar0c0e7a62008-10-29 22:36:39 +00004311 // Handle recursive construction of Protocol and ProtocolList types
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004312
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004313 ProtocolTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004314 llvm::StructType::create(VMContext, "struct._objc_protocol");
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004315
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004316 ProtocolListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004317 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004318 ProtocolListTy->setBody(llvm::PointerType::getUnqual(ProtocolListTy),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004319 LongTy,
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004320 llvm::ArrayType::get(ProtocolTy, 0),
Fariborz Jahanianee0af742009-01-21 22:04:16 +00004321 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004322
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004323 // struct _objc_protocol {
4324 // struct _objc_protocol_extension *isa;
4325 // char *protocol_name;
4326 // struct _objc_protocol **_objc_protocol_list;
4327 // struct _objc_method_description_list *instance_methods;
4328 // struct _objc_method_description_list *class_methods;
4329 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004330 ProtocolTy->setBody(ProtocolExtensionPtrTy, Int8PtrTy,
4331 llvm::PointerType::getUnqual(ProtocolListTy),
4332 MethodDescriptionListPtrTy,
4333 MethodDescriptionListPtrTy,
4334 NULL);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004335
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004336 // struct _objc_protocol_list *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004337 ProtocolListPtrTy = llvm::PointerType::getUnqual(ProtocolListTy);
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00004338
Owen Anderson96e0fc72009-07-29 22:16:19 +00004339 ProtocolPtrTy = llvm::PointerType::getUnqual(ProtocolTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004340
4341 // Class description structures
4342
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004343 // struct _objc_ivar {
4344 // char *ivar_name;
4345 // char *ivar_type;
4346 // int ivar_offset;
4347 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004348 IvarTy = llvm::StructType::create("struct._objc_ivar",
4349 Int8PtrTy, Int8PtrTy, IntTy, NULL);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004350
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004351 // struct _objc_ivar_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004352 IvarListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004353 llvm::StructType::create(VMContext, "struct._objc_ivar_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004354 IvarListPtrTy = llvm::PointerType::getUnqual(IvarListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004355
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004356 // struct _objc_method_list *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004357 MethodListTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004358 llvm::StructType::create(VMContext, "struct._objc_method_list");
Owen Anderson96e0fc72009-07-29 22:16:19 +00004359 MethodListPtrTy = llvm::PointerType::getUnqual(MethodListTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004360
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004361 // struct _objc_class_extension *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004362 ClassExtensionTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004363 llvm::StructType::create("struct._objc_class_extension",
4364 IntTy, Int8PtrTy, PropertyListPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004365 ClassExtensionPtrTy = llvm::PointerType::getUnqual(ClassExtensionTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004366
Chris Lattnerc1c20112011-08-12 17:43:31 +00004367 ClassTy = llvm::StructType::create(VMContext, "struct._objc_class");
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004368
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004369 // struct _objc_class {
4370 // Class isa;
4371 // Class super_class;
4372 // char *name;
4373 // long version;
4374 // long info;
4375 // long instance_size;
4376 // struct _objc_ivar_list *ivars;
4377 // struct _objc_method_list *methods;
4378 // struct _objc_cache *cache;
4379 // struct _objc_protocol_list *protocols;
4380 // char *ivar_layout;
4381 // struct _objc_class_ext *ext;
4382 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004383 ClassTy->setBody(llvm::PointerType::getUnqual(ClassTy),
4384 llvm::PointerType::getUnqual(ClassTy),
4385 Int8PtrTy,
4386 LongTy,
4387 LongTy,
4388 LongTy,
4389 IvarListPtrTy,
4390 MethodListPtrTy,
4391 CachePtrTy,
4392 ProtocolListPtrTy,
4393 Int8PtrTy,
4394 ClassExtensionPtrTy,
4395 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004396
Owen Anderson96e0fc72009-07-29 22:16:19 +00004397 ClassPtrTy = llvm::PointerType::getUnqual(ClassTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004398
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004399 // struct _objc_category {
4400 // char *category_name;
4401 // char *class_name;
4402 // struct _objc_method_list *instance_method;
4403 // struct _objc_method_list *class_method;
4404 // uint32_t size; // sizeof(struct _objc_category)
4405 // struct _objc_property_list *instance_properties;// category's @property
4406 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004407 CategoryTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004408 llvm::StructType::create("struct._objc_category",
4409 Int8PtrTy, Int8PtrTy, MethodListPtrTy,
4410 MethodListPtrTy, ProtocolListPtrTy,
4411 IntTy, PropertyListPtrTy, NULL);
Daniel Dunbar86e253a2008-08-22 20:34:54 +00004412
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004413 // Global metadata structures
4414
Fariborz Jahanian10a42312009-01-21 00:39:53 +00004415 // struct _objc_symtab {
4416 // long sel_ref_cnt;
4417 // SEL *refs;
4418 // short cls_def_cnt;
4419 // short cat_def_cnt;
4420 // char *defs[cls_def_cnt + cat_def_cnt];
4421 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004422 SymtabTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004423 llvm::StructType::create("struct._objc_symtab",
4424 LongTy, SelectorPtrTy, ShortTy, ShortTy,
4425 llvm::ArrayType::get(Int8PtrTy, 0), NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004426 SymtabPtrTy = llvm::PointerType::getUnqual(SymtabTy);
Daniel Dunbar27f9d772008-08-21 04:36:09 +00004427
Fariborz Jahaniandb286862009-01-22 00:37:21 +00004428 // struct _objc_module {
4429 // long version;
4430 // long size; // sizeof(struct _objc_module)
4431 // char *name;
4432 // struct _objc_symtab* symtab;
4433 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004434 ModuleTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004435 llvm::StructType::create("struct._objc_module",
4436 LongTy, LongTy, Int8PtrTy, SymtabPtrTy, NULL);
Daniel Dunbar14c80b72008-08-23 09:25:55 +00004437
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004438
Mike Stumpf5408fe2009-05-16 07:57:57 +00004439 // FIXME: This is the size of the setjmp buffer and should be target
4440 // specific. 18 is what's used on 32-bit X86.
Anders Carlsson124526b2008-09-09 10:10:21 +00004441 uint64_t SetJmpBufferSize = 18;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004442
Anders Carlsson124526b2008-09-09 10:10:21 +00004443 // Exceptions
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004444 llvm::Type *StackPtrTy = llvm::ArrayType::get(
Benjamin Kramer3c0ef8c2009-10-13 10:07:13 +00004445 llvm::Type::getInt8PtrTy(VMContext), 4);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004446
4447 ExceptionDataTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004448 llvm::StructType::create("struct._objc_exception_data",
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004449 llvm::ArrayType::get(llvm::Type::getInt32Ty(VMContext),
4450 SetJmpBufferSize),
4451 StackPtrTy, NULL);
Anders Carlsson124526b2008-09-09 10:10:21 +00004452
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004453}
4454
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004455ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)
Mike Stump1eb44332009-09-09 15:08:12 +00004456 : ObjCCommonTypesHelper(cgm) {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004457 // struct _method_list_t {
4458 // uint32_t entsize; // sizeof(struct _objc_method)
4459 // uint32_t method_count;
4460 // struct _objc_method method_list[method_count];
4461 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004462 MethodListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004463 llvm::StructType::create("struct.__method_list_t", IntTy, IntTy,
4464 llvm::ArrayType::get(MethodTy, 0), NULL);
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004465 // struct method_list_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004466 MethodListnfABIPtrTy = llvm::PointerType::getUnqual(MethodListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004467
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004468 // struct _protocol_t {
4469 // id isa; // NULL
4470 // const char * const protocol_name;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004471 // const struct _protocol_list_t * protocol_list; // super protocols
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004472 // const struct method_list_t * const instance_methods;
4473 // const struct method_list_t * const class_methods;
4474 // const struct method_list_t *optionalInstanceMethods;
4475 // const struct method_list_t *optionalClassMethods;
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004476 // const struct _prop_list_t * properties;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004477 // const uint32_t size; // sizeof(struct _protocol_t)
4478 // const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00004479 // const char ** extendedMethodTypes;
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004480 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004481
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004482 // Holder for struct _protocol_list_t *
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004483 ProtocolListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004484 llvm::StructType::create(VMContext, "struct._objc_protocol_list");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004485
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004486 ProtocolnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004487 llvm::StructType::create("struct._protocol_t", ObjectPtrTy, Int8PtrTy,
4488 llvm::PointerType::getUnqual(ProtocolListnfABITy),
4489 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
4490 MethodListnfABIPtrTy, MethodListnfABIPtrTy,
Bob Wilsondc8dab62011-11-30 01:57:58 +00004491 PropertyListPtrTy, IntTy, IntTy, Int8PtrPtrTy,
4492 NULL);
Daniel Dunbar948e2582009-02-15 07:36:20 +00004493
4494 // struct _protocol_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004495 ProtocolnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004496
Fariborz Jahanianda320092009-01-29 19:24:30 +00004497 // struct _protocol_list_t {
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004498 // long protocol_count; // Note, this is 32/64 bit
Daniel Dunbar948e2582009-02-15 07:36:20 +00004499 // struct _protocol_t *[protocol_count];
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004500 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004501 ProtocolListnfABITy->setBody(LongTy,
4502 llvm::ArrayType::get(ProtocolnfABIPtrTy, 0),
4503 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004504
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004505 // struct _objc_protocol_list*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004506 ProtocolListnfABIPtrTy = llvm::PointerType::getUnqual(ProtocolListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004507
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004508 // struct _ivar_t {
4509 // unsigned long int *offset; // pointer to ivar offset location
4510 // char *name;
4511 // char *type;
4512 // uint32_t alignment;
4513 // uint32_t size;
4514 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004515 IvarnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004516 llvm::StructType::create("struct._ivar_t",
4517 llvm::PointerType::getUnqual(LongTy),
4518 Int8PtrTy, Int8PtrTy, IntTy, IntTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004519
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004520 // struct _ivar_list_t {
4521 // uint32 entsize; // sizeof(struct _ivar_t)
4522 // uint32 count;
4523 // struct _iver_t list[count];
4524 // }
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004525 IvarListnfABITy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004526 llvm::StructType::create("struct._ivar_list_t", IntTy, IntTy,
4527 llvm::ArrayType::get(IvarnfABITy, 0), NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004528
Owen Anderson96e0fc72009-07-29 22:16:19 +00004529 IvarListnfABIPtrTy = llvm::PointerType::getUnqual(IvarListnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004530
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004531 // struct _class_ro_t {
Fariborz Jahanian30bc5712009-01-22 23:02:58 +00004532 // uint32_t const flags;
4533 // uint32_t const instanceStart;
4534 // uint32_t const instanceSize;
4535 // uint32_t const reserved; // only when building for 64bit targets
4536 // const uint8_t * const ivarLayout;
4537 // const char *const name;
4538 // const struct _method_list_t * const baseMethods;
4539 // const struct _objc_protocol_list *const baseProtocols;
4540 // const struct _ivar_list_t *const ivars;
4541 // const uint8_t * const weakIvarLayout;
4542 // const struct _prop_list_t * const properties;
4543 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004544
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004545 // FIXME. Add 'reserved' field in 64bit abi mode!
Chris Lattnerc1c20112011-08-12 17:43:31 +00004546 ClassRonfABITy = llvm::StructType::create("struct._class_ro_t",
4547 IntTy, IntTy, IntTy, Int8PtrTy,
4548 Int8PtrTy, MethodListnfABIPtrTy,
4549 ProtocolListnfABIPtrTy,
4550 IvarListnfABIPtrTy,
4551 Int8PtrTy, PropertyListPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004552
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004553 // ImpnfABITy - LLVM for id (*)(id, SEL, ...)
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004554 llvm::Type *params[] = { ObjectPtrTy, SelectorPtrTy };
John McCall0774cb82011-05-15 01:53:33 +00004555 ImpnfABITy = llvm::FunctionType::get(ObjectPtrTy, params, false)
4556 ->getPointerTo();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004557
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004558 // struct _class_t {
4559 // struct _class_t *isa;
4560 // struct _class_t * const superclass;
4561 // void *cache;
4562 // IMP *vtable;
4563 // struct class_ro_t *ro;
4564 // }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004565
Chris Lattnerc1c20112011-08-12 17:43:31 +00004566 ClassnfABITy = llvm::StructType::create(VMContext, "struct._class_t");
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004567 ClassnfABITy->setBody(llvm::PointerType::getUnqual(ClassnfABITy),
4568 llvm::PointerType::getUnqual(ClassnfABITy),
4569 CachePtrTy,
4570 llvm::PointerType::getUnqual(ImpnfABITy),
4571 llvm::PointerType::getUnqual(ClassRonfABITy),
4572 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004573
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004574 // LLVM for struct _class_t *
Owen Anderson96e0fc72009-07-29 22:16:19 +00004575 ClassnfABIPtrTy = llvm::PointerType::getUnqual(ClassnfABITy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004576
Fariborz Jahaniand55b6fc2009-01-23 01:46:23 +00004577 // struct _category_t {
4578 // const char * const name;
4579 // struct _class_t *const cls;
4580 // const struct _method_list_t * const instance_methods;
4581 // const struct _method_list_t * const class_methods;
4582 // const struct _protocol_list_t * const protocols;
4583 // const struct _prop_list_t * const properties;
Fariborz Jahanian45c2ba02009-01-23 17:41:22 +00004584 // }
Chris Lattnerc1c20112011-08-12 17:43:31 +00004585 CategorynfABITy = llvm::StructType::create("struct._category_t",
4586 Int8PtrTy, ClassnfABIPtrTy,
4587 MethodListnfABIPtrTy,
4588 MethodListnfABIPtrTy,
4589 ProtocolListnfABIPtrTy,
4590 PropertyListPtrTy,
4591 NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004592
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004593 // New types for nonfragile abi messaging.
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004594 CodeGen::CodeGenTypes &Types = CGM.getTypes();
4595 ASTContext &Ctx = CGM.getContext();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004596
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004597 // MessageRefTy - LLVM for:
4598 // struct _message_ref_t {
4599 // IMP messenger;
4600 // SEL name;
4601 // };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004602
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004603 // First the clang type for struct _message_ref_t
Abramo Bagnara465d41b2010-05-11 21:36:43 +00004604 RecordDecl *RD = RecordDecl::Create(Ctx, TTK_Struct,
Daniel Dunbardaa3ac52010-04-29 16:29:11 +00004605 Ctx.getTranslationUnitDecl(),
Abramo Bagnaraba877ad2011-03-09 14:09:51 +00004606 SourceLocation(), SourceLocation(),
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004607 &Ctx.Idents.get("_message_ref_t"));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004608 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004609 Ctx.VoidPtrTy, 0, 0, false, false));
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00004610 RD->addDecl(FieldDecl::Create(Ctx, RD, SourceLocation(), SourceLocation(), 0,
Richard Smith7a614d82011-06-11 17:19:42 +00004611 Ctx.getObjCSelType(), 0, 0, false, false));
Douglas Gregor838db382010-02-11 01:19:42 +00004612 RD->completeDefinition();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004613
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00004614 MessageRefCTy = Ctx.getTagDeclType(RD);
4615 MessageRefCPtrTy = Ctx.getPointerType(MessageRefCTy);
4616 MessageRefTy = cast<llvm::StructType>(Types.ConvertType(MessageRefCTy));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004617
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004618 // MessageRefPtrTy - LLVM for struct _message_ref_t*
Owen Anderson96e0fc72009-07-29 22:16:19 +00004619 MessageRefPtrTy = llvm::PointerType::getUnqual(MessageRefTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004620
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004621 // SuperMessageRefTy - LLVM for:
4622 // struct _super_message_ref_t {
4623 // SUPER_IMP messenger;
4624 // SEL name;
4625 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004626 SuperMessageRefTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004627 llvm::StructType::create("struct._super_message_ref_t",
4628 ImpnfABITy, SelectorPtrTy, NULL);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004629
Fariborz Jahanian2e4672b2009-02-03 23:49:23 +00004630 // SuperMessageRefPtrTy - LLVM for struct _super_message_ref_t*
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004631 SuperMessageRefPtrTy = llvm::PointerType::getUnqual(SuperMessageRefTy);
Fariborz Jahanian9d96bce2011-06-22 20:21:51 +00004632
Daniel Dunbare588b992009-03-01 04:46:24 +00004633
4634 // struct objc_typeinfo {
4635 // const void** vtable; // objc_ehtype_vtable + 2
4636 // const char* name; // c++ typeinfo string
4637 // Class cls;
4638 // };
Chris Lattner9cbe4f02011-07-09 17:41:47 +00004639 EHTypeTy =
Chris Lattnerc1c20112011-08-12 17:43:31 +00004640 llvm::StructType::create("struct._objc_typeinfo",
4641 llvm::PointerType::getUnqual(Int8PtrTy),
4642 Int8PtrTy, ClassnfABIPtrTy, NULL);
Owen Anderson96e0fc72009-07-29 22:16:19 +00004643 EHTypePtrTy = llvm::PointerType::getUnqual(EHTypeTy);
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00004644}
4645
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004646llvm::Function *CGObjCNonFragileABIMac::ModuleInitFunction() {
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004647 FinishNonFragileABIModule();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004648
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004649 return NULL;
4650}
4651
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004652void CGObjCNonFragileABIMac::AddModuleClassList(const
4653 std::vector<llvm::GlobalValue*>
4654 &Container,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004655 const char *SymbolName,
4656 const char *SectionName) {
4657 unsigned NumClasses = Container.size();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004658
Daniel Dunbar463b8762009-05-15 21:48:48 +00004659 if (!NumClasses)
4660 return;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004661
Daniel Dunbar463b8762009-05-15 21:48:48 +00004662 std::vector<llvm::Constant*> Symbols(NumClasses);
4663 for (unsigned i=0; i<NumClasses; i++)
Owen Anderson3c4972d2009-07-29 18:54:39 +00004664 Symbols[i] = llvm::ConstantExpr::getBitCast(Container[i],
Daniel Dunbar463b8762009-05-15 21:48:48 +00004665 ObjCTypes.Int8PtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004666 llvm::Constant* Init =
Owen Anderson96e0fc72009-07-29 22:16:19 +00004667 llvm::ConstantArray::get(llvm::ArrayType::get(ObjCTypes.Int8PtrTy,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004668 NumClasses),
4669 Symbols);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004670
Daniel Dunbar463b8762009-05-15 21:48:48 +00004671 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00004672 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004673 llvm::GlobalValue::InternalLinkage,
4674 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00004675 SymbolName);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004676 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Daniel Dunbar463b8762009-05-15 21:48:48 +00004677 GV->setSection(SectionName);
Chris Lattnerad64e022009-07-17 23:57:13 +00004678 CGM.AddUsedGlobal(GV);
Daniel Dunbar463b8762009-05-15 21:48:48 +00004679}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004680
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004681void CGObjCNonFragileABIMac::FinishNonFragileABIModule() {
4682 // nonfragile abi has no module definition.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004683
Daniel Dunbar463b8762009-05-15 21:48:48 +00004684 // Build list of all implemented class addresses in array
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004685 // L_OBJC_LABEL_CLASS_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004686 AddModuleClassList(DefinedClasses,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004687 "\01L_OBJC_LABEL_CLASS_$",
4688 "__DATA, __objc_classlist, regular, no_dead_strip");
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004689
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004690 for (unsigned i = 0; i < DefinedClasses.size(); i++) {
4691 llvm::GlobalValue *IMPLGV = DefinedClasses[i];
4692 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4693 continue;
4694 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004695 }
4696
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00004697 for (unsigned i = 0; i < DefinedMetaClasses.size(); i++) {
4698 llvm::GlobalValue *IMPLGV = DefinedMetaClasses[i];
4699 if (IMPLGV->getLinkage() != llvm::GlobalValue::ExternalWeakLinkage)
4700 continue;
4701 IMPLGV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4702 }
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00004703
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004704 AddModuleClassList(DefinedNonLazyClasses,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004705 "\01L_OBJC_LABEL_NONLAZY_CLASS_$",
4706 "__DATA, __objc_nlclslist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004707
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00004708 // Build list of all implemented category addresses in array
4709 // L_OBJC_LABEL_CATEGORY_$.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004710 AddModuleClassList(DefinedCategories,
Daniel Dunbar463b8762009-05-15 21:48:48 +00004711 "\01L_OBJC_LABEL_CATEGORY_$",
4712 "__DATA, __objc_catlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004713 AddModuleClassList(DefinedNonLazyCategories,
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004714 "\01L_OBJC_LABEL_NONLAZY_CATEGORY_$",
4715 "__DATA, __objc_nlcatlist, regular, no_dead_strip");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004716
Daniel Dunbarfce176b2010-04-25 20:39:01 +00004717 EmitImageInfo();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004718}
4719
John McCall944c8432011-05-14 03:10:52 +00004720/// isVTableDispatchedSelector - Returns true if SEL is not in the list of
4721/// VTableDispatchMethods; false otherwise. What this means is that
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004722/// except for the 19 selectors in the list, we generate 32bit-style
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004723/// message dispatch call for all the rest.
John McCall944c8432011-05-14 03:10:52 +00004724bool CGObjCNonFragileABIMac::isVTableDispatchedSelector(Selector Sel) {
4725 // At various points we've experimented with using vtable-based
4726 // dispatch for all methods.
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004727 switch (CGM.getCodeGenOpts().getObjCDispatchMethod()) {
4728 default:
John McCall944c8432011-05-14 03:10:52 +00004729 llvm_unreachable("Invalid dispatch method!");
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004730 case CodeGenOptions::Legacy:
Fariborz Jahanian776dbf92010-04-19 17:53:30 +00004731 return false;
John McCall944c8432011-05-14 03:10:52 +00004732 case CodeGenOptions::NonLegacy:
4733 return true;
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004734 case CodeGenOptions::Mixed:
4735 break;
4736 }
4737
4738 // If so, see whether this selector is in the white-list of things which must
4739 // use the new dispatch convention. We lazily build a dense set for this.
John McCall944c8432011-05-14 03:10:52 +00004740 if (VTableDispatchMethods.empty()) {
4741 VTableDispatchMethods.insert(GetNullarySelector("alloc"));
4742 VTableDispatchMethods.insert(GetNullarySelector("class"));
4743 VTableDispatchMethods.insert(GetNullarySelector("self"));
4744 VTableDispatchMethods.insert(GetNullarySelector("isFlipped"));
4745 VTableDispatchMethods.insert(GetNullarySelector("length"));
4746 VTableDispatchMethods.insert(GetNullarySelector("count"));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004747
John McCall944c8432011-05-14 03:10:52 +00004748 // These are vtable-based if GC is disabled.
4749 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004750 if (CGM.getLangOptions().getGC() != LangOptions::GCOnly) {
John McCall944c8432011-05-14 03:10:52 +00004751 VTableDispatchMethods.insert(GetNullarySelector("retain"));
4752 VTableDispatchMethods.insert(GetNullarySelector("release"));
4753 VTableDispatchMethods.insert(GetNullarySelector("autorelease"));
4754 }
4755
4756 VTableDispatchMethods.insert(GetUnarySelector("allocWithZone"));
4757 VTableDispatchMethods.insert(GetUnarySelector("isKindOfClass"));
4758 VTableDispatchMethods.insert(GetUnarySelector("respondsToSelector"));
4759 VTableDispatchMethods.insert(GetUnarySelector("objectForKey"));
4760 VTableDispatchMethods.insert(GetUnarySelector("objectAtIndex"));
4761 VTableDispatchMethods.insert(GetUnarySelector("isEqualToString"));
4762 VTableDispatchMethods.insert(GetUnarySelector("isEqual"));
4763
4764 // These are vtable-based if GC is enabled.
4765 // Optimistically use vtable dispatch for hybrid compiles.
Douglas Gregore289d812011-09-13 17:21:33 +00004766 if (CGM.getLangOptions().getGC() != LangOptions::NonGC) {
John McCall944c8432011-05-14 03:10:52 +00004767 VTableDispatchMethods.insert(GetNullarySelector("hash"));
4768 VTableDispatchMethods.insert(GetUnarySelector("addObject"));
4769
4770 // "countByEnumeratingWithState:objects:count"
4771 IdentifierInfo *KeyIdents[] = {
4772 &CGM.getContext().Idents.get("countByEnumeratingWithState"),
4773 &CGM.getContext().Idents.get("objects"),
4774 &CGM.getContext().Idents.get("count")
4775 };
4776 VTableDispatchMethods.insert(
4777 CGM.getContext().Selectors.getSelector(3, KeyIdents));
4778 }
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004779 }
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00004780
John McCall944c8432011-05-14 03:10:52 +00004781 return VTableDispatchMethods.count(Sel);
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00004782}
4783
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004784// Metadata flags
4785enum MetaDataDlags {
4786 CLS = 0x0,
4787 CLS_META = 0x1,
4788 CLS_ROOT = 0x2,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004789 OBJC2_CLS_HIDDEN = 0x10,
John McCallf85e1932011-06-15 23:02:42 +00004790 CLS_EXCEPTION = 0x20,
4791
4792 /// (Obsolete) ARC-specific: this class has a .release_ivars method
4793 CLS_HAS_IVAR_RELEASER = 0x40,
4794 /// class was compiled with -fobjc-arr
4795 CLS_COMPILED_BY_ARC = 0x80 // (1<<7)
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004796};
4797/// BuildClassRoTInitializer - generate meta-data for:
4798/// struct _class_ro_t {
4799/// uint32_t const flags;
4800/// uint32_t const instanceStart;
4801/// uint32_t const instanceSize;
4802/// uint32_t const reserved; // only when building for 64bit targets
4803/// const uint8_t * const ivarLayout;
4804/// const char *const name;
4805/// const struct _method_list_t * const baseMethods;
Fariborz Jahanianda320092009-01-29 19:24:30 +00004806/// const struct _protocol_list_t *const baseProtocols;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004807/// const struct _ivar_list_t *const ivars;
4808/// const uint8_t * const weakIvarLayout;
4809/// const struct _prop_list_t * const properties;
4810/// }
4811///
4812llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassRoTInitializer(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004813 unsigned flags,
4814 unsigned InstanceStart,
4815 unsigned InstanceSize,
4816 const ObjCImplementationDecl *ID) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004817 std::string ClassName = ID->getNameAsString();
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004818 llvm::Constant *Values[10]; // 11 for 64bit targets!
John McCallf85e1932011-06-15 23:02:42 +00004819
4820 if (CGM.getLangOptions().ObjCAutoRefCount)
4821 flags |= CLS_COMPILED_BY_ARC;
4822
Owen Anderson4a28d5d2009-07-24 23:12:58 +00004823 Values[ 0] = llvm::ConstantInt::get(ObjCTypes.IntTy, flags);
4824 Values[ 1] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceStart);
4825 Values[ 2] = llvm::ConstantInt::get(ObjCTypes.IntTy, InstanceSize);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004826 // FIXME. For 64bit targets add 0 here.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004827 Values[ 3] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4828 : BuildIvarLayout(ID, true);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004829 Values[ 4] = GetClassName(ID->getIdentifier());
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004830 // const struct _method_list_t * const baseMethods;
4831 std::vector<llvm::Constant*> Methods;
4832 std::string MethodListName("\01l_OBJC_$_");
4833 if (flags & CLS_META) {
4834 MethodListName += "CLASS_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004835 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004836 i = ID->classmeth_begin(), e = ID->classmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004837 // Class methods should always be defined.
4838 Methods.push_back(GetMethodConstant(*i));
4839 }
4840 } else {
4841 MethodListName += "INSTANCE_METHODS_" + ID->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004842 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004843 i = ID->instmeth_begin(), e = ID->instmeth_end(); i != e; ++i) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004844 // Instance methods should always be defined.
4845 Methods.push_back(GetMethodConstant(*i));
4846 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004847 for (ObjCImplementationDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004848 i = ID->propimpl_begin(), e = ID->propimpl_end(); i != e; ++i) {
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004849 ObjCPropertyImplDecl *PID = *i;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004850
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004851 if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize){
4852 ObjCPropertyDecl *PD = PID->getPropertyDecl();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004853
Fariborz Jahanian939abce2009-01-28 22:46:49 +00004854 if (ObjCMethodDecl *MD = PD->getGetterMethodDecl())
4855 if (llvm::Constant *C = GetMethodConstant(MD))
4856 Methods.push_back(C);
4857 if (ObjCMethodDecl *MD = PD->getSetterMethodDecl())
4858 if (llvm::Constant *C = GetMethodConstant(MD))
4859 Methods.push_back(C);
4860 }
4861 }
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004862 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004863 Values[ 5] = EmitMethodList(MethodListName,
4864 "__DATA, __objc_const", Methods);
4865
Fariborz Jahanianda320092009-01-29 19:24:30 +00004866 const ObjCInterfaceDecl *OID = ID->getClassInterface();
4867 assert(OID && "CGObjCNonFragileABIMac::BuildClassRoTInitializer");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004868 Values[ 6] = EmitProtocolList("\01l_OBJC_CLASS_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004869 + OID->getName(),
Ted Kremenek53b94412010-09-01 01:21:15 +00004870 OID->all_referenced_protocol_begin(),
4871 OID->all_referenced_protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004872
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004873 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004874 Values[ 7] = llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00004875 else
4876 Values[ 7] = EmitIvarList(ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004877 Values[ 8] = (flags & CLS_META) ? GetIvarLayoutName(0, ObjCTypes)
4878 : BuildIvarLayout(ID, false);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004879 if (flags & CLS_META)
Owen Andersonc9c88b42009-07-31 20:28:54 +00004880 Values[ 9] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00004881 else
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00004882 Values[ 9] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ID->getName(),
4883 ID, ID->getClassInterface(), ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00004884 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassRonfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004885 Values);
4886 llvm::GlobalVariable *CLASS_RO_GV =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004887 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassRonfABITy, false,
4888 llvm::GlobalValue::InternalLinkage,
4889 Init,
4890 (flags & CLS_META) ?
4891 std::string("\01l_OBJC_METACLASS_RO_$_")+ClassName :
4892 std::string("\01l_OBJC_CLASS_RO_$_")+ClassName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004893 CLASS_RO_GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004894 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassRonfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00004895 CLASS_RO_GV->setSection("__DATA, __objc_const");
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004896 return CLASS_RO_GV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00004897
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004898}
4899
4900/// BuildClassMetaData - This routine defines that to-level meta-data
4901/// for the given ClassName for:
4902/// struct _class_t {
4903/// struct _class_t *isa;
4904/// struct _class_t * const superclass;
4905/// void *cache;
4906/// IMP *vtable;
4907/// struct class_ro_t *ro;
4908/// }
4909///
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004910llvm::GlobalVariable * CGObjCNonFragileABIMac::BuildClassMetaData(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004911 std::string &ClassName,
4912 llvm::Constant *IsAGV,
4913 llvm::Constant *SuperClassGV,
4914 llvm::Constant *ClassRoGV,
4915 bool HiddenVisibility) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00004916 llvm::Constant *Values[] = {
4917 IsAGV,
4918 SuperClassGV,
4919 ObjCEmptyCacheVar, // &ObjCEmptyCacheVar
4920 ObjCEmptyVtableVar, // &ObjCEmptyVtableVar
4921 ClassRoGV // &CLASS_RO_GV
4922 };
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004923 if (!Values[1])
4924 Values[1] = llvm::Constant::getNullValue(ObjCTypes.ClassnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004925 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ClassnfABITy,
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004926 Values);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004927 llvm::GlobalVariable *GV = GetClassGlobal(ClassName);
4928 GV->setInitializer(Init);
Fariborz Jahaniandd0db2a2009-01-31 01:07:39 +00004929 GV->setSection("__DATA, __objc_data");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00004930 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00004931 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ClassnfABITy));
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004932 if (HiddenVisibility)
4933 GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00004934 return GV;
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004935}
4936
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004937bool
Fariborz Jahanianecfbdcb2009-05-21 01:03:45 +00004938CGObjCNonFragileABIMac::ImplementationIsNonLazy(const ObjCImplDecl *OD) const {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00004939 return OD->getClassMethod(GetNullarySelector("load")) != 0;
Daniel Dunbar74d4b122009-05-15 22:33:15 +00004940}
4941
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00004942void CGObjCNonFragileABIMac::GetClassSizeInfo(const ObjCImplementationDecl *OID,
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004943 uint32_t &InstanceStart,
4944 uint32_t &InstanceSize) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004945 const ASTRecordLayout &RL =
Daniel Dunbarb4c79e02009-05-04 21:26:30 +00004946 CGM.getContext().getASTObjCImplementationLayout(OID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004947
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004948 // InstanceSize is really instance end.
Ken Dyckec299032011-02-11 02:20:09 +00004949 InstanceSize = RL.getDataSize().getQuantity();
Daniel Dunbar6e8575b2009-05-04 23:23:09 +00004950
4951 // If there are no fields, the start is the same as the end.
4952 if (!RL.getFieldCount())
4953 InstanceStart = InstanceSize;
4954 else
Ken Dyckfb67ccd2011-04-14 00:43:09 +00004955 InstanceStart = RL.getFieldOffset(0) / CGM.getContext().getCharWidth();
Daniel Dunbarb02532a2009-04-19 23:41:48 +00004956}
4957
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004958void CGObjCNonFragileABIMac::GenerateClass(const ObjCImplementationDecl *ID) {
4959 std::string ClassName = ID->getNameAsString();
4960 if (!ObjCEmptyCacheVar) {
4961 ObjCEmptyCacheVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004962 CGM.getModule(),
4963 ObjCTypes.CacheTy,
4964 false,
4965 llvm::GlobalValue::ExternalLinkage,
4966 0,
4967 "_objc_empty_cache");
4968
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004969 ObjCEmptyVtableVar = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004970 CGM.getModule(),
4971 ObjCTypes.ImpnfABITy,
4972 false,
4973 llvm::GlobalValue::ExternalLinkage,
4974 0,
4975 "_objc_empty_vtable");
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00004976 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004977 assert(ID->getClassInterface() &&
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004978 "CGObjCNonFragileABIMac::GenerateClass - class is 0");
Daniel Dunbar6c1aac82009-04-20 20:18:54 +00004979 // FIXME: Is this correct (that meta class size is never computed)?
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004980 uint32_t InstanceStart =
Duncan Sands9408c452009-05-09 07:08:47 +00004981 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ClassnfABITy);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004982 uint32_t InstanceSize = InstanceStart;
4983 uint32_t flags = CLS_META;
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00004984 std::string ObjCMetaClassName(getMetaclassSymbolPrefix());
4985 std::string ObjCClassName(getClassSymbolPrefix());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004986
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004987 llvm::GlobalVariable *SuperClassGV, *IsAGV;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00004988
4989 bool classIsHidden =
John McCall1fb0caa2010-10-22 21:05:15 +00004990 ID->getClassInterface()->getVisibility() == HiddenVisibility;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00004991 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004992 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00004993 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00004994 flags |= eClassFlags_ABI2_HasCXXStructors;
Fariborz Jahanian493dab72009-01-26 21:38:32 +00004995 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00004996 // class is root
4997 flags |= CLS_ROOT;
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00004998 SuperClassGV = GetClassGlobal(ObjCClassName + ClassName);
Fariborz Jahanian0f902942009-04-14 18:41:56 +00004999 IsAGV = GetClassGlobal(ObjCMetaClassName + ClassName);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005000 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005001 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005002 const ObjCInterfaceDecl *Root = ID->getClassInterface();
5003 while (const ObjCInterfaceDecl *Super = Root->getSuperClass())
5004 Root = Super;
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005005 IsAGV = GetClassGlobal(ObjCMetaClassName + Root->getNameAsString());
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005006 if (Root->isWeakImported())
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005007 IsAGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005008 // work on super class metadata symbol.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005009 std::string SuperClassName =
Fariborz Jahanian0e93d252009-12-01 18:25:24 +00005010 ObjCMetaClassName +
5011 ID->getClassInterface()->getSuperClass()->getNameAsString();
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005012 SuperClassGV = GetClassGlobal(SuperClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005013 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005014 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian058a1b72009-01-24 20:21:50 +00005015 }
5016 llvm::GlobalVariable *CLASS_RO_GV = BuildClassRoTInitializer(flags,
5017 InstanceStart,
5018 InstanceSize,ID);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005019 std::string TClassName = ObjCMetaClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005020 llvm::GlobalVariable *MetaTClass =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005021 BuildClassMetaData(TClassName, IsAGV, SuperClassGV, CLASS_RO_GV,
5022 classIsHidden);
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005023 DefinedMetaClasses.push_back(MetaTClass);
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005024
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005025 // Metadata for the class
5026 flags = CLS;
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005027 if (classIsHidden)
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005028 flags |= OBJC2_CLS_HIDDEN;
John McCallf85e1932011-06-15 23:02:42 +00005029 if (ID->hasCXXStructors())
Fariborz Jahanian109dfc62010-04-28 21:28:56 +00005030 flags |= eClassFlags_ABI2_HasCXXStructors;
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005031
Douglas Gregor68584ed2009-06-18 16:11:24 +00005032 if (hasObjCExceptionAttribute(CGM.getContext(), ID->getClassInterface()))
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005033 flags |= CLS_EXCEPTION;
5034
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005035 if (!ID->getClassInterface()->getSuperClass()) {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005036 flags |= CLS_ROOT;
5037 SuperClassGV = 0;
Chris Lattnerb7b58b12009-04-19 06:02:28 +00005038 } else {
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005039 // Has a root. Current class is not a root.
Fariborz Jahanianfab98c42009-02-26 18:23:47 +00005040 std::string RootClassName =
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005041 ID->getClassInterface()->getSuperClass()->getNameAsString();
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005042 SuperClassGV = GetClassGlobal(ObjCClassName + RootClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005043 if (ID->getClassInterface()->getSuperClass()->isWeakImported())
Fariborz Jahaniana03d0dd2009-11-17 21:37:35 +00005044 SuperClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005045 }
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005046 GetClassSizeInfo(ID, InstanceStart, InstanceSize);
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005047 CLASS_RO_GV = BuildClassRoTInitializer(flags,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005048 InstanceStart,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005049 InstanceSize,
Fariborz Jahanianf6a077e2009-01-24 23:43:01 +00005050 ID);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005051
Fariborz Jahanian84394a52009-01-24 21:21:53 +00005052 TClassName = ObjCClassName + ClassName;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005053 llvm::GlobalVariable *ClassMD =
Fariborz Jahaniancf555162009-01-31 00:59:10 +00005054 BuildClassMetaData(TClassName, MetaTClass, SuperClassGV, CLASS_RO_GV,
5055 classIsHidden);
Fariborz Jahanianf87a0cc2009-01-30 20:55:31 +00005056 DefinedClasses.push_back(ClassMD);
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005057
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005058 // Determine if this class is also "non-lazy".
5059 if (ImplementationIsNonLazy(ID))
5060 DefinedNonLazyClasses.push_back(ClassMD);
5061
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00005062 // Force the definition of the EHType if necessary.
5063 if (flags & CLS_EXCEPTION)
5064 GetInterfaceEHType(ID->getClassInterface(), true);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005065 // Make sure method definition entries are all clear for next implementation.
5066 MethodDefinitions.clear();
Fariborz Jahanianaa23b572009-01-23 23:53:38 +00005067}
5068
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005069/// GenerateProtocolRef - This routine is called to generate code for
5070/// a protocol reference expression; as in:
5071/// @code
5072/// @protocol(Proto1);
5073/// @endcode
5074/// It generates a weak reference to l_OBJC_PROTOCOL_REFERENCE_$_Proto1
5075/// which will hold address of the protocol meta-data.
5076///
5077llvm::Value *CGObjCNonFragileABIMac::GenerateProtocolRef(CGBuilderTy &Builder,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005078 const ObjCProtocolDecl *PD) {
5079
Fariborz Jahanian960cd062009-04-10 18:47:34 +00005080 // This routine is called for @protocol only. So, we must build definition
5081 // of protocol's meta-data (not a reference to it!)
5082 //
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005083 llvm::Constant *Init =
5084 llvm::ConstantExpr::getBitCast(GetOrEmitProtocol(PD),
5085 ObjCTypes.ExternalProtocolPtrTy);
5086
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005087 std::string ProtocolName("\01l_OBJC_PROTOCOL_REFERENCE_$_");
Daniel Dunbar4087f272010-08-17 22:39:59 +00005088 ProtocolName += PD->getName();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005089
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005090 llvm::GlobalVariable *PTGV = CGM.getModule().getGlobalVariable(ProtocolName);
5091 if (PTGV)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005092 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005093 PTGV = new llvm::GlobalVariable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005094 CGM.getModule(),
5095 Init->getType(), false,
5096 llvm::GlobalValue::WeakAnyLinkage,
5097 Init,
5098 ProtocolName);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005099 PTGV->setSection("__DATA, __objc_protorefs, coalesced, no_dead_strip");
5100 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005101 CGM.AddUsedGlobal(PTGV);
Benjamin Kramer578faa82011-09-27 21:06:10 +00005102 return Builder.CreateLoad(PTGV);
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005103}
5104
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005105/// GenerateCategory - Build metadata for a category implementation.
5106/// struct _category_t {
5107/// const char * const name;
5108/// struct _class_t *const cls;
5109/// const struct _method_list_t * const instance_methods;
5110/// const struct _method_list_t * const class_methods;
5111/// const struct _protocol_list_t * const protocols;
5112/// const struct _prop_list_t * const properties;
5113/// }
5114///
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005115void CGObjCNonFragileABIMac::GenerateCategory(const ObjCCategoryImplDecl *OCD) {
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005116 const ObjCInterfaceDecl *Interface = OCD->getClassInterface();
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005117 const char *Prefix = "\01l_OBJC_$_CATEGORY_";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005118 std::string ExtCatName(Prefix + Interface->getNameAsString()+
5119 "_$_" + OCD->getNameAsString());
5120 std::string ExtClassName(getClassSymbolPrefix() +
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005121 Interface->getNameAsString());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005122
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005123 llvm::Constant *Values[6];
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005124 Values[0] = GetClassName(OCD->getIdentifier());
5125 // meta-class entry symbol
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005126 llvm::GlobalVariable *ClassGV = GetClassGlobal(ExtClassName);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005127 if (Interface->isWeakImported())
Fariborz Jahanian2cdcc4c2009-11-17 22:02:21 +00005128 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5129
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005130 Values[1] = ClassGV;
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005131 std::vector<llvm::Constant*> Methods;
5132 std::string MethodListName(Prefix);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005133 MethodListName += "INSTANCE_METHODS_" + Interface->getNameAsString() +
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005134 "_$_" + OCD->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005135
5136 for (ObjCCategoryImplDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005137 i = OCD->instmeth_begin(), e = OCD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005138 // Instance methods should always be defined.
5139 Methods.push_back(GetMethodConstant(*i));
5140 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005141
5142 Values[2] = EmitMethodList(MethodListName,
5143 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005144 Methods);
5145
5146 MethodListName = Prefix;
5147 MethodListName += "CLASS_METHODS_" + Interface->getNameAsString() + "_$_" +
5148 OCD->getNameAsString();
5149 Methods.clear();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005150 for (ObjCCategoryImplDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005151 i = OCD->classmeth_begin(), e = OCD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005152 // Class methods should always be defined.
5153 Methods.push_back(GetMethodConstant(*i));
5154 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005155
5156 Values[3] = EmitMethodList(MethodListName,
5157 "__DATA, __objc_const",
Fariborz Jahanianf6317dd2009-01-26 22:58:07 +00005158 Methods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005159 const ObjCCategoryDecl *Category =
Fariborz Jahanian5de14dc2009-01-28 22:18:42 +00005160 Interface->FindCategoryDeclaration(OCD->getIdentifier());
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005161 if (Category) {
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005162 llvm::SmallString<256> ExtName;
5163 llvm::raw_svector_ostream(ExtName) << Interface->getName() << "_$_"
5164 << OCD->getName();
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005165 Values[4] = EmitProtocolList("\01l_OBJC_CATEGORY_PROTOCOLS_$_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005166 + Interface->getName() + "_$_"
5167 + Category->getName(),
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005168 Category->protocol_begin(),
5169 Category->protocol_end());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005170 Values[5] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + ExtName.str(),
5171 OCD, Category, ObjCTypes);
Mike Stumpb3589f42009-07-30 22:28:39 +00005172 } else {
Owen Andersonc9c88b42009-07-31 20:28:54 +00005173 Values[4] = llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
5174 Values[5] = llvm::Constant::getNullValue(ObjCTypes.PropertyListPtrTy);
Fariborz Jahanian943ed6f2009-02-13 17:52:22 +00005175 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005176
5177 llvm::Constant *Init =
5178 llvm::ConstantStruct::get(ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005179 Values);
5180 llvm::GlobalVariable *GCATV
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005181 = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.CategorynfABITy,
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005182 false,
5183 llvm::GlobalValue::InternalLinkage,
5184 Init,
Owen Anderson1c431b32009-07-08 19:05:04 +00005185 ExtCatName);
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005186 GCATV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005187 CGM.getTargetData().getABITypeAlignment(ObjCTypes.CategorynfABITy));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005188 GCATV->setSection("__DATA, __objc_const");
Chris Lattnerad64e022009-07-17 23:57:13 +00005189 CGM.AddUsedGlobal(GCATV);
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005190 DefinedCategories.push_back(GCATV);
Daniel Dunbar74d4b122009-05-15 22:33:15 +00005191
5192 // Determine if this category is also "non-lazy".
5193 if (ImplementationIsNonLazy(OCD))
5194 DefinedNonLazyCategories.push_back(GCATV);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00005195 // method definition entries must be clear for next implementation.
5196 MethodDefinitions.clear();
Fariborz Jahanianeb062d92009-01-26 18:32:24 +00005197}
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005198
5199/// GetMethodConstant - Return a struct objc_method constant for the
5200/// given method if it has been defined. The result is null if the
5201/// method has not been defined. The return value has type MethodPtrTy.
5202llvm::Constant *CGObjCNonFragileABIMac::GetMethodConstant(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005203 const ObjCMethodDecl *MD) {
Argyrios Kyrtzidis9d50c062010-08-09 10:54:20 +00005204 llvm::Function *Fn = GetMethodDefinition(MD);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005205 if (!Fn)
5206 return 0;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005207
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005208 llvm::Constant *Method[] = {
Owen Anderson3c4972d2009-07-29 18:54:39 +00005209 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005210 ObjCTypes.SelectorPtrTy),
5211 GetMethodVarType(MD),
5212 llvm::ConstantExpr::getBitCast(Fn, ObjCTypes.Int8PtrTy)
5213 };
Owen Anderson08e25242009-07-27 22:29:56 +00005214 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Method);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005215}
5216
5217/// EmitMethodList - Build meta-data for method declarations
5218/// struct _method_list_t {
5219/// uint32_t entsize; // sizeof(struct _objc_method)
5220/// uint32_t method_count;
5221/// struct _objc_method method_list[method_count];
5222/// }
5223///
Chris Lattner5f9e2722011-07-23 10:55:15 +00005224llvm::Constant *CGObjCNonFragileABIMac::EmitMethodList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005225 const char *Section,
5226 const ConstantVector &Methods) {
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005227 // Return null for empty list.
5228 if (Methods.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005229 return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005230
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005231 llvm::Constant *Values[3];
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005232 // sizeof(struct _objc_method)
Duncan Sands9408c452009-05-09 07:08:47 +00005233 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.MethodTy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005234 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005235 // method_count
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005236 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Methods.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005237 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.MethodTy,
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005238 Methods.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005239 Values[2] = llvm::ConstantArray::get(AT, Methods);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005240 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005241
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005242 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005243 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005244 llvm::GlobalValue::InternalLinkage, Init, Name);
5245 GV->setAlignment(CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005246 GV->setSection(Section);
Chris Lattnerad64e022009-07-17 23:57:13 +00005247 CGM.AddUsedGlobal(GV);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005248 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListnfABIPtrTy);
Fariborz Jahanian493dab72009-01-26 21:38:32 +00005249}
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005250
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005251/// ObjCIvarOffsetVariable - Returns the ivar offset variable for
5252/// the given ivar.
Daniel Dunbare83be122010-04-02 21:14:02 +00005253llvm::GlobalVariable *
5254CGObjCNonFragileABIMac::ObjCIvarOffsetVariable(const ObjCInterfaceDecl *ID,
5255 const ObjCIvarDecl *Ivar) {
5256 const ObjCInterfaceDecl *Container = Ivar->getContainingInterface();
Daniel Dunbara81419d2009-05-05 00:36:57 +00005257 std::string Name = "OBJC_IVAR_$_" + Container->getNameAsString() +
Douglas Gregor6ab35242009-04-09 21:40:53 +00005258 '.' + Ivar->getNameAsString();
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005259 llvm::GlobalVariable *IvarOffsetGV =
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005260 CGM.getModule().getGlobalVariable(Name);
5261 if (!IvarOffsetGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005262 IvarOffsetGV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005263 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.LongTy,
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005264 false,
5265 llvm::GlobalValue::ExternalLinkage,
5266 0,
Owen Anderson1c431b32009-07-08 19:05:04 +00005267 Name);
Fariborz Jahanianed157d32009-02-10 20:21:06 +00005268 return IvarOffsetGV;
5269}
5270
Daniel Dunbare83be122010-04-02 21:14:02 +00005271llvm::Constant *
5272CGObjCNonFragileABIMac::EmitIvarOffsetVar(const ObjCInterfaceDecl *ID,
5273 const ObjCIvarDecl *Ivar,
5274 unsigned long int Offset) {
Daniel Dunbar737c5022009-04-19 00:44:02 +00005275 llvm::GlobalVariable *IvarOffsetGV = ObjCIvarOffsetVariable(ID, Ivar);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005276 IvarOffsetGV->setInitializer(llvm::ConstantInt::get(ObjCTypes.LongTy,
Daniel Dunbar737c5022009-04-19 00:44:02 +00005277 Offset));
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005278 IvarOffsetGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005279 CGM.getTargetData().getABITypeAlignment(ObjCTypes.LongTy));
Daniel Dunbar737c5022009-04-19 00:44:02 +00005280
Mike Stumpf5408fe2009-05-16 07:57:57 +00005281 // FIXME: This matches gcc, but shouldn't the visibility be set on the use as
5282 // well (i.e., in ObjCIvarOffsetVariable).
Daniel Dunbar737c5022009-04-19 00:44:02 +00005283 if (Ivar->getAccessControl() == ObjCIvarDecl::Private ||
5284 Ivar->getAccessControl() == ObjCIvarDecl::Package ||
John McCall1fb0caa2010-10-22 21:05:15 +00005285 ID->getVisibility() == HiddenVisibility)
Fariborz Jahanian2fa5a272009-01-28 01:36:42 +00005286 IvarOffsetGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar04d40782009-04-14 06:00:08 +00005287 else
Fariborz Jahanian77c9fd22009-04-06 18:30:00 +00005288 IvarOffsetGV->setVisibility(llvm::GlobalValue::DefaultVisibility);
Bill Wendling1f382512011-05-04 21:37:25 +00005289 IvarOffsetGV->setSection("__DATA, __objc_ivar");
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005290 return IvarOffsetGV;
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005291}
5292
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005293/// EmitIvarList - Emit the ivar list for the given
Daniel Dunbar11394522009-04-18 08:51:00 +00005294/// implementation. The return value has type
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005295/// IvarListnfABIPtrTy.
5296/// struct _ivar_t {
5297/// unsigned long int *offset; // pointer to ivar offset location
5298/// char *name;
5299/// char *type;
5300/// uint32_t alignment;
5301/// uint32_t size;
5302/// }
5303/// struct _ivar_list_t {
5304/// uint32 entsize; // sizeof(struct _ivar_t)
5305/// uint32 count;
5306/// struct _iver_t list[count];
5307/// }
5308///
Daniel Dunbar3e5f0d82009-04-20 06:54:31 +00005309
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005310llvm::Constant *CGObjCNonFragileABIMac::EmitIvarList(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005311 const ObjCImplementationDecl *ID) {
5312
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005313 std::vector<llvm::Constant*> Ivars;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005314
Jordy Rosedb8264e2011-07-22 02:08:32 +00005315 const ObjCInterfaceDecl *OID = ID->getClassInterface();
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005316 assert(OID && "CGObjCNonFragileABIMac::EmitIvarList - null interface");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005317
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005318 // FIXME. Consolidate this with similar code in GenerateClass.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005319
Jordy Rosedb8264e2011-07-22 02:08:32 +00005320 for (const ObjCIvarDecl *IVD = OID->all_declared_ivar_begin();
Fariborz Jahanianbf9eb882011-06-28 18:05:25 +00005321 IVD; IVD = IVD->getNextIvar()) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +00005322 // Ignore unnamed bit-fields.
5323 if (!IVD->getDeclName())
5324 continue;
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005325 llvm::Constant *Ivar[5];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005326 Ivar[0] = EmitIvarOffsetVar(ID->getClassInterface(), IVD,
Daniel Dunbar9f89f2b2009-05-03 12:57:56 +00005327 ComputeIvarBaseOffset(CGM, ID, IVD));
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005328 Ivar[1] = GetMethodVarName(IVD->getIdentifier());
5329 Ivar[2] = GetMethodVarType(IVD);
Chris Lattner2acc6e32011-07-18 04:24:23 +00005330 llvm::Type *FieldTy =
Daniel Dunbar3fea0c02009-04-22 08:22:17 +00005331 CGM.getTypes().ConvertTypeForMem(IVD->getType());
Duncan Sands9408c452009-05-09 07:08:47 +00005332 unsigned Size = CGM.getTargetData().getTypeAllocSize(FieldTy);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005333 unsigned Align = CGM.getContext().getPreferredTypeAlign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005334 IVD->getType().getTypePtr()) >> 3;
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005335 Align = llvm::Log2_32(Align);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005336 Ivar[3] = llvm::ConstantInt::get(ObjCTypes.IntTy, Align);
Daniel Dunbar91636d62009-04-20 00:33:43 +00005337 // NOTE. Size of a bitfield does not match gcc's, because of the
5338 // way bitfields are treated special in each. But I am told that
5339 // 'size' for bitfield ivars is ignored by the runtime so it does
5340 // not matter. If it matters, there is enough info to get the
5341 // bitfield right!
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005342 Ivar[4] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Anderson08e25242009-07-27 22:29:56 +00005343 Ivars.push_back(llvm::ConstantStruct::get(ObjCTypes.IvarnfABITy, Ivar));
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005344 }
5345 // Return null for empty list.
5346 if (Ivars.empty())
Owen Andersonc9c88b42009-07-31 20:28:54 +00005347 return llvm::Constant::getNullValue(ObjCTypes.IvarListnfABIPtrTy);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005348
5349 llvm::Constant *Values[3];
Duncan Sands9408c452009-05-09 07:08:47 +00005350 unsigned Size = CGM.getTargetData().getTypeAllocSize(ObjCTypes.IvarnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005351 Values[0] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
5352 Values[1] = llvm::ConstantInt::get(ObjCTypes.IntTy, Ivars.size());
Owen Anderson96e0fc72009-07-29 22:16:19 +00005353 llvm::ArrayType *AT = llvm::ArrayType::get(ObjCTypes.IvarnfABITy,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005354 Ivars.size());
Owen Anderson7db6d832009-07-28 18:33:04 +00005355 Values[2] = llvm::ConstantArray::get(AT, Ivars);
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005356 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005357 const char *Prefix = "\01l_OBJC_$_INSTANCE_VARIABLES_";
5358 llvm::GlobalVariable *GV =
Owen Anderson1c431b32009-07-08 19:05:04 +00005359 new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanian98abf4b2009-01-27 19:38:51 +00005360 llvm::GlobalValue::InternalLinkage,
5361 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005362 Prefix + OID->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005363 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005364 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Fariborz Jahanian1bf0afb2009-01-28 01:05:23 +00005365 GV->setSection("__DATA, __objc_const");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005366
Chris Lattnerad64e022009-07-17 23:57:13 +00005367 CGM.AddUsedGlobal(GV);
Owen Anderson3c4972d2009-07-29 18:54:39 +00005368 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.IvarListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005369}
5370
5371llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocolRef(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005372 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005373 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005374
Fariborz Jahanianda320092009-01-29 19:24:30 +00005375 if (!Entry) {
5376 // We use the initializer as a marker of whether this is a forward
5377 // reference or not. At module finalization we add the empty
5378 // contents for protocols which were referenced but never defined.
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005379 Entry =
5380 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy, false,
5381 llvm::GlobalValue::ExternalLinkage,
5382 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005383 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanianda320092009-01-29 19:24:30 +00005384 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005385 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005386
Fariborz Jahanianda320092009-01-29 19:24:30 +00005387 return Entry;
5388}
5389
5390/// GetOrEmitProtocol - Generate the protocol meta-data:
5391/// @code
5392/// struct _protocol_t {
5393/// id isa; // NULL
5394/// const char * const protocol_name;
5395/// const struct _protocol_list_t * protocol_list; // super protocols
5396/// const struct method_list_t * const instance_methods;
5397/// const struct method_list_t * const class_methods;
5398/// const struct method_list_t *optionalInstanceMethods;
5399/// const struct method_list_t *optionalClassMethods;
5400/// const struct _prop_list_t * properties;
5401/// const uint32_t size; // sizeof(struct _protocol_t)
5402/// const uint32_t flags; // = 0
Bob Wilsondc8dab62011-11-30 01:57:58 +00005403/// const char ** extendedMethodTypes;
Fariborz Jahanianda320092009-01-29 19:24:30 +00005404/// }
5405/// @endcode
5406///
5407
5408llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005409 const ObjCProtocolDecl *PD) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005410 llvm::GlobalVariable *&Entry = Protocols[PD->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005411
Fariborz Jahanianda320092009-01-29 19:24:30 +00005412 // Early exit if a defining object has already been generated.
5413 if (Entry && Entry->hasInitializer())
5414 return Entry;
5415
Fariborz Jahanianda320092009-01-29 19:24:30 +00005416 // Construct method lists.
5417 std::vector<llvm::Constant*> InstanceMethods, ClassMethods;
5418 std::vector<llvm::Constant*> OptInstanceMethods, OptClassMethods;
Bob Wilsondc8dab62011-11-30 01:57:58 +00005419 std::vector<llvm::Constant*> MethodTypesExt, OptMethodTypesExt;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005420 for (ObjCProtocolDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005421 i = PD->instmeth_begin(), e = PD->instmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005422 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005423 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005424 if (!C)
5425 return GetOrEmitProtocolRef(PD);
5426
Fariborz Jahanianda320092009-01-29 19:24:30 +00005427 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5428 OptInstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005429 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005430 } else {
5431 InstanceMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005432 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005433 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005434 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005435
5436 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00005437 i = PD->classmeth_begin(), e = PD->classmeth_end(); i != e; ++i) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005438 ObjCMethodDecl *MD = *i;
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005439 llvm::Constant *C = GetMethodDescriptionConstant(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005440 if (!C)
5441 return GetOrEmitProtocolRef(PD);
5442
Fariborz Jahanianda320092009-01-29 19:24:30 +00005443 if (MD->getImplementationControl() == ObjCMethodDecl::Optional) {
5444 OptClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005445 OptMethodTypesExt.push_back(GetMethodVarType(MD, true));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005446 } else {
5447 ClassMethods.push_back(C);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005448 MethodTypesExt.push_back(GetMethodVarType(MD, true));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005449 }
Fariborz Jahanianda320092009-01-29 19:24:30 +00005450 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005451
Bob Wilsondc8dab62011-11-30 01:57:58 +00005452 MethodTypesExt.insert(MethodTypesExt.end(),
5453 OptMethodTypesExt.begin(), OptMethodTypesExt.end());
5454
5455 llvm::Constant *Values[11];
Fariborz Jahanianda320092009-01-29 19:24:30 +00005456 // isa is NULL
Owen Andersonc9c88b42009-07-31 20:28:54 +00005457 Values[0] = llvm::Constant::getNullValue(ObjCTypes.ObjectPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005458 Values[1] = GetClassName(PD->getIdentifier());
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005459 Values[2] = EmitProtocolList("\01l_OBJC_$_PROTOCOL_REFS_" + PD->getName(),
5460 PD->protocol_begin(),
5461 PD->protocol_end());
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005462
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005463 Values[3] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005464 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005465 "__DATA, __objc_const",
5466 InstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005467 Values[4] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005468 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005469 "__DATA, __objc_const",
5470 ClassMethods);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005471 Values[5] = EmitMethodList("\01l_OBJC_$_PROTOCOL_INSTANCE_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005472 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005473 "__DATA, __objc_const",
5474 OptInstanceMethods);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005475 Values[6] = EmitMethodList("\01l_OBJC_$_PROTOCOL_CLASS_METHODS_OPT_"
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005476 + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005477 "__DATA, __objc_const",
5478 OptClassMethods);
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005479 Values[7] = EmitPropertyList("\01l_OBJC_$_PROP_LIST_" + PD->getName(),
Fariborz Jahanianda320092009-01-29 19:24:30 +00005480 0, PD, ObjCTypes);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005481 uint32_t Size =
Duncan Sands9408c452009-05-09 07:08:47 +00005482 CGM.getTargetData().getTypeAllocSize(ObjCTypes.ProtocolnfABITy);
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005483 Values[8] = llvm::ConstantInt::get(ObjCTypes.IntTy, Size);
Owen Andersonc9c88b42009-07-31 20:28:54 +00005484 Values[9] = llvm::Constant::getNullValue(ObjCTypes.IntTy);
Bob Wilsondc8dab62011-11-30 01:57:58 +00005485 Values[10] = EmitProtocolMethodTypes("\01l_OBJC_$_PROTOCOL_METHOD_TYPES_"
5486 + PD->getName(),
5487 MethodTypesExt, ObjCTypes);
Owen Anderson08e25242009-07-27 22:29:56 +00005488 llvm::Constant *Init = llvm::ConstantStruct::get(ObjCTypes.ProtocolnfABITy,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005489 Values);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005490
Fariborz Jahanianda320092009-01-29 19:24:30 +00005491 if (Entry) {
5492 // Already created, fix the linkage and update the initializer.
Mike Stump286acbd2009-03-07 16:33:28 +00005493 Entry->setLinkage(llvm::GlobalValue::WeakAnyLinkage);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005494 Entry->setInitializer(Init);
5495 } else {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005496 Entry =
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005497 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABITy,
5498 false, llvm::GlobalValue::WeakAnyLinkage, Init,
5499 "\01l_OBJC_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005500 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005501 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABITy));
Fariborz Jahanianda320092009-01-29 19:24:30 +00005502 Entry->setSection("__DATA,__datacoal_nt,coalesced");
Fariborz Jahanianda320092009-01-29 19:24:30 +00005503 }
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005504 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005505 CGM.AddUsedGlobal(Entry);
5506
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005507 // Use this protocol meta-data to build protocol list table in section
5508 // __DATA, __objc_protolist
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005509 llvm::GlobalVariable *PTGV =
5510 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ProtocolnfABIPtrTy,
5511 false, llvm::GlobalValue::WeakAnyLinkage, Entry,
5512 "\01l_OBJC_LABEL_PROTOCOL_$_" + PD->getName());
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005513 PTGV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005514 CGM.getTargetData().getABITypeAlignment(ObjCTypes.ProtocolnfABIPtrTy));
Daniel Dunbar0bf21992009-04-15 02:56:18 +00005515 PTGV->setSection("__DATA, __objc_protolist, coalesced, no_dead_strip");
Fariborz Jahanian8448c2c2009-01-29 20:10:59 +00005516 PTGV->setVisibility(llvm::GlobalValue::HiddenVisibility);
Chris Lattnerad64e022009-07-17 23:57:13 +00005517 CGM.AddUsedGlobal(PTGV);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005518 return Entry;
5519}
5520
5521/// EmitProtocolList - Generate protocol list meta-data:
5522/// @code
5523/// struct _protocol_list_t {
5524/// long protocol_count; // Note, this is 32/64 bit
5525/// struct _protocol_t[protocol_count];
5526/// }
5527/// @endcode
5528///
5529llvm::Constant *
Chris Lattner5f9e2722011-07-23 10:55:15 +00005530CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005531 ObjCProtocolDecl::protocol_iterator begin,
5532 ObjCProtocolDecl::protocol_iterator end) {
Fariborz Jahanianda320092009-01-29 19:24:30 +00005533 std::vector<llvm::Constant*> ProtocolRefs;
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005534
Fariborz Jahanianda320092009-01-29 19:24:30 +00005535 // Just return null for empty protocol lists
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005536 if (begin == end)
Owen Andersonc9c88b42009-07-31 20:28:54 +00005537 return llvm::Constant::getNullValue(ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005538
Daniel Dunbar948e2582009-02-15 07:36:20 +00005539 // FIXME: We shouldn't need to do this lookup here, should we?
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005540 llvm::SmallString<256> TmpName;
5541 Name.toVector(TmpName);
5542 llvm::GlobalVariable *GV =
5543 CGM.getModule().getGlobalVariable(TmpName.str(), true);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005544 if (GV)
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00005545 return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListnfABIPtrTy);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005546
Daniel Dunbar948e2582009-02-15 07:36:20 +00005547 for (; begin != end; ++begin)
5548 ProtocolRefs.push_back(GetProtocolRef(*begin)); // Implemented???
5549
Fariborz Jahanianda320092009-01-29 19:24:30 +00005550 // This list is null terminated.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005551 ProtocolRefs.push_back(llvm::Constant::getNullValue(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005552 ObjCTypes.ProtocolnfABIPtrTy));
5553
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005554 llvm::Constant *Values[2];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005555 Values[0] =
Owen Anderson4a28d5d2009-07-24 23:12:58 +00005556 llvm::ConstantInt::get(ObjCTypes.LongTy, ProtocolRefs.size() - 1);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005557 Values[1] =
Owen Anderson7db6d832009-07-28 18:33:04 +00005558 llvm::ConstantArray::get(
Owen Anderson96e0fc72009-07-29 22:16:19 +00005559 llvm::ArrayType::get(ObjCTypes.ProtocolnfABIPtrTy,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005560 ProtocolRefs.size()),
5561 ProtocolRefs);
5562
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005563 llvm::Constant *Init = llvm::ConstantStruct::getAnon(Values);
Owen Anderson1c431b32009-07-08 19:05:04 +00005564 GV = new llvm::GlobalVariable(CGM.getModule(), Init->getType(), false,
Fariborz Jahanianda320092009-01-29 19:24:30 +00005565 llvm::GlobalValue::InternalLinkage,
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005566 Init, Name);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005567 GV->setSection("__DATA, __objc_const");
Fariborz Jahanian09796d62009-01-31 02:43:27 +00005568 GV->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005569 CGM.getTargetData().getABITypeAlignment(Init->getType()));
Chris Lattnerad64e022009-07-17 23:57:13 +00005570 CGM.AddUsedGlobal(GV);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005571 return llvm::ConstantExpr::getBitCast(GV,
Daniel Dunbar948e2582009-02-15 07:36:20 +00005572 ObjCTypes.ProtocolListnfABIPtrTy);
Fariborz Jahanianda320092009-01-29 19:24:30 +00005573}
5574
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005575/// GetMethodDescriptionConstant - This routine build following meta-data:
5576/// struct _objc_method {
5577/// SEL _cmd;
5578/// char *method_type;
5579/// char *_imp;
5580/// }
5581
5582llvm::Constant *
5583CGObjCNonFragileABIMac::GetMethodDescriptionConstant(const ObjCMethodDecl *MD) {
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00005584 llvm::Constant *Desc[3];
Owen Andersona1cf15f2009-07-14 23:10:40 +00005585 Desc[0] =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005586 llvm::ConstantExpr::getBitCast(GetMethodVarName(MD->getSelector()),
5587 ObjCTypes.SelectorPtrTy);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005588 Desc[1] = GetMethodVarType(MD);
Douglas Gregorf968d832011-05-27 01:19:52 +00005589 if (!Desc[1])
5590 return 0;
5591
Fariborz Jahanian8cfd3972009-01-30 18:58:59 +00005592 // Protocol methods have no implementation. So, this entry is always NULL.
Owen Andersonc9c88b42009-07-31 20:28:54 +00005593 Desc[2] = llvm::Constant::getNullValue(ObjCTypes.Int8PtrTy);
Owen Anderson08e25242009-07-27 22:29:56 +00005594 return llvm::ConstantStruct::get(ObjCTypes.MethodTy, Desc);
Fariborz Jahanian3819a0b2009-01-30 00:46:37 +00005595}
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005596
5597/// EmitObjCValueForIvar - Code Gen for nonfragile ivar reference.
5598/// This code gen. amounts to generating code for:
5599/// @code
5600/// (type *)((char *)base + _OBJC_IVAR_$_.ivar;
5601/// @encode
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005602///
Fariborz Jahanian598d3f62009-02-03 19:03:09 +00005603LValue CGObjCNonFragileABIMac::EmitObjCValueForIvar(
John McCall506b57e2010-05-17 21:00:27 +00005604 CodeGen::CodeGenFunction &CGF,
5605 QualType ObjectTy,
5606 llvm::Value *BaseValue,
5607 const ObjCIvarDecl *Ivar,
5608 unsigned CVRQualifiers) {
5609 ObjCInterfaceDecl *ID = ObjectTy->getAs<ObjCObjectType>()->getInterface();
Daniel Dunbar97776872009-04-22 07:32:20 +00005610 return EmitValueForIvarAtOffset(CGF, ID, BaseValue, Ivar, CVRQualifiers,
5611 EmitIvarOffset(CGF, ID, Ivar));
Fariborz Jahanian45012a72009-02-03 00:09:52 +00005612}
5613
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005614llvm::Value *CGObjCNonFragileABIMac::EmitIvarOffset(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005615 CodeGen::CodeGenFunction &CGF,
5616 const ObjCInterfaceDecl *Interface,
5617 const ObjCIvarDecl *Ivar) {
Daniel Dunbar2da84ff2009-11-29 21:23:36 +00005618 return CGF.Builder.CreateLoad(ObjCIvarOffsetVariable(Interface, Ivar),"ivar");
Fariborz Jahanianf63aa3f2009-02-10 19:02:04 +00005619}
5620
John McCallb1e81442011-05-13 23:16:18 +00005621static void appendSelectorForMessageRefTable(std::string &buffer,
5622 Selector selector) {
5623 if (selector.isUnarySelector()) {
5624 buffer += selector.getNameForSlot(0);
5625 return;
5626 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005627
John McCallb1e81442011-05-13 23:16:18 +00005628 for (unsigned i = 0, e = selector.getNumArgs(); i != e; ++i) {
5629 buffer += selector.getNameForSlot(i);
5630 buffer += '_';
5631 }
5632}
5633
John McCall944c8432011-05-14 03:10:52 +00005634/// Emit a "v-table" message send. We emit a weak hidden-visibility
5635/// struct, initially containing the selector pointer and a pointer to
5636/// a "fixup" variant of the appropriate objc_msgSend. To call, we
5637/// load and call the function pointer, passing the address of the
5638/// struct as the second parameter. The runtime determines whether
5639/// the selector is currently emitted using vtable dispatch; if so, it
5640/// substitutes a stub function which simply tail-calls through the
5641/// appropriate vtable slot, and if not, it substitues a stub function
5642/// which tail-calls objc_msgSend. Both stubs adjust the selector
5643/// argument to correctly point to the selector.
5644RValue
5645CGObjCNonFragileABIMac::EmitVTableMessageSend(CodeGenFunction &CGF,
5646 ReturnValueSlot returnSlot,
5647 QualType resultType,
5648 Selector selector,
5649 llvm::Value *arg0,
5650 QualType arg0Type,
5651 bool isSuper,
5652 const CallArgList &formalArgs,
5653 const ObjCMethodDecl *method) {
John McCallb1e81442011-05-13 23:16:18 +00005654 // Compute the actual arguments.
5655 CallArgList args;
5656
John McCall944c8432011-05-14 03:10:52 +00005657 // First argument: the receiver / super-call structure.
John McCallb1e81442011-05-13 23:16:18 +00005658 if (!isSuper)
John McCall944c8432011-05-14 03:10:52 +00005659 arg0 = CGF.Builder.CreateBitCast(arg0, ObjCTypes.ObjectPtrTy);
5660 args.add(RValue::get(arg0), arg0Type);
John McCallb1e81442011-05-13 23:16:18 +00005661
John McCall944c8432011-05-14 03:10:52 +00005662 // Second argument: a pointer to the message ref structure. Leave
5663 // the actual argument value blank for now.
John McCallb1e81442011-05-13 23:16:18 +00005664 args.add(RValue::get(0), ObjCTypes.MessageRefCPtrTy);
5665
5666 args.insert(args.end(), formalArgs.begin(), formalArgs.end());
5667
5668 const CGFunctionInfo &fnInfo =
5669 CGM.getTypes().getFunctionInfo(resultType, args,
5670 FunctionType::ExtInfo());
5671
John McCallcba681a2011-05-14 21:12:11 +00005672 NullReturnState nullReturn;
5673
John McCall944c8432011-05-14 03:10:52 +00005674 // Find the function to call and the mangled name for the message
5675 // ref structure. Using a different mangled name wouldn't actually
5676 // be a problem; it would just be a waste.
5677 //
5678 // The runtime currently never uses vtable dispatch for anything
5679 // except normal, non-super message-sends.
5680 // FIXME: don't use this for that.
John McCallb1e81442011-05-13 23:16:18 +00005681 llvm::Constant *fn = 0;
5682 std::string messageRefName("\01l_");
5683 if (CGM.ReturnTypeUsesSRet(fnInfo)) {
John McCallb1e81442011-05-13 23:16:18 +00005684 if (isSuper) {
5685 fn = ObjCTypes.getMessageSendSuper2StretFixupFn();
5686 messageRefName += "objc_msgSendSuper2_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005687 } else {
John McCallcba681a2011-05-14 21:12:11 +00005688 nullReturn.init(CGF, arg0);
John McCallb1e81442011-05-13 23:16:18 +00005689 fn = ObjCTypes.getMessageSendStretFixupFn();
5690 messageRefName += "objc_msgSend_stret_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005691 }
John McCallb1e81442011-05-13 23:16:18 +00005692 } else if (!isSuper && CGM.ReturnTypeUsesFPRet(resultType)) {
5693 fn = ObjCTypes.getMessageSendFpretFixupFn();
5694 messageRefName += "objc_msgSend_fpret_fixup";
Mike Stumpb3589f42009-07-30 22:28:39 +00005695 } else {
John McCallb1e81442011-05-13 23:16:18 +00005696 if (isSuper) {
5697 fn = ObjCTypes.getMessageSendSuper2FixupFn();
5698 messageRefName += "objc_msgSendSuper2_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005699 } else {
John McCallb1e81442011-05-13 23:16:18 +00005700 fn = ObjCTypes.getMessageSendFixupFn();
5701 messageRefName += "objc_msgSend_fixup";
Chris Lattner9b7d6702010-08-18 16:09:06 +00005702 }
Fariborz Jahanian83a8a752009-02-04 20:42:28 +00005703 }
John McCallb1e81442011-05-13 23:16:18 +00005704 assert(fn && "CGObjCNonFragileABIMac::EmitMessageSend");
5705 messageRefName += '_';
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005706
John McCallb1e81442011-05-13 23:16:18 +00005707 // Append the selector name, except use underscores anywhere we
5708 // would have used colons.
5709 appendSelectorForMessageRefTable(messageRefName, selector);
5710
5711 llvm::GlobalVariable *messageRef
5712 = CGM.getModule().getGlobalVariable(messageRefName);
5713 if (!messageRef) {
John McCall944c8432011-05-14 03:10:52 +00005714 // Build the message ref structure.
John McCallb1e81442011-05-13 23:16:18 +00005715 llvm::Constant *values[] = { fn, GetMethodVarName(selector) };
Chris Lattnerc5cbb902011-06-20 04:01:35 +00005716 llvm::Constant *init = llvm::ConstantStruct::getAnon(values);
John McCallb1e81442011-05-13 23:16:18 +00005717 messageRef = new llvm::GlobalVariable(CGM.getModule(),
5718 init->getType(),
5719 /*constant*/ false,
5720 llvm::GlobalValue::WeakAnyLinkage,
5721 init,
5722 messageRefName);
5723 messageRef->setVisibility(llvm::GlobalValue::HiddenVisibility);
5724 messageRef->setAlignment(16);
5725 messageRef->setSection("__DATA, __objc_msgrefs, coalesced");
5726 }
5727 llvm::Value *mref =
5728 CGF.Builder.CreateBitCast(messageRef, ObjCTypes.MessageRefPtrTy);
5729
John McCall944c8432011-05-14 03:10:52 +00005730 // Update the message ref argument.
John McCallb1e81442011-05-13 23:16:18 +00005731 args[1].RV = RValue::get(mref);
5732
5733 // Load the function to call from the message ref table.
5734 llvm::Value *callee = CGF.Builder.CreateStructGEP(mref, 0);
5735 callee = CGF.Builder.CreateLoad(callee, "msgSend_fn");
5736
5737 bool variadic = method ? method->isVariadic() : false;
Chris Lattner2acc6e32011-07-18 04:24:23 +00005738 llvm::FunctionType *fnType =
John McCallb1e81442011-05-13 23:16:18 +00005739 CGF.getTypes().GetFunctionType(fnInfo, variadic);
5740 callee = CGF.Builder.CreateBitCast(callee,
5741 llvm::PointerType::getUnqual(fnType));
5742
John McCallcba681a2011-05-14 21:12:11 +00005743 RValue result = CGF.EmitCall(fnInfo, callee, returnSlot, args);
5744 return nullReturn.complete(CGF, result, resultType);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005745}
5746
5747/// Generate code for a message send expression in the nonfragile abi.
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005748CodeGen::RValue
5749CGObjCNonFragileABIMac::GenerateMessageSend(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005750 ReturnValueSlot Return,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005751 QualType ResultType,
5752 Selector Sel,
5753 llvm::Value *Receiver,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005754 const CallArgList &CallArgs,
David Chisnallc6cd5fd2010-04-28 19:33:36 +00005755 const ObjCInterfaceDecl *Class,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005756 const ObjCMethodDecl *Method) {
John McCall944c8432011-05-14 03:10:52 +00005757 return isVTableDispatchedSelector(Sel)
5758 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005759 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005760 false, CallArgs, Method)
5761 : EmitMessageSend(CGF, Return, ResultType,
5762 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005763 Receiver, CGF.getContext().getObjCIdType(),
John McCall944c8432011-05-14 03:10:52 +00005764 false, CallArgs, Method, ObjCTypes);
Fariborz Jahanian46551122009-02-04 00:22:57 +00005765}
5766
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005767llvm::GlobalVariable *
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005768CGObjCNonFragileABIMac::GetClassGlobal(const std::string &Name) {
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005769 llvm::GlobalVariable *GV = CGM.getModule().getGlobalVariable(Name);
5770
Daniel Dunbardfff2302009-03-02 05:18:14 +00005771 if (!GV) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005772 GV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABITy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005773 false, llvm::GlobalValue::ExternalLinkage,
5774 0, Name);
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005775 }
5776
5777 return GV;
5778}
5779
John McCallf85e1932011-06-15 23:02:42 +00005780llvm::Value *CGObjCNonFragileABIMac::EmitClassRefFromId(CGBuilderTy &Builder,
5781 IdentifierInfo *II) {
5782 llvm::GlobalVariable *&Entry = ClassReferences[II];
5783
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005784 if (!Entry) {
John McCallf85e1932011-06-15 23:02:42 +00005785 std::string ClassName(getClassSymbolPrefix() + II->getName().str());
Daniel Dunbar5a7379a2009-03-01 04:40:10 +00005786 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005787 Entry =
John McCallf85e1932011-06-15 23:02:42 +00005788 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
5789 false, llvm::GlobalValue::InternalLinkage,
5790 ClassGV,
5791 "\01L_OBJC_CLASSLIST_REFERENCES_$_");
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005792 Entry->setAlignment(
John McCallf85e1932011-06-15 23:02:42 +00005793 CGM.getTargetData().getABITypeAlignment(
5794 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005795 Entry->setSection("__DATA, __objc_classrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005796 CGM.AddUsedGlobal(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005797 }
John McCallf85e1932011-06-15 23:02:42 +00005798
Benjamin Kramer578faa82011-09-27 21:06:10 +00005799 return Builder.CreateLoad(Entry);
Daniel Dunbar11394522009-04-18 08:51:00 +00005800}
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005801
John McCallf85e1932011-06-15 23:02:42 +00005802llvm::Value *CGObjCNonFragileABIMac::EmitClassRef(CGBuilderTy &Builder,
5803 const ObjCInterfaceDecl *ID) {
5804 return EmitClassRefFromId(Builder, ID->getIdentifier());
5805}
5806
5807llvm::Value *CGObjCNonFragileABIMac::EmitNSAutoreleasePoolClassRef(
5808 CGBuilderTy &Builder) {
5809 IdentifierInfo *II = &CGM.getContext().Idents.get("NSAutoreleasePool");
5810 return EmitClassRefFromId(Builder, II);
5811}
5812
Daniel Dunbar11394522009-04-18 08:51:00 +00005813llvm::Value *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005814CGObjCNonFragileABIMac::EmitSuperClassRef(CGBuilderTy &Builder,
Daniel Dunbar11394522009-04-18 08:51:00 +00005815 const ObjCInterfaceDecl *ID) {
5816 llvm::GlobalVariable *&Entry = SuperClassReferences[ID->getIdentifier()];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005817
Daniel Dunbar11394522009-04-18 08:51:00 +00005818 if (!Entry) {
5819 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5820 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005821 Entry =
5822 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy,
Owen Anderson1c431b32009-07-08 19:05:04 +00005823 false, llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005824 ClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005825 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Daniel Dunbar11394522009-04-18 08:51:00 +00005826 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005827 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005828 ObjCTypes.ClassnfABIPtrTy));
Daniel Dunbar11394522009-04-18 08:51:00 +00005829 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005830 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005831 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005832
Benjamin Kramer578faa82011-09-27 21:06:10 +00005833 return Builder.CreateLoad(Entry);
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005834}
5835
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005836/// EmitMetaClassRef - Return a Value * of the address of _class_t
5837/// meta-data
5838///
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005839llvm::Value *CGObjCNonFragileABIMac::EmitMetaClassRef(CGBuilderTy &Builder,
5840 const ObjCInterfaceDecl *ID) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005841 llvm::GlobalVariable * &Entry = MetaClassReferences[ID->getIdentifier()];
5842 if (Entry)
Benjamin Kramer578faa82011-09-27 21:06:10 +00005843 return Builder.CreateLoad(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005844
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00005845 std::string MetaClassName(getMetaclassSymbolPrefix() + ID->getNameAsString());
Fariborz Jahanian0f902942009-04-14 18:41:56 +00005846 llvm::GlobalVariable *MetaClassGV = GetClassGlobal(MetaClassName);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005847 Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00005848 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.ClassnfABIPtrTy, false,
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005849 llvm::GlobalValue::InternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005850 MetaClassGV,
Owen Anderson1c431b32009-07-08 19:05:04 +00005851 "\01L_OBJC_CLASSLIST_SUP_REFS_$_");
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005852 Entry->setAlignment(
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00005853 CGM.getTargetData().getABITypeAlignment(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005854 ObjCTypes.ClassnfABIPtrTy));
5855
Daniel Dunbar33af70f2009-04-15 19:03:14 +00005856 Entry->setSection("__DATA, __objc_superrefs, regular, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005857 CGM.AddUsedGlobal(Entry);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005858
Benjamin Kramer578faa82011-09-27 21:06:10 +00005859 return Builder.CreateLoad(Entry);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005860}
5861
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005862/// GetClass - Return a reference to the class for the given interface
5863/// decl.
5864llvm::Value *CGObjCNonFragileABIMac::GetClass(CGBuilderTy &Builder,
5865 const ObjCInterfaceDecl *ID) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00005866 if (ID->isWeakImported()) {
Fariborz Jahanian269f8bc2009-11-17 22:42:00 +00005867 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
5868 llvm::GlobalVariable *ClassGV = GetClassGlobal(ClassName);
5869 ClassGV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
5870 }
5871
Fariborz Jahanian0e81f4b2009-02-05 20:41:40 +00005872 return EmitClassRef(Builder, ID);
5873}
5874
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005875/// Generates a message send where the super is the receiver. This is
5876/// a message send to self with special delivery semantics indicating
5877/// which class's method should be called.
5878CodeGen::RValue
5879CGObjCNonFragileABIMac::GenerateMessageSendSuper(CodeGen::CodeGenFunction &CGF,
John McCallef072fd2010-05-22 01:48:05 +00005880 ReturnValueSlot Return,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005881 QualType ResultType,
5882 Selector Sel,
5883 const ObjCInterfaceDecl *Class,
5884 bool isCategoryImpl,
5885 llvm::Value *Receiver,
5886 bool IsClassMessage,
Daniel Dunbard6c93d72009-09-17 04:01:22 +00005887 const CodeGen::CallArgList &CallArgs,
5888 const ObjCMethodDecl *Method) {
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005889 // ...
5890 // Create and init a super structure; this is a (receiver, class)
5891 // pair we will pass to objc_msgSendSuper.
5892 llvm::Value *ObjCSuper =
John McCall604da292011-03-04 08:00:29 +00005893 CGF.CreateTempAlloca(ObjCTypes.SuperTy, "objc_super");
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005894
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005895 llvm::Value *ReceiverAsObject =
5896 CGF.Builder.CreateBitCast(Receiver, ObjCTypes.ObjectPtrTy);
5897 CGF.Builder.CreateStore(ReceiverAsObject,
5898 CGF.Builder.CreateStructGEP(ObjCSuper, 0));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005899
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005900 // If this is a class message the metaclass is passed as the target.
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005901 llvm::Value *Target;
5902 if (IsClassMessage) {
5903 if (isCategoryImpl) {
5904 // Message sent to "super' in a class method defined in
5905 // a category implementation.
Daniel Dunbar11394522009-04-18 08:51:00 +00005906 Target = EmitClassRef(CGF.Builder, Class);
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005907 Target = CGF.Builder.CreateStructGEP(Target, 0);
5908 Target = CGF.Builder.CreateLoad(Target);
Mike Stumpb3589f42009-07-30 22:28:39 +00005909 } else
Fariborz Jahanian7ce77922009-02-28 20:07:56 +00005910 Target = EmitMetaClassRef(CGF.Builder, Class);
Mike Stumpb3589f42009-07-30 22:28:39 +00005911 } else
Daniel Dunbar11394522009-04-18 08:51:00 +00005912 Target = EmitSuperClassRef(CGF.Builder, Class);
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005913
Mike Stumpf5408fe2009-05-16 07:57:57 +00005914 // FIXME: We shouldn't need to do this cast, rectify the ASTContext and
5915 // ObjCTypes types.
Chris Lattner2acc6e32011-07-18 04:24:23 +00005916 llvm::Type *ClassTy =
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005917 CGM.getTypes().ConvertType(CGF.getContext().getObjCClassType());
5918 Target = CGF.Builder.CreateBitCast(Target, ClassTy);
5919 CGF.Builder.CreateStore(Target,
5920 CGF.Builder.CreateStructGEP(ObjCSuper, 1));
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005921
John McCall944c8432011-05-14 03:10:52 +00005922 return (isVTableDispatchedSelector(Sel))
5923 ? EmitVTableMessageSend(CGF, Return, ResultType, Sel,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005924 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00005925 true, CallArgs, Method)
5926 : EmitMessageSend(CGF, Return, ResultType,
5927 EmitSelector(CGF.Builder, Sel),
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005928 ObjCSuper, ObjCTypes.SuperPtrCTy,
John McCall944c8432011-05-14 03:10:52 +00005929 true, CallArgs, Method, ObjCTypes);
Fariborz Jahanian7a06aae2009-02-06 20:09:23 +00005930}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005931
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005932llvm::Value *CGObjCNonFragileABIMac::EmitSelector(CGBuilderTy &Builder,
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005933 Selector Sel, bool lval) {
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005934 llvm::GlobalVariable *&Entry = SelectorReferences[Sel];
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005935
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005936 if (!Entry) {
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005937 llvm::Constant *Casted =
5938 llvm::ConstantExpr::getBitCast(GetMethodVarName(Sel),
5939 ObjCTypes.SelectorPtrTy);
5940 Entry =
5941 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.SelectorPtrTy, false,
5942 llvm::GlobalValue::InternalLinkage,
5943 Casted, "\01L_OBJC_SELECTOR_REFERENCES_");
Fariborz Jahaniand0f8a8d2009-05-11 19:25:47 +00005944 Entry->setSection("__DATA, __objc_selrefs, literal_pointers, no_dead_strip");
Chris Lattnerad64e022009-07-17 23:57:13 +00005945 CGM.AddUsedGlobal(Entry);
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005946 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005947
Fariborz Jahanian03b29602010-06-17 19:56:20 +00005948 if (lval)
5949 return Entry;
Pete Cooperb6d71142011-11-10 21:45:06 +00005950 llvm::LoadInst* LI = Builder.CreateLoad(Entry);
5951
5952 LI->setMetadata(CGM.getModule().getMDKindID("invariant.load"),
5953 llvm::MDNode::get(VMContext,
5954 ArrayRef<llvm::Value*>()));
5955 return LI;
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00005956}
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005957/// EmitObjCIvarAssign - Code gen for assigning to a __strong object.
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005958/// objc_assign_ivar (id src, id *dst, ptrdiff_t)
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005959///
5960void CGObjCNonFragileABIMac::EmitObjCIvarAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005961 llvm::Value *src,
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005962 llvm::Value *dst,
5963 llvm::Value *ivarOffset) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005964 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005965 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005966 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005967 assert(Size <= 8 && "does not support size > 8");
5968 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
5969 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005970 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5971 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005972 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5973 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian6c7a1f32009-09-24 22:25:38 +00005974 CGF.Builder.CreateCall3(ObjCTypes.getGcAssignIvarFn(),
5975 src, dst, ivarOffset);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005976 return;
5977}
5978
5979/// EmitObjCStrongCastAssign - Code gen for assigning to a __strong cast object.
5980/// objc_assign_strongCast (id src, id *dst)
5981///
5982void CGObjCNonFragileABIMac::EmitObjCStrongCastAssign(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005983 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00005984 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00005985 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005986 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00005987 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00005988 assert(Size <= 8 && "does not support size > 8");
5989 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00005990 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00005991 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
5992 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005993 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
5994 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattnerbbccd612009-04-22 02:38:11 +00005995 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignStrongCastFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00005996 src, dst, "weakassign");
5997 return;
5998}
5999
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006000void CGObjCNonFragileABIMac::EmitGCMemmoveCollectable(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006001 CodeGen::CodeGenFunction &CGF,
6002 llvm::Value *DestPtr,
6003 llvm::Value *SrcPtr,
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006004 llvm::Value *Size) {
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006005 SrcPtr = CGF.Builder.CreateBitCast(SrcPtr, ObjCTypes.Int8PtrTy);
6006 DestPtr = CGF.Builder.CreateBitCast(DestPtr, ObjCTypes.Int8PtrTy);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006007 CGF.Builder.CreateCall3(ObjCTypes.GcMemmoveCollectableFn(),
Fariborz Jahanian55bcace2010-06-15 22:44:06 +00006008 DestPtr, SrcPtr, Size);
Fariborz Jahanian082b02e2009-07-08 01:18:33 +00006009 return;
6010}
6011
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006012/// EmitObjCWeakRead - Code gen for loading value of a __weak
6013/// object: objc_read_weak (id *src)
6014///
6015llvm::Value * CGObjCNonFragileABIMac::EmitObjCWeakRead(
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006016 CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006017 llvm::Value *AddrWeakObj) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006018 llvm::Type* DestTy =
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006019 cast<llvm::PointerType>(AddrWeakObj->getType())->getElementType();
6020 AddrWeakObj = CGF.Builder.CreateBitCast(AddrWeakObj, ObjCTypes.PtrObjectPtrTy);
Chris Lattner72db6c32009-04-22 02:44:54 +00006021 llvm::Value *read_weak = CGF.Builder.CreateCall(ObjCTypes.getGcReadWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006022 AddrWeakObj, "weakread");
Eli Friedman8339b352009-03-07 03:57:15 +00006023 read_weak = CGF.Builder.CreateBitCast(read_weak, DestTy);
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006024 return read_weak;
6025}
6026
6027/// EmitObjCWeakAssign - Code gen for assigning to a __weak object.
6028/// objc_assign_weak (id src, id *dst)
6029///
6030void CGObjCNonFragileABIMac::EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF,
Mike Stump1eb44332009-09-09 15:08:12 +00006031 llvm::Value *src, llvm::Value *dst) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006032 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006033 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006034 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006035 assert(Size <= 8 && "does not support size > 8");
6036 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6037 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006038 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6039 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006040 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6041 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Chris Lattner96508e12009-04-17 22:12:36 +00006042 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignWeakFn(),
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006043 src, dst, "weakassign");
6044 return;
6045}
6046
6047/// EmitObjCGlobalAssign - Code gen for assigning to a __strong object.
6048/// objc_assign_global (id src, id *dst)
6049///
6050void CGObjCNonFragileABIMac::EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF,
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006051 llvm::Value *src, llvm::Value *dst,
6052 bool threadlocal) {
Chris Lattner2acc6e32011-07-18 04:24:23 +00006053 llvm::Type * SrcTy = src->getType();
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006054 if (!isa<llvm::PointerType>(SrcTy)) {
Duncan Sands9408c452009-05-09 07:08:47 +00006055 unsigned Size = CGM.getTargetData().getTypeAllocSize(SrcTy);
Fariborz Jahanian0a855d02009-03-23 19:10:40 +00006056 assert(Size <= 8 && "does not support size > 8");
6057 src = (Size == 4 ? CGF.Builder.CreateBitCast(src, ObjCTypes.IntTy)
6058 : CGF.Builder.CreateBitCast(src, ObjCTypes.LongTy));
Fariborz Jahanian3b8a6522009-03-13 00:42:52 +00006059 src = CGF.Builder.CreateIntToPtr(src, ObjCTypes.Int8PtrTy);
6060 }
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006061 src = CGF.Builder.CreateBitCast(src, ObjCTypes.ObjectPtrTy);
6062 dst = CGF.Builder.CreateBitCast(dst, ObjCTypes.PtrObjectPtrTy);
Fariborz Jahanian021a7a62010-07-20 20:30:03 +00006063 if (!threadlocal)
6064 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignGlobalFn(),
6065 src, dst, "globalassign");
6066 else
6067 CGF.Builder.CreateCall2(ObjCTypes.getGcAssignThreadLocalFn(),
6068 src, dst, "threadlocalassign");
Fariborz Jahanian6948aea2009-02-16 22:52:32 +00006069 return;
6070}
Fariborz Jahanian26cc89f2009-02-11 20:51:17 +00006071
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006072void
John McCallf1549f62010-07-06 01:34:17 +00006073CGObjCNonFragileABIMac::EmitSynchronizedStmt(CodeGen::CodeGenFunction &CGF,
6074 const ObjCAtSynchronizedStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006075 EmitAtSynchronizedStmt(CGF, S,
6076 cast<llvm::Function>(ObjCTypes.getSyncEnterFn()),
6077 cast<llvm::Function>(ObjCTypes.getSyncExitFn()));
John McCallf1549f62010-07-06 01:34:17 +00006078}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006079
John McCall5a180392010-07-24 00:37:23 +00006080llvm::Constant *
Fariborz Jahaniancf5abc72011-06-23 19:00:08 +00006081CGObjCNonFragileABIMac::GetEHType(QualType T) {
John McCall5a180392010-07-24 00:37:23 +00006082 // There's a particular fixed type info for 'id'.
6083 if (T->isObjCIdType() ||
6084 T->isObjCQualifiedIdType()) {
6085 llvm::Constant *IDEHType =
6086 CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
6087 if (!IDEHType)
6088 IDEHType =
6089 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy,
6090 false,
6091 llvm::GlobalValue::ExternalLinkage,
6092 0, "OBJC_EHTYPE_id");
6093 return IDEHType;
6094 }
6095
6096 // All other types should be Objective-C interface pointer types.
6097 const ObjCObjectPointerType *PT =
6098 T->getAs<ObjCObjectPointerType>();
6099 assert(PT && "Invalid @catch type.");
6100 const ObjCInterfaceType *IT = PT->getInterfaceType();
6101 assert(IT && "Invalid @catch type.");
6102 return GetInterfaceEHType(IT->getDecl(), false);
6103}
6104
John McCallf1549f62010-07-06 01:34:17 +00006105void CGObjCNonFragileABIMac::EmitTryStmt(CodeGen::CodeGenFunction &CGF,
6106 const ObjCAtTryStmt &S) {
David Chisnall05dc91b2011-03-25 17:46:35 +00006107 EmitTryCatchStmt(CGF, S,
6108 cast<llvm::Function>(ObjCTypes.getObjCBeginCatchFn()),
6109 cast<llvm::Function>(ObjCTypes.getObjCEndCatchFn()),
6110 cast<llvm::Function>(ObjCTypes.getExceptionRethrowFn()));
Daniel Dunbar8ecbaf22009-02-24 07:47:38 +00006111}
6112
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006113/// EmitThrowStmt - Generate code for a throw statement.
6114void CGObjCNonFragileABIMac::EmitThrowStmt(CodeGen::CodeGenFunction &CGF,
6115 const ObjCAtThrowStmt &S) {
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006116 if (const Expr *ThrowExpr = S.getThrowExpr()) {
John McCall2b014d62011-10-01 10:32:24 +00006117 llvm::Value *Exception = CGF.EmitObjCThrowOperand(ThrowExpr);
Benjamin Kramer578faa82011-09-27 21:06:10 +00006118 Exception = CGF.Builder.CreateBitCast(Exception, ObjCTypes.ObjectPtrTy);
Jay Foad4c7d9f12011-07-15 08:37:34 +00006119 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionThrowFn(), Exception)
John McCall7ec404c2010-10-16 08:21:07 +00006120 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006121 } else {
Jay Foad4c7d9f12011-07-15 08:37:34 +00006122 CGF.EmitCallOrInvoke(ObjCTypes.getExceptionRethrowFn())
John McCall7ec404c2010-10-16 08:21:07 +00006123 .setDoesNotReturn();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006124 }
6125
John McCall7ec404c2010-10-16 08:21:07 +00006126 CGF.Builder.CreateUnreachable();
Anders Carlssonf57c5b22009-02-16 22:59:18 +00006127 CGF.Builder.ClearInsertionPoint();
6128}
Daniel Dunbare588b992009-03-01 04:46:24 +00006129
John McCall5a180392010-07-24 00:37:23 +00006130llvm::Constant *
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006131CGObjCNonFragileABIMac::GetInterfaceEHType(const ObjCInterfaceDecl *ID,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006132 bool ForDefinition) {
Daniel Dunbare588b992009-03-01 04:46:24 +00006133 llvm::GlobalVariable * &Entry = EHTypeReferences[ID->getIdentifier()];
Daniel Dunbare588b992009-03-01 04:46:24 +00006134
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006135 // If we don't need a definition, return the entry if found or check
6136 // if we use an external reference.
6137 if (!ForDefinition) {
6138 if (Entry)
6139 return Entry;
Daniel Dunbar7e075cb2009-04-07 06:43:45 +00006140
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006141 // If this type (or a super class) has the __objc_exception__
6142 // attribute, emit an external reference.
Douglas Gregor68584ed2009-06-18 16:11:24 +00006143 if (hasObjCExceptionAttribute(CGM.getContext(), ID))
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006144 return Entry =
Owen Anderson1c431b32009-07-08 19:05:04 +00006145 new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006146 llvm::GlobalValue::ExternalLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006147 0,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006148 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006149 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006150 }
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006151
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006152 // Otherwise we need to either make a new entry or fill in the
6153 // initializer.
6154 assert((!Entry || !Entry->hasInitializer()) && "Duplicate EHType definition");
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006155 std::string ClassName(getClassSymbolPrefix() + ID->getNameAsString());
Daniel Dunbare588b992009-03-01 04:46:24 +00006156 std::string VTableName = "objc_ehtype_vtable";
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006157 llvm::GlobalVariable *VTableGV =
Daniel Dunbare588b992009-03-01 04:46:24 +00006158 CGM.getModule().getGlobalVariable(VTableName);
6159 if (!VTableGV)
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006160 VTableGV = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.Int8PtrTy,
6161 false,
Daniel Dunbare588b992009-03-01 04:46:24 +00006162 llvm::GlobalValue::ExternalLinkage,
Owen Anderson1c431b32009-07-08 19:05:04 +00006163 0, VTableName);
Daniel Dunbare588b992009-03-01 04:46:24 +00006164
Chris Lattner77b89b82010-06-27 07:15:29 +00006165 llvm::Value *VTableIdx =
6166 llvm::ConstantInt::get(llvm::Type::getInt32Ty(VMContext), 2);
Daniel Dunbare588b992009-03-01 04:46:24 +00006167
Benjamin Kramer1d236ab2011-10-15 12:20:02 +00006168 llvm::Constant *Values[] = {
6169 llvm::ConstantExpr::getGetElementPtr(VTableGV, VTableIdx),
6170 GetClassName(ID->getIdentifier()),
6171 GetClassGlobal(ClassName)
6172 };
Owen Andersona1cf15f2009-07-14 23:10:40 +00006173 llvm::Constant *Init =
Owen Anderson08e25242009-07-27 22:29:56 +00006174 llvm::ConstantStruct::get(ObjCTypes.EHTypeTy, Values);
Daniel Dunbare588b992009-03-01 04:46:24 +00006175
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006176 if (Entry) {
6177 Entry->setInitializer(Init);
6178 } else {
Owen Anderson1c431b32009-07-08 19:05:04 +00006179 Entry = new llvm::GlobalVariable(CGM.getModule(), ObjCTypes.EHTypeTy, false,
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006180 llvm::GlobalValue::WeakAnyLinkage,
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006181 Init,
Daniel Dunbar9c29bf52009-10-18 20:48:59 +00006182 ("OBJC_EHTYPE_$_" +
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00006183 ID->getIdentifier()->getName()));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006184 }
6185
John McCall1fb0caa2010-10-22 21:05:15 +00006186 if (CGM.getLangOptions().getVisibilityMode() == HiddenVisibility)
Daniel Dunbar6ab187a2009-04-07 05:48:37 +00006187 Entry->setVisibility(llvm::GlobalValue::HiddenVisibility);
Daniel Dunbar4b429ae2010-04-25 20:39:32 +00006188 Entry->setAlignment(CGM.getTargetData().getABITypeAlignment(
6189 ObjCTypes.EHTypeTy));
Daniel Dunbar8158a2f2009-04-08 04:21:03 +00006190
6191 if (ForDefinition) {
6192 Entry->setSection("__DATA,__objc_const");
6193 Entry->setLinkage(llvm::GlobalValue::ExternalLinkage);
6194 } else {
6195 Entry->setSection("__DATA,__datacoal_nt,coalesced");
6196 }
Daniel Dunbare588b992009-03-01 04:46:24 +00006197
6198 return Entry;
6199}
Daniel Dunbar6bff2512009-08-03 17:06:42 +00006200
Daniel Dunbarbbce49b2008-08-12 00:12:39 +00006201/* *** */
6202
Daniel Dunbar6efc0c52008-08-13 03:21:16 +00006203CodeGen::CGObjCRuntime *
6204CodeGen::CreateMacObjCRuntime(CodeGen::CodeGenModule &CGM) {
David Chisnall60be6072011-03-22 21:21:24 +00006205 if (CGM.getLangOptions().ObjCNonFragileABI)
6206 return new CGObjCNonFragileABIMac(CGM);
Daniel Dunbarc17a4d32008-08-11 02:45:11 +00006207 return new CGObjCMac(CGM);
6208}